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_3");
532 // Check DebuggerStepThrough support
533 e = run_until ("ss_step_through");
534 req = create_step (e);
535 req.Filter = StepFilter.DebuggerStepThrough;
537 // Step through step_through_1 ()
539 assert_location (e, "ss_step_through");
540 // Step through StepThroughClass.step_through_2 ()
542 assert_location (e, "ss_step_through");
544 req.Filter = StepFilter.None;
546 assert_location (e, "step_through_3");
549 // Check DebuggerNonUserCode support
550 e = run_until ("ss_non_user_code");
551 req = create_step (e);
552 req.Filter = StepFilter.DebuggerNonUserCode;
554 // Step through non_user_code_1 ()
556 assert_location (e, "ss_non_user_code");
557 // Step through StepThroughClass.non_user_code_2 ()
559 assert_location (e, "ss_non_user_code");
561 req.Filter = StepFilter.None;
563 assert_location (e, "non_user_code_3");
566 // Check that step-over doesn't stop at inner frames with recursive functions
567 e = run_until ("ss_recursive");
568 req = create_step (e);
572 var f = e.Thread.GetFrames () [0];
573 assert_location (e, "ss_recursive");
574 AssertValue (1, f.GetValue (f.Method.GetLocal ("n")));
577 // Check that single stepping doesn't clobber fp values
578 e = run_until ("ss_fp_clobber");
579 req = create_step (e);
581 f = e.Thread.GetFrames ()[0];
583 if ((e as StepEvent).Method.Name == "ss_fp_clobber_2")
587 f = e.Thread.GetFrames ()[0];
588 AssertValue (7.0, f.GetValue (f.Method.GetParameters ()[0]));
593 public void MethodEntryExit () {
594 run_until ("single_stepping");
596 var req1 = vm.CreateMethodEntryRequest ();
597 var req2 = vm.CreateMethodExitRequest ();
603 Event e = GetNextEvent ();
604 Assert.IsTrue (e is MethodEntryEvent);
605 Assert.AreEqual ("ss1", (e as MethodEntryEvent).Method.Name);
609 Assert.IsTrue (e is MethodExitEvent);
610 Assert.AreEqual ("ss1", (e as MethodExitEvent).Method.Name);
617 public void CountFilter () {
618 run_until ("single_stepping");
620 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("ss3");
621 Assert.IsNotNull (m2);
622 vm.SetBreakpoint (m2, 0);
624 var req1 = vm.CreateMethodEntryRequest ();
628 // Enter ss2, ss1 is skipped
630 Event e = GetNextEvent ();
631 Assert.IsTrue (e is MethodEntryEvent);
632 Assert.AreEqual ("ss2", (e as MethodEntryEvent).Method.Name);
634 // Breakpoint on ss3, the entry event is no longer reported
637 Assert.IsTrue (e is BreakpointEvent);
643 public void Arguments () {
646 var e = run_until ("arg1");
648 StackFrame frame = e.Thread.GetFrames () [0];
650 check_arg_val (frame, 0, typeof (sbyte), SByte.MaxValue - 5);
651 check_arg_val (frame, 1, typeof (byte), Byte.MaxValue - 5);
652 check_arg_val (frame, 2, typeof (bool), true);
653 check_arg_val (frame, 3, typeof (short), Int16.MaxValue - 5);
654 check_arg_val (frame, 4, typeof (ushort), UInt16.MaxValue - 5);
655 check_arg_val (frame, 5, typeof (char), 'F');
656 check_arg_val (frame, 6, typeof (int), Int32.MaxValue - 5);
657 check_arg_val (frame, 7, typeof (uint), UInt32.MaxValue - 5);
658 check_arg_val (frame, 8, typeof (long), Int64.MaxValue - 5);
659 check_arg_val (frame, 9, typeof (ulong), UInt64.MaxValue - 5);
660 check_arg_val (frame, 10, typeof (float), 1.2345f);
661 check_arg_val (frame, 11, typeof (double), 6.78910);
663 e = run_until ("arg2");
665 frame = e.Thread.GetFrames () [0];
668 val = frame.GetArgument (0);
669 AssertValue ("FOO", val);
670 Assert.AreEqual ("String", (val as ObjectMirror).Type.Name);
673 val = frame.GetArgument (1);
674 AssertValue (null, val);
677 val = frame.GetArgument (2);
678 AssertValue ("BLA", val);
681 val = frame.GetArgument (3);
682 AssertValue (42, val);
685 val = frame.GetArgument (4);
686 Assert.IsTrue (val is ObjectMirror);
687 Assert.AreEqual ("GClass`1", (val as ObjectMirror).Type.Name);
690 val = frame.GetArgument (5);
691 Assert.IsTrue (val is ObjectMirror);
692 Assert.AreEqual ("Object", (val as ObjectMirror).Type.Name);
694 // this on static methods
695 val = frame.GetThis ();
696 AssertValue (null, val);
698 e = run_until ("arg3");
700 frame = e.Thread.GetFrames () [0];
703 val = frame.GetThis ();
704 Assert.IsTrue (val is ObjectMirror);
705 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
707 // objref in register
708 val = frame.GetArgument (0);
709 AssertValue ("BLA", val);
713 public void Arrays () {
716 var e = run_until ("o2");
718 StackFrame frame = e.Thread.GetFrames () [0];
721 val = frame.GetArgument (0);
722 Assert.IsTrue (val is ArrayMirror);
723 ArrayMirror arr = val as ArrayMirror;
724 Assert.AreEqual (2, arr.Length);
725 AssertValue ("BAR", arr [0]);
726 AssertValue ("BAZ", arr [1]);
728 var vals = arr.GetValues (0, 2);
729 Assert.AreEqual (2, vals.Count);
730 AssertValue ("BAR", vals [0]);
731 AssertValue ("BAZ", vals [1]);
733 arr [0] = vm.RootDomain.CreateString ("ABC");
734 AssertValue ("ABC", arr [0]);
736 arr [0] = vm.CreateValue (null);
737 AssertValue (null, arr [0]);
739 arr.SetValues (0, new Value [] { vm.RootDomain.CreateString ("D1"), vm.RootDomain.CreateString ("D2") });
740 AssertValue ("D1", arr [0]);
741 AssertValue ("D2", arr [1]);
744 val = frame.GetArgument (1);
745 Assert.IsTrue (val is ArrayMirror);
746 arr = val as ArrayMirror;
747 Assert.AreEqual (2, arr.Length);
748 AssertValue (42, arr [0]);
749 AssertValue (43, arr [1]);
752 AssertThrows<IndexOutOfRangeException> (delegate () {
756 AssertThrows<IndexOutOfRangeException> (delegate () {
757 val = arr [Int32.MinValue];
760 AssertThrows<IndexOutOfRangeException> (delegate () {
761 vals = arr.GetValues (0, 3);
764 AssertThrows<IndexOutOfRangeException> (delegate () {
765 arr [2] = vm.CreateValue (null);
768 AssertThrows<IndexOutOfRangeException> (delegate () {
769 arr [Int32.MinValue] = vm.CreateValue (null);
772 AssertThrows<IndexOutOfRangeException> (delegate () {
773 arr.SetValues (0, new Value [] { null, null, null });
777 val = frame.GetArgument (2);
778 Assert.IsTrue (val is ArrayMirror);
779 arr = val as ArrayMirror;
780 Assert.AreEqual (2, arr.Rank);
781 Assert.AreEqual (4, arr.Length);
782 Assert.AreEqual (2, arr.GetLength (0));
783 Assert.AreEqual (2, arr.GetLength (1));
784 Assert.AreEqual (0, arr.GetLowerBound (0));
785 Assert.AreEqual (0, arr.GetLowerBound (1));
786 vals = arr.GetValues (0, 4);
787 AssertValue (1, vals [0]);
788 AssertValue (2, vals [1]);
789 AssertValue (3, vals [2]);
790 AssertValue (4, vals [3]);
792 val = frame.GetArgument (3);
793 Assert.IsTrue (val is ArrayMirror);
794 arr = val as ArrayMirror;
795 Assert.AreEqual (2, arr.Rank);
796 Assert.AreEqual (4, arr.Length);
797 Assert.AreEqual (2, arr.GetLength (0));
798 Assert.AreEqual (2, arr.GetLength (1));
799 Assert.AreEqual (1, arr.GetLowerBound (0));
800 Assert.AreEqual (3, arr.GetLowerBound (1));
802 AssertThrows<ArgumentOutOfRangeException> (delegate () {
805 AssertThrows<ArgumentOutOfRangeException> (delegate () {
809 AssertThrows<ArgumentOutOfRangeException> (delegate () {
810 arr.GetLowerBound (-1);
812 AssertThrows<ArgumentOutOfRangeException> (delegate () {
813 arr.GetLowerBound (2);
816 // arrays treated as generic collections
817 val = frame.GetArgument (4);
818 Assert.IsTrue (val is ArrayMirror);
819 arr = val as ArrayMirror;
823 public void Object_GetValue () {
824 var e = run_until ("o1");
825 var frame = e.Thread.GetFrames () [0];
827 object val = frame.GetThis ();
828 Assert.IsTrue (val is ObjectMirror);
829 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
830 ObjectMirror o = (val as ObjectMirror);
832 TypeMirror t = o.Type;
835 object f = o.GetValue (t.GetField ("field_i"));
837 f = o.GetValue (t.GetField ("field_s"));
838 AssertValue ("S", f);
839 f = o.GetValue (t.GetField ("field_enum"));
840 Assert.IsTrue (f is EnumMirror);
841 Assert.AreEqual (1, (f as EnumMirror).Value);
842 Assert.AreEqual ("B", (f as EnumMirror).StringValue);
844 // Inherited object fields
845 TypeMirror parent = t.BaseType;
846 f = o.GetValue (parent.GetField ("base_field_i"));
848 f = o.GetValue (parent.GetField ("base_field_s"));
849 AssertValue ("T", f);
852 f = o.GetValue (o.Type.GetField ("static_i"));
856 ObjectMirror o2 = frame.GetValue (frame.Method.GetParameters ()[1]) as ObjectMirror;
857 Assert.AreEqual ("GClass`1", o2.Type.Name);
858 TypeMirror t2 = o2.Type;
859 f = o2.GetValue (t2.GetField ("field"));
862 ObjectMirror o3 = frame.GetValue (frame.Method.GetParameters ()[2]) as ObjectMirror;
863 Assert.AreEqual ("GClass`1", o3.Type.Name);
864 TypeMirror t3 = o3.Type;
865 f = o3.GetValue (t3.GetField ("field"));
866 AssertValue ("FOO", f);
869 AssertThrows<ArgumentNullException> (delegate () {
875 public void Object_GetValues () {
876 var e = run_until ("o1");
877 var frame = e.Thread.GetFrames () [0];
879 object val = frame.GetThis ();
880 Assert.IsTrue (val is ObjectMirror);
881 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
882 ObjectMirror o = (val as ObjectMirror);
884 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
886 TypeMirror t = o.Type;
888 object[] vals = o.GetValues (new FieldInfoMirror [] { t.GetField ("field_i"), t.GetField ("field_s") });
892 AssertValue ("S", f);
895 AssertThrows<ArgumentNullException> (delegate () {
899 AssertThrows<ArgumentNullException> (delegate () {
900 o.GetValues (new FieldInfoMirror [] { null });
903 // field of another class
904 AssertThrows<ArgumentException> (delegate () {
905 o.GetValue (val2.Type.GetField ("field_j"));
909 void TestSetValue (ObjectMirror o, string field_name, object val) {
911 o.SetValue (o.Type.GetField (field_name), vm.RootDomain.CreateString ((string)val));
913 o.SetValue (o.Type.GetField (field_name), vm.CreateValue (val));
914 Value f = o.GetValue (o.Type.GetField (field_name));
915 AssertValue (val, f);
919 public void Object_SetValues () {
920 var e = run_until ("o1");
921 var frame = e.Thread.GetFrames () [0];
923 object val = frame.GetThis ();
924 Assert.IsTrue (val is ObjectMirror);
925 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
926 ObjectMirror o = (val as ObjectMirror);
928 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
930 TestSetValue (o, "field_i", 22);
931 TestSetValue (o, "field_bool1", false);
932 TestSetValue (o, "field_bool2", true);
933 TestSetValue (o, "field_char", 'B');
934 TestSetValue (o, "field_byte", (byte)129);
935 TestSetValue (o, "field_sbyte", (sbyte)-33);
936 TestSetValue (o, "field_short", (short)(Int16.MaxValue - 5));
937 TestSetValue (o, "field_ushort", (ushort)(UInt16.MaxValue - 5));
938 TestSetValue (o, "field_long", Int64.MaxValue - 5);
939 TestSetValue (o, "field_ulong", (ulong)(UInt64.MaxValue - 5));
940 TestSetValue (o, "field_float", 6.28f);
941 TestSetValue (o, "field_double", 6.28);
942 TestSetValue (o, "static_i", 23);
943 TestSetValue (o, "field_s", "CDEF");
948 f = o.GetValue (o.Type.GetField ("field_intptr"));
949 Assert.IsInstanceOfType (typeof (StructMirror), f);
950 AssertValue (Int32.MaxValue - 5, (f as StructMirror).Fields [0]);
953 FieldInfoMirror field = o.Type.GetField ("field_enum");
954 f = o.GetValue (field);
955 (f as EnumMirror).Value = 5;
956 o.SetValue (field, f);
957 f = o.GetValue (field);
958 Assert.AreEqual (5, (f as EnumMirror).Value);
961 o.SetValue (o.Type.GetField ("field_s"), vm.CreateValue (null));
962 f = o.GetValue (o.Type.GetField ("field_s"));
963 AssertValue (null, f);
966 field = o.Type.GetField ("generic_field_struct");
967 f = o.GetValue (field);
968 o.SetValue (field, f);
971 field = o.Type.GetField ("field_nullable");
972 f = o.GetValue (field);
973 AssertValue (0, (f as StructMirror).Fields [0]);
974 AssertValue (false, (f as StructMirror).Fields [1]);
975 o.SetValue (field, vm.CreateValue (6));
976 f = o.GetValue (field);
977 AssertValue (6, (f as StructMirror).Fields [0]);
978 AssertValue (true, (f as StructMirror).Fields [1]);
979 o.SetValue (field, vm.CreateValue (null));
980 f = o.GetValue (field);
981 AssertValue (0, (f as StructMirror).Fields [0]);
982 AssertValue (false, (f as StructMirror).Fields [1]);
985 AssertThrows<ArgumentNullException> (delegate () {
986 o.SetValues (null, new Value [0]);
989 AssertThrows<ArgumentNullException> (delegate () {
990 o.SetValues (new FieldInfoMirror [0], null);
993 AssertThrows<ArgumentNullException> (delegate () {
994 o.SetValues (new FieldInfoMirror [] { null }, new Value [1] { null });
997 // vtype with a wrong type
998 AssertThrows<ArgumentException> (delegate () {
999 o.SetValue (o.Type.GetField ("field_struct"), o.GetValue (o.Type.GetField ("field_enum")));
1002 // reference type not assignment compatible
1003 AssertThrows<ArgumentException> (delegate () {
1004 o.SetValue (o.Type.GetField ("field_class"), o);
1007 // field of another class
1008 AssertThrows<ArgumentException> (delegate () {
1009 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
1014 public void Type_SetValue () {
1015 var e = run_until ("o1");
1016 var frame = e.Thread.GetFrames () [0];
1019 object val = frame.GetThis ();
1020 Assert.IsTrue (val is ObjectMirror);
1021 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
1022 ObjectMirror o = (val as ObjectMirror);
1024 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1026 o.Type.SetValue (o.Type.GetField ("static_i"), vm.CreateValue (55));
1027 f = o.Type.GetValue (o.Type.GetField ("static_i"));
1028 AssertValue (55, f);
1030 o.Type.SetValue (o.Type.GetField ("static_s"), vm.RootDomain.CreateString ("B"));
1031 f = o.Type.GetValue (o.Type.GetField ("static_s"));
1032 AssertValue ("B", f);
1034 // Argument checking
1035 AssertThrows<ArgumentNullException> (delegate () {
1036 o.Type.SetValue (null, vm.CreateValue (0));
1039 AssertThrows<ArgumentNullException> (delegate () {
1040 o.Type.SetValue (o.Type.GetField ("static_i"), null);
1043 // field of another class
1044 AssertThrows<ArgumentException> (delegate () {
1045 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
1050 public void TypeInfo () {
1051 Event e = run_until ("ti2");
1052 StackFrame frame = e.Thread.GetFrames () [0];
1057 t = frame.Method.GetParameters ()[0].ParameterType;
1059 Assert.AreEqual ("String[]", t.Name);
1060 Assert.AreEqual ("string[]", t.CSharpName);
1061 Assert.AreEqual ("Array", t.BaseType.Name);
1062 Assert.AreEqual (true, t.HasElementType);
1063 Assert.AreEqual (true, t.IsArray);
1064 Assert.AreEqual (1, t.GetArrayRank ());
1065 Assert.AreEqual ("String", t.GetElementType ().Name);
1067 t = frame.Method.GetParameters ()[2].ParameterType;
1069 Assert.AreEqual ("Int32[,]", t.Name);
1071 //Assert.AreEqual ("int[,]", t.CSharpName);
1072 Assert.AreEqual ("Array", t.BaseType.Name);
1073 Assert.AreEqual (true, t.HasElementType);
1074 Assert.AreEqual (true, t.IsArray);
1075 Assert.AreEqual (2, t.GetArrayRank ());
1076 Assert.AreEqual ("Int32", t.GetElementType ().Name);
1079 t = frame.Method.GetParameters ()[3].ParameterType;
1081 //Assert.AreEqual ("Int32&", t.Name);
1082 //Assert.AreEqual (true, t.IsByRef);
1083 //Assert.AreEqual (true, t.HasElementType);
1086 t = frame.Method.GetParameters ()[4].ParameterType;
1088 //Assert.AreEqual ("Int32*", t.Name);
1089 Assert.AreEqual (true, t.IsPointer);
1090 Assert.AreEqual (true, t.HasElementType);
1091 Assert.AreEqual ("Int32", t.GetElementType ().Name);
1092 Assert.AreEqual (false, t.IsPrimitive);
1095 t = frame.Method.GetParameters ()[5].ParameterType;
1096 Assert.AreEqual (true, t.IsPrimitive);
1099 t = frame.Method.GetParameters ()[6].ParameterType;
1100 Assert.AreEqual ("AStruct", t.Name);
1101 Assert.AreEqual (false, t.IsPrimitive);
1102 Assert.AreEqual (true, t.IsValueType);
1103 Assert.AreEqual (false, t.IsClass);
1106 t = frame.Method.GetParameters ()[7].ParameterType;
1107 Assert.AreEqual ("Tests", t.Name);
1108 var nested = (from nt in t.GetNestedTypes () where nt.IsNestedPublic select nt).ToArray ();
1109 Assert.AreEqual (1, nested.Length);
1110 Assert.AreEqual ("NestedClass", nested [0].Name);
1111 Assert.IsTrue (t.BaseType.IsAssignableFrom (t));
1112 Assert.IsTrue (!t.IsAssignableFrom (t.BaseType));
1114 // generic instances
1115 t = frame.Method.GetParameters ()[9].ParameterType;
1116 Assert.AreEqual ("GClass`1", t.Name);
1117 Assert.IsTrue (t.IsGenericType);
1118 Assert.IsFalse (t.IsGenericTypeDefinition);
1120 var args = t.GetGenericArguments ();
1121 Assert.AreEqual (1, args.Length);
1122 Assert.AreEqual ("Int32", args [0].Name);
1124 // generic type definitions
1125 var gtd = t.GetGenericTypeDefinition ();
1126 Assert.AreEqual ("GClass`1", gtd.Name);
1127 Assert.IsTrue (gtd.IsGenericType);
1128 Assert.IsTrue (gtd.IsGenericTypeDefinition);
1129 Assert.AreEqual (gtd, gtd.GetGenericTypeDefinition ());
1131 args = gtd.GetGenericArguments ();
1132 Assert.AreEqual (1, args.Length);
1133 Assert.AreEqual ("T", args [0].Name);
1136 t = frame.Method.GetParameters ()[10].ParameterType;
1137 Assert.AreEqual ("AnEnum", t.Name);
1138 Assert.IsTrue (t.IsEnum);
1139 Assert.AreEqual ("Int32", t.EnumUnderlyingType.Name);
1142 t = frame.Method.GetParameters ()[11].ParameterType;
1143 Assert.AreEqual ("TypedReference", t.Name);
1146 t = frame.Method.GetParameters ()[7].ParameterType;
1148 var props = t.GetProperties ();
1149 Assert.AreEqual (3, props.Length);
1150 foreach (PropertyInfoMirror prop in props) {
1151 ParameterInfoMirror[] indexes = prop.GetIndexParameters ();
1153 if (prop.Name == "IntProperty") {
1154 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1155 Assert.AreEqual ("get_IntProperty", prop.GetGetMethod ().Name);
1156 Assert.AreEqual ("set_IntProperty", prop.GetSetMethod ().Name);
1157 Assert.AreEqual (0, indexes.Length);
1158 } else if (prop.Name == "ReadOnlyProperty") {
1159 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1160 Assert.AreEqual ("get_ReadOnlyProperty", prop.GetGetMethod ().Name);
1161 Assert.AreEqual (null, prop.GetSetMethod ());
1162 Assert.AreEqual (0, indexes.Length);
1163 } else if (prop.Name == "IndexedProperty") {
1164 Assert.AreEqual (1, indexes.Length);
1165 Assert.AreEqual ("Int32", indexes [0].ParameterType.Name);
1169 // custom attributes
1170 t = frame.Method.GetParameters ()[8].ParameterType;
1171 Assert.AreEqual ("Tests2", t.Name);
1172 var attrs = t.GetCustomAttributes (true);
1173 Assert.AreEqual (5, attrs.Length);
1174 foreach (var attr in attrs) {
1175 if (attr.Constructor.DeclaringType.Name == "DebuggerDisplayAttribute") {
1176 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1177 Assert.AreEqual ("Tests", attr.ConstructorArguments [0].Value);
1178 Assert.AreEqual (2, attr.NamedArguments.Count);
1179 Assert.AreEqual ("Name", attr.NamedArguments [0].Property.Name);
1180 Assert.AreEqual ("FOO", attr.NamedArguments [0].TypedValue.Value);
1181 Assert.AreEqual ("Target", attr.NamedArguments [1].Property.Name);
1182 Assert.IsInstanceOfType (typeof (TypeMirror), attr.NamedArguments [1].TypedValue.Value);
1183 Assert.AreEqual ("Int32", (attr.NamedArguments [1].TypedValue.Value as TypeMirror).Name);
1184 } else if (attr.Constructor.DeclaringType.Name == "DebuggerTypeProxyAttribute") {
1185 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1186 Assert.IsInstanceOfType (typeof (TypeMirror), attr.ConstructorArguments [0].Value);
1187 Assert.AreEqual ("Tests", (attr.ConstructorArguments [0].Value as TypeMirror).Name);
1188 } else if (attr.Constructor.DeclaringType.Name == "BAttribute") {
1189 Assert.AreEqual (2, attr.NamedArguments.Count);
1190 Assert.AreEqual ("afield", attr.NamedArguments [0].Field.Name);
1191 Assert.AreEqual ("bfield", attr.NamedArguments [1].Field.Name);
1192 } else if (attr.Constructor.DeclaringType.Name == "ClassInterfaceAttribute") {
1193 // inherited from System.Object
1194 //} else if (attr.Constructor.DeclaringType.Name == "Serializable") {
1195 // inherited from System.Object
1196 } else if (attr.Constructor.DeclaringType.Name == "ComVisibleAttribute") {
1197 // inherited from System.Object
1199 Assert.Fail (attr.Constructor.DeclaringType.Name);
1203 var assembly = entry_point.DeclaringType.Assembly;
1204 var type = assembly.GetType ("Tests4");
1205 Assert.IsFalse (type.IsInitialized);
1209 public void FieldInfo () {
1210 Event e = run_until ("ti2");
1211 StackFrame frame = e.Thread.GetFrames () [0];
1215 t = frame.Method.GetParameters ()[8].ParameterType;
1216 Assert.AreEqual ("Tests2", t.Name);
1218 var fi = t.GetField ("field_j");
1219 var attrs = fi.GetCustomAttributes (true);
1220 Assert.AreEqual (1, attrs.Length);
1221 var attr = attrs [0];
1222 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1223 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1224 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1225 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1229 public void PropertyInfo () {
1230 Event e = run_until ("ti2");
1231 StackFrame frame = e.Thread.GetFrames () [0];
1235 t = frame.Method.GetParameters ()[8].ParameterType;
1236 Assert.AreEqual ("Tests2", t.Name);
1238 var pi = t.GetProperty ("AProperty");
1239 var attrs = pi.GetCustomAttributes (true);
1240 Assert.AreEqual (1, attrs.Length);
1241 var attr = attrs [0];
1242 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1243 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1244 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1245 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1249 [Category ("only5")]
1250 public void Type_GetValue () {
1251 Event e = run_until ("o1");
1252 StackFrame frame = e.Thread.GetFrames () [0];
1254 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1256 TypeMirror t = o.Type;
1258 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1261 object f = t.GetValue (o.Type.GetField ("static_i"));
1262 AssertValue (55, f);
1264 f = t.GetValue (o.Type.GetField ("static_s"));
1265 AssertValue ("A", f);
1267 // literal static fields
1268 f = t.GetValue (o.Type.GetField ("literal_i"));
1269 AssertValue (56, f);
1271 f = t.GetValue (o.Type.GetField ("literal_s"));
1272 AssertValue ("B", f);
1274 // Inherited static fields
1275 TypeMirror parent = t.BaseType;
1276 f = t.GetValue (parent.GetField ("base_static_i"));
1277 AssertValue (57, f);
1279 f = t.GetValue (parent.GetField ("base_static_s"));
1280 AssertValue ("C", f);
1282 // thread static field
1283 f = t.GetValue (t.GetField ("tls_i"), e.Thread);
1284 AssertValue (42, f);
1286 // Argument checking
1287 AssertThrows<ArgumentNullException> (delegate () {
1292 AssertThrows<ArgumentException> (delegate () {
1293 t.GetValue (o.Type.GetField ("field_i"));
1296 // field on another type
1297 AssertThrows<ArgumentException> (delegate () {
1298 t.GetValue (val2.Type.GetField ("static_field_j"));
1301 // special static field
1302 AssertThrows<ArgumentException> (delegate () {
1303 t.GetValue (t.GetField ("tls_i"));
1308 public void Type_GetValues () {
1309 Event e = run_until ("o1");
1310 StackFrame frame = e.Thread.GetFrames () [0];
1312 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1314 TypeMirror t = o.Type;
1317 object[] vals = t.GetValues (new FieldInfoMirror [] { t.GetField ("static_i"), t.GetField ("static_s") });
1318 object f = vals [0];
1319 AssertValue (55, f);
1322 AssertValue ("A", f);
1324 // Argument checking
1325 AssertThrows<ArgumentNullException> (delegate () {
1329 AssertThrows<ArgumentNullException> (delegate () {
1330 t.GetValues (new FieldInfoMirror [] { null });
1335 public void ObjRefs () {
1336 Event e = run_until ("objrefs1");
1337 StackFrame frame = e.Thread.GetFrames () [0];
1339 ObjectMirror o = frame.GetThis () as ObjectMirror;
1340 ObjectMirror child = o.GetValue (o.Type.GetField ("child")) as ObjectMirror;
1342 Assert.IsTrue (child.Address != 0);
1344 // Check that object references are internalized correctly
1345 Assert.AreEqual (o, frame.GetThis ());
1347 run_until ("objrefs2");
1349 // child should be gc'd now
1350 // This is not deterministic
1351 //Assert.IsTrue (child.IsCollected);
1354 * No longer works since Type is read eagerly
1357 AssertThrows<ObjectCollectedException> (delegate () {
1358 TypeMirror t = child.Type;
1362 AssertThrows<ObjectCollectedException> (delegate () {
1363 long addr = child.Address;
1369 public void Type_GetObject () {
1370 Event e = run_until ("o1");
1371 StackFrame frame = e.Thread.GetFrames () [0];
1373 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1375 TypeMirror t = o.Type;
1377 Assert.AreEqual ("MonoType", t.GetTypeObject ().Type.Name);
1381 public void VTypes () {
1382 Event e = run_until ("vtypes1");
1383 StackFrame frame = e.Thread.GetFrames () [0];
1386 ObjectMirror o = frame.GetThis () as ObjectMirror;
1387 var obj = o.GetValue (o.Type.GetField ("field_struct"));
1388 Assert.IsTrue (obj is StructMirror);
1389 var s = obj as StructMirror;
1390 Assert.AreEqual ("AStruct", s.Type.Name);
1391 AssertValue (42, s ["i"]);
1393 AssertValue ("S", obj);
1394 AssertValue (43, s ["k"]);
1395 obj = o.GetValue (o.Type.GetField ("field_boxed_struct"));
1396 Assert.IsTrue (obj is StructMirror);
1397 s = obj as StructMirror;
1398 Assert.AreEqual ("AStruct", s.Type.Name);
1399 AssertValue (42, s ["i"]);
1401 // Check decoding of nested structs (#14942)
1402 obj = o.GetValue (o.Type.GetField ("nested_struct"));
1403 o.SetValue (o.Type.GetField ("nested_struct"), obj);
1405 // Check round tripping of boxed struct fields (#12354)
1406 obj = o.GetValue (o.Type.GetField ("boxed_struct_field"));
1407 o.SetValue (o.Type.GetField ("boxed_struct_field"), obj);
1408 obj = o.GetValue (o.Type.GetField ("boxed_struct_field"));
1409 s = obj as StructMirror;
1410 AssertValue (1, s ["key"]);
1412 Assert.IsTrue (obj is StructMirror);
1413 s = obj as StructMirror;
1414 AssertValue (42, s ["m_value"]);
1416 // vtypes as arguments
1417 s = frame.GetArgument (0) as StructMirror;
1418 AssertValue (44, s ["i"]);
1420 AssertValue ("T", obj);
1421 AssertValue (45, s ["k"]);
1423 // vtypes as array entries
1424 var arr = frame.GetArgument (1) as ArrayMirror;
1426 Assert.IsTrue (obj is StructMirror);
1427 s = obj as StructMirror;
1428 AssertValue (1, s ["i"]);
1429 AssertValue ("S1", s ["s"]);
1431 Assert.IsTrue (obj is StructMirror);
1432 s = obj as StructMirror;
1433 AssertValue (2, s ["i"]);
1434 AssertValue ("S2", s ["s"]);
1437 var typedref = frame.GetArgument (2) as StructMirror;
1438 Assert.IsTrue (typedref is StructMirror);
1440 // Argument checking
1441 s = frame.GetArgument (0) as StructMirror;
1442 AssertThrows<ArgumentException> (delegate () {
1446 // generic vtype instances
1447 o = frame.GetThis () as ObjectMirror;
1448 obj = o.GetValue (o.Type.GetField ("generic_field_struct"));
1449 Assert.IsTrue (obj is StructMirror);
1450 s = obj as StructMirror;
1451 Assert.AreEqual ("GStruct`1", s.Type.Name);
1452 AssertValue (42, s ["i"]);
1454 // this on vtype methods
1455 e = run_until ("vtypes2");
1456 e = step_until (e.Thread, "foo");
1458 frame = e.Thread.GetFrames () [0];
1460 Assert.AreEqual ("foo", (e as StepEvent).Method.Name);
1461 obj = frame.GetThis ();
1463 Assert.IsTrue (obj is StructMirror);
1464 s = obj as StructMirror;
1465 AssertValue (44, s ["i"]);
1466 AssertValue ("T", s ["s"]);
1467 AssertValue (45, s ["k"]);
1469 // this on static vtype methods
1470 e = run_until ("vtypes3");
1471 e = step_until (e.Thread, "static_foo");
1473 frame = e.Thread.GetFrames () [0];
1475 Assert.AreEqual ("static_foo", (e as StepEvent).Method.Name);
1476 obj = frame.GetThis ();
1477 AssertValue (null, obj);
1479 // vtypes which reference themselves recursively
1480 e = run_until ("vtypes4_2");
1481 frame = e.Thread.GetFrames () [0];
1483 Assert.IsTrue (frame.GetArgument (0) is StructMirror);
1487 public void AssemblyInfo () {
1488 Event e = run_until ("single_stepping");
1490 StackFrame frame = e.Thread.GetFrames () [0];
1492 var aname = frame.Method.DeclaringType.Assembly.GetName ();
1493 Assert.AreEqual ("dtest-app, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", aname.ToString ());
1495 ModuleMirror m = frame.Method.DeclaringType.Module;
1497 Assert.AreEqual ("dtest-app.exe", m.Name);
1498 Assert.AreEqual ("dtest-app.exe", m.ScopeName);
1499 Assert.IsTrue (m.FullyQualifiedName.IndexOf ("dtest-app.exe") != -1);
1500 Guid guid = m.ModuleVersionId;
1501 Assert.AreEqual (frame.Method.DeclaringType.Assembly, m.Assembly);
1502 Assert.AreEqual (frame.Method.DeclaringType.Assembly.ManifestModule, m);
1504 // This is no longer true on 4.0
1505 //Assert.AreEqual ("Assembly", frame.Method.DeclaringType.Assembly.GetAssemblyObject ().Type.Name);
1507 TypeMirror t = vm.RootDomain.Corlib.GetType ("System.Diagnostics.DebuggerDisplayAttribute");
1508 Assert.AreEqual ("DebuggerDisplayAttribute", t.Name);
1512 public void LocalsInfo () {
1513 Event e = run_until ("locals2");
1515 StackFrame frame = e.Thread.GetFrames () [0];
1517 var locals = frame.Method.GetLocals ();
1518 Assert.AreEqual (8, locals.Length);
1519 for (int i = 0; i < 8; ++i) {
1520 if (locals [i].Name == "args") {
1521 Assert.IsTrue (locals [i].IsArg);
1522 Assert.AreEqual ("String[]", locals [i].Type.Name);
1523 } else if (locals [i].Name == "arg") {
1524 Assert.IsTrue (locals [i].IsArg);
1525 Assert.AreEqual ("Int32", locals [i].Type.Name);
1526 } else if (locals [i].Name == "i") {
1527 Assert.IsFalse (locals [i].IsArg);
1528 Assert.AreEqual ("Int64", locals [i].Type.Name);
1529 } else if (locals [i].Name == "j") {
1530 Assert.IsFalse (locals [i].IsArg);
1531 Assert.AreEqual ("Int32", locals [i].Type.Name);
1532 } else if (locals [i].Name == "s") {
1533 Assert.IsFalse (locals [i].IsArg);
1534 Assert.AreEqual ("String", locals [i].Type.Name);
1535 } else if (locals [i].Name == "t") {
1537 Assert.IsTrue (locals [i].IsArg);
1538 Assert.AreEqual ("String", locals [i].Type.Name);
1539 } else if (locals [i].Name == "rs") {
1540 Assert.IsTrue (locals [i].IsArg);
1541 Assert.AreEqual ("String", locals [i].Type.Name);
1542 } else if (locals [i].Name == "astruct") {
1549 Event step_once () {
1551 var e = GetNextEvent ();
1552 Assert.IsTrue (e is StepEvent);
1556 Event step_into () {
1557 step_req.Disable ();
1558 step_req.Depth = StepDepth.Into;
1560 return step_once ();
1563 Event step_over () {
1564 step_req.Disable ();
1565 step_req.Depth = StepDepth.Over;
1567 return step_once ();
1571 step_req.Disable ();
1572 step_req.Depth = StepDepth.Out;
1574 return step_once ();
1578 public void Locals () {
1579 var be = run_until ("locals1");
1581 StackFrame frame = be.Thread.GetFrames () [0];
1582 MethodMirror m1 = frame.Method;
1584 // Compiler generated byref local
1585 foreach (var l in m1.GetLocals ()) {
1586 // The byval flag is hidden from the type
1587 if (l.Name != "ri" && l.Type.Name == "Double")
1588 AssertValue (null, frame.GetValue (l));
1591 be = run_until ("locals2");
1593 frame = be.Thread.GetFrames () [0];
1595 object val = frame.GetValue (frame.Method.GetLocal ("i"));
1596 AssertValue (0, val);
1598 var req = create_step (be);
1605 var e = step_once ();
1606 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1608 // Execute s = "AB";
1610 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1612 frame = e.Thread.GetFrames () [0];
1614 val = frame.GetValue (frame.Method.GetLocal ("i"));
1615 AssertValue (42, val);
1617 LocalVariable[] locals = frame.Method.GetLocals ();
1618 var vals = frame.GetValues (locals);
1619 Assert.AreEqual (locals.Length, vals.Length);
1620 for (int i = 0; i < locals.Length; ++i) {
1621 if (locals [i].Name == "i")
1622 AssertValue (42, vals [i]);
1623 if (locals [i].Name == "s")
1624 AssertValue ("AB", vals [i]);
1625 if (locals [i].Name == "t")
1626 AssertValue ("ABC", vals [i]);
1629 // Argument checking
1632 AssertThrows<ArgumentNullException> (delegate () {
1633 frame.GetValue ((LocalVariable)null);
1635 // GetValue () local from another method
1636 AssertThrows<ArgumentException> (delegate () {
1637 frame.GetValue (m1.GetLocal ("foo"));
1641 AssertThrows<ArgumentNullException> (delegate () {
1642 frame.GetValue ((ParameterInfoMirror)null);
1644 // GetValue () local from another method
1645 AssertThrows<ArgumentException> (delegate () {
1646 frame.GetValue (m1.GetParameters ()[0]);
1649 // GetValues () null
1650 AssertThrows<ArgumentNullException> (delegate () {
1651 frame.GetValues (null);
1653 // GetValues () embedded null
1654 AssertThrows<ArgumentNullException> (delegate () {
1655 frame.GetValues (new LocalVariable [] { null });
1657 // GetValues () local from another method
1658 AssertThrows<ArgumentException> (delegate () {
1659 frame.GetValues (new LocalVariable [] { m1.GetLocal ("foo") });
1662 AssertThrows<ArgumentException> (delegate () {
1663 val = frame.GetValue (frame.Method.ReturnParameter);
1666 // invalid stack frames
1668 e = GetNextEvent ();
1669 Assert.IsTrue (e is StepEvent);
1670 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1672 AssertThrows<InvalidStackFrameException> (delegate () {
1673 frame.GetValue (frame.Method.GetLocal ("i"));
1679 be = run_until ("locals7");
1681 req = create_step (be);
1687 // Test that locals are initialized
1688 frame = e.Thread.GetFrames () [0];
1689 val = frame.GetValue (frame.Method.GetLocal ("t"));
1690 AssertValue (0, val);
1694 Assert.AreEqual ("locals7", (e as StepEvent).Method.Name);
1698 Assert.AreEqual ("locals7", (e as StepEvent).Method.Name);
1700 frame = e.Thread.GetFrames () [0];
1701 val = frame.GetValue (frame.Method.GetParameters ()[0]);
1702 AssertValue (22, val);
1703 val = frame.GetValue (frame.Method.GetLocal ("t"));
1704 AssertValue (22, val);
1705 val = frame.GetValue (frame.Method.GetLocal ("t2"));
1706 AssertValue (22, val);
1710 public void GetVisibleVariables () {
1711 Event e = run_until ("locals4");
1714 var locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
1715 Assert.AreEqual (2, locals.Count);
1716 var loc = locals.First (l => l.Name == "i");
1717 Assert.AreEqual ("Int64", loc.Type.Name);
1718 loc = locals.First (l => l.Name == "s");
1719 Assert.AreEqual ("String", loc.Type.Name);
1721 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
1722 Assert.AreEqual ("i", loc.Name);
1723 Assert.AreEqual ("Int64", loc.Type.Name);
1725 e = run_until ("locals5");
1728 locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
1729 Assert.AreEqual (2, locals.Count);
1730 loc = locals.First (l => l.Name == "i");
1731 Assert.AreEqual ("String", loc.Type.Name);
1732 loc = locals.First (l => l.Name == "s");
1733 Assert.AreEqual ("String", loc.Type.Name);
1735 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
1736 Assert.AreEqual ("i", loc.Name);
1737 Assert.AreEqual ("String", loc.Type.Name);
1739 // Variable in another scope
1740 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("j");
1741 Assert.IsNull (loc);
1745 public void Exit () {
1750 var e = GetNextEvent ();
1751 Assert.IsInstanceOfType (typeof (VMDeathEvent), e);
1753 Assert.AreEqual (5, (e as VMDeathEvent).ExitCode);
1756 /* Could be a remote vm with no process */
1759 Assert.AreEqual (5, p.ExitCode);
1762 AssertThrows<VMDisconnectedException> (delegate () {
1771 public void Dispose () {
1776 var e = GetNextEvent ();
1777 Assert.IsInstanceOfType (typeof (VMDisconnectEvent), e);
1780 /* Could be a remote vm with no process */
1783 Assert.AreEqual (3, p.ExitCode);
1786 AssertThrows<VMDisconnectedException> (delegate () {
1795 public void ColumnNumbers () {
1796 Event e = run_until ("line_numbers");
1798 // FIXME: Merge this with LineNumbers () when its fixed
1800 step_req = create_step (e);
1801 step_req.Depth = StepDepth.Into;
1809 e = GetNextEvent ();
1810 Assert.IsTrue (e is StepEvent);
1811 if (e.Thread.GetFrames ()[0].Method.Name == "ln1")
1815 // Do an additional step over so we are not on the beginning line of the method
1816 step_req.Disable ();
1817 step_req.Depth = StepDepth.Over;
1820 e = GetNextEvent ();
1821 Assert.IsTrue (e is StepEvent);
1823 l = e.Thread.GetFrames ()[0].Location;
1825 Assert.AreEqual (3, l.ColumnNumber);
1827 step_req.Disable ();
1831 // Broken by mcs+runtime changes (#5438)
1832 [Category("NotWorking")]
1833 public void LineNumbers () {
1834 Event e = run_until ("line_numbers");
1836 step_req = create_step (e);
1837 step_req.Depth = StepDepth.Into;
1844 e = GetNextEvent ();
1845 Assert.IsTrue (e is StepEvent);
1847 l = e.Thread.GetFrames ()[0].Location;
1849 Assert.IsTrue (l.SourceFile.IndexOf ("dtest-app.cs") != -1);
1850 Assert.AreEqual ("ln1", l.Method.Name);
1853 using (FileStream fs = new FileStream (l.SourceFile, FileMode.Open, FileAccess.Read)) {
1854 MD5 md5 = MD5.Create ();
1855 var hash = md5.ComputeHash (fs);
1857 for (int i = 0; i < 16; ++i)
1858 Assert.AreEqual (hash [i], l.SourceFileHash [i]);
1861 int line_base = l.LineNumber;
1864 e = GetNextEvent ();
1865 Assert.IsTrue (e is StepEvent);
1866 l = e.Thread.GetFrames ()[0].Location;
1867 Assert.AreEqual ("ln2", l.Method.Name);
1868 Assert.AreEqual (line_base + 6, l.LineNumber);
1871 e = GetNextEvent ();
1872 Assert.IsTrue (e is StepEvent);
1873 l = e.Thread.GetFrames ()[0].Location;
1874 Assert.AreEqual ("ln1", l.Method.Name);
1875 Assert.AreEqual (line_base + 1, l.LineNumber);
1878 e = GetNextEvent ();
1879 Assert.IsTrue (e is StepEvent);
1880 l = e.Thread.GetFrames ()[0].Location;
1881 Assert.AreEqual ("ln3", l.Method.Name);
1882 Assert.AreEqual (line_base + 11, 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.IsTrue (l.SourceFile.EndsWith ("FOO"));
1890 Assert.AreEqual (55, l.LineNumber);
1893 e = GetNextEvent ();
1894 Assert.IsTrue (e is StepEvent);
1895 l = e.Thread.GetFrames ()[0].Location;
1896 Assert.AreEqual ("ln1", l.Method.Name);
1897 Assert.AreEqual (line_base + 2, l.LineNumber);
1899 // GetSourceFiles ()
1900 string[] sources = l.Method.DeclaringType.GetSourceFiles ();
1901 Assert.AreEqual (2, sources.Length);
1902 Assert.AreEqual ("dtest-app.cs", sources [0]);
1903 Assert.AreEqual ("FOO", sources [1]);
1905 sources = l.Method.DeclaringType.GetSourceFiles (true);
1906 Assert.AreEqual (2, sources.Length);
1907 Assert.IsTrue (sources [0].EndsWith ("dtest-app.cs"));
1908 Assert.IsTrue (sources [1].EndsWith ("FOO"));
1912 public void Suspend () {
1915 Start (new string [] { "dtest-app.exe", "suspend-test" });
1917 Event e = run_until ("suspend");
1919 ThreadMirror main = e.Thread;
1927 // The debuggee should be suspended while it is running the infinite loop
1929 StackFrame frame = main.GetFrames ()[0];
1930 Assert.AreEqual ("suspend", frame.Method.Name);
1934 // resuming when not suspended
1935 AssertThrows<InvalidOperationException> (delegate () {
1945 public void AssemblyLoad () {
1946 Event e = run_until ("assembly_load");
1948 var load_req = vm.CreateAssemblyLoadRequest ();
1953 e = GetNextEvent ();
1954 Assert.IsInstanceOfType (typeof (AssemblyLoadEvent), e);
1955 Assert.IsTrue ((e as AssemblyLoadEvent).Assembly.Location.EndsWith ("System.dll"));
1957 var frames = e.Thread.GetFrames ();
1958 Assert.IsTrue (frames.Length > 0);
1959 Assert.AreEqual ("assembly_load", frames [0].Method.Name);
1963 public void CreateValue () {
1966 v = vm.CreateValue (1);
1967 Assert.AreEqual (vm, v.VirtualMachine);
1968 Assert.AreEqual (1, v.Value);
1970 v = vm.CreateValue (null);
1971 Assert.AreEqual (vm, v.VirtualMachine);
1972 Assert.AreEqual (null, v.Value);
1974 // Argument checking
1975 AssertThrows <ArgumentException> (delegate () {
1976 v = vm.CreateValue ("FOO");
1981 public void CreateString () {
1982 StringMirror s = vm.RootDomain.CreateString ("ABC");
1984 Assert.AreEqual (vm, s.VirtualMachine);
1985 Assert.AreEqual ("ABC", s.Value);
1986 Assert.AreEqual (vm.RootDomain, s.Domain);
1989 StringBuilder sb = new StringBuilder ();
1990 for (int i = 0; i < 1024; ++i)
1992 s = vm.RootDomain.CreateString (sb.ToString ());
1994 // Argument checking
1995 AssertThrows <ArgumentNullException> (delegate () {
1996 s = vm.RootDomain.CreateString (null);
2001 public void CreateBoxedValue () {
2002 ObjectMirror o = vm.RootDomain.CreateBoxedValue (new PrimitiveValue (vm, 42));
2004 Assert.AreEqual ("Int32", o.Type.Name);
2005 //AssertValue (42, m.GetValue (o.Type.GetField ("m_value")));
2007 // Argument checking
2008 AssertThrows <ArgumentNullException> (delegate () {
2009 vm.RootDomain.CreateBoxedValue (null);
2012 AssertThrows <ArgumentException> (delegate () {
2013 vm.RootDomain.CreateBoxedValue (o);
2018 public void Invoke () {
2019 Event e = run_until ("invoke1");
2021 StackFrame frame = e.Thread.GetFrames () [0];
2023 TypeMirror t = frame.Method.DeclaringType;
2024 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2026 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2032 m = t.GetMethod ("invoke_return_void");
2033 v = this_obj.InvokeMethod (e.Thread, m, null);
2037 m = t.GetMethod ("invoke_return_ref");
2038 v = this_obj.InvokeMethod (e.Thread, m, null);
2039 AssertValue ("ABC", v);
2042 m = t.GetMethod ("invoke_return_null");
2043 v = this_obj.InvokeMethod (e.Thread, m, null);
2044 AssertValue (null, v);
2047 m = t.GetMethod ("invoke_return_primitive");
2048 v = this_obj.InvokeMethod (e.Thread, m, null);
2049 AssertValue (42, v);
2052 m = t.GetMethod ("invoke_return_nullable");
2053 v = this_obj.InvokeMethod (e.Thread, m, null);
2054 Assert.IsInstanceOfType (typeof (StructMirror), v);
2055 var s = v as StructMirror;
2056 AssertValue (42, s.Fields [0]);
2057 AssertValue (true, s.Fields [1]);
2059 // pass nullable as this
2060 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2061 m = s.Type.GetMethod ("ToString");
2062 v = s.InvokeMethod (e.Thread, m, null);
2064 // return nullable null
2065 m = t.GetMethod ("invoke_return_nullable_null");
2066 v = this_obj.InvokeMethod (e.Thread, m, null);
2067 Assert.IsInstanceOfType (typeof (StructMirror), v);
2068 s = v as StructMirror;
2069 AssertValue (0, s.Fields [0]);
2070 AssertValue (false, s.Fields [1]);
2072 // pass nullable as this
2073 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2074 m = s.Type.GetMethod ("ToString");
2075 v = s.InvokeMethod (e.Thread, m, null);
2078 m = t.GetMethod ("invoke_pass_primitive");
2079 Value[] args = new Value [] {
2080 vm.CreateValue ((byte)Byte.MaxValue),
2081 vm.CreateValue ((sbyte)SByte.MaxValue),
2082 vm.CreateValue ((short)1),
2083 vm.CreateValue ((ushort)1),
2084 vm.CreateValue ((int)1),
2085 vm.CreateValue ((uint)1),
2086 vm.CreateValue ((long)1),
2087 vm.CreateValue ((ulong)1),
2088 vm.CreateValue ('A'),
2089 vm.CreateValue (true),
2090 vm.CreateValue (3.14f),
2091 vm.CreateValue (3.14) };
2093 v = this_obj.InvokeMethod (e.Thread, m, args);
2094 AssertValue ((int)Byte.MaxValue + (int)SByte.MaxValue + 1 + 1 + 1 + 1 + 1 + 1 + 'A' + 1 + 3 + 3, v);
2097 m = t.GetMethod ("invoke_pass_ref");
2098 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2099 AssertValue ("ABC", v);
2102 m = t.GetMethod ("invoke_pass_ref");
2103 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (null) });
2104 AssertValue (null, v);
2107 m = t.GetMethod ("invoke_static_pass_ref");
2108 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2109 AssertValue ("ABC", v);
2111 // static invoked using ObjectMirror.InvokeMethod
2112 m = t.GetMethod ("invoke_static_pass_ref");
2113 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2114 AssertValue ("ABC", v);
2116 // method which throws an exception
2118 m = t.GetMethod ("invoke_throws");
2119 v = this_obj.InvokeMethod (e.Thread, m, null);
2121 } catch (InvocationException ex) {
2122 Assert.AreEqual ("Exception", ex.Exception.Type.Name);
2127 m = t.GetMethod ("invoke_out");
2128 var out_task = this_obj.InvokeMethodAsyncWithResult (e.Thread, m, new Value [] { vm.CreateValue (1), vm.CreateValue (null) }, InvokeOptions.ReturnOutArgs);
2129 var out_args = out_task.Result.OutArgs;
2130 AssertValue (5, out_args [0]);
2131 Assert.IsTrue (out_args [1] is ArrayMirror);
2132 Assert.AreEqual (10, (out_args [1] as ArrayMirror).Length);
2134 // without ReturnOutArgs flag
2135 out_task = this_obj.InvokeMethodAsyncWithResult (e.Thread, m, new Value [] { vm.CreateValue (1), vm.CreateValue (null) });
2136 out_args = out_task.Result.OutArgs;
2137 Assert.IsNull (out_args);
2141 m = t.GetMethod (".ctor");
2142 v = t.InvokeMethod (e.Thread, m, null);
2143 Assert.IsInstanceOfType (typeof (ObjectMirror), v);
2144 Assert.AreEqual ("Tests", (v as ObjectMirror).Type.Name);
2147 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("ITest2");
2148 m = cl1.GetMethod ("invoke_iface");
2149 v = this_obj.InvokeMethod (e.Thread, m, null);
2150 AssertValue (42, v);
2153 m = t.BaseType.GetMethod ("virtual_method");
2154 v = this_obj.InvokeMethod (e.Thread, m, null, InvokeOptions.Virtual);
2155 AssertValue ("V2", v);
2157 // virtual call on static method
2158 m = t.GetMethod ("invoke_static_pass_ref");
2159 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") }, InvokeOptions.Virtual);
2160 AssertValue ("ABC", v);
2164 m = t.GetMethod ("invoke_pass_ref");
2165 var task = this_obj.InvokeMethodAsync (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2166 AssertValue ("ABC", task.Result);
2169 m = t.GetMethod ("invoke_static_pass_ref");
2170 task = t.InvokeMethodAsync (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2171 AssertValue ("ABC", task.Result);
2174 // Argument checking
2177 AssertThrows<ArgumentNullException> (delegate {
2178 m = t.GetMethod ("invoke_pass_ref");
2179 v = this_obj.InvokeMethod (null, m, new Value [] { vm.CreateValue (null) });
2183 AssertThrows<ArgumentNullException> (delegate {
2184 v = this_obj.InvokeMethod (e.Thread, null, new Value [] { vm.CreateValue (null) });
2187 // invalid number of arguments
2188 m = t.GetMethod ("invoke_pass_ref");
2189 AssertThrows<ArgumentException> (delegate {
2190 v = this_obj.InvokeMethod (e.Thread, m, null);
2193 // invalid type of argument (ref != primitive)
2194 m = t.GetMethod ("invoke_pass_ref");
2195 AssertThrows<ArgumentException> (delegate {
2196 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2199 // invalid type of argument (primitive != primitive)
2200 m = t.GetMethod ("invoke_pass_primitive_2");
2201 AssertThrows<ArgumentException> (delegate {
2202 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2205 // invoking a non-static method as static
2206 m = t.GetMethod ("invoke_pass_ref");
2207 AssertThrows<ArgumentException> (delegate {
2208 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2211 // invoking a method defined in another class
2212 m = t2.GetMethod ("invoke");
2213 AssertThrows<ArgumentException> (delegate {
2214 v = this_obj.InvokeMethod (e.Thread, m, null);
2219 public void InvokeVType () {
2220 Event e = run_until ("invoke1");
2222 StackFrame frame = e.Thread.GetFrames () [0];
2224 var s = frame.GetArgument (1) as StructMirror;
2226 TypeMirror t = s.Type;
2231 // Pass struct as this, receive int
2232 m = t.GetMethod ("invoke_return_int");
2233 v = s.InvokeMethod (e.Thread, m, null);
2234 AssertValue (42, v);
2236 // Pass struct as this, receive intptr
2237 m = t.GetMethod ("invoke_return_intptr");
2238 v = s.InvokeMethod (e.Thread, m, null);
2239 AssertValue (43, v);
2242 m = t.GetMethod ("invoke_static");
2243 v = t.InvokeMethod (e.Thread, m, null);
2246 // Pass generic struct as this
2247 s = frame.GetArgument (2) as StructMirror;
2249 m = t.GetMethod ("invoke_return_int");
2250 v = s.InvokeMethod (e.Thread, m, null);
2251 AssertValue (42, v);
2254 // Invoke a method which changes state
2255 s = frame.GetArgument (1) as StructMirror;
2257 m = t.GetMethod ("invoke_mutate");
2258 var task = s.InvokeMethodAsyncWithResult (e.Thread, m, null, InvokeOptions.ReturnOutThis);
2259 var out_this = task.Result.OutThis as StructMirror;
2260 AssertValue (5, out_this ["l"]);
2262 // Without the ReturnOutThis flag
2263 s = frame.GetArgument (1) as StructMirror;
2265 m = t.GetMethod ("invoke_mutate");
2266 task = s.InvokeMethodAsyncWithResult (e.Thread, m, null);
2267 out_this = task.Result.OutThis as StructMirror;
2268 Assert.AreEqual (null, out_this);
2273 public void BreakpointDuringInvoke () {
2274 Event e = run_until ("invoke1");
2276 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke2");
2277 Assert.IsNotNull (m);
2278 vm.SetBreakpoint (m, 0);
2280 StackFrame frame = e.Thread.GetFrames () [0];
2281 var o = frame.GetThis () as ObjectMirror;
2283 bool failed = false;
2285 bool finished = false;
2286 object wait = new object ();
2288 // Have to invoke in a separate thread as the invoke is suspended until we
2289 // resume after the breakpoint
2290 Thread t = new Thread (delegate () {
2292 o.InvokeMethod (e.Thread, m, null);
2298 Monitor.Pulse (wait);
2304 StackFrame invoke_frame = null;
2307 e = GetNextEvent ();
2308 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
2309 // Check stack trace support and invokes
2310 var frames = e.Thread.GetFrames ();
2311 invoke_frame = frames [0];
2312 Assert.AreEqual ("invoke2", frames [0].Method.Name);
2313 Assert.IsTrue (frames [0].IsDebuggerInvoke);
2314 Assert.AreEqual ("invoke1", frames [1].Method.Name);
2321 Monitor.Wait (wait);
2324 // Check that the invoke frames are no longer valid
2325 AssertThrows<InvalidStackFrameException> (delegate {
2326 invoke_frame.GetThis ();
2329 // Check InvokeOptions.DisableBreakpoints flag
2330 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2334 public void DisabledExceptionDuringInvoke () {
2335 Event e = run_until ("invoke_ex");
2337 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_ex_inner");
2339 StackFrame frame = e.Thread.GetFrames () [0];
2340 var o = frame.GetThis () as ObjectMirror;
2342 var req = vm.CreateExceptionRequest (null);
2345 // Check InvokeOptions.DisableBreakpoints flag
2346 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2352 public void InvokeSingleThreaded () {
2355 Start (new string [] { "dtest-app.exe", "invoke-single-threaded" });
2357 Event e = run_until ("invoke_single_threaded_2");
2359 StackFrame f = e.Thread.GetFrames ()[0];
2361 var obj = f.GetThis () as ObjectMirror;
2363 // Check that the counter value incremented by the other thread does not increase
2364 // during the invoke.
2365 object counter1 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2367 var m = obj.Type.GetMethod ("invoke_return_void");
2368 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2370 object counter2 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2372 Assert.AreEqual ((int)counter1, (int)counter2);
2374 // Test multiple invokes done in succession
2375 m = obj.Type.GetMethod ("invoke_return_void");
2376 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2378 // Test events during single-threaded invokes
2379 vm.EnableEvents (EventType.TypeLoad);
2380 m = obj.Type.GetMethod ("invoke_type_load");
2381 obj.BeginInvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded, delegate {
2385 e = GetNextEvent ();
2386 Assert.AreEqual (EventType.TypeLoad, e.EventType);
2389 List<Value> invoke_results;
2392 public void InvokeMultiple () {
2393 Event e = run_until ("invoke1");
2395 StackFrame frame = e.Thread.GetFrames () [0];
2397 TypeMirror t = frame.Method.DeclaringType;
2398 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2400 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2402 var methods = new MethodMirror [2];
2403 methods [0] = t.GetMethod ("invoke_return_ref");
2404 methods [1] = t.GetMethod ("invoke_return_primitive");
2406 invoke_results = new List<Value> ();
2408 var r = this_obj.BeginInvokeMultiple (e.Thread, methods, null, InvokeOptions.SingleThreaded, invoke_multiple_cb, this_obj);
2409 WaitHandle.WaitAll (new WaitHandle[] { r.AsyncWaitHandle });
2410 this_obj.EndInvokeMultiple (r);
2411 // The callback might still be running
2412 while (invoke_results.Count < 2) {
2415 if (invoke_results [0] is PrimitiveValue) {
2416 AssertValue ("ABC", invoke_results [1]);
2417 AssertValue (42, invoke_results [0]);
2419 AssertValue ("ABC", invoke_results [0]);
2420 AssertValue (42, invoke_results [1]);
2424 void invoke_multiple_cb (IAsyncResult ar) {
2425 ObjectMirror this_obj = (ObjectMirror)ar.AsyncState;
2427 var res = this_obj.EndInvokeMethod (ar);
2428 lock (invoke_results)
2429 invoke_results.Add (res);
2433 public void GetThreads () {
2438 public void Threads () {
2439 Event e = run_until ("threads");
2441 Assert.AreEqual (ThreadState.Running, e.Thread.ThreadState);
2443 Assert.IsTrue (e.Thread.ThreadId > 0);
2445 Assert.AreEqual (e.Thread.TID, e.Thread.TID);
2447 vm.EnableEvents (EventType.ThreadStart, EventType.ThreadDeath);
2451 e = GetNextEvent ();
2452 Assert.IsInstanceOfType (typeof (ThreadStartEvent), e);
2453 var state = e.Thread.ThreadState;
2454 Assert.IsTrue (state == ThreadState.Running || state == ThreadState.Unstarted);
2458 e = GetNextEvent ();
2459 Assert.IsInstanceOfType (typeof (ThreadDeathEvent), e);
2460 Assert.AreEqual (ThreadState.Stopped, e.Thread.ThreadState);
2464 public void Frame_SetValue () {
2465 Event e = run_until ("locals2");
2467 StackFrame frame = e.Thread.GetFrames () [0];
2470 var l = frame.Method.GetLocal ("i");
2471 frame.SetValue (l, vm.CreateValue ((long)55));
2472 AssertValue (55, frame.GetValue (l));
2475 l = frame.Method.GetLocal ("s");
2476 frame.SetValue (l, vm.RootDomain.CreateString ("DEF"));
2477 AssertValue ("DEF", frame.GetValue (l));
2479 // argument as local
2480 l = frame.Method.GetLocal ("arg");
2481 frame.SetValue (l, vm.CreateValue (6));
2482 AssertValue (6, frame.GetValue (l));
2485 var p = frame.Method.GetParameters ()[1];
2486 frame.SetValue (p, vm.CreateValue (7));
2487 AssertValue (7, frame.GetValue (p));
2490 p = frame.Method.GetParameters ()[2];
2491 frame.SetValue (p, vm.RootDomain.CreateString ("DEF"));
2492 AssertValue ("DEF", frame.GetValue (p));
2495 p = frame.Method.GetParameters ()[3];
2496 frame.SetValue (p, vm.RootDomain.CreateString ("DEF2"));
2497 AssertValue ("DEF2", frame.GetValue (p));
2500 p = frame.Method.GetParameters ()[4];
2501 var v = frame.GetValue (p) as StructMirror;
2502 v ["i"] = vm.CreateValue (43);
2503 frame.SetValue (p, v);
2504 v = frame.GetValue (p) as StructMirror;
2505 AssertValue (43, v ["i"]);
2507 // argument checking
2510 AssertThrows<ArgumentNullException> (delegate () {
2511 frame.SetValue ((LocalVariable)null, vm.CreateValue (55));
2515 AssertThrows<ArgumentNullException> (delegate () {
2516 l = frame.Method.GetLocal ("i");
2517 frame.SetValue (l, null);
2520 // value of invalid type
2521 AssertThrows<ArgumentException> (delegate () {
2522 l = frame.Method.GetLocal ("i");
2523 frame.SetValue (l, vm.CreateValue (55));
2529 public void Frame_SetValue_Registers () {
2530 Event e = run_until ("locals6_1");
2532 StackFrame frame = e.Thread.GetFrames () [1];
2535 var l = frame.Method.GetLocal ("j");
2536 frame.SetValue (l, vm.CreateValue (99));
2537 AssertValue (99, frame.GetValue (l));
2539 // Check it during execution
2540 e = run_until ("locals6_2");
2541 frame = e.Thread.GetFrames () [0];
2542 AssertValue (99, frame.GetValue (frame.Method.GetParameters ()[0]));
2544 // Set it while in a frame which clobbers its register
2545 e = run_until ("locals6_3");
2546 frame = e.Thread.GetFrames () [1];
2547 frame.SetValue (l, vm.CreateValue (100));
2548 AssertValue (100, frame.GetValue (l));
2550 // Check it during execution
2551 e = run_until ("locals6_4");
2552 frame = e.Thread.GetFrames () [0];
2553 AssertValue (100, frame.GetValue (frame.Method.GetParameters ()[0]));
2555 // Signed byte value
2556 e = run_until ("locals6_5");
2557 frame = e.Thread.GetFrames () [1];
2558 var l2 = frame.Method.GetLocal ("sb");
2559 frame.SetValue (l2, vm.CreateValue ((sbyte)-99));
2560 AssertValue (-99, frame.GetValue (l2));
2562 // Check it during execution
2563 e = run_until ("locals6_6");
2564 frame = e.Thread.GetFrames () [0];
2565 AssertValue (-99, frame.GetValue (frame.Method.GetParameters ()[0]));
2569 public void InvokeRegress () {
2570 Event e = run_until ("invoke1");
2572 StackFrame frame = e.Thread.GetFrames () [0];
2574 TypeMirror t = frame.Method.DeclaringType;
2575 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2577 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2583 m = t.GetMethod ("invoke_return_void");
2584 v = this_obj.InvokeMethod (e.Thread, m, null);
2587 // Check that the stack frames remain valid during the invoke
2588 Assert.AreEqual ("Tests", (frame.GetThis () as ObjectMirror).Type.Name);
2590 // do another invoke
2591 m = t.GetMethod ("invoke_return_void");
2592 v = this_obj.InvokeMethod (e.Thread, m, null);
2595 // Try a single step after the invoke
2596 var req = create_step (e);
2597 req.Depth = StepDepth.Into;
2598 req.Size = StepSize.Line;
2604 // Step into invoke2
2606 e = GetNextEvent ();
2607 Assert.IsTrue (e is StepEvent);
2608 Assert.AreEqual ("invoke2", (e as StepEvent).Method.Name);
2612 frame = e.Thread.GetFrames () [0];
2616 public void Exceptions () {
2617 Event e = run_until ("exceptions");
2618 var req = vm.CreateExceptionRequest (null);
2623 e = GetNextEvent ();
2624 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2625 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2627 var frames = e.Thread.GetFrames ();
2628 Assert.AreEqual ("exceptions", frames [0].Method.Name);
2631 // exception type filter
2633 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.ArgumentException"));
2636 // Skip the throwing of the second OverflowException
2639 e = GetNextEvent ();
2640 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2641 Assert.AreEqual ("ArgumentException", (e as ExceptionEvent).Exception.Type.Name);
2644 // exception type filter for subclasses
2645 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.Exception"));
2650 e = GetNextEvent ();
2651 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2652 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2656 req.IncludeSubclasses = false;
2661 e = GetNextEvent ();
2662 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2663 Assert.AreEqual ("Exception", (e as ExceptionEvent).Exception.Type.Name);
2666 // Implicit exceptions
2667 req = vm.CreateExceptionRequest (null);
2672 e = GetNextEvent ();
2673 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2674 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
2677 // Single stepping after an exception
2678 req = vm.CreateExceptionRequest (null);
2683 e = GetNextEvent ();
2684 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2685 Assert.AreEqual ("Exception", (e as ExceptionEvent).Exception.Type.Name);
2686 frames = e.Thread.GetFrames ();
2687 Assert.AreEqual ("exceptions2", frames [0].Method.Name);
2690 var sreq = create_step (e);
2691 sreq.Depth = StepDepth.Over;
2692 sreq.Size = StepSize.Line;
2696 e = GetNextEvent ();
2697 Assert.IsInstanceOfType (typeof (StepEvent), e);
2698 frames = e.Thread.GetFrames ();
2699 Assert.AreEqual ("exceptions", frames [0].Method.Name);
2702 // Argument checking
2703 AssertThrows<ArgumentException> (delegate {
2704 vm.CreateExceptionRequest (e.Thread.Type);
2709 public void ExceptionFilter () {
2710 Event e = run_until ("exception_filter");
2712 MethodMirror m = entry_point.DeclaringType.GetMethod ("exception_filter_filter");
2713 Assert.IsNotNull (m);
2715 vm.SetBreakpoint (m, 0);
2719 e = GetNextEvent ();
2720 Assert.AreEqual (EventType.Breakpoint, e.EventType);
2721 Assert.IsTrue (e is BreakpointEvent);
2722 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
2724 var frames = e.Thread.GetFrames ();
2726 Assert.IsTrue (frames [0].Location.SourceFile.IndexOf ("dtest-app.cs") != -1);
2727 Assert.AreEqual ("exception_filter_filter", frames [0].Location.Method.Name);
2729 Assert.AreEqual (0, frames [1].Location.Method.MetadataToken);
2730 Assert.AreEqual (0x0f, frames [1].Location.ILOffset);
2732 Assert.AreEqual ("exception_filter_method", frames [2].Location.Method.Name);
2733 Assert.AreEqual (0x06, frames [2].Location.ILOffset);
2735 Assert.AreEqual (0, frames [3].Location.Method.MetadataToken, 0);
2736 Assert.AreEqual (0, frames [3].Location.ILOffset);
2738 Assert.AreEqual ("exception_filter", frames [4].Location.Method.Name);
2742 public void ExceptionFilter2 () {
2745 Start (new string [] { "dtest-excfilter.exe" });
2747 MethodMirror filter_method = entry_point.DeclaringType.GetMethod ("Filter");
2748 Assert.IsNotNull (filter_method);
2750 MethodMirror test_method = entry_point.DeclaringType.GetMethod ("Test");
2751 Assert.IsNotNull (test_method);
2753 vm.SetBreakpoint (filter_method, 0);
2757 var e = GetNextEvent ();
2758 Assert.AreEqual (EventType.Breakpoint, e.EventType);
2759 Assert.IsTrue (e is BreakpointEvent);
2760 Assert.AreEqual (filter_method.Name, (e as BreakpointEvent).Method.Name);
2762 var frames = e.Thread.GetFrames ();
2764 Assert.AreEqual (4, frames.Count ());
2766 Assert.AreEqual (filter_method.Name, frames [0].Location.Method.Name);
2767 Assert.AreEqual (20, frames [0].Location.LineNumber);
2768 Assert.AreEqual (0, frames [0].Location.ILOffset);
2770 Assert.AreEqual (test_method.Name, frames [1].Location.Method.Name);
2771 Assert.AreEqual (37, frames [1].Location.LineNumber);
2772 Assert.AreEqual (0x0b, frames [1].Location.ILOffset);
2774 Assert.AreEqual (test_method.Name, frames [2].Location.Method.Name);
2775 Assert.AreEqual (33, frames [2].Location.LineNumber);
2776 Assert.AreEqual (0x05, frames [2].Location.ILOffset);
2778 Assert.AreEqual (entry_point.Name, frames [3].Location.Method.Name);
2779 Assert.AreEqual (14, frames [3].Location.LineNumber);
2780 Assert.AreEqual (0x00, frames [3].Location.ILOffset);
2788 public void EventSets () {
2790 // Create two filter which both match the same exception
2792 Event e = run_until ("exceptions");
2794 var req = vm.CreateExceptionRequest (null);
2797 var req2 = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.OverflowException"));
2802 var es = vm.GetNextEventSet ();
2803 Assert.AreEqual (2, es.Events.Length);
2806 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2807 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2810 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2811 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2818 // Test single threaded invokes during processing of nullref exceptions.
2819 // These won't work if the exception handling is done from the sigsegv signal
2820 // handler, since the sigsegv signal is disabled until control returns from the
2824 [Category ("only3")]
2825 public void NullRefExceptionAndSingleThreadedInvoke () {
2826 Event e = run_until ("exceptions");
2827 var req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.NullReferenceException"));
2832 e = GetNextEvent ();
2833 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2834 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
2836 var ex = (e as ExceptionEvent).Exception;
2837 var tostring_method = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2838 ex.InvokeMethod (e.Thread, tostring_method, null, InvokeOptions.SingleThreaded);
2842 public void Domains () {
2845 Start (new string [] { "dtest-app.exe", "domain-test" });
2847 vm.EnableEvents (EventType.AppDomainCreate, EventType.AppDomainUnload, EventType.AssemblyUnload);
2849 Event e = run_until ("domains");
2853 e = GetNextEvent ();
2854 Assert.IsInstanceOfType (typeof (AppDomainCreateEvent), e);
2856 var domain = (e as AppDomainCreateEvent).Domain;
2858 // Check the object type
2859 e = run_until ("domains_2");
2860 var frame = e.Thread.GetFrames ()[0];
2861 var o = frame.GetArgument (0) as ObjectMirror;
2862 Assert.AreEqual ("CrossDomain", o.Type.Name);
2864 // Do a remoting invoke
2865 var cross_domain_type = o.Type;
2866 var v = o.InvokeMethod (e.Thread, cross_domain_type.GetMethod ("invoke_3"), null);
2867 AssertValue (42, v);
2869 // Run until the callback in the domain
2870 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_in_domain");
2871 Assert.IsNotNull (m);
2872 vm.SetBreakpoint (m, 0);
2876 e = GetNextEvent ();
2877 if (e is BreakpointEvent)
2881 Assert.AreEqual ("invoke_in_domain", (e as BreakpointEvent).Method.Name);
2883 // d_method is from another domain
2884 MethodMirror d_method = (e as BreakpointEvent).Method;
2885 Assert.IsTrue (m != d_method);
2887 var frames = e.Thread.GetFrames ();
2888 Assert.AreEqual ("invoke_in_domain", frames [0].Method.Name);
2889 Assert.AreEqual (domain, frames [0].Domain);
2890 Assert.AreEqual ("invoke", frames [1].Method.Name);
2891 Assert.AreEqual ("domains", frames [2].Method.Name);
2892 Assert.AreEqual (vm.RootDomain, frames [2].Domain);
2894 // Test breakpoints on already JITted methods in other domains
2895 m = entry_point.DeclaringType.GetMethod ("invoke_in_domain_2");
2896 Assert.IsNotNull (m);
2897 vm.SetBreakpoint (m, 0);
2901 e = GetNextEvent ();
2902 if (e is BreakpointEvent)
2906 Assert.AreEqual ("invoke_in_domain_2", (e as BreakpointEvent).Method.Name);
2908 // This is empty when receiving the AppDomainCreateEvent
2909 Assert.AreEqual ("domain", domain.FriendlyName);
2911 // Run until the unload
2914 e = GetNextEvent ();
2915 if (e is AssemblyUnloadEvent) {
2921 Assert.IsInstanceOfType (typeof (AppDomainUnloadEvent), e);
2922 Assert.AreEqual (domain, (e as AppDomainUnloadEvent).Domain);
2924 // Run past the unload
2925 e = run_until ("domains_3");
2927 // Test access to unloaded types
2928 // FIXME: Add an exception type for this
2929 AssertThrows<Exception> (delegate {
2930 d_method.DeclaringType.GetValue (d_method.DeclaringType.GetField ("static_i"));
2933 // Check that .Domain is accessible for stack frames with native transitions
2934 e = run_until ("called_from_invoke");
2935 ThreadMirror.NativeTransitions = true;
2936 foreach (var f in e.Thread.GetFrames ()) {
2942 public void DynamicMethods () {
2943 Event e = run_until ("dyn_call");
2945 var m = e.Thread.GetFrames ()[1].Method;
2946 Assert.AreEqual ("dyn_method", m.Name);
2948 // Test access to IL
2949 var body = m.GetMethodBody ();
2951 ILInstruction ins = body.Instructions [0];
2952 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
2953 Assert.AreEqual ("FOO", ins.Operand);
2957 public void RefEmit () {
2960 Start (new string [] { "dtest-app.exe", "ref-emit-test" });
2962 Event e = run_until ("ref_emit_call");
2964 var m = e.Thread.GetFrames ()[1].Method;
2965 Assert.AreEqual ("ref_emit_method", m.Name);
2967 // Test access to IL
2968 var body = m.GetMethodBody ();
2972 ins = body.Instructions [0];
2973 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
2974 Assert.AreEqual ("FOO", ins.Operand);
2976 ins = body.Instructions [1];
2977 Assert.AreEqual (OpCodes.Call, ins.OpCode);
2978 Assert.IsInstanceOfType (typeof (MethodMirror), ins.Operand);
2979 Assert.AreEqual ("ref_emit_call", (ins.Operand as MethodMirror).Name);
2983 public void IsAttached () {
2984 var f = entry_point.DeclaringType.GetField ("is_attached");
2986 Event e = run_until ("Main");
2988 AssertValue (true, entry_point.DeclaringType.GetValue (f));
2992 public void StackTraceInNative () {
2993 // Check that stack traces can be produced for threads in native code
2996 Start (new string [] { "dtest-app.exe", "frames-in-native" });
2998 var e = run_until ("frames_in_native");
3000 // FIXME: This is racy
3007 StackFrame[] frames = e.Thread.GetFrames ();
3009 int frame_index = -1;
3010 for (int i = 0; i < frames.Length; ++i) {
3011 if (frames [i].Method.Name == "Sleep") {
3017 Assert.IsTrue (frame_index != -1);
3018 Assert.AreEqual ("Sleep", frames [frame_index].Method.Name);
3019 Assert.AreEqual ("frames_in_native", frames [frame_index + 1].Method.Name);
3020 Assert.AreEqual ("Main", frames [frame_index + 2].Method.Name);
3022 // Check that invokes are disabled for such threads
3023 TypeMirror t = frames [frame_index + 1].Method.DeclaringType;
3025 var m = t.GetMethod ("invoke_static_return_void");
3026 AssertThrows<InvalidOperationException> (delegate {
3027 t.InvokeMethod (e.Thread, m, null);
3030 // Check that the frame info is invalidated
3031 run_until ("frames_in_native_2");
3033 AssertThrows<InvalidStackFrameException> (delegate {
3034 Console.WriteLine (frames [frame_index].GetThis ());
3039 public void VirtualMachine_CreateEnumMirror () {
3040 var e = run_until ("o1");
3041 var frame = e.Thread.GetFrames () [0];
3043 object val = frame.GetThis ();
3044 Assert.IsTrue (val is ObjectMirror);
3045 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
3046 ObjectMirror o = (val as ObjectMirror);
3048 FieldInfoMirror field = o.Type.GetField ("field_enum");
3049 Value f = o.GetValue (field);
3050 TypeMirror enumType = (f as EnumMirror).Type;
3052 o.SetValue (field, vm.CreateEnumMirror (enumType, vm.CreateValue (1)));
3053 f = o.GetValue (field);
3054 Assert.AreEqual (1, (f as EnumMirror).Value);
3056 // Argument checking
3057 AssertThrows<ArgumentNullException> (delegate () {
3058 vm.CreateEnumMirror (enumType, null);
3061 AssertThrows<ArgumentNullException> (delegate () {
3062 vm.CreateEnumMirror (null, vm.CreateValue (1));
3066 AssertThrows<ArgumentException> (delegate () {
3067 vm.CreateEnumMirror (enumType, vm.CreateValue (null));
3070 // value of a wrong type
3071 AssertThrows<ArgumentException> (delegate () {
3072 vm.CreateEnumMirror (enumType, vm.CreateValue ((long)1));
3077 public void VirtualMachine_EnableEvents_Breakpoint () {
3078 AssertThrows<ArgumentException> (delegate () {
3079 vm.EnableEvents (EventType.Breakpoint);
3084 public void SingleStepRegress654694 () {
3087 MethodMirror m = entry_point.DeclaringType.GetMethod ("ss_regress_654694");
3088 foreach (Location l in m.Locations) {
3089 if (l.ILOffset > 0 && il_offset == -1)
3090 il_offset = l.ILOffset;
3093 Event e = run_until ("ss_regress_654694");
3095 Assert.IsNotNull (m);
3096 vm.SetBreakpoint (m, il_offset);
3100 e = GetNextEvent ();
3101 Assert.IsTrue (e is BreakpointEvent);
3103 var req = create_step (e);
3104 req.Depth = StepDepth.Over;
3105 req.Size = StepSize.Line;
3110 e = GetNextEvent ();
3111 Assert.IsTrue (e is StepEvent);
3117 public void DebugBreak () {
3118 vm.EnableEvents (EventType.UserBreak);
3123 var e = GetNextEvent ();
3124 Assert.IsTrue (e is UserBreakEvent);
3128 public void DebugLog () {
3129 vm.EnableEvents (EventType.UserLog);
3134 var e = GetNextEvent ();
3135 Assert.IsTrue (e is UserLogEvent);
3136 var le = e as UserLogEvent;
3138 Assert.AreEqual (5, le.Level);
3139 Assert.AreEqual ("A", le.Category);
3140 Assert.AreEqual ("B", le.Message);
3144 public void TypeGetMethodsByNameFlags () {
3146 var assembly = entry_point.DeclaringType.Assembly;
3147 var type = assembly.GetType ("Tests3");
3149 Assert.IsNotNull (type);
3151 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Public, false);
3152 Assert.AreEqual (1, mm.Length, "#1");
3153 Assert.AreEqual ("M1", mm[0].Name, "#2");
3155 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.NonPublic, false);
3156 Assert.AreEqual (1, mm.Length, "#3");
3157 Assert.AreEqual ("M2", mm[0].Name, "#4");
3159 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public, false);
3160 Assert.AreEqual (7, mm.Length, "#5"); //M3 plus Equals, GetHashCode, GetType, ToString, .ctor
3162 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, false);
3163 Assert.AreEqual (2, mm.Length, "#7");
3165 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
3166 Assert.AreEqual (1, mm.Length, "#9");
3168 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
3169 Assert.AreEqual (5, mm.Length, "#11");
3172 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Public, false);
3173 Assert.AreEqual (1, mm.Length, "#12");
3174 Assert.AreEqual ("M1", mm[0].Name, "#13");
3176 mm = type.GetMethodsByNameFlags ("m1", BindingFlags.Static | BindingFlags.Public, true);
3177 Assert.AreEqual (1, mm.Length, "#14");
3178 Assert.AreEqual ("M1", mm[0].Name, "#15");
3180 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, false);
3181 Assert.AreEqual (1, mm.Length, "#16");
3182 Assert.AreEqual ("M1", mm[0].Name, "#17");
3186 [Category ("only88")]
3187 public void TypeLoadSourceFileFilter () {
3188 Event e = run_until ("type_load");
3190 if (!vm.Version.AtLeast (2, 7))
3193 string srcfile = (e as BreakpointEvent).Method.DeclaringType.GetSourceFiles (true)[0];
3195 var req = vm.CreateTypeLoadRequest ();
3196 req.SourceFileFilter = new string [] { srcfile.ToUpper () };
3200 e = GetNextEvent ();
3201 Assert.IsTrue (e is TypeLoadEvent);
3202 Assert.AreEqual ("TypeLoadClass", (e as TypeLoadEvent).Type.FullName);
3206 public void TypeLoadTypeNameFilter () {
3207 Event e = run_until ("type_load");
3209 var req = vm.CreateTypeLoadRequest ();
3210 req.TypeNameFilter = new string [] { "TypeLoadClass2" };
3214 e = GetNextEvent ();
3215 Assert.IsTrue (e is TypeLoadEvent);
3216 Assert.AreEqual ("TypeLoadClass2", (e as TypeLoadEvent).Type.FullName);
3220 public void GetTypesForSourceFile () {
3223 var types = vm.GetTypesForSourceFile ("dtest-app.cs", false);
3224 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
3225 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
3227 types = vm.GetTypesForSourceFile ("DTEST-app.cs", true);
3228 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
3229 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
3233 public void GetTypesNamed () {
3236 var types = vm.GetTypes ("Tests", false);
3237 Assert.AreEqual (1, types.Count);
3238 Assert.AreEqual ("Tests", types [0].FullName);
3240 types = vm.GetTypes ("System.Exception", false);
3241 Assert.AreEqual (1, types.Count);
3242 Assert.AreEqual ("System.Exception", types [0].FullName);
3246 public void String_GetChars () {
3250 var e = run_until ("arg2");
3252 var frame = e.Thread.GetFrames () [0];
3254 val = frame.GetArgument (0);
3255 Assert.IsTrue (val is StringMirror);
3256 AssertValue ("FOO", val);
3257 var s = (val as StringMirror);
3258 Assert.AreEqual (3, s.Length);
3260 var c = s.GetChars (0, 2);
3261 Assert.AreEqual (2, c.Length);
3262 Assert.AreEqual ('F', c [0]);
3263 Assert.AreEqual ('O', c [1]);
3265 AssertThrows<ArgumentException> (delegate () {
3271 public void GetInterfaces () {
3272 var e = run_until ("arg2");
3274 var frame = e.Thread.GetFrames () [0];
3276 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
3277 var ifaces = cl1.GetInterfaces ();
3278 Assert.AreEqual (1, ifaces.Length);
3279 Assert.AreEqual ("ITest", ifaces [0].Name);
3281 var cl2 = cl1.GetMethod ("Baz").ReturnType;
3282 var ifaces2 = cl2.GetInterfaces ();
3283 Assert.AreEqual (1, ifaces2.Length);
3284 Assert.AreEqual ("ITest`1", ifaces2 [0].Name);
3288 public void GetInterfaceMap () {
3289 var e = run_until ("arg2");
3291 var frame = e.Thread.GetFrames () [0];
3293 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
3294 var iface = cl1.Assembly.GetType ("ITest");
3295 var map = cl1.GetInterfaceMap (iface);
3296 Assert.AreEqual (cl1, map.TargetType);
3297 Assert.AreEqual (iface, map.InterfaceType);
3298 Assert.AreEqual (2, map.InterfaceMethods.Length);
3299 Assert.AreEqual (2, map.TargetMethods.Length);
3303 public void StackAlloc_Breakpoints_Regress2775 () {
3304 // Check that breakpoints on arm don't overwrite stackalloc-ed memory
3305 var e = run_until ("regress_2755");
3307 var frame = e.Thread.GetFrames () [0];
3309 // This breaks at the call site
3310 vm.SetBreakpoint (m, m.Locations [2].ILOffset);
3313 var e2 = GetNextEvent ();
3314 Assert.IsTrue (e2 is BreakpointEvent);
3316 e = run_until ("regress_2755_3");
3317 frame = e.Thread.GetFrames () [1];
3318 var res = frame.GetValue (m.GetLocal ("sum"));
3319 AssertValue (0, res);
3323 public void MethodInfo () {
3324 Event e = run_until ("locals2");
3326 StackFrame frame = e.Thread.GetFrames () [0];
3327 var m = frame.Method;
3329 Assert.IsTrue (m.IsGenericMethod);
3330 Assert.IsFalse (m.IsGenericMethodDefinition);
3332 var args = m.GetGenericArguments ();
3333 Assert.AreEqual (1, args.Length);
3334 Assert.AreEqual ("String", args [0].Name);
3336 var gmd = m.GetGenericMethodDefinition ();
3337 Assert.IsTrue (gmd.IsGenericMethod);
3338 Assert.IsTrue (gmd.IsGenericMethodDefinition);
3339 Assert.AreEqual (gmd, gmd.GetGenericMethodDefinition ());
3341 args = gmd.GetGenericArguments ();
3342 Assert.AreEqual (1, args.Length);
3343 Assert.AreEqual ("T", args [0].Name);
3345 var attrs = m.GetCustomAttributes (true);
3346 Assert.AreEqual (1, attrs.Length);
3347 Assert.AreEqual ("StateMachineAttribute", attrs [0].Constructor.DeclaringType.Name);
3351 public void UnhandledException () {
3354 Start (new string [] { "dtest-app.exe", "unhandled-exception" });
3356 var req = vm.CreateExceptionRequest (null, false, true);
3359 var e = run_until ("unhandled_exception");
3362 var e2 = GetNextEvent ();
3363 Assert.IsTrue (e2 is ExceptionEvent);
3370 public void UnhandledException_2 () {
3373 Start (new string [] { "dtest-app.exe", "unhandled-exception-endinvoke" });
3375 var req = vm.CreateExceptionRequest (null, false, true);
3378 MethodMirror m = entry_point.DeclaringType.GetMethod ("unhandled_exception_endinvoke_2");
3379 Assert.IsNotNull (m);
3380 vm.SetBreakpoint (m, m.ILOffsets [0]);
3382 var e = run_until ("unhandled_exception_endinvoke");
3385 var e2 = GetNextEvent ();
3386 Assert.IsFalse (e2 is ExceptionEvent);
3394 public void UnhandledExceptionUserCode () {
3397 // Exceptions caught in non-user code are treated as unhandled
3398 Start (new string [] { "dtest-app.exe", "unhandled-exception-user" });
3400 var req = vm.CreateExceptionRequest (null, false, true);
3401 req.AssemblyFilter = new List<AssemblyMirror> () { entry_point.DeclaringType.Assembly };
3404 var e = run_until ("unhandled_exception_user");
3407 var e2 = GetNextEvent ();
3408 Assert.IsTrue (e2 is ExceptionEvent);
3416 public void GCWhileSuspended () {
3417 // Check that objects are kept alive during suspensions
3418 Event e = run_until ("gc_suspend_1");
3420 MethodMirror m = entry_point.DeclaringType.GetMethod ("gc_suspend_invoke");
3422 var o = entry_point.DeclaringType.GetValue (entry_point.DeclaringType.GetField ("gc_suspend_field")) as ObjectMirror;
3423 //Console.WriteLine (o);
3425 StackFrame frame = e.Thread.GetFrames () [0];
3426 TypeMirror t = frame.Method.DeclaringType;
3427 for (int i = 0; i < 10; ++i)
3428 t.InvokeMethod (e.Thread, m, new Value [] { });
3430 // This throws an exception if the object is collected
3431 long addr = o.Address;
3433 var o2 = entry_point.DeclaringType.GetValue (entry_point.DeclaringType.GetField ("gc_suspend_field")) as ObjectMirror;
3438 public void MakeGenericMethod () {
3439 Event e = run_until ("bp1");
3441 var intm = vm.RootDomain.GetCorrespondingType (typeof (int));
3442 var stringm = vm.RootDomain.GetCorrespondingType (typeof (string));
3443 var gm = entry_point.DeclaringType.GetMethod ("generic_method");
3444 var res = gm.MakeGenericMethod (new TypeMirror [] { stringm });
3445 var args = res.GetGenericArguments ();
3446 Assert.AreEqual (1, args.Length);
3447 Assert.AreEqual (stringm, args [0]);
3450 AssertThrows<ArgumentNullException> (delegate {
3451 gm.MakeGenericMethod (null);
3453 AssertThrows<ArgumentNullException> (delegate {
3454 gm.MakeGenericMethod (new TypeMirror [] { null });
3456 AssertThrows<ArgumentException> (delegate {
3457 gm.MakeGenericMethod (new TypeMirror [] { stringm, stringm });
3459 AssertThrows<InvalidOperationException> (delegate {
3460 gm.MakeGenericMethod (new TypeMirror [] { intm });
3462 AssertThrows<InvalidOperationException> (delegate {
3463 entry_point.DeclaringType.GetMethod ("Main").MakeGenericMethod (new TypeMirror [] { intm });
3468 public void InspectThreadSuspenedOnWaitOne () {
3470 Start (true, "dtest-app.exe", "wait-one" );
3472 ThreadMirror.NativeTransitions = true;
3474 var evt = run_until ("wait_one");
3475 Assert.IsNotNull (evt, "#1");
3477 var thread = evt.Thread;
3478 Assert.AreEqual (ThreadState.Running, thread.ThreadState, "#1.1");
3480 var frames = thread.GetFrames ();
3481 Assert.IsNotNull (frames, "#2");
3482 Assert.AreEqual (2, frames.Length, "#3");
3483 Assert.AreEqual ("wait_one", frames [0].Method.Name, "#4");
3484 Assert.AreEqual ("Main", frames [1].Method.Name, "#5");
3488 Thread.Sleep (500); //FIXME this is racy, maybe single step? or something?
3491 Assert.AreEqual (ThreadState.WaitSleepJoin, thread.ThreadState, "#6");
3493 frames = thread.GetFrames ();
3494 Assert.AreEqual (4, frames.Length, "#7");
3495 Assert.AreEqual ("WaitOne_internal", frames [0].Method.Name, "#8");
3496 Assert.AreEqual ("WaitOne", frames [1].Method.Name, "#8.1");
3497 Assert.AreEqual ("wait_one", frames [2].Method.Name, "#9");
3498 Assert.AreEqual ("Main", frames [3].Method.Name, "#10");
3501 var frame = frames [0];
3502 Assert.IsTrue (frame.IsNativeTransition, "#11.1");
3505 Assert.Fail ("Known limitation - can't get info from m2n frames");
3506 } catch (AbsentInformationException) {}
3509 Assert.IsFalse (frame.IsNativeTransition, "#12.1");
3510 var wait_one_this = frame.GetThis ();
3511 Assert.IsNotNull (wait_one_this, "#12.2");
3514 var locals = frame.GetVisibleVariables ();
3515 Assert.AreEqual (1, locals.Count, "#13.1");
3517 var local_0 = frame.GetValue (locals [0]);
3518 Assert.IsNotNull (local_0, "#13.2");
3520 Assert.AreEqual (wait_one_this, local_0, "#14.2");
3524 public void GetMethodBody () {
3525 var bevent = run_until ("Main");
3527 var m = bevent.Method.DeclaringType.GetMethod ("get_IntProperty");
3528 var body = m.GetMethodBody ();
3529 foreach (var ins in body.Instructions) {
3530 if (ins.OpCode == OpCodes.Ldfld) {
3531 var field = (FieldInfoMirror)ins.Operand;
3532 Assert.AreEqual ("field_i", field.Name);
3538 public void EvaluateMethod () {
3539 var bevent = run_until ("evaluate_method_2");
3541 var m = bevent.Method.DeclaringType.GetMethod ("get_IntProperty");
3543 var this_obj = bevent.Thread.GetFrames ()[0].GetThis ();
3544 var v = m.Evaluate (this_obj, null);
3545 AssertValue (42, v);
3549 public void SetIP () {
3550 var bevent = run_until ("set_ip_1");
3552 var invalid_loc = bevent.Thread.GetFrames ()[0].Location;
3554 var req = create_step (bevent);
3555 var e = step_out ();
3557 var frames = e.Thread.GetFrames ();
3558 var locs = frames [0].Method.Locations;
3559 var next_loc = locs.First (l => (l.LineNumber == frames [0].Location.LineNumber + 2));
3561 e.Thread.SetIP (next_loc);
3563 /* Check that i ++; j = 5; was skipped */
3564 bevent = run_until ("set_ip_2");
3565 var f = bevent.Thread.GetFrames ()[1];
3566 AssertValue (2, f.GetValue (f.Method.GetLocal ("i")));
3567 AssertValue (0, f.GetValue (f.Method.GetLocal ("j")));
3570 AssertThrows<ArgumentNullException> (delegate {
3571 e.Thread.SetIP (null);
3574 AssertThrows<ArgumentException> (delegate {
3575 e.Thread.SetIP (invalid_loc);
3580 public void SetIPSingleStep () {
3581 // Check that single stepping after set-ip steps from the new ip
3582 var bevent = run_until ("set_ip_1");
3584 var invalid_loc = bevent.Thread.GetFrames ()[0].Location;
3586 var req = create_step (bevent);
3587 req.Size = StepSize.Line;
3588 var e = step_out ();
3590 var frames = e.Thread.GetFrames ();
3591 var locs = frames [0].Method.Locations;
3592 var prev_loc = locs.First (l => (l.LineNumber == frames [0].Location.LineNumber - 3));
3593 AssertValue (2, frames [0].GetValue (frames [0].Method.GetLocal ("i")));
3595 // Set back the ip to the first i ++; line
3596 e.Thread.SetIP (prev_loc);
3599 var f = e.Thread.GetFrames ()[0];
3600 AssertValue (3, f.GetValue (f.Method.GetLocal ("i")));
3604 public void NewInstanceNoCtor () {
3605 var bevent = run_until ("Main");
3607 var stype = bevent.Method.DeclaringType.Assembly.GetType ("AStruct");
3608 var obj = stype.NewInstance ();
3609 Assert.IsTrue (obj is ObjectMirror);
3610 Assert.AreEqual ("AStruct", (obj as ObjectMirror).Type.Name);
3614 public void StaticCtorFilterInCctor () {
3615 // Check that single stepping when in a cctor only ignores
3616 // other cctors, not the current one
3617 var bevent = run_until ("step_filters");
3619 var assembly = entry_point.DeclaringType.Assembly;
3620 var type = assembly.GetType ("Tests/ClassWithCctor");
3621 var cctor = type.GetMethod (".cctor");
3622 vm.SetBreakpoint (cctor, 0);
3625 var e = vm.GetNextEvent ();
3626 Assert.IsTrue (e is BreakpointEvent);
3628 var req = create_step (e);
3629 req.Filter = StepFilter.StaticCtor;
3631 // Make sure we are still in the cctor
3632 Assert.AreEqual (".cctor", e.Thread.GetFrames ()[0].Location.Method.Name);