Merge remote branch 'upstream/master'
[mono.git] / mcs / class / Mono.Debugger.Soft / Test / dtest.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Threading;
4 using System.Net;
5 using System.Reflection;
6 using System.Text;
7 using Mono.Cecil.Cil;
8 using Mono.Debugger.Soft;
9 using Diag = System.Diagnostics;
10 using System.Linq;
11
12 using NUnit.Framework;
13
14 #pragma warning disable 0219
15
16 [TestFixture]
17 public class DebuggerTests
18 {
19         VirtualMachine vm;
20         MethodMirror entry_point;
21         StepEventRequest step_req;
22
23         void AssertThrows<ExType> (Action del) where ExType : Exception {
24                 bool thrown = false;
25
26                 try {
27                         del ();
28                 } catch (ExType) {
29                         thrown = true;
30                 }
31                 Assert.IsTrue (thrown);
32         }
33
34         // No other way to pass arguments to the tests ?
35         public static bool listening = Environment.GetEnvironmentVariable ("DBG_SUSPEND") != null;
36         public static string runtime = Environment.GetEnvironmentVariable ("DBG_RUNTIME");
37         public static string agent_args = Environment.GetEnvironmentVariable ("DBG_AGENT_ARGS");
38
39         void Start (string[] args) {
40                 if (!listening) {
41                         var pi = new Diag.ProcessStartInfo ();
42
43                         if (runtime != null)
44                                 pi.FileName = runtime;
45                         else
46                                 pi.FileName = "mono";
47                         pi.Arguments = String.Join (" ", args);
48                         vm = VirtualMachineManager.Launch (pi, new LaunchOptions { AgentArgs = agent_args });
49                 } else {
50                         Console.WriteLine ("Listening...");
51                         vm = VirtualMachineManager.Listen (new IPEndPoint (IPAddress.Any, 10000));
52                 }
53
54                 var load_req = vm.CreateAssemblyLoadRequest ();
55                 load_req.Enable ();
56
57                 Event vmstart = vm.GetNextEvent ();
58                 Assert.AreEqual (EventType.VMStart, vmstart.EventType);
59
60                 vm.Resume ();
61
62                 entry_point = null;
63                 step_req = null;
64
65                 Event e;
66
67                 /* Find out the entry point */
68                 while (true) {
69                         e = vm.GetNextEvent ();
70
71                         if (e is AssemblyLoadEvent) {
72                                 AssemblyLoadEvent ae = (AssemblyLoadEvent)e;
73                                 entry_point = ae.Assembly.EntryPoint;
74                                 if (entry_point != null)
75                                         break;
76                         }
77
78                         vm.Resume ();
79                 }
80
81                 load_req.Disable ();
82         }
83
84         BreakpointEvent run_until (string name) {
85                 // String
86                 MethodMirror m = entry_point.DeclaringType.GetMethod (name);
87                 Assert.IsNotNull (m);
88                 vm.SetBreakpoint (m, 0);
89
90                 Event e = null;
91
92                 while (true) {
93                         vm.Resume ();
94                         e = vm.GetNextEvent ();
95                         if (e is BreakpointEvent)
96                                 break;
97                 }
98
99                 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
100                 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
101
102                 return (e as BreakpointEvent);
103         }
104
105         Event single_step (ThreadMirror t) {
106                 var req = vm.CreateStepRequest (t);
107                 req.Enable ();
108
109                 vm.Resume ();
110                 Event e = vm.GetNextEvent ();
111                 Assert.IsTrue (e is StepEvent);
112
113                 req.Disable ();
114
115                 return e;
116         }
117
118         void check_arg_val (StackFrame frame, int pos, Type type, object eval) {
119                 object val = frame.GetArgument (pos);
120                 Assert.IsTrue (val is PrimitiveValue);
121                 object v = (val as PrimitiveValue).Value;
122                 Assert.AreEqual (type, v.GetType ());
123                 if (eval is float)
124                         Assert.IsTrue (Math.Abs ((float)eval - (float)v) < 0.0001);
125                 else if (eval is double)
126                         Assert.IsTrue (Math.Abs ((double)eval - (double)v) < 0.0001);
127                 else
128                         Assert.AreEqual (eval, v);
129         }
130
131         void AssertValue (object expected, object val) {
132                 if (expected is string) {
133                         Assert.IsTrue (val is StringMirror);
134                         Assert.AreEqual (expected, (val as StringMirror).Value);
135                 } else if (val is StructMirror && (val as StructMirror).Type.Name == "IntPtr") {
136                         AssertValue (expected, (val as StructMirror).Fields [0]);
137                 } else {
138                         Assert.IsTrue (val is PrimitiveValue);
139                         Assert.AreEqual (expected, (val as PrimitiveValue).Value);
140                 }
141         }
142
143         [SetUp]
144         public void SetUp () {
145                 Start (new string [] { "dtest-app.exe" });
146         }
147
148         [TearDown]
149         public void TearDown () {
150                 if (vm == null)
151                         return;
152
153                 if (step_req != null)
154                         step_req.Disable ();
155
156                 vm.Resume ();
157                 while (true) {
158                         Event e = vm.GetNextEvent ();
159
160                         if (e is VMDeathEvent)
161                                 break;
162
163                         vm.Resume ();
164                 }
165         }
166
167         [Test]
168         public void SimpleBreakpoint () {
169                 Event e;
170
171                 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp1");
172                 Assert.IsNotNull (m);
173
174                 vm.SetBreakpoint (m, 0);
175
176                 vm.Resume ();
177
178                 e = vm.GetNextEvent ();
179                 Assert.AreEqual (EventType.Breakpoint, e.EventType);
180                 Assert.IsTrue (e is BreakpointEvent);
181                 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
182
183                 // Argument checking
184                 AssertThrows<ArgumentException> (delegate {
185                                 // Invalid IL offset
186                                 vm.SetBreakpoint (m, 1);
187                         });
188         }
189
190         [Test]
191         public void BreakpointsSameLocation () {
192                 Event e;
193
194                 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp2");
195                 Assert.IsNotNull (m);
196
197                 vm.SetBreakpoint (m, 0);
198                 vm.SetBreakpoint (m, 0);
199
200                 vm.Resume ();
201
202                 e = vm.GetNextEvent ();
203                 Assert.IsTrue (e is BreakpointEvent);
204                 Assert.AreEqual (m, (e as BreakpointEvent).Method);
205
206                 e = vm.GetNextEvent ();
207                 Assert.IsTrue (e is BreakpointEvent);
208                 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
209         }
210
211         [Test]
212         public void BreakpointAlreadyJITted () {
213                 Event e = run_until ("bp1");
214
215                 /* Place a breakpoint on bp3 */
216                 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp3");
217                 Assert.IsNotNull (m);
218                 vm.SetBreakpoint (m, 0);
219
220                 /* Same with generic instances */
221                 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp7");
222                 Assert.IsNotNull (m2);
223                 vm.SetBreakpoint (m2, 0);
224
225                 vm.Resume ();
226
227                 e = vm.GetNextEvent ();
228                 Assert.AreEqual (EventType.Breakpoint, e.EventType);
229                 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
230
231                 vm.Resume ();
232
233                 /* Non-shared instance */
234                 e = vm.GetNextEvent ();
235                 Assert.AreEqual (EventType.Breakpoint, e.EventType);
236                 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
237
238                 vm.Resume ();
239
240                 /* Shared instance */
241                 e = vm.GetNextEvent ();
242                 Assert.AreEqual (EventType.Breakpoint, e.EventType);
243                 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
244         }
245
246         [Test]
247         public void ClearBreakpoint () {
248                 Event e;
249
250                 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
251                 Assert.IsNotNull (m);
252                 EventRequest req1 = vm.SetBreakpoint (m, 0);
253                 EventRequest req2 = vm.SetBreakpoint (m, 0);
254
255                 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
256                 Assert.IsNotNull (m2);
257                 vm.SetBreakpoint (m2, 0);
258
259                 /* Run until bp4 */
260                 vm.Resume ();
261
262                 e = vm.GetNextEvent ();
263                 Assert.AreEqual (EventType.Breakpoint, e.EventType);
264                 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
265                 e = vm.GetNextEvent ();
266                 Assert.AreEqual (EventType.Breakpoint, e.EventType);
267                 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
268
269                 /* Clear one of them */
270                 req1.Disable ();
271
272                 vm.Resume ();
273
274                 e = vm.GetNextEvent ();
275                 Assert.AreEqual (EventType.Breakpoint, e.EventType);
276                 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
277
278                 /* Clear the other */
279                 req2.Disable ();
280
281                 vm.Resume ();
282
283                 e = vm.GetNextEvent ();
284                 Assert.AreEqual (EventType.Breakpoint, e.EventType);
285                 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
286         }
287
288         [Test]
289         public void ClearAllBreakpoints () {
290                 Event e;
291
292                 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
293                 Assert.IsNotNull (m);
294                 vm.SetBreakpoint (m, 0);
295
296                 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
297                 Assert.IsNotNull (m2);
298                 vm.SetBreakpoint (m2, 0);
299
300                 vm.ClearAllBreakpoints ();
301
302                 vm.Resume ();
303
304                 e = vm.GetNextEvent ();
305                 Assert.IsTrue (!(e is BreakpointEvent));
306                 if (e is VMDeathEvent)
307                         vm = null;
308         }
309
310         [Test]
311         public void BreakpointOnGShared () {
312                 Event e;
313
314                 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp6");
315                 Assert.IsNotNull (m);
316
317                 vm.SetBreakpoint (m, 0);
318
319                 vm.Resume ();
320
321                 e = vm.GetNextEvent ();
322                 Assert.AreEqual (EventType.Breakpoint, e.EventType);
323                 Assert.IsTrue (e is BreakpointEvent);
324                 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
325         }
326
327         [Test]
328         public void SingleStepping () {
329                 Event e = run_until ("single_stepping");
330
331                 var req = vm.CreateStepRequest (e.Thread);
332                 req.Enable ();
333
334                 step_req = req;
335
336                 // Step into ss1
337                 vm.Resume ();
338                 e = vm.GetNextEvent ();
339                 Assert.IsTrue (e is StepEvent);
340                 Assert.AreEqual ("ss1", (e as StepEvent).Method.Name);
341
342                 // Step out of ss1
343                 vm.Resume ();
344                 e = vm.GetNextEvent ();
345                 Assert.IsTrue (e is StepEvent);
346                 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
347
348                 // Change to step over
349                 req.Disable ();
350                 req.Depth = StepDepth.Over;
351                 req.Enable ();
352
353                 // Step over ss2
354                 vm.Resume ();
355                 e = vm.GetNextEvent ();
356                 Assert.IsTrue (e is StepEvent);
357                 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
358
359                 // Change to step into
360                 req.Disable ();
361                 req.Depth = StepDepth.Into;
362                 req.Enable ();
363
364                 // Step into ss3
365                 vm.Resume ();
366                 e = vm.GetNextEvent ();
367                 Assert.IsTrue (e is StepEvent);
368                 Assert.AreEqual ("ss3", (e as StepEvent).Method.Name);
369
370                 // Change to step out
371                 req.Disable ();
372                 req.Depth = StepDepth.Out;
373                 req.Enable ();
374
375                 // Step back into single_stepping
376                 vm.Resume ();
377                 e = vm.GetNextEvent ();
378                 Assert.IsTrue (e is StepEvent);
379                 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
380
381                 // Change to step into
382                 req.Disable ();
383                 req.Depth = StepDepth.Into;
384                 req.Enable ();
385
386                 // Step into ss3_2 ()
387                 vm.Resume ();
388                 e = vm.GetNextEvent ();
389                 Assert.IsTrue (e is StepEvent);
390                 Assert.AreEqual ("ss3_2", (e as StepEvent).Method.Name);
391
392                 // Change to step over
393                 req.Disable ();
394                 req.Depth = StepDepth.Over;
395                 req.Enable ();
396
397                 // Step over ss3_2_2 ()
398                 vm.Resume ();
399                 e = vm.GetNextEvent ();
400                 Assert.IsTrue (e is StepEvent);
401                 Assert.AreEqual ("ss3_2", (e as StepEvent).Method.Name);
402
403                 // Recreate the request
404                 req.Disable ();
405                 req.Enable ();
406
407                 // Step back into single_stepping () with the new request
408                 vm.Resume ();
409                 e = vm.GetNextEvent ();
410                 Assert.IsTrue (e is StepEvent);
411                 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
412
413                 // Change to step into
414                 req.Disable ();
415                 req.Depth = StepDepth.Into;
416                 req.Enable ();
417
418                 // Step into ss4 ()
419                 vm.Resume ();
420                 e = vm.GetNextEvent ();
421                 Assert.IsTrue (e is StepEvent);
422                 Assert.AreEqual ("ss4", (e as StepEvent).Method.Name);
423
424                 // Change to StepSize.Line
425                 req.Disable ();
426                 req.Depth = StepDepth.Over;
427                 req.Size = StepSize.Line;
428                 req.Enable ();
429
430                 // Step over ss1 (); ss1 ();
431                 vm.Resume ();
432                 e = vm.GetNextEvent ();
433                 Assert.IsTrue (e is StepEvent);
434
435                 // Step into ss2 ()
436                 req.Disable ();
437                 req.Depth = StepDepth.Into;
438                 req.Enable ();
439
440                 vm.Resume ();
441                 e = vm.GetNextEvent ();
442                 Assert.IsTrue (e is StepEvent);
443                 Assert.AreEqual ("ss2", (e as StepEvent).Method.Name);
444
445                 req.Disable ();
446
447                 // Run until ss5
448                 e = run_until ("ss5");
449
450                 // Add an assembly filter
451                 req.AssemblyFilter = new AssemblyMirror [] { (e as BreakpointEvent).Method.DeclaringType.Assembly };
452                 req.Enable ();
453
454                 // Step into is_even, skipping the linq stuff
455                 vm.Resume ();
456                 e = vm.GetNextEvent ();
457                 Assert.IsTrue (e is StepEvent);
458                 Assert.AreEqual ("is_even", (e as StepEvent).Method.Name);
459
460                 // FIXME: Check that single stepping works with lock (obj)
461                 
462                 req.Disable ();
463
464                 // Run until ss6
465                 e = run_until ("ss6");
466
467                 req = vm.CreateStepRequest (e.Thread);
468                 req.Depth = StepDepth.Over;
469                 req.Enable ();
470
471                 // Check that single stepping works in out-of-line bblocks
472                 vm.Resume ();
473                 e = vm.GetNextEvent ();
474                 Assert.IsTrue (e is StepEvent);
475                 vm.Resume ();
476                 e = vm.GetNextEvent ();
477                 Assert.IsTrue (e is StepEvent);
478                 Assert.AreEqual ("ss6", (e as StepEvent).Method.Name);
479
480                 req.Disable ();
481         }
482
483         [Test]
484         public void MethodEntryExit () {
485                 run_until ("single_stepping");
486
487                 var req1 = vm.CreateMethodEntryRequest ();
488                 var req2 = vm.CreateMethodExitRequest ();
489
490                 req1.Enable ();
491                 req2.Enable ();
492
493                 vm.Resume ();
494                 Event e = vm.GetNextEvent ();
495                 Assert.IsTrue (e is MethodEntryEvent);
496                 Assert.AreEqual ("ss1", (e as MethodEntryEvent).Method.Name);
497
498                 vm.Resume ();
499                 e = vm.GetNextEvent ();
500                 Assert.IsTrue (e is MethodExitEvent);
501                 Assert.AreEqual ("ss1", (e as MethodExitEvent).Method.Name);
502
503                 req1.Disable ();
504                 req2.Disable ();
505         }
506
507         [Test]
508         public void CountFilter () {
509                 run_until ("single_stepping");
510
511                 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("ss3");
512                 Assert.IsNotNull (m2);
513                 vm.SetBreakpoint (m2, 0);
514
515                 var req1 = vm.CreateMethodEntryRequest ();
516                 req1.Count = 2;
517                 req1.Enable ();
518
519                 // Enter ss2, ss1 is skipped
520                 vm.Resume ();
521                 Event e = vm.GetNextEvent ();
522                 Assert.IsTrue (e is MethodEntryEvent);
523                 Assert.AreEqual ("ss2", (e as MethodEntryEvent).Method.Name);
524
525                 // Breakpoint on ss3, the entry event is no longer reported
526                 vm.Resume ();
527                 e = vm.GetNextEvent ();
528                 Assert.IsTrue (e is BreakpointEvent);
529
530                 req1.Disable ();
531         }
532
533         [Test]
534         public void Arguments () {
535                 object val;
536
537                 var e = run_until ("arg1");
538
539                 StackFrame frame = e.Thread.GetFrames () [0];
540
541                 check_arg_val (frame, 0, typeof (sbyte), SByte.MaxValue - 5);
542                 check_arg_val (frame, 1, typeof (byte), Byte.MaxValue - 5);
543                 check_arg_val (frame, 2, typeof (bool), true);
544                 check_arg_val (frame, 3, typeof (short), Int16.MaxValue - 5);
545                 check_arg_val (frame, 4, typeof (ushort), UInt16.MaxValue - 5);
546                 check_arg_val (frame, 5, typeof (char), 'F');
547                 check_arg_val (frame, 6, typeof (int), Int32.MaxValue - 5);
548                 check_arg_val (frame, 7, typeof (uint), UInt32.MaxValue - 5);
549                 check_arg_val (frame, 8, typeof (long), Int64.MaxValue - 5);
550                 check_arg_val (frame, 9, typeof (ulong), UInt64.MaxValue - 5);
551                 check_arg_val (frame, 10, typeof (float), 1.2345f);
552                 check_arg_val (frame, 11, typeof (double), 6.78910);
553
554                 e = run_until ("arg2");
555
556                 frame = e.Thread.GetFrames () [0];
557
558                 // String
559                 val = frame.GetArgument (0);
560                 AssertValue ("FOO", val);
561                 Assert.AreEqual ("String", (val as ObjectMirror).Type.Name);
562
563                 // null
564                 val = frame.GetArgument (1);
565                 AssertValue (null, val);
566
567                 // object
568                 val = frame.GetArgument (2);
569                 AssertValue ("BLA", val);
570
571                 // byref
572                 val = frame.GetArgument (3);
573                 AssertValue (42, val);
574
575                 // generic instance
576                 val = frame.GetArgument (4);
577                 Assert.IsTrue (val is ObjectMirror);
578                 Assert.AreEqual ("GClass`1", (val as ObjectMirror).Type.Name);
579
580                 // System.Object
581                 val = frame.GetArgument (5);
582                 Assert.IsTrue (val is ObjectMirror);
583                 Assert.AreEqual ("Object", (val as ObjectMirror).Type.Name);
584
585                 // this on static methods
586                 val = frame.GetThis ();
587                 AssertValue (null, val);
588
589                 e = run_until ("arg3");
590
591                 frame = e.Thread.GetFrames () [0];
592
593                 // this
594                 val = frame.GetThis ();
595                 Assert.IsTrue (val is ObjectMirror);
596                 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
597
598                 // objref in register
599                 val = frame.GetArgument (0);
600                 AssertValue ("BLA", val);
601         }
602
603         [Test]
604         public void Arrays () {
605                 object val;
606
607                 var e = run_until ("o2");
608
609                 StackFrame frame = e.Thread.GetFrames () [0];
610
611                 // String[]
612                 val = frame.GetArgument (0);
613                 Assert.IsTrue (val is ArrayMirror);
614                 ArrayMirror arr = val as ArrayMirror;
615                 Assert.AreEqual (2, arr.Length);
616                 AssertValue ("BAR", arr [0]);
617                 AssertValue ("BAZ", arr [1]);
618
619                 var vals = arr.GetValues (0, 2);
620                 Assert.AreEqual (2, vals.Count);
621                 AssertValue ("BAR", vals [0]);
622                 AssertValue ("BAZ", vals [1]);
623
624                 arr [0] = vm.RootDomain.CreateString ("ABC");
625                 AssertValue ("ABC", arr [0]);
626
627                 arr [0] = vm.CreateValue (null);
628                 AssertValue (null, arr [0]);
629
630                 arr.SetValues (0, new Value [] { vm.RootDomain.CreateString ("D1"), vm.RootDomain.CreateString ("D2") });
631                 AssertValue ("D1", arr [0]);
632                 AssertValue ("D2", arr [1]);
633
634                 // int
635                 val = frame.GetArgument (1);
636                 Assert.IsTrue (val is ArrayMirror);
637                 arr = val as ArrayMirror;
638                 Assert.AreEqual (2, arr.Length);
639                 AssertValue (42, arr [0]);
640                 AssertValue (43, arr [1]);
641
642                 // Argument checking
643                 AssertThrows<IndexOutOfRangeException> (delegate () {
644                                 val = arr [2];
645                         });
646
647                 AssertThrows<IndexOutOfRangeException> (delegate () {
648                                 val = arr [Int32.MinValue];
649                         });
650
651                 AssertThrows<IndexOutOfRangeException> (delegate () {
652                                 vals = arr.GetValues (0, 3);
653                         });
654
655                 AssertThrows<IndexOutOfRangeException> (delegate () {
656                                 arr [2] = vm.CreateValue (null);
657                         });
658
659                 AssertThrows<IndexOutOfRangeException> (delegate () {
660                                 arr [Int32.MinValue] = vm.CreateValue (null);
661                         });
662
663                 AssertThrows<IndexOutOfRangeException> (delegate () {
664                                 arr.SetValues (0, new Value [] { null, null, null });
665                         });
666
667                 // Multidim arrays
668                 val = frame.GetArgument (2);
669                 Assert.IsTrue (val is ArrayMirror);
670                 arr = val as ArrayMirror;
671                 Assert.AreEqual (2, arr.Rank);
672                 Assert.AreEqual (4, arr.Length);
673                 Assert.AreEqual (2, arr.GetLength (0));
674                 Assert.AreEqual (2, arr.GetLength (1));
675                 Assert.AreEqual (0, arr.GetLowerBound (0));
676                 Assert.AreEqual (0, arr.GetLowerBound (1));
677                 vals = arr.GetValues (0, 4);
678                 AssertValue (1, vals [0]);
679                 AssertValue (2, vals [1]);
680                 AssertValue (3, vals [2]);
681                 AssertValue (4, vals [3]);
682
683                 val = frame.GetArgument (3);
684                 Assert.IsTrue (val is ArrayMirror);
685                 arr = val as ArrayMirror;
686                 Assert.AreEqual (2, arr.Rank);
687                 Assert.AreEqual (4, arr.Length);
688                 Assert.AreEqual (2, arr.GetLength (0));
689                 Assert.AreEqual (2, arr.GetLength (1));
690                 Assert.AreEqual (1, arr.GetLowerBound (0));
691                 Assert.AreEqual (3, arr.GetLowerBound (1));
692
693                 AssertThrows<ArgumentOutOfRangeException> (delegate () {
694                                 arr.GetLength (-1);
695                         });
696                 AssertThrows<ArgumentOutOfRangeException> (delegate () {
697                                 arr.GetLength (2);
698                         });
699
700                 AssertThrows<ArgumentOutOfRangeException> (delegate () {
701                                 arr.GetLowerBound (-1);
702                         });
703                 AssertThrows<ArgumentOutOfRangeException> (delegate () {
704                                 arr.GetLowerBound (2);
705                         });
706
707                 // arrays treated as generic collections
708                 val = frame.GetArgument (4);
709                 Assert.IsTrue (val is ArrayMirror);
710                 arr = val as ArrayMirror;
711         }
712
713         [Test]
714         public void Object_GetValue () {
715                 var e = run_until ("o1");
716                 var frame = e.Thread.GetFrames () [0];
717
718                 object val = frame.GetThis ();
719                 Assert.IsTrue (val is ObjectMirror);
720                 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
721                 ObjectMirror o = (val as ObjectMirror);
722
723                 TypeMirror t = o.Type;
724
725                 // object fields
726                 object f = o.GetValue (t.GetField ("field_i"));
727                 AssertValue (42, f);
728                 f = o.GetValue (t.GetField ("field_s"));
729                 AssertValue ("S", f);
730                 f = o.GetValue (t.GetField ("field_enum"));
731                 Assert.IsTrue (f is EnumMirror);
732                 Assert.AreEqual (1, (f as EnumMirror).Value);
733                 Assert.AreEqual ("B", (f as EnumMirror).StringValue);
734
735                 // Inherited object fields
736                 TypeMirror parent = t.BaseType;
737                 f = o.GetValue (parent.GetField ("base_field_i"));
738                 AssertValue (43, f);
739                 f = o.GetValue (parent.GetField ("base_field_s"));
740                 AssertValue ("T", f);
741
742                 // Static fields
743                 f = o.GetValue (o.Type.GetField ("static_i"));
744                 AssertValue (55, f);
745
746                 // generic instances
747                 ObjectMirror o2 = frame.GetValue (frame.Method.GetParameters ()[1]) as ObjectMirror;
748                 Assert.AreEqual ("GClass`1", o2.Type.Name);
749                 TypeMirror t2 = o2.Type;
750                 f = o2.GetValue (t2.GetField ("field"));
751                 AssertValue (42, f);
752
753                 ObjectMirror o3 = frame.GetValue (frame.Method.GetParameters ()[2]) as ObjectMirror;
754                 Assert.AreEqual ("GClass`1", o3.Type.Name);
755                 TypeMirror t3 = o3.Type;
756                 f = o3.GetValue (t3.GetField ("field"));
757                 AssertValue ("FOO", f);
758
759                 // Argument checking
760                 AssertThrows<ArgumentNullException> (delegate () {
761                         o.GetValue (null);
762                         });
763         }
764
765         [Test]
766         public void Object_GetValues () {
767                 var e = run_until ("o1");
768                 var frame = e.Thread.GetFrames () [0];
769
770                 object val = frame.GetThis ();
771                 Assert.IsTrue (val is ObjectMirror);
772                 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
773                 ObjectMirror o = (val as ObjectMirror);
774
775                 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
776
777                 TypeMirror t = o.Type;
778
779                 object[] vals = o.GetValues (new FieldInfoMirror [] { t.GetField ("field_i"), t.GetField ("field_s") });
780                 object f = vals [0];
781                 AssertValue (42, f);
782                 f = vals [1];
783                 AssertValue ("S", f);
784
785                 // Argument checking
786                 AssertThrows<ArgumentNullException> (delegate () {
787                         o.GetValues (null);
788                         });
789
790                 AssertThrows<ArgumentNullException> (delegate () {
791                         o.GetValues (new FieldInfoMirror [] { null });
792                         });
793
794                 // field of another class
795                 AssertThrows<ArgumentException> (delegate () {
796                                 o.GetValue (val2.Type.GetField ("field_j"));
797                         });
798         }
799
800         void TestSetValue (ObjectMirror o, string field_name, object val) {
801                 if (val is string)
802                         o.SetValue (o.Type.GetField (field_name), vm.RootDomain.CreateString ((string)val));
803                 else
804                         o.SetValue (o.Type.GetField (field_name), vm.CreateValue (val));
805                 Value f = o.GetValue (o.Type.GetField (field_name));
806                 AssertValue (val, f);
807         }
808
809         [Test]
810         public void Object_SetValues () {
811                 var e = run_until ("o1");
812                 var frame = e.Thread.GetFrames () [0];
813
814                 object val = frame.GetThis ();
815                 Assert.IsTrue (val is ObjectMirror);
816                 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
817                 ObjectMirror o = (val as ObjectMirror);
818
819                 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
820
821                 TestSetValue (o, "field_i", 22);
822                 TestSetValue (o, "field_bool1", false);
823                 TestSetValue (o, "field_bool2", true);
824                 TestSetValue (o, "field_char", 'B');
825                 TestSetValue (o, "field_byte", (byte)129);
826                 TestSetValue (o, "field_sbyte", (sbyte)-33);
827                 TestSetValue (o, "field_short", (short)(Int16.MaxValue - 5));
828                 TestSetValue (o, "field_ushort", (ushort)(UInt16.MaxValue - 5));
829                 TestSetValue (o, "field_long", Int64.MaxValue - 5);
830                 TestSetValue (o, "field_ulong", (ulong)(UInt64.MaxValue - 5));
831                 TestSetValue (o, "field_float", 6.28f);
832                 TestSetValue (o, "field_double", 6.28);
833                 TestSetValue (o, "static_i", 23);
834                 TestSetValue (o, "field_s", "CDEF");
835
836                 Value f;
837
838                 // intptrs
839                 f = o.GetValue (o.Type.GetField ("field_intptr"));
840                 Assert.IsInstanceOfType (typeof (StructMirror), f);
841                 AssertValue (Int32.MaxValue - 5, (f as StructMirror).Fields [0]);
842
843                 // enums
844
845                 FieldInfoMirror field = o.Type.GetField ("field_enum");
846                 f = o.GetValue (field);
847                 (f as EnumMirror).Value = 5;
848                 o.SetValue (field, f);
849                 f = o.GetValue (field);
850                 Assert.AreEqual (5, (f as EnumMirror).Value);
851
852                 // null
853                 o.SetValue (o.Type.GetField ("field_s"), vm.CreateValue (null));
854                 f = o.GetValue (o.Type.GetField ("field_s"));
855                 AssertValue (null, f);
856
857                 // vtype instances
858                 field = o.Type.GetField ("generic_field_struct");
859                 f = o.GetValue (field);
860                 o.SetValue (field, f);
861
862                 // Argument checking
863                 AssertThrows<ArgumentNullException> (delegate () {
864                                 o.SetValues (null, new Value [0]);
865                         });
866
867                 AssertThrows<ArgumentNullException> (delegate () {
868                                 o.SetValues (new FieldInfoMirror [0], null);
869                         });
870
871                 AssertThrows<ArgumentNullException> (delegate () {
872                                 o.SetValues (new FieldInfoMirror [] { null }, new Value [1] { null });
873                         });
874
875                 // vtype with a wrong type
876                 AssertThrows<ArgumentException> (delegate () {
877                                 o.SetValue (o.Type.GetField ("field_struct"), o.GetValue (o.Type.GetField ("field_enum")));
878                         });
879
880                 // reference type not assignment compatible
881                 AssertThrows<ArgumentException> (delegate () {
882                                 o.SetValue (o.Type.GetField ("field_class"), o);
883                         });
884
885                 // field of another class
886                 AssertThrows<ArgumentException> (delegate () {
887                                 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
888                         });
889         }
890
891         [Test]
892         public void Type_SetValue () {
893                 var e = run_until ("o1");
894                 var frame = e.Thread.GetFrames () [0];
895                 Value f;
896
897                 object val = frame.GetThis ();
898                 Assert.IsTrue (val is ObjectMirror);
899                 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
900                 ObjectMirror o = (val as ObjectMirror);
901
902                 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
903
904                 o.Type.SetValue (o.Type.GetField ("static_i"), vm.CreateValue (55));
905                 f = o.Type.GetValue (o.Type.GetField ("static_i"));
906                 AssertValue (55, f);
907
908                 o.Type.SetValue (o.Type.GetField ("static_s"), vm.RootDomain.CreateString ("B"));
909                 f = o.Type.GetValue (o.Type.GetField ("static_s"));
910                 AssertValue ("B", f);
911
912                 // Argument checking
913                 AssertThrows<ArgumentNullException> (delegate () {
914                                 o.Type.SetValue (null, vm.CreateValue (0));
915                         });
916
917                 AssertThrows<ArgumentNullException> (delegate () {
918                                 o.Type.SetValue (o.Type.GetField ("static_i"), null);
919                         });
920
921                 // field of another class
922                 AssertThrows<ArgumentException> (delegate () {
923                                 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
924                         });
925         }
926
927         [Test]
928         public void TypeInfo () {
929                 Event e = run_until ("ti2");
930                 StackFrame frame = e.Thread.GetFrames () [0];
931
932                 TypeMirror t;
933
934                 // Array types
935                 t = frame.Method.GetParameters ()[0].ParameterType;
936
937                 Assert.AreEqual ("String[]", t.Name);
938                 Assert.AreEqual ("string[]", t.CSharpName);
939                 Assert.AreEqual ("Array", t.BaseType.Name);
940                 Assert.AreEqual (true, t.HasElementType);
941                 Assert.AreEqual (true, t.IsArray);
942                 Assert.AreEqual (1, t.GetArrayRank ());
943                 Assert.AreEqual ("String", t.GetElementType ().Name);
944
945                 t = frame.Method.GetParameters ()[2].ParameterType;
946
947                 Assert.AreEqual ("Int32[,]", t.Name);
948                 // FIXME:
949                 //Assert.AreEqual ("int[,]", t.CSharpName);
950                 Assert.AreEqual ("Array", t.BaseType.Name);
951                 Assert.AreEqual (true, t.HasElementType);
952                 Assert.AreEqual (true, t.IsArray);
953                 Assert.AreEqual (2, t.GetArrayRank ());
954                 Assert.AreEqual ("Int32", t.GetElementType ().Name);
955
956                 // Byref types
957                 t = frame.Method.GetParameters ()[3].ParameterType;
958                 // FIXME:
959                 //Assert.AreEqual ("Int32&", t.Name);
960                 //Assert.AreEqual (true, t.IsByRef);
961                 //Assert.AreEqual (true, t.HasElementType);
962
963                 // Pointer types
964                 t = frame.Method.GetParameters ()[4].ParameterType;
965                 // FIXME:
966                 //Assert.AreEqual ("Int32*", t.Name);
967                 Assert.AreEqual (true, t.IsPointer);
968                 Assert.AreEqual (true, t.HasElementType);
969                 Assert.AreEqual ("Int32", t.GetElementType ().Name);
970                 Assert.AreEqual (false, t.IsPrimitive);
971
972                 // primitive types 
973                 t = frame.Method.GetParameters ()[5].ParameterType;
974                 Assert.AreEqual (true, t.IsPrimitive);
975
976                 // value types
977                 t = frame.Method.GetParameters ()[6].ParameterType;
978                 Assert.AreEqual ("AStruct", t.Name);
979                 Assert.AreEqual (false, t.IsPrimitive);
980                 Assert.AreEqual (true, t.IsValueType);
981                 Assert.AreEqual (false, t.IsClass);
982
983                 // reference types
984                 t = frame.Method.GetParameters ()[7].ParameterType;
985                 Assert.AreEqual ("Tests", t.Name);
986                 var nested = (from nt in t.GetNestedTypes () where nt.IsNestedPublic select nt).ToArray ();
987                 Assert.AreEqual (1, nested.Length);
988                 Assert.AreEqual ("NestedClass", nested [0].Name);
989                 Assert.IsTrue (t.BaseType.IsAssignableFrom (t));
990                 Assert.IsTrue (!t.IsAssignableFrom (t.BaseType));
991
992                 // generic instances
993                 t = frame.Method.GetParameters ()[9].ParameterType;
994                 Assert.AreEqual ("GClass`1", t.Name);
995
996                 // enums
997                 t = frame.Method.GetParameters ()[10].ParameterType;
998                 Assert.AreEqual ("AnEnum", t.Name);
999                 Assert.IsTrue (t.IsEnum);
1000                 Assert.AreEqual ("Int32", t.EnumUnderlyingType.Name);
1001
1002                 // properties
1003                 t = frame.Method.GetParameters ()[7].ParameterType;
1004
1005                 var props = t.GetProperties ();
1006                 Assert.AreEqual (3, props.Length);
1007                 foreach (PropertyInfoMirror prop in props) {
1008                         ParameterInfoMirror[] indexes = prop.GetIndexParameters ();
1009
1010                         if (prop.Name == "IntProperty") {
1011                                 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1012                                 Assert.AreEqual ("get_IntProperty", prop.GetGetMethod ().Name);
1013                                 Assert.AreEqual ("set_IntProperty", prop.GetSetMethod ().Name);
1014                                 Assert.AreEqual (0, indexes.Length);
1015                         } else if (prop.Name == "ReadOnlyProperty") {
1016                                 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1017                                 Assert.AreEqual ("get_ReadOnlyProperty", prop.GetGetMethod ().Name);
1018                                 Assert.AreEqual (null, prop.GetSetMethod ());
1019                                 Assert.AreEqual (0, indexes.Length);
1020                         } else if (prop.Name == "IndexedProperty") {
1021                                 Assert.AreEqual (1, indexes.Length);
1022                                 Assert.AreEqual ("Int32", indexes [0].ParameterType.Name);
1023                         }
1024                 }
1025
1026                 // custom attributes
1027                 t = frame.Method.GetParameters ()[8].ParameterType;
1028                 Assert.AreEqual ("Tests2", t.Name);
1029                 var attrs = t.GetCustomAttributes (true);
1030                 Assert.AreEqual (2, attrs.Length);
1031                 foreach (var attr in attrs) {
1032                         if (attr.Constructor.DeclaringType.Name == "DebuggerDisplayAttribute") {
1033                                 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1034                                 Assert.AreEqual ("Tests", attr.ConstructorArguments [0].Value);
1035                                 Assert.AreEqual (2, attr.NamedArguments.Count);
1036                                 Assert.AreEqual ("Name", attr.NamedArguments [0].Property.Name);
1037                                 Assert.AreEqual ("FOO", attr.NamedArguments [0].TypedValue.Value);
1038                                 Assert.AreEqual ("Target", attr.NamedArguments [1].Property.Name);
1039                                 Assert.IsInstanceOfType (typeof (TypeMirror), attr.NamedArguments [1].TypedValue.Value);
1040                                 Assert.AreEqual ("Int32", (attr.NamedArguments [1].TypedValue.Value as TypeMirror).Name);
1041                         } else if (attr.Constructor.DeclaringType.Name == "DebuggerTypeProxyAttribute") {
1042                                 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1043                                 Assert.IsInstanceOfType (typeof (TypeMirror), attr.ConstructorArguments [0].Value);
1044                                 Assert.AreEqual ("Tests", (attr.ConstructorArguments [0].Value as TypeMirror).Name);
1045                         } else {
1046                                 Assert.Fail (attr.Constructor.DeclaringType.Name);
1047                         }
1048                 }
1049         }
1050
1051         [Test]
1052         public void FieldInfo () {
1053                 Event e = run_until ("ti2");
1054                 StackFrame frame = e.Thread.GetFrames () [0];
1055
1056                 TypeMirror t;
1057
1058                 t = frame.Method.GetParameters ()[8].ParameterType;
1059                 Assert.AreEqual ("Tests2", t.Name);
1060
1061                 var fi = t.GetField ("field_j");
1062                 var attrs = fi.GetCustomAttributes (true);
1063                 Assert.AreEqual (1, attrs.Length);
1064                 var attr = attrs [0];
1065                 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1066                 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1067                 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1068                 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1069         }
1070
1071         [Test]
1072         public void PropertyInfo () {
1073                 Event e = run_until ("ti2");
1074                 StackFrame frame = e.Thread.GetFrames () [0];
1075
1076                 TypeMirror t;
1077
1078                 t = frame.Method.GetParameters ()[8].ParameterType;
1079                 Assert.AreEqual ("Tests2", t.Name);
1080
1081                 var pi = t.GetProperty ("AProperty");
1082                 var attrs = pi.GetCustomAttributes (true);
1083                 Assert.AreEqual (1, attrs.Length);
1084                 var attr = attrs [0];
1085                 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1086                 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1087                 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1088                 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1089         }
1090
1091         [Test]
1092         [Category ("only5")]
1093         public void Type_GetValue () {
1094                 Event e = run_until ("o1");
1095                 StackFrame frame = e.Thread.GetFrames () [0];
1096
1097                 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1098
1099                 TypeMirror t = o.Type;
1100
1101                 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1102
1103                 // static fields
1104                 object f = t.GetValue (o.Type.GetField ("static_i"));
1105                 AssertValue (55, f);
1106
1107                 f = t.GetValue (o.Type.GetField ("static_s"));
1108                 AssertValue ("A", f);
1109
1110                 // literal static fields
1111                 f = t.GetValue (o.Type.GetField ("literal_i"));
1112                 AssertValue (56, f);
1113
1114                 f = t.GetValue (o.Type.GetField ("literal_s"));
1115                 AssertValue ("B", f);
1116
1117                 // Inherited static fields
1118                 TypeMirror parent = t.BaseType;
1119                 f = t.GetValue (parent.GetField ("base_static_i"));
1120                 AssertValue (57, f);
1121
1122                 f = t.GetValue (parent.GetField ("base_static_s"));
1123                 AssertValue ("C", f);
1124
1125                 // thread static field
1126                 f = t.GetValue (t.GetField ("tls_i"), e.Thread);
1127                 AssertValue (42, f);
1128
1129                 // Argument checking
1130                 AssertThrows<ArgumentNullException> (delegate () {
1131                         t.GetValue (null);
1132                         });
1133
1134                 // instance fields
1135                 AssertThrows<ArgumentException> (delegate () {
1136                         t.GetValue (o.Type.GetField ("field_i"));
1137                         });
1138
1139                 // field on another type
1140                 AssertThrows<ArgumentException> (delegate () {
1141                                 t.GetValue (val2.Type.GetField ("static_field_j"));
1142                         });
1143
1144                 // special static field
1145                 AssertThrows<ArgumentException> (delegate () {
1146                                 t.GetValue (t.GetField ("tls_i"));
1147                         });
1148         }
1149
1150         [Test]
1151         public void Type_GetValues () {
1152                 Event e = run_until ("o1");
1153                 StackFrame frame = e.Thread.GetFrames () [0];
1154
1155                 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1156
1157                 TypeMirror t = o.Type;
1158
1159                 // static fields
1160                 object[] vals = t.GetValues (new FieldInfoMirror [] { t.GetField ("static_i"), t.GetField ("static_s") });
1161                 object f = vals [0];
1162                 AssertValue (55, f);
1163
1164                 f = vals [1];
1165                 AssertValue ("A", f);
1166
1167                 // Argument checking
1168                 AssertThrows<ArgumentNullException> (delegate () {
1169                         t.GetValues (null);
1170                         });
1171
1172                 AssertThrows<ArgumentNullException> (delegate () {
1173                         t.GetValues (new FieldInfoMirror [] { null });
1174                         });
1175         }
1176
1177         [Test]
1178         public void ObjRefs () {
1179                 Event e = run_until ("objrefs1");
1180                 StackFrame frame = e.Thread.GetFrames () [0];
1181
1182                 ObjectMirror o = frame.GetThis () as ObjectMirror;
1183                 ObjectMirror child = o.GetValue (o.Type.GetField ("child")) as ObjectMirror;
1184
1185                 Assert.IsTrue (child.Address > 0);
1186
1187                 // Check that object references are internalized correctly
1188                 Assert.AreEqual (o, frame.GetThis ());
1189
1190                 run_until ("objrefs2");
1191
1192                 // child should be gc'd now
1193                 Assert.IsTrue (child.IsCollected);
1194
1195                 AssertThrows<ObjectCollectedException> (delegate () {
1196                         TypeMirror t = child.Type;
1197                         });
1198
1199                 AssertThrows<ObjectCollectedException> (delegate () {
1200                                 long addr = child.Address;
1201                         });
1202         }
1203
1204         [Test]
1205         public void Type_GetObject () {
1206                 Event e = run_until ("o1");
1207                 StackFrame frame = e.Thread.GetFrames () [0];
1208
1209                 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1210
1211                 TypeMirror t = o.Type;
1212
1213                 Assert.AreEqual ("MonoType", t.GetTypeObject ().Type.Name);
1214         }
1215
1216         [Test]
1217         public void VTypes () {
1218                 Event e = run_until ("vtypes1");
1219                 StackFrame frame = e.Thread.GetFrames () [0];
1220
1221                 // vtypes as fields
1222                 ObjectMirror o = frame.GetThis () as ObjectMirror;
1223                 var obj = o.GetValue (o.Type.GetField ("field_struct"));
1224                 Assert.IsTrue (obj is StructMirror);
1225                 var s = obj as StructMirror;
1226                 Assert.AreEqual ("AStruct", s.Type.Name);
1227                 AssertValue (42, s ["i"]);
1228                 obj = s ["s"];
1229                 AssertValue ("S", obj);
1230                 AssertValue (43, s ["k"]);
1231                 obj = o.GetValue (o.Type.GetField ("field_boxed_struct"));
1232                 Assert.IsTrue (obj is StructMirror);
1233                 s = obj as StructMirror;
1234                 Assert.AreEqual ("AStruct", s.Type.Name);
1235                 AssertValue (42, s ["i"]);
1236
1237                 // vtypes as arguments
1238                 s = frame.GetArgument (0) as StructMirror;
1239                 AssertValue (44, s ["i"]);
1240                 obj = s ["s"];
1241                 AssertValue ("T", obj);
1242                 AssertValue (45, s ["k"]);
1243
1244                 // vtypes as array entries
1245                 var arr = frame.GetArgument (1) as ArrayMirror;
1246                 obj = arr [0];
1247                 Assert.IsTrue (obj is StructMirror);
1248                 s = obj as StructMirror;
1249                 AssertValue (1, s ["i"]);
1250                 AssertValue ("S1", s ["s"]);
1251                 obj = arr [1];
1252                 Assert.IsTrue (obj is StructMirror);
1253                 s = obj as StructMirror;
1254                 AssertValue (2, s ["i"]);
1255                 AssertValue ("S2", s ["s"]);
1256
1257                 // Argument checking
1258                 s = frame.GetArgument (0) as StructMirror;
1259                 AssertThrows<ArgumentException> (delegate () {
1260                                 obj = s ["FOO"];
1261                         });
1262
1263                 // generic vtype instances
1264                 o = frame.GetThis () as ObjectMirror;
1265                 obj = o.GetValue (o.Type.GetField ("generic_field_struct"));
1266                 Assert.IsTrue (obj is StructMirror);
1267                 s = obj as StructMirror;
1268                 Assert.AreEqual ("GStruct`1", s.Type.Name);
1269                 AssertValue (42, s ["i"]);
1270
1271                 // this on vtype methods
1272                 e = run_until ("vtypes2");
1273                 
1274                 e = single_step (e.Thread);
1275
1276                 frame = e.Thread.GetFrames () [0];
1277
1278                 Assert.AreEqual ("foo", (e as StepEvent).Method.Name);
1279                 obj = frame.GetThis ();
1280
1281                 Assert.IsTrue (obj is StructMirror);
1282                 s = obj as StructMirror;
1283                 AssertValue (44, s ["i"]);
1284                 AssertValue ("T", s ["s"]);
1285                 AssertValue (45, s ["k"]);
1286
1287                 // this on static vtype methods
1288                 e = run_until ("vtypes3");
1289
1290                 e = single_step (e.Thread);
1291
1292                 frame = e.Thread.GetFrames () [0];
1293
1294                 Assert.AreEqual ("static_foo", (e as StepEvent).Method.Name);
1295                 obj = frame.GetThis ();
1296                 AssertValue (null, obj);
1297         }
1298
1299         [Test]
1300         public void AssemblyInfo () {
1301                 Event e = run_until ("single_stepping");
1302
1303                 StackFrame frame = e.Thread.GetFrames () [0];
1304
1305                 var aname = frame.Method.DeclaringType.Assembly.GetName ();
1306                 Assert.AreEqual ("dtest-app, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", aname.ToString ());
1307
1308                 ModuleMirror m = frame.Method.DeclaringType.Module;
1309
1310                 Assert.AreEqual ("dtest-app.exe", m.Name);
1311                 Assert.AreEqual ("dtest-app.exe", m.ScopeName);
1312                 Assert.IsTrue (m.FullyQualifiedName.IndexOf ("dtest-app.exe") != -1);
1313                 Guid guid = m.ModuleVersionId;
1314                 Assert.AreEqual (frame.Method.DeclaringType.Assembly, m.Assembly);
1315                 Assert.AreEqual (frame.Method.DeclaringType.Assembly.ManifestModule, m);
1316
1317                 Assert.AreEqual ("Assembly", frame.Method.DeclaringType.Assembly.GetAssemblyObject ().Type.Name);
1318
1319                 TypeMirror t = vm.RootDomain.Corlib.GetType ("System.Diagnostics.DebuggerDisplayAttribute");
1320                 Assert.AreEqual ("DebuggerDisplayAttribute", t.Name);
1321         }
1322
1323         [Test]
1324         public void LocalsInfo () {
1325                 Event e = run_until ("locals2");
1326
1327                 StackFrame frame = e.Thread.GetFrames () [0];
1328
1329                 var locals = frame.Method.GetLocals ();
1330                 Assert.AreEqual (6, locals.Length);
1331                 for (int i = 0; i < 6; ++i) {
1332                         if (locals [i].Name == "args") {
1333                                 Assert.IsTrue (locals [i].IsArg);
1334                                 Assert.AreEqual ("String[]", locals [i].Type.Name);
1335                         } else if (locals [i].Name == "arg") {
1336                                 Assert.IsTrue (locals [i].IsArg);
1337                                 Assert.AreEqual ("Int32", locals [i].Type.Name);
1338                         } else if (locals [i].Name == "i") {
1339                                 Assert.IsFalse (locals [i].IsArg);
1340                                 Assert.AreEqual ("Int64", locals [i].Type.Name);
1341                         } else if (locals [i].Name == "j") {
1342                                 Assert.IsFalse (locals [i].IsArg);
1343                                 Assert.AreEqual ("Int32", locals [i].Type.Name);
1344                         } else if (locals [i].Name == "s") {
1345                                 Assert.IsFalse (locals [i].IsArg);
1346                                 Assert.AreEqual ("String", locals [i].Type.Name);
1347                         } else if (locals [i].Name == "t") {
1348                                 // gshared
1349                                 Assert.IsTrue (locals [i].IsArg);
1350                                 Assert.AreEqual ("String", locals [i].Type.Name);
1351                         } else {
1352                                 Assert.Fail ();
1353                         }
1354                 }
1355         }
1356
1357         [Test]
1358         [Category ("only")]
1359         public void Locals () {
1360                 var be = run_until ("locals1");
1361
1362                 StackFrame frame = be.Thread.GetFrames () [0];
1363
1364                 MethodMirror m1 = frame.Method;
1365
1366                 be = run_until ("locals2");
1367
1368                 frame = be.Thread.GetFrames () [0];
1369
1370                 object val = frame.GetValue (frame.Method.GetLocal ("i"));
1371                 AssertValue (0, val);
1372
1373                 // Execute i = 42
1374                 var req = vm.CreateStepRequest (be.Thread);
1375                 req.Enable ();
1376
1377                 vm.Resume ();
1378                 var e = vm.GetNextEvent ();
1379                 Assert.IsTrue (e is StepEvent);
1380                 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1381
1382                 // Execute s = "AB";
1383                 vm.Resume ();
1384                 e = vm.GetNextEvent ();
1385                 Assert.IsTrue (e is StepEvent);
1386                 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1387
1388                 frame = e.Thread.GetFrames () [0];
1389
1390                 val = frame.GetValue (frame.Method.GetLocal ("i"));
1391                 AssertValue (42, val);
1392
1393                 LocalVariable[] locals = frame.Method.GetLocals ();
1394                 var vals = frame.GetValues (locals);
1395                 Assert.AreEqual (locals.Length, vals.Length);
1396                 for (int i = 0; i < locals.Length; ++i) {
1397                         if (locals [i].Name == "i")
1398                                 AssertValue (42, vals [i]);
1399                         if (locals [i].Name == "s")
1400                                 AssertValue ("AB", vals [i]);
1401                         if (locals [i].Name == "t")
1402                                 AssertValue ("ABC", vals [i]);
1403                 }
1404
1405                 // Argument checking
1406
1407                 // GetValue () null
1408                 AssertThrows<ArgumentNullException> (delegate () {
1409                                 frame.GetValue ((LocalVariable)null);
1410                         });
1411                 // GetValue () local from another method
1412                 AssertThrows<ArgumentException> (delegate () {
1413                                 frame.GetValue (m1.GetLocal ("foo"));
1414                         });
1415
1416                 // GetValue () null
1417                 AssertThrows<ArgumentNullException> (delegate () {
1418                                 frame.GetValue ((ParameterInfoMirror)null);
1419                         });
1420                 // GetValue () local from another method
1421                 AssertThrows<ArgumentException> (delegate () {
1422                                 frame.GetValue (m1.GetParameters ()[0]);
1423                         });
1424
1425                 // GetValues () null
1426                 AssertThrows<ArgumentNullException> (delegate () {
1427                                 frame.GetValues (null);
1428                         });
1429                 // GetValues () embedded null
1430                 AssertThrows<ArgumentNullException> (delegate () {
1431                                 frame.GetValues (new LocalVariable [] { null });
1432                         });
1433                 // GetValues () local from another method
1434                 AssertThrows<ArgumentException> (delegate () {
1435                                 frame.GetValues (new LocalVariable [] { m1.GetLocal ("foo") });
1436                         });
1437                 // return value
1438                 AssertThrows<ArgumentException> (delegate () {
1439                                 val = frame.GetValue (frame.Method.ReturnParameter);
1440                         });
1441
1442                 // invalid stack frames
1443                 vm.Resume ();
1444                 e = vm.GetNextEvent ();
1445                 Assert.IsTrue (e is StepEvent);
1446                 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1447
1448                 AssertThrows<InvalidStackFrameException> (delegate () {
1449                                 frame.GetValue (frame.Method.GetLocal ("i"));
1450                         });
1451
1452                 req.Disable ();
1453         }
1454
1455         [Test]
1456         public void GetVisibleVariables () {
1457                 Event e = run_until ("locals4");
1458
1459                 // First scope
1460                 var locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
1461                 Assert.AreEqual (2, locals.Count);
1462                 var loc = locals.First (l => l.Name == "i");
1463                 Assert.AreEqual ("Int64", loc.Type.Name);
1464                 loc = locals.First (l => l.Name == "s");
1465                 Assert.AreEqual ("String", loc.Type.Name);
1466
1467                 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
1468                 Assert.AreEqual ("i", loc.Name);
1469                 Assert.AreEqual ("Int64", loc.Type.Name);
1470
1471                 e = run_until ("locals5");
1472
1473                 // Second scope
1474                 locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
1475                 Assert.AreEqual (2, locals.Count);
1476                 loc = locals.First (l => l.Name == "i");
1477                 Assert.AreEqual ("String", loc.Type.Name);
1478                 loc = locals.First (l => l.Name == "s");
1479                 Assert.AreEqual ("String", loc.Type.Name);
1480
1481                 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
1482                 Assert.AreEqual ("i", loc.Name);
1483                 Assert.AreEqual ("String", loc.Type.Name);
1484
1485                 // Variable in another scope
1486                 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("j");
1487                 Assert.IsNull (loc);
1488         }
1489
1490         [Test]
1491         public void Exit () {
1492                 run_until ("Main");
1493
1494                 vm.Exit (5);
1495
1496                 var e = vm.GetNextEvent ();
1497                 Assert.IsInstanceOfType (typeof (VMDeathEvent), e);
1498
1499                 var p = vm.Process;
1500                 /* Could be a remote vm with no process */
1501                 if (p != null) {
1502                         p.WaitForExit ();
1503                         Assert.AreEqual (5, p.ExitCode);
1504
1505                         // error handling
1506                         AssertThrows<VMDisconnectedException> (delegate () {
1507                                         vm.Resume ();
1508                                 });
1509                 }
1510
1511                 vm = null;
1512         }
1513
1514         [Test]
1515         public void Dispose () {
1516                 run_until ("Main");
1517
1518                 vm.Dispose ();
1519
1520                 var e = vm.GetNextEvent ();
1521                 Assert.IsInstanceOfType (typeof (VMDisconnectEvent), e);
1522
1523                 var p = vm.Process;
1524                 /* Could be a remote vm with no process */
1525                 if (p != null) {
1526                         p.WaitForExit ();
1527                         Assert.AreEqual (3, p.ExitCode);
1528
1529                         // error handling
1530                         AssertThrows<VMDisconnectedException> (delegate () {
1531                                         vm.Resume ();
1532                                 });
1533                 }
1534
1535                 vm = null;
1536         }
1537
1538         [Test]
1539         public void LineNumbers () {
1540                 Event e = run_until ("line_numbers");
1541
1542                 step_req = vm.CreateStepRequest (e.Thread);
1543                 step_req.Depth = StepDepth.Into;
1544                 step_req.Enable ();
1545
1546                 Location l;
1547                 
1548                 vm.Resume ();
1549
1550                 e = vm.GetNextEvent ();
1551                 Assert.IsTrue (e is StepEvent);
1552
1553                 l = e.Thread.GetFrames ()[0].Location;
1554
1555                 Assert.IsTrue (l.SourceFile.IndexOf ("dtest-app.cs") != -1);
1556                 Assert.AreEqual ("ln1", l.Method.Name);
1557                 
1558                 int line_base = l.LineNumber;
1559
1560                 vm.Resume ();
1561                 e = vm.GetNextEvent ();
1562                 Assert.IsTrue (e is StepEvent);
1563                 l = e.Thread.GetFrames ()[0].Location;
1564                 Assert.AreEqual ("ln2", l.Method.Name);
1565                 Assert.AreEqual (line_base + 6, l.LineNumber);
1566
1567                 vm.Resume ();
1568                 e = vm.GetNextEvent ();
1569                 Assert.IsTrue (e is StepEvent);
1570                 l = e.Thread.GetFrames ()[0].Location;
1571                 Assert.AreEqual ("ln1", l.Method.Name);
1572                 Assert.AreEqual (line_base + 1, l.LineNumber);
1573
1574                 vm.Resume ();
1575                 e = vm.GetNextEvent ();
1576                 Assert.IsTrue (e is StepEvent);
1577                 l = e.Thread.GetFrames ()[0].Location;
1578                 Assert.AreEqual ("ln3", l.Method.Name);
1579                 Assert.AreEqual (line_base + 10, l.LineNumber);
1580
1581                 vm.Resume ();
1582                 e = vm.GetNextEvent ();
1583                 Assert.IsTrue (e is StepEvent);
1584                 l = e.Thread.GetFrames ()[0].Location;
1585                 Assert.AreEqual ("ln1", l.Method.Name);
1586                 Assert.AreEqual (line_base + 2, l.LineNumber);
1587
1588                 // GetSourceFiles ()
1589                 string[] sources = l.Method.DeclaringType.GetSourceFiles ();
1590                 Assert.AreEqual (1, sources.Length);
1591                 Assert.AreEqual ("dtest-app.cs", sources [0]);
1592
1593                 sources = l.Method.DeclaringType.GetSourceFiles (true);
1594                 Assert.AreEqual (1, sources.Length);
1595                 Assert.IsTrue (sources [0].EndsWith ("dtest-app.cs"));
1596         }
1597
1598         [Test]
1599         public void Suspend () {
1600                 vm.Dispose ();
1601
1602                 Start (new string [] { "dtest-app.exe", "suspend-test" });
1603
1604                 Event e = run_until ("suspend");
1605
1606                 ThreadMirror main = e.Thread;
1607
1608                 vm.Resume ();
1609
1610                 Thread.Sleep (100);
1611
1612                 vm.Suspend ();
1613
1614                 // The debuggee should be suspended while it is running the infinite loop
1615                 // in suspend ()
1616                 StackFrame frame = main.GetFrames ()[0];
1617                 Assert.AreEqual ("suspend", frame.Method.Name);
1618
1619                 vm.Resume ();
1620
1621                 // resuming when not suspended
1622                 AssertThrows<InvalidOperationException> (delegate () {
1623                                 vm.Resume ();
1624                         });
1625
1626                 vm.Exit (0);
1627
1628                 vm = null;
1629         }
1630
1631         [Test]
1632         public void AssemblyLoad () {
1633                 Event e = run_until ("assembly_load");
1634
1635                 var load_req = vm.CreateAssemblyLoadRequest ();
1636                 load_req.Enable ();
1637
1638                 vm.Resume ();
1639
1640                 e = vm.GetNextEvent ();
1641                 Assert.IsInstanceOfType (typeof (AssemblyLoadEvent), e);
1642                 Assert.IsTrue ((e as AssemblyLoadEvent).Assembly.Location.EndsWith ("System.dll"));
1643
1644                 var frames = e.Thread.GetFrames ();
1645                 Assert.IsTrue (frames.Length > 0);
1646                 Assert.AreEqual ("assembly_load", frames [0].Method.Name);
1647         }
1648
1649         [Test]
1650         public void CreateValue () {
1651                 PrimitiveValue v;
1652
1653                 v = vm.CreateValue (1);
1654                 Assert.AreEqual (vm, v.VirtualMachine);
1655                 Assert.AreEqual (1, v.Value);
1656
1657                 v = vm.CreateValue (null);
1658                 Assert.AreEqual (vm, v.VirtualMachine);
1659                 Assert.AreEqual (null, v.Value);
1660
1661                 // Argument checking
1662                 AssertThrows <ArgumentException> (delegate () {
1663                                 v = vm.CreateValue ("FOO");
1664                         });
1665         }
1666
1667         [Test]
1668         public void CreateString () {
1669                 StringMirror s = vm.RootDomain.CreateString ("ABC");
1670
1671                 Assert.AreEqual (vm, s.VirtualMachine);
1672                 Assert.AreEqual ("ABC", s.Value);
1673                 Assert.AreEqual (vm.RootDomain, s.Domain);
1674
1675                 // Long strings
1676                 StringBuilder sb = new StringBuilder ();
1677                 for (int i = 0; i < 1024; ++i)
1678                         sb.Append ('A');
1679                 s = vm.RootDomain.CreateString (sb.ToString ());
1680
1681                 // Argument checking
1682                 AssertThrows <ArgumentNullException> (delegate () {
1683                                 s = vm.RootDomain.CreateString (null);
1684                         });
1685         }
1686
1687         [Test]
1688         public void CreateBoxedValue () {
1689                 ObjectMirror o = vm.RootDomain.CreateBoxedValue (new PrimitiveValue (vm, 42));
1690
1691                 Assert.AreEqual ("Int32", o.Type.Name);
1692                 //AssertValue (42, m.GetValue (o.Type.GetField ("m_value")));
1693
1694                 // Argument checking
1695                 AssertThrows <ArgumentNullException> (delegate () {
1696                                 vm.RootDomain.CreateBoxedValue (null);
1697                         });
1698
1699                 AssertThrows <ArgumentException> (delegate () {
1700                                 vm.RootDomain.CreateBoxedValue (o);
1701                         });
1702         }
1703
1704         [Test]
1705         public void Invoke () {
1706                 Event e = run_until ("invoke1");
1707
1708                 StackFrame frame = e.Thread.GetFrames () [0];
1709
1710                 TypeMirror t = frame.Method.DeclaringType;
1711                 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
1712
1713                 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
1714
1715                 MethodMirror m;
1716                 Value v;
1717
1718                 // return void
1719                 m = t.GetMethod ("invoke_return_void");
1720                 v = this_obj.InvokeMethod (e.Thread, m, null);
1721                 Assert.IsNull (v);
1722
1723                 // return ref
1724                 m = t.GetMethod ("invoke_return_ref");
1725                 v = this_obj.InvokeMethod (e.Thread, m, null);
1726                 AssertValue ("ABC", v);
1727
1728                 // return null
1729                 m = t.GetMethod ("invoke_return_null");
1730                 v = this_obj.InvokeMethod (e.Thread, m, null);
1731                 AssertValue (null, v);
1732
1733                 // return primitive
1734                 m = t.GetMethod ("invoke_return_primitive");
1735                 v = this_obj.InvokeMethod (e.Thread, m, null);
1736                 AssertValue (42, v);
1737
1738                 // pass primitive
1739                 m = t.GetMethod ("invoke_pass_primitive");
1740                 Value[] args = new Value [] {
1741                         vm.CreateValue ((byte)Byte.MaxValue),
1742                         vm.CreateValue ((sbyte)SByte.MaxValue),
1743                         vm.CreateValue ((short)1),
1744                         vm.CreateValue ((ushort)1),
1745                         vm.CreateValue ((int)1),
1746                         vm.CreateValue ((uint)1),
1747                         vm.CreateValue ((long)1),
1748                         vm.CreateValue ((ulong)1),
1749                         vm.CreateValue ('A'),
1750                         vm.CreateValue (true),
1751                         vm.CreateValue (3.14f),
1752                         vm.CreateValue (3.14) };
1753
1754                 v = this_obj.InvokeMethod (e.Thread, m, args);
1755                 AssertValue ((int)Byte.MaxValue + (int)SByte.MaxValue + 1 + 1 + 1 + 1 + 1 + 1 + 'A' + 1 + 3 + 3, v);
1756
1757                 // pass ref
1758                 m = t.GetMethod ("invoke_pass_ref");
1759                 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
1760                 AssertValue ("ABC", v);
1761
1762                 // pass null
1763                 m = t.GetMethod ("invoke_pass_ref");
1764                 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (null) });
1765                 AssertValue (null, v);
1766
1767                 // static
1768                 m = t.GetMethod ("invoke_static_pass_ref");
1769                 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
1770                 AssertValue ("ABC", v);
1771
1772                 // static invoked using ObjectMirror.InvokeMethod
1773                 m = t.GetMethod ("invoke_static_pass_ref");
1774                 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
1775                 AssertValue ("ABC", v);
1776
1777                 // method which throws an exception
1778                 try {
1779                         m = t.GetMethod ("invoke_throws");
1780                         v = this_obj.InvokeMethod (e.Thread, m, null);
1781                         Assert.Fail ();
1782                 } catch (InvocationException ex) {
1783                         Assert.AreEqual ("Exception", ex.Exception.Type.Name);
1784                 }
1785
1786                 // newobj
1787                 m = t.GetMethod (".ctor");
1788                 v = t.InvokeMethod (e.Thread, m, null);
1789                 Assert.IsInstanceOfType (typeof (ObjectMirror), v);
1790                 Assert.AreEqual ("Tests", (v as ObjectMirror).Type.Name);
1791
1792                 // Argument checking
1793                 
1794                 // null thread
1795                 AssertThrows<ArgumentNullException> (delegate {
1796                                 m = t.GetMethod ("invoke_pass_ref");
1797                                 v = this_obj.InvokeMethod (null, m, new Value [] { vm.CreateValue (null) });                            
1798                         });
1799
1800                 // null method
1801                 AssertThrows<ArgumentNullException> (delegate {
1802                                 v = this_obj.InvokeMethod (e.Thread, null, new Value [] { vm.CreateValue (null) });                             
1803                         });
1804
1805                 // invalid number of arguments
1806                 m = t.GetMethod ("invoke_pass_ref");
1807                 AssertThrows<ArgumentException> (delegate {
1808                                 v = this_obj.InvokeMethod (e.Thread, m, null);
1809                         });
1810
1811                 // invalid type of argument (ref != primitive)
1812                 m = t.GetMethod ("invoke_pass_ref");
1813                 AssertThrows<ArgumentException> (delegate {
1814                                 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
1815                         });
1816
1817                 // invalid type of argument (primitive != primitive)
1818                 m = t.GetMethod ("invoke_pass_primitive_2");
1819                 AssertThrows<ArgumentException> (delegate {
1820                                 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
1821                         });
1822
1823                 // invoking a non-static method as static
1824                 m = t.GetMethod ("invoke_pass_ref");
1825                 AssertThrows<ArgumentException> (delegate {
1826                                 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
1827                         });
1828
1829                 // invoking a method defined in another class
1830                 m = t2.GetMethod ("invoke");
1831                 AssertThrows<ArgumentException> (delegate {
1832                                 v = this_obj.InvokeMethod (e.Thread, m, null);
1833                         });
1834         }
1835
1836         [Test]
1837         public void InvokeVType () {
1838                 Event e = run_until ("invoke1");
1839
1840                 StackFrame frame = e.Thread.GetFrames () [0];
1841
1842                 var s = frame.GetArgument (1) as StructMirror;
1843
1844                 TypeMirror t = s.Type;
1845
1846                 MethodMirror m;
1847                 Value v;
1848
1849                 // Pass struct as this, receive int
1850                 m = t.GetMethod ("invoke_return_int");
1851                 v = s.InvokeMethod (e.Thread, m, null);
1852                 AssertValue (42, v);
1853
1854                 // Pass struct as this, receive intptr
1855                 m = t.GetMethod ("invoke_return_intptr");
1856                 v = s.InvokeMethod (e.Thread, m, null);
1857                 AssertValue (43, v);
1858
1859                 // Static method
1860                 m = t.GetMethod ("invoke_static");
1861                 v = t.InvokeMethod (e.Thread, m, null);
1862                 AssertValue (5, v);
1863
1864                 // Pass generic struct as this
1865                 s = frame.GetArgument (2) as StructMirror;
1866                 t = s.Type;
1867                 m = t.GetMethod ("invoke_return_int");
1868                 v = s.InvokeMethod (e.Thread, m, null);
1869                 AssertValue (42, v);
1870         }
1871
1872         [Test]
1873         public void BreakpointDuringInvoke () {
1874                 Event e = run_until ("invoke1");
1875
1876                 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke2");
1877                 Assert.IsNotNull (m);
1878                 vm.SetBreakpoint (m, 0);
1879
1880                 StackFrame frame = e.Thread.GetFrames () [0];
1881                 var o = frame.GetThis () as ObjectMirror;
1882
1883                 bool failed = false;
1884
1885                 bool finished = false;
1886                 object wait = new object ();
1887
1888                 // Have to invoke in a separate thread as the invoke is suspended until we
1889                 // resume after the breakpoint
1890                 Thread t = new Thread (delegate () {
1891                                 try {
1892                                         o.InvokeMethod (e.Thread, m, null);
1893                                 } catch {
1894                                         failed = true;
1895                                 }
1896                                 lock (wait) {
1897                                         finished = true;
1898                                         Monitor.Pulse (wait);
1899                                 }
1900                         });
1901
1902                 t.Start ();
1903
1904                 StackFrame invoke_frame = null;
1905
1906                 try {
1907                         e = vm.GetNextEvent ();
1908                         Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
1909                         // Check stack trace support and invokes
1910                         var frames = e.Thread.GetFrames ();
1911                         invoke_frame = frames [0];
1912                         Assert.AreEqual ("invoke2", frames [0].Method.Name);
1913                         Assert.IsTrue (frames [0].IsDebuggerInvoke);
1914                         Assert.AreEqual ("invoke1", frames [1].Method.Name);
1915                 } finally {
1916                         vm.Resume ();
1917                 }
1918
1919                 // Check that the invoke frames are no longer valid
1920                 AssertThrows<InvalidStackFrameException> (delegate {
1921                                 invoke_frame.GetThis ();
1922                         });
1923
1924                 lock (wait) {
1925                         if (!finished)
1926                                 Monitor.Wait (wait);
1927                 }
1928
1929                 // Check InvokeOptions.DisableBreakpoints flag
1930                 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
1931         }
1932
1933         [Test]
1934         public void DisabledExceptionDuringInvoke () {
1935                 Event e = run_until ("invoke_ex");
1936
1937                 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_ex_inner");
1938
1939                 StackFrame frame = e.Thread.GetFrames () [0];
1940                 var o = frame.GetThis () as ObjectMirror;
1941
1942                 var req = vm.CreateExceptionRequest (null);
1943                 req.Enable ();
1944
1945                 // Check InvokeOptions.DisableBreakpoints flag
1946                 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
1947
1948                 req.Disable ();
1949         }
1950
1951         [Test]
1952         public void InvokeSingleThreaded () {
1953                 vm.Dispose ();
1954
1955                 Start (new string [] { "dtest-app.exe", "invoke-single-threaded" });
1956
1957                 Event e = run_until ("invoke_single_threaded_2");
1958
1959                 StackFrame f = e.Thread.GetFrames ()[0];
1960
1961                 var obj = f.GetThis () as ObjectMirror;
1962
1963                 // Check that the counter value incremented by the other thread does not increase
1964                 // during the invoke.
1965                 object counter1 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
1966
1967                 var m = obj.Type.GetMethod ("invoke_return_void");
1968                 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
1969
1970             object counter2 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
1971
1972                 Assert.AreEqual ((int)counter1, (int)counter2);
1973
1974                 // Test multiple invokes done in succession
1975                 m = obj.Type.GetMethod ("invoke_return_void");
1976                 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
1977
1978                 // Test events during single-threaded invokes
1979                 vm.EnableEvents (EventType.TypeLoad);
1980                 m = obj.Type.GetMethod ("invoke_type_load");
1981                 obj.BeginInvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded, delegate {
1982                                 vm.Resume ();
1983                         }, null);
1984
1985                 e = vm.GetNextEvent ();
1986                 Assert.AreEqual (EventType.TypeLoad, e.EventType);
1987         }
1988
1989         [Test]
1990         public void GetThreads () {
1991                 vm.GetThreads ();
1992         }
1993
1994         [Test]
1995         public void Threads () {
1996                 Event e = run_until ("threads");
1997
1998                 Assert.AreEqual (ThreadState.Running, e.Thread.ThreadState);
1999
2000                 Assert.IsTrue (e.Thread.ThreadId > 0);
2001
2002                 vm.EnableEvents (EventType.ThreadStart, EventType.ThreadDeath);
2003
2004                 vm.Resume ();
2005
2006                 e = vm.GetNextEvent ();
2007                 Assert.IsInstanceOfType (typeof (ThreadStartEvent), e);
2008                 var state = e.Thread.ThreadState;
2009                 Assert.IsTrue (state == ThreadState.Running || state == ThreadState.Unstarted);
2010
2011                 vm.Resume ();
2012
2013                 e = vm.GetNextEvent ();
2014                 Assert.IsInstanceOfType (typeof (ThreadDeathEvent), e);
2015                 Assert.AreEqual (ThreadState.Stopped, e.Thread.ThreadState);
2016         }
2017
2018         [Test]
2019         [Category ("only")]
2020         public void Frame_SetValue () {
2021                 Event e = run_until ("locals2");
2022
2023                 StackFrame frame = e.Thread.GetFrames () [0];
2024
2025                 // primitive
2026                 var l = frame.Method.GetLocal ("i");
2027                 frame.SetValue (l, vm.CreateValue ((long)55));
2028                 AssertValue (55, frame.GetValue (l));
2029
2030                 // reference
2031                 l = frame.Method.GetLocal ("s");
2032                 frame.SetValue (l, vm.RootDomain.CreateString ("DEF"));
2033                 AssertValue ("DEF", frame.GetValue (l));
2034
2035                 // argument as local
2036                 l = frame.Method.GetLocal ("arg");
2037                 frame.SetValue (l, vm.CreateValue (6));
2038                 AssertValue (6, frame.GetValue (l));
2039
2040                 // argument
2041                 var p = frame.Method.GetParameters ()[1];
2042                 frame.SetValue (p, vm.CreateValue (7));
2043                 AssertValue (7, frame.GetValue (p));
2044
2045                 // gshared
2046                 p = frame.Method.GetParameters ()[2];
2047                 frame.SetValue (p, vm.RootDomain.CreateString ("DEF"));
2048                 AssertValue ("DEF", frame.GetValue (p));
2049
2050                 // argument checking
2051
2052                 // variable null
2053                 AssertThrows<ArgumentNullException> (delegate () {
2054                                 frame.SetValue ((LocalVariable)null, vm.CreateValue (55));
2055                         });
2056
2057                 // value null
2058                 AssertThrows<ArgumentNullException> (delegate () {
2059                                 l = frame.Method.GetLocal ("i");
2060                                 frame.SetValue (l, null);
2061                         });
2062
2063                 // value of invalid type
2064                 AssertThrows<ArgumentException> (delegate () {
2065                                 l = frame.Method.GetLocal ("i");
2066                                 frame.SetValue (l, vm.CreateValue (55));
2067                         });
2068         }
2069
2070         [Test]
2071         public void InvokeRegress () {
2072                 Event e = run_until ("invoke1");
2073
2074                 StackFrame frame = e.Thread.GetFrames () [0];
2075
2076                 TypeMirror t = frame.Method.DeclaringType;
2077                 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2078
2079                 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2080
2081                 MethodMirror m;
2082                 Value v;
2083
2084                 // do an invoke
2085                 m = t.GetMethod ("invoke_return_void");
2086                 v = this_obj.InvokeMethod (e.Thread, m, null);
2087                 Assert.IsNull (v);
2088
2089                 // Check that the stack frames remain valid during the invoke
2090                 Assert.AreEqual ("Tests", (frame.GetThis () as ObjectMirror).Type.Name);
2091
2092                 // do another invoke
2093                 m = t.GetMethod ("invoke_return_void");
2094                 v = this_obj.InvokeMethod (e.Thread, m, null);
2095                 Assert.IsNull (v);
2096
2097                 // Try a single step after the invoke
2098                 var req = vm.CreateStepRequest (e.Thread);
2099                 req.Depth = StepDepth.Into;
2100                 req.Size = StepSize.Line;
2101                 req.Enable ();
2102
2103                 step_req = req;
2104
2105                 // Step into invoke2
2106                 vm.Resume ();
2107                 e = vm.GetNextEvent ();
2108                 Assert.IsTrue (e is StepEvent);
2109                 Assert.AreEqual ("invoke2", (e as StepEvent).Method.Name);
2110
2111                 req.Disable ();
2112
2113                 frame = e.Thread.GetFrames () [0];
2114         }
2115
2116         [Test]
2117         public void Exceptions () {
2118                 Event e = run_until ("exceptions");
2119                 var req = vm.CreateExceptionRequest (null);
2120                 req.Enable ();
2121
2122                 vm.Resume ();
2123
2124                 e = vm.GetNextEvent ();
2125                 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2126                 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2127
2128                 var frames = e.Thread.GetFrames ();
2129                 Assert.AreEqual ("exceptions", frames [0].Method.Name);
2130                 req.Disable ();
2131
2132                 // exception type filter
2133
2134                 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.ArgumentException"));
2135                 req.Enable ();
2136
2137                 // Skip the throwing of the second OverflowException       
2138                 vm.Resume ();
2139
2140                 e = vm.GetNextEvent ();
2141                 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2142                 Assert.AreEqual ("ArgumentException", (e as ExceptionEvent).Exception.Type.Name);
2143                 req.Disable ();
2144
2145                 // exception type filter for subclasses
2146                 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.Exception"));
2147                 req.Enable ();
2148
2149                 vm.Resume ();
2150
2151                 e = vm.GetNextEvent ();
2152                 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2153                 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2154                 req.Disable ();
2155
2156                 // Implicit exceptions
2157                 req = vm.CreateExceptionRequest (null);
2158                 req.Enable ();
2159
2160                 vm.Resume ();
2161
2162                 e = vm.GetNextEvent ();
2163                 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2164                 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
2165                 req.Disable ();
2166
2167                 // Single stepping after an exception
2168                 req = vm.CreateExceptionRequest (null);
2169                 req.Enable ();
2170
2171                 vm.Resume ();
2172
2173                 e = vm.GetNextEvent ();
2174                 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2175                 Assert.AreEqual ("Exception", (e as ExceptionEvent).Exception.Type.Name);
2176                 frames = e.Thread.GetFrames ();
2177                 Assert.AreEqual ("exceptions2", frames [0].Method.Name);
2178                 req.Disable ();
2179
2180                 var sreq = vm.CreateStepRequest (e.Thread);
2181                 sreq.Depth = StepDepth.Over;
2182                 sreq.Size = StepSize.Line;
2183                 sreq.Enable ();
2184
2185                 vm.Resume ();
2186                 e = vm.GetNextEvent ();
2187                 Assert.IsInstanceOfType (typeof (StepEvent), e);
2188                 frames = e.Thread.GetFrames ();
2189                 Assert.AreEqual ("exceptions", frames [0].Method.Name);
2190                 sreq.Disable ();
2191
2192                 // Argument checking
2193                 AssertThrows<ArgumentException> (delegate {
2194                                 vm.CreateExceptionRequest (e.Thread.Type);
2195                         });
2196         }
2197
2198         [Test]
2199         public void ExceptionFilter () {
2200                 Event e = run_until ("exception_filter");
2201
2202                 MethodMirror m = entry_point.DeclaringType.GetMethod ("exception_filter_filter");
2203                 Assert.IsNotNull (m);
2204
2205                 vm.SetBreakpoint (m, 0);
2206
2207                 vm.Resume ();
2208
2209                 e = vm.GetNextEvent ();
2210                 Assert.AreEqual (EventType.Breakpoint, e.EventType);
2211                 Assert.IsTrue (e is BreakpointEvent);
2212                 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
2213
2214                 var frames = e.Thread.GetFrames ();
2215
2216                 Assert.IsTrue (frames [0].Location.SourceFile.IndexOf ("dtest-app.cs") != -1);
2217                 Assert.AreEqual ("exception_filter_filter", frames [0].Location.Method.Name);
2218
2219                 Assert.AreEqual (0, frames [1].Location.Method.MetadataToken);
2220                 Assert.AreEqual (0x0f, frames [1].Location.ILOffset);
2221
2222                 Assert.AreEqual ("exception_filter_method", frames [2].Location.Method.Name);
2223                 Assert.AreEqual (0x05, frames [2].Location.ILOffset);
2224
2225                 Assert.AreEqual (0, frames [3].Location.Method.MetadataToken, 0);
2226                 Assert.AreEqual (0, frames [3].Location.ILOffset);
2227
2228                 Assert.AreEqual ("exception_filter", frames [4].Location.Method.Name);
2229         }
2230
2231         [Test]
2232         public void ExceptionFilter2 () {
2233                 vm.Dispose ();
2234
2235                 Start (new string [] { "dtest-excfilter.exe" });
2236
2237                 MethodMirror filter_method = entry_point.DeclaringType.GetMethod ("Filter");
2238                 Assert.IsNotNull (filter_method);
2239
2240                 MethodMirror test_method = entry_point.DeclaringType.GetMethod ("Test");
2241                 Assert.IsNotNull (test_method);
2242
2243                 vm.SetBreakpoint (filter_method, 0);
2244
2245                 vm.Resume ();
2246
2247                 var e = vm.GetNextEvent ();
2248                 Assert.AreEqual (EventType.Breakpoint, e.EventType);
2249                 Assert.IsTrue (e is BreakpointEvent);
2250                 Assert.AreEqual (filter_method.Name, (e as BreakpointEvent).Method.Name);
2251
2252                 var frames = e.Thread.GetFrames ();
2253
2254                 Assert.AreEqual (4, frames.Count ());
2255
2256                 Assert.AreEqual (filter_method.Name, frames [0].Location.Method.Name);
2257                 Assert.AreEqual (20, frames [0].Location.LineNumber);
2258                 Assert.AreEqual (0, frames [0].Location.ILOffset);
2259
2260                 Assert.AreEqual (test_method.Name, frames [1].Location.Method.Name);
2261                 Assert.AreEqual (37, frames [1].Location.LineNumber);
2262                 Assert.AreEqual (0x0b, frames [1].Location.ILOffset);
2263
2264                 Assert.AreEqual (test_method.Name, frames [2].Location.Method.Name);
2265                 Assert.AreEqual (33, frames [2].Location.LineNumber);
2266                 Assert.AreEqual (0x05, frames [2].Location.ILOffset);
2267
2268                 Assert.AreEqual (entry_point.Name, frames [3].Location.Method.Name);
2269                 Assert.AreEqual (14, frames [3].Location.LineNumber);
2270                 Assert.AreEqual (0x00, frames [3].Location.ILOffset);
2271
2272                 vm.Exit (0);
2273
2274                 vm = null;
2275         }
2276
2277         [Test]
2278         public void EventSets () {
2279                 //
2280                 // Create two filter which both match the same exception
2281                 //
2282                 Event e = run_until ("exceptions");
2283
2284                 var req = vm.CreateExceptionRequest (null);
2285                 req.Enable ();
2286
2287                 var req2 = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.OverflowException"));
2288                 req2.Enable ();
2289
2290                 vm.Resume ();
2291
2292                 var es = vm.GetNextEventSet ();
2293                 Assert.AreEqual (2, es.Events.Length);
2294
2295                 e = es [0];
2296                 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2297                 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2298
2299                 e = es [1];
2300                 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2301                 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2302
2303                 req.Disable ();
2304                 req2.Disable ();
2305         }
2306
2307         //
2308         // Test single threaded invokes during processing of nullref exceptions.
2309         // These won't work if the exception handling is done from the sigsegv signal
2310         // handler, since the sigsegv signal is disabled until control returns from the
2311         // signal handler.
2312         //
2313         [Test]
2314         [Category ("only3")]
2315         public void NullRefExceptionAndSingleThreadedInvoke () {
2316                 Event e = run_until ("exceptions");
2317                 var req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.NullReferenceException"));
2318                 req.Enable ();
2319
2320                 vm.Resume ();
2321
2322                 e = vm.GetNextEvent ();
2323                 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2324                 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
2325
2326                 var ex = (e as ExceptionEvent).Exception;
2327                 var tostring_method = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2328                 ex.InvokeMethod (e.Thread, tostring_method, null, InvokeOptions.SingleThreaded);
2329         }
2330
2331         [Test]
2332         public void Domains () {
2333                 vm.Dispose ();
2334
2335                 Start (new string [] { "dtest-app.exe", "domain-test" });
2336
2337                 vm.EnableEvents (EventType.AppDomainCreate, EventType.AppDomainUnload, EventType.AssemblyUnload);
2338
2339                 Event e = run_until ("domains");
2340
2341                 vm.Resume ();
2342                 
2343                 e = vm.GetNextEvent ();
2344                 Assert.IsInstanceOfType (typeof (AppDomainCreateEvent), e);
2345
2346                 var domain = (e as AppDomainCreateEvent).Domain;
2347
2348                 // Run until the callback in the domain
2349                 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_in_domain");
2350                 Assert.IsNotNull (m);
2351                 vm.SetBreakpoint (m, 0);
2352
2353                 while (true) {
2354                         vm.Resume ();
2355                         e = vm.GetNextEvent ();
2356                         if (e is BreakpointEvent)
2357                                 break;
2358                 }
2359
2360                 Assert.AreEqual ("invoke_in_domain", (e as BreakpointEvent).Method.Name);
2361
2362                 // d_method is from another domain
2363                 MethodMirror d_method = (e as BreakpointEvent).Method;
2364                 Assert.IsTrue (m != d_method);
2365
2366                 var frames = e.Thread.GetFrames ();
2367                 Assert.AreEqual ("invoke_in_domain", frames [0].Method.Name);
2368                 Assert.AreEqual ("invoke", frames [1].Method.Name);
2369                 Assert.AreEqual ("domains", frames [2].Method.Name);
2370
2371                 // Test breakpoints on already JITted methods in other domains
2372                 m = entry_point.DeclaringType.GetMethod ("invoke_in_domain_2");
2373                 Assert.IsNotNull (m);
2374                 vm.SetBreakpoint (m, 0);
2375
2376                 while (true) {
2377                         vm.Resume ();
2378                         e = vm.GetNextEvent ();
2379                         if (e is BreakpointEvent)
2380                                 break;
2381                 }
2382
2383                 Assert.AreEqual ("invoke_in_domain_2", (e as BreakpointEvent).Method.Name);
2384
2385                 // This is empty when receiving the AppDomainCreateEvent
2386                 Assert.AreEqual ("domain", domain.FriendlyName);
2387
2388                 // Run until the unload
2389                 while (true) {
2390                         vm.Resume ();
2391                         e = vm.GetNextEvent ();
2392                         if (e is AssemblyUnloadEvent) {
2393                                 continue;
2394                         } else {
2395                                 break;
2396                         }
2397                 }
2398                 Assert.IsInstanceOfType (typeof (AppDomainUnloadEvent), e);
2399                 Assert.AreEqual (domain, (e as AppDomainUnloadEvent).Domain);
2400
2401                 // Run past the unload
2402                 e = run_until ("domains_2");
2403
2404                 // Test access to unloaded types
2405                 // FIXME: Add an exception type for this
2406                 AssertThrows<Exception> (delegate {
2407                                 d_method.DeclaringType.GetValue (d_method.DeclaringType.GetField ("static_i"));
2408                         });
2409         }
2410
2411         [Test]
2412         public void DynamicMethods () {
2413                 Event e = run_until ("dyn_call");
2414
2415                 var m = e.Thread.GetFrames ()[1].Method;
2416                 Assert.AreEqual ("dyn_method", m.Name);
2417
2418                 // Test access to IL
2419                 var body = m.GetMethodBody ();
2420
2421                 ILInstruction ins = body.Instructions [0];
2422                 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
2423                 Assert.AreEqual ("FOO", ins.Operand);
2424         }
2425
2426         [Test]
2427         public void RefEmit () {
2428                 vm.Dispose ();
2429
2430                 Start (new string [] { "dtest-app.exe", "ref-emit-test" });
2431
2432                 Event e = run_until ("ref_emit_call");
2433
2434                 var m = e.Thread.GetFrames ()[1].Method;
2435                 Assert.AreEqual ("ref_emit_method", m.Name);
2436
2437                 // Test access to IL
2438                 var body = m.GetMethodBody ();
2439
2440                 ILInstruction ins;
2441
2442                 ins = body.Instructions [0];
2443                 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
2444                 Assert.AreEqual ("FOO", ins.Operand);
2445
2446                 ins = body.Instructions [1];
2447                 Assert.AreEqual (OpCodes.Call, ins.OpCode);
2448                 Assert.IsInstanceOfType (typeof (MethodMirror), ins.Operand);
2449                 Assert.AreEqual ("ref_emit_call", (ins.Operand as MethodMirror).Name);
2450         }
2451
2452         [Test]
2453         public void IsAttached () {
2454                 var f = entry_point.DeclaringType.GetField ("is_attached");
2455
2456                 Event e = run_until ("Main");
2457
2458                 AssertValue (true, entry_point.DeclaringType.GetValue (f));
2459         }
2460
2461         [Test]
2462         public void StackTraceInNative () {
2463                 // Check that stack traces can be produced for threads in native code
2464                 vm.Dispose ();
2465
2466                 Start (new string [] { "dtest-app.exe", "frames-in-native" });
2467
2468                 var e = run_until ("frames_in_native");
2469
2470                 // FIXME: This is racy
2471                 vm.Resume ();
2472
2473                 Thread.Sleep (100);
2474
2475                 vm.Suspend ();
2476
2477                 StackFrame[] frames = e.Thread.GetFrames ();
2478
2479                 int frame_index = -1;
2480                 for (int i = 0; i < frames.Length; ++i) {
2481                         if (frames [i].Method.Name == "Sleep") {
2482                                 frame_index = i;
2483                                 break;
2484                         }
2485                 }
2486
2487                 Assert.IsTrue (frame_index != -1);
2488                 Assert.AreEqual ("Sleep", frames [frame_index].Method.Name);
2489                 Assert.AreEqual ("frames_in_native", frames [frame_index + 1].Method.Name);
2490                 Assert.AreEqual ("Main", frames [frame_index + 2].Method.Name);
2491
2492                 // Check that invokes are disabled for such threads
2493                 TypeMirror t = frames [frame_index + 1].Method.DeclaringType;
2494
2495                 // return void
2496                 var m = t.GetMethod ("invoke_static_return_void");
2497                 AssertThrows<InvalidOperationException> (delegate {
2498                                 t.InvokeMethod (e.Thread, m, null);
2499                         });
2500         }
2501
2502         [Test]
2503         public void VirtualMachine_CreateEnumMirror () {
2504                 var e = run_until ("o1");
2505                 var frame = e.Thread.GetFrames () [0];
2506
2507                 object val = frame.GetThis ();
2508                 Assert.IsTrue (val is ObjectMirror);
2509                 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
2510                 ObjectMirror o = (val as ObjectMirror);
2511
2512                 FieldInfoMirror field = o.Type.GetField ("field_enum");
2513                 Value f = o.GetValue (field);
2514                 TypeMirror enumType = (f as EnumMirror).Type;
2515
2516                 o.SetValue (field, vm.CreateEnumMirror (enumType, vm.CreateValue (1)));
2517                 f = o.GetValue (field);
2518                 Assert.AreEqual (1, (f as EnumMirror).Value);
2519
2520                 // Argument checking
2521                 AssertThrows<ArgumentNullException> (delegate () {
2522                                 vm.CreateEnumMirror (enumType, null);
2523                         });
2524
2525                 AssertThrows<ArgumentNullException> (delegate () {
2526                                 vm.CreateEnumMirror (null, vm.CreateValue (1));
2527                         });
2528
2529                 // null value
2530                 AssertThrows<ArgumentException> (delegate () {
2531                                 vm.CreateEnumMirror (enumType, vm.CreateValue (null));
2532                         });
2533
2534                 // value of a wrong type
2535                 AssertThrows<ArgumentException> (delegate () {
2536                                 vm.CreateEnumMirror (enumType, vm.CreateValue ((long)1));
2537                         });
2538         }
2539
2540         [Test]
2541         public void VirtualMachine_EnableEvents_Breakpoint () {
2542                 AssertThrows<ArgumentException> (delegate () {
2543                                 vm.EnableEvents (EventType.Breakpoint);
2544                         });
2545         }
2546
2547         [Test]
2548         public void SingleStepRegress654694 () {
2549                 int il_offset = -1;
2550
2551                 MethodMirror m = entry_point.DeclaringType.GetMethod ("ss_regress_654694");
2552                 foreach (Location l in m.Locations) {
2553                         if (l.ILOffset > 0 && il_offset == -1)
2554                                 il_offset = l.ILOffset;
2555                 }
2556
2557                 Event e = run_until ("ss_regress_654694");
2558
2559                 Assert.IsNotNull (m);
2560                 vm.SetBreakpoint (m, il_offset);
2561
2562                 vm.Resume ();
2563
2564                 e = vm.GetNextEvent ();
2565                 Assert.IsTrue (e is BreakpointEvent);
2566
2567                 var req = vm.CreateStepRequest (e.Thread);
2568                 req.Depth = StepDepth.Over;
2569                 req.Size = StepSize.Line;
2570                 req.Enable ();
2571
2572                 vm.Resume ();
2573
2574                 e = vm.GetNextEvent ();
2575                 Assert.IsTrue (e is StepEvent);
2576         }
2577
2578 }