2 using System.Collections.Generic;
3 using System.Threading;
5 using System.Reflection;
7 using Mono.Debugger.Soft;
8 using Diag = System.Diagnostics;
11 using NUnit.Framework;
13 #pragma warning disable 0219
16 public class DebuggerTests
19 MethodMirror entry_point;
20 StepEventRequest step_req;
22 void AssertThrows<ExType> (Action del) where ExType : Exception {
30 Assert.IsTrue (thrown);
33 // No other way to pass arguments to the tests ?
34 public static bool listening = Environment.GetEnvironmentVariable ("DBG_SUSPEND") != null;
35 public static string runtime = Environment.GetEnvironmentVariable ("DBG_RUNTIME");
36 public static string agent_args = Environment.GetEnvironmentVariable ("DBG_AGENT_ARGS");
38 void Start (string[] args) {
40 var pi = new Diag.ProcessStartInfo ();
43 pi.FileName = runtime;
46 pi.Arguments = String.Join (" ", args);
47 vm = VirtualMachineManager.Launch (pi, new LaunchOptions { AgentArgs = agent_args });
49 Console.WriteLine ("Listening...");
50 vm = VirtualMachineManager.Listen (new IPEndPoint (IPAddress.Any, 10000));
53 vm.EnableEvents (EventType.AssemblyLoad);
55 Event vmstart = vm.GetNextEvent ();
56 Assert.AreEqual (EventType.VMStart, vmstart.EventType);
65 /* Find out the entry point */
67 e = vm.GetNextEvent ();
69 if (e is AssemblyLoadEvent) {
70 AssemblyLoadEvent ae = (AssemblyLoadEvent)e;
71 entry_point = ae.Assembly.EntryPoint;
72 if (entry_point != null)
80 BreakpointEvent run_until (string name) {
82 MethodMirror m = entry_point.DeclaringType.GetMethod (name);
84 vm.SetBreakpoint (m, 0);
90 e = vm.GetNextEvent ();
91 if (e is BreakpointEvent)
95 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
96 Assert.AreEqual (m, (e as BreakpointEvent).Method);
98 return (e as BreakpointEvent);
101 Event single_step (ThreadMirror t) {
102 var req = vm.CreateStepRequest (t);
106 Event e = vm.GetNextEvent ();
107 Assert.IsTrue (e is StepEvent);
114 void check_arg_val (StackFrame frame, int pos, Type type, object eval) {
115 object val = frame.GetArgument (pos);
116 Assert.IsTrue (val is PrimitiveValue);
117 object v = (val as PrimitiveValue).Value;
118 Assert.AreEqual (type, v.GetType ());
120 Assert.IsTrue (Math.Abs ((float)eval - (float)v) < 0.0001);
121 else if (eval is double)
122 Assert.IsTrue (Math.Abs ((double)eval - (double)v) < 0.0001);
124 Assert.AreEqual (eval, v);
127 void AssertValue (object expected, object val) {
128 if (expected is string) {
129 Assert.IsTrue (val is StringMirror);
130 Assert.AreEqual (expected, (val as StringMirror).Value);
131 } else if (val is StructMirror && (val as StructMirror).Type.Name == "IntPtr") {
132 AssertValue (expected, (val as StructMirror).Fields [0]);
134 Assert.IsTrue (val is PrimitiveValue);
135 Assert.AreEqual (expected, (val as PrimitiveValue).Value);
140 public void SetUp () {
141 Start (new string [] { "dtest-app.exe" });
145 public void TearDown () {
149 if (step_req != null)
154 Event e = vm.GetNextEvent ();
156 if (e is VMDeathEvent)
164 public void SimpleBreakpoint () {
167 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp1");
168 Assert.IsNotNull (m);
170 vm.SetBreakpoint (m, 0);
174 e = vm.GetNextEvent ();
175 Assert.AreEqual (EventType.Breakpoint, e.EventType);
176 Assert.IsTrue (e is BreakpointEvent);
177 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
180 AssertThrows<ArgumentException> (delegate {
182 vm.SetBreakpoint (m, 1);
187 public void BreakpointsSameLocation () {
190 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp2");
191 Assert.IsNotNull (m);
193 vm.SetBreakpoint (m, 0);
194 vm.SetBreakpoint (m, 0);
198 e = vm.GetNextEvent ();
199 Assert.IsTrue (e is BreakpointEvent);
200 Assert.AreEqual (m, (e as BreakpointEvent).Method);
202 e = vm.GetNextEvent ();
203 Assert.IsTrue (e is BreakpointEvent);
204 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
208 public void BreakpointAlreadyJITted () {
209 Event e = run_until ("bp1");
211 /* Place a breakpoint on bp3 */
212 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp3");
213 Assert.IsNotNull (m);
214 vm.SetBreakpoint (m, 0);
216 /* Same with generic instances */
217 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp7");
218 Assert.IsNotNull (m2);
219 vm.SetBreakpoint (m2, 0);
223 e = vm.GetNextEvent ();
224 Assert.AreEqual (EventType.Breakpoint, e.EventType);
225 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
229 /* Non-shared instance */
230 e = vm.GetNextEvent ();
231 Assert.AreEqual (EventType.Breakpoint, e.EventType);
232 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
236 /* Shared instance */
237 e = vm.GetNextEvent ();
238 Assert.AreEqual (EventType.Breakpoint, e.EventType);
239 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
243 public void ClearBreakpoint () {
246 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
247 Assert.IsNotNull (m);
248 EventRequest req1 = vm.SetBreakpoint (m, 0);
249 EventRequest req2 = vm.SetBreakpoint (m, 0);
251 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
252 Assert.IsNotNull (m2);
253 vm.SetBreakpoint (m2, 0);
258 e = vm.GetNextEvent ();
259 Assert.AreEqual (EventType.Breakpoint, e.EventType);
260 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
261 e = vm.GetNextEvent ();
262 Assert.AreEqual (EventType.Breakpoint, e.EventType);
263 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
265 /* Clear one of them */
270 e = vm.GetNextEvent ();
271 Assert.AreEqual (EventType.Breakpoint, e.EventType);
272 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
274 /* Clear the other */
279 e = vm.GetNextEvent ();
280 Assert.AreEqual (EventType.Breakpoint, e.EventType);
281 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
285 public void ClearAllBreakpoints () {
288 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
289 Assert.IsNotNull (m);
290 vm.SetBreakpoint (m, 0);
292 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
293 Assert.IsNotNull (m2);
294 vm.SetBreakpoint (m2, 0);
296 vm.ClearAllBreakpoints ();
300 e = vm.GetNextEvent ();
301 Assert.IsTrue (!(e is BreakpointEvent));
302 if (e is VMDeathEvent)
307 public void BreakpointOnGShared () {
310 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp6");
311 Assert.IsNotNull (m);
313 vm.SetBreakpoint (m, 0);
317 e = vm.GetNextEvent ();
318 Assert.AreEqual (EventType.Breakpoint, e.EventType);
319 Assert.IsTrue (e is BreakpointEvent);
320 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
324 public void SingleStepping () {
325 Event e = run_until ("single_stepping");
327 var req = vm.CreateStepRequest (e.Thread);
334 e = vm.GetNextEvent ();
335 Assert.IsTrue (e is StepEvent);
336 Assert.AreEqual ("ss1", (e as StepEvent).Method.Name);
340 e = vm.GetNextEvent ();
341 Assert.IsTrue (e is StepEvent);
342 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
344 // Change to step over
346 req.Depth = StepDepth.Over;
351 e = vm.GetNextEvent ();
352 Assert.IsTrue (e is StepEvent);
353 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
355 // Change to step into
357 req.Depth = StepDepth.Into;
362 e = vm.GetNextEvent ();
363 Assert.IsTrue (e is StepEvent);
364 Assert.AreEqual ("ss3", (e as StepEvent).Method.Name);
366 // Change to step out
368 req.Depth = StepDepth.Out;
371 // Step back into single_stepping
373 e = vm.GetNextEvent ();
374 Assert.IsTrue (e is StepEvent);
375 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
377 // Change to step into
379 req.Depth = StepDepth.Into;
382 // Step into ss3_2 ()
384 e = vm.GetNextEvent ();
385 Assert.IsTrue (e is StepEvent);
386 Assert.AreEqual ("ss3_2", (e as StepEvent).Method.Name);
388 // Change to step over
390 req.Depth = StepDepth.Over;
393 // Step over ss3_2_2 ()
395 e = vm.GetNextEvent ();
396 Assert.IsTrue (e is StepEvent);
397 Assert.AreEqual ("ss3_2", (e as StepEvent).Method.Name);
399 // Recreate the request
403 // Step back into single_stepping () with the new request
405 e = vm.GetNextEvent ();
406 Assert.IsTrue (e is StepEvent);
407 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
409 // Change to step into
411 req.Depth = StepDepth.Into;
416 e = vm.GetNextEvent ();
417 Assert.IsTrue (e is StepEvent);
418 Assert.AreEqual ("ss4", (e as StepEvent).Method.Name);
420 // Change to StepSize.Line
422 req.Depth = StepDepth.Over;
423 req.Size = StepSize.Line;
426 // Step over ss1 (); ss1 ();
428 e = vm.GetNextEvent ();
429 Assert.IsTrue (e is StepEvent);
433 req.Depth = StepDepth.Into;
437 e = vm.GetNextEvent ();
438 Assert.IsTrue (e is StepEvent);
439 Assert.AreEqual ("ss2", (e as StepEvent).Method.Name);
444 e = run_until ("ss5");
446 // Add an assembly filter
447 req.AssemblyFilter = new AssemblyMirror [] { (e as BreakpointEvent).Method.DeclaringType.Assembly };
450 // Step into is_even, skipping the linq stuff
452 e = vm.GetNextEvent ();
453 Assert.IsTrue (e is StepEvent);
454 Assert.AreEqual ("is_even", (e as StepEvent).Method.Name);
456 // FIXME: Check that single stepping works with lock (obj)
461 e = run_until ("ss6");
463 req = vm.CreateStepRequest (e.Thread);
464 req.Depth = StepDepth.Over;
467 // Check that single stepping works in out-of-line bblocks
469 e = vm.GetNextEvent ();
470 Assert.IsTrue (e is StepEvent);
472 e = vm.GetNextEvent ();
473 Assert.IsTrue (e is StepEvent);
474 Assert.AreEqual ("ss6", (e as StepEvent).Method.Name);
480 public void MethodEntryExit () {
481 run_until ("single_stepping");
483 var req1 = vm.CreateMethodEntryRequest ();
484 var req2 = vm.CreateMethodExitRequest ();
490 Event e = vm.GetNextEvent ();
491 Assert.IsTrue (e is MethodEntryEvent);
492 Assert.AreEqual ("ss1", (e as MethodEntryEvent).Method.Name);
495 e = vm.GetNextEvent ();
496 Assert.IsTrue (e is MethodExitEvent);
497 Assert.AreEqual ("ss1", (e as MethodExitEvent).Method.Name);
504 public void CountFilter () {
505 run_until ("single_stepping");
507 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("ss3");
508 Assert.IsNotNull (m2);
509 vm.SetBreakpoint (m2, 0);
511 var req1 = vm.CreateMethodEntryRequest ();
515 // Enter ss2, ss1 is skipped
517 Event e = vm.GetNextEvent ();
518 Assert.IsTrue (e is MethodEntryEvent);
519 Assert.AreEqual ("ss2", (e as MethodEntryEvent).Method.Name);
521 // Breakpoint on ss3, the entry event is no longer reported
523 e = vm.GetNextEvent ();
524 Assert.IsTrue (e is BreakpointEvent);
530 public void Arguments () {
533 var e = run_until ("arg1");
535 StackFrame frame = e.Thread.GetFrames () [0];
537 check_arg_val (frame, 0, typeof (sbyte), SByte.MaxValue - 5);
538 check_arg_val (frame, 1, typeof (byte), Byte.MaxValue - 5);
539 check_arg_val (frame, 2, typeof (bool), true);
540 check_arg_val (frame, 3, typeof (short), Int16.MaxValue - 5);
541 check_arg_val (frame, 4, typeof (ushort), UInt16.MaxValue - 5);
542 check_arg_val (frame, 5, typeof (char), 'F');
543 check_arg_val (frame, 6, typeof (int), Int32.MaxValue - 5);
544 check_arg_val (frame, 7, typeof (uint), UInt32.MaxValue - 5);
545 check_arg_val (frame, 8, typeof (long), Int64.MaxValue - 5);
546 check_arg_val (frame, 9, typeof (ulong), UInt64.MaxValue - 5);
547 check_arg_val (frame, 10, typeof (float), 1.2345f);
548 check_arg_val (frame, 11, typeof (double), 6.78910);
550 e = run_until ("arg2");
552 frame = e.Thread.GetFrames () [0];
555 val = frame.GetArgument (0);
556 AssertValue ("FOO", val);
557 Assert.AreEqual ("String", (val as ObjectMirror).Type.Name);
560 val = frame.GetArgument (1);
561 AssertValue (null, val);
564 val = frame.GetArgument (2);
565 AssertValue ("BLA", val);
568 val = frame.GetArgument (3);
569 AssertValue (42, val);
572 val = frame.GetArgument (4);
573 Assert.IsTrue (val is ObjectMirror);
574 Assert.AreEqual ("GClass`1", (val as ObjectMirror).Type.Name);
577 val = frame.GetArgument (5);
578 Assert.IsTrue (val is ObjectMirror);
579 Assert.AreEqual ("Object", (val as ObjectMirror).Type.Name);
581 // this on static methods
582 val = frame.GetThis ();
583 AssertValue (null, val);
585 e = run_until ("arg3");
587 frame = e.Thread.GetFrames () [0];
590 val = frame.GetThis ();
591 Assert.IsTrue (val is ObjectMirror);
592 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
594 // objref in register
595 val = frame.GetArgument (0);
596 AssertValue ("BLA", val);
600 public void Arrays () {
603 var e = run_until ("o2");
605 StackFrame frame = e.Thread.GetFrames () [0];
608 val = frame.GetArgument (0);
609 Assert.IsTrue (val is ArrayMirror);
610 ArrayMirror arr = val as ArrayMirror;
611 Assert.AreEqual (2, arr.Length);
612 AssertValue ("BAR", arr [0]);
613 AssertValue ("BAZ", arr [1]);
615 var vals = arr.GetValues (0, 2);
616 Assert.AreEqual (2, vals.Count);
617 AssertValue ("BAR", vals [0]);
618 AssertValue ("BAZ", vals [1]);
620 arr [0] = vm.RootDomain.CreateString ("ABC");
621 AssertValue ("ABC", arr [0]);
623 arr [0] = vm.CreateValue (null);
624 AssertValue (null, arr [0]);
626 arr.SetValues (0, new Value [] { vm.RootDomain.CreateString ("D1"), vm.RootDomain.CreateString ("D2") });
627 AssertValue ("D1", arr [0]);
628 AssertValue ("D2", arr [1]);
631 val = frame.GetArgument (1);
632 Assert.IsTrue (val is ArrayMirror);
633 arr = val as ArrayMirror;
634 Assert.AreEqual (2, arr.Length);
635 AssertValue (42, arr [0]);
636 AssertValue (43, arr [1]);
639 AssertThrows<IndexOutOfRangeException> (delegate () {
643 AssertThrows<IndexOutOfRangeException> (delegate () {
644 val = arr [Int32.MinValue];
647 AssertThrows<IndexOutOfRangeException> (delegate () {
648 vals = arr.GetValues (0, 3);
651 AssertThrows<IndexOutOfRangeException> (delegate () {
652 arr [2] = vm.CreateValue (null);
655 AssertThrows<IndexOutOfRangeException> (delegate () {
656 arr [Int32.MinValue] = vm.CreateValue (null);
659 AssertThrows<IndexOutOfRangeException> (delegate () {
660 arr.SetValues (0, new Value [] { null, null, null });
664 val = frame.GetArgument (2);
665 Assert.IsTrue (val is ArrayMirror);
666 arr = val as ArrayMirror;
667 Assert.AreEqual (2, arr.Rank);
668 Assert.AreEqual (4, arr.Length);
669 Assert.AreEqual (2, arr.GetLength (0));
670 Assert.AreEqual (2, arr.GetLength (1));
671 Assert.AreEqual (0, arr.GetLowerBound (0));
672 Assert.AreEqual (0, arr.GetLowerBound (1));
673 vals = arr.GetValues (0, 4);
674 AssertValue (1, vals [0]);
675 AssertValue (2, vals [1]);
676 AssertValue (3, vals [2]);
677 AssertValue (4, vals [3]);
679 val = frame.GetArgument (3);
680 Assert.IsTrue (val is ArrayMirror);
681 arr = val as ArrayMirror;
682 Assert.AreEqual (2, arr.Rank);
683 Assert.AreEqual (4, arr.Length);
684 Assert.AreEqual (2, arr.GetLength (0));
685 Assert.AreEqual (2, arr.GetLength (1));
686 Assert.AreEqual (1, arr.GetLowerBound (0));
687 Assert.AreEqual (3, arr.GetLowerBound (1));
689 AssertThrows<ArgumentOutOfRangeException> (delegate () {
692 AssertThrows<ArgumentOutOfRangeException> (delegate () {
696 AssertThrows<ArgumentOutOfRangeException> (delegate () {
697 arr.GetLowerBound (-1);
699 AssertThrows<ArgumentOutOfRangeException> (delegate () {
700 arr.GetLowerBound (2);
703 // arrays treated as generic collections
704 val = frame.GetArgument (4);
705 Assert.IsTrue (val is ArrayMirror);
706 arr = val as ArrayMirror;
710 public void Object_GetValue () {
711 var e = run_until ("o1");
712 var frame = e.Thread.GetFrames () [0];
714 object val = frame.GetThis ();
715 Assert.IsTrue (val is ObjectMirror);
716 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
717 ObjectMirror o = (val as ObjectMirror);
719 TypeMirror t = o.Type;
722 object f = o.GetValue (t.GetField ("field_i"));
724 f = o.GetValue (t.GetField ("field_s"));
725 AssertValue ("S", f);
726 f = o.GetValue (t.GetField ("field_enum"));
727 Assert.IsTrue (f is EnumMirror);
728 Assert.AreEqual (1, (f as EnumMirror).Value);
729 Assert.AreEqual ("B", (f as EnumMirror).StringValue);
731 // Inherited object fields
732 TypeMirror parent = t.BaseType;
733 f = o.GetValue (parent.GetField ("base_field_i"));
735 f = o.GetValue (parent.GetField ("base_field_s"));
736 AssertValue ("T", f);
739 f = o.GetValue (o.Type.GetField ("static_i"));
743 ObjectMirror o2 = frame.GetValue (frame.Method.GetParameters ()[1]) as ObjectMirror;
744 Assert.AreEqual ("GClass`1", o2.Type.Name);
745 TypeMirror t2 = o2.Type;
746 f = o2.GetValue (t2.GetField ("field"));
749 ObjectMirror o3 = frame.GetValue (frame.Method.GetParameters ()[2]) as ObjectMirror;
750 Assert.AreEqual ("GClass`1", o3.Type.Name);
751 TypeMirror t3 = o3.Type;
752 f = o3.GetValue (t3.GetField ("field"));
753 AssertValue ("FOO", f);
756 AssertThrows<ArgumentNullException> (delegate () {
762 public void Object_GetValues () {
763 var e = run_until ("o1");
764 var frame = e.Thread.GetFrames () [0];
766 object val = frame.GetThis ();
767 Assert.IsTrue (val is ObjectMirror);
768 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
769 ObjectMirror o = (val as ObjectMirror);
771 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
773 TypeMirror t = o.Type;
775 object[] vals = o.GetValues (new FieldInfoMirror [] { t.GetField ("field_i"), t.GetField ("field_s") });
779 AssertValue ("S", f);
782 AssertThrows<ArgumentNullException> (delegate () {
786 AssertThrows<ArgumentNullException> (delegate () {
787 o.GetValues (new FieldInfoMirror [] { null });
790 // field of another class
791 AssertThrows<ArgumentException> (delegate () {
792 o.GetValue (val2.Type.GetField ("field_j"));
796 void TestSetValue (ObjectMirror o, string field_name, object val) {
798 o.SetValue (o.Type.GetField (field_name), vm.RootDomain.CreateString ((string)val));
800 o.SetValue (o.Type.GetField (field_name), vm.CreateValue (val));
801 Value f = o.GetValue (o.Type.GetField (field_name));
802 AssertValue (val, f);
806 public void Object_SetValues () {
807 var e = run_until ("o1");
808 var frame = e.Thread.GetFrames () [0];
810 object val = frame.GetThis ();
811 Assert.IsTrue (val is ObjectMirror);
812 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
813 ObjectMirror o = (val as ObjectMirror);
815 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
817 TestSetValue (o, "field_i", 22);
818 TestSetValue (o, "field_bool1", false);
819 TestSetValue (o, "field_bool2", true);
820 TestSetValue (o, "field_char", 'B');
821 TestSetValue (o, "field_byte", (byte)129);
822 TestSetValue (o, "field_sbyte", (sbyte)-33);
823 TestSetValue (o, "field_short", (short)(Int16.MaxValue - 5));
824 TestSetValue (o, "field_ushort", (ushort)(UInt16.MaxValue - 5));
825 TestSetValue (o, "field_long", Int64.MaxValue - 5);
826 TestSetValue (o, "field_ulong", (ulong)(UInt64.MaxValue - 5));
827 TestSetValue (o, "field_float", 6.28f);
828 TestSetValue (o, "field_double", 6.28);
829 TestSetValue (o, "static_i", 23);
830 TestSetValue (o, "field_s", "CDEF");
835 f = o.GetValue (o.Type.GetField ("field_intptr"));
836 Assert.IsInstanceOfType (typeof (StructMirror), f);
837 AssertValue (Int32.MaxValue - 5, (f as StructMirror).Fields [0]);
841 f = o.GetValue (o.Type.GetField ("field_enum"));
842 (f as EnumMirror).Value = 5;
843 o.SetValue (o.Type.GetField ("field_enum"), f);
844 f = o.GetValue (o.Type.GetField ("field_enum"));
845 Assert.AreEqual (5, (f as EnumMirror).Value);
848 o.SetValue (o.Type.GetField ("field_s"), vm.CreateValue (null));
849 f = o.GetValue (o.Type.GetField ("field_s"));
850 AssertValue (null, f);
853 AssertThrows<ArgumentNullException> (delegate () {
854 o.SetValues (null, new Value [0]);
857 AssertThrows<ArgumentNullException> (delegate () {
858 o.SetValues (new FieldInfoMirror [0], null);
861 AssertThrows<ArgumentNullException> (delegate () {
862 o.SetValues (new FieldInfoMirror [] { null }, new Value [1] { null });
865 // vtype with a wrong type
866 AssertThrows<ArgumentException> (delegate () {
867 o.SetValue (o.Type.GetField ("field_struct"), o.GetValue (o.Type.GetField ("field_enum")));
870 // reference type not assignment compatible
871 AssertThrows<ArgumentException> (delegate () {
872 o.SetValue (o.Type.GetField ("field_class"), o);
875 // field of another class
876 AssertThrows<ArgumentException> (delegate () {
877 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
882 public void Type_SetValue () {
883 var e = run_until ("o1");
884 var frame = e.Thread.GetFrames () [0];
887 object val = frame.GetThis ();
888 Assert.IsTrue (val is ObjectMirror);
889 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
890 ObjectMirror o = (val as ObjectMirror);
892 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
894 o.Type.SetValue (o.Type.GetField ("static_i"), vm.CreateValue (55));
895 f = o.Type.GetValue (o.Type.GetField ("static_i"));
899 AssertThrows<ArgumentNullException> (delegate () {
900 o.Type.SetValue (null, vm.CreateValue (0));
903 AssertThrows<ArgumentNullException> (delegate () {
904 o.Type.SetValue (o.Type.GetField ("static_i"), null);
907 // field of another class
908 AssertThrows<ArgumentException> (delegate () {
909 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
914 public void TypeInfo () {
915 Event e = run_until ("ti2");
916 StackFrame frame = e.Thread.GetFrames () [0];
921 t = frame.Method.GetParameters ()[0].ParameterType;
923 Assert.AreEqual ("String[]", t.Name);
924 Assert.AreEqual ("string[]", t.CSharpName);
925 Assert.AreEqual ("Array", t.BaseType.Name);
926 Assert.AreEqual (true, t.HasElementType);
927 Assert.AreEqual (true, t.IsArray);
928 Assert.AreEqual (1, t.GetArrayRank ());
929 Assert.AreEqual ("String", t.GetElementType ().Name);
931 t = frame.Method.GetParameters ()[2].ParameterType;
933 Assert.AreEqual ("Int32[,]", t.Name);
935 //Assert.AreEqual ("int[,]", t.CSharpName);
936 Assert.AreEqual ("Array", t.BaseType.Name);
937 Assert.AreEqual (true, t.HasElementType);
938 Assert.AreEqual (true, t.IsArray);
939 Assert.AreEqual (2, t.GetArrayRank ());
940 Assert.AreEqual ("Int32", t.GetElementType ().Name);
943 t = frame.Method.GetParameters ()[3].ParameterType;
945 //Assert.AreEqual ("Int32&", t.Name);
946 //Assert.AreEqual (true, t.IsByRef);
947 //Assert.AreEqual (true, t.HasElementType);
950 t = frame.Method.GetParameters ()[4].ParameterType;
952 //Assert.AreEqual ("Int32*", t.Name);
953 Assert.AreEqual (true, t.IsPointer);
954 Assert.AreEqual (true, t.HasElementType);
955 Assert.AreEqual ("Int32", t.GetElementType ().Name);
956 Assert.AreEqual (false, t.IsPrimitive);
959 t = frame.Method.GetParameters ()[5].ParameterType;
960 Assert.AreEqual (true, t.IsPrimitive);
963 t = frame.Method.GetParameters ()[6].ParameterType;
964 Assert.AreEqual ("AStruct", t.Name);
965 Assert.AreEqual (false, t.IsPrimitive);
966 Assert.AreEqual (true, t.IsValueType);
967 Assert.AreEqual (false, t.IsClass);
970 t = frame.Method.GetParameters ()[7].ParameterType;
971 Assert.AreEqual ("Tests", t.Name);
972 var nested = (from nt in t.GetNestedTypes () where nt.IsNestedPublic select nt).ToArray ();
973 Assert.AreEqual (1, nested.Length);
974 Assert.AreEqual ("NestedClass", nested [0].Name);
975 Assert.IsTrue (t.BaseType.IsAssignableFrom (t));
976 Assert.IsTrue (!t.IsAssignableFrom (t.BaseType));
979 t = frame.Method.GetParameters ()[9].ParameterType;
980 Assert.AreEqual ("GClass`1", t.Name);
983 t = frame.Method.GetParameters ()[7].ParameterType;
985 var props = t.GetProperties ();
986 Assert.AreEqual (3, props.Length);
987 foreach (PropertyInfoMirror prop in props) {
988 ParameterInfoMirror[] indexes = prop.GetIndexParameters ();
990 if (prop.Name == "IntProperty") {
991 Assert.AreEqual ("Int32", prop.PropertyType.Name);
992 Assert.AreEqual ("get_IntProperty", prop.GetGetMethod ().Name);
993 Assert.AreEqual ("set_IntProperty", prop.GetSetMethod ().Name);
994 Assert.AreEqual (0, indexes.Length);
995 } else if (prop.Name == "ReadOnlyProperty") {
996 Assert.AreEqual ("Int32", prop.PropertyType.Name);
997 Assert.AreEqual ("get_ReadOnlyProperty", prop.GetGetMethod ().Name);
998 Assert.AreEqual (null, prop.GetSetMethod ());
999 Assert.AreEqual (0, indexes.Length);
1000 } else if (prop.Name == "IndexedProperty") {
1001 Assert.AreEqual (1, indexes.Length);
1002 Assert.AreEqual ("Int32", indexes [0].ParameterType.Name);
1006 // custom attributes
1007 t = frame.Method.GetParameters ()[8].ParameterType;
1008 Assert.AreEqual ("Tests2", t.Name);
1009 var attrs = t.GetCustomAttributes (true);
1010 Assert.AreEqual (2, attrs.Length);
1011 foreach (var attr in attrs) {
1012 if (attr.Constructor.DeclaringType.Name == "DebuggerDisplayAttribute") {
1013 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1014 Assert.AreEqual ("Tests", attr.ConstructorArguments [0].Value);
1015 Assert.AreEqual (2, attr.NamedArguments.Count);
1016 Assert.AreEqual ("Name", attr.NamedArguments [0].Property.Name);
1017 Assert.AreEqual ("FOO", attr.NamedArguments [0].TypedValue.Value);
1018 Assert.AreEqual ("Target", attr.NamedArguments [1].Property.Name);
1019 Assert.IsInstanceOfType (typeof (TypeMirror), attr.NamedArguments [1].TypedValue.Value);
1020 Assert.AreEqual ("Int32", (attr.NamedArguments [1].TypedValue.Value as TypeMirror).Name);
1021 } else if (attr.Constructor.DeclaringType.Name == "DebuggerTypeProxyAttribute") {
1022 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1023 Assert.IsInstanceOfType (typeof (TypeMirror), attr.ConstructorArguments [0].Value);
1024 Assert.AreEqual ("Tests", (attr.ConstructorArguments [0].Value as TypeMirror).Name);
1026 Assert.Fail (attr.Constructor.DeclaringType.Name);
1032 public void FieldInfo () {
1033 Event e = run_until ("ti2");
1034 StackFrame frame = e.Thread.GetFrames () [0];
1038 t = frame.Method.GetParameters ()[8].ParameterType;
1039 Assert.AreEqual ("Tests2", t.Name);
1041 var fi = t.GetField ("field_j");
1042 var attrs = fi.GetCustomAttributes (true);
1043 Assert.AreEqual (1, attrs.Length);
1044 var attr = attrs [0];
1045 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1046 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1047 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1048 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1052 public void PropertyInfo () {
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 pi = t.GetProperty ("AProperty");
1062 var attrs = pi.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 Type_GetValue () {
1073 Event e = run_until ("o1");
1074 StackFrame frame = e.Thread.GetFrames () [0];
1076 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1078 TypeMirror t = o.Type;
1080 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1083 object f = t.GetValue (o.Type.GetField ("static_i"));
1084 AssertValue (55, f);
1086 f = t.GetValue (o.Type.GetField ("static_s"));
1087 AssertValue ("A", f);
1089 // literal static fields
1090 f = t.GetValue (o.Type.GetField ("literal_i"));
1091 AssertValue (56, f);
1093 f = t.GetValue (o.Type.GetField ("literal_s"));
1094 AssertValue ("B", f);
1096 // Inherited static fields
1097 TypeMirror parent = t.BaseType;
1098 f = t.GetValue (parent.GetField ("base_static_i"));
1099 AssertValue (57, f);
1101 f = t.GetValue (parent.GetField ("base_static_s"));
1102 AssertValue ("C", f);
1104 // Argument checking
1105 AssertThrows<ArgumentNullException> (delegate () {
1110 AssertThrows<ArgumentException> (delegate () {
1111 t.GetValue (o.Type.GetField ("field_i"));
1114 // field on another type
1115 AssertThrows<ArgumentException> (delegate () {
1116 t.GetValue (val2.Type.GetField ("static_field_j"));
1119 // special static field
1120 AssertThrows<ArgumentException> (delegate () {
1121 t.GetValue (t.GetField ("tls_i"));
1126 public void Type_GetValues () {
1127 Event e = run_until ("o1");
1128 StackFrame frame = e.Thread.GetFrames () [0];
1130 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1132 TypeMirror t = o.Type;
1135 object[] vals = t.GetValues (new FieldInfoMirror [] { t.GetField ("static_i"), t.GetField ("static_s") });
1136 object f = vals [0];
1137 AssertValue (55, f);
1140 AssertValue ("A", f);
1142 // Argument checking
1143 AssertThrows<ArgumentNullException> (delegate () {
1147 AssertThrows<ArgumentNullException> (delegate () {
1148 t.GetValues (new FieldInfoMirror [] { null });
1153 public void ObjRefs () {
1154 Event e = run_until ("objrefs1");
1155 StackFrame frame = e.Thread.GetFrames () [0];
1157 ObjectMirror o = frame.GetThis () as ObjectMirror;
1158 ObjectMirror child = o.GetValue (o.Type.GetField ("child")) as ObjectMirror;
1160 Assert.IsTrue (child.Address > 0);
1162 // Check that object references are internalized correctly
1163 Assert.AreEqual (o, frame.GetThis ());
1165 run_until ("objrefs2");
1167 // child should be gc'd now
1168 Assert.IsTrue (child.IsCollected);
1170 AssertThrows<ObjectCollectedException> (delegate () {
1171 TypeMirror t = child.Type;
1174 AssertThrows<ObjectCollectedException> (delegate () {
1175 long addr = child.Address;
1180 public void Type_GetObject () {
1181 Event e = run_until ("o1");
1182 StackFrame frame = e.Thread.GetFrames () [0];
1184 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1186 TypeMirror t = o.Type;
1188 Assert.AreEqual ("MonoType", t.GetTypeObject ().Type.Name);
1192 public void VTypes () {
1193 Event e = run_until ("vtypes1");
1194 StackFrame frame = e.Thread.GetFrames () [0];
1197 ObjectMirror o = frame.GetThis () as ObjectMirror;
1198 var obj = o.GetValue (o.Type.GetField ("field_struct"));
1199 Assert.IsTrue (obj is StructMirror);
1200 var s = obj as StructMirror;
1201 Assert.AreEqual ("AStruct", s.Type.Name);
1202 AssertValue (42, s ["i"]);
1204 AssertValue ("S", obj);
1205 AssertValue (43, s ["k"]);
1206 obj = o.GetValue (o.Type.GetField ("field_boxed_struct"));
1207 Assert.IsTrue (obj is StructMirror);
1208 s = obj as StructMirror;
1209 Assert.AreEqual ("AStruct", s.Type.Name);
1210 AssertValue (42, s ["i"]);
1212 // vtypes as arguments
1213 s = frame.GetArgument (0) as StructMirror;
1214 AssertValue (44, s ["i"]);
1216 AssertValue ("T", obj);
1217 AssertValue (45, s ["k"]);
1219 // vtypes as array entries
1220 var arr = frame.GetArgument (1) as ArrayMirror;
1222 Assert.IsTrue (obj is StructMirror);
1223 s = obj as StructMirror;
1224 AssertValue (1, s ["i"]);
1225 AssertValue ("S1", s ["s"]);
1227 Assert.IsTrue (obj is StructMirror);
1228 s = obj as StructMirror;
1229 AssertValue (2, s ["i"]);
1230 AssertValue ("S2", s ["s"]);
1232 // Argument checking
1233 s = frame.GetArgument (0) as StructMirror;
1234 AssertThrows<ArgumentException> (delegate () {
1238 // generic vtype instances
1239 o = frame.GetThis () as ObjectMirror;
1240 obj = o.GetValue (o.Type.GetField ("generic_field_struct"));
1241 Assert.IsTrue (obj is StructMirror);
1242 s = obj as StructMirror;
1243 Assert.AreEqual ("GStruct`1", s.Type.Name);
1244 AssertValue (42, s ["i"]);
1246 // this on vtype methods
1247 e = run_until ("vtypes2");
1249 e = single_step (e.Thread);
1251 frame = e.Thread.GetFrames () [0];
1253 Assert.AreEqual ("foo", (e as StepEvent).Method.Name);
1254 obj = frame.GetThis ();
1256 Assert.IsTrue (obj is StructMirror);
1257 s = obj as StructMirror;
1258 AssertValue (44, s ["i"]);
1259 AssertValue ("T", s ["s"]);
1260 AssertValue (45, s ["k"]);
1262 // this on static vtype methods
1263 e = run_until ("vtypes3");
1265 e = single_step (e.Thread);
1267 frame = e.Thread.GetFrames () [0];
1269 Assert.AreEqual ("static_foo", (e as StepEvent).Method.Name);
1270 obj = frame.GetThis ();
1271 AssertValue (null, obj);
1275 public void AssemblyInfo () {
1276 Event e = run_until ("single_stepping");
1278 StackFrame frame = e.Thread.GetFrames () [0];
1280 var aname = frame.Method.DeclaringType.Assembly.GetName ();
1281 Assert.AreEqual ("dtest-app, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", aname.ToString ());
1283 ModuleMirror m = frame.Method.DeclaringType.Module;
1285 Assert.AreEqual ("dtest-app.exe", m.Name);
1286 Assert.AreEqual ("dtest-app.exe", m.ScopeName);
1287 Assert.IsTrue (m.FullyQualifiedName.IndexOf ("dtest-app.exe") != -1);
1288 Guid guid = m.ModuleVersionId;
1289 Assert.AreEqual (frame.Method.DeclaringType.Assembly, m.Assembly);
1290 Assert.AreEqual (frame.Method.DeclaringType.Assembly.ManifestModule, m);
1292 Assert.AreEqual ("Assembly", frame.Method.DeclaringType.Assembly.GetAssemblyObject ().Type.Name);
1294 TypeMirror t = vm.RootDomain.Corlib.GetType ("System.Diagnostics.DebuggerDisplayAttribute", false, false);
1295 Assert.AreEqual ("DebuggerDisplayAttribute", t.Name);
1299 public void LocalsInfo () {
1300 Event e = run_until ("locals2");
1302 StackFrame frame = e.Thread.GetFrames () [0];
1304 var locals = frame.Method.GetLocals ();
1305 Assert.AreEqual (5, locals.Length);
1306 for (int i = 0; i < 5; ++i) {
1307 if (locals [i].Name == "args") {
1308 Assert.IsTrue (locals [i].IsArg);
1309 Assert.AreEqual ("String[]", locals [i].Type.Name);
1310 } else if (locals [i].Name == "arg") {
1311 Assert.IsTrue (locals [i].IsArg);
1312 Assert.AreEqual ("Int32", locals [i].Type.Name);
1313 } else if (locals [i].Name == "i") {
1314 Assert.IsFalse (locals [i].IsArg);
1315 Assert.AreEqual ("Int64", locals [i].Type.Name);
1316 } else if (locals [i].Name == "j") {
1317 Assert.IsFalse (locals [i].IsArg);
1318 Assert.AreEqual ("Int32", locals [i].Type.Name);
1319 } else if (locals [i].Name == "s") {
1320 Assert.IsFalse (locals [i].IsArg);
1321 Assert.AreEqual ("String", locals [i].Type.Name);
1329 public void Locals () {
1330 var be = run_until ("locals1");
1332 StackFrame frame = be.Thread.GetFrames () [0];
1334 MethodMirror m1 = frame.Method;
1336 be = run_until ("locals2");
1338 frame = be.Thread.GetFrames () [0];
1340 object val = frame.GetValue (frame.Method.GetLocal ("i"));
1341 AssertValue (0, val);
1344 var req = vm.CreateStepRequest (be.Thread);
1348 var e = vm.GetNextEvent ();
1349 Assert.IsTrue (e is StepEvent);
1350 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1352 // Execute s = "AB";
1354 e = vm.GetNextEvent ();
1355 Assert.IsTrue (e is StepEvent);
1356 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1358 frame = e.Thread.GetFrames () [0];
1360 val = frame.GetValue (frame.Method.GetLocal ("i"));
1361 AssertValue (42, val);
1363 LocalVariable[] locals = frame.Method.GetLocals ();
1364 var vals = frame.GetValues (locals);
1365 Assert.AreEqual (locals.Length, vals.Length);
1366 for (int i = 0; i < locals.Length; ++i) {
1367 if (locals [i].Name == "i")
1368 AssertValue (42, vals [i]);
1369 if (locals [i].Name == "s")
1370 AssertValue ("AB", vals [i]);
1373 // Argument checking
1376 AssertThrows<ArgumentNullException> (delegate () {
1377 frame.GetValue ((LocalVariable)null);
1379 // GetValue () local from another method
1380 AssertThrows<ArgumentException> (delegate () {
1381 frame.GetValue (m1.GetLocal ("foo"));
1385 AssertThrows<ArgumentNullException> (delegate () {
1386 frame.GetValue ((ParameterInfoMirror)null);
1388 // GetValue () local from another method
1389 AssertThrows<ArgumentException> (delegate () {
1390 frame.GetValue (m1.GetParameters ()[0]);
1393 // GetValues () null
1394 AssertThrows<ArgumentNullException> (delegate () {
1395 frame.GetValues (null);
1397 // GetValues () embedded null
1398 AssertThrows<ArgumentNullException> (delegate () {
1399 frame.GetValues (new LocalVariable [] { null });
1401 // GetValues () local from another method
1402 AssertThrows<ArgumentException> (delegate () {
1403 frame.GetValues (new LocalVariable [] { m1.GetLocal ("foo") });
1406 AssertThrows<ArgumentException> (delegate () {
1407 val = frame.GetValue (frame.Method.ReturnParameter);
1410 // invalid stack frames
1412 e = vm.GetNextEvent ();
1413 Assert.IsTrue (e is StepEvent);
1414 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1416 AssertThrows<InvalidStackFrameException> (delegate () {
1417 frame.GetValue (frame.Method.GetLocal ("i"));
1424 public void Exit () {
1429 var e = vm.GetNextEvent ();
1430 Assert.IsInstanceOfType (typeof (VMDeathEvent), e);
1433 /* Could be a remote vm with no process */
1436 Assert.AreEqual (5, p.ExitCode);
1439 AssertThrows<VMDisconnectedException> (delegate () {
1448 public void Dispose () {
1453 var e = vm.GetNextEvent ();
1454 Assert.IsInstanceOfType (typeof (VMDisconnectEvent), e);
1457 /* Could be a remote vm with no process */
1460 Assert.AreEqual (3, p.ExitCode);
1463 AssertThrows<VMDisconnectedException> (delegate () {
1472 public void LineNumbers () {
1473 Event e = run_until ("line_numbers");
1475 step_req = vm.CreateStepRequest (e.Thread);
1476 step_req.Depth = StepDepth.Into;
1483 e = vm.GetNextEvent ();
1484 Assert.IsTrue (e is StepEvent);
1486 l = e.Thread.GetFrames ()[0].Location;
1488 Assert.IsTrue (l.SourceFile.IndexOf ("dtest-app.cs") != -1);
1489 Assert.AreEqual ("ln1", l.Method.Name);
1491 int line_base = l.LineNumber;
1494 e = vm.GetNextEvent ();
1495 Assert.IsTrue (e is StepEvent);
1496 l = e.Thread.GetFrames ()[0].Location;
1497 Assert.AreEqual ("ln2", l.Method.Name);
1498 Assert.AreEqual (line_base + 6, l.LineNumber);
1501 e = vm.GetNextEvent ();
1502 Assert.IsTrue (e is StepEvent);
1503 l = e.Thread.GetFrames ()[0].Location;
1504 Assert.AreEqual ("ln1", l.Method.Name);
1505 Assert.AreEqual (line_base + 1, l.LineNumber);
1508 e = vm.GetNextEvent ();
1509 Assert.IsTrue (e is StepEvent);
1510 l = e.Thread.GetFrames ()[0].Location;
1511 Assert.AreEqual ("ln3", l.Method.Name);
1512 Assert.AreEqual (line_base + 10, l.LineNumber);
1515 e = vm.GetNextEvent ();
1516 Assert.IsTrue (e is StepEvent);
1517 l = e.Thread.GetFrames ()[0].Location;
1518 Assert.AreEqual ("ln1", l.Method.Name);
1519 Assert.AreEqual (line_base + 2, l.LineNumber);
1521 // GetSourceFiles ()
1522 string[] sources = l.Method.DeclaringType.GetSourceFiles ();
1523 Assert.AreEqual (1, sources.Length);
1524 Assert.AreEqual ("dtest-app.cs", sources [0]);
1528 public void Suspend () {
1531 Start (new string [] { "dtest-app.exe", "suspend-test" });
1533 Event e = run_until ("suspend");
1535 ThreadMirror main = e.Thread;
1543 // The debuggee should be suspended while it is running the infinite loop
1545 StackFrame frame = main.GetFrames ()[0];
1546 Assert.AreEqual ("suspend", frame.Method.Name);
1550 // resuming when not suspended
1551 AssertThrows<InvalidOperationException> (delegate () {
1561 public void AssemblyLoad () {
1562 Event e = run_until ("assembly_load");
1566 e = vm.GetNextEvent ();
1567 Assert.IsInstanceOfType (typeof (AssemblyLoadEvent), e);
1568 Assert.IsTrue ((e as AssemblyLoadEvent).Assembly.Location.EndsWith ("System.dll"));
1570 var frames = e.Thread.GetFrames ();
1571 Assert.IsTrue (frames.Length > 0);
1572 Assert.AreEqual ("assembly_load", frames [0].Method.Name);
1576 public void CreateValue () {
1579 v = vm.CreateValue (1);
1580 Assert.AreEqual (vm, v.VirtualMachine);
1581 Assert.AreEqual (1, v.Value);
1583 v = vm.CreateValue (null);
1584 Assert.AreEqual (vm, v.VirtualMachine);
1585 Assert.AreEqual (null, v.Value);
1587 // Argument checking
1588 AssertThrows <ArgumentException> (delegate () {
1589 v = vm.CreateValue ("FOO");
1594 public void CreateString () {
1595 StringMirror s = vm.RootDomain.CreateString ("ABC");
1597 Assert.AreEqual (vm, s.VirtualMachine);
1598 Assert.AreEqual ("ABC", s.Value);
1599 Assert.AreEqual (vm.RootDomain, s.Domain);
1601 // Argument checking
1602 AssertThrows <ArgumentNullException> (delegate () {
1603 s = vm.RootDomain.CreateString (null);
1608 public void Invoke () {
1609 Event e = run_until ("invoke1");
1611 StackFrame frame = e.Thread.GetFrames () [0];
1613 TypeMirror t = frame.Method.DeclaringType;
1614 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
1616 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
1622 m = t.GetMethod ("invoke_return_void");
1623 v = this_obj.InvokeMethod (e.Thread, m, null);
1627 m = t.GetMethod ("invoke_return_ref");
1628 v = this_obj.InvokeMethod (e.Thread, m, null);
1629 AssertValue ("ABC", v);
1632 m = t.GetMethod ("invoke_return_null");
1633 v = this_obj.InvokeMethod (e.Thread, m, null);
1634 AssertValue (null, v);
1637 m = t.GetMethod ("invoke_return_primitive");
1638 v = this_obj.InvokeMethod (e.Thread, m, null);
1639 AssertValue (42, v);
1642 m = t.GetMethod ("invoke_pass_primitive");
1643 Value[] args = new Value [] {
1644 vm.CreateValue ((byte)Byte.MaxValue),
1645 vm.CreateValue ((sbyte)SByte.MaxValue),
1646 vm.CreateValue ((short)1),
1647 vm.CreateValue ((ushort)1),
1648 vm.CreateValue ((int)1),
1649 vm.CreateValue ((uint)1),
1650 vm.CreateValue ((long)1),
1651 vm.CreateValue ((ulong)1),
1652 vm.CreateValue ('A'),
1653 vm.CreateValue (true),
1654 vm.CreateValue (3.14f),
1655 vm.CreateValue (3.14) };
1657 v = this_obj.InvokeMethod (e.Thread, m, args);
1658 AssertValue ((int)Byte.MaxValue + (int)SByte.MaxValue + 1 + 1 + 1 + 1 + 1 + 1 + 'A' + 1 + 3 + 3, v);
1661 m = t.GetMethod ("invoke_pass_ref");
1662 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
1663 AssertValue ("ABC", v);
1666 m = t.GetMethod ("invoke_pass_ref");
1667 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (null) });
1668 AssertValue (null, v);
1671 m = t.GetMethod ("invoke_static_pass_ref");
1672 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
1673 AssertValue ("ABC", v);
1675 // static invoked using ObjectMirror.InvokeMethod
1676 m = t.GetMethod ("invoke_static_pass_ref");
1677 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
1678 AssertValue ("ABC", v);
1680 // method which throws an exception
1682 m = t.GetMethod ("invoke_throws");
1683 v = this_obj.InvokeMethod (e.Thread, m, null);
1685 } catch (InvocationException ex) {
1686 Assert.AreEqual ("Exception", ex.Exception.Type.Name);
1690 m = t.GetMethod (".ctor");
1691 v = t.InvokeMethod (e.Thread, m, null);
1692 Assert.IsInstanceOfType (typeof (ObjectMirror), v);
1693 Assert.AreEqual ("Tests", (v as ObjectMirror).Type.Name);
1695 // Argument checking
1698 AssertThrows<ArgumentNullException> (delegate {
1699 m = t.GetMethod ("invoke_pass_ref");
1700 v = this_obj.InvokeMethod (null, m, new Value [] { vm.CreateValue (null) });
1704 AssertThrows<ArgumentNullException> (delegate {
1705 v = this_obj.InvokeMethod (e.Thread, null, new Value [] { vm.CreateValue (null) });
1708 // invalid number of arguments
1709 m = t.GetMethod ("invoke_pass_ref");
1710 AssertThrows<ArgumentException> (delegate {
1711 v = this_obj.InvokeMethod (e.Thread, m, null);
1714 // invalid type of argument (ref != primitive)
1715 m = t.GetMethod ("invoke_pass_ref");
1716 AssertThrows<ArgumentException> (delegate {
1717 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
1720 // invalid type of argument (primitive != primitive)
1721 m = t.GetMethod ("invoke_pass_primitive_2");
1722 AssertThrows<ArgumentException> (delegate {
1723 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
1726 // invoking a non-static method as static
1727 m = t.GetMethod ("invoke_pass_ref");
1728 AssertThrows<ArgumentException> (delegate {
1729 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
1732 // invoking a method defined in another class
1733 m = t2.GetMethod ("invoke");
1734 AssertThrows<ArgumentException> (delegate {
1735 v = this_obj.InvokeMethod (e.Thread, m, null);
1741 public void InvokeVType () {
1742 Event e = run_until ("invoke1");
1744 StackFrame frame = e.Thread.GetFrames () [0];
1746 var s = frame.GetArgument (1) as StructMirror;
1748 TypeMirror t = s.Type;
1753 // Pass struct as this, receive int
1754 m = t.GetMethod ("invoke_return_int");
1755 v = s.InvokeMethod (e.Thread, m, null);
1756 AssertValue (42, v);
1758 // Pass struct as this, receive intptr
1759 m = t.GetMethod ("invoke_return_intptr");
1760 v = s.InvokeMethod (e.Thread, m, null);
1761 AssertValue (43, v);
1765 public void BreakpointDuringInvoke () {
1766 Event e = run_until ("invoke1");
1768 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke2");
1769 Assert.IsNotNull (m);
1770 vm.SetBreakpoint (m, 0);
1772 StackFrame frame = e.Thread.GetFrames () [0];
1773 var o = frame.GetThis () as ObjectMirror;
1775 bool failed = false;
1777 bool finished = false;
1778 object wait = new object ();
1780 // Have to invoke in a separate thread as the invoke is suspended until we
1781 // resume after the breakpoint
1782 Thread t = new Thread (delegate () {
1784 o.InvokeMethod (e.Thread, m, null);
1790 Monitor.Pulse (wait);
1796 StackFrame invoke_frame = null;
1799 e = vm.GetNextEvent ();
1800 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
1801 // Check stack trace support and invokes
1802 var frames = e.Thread.GetFrames ();
1803 invoke_frame = frames [0];
1804 Assert.AreEqual ("invoke2", frames [0].Method.Name);
1805 Assert.IsTrue (frames [0].IsDebuggerInvoke);
1806 Assert.AreEqual ("invoke1", frames [1].Method.Name);
1811 // Check that the invoke frames are no longer valid
1812 AssertThrows<InvalidStackFrameException> (delegate {
1813 invoke_frame.GetThis ();
1818 Monitor.Wait (wait);
1821 // Check InvokeOptions.DisableBreakpoints flag
1822 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
1826 public void InvokeSingleThreaded () {
1829 Start (new string [] { "dtest-app.exe", "invoke-single-threaded" });
1831 Event e = run_until ("invoke_single_threaded_2");
1833 StackFrame f = e.Thread.GetFrames ()[0];
1835 var obj = f.GetThis () as ObjectMirror;
1837 // Check that the counter value incremented by the other thread does not increase
1838 // during the invoke.
1839 object counter1 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
1841 var m = obj.Type.GetMethod ("invoke_return_void");
1842 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
1844 object counter2 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
1846 Assert.AreEqual ((int)counter1, (int)counter2);
1848 // Test multiple invokes done in succession
1849 m = obj.Type.GetMethod ("invoke_return_void");
1850 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
1852 // Test events during single-threaded invokes
1853 vm.EnableEvents (EventType.TypeLoad);
1854 m = obj.Type.GetMethod ("invoke_type_load");
1855 obj.BeginInvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded, delegate {
1859 e = vm.GetNextEvent ();
1860 Assert.AreEqual (EventType.TypeLoad, e.EventType);
1864 public void GetThreads () {
1869 public void Threads () {
1870 Event e = run_until ("threads");
1872 Assert.AreEqual (ThreadState.Running, e.Thread.ThreadState);
1874 vm.EnableEvents (EventType.ThreadStart, EventType.ThreadDeath);
1878 e = vm.GetNextEvent ();
1879 Assert.IsInstanceOfType (typeof (ThreadStartEvent), e);
1880 var state = e.Thread.ThreadState;
1881 Assert.IsTrue (state == ThreadState.Running || state == ThreadState.Unstarted);
1885 e = vm.GetNextEvent ();
1886 Assert.IsInstanceOfType (typeof (ThreadDeathEvent), e);
1887 Assert.AreEqual (ThreadState.Stopped, e.Thread.ThreadState);
1891 public void Frame_SetValue () {
1892 Event e = run_until ("locals2");
1894 StackFrame frame = e.Thread.GetFrames () [0];
1897 var l = frame.Method.GetLocal ("i");
1898 frame.SetValue (l, vm.CreateValue ((long)55));
1899 AssertValue (55, frame.GetValue (l));
1902 l = frame.Method.GetLocal ("s");
1903 frame.SetValue (l, vm.RootDomain.CreateString ("DEF"));
1904 AssertValue ("DEF", frame.GetValue (l));
1906 // argument as local
1907 l = frame.Method.GetLocal ("arg");
1908 frame.SetValue (l, vm.CreateValue (6));
1909 AssertValue (6, frame.GetValue (l));
1912 var p = frame.Method.GetParameters ()[1];
1913 frame.SetValue (p, vm.CreateValue (7));
1914 AssertValue (7, frame.GetValue (p));
1916 // argument checking
1919 AssertThrows<ArgumentNullException> (delegate () {
1920 frame.SetValue ((LocalVariable)null, vm.CreateValue (55));
1924 AssertThrows<ArgumentNullException> (delegate () {
1925 l = frame.Method.GetLocal ("i");
1926 frame.SetValue (l, null);
1929 // value of invalid type
1930 AssertThrows<ArgumentException> (delegate () {
1931 l = frame.Method.GetLocal ("i");
1932 frame.SetValue (l, vm.CreateValue (55));
1937 public void InvokeRegress () {
1938 Event e = run_until ("invoke1");
1940 StackFrame frame = e.Thread.GetFrames () [0];
1942 TypeMirror t = frame.Method.DeclaringType;
1943 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
1945 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
1951 m = t.GetMethod ("invoke_return_void");
1952 v = this_obj.InvokeMethod (e.Thread, m, null);
1955 // Check that the stack frames remain valid during the invoke
1956 Assert.AreEqual ("Tests", (frame.GetThis () as ObjectMirror).Type.Name);
1958 // do another invoke
1959 m = t.GetMethod ("invoke_return_void");
1960 v = this_obj.InvokeMethod (e.Thread, m, null);
1963 // Try a single step after the invoke
1964 var req = vm.CreateStepRequest (e.Thread);
1965 req.Depth = StepDepth.Into;
1966 req.Size = StepSize.Line;
1971 // Step into invoke2
1973 e = vm.GetNextEvent ();
1974 Assert.IsTrue (e is StepEvent);
1975 Assert.AreEqual ("invoke2", (e as StepEvent).Method.Name);
1979 frame = e.Thread.GetFrames () [0];
1983 public void Exceptions () {
1984 Event e = run_until ("exceptions");
1985 var req = vm.CreateExceptionRequest (null);
1990 e = vm.GetNextEvent ();
1991 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
1992 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
1994 var frames = e.Thread.GetFrames ();
1995 Assert.AreEqual ("exceptions", frames [0].Method.Name);
1998 // exception type filter
2000 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.ArgumentException", false, false));
2003 // Skip the throwing of the second OverflowException
2006 e = vm.GetNextEvent ();
2007 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2008 Assert.AreEqual ("ArgumentException", (e as ExceptionEvent).Exception.Type.Name);
2011 // Implicit exceptions
2012 req = vm.CreateExceptionRequest (null);
2017 e = vm.GetNextEvent ();
2018 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2019 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
2022 // Argument checking
2023 AssertThrows<ArgumentException> (delegate {
2024 vm.CreateExceptionRequest (e.Thread.Type);
2029 public void Domains () {
2032 Start (new string [] { "dtest-app.exe", "domain-test" });
2034 vm.EnableEvents (EventType.AppDomainCreate, EventType.AppDomainUnload, EventType.AssemblyUnload);
2036 Event e = run_until ("domains");
2040 e = vm.GetNextEvent ();
2041 Assert.IsInstanceOfType (typeof (AppDomainCreateEvent), e);
2043 var domain = (e as AppDomainCreateEvent).Domain;
2045 // Run until the callback in the domain
2046 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_in_domain");
2047 Assert.IsNotNull (m);
2048 vm.SetBreakpoint (m, 0);
2052 e = vm.GetNextEvent ();
2053 if (e is BreakpointEvent)
2057 Assert.AreEqual ("invoke_in_domain", (e as BreakpointEvent).Method.Name);
2059 // d_method is from another domain
2060 MethodMirror d_method = (e as BreakpointEvent).Method;
2061 Assert.IsTrue (m != d_method);
2063 var frames = e.Thread.GetFrames ();
2064 Assert.AreEqual ("invoke_in_domain", frames [0].Method.Name);
2065 Assert.AreEqual ("invoke", frames [1].Method.Name);
2066 Assert.AreEqual ("domains", frames [2].Method.Name);
2068 // This is empty when receiving the AppDomainCreateEvent
2069 Assert.AreEqual ("domain", domain.FriendlyName);
2071 // Run until the unload
2074 e = vm.GetNextEvent ();
2075 if (e is AssemblyUnloadEvent) {
2081 Assert.IsInstanceOfType (typeof (AppDomainUnloadEvent), e);
2082 Assert.AreEqual (domain, (e as AppDomainUnloadEvent).Domain);
2084 // Run past the unload
2085 e = run_until ("domains_2");
2087 // Test access to unloaded types
2088 // FIXME: Add an exception type for this
2089 AssertThrows<Exception> (delegate {
2090 d_method.DeclaringType.GetValue (d_method.DeclaringType.GetField ("static_i"));
2095 public void DynamicMethods () {
2096 Event e = run_until ("dyn_call");
2098 var m = e.Thread.GetFrames ()[1].Method;
2099 Assert.AreEqual ("dyn_method", m.Name);
2101 // Test access to IL
2102 var body = m.GetMethodBody ();
2104 ILInstruction ins = body.Instructions [0];
2105 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
2106 Assert.AreEqual ("FOO", ins.Operand);
2110 public void RefEmit () {
2113 Start (new string [] { "dtest-app.exe", "ref-emit-test" });
2115 Event e = run_until ("ref_emit_call");
2117 var m = e.Thread.GetFrames ()[1].Method;
2118 Assert.AreEqual ("ref_emit_method", m.Name);
2120 // Test access to IL
2121 var body = m.GetMethodBody ();
2125 ins = body.Instructions [0];
2126 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
2127 Assert.AreEqual ("FOO", ins.Operand);
2129 ins = body.Instructions [1];
2130 Assert.AreEqual (OpCodes.Call, ins.OpCode);
2131 Assert.IsInstanceOfType (typeof (MethodMirror), ins.Operand);
2132 Assert.AreEqual ("ref_emit_call", (ins.Operand as MethodMirror).Name);
2136 public void IsAttached () {
2137 var f = entry_point.DeclaringType.GetField ("is_attached");
2139 Event e = run_until ("Main");
2141 AssertValue (true, entry_point.DeclaringType.GetValue (f));
2145 public void StackTraceInNative () {
2146 // Check that stack traces can be produced for threads in native code
2149 Start (new string [] { "dtest-app.exe", "frames-in-native" });
2151 var e = run_until ("frames_in_native");
2153 // FIXME: This is racy
2160 StackFrame[] frames = e.Thread.GetFrames ();
2162 int frame_index = -1;
2163 for (int i = 0; i < frames.Length; ++i) {
2164 if (frames [i].Method.Name == "Sleep") {
2170 Assert.IsTrue (frame_index != -1);
2171 Assert.AreEqual ("Sleep", frames [frame_index].Method.Name);
2172 Assert.AreEqual ("frames_in_native", frames [frame_index + 1].Method.Name);
2173 Assert.AreEqual ("Main", frames [frame_index + 2].Method.Name);
2175 // Check that invokes are disabled for such threads
2176 TypeMirror t = frames [frame_index + 1].Method.DeclaringType;
2179 var m = t.GetMethod ("invoke_static_return_void");
2180 AssertThrows<InvalidOperationException> (delegate {
2181 t.InvokeMethod (e.Thread, m, null);