2 using System.Collections.Generic;
3 using System.Threading;
5 using System.Reflection;
8 using Mono.Debugger.Soft;
9 using Diag = System.Diagnostics;
12 using NUnit.Framework;
14 #pragma warning disable 0219
17 public class DebuggerTests
20 MethodMirror entry_point;
21 StepEventRequest step_req;
23 void AssertThrows<ExType> (Action del) where ExType : Exception {
31 Assert.IsTrue (thrown);
34 // No other way to pass arguments to the tests ?
35 public static bool listening = Environment.GetEnvironmentVariable ("DBG_SUSPEND") != null;
36 public static string runtime = Environment.GetEnvironmentVariable ("DBG_RUNTIME");
37 public static string agent_args = Environment.GetEnvironmentVariable ("DBG_AGENT_ARGS");
39 void Start (string[] args) {
41 var pi = new Diag.ProcessStartInfo ();
44 pi.FileName = runtime;
47 pi.Arguments = String.Join (" ", args);
48 vm = VirtualMachineManager.Launch (pi, new LaunchOptions { AgentArgs = agent_args });
50 Console.WriteLine ("Listening...");
51 vm = VirtualMachineManager.Listen (new IPEndPoint (IPAddress.Any, 10000));
54 var load_req = vm.CreateAssemblyLoadRequest ();
57 Event vmstart = vm.GetNextEvent ();
58 Assert.AreEqual (EventType.VMStart, vmstart.EventType);
67 /* Find out the entry point */
69 e = vm.GetNextEvent ();
71 if (e is AssemblyLoadEvent) {
72 AssemblyLoadEvent ae = (AssemblyLoadEvent)e;
73 entry_point = ae.Assembly.EntryPoint;
74 if (entry_point != null)
84 BreakpointEvent run_until (string name) {
86 MethodMirror m = entry_point.DeclaringType.GetMethod (name);
88 vm.SetBreakpoint (m, 0);
94 e = vm.GetNextEvent ();
95 if (e is BreakpointEvent)
99 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
100 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
102 return (e as BreakpointEvent);
105 Event single_step (ThreadMirror t) {
106 var req = vm.CreateStepRequest (t);
110 Event e = vm.GetNextEvent ();
111 Assert.IsTrue (e is StepEvent);
118 void check_arg_val (StackFrame frame, int pos, Type type, object eval) {
119 object val = frame.GetArgument (pos);
120 Assert.IsTrue (val is PrimitiveValue);
121 object v = (val as PrimitiveValue).Value;
122 Assert.AreEqual (type, v.GetType ());
124 Assert.IsTrue (Math.Abs ((float)eval - (float)v) < 0.0001);
125 else if (eval is double)
126 Assert.IsTrue (Math.Abs ((double)eval - (double)v) < 0.0001);
128 Assert.AreEqual (eval, v);
131 void AssertValue (object expected, object val) {
132 if (expected is string) {
133 Assert.IsTrue (val is StringMirror);
134 Assert.AreEqual (expected, (val as StringMirror).Value);
135 } else if (val is StructMirror && (val as StructMirror).Type.Name == "IntPtr") {
136 AssertValue (expected, (val as StructMirror).Fields [0]);
138 Assert.IsTrue (val is PrimitiveValue);
139 Assert.AreEqual (expected, (val as PrimitiveValue).Value);
144 public void SetUp () {
145 Start (new string [] { "dtest-app.exe" });
149 public void TearDown () {
153 if (step_req != null)
158 Event e = vm.GetNextEvent ();
160 if (e is VMDeathEvent)
168 public void SimpleBreakpoint () {
171 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp1");
172 Assert.IsNotNull (m);
174 vm.SetBreakpoint (m, 0);
178 e = vm.GetNextEvent ();
179 Assert.AreEqual (EventType.Breakpoint, e.EventType);
180 Assert.IsTrue (e is BreakpointEvent);
181 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
184 AssertThrows<ArgumentException> (delegate {
186 vm.SetBreakpoint (m, 1);
191 public void BreakpointsSameLocation () {
194 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp2");
195 Assert.IsNotNull (m);
197 vm.SetBreakpoint (m, 0);
198 vm.SetBreakpoint (m, 0);
202 e = vm.GetNextEvent ();
203 Assert.IsTrue (e is BreakpointEvent);
204 Assert.AreEqual (m, (e as BreakpointEvent).Method);
206 e = vm.GetNextEvent ();
207 Assert.IsTrue (e is BreakpointEvent);
208 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
212 public void BreakpointAlreadyJITted () {
213 Event e = run_until ("bp1");
215 /* Place a breakpoint on bp3 */
216 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp3");
217 Assert.IsNotNull (m);
218 vm.SetBreakpoint (m, 0);
220 /* Same with generic instances */
221 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp7");
222 Assert.IsNotNull (m2);
223 vm.SetBreakpoint (m2, 0);
227 e = vm.GetNextEvent ();
228 Assert.AreEqual (EventType.Breakpoint, e.EventType);
229 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
233 /* Non-shared instance */
234 e = vm.GetNextEvent ();
235 Assert.AreEqual (EventType.Breakpoint, e.EventType);
236 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
240 /* Shared instance */
241 e = vm.GetNextEvent ();
242 Assert.AreEqual (EventType.Breakpoint, e.EventType);
243 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
247 public void ClearBreakpoint () {
250 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
251 Assert.IsNotNull (m);
252 EventRequest req1 = vm.SetBreakpoint (m, 0);
253 EventRequest req2 = vm.SetBreakpoint (m, 0);
255 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
256 Assert.IsNotNull (m2);
257 vm.SetBreakpoint (m2, 0);
262 e = vm.GetNextEvent ();
263 Assert.AreEqual (EventType.Breakpoint, e.EventType);
264 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
265 e = vm.GetNextEvent ();
266 Assert.AreEqual (EventType.Breakpoint, e.EventType);
267 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
269 /* Clear one of them */
274 e = vm.GetNextEvent ();
275 Assert.AreEqual (EventType.Breakpoint, e.EventType);
276 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
278 /* Clear the other */
283 e = vm.GetNextEvent ();
284 Assert.AreEqual (EventType.Breakpoint, e.EventType);
285 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
289 public void ClearAllBreakpoints () {
292 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
293 Assert.IsNotNull (m);
294 vm.SetBreakpoint (m, 0);
296 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
297 Assert.IsNotNull (m2);
298 vm.SetBreakpoint (m2, 0);
300 vm.ClearAllBreakpoints ();
304 e = vm.GetNextEvent ();
305 Assert.IsTrue (!(e is BreakpointEvent));
306 if (e is VMDeathEvent)
311 public void BreakpointOnGShared () {
314 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp6");
315 Assert.IsNotNull (m);
317 vm.SetBreakpoint (m, 0);
321 e = vm.GetNextEvent ();
322 Assert.AreEqual (EventType.Breakpoint, e.EventType);
323 Assert.IsTrue (e is BreakpointEvent);
324 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
328 public void SingleStepping () {
329 Event e = run_until ("single_stepping");
331 var req = vm.CreateStepRequest (e.Thread);
338 e = vm.GetNextEvent ();
339 Assert.IsTrue (e is StepEvent);
340 Assert.AreEqual ("ss1", (e as StepEvent).Method.Name);
344 e = vm.GetNextEvent ();
345 Assert.IsTrue (e is StepEvent);
346 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
348 // Change to step over
350 req.Depth = StepDepth.Over;
355 e = vm.GetNextEvent ();
356 Assert.IsTrue (e is StepEvent);
357 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
359 // Change to step into
361 req.Depth = StepDepth.Into;
366 e = vm.GetNextEvent ();
367 Assert.IsTrue (e is StepEvent);
368 Assert.AreEqual ("ss3", (e as StepEvent).Method.Name);
370 // Change to step out
372 req.Depth = StepDepth.Out;
375 // Step back into single_stepping
377 e = vm.GetNextEvent ();
378 Assert.IsTrue (e is StepEvent);
379 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
381 // Change to step into
383 req.Depth = StepDepth.Into;
386 // Step into ss3_2 ()
388 e = vm.GetNextEvent ();
389 Assert.IsTrue (e is StepEvent);
390 Assert.AreEqual ("ss3_2", (e as StepEvent).Method.Name);
392 // Change to step over
394 req.Depth = StepDepth.Over;
397 // Step over ss3_2_2 ()
399 e = vm.GetNextEvent ();
400 Assert.IsTrue (e is StepEvent);
401 Assert.AreEqual ("ss3_2", (e as StepEvent).Method.Name);
403 // Recreate the request
407 // Step back into single_stepping () with the new request
409 e = vm.GetNextEvent ();
410 Assert.IsTrue (e is StepEvent);
411 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
413 // Change to step into
415 req.Depth = StepDepth.Into;
420 e = vm.GetNextEvent ();
421 Assert.IsTrue (e is StepEvent);
422 Assert.AreEqual ("ss4", (e as StepEvent).Method.Name);
424 // Change to StepSize.Line
426 req.Depth = StepDepth.Over;
427 req.Size = StepSize.Line;
430 // Step over ss1 (); ss1 ();
432 e = vm.GetNextEvent ();
433 Assert.IsTrue (e is StepEvent);
437 req.Depth = StepDepth.Into;
441 e = vm.GetNextEvent ();
442 Assert.IsTrue (e is StepEvent);
443 Assert.AreEqual ("ss2", (e as StepEvent).Method.Name);
448 e = run_until ("ss5");
450 // Add an assembly filter
451 req.AssemblyFilter = new AssemblyMirror [] { (e as BreakpointEvent).Method.DeclaringType.Assembly };
454 // Step into is_even, skipping the linq stuff
456 e = vm.GetNextEvent ();
457 Assert.IsTrue (e is StepEvent);
458 Assert.AreEqual ("is_even", (e as StepEvent).Method.Name);
460 // FIXME: Check that single stepping works with lock (obj)
465 e = run_until ("ss6");
467 req = vm.CreateStepRequest (e.Thread);
468 req.Depth = StepDepth.Over;
471 // Check that single stepping works in out-of-line bblocks
473 e = vm.GetNextEvent ();
474 Assert.IsTrue (e is StepEvent);
476 e = vm.GetNextEvent ();
477 Assert.IsTrue (e is StepEvent);
478 Assert.AreEqual ("ss6", (e as StepEvent).Method.Name);
484 public void MethodEntryExit () {
485 run_until ("single_stepping");
487 var req1 = vm.CreateMethodEntryRequest ();
488 var req2 = vm.CreateMethodExitRequest ();
494 Event e = vm.GetNextEvent ();
495 Assert.IsTrue (e is MethodEntryEvent);
496 Assert.AreEqual ("ss1", (e as MethodEntryEvent).Method.Name);
499 e = vm.GetNextEvent ();
500 Assert.IsTrue (e is MethodExitEvent);
501 Assert.AreEqual ("ss1", (e as MethodExitEvent).Method.Name);
508 public void CountFilter () {
509 run_until ("single_stepping");
511 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("ss3");
512 Assert.IsNotNull (m2);
513 vm.SetBreakpoint (m2, 0);
515 var req1 = vm.CreateMethodEntryRequest ();
519 // Enter ss2, ss1 is skipped
521 Event e = vm.GetNextEvent ();
522 Assert.IsTrue (e is MethodEntryEvent);
523 Assert.AreEqual ("ss2", (e as MethodEntryEvent).Method.Name);
525 // Breakpoint on ss3, the entry event is no longer reported
527 e = vm.GetNextEvent ();
528 Assert.IsTrue (e is BreakpointEvent);
534 public void Arguments () {
537 var e = run_until ("arg1");
539 StackFrame frame = e.Thread.GetFrames () [0];
541 check_arg_val (frame, 0, typeof (sbyte), SByte.MaxValue - 5);
542 check_arg_val (frame, 1, typeof (byte), Byte.MaxValue - 5);
543 check_arg_val (frame, 2, typeof (bool), true);
544 check_arg_val (frame, 3, typeof (short), Int16.MaxValue - 5);
545 check_arg_val (frame, 4, typeof (ushort), UInt16.MaxValue - 5);
546 check_arg_val (frame, 5, typeof (char), 'F');
547 check_arg_val (frame, 6, typeof (int), Int32.MaxValue - 5);
548 check_arg_val (frame, 7, typeof (uint), UInt32.MaxValue - 5);
549 check_arg_val (frame, 8, typeof (long), Int64.MaxValue - 5);
550 check_arg_val (frame, 9, typeof (ulong), UInt64.MaxValue - 5);
551 check_arg_val (frame, 10, typeof (float), 1.2345f);
552 check_arg_val (frame, 11, typeof (double), 6.78910);
554 e = run_until ("arg2");
556 frame = e.Thread.GetFrames () [0];
559 val = frame.GetArgument (0);
560 AssertValue ("FOO", val);
561 Assert.AreEqual ("String", (val as ObjectMirror).Type.Name);
564 val = frame.GetArgument (1);
565 AssertValue (null, val);
568 val = frame.GetArgument (2);
569 AssertValue ("BLA", val);
572 val = frame.GetArgument (3);
573 AssertValue (42, val);
576 val = frame.GetArgument (4);
577 Assert.IsTrue (val is ObjectMirror);
578 Assert.AreEqual ("GClass`1", (val as ObjectMirror).Type.Name);
581 val = frame.GetArgument (5);
582 Assert.IsTrue (val is ObjectMirror);
583 Assert.AreEqual ("Object", (val as ObjectMirror).Type.Name);
585 // this on static methods
586 val = frame.GetThis ();
587 AssertValue (null, val);
589 e = run_until ("arg3");
591 frame = e.Thread.GetFrames () [0];
594 val = frame.GetThis ();
595 Assert.IsTrue (val is ObjectMirror);
596 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
598 // objref in register
599 val = frame.GetArgument (0);
600 AssertValue ("BLA", val);
604 public void Arrays () {
607 var e = run_until ("o2");
609 StackFrame frame = e.Thread.GetFrames () [0];
612 val = frame.GetArgument (0);
613 Assert.IsTrue (val is ArrayMirror);
614 ArrayMirror arr = val as ArrayMirror;
615 Assert.AreEqual (2, arr.Length);
616 AssertValue ("BAR", arr [0]);
617 AssertValue ("BAZ", arr [1]);
619 var vals = arr.GetValues (0, 2);
620 Assert.AreEqual (2, vals.Count);
621 AssertValue ("BAR", vals [0]);
622 AssertValue ("BAZ", vals [1]);
624 arr [0] = vm.RootDomain.CreateString ("ABC");
625 AssertValue ("ABC", arr [0]);
627 arr [0] = vm.CreateValue (null);
628 AssertValue (null, arr [0]);
630 arr.SetValues (0, new Value [] { vm.RootDomain.CreateString ("D1"), vm.RootDomain.CreateString ("D2") });
631 AssertValue ("D1", arr [0]);
632 AssertValue ("D2", arr [1]);
635 val = frame.GetArgument (1);
636 Assert.IsTrue (val is ArrayMirror);
637 arr = val as ArrayMirror;
638 Assert.AreEqual (2, arr.Length);
639 AssertValue (42, arr [0]);
640 AssertValue (43, arr [1]);
643 AssertThrows<IndexOutOfRangeException> (delegate () {
647 AssertThrows<IndexOutOfRangeException> (delegate () {
648 val = arr [Int32.MinValue];
651 AssertThrows<IndexOutOfRangeException> (delegate () {
652 vals = arr.GetValues (0, 3);
655 AssertThrows<IndexOutOfRangeException> (delegate () {
656 arr [2] = vm.CreateValue (null);
659 AssertThrows<IndexOutOfRangeException> (delegate () {
660 arr [Int32.MinValue] = vm.CreateValue (null);
663 AssertThrows<IndexOutOfRangeException> (delegate () {
664 arr.SetValues (0, new Value [] { null, null, null });
668 val = frame.GetArgument (2);
669 Assert.IsTrue (val is ArrayMirror);
670 arr = val as ArrayMirror;
671 Assert.AreEqual (2, arr.Rank);
672 Assert.AreEqual (4, arr.Length);
673 Assert.AreEqual (2, arr.GetLength (0));
674 Assert.AreEqual (2, arr.GetLength (1));
675 Assert.AreEqual (0, arr.GetLowerBound (0));
676 Assert.AreEqual (0, arr.GetLowerBound (1));
677 vals = arr.GetValues (0, 4);
678 AssertValue (1, vals [0]);
679 AssertValue (2, vals [1]);
680 AssertValue (3, vals [2]);
681 AssertValue (4, vals [3]);
683 val = frame.GetArgument (3);
684 Assert.IsTrue (val is ArrayMirror);
685 arr = val as ArrayMirror;
686 Assert.AreEqual (2, arr.Rank);
687 Assert.AreEqual (4, arr.Length);
688 Assert.AreEqual (2, arr.GetLength (0));
689 Assert.AreEqual (2, arr.GetLength (1));
690 Assert.AreEqual (1, arr.GetLowerBound (0));
691 Assert.AreEqual (3, arr.GetLowerBound (1));
693 AssertThrows<ArgumentOutOfRangeException> (delegate () {
696 AssertThrows<ArgumentOutOfRangeException> (delegate () {
700 AssertThrows<ArgumentOutOfRangeException> (delegate () {
701 arr.GetLowerBound (-1);
703 AssertThrows<ArgumentOutOfRangeException> (delegate () {
704 arr.GetLowerBound (2);
707 // arrays treated as generic collections
708 val = frame.GetArgument (4);
709 Assert.IsTrue (val is ArrayMirror);
710 arr = val as ArrayMirror;
714 public void Object_GetValue () {
715 var e = run_until ("o1");
716 var frame = e.Thread.GetFrames () [0];
718 object val = frame.GetThis ();
719 Assert.IsTrue (val is ObjectMirror);
720 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
721 ObjectMirror o = (val as ObjectMirror);
723 TypeMirror t = o.Type;
726 object f = o.GetValue (t.GetField ("field_i"));
728 f = o.GetValue (t.GetField ("field_s"));
729 AssertValue ("S", f);
730 f = o.GetValue (t.GetField ("field_enum"));
731 Assert.IsTrue (f is EnumMirror);
732 Assert.AreEqual (1, (f as EnumMirror).Value);
733 Assert.AreEqual ("B", (f as EnumMirror).StringValue);
735 // Inherited object fields
736 TypeMirror parent = t.BaseType;
737 f = o.GetValue (parent.GetField ("base_field_i"));
739 f = o.GetValue (parent.GetField ("base_field_s"));
740 AssertValue ("T", f);
743 f = o.GetValue (o.Type.GetField ("static_i"));
747 ObjectMirror o2 = frame.GetValue (frame.Method.GetParameters ()[1]) as ObjectMirror;
748 Assert.AreEqual ("GClass`1", o2.Type.Name);
749 TypeMirror t2 = o2.Type;
750 f = o2.GetValue (t2.GetField ("field"));
753 ObjectMirror o3 = frame.GetValue (frame.Method.GetParameters ()[2]) as ObjectMirror;
754 Assert.AreEqual ("GClass`1", o3.Type.Name);
755 TypeMirror t3 = o3.Type;
756 f = o3.GetValue (t3.GetField ("field"));
757 AssertValue ("FOO", f);
760 AssertThrows<ArgumentNullException> (delegate () {
766 public void Object_GetValues () {
767 var e = run_until ("o1");
768 var frame = e.Thread.GetFrames () [0];
770 object val = frame.GetThis ();
771 Assert.IsTrue (val is ObjectMirror);
772 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
773 ObjectMirror o = (val as ObjectMirror);
775 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
777 TypeMirror t = o.Type;
779 object[] vals = o.GetValues (new FieldInfoMirror [] { t.GetField ("field_i"), t.GetField ("field_s") });
783 AssertValue ("S", f);
786 AssertThrows<ArgumentNullException> (delegate () {
790 AssertThrows<ArgumentNullException> (delegate () {
791 o.GetValues (new FieldInfoMirror [] { null });
794 // field of another class
795 AssertThrows<ArgumentException> (delegate () {
796 o.GetValue (val2.Type.GetField ("field_j"));
800 void TestSetValue (ObjectMirror o, string field_name, object val) {
802 o.SetValue (o.Type.GetField (field_name), vm.RootDomain.CreateString ((string)val));
804 o.SetValue (o.Type.GetField (field_name), vm.CreateValue (val));
805 Value f = o.GetValue (o.Type.GetField (field_name));
806 AssertValue (val, f);
810 public void Object_SetValues () {
811 var e = run_until ("o1");
812 var frame = e.Thread.GetFrames () [0];
814 object val = frame.GetThis ();
815 Assert.IsTrue (val is ObjectMirror);
816 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
817 ObjectMirror o = (val as ObjectMirror);
819 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
821 TestSetValue (o, "field_i", 22);
822 TestSetValue (o, "field_bool1", false);
823 TestSetValue (o, "field_bool2", true);
824 TestSetValue (o, "field_char", 'B');
825 TestSetValue (o, "field_byte", (byte)129);
826 TestSetValue (o, "field_sbyte", (sbyte)-33);
827 TestSetValue (o, "field_short", (short)(Int16.MaxValue - 5));
828 TestSetValue (o, "field_ushort", (ushort)(UInt16.MaxValue - 5));
829 TestSetValue (o, "field_long", Int64.MaxValue - 5);
830 TestSetValue (o, "field_ulong", (ulong)(UInt64.MaxValue - 5));
831 TestSetValue (o, "field_float", 6.28f);
832 TestSetValue (o, "field_double", 6.28);
833 TestSetValue (o, "static_i", 23);
834 TestSetValue (o, "field_s", "CDEF");
839 f = o.GetValue (o.Type.GetField ("field_intptr"));
840 Assert.IsInstanceOfType (typeof (StructMirror), f);
841 AssertValue (Int32.MaxValue - 5, (f as StructMirror).Fields [0]);
845 FieldInfoMirror field = o.Type.GetField ("field_enum");
846 f = o.GetValue (field);
847 (f as EnumMirror).Value = 5;
848 o.SetValue (field, f);
849 f = o.GetValue (field);
850 Assert.AreEqual (5, (f as EnumMirror).Value);
853 o.SetValue (o.Type.GetField ("field_s"), vm.CreateValue (null));
854 f = o.GetValue (o.Type.GetField ("field_s"));
855 AssertValue (null, f);
858 field = o.Type.GetField ("generic_field_struct");
859 f = o.GetValue (field);
860 o.SetValue (field, f);
863 AssertThrows<ArgumentNullException> (delegate () {
864 o.SetValues (null, new Value [0]);
867 AssertThrows<ArgumentNullException> (delegate () {
868 o.SetValues (new FieldInfoMirror [0], null);
871 AssertThrows<ArgumentNullException> (delegate () {
872 o.SetValues (new FieldInfoMirror [] { null }, new Value [1] { null });
875 // vtype with a wrong type
876 AssertThrows<ArgumentException> (delegate () {
877 o.SetValue (o.Type.GetField ("field_struct"), o.GetValue (o.Type.GetField ("field_enum")));
880 // reference type not assignment compatible
881 AssertThrows<ArgumentException> (delegate () {
882 o.SetValue (o.Type.GetField ("field_class"), o);
885 // field of another class
886 AssertThrows<ArgumentException> (delegate () {
887 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
892 public void Type_SetValue () {
893 var e = run_until ("o1");
894 var frame = e.Thread.GetFrames () [0];
897 object val = frame.GetThis ();
898 Assert.IsTrue (val is ObjectMirror);
899 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
900 ObjectMirror o = (val as ObjectMirror);
902 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
904 o.Type.SetValue (o.Type.GetField ("static_i"), vm.CreateValue (55));
905 f = o.Type.GetValue (o.Type.GetField ("static_i"));
908 o.Type.SetValue (o.Type.GetField ("static_s"), vm.RootDomain.CreateString ("B"));
909 f = o.Type.GetValue (o.Type.GetField ("static_s"));
910 AssertValue ("B", f);
913 AssertThrows<ArgumentNullException> (delegate () {
914 o.Type.SetValue (null, vm.CreateValue (0));
917 AssertThrows<ArgumentNullException> (delegate () {
918 o.Type.SetValue (o.Type.GetField ("static_i"), null);
921 // field of another class
922 AssertThrows<ArgumentException> (delegate () {
923 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
928 public void TypeInfo () {
929 Event e = run_until ("ti2");
930 StackFrame frame = e.Thread.GetFrames () [0];
935 t = frame.Method.GetParameters ()[0].ParameterType;
937 Assert.AreEqual ("String[]", t.Name);
938 Assert.AreEqual ("string[]", t.CSharpName);
939 Assert.AreEqual ("Array", t.BaseType.Name);
940 Assert.AreEqual (true, t.HasElementType);
941 Assert.AreEqual (true, t.IsArray);
942 Assert.AreEqual (1, t.GetArrayRank ());
943 Assert.AreEqual ("String", t.GetElementType ().Name);
945 t = frame.Method.GetParameters ()[2].ParameterType;
947 Assert.AreEqual ("Int32[,]", t.Name);
949 //Assert.AreEqual ("int[,]", t.CSharpName);
950 Assert.AreEqual ("Array", t.BaseType.Name);
951 Assert.AreEqual (true, t.HasElementType);
952 Assert.AreEqual (true, t.IsArray);
953 Assert.AreEqual (2, t.GetArrayRank ());
954 Assert.AreEqual ("Int32", t.GetElementType ().Name);
957 t = frame.Method.GetParameters ()[3].ParameterType;
959 //Assert.AreEqual ("Int32&", t.Name);
960 //Assert.AreEqual (true, t.IsByRef);
961 //Assert.AreEqual (true, t.HasElementType);
964 t = frame.Method.GetParameters ()[4].ParameterType;
966 //Assert.AreEqual ("Int32*", t.Name);
967 Assert.AreEqual (true, t.IsPointer);
968 Assert.AreEqual (true, t.HasElementType);
969 Assert.AreEqual ("Int32", t.GetElementType ().Name);
970 Assert.AreEqual (false, t.IsPrimitive);
973 t = frame.Method.GetParameters ()[5].ParameterType;
974 Assert.AreEqual (true, t.IsPrimitive);
977 t = frame.Method.GetParameters ()[6].ParameterType;
978 Assert.AreEqual ("AStruct", t.Name);
979 Assert.AreEqual (false, t.IsPrimitive);
980 Assert.AreEqual (true, t.IsValueType);
981 Assert.AreEqual (false, t.IsClass);
984 t = frame.Method.GetParameters ()[7].ParameterType;
985 Assert.AreEqual ("Tests", t.Name);
986 var nested = (from nt in t.GetNestedTypes () where nt.IsNestedPublic select nt).ToArray ();
987 Assert.AreEqual (1, nested.Length);
988 Assert.AreEqual ("NestedClass", nested [0].Name);
989 Assert.IsTrue (t.BaseType.IsAssignableFrom (t));
990 Assert.IsTrue (!t.IsAssignableFrom (t.BaseType));
993 t = frame.Method.GetParameters ()[9].ParameterType;
994 Assert.AreEqual ("GClass`1", t.Name);
997 t = frame.Method.GetParameters ()[10].ParameterType;
998 Assert.AreEqual ("AnEnum", t.Name);
999 Assert.IsTrue (t.IsEnum);
1000 Assert.AreEqual ("Int32", t.EnumUnderlyingType.Name);
1003 t = frame.Method.GetParameters ()[7].ParameterType;
1005 var props = t.GetProperties ();
1006 Assert.AreEqual (3, props.Length);
1007 foreach (PropertyInfoMirror prop in props) {
1008 ParameterInfoMirror[] indexes = prop.GetIndexParameters ();
1010 if (prop.Name == "IntProperty") {
1011 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1012 Assert.AreEqual ("get_IntProperty", prop.GetGetMethod ().Name);
1013 Assert.AreEqual ("set_IntProperty", prop.GetSetMethod ().Name);
1014 Assert.AreEqual (0, indexes.Length);
1015 } else if (prop.Name == "ReadOnlyProperty") {
1016 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1017 Assert.AreEqual ("get_ReadOnlyProperty", prop.GetGetMethod ().Name);
1018 Assert.AreEqual (null, prop.GetSetMethod ());
1019 Assert.AreEqual (0, indexes.Length);
1020 } else if (prop.Name == "IndexedProperty") {
1021 Assert.AreEqual (1, indexes.Length);
1022 Assert.AreEqual ("Int32", indexes [0].ParameterType.Name);
1026 // custom attributes
1027 t = frame.Method.GetParameters ()[8].ParameterType;
1028 Assert.AreEqual ("Tests2", t.Name);
1029 var attrs = t.GetCustomAttributes (true);
1030 Assert.AreEqual (2, attrs.Length);
1031 foreach (var attr in attrs) {
1032 if (attr.Constructor.DeclaringType.Name == "DebuggerDisplayAttribute") {
1033 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1034 Assert.AreEqual ("Tests", attr.ConstructorArguments [0].Value);
1035 Assert.AreEqual (2, attr.NamedArguments.Count);
1036 Assert.AreEqual ("Name", attr.NamedArguments [0].Property.Name);
1037 Assert.AreEqual ("FOO", attr.NamedArguments [0].TypedValue.Value);
1038 Assert.AreEqual ("Target", attr.NamedArguments [1].Property.Name);
1039 Assert.IsInstanceOfType (typeof (TypeMirror), attr.NamedArguments [1].TypedValue.Value);
1040 Assert.AreEqual ("Int32", (attr.NamedArguments [1].TypedValue.Value as TypeMirror).Name);
1041 } else if (attr.Constructor.DeclaringType.Name == "DebuggerTypeProxyAttribute") {
1042 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1043 Assert.IsInstanceOfType (typeof (TypeMirror), attr.ConstructorArguments [0].Value);
1044 Assert.AreEqual ("Tests", (attr.ConstructorArguments [0].Value as TypeMirror).Name);
1046 Assert.Fail (attr.Constructor.DeclaringType.Name);
1052 public void FieldInfo () {
1053 Event e = run_until ("ti2");
1054 StackFrame frame = e.Thread.GetFrames () [0];
1058 t = frame.Method.GetParameters ()[8].ParameterType;
1059 Assert.AreEqual ("Tests2", t.Name);
1061 var fi = t.GetField ("field_j");
1062 var attrs = fi.GetCustomAttributes (true);
1063 Assert.AreEqual (1, attrs.Length);
1064 var attr = attrs [0];
1065 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1066 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1067 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1068 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1072 public void PropertyInfo () {
1073 Event e = run_until ("ti2");
1074 StackFrame frame = e.Thread.GetFrames () [0];
1078 t = frame.Method.GetParameters ()[8].ParameterType;
1079 Assert.AreEqual ("Tests2", t.Name);
1081 var pi = t.GetProperty ("AProperty");
1082 var attrs = pi.GetCustomAttributes (true);
1083 Assert.AreEqual (1, attrs.Length);
1084 var attr = attrs [0];
1085 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1086 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1087 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1088 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1092 [Category ("only5")]
1093 public void Type_GetValue () {
1094 Event e = run_until ("o1");
1095 StackFrame frame = e.Thread.GetFrames () [0];
1097 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1099 TypeMirror t = o.Type;
1101 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1104 object f = t.GetValue (o.Type.GetField ("static_i"));
1105 AssertValue (55, f);
1107 f = t.GetValue (o.Type.GetField ("static_s"));
1108 AssertValue ("A", f);
1110 // literal static fields
1111 f = t.GetValue (o.Type.GetField ("literal_i"));
1112 AssertValue (56, f);
1114 f = t.GetValue (o.Type.GetField ("literal_s"));
1115 AssertValue ("B", f);
1117 // Inherited static fields
1118 TypeMirror parent = t.BaseType;
1119 f = t.GetValue (parent.GetField ("base_static_i"));
1120 AssertValue (57, f);
1122 f = t.GetValue (parent.GetField ("base_static_s"));
1123 AssertValue ("C", f);
1125 // thread static field
1126 f = t.GetValue (t.GetField ("tls_i"), e.Thread);
1127 AssertValue (42, f);
1129 // Argument checking
1130 AssertThrows<ArgumentNullException> (delegate () {
1135 AssertThrows<ArgumentException> (delegate () {
1136 t.GetValue (o.Type.GetField ("field_i"));
1139 // field on another type
1140 AssertThrows<ArgumentException> (delegate () {
1141 t.GetValue (val2.Type.GetField ("static_field_j"));
1144 // special static field
1145 AssertThrows<ArgumentException> (delegate () {
1146 t.GetValue (t.GetField ("tls_i"));
1151 public void Type_GetValues () {
1152 Event e = run_until ("o1");
1153 StackFrame frame = e.Thread.GetFrames () [0];
1155 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1157 TypeMirror t = o.Type;
1160 object[] vals = t.GetValues (new FieldInfoMirror [] { t.GetField ("static_i"), t.GetField ("static_s") });
1161 object f = vals [0];
1162 AssertValue (55, f);
1165 AssertValue ("A", f);
1167 // Argument checking
1168 AssertThrows<ArgumentNullException> (delegate () {
1172 AssertThrows<ArgumentNullException> (delegate () {
1173 t.GetValues (new FieldInfoMirror [] { null });
1178 public void ObjRefs () {
1179 Event e = run_until ("objrefs1");
1180 StackFrame frame = e.Thread.GetFrames () [0];
1182 ObjectMirror o = frame.GetThis () as ObjectMirror;
1183 ObjectMirror child = o.GetValue (o.Type.GetField ("child")) as ObjectMirror;
1185 Assert.IsTrue (child.Address > 0);
1187 // Check that object references are internalized correctly
1188 Assert.AreEqual (o, frame.GetThis ());
1190 run_until ("objrefs2");
1192 // child should be gc'd now
1193 Assert.IsTrue (child.IsCollected);
1195 AssertThrows<ObjectCollectedException> (delegate () {
1196 TypeMirror t = child.Type;
1199 AssertThrows<ObjectCollectedException> (delegate () {
1200 long addr = child.Address;
1205 public void Type_GetObject () {
1206 Event e = run_until ("o1");
1207 StackFrame frame = e.Thread.GetFrames () [0];
1209 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1211 TypeMirror t = o.Type;
1213 Assert.AreEqual ("MonoType", t.GetTypeObject ().Type.Name);
1217 public void VTypes () {
1218 Event e = run_until ("vtypes1");
1219 StackFrame frame = e.Thread.GetFrames () [0];
1222 ObjectMirror o = frame.GetThis () as ObjectMirror;
1223 var obj = o.GetValue (o.Type.GetField ("field_struct"));
1224 Assert.IsTrue (obj is StructMirror);
1225 var s = obj as StructMirror;
1226 Assert.AreEqual ("AStruct", s.Type.Name);
1227 AssertValue (42, s ["i"]);
1229 AssertValue ("S", obj);
1230 AssertValue (43, s ["k"]);
1231 obj = o.GetValue (o.Type.GetField ("field_boxed_struct"));
1232 Assert.IsTrue (obj is StructMirror);
1233 s = obj as StructMirror;
1234 Assert.AreEqual ("AStruct", s.Type.Name);
1235 AssertValue (42, s ["i"]);
1237 // vtypes as arguments
1238 s = frame.GetArgument (0) as StructMirror;
1239 AssertValue (44, s ["i"]);
1241 AssertValue ("T", obj);
1242 AssertValue (45, s ["k"]);
1244 // vtypes as array entries
1245 var arr = frame.GetArgument (1) as ArrayMirror;
1247 Assert.IsTrue (obj is StructMirror);
1248 s = obj as StructMirror;
1249 AssertValue (1, s ["i"]);
1250 AssertValue ("S1", s ["s"]);
1252 Assert.IsTrue (obj is StructMirror);
1253 s = obj as StructMirror;
1254 AssertValue (2, s ["i"]);
1255 AssertValue ("S2", s ["s"]);
1257 // Argument checking
1258 s = frame.GetArgument (0) as StructMirror;
1259 AssertThrows<ArgumentException> (delegate () {
1263 // generic vtype instances
1264 o = frame.GetThis () as ObjectMirror;
1265 obj = o.GetValue (o.Type.GetField ("generic_field_struct"));
1266 Assert.IsTrue (obj is StructMirror);
1267 s = obj as StructMirror;
1268 Assert.AreEqual ("GStruct`1", s.Type.Name);
1269 AssertValue (42, s ["i"]);
1271 // this on vtype methods
1272 e = run_until ("vtypes2");
1274 e = single_step (e.Thread);
1276 frame = e.Thread.GetFrames () [0];
1278 Assert.AreEqual ("foo", (e as StepEvent).Method.Name);
1279 obj = frame.GetThis ();
1281 Assert.IsTrue (obj is StructMirror);
1282 s = obj as StructMirror;
1283 AssertValue (44, s ["i"]);
1284 AssertValue ("T", s ["s"]);
1285 AssertValue (45, s ["k"]);
1287 // this on static vtype methods
1288 e = run_until ("vtypes3");
1290 e = single_step (e.Thread);
1292 frame = e.Thread.GetFrames () [0];
1294 Assert.AreEqual ("static_foo", (e as StepEvent).Method.Name);
1295 obj = frame.GetThis ();
1296 AssertValue (null, obj);
1300 public void AssemblyInfo () {
1301 Event e = run_until ("single_stepping");
1303 StackFrame frame = e.Thread.GetFrames () [0];
1305 var aname = frame.Method.DeclaringType.Assembly.GetName ();
1306 Assert.AreEqual ("dtest-app, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", aname.ToString ());
1308 ModuleMirror m = frame.Method.DeclaringType.Module;
1310 Assert.AreEqual ("dtest-app.exe", m.Name);
1311 Assert.AreEqual ("dtest-app.exe", m.ScopeName);
1312 Assert.IsTrue (m.FullyQualifiedName.IndexOf ("dtest-app.exe") != -1);
1313 Guid guid = m.ModuleVersionId;
1314 Assert.AreEqual (frame.Method.DeclaringType.Assembly, m.Assembly);
1315 Assert.AreEqual (frame.Method.DeclaringType.Assembly.ManifestModule, m);
1317 Assert.AreEqual ("Assembly", frame.Method.DeclaringType.Assembly.GetAssemblyObject ().Type.Name);
1319 TypeMirror t = vm.RootDomain.Corlib.GetType ("System.Diagnostics.DebuggerDisplayAttribute");
1320 Assert.AreEqual ("DebuggerDisplayAttribute", t.Name);
1324 public void LocalsInfo () {
1325 Event e = run_until ("locals2");
1327 StackFrame frame = e.Thread.GetFrames () [0];
1329 var locals = frame.Method.GetLocals ();
1330 Assert.AreEqual (6, locals.Length);
1331 for (int i = 0; i < 6; ++i) {
1332 if (locals [i].Name == "args") {
1333 Assert.IsTrue (locals [i].IsArg);
1334 Assert.AreEqual ("String[]", locals [i].Type.Name);
1335 } else if (locals [i].Name == "arg") {
1336 Assert.IsTrue (locals [i].IsArg);
1337 Assert.AreEqual ("Int32", locals [i].Type.Name);
1338 } else if (locals [i].Name == "i") {
1339 Assert.IsFalse (locals [i].IsArg);
1340 Assert.AreEqual ("Int64", locals [i].Type.Name);
1341 } else if (locals [i].Name == "j") {
1342 Assert.IsFalse (locals [i].IsArg);
1343 Assert.AreEqual ("Int32", locals [i].Type.Name);
1344 } else if (locals [i].Name == "s") {
1345 Assert.IsFalse (locals [i].IsArg);
1346 Assert.AreEqual ("String", locals [i].Type.Name);
1347 } else if (locals [i].Name == "t") {
1349 Assert.IsTrue (locals [i].IsArg);
1350 Assert.AreEqual ("String", locals [i].Type.Name);
1359 public void Locals () {
1360 var be = run_until ("locals1");
1362 StackFrame frame = be.Thread.GetFrames () [0];
1364 MethodMirror m1 = frame.Method;
1366 be = run_until ("locals2");
1368 frame = be.Thread.GetFrames () [0];
1370 object val = frame.GetValue (frame.Method.GetLocal ("i"));
1371 AssertValue (0, val);
1374 var req = vm.CreateStepRequest (be.Thread);
1378 var e = vm.GetNextEvent ();
1379 Assert.IsTrue (e is StepEvent);
1380 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1382 // Execute s = "AB";
1384 e = vm.GetNextEvent ();
1385 Assert.IsTrue (e is StepEvent);
1386 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1388 frame = e.Thread.GetFrames () [0];
1390 val = frame.GetValue (frame.Method.GetLocal ("i"));
1391 AssertValue (42, val);
1393 LocalVariable[] locals = frame.Method.GetLocals ();
1394 var vals = frame.GetValues (locals);
1395 Assert.AreEqual (locals.Length, vals.Length);
1396 for (int i = 0; i < locals.Length; ++i) {
1397 if (locals [i].Name == "i")
1398 AssertValue (42, vals [i]);
1399 if (locals [i].Name == "s")
1400 AssertValue ("AB", vals [i]);
1401 if (locals [i].Name == "t")
1402 AssertValue ("ABC", vals [i]);
1405 // Argument checking
1408 AssertThrows<ArgumentNullException> (delegate () {
1409 frame.GetValue ((LocalVariable)null);
1411 // GetValue () local from another method
1412 AssertThrows<ArgumentException> (delegate () {
1413 frame.GetValue (m1.GetLocal ("foo"));
1417 AssertThrows<ArgumentNullException> (delegate () {
1418 frame.GetValue ((ParameterInfoMirror)null);
1420 // GetValue () local from another method
1421 AssertThrows<ArgumentException> (delegate () {
1422 frame.GetValue (m1.GetParameters ()[0]);
1425 // GetValues () null
1426 AssertThrows<ArgumentNullException> (delegate () {
1427 frame.GetValues (null);
1429 // GetValues () embedded null
1430 AssertThrows<ArgumentNullException> (delegate () {
1431 frame.GetValues (new LocalVariable [] { null });
1433 // GetValues () local from another method
1434 AssertThrows<ArgumentException> (delegate () {
1435 frame.GetValues (new LocalVariable [] { m1.GetLocal ("foo") });
1438 AssertThrows<ArgumentException> (delegate () {
1439 val = frame.GetValue (frame.Method.ReturnParameter);
1442 // invalid stack frames
1444 e = vm.GetNextEvent ();
1445 Assert.IsTrue (e is StepEvent);
1446 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1448 AssertThrows<InvalidStackFrameException> (delegate () {
1449 frame.GetValue (frame.Method.GetLocal ("i"));
1456 public void GetVisibleVariables () {
1457 Event e = run_until ("locals4");
1460 var locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
1461 Assert.AreEqual (2, locals.Count);
1462 var loc = locals.First (l => l.Name == "i");
1463 Assert.AreEqual ("Int64", loc.Type.Name);
1464 loc = locals.First (l => l.Name == "s");
1465 Assert.AreEqual ("String", loc.Type.Name);
1467 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
1468 Assert.AreEqual ("i", loc.Name);
1469 Assert.AreEqual ("Int64", loc.Type.Name);
1471 e = run_until ("locals5");
1474 locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
1475 Assert.AreEqual (2, locals.Count);
1476 loc = locals.First (l => l.Name == "i");
1477 Assert.AreEqual ("String", loc.Type.Name);
1478 loc = locals.First (l => l.Name == "s");
1479 Assert.AreEqual ("String", loc.Type.Name);
1481 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
1482 Assert.AreEqual ("i", loc.Name);
1483 Assert.AreEqual ("String", loc.Type.Name);
1485 // Variable in another scope
1486 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("j");
1487 Assert.IsNull (loc);
1491 public void Exit () {
1496 var e = vm.GetNextEvent ();
1497 Assert.IsInstanceOfType (typeof (VMDeathEvent), e);
1500 /* Could be a remote vm with no process */
1503 Assert.AreEqual (5, p.ExitCode);
1506 AssertThrows<VMDisconnectedException> (delegate () {
1515 public void Dispose () {
1520 var e = vm.GetNextEvent ();
1521 Assert.IsInstanceOfType (typeof (VMDisconnectEvent), e);
1524 /* Could be a remote vm with no process */
1527 Assert.AreEqual (3, p.ExitCode);
1530 AssertThrows<VMDisconnectedException> (delegate () {
1539 public void LineNumbers () {
1540 Event e = run_until ("line_numbers");
1542 step_req = vm.CreateStepRequest (e.Thread);
1543 step_req.Depth = StepDepth.Into;
1550 e = vm.GetNextEvent ();
1551 Assert.IsTrue (e is StepEvent);
1553 l = e.Thread.GetFrames ()[0].Location;
1555 Assert.IsTrue (l.SourceFile.IndexOf ("dtest-app.cs") != -1);
1556 Assert.AreEqual ("ln1", l.Method.Name);
1558 int line_base = l.LineNumber;
1561 e = vm.GetNextEvent ();
1562 Assert.IsTrue (e is StepEvent);
1563 l = e.Thread.GetFrames ()[0].Location;
1564 Assert.AreEqual ("ln2", l.Method.Name);
1565 Assert.AreEqual (line_base + 6, l.LineNumber);
1568 e = vm.GetNextEvent ();
1569 Assert.IsTrue (e is StepEvent);
1570 l = e.Thread.GetFrames ()[0].Location;
1571 Assert.AreEqual ("ln1", l.Method.Name);
1572 Assert.AreEqual (line_base + 1, l.LineNumber);
1575 e = vm.GetNextEvent ();
1576 Assert.IsTrue (e is StepEvent);
1577 l = e.Thread.GetFrames ()[0].Location;
1578 Assert.AreEqual ("ln3", l.Method.Name);
1579 Assert.AreEqual (line_base + 10, l.LineNumber);
1582 e = vm.GetNextEvent ();
1583 Assert.IsTrue (e is StepEvent);
1584 l = e.Thread.GetFrames ()[0].Location;
1585 Assert.AreEqual ("ln1", l.Method.Name);
1586 Assert.AreEqual (line_base + 2, l.LineNumber);
1588 // GetSourceFiles ()
1589 string[] sources = l.Method.DeclaringType.GetSourceFiles ();
1590 Assert.AreEqual (1, sources.Length);
1591 Assert.AreEqual ("dtest-app.cs", sources [0]);
1593 sources = l.Method.DeclaringType.GetSourceFiles (true);
1594 Assert.AreEqual (1, sources.Length);
1595 Assert.IsTrue (sources [0].EndsWith ("dtest-app.cs"));
1599 public void Suspend () {
1602 Start (new string [] { "dtest-app.exe", "suspend-test" });
1604 Event e = run_until ("suspend");
1606 ThreadMirror main = e.Thread;
1614 // The debuggee should be suspended while it is running the infinite loop
1616 StackFrame frame = main.GetFrames ()[0];
1617 Assert.AreEqual ("suspend", frame.Method.Name);
1621 // resuming when not suspended
1622 AssertThrows<InvalidOperationException> (delegate () {
1632 public void AssemblyLoad () {
1633 Event e = run_until ("assembly_load");
1635 var load_req = vm.CreateAssemblyLoadRequest ();
1640 e = vm.GetNextEvent ();
1641 Assert.IsInstanceOfType (typeof (AssemblyLoadEvent), e);
1642 Assert.IsTrue ((e as AssemblyLoadEvent).Assembly.Location.EndsWith ("System.dll"));
1644 var frames = e.Thread.GetFrames ();
1645 Assert.IsTrue (frames.Length > 0);
1646 Assert.AreEqual ("assembly_load", frames [0].Method.Name);
1650 public void CreateValue () {
1653 v = vm.CreateValue (1);
1654 Assert.AreEqual (vm, v.VirtualMachine);
1655 Assert.AreEqual (1, v.Value);
1657 v = vm.CreateValue (null);
1658 Assert.AreEqual (vm, v.VirtualMachine);
1659 Assert.AreEqual (null, v.Value);
1661 // Argument checking
1662 AssertThrows <ArgumentException> (delegate () {
1663 v = vm.CreateValue ("FOO");
1668 public void CreateString () {
1669 StringMirror s = vm.RootDomain.CreateString ("ABC");
1671 Assert.AreEqual (vm, s.VirtualMachine);
1672 Assert.AreEqual ("ABC", s.Value);
1673 Assert.AreEqual (vm.RootDomain, s.Domain);
1676 StringBuilder sb = new StringBuilder ();
1677 for (int i = 0; i < 1024; ++i)
1679 s = vm.RootDomain.CreateString (sb.ToString ());
1681 // Argument checking
1682 AssertThrows <ArgumentNullException> (delegate () {
1683 s = vm.RootDomain.CreateString (null);
1688 public void CreateBoxedValue () {
1689 ObjectMirror o = vm.RootDomain.CreateBoxedValue (new PrimitiveValue (vm, 42));
1691 Assert.AreEqual ("Int32", o.Type.Name);
1692 //AssertValue (42, m.GetValue (o.Type.GetField ("m_value")));
1694 // Argument checking
1695 AssertThrows <ArgumentNullException> (delegate () {
1696 vm.RootDomain.CreateBoxedValue (null);
1699 AssertThrows <ArgumentException> (delegate () {
1700 vm.RootDomain.CreateBoxedValue (o);
1705 public void Invoke () {
1706 Event e = run_until ("invoke1");
1708 StackFrame frame = e.Thread.GetFrames () [0];
1710 TypeMirror t = frame.Method.DeclaringType;
1711 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
1713 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
1719 m = t.GetMethod ("invoke_return_void");
1720 v = this_obj.InvokeMethod (e.Thread, m, null);
1724 m = t.GetMethod ("invoke_return_ref");
1725 v = this_obj.InvokeMethod (e.Thread, m, null);
1726 AssertValue ("ABC", v);
1729 m = t.GetMethod ("invoke_return_null");
1730 v = this_obj.InvokeMethod (e.Thread, m, null);
1731 AssertValue (null, v);
1734 m = t.GetMethod ("invoke_return_primitive");
1735 v = this_obj.InvokeMethod (e.Thread, m, null);
1736 AssertValue (42, v);
1739 m = t.GetMethod ("invoke_pass_primitive");
1740 Value[] args = new Value [] {
1741 vm.CreateValue ((byte)Byte.MaxValue),
1742 vm.CreateValue ((sbyte)SByte.MaxValue),
1743 vm.CreateValue ((short)1),
1744 vm.CreateValue ((ushort)1),
1745 vm.CreateValue ((int)1),
1746 vm.CreateValue ((uint)1),
1747 vm.CreateValue ((long)1),
1748 vm.CreateValue ((ulong)1),
1749 vm.CreateValue ('A'),
1750 vm.CreateValue (true),
1751 vm.CreateValue (3.14f),
1752 vm.CreateValue (3.14) };
1754 v = this_obj.InvokeMethod (e.Thread, m, args);
1755 AssertValue ((int)Byte.MaxValue + (int)SByte.MaxValue + 1 + 1 + 1 + 1 + 1 + 1 + 'A' + 1 + 3 + 3, v);
1758 m = t.GetMethod ("invoke_pass_ref");
1759 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
1760 AssertValue ("ABC", v);
1763 m = t.GetMethod ("invoke_pass_ref");
1764 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (null) });
1765 AssertValue (null, v);
1768 m = t.GetMethod ("invoke_static_pass_ref");
1769 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
1770 AssertValue ("ABC", v);
1772 // static invoked using ObjectMirror.InvokeMethod
1773 m = t.GetMethod ("invoke_static_pass_ref");
1774 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
1775 AssertValue ("ABC", v);
1777 // method which throws an exception
1779 m = t.GetMethod ("invoke_throws");
1780 v = this_obj.InvokeMethod (e.Thread, m, null);
1782 } catch (InvocationException ex) {
1783 Assert.AreEqual ("Exception", ex.Exception.Type.Name);
1787 m = t.GetMethod (".ctor");
1788 v = t.InvokeMethod (e.Thread, m, null);
1789 Assert.IsInstanceOfType (typeof (ObjectMirror), v);
1790 Assert.AreEqual ("Tests", (v as ObjectMirror).Type.Name);
1792 // Argument checking
1795 AssertThrows<ArgumentNullException> (delegate {
1796 m = t.GetMethod ("invoke_pass_ref");
1797 v = this_obj.InvokeMethod (null, m, new Value [] { vm.CreateValue (null) });
1801 AssertThrows<ArgumentNullException> (delegate {
1802 v = this_obj.InvokeMethod (e.Thread, null, new Value [] { vm.CreateValue (null) });
1805 // invalid number of arguments
1806 m = t.GetMethod ("invoke_pass_ref");
1807 AssertThrows<ArgumentException> (delegate {
1808 v = this_obj.InvokeMethod (e.Thread, m, null);
1811 // invalid type of argument (ref != primitive)
1812 m = t.GetMethod ("invoke_pass_ref");
1813 AssertThrows<ArgumentException> (delegate {
1814 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
1817 // invalid type of argument (primitive != primitive)
1818 m = t.GetMethod ("invoke_pass_primitive_2");
1819 AssertThrows<ArgumentException> (delegate {
1820 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
1823 // invoking a non-static method as static
1824 m = t.GetMethod ("invoke_pass_ref");
1825 AssertThrows<ArgumentException> (delegate {
1826 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
1829 // invoking a method defined in another class
1830 m = t2.GetMethod ("invoke");
1831 AssertThrows<ArgumentException> (delegate {
1832 v = this_obj.InvokeMethod (e.Thread, m, null);
1837 public void InvokeVType () {
1838 Event e = run_until ("invoke1");
1840 StackFrame frame = e.Thread.GetFrames () [0];
1842 var s = frame.GetArgument (1) as StructMirror;
1844 TypeMirror t = s.Type;
1849 // Pass struct as this, receive int
1850 m = t.GetMethod ("invoke_return_int");
1851 v = s.InvokeMethod (e.Thread, m, null);
1852 AssertValue (42, v);
1854 // Pass struct as this, receive intptr
1855 m = t.GetMethod ("invoke_return_intptr");
1856 v = s.InvokeMethod (e.Thread, m, null);
1857 AssertValue (43, v);
1860 m = t.GetMethod ("invoke_static");
1861 v = t.InvokeMethod (e.Thread, m, null);
1864 // Pass generic struct as this
1865 s = frame.GetArgument (2) as StructMirror;
1867 m = t.GetMethod ("invoke_return_int");
1868 v = s.InvokeMethod (e.Thread, m, null);
1869 AssertValue (42, v);
1873 public void BreakpointDuringInvoke () {
1874 Event e = run_until ("invoke1");
1876 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke2");
1877 Assert.IsNotNull (m);
1878 vm.SetBreakpoint (m, 0);
1880 StackFrame frame = e.Thread.GetFrames () [0];
1881 var o = frame.GetThis () as ObjectMirror;
1883 bool failed = false;
1885 bool finished = false;
1886 object wait = new object ();
1888 // Have to invoke in a separate thread as the invoke is suspended until we
1889 // resume after the breakpoint
1890 Thread t = new Thread (delegate () {
1892 o.InvokeMethod (e.Thread, m, null);
1898 Monitor.Pulse (wait);
1904 StackFrame invoke_frame = null;
1907 e = vm.GetNextEvent ();
1908 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
1909 // Check stack trace support and invokes
1910 var frames = e.Thread.GetFrames ();
1911 invoke_frame = frames [0];
1912 Assert.AreEqual ("invoke2", frames [0].Method.Name);
1913 Assert.IsTrue (frames [0].IsDebuggerInvoke);
1914 Assert.AreEqual ("invoke1", frames [1].Method.Name);
1919 // Check that the invoke frames are no longer valid
1920 AssertThrows<InvalidStackFrameException> (delegate {
1921 invoke_frame.GetThis ();
1926 Monitor.Wait (wait);
1929 // Check InvokeOptions.DisableBreakpoints flag
1930 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
1934 public void DisabledExceptionDuringInvoke () {
1935 Event e = run_until ("invoke_ex");
1937 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_ex_inner");
1939 StackFrame frame = e.Thread.GetFrames () [0];
1940 var o = frame.GetThis () as ObjectMirror;
1942 var req = vm.CreateExceptionRequest (null);
1945 // Check InvokeOptions.DisableBreakpoints flag
1946 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
1952 public void InvokeSingleThreaded () {
1955 Start (new string [] { "dtest-app.exe", "invoke-single-threaded" });
1957 Event e = run_until ("invoke_single_threaded_2");
1959 StackFrame f = e.Thread.GetFrames ()[0];
1961 var obj = f.GetThis () as ObjectMirror;
1963 // Check that the counter value incremented by the other thread does not increase
1964 // during the invoke.
1965 object counter1 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
1967 var m = obj.Type.GetMethod ("invoke_return_void");
1968 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
1970 object counter2 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
1972 Assert.AreEqual ((int)counter1, (int)counter2);
1974 // Test multiple invokes done in succession
1975 m = obj.Type.GetMethod ("invoke_return_void");
1976 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
1978 // Test events during single-threaded invokes
1979 vm.EnableEvents (EventType.TypeLoad);
1980 m = obj.Type.GetMethod ("invoke_type_load");
1981 obj.BeginInvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded, delegate {
1985 e = vm.GetNextEvent ();
1986 Assert.AreEqual (EventType.TypeLoad, e.EventType);
1990 public void GetThreads () {
1995 public void Threads () {
1996 Event e = run_until ("threads");
1998 Assert.AreEqual (ThreadState.Running, e.Thread.ThreadState);
2000 Assert.IsTrue (e.Thread.ThreadId > 0);
2002 vm.EnableEvents (EventType.ThreadStart, EventType.ThreadDeath);
2006 e = vm.GetNextEvent ();
2007 Assert.IsInstanceOfType (typeof (ThreadStartEvent), e);
2008 var state = e.Thread.ThreadState;
2009 Assert.IsTrue (state == ThreadState.Running || state == ThreadState.Unstarted);
2013 e = vm.GetNextEvent ();
2014 Assert.IsInstanceOfType (typeof (ThreadDeathEvent), e);
2015 Assert.AreEqual (ThreadState.Stopped, e.Thread.ThreadState);
2020 public void Frame_SetValue () {
2021 Event e = run_until ("locals2");
2023 StackFrame frame = e.Thread.GetFrames () [0];
2026 var l = frame.Method.GetLocal ("i");
2027 frame.SetValue (l, vm.CreateValue ((long)55));
2028 AssertValue (55, frame.GetValue (l));
2031 l = frame.Method.GetLocal ("s");
2032 frame.SetValue (l, vm.RootDomain.CreateString ("DEF"));
2033 AssertValue ("DEF", frame.GetValue (l));
2035 // argument as local
2036 l = frame.Method.GetLocal ("arg");
2037 frame.SetValue (l, vm.CreateValue (6));
2038 AssertValue (6, frame.GetValue (l));
2041 var p = frame.Method.GetParameters ()[1];
2042 frame.SetValue (p, vm.CreateValue (7));
2043 AssertValue (7, frame.GetValue (p));
2046 p = frame.Method.GetParameters ()[2];
2047 frame.SetValue (p, vm.RootDomain.CreateString ("DEF"));
2048 AssertValue ("DEF", frame.GetValue (p));
2050 // argument checking
2053 AssertThrows<ArgumentNullException> (delegate () {
2054 frame.SetValue ((LocalVariable)null, vm.CreateValue (55));
2058 AssertThrows<ArgumentNullException> (delegate () {
2059 l = frame.Method.GetLocal ("i");
2060 frame.SetValue (l, null);
2063 // value of invalid type
2064 AssertThrows<ArgumentException> (delegate () {
2065 l = frame.Method.GetLocal ("i");
2066 frame.SetValue (l, vm.CreateValue (55));
2071 public void InvokeRegress () {
2072 Event e = run_until ("invoke1");
2074 StackFrame frame = e.Thread.GetFrames () [0];
2076 TypeMirror t = frame.Method.DeclaringType;
2077 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2079 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2085 m = t.GetMethod ("invoke_return_void");
2086 v = this_obj.InvokeMethod (e.Thread, m, null);
2089 // Check that the stack frames remain valid during the invoke
2090 Assert.AreEqual ("Tests", (frame.GetThis () as ObjectMirror).Type.Name);
2092 // do another invoke
2093 m = t.GetMethod ("invoke_return_void");
2094 v = this_obj.InvokeMethod (e.Thread, m, null);
2097 // Try a single step after the invoke
2098 var req = vm.CreateStepRequest (e.Thread);
2099 req.Depth = StepDepth.Into;
2100 req.Size = StepSize.Line;
2105 // Step into invoke2
2107 e = vm.GetNextEvent ();
2108 Assert.IsTrue (e is StepEvent);
2109 Assert.AreEqual ("invoke2", (e as StepEvent).Method.Name);
2113 frame = e.Thread.GetFrames () [0];
2117 public void Exceptions () {
2118 Event e = run_until ("exceptions");
2119 var req = vm.CreateExceptionRequest (null);
2124 e = vm.GetNextEvent ();
2125 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2126 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2128 var frames = e.Thread.GetFrames ();
2129 Assert.AreEqual ("exceptions", frames [0].Method.Name);
2132 // exception type filter
2134 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.ArgumentException"));
2137 // Skip the throwing of the second OverflowException
2140 e = vm.GetNextEvent ();
2141 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2142 Assert.AreEqual ("ArgumentException", (e as ExceptionEvent).Exception.Type.Name);
2145 // exception type filter for subclasses
2146 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.Exception"));
2151 e = vm.GetNextEvent ();
2152 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2153 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2156 // Implicit exceptions
2157 req = vm.CreateExceptionRequest (null);
2162 e = vm.GetNextEvent ();
2163 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2164 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
2167 // Single stepping after an exception
2168 req = vm.CreateExceptionRequest (null);
2173 e = vm.GetNextEvent ();
2174 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2175 Assert.AreEqual ("Exception", (e as ExceptionEvent).Exception.Type.Name);
2176 frames = e.Thread.GetFrames ();
2177 Assert.AreEqual ("exceptions2", frames [0].Method.Name);
2180 var sreq = vm.CreateStepRequest (e.Thread);
2181 sreq.Depth = StepDepth.Over;
2182 sreq.Size = StepSize.Line;
2186 e = vm.GetNextEvent ();
2187 Assert.IsInstanceOfType (typeof (StepEvent), e);
2188 frames = e.Thread.GetFrames ();
2189 Assert.AreEqual ("exceptions", frames [0].Method.Name);
2192 // Argument checking
2193 AssertThrows<ArgumentException> (delegate {
2194 vm.CreateExceptionRequest (e.Thread.Type);
2199 public void ExceptionFilter () {
2200 Event e = run_until ("exception_filter");
2202 MethodMirror m = entry_point.DeclaringType.GetMethod ("exception_filter_filter");
2203 Assert.IsNotNull (m);
2205 vm.SetBreakpoint (m, 0);
2209 e = vm.GetNextEvent ();
2210 Assert.AreEqual (EventType.Breakpoint, e.EventType);
2211 Assert.IsTrue (e is BreakpointEvent);
2212 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
2214 var frames = e.Thread.GetFrames ();
2216 Assert.IsTrue (frames [0].Location.SourceFile.IndexOf ("dtest-app.cs") != -1);
2217 Assert.AreEqual ("exception_filter_filter", frames [0].Location.Method.Name);
2219 Assert.AreEqual (0, frames [1].Location.Method.MetadataToken);
2220 Assert.AreEqual (0x0f, frames [1].Location.ILOffset);
2222 Assert.AreEqual ("exception_filter_method", frames [2].Location.Method.Name);
2223 Assert.AreEqual (0x05, frames [2].Location.ILOffset);
2225 Assert.AreEqual (0, frames [3].Location.Method.MetadataToken, 0);
2226 Assert.AreEqual (0, frames [3].Location.ILOffset);
2228 Assert.AreEqual ("exception_filter", frames [4].Location.Method.Name);
2232 public void ExceptionFilter2 () {
2235 Start (new string [] { "dtest-excfilter.exe" });
2237 MethodMirror filter_method = entry_point.DeclaringType.GetMethod ("Filter");
2238 Assert.IsNotNull (filter_method);
2240 MethodMirror test_method = entry_point.DeclaringType.GetMethod ("Test");
2241 Assert.IsNotNull (test_method);
2243 vm.SetBreakpoint (filter_method, 0);
2247 var e = vm.GetNextEvent ();
2248 Assert.AreEqual (EventType.Breakpoint, e.EventType);
2249 Assert.IsTrue (e is BreakpointEvent);
2250 Assert.AreEqual (filter_method.Name, (e as BreakpointEvent).Method.Name);
2252 var frames = e.Thread.GetFrames ();
2254 Assert.AreEqual (4, frames.Count ());
2256 Assert.AreEqual (filter_method.Name, frames [0].Location.Method.Name);
2257 Assert.AreEqual (20, frames [0].Location.LineNumber);
2258 Assert.AreEqual (0, frames [0].Location.ILOffset);
2260 Assert.AreEqual (test_method.Name, frames [1].Location.Method.Name);
2261 Assert.AreEqual (37, frames [1].Location.LineNumber);
2262 Assert.AreEqual (0x0b, frames [1].Location.ILOffset);
2264 Assert.AreEqual (test_method.Name, frames [2].Location.Method.Name);
2265 Assert.AreEqual (33, frames [2].Location.LineNumber);
2266 Assert.AreEqual (0x05, frames [2].Location.ILOffset);
2268 Assert.AreEqual (entry_point.Name, frames [3].Location.Method.Name);
2269 Assert.AreEqual (14, frames [3].Location.LineNumber);
2270 Assert.AreEqual (0x00, frames [3].Location.ILOffset);
2278 public void EventSets () {
2280 // Create two filter which both match the same exception
2282 Event e = run_until ("exceptions");
2284 var req = vm.CreateExceptionRequest (null);
2287 var req2 = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.OverflowException"));
2292 var es = vm.GetNextEventSet ();
2293 Assert.AreEqual (2, es.Events.Length);
2296 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2297 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2300 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2301 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2308 // Test single threaded invokes during processing of nullref exceptions.
2309 // These won't work if the exception handling is done from the sigsegv signal
2310 // handler, since the sigsegv signal is disabled until control returns from the
2314 [Category ("only3")]
2315 public void NullRefExceptionAndSingleThreadedInvoke () {
2316 Event e = run_until ("exceptions");
2317 var req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.NullReferenceException"));
2322 e = vm.GetNextEvent ();
2323 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2324 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
2326 var ex = (e as ExceptionEvent).Exception;
2327 var tostring_method = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2328 ex.InvokeMethod (e.Thread, tostring_method, null, InvokeOptions.SingleThreaded);
2332 public void Domains () {
2335 Start (new string [] { "dtest-app.exe", "domain-test" });
2337 vm.EnableEvents (EventType.AppDomainCreate, EventType.AppDomainUnload, EventType.AssemblyUnload);
2339 Event e = run_until ("domains");
2343 e = vm.GetNextEvent ();
2344 Assert.IsInstanceOfType (typeof (AppDomainCreateEvent), e);
2346 var domain = (e as AppDomainCreateEvent).Domain;
2348 // Run until the callback in the domain
2349 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_in_domain");
2350 Assert.IsNotNull (m);
2351 vm.SetBreakpoint (m, 0);
2355 e = vm.GetNextEvent ();
2356 if (e is BreakpointEvent)
2360 Assert.AreEqual ("invoke_in_domain", (e as BreakpointEvent).Method.Name);
2362 // d_method is from another domain
2363 MethodMirror d_method = (e as BreakpointEvent).Method;
2364 Assert.IsTrue (m != d_method);
2366 var frames = e.Thread.GetFrames ();
2367 Assert.AreEqual ("invoke_in_domain", frames [0].Method.Name);
2368 Assert.AreEqual ("invoke", frames [1].Method.Name);
2369 Assert.AreEqual ("domains", frames [2].Method.Name);
2371 // Test breakpoints on already JITted methods in other domains
2372 m = entry_point.DeclaringType.GetMethod ("invoke_in_domain_2");
2373 Assert.IsNotNull (m);
2374 vm.SetBreakpoint (m, 0);
2378 e = vm.GetNextEvent ();
2379 if (e is BreakpointEvent)
2383 Assert.AreEqual ("invoke_in_domain_2", (e as BreakpointEvent).Method.Name);
2385 // This is empty when receiving the AppDomainCreateEvent
2386 Assert.AreEqual ("domain", domain.FriendlyName);
2388 // Run until the unload
2391 e = vm.GetNextEvent ();
2392 if (e is AssemblyUnloadEvent) {
2398 Assert.IsInstanceOfType (typeof (AppDomainUnloadEvent), e);
2399 Assert.AreEqual (domain, (e as AppDomainUnloadEvent).Domain);
2401 // Run past the unload
2402 e = run_until ("domains_2");
2404 // Test access to unloaded types
2405 // FIXME: Add an exception type for this
2406 AssertThrows<Exception> (delegate {
2407 d_method.DeclaringType.GetValue (d_method.DeclaringType.GetField ("static_i"));
2412 public void DynamicMethods () {
2413 Event e = run_until ("dyn_call");
2415 var m = e.Thread.GetFrames ()[1].Method;
2416 Assert.AreEqual ("dyn_method", m.Name);
2418 // Test access to IL
2419 var body = m.GetMethodBody ();
2421 ILInstruction ins = body.Instructions [0];
2422 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
2423 Assert.AreEqual ("FOO", ins.Operand);
2427 public void RefEmit () {
2430 Start (new string [] { "dtest-app.exe", "ref-emit-test" });
2432 Event e = run_until ("ref_emit_call");
2434 var m = e.Thread.GetFrames ()[1].Method;
2435 Assert.AreEqual ("ref_emit_method", m.Name);
2437 // Test access to IL
2438 var body = m.GetMethodBody ();
2442 ins = body.Instructions [0];
2443 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
2444 Assert.AreEqual ("FOO", ins.Operand);
2446 ins = body.Instructions [1];
2447 Assert.AreEqual (OpCodes.Call, ins.OpCode);
2448 Assert.IsInstanceOfType (typeof (MethodMirror), ins.Operand);
2449 Assert.AreEqual ("ref_emit_call", (ins.Operand as MethodMirror).Name);
2453 public void IsAttached () {
2454 var f = entry_point.DeclaringType.GetField ("is_attached");
2456 Event e = run_until ("Main");
2458 AssertValue (true, entry_point.DeclaringType.GetValue (f));
2462 public void StackTraceInNative () {
2463 // Check that stack traces can be produced for threads in native code
2466 Start (new string [] { "dtest-app.exe", "frames-in-native" });
2468 var e = run_until ("frames_in_native");
2470 // FIXME: This is racy
2477 StackFrame[] frames = e.Thread.GetFrames ();
2479 int frame_index = -1;
2480 for (int i = 0; i < frames.Length; ++i) {
2481 if (frames [i].Method.Name == "Sleep") {
2487 Assert.IsTrue (frame_index != -1);
2488 Assert.AreEqual ("Sleep", frames [frame_index].Method.Name);
2489 Assert.AreEqual ("frames_in_native", frames [frame_index + 1].Method.Name);
2490 Assert.AreEqual ("Main", frames [frame_index + 2].Method.Name);
2492 // Check that invokes are disabled for such threads
2493 TypeMirror t = frames [frame_index + 1].Method.DeclaringType;
2496 var m = t.GetMethod ("invoke_static_return_void");
2497 AssertThrows<InvalidOperationException> (delegate {
2498 t.InvokeMethod (e.Thread, m, null);
2503 public void VirtualMachine_CreateEnumMirror () {
2504 var e = run_until ("o1");
2505 var frame = e.Thread.GetFrames () [0];
2507 object val = frame.GetThis ();
2508 Assert.IsTrue (val is ObjectMirror);
2509 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
2510 ObjectMirror o = (val as ObjectMirror);
2512 FieldInfoMirror field = o.Type.GetField ("field_enum");
2513 Value f = o.GetValue (field);
2514 TypeMirror enumType = (f as EnumMirror).Type;
2516 o.SetValue (field, vm.CreateEnumMirror (enumType, vm.CreateValue (1)));
2517 f = o.GetValue (field);
2518 Assert.AreEqual (1, (f as EnumMirror).Value);
2520 // Argument checking
2521 AssertThrows<ArgumentNullException> (delegate () {
2522 vm.CreateEnumMirror (enumType, null);
2525 AssertThrows<ArgumentNullException> (delegate () {
2526 vm.CreateEnumMirror (null, vm.CreateValue (1));
2530 AssertThrows<ArgumentException> (delegate () {
2531 vm.CreateEnumMirror (enumType, vm.CreateValue (null));
2534 // value of a wrong type
2535 AssertThrows<ArgumentException> (delegate () {
2536 vm.CreateEnumMirror (enumType, vm.CreateValue ((long)1));
2541 public void VirtualMachine_EnableEvents_Breakpoint () {
2542 AssertThrows<ArgumentException> (delegate () {
2543 vm.EnableEvents (EventType.Breakpoint);
2548 public void SingleStepRegress654694 () {
2551 MethodMirror m = entry_point.DeclaringType.GetMethod ("ss_regress_654694");
2552 foreach (Location l in m.Locations) {
2553 if (l.ILOffset > 0 && il_offset == -1)
2554 il_offset = l.ILOffset;
2557 Event e = run_until ("ss_regress_654694");
2559 Assert.IsNotNull (m);
2560 vm.SetBreakpoint (m, il_offset);
2564 e = vm.GetNextEvent ();
2565 Assert.IsTrue (e is BreakpointEvent);
2567 var req = vm.CreateStepRequest (e.Thread);
2568 req.Depth = StepDepth.Over;
2569 req.Size = StepSize.Line;
2574 e = vm.GetNextEvent ();
2575 Assert.IsTrue (e is StepEvent);