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 SourceInfo[] source_files;
53 public StackFrameFlags flags;
57 public string ns, name, full_name;
58 public long assembly, module, base_type, element_type;
59 public int token, rank, attributes;
60 public bool is_byref, is_pointer, is_primitive, is_valuetype, is_enum;
61 public bool is_gtd, is_generic_type;
64 public long[] type_args;
69 public long[] iface_methods;
70 public long[] target_methods;
74 public int attributes, iattributes, token;
75 public bool is_gmd, is_generic_method;
77 public long[] type_args;
80 class MethodBodyInfo {
86 public int param_count;
87 public int generic_param_count;
89 public long[] param_types;
90 public string[] param_names;
95 public string[] names;
96 public int[] live_range_start;
97 public int[] live_range_end;
103 public long get_method, set_method;
107 class CattrNamedArgInfo {
108 public bool is_property;
110 public ValueImpl value;
115 public ValueImpl[] ctor_args;
116 public CattrNamedArgInfo[] named_args;
120 public bool is_thread_pool;
123 struct ObjectRefInfo {
125 public long domain_id;
129 VALUE_TYPE_ID_NULL = 0xf0,
130 VALUE_TYPE_ID_TYPE = 0xf1
135 DISABLE_BREAKPOINTS = 0x1,
136 SINGLE_THREADED = 0x2
182 public ElementType Type; /* or one of the VALUE_TYPE_ID constants */
185 public long Klass; // For ElementType.ValueType
186 public ValueImpl[] Fields; // for ElementType.ValueType
187 public bool IsEnum; // For ElementType.ValueType
188 public long Id; /* For VALUE_TYPE_ID_TYPE */
192 public string Name, ScopeName, FQName, Guid;
193 public long Assembly;
204 enum StackFrameFlags {
207 NATIVE_TRANSITION = 2
210 class ResolvedToken {
211 public TokenType Type;
219 class CountModifier : Modifier {
225 class LocationModifier : Modifier {
230 public long Location {
235 class StepModifier : Modifier {
253 class ThreadModifier : Modifier {
259 class ExceptionModifier : Modifier {
266 public bool Uncaught {
271 class AssemblyModifier : Modifier {
272 public long[] Assemblies {
277 class SourceFileModifier : Modifier {
278 public string[] SourceFiles {
283 class TypeNameModifier : Modifier {
284 public string[] TypeNames {
290 public EventType EventType {
298 public SuspendPolicy SuspendPolicy {
302 public long ThreadId {
310 public long Location {
318 public string Category {
322 public string Message {
326 public EventInfo (EventType type, int req_id) {
332 public enum ErrorCode {
335 INVALID_FIELDID = 25,
336 INVALID_FRAMEID = 30,
337 NOT_IMPLEMENTED = 100,
339 INVALID_ARGUMENT = 102,
341 ERR_NO_INVOCATION = 104,
342 ABSENT_INFORMATION = 105,
343 NO_SEQ_POINT_AT_IL_OFFSET = 106
346 public class ErrorHandlerEventArgs : EventArgs {
348 public ErrorCode ErrorCode {
354 * Represents the connection to the debuggee
356 public abstract class Connection : IDisposable
359 * The protocol and the packet format is based on JDWP, the differences
360 * are in the set of supported events, and the commands.
362 internal const string HANDSHAKE_STRING = "DWP-Handshake";
364 internal const int HEADER_LENGTH = 11;
366 static readonly bool EnableConnectionLogging = !String.IsNullOrEmpty (Environment.GetEnvironmentVariable ("MONO_SDB_LOG"));
367 static int ConnectionId;
368 readonly StreamWriter LoggingStream = EnableConnectionLogging ?
369 new StreamWriter (string.Format ("/tmp/sdb_conn_log_{0}", ConnectionId++), false) : null;
372 * Th version of the wire-protocol implemented by the library. The library
373 * and the debuggee can communicate if they implement the same major version.
374 * If they implement a different minor version, they can communicate, but some
375 * features might not be available. This allows older clients to communicate
376 * with newer runtimes, and vice versa.
378 internal const int MAJOR_VERSION = 2;
379 internal const int MINOR_VERSION = 17;
381 enum WPSuspendPolicy {
408 APPDOMAIN_CREATE = 4, // Not in JDI
409 APPDOMAIN_UNLOAD = 5, // Not in JDI
430 SOURCE_FILE_ONLY = 12,
442 SET_PROTOCOL_VERSION = 8,
445 GET_TYPES_FOR_SOURCE_FILE = 11,
458 /* FIXME: Merge into GET_INFO when the major protocol version is increased */
464 enum CmdEventRequest {
467 CLEAR_ALL_BREAKPOINTS = 3
472 GET_FRIENDLY_NAME = 2,
474 GET_ENTRY_ASSEMBLY = 4,
477 CREATE_BOXED_VALUE = 7
483 GET_MANIFEST_MODULE = 3,
495 GET_DECLARING_TYPE = 2,
510 GET_SOURCE_FILES = 6,
512 IS_ASSIGNABLE_FROM = 8,
515 GET_FIELD_CATTRS = 11,
516 GET_PROPERTY_CATTRS = 12,
517 /* FIXME: Merge into GET_SOURCE_FILES when the major protocol version is increased */
518 GET_SOURCE_FILES_2 = 13,
519 /* FIXME: Merge into GET_VALUES when the major protocol version is increased */
521 CMD_TYPE_GET_METHODS_BY_NAME_FLAGS = 15,
523 GET_INTERFACE_MAP = 17
526 enum BindingFlagsExtensions {
527 BINDING_FLAGS_IGNORE_CASE = 0x70000000,
560 public int command_set;
565 internal static int GetPacketLength (byte[] header) {
567 return decode_int (header, ref offset);
570 internal static bool IsReplyPacket (byte[] packet) {
572 return decode_byte (packet, ref offset) == 0x80;
575 internal static int GetPacketId (byte[] packet) {
577 return decode_int (packet, ref offset);
580 static int decode_byte (byte[] packet, ref int offset) {
581 return packet [offset++];
584 static int decode_short (byte[] packet, ref int offset) {
585 int res = ((int)packet [offset] << 8) | (int)packet [offset + 1];
590 static int decode_int (byte[] packet, ref int offset) {
591 int res = ((int)packet [offset] << 24) | ((int)packet [offset + 1] << 16) | ((int)packet [offset + 2] << 8) | (int)packet [offset + 3];
596 static long decode_id (byte[] packet, ref int offset) {
597 return decode_int (packet, ref offset);
600 static long decode_long (byte[] packet, ref int offset) {
601 uint high = (uint)decode_int (packet, ref offset);
602 uint low = (uint)decode_int (packet, ref offset);
604 return (long)(((ulong)high << 32) | (ulong)low);
607 internal static SuspendPolicy decode_suspend_policy (int suspend_policy) {
608 switch ((WPSuspendPolicy)suspend_policy) {
609 case WPSuspendPolicy.NONE:
610 return SuspendPolicy.None;
611 case WPSuspendPolicy.EVENT_THREAD:
612 return SuspendPolicy.EventThread;
613 case WPSuspendPolicy.ALL:
614 return SuspendPolicy.All;
616 throw new NotImplementedException ();
620 static Header decode_command_header (byte[] packet) {
622 Header res = new Header ();
624 decode_int (packet, ref offset);
625 res.id = decode_int (packet, ref offset);
626 res.flags = decode_byte (packet, ref offset);
627 res.command_set = decode_byte (packet, ref offset);
628 res.command = decode_byte (packet, ref offset);
633 static void encode_byte (byte[] buf, int b, ref int offset) {
634 buf [offset] = (byte)b;
638 static void encode_int (byte[] buf, int i, ref int offset) {
639 buf [offset] = (byte)((i >> 24) & 0xff);
640 buf [offset + 1] = (byte)((i >> 16) & 0xff);
641 buf [offset + 2] = (byte)((i >> 8) & 0xff);
642 buf [offset + 3] = (byte)((i >> 0) & 0xff);
646 static void encode_id (byte[] buf, long id, ref int offset) {
647 encode_int (buf, (int)id, ref offset);
650 static void encode_long (byte[] buf, long l, ref int offset) {
651 encode_int (buf, (int)((l >> 32) & 0xffffffff), ref offset);
652 encode_int (buf, (int)(l & 0xffffffff), ref offset);
655 internal static byte[] EncodePacket (int id, int commandSet, int command, byte[] data, int dataLen) {
656 byte[] buf = new byte [dataLen + 11];
659 encode_int (buf, buf.Length, ref offset);
660 encode_int (buf, id, ref offset);
661 encode_byte (buf, 0, ref offset);
662 encode_byte (buf, commandSet, ref offset);
663 encode_byte (buf, command, ref offset);
665 for (int i = 0; i < dataLen; ++i)
666 buf [offset + i] = data [i];
675 public PacketReader (byte[] packet) {
676 this.packet = packet;
679 Header header = decode_command_header (packet);
680 CommandSet = (CommandSet)header.command_set;
681 Command = header.command;
685 ReadInt (); // length
687 ReadByte (); // flags
688 ErrorCode = ReadShort ();
691 public CommandSet CommandSet {
699 public int ErrorCode {
709 public int ReadByte () {
710 return decode_byte (packet, ref offset);
713 public int ReadShort () {
714 return decode_short (packet, ref offset);
717 public int ReadInt () {
718 return decode_int (packet, ref offset);
721 public long ReadId () {
722 return decode_id (packet, ref offset);
725 public long ReadLong () {
726 return decode_long (packet, ref offset);
729 public float ReadFloat () {
730 float f = DataConverter.FloatFromBE (packet, offset);
735 public double ReadDouble () {
736 double d = DataConverter.DoubleFromBE (packet, offset);
741 public string ReadString () {
742 int len = decode_int (packet, ref offset);
743 string res = new String (Encoding.UTF8.GetChars (packet, offset, len));
748 public ValueImpl ReadValue () {
749 ElementType etype = (ElementType)ReadByte ();
752 case ElementType.Void:
753 return new ValueImpl { Type = etype };
755 return new ValueImpl { Type = etype, Value = (sbyte)ReadInt () };
757 return new ValueImpl { Type = etype, Value = (byte)ReadInt () };
758 case ElementType.Boolean:
759 return new ValueImpl { Type = etype, Value = ReadInt () != 0 };
761 return new ValueImpl { Type = etype, Value = (short)ReadInt () };
763 return new ValueImpl { Type = etype, Value = (ushort)ReadInt () };
764 case ElementType.Char:
765 return new ValueImpl { Type = etype, Value = (char)ReadInt () };
767 return new ValueImpl { Type = etype, Value = ReadInt () };
769 return new ValueImpl { Type = etype, Value = (uint)ReadInt () };
771 return new ValueImpl { Type = etype, Value = ReadLong () };
773 return new ValueImpl { Type = etype, Value = (ulong)ReadLong () };
775 return new ValueImpl { Type = etype, Value = ReadFloat () };
777 return new ValueImpl { Type = etype, Value = ReadDouble () };
780 case ElementType.Ptr:
781 // FIXME: The client and the debuggee might have different word sizes
782 return new ValueImpl { Type = etype, Value = ReadLong () };
783 case ElementType.String:
784 case ElementType.SzArray:
785 case ElementType.Class:
786 case ElementType.Array:
787 case ElementType.Object:
788 long objid = ReadId ();
789 return new ValueImpl () { Type = etype, Objid = objid };
790 case ElementType.ValueType:
791 bool is_enum = ReadByte () == 1;
792 long klass = ReadId ();
793 long nfields = ReadInt ();
794 ValueImpl[] fields = new ValueImpl [nfields];
795 for (int i = 0; i < nfields; ++i)
796 fields [i] = ReadValue ();
797 return new ValueImpl () { Type = etype, Klass = klass, Fields = fields, IsEnum = is_enum };
798 case (ElementType)ValueTypeId.VALUE_TYPE_ID_NULL:
799 return new ValueImpl { Type = etype };
800 case (ElementType)ValueTypeId.VALUE_TYPE_ID_TYPE:
801 return new ValueImpl () { Type = etype, Id = ReadId () };
803 throw new NotImplementedException ("Unable to handle type " + etype);
807 public long[] ReadIds (int n) {
808 long[] res = new long [n];
809 for (int i = 0; i < n; ++i)
820 public PacketWriter () {
821 data = new byte [1024];
825 void MakeRoom (int size) {
826 if (offset + size >= data.Length) {
827 int new_len = data.Length * 2;
828 while (new_len < offset + size) {
831 byte[] new_data = new byte [new_len];
832 Array.Copy (data, new_data, data.Length);
837 public PacketWriter WriteByte (byte val) {
839 encode_byte (data, val, ref offset);
843 public PacketWriter WriteInt (int val) {
845 encode_int (data, val, ref offset);
849 public PacketWriter WriteId (long id) {
851 encode_id (data, id, ref offset);
855 public PacketWriter WriteLong (long val) {
857 encode_long (data, val, ref offset);
861 public PacketWriter WriteFloat (float f) {
863 byte[] b = DataConverter.GetBytesBE (f);
864 for (int i = 0; i < 4; ++i)
865 data [offset + i] = b [i];
870 public PacketWriter WriteDouble (double d) {
872 byte[] b = DataConverter.GetBytesBE (d);
873 for (int i = 0; i < 8; ++i)
874 data [offset + i] = b [i];
879 public PacketWriter WriteInts (int[] ids) {
880 for (int i = 0; i < ids.Length; ++i)
885 public PacketWriter WriteIds (long[] ids) {
886 for (int i = 0; i < ids.Length; ++i)
891 public PacketWriter WriteString (string s) {
893 return WriteInt (-1);
895 byte[] b = Encoding.UTF8.GetBytes (s);
897 encode_int (data, b.Length, ref offset);
899 Buffer.BlockCopy (b, 0, data, offset, b.Length);
904 public PacketWriter WriteBool (bool val) {
905 WriteByte (val ? (byte)1 : (byte)0);
909 public PacketWriter WriteValue (ValueImpl v) {
913 t = TypeCodeToElementType (Type.GetTypeCode (v.Value.GetType ()));
918 case ElementType.Boolean:
919 WriteInt ((bool)v.Value ? 1 : 0);
921 case ElementType.Char:
922 WriteInt ((int)(char)v.Value);
925 WriteInt ((int)(sbyte)v.Value);
928 WriteInt ((int)(byte)v.Value);
931 WriteInt ((int)(short)v.Value);
934 WriteInt ((int)(ushort)v.Value);
937 WriteInt ((int)(int)v.Value);
940 WriteInt ((int)(uint)v.Value);
943 WriteLong ((long)(long)v.Value);
946 WriteLong ((long)(ulong)v.Value);
949 WriteFloat ((float)v.Value);
952 WriteDouble ((double)v.Value);
954 case ElementType.String:
955 case ElementType.SzArray:
956 case ElementType.Class:
957 case ElementType.Array:
958 case ElementType.Object:
961 case ElementType.ValueType:
964 throw new NotImplementedException ();
967 WriteInt (v.Fields.Length);
968 for (int i = 0; i < v.Fields.Length; ++i)
969 WriteValue (v.Fields [i]);
971 case (ElementType)ValueTypeId.VALUE_TYPE_ID_NULL:
974 throw new NotImplementedException ();
980 public PacketWriter WriteValues (ValueImpl[] values) {
981 for (int i = 0; i < values.Length; ++i)
982 WriteValue (values [i]);
999 delegate void ReplyCallback (int packet_id, byte[] packet);
1002 Thread receiver_thread;
1003 Dictionary<int, byte[]> reply_packets;
1004 Dictionary<int, ReplyCallback> reply_cbs;
1005 object reply_packets_monitor;
1007 internal event EventHandler<ErrorHandlerEventArgs> ErrorHandler;
1009 protected Connection () {
1011 reply_packets = new Dictionary<int, byte[]> ();
1012 reply_cbs = new Dictionary<int, ReplyCallback> ();
1013 reply_packets_monitor = new Object ();
1016 protected abstract int TransportReceive (byte[] buf, int buf_offset, int len);
1017 protected abstract int TransportSend (byte[] buf, int buf_offset, int len);
1018 protected abstract void TransportSetTimeouts (int send_timeout, int receive_timeout);
1019 protected abstract void TransportClose ();
1021 internal VersionInfo Version;
1023 int Receive (byte[] buf, int buf_offset, int len) {
1026 while (offset < len) {
1027 int n = TransportReceive (buf, buf_offset + offset, len - offset);
1037 // Do the wire protocol handshake
1038 internal void Connect () {
1039 byte[] buf = new byte [HANDSHAKE_STRING.Length];
1040 char[] cbuf = new char [buf.Length];
1042 // FIXME: Add a timeout
1043 int n = Receive (buf, 0, buf.Length);
1045 throw new IOException ("DWP Handshake failed.");
1046 for (int i = 0; i < buf.Length; ++i)
1047 cbuf [i] = (char)buf [i];
1049 if (new String (cbuf) != HANDSHAKE_STRING)
1050 throw new IOException ("DWP Handshake failed.");
1052 TransportSend (buf, 0, buf.Length);
1054 receiver_thread = new Thread (new ThreadStart (receiver_thread_main));
1055 receiver_thread.Start ();
1057 Version = VM_GetVersion ();
1060 // Tell the debuggee our protocol version, so newer debuggees can work
1061 // with older clients
1065 // Older debuggees might not support this request
1066 EventHandler<ErrorHandlerEventArgs> OrigErrorHandler = ErrorHandler;
1067 ErrorHandler = null;
1068 ErrorHandler += delegate (object sender, ErrorHandlerEventArgs args) {
1069 throw new NotSupportedException ();
1072 VM_SetProtocolVersion (MAJOR_VERSION, MINOR_VERSION);
1073 } catch (NotSupportedException) {
1075 ErrorHandler = OrigErrorHandler;
1078 internal byte[] ReadPacket () {
1079 // FIXME: Throw ClosedConnectionException () if the connection is closed
1080 // FIXME: Throw ClosedConnectionException () if another thread closes the connection
1082 byte[] header = new byte [HEADER_LENGTH];
1084 int len = Receive (header, 0, header.Length);
1086 return new byte [0];
1087 if (len != HEADER_LENGTH) {
1089 throw new IOException ("Packet of length " + len + " is read.");
1092 int packetLength = GetPacketLength (header);
1093 if (packetLength < 11)
1094 throw new IOException ("Invalid packet length.");
1096 if (packetLength == 11) {
1099 byte[] buf = new byte [packetLength];
1100 for (int i = 0; i < header.Length; ++i)
1101 buf [i] = header [i];
1102 len = Receive (buf, header.Length, packetLength - header.Length);
1103 if (len != packetLength - header.Length)
1104 throw new IOException ();
1109 internal void WritePacket (byte[] packet) {
1110 // FIXME: Throw ClosedConnectionException () if the connection is closed
1111 // FIXME: Throw ClosedConnectionException () if another thread closes the connection
1113 TransportSend (packet, 0, packet.Length);
1116 internal void Close () {
1120 internal bool IsClosed {
1128 void receiver_thread_main () {
1131 bool res = ReceivePacket ();
1134 } catch (Exception ex) {
1135 Console.WriteLine (ex);
1140 lock (reply_packets_monitor) {
1141 disconnected = true;
1142 Monitor.PulseAll (reply_packets_monitor);
1145 EventHandler.VMDisconnect (0, 0, null);
1148 bool ReceivePacket () {
1149 byte[] packet = ReadPacket ();
1151 if (packet.Length == 0) {
1155 if (IsReplyPacket (packet)) {
1156 int id = GetPacketId (packet);
1157 ReplyCallback cb = null;
1158 lock (reply_packets_monitor) {
1159 reply_cbs.TryGetValue (id, out cb);
1161 reply_packets [id] = packet;
1162 Monitor.PulseAll (reply_packets_monitor);
1167 cb.Invoke (id, packet);
1169 PacketReader r = new PacketReader (packet);
1171 if (r.CommandSet == CommandSet.EVENT && r.Command == (int)CmdEvent.COMPOSITE) {
1172 int spolicy = r.ReadByte ();
1173 int nevents = r.ReadInt ();
1175 SuspendPolicy suspend_policy = decode_suspend_policy (spolicy);
1177 EventInfo[] events = new EventInfo [nevents];
1179 for (int i = 0; i < nevents; ++i) {
1180 EventKind kind = (EventKind)r.ReadByte ();
1181 int req_id = r.ReadInt ();
1183 EventType etype = (EventType)kind;
1185 if (kind == EventKind.VM_START) {
1186 long thread_id = r.ReadId ();
1187 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id };
1188 //EventHandler.VMStart (req_id, thread_id, null);
1189 } else if (kind == EventKind.VM_DEATH) {
1190 //EventHandler.VMDeath (req_id, 0, null);
1191 events [i] = new EventInfo (etype, req_id) { };
1192 } else if (kind == EventKind.THREAD_START) {
1193 long thread_id = r.ReadId ();
1194 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = thread_id };
1195 //EventHandler.ThreadStart (req_id, thread_id, thread_id);
1196 } else if (kind == EventKind.THREAD_DEATH) {
1197 long thread_id = r.ReadId ();
1198 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = thread_id };
1199 //EventHandler.ThreadDeath (req_id, thread_id, thread_id);
1200 } else if (kind == EventKind.ASSEMBLY_LOAD) {
1201 long thread_id = r.ReadId ();
1202 long id = r.ReadId ();
1203 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1204 //EventHandler.AssemblyLoad (req_id, thread_id, id);
1205 } else if (kind == EventKind.ASSEMBLY_UNLOAD) {
1206 long thread_id = r.ReadId ();
1207 long id = r.ReadId ();
1208 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1209 //EventHandler.AssemblyUnload (req_id, thread_id, id);
1210 } else if (kind == EventKind.TYPE_LOAD) {
1211 long thread_id = r.ReadId ();
1212 long id = r.ReadId ();
1213 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1214 //EventHandler.TypeLoad (req_id, thread_id, id);
1215 } else if (kind == EventKind.METHOD_ENTRY) {
1216 long thread_id = r.ReadId ();
1217 long id = r.ReadId ();
1218 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1219 //EventHandler.MethodEntry (req_id, thread_id, id);
1220 } else if (kind == EventKind.METHOD_EXIT) {
1221 long thread_id = r.ReadId ();
1222 long id = r.ReadId ();
1223 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1224 //EventHandler.MethodExit (req_id, thread_id, id);
1225 } else if (kind == EventKind.BREAKPOINT) {
1226 long thread_id = r.ReadId ();
1227 long id = r.ReadId ();
1228 long loc = r.ReadLong ();
1229 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1230 //EventHandler.Breakpoint (req_id, thread_id, id, loc);
1231 } else if (kind == EventKind.STEP) {
1232 long thread_id = r.ReadId ();
1233 long id = r.ReadId ();
1234 long loc = r.ReadLong ();
1235 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1236 //EventHandler.Step (req_id, thread_id, id, loc);
1237 } else if (kind == EventKind.EXCEPTION) {
1238 long thread_id = r.ReadId ();
1239 long id = r.ReadId ();
1240 long loc = 0; // FIXME
1241 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1242 //EventHandler.Exception (req_id, thread_id, id, loc);
1243 } else if (kind == EventKind.APPDOMAIN_CREATE) {
1244 long thread_id = r.ReadId ();
1245 long id = r.ReadId ();
1246 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1247 //EventHandler.AppDomainCreate (req_id, thread_id, id);
1248 } else if (kind == EventKind.APPDOMAIN_UNLOAD) {
1249 long thread_id = r.ReadId ();
1250 long id = r.ReadId ();
1251 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1252 //EventHandler.AppDomainUnload (req_id, thread_id, id);
1253 } else if (kind == EventKind.USER_BREAK) {
1254 long thread_id = r.ReadId ();
1257 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1258 //EventHandler.Exception (req_id, thread_id, id, loc);
1259 } else if (kind == EventKind.USER_LOG) {
1260 long thread_id = r.ReadId ();
1261 int level = r.ReadInt ();
1262 string category = r.ReadString ();
1263 string message = r.ReadString ();
1264 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Level = level, Category = category, Message = message };
1265 //EventHandler.Exception (req_id, thread_id, id, loc);
1266 } else if (kind == EventKind.KEEPALIVE) {
1267 events [i] = new EventInfo (etype, req_id) { };
1269 throw new NotImplementedException ("Unknown event kind: " + kind);
1273 EventHandler.Events (suspend_policy, events);
1280 internal IEventHandler EventHandler {
1284 static String CommandString (CommandSet command_set, int command)
1287 switch (command_set) {
1289 cmd = ((CmdVM)command).ToString ();
1291 case CommandSet.OBJECT_REF:
1292 cmd = ((CmdObjectRef)command).ToString ();
1294 case CommandSet.STRING_REF:
1295 cmd = ((CmdStringRef)command).ToString ();
1297 case CommandSet.THREAD:
1298 cmd = ((CmdThread)command).ToString ();
1300 case CommandSet.ARRAY_REF:
1301 cmd = ((CmdArrayRef)command).ToString ();
1303 case CommandSet.EVENT_REQUEST:
1304 cmd = ((CmdEventRequest)command).ToString ();
1306 case CommandSet.STACK_FRAME:
1307 cmd = ((CmdStackFrame)command).ToString ();
1309 case CommandSet.APPDOMAIN:
1310 cmd = ((CmdAppDomain)command).ToString ();
1312 case CommandSet.ASSEMBLY:
1313 cmd = ((CmdAssembly)command).ToString ();
1315 case CommandSet.METHOD:
1316 cmd = ((CmdMethod)command).ToString ();
1318 case CommandSet.TYPE:
1319 cmd = ((CmdType)command).ToString ();
1321 case CommandSet.MODULE:
1322 cmd = ((CmdModule)command).ToString ();
1324 case CommandSet.EVENT:
1325 cmd = ((CmdEvent)command).ToString ();
1328 cmd = command.ToString ();
1331 return string.Format ("[{0} {1}]", command_set, cmd);
1334 long total_protocol_ticks;
1336 void LogPacket (int packet_id, byte[] encoded_packet, byte[] reply_packet, CommandSet command_set, int command, Stopwatch watch) {
1338 total_protocol_ticks += watch.ElapsedTicks;
1339 var ts = TimeSpan.FromTicks (total_protocol_ticks);
1340 string msg = string.Format ("Packet: {0} sent: {1} received: {2} ms: {3} total ms: {4} {5}",
1341 packet_id, encoded_packet.Length, reply_packet.Length, watch.ElapsedMilliseconds,
1342 (ts.Seconds * 1000) + ts.Milliseconds,
1343 CommandString (command_set, command));
1345 LoggingStream.WriteLine (msg);
1346 LoggingStream.Flush ();
1349 /* Send a request and call cb when a result is received */
1350 int Send (CommandSet command_set, int command, PacketWriter packet, Action<PacketReader> cb) {
1351 int id = IdGenerator;
1353 Stopwatch watch = null;
1354 if (EnableConnectionLogging)
1355 watch = Stopwatch.StartNew ();
1357 byte[] encoded_packet;
1359 encoded_packet = EncodePacket (id, (int)command_set, command, null, 0);
1361 encoded_packet = EncodePacket (id, (int)command_set, command, packet.Data, packet.Offset);
1363 lock (reply_packets_monitor) {
1364 reply_cbs [id] = delegate (int packet_id, byte[] p) {
1365 if (EnableConnectionLogging)
1366 LogPacket (packet_id, encoded_packet, p, command_set, command, watch);
1367 /* Run the callback on a tp thread to avoid blocking the receive thread */
1368 PacketReader r = new PacketReader (p);
1369 cb.BeginInvoke (r, null, null);
1373 WritePacket (encoded_packet);
1378 PacketReader SendReceive (CommandSet command_set, int command, PacketWriter packet) {
1379 int id = IdGenerator;
1380 Stopwatch watch = null;
1383 throw new VMDisconnectedException ();
1385 if (EnableConnectionLogging)
1386 watch = Stopwatch.StartNew ();
1388 byte[] encoded_packet;
1391 encoded_packet = EncodePacket (id, (int)command_set, command, null, 0);
1393 encoded_packet = EncodePacket (id, (int)command_set, command, packet.Data, packet.Offset);
1395 WritePacket (encoded_packet);
1399 /* Wait for the reply packet */
1401 lock (reply_packets_monitor) {
1402 if (reply_packets.ContainsKey (packetId)) {
1403 byte[] reply = reply_packets [packetId];
1404 reply_packets.Remove (packetId);
1405 PacketReader r = new PacketReader (reply);
1407 if (EnableConnectionLogging)
1408 LogPacket (packetId, encoded_packet, reply, command_set, command, watch);
1409 if (r.ErrorCode != 0) {
1410 if (ErrorHandler != null)
1411 ErrorHandler (this, new ErrorHandlerEventArgs () { ErrorCode = (ErrorCode)r.ErrorCode });
1412 throw new NotImplementedException ("No error handler set.");
1418 throw new VMDisconnectedException ();
1419 Monitor.Wait (reply_packets_monitor);
1425 PacketReader SendReceive (CommandSet command_set, int command) {
1426 return SendReceive (command_set, command, null);
1429 int packet_id_generator;
1433 return Interlocked.Increment (ref packet_id_generator);
1437 CattrInfo[] ReadCattrs (PacketReader r) {
1438 CattrInfo[] res = new CattrInfo [r.ReadInt ()];
1439 for (int i = 0; i < res.Length; ++i) {
1440 CattrInfo info = new CattrInfo ();
1441 info.ctor_id = r.ReadId ();
1442 info.ctor_args = new ValueImpl [r.ReadInt ()];
1443 for (int j = 0; j < info.ctor_args.Length; ++j) {
1444 info.ctor_args [j] = r.ReadValue ();
1446 info.named_args = new CattrNamedArgInfo [r.ReadInt ()];
1447 for (int j = 0; j < info.named_args.Length; ++j) {
1448 CattrNamedArgInfo arg = new CattrNamedArgInfo ();
1449 int arg_type = r.ReadByte ();
1450 arg.is_property = arg_type == 0x54;
1451 arg.id = r.ReadId ();
1452 arg.value = r.ReadValue ();
1453 info.named_args [j] = arg;
1460 static ElementType TypeCodeToElementType (TypeCode c) {
1462 case TypeCode.Boolean:
1463 return ElementType.Boolean;
1465 return ElementType.Char;
1466 case TypeCode.SByte:
1467 return ElementType.I1;
1469 return ElementType.U1;
1470 case TypeCode.Int16:
1471 return ElementType.I2;
1472 case TypeCode.UInt16:
1473 return ElementType.U2;
1474 case TypeCode.Int32:
1475 return ElementType.I4;
1476 case TypeCode.UInt32:
1477 return ElementType.U4;
1478 case TypeCode.Int64:
1479 return ElementType.I8;
1480 case TypeCode.UInt64:
1481 return ElementType.U8;
1482 case TypeCode.Single:
1483 return ElementType.R4;
1484 case TypeCode.Double:
1485 return ElementType.R8;
1487 throw new NotImplementedException ();
1492 * Implementation of debugger commands
1495 internal VersionInfo VM_GetVersion () {
1496 var res = SendReceive (CommandSet.VM, (int)CmdVM.VERSION, null);
1497 VersionInfo info = new VersionInfo ();
1498 info.VMVersion = res.ReadString ();
1499 info.MajorVersion = res.ReadInt ();
1500 info.MinorVersion = res.ReadInt ();
1504 internal void VM_SetProtocolVersion (int major, int minor) {
1505 SendReceive (CommandSet.VM, (int)CmdVM.SET_PROTOCOL_VERSION, new PacketWriter ().WriteInt (major).WriteInt (minor));
1508 internal long[] VM_GetThreads () {
1509 var res = SendReceive (CommandSet.VM, (int)CmdVM.ALL_THREADS, null);
1510 int len = res.ReadInt ();
1511 long[] arr = new long [len];
1512 for (int i = 0; i < len; ++i)
1513 arr [i] = res.ReadId ();
1517 internal void VM_Suspend () {
1518 SendReceive (CommandSet.VM, (int)CmdVM.SUSPEND);
1521 internal void VM_Resume () {
1522 SendReceive (CommandSet.VM, (int)CmdVM.RESUME);
1525 internal void VM_Exit (int exitCode) {
1526 SendReceive (CommandSet.VM, (int)CmdVM.EXIT, new PacketWriter ().WriteInt (exitCode));
1529 internal void VM_Dispose () {
1530 SendReceive (CommandSet.VM, (int)CmdVM.DISPOSE);
1533 internal ValueImpl VM_InvokeMethod (long thread, long method, ValueImpl this_arg, ValueImpl[] arguments, InvokeFlags flags, out ValueImpl exc) {
1535 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));
1536 if (r.ReadByte () == 0) {
1537 exc = r.ReadValue ();
1540 return r.ReadValue ();
1544 internal delegate void InvokeMethodCallback (ValueImpl v, ValueImpl exc, ErrorCode error, object state);
1546 internal int VM_BeginInvokeMethod (long thread, long method, ValueImpl this_arg, ValueImpl[] arguments, InvokeFlags flags, InvokeMethodCallback callback, object state) {
1547 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) {
1550 if (r.ErrorCode != 0) {
1551 callback (null, null, (ErrorCode)r.ErrorCode, state);
1553 if (r.ReadByte () == 0) {
1554 exc = r.ReadValue ();
1561 callback (v, exc, 0, state);
1566 internal void VM_AbortInvoke (long thread, int id)
1568 SendReceive (CommandSet.VM, (int)CmdVM.ABORT_INVOKE, new PacketWriter ().WriteId (thread).WriteInt (id));
1571 internal void SetSocketTimeouts (int send_timeout, int receive_timeout, int keepalive_interval)
1573 TransportSetTimeouts (send_timeout, receive_timeout);
1574 SendReceive (CommandSet.VM, (int)CmdVM.SET_KEEPALIVE, new PacketWriter ().WriteId (keepalive_interval));
1577 internal long[] VM_GetTypesForSourceFile (string fname, bool ignoreCase) {
1578 var res = SendReceive (CommandSet.VM, (int)CmdVM.GET_TYPES_FOR_SOURCE_FILE, new PacketWriter ().WriteString (fname).WriteBool (ignoreCase));
1579 int count = res.ReadInt ();
1580 long[] types = new long [count];
1581 for (int i = 0; i < count; ++i)
1582 types [i] = res.ReadId ();
1586 internal long[] VM_GetTypes (string name, bool ignoreCase) {
1587 var res = SendReceive (CommandSet.VM, (int)CmdVM.GET_TYPES, new PacketWriter ().WriteString (name).WriteBool (ignoreCase));
1588 int count = res.ReadInt ();
1589 long[] types = new long [count];
1590 for (int i = 0; i < count; ++i)
1591 types [i] = res.ReadId ();
1599 internal long RootDomain {
1601 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_ROOT_DOMAIN, null).ReadId ();
1605 internal string Domain_GetName (long id) {
1606 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_FRIENDLY_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1609 internal long[] Domain_GetAssemblies (long id) {
1610 var res = SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_ASSEMBLIES, new PacketWriter ().WriteId (id));
1611 int count = res.ReadInt ();
1612 long[] assemblies = new long [count];
1613 for (int i = 0; i < count; ++i)
1614 assemblies [i] = res.ReadId ();
1618 internal long Domain_GetEntryAssembly (long id) {
1619 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_ENTRY_ASSEMBLY, new PacketWriter ().WriteId (id)).ReadId ();
1622 internal long Domain_GetCorlib (long id) {
1623 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_CORLIB, new PacketWriter ().WriteId (id)).ReadId ();
1626 internal long Domain_CreateString (long id, string s) {
1627 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.CREATE_STRING, new PacketWriter ().WriteId (id).WriteString (s)).ReadId ();
1630 internal long Domain_CreateBoxedValue (long id, long type_id, ValueImpl v) {
1631 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.CREATE_BOXED_VALUE, new PacketWriter ().WriteId (id).WriteId (type_id).WriteValue (v)).ReadId ();
1638 internal string Method_GetName (long id) {
1639 return SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1642 internal long Method_GetDeclaringType (long id) {
1643 return SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_DECLARING_TYPE, new PacketWriter ().WriteId (id)).ReadId ();
1646 internal DebugInfo Method_GetDebugInfo (long id) {
1647 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_DEBUG_INFO, new PacketWriter ().WriteId (id));
1649 DebugInfo info = new DebugInfo ();
1650 info.max_il_offset = res.ReadInt ();
1652 SourceInfo[] sources = null;
1653 if (Version.AtLeast (2, 13)) {
1654 int n = res.ReadInt ();
1655 sources = new SourceInfo [n];
1656 for (int i = 0; i < n; ++i) {
1657 sources [i].source_file = res.ReadString ();
1658 if (Version.AtLeast (2, 14)) {
1659 sources [i].hash = new byte [16];
1660 for (int j = 0; j < 16; ++j)
1661 sources [i].hash [j] = (byte)res.ReadByte ();
1665 sources = new SourceInfo [1];
1666 sources [0].source_file = res.ReadString ();
1669 int n_il_offsets = res.ReadInt ();
1670 info.il_offsets = new int [n_il_offsets];
1671 info.line_numbers = new int [n_il_offsets];
1672 info.source_files = new SourceInfo [n_il_offsets];
1673 for (int i = 0; i < n_il_offsets; ++i) {
1674 info.il_offsets [i] = res.ReadInt ();
1675 info.line_numbers [i] = res.ReadInt ();
1676 if (Version.AtLeast (2, 12)) {
1677 int idx = res.ReadInt ();
1678 info.source_files [i] = idx >= 0 ? sources [idx] : default (SourceInfo);
1680 info.source_files [i] = sources [0];
1687 internal ParamInfo Method_GetParamInfo (long id) {
1688 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_PARAM_INFO, new PacketWriter ().WriteId (id));
1690 ParamInfo info = new ParamInfo ();
1691 info.call_conv = res.ReadInt ();
1692 info.param_count = res.ReadInt ();
1693 info.generic_param_count = res.ReadInt ();
1694 info.ret_type = res.ReadId ();
1695 info.param_types = new long [info.param_count];
1696 for (int i = 0; i < info.param_count; ++i)
1697 info.param_types [i] = res.ReadId ();
1698 info.param_names = new string [info.param_count];
1699 for (int i = 0; i < info.param_count; ++i)
1700 info.param_names [i] = res.ReadString ();
1705 internal LocalsInfo Method_GetLocalsInfo (long id) {
1706 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_LOCALS_INFO, new PacketWriter ().WriteId (id));
1708 LocalsInfo info = new LocalsInfo ();
1709 int nlocals = res.ReadInt ();
1710 info.types = new long [nlocals];
1711 for (int i = 0; i < nlocals; ++i)
1712 info.types [i] = res.ReadId ();
1713 info.names = new string [nlocals];
1714 for (int i = 0; i < nlocals; ++i)
1715 info.names [i] = res.ReadString ();
1716 info.live_range_start = new int [nlocals];
1717 info.live_range_end = new int [nlocals];
1718 for (int i = 0; i < nlocals; ++i) {
1719 info.live_range_start [i] = res.ReadInt ();
1720 info.live_range_end [i] = res.ReadInt ();
1726 internal MethodInfo Method_GetInfo (long id) {
1727 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_INFO, new PacketWriter ().WriteId (id));
1729 MethodInfo info = new MethodInfo ();
1730 info.attributes = res.ReadInt ();
1731 info.iattributes = res.ReadInt ();
1732 info.token = res.ReadInt ();
1733 if (Version.AtLeast (2, 12)) {
1734 int attrs = res.ReadByte ();
1735 if ((attrs & (1 << 0)) != 0)
1737 if ((attrs & (1 << 1)) != 0)
1738 info.is_generic_method = true;
1739 info.gmd = res.ReadId ();
1740 if (Version.AtLeast (2, 15)) {
1741 if (info.is_generic_method) {
1742 int n = res.ReadInt ();
1743 info.type_args = res.ReadIds (n);
1750 internal MethodBodyInfo Method_GetBody (long id) {
1751 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_BODY, new PacketWriter ().WriteId (id));
1753 MethodBodyInfo info = new MethodBodyInfo ();
1754 info.il = new byte [res.ReadInt ()];
1755 for (int i = 0; i < info.il.Length; ++i)
1756 info.il [i] = (byte)res.ReadByte ();
1761 internal ResolvedToken Method_ResolveToken (long id, int token) {
1762 var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.RESOLVE_TOKEN, new PacketWriter ().WriteId (id).WriteInt (token));
1764 TokenType type = (TokenType)res.ReadByte ();
1766 case TokenType.STRING:
1767 return new ResolvedToken () { Type = type, Str = res.ReadString () };
1768 case TokenType.TYPE:
1769 case TokenType.METHOD:
1770 case TokenType.FIELD:
1771 return new ResolvedToken () { Type = type, Id = res.ReadId () };
1772 case TokenType.UNKNOWN:
1773 return new ResolvedToken () { Type = type };
1775 throw new NotImplementedException ();
1783 internal string Thread_GetName (long id) {
1784 return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1787 internal FrameInfo[] Thread_GetFrameInfo (long id, int start_frame, int length) {
1788 var res = SendReceive (CommandSet.THREAD, (int)CmdThread.GET_FRAME_INFO, new PacketWriter ().WriteId (id).WriteInt (start_frame).WriteInt (length));
1789 int count = res.ReadInt ();
1791 var frames = new FrameInfo [count];
1792 for (int i = 0; i < count; ++i) {
1793 var f = new FrameInfo ();
1794 f.id = res.ReadInt ();
1795 f.method = res.ReadId ();
1796 f.il_offset = res.ReadInt ();
1797 f.flags = (StackFrameFlags)res.ReadByte ();
1804 internal int Thread_GetState (long id) {
1805 return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_STATE, new PacketWriter ().WriteId (id)).ReadInt ();
1808 internal ThreadInfo Thread_GetInfo (long id) {
1809 PacketReader r = SendReceive (CommandSet.THREAD, (int)CmdThread.GET_INFO, new PacketWriter ().WriteId (id));
1811 ThreadInfo res = new ThreadInfo () { is_thread_pool = r.ReadByte () > 0 ? true : false };
1816 internal long Thread_GetId (long id) {
1817 return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_ID, new PacketWriter ().WriteId (id)).ReadLong ();
1820 internal long Thread_GetTID (long id) {
1821 return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_TID, new PacketWriter ().WriteId (id)).ReadLong ();
1828 internal ModuleInfo Module_GetInfo (long id) {
1829 PacketReader r = SendReceive (CommandSet.MODULE, (int)CmdModule.GET_INFO, new PacketWriter ().WriteId (id));
1830 ModuleInfo info = new ModuleInfo { Name = r.ReadString (), ScopeName = r.ReadString (), FQName = r.ReadString (), Guid = r.ReadString (), Assembly = r.ReadId () };
1838 internal string Assembly_GetLocation (long id) {
1839 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_LOCATION, new PacketWriter ().WriteId (id)).ReadString ();
1842 internal long Assembly_GetEntryPoint (long id) {
1843 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_ENTRY_POINT, new PacketWriter ().WriteId (id)).ReadId ();
1846 internal long Assembly_GetManifestModule (long id) {
1847 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_MANIFEST_MODULE, new PacketWriter ().WriteId (id)).ReadId ();
1850 internal long Assembly_GetObject (long id) {
1851 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_OBJECT, new PacketWriter ().WriteId (id)).ReadId ();
1854 internal long Assembly_GetType (long id, string name, bool ignoreCase) {
1855 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_TYPE, new PacketWriter ().WriteId (id).WriteString (name).WriteBool (ignoreCase)).ReadId ();
1858 internal string Assembly_GetName (long id) {
1859 return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1866 internal TypeInfo Type_GetInfo (long id) {
1867 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_INFO, new PacketWriter ().WriteId (id));
1868 TypeInfo res = new TypeInfo ();
1870 res.ns = r.ReadString ();
1871 res.name = r.ReadString ();
1872 res.full_name = r.ReadString ();
1873 res.assembly = r.ReadId ();
1874 res.module = r.ReadId ();
1875 res.base_type = r.ReadId ();
1876 res.element_type = r.ReadId ();
1877 res.token = r.ReadInt ();
1878 res.rank = r.ReadByte ();
1879 res.attributes = r.ReadInt ();
1880 int b = r.ReadByte ();
1881 res.is_byref = (b & 1) != 0;
1882 res.is_pointer = (b & 2) != 0;
1883 res.is_primitive = (b & 4) != 0;
1884 res.is_valuetype = (b & 8) != 0;
1885 res.is_enum = (b & 16) != 0;
1886 res.is_gtd = (b & 32) != 0;
1887 res.is_generic_type = (b & 64) != 0;
1889 int nested_len = r.ReadInt ();
1890 res.nested = new long [nested_len];
1891 for (int i = 0; i < nested_len; ++i)
1892 res.nested [i] = r.ReadId ();
1894 if (Version.AtLeast (2, 12))
1895 res.gtd = r.ReadId ();
1896 if (Version.AtLeast (2, 15) && res.is_generic_type) {
1897 int n = r.ReadInt ();
1898 res.type_args = r.ReadIds (n);
1904 internal long[] Type_GetMethods (long id) {
1905 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_METHODS, new PacketWriter ().WriteId (id));
1907 int n = r.ReadInt ();
1908 long[] res = new long [n];
1909 for (int i = 0; i < n; ++i)
1910 res [i] = r.ReadId ();
1914 internal long[] Type_GetFields (long id, out string[] names, out long[] types, out int[] attrs) {
1915 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_FIELDS, new PacketWriter ().WriteId (id));
1917 int n = r.ReadInt ();
1918 long[] res = new long [n];
1919 names = new string [n];
1920 types = new long [n];
1921 attrs = new int [n];
1922 for (int i = 0; i < n; ++i) {
1923 res [i] = r.ReadId ();
1924 names [i] = r.ReadString ();
1925 types [i] = r.ReadId ();
1926 attrs [i] = r.ReadInt ();
1931 internal PropInfo[] Type_GetProperties (long id) {
1932 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_PROPERTIES, new PacketWriter ().WriteId (id));
1934 int n = r.ReadInt ();
1935 PropInfo[] res = new PropInfo [n];
1936 for (int i = 0; i < n; ++i) {
1937 res [i] = new PropInfo ();
1938 res [i].id = r.ReadId ();
1939 res [i].name = r.ReadString ();
1940 res [i].get_method = r.ReadId ();
1941 res [i].set_method = r.ReadId ();
1942 res [i].attrs = r.ReadInt ();
1948 internal long Type_GetObject (long id) {
1949 return SendReceive (CommandSet.TYPE, (int)CmdType.GET_OBJECT, new PacketWriter ().WriteId (id)).ReadId ();
1952 internal ValueImpl[] Type_GetValues (long id, long[] fields, long thread_id) {
1953 int len = fields.Length;
1956 r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_VALUES_2, new PacketWriter ().WriteId (id).WriteId (thread_id).WriteInt (len).WriteIds (fields));
1958 r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_VALUES, new PacketWriter ().WriteId (id).WriteInt (len).WriteIds (fields));
1960 ValueImpl[] res = new ValueImpl [len];
1961 for (int i = 0; i < len; ++i)
1962 res [i] = r.ReadValue ();
1966 internal void Type_SetValues (long id, long[] fields, ValueImpl[] values) {
1967 SendReceive (CommandSet.TYPE, (int)CmdType.SET_VALUES, new PacketWriter ().WriteId (id).WriteInt (fields.Length).WriteIds (fields).WriteValues (values));
1970 internal string[] Type_GetSourceFiles (long id, bool return_full_paths) {
1971 var r = SendReceive (CommandSet.TYPE, return_full_paths ? (int)CmdType.GET_SOURCE_FILES_2 : (int)CmdType.GET_SOURCE_FILES, new PacketWriter ().WriteId (id));
1972 int len = r.ReadInt ();
1973 string[] res = new string [len];
1974 for (int i = 0; i < len; ++i)
1975 res [i] = r.ReadString ();
1979 internal bool Type_IsAssignableFrom (long id, long c_id) {
1980 return SendReceive (CommandSet.TYPE, (int)CmdType.IS_ASSIGNABLE_FROM, new PacketWriter ().WriteId (id).WriteId (c_id)).ReadByte () > 0;
1983 internal CattrInfo[] Type_GetCustomAttributes (long id, long attr_type_id, bool inherit) {
1984 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_CATTRS, new PacketWriter ().WriteId (id).WriteId (attr_type_id));
1985 return ReadCattrs (r);
1988 internal CattrInfo[] Type_GetFieldCustomAttributes (long id, long field_id, long attr_type_id, bool inherit) {
1989 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_FIELD_CATTRS, new PacketWriter ().WriteId (id).WriteId (field_id).WriteId (attr_type_id));
1990 return ReadCattrs (r);
1993 internal CattrInfo[] Type_GetPropertyCustomAttributes (long id, long field_id, long attr_type_id, bool inherit) {
1994 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_PROPERTY_CATTRS, new PacketWriter ().WriteId (id).WriteId (field_id).WriteId (attr_type_id));
1995 return ReadCattrs (r);
1998 public long[] Type_GetMethodsByNameFlags (long id, string name, int flags, bool ignoreCase) {
1999 flags |= ignoreCase ? (int)BindingFlagsExtensions.BINDING_FLAGS_IGNORE_CASE : 0;
2000 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.CMD_TYPE_GET_METHODS_BY_NAME_FLAGS, new PacketWriter ().WriteId (id).WriteString (name).WriteInt (flags));
2001 int len = r.ReadInt ();
2002 long[] res = new long [len];
2003 for (int i = 0; i < len; ++i)
2004 res [i] = r.ReadId ();
2008 internal long[] Type_GetInterfaces (long id) {
2009 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_INTERFACES, new PacketWriter ().WriteId (id));
2010 int len = r.ReadInt ();
2011 return r.ReadIds (len);
2014 internal IfaceMapInfo[] Type_GetInterfaceMap (long id, long[] ids) {
2015 PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_INTERFACE_MAP, new PacketWriter ().WriteId (id).WriteInt (ids.Length).WriteIds (ids));
2016 var res = new IfaceMapInfo [ids.Length];
2017 for (int i = 0; i < ids.Length; ++i) {
2018 int n = r.ReadInt ();
2020 res [i].iface_id = ids [i];
2021 res [i].iface_methods = r.ReadIds (n);
2022 res [i].target_methods = r.ReadIds (n);
2032 internal int EnableEvent (EventType etype, SuspendPolicy suspend_policy, List<Modifier> mods) {
2033 var w = new PacketWriter ().WriteByte ((byte)etype).WriteByte ((byte)suspend_policy);
2035 if (mods.Count > 255)
2036 throw new NotImplementedException ();
2037 w.WriteByte ((byte)mods.Count);
2038 foreach (Modifier mod in mods) {
2039 if (mod is CountModifier) {
2040 w.WriteByte ((byte)ModifierKind.COUNT);
2041 w.WriteInt ((mod as CountModifier).Count);
2042 } else if (mod is LocationModifier) {
2043 w.WriteByte ((byte)ModifierKind.LOCATION_ONLY);
2044 w.WriteId ((mod as LocationModifier).Method);
2045 w.WriteLong ((mod as LocationModifier).Location);
2046 } else if (mod is StepModifier) {
2047 w.WriteByte ((byte)ModifierKind.STEP);
2048 w.WriteId ((mod as StepModifier).Thread);
2049 w.WriteInt ((mod as StepModifier).Size);
2050 w.WriteInt ((mod as StepModifier).Depth);
2051 if (Version.AtLeast (2, 16))
2052 w.WriteInt ((mod as StepModifier).Filter);
2053 } else if (mod is ThreadModifier) {
2054 w.WriteByte ((byte)ModifierKind.THREAD_ONLY);
2055 w.WriteId ((mod as ThreadModifier).Thread);
2056 } else if (mod is ExceptionModifier) {
2057 var em = mod as ExceptionModifier;
2058 w.WriteByte ((byte)ModifierKind.EXCEPTION_ONLY);
2059 w.WriteId (em.Type);
2060 if (Version.MajorVersion > 2 || Version.MinorVersion > 0) {
2061 /* This is only supported in protocol version 2.1 */
2062 w.WriteBool (em.Caught);
2063 w.WriteBool (em.Uncaught);
2064 } else if (!em.Caught || !em.Uncaught) {
2065 throw new NotSupportedException ("This request is not supported by the protocol version implemented by the debuggee.");
2067 } else if (mod is AssemblyModifier) {
2068 w.WriteByte ((byte)ModifierKind.ASSEMBLY_ONLY);
2069 var amod = (mod as AssemblyModifier);
2070 w.WriteInt (amod.Assemblies.Length);
2071 foreach (var id in amod.Assemblies)
2073 } else if (mod is SourceFileModifier) {
2074 w.WriteByte ((byte)ModifierKind.SOURCE_FILE_ONLY);
2075 var smod = (mod as SourceFileModifier);
2076 w.WriteInt (smod.SourceFiles.Length);
2077 foreach (var s in smod.SourceFiles)
2079 } else if (mod is TypeNameModifier) {
2080 w.WriteByte ((byte)ModifierKind.TYPE_NAME_ONLY);
2081 var tmod = (mod as TypeNameModifier);
2082 w.WriteInt (tmod.TypeNames.Length);
2083 foreach (var s in tmod.TypeNames)
2086 throw new NotImplementedException ();
2092 return SendReceive (CommandSet.EVENT_REQUEST, (int)CmdEventRequest.SET, w).ReadInt ();
2095 internal void ClearEventRequest (EventType etype, int req_id) {
2096 SendReceive (CommandSet.EVENT_REQUEST, (int)CmdEventRequest.CLEAR, new PacketWriter ().WriteByte ((byte)etype).WriteInt (req_id));
2099 internal void ClearAllBreakpoints () {
2100 SendReceive (CommandSet.EVENT_REQUEST, (int)CmdEventRequest.CLEAR_ALL_BREAKPOINTS, new PacketWriter ());
2106 internal ValueImpl StackFrame_GetThis (long thread_id, long id) {
2107 PacketReader r = SendReceive (CommandSet.STACK_FRAME, (int)CmdStackFrame.GET_THIS, new PacketWriter ().WriteId (thread_id).WriteId (id));
2108 return r.ReadValue ();
2111 internal ValueImpl[] StackFrame_GetValues (long thread_id, long id, int[] pos) {
2112 /* pos < 0 -> argument at pos (-pos) - 1 */
2113 /* pos >= 0 -> local at pos */
2114 int len = pos.Length;
2115 PacketReader r = SendReceive (CommandSet.STACK_FRAME, (int)CmdStackFrame.GET_VALUES, new PacketWriter ().WriteId (thread_id).WriteId (id).WriteInt (len).WriteInts (pos));
2117 ValueImpl[] res = new ValueImpl [len];
2118 for (int i = 0; i < len; ++i)
2119 res [i] = r.ReadValue ();
2123 internal void StackFrame_SetValues (long thread_id, long id, int[] pos, ValueImpl[] values) {
2124 /* pos < 0 -> argument at pos (-pos) - 1 */
2125 /* pos >= 0 -> local at pos */
2126 int len = pos.Length;
2127 SendReceive (CommandSet.STACK_FRAME, (int)CmdStackFrame.SET_VALUES, new PacketWriter ().WriteId (thread_id).WriteId (id).WriteInt (len).WriteInts (pos).WriteValues (values));
2133 internal int[] Array_GetLength (long id, out int rank, out int[] lower_bounds) {
2134 var r = SendReceive (CommandSet.ARRAY_REF, (int)CmdArrayRef.GET_LENGTH, new PacketWriter ().WriteId (id));
2135 rank = r.ReadInt ();
2136 int[] res = new int [rank];
2137 lower_bounds = new int [rank];
2138 for (int i = 0; i < rank; ++i) {
2139 res [i] = r.ReadInt ();
2140 lower_bounds [i] = r.ReadInt ();
2145 internal ValueImpl[] Array_GetValues (long id, int index, int len) {
2146 var r = SendReceive (CommandSet.ARRAY_REF, (int)CmdArrayRef.GET_VALUES, new PacketWriter ().WriteId (id).WriteInt (index).WriteInt (len));
2147 ValueImpl[] res = new ValueImpl [len];
2148 for (int i = 0; i < len; ++i)
2149 res [i] = r.ReadValue ();
2153 internal void Array_SetValues (long id, int index, ValueImpl[] values) {
2154 SendReceive (CommandSet.ARRAY_REF, (int)CmdArrayRef.SET_VALUES, new PacketWriter ().WriteId (id).WriteInt (index).WriteInt (values.Length).WriteValues (values));
2160 internal string String_GetValue (long id) {
2161 return SendReceive (CommandSet.STRING_REF, (int)CmdStringRef.GET_VALUE, new PacketWriter ().WriteId (id)).ReadString ();
2164 internal int String_GetLength (long id) {
2165 return (int)SendReceive (CommandSet.STRING_REF, (int)CmdStringRef.GET_LENGTH, new PacketWriter ().WriteId (id)).ReadLong ();
2168 internal char[] String_GetChars (long id, int index, int length) {
2169 var r = SendReceive (CommandSet.STRING_REF, (int)CmdStringRef.GET_CHARS, new PacketWriter ().WriteId (id).WriteLong (index).WriteLong (length));
2170 var res = new char [length];
2171 for (int i = 0; i < length; ++i)
2172 res [i] = (char)r.ReadShort ();
2179 internal long Object_GetType (long id) {
2180 return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_TYPE, new PacketWriter ().WriteId (id)).ReadId ();
2183 internal long Object_GetDomain (long id) {
2184 return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_DOMAIN, new PacketWriter ().WriteId (id)).ReadId ();
2187 internal ValueImpl[] Object_GetValues (long id, long[] fields) {
2188 int len = fields.Length;
2189 PacketReader r = SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_VALUES, new PacketWriter ().WriteId (id).WriteInt (len).WriteIds (fields));
2191 ValueImpl[] res = new ValueImpl [len];
2192 for (int i = 0; i < len; ++i)
2193 res [i] = r.ReadValue ();
2197 internal void Object_SetValues (long id, long[] fields, ValueImpl[] values) {
2198 SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.SET_VALUES, new PacketWriter ().WriteId (id).WriteInt (fields.Length).WriteIds (fields).WriteValues (values));
2201 internal bool Object_IsCollected (long id) {
2202 return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.IS_COLLECTED, new PacketWriter ().WriteId (id)).ReadInt () == 1;
2205 internal long Object_GetAddress (long id) {
2206 return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_ADDRESS, new PacketWriter ().WriteId (id)).ReadLong ();
2209 internal ObjectRefInfo Object_GetInfo (long id) {
2210 ObjectRefInfo res = new ObjectRefInfo ();
2211 PacketReader r = SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_INFO, new PacketWriter ().WriteId (id));
2213 res.type_id = r.ReadId ();
2214 res.domain_id = r.ReadId ();
2218 public void Dispose ()
2221 GC.SuppressFinalize (this);
2224 protected virtual void Dispose (bool disposing)
2234 class TcpConnection : Connection
2238 internal TcpConnection (Socket socket)
2240 this.socket = socket;
2241 //socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.NoDelay, 1);
2244 internal EndPoint EndPoint {
2246 return socket.RemoteEndPoint;
2250 protected override int TransportSend (byte[] buf, int buf_offset, int len)
2252 return socket.Send (buf, buf_offset, len, SocketFlags.None);
2255 protected override int TransportReceive (byte[] buf, int buf_offset, int len)
2257 return socket.Receive (buf, buf_offset, len, SocketFlags.None);
2260 protected override void TransportSetTimeouts (int send_timeout, int receive_timeout)
2262 socket.SendTimeout = send_timeout;
2263 socket.ReceiveTimeout = receive_timeout;
2266 protected override void TransportClose ()
2271 protected override void Dispose (bool disposing)
2274 //Socket.Dispose is explicit in < .NET 4.0
2275 ((IDisposable)socket).Dispose ();
2277 base.Dispose (disposing);
2281 /* This is the interface exposed by the debugger towards the debugger agent */
2282 interface IEventHandler
2284 void Events (SuspendPolicy suspend_policy, EventInfo[] events);
2286 void VMDisconnect (int req_id, long thread_id, string vm_uri);