4 using System.Net.Sockets;
5 using System.Threading;
6 using System.Collections.Generic;
8 using System.Diagnostics;
9 using Mono.Cecil.Metadata;
11 namespace Mono.Debugger.Soft
13 public class VersionInfo {
14 public string VMVersion {
18 public int MajorVersion {
22 public int MinorVersion {
27 * Check that this version is at least major:minor
29 public bool AtLeast (int major, int minor) {
30 if ((MajorVersion > major) || ((MajorVersion == major && MinorVersion >= minor)))
38 public string source_file;
39 public byte[] guid, hash;
43 public int max_il_offset;
44 public int[] il_offsets;
45 public int[] line_numbers;
46 public int[] column_numbers;
47 public SourceInfo[] source_files;
54 public StackFrameFlags flags;
58 public string ns, name, full_name;
59 public long assembly, module, base_type, element_type;
60 public int token, rank, attributes;
61 public bool is_byref, is_pointer, is_primitive, is_valuetype, is_enum;
62 public bool is_gtd, is_generic_type;
65 public long[] type_args;
70 public long[] iface_methods;
71 public long[] target_methods;
75 public int attributes, iattributes, token;
76 public bool is_gmd, is_generic_method;
78 public long[] type_args;
81 class MethodBodyInfo {
83 public ExceptionClauseInfo[] clauses;
86 struct ExceptionClauseInfo {
87 public ExceptionClauseFlags flags;
88 public int try_offset;
89 public int try_length;
90 public int handler_offset;
91 public int handler_length;
92 public int filter_offset;
93 public long catch_type_id;
97 enum ExceptionClauseFlags {
105 public int call_conv;
106 public int param_count;
107 public int generic_param_count;
108 public long ret_type;
109 public long[] param_types;
110 public string[] param_names;
115 public string[] names;
116 public int[] live_range_start;
117 public int[] live_range_end;
123 public long get_method, set_method;
127 class CattrNamedArgInfo {
128 public bool is_property;
130 public ValueImpl value;
135 public ValueImpl[] ctor_args;
136 public CattrNamedArgInfo[] named_args;
140 public bool is_thread_pool;
143 struct ObjectRefInfo {
145 public long domain_id;
149 VALUE_TYPE_ID_NULL = 0xf0,
150 VALUE_TYPE_ID_TYPE = 0xf1
156 DISABLE_BREAKPOINTS = 0x1,
157 SINGLE_THREADED = 0x2
203 public ElementType Type; /* or one of the VALUE_TYPE_ID constants */
206 public long Klass; // For ElementType.ValueType
207 public ValueImpl[] Fields; // for ElementType.ValueType
208 public bool IsEnum; // For ElementType.ValueType
209 public long Id; /* For VALUE_TYPE_ID_TYPE */
213 public string Name, ScopeName, FQName, Guid;
214 public long Assembly;
226 enum StackFrameFlags {
229 NATIVE_TRANSITION = 2
232 class ResolvedToken {
233 public TokenType Type;
241 class CountModifier : Modifier {
247 class LocationModifier : Modifier {
252 public long Location {
257 class StepModifier : Modifier {
275 class ThreadModifier : Modifier {
281 class ExceptionModifier : Modifier {
288 public bool Uncaught {
291 public bool Subclasses {
296 class AssemblyModifier : Modifier {
297 public long[] Assemblies {
302 class SourceFileModifier : Modifier {
303 public string[] SourceFiles {
308 class TypeNameModifier : Modifier {
309 public string[] TypeNames {
315 public EventType EventType {
323 public SuspendPolicy SuspendPolicy {
327 public long ThreadId {
335 public long Location {
343 public string Category {
347 public string Message {
351 public EventInfo (EventType type, int req_id) {
357 public enum ErrorCode {
360 INVALID_FIELDID = 25,
361 INVALID_FRAMEID = 30,
362 NOT_IMPLEMENTED = 100,
364 INVALID_ARGUMENT = 102,
366 ERR_NO_INVOCATION = 104,
367 ABSENT_INFORMATION = 105,
368 NO_SEQ_POINT_AT_IL_OFFSET = 106
371 public class ErrorHandlerEventArgs : EventArgs {
373 public ErrorCode ErrorCode {
379 * Represents the connection to the debuggee
381 public abstract class Connection
384 * The protocol and the packet format is based on JDWP, the differences
385 * are in the set of supported events, and the commands.
387 internal const string HANDSHAKE_STRING = "DWP-Handshake";
389 internal const int HEADER_LENGTH = 11;
391 static readonly bool EnableConnectionLogging = !String.IsNullOrEmpty (Environment.GetEnvironmentVariable ("MONO_SDB_LOG"));
392 static int ConnectionId;
393 readonly StreamWriter LoggingStream = EnableConnectionLogging ?
394 new StreamWriter (string.Format ("/tmp/sdb_conn_log_{0}", ConnectionId++), false) : null;
397 * Th version of the wire-protocol implemented by the library. The library
398 * and the debuggee can communicate if they implement the same major version.
399 * If they implement a different minor version, they can communicate, but some
400 * features might not be available. This allows older clients to communicate
401 * with newer runtimes, and vice versa.
403 internal const int MAJOR_VERSION = 2;
404 internal const int MINOR_VERSION = 26;
406 enum WPSuspendPolicy {
433 APPDOMAIN_CREATE = 4, // Not in JDI
434 APPDOMAIN_UNLOAD = 5, // Not in JDI
455 SOURCE_FILE_ONLY = 12,
467 SET_PROTOCOL_VERSION = 8,
470 GET_TYPES_FOR_SOURCE_FILE = 11,
484 /* FIXME: Merge into GET_INFO when the major protocol version is increased */
490 enum CmdEventRequest {
493 CLEAR_ALL_BREAKPOINTS = 3
498 GET_FRIENDLY_NAME = 2,
500 GET_ENTRY_ASSEMBLY = 4,
503 CREATE_BOXED_VALUE = 7
509 GET_MANIFEST_MODULE = 3,
521 GET_DECLARING_TYPE = 2,
529 MAKE_GENERIC_METHOD = 10
538 GET_SOURCE_FILES = 6,
540 IS_ASSIGNABLE_FROM = 8,
543 GET_FIELD_CATTRS = 11,
544 GET_PROPERTY_CATTRS = 12,
545 /* FIXME: Merge into GET_SOURCE_FILES when the major protocol version is increased */
546 GET_SOURCE_FILES_2 = 13,
547 /* FIXME: Merge into GET_VALUES when the major protocol version is increased */
549 CMD_TYPE_GET_METHODS_BY_NAME_FLAGS = 15,
551 GET_INTERFACE_MAP = 17,
556 enum BindingFlagsExtensions {
557 BINDING_FLAGS_IGNORE_CASE = 0x70000000,
590 public int command_set;
595 internal static int GetPacketLength (byte[] header) {
597 return decode_int (header, ref offset);
600 internal static bool IsReplyPacket (byte[] packet) {
602 return decode_byte (packet, ref offset) == 0x80;
605 internal static int GetPacketId (byte[] packet) {
607 return decode_int (packet, ref offset);
610 static int decode_byte (byte[] packet, ref int offset) {
611 return packet [offset++];
614 static int decode_short (byte[] packet, ref int offset) {
615 int res = ((int)packet [offset] << 8) | (int)packet [offset + 1];
620 static int decode_int (byte[] packet, ref int offset) {
621 int res = ((int)packet [offset] << 24) | ((int)packet [offset + 1] << 16) | ((int)packet [offset + 2] << 8) | (int)packet [offset + 3];
626 static long decode_id (byte[] packet, ref int offset) {
627 return decode_int (packet, ref offset);
630 static long decode_long (byte[] packet, ref int offset) {
631 uint high = (uint)decode_int (packet, ref offset);
632 uint low = (uint)decode_int (packet, ref offset);
634 return (long)(((ulong)high << 32) | (ulong)low);
637 internal static SuspendPolicy decode_suspend_policy (int suspend_policy) {
638 switch ((WPSuspendPolicy)suspend_policy) {
639 case WPSuspendPolicy.NONE:
640 return SuspendPolicy.None;
641 case WPSuspendPolicy.EVENT_THREAD:
642 return SuspendPolicy.EventThread;
643 case WPSuspendPolicy.ALL:
644 return SuspendPolicy.All;
646 throw new NotImplementedException ();
650 static Header decode_command_header (byte[] packet) {
652 Header res = new Header ();
654 decode_int (packet, ref offset);
655 res.id = decode_int (packet, ref offset);
656 res.flags = decode_byte (packet, ref offset);
657 res.command_set = decode_byte (packet, ref offset);
658 res.command = decode_byte (packet, ref offset);
663 static void encode_byte (byte[] buf, int b, ref int offset) {
664 buf [offset] = (byte)b;
668 static void encode_int (byte[] buf, int i, ref int offset) {
669 buf [offset] = (byte)((i >> 24) & 0xff);
670 buf [offset + 1] = (byte)((i >> 16) & 0xff);
671 buf [offset + 2] = (byte)((i >> 8) & 0xff);
672 buf [offset + 3] = (byte)((i >> 0) & 0xff);
676 static void encode_id (byte[] buf, long id, ref int offset) {
677 encode_int (buf, (int)id, ref offset);
680 static void encode_long (byte[] buf, long l, ref int offset) {
681 encode_int (buf, (int)((l >> 32) & 0xffffffff), ref offset);
682 encode_int (buf, (int)(l & 0xffffffff), ref offset);
685 internal static byte[] EncodePacket (int id, int commandSet, int command, byte[] data, int dataLen) {
686 byte[] buf = new byte [dataLen + 11];
689 encode_int (buf, buf.Length, ref offset);
690 encode_int (buf, id, ref offset);
691 encode_byte (buf, 0, ref offset);
692 encode_byte (buf, commandSet, ref offset);
693 encode_byte (buf, command, ref offset);
695 for (int i = 0; i < dataLen; ++i)
696 buf [offset + i] = data [i];
705 public PacketReader (byte[] packet) {
706 this.packet = packet;
709 Header header = decode_command_header (packet);
710 CommandSet = (CommandSet)header.command_set;
711 Command = header.command;
715 ReadInt (); // length
717 ReadByte (); // flags
718 ErrorCode = ReadShort ();
721 public CommandSet CommandSet {
729 public int ErrorCode {
739 public int ReadByte () {
740 return decode_byte (packet, ref offset);
743 public int ReadShort () {
744 return decode_short (packet, ref offset);
747 public int ReadInt () {
748 return decode_int (packet, ref offset);
751 public long ReadId () {
752 return decode_id (packet, ref offset);
755 public long ReadLong () {
756 return decode_long (packet, ref offset);
759 public float ReadFloat () {
760 float f = DataConverter.FloatFromBE (packet, offset);
765 public double ReadDouble () {
766 double d = DataConverter.DoubleFromBE (packet, offset);
771 public string ReadString () {
772 int len = decode_int (packet, ref offset);
773 string res = new String (Encoding.UTF8.GetChars (packet, offset, len));
778 public ValueImpl ReadValue () {
779 ElementType etype = (ElementType)ReadByte ();
782 case ElementType.Void:
783 return new ValueImpl { Type = etype };
785 return new ValueImpl { Type = etype, Value = (sbyte)ReadInt () };
787 return new ValueImpl { Type = etype, Value = (byte)ReadInt () };
788 case ElementType.Boolean:
789 return new ValueImpl { Type = etype, Value = ReadInt () != 0 };
791 return new ValueImpl { Type = etype, Value = (short)ReadInt () };
793 return new ValueImpl { Type = etype, Value = (ushort)ReadInt () };
794 case ElementType.Char:
795 return new ValueImpl { Type = etype, Value = (char)ReadInt () };
797 return new ValueImpl { Type = etype, Value = ReadInt () };
799 return new ValueImpl { Type = etype, Value = (uint)ReadInt () };
801 return new ValueImpl { Type = etype, Value = ReadLong () };
803 return new ValueImpl { Type = etype, Value = (ulong)ReadLong () };
805 return new ValueImpl { Type = etype, Value = ReadFloat () };
807 return new ValueImpl { Type = etype, Value = ReadDouble () };
810 case ElementType.Ptr:
811 // FIXME: The client and the debuggee might have different word sizes
812 return new ValueImpl { Type = etype, Value = ReadLong () };
813 case ElementType.String:
814 case ElementType.SzArray:
815 case ElementType.Class:
816 case ElementType.Array:
817 case ElementType.Object:
818 long objid = ReadId ();
819 return new ValueImpl () { Type = etype, Objid = objid };
820 case ElementType.ValueType:
821 bool is_enum = ReadByte () == 1;
822 long klass = ReadId ();
823 long nfields = ReadInt ();
824 ValueImpl[] fields = new ValueImpl [nfields];
825 for (int i = 0; i < nfields; ++i)
826 fields [i] = ReadValue ();
827 return new ValueImpl () { Type = etype, Klass = klass, Fields = fields, IsEnum = is_enum };
828 case (ElementType)ValueTypeId.VALUE_TYPE_ID_NULL:
829 return new ValueImpl { Type = etype };
830 case (ElementType)ValueTypeId.VALUE_TYPE_ID_TYPE:
831 return new ValueImpl () { Type = etype, Id = ReadId () };
833 throw new NotImplementedException ("Unable to handle type " + etype);
837 public long[] ReadIds (int n) {
838 long[] res = new long [n];
839 for (int i = 0; i < n; ++i)
850 public PacketWriter () {
851 data = new byte [1024];
855 void MakeRoom (int size) {
856 if (offset + size >= data.Length) {
857 int new_len = data.Length * 2;
858 while (new_len < offset + size) {
861 byte[] new_data = new byte [new_len];
862 Array.Copy (data, new_data, data.Length);
867 public PacketWriter WriteByte (byte val) {
869 encode_byte (data, val, ref offset);
873 public PacketWriter WriteInt (int val) {
875 encode_int (data, val, ref offset);
879 public PacketWriter WriteId (long id) {
881 encode_id (data, id, ref offset);
885 public PacketWriter WriteLong (long val) {
887 encode_long (data, val, ref offset);
891 public PacketWriter WriteFloat (float f) {
893 byte[] b = DataConverter.GetBytesBE (f);
894 for (int i = 0; i < 4; ++i)
895 data [offset + i] = b [i];
900 public PacketWriter WriteDouble (double d) {
902 byte[] b = DataConverter.GetBytesBE (d);
903 for (int i = 0; i < 8; ++i)
904 data [offset + i] = b [i];
909 public PacketWriter WriteInts (int[] ids) {
910 for (int i = 0; i < ids.Length; ++i)
915 public PacketWriter WriteIds (long[] ids) {
916 for (int i = 0; i < ids.Length; ++i)
921 public PacketWriter WriteString (string s) {
923 return WriteInt (-1);
925 byte[] b = Encoding.UTF8.GetBytes (s);
927 encode_int (data, b.Length, ref offset);
929 Buffer.BlockCopy (b, 0, data, offset, b.Length);
934 public PacketWriter WriteBool (bool val) {
935 WriteByte (val ? (byte)1 : (byte)0);
939 public PacketWriter WriteValue (ValueImpl v) {
943 t = TypeCodeToElementType (Type.GetTypeCode (v.Value.GetType ()));
948 case ElementType.Boolean:
949 WriteInt ((bool)v.Value ? 1 : 0);
951 case ElementType.Char:
952 WriteInt ((int)(char)v.Value);
955 WriteInt ((int)(sbyte)v.Value);
958 WriteInt ((int)(byte)v.Value);
961 WriteInt ((int)(short)v.Value);
964 WriteInt ((int)(ushort)v.Value);
967 WriteInt ((int)(int)v.Value);
970 WriteInt ((int)(uint)v.Value);
973 WriteLong ((long)(long)v.Value);
976 WriteLong ((long)(ulong)v.Value);
979 WriteFloat ((float)v.Value);
982 WriteDouble ((double)v.Value);
984 case ElementType.String:
985 case ElementType.SzArray:
986 case ElementType.Class:
987 case ElementType.Array:
988 case ElementType.Object:
991 case ElementType.ValueType:
994 throw new NotImplementedException ();
997 WriteInt (v.Fields.Length);
998 for (int i = 0; i < v.Fields.Length; ++i)
999 WriteValue (v.Fields [i]);
1001 case (ElementType)ValueTypeId.VALUE_TYPE_ID_NULL:
1004 throw new NotImplementedException ();
1010 public PacketWriter WriteValues (ValueImpl[] values) {
1011 for (int i = 0; i < values.Length; ++i)
1012 WriteValue (values [i]);
1016 public byte[] Data {
1029 delegate void ReplyCallback (int packet_id, byte[] packet);
1032 Thread receiver_thread;
1033 Dictionary<int, byte[]> reply_packets;
1034 Dictionary<int, ReplyCallback> reply_cbs;
1035 Dictionary<int, int> reply_cb_counts;
1036 object reply_packets_monitor;
1038 internal event EventHandler<ErrorHandlerEventArgs> ErrorHandler;
1040 protected Connection () {
1042 reply_packets = new Dictionary<int, byte[]> ();
1043 reply_cbs = new Dictionary<int, ReplyCallback> ();
1044 reply_cb_counts = new Dictionary<int, int> ();
1045 reply_packets_monitor = new Object ();
1048 protected abstract int TransportReceive (byte[] buf, int buf_offset, int len);
1049 protected abstract int TransportSend (byte[] buf, int buf_offset, int len);
1050 protected abstract void TransportSetTimeouts (int send_timeout, int receive_timeout);
1051 protected abstract void TransportClose ();
1053 internal VersionInfo Version;
1055 int Receive (byte[] buf, int buf_offset, int len) {
1058 while (offset < len) {
1059 int n = TransportReceive (buf, buf_offset + offset, len - offset);
1069 // Do the wire protocol handshake
1070 internal void Connect () {
1071 byte[] buf = new byte [HANDSHAKE_STRING.Length];
1072 char[] cbuf = new char [buf.Length];
1074 // FIXME: Add a timeout
1075 int n = Receive (buf, 0, buf.Length);
1077 throw new IOException ("DWP Handshake failed.");
1078 for (int i = 0; i < buf.Length; ++i)
1079 cbuf [i] = (char)buf [i];
1081 if (new String (cbuf) != HANDSHAKE_STRING)
1082 throw new IOException ("DWP Handshake failed.");
1084 TransportSend (buf, 0, buf.Length);
1086 receiver_thread = new Thread (new ThreadStart (receiver_thread_main));
1087 receiver_thread.Name = "SDB Receiver";
1088 receiver_thread.IsBackground = true;
1089 receiver_thread.Start ();
1091 Version = VM_GetVersion ();
1094 // Tell the debuggee our protocol version, so newer debuggees can work
1095 // with older clients
1099 // Older debuggees might not support this request
1100 EventHandler<ErrorHandlerEventArgs> OrigErrorHandler = ErrorHandler;
1101 ErrorHandler = null;
1102 ErrorHandler += delegate (object sender, ErrorHandlerEventArgs args) {
1103 throw new NotSupportedException ();
1106 VM_SetProtocolVersion (MAJOR_VERSION, MINOR_VERSION);
1107 } catch (NotSupportedException) {
1109 ErrorHandler = OrigErrorHandler;
1112 internal byte[] ReadPacket () {
1113 // FIXME: Throw ClosedConnectionException () if the connection is closed
1114 // FIXME: Throw ClosedConnectionException () if another thread closes the connection
1116 byte[] header = new byte [HEADER_LENGTH];
1118 int len = Receive (header, 0, header.Length);
1120 return new byte [0];
1121 if (len != HEADER_LENGTH) {
1123 throw new IOException ("Packet of length " + len + " is read.");
1126 int packetLength = GetPacketLength (header);
1127 if (packetLength < 11)
1128 throw new IOException ("Invalid packet length.");
1130 if (packetLength == 11) {
1133 byte[] buf = new byte [packetLength];
1134 for (int i = 0; i < header.Length; ++i)
1135 buf [i] = header [i];
1136 len = Receive (buf, header.Length, packetLength - header.Length);
1137 if (len != packetLength - header.Length)
1138 throw new IOException ();
1143 internal void WritePacket (byte[] packet) {
1144 // FIXME: Throw ClosedConnectionException () if the connection is closed
1145 // FIXME: Throw ClosedConnectionException () if another thread closes the connection
1147 TransportSend (packet, 0, packet.Length);
1150 internal void Close () {
1154 internal bool IsClosed {
1162 void receiver_thread_main () {
1165 bool res = ReceivePacket ();
1168 } catch (Exception ex) {
1170 Console.WriteLine (ex);
1176 lock (reply_packets_monitor) {
1177 disconnected = true;
1178 Monitor.PulseAll (reply_packets_monitor);
1181 EventHandler.VMDisconnect (0, 0, null);
1184 bool ReceivePacket () {
1185 byte[] packet = ReadPacket ();
1187 if (packet.Length == 0) {
1191 if (IsReplyPacket (packet)) {
1192 int id = GetPacketId (packet);
1193 ReplyCallback cb = null;
1194 lock (reply_packets_monitor) {
1195 reply_cbs.TryGetValue (id, out cb);
1197 reply_packets [id] = packet;
1198 Monitor.PulseAll (reply_packets_monitor);
1200 int c = reply_cb_counts [id];
1203 reply_cbs.Remove (id);
1204 reply_cb_counts.Remove (id);
1210 cb.Invoke (id, packet);
1212 PacketReader r = new PacketReader (packet);
1214 if (r.CommandSet == CommandSet.EVENT && r.Command == (int)CmdEvent.COMPOSITE) {
1215 int spolicy = r.ReadByte ();
1216 int nevents = r.ReadInt ();
1218 SuspendPolicy suspend_policy = decode_suspend_policy (spolicy);
1220 EventInfo[] events = new EventInfo [nevents];
1222 for (int i = 0; i < nevents; ++i) {
1223 EventKind kind = (EventKind)r.ReadByte ();
1224 int req_id = r.ReadInt ();
1226 EventType etype = (EventType)kind;
1228 if (kind == EventKind.VM_START) {
1229 long thread_id = r.ReadId ();
1230 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id };
1231 //EventHandler.VMStart (req_id, thread_id, null);
1232 } else if (kind == EventKind.VM_DEATH) {
1233 //EventHandler.VMDeath (req_id, 0, null);
1234 events [i] = new EventInfo (etype, req_id) { };
1235 } else if (kind == EventKind.THREAD_START) {
1236 long thread_id = r.ReadId ();
1237 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = thread_id };
1238 //EventHandler.ThreadStart (req_id, thread_id, thread_id);
1239 } else if (kind == EventKind.THREAD_DEATH) {
1240 long thread_id = r.ReadId ();
1241 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = thread_id };
1242 //EventHandler.ThreadDeath (req_id, thread_id, thread_id);
1243 } else if (kind == EventKind.ASSEMBLY_LOAD) {
1244 long thread_id = r.ReadId ();
1245 long id = r.ReadId ();
1246 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1247 //EventHandler.AssemblyLoad (req_id, thread_id, id);
1248 } else if (kind == EventKind.ASSEMBLY_UNLOAD) {
1249 long thread_id = r.ReadId ();
1250 long id = r.ReadId ();
1251 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1252 //EventHandler.AssemblyUnload (req_id, thread_id, id);
1253 } else if (kind == EventKind.TYPE_LOAD) {
1254 long thread_id = r.ReadId ();
1255 long id = r.ReadId ();
1256 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1257 //EventHandler.TypeLoad (req_id, thread_id, id);
1258 } else if (kind == EventKind.METHOD_ENTRY) {
1259 long thread_id = r.ReadId ();
1260 long id = r.ReadId ();
1261 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1262 //EventHandler.MethodEntry (req_id, thread_id, id);
1263 } else if (kind == EventKind.METHOD_EXIT) {
1264 long thread_id = r.ReadId ();
1265 long id = r.ReadId ();
1266 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1267 //EventHandler.MethodExit (req_id, thread_id, id);
1268 } else if (kind == EventKind.BREAKPOINT) {
1269 long thread_id = r.ReadId ();
1270 long id = r.ReadId ();
1271 long loc = r.ReadLong ();
1272 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1273 //EventHandler.Breakpoint (req_id, thread_id, id, loc);
1274 } else if (kind == EventKind.STEP) {
1275 long thread_id = r.ReadId ();
1276 long id = r.ReadId ();
1277 long loc = r.ReadLong ();
1278 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1279 //EventHandler.Step (req_id, thread_id, id, loc);
1280 } else if (kind == EventKind.EXCEPTION) {
1281 long thread_id = r.ReadId ();
1282 long id = r.ReadId ();
1283 long loc = 0; // FIXME
1284 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1285 //EventHandler.Exception (req_id, thread_id, id, loc);
1286 } else if (kind == EventKind.APPDOMAIN_CREATE) {
1287 long thread_id = r.ReadId ();
1288 long id = r.ReadId ();
1289 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1290 //EventHandler.AppDomainCreate (req_id, thread_id, id);
1291 } else if (kind == EventKind.APPDOMAIN_UNLOAD) {
1292 long thread_id = r.ReadId ();
1293 long id = r.ReadId ();
1294 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1295 //EventHandler.AppDomainUnload (req_id, thread_id, id);
1296 } else if (kind == EventKind.USER_BREAK) {
1297 long thread_id = r.ReadId ();
1300 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1301 //EventHandler.Exception (req_id, thread_id, id, loc);
1302 } else if (kind == EventKind.USER_LOG) {
1303 long thread_id = r.ReadId ();
1304 int level = r.ReadInt ();
1305 string category = r.ReadString ();
1306 string message = r.ReadString ();
1307 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Level = level, Category = category, Message = message };
1308 //EventHandler.Exception (req_id, thread_id, id, loc);
1309 } else if (kind == EventKind.KEEPALIVE) {
1310 events [i] = new EventInfo (etype, req_id) { };
1312 throw new NotImplementedException ("Unknown event kind: " + kind);
1316 EventHandler.Events (suspend_policy, events);
1323 internal IEventHandler EventHandler {
1327 static String CommandString (CommandSet command_set, int command)
1330 switch (command_set) {
1332 cmd = ((CmdVM)command).ToString ();
1334 case CommandSet.OBJECT_REF:
1335 cmd = ((CmdObjectRef)command).ToString ();
1337 case CommandSet.STRING_REF:
1338 cmd = ((CmdStringRef)command).ToString ();
1340 case CommandSet.THREAD:
1341 cmd = ((CmdThread)command).ToString ();
1343 case CommandSet.ARRAY_REF:
1344 cmd = ((CmdArrayRef)command).ToString ();
1346 case CommandSet.EVENT_REQUEST:
1347 cmd = ((CmdEventRequest)command).ToString ();
1349 case CommandSet.STACK_FRAME:
1350 cmd = ((CmdStackFrame)command).ToString ();
1352 case CommandSet.APPDOMAIN:
1353 cmd = ((CmdAppDomain)command).ToString ();
1355 case CommandSet.ASSEMBLY:
1356 cmd = ((CmdAssembly)command).ToString ();
1358 case CommandSet.METHOD:
1359 cmd = ((CmdMethod)command).ToString ();
1361 case CommandSet.TYPE:
1362 cmd = ((CmdType)command).ToString ();
1364 case CommandSet.MODULE:
1365 cmd = ((CmdModule)command).ToString ();
1367 case CommandSet.EVENT:
1368 cmd = ((CmdEvent)command).ToString ();
1371 cmd = command.ToString ();
1374 return string.Format ("[{0} {1}]", command_set, cmd);
1377 long total_protocol_ticks;
1379 void LogPacket (int packet_id, byte[] encoded_packet, byte[] reply_packet, CommandSet command_set, int command, Stopwatch watch) {
1381 total_protocol_ticks += watch.ElapsedTicks;
1382 var ts = TimeSpan.FromTicks (total_protocol_ticks);
1383 string msg = string.Format ("Packet: {0} sent: {1} received: {2} ms: {3} total ms: {4} {5}",
1384 packet_id, encoded_packet.Length, reply_packet.Length, watch.ElapsedMilliseconds,
1385 (ts.Seconds * 1000) + ts.Milliseconds,
1386 CommandString (command_set, command));
1388 LoggingStream.WriteLine (msg);
1389 LoggingStream.Flush ();
1392 /* Send a request and call cb when a result is received */
1393 int Send (CommandSet command_set, int command, PacketWriter packet, Action<PacketReader> cb, int count) {
1394 int id = IdGenerator;
1396 Stopwatch watch = null;
1397 if (EnableConnectionLogging)
1398 watch = Stopwatch.StartNew ();
1400 byte[] encoded_packet;
1402 encoded_packet = EncodePacket (id, (int)command_set, command, null, 0);
1404 encoded_packet = EncodePacket (id, (int)command_set, command, packet.Data, packet.Offset);
1406 lock (reply_packets_monitor) {
1407 reply_cbs [id] = delegate (int packet_id, byte[] p) {
1408 if (EnableConnectionLogging)
1409 LogPacket (packet_id, encoded_packet, p, command_set, command, watch);
1410 /* Run the callback on a tp thread to avoid blocking the receive thread */
1411 PacketReader r = new PacketReader (p);
1412 cb.BeginInvoke (r, null, null);
1414 reply_cb_counts [id] = count;
1417 WritePacket (encoded_packet);
1422 PacketReader SendReceive (CommandSet command_set, int command, PacketWriter packet) {
1423 int id = IdGenerator;
1424 Stopwatch watch = null;
1427 throw new VMDisconnectedException ();
1429 if (EnableConnectionLogging)
1430 watch = Stopwatch.StartNew ();
1432 byte[] encoded_packet;
1435 encoded_packet = EncodePacket (id, (int)command_set, command, null, 0);
1437 encoded_packet = EncodePacket (id, (int)command_set, command, packet.Data, packet.Offset);
1439 WritePacket (encoded_packet);
1443 /* Wait for the reply packet */
1445 lock (reply_packets_monitor) {
1446 if (reply_packets.ContainsKey (packetId)) {
1447 byte[] reply = reply_packets [packetId];
1448 reply_packets.Remove (packetId);
1449 PacketReader r = new PacketReader (reply);
1451 if (EnableConnectionLogging)
1452 LogPacket (packetId, encoded_packet, reply, command_set, command, watch);
1453 if (r.ErrorCode != 0) {
1454 if (ErrorHandler != null)
1455 ErrorHandler (this, new ErrorHandlerEventArgs () { ErrorCode = (ErrorCode)r.ErrorCode });
1456 throw new NotImplementedException ("No error handler set.");
1462 throw new VMDisconnectedException ();
1463 Monitor.Wait (reply_packets_monitor);
1469 PacketReader SendReceive (CommandSet command_set, int command) {
1470 return SendReceive (command_set, command, null);
1473 int packet_id_generator;
1477 return Interlocked.Increment (ref packet_id_generator);
1481 CattrInfo[] ReadCattrs (PacketReader r) {
1482 CattrInfo[] res = new CattrInfo [r.ReadInt ()];
1483 for (int i = 0; i < res.Length; ++i) {
1484 CattrInfo info = new CattrInfo ();
1485 info.ctor_id = r.ReadId ();
1486 info.ctor_args = new ValueImpl [r.ReadInt ()];
1487 for (int j = 0; j < info.ctor_args.Length; ++j) {
1488 info.ctor_args [j] = r.ReadValue ();
1490 info.named_args = new CattrNamedArgInfo [r.ReadInt ()];
1491 for (int j = 0; j < info.named_args.Length; ++j) {
1492 CattrNamedArgInfo arg = new CattrNamedArgInfo ();
1493 int arg_type = r.ReadByte ();
1494 arg.is_property = arg_type == 0x54;
1495 arg.id = r.ReadId ();
1496 arg.value = r.ReadValue ();
1497 info.named_args [j] = arg;
1504 static ElementType TypeCodeToElementType (TypeCode c) {
1506 case TypeCode.Boolean:
1507 return ElementType.Boolean;
1509 return ElementType.Char;
1510 case TypeCode.SByte:
1511 return ElementType.I1;
1513 return ElementType.U1;
1514 case TypeCode.Int16:
1515 return ElementType.I2;
1516 case TypeCode.UInt16:
1517 return ElementType.U2;
1518 case TypeCode.Int32:
1519 return ElementType.I4;
1520 case TypeCode.UInt32:
1521 return ElementType.U4;
1522 case TypeCode.Int64:
1523 return ElementType.I8;
1524 case TypeCode.UInt64:
1525 return ElementType.U8;
1526 case TypeCode.Single:
1527 return ElementType.R4;
1528 case TypeCode.Double:
1529 return ElementType.R8;
1531 throw new NotImplementedException ();
1536 * Implementation of debugger commands
1539 internal VersionInfo VM_GetVersion () {
1540 var res = SendReceive (CommandSet.VM, (int)CmdVM.VERSION, null);
1541 VersionInfo info = new VersionInfo ();
1542 info.VMVersion = res.ReadString ();
1543 info.MajorVersion = res.ReadInt ();
1544 info.MinorVersion = res.ReadInt ();
1548 internal void VM_SetProtocolVersion (int major, int minor) {
1549 SendReceive (CommandSet.VM, (int)CmdVM.SET_PROTOCOL_VERSION, new PacketWriter ().WriteInt (major).WriteInt (minor));
1552 internal long[] VM_GetThreads () {
1553 var res = SendReceive (CommandSet.VM, (int)CmdVM.ALL_THREADS, null);
1554 int len = res.ReadInt ();
1555 long[] arr = new long [len];
1556 for (int i = 0; i < len; ++i)
1557 arr [i] = res.ReadId ();
1561 internal void VM_Suspend () {
1562 SendReceive (CommandSet.VM, (int)CmdVM.SUSPEND);
1565 internal void VM_Resume () {
1566 SendReceive (CommandSet.VM, (int)CmdVM.RESUME);
1569 internal void VM_Exit (int exitCode) {
1570 SendReceive (CommandSet.VM, (int)CmdVM.EXIT, new PacketWriter ().WriteInt (exitCode));
1573 internal void VM_Dispose () {
1574 SendReceive (CommandSet.VM, (int)CmdVM.DISPOSE);
1577 internal ValueImpl VM_InvokeMethod (long thread, long method, ValueImpl this_arg, ValueImpl[] arguments, InvokeFlags flags, out ValueImpl exc) {
1579 PacketReader r = SendReceive (CommandSet.VM, (int)CmdVM.INVOKE_METHOD, new PacketWriter ().WriteId (thread).WriteInt ((int)flags).WriteId (method).WriteValue (this_arg).WriteInt (arguments.Length).WriteValues (arguments));
1580 if (r.ReadByte () == 0) {
1581 exc = r.ReadValue ();
1584 return r.ReadValue ();
1588 internal delegate void InvokeMethodCallback (ValueImpl v, ValueImpl exc, ErrorCode error, object state);
1590 internal int VM_BeginInvokeMethod (long thread, long method, ValueImpl this_arg, ValueImpl[] arguments, InvokeFlags flags, InvokeMethodCallback callback, object state) {
1591 return Send (CommandSet.VM, (int)CmdVM.INVOKE_METHOD, new PacketWriter ().WriteId (thread).WriteInt ((int)flags).WriteId (method).WriteValue (this_arg).WriteInt (arguments.Length).WriteValues (arguments), delegate (PacketReader r) {
1594 if (r.ErrorCode != 0) {
1595 callback (null, null, (ErrorCode)r.ErrorCode, state);
1597 if (r.ReadByte () == 0) {
1598 exc = r.ReadValue ();
1605 callback (v, exc, 0, state);
1610 internal int VM_BeginInvokeMethods (long thread, long[] methods, ValueImpl this_arg, List<ValueImpl[]> arguments, InvokeFlags flags, InvokeMethodCallback callback, object state) {
1611 // FIXME: Merge this with INVOKE_METHOD
1612 var w = new PacketWriter ();
1614 w.WriteInt ((int)flags);
1615 w.WriteInt (methods.Length);
1616 for (int i = 0; i < methods.Length; ++i) {
1617 w.WriteId (methods [i]);
1618 w.WriteValue (this_arg);
1619 w.WriteInt (arguments [i].Length);
1620 w.WriteValues (arguments [i]);
1622 return Send (CommandSet.VM, (int)CmdVM.INVOKE_METHODS, w, delegate (PacketReader r) {
1625 if (r.ErrorCode != 0) {
1626 callback (null, null, (ErrorCode)r.ErrorCode, state);
1628 if (r.ReadByte () == 0) {
1629 exc = r.ReadValue ();
1636 callback (v, exc, 0, state);
1641 internal void VM_AbortInvoke (long thread, int id)
1643 SendReceive (CommandSet.VM, (int)CmdVM.ABORT_INVOKE, new PacketWriter ().WriteId (thread).WriteInt (id));
1646 internal void SetSocketTimeouts (int send_timeout, int receive_timeout, int keepalive_interval)
1648 TransportSetTimeouts (send_timeout, receive_timeout);
1649 SendReceive (CommandSet.VM, (int)CmdVM.SET_KEEPALIVE, new PacketWriter ().WriteId (keepalive_interval));
1652 internal long[] VM_GetTypesForSourceFile (string fname, bool ignoreCase) {
1653 var res = SendReceive (CommandSet.VM, (int)CmdVM.GET_TYPES_FOR_SOURCE_FILE, new PacketWriter ().WriteString (fname).WriteBool (ignoreCase));
1654 int count = res.ReadInt ();
1655 long[] types = new long [count];
1656 for (int i = 0; i < count; ++i)
1657 types [i] = res.ReadId ();
1661 internal long[] VM_GetTypes (string name, bool ignoreCase) {
1662 var res = SendReceive (CommandSet.VM, (int)CmdVM.GET_TYPES, new PacketWriter ().WriteString (name).WriteBool (ignoreCase));
1663 int count = res.ReadInt ();
1664 long[] types = new long [count];
1665 for (int i = 0; i < count; ++i)
1666 types [i] = res.ReadId ();
1674 internal long RootDomain {
1676 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_ROOT_DOMAIN, null).ReadId ();
1680 internal string Domain_GetName (long id) {
1681 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_FRIENDLY_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1684 internal long[] Domain_GetAssemblies (long id) {
1685 var res = SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_ASSEMBLIES, new PacketWriter ().WriteId (id));
1686 int count = res.ReadInt ();
1687 long[] assemblies = new long [count];
1688 for (int i = 0; i < count; ++i)
1689 assemblies [i] = res.ReadId ();
1693 internal long Domain_GetEntryAssembly (long id) {
1694 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_ENTRY_ASSEMBLY, new PacketWriter ().WriteId (id)).ReadId ();
1697 internal long Domain_GetCorlib (long id) {
1698 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_CORLIB, new PacketWriter ().WriteId (id)).ReadId ();
1701 internal long Domain_CreateString (long id, string s) {
1702 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.CREATE_STRING, new PacketWriter ().WriteId (id).WriteString (s)).ReadId ();
1705 internal long Domain_CreateBoxedValue (long id, long type_id, ValueImpl v) {
1706 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.CREATE_BOXED_VALUE, new PacketWriter ().WriteId (id).WriteId (type_id).WriteValue (v)).ReadId ();
1713 internal string Method_GetName (long id) {
1714 return SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1717 internal long Method_GetDeclaringType (long id) {
1718 return SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_DECLARING_TYPE, new PacketWriter ().WriteId (id)).ReadId ();
1721 internal DebugInfo Method_GetDebugInfo (long id) {
1722 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_DEBUG_INFO, new PacketWriter ().WriteId (id));
1724 DebugInfo info = new DebugInfo ();
1725 info.max_il_offset = res.ReadInt ();
1727 SourceInfo[] sources = null;
1728 if (Version.AtLeast (2, 13)) {
1729 int n = res.ReadInt ();
1730 sources = new SourceInfo [n];
1731 for (int i = 0; i < n; ++i) {
1732 sources [i].source_file = res.ReadString ();
1733 if (Version.AtLeast (2, 14)) {
1734 sources [i].hash = new byte [16];
1735 for (int j = 0; j < 16; ++j)
1736 sources [i].hash [j] = (byte)res.ReadByte ();
1740 sources = new SourceInfo [1];
1741 sources [0].source_file = res.ReadString ();
1744 int n_il_offsets = res.ReadInt ();
1745 info.il_offsets = new int [n_il_offsets];
1746 info.line_numbers = new int [n_il_offsets];
1747 info.source_files = new SourceInfo [n_il_offsets];
1748 info.column_numbers = new int [n_il_offsets];
1749 for (int i = 0; i < n_il_offsets; ++i) {
1750 info.il_offsets [i] = res.ReadInt ();
1751 info.line_numbers [i] = res.ReadInt ();
1752 if (Version.AtLeast (2, 12)) {
1753 int idx = res.ReadInt ();
1754 info.source_files [i] = idx >= 0 ? sources [idx] : default (SourceInfo);
1756 info.source_files [i] = sources [0];
1758 if (Version.AtLeast (2, 19))
1759 info.column_numbers [i] = res.ReadInt ();
1761 info.column_numbers [i] = 0;
1767 internal ParamInfo Method_GetParamInfo (long id) {
1768 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_PARAM_INFO, new PacketWriter ().WriteId (id));
1770 ParamInfo info = new ParamInfo ();
1771 info.call_conv = res.ReadInt ();
1772 info.param_count = res.ReadInt ();
1773 info.generic_param_count = res.ReadInt ();
1774 info.ret_type = res.ReadId ();
1775 info.param_types = new long [info.param_count];
1776 for (int i = 0; i < info.param_count; ++i)
1777 info.param_types [i] = res.ReadId ();
1778 info.param_names = new string [info.param_count];
1779 for (int i = 0; i < info.param_count; ++i)
1780 info.param_names [i] = res.ReadString ();
1785 internal LocalsInfo Method_GetLocalsInfo (long id) {
1786 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_LOCALS_INFO, new PacketWriter ().WriteId (id));
1788 LocalsInfo info = new LocalsInfo ();
1789 int nlocals = res.ReadInt ();
1790 info.types = new long [nlocals];
1791 for (int i = 0; i < nlocals; ++i)
1792 info.types [i] = res.ReadId ();
1793 info.names = new string [nlocals];
1794 for (int i = 0; i < nlocals; ++i)
1795 info.names [i] = res.ReadString ();
1796 info.live_range_start = new int [nlocals];
1797 info.live_range_end = new int [nlocals];
1798 for (int i = 0; i < nlocals; ++i) {
1799 info.live_range_start [i] = res.ReadInt ();
1800 info.live_range_end [i] = res.ReadInt ();
1806 internal MethodInfo Method_GetInfo (long id) {
1807 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_INFO, new PacketWriter ().WriteId (id));
1809 MethodInfo info = new MethodInfo ();
1810 info.attributes = res.ReadInt ();
1811 info.iattributes = res.ReadInt ();
1812 info.token = res.ReadInt ();
1813 if (Version.AtLeast (2, 12)) {
1814 int attrs = res.ReadByte ();
1815 if ((attrs & (1 << 0)) != 0)
1817 if ((attrs & (1 << 1)) != 0)
1818 info.is_generic_method = true;
1819 info.gmd = res.ReadId ();
1820 if (Version.AtLeast (2, 15)) {
1821 if (info.is_generic_method) {
1822 int n = res.ReadInt ();
1823 info.type_args = res.ReadIds (n);
1830 internal MethodBodyInfo Method_GetBody (long id) {
1831 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_BODY, new PacketWriter ().WriteId (id));
1833 MethodBodyInfo info = new MethodBodyInfo ();
1834 info.il = new byte [res.ReadInt ()];
1835 for (int i = 0; i < info.il.Length; ++i)
1836 info.il [i] = (byte)res.ReadByte ();
1838 if (Version.AtLeast (2, 18)) {
1839 info.clauses = new ExceptionClauseInfo [res.ReadInt ()];
1841 for (int i = 0; i < info.clauses.Length; ++i) {
1842 var clause = new ExceptionClauseInfo {
1843 flags = (ExceptionClauseFlags) res.ReadInt (),
1844 try_offset = res.ReadInt (),
1845 try_length = res.ReadInt (),
1846 handler_offset = res.ReadInt (),
1847 handler_length = res.ReadInt (),
1850 if (clause.flags == ExceptionClauseFlags.None)
1851 clause.catch_type_id = res.ReadId ();
1852 else if (clause.flags == ExceptionClauseFlags.Filter)
1853 clause.filter_offset = res.ReadInt ();
1855 info.clauses [i] = clause;
1858 info.clauses = new ExceptionClauseInfo [0];
1863 internal ResolvedToken Method_ResolveToken (long id, int token) {
1864 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.RESOLVE_TOKEN, new PacketWriter ().WriteId (id).WriteInt (token));
1866 TokenType type = (TokenType)res.ReadByte ();
1868 case TokenType.STRING:
1869 return new ResolvedToken () { Type = type, Str = res.ReadString () };
1870 case TokenType.TYPE:
1871 case TokenType.METHOD:
1872 case TokenType.FIELD:
1873 return new ResolvedToken () { Type = type, Id = res.ReadId () };
1874 case TokenType.UNKNOWN:
1875 return new ResolvedToken () { Type = type };
1877 throw new NotImplementedException ();
1881 internal CattrInfo[] Method_GetCustomAttributes (long id, long attr_type_id, bool inherit) {
1882 PacketReader r = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_CATTRS, new PacketWriter ().WriteId (id).WriteId (attr_type_id));
1883 return ReadCattrs (r);
1886 internal long Method_MakeGenericMethod (long id, long[] args) {
1887 PacketReader r = SendReceive (CommandSet.METHOD, (int)CmdMethod.MAKE_GENERIC_METHOD, new PacketWriter ().WriteId (id).WriteInt (args.Length).WriteIds (args));
1895 internal string Thread_GetName (long id) {
1896 return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1899 internal FrameInfo[] Thread_GetFrameInfo (long id, int start_frame, int length) {
1900 var res = SendReceive (CommandSet.THREAD, (int)CmdThread.GET_FRAME_INFO, new PacketWriter ().WriteId (id).WriteInt (start_frame).WriteInt (length));
1901 int count = res.ReadInt ();
1903 var frames = new FrameInfo [count];
1904 for (int i = 0; i < count; ++i) {
1905 var f = new FrameInfo ();
1906 f.id = res.ReadInt ();
1907 f.method = res.ReadId ();
1908 f.il_offset = res.ReadInt ();
1909 f.flags = (StackFrameFlags)res.ReadByte ();
1916 internal int Thread_GetState (long id) {
1917 return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_STATE, new PacketWriter ().WriteId (id)).ReadInt ();
1920 internal ThreadInfo Thread_GetInfo (long id) {
1921 PacketReader r = SendReceive (CommandSet.THREAD, (int)CmdThread.GET_INFO, new PacketWriter ().WriteId (id));
1923 ThreadInfo res = new ThreadInfo () { is_thread_pool = r.ReadByte () > 0 ? true : false };
1928 internal long Thread_GetId (long id) {
1929 return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_ID, new PacketWriter ().WriteId (id)).ReadLong ();
1932 internal long Thread_GetTID (long id) {
1933 return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_TID, new PacketWriter ().WriteId (id)).ReadLong ();
1940 internal ModuleInfo Module_GetInfo (long id) {
1941 PacketReader r = SendReceive (CommandSet.MODULE, (int)CmdModule.GET_INFO, new PacketWriter ().WriteId (id));
1942 ModuleInfo info = new ModuleInfo { Name = r.ReadString (), ScopeName = r.ReadString (), FQName = r.ReadString (), Guid = r.ReadString (), Assembly = r.ReadId () };
1950 internal string Assembly_GetLocation (long id) {
1951 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_LOCATION, new PacketWriter ().WriteId (id)).ReadString ();
1954 internal long Assembly_GetEntryPoint (long id) {
1955 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_ENTRY_POINT, new PacketWriter ().WriteId (id)).ReadId ();
1958 internal long Assembly_GetManifestModule (long id) {
1959 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_MANIFEST_MODULE, new PacketWriter ().WriteId (id)).ReadId ();
1962 internal long Assembly_GetObject (long id) {
1963 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_OBJECT, new PacketWriter ().WriteId (id)).ReadId ();
1966 internal long Assembly_GetType (long id, string name, bool ignoreCase) {
1967 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_TYPE, new PacketWriter ().WriteId (id).WriteString (name).WriteBool (ignoreCase)).ReadId ();
1970 internal string Assembly_GetName (long id) {
1971 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1978 internal TypeInfo Type_GetInfo (long id) {
1979 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_INFO, new PacketWriter ().WriteId (id));
1980 TypeInfo res = new TypeInfo ();
1982 res.ns = r.ReadString ();
1983 res.name = r.ReadString ();
1984 res.full_name = r.ReadString ();
1985 res.assembly = r.ReadId ();
1986 res.module = r.ReadId ();
1987 res.base_type = r.ReadId ();
1988 res.element_type = r.ReadId ();
1989 res.token = r.ReadInt ();
1990 res.rank = r.ReadByte ();
1991 res.attributes = r.ReadInt ();
1992 int b = r.ReadByte ();
1993 res.is_byref = (b & 1) != 0;
1994 res.is_pointer = (b & 2) != 0;
1995 res.is_primitive = (b & 4) != 0;
1996 res.is_valuetype = (b & 8) != 0;
1997 res.is_enum = (b & 16) != 0;
1998 res.is_gtd = (b & 32) != 0;
1999 res.is_generic_type = (b & 64) != 0;
2001 int nested_len = r.ReadInt ();
2002 res.nested = new long [nested_len];
2003 for (int i = 0; i < nested_len; ++i)
2004 res.nested [i] = r.ReadId ();
2006 if (Version.AtLeast (2, 12))
2007 res.gtd = r.ReadId ();
2008 if (Version.AtLeast (2, 15) && res.is_generic_type) {
2009 int n = r.ReadInt ();
2010 res.type_args = r.ReadIds (n);
2016 internal long[] Type_GetMethods (long id) {
2017 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_METHODS, new PacketWriter ().WriteId (id));
2019 int n = r.ReadInt ();
2020 long[] res = new long [n];
2021 for (int i = 0; i < n; ++i)
2022 res [i] = r.ReadId ();
2026 internal long[] Type_GetFields (long id, out string[] names, out long[] types, out int[] attrs) {
2027 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_FIELDS, new PacketWriter ().WriteId (id));
2029 int n = r.ReadInt ();
2030 long[] res = new long [n];
2031 names = new string [n];
2032 types = new long [n];
2033 attrs = new int [n];
2034 for (int i = 0; i < n; ++i) {
2035 res [i] = r.ReadId ();
2036 names [i] = r.ReadString ();
2037 types [i] = r.ReadId ();
2038 attrs [i] = r.ReadInt ();
2043 internal PropInfo[] Type_GetProperties (long id) {
2044 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_PROPERTIES, new PacketWriter ().WriteId (id));
2046 int n = r.ReadInt ();
2047 PropInfo[] res = new PropInfo [n];
2048 for (int i = 0; i < n; ++i) {
2049 res [i] = new PropInfo ();
2050 res [i].id = r.ReadId ();
2051 res [i].name = r.ReadString ();
2052 res [i].get_method = r.ReadId ();
2053 res [i].set_method = r.ReadId ();
2054 res [i].attrs = r.ReadInt ();
2060 internal long Type_GetObject (long id) {
2061 return SendReceive (CommandSet.TYPE, (int)CmdType.GET_OBJECT, new PacketWriter ().WriteId (id)).ReadId ();
2064 internal ValueImpl[] Type_GetValues (long id, long[] fields, long thread_id) {
2065 int len = fields.Length;
2068 r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_VALUES_2, new PacketWriter ().WriteId (id).WriteId (thread_id).WriteInt (len).WriteIds (fields));
2070 r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_VALUES, new PacketWriter ().WriteId (id).WriteInt (len).WriteIds (fields));
2072 ValueImpl[] res = new ValueImpl [len];
2073 for (int i = 0; i < len; ++i)
2074 res [i] = r.ReadValue ();
2078 internal void Type_SetValues (long id, long[] fields, ValueImpl[] values) {
2079 SendReceive (CommandSet.TYPE, (int)CmdType.SET_VALUES, new PacketWriter ().WriteId (id).WriteInt (fields.Length).WriteIds (fields).WriteValues (values));
2082 internal string[] Type_GetSourceFiles (long id, bool return_full_paths) {
2083 var r = SendReceive (CommandSet.TYPE, return_full_paths ? (int)CmdType.GET_SOURCE_FILES_2 : (int)CmdType.GET_SOURCE_FILES, new PacketWriter ().WriteId (id));
2084 int len = r.ReadInt ();
2085 string[] res = new string [len];
2086 for (int i = 0; i < len; ++i)
2087 res [i] = r.ReadString ();
2091 internal bool Type_IsAssignableFrom (long id, long c_id) {
2092 return SendReceive (CommandSet.TYPE, (int)CmdType.IS_ASSIGNABLE_FROM, new PacketWriter ().WriteId (id).WriteId (c_id)).ReadByte () > 0;
2095 internal CattrInfo[] Type_GetCustomAttributes (long id, long attr_type_id, bool inherit) {
2096 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_CATTRS, new PacketWriter ().WriteId (id).WriteId (attr_type_id));
2097 return ReadCattrs (r);
2100 internal CattrInfo[] Type_GetFieldCustomAttributes (long id, long field_id, long attr_type_id, bool inherit) {
2101 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_FIELD_CATTRS, new PacketWriter ().WriteId (id).WriteId (field_id).WriteId (attr_type_id));
2102 return ReadCattrs (r);
2105 internal CattrInfo[] Type_GetPropertyCustomAttributes (long id, long field_id, long attr_type_id, bool inherit) {
2106 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_PROPERTY_CATTRS, new PacketWriter ().WriteId (id).WriteId (field_id).WriteId (attr_type_id));
2107 return ReadCattrs (r);
2110 public long[] Type_GetMethodsByNameFlags (long id, string name, int flags, bool ignoreCase) {
2111 flags |= ignoreCase ? (int)BindingFlagsExtensions.BINDING_FLAGS_IGNORE_CASE : 0;
2112 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.CMD_TYPE_GET_METHODS_BY_NAME_FLAGS, new PacketWriter ().WriteId (id).WriteString (name).WriteInt (flags));
2113 int len = r.ReadInt ();
2114 long[] res = new long [len];
2115 for (int i = 0; i < len; ++i)
2116 res [i] = r.ReadId ();
2120 internal long[] Type_GetInterfaces (long id) {
2121 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_INTERFACES, new PacketWriter ().WriteId (id));
2122 int len = r.ReadInt ();
2123 return r.ReadIds (len);
2126 internal IfaceMapInfo[] Type_GetInterfaceMap (long id, long[] ids) {
2127 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_INTERFACE_MAP, new PacketWriter ().WriteId (id).WriteInt (ids.Length).WriteIds (ids));
2128 var res = new IfaceMapInfo [ids.Length];
2129 for (int i = 0; i < ids.Length; ++i) {
2130 int n = r.ReadInt ();
2132 res [i].iface_id = ids [i];
2133 res [i].iface_methods = r.ReadIds (n);
2134 res [i].target_methods = r.ReadIds (n);
2140 internal bool Type_IsInitialized (long id) {
2141 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.IS_INITIALIZED, new PacketWriter ().WriteId (id));
2142 return r.ReadInt () == 1;
2149 internal int EnableEvent (EventType etype, SuspendPolicy suspend_policy, List<Modifier> mods) {
2150 var w = new PacketWriter ().WriteByte ((byte)etype).WriteByte ((byte)suspend_policy);
2152 if (mods.Count > 255)
2153 throw new NotImplementedException ();
2154 w.WriteByte ((byte)mods.Count);
2155 foreach (Modifier mod in mods) {
2156 if (mod is CountModifier) {
2157 w.WriteByte ((byte)ModifierKind.COUNT);
2158 w.WriteInt ((mod as CountModifier).Count);
2159 } else if (mod is LocationModifier) {
2160 w.WriteByte ((byte)ModifierKind.LOCATION_ONLY);
2161 w.WriteId ((mod as LocationModifier).Method);
2162 w.WriteLong ((mod as LocationModifier).Location);
2163 } else if (mod is StepModifier) {
2164 w.WriteByte ((byte)ModifierKind.STEP);
2165 w.WriteId ((mod as StepModifier).Thread);
2166 w.WriteInt ((mod as StepModifier).Size);
2167 w.WriteInt ((mod as StepModifier).Depth);
2168 if (Version.AtLeast (2, 16))
2169 w.WriteInt ((mod as StepModifier).Filter);
2170 } else if (mod is ThreadModifier) {
2171 w.WriteByte ((byte)ModifierKind.THREAD_ONLY);
2172 w.WriteId ((mod as ThreadModifier).Thread);
2173 } else if (mod is ExceptionModifier) {
2174 var em = mod as ExceptionModifier;
2175 w.WriteByte ((byte)ModifierKind.EXCEPTION_ONLY);
2176 w.WriteId (em.Type);
2177 if (Version.MajorVersion > 2 || Version.MinorVersion > 0) {
2178 /* This is only supported in protocol version 2.1 */
2179 w.WriteBool (em.Caught);
2180 w.WriteBool (em.Uncaught);
2181 } else if (!em.Caught || !em.Uncaught) {
2182 throw new NotSupportedException ("This request is not supported by the protocol version implemented by the debuggee.");
2184 if (Version.MajorVersion > 2 || Version.MinorVersion > 24) {
2185 w.WriteBool (em.Subclasses);
2186 } else if (!em.Subclasses) {
2187 throw new NotSupportedException ("This request is not supported by the protocol version implemented by the debuggee.");
2189 } else if (mod is AssemblyModifier) {
2190 w.WriteByte ((byte)ModifierKind.ASSEMBLY_ONLY);
2191 var amod = (mod as AssemblyModifier);
2192 w.WriteInt (amod.Assemblies.Length);
2193 foreach (var id in amod.Assemblies)
2195 } else if (mod is SourceFileModifier) {
2196 w.WriteByte ((byte)ModifierKind.SOURCE_FILE_ONLY);
2197 var smod = (mod as SourceFileModifier);
2198 w.WriteInt (smod.SourceFiles.Length);
2199 foreach (var s in smod.SourceFiles)
2201 } else if (mod is TypeNameModifier) {
2202 w.WriteByte ((byte)ModifierKind.TYPE_NAME_ONLY);
2203 var tmod = (mod as TypeNameModifier);
2204 w.WriteInt (tmod.TypeNames.Length);
2205 foreach (var s in tmod.TypeNames)
2208 throw new NotImplementedException ();
2214 return SendReceive (CommandSet.EVENT_REQUEST, (int)CmdEventRequest.SET, w).ReadInt ();
2217 internal void ClearEventRequest (EventType etype, int req_id) {
2218 SendReceive (CommandSet.EVENT_REQUEST, (int)CmdEventRequest.CLEAR, new PacketWriter ().WriteByte ((byte)etype).WriteInt (req_id));
2221 internal void ClearAllBreakpoints () {
2222 SendReceive (CommandSet.EVENT_REQUEST, (int)CmdEventRequest.CLEAR_ALL_BREAKPOINTS, new PacketWriter ());
2228 internal ValueImpl StackFrame_GetThis (long thread_id, long id) {
2229 PacketReader r = SendReceive (CommandSet.STACK_FRAME, (int)CmdStackFrame.GET_THIS, new PacketWriter ().WriteId (thread_id).WriteId (id));
2230 return r.ReadValue ();
2233 internal ValueImpl[] StackFrame_GetValues (long thread_id, long id, int[] pos) {
2234 /* pos < 0 -> argument at pos (-pos) - 1 */
2235 /* pos >= 0 -> local at pos */
2236 int len = pos.Length;
2237 PacketReader r = SendReceive (CommandSet.STACK_FRAME, (int)CmdStackFrame.GET_VALUES, new PacketWriter ().WriteId (thread_id).WriteId (id).WriteInt (len).WriteInts (pos));
2239 ValueImpl[] res = new ValueImpl [len];
2240 for (int i = 0; i < len; ++i)
2241 res [i] = r.ReadValue ();
2245 internal void StackFrame_SetValues (long thread_id, long id, int[] pos, ValueImpl[] values) {
2246 /* pos < 0 -> argument at pos (-pos) - 1 */
2247 /* pos >= 0 -> local at pos */
2248 int len = pos.Length;
2249 SendReceive (CommandSet.STACK_FRAME, (int)CmdStackFrame.SET_VALUES, new PacketWriter ().WriteId (thread_id).WriteId (id).WriteInt (len).WriteInts (pos).WriteValues (values));
2255 internal int[] Array_GetLength (long id, out int rank, out int[] lower_bounds) {
2256 var r = SendReceive (CommandSet.ARRAY_REF, (int)CmdArrayRef.GET_LENGTH, new PacketWriter ().WriteId (id));
2257 rank = r.ReadInt ();
2258 int[] res = new int [rank];
2259 lower_bounds = new int [rank];
2260 for (int i = 0; i < rank; ++i) {
2261 res [i] = r.ReadInt ();
2262 lower_bounds [i] = r.ReadInt ();
2267 internal ValueImpl[] Array_GetValues (long id, int index, int len) {
2268 var r = SendReceive (CommandSet.ARRAY_REF, (int)CmdArrayRef.GET_VALUES, new PacketWriter ().WriteId (id).WriteInt (index).WriteInt (len));
2269 ValueImpl[] res = new ValueImpl [len];
2270 for (int i = 0; i < len; ++i)
2271 res [i] = r.ReadValue ();
2275 internal void Array_SetValues (long id, int index, ValueImpl[] values) {
2276 SendReceive (CommandSet.ARRAY_REF, (int)CmdArrayRef.SET_VALUES, new PacketWriter ().WriteId (id).WriteInt (index).WriteInt (values.Length).WriteValues (values));
2282 internal string String_GetValue (long id) {
2283 return SendReceive (CommandSet.STRING_REF, (int)CmdStringRef.GET_VALUE, new PacketWriter ().WriteId (id)).ReadString ();
2286 internal int String_GetLength (long id) {
2287 return (int)SendReceive (CommandSet.STRING_REF, (int)CmdStringRef.GET_LENGTH, new PacketWriter ().WriteId (id)).ReadLong ();
2290 internal char[] String_GetChars (long id, int index, int length) {
2291 var r = SendReceive (CommandSet.STRING_REF, (int)CmdStringRef.GET_CHARS, new PacketWriter ().WriteId (id).WriteLong (index).WriteLong (length));
2292 var res = new char [length];
2293 for (int i = 0; i < length; ++i)
2294 res [i] = (char)r.ReadShort ();
2301 internal long Object_GetType (long id) {
2302 return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_TYPE, new PacketWriter ().WriteId (id)).ReadId ();
2305 internal long Object_GetDomain (long id) {
2306 return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_DOMAIN, new PacketWriter ().WriteId (id)).ReadId ();
2309 internal ValueImpl[] Object_GetValues (long id, long[] fields) {
2310 int len = fields.Length;
2311 PacketReader r = SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_VALUES, new PacketWriter ().WriteId (id).WriteInt (len).WriteIds (fields));
2313 ValueImpl[] res = new ValueImpl [len];
2314 for (int i = 0; i < len; ++i)
2315 res [i] = r.ReadValue ();
2319 internal void Object_SetValues (long id, long[] fields, ValueImpl[] values) {
2320 SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.SET_VALUES, new PacketWriter ().WriteId (id).WriteInt (fields.Length).WriteIds (fields).WriteValues (values));
2323 internal bool Object_IsCollected (long id) {
2324 return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.IS_COLLECTED, new PacketWriter ().WriteId (id)).ReadInt () == 1;
2327 internal long Object_GetAddress (long id) {
2328 return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_ADDRESS, new PacketWriter ().WriteId (id)).ReadLong ();
2331 internal ObjectRefInfo Object_GetInfo (long id) {
2332 ObjectRefInfo res = new ObjectRefInfo ();
2333 PacketReader r = SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_INFO, new PacketWriter ().WriteId (id));
2335 res.type_id = r.ReadId ();
2336 res.domain_id = r.ReadId ();
2340 public void ForceDisconnect ()
2343 disconnected = true;
2348 class TcpConnection : Connection
2352 internal TcpConnection (Socket socket)
2354 this.socket = socket;
2355 //socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.NoDelay, 1);
2358 internal EndPoint EndPoint {
2360 return socket.RemoteEndPoint;
2364 protected override int TransportSend (byte[] buf, int buf_offset, int len)
2366 return socket.Send (buf, buf_offset, len, SocketFlags.None);
2369 protected override int TransportReceive (byte[] buf, int buf_offset, int len)
2371 return socket.Receive (buf, buf_offset, len, SocketFlags.None);
2374 protected override void TransportSetTimeouts (int send_timeout, int receive_timeout)
2376 socket.SendTimeout = send_timeout;
2377 socket.ReceiveTimeout = receive_timeout;
2380 protected override void TransportClose ()
2386 /* This is the interface exposed by the debugger towards the debugger agent */
2387 interface IEventHandler
2389 void Events (SuspendPolicy suspend_policy, EventInfo[] events);
2391 void VMDisconnect (int req_id, long thread_id, string vm_uri);