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