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