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 next line
425 assert_location (e, "single_stepping");
427 // Step into ss3_2 ()
429 assert_location (e, "ss3_2");
431 // Step over ss3_2_2 ()
433 assert_location (e, "ss3_2");
435 // Recreate the request
442 // Step back into single_stepping () with the new request
444 assert_location (e, "single_stepping");
448 assert_location (e, "ss4");
453 // Change to StepSize.Line
455 req.Depth = StepDepth.Over;
456 req.Size = StepSize.Line;
459 // Step over ss1 (); ss1 ();
464 req.Depth = StepDepth.Into;
468 assert_location (e, "ss2");
473 e = run_until ("ss5");
475 // Add an assembly filter
476 req.AssemblyFilter = new AssemblyMirror [] { (e as BreakpointEvent).Method.DeclaringType.Assembly };
482 // Step into is_even, skipping the linq stuff
484 assert_location (e, "is_even");
486 // FIXME: Check that single stepping works with lock (obj)
490 e = run_until ("ss6");
492 req = create_step (e);
493 req.Depth = StepDepth.Over;
496 // Check that single stepping works in out-of-line bblocks
499 assert_location (e, "ss6");
502 // Check that a step over stops at an EH clause
503 e = run_until ("ss7_2");
504 req = create_step (e);
505 req.Depth = StepDepth.Out;
508 assert_location (e, "ss7");
510 req = create_step (e);
511 req.Depth = StepDepth.Over;
514 assert_location (e, "ss7");
517 // Check that stepping stops between nested calls
518 e = run_until ("ss_nested_2");
520 assert_location (e, "ss_nested");
522 assert_location (e, "ss_nested_1");
524 assert_location (e, "ss_nested");
525 // Check that step over steps over nested calls
527 assert_location (e, "ss_nested");
529 assert_location (e, "ss_nested_1");
531 assert_location (e, "ss_nested_1");
533 assert_location (e, "ss_nested");
536 // Check DebuggerStepThrough support
537 e = run_until ("ss_step_through");
538 req = create_step (e);
539 req.Filter = StepFilter.DebuggerStepThrough;
541 // Step through step_through_1 ()
543 assert_location (e, "ss_step_through");
544 // Step through StepThroughClass.step_through_2 ()
546 assert_location (e, "ss_step_through");
548 req.Filter = StepFilter.None;
550 assert_location (e, "step_through_3");
553 // Check DebuggerNonUserCode support
554 e = run_until ("ss_non_user_code");
555 req = create_step (e);
556 req.Filter = StepFilter.DebuggerNonUserCode;
558 // Step through non_user_code_1 ()
560 assert_location (e, "ss_non_user_code");
561 // Step through StepThroughClass.non_user_code_2 ()
563 assert_location (e, "ss_non_user_code");
565 req.Filter = StepFilter.None;
567 assert_location (e, "non_user_code_3");
570 // Check that step-over doesn't stop at inner frames with recursive functions
571 e = run_until ("ss_recursive");
572 req = create_step (e);
576 var f = e.Thread.GetFrames () [0];
577 assert_location (e, "ss_recursive");
578 AssertValue (1, f.GetValue (f.Method.GetLocal ("n")));
581 // Check that single stepping doesn't clobber fp values
582 e = run_until ("ss_fp_clobber");
583 req = create_step (e);
585 f = e.Thread.GetFrames ()[0];
587 if ((e as StepEvent).Method.Name == "ss_fp_clobber_2")
591 f = e.Thread.GetFrames ()[0];
592 AssertValue (7.0, f.GetValue (f.Method.GetParameters ()[0]));
597 public void MethodEntryExit () {
598 run_until ("single_stepping");
600 var req1 = vm.CreateMethodEntryRequest ();
601 var req2 = vm.CreateMethodExitRequest ();
607 Event e = GetNextEvent ();
608 Assert.IsTrue (e is MethodEntryEvent);
609 Assert.AreEqual ("ss1", (e as MethodEntryEvent).Method.Name);
613 Assert.IsTrue (e is MethodExitEvent);
614 Assert.AreEqual ("ss1", (e as MethodExitEvent).Method.Name);
621 public void CountFilter () {
622 run_until ("single_stepping");
624 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("ss3");
625 Assert.IsNotNull (m2);
626 vm.SetBreakpoint (m2, 0);
628 var req1 = vm.CreateMethodEntryRequest ();
632 // Enter ss2, ss1 is skipped
634 Event e = GetNextEvent ();
635 Assert.IsTrue (e is MethodEntryEvent);
636 Assert.AreEqual ("ss2", (e as MethodEntryEvent).Method.Name);
638 // Breakpoint on ss3, the entry event is no longer reported
641 Assert.IsTrue (e is BreakpointEvent);
647 public void Arguments () {
650 var e = run_until ("arg1");
652 StackFrame frame = e.Thread.GetFrames () [0];
654 check_arg_val (frame, 0, typeof (sbyte), SByte.MaxValue - 5);
655 check_arg_val (frame, 1, typeof (byte), Byte.MaxValue - 5);
656 check_arg_val (frame, 2, typeof (bool), true);
657 check_arg_val (frame, 3, typeof (short), Int16.MaxValue - 5);
658 check_arg_val (frame, 4, typeof (ushort), UInt16.MaxValue - 5);
659 check_arg_val (frame, 5, typeof (char), 'F');
660 check_arg_val (frame, 6, typeof (int), Int32.MaxValue - 5);
661 check_arg_val (frame, 7, typeof (uint), UInt32.MaxValue - 5);
662 check_arg_val (frame, 8, typeof (long), Int64.MaxValue - 5);
663 check_arg_val (frame, 9, typeof (ulong), UInt64.MaxValue - 5);
664 check_arg_val (frame, 10, typeof (float), 1.2345f);
665 check_arg_val (frame, 11, typeof (double), 6.78910);
667 e = run_until ("arg2");
669 frame = e.Thread.GetFrames () [0];
672 val = frame.GetArgument (0);
673 AssertValue ("FOO", val);
674 Assert.AreEqual ("String", (val as ObjectMirror).Type.Name);
677 val = frame.GetArgument (1);
678 AssertValue (null, val);
681 val = frame.GetArgument (2);
682 AssertValue ("BLA", val);
685 val = frame.GetArgument (3);
686 AssertValue (42, val);
689 val = frame.GetArgument (4);
690 Assert.IsTrue (val is ObjectMirror);
691 Assert.AreEqual ("GClass`1", (val as ObjectMirror).Type.Name);
694 val = frame.GetArgument (5);
695 Assert.IsTrue (val is ObjectMirror);
696 Assert.AreEqual ("Object", (val as ObjectMirror).Type.Name);
698 // this on static methods
699 val = frame.GetThis ();
700 AssertValue (null, val);
702 e = run_until ("arg3");
704 frame = e.Thread.GetFrames () [0];
707 val = frame.GetThis ();
708 Assert.IsTrue (val is ObjectMirror);
709 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
711 // objref in register
712 val = frame.GetArgument (0);
713 AssertValue ("BLA", val);
717 public void Arrays () {
720 var e = run_until ("o2");
722 StackFrame frame = e.Thread.GetFrames () [0];
725 val = frame.GetArgument (0);
726 Assert.IsTrue (val is ArrayMirror);
727 ArrayMirror arr = val as ArrayMirror;
728 Assert.AreEqual (2, arr.Length);
729 AssertValue ("BAR", arr [0]);
730 AssertValue ("BAZ", arr [1]);
732 var vals = arr.GetValues (0, 2);
733 Assert.AreEqual (2, vals.Count);
734 AssertValue ("BAR", vals [0]);
735 AssertValue ("BAZ", vals [1]);
737 arr [0] = vm.RootDomain.CreateString ("ABC");
738 AssertValue ("ABC", arr [0]);
740 arr [0] = vm.CreateValue (null);
741 AssertValue (null, arr [0]);
743 arr.SetValues (0, new Value [] { vm.RootDomain.CreateString ("D1"), vm.RootDomain.CreateString ("D2") });
744 AssertValue ("D1", arr [0]);
745 AssertValue ("D2", arr [1]);
748 val = frame.GetArgument (1);
749 Assert.IsTrue (val is ArrayMirror);
750 arr = val as ArrayMirror;
751 Assert.AreEqual (2, arr.Length);
752 AssertValue (42, arr [0]);
753 AssertValue (43, arr [1]);
756 AssertThrows<IndexOutOfRangeException> (delegate () {
760 AssertThrows<IndexOutOfRangeException> (delegate () {
761 val = arr [Int32.MinValue];
764 AssertThrows<IndexOutOfRangeException> (delegate () {
765 vals = arr.GetValues (0, 3);
768 AssertThrows<IndexOutOfRangeException> (delegate () {
769 arr [2] = vm.CreateValue (null);
772 AssertThrows<IndexOutOfRangeException> (delegate () {
773 arr [Int32.MinValue] = vm.CreateValue (null);
776 AssertThrows<IndexOutOfRangeException> (delegate () {
777 arr.SetValues (0, new Value [] { null, null, null });
781 val = frame.GetArgument (2);
782 Assert.IsTrue (val is ArrayMirror);
783 arr = val as ArrayMirror;
784 Assert.AreEqual (2, arr.Rank);
785 Assert.AreEqual (4, arr.Length);
786 Assert.AreEqual (2, arr.GetLength (0));
787 Assert.AreEqual (2, arr.GetLength (1));
788 Assert.AreEqual (0, arr.GetLowerBound (0));
789 Assert.AreEqual (0, arr.GetLowerBound (1));
790 vals = arr.GetValues (0, 4);
791 AssertValue (1, vals [0]);
792 AssertValue (2, vals [1]);
793 AssertValue (3, vals [2]);
794 AssertValue (4, vals [3]);
796 val = frame.GetArgument (3);
797 Assert.IsTrue (val is ArrayMirror);
798 arr = val as ArrayMirror;
799 Assert.AreEqual (2, arr.Rank);
800 Assert.AreEqual (4, arr.Length);
801 Assert.AreEqual (2, arr.GetLength (0));
802 Assert.AreEqual (2, arr.GetLength (1));
803 Assert.AreEqual (1, arr.GetLowerBound (0));
804 Assert.AreEqual (3, arr.GetLowerBound (1));
806 AssertThrows<ArgumentOutOfRangeException> (delegate () {
809 AssertThrows<ArgumentOutOfRangeException> (delegate () {
813 AssertThrows<ArgumentOutOfRangeException> (delegate () {
814 arr.GetLowerBound (-1);
816 AssertThrows<ArgumentOutOfRangeException> (delegate () {
817 arr.GetLowerBound (2);
820 // arrays treated as generic collections
821 val = frame.GetArgument (4);
822 Assert.IsTrue (val is ArrayMirror);
823 arr = val as ArrayMirror;
827 public void Object_GetValue () {
828 var e = run_until ("o1");
829 var frame = e.Thread.GetFrames () [0];
831 object val = frame.GetThis ();
832 Assert.IsTrue (val is ObjectMirror);
833 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
834 ObjectMirror o = (val as ObjectMirror);
836 TypeMirror t = o.Type;
839 object f = o.GetValue (t.GetField ("field_i"));
841 f = o.GetValue (t.GetField ("field_s"));
842 AssertValue ("S", f);
843 f = o.GetValue (t.GetField ("field_enum"));
844 Assert.IsTrue (f is EnumMirror);
845 Assert.AreEqual (1, (f as EnumMirror).Value);
846 Assert.AreEqual ("B", (f as EnumMirror).StringValue);
848 // Inherited object fields
849 TypeMirror parent = t.BaseType;
850 f = o.GetValue (parent.GetField ("base_field_i"));
852 f = o.GetValue (parent.GetField ("base_field_s"));
853 AssertValue ("T", f);
856 f = o.GetValue (o.Type.GetField ("static_i"));
860 ObjectMirror o2 = frame.GetValue (frame.Method.GetParameters ()[1]) as ObjectMirror;
861 Assert.AreEqual ("GClass`1", o2.Type.Name);
862 TypeMirror t2 = o2.Type;
863 f = o2.GetValue (t2.GetField ("field"));
866 ObjectMirror o3 = frame.GetValue (frame.Method.GetParameters ()[2]) as ObjectMirror;
867 Assert.AreEqual ("GClass`1", o3.Type.Name);
868 TypeMirror t3 = o3.Type;
869 f = o3.GetValue (t3.GetField ("field"));
870 AssertValue ("FOO", f);
873 AssertThrows<ArgumentNullException> (delegate () {
879 public void Object_GetValues () {
880 var e = run_until ("o1");
881 var frame = e.Thread.GetFrames () [0];
883 object val = frame.GetThis ();
884 Assert.IsTrue (val is ObjectMirror);
885 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
886 ObjectMirror o = (val as ObjectMirror);
888 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
890 TypeMirror t = o.Type;
892 object[] vals = o.GetValues (new FieldInfoMirror [] { t.GetField ("field_i"), t.GetField ("field_s") });
896 AssertValue ("S", f);
899 AssertThrows<ArgumentNullException> (delegate () {
903 AssertThrows<ArgumentNullException> (delegate () {
904 o.GetValues (new FieldInfoMirror [] { null });
907 // field of another class
908 AssertThrows<ArgumentException> (delegate () {
909 o.GetValue (val2.Type.GetField ("field_j"));
913 void TestSetValue (ObjectMirror o, string field_name, object val) {
915 o.SetValue (o.Type.GetField (field_name), vm.RootDomain.CreateString ((string)val));
917 o.SetValue (o.Type.GetField (field_name), vm.CreateValue (val));
918 Value f = o.GetValue (o.Type.GetField (field_name));
919 AssertValue (val, f);
923 public void Object_SetValues () {
924 var e = run_until ("o1");
925 var frame = e.Thread.GetFrames () [0];
927 object val = frame.GetThis ();
928 Assert.IsTrue (val is ObjectMirror);
929 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
930 ObjectMirror o = (val as ObjectMirror);
932 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
934 TestSetValue (o, "field_i", 22);
935 TestSetValue (o, "field_bool1", false);
936 TestSetValue (o, "field_bool2", true);
937 TestSetValue (o, "field_char", 'B');
938 TestSetValue (o, "field_byte", (byte)129);
939 TestSetValue (o, "field_sbyte", (sbyte)-33);
940 TestSetValue (o, "field_short", (short)(Int16.MaxValue - 5));
941 TestSetValue (o, "field_ushort", (ushort)(UInt16.MaxValue - 5));
942 TestSetValue (o, "field_long", Int64.MaxValue - 5);
943 TestSetValue (o, "field_ulong", (ulong)(UInt64.MaxValue - 5));
944 TestSetValue (o, "field_float", 6.28f);
945 TestSetValue (o, "field_double", 6.28);
946 TestSetValue (o, "static_i", 23);
947 TestSetValue (o, "field_s", "CDEF");
952 f = o.GetValue (o.Type.GetField ("field_intptr"));
953 Assert.IsInstanceOfType (typeof (StructMirror), f);
954 AssertValue (Int32.MaxValue - 5, (f as StructMirror).Fields [0]);
957 FieldInfoMirror field = o.Type.GetField ("field_enum");
958 f = o.GetValue (field);
959 (f as EnumMirror).Value = 5;
960 o.SetValue (field, f);
961 f = o.GetValue (field);
962 Assert.AreEqual (5, (f as EnumMirror).Value);
965 o.SetValue (o.Type.GetField ("field_s"), vm.CreateValue (null));
966 f = o.GetValue (o.Type.GetField ("field_s"));
967 AssertValue (null, f);
970 field = o.Type.GetField ("generic_field_struct");
971 f = o.GetValue (field);
972 o.SetValue (field, f);
975 field = o.Type.GetField ("field_nullable");
976 f = o.GetValue (field);
977 AssertValue (0, (f as StructMirror).Fields [0]);
978 AssertValue (false, (f as StructMirror).Fields [1]);
979 o.SetValue (field, vm.CreateValue (6));
980 f = o.GetValue (field);
981 AssertValue (6, (f as StructMirror).Fields [0]);
982 AssertValue (true, (f as StructMirror).Fields [1]);
983 o.SetValue (field, vm.CreateValue (null));
984 f = o.GetValue (field);
985 AssertValue (0, (f as StructMirror).Fields [0]);
986 AssertValue (false, (f as StructMirror).Fields [1]);
989 AssertThrows<ArgumentNullException> (delegate () {
990 o.SetValues (null, new Value [0]);
993 AssertThrows<ArgumentNullException> (delegate () {
994 o.SetValues (new FieldInfoMirror [0], null);
997 AssertThrows<ArgumentNullException> (delegate () {
998 o.SetValues (new FieldInfoMirror [] { null }, new Value [1] { null });
1001 // vtype with a wrong type
1002 AssertThrows<ArgumentException> (delegate () {
1003 o.SetValue (o.Type.GetField ("field_struct"), o.GetValue (o.Type.GetField ("field_enum")));
1006 // reference type not assignment compatible
1007 AssertThrows<ArgumentException> (delegate () {
1008 o.SetValue (o.Type.GetField ("field_class"), o);
1011 // field of another class
1012 AssertThrows<ArgumentException> (delegate () {
1013 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
1018 public void Type_SetValue () {
1019 var e = run_until ("o1");
1020 var frame = e.Thread.GetFrames () [0];
1023 object val = frame.GetThis ();
1024 Assert.IsTrue (val is ObjectMirror);
1025 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
1026 ObjectMirror o = (val as ObjectMirror);
1028 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1030 o.Type.SetValue (o.Type.GetField ("static_i"), vm.CreateValue (55));
1031 f = o.Type.GetValue (o.Type.GetField ("static_i"));
1032 AssertValue (55, f);
1034 o.Type.SetValue (o.Type.GetField ("static_s"), vm.RootDomain.CreateString ("B"));
1035 f = o.Type.GetValue (o.Type.GetField ("static_s"));
1036 AssertValue ("B", f);
1038 // Argument checking
1039 AssertThrows<ArgumentNullException> (delegate () {
1040 o.Type.SetValue (null, vm.CreateValue (0));
1043 AssertThrows<ArgumentNullException> (delegate () {
1044 o.Type.SetValue (o.Type.GetField ("static_i"), null);
1047 // field of another class
1048 AssertThrows<ArgumentException> (delegate () {
1049 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
1054 public void TypeInfo () {
1055 Event e = run_until ("ti2");
1056 StackFrame frame = e.Thread.GetFrames () [0];
1061 t = frame.Method.GetParameters ()[0].ParameterType;
1063 Assert.AreEqual ("String[]", t.Name);
1064 Assert.AreEqual ("string[]", t.CSharpName);
1065 Assert.AreEqual ("Array", t.BaseType.Name);
1066 Assert.AreEqual (true, t.HasElementType);
1067 Assert.AreEqual (true, t.IsArray);
1068 Assert.AreEqual (1, t.GetArrayRank ());
1069 Assert.AreEqual ("String", t.GetElementType ().Name);
1071 t = frame.Method.GetParameters ()[2].ParameterType;
1073 Assert.AreEqual ("Int32[,]", t.Name);
1075 //Assert.AreEqual ("int[,]", t.CSharpName);
1076 Assert.AreEqual ("Array", t.BaseType.Name);
1077 Assert.AreEqual (true, t.HasElementType);
1078 Assert.AreEqual (true, t.IsArray);
1079 Assert.AreEqual (2, t.GetArrayRank ());
1080 Assert.AreEqual ("Int32", t.GetElementType ().Name);
1083 t = frame.Method.GetParameters ()[3].ParameterType;
1085 //Assert.AreEqual ("Int32&", t.Name);
1086 //Assert.AreEqual (true, t.IsByRef);
1087 //Assert.AreEqual (true, t.HasElementType);
1090 t = frame.Method.GetParameters ()[4].ParameterType;
1092 //Assert.AreEqual ("Int32*", t.Name);
1093 Assert.AreEqual (true, t.IsPointer);
1094 Assert.AreEqual (true, t.HasElementType);
1095 Assert.AreEqual ("Int32", t.GetElementType ().Name);
1096 Assert.AreEqual (false, t.IsPrimitive);
1099 t = frame.Method.GetParameters ()[5].ParameterType;
1100 Assert.AreEqual (true, t.IsPrimitive);
1103 t = frame.Method.GetParameters ()[6].ParameterType;
1104 Assert.AreEqual ("AStruct", t.Name);
1105 Assert.AreEqual (false, t.IsPrimitive);
1106 Assert.AreEqual (true, t.IsValueType);
1107 Assert.AreEqual (false, t.IsClass);
1110 t = frame.Method.GetParameters ()[7].ParameterType;
1111 Assert.AreEqual ("Tests", t.Name);
1112 var nested = (from nt in t.GetNestedTypes () where nt.IsNestedPublic select nt).ToArray ();
1113 Assert.AreEqual (1, nested.Length);
1114 Assert.AreEqual ("NestedClass", nested [0].Name);
1115 Assert.IsTrue (t.BaseType.IsAssignableFrom (t));
1116 Assert.IsTrue (!t.IsAssignableFrom (t.BaseType));
1118 // generic instances
1119 t = frame.Method.GetParameters ()[9].ParameterType;
1120 Assert.AreEqual ("GClass`1", t.Name);
1121 Assert.IsTrue (t.IsGenericType);
1122 Assert.IsFalse (t.IsGenericTypeDefinition);
1124 var args = t.GetGenericArguments ();
1125 Assert.AreEqual (1, args.Length);
1126 Assert.AreEqual ("Int32", args [0].Name);
1128 // generic type definitions
1129 var gtd = t.GetGenericTypeDefinition ();
1130 Assert.AreEqual ("GClass`1", gtd.Name);
1131 Assert.IsTrue (gtd.IsGenericType);
1132 Assert.IsTrue (gtd.IsGenericTypeDefinition);
1133 Assert.AreEqual (gtd, gtd.GetGenericTypeDefinition ());
1135 args = gtd.GetGenericArguments ();
1136 Assert.AreEqual (1, args.Length);
1137 Assert.AreEqual ("T", args [0].Name);
1140 t = frame.Method.GetParameters ()[10].ParameterType;
1141 Assert.AreEqual ("AnEnum", t.Name);
1142 Assert.IsTrue (t.IsEnum);
1143 Assert.AreEqual ("Int32", t.EnumUnderlyingType.Name);
1146 t = frame.Method.GetParameters ()[11].ParameterType;
1147 Assert.AreEqual ("TypedReference", t.Name);
1150 t = frame.Method.GetParameters ()[7].ParameterType;
1152 var props = t.GetProperties ();
1153 Assert.AreEqual (3, props.Length);
1154 foreach (PropertyInfoMirror prop in props) {
1155 ParameterInfoMirror[] indexes = prop.GetIndexParameters ();
1157 if (prop.Name == "IntProperty") {
1158 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1159 Assert.AreEqual ("get_IntProperty", prop.GetGetMethod ().Name);
1160 Assert.AreEqual ("set_IntProperty", prop.GetSetMethod ().Name);
1161 Assert.AreEqual (0, indexes.Length);
1162 } else if (prop.Name == "ReadOnlyProperty") {
1163 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1164 Assert.AreEqual ("get_ReadOnlyProperty", prop.GetGetMethod ().Name);
1165 Assert.AreEqual (null, prop.GetSetMethod ());
1166 Assert.AreEqual (0, indexes.Length);
1167 } else if (prop.Name == "IndexedProperty") {
1168 Assert.AreEqual (1, indexes.Length);
1169 Assert.AreEqual ("Int32", indexes [0].ParameterType.Name);
1173 // custom attributes
1174 t = frame.Method.GetParameters ()[8].ParameterType;
1175 Assert.AreEqual ("Tests2", t.Name);
1176 var attrs = t.GetCustomAttributes (true);
1177 Assert.AreEqual (5, attrs.Length);
1178 foreach (var attr in attrs) {
1179 if (attr.Constructor.DeclaringType.Name == "DebuggerDisplayAttribute") {
1180 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1181 Assert.AreEqual ("Tests", attr.ConstructorArguments [0].Value);
1182 Assert.AreEqual (2, attr.NamedArguments.Count);
1183 Assert.AreEqual ("Name", attr.NamedArguments [0].Property.Name);
1184 Assert.AreEqual ("FOO", attr.NamedArguments [0].TypedValue.Value);
1185 Assert.AreEqual ("Target", attr.NamedArguments [1].Property.Name);
1186 Assert.IsInstanceOfType (typeof (TypeMirror), attr.NamedArguments [1].TypedValue.Value);
1187 Assert.AreEqual ("Int32", (attr.NamedArguments [1].TypedValue.Value as TypeMirror).Name);
1188 } else if (attr.Constructor.DeclaringType.Name == "DebuggerTypeProxyAttribute") {
1189 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1190 Assert.IsInstanceOfType (typeof (TypeMirror), attr.ConstructorArguments [0].Value);
1191 Assert.AreEqual ("Tests", (attr.ConstructorArguments [0].Value as TypeMirror).Name);
1192 } else if (attr.Constructor.DeclaringType.Name == "BAttribute") {
1193 Assert.AreEqual (2, attr.NamedArguments.Count);
1194 Assert.AreEqual ("afield", attr.NamedArguments [0].Field.Name);
1195 Assert.AreEqual ("bfield", attr.NamedArguments [1].Field.Name);
1196 } else if (attr.Constructor.DeclaringType.Name == "ClassInterfaceAttribute") {
1197 // inherited from System.Object
1198 //} else if (attr.Constructor.DeclaringType.Name == "Serializable") {
1199 // inherited from System.Object
1200 } else if (attr.Constructor.DeclaringType.Name == "ComVisibleAttribute") {
1201 // inherited from System.Object
1203 Assert.Fail (attr.Constructor.DeclaringType.Name);
1207 var assembly = entry_point.DeclaringType.Assembly;
1208 var type = assembly.GetType ("Tests4");
1209 Assert.IsFalse (type.IsInitialized);
1213 public void FieldInfo () {
1214 Event e = run_until ("ti2");
1215 StackFrame frame = e.Thread.GetFrames () [0];
1219 t = frame.Method.GetParameters ()[8].ParameterType;
1220 Assert.AreEqual ("Tests2", t.Name);
1222 var fi = t.GetField ("field_j");
1223 var attrs = fi.GetCustomAttributes (true);
1224 Assert.AreEqual (1, attrs.Length);
1225 var attr = attrs [0];
1226 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1227 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1228 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1229 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1233 public void PropertyInfo () {
1234 Event e = run_until ("ti2");
1235 StackFrame frame = e.Thread.GetFrames () [0];
1239 t = frame.Method.GetParameters ()[8].ParameterType;
1240 Assert.AreEqual ("Tests2", t.Name);
1242 var pi = t.GetProperty ("AProperty");
1243 var attrs = pi.GetCustomAttributes (true);
1244 Assert.AreEqual (1, attrs.Length);
1245 var attr = attrs [0];
1246 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1247 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1248 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1249 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1253 [Category ("only5")]
1254 public void Type_GetValue () {
1255 Event e = run_until ("o1");
1256 StackFrame frame = e.Thread.GetFrames () [0];
1258 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1260 TypeMirror t = o.Type;
1262 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1265 object f = t.GetValue (o.Type.GetField ("static_i"));
1266 AssertValue (55, f);
1268 f = t.GetValue (o.Type.GetField ("static_s"));
1269 AssertValue ("A", f);
1271 // literal static fields
1272 f = t.GetValue (o.Type.GetField ("literal_i"));
1273 AssertValue (56, f);
1275 f = t.GetValue (o.Type.GetField ("literal_s"));
1276 AssertValue ("B", f);
1278 // Inherited static fields
1279 TypeMirror parent = t.BaseType;
1280 f = t.GetValue (parent.GetField ("base_static_i"));
1281 AssertValue (57, f);
1283 f = t.GetValue (parent.GetField ("base_static_s"));
1284 AssertValue ("C", f);
1286 // thread static field
1287 f = t.GetValue (t.GetField ("tls_i"), e.Thread);
1288 AssertValue (42, f);
1290 // Argument checking
1291 AssertThrows<ArgumentNullException> (delegate () {
1296 AssertThrows<ArgumentException> (delegate () {
1297 t.GetValue (o.Type.GetField ("field_i"));
1300 // field on another type
1301 AssertThrows<ArgumentException> (delegate () {
1302 t.GetValue (val2.Type.GetField ("static_field_j"));
1305 // special static field
1306 AssertThrows<ArgumentException> (delegate () {
1307 t.GetValue (t.GetField ("tls_i"));
1312 public void Type_GetValues () {
1313 Event e = run_until ("o1");
1314 StackFrame frame = e.Thread.GetFrames () [0];
1316 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1318 TypeMirror t = o.Type;
1321 object[] vals = t.GetValues (new FieldInfoMirror [] { t.GetField ("static_i"), t.GetField ("static_s") });
1322 object f = vals [0];
1323 AssertValue (55, f);
1326 AssertValue ("A", f);
1328 // Argument checking
1329 AssertThrows<ArgumentNullException> (delegate () {
1333 AssertThrows<ArgumentNullException> (delegate () {
1334 t.GetValues (new FieldInfoMirror [] { null });
1339 public void ObjRefs () {
1340 Event e = run_until ("objrefs1");
1341 StackFrame frame = e.Thread.GetFrames () [0];
1343 ObjectMirror o = frame.GetThis () as ObjectMirror;
1344 ObjectMirror child = o.GetValue (o.Type.GetField ("child")) as ObjectMirror;
1346 Assert.IsTrue (child.Address != 0);
1348 // Check that object references are internalized correctly
1349 Assert.AreEqual (o, frame.GetThis ());
1351 run_until ("objrefs2");
1353 // child should be gc'd now
1354 // This is not deterministic
1355 //Assert.IsTrue (child.IsCollected);
1358 * No longer works since Type is read eagerly
1361 AssertThrows<ObjectCollectedException> (delegate () {
1362 TypeMirror t = child.Type;
1366 AssertThrows<ObjectCollectedException> (delegate () {
1367 long addr = child.Address;
1373 public void Type_GetObject () {
1374 Event e = run_until ("o1");
1375 StackFrame frame = e.Thread.GetFrames () [0];
1377 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1379 TypeMirror t = o.Type;
1381 Assert.AreEqual ("MonoType", t.GetTypeObject ().Type.Name);
1385 public void VTypes () {
1386 Event e = run_until ("vtypes1");
1387 StackFrame frame = e.Thread.GetFrames () [0];
1390 ObjectMirror o = frame.GetThis () as ObjectMirror;
1391 var obj = o.GetValue (o.Type.GetField ("field_struct"));
1392 Assert.IsTrue (obj is StructMirror);
1393 var s = obj as StructMirror;
1394 Assert.AreEqual ("AStruct", s.Type.Name);
1395 AssertValue (42, s ["i"]);
1397 AssertValue ("S", obj);
1398 AssertValue (43, s ["k"]);
1399 obj = o.GetValue (o.Type.GetField ("field_boxed_struct"));
1400 Assert.IsTrue (obj is StructMirror);
1401 s = obj as StructMirror;
1402 Assert.AreEqual ("AStruct", s.Type.Name);
1403 AssertValue (42, s ["i"]);
1405 // Check decoding of nested structs (#14942)
1406 obj = o.GetValue (o.Type.GetField ("nested_struct"));
1407 o.SetValue (o.Type.GetField ("nested_struct"), obj);
1409 // Check round tripping of boxed struct fields (#12354)
1410 obj = o.GetValue (o.Type.GetField ("boxed_struct_field"));
1411 o.SetValue (o.Type.GetField ("boxed_struct_field"), obj);
1412 obj = o.GetValue (o.Type.GetField ("boxed_struct_field"));
1413 s = obj as StructMirror;
1414 AssertValue (1, s ["key"]);
1416 Assert.IsTrue (obj is StructMirror);
1417 s = obj as StructMirror;
1418 AssertValue (42, s ["m_value"]);
1420 // vtypes as arguments
1421 s = frame.GetArgument (0) as StructMirror;
1422 AssertValue (44, s ["i"]);
1424 AssertValue ("T", obj);
1425 AssertValue (45, s ["k"]);
1427 // vtypes as array entries
1428 var arr = frame.GetArgument (1) as ArrayMirror;
1430 Assert.IsTrue (obj is StructMirror);
1431 s = obj as StructMirror;
1432 AssertValue (1, s ["i"]);
1433 AssertValue ("S1", s ["s"]);
1435 Assert.IsTrue (obj is StructMirror);
1436 s = obj as StructMirror;
1437 AssertValue (2, s ["i"]);
1438 AssertValue ("S2", s ["s"]);
1441 var typedref = frame.GetArgument (2) as StructMirror;
1442 Assert.IsTrue (typedref is StructMirror);
1444 // Argument checking
1445 s = frame.GetArgument (0) as StructMirror;
1446 AssertThrows<ArgumentException> (delegate () {
1450 // generic vtype instances
1451 o = frame.GetThis () as ObjectMirror;
1452 obj = o.GetValue (o.Type.GetField ("generic_field_struct"));
1453 Assert.IsTrue (obj is StructMirror);
1454 s = obj as StructMirror;
1455 Assert.AreEqual ("GStruct`1", s.Type.Name);
1456 AssertValue (42, s ["i"]);
1458 // this on vtype methods
1459 e = run_until ("vtypes2");
1460 e = step_until (e.Thread, "foo");
1462 frame = e.Thread.GetFrames () [0];
1464 Assert.AreEqual ("foo", (e as StepEvent).Method.Name);
1465 obj = frame.GetThis ();
1467 Assert.IsTrue (obj is StructMirror);
1468 s = obj as StructMirror;
1469 AssertValue (44, s ["i"]);
1470 AssertValue ("T", s ["s"]);
1471 AssertValue (45, s ["k"]);
1473 // this on static vtype methods
1474 e = run_until ("vtypes3");
1475 e = step_until (e.Thread, "static_foo");
1477 frame = e.Thread.GetFrames () [0];
1479 Assert.AreEqual ("static_foo", (e as StepEvent).Method.Name);
1480 obj = frame.GetThis ();
1481 AssertValue (null, obj);
1483 // vtypes which reference themselves recursively
1484 e = run_until ("vtypes4_2");
1485 frame = e.Thread.GetFrames () [0];
1487 Assert.IsTrue (frame.GetArgument (0) is StructMirror);
1491 public void AssemblyInfo () {
1492 Event e = run_until ("single_stepping");
1494 StackFrame frame = e.Thread.GetFrames () [0];
1496 var aname = frame.Method.DeclaringType.Assembly.GetName ();
1497 Assert.AreEqual ("dtest-app, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", aname.ToString ());
1499 ModuleMirror m = frame.Method.DeclaringType.Module;
1501 Assert.AreEqual ("dtest-app.exe", m.Name);
1502 Assert.AreEqual ("dtest-app.exe", m.ScopeName);
1503 Assert.IsTrue (m.FullyQualifiedName.IndexOf ("dtest-app.exe") != -1);
1504 Guid guid = m.ModuleVersionId;
1505 Assert.AreEqual (frame.Method.DeclaringType.Assembly, m.Assembly);
1506 Assert.AreEqual (frame.Method.DeclaringType.Assembly.ManifestModule, m);
1508 // This is no longer true on 4.0
1509 //Assert.AreEqual ("Assembly", frame.Method.DeclaringType.Assembly.GetAssemblyObject ().Type.Name);
1511 TypeMirror t = vm.RootDomain.Corlib.GetType ("System.Diagnostics.DebuggerDisplayAttribute");
1512 Assert.AreEqual ("DebuggerDisplayAttribute", t.Name);
1516 public void LocalsInfo () {
1517 Event e = run_until ("locals2");
1519 StackFrame frame = e.Thread.GetFrames () [0];
1521 var locals = frame.Method.GetLocals ();
1522 Assert.AreEqual (9, locals.Length);
1523 for (int i = 0; i < 9; ++i) {
1524 if (locals [i].Name == "args") {
1525 Assert.IsTrue (locals [i].IsArg);
1526 Assert.AreEqual ("String[]", locals [i].Type.Name);
1527 } else if (locals [i].Name == "arg") {
1528 Assert.IsTrue (locals [i].IsArg);
1529 Assert.AreEqual ("Int32", locals [i].Type.Name);
1530 } else if (locals [i].Name == "i") {
1531 Assert.IsFalse (locals [i].IsArg);
1532 Assert.AreEqual ("Int64", locals [i].Type.Name);
1533 } else if (locals [i].Name == "j") {
1534 Assert.IsFalse (locals [i].IsArg);
1535 Assert.AreEqual ("Int32", locals [i].Type.Name);
1536 } else if (locals [i].Name == "s") {
1537 Assert.IsFalse (locals [i].IsArg);
1538 Assert.AreEqual ("String", locals [i].Type.Name);
1539 } else if (locals [i].Name == "t") {
1541 Assert.IsTrue (locals [i].IsArg);
1542 Assert.AreEqual ("String", locals [i].Type.Name);
1543 } else if (locals [i].Name == "rs") {
1544 Assert.IsTrue (locals [i].IsArg);
1545 Assert.AreEqual ("String", locals [i].Type.Name);
1546 } else if (locals [i].Name == "astruct") {
1547 } else if (locals [i].Name == "alist") {
1554 Event step_once () {
1556 var e = GetNextEvent ();
1557 Assert.IsTrue (e is StepEvent);
1561 Event step_into () {
1562 step_req.Disable ();
1563 step_req.Depth = StepDepth.Into;
1565 return step_once ();
1568 Event step_over () {
1569 step_req.Disable ();
1570 step_req.Depth = StepDepth.Over;
1572 return step_once ();
1576 step_req.Disable ();
1577 step_req.Depth = StepDepth.Out;
1579 return step_once ();
1583 public void Locals () {
1584 var be = run_until ("locals1");
1586 StackFrame frame = be.Thread.GetFrames () [0];
1587 MethodMirror m1 = frame.Method;
1589 // Compiler generated byref local
1590 foreach (var l in m1.GetLocals ()) {
1591 // The byval flag is hidden from the type
1592 if (l.Name != "ri" && l.Type.Name == "Double")
1593 AssertValue (null, frame.GetValue (l));
1596 be = run_until ("locals2");
1598 frame = be.Thread.GetFrames () [0];
1600 object val = frame.GetValue (frame.Method.GetLocal ("i"));
1601 AssertValue (0, val);
1603 var req = create_step (be);
1610 var e = step_once ();
1611 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1613 // Execute s = "AB";
1615 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1617 frame = e.Thread.GetFrames () [0];
1619 val = frame.GetValue (frame.Method.GetLocal ("i"));
1620 AssertValue (42, val);
1622 LocalVariable[] locals = frame.Method.GetLocals ();
1623 var vals = frame.GetValues (locals);
1624 Assert.AreEqual (locals.Length, vals.Length);
1625 for (int i = 0; i < locals.Length; ++i) {
1626 if (locals [i].Name == "i")
1627 AssertValue (42, vals [i]);
1628 if (locals [i].Name == "s")
1629 AssertValue ("AB", vals [i]);
1630 if (locals [i].Name == "t")
1631 AssertValue ("ABC", vals [i]);
1632 if (locals [i].Name == "alist") {
1636 // Argument checking
1639 AssertThrows<ArgumentNullException> (delegate () {
1640 frame.GetValue ((LocalVariable)null);
1642 // GetValue () local from another method
1643 AssertThrows<ArgumentException> (delegate () {
1644 frame.GetValue (m1.GetLocal ("foo"));
1648 AssertThrows<ArgumentNullException> (delegate () {
1649 frame.GetValue ((ParameterInfoMirror)null);
1651 // GetValue () local from another method
1652 AssertThrows<ArgumentException> (delegate () {
1653 frame.GetValue (m1.GetParameters ()[0]);
1656 // GetValues () null
1657 AssertThrows<ArgumentNullException> (delegate () {
1658 frame.GetValues (null);
1660 // GetValues () embedded null
1661 AssertThrows<ArgumentNullException> (delegate () {
1662 frame.GetValues (new LocalVariable [] { null });
1664 // GetValues () local from another method
1665 AssertThrows<ArgumentException> (delegate () {
1666 frame.GetValues (new LocalVariable [] { m1.GetLocal ("foo") });
1669 AssertThrows<ArgumentException> (delegate () {
1670 val = frame.GetValue (frame.Method.ReturnParameter);
1673 // invalid stack frames
1675 e = GetNextEvent ();
1676 Assert.IsTrue (e is StepEvent);
1677 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1679 AssertThrows<InvalidStackFrameException> (delegate () {
1680 frame.GetValue (frame.Method.GetLocal ("i"));
1686 be = run_until ("locals7");
1688 req = create_step (be);
1694 // Test that locals are initialized
1695 frame = e.Thread.GetFrames () [0];
1696 val = frame.GetValue (frame.Method.GetLocal ("t"));
1697 AssertValue (0, val);
1701 Assert.AreEqual ("locals7", (e as StepEvent).Method.Name);
1705 Assert.AreEqual ("locals7", (e as StepEvent).Method.Name);
1707 frame = e.Thread.GetFrames () [0];
1708 val = frame.GetValue (frame.Method.GetParameters ()[0]);
1709 AssertValue (22, val);
1710 val = frame.GetValue (frame.Method.GetLocal ("t"));
1711 AssertValue (22, val);
1712 val = frame.GetValue (frame.Method.GetLocal ("t2"));
1713 AssertValue (22, val);
1717 public void GetVisibleVariables () {
1718 Event e = run_until ("locals4");
1721 var locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
1722 Assert.AreEqual (2, locals.Count);
1723 var loc = locals.First (l => l.Name == "i");
1724 Assert.AreEqual ("Int64", loc.Type.Name);
1725 loc = locals.First (l => l.Name == "s");
1726 Assert.AreEqual ("String", loc.Type.Name);
1728 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
1729 Assert.AreEqual ("i", loc.Name);
1730 Assert.AreEqual ("Int64", loc.Type.Name);
1732 e = run_until ("locals5");
1735 locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
1736 Assert.AreEqual (2, locals.Count);
1737 loc = locals.First (l => l.Name == "i");
1738 Assert.AreEqual ("String", loc.Type.Name);
1739 loc = locals.First (l => l.Name == "s");
1740 Assert.AreEqual ("String", loc.Type.Name);
1742 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
1743 Assert.AreEqual ("i", loc.Name);
1744 Assert.AreEqual ("String", loc.Type.Name);
1746 // Variable in another scope
1747 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("j");
1748 Assert.IsNull (loc);
1752 public void Exit () {
1757 var e = GetNextEvent ();
1758 Assert.IsInstanceOfType (typeof (VMDeathEvent), e);
1760 Assert.AreEqual (5, (e as VMDeathEvent).ExitCode);
1763 /* Could be a remote vm with no process */
1766 Assert.AreEqual (5, p.ExitCode);
1769 AssertThrows<VMDisconnectedException> (delegate () {
1778 public void Dispose () {
1783 var e = GetNextEvent ();
1784 Assert.IsInstanceOfType (typeof (VMDisconnectEvent), e);
1787 /* Could be a remote vm with no process */
1790 Assert.AreEqual (3, p.ExitCode);
1793 AssertThrows<VMDisconnectedException> (delegate () {
1802 public void ColumnNumbers () {
1803 Event e = run_until ("line_numbers");
1805 // FIXME: Merge this with LineNumbers () when its fixed
1807 step_req = create_step (e);
1808 step_req.Depth = StepDepth.Into;
1816 e = GetNextEvent ();
1817 Assert.IsTrue (e is StepEvent);
1818 if (e.Thread.GetFrames ()[0].Method.Name == "ln1")
1822 // Do an additional step over so we are not on the beginning line of the method
1823 step_req.Disable ();
1824 step_req.Depth = StepDepth.Over;
1827 e = GetNextEvent ();
1828 Assert.IsTrue (e is StepEvent);
1830 l = e.Thread.GetFrames ()[0].Location;
1832 Assert.AreEqual (3, l.ColumnNumber);
1834 step_req.Disable ();
1838 // Broken by mcs+runtime changes (#5438)
1839 [Category("NotWorking")]
1840 public void LineNumbers () {
1841 Event e = run_until ("line_numbers");
1843 step_req = create_step (e);
1844 step_req.Depth = StepDepth.Into;
1851 e = GetNextEvent ();
1852 Assert.IsTrue (e is StepEvent);
1854 l = e.Thread.GetFrames ()[0].Location;
1856 Assert.IsTrue (l.SourceFile.IndexOf ("dtest-app.cs") != -1);
1857 Assert.AreEqual ("ln1", l.Method.Name);
1860 using (FileStream fs = new FileStream (l.SourceFile, FileMode.Open, FileAccess.Read)) {
1861 MD5 md5 = MD5.Create ();
1862 var hash = md5.ComputeHash (fs);
1864 for (int i = 0; i < 16; ++i)
1865 Assert.AreEqual (hash [i], l.SourceFileHash [i]);
1868 int line_base = l.LineNumber;
1871 e = GetNextEvent ();
1872 Assert.IsTrue (e is StepEvent);
1873 l = e.Thread.GetFrames ()[0].Location;
1874 Assert.AreEqual ("ln2", l.Method.Name);
1875 Assert.AreEqual (line_base + 6, l.LineNumber);
1878 e = GetNextEvent ();
1879 Assert.IsTrue (e is StepEvent);
1880 l = e.Thread.GetFrames ()[0].Location;
1881 Assert.AreEqual ("ln1", l.Method.Name);
1882 Assert.AreEqual (line_base + 1, l.LineNumber);
1885 e = GetNextEvent ();
1886 Assert.IsTrue (e is StepEvent);
1887 l = e.Thread.GetFrames ()[0].Location;
1888 Assert.AreEqual ("ln3", l.Method.Name);
1889 Assert.AreEqual (line_base + 11, l.LineNumber);
1892 e = GetNextEvent ();
1893 Assert.IsTrue (e is StepEvent);
1894 l = e.Thread.GetFrames ()[0].Location;
1895 Assert.AreEqual ("ln3", l.Method.Name);
1896 Assert.IsTrue (l.SourceFile.EndsWith ("FOO"));
1897 Assert.AreEqual (55, l.LineNumber);
1900 e = GetNextEvent ();
1901 Assert.IsTrue (e is StepEvent);
1902 l = e.Thread.GetFrames ()[0].Location;
1903 Assert.AreEqual ("ln1", l.Method.Name);
1904 Assert.AreEqual (line_base + 2, l.LineNumber);
1906 // GetSourceFiles ()
1907 string[] sources = l.Method.DeclaringType.GetSourceFiles ();
1908 Assert.AreEqual (2, sources.Length);
1909 Assert.AreEqual ("dtest-app.cs", sources [0]);
1910 Assert.AreEqual ("FOO", sources [1]);
1912 sources = l.Method.DeclaringType.GetSourceFiles (true);
1913 Assert.AreEqual (2, sources.Length);
1914 Assert.IsTrue (sources [0].EndsWith ("dtest-app.cs"));
1915 Assert.IsTrue (sources [1].EndsWith ("FOO"));
1919 public void Suspend () {
1922 Start (new string [] { "dtest-app.exe", "suspend-test" });
1924 Event e = run_until ("suspend");
1926 ThreadMirror main = e.Thread;
1934 // The debuggee should be suspended while it is running the infinite loop
1936 StackFrame frame = main.GetFrames ()[0];
1937 Assert.AreEqual ("suspend", frame.Method.Name);
1941 // resuming when not suspended
1942 AssertThrows<InvalidOperationException> (delegate () {
1952 public void AssemblyLoad () {
1953 Event e = run_until ("assembly_load");
1955 var load_req = vm.CreateAssemblyLoadRequest ();
1960 e = GetNextEvent ();
1961 Assert.IsInstanceOfType (typeof (AssemblyLoadEvent), e);
1962 Assert.IsTrue ((e as AssemblyLoadEvent).Assembly.Location.EndsWith ("System.dll"));
1964 var frames = e.Thread.GetFrames ();
1965 Assert.IsTrue (frames.Length > 0);
1966 Assert.AreEqual ("assembly_load", frames [0].Method.Name);
1970 public void CreateValue () {
1973 v = vm.CreateValue (1);
1974 Assert.AreEqual (vm, v.VirtualMachine);
1975 Assert.AreEqual (1, v.Value);
1977 v = vm.CreateValue (null);
1978 Assert.AreEqual (vm, v.VirtualMachine);
1979 Assert.AreEqual (null, v.Value);
1981 // Argument checking
1982 AssertThrows <ArgumentException> (delegate () {
1983 v = vm.CreateValue ("FOO");
1988 public void CreateString () {
1989 StringMirror s = vm.RootDomain.CreateString ("ABC");
1991 Assert.AreEqual (vm, s.VirtualMachine);
1992 Assert.AreEqual ("ABC", s.Value);
1993 Assert.AreEqual (vm.RootDomain, s.Domain);
1996 StringBuilder sb = new StringBuilder ();
1997 for (int i = 0; i < 1024; ++i)
1999 s = vm.RootDomain.CreateString (sb.ToString ());
2001 // Argument checking
2002 AssertThrows <ArgumentNullException> (delegate () {
2003 s = vm.RootDomain.CreateString (null);
2008 public void CreateBoxedValue () {
2009 ObjectMirror o = vm.RootDomain.CreateBoxedValue (new PrimitiveValue (vm, 42));
2011 Assert.AreEqual ("Int32", o.Type.Name);
2012 //AssertValue (42, m.GetValue (o.Type.GetField ("m_value")));
2014 // Argument checking
2015 AssertThrows <ArgumentNullException> (delegate () {
2016 vm.RootDomain.CreateBoxedValue (null);
2019 AssertThrows <ArgumentException> (delegate () {
2020 vm.RootDomain.CreateBoxedValue (o);
2025 public void Invoke () {
2026 Event e = run_until ("invoke1");
2028 StackFrame frame = e.Thread.GetFrames () [0];
2030 TypeMirror t = frame.Method.DeclaringType;
2031 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2033 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2039 m = t.GetMethod ("invoke_return_void");
2040 v = this_obj.InvokeMethod (e.Thread, m, null);
2044 m = t.GetMethod ("invoke_return_ref");
2045 v = this_obj.InvokeMethod (e.Thread, m, null);
2046 AssertValue ("ABC", v);
2049 m = t.GetMethod ("invoke_return_null");
2050 v = this_obj.InvokeMethod (e.Thread, m, null);
2051 AssertValue (null, v);
2054 m = t.GetMethod ("invoke_return_primitive");
2055 v = this_obj.InvokeMethod (e.Thread, m, null);
2056 AssertValue (42, v);
2059 m = t.GetMethod ("invoke_return_nullable");
2060 v = this_obj.InvokeMethod (e.Thread, m, null);
2061 Assert.IsInstanceOfType (typeof (StructMirror), v);
2062 var s = v as StructMirror;
2063 AssertValue (42, s.Fields [0]);
2064 AssertValue (true, s.Fields [1]);
2066 // pass nullable as this
2067 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2068 m = s.Type.GetMethod ("ToString");
2069 v = s.InvokeMethod (e.Thread, m, null);
2071 // return nullable null
2072 m = t.GetMethod ("invoke_return_nullable_null");
2073 v = this_obj.InvokeMethod (e.Thread, m, null);
2074 Assert.IsInstanceOfType (typeof (StructMirror), v);
2075 s = v as StructMirror;
2076 AssertValue (0, s.Fields [0]);
2077 AssertValue (false, s.Fields [1]);
2079 // pass nullable as this
2080 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2081 m = s.Type.GetMethod ("ToString");
2082 v = s.InvokeMethod (e.Thread, m, null);
2085 m = t.GetMethod ("invoke_pass_primitive");
2086 Value[] args = new Value [] {
2087 vm.CreateValue ((byte)Byte.MaxValue),
2088 vm.CreateValue ((sbyte)SByte.MaxValue),
2089 vm.CreateValue ((short)1),
2090 vm.CreateValue ((ushort)1),
2091 vm.CreateValue ((int)1),
2092 vm.CreateValue ((uint)1),
2093 vm.CreateValue ((long)1),
2094 vm.CreateValue ((ulong)1),
2095 vm.CreateValue ('A'),
2096 vm.CreateValue (true),
2097 vm.CreateValue (3.14f),
2098 vm.CreateValue (3.14) };
2100 v = this_obj.InvokeMethod (e.Thread, m, args);
2101 AssertValue ((int)Byte.MaxValue + (int)SByte.MaxValue + 1 + 1 + 1 + 1 + 1 + 1 + 'A' + 1 + 3 + 3, v);
2104 m = t.GetMethod ("invoke_pass_ref");
2105 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2106 AssertValue ("ABC", v);
2109 m = t.GetMethod ("invoke_pass_ref");
2110 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (null) });
2111 AssertValue (null, v);
2114 m = t.GetMethod ("invoke_static_pass_ref");
2115 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2116 AssertValue ("ABC", v);
2118 // static invoked using ObjectMirror.InvokeMethod
2119 m = t.GetMethod ("invoke_static_pass_ref");
2120 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2121 AssertValue ("ABC", v);
2123 // method which throws an exception
2125 m = t.GetMethod ("invoke_throws");
2126 v = this_obj.InvokeMethod (e.Thread, m, null);
2128 } catch (InvocationException ex) {
2129 Assert.AreEqual ("Exception", ex.Exception.Type.Name);
2134 m = t.GetMethod ("invoke_out");
2135 var out_task = this_obj.InvokeMethodAsyncWithResult (e.Thread, m, new Value [] { vm.CreateValue (1), vm.CreateValue (null) }, InvokeOptions.ReturnOutArgs);
2136 var out_args = out_task.Result.OutArgs;
2137 AssertValue (5, out_args [0]);
2138 Assert.IsTrue (out_args [1] is ArrayMirror);
2139 Assert.AreEqual (10, (out_args [1] as ArrayMirror).Length);
2141 // without ReturnOutArgs flag
2142 out_task = this_obj.InvokeMethodAsyncWithResult (e.Thread, m, new Value [] { vm.CreateValue (1), vm.CreateValue (null) });
2143 out_args = out_task.Result.OutArgs;
2144 Assert.IsNull (out_args);
2148 m = t.GetMethod (".ctor");
2149 v = t.InvokeMethod (e.Thread, m, null);
2150 Assert.IsInstanceOfType (typeof (ObjectMirror), v);
2151 Assert.AreEqual ("Tests", (v as ObjectMirror).Type.Name);
2154 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("ITest2");
2155 m = cl1.GetMethod ("invoke_iface");
2156 v = this_obj.InvokeMethod (e.Thread, m, null);
2157 AssertValue (42, v);
2160 m = t.BaseType.GetMethod ("virtual_method");
2161 v = this_obj.InvokeMethod (e.Thread, m, null, InvokeOptions.Virtual);
2162 AssertValue ("V2", v);
2164 // virtual call on static method
2165 m = t.GetMethod ("invoke_static_pass_ref");
2166 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") }, InvokeOptions.Virtual);
2167 AssertValue ("ABC", v);
2171 m = t.GetMethod ("invoke_pass_ref");
2172 var task = this_obj.InvokeMethodAsync (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2173 AssertValue ("ABC", task.Result);
2176 m = t.GetMethod ("invoke_static_pass_ref");
2177 task = t.InvokeMethodAsync (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2178 AssertValue ("ABC", task.Result);
2181 // Argument checking
2184 AssertThrows<ArgumentNullException> (delegate {
2185 m = t.GetMethod ("invoke_pass_ref");
2186 v = this_obj.InvokeMethod (null, m, new Value [] { vm.CreateValue (null) });
2190 AssertThrows<ArgumentNullException> (delegate {
2191 v = this_obj.InvokeMethod (e.Thread, null, new Value [] { vm.CreateValue (null) });
2194 // invalid number of arguments
2195 m = t.GetMethod ("invoke_pass_ref");
2196 AssertThrows<ArgumentException> (delegate {
2197 v = this_obj.InvokeMethod (e.Thread, m, null);
2200 // invalid type of argument (ref != primitive)
2201 m = t.GetMethod ("invoke_pass_ref");
2202 AssertThrows<ArgumentException> (delegate {
2203 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2206 // invalid type of argument (primitive != primitive)
2207 m = t.GetMethod ("invoke_pass_primitive_2");
2208 AssertThrows<ArgumentException> (delegate {
2209 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2212 // invoking a non-static method as static
2213 m = t.GetMethod ("invoke_pass_ref");
2214 AssertThrows<ArgumentException> (delegate {
2215 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2218 // invoking a method defined in another class
2219 m = t2.GetMethod ("invoke");
2220 AssertThrows<ArgumentException> (delegate {
2221 v = this_obj.InvokeMethod (e.Thread, m, null);
2226 public void InvokeVType () {
2227 Event e = run_until ("invoke1");
2229 StackFrame frame = e.Thread.GetFrames () [0];
2231 var s = frame.GetArgument (1) as StructMirror;
2233 TypeMirror t = s.Type;
2238 // Pass struct as this, receive int
2239 m = t.GetMethod ("invoke_return_int");
2240 v = s.InvokeMethod (e.Thread, m, null);
2241 AssertValue (42, v);
2243 // Pass struct as this, receive intptr
2244 m = t.GetMethod ("invoke_return_intptr");
2245 v = s.InvokeMethod (e.Thread, m, null);
2246 AssertValue (43, v);
2249 m = t.GetMethod ("invoke_static");
2250 v = t.InvokeMethod (e.Thread, m, null);
2253 // Pass generic struct as this
2254 s = frame.GetArgument (2) as StructMirror;
2256 m = t.GetMethod ("invoke_return_int");
2257 v = s.InvokeMethod (e.Thread, m, null);
2258 AssertValue (42, v);
2261 // Invoke a method which changes state
2262 s = frame.GetArgument (1) as StructMirror;
2264 m = t.GetMethod ("invoke_mutate");
2265 var task = s.InvokeMethodAsyncWithResult (e.Thread, m, null, InvokeOptions.ReturnOutThis);
2266 var out_this = task.Result.OutThis as StructMirror;
2267 AssertValue (5, out_this ["l"]);
2269 // Without the ReturnOutThis flag
2270 s = frame.GetArgument (1) as StructMirror;
2272 m = t.GetMethod ("invoke_mutate");
2273 task = s.InvokeMethodAsyncWithResult (e.Thread, m, null);
2274 out_this = task.Result.OutThis as StructMirror;
2275 Assert.AreEqual (null, out_this);
2278 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("ITest2");
2279 m = cl1.GetMethod ("invoke_iface");
2280 v = s.InvokeMethod (e.Thread, m, null);
2281 AssertValue (42, v);
2284 m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2285 v = s.InvokeMethod (e.Thread, m, null, InvokeOptions.Virtual);
2286 AssertValue ("42", v);
2291 public void BreakpointDuringInvoke () {
2292 Event e = run_until ("invoke1");
2294 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke2");
2295 Assert.IsNotNull (m);
2296 vm.SetBreakpoint (m, 0);
2298 StackFrame frame = e.Thread.GetFrames () [0];
2299 var o = frame.GetThis () as ObjectMirror;
2301 bool failed = false;
2303 bool finished = false;
2304 object wait = new object ();
2306 // Have to invoke in a separate thread as the invoke is suspended until we
2307 // resume after the breakpoint
2308 Thread t = new Thread (delegate () {
2310 o.InvokeMethod (e.Thread, m, null);
2316 Monitor.Pulse (wait);
2322 StackFrame invoke_frame = null;
2325 e = GetNextEvent ();
2326 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
2327 // Check stack trace support and invokes
2328 var frames = e.Thread.GetFrames ();
2329 invoke_frame = frames [0];
2330 Assert.AreEqual ("invoke2", frames [0].Method.Name);
2331 Assert.IsTrue (frames [0].IsDebuggerInvoke);
2332 Assert.AreEqual ("invoke1", frames [1].Method.Name);
2339 Monitor.Wait (wait);
2342 // Check that the invoke frames are no longer valid
2343 AssertThrows<InvalidStackFrameException> (delegate {
2344 invoke_frame.GetThis ();
2347 // Check InvokeOptions.DisableBreakpoints flag
2348 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2352 public void DisabledExceptionDuringInvoke () {
2353 Event e = run_until ("invoke_ex");
2355 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_ex_inner");
2357 StackFrame frame = e.Thread.GetFrames () [0];
2358 var o = frame.GetThis () as ObjectMirror;
2360 var req = vm.CreateExceptionRequest (null);
2363 // Check InvokeOptions.DisableBreakpoints flag
2364 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2370 public void InvokeSingleThreaded () {
2373 Start (new string [] { "dtest-app.exe", "invoke-single-threaded" });
2375 Event e = run_until ("invoke_single_threaded_2");
2377 StackFrame f = e.Thread.GetFrames ()[0];
2379 var obj = f.GetThis () as ObjectMirror;
2381 // Check that the counter value incremented by the other thread does not increase
2382 // during the invoke.
2383 object counter1 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2385 var m = obj.Type.GetMethod ("invoke_return_void");
2386 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2388 object counter2 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2390 Assert.AreEqual ((int)counter1, (int)counter2);
2392 // Test multiple invokes done in succession
2393 m = obj.Type.GetMethod ("invoke_return_void");
2394 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2396 // Test events during single-threaded invokes
2397 vm.EnableEvents (EventType.TypeLoad);
2398 m = obj.Type.GetMethod ("invoke_type_load");
2399 obj.BeginInvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded, delegate {
2403 e = GetNextEvent ();
2404 Assert.AreEqual (EventType.TypeLoad, e.EventType);
2407 List<Value> invoke_results;
2410 public void InvokeMultiple () {
2411 Event e = run_until ("invoke1");
2413 StackFrame frame = e.Thread.GetFrames () [0];
2415 TypeMirror t = frame.Method.DeclaringType;
2416 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2418 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2420 var methods = new MethodMirror [2];
2421 methods [0] = t.GetMethod ("invoke_return_ref");
2422 methods [1] = t.GetMethod ("invoke_return_primitive");
2424 invoke_results = new List<Value> ();
2426 var r = this_obj.BeginInvokeMultiple (e.Thread, methods, null, InvokeOptions.SingleThreaded, invoke_multiple_cb, this_obj);
2427 WaitHandle.WaitAll (new WaitHandle[] { r.AsyncWaitHandle });
2428 this_obj.EndInvokeMultiple (r);
2429 // The callback might still be running
2430 while (invoke_results.Count < 2) {
2433 if (invoke_results [0] is PrimitiveValue) {
2434 AssertValue ("ABC", invoke_results [1]);
2435 AssertValue (42, invoke_results [0]);
2437 AssertValue ("ABC", invoke_results [0]);
2438 AssertValue (42, invoke_results [1]);
2442 void invoke_multiple_cb (IAsyncResult ar) {
2443 ObjectMirror this_obj = (ObjectMirror)ar.AsyncState;
2445 var res = this_obj.EndInvokeMethod (ar);
2446 lock (invoke_results)
2447 invoke_results.Add (res);
2451 public void GetThreads () {
2456 public void Threads () {
2457 Event e = run_until ("threads");
2459 Assert.AreEqual (ThreadState.Running, e.Thread.ThreadState);
2461 Assert.IsTrue (e.Thread.ThreadId > 0);
2463 Assert.AreEqual (e.Thread.TID, e.Thread.TID);
2465 vm.EnableEvents (EventType.ThreadStart, EventType.ThreadDeath);
2469 e = GetNextEvent ();
2470 Assert.IsInstanceOfType (typeof (ThreadStartEvent), e);
2471 var state = e.Thread.ThreadState;
2472 Assert.IsTrue (state == ThreadState.Running || state == ThreadState.Unstarted);
2476 e = GetNextEvent ();
2477 Assert.IsInstanceOfType (typeof (ThreadDeathEvent), e);
2478 Assert.AreEqual (ThreadState.Stopped, e.Thread.ThreadState);
2482 public void Frame_SetValue () {
2483 Event e = run_until ("locals2");
2485 StackFrame frame = e.Thread.GetFrames () [0];
2488 var l = frame.Method.GetLocal ("i");
2489 frame.SetValue (l, vm.CreateValue ((long)55));
2490 AssertValue (55, frame.GetValue (l));
2493 l = frame.Method.GetLocal ("s");
2494 frame.SetValue (l, vm.RootDomain.CreateString ("DEF"));
2495 AssertValue ("DEF", frame.GetValue (l));
2497 // argument as local
2498 l = frame.Method.GetLocal ("arg");
2499 frame.SetValue (l, vm.CreateValue (6));
2500 AssertValue (6, frame.GetValue (l));
2503 var p = frame.Method.GetParameters ()[1];
2504 frame.SetValue (p, vm.CreateValue (7));
2505 AssertValue (7, frame.GetValue (p));
2508 p = frame.Method.GetParameters ()[2];
2509 frame.SetValue (p, vm.RootDomain.CreateString ("DEF"));
2510 AssertValue ("DEF", frame.GetValue (p));
2513 p = frame.Method.GetParameters ()[3];
2514 frame.SetValue (p, vm.RootDomain.CreateString ("DEF2"));
2515 AssertValue ("DEF2", frame.GetValue (p));
2518 p = frame.Method.GetParameters ()[4];
2519 var v = frame.GetValue (p) as StructMirror;
2520 v ["i"] = vm.CreateValue (43);
2521 frame.SetValue (p, v);
2522 v = frame.GetValue (p) as StructMirror;
2523 AssertValue (43, v ["i"]);
2525 // argument checking
2528 AssertThrows<ArgumentNullException> (delegate () {
2529 frame.SetValue ((LocalVariable)null, vm.CreateValue (55));
2533 AssertThrows<ArgumentNullException> (delegate () {
2534 l = frame.Method.GetLocal ("i");
2535 frame.SetValue (l, null);
2538 // value of invalid type
2539 AssertThrows<ArgumentException> (delegate () {
2540 l = frame.Method.GetLocal ("i");
2541 frame.SetValue (l, vm.CreateValue (55));
2547 public void Frame_SetValue_Registers () {
2548 Event e = run_until ("locals6_1");
2550 StackFrame frame = e.Thread.GetFrames () [1];
2553 var l = frame.Method.GetLocal ("j");
2554 frame.SetValue (l, vm.CreateValue (99));
2555 AssertValue (99, frame.GetValue (l));
2557 // Check it during execution
2558 e = run_until ("locals6_2");
2559 frame = e.Thread.GetFrames () [0];
2560 AssertValue (99, frame.GetValue (frame.Method.GetParameters ()[0]));
2562 // Set it while in a frame which clobbers its register
2563 e = run_until ("locals6_3");
2564 frame = e.Thread.GetFrames () [1];
2565 frame.SetValue (l, vm.CreateValue (100));
2566 AssertValue (100, frame.GetValue (l));
2568 // Check it during execution
2569 e = run_until ("locals6_4");
2570 frame = e.Thread.GetFrames () [0];
2571 AssertValue (100, frame.GetValue (frame.Method.GetParameters ()[0]));
2573 // Signed byte value
2574 e = run_until ("locals6_5");
2575 frame = e.Thread.GetFrames () [1];
2576 var l2 = frame.Method.GetLocal ("sb");
2577 frame.SetValue (l2, vm.CreateValue ((sbyte)-99));
2578 AssertValue (-99, frame.GetValue (l2));
2580 // Check it during execution
2581 e = run_until ("locals6_6");
2582 frame = e.Thread.GetFrames () [0];
2583 AssertValue (-99, frame.GetValue (frame.Method.GetParameters ()[0]));
2587 public void InvokeRegress () {
2588 Event e = run_until ("invoke1");
2590 StackFrame frame = e.Thread.GetFrames () [0];
2592 TypeMirror t = frame.Method.DeclaringType;
2593 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2595 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2601 m = t.GetMethod ("invoke_return_void");
2602 v = this_obj.InvokeMethod (e.Thread, m, null);
2605 // Check that the stack frames remain valid during the invoke
2606 Assert.AreEqual ("Tests", (frame.GetThis () as ObjectMirror).Type.Name);
2608 // do another invoke
2609 m = t.GetMethod ("invoke_return_void");
2610 v = this_obj.InvokeMethod (e.Thread, m, null);
2613 // Try a single step after the invoke
2614 var req = create_step (e);
2615 req.Depth = StepDepth.Into;
2616 req.Size = StepSize.Line;
2622 // Step into invoke2
2624 e = GetNextEvent ();
2625 Assert.IsTrue (e is StepEvent);
2626 Assert.AreEqual ("invoke2", (e as StepEvent).Method.Name);
2630 frame = e.Thread.GetFrames () [0];
2634 public void Exceptions () {
2635 Event e = run_until ("exceptions");
2636 var req = vm.CreateExceptionRequest (null);
2641 e = GetNextEvent ();
2642 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2643 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2645 var frames = e.Thread.GetFrames ();
2646 Assert.AreEqual ("exceptions", frames [0].Method.Name);
2649 // exception type filter
2651 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.ArgumentException"));
2654 // Skip the throwing of the second OverflowException
2657 e = GetNextEvent ();
2658 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2659 Assert.AreEqual ("ArgumentException", (e as ExceptionEvent).Exception.Type.Name);
2662 // exception type filter for subclasses
2663 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.Exception"));
2668 e = GetNextEvent ();
2669 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2670 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2674 req.IncludeSubclasses = false;
2679 e = GetNextEvent ();
2680 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2681 Assert.AreEqual ("Exception", (e as ExceptionEvent).Exception.Type.Name);
2684 // Implicit exceptions
2685 req = vm.CreateExceptionRequest (null);
2690 e = GetNextEvent ();
2691 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2692 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
2695 // Single stepping after an exception
2696 req = vm.CreateExceptionRequest (null);
2701 e = GetNextEvent ();
2702 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2703 Assert.AreEqual ("Exception", (e as ExceptionEvent).Exception.Type.Name);
2704 frames = e.Thread.GetFrames ();
2705 Assert.AreEqual ("exceptions2", frames [0].Method.Name);
2708 var sreq = create_step (e);
2709 sreq.Depth = StepDepth.Over;
2710 sreq.Size = StepSize.Line;
2714 e = GetNextEvent ();
2715 Assert.IsInstanceOfType (typeof (StepEvent), e);
2716 frames = e.Thread.GetFrames ();
2717 Assert.AreEqual ("exceptions", frames [0].Method.Name);
2720 // Argument checking
2721 AssertThrows<ArgumentException> (delegate {
2722 vm.CreateExceptionRequest (e.Thread.Type);
2727 public void ExceptionFilter () {
2728 Event e = run_until ("exception_filter");
2730 MethodMirror m = entry_point.DeclaringType.GetMethod ("exception_filter_filter");
2731 Assert.IsNotNull (m);
2733 vm.SetBreakpoint (m, 0);
2737 e = GetNextEvent ();
2738 Assert.AreEqual (EventType.Breakpoint, e.EventType);
2739 Assert.IsTrue (e is BreakpointEvent);
2740 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
2742 var frames = e.Thread.GetFrames ();
2744 Assert.IsTrue (frames [0].Location.SourceFile.IndexOf ("dtest-app.cs") != -1);
2745 Assert.AreEqual ("exception_filter_filter", frames [0].Location.Method.Name);
2747 Assert.AreEqual (0, frames [1].Location.Method.MetadataToken);
2748 Assert.AreEqual (0x0f, frames [1].Location.ILOffset);
2750 Assert.AreEqual ("exception_filter_method", frames [2].Location.Method.Name);
2751 Assert.AreEqual (0x06, frames [2].Location.ILOffset);
2753 Assert.AreEqual (0, frames [3].Location.Method.MetadataToken, 0);
2754 Assert.AreEqual (0, frames [3].Location.ILOffset);
2756 Assert.AreEqual ("exception_filter", frames [4].Location.Method.Name);
2760 public void ExceptionFilter2 () {
2763 Start (new string [] { "dtest-excfilter.exe" });
2765 MethodMirror filter_method = entry_point.DeclaringType.GetMethod ("Filter");
2766 Assert.IsNotNull (filter_method);
2768 MethodMirror test_method = entry_point.DeclaringType.GetMethod ("Test");
2769 Assert.IsNotNull (test_method);
2771 vm.SetBreakpoint (filter_method, 0);
2775 var e = GetNextEvent ();
2776 Assert.AreEqual (EventType.Breakpoint, e.EventType);
2777 Assert.IsTrue (e is BreakpointEvent);
2778 Assert.AreEqual (filter_method.Name, (e as BreakpointEvent).Method.Name);
2780 var frames = e.Thread.GetFrames ();
2782 Assert.AreEqual (4, frames.Count ());
2784 Assert.AreEqual (filter_method.Name, frames [0].Location.Method.Name);
2785 Assert.AreEqual (20, frames [0].Location.LineNumber);
2786 Assert.AreEqual (0, frames [0].Location.ILOffset);
2788 Assert.AreEqual (test_method.Name, frames [1].Location.Method.Name);
2789 Assert.AreEqual (37, frames [1].Location.LineNumber);
2790 Assert.AreEqual (0x0b, frames [1].Location.ILOffset);
2792 Assert.AreEqual (test_method.Name, frames [2].Location.Method.Name);
2793 Assert.AreEqual (33, frames [2].Location.LineNumber);
2794 Assert.AreEqual (0x05, frames [2].Location.ILOffset);
2796 Assert.AreEqual (entry_point.Name, frames [3].Location.Method.Name);
2797 Assert.AreEqual (14, frames [3].Location.LineNumber);
2798 Assert.AreEqual (0x00, frames [3].Location.ILOffset);
2806 public void EventSets () {
2808 // Create two filter which both match the same exception
2810 Event e = run_until ("exceptions");
2812 var req = vm.CreateExceptionRequest (null);
2815 var req2 = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.OverflowException"));
2820 var es = vm.GetNextEventSet ();
2821 Assert.AreEqual (2, es.Events.Length);
2824 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2825 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2828 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2829 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2836 // Test single threaded invokes during processing of nullref exceptions.
2837 // These won't work if the exception handling is done from the sigsegv signal
2838 // handler, since the sigsegv signal is disabled until control returns from the
2842 [Category ("only3")]
2843 public void NullRefExceptionAndSingleThreadedInvoke () {
2844 Event e = run_until ("exceptions");
2845 var req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.NullReferenceException"));
2850 e = GetNextEvent ();
2851 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2852 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
2854 var ex = (e as ExceptionEvent).Exception;
2855 var tostring_method = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2856 ex.InvokeMethod (e.Thread, tostring_method, null, InvokeOptions.SingleThreaded);
2860 public void Domains () {
2863 Start (new string [] { "dtest-app.exe", "domain-test" });
2865 vm.EnableEvents (EventType.AppDomainCreate, EventType.AppDomainUnload, EventType.AssemblyUnload);
2867 Event e = run_until ("domains");
2871 e = GetNextEvent ();
2872 Assert.IsInstanceOfType (typeof (AppDomainCreateEvent), e);
2874 var domain = (e as AppDomainCreateEvent).Domain;
2876 // Check the object type
2877 e = run_until ("domains_2");
2878 var frame = e.Thread.GetFrames ()[0];
2879 var o = frame.GetArgument (0) as ObjectMirror;
2880 Assert.AreEqual ("CrossDomain", o.Type.Name);
2882 // Do a remoting invoke
2883 var cross_domain_type = o.Type;
2884 var v = o.InvokeMethod (e.Thread, cross_domain_type.GetMethod ("invoke_3"), null);
2885 AssertValue (42, v);
2887 // Run until the callback in the domain
2888 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_in_domain");
2889 Assert.IsNotNull (m);
2890 vm.SetBreakpoint (m, 0);
2894 e = GetNextEvent ();
2895 if (e is BreakpointEvent)
2899 Assert.AreEqual ("invoke_in_domain", (e as BreakpointEvent).Method.Name);
2901 // d_method is from another domain
2902 MethodMirror d_method = (e as BreakpointEvent).Method;
2903 Assert.IsTrue (m != d_method);
2905 var frames = e.Thread.GetFrames ();
2906 Assert.AreEqual ("invoke_in_domain", frames [0].Method.Name);
2907 Assert.AreEqual (domain, frames [0].Domain);
2908 Assert.AreEqual ("invoke", frames [1].Method.Name);
2909 Assert.AreEqual ("domains", frames [2].Method.Name);
2910 Assert.AreEqual (vm.RootDomain, frames [2].Domain);
2912 // Test breakpoints on already JITted methods in other domains
2913 m = entry_point.DeclaringType.GetMethod ("invoke_in_domain_2");
2914 Assert.IsNotNull (m);
2915 vm.SetBreakpoint (m, 0);
2919 e = GetNextEvent ();
2920 if (e is BreakpointEvent)
2924 Assert.AreEqual ("invoke_in_domain_2", (e as BreakpointEvent).Method.Name);
2926 // This is empty when receiving the AppDomainCreateEvent
2927 Assert.AreEqual ("domain", domain.FriendlyName);
2929 // Run until the unload
2932 e = GetNextEvent ();
2933 if (e is AssemblyUnloadEvent) {
2939 Assert.IsInstanceOfType (typeof (AppDomainUnloadEvent), e);
2940 Assert.AreEqual (domain, (e as AppDomainUnloadEvent).Domain);
2942 // Run past the unload
2943 e = run_until ("domains_3");
2945 // Test access to unloaded types
2946 // FIXME: Add an exception type for this
2947 AssertThrows<Exception> (delegate {
2948 d_method.DeclaringType.GetValue (d_method.DeclaringType.GetField ("static_i"));
2951 // Check that .Domain is accessible for stack frames with native transitions
2952 e = run_until ("called_from_invoke");
2953 ThreadMirror.NativeTransitions = true;
2954 foreach (var f in e.Thread.GetFrames ()) {
2960 public void DynamicMethods () {
2961 Event e = run_until ("dyn_call");
2963 var m = e.Thread.GetFrames ()[1].Method;
2964 Assert.AreEqual ("dyn_method", m.Name);
2966 // Test access to IL
2967 var body = m.GetMethodBody ();
2969 ILInstruction ins = body.Instructions [0];
2970 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
2971 Assert.AreEqual ("FOO", ins.Operand);
2975 public void RefEmit () {
2978 Start (new string [] { "dtest-app.exe", "ref-emit-test" });
2980 Event e = run_until ("ref_emit_call");
2982 var m = e.Thread.GetFrames ()[1].Method;
2983 Assert.AreEqual ("ref_emit_method", m.Name);
2985 // Test access to IL
2986 var body = m.GetMethodBody ();
2990 ins = body.Instructions [0];
2991 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
2992 Assert.AreEqual ("FOO", ins.Operand);
2994 ins = body.Instructions [1];
2995 Assert.AreEqual (OpCodes.Call, ins.OpCode);
2996 Assert.IsInstanceOfType (typeof (MethodMirror), ins.Operand);
2997 Assert.AreEqual ("ref_emit_call", (ins.Operand as MethodMirror).Name);
3001 public void IsAttached () {
3002 var f = entry_point.DeclaringType.GetField ("is_attached");
3004 Event e = run_until ("Main");
3006 AssertValue (true, entry_point.DeclaringType.GetValue (f));
3010 public void StackTraceInNative () {
3011 // Check that stack traces can be produced for threads in native code
3014 Start (new string [] { "dtest-app.exe", "frames-in-native" });
3016 var e = run_until ("frames_in_native");
3018 // FIXME: This is racy
3025 StackFrame[] frames = e.Thread.GetFrames ();
3027 int frame_index = -1;
3028 for (int i = 0; i < frames.Length; ++i) {
3029 if (frames [i].Method.Name == "Sleep") {
3035 Assert.IsTrue (frame_index != -1);
3036 Assert.AreEqual ("Sleep", frames [frame_index].Method.Name);
3037 Assert.AreEqual ("frames_in_native", frames [frame_index + 1].Method.Name);
3038 Assert.AreEqual ("Main", frames [frame_index + 2].Method.Name);
3040 // Check that invokes are disabled for such threads
3041 TypeMirror t = frames [frame_index + 1].Method.DeclaringType;
3043 var m = t.GetMethod ("invoke_static_return_void");
3044 AssertThrows<InvalidOperationException> (delegate {
3045 t.InvokeMethod (e.Thread, m, null);
3048 // Check that the frame info is invalidated
3049 run_until ("frames_in_native_2");
3051 AssertThrows<InvalidStackFrameException> (delegate {
3052 Console.WriteLine (frames [frame_index].GetThis ());
3057 public void VirtualMachine_CreateEnumMirror () {
3058 var e = run_until ("o1");
3059 var frame = e.Thread.GetFrames () [0];
3061 object val = frame.GetThis ();
3062 Assert.IsTrue (val is ObjectMirror);
3063 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
3064 ObjectMirror o = (val as ObjectMirror);
3066 FieldInfoMirror field = o.Type.GetField ("field_enum");
3067 Value f = o.GetValue (field);
3068 TypeMirror enumType = (f as EnumMirror).Type;
3070 o.SetValue (field, vm.CreateEnumMirror (enumType, vm.CreateValue (1)));
3071 f = o.GetValue (field);
3072 Assert.AreEqual (1, (f as EnumMirror).Value);
3074 // Argument checking
3075 AssertThrows<ArgumentNullException> (delegate () {
3076 vm.CreateEnumMirror (enumType, null);
3079 AssertThrows<ArgumentNullException> (delegate () {
3080 vm.CreateEnumMirror (null, vm.CreateValue (1));
3084 AssertThrows<ArgumentException> (delegate () {
3085 vm.CreateEnumMirror (enumType, vm.CreateValue (null));
3088 // value of a wrong type
3089 AssertThrows<ArgumentException> (delegate () {
3090 vm.CreateEnumMirror (enumType, vm.CreateValue ((long)1));
3095 public void VirtualMachine_EnableEvents_Breakpoint () {
3096 AssertThrows<ArgumentException> (delegate () {
3097 vm.EnableEvents (EventType.Breakpoint);
3102 public void SingleStepRegress654694 () {
3105 MethodMirror m = entry_point.DeclaringType.GetMethod ("ss_regress_654694");
3106 foreach (Location l in m.Locations) {
3107 if (l.ILOffset > 0 && il_offset == -1)
3108 il_offset = l.ILOffset;
3111 Event e = run_until ("ss_regress_654694");
3113 Assert.IsNotNull (m);
3114 vm.SetBreakpoint (m, il_offset);
3118 e = GetNextEvent ();
3119 Assert.IsTrue (e is BreakpointEvent);
3121 var req = create_step (e);
3122 req.Depth = StepDepth.Over;
3123 req.Size = StepSize.Line;
3128 e = GetNextEvent ();
3129 Assert.IsTrue (e is StepEvent);
3135 public void DebugBreak () {
3136 vm.EnableEvents (EventType.UserBreak);
3141 var e = GetNextEvent ();
3142 Assert.IsTrue (e is UserBreakEvent);
3146 public void DebugLog () {
3147 vm.EnableEvents (EventType.UserLog);
3152 var e = GetNextEvent ();
3153 Assert.IsTrue (e is UserLogEvent);
3154 var le = e as UserLogEvent;
3156 Assert.AreEqual (5, le.Level);
3157 Assert.AreEqual ("A", le.Category);
3158 Assert.AreEqual ("B", le.Message);
3162 public void TypeGetMethodsByNameFlags () {
3164 var assembly = entry_point.DeclaringType.Assembly;
3165 var type = assembly.GetType ("Tests3");
3167 Assert.IsNotNull (type);
3169 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Public, false);
3170 Assert.AreEqual (1, mm.Length, "#1");
3171 Assert.AreEqual ("M1", mm[0].Name, "#2");
3173 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.NonPublic, false);
3174 Assert.AreEqual (1, mm.Length, "#3");
3175 Assert.AreEqual ("M2", mm[0].Name, "#4");
3177 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public, false);
3178 Assert.AreEqual (7, mm.Length, "#5"); //M3 plus Equals, GetHashCode, GetType, ToString, .ctor
3180 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, false);
3181 Assert.AreEqual (2, mm.Length, "#7");
3183 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
3184 Assert.AreEqual (1, mm.Length, "#9");
3186 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
3187 Assert.AreEqual (5, mm.Length, "#11");
3190 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Public, false);
3191 Assert.AreEqual (1, mm.Length, "#12");
3192 Assert.AreEqual ("M1", mm[0].Name, "#13");
3194 mm = type.GetMethodsByNameFlags ("m1", BindingFlags.Static | BindingFlags.Public, true);
3195 Assert.AreEqual (1, mm.Length, "#14");
3196 Assert.AreEqual ("M1", mm[0].Name, "#15");
3198 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, false);
3199 Assert.AreEqual (1, mm.Length, "#16");
3200 Assert.AreEqual ("M1", mm[0].Name, "#17");
3204 [Category ("only88")]
3205 public void TypeLoadSourceFileFilter () {
3206 Event e = run_until ("type_load");
3208 if (!vm.Version.AtLeast (2, 7))
3211 string srcfile = (e as BreakpointEvent).Method.DeclaringType.GetSourceFiles (true)[0];
3213 var req = vm.CreateTypeLoadRequest ();
3214 req.SourceFileFilter = new string [] { srcfile.ToUpper () };
3218 e = GetNextEvent ();
3219 Assert.IsTrue (e is TypeLoadEvent);
3220 Assert.AreEqual ("TypeLoadClass", (e as TypeLoadEvent).Type.FullName);
3224 public void TypeLoadTypeNameFilter () {
3225 Event e = run_until ("type_load");
3227 var req = vm.CreateTypeLoadRequest ();
3228 req.TypeNameFilter = new string [] { "TypeLoadClass2" };
3232 e = GetNextEvent ();
3233 Assert.IsTrue (e is TypeLoadEvent);
3234 Assert.AreEqual ("TypeLoadClass2", (e as TypeLoadEvent).Type.FullName);
3238 public void GetTypesForSourceFile () {
3241 var types = vm.GetTypesForSourceFile ("dtest-app.cs", false);
3242 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
3243 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
3245 types = vm.GetTypesForSourceFile ("DTEST-app.cs", true);
3246 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
3247 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
3251 public void GetTypesNamed () {
3254 var types = vm.GetTypes ("Tests", false);
3255 Assert.AreEqual (1, types.Count);
3256 Assert.AreEqual ("Tests", types [0].FullName);
3258 types = vm.GetTypes ("System.Exception", false);
3259 Assert.AreEqual (1, types.Count);
3260 Assert.AreEqual ("System.Exception", types [0].FullName);
3264 public void String_GetChars () {
3268 var e = run_until ("arg2");
3270 var frame = e.Thread.GetFrames () [0];
3272 val = frame.GetArgument (0);
3273 Assert.IsTrue (val is StringMirror);
3274 AssertValue ("FOO", val);
3275 var s = (val as StringMirror);
3276 Assert.AreEqual (3, s.Length);
3278 var c = s.GetChars (0, 2);
3279 Assert.AreEqual (2, c.Length);
3280 Assert.AreEqual ('F', c [0]);
3281 Assert.AreEqual ('O', c [1]);
3283 AssertThrows<ArgumentException> (delegate () {
3289 public void GetInterfaces () {
3290 var e = run_until ("arg2");
3292 var frame = e.Thread.GetFrames () [0];
3294 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
3295 var ifaces = cl1.GetInterfaces ();
3296 Assert.AreEqual (1, ifaces.Length);
3297 Assert.AreEqual ("ITest", ifaces [0].Name);
3299 var cl2 = cl1.GetMethod ("Baz").ReturnType;
3300 var ifaces2 = cl2.GetInterfaces ();
3301 Assert.AreEqual (1, ifaces2.Length);
3302 Assert.AreEqual ("ITest`1", ifaces2 [0].Name);
3306 public void GetInterfaceMap () {
3307 var e = run_until ("arg2");
3309 var frame = e.Thread.GetFrames () [0];
3311 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
3312 var iface = cl1.Assembly.GetType ("ITest");
3313 var map = cl1.GetInterfaceMap (iface);
3314 Assert.AreEqual (cl1, map.TargetType);
3315 Assert.AreEqual (iface, map.InterfaceType);
3316 Assert.AreEqual (2, map.InterfaceMethods.Length);
3317 Assert.AreEqual (2, map.TargetMethods.Length);
3321 public void StackAlloc_Breakpoints_Regress2775 () {
3322 // Check that breakpoints on arm don't overwrite stackalloc-ed memory
3323 var e = run_until ("regress_2755");
3325 var frame = e.Thread.GetFrames () [0];
3327 // This breaks at the call site
3328 vm.SetBreakpoint (m, m.Locations [2].ILOffset);
3331 var e2 = GetNextEvent ();
3332 Assert.IsTrue (e2 is BreakpointEvent);
3334 e = run_until ("regress_2755_3");
3335 frame = e.Thread.GetFrames () [1];
3336 var res = frame.GetValue (m.GetLocal ("sum"));
3337 AssertValue (0, res);
3341 public void MethodInfo () {
3342 Event e = run_until ("locals2");
3344 StackFrame frame = e.Thread.GetFrames () [0];
3345 var m = frame.Method;
3347 Assert.IsTrue (m.IsGenericMethod);
3348 Assert.IsFalse (m.IsGenericMethodDefinition);
3350 var args = m.GetGenericArguments ();
3351 Assert.AreEqual (1, args.Length);
3352 Assert.AreEqual ("String", args [0].Name);
3354 var gmd = m.GetGenericMethodDefinition ();
3355 Assert.IsTrue (gmd.IsGenericMethod);
3356 Assert.IsTrue (gmd.IsGenericMethodDefinition);
3357 Assert.AreEqual (gmd, gmd.GetGenericMethodDefinition ());
3359 args = gmd.GetGenericArguments ();
3360 Assert.AreEqual (1, args.Length);
3361 Assert.AreEqual ("T", args [0].Name);
3363 var attrs = m.GetCustomAttributes (true);
3364 Assert.AreEqual (1, attrs.Length);
3365 Assert.AreEqual ("StateMachineAttribute", attrs [0].Constructor.DeclaringType.Name);
3369 public void UnhandledException () {
3372 Start (new string [] { "dtest-app.exe", "unhandled-exception" });
3374 var req = vm.CreateExceptionRequest (null, false, true);
3377 var e = run_until ("unhandled_exception");
3380 var e2 = GetNextEvent ();
3381 Assert.IsTrue (e2 is ExceptionEvent);
3388 public void UnhandledException_2 () {
3391 Start (new string [] { "dtest-app.exe", "unhandled-exception-endinvoke" });
3393 var req = vm.CreateExceptionRequest (null, false, true);
3396 MethodMirror m = entry_point.DeclaringType.GetMethod ("unhandled_exception_endinvoke_2");
3397 Assert.IsNotNull (m);
3398 vm.SetBreakpoint (m, m.ILOffsets [0]);
3400 var e = run_until ("unhandled_exception_endinvoke");
3403 var e2 = GetNextEvent ();
3404 Assert.IsFalse (e2 is ExceptionEvent);
3412 public void UnhandledExceptionUserCode () {
3415 // Exceptions caught in non-user code are treated as unhandled
3416 Start (new string [] { "dtest-app.exe", "unhandled-exception-user" });
3418 var req = vm.CreateExceptionRequest (null, false, true);
3419 req.AssemblyFilter = new List<AssemblyMirror> () { entry_point.DeclaringType.Assembly };
3422 var e = run_until ("unhandled_exception_user");
3425 var e2 = GetNextEvent ();
3426 Assert.IsTrue (e2 is ExceptionEvent);
3434 public void GCWhileSuspended () {
3435 // Check that objects are kept alive during suspensions
3436 Event e = run_until ("gc_suspend_1");
3438 MethodMirror m = entry_point.DeclaringType.GetMethod ("gc_suspend_invoke");
3440 var o = entry_point.DeclaringType.GetValue (entry_point.DeclaringType.GetField ("gc_suspend_field")) as ObjectMirror;
3441 //Console.WriteLine (o);
3443 StackFrame frame = e.Thread.GetFrames () [0];
3444 TypeMirror t = frame.Method.DeclaringType;
3445 for (int i = 0; i < 10; ++i)
3446 t.InvokeMethod (e.Thread, m, new Value [] { });
3448 // This throws an exception if the object is collected
3449 long addr = o.Address;
3451 var o2 = entry_point.DeclaringType.GetValue (entry_point.DeclaringType.GetField ("gc_suspend_field")) as ObjectMirror;
3456 public void MakeGenericMethod () {
3457 Event e = run_until ("bp1");
3459 var intm = vm.RootDomain.GetCorrespondingType (typeof (int));
3460 var stringm = vm.RootDomain.GetCorrespondingType (typeof (string));
3461 var gm = entry_point.DeclaringType.GetMethod ("generic_method");
3462 var res = gm.MakeGenericMethod (new TypeMirror [] { stringm });
3463 var args = res.GetGenericArguments ();
3464 Assert.AreEqual (1, args.Length);
3465 Assert.AreEqual (stringm, args [0]);
3468 AssertThrows<ArgumentNullException> (delegate {
3469 gm.MakeGenericMethod (null);
3471 AssertThrows<ArgumentNullException> (delegate {
3472 gm.MakeGenericMethod (new TypeMirror [] { null });
3474 AssertThrows<ArgumentException> (delegate {
3475 gm.MakeGenericMethod (new TypeMirror [] { stringm, stringm });
3477 AssertThrows<InvalidOperationException> (delegate {
3478 gm.MakeGenericMethod (new TypeMirror [] { intm });
3480 AssertThrows<InvalidOperationException> (delegate {
3481 entry_point.DeclaringType.GetMethod ("Main").MakeGenericMethod (new TypeMirror [] { intm });
3486 public void InspectThreadSuspenedOnWaitOne () {
3488 Start (true, "dtest-app.exe", "wait-one" );
3490 ThreadMirror.NativeTransitions = true;
3492 var evt = run_until ("wait_one");
3493 Assert.IsNotNull (evt, "#1");
3495 var thread = evt.Thread;
3496 Assert.AreEqual (ThreadState.Running, thread.ThreadState, "#1.1");
3498 var frames = thread.GetFrames ();
3499 Assert.IsNotNull (frames, "#2");
3500 Assert.AreEqual (2, frames.Length, "#3");
3501 Assert.AreEqual ("wait_one", frames [0].Method.Name, "#4");
3502 Assert.AreEqual ("Main", frames [1].Method.Name, "#5");
3506 Thread.Sleep (500); //FIXME this is racy, maybe single step? or something?
3509 Assert.AreEqual (ThreadState.WaitSleepJoin, thread.ThreadState, "#6");
3511 frames = thread.GetFrames ();
3512 Assert.AreEqual (4, frames.Length, "#7");
3513 Assert.AreEqual ("WaitOne_internal", frames [0].Method.Name, "#8");
3514 Assert.AreEqual ("WaitOne", frames [1].Method.Name, "#8.1");
3515 Assert.AreEqual ("wait_one", frames [2].Method.Name, "#9");
3516 Assert.AreEqual ("Main", frames [3].Method.Name, "#10");
3519 var frame = frames [0];
3520 Assert.IsTrue (frame.IsNativeTransition, "#11.1");
3523 Assert.Fail ("Known limitation - can't get info from m2n frames");
3524 } catch (AbsentInformationException) {}
3527 Assert.IsFalse (frame.IsNativeTransition, "#12.1");
3528 var wait_one_this = frame.GetThis ();
3529 Assert.IsNotNull (wait_one_this, "#12.2");
3532 var locals = frame.GetVisibleVariables ();
3533 Assert.AreEqual (1, locals.Count, "#13.1");
3535 var local_0 = frame.GetValue (locals [0]);
3536 Assert.IsNotNull (local_0, "#13.2");
3538 Assert.AreEqual (wait_one_this, local_0, "#14.2");
3542 public void GetMethodBody () {
3543 var bevent = run_until ("Main");
3545 var m = bevent.Method.DeclaringType.GetMethod ("get_IntProperty");
3546 var body = m.GetMethodBody ();
3547 foreach (var ins in body.Instructions) {
3548 if (ins.OpCode == OpCodes.Ldfld) {
3549 var field = (FieldInfoMirror)ins.Operand;
3550 Assert.AreEqual ("field_i", field.Name);
3556 public void EvaluateMethod () {
3557 var bevent = run_until ("evaluate_method_2");
3559 var m = bevent.Method.DeclaringType.GetMethod ("get_IntProperty");
3561 var this_obj = bevent.Thread.GetFrames ()[0].GetThis ();
3562 var v = m.Evaluate (this_obj, null);
3563 AssertValue (42, v);
3567 public void SetIP () {
3568 var bevent = run_until ("set_ip_1");
3570 var invalid_loc = bevent.Thread.GetFrames ()[0].Location;
3572 var req = create_step (bevent);
3573 var e = step_out ();
3575 var frames = e.Thread.GetFrames ();
3576 var locs = frames [0].Method.Locations;
3577 var next_loc = locs.First (l => (l.LineNumber == frames [0].Location.LineNumber + 2));
3579 e.Thread.SetIP (next_loc);
3581 /* Check that i ++; j = 5; was skipped */
3582 bevent = run_until ("set_ip_2");
3583 var f = bevent.Thread.GetFrames ()[1];
3584 AssertValue (2, f.GetValue (f.Method.GetLocal ("i")));
3585 AssertValue (0, f.GetValue (f.Method.GetLocal ("j")));
3588 AssertThrows<ArgumentNullException> (delegate {
3589 e.Thread.SetIP (null);
3592 AssertThrows<ArgumentException> (delegate {
3593 e.Thread.SetIP (invalid_loc);
3598 public void SetIPSingleStep () {
3599 // Check that single stepping after set-ip steps from the new ip
3600 var bevent = run_until ("set_ip_1");
3602 var invalid_loc = bevent.Thread.GetFrames ()[0].Location;
3604 var req = create_step (bevent);
3605 req.Size = StepSize.Line;
3606 var e = step_out ();
3608 var frames = e.Thread.GetFrames ();
3609 var locs = frames [0].Method.Locations;
3610 var prev_loc = locs.First (l => (l.LineNumber == frames [0].Location.LineNumber - 3));
3611 AssertValue (2, frames [0].GetValue (frames [0].Method.GetLocal ("i")));
3613 // Set back the ip to the first i ++; line
3614 e.Thread.SetIP (prev_loc);
3617 var f = e.Thread.GetFrames ()[0];
3618 AssertValue (3, f.GetValue (f.Method.GetLocal ("i")));
3622 public void NewInstanceNoCtor () {
3623 var bevent = run_until ("Main");
3625 var stype = bevent.Method.DeclaringType.Assembly.GetType ("AStruct");
3626 var obj = stype.NewInstance ();
3627 Assert.IsTrue (obj is ObjectMirror);
3628 Assert.AreEqual ("AStruct", (obj as ObjectMirror).Type.Name);
3632 public void StaticCtorFilterInCctor () {
3633 // Check that single stepping when in a cctor only ignores
3634 // other cctors, not the current one
3635 var bevent = run_until ("step_filters");
3637 var assembly = entry_point.DeclaringType.Assembly;
3638 var type = assembly.GetType ("Tests/ClassWithCctor");
3639 var cctor = type.GetMethod (".cctor");
3640 vm.SetBreakpoint (cctor, 0);
3643 var e = vm.GetNextEvent ();
3644 Assert.IsTrue (e is BreakpointEvent);
3646 var req = create_step (e);
3647 req.Filter = StepFilter.StaticCtor;
3649 // Make sure we are still in the cctor
3650 Assert.AreEqual (".cctor", e.Thread.GetFrames ()[0].Location.Method.Name);