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;
410 * Th version of the wire-protocol implemented by the library. The library
411 * and the debuggee can communicate if they implement the same major version.
412 * If they implement a different minor version, they can communicate, but some
413 * features might not be available. This allows older clients to communicate
414 * with newer runtimes, and vice versa.
416 internal const int MAJOR_VERSION = 2;
417 internal const int MINOR_VERSION = 34;
419 enum WPSuspendPolicy {
447 APPDOMAIN_CREATE = 4, // Not in JDI
448 APPDOMAIN_UNLOAD = 5, // Not in JDI
469 SOURCE_FILE_ONLY = 12,
481 SET_PROTOCOL_VERSION = 8,
484 GET_TYPES_FOR_SOURCE_FILE = 11,
487 START_BUFFERING = 14,
500 /* FIXME: Merge into GET_INFO when the major protocol version is increased */
507 enum CmdEventRequest {
510 CLEAR_ALL_BREAKPOINTS = 3
515 GET_FRIENDLY_NAME = 2,
517 GET_ENTRY_ASSEMBLY = 4,
520 CREATE_BOXED_VALUE = 7
526 GET_MANIFEST_MODULE = 3,
538 GET_DECLARING_TYPE = 2,
546 MAKE_GENERIC_METHOD = 10
555 GET_SOURCE_FILES = 6,
557 IS_ASSIGNABLE_FROM = 8,
560 GET_FIELD_CATTRS = 11,
561 GET_PROPERTY_CATTRS = 12,
562 /* FIXME: Merge into GET_SOURCE_FILES when the major protocol version is increased */
563 GET_SOURCE_FILES_2 = 13,
564 /* FIXME: Merge into GET_VALUES when the major protocol version is increased */
566 CMD_TYPE_GET_METHODS_BY_NAME_FLAGS = 15,
568 GET_INTERFACE_MAP = 17,
578 enum BindingFlagsExtensions {
579 BINDING_FLAGS_IGNORE_CASE = 0x70000000,
612 public int command_set;
617 internal static int GetPacketLength (byte[] header) {
619 return decode_int (header, ref offset);
622 internal static bool IsReplyPacket (byte[] packet) {
624 return decode_byte (packet, ref offset) == 0x80;
627 internal static int GetPacketId (byte[] packet) {
629 return decode_int (packet, ref offset);
632 static int decode_byte (byte[] packet, ref int offset) {
633 return packet [offset++];
636 static int decode_short (byte[] packet, ref int offset) {
637 int res = ((int)packet [offset] << 8) | (int)packet [offset + 1];
642 static int decode_int (byte[] packet, ref int offset) {
643 int res = ((int)packet [offset] << 24) | ((int)packet [offset + 1] << 16) | ((int)packet [offset + 2] << 8) | (int)packet [offset + 3];
648 static long decode_id (byte[] packet, ref int offset) {
649 return decode_int (packet, ref offset);
652 static long decode_long (byte[] packet, ref int offset) {
653 uint high = (uint)decode_int (packet, ref offset);
654 uint low = (uint)decode_int (packet, ref offset);
656 return (long)(((ulong)high << 32) | (ulong)low);
659 internal static SuspendPolicy decode_suspend_policy (int suspend_policy) {
660 switch ((WPSuspendPolicy)suspend_policy) {
661 case WPSuspendPolicy.NONE:
662 return SuspendPolicy.None;
663 case WPSuspendPolicy.EVENT_THREAD:
664 return SuspendPolicy.EventThread;
665 case WPSuspendPolicy.ALL:
666 return SuspendPolicy.All;
668 throw new NotImplementedException ();
672 static Header decode_command_header (byte[] packet) {
674 Header res = new Header ();
676 decode_int (packet, ref offset);
677 res.id = decode_int (packet, ref offset);
678 res.flags = decode_byte (packet, ref offset);
679 res.command_set = decode_byte (packet, ref offset);
680 res.command = decode_byte (packet, ref offset);
685 static void encode_byte (byte[] buf, int b, ref int offset) {
686 buf [offset] = (byte)b;
690 static void encode_int (byte[] buf, int i, ref int offset) {
691 buf [offset] = (byte)((i >> 24) & 0xff);
692 buf [offset + 1] = (byte)((i >> 16) & 0xff);
693 buf [offset + 2] = (byte)((i >> 8) & 0xff);
694 buf [offset + 3] = (byte)((i >> 0) & 0xff);
698 static void encode_id (byte[] buf, long id, ref int offset) {
699 encode_int (buf, (int)id, ref offset);
702 static void encode_long (byte[] buf, long l, ref int offset) {
703 encode_int (buf, (int)((l >> 32) & 0xffffffff), ref offset);
704 encode_int (buf, (int)(l & 0xffffffff), ref offset);
707 internal static byte[] EncodePacket (int id, int commandSet, int command, byte[] data, int dataLen) {
708 byte[] buf = new byte [dataLen + 11];
711 encode_int (buf, buf.Length, ref offset);
712 encode_int (buf, id, ref offset);
713 encode_byte (buf, 0, ref offset);
714 encode_byte (buf, commandSet, ref offset);
715 encode_byte (buf, command, ref offset);
717 for (int i = 0; i < dataLen; ++i)
718 buf [offset + i] = data [i];
727 public PacketReader (byte[] packet) {
728 this.packet = packet;
731 Header header = decode_command_header (packet);
732 CommandSet = (CommandSet)header.command_set;
733 Command = header.command;
737 ReadInt (); // length
739 ReadByte (); // flags
740 ErrorCode = ReadShort ();
743 public CommandSet CommandSet {
751 public int ErrorCode {
761 public int ReadByte () {
762 return decode_byte (packet, ref offset);
765 public int ReadShort () {
766 return decode_short (packet, ref offset);
769 public int ReadInt () {
770 return decode_int (packet, ref offset);
773 public long ReadId () {
774 return decode_id (packet, ref offset);
777 public long ReadLong () {
778 return decode_long (packet, ref offset);
781 public float ReadFloat () {
782 float f = DataConverter.FloatFromBE (packet, offset);
787 public double ReadDouble () {
788 double d = DataConverter.DoubleFromBE (packet, offset);
793 public string ReadString () {
794 int len = decode_int (packet, ref offset);
795 string res = new String (Encoding.UTF8.GetChars (packet, offset, len));
800 public ValueImpl ReadValue () {
801 ElementType etype = (ElementType)ReadByte ();
804 case ElementType.Void:
805 return new ValueImpl { Type = etype };
807 return new ValueImpl { Type = etype, Value = (sbyte)ReadInt () };
809 return new ValueImpl { Type = etype, Value = (byte)ReadInt () };
810 case ElementType.Boolean:
811 return new ValueImpl { Type = etype, Value = ReadInt () != 0 };
813 return new ValueImpl { Type = etype, Value = (short)ReadInt () };
815 return new ValueImpl { Type = etype, Value = (ushort)ReadInt () };
816 case ElementType.Char:
817 return new ValueImpl { Type = etype, Value = (char)ReadInt () };
819 return new ValueImpl { Type = etype, Value = ReadInt () };
821 return new ValueImpl { Type = etype, Value = (uint)ReadInt () };
823 return new ValueImpl { Type = etype, Value = ReadLong () };
825 return new ValueImpl { Type = etype, Value = (ulong)ReadLong () };
827 return new ValueImpl { Type = etype, Value = ReadFloat () };
829 return new ValueImpl { Type = etype, Value = ReadDouble () };
832 case ElementType.Ptr:
833 // FIXME: The client and the debuggee might have different word sizes
834 return new ValueImpl { Type = etype, Value = ReadLong () };
835 case ElementType.String:
836 case ElementType.SzArray:
837 case ElementType.Class:
838 case ElementType.Array:
839 case ElementType.Object:
840 long objid = ReadId ();
841 return new ValueImpl () { Type = etype, Objid = objid };
842 case ElementType.ValueType:
843 bool is_enum = ReadByte () == 1;
844 long klass = ReadId ();
845 long nfields = ReadInt ();
846 ValueImpl[] fields = new ValueImpl [nfields];
847 for (int i = 0; i < nfields; ++i)
848 fields [i] = ReadValue ();
849 return new ValueImpl () { Type = etype, Klass = klass, Fields = fields, IsEnum = is_enum };
850 case (ElementType)ValueTypeId.VALUE_TYPE_ID_NULL:
851 return new ValueImpl { Type = etype };
852 case (ElementType)ValueTypeId.VALUE_TYPE_ID_TYPE:
853 return new ValueImpl () { Type = etype, Id = ReadId () };
854 case (ElementType)ValueTypeId.VALUE_TYPE_ID_PARENT_VTYPE:
855 return new ValueImpl () { Type = etype, Index = ReadInt () };
857 throw new NotImplementedException ("Unable to handle type " + etype);
861 public long[] ReadIds (int n) {
862 long[] res = new long [n];
863 for (int i = 0; i < n; ++i)
874 public PacketWriter () {
875 data = new byte [1024];
879 void MakeRoom (int size) {
880 if (offset + size >= data.Length) {
881 int new_len = data.Length * 2;
882 while (new_len < offset + size) {
885 byte[] new_data = new byte [new_len];
886 Array.Copy (data, new_data, data.Length);
891 public PacketWriter WriteByte (byte val) {
893 encode_byte (data, val, ref offset);
897 public PacketWriter WriteInt (int val) {
899 encode_int (data, val, ref offset);
903 public PacketWriter WriteId (long id) {
905 encode_id (data, id, ref offset);
909 public PacketWriter WriteLong (long val) {
911 encode_long (data, val, ref offset);
915 public PacketWriter WriteFloat (float f) {
917 byte[] b = DataConverter.GetBytesBE (f);
918 for (int i = 0; i < 4; ++i)
919 data [offset + i] = b [i];
924 public PacketWriter WriteDouble (double d) {
926 byte[] b = DataConverter.GetBytesBE (d);
927 for (int i = 0; i < 8; ++i)
928 data [offset + i] = b [i];
933 public PacketWriter WriteInts (int[] ids) {
934 for (int i = 0; i < ids.Length; ++i)
939 public PacketWriter WriteIds (long[] ids) {
940 for (int i = 0; i < ids.Length; ++i)
945 public PacketWriter WriteString (string s) {
947 return WriteInt (-1);
949 byte[] b = Encoding.UTF8.GetBytes (s);
951 encode_int (data, b.Length, ref offset);
953 Buffer.BlockCopy (b, 0, data, offset, b.Length);
958 public PacketWriter WriteBool (bool val) {
959 WriteByte (val ? (byte)1 : (byte)0);
963 public PacketWriter WriteValue (ValueImpl v) {
967 t = TypeCodeToElementType (Type.GetTypeCode (v.Value.GetType ()));
972 case ElementType.Boolean:
973 WriteInt ((bool)v.Value ? 1 : 0);
975 case ElementType.Char:
976 WriteInt ((int)(char)v.Value);
979 WriteInt ((int)(sbyte)v.Value);
982 WriteInt ((int)(byte)v.Value);
985 WriteInt ((int)(short)v.Value);
988 WriteInt ((int)(ushort)v.Value);
991 WriteInt ((int)(int)v.Value);
994 WriteInt ((int)(uint)v.Value);
997 WriteLong ((long)(long)v.Value);
1000 WriteLong ((long)(ulong)v.Value);
1002 case ElementType.R4:
1003 WriteFloat ((float)v.Value);
1005 case ElementType.R8:
1006 WriteDouble ((double)v.Value);
1008 case ElementType.String:
1009 case ElementType.SzArray:
1010 case ElementType.Class:
1011 case ElementType.Array:
1012 case ElementType.Object:
1015 case ElementType.ValueType:
1018 throw new NotImplementedException ();
1021 WriteInt (v.Fields.Length);
1022 for (int i = 0; i < v.Fields.Length; ++i)
1023 WriteValue (v.Fields [i]);
1025 case (ElementType)ValueTypeId.VALUE_TYPE_ID_NULL:
1028 throw new NotImplementedException ();
1034 public PacketWriter WriteValues (ValueImpl[] values) {
1035 for (int i = 0; i < values.Length; ++i)
1036 WriteValue (values [i]);
1040 public byte[] Data {
1053 delegate void ReplyCallback (int packet_id, byte[] packet);
1056 Thread receiver_thread;
1057 Dictionary<int, byte[]> reply_packets;
1058 Dictionary<int, ReplyCallback> reply_cbs;
1059 Dictionary<int, int> reply_cb_counts;
1060 object reply_packets_monitor;
1062 internal event EventHandler<ErrorHandlerEventArgs> ErrorHandler;
1064 protected Connection () {
1066 reply_packets = new Dictionary<int, byte[]> ();
1067 reply_cbs = new Dictionary<int, ReplyCallback> ();
1068 reply_cb_counts = new Dictionary<int, int> ();
1069 reply_packets_monitor = new Object ();
1070 if (EnableConnectionLogging) {
1071 var path = Environment.GetEnvironmentVariable ("MONO_SDB_LOG");
1072 if (path.Contains ("{0}")) {
1073 //C:\SomeDir\sdbLog{0}.txt -> C:\SomeDir\sdbLog1.txt
1074 LoggingStream = new StreamWriter (string.Format (path, ConnectionId++), false);
1075 } else if (Path.HasExtension (path)) {
1076 //C:\SomeDir\sdbLog.txt -> C:\SomeDir\sdbLog1.txt
1077 LoggingStream = new StreamWriter (Path.GetDirectoryName (path) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension (path) + ConnectionId++ + "." + Path.GetExtension (path), false);
1079 //C:\SomeDir\sdbLog -> C:\SomeDir\sdbLog1
1080 LoggingStream = new StreamWriter (path + ConnectionId++, false);
1085 protected abstract int TransportReceive (byte[] buf, int buf_offset, int len);
1086 protected abstract int TransportSend (byte[] buf, int buf_offset, int len);
1087 protected abstract void TransportSetTimeouts (int send_timeout, int receive_timeout);
1088 protected abstract void TransportClose ();
1090 internal VersionInfo Version;
1092 int Receive (byte[] buf, int buf_offset, int len) {
1095 while (offset < len) {
1096 int n = TransportReceive (buf, buf_offset + offset, len - offset);
1106 // Do the wire protocol handshake
1107 internal void Connect () {
1108 byte[] buf = new byte [HANDSHAKE_STRING.Length];
1109 char[] cbuf = new char [buf.Length];
1111 // FIXME: Add a timeout
1112 int n = Receive (buf, 0, buf.Length);
1114 throw new IOException ("DWP Handshake failed.");
1115 for (int i = 0; i < buf.Length; ++i)
1116 cbuf [i] = (char)buf [i];
1118 if (new String (cbuf) != HANDSHAKE_STRING)
1119 throw new IOException ("DWP Handshake failed.");
1121 TransportSend (buf, 0, buf.Length);
1123 receiver_thread = new Thread (new ThreadStart (receiver_thread_main));
1124 receiver_thread.Name = "SDB Receiver";
1125 receiver_thread.IsBackground = true;
1126 receiver_thread.Start ();
1128 Version = VM_GetVersion ();
1131 // Tell the debuggee our protocol version, so newer debuggees can work
1132 // with older clients
1136 // Older debuggees might not support this request
1137 EventHandler<ErrorHandlerEventArgs> OrigErrorHandler = ErrorHandler;
1138 ErrorHandler = null;
1139 ErrorHandler += delegate (object sender, ErrorHandlerEventArgs args) {
1140 throw new NotSupportedException ();
1143 VM_SetProtocolVersion (MAJOR_VERSION, MINOR_VERSION);
1144 } catch (NotSupportedException) {
1146 ErrorHandler = OrigErrorHandler;
1149 internal byte[] ReadPacket () {
1150 // FIXME: Throw ClosedConnectionException () if the connection is closed
1151 // FIXME: Throw ClosedConnectionException () if another thread closes the connection
1153 byte[] header = new byte [HEADER_LENGTH];
1155 int len = Receive (header, 0, header.Length);
1157 return new byte [0];
1158 if (len != HEADER_LENGTH) {
1160 throw new IOException ("Packet of length " + len + " is read.");
1163 int packetLength = GetPacketLength (header);
1164 if (packetLength < 11)
1165 throw new IOException ("Invalid packet length.");
1167 if (packetLength == 11) {
1170 byte[] buf = new byte [packetLength];
1171 for (int i = 0; i < header.Length; ++i)
1172 buf [i] = header [i];
1173 len = Receive (buf, header.Length, packetLength - header.Length);
1174 if (len != packetLength - header.Length)
1175 throw new IOException ();
1180 internal void WritePacket (byte[] packet) {
1181 // FIXME: Throw ClosedConnectionException () if the connection is closed
1182 // FIXME: Throw ClosedConnectionException () if another thread closes the connection
1184 TransportSend (packet, 0, packet.Length);
1187 internal void WritePackets (List<byte[]> packets) {
1188 // FIXME: Throw ClosedConnectionException () if the connection is closed
1189 // FIXME: Throw ClosedConnectionException () if another thread closes the connection
1192 for (int i = 0; i < packets.Count; ++i)
1193 len += packets [i].Length;
1194 byte[] data = new byte [len];
1196 for (int i = 0; i < packets.Count; ++i) {
1197 Buffer.BlockCopy (packets [i], 0, data, pos, packets [i].Length);
1198 pos += packets [i].Length;
1200 TransportSend (data, 0, data.Length);
1203 internal void Close () {
1207 internal bool IsClosed {
1215 void receiver_thread_main () {
1218 bool res = ReceivePacket ();
1221 } catch (Exception ex) {
1223 Console.WriteLine (ex);
1229 lock (reply_packets_monitor) {
1230 disconnected = true;
1231 Monitor.PulseAll (reply_packets_monitor);
1234 EventHandler.VMDisconnect (0, 0, null);
1237 bool ReceivePacket () {
1238 byte[] packet = ReadPacket ();
1240 if (packet.Length == 0) {
1244 if (IsReplyPacket (packet)) {
1245 int id = GetPacketId (packet);
1246 ReplyCallback cb = null;
1247 lock (reply_packets_monitor) {
1248 reply_cbs.TryGetValue (id, out cb);
1250 reply_packets [id] = packet;
1251 Monitor.PulseAll (reply_packets_monitor);
1253 int c = reply_cb_counts [id];
1256 reply_cbs.Remove (id);
1257 reply_cb_counts.Remove (id);
1263 cb.Invoke (id, packet);
1265 PacketReader r = new PacketReader (packet);
1267 if (r.CommandSet == CommandSet.EVENT && r.Command == (int)CmdEvent.COMPOSITE) {
1268 int spolicy = r.ReadByte ();
1269 int nevents = r.ReadInt ();
1271 SuspendPolicy suspend_policy = decode_suspend_policy (spolicy);
1273 EventInfo[] events = new EventInfo [nevents];
1275 for (int i = 0; i < nevents; ++i) {
1276 EventKind kind = (EventKind)r.ReadByte ();
1277 int req_id = r.ReadInt ();
1279 EventType etype = (EventType)kind;
1281 long thread_id = r.ReadId ();
1282 if (kind == EventKind.VM_START) {
1283 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id };
1284 //EventHandler.VMStart (req_id, thread_id, null);
1285 } else if (kind == EventKind.VM_DEATH) {
1287 if (Version.AtLeast (2, 27))
1288 exit_code = r.ReadInt ();
1289 //EventHandler.VMDeath (req_id, 0, null);
1290 events [i] = new EventInfo (etype, req_id) { ExitCode = exit_code };
1291 } else if (kind == EventKind.THREAD_START) {
1292 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = thread_id };
1293 //EventHandler.ThreadStart (req_id, thread_id, thread_id);
1294 } else if (kind == EventKind.THREAD_DEATH) {
1295 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = thread_id };
1296 //EventHandler.ThreadDeath (req_id, thread_id, thread_id);
1297 } else if (kind == EventKind.ASSEMBLY_LOAD) {
1298 long id = r.ReadId ();
1299 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1300 //EventHandler.AssemblyLoad (req_id, thread_id, id);
1301 } else if (kind == EventKind.ASSEMBLY_UNLOAD) {
1302 long id = r.ReadId ();
1303 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1304 //EventHandler.AssemblyUnload (req_id, thread_id, id);
1305 } else if (kind == EventKind.TYPE_LOAD) {
1306 long id = r.ReadId ();
1307 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1308 //EventHandler.TypeLoad (req_id, thread_id, id);
1309 } else if (kind == EventKind.METHOD_ENTRY) {
1310 long id = r.ReadId ();
1311 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1312 //EventHandler.MethodEntry (req_id, thread_id, id);
1313 } else if (kind == EventKind.METHOD_EXIT) {
1314 long id = r.ReadId ();
1315 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1316 //EventHandler.MethodExit (req_id, thread_id, id);
1317 } else if (kind == EventKind.BREAKPOINT) {
1318 long id = r.ReadId ();
1319 long loc = r.ReadLong ();
1320 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1321 //EventHandler.Breakpoint (req_id, thread_id, id, loc);
1322 } else if (kind == EventKind.STEP) {
1323 long id = r.ReadId ();
1324 long loc = r.ReadLong ();
1325 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1326 //EventHandler.Step (req_id, thread_id, id, loc);
1327 } else if (kind == EventKind.EXCEPTION) {
1328 long id = r.ReadId ();
1329 long loc = 0; // FIXME
1330 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1331 //EventHandler.Exception (req_id, thread_id, id, loc);
1332 } else if (kind == EventKind.APPDOMAIN_CREATE) {
1333 long id = r.ReadId ();
1334 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1335 //EventHandler.AppDomainCreate (req_id, thread_id, id);
1336 } else if (kind == EventKind.APPDOMAIN_UNLOAD) {
1337 long id = r.ReadId ();
1338 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1339 //EventHandler.AppDomainUnload (req_id, thread_id, id);
1340 } else if (kind == EventKind.USER_BREAK) {
1343 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1344 //EventHandler.Exception (req_id, thread_id, id, loc);
1345 } else if (kind == EventKind.USER_LOG) {
1346 int level = r.ReadInt ();
1347 string category = r.ReadString ();
1348 string message = r.ReadString ();
1349 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Level = level, Category = category, Message = message };
1350 //EventHandler.Exception (req_id, thread_id, id, loc);
1351 } else if (kind == EventKind.KEEPALIVE) {
1352 events [i] = new EventInfo (etype, req_id) { };
1354 throw new NotImplementedException ("Unknown event kind: " + kind);
1358 EventHandler.Events (suspend_policy, events);
1365 internal IEventHandler EventHandler {
1369 static String CommandString (CommandSet command_set, int command)
1372 switch (command_set) {
1374 cmd = ((CmdVM)command).ToString ();
1376 case CommandSet.OBJECT_REF:
1377 cmd = ((CmdObjectRef)command).ToString ();
1379 case CommandSet.STRING_REF:
1380 cmd = ((CmdStringRef)command).ToString ();
1382 case CommandSet.THREAD:
1383 cmd = ((CmdThread)command).ToString ();
1385 case CommandSet.ARRAY_REF:
1386 cmd = ((CmdArrayRef)command).ToString ();
1388 case CommandSet.EVENT_REQUEST:
1389 cmd = ((CmdEventRequest)command).ToString ();
1391 case CommandSet.STACK_FRAME:
1392 cmd = ((CmdStackFrame)command).ToString ();
1394 case CommandSet.APPDOMAIN:
1395 cmd = ((CmdAppDomain)command).ToString ();
1397 case CommandSet.ASSEMBLY:
1398 cmd = ((CmdAssembly)command).ToString ();
1400 case CommandSet.METHOD:
1401 cmd = ((CmdMethod)command).ToString ();
1403 case CommandSet.TYPE:
1404 cmd = ((CmdType)command).ToString ();
1406 case CommandSet.MODULE:
1407 cmd = ((CmdModule)command).ToString ();
1409 case CommandSet.FIELD:
1410 cmd = ((CmdField)command).ToString ();
1412 case CommandSet.EVENT:
1413 cmd = ((CmdEvent)command).ToString ();
1416 cmd = command.ToString ();
1419 return string.Format ("[{0} {1}]", command_set, cmd);
1422 long total_protocol_ticks;
1424 void LogPacket (int packet_id, byte[] encoded_packet, byte[] reply_packet, CommandSet command_set, int command, Stopwatch watch) {
1426 total_protocol_ticks += watch.ElapsedTicks;
1427 var ts = TimeSpan.FromTicks (total_protocol_ticks);
1428 string msg = string.Format ("Packet: {0} sent: {1} received: {2} ms: {3} total ms: {4} {5}",
1429 packet_id, encoded_packet.Length, reply_packet.Length, watch.ElapsedMilliseconds,
1430 (ts.Seconds * 1000) + ts.Milliseconds,
1431 CommandString (command_set, command));
1433 LoggingStream.WriteLine (msg);
1434 LoggingStream.Flush ();
1437 bool buffer_packets;
1438 List<byte[]> buffered_packets = new List<byte[]> ();
1441 // Start buffering request/response packets on both the client and the debuggee side.
1442 // Packets sent between StartBuffering ()/StopBuffering () must be async, i.e. sent
1443 // using Send () and not SendReceive ().
1445 public void StartBuffering () {
1446 buffer_packets = true;
1447 if (Version.AtLeast (2, 34))
1448 VM_StartBuffering ();
1451 public void StopBuffering () {
1452 if (Version.AtLeast (2, 34))
1453 VM_StopBuffering ();
1454 buffer_packets = false;
1456 WritePackets (buffered_packets);
1457 if (EnableConnectionLogging) {
1458 LoggingStream.WriteLine (String.Format ("Sent {0} packets.", buffered_packets.Count));
1459 LoggingStream.Flush ();
1461 buffered_packets.Clear ();
1464 /* Send a request and call cb when a result is received */
1465 int Send (CommandSet command_set, int command, PacketWriter packet, Action<PacketReader> cb, int count) {
1466 int id = IdGenerator;
1468 Stopwatch watch = null;
1469 if (EnableConnectionLogging)
1470 watch = Stopwatch.StartNew ();
1472 byte[] encoded_packet;
1474 encoded_packet = EncodePacket (id, (int)command_set, command, null, 0);
1476 encoded_packet = EncodePacket (id, (int)command_set, command, packet.Data, packet.Offset);
1479 lock (reply_packets_monitor) {
1480 reply_cbs [id] = delegate (int packet_id, byte[] p) {
1481 if (EnableConnectionLogging)
1482 LogPacket (packet_id, encoded_packet, p, command_set, command, watch);
1483 /* Run the callback on a tp thread to avoid blocking the receive thread */
1484 PacketReader r = new PacketReader (p);
1485 cb.BeginInvoke (r, null, null);
1487 reply_cb_counts [id] = count;
1492 buffered_packets.Add (encoded_packet);
1494 WritePacket (encoded_packet);
1499 // Send a request without waiting for an answer
1500 void Send (CommandSet command_set, int command) {
1501 Send (command_set, command, null, null, 0);
1504 PacketReader SendReceive (CommandSet command_set, int command, PacketWriter packet) {
1505 int id = IdGenerator;
1506 Stopwatch watch = null;
1509 throw new VMDisconnectedException ();
1511 if (EnableConnectionLogging)
1512 watch = Stopwatch.StartNew ();
1514 byte[] encoded_packet;
1517 encoded_packet = EncodePacket (id, (int)command_set, command, null, 0);
1519 encoded_packet = EncodePacket (id, (int)command_set, command, packet.Data, packet.Offset);
1521 WritePacket (encoded_packet);
1525 /* Wait for the reply packet */
1527 lock (reply_packets_monitor) {
1528 if (reply_packets.ContainsKey (packetId)) {
1529 byte[] reply = reply_packets [packetId];
1530 reply_packets.Remove (packetId);
1531 PacketReader r = new PacketReader (reply);
1533 if (EnableConnectionLogging)
1534 LogPacket (packetId, encoded_packet, reply, command_set, command, watch);
1535 if (r.ErrorCode != 0) {
1536 if (ErrorHandler != null)
1537 ErrorHandler (this, new ErrorHandlerEventArgs () { ErrorCode = (ErrorCode)r.ErrorCode });
1538 throw new NotImplementedException ("No error handler set.");
1544 throw new VMDisconnectedException ();
1545 Monitor.Wait (reply_packets_monitor);
1551 PacketReader SendReceive (CommandSet command_set, int command) {
1552 return SendReceive (command_set, command, null);
1555 int packet_id_generator;
1559 return Interlocked.Increment (ref packet_id_generator);
1563 CattrInfo[] ReadCattrs (PacketReader r) {
1564 CattrInfo[] res = new CattrInfo [r.ReadInt ()];
1565 for (int i = 0; i < res.Length; ++i) {
1566 CattrInfo info = new CattrInfo ();
1567 info.ctor_id = r.ReadId ();
1568 info.ctor_args = new ValueImpl [r.ReadInt ()];
1569 for (int j = 0; j < info.ctor_args.Length; ++j) {
1570 info.ctor_args [j] = r.ReadValue ();
1572 info.named_args = new CattrNamedArgInfo [r.ReadInt ()];
1573 for (int j = 0; j < info.named_args.Length; ++j) {
1574 CattrNamedArgInfo arg = new CattrNamedArgInfo ();
1575 int arg_type = r.ReadByte ();
1576 arg.is_property = arg_type == 0x54;
1577 arg.id = r.ReadId ();
1578 arg.value = r.ReadValue ();
1579 info.named_args [j] = arg;
1586 static ElementType TypeCodeToElementType (TypeCode c) {
1588 case TypeCode.Boolean:
1589 return ElementType.Boolean;
1591 return ElementType.Char;
1592 case TypeCode.SByte:
1593 return ElementType.I1;
1595 return ElementType.U1;
1596 case TypeCode.Int16:
1597 return ElementType.I2;
1598 case TypeCode.UInt16:
1599 return ElementType.U2;
1600 case TypeCode.Int32:
1601 return ElementType.I4;
1602 case TypeCode.UInt32:
1603 return ElementType.U4;
1604 case TypeCode.Int64:
1605 return ElementType.I8;
1606 case TypeCode.UInt64:
1607 return ElementType.U8;
1608 case TypeCode.Single:
1609 return ElementType.R4;
1610 case TypeCode.Double:
1611 return ElementType.R8;
1613 throw new NotImplementedException ();
1618 * Implementation of debugger commands
1621 internal VersionInfo VM_GetVersion () {
1622 var res = SendReceive (CommandSet.VM, (int)CmdVM.VERSION, null);
1623 VersionInfo info = new VersionInfo ();
1624 info.VMVersion = res.ReadString ();
1625 info.MajorVersion = res.ReadInt ();
1626 info.MinorVersion = res.ReadInt ();
1630 internal void VM_SetProtocolVersion (int major, int minor) {
1631 SendReceive (CommandSet.VM, (int)CmdVM.SET_PROTOCOL_VERSION, new PacketWriter ().WriteInt (major).WriteInt (minor));
1634 internal long[] VM_GetThreads () {
1635 var res = SendReceive (CommandSet.VM, (int)CmdVM.ALL_THREADS, null);
1636 int len = res.ReadInt ();
1637 long[] arr = new long [len];
1638 for (int i = 0; i < len; ++i)
1639 arr [i] = res.ReadId ();
1643 internal void VM_Suspend () {
1644 SendReceive (CommandSet.VM, (int)CmdVM.SUSPEND);
1647 internal void VM_Resume () {
1648 SendReceive (CommandSet.VM, (int)CmdVM.RESUME);
1651 internal void VM_Exit (int exitCode) {
1652 SendReceive (CommandSet.VM, (int)CmdVM.EXIT, new PacketWriter ().WriteInt (exitCode));
1655 internal void VM_Dispose () {
1656 SendReceive (CommandSet.VM, (int)CmdVM.DISPOSE);
1659 internal ValueImpl VM_InvokeMethod (long thread, long method, ValueImpl this_arg, ValueImpl[] arguments, InvokeFlags flags, out ValueImpl exc) {
1661 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));
1662 if (r.ReadByte () == 0) {
1663 exc = r.ReadValue ();
1666 return r.ReadValue ();
1670 internal delegate void InvokeMethodCallback (ValueImpl v, ValueImpl exc, ErrorCode error, object state);
1672 internal int VM_BeginInvokeMethod (long thread, long method, ValueImpl this_arg, ValueImpl[] arguments, InvokeFlags flags, InvokeMethodCallback callback, object state) {
1673 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) {
1676 if (r.ErrorCode != 0) {
1677 callback (null, null, (ErrorCode)r.ErrorCode, state);
1679 if (r.ReadByte () == 0) {
1680 exc = r.ReadValue ();
1687 callback (v, exc, 0, state);
1692 internal int VM_BeginInvokeMethods (long thread, long[] methods, ValueImpl this_arg, List<ValueImpl[]> arguments, InvokeFlags flags, InvokeMethodCallback callback, object state) {
1693 // FIXME: Merge this with INVOKE_METHOD
1694 var w = new PacketWriter ();
1696 w.WriteInt ((int)flags);
1697 w.WriteInt (methods.Length);
1698 for (int i = 0; i < methods.Length; ++i) {
1699 w.WriteId (methods [i]);
1700 w.WriteValue (this_arg);
1701 w.WriteInt (arguments [i].Length);
1702 w.WriteValues (arguments [i]);
1704 return Send (CommandSet.VM, (int)CmdVM.INVOKE_METHODS, w, delegate (PacketReader r) {
1707 if (r.ErrorCode != 0) {
1708 callback (null, null, (ErrorCode)r.ErrorCode, state);
1710 if (r.ReadByte () == 0) {
1711 exc = r.ReadValue ();
1718 callback (v, exc, 0, state);
1723 internal void VM_AbortInvoke (long thread, int id)
1725 SendReceive (CommandSet.VM, (int)CmdVM.ABORT_INVOKE, new PacketWriter ().WriteId (thread).WriteInt (id));
1728 internal void SetSocketTimeouts (int send_timeout, int receive_timeout, int keepalive_interval)
1730 TransportSetTimeouts (send_timeout, receive_timeout);
1731 SendReceive (CommandSet.VM, (int)CmdVM.SET_KEEPALIVE, new PacketWriter ().WriteId (keepalive_interval));
1734 internal long[] VM_GetTypesForSourceFile (string fname, bool ignoreCase) {
1735 var res = SendReceive (CommandSet.VM, (int)CmdVM.GET_TYPES_FOR_SOURCE_FILE, new PacketWriter ().WriteString (fname).WriteBool (ignoreCase));
1736 int count = res.ReadInt ();
1737 long[] types = new long [count];
1738 for (int i = 0; i < count; ++i)
1739 types [i] = res.ReadId ();
1743 internal long[] VM_GetTypes (string name, bool ignoreCase) {
1744 var res = SendReceive (CommandSet.VM, (int)CmdVM.GET_TYPES, new PacketWriter ().WriteString (name).WriteBool (ignoreCase));
1745 int count = res.ReadInt ();
1746 long[] types = new long [count];
1747 for (int i = 0; i < count; ++i)
1748 types [i] = res.ReadId ();
1752 internal void VM_StartBuffering () {
1753 Send (CommandSet.VM, (int)CmdVM.START_BUFFERING);
1756 internal void VM_StopBuffering () {
1757 Send (CommandSet.VM, (int)CmdVM.STOP_BUFFERING);
1764 internal long RootDomain {
1766 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_ROOT_DOMAIN, null).ReadId ();
1770 internal string Domain_GetName (long id) {
1771 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_FRIENDLY_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1774 internal long[] Domain_GetAssemblies (long id) {
1775 var res = SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_ASSEMBLIES, new PacketWriter ().WriteId (id));
1776 int count = res.ReadInt ();
1777 long[] assemblies = new long [count];
1778 for (int i = 0; i < count; ++i)
1779 assemblies [i] = res.ReadId ();
1783 internal long Domain_GetEntryAssembly (long id) {
1784 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_ENTRY_ASSEMBLY, new PacketWriter ().WriteId (id)).ReadId ();
1787 internal long Domain_GetCorlib (long id) {
1788 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_CORLIB, new PacketWriter ().WriteId (id)).ReadId ();
1791 internal long Domain_CreateString (long id, string s) {
1792 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.CREATE_STRING, new PacketWriter ().WriteId (id).WriteString (s)).ReadId ();
1795 internal long Domain_CreateBoxedValue (long id, long type_id, ValueImpl v) {
1796 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.CREATE_BOXED_VALUE, new PacketWriter ().WriteId (id).WriteId (type_id).WriteValue (v)).ReadId ();
1803 internal string Method_GetName (long id) {
1804 return SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1807 internal long Method_GetDeclaringType (long id) {
1808 return SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_DECLARING_TYPE, new PacketWriter ().WriteId (id)).ReadId ();
1811 internal DebugInfo Method_GetDebugInfo (long id) {
1812 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_DEBUG_INFO, new PacketWriter ().WriteId (id));
1814 DebugInfo info = new DebugInfo ();
1815 info.max_il_offset = res.ReadInt ();
1817 SourceInfo[] sources = null;
1818 if (Version.AtLeast (2, 13)) {
1819 int n = res.ReadInt ();
1820 sources = new SourceInfo [n];
1821 for (int i = 0; i < n; ++i) {
1822 sources [i].source_file = res.ReadString ();
1823 if (Version.AtLeast (2, 14)) {
1824 sources [i].hash = new byte [16];
1825 for (int j = 0; j < 16; ++j)
1826 sources [i].hash [j] = (byte)res.ReadByte ();
1830 sources = new SourceInfo [1];
1831 sources [0].source_file = res.ReadString ();
1834 int n_il_offsets = res.ReadInt ();
1835 info.il_offsets = new int [n_il_offsets];
1836 info.line_numbers = new int [n_il_offsets];
1837 info.source_files = new SourceInfo [n_il_offsets];
1838 info.column_numbers = new int [n_il_offsets];
1839 info.end_line_numbers = new int [n_il_offsets];
1840 info.end_column_numbers = new int [n_il_offsets];
1841 for (int i = 0; i < n_il_offsets; ++i) {
1842 info.il_offsets [i] = res.ReadInt ();
1843 info.line_numbers [i] = res.ReadInt ();
1844 if (Version.AtLeast (2, 12)) {
1845 int idx = res.ReadInt ();
1846 info.source_files [i] = idx >= 0 ? sources [idx] : default (SourceInfo);
1848 info.source_files [i] = sources [0];
1850 if (Version.AtLeast (2, 19))
1851 info.column_numbers [i] = res.ReadInt ();
1853 info.column_numbers [i] = 0;
1854 if (Version.AtLeast (2, 32)) {
1855 info.end_line_numbers [i] = res.ReadInt ();
1856 info.end_column_numbers [i] = res.ReadInt ();
1858 info.end_column_numbers [i] = -1;
1859 info.end_column_numbers [i] = -1;
1866 internal ParamInfo Method_GetParamInfo (long id) {
1867 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_PARAM_INFO, new PacketWriter ().WriteId (id));
1869 ParamInfo info = new ParamInfo ();
1870 info.call_conv = res.ReadInt ();
1871 info.param_count = res.ReadInt ();
1872 info.generic_param_count = res.ReadInt ();
1873 info.ret_type = res.ReadId ();
1874 info.param_types = new long [info.param_count];
1875 for (int i = 0; i < info.param_count; ++i)
1876 info.param_types [i] = res.ReadId ();
1877 info.param_names = new string [info.param_count];
1878 for (int i = 0; i < info.param_count; ++i)
1879 info.param_names [i] = res.ReadString ();
1884 internal LocalsInfo Method_GetLocalsInfo (long id) {
1885 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_LOCALS_INFO, new PacketWriter ().WriteId (id));
1887 LocalsInfo info = new LocalsInfo ();
1888 int nlocals = res.ReadInt ();
1889 info.types = new long [nlocals];
1890 for (int i = 0; i < nlocals; ++i)
1891 info.types [i] = res.ReadId ();
1892 info.names = new string [nlocals];
1893 for (int i = 0; i < nlocals; ++i)
1894 info.names [i] = res.ReadString ();
1895 info.live_range_start = new int [nlocals];
1896 info.live_range_end = new int [nlocals];
1897 for (int i = 0; i < nlocals; ++i) {
1898 info.live_range_start [i] = res.ReadInt ();
1899 info.live_range_end [i] = res.ReadInt ();
1905 internal MethodInfo Method_GetInfo (long id) {
1906 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_INFO, new PacketWriter ().WriteId (id));
1908 MethodInfo info = new MethodInfo ();
1909 info.attributes = res.ReadInt ();
1910 info.iattributes = res.ReadInt ();
1911 info.token = res.ReadInt ();
1912 if (Version.AtLeast (2, 12)) {
1913 int attrs = res.ReadByte ();
1914 if ((attrs & (1 << 0)) != 0)
1916 if ((attrs & (1 << 1)) != 0)
1917 info.is_generic_method = true;
1918 info.gmd = res.ReadId ();
1919 if (Version.AtLeast (2, 15)) {
1920 if (info.is_generic_method) {
1921 int n = res.ReadInt ();
1922 info.type_args = res.ReadIds (n);
1929 internal MethodBodyInfo Method_GetBody (long id) {
1930 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_BODY, new PacketWriter ().WriteId (id));
1932 MethodBodyInfo info = new MethodBodyInfo ();
1933 info.il = new byte [res.ReadInt ()];
1934 for (int i = 0; i < info.il.Length; ++i)
1935 info.il [i] = (byte)res.ReadByte ();
1937 if (Version.AtLeast (2, 18)) {
1938 info.clauses = new ExceptionClauseInfo [res.ReadInt ()];
1940 for (int i = 0; i < info.clauses.Length; ++i) {
1941 var clause = new ExceptionClauseInfo {
1942 flags = (ExceptionClauseFlags) res.ReadInt (),
1943 try_offset = res.ReadInt (),
1944 try_length = res.ReadInt (),
1945 handler_offset = res.ReadInt (),
1946 handler_length = res.ReadInt (),
1949 if (clause.flags == ExceptionClauseFlags.None)
1950 clause.catch_type_id = res.ReadId ();
1951 else if (clause.flags == ExceptionClauseFlags.Filter)
1952 clause.filter_offset = res.ReadInt ();
1954 info.clauses [i] = clause;
1957 info.clauses = new ExceptionClauseInfo [0];
1962 internal ResolvedToken Method_ResolveToken (long id, int token) {
1963 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.RESOLVE_TOKEN, new PacketWriter ().WriteId (id).WriteInt (token));
1965 TokenType type = (TokenType)res.ReadByte ();
1967 case TokenType.STRING:
1968 return new ResolvedToken () { Type = type, Str = res.ReadString () };
1969 case TokenType.TYPE:
1970 case TokenType.METHOD:
1971 case TokenType.FIELD:
1972 return new ResolvedToken () { Type = type, Id = res.ReadId () };
1973 case TokenType.UNKNOWN:
1974 return new ResolvedToken () { Type = type };
1976 throw new NotImplementedException ();
1980 internal CattrInfo[] Method_GetCustomAttributes (long id, long attr_type_id, bool inherit) {
1981 PacketReader r = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_CATTRS, new PacketWriter ().WriteId (id).WriteId (attr_type_id));
1982 return ReadCattrs (r);
1985 internal long Method_MakeGenericMethod (long id, long[] args) {
1986 PacketReader r = SendReceive (CommandSet.METHOD, (int)CmdMethod.MAKE_GENERIC_METHOD, new PacketWriter ().WriteId (id).WriteInt (args.Length).WriteIds (args));
1994 internal string Thread_GetName (long id) {
1995 return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1998 internal void Thread_GetFrameInfo (long id, int start_frame, int length, Action<FrameInfo[]> resultCallaback) {
1999 Send (CommandSet.THREAD, (int)CmdThread.GET_FRAME_INFO, new PacketWriter ().WriteId (id).WriteInt (start_frame).WriteInt (length), (res) => {
2000 int count = res.ReadInt ();
2001 var frames = new FrameInfo[count];
2002 for (int i = 0; i < count; ++i) {
2003 var f = new FrameInfo ();
2004 f.id = res.ReadInt ();
2005 f.method = res.ReadId ();
2006 f.il_offset = res.ReadInt ();
2007 f.flags = (StackFrameFlags)res.ReadByte ();
2010 resultCallaback (frames);
2014 internal int Thread_GetState (long id) {
2015 return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_STATE, new PacketWriter ().WriteId (id)).ReadInt ();
2018 internal ThreadInfo Thread_GetInfo (long id) {
2019 PacketReader r = SendReceive (CommandSet.THREAD, (int)CmdThread.GET_INFO, new PacketWriter ().WriteId (id));
2021 ThreadInfo res = new ThreadInfo () { is_thread_pool = r.ReadByte () > 0 ? true : false };
2026 internal long Thread_GetId (long id) {
2027 return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_ID, new PacketWriter ().WriteId (id)).ReadLong ();
2030 internal long Thread_GetTID (long id) {
2031 return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_TID, new PacketWriter ().WriteId (id)).ReadLong ();
2034 internal void Thread_SetIP (long id, long method_id, long il_offset) {
2035 SendReceive (CommandSet.THREAD, (int)CmdThread.SET_IP, new PacketWriter ().WriteId (id).WriteId (method_id).WriteLong (il_offset));
2042 internal ModuleInfo Module_GetInfo (long id) {
2043 PacketReader r = SendReceive (CommandSet.MODULE, (int)CmdModule.GET_INFO, new PacketWriter ().WriteId (id));
2044 ModuleInfo info = new ModuleInfo { Name = r.ReadString (), ScopeName = r.ReadString (), FQName = r.ReadString (), Guid = r.ReadString (), Assembly = r.ReadId () };
2052 internal string Assembly_GetLocation (long id) {
2053 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_LOCATION, new PacketWriter ().WriteId (id)).ReadString ();
2056 internal long Assembly_GetEntryPoint (long id) {
2057 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_ENTRY_POINT, new PacketWriter ().WriteId (id)).ReadId ();
2060 internal long Assembly_GetManifestModule (long id) {
2061 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_MANIFEST_MODULE, new PacketWriter ().WriteId (id)).ReadId ();
2064 internal long Assembly_GetObject (long id) {
2065 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_OBJECT, new PacketWriter ().WriteId (id)).ReadId ();
2068 internal long Assembly_GetType (long id, string name, bool ignoreCase) {
2069 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_TYPE, new PacketWriter ().WriteId (id).WriteString (name).WriteBool (ignoreCase)).ReadId ();
2072 internal string Assembly_GetName (long id) {
2073 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_NAME, new PacketWriter ().WriteId (id)).ReadString ();
2080 internal TypeInfo Type_GetInfo (long id) {
2081 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_INFO, new PacketWriter ().WriteId (id));
2082 TypeInfo res = new TypeInfo ();
2084 res.ns = r.ReadString ();
2085 res.name = r.ReadString ();
2086 res.full_name = r.ReadString ();
2087 res.assembly = r.ReadId ();
2088 res.module = r.ReadId ();
2089 res.base_type = r.ReadId ();
2090 res.element_type = r.ReadId ();
2091 res.token = r.ReadInt ();
2092 res.rank = r.ReadByte ();
2093 res.attributes = r.ReadInt ();
2094 int b = r.ReadByte ();
2095 res.is_byref = (b & 1) != 0;
2096 res.is_pointer = (b & 2) != 0;
2097 res.is_primitive = (b & 4) != 0;
2098 res.is_valuetype = (b & 8) != 0;
2099 res.is_enum = (b & 16) != 0;
2100 res.is_gtd = (b & 32) != 0;
2101 res.is_generic_type = (b & 64) != 0;
2103 int nested_len = r.ReadInt ();
2104 res.nested = new long [nested_len];
2105 for (int i = 0; i < nested_len; ++i)
2106 res.nested [i] = r.ReadId ();
2108 if (Version.AtLeast (2, 12))
2109 res.gtd = r.ReadId ();
2110 if (Version.AtLeast (2, 15) && res.is_generic_type) {
2111 int n = r.ReadInt ();
2112 res.type_args = r.ReadIds (n);
2118 internal long[] Type_GetMethods (long id) {
2119 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_METHODS, new PacketWriter ().WriteId (id));
2121 int n = r.ReadInt ();
2122 long[] res = new long [n];
2123 for (int i = 0; i < n; ++i)
2124 res [i] = r.ReadId ();
2128 internal long[] Type_GetFields (long id, out string[] names, out long[] types, out int[] attrs) {
2129 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_FIELDS, new PacketWriter ().WriteId (id));
2131 int n = r.ReadInt ();
2132 long[] res = new long [n];
2133 names = new string [n];
2134 types = new long [n];
2135 attrs = new int [n];
2136 for (int i = 0; i < n; ++i) {
2137 res [i] = r.ReadId ();
2138 names [i] = r.ReadString ();
2139 types [i] = r.ReadId ();
2140 attrs [i] = r.ReadInt ();
2145 internal PropInfo[] Type_GetProperties (long id) {
2146 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_PROPERTIES, new PacketWriter ().WriteId (id));
2148 int n = r.ReadInt ();
2149 PropInfo[] res = new PropInfo [n];
2150 for (int i = 0; i < n; ++i) {
2151 res [i] = new PropInfo ();
2152 res [i].id = r.ReadId ();
2153 res [i].name = r.ReadString ();
2154 res [i].get_method = r.ReadId ();
2155 res [i].set_method = r.ReadId ();
2156 res [i].attrs = r.ReadInt ();
2162 internal long Type_GetObject (long id) {
2163 return SendReceive (CommandSet.TYPE, (int)CmdType.GET_OBJECT, new PacketWriter ().WriteId (id)).ReadId ();
2166 internal ValueImpl[] Type_GetValues (long id, long[] fields, long thread_id) {
2167 int len = fields.Length;
2170 r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_VALUES_2, new PacketWriter ().WriteId (id).WriteId (thread_id).WriteInt (len).WriteIds (fields));
2172 r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_VALUES, new PacketWriter ().WriteId (id).WriteInt (len).WriteIds (fields));
2174 ValueImpl[] res = new ValueImpl [len];
2175 for (int i = 0; i < len; ++i)
2176 res [i] = r.ReadValue ();
2180 internal void Type_SetValues (long id, long[] fields, ValueImpl[] values) {
2181 SendReceive (CommandSet.TYPE, (int)CmdType.SET_VALUES, new PacketWriter ().WriteId (id).WriteInt (fields.Length).WriteIds (fields).WriteValues (values));
2184 internal string[] Type_GetSourceFiles (long id, bool return_full_paths) {
2185 var r = SendReceive (CommandSet.TYPE, return_full_paths ? (int)CmdType.GET_SOURCE_FILES_2 : (int)CmdType.GET_SOURCE_FILES, new PacketWriter ().WriteId (id));
2186 int len = r.ReadInt ();
2187 string[] res = new string [len];
2188 for (int i = 0; i < len; ++i)
2189 res [i] = r.ReadString ();
2193 internal bool Type_IsAssignableFrom (long id, long c_id) {
2194 return SendReceive (CommandSet.TYPE, (int)CmdType.IS_ASSIGNABLE_FROM, new PacketWriter ().WriteId (id).WriteId (c_id)).ReadByte () > 0;
2197 internal CattrInfo[] Type_GetCustomAttributes (long id, long attr_type_id, bool inherit) {
2198 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_CATTRS, new PacketWriter ().WriteId (id).WriteId (attr_type_id));
2199 return ReadCattrs (r);
2202 internal CattrInfo[] Type_GetFieldCustomAttributes (long id, long field_id, long attr_type_id, bool inherit) {
2203 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_FIELD_CATTRS, new PacketWriter ().WriteId (id).WriteId (field_id).WriteId (attr_type_id));
2204 return ReadCattrs (r);
2207 internal CattrInfo[] Type_GetPropertyCustomAttributes (long id, long field_id, long attr_type_id, bool inherit) {
2208 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_PROPERTY_CATTRS, new PacketWriter ().WriteId (id).WriteId (field_id).WriteId (attr_type_id));
2209 return ReadCattrs (r);
2212 public long[] Type_GetMethodsByNameFlags (long id, string name, int flags, bool ignoreCase) {
2213 flags |= ignoreCase ? (int)BindingFlagsExtensions.BINDING_FLAGS_IGNORE_CASE : 0;
2214 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.CMD_TYPE_GET_METHODS_BY_NAME_FLAGS, new PacketWriter ().WriteId (id).WriteString (name).WriteInt (flags));
2215 int len = r.ReadInt ();
2216 long[] res = new long [len];
2217 for (int i = 0; i < len; ++i)
2218 res [i] = r.ReadId ();
2222 internal long[] Type_GetInterfaces (long id) {
2223 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_INTERFACES, new PacketWriter ().WriteId (id));
2224 int len = r.ReadInt ();
2225 return r.ReadIds (len);
2228 internal IfaceMapInfo[] Type_GetInterfaceMap (long id, long[] ids) {
2229 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_INTERFACE_MAP, new PacketWriter ().WriteId (id).WriteInt (ids.Length).WriteIds (ids));
2230 var res = new IfaceMapInfo [ids.Length];
2231 for (int i = 0; i < ids.Length; ++i) {
2232 int n = r.ReadInt ();
2234 res [i].iface_id = ids [i];
2235 res [i].iface_methods = r.ReadIds (n);
2236 res [i].target_methods = r.ReadIds (n);
2242 internal bool Type_IsInitialized (long id) {
2243 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.IS_INITIALIZED, new PacketWriter ().WriteId (id));
2244 return r.ReadInt () == 1;
2247 internal long Type_CreateInstance (long id) {
2248 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.CREATE_INSTANCE, new PacketWriter ().WriteId (id));
2256 internal FieldMirrorInfo Field_GetInfo (long id) {
2257 PacketReader r = SendReceive (CommandSet.FIELD, (int)CmdField.GET_INFO, new PacketWriter ().WriteId (id));
2258 FieldMirrorInfo info = new FieldMirrorInfo { Name = r.ReadString (), Parent = r.ReadId (), TypeId = r.ReadId (), Attrs = r.ReadInt () };
2266 internal int EnableEvent (EventType etype, SuspendPolicy suspend_policy, List<Modifier> mods) {
2267 var w = new PacketWriter ().WriteByte ((byte)etype).WriteByte ((byte)suspend_policy);
2269 if (mods.Count > 255)
2270 throw new NotImplementedException ();
2271 w.WriteByte ((byte)mods.Count);
2272 foreach (Modifier mod in mods) {
2273 if (mod is CountModifier) {
2274 w.WriteByte ((byte)ModifierKind.COUNT);
2275 w.WriteInt ((mod as CountModifier).Count);
2276 } else if (mod is LocationModifier) {
2277 w.WriteByte ((byte)ModifierKind.LOCATION_ONLY);
2278 w.WriteId ((mod as LocationModifier).Method);
2279 w.WriteLong ((mod as LocationModifier).Location);
2280 } else if (mod is StepModifier) {
2281 w.WriteByte ((byte)ModifierKind.STEP);
2282 w.WriteId ((mod as StepModifier).Thread);
2283 w.WriteInt ((mod as StepModifier).Size);
2284 w.WriteInt ((mod as StepModifier).Depth);
2285 if (Version.AtLeast (2, 16))
2286 w.WriteInt ((mod as StepModifier).Filter);
2287 } else if (mod is ThreadModifier) {
2288 w.WriteByte ((byte)ModifierKind.THREAD_ONLY);
2289 w.WriteId ((mod as ThreadModifier).Thread);
2290 } else if (mod is ExceptionModifier) {
2291 var em = mod as ExceptionModifier;
2292 w.WriteByte ((byte)ModifierKind.EXCEPTION_ONLY);
2293 w.WriteId (em.Type);
2294 if (Version.MajorVersion > 2 || Version.MinorVersion > 0) {
2295 /* This is only supported in protocol version 2.1 */
2296 w.WriteBool (em.Caught);
2297 w.WriteBool (em.Uncaught);
2298 } else if (!em.Caught || !em.Uncaught) {
2299 throw new NotSupportedException ("This request is not supported by the protocol version implemented by the debuggee.");
2301 if (Version.MajorVersion > 2 || Version.MinorVersion > 24) {
2302 w.WriteBool (em.Subclasses);
2303 } else if (!em.Subclasses) {
2304 throw new NotSupportedException ("This request is not supported by the protocol version implemented by the debuggee.");
2306 } else if (mod is AssemblyModifier) {
2307 w.WriteByte ((byte)ModifierKind.ASSEMBLY_ONLY);
2308 var amod = (mod as AssemblyModifier);
2309 w.WriteInt (amod.Assemblies.Length);
2310 foreach (var id in amod.Assemblies)
2312 } else if (mod is SourceFileModifier) {
2313 w.WriteByte ((byte)ModifierKind.SOURCE_FILE_ONLY);
2314 var smod = (mod as SourceFileModifier);
2315 w.WriteInt (smod.SourceFiles.Length);
2316 foreach (var s in smod.SourceFiles)
2318 } else if (mod is TypeNameModifier) {
2319 w.WriteByte ((byte)ModifierKind.TYPE_NAME_ONLY);
2320 var tmod = (mod as TypeNameModifier);
2321 w.WriteInt (tmod.TypeNames.Length);
2322 foreach (var s in tmod.TypeNames)
2325 throw new NotImplementedException ();
2331 return SendReceive (CommandSet.EVENT_REQUEST, (int)CmdEventRequest.SET, w).ReadInt ();
2334 internal void ClearEventRequest (EventType etype, int req_id) {
2335 SendReceive (CommandSet.EVENT_REQUEST, (int)CmdEventRequest.CLEAR, new PacketWriter ().WriteByte ((byte)etype).WriteInt (req_id));
2338 internal void ClearAllBreakpoints () {
2339 SendReceive (CommandSet.EVENT_REQUEST, (int)CmdEventRequest.CLEAR_ALL_BREAKPOINTS, new PacketWriter ());
2345 internal ValueImpl StackFrame_GetThis (long thread_id, long id) {
2346 PacketReader r = SendReceive (CommandSet.STACK_FRAME, (int)CmdStackFrame.GET_THIS, new PacketWriter ().WriteId (thread_id).WriteId (id));
2347 return r.ReadValue ();
2350 internal ValueImpl[] StackFrame_GetValues (long thread_id, long id, int[] pos) {
2351 /* pos < 0 -> argument at pos (-pos) - 1 */
2352 /* pos >= 0 -> local at pos */
2353 int len = pos.Length;
2354 PacketReader r = SendReceive (CommandSet.STACK_FRAME, (int)CmdStackFrame.GET_VALUES, new PacketWriter ().WriteId (thread_id).WriteId (id).WriteInt (len).WriteInts (pos));
2356 ValueImpl[] res = new ValueImpl [len];
2357 for (int i = 0; i < len; ++i)
2358 res [i] = r.ReadValue ();
2362 internal void StackFrame_SetValues (long thread_id, long id, int[] pos, ValueImpl[] values) {
2363 /* pos < 0 -> argument at pos (-pos) - 1 */
2364 /* pos >= 0 -> local at pos */
2365 int len = pos.Length;
2366 SendReceive (CommandSet.STACK_FRAME, (int)CmdStackFrame.SET_VALUES, new PacketWriter ().WriteId (thread_id).WriteId (id).WriteInt (len).WriteInts (pos).WriteValues (values));
2372 internal int[] Array_GetLength (long id, out int rank, out int[] lower_bounds) {
2373 var r = SendReceive (CommandSet.ARRAY_REF, (int)CmdArrayRef.GET_LENGTH, new PacketWriter ().WriteId (id));
2374 rank = r.ReadInt ();
2375 int[] res = new int [rank];
2376 lower_bounds = new int [rank];
2377 for (int i = 0; i < rank; ++i) {
2378 res [i] = r.ReadInt ();
2379 lower_bounds [i] = r.ReadInt ();
2384 internal ValueImpl[] Array_GetValues (long id, int index, int len) {
2385 var r = SendReceive (CommandSet.ARRAY_REF, (int)CmdArrayRef.GET_VALUES, new PacketWriter ().WriteId (id).WriteInt (index).WriteInt (len));
2386 ValueImpl[] res = new ValueImpl [len];
2387 for (int i = 0; i < len; ++i)
2388 res [i] = r.ReadValue ();
2392 internal void Array_SetValues (long id, int index, ValueImpl[] values) {
2393 SendReceive (CommandSet.ARRAY_REF, (int)CmdArrayRef.SET_VALUES, new PacketWriter ().WriteId (id).WriteInt (index).WriteInt (values.Length).WriteValues (values));
2399 internal string String_GetValue (long id) {
2400 return SendReceive (CommandSet.STRING_REF, (int)CmdStringRef.GET_VALUE, new PacketWriter ().WriteId (id)).ReadString ();
2403 internal int String_GetLength (long id) {
2404 return (int)SendReceive (CommandSet.STRING_REF, (int)CmdStringRef.GET_LENGTH, new PacketWriter ().WriteId (id)).ReadLong ();
2407 internal char[] String_GetChars (long id, int index, int length) {
2408 var r = SendReceive (CommandSet.STRING_REF, (int)CmdStringRef.GET_CHARS, new PacketWriter ().WriteId (id).WriteLong (index).WriteLong (length));
2409 var res = new char [length];
2410 for (int i = 0; i < length; ++i)
2411 res [i] = (char)r.ReadShort ();
2418 internal long Object_GetType (long id) {
2419 return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_TYPE, new PacketWriter ().WriteId (id)).ReadId ();
2422 internal long Object_GetDomain (long id) {
2423 return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_DOMAIN, new PacketWriter ().WriteId (id)).ReadId ();
2426 internal ValueImpl[] Object_GetValues (long id, long[] fields) {
2427 int len = fields.Length;
2428 PacketReader r = SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_VALUES, new PacketWriter ().WriteId (id).WriteInt (len).WriteIds (fields));
2430 ValueImpl[] res = new ValueImpl [len];
2431 for (int i = 0; i < len; ++i)
2432 res [i] = r.ReadValue ();
2436 internal void Object_SetValues (long id, long[] fields, ValueImpl[] values) {
2437 SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.SET_VALUES, new PacketWriter ().WriteId (id).WriteInt (fields.Length).WriteIds (fields).WriteValues (values));
2440 internal bool Object_IsCollected (long id) {
2441 return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.IS_COLLECTED, new PacketWriter ().WriteId (id)).ReadInt () == 1;
2444 internal long Object_GetAddress (long id) {
2445 return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_ADDRESS, new PacketWriter ().WriteId (id)).ReadLong ();
2448 internal ObjectRefInfo Object_GetInfo (long id) {
2449 ObjectRefInfo res = new ObjectRefInfo ();
2450 PacketReader r = SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_INFO, new PacketWriter ().WriteId (id));
2452 res.type_id = r.ReadId ();
2453 res.domain_id = r.ReadId ();
2457 public void ForceDisconnect ()
2460 disconnected = true;
2465 class TcpConnection : Connection
2469 internal TcpConnection (Socket socket)
2471 this.socket = socket;
2472 //socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.NoDelay, 1);
2475 internal EndPoint EndPoint {
2477 return socket.RemoteEndPoint;
2481 protected override int TransportSend (byte[] buf, int buf_offset, int len)
2483 return socket.Send (buf, buf_offset, len, SocketFlags.None);
2486 protected override int TransportReceive (byte[] buf, int buf_offset, int len)
2488 return socket.Receive (buf, buf_offset, len, SocketFlags.None);
2491 protected override void TransportSetTimeouts (int send_timeout, int receive_timeout)
2493 socket.SendTimeout = send_timeout;
2494 socket.ReceiveTimeout = receive_timeout;
2497 protected override void TransportClose ()
2503 /* This is the interface exposed by the debugger towards the debugger agent */
2504 interface IEventHandler
2506 void Events (SuspendPolicy suspend_policy, EventInfo[] events);
2508 void VMDisconnect (int req_id, long thread_id, string vm_uri);