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