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