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 int[] end_line_numbers;
48 public int[] end_column_numbers;
49 public SourceInfo[] source_files;
56 public StackFrameFlags flags;
60 public string ns, name, full_name;
61 public long assembly, module, base_type, element_type;
62 public int token, rank, attributes;
63 public bool is_byref, is_pointer, is_primitive, is_valuetype, is_enum;
64 public bool is_gtd, is_generic_type;
67 public long[] type_args;
72 public long[] iface_methods;
73 public long[] target_methods;
77 public int attributes, iattributes, token;
78 public bool is_gmd, is_generic_method;
80 public long[] type_args;
83 class MethodBodyInfo {
85 public ExceptionClauseInfo[] clauses;
88 struct ExceptionClauseInfo {
89 public ExceptionClauseFlags flags;
90 public int try_offset;
91 public int try_length;
92 public int handler_offset;
93 public int handler_length;
94 public int filter_offset;
95 public long catch_type_id;
99 enum ExceptionClauseFlags {
107 public int call_conv;
108 public int param_count;
109 public int generic_param_count;
110 public long ret_type;
111 public long[] param_types;
112 public string[] param_names;
117 public string[] names;
118 public int[] live_range_start;
119 public int[] live_range_end;
125 public long get_method, set_method;
129 class CattrNamedArgInfo {
130 public bool is_property;
132 public ValueImpl value;
137 public ValueImpl[] ctor_args;
138 public CattrNamedArgInfo[] named_args;
142 public bool is_thread_pool;
145 struct ObjectRefInfo {
147 public long domain_id;
151 VALUE_TYPE_ID_NULL = 0xf0,
152 VALUE_TYPE_ID_TYPE = 0xf1,
153 VALUE_TYPE_ID_PARENT_VTYPE = 0xf2
159 DISABLE_BREAKPOINTS = 0x1,
160 SINGLE_THREADED = 0x2
206 public ElementType Type; /* or one of the VALUE_TYPE_ID constants */
209 public long Klass; // For ElementType.ValueType
210 public ValueImpl[] Fields; // for ElementType.ValueType
211 public bool IsEnum; // For ElementType.ValueType
212 public long Id; /* For VALUE_TYPE_ID_TYPE */
213 public int Index; /* For VALUE_TYPE_PARENT_VTYPE */
217 public string Name, ScopeName, FQName, Guid;
218 public long Assembly;
221 class FieldMirrorInfo {
223 public long Parent, TypeId;
236 enum StackFrameFlags {
239 NATIVE_TRANSITION = 2
242 class ResolvedToken {
243 public TokenType Type;
251 class CountModifier : Modifier {
257 class LocationModifier : Modifier {
262 public long Location {
267 class StepModifier : Modifier {
285 class ThreadModifier : Modifier {
291 class ExceptionModifier : Modifier {
298 public bool Uncaught {
301 public bool Subclasses {
306 class AssemblyModifier : Modifier {
307 public long[] Assemblies {
312 class SourceFileModifier : Modifier {
313 public string[] SourceFiles {
318 class TypeNameModifier : Modifier {
319 public string[] TypeNames {
325 public EventType EventType {
333 public SuspendPolicy SuspendPolicy {
337 public long ThreadId {
345 public long Location {
353 public string Category {
357 public string Message {
361 public int ExitCode {
365 public EventInfo (EventType type, int req_id) {
371 public enum ErrorCode {
374 INVALID_FIELDID = 25,
375 INVALID_FRAMEID = 30,
376 NOT_IMPLEMENTED = 100,
378 INVALID_ARGUMENT = 102,
380 ERR_NO_INVOCATION = 104,
381 ABSENT_INFORMATION = 105,
382 NO_SEQ_POINT_AT_IL_OFFSET = 106
385 public class ErrorHandlerEventArgs : EventArgs {
387 public ErrorCode ErrorCode {
393 * Represents the connection to the debuggee
395 public abstract class Connection
398 * The protocol and the packet format is based on JDWP, the differences
399 * are in the set of supported events, and the commands.
401 internal const string HANDSHAKE_STRING = "DWP-Handshake";
403 internal const int HEADER_LENGTH = 11;
405 static readonly bool EnableConnectionLogging = !String.IsNullOrEmpty (Environment.GetEnvironmentVariable ("MONO_SDB_LOG"));
406 static int ConnectionId;
407 readonly StreamWriter LoggingStream = EnableConnectionLogging ?
408 new StreamWriter (string.Format ("/tmp/sdb_conn_log_{0}", ConnectionId++), false) : null;
411 * Th version of the wire-protocol implemented by the library. The library
412 * and the debuggee can communicate if they implement the same major version.
413 * If they implement a different minor version, they can communicate, but some
414 * features might not be available. This allows older clients to communicate
415 * with newer runtimes, and vice versa.
417 internal const int MAJOR_VERSION = 2;
418 internal const int MINOR_VERSION = 34;
420 enum WPSuspendPolicy {
448 APPDOMAIN_CREATE = 4, // Not in JDI
449 APPDOMAIN_UNLOAD = 5, // Not in JDI
470 SOURCE_FILE_ONLY = 12,
482 SET_PROTOCOL_VERSION = 8,
485 GET_TYPES_FOR_SOURCE_FILE = 11,
488 START_BUFFERING = 14,
501 /* FIXME: Merge into GET_INFO when the major protocol version is increased */
508 enum CmdEventRequest {
511 CLEAR_ALL_BREAKPOINTS = 3
516 GET_FRIENDLY_NAME = 2,
518 GET_ENTRY_ASSEMBLY = 4,
521 CREATE_BOXED_VALUE = 7
527 GET_MANIFEST_MODULE = 3,
539 GET_DECLARING_TYPE = 2,
547 MAKE_GENERIC_METHOD = 10
556 GET_SOURCE_FILES = 6,
558 IS_ASSIGNABLE_FROM = 8,
561 GET_FIELD_CATTRS = 11,
562 GET_PROPERTY_CATTRS = 12,
563 /* FIXME: Merge into GET_SOURCE_FILES when the major protocol version is increased */
564 GET_SOURCE_FILES_2 = 13,
565 /* FIXME: Merge into GET_VALUES when the major protocol version is increased */
567 CMD_TYPE_GET_METHODS_BY_NAME_FLAGS = 15,
569 GET_INTERFACE_MAP = 17,
579 enum BindingFlagsExtensions {
580 BINDING_FLAGS_IGNORE_CASE = 0x70000000,
613 public int command_set;
618 internal static int GetPacketLength (byte[] header) {
620 return decode_int (header, ref offset);
623 internal static bool IsReplyPacket (byte[] packet) {
625 return decode_byte (packet, ref offset) == 0x80;
628 internal static int GetPacketId (byte[] packet) {
630 return decode_int (packet, ref offset);
633 static int decode_byte (byte[] packet, ref int offset) {
634 return packet [offset++];
637 static int decode_short (byte[] packet, ref int offset) {
638 int res = ((int)packet [offset] << 8) | (int)packet [offset + 1];
643 static int decode_int (byte[] packet, ref int offset) {
644 int res = ((int)packet [offset] << 24) | ((int)packet [offset + 1] << 16) | ((int)packet [offset + 2] << 8) | (int)packet [offset + 3];
649 static long decode_id (byte[] packet, ref int offset) {
650 return decode_int (packet, ref offset);
653 static long decode_long (byte[] packet, ref int offset) {
654 uint high = (uint)decode_int (packet, ref offset);
655 uint low = (uint)decode_int (packet, ref offset);
657 return (long)(((ulong)high << 32) | (ulong)low);
660 internal static SuspendPolicy decode_suspend_policy (int suspend_policy) {
661 switch ((WPSuspendPolicy)suspend_policy) {
662 case WPSuspendPolicy.NONE:
663 return SuspendPolicy.None;
664 case WPSuspendPolicy.EVENT_THREAD:
665 return SuspendPolicy.EventThread;
666 case WPSuspendPolicy.ALL:
667 return SuspendPolicy.All;
669 throw new NotImplementedException ();
673 static Header decode_command_header (byte[] packet) {
675 Header res = new Header ();
677 decode_int (packet, ref offset);
678 res.id = decode_int (packet, ref offset);
679 res.flags = decode_byte (packet, ref offset);
680 res.command_set = decode_byte (packet, ref offset);
681 res.command = decode_byte (packet, ref offset);
686 static void encode_byte (byte[] buf, int b, ref int offset) {
687 buf [offset] = (byte)b;
691 static void encode_int (byte[] buf, int i, ref int offset) {
692 buf [offset] = (byte)((i >> 24) & 0xff);
693 buf [offset + 1] = (byte)((i >> 16) & 0xff);
694 buf [offset + 2] = (byte)((i >> 8) & 0xff);
695 buf [offset + 3] = (byte)((i >> 0) & 0xff);
699 static void encode_id (byte[] buf, long id, ref int offset) {
700 encode_int (buf, (int)id, ref offset);
703 static void encode_long (byte[] buf, long l, ref int offset) {
704 encode_int (buf, (int)((l >> 32) & 0xffffffff), ref offset);
705 encode_int (buf, (int)(l & 0xffffffff), ref offset);
708 internal static byte[] EncodePacket (int id, int commandSet, int command, byte[] data, int dataLen) {
709 byte[] buf = new byte [dataLen + 11];
712 encode_int (buf, buf.Length, ref offset);
713 encode_int (buf, id, ref offset);
714 encode_byte (buf, 0, ref offset);
715 encode_byte (buf, commandSet, ref offset);
716 encode_byte (buf, command, ref offset);
718 for (int i = 0; i < dataLen; ++i)
719 buf [offset + i] = data [i];
728 public PacketReader (byte[] packet) {
729 this.packet = packet;
732 Header header = decode_command_header (packet);
733 CommandSet = (CommandSet)header.command_set;
734 Command = header.command;
738 ReadInt (); // length
740 ReadByte (); // flags
741 ErrorCode = ReadShort ();
744 public CommandSet CommandSet {
752 public int ErrorCode {
762 public int ReadByte () {
763 return decode_byte (packet, ref offset);
766 public int ReadShort () {
767 return decode_short (packet, ref offset);
770 public int ReadInt () {
771 return decode_int (packet, ref offset);
774 public long ReadId () {
775 return decode_id (packet, ref offset);
778 public long ReadLong () {
779 return decode_long (packet, ref offset);
782 public float ReadFloat () {
783 float f = DataConverter.FloatFromBE (packet, offset);
788 public double ReadDouble () {
789 double d = DataConverter.DoubleFromBE (packet, offset);
794 public string ReadString () {
795 int len = decode_int (packet, ref offset);
796 string res = new String (Encoding.UTF8.GetChars (packet, offset, len));
801 public ValueImpl ReadValue () {
802 ElementType etype = (ElementType)ReadByte ();
805 case ElementType.Void:
806 return new ValueImpl { Type = etype };
808 return new ValueImpl { Type = etype, Value = (sbyte)ReadInt () };
810 return new ValueImpl { Type = etype, Value = (byte)ReadInt () };
811 case ElementType.Boolean:
812 return new ValueImpl { Type = etype, Value = ReadInt () != 0 };
814 return new ValueImpl { Type = etype, Value = (short)ReadInt () };
816 return new ValueImpl { Type = etype, Value = (ushort)ReadInt () };
817 case ElementType.Char:
818 return new ValueImpl { Type = etype, Value = (char)ReadInt () };
820 return new ValueImpl { Type = etype, Value = ReadInt () };
822 return new ValueImpl { Type = etype, Value = (uint)ReadInt () };
824 return new ValueImpl { Type = etype, Value = ReadLong () };
826 return new ValueImpl { Type = etype, Value = (ulong)ReadLong () };
828 return new ValueImpl { Type = etype, Value = ReadFloat () };
830 return new ValueImpl { Type = etype, Value = ReadDouble () };
833 case ElementType.Ptr:
834 // FIXME: The client and the debuggee might have different word sizes
835 return new ValueImpl { Type = etype, Value = ReadLong () };
836 case ElementType.String:
837 case ElementType.SzArray:
838 case ElementType.Class:
839 case ElementType.Array:
840 case ElementType.Object:
841 long objid = ReadId ();
842 return new ValueImpl () { Type = etype, Objid = objid };
843 case ElementType.ValueType:
844 bool is_enum = ReadByte () == 1;
845 long klass = ReadId ();
846 long nfields = ReadInt ();
847 ValueImpl[] fields = new ValueImpl [nfields];
848 for (int i = 0; i < nfields; ++i)
849 fields [i] = ReadValue ();
850 return new ValueImpl () { Type = etype, Klass = klass, Fields = fields, IsEnum = is_enum };
851 case (ElementType)ValueTypeId.VALUE_TYPE_ID_NULL:
852 return new ValueImpl { Type = etype };
853 case (ElementType)ValueTypeId.VALUE_TYPE_ID_TYPE:
854 return new ValueImpl () { Type = etype, Id = ReadId () };
855 case (ElementType)ValueTypeId.VALUE_TYPE_ID_PARENT_VTYPE:
856 return new ValueImpl () { Type = etype, Index = ReadInt () };
858 throw new NotImplementedException ("Unable to handle type " + etype);
862 public long[] ReadIds (int n) {
863 long[] res = new long [n];
864 for (int i = 0; i < n; ++i)
875 public PacketWriter () {
876 data = new byte [1024];
880 void MakeRoom (int size) {
881 if (offset + size >= data.Length) {
882 int new_len = data.Length * 2;
883 while (new_len < offset + size) {
886 byte[] new_data = new byte [new_len];
887 Array.Copy (data, new_data, data.Length);
892 public PacketWriter WriteByte (byte val) {
894 encode_byte (data, val, ref offset);
898 public PacketWriter WriteInt (int val) {
900 encode_int (data, val, ref offset);
904 public PacketWriter WriteId (long id) {
906 encode_id (data, id, ref offset);
910 public PacketWriter WriteLong (long val) {
912 encode_long (data, val, ref offset);
916 public PacketWriter WriteFloat (float f) {
918 byte[] b = DataConverter.GetBytesBE (f);
919 for (int i = 0; i < 4; ++i)
920 data [offset + i] = b [i];
925 public PacketWriter WriteDouble (double d) {
927 byte[] b = DataConverter.GetBytesBE (d);
928 for (int i = 0; i < 8; ++i)
929 data [offset + i] = b [i];
934 public PacketWriter WriteInts (int[] ids) {
935 for (int i = 0; i < ids.Length; ++i)
940 public PacketWriter WriteIds (long[] ids) {
941 for (int i = 0; i < ids.Length; ++i)
946 public PacketWriter WriteString (string s) {
948 return WriteInt (-1);
950 byte[] b = Encoding.UTF8.GetBytes (s);
952 encode_int (data, b.Length, ref offset);
954 Buffer.BlockCopy (b, 0, data, offset, b.Length);
959 public PacketWriter WriteBool (bool val) {
960 WriteByte (val ? (byte)1 : (byte)0);
964 public PacketWriter WriteValue (ValueImpl v) {
968 t = TypeCodeToElementType (Type.GetTypeCode (v.Value.GetType ()));
973 case ElementType.Boolean:
974 WriteInt ((bool)v.Value ? 1 : 0);
976 case ElementType.Char:
977 WriteInt ((int)(char)v.Value);
980 WriteInt ((int)(sbyte)v.Value);
983 WriteInt ((int)(byte)v.Value);
986 WriteInt ((int)(short)v.Value);
989 WriteInt ((int)(ushort)v.Value);
992 WriteInt ((int)(int)v.Value);
995 WriteInt ((int)(uint)v.Value);
998 WriteLong ((long)(long)v.Value);
1000 case ElementType.U8:
1001 WriteLong ((long)(ulong)v.Value);
1003 case ElementType.R4:
1004 WriteFloat ((float)v.Value);
1006 case ElementType.R8:
1007 WriteDouble ((double)v.Value);
1009 case ElementType.String:
1010 case ElementType.SzArray:
1011 case ElementType.Class:
1012 case ElementType.Array:
1013 case ElementType.Object:
1016 case ElementType.ValueType:
1019 throw new NotImplementedException ();
1022 WriteInt (v.Fields.Length);
1023 for (int i = 0; i < v.Fields.Length; ++i)
1024 WriteValue (v.Fields [i]);
1026 case (ElementType)ValueTypeId.VALUE_TYPE_ID_NULL:
1029 throw new NotImplementedException ();
1035 public PacketWriter WriteValues (ValueImpl[] values) {
1036 for (int i = 0; i < values.Length; ++i)
1037 WriteValue (values [i]);
1041 public byte[] Data {
1054 delegate void ReplyCallback (int packet_id, byte[] packet);
1057 Thread receiver_thread;
1058 Dictionary<int, byte[]> reply_packets;
1059 Dictionary<int, ReplyCallback> reply_cbs;
1060 Dictionary<int, int> reply_cb_counts;
1061 object reply_packets_monitor;
1063 internal event EventHandler<ErrorHandlerEventArgs> ErrorHandler;
1065 protected Connection () {
1067 reply_packets = new Dictionary<int, byte[]> ();
1068 reply_cbs = new Dictionary<int, ReplyCallback> ();
1069 reply_cb_counts = new Dictionary<int, int> ();
1070 reply_packets_monitor = new Object ();
1073 protected abstract int TransportReceive (byte[] buf, int buf_offset, int len);
1074 protected abstract int TransportSend (byte[] buf, int buf_offset, int len);
1075 protected abstract void TransportSetTimeouts (int send_timeout, int receive_timeout);
1076 protected abstract void TransportClose ();
1078 internal VersionInfo Version;
1080 int Receive (byte[] buf, int buf_offset, int len) {
1083 while (offset < len) {
1084 int n = TransportReceive (buf, buf_offset + offset, len - offset);
1094 // Do the wire protocol handshake
1095 internal void Connect () {
1096 byte[] buf = new byte [HANDSHAKE_STRING.Length];
1097 char[] cbuf = new char [buf.Length];
1099 // FIXME: Add a timeout
1100 int n = Receive (buf, 0, buf.Length);
1102 throw new IOException ("DWP Handshake failed.");
1103 for (int i = 0; i < buf.Length; ++i)
1104 cbuf [i] = (char)buf [i];
1106 if (new String (cbuf) != HANDSHAKE_STRING)
1107 throw new IOException ("DWP Handshake failed.");
1109 TransportSend (buf, 0, buf.Length);
1111 receiver_thread = new Thread (new ThreadStart (receiver_thread_main));
1112 receiver_thread.Name = "SDB Receiver";
1113 receiver_thread.IsBackground = true;
1114 receiver_thread.Start ();
1116 Version = VM_GetVersion ();
1119 // Tell the debuggee our protocol version, so newer debuggees can work
1120 // with older clients
1124 // Older debuggees might not support this request
1125 EventHandler<ErrorHandlerEventArgs> OrigErrorHandler = ErrorHandler;
1126 ErrorHandler = null;
1127 ErrorHandler += delegate (object sender, ErrorHandlerEventArgs args) {
1128 throw new NotSupportedException ();
1131 VM_SetProtocolVersion (MAJOR_VERSION, MINOR_VERSION);
1132 } catch (NotSupportedException) {
1134 ErrorHandler = OrigErrorHandler;
1137 internal byte[] ReadPacket () {
1138 // FIXME: Throw ClosedConnectionException () if the connection is closed
1139 // FIXME: Throw ClosedConnectionException () if another thread closes the connection
1141 byte[] header = new byte [HEADER_LENGTH];
1143 int len = Receive (header, 0, header.Length);
1145 return new byte [0];
1146 if (len != HEADER_LENGTH) {
1148 throw new IOException ("Packet of length " + len + " is read.");
1151 int packetLength = GetPacketLength (header);
1152 if (packetLength < 11)
1153 throw new IOException ("Invalid packet length.");
1155 if (packetLength == 11) {
1158 byte[] buf = new byte [packetLength];
1159 for (int i = 0; i < header.Length; ++i)
1160 buf [i] = header [i];
1161 len = Receive (buf, header.Length, packetLength - header.Length);
1162 if (len != packetLength - header.Length)
1163 throw new IOException ();
1168 internal void WritePacket (byte[] packet) {
1169 // FIXME: Throw ClosedConnectionException () if the connection is closed
1170 // FIXME: Throw ClosedConnectionException () if another thread closes the connection
1172 TransportSend (packet, 0, packet.Length);
1175 internal void WritePackets (List<byte[]> packets) {
1176 // FIXME: Throw ClosedConnectionException () if the connection is closed
1177 // FIXME: Throw ClosedConnectionException () if another thread closes the connection
1180 for (int i = 0; i < packets.Count; ++i)
1181 len += packets [i].Length;
1182 byte[] data = new byte [len];
1184 for (int i = 0; i < packets.Count; ++i) {
1185 Buffer.BlockCopy (packets [i], 0, data, pos, packets [i].Length);
1186 pos += packets [i].Length;
1188 TransportSend (data, 0, data.Length);
1191 internal void Close () {
1195 internal bool IsClosed {
1203 void receiver_thread_main () {
1206 bool res = ReceivePacket ();
1209 } catch (Exception ex) {
1211 Console.WriteLine (ex);
1217 lock (reply_packets_monitor) {
1218 disconnected = true;
1219 Monitor.PulseAll (reply_packets_monitor);
1222 EventHandler.VMDisconnect (0, 0, null);
1225 bool ReceivePacket () {
1226 byte[] packet = ReadPacket ();
1228 if (packet.Length == 0) {
1232 if (IsReplyPacket (packet)) {
1233 int id = GetPacketId (packet);
1234 ReplyCallback cb = null;
1235 lock (reply_packets_monitor) {
1236 reply_cbs.TryGetValue (id, out cb);
1238 reply_packets [id] = packet;
1239 Monitor.PulseAll (reply_packets_monitor);
1241 int c = reply_cb_counts [id];
1244 reply_cbs.Remove (id);
1245 reply_cb_counts.Remove (id);
1251 cb.Invoke (id, packet);
1253 PacketReader r = new PacketReader (packet);
1255 if (r.CommandSet == CommandSet.EVENT && r.Command == (int)CmdEvent.COMPOSITE) {
1256 int spolicy = r.ReadByte ();
1257 int nevents = r.ReadInt ();
1259 SuspendPolicy suspend_policy = decode_suspend_policy (spolicy);
1261 EventInfo[] events = new EventInfo [nevents];
1263 for (int i = 0; i < nevents; ++i) {
1264 EventKind kind = (EventKind)r.ReadByte ();
1265 int req_id = r.ReadInt ();
1267 EventType etype = (EventType)kind;
1269 long thread_id = r.ReadId ();
1270 if (kind == EventKind.VM_START) {
1271 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id };
1272 //EventHandler.VMStart (req_id, thread_id, null);
1273 } else if (kind == EventKind.VM_DEATH) {
1275 if (Version.AtLeast (2, 27))
1276 exit_code = r.ReadInt ();
1277 //EventHandler.VMDeath (req_id, 0, null);
1278 events [i] = new EventInfo (etype, req_id) { ExitCode = exit_code };
1279 } else if (kind == EventKind.THREAD_START) {
1280 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = thread_id };
1281 //EventHandler.ThreadStart (req_id, thread_id, thread_id);
1282 } else if (kind == EventKind.THREAD_DEATH) {
1283 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = thread_id };
1284 //EventHandler.ThreadDeath (req_id, thread_id, thread_id);
1285 } else if (kind == EventKind.ASSEMBLY_LOAD) {
1286 long id = r.ReadId ();
1287 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1288 //EventHandler.AssemblyLoad (req_id, thread_id, id);
1289 } else if (kind == EventKind.ASSEMBLY_UNLOAD) {
1290 long id = r.ReadId ();
1291 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1292 //EventHandler.AssemblyUnload (req_id, thread_id, id);
1293 } else if (kind == EventKind.TYPE_LOAD) {
1294 long id = r.ReadId ();
1295 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1296 //EventHandler.TypeLoad (req_id, thread_id, id);
1297 } else if (kind == EventKind.METHOD_ENTRY) {
1298 long id = r.ReadId ();
1299 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1300 //EventHandler.MethodEntry (req_id, thread_id, id);
1301 } else if (kind == EventKind.METHOD_EXIT) {
1302 long id = r.ReadId ();
1303 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1304 //EventHandler.MethodExit (req_id, thread_id, id);
1305 } else if (kind == EventKind.BREAKPOINT) {
1306 long id = r.ReadId ();
1307 long loc = r.ReadLong ();
1308 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1309 //EventHandler.Breakpoint (req_id, thread_id, id, loc);
1310 } else if (kind == EventKind.STEP) {
1311 long id = r.ReadId ();
1312 long loc = r.ReadLong ();
1313 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1314 //EventHandler.Step (req_id, thread_id, id, loc);
1315 } else if (kind == EventKind.EXCEPTION) {
1316 long id = r.ReadId ();
1317 long loc = 0; // FIXME
1318 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1319 //EventHandler.Exception (req_id, thread_id, id, loc);
1320 } else if (kind == EventKind.APPDOMAIN_CREATE) {
1321 long id = r.ReadId ();
1322 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1323 //EventHandler.AppDomainCreate (req_id, thread_id, id);
1324 } else if (kind == EventKind.APPDOMAIN_UNLOAD) {
1325 long id = r.ReadId ();
1326 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1327 //EventHandler.AppDomainUnload (req_id, thread_id, id);
1328 } else if (kind == EventKind.USER_BREAK) {
1331 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1332 //EventHandler.Exception (req_id, thread_id, id, loc);
1333 } else if (kind == EventKind.USER_LOG) {
1334 int level = r.ReadInt ();
1335 string category = r.ReadString ();
1336 string message = r.ReadString ();
1337 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Level = level, Category = category, Message = message };
1338 //EventHandler.Exception (req_id, thread_id, id, loc);
1339 } else if (kind == EventKind.KEEPALIVE) {
1340 events [i] = new EventInfo (etype, req_id) { };
1342 throw new NotImplementedException ("Unknown event kind: " + kind);
1346 EventHandler.Events (suspend_policy, events);
1353 internal IEventHandler EventHandler {
1357 static String CommandString (CommandSet command_set, int command)
1360 switch (command_set) {
1362 cmd = ((CmdVM)command).ToString ();
1364 case CommandSet.OBJECT_REF:
1365 cmd = ((CmdObjectRef)command).ToString ();
1367 case CommandSet.STRING_REF:
1368 cmd = ((CmdStringRef)command).ToString ();
1370 case CommandSet.THREAD:
1371 cmd = ((CmdThread)command).ToString ();
1373 case CommandSet.ARRAY_REF:
1374 cmd = ((CmdArrayRef)command).ToString ();
1376 case CommandSet.EVENT_REQUEST:
1377 cmd = ((CmdEventRequest)command).ToString ();
1379 case CommandSet.STACK_FRAME:
1380 cmd = ((CmdStackFrame)command).ToString ();
1382 case CommandSet.APPDOMAIN:
1383 cmd = ((CmdAppDomain)command).ToString ();
1385 case CommandSet.ASSEMBLY:
1386 cmd = ((CmdAssembly)command).ToString ();
1388 case CommandSet.METHOD:
1389 cmd = ((CmdMethod)command).ToString ();
1391 case CommandSet.TYPE:
1392 cmd = ((CmdType)command).ToString ();
1394 case CommandSet.MODULE:
1395 cmd = ((CmdModule)command).ToString ();
1397 case CommandSet.FIELD:
1398 cmd = ((CmdField)command).ToString ();
1400 case CommandSet.EVENT:
1401 cmd = ((CmdEvent)command).ToString ();
1404 cmd = command.ToString ();
1407 return string.Format ("[{0} {1}]", command_set, cmd);
1410 long total_protocol_ticks;
1412 void LogPacket (int packet_id, byte[] encoded_packet, byte[] reply_packet, CommandSet command_set, int command, Stopwatch watch) {
1414 total_protocol_ticks += watch.ElapsedTicks;
1415 var ts = TimeSpan.FromTicks (total_protocol_ticks);
1416 string msg = string.Format ("Packet: {0} sent: {1} received: {2} ms: {3} total ms: {4} {5}",
1417 packet_id, encoded_packet.Length, reply_packet.Length, watch.ElapsedMilliseconds,
1418 (ts.Seconds * 1000) + ts.Milliseconds,
1419 CommandString (command_set, command));
1421 LoggingStream.WriteLine (msg);
1422 LoggingStream.Flush ();
1425 bool buffer_packets;
1426 List<byte[]> buffered_packets = new List<byte[]> ();
1429 // Start buffering request/response packets on both the client and the debuggee side.
1430 // Packets sent between StartBuffering ()/StopBuffering () must be async, i.e. sent
1431 // using Send () and not SendReceive ().
1433 public void StartBuffering () {
1434 buffer_packets = true;
1435 if (Version.AtLeast (3, 34))
1436 VM_StartBuffering ();
1439 public void StopBuffering () {
1440 if (Version.AtLeast (3, 34))
1441 VM_StopBuffering ();
1442 buffer_packets = false;
1444 WritePackets (buffered_packets);
1445 if (EnableConnectionLogging) {
1446 LoggingStream.WriteLine (String.Format ("Sent {1} packets.", buffered_packets.Count));
1447 LoggingStream.Flush ();
1449 buffered_packets.Clear ();
1452 /* Send a request and call cb when a result is received */
1453 int Send (CommandSet command_set, int command, PacketWriter packet, Action<PacketReader> cb, int count) {
1454 int id = IdGenerator;
1456 Stopwatch watch = null;
1457 if (EnableConnectionLogging)
1458 watch = Stopwatch.StartNew ();
1460 byte[] encoded_packet;
1462 encoded_packet = EncodePacket (id, (int)command_set, command, null, 0);
1464 encoded_packet = EncodePacket (id, (int)command_set, command, packet.Data, packet.Offset);
1467 lock (reply_packets_monitor) {
1468 reply_cbs [id] = delegate (int packet_id, byte[] p) {
1469 if (EnableConnectionLogging)
1470 LogPacket (packet_id, encoded_packet, p, command_set, command, watch);
1471 /* Run the callback on a tp thread to avoid blocking the receive thread */
1472 PacketReader r = new PacketReader (p);
1473 cb.BeginInvoke (r, null, null);
1475 reply_cb_counts [id] = count;
1480 buffered_packets.Add (encoded_packet);
1482 WritePacket (encoded_packet);
1487 // Send a request without waiting for an answer
1488 void Send (CommandSet command_set, int command) {
1489 Send (command_set, command, null, null, 0);
1492 PacketReader SendReceive (CommandSet command_set, int command, PacketWriter packet) {
1493 int id = IdGenerator;
1494 Stopwatch watch = null;
1497 throw new VMDisconnectedException ();
1499 if (EnableConnectionLogging)
1500 watch = Stopwatch.StartNew ();
1502 byte[] encoded_packet;
1505 encoded_packet = EncodePacket (id, (int)command_set, command, null, 0);
1507 encoded_packet = EncodePacket (id, (int)command_set, command, packet.Data, packet.Offset);
1509 WritePacket (encoded_packet);
1513 /* Wait for the reply packet */
1515 lock (reply_packets_monitor) {
1516 if (reply_packets.ContainsKey (packetId)) {
1517 byte[] reply = reply_packets [packetId];
1518 reply_packets.Remove (packetId);
1519 PacketReader r = new PacketReader (reply);
1521 if (EnableConnectionLogging)
1522 LogPacket (packetId, encoded_packet, reply, command_set, command, watch);
1523 if (r.ErrorCode != 0) {
1524 if (ErrorHandler != null)
1525 ErrorHandler (this, new ErrorHandlerEventArgs () { ErrorCode = (ErrorCode)r.ErrorCode });
1526 throw new NotImplementedException ("No error handler set.");
1532 throw new VMDisconnectedException ();
1533 Monitor.Wait (reply_packets_monitor);
1539 PacketReader SendReceive (CommandSet command_set, int command) {
1540 return SendReceive (command_set, command, null);
1543 int packet_id_generator;
1547 return Interlocked.Increment (ref packet_id_generator);
1551 CattrInfo[] ReadCattrs (PacketReader r) {
1552 CattrInfo[] res = new CattrInfo [r.ReadInt ()];
1553 for (int i = 0; i < res.Length; ++i) {
1554 CattrInfo info = new CattrInfo ();
1555 info.ctor_id = r.ReadId ();
1556 info.ctor_args = new ValueImpl [r.ReadInt ()];
1557 for (int j = 0; j < info.ctor_args.Length; ++j) {
1558 info.ctor_args [j] = r.ReadValue ();
1560 info.named_args = new CattrNamedArgInfo [r.ReadInt ()];
1561 for (int j = 0; j < info.named_args.Length; ++j) {
1562 CattrNamedArgInfo arg = new CattrNamedArgInfo ();
1563 int arg_type = r.ReadByte ();
1564 arg.is_property = arg_type == 0x54;
1565 arg.id = r.ReadId ();
1566 arg.value = r.ReadValue ();
1567 info.named_args [j] = arg;
1574 static ElementType TypeCodeToElementType (TypeCode c) {
1576 case TypeCode.Boolean:
1577 return ElementType.Boolean;
1579 return ElementType.Char;
1580 case TypeCode.SByte:
1581 return ElementType.I1;
1583 return ElementType.U1;
1584 case TypeCode.Int16:
1585 return ElementType.I2;
1586 case TypeCode.UInt16:
1587 return ElementType.U2;
1588 case TypeCode.Int32:
1589 return ElementType.I4;
1590 case TypeCode.UInt32:
1591 return ElementType.U4;
1592 case TypeCode.Int64:
1593 return ElementType.I8;
1594 case TypeCode.UInt64:
1595 return ElementType.U8;
1596 case TypeCode.Single:
1597 return ElementType.R4;
1598 case TypeCode.Double:
1599 return ElementType.R8;
1601 throw new NotImplementedException ();
1606 * Implementation of debugger commands
1609 internal VersionInfo VM_GetVersion () {
1610 var res = SendReceive (CommandSet.VM, (int)CmdVM.VERSION, null);
1611 VersionInfo info = new VersionInfo ();
1612 info.VMVersion = res.ReadString ();
1613 info.MajorVersion = res.ReadInt ();
1614 info.MinorVersion = res.ReadInt ();
1618 internal void VM_SetProtocolVersion (int major, int minor) {
1619 SendReceive (CommandSet.VM, (int)CmdVM.SET_PROTOCOL_VERSION, new PacketWriter ().WriteInt (major).WriteInt (minor));
1622 internal long[] VM_GetThreads () {
1623 var res = SendReceive (CommandSet.VM, (int)CmdVM.ALL_THREADS, null);
1624 int len = res.ReadInt ();
1625 long[] arr = new long [len];
1626 for (int i = 0; i < len; ++i)
1627 arr [i] = res.ReadId ();
1631 internal void VM_Suspend () {
1632 SendReceive (CommandSet.VM, (int)CmdVM.SUSPEND);
1635 internal void VM_Resume () {
1636 SendReceive (CommandSet.VM, (int)CmdVM.RESUME);
1639 internal void VM_Exit (int exitCode) {
1640 SendReceive (CommandSet.VM, (int)CmdVM.EXIT, new PacketWriter ().WriteInt (exitCode));
1643 internal void VM_Dispose () {
1644 SendReceive (CommandSet.VM, (int)CmdVM.DISPOSE);
1647 internal ValueImpl VM_InvokeMethod (long thread, long method, ValueImpl this_arg, ValueImpl[] arguments, InvokeFlags flags, out ValueImpl exc) {
1649 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));
1650 if (r.ReadByte () == 0) {
1651 exc = r.ReadValue ();
1654 return r.ReadValue ();
1658 internal delegate void InvokeMethodCallback (ValueImpl v, ValueImpl exc, ErrorCode error, object state);
1660 internal int VM_BeginInvokeMethod (long thread, long method, ValueImpl this_arg, ValueImpl[] arguments, InvokeFlags flags, InvokeMethodCallback callback, object state) {
1661 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) {
1664 if (r.ErrorCode != 0) {
1665 callback (null, null, (ErrorCode)r.ErrorCode, state);
1667 if (r.ReadByte () == 0) {
1668 exc = r.ReadValue ();
1675 callback (v, exc, 0, state);
1680 internal int VM_BeginInvokeMethods (long thread, long[] methods, ValueImpl this_arg, List<ValueImpl[]> arguments, InvokeFlags flags, InvokeMethodCallback callback, object state) {
1681 // FIXME: Merge this with INVOKE_METHOD
1682 var w = new PacketWriter ();
1684 w.WriteInt ((int)flags);
1685 w.WriteInt (methods.Length);
1686 for (int i = 0; i < methods.Length; ++i) {
1687 w.WriteId (methods [i]);
1688 w.WriteValue (this_arg);
1689 w.WriteInt (arguments [i].Length);
1690 w.WriteValues (arguments [i]);
1692 return Send (CommandSet.VM, (int)CmdVM.INVOKE_METHODS, w, delegate (PacketReader r) {
1695 if (r.ErrorCode != 0) {
1696 callback (null, null, (ErrorCode)r.ErrorCode, state);
1698 if (r.ReadByte () == 0) {
1699 exc = r.ReadValue ();
1706 callback (v, exc, 0, state);
1711 internal void VM_AbortInvoke (long thread, int id)
1713 SendReceive (CommandSet.VM, (int)CmdVM.ABORT_INVOKE, new PacketWriter ().WriteId (thread).WriteInt (id));
1716 internal void SetSocketTimeouts (int send_timeout, int receive_timeout, int keepalive_interval)
1718 TransportSetTimeouts (send_timeout, receive_timeout);
1719 SendReceive (CommandSet.VM, (int)CmdVM.SET_KEEPALIVE, new PacketWriter ().WriteId (keepalive_interval));
1722 internal long[] VM_GetTypesForSourceFile (string fname, bool ignoreCase) {
1723 var res = SendReceive (CommandSet.VM, (int)CmdVM.GET_TYPES_FOR_SOURCE_FILE, new PacketWriter ().WriteString (fname).WriteBool (ignoreCase));
1724 int count = res.ReadInt ();
1725 long[] types = new long [count];
1726 for (int i = 0; i < count; ++i)
1727 types [i] = res.ReadId ();
1731 internal long[] VM_GetTypes (string name, bool ignoreCase) {
1732 var res = SendReceive (CommandSet.VM, (int)CmdVM.GET_TYPES, new PacketWriter ().WriteString (name).WriteBool (ignoreCase));
1733 int count = res.ReadInt ();
1734 long[] types = new long [count];
1735 for (int i = 0; i < count; ++i)
1736 types [i] = res.ReadId ();
1740 internal void VM_StartBuffering () {
1741 Send (CommandSet.VM, (int)CmdVM.START_BUFFERING);
1744 internal void VM_StopBuffering () {
1745 Send (CommandSet.VM, (int)CmdVM.STOP_BUFFERING);
1752 internal long RootDomain {
1754 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_ROOT_DOMAIN, null).ReadId ();
1758 internal string Domain_GetName (long id) {
1759 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_FRIENDLY_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1762 internal long[] Domain_GetAssemblies (long id) {
1763 var res = SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_ASSEMBLIES, new PacketWriter ().WriteId (id));
1764 int count = res.ReadInt ();
1765 long[] assemblies = new long [count];
1766 for (int i = 0; i < count; ++i)
1767 assemblies [i] = res.ReadId ();
1771 internal long Domain_GetEntryAssembly (long id) {
1772 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_ENTRY_ASSEMBLY, new PacketWriter ().WriteId (id)).ReadId ();
1775 internal long Domain_GetCorlib (long id) {
1776 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_CORLIB, new PacketWriter ().WriteId (id)).ReadId ();
1779 internal long Domain_CreateString (long id, string s) {
1780 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.CREATE_STRING, new PacketWriter ().WriteId (id).WriteString (s)).ReadId ();
1783 internal long Domain_CreateBoxedValue (long id, long type_id, ValueImpl v) {
1784 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.CREATE_BOXED_VALUE, new PacketWriter ().WriteId (id).WriteId (type_id).WriteValue (v)).ReadId ();
1791 internal string Method_GetName (long id) {
1792 return SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1795 internal long Method_GetDeclaringType (long id) {
1796 return SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_DECLARING_TYPE, new PacketWriter ().WriteId (id)).ReadId ();
1799 internal DebugInfo Method_GetDebugInfo (long id) {
1800 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_DEBUG_INFO, new PacketWriter ().WriteId (id));
1802 DebugInfo info = new DebugInfo ();
1803 info.max_il_offset = res.ReadInt ();
1805 SourceInfo[] sources = null;
1806 if (Version.AtLeast (2, 13)) {
1807 int n = res.ReadInt ();
1808 sources = new SourceInfo [n];
1809 for (int i = 0; i < n; ++i) {
1810 sources [i].source_file = res.ReadString ();
1811 if (Version.AtLeast (2, 14)) {
1812 sources [i].hash = new byte [16];
1813 for (int j = 0; j < 16; ++j)
1814 sources [i].hash [j] = (byte)res.ReadByte ();
1818 sources = new SourceInfo [1];
1819 sources [0].source_file = res.ReadString ();
1822 int n_il_offsets = res.ReadInt ();
1823 info.il_offsets = new int [n_il_offsets];
1824 info.line_numbers = new int [n_il_offsets];
1825 info.source_files = new SourceInfo [n_il_offsets];
1826 info.column_numbers = new int [n_il_offsets];
1827 info.end_line_numbers = new int [n_il_offsets];
1828 info.end_column_numbers = new int [n_il_offsets];
1829 for (int i = 0; i < n_il_offsets; ++i) {
1830 info.il_offsets [i] = res.ReadInt ();
1831 info.line_numbers [i] = res.ReadInt ();
1832 if (Version.AtLeast (2, 12)) {
1833 int idx = res.ReadInt ();
1834 info.source_files [i] = idx >= 0 ? sources [idx] : default (SourceInfo);
1836 info.source_files [i] = sources [0];
1838 if (Version.AtLeast (2, 19))
1839 info.column_numbers [i] = res.ReadInt ();
1841 info.column_numbers [i] = 0;
1842 if (Version.AtLeast (2, 32)) {
1843 info.end_line_numbers [i] = res.ReadInt ();
1844 info.end_column_numbers [i] = res.ReadInt ();
1846 info.end_column_numbers [i] = -1;
1847 info.end_column_numbers [i] = -1;
1854 internal ParamInfo Method_GetParamInfo (long id) {
1855 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_PARAM_INFO, new PacketWriter ().WriteId (id));
1857 ParamInfo info = new ParamInfo ();
1858 info.call_conv = res.ReadInt ();
1859 info.param_count = res.ReadInt ();
1860 info.generic_param_count = res.ReadInt ();
1861 info.ret_type = res.ReadId ();
1862 info.param_types = new long [info.param_count];
1863 for (int i = 0; i < info.param_count; ++i)
1864 info.param_types [i] = res.ReadId ();
1865 info.param_names = new string [info.param_count];
1866 for (int i = 0; i < info.param_count; ++i)
1867 info.param_names [i] = res.ReadString ();
1872 internal LocalsInfo Method_GetLocalsInfo (long id) {
1873 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_LOCALS_INFO, new PacketWriter ().WriteId (id));
1875 LocalsInfo info = new LocalsInfo ();
1876 int nlocals = res.ReadInt ();
1877 info.types = new long [nlocals];
1878 for (int i = 0; i < nlocals; ++i)
1879 info.types [i] = res.ReadId ();
1880 info.names = new string [nlocals];
1881 for (int i = 0; i < nlocals; ++i)
1882 info.names [i] = res.ReadString ();
1883 info.live_range_start = new int [nlocals];
1884 info.live_range_end = new int [nlocals];
1885 for (int i = 0; i < nlocals; ++i) {
1886 info.live_range_start [i] = res.ReadInt ();
1887 info.live_range_end [i] = res.ReadInt ();
1893 internal MethodInfo Method_GetInfo (long id) {
1894 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_INFO, new PacketWriter ().WriteId (id));
1896 MethodInfo info = new MethodInfo ();
1897 info.attributes = res.ReadInt ();
1898 info.iattributes = res.ReadInt ();
1899 info.token = res.ReadInt ();
1900 if (Version.AtLeast (2, 12)) {
1901 int attrs = res.ReadByte ();
1902 if ((attrs & (1 << 0)) != 0)
1904 if ((attrs & (1 << 1)) != 0)
1905 info.is_generic_method = true;
1906 info.gmd = res.ReadId ();
1907 if (Version.AtLeast (2, 15)) {
1908 if (info.is_generic_method) {
1909 int n = res.ReadInt ();
1910 info.type_args = res.ReadIds (n);
1917 internal MethodBodyInfo Method_GetBody (long id) {
1918 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_BODY, new PacketWriter ().WriteId (id));
1920 MethodBodyInfo info = new MethodBodyInfo ();
1921 info.il = new byte [res.ReadInt ()];
1922 for (int i = 0; i < info.il.Length; ++i)
1923 info.il [i] = (byte)res.ReadByte ();
1925 if (Version.AtLeast (2, 18)) {
1926 info.clauses = new ExceptionClauseInfo [res.ReadInt ()];
1928 for (int i = 0; i < info.clauses.Length; ++i) {
1929 var clause = new ExceptionClauseInfo {
1930 flags = (ExceptionClauseFlags) res.ReadInt (),
1931 try_offset = res.ReadInt (),
1932 try_length = res.ReadInt (),
1933 handler_offset = res.ReadInt (),
1934 handler_length = res.ReadInt (),
1937 if (clause.flags == ExceptionClauseFlags.None)
1938 clause.catch_type_id = res.ReadId ();
1939 else if (clause.flags == ExceptionClauseFlags.Filter)
1940 clause.filter_offset = res.ReadInt ();
1942 info.clauses [i] = clause;
1945 info.clauses = new ExceptionClauseInfo [0];
1950 internal ResolvedToken Method_ResolveToken (long id, int token) {
1951 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.RESOLVE_TOKEN, new PacketWriter ().WriteId (id).WriteInt (token));
1953 TokenType type = (TokenType)res.ReadByte ();
1955 case TokenType.STRING:
1956 return new ResolvedToken () { Type = type, Str = res.ReadString () };
1957 case TokenType.TYPE:
1958 case TokenType.METHOD:
1959 case TokenType.FIELD:
1960 return new ResolvedToken () { Type = type, Id = res.ReadId () };
1961 case TokenType.UNKNOWN:
1962 return new ResolvedToken () { Type = type };
1964 throw new NotImplementedException ();
1968 internal CattrInfo[] Method_GetCustomAttributes (long id, long attr_type_id, bool inherit) {
1969 PacketReader r = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_CATTRS, new PacketWriter ().WriteId (id).WriteId (attr_type_id));
1970 return ReadCattrs (r);
1973 internal long Method_MakeGenericMethod (long id, long[] args) {
1974 PacketReader r = SendReceive (CommandSet.METHOD, (int)CmdMethod.MAKE_GENERIC_METHOD, new PacketWriter ().WriteId (id).WriteInt (args.Length).WriteIds (args));
1982 internal string Thread_GetName (long id) {
1983 return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1986 internal FrameInfo[] Thread_GetFrameInfo (long id, int start_frame, int length) {
1987 var res = SendReceive (CommandSet.THREAD, (int)CmdThread.GET_FRAME_INFO, new PacketWriter ().WriteId (id).WriteInt (start_frame).WriteInt (length));
1988 int count = res.ReadInt ();
1990 var frames = new FrameInfo [count];
1991 for (int i = 0; i < count; ++i) {
1992 var f = new FrameInfo ();
1993 f.id = res.ReadInt ();
1994 f.method = res.ReadId ();
1995 f.il_offset = res.ReadInt ();
1996 f.flags = (StackFrameFlags)res.ReadByte ();
2003 internal int Thread_GetState (long id) {
2004 return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_STATE, new PacketWriter ().WriteId (id)).ReadInt ();
2007 internal ThreadInfo Thread_GetInfo (long id) {
2008 PacketReader r = SendReceive (CommandSet.THREAD, (int)CmdThread.GET_INFO, new PacketWriter ().WriteId (id));
2010 ThreadInfo res = new ThreadInfo () { is_thread_pool = r.ReadByte () > 0 ? true : false };
2015 internal long Thread_GetId (long id) {
2016 return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_ID, new PacketWriter ().WriteId (id)).ReadLong ();
2019 internal long Thread_GetTID (long id) {
2020 return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_TID, new PacketWriter ().WriteId (id)).ReadLong ();
2023 internal void Thread_SetIP (long id, long method_id, long il_offset) {
2024 SendReceive (CommandSet.THREAD, (int)CmdThread.SET_IP, new PacketWriter ().WriteId (id).WriteId (method_id).WriteLong (il_offset));
2031 internal ModuleInfo Module_GetInfo (long id) {
2032 PacketReader r = SendReceive (CommandSet.MODULE, (int)CmdModule.GET_INFO, new PacketWriter ().WriteId (id));
2033 ModuleInfo info = new ModuleInfo { Name = r.ReadString (), ScopeName = r.ReadString (), FQName = r.ReadString (), Guid = r.ReadString (), Assembly = r.ReadId () };
2041 internal string Assembly_GetLocation (long id) {
2042 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_LOCATION, new PacketWriter ().WriteId (id)).ReadString ();
2045 internal long Assembly_GetEntryPoint (long id) {
2046 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_ENTRY_POINT, new PacketWriter ().WriteId (id)).ReadId ();
2049 internal long Assembly_GetManifestModule (long id) {
2050 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_MANIFEST_MODULE, new PacketWriter ().WriteId (id)).ReadId ();
2053 internal long Assembly_GetObject (long id) {
2054 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_OBJECT, new PacketWriter ().WriteId (id)).ReadId ();
2057 internal long Assembly_GetType (long id, string name, bool ignoreCase) {
2058 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_TYPE, new PacketWriter ().WriteId (id).WriteString (name).WriteBool (ignoreCase)).ReadId ();
2061 internal string Assembly_GetName (long id) {
2062 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_NAME, new PacketWriter ().WriteId (id)).ReadString ();
2069 internal TypeInfo Type_GetInfo (long id) {
2070 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_INFO, new PacketWriter ().WriteId (id));
2071 TypeInfo res = new TypeInfo ();
2073 res.ns = r.ReadString ();
2074 res.name = r.ReadString ();
2075 res.full_name = r.ReadString ();
2076 res.assembly = r.ReadId ();
2077 res.module = r.ReadId ();
2078 res.base_type = r.ReadId ();
2079 res.element_type = r.ReadId ();
2080 res.token = r.ReadInt ();
2081 res.rank = r.ReadByte ();
2082 res.attributes = r.ReadInt ();
2083 int b = r.ReadByte ();
2084 res.is_byref = (b & 1) != 0;
2085 res.is_pointer = (b & 2) != 0;
2086 res.is_primitive = (b & 4) != 0;
2087 res.is_valuetype = (b & 8) != 0;
2088 res.is_enum = (b & 16) != 0;
2089 res.is_gtd = (b & 32) != 0;
2090 res.is_generic_type = (b & 64) != 0;
2092 int nested_len = r.ReadInt ();
2093 res.nested = new long [nested_len];
2094 for (int i = 0; i < nested_len; ++i)
2095 res.nested [i] = r.ReadId ();
2097 if (Version.AtLeast (2, 12))
2098 res.gtd = r.ReadId ();
2099 if (Version.AtLeast (2, 15) && res.is_generic_type) {
2100 int n = r.ReadInt ();
2101 res.type_args = r.ReadIds (n);
2107 internal long[] Type_GetMethods (long id) {
2108 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_METHODS, new PacketWriter ().WriteId (id));
2110 int n = r.ReadInt ();
2111 long[] res = new long [n];
2112 for (int i = 0; i < n; ++i)
2113 res [i] = r.ReadId ();
2117 internal long[] Type_GetFields (long id, out string[] names, out long[] types, out int[] attrs) {
2118 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_FIELDS, new PacketWriter ().WriteId (id));
2120 int n = r.ReadInt ();
2121 long[] res = new long [n];
2122 names = new string [n];
2123 types = new long [n];
2124 attrs = new int [n];
2125 for (int i = 0; i < n; ++i) {
2126 res [i] = r.ReadId ();
2127 names [i] = r.ReadString ();
2128 types [i] = r.ReadId ();
2129 attrs [i] = r.ReadInt ();
2134 internal PropInfo[] Type_GetProperties (long id) {
2135 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_PROPERTIES, new PacketWriter ().WriteId (id));
2137 int n = r.ReadInt ();
2138 PropInfo[] res = new PropInfo [n];
2139 for (int i = 0; i < n; ++i) {
2140 res [i] = new PropInfo ();
2141 res [i].id = r.ReadId ();
2142 res [i].name = r.ReadString ();
2143 res [i].get_method = r.ReadId ();
2144 res [i].set_method = r.ReadId ();
2145 res [i].attrs = r.ReadInt ();
2151 internal long Type_GetObject (long id) {
2152 return SendReceive (CommandSet.TYPE, (int)CmdType.GET_OBJECT, new PacketWriter ().WriteId (id)).ReadId ();
2155 internal ValueImpl[] Type_GetValues (long id, long[] fields, long thread_id) {
2156 int len = fields.Length;
2159 r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_VALUES_2, new PacketWriter ().WriteId (id).WriteId (thread_id).WriteInt (len).WriteIds (fields));
2161 r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_VALUES, new PacketWriter ().WriteId (id).WriteInt (len).WriteIds (fields));
2163 ValueImpl[] res = new ValueImpl [len];
2164 for (int i = 0; i < len; ++i)
2165 res [i] = r.ReadValue ();
2169 internal void Type_SetValues (long id, long[] fields, ValueImpl[] values) {
2170 SendReceive (CommandSet.TYPE, (int)CmdType.SET_VALUES, new PacketWriter ().WriteId (id).WriteInt (fields.Length).WriteIds (fields).WriteValues (values));
2173 internal string[] Type_GetSourceFiles (long id, bool return_full_paths) {
2174 var r = SendReceive (CommandSet.TYPE, return_full_paths ? (int)CmdType.GET_SOURCE_FILES_2 : (int)CmdType.GET_SOURCE_FILES, new PacketWriter ().WriteId (id));
2175 int len = r.ReadInt ();
2176 string[] res = new string [len];
2177 for (int i = 0; i < len; ++i)
2178 res [i] = r.ReadString ();
2182 internal bool Type_IsAssignableFrom (long id, long c_id) {
2183 return SendReceive (CommandSet.TYPE, (int)CmdType.IS_ASSIGNABLE_FROM, new PacketWriter ().WriteId (id).WriteId (c_id)).ReadByte () > 0;
2186 internal CattrInfo[] Type_GetCustomAttributes (long id, long attr_type_id, bool inherit) {
2187 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_CATTRS, new PacketWriter ().WriteId (id).WriteId (attr_type_id));
2188 return ReadCattrs (r);
2191 internal CattrInfo[] Type_GetFieldCustomAttributes (long id, long field_id, long attr_type_id, bool inherit) {
2192 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_FIELD_CATTRS, new PacketWriter ().WriteId (id).WriteId (field_id).WriteId (attr_type_id));
2193 return ReadCattrs (r);
2196 internal CattrInfo[] Type_GetPropertyCustomAttributes (long id, long field_id, long attr_type_id, bool inherit) {
2197 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_PROPERTY_CATTRS, new PacketWriter ().WriteId (id).WriteId (field_id).WriteId (attr_type_id));
2198 return ReadCattrs (r);
2201 public long[] Type_GetMethodsByNameFlags (long id, string name, int flags, bool ignoreCase) {
2202 flags |= ignoreCase ? (int)BindingFlagsExtensions.BINDING_FLAGS_IGNORE_CASE : 0;
2203 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.CMD_TYPE_GET_METHODS_BY_NAME_FLAGS, new PacketWriter ().WriteId (id).WriteString (name).WriteInt (flags));
2204 int len = r.ReadInt ();
2205 long[] res = new long [len];
2206 for (int i = 0; i < len; ++i)
2207 res [i] = r.ReadId ();
2211 internal long[] Type_GetInterfaces (long id) {
2212 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_INTERFACES, new PacketWriter ().WriteId (id));
2213 int len = r.ReadInt ();
2214 return r.ReadIds (len);
2217 internal IfaceMapInfo[] Type_GetInterfaceMap (long id, long[] ids) {
2218 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_INTERFACE_MAP, new PacketWriter ().WriteId (id).WriteInt (ids.Length).WriteIds (ids));
2219 var res = new IfaceMapInfo [ids.Length];
2220 for (int i = 0; i < ids.Length; ++i) {
2221 int n = r.ReadInt ();
2223 res [i].iface_id = ids [i];
2224 res [i].iface_methods = r.ReadIds (n);
2225 res [i].target_methods = r.ReadIds (n);
2231 internal bool Type_IsInitialized (long id) {
2232 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.IS_INITIALIZED, new PacketWriter ().WriteId (id));
2233 return r.ReadInt () == 1;
2236 internal long Type_CreateInstance (long id) {
2237 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.CREATE_INSTANCE, new PacketWriter ().WriteId (id));
2245 internal FieldMirrorInfo Field_GetInfo (long id) {
2246 PacketReader r = SendReceive (CommandSet.FIELD, (int)CmdField.GET_INFO, new PacketWriter ().WriteId (id));
2247 FieldMirrorInfo info = new FieldMirrorInfo { Name = r.ReadString (), Parent = r.ReadId (), TypeId = r.ReadId (), Attrs = r.ReadInt () };
2255 internal int EnableEvent (EventType etype, SuspendPolicy suspend_policy, List<Modifier> mods) {
2256 var w = new PacketWriter ().WriteByte ((byte)etype).WriteByte ((byte)suspend_policy);
2258 if (mods.Count > 255)
2259 throw new NotImplementedException ();
2260 w.WriteByte ((byte)mods.Count);
2261 foreach (Modifier mod in mods) {
2262 if (mod is CountModifier) {
2263 w.WriteByte ((byte)ModifierKind.COUNT);
2264 w.WriteInt ((mod as CountModifier).Count);
2265 } else if (mod is LocationModifier) {
2266 w.WriteByte ((byte)ModifierKind.LOCATION_ONLY);
2267 w.WriteId ((mod as LocationModifier).Method);
2268 w.WriteLong ((mod as LocationModifier).Location);
2269 } else if (mod is StepModifier) {
2270 w.WriteByte ((byte)ModifierKind.STEP);
2271 w.WriteId ((mod as StepModifier).Thread);
2272 w.WriteInt ((mod as StepModifier).Size);
2273 w.WriteInt ((mod as StepModifier).Depth);
2274 if (Version.AtLeast (2, 16))
2275 w.WriteInt ((mod as StepModifier).Filter);
2276 } else if (mod is ThreadModifier) {
2277 w.WriteByte ((byte)ModifierKind.THREAD_ONLY);
2278 w.WriteId ((mod as ThreadModifier).Thread);
2279 } else if (mod is ExceptionModifier) {
2280 var em = mod as ExceptionModifier;
2281 w.WriteByte ((byte)ModifierKind.EXCEPTION_ONLY);
2282 w.WriteId (em.Type);
2283 if (Version.MajorVersion > 2 || Version.MinorVersion > 0) {
2284 /* This is only supported in protocol version 2.1 */
2285 w.WriteBool (em.Caught);
2286 w.WriteBool (em.Uncaught);
2287 } else if (!em.Caught || !em.Uncaught) {
2288 throw new NotSupportedException ("This request is not supported by the protocol version implemented by the debuggee.");
2290 if (Version.MajorVersion > 2 || Version.MinorVersion > 24) {
2291 w.WriteBool (em.Subclasses);
2292 } else if (!em.Subclasses) {
2293 throw new NotSupportedException ("This request is not supported by the protocol version implemented by the debuggee.");
2295 } else if (mod is AssemblyModifier) {
2296 w.WriteByte ((byte)ModifierKind.ASSEMBLY_ONLY);
2297 var amod = (mod as AssemblyModifier);
2298 w.WriteInt (amod.Assemblies.Length);
2299 foreach (var id in amod.Assemblies)
2301 } else if (mod is SourceFileModifier) {
2302 w.WriteByte ((byte)ModifierKind.SOURCE_FILE_ONLY);
2303 var smod = (mod as SourceFileModifier);
2304 w.WriteInt (smod.SourceFiles.Length);
2305 foreach (var s in smod.SourceFiles)
2307 } else if (mod is TypeNameModifier) {
2308 w.WriteByte ((byte)ModifierKind.TYPE_NAME_ONLY);
2309 var tmod = (mod as TypeNameModifier);
2310 w.WriteInt (tmod.TypeNames.Length);
2311 foreach (var s in tmod.TypeNames)
2314 throw new NotImplementedException ();
2320 return SendReceive (CommandSet.EVENT_REQUEST, (int)CmdEventRequest.SET, w).ReadInt ();
2323 internal void ClearEventRequest (EventType etype, int req_id) {
2324 SendReceive (CommandSet.EVENT_REQUEST, (int)CmdEventRequest.CLEAR, new PacketWriter ().WriteByte ((byte)etype).WriteInt (req_id));
2327 internal void ClearAllBreakpoints () {
2328 SendReceive (CommandSet.EVENT_REQUEST, (int)CmdEventRequest.CLEAR_ALL_BREAKPOINTS, new PacketWriter ());
2334 internal ValueImpl StackFrame_GetThis (long thread_id, long id) {
2335 PacketReader r = SendReceive (CommandSet.STACK_FRAME, (int)CmdStackFrame.GET_THIS, new PacketWriter ().WriteId (thread_id).WriteId (id));
2336 return r.ReadValue ();
2339 internal ValueImpl[] StackFrame_GetValues (long thread_id, long id, int[] pos) {
2340 /* pos < 0 -> argument at pos (-pos) - 1 */
2341 /* pos >= 0 -> local at pos */
2342 int len = pos.Length;
2343 PacketReader r = SendReceive (CommandSet.STACK_FRAME, (int)CmdStackFrame.GET_VALUES, new PacketWriter ().WriteId (thread_id).WriteId (id).WriteInt (len).WriteInts (pos));
2345 ValueImpl[] res = new ValueImpl [len];
2346 for (int i = 0; i < len; ++i)
2347 res [i] = r.ReadValue ();
2351 internal void StackFrame_SetValues (long thread_id, long id, int[] pos, ValueImpl[] values) {
2352 /* pos < 0 -> argument at pos (-pos) - 1 */
2353 /* pos >= 0 -> local at pos */
2354 int len = pos.Length;
2355 SendReceive (CommandSet.STACK_FRAME, (int)CmdStackFrame.SET_VALUES, new PacketWriter ().WriteId (thread_id).WriteId (id).WriteInt (len).WriteInts (pos).WriteValues (values));
2361 internal int[] Array_GetLength (long id, out int rank, out int[] lower_bounds) {
2362 var r = SendReceive (CommandSet.ARRAY_REF, (int)CmdArrayRef.GET_LENGTH, new PacketWriter ().WriteId (id));
2363 rank = r.ReadInt ();
2364 int[] res = new int [rank];
2365 lower_bounds = new int [rank];
2366 for (int i = 0; i < rank; ++i) {
2367 res [i] = r.ReadInt ();
2368 lower_bounds [i] = r.ReadInt ();
2373 internal ValueImpl[] Array_GetValues (long id, int index, int len) {
2374 var r = SendReceive (CommandSet.ARRAY_REF, (int)CmdArrayRef.GET_VALUES, new PacketWriter ().WriteId (id).WriteInt (index).WriteInt (len));
2375 ValueImpl[] res = new ValueImpl [len];
2376 for (int i = 0; i < len; ++i)
2377 res [i] = r.ReadValue ();
2381 internal void Array_SetValues (long id, int index, ValueImpl[] values) {
2382 SendReceive (CommandSet.ARRAY_REF, (int)CmdArrayRef.SET_VALUES, new PacketWriter ().WriteId (id).WriteInt (index).WriteInt (values.Length).WriteValues (values));
2388 internal string String_GetValue (long id) {
2389 return SendReceive (CommandSet.STRING_REF, (int)CmdStringRef.GET_VALUE, new PacketWriter ().WriteId (id)).ReadString ();
2392 internal int String_GetLength (long id) {
2393 return (int)SendReceive (CommandSet.STRING_REF, (int)CmdStringRef.GET_LENGTH, new PacketWriter ().WriteId (id)).ReadLong ();
2396 internal char[] String_GetChars (long id, int index, int length) {
2397 var r = SendReceive (CommandSet.STRING_REF, (int)CmdStringRef.GET_CHARS, new PacketWriter ().WriteId (id).WriteLong (index).WriteLong (length));
2398 var res = new char [length];
2399 for (int i = 0; i < length; ++i)
2400 res [i] = (char)r.ReadShort ();
2407 internal long Object_GetType (long id) {
2408 return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_TYPE, new PacketWriter ().WriteId (id)).ReadId ();
2411 internal long Object_GetDomain (long id) {
2412 return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_DOMAIN, new PacketWriter ().WriteId (id)).ReadId ();
2415 internal ValueImpl[] Object_GetValues (long id, long[] fields) {
2416 int len = fields.Length;
2417 PacketReader r = SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_VALUES, new PacketWriter ().WriteId (id).WriteInt (len).WriteIds (fields));
2419 ValueImpl[] res = new ValueImpl [len];
2420 for (int i = 0; i < len; ++i)
2421 res [i] = r.ReadValue ();
2425 internal void Object_SetValues (long id, long[] fields, ValueImpl[] values) {
2426 SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.SET_VALUES, new PacketWriter ().WriteId (id).WriteInt (fields.Length).WriteIds (fields).WriteValues (values));
2429 internal bool Object_IsCollected (long id) {
2430 return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.IS_COLLECTED, new PacketWriter ().WriteId (id)).ReadInt () == 1;
2433 internal long Object_GetAddress (long id) {
2434 return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_ADDRESS, new PacketWriter ().WriteId (id)).ReadLong ();
2437 internal ObjectRefInfo Object_GetInfo (long id) {
2438 ObjectRefInfo res = new ObjectRefInfo ();
2439 PacketReader r = SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_INFO, new PacketWriter ().WriteId (id));
2441 res.type_id = r.ReadId ();
2442 res.domain_id = r.ReadId ();
2446 public void ForceDisconnect ()
2449 disconnected = true;
2454 class TcpConnection : Connection
2458 internal TcpConnection (Socket socket)
2460 this.socket = socket;
2461 //socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.NoDelay, 1);
2464 internal EndPoint EndPoint {
2466 return socket.RemoteEndPoint;
2470 protected override int TransportSend (byte[] buf, int buf_offset, int len)
2472 return socket.Send (buf, buf_offset, len, SocketFlags.None);
2475 protected override int TransportReceive (byte[] buf, int buf_offset, int len)
2477 return socket.Receive (buf, buf_offset, len, SocketFlags.None);
2480 protected override void TransportSetTimeouts (int send_timeout, int receive_timeout)
2482 socket.SendTimeout = send_timeout;
2483 socket.ReceiveTimeout = receive_timeout;
2486 protected override void TransportClose ()
2492 /* This is the interface exposed by the debugger towards the debugger agent */
2493 interface IEventHandler
2495 void Events (SuspendPolicy suspend_policy, EventInfo[] events);
2497 void VMDisconnect (int req_id, long thread_id, string vm_uri);