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