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