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 System.Security.Cryptography;
14 using NUnit.Framework;
16 #pragma warning disable 0219
19 public class DebuggerTests
22 MethodMirror entry_point;
23 StepEventRequest step_req;
25 void AssertThrows<ExType> (Action del) where ExType : Exception {
33 Assert.IsTrue (thrown);
36 // No other way to pass arguments to the tests ?
37 public static bool listening = Environment.GetEnvironmentVariable ("DBG_SUSPEND") != null;
38 public static string runtime = Environment.GetEnvironmentVariable ("DBG_RUNTIME");
39 public static string agent_args = Environment.GetEnvironmentVariable ("DBG_AGENT_ARGS");
41 Event GetNextEvent () {
42 var es = vm.GetNextEventSet ();
43 Assert.AreEqual (1, es.Events.Length);
47 void Start (string[] args) {
49 var pi = new Diag.ProcessStartInfo ();
52 pi.FileName = runtime;
55 pi.Arguments = String.Join (" ", args);
56 vm = VirtualMachineManager.Launch (pi, new LaunchOptions { AgentArgs = agent_args });
58 var ep = new IPEndPoint (IPAddress.Any, 10000);
59 Console.WriteLine ("Listening on " + ep + "...");
60 vm = VirtualMachineManager.Listen (ep);
63 var load_req = vm.CreateAssemblyLoadRequest ();
66 Event vmstart = GetNextEvent ();
67 Assert.AreEqual (EventType.VMStart, vmstart.EventType);
76 /* Find out the entry point */
80 if (e is AssemblyLoadEvent) {
81 AssemblyLoadEvent ae = (AssemblyLoadEvent)e;
82 entry_point = ae.Assembly.EntryPoint;
83 if (entry_point != null)
93 BreakpointEvent run_until (string name) {
95 MethodMirror m = entry_point.DeclaringType.GetMethod (name);
97 vm.SetBreakpoint (m, 0);
104 if (e is BreakpointEvent)
108 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
109 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
111 return (e as BreakpointEvent);
114 Event single_step (ThreadMirror t) {
115 var req = vm.CreateStepRequest (t);
119 Event e = GetNextEvent ();
120 Assert.IsTrue (e is StepEvent);
127 void check_arg_val (StackFrame frame, int pos, Type type, object eval) {
128 object val = frame.GetArgument (pos);
129 Assert.IsTrue (val is PrimitiveValue);
130 object v = (val as PrimitiveValue).Value;
131 Assert.AreEqual (type, v.GetType ());
133 Assert.IsTrue (Math.Abs ((float)eval - (float)v) < 0.0001);
134 else if (eval is double)
135 Assert.IsTrue (Math.Abs ((double)eval - (double)v) < 0.0001);
137 Assert.AreEqual (eval, v);
140 void AssertValue (object expected, object val) {
141 if (expected is string) {
142 Assert.IsTrue (val is StringMirror);
143 Assert.AreEqual (expected, (val as StringMirror).Value);
144 } else if (val is StructMirror && (val as StructMirror).Type.Name == "IntPtr") {
145 AssertValue (expected, (val as StructMirror).Fields [0]);
147 Assert.IsTrue (val is PrimitiveValue);
148 Assert.AreEqual (expected, (val as PrimitiveValue).Value);
153 public void SetUp () {
154 Start (new string [] { "dtest-app.exe" });
158 public void TearDown () {
162 if (step_req != null)
167 Event e = GetNextEvent ();
169 if (e is VMDeathEvent)
177 public void SimpleBreakpoint () {
180 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp1");
181 Assert.IsNotNull (m);
183 vm.SetBreakpoint (m, 0);
188 Assert.AreEqual (EventType.Breakpoint, e.EventType);
189 Assert.IsTrue (e is BreakpointEvent);
190 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
193 AssertThrows<ArgumentException> (delegate {
195 vm.SetBreakpoint (m, 1);
200 public void BreakpointsSameLocation () {
201 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp2");
202 Assert.IsNotNull (m);
204 vm.SetBreakpoint (m, 0);
205 vm.SetBreakpoint (m, 0);
209 var es = vm.GetNextEventSet ();
210 Assert.AreEqual (2, es.Events.Length);
211 Assert.IsTrue (es [0] is BreakpointEvent);
212 Assert.AreEqual (m, (es [0] as BreakpointEvent).Method);
214 Assert.IsTrue (es [1] is BreakpointEvent);
215 Assert.AreEqual (m.Name, (es [1] as BreakpointEvent).Method.Name);
219 public void BreakpointAlreadyJITted () {
220 Event e = run_until ("bp1");
222 /* Place a breakpoint on bp3 */
223 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp3");
224 Assert.IsNotNull (m);
225 vm.SetBreakpoint (m, 0);
227 /* Same with generic instances */
228 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp7");
229 Assert.IsNotNull (m2);
230 vm.SetBreakpoint (m2, 0);
235 Assert.AreEqual (EventType.Breakpoint, e.EventType);
236 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
240 /* Non-shared instance */
242 Assert.AreEqual (EventType.Breakpoint, e.EventType);
243 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
247 /* Shared instance */
249 Assert.AreEqual (EventType.Breakpoint, e.EventType);
250 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
254 public void ClearBreakpoint () {
257 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
258 Assert.IsNotNull (m);
259 EventRequest req1 = vm.SetBreakpoint (m, 0);
260 EventRequest req2 = vm.SetBreakpoint (m, 0);
262 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
263 Assert.IsNotNull (m2);
264 vm.SetBreakpoint (m2, 0);
269 var es = vm.GetNextEventSet ();
270 Assert.AreEqual (2, es.Events.Length);
271 Assert.AreEqual (EventType.Breakpoint, es [0].EventType);
272 Assert.AreEqual (m.Name, (es [0] as BreakpointEvent).Method.Name);
273 Assert.AreEqual (EventType.Breakpoint, es [1].EventType);
274 Assert.AreEqual (m.Name, (es [1] as BreakpointEvent).Method.Name);
276 /* Clear one of them */
282 Assert.AreEqual (EventType.Breakpoint, e.EventType);
283 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
285 /* Clear the other */
291 Assert.AreEqual (EventType.Breakpoint, e.EventType);
292 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
296 public void ClearAllBreakpoints () {
299 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
300 Assert.IsNotNull (m);
301 vm.SetBreakpoint (m, 0);
303 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
304 Assert.IsNotNull (m2);
305 vm.SetBreakpoint (m2, 0);
307 vm.ClearAllBreakpoints ();
312 Assert.IsTrue (!(e is BreakpointEvent));
313 if (e is VMDeathEvent)
318 public void BreakpointOnGShared () {
321 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp6");
322 Assert.IsNotNull (m);
324 vm.SetBreakpoint (m, 0);
329 Assert.AreEqual (EventType.Breakpoint, e.EventType);
330 Assert.IsTrue (e is BreakpointEvent);
331 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
333 // Breakpoint on an open generic method of a closed generic class (#3422)
334 var frame = e.Thread.GetFrames ()[0];
335 var ginst = frame.GetValue (frame.Method.GetLocal ("gc"));
336 var m2 = (ginst as ObjectMirror).Type.GetMethod ("bp");
337 vm.SetBreakpoint (m2, 0);
342 Assert.AreEqual (EventType.Breakpoint, e.EventType);
343 Assert.IsTrue (e is BreakpointEvent);
344 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
348 public void SingleStepping () {
349 Event e = run_until ("single_stepping");
351 var req = vm.CreateStepRequest (e.Thread);
356 // Step over 'bool b = true'
359 Assert.IsTrue (e is StepEvent);
360 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
365 Assert.IsTrue (e is StepEvent);
366 Assert.AreEqual ("ss1", (e as StepEvent).Method.Name);
371 Assert.IsTrue (e is StepEvent);
372 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
374 // Change to step over
376 req.Depth = StepDepth.Over;
382 Assert.IsTrue (e is StepEvent);
383 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
385 // Change to step into
387 req.Depth = StepDepth.Into;
393 Assert.IsTrue (e is StepEvent);
394 Assert.AreEqual ("ss3", (e as StepEvent).Method.Name);
396 // Change to step out
398 req.Depth = StepDepth.Out;
401 // Step back into single_stepping
404 Assert.IsTrue (e is StepEvent);
405 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
407 // Change to step into
409 req.Depth = StepDepth.Into;
412 // Step into ss3_2 ()
415 Assert.IsTrue (e is StepEvent);
416 Assert.AreEqual ("ss3_2", (e as StepEvent).Method.Name);
418 // Change to step over
420 req.Depth = StepDepth.Over;
423 // Step over ss3_2_2 ()
426 Assert.IsTrue (e is StepEvent);
427 Assert.AreEqual ("ss3_2", (e as StepEvent).Method.Name);
429 // Recreate the request
433 // Step back into single_stepping () with the new request
436 Assert.IsTrue (e is StepEvent);
437 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
439 // Change to step into
441 req.Depth = StepDepth.Into;
447 Assert.IsTrue (e is StepEvent);
448 Assert.AreEqual ("ss4", (e as StepEvent).Method.Name);
450 // Change to StepSize.Line
452 req.Depth = StepDepth.Over;
453 req.Size = StepSize.Line;
456 // Step over ss1 (); ss1 ();
459 Assert.IsTrue (e is StepEvent);
463 req.Depth = StepDepth.Into;
468 Assert.IsTrue (e is StepEvent);
469 Assert.AreEqual ("ss2", (e as StepEvent).Method.Name);
474 e = run_until ("ss5");
476 // Add an assembly filter
477 req.AssemblyFilter = new AssemblyMirror [] { (e as BreakpointEvent).Method.DeclaringType.Assembly };
480 // Step into is_even, skipping the linq stuff
483 Assert.IsTrue (e is StepEvent);
484 Assert.AreEqual ("is_even", (e as StepEvent).Method.Name);
486 // FIXME: Check that single stepping works with lock (obj)
491 e = run_until ("ss6");
493 req = vm.CreateStepRequest (e.Thread);
494 req.Depth = StepDepth.Over;
497 // Check that single stepping works in out-of-line bblocks
500 Assert.IsTrue (e is StepEvent);
503 Assert.IsTrue (e is StepEvent);
504 Assert.AreEqual ("ss6", (e as StepEvent).Method.Name);
507 // Check that a step over stops at an EH clause
508 e = run_until ("ss7_2");
509 req = vm.CreateStepRequest (e.Thread);
510 req.Depth = StepDepth.Out;
514 Assert.IsTrue (e is StepEvent);
515 Assert.AreEqual ("ss7", (e as StepEvent).Method.Name);
517 req = vm.CreateStepRequest (e.Thread);
518 req.Depth = StepDepth.Over;
522 Assert.IsTrue (e is StepEvent);
523 Assert.AreEqual ("ss7", (e as StepEvent).Method.Name);
528 public void MethodEntryExit () {
529 run_until ("single_stepping");
531 var req1 = vm.CreateMethodEntryRequest ();
532 var req2 = vm.CreateMethodExitRequest ();
538 Event e = GetNextEvent ();
539 Assert.IsTrue (e is MethodEntryEvent);
540 Assert.AreEqual ("ss1", (e as MethodEntryEvent).Method.Name);
544 Assert.IsTrue (e is MethodExitEvent);
545 Assert.AreEqual ("ss1", (e as MethodExitEvent).Method.Name);
552 public void CountFilter () {
553 run_until ("single_stepping");
555 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("ss3");
556 Assert.IsNotNull (m2);
557 vm.SetBreakpoint (m2, 0);
559 var req1 = vm.CreateMethodEntryRequest ();
563 // Enter ss2, ss1 is skipped
565 Event e = GetNextEvent ();
566 Assert.IsTrue (e is MethodEntryEvent);
567 Assert.AreEqual ("ss2", (e as MethodEntryEvent).Method.Name);
569 // Breakpoint on ss3, the entry event is no longer reported
572 Assert.IsTrue (e is BreakpointEvent);
578 public void Arguments () {
581 var e = run_until ("arg1");
583 StackFrame frame = e.Thread.GetFrames () [0];
585 check_arg_val (frame, 0, typeof (sbyte), SByte.MaxValue - 5);
586 check_arg_val (frame, 1, typeof (byte), Byte.MaxValue - 5);
587 check_arg_val (frame, 2, typeof (bool), true);
588 check_arg_val (frame, 3, typeof (short), Int16.MaxValue - 5);
589 check_arg_val (frame, 4, typeof (ushort), UInt16.MaxValue - 5);
590 check_arg_val (frame, 5, typeof (char), 'F');
591 check_arg_val (frame, 6, typeof (int), Int32.MaxValue - 5);
592 check_arg_val (frame, 7, typeof (uint), UInt32.MaxValue - 5);
593 check_arg_val (frame, 8, typeof (long), Int64.MaxValue - 5);
594 check_arg_val (frame, 9, typeof (ulong), UInt64.MaxValue - 5);
595 check_arg_val (frame, 10, typeof (float), 1.2345f);
596 check_arg_val (frame, 11, typeof (double), 6.78910);
598 e = run_until ("arg2");
600 frame = e.Thread.GetFrames () [0];
603 val = frame.GetArgument (0);
604 AssertValue ("FOO", val);
605 Assert.AreEqual ("String", (val as ObjectMirror).Type.Name);
608 val = frame.GetArgument (1);
609 AssertValue (null, val);
612 val = frame.GetArgument (2);
613 AssertValue ("BLA", val);
616 val = frame.GetArgument (3);
617 AssertValue (42, val);
620 val = frame.GetArgument (4);
621 Assert.IsTrue (val is ObjectMirror);
622 Assert.AreEqual ("GClass`1", (val as ObjectMirror).Type.Name);
625 val = frame.GetArgument (5);
626 Assert.IsTrue (val is ObjectMirror);
627 Assert.AreEqual ("Object", (val as ObjectMirror).Type.Name);
629 // this on static methods
630 val = frame.GetThis ();
631 AssertValue (null, val);
633 e = run_until ("arg3");
635 frame = e.Thread.GetFrames () [0];
638 val = frame.GetThis ();
639 Assert.IsTrue (val is ObjectMirror);
640 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
642 // objref in register
643 val = frame.GetArgument (0);
644 AssertValue ("BLA", val);
648 public void Arrays () {
651 var e = run_until ("o2");
653 StackFrame frame = e.Thread.GetFrames () [0];
656 val = frame.GetArgument (0);
657 Assert.IsTrue (val is ArrayMirror);
658 ArrayMirror arr = val as ArrayMirror;
659 Assert.AreEqual (2, arr.Length);
660 AssertValue ("BAR", arr [0]);
661 AssertValue ("BAZ", arr [1]);
663 var vals = arr.GetValues (0, 2);
664 Assert.AreEqual (2, vals.Count);
665 AssertValue ("BAR", vals [0]);
666 AssertValue ("BAZ", vals [1]);
668 arr [0] = vm.RootDomain.CreateString ("ABC");
669 AssertValue ("ABC", arr [0]);
671 arr [0] = vm.CreateValue (null);
672 AssertValue (null, arr [0]);
674 arr.SetValues (0, new Value [] { vm.RootDomain.CreateString ("D1"), vm.RootDomain.CreateString ("D2") });
675 AssertValue ("D1", arr [0]);
676 AssertValue ("D2", arr [1]);
679 val = frame.GetArgument (1);
680 Assert.IsTrue (val is ArrayMirror);
681 arr = val as ArrayMirror;
682 Assert.AreEqual (2, arr.Length);
683 AssertValue (42, arr [0]);
684 AssertValue (43, arr [1]);
687 AssertThrows<IndexOutOfRangeException> (delegate () {
691 AssertThrows<IndexOutOfRangeException> (delegate () {
692 val = arr [Int32.MinValue];
695 AssertThrows<IndexOutOfRangeException> (delegate () {
696 vals = arr.GetValues (0, 3);
699 AssertThrows<IndexOutOfRangeException> (delegate () {
700 arr [2] = vm.CreateValue (null);
703 AssertThrows<IndexOutOfRangeException> (delegate () {
704 arr [Int32.MinValue] = vm.CreateValue (null);
707 AssertThrows<IndexOutOfRangeException> (delegate () {
708 arr.SetValues (0, new Value [] { null, null, null });
712 val = frame.GetArgument (2);
713 Assert.IsTrue (val is ArrayMirror);
714 arr = val as ArrayMirror;
715 Assert.AreEqual (2, arr.Rank);
716 Assert.AreEqual (4, arr.Length);
717 Assert.AreEqual (2, arr.GetLength (0));
718 Assert.AreEqual (2, arr.GetLength (1));
719 Assert.AreEqual (0, arr.GetLowerBound (0));
720 Assert.AreEqual (0, arr.GetLowerBound (1));
721 vals = arr.GetValues (0, 4);
722 AssertValue (1, vals [0]);
723 AssertValue (2, vals [1]);
724 AssertValue (3, vals [2]);
725 AssertValue (4, vals [3]);
727 val = frame.GetArgument (3);
728 Assert.IsTrue (val is ArrayMirror);
729 arr = val as ArrayMirror;
730 Assert.AreEqual (2, arr.Rank);
731 Assert.AreEqual (4, arr.Length);
732 Assert.AreEqual (2, arr.GetLength (0));
733 Assert.AreEqual (2, arr.GetLength (1));
734 Assert.AreEqual (1, arr.GetLowerBound (0));
735 Assert.AreEqual (3, arr.GetLowerBound (1));
737 AssertThrows<ArgumentOutOfRangeException> (delegate () {
740 AssertThrows<ArgumentOutOfRangeException> (delegate () {
744 AssertThrows<ArgumentOutOfRangeException> (delegate () {
745 arr.GetLowerBound (-1);
747 AssertThrows<ArgumentOutOfRangeException> (delegate () {
748 arr.GetLowerBound (2);
751 // arrays treated as generic collections
752 val = frame.GetArgument (4);
753 Assert.IsTrue (val is ArrayMirror);
754 arr = val as ArrayMirror;
758 public void Object_GetValue () {
759 var e = run_until ("o1");
760 var frame = e.Thread.GetFrames () [0];
762 object val = frame.GetThis ();
763 Assert.IsTrue (val is ObjectMirror);
764 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
765 ObjectMirror o = (val as ObjectMirror);
767 TypeMirror t = o.Type;
770 object f = o.GetValue (t.GetField ("field_i"));
772 f = o.GetValue (t.GetField ("field_s"));
773 AssertValue ("S", f);
774 f = o.GetValue (t.GetField ("field_enum"));
775 Assert.IsTrue (f is EnumMirror);
776 Assert.AreEqual (1, (f as EnumMirror).Value);
777 Assert.AreEqual ("B", (f as EnumMirror).StringValue);
779 // Inherited object fields
780 TypeMirror parent = t.BaseType;
781 f = o.GetValue (parent.GetField ("base_field_i"));
783 f = o.GetValue (parent.GetField ("base_field_s"));
784 AssertValue ("T", f);
787 f = o.GetValue (o.Type.GetField ("static_i"));
791 ObjectMirror o2 = frame.GetValue (frame.Method.GetParameters ()[1]) as ObjectMirror;
792 Assert.AreEqual ("GClass`1", o2.Type.Name);
793 TypeMirror t2 = o2.Type;
794 f = o2.GetValue (t2.GetField ("field"));
797 ObjectMirror o3 = frame.GetValue (frame.Method.GetParameters ()[2]) as ObjectMirror;
798 Assert.AreEqual ("GClass`1", o3.Type.Name);
799 TypeMirror t3 = o3.Type;
800 f = o3.GetValue (t3.GetField ("field"));
801 AssertValue ("FOO", f);
804 AssertThrows<ArgumentNullException> (delegate () {
810 public void Object_GetValues () {
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 TypeMirror t = o.Type;
823 object[] vals = o.GetValues (new FieldInfoMirror [] { t.GetField ("field_i"), t.GetField ("field_s") });
827 AssertValue ("S", f);
830 AssertThrows<ArgumentNullException> (delegate () {
834 AssertThrows<ArgumentNullException> (delegate () {
835 o.GetValues (new FieldInfoMirror [] { null });
838 // field of another class
839 AssertThrows<ArgumentException> (delegate () {
840 o.GetValue (val2.Type.GetField ("field_j"));
844 void TestSetValue (ObjectMirror o, string field_name, object val) {
846 o.SetValue (o.Type.GetField (field_name), vm.RootDomain.CreateString ((string)val));
848 o.SetValue (o.Type.GetField (field_name), vm.CreateValue (val));
849 Value f = o.GetValue (o.Type.GetField (field_name));
850 AssertValue (val, f);
854 public void Object_SetValues () {
855 var e = run_until ("o1");
856 var frame = e.Thread.GetFrames () [0];
858 object val = frame.GetThis ();
859 Assert.IsTrue (val is ObjectMirror);
860 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
861 ObjectMirror o = (val as ObjectMirror);
863 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
865 TestSetValue (o, "field_i", 22);
866 TestSetValue (o, "field_bool1", false);
867 TestSetValue (o, "field_bool2", true);
868 TestSetValue (o, "field_char", 'B');
869 TestSetValue (o, "field_byte", (byte)129);
870 TestSetValue (o, "field_sbyte", (sbyte)-33);
871 TestSetValue (o, "field_short", (short)(Int16.MaxValue - 5));
872 TestSetValue (o, "field_ushort", (ushort)(UInt16.MaxValue - 5));
873 TestSetValue (o, "field_long", Int64.MaxValue - 5);
874 TestSetValue (o, "field_ulong", (ulong)(UInt64.MaxValue - 5));
875 TestSetValue (o, "field_float", 6.28f);
876 TestSetValue (o, "field_double", 6.28);
877 TestSetValue (o, "static_i", 23);
878 TestSetValue (o, "field_s", "CDEF");
883 f = o.GetValue (o.Type.GetField ("field_intptr"));
884 Assert.IsInstanceOfType (typeof (StructMirror), f);
885 AssertValue (Int32.MaxValue - 5, (f as StructMirror).Fields [0]);
888 FieldInfoMirror field = o.Type.GetField ("field_enum");
889 f = o.GetValue (field);
890 (f as EnumMirror).Value = 5;
891 o.SetValue (field, f);
892 f = o.GetValue (field);
893 Assert.AreEqual (5, (f as EnumMirror).Value);
896 o.SetValue (o.Type.GetField ("field_s"), vm.CreateValue (null));
897 f = o.GetValue (o.Type.GetField ("field_s"));
898 AssertValue (null, f);
901 field = o.Type.GetField ("generic_field_struct");
902 f = o.GetValue (field);
903 o.SetValue (field, f);
906 field = o.Type.GetField ("field_nullable");
907 f = o.GetValue (field);
908 AssertValue (0, (f as StructMirror).Fields [0]);
909 AssertValue (false, (f as StructMirror).Fields [1]);
910 o.SetValue (field, vm.CreateValue (6));
911 f = o.GetValue (field);
912 AssertValue (6, (f as StructMirror).Fields [0]);
913 AssertValue (true, (f as StructMirror).Fields [1]);
914 o.SetValue (field, vm.CreateValue (null));
915 f = o.GetValue (field);
916 AssertValue (0, (f as StructMirror).Fields [0]);
917 AssertValue (false, (f as StructMirror).Fields [1]);
920 AssertThrows<ArgumentNullException> (delegate () {
921 o.SetValues (null, new Value [0]);
924 AssertThrows<ArgumentNullException> (delegate () {
925 o.SetValues (new FieldInfoMirror [0], null);
928 AssertThrows<ArgumentNullException> (delegate () {
929 o.SetValues (new FieldInfoMirror [] { null }, new Value [1] { null });
932 // vtype with a wrong type
933 AssertThrows<ArgumentException> (delegate () {
934 o.SetValue (o.Type.GetField ("field_struct"), o.GetValue (o.Type.GetField ("field_enum")));
937 // reference type not assignment compatible
938 AssertThrows<ArgumentException> (delegate () {
939 o.SetValue (o.Type.GetField ("field_class"), o);
942 // field of another class
943 AssertThrows<ArgumentException> (delegate () {
944 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
949 public void Type_SetValue () {
950 var e = run_until ("o1");
951 var frame = e.Thread.GetFrames () [0];
954 object val = frame.GetThis ();
955 Assert.IsTrue (val is ObjectMirror);
956 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
957 ObjectMirror o = (val as ObjectMirror);
959 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
961 o.Type.SetValue (o.Type.GetField ("static_i"), vm.CreateValue (55));
962 f = o.Type.GetValue (o.Type.GetField ("static_i"));
965 o.Type.SetValue (o.Type.GetField ("static_s"), vm.RootDomain.CreateString ("B"));
966 f = o.Type.GetValue (o.Type.GetField ("static_s"));
967 AssertValue ("B", f);
970 AssertThrows<ArgumentNullException> (delegate () {
971 o.Type.SetValue (null, vm.CreateValue (0));
974 AssertThrows<ArgumentNullException> (delegate () {
975 o.Type.SetValue (o.Type.GetField ("static_i"), null);
978 // field of another class
979 AssertThrows<ArgumentException> (delegate () {
980 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
985 public void TypeInfo () {
986 Event e = run_until ("ti2");
987 StackFrame frame = e.Thread.GetFrames () [0];
992 t = frame.Method.GetParameters ()[0].ParameterType;
994 Assert.AreEqual ("String[]", t.Name);
995 Assert.AreEqual ("string[]", t.CSharpName);
996 Assert.AreEqual ("Array", t.BaseType.Name);
997 Assert.AreEqual (true, t.HasElementType);
998 Assert.AreEqual (true, t.IsArray);
999 Assert.AreEqual (1, t.GetArrayRank ());
1000 Assert.AreEqual ("String", t.GetElementType ().Name);
1002 t = frame.Method.GetParameters ()[2].ParameterType;
1004 Assert.AreEqual ("Int32[,]", t.Name);
1006 //Assert.AreEqual ("int[,]", t.CSharpName);
1007 Assert.AreEqual ("Array", t.BaseType.Name);
1008 Assert.AreEqual (true, t.HasElementType);
1009 Assert.AreEqual (true, t.IsArray);
1010 Assert.AreEqual (2, t.GetArrayRank ());
1011 Assert.AreEqual ("Int32", t.GetElementType ().Name);
1014 t = frame.Method.GetParameters ()[3].ParameterType;
1016 //Assert.AreEqual ("Int32&", t.Name);
1017 //Assert.AreEqual (true, t.IsByRef);
1018 //Assert.AreEqual (true, t.HasElementType);
1021 t = frame.Method.GetParameters ()[4].ParameterType;
1023 //Assert.AreEqual ("Int32*", t.Name);
1024 Assert.AreEqual (true, t.IsPointer);
1025 Assert.AreEqual (true, t.HasElementType);
1026 Assert.AreEqual ("Int32", t.GetElementType ().Name);
1027 Assert.AreEqual (false, t.IsPrimitive);
1030 t = frame.Method.GetParameters ()[5].ParameterType;
1031 Assert.AreEqual (true, t.IsPrimitive);
1034 t = frame.Method.GetParameters ()[6].ParameterType;
1035 Assert.AreEqual ("AStruct", t.Name);
1036 Assert.AreEqual (false, t.IsPrimitive);
1037 Assert.AreEqual (true, t.IsValueType);
1038 Assert.AreEqual (false, t.IsClass);
1041 t = frame.Method.GetParameters ()[7].ParameterType;
1042 Assert.AreEqual ("Tests", t.Name);
1043 var nested = (from nt in t.GetNestedTypes () where nt.IsNestedPublic select nt).ToArray ();
1044 Assert.AreEqual (1, nested.Length);
1045 Assert.AreEqual ("NestedClass", nested [0].Name);
1046 Assert.IsTrue (t.BaseType.IsAssignableFrom (t));
1047 Assert.IsTrue (!t.IsAssignableFrom (t.BaseType));
1049 // generic instances
1050 t = frame.Method.GetParameters ()[9].ParameterType;
1051 Assert.AreEqual ("GClass`1", t.Name);
1052 Assert.IsTrue (t.IsGenericType);
1053 Assert.IsFalse (t.IsGenericTypeDefinition);
1055 var args = t.GetGenericArguments ();
1056 Assert.AreEqual (1, args.Length);
1057 Assert.AreEqual ("Int32", args [0].Name);
1059 // generic type definitions
1060 var gtd = t.GetGenericTypeDefinition ();
1061 Assert.AreEqual ("GClass`1", gtd.Name);
1062 Assert.IsTrue (gtd.IsGenericType);
1063 Assert.IsTrue (gtd.IsGenericTypeDefinition);
1064 Assert.AreEqual (gtd, gtd.GetGenericTypeDefinition ());
1066 args = gtd.GetGenericArguments ();
1067 Assert.AreEqual (1, args.Length);
1068 Assert.AreEqual ("T", args [0].Name);
1071 t = frame.Method.GetParameters ()[10].ParameterType;
1072 Assert.AreEqual ("AnEnum", t.Name);
1073 Assert.IsTrue (t.IsEnum);
1074 Assert.AreEqual ("Int32", t.EnumUnderlyingType.Name);
1077 t = frame.Method.GetParameters ()[7].ParameterType;
1079 var props = t.GetProperties ();
1080 Assert.AreEqual (3, props.Length);
1081 foreach (PropertyInfoMirror prop in props) {
1082 ParameterInfoMirror[] indexes = prop.GetIndexParameters ();
1084 if (prop.Name == "IntProperty") {
1085 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1086 Assert.AreEqual ("get_IntProperty", prop.GetGetMethod ().Name);
1087 Assert.AreEqual ("set_IntProperty", prop.GetSetMethod ().Name);
1088 Assert.AreEqual (0, indexes.Length);
1089 } else if (prop.Name == "ReadOnlyProperty") {
1090 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1091 Assert.AreEqual ("get_ReadOnlyProperty", prop.GetGetMethod ().Name);
1092 Assert.AreEqual (null, prop.GetSetMethod ());
1093 Assert.AreEqual (0, indexes.Length);
1094 } else if (prop.Name == "IndexedProperty") {
1095 Assert.AreEqual (1, indexes.Length);
1096 Assert.AreEqual ("Int32", indexes [0].ParameterType.Name);
1100 // custom attributes
1101 t = frame.Method.GetParameters ()[8].ParameterType;
1102 Assert.AreEqual ("Tests2", t.Name);
1103 var attrs = t.GetCustomAttributes (true);
1104 Assert.AreEqual (2, attrs.Length);
1105 foreach (var attr in attrs) {
1106 if (attr.Constructor.DeclaringType.Name == "DebuggerDisplayAttribute") {
1107 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1108 Assert.AreEqual ("Tests", attr.ConstructorArguments [0].Value);
1109 Assert.AreEqual (2, attr.NamedArguments.Count);
1110 Assert.AreEqual ("Name", attr.NamedArguments [0].Property.Name);
1111 Assert.AreEqual ("FOO", attr.NamedArguments [0].TypedValue.Value);
1112 Assert.AreEqual ("Target", attr.NamedArguments [1].Property.Name);
1113 Assert.IsInstanceOfType (typeof (TypeMirror), attr.NamedArguments [1].TypedValue.Value);
1114 Assert.AreEqual ("Int32", (attr.NamedArguments [1].TypedValue.Value as TypeMirror).Name);
1115 } else if (attr.Constructor.DeclaringType.Name == "DebuggerTypeProxyAttribute") {
1116 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1117 Assert.IsInstanceOfType (typeof (TypeMirror), attr.ConstructorArguments [0].Value);
1118 Assert.AreEqual ("Tests", (attr.ConstructorArguments [0].Value as TypeMirror).Name);
1120 Assert.Fail (attr.Constructor.DeclaringType.Name);
1126 public void FieldInfo () {
1127 Event e = run_until ("ti2");
1128 StackFrame frame = e.Thread.GetFrames () [0];
1132 t = frame.Method.GetParameters ()[8].ParameterType;
1133 Assert.AreEqual ("Tests2", t.Name);
1135 var fi = t.GetField ("field_j");
1136 var attrs = fi.GetCustomAttributes (true);
1137 Assert.AreEqual (1, attrs.Length);
1138 var attr = attrs [0];
1139 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1140 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1141 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1142 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1146 public void PropertyInfo () {
1147 Event e = run_until ("ti2");
1148 StackFrame frame = e.Thread.GetFrames () [0];
1152 t = frame.Method.GetParameters ()[8].ParameterType;
1153 Assert.AreEqual ("Tests2", t.Name);
1155 var pi = t.GetProperty ("AProperty");
1156 var attrs = pi.GetCustomAttributes (true);
1157 Assert.AreEqual (1, attrs.Length);
1158 var attr = attrs [0];
1159 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1160 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1161 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1162 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1166 [Category ("only5")]
1167 public void Type_GetValue () {
1168 Event e = run_until ("o1");
1169 StackFrame frame = e.Thread.GetFrames () [0];
1171 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1173 TypeMirror t = o.Type;
1175 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1178 object f = t.GetValue (o.Type.GetField ("static_i"));
1179 AssertValue (55, f);
1181 f = t.GetValue (o.Type.GetField ("static_s"));
1182 AssertValue ("A", f);
1184 // literal static fields
1185 f = t.GetValue (o.Type.GetField ("literal_i"));
1186 AssertValue (56, f);
1188 f = t.GetValue (o.Type.GetField ("literal_s"));
1189 AssertValue ("B", f);
1191 // Inherited static fields
1192 TypeMirror parent = t.BaseType;
1193 f = t.GetValue (parent.GetField ("base_static_i"));
1194 AssertValue (57, f);
1196 f = t.GetValue (parent.GetField ("base_static_s"));
1197 AssertValue ("C", f);
1199 // thread static field
1200 f = t.GetValue (t.GetField ("tls_i"), e.Thread);
1201 AssertValue (42, f);
1203 // Argument checking
1204 AssertThrows<ArgumentNullException> (delegate () {
1209 AssertThrows<ArgumentException> (delegate () {
1210 t.GetValue (o.Type.GetField ("field_i"));
1213 // field on another type
1214 AssertThrows<ArgumentException> (delegate () {
1215 t.GetValue (val2.Type.GetField ("static_field_j"));
1218 // special static field
1219 AssertThrows<ArgumentException> (delegate () {
1220 t.GetValue (t.GetField ("tls_i"));
1225 public void Type_GetValues () {
1226 Event e = run_until ("o1");
1227 StackFrame frame = e.Thread.GetFrames () [0];
1229 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1231 TypeMirror t = o.Type;
1234 object[] vals = t.GetValues (new FieldInfoMirror [] { t.GetField ("static_i"), t.GetField ("static_s") });
1235 object f = vals [0];
1236 AssertValue (55, f);
1239 AssertValue ("A", f);
1241 // Argument checking
1242 AssertThrows<ArgumentNullException> (delegate () {
1246 AssertThrows<ArgumentNullException> (delegate () {
1247 t.GetValues (new FieldInfoMirror [] { null });
1252 public void ObjRefs () {
1253 Event e = run_until ("objrefs1");
1254 StackFrame frame = e.Thread.GetFrames () [0];
1256 ObjectMirror o = frame.GetThis () as ObjectMirror;
1257 ObjectMirror child = o.GetValue (o.Type.GetField ("child")) as ObjectMirror;
1259 Assert.IsTrue (child.Address != 0);
1261 // Check that object references are internalized correctly
1262 Assert.AreEqual (o, frame.GetThis ());
1264 run_until ("objrefs2");
1266 // child should be gc'd now
1267 Assert.IsTrue (child.IsCollected);
1270 * No longer works since Type is read eagerly
1273 AssertThrows<ObjectCollectedException> (delegate () {
1274 TypeMirror t = child.Type;
1278 AssertThrows<ObjectCollectedException> (delegate () {
1279 long addr = child.Address;
1284 public void Type_GetObject () {
1285 Event e = run_until ("o1");
1286 StackFrame frame = e.Thread.GetFrames () [0];
1288 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1290 TypeMirror t = o.Type;
1292 Assert.AreEqual ("MonoType", t.GetTypeObject ().Type.Name);
1296 public void VTypes () {
1297 Event e = run_until ("vtypes1");
1298 StackFrame frame = e.Thread.GetFrames () [0];
1301 ObjectMirror o = frame.GetThis () as ObjectMirror;
1302 var obj = o.GetValue (o.Type.GetField ("field_struct"));
1303 Assert.IsTrue (obj is StructMirror);
1304 var s = obj as StructMirror;
1305 Assert.AreEqual ("AStruct", s.Type.Name);
1306 AssertValue (42, s ["i"]);
1308 AssertValue ("S", obj);
1309 AssertValue (43, s ["k"]);
1310 obj = o.GetValue (o.Type.GetField ("field_boxed_struct"));
1311 Assert.IsTrue (obj is StructMirror);
1312 s = obj as StructMirror;
1313 Assert.AreEqual ("AStruct", s.Type.Name);
1314 AssertValue (42, s ["i"]);
1316 // vtypes as arguments
1317 s = frame.GetArgument (0) as StructMirror;
1318 AssertValue (44, s ["i"]);
1320 AssertValue ("T", obj);
1321 AssertValue (45, s ["k"]);
1323 // vtypes as array entries
1324 var arr = frame.GetArgument (1) as ArrayMirror;
1326 Assert.IsTrue (obj is StructMirror);
1327 s = obj as StructMirror;
1328 AssertValue (1, s ["i"]);
1329 AssertValue ("S1", s ["s"]);
1331 Assert.IsTrue (obj is StructMirror);
1332 s = obj as StructMirror;
1333 AssertValue (2, s ["i"]);
1334 AssertValue ("S2", s ["s"]);
1336 // Argument checking
1337 s = frame.GetArgument (0) as StructMirror;
1338 AssertThrows<ArgumentException> (delegate () {
1342 // generic vtype instances
1343 o = frame.GetThis () as ObjectMirror;
1344 obj = o.GetValue (o.Type.GetField ("generic_field_struct"));
1345 Assert.IsTrue (obj is StructMirror);
1346 s = obj as StructMirror;
1347 Assert.AreEqual ("GStruct`1", s.Type.Name);
1348 AssertValue (42, s ["i"]);
1350 // this on vtype methods
1351 e = run_until ("vtypes2");
1353 e = single_step (e.Thread);
1355 frame = e.Thread.GetFrames () [0];
1357 Assert.AreEqual ("foo", (e as StepEvent).Method.Name);
1358 obj = frame.GetThis ();
1360 Assert.IsTrue (obj is StructMirror);
1361 s = obj as StructMirror;
1362 AssertValue (44, s ["i"]);
1363 AssertValue ("T", s ["s"]);
1364 AssertValue (45, s ["k"]);
1366 // this on static vtype methods
1367 e = run_until ("vtypes3");
1369 e = single_step (e.Thread);
1371 frame = e.Thread.GetFrames () [0];
1373 Assert.AreEqual ("static_foo", (e as StepEvent).Method.Name);
1374 obj = frame.GetThis ();
1375 AssertValue (null, obj);
1379 public void AssemblyInfo () {
1380 Event e = run_until ("single_stepping");
1382 StackFrame frame = e.Thread.GetFrames () [0];
1384 var aname = frame.Method.DeclaringType.Assembly.GetName ();
1385 Assert.AreEqual ("dtest-app, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", aname.ToString ());
1387 ModuleMirror m = frame.Method.DeclaringType.Module;
1389 Assert.AreEqual ("dtest-app.exe", m.Name);
1390 Assert.AreEqual ("dtest-app.exe", m.ScopeName);
1391 Assert.IsTrue (m.FullyQualifiedName.IndexOf ("dtest-app.exe") != -1);
1392 Guid guid = m.ModuleVersionId;
1393 Assert.AreEqual (frame.Method.DeclaringType.Assembly, m.Assembly);
1394 Assert.AreEqual (frame.Method.DeclaringType.Assembly.ManifestModule, m);
1396 // This is no longer true on 4.0
1397 //Assert.AreEqual ("Assembly", frame.Method.DeclaringType.Assembly.GetAssemblyObject ().Type.Name);
1399 TypeMirror t = vm.RootDomain.Corlib.GetType ("System.Diagnostics.DebuggerDisplayAttribute");
1400 Assert.AreEqual ("DebuggerDisplayAttribute", t.Name);
1404 public void LocalsInfo () {
1405 Event e = run_until ("locals2");
1407 StackFrame frame = e.Thread.GetFrames () [0];
1409 var locals = frame.Method.GetLocals ();
1410 Assert.AreEqual (7, locals.Length);
1411 for (int i = 0; i < 7; ++i) {
1412 if (locals [i].Name == "args") {
1413 Assert.IsTrue (locals [i].IsArg);
1414 Assert.AreEqual ("String[]", locals [i].Type.Name);
1415 } else if (locals [i].Name == "arg") {
1416 Assert.IsTrue (locals [i].IsArg);
1417 Assert.AreEqual ("Int32", locals [i].Type.Name);
1418 } else if (locals [i].Name == "i") {
1419 Assert.IsFalse (locals [i].IsArg);
1420 Assert.AreEqual ("Int64", locals [i].Type.Name);
1421 } else if (locals [i].Name == "j") {
1422 Assert.IsFalse (locals [i].IsArg);
1423 Assert.AreEqual ("Int32", locals [i].Type.Name);
1424 } else if (locals [i].Name == "s") {
1425 Assert.IsFalse (locals [i].IsArg);
1426 Assert.AreEqual ("String", locals [i].Type.Name);
1427 } else if (locals [i].Name == "t") {
1429 Assert.IsTrue (locals [i].IsArg);
1430 Assert.AreEqual ("String", locals [i].Type.Name);
1431 } else if (locals [i].Name == "rs") {
1432 Assert.IsTrue (locals [i].IsArg);
1433 Assert.AreEqual ("String", locals [i].Type.Name);
1441 public void Locals () {
1442 var be = run_until ("locals1");
1444 StackFrame frame = be.Thread.GetFrames () [0];
1446 MethodMirror m1 = frame.Method;
1448 be = run_until ("locals2");
1450 frame = be.Thread.GetFrames () [0];
1452 object val = frame.GetValue (frame.Method.GetLocal ("i"));
1453 AssertValue (0, val);
1456 var req = vm.CreateStepRequest (be.Thread);
1460 var e = GetNextEvent ();
1461 Assert.IsTrue (e is StepEvent);
1462 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1464 // Execute s = "AB";
1466 e = GetNextEvent ();
1467 Assert.IsTrue (e is StepEvent);
1468 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1470 frame = e.Thread.GetFrames () [0];
1472 val = frame.GetValue (frame.Method.GetLocal ("i"));
1473 AssertValue (42, val);
1475 LocalVariable[] locals = frame.Method.GetLocals ();
1476 var vals = frame.GetValues (locals);
1477 Assert.AreEqual (locals.Length, vals.Length);
1478 for (int i = 0; i < locals.Length; ++i) {
1479 if (locals [i].Name == "i")
1480 AssertValue (42, vals [i]);
1481 if (locals [i].Name == "s")
1482 AssertValue ("AB", vals [i]);
1483 if (locals [i].Name == "t")
1484 AssertValue ("ABC", vals [i]);
1487 // Argument checking
1490 AssertThrows<ArgumentNullException> (delegate () {
1491 frame.GetValue ((LocalVariable)null);
1493 // GetValue () local from another method
1494 AssertThrows<ArgumentException> (delegate () {
1495 frame.GetValue (m1.GetLocal ("foo"));
1499 AssertThrows<ArgumentNullException> (delegate () {
1500 frame.GetValue ((ParameterInfoMirror)null);
1502 // GetValue () local from another method
1503 AssertThrows<ArgumentException> (delegate () {
1504 frame.GetValue (m1.GetParameters ()[0]);
1507 // GetValues () null
1508 AssertThrows<ArgumentNullException> (delegate () {
1509 frame.GetValues (null);
1511 // GetValues () embedded null
1512 AssertThrows<ArgumentNullException> (delegate () {
1513 frame.GetValues (new LocalVariable [] { null });
1515 // GetValues () local from another method
1516 AssertThrows<ArgumentException> (delegate () {
1517 frame.GetValues (new LocalVariable [] { m1.GetLocal ("foo") });
1520 AssertThrows<ArgumentException> (delegate () {
1521 val = frame.GetValue (frame.Method.ReturnParameter);
1524 // invalid stack frames
1526 e = GetNextEvent ();
1527 Assert.IsTrue (e is StepEvent);
1528 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1530 AssertThrows<InvalidStackFrameException> (delegate () {
1531 frame.GetValue (frame.Method.GetLocal ("i"));
1538 public void GetVisibleVariables () {
1539 Event e = run_until ("locals4");
1542 var locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
1543 Assert.AreEqual (2, locals.Count);
1544 var loc = locals.First (l => l.Name == "i");
1545 Assert.AreEqual ("Int64", loc.Type.Name);
1546 loc = locals.First (l => l.Name == "s");
1547 Assert.AreEqual ("String", loc.Type.Name);
1549 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
1550 Assert.AreEqual ("i", loc.Name);
1551 Assert.AreEqual ("Int64", loc.Type.Name);
1553 e = run_until ("locals5");
1556 locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
1557 Assert.AreEqual (2, locals.Count);
1558 loc = locals.First (l => l.Name == "i");
1559 Assert.AreEqual ("String", loc.Type.Name);
1560 loc = locals.First (l => l.Name == "s");
1561 Assert.AreEqual ("String", loc.Type.Name);
1563 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
1564 Assert.AreEqual ("i", loc.Name);
1565 Assert.AreEqual ("String", loc.Type.Name);
1567 // Variable in another scope
1568 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("j");
1569 Assert.IsNull (loc);
1573 public void Exit () {
1578 var e = GetNextEvent ();
1579 Assert.IsInstanceOfType (typeof (VMDeathEvent), e);
1582 /* Could be a remote vm with no process */
1585 Assert.AreEqual (5, p.ExitCode);
1588 AssertThrows<VMDisconnectedException> (delegate () {
1597 public void Dispose () {
1602 var e = GetNextEvent ();
1603 Assert.IsInstanceOfType (typeof (VMDisconnectEvent), e);
1606 /* Could be a remote vm with no process */
1609 Assert.AreEqual (3, p.ExitCode);
1612 AssertThrows<VMDisconnectedException> (delegate () {
1621 [Category("only88")]
1622 public void LineNumbers () {
1623 Event e = run_until ("line_numbers");
1625 step_req = vm.CreateStepRequest (e.Thread);
1626 step_req.Depth = StepDepth.Into;
1633 e = GetNextEvent ();
1634 Assert.IsTrue (e is StepEvent);
1636 l = e.Thread.GetFrames ()[0].Location;
1638 Assert.IsTrue (l.SourceFile.IndexOf ("dtest-app.cs") != -1);
1639 Assert.AreEqual ("ln1", l.Method.Name);
1642 using (FileStream fs = new FileStream (l.SourceFile, FileMode.Open, FileAccess.Read)) {
1643 MD5 md5 = MD5.Create ();
1644 var hash = md5.ComputeHash (fs);
1646 for (int i = 0; i < 16; ++i)
1647 Assert.AreEqual (hash [i], l.SourceFileHash [i]);
1650 int line_base = l.LineNumber;
1653 e = GetNextEvent ();
1654 Assert.IsTrue (e is StepEvent);
1655 l = e.Thread.GetFrames ()[0].Location;
1656 Assert.AreEqual ("ln2", l.Method.Name);
1657 Assert.AreEqual (line_base + 6, l.LineNumber);
1660 e = GetNextEvent ();
1661 Assert.IsTrue (e is StepEvent);
1662 l = e.Thread.GetFrames ()[0].Location;
1663 Assert.AreEqual ("ln1", l.Method.Name);
1664 Assert.AreEqual (line_base + 1, l.LineNumber);
1667 e = GetNextEvent ();
1668 Assert.IsTrue (e is StepEvent);
1669 l = e.Thread.GetFrames ()[0].Location;
1670 Assert.AreEqual ("ln3", l.Method.Name);
1671 Assert.AreEqual (line_base + 11, l.LineNumber);
1674 e = GetNextEvent ();
1675 Assert.IsTrue (e is StepEvent);
1676 l = e.Thread.GetFrames ()[0].Location;
1677 Assert.AreEqual ("ln3", l.Method.Name);
1678 Assert.IsTrue (l.SourceFile.EndsWith ("FOO"));
1679 Assert.AreEqual (55, l.LineNumber);
1682 e = GetNextEvent ();
1683 Assert.IsTrue (e is StepEvent);
1684 l = e.Thread.GetFrames ()[0].Location;
1685 Assert.AreEqual ("ln1", l.Method.Name);
1686 Assert.AreEqual (line_base + 2, l.LineNumber);
1688 // GetSourceFiles ()
1689 string[] sources = l.Method.DeclaringType.GetSourceFiles ();
1690 Assert.AreEqual (2, sources.Length);
1691 Assert.AreEqual ("dtest-app.cs", sources [0]);
1692 Assert.AreEqual ("FOO", sources [1]);
1694 sources = l.Method.DeclaringType.GetSourceFiles (true);
1695 Assert.AreEqual (2, sources.Length);
1696 Assert.IsTrue (sources [0].EndsWith ("dtest-app.cs"));
1697 Assert.IsTrue (sources [1].EndsWith ("FOO"));
1701 public void Suspend () {
1704 Start (new string [] { "dtest-app.exe", "suspend-test" });
1706 Event e = run_until ("suspend");
1708 ThreadMirror main = e.Thread;
1716 // The debuggee should be suspended while it is running the infinite loop
1718 StackFrame frame = main.GetFrames ()[0];
1719 Assert.AreEqual ("suspend", frame.Method.Name);
1723 // resuming when not suspended
1724 AssertThrows<InvalidOperationException> (delegate () {
1734 public void AssemblyLoad () {
1735 Event e = run_until ("assembly_load");
1737 var load_req = vm.CreateAssemblyLoadRequest ();
1742 e = GetNextEvent ();
1743 Assert.IsInstanceOfType (typeof (AssemblyLoadEvent), e);
1744 Assert.IsTrue ((e as AssemblyLoadEvent).Assembly.Location.EndsWith ("System.dll"));
1746 var frames = e.Thread.GetFrames ();
1747 Assert.IsTrue (frames.Length > 0);
1748 Assert.AreEqual ("assembly_load", frames [0].Method.Name);
1752 public void CreateValue () {
1755 v = vm.CreateValue (1);
1756 Assert.AreEqual (vm, v.VirtualMachine);
1757 Assert.AreEqual (1, v.Value);
1759 v = vm.CreateValue (null);
1760 Assert.AreEqual (vm, v.VirtualMachine);
1761 Assert.AreEqual (null, v.Value);
1763 // Argument checking
1764 AssertThrows <ArgumentException> (delegate () {
1765 v = vm.CreateValue ("FOO");
1770 public void CreateString () {
1771 StringMirror s = vm.RootDomain.CreateString ("ABC");
1773 Assert.AreEqual (vm, s.VirtualMachine);
1774 Assert.AreEqual ("ABC", s.Value);
1775 Assert.AreEqual (vm.RootDomain, s.Domain);
1778 StringBuilder sb = new StringBuilder ();
1779 for (int i = 0; i < 1024; ++i)
1781 s = vm.RootDomain.CreateString (sb.ToString ());
1783 // Argument checking
1784 AssertThrows <ArgumentNullException> (delegate () {
1785 s = vm.RootDomain.CreateString (null);
1790 public void CreateBoxedValue () {
1791 ObjectMirror o = vm.RootDomain.CreateBoxedValue (new PrimitiveValue (vm, 42));
1793 Assert.AreEqual ("Int32", o.Type.Name);
1794 //AssertValue (42, m.GetValue (o.Type.GetField ("m_value")));
1796 // Argument checking
1797 AssertThrows <ArgumentNullException> (delegate () {
1798 vm.RootDomain.CreateBoxedValue (null);
1801 AssertThrows <ArgumentException> (delegate () {
1802 vm.RootDomain.CreateBoxedValue (o);
1807 public void Invoke () {
1808 Event e = run_until ("invoke1");
1810 StackFrame frame = e.Thread.GetFrames () [0];
1812 TypeMirror t = frame.Method.DeclaringType;
1813 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
1815 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
1821 m = t.GetMethod ("invoke_return_void");
1822 v = this_obj.InvokeMethod (e.Thread, m, null);
1826 m = t.GetMethod ("invoke_return_ref");
1827 v = this_obj.InvokeMethod (e.Thread, m, null);
1828 AssertValue ("ABC", v);
1831 m = t.GetMethod ("invoke_return_null");
1832 v = this_obj.InvokeMethod (e.Thread, m, null);
1833 AssertValue (null, v);
1836 m = t.GetMethod ("invoke_return_primitive");
1837 v = this_obj.InvokeMethod (e.Thread, m, null);
1838 AssertValue (42, v);
1841 m = t.GetMethod ("invoke_return_nullable");
1842 v = this_obj.InvokeMethod (e.Thread, m, null);
1843 Assert.IsInstanceOfType (typeof (StructMirror), v);
1844 var s = v as StructMirror;
1845 AssertValue (42, s.Fields [0]);
1846 AssertValue (true, s.Fields [1]);
1848 // pass nullable as this
1849 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
1850 m = s.Type.GetMethod ("ToString");
1851 v = s.InvokeMethod (e.Thread, m, null);
1853 // return nullable null
1854 m = t.GetMethod ("invoke_return_nullable_null");
1855 v = this_obj.InvokeMethod (e.Thread, m, null);
1856 Assert.IsInstanceOfType (typeof (StructMirror), v);
1857 s = v as StructMirror;
1858 AssertValue (0, s.Fields [0]);
1859 AssertValue (false, s.Fields [1]);
1861 // pass nullable as this
1862 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
1863 m = s.Type.GetMethod ("ToString");
1864 v = s.InvokeMethod (e.Thread, m, null);
1867 m = t.GetMethod ("invoke_pass_primitive");
1868 Value[] args = new Value [] {
1869 vm.CreateValue ((byte)Byte.MaxValue),
1870 vm.CreateValue ((sbyte)SByte.MaxValue),
1871 vm.CreateValue ((short)1),
1872 vm.CreateValue ((ushort)1),
1873 vm.CreateValue ((int)1),
1874 vm.CreateValue ((uint)1),
1875 vm.CreateValue ((long)1),
1876 vm.CreateValue ((ulong)1),
1877 vm.CreateValue ('A'),
1878 vm.CreateValue (true),
1879 vm.CreateValue (3.14f),
1880 vm.CreateValue (3.14) };
1882 v = this_obj.InvokeMethod (e.Thread, m, args);
1883 AssertValue ((int)Byte.MaxValue + (int)SByte.MaxValue + 1 + 1 + 1 + 1 + 1 + 1 + 'A' + 1 + 3 + 3, v);
1886 m = t.GetMethod ("invoke_pass_ref");
1887 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
1888 AssertValue ("ABC", v);
1891 m = t.GetMethod ("invoke_pass_ref");
1892 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (null) });
1893 AssertValue (null, v);
1896 m = t.GetMethod ("invoke_static_pass_ref");
1897 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
1898 AssertValue ("ABC", v);
1900 // static invoked using ObjectMirror.InvokeMethod
1901 m = t.GetMethod ("invoke_static_pass_ref");
1902 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
1903 AssertValue ("ABC", v);
1905 // method which throws an exception
1907 m = t.GetMethod ("invoke_throws");
1908 v = this_obj.InvokeMethod (e.Thread, m, null);
1910 } catch (InvocationException ex) {
1911 Assert.AreEqual ("Exception", ex.Exception.Type.Name);
1915 m = t.GetMethod (".ctor");
1916 v = t.InvokeMethod (e.Thread, m, null);
1917 Assert.IsInstanceOfType (typeof (ObjectMirror), v);
1918 Assert.AreEqual ("Tests", (v as ObjectMirror).Type.Name);
1920 // Argument checking
1923 AssertThrows<ArgumentNullException> (delegate {
1924 m = t.GetMethod ("invoke_pass_ref");
1925 v = this_obj.InvokeMethod (null, m, new Value [] { vm.CreateValue (null) });
1929 AssertThrows<ArgumentNullException> (delegate {
1930 v = this_obj.InvokeMethod (e.Thread, null, new Value [] { vm.CreateValue (null) });
1933 // invalid number of arguments
1934 m = t.GetMethod ("invoke_pass_ref");
1935 AssertThrows<ArgumentException> (delegate {
1936 v = this_obj.InvokeMethod (e.Thread, m, null);
1939 // invalid type of argument (ref != primitive)
1940 m = t.GetMethod ("invoke_pass_ref");
1941 AssertThrows<ArgumentException> (delegate {
1942 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
1945 // invalid type of argument (primitive != primitive)
1946 m = t.GetMethod ("invoke_pass_primitive_2");
1947 AssertThrows<ArgumentException> (delegate {
1948 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
1951 // invoking a non-static method as static
1952 m = t.GetMethod ("invoke_pass_ref");
1953 AssertThrows<ArgumentException> (delegate {
1954 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
1957 // invoking a method defined in another class
1958 m = t2.GetMethod ("invoke");
1959 AssertThrows<ArgumentException> (delegate {
1960 v = this_obj.InvokeMethod (e.Thread, m, null);
1965 public void InvokeVType () {
1966 Event e = run_until ("invoke1");
1968 StackFrame frame = e.Thread.GetFrames () [0];
1970 var s = frame.GetArgument (1) as StructMirror;
1972 TypeMirror t = s.Type;
1977 // Pass struct as this, receive int
1978 m = t.GetMethod ("invoke_return_int");
1979 v = s.InvokeMethod (e.Thread, m, null);
1980 AssertValue (42, v);
1982 // Pass struct as this, receive intptr
1983 m = t.GetMethod ("invoke_return_intptr");
1984 v = s.InvokeMethod (e.Thread, m, null);
1985 AssertValue (43, v);
1988 m = t.GetMethod ("invoke_static");
1989 v = t.InvokeMethod (e.Thread, m, null);
1992 // Pass generic struct as this
1993 s = frame.GetArgument (2) as StructMirror;
1995 m = t.GetMethod ("invoke_return_int");
1996 v = s.InvokeMethod (e.Thread, m, null);
1997 AssertValue (42, v);
2001 public void BreakpointDuringInvoke () {
2002 Event e = run_until ("invoke1");
2004 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke2");
2005 Assert.IsNotNull (m);
2006 vm.SetBreakpoint (m, 0);
2008 StackFrame frame = e.Thread.GetFrames () [0];
2009 var o = frame.GetThis () as ObjectMirror;
2011 bool failed = false;
2013 bool finished = false;
2014 object wait = new object ();
2016 // Have to invoke in a separate thread as the invoke is suspended until we
2017 // resume after the breakpoint
2018 Thread t = new Thread (delegate () {
2020 o.InvokeMethod (e.Thread, m, null);
2026 Monitor.Pulse (wait);
2032 StackFrame invoke_frame = null;
2035 e = GetNextEvent ();
2036 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
2037 // Check stack trace support and invokes
2038 var frames = e.Thread.GetFrames ();
2039 invoke_frame = frames [0];
2040 Assert.AreEqual ("invoke2", frames [0].Method.Name);
2041 Assert.IsTrue (frames [0].IsDebuggerInvoke);
2042 Assert.AreEqual ("invoke1", frames [1].Method.Name);
2049 Monitor.Wait (wait);
2052 // Check that the invoke frames are no longer valid
2053 AssertThrows<InvalidStackFrameException> (delegate {
2054 invoke_frame.GetThis ();
2057 // Check InvokeOptions.DisableBreakpoints flag
2058 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2062 public void DisabledExceptionDuringInvoke () {
2063 Event e = run_until ("invoke_ex");
2065 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_ex_inner");
2067 StackFrame frame = e.Thread.GetFrames () [0];
2068 var o = frame.GetThis () as ObjectMirror;
2070 var req = vm.CreateExceptionRequest (null);
2073 // Check InvokeOptions.DisableBreakpoints flag
2074 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2080 public void InvokeSingleThreaded () {
2083 Start (new string [] { "dtest-app.exe", "invoke-single-threaded" });
2085 Event e = run_until ("invoke_single_threaded_2");
2087 StackFrame f = e.Thread.GetFrames ()[0];
2089 var obj = f.GetThis () as ObjectMirror;
2091 // Check that the counter value incremented by the other thread does not increase
2092 // during the invoke.
2093 object counter1 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2095 var m = obj.Type.GetMethod ("invoke_return_void");
2096 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2098 object counter2 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2100 Assert.AreEqual ((int)counter1, (int)counter2);
2102 // Test multiple invokes done in succession
2103 m = obj.Type.GetMethod ("invoke_return_void");
2104 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2106 // Test events during single-threaded invokes
2107 vm.EnableEvents (EventType.TypeLoad);
2108 m = obj.Type.GetMethod ("invoke_type_load");
2109 obj.BeginInvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded, delegate {
2113 e = GetNextEvent ();
2114 Assert.AreEqual (EventType.TypeLoad, e.EventType);
2118 public void GetThreads () {
2123 public void Threads () {
2124 Event e = run_until ("threads");
2126 Assert.AreEqual (ThreadState.Running, e.Thread.ThreadState);
2128 Assert.IsTrue (e.Thread.ThreadId > 0);
2130 Assert.AreEqual (e.Thread.TID, e.Thread.TID);
2132 vm.EnableEvents (EventType.ThreadStart, EventType.ThreadDeath);
2136 e = GetNextEvent ();
2137 Assert.IsInstanceOfType (typeof (ThreadStartEvent), e);
2138 var state = e.Thread.ThreadState;
2139 Assert.IsTrue (state == ThreadState.Running || state == ThreadState.Unstarted);
2143 e = GetNextEvent ();
2144 Assert.IsInstanceOfType (typeof (ThreadDeathEvent), e);
2145 Assert.AreEqual (ThreadState.Stopped, e.Thread.ThreadState);
2149 public void Frame_SetValue () {
2150 Event e = run_until ("locals2");
2152 StackFrame frame = e.Thread.GetFrames () [0];
2155 var l = frame.Method.GetLocal ("i");
2156 frame.SetValue (l, vm.CreateValue ((long)55));
2157 AssertValue (55, frame.GetValue (l));
2160 l = frame.Method.GetLocal ("s");
2161 frame.SetValue (l, vm.RootDomain.CreateString ("DEF"));
2162 AssertValue ("DEF", frame.GetValue (l));
2164 // argument as local
2165 l = frame.Method.GetLocal ("arg");
2166 frame.SetValue (l, vm.CreateValue (6));
2167 AssertValue (6, frame.GetValue (l));
2170 var p = frame.Method.GetParameters ()[1];
2171 frame.SetValue (p, vm.CreateValue (7));
2172 AssertValue (7, frame.GetValue (p));
2175 p = frame.Method.GetParameters ()[2];
2176 frame.SetValue (p, vm.RootDomain.CreateString ("DEF"));
2177 AssertValue ("DEF", frame.GetValue (p));
2180 p = frame.Method.GetParameters ()[3];
2181 frame.SetValue (p, vm.RootDomain.CreateString ("DEF2"));
2182 AssertValue ("DEF2", frame.GetValue (p));
2184 // argument checking
2187 AssertThrows<ArgumentNullException> (delegate () {
2188 frame.SetValue ((LocalVariable)null, vm.CreateValue (55));
2192 AssertThrows<ArgumentNullException> (delegate () {
2193 l = frame.Method.GetLocal ("i");
2194 frame.SetValue (l, null);
2197 // value of invalid type
2198 AssertThrows<ArgumentException> (delegate () {
2199 l = frame.Method.GetLocal ("i");
2200 frame.SetValue (l, vm.CreateValue (55));
2206 public void Frame_SetValue_Registers () {
2207 Event e = run_until ("locals6_1");
2209 StackFrame frame = e.Thread.GetFrames () [1];
2212 var l = frame.Method.GetLocal ("j");
2213 frame.SetValue (l, vm.CreateValue (99));
2214 AssertValue (99, frame.GetValue (l));
2216 // Check it during execution
2217 e = run_until ("locals6_2");
2218 frame = e.Thread.GetFrames () [0];
2219 AssertValue (99, frame.GetValue (frame.Method.GetParameters ()[0]));
2221 // Set it while in a frame which clobbers its register
2222 e = run_until ("locals6_3");
2223 frame = e.Thread.GetFrames () [1];
2224 frame.SetValue (l, vm.CreateValue (100));
2225 AssertValue (100, frame.GetValue (l));
2227 // Check it during execution
2228 e = run_until ("locals6_4");
2229 frame = e.Thread.GetFrames () [0];
2230 AssertValue (100, frame.GetValue (frame.Method.GetParameters ()[0]));
2232 // Signed byte value
2233 e = run_until ("locals6_5");
2234 frame = e.Thread.GetFrames () [1];
2235 var l2 = frame.Method.GetLocal ("sb");
2236 frame.SetValue (l2, vm.CreateValue ((sbyte)-99));
2237 AssertValue (-99, frame.GetValue (l2));
2239 // Check it during execution
2240 e = run_until ("locals6_6");
2241 frame = e.Thread.GetFrames () [0];
2242 AssertValue (-99, frame.GetValue (frame.Method.GetParameters ()[0]));
2246 public void InvokeRegress () {
2247 Event e = run_until ("invoke1");
2249 StackFrame frame = e.Thread.GetFrames () [0];
2251 TypeMirror t = frame.Method.DeclaringType;
2252 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2254 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2260 m = t.GetMethod ("invoke_return_void");
2261 v = this_obj.InvokeMethod (e.Thread, m, null);
2264 // Check that the stack frames remain valid during the invoke
2265 Assert.AreEqual ("Tests", (frame.GetThis () as ObjectMirror).Type.Name);
2267 // do another invoke
2268 m = t.GetMethod ("invoke_return_void");
2269 v = this_obj.InvokeMethod (e.Thread, m, null);
2272 // Try a single step after the invoke
2273 var req = vm.CreateStepRequest (e.Thread);
2274 req.Depth = StepDepth.Into;
2275 req.Size = StepSize.Line;
2280 // Step into invoke2
2282 e = GetNextEvent ();
2283 Assert.IsTrue (e is StepEvent);
2284 Assert.AreEqual ("invoke2", (e as StepEvent).Method.Name);
2288 frame = e.Thread.GetFrames () [0];
2292 public void Exceptions () {
2293 Event e = run_until ("exceptions");
2294 var req = vm.CreateExceptionRequest (null);
2299 e = GetNextEvent ();
2300 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2301 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2303 var frames = e.Thread.GetFrames ();
2304 Assert.AreEqual ("exceptions", frames [0].Method.Name);
2307 // exception type filter
2309 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.ArgumentException"));
2312 // Skip the throwing of the second OverflowException
2315 e = GetNextEvent ();
2316 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2317 Assert.AreEqual ("ArgumentException", (e as ExceptionEvent).Exception.Type.Name);
2320 // exception type filter for subclasses
2321 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.Exception"));
2326 e = GetNextEvent ();
2327 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2328 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2331 // Implicit exceptions
2332 req = vm.CreateExceptionRequest (null);
2337 e = GetNextEvent ();
2338 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2339 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
2342 // Single stepping after an exception
2343 req = vm.CreateExceptionRequest (null);
2348 e = GetNextEvent ();
2349 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2350 Assert.AreEqual ("Exception", (e as ExceptionEvent).Exception.Type.Name);
2351 frames = e.Thread.GetFrames ();
2352 Assert.AreEqual ("exceptions2", frames [0].Method.Name);
2355 var sreq = vm.CreateStepRequest (e.Thread);
2356 sreq.Depth = StepDepth.Over;
2357 sreq.Size = StepSize.Line;
2361 e = GetNextEvent ();
2362 Assert.IsInstanceOfType (typeof (StepEvent), e);
2363 frames = e.Thread.GetFrames ();
2364 Assert.AreEqual ("exceptions", frames [0].Method.Name);
2367 // Argument checking
2368 AssertThrows<ArgumentException> (delegate {
2369 vm.CreateExceptionRequest (e.Thread.Type);
2374 public void ExceptionFilter () {
2375 Event e = run_until ("exception_filter");
2377 MethodMirror m = entry_point.DeclaringType.GetMethod ("exception_filter_filter");
2378 Assert.IsNotNull (m);
2380 vm.SetBreakpoint (m, 0);
2384 e = GetNextEvent ();
2385 Assert.AreEqual (EventType.Breakpoint, e.EventType);
2386 Assert.IsTrue (e is BreakpointEvent);
2387 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
2389 var frames = e.Thread.GetFrames ();
2391 Assert.IsTrue (frames [0].Location.SourceFile.IndexOf ("dtest-app.cs") != -1);
2392 Assert.AreEqual ("exception_filter_filter", frames [0].Location.Method.Name);
2394 Assert.AreEqual (0, frames [1].Location.Method.MetadataToken);
2395 Assert.AreEqual (0x0f, frames [1].Location.ILOffset);
2397 Assert.AreEqual ("exception_filter_method", frames [2].Location.Method.Name);
2398 Assert.AreEqual (0x05, frames [2].Location.ILOffset);
2400 Assert.AreEqual (0, frames [3].Location.Method.MetadataToken, 0);
2401 Assert.AreEqual (0, frames [3].Location.ILOffset);
2403 Assert.AreEqual ("exception_filter", frames [4].Location.Method.Name);
2407 public void ExceptionFilter2 () {
2410 Start (new string [] { "dtest-excfilter.exe" });
2412 MethodMirror filter_method = entry_point.DeclaringType.GetMethod ("Filter");
2413 Assert.IsNotNull (filter_method);
2415 MethodMirror test_method = entry_point.DeclaringType.GetMethod ("Test");
2416 Assert.IsNotNull (test_method);
2418 vm.SetBreakpoint (filter_method, 0);
2422 var e = GetNextEvent ();
2423 Assert.AreEqual (EventType.Breakpoint, e.EventType);
2424 Assert.IsTrue (e is BreakpointEvent);
2425 Assert.AreEqual (filter_method.Name, (e as BreakpointEvent).Method.Name);
2427 var frames = e.Thread.GetFrames ();
2429 Assert.AreEqual (4, frames.Count ());
2431 Assert.AreEqual (filter_method.Name, frames [0].Location.Method.Name);
2432 Assert.AreEqual (20, frames [0].Location.LineNumber);
2433 Assert.AreEqual (0, frames [0].Location.ILOffset);
2435 Assert.AreEqual (test_method.Name, frames [1].Location.Method.Name);
2436 Assert.AreEqual (37, frames [1].Location.LineNumber);
2437 Assert.AreEqual (0x0b, frames [1].Location.ILOffset);
2439 Assert.AreEqual (test_method.Name, frames [2].Location.Method.Name);
2440 Assert.AreEqual (33, frames [2].Location.LineNumber);
2441 Assert.AreEqual (0x05, frames [2].Location.ILOffset);
2443 Assert.AreEqual (entry_point.Name, frames [3].Location.Method.Name);
2444 Assert.AreEqual (14, frames [3].Location.LineNumber);
2445 Assert.AreEqual (0x00, frames [3].Location.ILOffset);
2453 public void EventSets () {
2455 // Create two filter which both match the same exception
2457 Event e = run_until ("exceptions");
2459 var req = vm.CreateExceptionRequest (null);
2462 var req2 = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.OverflowException"));
2467 var es = vm.GetNextEventSet ();
2468 Assert.AreEqual (2, es.Events.Length);
2471 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2472 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2475 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2476 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2483 // Test single threaded invokes during processing of nullref exceptions.
2484 // These won't work if the exception handling is done from the sigsegv signal
2485 // handler, since the sigsegv signal is disabled until control returns from the
2489 [Category ("only3")]
2490 public void NullRefExceptionAndSingleThreadedInvoke () {
2491 Event e = run_until ("exceptions");
2492 var req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.NullReferenceException"));
2497 e = GetNextEvent ();
2498 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2499 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
2501 var ex = (e as ExceptionEvent).Exception;
2502 var tostring_method = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2503 ex.InvokeMethod (e.Thread, tostring_method, null, InvokeOptions.SingleThreaded);
2507 public void Domains () {
2510 Start (new string [] { "dtest-app.exe", "domain-test" });
2512 vm.EnableEvents (EventType.AppDomainCreate, EventType.AppDomainUnload, EventType.AssemblyUnload);
2514 Event e = run_until ("domains");
2518 e = GetNextEvent ();
2519 Assert.IsInstanceOfType (typeof (AppDomainCreateEvent), e);
2521 var domain = (e as AppDomainCreateEvent).Domain;
2523 // Run until the callback in the domain
2524 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_in_domain");
2525 Assert.IsNotNull (m);
2526 vm.SetBreakpoint (m, 0);
2530 e = GetNextEvent ();
2531 if (e is BreakpointEvent)
2535 Assert.AreEqual ("invoke_in_domain", (e as BreakpointEvent).Method.Name);
2537 // d_method is from another domain
2538 MethodMirror d_method = (e as BreakpointEvent).Method;
2539 Assert.IsTrue (m != d_method);
2541 var frames = e.Thread.GetFrames ();
2542 Assert.AreEqual ("invoke_in_domain", frames [0].Method.Name);
2543 Assert.AreEqual ("invoke", frames [1].Method.Name);
2544 Assert.AreEqual ("domains", frames [2].Method.Name);
2546 // Test breakpoints on already JITted methods in other domains
2547 m = entry_point.DeclaringType.GetMethod ("invoke_in_domain_2");
2548 Assert.IsNotNull (m);
2549 vm.SetBreakpoint (m, 0);
2553 e = GetNextEvent ();
2554 if (e is BreakpointEvent)
2558 Assert.AreEqual ("invoke_in_domain_2", (e as BreakpointEvent).Method.Name);
2560 // This is empty when receiving the AppDomainCreateEvent
2561 Assert.AreEqual ("domain", domain.FriendlyName);
2563 // Run until the unload
2566 e = GetNextEvent ();
2567 if (e is AssemblyUnloadEvent) {
2573 Assert.IsInstanceOfType (typeof (AppDomainUnloadEvent), e);
2574 Assert.AreEqual (domain, (e as AppDomainUnloadEvent).Domain);
2576 // Run past the unload
2577 e = run_until ("domains_2");
2579 // Test access to unloaded types
2580 // FIXME: Add an exception type for this
2581 AssertThrows<Exception> (delegate {
2582 d_method.DeclaringType.GetValue (d_method.DeclaringType.GetField ("static_i"));
2587 public void DynamicMethods () {
2588 Event e = run_until ("dyn_call");
2590 var m = e.Thread.GetFrames ()[1].Method;
2591 Assert.AreEqual ("dyn_method", m.Name);
2593 // Test access to IL
2594 var body = m.GetMethodBody ();
2596 ILInstruction ins = body.Instructions [0];
2597 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
2598 Assert.AreEqual ("FOO", ins.Operand);
2602 public void RefEmit () {
2605 Start (new string [] { "dtest-app.exe", "ref-emit-test" });
2607 Event e = run_until ("ref_emit_call");
2609 var m = e.Thread.GetFrames ()[1].Method;
2610 Assert.AreEqual ("ref_emit_method", m.Name);
2612 // Test access to IL
2613 var body = m.GetMethodBody ();
2617 ins = body.Instructions [0];
2618 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
2619 Assert.AreEqual ("FOO", ins.Operand);
2621 ins = body.Instructions [1];
2622 Assert.AreEqual (OpCodes.Call, ins.OpCode);
2623 Assert.IsInstanceOfType (typeof (MethodMirror), ins.Operand);
2624 Assert.AreEqual ("ref_emit_call", (ins.Operand as MethodMirror).Name);
2628 public void IsAttached () {
2629 var f = entry_point.DeclaringType.GetField ("is_attached");
2631 Event e = run_until ("Main");
2633 AssertValue (true, entry_point.DeclaringType.GetValue (f));
2637 public void StackTraceInNative () {
2638 // Check that stack traces can be produced for threads in native code
2641 Start (new string [] { "dtest-app.exe", "frames-in-native" });
2643 var e = run_until ("frames_in_native");
2645 // FIXME: This is racy
2652 StackFrame[] frames = e.Thread.GetFrames ();
2654 int frame_index = -1;
2655 for (int i = 0; i < frames.Length; ++i) {
2656 if (frames [i].Method.Name == "Sleep") {
2662 Assert.IsTrue (frame_index != -1);
2663 Assert.AreEqual ("Sleep", frames [frame_index].Method.Name);
2664 Assert.AreEqual ("frames_in_native", frames [frame_index + 1].Method.Name);
2665 Assert.AreEqual ("Main", frames [frame_index + 2].Method.Name);
2667 // Check that invokes are disabled for such threads
2668 TypeMirror t = frames [frame_index + 1].Method.DeclaringType;
2671 var m = t.GetMethod ("invoke_static_return_void");
2672 AssertThrows<InvalidOperationException> (delegate {
2673 t.InvokeMethod (e.Thread, m, null);
2678 public void VirtualMachine_CreateEnumMirror () {
2679 var e = run_until ("o1");
2680 var frame = e.Thread.GetFrames () [0];
2682 object val = frame.GetThis ();
2683 Assert.IsTrue (val is ObjectMirror);
2684 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
2685 ObjectMirror o = (val as ObjectMirror);
2687 FieldInfoMirror field = o.Type.GetField ("field_enum");
2688 Value f = o.GetValue (field);
2689 TypeMirror enumType = (f as EnumMirror).Type;
2691 o.SetValue (field, vm.CreateEnumMirror (enumType, vm.CreateValue (1)));
2692 f = o.GetValue (field);
2693 Assert.AreEqual (1, (f as EnumMirror).Value);
2695 // Argument checking
2696 AssertThrows<ArgumentNullException> (delegate () {
2697 vm.CreateEnumMirror (enumType, null);
2700 AssertThrows<ArgumentNullException> (delegate () {
2701 vm.CreateEnumMirror (null, vm.CreateValue (1));
2705 AssertThrows<ArgumentException> (delegate () {
2706 vm.CreateEnumMirror (enumType, vm.CreateValue (null));
2709 // value of a wrong type
2710 AssertThrows<ArgumentException> (delegate () {
2711 vm.CreateEnumMirror (enumType, vm.CreateValue ((long)1));
2716 public void VirtualMachine_EnableEvents_Breakpoint () {
2717 AssertThrows<ArgumentException> (delegate () {
2718 vm.EnableEvents (EventType.Breakpoint);
2723 public void SingleStepRegress654694 () {
2726 MethodMirror m = entry_point.DeclaringType.GetMethod ("ss_regress_654694");
2727 foreach (Location l in m.Locations) {
2728 if (l.ILOffset > 0 && il_offset == -1)
2729 il_offset = l.ILOffset;
2732 Event e = run_until ("ss_regress_654694");
2734 Assert.IsNotNull (m);
2735 vm.SetBreakpoint (m, il_offset);
2739 e = GetNextEvent ();
2740 Assert.IsTrue (e is BreakpointEvent);
2742 var req = vm.CreateStepRequest (e.Thread);
2743 req.Depth = StepDepth.Over;
2744 req.Size = StepSize.Line;
2749 e = GetNextEvent ();
2750 Assert.IsTrue (e is StepEvent);
2756 public void DebugBreak () {
2757 vm.EnableEvents (EventType.UserBreak);
2762 var e = GetNextEvent ();
2763 Assert.IsTrue (e is UserBreakEvent);
2767 public void DebugLog () {
2768 vm.EnableEvents (EventType.UserLog);
2773 var e = GetNextEvent ();
2774 Assert.IsTrue (e is UserLogEvent);
2775 var le = e as UserLogEvent;
2777 Assert.AreEqual (5, le.Level);
2778 Assert.AreEqual ("A", le.Category);
2779 Assert.AreEqual ("B", le.Message);
2783 public void TypeGetMethodsByNameFlags () {
2785 var assembly = entry_point.DeclaringType.Assembly;
2786 var type = assembly.GetType ("Tests3");
2788 Assert.IsNotNull (type);
2790 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Public, false);
2791 Assert.AreEqual (1, mm.Length, "#1");
2792 Assert.AreEqual ("M1", mm[0].Name, "#2");
2794 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.NonPublic, false);
2795 Assert.AreEqual (1, mm.Length, "#3");
2796 Assert.AreEqual ("M2", mm[0].Name, "#4");
2798 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public, false);
2799 Assert.AreEqual (7, mm.Length, "#5"); //M3 plus Equals, GetHashCode, GetType, ToString, .ctor
2801 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, false);
2802 Assert.AreEqual (2, mm.Length, "#7");
2804 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
2805 Assert.AreEqual (1, mm.Length, "#9");
2807 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
2808 Assert.AreEqual (5, mm.Length, "#11");
2811 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Public, false);
2812 Assert.AreEqual (1, mm.Length, "#12");
2813 Assert.AreEqual ("M1", mm[0].Name, "#13");
2815 mm = type.GetMethodsByNameFlags ("m1", BindingFlags.Static | BindingFlags.Public, true);
2816 Assert.AreEqual (1, mm.Length, "#14");
2817 Assert.AreEqual ("M1", mm[0].Name, "#15");
2819 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, false);
2820 Assert.AreEqual (1, mm.Length, "#16");
2821 Assert.AreEqual ("M1", mm[0].Name, "#17");
2825 [Category ("only88")]
2826 public void TypeLoadSourceFileFilter () {
2827 Event e = run_until ("type_load");
2829 if (!vm.Version.AtLeast (2, 7))
2832 string srcfile = (e as BreakpointEvent).Method.DeclaringType.GetSourceFiles (true)[0];
2834 var req = vm.CreateTypeLoadRequest ();
2835 req.SourceFileFilter = new string [] { srcfile.ToUpper () };
2839 e = GetNextEvent ();
2840 Assert.IsTrue (e is TypeLoadEvent);
2841 Assert.AreEqual ("TypeLoadClass", (e as TypeLoadEvent).Type.FullName);
2845 public void TypeLoadTypeNameFilter () {
2846 Event e = run_until ("type_load");
2848 var req = vm.CreateTypeLoadRequest ();
2849 req.TypeNameFilter = new string [] { "TypeLoadClass2" };
2853 e = GetNextEvent ();
2854 Assert.IsTrue (e is TypeLoadEvent);
2855 Assert.AreEqual ("TypeLoadClass2", (e as TypeLoadEvent).Type.FullName);
2859 public void GetTypesForSourceFile () {
2862 var types = vm.GetTypesForSourceFile ("dtest-app.cs", false);
2863 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
2864 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
2866 types = vm.GetTypesForSourceFile ("DTEST-app.cs", true);
2867 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
2868 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
2872 public void GetTypesNamed () {
2875 var types = vm.GetTypes ("Tests", false);
2876 Assert.AreEqual (1, types.Count);
2877 Assert.AreEqual ("Tests", types [0].FullName);
2879 types = vm.GetTypes ("System.Exception", false);
2880 Assert.AreEqual (1, types.Count);
2881 Assert.AreEqual ("System.Exception", types [0].FullName);
2885 public void String_GetChars () {
2889 var e = run_until ("arg2");
2891 var frame = e.Thread.GetFrames () [0];
2893 val = frame.GetArgument (0);
2894 Assert.IsTrue (val is StringMirror);
2895 AssertValue ("FOO", val);
2896 var s = (val as StringMirror);
2897 Assert.AreEqual (3, s.Length);
2899 var c = s.GetChars (0, 2);
2900 Assert.AreEqual (2, c.Length);
2901 Assert.AreEqual ('F', c [0]);
2902 Assert.AreEqual ('O', c [1]);
2904 AssertThrows<ArgumentException> (delegate () {
2910 public void GetInterfaces () {
2911 var e = run_until ("arg2");
2913 var frame = e.Thread.GetFrames () [0];
2915 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
2916 var ifaces = cl1.GetInterfaces ();
2917 Assert.AreEqual (1, ifaces.Length);
2918 Assert.AreEqual ("ITest", ifaces [0].Name);
2920 var cl2 = cl1.GetMethod ("Baz").ReturnType;
2921 var ifaces2 = cl2.GetInterfaces ();
2922 Assert.AreEqual (1, ifaces2.Length);
2923 Assert.AreEqual ("ITest`1", ifaces2 [0].Name);
2927 public void GetInterfaceMap () {
2928 var e = run_until ("arg2");
2930 var frame = e.Thread.GetFrames () [0];
2932 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
2933 var iface = cl1.Assembly.GetType ("ITest");
2934 var map = cl1.GetInterfaceMap (iface);
2935 Assert.AreEqual (cl1, map.TargetType);
2936 Assert.AreEqual (iface, map.InterfaceType);
2937 Assert.AreEqual (2, map.InterfaceMethods.Length);
2938 Assert.AreEqual (2, map.TargetMethods.Length);
2942 public void StackAlloc_Breakpoints_Regress2775 () {
2943 // Check that breakpoints on arm don't overwrite stackalloc-ed memory
2944 var e = run_until ("regress_2755");
2946 var frame = e.Thread.GetFrames () [0];
2948 // This breaks at the call site
2949 vm.SetBreakpoint (m, m.Locations [2].ILOffset);
2952 var e2 = GetNextEvent ();
2953 Assert.IsTrue (e2 is BreakpointEvent);
2955 e = run_until ("regress_2755_3");
2956 frame = e.Thread.GetFrames () [1];
2957 var res = frame.GetValue (m.GetLocal ("sum"));
2958 AssertValue (0, res);
2962 public void MethodInfo () {
2963 Event e = run_until ("locals2");
2965 StackFrame frame = e.Thread.GetFrames () [0];
2966 var m = frame.Method;
2968 Assert.IsTrue (m.IsGenericMethod);
2969 Assert.IsFalse (m.IsGenericMethodDefinition);
2971 var args = m.GetGenericArguments ();
2972 Assert.AreEqual (1, args.Length);
2973 Assert.AreEqual ("String", args [0].Name);
2975 var gmd = m.GetGenericMethodDefinition ();
2976 Assert.IsTrue (gmd.IsGenericMethod);
2977 Assert.IsTrue (gmd.IsGenericMethodDefinition);
2978 Assert.AreEqual (gmd, gmd.GetGenericMethodDefinition ());
2980 args = gmd.GetGenericArguments ();
2981 Assert.AreEqual (1, args.Length);
2982 Assert.AreEqual ("T", args [0].Name);