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
158 DISABLE_BREAKPOINTS = 0x1,
159 SINGLE_THREADED = 0x2
205 public ElementType Type; /* or one of the VALUE_TYPE_ID constants */
208 public long Klass; // For ElementType.ValueType
209 public ValueImpl[] Fields; // for ElementType.ValueType
210 public bool IsEnum; // For ElementType.ValueType
211 public long Id; /* For VALUE_TYPE_ID_TYPE */
215 public string Name, ScopeName, FQName, Guid;
216 public long Assembly;
219 class FieldMirrorInfo {
221 public long Parent, TypeId;
234 enum StackFrameFlags {
237 NATIVE_TRANSITION = 2
240 class ResolvedToken {
241 public TokenType Type;
249 class CountModifier : Modifier {
255 class LocationModifier : Modifier {
260 public long Location {
265 class StepModifier : Modifier {
283 class ThreadModifier : Modifier {
289 class ExceptionModifier : Modifier {
296 public bool Uncaught {
299 public bool Subclasses {
304 class AssemblyModifier : Modifier {
305 public long[] Assemblies {
310 class SourceFileModifier : Modifier {
311 public string[] SourceFiles {
316 class TypeNameModifier : Modifier {
317 public string[] TypeNames {
323 public EventType EventType {
331 public SuspendPolicy SuspendPolicy {
335 public long ThreadId {
343 public long Location {
351 public string Category {
355 public string Message {
359 public int ExitCode {
363 public EventInfo (EventType type, int req_id) {
369 public enum ErrorCode {
372 INVALID_FIELDID = 25,
373 INVALID_FRAMEID = 30,
374 NOT_IMPLEMENTED = 100,
376 INVALID_ARGUMENT = 102,
378 ERR_NO_INVOCATION = 104,
379 ABSENT_INFORMATION = 105,
380 NO_SEQ_POINT_AT_IL_OFFSET = 106
383 public class ErrorHandlerEventArgs : EventArgs {
385 public ErrorCode ErrorCode {
391 * Represents the connection to the debuggee
393 public abstract class Connection
396 * The protocol and the packet format is based on JDWP, the differences
397 * are in the set of supported events, and the commands.
399 internal const string HANDSHAKE_STRING = "DWP-Handshake";
401 internal const int HEADER_LENGTH = 11;
403 static readonly bool EnableConnectionLogging = !String.IsNullOrEmpty (Environment.GetEnvironmentVariable ("MONO_SDB_LOG"));
404 static int ConnectionId;
405 readonly StreamWriter LoggingStream = EnableConnectionLogging ?
406 new StreamWriter (string.Format ("/tmp/sdb_conn_log_{0}", ConnectionId++), false) : null;
409 * Th version of the wire-protocol implemented by the library. The library
410 * and the debuggee can communicate if they implement the same major version.
411 * If they implement a different minor version, they can communicate, but some
412 * features might not be available. This allows older clients to communicate
413 * with newer runtimes, and vice versa.
415 internal const int MAJOR_VERSION = 2;
416 internal const int MINOR_VERSION = 32;
418 enum WPSuspendPolicy {
446 APPDOMAIN_CREATE = 4, // Not in JDI
447 APPDOMAIN_UNLOAD = 5, // Not in JDI
468 SOURCE_FILE_ONLY = 12,
480 SET_PROTOCOL_VERSION = 8,
483 GET_TYPES_FOR_SOURCE_FILE = 11,
497 /* FIXME: Merge into GET_INFO when the major protocol version is increased */
504 enum CmdEventRequest {
507 CLEAR_ALL_BREAKPOINTS = 3
512 GET_FRIENDLY_NAME = 2,
514 GET_ENTRY_ASSEMBLY = 4,
517 CREATE_BOXED_VALUE = 7
523 GET_MANIFEST_MODULE = 3,
535 GET_DECLARING_TYPE = 2,
543 MAKE_GENERIC_METHOD = 10
552 GET_SOURCE_FILES = 6,
554 IS_ASSIGNABLE_FROM = 8,
557 GET_FIELD_CATTRS = 11,
558 GET_PROPERTY_CATTRS = 12,
559 /* FIXME: Merge into GET_SOURCE_FILES when the major protocol version is increased */
560 GET_SOURCE_FILES_2 = 13,
561 /* FIXME: Merge into GET_VALUES when the major protocol version is increased */
563 CMD_TYPE_GET_METHODS_BY_NAME_FLAGS = 15,
565 GET_INTERFACE_MAP = 17,
575 enum BindingFlagsExtensions {
576 BINDING_FLAGS_IGNORE_CASE = 0x70000000,
609 public int command_set;
614 internal static int GetPacketLength (byte[] header) {
616 return decode_int (header, ref offset);
619 internal static bool IsReplyPacket (byte[] packet) {
621 return decode_byte (packet, ref offset) == 0x80;
624 internal static int GetPacketId (byte[] packet) {
626 return decode_int (packet, ref offset);
629 static int decode_byte (byte[] packet, ref int offset) {
630 return packet [offset++];
633 static int decode_short (byte[] packet, ref int offset) {
634 int res = ((int)packet [offset] << 8) | (int)packet [offset + 1];
639 static int decode_int (byte[] packet, ref int offset) {
640 int res = ((int)packet [offset] << 24) | ((int)packet [offset + 1] << 16) | ((int)packet [offset + 2] << 8) | (int)packet [offset + 3];
645 static long decode_id (byte[] packet, ref int offset) {
646 return decode_int (packet, ref offset);
649 static long decode_long (byte[] packet, ref int offset) {
650 uint high = (uint)decode_int (packet, ref offset);
651 uint low = (uint)decode_int (packet, ref offset);
653 return (long)(((ulong)high << 32) | (ulong)low);
656 internal static SuspendPolicy decode_suspend_policy (int suspend_policy) {
657 switch ((WPSuspendPolicy)suspend_policy) {
658 case WPSuspendPolicy.NONE:
659 return SuspendPolicy.None;
660 case WPSuspendPolicy.EVENT_THREAD:
661 return SuspendPolicy.EventThread;
662 case WPSuspendPolicy.ALL:
663 return SuspendPolicy.All;
665 throw new NotImplementedException ();
669 static Header decode_command_header (byte[] packet) {
671 Header res = new Header ();
673 decode_int (packet, ref offset);
674 res.id = decode_int (packet, ref offset);
675 res.flags = decode_byte (packet, ref offset);
676 res.command_set = decode_byte (packet, ref offset);
677 res.command = decode_byte (packet, ref offset);
682 static void encode_byte (byte[] buf, int b, ref int offset) {
683 buf [offset] = (byte)b;
687 static void encode_int (byte[] buf, int i, ref int offset) {
688 buf [offset] = (byte)((i >> 24) & 0xff);
689 buf [offset + 1] = (byte)((i >> 16) & 0xff);
690 buf [offset + 2] = (byte)((i >> 8) & 0xff);
691 buf [offset + 3] = (byte)((i >> 0) & 0xff);
695 static void encode_id (byte[] buf, long id, ref int offset) {
696 encode_int (buf, (int)id, ref offset);
699 static void encode_long (byte[] buf, long l, ref int offset) {
700 encode_int (buf, (int)((l >> 32) & 0xffffffff), ref offset);
701 encode_int (buf, (int)(l & 0xffffffff), ref offset);
704 internal static byte[] EncodePacket (int id, int commandSet, int command, byte[] data, int dataLen) {
705 byte[] buf = new byte [dataLen + 11];
708 encode_int (buf, buf.Length, ref offset);
709 encode_int (buf, id, ref offset);
710 encode_byte (buf, 0, ref offset);
711 encode_byte (buf, commandSet, ref offset);
712 encode_byte (buf, command, ref offset);
714 for (int i = 0; i < dataLen; ++i)
715 buf [offset + i] = data [i];
724 public PacketReader (byte[] packet) {
725 this.packet = packet;
728 Header header = decode_command_header (packet);
729 CommandSet = (CommandSet)header.command_set;
730 Command = header.command;
734 ReadInt (); // length
736 ReadByte (); // flags
737 ErrorCode = ReadShort ();
740 public CommandSet CommandSet {
748 public int ErrorCode {
758 public int ReadByte () {
759 return decode_byte (packet, ref offset);
762 public int ReadShort () {
763 return decode_short (packet, ref offset);
766 public int ReadInt () {
767 return decode_int (packet, ref offset);
770 public long ReadId () {
771 return decode_id (packet, ref offset);
774 public long ReadLong () {
775 return decode_long (packet, ref offset);
778 public float ReadFloat () {
779 float f = DataConverter.FloatFromBE (packet, offset);
784 public double ReadDouble () {
785 double d = DataConverter.DoubleFromBE (packet, offset);
790 public string ReadString () {
791 int len = decode_int (packet, ref offset);
792 string res = new String (Encoding.UTF8.GetChars (packet, offset, len));
797 public ValueImpl ReadValue () {
798 ElementType etype = (ElementType)ReadByte ();
801 case ElementType.Void:
802 return new ValueImpl { Type = etype };
804 return new ValueImpl { Type = etype, Value = (sbyte)ReadInt () };
806 return new ValueImpl { Type = etype, Value = (byte)ReadInt () };
807 case ElementType.Boolean:
808 return new ValueImpl { Type = etype, Value = ReadInt () != 0 };
810 return new ValueImpl { Type = etype, Value = (short)ReadInt () };
812 return new ValueImpl { Type = etype, Value = (ushort)ReadInt () };
813 case ElementType.Char:
814 return new ValueImpl { Type = etype, Value = (char)ReadInt () };
816 return new ValueImpl { Type = etype, Value = ReadInt () };
818 return new ValueImpl { Type = etype, Value = (uint)ReadInt () };
820 return new ValueImpl { Type = etype, Value = ReadLong () };
822 return new ValueImpl { Type = etype, Value = (ulong)ReadLong () };
824 return new ValueImpl { Type = etype, Value = ReadFloat () };
826 return new ValueImpl { Type = etype, Value = ReadDouble () };
829 case ElementType.Ptr:
830 // FIXME: The client and the debuggee might have different word sizes
831 return new ValueImpl { Type = etype, Value = ReadLong () };
832 case ElementType.String:
833 case ElementType.SzArray:
834 case ElementType.Class:
835 case ElementType.Array:
836 case ElementType.Object:
837 long objid = ReadId ();
838 return new ValueImpl () { Type = etype, Objid = objid };
839 case ElementType.ValueType:
840 bool is_enum = ReadByte () == 1;
841 long klass = ReadId ();
842 long nfields = ReadInt ();
843 ValueImpl[] fields = new ValueImpl [nfields];
844 for (int i = 0; i < nfields; ++i)
845 fields [i] = ReadValue ();
846 return new ValueImpl () { Type = etype, Klass = klass, Fields = fields, IsEnum = is_enum };
847 case (ElementType)ValueTypeId.VALUE_TYPE_ID_NULL:
848 return new ValueImpl { Type = etype };
849 case (ElementType)ValueTypeId.VALUE_TYPE_ID_TYPE:
850 return new ValueImpl () { Type = etype, Id = ReadId () };
852 throw new NotImplementedException ("Unable to handle type " + etype);
856 public long[] ReadIds (int n) {
857 long[] res = new long [n];
858 for (int i = 0; i < n; ++i)
869 public PacketWriter () {
870 data = new byte [1024];
874 void MakeRoom (int size) {
875 if (offset + size >= data.Length) {
876 int new_len = data.Length * 2;
877 while (new_len < offset + size) {
880 byte[] new_data = new byte [new_len];
881 Array.Copy (data, new_data, data.Length);
886 public PacketWriter WriteByte (byte val) {
888 encode_byte (data, val, ref offset);
892 public PacketWriter WriteInt (int val) {
894 encode_int (data, val, ref offset);
898 public PacketWriter WriteId (long id) {
900 encode_id (data, id, ref offset);
904 public PacketWriter WriteLong (long val) {
906 encode_long (data, val, ref offset);
910 public PacketWriter WriteFloat (float f) {
912 byte[] b = DataConverter.GetBytesBE (f);
913 for (int i = 0; i < 4; ++i)
914 data [offset + i] = b [i];
919 public PacketWriter WriteDouble (double d) {
921 byte[] b = DataConverter.GetBytesBE (d);
922 for (int i = 0; i < 8; ++i)
923 data [offset + i] = b [i];
928 public PacketWriter WriteInts (int[] ids) {
929 for (int i = 0; i < ids.Length; ++i)
934 public PacketWriter WriteIds (long[] ids) {
935 for (int i = 0; i < ids.Length; ++i)
940 public PacketWriter WriteString (string s) {
942 return WriteInt (-1);
944 byte[] b = Encoding.UTF8.GetBytes (s);
946 encode_int (data, b.Length, ref offset);
948 Buffer.BlockCopy (b, 0, data, offset, b.Length);
953 public PacketWriter WriteBool (bool val) {
954 WriteByte (val ? (byte)1 : (byte)0);
958 public PacketWriter WriteValue (ValueImpl v) {
962 t = TypeCodeToElementType (Type.GetTypeCode (v.Value.GetType ()));
967 case ElementType.Boolean:
968 WriteInt ((bool)v.Value ? 1 : 0);
970 case ElementType.Char:
971 WriteInt ((int)(char)v.Value);
974 WriteInt ((int)(sbyte)v.Value);
977 WriteInt ((int)(byte)v.Value);
980 WriteInt ((int)(short)v.Value);
983 WriteInt ((int)(ushort)v.Value);
986 WriteInt ((int)(int)v.Value);
989 WriteInt ((int)(uint)v.Value);
992 WriteLong ((long)(long)v.Value);
995 WriteLong ((long)(ulong)v.Value);
998 WriteFloat ((float)v.Value);
1000 case ElementType.R8:
1001 WriteDouble ((double)v.Value);
1003 case ElementType.String:
1004 case ElementType.SzArray:
1005 case ElementType.Class:
1006 case ElementType.Array:
1007 case ElementType.Object:
1010 case ElementType.ValueType:
1013 throw new NotImplementedException ();
1016 WriteInt (v.Fields.Length);
1017 for (int i = 0; i < v.Fields.Length; ++i)
1018 WriteValue (v.Fields [i]);
1020 case (ElementType)ValueTypeId.VALUE_TYPE_ID_NULL:
1023 throw new NotImplementedException ();
1029 public PacketWriter WriteValues (ValueImpl[] values) {
1030 for (int i = 0; i < values.Length; ++i)
1031 WriteValue (values [i]);
1035 public byte[] Data {
1048 delegate void ReplyCallback (int packet_id, byte[] packet);
1051 Thread receiver_thread;
1052 Dictionary<int, byte[]> reply_packets;
1053 Dictionary<int, ReplyCallback> reply_cbs;
1054 Dictionary<int, int> reply_cb_counts;
1055 object reply_packets_monitor;
1057 internal event EventHandler<ErrorHandlerEventArgs> ErrorHandler;
1059 protected Connection () {
1061 reply_packets = new Dictionary<int, byte[]> ();
1062 reply_cbs = new Dictionary<int, ReplyCallback> ();
1063 reply_cb_counts = new Dictionary<int, int> ();
1064 reply_packets_monitor = new Object ();
1067 protected abstract int TransportReceive (byte[] buf, int buf_offset, int len);
1068 protected abstract int TransportSend (byte[] buf, int buf_offset, int len);
1069 protected abstract void TransportSetTimeouts (int send_timeout, int receive_timeout);
1070 protected abstract void TransportClose ();
1072 internal VersionInfo Version;
1074 int Receive (byte[] buf, int buf_offset, int len) {
1077 while (offset < len) {
1078 int n = TransportReceive (buf, buf_offset + offset, len - offset);
1088 // Do the wire protocol handshake
1089 internal void Connect () {
1090 byte[] buf = new byte [HANDSHAKE_STRING.Length];
1091 char[] cbuf = new char [buf.Length];
1093 // FIXME: Add a timeout
1094 int n = Receive (buf, 0, buf.Length);
1096 throw new IOException ("DWP Handshake failed.");
1097 for (int i = 0; i < buf.Length; ++i)
1098 cbuf [i] = (char)buf [i];
1100 if (new String (cbuf) != HANDSHAKE_STRING)
1101 throw new IOException ("DWP Handshake failed.");
1103 TransportSend (buf, 0, buf.Length);
1105 receiver_thread = new Thread (new ThreadStart (receiver_thread_main));
1106 receiver_thread.Name = "SDB Receiver";
1107 receiver_thread.IsBackground = true;
1108 receiver_thread.Start ();
1110 Version = VM_GetVersion ();
1113 // Tell the debuggee our protocol version, so newer debuggees can work
1114 // with older clients
1118 // Older debuggees might not support this request
1119 EventHandler<ErrorHandlerEventArgs> OrigErrorHandler = ErrorHandler;
1120 ErrorHandler = null;
1121 ErrorHandler += delegate (object sender, ErrorHandlerEventArgs args) {
1122 throw new NotSupportedException ();
1125 VM_SetProtocolVersion (MAJOR_VERSION, MINOR_VERSION);
1126 } catch (NotSupportedException) {
1128 ErrorHandler = OrigErrorHandler;
1131 internal byte[] ReadPacket () {
1132 // FIXME: Throw ClosedConnectionException () if the connection is closed
1133 // FIXME: Throw ClosedConnectionException () if another thread closes the connection
1135 byte[] header = new byte [HEADER_LENGTH];
1137 int len = Receive (header, 0, header.Length);
1139 return new byte [0];
1140 if (len != HEADER_LENGTH) {
1142 throw new IOException ("Packet of length " + len + " is read.");
1145 int packetLength = GetPacketLength (header);
1146 if (packetLength < 11)
1147 throw new IOException ("Invalid packet length.");
1149 if (packetLength == 11) {
1152 byte[] buf = new byte [packetLength];
1153 for (int i = 0; i < header.Length; ++i)
1154 buf [i] = header [i];
1155 len = Receive (buf, header.Length, packetLength - header.Length);
1156 if (len != packetLength - header.Length)
1157 throw new IOException ();
1162 internal void WritePacket (byte[] packet) {
1163 // FIXME: Throw ClosedConnectionException () if the connection is closed
1164 // FIXME: Throw ClosedConnectionException () if another thread closes the connection
1166 TransportSend (packet, 0, packet.Length);
1169 internal void Close () {
1173 internal bool IsClosed {
1181 void receiver_thread_main () {
1184 bool res = ReceivePacket ();
1187 } catch (Exception ex) {
1189 Console.WriteLine (ex);
1195 lock (reply_packets_monitor) {
1196 disconnected = true;
1197 Monitor.PulseAll (reply_packets_monitor);
1200 EventHandler.VMDisconnect (0, 0, null);
1203 bool ReceivePacket () {
1204 byte[] packet = ReadPacket ();
1206 if (packet.Length == 0) {
1210 if (IsReplyPacket (packet)) {
1211 int id = GetPacketId (packet);
1212 ReplyCallback cb = null;
1213 lock (reply_packets_monitor) {
1214 reply_cbs.TryGetValue (id, out cb);
1216 reply_packets [id] = packet;
1217 Monitor.PulseAll (reply_packets_monitor);
1219 int c = reply_cb_counts [id];
1222 reply_cbs.Remove (id);
1223 reply_cb_counts.Remove (id);
1229 cb.Invoke (id, packet);
1231 PacketReader r = new PacketReader (packet);
1233 if (r.CommandSet == CommandSet.EVENT && r.Command == (int)CmdEvent.COMPOSITE) {
1234 int spolicy = r.ReadByte ();
1235 int nevents = r.ReadInt ();
1237 SuspendPolicy suspend_policy = decode_suspend_policy (spolicy);
1239 EventInfo[] events = new EventInfo [nevents];
1241 for (int i = 0; i < nevents; ++i) {
1242 EventKind kind = (EventKind)r.ReadByte ();
1243 int req_id = r.ReadInt ();
1245 EventType etype = (EventType)kind;
1247 long thread_id = r.ReadId ();
1248 if (kind == EventKind.VM_START) {
1249 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id };
1250 //EventHandler.VMStart (req_id, thread_id, null);
1251 } else if (kind == EventKind.VM_DEATH) {
1253 if (Version.AtLeast (2, 27))
1254 exit_code = r.ReadInt ();
1255 //EventHandler.VMDeath (req_id, 0, null);
1256 events [i] = new EventInfo (etype, req_id) { ExitCode = exit_code };
1257 } else if (kind == EventKind.THREAD_START) {
1258 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = thread_id };
1259 //EventHandler.ThreadStart (req_id, thread_id, thread_id);
1260 } else if (kind == EventKind.THREAD_DEATH) {
1261 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = thread_id };
1262 //EventHandler.ThreadDeath (req_id, thread_id, thread_id);
1263 } else if (kind == EventKind.ASSEMBLY_LOAD) {
1264 long id = r.ReadId ();
1265 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1266 //EventHandler.AssemblyLoad (req_id, thread_id, id);
1267 } else if (kind == EventKind.ASSEMBLY_UNLOAD) {
1268 long id = r.ReadId ();
1269 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1270 //EventHandler.AssemblyUnload (req_id, thread_id, id);
1271 } else if (kind == EventKind.TYPE_LOAD) {
1272 long id = r.ReadId ();
1273 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1274 //EventHandler.TypeLoad (req_id, thread_id, id);
1275 } else if (kind == EventKind.METHOD_ENTRY) {
1276 long id = r.ReadId ();
1277 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1278 //EventHandler.MethodEntry (req_id, thread_id, id);
1279 } else if (kind == EventKind.METHOD_EXIT) {
1280 long id = r.ReadId ();
1281 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1282 //EventHandler.MethodExit (req_id, thread_id, id);
1283 } else if (kind == EventKind.BREAKPOINT) {
1284 long id = r.ReadId ();
1285 long loc = r.ReadLong ();
1286 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1287 //EventHandler.Breakpoint (req_id, thread_id, id, loc);
1288 } else if (kind == EventKind.STEP) {
1289 long id = r.ReadId ();
1290 long loc = r.ReadLong ();
1291 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1292 //EventHandler.Step (req_id, thread_id, id, loc);
1293 } else if (kind == EventKind.EXCEPTION) {
1294 long id = r.ReadId ();
1295 long loc = 0; // FIXME
1296 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1297 //EventHandler.Exception (req_id, thread_id, id, loc);
1298 } else if (kind == EventKind.APPDOMAIN_CREATE) {
1299 long id = r.ReadId ();
1300 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1301 //EventHandler.AppDomainCreate (req_id, thread_id, id);
1302 } else if (kind == EventKind.APPDOMAIN_UNLOAD) {
1303 long id = r.ReadId ();
1304 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1305 //EventHandler.AppDomainUnload (req_id, thread_id, id);
1306 } else if (kind == EventKind.USER_BREAK) {
1309 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1310 //EventHandler.Exception (req_id, thread_id, id, loc);
1311 } else if (kind == EventKind.USER_LOG) {
1312 int level = r.ReadInt ();
1313 string category = r.ReadString ();
1314 string message = r.ReadString ();
1315 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Level = level, Category = category, Message = message };
1316 //EventHandler.Exception (req_id, thread_id, id, loc);
1317 } else if (kind == EventKind.KEEPALIVE) {
1318 events [i] = new EventInfo (etype, req_id) { };
1320 throw new NotImplementedException ("Unknown event kind: " + kind);
1324 EventHandler.Events (suspend_policy, events);
1331 internal IEventHandler EventHandler {
1335 static String CommandString (CommandSet command_set, int command)
1338 switch (command_set) {
1340 cmd = ((CmdVM)command).ToString ();
1342 case CommandSet.OBJECT_REF:
1343 cmd = ((CmdObjectRef)command).ToString ();
1345 case CommandSet.STRING_REF:
1346 cmd = ((CmdStringRef)command).ToString ();
1348 case CommandSet.THREAD:
1349 cmd = ((CmdThread)command).ToString ();
1351 case CommandSet.ARRAY_REF:
1352 cmd = ((CmdArrayRef)command).ToString ();
1354 case CommandSet.EVENT_REQUEST:
1355 cmd = ((CmdEventRequest)command).ToString ();
1357 case CommandSet.STACK_FRAME:
1358 cmd = ((CmdStackFrame)command).ToString ();
1360 case CommandSet.APPDOMAIN:
1361 cmd = ((CmdAppDomain)command).ToString ();
1363 case CommandSet.ASSEMBLY:
1364 cmd = ((CmdAssembly)command).ToString ();
1366 case CommandSet.METHOD:
1367 cmd = ((CmdMethod)command).ToString ();
1369 case CommandSet.TYPE:
1370 cmd = ((CmdType)command).ToString ();
1372 case CommandSet.MODULE:
1373 cmd = ((CmdModule)command).ToString ();
1375 case CommandSet.FIELD:
1376 cmd = ((CmdField)command).ToString ();
1378 case CommandSet.EVENT:
1379 cmd = ((CmdEvent)command).ToString ();
1382 cmd = command.ToString ();
1385 return string.Format ("[{0} {1}]", command_set, cmd);
1388 long total_protocol_ticks;
1390 void LogPacket (int packet_id, byte[] encoded_packet, byte[] reply_packet, CommandSet command_set, int command, Stopwatch watch) {
1392 total_protocol_ticks += watch.ElapsedTicks;
1393 var ts = TimeSpan.FromTicks (total_protocol_ticks);
1394 string msg = string.Format ("Packet: {0} sent: {1} received: {2} ms: {3} total ms: {4} {5}",
1395 packet_id, encoded_packet.Length, reply_packet.Length, watch.ElapsedMilliseconds,
1396 (ts.Seconds * 1000) + ts.Milliseconds,
1397 CommandString (command_set, command));
1399 LoggingStream.WriteLine (msg);
1400 LoggingStream.Flush ();
1403 /* Send a request and call cb when a result is received */
1404 int Send (CommandSet command_set, int command, PacketWriter packet, Action<PacketReader> cb, int count) {
1405 int id = IdGenerator;
1407 Stopwatch watch = null;
1408 if (EnableConnectionLogging)
1409 watch = Stopwatch.StartNew ();
1411 byte[] encoded_packet;
1413 encoded_packet = EncodePacket (id, (int)command_set, command, null, 0);
1415 encoded_packet = EncodePacket (id, (int)command_set, command, packet.Data, packet.Offset);
1417 lock (reply_packets_monitor) {
1418 reply_cbs [id] = delegate (int packet_id, byte[] p) {
1419 if (EnableConnectionLogging)
1420 LogPacket (packet_id, encoded_packet, p, command_set, command, watch);
1421 /* Run the callback on a tp thread to avoid blocking the receive thread */
1422 PacketReader r = new PacketReader (p);
1423 cb.BeginInvoke (r, null, null);
1425 reply_cb_counts [id] = count;
1428 WritePacket (encoded_packet);
1433 PacketReader SendReceive (CommandSet command_set, int command, PacketWriter packet) {
1434 int id = IdGenerator;
1435 Stopwatch watch = null;
1438 throw new VMDisconnectedException ();
1440 if (EnableConnectionLogging)
1441 watch = Stopwatch.StartNew ();
1443 byte[] encoded_packet;
1446 encoded_packet = EncodePacket (id, (int)command_set, command, null, 0);
1448 encoded_packet = EncodePacket (id, (int)command_set, command, packet.Data, packet.Offset);
1450 WritePacket (encoded_packet);
1454 /* Wait for the reply packet */
1456 lock (reply_packets_monitor) {
1457 if (reply_packets.ContainsKey (packetId)) {
1458 byte[] reply = reply_packets [packetId];
1459 reply_packets.Remove (packetId);
1460 PacketReader r = new PacketReader (reply);
1462 if (EnableConnectionLogging)
1463 LogPacket (packetId, encoded_packet, reply, command_set, command, watch);
1464 if (r.ErrorCode != 0) {
1465 if (ErrorHandler != null)
1466 ErrorHandler (this, new ErrorHandlerEventArgs () { ErrorCode = (ErrorCode)r.ErrorCode });
1467 throw new NotImplementedException ("No error handler set.");
1473 throw new VMDisconnectedException ();
1474 Monitor.Wait (reply_packets_monitor);
1480 PacketReader SendReceive (CommandSet command_set, int command) {
1481 return SendReceive (command_set, command, null);
1484 int packet_id_generator;
1488 return Interlocked.Increment (ref packet_id_generator);
1492 CattrInfo[] ReadCattrs (PacketReader r) {
1493 CattrInfo[] res = new CattrInfo [r.ReadInt ()];
1494 for (int i = 0; i < res.Length; ++i) {
1495 CattrInfo info = new CattrInfo ();
1496 info.ctor_id = r.ReadId ();
1497 info.ctor_args = new ValueImpl [r.ReadInt ()];
1498 for (int j = 0; j < info.ctor_args.Length; ++j) {
1499 info.ctor_args [j] = r.ReadValue ();
1501 info.named_args = new CattrNamedArgInfo [r.ReadInt ()];
1502 for (int j = 0; j < info.named_args.Length; ++j) {
1503 CattrNamedArgInfo arg = new CattrNamedArgInfo ();
1504 int arg_type = r.ReadByte ();
1505 arg.is_property = arg_type == 0x54;
1506 arg.id = r.ReadId ();
1507 arg.value = r.ReadValue ();
1508 info.named_args [j] = arg;
1515 static ElementType TypeCodeToElementType (TypeCode c) {
1517 case TypeCode.Boolean:
1518 return ElementType.Boolean;
1520 return ElementType.Char;
1521 case TypeCode.SByte:
1522 return ElementType.I1;
1524 return ElementType.U1;
1525 case TypeCode.Int16:
1526 return ElementType.I2;
1527 case TypeCode.UInt16:
1528 return ElementType.U2;
1529 case TypeCode.Int32:
1530 return ElementType.I4;
1531 case TypeCode.UInt32:
1532 return ElementType.U4;
1533 case TypeCode.Int64:
1534 return ElementType.I8;
1535 case TypeCode.UInt64:
1536 return ElementType.U8;
1537 case TypeCode.Single:
1538 return ElementType.R4;
1539 case TypeCode.Double:
1540 return ElementType.R8;
1542 throw new NotImplementedException ();
1547 * Implementation of debugger commands
1550 internal VersionInfo VM_GetVersion () {
1551 var res = SendReceive (CommandSet.VM, (int)CmdVM.VERSION, null);
1552 VersionInfo info = new VersionInfo ();
1553 info.VMVersion = res.ReadString ();
1554 info.MajorVersion = res.ReadInt ();
1555 info.MinorVersion = res.ReadInt ();
1559 internal void VM_SetProtocolVersion (int major, int minor) {
1560 SendReceive (CommandSet.VM, (int)CmdVM.SET_PROTOCOL_VERSION, new PacketWriter ().WriteInt (major).WriteInt (minor));
1563 internal long[] VM_GetThreads () {
1564 var res = SendReceive (CommandSet.VM, (int)CmdVM.ALL_THREADS, null);
1565 int len = res.ReadInt ();
1566 long[] arr = new long [len];
1567 for (int i = 0; i < len; ++i)
1568 arr [i] = res.ReadId ();
1572 internal void VM_Suspend () {
1573 SendReceive (CommandSet.VM, (int)CmdVM.SUSPEND);
1576 internal void VM_Resume () {
1577 SendReceive (CommandSet.VM, (int)CmdVM.RESUME);
1580 internal void VM_Exit (int exitCode) {
1581 SendReceive (CommandSet.VM, (int)CmdVM.EXIT, new PacketWriter ().WriteInt (exitCode));
1584 internal void VM_Dispose () {
1585 SendReceive (CommandSet.VM, (int)CmdVM.DISPOSE);
1588 internal ValueImpl VM_InvokeMethod (long thread, long method, ValueImpl this_arg, ValueImpl[] arguments, InvokeFlags flags, out ValueImpl exc) {
1590 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));
1591 if (r.ReadByte () == 0) {
1592 exc = r.ReadValue ();
1595 return r.ReadValue ();
1599 internal delegate void InvokeMethodCallback (ValueImpl v, ValueImpl exc, ErrorCode error, object state);
1601 internal int VM_BeginInvokeMethod (long thread, long method, ValueImpl this_arg, ValueImpl[] arguments, InvokeFlags flags, InvokeMethodCallback callback, object state) {
1602 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) {
1605 if (r.ErrorCode != 0) {
1606 callback (null, null, (ErrorCode)r.ErrorCode, state);
1608 if (r.ReadByte () == 0) {
1609 exc = r.ReadValue ();
1616 callback (v, exc, 0, state);
1621 internal int VM_BeginInvokeMethods (long thread, long[] methods, ValueImpl this_arg, List<ValueImpl[]> arguments, InvokeFlags flags, InvokeMethodCallback callback, object state) {
1622 // FIXME: Merge this with INVOKE_METHOD
1623 var w = new PacketWriter ();
1625 w.WriteInt ((int)flags);
1626 w.WriteInt (methods.Length);
1627 for (int i = 0; i < methods.Length; ++i) {
1628 w.WriteId (methods [i]);
1629 w.WriteValue (this_arg);
1630 w.WriteInt (arguments [i].Length);
1631 w.WriteValues (arguments [i]);
1633 return Send (CommandSet.VM, (int)CmdVM.INVOKE_METHODS, w, delegate (PacketReader r) {
1636 if (r.ErrorCode != 0) {
1637 callback (null, null, (ErrorCode)r.ErrorCode, state);
1639 if (r.ReadByte () == 0) {
1640 exc = r.ReadValue ();
1647 callback (v, exc, 0, state);
1652 internal void VM_AbortInvoke (long thread, int id)
1654 SendReceive (CommandSet.VM, (int)CmdVM.ABORT_INVOKE, new PacketWriter ().WriteId (thread).WriteInt (id));
1657 internal void SetSocketTimeouts (int send_timeout, int receive_timeout, int keepalive_interval)
1659 TransportSetTimeouts (send_timeout, receive_timeout);
1660 SendReceive (CommandSet.VM, (int)CmdVM.SET_KEEPALIVE, new PacketWriter ().WriteId (keepalive_interval));
1663 internal long[] VM_GetTypesForSourceFile (string fname, bool ignoreCase) {
1664 var res = SendReceive (CommandSet.VM, (int)CmdVM.GET_TYPES_FOR_SOURCE_FILE, new PacketWriter ().WriteString (fname).WriteBool (ignoreCase));
1665 int count = res.ReadInt ();
1666 long[] types = new long [count];
1667 for (int i = 0; i < count; ++i)
1668 types [i] = res.ReadId ();
1672 internal long[] VM_GetTypes (string name, bool ignoreCase) {
1673 var res = SendReceive (CommandSet.VM, (int)CmdVM.GET_TYPES, new PacketWriter ().WriteString (name).WriteBool (ignoreCase));
1674 int count = res.ReadInt ();
1675 long[] types = new long [count];
1676 for (int i = 0; i < count; ++i)
1677 types [i] = res.ReadId ();
1685 internal long RootDomain {
1687 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_ROOT_DOMAIN, null).ReadId ();
1691 internal string Domain_GetName (long id) {
1692 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_FRIENDLY_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1695 internal long[] Domain_GetAssemblies (long id) {
1696 var res = SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_ASSEMBLIES, new PacketWriter ().WriteId (id));
1697 int count = res.ReadInt ();
1698 long[] assemblies = new long [count];
1699 for (int i = 0; i < count; ++i)
1700 assemblies [i] = res.ReadId ();
1704 internal long Domain_GetEntryAssembly (long id) {
1705 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_ENTRY_ASSEMBLY, new PacketWriter ().WriteId (id)).ReadId ();
1708 internal long Domain_GetCorlib (long id) {
1709 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_CORLIB, new PacketWriter ().WriteId (id)).ReadId ();
1712 internal long Domain_CreateString (long id, string s) {
1713 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.CREATE_STRING, new PacketWriter ().WriteId (id).WriteString (s)).ReadId ();
1716 internal long Domain_CreateBoxedValue (long id, long type_id, ValueImpl v) {
1717 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.CREATE_BOXED_VALUE, new PacketWriter ().WriteId (id).WriteId (type_id).WriteValue (v)).ReadId ();
1724 internal string Method_GetName (long id) {
1725 return SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1728 internal long Method_GetDeclaringType (long id) {
1729 return SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_DECLARING_TYPE, new PacketWriter ().WriteId (id)).ReadId ();
1732 internal DebugInfo Method_GetDebugInfo (long id) {
1733 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_DEBUG_INFO, new PacketWriter ().WriteId (id));
1735 DebugInfo info = new DebugInfo ();
1736 info.max_il_offset = res.ReadInt ();
1738 SourceInfo[] sources = null;
1739 if (Version.AtLeast (2, 13)) {
1740 int n = res.ReadInt ();
1741 sources = new SourceInfo [n];
1742 for (int i = 0; i < n; ++i) {
1743 sources [i].source_file = res.ReadString ();
1744 if (Version.AtLeast (2, 14)) {
1745 sources [i].hash = new byte [16];
1746 for (int j = 0; j < 16; ++j)
1747 sources [i].hash [j] = (byte)res.ReadByte ();
1751 sources = new SourceInfo [1];
1752 sources [0].source_file = res.ReadString ();
1755 int n_il_offsets = res.ReadInt ();
1756 info.il_offsets = new int [n_il_offsets];
1757 info.line_numbers = new int [n_il_offsets];
1758 info.source_files = new SourceInfo [n_il_offsets];
1759 info.column_numbers = new int [n_il_offsets];
1760 info.end_line_numbers = new int [n_il_offsets];
1761 info.end_column_numbers = new int [n_il_offsets];
1762 for (int i = 0; i < n_il_offsets; ++i) {
1763 info.il_offsets [i] = res.ReadInt ();
1764 info.line_numbers [i] = res.ReadInt ();
1765 if (Version.AtLeast (2, 12)) {
1766 int idx = res.ReadInt ();
1767 info.source_files [i] = idx >= 0 ? sources [idx] : default (SourceInfo);
1769 info.source_files [i] = sources [0];
1771 if (Version.AtLeast (2, 19))
1772 info.column_numbers [i] = res.ReadInt ();
1774 info.column_numbers [i] = 0;
1775 if (Version.AtLeast (2, 32)) {
1776 info.end_line_numbers [i] = res.ReadInt ();
1777 info.end_column_numbers [i] = res.ReadInt ();
1779 info.end_column_numbers [i] = -1;
1780 info.end_column_numbers [i] = -1;
1787 internal ParamInfo Method_GetParamInfo (long id) {
1788 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_PARAM_INFO, new PacketWriter ().WriteId (id));
1790 ParamInfo info = new ParamInfo ();
1791 info.call_conv = res.ReadInt ();
1792 info.param_count = res.ReadInt ();
1793 info.generic_param_count = res.ReadInt ();
1794 info.ret_type = res.ReadId ();
1795 info.param_types = new long [info.param_count];
1796 for (int i = 0; i < info.param_count; ++i)
1797 info.param_types [i] = res.ReadId ();
1798 info.param_names = new string [info.param_count];
1799 for (int i = 0; i < info.param_count; ++i)
1800 info.param_names [i] = res.ReadString ();
1805 internal LocalsInfo Method_GetLocalsInfo (long id) {
1806 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_LOCALS_INFO, new PacketWriter ().WriteId (id));
1808 LocalsInfo info = new LocalsInfo ();
1809 int nlocals = res.ReadInt ();
1810 info.types = new long [nlocals];
1811 for (int i = 0; i < nlocals; ++i)
1812 info.types [i] = res.ReadId ();
1813 info.names = new string [nlocals];
1814 for (int i = 0; i < nlocals; ++i)
1815 info.names [i] = res.ReadString ();
1816 info.live_range_start = new int [nlocals];
1817 info.live_range_end = new int [nlocals];
1818 for (int i = 0; i < nlocals; ++i) {
1819 info.live_range_start [i] = res.ReadInt ();
1820 info.live_range_end [i] = res.ReadInt ();
1826 internal MethodInfo Method_GetInfo (long id) {
1827 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_INFO, new PacketWriter ().WriteId (id));
1829 MethodInfo info = new MethodInfo ();
1830 info.attributes = res.ReadInt ();
1831 info.iattributes = res.ReadInt ();
1832 info.token = res.ReadInt ();
1833 if (Version.AtLeast (2, 12)) {
1834 int attrs = res.ReadByte ();
1835 if ((attrs & (1 << 0)) != 0)
1837 if ((attrs & (1 << 1)) != 0)
1838 info.is_generic_method = true;
1839 info.gmd = res.ReadId ();
1840 if (Version.AtLeast (2, 15)) {
1841 if (info.is_generic_method) {
1842 int n = res.ReadInt ();
1843 info.type_args = res.ReadIds (n);
1850 internal MethodBodyInfo Method_GetBody (long id) {
1851 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_BODY, new PacketWriter ().WriteId (id));
1853 MethodBodyInfo info = new MethodBodyInfo ();
1854 info.il = new byte [res.ReadInt ()];
1855 for (int i = 0; i < info.il.Length; ++i)
1856 info.il [i] = (byte)res.ReadByte ();
1858 if (Version.AtLeast (2, 18)) {
1859 info.clauses = new ExceptionClauseInfo [res.ReadInt ()];
1861 for (int i = 0; i < info.clauses.Length; ++i) {
1862 var clause = new ExceptionClauseInfo {
1863 flags = (ExceptionClauseFlags) res.ReadInt (),
1864 try_offset = res.ReadInt (),
1865 try_length = res.ReadInt (),
1866 handler_offset = res.ReadInt (),
1867 handler_length = res.ReadInt (),
1870 if (clause.flags == ExceptionClauseFlags.None)
1871 clause.catch_type_id = res.ReadId ();
1872 else if (clause.flags == ExceptionClauseFlags.Filter)
1873 clause.filter_offset = res.ReadInt ();
1875 info.clauses [i] = clause;
1878 info.clauses = new ExceptionClauseInfo [0];
1883 internal ResolvedToken Method_ResolveToken (long id, int token) {
1884 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.RESOLVE_TOKEN, new PacketWriter ().WriteId (id).WriteInt (token));
1886 TokenType type = (TokenType)res.ReadByte ();
1888 case TokenType.STRING:
1889 return new ResolvedToken () { Type = type, Str = res.ReadString () };
1890 case TokenType.TYPE:
1891 case TokenType.METHOD:
1892 case TokenType.FIELD:
1893 return new ResolvedToken () { Type = type, Id = res.ReadId () };
1894 case TokenType.UNKNOWN:
1895 return new ResolvedToken () { Type = type };
1897 throw new NotImplementedException ();
1901 internal CattrInfo[] Method_GetCustomAttributes (long id, long attr_type_id, bool inherit) {
1902 PacketReader r = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_CATTRS, new PacketWriter ().WriteId (id).WriteId (attr_type_id));
1903 return ReadCattrs (r);
1906 internal long Method_MakeGenericMethod (long id, long[] args) {
1907 PacketReader r = SendReceive (CommandSet.METHOD, (int)CmdMethod.MAKE_GENERIC_METHOD, new PacketWriter ().WriteId (id).WriteInt (args.Length).WriteIds (args));
1915 internal string Thread_GetName (long id) {
1916 return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1919 internal FrameInfo[] Thread_GetFrameInfo (long id, int start_frame, int length) {
1920 var res = SendReceive (CommandSet.THREAD, (int)CmdThread.GET_FRAME_INFO, new PacketWriter ().WriteId (id).WriteInt (start_frame).WriteInt (length));
1921 int count = res.ReadInt ();
1923 var frames = new FrameInfo [count];
1924 for (int i = 0; i < count; ++i) {
1925 var f = new FrameInfo ();
1926 f.id = res.ReadInt ();
1927 f.method = res.ReadId ();
1928 f.il_offset = res.ReadInt ();
1929 f.flags = (StackFrameFlags)res.ReadByte ();
1936 internal int Thread_GetState (long id) {
1937 return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_STATE, new PacketWriter ().WriteId (id)).ReadInt ();
1940 internal ThreadInfo Thread_GetInfo (long id) {
1941 PacketReader r = SendReceive (CommandSet.THREAD, (int)CmdThread.GET_INFO, new PacketWriter ().WriteId (id));
1943 ThreadInfo res = new ThreadInfo () { is_thread_pool = r.ReadByte () > 0 ? true : false };
1948 internal long Thread_GetId (long id) {
1949 return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_ID, new PacketWriter ().WriteId (id)).ReadLong ();
1952 internal long Thread_GetTID (long id) {
1953 return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_TID, new PacketWriter ().WriteId (id)).ReadLong ();
1956 internal void Thread_SetIP (long id, long method_id, long il_offset) {
1957 SendReceive (CommandSet.THREAD, (int)CmdThread.SET_IP, new PacketWriter ().WriteId (id).WriteId (method_id).WriteLong (il_offset));
1964 internal ModuleInfo Module_GetInfo (long id) {
1965 PacketReader r = SendReceive (CommandSet.MODULE, (int)CmdModule.GET_INFO, new PacketWriter ().WriteId (id));
1966 ModuleInfo info = new ModuleInfo { Name = r.ReadString (), ScopeName = r.ReadString (), FQName = r.ReadString (), Guid = r.ReadString (), Assembly = r.ReadId () };
1974 internal string Assembly_GetLocation (long id) {
1975 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_LOCATION, new PacketWriter ().WriteId (id)).ReadString ();
1978 internal long Assembly_GetEntryPoint (long id) {
1979 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_ENTRY_POINT, new PacketWriter ().WriteId (id)).ReadId ();
1982 internal long Assembly_GetManifestModule (long id) {
1983 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_MANIFEST_MODULE, new PacketWriter ().WriteId (id)).ReadId ();
1986 internal long Assembly_GetObject (long id) {
1987 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_OBJECT, new PacketWriter ().WriteId (id)).ReadId ();
1990 internal long Assembly_GetType (long id, string name, bool ignoreCase) {
1991 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_TYPE, new PacketWriter ().WriteId (id).WriteString (name).WriteBool (ignoreCase)).ReadId ();
1994 internal string Assembly_GetName (long id) {
1995 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_NAME, new PacketWriter ().WriteId (id)).ReadString ();
2002 internal TypeInfo Type_GetInfo (long id) {
2003 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_INFO, new PacketWriter ().WriteId (id));
2004 TypeInfo res = new TypeInfo ();
2006 res.ns = r.ReadString ();
2007 res.name = r.ReadString ();
2008 res.full_name = r.ReadString ();
2009 res.assembly = r.ReadId ();
2010 res.module = r.ReadId ();
2011 res.base_type = r.ReadId ();
2012 res.element_type = r.ReadId ();
2013 res.token = r.ReadInt ();
2014 res.rank = r.ReadByte ();
2015 res.attributes = r.ReadInt ();
2016 int b = r.ReadByte ();
2017 res.is_byref = (b & 1) != 0;
2018 res.is_pointer = (b & 2) != 0;
2019 res.is_primitive = (b & 4) != 0;
2020 res.is_valuetype = (b & 8) != 0;
2021 res.is_enum = (b & 16) != 0;
2022 res.is_gtd = (b & 32) != 0;
2023 res.is_generic_type = (b & 64) != 0;
2025 int nested_len = r.ReadInt ();
2026 res.nested = new long [nested_len];
2027 for (int i = 0; i < nested_len; ++i)
2028 res.nested [i] = r.ReadId ();
2030 if (Version.AtLeast (2, 12))
2031 res.gtd = r.ReadId ();
2032 if (Version.AtLeast (2, 15) && res.is_generic_type) {
2033 int n = r.ReadInt ();
2034 res.type_args = r.ReadIds (n);
2040 internal long[] Type_GetMethods (long id) {
2041 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_METHODS, new PacketWriter ().WriteId (id));
2043 int n = r.ReadInt ();
2044 long[] res = new long [n];
2045 for (int i = 0; i < n; ++i)
2046 res [i] = r.ReadId ();
2050 internal long[] Type_GetFields (long id, out string[] names, out long[] types, out int[] attrs) {
2051 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_FIELDS, new PacketWriter ().WriteId (id));
2053 int n = r.ReadInt ();
2054 long[] res = new long [n];
2055 names = new string [n];
2056 types = new long [n];
2057 attrs = new int [n];
2058 for (int i = 0; i < n; ++i) {
2059 res [i] = r.ReadId ();
2060 names [i] = r.ReadString ();
2061 types [i] = r.ReadId ();
2062 attrs [i] = r.ReadInt ();
2067 internal PropInfo[] Type_GetProperties (long id) {
2068 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_PROPERTIES, new PacketWriter ().WriteId (id));
2070 int n = r.ReadInt ();
2071 PropInfo[] res = new PropInfo [n];
2072 for (int i = 0; i < n; ++i) {
2073 res [i] = new PropInfo ();
2074 res [i].id = r.ReadId ();
2075 res [i].name = r.ReadString ();
2076 res [i].get_method = r.ReadId ();
2077 res [i].set_method = r.ReadId ();
2078 res [i].attrs = r.ReadInt ();
2084 internal long Type_GetObject (long id) {
2085 return SendReceive (CommandSet.TYPE, (int)CmdType.GET_OBJECT, new PacketWriter ().WriteId (id)).ReadId ();
2088 internal ValueImpl[] Type_GetValues (long id, long[] fields, long thread_id) {
2089 int len = fields.Length;
2092 r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_VALUES_2, new PacketWriter ().WriteId (id).WriteId (thread_id).WriteInt (len).WriteIds (fields));
2094 r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_VALUES, new PacketWriter ().WriteId (id).WriteInt (len).WriteIds (fields));
2096 ValueImpl[] res = new ValueImpl [len];
2097 for (int i = 0; i < len; ++i)
2098 res [i] = r.ReadValue ();
2102 internal void Type_SetValues (long id, long[] fields, ValueImpl[] values) {
2103 SendReceive (CommandSet.TYPE, (int)CmdType.SET_VALUES, new PacketWriter ().WriteId (id).WriteInt (fields.Length).WriteIds (fields).WriteValues (values));
2106 internal string[] Type_GetSourceFiles (long id, bool return_full_paths) {
2107 var r = SendReceive (CommandSet.TYPE, return_full_paths ? (int)CmdType.GET_SOURCE_FILES_2 : (int)CmdType.GET_SOURCE_FILES, new PacketWriter ().WriteId (id));
2108 int len = r.ReadInt ();
2109 string[] res = new string [len];
2110 for (int i = 0; i < len; ++i)
2111 res [i] = r.ReadString ();
2115 internal bool Type_IsAssignableFrom (long id, long c_id) {
2116 return SendReceive (CommandSet.TYPE, (int)CmdType.IS_ASSIGNABLE_FROM, new PacketWriter ().WriteId (id).WriteId (c_id)).ReadByte () > 0;
2119 internal CattrInfo[] Type_GetCustomAttributes (long id, long attr_type_id, bool inherit) {
2120 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_CATTRS, new PacketWriter ().WriteId (id).WriteId (attr_type_id));
2121 return ReadCattrs (r);
2124 internal CattrInfo[] Type_GetFieldCustomAttributes (long id, long field_id, long attr_type_id, bool inherit) {
2125 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_FIELD_CATTRS, new PacketWriter ().WriteId (id).WriteId (field_id).WriteId (attr_type_id));
2126 return ReadCattrs (r);
2129 internal CattrInfo[] Type_GetPropertyCustomAttributes (long id, long field_id, long attr_type_id, bool inherit) {
2130 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_PROPERTY_CATTRS, new PacketWriter ().WriteId (id).WriteId (field_id).WriteId (attr_type_id));
2131 return ReadCattrs (r);
2134 public long[] Type_GetMethodsByNameFlags (long id, string name, int flags, bool ignoreCase) {
2135 flags |= ignoreCase ? (int)BindingFlagsExtensions.BINDING_FLAGS_IGNORE_CASE : 0;
2136 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.CMD_TYPE_GET_METHODS_BY_NAME_FLAGS, new PacketWriter ().WriteId (id).WriteString (name).WriteInt (flags));
2137 int len = r.ReadInt ();
2138 long[] res = new long [len];
2139 for (int i = 0; i < len; ++i)
2140 res [i] = r.ReadId ();
2144 internal long[] Type_GetInterfaces (long id) {
2145 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_INTERFACES, new PacketWriter ().WriteId (id));
2146 int len = r.ReadInt ();
2147 return r.ReadIds (len);
2150 internal IfaceMapInfo[] Type_GetInterfaceMap (long id, long[] ids) {
2151 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_INTERFACE_MAP, new PacketWriter ().WriteId (id).WriteInt (ids.Length).WriteIds (ids));
2152 var res = new IfaceMapInfo [ids.Length];
2153 for (int i = 0; i < ids.Length; ++i) {
2154 int n = r.ReadInt ();
2156 res [i].iface_id = ids [i];
2157 res [i].iface_methods = r.ReadIds (n);
2158 res [i].target_methods = r.ReadIds (n);
2164 internal bool Type_IsInitialized (long id) {
2165 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.IS_INITIALIZED, new PacketWriter ().WriteId (id));
2166 return r.ReadInt () == 1;
2169 internal long Type_CreateInstance (long id) {
2170 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.CREATE_INSTANCE, new PacketWriter ().WriteId (id));
2178 internal FieldMirrorInfo Field_GetInfo (long id) {
2179 PacketReader r = SendReceive (CommandSet.FIELD, (int)CmdField.GET_INFO, new PacketWriter ().WriteId (id));
2180 FieldMirrorInfo info = new FieldMirrorInfo { Name = r.ReadString (), Parent = r.ReadId (), TypeId = r.ReadId (), Attrs = r.ReadInt () };
2188 internal int EnableEvent (EventType etype, SuspendPolicy suspend_policy, List<Modifier> mods) {
2189 var w = new PacketWriter ().WriteByte ((byte)etype).WriteByte ((byte)suspend_policy);
2191 if (mods.Count > 255)
2192 throw new NotImplementedException ();
2193 w.WriteByte ((byte)mods.Count);
2194 foreach (Modifier mod in mods) {
2195 if (mod is CountModifier) {
2196 w.WriteByte ((byte)ModifierKind.COUNT);
2197 w.WriteInt ((mod as CountModifier).Count);
2198 } else if (mod is LocationModifier) {
2199 w.WriteByte ((byte)ModifierKind.LOCATION_ONLY);
2200 w.WriteId ((mod as LocationModifier).Method);
2201 w.WriteLong ((mod as LocationModifier).Location);
2202 } else if (mod is StepModifier) {
2203 w.WriteByte ((byte)ModifierKind.STEP);
2204 w.WriteId ((mod as StepModifier).Thread);
2205 w.WriteInt ((mod as StepModifier).Size);
2206 w.WriteInt ((mod as StepModifier).Depth);
2207 if (Version.AtLeast (2, 16))
2208 w.WriteInt ((mod as StepModifier).Filter);
2209 } else if (mod is ThreadModifier) {
2210 w.WriteByte ((byte)ModifierKind.THREAD_ONLY);
2211 w.WriteId ((mod as ThreadModifier).Thread);
2212 } else if (mod is ExceptionModifier) {
2213 var em = mod as ExceptionModifier;
2214 w.WriteByte ((byte)ModifierKind.EXCEPTION_ONLY);
2215 w.WriteId (em.Type);
2216 if (Version.MajorVersion > 2 || Version.MinorVersion > 0) {
2217 /* This is only supported in protocol version 2.1 */
2218 w.WriteBool (em.Caught);
2219 w.WriteBool (em.Uncaught);
2220 } else if (!em.Caught || !em.Uncaught) {
2221 throw new NotSupportedException ("This request is not supported by the protocol version implemented by the debuggee.");
2223 if (Version.MajorVersion > 2 || Version.MinorVersion > 24) {
2224 w.WriteBool (em.Subclasses);
2225 } else if (!em.Subclasses) {
2226 throw new NotSupportedException ("This request is not supported by the protocol version implemented by the debuggee.");
2228 } else if (mod is AssemblyModifier) {
2229 w.WriteByte ((byte)ModifierKind.ASSEMBLY_ONLY);
2230 var amod = (mod as AssemblyModifier);
2231 w.WriteInt (amod.Assemblies.Length);
2232 foreach (var id in amod.Assemblies)
2234 } else if (mod is SourceFileModifier) {
2235 w.WriteByte ((byte)ModifierKind.SOURCE_FILE_ONLY);
2236 var smod = (mod as SourceFileModifier);
2237 w.WriteInt (smod.SourceFiles.Length);
2238 foreach (var s in smod.SourceFiles)
2240 } else if (mod is TypeNameModifier) {
2241 w.WriteByte ((byte)ModifierKind.TYPE_NAME_ONLY);
2242 var tmod = (mod as TypeNameModifier);
2243 w.WriteInt (tmod.TypeNames.Length);
2244 foreach (var s in tmod.TypeNames)
2247 throw new NotImplementedException ();
2253 return SendReceive (CommandSet.EVENT_REQUEST, (int)CmdEventRequest.SET, w).ReadInt ();
2256 internal void ClearEventRequest (EventType etype, int req_id) {
2257 SendReceive (CommandSet.EVENT_REQUEST, (int)CmdEventRequest.CLEAR, new PacketWriter ().WriteByte ((byte)etype).WriteInt (req_id));
2260 internal void ClearAllBreakpoints () {
2261 SendReceive (CommandSet.EVENT_REQUEST, (int)CmdEventRequest.CLEAR_ALL_BREAKPOINTS, new PacketWriter ());
2267 internal ValueImpl StackFrame_GetThis (long thread_id, long id) {
2268 PacketReader r = SendReceive (CommandSet.STACK_FRAME, (int)CmdStackFrame.GET_THIS, new PacketWriter ().WriteId (thread_id).WriteId (id));
2269 return r.ReadValue ();
2272 internal ValueImpl[] StackFrame_GetValues (long thread_id, long id, int[] pos) {
2273 /* pos < 0 -> argument at pos (-pos) - 1 */
2274 /* pos >= 0 -> local at pos */
2275 int len = pos.Length;
2276 PacketReader r = SendReceive (CommandSet.STACK_FRAME, (int)CmdStackFrame.GET_VALUES, new PacketWriter ().WriteId (thread_id).WriteId (id).WriteInt (len).WriteInts (pos));
2278 ValueImpl[] res = new ValueImpl [len];
2279 for (int i = 0; i < len; ++i)
2280 res [i] = r.ReadValue ();
2284 internal void StackFrame_SetValues (long thread_id, long id, int[] pos, ValueImpl[] values) {
2285 /* pos < 0 -> argument at pos (-pos) - 1 */
2286 /* pos >= 0 -> local at pos */
2287 int len = pos.Length;
2288 SendReceive (CommandSet.STACK_FRAME, (int)CmdStackFrame.SET_VALUES, new PacketWriter ().WriteId (thread_id).WriteId (id).WriteInt (len).WriteInts (pos).WriteValues (values));
2294 internal int[] Array_GetLength (long id, out int rank, out int[] lower_bounds) {
2295 var r = SendReceive (CommandSet.ARRAY_REF, (int)CmdArrayRef.GET_LENGTH, new PacketWriter ().WriteId (id));
2296 rank = r.ReadInt ();
2297 int[] res = new int [rank];
2298 lower_bounds = new int [rank];
2299 for (int i = 0; i < rank; ++i) {
2300 res [i] = r.ReadInt ();
2301 lower_bounds [i] = r.ReadInt ();
2306 internal ValueImpl[] Array_GetValues (long id, int index, int len) {
2307 var r = SendReceive (CommandSet.ARRAY_REF, (int)CmdArrayRef.GET_VALUES, new PacketWriter ().WriteId (id).WriteInt (index).WriteInt (len));
2308 ValueImpl[] res = new ValueImpl [len];
2309 for (int i = 0; i < len; ++i)
2310 res [i] = r.ReadValue ();
2314 internal void Array_SetValues (long id, int index, ValueImpl[] values) {
2315 SendReceive (CommandSet.ARRAY_REF, (int)CmdArrayRef.SET_VALUES, new PacketWriter ().WriteId (id).WriteInt (index).WriteInt (values.Length).WriteValues (values));
2321 internal string String_GetValue (long id) {
2322 return SendReceive (CommandSet.STRING_REF, (int)CmdStringRef.GET_VALUE, new PacketWriter ().WriteId (id)).ReadString ();
2325 internal int String_GetLength (long id) {
2326 return (int)SendReceive (CommandSet.STRING_REF, (int)CmdStringRef.GET_LENGTH, new PacketWriter ().WriteId (id)).ReadLong ();
2329 internal char[] String_GetChars (long id, int index, int length) {
2330 var r = SendReceive (CommandSet.STRING_REF, (int)CmdStringRef.GET_CHARS, new PacketWriter ().WriteId (id).WriteLong (index).WriteLong (length));
2331 var res = new char [length];
2332 for (int i = 0; i < length; ++i)
2333 res [i] = (char)r.ReadShort ();
2340 internal long Object_GetType (long id) {
2341 return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_TYPE, new PacketWriter ().WriteId (id)).ReadId ();
2344 internal long Object_GetDomain (long id) {
2345 return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_DOMAIN, new PacketWriter ().WriteId (id)).ReadId ();
2348 internal ValueImpl[] Object_GetValues (long id, long[] fields) {
2349 int len = fields.Length;
2350 PacketReader r = SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_VALUES, new PacketWriter ().WriteId (id).WriteInt (len).WriteIds (fields));
2352 ValueImpl[] res = new ValueImpl [len];
2353 for (int i = 0; i < len; ++i)
2354 res [i] = r.ReadValue ();
2358 internal void Object_SetValues (long id, long[] fields, ValueImpl[] values) {
2359 SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.SET_VALUES, new PacketWriter ().WriteId (id).WriteInt (fields.Length).WriteIds (fields).WriteValues (values));
2362 internal bool Object_IsCollected (long id) {
2363 return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.IS_COLLECTED, new PacketWriter ().WriteId (id)).ReadInt () == 1;
2366 internal long Object_GetAddress (long id) {
2367 return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_ADDRESS, new PacketWriter ().WriteId (id)).ReadLong ();
2370 internal ObjectRefInfo Object_GetInfo (long id) {
2371 ObjectRefInfo res = new ObjectRefInfo ();
2372 PacketReader r = SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_INFO, new PacketWriter ().WriteId (id));
2374 res.type_id = r.ReadId ();
2375 res.domain_id = r.ReadId ();
2379 public void ForceDisconnect ()
2382 disconnected = true;
2387 class TcpConnection : Connection
2391 internal TcpConnection (Socket socket)
2393 this.socket = socket;
2394 //socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.NoDelay, 1);
2397 internal EndPoint EndPoint {
2399 return socket.RemoteEndPoint;
2403 protected override int TransportSend (byte[] buf, int buf_offset, int len)
2405 return socket.Send (buf, buf_offset, len, SocketFlags.None);
2408 protected override int TransportReceive (byte[] buf, int buf_offset, int len)
2410 return socket.Receive (buf, buf_offset, len, SocketFlags.None);
2413 protected override void TransportSetTimeouts (int send_timeout, int receive_timeout)
2415 socket.SendTimeout = send_timeout;
2416 socket.ReceiveTimeout = receive_timeout;
2419 protected override void TransportClose ()
2425 /* This is the interface exposed by the debugger towards the debugger agent */
2426 interface IEventHandler
2428 void Events (SuspendPolicy suspend_policy, EventInfo[] events);
2430 void VMDisconnect (int req_id, long thread_id, string vm_uri);