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
22 public class DebuggerTests
25 MethodMirror entry_point;
26 StepEventRequest step_req;
29 void AssertThrows<ExType> (Action del) where ExType : Exception {
37 Assert.IsTrue (thrown);
40 // No other way to pass arguments to the tests ?
41 public static bool listening = Environment.GetEnvironmentVariable ("DBG_SUSPEND") != null;
42 public static string runtime = Environment.GetEnvironmentVariable ("DBG_RUNTIME");
43 public static string agent_args = Environment.GetEnvironmentVariable ("DBG_AGENT_ARGS");
45 Event GetNextEvent () {
46 var es = vm.GetNextEventSet ();
47 Assert.AreEqual (1, es.Events.Length);
51 void Start (params string[] args) {
55 void Start (bool forceExit, params string[] args) {
56 this.forceExit = forceExit;
59 var pi = new Diag.ProcessStartInfo ();
62 pi.FileName = runtime;
65 pi.Arguments = String.Join (" ", args);
66 vm = VirtualMachineManager.Launch (pi, new LaunchOptions { AgentArgs = agent_args });
68 var ep = new IPEndPoint (IPAddress.Any, 10000);
69 Console.WriteLine ("Listening on " + ep + "...");
70 vm = VirtualMachineManager.Listen (ep);
73 var load_req = vm.CreateAssemblyLoadRequest ();
76 Event vmstart = GetNextEvent ();
77 Assert.AreEqual (EventType.VMStart, vmstart.EventType);
86 /* Find out the entry point */
90 if (e is AssemblyLoadEvent) {
91 AssemblyLoadEvent ae = (AssemblyLoadEvent)e;
92 entry_point = ae.Assembly.EntryPoint;
93 if (entry_point != null)
103 BreakpointEvent run_until (string name) {
105 MethodMirror m = entry_point.DeclaringType.GetMethod (name);
106 Assert.IsNotNull (m);
107 //Console.WriteLine ("X: " + name + " " + m.ILOffsets.Count + " " + m.Locations.Count);
108 var req = vm.SetBreakpoint (m, m.ILOffsets [0]);
115 if (e is BreakpointEvent)
121 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
122 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
124 return (e as BreakpointEvent);
127 Event single_step (ThreadMirror t) {
128 var req = vm.CreateStepRequest (t);
132 Event e = GetNextEvent ();
133 Assert.IsTrue (e is StepEvent);
140 Event step_until (ThreadMirror t, string method_name) {
144 if ((e as StepEvent).Method.Name == method_name)
150 void check_arg_val (StackFrame frame, int pos, Type type, object eval) {
151 object val = frame.GetArgument (pos);
152 Assert.IsTrue (val is PrimitiveValue);
153 object v = (val as PrimitiveValue).Value;
154 Assert.AreEqual (type, v.GetType ());
156 Assert.IsTrue (Math.Abs ((float)eval - (float)v) < 0.0001);
157 else if (eval is double)
158 Assert.IsTrue (Math.Abs ((double)eval - (double)v) < 0.0001);
160 Assert.AreEqual (eval, v);
163 void AssertValue (object expected, object val) {
164 if (expected is string) {
165 Assert.IsTrue (val is StringMirror);
166 Assert.AreEqual (expected, (val as StringMirror).Value);
167 } else if (val is StructMirror && (val as StructMirror).Type.Name == "IntPtr") {
168 AssertValue (expected, (val as StructMirror).Fields [0]);
170 Assert.IsTrue (val is PrimitiveValue);
171 Assert.AreEqual (expected, (val as PrimitiveValue).Value);
176 public void SetUp () {
177 ThreadMirror.NativeTransitions = false;
178 Start (new string [] { "dtest-app.exe" });
182 public void TearDown () {
186 if (step_req != null)
194 Event e = GetNextEvent ();
196 if (e is VMDeathEvent)
205 public void SimpleBreakpoint () {
208 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp1");
209 Assert.IsNotNull (m);
211 vm.SetBreakpoint (m, 0);
216 Assert.AreEqual (EventType.Breakpoint, e.EventType);
217 Assert.IsTrue (e is BreakpointEvent);
218 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
221 AssertThrows<ArgumentException> (delegate {
223 vm.SetBreakpoint (m, 2);
228 public void BreakpointsSameLocation () {
229 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp2");
230 Assert.IsNotNull (m);
232 vm.SetBreakpoint (m, 0);
233 vm.SetBreakpoint (m, 0);
237 var es = vm.GetNextEventSet ();
238 Assert.AreEqual (2, es.Events.Length);
239 Assert.IsTrue (es [0] is BreakpointEvent);
240 Assert.AreEqual (m, (es [0] as BreakpointEvent).Method);
242 Assert.IsTrue (es [1] is BreakpointEvent);
243 Assert.AreEqual (m.Name, (es [1] as BreakpointEvent).Method.Name);
247 public void BreakpointAlreadyJITted () {
248 Event e = run_until ("bp1");
250 /* Place a breakpoint on bp3 */
251 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp3");
252 Assert.IsNotNull (m);
253 vm.SetBreakpoint (m, 0);
255 /* Same with generic instances */
256 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp7");
257 Assert.IsNotNull (m2);
258 vm.SetBreakpoint (m2, 0);
263 Assert.AreEqual (EventType.Breakpoint, e.EventType);
264 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
268 /* Non-shared instance */
270 Assert.AreEqual (EventType.Breakpoint, e.EventType);
271 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
275 /* Shared instance */
277 Assert.AreEqual (EventType.Breakpoint, e.EventType);
278 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
282 public void ClearBreakpoint () {
285 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
286 Assert.IsNotNull (m);
287 EventRequest req1 = vm.SetBreakpoint (m, 0);
288 EventRequest req2 = vm.SetBreakpoint (m, 0);
290 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
291 Assert.IsNotNull (m2);
292 vm.SetBreakpoint (m2, 0);
297 var es = vm.GetNextEventSet ();
298 Assert.AreEqual (2, es.Events.Length);
299 Assert.AreEqual (EventType.Breakpoint, es [0].EventType);
300 Assert.AreEqual (m.Name, (es [0] as BreakpointEvent).Method.Name);
301 Assert.AreEqual (EventType.Breakpoint, es [1].EventType);
302 Assert.AreEqual (m.Name, (es [1] as BreakpointEvent).Method.Name);
304 /* Clear one of them */
310 Assert.AreEqual (EventType.Breakpoint, e.EventType);
311 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
313 /* Clear the other */
319 Assert.AreEqual (EventType.Breakpoint, e.EventType);
320 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
324 public void ClearAllBreakpoints () {
327 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
328 Assert.IsNotNull (m);
329 vm.SetBreakpoint (m, 0);
331 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
332 Assert.IsNotNull (m2);
333 vm.SetBreakpoint (m2, 0);
335 vm.ClearAllBreakpoints ();
340 Assert.IsTrue (!(e is BreakpointEvent));
341 if (e is VMDeathEvent)
346 public void BreakpointOnGShared () {
349 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp6");
350 Assert.IsNotNull (m);
352 vm.SetBreakpoint (m, 0);
357 Assert.AreEqual (EventType.Breakpoint, e.EventType);
358 Assert.IsTrue (e is BreakpointEvent);
359 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
361 // Breakpoint on an open generic method of a closed generic class (#3422)
362 var frame = e.Thread.GetFrames ()[0];
363 var ginst = frame.GetValue (frame.Method.GetLocal ("gc"));
364 var m2 = (ginst as ObjectMirror).Type.GetMethod ("bp");
365 vm.SetBreakpoint (m2, 0);
370 Assert.AreEqual (EventType.Breakpoint, e.EventType);
371 Assert.IsTrue (e is BreakpointEvent);
372 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
375 void assert_location (Event e, string method) {
376 Assert.IsTrue (e is StepEvent);
377 Assert.AreEqual (method, (e as StepEvent).Method.Name);
380 StepEventRequest create_step (Event e) {
381 var req = vm.CreateStepRequest (e.Thread);
387 public void SingleStepping () {
388 Event e = run_until ("single_stepping");
390 var req = create_step (e);
393 // Step over 'bool b = true'
395 assert_location (e, "single_stepping");
402 assert_location (e, "ss1");
409 assert_location (e, "single_stepping");
413 assert_location (e, "single_stepping");
417 assert_location (e, "ss3");
419 // Step back into single_stepping
421 assert_location (e, "single_stepping");
423 // Step into ss3_2 ()
425 assert_location (e, "ss3_2");
427 // Step over ss3_2_2 ()
429 assert_location (e, "ss3_2");
431 // Recreate the request
438 // Step back into single_stepping () with the new request
440 assert_location (e, "single_stepping");
444 assert_location (e, "ss4");
449 // Change to StepSize.Line
451 req.Depth = StepDepth.Over;
452 req.Size = StepSize.Line;
455 // Step over ss1 (); ss1 ();
460 req.Depth = StepDepth.Into;
464 assert_location (e, "ss2");
469 e = run_until ("ss5");
471 // Add an assembly filter
472 req.AssemblyFilter = new AssemblyMirror [] { (e as BreakpointEvent).Method.DeclaringType.Assembly };
478 // Step into is_even, skipping the linq stuff
480 assert_location (e, "is_even");
482 // FIXME: Check that single stepping works with lock (obj)
486 e = run_until ("ss6");
488 req = create_step (e);
489 req.Depth = StepDepth.Over;
492 // Check that single stepping works in out-of-line bblocks
495 assert_location (e, "ss6");
498 // Check that a step over stops at an EH clause
499 e = run_until ("ss7_2");
500 req = create_step (e);
501 req.Depth = StepDepth.Out;
504 assert_location (e, "ss7");
506 req = create_step (e);
507 req.Depth = StepDepth.Over;
510 assert_location (e, "ss7");
513 // Check that stepping stops between nested calls
514 e = run_until ("ss_nested_2");
516 assert_location (e, "ss_nested");
518 assert_location (e, "ss_nested_1");
520 assert_location (e, "ss_nested");
521 // Check that step over steps over nested calls
523 assert_location (e, "ss_nested");
525 assert_location (e, "ss_nested_3");
528 // Check DebuggerStepThrough support
529 e = run_until ("ss_step_through");
530 req = create_step (e);
531 req.Filter = StepFilter.DebuggerStepThrough;
533 // Step through step_through_1 ()
535 assert_location (e, "ss_step_through");
536 // Step through StepThroughClass.step_through_2 ()
538 assert_location (e, "ss_step_through");
540 req.Filter = StepFilter.None;
542 assert_location (e, "step_through_3");
545 // Check that step-over doesn't stop at inner frames with recursive functions
546 e = run_until ("ss_recursive");
547 req = create_step (e);
551 var f = e.Thread.GetFrames () [0];
552 assert_location (e, "ss_recursive");
553 AssertValue (1, f.GetValue (f.Method.GetLocal ("n")));
559 public void MethodEntryExit () {
560 run_until ("single_stepping");
562 var req1 = vm.CreateMethodEntryRequest ();
563 var req2 = vm.CreateMethodExitRequest ();
569 Event e = GetNextEvent ();
570 Assert.IsTrue (e is MethodEntryEvent);
571 Assert.AreEqual ("ss1", (e as MethodEntryEvent).Method.Name);
575 Assert.IsTrue (e is MethodExitEvent);
576 Assert.AreEqual ("ss1", (e as MethodExitEvent).Method.Name);
583 public void CountFilter () {
584 run_until ("single_stepping");
586 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("ss3");
587 Assert.IsNotNull (m2);
588 vm.SetBreakpoint (m2, 0);
590 var req1 = vm.CreateMethodEntryRequest ();
594 // Enter ss2, ss1 is skipped
596 Event e = GetNextEvent ();
597 Assert.IsTrue (e is MethodEntryEvent);
598 Assert.AreEqual ("ss2", (e as MethodEntryEvent).Method.Name);
600 // Breakpoint on ss3, the entry event is no longer reported
603 Assert.IsTrue (e is BreakpointEvent);
609 public void Arguments () {
612 var e = run_until ("arg1");
614 StackFrame frame = e.Thread.GetFrames () [0];
616 check_arg_val (frame, 0, typeof (sbyte), SByte.MaxValue - 5);
617 check_arg_val (frame, 1, typeof (byte), Byte.MaxValue - 5);
618 check_arg_val (frame, 2, typeof (bool), true);
619 check_arg_val (frame, 3, typeof (short), Int16.MaxValue - 5);
620 check_arg_val (frame, 4, typeof (ushort), UInt16.MaxValue - 5);
621 check_arg_val (frame, 5, typeof (char), 'F');
622 check_arg_val (frame, 6, typeof (int), Int32.MaxValue - 5);
623 check_arg_val (frame, 7, typeof (uint), UInt32.MaxValue - 5);
624 check_arg_val (frame, 8, typeof (long), Int64.MaxValue - 5);
625 check_arg_val (frame, 9, typeof (ulong), UInt64.MaxValue - 5);
626 check_arg_val (frame, 10, typeof (float), 1.2345f);
627 check_arg_val (frame, 11, typeof (double), 6.78910);
629 e = run_until ("arg2");
631 frame = e.Thread.GetFrames () [0];
634 val = frame.GetArgument (0);
635 AssertValue ("FOO", val);
636 Assert.AreEqual ("String", (val as ObjectMirror).Type.Name);
639 val = frame.GetArgument (1);
640 AssertValue (null, val);
643 val = frame.GetArgument (2);
644 AssertValue ("BLA", val);
647 val = frame.GetArgument (3);
648 AssertValue (42, val);
651 val = frame.GetArgument (4);
652 Assert.IsTrue (val is ObjectMirror);
653 Assert.AreEqual ("GClass`1", (val as ObjectMirror).Type.Name);
656 val = frame.GetArgument (5);
657 Assert.IsTrue (val is ObjectMirror);
658 Assert.AreEqual ("Object", (val as ObjectMirror).Type.Name);
660 // this on static methods
661 val = frame.GetThis ();
662 AssertValue (null, val);
664 e = run_until ("arg3");
666 frame = e.Thread.GetFrames () [0];
669 val = frame.GetThis ();
670 Assert.IsTrue (val is ObjectMirror);
671 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
673 // objref in register
674 val = frame.GetArgument (0);
675 AssertValue ("BLA", val);
679 public void Arrays () {
682 var e = run_until ("o2");
684 StackFrame frame = e.Thread.GetFrames () [0];
687 val = frame.GetArgument (0);
688 Assert.IsTrue (val is ArrayMirror);
689 ArrayMirror arr = val as ArrayMirror;
690 Assert.AreEqual (2, arr.Length);
691 AssertValue ("BAR", arr [0]);
692 AssertValue ("BAZ", arr [1]);
694 var vals = arr.GetValues (0, 2);
695 Assert.AreEqual (2, vals.Count);
696 AssertValue ("BAR", vals [0]);
697 AssertValue ("BAZ", vals [1]);
699 arr [0] = vm.RootDomain.CreateString ("ABC");
700 AssertValue ("ABC", arr [0]);
702 arr [0] = vm.CreateValue (null);
703 AssertValue (null, arr [0]);
705 arr.SetValues (0, new Value [] { vm.RootDomain.CreateString ("D1"), vm.RootDomain.CreateString ("D2") });
706 AssertValue ("D1", arr [0]);
707 AssertValue ("D2", arr [1]);
710 val = frame.GetArgument (1);
711 Assert.IsTrue (val is ArrayMirror);
712 arr = val as ArrayMirror;
713 Assert.AreEqual (2, arr.Length);
714 AssertValue (42, arr [0]);
715 AssertValue (43, arr [1]);
718 AssertThrows<IndexOutOfRangeException> (delegate () {
722 AssertThrows<IndexOutOfRangeException> (delegate () {
723 val = arr [Int32.MinValue];
726 AssertThrows<IndexOutOfRangeException> (delegate () {
727 vals = arr.GetValues (0, 3);
730 AssertThrows<IndexOutOfRangeException> (delegate () {
731 arr [2] = vm.CreateValue (null);
734 AssertThrows<IndexOutOfRangeException> (delegate () {
735 arr [Int32.MinValue] = vm.CreateValue (null);
738 AssertThrows<IndexOutOfRangeException> (delegate () {
739 arr.SetValues (0, new Value [] { null, null, null });
743 val = frame.GetArgument (2);
744 Assert.IsTrue (val is ArrayMirror);
745 arr = val as ArrayMirror;
746 Assert.AreEqual (2, arr.Rank);
747 Assert.AreEqual (4, arr.Length);
748 Assert.AreEqual (2, arr.GetLength (0));
749 Assert.AreEqual (2, arr.GetLength (1));
750 Assert.AreEqual (0, arr.GetLowerBound (0));
751 Assert.AreEqual (0, arr.GetLowerBound (1));
752 vals = arr.GetValues (0, 4);
753 AssertValue (1, vals [0]);
754 AssertValue (2, vals [1]);
755 AssertValue (3, vals [2]);
756 AssertValue (4, vals [3]);
758 val = frame.GetArgument (3);
759 Assert.IsTrue (val is ArrayMirror);
760 arr = val as ArrayMirror;
761 Assert.AreEqual (2, arr.Rank);
762 Assert.AreEqual (4, arr.Length);
763 Assert.AreEqual (2, arr.GetLength (0));
764 Assert.AreEqual (2, arr.GetLength (1));
765 Assert.AreEqual (1, arr.GetLowerBound (0));
766 Assert.AreEqual (3, arr.GetLowerBound (1));
768 AssertThrows<ArgumentOutOfRangeException> (delegate () {
771 AssertThrows<ArgumentOutOfRangeException> (delegate () {
775 AssertThrows<ArgumentOutOfRangeException> (delegate () {
776 arr.GetLowerBound (-1);
778 AssertThrows<ArgumentOutOfRangeException> (delegate () {
779 arr.GetLowerBound (2);
782 // arrays treated as generic collections
783 val = frame.GetArgument (4);
784 Assert.IsTrue (val is ArrayMirror);
785 arr = val as ArrayMirror;
789 public void Object_GetValue () {
790 var e = run_until ("o1");
791 var frame = e.Thread.GetFrames () [0];
793 object val = frame.GetThis ();
794 Assert.IsTrue (val is ObjectMirror);
795 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
796 ObjectMirror o = (val as ObjectMirror);
798 TypeMirror t = o.Type;
801 object f = o.GetValue (t.GetField ("field_i"));
803 f = o.GetValue (t.GetField ("field_s"));
804 AssertValue ("S", f);
805 f = o.GetValue (t.GetField ("field_enum"));
806 Assert.IsTrue (f is EnumMirror);
807 Assert.AreEqual (1, (f as EnumMirror).Value);
808 Assert.AreEqual ("B", (f as EnumMirror).StringValue);
810 // Inherited object fields
811 TypeMirror parent = t.BaseType;
812 f = o.GetValue (parent.GetField ("base_field_i"));
814 f = o.GetValue (parent.GetField ("base_field_s"));
815 AssertValue ("T", f);
818 f = o.GetValue (o.Type.GetField ("static_i"));
822 ObjectMirror o2 = frame.GetValue (frame.Method.GetParameters ()[1]) as ObjectMirror;
823 Assert.AreEqual ("GClass`1", o2.Type.Name);
824 TypeMirror t2 = o2.Type;
825 f = o2.GetValue (t2.GetField ("field"));
828 ObjectMirror o3 = frame.GetValue (frame.Method.GetParameters ()[2]) as ObjectMirror;
829 Assert.AreEqual ("GClass`1", o3.Type.Name);
830 TypeMirror t3 = o3.Type;
831 f = o3.GetValue (t3.GetField ("field"));
832 AssertValue ("FOO", f);
835 AssertThrows<ArgumentNullException> (delegate () {
841 public void Object_GetValues () {
842 var e = run_until ("o1");
843 var frame = e.Thread.GetFrames () [0];
845 object val = frame.GetThis ();
846 Assert.IsTrue (val is ObjectMirror);
847 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
848 ObjectMirror o = (val as ObjectMirror);
850 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
852 TypeMirror t = o.Type;
854 object[] vals = o.GetValues (new FieldInfoMirror [] { t.GetField ("field_i"), t.GetField ("field_s") });
858 AssertValue ("S", f);
861 AssertThrows<ArgumentNullException> (delegate () {
865 AssertThrows<ArgumentNullException> (delegate () {
866 o.GetValues (new FieldInfoMirror [] { null });
869 // field of another class
870 AssertThrows<ArgumentException> (delegate () {
871 o.GetValue (val2.Type.GetField ("field_j"));
875 void TestSetValue (ObjectMirror o, string field_name, object val) {
877 o.SetValue (o.Type.GetField (field_name), vm.RootDomain.CreateString ((string)val));
879 o.SetValue (o.Type.GetField (field_name), vm.CreateValue (val));
880 Value f = o.GetValue (o.Type.GetField (field_name));
881 AssertValue (val, f);
885 public void Object_SetValues () {
886 var e = run_until ("o1");
887 var frame = e.Thread.GetFrames () [0];
889 object val = frame.GetThis ();
890 Assert.IsTrue (val is ObjectMirror);
891 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
892 ObjectMirror o = (val as ObjectMirror);
894 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
896 TestSetValue (o, "field_i", 22);
897 TestSetValue (o, "field_bool1", false);
898 TestSetValue (o, "field_bool2", true);
899 TestSetValue (o, "field_char", 'B');
900 TestSetValue (o, "field_byte", (byte)129);
901 TestSetValue (o, "field_sbyte", (sbyte)-33);
902 TestSetValue (o, "field_short", (short)(Int16.MaxValue - 5));
903 TestSetValue (o, "field_ushort", (ushort)(UInt16.MaxValue - 5));
904 TestSetValue (o, "field_long", Int64.MaxValue - 5);
905 TestSetValue (o, "field_ulong", (ulong)(UInt64.MaxValue - 5));
906 TestSetValue (o, "field_float", 6.28f);
907 TestSetValue (o, "field_double", 6.28);
908 TestSetValue (o, "static_i", 23);
909 TestSetValue (o, "field_s", "CDEF");
914 f = o.GetValue (o.Type.GetField ("field_intptr"));
915 Assert.IsInstanceOfType (typeof (StructMirror), f);
916 AssertValue (Int32.MaxValue - 5, (f as StructMirror).Fields [0]);
919 FieldInfoMirror field = o.Type.GetField ("field_enum");
920 f = o.GetValue (field);
921 (f as EnumMirror).Value = 5;
922 o.SetValue (field, f);
923 f = o.GetValue (field);
924 Assert.AreEqual (5, (f as EnumMirror).Value);
927 o.SetValue (o.Type.GetField ("field_s"), vm.CreateValue (null));
928 f = o.GetValue (o.Type.GetField ("field_s"));
929 AssertValue (null, f);
932 field = o.Type.GetField ("generic_field_struct");
933 f = o.GetValue (field);
934 o.SetValue (field, f);
937 field = o.Type.GetField ("field_nullable");
938 f = o.GetValue (field);
939 AssertValue (0, (f as StructMirror).Fields [0]);
940 AssertValue (false, (f as StructMirror).Fields [1]);
941 o.SetValue (field, vm.CreateValue (6));
942 f = o.GetValue (field);
943 AssertValue (6, (f as StructMirror).Fields [0]);
944 AssertValue (true, (f as StructMirror).Fields [1]);
945 o.SetValue (field, vm.CreateValue (null));
946 f = o.GetValue (field);
947 AssertValue (0, (f as StructMirror).Fields [0]);
948 AssertValue (false, (f as StructMirror).Fields [1]);
951 AssertThrows<ArgumentNullException> (delegate () {
952 o.SetValues (null, new Value [0]);
955 AssertThrows<ArgumentNullException> (delegate () {
956 o.SetValues (new FieldInfoMirror [0], null);
959 AssertThrows<ArgumentNullException> (delegate () {
960 o.SetValues (new FieldInfoMirror [] { null }, new Value [1] { null });
963 // vtype with a wrong type
964 AssertThrows<ArgumentException> (delegate () {
965 o.SetValue (o.Type.GetField ("field_struct"), o.GetValue (o.Type.GetField ("field_enum")));
968 // reference type not assignment compatible
969 AssertThrows<ArgumentException> (delegate () {
970 o.SetValue (o.Type.GetField ("field_class"), o);
973 // field of another class
974 AssertThrows<ArgumentException> (delegate () {
975 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
980 public void Type_SetValue () {
981 var e = run_until ("o1");
982 var frame = e.Thread.GetFrames () [0];
985 object val = frame.GetThis ();
986 Assert.IsTrue (val is ObjectMirror);
987 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
988 ObjectMirror o = (val as ObjectMirror);
990 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
992 o.Type.SetValue (o.Type.GetField ("static_i"), vm.CreateValue (55));
993 f = o.Type.GetValue (o.Type.GetField ("static_i"));
996 o.Type.SetValue (o.Type.GetField ("static_s"), vm.RootDomain.CreateString ("B"));
997 f = o.Type.GetValue (o.Type.GetField ("static_s"));
998 AssertValue ("B", f);
1000 // Argument checking
1001 AssertThrows<ArgumentNullException> (delegate () {
1002 o.Type.SetValue (null, vm.CreateValue (0));
1005 AssertThrows<ArgumentNullException> (delegate () {
1006 o.Type.SetValue (o.Type.GetField ("static_i"), null);
1009 // field of another class
1010 AssertThrows<ArgumentException> (delegate () {
1011 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
1016 public void TypeInfo () {
1017 Event e = run_until ("ti2");
1018 StackFrame frame = e.Thread.GetFrames () [0];
1023 t = frame.Method.GetParameters ()[0].ParameterType;
1025 Assert.AreEqual ("String[]", t.Name);
1026 Assert.AreEqual ("string[]", t.CSharpName);
1027 Assert.AreEqual ("Array", t.BaseType.Name);
1028 Assert.AreEqual (true, t.HasElementType);
1029 Assert.AreEqual (true, t.IsArray);
1030 Assert.AreEqual (1, t.GetArrayRank ());
1031 Assert.AreEqual ("String", t.GetElementType ().Name);
1033 t = frame.Method.GetParameters ()[2].ParameterType;
1035 Assert.AreEqual ("Int32[,]", t.Name);
1037 //Assert.AreEqual ("int[,]", t.CSharpName);
1038 Assert.AreEqual ("Array", t.BaseType.Name);
1039 Assert.AreEqual (true, t.HasElementType);
1040 Assert.AreEqual (true, t.IsArray);
1041 Assert.AreEqual (2, t.GetArrayRank ());
1042 Assert.AreEqual ("Int32", t.GetElementType ().Name);
1045 t = frame.Method.GetParameters ()[3].ParameterType;
1047 //Assert.AreEqual ("Int32&", t.Name);
1048 //Assert.AreEqual (true, t.IsByRef);
1049 //Assert.AreEqual (true, t.HasElementType);
1052 t = frame.Method.GetParameters ()[4].ParameterType;
1054 //Assert.AreEqual ("Int32*", t.Name);
1055 Assert.AreEqual (true, t.IsPointer);
1056 Assert.AreEqual (true, t.HasElementType);
1057 Assert.AreEqual ("Int32", t.GetElementType ().Name);
1058 Assert.AreEqual (false, t.IsPrimitive);
1061 t = frame.Method.GetParameters ()[5].ParameterType;
1062 Assert.AreEqual (true, t.IsPrimitive);
1065 t = frame.Method.GetParameters ()[6].ParameterType;
1066 Assert.AreEqual ("AStruct", t.Name);
1067 Assert.AreEqual (false, t.IsPrimitive);
1068 Assert.AreEqual (true, t.IsValueType);
1069 Assert.AreEqual (false, t.IsClass);
1072 t = frame.Method.GetParameters ()[7].ParameterType;
1073 Assert.AreEqual ("Tests", t.Name);
1074 var nested = (from nt in t.GetNestedTypes () where nt.IsNestedPublic select nt).ToArray ();
1075 Assert.AreEqual (1, nested.Length);
1076 Assert.AreEqual ("NestedClass", nested [0].Name);
1077 Assert.IsTrue (t.BaseType.IsAssignableFrom (t));
1078 Assert.IsTrue (!t.IsAssignableFrom (t.BaseType));
1080 // generic instances
1081 t = frame.Method.GetParameters ()[9].ParameterType;
1082 Assert.AreEqual ("GClass`1", t.Name);
1083 Assert.IsTrue (t.IsGenericType);
1084 Assert.IsFalse (t.IsGenericTypeDefinition);
1086 var args = t.GetGenericArguments ();
1087 Assert.AreEqual (1, args.Length);
1088 Assert.AreEqual ("Int32", args [0].Name);
1090 // generic type definitions
1091 var gtd = t.GetGenericTypeDefinition ();
1092 Assert.AreEqual ("GClass`1", gtd.Name);
1093 Assert.IsTrue (gtd.IsGenericType);
1094 Assert.IsTrue (gtd.IsGenericTypeDefinition);
1095 Assert.AreEqual (gtd, gtd.GetGenericTypeDefinition ());
1097 args = gtd.GetGenericArguments ();
1098 Assert.AreEqual (1, args.Length);
1099 Assert.AreEqual ("T", args [0].Name);
1102 t = frame.Method.GetParameters ()[10].ParameterType;
1103 Assert.AreEqual ("AnEnum", t.Name);
1104 Assert.IsTrue (t.IsEnum);
1105 Assert.AreEqual ("Int32", t.EnumUnderlyingType.Name);
1108 t = frame.Method.GetParameters ()[7].ParameterType;
1110 var props = t.GetProperties ();
1111 Assert.AreEqual (3, props.Length);
1112 foreach (PropertyInfoMirror prop in props) {
1113 ParameterInfoMirror[] indexes = prop.GetIndexParameters ();
1115 if (prop.Name == "IntProperty") {
1116 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1117 Assert.AreEqual ("get_IntProperty", prop.GetGetMethod ().Name);
1118 Assert.AreEqual ("set_IntProperty", prop.GetSetMethod ().Name);
1119 Assert.AreEqual (0, indexes.Length);
1120 } else if (prop.Name == "ReadOnlyProperty") {
1121 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1122 Assert.AreEqual ("get_ReadOnlyProperty", prop.GetGetMethod ().Name);
1123 Assert.AreEqual (null, prop.GetSetMethod ());
1124 Assert.AreEqual (0, indexes.Length);
1125 } else if (prop.Name == "IndexedProperty") {
1126 Assert.AreEqual (1, indexes.Length);
1127 Assert.AreEqual ("Int32", indexes [0].ParameterType.Name);
1131 // custom attributes
1132 t = frame.Method.GetParameters ()[8].ParameterType;
1133 Assert.AreEqual ("Tests2", t.Name);
1134 var attrs = t.GetCustomAttributes (true);
1135 Assert.AreEqual (5, attrs.Length);
1136 foreach (var attr in attrs) {
1137 if (attr.Constructor.DeclaringType.Name == "DebuggerDisplayAttribute") {
1138 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1139 Assert.AreEqual ("Tests", attr.ConstructorArguments [0].Value);
1140 Assert.AreEqual (2, attr.NamedArguments.Count);
1141 Assert.AreEqual ("Name", attr.NamedArguments [0].Property.Name);
1142 Assert.AreEqual ("FOO", attr.NamedArguments [0].TypedValue.Value);
1143 Assert.AreEqual ("Target", attr.NamedArguments [1].Property.Name);
1144 Assert.IsInstanceOfType (typeof (TypeMirror), attr.NamedArguments [1].TypedValue.Value);
1145 Assert.AreEqual ("Int32", (attr.NamedArguments [1].TypedValue.Value as TypeMirror).Name);
1146 } else if (attr.Constructor.DeclaringType.Name == "DebuggerTypeProxyAttribute") {
1147 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1148 Assert.IsInstanceOfType (typeof (TypeMirror), attr.ConstructorArguments [0].Value);
1149 Assert.AreEqual ("Tests", (attr.ConstructorArguments [0].Value as TypeMirror).Name);
1150 } else if (attr.Constructor.DeclaringType.Name == "BAttribute") {
1151 Assert.AreEqual (2, attr.NamedArguments.Count);
1152 Assert.AreEqual ("afield", attr.NamedArguments [0].Field.Name);
1153 Assert.AreEqual ("bfield", attr.NamedArguments [1].Field.Name);
1154 } else if (attr.Constructor.DeclaringType.Name == "ClassInterfaceAttribute") {
1155 // inherited from System.Object
1156 //} else if (attr.Constructor.DeclaringType.Name == "Serializable") {
1157 // inherited from System.Object
1158 } else if (attr.Constructor.DeclaringType.Name == "ComVisibleAttribute") {
1159 // inherited from System.Object
1161 Assert.Fail (attr.Constructor.DeclaringType.Name);
1165 var assembly = entry_point.DeclaringType.Assembly;
1166 var type = assembly.GetType ("Tests4");
1167 Assert.IsFalse (type.IsInitialized);
1171 public void FieldInfo () {
1172 Event e = run_until ("ti2");
1173 StackFrame frame = e.Thread.GetFrames () [0];
1177 t = frame.Method.GetParameters ()[8].ParameterType;
1178 Assert.AreEqual ("Tests2", t.Name);
1180 var fi = t.GetField ("field_j");
1181 var attrs = fi.GetCustomAttributes (true);
1182 Assert.AreEqual (1, attrs.Length);
1183 var attr = attrs [0];
1184 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1185 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1186 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1187 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1191 public void PropertyInfo () {
1192 Event e = run_until ("ti2");
1193 StackFrame frame = e.Thread.GetFrames () [0];
1197 t = frame.Method.GetParameters ()[8].ParameterType;
1198 Assert.AreEqual ("Tests2", t.Name);
1200 var pi = t.GetProperty ("AProperty");
1201 var attrs = pi.GetCustomAttributes (true);
1202 Assert.AreEqual (1, attrs.Length);
1203 var attr = attrs [0];
1204 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1205 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1206 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1207 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1211 [Category ("only5")]
1212 public void Type_GetValue () {
1213 Event e = run_until ("o1");
1214 StackFrame frame = e.Thread.GetFrames () [0];
1216 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1218 TypeMirror t = o.Type;
1220 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1223 object f = t.GetValue (o.Type.GetField ("static_i"));
1224 AssertValue (55, f);
1226 f = t.GetValue (o.Type.GetField ("static_s"));
1227 AssertValue ("A", f);
1229 // literal static fields
1230 f = t.GetValue (o.Type.GetField ("literal_i"));
1231 AssertValue (56, f);
1233 f = t.GetValue (o.Type.GetField ("literal_s"));
1234 AssertValue ("B", f);
1236 // Inherited static fields
1237 TypeMirror parent = t.BaseType;
1238 f = t.GetValue (parent.GetField ("base_static_i"));
1239 AssertValue (57, f);
1241 f = t.GetValue (parent.GetField ("base_static_s"));
1242 AssertValue ("C", f);
1244 // thread static field
1245 f = t.GetValue (t.GetField ("tls_i"), e.Thread);
1246 AssertValue (42, f);
1248 // Argument checking
1249 AssertThrows<ArgumentNullException> (delegate () {
1254 AssertThrows<ArgumentException> (delegate () {
1255 t.GetValue (o.Type.GetField ("field_i"));
1258 // field on another type
1259 AssertThrows<ArgumentException> (delegate () {
1260 t.GetValue (val2.Type.GetField ("static_field_j"));
1263 // special static field
1264 AssertThrows<ArgumentException> (delegate () {
1265 t.GetValue (t.GetField ("tls_i"));
1270 public void Type_GetValues () {
1271 Event e = run_until ("o1");
1272 StackFrame frame = e.Thread.GetFrames () [0];
1274 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1276 TypeMirror t = o.Type;
1279 object[] vals = t.GetValues (new FieldInfoMirror [] { t.GetField ("static_i"), t.GetField ("static_s") });
1280 object f = vals [0];
1281 AssertValue (55, f);
1284 AssertValue ("A", f);
1286 // Argument checking
1287 AssertThrows<ArgumentNullException> (delegate () {
1291 AssertThrows<ArgumentNullException> (delegate () {
1292 t.GetValues (new FieldInfoMirror [] { null });
1297 public void ObjRefs () {
1298 Event e = run_until ("objrefs1");
1299 StackFrame frame = e.Thread.GetFrames () [0];
1301 ObjectMirror o = frame.GetThis () as ObjectMirror;
1302 ObjectMirror child = o.GetValue (o.Type.GetField ("child")) as ObjectMirror;
1304 Assert.IsTrue (child.Address != 0);
1306 // Check that object references are internalized correctly
1307 Assert.AreEqual (o, frame.GetThis ());
1309 run_until ("objrefs2");
1311 // child should be gc'd now
1312 // This is not deterministic
1313 //Assert.IsTrue (child.IsCollected);
1316 * No longer works since Type is read eagerly
1319 AssertThrows<ObjectCollectedException> (delegate () {
1320 TypeMirror t = child.Type;
1324 AssertThrows<ObjectCollectedException> (delegate () {
1325 long addr = child.Address;
1331 public void Type_GetObject () {
1332 Event e = run_until ("o1");
1333 StackFrame frame = e.Thread.GetFrames () [0];
1335 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1337 TypeMirror t = o.Type;
1339 Assert.AreEqual ("MonoType", t.GetTypeObject ().Type.Name);
1343 public void VTypes () {
1344 Event e = run_until ("vtypes1");
1345 StackFrame frame = e.Thread.GetFrames () [0];
1348 ObjectMirror o = frame.GetThis () as ObjectMirror;
1349 var obj = o.GetValue (o.Type.GetField ("field_struct"));
1350 Assert.IsTrue (obj is StructMirror);
1351 var s = obj as StructMirror;
1352 Assert.AreEqual ("AStruct", s.Type.Name);
1353 AssertValue (42, s ["i"]);
1355 AssertValue ("S", obj);
1356 AssertValue (43, s ["k"]);
1357 obj = o.GetValue (o.Type.GetField ("field_boxed_struct"));
1358 Assert.IsTrue (obj is StructMirror);
1359 s = obj as StructMirror;
1360 Assert.AreEqual ("AStruct", s.Type.Name);
1361 AssertValue (42, s ["i"]);
1363 // Check decoding of nested structs (#14942)
1364 obj = o.GetValue (o.Type.GetField ("nested_struct"));
1365 o.SetValue (o.Type.GetField ("nested_struct"), obj);
1367 // Check round tripping of boxed struct fields (#12354)
1368 obj = o.GetValue (o.Type.GetField ("boxed_struct_field"));
1369 o.SetValue (o.Type.GetField ("boxed_struct_field"), obj);
1370 obj = o.GetValue (o.Type.GetField ("boxed_struct_field"));
1371 s = obj as StructMirror;
1372 AssertValue (1, s ["key"]);
1374 Assert.IsTrue (obj is StructMirror);
1375 s = obj as StructMirror;
1376 AssertValue (42, s ["m_value"]);
1378 // vtypes as arguments
1379 s = frame.GetArgument (0) as StructMirror;
1380 AssertValue (44, s ["i"]);
1382 AssertValue ("T", obj);
1383 AssertValue (45, s ["k"]);
1385 // vtypes as array entries
1386 var arr = frame.GetArgument (1) as ArrayMirror;
1388 Assert.IsTrue (obj is StructMirror);
1389 s = obj as StructMirror;
1390 AssertValue (1, s ["i"]);
1391 AssertValue ("S1", s ["s"]);
1393 Assert.IsTrue (obj is StructMirror);
1394 s = obj as StructMirror;
1395 AssertValue (2, s ["i"]);
1396 AssertValue ("S2", s ["s"]);
1398 // Argument checking
1399 s = frame.GetArgument (0) as StructMirror;
1400 AssertThrows<ArgumentException> (delegate () {
1404 // generic vtype instances
1405 o = frame.GetThis () as ObjectMirror;
1406 obj = o.GetValue (o.Type.GetField ("generic_field_struct"));
1407 Assert.IsTrue (obj is StructMirror);
1408 s = obj as StructMirror;
1409 Assert.AreEqual ("GStruct`1", s.Type.Name);
1410 AssertValue (42, s ["i"]);
1412 // this on vtype methods
1413 e = run_until ("vtypes2");
1414 e = step_until (e.Thread, "foo");
1416 frame = e.Thread.GetFrames () [0];
1418 Assert.AreEqual ("foo", (e as StepEvent).Method.Name);
1419 obj = frame.GetThis ();
1421 Assert.IsTrue (obj is StructMirror);
1422 s = obj as StructMirror;
1423 AssertValue (44, s ["i"]);
1424 AssertValue ("T", s ["s"]);
1425 AssertValue (45, s ["k"]);
1427 // this on static vtype methods
1428 e = run_until ("vtypes3");
1429 e = step_until (e.Thread, "static_foo");
1431 frame = e.Thread.GetFrames () [0];
1433 Assert.AreEqual ("static_foo", (e as StepEvent).Method.Name);
1434 obj = frame.GetThis ();
1435 AssertValue (null, obj);
1439 public void AssemblyInfo () {
1440 Event e = run_until ("single_stepping");
1442 StackFrame frame = e.Thread.GetFrames () [0];
1444 var aname = frame.Method.DeclaringType.Assembly.GetName ();
1445 Assert.AreEqual ("dtest-app, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", aname.ToString ());
1447 ModuleMirror m = frame.Method.DeclaringType.Module;
1449 Assert.AreEqual ("dtest-app.exe", m.Name);
1450 Assert.AreEqual ("dtest-app.exe", m.ScopeName);
1451 Assert.IsTrue (m.FullyQualifiedName.IndexOf ("dtest-app.exe") != -1);
1452 Guid guid = m.ModuleVersionId;
1453 Assert.AreEqual (frame.Method.DeclaringType.Assembly, m.Assembly);
1454 Assert.AreEqual (frame.Method.DeclaringType.Assembly.ManifestModule, m);
1456 // This is no longer true on 4.0
1457 //Assert.AreEqual ("Assembly", frame.Method.DeclaringType.Assembly.GetAssemblyObject ().Type.Name);
1459 TypeMirror t = vm.RootDomain.Corlib.GetType ("System.Diagnostics.DebuggerDisplayAttribute");
1460 Assert.AreEqual ("DebuggerDisplayAttribute", t.Name);
1464 public void LocalsInfo () {
1465 Event e = run_until ("locals2");
1467 StackFrame frame = e.Thread.GetFrames () [0];
1469 var locals = frame.Method.GetLocals ();
1470 Assert.AreEqual (7, locals.Length);
1471 for (int i = 0; i < 7; ++i) {
1472 if (locals [i].Name == "args") {
1473 Assert.IsTrue (locals [i].IsArg);
1474 Assert.AreEqual ("String[]", locals [i].Type.Name);
1475 } else if (locals [i].Name == "arg") {
1476 Assert.IsTrue (locals [i].IsArg);
1477 Assert.AreEqual ("Int32", locals [i].Type.Name);
1478 } else if (locals [i].Name == "i") {
1479 Assert.IsFalse (locals [i].IsArg);
1480 Assert.AreEqual ("Int64", locals [i].Type.Name);
1481 } else if (locals [i].Name == "j") {
1482 Assert.IsFalse (locals [i].IsArg);
1483 Assert.AreEqual ("Int32", locals [i].Type.Name);
1484 } else if (locals [i].Name == "s") {
1485 Assert.IsFalse (locals [i].IsArg);
1486 Assert.AreEqual ("String", locals [i].Type.Name);
1487 } else if (locals [i].Name == "t") {
1489 Assert.IsTrue (locals [i].IsArg);
1490 Assert.AreEqual ("String", locals [i].Type.Name);
1491 } else if (locals [i].Name == "rs") {
1492 Assert.IsTrue (locals [i].IsArg);
1493 Assert.AreEqual ("String", locals [i].Type.Name);
1500 Event step_once () {
1502 var e = GetNextEvent ();
1503 Assert.IsTrue (e is StepEvent);
1507 Event step_into () {
1508 step_req.Disable ();
1509 step_req.Depth = StepDepth.Into;
1511 return step_once ();
1514 Event step_over () {
1515 step_req.Disable ();
1516 step_req.Depth = StepDepth.Over;
1518 return step_once ();
1522 step_req.Disable ();
1523 step_req.Depth = StepDepth.Out;
1525 return step_once ();
1529 public void Locals () {
1530 var be = run_until ("locals1");
1532 StackFrame frame = be.Thread.GetFrames () [0];
1533 MethodMirror m1 = frame.Method;
1535 // Compiler generated byref local
1536 foreach (var l in m1.GetLocals ()) {
1537 // The byval flag is hidden from the type
1538 if (l.Name != "ri" && l.Type.Name == "Double")
1539 AssertValue (null, frame.GetValue (l));
1542 be = run_until ("locals2");
1544 frame = be.Thread.GetFrames () [0];
1546 object val = frame.GetValue (frame.Method.GetLocal ("i"));
1547 AssertValue (0, val);
1549 var req = create_step (be);
1556 var e = step_once ();
1557 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1559 // Execute s = "AB";
1561 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1563 frame = e.Thread.GetFrames () [0];
1565 val = frame.GetValue (frame.Method.GetLocal ("i"));
1566 AssertValue (42, val);
1568 LocalVariable[] locals = frame.Method.GetLocals ();
1569 var vals = frame.GetValues (locals);
1570 Assert.AreEqual (locals.Length, vals.Length);
1571 for (int i = 0; i < locals.Length; ++i) {
1572 if (locals [i].Name == "i")
1573 AssertValue (42, vals [i]);
1574 if (locals [i].Name == "s")
1575 AssertValue ("AB", vals [i]);
1576 if (locals [i].Name == "t")
1577 AssertValue ("ABC", vals [i]);
1580 // Argument checking
1583 AssertThrows<ArgumentNullException> (delegate () {
1584 frame.GetValue ((LocalVariable)null);
1586 // GetValue () local from another method
1587 AssertThrows<ArgumentException> (delegate () {
1588 frame.GetValue (m1.GetLocal ("foo"));
1592 AssertThrows<ArgumentNullException> (delegate () {
1593 frame.GetValue ((ParameterInfoMirror)null);
1595 // GetValue () local from another method
1596 AssertThrows<ArgumentException> (delegate () {
1597 frame.GetValue (m1.GetParameters ()[0]);
1600 // GetValues () null
1601 AssertThrows<ArgumentNullException> (delegate () {
1602 frame.GetValues (null);
1604 // GetValues () embedded null
1605 AssertThrows<ArgumentNullException> (delegate () {
1606 frame.GetValues (new LocalVariable [] { null });
1608 // GetValues () local from another method
1609 AssertThrows<ArgumentException> (delegate () {
1610 frame.GetValues (new LocalVariable [] { m1.GetLocal ("foo") });
1613 AssertThrows<ArgumentException> (delegate () {
1614 val = frame.GetValue (frame.Method.ReturnParameter);
1617 // invalid stack frames
1619 e = GetNextEvent ();
1620 Assert.IsTrue (e is StepEvent);
1621 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1623 AssertThrows<InvalidStackFrameException> (delegate () {
1624 frame.GetValue (frame.Method.GetLocal ("i"));
1630 be = run_until ("locals7");
1632 req = create_step (be);
1638 // Test that locals are initialized
1639 frame = e.Thread.GetFrames () [0];
1640 val = frame.GetValue (frame.Method.GetLocal ("t"));
1641 AssertValue (0, val);
1645 Assert.AreEqual ("locals7", (e as StepEvent).Method.Name);
1649 Assert.AreEqual ("locals7", (e as StepEvent).Method.Name);
1651 frame = e.Thread.GetFrames () [0];
1652 val = frame.GetValue (frame.Method.GetParameters ()[0]);
1653 AssertValue (22, val);
1654 val = frame.GetValue (frame.Method.GetLocal ("t"));
1655 AssertValue (22, val);
1656 val = frame.GetValue (frame.Method.GetLocal ("t2"));
1657 AssertValue (22, val);
1661 public void GetVisibleVariables () {
1662 Event e = run_until ("locals4");
1665 var locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
1666 Assert.AreEqual (2, locals.Count);
1667 var loc = locals.First (l => l.Name == "i");
1668 Assert.AreEqual ("Int64", loc.Type.Name);
1669 loc = locals.First (l => l.Name == "s");
1670 Assert.AreEqual ("String", loc.Type.Name);
1672 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
1673 Assert.AreEqual ("i", loc.Name);
1674 Assert.AreEqual ("Int64", loc.Type.Name);
1676 e = run_until ("locals5");
1679 locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
1680 Assert.AreEqual (2, locals.Count);
1681 loc = locals.First (l => l.Name == "i");
1682 Assert.AreEqual ("String", loc.Type.Name);
1683 loc = locals.First (l => l.Name == "s");
1684 Assert.AreEqual ("String", loc.Type.Name);
1686 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
1687 Assert.AreEqual ("i", loc.Name);
1688 Assert.AreEqual ("String", loc.Type.Name);
1690 // Variable in another scope
1691 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("j");
1692 Assert.IsNull (loc);
1696 public void Exit () {
1701 var e = GetNextEvent ();
1702 Assert.IsInstanceOfType (typeof (VMDeathEvent), e);
1704 Assert.AreEqual (5, (e as VMDeathEvent).ExitCode);
1707 /* Could be a remote vm with no process */
1710 Assert.AreEqual (5, p.ExitCode);
1713 AssertThrows<VMDisconnectedException> (delegate () {
1722 public void Dispose () {
1727 var e = GetNextEvent ();
1728 Assert.IsInstanceOfType (typeof (VMDisconnectEvent), e);
1731 /* Could be a remote vm with no process */
1734 Assert.AreEqual (3, p.ExitCode);
1737 AssertThrows<VMDisconnectedException> (delegate () {
1746 public void ColumnNumbers () {
1747 Event e = run_until ("line_numbers");
1749 // FIXME: Merge this with LineNumbers () when its fixed
1751 step_req = create_step (e);
1752 step_req.Depth = StepDepth.Into;
1760 e = GetNextEvent ();
1761 Assert.IsTrue (e is StepEvent);
1762 if (e.Thread.GetFrames ()[0].Method.Name == "ln1")
1766 // Do an additional step over so we are not on the beginning line of the method
1767 step_req.Disable ();
1768 step_req.Depth = StepDepth.Over;
1771 e = GetNextEvent ();
1772 Assert.IsTrue (e is StepEvent);
1774 l = e.Thread.GetFrames ()[0].Location;
1776 Assert.AreEqual (3, l.ColumnNumber);
1778 step_req.Disable ();
1782 // Broken by mcs+runtime changes (#5438)
1783 [Category("NotWorking")]
1784 public void LineNumbers () {
1785 Event e = run_until ("line_numbers");
1787 step_req = create_step (e);
1788 step_req.Depth = StepDepth.Into;
1795 e = GetNextEvent ();
1796 Assert.IsTrue (e is StepEvent);
1798 l = e.Thread.GetFrames ()[0].Location;
1800 Assert.IsTrue (l.SourceFile.IndexOf ("dtest-app.cs") != -1);
1801 Assert.AreEqual ("ln1", l.Method.Name);
1804 using (FileStream fs = new FileStream (l.SourceFile, FileMode.Open, FileAccess.Read)) {
1805 MD5 md5 = MD5.Create ();
1806 var hash = md5.ComputeHash (fs);
1808 for (int i = 0; i < 16; ++i)
1809 Assert.AreEqual (hash [i], l.SourceFileHash [i]);
1812 int line_base = l.LineNumber;
1815 e = GetNextEvent ();
1816 Assert.IsTrue (e is StepEvent);
1817 l = e.Thread.GetFrames ()[0].Location;
1818 Assert.AreEqual ("ln2", l.Method.Name);
1819 Assert.AreEqual (line_base + 6, l.LineNumber);
1822 e = GetNextEvent ();
1823 Assert.IsTrue (e is StepEvent);
1824 l = e.Thread.GetFrames ()[0].Location;
1825 Assert.AreEqual ("ln1", l.Method.Name);
1826 Assert.AreEqual (line_base + 1, l.LineNumber);
1829 e = GetNextEvent ();
1830 Assert.IsTrue (e is StepEvent);
1831 l = e.Thread.GetFrames ()[0].Location;
1832 Assert.AreEqual ("ln3", l.Method.Name);
1833 Assert.AreEqual (line_base + 11, l.LineNumber);
1836 e = GetNextEvent ();
1837 Assert.IsTrue (e is StepEvent);
1838 l = e.Thread.GetFrames ()[0].Location;
1839 Assert.AreEqual ("ln3", l.Method.Name);
1840 Assert.IsTrue (l.SourceFile.EndsWith ("FOO"));
1841 Assert.AreEqual (55, l.LineNumber);
1844 e = GetNextEvent ();
1845 Assert.IsTrue (e is StepEvent);
1846 l = e.Thread.GetFrames ()[0].Location;
1847 Assert.AreEqual ("ln1", l.Method.Name);
1848 Assert.AreEqual (line_base + 2, l.LineNumber);
1850 // GetSourceFiles ()
1851 string[] sources = l.Method.DeclaringType.GetSourceFiles ();
1852 Assert.AreEqual (2, sources.Length);
1853 Assert.AreEqual ("dtest-app.cs", sources [0]);
1854 Assert.AreEqual ("FOO", sources [1]);
1856 sources = l.Method.DeclaringType.GetSourceFiles (true);
1857 Assert.AreEqual (2, sources.Length);
1858 Assert.IsTrue (sources [0].EndsWith ("dtest-app.cs"));
1859 Assert.IsTrue (sources [1].EndsWith ("FOO"));
1863 public void Suspend () {
1866 Start (new string [] { "dtest-app.exe", "suspend-test" });
1868 Event e = run_until ("suspend");
1870 ThreadMirror main = e.Thread;
1878 // The debuggee should be suspended while it is running the infinite loop
1880 StackFrame frame = main.GetFrames ()[0];
1881 Assert.AreEqual ("suspend", frame.Method.Name);
1885 // resuming when not suspended
1886 AssertThrows<InvalidOperationException> (delegate () {
1896 public void AssemblyLoad () {
1897 Event e = run_until ("assembly_load");
1899 var load_req = vm.CreateAssemblyLoadRequest ();
1904 e = GetNextEvent ();
1905 Assert.IsInstanceOfType (typeof (AssemblyLoadEvent), e);
1906 Assert.IsTrue ((e as AssemblyLoadEvent).Assembly.Location.EndsWith ("System.dll"));
1908 var frames = e.Thread.GetFrames ();
1909 Assert.IsTrue (frames.Length > 0);
1910 Assert.AreEqual ("assembly_load", frames [0].Method.Name);
1914 public void CreateValue () {
1917 v = vm.CreateValue (1);
1918 Assert.AreEqual (vm, v.VirtualMachine);
1919 Assert.AreEqual (1, v.Value);
1921 v = vm.CreateValue (null);
1922 Assert.AreEqual (vm, v.VirtualMachine);
1923 Assert.AreEqual (null, v.Value);
1925 // Argument checking
1926 AssertThrows <ArgumentException> (delegate () {
1927 v = vm.CreateValue ("FOO");
1932 public void CreateString () {
1933 StringMirror s = vm.RootDomain.CreateString ("ABC");
1935 Assert.AreEqual (vm, s.VirtualMachine);
1936 Assert.AreEqual ("ABC", s.Value);
1937 Assert.AreEqual (vm.RootDomain, s.Domain);
1940 StringBuilder sb = new StringBuilder ();
1941 for (int i = 0; i < 1024; ++i)
1943 s = vm.RootDomain.CreateString (sb.ToString ());
1945 // Argument checking
1946 AssertThrows <ArgumentNullException> (delegate () {
1947 s = vm.RootDomain.CreateString (null);
1952 public void CreateBoxedValue () {
1953 ObjectMirror o = vm.RootDomain.CreateBoxedValue (new PrimitiveValue (vm, 42));
1955 Assert.AreEqual ("Int32", o.Type.Name);
1956 //AssertValue (42, m.GetValue (o.Type.GetField ("m_value")));
1958 // Argument checking
1959 AssertThrows <ArgumentNullException> (delegate () {
1960 vm.RootDomain.CreateBoxedValue (null);
1963 AssertThrows <ArgumentException> (delegate () {
1964 vm.RootDomain.CreateBoxedValue (o);
1969 public void Invoke () {
1970 Event e = run_until ("invoke1");
1972 StackFrame frame = e.Thread.GetFrames () [0];
1974 TypeMirror t = frame.Method.DeclaringType;
1975 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
1977 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
1983 m = t.GetMethod ("invoke_return_void");
1984 v = this_obj.InvokeMethod (e.Thread, m, null);
1988 m = t.GetMethod ("invoke_return_ref");
1989 v = this_obj.InvokeMethod (e.Thread, m, null);
1990 AssertValue ("ABC", v);
1993 m = t.GetMethod ("invoke_return_null");
1994 v = this_obj.InvokeMethod (e.Thread, m, null);
1995 AssertValue (null, v);
1998 m = t.GetMethod ("invoke_return_primitive");
1999 v = this_obj.InvokeMethod (e.Thread, m, null);
2000 AssertValue (42, v);
2003 m = t.GetMethod ("invoke_return_nullable");
2004 v = this_obj.InvokeMethod (e.Thread, m, null);
2005 Assert.IsInstanceOfType (typeof (StructMirror), v);
2006 var s = v as StructMirror;
2007 AssertValue (42, s.Fields [0]);
2008 AssertValue (true, s.Fields [1]);
2010 // pass nullable as this
2011 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2012 m = s.Type.GetMethod ("ToString");
2013 v = s.InvokeMethod (e.Thread, m, null);
2015 // return nullable null
2016 m = t.GetMethod ("invoke_return_nullable_null");
2017 v = this_obj.InvokeMethod (e.Thread, m, null);
2018 Assert.IsInstanceOfType (typeof (StructMirror), v);
2019 s = v as StructMirror;
2020 AssertValue (0, s.Fields [0]);
2021 AssertValue (false, s.Fields [1]);
2023 // pass nullable as this
2024 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2025 m = s.Type.GetMethod ("ToString");
2026 v = s.InvokeMethod (e.Thread, m, null);
2029 m = t.GetMethod ("invoke_pass_primitive");
2030 Value[] args = new Value [] {
2031 vm.CreateValue ((byte)Byte.MaxValue),
2032 vm.CreateValue ((sbyte)SByte.MaxValue),
2033 vm.CreateValue ((short)1),
2034 vm.CreateValue ((ushort)1),
2035 vm.CreateValue ((int)1),
2036 vm.CreateValue ((uint)1),
2037 vm.CreateValue ((long)1),
2038 vm.CreateValue ((ulong)1),
2039 vm.CreateValue ('A'),
2040 vm.CreateValue (true),
2041 vm.CreateValue (3.14f),
2042 vm.CreateValue (3.14) };
2044 v = this_obj.InvokeMethod (e.Thread, m, args);
2045 AssertValue ((int)Byte.MaxValue + (int)SByte.MaxValue + 1 + 1 + 1 + 1 + 1 + 1 + 'A' + 1 + 3 + 3, v);
2048 m = t.GetMethod ("invoke_pass_ref");
2049 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2050 AssertValue ("ABC", v);
2053 m = t.GetMethod ("invoke_pass_ref");
2054 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (null) });
2055 AssertValue (null, v);
2058 m = t.GetMethod ("invoke_static_pass_ref");
2059 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2060 AssertValue ("ABC", v);
2062 // static invoked using ObjectMirror.InvokeMethod
2063 m = t.GetMethod ("invoke_static_pass_ref");
2064 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2065 AssertValue ("ABC", v);
2067 // method which throws an exception
2069 m = t.GetMethod ("invoke_throws");
2070 v = this_obj.InvokeMethod (e.Thread, m, null);
2072 } catch (InvocationException ex) {
2073 Assert.AreEqual ("Exception", ex.Exception.Type.Name);
2077 m = t.GetMethod (".ctor");
2078 v = t.InvokeMethod (e.Thread, m, null);
2079 Assert.IsInstanceOfType (typeof (ObjectMirror), v);
2080 Assert.AreEqual ("Tests", (v as ObjectMirror).Type.Name);
2083 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("ITest2");
2084 m = cl1.GetMethod ("invoke_iface");
2085 v = this_obj.InvokeMethod (e.Thread, m, null);
2086 AssertValue (42, v);
2088 // Argument checking
2091 AssertThrows<ArgumentNullException> (delegate {
2092 m = t.GetMethod ("invoke_pass_ref");
2093 v = this_obj.InvokeMethod (null, m, new Value [] { vm.CreateValue (null) });
2097 AssertThrows<ArgumentNullException> (delegate {
2098 v = this_obj.InvokeMethod (e.Thread, null, new Value [] { vm.CreateValue (null) });
2101 // invalid number of arguments
2102 m = t.GetMethod ("invoke_pass_ref");
2103 AssertThrows<ArgumentException> (delegate {
2104 v = this_obj.InvokeMethod (e.Thread, m, null);
2107 // invalid type of argument (ref != primitive)
2108 m = t.GetMethod ("invoke_pass_ref");
2109 AssertThrows<ArgumentException> (delegate {
2110 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2113 // invalid type of argument (primitive != primitive)
2114 m = t.GetMethod ("invoke_pass_primitive_2");
2115 AssertThrows<ArgumentException> (delegate {
2116 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2119 // invoking a non-static method as static
2120 m = t.GetMethod ("invoke_pass_ref");
2121 AssertThrows<ArgumentException> (delegate {
2122 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2125 // invoking a method defined in another class
2126 m = t2.GetMethod ("invoke");
2127 AssertThrows<ArgumentException> (delegate {
2128 v = this_obj.InvokeMethod (e.Thread, m, null);
2133 public void InvokeVType () {
2134 Event e = run_until ("invoke1");
2136 StackFrame frame = e.Thread.GetFrames () [0];
2138 var s = frame.GetArgument (1) as StructMirror;
2140 TypeMirror t = s.Type;
2145 // Pass struct as this, receive int
2146 m = t.GetMethod ("invoke_return_int");
2147 v = s.InvokeMethod (e.Thread, m, null);
2148 AssertValue (42, v);
2150 // Pass struct as this, receive intptr
2151 m = t.GetMethod ("invoke_return_intptr");
2152 v = s.InvokeMethod (e.Thread, m, null);
2153 AssertValue (43, v);
2156 m = t.GetMethod ("invoke_static");
2157 v = t.InvokeMethod (e.Thread, m, null);
2160 // Pass generic struct as this
2161 s = frame.GetArgument (2) as StructMirror;
2163 m = t.GetMethod ("invoke_return_int");
2164 v = s.InvokeMethod (e.Thread, m, null);
2165 AssertValue (42, v);
2169 public void BreakpointDuringInvoke () {
2170 Event e = run_until ("invoke1");
2172 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke2");
2173 Assert.IsNotNull (m);
2174 vm.SetBreakpoint (m, 0);
2176 StackFrame frame = e.Thread.GetFrames () [0];
2177 var o = frame.GetThis () as ObjectMirror;
2179 bool failed = false;
2181 bool finished = false;
2182 object wait = new object ();
2184 // Have to invoke in a separate thread as the invoke is suspended until we
2185 // resume after the breakpoint
2186 Thread t = new Thread (delegate () {
2188 o.InvokeMethod (e.Thread, m, null);
2194 Monitor.Pulse (wait);
2200 StackFrame invoke_frame = null;
2203 e = GetNextEvent ();
2204 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
2205 // Check stack trace support and invokes
2206 var frames = e.Thread.GetFrames ();
2207 invoke_frame = frames [0];
2208 Assert.AreEqual ("invoke2", frames [0].Method.Name);
2209 Assert.IsTrue (frames [0].IsDebuggerInvoke);
2210 Assert.AreEqual ("invoke1", frames [1].Method.Name);
2217 Monitor.Wait (wait);
2220 // Check that the invoke frames are no longer valid
2221 AssertThrows<InvalidStackFrameException> (delegate {
2222 invoke_frame.GetThis ();
2225 // Check InvokeOptions.DisableBreakpoints flag
2226 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2230 public void DisabledExceptionDuringInvoke () {
2231 Event e = run_until ("invoke_ex");
2233 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_ex_inner");
2235 StackFrame frame = e.Thread.GetFrames () [0];
2236 var o = frame.GetThis () as ObjectMirror;
2238 var req = vm.CreateExceptionRequest (null);
2241 // Check InvokeOptions.DisableBreakpoints flag
2242 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2248 public void InvokeSingleThreaded () {
2251 Start (new string [] { "dtest-app.exe", "invoke-single-threaded" });
2253 Event e = run_until ("invoke_single_threaded_2");
2255 StackFrame f = e.Thread.GetFrames ()[0];
2257 var obj = f.GetThis () as ObjectMirror;
2259 // Check that the counter value incremented by the other thread does not increase
2260 // during the invoke.
2261 object counter1 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2263 var m = obj.Type.GetMethod ("invoke_return_void");
2264 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2266 object counter2 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2268 Assert.AreEqual ((int)counter1, (int)counter2);
2270 // Test multiple invokes done in succession
2271 m = obj.Type.GetMethod ("invoke_return_void");
2272 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2274 // Test events during single-threaded invokes
2275 vm.EnableEvents (EventType.TypeLoad);
2276 m = obj.Type.GetMethod ("invoke_type_load");
2277 obj.BeginInvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded, delegate {
2281 e = GetNextEvent ();
2282 Assert.AreEqual (EventType.TypeLoad, e.EventType);
2285 List<Value> invoke_results;
2288 public void InvokeMultiple () {
2289 Event e = run_until ("invoke1");
2291 StackFrame frame = e.Thread.GetFrames () [0];
2293 TypeMirror t = frame.Method.DeclaringType;
2294 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2296 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2298 var methods = new MethodMirror [2];
2299 methods [0] = t.GetMethod ("invoke_return_ref");
2300 methods [1] = t.GetMethod ("invoke_return_primitive");
2302 invoke_results = new List<Value> ();
2304 var r = this_obj.BeginInvokeMultiple (e.Thread, methods, null, InvokeOptions.SingleThreaded, invoke_multiple_cb, this_obj);
2305 WaitHandle.WaitAll (new WaitHandle[] { r.AsyncWaitHandle });
2306 this_obj.EndInvokeMultiple (r);
2307 // The callback might still be running
2308 while (invoke_results.Count < 2) {
2311 if (invoke_results [0] is PrimitiveValue) {
2312 AssertValue ("ABC", invoke_results [1]);
2313 AssertValue (42, invoke_results [0]);
2315 AssertValue ("ABC", invoke_results [0]);
2316 AssertValue (42, invoke_results [1]);
2320 void invoke_multiple_cb (IAsyncResult ar) {
2321 ObjectMirror this_obj = (ObjectMirror)ar.AsyncState;
2323 var res = this_obj.EndInvokeMethod (ar);
2324 lock (invoke_results)
2325 invoke_results.Add (res);
2329 public void GetThreads () {
2334 public void Threads () {
2335 Event e = run_until ("threads");
2337 Assert.AreEqual (ThreadState.Running, e.Thread.ThreadState);
2339 Assert.IsTrue (e.Thread.ThreadId > 0);
2341 Assert.AreEqual (e.Thread.TID, e.Thread.TID);
2343 vm.EnableEvents (EventType.ThreadStart, EventType.ThreadDeath);
2347 e = GetNextEvent ();
2348 Assert.IsInstanceOfType (typeof (ThreadStartEvent), e);
2349 var state = e.Thread.ThreadState;
2350 Assert.IsTrue (state == ThreadState.Running || state == ThreadState.Unstarted);
2354 e = GetNextEvent ();
2355 Assert.IsInstanceOfType (typeof (ThreadDeathEvent), e);
2356 Assert.AreEqual (ThreadState.Stopped, e.Thread.ThreadState);
2360 public void Frame_SetValue () {
2361 Event e = run_until ("locals2");
2363 StackFrame frame = e.Thread.GetFrames () [0];
2366 var l = frame.Method.GetLocal ("i");
2367 frame.SetValue (l, vm.CreateValue ((long)55));
2368 AssertValue (55, frame.GetValue (l));
2371 l = frame.Method.GetLocal ("s");
2372 frame.SetValue (l, vm.RootDomain.CreateString ("DEF"));
2373 AssertValue ("DEF", frame.GetValue (l));
2375 // argument as local
2376 l = frame.Method.GetLocal ("arg");
2377 frame.SetValue (l, vm.CreateValue (6));
2378 AssertValue (6, frame.GetValue (l));
2381 var p = frame.Method.GetParameters ()[1];
2382 frame.SetValue (p, vm.CreateValue (7));
2383 AssertValue (7, frame.GetValue (p));
2386 p = frame.Method.GetParameters ()[2];
2387 frame.SetValue (p, vm.RootDomain.CreateString ("DEF"));
2388 AssertValue ("DEF", frame.GetValue (p));
2391 p = frame.Method.GetParameters ()[3];
2392 frame.SetValue (p, vm.RootDomain.CreateString ("DEF2"));
2393 AssertValue ("DEF2", frame.GetValue (p));
2395 // argument checking
2398 AssertThrows<ArgumentNullException> (delegate () {
2399 frame.SetValue ((LocalVariable)null, vm.CreateValue (55));
2403 AssertThrows<ArgumentNullException> (delegate () {
2404 l = frame.Method.GetLocal ("i");
2405 frame.SetValue (l, null);
2408 // value of invalid type
2409 AssertThrows<ArgumentException> (delegate () {
2410 l = frame.Method.GetLocal ("i");
2411 frame.SetValue (l, vm.CreateValue (55));
2417 public void Frame_SetValue_Registers () {
2418 Event e = run_until ("locals6_1");
2420 StackFrame frame = e.Thread.GetFrames () [1];
2423 var l = frame.Method.GetLocal ("j");
2424 frame.SetValue (l, vm.CreateValue (99));
2425 AssertValue (99, frame.GetValue (l));
2427 // Check it during execution
2428 e = run_until ("locals6_2");
2429 frame = e.Thread.GetFrames () [0];
2430 AssertValue (99, frame.GetValue (frame.Method.GetParameters ()[0]));
2432 // Set it while in a frame which clobbers its register
2433 e = run_until ("locals6_3");
2434 frame = e.Thread.GetFrames () [1];
2435 frame.SetValue (l, vm.CreateValue (100));
2436 AssertValue (100, frame.GetValue (l));
2438 // Check it during execution
2439 e = run_until ("locals6_4");
2440 frame = e.Thread.GetFrames () [0];
2441 AssertValue (100, frame.GetValue (frame.Method.GetParameters ()[0]));
2443 // Signed byte value
2444 e = run_until ("locals6_5");
2445 frame = e.Thread.GetFrames () [1];
2446 var l2 = frame.Method.GetLocal ("sb");
2447 frame.SetValue (l2, vm.CreateValue ((sbyte)-99));
2448 AssertValue (-99, frame.GetValue (l2));
2450 // Check it during execution
2451 e = run_until ("locals6_6");
2452 frame = e.Thread.GetFrames () [0];
2453 AssertValue (-99, frame.GetValue (frame.Method.GetParameters ()[0]));
2457 public void InvokeRegress () {
2458 Event e = run_until ("invoke1");
2460 StackFrame frame = e.Thread.GetFrames () [0];
2462 TypeMirror t = frame.Method.DeclaringType;
2463 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2465 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2471 m = t.GetMethod ("invoke_return_void");
2472 v = this_obj.InvokeMethod (e.Thread, m, null);
2475 // Check that the stack frames remain valid during the invoke
2476 Assert.AreEqual ("Tests", (frame.GetThis () as ObjectMirror).Type.Name);
2478 // do another invoke
2479 m = t.GetMethod ("invoke_return_void");
2480 v = this_obj.InvokeMethod (e.Thread, m, null);
2483 // Try a single step after the invoke
2484 var req = create_step (e);
2485 req.Depth = StepDepth.Into;
2486 req.Size = StepSize.Line;
2492 // Step into invoke2
2494 e = GetNextEvent ();
2495 Assert.IsTrue (e is StepEvent);
2496 Assert.AreEqual ("invoke2", (e as StepEvent).Method.Name);
2500 frame = e.Thread.GetFrames () [0];
2504 public void Exceptions () {
2505 Event e = run_until ("exceptions");
2506 var req = vm.CreateExceptionRequest (null);
2511 e = GetNextEvent ();
2512 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2513 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2515 var frames = e.Thread.GetFrames ();
2516 Assert.AreEqual ("exceptions", frames [0].Method.Name);
2519 // exception type filter
2521 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.ArgumentException"));
2524 // Skip the throwing of the second OverflowException
2527 e = GetNextEvent ();
2528 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2529 Assert.AreEqual ("ArgumentException", (e as ExceptionEvent).Exception.Type.Name);
2532 // exception type filter for subclasses
2533 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.Exception"));
2538 e = GetNextEvent ();
2539 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2540 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2544 req.IncludeSubclasses = false;
2549 e = GetNextEvent ();
2550 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2551 Assert.AreEqual ("Exception", (e as ExceptionEvent).Exception.Type.Name);
2554 // Implicit exceptions
2555 req = vm.CreateExceptionRequest (null);
2560 e = GetNextEvent ();
2561 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2562 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
2565 // Single stepping after an exception
2566 req = vm.CreateExceptionRequest (null);
2571 e = GetNextEvent ();
2572 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2573 Assert.AreEqual ("Exception", (e as ExceptionEvent).Exception.Type.Name);
2574 frames = e.Thread.GetFrames ();
2575 Assert.AreEqual ("exceptions2", frames [0].Method.Name);
2578 var sreq = create_step (e);
2579 sreq.Depth = StepDepth.Over;
2580 sreq.Size = StepSize.Line;
2584 e = GetNextEvent ();
2585 Assert.IsInstanceOfType (typeof (StepEvent), e);
2586 frames = e.Thread.GetFrames ();
2587 Assert.AreEqual ("exceptions", frames [0].Method.Name);
2590 // Argument checking
2591 AssertThrows<ArgumentException> (delegate {
2592 vm.CreateExceptionRequest (e.Thread.Type);
2597 public void ExceptionFilter () {
2598 Event e = run_until ("exception_filter");
2600 MethodMirror m = entry_point.DeclaringType.GetMethod ("exception_filter_filter");
2601 Assert.IsNotNull (m);
2603 vm.SetBreakpoint (m, 0);
2607 e = GetNextEvent ();
2608 Assert.AreEqual (EventType.Breakpoint, e.EventType);
2609 Assert.IsTrue (e is BreakpointEvent);
2610 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
2612 var frames = e.Thread.GetFrames ();
2614 Assert.IsTrue (frames [0].Location.SourceFile.IndexOf ("dtest-app.cs") != -1);
2615 Assert.AreEqual ("exception_filter_filter", frames [0].Location.Method.Name);
2617 Assert.AreEqual (0, frames [1].Location.Method.MetadataToken);
2618 Assert.AreEqual (0x0f, frames [1].Location.ILOffset);
2620 Assert.AreEqual ("exception_filter_method", frames [2].Location.Method.Name);
2621 Assert.AreEqual (0x06, frames [2].Location.ILOffset);
2623 Assert.AreEqual (0, frames [3].Location.Method.MetadataToken, 0);
2624 Assert.AreEqual (0, frames [3].Location.ILOffset);
2626 Assert.AreEqual ("exception_filter", frames [4].Location.Method.Name);
2630 public void ExceptionFilter2 () {
2633 Start (new string [] { "dtest-excfilter.exe" });
2635 MethodMirror filter_method = entry_point.DeclaringType.GetMethod ("Filter");
2636 Assert.IsNotNull (filter_method);
2638 MethodMirror test_method = entry_point.DeclaringType.GetMethod ("Test");
2639 Assert.IsNotNull (test_method);
2641 vm.SetBreakpoint (filter_method, 0);
2645 var e = GetNextEvent ();
2646 Assert.AreEqual (EventType.Breakpoint, e.EventType);
2647 Assert.IsTrue (e is BreakpointEvent);
2648 Assert.AreEqual (filter_method.Name, (e as BreakpointEvent).Method.Name);
2650 var frames = e.Thread.GetFrames ();
2652 Assert.AreEqual (4, frames.Count ());
2654 Assert.AreEqual (filter_method.Name, frames [0].Location.Method.Name);
2655 Assert.AreEqual (20, frames [0].Location.LineNumber);
2656 Assert.AreEqual (0, frames [0].Location.ILOffset);
2658 Assert.AreEqual (test_method.Name, frames [1].Location.Method.Name);
2659 Assert.AreEqual (37, frames [1].Location.LineNumber);
2660 Assert.AreEqual (0x0b, frames [1].Location.ILOffset);
2662 Assert.AreEqual (test_method.Name, frames [2].Location.Method.Name);
2663 Assert.AreEqual (33, frames [2].Location.LineNumber);
2664 Assert.AreEqual (0x05, frames [2].Location.ILOffset);
2666 Assert.AreEqual (entry_point.Name, frames [3].Location.Method.Name);
2667 Assert.AreEqual (14, frames [3].Location.LineNumber);
2668 Assert.AreEqual (0x00, frames [3].Location.ILOffset);
2676 public void EventSets () {
2678 // Create two filter which both match the same exception
2680 Event e = run_until ("exceptions");
2682 var req = vm.CreateExceptionRequest (null);
2685 var req2 = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.OverflowException"));
2690 var es = vm.GetNextEventSet ();
2691 Assert.AreEqual (2, es.Events.Length);
2694 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2695 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2698 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2699 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2706 // Test single threaded invokes during processing of nullref exceptions.
2707 // These won't work if the exception handling is done from the sigsegv signal
2708 // handler, since the sigsegv signal is disabled until control returns from the
2712 [Category ("only3")]
2713 public void NullRefExceptionAndSingleThreadedInvoke () {
2714 Event e = run_until ("exceptions");
2715 var req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.NullReferenceException"));
2720 e = GetNextEvent ();
2721 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2722 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
2724 var ex = (e as ExceptionEvent).Exception;
2725 var tostring_method = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2726 ex.InvokeMethod (e.Thread, tostring_method, null, InvokeOptions.SingleThreaded);
2730 public void Domains () {
2733 Start (new string [] { "dtest-app.exe", "domain-test" });
2735 vm.EnableEvents (EventType.AppDomainCreate, EventType.AppDomainUnload, EventType.AssemblyUnload);
2737 Event e = run_until ("domains");
2741 e = GetNextEvent ();
2742 Assert.IsInstanceOfType (typeof (AppDomainCreateEvent), e);
2744 var domain = (e as AppDomainCreateEvent).Domain;
2746 // Check the object type
2747 e = run_until ("domains_2");
2748 var frame = e.Thread.GetFrames ()[0];
2749 var o = frame.GetArgument (0) as ObjectMirror;
2750 Assert.AreEqual ("CrossDomain", o.Type.Name);
2752 // Do a remoting invoke
2753 var cross_domain_type = o.Type;
2754 var v = o.InvokeMethod (e.Thread, cross_domain_type.GetMethod ("invoke_3"), null);
2755 AssertValue (42, v);
2757 // Run until the callback in the domain
2758 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_in_domain");
2759 Assert.IsNotNull (m);
2760 vm.SetBreakpoint (m, 0);
2764 e = GetNextEvent ();
2765 if (e is BreakpointEvent)
2769 Assert.AreEqual ("invoke_in_domain", (e as BreakpointEvent).Method.Name);
2771 // d_method is from another domain
2772 MethodMirror d_method = (e as BreakpointEvent).Method;
2773 Assert.IsTrue (m != d_method);
2775 var frames = e.Thread.GetFrames ();
2776 Assert.AreEqual ("invoke_in_domain", frames [0].Method.Name);
2777 Assert.AreEqual ("invoke", frames [1].Method.Name);
2778 Assert.AreEqual ("domains", frames [2].Method.Name);
2780 // Test breakpoints on already JITted methods in other domains
2781 m = entry_point.DeclaringType.GetMethod ("invoke_in_domain_2");
2782 Assert.IsNotNull (m);
2783 vm.SetBreakpoint (m, 0);
2787 e = GetNextEvent ();
2788 if (e is BreakpointEvent)
2792 Assert.AreEqual ("invoke_in_domain_2", (e as BreakpointEvent).Method.Name);
2794 // This is empty when receiving the AppDomainCreateEvent
2795 Assert.AreEqual ("domain", domain.FriendlyName);
2797 // Run until the unload
2800 e = GetNextEvent ();
2801 if (e is AssemblyUnloadEvent) {
2807 Assert.IsInstanceOfType (typeof (AppDomainUnloadEvent), e);
2808 Assert.AreEqual (domain, (e as AppDomainUnloadEvent).Domain);
2810 // Run past the unload
2811 e = run_until ("domains_3");
2813 // Test access to unloaded types
2814 // FIXME: Add an exception type for this
2815 AssertThrows<Exception> (delegate {
2816 d_method.DeclaringType.GetValue (d_method.DeclaringType.GetField ("static_i"));
2821 public void DynamicMethods () {
2822 Event e = run_until ("dyn_call");
2824 var m = e.Thread.GetFrames ()[1].Method;
2825 Assert.AreEqual ("dyn_method", m.Name);
2827 // Test access to IL
2828 var body = m.GetMethodBody ();
2830 ILInstruction ins = body.Instructions [0];
2831 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
2832 Assert.AreEqual ("FOO", ins.Operand);
2836 public void RefEmit () {
2839 Start (new string [] { "dtest-app.exe", "ref-emit-test" });
2841 Event e = run_until ("ref_emit_call");
2843 var m = e.Thread.GetFrames ()[1].Method;
2844 Assert.AreEqual ("ref_emit_method", m.Name);
2846 // Test access to IL
2847 var body = m.GetMethodBody ();
2851 ins = body.Instructions [0];
2852 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
2853 Assert.AreEqual ("FOO", ins.Operand);
2855 ins = body.Instructions [1];
2856 Assert.AreEqual (OpCodes.Call, ins.OpCode);
2857 Assert.IsInstanceOfType (typeof (MethodMirror), ins.Operand);
2858 Assert.AreEqual ("ref_emit_call", (ins.Operand as MethodMirror).Name);
2862 public void IsAttached () {
2863 var f = entry_point.DeclaringType.GetField ("is_attached");
2865 Event e = run_until ("Main");
2867 AssertValue (true, entry_point.DeclaringType.GetValue (f));
2871 public void StackTraceInNative () {
2872 // Check that stack traces can be produced for threads in native code
2875 Start (new string [] { "dtest-app.exe", "frames-in-native" });
2877 var e = run_until ("frames_in_native");
2879 // FIXME: This is racy
2886 StackFrame[] frames = e.Thread.GetFrames ();
2888 int frame_index = -1;
2889 for (int i = 0; i < frames.Length; ++i) {
2890 if (frames [i].Method.Name == "Sleep") {
2896 Assert.IsTrue (frame_index != -1);
2897 Assert.AreEqual ("Sleep", frames [frame_index].Method.Name);
2898 Assert.AreEqual ("frames_in_native", frames [frame_index + 1].Method.Name);
2899 Assert.AreEqual ("Main", frames [frame_index + 2].Method.Name);
2901 // Check that invokes are disabled for such threads
2902 TypeMirror t = frames [frame_index + 1].Method.DeclaringType;
2904 var m = t.GetMethod ("invoke_static_return_void");
2905 AssertThrows<InvalidOperationException> (delegate {
2906 t.InvokeMethod (e.Thread, m, null);
2909 // Check that the frame info is invalidated
2910 run_until ("frames_in_native_2");
2912 AssertThrows<InvalidStackFrameException> (delegate {
2913 Console.WriteLine (frames [frame_index].GetThis ());
2918 public void VirtualMachine_CreateEnumMirror () {
2919 var e = run_until ("o1");
2920 var frame = e.Thread.GetFrames () [0];
2922 object val = frame.GetThis ();
2923 Assert.IsTrue (val is ObjectMirror);
2924 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
2925 ObjectMirror o = (val as ObjectMirror);
2927 FieldInfoMirror field = o.Type.GetField ("field_enum");
2928 Value f = o.GetValue (field);
2929 TypeMirror enumType = (f as EnumMirror).Type;
2931 o.SetValue (field, vm.CreateEnumMirror (enumType, vm.CreateValue (1)));
2932 f = o.GetValue (field);
2933 Assert.AreEqual (1, (f as EnumMirror).Value);
2935 // Argument checking
2936 AssertThrows<ArgumentNullException> (delegate () {
2937 vm.CreateEnumMirror (enumType, null);
2940 AssertThrows<ArgumentNullException> (delegate () {
2941 vm.CreateEnumMirror (null, vm.CreateValue (1));
2945 AssertThrows<ArgumentException> (delegate () {
2946 vm.CreateEnumMirror (enumType, vm.CreateValue (null));
2949 // value of a wrong type
2950 AssertThrows<ArgumentException> (delegate () {
2951 vm.CreateEnumMirror (enumType, vm.CreateValue ((long)1));
2956 public void VirtualMachine_EnableEvents_Breakpoint () {
2957 AssertThrows<ArgumentException> (delegate () {
2958 vm.EnableEvents (EventType.Breakpoint);
2963 public void SingleStepRegress654694 () {
2966 MethodMirror m = entry_point.DeclaringType.GetMethod ("ss_regress_654694");
2967 foreach (Location l in m.Locations) {
2968 if (l.ILOffset > 0 && il_offset == -1)
2969 il_offset = l.ILOffset;
2972 Event e = run_until ("ss_regress_654694");
2974 Assert.IsNotNull (m);
2975 vm.SetBreakpoint (m, il_offset);
2979 e = GetNextEvent ();
2980 Assert.IsTrue (e is BreakpointEvent);
2982 var req = create_step (e);
2983 req.Depth = StepDepth.Over;
2984 req.Size = StepSize.Line;
2989 e = GetNextEvent ();
2990 Assert.IsTrue (e is StepEvent);
2996 public void DebugBreak () {
2997 vm.EnableEvents (EventType.UserBreak);
3002 var e = GetNextEvent ();
3003 Assert.IsTrue (e is UserBreakEvent);
3007 public void DebugLog () {
3008 vm.EnableEvents (EventType.UserLog);
3013 var e = GetNextEvent ();
3014 Assert.IsTrue (e is UserLogEvent);
3015 var le = e as UserLogEvent;
3017 Assert.AreEqual (5, le.Level);
3018 Assert.AreEqual ("A", le.Category);
3019 Assert.AreEqual ("B", le.Message);
3023 public void TypeGetMethodsByNameFlags () {
3025 var assembly = entry_point.DeclaringType.Assembly;
3026 var type = assembly.GetType ("Tests3");
3028 Assert.IsNotNull (type);
3030 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Public, false);
3031 Assert.AreEqual (1, mm.Length, "#1");
3032 Assert.AreEqual ("M1", mm[0].Name, "#2");
3034 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.NonPublic, false);
3035 Assert.AreEqual (1, mm.Length, "#3");
3036 Assert.AreEqual ("M2", mm[0].Name, "#4");
3038 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public, false);
3039 Assert.AreEqual (7, mm.Length, "#5"); //M3 plus Equals, GetHashCode, GetType, ToString, .ctor
3041 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, false);
3042 Assert.AreEqual (2, mm.Length, "#7");
3044 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
3045 Assert.AreEqual (1, mm.Length, "#9");
3047 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
3048 Assert.AreEqual (5, mm.Length, "#11");
3051 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Public, false);
3052 Assert.AreEqual (1, mm.Length, "#12");
3053 Assert.AreEqual ("M1", mm[0].Name, "#13");
3055 mm = type.GetMethodsByNameFlags ("m1", BindingFlags.Static | BindingFlags.Public, true);
3056 Assert.AreEqual (1, mm.Length, "#14");
3057 Assert.AreEqual ("M1", mm[0].Name, "#15");
3059 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, false);
3060 Assert.AreEqual (1, mm.Length, "#16");
3061 Assert.AreEqual ("M1", mm[0].Name, "#17");
3065 [Category ("only88")]
3066 public void TypeLoadSourceFileFilter () {
3067 Event e = run_until ("type_load");
3069 if (!vm.Version.AtLeast (2, 7))
3072 string srcfile = (e as BreakpointEvent).Method.DeclaringType.GetSourceFiles (true)[0];
3074 var req = vm.CreateTypeLoadRequest ();
3075 req.SourceFileFilter = new string [] { srcfile.ToUpper () };
3079 e = GetNextEvent ();
3080 Assert.IsTrue (e is TypeLoadEvent);
3081 Assert.AreEqual ("TypeLoadClass", (e as TypeLoadEvent).Type.FullName);
3085 public void TypeLoadTypeNameFilter () {
3086 Event e = run_until ("type_load");
3088 var req = vm.CreateTypeLoadRequest ();
3089 req.TypeNameFilter = new string [] { "TypeLoadClass2" };
3093 e = GetNextEvent ();
3094 Assert.IsTrue (e is TypeLoadEvent);
3095 Assert.AreEqual ("TypeLoadClass2", (e as TypeLoadEvent).Type.FullName);
3099 public void GetTypesForSourceFile () {
3102 var types = vm.GetTypesForSourceFile ("dtest-app.cs", false);
3103 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
3104 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
3106 types = vm.GetTypesForSourceFile ("DTEST-app.cs", true);
3107 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
3108 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
3112 public void GetTypesNamed () {
3115 var types = vm.GetTypes ("Tests", false);
3116 Assert.AreEqual (1, types.Count);
3117 Assert.AreEqual ("Tests", types [0].FullName);
3119 types = vm.GetTypes ("System.Exception", false);
3120 Assert.AreEqual (1, types.Count);
3121 Assert.AreEqual ("System.Exception", types [0].FullName);
3125 public void String_GetChars () {
3129 var e = run_until ("arg2");
3131 var frame = e.Thread.GetFrames () [0];
3133 val = frame.GetArgument (0);
3134 Assert.IsTrue (val is StringMirror);
3135 AssertValue ("FOO", val);
3136 var s = (val as StringMirror);
3137 Assert.AreEqual (3, s.Length);
3139 var c = s.GetChars (0, 2);
3140 Assert.AreEqual (2, c.Length);
3141 Assert.AreEqual ('F', c [0]);
3142 Assert.AreEqual ('O', c [1]);
3144 AssertThrows<ArgumentException> (delegate () {
3150 public void GetInterfaces () {
3151 var e = run_until ("arg2");
3153 var frame = e.Thread.GetFrames () [0];
3155 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
3156 var ifaces = cl1.GetInterfaces ();
3157 Assert.AreEqual (1, ifaces.Length);
3158 Assert.AreEqual ("ITest", ifaces [0].Name);
3160 var cl2 = cl1.GetMethod ("Baz").ReturnType;
3161 var ifaces2 = cl2.GetInterfaces ();
3162 Assert.AreEqual (1, ifaces2.Length);
3163 Assert.AreEqual ("ITest`1", ifaces2 [0].Name);
3167 public void GetInterfaceMap () {
3168 var e = run_until ("arg2");
3170 var frame = e.Thread.GetFrames () [0];
3172 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
3173 var iface = cl1.Assembly.GetType ("ITest");
3174 var map = cl1.GetInterfaceMap (iface);
3175 Assert.AreEqual (cl1, map.TargetType);
3176 Assert.AreEqual (iface, map.InterfaceType);
3177 Assert.AreEqual (2, map.InterfaceMethods.Length);
3178 Assert.AreEqual (2, map.TargetMethods.Length);
3182 public void StackAlloc_Breakpoints_Regress2775 () {
3183 // Check that breakpoints on arm don't overwrite stackalloc-ed memory
3184 var e = run_until ("regress_2755");
3186 var frame = e.Thread.GetFrames () [0];
3188 // This breaks at the call site
3189 vm.SetBreakpoint (m, m.Locations [2].ILOffset);
3192 var e2 = GetNextEvent ();
3193 Assert.IsTrue (e2 is BreakpointEvent);
3195 e = run_until ("regress_2755_3");
3196 frame = e.Thread.GetFrames () [1];
3197 var res = frame.GetValue (m.GetLocal ("sum"));
3198 AssertValue (0, res);
3202 public void MethodInfo () {
3203 Event e = run_until ("locals2");
3205 StackFrame frame = e.Thread.GetFrames () [0];
3206 var m = frame.Method;
3208 Assert.IsTrue (m.IsGenericMethod);
3209 Assert.IsFalse (m.IsGenericMethodDefinition);
3211 var args = m.GetGenericArguments ();
3212 Assert.AreEqual (1, args.Length);
3213 Assert.AreEqual ("String", args [0].Name);
3215 var gmd = m.GetGenericMethodDefinition ();
3216 Assert.IsTrue (gmd.IsGenericMethod);
3217 Assert.IsTrue (gmd.IsGenericMethodDefinition);
3218 Assert.AreEqual (gmd, gmd.GetGenericMethodDefinition ());
3220 args = gmd.GetGenericArguments ();
3221 Assert.AreEqual (1, args.Length);
3222 Assert.AreEqual ("T", args [0].Name);
3224 var attrs = m.GetCustomAttributes (true);
3225 Assert.AreEqual (1, attrs.Length);
3226 Assert.AreEqual ("StateMachineAttribute", attrs [0].Constructor.DeclaringType.Name);
3230 public void UnhandledException () {
3233 Start (new string [] { "dtest-app.exe", "unhandled-exception" });
3235 var req = vm.CreateExceptionRequest (null, false, true);
3238 var e = run_until ("unhandled_exception");
3241 var e2 = GetNextEvent ();
3242 Assert.IsTrue (e2 is ExceptionEvent);
3249 public void UnhandledException_2 () {
3252 Start (new string [] { "dtest-app.exe", "unhandled-exception-endinvoke" });
3254 var req = vm.CreateExceptionRequest (null, false, true);
3257 MethodMirror m = entry_point.DeclaringType.GetMethod ("unhandled_exception_endinvoke_2");
3258 Assert.IsNotNull (m);
3259 vm.SetBreakpoint (m, m.ILOffsets [0]);
3261 var e = run_until ("unhandled_exception_endinvoke");
3264 var e2 = GetNextEvent ();
3265 Assert.IsFalse (e2 is ExceptionEvent);
3273 public void UnhandledExceptionUserCode () {
3276 // Exceptions caught in non-user code are treated as unhandled
3277 Start (new string [] { "dtest-app.exe", "unhandled-exception-user" });
3279 var req = vm.CreateExceptionRequest (null, false, true);
3280 req.AssemblyFilter = new List<AssemblyMirror> () { entry_point.DeclaringType.Assembly };
3283 var e = run_until ("unhandled_exception_user");
3286 var e2 = GetNextEvent ();
3287 Assert.IsTrue (e2 is ExceptionEvent);
3295 public void GCWhileSuspended () {
3296 // Check that objects are kept alive during suspensions
3297 Event e = run_until ("gc_suspend_1");
3299 MethodMirror m = entry_point.DeclaringType.GetMethod ("gc_suspend_invoke");
3301 var o = entry_point.DeclaringType.GetValue (entry_point.DeclaringType.GetField ("gc_suspend_field")) as ObjectMirror;
3302 //Console.WriteLine (o);
3304 StackFrame frame = e.Thread.GetFrames () [0];
3305 TypeMirror t = frame.Method.DeclaringType;
3306 for (int i = 0; i < 10; ++i)
3307 t.InvokeMethod (e.Thread, m, new Value [] { });
3309 // This throws an exception if the object is collected
3310 long addr = o.Address;
3312 var o2 = entry_point.DeclaringType.GetValue (entry_point.DeclaringType.GetField ("gc_suspend_field")) as ObjectMirror;
3317 public void MakeGenericMethod () {
3318 Event e = run_until ("bp1");
3320 var intm = vm.RootDomain.GetCorrespondingType (typeof (int));
3321 var stringm = vm.RootDomain.GetCorrespondingType (typeof (string));
3322 var gm = entry_point.DeclaringType.GetMethod ("generic_method");
3323 var res = gm.MakeGenericMethod (new TypeMirror [] { stringm });
3324 var args = res.GetGenericArguments ();
3325 Assert.AreEqual (1, args.Length);
3326 Assert.AreEqual (stringm, args [0]);
3329 AssertThrows<ArgumentNullException> (delegate {
3330 gm.MakeGenericMethod (null);
3332 AssertThrows<ArgumentNullException> (delegate {
3333 gm.MakeGenericMethod (new TypeMirror [] { null });
3335 AssertThrows<ArgumentException> (delegate {
3336 gm.MakeGenericMethod (new TypeMirror [] { stringm, stringm });
3338 AssertThrows<InvalidOperationException> (delegate {
3339 gm.MakeGenericMethod (new TypeMirror [] { intm });
3341 AssertThrows<InvalidOperationException> (delegate {
3342 entry_point.DeclaringType.GetMethod ("Main").MakeGenericMethod (new TypeMirror [] { intm });
3347 public void InspectThreadSuspenedOnWaitOne () {
3349 Start (true, "dtest-app.exe", "wait-one" );
3351 ThreadMirror.NativeTransitions = true;
3353 var evt = run_until ("wait_one");
3354 Assert.IsNotNull (evt, "#1");
3356 var thread = evt.Thread;
3357 Assert.AreEqual (ThreadState.Running, thread.ThreadState, "#1.1");
3359 var frames = thread.GetFrames ();
3360 Assert.IsNotNull (frames, "#2");
3361 Assert.AreEqual (2, frames.Length, "#3");
3362 Assert.AreEqual ("wait_one", frames [0].Method.Name, "#4");
3363 Assert.AreEqual ("Main", frames [1].Method.Name, "#5");
3367 Thread.Sleep (500); //FIXME this is racy, maybe single step? or something?
3370 Assert.AreEqual (ThreadState.WaitSleepJoin, thread.ThreadState, "#6");
3372 frames = thread.GetFrames ();
3373 Assert.AreEqual (4, frames.Length, "#7");
3374 Assert.AreEqual ("WaitOne_internal", frames [0].Method.Name, "#8");
3375 Assert.AreEqual ("WaitOne", frames [1].Method.Name, "#8.1");
3376 Assert.AreEqual ("wait_one", frames [2].Method.Name, "#9");
3377 Assert.AreEqual ("Main", frames [3].Method.Name, "#10");
3380 var frame = frames [0];
3381 Assert.IsTrue (frame.IsNativeTransition, "#11.1");
3384 Assert.Fail ("Known limitation - can't get info from m2n frames");
3385 } catch (AbsentInformationException) {}
3388 Assert.IsFalse (frame.IsNativeTransition, "#12.1");
3389 var wait_one_this = frame.GetThis ();
3390 Assert.IsNotNull (wait_one_this, "#12.2");
3393 var locals = frame.GetVisibleVariables ();
3394 Assert.AreEqual (1, locals.Count, "#13.1");
3396 var local_0 = frame.GetValue (locals [0]);
3397 Assert.IsNotNull (local_0, "#13.2");
3399 Assert.AreEqual (wait_one_this, local_0, "#14.2");