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