[sgen] Write barrier nursery checks might be needed
[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                 // properties
1142                 t = frame.Method.GetParameters ()[7].ParameterType;
1143
1144                 var props = t.GetProperties ();
1145                 Assert.AreEqual (3, props.Length);
1146                 foreach (PropertyInfoMirror prop in props) {
1147                         ParameterInfoMirror[] indexes = prop.GetIndexParameters ();
1148
1149                         if (prop.Name == "IntProperty") {
1150                                 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1151                                 Assert.AreEqual ("get_IntProperty", prop.GetGetMethod ().Name);
1152                                 Assert.AreEqual ("set_IntProperty", prop.GetSetMethod ().Name);
1153                                 Assert.AreEqual (0, indexes.Length);
1154                         } else if (prop.Name == "ReadOnlyProperty") {
1155                                 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1156                                 Assert.AreEqual ("get_ReadOnlyProperty", prop.GetGetMethod ().Name);
1157                                 Assert.AreEqual (null, prop.GetSetMethod ());
1158                                 Assert.AreEqual (0, indexes.Length);
1159                         } else if (prop.Name == "IndexedProperty") {
1160                                 Assert.AreEqual (1, indexes.Length);
1161                                 Assert.AreEqual ("Int32", indexes [0].ParameterType.Name);
1162                         }
1163                 }
1164
1165                 // custom attributes
1166                 t = frame.Method.GetParameters ()[8].ParameterType;
1167                 Assert.AreEqual ("Tests2", t.Name);
1168                 var attrs = t.GetCustomAttributes (true);
1169                 Assert.AreEqual (5, attrs.Length);
1170                 foreach (var attr in attrs) {
1171                         if (attr.Constructor.DeclaringType.Name == "DebuggerDisplayAttribute") {
1172                                 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1173                                 Assert.AreEqual ("Tests", attr.ConstructorArguments [0].Value);
1174                                 Assert.AreEqual (2, attr.NamedArguments.Count);
1175                                 Assert.AreEqual ("Name", attr.NamedArguments [0].Property.Name);
1176                                 Assert.AreEqual ("FOO", attr.NamedArguments [0].TypedValue.Value);
1177                                 Assert.AreEqual ("Target", attr.NamedArguments [1].Property.Name);
1178                                 Assert.IsInstanceOfType (typeof (TypeMirror), attr.NamedArguments [1].TypedValue.Value);
1179                                 Assert.AreEqual ("Int32", (attr.NamedArguments [1].TypedValue.Value as TypeMirror).Name);
1180                         } else if (attr.Constructor.DeclaringType.Name == "DebuggerTypeProxyAttribute") {
1181                                 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1182                                 Assert.IsInstanceOfType (typeof (TypeMirror), attr.ConstructorArguments [0].Value);
1183                                 Assert.AreEqual ("Tests", (attr.ConstructorArguments [0].Value as TypeMirror).Name);
1184                         } else if (attr.Constructor.DeclaringType.Name == "BAttribute") {
1185                                 Assert.AreEqual (2, attr.NamedArguments.Count);
1186                                 Assert.AreEqual ("afield", attr.NamedArguments [0].Field.Name);
1187                                 Assert.AreEqual ("bfield", attr.NamedArguments [1].Field.Name);
1188                         } else if (attr.Constructor.DeclaringType.Name == "ClassInterfaceAttribute") {
1189                                 // inherited from System.Object
1190                                 //} else if (attr.Constructor.DeclaringType.Name == "Serializable") {
1191                                 // inherited from System.Object
1192                         } else if (attr.Constructor.DeclaringType.Name == "ComVisibleAttribute") {
1193                                 // inherited from System.Object
1194                         } else {
1195                                 Assert.Fail (attr.Constructor.DeclaringType.Name);
1196                         }
1197                 }
1198
1199                 var assembly = entry_point.DeclaringType.Assembly;
1200                 var type = assembly.GetType ("Tests4");
1201                 Assert.IsFalse (type.IsInitialized);
1202         }
1203
1204         [Test]
1205         public void FieldInfo () {
1206                 Event e = run_until ("ti2");
1207                 StackFrame frame = e.Thread.GetFrames () [0];
1208
1209                 TypeMirror t;
1210
1211                 t = frame.Method.GetParameters ()[8].ParameterType;
1212                 Assert.AreEqual ("Tests2", t.Name);
1213
1214                 var fi = t.GetField ("field_j");
1215                 var attrs = fi.GetCustomAttributes (true);
1216                 Assert.AreEqual (1, attrs.Length);
1217                 var attr = attrs [0];
1218                 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1219                 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1220                 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1221                 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1222         }
1223
1224         [Test]
1225         public void PropertyInfo () {
1226                 Event e = run_until ("ti2");
1227                 StackFrame frame = e.Thread.GetFrames () [0];
1228
1229                 TypeMirror t;
1230
1231                 t = frame.Method.GetParameters ()[8].ParameterType;
1232                 Assert.AreEqual ("Tests2", t.Name);
1233
1234                 var pi = t.GetProperty ("AProperty");
1235                 var attrs = pi.GetCustomAttributes (true);
1236                 Assert.AreEqual (1, attrs.Length);
1237                 var attr = attrs [0];
1238                 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1239                 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1240                 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1241                 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1242         }
1243
1244         [Test]
1245         [Category ("only5")]
1246         public void Type_GetValue () {
1247                 Event e = run_until ("o1");
1248                 StackFrame frame = e.Thread.GetFrames () [0];
1249
1250                 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1251
1252                 TypeMirror t = o.Type;
1253
1254                 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1255
1256                 // static fields
1257                 object f = t.GetValue (o.Type.GetField ("static_i"));
1258                 AssertValue (55, f);
1259
1260                 f = t.GetValue (o.Type.GetField ("static_s"));
1261                 AssertValue ("A", f);
1262
1263                 // literal static fields
1264                 f = t.GetValue (o.Type.GetField ("literal_i"));
1265                 AssertValue (56, f);
1266
1267                 f = t.GetValue (o.Type.GetField ("literal_s"));
1268                 AssertValue ("B", f);
1269
1270                 // Inherited static fields
1271                 TypeMirror parent = t.BaseType;
1272                 f = t.GetValue (parent.GetField ("base_static_i"));
1273                 AssertValue (57, f);
1274
1275                 f = t.GetValue (parent.GetField ("base_static_s"));
1276                 AssertValue ("C", f);
1277
1278                 // thread static field
1279                 f = t.GetValue (t.GetField ("tls_i"), e.Thread);
1280                 AssertValue (42, f);
1281
1282                 // Argument checking
1283                 AssertThrows<ArgumentNullException> (delegate () {
1284                         t.GetValue (null);
1285                         });
1286
1287                 // instance fields
1288                 AssertThrows<ArgumentException> (delegate () {
1289                         t.GetValue (o.Type.GetField ("field_i"));
1290                         });
1291
1292                 // field on another type
1293                 AssertThrows<ArgumentException> (delegate () {
1294                                 t.GetValue (val2.Type.GetField ("static_field_j"));
1295                         });
1296
1297                 // special static field
1298                 AssertThrows<ArgumentException> (delegate () {
1299                                 t.GetValue (t.GetField ("tls_i"));
1300                         });
1301         }
1302
1303         [Test]
1304         public void Type_GetValues () {
1305                 Event e = run_until ("o1");
1306                 StackFrame frame = e.Thread.GetFrames () [0];
1307
1308                 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1309
1310                 TypeMirror t = o.Type;
1311
1312                 // static fields
1313                 object[] vals = t.GetValues (new FieldInfoMirror [] { t.GetField ("static_i"), t.GetField ("static_s") });
1314                 object f = vals [0];
1315                 AssertValue (55, f);
1316
1317                 f = vals [1];
1318                 AssertValue ("A", f);
1319
1320                 // Argument checking
1321                 AssertThrows<ArgumentNullException> (delegate () {
1322                         t.GetValues (null);
1323                         });
1324
1325                 AssertThrows<ArgumentNullException> (delegate () {
1326                         t.GetValues (new FieldInfoMirror [] { null });
1327                         });
1328         }
1329
1330         [Test]
1331         public void ObjRefs () {
1332                 Event e = run_until ("objrefs1");
1333                 StackFrame frame = e.Thread.GetFrames () [0];
1334
1335                 ObjectMirror o = frame.GetThis () as ObjectMirror;
1336                 ObjectMirror child = o.GetValue (o.Type.GetField ("child")) as ObjectMirror;
1337
1338                 Assert.IsTrue (child.Address != 0);
1339
1340                 // Check that object references are internalized correctly
1341                 Assert.AreEqual (o, frame.GetThis ());
1342
1343                 run_until ("objrefs2");
1344
1345                 // child should be gc'd now
1346                 // This is not deterministic
1347                 //Assert.IsTrue (child.IsCollected);
1348
1349                 /*
1350                  * No longer works since Type is read eagerly
1351                  */
1352                 /*
1353                 AssertThrows<ObjectCollectedException> (delegate () {
1354                         TypeMirror t = child.Type;
1355                         });
1356                 */
1357                 /*
1358                 AssertThrows<ObjectCollectedException> (delegate () {
1359                                 long addr = child.Address;
1360                         });
1361                 */
1362         }
1363
1364         [Test]
1365         public void Type_GetObject () {
1366                 Event e = run_until ("o1");
1367                 StackFrame frame = e.Thread.GetFrames () [0];
1368
1369                 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1370
1371                 TypeMirror t = o.Type;
1372
1373                 Assert.AreEqual ("MonoType", t.GetTypeObject ().Type.Name);
1374         }
1375
1376         [Test]
1377         public void VTypes () {
1378                 Event e = run_until ("vtypes1");
1379                 StackFrame frame = e.Thread.GetFrames () [0];
1380
1381                 // vtypes as fields
1382                 ObjectMirror o = frame.GetThis () as ObjectMirror;
1383                 var obj = o.GetValue (o.Type.GetField ("field_struct"));
1384                 Assert.IsTrue (obj is StructMirror);
1385                 var s = obj as StructMirror;
1386                 Assert.AreEqual ("AStruct", s.Type.Name);
1387                 AssertValue (42, s ["i"]);
1388                 obj = s ["s"];
1389                 AssertValue ("S", obj);
1390                 AssertValue (43, s ["k"]);
1391                 obj = o.GetValue (o.Type.GetField ("field_boxed_struct"));
1392                 Assert.IsTrue (obj is StructMirror);
1393                 s = obj as StructMirror;
1394                 Assert.AreEqual ("AStruct", s.Type.Name);
1395                 AssertValue (42, s ["i"]);
1396
1397                 // Check decoding of nested structs (#14942)
1398                 obj = o.GetValue (o.Type.GetField ("nested_struct"));
1399                 o.SetValue (o.Type.GetField ("nested_struct"), obj);
1400
1401                 // Check round tripping of boxed struct fields (#12354)
1402                 obj = o.GetValue (o.Type.GetField ("boxed_struct_field"));
1403                 o.SetValue (o.Type.GetField ("boxed_struct_field"), obj);
1404                 obj = o.GetValue (o.Type.GetField ("boxed_struct_field"));
1405                 s = obj as StructMirror;
1406                 AssertValue (1, s ["key"]);
1407                 obj = s ["value"];
1408                 Assert.IsTrue (obj is StructMirror);
1409                 s = obj as StructMirror;
1410                 AssertValue (42, s ["m_value"]);
1411
1412                 // vtypes as arguments
1413                 s = frame.GetArgument (0) as StructMirror;
1414                 AssertValue (44, s ["i"]);
1415                 obj = s ["s"];
1416                 AssertValue ("T", obj);
1417                 AssertValue (45, s ["k"]);
1418
1419                 // vtypes as array entries
1420                 var arr = frame.GetArgument (1) as ArrayMirror;
1421                 obj = arr [0];
1422                 Assert.IsTrue (obj is StructMirror);
1423                 s = obj as StructMirror;
1424                 AssertValue (1, s ["i"]);
1425                 AssertValue ("S1", s ["s"]);
1426                 obj = arr [1];
1427                 Assert.IsTrue (obj is StructMirror);
1428                 s = obj as StructMirror;
1429                 AssertValue (2, s ["i"]);
1430                 AssertValue ("S2", s ["s"]);
1431
1432                 // Argument checking
1433                 s = frame.GetArgument (0) as StructMirror;
1434                 AssertThrows<ArgumentException> (delegate () {
1435                                 obj = s ["FOO"];
1436                         });
1437
1438                 // generic vtype instances
1439                 o = frame.GetThis () as ObjectMirror;
1440                 obj = o.GetValue (o.Type.GetField ("generic_field_struct"));
1441                 Assert.IsTrue (obj is StructMirror);
1442                 s = obj as StructMirror;
1443                 Assert.AreEqual ("GStruct`1", s.Type.Name);
1444                 AssertValue (42, s ["i"]);
1445
1446                 // this on vtype methods
1447                 e = run_until ("vtypes2");
1448                 e = step_until (e.Thread, "foo");
1449
1450                 frame = e.Thread.GetFrames () [0];
1451
1452                 Assert.AreEqual ("foo", (e as StepEvent).Method.Name);
1453                 obj = frame.GetThis ();
1454
1455                 Assert.IsTrue (obj is StructMirror);
1456                 s = obj as StructMirror;
1457                 AssertValue (44, s ["i"]);
1458                 AssertValue ("T", s ["s"]);
1459                 AssertValue (45, s ["k"]);
1460
1461                 // this on static vtype methods
1462                 e = run_until ("vtypes3");
1463                 e = step_until (e.Thread, "static_foo");
1464
1465                 frame = e.Thread.GetFrames () [0];
1466
1467                 Assert.AreEqual ("static_foo", (e as StepEvent).Method.Name);
1468                 obj = frame.GetThis ();
1469                 AssertValue (null, obj);
1470
1471                 // vtypes which reference themselves recursively
1472                 e = run_until ("vtypes4_2");
1473                 frame = e.Thread.GetFrames () [0];
1474
1475                 Assert.IsTrue (frame.GetArgument (0) is StructMirror);
1476         }
1477
1478         [Test]
1479         public void AssemblyInfo () {
1480                 Event e = run_until ("single_stepping");
1481
1482                 StackFrame frame = e.Thread.GetFrames () [0];
1483
1484                 var aname = frame.Method.DeclaringType.Assembly.GetName ();
1485                 Assert.AreEqual ("dtest-app, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", aname.ToString ());
1486
1487                 ModuleMirror m = frame.Method.DeclaringType.Module;
1488
1489                 Assert.AreEqual ("dtest-app.exe", m.Name);
1490                 Assert.AreEqual ("dtest-app.exe", m.ScopeName);
1491                 Assert.IsTrue (m.FullyQualifiedName.IndexOf ("dtest-app.exe") != -1);
1492                 Guid guid = m.ModuleVersionId;
1493                 Assert.AreEqual (frame.Method.DeclaringType.Assembly, m.Assembly);
1494                 Assert.AreEqual (frame.Method.DeclaringType.Assembly.ManifestModule, m);
1495
1496                 // This is no longer true on 4.0
1497                 //Assert.AreEqual ("Assembly", frame.Method.DeclaringType.Assembly.GetAssemblyObject ().Type.Name);
1498
1499                 TypeMirror t = vm.RootDomain.Corlib.GetType ("System.Diagnostics.DebuggerDisplayAttribute");
1500                 Assert.AreEqual ("DebuggerDisplayAttribute", t.Name);
1501         }
1502
1503         [Test]
1504         public void LocalsInfo () {
1505                 Event e = run_until ("locals2");
1506
1507                 StackFrame frame = e.Thread.GetFrames () [0];
1508
1509                 var locals = frame.Method.GetLocals ();
1510                 Assert.AreEqual (8, locals.Length);
1511                 for (int i = 0; i < 8; ++i) {
1512                         if (locals [i].Name == "args") {
1513                                 Assert.IsTrue (locals [i].IsArg);
1514                                 Assert.AreEqual ("String[]", locals [i].Type.Name);
1515                         } else if (locals [i].Name == "arg") {
1516                                 Assert.IsTrue (locals [i].IsArg);
1517                                 Assert.AreEqual ("Int32", locals [i].Type.Name);
1518                         } else if (locals [i].Name == "i") {
1519                                 Assert.IsFalse (locals [i].IsArg);
1520                                 Assert.AreEqual ("Int64", locals [i].Type.Name);
1521                         } else if (locals [i].Name == "j") {
1522                                 Assert.IsFalse (locals [i].IsArg);
1523                                 Assert.AreEqual ("Int32", locals [i].Type.Name);
1524                         } else if (locals [i].Name == "s") {
1525                                 Assert.IsFalse (locals [i].IsArg);
1526                                 Assert.AreEqual ("String", locals [i].Type.Name);
1527                         } else if (locals [i].Name == "t") {
1528                                 // gshared
1529                                 Assert.IsTrue (locals [i].IsArg);
1530                                 Assert.AreEqual ("String", locals [i].Type.Name);
1531                         } else if (locals [i].Name == "rs") {
1532                                 Assert.IsTrue (locals [i].IsArg);
1533                                 Assert.AreEqual ("String", locals [i].Type.Name);
1534                         } else if (locals [i].Name == "astruct") {
1535                         } else {
1536                                 Assert.Fail ();
1537                         }
1538                 }
1539         }
1540
1541         Event step_once () {
1542                 vm.Resume ();
1543                 var e = GetNextEvent ();
1544                 Assert.IsTrue (e is StepEvent);
1545                 return e;
1546         }
1547
1548         Event step_into () {
1549                 step_req.Disable ();
1550                 step_req.Depth = StepDepth.Into;
1551                 step_req.Enable ();
1552                 return step_once ();
1553         }
1554
1555         Event step_over () {
1556                 step_req.Disable ();
1557                 step_req.Depth = StepDepth.Over;
1558                 step_req.Enable ();
1559                 return step_once ();
1560         }
1561
1562         Event step_out () {
1563                 step_req.Disable ();
1564                 step_req.Depth = StepDepth.Out;
1565                 step_req.Enable ();
1566                 return step_once ();
1567         }
1568
1569         [Test]
1570         public void Locals () {
1571                 var be = run_until ("locals1");
1572
1573                 StackFrame frame = be.Thread.GetFrames () [0];
1574                 MethodMirror m1 = frame.Method;
1575
1576                 // Compiler generated byref local
1577                 foreach (var l in m1.GetLocals ()) {
1578                         // The byval flag is hidden from the type
1579                         if (l.Name != "ri" && l.Type.Name == "Double")
1580                                 AssertValue (null, frame.GetValue (l));
1581                 }
1582
1583                 be = run_until ("locals2");
1584
1585                 frame = be.Thread.GetFrames () [0];
1586
1587                 object val = frame.GetValue (frame.Method.GetLocal ("i"));
1588                 AssertValue (0, val);
1589
1590                 var req = create_step (be);
1591                 req.Enable ();
1592
1593                 // Skip nop
1594                 step_once ();
1595
1596                 // Execute i = 42
1597                 var e = step_once ();
1598                 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1599
1600                 // Execute s = "AB";
1601                 e = step_once ();
1602                 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1603
1604                 frame = e.Thread.GetFrames () [0];
1605
1606                 val = frame.GetValue (frame.Method.GetLocal ("i"));
1607                 AssertValue (42, val);
1608
1609                 LocalVariable[] locals = frame.Method.GetLocals ();
1610                 var vals = frame.GetValues (locals);
1611                 Assert.AreEqual (locals.Length, vals.Length);
1612                 for (int i = 0; i < locals.Length; ++i) {
1613                         if (locals [i].Name == "i")
1614                                 AssertValue (42, vals [i]);
1615                         if (locals [i].Name == "s")
1616                                 AssertValue ("AB", vals [i]);
1617                         if (locals [i].Name == "t")
1618                                 AssertValue ("ABC", vals [i]);
1619                 }
1620
1621                 // Argument checking
1622
1623                 // GetValue () null
1624                 AssertThrows<ArgumentNullException> (delegate () {
1625                                 frame.GetValue ((LocalVariable)null);
1626                         });
1627                 // GetValue () local from another method
1628                 AssertThrows<ArgumentException> (delegate () {
1629                                 frame.GetValue (m1.GetLocal ("foo"));
1630                         });
1631
1632                 // GetValue () null
1633                 AssertThrows<ArgumentNullException> (delegate () {
1634                                 frame.GetValue ((ParameterInfoMirror)null);
1635                         });
1636                 // GetValue () local from another method
1637                 AssertThrows<ArgumentException> (delegate () {
1638                                 frame.GetValue (m1.GetParameters ()[0]);
1639                         });
1640
1641                 // GetValues () null
1642                 AssertThrows<ArgumentNullException> (delegate () {
1643                                 frame.GetValues (null);
1644                         });
1645                 // GetValues () embedded null
1646                 AssertThrows<ArgumentNullException> (delegate () {
1647                                 frame.GetValues (new LocalVariable [] { null });
1648                         });
1649                 // GetValues () local from another method
1650                 AssertThrows<ArgumentException> (delegate () {
1651                                 frame.GetValues (new LocalVariable [] { m1.GetLocal ("foo") });
1652                         });
1653                 // return value
1654                 AssertThrows<ArgumentException> (delegate () {
1655                                 val = frame.GetValue (frame.Method.ReturnParameter);
1656                         });
1657
1658                 // invalid stack frames
1659                 vm.Resume ();
1660                 e = GetNextEvent ();
1661                 Assert.IsTrue (e is StepEvent);
1662                 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1663
1664                 AssertThrows<InvalidStackFrameException> (delegate () {
1665                                 frame.GetValue (frame.Method.GetLocal ("i"));
1666                         });
1667
1668                 req.Disable ();
1669
1670                 // gsharedvt
1671                 be = run_until ("locals7");
1672
1673                 req = create_step (be);
1674                 req.Enable ();
1675
1676                 // Skip nop
1677                 e = step_once ();
1678
1679                 // Test that locals are initialized
1680                 frame = e.Thread.GetFrames () [0];
1681                 val = frame.GetValue (frame.Method.GetLocal ("t"));
1682                 AssertValue (0, val);
1683
1684                 // Execute t = arg
1685                 e = step_once ();
1686                 Assert.AreEqual ("locals7", (e as StepEvent).Method.Name);
1687
1688                 // Execute t2 = t
1689                 e = step_once ();
1690                 Assert.AreEqual ("locals7", (e as StepEvent).Method.Name);
1691
1692                 frame = e.Thread.GetFrames () [0];
1693                 val = frame.GetValue (frame.Method.GetParameters ()[0]);
1694                 AssertValue (22, val);
1695                 val = frame.GetValue (frame.Method.GetLocal ("t"));
1696                 AssertValue (22, val);
1697                 val = frame.GetValue (frame.Method.GetLocal ("t2"));
1698                 AssertValue (22, val);
1699         }
1700
1701         [Test]
1702         public void GetVisibleVariables () {
1703                 Event e = run_until ("locals4");
1704
1705                 // First scope
1706                 var locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
1707                 Assert.AreEqual (2, locals.Count);
1708                 var loc = locals.First (l => l.Name == "i");
1709                 Assert.AreEqual ("Int64", loc.Type.Name);
1710                 loc = locals.First (l => l.Name == "s");
1711                 Assert.AreEqual ("String", loc.Type.Name);
1712
1713                 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
1714                 Assert.AreEqual ("i", loc.Name);
1715                 Assert.AreEqual ("Int64", loc.Type.Name);
1716
1717                 e = run_until ("locals5");
1718
1719                 // Second scope
1720                 locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
1721                 Assert.AreEqual (2, locals.Count);
1722                 loc = locals.First (l => l.Name == "i");
1723                 Assert.AreEqual ("String", loc.Type.Name);
1724                 loc = locals.First (l => l.Name == "s");
1725                 Assert.AreEqual ("String", loc.Type.Name);
1726
1727                 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
1728                 Assert.AreEqual ("i", loc.Name);
1729                 Assert.AreEqual ("String", loc.Type.Name);
1730
1731                 // Variable in another scope
1732                 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("j");
1733                 Assert.IsNull (loc);
1734         }
1735
1736         [Test]
1737         public void Exit () {
1738                 run_until ("Main");
1739
1740                 vm.Exit (5);
1741
1742                 var e = GetNextEvent ();
1743                 Assert.IsInstanceOfType (typeof (VMDeathEvent), e);
1744
1745                 Assert.AreEqual (5, (e as VMDeathEvent).ExitCode);
1746
1747                 var p = vm.Process;
1748                 /* Could be a remote vm with no process */
1749                 if (p != null) {
1750                         p.WaitForExit ();
1751                         Assert.AreEqual (5, p.ExitCode);
1752
1753                         // error handling
1754                         AssertThrows<VMDisconnectedException> (delegate () {
1755                                         vm.Resume ();
1756                                 });
1757                 }
1758
1759                 vm = null;
1760         }
1761
1762         [Test]
1763         public void Dispose () {
1764                 run_until ("Main");
1765
1766                 vm.Detach ();
1767
1768                 var e = GetNextEvent ();
1769                 Assert.IsInstanceOfType (typeof (VMDisconnectEvent), e);
1770
1771                 var p = vm.Process;
1772                 /* Could be a remote vm with no process */
1773                 if (p != null) {
1774                         p.WaitForExit ();
1775                         Assert.AreEqual (3, p.ExitCode);
1776
1777                         // error handling
1778                         AssertThrows<VMDisconnectedException> (delegate () {
1779                                         vm.Resume ();
1780                                 });
1781                 }
1782
1783                 vm = null;
1784         }
1785
1786         [Test]
1787         public void ColumnNumbers () {
1788                 Event e = run_until ("line_numbers");
1789
1790                 // FIXME: Merge this with LineNumbers () when its fixed
1791
1792                 step_req = create_step (e);
1793                 step_req.Depth = StepDepth.Into;
1794                 step_req.Enable ();
1795
1796                 Location l;
1797                 
1798                 while (true) {
1799                         vm.Resume ();
1800
1801                         e = GetNextEvent ();
1802                         Assert.IsTrue (e is StepEvent);
1803                         if (e.Thread.GetFrames ()[0].Method.Name == "ln1")
1804                                 break;
1805                 }
1806
1807                 // Do an additional step over so we are not on the beginning line of the method
1808                 step_req.Disable ();
1809                 step_req.Depth = StepDepth.Over;
1810                 step_req.Enable ();
1811                 vm.Resume ();
1812                 e = GetNextEvent ();
1813                 Assert.IsTrue (e is StepEvent);         
1814
1815                 l = e.Thread.GetFrames ()[0].Location;
1816
1817                 Assert.AreEqual (3, l.ColumnNumber);
1818
1819                 step_req.Disable ();
1820         }
1821
1822         [Test]
1823         // Broken by mcs+runtime changes (#5438)
1824         [Category("NotWorking")]
1825         public void LineNumbers () {
1826                 Event e = run_until ("line_numbers");
1827
1828                 step_req = create_step (e);
1829                 step_req.Depth = StepDepth.Into;
1830                 step_req.Enable ();
1831
1832                 Location l;
1833                 
1834                 vm.Resume ();
1835
1836                 e = GetNextEvent ();
1837                 Assert.IsTrue (e is StepEvent);
1838
1839                 l = e.Thread.GetFrames ()[0].Location;
1840
1841                 Assert.IsTrue (l.SourceFile.IndexOf ("dtest-app.cs") != -1);
1842                 Assert.AreEqual ("ln1", l.Method.Name);
1843
1844                 // Check hash
1845                 using (FileStream fs = new FileStream (l.SourceFile, FileMode.Open, FileAccess.Read)) {
1846                         MD5 md5 = MD5.Create ();
1847                         var hash = md5.ComputeHash (fs);
1848
1849                         for (int i = 0; i < 16; ++i)
1850                                 Assert.AreEqual (hash [i], l.SourceFileHash [i]);
1851                 }
1852                 
1853                 int line_base = l.LineNumber;
1854
1855                 vm.Resume ();
1856                 e = GetNextEvent ();
1857                 Assert.IsTrue (e is StepEvent);
1858                 l = e.Thread.GetFrames ()[0].Location;
1859                 Assert.AreEqual ("ln2", l.Method.Name);
1860                 Assert.AreEqual (line_base + 6, l.LineNumber);
1861
1862                 vm.Resume ();
1863                 e = GetNextEvent ();
1864                 Assert.IsTrue (e is StepEvent);
1865                 l = e.Thread.GetFrames ()[0].Location;
1866                 Assert.AreEqual ("ln1", l.Method.Name);
1867                 Assert.AreEqual (line_base + 1, l.LineNumber);
1868
1869                 vm.Resume ();
1870                 e = GetNextEvent ();
1871                 Assert.IsTrue (e is StepEvent);
1872                 l = e.Thread.GetFrames ()[0].Location;
1873                 Assert.AreEqual ("ln3", l.Method.Name);
1874                 Assert.AreEqual (line_base + 11, l.LineNumber);
1875
1876                 vm.Resume ();
1877                 e = GetNextEvent ();
1878                 Assert.IsTrue (e is StepEvent);
1879                 l = e.Thread.GetFrames ()[0].Location;
1880                 Assert.AreEqual ("ln3", l.Method.Name);
1881                 Assert.IsTrue (l.SourceFile.EndsWith ("FOO"));
1882                 Assert.AreEqual (55, l.LineNumber);
1883
1884                 vm.Resume ();
1885                 e = GetNextEvent ();
1886                 Assert.IsTrue (e is StepEvent);
1887                 l = e.Thread.GetFrames ()[0].Location;
1888                 Assert.AreEqual ("ln1", l.Method.Name);
1889                 Assert.AreEqual (line_base + 2, l.LineNumber);
1890
1891                 // GetSourceFiles ()
1892                 string[] sources = l.Method.DeclaringType.GetSourceFiles ();
1893                 Assert.AreEqual (2, sources.Length);
1894                 Assert.AreEqual ("dtest-app.cs", sources [0]);
1895                 Assert.AreEqual ("FOO", sources [1]);
1896
1897                 sources = l.Method.DeclaringType.GetSourceFiles (true);
1898                 Assert.AreEqual (2, sources.Length);
1899                 Assert.IsTrue (sources [0].EndsWith ("dtest-app.cs"));
1900                 Assert.IsTrue (sources [1].EndsWith ("FOO"));
1901         }
1902
1903         [Test]
1904         public void Suspend () {
1905                 vm.Detach ();
1906
1907                 Start (new string [] { "dtest-app.exe", "suspend-test" });
1908
1909                 Event e = run_until ("suspend");
1910
1911                 ThreadMirror main = e.Thread;
1912
1913                 vm.Resume ();
1914
1915                 Thread.Sleep (100);
1916
1917                 vm.Suspend ();
1918
1919                 // The debuggee should be suspended while it is running the infinite loop
1920                 // in suspend ()
1921                 StackFrame frame = main.GetFrames ()[0];
1922                 Assert.AreEqual ("suspend", frame.Method.Name);
1923
1924                 vm.Resume ();
1925
1926                 // resuming when not suspended
1927                 AssertThrows<InvalidOperationException> (delegate () {
1928                                 vm.Resume ();
1929                         });
1930
1931                 vm.Exit (0);
1932
1933                 vm = null;
1934         }
1935
1936         [Test]
1937         public void AssemblyLoad () {
1938                 Event e = run_until ("assembly_load");
1939
1940                 var load_req = vm.CreateAssemblyLoadRequest ();
1941                 load_req.Enable ();
1942
1943                 vm.Resume ();
1944
1945                 e = GetNextEvent ();
1946                 Assert.IsInstanceOfType (typeof (AssemblyLoadEvent), e);
1947                 Assert.IsTrue ((e as AssemblyLoadEvent).Assembly.Location.EndsWith ("System.dll"));
1948
1949                 var frames = e.Thread.GetFrames ();
1950                 Assert.IsTrue (frames.Length > 0);
1951                 Assert.AreEqual ("assembly_load", frames [0].Method.Name);
1952         }
1953
1954         [Test]
1955         public void CreateValue () {
1956                 PrimitiveValue v;
1957
1958                 v = vm.CreateValue (1);
1959                 Assert.AreEqual (vm, v.VirtualMachine);
1960                 Assert.AreEqual (1, v.Value);
1961
1962                 v = vm.CreateValue (null);
1963                 Assert.AreEqual (vm, v.VirtualMachine);
1964                 Assert.AreEqual (null, v.Value);
1965
1966                 // Argument checking
1967                 AssertThrows <ArgumentException> (delegate () {
1968                                 v = vm.CreateValue ("FOO");
1969                         });
1970         }
1971
1972         [Test]
1973         public void CreateString () {
1974                 StringMirror s = vm.RootDomain.CreateString ("ABC");
1975
1976                 Assert.AreEqual (vm, s.VirtualMachine);
1977                 Assert.AreEqual ("ABC", s.Value);
1978                 Assert.AreEqual (vm.RootDomain, s.Domain);
1979
1980                 // Long strings
1981                 StringBuilder sb = new StringBuilder ();
1982                 for (int i = 0; i < 1024; ++i)
1983                         sb.Append ('A');
1984                 s = vm.RootDomain.CreateString (sb.ToString ());
1985
1986                 // Argument checking
1987                 AssertThrows <ArgumentNullException> (delegate () {
1988                                 s = vm.RootDomain.CreateString (null);
1989                         });
1990         }
1991
1992         [Test]
1993         public void CreateBoxedValue () {
1994                 ObjectMirror o = vm.RootDomain.CreateBoxedValue (new PrimitiveValue (vm, 42));
1995
1996                 Assert.AreEqual ("Int32", o.Type.Name);
1997                 //AssertValue (42, m.GetValue (o.Type.GetField ("m_value")));
1998
1999                 // Argument checking
2000                 AssertThrows <ArgumentNullException> (delegate () {
2001                                 vm.RootDomain.CreateBoxedValue (null);
2002                         });
2003
2004                 AssertThrows <ArgumentException> (delegate () {
2005                                 vm.RootDomain.CreateBoxedValue (o);
2006                         });
2007         }
2008
2009         [Test]
2010         public void Invoke () {
2011                 Event e = run_until ("invoke1");
2012
2013                 StackFrame frame = e.Thread.GetFrames () [0];
2014
2015                 TypeMirror t = frame.Method.DeclaringType;
2016                 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2017
2018                 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2019
2020                 MethodMirror m;
2021                 Value v;
2022
2023                 // return void
2024                 m = t.GetMethod ("invoke_return_void");
2025                 v = this_obj.InvokeMethod (e.Thread, m, null);
2026                 Assert.IsNull (v);
2027
2028                 // return ref
2029                 m = t.GetMethod ("invoke_return_ref");
2030                 v = this_obj.InvokeMethod (e.Thread, m, null);
2031                 AssertValue ("ABC", v);
2032
2033                 // return null
2034                 m = t.GetMethod ("invoke_return_null");
2035                 v = this_obj.InvokeMethod (e.Thread, m, null);
2036                 AssertValue (null, v);
2037
2038                 // return primitive
2039                 m = t.GetMethod ("invoke_return_primitive");
2040                 v = this_obj.InvokeMethod (e.Thread, m, null);
2041                 AssertValue (42, v);
2042
2043                 // return nullable
2044                 m = t.GetMethod ("invoke_return_nullable");
2045                 v = this_obj.InvokeMethod (e.Thread, m, null);
2046                 Assert.IsInstanceOfType (typeof (StructMirror), v);
2047                 var s = v as StructMirror;
2048                 AssertValue (42, s.Fields [0]);
2049                 AssertValue (true, s.Fields [1]);
2050
2051                 // pass nullable as this
2052                 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2053                 m = s.Type.GetMethod ("ToString");
2054                 v = s.InvokeMethod (e.Thread, m, null);
2055
2056                 // return nullable null
2057                 m = t.GetMethod ("invoke_return_nullable_null");
2058                 v = this_obj.InvokeMethod (e.Thread, m, null);
2059                 Assert.IsInstanceOfType (typeof (StructMirror), v);
2060                 s = v as StructMirror;
2061                 AssertValue (0, s.Fields [0]);
2062                 AssertValue (false, s.Fields [1]);
2063
2064                 // pass nullable as this
2065                 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2066                 m = s.Type.GetMethod ("ToString");
2067                 v = s.InvokeMethod (e.Thread, m, null);
2068
2069                 // pass primitive
2070                 m = t.GetMethod ("invoke_pass_primitive");
2071                 Value[] args = new Value [] {
2072                         vm.CreateValue ((byte)Byte.MaxValue),
2073                         vm.CreateValue ((sbyte)SByte.MaxValue),
2074                         vm.CreateValue ((short)1),
2075                         vm.CreateValue ((ushort)1),
2076                         vm.CreateValue ((int)1),
2077                         vm.CreateValue ((uint)1),
2078                         vm.CreateValue ((long)1),
2079                         vm.CreateValue ((ulong)1),
2080                         vm.CreateValue ('A'),
2081                         vm.CreateValue (true),
2082                         vm.CreateValue (3.14f),
2083                         vm.CreateValue (3.14) };
2084
2085                 v = this_obj.InvokeMethod (e.Thread, m, args);
2086                 AssertValue ((int)Byte.MaxValue + (int)SByte.MaxValue + 1 + 1 + 1 + 1 + 1 + 1 + 'A' + 1 + 3 + 3, v);
2087
2088                 // pass ref
2089                 m = t.GetMethod ("invoke_pass_ref");
2090                 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2091                 AssertValue ("ABC", v);
2092
2093                 // pass null
2094                 m = t.GetMethod ("invoke_pass_ref");
2095                 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (null) });
2096                 AssertValue (null, v);
2097
2098                 // static
2099                 m = t.GetMethod ("invoke_static_pass_ref");
2100                 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2101                 AssertValue ("ABC", v);
2102
2103                 // static invoked using ObjectMirror.InvokeMethod
2104                 m = t.GetMethod ("invoke_static_pass_ref");
2105                 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2106                 AssertValue ("ABC", v);
2107
2108                 // method which throws an exception
2109                 try {
2110                         m = t.GetMethod ("invoke_throws");
2111                         v = this_obj.InvokeMethod (e.Thread, m, null);
2112                         Assert.Fail ();
2113                 } catch (InvocationException ex) {
2114                         Assert.AreEqual ("Exception", ex.Exception.Type.Name);
2115                 }
2116
2117 #if NET_4_5
2118                 // out argument
2119                 m = t.GetMethod ("invoke_out");
2120                 var out_task = this_obj.InvokeMethodAsyncWithResult (e.Thread, m, new Value [] { vm.CreateValue (1), vm.CreateValue (null) }, InvokeOptions.ReturnOutArgs);
2121                 var out_args = out_task.Result.OutArgs;
2122                 AssertValue (5, out_args [0]);
2123                 Assert.IsTrue (out_args [1] is ArrayMirror);
2124                 Assert.AreEqual (10, (out_args [1] as ArrayMirror).Length);
2125
2126                 // without ReturnOutArgs flag
2127                 out_task = this_obj.InvokeMethodAsyncWithResult (e.Thread, m, new Value [] { vm.CreateValue (1), vm.CreateValue (null) });
2128                 out_args = out_task.Result.OutArgs;
2129                 Assert.IsNull (out_args);
2130 #endif
2131
2132                 // newobj
2133                 m = t.GetMethod (".ctor");
2134                 v = t.InvokeMethod (e.Thread, m, null);
2135                 Assert.IsInstanceOfType (typeof (ObjectMirror), v);
2136                 Assert.AreEqual ("Tests", (v as ObjectMirror).Type.Name);
2137
2138                 // interface method
2139                 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("ITest2");
2140                 m = cl1.GetMethod ("invoke_iface");
2141                 v = this_obj.InvokeMethod (e.Thread, m, null);
2142                 AssertValue (42, v);
2143
2144                 // virtual call
2145                 m = t.BaseType.GetMethod ("virtual_method");
2146                 v = this_obj.InvokeMethod (e.Thread, m, null, InvokeOptions.Virtual);
2147                 AssertValue ("V2", v);
2148
2149                 // virtual call on static method
2150                 m = t.GetMethod ("invoke_static_pass_ref");
2151                 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") }, InvokeOptions.Virtual);
2152                 AssertValue ("ABC", v);
2153
2154 #if NET_4_5
2155                 // instance
2156                 m = t.GetMethod ("invoke_pass_ref");
2157                 var task = this_obj.InvokeMethodAsync (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2158                 AssertValue ("ABC", task.Result);
2159
2160                 // static
2161                 m = t.GetMethod ("invoke_static_pass_ref");
2162                 task = t.InvokeMethodAsync (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2163                 AssertValue ("ABC", task.Result);
2164 #endif
2165
2166                 // Argument checking
2167                 
2168                 // null thread
2169                 AssertThrows<ArgumentNullException> (delegate {
2170                                 m = t.GetMethod ("invoke_pass_ref");
2171                                 v = this_obj.InvokeMethod (null, m, new Value [] { vm.CreateValue (null) });                            
2172                         });
2173
2174                 // null method
2175                 AssertThrows<ArgumentNullException> (delegate {
2176                                 v = this_obj.InvokeMethod (e.Thread, null, new Value [] { vm.CreateValue (null) });                             
2177                         });
2178
2179                 // invalid number of arguments
2180                 m = t.GetMethod ("invoke_pass_ref");
2181                 AssertThrows<ArgumentException> (delegate {
2182                                 v = this_obj.InvokeMethod (e.Thread, m, null);
2183                         });
2184
2185                 // invalid type of argument (ref != primitive)
2186                 m = t.GetMethod ("invoke_pass_ref");
2187                 AssertThrows<ArgumentException> (delegate {
2188                                 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2189                         });
2190
2191                 // invalid type of argument (primitive != primitive)
2192                 m = t.GetMethod ("invoke_pass_primitive_2");
2193                 AssertThrows<ArgumentException> (delegate {
2194                                 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2195                         });
2196
2197                 // invoking a non-static method as static
2198                 m = t.GetMethod ("invoke_pass_ref");
2199                 AssertThrows<ArgumentException> (delegate {
2200                                 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2201                         });
2202
2203                 // invoking a method defined in another class
2204                 m = t2.GetMethod ("invoke");
2205                 AssertThrows<ArgumentException> (delegate {
2206                                 v = this_obj.InvokeMethod (e.Thread, m, null);
2207                         });
2208         }
2209
2210         [Test]
2211         public void InvokeVType () {
2212                 Event e = run_until ("invoke1");
2213
2214                 StackFrame frame = e.Thread.GetFrames () [0];
2215
2216                 var s = frame.GetArgument (1) as StructMirror;
2217
2218                 TypeMirror t = s.Type;
2219
2220                 MethodMirror m;
2221                 Value v;
2222
2223                 // Pass struct as this, receive int
2224                 m = t.GetMethod ("invoke_return_int");
2225                 v = s.InvokeMethod (e.Thread, m, null);
2226                 AssertValue (42, v);
2227
2228                 // Pass struct as this, receive intptr
2229                 m = t.GetMethod ("invoke_return_intptr");
2230                 v = s.InvokeMethod (e.Thread, m, null);
2231                 AssertValue (43, v);
2232
2233                 // Static method
2234                 m = t.GetMethod ("invoke_static");
2235                 v = t.InvokeMethod (e.Thread, m, null);
2236                 AssertValue (5, v);
2237
2238                 // Pass generic struct as this
2239                 s = frame.GetArgument (2) as StructMirror;
2240                 t = s.Type;
2241                 m = t.GetMethod ("invoke_return_int");
2242                 v = s.InvokeMethod (e.Thread, m, null);
2243                 AssertValue (42, v);
2244
2245 #if NET_4_5
2246                 // Invoke a method which changes state
2247                 s = frame.GetArgument (1) as StructMirror;
2248                 t = s.Type;
2249                 m = t.GetMethod ("invoke_mutate");
2250                 var task = s.InvokeMethodAsyncWithResult (e.Thread, m, null, InvokeOptions.ReturnOutThis);
2251                 var out_this = task.Result.OutThis as StructMirror;
2252                 AssertValue (5, out_this ["l"]);
2253
2254                 // Without the ReturnOutThis flag
2255                 s = frame.GetArgument (1) as StructMirror;
2256                 t = s.Type;
2257                 m = t.GetMethod ("invoke_mutate");
2258                 task = s.InvokeMethodAsyncWithResult (e.Thread, m, null);
2259                 out_this = task.Result.OutThis as StructMirror;
2260                 Assert.AreEqual (null, out_this);
2261 #endif
2262         }
2263
2264         [Test]
2265         public void BreakpointDuringInvoke () {
2266                 Event e = run_until ("invoke1");
2267
2268                 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke2");
2269                 Assert.IsNotNull (m);
2270                 vm.SetBreakpoint (m, 0);
2271
2272                 StackFrame frame = e.Thread.GetFrames () [0];
2273                 var o = frame.GetThis () as ObjectMirror;
2274
2275                 bool failed = false;
2276
2277                 bool finished = false;
2278                 object wait = new object ();
2279
2280                 // Have to invoke in a separate thread as the invoke is suspended until we
2281                 // resume after the breakpoint
2282                 Thread t = new Thread (delegate () {
2283                                 try {
2284                                         o.InvokeMethod (e.Thread, m, null);
2285                                 } catch {
2286                                         failed = true;
2287                                 }
2288                                 lock (wait) {
2289                                         finished = true;
2290                                         Monitor.Pulse (wait);
2291                                 }
2292                         });
2293
2294                 t.Start ();
2295
2296                 StackFrame invoke_frame = null;
2297
2298                 try {
2299                         e = GetNextEvent ();
2300                         Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
2301                         // Check stack trace support and invokes
2302                         var frames = e.Thread.GetFrames ();
2303                         invoke_frame = frames [0];
2304                         Assert.AreEqual ("invoke2", frames [0].Method.Name);
2305                         Assert.IsTrue (frames [0].IsDebuggerInvoke);
2306                         Assert.AreEqual ("invoke1", frames [1].Method.Name);
2307                 } finally {
2308                         vm.Resume ();
2309                 }
2310
2311                 lock (wait) {
2312                         if (!finished)
2313                                 Monitor.Wait (wait);
2314                 }
2315
2316                 // Check that the invoke frames are no longer valid
2317                 AssertThrows<InvalidStackFrameException> (delegate {
2318                                 invoke_frame.GetThis ();
2319                         });
2320
2321                 // Check InvokeOptions.DisableBreakpoints flag
2322                 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2323         }
2324
2325         [Test]
2326         public void DisabledExceptionDuringInvoke () {
2327                 Event e = run_until ("invoke_ex");
2328
2329                 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_ex_inner");
2330
2331                 StackFrame frame = e.Thread.GetFrames () [0];
2332                 var o = frame.GetThis () as ObjectMirror;
2333
2334                 var req = vm.CreateExceptionRequest (null);
2335                 req.Enable ();
2336
2337                 // Check InvokeOptions.DisableBreakpoints flag
2338                 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2339
2340                 req.Disable ();
2341         }
2342
2343         [Test]
2344         public void InvokeSingleThreaded () {
2345                 vm.Detach ();
2346
2347                 Start (new string [] { "dtest-app.exe", "invoke-single-threaded" });
2348
2349                 Event e = run_until ("invoke_single_threaded_2");
2350
2351                 StackFrame f = e.Thread.GetFrames ()[0];
2352
2353                 var obj = f.GetThis () as ObjectMirror;
2354
2355                 // Check that the counter value incremented by the other thread does not increase
2356                 // during the invoke.
2357                 object counter1 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2358
2359                 var m = obj.Type.GetMethod ("invoke_return_void");
2360                 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2361
2362             object counter2 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2363
2364                 Assert.AreEqual ((int)counter1, (int)counter2);
2365
2366                 // Test multiple invokes done in succession
2367                 m = obj.Type.GetMethod ("invoke_return_void");
2368                 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2369
2370                 // Test events during single-threaded invokes
2371                 vm.EnableEvents (EventType.TypeLoad);
2372                 m = obj.Type.GetMethod ("invoke_type_load");
2373                 obj.BeginInvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded, delegate {
2374                                 vm.Resume ();
2375                         }, null);
2376
2377                 e = GetNextEvent ();
2378                 Assert.AreEqual (EventType.TypeLoad, e.EventType);
2379         }
2380
2381         List<Value> invoke_results;
2382
2383         [Test]
2384         public void InvokeMultiple () {
2385                 Event e = run_until ("invoke1");
2386
2387                 StackFrame frame = e.Thread.GetFrames () [0];
2388
2389                 TypeMirror t = frame.Method.DeclaringType;
2390                 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2391
2392                 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2393
2394                 var methods = new MethodMirror [2];
2395                 methods [0] = t.GetMethod ("invoke_return_ref");
2396                 methods [1] = t.GetMethod ("invoke_return_primitive");
2397
2398                 invoke_results = new List<Value> ();
2399
2400                 var r = this_obj.BeginInvokeMultiple (e.Thread, methods, null, InvokeOptions.SingleThreaded, invoke_multiple_cb, this_obj);
2401                 WaitHandle.WaitAll (new WaitHandle[] { r.AsyncWaitHandle });
2402                 this_obj.EndInvokeMultiple (r);
2403                 // The callback might still be running
2404                 while (invoke_results.Count < 2) {
2405                         Thread.Sleep (100);
2406                 }
2407                 if (invoke_results [0] is PrimitiveValue) {
2408                         AssertValue ("ABC", invoke_results [1]);
2409                         AssertValue (42, invoke_results [0]);
2410                 } else {
2411                         AssertValue ("ABC", invoke_results [0]);
2412                         AssertValue (42, invoke_results [1]);
2413                 }
2414         }
2415
2416         void invoke_multiple_cb (IAsyncResult ar) {
2417                 ObjectMirror this_obj = (ObjectMirror)ar.AsyncState;
2418
2419                 var res = this_obj.EndInvokeMethod (ar);
2420                 lock (invoke_results)
2421                         invoke_results.Add (res);
2422         }
2423
2424         [Test]
2425         public void GetThreads () {
2426                 vm.GetThreads ();
2427         }
2428
2429         [Test]
2430         public void Threads () {
2431                 Event e = run_until ("threads");
2432
2433                 Assert.AreEqual (ThreadState.Running, e.Thread.ThreadState);
2434
2435                 Assert.IsTrue (e.Thread.ThreadId > 0);
2436
2437                 Assert.AreEqual (e.Thread.TID, e.Thread.TID);
2438
2439                 vm.EnableEvents (EventType.ThreadStart, EventType.ThreadDeath);
2440
2441                 vm.Resume ();
2442
2443                 e = GetNextEvent ();
2444                 Assert.IsInstanceOfType (typeof (ThreadStartEvent), e);
2445                 var state = e.Thread.ThreadState;
2446                 Assert.IsTrue (state == ThreadState.Running || state == ThreadState.Unstarted);
2447
2448                 vm.Resume ();
2449
2450                 e = GetNextEvent ();
2451                 Assert.IsInstanceOfType (typeof (ThreadDeathEvent), e);
2452                 Assert.AreEqual (ThreadState.Stopped, e.Thread.ThreadState);
2453         }
2454
2455         [Test]
2456         public void Frame_SetValue () {
2457                 Event e = run_until ("locals2");
2458
2459                 StackFrame frame = e.Thread.GetFrames () [0];
2460
2461                 // primitive
2462                 var l = frame.Method.GetLocal ("i");
2463                 frame.SetValue (l, vm.CreateValue ((long)55));
2464                 AssertValue (55, frame.GetValue (l));
2465
2466                 // reference
2467                 l = frame.Method.GetLocal ("s");
2468                 frame.SetValue (l, vm.RootDomain.CreateString ("DEF"));
2469                 AssertValue ("DEF", frame.GetValue (l));
2470
2471                 // argument as local
2472                 l = frame.Method.GetLocal ("arg");
2473                 frame.SetValue (l, vm.CreateValue (6));
2474                 AssertValue (6, frame.GetValue (l));
2475
2476                 // argument
2477                 var p = frame.Method.GetParameters ()[1];
2478                 frame.SetValue (p, vm.CreateValue (7));
2479                 AssertValue (7, frame.GetValue (p));
2480
2481                 // gshared
2482                 p = frame.Method.GetParameters ()[2];
2483                 frame.SetValue (p, vm.RootDomain.CreateString ("DEF"));
2484                 AssertValue ("DEF", frame.GetValue (p));
2485
2486                 // byref
2487                 p = frame.Method.GetParameters ()[3];
2488                 frame.SetValue (p, vm.RootDomain.CreateString ("DEF2"));
2489                 AssertValue ("DEF2", frame.GetValue (p));
2490
2491                 // byref struct
2492                 p = frame.Method.GetParameters ()[4];
2493                 var v = frame.GetValue (p) as StructMirror;
2494                 v ["i"] = vm.CreateValue (43);
2495                 frame.SetValue (p, v);
2496                 v = frame.GetValue (p) as StructMirror;
2497                 AssertValue (43, v ["i"]);
2498
2499                 // argument checking
2500
2501                 // variable null
2502                 AssertThrows<ArgumentNullException> (delegate () {
2503                                 frame.SetValue ((LocalVariable)null, vm.CreateValue (55));
2504                         });
2505
2506                 // value null
2507                 AssertThrows<ArgumentNullException> (delegate () {
2508                                 l = frame.Method.GetLocal ("i");
2509                                 frame.SetValue (l, null);
2510                         });
2511
2512                 // value of invalid type
2513                 AssertThrows<ArgumentException> (delegate () {
2514                                 l = frame.Method.GetLocal ("i");
2515                                 frame.SetValue (l, vm.CreateValue (55));
2516                         });
2517         }
2518
2519         [Test]
2520         [Category ("only")]
2521         public void Frame_SetValue_Registers () {
2522                 Event e = run_until ("locals6_1");
2523
2524                 StackFrame frame = e.Thread.GetFrames () [1];
2525
2526                 // Set 'j' to 99
2527                 var l = frame.Method.GetLocal ("j");
2528                 frame.SetValue (l, vm.CreateValue (99));
2529                 AssertValue (99, frame.GetValue (l));
2530
2531                 // Check it during execution
2532                 e = run_until ("locals6_2");
2533                 frame = e.Thread.GetFrames () [0];
2534                 AssertValue (99, frame.GetValue (frame.Method.GetParameters ()[0]));
2535
2536                 // Set it while in a frame which clobbers its register
2537                 e = run_until ("locals6_3");
2538                 frame = e.Thread.GetFrames () [1];
2539                 frame.SetValue (l, vm.CreateValue (100));
2540                 AssertValue (100, frame.GetValue (l));
2541
2542                 // Check it during execution
2543                 e = run_until ("locals6_4");
2544                 frame = e.Thread.GetFrames () [0];
2545                 AssertValue (100, frame.GetValue (frame.Method.GetParameters ()[0]));
2546
2547                 // Signed byte value
2548                 e = run_until ("locals6_5");
2549                 frame = e.Thread.GetFrames () [1];
2550                 var l2 = frame.Method.GetLocal ("sb");
2551                 frame.SetValue (l2, vm.CreateValue ((sbyte)-99));
2552                 AssertValue (-99, frame.GetValue (l2));
2553
2554                 // Check it during execution
2555                 e = run_until ("locals6_6");
2556                 frame = e.Thread.GetFrames () [0];
2557                 AssertValue (-99, frame.GetValue (frame.Method.GetParameters ()[0]));
2558         }
2559
2560         [Test]
2561         public void InvokeRegress () {
2562                 Event e = run_until ("invoke1");
2563
2564                 StackFrame frame = e.Thread.GetFrames () [0];
2565
2566                 TypeMirror t = frame.Method.DeclaringType;
2567                 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2568
2569                 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2570
2571                 MethodMirror m;
2572                 Value v;
2573
2574                 // do an invoke
2575                 m = t.GetMethod ("invoke_return_void");
2576                 v = this_obj.InvokeMethod (e.Thread, m, null);
2577                 Assert.IsNull (v);
2578
2579                 // Check that the stack frames remain valid during the invoke
2580                 Assert.AreEqual ("Tests", (frame.GetThis () as ObjectMirror).Type.Name);
2581
2582                 // do another invoke
2583                 m = t.GetMethod ("invoke_return_void");
2584                 v = this_obj.InvokeMethod (e.Thread, m, null);
2585                 Assert.IsNull (v);
2586
2587                 // Try a single step after the invoke
2588                 var req = create_step (e);
2589                 req.Depth = StepDepth.Into;
2590                 req.Size = StepSize.Line;
2591                 req.Enable ();
2592
2593                 // Skip nop
2594                 step_once ();
2595
2596                 // Step into invoke2
2597                 vm.Resume ();
2598                 e = GetNextEvent ();
2599                 Assert.IsTrue (e is StepEvent);
2600                 Assert.AreEqual ("invoke2", (e as StepEvent).Method.Name);
2601
2602                 req.Disable ();
2603
2604                 frame = e.Thread.GetFrames () [0];
2605         }
2606
2607         [Test]
2608         public void Exceptions () {
2609                 Event e = run_until ("exceptions");
2610                 var req = vm.CreateExceptionRequest (null);
2611                 req.Enable ();
2612
2613                 vm.Resume ();
2614
2615                 e = GetNextEvent ();
2616                 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2617                 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2618
2619                 var frames = e.Thread.GetFrames ();
2620                 Assert.AreEqual ("exceptions", frames [0].Method.Name);
2621                 req.Disable ();
2622
2623                 // exception type filter
2624
2625                 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.ArgumentException"));
2626                 req.Enable ();
2627
2628                 // Skip the throwing of the second OverflowException       
2629                 vm.Resume ();
2630
2631                 e = GetNextEvent ();
2632                 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2633                 Assert.AreEqual ("ArgumentException", (e as ExceptionEvent).Exception.Type.Name);
2634                 req.Disable ();
2635
2636                 // exception type filter for subclasses
2637                 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.Exception"));
2638                 req.Enable ();
2639
2640                 vm.Resume ();
2641
2642                 e = GetNextEvent ();
2643                 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2644                 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2645                 req.Disable ();
2646
2647                 // no subclasses
2648                 req.IncludeSubclasses = false;
2649                 req.Enable ();
2650
2651                 vm.Resume ();
2652
2653                 e = GetNextEvent ();
2654                 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2655                 Assert.AreEqual ("Exception", (e as ExceptionEvent).Exception.Type.Name);
2656                 req.Disable ();
2657
2658                 // Implicit exceptions
2659                 req = vm.CreateExceptionRequest (null);
2660                 req.Enable ();
2661
2662                 vm.Resume ();
2663
2664                 e = GetNextEvent ();
2665                 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2666                 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
2667                 req.Disable ();
2668
2669                 // Single stepping after an exception
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 ("Exception", (e as ExceptionEvent).Exception.Type.Name);
2678                 frames = e.Thread.GetFrames ();
2679                 Assert.AreEqual ("exceptions2", frames [0].Method.Name);
2680                 req.Disable ();
2681
2682                 var sreq = create_step (e);
2683                 sreq.Depth = StepDepth.Over;
2684                 sreq.Size = StepSize.Line;
2685                 sreq.Enable ();
2686
2687                 vm.Resume ();
2688                 e = GetNextEvent ();
2689                 Assert.IsInstanceOfType (typeof (StepEvent), e);
2690                 frames = e.Thread.GetFrames ();
2691                 Assert.AreEqual ("exceptions", frames [0].Method.Name);
2692                 sreq.Disable ();
2693
2694                 // Argument checking
2695                 AssertThrows<ArgumentException> (delegate {
2696                                 vm.CreateExceptionRequest (e.Thread.Type);
2697                         });
2698         }
2699
2700         [Test]
2701         public void ExceptionFilter () {
2702                 Event e = run_until ("exception_filter");
2703
2704                 MethodMirror m = entry_point.DeclaringType.GetMethod ("exception_filter_filter");
2705                 Assert.IsNotNull (m);
2706
2707                 vm.SetBreakpoint (m, 0);
2708
2709                 vm.Resume ();
2710
2711                 e = GetNextEvent ();
2712                 Assert.AreEqual (EventType.Breakpoint, e.EventType);
2713                 Assert.IsTrue (e is BreakpointEvent);
2714                 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
2715
2716                 var frames = e.Thread.GetFrames ();
2717
2718                 Assert.IsTrue (frames [0].Location.SourceFile.IndexOf ("dtest-app.cs") != -1);
2719                 Assert.AreEqual ("exception_filter_filter", frames [0].Location.Method.Name);
2720
2721                 Assert.AreEqual (0, frames [1].Location.Method.MetadataToken);
2722                 Assert.AreEqual (0x0f, frames [1].Location.ILOffset);
2723
2724                 Assert.AreEqual ("exception_filter_method", frames [2].Location.Method.Name);
2725                 Assert.AreEqual (0x06, frames [2].Location.ILOffset);
2726
2727                 Assert.AreEqual (0, frames [3].Location.Method.MetadataToken, 0);
2728                 Assert.AreEqual (0, frames [3].Location.ILOffset);
2729
2730                 Assert.AreEqual ("exception_filter", frames [4].Location.Method.Name);
2731         }
2732
2733         [Test]
2734         public void ExceptionFilter2 () {
2735                 vm.Detach ();
2736
2737                 Start (new string [] { "dtest-excfilter.exe" });
2738
2739                 MethodMirror filter_method = entry_point.DeclaringType.GetMethod ("Filter");
2740                 Assert.IsNotNull (filter_method);
2741
2742                 MethodMirror test_method = entry_point.DeclaringType.GetMethod ("Test");
2743                 Assert.IsNotNull (test_method);
2744
2745                 vm.SetBreakpoint (filter_method, 0);
2746
2747                 vm.Resume ();
2748
2749                 var e = GetNextEvent ();
2750                 Assert.AreEqual (EventType.Breakpoint, e.EventType);
2751                 Assert.IsTrue (e is BreakpointEvent);
2752                 Assert.AreEqual (filter_method.Name, (e as BreakpointEvent).Method.Name);
2753
2754                 var frames = e.Thread.GetFrames ();
2755
2756                 Assert.AreEqual (4, frames.Count ());
2757
2758                 Assert.AreEqual (filter_method.Name, frames [0].Location.Method.Name);
2759                 Assert.AreEqual (20, frames [0].Location.LineNumber);
2760                 Assert.AreEqual (0, frames [0].Location.ILOffset);
2761
2762                 Assert.AreEqual (test_method.Name, frames [1].Location.Method.Name);
2763                 Assert.AreEqual (37, frames [1].Location.LineNumber);
2764                 Assert.AreEqual (0x0b, frames [1].Location.ILOffset);
2765
2766                 Assert.AreEqual (test_method.Name, frames [2].Location.Method.Name);
2767                 Assert.AreEqual (33, frames [2].Location.LineNumber);
2768                 Assert.AreEqual (0x05, frames [2].Location.ILOffset);
2769
2770                 Assert.AreEqual (entry_point.Name, frames [3].Location.Method.Name);
2771                 Assert.AreEqual (14, frames [3].Location.LineNumber);
2772                 Assert.AreEqual (0x00, frames [3].Location.ILOffset);
2773
2774                 vm.Exit (0);
2775
2776                 vm = null;
2777         }
2778
2779         [Test]
2780         public void EventSets () {
2781                 //
2782                 // Create two filter which both match the same exception
2783                 //
2784                 Event e = run_until ("exceptions");
2785
2786                 var req = vm.CreateExceptionRequest (null);
2787                 req.Enable ();
2788
2789                 var req2 = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.OverflowException"));
2790                 req2.Enable ();
2791
2792                 vm.Resume ();
2793
2794                 var es = vm.GetNextEventSet ();
2795                 Assert.AreEqual (2, es.Events.Length);
2796
2797                 e = es [0];
2798                 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2799                 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2800
2801                 e = es [1];
2802                 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2803                 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2804
2805                 req.Disable ();
2806                 req2.Disable ();
2807         }
2808
2809         //
2810         // Test single threaded invokes during processing of nullref exceptions.
2811         // These won't work if the exception handling is done from the sigsegv signal
2812         // handler, since the sigsegv signal is disabled until control returns from the
2813         // signal handler.
2814         //
2815         [Test]
2816         [Category ("only3")]
2817         public void NullRefExceptionAndSingleThreadedInvoke () {
2818                 Event e = run_until ("exceptions");
2819                 var req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.NullReferenceException"));
2820                 req.Enable ();
2821
2822                 vm.Resume ();
2823
2824                 e = GetNextEvent ();
2825                 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2826                 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
2827
2828                 var ex = (e as ExceptionEvent).Exception;
2829                 var tostring_method = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2830                 ex.InvokeMethod (e.Thread, tostring_method, null, InvokeOptions.SingleThreaded);
2831         }
2832
2833         [Test]
2834         public void Domains () {
2835                 vm.Detach ();
2836
2837                 Start (new string [] { "dtest-app.exe", "domain-test" });
2838
2839                 vm.EnableEvents (EventType.AppDomainCreate, EventType.AppDomainUnload, EventType.AssemblyUnload);
2840
2841                 Event e = run_until ("domains");
2842
2843                 vm.Resume ();
2844                 
2845                 e = GetNextEvent ();
2846                 Assert.IsInstanceOfType (typeof (AppDomainCreateEvent), e);
2847
2848                 var domain = (e as AppDomainCreateEvent).Domain;
2849
2850                 // Check the object type
2851                 e = run_until ("domains_2");
2852                 var frame = e.Thread.GetFrames ()[0];
2853                 var o = frame.GetArgument (0) as ObjectMirror;
2854                 Assert.AreEqual ("CrossDomain", o.Type.Name);
2855
2856                 // Do a remoting invoke
2857                 var cross_domain_type = o.Type;
2858                 var v = o.InvokeMethod (e.Thread, cross_domain_type.GetMethod ("invoke_3"), null);
2859                 AssertValue (42, v);
2860
2861                 // Run until the callback in the domain
2862                 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_in_domain");
2863                 Assert.IsNotNull (m);
2864                 vm.SetBreakpoint (m, 0);
2865
2866                 while (true) {
2867                         vm.Resume ();
2868                         e = GetNextEvent ();
2869                         if (e is BreakpointEvent)
2870                                 break;
2871                 }
2872
2873                 Assert.AreEqual ("invoke_in_domain", (e as BreakpointEvent).Method.Name);
2874
2875                 // d_method is from another domain
2876                 MethodMirror d_method = (e as BreakpointEvent).Method;
2877                 Assert.IsTrue (m != d_method);
2878
2879                 var frames = e.Thread.GetFrames ();
2880                 Assert.AreEqual ("invoke_in_domain", frames [0].Method.Name);
2881                 Assert.AreEqual (domain, frames [0].Domain);
2882                 Assert.AreEqual ("invoke", frames [1].Method.Name);
2883                 Assert.AreEqual ("domains", frames [2].Method.Name);
2884                 Assert.AreEqual (vm.RootDomain, frames [2].Domain);
2885
2886                 // Test breakpoints on already JITted methods in other domains
2887                 m = entry_point.DeclaringType.GetMethod ("invoke_in_domain_2");
2888                 Assert.IsNotNull (m);
2889                 vm.SetBreakpoint (m, 0);
2890
2891                 while (true) {
2892                         vm.Resume ();
2893                         e = GetNextEvent ();
2894                         if (e is BreakpointEvent)
2895                                 break;
2896                 }
2897
2898                 Assert.AreEqual ("invoke_in_domain_2", (e as BreakpointEvent).Method.Name);
2899
2900                 // This is empty when receiving the AppDomainCreateEvent
2901                 Assert.AreEqual ("domain", domain.FriendlyName);
2902
2903                 // Run until the unload
2904                 while (true) {
2905                         vm.Resume ();
2906                         e = GetNextEvent ();
2907                         if (e is AssemblyUnloadEvent) {
2908                                 continue;
2909                         } else {
2910                                 break;
2911                         }
2912                 }
2913                 Assert.IsInstanceOfType (typeof (AppDomainUnloadEvent), e);
2914                 Assert.AreEqual (domain, (e as AppDomainUnloadEvent).Domain);
2915
2916                 // Run past the unload
2917                 e = run_until ("domains_3");
2918
2919                 // Test access to unloaded types
2920                 // FIXME: Add an exception type for this
2921                 AssertThrows<Exception> (delegate {
2922                                 d_method.DeclaringType.GetValue (d_method.DeclaringType.GetField ("static_i"));
2923                         });
2924
2925                 // Check that .Domain is accessible for stack frames with native transitions
2926                 e = run_until ("called_from_invoke");
2927                 ThreadMirror.NativeTransitions = true;
2928                 foreach (var f in e.Thread.GetFrames ()) {
2929                         var dom = f.Domain;
2930                 }
2931         }
2932
2933         [Test]
2934         public void DynamicMethods () {
2935                 Event e = run_until ("dyn_call");
2936
2937                 var m = e.Thread.GetFrames ()[1].Method;
2938                 Assert.AreEqual ("dyn_method", m.Name);
2939
2940                 // Test access to IL
2941                 var body = m.GetMethodBody ();
2942
2943                 ILInstruction ins = body.Instructions [0];
2944                 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
2945                 Assert.AreEqual ("FOO", ins.Operand);
2946         }
2947
2948         [Test]
2949         public void RefEmit () {
2950                 vm.Detach ();
2951
2952                 Start (new string [] { "dtest-app.exe", "ref-emit-test" });
2953
2954                 Event e = run_until ("ref_emit_call");
2955
2956                 var m = e.Thread.GetFrames ()[1].Method;
2957                 Assert.AreEqual ("ref_emit_method", m.Name);
2958
2959                 // Test access to IL
2960                 var body = m.GetMethodBody ();
2961
2962                 ILInstruction ins;
2963
2964                 ins = body.Instructions [0];
2965                 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
2966                 Assert.AreEqual ("FOO", ins.Operand);
2967
2968                 ins = body.Instructions [1];
2969                 Assert.AreEqual (OpCodes.Call, ins.OpCode);
2970                 Assert.IsInstanceOfType (typeof (MethodMirror), ins.Operand);
2971                 Assert.AreEqual ("ref_emit_call", (ins.Operand as MethodMirror).Name);
2972         }
2973
2974         [Test]
2975         public void IsAttached () {
2976                 var f = entry_point.DeclaringType.GetField ("is_attached");
2977
2978                 Event e = run_until ("Main");
2979
2980                 AssertValue (true, entry_point.DeclaringType.GetValue (f));
2981         }
2982
2983         [Test]
2984         public void StackTraceInNative () {
2985                 // Check that stack traces can be produced for threads in native code
2986                 vm.Detach ();
2987
2988                 Start (new string [] { "dtest-app.exe", "frames-in-native" });
2989
2990                 var e = run_until ("frames_in_native");
2991
2992                 // FIXME: This is racy
2993                 vm.Resume ();
2994
2995                 Thread.Sleep (100);
2996
2997                 vm.Suspend ();
2998
2999                 StackFrame[] frames = e.Thread.GetFrames ();
3000
3001                 int frame_index = -1;
3002                 for (int i = 0; i < frames.Length; ++i) {
3003                         if (frames [i].Method.Name == "Sleep") {
3004                                 frame_index = i;
3005                                 break;
3006                         }
3007                 }
3008
3009                 Assert.IsTrue (frame_index != -1);
3010                 Assert.AreEqual ("Sleep", frames [frame_index].Method.Name);
3011                 Assert.AreEqual ("frames_in_native", frames [frame_index + 1].Method.Name);
3012                 Assert.AreEqual ("Main", frames [frame_index + 2].Method.Name);
3013
3014                 // Check that invokes are disabled for such threads
3015                 TypeMirror t = frames [frame_index + 1].Method.DeclaringType;
3016
3017                 var m = t.GetMethod ("invoke_static_return_void");
3018                 AssertThrows<InvalidOperationException> (delegate {
3019                                 t.InvokeMethod (e.Thread, m, null);
3020                         });
3021
3022                 // Check that the frame info is invalidated
3023                 run_until ("frames_in_native_2");
3024
3025                 AssertThrows<InvalidStackFrameException> (delegate {
3026                                 Console.WriteLine (frames [frame_index].GetThis ());
3027                         });
3028         }
3029
3030         [Test]
3031         public void VirtualMachine_CreateEnumMirror () {
3032                 var e = run_until ("o1");
3033                 var frame = e.Thread.GetFrames () [0];
3034
3035                 object val = frame.GetThis ();
3036                 Assert.IsTrue (val is ObjectMirror);
3037                 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
3038                 ObjectMirror o = (val as ObjectMirror);
3039
3040                 FieldInfoMirror field = o.Type.GetField ("field_enum");
3041                 Value f = o.GetValue (field);
3042                 TypeMirror enumType = (f as EnumMirror).Type;
3043
3044                 o.SetValue (field, vm.CreateEnumMirror (enumType, vm.CreateValue (1)));
3045                 f = o.GetValue (field);
3046                 Assert.AreEqual (1, (f as EnumMirror).Value);
3047
3048                 // Argument checking
3049                 AssertThrows<ArgumentNullException> (delegate () {
3050                                 vm.CreateEnumMirror (enumType, null);
3051                         });
3052
3053                 AssertThrows<ArgumentNullException> (delegate () {
3054                                 vm.CreateEnumMirror (null, vm.CreateValue (1));
3055                         });
3056
3057                 // null value
3058                 AssertThrows<ArgumentException> (delegate () {
3059                                 vm.CreateEnumMirror (enumType, vm.CreateValue (null));
3060                         });
3061
3062                 // value of a wrong type
3063                 AssertThrows<ArgumentException> (delegate () {
3064                                 vm.CreateEnumMirror (enumType, vm.CreateValue ((long)1));
3065                         });
3066         }
3067
3068         [Test]
3069         public void VirtualMachine_EnableEvents_Breakpoint () {
3070                 AssertThrows<ArgumentException> (delegate () {
3071                                 vm.EnableEvents (EventType.Breakpoint);
3072                         });
3073         }
3074
3075         [Test]
3076         public void SingleStepRegress654694 () {
3077                 int il_offset = -1;
3078
3079                 MethodMirror m = entry_point.DeclaringType.GetMethod ("ss_regress_654694");
3080                 foreach (Location l in m.Locations) {
3081                         if (l.ILOffset > 0 && il_offset == -1)
3082                                 il_offset = l.ILOffset;
3083                 }
3084
3085                 Event e = run_until ("ss_regress_654694");
3086
3087                 Assert.IsNotNull (m);
3088                 vm.SetBreakpoint (m, il_offset);
3089
3090                 vm.Resume ();
3091
3092                 e = GetNextEvent ();
3093                 Assert.IsTrue (e is BreakpointEvent);
3094
3095                 var req = create_step (e);
3096                 req.Depth = StepDepth.Over;
3097                 req.Size = StepSize.Line;
3098                 req.Enable ();
3099
3100                 vm.Resume ();
3101
3102                 e = GetNextEvent ();
3103                 Assert.IsTrue (e is StepEvent);
3104
3105                 req.Disable ();
3106         }
3107
3108         [Test]
3109         public void DebugBreak () {
3110                 vm.EnableEvents (EventType.UserBreak);
3111
3112                 run_until ("user");
3113
3114                 vm.Resume ();
3115                 var e = GetNextEvent ();
3116                 Assert.IsTrue (e is UserBreakEvent);
3117         }
3118
3119         [Test]
3120         public void DebugLog () {
3121                 vm.EnableEvents (EventType.UserLog);
3122
3123                 run_until ("user");
3124
3125                 vm.Resume ();
3126                 var e = GetNextEvent ();
3127                 Assert.IsTrue (e is UserLogEvent);
3128                 var le = e as UserLogEvent;
3129
3130                 Assert.AreEqual (5, le.Level);
3131                 Assert.AreEqual ("A", le.Category);
3132                 Assert.AreEqual ("B", le.Message);
3133         }
3134
3135         [Test]
3136         public void TypeGetMethodsByNameFlags () {
3137                 MethodMirror[] mm;
3138                 var assembly = entry_point.DeclaringType.Assembly;
3139                 var type = assembly.GetType ("Tests3");
3140
3141                 Assert.IsNotNull (type);
3142
3143                 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Public, false);
3144                 Assert.AreEqual (1, mm.Length, "#1");
3145                 Assert.AreEqual ("M1", mm[0].Name, "#2");
3146
3147                 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.NonPublic, false);
3148                 Assert.AreEqual (1, mm.Length, "#3");
3149                 Assert.AreEqual ("M2", mm[0].Name, "#4");
3150
3151                 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public, false);
3152                 Assert.AreEqual (7, mm.Length, "#5"); //M3 plus Equals, GetHashCode, GetType, ToString, .ctor
3153
3154                 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, false);
3155                 Assert.AreEqual (2, mm.Length, "#7");
3156
3157                 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
3158                 Assert.AreEqual (1, mm.Length, "#9");
3159
3160                 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
3161                 Assert.AreEqual (5, mm.Length, "#11");
3162
3163                 //Now with name
3164                 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Public, false);
3165                 Assert.AreEqual (1, mm.Length, "#12");
3166                 Assert.AreEqual ("M1", mm[0].Name, "#13");
3167
3168                 mm = type.GetMethodsByNameFlags ("m1", BindingFlags.Static | BindingFlags.Public, true);
3169                 Assert.AreEqual (1, mm.Length, "#14");
3170                 Assert.AreEqual ("M1", mm[0].Name, "#15");
3171
3172                 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static  | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, false);
3173                 Assert.AreEqual (1, mm.Length, "#16");
3174                 Assert.AreEqual ("M1", mm[0].Name, "#17");
3175         }
3176
3177         [Test]
3178         [Category ("only88")]
3179         public void TypeLoadSourceFileFilter () {
3180                 Event e = run_until ("type_load");
3181
3182                 if (!vm.Version.AtLeast (2, 7))
3183                         return;
3184
3185                 string srcfile = (e as BreakpointEvent).Method.DeclaringType.GetSourceFiles (true)[0];
3186
3187                 var req = vm.CreateTypeLoadRequest ();
3188                 req.SourceFileFilter = new string [] { srcfile.ToUpper () };
3189                 req.Enable ();
3190
3191                 vm.Resume ();
3192                 e = GetNextEvent ();
3193                 Assert.IsTrue (e is TypeLoadEvent);
3194                 Assert.AreEqual ("TypeLoadClass", (e as TypeLoadEvent).Type.FullName);
3195         }
3196
3197         [Test]
3198         public void TypeLoadTypeNameFilter () {
3199                 Event e = run_until ("type_load");
3200
3201                 var req = vm.CreateTypeLoadRequest ();
3202                 req.TypeNameFilter = new string [] { "TypeLoadClass2" };
3203                 req.Enable ();
3204
3205                 vm.Resume ();
3206                 e = GetNextEvent ();
3207                 Assert.IsTrue (e is TypeLoadEvent);
3208                 Assert.AreEqual ("TypeLoadClass2", (e as TypeLoadEvent).Type.FullName);
3209         }
3210
3211         [Test]
3212         public void GetTypesForSourceFile () {
3213                 run_until ("user");
3214
3215                 var types = vm.GetTypesForSourceFile ("dtest-app.cs", false);
3216                 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
3217                 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
3218
3219                 types = vm.GetTypesForSourceFile ("DTEST-app.cs", true);
3220                 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
3221                 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
3222         }
3223
3224         [Test]
3225         public void GetTypesNamed () {
3226                 run_until ("user");
3227
3228                 var types = vm.GetTypes ("Tests", false);
3229                 Assert.AreEqual (1, types.Count);
3230                 Assert.AreEqual ("Tests", types [0].FullName);
3231
3232                 types = vm.GetTypes ("System.Exception", false);
3233                 Assert.AreEqual (1, types.Count);
3234                 Assert.AreEqual ("System.Exception", types [0].FullName);
3235         }
3236
3237         [Test]
3238         public void String_GetChars () {
3239                 object val;
3240
3241                 // Reuse this test
3242                 var e = run_until ("arg2");
3243
3244                 var frame = e.Thread.GetFrames () [0];
3245
3246                 val = frame.GetArgument (0);
3247                 Assert.IsTrue (val is StringMirror);
3248                 AssertValue ("FOO", val);
3249                 var s = (val as StringMirror);
3250                 Assert.AreEqual (3, s.Length);
3251
3252                 var c = s.GetChars (0, 2);
3253                 Assert.AreEqual (2, c.Length);
3254                 Assert.AreEqual ('F', c [0]);
3255                 Assert.AreEqual ('O', c [1]);
3256
3257                 AssertThrows<ArgumentException> (delegate () {          
3258                                 s.GetChars (2, 2);
3259                         });
3260         }
3261
3262         [Test]
3263         public void GetInterfaces () {
3264                 var e = run_until ("arg2");
3265
3266                 var frame = e.Thread.GetFrames () [0];
3267
3268                 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
3269                 var ifaces = cl1.GetInterfaces ();
3270                 Assert.AreEqual (1, ifaces.Length);
3271                 Assert.AreEqual ("ITest", ifaces [0].Name);
3272
3273                 var cl2 = cl1.GetMethod ("Baz").ReturnType;
3274                 var ifaces2 = cl2.GetInterfaces ();
3275                 Assert.AreEqual (1, ifaces2.Length);
3276                 Assert.AreEqual ("ITest`1", ifaces2 [0].Name);
3277         }
3278
3279         [Test]
3280         public void GetInterfaceMap () {
3281                 var e = run_until ("arg2");
3282
3283                 var frame = e.Thread.GetFrames () [0];
3284
3285                 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
3286                 var iface = cl1.Assembly.GetType ("ITest");
3287                 var map = cl1.GetInterfaceMap (iface);
3288                 Assert.AreEqual (cl1, map.TargetType);
3289                 Assert.AreEqual (iface, map.InterfaceType);
3290                 Assert.AreEqual (2, map.InterfaceMethods.Length);
3291                 Assert.AreEqual (2, map.TargetMethods.Length);
3292         }
3293
3294         [Test]
3295         public void StackAlloc_Breakpoints_Regress2775 () {
3296                 // Check that breakpoints on arm don't overwrite stackalloc-ed memory
3297                 var e = run_until ("regress_2755");
3298
3299                 var frame = e.Thread.GetFrames () [0];
3300                 var m = e.Method;
3301                 // This breaks at the call site
3302                 vm.SetBreakpoint (m, m.Locations [2].ILOffset);
3303
3304                 vm.Resume ();
3305                 var e2 = GetNextEvent ();
3306                 Assert.IsTrue (e2 is BreakpointEvent);
3307
3308                 e = run_until ("regress_2755_3");
3309                 frame = e.Thread.GetFrames () [1];
3310                 var res = frame.GetValue (m.GetLocal ("sum"));
3311                 AssertValue (0, res);
3312         }
3313
3314         [Test]
3315         public void MethodInfo () {
3316                 Event e = run_until ("locals2");
3317
3318                 StackFrame frame = e.Thread.GetFrames () [0];
3319                 var m = frame.Method;
3320
3321                 Assert.IsTrue (m.IsGenericMethod);
3322                 Assert.IsFalse (m.IsGenericMethodDefinition);
3323
3324                 var args = m.GetGenericArguments ();
3325                 Assert.AreEqual (1, args.Length);
3326                 Assert.AreEqual ("String", args [0].Name);
3327
3328                 var gmd = m.GetGenericMethodDefinition ();
3329                 Assert.IsTrue (gmd.IsGenericMethod);
3330                 Assert.IsTrue (gmd.IsGenericMethodDefinition);
3331                 Assert.AreEqual (gmd, gmd.GetGenericMethodDefinition ());
3332
3333                 args = gmd.GetGenericArguments ();
3334                 Assert.AreEqual (1, args.Length);
3335                 Assert.AreEqual ("T", args [0].Name);
3336
3337                 var attrs = m.GetCustomAttributes (true);
3338                 Assert.AreEqual (1, attrs.Length);
3339                 Assert.AreEqual ("StateMachineAttribute", attrs [0].Constructor.DeclaringType.Name);
3340         }
3341
3342         [Test]
3343         public void UnhandledException () {
3344                 vm.Exit (0);
3345
3346                 Start (new string [] { "dtest-app.exe", "unhandled-exception" });
3347
3348                 var req = vm.CreateExceptionRequest (null, false, true);
3349                 req.Enable ();
3350
3351                 var e = run_until ("unhandled_exception");
3352                 vm.Resume ();
3353
3354                 var e2 = GetNextEvent ();
3355                 Assert.IsTrue (e2 is ExceptionEvent);
3356
3357                 vm.Exit (0);
3358                 vm = null;
3359         }
3360
3361         [Test]
3362         public void UnhandledException_2 () {
3363                 vm.Exit (0);
3364
3365                 Start (new string [] { "dtest-app.exe", "unhandled-exception-endinvoke" });
3366
3367                 var req = vm.CreateExceptionRequest (null, false, true);
3368                 req.Enable ();
3369
3370                 MethodMirror m = entry_point.DeclaringType.GetMethod ("unhandled_exception_endinvoke_2");
3371                 Assert.IsNotNull (m);
3372                 vm.SetBreakpoint (m, m.ILOffsets [0]);
3373
3374                 var e = run_until ("unhandled_exception_endinvoke");
3375                 vm.Resume ();
3376
3377                 var e2 = GetNextEvent ();
3378                 Assert.IsFalse (e2 is ExceptionEvent);
3379
3380                 vm.Exit (0);
3381                 vm = null;
3382         }
3383
3384 #if NET_4_5
3385         [Test]
3386         public void UnhandledExceptionUserCode () {
3387                 vm.Detach ();
3388
3389                 // Exceptions caught in non-user code are treated as unhandled
3390                 Start (new string [] { "dtest-app.exe", "unhandled-exception-user" });
3391
3392                 var req = vm.CreateExceptionRequest (null, false, true);
3393                 req.AssemblyFilter = new List<AssemblyMirror> () { entry_point.DeclaringType.Assembly };
3394                 req.Enable ();
3395
3396                 var e = run_until ("unhandled_exception_user");
3397                 vm.Resume ();
3398
3399                 var e2 = GetNextEvent ();
3400                 Assert.IsTrue (e2 is ExceptionEvent);
3401
3402                 vm.Exit (0);
3403                 vm = null;
3404         }
3405 #endif
3406
3407         [Test]
3408         public void GCWhileSuspended () {
3409                 // Check that objects are kept alive during suspensions
3410                 Event e = run_until ("gc_suspend_1");
3411
3412                 MethodMirror m = entry_point.DeclaringType.GetMethod ("gc_suspend_invoke");
3413
3414                 var o = entry_point.DeclaringType.GetValue (entry_point.DeclaringType.GetField ("gc_suspend_field")) as ObjectMirror;
3415                 //Console.WriteLine (o);
3416
3417                 StackFrame frame = e.Thread.GetFrames () [0];
3418                 TypeMirror t = frame.Method.DeclaringType;
3419                 for (int i = 0; i < 10; ++i)
3420                         t.InvokeMethod (e.Thread, m, new Value [] { });
3421
3422                 // This throws an exception if the object is collected
3423                 long addr = o.Address;
3424
3425                 var o2 = entry_point.DeclaringType.GetValue (entry_point.DeclaringType.GetField ("gc_suspend_field")) as ObjectMirror;
3426                 Assert.IsNull (o2);
3427         }
3428
3429         [Test]
3430         public void MakeGenericMethod () {
3431                 Event e = run_until ("bp1");
3432
3433                 var intm = vm.RootDomain.GetCorrespondingType (typeof (int));
3434                 var stringm = vm.RootDomain.GetCorrespondingType (typeof (string));
3435                 var gm = entry_point.DeclaringType.GetMethod ("generic_method");
3436                 var res = gm.MakeGenericMethod (new TypeMirror [] { stringm });
3437                 var args = res.GetGenericArguments ();
3438                 Assert.AreEqual (1, args.Length);
3439                 Assert.AreEqual (stringm, args [0]);
3440
3441                 // Error checking
3442                 AssertThrows<ArgumentNullException> (delegate {
3443                                 gm.MakeGenericMethod (null);
3444                         });
3445                 AssertThrows<ArgumentNullException> (delegate {
3446                                 gm.MakeGenericMethod (new TypeMirror [] { null });
3447                         });
3448                 AssertThrows<ArgumentException> (delegate {
3449                                 gm.MakeGenericMethod (new TypeMirror [] { stringm, stringm });
3450                         });
3451                 AssertThrows<InvalidOperationException> (delegate {
3452                                 gm.MakeGenericMethod (new TypeMirror [] { intm });
3453                         });
3454                 AssertThrows<InvalidOperationException> (delegate {
3455                                 entry_point.DeclaringType.GetMethod ("Main").MakeGenericMethod (new TypeMirror [] { intm });
3456                         });
3457         }
3458
3459         [Test]
3460         public void InspectThreadSuspenedOnWaitOne () {
3461                 TearDown ();
3462                 Start (true, "dtest-app.exe", "wait-one" );
3463
3464                 ThreadMirror.NativeTransitions = true;
3465
3466                 var evt = run_until ("wait_one");
3467                 Assert.IsNotNull (evt, "#1");
3468
3469                 var thread = evt.Thread;
3470                 Assert.AreEqual (ThreadState.Running, thread.ThreadState, "#1.1");
3471
3472                 var frames = thread.GetFrames ();
3473                 Assert.IsNotNull (frames, "#2");
3474                 Assert.AreEqual (2, frames.Length, "#3");
3475                 Assert.AreEqual ("wait_one", frames [0].Method.Name, "#4");
3476                 Assert.AreEqual ("Main", frames [1].Method.Name, "#5");
3477
3478                 vm.Resume ();
3479
3480                 Thread.Sleep (500); //FIXME this is racy, maybe single step? or something?
3481
3482                 vm.Suspend ();
3483                 Assert.AreEqual (ThreadState.WaitSleepJoin, thread.ThreadState, "#6");
3484
3485                 frames = thread.GetFrames ();
3486                 Assert.AreEqual (4, frames.Length, "#7");
3487                 Assert.AreEqual ("WaitOne_internal", frames [0].Method.Name, "#8");
3488                 Assert.AreEqual ("WaitOne", frames [1].Method.Name, "#8.1");
3489                 Assert.AreEqual ("wait_one", frames [2].Method.Name, "#9");
3490                 Assert.AreEqual ("Main", frames [3].Method.Name, "#10");
3491
3492
3493                 var frame = frames [0];
3494                 Assert.IsTrue (frame.IsNativeTransition, "#11.1");
3495                 try {
3496                         frame.GetThis ();
3497                         Assert.Fail ("Known limitation - can't get info from m2n frames");
3498                 } catch (AbsentInformationException) {}
3499
3500                 frame = frames [1];
3501                 Assert.IsFalse (frame.IsNativeTransition, "#12.1");
3502                 var wait_one_this = frame.GetThis ();
3503                 Assert.IsNotNull (wait_one_this, "#12.2");
3504
3505                 frame = frames [2];
3506                 var locals = frame.GetVisibleVariables ();
3507                 Assert.AreEqual (1, locals.Count, "#13.1");
3508
3509                 var local_0 = frame.GetValue (locals [0]);
3510                 Assert.IsNotNull (local_0, "#13.2");
3511
3512                 Assert.AreEqual (wait_one_this, local_0, "#14.2");
3513         }
3514
3515         [Test]
3516         public void GetMethodBody () {
3517                 var bevent = run_until ("Main");
3518
3519                 var m = bevent.Method.DeclaringType.GetMethod ("get_IntProperty");
3520                 var body = m.GetMethodBody ();
3521                 foreach (var ins in body.Instructions) {
3522                         if (ins.OpCode == OpCodes.Ldfld) {
3523                                 var field = (FieldInfoMirror)ins.Operand;
3524                                 Assert.AreEqual ("field_i", field.Name);
3525                         }
3526                 }
3527         }
3528
3529         [Test]
3530         public void EvaluateMethod () {
3531                 var bevent = run_until ("evaluate_method_2");
3532
3533                 var m = bevent.Method.DeclaringType.GetMethod ("get_IntProperty");
3534
3535                 var this_obj = bevent.Thread.GetFrames ()[0].GetThis ();
3536                 var v = m.Evaluate (this_obj, null);
3537                 AssertValue (42, v);
3538         }
3539
3540         [Test]
3541         public void SetIP () {
3542                 var bevent = run_until ("set_ip_1");
3543
3544                 var invalid_loc = bevent.Thread.GetFrames ()[0].Location;
3545
3546                 var req = create_step (bevent);
3547                 var e = step_out ();
3548                 req.Disable ();
3549                 var frames = e.Thread.GetFrames ();
3550                 var locs = frames [0].Method.Locations;
3551                 var next_loc = locs.First (l => (l.LineNumber == frames [0].Location.LineNumber + 2));
3552
3553                 e.Thread.SetIP (next_loc);
3554
3555                 /* Check that i ++; j = 5; was skipped */
3556                 bevent = run_until ("set_ip_2");
3557                 var f = bevent.Thread.GetFrames ()[1];
3558                 AssertValue (2, f.GetValue (f.Method.GetLocal ("i")));
3559                 AssertValue (0, f.GetValue (f.Method.GetLocal ("j")));
3560
3561                 // Error handling
3562                 AssertThrows<ArgumentNullException> (delegate {
3563                                 e.Thread.SetIP (null);
3564                         });
3565
3566                 AssertThrows<ArgumentException> (delegate {
3567                                 e.Thread.SetIP (invalid_loc);
3568                         });
3569         }
3570
3571         [Test]
3572         public void SetIPSingleStep () {
3573                 // Check that single stepping after set-ip steps from the new ip
3574                 var bevent = run_until ("set_ip_1");
3575
3576                 var invalid_loc = bevent.Thread.GetFrames ()[0].Location;
3577
3578                 var req = create_step (bevent);
3579                 req.Size = StepSize.Line;
3580                 var e = step_out ();
3581                 req.Disable ();
3582                 var frames = e.Thread.GetFrames ();
3583                 var locs = frames [0].Method.Locations;
3584                 var prev_loc = locs.First (l => (l.LineNumber == frames [0].Location.LineNumber - 3));
3585                 AssertValue (2, frames [0].GetValue (frames [0].Method.GetLocal ("i")));
3586
3587                 Console.WriteLine ("X: " + frames [0].Location.LineNumber);
3588                 // Set back the ip to the first i ++; line
3589                 e.Thread.SetIP (prev_loc);
3590
3591                 e = step_over ();
3592                 var f = e.Thread.GetFrames ()[0];
3593                 Console.WriteLine (f.GetValue (f.Method.GetLocal ("i")));
3594                 AssertValue (3, f.GetValue (f.Method.GetLocal ("i")));
3595         }
3596
3597         [Test]
3598         public void NewInstanceNoCtor () {
3599                 var bevent = run_until ("Main");
3600
3601                 var stype = bevent.Method.DeclaringType.Assembly.GetType ("AStruct");
3602                 var obj = stype.NewInstance ();
3603                 Assert.IsTrue (obj is ObjectMirror);
3604                 Assert.AreEqual ("AStruct", (obj as ObjectMirror).Type.Name);
3605         }
3606
3607         [Test]
3608         public void StaticCtorFilterInCctor () {
3609                 // Check that single stepping when in a cctor only ignores
3610                 // other cctors, not the current one
3611                 var bevent = run_until ("step_filters");
3612
3613                 var assembly = entry_point.DeclaringType.Assembly;
3614                 var type = assembly.GetType ("Tests/ClassWithCctor");
3615                 var cctor = type.GetMethod (".cctor");
3616                 vm.SetBreakpoint (cctor, 0);
3617
3618                 vm.Resume ();
3619                 var e = vm.GetNextEvent ();
3620                 Assert.IsTrue (e is BreakpointEvent);
3621
3622                 var req = create_step (e);
3623                 req.Filter = StepFilter.StaticCtor;
3624                 e = step_into ();
3625                 // Make sure we are still in the cctor
3626                 Assert.AreEqual (".cctor", e.Thread.GetFrames ()[0].Location.Method.Name);
3627         }
3628 }
3629
3630 }