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