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