4 using System.Net.Sockets;
5 using System.Threading;
6 using System.Collections.Generic;
8 using System.Diagnostics;
9 using Mono.Cecil.Metadata;
11 namespace Mono.Debugger.Soft
13 public class VersionInfo {
14 public string VMVersion {
18 public int MajorVersion {
22 public int MinorVersion {
27 * Check that this version is at least major:minor
29 public bool AtLeast (int major, int minor) {
30 if ((MajorVersion > major) || ((MajorVersion == major && MinorVersion >= minor)))
38 public string source_file;
39 public byte[] guid, hash;
43 public int max_il_offset;
44 public int[] il_offsets;
45 public int[] line_numbers;
46 public int[] column_numbers;
47 public int[] end_line_numbers;
48 public int[] end_column_numbers;
49 public SourceInfo[] source_files;
56 public StackFrameFlags flags;
60 public string ns, name, full_name;
61 public long assembly, module, base_type, element_type;
62 public int token, rank, attributes;
63 public bool is_byref, is_pointer, is_primitive, is_valuetype, is_enum;
64 public bool is_gtd, is_generic_type;
67 public long[] type_args;
72 public long[] iface_methods;
73 public long[] target_methods;
77 public int attributes, iattributes, token;
78 public bool is_gmd, is_generic_method;
80 public long[] type_args;
83 class MethodBodyInfo {
85 public ExceptionClauseInfo[] clauses;
88 struct ExceptionClauseInfo {
89 public ExceptionClauseFlags flags;
90 public int try_offset;
91 public int try_length;
92 public int handler_offset;
93 public int handler_length;
94 public int filter_offset;
95 public long catch_type_id;
99 enum ExceptionClauseFlags {
107 public int call_conv;
108 public int param_count;
109 public int generic_param_count;
110 public long ret_type;
111 public long[] param_types;
112 public string[] param_names;
117 public string[] names;
118 public int[] live_range_start;
119 public int[] live_range_end;
125 public long get_method, set_method;
129 class CattrNamedArgInfo {
130 public bool is_property;
132 public ValueImpl value;
137 public ValueImpl[] ctor_args;
138 public CattrNamedArgInfo[] named_args;
142 public bool is_thread_pool;
145 struct ObjectRefInfo {
147 public long domain_id;
151 VALUE_TYPE_ID_NULL = 0xf0,
152 VALUE_TYPE_ID_TYPE = 0xf1,
153 VALUE_TYPE_ID_PARENT_VTYPE = 0xf2
159 DISABLE_BREAKPOINTS = 0x1,
160 SINGLE_THREADED = 0x2
206 public ElementType Type; /* or one of the VALUE_TYPE_ID constants */
209 public long Klass; // For ElementType.ValueType
210 public ValueImpl[] Fields; // for ElementType.ValueType
211 public bool IsEnum; // For ElementType.ValueType
212 public long Id; /* For VALUE_TYPE_ID_TYPE */
213 public int Index; /* For VALUE_TYPE_PARENT_VTYPE */
217 public string Name, ScopeName, FQName, Guid;
218 public long Assembly;
221 class FieldMirrorInfo {
223 public long Parent, TypeId;
236 enum StackFrameFlags {
239 NATIVE_TRANSITION = 2
242 class ResolvedToken {
243 public TokenType Type;
251 class CountModifier : Modifier {
257 class LocationModifier : Modifier {
262 public long Location {
267 class StepModifier : Modifier {
285 class ThreadModifier : Modifier {
291 class ExceptionModifier : Modifier {
298 public bool Uncaught {
301 public bool Subclasses {
306 class AssemblyModifier : Modifier {
307 public long[] Assemblies {
312 class SourceFileModifier : Modifier {
313 public string[] SourceFiles {
318 class TypeNameModifier : Modifier {
319 public string[] TypeNames {
325 public EventType EventType {
333 public SuspendPolicy SuspendPolicy {
337 public long ThreadId {
345 public long Location {
353 public string Category {
357 public string Message {
361 public int ExitCode {
365 public EventInfo (EventType type, int req_id) {
371 public enum ErrorCode {
374 INVALID_FIELDID = 25,
375 INVALID_FRAMEID = 30,
376 NOT_IMPLEMENTED = 100,
378 INVALID_ARGUMENT = 102,
380 ERR_NO_INVOCATION = 104,
381 ABSENT_INFORMATION = 105,
382 NO_SEQ_POINT_AT_IL_OFFSET = 106
385 public class ErrorHandlerEventArgs : EventArgs {
387 public ErrorCode ErrorCode {
393 * Represents the connection to the debuggee
395 public abstract class Connection
398 * The protocol and the packet format is based on JDWP, the differences
399 * are in the set of supported events, and the commands.
401 internal const string HANDSHAKE_STRING = "DWP-Handshake";
403 internal const int HEADER_LENGTH = 11;
405 static readonly bool EnableConnectionLogging = !String.IsNullOrEmpty (Environment.GetEnvironmentVariable ("MONO_SDB_LOG"));
406 static int ConnectionId;
407 readonly StreamWriter LoggingStream = EnableConnectionLogging ?
408 new StreamWriter (string.Format ("/tmp/sdb_conn_log_{0}", ConnectionId++), false) : null;
411 * Th version of the wire-protocol implemented by the library. The library
412 * and the debuggee can communicate if they implement the same major version.
413 * If they implement a different minor version, they can communicate, but some
414 * features might not be available. This allows older clients to communicate
415 * with newer runtimes, and vice versa.
417 internal const int MAJOR_VERSION = 2;
418 internal const int MINOR_VERSION = 33;
420 enum WPSuspendPolicy {
448 APPDOMAIN_CREATE = 4, // Not in JDI
449 APPDOMAIN_UNLOAD = 5, // Not in JDI
470 SOURCE_FILE_ONLY = 12,
482 SET_PROTOCOL_VERSION = 8,
485 GET_TYPES_FOR_SOURCE_FILE = 11,
499 /* FIXME: Merge into GET_INFO when the major protocol version is increased */
506 enum CmdEventRequest {
509 CLEAR_ALL_BREAKPOINTS = 3
514 GET_FRIENDLY_NAME = 2,
516 GET_ENTRY_ASSEMBLY = 4,
519 CREATE_BOXED_VALUE = 7
525 GET_MANIFEST_MODULE = 3,
537 GET_DECLARING_TYPE = 2,
545 MAKE_GENERIC_METHOD = 10
554 GET_SOURCE_FILES = 6,
556 IS_ASSIGNABLE_FROM = 8,
559 GET_FIELD_CATTRS = 11,
560 GET_PROPERTY_CATTRS = 12,
561 /* FIXME: Merge into GET_SOURCE_FILES when the major protocol version is increased */
562 GET_SOURCE_FILES_2 = 13,
563 /* FIXME: Merge into GET_VALUES when the major protocol version is increased */
565 CMD_TYPE_GET_METHODS_BY_NAME_FLAGS = 15,
567 GET_INTERFACE_MAP = 17,
577 enum BindingFlagsExtensions {
578 BINDING_FLAGS_IGNORE_CASE = 0x70000000,
611 public int command_set;
616 internal static int GetPacketLength (byte[] header) {
618 return decode_int (header, ref offset);
621 internal static bool IsReplyPacket (byte[] packet) {
623 return decode_byte (packet, ref offset) == 0x80;
626 internal static int GetPacketId (byte[] packet) {
628 return decode_int (packet, ref offset);
631 static int decode_byte (byte[] packet, ref int offset) {
632 return packet [offset++];
635 static int decode_short (byte[] packet, ref int offset) {
636 int res = ((int)packet [offset] << 8) | (int)packet [offset + 1];
641 static int decode_int (byte[] packet, ref int offset) {
642 int res = ((int)packet [offset] << 24) | ((int)packet [offset + 1] << 16) | ((int)packet [offset + 2] << 8) | (int)packet [offset + 3];
647 static long decode_id (byte[] packet, ref int offset) {
648 return decode_int (packet, ref offset);
651 static long decode_long (byte[] packet, ref int offset) {
652 uint high = (uint)decode_int (packet, ref offset);
653 uint low = (uint)decode_int (packet, ref offset);
655 return (long)(((ulong)high << 32) | (ulong)low);
658 internal static SuspendPolicy decode_suspend_policy (int suspend_policy) {
659 switch ((WPSuspendPolicy)suspend_policy) {
660 case WPSuspendPolicy.NONE:
661 return SuspendPolicy.None;
662 case WPSuspendPolicy.EVENT_THREAD:
663 return SuspendPolicy.EventThread;
664 case WPSuspendPolicy.ALL:
665 return SuspendPolicy.All;
667 throw new NotImplementedException ();
671 static Header decode_command_header (byte[] packet) {
673 Header res = new Header ();
675 decode_int (packet, ref offset);
676 res.id = decode_int (packet, ref offset);
677 res.flags = decode_byte (packet, ref offset);
678 res.command_set = decode_byte (packet, ref offset);
679 res.command = decode_byte (packet, ref offset);
684 static void encode_byte (byte[] buf, int b, ref int offset) {
685 buf [offset] = (byte)b;
689 static void encode_int (byte[] buf, int i, ref int offset) {
690 buf [offset] = (byte)((i >> 24) & 0xff);
691 buf [offset + 1] = (byte)((i >> 16) & 0xff);
692 buf [offset + 2] = (byte)((i >> 8) & 0xff);
693 buf [offset + 3] = (byte)((i >> 0) & 0xff);
697 static void encode_id (byte[] buf, long id, ref int offset) {
698 encode_int (buf, (int)id, ref offset);
701 static void encode_long (byte[] buf, long l, ref int offset) {
702 encode_int (buf, (int)((l >> 32) & 0xffffffff), ref offset);
703 encode_int (buf, (int)(l & 0xffffffff), ref offset);
706 internal static byte[] EncodePacket (int id, int commandSet, int command, byte[] data, int dataLen) {
707 byte[] buf = new byte [dataLen + 11];
710 encode_int (buf, buf.Length, ref offset);
711 encode_int (buf, id, ref offset);
712 encode_byte (buf, 0, ref offset);
713 encode_byte (buf, commandSet, ref offset);
714 encode_byte (buf, command, ref offset);
716 for (int i = 0; i < dataLen; ++i)
717 buf [offset + i] = data [i];
726 public PacketReader (byte[] packet) {
727 this.packet = packet;
730 Header header = decode_command_header (packet);
731 CommandSet = (CommandSet)header.command_set;
732 Command = header.command;
736 ReadInt (); // length
738 ReadByte (); // flags
739 ErrorCode = ReadShort ();
742 public CommandSet CommandSet {
750 public int ErrorCode {
760 public int ReadByte () {
761 return decode_byte (packet, ref offset);
764 public int ReadShort () {
765 return decode_short (packet, ref offset);
768 public int ReadInt () {
769 return decode_int (packet, ref offset);
772 public long ReadId () {
773 return decode_id (packet, ref offset);
776 public long ReadLong () {
777 return decode_long (packet, ref offset);
780 public float ReadFloat () {
781 float f = DataConverter.FloatFromBE (packet, offset);
786 public double ReadDouble () {
787 double d = DataConverter.DoubleFromBE (packet, offset);
792 public string ReadString () {
793 int len = decode_int (packet, ref offset);
794 string res = new String (Encoding.UTF8.GetChars (packet, offset, len));
799 public ValueImpl ReadValue () {
800 ElementType etype = (ElementType)ReadByte ();
803 case ElementType.Void:
804 return new ValueImpl { Type = etype };
806 return new ValueImpl { Type = etype, Value = (sbyte)ReadInt () };
808 return new ValueImpl { Type = etype, Value = (byte)ReadInt () };
809 case ElementType.Boolean:
810 return new ValueImpl { Type = etype, Value = ReadInt () != 0 };
812 return new ValueImpl { Type = etype, Value = (short)ReadInt () };
814 return new ValueImpl { Type = etype, Value = (ushort)ReadInt () };
815 case ElementType.Char:
816 return new ValueImpl { Type = etype, Value = (char)ReadInt () };
818 return new ValueImpl { Type = etype, Value = ReadInt () };
820 return new ValueImpl { Type = etype, Value = (uint)ReadInt () };
822 return new ValueImpl { Type = etype, Value = ReadLong () };
824 return new ValueImpl { Type = etype, Value = (ulong)ReadLong () };
826 return new ValueImpl { Type = etype, Value = ReadFloat () };
828 return new ValueImpl { Type = etype, Value = ReadDouble () };
831 case ElementType.Ptr:
832 // FIXME: The client and the debuggee might have different word sizes
833 return new ValueImpl { Type = etype, Value = ReadLong () };
834 case ElementType.String:
835 case ElementType.SzArray:
836 case ElementType.Class:
837 case ElementType.Array:
838 case ElementType.Object:
839 long objid = ReadId ();
840 return new ValueImpl () { Type = etype, Objid = objid };
841 case ElementType.ValueType:
842 bool is_enum = ReadByte () == 1;
843 long klass = ReadId ();
844 long nfields = ReadInt ();
845 ValueImpl[] fields = new ValueImpl [nfields];
846 for (int i = 0; i < nfields; ++i)
847 fields [i] = ReadValue ();
848 return new ValueImpl () { Type = etype, Klass = klass, Fields = fields, IsEnum = is_enum };
849 case (ElementType)ValueTypeId.VALUE_TYPE_ID_NULL:
850 return new ValueImpl { Type = etype };
851 case (ElementType)ValueTypeId.VALUE_TYPE_ID_TYPE:
852 return new ValueImpl () { Type = etype, Id = ReadId () };
853 case (ElementType)ValueTypeId.VALUE_TYPE_ID_PARENT_VTYPE:
854 return new ValueImpl () { Type = etype, Index = ReadInt () };
856 throw new NotImplementedException ("Unable to handle type " + etype);
860 public long[] ReadIds (int n) {
861 long[] res = new long [n];
862 for (int i = 0; i < n; ++i)
873 public PacketWriter () {
874 data = new byte [1024];
878 void MakeRoom (int size) {
879 if (offset + size >= data.Length) {
880 int new_len = data.Length * 2;
881 while (new_len < offset + size) {
884 byte[] new_data = new byte [new_len];
885 Array.Copy (data, new_data, data.Length);
890 public PacketWriter WriteByte (byte val) {
892 encode_byte (data, val, ref offset);
896 public PacketWriter WriteInt (int val) {
898 encode_int (data, val, ref offset);
902 public PacketWriter WriteId (long id) {
904 encode_id (data, id, ref offset);
908 public PacketWriter WriteLong (long val) {
910 encode_long (data, val, ref offset);
914 public PacketWriter WriteFloat (float f) {
916 byte[] b = DataConverter.GetBytesBE (f);
917 for (int i = 0; i < 4; ++i)
918 data [offset + i] = b [i];
923 public PacketWriter WriteDouble (double d) {
925 byte[] b = DataConverter.GetBytesBE (d);
926 for (int i = 0; i < 8; ++i)
927 data [offset + i] = b [i];
932 public PacketWriter WriteInts (int[] ids) {
933 for (int i = 0; i < ids.Length; ++i)
938 public PacketWriter WriteIds (long[] ids) {
939 for (int i = 0; i < ids.Length; ++i)
944 public PacketWriter WriteString (string s) {
946 return WriteInt (-1);
948 byte[] b = Encoding.UTF8.GetBytes (s);
950 encode_int (data, b.Length, ref offset);
952 Buffer.BlockCopy (b, 0, data, offset, b.Length);
957 public PacketWriter WriteBool (bool val) {
958 WriteByte (val ? (byte)1 : (byte)0);
962 public PacketWriter WriteValue (ValueImpl v) {
966 t = TypeCodeToElementType (Type.GetTypeCode (v.Value.GetType ()));
971 case ElementType.Boolean:
972 WriteInt ((bool)v.Value ? 1 : 0);
974 case ElementType.Char:
975 WriteInt ((int)(char)v.Value);
978 WriteInt ((int)(sbyte)v.Value);
981 WriteInt ((int)(byte)v.Value);
984 WriteInt ((int)(short)v.Value);
987 WriteInt ((int)(ushort)v.Value);
990 WriteInt ((int)(int)v.Value);
993 WriteInt ((int)(uint)v.Value);
996 WriteLong ((long)(long)v.Value);
999 WriteLong ((long)(ulong)v.Value);
1001 case ElementType.R4:
1002 WriteFloat ((float)v.Value);
1004 case ElementType.R8:
1005 WriteDouble ((double)v.Value);
1007 case ElementType.String:
1008 case ElementType.SzArray:
1009 case ElementType.Class:
1010 case ElementType.Array:
1011 case ElementType.Object:
1014 case ElementType.ValueType:
1017 throw new NotImplementedException ();
1020 WriteInt (v.Fields.Length);
1021 for (int i = 0; i < v.Fields.Length; ++i)
1022 WriteValue (v.Fields [i]);
1024 case (ElementType)ValueTypeId.VALUE_TYPE_ID_NULL:
1027 throw new NotImplementedException ();
1033 public PacketWriter WriteValues (ValueImpl[] values) {
1034 for (int i = 0; i < values.Length; ++i)
1035 WriteValue (values [i]);
1039 public byte[] Data {
1052 delegate void ReplyCallback (int packet_id, byte[] packet);
1055 Thread receiver_thread;
1056 Dictionary<int, byte[]> reply_packets;
1057 Dictionary<int, ReplyCallback> reply_cbs;
1058 Dictionary<int, int> reply_cb_counts;
1059 object reply_packets_monitor;
1061 internal event EventHandler<ErrorHandlerEventArgs> ErrorHandler;
1063 protected Connection () {
1065 reply_packets = new Dictionary<int, byte[]> ();
1066 reply_cbs = new Dictionary<int, ReplyCallback> ();
1067 reply_cb_counts = new Dictionary<int, int> ();
1068 reply_packets_monitor = new Object ();
1071 protected abstract int TransportReceive (byte[] buf, int buf_offset, int len);
1072 protected abstract int TransportSend (byte[] buf, int buf_offset, int len);
1073 protected abstract void TransportSetTimeouts (int send_timeout, int receive_timeout);
1074 protected abstract void TransportClose ();
1076 internal VersionInfo Version;
1078 int Receive (byte[] buf, int buf_offset, int len) {
1081 while (offset < len) {
1082 int n = TransportReceive (buf, buf_offset + offset, len - offset);
1092 // Do the wire protocol handshake
1093 internal void Connect () {
1094 byte[] buf = new byte [HANDSHAKE_STRING.Length];
1095 char[] cbuf = new char [buf.Length];
1097 // FIXME: Add a timeout
1098 int n = Receive (buf, 0, buf.Length);
1100 throw new IOException ("DWP Handshake failed.");
1101 for (int i = 0; i < buf.Length; ++i)
1102 cbuf [i] = (char)buf [i];
1104 if (new String (cbuf) != HANDSHAKE_STRING)
1105 throw new IOException ("DWP Handshake failed.");
1107 TransportSend (buf, 0, buf.Length);
1109 receiver_thread = new Thread (new ThreadStart (receiver_thread_main));
1110 receiver_thread.Name = "SDB Receiver";
1111 receiver_thread.IsBackground = true;
1112 receiver_thread.Start ();
1114 Version = VM_GetVersion ();
1117 // Tell the debuggee our protocol version, so newer debuggees can work
1118 // with older clients
1122 // Older debuggees might not support this request
1123 EventHandler<ErrorHandlerEventArgs> OrigErrorHandler = ErrorHandler;
1124 ErrorHandler = null;
1125 ErrorHandler += delegate (object sender, ErrorHandlerEventArgs args) {
1126 throw new NotSupportedException ();
1129 VM_SetProtocolVersion (MAJOR_VERSION, MINOR_VERSION);
1130 } catch (NotSupportedException) {
1132 ErrorHandler = OrigErrorHandler;
1135 internal byte[] ReadPacket () {
1136 // FIXME: Throw ClosedConnectionException () if the connection is closed
1137 // FIXME: Throw ClosedConnectionException () if another thread closes the connection
1139 byte[] header = new byte [HEADER_LENGTH];
1141 int len = Receive (header, 0, header.Length);
1143 return new byte [0];
1144 if (len != HEADER_LENGTH) {
1146 throw new IOException ("Packet of length " + len + " is read.");
1149 int packetLength = GetPacketLength (header);
1150 if (packetLength < 11)
1151 throw new IOException ("Invalid packet length.");
1153 if (packetLength == 11) {
1156 byte[] buf = new byte [packetLength];
1157 for (int i = 0; i < header.Length; ++i)
1158 buf [i] = header [i];
1159 len = Receive (buf, header.Length, packetLength - header.Length);
1160 if (len != packetLength - header.Length)
1161 throw new IOException ();
1166 internal void WritePacket (byte[] packet) {
1167 // FIXME: Throw ClosedConnectionException () if the connection is closed
1168 // FIXME: Throw ClosedConnectionException () if another thread closes the connection
1170 TransportSend (packet, 0, packet.Length);
1173 internal void Close () {
1177 internal bool IsClosed {
1185 void receiver_thread_main () {
1188 bool res = ReceivePacket ();
1191 } catch (Exception ex) {
1193 Console.WriteLine (ex);
1199 lock (reply_packets_monitor) {
1200 disconnected = true;
1201 Monitor.PulseAll (reply_packets_monitor);
1204 EventHandler.VMDisconnect (0, 0, null);
1207 bool ReceivePacket () {
1208 byte[] packet = ReadPacket ();
1210 if (packet.Length == 0) {
1214 if (IsReplyPacket (packet)) {
1215 int id = GetPacketId (packet);
1216 ReplyCallback cb = null;
1217 lock (reply_packets_monitor) {
1218 reply_cbs.TryGetValue (id, out cb);
1220 reply_packets [id] = packet;
1221 Monitor.PulseAll (reply_packets_monitor);
1223 int c = reply_cb_counts [id];
1226 reply_cbs.Remove (id);
1227 reply_cb_counts.Remove (id);
1233 cb.Invoke (id, packet);
1235 PacketReader r = new PacketReader (packet);
1237 if (r.CommandSet == CommandSet.EVENT && r.Command == (int)CmdEvent.COMPOSITE) {
1238 int spolicy = r.ReadByte ();
1239 int nevents = r.ReadInt ();
1241 SuspendPolicy suspend_policy = decode_suspend_policy (spolicy);
1243 EventInfo[] events = new EventInfo [nevents];
1245 for (int i = 0; i < nevents; ++i) {
1246 EventKind kind = (EventKind)r.ReadByte ();
1247 int req_id = r.ReadInt ();
1249 EventType etype = (EventType)kind;
1251 long thread_id = r.ReadId ();
1252 if (kind == EventKind.VM_START) {
1253 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id };
1254 //EventHandler.VMStart (req_id, thread_id, null);
1255 } else if (kind == EventKind.VM_DEATH) {
1257 if (Version.AtLeast (2, 27))
1258 exit_code = r.ReadInt ();
1259 //EventHandler.VMDeath (req_id, 0, null);
1260 events [i] = new EventInfo (etype, req_id) { ExitCode = exit_code };
1261 } else if (kind == EventKind.THREAD_START) {
1262 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = thread_id };
1263 //EventHandler.ThreadStart (req_id, thread_id, thread_id);
1264 } else if (kind == EventKind.THREAD_DEATH) {
1265 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = thread_id };
1266 //EventHandler.ThreadDeath (req_id, thread_id, thread_id);
1267 } else if (kind == EventKind.ASSEMBLY_LOAD) {
1268 long id = r.ReadId ();
1269 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1270 //EventHandler.AssemblyLoad (req_id, thread_id, id);
1271 } else if (kind == EventKind.ASSEMBLY_UNLOAD) {
1272 long id = r.ReadId ();
1273 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1274 //EventHandler.AssemblyUnload (req_id, thread_id, id);
1275 } else if (kind == EventKind.TYPE_LOAD) {
1276 long id = r.ReadId ();
1277 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1278 //EventHandler.TypeLoad (req_id, thread_id, id);
1279 } else if (kind == EventKind.METHOD_ENTRY) {
1280 long id = r.ReadId ();
1281 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1282 //EventHandler.MethodEntry (req_id, thread_id, id);
1283 } else if (kind == EventKind.METHOD_EXIT) {
1284 long id = r.ReadId ();
1285 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1286 //EventHandler.MethodExit (req_id, thread_id, id);
1287 } else if (kind == EventKind.BREAKPOINT) {
1288 long id = r.ReadId ();
1289 long loc = r.ReadLong ();
1290 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1291 //EventHandler.Breakpoint (req_id, thread_id, id, loc);
1292 } else if (kind == EventKind.STEP) {
1293 long id = r.ReadId ();
1294 long loc = r.ReadLong ();
1295 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1296 //EventHandler.Step (req_id, thread_id, id, loc);
1297 } else if (kind == EventKind.EXCEPTION) {
1298 long id = r.ReadId ();
1299 long loc = 0; // FIXME
1300 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1301 //EventHandler.Exception (req_id, thread_id, id, loc);
1302 } else if (kind == EventKind.APPDOMAIN_CREATE) {
1303 long id = r.ReadId ();
1304 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1305 //EventHandler.AppDomainCreate (req_id, thread_id, id);
1306 } else if (kind == EventKind.APPDOMAIN_UNLOAD) {
1307 long id = r.ReadId ();
1308 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1309 //EventHandler.AppDomainUnload (req_id, thread_id, id);
1310 } else if (kind == EventKind.USER_BREAK) {
1313 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1314 //EventHandler.Exception (req_id, thread_id, id, loc);
1315 } else if (kind == EventKind.USER_LOG) {
1316 int level = r.ReadInt ();
1317 string category = r.ReadString ();
1318 string message = r.ReadString ();
1319 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Level = level, Category = category, Message = message };
1320 //EventHandler.Exception (req_id, thread_id, id, loc);
1321 } else if (kind == EventKind.KEEPALIVE) {
1322 events [i] = new EventInfo (etype, req_id) { };
1324 throw new NotImplementedException ("Unknown event kind: " + kind);
1328 EventHandler.Events (suspend_policy, events);
1335 internal IEventHandler EventHandler {
1339 static String CommandString (CommandSet command_set, int command)
1342 switch (command_set) {
1344 cmd = ((CmdVM)command).ToString ();
1346 case CommandSet.OBJECT_REF:
1347 cmd = ((CmdObjectRef)command).ToString ();
1349 case CommandSet.STRING_REF:
1350 cmd = ((CmdStringRef)command).ToString ();
1352 case CommandSet.THREAD:
1353 cmd = ((CmdThread)command).ToString ();
1355 case CommandSet.ARRAY_REF:
1356 cmd = ((CmdArrayRef)command).ToString ();
1358 case CommandSet.EVENT_REQUEST:
1359 cmd = ((CmdEventRequest)command).ToString ();
1361 case CommandSet.STACK_FRAME:
1362 cmd = ((CmdStackFrame)command).ToString ();
1364 case CommandSet.APPDOMAIN:
1365 cmd = ((CmdAppDomain)command).ToString ();
1367 case CommandSet.ASSEMBLY:
1368 cmd = ((CmdAssembly)command).ToString ();
1370 case CommandSet.METHOD:
1371 cmd = ((CmdMethod)command).ToString ();
1373 case CommandSet.TYPE:
1374 cmd = ((CmdType)command).ToString ();
1376 case CommandSet.MODULE:
1377 cmd = ((CmdModule)command).ToString ();
1379 case CommandSet.FIELD:
1380 cmd = ((CmdField)command).ToString ();
1382 case CommandSet.EVENT:
1383 cmd = ((CmdEvent)command).ToString ();
1386 cmd = command.ToString ();
1389 return string.Format ("[{0} {1}]", command_set, cmd);
1392 long total_protocol_ticks;
1394 void LogPacket (int packet_id, byte[] encoded_packet, byte[] reply_packet, CommandSet command_set, int command, Stopwatch watch) {
1396 total_protocol_ticks += watch.ElapsedTicks;
1397 var ts = TimeSpan.FromTicks (total_protocol_ticks);
1398 string msg = string.Format ("Packet: {0} sent: {1} received: {2} ms: {3} total ms: {4} {5}",
1399 packet_id, encoded_packet.Length, reply_packet.Length, watch.ElapsedMilliseconds,
1400 (ts.Seconds * 1000) + ts.Milliseconds,
1401 CommandString (command_set, command));
1403 LoggingStream.WriteLine (msg);
1404 LoggingStream.Flush ();
1407 /* Send a request and call cb when a result is received */
1408 int Send (CommandSet command_set, int command, PacketWriter packet, Action<PacketReader> cb, int count) {
1409 int id = IdGenerator;
1411 Stopwatch watch = null;
1412 if (EnableConnectionLogging)
1413 watch = Stopwatch.StartNew ();
1415 byte[] encoded_packet;
1417 encoded_packet = EncodePacket (id, (int)command_set, command, null, 0);
1419 encoded_packet = EncodePacket (id, (int)command_set, command, packet.Data, packet.Offset);
1421 lock (reply_packets_monitor) {
1422 reply_cbs [id] = delegate (int packet_id, byte[] p) {
1423 if (EnableConnectionLogging)
1424 LogPacket (packet_id, encoded_packet, p, command_set, command, watch);
1425 /* Run the callback on a tp thread to avoid blocking the receive thread */
1426 PacketReader r = new PacketReader (p);
1427 cb.BeginInvoke (r, null, null);
1429 reply_cb_counts [id] = count;
1432 WritePacket (encoded_packet);
1437 PacketReader SendReceive (CommandSet command_set, int command, PacketWriter packet) {
1438 int id = IdGenerator;
1439 Stopwatch watch = null;
1442 throw new VMDisconnectedException ();
1444 if (EnableConnectionLogging)
1445 watch = Stopwatch.StartNew ();
1447 byte[] encoded_packet;
1450 encoded_packet = EncodePacket (id, (int)command_set, command, null, 0);
1452 encoded_packet = EncodePacket (id, (int)command_set, command, packet.Data, packet.Offset);
1454 WritePacket (encoded_packet);
1458 /* Wait for the reply packet */
1460 lock (reply_packets_monitor) {
1461 if (reply_packets.ContainsKey (packetId)) {
1462 byte[] reply = reply_packets [packetId];
1463 reply_packets.Remove (packetId);
1464 PacketReader r = new PacketReader (reply);
1466 if (EnableConnectionLogging)
1467 LogPacket (packetId, encoded_packet, reply, command_set, command, watch);
1468 if (r.ErrorCode != 0) {
1469 if (ErrorHandler != null)
1470 ErrorHandler (this, new ErrorHandlerEventArgs () { ErrorCode = (ErrorCode)r.ErrorCode });
1471 throw new NotImplementedException ("No error handler set.");
1477 throw new VMDisconnectedException ();
1478 Monitor.Wait (reply_packets_monitor);
1484 PacketReader SendReceive (CommandSet command_set, int command) {
1485 return SendReceive (command_set, command, null);
1488 int packet_id_generator;
1492 return Interlocked.Increment (ref packet_id_generator);
1496 CattrInfo[] ReadCattrs (PacketReader r) {
1497 CattrInfo[] res = new CattrInfo [r.ReadInt ()];
1498 for (int i = 0; i < res.Length; ++i) {
1499 CattrInfo info = new CattrInfo ();
1500 info.ctor_id = r.ReadId ();
1501 info.ctor_args = new ValueImpl [r.ReadInt ()];
1502 for (int j = 0; j < info.ctor_args.Length; ++j) {
1503 info.ctor_args [j] = r.ReadValue ();
1505 info.named_args = new CattrNamedArgInfo [r.ReadInt ()];
1506 for (int j = 0; j < info.named_args.Length; ++j) {
1507 CattrNamedArgInfo arg = new CattrNamedArgInfo ();
1508 int arg_type = r.ReadByte ();
1509 arg.is_property = arg_type == 0x54;
1510 arg.id = r.ReadId ();
1511 arg.value = r.ReadValue ();
1512 info.named_args [j] = arg;
1519 static ElementType TypeCodeToElementType (TypeCode c) {
1521 case TypeCode.Boolean:
1522 return ElementType.Boolean;
1524 return ElementType.Char;
1525 case TypeCode.SByte:
1526 return ElementType.I1;
1528 return ElementType.U1;
1529 case TypeCode.Int16:
1530 return ElementType.I2;
1531 case TypeCode.UInt16:
1532 return ElementType.U2;
1533 case TypeCode.Int32:
1534 return ElementType.I4;
1535 case TypeCode.UInt32:
1536 return ElementType.U4;
1537 case TypeCode.Int64:
1538 return ElementType.I8;
1539 case TypeCode.UInt64:
1540 return ElementType.U8;
1541 case TypeCode.Single:
1542 return ElementType.R4;
1543 case TypeCode.Double:
1544 return ElementType.R8;
1546 throw new NotImplementedException ();
1551 * Implementation of debugger commands
1554 internal VersionInfo VM_GetVersion () {
1555 var res = SendReceive (CommandSet.VM, (int)CmdVM.VERSION, null);
1556 VersionInfo info = new VersionInfo ();
1557 info.VMVersion = res.ReadString ();
1558 info.MajorVersion = res.ReadInt ();
1559 info.MinorVersion = res.ReadInt ();
1563 internal void VM_SetProtocolVersion (int major, int minor) {
1564 SendReceive (CommandSet.VM, (int)CmdVM.SET_PROTOCOL_VERSION, new PacketWriter ().WriteInt (major).WriteInt (minor));
1567 internal long[] VM_GetThreads () {
1568 var res = SendReceive (CommandSet.VM, (int)CmdVM.ALL_THREADS, null);
1569 int len = res.ReadInt ();
1570 long[] arr = new long [len];
1571 for (int i = 0; i < len; ++i)
1572 arr [i] = res.ReadId ();
1576 internal void VM_Suspend () {
1577 SendReceive (CommandSet.VM, (int)CmdVM.SUSPEND);
1580 internal void VM_Resume () {
1581 SendReceive (CommandSet.VM, (int)CmdVM.RESUME);
1584 internal void VM_Exit (int exitCode) {
1585 SendReceive (CommandSet.VM, (int)CmdVM.EXIT, new PacketWriter ().WriteInt (exitCode));
1588 internal void VM_Dispose () {
1589 SendReceive (CommandSet.VM, (int)CmdVM.DISPOSE);
1592 internal ValueImpl VM_InvokeMethod (long thread, long method, ValueImpl this_arg, ValueImpl[] arguments, InvokeFlags flags, out ValueImpl exc) {
1594 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));
1595 if (r.ReadByte () == 0) {
1596 exc = r.ReadValue ();
1599 return r.ReadValue ();
1603 internal delegate void InvokeMethodCallback (ValueImpl v, ValueImpl exc, ErrorCode error, object state);
1605 internal int VM_BeginInvokeMethod (long thread, long method, ValueImpl this_arg, ValueImpl[] arguments, InvokeFlags flags, InvokeMethodCallback callback, object state) {
1606 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) {
1609 if (r.ErrorCode != 0) {
1610 callback (null, null, (ErrorCode)r.ErrorCode, state);
1612 if (r.ReadByte () == 0) {
1613 exc = r.ReadValue ();
1620 callback (v, exc, 0, state);
1625 internal int VM_BeginInvokeMethods (long thread, long[] methods, ValueImpl this_arg, List<ValueImpl[]> arguments, InvokeFlags flags, InvokeMethodCallback callback, object state) {
1626 // FIXME: Merge this with INVOKE_METHOD
1627 var w = new PacketWriter ();
1629 w.WriteInt ((int)flags);
1630 w.WriteInt (methods.Length);
1631 for (int i = 0; i < methods.Length; ++i) {
1632 w.WriteId (methods [i]);
1633 w.WriteValue (this_arg);
1634 w.WriteInt (arguments [i].Length);
1635 w.WriteValues (arguments [i]);
1637 return Send (CommandSet.VM, (int)CmdVM.INVOKE_METHODS, w, delegate (PacketReader r) {
1640 if (r.ErrorCode != 0) {
1641 callback (null, null, (ErrorCode)r.ErrorCode, state);
1643 if (r.ReadByte () == 0) {
1644 exc = r.ReadValue ();
1651 callback (v, exc, 0, state);
1656 internal void VM_AbortInvoke (long thread, int id)
1658 SendReceive (CommandSet.VM, (int)CmdVM.ABORT_INVOKE, new PacketWriter ().WriteId (thread).WriteInt (id));
1661 internal void SetSocketTimeouts (int send_timeout, int receive_timeout, int keepalive_interval)
1663 TransportSetTimeouts (send_timeout, receive_timeout);
1664 SendReceive (CommandSet.VM, (int)CmdVM.SET_KEEPALIVE, new PacketWriter ().WriteId (keepalive_interval));
1667 internal long[] VM_GetTypesForSourceFile (string fname, bool ignoreCase) {
1668 var res = SendReceive (CommandSet.VM, (int)CmdVM.GET_TYPES_FOR_SOURCE_FILE, new PacketWriter ().WriteString (fname).WriteBool (ignoreCase));
1669 int count = res.ReadInt ();
1670 long[] types = new long [count];
1671 for (int i = 0; i < count; ++i)
1672 types [i] = res.ReadId ();
1676 internal long[] VM_GetTypes (string name, bool ignoreCase) {
1677 var res = SendReceive (CommandSet.VM, (int)CmdVM.GET_TYPES, new PacketWriter ().WriteString (name).WriteBool (ignoreCase));
1678 int count = res.ReadInt ();
1679 long[] types = new long [count];
1680 for (int i = 0; i < count; ++i)
1681 types [i] = res.ReadId ();
1689 internal long RootDomain {
1691 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_ROOT_DOMAIN, null).ReadId ();
1695 internal string Domain_GetName (long id) {
1696 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_FRIENDLY_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1699 internal long[] Domain_GetAssemblies (long id) {
1700 var res = SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_ASSEMBLIES, new PacketWriter ().WriteId (id));
1701 int count = res.ReadInt ();
1702 long[] assemblies = new long [count];
1703 for (int i = 0; i < count; ++i)
1704 assemblies [i] = res.ReadId ();
1708 internal long Domain_GetEntryAssembly (long id) {
1709 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_ENTRY_ASSEMBLY, new PacketWriter ().WriteId (id)).ReadId ();
1712 internal long Domain_GetCorlib (long id) {
1713 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_CORLIB, new PacketWriter ().WriteId (id)).ReadId ();
1716 internal long Domain_CreateString (long id, string s) {
1717 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.CREATE_STRING, new PacketWriter ().WriteId (id).WriteString (s)).ReadId ();
1720 internal long Domain_CreateBoxedValue (long id, long type_id, ValueImpl v) {
1721 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.CREATE_BOXED_VALUE, new PacketWriter ().WriteId (id).WriteId (type_id).WriteValue (v)).ReadId ();
1728 internal string Method_GetName (long id) {
1729 return SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1732 internal long Method_GetDeclaringType (long id) {
1733 return SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_DECLARING_TYPE, new PacketWriter ().WriteId (id)).ReadId ();
1736 internal DebugInfo Method_GetDebugInfo (long id) {
1737 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_DEBUG_INFO, new PacketWriter ().WriteId (id));
1739 DebugInfo info = new DebugInfo ();
1740 info.max_il_offset = res.ReadInt ();
1742 SourceInfo[] sources = null;
1743 if (Version.AtLeast (2, 13)) {
1744 int n = res.ReadInt ();
1745 sources = new SourceInfo [n];
1746 for (int i = 0; i < n; ++i) {
1747 sources [i].source_file = res.ReadString ();
1748 if (Version.AtLeast (2, 14)) {
1749 sources [i].hash = new byte [16];
1750 for (int j = 0; j < 16; ++j)
1751 sources [i].hash [j] = (byte)res.ReadByte ();
1755 sources = new SourceInfo [1];
1756 sources [0].source_file = res.ReadString ();
1759 int n_il_offsets = res.ReadInt ();
1760 info.il_offsets = new int [n_il_offsets];
1761 info.line_numbers = new int [n_il_offsets];
1762 info.source_files = new SourceInfo [n_il_offsets];
1763 info.column_numbers = new int [n_il_offsets];
1764 info.end_line_numbers = new int [n_il_offsets];
1765 info.end_column_numbers = new int [n_il_offsets];
1766 for (int i = 0; i < n_il_offsets; ++i) {
1767 info.il_offsets [i] = res.ReadInt ();
1768 info.line_numbers [i] = res.ReadInt ();
1769 if (Version.AtLeast (2, 12)) {
1770 int idx = res.ReadInt ();
1771 info.source_files [i] = idx >= 0 ? sources [idx] : default (SourceInfo);
1773 info.source_files [i] = sources [0];
1775 if (Version.AtLeast (2, 19))
1776 info.column_numbers [i] = res.ReadInt ();
1778 info.column_numbers [i] = 0;
1779 if (Version.AtLeast (2, 32)) {
1780 info.end_line_numbers [i] = res.ReadInt ();
1781 info.end_column_numbers [i] = res.ReadInt ();
1783 info.end_column_numbers [i] = -1;
1784 info.end_column_numbers [i] = -1;
1791 internal ParamInfo Method_GetParamInfo (long id) {
1792 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_PARAM_INFO, new PacketWriter ().WriteId (id));
1794 ParamInfo info = new ParamInfo ();
1795 info.call_conv = res.ReadInt ();
1796 info.param_count = res.ReadInt ();
1797 info.generic_param_count = res.ReadInt ();
1798 info.ret_type = res.ReadId ();
1799 info.param_types = new long [info.param_count];
1800 for (int i = 0; i < info.param_count; ++i)
1801 info.param_types [i] = res.ReadId ();
1802 info.param_names = new string [info.param_count];
1803 for (int i = 0; i < info.param_count; ++i)
1804 info.param_names [i] = res.ReadString ();
1809 internal LocalsInfo Method_GetLocalsInfo (long id) {
1810 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_LOCALS_INFO, new PacketWriter ().WriteId (id));
1812 LocalsInfo info = new LocalsInfo ();
1813 int nlocals = res.ReadInt ();
1814 info.types = new long [nlocals];
1815 for (int i = 0; i < nlocals; ++i)
1816 info.types [i] = res.ReadId ();
1817 info.names = new string [nlocals];
1818 for (int i = 0; i < nlocals; ++i)
1819 info.names [i] = res.ReadString ();
1820 info.live_range_start = new int [nlocals];
1821 info.live_range_end = new int [nlocals];
1822 for (int i = 0; i < nlocals; ++i) {
1823 info.live_range_start [i] = res.ReadInt ();
1824 info.live_range_end [i] = res.ReadInt ();
1830 internal MethodInfo Method_GetInfo (long id) {
1831 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_INFO, new PacketWriter ().WriteId (id));
1833 MethodInfo info = new MethodInfo ();
1834 info.attributes = res.ReadInt ();
1835 info.iattributes = res.ReadInt ();
1836 info.token = res.ReadInt ();
1837 if (Version.AtLeast (2, 12)) {
1838 int attrs = res.ReadByte ();
1839 if ((attrs & (1 << 0)) != 0)
1841 if ((attrs & (1 << 1)) != 0)
1842 info.is_generic_method = true;
1843 info.gmd = res.ReadId ();
1844 if (Version.AtLeast (2, 15)) {
1845 if (info.is_generic_method) {
1846 int n = res.ReadInt ();
1847 info.type_args = res.ReadIds (n);
1854 internal MethodBodyInfo Method_GetBody (long id) {
1855 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_BODY, new PacketWriter ().WriteId (id));
1857 MethodBodyInfo info = new MethodBodyInfo ();
1858 info.il = new byte [res.ReadInt ()];
1859 for (int i = 0; i < info.il.Length; ++i)
1860 info.il [i] = (byte)res.ReadByte ();
1862 if (Version.AtLeast (2, 18)) {
1863 info.clauses = new ExceptionClauseInfo [res.ReadInt ()];
1865 for (int i = 0; i < info.clauses.Length; ++i) {
1866 var clause = new ExceptionClauseInfo {
1867 flags = (ExceptionClauseFlags) res.ReadInt (),
1868 try_offset = res.ReadInt (),
1869 try_length = res.ReadInt (),
1870 handler_offset = res.ReadInt (),
1871 handler_length = res.ReadInt (),
1874 if (clause.flags == ExceptionClauseFlags.None)
1875 clause.catch_type_id = res.ReadId ();
1876 else if (clause.flags == ExceptionClauseFlags.Filter)
1877 clause.filter_offset = res.ReadInt ();
1879 info.clauses [i] = clause;
1882 info.clauses = new ExceptionClauseInfo [0];
1887 internal ResolvedToken Method_ResolveToken (long id, int token) {
1888 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.RESOLVE_TOKEN, new PacketWriter ().WriteId (id).WriteInt (token));
1890 TokenType type = (TokenType)res.ReadByte ();
1892 case TokenType.STRING:
1893 return new ResolvedToken () { Type = type, Str = res.ReadString () };
1894 case TokenType.TYPE:
1895 case TokenType.METHOD:
1896 case TokenType.FIELD:
1897 return new ResolvedToken () { Type = type, Id = res.ReadId () };
1898 case TokenType.UNKNOWN:
1899 return new ResolvedToken () { Type = type };
1901 throw new NotImplementedException ();
1905 internal CattrInfo[] Method_GetCustomAttributes (long id, long attr_type_id, bool inherit) {
1906 PacketReader r = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_CATTRS, new PacketWriter ().WriteId (id).WriteId (attr_type_id));
1907 return ReadCattrs (r);
1910 internal long Method_MakeGenericMethod (long id, long[] args) {
1911 PacketReader r = SendReceive (CommandSet.METHOD, (int)CmdMethod.MAKE_GENERIC_METHOD, new PacketWriter ().WriteId (id).WriteInt (args.Length).WriteIds (args));
1919 internal string Thread_GetName (long id) {
1920 return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1923 internal FrameInfo[] Thread_GetFrameInfo (long id, int start_frame, int length) {
1924 var res = SendReceive (CommandSet.THREAD, (int)CmdThread.GET_FRAME_INFO, new PacketWriter ().WriteId (id).WriteInt (start_frame).WriteInt (length));
1925 int count = res.ReadInt ();
1927 var frames = new FrameInfo [count];
1928 for (int i = 0; i < count; ++i) {
1929 var f = new FrameInfo ();
1930 f.id = res.ReadInt ();
1931 f.method = res.ReadId ();
1932 f.il_offset = res.ReadInt ();
1933 f.flags = (StackFrameFlags)res.ReadByte ();
1940 internal int Thread_GetState (long id) {
1941 return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_STATE, new PacketWriter ().WriteId (id)).ReadInt ();
1944 internal ThreadInfo Thread_GetInfo (long id) {
1945 PacketReader r = SendReceive (CommandSet.THREAD, (int)CmdThread.GET_INFO, new PacketWriter ().WriteId (id));
1947 ThreadInfo res = new ThreadInfo () { is_thread_pool = r.ReadByte () > 0 ? true : false };
1952 internal long Thread_GetId (long id) {
1953 return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_ID, new PacketWriter ().WriteId (id)).ReadLong ();
1956 internal long Thread_GetTID (long id) {
1957 return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_TID, new PacketWriter ().WriteId (id)).ReadLong ();
1960 internal void Thread_SetIP (long id, long method_id, long il_offset) {
1961 SendReceive (CommandSet.THREAD, (int)CmdThread.SET_IP, new PacketWriter ().WriteId (id).WriteId (method_id).WriteLong (il_offset));
1968 internal ModuleInfo Module_GetInfo (long id) {
1969 PacketReader r = SendReceive (CommandSet.MODULE, (int)CmdModule.GET_INFO, new PacketWriter ().WriteId (id));
1970 ModuleInfo info = new ModuleInfo { Name = r.ReadString (), ScopeName = r.ReadString (), FQName = r.ReadString (), Guid = r.ReadString (), Assembly = r.ReadId () };
1978 internal string Assembly_GetLocation (long id) {
1979 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_LOCATION, new PacketWriter ().WriteId (id)).ReadString ();
1982 internal long Assembly_GetEntryPoint (long id) {
1983 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_ENTRY_POINT, new PacketWriter ().WriteId (id)).ReadId ();
1986 internal long Assembly_GetManifestModule (long id) {
1987 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_MANIFEST_MODULE, new PacketWriter ().WriteId (id)).ReadId ();
1990 internal long Assembly_GetObject (long id) {
1991 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_OBJECT, new PacketWriter ().WriteId (id)).ReadId ();
1994 internal long Assembly_GetType (long id, string name, bool ignoreCase) {
1995 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_TYPE, new PacketWriter ().WriteId (id).WriteString (name).WriteBool (ignoreCase)).ReadId ();
1998 internal string Assembly_GetName (long id) {
1999 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_NAME, new PacketWriter ().WriteId (id)).ReadString ();
2006 internal TypeInfo Type_GetInfo (long id) {
2007 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_INFO, new PacketWriter ().WriteId (id));
2008 TypeInfo res = new TypeInfo ();
2010 res.ns = r.ReadString ();
2011 res.name = r.ReadString ();
2012 res.full_name = r.ReadString ();
2013 res.assembly = r.ReadId ();
2014 res.module = r.ReadId ();
2015 res.base_type = r.ReadId ();
2016 res.element_type = r.ReadId ();
2017 res.token = r.ReadInt ();
2018 res.rank = r.ReadByte ();
2019 res.attributes = r.ReadInt ();
2020 int b = r.ReadByte ();
2021 res.is_byref = (b & 1) != 0;
2022 res.is_pointer = (b & 2) != 0;
2023 res.is_primitive = (b & 4) != 0;
2024 res.is_valuetype = (b & 8) != 0;
2025 res.is_enum = (b & 16) != 0;
2026 res.is_gtd = (b & 32) != 0;
2027 res.is_generic_type = (b & 64) != 0;
2029 int nested_len = r.ReadInt ();
2030 res.nested = new long [nested_len];
2031 for (int i = 0; i < nested_len; ++i)
2032 res.nested [i] = r.ReadId ();
2034 if (Version.AtLeast (2, 12))
2035 res.gtd = r.ReadId ();
2036 if (Version.AtLeast (2, 15) && res.is_generic_type) {
2037 int n = r.ReadInt ();
2038 res.type_args = r.ReadIds (n);
2044 internal long[] Type_GetMethods (long id) {
2045 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_METHODS, new PacketWriter ().WriteId (id));
2047 int n = r.ReadInt ();
2048 long[] res = new long [n];
2049 for (int i = 0; i < n; ++i)
2050 res [i] = r.ReadId ();
2054 internal long[] Type_GetFields (long id, out string[] names, out long[] types, out int[] attrs) {
2055 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_FIELDS, new PacketWriter ().WriteId (id));
2057 int n = r.ReadInt ();
2058 long[] res = new long [n];
2059 names = new string [n];
2060 types = new long [n];
2061 attrs = new int [n];
2062 for (int i = 0; i < n; ++i) {
2063 res [i] = r.ReadId ();
2064 names [i] = r.ReadString ();
2065 types [i] = r.ReadId ();
2066 attrs [i] = r.ReadInt ();
2071 internal PropInfo[] Type_GetProperties (long id) {
2072 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_PROPERTIES, new PacketWriter ().WriteId (id));
2074 int n = r.ReadInt ();
2075 PropInfo[] res = new PropInfo [n];
2076 for (int i = 0; i < n; ++i) {
2077 res [i] = new PropInfo ();
2078 res [i].id = r.ReadId ();
2079 res [i].name = r.ReadString ();
2080 res [i].get_method = r.ReadId ();
2081 res [i].set_method = r.ReadId ();
2082 res [i].attrs = r.ReadInt ();
2088 internal long Type_GetObject (long id) {
2089 return SendReceive (CommandSet.TYPE, (int)CmdType.GET_OBJECT, new PacketWriter ().WriteId (id)).ReadId ();
2092 internal ValueImpl[] Type_GetValues (long id, long[] fields, long thread_id) {
2093 int len = fields.Length;
2096 r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_VALUES_2, new PacketWriter ().WriteId (id).WriteId (thread_id).WriteInt (len).WriteIds (fields));
2098 r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_VALUES, new PacketWriter ().WriteId (id).WriteInt (len).WriteIds (fields));
2100 ValueImpl[] res = new ValueImpl [len];
2101 for (int i = 0; i < len; ++i)
2102 res [i] = r.ReadValue ();
2106 internal void Type_SetValues (long id, long[] fields, ValueImpl[] values) {
2107 SendReceive (CommandSet.TYPE, (int)CmdType.SET_VALUES, new PacketWriter ().WriteId (id).WriteInt (fields.Length).WriteIds (fields).WriteValues (values));
2110 internal string[] Type_GetSourceFiles (long id, bool return_full_paths) {
2111 var r = SendReceive (CommandSet.TYPE, return_full_paths ? (int)CmdType.GET_SOURCE_FILES_2 : (int)CmdType.GET_SOURCE_FILES, new PacketWriter ().WriteId (id));
2112 int len = r.ReadInt ();
2113 string[] res = new string [len];
2114 for (int i = 0; i < len; ++i)
2115 res [i] = r.ReadString ();
2119 internal bool Type_IsAssignableFrom (long id, long c_id) {
2120 return SendReceive (CommandSet.TYPE, (int)CmdType.IS_ASSIGNABLE_FROM, new PacketWriter ().WriteId (id).WriteId (c_id)).ReadByte () > 0;
2123 internal CattrInfo[] Type_GetCustomAttributes (long id, long attr_type_id, bool inherit) {
2124 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_CATTRS, new PacketWriter ().WriteId (id).WriteId (attr_type_id));
2125 return ReadCattrs (r);
2128 internal CattrInfo[] Type_GetFieldCustomAttributes (long id, long field_id, long attr_type_id, bool inherit) {
2129 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_FIELD_CATTRS, new PacketWriter ().WriteId (id).WriteId (field_id).WriteId (attr_type_id));
2130 return ReadCattrs (r);
2133 internal CattrInfo[] Type_GetPropertyCustomAttributes (long id, long field_id, long attr_type_id, bool inherit) {
2134 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_PROPERTY_CATTRS, new PacketWriter ().WriteId (id).WriteId (field_id).WriteId (attr_type_id));
2135 return ReadCattrs (r);
2138 public long[] Type_GetMethodsByNameFlags (long id, string name, int flags, bool ignoreCase) {
2139 flags |= ignoreCase ? (int)BindingFlagsExtensions.BINDING_FLAGS_IGNORE_CASE : 0;
2140 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.CMD_TYPE_GET_METHODS_BY_NAME_FLAGS, new PacketWriter ().WriteId (id).WriteString (name).WriteInt (flags));
2141 int len = r.ReadInt ();
2142 long[] res = new long [len];
2143 for (int i = 0; i < len; ++i)
2144 res [i] = r.ReadId ();
2148 internal long[] Type_GetInterfaces (long id) {
2149 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_INTERFACES, new PacketWriter ().WriteId (id));
2150 int len = r.ReadInt ();
2151 return r.ReadIds (len);
2154 internal IfaceMapInfo[] Type_GetInterfaceMap (long id, long[] ids) {
2155 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_INTERFACE_MAP, new PacketWriter ().WriteId (id).WriteInt (ids.Length).WriteIds (ids));
2156 var res = new IfaceMapInfo [ids.Length];
2157 for (int i = 0; i < ids.Length; ++i) {
2158 int n = r.ReadInt ();
2160 res [i].iface_id = ids [i];
2161 res [i].iface_methods = r.ReadIds (n);
2162 res [i].target_methods = r.ReadIds (n);
2168 internal bool Type_IsInitialized (long id) {
2169 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.IS_INITIALIZED, new PacketWriter ().WriteId (id));
2170 return r.ReadInt () == 1;
2173 internal long Type_CreateInstance (long id) {
2174 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.CREATE_INSTANCE, new PacketWriter ().WriteId (id));
2182 internal FieldMirrorInfo Field_GetInfo (long id) {
2183 PacketReader r = SendReceive (CommandSet.FIELD, (int)CmdField.GET_INFO, new PacketWriter ().WriteId (id));
2184 FieldMirrorInfo info = new FieldMirrorInfo { Name = r.ReadString (), Parent = r.ReadId (), TypeId = r.ReadId (), Attrs = r.ReadInt () };
2192 internal int EnableEvent (EventType etype, SuspendPolicy suspend_policy, List<Modifier> mods) {
2193 var w = new PacketWriter ().WriteByte ((byte)etype).WriteByte ((byte)suspend_policy);
2195 if (mods.Count > 255)
2196 throw new NotImplementedException ();
2197 w.WriteByte ((byte)mods.Count);
2198 foreach (Modifier mod in mods) {
2199 if (mod is CountModifier) {
2200 w.WriteByte ((byte)ModifierKind.COUNT);
2201 w.WriteInt ((mod as CountModifier).Count);
2202 } else if (mod is LocationModifier) {
2203 w.WriteByte ((byte)ModifierKind.LOCATION_ONLY);
2204 w.WriteId ((mod as LocationModifier).Method);
2205 w.WriteLong ((mod as LocationModifier).Location);
2206 } else if (mod is StepModifier) {
2207 w.WriteByte ((byte)ModifierKind.STEP);
2208 w.WriteId ((mod as StepModifier).Thread);
2209 w.WriteInt ((mod as StepModifier).Size);
2210 w.WriteInt ((mod as StepModifier).Depth);
2211 if (Version.AtLeast (2, 16))
2212 w.WriteInt ((mod as StepModifier).Filter);
2213 } else if (mod is ThreadModifier) {
2214 w.WriteByte ((byte)ModifierKind.THREAD_ONLY);
2215 w.WriteId ((mod as ThreadModifier).Thread);
2216 } else if (mod is ExceptionModifier) {
2217 var em = mod as ExceptionModifier;
2218 w.WriteByte ((byte)ModifierKind.EXCEPTION_ONLY);
2219 w.WriteId (em.Type);
2220 if (Version.MajorVersion > 2 || Version.MinorVersion > 0) {
2221 /* This is only supported in protocol version 2.1 */
2222 w.WriteBool (em.Caught);
2223 w.WriteBool (em.Uncaught);
2224 } else if (!em.Caught || !em.Uncaught) {
2225 throw new NotSupportedException ("This request is not supported by the protocol version implemented by the debuggee.");
2227 if (Version.MajorVersion > 2 || Version.MinorVersion > 24) {
2228 w.WriteBool (em.Subclasses);
2229 } else if (!em.Subclasses) {
2230 throw new NotSupportedException ("This request is not supported by the protocol version implemented by the debuggee.");
2232 } else if (mod is AssemblyModifier) {
2233 w.WriteByte ((byte)ModifierKind.ASSEMBLY_ONLY);
2234 var amod = (mod as AssemblyModifier);
2235 w.WriteInt (amod.Assemblies.Length);
2236 foreach (var id in amod.Assemblies)
2238 } else if (mod is SourceFileModifier) {
2239 w.WriteByte ((byte)ModifierKind.SOURCE_FILE_ONLY);
2240 var smod = (mod as SourceFileModifier);
2241 w.WriteInt (smod.SourceFiles.Length);
2242 foreach (var s in smod.SourceFiles)
2244 } else if (mod is TypeNameModifier) {
2245 w.WriteByte ((byte)ModifierKind.TYPE_NAME_ONLY);
2246 var tmod = (mod as TypeNameModifier);
2247 w.WriteInt (tmod.TypeNames.Length);
2248 foreach (var s in tmod.TypeNames)
2251 throw new NotImplementedException ();
2257 return SendReceive (CommandSet.EVENT_REQUEST, (int)CmdEventRequest.SET, w).ReadInt ();
2260 internal void ClearEventRequest (EventType etype, int req_id) {
2261 SendReceive (CommandSet.EVENT_REQUEST, (int)CmdEventRequest.CLEAR, new PacketWriter ().WriteByte ((byte)etype).WriteInt (req_id));
2264 internal void ClearAllBreakpoints () {
2265 SendReceive (CommandSet.EVENT_REQUEST, (int)CmdEventRequest.CLEAR_ALL_BREAKPOINTS, new PacketWriter ());
2271 internal ValueImpl StackFrame_GetThis (long thread_id, long id) {
2272 PacketReader r = SendReceive (CommandSet.STACK_FRAME, (int)CmdStackFrame.GET_THIS, new PacketWriter ().WriteId (thread_id).WriteId (id));
2273 return r.ReadValue ();
2276 internal ValueImpl[] StackFrame_GetValues (long thread_id, long id, int[] pos) {
2277 /* pos < 0 -> argument at pos (-pos) - 1 */
2278 /* pos >= 0 -> local at pos */
2279 int len = pos.Length;
2280 PacketReader r = SendReceive (CommandSet.STACK_FRAME, (int)CmdStackFrame.GET_VALUES, new PacketWriter ().WriteId (thread_id).WriteId (id).WriteInt (len).WriteInts (pos));
2282 ValueImpl[] res = new ValueImpl [len];
2283 for (int i = 0; i < len; ++i)
2284 res [i] = r.ReadValue ();
2288 internal void StackFrame_SetValues (long thread_id, long id, int[] pos, ValueImpl[] values) {
2289 /* pos < 0 -> argument at pos (-pos) - 1 */
2290 /* pos >= 0 -> local at pos */
2291 int len = pos.Length;
2292 SendReceive (CommandSet.STACK_FRAME, (int)CmdStackFrame.SET_VALUES, new PacketWriter ().WriteId (thread_id).WriteId (id).WriteInt (len).WriteInts (pos).WriteValues (values));
2298 internal int[] Array_GetLength (long id, out int rank, out int[] lower_bounds) {
2299 var r = SendReceive (CommandSet.ARRAY_REF, (int)CmdArrayRef.GET_LENGTH, new PacketWriter ().WriteId (id));
2300 rank = r.ReadInt ();
2301 int[] res = new int [rank];
2302 lower_bounds = new int [rank];
2303 for (int i = 0; i < rank; ++i) {
2304 res [i] = r.ReadInt ();
2305 lower_bounds [i] = r.ReadInt ();
2310 internal ValueImpl[] Array_GetValues (long id, int index, int len) {
2311 var r = SendReceive (CommandSet.ARRAY_REF, (int)CmdArrayRef.GET_VALUES, new PacketWriter ().WriteId (id).WriteInt (index).WriteInt (len));
2312 ValueImpl[] res = new ValueImpl [len];
2313 for (int i = 0; i < len; ++i)
2314 res [i] = r.ReadValue ();
2318 internal void Array_SetValues (long id, int index, ValueImpl[] values) {
2319 SendReceive (CommandSet.ARRAY_REF, (int)CmdArrayRef.SET_VALUES, new PacketWriter ().WriteId (id).WriteInt (index).WriteInt (values.Length).WriteValues (values));
2325 internal string String_GetValue (long id) {
2326 return SendReceive (CommandSet.STRING_REF, (int)CmdStringRef.GET_VALUE, new PacketWriter ().WriteId (id)).ReadString ();
2329 internal int String_GetLength (long id) {
2330 return (int)SendReceive (CommandSet.STRING_REF, (int)CmdStringRef.GET_LENGTH, new PacketWriter ().WriteId (id)).ReadLong ();
2333 internal char[] String_GetChars (long id, int index, int length) {
2334 var r = SendReceive (CommandSet.STRING_REF, (int)CmdStringRef.GET_CHARS, new PacketWriter ().WriteId (id).WriteLong (index).WriteLong (length));
2335 var res = new char [length];
2336 for (int i = 0; i < length; ++i)
2337 res [i] = (char)r.ReadShort ();
2344 internal long Object_GetType (long id) {
2345 return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_TYPE, new PacketWriter ().WriteId (id)).ReadId ();
2348 internal long Object_GetDomain (long id) {
2349 return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_DOMAIN, new PacketWriter ().WriteId (id)).ReadId ();
2352 internal ValueImpl[] Object_GetValues (long id, long[] fields) {
2353 int len = fields.Length;
2354 PacketReader r = SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_VALUES, new PacketWriter ().WriteId (id).WriteInt (len).WriteIds (fields));
2356 ValueImpl[] res = new ValueImpl [len];
2357 for (int i = 0; i < len; ++i)
2358 res [i] = r.ReadValue ();
2362 internal void Object_SetValues (long id, long[] fields, ValueImpl[] values) {
2363 SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.SET_VALUES, new PacketWriter ().WriteId (id).WriteInt (fields.Length).WriteIds (fields).WriteValues (values));
2366 internal bool Object_IsCollected (long id) {
2367 return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.IS_COLLECTED, new PacketWriter ().WriteId (id)).ReadInt () == 1;
2370 internal long Object_GetAddress (long id) {
2371 return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_ADDRESS, new PacketWriter ().WriteId (id)).ReadLong ();
2374 internal ObjectRefInfo Object_GetInfo (long id) {
2375 ObjectRefInfo res = new ObjectRefInfo ();
2376 PacketReader r = SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_INFO, new PacketWriter ().WriteId (id));
2378 res.type_id = r.ReadId ();
2379 res.domain_id = r.ReadId ();
2383 public void ForceDisconnect ()
2386 disconnected = true;
2391 class TcpConnection : Connection
2395 internal TcpConnection (Socket socket)
2397 this.socket = socket;
2398 //socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.NoDelay, 1);
2401 internal EndPoint EndPoint {
2403 return socket.RemoteEndPoint;
2407 protected override int TransportSend (byte[] buf, int buf_offset, int len)
2409 return socket.Send (buf, buf_offset, len, SocketFlags.None);
2412 protected override int TransportReceive (byte[] buf, int buf_offset, int len)
2414 return socket.Receive (buf, buf_offset, len, SocketFlags.None);
2417 protected override void TransportSetTimeouts (int send_timeout, int receive_timeout)
2419 socket.SendTimeout = send_timeout;
2420 socket.ReceiveTimeout = receive_timeout;
2423 protected override void TransportClose ()
2429 /* This is the interface exposed by the debugger towards the debugger agent */
2430 interface IEventHandler
2432 void Events (SuspendPolicy suspend_policy, EventInfo[] events);
2434 void VMDisconnect (int req_id, long thread_id, string vm_uri);