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