Merge branch 'cecil-light'
[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 Mono.Cecil.Metadata;
9
10 namespace Mono.Debugger.Soft
11 {
12         public class VersionInfo {
13                 public string VMVersion {
14                         get; set;
15                 }
16
17                 public int MajorVersion {
18                         get; set;
19                 }
20
21                 public int MinorVersion {
22                         get; set;
23                 }
24         }
25
26         class DebugInfo {
27                 public int max_il_offset;
28                 public string filename;
29                 public int[] il_offsets;
30                 public int[] line_numbers;
31         }
32
33         struct FrameInfo {
34                 public long id;
35                 public long method;
36                 public int il_offset;
37                 public StackFrameFlags flags;
38         }
39
40         class TypeInfo {
41                 public string ns, name, full_name;
42                 public long assembly, module, base_type, element_type;
43                 public int token, rank, attributes;
44                 public bool is_byref, is_pointer, is_primitive, is_valuetype, is_enum;
45                 public long[] nested;
46         }
47
48         class MethodInfo {
49                 public int attributes, iattributes, token;
50         }
51
52         class MethodBodyInfo {
53                 public byte[] il;
54         }
55
56         struct ParamInfo {
57                 public int call_conv;
58                 public int param_count;
59                 public int generic_param_count;
60                 public long ret_type;
61                 public long[] param_types;
62                 public string[] param_names;
63         }
64
65         struct LocalsInfo {
66                 public long[] types;
67                 public string[] names;
68                 public int[] live_range_start;
69                 public int[] live_range_end;
70         }
71
72         struct PropInfo {
73                 public long id;
74                 public string name;
75                 public long get_method, set_method;
76                 public int attrs;
77         }
78
79         class CattrNamedArgInfo {
80                 public bool is_property;
81                 public long id;
82                 public ValueImpl value;
83         }
84
85         class CattrInfo {
86                 public long ctor_id;
87                 public ValueImpl[] ctor_args;
88                 public CattrNamedArgInfo[] named_args;
89         }
90
91         class ThreadInfo {
92                 public bool is_thread_pool;
93         }
94
95         enum ValueTypeId {
96                 VALUE_TYPE_ID_NULL = 0xf0,
97                 VALUE_TYPE_ID_TYPE = 0xf1
98         }
99
100         enum InvokeFlags {
101                 NONE = 0x0,
102                 DISABLE_BREAKPOINTS = 0x1,
103                 SINGLE_THREADED = 0x2
104         }
105
106         enum ElementType {
107                 End              = 0x00,
108                 Void            = 0x01,
109                 Boolean  = 0x02,
110                 Char            = 0x03,
111                 I1                = 0x04,
112                 U1                = 0x05,
113                 I2                = 0x06,
114                 U2                = 0x07,
115                 I4                = 0x08,
116                 U4                = 0x09,
117                 I8                = 0x0a,
118                 U8                = 0x0b,
119                 R4                = 0x0c,
120                 R8                = 0x0d,
121                 String    = 0x0e,
122                 Ptr              = 0x0f,
123                 ByRef      = 0x10,
124                 ValueType   = 0x11,
125                 Class      = 0x12,
126                 Var        = 0x13,
127                 Array      = 0x14,
128                 GenericInst = 0x15,
129                 TypedByRef  = 0x16,
130                 I                  = 0x18,
131                 U                  = 0x19,
132                 FnPtr      = 0x1b,
133                 Object    = 0x1c,
134                 SzArray  = 0x1d,
135                 MVar       = 0x1e,
136                 CModReqD        = 0x1f,
137                 CModOpt  = 0x20,
138                 Internal        = 0x21,
139                 Modifier        = 0x40,
140                 Sentinel        = 0x41,
141                 Pinned    = 0x45,
142
143                 Type            = 0x50,
144                 Boxed      = 0x51,
145                 Enum            = 0x55
146         }
147
148         class ValueImpl {
149                 public ElementType Type; /* or one of the VALUE_TYPE_ID constants */
150                 public long Objid;
151                 public object Value;
152                 public long Klass; // For ElementType.ValueType
153                 public ValueImpl[] Fields; // for ElementType.ValueType
154                 public bool IsEnum; // For ElementType.ValueType
155                 public long Id; /* For VALUE_TYPE_ID_TYPE */
156         }
157
158         class ModuleInfo {
159                 public string Name, ScopeName, FQName, Guid;
160                 public long Assembly;
161         }               
162
163         enum TokenType {
164                 STRING = 0,
165                 TYPE = 1,
166                 FIELD = 2,
167                 METHOD = 3,
168                 UNKNOWN = 4
169         }
170
171         enum StackFrameFlags {
172                 DEBUGGER_INVOKE = 1
173         }
174
175         class ResolvedToken {
176                 public TokenType Type;
177                 public string Str;
178                 public long Id;
179         }
180
181         class Modifier {
182         }
183
184         class CountModifier : Modifier {
185                 public int Count {
186                         get; set;
187                 }
188         }
189
190         class LocationModifier : Modifier {
191                 public long Method {
192                         get; set;
193                 }
194
195                 public long Location {
196                         get; set;
197                 }
198         }
199
200         class StepModifier : Modifier {
201                 public long Thread {
202                         get; set;
203                 }
204
205                 public int Depth {
206                         get; set;
207                 }
208
209                 public int Size {
210                         get; set;
211                 }
212         }
213
214         class ThreadModifier : Modifier {
215                 public long Thread {
216                         get; set;
217                 }
218         }
219
220         class ExceptionModifier : Modifier {
221                 public long Type {
222                         get; set;
223                 }
224                 public bool Caught {
225                         get; set;
226                 }
227                 public bool Uncaught {
228                         get; set;
229                 }
230         }
231
232         class AssemblyModifier : Modifier {
233                 public long[] Assemblies {
234                         get; set;
235                 }
236         }
237
238         class EventInfo {
239                 public EventType EventType {
240                         get; set;
241                 }
242
243                 public int ReqId {
244                         get; set;
245                 }
246
247                 public SuspendPolicy SuspendPolicy {
248                         get; set;
249                 }
250
251                 public long ThreadId {
252                         get; set;
253                 }
254
255                 public long Id {
256                         get; set;
257                 }
258
259                 public long Location {
260                         get; set;
261                 }
262
263                 public EventInfo (EventType type, int req_id) {
264                         EventType = type;
265                         ReqId = req_id;
266                 }
267         }
268
269         public enum ErrorCode {
270                 NONE = 0,
271                 INVALID_OBJECT = 20,
272                 INVALID_FIELDID = 25,
273                 INVALID_FRAMEID = 30,
274                 NOT_IMPLEMENTED = 100,
275                 NOT_SUSPENDED = 101,
276                 INVALID_ARGUMENT = 102,
277                 ERR_UNLOADED = 103,
278                 ERR_NO_INVOCATION = 104,
279                 ABSENT_INFORMATION = 105
280         }
281
282         public class ErrorHandlerEventArgs : EventArgs {
283
284                 public ErrorCode ErrorCode {
285                         get; set;
286                 }
287         }
288
289         /*
290          * Represents the connection to the debuggee
291          */
292         class Connection
293         {
294                 /*
295                  * The protocol and the packet format is based on JDWP, the differences 
296                  * are in the set of supported events, and the commands.
297                  */
298                 public const string HANDSHAKE_STRING = "DWP-Handshake";
299
300                 public const int HEADER_LENGTH = 11;
301
302                 /*
303                  * Th version of the wire-protocol implemented by the library. The library
304                  * and the debuggee can communicate if they implement the same major version.
305                  * If they implement a different minor version, they can communicate, but some
306                  * features might not be available. This allows older clients to communicate
307                  * with newer runtimes, and vice versa.
308                  */
309                 public const int MAJOR_VERSION = 2;
310                 public const int MINOR_VERSION = 2;
311
312                 enum WPSuspendPolicy {
313                         NONE = 0,
314                         EVENT_THREAD = 1,
315                         ALL = 2
316                 }
317
318                 enum CommandSet {
319                         VM = 1,
320                         OBJECT_REF = 9,
321                         STRING_REF = 10,
322                         THREAD = 11,
323                         ARRAY_REF = 13,
324                         EVENT_REQUEST = 15,
325                         STACK_FRAME = 16,
326                         APPDOMAIN = 20,
327                         ASSEMBLY = 21,
328                         METHOD = 22,
329                         TYPE = 23,
330                         MODULE = 24,
331                         EVENT = 64
332                 }
333
334                 enum EventKind {
335                         VM_START = 0,
336                         VM_DEATH = 1,
337                         THREAD_START = 2,
338                         THREAD_DEATH = 3,
339                         APPDOMAIN_CREATE = 4, // Not in JDI
340                         APPDOMAIN_UNLOAD = 5, // Not in JDI
341                         METHOD_ENTRY = 6,
342                         METHOD_EXIT = 7,
343                         ASSEMBLY_LOAD = 8,
344                         ASSEMBLY_UNLOAD = 9,
345                         BREAKPOINT = 10,
346                         STEP = 11,
347                         TYPE_LOAD = 12,
348                         EXCEPTION = 13
349                 }
350
351                 enum ModifierKind {
352                         COUNT = 1,
353                         THREAD_ONLY = 3,
354                         LOCATION_ONLY = 7,
355                         EXCEPTION_ONLY = 8,
356                         STEP = 10,
357                         ASSEMBLY_ONLY = 11
358                 }
359
360                 enum CmdVM {
361                         VERSION = 1,
362                         ALL_THREADS = 2,
363                         SUSPEND = 3,
364                         RESUME = 4,
365                         EXIT = 5,
366                         DISPOSE = 6,
367                         INVOKE_METHOD = 7,
368                         SET_PROTOCOL_VERSION = 8,
369                         ABORT_INVOKE = 9
370                 }
371
372                 enum CmdEvent {
373                         COMPOSITE = 100
374                 }
375
376                 enum CmdThread {
377                         GET_FRAME_INFO = 1,
378                         GET_NAME = 2,
379                         GET_STATE = 3,
380                         GET_INFO = 4,
381                         /* FIXME: Merge into GET_INFO when the major protocol version is increased */
382                         GET_ID = 5
383                 }
384
385                 enum CmdEventRequest {
386                         SET = 1,
387                         CLEAR = 2,
388                         CLEAR_ALL_BREAKPOINTS = 3
389                 }
390
391                 enum CmdAppDomain {
392                         GET_ROOT_DOMAIN = 1,
393                         GET_FRIENDLY_NAME = 2,
394                         GET_ASSEMBLIES = 3,
395                         GET_ENTRY_ASSEMBLY = 4,
396                         CREATE_STRING = 5,
397                         GET_CORLIB = 6,
398                         CREATE_BOXED_VALUE = 7
399                 }
400
401                 enum CmdAssembly {
402                         GET_LOCATION = 1,
403                         GET_ENTRY_POINT = 2,
404                         GET_MANIFEST_MODULE = 3,
405                         GET_OBJECT = 4,
406                         GET_TYPE = 5,
407                         GET_NAME = 6
408                 }
409
410                 enum CmdModule {
411                         GET_INFO = 1,
412                 }
413
414                 enum CmdMethod {
415                         GET_NAME = 1,
416                         GET_DECLARING_TYPE = 2,
417                         GET_DEBUG_INFO = 3,
418                         GET_PARAM_INFO = 4,
419                         GET_LOCALS_INFO = 5,
420                         GET_INFO = 6,
421                         GET_BODY = 7,
422                         RESOLVE_TOKEN = 8
423                 }
424
425                 enum CmdType {
426                         GET_INFO = 1,
427                         GET_METHODS = 2,
428                         GET_FIELDS = 3,
429                         GET_VALUES = 4,
430                         GET_OBJECT = 5,
431                         GET_SOURCE_FILES = 6,
432                         SET_VALUES = 7,
433                         IS_ASSIGNABLE_FROM = 8,
434                         GET_PROPERTIES = 9,
435                         GET_CATTRS = 10,
436                         GET_FIELD_CATTRS = 11,
437                         GET_PROPERTY_CATTRS = 12,
438                         /* FIXME: Merge into GET_SOURCE_FILES when the major protocol version is increased */
439                         GET_SOURCE_FILES_2 = 13
440                 }
441
442                 enum CmdStackFrame {
443                         GET_VALUES = 1,
444                         GET_THIS = 2,
445                         SET_VALUES = 3
446                 }
447
448                 enum CmdArrayRef {
449                         GET_LENGTH = 1,
450                         GET_VALUES = 2,
451                         SET_VALUES = 3
452                 }
453
454                 enum CmdStringRef {
455                         GET_VALUE = 1
456                 }
457
458                 enum CmdObjectRef {
459                         GET_TYPE = 1,
460                         GET_VALUES = 2,
461                         IS_COLLECTED = 3,
462                         GET_ADDRESS = 4,
463                         GET_DOMAIN = 5,
464                         SET_VALUES = 6
465                 }
466
467                 class Header {
468                         public int id;
469                         public int command_set;
470                         public int command;
471                         public int flags;
472                 }                       
473
474                 public static int GetPacketLength (byte[] header) {
475                         int offset = 0;
476                         return decode_int (header, ref offset);
477                 }
478
479                 public static bool IsReplyPacket (byte[] packet) {
480                         int offset = 8;
481                         return decode_byte (packet, ref offset) == 0x80;
482                 }
483
484                 public static int GetPacketId (byte[] packet) {
485                         int offset = 4;
486                         return decode_int (packet, ref offset);
487                 }
488
489                 static int decode_byte (byte[] packet, ref int offset) {
490                         return packet [offset++];
491                 }
492
493                 static int decode_short (byte[] packet, ref int offset) {
494                         int res = ((int)packet [offset] << 8) | (int)packet [offset + 1];
495                         offset += 2;
496                         return res;
497                 }
498
499                 static int decode_int (byte[] packet, ref int offset) {
500                         int res = ((int)packet [offset] << 24) | ((int)packet [offset + 1] << 16) | ((int)packet [offset + 2] << 8) | (int)packet [offset + 3];
501                         offset += 4;
502                         return res;
503                 }
504
505                 static long decode_id (byte[] packet, ref int offset) {
506                         return decode_int (packet, ref offset);
507                 }
508
509                 static long decode_long (byte[] packet, ref int offset) {
510                         uint high = (uint)decode_int (packet, ref offset);
511                         uint low = (uint)decode_int (packet, ref offset);
512
513                         return (long)(((ulong)high << 32) | (ulong)low);
514                 }
515
516                 public static SuspendPolicy decode_suspend_policy (int suspend_policy) {
517                         switch ((WPSuspendPolicy)suspend_policy) {
518                         case WPSuspendPolicy.NONE:
519                                 return SuspendPolicy.None;
520                         case WPSuspendPolicy.EVENT_THREAD:
521                                 return SuspendPolicy.EventThread;
522                         case WPSuspendPolicy.ALL:
523                                 return SuspendPolicy.All;
524                         default:
525                                 throw new NotImplementedException ();
526                         }
527                 }
528
529                 static Header decode_command_header (byte[] packet) {
530                         int offset = 0;
531                         Header res = new Header ();
532
533                         decode_int (packet, ref offset);
534                         res.id = decode_int (packet, ref offset);
535                         res.flags = decode_byte (packet, ref offset);
536                         res.command_set = decode_byte (packet, ref offset);
537                         res.command = decode_byte (packet, ref offset);
538
539                         return res;
540                 }
541
542                 static void encode_byte (byte[] buf, int b, ref int offset) {
543                         buf [offset] = (byte)b;
544                         offset ++;
545                 }
546
547                 static void encode_int (byte[] buf, int i, ref int offset) {
548                         buf [offset] = (byte)((i >> 24) & 0xff);
549                         buf [offset + 1] = (byte)((i >> 16) & 0xff);
550                         buf [offset + 2] = (byte)((i >> 8) & 0xff);
551                         buf [offset + 3] = (byte)((i >> 0) & 0xff);
552                         offset += 4;
553                 }
554
555                 static void encode_id (byte[] buf, long id, ref int offset) {
556                         encode_int (buf, (int)id, ref offset);
557                 }
558
559                 static void encode_long (byte[] buf, long l, ref int offset) {
560                         encode_int (buf, (int)((l >> 32) & 0xffffffff), ref offset);
561                         encode_int (buf, (int)(l & 0xffffffff), ref offset);
562                 }
563
564                 public static byte[] EncodePacket (int id, int commandSet, int command, byte[] data, int dataLen) {
565                         byte[] buf = new byte [dataLen + 11];
566                         int offset = 0;
567                         
568                         encode_int (buf, buf.Length, ref offset);
569                         encode_int (buf, id, ref offset);
570                         encode_byte (buf, 0, ref offset);
571                         encode_byte (buf, commandSet, ref offset);
572                         encode_byte (buf, command, ref offset);
573
574                         for (int i = 0; i < dataLen; ++i)
575                                 buf [offset + i] = data [i];
576
577                         return buf;
578                 }
579
580                 class PacketReader {
581                         byte[] packet;
582                         int offset;
583
584                         public PacketReader (byte[] packet) {
585                                 this.packet = packet;
586
587                                 // For event packets
588                                 Header header = decode_command_header (packet);
589                                 CommandSet = (CommandSet)header.command_set;
590                                 Command = header.command;
591
592                                 // For reply packets
593                                 offset = 0;
594                                 ReadInt (); // length
595                                 ReadInt (); // id
596                                 ReadByte (); // flags
597                                 ErrorCode = ReadShort ();
598                         }
599
600                         public CommandSet CommandSet {
601                                 get; set;
602                         }
603
604                         public int Command {
605                                 get; set;
606                         }
607
608                         public int ErrorCode {
609                                 get; set;
610                         }
611
612                         public int Offset {
613                                 get {
614                                         return offset;
615                                 }
616                         }
617
618                         public int ReadByte () {
619                                 return decode_byte (packet, ref offset);
620                         }
621
622                         public int ReadShort () {
623                                 return decode_short (packet, ref offset);
624                         }
625
626                         public int ReadInt () {
627                                 return decode_int (packet, ref offset);
628                         }
629
630                         public long ReadId () {
631                                 return decode_id (packet, ref offset);
632                         }
633
634                         public long ReadLong () {
635                                 return decode_long (packet, ref offset);
636                         }
637
638                         public float ReadFloat () {
639                                 float f = DataConverter.FloatFromBE (packet, offset);
640                                 offset += 4;
641                                 return f;
642                         }
643
644                         public double ReadDouble () {
645                                 double d = DataConverter.DoubleFromBE (packet, offset);
646                                 offset += 8;
647                                 return d;
648                         }
649
650                         public string ReadString () {
651                                 int len = decode_int (packet, ref offset);
652                                 string res = new String (Encoding.UTF8.GetChars (packet, offset, len));
653                                 offset += len;
654                                 return res;
655                         }
656
657                         public ValueImpl ReadValue () {
658                                 ElementType etype = (ElementType)ReadByte ();
659
660                                 switch (etype) {
661                                 case ElementType.Void:
662                                         return new ValueImpl { Type = etype };
663                                 case ElementType.I1:
664                                         return new ValueImpl { Type = etype, Value = (sbyte)ReadInt () };
665                                 case ElementType.U1:
666                                         return new ValueImpl { Type = etype, Value = (byte)ReadInt () };
667                                 case ElementType.Boolean:
668                                         return new ValueImpl { Type = etype, Value = ReadInt () != 0 };
669                                 case ElementType.I2:
670                                         return new ValueImpl { Type = etype, Value = (short)ReadInt () };
671                                 case ElementType.U2:
672                                         return new ValueImpl { Type = etype, Value = (ushort)ReadInt () };
673                                 case ElementType.Char:
674                                         return new ValueImpl { Type = etype, Value = (char)ReadInt () };
675                                 case ElementType.I4:
676                                         return new ValueImpl { Type = etype, Value = ReadInt () };
677                                 case ElementType.U4:
678                                         return new ValueImpl { Type = etype, Value = (uint)ReadInt () };
679                                 case ElementType.I8:
680                                         return new ValueImpl { Type = etype, Value = ReadLong () };
681                                 case ElementType.U8:
682                                         return new ValueImpl { Type = etype, Value = (ulong)ReadLong () };
683                                 case ElementType.R4:
684                                         return new ValueImpl { Type = etype, Value = ReadFloat () };
685                                 case ElementType.R8:
686                                         return new ValueImpl { Type = etype, Value = ReadDouble () };
687                                 case ElementType.I:
688                                 case ElementType.U:
689                                 case ElementType.Ptr:
690                                         // FIXME: The client and the debuggee might have different word sizes
691                                         return new ValueImpl { Type = etype, Value = ReadLong () };
692                                 case ElementType.String:
693                                 case ElementType.SzArray:
694                                 case ElementType.Class:
695                                 case ElementType.Array:
696                                 case ElementType.Object:
697                                         long objid = ReadId ();
698                                         return new ValueImpl () { Type = etype, Objid = objid };
699                                 case ElementType.ValueType:
700                                         bool is_enum = ReadByte () == 1;
701                                         long klass = ReadId ();
702                                         long nfields = ReadInt ();
703                                         ValueImpl[] fields = new ValueImpl [nfields];
704                                         for (int i = 0; i < nfields; ++i)
705                                                 fields [i] = ReadValue ();
706                                         return new ValueImpl () { Type = etype, Klass = klass, Fields = fields, IsEnum = is_enum };
707                                 case (ElementType)ValueTypeId.VALUE_TYPE_ID_NULL:
708                                         return new ValueImpl { Type = etype };
709                                 case (ElementType)ValueTypeId.VALUE_TYPE_ID_TYPE:
710                                         return new ValueImpl () { Type = etype, Id = ReadId () };
711                                 default:
712                                         throw new NotImplementedException ("Unable to handle type " + etype);
713                                 }
714                         }
715                 }
716
717                 class PacketWriter {
718
719                         byte[] data;
720                         int offset;
721
722                         public PacketWriter () {
723                                 data = new byte [1024];
724                                 offset = 0;
725                         }
726
727                         void MakeRoom (int size) {
728                                 if (offset + size >= data.Length) {
729                                         int new_len = data.Length * 2;
730                                         while (new_len < offset + size) {
731                                                 new_len *= 2;
732                                         }
733                                         byte[] new_data = new byte [new_len];
734                                         Array.Copy (data, new_data, data.Length);
735                                         data = new_data;
736                                 }
737                         }
738
739                         public PacketWriter WriteByte (byte val) {
740                                 MakeRoom (1);
741                                 encode_byte (data, val, ref offset);
742                                 return this;
743                         }
744
745                         public PacketWriter WriteInt (int val) {
746                                 MakeRoom (4);
747                                 encode_int (data, val, ref offset);
748                                 return this;
749                         }
750
751                         public PacketWriter WriteId (long id) {
752                                 MakeRoom (8);
753                                 encode_id (data, id, ref offset);
754                                 return this;
755                         }
756
757                         public PacketWriter WriteLong (long val) {
758                                 MakeRoom (8);
759                                 encode_long (data, val, ref offset);
760                                 return this;
761                         }
762
763                         public PacketWriter WriteFloat (float f) {
764                                 MakeRoom (8);
765                                 byte[] b = DataConverter.GetBytesBE (f);
766                                 for (int i = 0; i < 4; ++i)
767                                         data [offset + i] = b [i];
768                                 offset += 4;
769                                 return this;
770                         }
771
772                         public PacketWriter WriteDouble (double d) {
773                                 MakeRoom (8);
774                                 byte[] b = DataConverter.GetBytesBE (d);
775                                 for (int i = 0; i < 8; ++i)
776                                         data [offset + i] = b [i];
777                                 offset += 8;
778                                 return this;
779                         }
780
781                         public PacketWriter WriteInts (int[] ids) {
782                                 for (int i = 0; i < ids.Length; ++i)
783                                         WriteInt (ids [i]);
784                                 return this;
785                         }
786
787                         public PacketWriter WriteIds (long[] ids) {
788                                 for (int i = 0; i < ids.Length; ++i)
789                                         WriteId (ids [i]);
790                                 return this;
791                         }
792
793                         public PacketWriter WriteString (string s) {
794                                 encode_int (data, s.Length, ref offset);
795                                 byte[] b = Encoding.UTF8.GetBytes (s);
796                                 MakeRoom (b.Length);
797                                 Buffer.BlockCopy (b, 0, data, offset, b.Length);
798                                 offset += b.Length;
799                                 return this;
800                         }
801
802                         public PacketWriter WriteBool (bool val) {
803                                 WriteByte (val ? (byte)1 : (byte)0);
804                                 return this;
805                         }
806
807                         public PacketWriter WriteValue (ValueImpl v) {
808                                 ElementType t;
809
810                                 if (v.Value != null)
811                                         t = TypeCodeToElementType (Type.GetTypeCode (v.Value.GetType ()));
812                                 else
813                                         t = v.Type;
814                                 WriteByte ((byte)t);
815                                 switch (t) {
816                                 case ElementType.Boolean:
817                                         WriteInt ((bool)v.Value ? 1 : 0);
818                                         break;
819                                 case ElementType.Char:
820                                         WriteInt ((int)(char)v.Value);
821                                         break;
822                                 case ElementType.I1:
823                                         WriteInt ((int)(sbyte)v.Value);
824                                         break;
825                                 case ElementType.U1:
826                                         WriteInt ((int)(byte)v.Value);
827                                         break;
828                                 case ElementType.I2:
829                                         WriteInt ((int)(short)v.Value);
830                                         break;
831                                 case ElementType.U2:
832                                         WriteInt ((int)(ushort)v.Value);
833                                         break;
834                                 case ElementType.I4:
835                                         WriteInt ((int)(int)v.Value);
836                                         break;
837                                 case ElementType.U4:
838                                         WriteInt ((int)(uint)v.Value);
839                                         break;
840                                 case ElementType.I8:
841                                         WriteLong ((long)(long)v.Value);
842                                         break;
843                                 case ElementType.U8:
844                                         WriteLong ((long)(ulong)v.Value);
845                                         break;
846                                 case ElementType.R4:
847                                         WriteFloat ((float)v.Value);
848                                         break;
849                                 case ElementType.R8:
850                                         WriteDouble ((double)v.Value);
851                                         break;
852                                 case ElementType.String:
853                                 case ElementType.SzArray:
854                                 case ElementType.Class:
855                                 case ElementType.Array:
856                                 case ElementType.Object:
857                                         WriteId (v.Objid);
858                                         break;
859                                 case ElementType.ValueType:
860                                         // FIXME: 
861                                         if (v.IsEnum)
862                                                 throw new NotImplementedException ();
863                                         WriteByte (0);
864                                         WriteId (v.Klass);
865                                         WriteInt (v.Fields.Length);
866                                         for (int i = 0; i < v.Fields.Length; ++i)
867                                                 WriteValue (v.Fields [i]);
868                                         break;
869                                 case (ElementType)ValueTypeId.VALUE_TYPE_ID_NULL:
870                                         break;
871                                 default:
872                                         throw new NotImplementedException ();
873                                 }
874
875                                 return this;
876                         }
877
878                         public PacketWriter WriteValues (ValueImpl[] values) {
879                                 for (int i = 0; i < values.Length; ++i)
880                                         WriteValue (values [i]);
881                                 return this;
882                         }
883
884                         public byte[] Data {
885                                 get {
886                                         return data;
887                                 }
888                         }
889
890                         public int Offset {
891                                 get {
892                                         return offset;
893                                 }
894                         }
895                 }
896
897                 delegate void ReplyCallback (int packet_id, byte[] packet);
898
899                 Socket socket;
900                 bool closed;
901                 Thread receiver_thread;
902                 Dictionary<int, byte[]> reply_packets;
903                 Dictionary<int, ReplyCallback> reply_cbs;
904                 object reply_packets_monitor;
905
906                 public event EventHandler<ErrorHandlerEventArgs> ErrorHandler;
907
908                 public Connection (Socket socket) {
909                         this.socket = socket;
910                         //socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.NoDelay, 1);
911                         closed = false;
912                         reply_packets = new Dictionary<int, byte[]> ();
913                         reply_cbs = new Dictionary<int, ReplyCallback> ();
914                         reply_packets_monitor = new Object ();
915                 }
916
917                 int Receive (byte[] buf, int buf_offset, int len) {
918                         int offset = 0;
919
920                         while (offset < len) {
921                                 int n = socket.Receive (buf, buf_offset + offset, len - offset, SocketFlags.None);
922
923                                 if (n == 0)
924                                         return offset;
925                                 offset += n;
926                         }
927
928                         return offset;
929                 }
930
931                 public VersionInfo Version;
932
933                 // Do the wire protocol handshake
934                 public void Connect () {
935                         byte[] buf = new byte [HANDSHAKE_STRING.Length];
936                         char[] cbuf = new char [buf.Length];
937
938                         // FIXME: Add a timeout
939                         int n = Receive (buf, 0, buf.Length);
940                         if (n == 0)
941                                 throw new IOException ("DWP Handshake failed.");
942                         for (int i = 0; i < buf.Length; ++i)
943                                 cbuf [i] = (char)buf [i];
944
945                         if (new String (cbuf) != HANDSHAKE_STRING)
946                                 throw new IOException ("DWP Handshake failed.");
947
948                         socket.Send (buf);
949
950                         receiver_thread = new Thread (new ThreadStart (receiver_thread_main));
951                         receiver_thread.Start ();
952
953                         Version = VM_GetVersion ();
954
955                         //
956                         // Tell the debuggee our protocol version, so newer debuggees can work
957                         // with older clients
958                         //
959
960                         //
961                         // Older debuggees might not support this request
962                         EventHandler<ErrorHandlerEventArgs> OrigErrorHandler = ErrorHandler;
963                         ErrorHandler = null;
964                         ErrorHandler += delegate (object sender, ErrorHandlerEventArgs args) {
965                                 throw new NotSupportedException ();
966                         };
967                         try {
968                                 VM_SetProtocolVersion (MAJOR_VERSION, MINOR_VERSION);
969                         } catch (NotSupportedException) {
970                         }
971                         ErrorHandler = OrigErrorHandler;
972                 }
973
974                 public EndPoint EndPoint {
975                         get {
976                                 return socket.RemoteEndPoint;
977                         }
978                 }
979
980                 public byte[] ReadPacket () {
981                         // FIXME: Throw ClosedConnectionException () if the connection is closed
982                         // FIXME: Throw ClosedConnectionException () if another thread closes the connection
983                         // FIXME: Locking
984                         byte[] header = new byte [HEADER_LENGTH];
985
986                         int len = Receive (header, 0, header.Length);
987                         if (len == 0)
988                                 return new byte [0];
989                         if (len != HEADER_LENGTH) {
990                                 // FIXME:
991                                 throw new IOException ("Packet of length " + len + " is read.");
992                         }
993
994                         int packetLength = GetPacketLength (header);
995                         if (packetLength < 11)
996                                 throw new IOException ("Invalid packet length.");
997
998                         if (packetLength == 11) {
999                                 return header;
1000                         } else {
1001                                 byte[] buf = new byte [packetLength];
1002                                 for (int i = 0; i < header.Length; ++i)
1003                                         buf [i] = header [i];
1004                                 len = Receive (buf, header.Length, packetLength - header.Length);
1005                                 if (len != packetLength - header.Length)
1006                                         throw new IOException ();
1007                                 return buf;
1008                         }
1009                 }
1010
1011                 public void WritePacket (byte[] packet) {
1012                         // FIXME: Throw ClosedConnectionException () if the connection is closed
1013                         // FIXME: Throw ClosedConnectionException () if another thread closes the connection
1014                         // FIXME: Locking
1015                         socket.Send (packet);
1016                 }
1017
1018                 public void Close () {
1019                         closed = true;
1020                 }
1021
1022                 public bool IsClosed {
1023                         get {
1024                                 return closed;
1025                         }
1026                 }
1027
1028                 bool disconnected;
1029
1030                 void receiver_thread_main () {
1031                         while (true) {
1032                                 try {
1033                                         bool res = ReceivePacket ();
1034                                         if (!res)
1035                                                 break;
1036                                 } catch (Exception ex) {
1037                                         Console.WriteLine (ex);
1038                                         break;
1039                                 }
1040                         }
1041
1042                         lock (reply_packets_monitor) {
1043                                 disconnected = true;
1044                                 Monitor.PulseAll (reply_packets_monitor);
1045                         }
1046                         EventHandler.VMDisconnect (0, 0, null);
1047                 }
1048
1049                 bool ReceivePacket () {
1050                                 byte[] packet = ReadPacket ();
1051
1052                                 if (packet.Length == 0) {
1053                                         return false;
1054                                 }
1055
1056                                 if (IsReplyPacket (packet)) {
1057                                         int id = GetPacketId (packet);
1058                                         ReplyCallback cb = null;
1059                                         lock (reply_packets_monitor) {
1060                                                 reply_cbs.TryGetValue (id, out cb);
1061                                                 if (cb == null) {
1062                                                         reply_packets [id] = packet;
1063                                                         Monitor.PulseAll (reply_packets_monitor);
1064                                                 }
1065                                         }
1066
1067                                         if (cb != null)
1068                                                 cb.Invoke (id, packet);
1069                                 } else {
1070                                         PacketReader r = new PacketReader (packet);
1071
1072                                         if (r.CommandSet == CommandSet.EVENT && r.Command == (int)CmdEvent.COMPOSITE) {
1073                                                 int spolicy = r.ReadByte ();
1074                                                 int nevents = r.ReadInt ();
1075
1076                                                 SuspendPolicy suspend_policy = decode_suspend_policy (spolicy);
1077
1078                                                 EventInfo[] events = new EventInfo [nevents];
1079
1080                                                 for (int i = 0; i < nevents; ++i) {
1081                                                         EventKind kind = (EventKind)r.ReadByte ();
1082                                                         int req_id = r.ReadInt ();
1083
1084                                                         EventType etype = (EventType)kind;
1085
1086                                                         if (kind == EventKind.VM_START) {
1087                                                                 long thread_id = r.ReadId ();
1088                                                                 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id };
1089                                                                 //EventHandler.VMStart (req_id, thread_id, null);
1090                                                         } else if (kind == EventKind.VM_DEATH) {
1091                                                                 //EventHandler.VMDeath (req_id, 0, null);
1092                                                                 events [i] = new EventInfo (etype, req_id) { };
1093                                                         } else if (kind == EventKind.THREAD_START) {
1094                                                                 long thread_id = r.ReadId ();
1095                                                                 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = thread_id };
1096                                                                 //EventHandler.ThreadStart (req_id, thread_id, thread_id);
1097                                                         } else if (kind == EventKind.THREAD_DEATH) {
1098                                                                 long thread_id = r.ReadId ();
1099                                                                 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = thread_id };
1100                                                                 //EventHandler.ThreadDeath (req_id, thread_id, thread_id);
1101                                                         } else if (kind == EventKind.ASSEMBLY_LOAD) {
1102                                                                 long thread_id = r.ReadId ();
1103                                                                 long id = r.ReadId ();
1104                                                                 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1105                                                                 //EventHandler.AssemblyLoad (req_id, thread_id, id);
1106                                                         } else if (kind == EventKind.ASSEMBLY_UNLOAD) {
1107                                                                 long thread_id = r.ReadId ();
1108                                                                 long id = r.ReadId ();
1109                                                                 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1110                                                                 //EventHandler.AssemblyUnload (req_id, thread_id, id);
1111                                                         } else if (kind == EventKind.TYPE_LOAD) {
1112                                                                 long thread_id = r.ReadId ();
1113                                                                 long id = r.ReadId ();
1114                                                                 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1115                                                                 //EventHandler.TypeLoad (req_id, thread_id, id);
1116                                                         } else if (kind == EventKind.METHOD_ENTRY) {
1117                                                                 long thread_id = r.ReadId ();
1118                                                                 long id = r.ReadId ();
1119                                                                 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1120                                                                 //EventHandler.MethodEntry (req_id, thread_id, id);
1121                                                         } else if (kind == EventKind.METHOD_EXIT) {
1122                                                                 long thread_id = r.ReadId ();
1123                                                                 long id = r.ReadId ();
1124                                                                 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1125                                                                 //EventHandler.MethodExit (req_id, thread_id, id);
1126                                                         } else if (kind == EventKind.BREAKPOINT) {
1127                                                                 long thread_id = r.ReadId ();
1128                                                                 long id = r.ReadId ();
1129                                                                 long loc = r.ReadLong ();
1130                                                                 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1131                                                                 //EventHandler.Breakpoint (req_id, thread_id, id, loc);
1132                                                         } else if (kind == EventKind.STEP) {
1133                                                                 long thread_id = r.ReadId ();
1134                                                                 long id = r.ReadId ();
1135                                                                 long loc = r.ReadLong ();
1136                                                                 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1137                                                                 //EventHandler.Step (req_id, thread_id, id, loc);
1138                                                         } else if (kind == EventKind.EXCEPTION) {
1139                                                                 long thread_id = r.ReadId ();
1140                                                                 long id = r.ReadId ();
1141                                                                 long loc = 0; // FIXME
1142                                                                 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id, Location = loc };
1143                                                                 //EventHandler.Exception (req_id, thread_id, id, loc);
1144                                                         } else if (kind == EventKind.APPDOMAIN_CREATE) {
1145                                                                 long thread_id = r.ReadId ();
1146                                                                 long id = r.ReadId ();
1147                                                                 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1148                                                                 //EventHandler.AppDomainCreate (req_id, thread_id, id);
1149                                                         } else if (kind == EventKind.APPDOMAIN_UNLOAD) {
1150                                                                 long thread_id = r.ReadId ();
1151                                                                 long id = r.ReadId ();
1152                                                                 events [i] = new EventInfo (etype, req_id) { ThreadId = thread_id, Id = id };
1153                                                                 //EventHandler.AppDomainUnload (req_id, thread_id, id);
1154                                                         } else {
1155                                                                 throw new NotImplementedException ("Unknown event kind: " + kind);
1156                                                         }
1157                                                 }
1158
1159                                                 EventHandler.Events (suspend_policy, events);
1160                                         }
1161                                 }
1162
1163                                 return true;
1164                 }
1165
1166                 public IEventHandler EventHandler {
1167                         get; set;
1168                 }
1169
1170                 /* Send a request and call cb when a result is received */
1171                 int Send (CommandSet command_set, int command, PacketWriter packet, Action<PacketReader> cb) {
1172                         int id = IdGenerator;
1173
1174                         lock (reply_packets_monitor) {
1175                                 reply_cbs [id] = delegate (int packet_id, byte[] p) {
1176                                         /* Run the callback on a tp thread to avoid blocking the receive thread */
1177                                         PacketReader r = new PacketReader (p);
1178                                         cb.BeginInvoke (r, null, null);
1179                                 };
1180                         }
1181                                                 
1182                         if (packet == null)
1183                                 WritePacket (EncodePacket (id, (int)command_set, command, null, 0));
1184                         else
1185                                 WritePacket (EncodePacket (id, (int)command_set, command, packet.Data, packet.Offset));
1186
1187                         return id;
1188                 }
1189
1190                 PacketReader SendReceive (CommandSet command_set, int command, PacketWriter packet) {
1191                         int id = IdGenerator;
1192
1193                         if (packet == null)
1194                                 WritePacket (EncodePacket (id, (int)command_set, command, null, 0));
1195                         else
1196                                 WritePacket (EncodePacket (id, (int)command_set, command, packet.Data, packet.Offset));
1197
1198                         int packetId = id;
1199
1200                         /* Wait for the reply packet */
1201                         while (true) {
1202                                 lock (reply_packets_monitor) {
1203                                         if (reply_packets.ContainsKey (packetId)) {
1204                                                 byte[] reply = reply_packets [packetId];
1205                                                 reply_packets.Remove (packetId);
1206                                                 PacketReader r = new PacketReader (reply);
1207                                                 if (r.ErrorCode != 0) {
1208                                                         if (ErrorHandler != null)
1209                                                                 ErrorHandler (this, new ErrorHandlerEventArgs () { ErrorCode = (ErrorCode)r.ErrorCode });
1210                                                         throw new NotImplementedException ("No error handler set.");
1211                                                 } else {
1212                                                         return r;
1213                                                 }
1214                                         } else {
1215                                                 if (disconnected)
1216                                                         throw new VMDisconnectedException ();
1217                                                 Monitor.Wait (reply_packets_monitor);
1218                                         }
1219                                 }
1220                         }
1221                 }
1222
1223                 PacketReader SendReceive (CommandSet command_set, int command) {
1224                         return SendReceive (command_set, command, null);
1225                 }
1226
1227                 int packet_id_generator;
1228
1229                 int IdGenerator {
1230                         get {
1231                                 return Interlocked.Increment (ref packet_id_generator);
1232                         }
1233                 }
1234
1235                 CattrInfo[] ReadCattrs (PacketReader r) {
1236                         CattrInfo[] res = new CattrInfo [r.ReadInt ()];
1237                         for (int i = 0; i < res.Length; ++i) {
1238                                 CattrInfo info = new CattrInfo ();
1239                                 info.ctor_id = r.ReadId ();
1240                                 info.ctor_args = new ValueImpl [r.ReadInt ()];
1241                                 for (int j = 0; j < info.ctor_args.Length; ++j) {
1242                                         info.ctor_args [j] = r.ReadValue ();
1243                                 }
1244                                 info.named_args = new CattrNamedArgInfo [r.ReadInt ()];
1245                                 for (int j = 0; j < info.named_args.Length; ++j) {
1246                                         CattrNamedArgInfo arg = new CattrNamedArgInfo ();
1247                                         int arg_type = r.ReadByte ();
1248                                         arg.is_property = arg_type == 0x54;
1249                                         arg.id = r.ReadId ();
1250                                         arg.value = r.ReadValue ();
1251                                         info.named_args [j] = arg;
1252                                 }
1253                                 res [i] = info;
1254                         }
1255                         return res;
1256                 }
1257
1258                 static ElementType TypeCodeToElementType (TypeCode c) {
1259                         switch (c) {
1260                         case TypeCode.Boolean:
1261                                 return ElementType.Boolean;
1262                         case TypeCode.Char:
1263                                 return ElementType.Char;
1264                         case TypeCode.SByte:
1265                                 return ElementType.I1;
1266                         case TypeCode.Byte:
1267                                 return ElementType.U1;
1268                         case TypeCode.Int16:
1269                                 return ElementType.I2;
1270                         case TypeCode.UInt16:
1271                                 return ElementType.U2;
1272                         case TypeCode.Int32:
1273                                 return ElementType.I4;
1274                         case TypeCode.UInt32:
1275                                 return ElementType.U4;
1276                         case TypeCode.Int64:
1277                                 return ElementType.I8;
1278                         case TypeCode.UInt64:
1279                                 return ElementType.U8;
1280                         case TypeCode.Single:
1281                                 return ElementType.R4;
1282                         case TypeCode.Double:
1283                                 return ElementType.R8;
1284                         default:
1285                                 throw new NotImplementedException ();
1286                         }
1287                 }
1288
1289                 /*
1290                  * Implementation of debugger commands
1291                  */
1292
1293                 public VersionInfo VM_GetVersion () {
1294                         var res = SendReceive (CommandSet.VM, (int)CmdVM.VERSION, null);
1295                         VersionInfo info = new VersionInfo ();
1296                         info.VMVersion = res.ReadString ();
1297                         info.MajorVersion = res.ReadInt ();
1298                         info.MinorVersion = res.ReadInt ();
1299                         return info;
1300                 }
1301
1302                 public void VM_SetProtocolVersion (int major, int minor) {
1303                         SendReceive (CommandSet.VM, (int)CmdVM.SET_PROTOCOL_VERSION, new PacketWriter ().WriteInt (major).WriteInt (minor));
1304                 }
1305
1306                 public long[] VM_GetThreads () {
1307                         var res = SendReceive (CommandSet.VM, (int)CmdVM.ALL_THREADS, null);
1308                         int len = res.ReadInt ();
1309                         long[] arr = new long [len];
1310                         for (int i = 0; i < len; ++i)
1311                                 arr [i] = res.ReadId ();
1312                         return arr;
1313                 }
1314
1315                 public void VM_Suspend () {
1316                         SendReceive (CommandSet.VM, (int)CmdVM.SUSPEND);
1317                 }
1318
1319                 public void VM_Resume () {
1320                         SendReceive (CommandSet.VM, (int)CmdVM.RESUME);
1321                 }
1322
1323                 public void VM_Exit (int exitCode) {
1324                         SendReceive (CommandSet.VM, (int)CmdVM.EXIT, new PacketWriter ().WriteInt (exitCode));
1325                 }
1326
1327                 public void VM_Dispose () {
1328                         SendReceive (CommandSet.VM, (int)CmdVM.DISPOSE);
1329                 }
1330
1331                 public ValueImpl VM_InvokeMethod (long thread, long method, ValueImpl this_arg, ValueImpl[] arguments, InvokeFlags flags, out ValueImpl exc) {
1332                         exc = null;
1333                         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));
1334                         if (r.ReadByte () == 0) {
1335                                 exc = r.ReadValue ();
1336                                 return null;
1337                         } else {
1338                                 return r.ReadValue ();
1339                         }
1340                 }
1341
1342                 public delegate void InvokeMethodCallback (ValueImpl v, ValueImpl exc, ErrorCode error, object state);
1343
1344                 public int VM_BeginInvokeMethod (long thread, long method, ValueImpl this_arg, ValueImpl[] arguments, InvokeFlags flags, InvokeMethodCallback callback, object state) {
1345                         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) {
1346                                         ValueImpl v, exc;
1347
1348                                         if (r.ErrorCode != 0) {
1349                                                 callback (null, null, (ErrorCode)r.ErrorCode, state);
1350                                         } else {
1351                                                 if (r.ReadByte () == 0) {
1352                                                         exc = r.ReadValue ();
1353                                                         v = null;
1354                                                 } else {
1355                                                         v = r.ReadValue ();
1356                                                         exc = null;
1357                                                 }
1358
1359                                                 callback (v, exc, 0, state);
1360                                         }
1361                                 });
1362                 }
1363
1364                 public void VM_AbortInvoke (long thread, int id)
1365                 {
1366                         SendReceive (CommandSet.VM, (int)CmdVM.ABORT_INVOKE, new PacketWriter ().WriteId (thread).WriteInt (id));
1367                 }
1368
1369                 /*
1370                  * DOMAIN
1371                  */
1372
1373                 public long RootDomain {
1374                         get {
1375                                 return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_ROOT_DOMAIN, null).ReadId ();
1376                         }
1377                 }
1378
1379                 public string Domain_GetName (long id) {
1380                         return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_FRIENDLY_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1381                 }
1382
1383                 public long[] Domain_GetAssemblies (long id) {
1384                         var res = SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_ASSEMBLIES, new PacketWriter ().WriteId (id));
1385                         int count = res.ReadInt ();
1386                         long[] assemblies = new long [count];
1387                         for (int i = 0; i < count; ++i)
1388                                 assemblies [i] = res.ReadId ();
1389                         return assemblies;
1390                 }
1391
1392                 public long Domain_GetEntryAssembly (long id) {
1393                         return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_ENTRY_ASSEMBLY, new PacketWriter ().WriteId (id)).ReadId ();
1394                 }
1395
1396                 public long Domain_GetCorlib (long id) {
1397                         return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.GET_CORLIB, new PacketWriter ().WriteId (id)).ReadId ();
1398                 }
1399
1400                 public long Domain_CreateString (long id, string s) {
1401                         return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.CREATE_STRING, new PacketWriter ().WriteId (id).WriteString (s)).ReadId ();
1402                 }
1403
1404                 public long Domain_CreateBoxedValue (long id, long type_id, ValueImpl v) {
1405                         return SendReceive (CommandSet.APPDOMAIN, (int)CmdAppDomain.CREATE_BOXED_VALUE, new PacketWriter ().WriteId (id).WriteId (type_id).WriteValue (v)).ReadId ();
1406                 }
1407
1408                 /*
1409                  * METHOD
1410                  */
1411
1412                 public string Method_GetName (long id) {
1413                         return SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1414                 }
1415
1416                 public long Method_GetDeclaringType (long id) {
1417                         return SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_DECLARING_TYPE, new PacketWriter ().WriteId (id)).ReadId ();
1418                 }
1419
1420                 public DebugInfo Method_GetDebugInfo (long id) {
1421                         var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_DEBUG_INFO, new PacketWriter ().WriteId (id));
1422
1423                         DebugInfo info = new DebugInfo ();
1424                         info.max_il_offset = res.ReadInt ();
1425                         info.filename = res.ReadString ();
1426
1427                         int n_il_offsets = res.ReadInt ();
1428                         info.il_offsets = new int [n_il_offsets];
1429                         info.line_numbers = new int [n_il_offsets];
1430                         for (int i = 0; i < n_il_offsets; ++i) {
1431                                 info.il_offsets [i] = res.ReadInt ();
1432                                 info.line_numbers [i] = res.ReadInt ();
1433                         }
1434
1435                         return info;
1436                 }
1437
1438                 public ParamInfo Method_GetParamInfo (long id) {
1439                         var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_PARAM_INFO, new PacketWriter ().WriteId (id));
1440
1441                         ParamInfo info = new ParamInfo ();
1442                         info.call_conv = res.ReadInt ();
1443                         info.param_count = res.ReadInt ();
1444                         info.generic_param_count = res.ReadInt ();
1445                         info.ret_type = res.ReadId ();
1446                         info.param_types = new long [info.param_count];
1447                         for (int i = 0; i < info.param_count; ++i)
1448                                 info.param_types [i] = res.ReadId ();
1449                         info.param_names = new string [info.param_count];                       
1450                         for (int i = 0; i < info.param_count; ++i)
1451                                 info.param_names [i] = res.ReadString ();
1452
1453                         return info;
1454                 }
1455
1456                 public LocalsInfo Method_GetLocalsInfo (long id) {
1457                         var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_LOCALS_INFO, new PacketWriter ().WriteId (id));
1458
1459                         LocalsInfo info = new LocalsInfo ();
1460                         int nlocals = res.ReadInt ();
1461                         info.types = new long [nlocals];
1462                         for (int i = 0; i < nlocals; ++i)
1463                                 info.types [i] = res.ReadId ();
1464                         info.names = new string [nlocals];
1465                         for (int i = 0; i < nlocals; ++i)
1466                                 info.names [i] = res.ReadString ();
1467                         info.live_range_start = new int [nlocals];
1468                         info.live_range_end = new int [nlocals];
1469                         for (int i = 0; i < nlocals; ++i) {
1470                                 info.live_range_start [i] = res.ReadInt ();
1471                                 info.live_range_end [i] = res.ReadInt ();
1472                         }
1473
1474                         return info;
1475                 }
1476
1477                 public MethodInfo Method_GetInfo (long id) {
1478                         var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_INFO, new PacketWriter ().WriteId (id));
1479
1480                         MethodInfo info = new MethodInfo ();
1481                         info.attributes = res.ReadInt ();
1482                         info.iattributes = res.ReadInt ();
1483                         info.token = res.ReadInt ();
1484
1485                         return info;
1486                 }
1487
1488                 public MethodBodyInfo Method_GetBody (long id) {
1489                         var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.GET_BODY, new PacketWriter ().WriteId (id));
1490
1491                         MethodBodyInfo info = new MethodBodyInfo ();
1492                         info.il = new byte [res.ReadInt ()];
1493                         for (int i = 0; i < info.il.Length; ++i)
1494                                 info.il [i] = (byte)res.ReadByte ();
1495
1496                         return info;
1497                 }
1498
1499                 public ResolvedToken Method_ResolveToken (long id, int token) {
1500                         var res = SendReceive (CommandSet.METHOD, (int)CmdMethod.RESOLVE_TOKEN, new PacketWriter ().WriteId (id).WriteInt (token));
1501
1502                         TokenType type = (TokenType)res.ReadByte ();
1503                         switch (type) {
1504                         case TokenType.STRING:
1505                                 return new ResolvedToken () { Type = type, Str = res.ReadString () };
1506                         case TokenType.TYPE:
1507                         case TokenType.METHOD:
1508                         case TokenType.FIELD:
1509                                 return new ResolvedToken () { Type = type, Id = res.ReadId () };
1510                         case TokenType.UNKNOWN:
1511                                 return new ResolvedToken () { Type = type };
1512                         default:
1513                                 throw new NotImplementedException ();
1514                         }
1515                 }
1516
1517                 /*
1518                  * THREAD
1519                  */
1520
1521                 public string Thread_GetName (long id) {
1522                         return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1523                 }
1524
1525                 public FrameInfo[] Thread_GetFrameInfo (long id, int start_frame, int length) {
1526                         var res = SendReceive (CommandSet.THREAD, (int)CmdThread.GET_FRAME_INFO, new PacketWriter ().WriteId (id).WriteInt (start_frame).WriteInt (length));
1527                         int count = res.ReadInt ();
1528
1529                         var frames = new FrameInfo [count];
1530                         for (int i = 0; i < count; ++i) {
1531                                 frames [i].id = res.ReadInt ();
1532                                 frames [i].method = res.ReadId ();
1533                                 frames [i].il_offset = res.ReadInt ();
1534                                 frames [i].flags = (StackFrameFlags)res.ReadByte ();
1535                         }
1536                         return frames;
1537                 }
1538
1539                 public int Thread_GetState (long id) {
1540                         return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_STATE, new PacketWriter ().WriteId (id)).ReadInt ();
1541                 }
1542
1543                 public ThreadInfo Thread_GetInfo (long id) {
1544                         PacketReader r = SendReceive (CommandSet.THREAD, (int)CmdThread.GET_INFO, new PacketWriter ().WriteId (id));
1545
1546                         ThreadInfo res = new ThreadInfo () { is_thread_pool = r.ReadByte () > 0 ? true : false };
1547
1548                         return res;
1549                 }
1550
1551                 public long Thread_GetId (long id) {
1552                         return SendReceive (CommandSet.THREAD, (int)CmdThread.GET_ID, new PacketWriter ().WriteId (id)).ReadLong ();
1553                 }
1554
1555                 /*
1556                  * MODULE
1557                  */
1558
1559                 public ModuleInfo Module_GetInfo (long id) {
1560                         PacketReader r = SendReceive (CommandSet.MODULE, (int)CmdModule.GET_INFO, new PacketWriter ().WriteId (id));
1561                         ModuleInfo info = new ModuleInfo { Name = r.ReadString (), ScopeName = r.ReadString (), FQName = r.ReadString (), Guid = r.ReadString (), Assembly = r.ReadId () };
1562                         return info;
1563                 }
1564
1565                 /*
1566                  * ASSEMBLY
1567                  */
1568
1569                 public string Assembly_GetLocation (long id) {
1570                         return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_LOCATION, new PacketWriter ().WriteId (id)).ReadString ();
1571                 }
1572
1573                 public long Assembly_GetEntryPoint (long id) {
1574                         return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_ENTRY_POINT, new PacketWriter ().WriteId (id)).ReadId ();
1575                 }
1576
1577                 public long Assembly_GetManifestModule (long id) {
1578                         return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_MANIFEST_MODULE, new PacketWriter ().WriteId (id)).ReadId ();
1579                 }
1580
1581                 public long Assembly_GetObject (long id) {
1582                         return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_OBJECT, new PacketWriter ().WriteId (id)).ReadId ();
1583                 }
1584
1585                 public long Assembly_GetType (long id, string name, bool ignoreCase) {
1586                         return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_TYPE, new PacketWriter ().WriteId (id).WriteString (name).WriteBool (ignoreCase)).ReadId ();
1587                 }
1588
1589                 public string Assembly_GetName (long id) {
1590                         return SendReceive (CommandSet.ASSEMBLY, (int)CmdAssembly.GET_NAME, new PacketWriter ().WriteId (id)).ReadString ();
1591                 }
1592
1593                 /*
1594                  * TYPE
1595                  */
1596
1597                 public TypeInfo Type_GetInfo (long id) {
1598                         PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_INFO, new PacketWriter ().WriteId (id));
1599                         TypeInfo res = new TypeInfo ();
1600
1601                         res.ns = r.ReadString ();
1602                         res.name = r.ReadString ();
1603                         res.full_name = r.ReadString ();
1604                         res.assembly = r.ReadId ();
1605                         res.module = r.ReadId ();
1606                         res.base_type = r.ReadId ();
1607                         res.element_type = r.ReadId ();
1608                         res.token = r.ReadInt ();
1609                         res.rank = r.ReadByte ();
1610                         res.attributes = r.ReadInt ();
1611                         int b = r.ReadByte ();
1612                         res.is_byref = (b & 1) != 0;
1613                         res.is_pointer = (b & 2) != 0;
1614                         res.is_primitive = (b & 4) != 0;
1615                         res.is_valuetype = (b & 8) != 0;
1616                         res.is_enum = (b & 16) != 0;
1617
1618                         int nested_len = r.ReadInt ();
1619                         res.nested = new long [nested_len];
1620                         for (int i = 0; i < nested_len; ++i)
1621                                 res.nested [i] = r.ReadId ();
1622
1623                         return res;
1624                 }
1625
1626                 public long[] Type_GetMethods (long id) {
1627                         PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_METHODS, new PacketWriter ().WriteId (id));
1628
1629                         int n = r.ReadInt ();
1630                         long[] res = new long [n];
1631                         for (int i = 0; i < n; ++i)
1632                                 res [i] = r.ReadId ();
1633                         return res;
1634                 }
1635
1636                 public long[] Type_GetFields (long id, out string[] names, out long[] types, out int[] attrs) {
1637                         PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_FIELDS, new PacketWriter ().WriteId (id));
1638
1639                         int n = r.ReadInt ();
1640                         long[] res = new long [n];
1641                         names = new string [n];
1642                         types = new long [n];
1643                         attrs = new int [n];
1644                         for (int i = 0; i < n; ++i) {
1645                                 res [i] = r.ReadId ();
1646                                 names [i] = r.ReadString ();
1647                                 types [i] = r.ReadId ();
1648                                 attrs [i] = r.ReadInt ();
1649                         }
1650                         return res;
1651                 }
1652
1653                 public PropInfo[] Type_GetProperties (long id) {
1654                         PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_PROPERTIES, new PacketWriter ().WriteId (id));
1655
1656                         int n = r.ReadInt ();
1657                         PropInfo[] res = new PropInfo [n];
1658                         for (int i = 0; i < n; ++i) {
1659                                 res [i] = new PropInfo ();
1660                                 res [i].id = r.ReadId ();
1661                                 res [i].name = r.ReadString ();
1662                                 res [i].get_method = r.ReadId ();
1663                                 res [i].set_method = r.ReadId ();
1664                                 res [i].attrs = r.ReadInt ();
1665                         }
1666
1667                         return res;
1668                 }
1669
1670                 public long Type_GetObject (long id) {
1671                         return SendReceive (CommandSet.TYPE, (int)CmdType.GET_OBJECT, new PacketWriter ().WriteId (id)).ReadId ();
1672                 }
1673
1674                 public ValueImpl[] Type_GetValues (long id, long[] fields) {
1675                         int len = fields.Length;
1676                         PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_VALUES, new PacketWriter ().WriteId (id).WriteInt (len).WriteIds (fields));
1677
1678                         ValueImpl[] res = new ValueImpl [len];
1679                         for (int i = 0; i < len; ++i)
1680                                 res [i] = r.ReadValue ();
1681                         return res;
1682                 }                       
1683
1684                 public void Type_SetValues (long id, long[] fields, ValueImpl[] values) {
1685                         SendReceive (CommandSet.TYPE, (int)CmdType.SET_VALUES, new PacketWriter ().WriteId (id).WriteInt (fields.Length).WriteIds (fields).WriteValues (values));
1686                 }
1687
1688                 public string[] Type_GetSourceFiles (long id, bool return_full_paths) {
1689                         var r = SendReceive (CommandSet.TYPE, return_full_paths ? (int)CmdType.GET_SOURCE_FILES_2 : (int)CmdType.GET_SOURCE_FILES, new PacketWriter ().WriteId (id));
1690                         int len = r.ReadInt ();
1691                         string[] res = new string [len];
1692                         for (int i = 0; i < len; ++i)
1693                                 res [i] = r.ReadString ();
1694                         return res;
1695                 }
1696
1697                 public bool Type_IsAssignableFrom (long id, long c_id) {
1698                         return SendReceive (CommandSet.TYPE, (int)CmdType.IS_ASSIGNABLE_FROM, new PacketWriter ().WriteId (id).WriteId (c_id)).ReadByte () > 0;
1699                 }
1700
1701                 public CattrInfo[] Type_GetCustomAttributes (long id, long attr_type_id, bool inherit) {
1702                         PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_CATTRS, new PacketWriter ().WriteId (id).WriteId (attr_type_id));
1703                         return ReadCattrs (r);
1704                 }
1705
1706                 public CattrInfo[] Type_GetFieldCustomAttributes (long id, long field_id, long attr_type_id, bool inherit) {
1707                         PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_FIELD_CATTRS, new PacketWriter ().WriteId (id).WriteId (field_id).WriteId (attr_type_id));
1708                         return ReadCattrs (r);
1709                 }
1710
1711                 public CattrInfo[] Type_GetPropertyCustomAttributes (long id, long field_id, long attr_type_id, bool inherit) {
1712                         PacketReader r = SendReceive (CommandSet.TYPE, (int)CmdType.GET_PROPERTY_CATTRS, new PacketWriter ().WriteId (id).WriteId (field_id).WriteId (attr_type_id));
1713                         return ReadCattrs (r);
1714                 }
1715                         
1716                 /*
1717                  * EVENTS
1718                  */
1719
1720                 public int EnableEvent (EventType etype, SuspendPolicy suspend_policy, List<Modifier> mods) {
1721                         var w = new PacketWriter ().WriteByte ((byte)etype).WriteByte ((byte)suspend_policy);
1722                         if (mods != null) {
1723                                 if (mods.Count > 255)
1724                                         throw new NotImplementedException ();
1725                                 w.WriteByte ((byte)mods.Count);
1726                                 foreach (Modifier mod in mods) {
1727                                         if (mod is CountModifier) {
1728                                                 w.WriteByte ((byte)ModifierKind.COUNT);
1729                                                 w.WriteInt ((mod as CountModifier).Count);
1730                                         } else if (mod is LocationModifier) {
1731                                                 w.WriteByte ((byte)ModifierKind.LOCATION_ONLY);
1732                                                 w.WriteId ((mod as LocationModifier).Method);
1733                                                 w.WriteLong ((mod as LocationModifier).Location);
1734                                         } else if (mod is StepModifier) {
1735                                                 w.WriteByte ((byte)ModifierKind.STEP);
1736                                                 w.WriteId ((mod as StepModifier).Thread);
1737                                                 w.WriteInt ((mod as StepModifier).Size);
1738                                                 w.WriteInt ((mod as StepModifier).Depth);
1739                                         } else if (mod is ThreadModifier) {
1740                                                 w.WriteByte ((byte)ModifierKind.THREAD_ONLY);
1741                                                 w.WriteId ((mod as ThreadModifier).Thread);
1742                                         } else if (mod is ExceptionModifier) {
1743                                                 var em = mod as ExceptionModifier;
1744                                                 w.WriteByte ((byte)ModifierKind.EXCEPTION_ONLY);
1745                                                 w.WriteId (em.Type);
1746                                                 if (Version.MajorVersion > 2 || Version.MinorVersion > 0) {
1747                                                         /* This is only supported in protocol version 2.1 */
1748                                                         w.WriteBool (em.Caught);
1749                                                         w.WriteBool (em.Uncaught);
1750                                                 } else if (!em.Caught || !em.Uncaught) {
1751                                                         throw new NotSupportedException ("This request is not supported by the protocol version implemented by the debuggee.");
1752                                                 }
1753                                         } else if (mod is AssemblyModifier) {
1754                                                 w.WriteByte ((byte)ModifierKind.ASSEMBLY_ONLY);
1755                                                 var amod = (mod as AssemblyModifier);
1756                                                 w.WriteInt (amod.Assemblies.Length);
1757                                                 foreach (var id in amod.Assemblies)
1758                                                         w.WriteId (id);
1759                                         } else {
1760                                                 throw new NotImplementedException ();
1761                                         }
1762                                 }
1763                         } else {
1764                                 w.WriteByte (0);
1765                         }
1766                         return SendReceive (CommandSet.EVENT_REQUEST, (int)CmdEventRequest.SET, w).ReadInt ();
1767                 }
1768
1769                 public void ClearEventRequest (EventType etype, int req_id) {
1770                         SendReceive (CommandSet.EVENT_REQUEST, (int)CmdEventRequest.CLEAR, new PacketWriter ().WriteByte ((byte)etype).WriteInt (req_id));
1771                 }                       
1772
1773                 public void ClearAllBreakpoints () {
1774                         SendReceive (CommandSet.EVENT_REQUEST, (int)CmdEventRequest.CLEAR_ALL_BREAKPOINTS, new PacketWriter ());
1775                 }
1776                         
1777                 /*
1778                  * STACK FRAME
1779                  */
1780                 public ValueImpl StackFrame_GetThis (long thread_id, long id) {
1781                         PacketReader r = SendReceive (CommandSet.STACK_FRAME, (int)CmdStackFrame.GET_THIS, new PacketWriter ().WriteId (thread_id).WriteId (id));
1782                         return r.ReadValue ();
1783                 }
1784
1785                 public ValueImpl[] StackFrame_GetValues (long thread_id, long id, int[] pos) {
1786                         /* pos < 0 -> argument at pos (-pos) - 1 */
1787                         /* pos >= 0 -> local at pos */
1788                         int len = pos.Length;
1789                         PacketReader r = SendReceive (CommandSet.STACK_FRAME, (int)CmdStackFrame.GET_VALUES, new PacketWriter ().WriteId (thread_id).WriteId (id).WriteInt (len).WriteInts (pos));
1790
1791                         ValueImpl[] res = new ValueImpl [len];
1792                         for (int i = 0; i < len; ++i)
1793                                 res [i] = r.ReadValue ();
1794                         return res;
1795                 }
1796
1797                 public void StackFrame_SetValues (long thread_id, long id, int[] pos, ValueImpl[] values) {
1798                         /* pos < 0 -> argument at pos (-pos) - 1 */
1799                         /* pos >= 0 -> local at pos */
1800                         int len = pos.Length;
1801                         SendReceive (CommandSet.STACK_FRAME, (int)CmdStackFrame.SET_VALUES, new PacketWriter ().WriteId (thread_id).WriteId (id).WriteInt (len).WriteInts (pos).WriteValues (values));
1802                 }
1803
1804                 /*
1805                  * ARRAYS
1806                  */
1807                 public int[] Array_GetLength (long id, out int rank, out int[] lower_bounds) {
1808                         var r = SendReceive (CommandSet.ARRAY_REF, (int)CmdArrayRef.GET_LENGTH, new PacketWriter ().WriteId (id));
1809                         rank = r.ReadInt ();
1810                         int[] res = new int [rank];
1811                         lower_bounds = new int [rank];
1812                         for (int i = 0; i < rank; ++i) {
1813                                 res [i] = r.ReadInt ();
1814                                 lower_bounds [i] = r.ReadInt ();
1815                         }
1816                         return res;
1817                 }
1818
1819                 public ValueImpl[] Array_GetValues (long id, int index, int len) {
1820                         var r = SendReceive (CommandSet.ARRAY_REF, (int)CmdArrayRef.GET_VALUES, new PacketWriter ().WriteId (id).WriteInt (index).WriteInt (len));
1821                         ValueImpl[] res = new ValueImpl [len];
1822                         for (int i = 0; i < len; ++i)
1823                                 res [i] = r.ReadValue ();
1824                         return res;
1825                 }
1826
1827                 public void Array_SetValues (long id, int index, ValueImpl[] values) {
1828                         SendReceive (CommandSet.ARRAY_REF, (int)CmdArrayRef.SET_VALUES, new PacketWriter ().WriteId (id).WriteInt (index).WriteInt (values.Length).WriteValues (values));
1829                 }
1830
1831                 /*
1832                  * STRINGS
1833                  */
1834                 public string String_GetValue (long id) {
1835                         return SendReceive (CommandSet.STRING_REF, (int)CmdStringRef.GET_VALUE, new PacketWriter ().WriteId (id)).ReadString ();
1836                 }                       
1837
1838                 /*
1839                  * OBJECTS
1840                  */
1841                 public long Object_GetType (long id) {
1842                         return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_TYPE, new PacketWriter ().WriteId (id)).ReadId ();
1843                 }                       
1844
1845                 public long Object_GetDomain (long id) {
1846                         return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_DOMAIN, new PacketWriter ().WriteId (id)).ReadId ();
1847                 }                       
1848
1849                 public ValueImpl[] Object_GetValues (long id, long[] fields) {
1850                         int len = fields.Length;
1851                         PacketReader r = SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_VALUES, new PacketWriter ().WriteId (id).WriteInt (len).WriteIds (fields));
1852
1853                         ValueImpl[] res = new ValueImpl [len];
1854                         for (int i = 0; i < len; ++i)
1855                                 res [i] = r.ReadValue ();
1856                         return res;
1857                 }
1858
1859                 public void Object_SetValues (long id, long[] fields, ValueImpl[] values) {
1860                         SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.SET_VALUES, new PacketWriter ().WriteId (id).WriteInt (fields.Length).WriteIds (fields).WriteValues (values));
1861                 }
1862
1863                 public bool Object_IsCollected (long id) {
1864                         return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.IS_COLLECTED, new PacketWriter ().WriteId (id)).ReadInt () == 1;
1865                 }                       
1866
1867                 public long Object_GetAddress (long id) {
1868                         return SendReceive (CommandSet.OBJECT_REF, (int)CmdObjectRef.GET_ADDRESS, new PacketWriter ().WriteId (id)).ReadLong ();
1869                 }                       
1870
1871         }
1872
1873         /* This is the interface exposed by the debugger towards the debugger agent */
1874         interface IEventHandler
1875         {
1876                 void Events (SuspendPolicy suspend_policy, EventInfo[] events);
1877
1878                 void VMDisconnect (int req_id, long thread_id, string vm_uri);
1879         }
1880 }