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