[Mono.Debugger.Soft] Added FlagsAttrbite where appropriate
[mono.git] / mcs / class / Mono.Debugger.Soft / Mono.Debugger.Soft / Connection.cs
1 using System;
2 using System.IO;
3 using System.Net;
4 using System.Net.Sockets;
5 using System.Threading;
6 using System.Collections.Generic;
7 using System.Text;
8 using System.Diagnostics;
9 using Mono.Cecil.Metadata;
10
11 namespace Mono.Debugger.Soft
12 {
13         public class VersionInfo {
14                 public string VMVersion {
15                         get; set;
16                 }
17
18                 public int MajorVersion {
19                         get; set;
20                 }
21
22                 public int MinorVersion {
23                         get; set;
24                 }
25
26                 /*
27                  * Check that this version is at least major:minor
28                  */
29                 public bool AtLeast (int major, int minor) {
30                         if ((MajorVersion > major) || ((MajorVersion == major && MinorVersion >= minor)))
31                                 return true;
32                         else
33                                 return false;
34                 }
35         }
36
37         struct SourceInfo {
38                 public string source_file;
39                 public byte[] guid, hash;
40         }
41
42         class DebugInfo {
43                 public int max_il_offset;
44                 public int[] il_offsets;
45                 public int[] line_numbers;
46                 public int[] column_numbers;
47                 public SourceInfo[] source_files;
48         }
49
50         struct FrameInfo {
51                 public long id;
52                 public long method;
53                 public int il_offset;
54                 public StackFrameFlags flags;
55         }
56
57         class TypeInfo {
58                 public string ns, name, full_name;
59                 public long assembly, module, base_type, element_type;
60                 public int token, rank, attributes;
61                 public bool is_byref, is_pointer, is_primitive, is_valuetype, is_enum;
62                 public bool is_gtd, is_generic_type;
63                 public long[] nested;
64                 public long gtd;
65                 public long[] type_args;
66         }
67
68         struct IfaceMapInfo {
69                 public long iface_id;
70                 public long[] iface_methods;
71                 public long[] target_methods;
72         }
73
74         class MethodInfo {
75                 public int attributes, iattributes, token;
76                 public bool is_gmd, is_generic_method;
77                 public long gmd;
78                 public long[] type_args;
79         }
80
81         class MethodBodyInfo {
82                 public byte[] il;
83                 public ExceptionClauseInfo[] clauses;
84         }
85
86         struct ExceptionClauseInfo {
87                 public ExceptionClauseFlags flags;
88                 public int try_offset;
89                 public int try_length;
90                 public int handler_offset;
91                 public int handler_length;
92                 public int filter_offset;
93                 public long catch_type_id;
94         }
95
96         [Flags]
97         enum ExceptionClauseFlags {
98                 None = 0x0,
99                 Filter = 0x1,
100                 Finally = 0x2,
101                 Fault = 0x4,
102         }
103
104         struct ParamInfo {
105                 public int call_conv;
106                 public int param_count;
107                 public int generic_param_count;
108                 public long ret_type;
109                 public long[] param_types;
110                 public string[] param_names;
111         }
112
113         struct LocalsInfo {
114                 public long[] types;
115                 public string[] names;
116                 public int[] live_range_start;
117                 public int[] live_range_end;
118         }
119
120         struct PropInfo {
121                 public long id;
122                 public string name;
123                 public long get_method, set_method;
124                 public int attrs;
125         }
126
127         class CattrNamedArgInfo {
128                 public bool is_property;
129                 public long id;
130                 public ValueImpl value;
131         }
132
133         class CattrInfo {
134                 public long ctor_id;
135                 public ValueImpl[] ctor_args;
136                 public CattrNamedArgInfo[] named_args;
137         }
138
139         class ThreadInfo {
140                 public bool is_thread_pool;
141         }
142
143         struct ObjectRefInfo {
144                 public long type_id;
145                 public long domain_id;
146         }
147
148         enum ValueTypeId {
149                 VALUE_TYPE_ID_NULL = 0xf0,
150                 VALUE_TYPE_ID_TYPE = 0xf1
151         }
152
153         [Flags]
154         enum InvokeFlags {
155                 NONE = 0x0,
156                 DISABLE_BREAKPOINTS = 0x1,
157                 SINGLE_THREADED = 0x2
158         }
159
160         enum ElementType {
161                 End              = 0x00,
162                 Void            = 0x01,
163                 Boolean  = 0x02,
164                 Char            = 0x03,
165                 I1                = 0x04,
166                 U1                = 0x05,
167                 I2                = 0x06,
168                 U2                = 0x07,
169                 I4                = 0x08,
170                 U4                = 0x09,
171                 I8                = 0x0a,
172                 U8                = 0x0b,
173                 R4                = 0x0c,
174                 R8                = 0x0d,
175                 String    = 0x0e,
176                 Ptr              = 0x0f,
177                 ByRef      = 0x10,
178                 ValueType   = 0x11,
179                 Class      = 0x12,
180                 Var        = 0x13,
181                 Array      = 0x14,
182                 GenericInst = 0x15,
183                 TypedByRef  = 0x16,
184                 I                  = 0x18,
185                 U                  = 0x19,
186                 FnPtr      = 0x1b,
187                 Object    = 0x1c,
188                 SzArray  = 0x1d,
189                 MVar       = 0x1e,
190                 CModReqD        = 0x1f,
191                 CModOpt  = 0x20,
192                 Internal        = 0x21,
193                 Modifier        = 0x40,
194                 Sentinel        = 0x41,
195                 Pinned    = 0x45,
196
197                 Type            = 0x50,
198                 Boxed      = 0x51,
199                 Enum            = 0x55
200         }
201
202         class ValueImpl {
203                 public ElementType Type; /* or one of the VALUE_TYPE_ID constants */
204                 public long Objid;
205                 public object Value;
206                 public long Klass; // For ElementType.ValueType
207                 public ValueImpl[] Fields; // for ElementType.ValueType
208                 public bool IsEnum; // For ElementType.ValueType
209                 public long Id; /* For VALUE_TYPE_ID_TYPE */
210         }
211
212         class ModuleInfo {
213                 public string Name, ScopeName, FQName, Guid;
214                 public long Assembly;
215         }               
216
217         enum TokenType {
218                 STRING = 0,
219                 TYPE = 1,
220                 FIELD = 2,
221                 METHOD = 3,
222                 UNKNOWN = 4
223         }
224
225         [Flags]
226         enum StackFrameFlags {
227                 NONE = 0,
228                 DEBUGGER_INVOKE = 1,
229                 NATIVE_TRANSITION = 2
230         }
231
232         class ResolvedToken {
233                 public TokenType Type;
234                 public string Str;
235                 public long Id;
236         }
237
238         class Modifier {
239         }
240
241         class CountModifier : Modifier {
242                 public int Count {
243                         get; set;
244                 }
245         }
246
247         class LocationModifier : Modifier {
248                 public long Method {
249                         get; set;
250                 }
251
252                 public long Location {
253                         get; set;
254                 }
255         }
256
257         class StepModifier : Modifier {
258                 public long Thread {
259                         get; set;
260                 }
261
262                 public int Depth {
263                         get; set;
264                 }
265
266                 public int Size {
267                         get; set;
268                 }
269
270                 public int Filter {
271                         get; set;
272                 }
273         }
274
275         class ThreadModifier : Modifier {
276                 public long Thread {
277                         get; set;
278                 }
279         }
280
281         class ExceptionModifier : Modifier {
282                 public long Type {
283                         get; set;
284                 }
285                 public bool Caught {
286                         get; set;
287                 }
288                 public bool Uncaught {
289                         get; set;
290                 }
291                 public bool Subclasses {
292                         get; set;
293                 }
294         }
295
296         class AssemblyModifier : Modifier {
297                 public long[] Assemblies {
298                         get; set;
299                 }
300         }
301
302         class SourceFileModifier : Modifier {
303                 public string[] SourceFiles {
304                         get; set;
305                 }
306         }
307
308         class TypeNameModifier : Modifier {
309                 public string[] TypeNames {
310                         get; set;
311                 }
312         }
313
314         class EventInfo {
315                 public EventType EventType {
316                         get; set;
317                 }
318
319                 public int ReqId {
320                         get; set;
321                 }
322
323                 public SuspendPolicy SuspendPolicy {
324                         get; set;
325                 }
326
327                 public long ThreadId {
328                         get; set;
329                 }
330
331                 public long Id {
332                         get; set;
333                 }
334
335                 public long Location {
336                         get; set;
337                 }
338
339                 public int Level {
340                         get; set;
341                 }
342
343                 public string Category {
344                         get; set;
345                 }
346
347                 public string Message {
348                         get; set;
349                 }
350
351                 public EventInfo (EventType type, int req_id) {
352                         EventType = type;
353                         ReqId = req_id;
354                 }
355         }
356
357         public enum ErrorCode {
358                 NONE = 0,
359                 INVALID_OBJECT = 20,
360                 INVALID_FIELDID = 25,
361                 INVALID_FRAMEID = 30,
362                 NOT_IMPLEMENTED = 100,
363                 NOT_SUSPENDED = 101,
364                 INVALID_ARGUMENT = 102,
365                 ERR_UNLOADED = 103,
366                 ERR_NO_INVOCATION = 104,
367                 ABSENT_INFORMATION = 105,
368                 NO_SEQ_POINT_AT_IL_OFFSET = 106
369         }
370
371         public class ErrorHandlerEventArgs : EventArgs {
372
373                 public ErrorCode ErrorCode {
374                         get; set;
375                 }
376         }
377
378         /*
379          * Represents the connection to the debuggee
380          */
381         public abstract class Connection
382         {
383                 /*
384                  * The protocol and the packet format is based on JDWP, the differences 
385                  * are in the set of supported events, and the commands.
386                  */
387                 internal const string HANDSHAKE_STRING = "DWP-Handshake";
388
389                 internal const int HEADER_LENGTH = 11;
390
391                 static readonly bool EnableConnectionLogging = !String.IsNullOrEmpty (Environment.GetEnvironmentVariable ("MONO_SDB_LOG"));
392                 static int ConnectionId;
393                 readonly StreamWriter LoggingStream = EnableConnectionLogging ? 
394                         new StreamWriter (string.Format ("/tmp/sdb_conn_log_{0}", ConnectionId++), false) : null;
395
396                 /*
397                  * Th version of the wire-protocol implemented by the library. The library
398                  * and the debuggee can communicate if they implement the same major version.
399                  * If they implement a different minor version, they can communicate, but some
400                  * features might not be available. This allows older clients to communicate
401                  * with newer runtimes, and vice versa.
402                  */
403                 internal const int MAJOR_VERSION = 2;
404                 internal const int MINOR_VERSION = 26;
405
406                 enum WPSuspendPolicy {
407                         NONE = 0,
408                         EVENT_THREAD = 1,
409                         ALL = 2
410                 }
411
412                 enum CommandSet {
413                         VM = 1,
414                         OBJECT_REF = 9,
415                         STRING_REF = 10,
416                         THREAD = 11,
417                         ARRAY_REF = 13,
418                         EVENT_REQUEST = 15,
419                         STACK_FRAME = 16,
420                         APPDOMAIN = 20,
421                         ASSEMBLY = 21,
422                         METHOD = 22,
423                         TYPE = 23,
424                         MODULE = 24,
425                         EVENT = 64
426                 }
427
428                 enum EventKind {
429                         VM_START = 0,
430                         VM_DEATH = 1,
431                         THREAD_START = 2,
432                         THREAD_DEATH = 3,
433                         APPDOMAIN_CREATE = 4, // Not in JDI
434                         APPDOMAIN_UNLOAD = 5, // Not in JDI
435                         METHOD_ENTRY = 6,
436                         METHOD_EXIT = 7,
437                         ASSEMBLY_LOAD = 8,
438                         ASSEMBLY_UNLOAD = 9,
439                         BREAKPOINT = 10,
440                         STEP = 11,
441                         TYPE_LOAD = 12,
442                         EXCEPTION = 13,
443                         KEEPALIVE = 14,
444                         USER_BREAK = 15,
445                         USER_LOG = 16
446                 }
447
448                 enum ModifierKind {
449                         COUNT = 1,
450                         THREAD_ONLY = 3,
451                         LOCATION_ONLY = 7,
452                         EXCEPTION_ONLY = 8,
453                         STEP = 10,
454                         ASSEMBLY_ONLY = 11,
455                         SOURCE_FILE_ONLY = 12,
456                         TYPE_NAME_ONLY = 13
457                 }
458
459                 enum CmdVM {
460                         VERSION = 1,
461                         ALL_THREADS = 2,
462                         SUSPEND = 3,
463                         RESUME = 4,
464                         EXIT = 5,
465                         DISPOSE = 6,
466                         INVOKE_METHOD = 7,
467                         SET_PROTOCOL_VERSION = 8,
468                         ABORT_INVOKE = 9,
469                         SET_KEEPALIVE = 10,
470                         GET_TYPES_FOR_SOURCE_FILE = 11,
471                         GET_TYPES = 12,
472                         INVOKE_METHODS = 13
473                 }
474
475                 enum CmdEvent {
476                         COMPOSITE = 100
477                 }
478
479                 enum CmdThread {
480                         GET_FRAME_INFO = 1,
481                         GET_NAME = 2,
482                         GET_STATE = 3,
483                         GET_INFO = 4,
484                         /* FIXME: Merge into GET_INFO when the major protocol version is increased */
485                         GET_ID = 5,
486                         /* Ditto */
487                         GET_TID = 6
488                 }
489
490                 enum CmdEventRequest {
491                         SET = 1,
492                         CLEAR = 2,
493                         CLEAR_ALL_BREAKPOINTS = 3
494                 }
495
496                 enum CmdAppDomain {
497                         GET_ROOT_DOMAIN = 1,
498                         GET_FRIENDLY_NAME = 2,
499                         GET_ASSEMBLIES = 3,
500                         GET_ENTRY_ASSEMBLY = 4,
501                         CREATE_STRING = 5,
502                         GET_CORLIB = 6,
503                         CREATE_BOXED_VALUE = 7
504                 }
505
506                 enum CmdAssembly {
507                         GET_LOCATION = 1,
508                         GET_ENTRY_POINT = 2,
509                         GET_MANIFEST_MODULE = 3,
510                         GET_OBJECT = 4,
511                         GET_TYPE = 5,
512                         GET_NAME = 6
513                 }
514
515                 enum CmdModule {
516                         GET_INFO = 1,
517                 }
518
519                 enum CmdMethod {
520                         GET_NAME = 1,
521                         GET_DECLARING_TYPE = 2,
522                         GET_DEBUG_INFO = 3,
523                         GET_PARAM_INFO = 4,
524                         GET_LOCALS_INFO = 5,
525                         GET_INFO = 6,
526                         GET_BODY = 7,
527                         RESOLVE_TOKEN = 8,
528                         GET_CATTRS = 9,
529                         MAKE_GENERIC_METHOD = 10
530                 }
531
532                 enum CmdType {
533                         GET_INFO = 1,
534                         GET_METHODS = 2,
535                         GET_FIELDS = 3,
536                         GET_VALUES = 4,
537                         GET_OBJECT = 5,
538                         GET_SOURCE_FILES = 6,
539                         SET_VALUES = 7,
540                         IS_ASSIGNABLE_FROM = 8,
541                         GET_PROPERTIES = 9,
542                         GET_CATTRS = 10,
543                         GET_FIELD_CATTRS = 11,
544                         GET_PROPERTY_CATTRS = 12,
545                         /* FIXME: Merge into GET_SOURCE_FILES when the major protocol version is increased */
546                         GET_SOURCE_FILES_2 = 13,
547                         /* FIXME: Merge into GET_VALUES when the major protocol version is increased */
548                         GET_VALUES_2 = 14,
549                         CMD_TYPE_GET_METHODS_BY_NAME_FLAGS = 15,
550                         GET_INTERFACES = 16,
551                         GET_INTERFACE_MAP = 17,
552                         IS_INITIALIZED = 18
553                 }
554
555                 [Flags]
556                 enum BindingFlagsExtensions {
557                         BINDING_FLAGS_IGNORE_CASE = 0x70000000,
558                 }
559
560                 enum CmdStackFrame {
561                         GET_VALUES = 1,
562                         GET_THIS = 2,
563                         SET_VALUES = 3
564                 }
565
566                 enum CmdArrayRef {
567                         GET_LENGTH = 1,
568                         GET_VALUES = 2,
569                         SET_VALUES = 3
570                 }
571
572                 enum CmdStringRef {
573                         GET_VALUE = 1,
574                         GET_LENGTH = 2,
575                         GET_CHARS = 3
576                 }
577
578                 enum CmdObjectRef {
579                         GET_TYPE = 1,
580                         GET_VALUES = 2,
581                         IS_COLLECTED = 3,
582                         GET_ADDRESS = 4,
583                         GET_DOMAIN = 5,
584                         SET_VALUES = 6,
585                         GET_INFO = 7,
586                 }
587
588                 class Header {
589                         public int id;
590                         public int command_set;
591                         public int command;
592                         public int flags;
593                 }                       
594
595                 internal static int GetPacketLength (byte[] header) {
596                         int offset = 0;
597                         return decode_int (header, ref offset);
598                 }
599
600                 internal static bool IsReplyPacket (byte[] packet) {
601                         int offset = 8;
602                         return decode_byte (packet, ref offset) == 0x80;
603                 }
604
605                 internal static int GetPacketId (byte[] packet) {
606                         int offset = 4;
607                         return decode_int (packet, ref offset);
608                 }
609
610                 static int decode_byte (byte[] packet, ref int offset) {
611                         return packet [offset++];
612                 }
613
614                 static int decode_short (byte[] packet, ref int offset) {
615                         int res = ((int)packet [offset] << 8) | (int)packet [offset + 1];
616                         offset += 2;
617                         return res;
618                 }
619
620                 static int decode_int (byte[] packet, ref int offset) {
621                         int res = ((int)packet [offset] << 24) | ((int)packet [offset + 1] << 16) | ((int)packet [offset + 2] << 8) | (int)packet [offset + 3];
622                         offset += 4;
623                         return res;
624                 }
625
626                 static long decode_id (byte[] packet, ref int offset) {
627                         return decode_int (packet, ref offset);
628                 }
629
630                 static long decode_long (byte[] packet, ref int offset) {
631                         uint high = (uint)decode_int (packet, ref offset);
632                         uint low = (uint)decode_int (packet, ref offset);
633
634                         return (long)(((ulong)high << 32) | (ulong)low);
635                 }
636
637                 internal static SuspendPolicy decode_suspend_policy (int suspend_policy) {
638                         switch ((WPSuspendPolicy)suspend_policy) {
639                         case WPSuspendPolicy.NONE:
640                                 return SuspendPolicy.None;
641                         case WPSuspendPolicy.EVENT_THREAD:
642                                 return SuspendPolicy.EventThread;
643                         case WPSuspendPolicy.ALL:
644                                 return SuspendPolicy.All;
645                         default:
646                                 throw new NotImplementedException ();
647                         }
648                 }
649
650                 static Header decode_command_header (byte[] packet) {
651                         int offset = 0;
652                         Header res = new Header ();
653
654                         decode_int (packet, ref offset);
655                         res.id = decode_int (packet, ref offset);
656                         res.flags = decode_byte (packet, ref offset);
657                         res.command_set = decode_byte (packet, ref offset);
658                         res.command = decode_byte (packet, ref offset);
659
660                         return res;
661                 }
662
663                 static void encode_byte (byte[] buf, int b, ref int offset) {
664                         buf [offset] = (byte)b;
665                         offset ++;
666                 }
667
668                 static void encode_int (byte[] buf, int i, ref int offset) {
669                         buf [offset] = (byte)((i >> 24) & 0xff);
670                         buf [offset + 1] = (byte)((i >> 16) & 0xff);
671                         buf [offset + 2] = (byte)((i >> 8) & 0xff);
672                         buf [offset + 3] = (byte)((i >> 0) & 0xff);
673                         offset += 4;
674                 }
675
676                 static void encode_id (byte[] buf, long id, ref int offset) {
677                         encode_int (buf, (int)id, ref offset);
678                 }
679
680                 static void encode_long (byte[] buf, long l, ref int offset) {
681                         encode_int (buf, (int)((l >> 32) & 0xffffffff), ref offset);
682                         encode_int (buf, (int)(l & 0xffffffff), ref offset);
683                 }
684
685                 internal static byte[] EncodePacket (int id, int commandSet, int command, byte[] data, int dataLen) {
686                         byte[] buf = new byte [dataLen + 11];
687                         int offset = 0;
688                         
689                         encode_int (buf, buf.Length, ref offset);
690                         encode_int (buf, id, ref offset);
691                         encode_byte (buf, 0, ref offset);
692                         encode_byte (buf, commandSet, ref offset);
693                         encode_byte (buf, command, ref offset);
694
695                         for (int i = 0; i < dataLen; ++i)
696                                 buf [offset + i] = data [i];
697
698                         return buf;
699                 }
700
701                 class PacketReader {
702                         byte[] packet;
703                         int offset;
704
705                         public PacketReader (byte[] packet) {
706                                 this.packet = packet;
707
708                                 // For event packets
709                                 Header header = decode_command_header (packet);
710                                 CommandSet = (CommandSet)header.command_set;
711                                 Command = header.command;
712
713                                 // For reply packets
714                                 offset = 0;
715                                 ReadInt (); // length
716                                 ReadInt (); // id
717                                 ReadByte (); // flags
718                                 ErrorCode = ReadShort ();
719                         }
720
721                         public CommandSet CommandSet {
722                                 get; set;
723                         }
724
725                         public int Command {
726                                 get; set;
727                         }
728
729                         public int ErrorCode {
730                                 get; set;
731                         }
732
733                         public int Offset {
734                                 get {
735                                         return offset;
736                                 }
737                         }
738
739                         public int ReadByte () {
740                                 return decode_byte (packet, ref offset);
741                         }
742
743                         public int ReadShort () {
744                                 return decode_short (packet, ref offset);
745                         }
746
747                         public int ReadInt () {
748                                 return decode_int (packet, ref offset);
749                         }
750
751                         public long ReadId () {
752                                 return decode_id (packet, ref offset);
753                         }
754
755                         public long ReadLong () {
756                                 return decode_long (packet, ref offset);
757                         }
758
759                         public float ReadFloat () {
760                                 float f = DataConverter.FloatFromBE (packet, offset);
761                                 offset += 4;
762                                 return f;
763                         }
764
765                         public double ReadDouble () {
766                                 double d = DataConverter.DoubleFromBE (packet, offset);
767                                 offset += 8;
768                                 return d;
769                         }
770
771                         public string ReadString () {
772                                 int len = decode_int (packet, ref offset);
773                                 string res = new String (Encoding.UTF8.GetChars (packet, offset, len));
774                                 offset += len;
775                                 return res;
776                         }
777
778                         public ValueImpl ReadValue () {
779                                 ElementType etype = (ElementType)ReadByte ();
780
781                                 switch (etype) {
782                                 case ElementType.Void:
783                                         return new ValueImpl { Type = etype };
784                                 case ElementType.I1:
785                                         return new ValueImpl { Type = etype, Value = (sbyte)ReadInt () };
786                                 case ElementType.U1:
787                                         return new ValueImpl { Type = etype, Value = (byte)ReadInt () };
788                                 case ElementType.Boolean:
789                                         return new ValueImpl { Type = etype, Value = ReadInt () != 0 };
790                                 case ElementType.I2:
791                                         return new ValueImpl { Type = etype, Value = (short)ReadInt () };
792                                 case ElementType.U2:
793                                         return new ValueImpl { Type = etype, Value = (ushort)ReadInt () };
794                                 case ElementType.Char:
795                                         return new ValueImpl { Type = etype, Value = (char)ReadInt () };
796                                 case ElementType.I4:
797                                         return new ValueImpl { Type = etype, Value = ReadInt () };
798                                 case ElementType.U4:
799                                         return new ValueImpl { Type = etype, Value = (uint)ReadInt () };
800                                 case ElementType.I8:
801                                         return new ValueImpl { Type = etype, Value = ReadLong () };
802                                 case ElementType.U8:
803                                         return new ValueImpl { Type = etype, Value = (ulong)ReadLong () };
804                                 case ElementType.R4:
805                                         return new ValueImpl { Type = etype, Value = ReadFloat () };
806                                 case ElementType.R8:
807                                         return new ValueImpl { Type = etype, Value = ReadDouble () };
808                                 case ElementType.I:
809                                 case ElementType.U:
810                                 case ElementType.Ptr:
811                                         // FIXME: The client and the debuggee might have different word sizes
812                                         return new ValueImpl { Type = etype, Value = ReadLong () };
813                                 case ElementType.String:
814                                 case ElementType.SzArray:
815                                 case ElementType.Class:
816                                 case ElementType.Array:
817                                 case ElementType.Object:
818                                         long objid = ReadId ();
819                                         return new ValueImpl () { Type = etype, Objid = objid };
820                                 case ElementType.ValueType:
821                                         bool is_enum = ReadByte () == 1;
822                                         long klass = ReadId ();
823                                         long nfields = ReadInt ();
824                                         ValueImpl[] fields = new ValueImpl [nfields];
825                                         for (int i = 0; i < nfields; ++i)
826                                                 fields [i] = ReadValue ();
827                                         return new ValueImpl () { Type = etype, Klass = klass, Fields = fields, IsEnum = is_enum };
828                                 case (ElementType)ValueTypeId.VALUE_TYPE_ID_NULL:
829                                         return new ValueImpl { Type = etype };
830                                 case (ElementType)ValueTypeId.VALUE_TYPE_ID_TYPE:
831                                         return new ValueImpl () { Type = etype, Id = ReadId () };
832                                 default:
833                                         throw new NotImplementedException ("Unable to handle type " + etype);
834                                 }
835                         }
836
837                         public long[] ReadIds (int n) {
838                                 long[] res = new long [n];
839                                 for (int i = 0; i < n; ++i)
840                                         res [i] = ReadId ();
841                                 return res;
842                         }
843                 }
844
845                 class PacketWriter {
846
847                         byte[] data;
848                         int offset;
849
850                         public PacketWriter () {
851                                 data = new byte [1024];
852                                 offset = 0;
853                         }
854
855                         void MakeRoom (int size) {
856                                 if (offset + size >= data.Length) {
857                                         int new_len = data.Length * 2;
858                                         while (new_len < offset + size) {
859                                                 new_len *= 2;
860                                         }
861                                         byte[] new_data = new byte [new_len];
862                                         Array.Copy (data, new_data, data.Length);
863                                         data = new_data;
864                                 }
865                         }
866
867                         public PacketWriter WriteByte (byte val) {
868                                 MakeRoom (1);
869                                 encode_byte (data, val, ref offset);
870                                 return this;
871                         }
872
873                         public PacketWriter WriteInt (int val) {
874                                 MakeRoom (4);
875                                 encode_int (data, val, ref offset);
876                                 return this;
877                         }
878
879                         public PacketWriter WriteId (long id) {
880                                 MakeRoom (8);
881                                 encode_id (data, id, ref offset);
882                                 return this;
883                         }
884
885                         public PacketWriter WriteLong (long val) {
886                                 MakeRoom (8);
887                                 encode_long (data, val, ref offset);
888                                 return this;
889                         }
890
891                         public PacketWriter WriteFloat (float f) {
892                                 MakeRoom (8);
893                                 byte[] b = DataConverter.GetBytesBE (f);
894                                 for (int i = 0; i < 4; ++i)
895                                         data [offset + i] = b [i];
896                                 offset += 4;
897                                 return this;
898                         }
899
900                         public PacketWriter WriteDouble (double d) {
901                                 MakeRoom (8);
902                                 byte[] b = DataConverter.GetBytesBE (d);
903                                 for (int i = 0; i < 8; ++i)
904                                         data [offset + i] = b [i];
905                                 offset += 8;
906                                 return this;
907                         }
908
909                         public PacketWriter WriteInts (int[] ids) {
910                                 for (int i = 0; i < ids.Length; ++i)
911                                         WriteInt (ids [i]);
912                                 return this;
913                         }
914
915                         public PacketWriter WriteIds (long[] ids) {
916                                 for (int i = 0; i < ids.Length; ++i)
917                                         WriteId (ids [i]);
918                                 return this;
919                         }
920
921                         public PacketWriter WriteString (string s) {
922                                 if (s == null)
923                                         return WriteInt (-1);
924
925                                 byte[] b = Encoding.UTF8.GetBytes (s);
926                                 MakeRoom (4);
927                                 encode_int (data, b.Length, ref offset);
928                                 MakeRoom (b.Length);
929                                 Buffer.BlockCopy (b, 0, data, offset, b.Length);
930                                 offset += b.Length;
931                                 return this;
932                         }
933
934                         public PacketWriter WriteBool (bool val) {
935                                 WriteByte (val ? (byte)1 : (byte)0);
936                                 return this;
937                         }
938
939                         public PacketWriter WriteValue (ValueImpl v) {
940                                 ElementType t;
941
942                                 if (v.Value != null)
943                                         t = TypeCodeToElementType (Type.GetTypeCode (v.Value.GetType ()));
944                                 else
945                                         t = v.Type;
946                                 WriteByte ((byte)t);
947                                 switch (t) {
948                                 case ElementType.Boolean:
949                                         WriteInt ((bool)v.Value ? 1 : 0);
950                                         break;
951                                 case ElementType.Char:
952                                         WriteInt ((int)(char)v.Value);
953                                         break;
954                                 case ElementType.I1:
955                                         WriteInt ((int)(sbyte)v.Value);
956                                         break;
957                                 case ElementType.U1:
958                                         WriteInt ((int)(byte)v.Value);
959                                         break;
960                                 case ElementType.I2:
961                                         WriteInt ((int)(short)v.Value);
962                                         break;
963                                 case ElementType.U2:
964                                         WriteInt ((int)(ushort)v.Value);
965                                         break;
966                                 case ElementType.I4:
967                                         WriteInt ((int)(int)v.Value);
968                                         break;
969                                 case ElementType.U4:
970                                         WriteInt ((int)(uint)v.Value);
971                                         break;
972                                 case ElementType.I8:
973                                         WriteLong ((long)(long)v.Value);
974                                         break;
975                                 case ElementType.U8:
976                                         WriteLong ((long)(ulong)v.Value);
977                                         break;
978                                 case ElementType.R4:
979                                         WriteFloat ((float)v.Value);
980                                         break;
981                                 case ElementType.R8:
982                                         WriteDouble ((double)v.Value);
983                                         break;
984                                 case ElementType.String:
985                                 case ElementType.SzArray:
986                                 case ElementType.Class:
987                                 case ElementType.Array:
988                                 case ElementType.Object:
989                                         WriteId (v.Objid);
990                                         break;
991                                 case ElementType.ValueType:
992                                         // FIXME: 
993                                         if (v.IsEnum)
994                                                 throw new NotImplementedException ();
995                                         WriteByte (0);
996                                         WriteId (v.Klass);
997                                         WriteInt (v.Fields.Length);
998                                         for (int i = 0; i < v.Fields.Length; ++i)
999                                                 WriteValue (v.Fields [i]);
1000                                         break;
1001                                 case (ElementType)ValueTypeId.VALUE_TYPE_ID_NULL:
1002                                         break;
1003                                 default:
1004                                         throw new NotImplementedException ();
1005                                 }
1006
1007                                 return this;
1008                         }
1009
1010                         public PacketWriter WriteValues (ValueImpl[] values) {
1011                                 for (int i = 0; i < values.Length; ++i)
1012                                         WriteValue (values [i]);
1013                                 return this;
1014                         }
1015
1016                         public byte[] Data {
1017                                 get {
1018                                         return data;
1019                                 }
1020                         }
1021
1022                         public int Offset {
1023                                 get {
1024                                         return offset;
1025                                 }
1026                         }
1027                 }
1028
1029                 delegate void ReplyCallback (int packet_id, byte[] packet);
1030
1031                 bool closed;
1032                 Thread receiver_thread;
1033                 Dictionary<int, byte[]> reply_packets;
1034                 Dictionary<int, ReplyCallback> reply_cbs;
1035                 Dictionary<int, int> reply_cb_counts;
1036                 object reply_packets_monitor;
1037
1038                 internal event EventHandler<ErrorHandlerEventArgs> ErrorHandler;
1039
1040                 protected Connection () {
1041                         closed = false;
1042                         reply_packets = new Dictionary<int, byte[]> ();
1043                         reply_cbs = new Dictionary<int, ReplyCallback> ();
1044                         reply_cb_counts = new Dictionary<int, int> ();
1045                         reply_packets_monitor = new Object ();
1046                 }
1047                 
1048                 protected abstract int TransportReceive (byte[] buf, int buf_offset, int len);
1049                 protected abstract int TransportSend (byte[] buf, int buf_offset, int len);
1050                 protected abstract void TransportSetTimeouts (int send_timeout, int receive_timeout);
1051                 protected abstract void TransportClose ();
1052
1053                 internal VersionInfo Version;
1054                 
1055                 int Receive (byte[] buf, int buf_offset, int len) {
1056                         int offset = 0;
1057
1058                         while (offset < len) {
1059                                 int n = TransportReceive (buf, buf_offset + offset, len - offset);
1060
1061                                 if (n == 0)
1062                                         return offset;
1063                                 offset += n;
1064                         }
1065
1066                         return offset;
1067                 }
1068                 
1069                 // Do the wire protocol handshake
1070                 internal void Connect () {
1071                         byte[] buf = new byte [HANDSHAKE_STRING.Length];
1072                         char[] cbuf = new char [buf.Length];
1073
1074                         // FIXME: Add a timeout
1075                         int n = Receive (buf, 0, buf.Length);
1076                         if (n == 0)
1077                                 throw new IOException ("DWP Handshake failed.");
1078                         for (int i = 0; i < buf.Length; ++i)
1079                                 cbuf [i] = (char)buf [i];
1080
1081                         if (new String (cbuf) != HANDSHAKE_STRING)
1082                                 throw new IOException ("DWP Handshake failed.");
1083                         
1084                         TransportSend (buf, 0, buf.Length);
1085
1086                         receiver_thread = new Thread (new ThreadStart (receiver_thread_main));
1087                         receiver_thread.Name = "SDB Receiver";
1088                         receiver_thread.IsBackground = true;
1089                         receiver_thread.Start ();
1090
1091                         Version = VM_GetVersion ();
1092
1093                         //
1094                         // Tell the debuggee our protocol version, so newer debuggees can work
1095                         // with older clients
1096                         //
1097
1098                         //
1099                         // Older debuggees might not support this request
1100                         EventHandler<ErrorHandlerEventArgs> OrigErrorHandler = ErrorHandler;
1101                         ErrorHandler = null;
1102                         ErrorHandler += delegate (object sender, ErrorHandlerEventArgs args) {
1103                                 throw new NotSupportedException ();
1104                         };
1105                         try {
1106                                 VM_SetProtocolVersion (MAJOR_VERSION, MINOR_VERSION);
1107                         } catch (NotSupportedException) {
1108                         }
1109                         ErrorHandler = OrigErrorHandler;
1110                 }
1111
1112                 internal byte[] ReadPacket () {
1113                         // FIXME: Throw ClosedConnectionException () if the connection is closed
1114                         // FIXME: Throw ClosedConnectionException () if another thread closes the connection
1115                         // FIXME: Locking
1116                         byte[] header = new byte [HEADER_LENGTH];
1117
1118                         int len = Receive (header, 0, header.Length);
1119                         if (len == 0)
1120                                 return new byte [0];
1121                         if (len != HEADER_LENGTH) {
1122                                 // FIXME:
1123                                 throw new IOException ("Packet of length " + len + " is read.");
1124                         }
1125
1126                         int packetLength = GetPacketLength (header);
1127                         if (packetLength < 11)
1128                                 throw new IOException ("Invalid packet length.");
1129
1130                         if (packetLength == 11) {
1131                                 return header;
1132                         } else {
1133                                 byte[] buf = new byte [packetLength];
1134                                 for (int i = 0; i < header.Length; ++i)
1135                                         buf [i] = header [i];
1136                                 len = Receive (buf, header.Length, packetLength - header.Length);
1137                                 if (len != packetLength - header.Length)
1138                                         throw new IOException ();
1139                                 return buf;
1140                         }
1141                 }
1142
1143                 internal void WritePacket (byte[] packet) {
1144                         // FIXME: Throw ClosedConnectionException () if the connection is closed
1145                         // FIXME: Throw ClosedConnectionException () if another thread closes the connection
1146                         // FIXME: Locking
1147                         TransportSend (packet, 0, packet.Length);
1148                 }
1149
1150                 internal void Close () {
1151                         closed = true;
1152                 }
1153
1154                 internal bool IsClosed {
1155                         get {
1156                                 return closed;
1157                         }
1158                 }
1159
1160                 bool disconnected;
1161
1162                 void receiver_thread_main () {
1163                         while (!closed) {
1164                                 try {
1165                                         bool res = ReceivePacket ();
1166                                         if (!res)
1167                                                 break;
1168                                 } catch (Exception ex) {
1169                                         if (!closed) {
1170                                                 Console.WriteLine (ex);
1171                                         }
1172                                         break;
1173                                 }
1174                         }
1175
1176                         lock (reply_packets_monitor) {
1177                                 disconnected = true;
1178                                 Monitor.PulseAll (reply_packets_monitor);
1179                                 TransportClose ();
1180                         }
1181                         EventHandler.VMDisconnect (0, 0, null);
1182                 }
1183
1184                 bool ReceivePacket () {
1185                                 byte[] packet = ReadPacket ();
1186
1187                                 if (packet.Length == 0) {
1188                                         return false;
1189                                 }
1190
1191                                 if (IsReplyPacket (packet)) {
1192                                         int id = GetPacketId (packet);
1193                                         ReplyCallback cb = null;
1194                                         lock (reply_packets_monitor) {
1195                                                 reply_cbs.TryGetValue (id, out cb);
1196                                                 if (cb == null) {
1197                                                         reply_packets [id] = packet;
1198                                                         Monitor.PulseAll (reply_packets_monitor);
1199                                                 } else {
1200                                                         int c = reply_cb_counts [id];
1201                                                         c --;
1202                                                         if (c == 0) {
1203                                                                 reply_cbs.Remove (id);
1204                                                                 reply_cb_counts.Remove (id);
1205                                                         }
1206                                                 }
1207                                         }
1208
1209                                         if (cb != null)
1210                                                 cb.Invoke (id, packet);
1211                                 } else {
1212                                         PacketReader r = new PacketReader (packet);
1213
1214                                         if (r.CommandSet == CommandSet.EVENT && r.Command == (int)CmdEvent.COMPOSITE) {
1215                                                 int spolicy = r.ReadByte ();
1216                                                 int nevents = r.ReadInt ();
1217
1218                                                 SuspendPolicy suspend_policy = decode_suspend_policy (spolicy);
1219
1220                                                 EventInfo[] events = new EventInfo [nevents];
1221
1222                                                 for (int i = 0; i < nevents; ++i) {
1223                                                         EventKind kind = (EventKind)r.ReadByte ();
1224                                                         int req_id = r.ReadInt ();
1225
1226                                                         EventType etype = (EventType)kind;
1227
1228                                                         if (kind == EventKind.VM_START) {
1229                                                                 long thread_id = r.ReadId ();
1230                                                                 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id };
1231                                                                 //EventHandler.VMStart (req_id, thread_id, null);
1232                                                         } else if (kind == EventKind.VM_DEATH) {
1233                                                                 //EventHandler.VMDeath (req_id, 0, null);
1234                                                                 events [i] = new EventInfo (etype, req_id) { };
1235                                                         } else if (kind == EventKind.THREAD_START) {
1236                                                                 long thread_id = r.ReadId ();
1237                                                                 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = thread_id };
1238                                                                 //EventHandler.ThreadStart (req_id, thread_id, thread_id);
1239                                                         } else if (kind == EventKind.THREAD_DEATH) {
1240                                                                 long thread_id = r.ReadId ();
1241                                                                 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = thread_id };
1242                                                                 //EventHandler.ThreadDeath (req_id, thread_id, thread_id);
1243                                                         } else if (kind == EventKind.ASSEMBLY_LOAD) {
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.AssemblyLoad (req_id, thread_id, id);
1248                                                         } else if (kind == EventKind.ASSEMBLY_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.AssemblyUnload (req_id, thread_id, id);
1253                                                         } else if (kind == EventKind.TYPE_LOAD) {
1254                                                                 long thread_id = r.ReadId ();
1255                                                                 long id = r.ReadId ();
1256                                                                 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1257                                                                 //EventHandler.TypeLoad (req_id, thread_id, id);
1258                                                         } else if (kind == EventKind.METHOD_ENTRY) {
1259                                                                 long thread_id = r.ReadId ();
1260                                                                 long id = r.ReadId ();
1261                                                                 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1262                                                                 //EventHandler.MethodEntry (req_id, thread_id, id);
1263                                                         } else if (kind == EventKind.METHOD_EXIT) {
1264                                                                 long thread_id = r.ReadId ();
1265                                                                 long id = r.ReadId ();
1266                                                                 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1267                                                                 //EventHandler.MethodExit (req_id, thread_id, id);
1268                                                         } else if (kind == EventKind.BREAKPOINT) {
1269                                                                 long thread_id = r.ReadId ();
1270                                                                 long id = r.ReadId ();
1271                                                                 long loc = r.ReadLong ();
1272                                                                 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1273                                                                 //EventHandler.Breakpoint (req_id, thread_id, id, loc);
1274                                                         } else if (kind == EventKind.STEP) {
1275                                                                 long thread_id = r.ReadId ();
1276                                                                 long id = r.ReadId ();
1277                                                                 long loc = r.ReadLong ();
1278                                                                 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1279                                                                 //EventHandler.Step (req_id, thread_id, id, loc);
1280                                                         } else if (kind == EventKind.EXCEPTION) {
1281                                                                 long thread_id = r.ReadId ();
1282                                                                 long id = r.ReadId ();
1283                                                                 long loc = 0; // FIXME
1284                                                                 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1285                                                                 //EventHandler.Exception (req_id, thread_id, id, loc);
1286                                                         } else if (kind == EventKind.APPDOMAIN_CREATE) {
1287                                                                 long thread_id = r.ReadId ();
1288                                                                 long id = r.ReadId ();
1289                                                                 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1290                                                                 //EventHandler.AppDomainCreate (req_id, thread_id, id);
1291                                                         } else if (kind == EventKind.APPDOMAIN_UNLOAD) {
1292                                                                 long thread_id = r.ReadId ();
1293                                                                 long id = r.ReadId ();
1294                                                                 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1295                                                                 //EventHandler.AppDomainUnload (req_id, thread_id, id);
1296                                                         } else if (kind == EventKind.USER_BREAK) {
1297                                                                 long thread_id = r.ReadId ();
1298                                                                 long id = 0;
1299                                                                 long loc = 0;
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.USER_LOG) {
1303                                                                 long thread_id = r.ReadId ();
1304                                                                 int level = r.ReadInt ();
1305                                                                 string category = r.ReadString ();
1306                                                                 string message = r.ReadString ();
1307                                                                 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Level = level, Category = category, Message = message };
1308                                                                 //EventHandler.Exception (req_id, thread_id, id, loc);
1309                                                         } else if (kind == EventKind.KEEPALIVE) {
1310                                                                 events [i] = new EventInfo (etype, req_id) { };
1311                                                         } else {
1312                                                                 throw new NotImplementedException ("Unknown event kind: " + kind);
1313                                                         }
1314                                                 }
1315
1316                                                 EventHandler.Events (suspend_policy, events);
1317                                         }
1318                                 }
1319
1320                                 return true;
1321                 }
1322
1323                 internal IEventHandler EventHandler {
1324                         get; set;
1325                 }
1326
1327                 static String CommandString (CommandSet command_set, int command)
1328                 {
1329                         string cmd;
1330                         switch (command_set) {
1331                         case CommandSet.VM:
1332                                 cmd = ((CmdVM)command).ToString ();
1333                                 break;
1334                         case CommandSet.OBJECT_REF:
1335                                 cmd = ((CmdObjectRef)command).ToString ();
1336                                 break;
1337                         case CommandSet.STRING_REF:
1338                                 cmd = ((CmdStringRef)command).ToString ();
1339                                 break;
1340                         case CommandSet.THREAD:
1341                                 cmd = ((CmdThread)command).ToString ();
1342                                 break;
1343                         case CommandSet.ARRAY_REF:
1344                                 cmd = ((CmdArrayRef)command).ToString ();
1345                                 break;
1346                         case CommandSet.EVENT_REQUEST:
1347                                 cmd = ((CmdEventRequest)command).ToString ();
1348                                 break;
1349                         case CommandSet.STACK_FRAME:
1350                                 cmd = ((CmdStackFrame)command).ToString ();
1351                                 break;
1352                         case CommandSet.APPDOMAIN:
1353                                 cmd = ((CmdAppDomain)command).ToString ();
1354                                 break;
1355                         case CommandSet.ASSEMBLY:
1356                                 cmd = ((CmdAssembly)command).ToString ();
1357                                 break;
1358                         case CommandSet.METHOD:
1359                                 cmd = ((CmdMethod)command).ToString ();
1360                                 break;
1361                         case CommandSet.TYPE:
1362                                 cmd = ((CmdType)command).ToString ();
1363                                 break;
1364                         case CommandSet.MODULE:
1365                                 cmd = ((CmdModule)command).ToString ();
1366                                 break;
1367                         case CommandSet.EVENT:
1368                                 cmd = ((CmdEvent)command).ToString ();
1369                                 break;
1370                         default:
1371                                 cmd = command.ToString ();
1372                                 break;
1373                         }
1374                         return string.Format ("[{0} {1}]", command_set, cmd);
1375                 }
1376
1377                 long total_protocol_ticks;
1378
1379                 void LogPacket (int packet_id, byte[] encoded_packet, byte[] reply_packet, CommandSet command_set, int command, Stopwatch watch) {
1380                         watch.Stop ();
1381                         total_protocol_ticks += watch.ElapsedTicks;
1382                         var ts = TimeSpan.FromTicks (total_protocol_ticks);
1383                         string msg = string.Format ("Packet: {0} sent: {1} received: {2} ms: {3} total ms: {4} {5}",
1384                            packet_id, encoded_packet.Length, reply_packet.Length, watch.ElapsedMilliseconds,
1385                            (ts.Seconds * 1000) + ts.Milliseconds,
1386                            CommandString (command_set, command));
1387
1388                         LoggingStream.WriteLine (msg);
1389                         LoggingStream.Flush ();
1390                 }
1391
1392                 /* Send a request and call cb when a result is received */
1393                 int Send (CommandSet command_set, int command, PacketWriter packet, Action<PacketReader> cb, int count) {
1394                         int id = IdGenerator;
1395
1396                         Stopwatch watch = null;
1397                         if (EnableConnectionLogging)
1398                                 watch = Stopwatch.StartNew ();
1399
1400                         byte[] encoded_packet;
1401                         if (packet == null)
1402                                 encoded_packet = EncodePacket (id, (int)command_set, command, null, 0);
1403                         else
1404                                 encoded_packet = EncodePacket (id, (int)command_set, command, packet.Data, packet.Offset);
1405
1406                         lock (reply_packets_monitor) {
1407                                 reply_cbs [id] = delegate (int packet_id, byte[] p) {
1408                                         if (EnableConnectionLogging)
1409                                                 LogPacket (packet_id, encoded_packet, p, command_set, command, watch);
1410                                         /* Run the callback on a tp thread to avoid blocking the receive thread */
1411                                         PacketReader r = new PacketReader (p);
1412                                         cb.BeginInvoke (r, null, null);
1413                                 };
1414                                 reply_cb_counts [id] = count;
1415                         }
1416
1417                         WritePacket (encoded_packet);
1418
1419                         return id;
1420                 }
1421
1422                 PacketReader SendReceive (CommandSet command_set, int command, PacketWriter packet) {
1423                         int id = IdGenerator;
1424                         Stopwatch watch = null;
1425
1426                         if (disconnected)
1427                                 throw new VMDisconnectedException ();
1428
1429                         if (EnableConnectionLogging)
1430                                 watch = Stopwatch.StartNew ();
1431
1432                         byte[] encoded_packet;
1433
1434                         if (packet == null)
1435                                 encoded_packet = EncodePacket (id, (int)command_set, command, null, 0);
1436                         else
1437                                 encoded_packet = EncodePacket (id, (int)command_set, command, packet.Data, packet.Offset);
1438
1439                         WritePacket (encoded_packet);
1440
1441                         int packetId = id;
1442
1443                         /* Wait for the reply packet */
1444                         while (true) {
1445                                 lock (reply_packets_monitor) {
1446                                         if (reply_packets.ContainsKey (packetId)) {
1447                                                 byte[] reply = reply_packets [packetId];
1448                                                 reply_packets.Remove (packetId);
1449                                                 PacketReader r = new PacketReader (reply);
1450
1451                                                 if (EnableConnectionLogging)
1452                                                         LogPacket (packetId, encoded_packet, reply, command_set, command, watch);
1453                                                 if (r.ErrorCode != 0) {
1454                                                         if (ErrorHandler != null)
1455                                                                 ErrorHandler (this, new ErrorHandlerEventArgs () { ErrorCode = (ErrorCode)r.ErrorCode });
1456                                                         throw new NotImplementedException ("No error handler set.");
1457                                                 } else {
1458                                                         return r;
1459                                                 }
1460                                         } else {
1461                                                 if (disconnected)
1462                                                         throw new VMDisconnectedException ();
1463                                                 Monitor.Wait (reply_packets_monitor);
1464                                         }
1465                                 }
1466                         }
1467                 }
1468
1469                 PacketReader SendReceive (CommandSet command_set, int command) {
1470                         return SendReceive (command_set, command, null);
1471                 }
1472
1473                 int packet_id_generator;
1474
1475                 int IdGenerator {
1476                         get {
1477                                 return Interlocked.Increment (ref packet_id_generator);
1478                         }
1479                 }
1480
1481                 CattrInfo[] ReadCattrs (PacketReader r) {
1482                         CattrInfo[] res = new CattrInfo [r.ReadInt ()];
1483                         for (int i = 0; i < res.Length; ++i) {
1484                                 CattrInfo info = new CattrInfo ();
1485                                 info.ctor_id = r.ReadId ();
1486                                 info.ctor_args = new ValueImpl [r.ReadInt ()];
1487                                 for (int j = 0; j < info.ctor_args.Length; ++j) {
1488                                         info.ctor_args [j] = r.ReadValue ();
1489                                 }
1490                                 info.named_args = new CattrNamedArgInfo [r.ReadInt ()];
1491                                 for (int j = 0; j < info.named_args.Length; ++j) {
1492                                         CattrNamedArgInfo arg = new CattrNamedArgInfo ();
1493                                         int arg_type = r.ReadByte ();
1494                                         arg.is_property = arg_type == 0x54;
1495                                         arg.id = r.ReadId ();
1496                                         arg.value = r.ReadValue ();
1497                                         info.named_args [j] = arg;
1498                                 }
1499                                 res [i] = info;
1500                         }
1501                         return res;
1502                 }
1503
1504                 static ElementType TypeCodeToElementType (TypeCode c) {
1505                         switch (c) {
1506                         case TypeCode.Boolean:
1507                                 return ElementType.Boolean;
1508                         case TypeCode.Char:
1509                                 return ElementType.Char;
1510                         case TypeCode.SByte:
1511                                 return ElementType.I1;
1512                         case TypeCode.Byte:
1513                                 return ElementType.U1;
1514                         case TypeCode.Int16:
1515                                 return ElementType.I2;
1516                         case TypeCode.UInt16:
1517                                 return ElementType.U2;
1518                         case TypeCode.Int32:
1519                                 return ElementType.I4;
1520                         case TypeCode.UInt32:
1521                                 return ElementType.U4;
1522                         case TypeCode.Int64:
1523                                 return ElementType.I8;
1524                         case TypeCode.UInt64:
1525                                 return ElementType.U8;
1526                         case TypeCode.Single:
1527                                 return ElementType.R4;
1528                         case TypeCode.Double:
1529                                 return ElementType.R8;
1530                         default:
1531                                 throw new NotImplementedException ();
1532                         }
1533                 }
1534
1535                 /*
1536                  * Implementation of debugger commands
1537                  */
1538
1539                 internal VersionInfo VM_GetVersion () {
1540                         var res = SendReceive (CommandSet.VM, (int)CmdVM.VERSION, null);
1541                         VersionInfo info = new VersionInfo ();
1542                         info.VMVersion = res.ReadString ();
1543                         info.MajorVersion = res.ReadInt ();
1544                         info.MinorVersion = res.ReadInt ();
1545                         return info;
1546                 }
1547
1548                 internal void VM_SetProtocolVersion (int major, int minor) {
1549                         SendReceive (CommandSet.VM, (int)CmdVM.SET_PROTOCOL_VERSION, new PacketWriter ().WriteInt (major).WriteInt (minor));
1550                 }
1551
1552                 internal long[] VM_GetThreads () {
1553                         var res = SendReceive (CommandSet.VM, (int)CmdVM.ALL_THREADS, null);
1554                         int len = res.ReadInt ();
1555                         long[] arr = new long [len];
1556                         for (int i = 0; i < len; ++i)
1557                                 arr [i] = res.ReadId ();
1558                         return arr;
1559                 }
1560
1561                 internal void VM_Suspend () {
1562                         SendReceive (CommandSet.VM, (int)CmdVM.SUSPEND);
1563                 }
1564
1565                 internal void VM_Resume () {
1566                         SendReceive (CommandSet.VM, (int)CmdVM.RESUME);
1567                 }
1568
1569                 internal void VM_Exit (int exitCode) {
1570                         SendReceive (CommandSet.VM, (int)CmdVM.EXIT, new PacketWriter ().WriteInt (exitCode));
1571                 }
1572
1573                 internal void VM_Dispose () {
1574                         SendReceive (CommandSet.VM, (int)CmdVM.DISPOSE);
1575                 }
1576
1577                 internal ValueImpl VM_InvokeMethod (long thread, long method, ValueImpl this_arg, ValueImpl[] arguments, InvokeFlags flags, out ValueImpl exc) {
1578                         exc = null;
1579                         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));
1580                         if (r.ReadByte () == 0) {
1581                                 exc = r.ReadValue ();
1582                                 return null;
1583                         } else {
1584                                 return r.ReadValue ();
1585                         }
1586                 }
1587
1588                 internal delegate void InvokeMethodCallback (ValueImpl v, ValueImpl exc, ErrorCode error, object state);
1589
1590                 internal int VM_BeginInvokeMethod (long thread, long method, ValueImpl this_arg, ValueImpl[] arguments, InvokeFlags flags, InvokeMethodCallback callback, object state) {
1591                         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) {
1592                                         ValueImpl v, exc;
1593
1594                                         if (r.ErrorCode != 0) {
1595                                                 callback (null, null, (ErrorCode)r.ErrorCode, state);
1596                                         } else {
1597                                                 if (r.ReadByte () == 0) {
1598                                                         exc = r.ReadValue ();
1599                                                         v = null;
1600                                                 } else {
1601                                                         v = r.ReadValue ();
1602                                                         exc = null;
1603                                                 }
1604
1605                                                 callback (v, exc, 0, state);
1606                                         }
1607                                 }, 1);
1608                 }
1609
1610                 internal int VM_BeginInvokeMethods (long thread, long[] methods, ValueImpl this_arg, List<ValueImpl[]> arguments, InvokeFlags flags, InvokeMethodCallback callback, object state) {
1611                         // FIXME: Merge this with INVOKE_METHOD
1612                         var w = new PacketWriter ();
1613                         w.WriteId (thread);
1614                         w.WriteInt ((int)flags);
1615                         w.WriteInt (methods.Length);
1616                         for (int i = 0; i < methods.Length; ++i) {
1617                                 w.WriteId (methods [i]);
1618                                 w.WriteValue (this_arg);
1619                                 w.WriteInt (arguments [i].Length);
1620                                 w.WriteValues (arguments [i]);
1621                         }
1622                         return Send (CommandSet.VM, (int)CmdVM.INVOKE_METHODS, w, delegate (PacketReader r) {
1623                                         ValueImpl v, exc;
1624
1625                                         if (r.ErrorCode != 0) {
1626                                                 callback (null, null, (ErrorCode)r.ErrorCode, state);
1627                                         } else {
1628                                                 if (r.ReadByte () == 0) {
1629                                                         exc = r.ReadValue ();
1630                                                         v = null;
1631                                                 } else {
1632                                                         v = r.ReadValue ();
1633                                                         exc = null;
1634                                                 }
1635
1636                                                 callback (v, exc, 0, state);
1637                                         }
1638                                 }, methods.Length);
1639                 }
1640
1641                 internal void VM_AbortInvoke (long thread, int id)
1642                 {
1643                         SendReceive (CommandSet.VM, (int)CmdVM.ABORT_INVOKE, new PacketWriter ().WriteId (thread).WriteInt (id));
1644                 }
1645
1646                 internal void SetSocketTimeouts (int send_timeout, int receive_timeout, int keepalive_interval)
1647                 {
1648                         TransportSetTimeouts (send_timeout, receive_timeout);
1649                         SendReceive (CommandSet.VM, (int)CmdVM.SET_KEEPALIVE, new PacketWriter ().WriteId (keepalive_interval));
1650                 }
1651
1652                 internal long[] VM_GetTypesForSourceFile (string fname, bool ignoreCase) {
1653                         var res = SendReceive (CommandSet.VM, (int)CmdVM.GET_TYPES_FOR_SOURCE_FILE, new PacketWriter ().WriteString (fname).WriteBool (ignoreCase));
1654                         int count = res.ReadInt ();
1655                         long[] types = new long [count];
1656                         for (int i = 0; i < count; ++i)
1657                                 types [i] = res.ReadId ();
1658                         return types;
1659                 }
1660
1661                 internal long[] VM_GetTypes (string name, bool ignoreCase) {
1662                         var res = SendReceive (CommandSet.VM, (int)CmdVM.GET_TYPES, new PacketWriter ().WriteString (name).WriteBool (ignoreCase));
1663                         int count = res.ReadInt ();
1664                         long[] types = new long [count];
1665                         for (int i = 0; i < count; ++i)
1666                                 types [i] = res.ReadId ();
1667                         return types;
1668                 }
1669
1670                 /*
1671                  * DOMAIN
1672                  */
1673
1674                 internal long RootDomain {
1675                         get {
1676                                 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_ROOT_DOMAIN, null).ReadId ();
1677                         }
1678                 }
1679
1680                 internal string Domain_GetName (long id) {
1681                         return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_FRIENDLY_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1682                 }
1683
1684                 internal long[] Domain_GetAssemblies (long id) {
1685                         var res = SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_ASSEMBLIES, new PacketWriter ().WriteId (id));
1686                         int count = res.ReadInt ();
1687                         long[] assemblies = new long [count];
1688                         for (int i = 0; i < count; ++i)
1689                                 assemblies [i] = res.ReadId ();
1690                         return assemblies;
1691                 }
1692
1693                 internal long Domain_GetEntryAssembly (long id) {
1694                         return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_ENTRY_ASSEMBLY, new PacketWriter ().WriteId (id)).ReadId ();
1695                 }
1696
1697                 internal long Domain_GetCorlib (long id) {
1698                         return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_CORLIB, new PacketWriter ().WriteId (id)).ReadId ();
1699                 }
1700
1701                 internal long Domain_CreateString (long id, string s) {
1702                         return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.CREATE_STRING, new PacketWriter ().WriteId (id).WriteString (s)).ReadId ();
1703                 }
1704
1705                 internal long Domain_CreateBoxedValue (long id, long type_id, ValueImpl v) {
1706                         return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.CREATE_BOXED_VALUE, new PacketWriter ().WriteId (id).WriteId (type_id).WriteValue (v)).ReadId ();
1707                 }
1708
1709                 /*
1710                  * METHOD
1711                  */
1712
1713                 internal string Method_GetName (long id) {
1714                         return SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1715                 }
1716
1717                 internal long Method_GetDeclaringType (long id) {
1718                         return SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_DECLARING_TYPE, new PacketWriter ().WriteId (id)).ReadId ();
1719                 }
1720
1721                 internal DebugInfo Method_GetDebugInfo (long id) {
1722                         var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_DEBUG_INFO, new PacketWriter ().WriteId (id));
1723
1724                         DebugInfo info = new DebugInfo ();
1725                         info.max_il_offset = res.ReadInt ();
1726
1727                         SourceInfo[] sources = null;
1728                         if (Version.AtLeast (2, 13)) {
1729                                 int n = res.ReadInt ();
1730                                 sources = new SourceInfo [n];
1731                                 for (int i = 0; i < n; ++i) {
1732                                         sources [i].source_file = res.ReadString ();
1733                                         if (Version.AtLeast (2, 14)) {
1734                                                 sources [i].hash = new byte [16];
1735                                                 for (int j = 0; j < 16; ++j)
1736                                                         sources [i].hash [j] = (byte)res.ReadByte ();
1737                                         }
1738                                 }
1739                         } else {
1740                                 sources = new SourceInfo [1];
1741                                 sources [0].source_file = res.ReadString ();
1742                         }
1743
1744                         int n_il_offsets = res.ReadInt ();
1745                         info.il_offsets = new int [n_il_offsets];
1746                         info.line_numbers = new int [n_il_offsets];
1747                         info.source_files = new SourceInfo [n_il_offsets];
1748                         info.column_numbers = new int [n_il_offsets];
1749                         for (int i = 0; i < n_il_offsets; ++i) {
1750                                 info.il_offsets [i] = res.ReadInt ();
1751                                 info.line_numbers [i] = res.ReadInt ();
1752                                 if (Version.AtLeast (2, 12)) {
1753                                         int idx = res.ReadInt ();
1754                                         info.source_files [i] = idx >= 0 ? sources [idx] : default (SourceInfo);
1755                                 } else {
1756                                         info.source_files [i] = sources [0];
1757                                 }
1758                                 if (Version.AtLeast (2, 19))
1759                                         info.column_numbers [i] = res.ReadInt ();
1760                                 else
1761                                         info.column_numbers [i] = 0;
1762                         }
1763
1764                         return info;
1765                 }
1766
1767                 internal ParamInfo Method_GetParamInfo (long id) {
1768                         var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_PARAM_INFO, new PacketWriter ().WriteId (id));
1769
1770                         ParamInfo info = new ParamInfo ();
1771                         info.call_conv = res.ReadInt ();
1772                         info.param_count = res.ReadInt ();
1773                         info.generic_param_count = res.ReadInt ();
1774                         info.ret_type = res.ReadId ();
1775                         info.param_types = new long [info.param_count];
1776                         for (int i = 0; i < info.param_count; ++i)
1777                                 info.param_types [i] = res.ReadId ();
1778                         info.param_names = new string [info.param_count];                       
1779                         for (int i = 0; i < info.param_count; ++i)
1780                                 info.param_names [i] = res.ReadString ();
1781
1782                         return info;
1783                 }
1784
1785                 internal LocalsInfo Method_GetLocalsInfo (long id) {
1786                         var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_LOCALS_INFO, new PacketWriter ().WriteId (id));
1787
1788                         LocalsInfo info = new LocalsInfo ();
1789                         int nlocals = res.ReadInt ();
1790                         info.types = new long [nlocals];
1791                         for (int i = 0; i < nlocals; ++i)
1792                                 info.types [i] = res.ReadId ();
1793                         info.names = new string [nlocals];
1794                         for (int i = 0; i < nlocals; ++i)
1795                                 info.names [i] = res.ReadString ();
1796                         info.live_range_start = new int [nlocals];
1797                         info.live_range_end = new int [nlocals];
1798                         for (int i = 0; i < nlocals; ++i) {
1799                                 info.live_range_start [i] = res.ReadInt ();
1800                                 info.live_range_end [i] = res.ReadInt ();
1801                         }
1802
1803                         return info;
1804                 }
1805
1806                 internal MethodInfo Method_GetInfo (long id) {
1807                         var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_INFO, new PacketWriter ().WriteId (id));
1808
1809                         MethodInfo info = new MethodInfo ();
1810                         info.attributes = res.ReadInt ();
1811                         info.iattributes = res.ReadInt ();
1812                         info.token = res.ReadInt ();
1813                         if (Version.AtLeast (2, 12)) {
1814                                 int attrs = res.ReadByte ();
1815                                 if ((attrs & (1 << 0)) != 0)
1816                                         info.is_gmd = true;
1817                                 if ((attrs & (1 << 1)) != 0)
1818                                         info.is_generic_method = true;
1819                                 info.gmd = res.ReadId ();
1820                                 if (Version.AtLeast (2, 15)) {
1821                                         if (info.is_generic_method) {
1822                                                 int n = res.ReadInt ();
1823                                                 info.type_args = res.ReadIds (n);
1824                                         }
1825                                 }
1826                         }
1827                         return info;
1828                 }
1829
1830                 internal MethodBodyInfo Method_GetBody (long id) {
1831                         var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_BODY, new PacketWriter ().WriteId (id));
1832
1833                         MethodBodyInfo info = new MethodBodyInfo ();
1834                         info.il = new byte [res.ReadInt ()];
1835                         for (int i = 0; i < info.il.Length; ++i)
1836                                 info.il [i] = (byte)res.ReadByte ();
1837
1838                         if (Version.AtLeast (2, 18)) {
1839                                 info.clauses = new ExceptionClauseInfo [res.ReadInt ()];
1840
1841                                 for (int i = 0; i < info.clauses.Length; ++i) {
1842                                         var clause = new ExceptionClauseInfo {
1843                                                 flags = (ExceptionClauseFlags) res.ReadInt (),
1844                                                 try_offset = res.ReadInt (),
1845                                                 try_length = res.ReadInt (),
1846                                                 handler_offset = res.ReadInt (),
1847                                                 handler_length = res.ReadInt (),
1848                                         };
1849
1850                                         if (clause.flags == ExceptionClauseFlags.None)
1851                                                 clause.catch_type_id = res.ReadId ();
1852                                         else if (clause.flags == ExceptionClauseFlags.Filter)
1853                                                 clause.filter_offset = res.ReadInt ();
1854
1855                                         info.clauses [i] = clause;
1856                                 }
1857                         } else
1858                                 info.clauses = new ExceptionClauseInfo [0];
1859
1860                         return info;
1861                 }
1862
1863                 internal ResolvedToken Method_ResolveToken (long id, int token) {
1864                         var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.RESOLVE_TOKEN, new PacketWriter ().WriteId (id).WriteInt (token));
1865
1866                         TokenType type = (TokenType)res.ReadByte ();
1867                         switch (type) {
1868                         case TokenType.STRING:
1869                                 return new ResolvedToken () { Type = type, Str = res.ReadString () };
1870                         case TokenType.TYPE:
1871                         case TokenType.METHOD:
1872                         case TokenType.FIELD:
1873                                 return new ResolvedToken () { Type = type, Id = res.ReadId () };
1874                         case TokenType.UNKNOWN:
1875                                 return new ResolvedToken () { Type = type };
1876                         default:
1877                                 throw new NotImplementedException ();
1878                         }
1879                 }
1880
1881                 internal CattrInfo[] Method_GetCustomAttributes (long id, long attr_type_id, bool inherit) {
1882                         PacketReader r = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_CATTRS, new PacketWriter ().WriteId (id).WriteId (attr_type_id));
1883                         return ReadCattrs (r);
1884                 }
1885
1886                 internal long Method_MakeGenericMethod (long id, long[] args) {
1887                         PacketReader r = SendReceive (CommandSet.METHOD, (int)CmdMethod.MAKE_GENERIC_METHOD, new PacketWriter ().WriteId (id).WriteInt (args.Length).WriteIds (args));
1888                         return r.ReadId ();
1889                 }
1890
1891                 /*
1892                  * THREAD
1893                  */
1894
1895                 internal string Thread_GetName (long id) {
1896                         return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1897                 }
1898
1899                 internal FrameInfo[] Thread_GetFrameInfo (long id, int start_frame, int length) {
1900                         var res = SendReceive (CommandSet.THREAD, (int)CmdThread.GET_FRAME_INFO, new PacketWriter ().WriteId (id).WriteInt (start_frame).WriteInt (length));
1901                         int count = res.ReadInt ();
1902
1903                         var frames = new FrameInfo [count];
1904                         for (int i = 0; i < count; ++i) {
1905                                 var f = new FrameInfo ();
1906                                 f.id = res.ReadInt ();
1907                                 f.method = res.ReadId ();
1908                                 f.il_offset = res.ReadInt ();
1909                                 f.flags = (StackFrameFlags)res.ReadByte ();
1910                                 frames [i] = f;
1911                         }
1912
1913                         return frames;
1914                 }
1915
1916                 internal int Thread_GetState (long id) {
1917                         return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_STATE, new PacketWriter ().WriteId (id)).ReadInt ();
1918                 }
1919
1920                 internal ThreadInfo Thread_GetInfo (long id) {
1921                         PacketReader r = SendReceive (CommandSet.THREAD, (int)CmdThread.GET_INFO, new PacketWriter ().WriteId (id));
1922
1923                         ThreadInfo res = new ThreadInfo () { is_thread_pool = r.ReadByte () > 0 ? true : false };
1924
1925                         return res;
1926                 }
1927
1928                 internal long Thread_GetId (long id) {
1929                         return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_ID, new PacketWriter ().WriteId (id)).ReadLong ();
1930                 }
1931
1932                 internal long Thread_GetTID (long id) {
1933                         return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_TID, new PacketWriter ().WriteId (id)).ReadLong ();
1934                 }
1935
1936                 /*
1937                  * MODULE
1938                  */
1939
1940                 internal ModuleInfo Module_GetInfo (long id) {
1941                         PacketReader r = SendReceive (CommandSet.MODULE, (int)CmdModule.GET_INFO, new PacketWriter ().WriteId (id));
1942                         ModuleInfo info = new ModuleInfo { Name = r.ReadString (), ScopeName = r.ReadString (), FQName = r.ReadString (), Guid = r.ReadString (), Assembly = r.ReadId () };
1943                         return info;
1944                 }
1945
1946                 /*
1947                  * ASSEMBLY
1948                  */
1949
1950                 internal string Assembly_GetLocation (long id) {
1951                         return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_LOCATION, new PacketWriter ().WriteId (id)).ReadString ();
1952                 }
1953
1954                 internal long Assembly_GetEntryPoint (long id) {
1955                         return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_ENTRY_POINT, new PacketWriter ().WriteId (id)).ReadId ();
1956                 }
1957
1958                 internal long Assembly_GetManifestModule (long id) {
1959                         return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_MANIFEST_MODULE, new PacketWriter ().WriteId (id)).ReadId ();
1960                 }
1961
1962                 internal long Assembly_GetObject (long id) {
1963                         return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_OBJECT, new PacketWriter ().WriteId (id)).ReadId ();
1964                 }
1965
1966                 internal long Assembly_GetType (long id, string name, bool ignoreCase) {
1967                         return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_TYPE, new PacketWriter ().WriteId (id).WriteString (name).WriteBool (ignoreCase)).ReadId ();
1968                 }
1969
1970                 internal string Assembly_GetName (long id) {
1971                         return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1972                 }
1973
1974                 /*
1975                  * TYPE
1976                  */
1977
1978                 internal TypeInfo Type_GetInfo (long id) {
1979                         PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_INFO, new PacketWriter ().WriteId (id));
1980                         TypeInfo res = new TypeInfo ();
1981
1982                         res.ns = r.ReadString ();
1983                         res.name = r.ReadString ();
1984                         res.full_name = r.ReadString ();
1985                         res.assembly = r.ReadId ();
1986                         res.module = r.ReadId ();
1987                         res.base_type = r.ReadId ();
1988                         res.element_type = r.ReadId ();
1989                         res.token = r.ReadInt ();
1990                         res.rank = r.ReadByte ();
1991                         res.attributes = r.ReadInt ();
1992                         int b = r.ReadByte ();
1993                         res.is_byref = (b & 1) != 0;
1994                         res.is_pointer = (b & 2) != 0;
1995                         res.is_primitive = (b & 4) != 0;
1996                         res.is_valuetype = (b & 8) != 0;
1997                         res.is_enum = (b & 16) != 0;
1998                         res.is_gtd = (b & 32) != 0;
1999                         res.is_generic_type = (b & 64) != 0;
2000
2001                         int nested_len = r.ReadInt ();
2002                         res.nested = new long [nested_len];
2003                         for (int i = 0; i < nested_len; ++i)
2004                                 res.nested [i] = r.ReadId ();
2005
2006                         if (Version.AtLeast (2, 12))
2007                                 res.gtd = r.ReadId ();
2008                         if (Version.AtLeast (2, 15) && res.is_generic_type) {
2009                                 int n = r.ReadInt ();
2010                                 res.type_args = r.ReadIds (n);
2011                         }
2012
2013                         return res;
2014                 }
2015
2016                 internal long[] Type_GetMethods (long id) {
2017                         PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_METHODS, new PacketWriter ().WriteId (id));
2018
2019                         int n = r.ReadInt ();
2020                         long[] res = new long [n];
2021                         for (int i = 0; i < n; ++i)
2022                                 res [i] = r.ReadId ();
2023                         return res;
2024                 }
2025
2026                 internal long[] Type_GetFields (long id, out string[] names, out long[] types, out int[] attrs) {
2027                         PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_FIELDS, new PacketWriter ().WriteId (id));
2028
2029                         int n = r.ReadInt ();
2030                         long[] res = new long [n];
2031                         names = new string [n];
2032                         types = new long [n];
2033                         attrs = new int [n];
2034                         for (int i = 0; i < n; ++i) {
2035                                 res [i] = r.ReadId ();
2036                                 names [i] = r.ReadString ();
2037                                 types [i] = r.ReadId ();
2038                                 attrs [i] = r.ReadInt ();
2039                         }
2040                         return res;
2041                 }
2042
2043                 internal PropInfo[] Type_GetProperties (long id) {
2044                         PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_PROPERTIES, new PacketWriter ().WriteId (id));
2045
2046                         int n = r.ReadInt ();
2047                         PropInfo[] res = new PropInfo [n];
2048                         for (int i = 0; i < n; ++i) {
2049                                 res [i] = new PropInfo ();
2050                                 res [i].id = r.ReadId ();
2051                                 res [i].name = r.ReadString ();
2052                                 res [i].get_method = r.ReadId ();
2053                                 res [i].set_method = r.ReadId ();
2054                                 res [i].attrs = r.ReadInt ();
2055                         }
2056
2057                         return res;
2058                 }
2059
2060                 internal long Type_GetObject (long id) {
2061                         return SendReceive (CommandSet.TYPE, (int)CmdType.GET_OBJECT, new PacketWriter ().WriteId (id)).ReadId ();
2062                 }
2063
2064                 internal ValueImpl[] Type_GetValues (long id, long[] fields, long thread_id) {
2065                         int len = fields.Length;
2066                         PacketReader r;
2067                         if (thread_id != 0)
2068                                 r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_VALUES_2, new PacketWriter ().WriteId (id).WriteId (thread_id).WriteInt (len).WriteIds (fields));
2069                         else
2070                                 r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_VALUES, new PacketWriter ().WriteId (id).WriteInt (len).WriteIds (fields));
2071
2072                         ValueImpl[] res = new ValueImpl [len];
2073                         for (int i = 0; i < len; ++i)
2074                                 res [i] = r.ReadValue ();
2075                         return res;
2076                 }                       
2077
2078                 internal void Type_SetValues (long id, long[] fields, ValueImpl[] values) {
2079                         SendReceive (CommandSet.TYPE, (int)CmdType.SET_VALUES, new PacketWriter ().WriteId (id).WriteInt (fields.Length).WriteIds (fields).WriteValues (values));
2080                 }
2081
2082                 internal string[] Type_GetSourceFiles (long id, bool return_full_paths) {
2083                         var r = SendReceive (CommandSet.TYPE, return_full_paths ? (int)CmdType.GET_SOURCE_FILES_2 : (int)CmdType.GET_SOURCE_FILES, new PacketWriter ().WriteId (id));
2084                         int len = r.ReadInt ();
2085                         string[] res = new string [len];
2086                         for (int i = 0; i < len; ++i)
2087                                 res [i] = r.ReadString ();
2088                         return res;
2089                 }
2090
2091                 internal bool Type_IsAssignableFrom (long id, long c_id) {
2092                         return SendReceive (CommandSet.TYPE, (int)CmdType.IS_ASSIGNABLE_FROM, new PacketWriter ().WriteId (id).WriteId (c_id)).ReadByte () > 0;
2093                 }
2094
2095                 internal CattrInfo[] Type_GetCustomAttributes (long id, long attr_type_id, bool inherit) {
2096                         PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_CATTRS, new PacketWriter ().WriteId (id).WriteId (attr_type_id));
2097                         return ReadCattrs (r);
2098                 }
2099
2100                 internal CattrInfo[] Type_GetFieldCustomAttributes (long id, long field_id, long attr_type_id, bool inherit) {
2101                         PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_FIELD_CATTRS, new PacketWriter ().WriteId (id).WriteId (field_id).WriteId (attr_type_id));
2102                         return ReadCattrs (r);
2103                 }
2104
2105                 internal CattrInfo[] Type_GetPropertyCustomAttributes (long id, long field_id, long attr_type_id, bool inherit) {
2106                         PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_PROPERTY_CATTRS, new PacketWriter ().WriteId (id).WriteId (field_id).WriteId (attr_type_id));
2107                         return ReadCattrs (r);
2108                 }
2109
2110                 public long[] Type_GetMethodsByNameFlags (long id, string name, int flags, bool ignoreCase) {
2111                         flags |= ignoreCase ? (int)BindingFlagsExtensions.BINDING_FLAGS_IGNORE_CASE : 0;
2112                         PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.CMD_TYPE_GET_METHODS_BY_NAME_FLAGS, new PacketWriter ().WriteId (id).WriteString (name).WriteInt (flags));
2113                         int len = r.ReadInt ();
2114                         long[] res = new long [len];
2115                         for (int i = 0; i < len; ++i)
2116                                 res [i] = r.ReadId ();
2117                         return res;
2118                 }
2119
2120                 internal long[] Type_GetInterfaces (long id) {
2121                         PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_INTERFACES, new PacketWriter ().WriteId (id));
2122                         int len = r.ReadInt ();
2123                         return r.ReadIds (len);
2124                 }
2125
2126                 internal IfaceMapInfo[] Type_GetInterfaceMap (long id, long[] ids) {
2127                         PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_INTERFACE_MAP, new PacketWriter ().WriteId (id).WriteInt (ids.Length).WriteIds (ids));
2128                         var res = new IfaceMapInfo [ids.Length];
2129                         for (int i = 0; i < ids.Length; ++i) {
2130                                 int n = r.ReadInt ();
2131
2132                                 res [i].iface_id = ids [i];
2133                                 res [i].iface_methods = r.ReadIds (n);
2134                                 res [i].target_methods = r.ReadIds (n);
2135                         }
2136
2137                         return res;
2138                 }
2139
2140                 internal bool Type_IsInitialized (long id) {
2141                         PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.IS_INITIALIZED, new PacketWriter ().WriteId (id));
2142                         return r.ReadInt () == 1;
2143                 }
2144
2145                 /*
2146                  * EVENTS
2147                  */
2148
2149                 internal int EnableEvent (EventType etype, SuspendPolicy suspend_policy, List<Modifier> mods) {
2150                         var w = new PacketWriter ().WriteByte ((byte)etype).WriteByte ((byte)suspend_policy);
2151                         if (mods != null) {
2152                                 if (mods.Count > 255)
2153                                         throw new NotImplementedException ();
2154                                 w.WriteByte ((byte)mods.Count);
2155                                 foreach (Modifier mod in mods) {
2156                                         if (mod is CountModifier) {
2157                                                 w.WriteByte ((byte)ModifierKind.COUNT);
2158                                                 w.WriteInt ((mod as CountModifier).Count);
2159                                         } else if (mod is LocationModifier) {
2160                                                 w.WriteByte ((byte)ModifierKind.LOCATION_ONLY);
2161                                                 w.WriteId ((mod as LocationModifier).Method);
2162                                                 w.WriteLong ((mod as LocationModifier).Location);
2163                                         } else if (mod is StepModifier) {
2164                                                 w.WriteByte ((byte)ModifierKind.STEP);
2165                                                 w.WriteId ((mod as StepModifier).Thread);
2166                                                 w.WriteInt ((mod as StepModifier).Size);
2167                                                 w.WriteInt ((mod as StepModifier).Depth);
2168                                                 if (Version.AtLeast (2, 16))
2169                                                         w.WriteInt ((mod as StepModifier).Filter);
2170                                         } else if (mod is ThreadModifier) {
2171                                                 w.WriteByte ((byte)ModifierKind.THREAD_ONLY);
2172                                                 w.WriteId ((mod as ThreadModifier).Thread);
2173                                         } else if (mod is ExceptionModifier) {
2174                                                 var em = mod as ExceptionModifier;
2175                                                 w.WriteByte ((byte)ModifierKind.EXCEPTION_ONLY);
2176                                                 w.WriteId (em.Type);
2177                                                 if (Version.MajorVersion > 2 || Version.MinorVersion > 0) {
2178                                                         /* This is only supported in protocol version 2.1 */
2179                                                         w.WriteBool (em.Caught);
2180                                                         w.WriteBool (em.Uncaught);
2181                                                 } else if (!em.Caught || !em.Uncaught) {
2182                                                         throw new NotSupportedException ("This request is not supported by the protocol version implemented by the debuggee.");
2183                                                 }
2184                                                 if (Version.MajorVersion > 2 || Version.MinorVersion > 24) {
2185                                                         w.WriteBool (em.Subclasses);
2186                                                 } else if (!em.Subclasses) {
2187                                                         throw new NotSupportedException ("This request is not supported by the protocol version implemented by the debuggee.");
2188                                                 }
2189                                         } else if (mod is AssemblyModifier) {
2190                                                 w.WriteByte ((byte)ModifierKind.ASSEMBLY_ONLY);
2191                                                 var amod = (mod as AssemblyModifier);
2192                                                 w.WriteInt (amod.Assemblies.Length);
2193                                                 foreach (var id in amod.Assemblies)
2194                                                         w.WriteId (id);
2195                                         } else if (mod is SourceFileModifier) {
2196                                                 w.WriteByte ((byte)ModifierKind.SOURCE_FILE_ONLY);
2197                                                 var smod = (mod as SourceFileModifier);
2198                                                 w.WriteInt (smod.SourceFiles.Length);
2199                                                 foreach (var s in smod.SourceFiles)
2200                                                         w.WriteString (s);
2201                                         } else if (mod is TypeNameModifier) {
2202                                                 w.WriteByte ((byte)ModifierKind.TYPE_NAME_ONLY);
2203                                                 var tmod = (mod as TypeNameModifier);
2204                                                 w.WriteInt (tmod.TypeNames.Length);
2205                                                 foreach (var s in tmod.TypeNames)
2206                                                         w.WriteString (s);
2207                                         } else {
2208                                                 throw new NotImplementedException ();
2209                                         }
2210                                 }
2211                         } else {
2212                                 w.WriteByte (0);
2213                         }
2214                         return SendReceive (CommandSet.EVENT_REQUEST, (int)CmdEventRequest.SET, w).ReadInt ();
2215                 }
2216
2217                 internal void ClearEventRequest (EventType etype, int req_id) {
2218                         SendReceive (CommandSet.EVENT_REQUEST, (int)CmdEventRequest.CLEAR, new PacketWriter ().WriteByte ((byte)etype).WriteInt (req_id));
2219                 }                       
2220
2221                 internal void ClearAllBreakpoints () {
2222                         SendReceive (CommandSet.EVENT_REQUEST, (int)CmdEventRequest.CLEAR_ALL_BREAKPOINTS, new PacketWriter ());
2223                 }
2224                         
2225                 /*
2226                  * STACK FRAME
2227                  */
2228                 internal ValueImpl StackFrame_GetThis (long thread_id, long id) {
2229                         PacketReader r = SendReceive (CommandSet.STACK_FRAME, (int)CmdStackFrame.GET_THIS, new PacketWriter ().WriteId (thread_id).WriteId (id));
2230                         return r.ReadValue ();
2231                 }
2232
2233                 internal ValueImpl[] StackFrame_GetValues (long thread_id, long id, int[] pos) {
2234                         /* pos < 0 -> argument at pos (-pos) - 1 */
2235                         /* pos >= 0 -> local at pos */
2236                         int len = pos.Length;
2237                         PacketReader r = SendReceive (CommandSet.STACK_FRAME, (int)CmdStackFrame.GET_VALUES, new PacketWriter ().WriteId (thread_id).WriteId (id).WriteInt (len).WriteInts (pos));
2238
2239                         ValueImpl[] res = new ValueImpl [len];
2240                         for (int i = 0; i < len; ++i)
2241                                 res [i] = r.ReadValue ();
2242                         return res;
2243                 }
2244
2245                 internal void StackFrame_SetValues (long thread_id, long id, int[] pos, ValueImpl[] values) {
2246                         /* pos < 0 -> argument at pos (-pos) - 1 */
2247                         /* pos >= 0 -> local at pos */
2248                         int len = pos.Length;
2249                         SendReceive (CommandSet.STACK_FRAME, (int)CmdStackFrame.SET_VALUES, new PacketWriter ().WriteId (thread_id).WriteId (id).WriteInt (len).WriteInts (pos).WriteValues (values));
2250                 }
2251
2252                 /*
2253                  * ARRAYS
2254                  */
2255                 internal int[] Array_GetLength (long id, out int rank, out int[] lower_bounds) {
2256                         var r = SendReceive (CommandSet.ARRAY_REF, (int)CmdArrayRef.GET_LENGTH, new PacketWriter ().WriteId (id));
2257                         rank = r.ReadInt ();
2258                         int[] res = new int [rank];
2259                         lower_bounds = new int [rank];
2260                         for (int i = 0; i < rank; ++i) {
2261                                 res [i] = r.ReadInt ();
2262                                 lower_bounds [i] = r.ReadInt ();
2263                         }
2264                         return res;
2265                 }
2266
2267                 internal ValueImpl[] Array_GetValues (long id, int index, int len) {
2268                         var r = SendReceive (CommandSet.ARRAY_REF, (int)CmdArrayRef.GET_VALUES, new PacketWriter ().WriteId (id).WriteInt (index).WriteInt (len));
2269                         ValueImpl[] res = new ValueImpl [len];
2270                         for (int i = 0; i < len; ++i)
2271                                 res [i] = r.ReadValue ();
2272                         return res;
2273                 }
2274
2275                 internal void Array_SetValues (long id, int index, ValueImpl[] values) {
2276                         SendReceive (CommandSet.ARRAY_REF, (int)CmdArrayRef.SET_VALUES, new PacketWriter ().WriteId (id).WriteInt (index).WriteInt (values.Length).WriteValues (values));
2277                 }
2278
2279                 /*
2280                  * STRINGS
2281                  */
2282                 internal string String_GetValue (long id) {
2283                         return SendReceive (CommandSet.STRING_REF, (int)CmdStringRef.GET_VALUE, new PacketWriter ().WriteId (id)).ReadString ();
2284                 }                       
2285
2286                 internal int String_GetLength (long id) {
2287                         return (int)SendReceive (CommandSet.STRING_REF, (int)CmdStringRef.GET_LENGTH, new PacketWriter ().WriteId (id)).ReadLong ();
2288                 }                       
2289
2290                 internal char[] String_GetChars (long id, int index, int length) {
2291                         var r = SendReceive (CommandSet.STRING_REF, (int)CmdStringRef.GET_CHARS, new PacketWriter ().WriteId (id).WriteLong (index).WriteLong (length));
2292                         var res = new char [length];
2293                         for (int i = 0; i < length; ++i)
2294                                 res [i] = (char)r.ReadShort ();
2295                         return res;
2296                 }                       
2297
2298                 /*
2299                  * OBJECTS
2300                  */
2301                 internal long Object_GetType (long id) {
2302                         return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_TYPE, new PacketWriter ().WriteId (id)).ReadId ();
2303                 }                       
2304
2305                 internal long Object_GetDomain (long id) {
2306                         return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_DOMAIN, new PacketWriter ().WriteId (id)).ReadId ();
2307                 }                       
2308
2309                 internal ValueImpl[] Object_GetValues (long id, long[] fields) {
2310                         int len = fields.Length;
2311                         PacketReader r = SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_VALUES, new PacketWriter ().WriteId (id).WriteInt (len).WriteIds (fields));
2312
2313                         ValueImpl[] res = new ValueImpl [len];
2314                         for (int i = 0; i < len; ++i)
2315                                 res [i] = r.ReadValue ();
2316                         return res;
2317                 }
2318
2319                 internal void Object_SetValues (long id, long[] fields, ValueImpl[] values) {
2320                         SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.SET_VALUES, new PacketWriter ().WriteId (id).WriteInt (fields.Length).WriteIds (fields).WriteValues (values));
2321                 }
2322
2323                 internal bool Object_IsCollected (long id) {
2324                         return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.IS_COLLECTED, new PacketWriter ().WriteId (id)).ReadInt () == 1;
2325                 }                       
2326
2327                 internal long Object_GetAddress (long id) {
2328                         return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_ADDRESS, new PacketWriter ().WriteId (id)).ReadLong ();
2329                 }                       
2330
2331                 internal ObjectRefInfo Object_GetInfo (long id) {
2332                         ObjectRefInfo res = new ObjectRefInfo ();
2333                         PacketReader r = SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_INFO, new PacketWriter ().WriteId (id));
2334
2335                         res.type_id = r.ReadId ();
2336                         res.domain_id = r.ReadId ();
2337                         return res;
2338                 }
2339
2340                 public void ForceDisconnect ()
2341                 {
2342                         closed = true;
2343                         disconnected = true;
2344                         TransportClose ();
2345                 }
2346         }
2347         
2348         class TcpConnection : Connection
2349         {
2350                 Socket socket;
2351                 
2352                 internal TcpConnection (Socket socket)
2353                 {
2354                         this.socket = socket;
2355                         //socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.NoDelay, 1);
2356                 }
2357                 
2358                 internal EndPoint EndPoint {
2359                         get {
2360                                 return socket.RemoteEndPoint;
2361                         }
2362                 }
2363                 
2364                 protected override int TransportSend (byte[] buf, int buf_offset, int len)
2365                 {
2366                         return socket.Send (buf, buf_offset, len, SocketFlags.None);
2367                 }
2368                 
2369                 protected override int TransportReceive (byte[] buf, int buf_offset, int len)
2370                 {
2371                         return socket.Receive (buf, buf_offset, len, SocketFlags.None);
2372                 }
2373                 
2374                 protected override void TransportSetTimeouts (int send_timeout, int receive_timeout)
2375                 {
2376                         socket.SendTimeout = send_timeout;
2377                         socket.ReceiveTimeout = receive_timeout;
2378                 }
2379                 
2380                 protected override void TransportClose ()
2381                 {
2382                         socket.Close ();
2383                 }
2384         }
2385
2386         /* This is the interface exposed by the debugger towards the debugger agent */
2387         interface IEventHandler
2388         {
2389                 void Events (SuspendPolicy suspend_policy, EventInfo[] events);
2390
2391                 void VMDisconnect (int req_id, long thread_id, string vm_uri);
2392         }
2393 }