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