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 // Not currently used, but can be useful when debugging individual tests.
46 void StackTraceDump (Event e)
49 foreach (var frame in e.Thread.GetFrames ())
52 Console.WriteLine ("Frame " + i + ", " + frame.Method.Name);
56 Event GetNextEvent () {
57 var es = vm.GetNextEventSet ();
58 Assert.AreEqual (1, es.Events.Length);
62 void Start (params string[] args) {
66 void Start (bool forceExit, params string[] args) {
67 this.forceExit = forceExit;
70 var pi = new Diag.ProcessStartInfo ();
72 if (runtime != null) {
73 pi.FileName = runtime;
74 } else if (Path.DirectorySeparatorChar == '\\') {
75 string processExe = Diag.Process.GetCurrentProcess ().MainModule.FileName;
76 if (processExe != null) {
77 string fileName = Path.GetFileName (processExe);
78 if (fileName.StartsWith ("mono") && fileName.EndsWith (".exe"))
79 pi.FileName = processExe;
82 if (string.IsNullOrEmpty (pi.FileName))
84 pi.Arguments = String.Join (" ", args);
85 vm = VirtualMachineManager.Launch (pi, new LaunchOptions { AgentArgs = agent_args });
87 var ep = new IPEndPoint (IPAddress.Any, 10000);
88 Console.WriteLine ("Listening on " + ep + "...");
89 vm = VirtualMachineManager.Listen (ep);
92 var load_req = vm.CreateAssemblyLoadRequest ();
95 Event vmstart = GetNextEvent ();
96 Assert.AreEqual (EventType.VMStart, vmstart.EventType);
105 /* Find out the entry point */
109 if (e is AssemblyLoadEvent) {
110 AssemblyLoadEvent ae = (AssemblyLoadEvent)e;
111 entry_point = ae.Assembly.EntryPoint;
112 if (entry_point != null)
122 BreakpointEvent run_until (string name) {
124 MethodMirror m = entry_point.DeclaringType.GetMethod (name);
125 Assert.IsNotNull (m);
126 //Console.WriteLine ("X: " + name + " " + m.ILOffsets.Count + " " + m.Locations.Count);
127 var req = vm.SetBreakpoint (m, m.ILOffsets [0]);
134 if (e is BreakpointEvent)
140 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
141 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
143 return (e as BreakpointEvent);
146 class ReusableBreakpoint {
148 public string method_name;
149 public BreakpointEventRequest req;
150 public BreakpointEvent lastEvent = null;
151 public ReusableBreakpoint (DebuggerTests owner, string method_name)
154 this.method_name = method_name;
155 MethodMirror m = owner.entry_point.DeclaringType.GetMethod (method_name);
156 Assert.IsNotNull (m);
157 req = owner.vm.SetBreakpoint (m, m.ILOffsets [0]);
160 public void Continue ()
162 bool survived = false;
169 e = owner.GetNextEvent ();
170 if (e is BreakpointEvent)
174 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
175 Assert.AreEqual (method_name, (e as BreakpointEvent).Method.Name);
177 lastEvent = e as BreakpointEvent;
181 if (!survived) { // Ensure cleanup if we triggered an assert
187 public void Disable ()
193 /* One of the tests executes a complex tree of recursive functions.
194 The only good way to specify how its behavior should appear from this side
195 is to just run the function tree once over here and record what it does. */
196 public struct RecursiveChaoticPoint
198 public bool breakpoint;
202 public RecursiveChaoticPoint (bool breakpoint, string name, int depth)
204 this.breakpoint = breakpoint;
210 // The breakpoint is placed here in dtest-app.cs
211 public static void ss_recursive_chaotic_trap (int n, List<RecursiveChaoticPoint> trace, ref bool didLast, ref bool didAny)
213 // Depth is calculated as:
214 // Main + single_stepping + ss_recursive_chaotic + (n is 5 at outermost frame and 0 at innermost frame) + ss_recursive_chaotic_trap
215 trace.Add (new RecursiveChaoticPoint (true, "ss_recursive_chaotic_trap", 5 - n + 5));
219 public static void ss_recursive_chaotic_at (string at, int n, List<RecursiveChaoticPoint> trace, ref bool didLast, ref bool didAny)
221 // This will be called after every return from a function. The other function will return whether "step out" is currently active, and it will be passed in here as didLast.
223 // Depth is calculated as:
224 // Main + single_stepping + ss_recursive_chaotic + (n is 5 at outermost frame and 0 at innermost frame)
225 trace.Add (new RecursiveChaoticPoint (false, "ss_recursive_chaotic_" + at, 5 - n + 4));
231 public static bool ss_recursive_chaotic_fizz (int n, List<RecursiveChaoticPoint> trace)
233 bool didLast = false, didAny = false;
236 didLast = ss_recursive_chaotic_buzz (next, trace);
237 ss_recursive_chaotic_at ("fizz", n, trace, ref didLast, ref didAny);
238 didLast = ss_recursive_chaotic_fizzbuzz (next, trace);
239 ss_recursive_chaotic_at ("fizz", n, trace, ref didLast, ref didAny);
241 ss_recursive_chaotic_trap (n, trace, ref didLast, ref didAny);
242 ss_recursive_chaotic_at ("fizz", n, trace, ref didLast, ref didAny);
247 public static bool ss_recursive_chaotic_buzz (int n, List<RecursiveChaoticPoint> trace)
249 bool didLast = false, didAny = false;
252 didLast = ss_recursive_chaotic_fizz (next, trace);
253 ss_recursive_chaotic_at ("buzz", n, trace, ref didLast, ref didAny);
254 didLast = ss_recursive_chaotic_fizzbuzz (next, trace);
255 ss_recursive_chaotic_at ("buzz", n, trace, ref didLast, ref didAny);
260 public static bool ss_recursive_chaotic_fizzbuzz (int n, List<RecursiveChaoticPoint> trace)
262 bool didLast = false, didAny = false;
265 didLast = ss_recursive_chaotic_fizz (next, trace);
266 ss_recursive_chaotic_at ("fizzbuzz", n, trace, ref didLast, ref didAny);
267 didLast = ss_recursive_chaotic_buzz (next, trace);
268 ss_recursive_chaotic_at ("fizzbuzz", n, trace, ref didLast, ref didAny);
269 didLast = ss_recursive_chaotic_fizzbuzz (next, trace);
270 ss_recursive_chaotic_at ("fizzbuzz", n, trace, ref didLast, ref didAny);
275 public static void trace_ss_recursive_chaotic (List<RecursiveChaoticPoint> trace)
277 ss_recursive_chaotic_fizz (5, trace);
280 Event single_step (ThreadMirror t) {
281 var req = vm.CreateStepRequest (t);
285 Event e = GetNextEvent ();
286 Assert.IsTrue (e is StepEvent);
293 Event step_until (ThreadMirror t, string method_name) {
297 if ((e as StepEvent).Method.Name == method_name)
303 void check_arg_val (StackFrame frame, int pos, Type type, object eval) {
304 object val = frame.GetArgument (pos);
305 Assert.IsTrue (val is PrimitiveValue);
306 object v = (val as PrimitiveValue).Value;
307 Assert.AreEqual (type, v.GetType ());
309 Assert.IsTrue (Math.Abs ((float)eval - (float)v) < 0.0001);
310 else if (eval is double)
311 Assert.IsTrue (Math.Abs ((double)eval - (double)v) < 0.0001);
313 Assert.AreEqual (eval, v);
316 void AssertValue (object expected, object val) {
317 if (expected is string) {
318 Assert.IsTrue (val is StringMirror);
319 Assert.AreEqual (expected, (val as StringMirror).Value);
320 } else if (val is StructMirror && (val as StructMirror).Type.Name == "IntPtr") {
321 AssertValue (expected, (val as StructMirror).Fields [0]);
323 Assert.IsTrue (val is PrimitiveValue);
324 Assert.AreEqual (expected, (val as PrimitiveValue).Value);
329 public void SetUp () {
330 ThreadMirror.NativeTransitions = false;
331 Start (new string [] { "dtest-app.exe" });
335 public void TearDown () {
339 if (step_req != null)
347 Event e = GetNextEvent ();
349 if (e is VMDeathEvent)
358 public void SimpleBreakpoint () {
361 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp1");
362 Assert.IsNotNull (m);
364 vm.SetBreakpoint (m, 0);
369 Assert.AreEqual (EventType.Breakpoint, e.EventType);
370 Assert.IsTrue (e is BreakpointEvent);
371 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
374 AssertThrows<ArgumentException> (delegate {
376 vm.SetBreakpoint (m, 2);
381 public void BreakpointsSameLocation () {
382 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp2");
383 Assert.IsNotNull (m);
385 vm.SetBreakpoint (m, 0);
386 vm.SetBreakpoint (m, 0);
390 var es = vm.GetNextEventSet ();
391 Assert.AreEqual (2, es.Events.Length);
392 Assert.IsTrue (es [0] is BreakpointEvent);
393 Assert.AreEqual (m, (es [0] as BreakpointEvent).Method);
395 Assert.IsTrue (es [1] is BreakpointEvent);
396 Assert.AreEqual (m.Name, (es [1] as BreakpointEvent).Method.Name);
400 public void BreakpointAlreadyJITted () {
401 Event e = run_until ("bp1");
403 /* Place a breakpoint on bp3 */
404 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp3");
405 Assert.IsNotNull (m);
406 vm.SetBreakpoint (m, 0);
408 /* Same with generic instances */
409 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp7");
410 Assert.IsNotNull (m2);
411 vm.SetBreakpoint (m2, 0);
416 Assert.AreEqual (EventType.Breakpoint, e.EventType);
417 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
421 /* Non-shared instance */
423 Assert.AreEqual (EventType.Breakpoint, e.EventType);
424 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
428 /* Shared instance */
430 Assert.AreEqual (EventType.Breakpoint, e.EventType);
431 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
435 public void ClearBreakpoint () {
438 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
439 Assert.IsNotNull (m);
440 EventRequest req1 = vm.SetBreakpoint (m, 0);
441 EventRequest req2 = vm.SetBreakpoint (m, 0);
443 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
444 Assert.IsNotNull (m2);
445 vm.SetBreakpoint (m2, 0);
450 var es = vm.GetNextEventSet ();
451 Assert.AreEqual (2, es.Events.Length);
452 Assert.AreEqual (EventType.Breakpoint, es [0].EventType);
453 Assert.AreEqual (m.Name, (es [0] as BreakpointEvent).Method.Name);
454 Assert.AreEqual (EventType.Breakpoint, es [1].EventType);
455 Assert.AreEqual (m.Name, (es [1] as BreakpointEvent).Method.Name);
457 /* Clear one of them */
463 Assert.AreEqual (EventType.Breakpoint, e.EventType);
464 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
466 /* Clear the other */
472 Assert.AreEqual (EventType.Breakpoint, e.EventType);
473 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
477 public void ClearAllBreakpoints () {
480 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
481 Assert.IsNotNull (m);
482 vm.SetBreakpoint (m, 0);
484 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
485 Assert.IsNotNull (m2);
486 vm.SetBreakpoint (m2, 0);
488 vm.ClearAllBreakpoints ();
493 Assert.IsTrue (!(e is BreakpointEvent));
494 if (e is VMDeathEvent)
499 public void BreakpointOnGShared () {
502 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp6");
503 Assert.IsNotNull (m);
505 vm.SetBreakpoint (m, 0);
510 Assert.AreEqual (EventType.Breakpoint, e.EventType);
511 Assert.IsTrue (e is BreakpointEvent);
512 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
514 // Breakpoint on an open generic method of a closed generic class (#3422)
515 var frame = e.Thread.GetFrames ()[0];
516 var ginst = frame.GetValue (frame.Method.GetLocal ("gc"));
517 var m2 = (ginst as ObjectMirror).Type.GetMethod ("bp");
518 vm.SetBreakpoint (m2, 0);
523 Assert.AreEqual (EventType.Breakpoint, e.EventType);
524 Assert.IsTrue (e is BreakpointEvent);
525 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
528 // Assert we have stepped to a location
529 void assert_location (Event e, string method) {
530 Assert.IsTrue (e is StepEvent);
531 Assert.AreEqual (method, (e as StepEvent).Method.Name);
534 // Assert we have breakpointed at a location
535 void assert_location_at_breakpoint (Event e, string method) {
536 Assert.IsTrue (e is BreakpointEvent);
537 Assert.AreEqual (method, (e as BreakpointEvent).Method.Name);
540 // Assert we have stepped to or breakpointed at a location
541 void assert_location_allow_breakpoint (Event e, string method) {
543 Assert.AreEqual (method, (e as StepEvent).Method.Name);
544 else if (e is BreakpointEvent)
545 Assert.AreEqual (method, (e as BreakpointEvent).Method.Name);
547 Assert.Fail ("Neither step nor breakpoint event");
550 StepEventRequest create_step (Event e) {
551 var req = vm.CreateStepRequest (e.Thread);
557 public void ClassLocalReflection () {
558 MethodMirror m = entry_point.DeclaringType.Assembly.GetType ("LocalReflectClass").GetMethod ("RunMe");
560 Assert.IsNotNull (m);
561 //Console.WriteLine ("X: " + name + " " + m.ILOffsets.Count + " " + m.Locations.Count);
563 int method_base_linum = m.Locations [0].LineNumber;
564 foreach (var location in m.Locations)
565 if (location.LineNumber == method_base_linum + 2) {
566 offset = location.ILOffset;
570 var req = vm.SetBreakpoint (m, offset);
577 if (e is BreakpointEvent)
583 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
584 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
586 e = single_step (e.Thread);
588 var frame = e.Thread.GetFrames ()[0];
589 Value variable = frame.GetValue (frame.Method.GetLocal ("reflectMe"));
591 ObjectMirror thisObj = (ObjectMirror)variable;
592 TypeMirror thisType = thisObj.Type;
593 FieldInfoMirror thisFi = null;
594 foreach (var fi in thisType.GetFields ())
595 if (fi.Name == "someField")
598 var gotVal = thisObj.GetValue (thisFi);
599 // If we got this far, we're good.
603 public void SingleStepping () {
604 Event e = run_until ("single_stepping");
606 var req = create_step (e);
609 // Step over 'bool b = true'
611 assert_location (e, "single_stepping");
618 assert_location (e, "ss1");
625 assert_location (e, "single_stepping");
629 assert_location (e, "single_stepping");
633 assert_location (e, "ss3");
635 // Step back into single_stepping
637 assert_location (e, "single_stepping");
639 // Step into next line
641 assert_location (e, "single_stepping");
643 // Step into ss3_2 ()
645 assert_location (e, "ss3_2");
647 // Step over ss3_2_2 ()
649 assert_location (e, "ss3_2");
651 // Recreate the request
658 // Step back into single_stepping () with the new request
660 assert_location (e, "single_stepping");
664 assert_location (e, "ss4");
669 // Change to StepSize.Line
671 req.Depth = StepDepth.Over;
672 req.Size = StepSize.Line;
675 // Step over ss1 (); ss1 ();
680 req.Depth = StepDepth.Into;
684 assert_location (e, "ss2");
689 e = run_until ("ss5");
691 // Add an assembly filter
692 req.AssemblyFilter = new AssemblyMirror [] { (e as BreakpointEvent).Method.DeclaringType.Assembly };
698 // Step into is_even, skipping the linq stuff
700 assert_location (e, "is_even");
702 // FIXME: Check that single stepping works with lock (obj)
706 e = run_until ("ss6");
708 req = create_step (e);
709 req.Depth = StepDepth.Over;
712 // Check that single stepping works in out-of-line bblocks
715 assert_location (e, "ss6");
718 // Check that a step over stops at an EH clause
719 e = run_until ("ss7_2");
720 req = create_step (e);
721 req.Depth = StepDepth.Out;
724 assert_location (e, "ss7");
726 req = create_step (e);
727 req.Depth = StepDepth.Over;
730 assert_location (e, "ss7");
733 // Check that stepping stops between nested calls
734 e = run_until ("ss_nested_2");
736 assert_location (e, "ss_nested");
738 assert_location (e, "ss_nested_1");
740 assert_location (e, "ss_nested");
741 // Check that step over steps over nested calls
743 assert_location (e, "ss_nested");
745 assert_location (e, "ss_nested_1");
747 assert_location (e, "ss_nested_1");
749 assert_location (e, "ss_nested");
752 // Check DebuggerStepThrough support
753 e = run_until ("ss_step_through");
754 req = create_step (e);
755 req.Filter = StepFilter.DebuggerStepThrough;
757 // Step through step_through_1 ()
759 assert_location (e, "ss_step_through");
760 // Step through StepThroughClass.step_through_2 ()
762 assert_location (e, "ss_step_through");
764 req.Filter = StepFilter.None;
766 assert_location (e, "step_through_3");
769 // Check DebuggerNonUserCode support
770 e = run_until ("ss_non_user_code");
771 req = create_step (e);
772 req.Filter = StepFilter.DebuggerNonUserCode;
774 // Step through non_user_code_1 ()
776 assert_location (e, "ss_non_user_code");
777 // Step through StepThroughClass.non_user_code_2 ()
779 assert_location (e, "ss_non_user_code");
781 req.Filter = StepFilter.None;
783 assert_location (e, "non_user_code_3");
786 // Check that step-over doesn't stop at inner frames with recursive functions
787 e = run_until ("ss_recursive");
788 req = create_step (e);
792 var f = e.Thread.GetFrames () [0];
793 assert_location (e, "ss_recursive");
794 AssertValue (1, f.GetValue (f.Method.GetLocal ("n")));
797 // Check that step-over stops correctly when inner frames with recursive functions contain breakpoints
798 e = run_until ("ss_recursive2");
799 ReusableBreakpoint breakpoint = new ReusableBreakpoint (this, "ss_recursive2_trap");
801 breakpoint.Continue ();
802 e = breakpoint.lastEvent;
803 req = create_step (e);
804 for (int c = 1; c <= 4; c++) {
805 // The first five times we try to step over this function, the breakpoint will stop us
806 assert_location_at_breakpoint (e, "ss_recursive2_trap");
809 req = create_step (e);
810 req.Size = StepSize.Line;
813 assert_location (e, "ss_recursive2");
815 // Stack should consist of Main + single_stepping + (1 ss_recursive2 frame per loop iteration)
816 Assert.AreEqual (c+2, e.Thread.GetFrames ().Length);
817 e = step_over_or_breakpoint ();
819 // At this point we should have escaped the breakpoints and this will be a normal step stop
820 assert_location (e, "ss_recursive2");
821 Assert.AreEqual (6, e.Thread.GetFrames ().Length);
824 breakpoint.Disable ();
827 // Check that step-out stops correctly when inner frames with recursive functions contain breakpoints
828 e = run_until ("ss_recursive2");
829 breakpoint = new ReusableBreakpoint (this, "ss_recursive2_trap");
831 breakpoint.Continue ();
832 e = breakpoint.lastEvent;
833 req = create_step (e);
834 for (int c = 1; c <= 4; c++) {
835 // The first five times we try to step over this function, the breakpoint will stop us
836 assert_location_at_breakpoint (e, "ss_recursive2_trap");
839 req = create_step (e);
840 req.Size = StepSize.Line;
843 assert_location (e, "ss_recursive2");
845 // Stack should consist of Main + single_stepping + (1 ss_recursive2 frame per loop iteration)
846 Assert.AreEqual (c+2, e.Thread.GetFrames ().Length);
847 e = step_out_or_breakpoint ();
849 for (int c = 3; c >= 1; c--) {
850 assert_location (e, "ss_recursive2");
851 Assert.AreEqual (c + 2, e.Thread.GetFrames ().Length);
857 breakpoint.Disable ();
860 // Test step out with a really complicated call tree
861 List<RecursiveChaoticPoint> trace = new List<RecursiveChaoticPoint>();
862 trace_ss_recursive_chaotic (trace);
863 e = run_until ("ss_recursive_chaotic");
865 breakpoint = new ReusableBreakpoint (this, "ss_recursive_chaotic_trap");
866 breakpoint.Continue ();
867 e = breakpoint.lastEvent;
868 foreach (RecursiveChaoticPoint point in trace)
870 if (point.breakpoint)
871 assert_location_at_breakpoint (e, point.name);
873 assert_location (e, point.name);
874 Assert.AreEqual (point.depth, e.Thread.GetFrames ().Length);
877 req = create_step (e);
878 req.Size = StepSize.Line;
879 e = step_out_or_breakpoint ();
883 breakpoint.Disable ();
886 // Check that single stepping doesn't clobber fp values
887 e = run_until ("ss_fp_clobber");
888 req = create_step (e);
890 f = e.Thread.GetFrames ()[0];
892 if ((e as StepEvent).Method.Name == "ss_fp_clobber_2")
896 f = e.Thread.GetFrames ()[0];
897 AssertValue (7.0, f.GetValue (f.Method.GetParameters ()[0]));
902 public void MethodEntryExit () {
903 run_until ("single_stepping");
905 var req1 = vm.CreateMethodEntryRequest ();
906 var req2 = vm.CreateMethodExitRequest ();
912 Event e = GetNextEvent ();
913 Assert.IsTrue (e is MethodEntryEvent);
914 Assert.AreEqual ("ss1", (e as MethodEntryEvent).Method.Name);
918 Assert.IsTrue (e is MethodExitEvent);
919 Assert.AreEqual ("ss1", (e as MethodExitEvent).Method.Name);
926 public void CountFilter () {
927 run_until ("single_stepping");
929 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("ss3");
930 Assert.IsNotNull (m2);
931 vm.SetBreakpoint (m2, 0);
933 var req1 = vm.CreateMethodEntryRequest ();
937 // Enter ss2, ss1 is skipped
939 Event e = GetNextEvent ();
940 Assert.IsTrue (e is MethodEntryEvent);
941 Assert.AreEqual ("ss2", (e as MethodEntryEvent).Method.Name);
943 // Breakpoint on ss3, the entry event is no longer reported
946 Assert.IsTrue (e is BreakpointEvent);
952 public void Arguments () {
955 var e = run_until ("arg1");
957 StackFrame frame = e.Thread.GetFrames () [0];
959 check_arg_val (frame, 0, typeof (sbyte), SByte.MaxValue - 5);
960 check_arg_val (frame, 1, typeof (byte), Byte.MaxValue - 5);
961 check_arg_val (frame, 2, typeof (bool), true);
962 check_arg_val (frame, 3, typeof (short), Int16.MaxValue - 5);
963 check_arg_val (frame, 4, typeof (ushort), UInt16.MaxValue - 5);
964 check_arg_val (frame, 5, typeof (char), 'F');
965 check_arg_val (frame, 6, typeof (int), Int32.MaxValue - 5);
966 check_arg_val (frame, 7, typeof (uint), UInt32.MaxValue - 5);
967 check_arg_val (frame, 8, typeof (long), Int64.MaxValue - 5);
968 check_arg_val (frame, 9, typeof (ulong), UInt64.MaxValue - 5);
969 check_arg_val (frame, 10, typeof (float), 1.2345f);
970 check_arg_val (frame, 11, typeof (double), 6.78910);
972 e = run_until ("arg2");
974 frame = e.Thread.GetFrames () [0];
977 val = frame.GetArgument (0);
978 AssertValue ("FOO", val);
979 Assert.AreEqual ("String", (val as ObjectMirror).Type.Name);
982 val = frame.GetArgument (1);
983 AssertValue (null, val);
986 val = frame.GetArgument (2);
987 AssertValue ("BLA", val);
990 val = frame.GetArgument (3);
991 AssertValue (42, val);
994 val = frame.GetArgument (4);
995 Assert.IsTrue (val is ObjectMirror);
996 Assert.AreEqual ("GClass`1", (val as ObjectMirror).Type.Name);
999 val = frame.GetArgument (5);
1000 Assert.IsTrue (val is ObjectMirror);
1001 Assert.AreEqual ("Object", (val as ObjectMirror).Type.Name);
1003 // this on static methods
1004 val = frame.GetThis ();
1005 AssertValue (null, val);
1007 e = run_until ("arg3");
1009 frame = e.Thread.GetFrames () [0];
1012 val = frame.GetThis ();
1013 Assert.IsTrue (val is ObjectMirror);
1014 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
1016 // objref in register
1017 val = frame.GetArgument (0);
1018 AssertValue ("BLA", val);
1022 public void Arrays () {
1025 var e = run_until ("o2");
1027 StackFrame frame = e.Thread.GetFrames () [0];
1030 val = frame.GetArgument (0);
1031 Assert.IsTrue (val is ArrayMirror);
1032 ArrayMirror arr = val as ArrayMirror;
1033 Assert.AreEqual (2, arr.Length);
1034 AssertValue ("BAR", arr [0]);
1035 AssertValue ("BAZ", arr [1]);
1037 var vals = arr.GetValues (0, 2);
1038 Assert.AreEqual (2, vals.Count);
1039 AssertValue ("BAR", vals [0]);
1040 AssertValue ("BAZ", vals [1]);
1042 arr [0] = vm.RootDomain.CreateString ("ABC");
1043 AssertValue ("ABC", arr [0]);
1045 arr [0] = vm.CreateValue (null);
1046 AssertValue (null, arr [0]);
1048 arr.SetValues (0, new Value [] { vm.RootDomain.CreateString ("D1"), vm.RootDomain.CreateString ("D2") });
1049 AssertValue ("D1", arr [0]);
1050 AssertValue ("D2", arr [1]);
1053 val = frame.GetArgument (1);
1054 Assert.IsTrue (val is ArrayMirror);
1055 arr = val as ArrayMirror;
1056 Assert.AreEqual (2, arr.Length);
1057 AssertValue (42, arr [0]);
1058 AssertValue (43, arr [1]);
1060 // Argument checking
1061 AssertThrows<IndexOutOfRangeException> (delegate () {
1065 AssertThrows<IndexOutOfRangeException> (delegate () {
1066 val = arr [Int32.MinValue];
1069 AssertThrows<IndexOutOfRangeException> (delegate () {
1070 vals = arr.GetValues (0, 3);
1073 AssertThrows<IndexOutOfRangeException> (delegate () {
1074 arr [2] = vm.CreateValue (null);
1077 AssertThrows<IndexOutOfRangeException> (delegate () {
1078 arr [Int32.MinValue] = vm.CreateValue (null);
1081 AssertThrows<IndexOutOfRangeException> (delegate () {
1082 arr.SetValues (0, new Value [] { null, null, null });
1086 val = frame.GetArgument (2);
1087 Assert.IsTrue (val is ArrayMirror);
1088 arr = val as ArrayMirror;
1089 Assert.AreEqual (2, arr.Rank);
1090 Assert.AreEqual (4, arr.Length);
1091 Assert.AreEqual (2, arr.GetLength (0));
1092 Assert.AreEqual (2, arr.GetLength (1));
1093 Assert.AreEqual (0, arr.GetLowerBound (0));
1094 Assert.AreEqual (0, arr.GetLowerBound (1));
1095 vals = arr.GetValues (0, 4);
1096 AssertValue (1, vals [0]);
1097 AssertValue (2, vals [1]);
1098 AssertValue (3, vals [2]);
1099 AssertValue (4, vals [3]);
1101 val = frame.GetArgument (3);
1102 Assert.IsTrue (val is ArrayMirror);
1103 arr = val as ArrayMirror;
1104 Assert.AreEqual (2, arr.Rank);
1105 Assert.AreEqual (4, arr.Length);
1106 Assert.AreEqual (2, arr.GetLength (0));
1107 Assert.AreEqual (2, arr.GetLength (1));
1108 Assert.AreEqual (1, arr.GetLowerBound (0));
1109 Assert.AreEqual (3, arr.GetLowerBound (1));
1111 AssertThrows<ArgumentOutOfRangeException> (delegate () {
1114 AssertThrows<ArgumentOutOfRangeException> (delegate () {
1118 AssertThrows<ArgumentOutOfRangeException> (delegate () {
1119 arr.GetLowerBound (-1);
1121 AssertThrows<ArgumentOutOfRangeException> (delegate () {
1122 arr.GetLowerBound (2);
1125 // arrays treated as generic collections
1126 val = frame.GetArgument (4);
1127 Assert.IsTrue (val is ArrayMirror);
1128 arr = val as ArrayMirror;
1132 public void Object_GetValue () {
1133 var e = run_until ("o1");
1134 var frame = e.Thread.GetFrames () [0];
1136 object val = frame.GetThis ();
1137 Assert.IsTrue (val is ObjectMirror);
1138 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
1139 ObjectMirror o = (val as ObjectMirror);
1141 TypeMirror t = o.Type;
1144 object f = o.GetValue (t.GetField ("field_i"));
1145 AssertValue (42, f);
1146 f = o.GetValue (t.GetField ("field_s"));
1147 AssertValue ("S", f);
1148 f = o.GetValue (t.GetField ("field_enum"));
1149 Assert.IsTrue (f is EnumMirror);
1150 Assert.AreEqual (1, (f as EnumMirror).Value);
1151 Assert.AreEqual ("B", (f as EnumMirror).StringValue);
1153 // Inherited object fields
1154 TypeMirror parent = t.BaseType;
1155 f = o.GetValue (parent.GetField ("base_field_i"));
1156 AssertValue (43, f);
1157 f = o.GetValue (parent.GetField ("base_field_s"));
1158 AssertValue ("T", f);
1161 f = o.GetValue (o.Type.GetField ("static_i"));
1162 AssertValue (55, f);
1164 // generic instances
1165 ObjectMirror o2 = frame.GetValue (frame.Method.GetParameters ()[1]) as ObjectMirror;
1166 Assert.AreEqual ("GClass`1", o2.Type.Name);
1167 TypeMirror t2 = o2.Type;
1168 f = o2.GetValue (t2.GetField ("field"));
1169 AssertValue (42, f);
1171 ObjectMirror o3 = frame.GetValue (frame.Method.GetParameters ()[2]) as ObjectMirror;
1172 Assert.AreEqual ("GClass`1", o3.Type.Name);
1173 TypeMirror t3 = o3.Type;
1174 f = o3.GetValue (t3.GetField ("field"));
1175 AssertValue ("FOO", f);
1177 // Argument checking
1178 AssertThrows<ArgumentNullException> (delegate () {
1184 public void Object_GetValues () {
1185 var e = run_until ("o1");
1186 var frame = e.Thread.GetFrames () [0];
1188 object val = frame.GetThis ();
1189 Assert.IsTrue (val is ObjectMirror);
1190 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
1191 ObjectMirror o = (val as ObjectMirror);
1193 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1195 TypeMirror t = o.Type;
1197 object[] vals = o.GetValues (new FieldInfoMirror [] { t.GetField ("field_i"), t.GetField ("field_s") });
1198 object f = vals [0];
1199 AssertValue (42, f);
1201 AssertValue ("S", f);
1203 // Argument checking
1204 AssertThrows<ArgumentNullException> (delegate () {
1208 AssertThrows<ArgumentNullException> (delegate () {
1209 o.GetValues (new FieldInfoMirror [] { null });
1212 // field of another class
1213 AssertThrows<ArgumentException> (delegate () {
1214 o.GetValue (val2.Type.GetField ("field_j"));
1218 void TestSetValue (ObjectMirror o, string field_name, object val) {
1220 o.SetValue (o.Type.GetField (field_name), vm.RootDomain.CreateString ((string)val));
1222 o.SetValue (o.Type.GetField (field_name), vm.CreateValue (val));
1223 Value f = o.GetValue (o.Type.GetField (field_name));
1224 AssertValue (val, f);
1228 public void Object_SetValues () {
1229 var e = run_until ("o1");
1230 var frame = e.Thread.GetFrames () [0];
1232 object val = frame.GetThis ();
1233 Assert.IsTrue (val is ObjectMirror);
1234 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
1235 ObjectMirror o = (val as ObjectMirror);
1237 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1239 TestSetValue (o, "field_i", 22);
1240 TestSetValue (o, "field_bool1", false);
1241 TestSetValue (o, "field_bool2", true);
1242 TestSetValue (o, "field_char", 'B');
1243 TestSetValue (o, "field_byte", (byte)129);
1244 TestSetValue (o, "field_sbyte", (sbyte)-33);
1245 TestSetValue (o, "field_short", (short)(Int16.MaxValue - 5));
1246 TestSetValue (o, "field_ushort", (ushort)(UInt16.MaxValue - 5));
1247 TestSetValue (o, "field_long", Int64.MaxValue - 5);
1248 TestSetValue (o, "field_ulong", (ulong)(UInt64.MaxValue - 5));
1249 TestSetValue (o, "field_float", 6.28f);
1250 TestSetValue (o, "field_double", 6.28);
1251 TestSetValue (o, "static_i", 23);
1252 TestSetValue (o, "field_s", "CDEF");
1257 f = o.GetValue (o.Type.GetField ("field_intptr"));
1258 Assert.IsInstanceOfType (typeof (StructMirror), f);
1259 AssertValue (Int32.MaxValue - 5, (f as StructMirror).Fields [0]);
1262 FieldInfoMirror field = o.Type.GetField ("field_enum");
1263 f = o.GetValue (field);
1264 (f as EnumMirror).Value = 5;
1265 o.SetValue (field, f);
1266 f = o.GetValue (field);
1267 Assert.AreEqual (5, (f as EnumMirror).Value);
1270 o.SetValue (o.Type.GetField ("field_s"), vm.CreateValue (null));
1271 f = o.GetValue (o.Type.GetField ("field_s"));
1272 AssertValue (null, f);
1275 field = o.Type.GetField ("generic_field_struct");
1276 f = o.GetValue (field);
1277 o.SetValue (field, f);
1280 field = o.Type.GetField ("field_nullable");
1281 f = o.GetValue (field);
1282 AssertValue (0, (f as StructMirror).Fields [0]);
1283 AssertValue (false, (f as StructMirror).Fields [1]);
1284 o.SetValue (field, vm.CreateValue (6));
1285 f = o.GetValue (field);
1286 AssertValue (6, (f as StructMirror).Fields [0]);
1287 AssertValue (true, (f as StructMirror).Fields [1]);
1288 o.SetValue (field, vm.CreateValue (null));
1289 f = o.GetValue (field);
1290 AssertValue (0, (f as StructMirror).Fields [0]);
1291 AssertValue (false, (f as StructMirror).Fields [1]);
1293 // Argument checking
1294 AssertThrows<ArgumentNullException> (delegate () {
1295 o.SetValues (null, new Value [0]);
1298 AssertThrows<ArgumentNullException> (delegate () {
1299 o.SetValues (new FieldInfoMirror [0], null);
1302 AssertThrows<ArgumentNullException> (delegate () {
1303 o.SetValues (new FieldInfoMirror [] { null }, new Value [1] { null });
1306 // vtype with a wrong type
1307 AssertThrows<ArgumentException> (delegate () {
1308 o.SetValue (o.Type.GetField ("field_struct"), o.GetValue (o.Type.GetField ("field_enum")));
1311 // reference type not assignment compatible
1312 AssertThrows<ArgumentException> (delegate () {
1313 o.SetValue (o.Type.GetField ("field_class"), o);
1316 // field of another class
1317 AssertThrows<ArgumentException> (delegate () {
1318 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
1323 public void Type_SetValue () {
1324 var e = run_until ("o1");
1325 var frame = e.Thread.GetFrames () [0];
1328 object val = frame.GetThis ();
1329 Assert.IsTrue (val is ObjectMirror);
1330 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
1331 ObjectMirror o = (val as ObjectMirror);
1333 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1335 o.Type.SetValue (o.Type.GetField ("static_i"), vm.CreateValue (55));
1336 f = o.Type.GetValue (o.Type.GetField ("static_i"));
1337 AssertValue (55, f);
1339 o.Type.SetValue (o.Type.GetField ("static_s"), vm.RootDomain.CreateString ("B"));
1340 f = o.Type.GetValue (o.Type.GetField ("static_s"));
1341 AssertValue ("B", f);
1343 // Argument checking
1344 AssertThrows<ArgumentNullException> (delegate () {
1345 o.Type.SetValue (null, vm.CreateValue (0));
1348 AssertThrows<ArgumentNullException> (delegate () {
1349 o.Type.SetValue (o.Type.GetField ("static_i"), null);
1352 // field of another class
1353 AssertThrows<ArgumentException> (delegate () {
1354 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
1359 public void TypeInfo () {
1360 Event e = run_until ("ti2");
1361 StackFrame frame = e.Thread.GetFrames () [0];
1366 t = frame.Method.GetParameters ()[0].ParameterType;
1368 Assert.AreEqual ("String[]", t.Name);
1369 Assert.AreEqual ("string[]", t.CSharpName);
1370 Assert.AreEqual ("Array", t.BaseType.Name);
1371 Assert.AreEqual (true, t.HasElementType);
1372 Assert.AreEqual (true, t.IsArray);
1373 Assert.AreEqual (1, t.GetArrayRank ());
1374 Assert.AreEqual ("String", t.GetElementType ().Name);
1376 t = frame.Method.GetParameters ()[2].ParameterType;
1378 Assert.AreEqual ("Int32[,]", t.Name);
1380 //Assert.AreEqual ("int[,]", t.CSharpName);
1381 Assert.AreEqual ("Array", t.BaseType.Name);
1382 Assert.AreEqual (true, t.HasElementType);
1383 Assert.AreEqual (true, t.IsArray);
1384 Assert.AreEqual (2, t.GetArrayRank ());
1385 Assert.AreEqual ("Int32", t.GetElementType ().Name);
1388 t = frame.Method.GetParameters ()[3].ParameterType;
1390 //Assert.AreEqual ("Int32&", t.Name);
1391 //Assert.AreEqual (true, t.IsByRef);
1392 //Assert.AreEqual (true, t.HasElementType);
1395 t = frame.Method.GetParameters ()[4].ParameterType;
1397 //Assert.AreEqual ("Int32*", t.Name);
1398 Assert.AreEqual (true, t.IsPointer);
1399 Assert.AreEqual (true, t.HasElementType);
1400 Assert.AreEqual ("Int32", t.GetElementType ().Name);
1401 Assert.AreEqual (false, t.IsPrimitive);
1404 t = frame.Method.GetParameters ()[5].ParameterType;
1405 Assert.AreEqual (true, t.IsPrimitive);
1408 t = frame.Method.GetParameters ()[6].ParameterType;
1409 Assert.AreEqual ("AStruct", t.Name);
1410 Assert.AreEqual (false, t.IsPrimitive);
1411 Assert.AreEqual (true, t.IsValueType);
1412 Assert.AreEqual (false, t.IsClass);
1415 t = frame.Method.GetParameters ()[7].ParameterType;
1416 Assert.AreEqual ("Tests", t.Name);
1417 var nested = (from nt in t.GetNestedTypes () where nt.IsNestedPublic select nt).ToArray ();
1418 Assert.AreEqual (1, nested.Length);
1419 Assert.AreEqual ("NestedClass", nested [0].Name);
1420 Assert.IsTrue (t.BaseType.IsAssignableFrom (t));
1421 Assert.IsTrue (!t.IsAssignableFrom (t.BaseType));
1423 // generic instances
1424 t = frame.Method.GetParameters ()[9].ParameterType;
1425 Assert.AreEqual ("GClass`1", t.Name);
1426 Assert.IsTrue (t.IsGenericType);
1427 Assert.IsFalse (t.IsGenericTypeDefinition);
1429 var args = t.GetGenericArguments ();
1430 Assert.AreEqual (1, args.Length);
1431 Assert.AreEqual ("Int32", args [0].Name);
1433 // generic type definitions
1434 var gtd = t.GetGenericTypeDefinition ();
1435 Assert.AreEqual ("GClass`1", gtd.Name);
1436 Assert.IsTrue (gtd.IsGenericType);
1437 Assert.IsTrue (gtd.IsGenericTypeDefinition);
1438 Assert.AreEqual (gtd, gtd.GetGenericTypeDefinition ());
1440 args = gtd.GetGenericArguments ();
1441 Assert.AreEqual (1, args.Length);
1442 Assert.AreEqual ("T", args [0].Name);
1445 t = frame.Method.GetParameters ()[10].ParameterType;
1446 Assert.AreEqual ("AnEnum", t.Name);
1447 Assert.IsTrue (t.IsEnum);
1448 Assert.AreEqual ("Int32", t.EnumUnderlyingType.Name);
1451 t = frame.Method.GetParameters ()[11].ParameterType;
1452 Assert.AreEqual ("TypedReference", t.Name);
1455 t = frame.Method.GetParameters ()[7].ParameterType;
1457 var props = t.GetProperties ();
1458 Assert.AreEqual (3, props.Length);
1459 foreach (PropertyInfoMirror prop in props) {
1460 ParameterInfoMirror[] indexes = prop.GetIndexParameters ();
1462 if (prop.Name == "IntProperty") {
1463 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1464 Assert.AreEqual ("get_IntProperty", prop.GetGetMethod ().Name);
1465 Assert.AreEqual ("set_IntProperty", prop.GetSetMethod ().Name);
1466 Assert.AreEqual (0, indexes.Length);
1467 } else if (prop.Name == "ReadOnlyProperty") {
1468 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1469 Assert.AreEqual ("get_ReadOnlyProperty", prop.GetGetMethod ().Name);
1470 Assert.AreEqual (null, prop.GetSetMethod ());
1471 Assert.AreEqual (0, indexes.Length);
1472 } else if (prop.Name == "IndexedProperty") {
1473 Assert.AreEqual (1, indexes.Length);
1474 Assert.AreEqual ("Int32", indexes [0].ParameterType.Name);
1478 // custom attributes
1479 t = frame.Method.GetParameters ()[8].ParameterType;
1480 Assert.AreEqual ("Tests2", t.Name);
1481 var attrs = t.GetCustomAttributes (true);
1482 Assert.AreEqual (5, attrs.Length);
1483 foreach (var attr in attrs) {
1484 if (attr.Constructor.DeclaringType.Name == "DebuggerDisplayAttribute") {
1485 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1486 Assert.AreEqual ("Tests", attr.ConstructorArguments [0].Value);
1487 Assert.AreEqual (2, attr.NamedArguments.Count);
1488 Assert.AreEqual ("Name", attr.NamedArguments [0].Property.Name);
1489 Assert.AreEqual ("FOO", attr.NamedArguments [0].TypedValue.Value);
1490 Assert.AreEqual ("Target", attr.NamedArguments [1].Property.Name);
1491 Assert.IsInstanceOfType (typeof (TypeMirror), attr.NamedArguments [1].TypedValue.Value);
1492 Assert.AreEqual ("Int32", (attr.NamedArguments [1].TypedValue.Value as TypeMirror).Name);
1493 } else if (attr.Constructor.DeclaringType.Name == "DebuggerTypeProxyAttribute") {
1494 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1495 Assert.IsInstanceOfType (typeof (TypeMirror), attr.ConstructorArguments [0].Value);
1496 Assert.AreEqual ("Tests", (attr.ConstructorArguments [0].Value as TypeMirror).Name);
1497 } else if (attr.Constructor.DeclaringType.Name == "BAttribute") {
1498 Assert.AreEqual (2, attr.NamedArguments.Count);
1499 Assert.AreEqual ("afield", attr.NamedArguments [0].Field.Name);
1500 Assert.AreEqual ("bfield", attr.NamedArguments [1].Field.Name);
1501 } else if (attr.Constructor.DeclaringType.Name == "ClassInterfaceAttribute") {
1502 // inherited from System.Object
1503 //} else if (attr.Constructor.DeclaringType.Name == "Serializable") {
1504 // inherited from System.Object
1505 } else if (attr.Constructor.DeclaringType.Name == "ComVisibleAttribute") {
1506 // inherited from System.Object
1508 Assert.Fail (attr.Constructor.DeclaringType.Name);
1512 var assembly = entry_point.DeclaringType.Assembly;
1513 var type = assembly.GetType ("Tests4");
1514 Assert.IsFalse (type.IsInitialized);
1518 public void FieldInfo () {
1519 Event e = run_until ("ti2");
1520 StackFrame frame = e.Thread.GetFrames () [0];
1524 t = frame.Method.GetParameters ()[8].ParameterType;
1525 Assert.AreEqual ("Tests2", t.Name);
1527 var fi = t.GetField ("field_j");
1528 var attrs = fi.GetCustomAttributes (true);
1529 Assert.AreEqual (1, attrs.Length);
1530 var attr = attrs [0];
1531 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1532 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1533 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1534 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1538 public void PropertyInfo () {
1539 Event e = run_until ("ti2");
1540 StackFrame frame = e.Thread.GetFrames () [0];
1544 t = frame.Method.GetParameters ()[8].ParameterType;
1545 Assert.AreEqual ("Tests2", t.Name);
1547 var pi = t.GetProperty ("AProperty");
1548 var attrs = pi.GetCustomAttributes (true);
1549 Assert.AreEqual (1, attrs.Length);
1550 var attr = attrs [0];
1551 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1552 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1553 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1554 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1558 [Category ("only5")]
1559 public void Type_GetValue () {
1560 Event e = run_until ("o1");
1561 StackFrame frame = e.Thread.GetFrames () [0];
1563 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1565 TypeMirror t = o.Type;
1567 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1570 object f = t.GetValue (o.Type.GetField ("static_i"));
1571 AssertValue (55, f);
1573 f = t.GetValue (o.Type.GetField ("static_s"));
1574 AssertValue ("A", f);
1576 // literal static fields
1577 f = t.GetValue (o.Type.GetField ("literal_i"));
1578 AssertValue (56, f);
1580 f = t.GetValue (o.Type.GetField ("literal_s"));
1581 AssertValue ("B", f);
1583 // Inherited static fields
1584 TypeMirror parent = t.BaseType;
1585 f = t.GetValue (parent.GetField ("base_static_i"));
1586 AssertValue (57, f);
1588 f = t.GetValue (parent.GetField ("base_static_s"));
1589 AssertValue ("C", f);
1591 // thread static field
1592 f = t.GetValue (t.GetField ("tls_i"), e.Thread);
1593 AssertValue (42, f);
1595 // Argument checking
1596 AssertThrows<ArgumentNullException> (delegate () {
1601 AssertThrows<ArgumentException> (delegate () {
1602 t.GetValue (o.Type.GetField ("field_i"));
1605 // field on another type
1606 AssertThrows<ArgumentException> (delegate () {
1607 t.GetValue (val2.Type.GetField ("static_field_j"));
1610 // special static field
1611 AssertThrows<ArgumentException> (delegate () {
1612 t.GetValue (t.GetField ("tls_i"));
1617 public void Type_GetValues () {
1618 Event e = run_until ("o1");
1619 StackFrame frame = e.Thread.GetFrames () [0];
1621 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1623 TypeMirror t = o.Type;
1626 object[] vals = t.GetValues (new FieldInfoMirror [] { t.GetField ("static_i"), t.GetField ("static_s") });
1627 object f = vals [0];
1628 AssertValue (55, f);
1631 AssertValue ("A", f);
1633 // Argument checking
1634 AssertThrows<ArgumentNullException> (delegate () {
1638 AssertThrows<ArgumentNullException> (delegate () {
1639 t.GetValues (new FieldInfoMirror [] { null });
1644 public void ObjRefs () {
1645 Event e = run_until ("objrefs1");
1646 StackFrame frame = e.Thread.GetFrames () [0];
1648 ObjectMirror o = frame.GetThis () as ObjectMirror;
1649 ObjectMirror child = o.GetValue (o.Type.GetField ("child")) as ObjectMirror;
1651 Assert.IsTrue (child.Address != 0);
1653 // Check that object references are internalized correctly
1654 Assert.AreEqual (o, frame.GetThis ());
1656 run_until ("objrefs2");
1658 // child should be gc'd now
1659 // This is not deterministic
1660 //Assert.IsTrue (child.IsCollected);
1663 * No longer works since Type is read eagerly
1666 AssertThrows<ObjectCollectedException> (delegate () {
1667 TypeMirror t = child.Type;
1671 AssertThrows<ObjectCollectedException> (delegate () {
1672 long addr = child.Address;
1678 public void Type_GetObject () {
1679 Event e = run_until ("o1");
1680 StackFrame frame = e.Thread.GetFrames () [0];
1682 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1684 TypeMirror t = o.Type;
1686 Assert.AreEqual ("RuntimeType", t.GetTypeObject ().Type.Name);
1690 public void VTypes () {
1691 Event e = run_until ("vtypes1");
1692 StackFrame frame = e.Thread.GetFrames () [0];
1695 ObjectMirror o = frame.GetThis () as ObjectMirror;
1696 var obj = o.GetValue (o.Type.GetField ("field_struct"));
1697 Assert.IsTrue (obj is StructMirror);
1698 var s = obj as StructMirror;
1699 Assert.AreEqual ("AStruct", s.Type.Name);
1700 AssertValue (42, s ["i"]);
1702 AssertValue ("S", obj);
1703 AssertValue (43, s ["k"]);
1704 obj = o.GetValue (o.Type.GetField ("field_boxed_struct"));
1705 Assert.IsTrue (obj is StructMirror);
1706 s = obj as StructMirror;
1707 Assert.AreEqual ("AStruct", s.Type.Name);
1708 AssertValue (42, s ["i"]);
1710 // Check decoding of nested structs (#14942)
1711 obj = o.GetValue (o.Type.GetField ("nested_struct"));
1712 o.SetValue (o.Type.GetField ("nested_struct"), obj);
1714 // Check round tripping of boxed struct fields (#12354)
1715 obj = o.GetValue (o.Type.GetField ("boxed_struct_field"));
1716 o.SetValue (o.Type.GetField ("boxed_struct_field"), obj);
1717 obj = o.GetValue (o.Type.GetField ("boxed_struct_field"));
1718 s = obj as StructMirror;
1719 AssertValue (1, s ["key"]);
1721 Assert.IsTrue (obj is StructMirror);
1722 s = obj as StructMirror;
1723 AssertValue (42, s ["m_value"]);
1725 // vtypes as arguments
1726 s = frame.GetArgument (0) as StructMirror;
1727 AssertValue (44, s ["i"]);
1729 AssertValue ("T", obj);
1730 AssertValue (45, s ["k"]);
1732 // vtypes as array entries
1733 var arr = frame.GetArgument (1) as ArrayMirror;
1735 Assert.IsTrue (obj is StructMirror);
1736 s = obj as StructMirror;
1737 AssertValue (1, s ["i"]);
1738 AssertValue ("S1", s ["s"]);
1740 Assert.IsTrue (obj is StructMirror);
1741 s = obj as StructMirror;
1742 AssertValue (2, s ["i"]);
1743 AssertValue ("S2", s ["s"]);
1746 var typedref = frame.GetArgument (2) as StructMirror;
1747 Assert.IsTrue (typedref is StructMirror);
1749 // Argument checking
1750 s = frame.GetArgument (0) as StructMirror;
1751 AssertThrows<ArgumentException> (delegate () {
1755 // generic vtype instances
1756 o = frame.GetThis () as ObjectMirror;
1757 obj = o.GetValue (o.Type.GetField ("generic_field_struct"));
1758 Assert.IsTrue (obj is StructMirror);
1759 s = obj as StructMirror;
1760 Assert.AreEqual ("GStruct`1", s.Type.Name);
1761 AssertValue (42, s ["i"]);
1763 // this on vtype methods
1764 e = run_until ("vtypes2");
1765 e = step_until (e.Thread, "foo");
1767 frame = e.Thread.GetFrames () [0];
1769 Assert.AreEqual ("foo", (e as StepEvent).Method.Name);
1770 obj = frame.GetThis ();
1772 Assert.IsTrue (obj is StructMirror);
1773 s = obj as StructMirror;
1774 AssertValue (44, s ["i"]);
1775 AssertValue ("T", s ["s"]);
1776 AssertValue (45, s ["k"]);
1779 s ["i"] = vm.CreateValue (55);
1781 obj = frame.GetThis ();
1782 Assert.IsTrue (obj is StructMirror);
1783 s = obj as StructMirror;
1784 AssertValue (55, s ["i"]);
1785 AssertValue ("T", s ["s"]);
1786 AssertValue (45, s ["k"]);
1788 // this on static vtype methods
1789 e = run_until ("vtypes3");
1790 e = step_until (e.Thread, "static_foo");
1792 frame = e.Thread.GetFrames () [0];
1794 Assert.AreEqual ("static_foo", (e as StepEvent).Method.Name);
1795 obj = frame.GetThis ();
1796 AssertValue (null, obj);
1798 // vtypes which reference themselves recursively
1799 e = run_until ("vtypes4_2");
1800 frame = e.Thread.GetFrames () [0];
1802 Assert.IsTrue (frame.GetArgument (0) is StructMirror);
1806 public void AssemblyInfo () {
1807 Event e = run_until ("single_stepping");
1809 StackFrame frame = e.Thread.GetFrames () [0];
1811 var aname = frame.Method.DeclaringType.Assembly.GetName ();
1812 Assert.AreEqual ("dtest-app, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", aname.ToString ());
1814 ModuleMirror m = frame.Method.DeclaringType.Module;
1816 Assert.AreEqual ("dtest-app.exe", m.Name);
1817 Assert.AreEqual ("dtest-app.exe", m.ScopeName);
1818 Assert.IsTrue (m.FullyQualifiedName.IndexOf ("dtest-app.exe") != -1);
1819 Guid guid = m.ModuleVersionId;
1820 Assert.AreEqual (frame.Method.DeclaringType.Assembly, m.Assembly);
1821 Assert.AreEqual (frame.Method.DeclaringType.Assembly.ManifestModule, m);
1823 // This is no longer true on 4.0
1824 //Assert.AreEqual ("Assembly", frame.Method.DeclaringType.Assembly.GetAssemblyObject ().Type.Name);
1826 TypeMirror t = vm.RootDomain.Corlib.GetType ("System.Diagnostics.DebuggerDisplayAttribute");
1827 Assert.AreEqual ("DebuggerDisplayAttribute", t.Name);
1831 public void LocalsInfo () {
1832 Event e = run_until ("locals2");
1834 StackFrame frame = e.Thread.GetFrames () [0];
1836 var locals = frame.Method.GetLocals ();
1837 Assert.AreEqual (9, locals.Length);
1838 for (int i = 0; i < 9; ++i) {
1839 if (locals [i].Name == "args") {
1840 Assert.IsTrue (locals [i].IsArg);
1841 Assert.AreEqual ("String[]", locals [i].Type.Name);
1842 } else if (locals [i].Name == "arg") {
1843 Assert.IsTrue (locals [i].IsArg);
1844 Assert.AreEqual ("Int32", locals [i].Type.Name);
1845 } else if (locals [i].Name == "i") {
1846 Assert.IsFalse (locals [i].IsArg);
1847 Assert.AreEqual ("Int64", locals [i].Type.Name);
1848 } else if (locals [i].Name == "j") {
1849 Assert.IsFalse (locals [i].IsArg);
1850 Assert.AreEqual ("Int32", locals [i].Type.Name);
1851 } else if (locals [i].Name == "s") {
1852 Assert.IsFalse (locals [i].IsArg);
1853 Assert.AreEqual ("String", locals [i].Type.Name);
1854 } else if (locals [i].Name == "t") {
1856 Assert.IsTrue (locals [i].IsArg);
1857 Assert.AreEqual ("String", locals [i].Type.Name);
1858 } else if (locals [i].Name == "rs") {
1859 Assert.IsTrue (locals [i].IsArg);
1860 Assert.AreEqual ("String", locals [i].Type.Name);
1861 } else if (locals [i].Name == "astruct") {
1862 } else if (locals [i].Name == "alist") {
1868 var scopes = frame.Method.GetScopes ();
1869 Assert.AreEqual (2, scopes.Length);
1872 Event step_once () {
1874 var e = GetNextEvent ();
1875 Assert.IsTrue (e is StepEvent);
1879 Event step_into () {
1880 step_req.Disable ();
1881 step_req.Depth = StepDepth.Into;
1883 return step_once ();
1886 Event step_over () {
1887 step_req.Disable ();
1888 step_req.Depth = StepDepth.Over;
1890 return step_once ();
1894 step_req.Disable ();
1895 step_req.Depth = StepDepth.Out;
1897 return step_once ();
1900 Event step_once_or_breakpoint () {
1902 var e = GetNextEvent ();
1903 Assert.IsTrue (e is StepEvent || e is BreakpointEvent);
1907 Event step_over_or_breakpoint () {
1908 step_req.Disable ();
1909 step_req.Depth = StepDepth.Over;
1911 return step_once_or_breakpoint ();
1914 Event step_out_or_breakpoint () {
1915 step_req.Disable ();
1916 step_req.Depth = StepDepth.Out;
1918 return step_once_or_breakpoint ();
1922 public void Locals () {
1923 var be = run_until ("locals1");
1925 StackFrame frame = be.Thread.GetFrames () [0];
1926 MethodMirror m1 = frame.Method;
1928 // Compiler generated byref local
1929 foreach (var l in m1.GetLocals ()) {
1930 // The byval flag is hidden from the type
1931 if (l.Name != "ri" && l.Type.Name == "Double")
1932 AssertValue (null, frame.GetValue (l));
1935 be = run_until ("locals2");
1937 frame = be.Thread.GetFrames () [0];
1939 object val = frame.GetValue (frame.Method.GetLocal ("i"));
1940 AssertValue (0, val);
1942 var req = create_step (be);
1949 var e = step_once ();
1950 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1952 // Execute s = "AB";
1954 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1956 frame = e.Thread.GetFrames () [0];
1958 val = frame.GetValue (frame.Method.GetLocal ("i"));
1959 AssertValue (42, val);
1961 LocalVariable[] locals = frame.Method.GetLocals ();
1962 var vals = frame.GetValues (locals);
1963 Assert.AreEqual (locals.Length, vals.Length);
1964 for (int i = 0; i < locals.Length; ++i) {
1965 if (locals [i].Name == "i")
1966 AssertValue (42, vals [i]);
1967 if (locals [i].Name == "s")
1968 AssertValue ("AB", vals [i]);
1969 if (locals [i].Name == "t")
1970 AssertValue ("ABC", vals [i]);
1971 if (locals [i].Name == "alist") {
1975 // Argument checking
1978 AssertThrows<ArgumentNullException> (delegate () {
1979 frame.GetValue ((LocalVariable)null);
1981 // GetValue () local from another method
1982 AssertThrows<ArgumentException> (delegate () {
1983 frame.GetValue (m1.GetLocal ("foo"));
1987 AssertThrows<ArgumentNullException> (delegate () {
1988 frame.GetValue ((ParameterInfoMirror)null);
1990 // GetValue () local from another method
1991 AssertThrows<ArgumentException> (delegate () {
1992 frame.GetValue (m1.GetParameters ()[0]);
1995 // GetValues () null
1996 AssertThrows<ArgumentNullException> (delegate () {
1997 frame.GetValues (null);
1999 // GetValues () embedded null
2000 AssertThrows<ArgumentNullException> (delegate () {
2001 frame.GetValues (new LocalVariable [] { null });
2003 // GetValues () local from another method
2004 AssertThrows<ArgumentException> (delegate () {
2005 frame.GetValues (new LocalVariable [] { m1.GetLocal ("foo") });
2008 AssertThrows<ArgumentException> (delegate () {
2009 val = frame.GetValue (frame.Method.ReturnParameter);
2012 // invalid stack frames
2014 e = GetNextEvent ();
2015 Assert.IsTrue (e is StepEvent);
2016 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
2018 AssertThrows<InvalidStackFrameException> (delegate () {
2019 frame.GetValue (frame.Method.GetLocal ("i"));
2025 be = run_until ("locals7");
2027 req = create_step (be);
2033 // Test that locals are initialized
2034 frame = e.Thread.GetFrames () [0];
2035 val = frame.GetValue (frame.Method.GetLocal ("t"));
2036 AssertValue (0, val);
2040 Assert.AreEqual ("locals7", (e as StepEvent).Method.Name);
2044 Assert.AreEqual ("locals7", (e as StepEvent).Method.Name);
2046 frame = e.Thread.GetFrames () [0];
2047 val = frame.GetValue (frame.Method.GetParameters ()[0]);
2048 AssertValue (22, val);
2049 val = frame.GetValue (frame.Method.GetLocal ("t"));
2050 AssertValue (22, val);
2051 val = frame.GetValue (frame.Method.GetLocal ("t2"));
2052 AssertValue (22, val);
2056 public void GetVisibleVariables () {
2057 Event e = run_until ("locals4");
2060 var locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
2061 Assert.AreEqual (2, locals.Count);
2062 var loc = locals.First (l => l.Name == "i");
2063 Assert.AreEqual ("Int64", loc.Type.Name);
2064 loc = locals.First (l => l.Name == "s");
2065 Assert.AreEqual ("String", loc.Type.Name);
2067 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
2068 Assert.AreEqual ("i", loc.Name);
2069 Assert.AreEqual ("Int64", loc.Type.Name);
2071 e = run_until ("locals5");
2074 locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
2075 Assert.AreEqual (2, locals.Count);
2076 loc = locals.First (l => l.Name == "i");
2077 Assert.AreEqual ("String", loc.Type.Name);
2078 loc = locals.First (l => l.Name == "s");
2079 Assert.AreEqual ("String", loc.Type.Name);
2081 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
2082 Assert.AreEqual ("i", loc.Name);
2083 Assert.AreEqual ("String", loc.Type.Name);
2085 // Variable in another scope
2086 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("j");
2087 Assert.IsNull (loc);
2091 public void Exit () {
2096 var e = GetNextEvent ();
2097 Assert.IsInstanceOfType (typeof (VMDeathEvent), e);
2099 Assert.AreEqual (5, (e as VMDeathEvent).ExitCode);
2102 /* Could be a remote vm with no process */
2105 Assert.AreEqual (5, p.ExitCode);
2108 AssertThrows<VMDisconnectedException> (delegate () {
2117 public void Dispose () {
2122 var e = GetNextEvent ();
2123 Assert.IsInstanceOfType (typeof (VMDisconnectEvent), e);
2126 /* Could be a remote vm with no process */
2129 Assert.AreEqual (3, p.ExitCode);
2132 AssertThrows<VMDisconnectedException> (delegate () {
2141 public void ColumnNumbers () {
2142 Event e = run_until ("line_numbers");
2144 // FIXME: Merge this with LineNumbers () when its fixed
2146 step_req = create_step (e);
2147 step_req.Depth = StepDepth.Into;
2155 e = GetNextEvent ();
2156 Assert.IsTrue (e is StepEvent);
2157 if (e.Thread.GetFrames ()[0].Method.Name == "ln1")
2161 // Do an additional step over so we are not on the beginning line of the method
2162 step_req.Disable ();
2163 step_req.Depth = StepDepth.Over;
2166 e = GetNextEvent ();
2167 Assert.IsTrue (e is StepEvent);
2169 l = e.Thread.GetFrames ()[0].Location;
2171 Assert.AreEqual (3, l.ColumnNumber);
2173 step_req.Disable ();
2177 // Broken by mcs+runtime changes (#5438)
2178 [Category("NotWorking")]
2179 public void LineNumbers () {
2180 Event e = run_until ("line_numbers");
2182 step_req = create_step (e);
2183 step_req.Depth = StepDepth.Into;
2190 e = GetNextEvent ();
2191 Assert.IsTrue (e is StepEvent);
2193 l = e.Thread.GetFrames ()[0].Location;
2195 Assert.IsTrue (l.SourceFile.IndexOf ("dtest-app.cs") != -1);
2196 Assert.AreEqual ("ln1", l.Method.Name);
2199 using (FileStream fs = new FileStream (l.SourceFile, FileMode.Open, FileAccess.Read)) {
2200 MD5 md5 = MD5.Create ();
2201 var hash = md5.ComputeHash (fs);
2203 for (int i = 0; i < 16; ++i)
2204 Assert.AreEqual (hash [i], l.SourceFileHash [i]);
2207 int line_base = l.LineNumber;
2210 e = GetNextEvent ();
2211 Assert.IsTrue (e is StepEvent);
2212 l = e.Thread.GetFrames ()[0].Location;
2213 Assert.AreEqual ("ln2", l.Method.Name);
2214 Assert.AreEqual (line_base + 6, l.LineNumber);
2217 e = GetNextEvent ();
2218 Assert.IsTrue (e is StepEvent);
2219 l = e.Thread.GetFrames ()[0].Location;
2220 Assert.AreEqual ("ln1", l.Method.Name);
2221 Assert.AreEqual (line_base + 1, l.LineNumber);
2224 e = GetNextEvent ();
2225 Assert.IsTrue (e is StepEvent);
2226 l = e.Thread.GetFrames ()[0].Location;
2227 Assert.AreEqual ("ln3", l.Method.Name);
2228 Assert.AreEqual (line_base + 11, l.LineNumber);
2231 e = GetNextEvent ();
2232 Assert.IsTrue (e is StepEvent);
2233 l = e.Thread.GetFrames ()[0].Location;
2234 Assert.AreEqual ("ln3", l.Method.Name);
2235 Assert.IsTrue (l.SourceFile.EndsWith ("FOO"));
2236 Assert.AreEqual (55, l.LineNumber);
2239 e = GetNextEvent ();
2240 Assert.IsTrue (e is StepEvent);
2241 l = e.Thread.GetFrames ()[0].Location;
2242 Assert.AreEqual ("ln1", l.Method.Name);
2243 Assert.AreEqual (line_base + 2, l.LineNumber);
2245 // GetSourceFiles ()
2246 string[] sources = l.Method.DeclaringType.GetSourceFiles ();
2247 Assert.AreEqual (2, sources.Length);
2248 Assert.AreEqual ("dtest-app.cs", sources [0]);
2249 Assert.AreEqual ("FOO", sources [1]);
2251 sources = l.Method.DeclaringType.GetSourceFiles (true);
2252 Assert.AreEqual (2, sources.Length);
2253 Assert.IsTrue (sources [0].EndsWith ("dtest-app.cs"));
2254 Assert.IsTrue (sources [1].EndsWith ("FOO"));
2258 public void Suspend () {
2261 Start (new string [] { "dtest-app.exe", "suspend-test" });
2263 Event e = run_until ("suspend");
2265 ThreadMirror main = e.Thread;
2273 // The debuggee should be suspended while it is running the infinite loop
2275 StackFrame frame = main.GetFrames ()[0];
2276 Assert.AreEqual ("suspend", frame.Method.Name);
2280 // resuming when not suspended
2281 AssertThrows<InvalidOperationException> (delegate () {
2291 public void AssemblyLoad () {
2292 Event e = run_until ("assembly_load");
2294 var load_req = vm.CreateAssemblyLoadRequest ();
2299 e = GetNextEvent ();
2300 Assert.IsInstanceOfType (typeof (AssemblyLoadEvent), e);
2301 Assert.IsTrue ((e as AssemblyLoadEvent).Assembly.Location.EndsWith ("System.dll"));
2303 var frames = e.Thread.GetFrames ();
2304 Assert.IsTrue (frames.Length > 0);
2305 Assert.AreEqual ("assembly_load", frames [0].Method.Name);
2309 public void CreateValue () {
2312 v = vm.CreateValue (1);
2313 Assert.AreEqual (vm, v.VirtualMachine);
2314 Assert.AreEqual (1, v.Value);
2316 v = vm.CreateValue (null);
2317 Assert.AreEqual (vm, v.VirtualMachine);
2318 Assert.AreEqual (null, v.Value);
2320 // Argument checking
2321 AssertThrows <ArgumentException> (delegate () {
2322 v = vm.CreateValue ("FOO");
2327 public void CreateString () {
2328 StringMirror s = vm.RootDomain.CreateString ("ABC");
2330 Assert.AreEqual (vm, s.VirtualMachine);
2331 Assert.AreEqual ("ABC", s.Value);
2332 Assert.AreEqual (vm.RootDomain, s.Domain);
2335 StringBuilder sb = new StringBuilder ();
2336 for (int i = 0; i < 1024; ++i)
2338 s = vm.RootDomain.CreateString (sb.ToString ());
2340 // Argument checking
2341 AssertThrows <ArgumentNullException> (delegate () {
2342 s = vm.RootDomain.CreateString (null);
2347 public void CreateBoxedValue () {
2348 ObjectMirror o = vm.RootDomain.CreateBoxedValue (new PrimitiveValue (vm, 42));
2350 Assert.AreEqual ("Int32", o.Type.Name);
2351 //AssertValue (42, m.GetValue (o.Type.GetField ("m_value")));
2353 // Argument checking
2354 AssertThrows <ArgumentNullException> (delegate () {
2355 vm.RootDomain.CreateBoxedValue (null);
2358 AssertThrows <ArgumentException> (delegate () {
2359 vm.RootDomain.CreateBoxedValue (o);
2364 public void Invoke () {
2365 Event e = run_until ("invoke1");
2367 StackFrame frame = e.Thread.GetFrames () [0];
2369 TypeMirror t = frame.Method.DeclaringType;
2370 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2372 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2378 m = t.GetMethod ("invoke_return_void");
2379 v = this_obj.InvokeMethod (e.Thread, m, null);
2383 m = t.GetMethod ("invoke_return_ref");
2384 v = this_obj.InvokeMethod (e.Thread, m, null);
2385 AssertValue ("ABC", v);
2388 m = t.GetMethod ("invoke_return_null");
2389 v = this_obj.InvokeMethod (e.Thread, m, null);
2390 AssertValue (null, v);
2393 m = t.GetMethod ("invoke_return_primitive");
2394 v = this_obj.InvokeMethod (e.Thread, m, null);
2395 AssertValue (42, v);
2398 m = t.GetMethod ("invoke_return_nullable");
2399 v = this_obj.InvokeMethod (e.Thread, m, null);
2400 Assert.IsInstanceOfType (typeof (StructMirror), v);
2401 var s = v as StructMirror;
2402 AssertValue (42, s.Fields [0]);
2403 AssertValue (true, s.Fields [1]);
2405 // pass nullable as this
2406 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2407 m = s.Type.GetMethod ("ToString");
2408 v = s.InvokeMethod (e.Thread, m, null);
2410 // return nullable null
2411 m = t.GetMethod ("invoke_return_nullable_null");
2412 v = this_obj.InvokeMethod (e.Thread, m, null);
2413 Assert.IsInstanceOfType (typeof (StructMirror), v);
2414 s = v as StructMirror;
2415 AssertValue (0, s.Fields [0]);
2416 AssertValue (false, s.Fields [1]);
2418 // pass nullable as this
2419 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2420 m = s.Type.GetMethod ("ToString");
2421 v = s.InvokeMethod (e.Thread, m, null);
2424 m = t.GetMethod ("invoke_pass_primitive");
2425 Value[] args = new Value [] {
2426 vm.CreateValue ((byte)Byte.MaxValue),
2427 vm.CreateValue ((sbyte)SByte.MaxValue),
2428 vm.CreateValue ((short)1),
2429 vm.CreateValue ((ushort)1),
2430 vm.CreateValue ((int)1),
2431 vm.CreateValue ((uint)1),
2432 vm.CreateValue ((long)1),
2433 vm.CreateValue ((ulong)1),
2434 vm.CreateValue ('A'),
2435 vm.CreateValue (true),
2436 vm.CreateValue (3.14f),
2437 vm.CreateValue (3.14) };
2439 v = this_obj.InvokeMethod (e.Thread, m, args);
2440 AssertValue ((int)Byte.MaxValue + (int)SByte.MaxValue + 1 + 1 + 1 + 1 + 1 + 1 + 'A' + 1 + 3 + 3, v);
2443 m = t.GetMethod ("invoke_pass_ref");
2444 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2445 AssertValue ("ABC", v);
2448 m = t.GetMethod ("invoke_pass_ref");
2449 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (null) });
2450 AssertValue (null, v);
2453 m = t.GetMethod ("invoke_static_pass_ref");
2454 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2455 AssertValue ("ABC", v);
2457 // static invoked using ObjectMirror.InvokeMethod
2458 m = t.GetMethod ("invoke_static_pass_ref");
2459 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2460 AssertValue ("ABC", v);
2462 // method which throws an exception
2464 m = t.GetMethod ("invoke_throws");
2465 v = this_obj.InvokeMethod (e.Thread, m, null);
2467 } catch (InvocationException ex) {
2468 Assert.AreEqual ("Exception", ex.Exception.Type.Name);
2472 m = t.GetMethod ("invoke_out");
2473 var out_task = this_obj.InvokeMethodAsyncWithResult (e.Thread, m, new Value [] { vm.CreateValue (1), vm.CreateValue (null) }, InvokeOptions.ReturnOutArgs);
2474 var out_args = out_task.Result.OutArgs;
2475 AssertValue (5, out_args [0]);
2476 Assert.IsTrue (out_args [1] is ArrayMirror);
2477 Assert.AreEqual (10, (out_args [1] as ArrayMirror).Length);
2479 // without ReturnOutArgs flag
2480 out_task = this_obj.InvokeMethodAsyncWithResult (e.Thread, m, new Value [] { vm.CreateValue (1), vm.CreateValue (null) });
2481 out_args = out_task.Result.OutArgs;
2482 Assert.IsNull (out_args);
2485 m = t.GetMethod (".ctor");
2486 v = t.InvokeMethod (e.Thread, m, null);
2487 Assert.IsInstanceOfType (typeof (ObjectMirror), v);
2488 Assert.AreEqual ("Tests", (v as ObjectMirror).Type.Name);
2491 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("ITest2");
2492 m = cl1.GetMethod ("invoke_iface");
2493 v = this_obj.InvokeMethod (e.Thread, m, null);
2494 AssertValue (42, v);
2497 m = t.BaseType.GetMethod ("virtual_method");
2498 v = this_obj.InvokeMethod (e.Thread, m, null, InvokeOptions.Virtual);
2499 AssertValue ("V2", v);
2501 // virtual call on static method
2502 m = t.GetMethod ("invoke_static_pass_ref");
2503 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") }, InvokeOptions.Virtual);
2504 AssertValue ("ABC", v);
2507 m = t.GetMethod ("invoke_pass_ref");
2508 var task = this_obj.InvokeMethodAsync (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2509 AssertValue ("ABC", task.Result);
2512 m = t.GetMethod ("invoke_static_pass_ref");
2513 task = t.InvokeMethodAsync (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2514 AssertValue ("ABC", task.Result);
2516 // Argument checking
2519 AssertThrows<ArgumentNullException> (delegate {
2520 m = t.GetMethod ("invoke_pass_ref");
2521 v = this_obj.InvokeMethod (null, m, new Value [] { vm.CreateValue (null) });
2525 AssertThrows<ArgumentNullException> (delegate {
2526 v = this_obj.InvokeMethod (e.Thread, null, new Value [] { vm.CreateValue (null) });
2529 // invalid number of arguments
2530 m = t.GetMethod ("invoke_pass_ref");
2531 AssertThrows<ArgumentException> (delegate {
2532 v = this_obj.InvokeMethod (e.Thread, m, null);
2535 // invalid type of argument (ref != primitive)
2536 m = t.GetMethod ("invoke_pass_ref");
2537 AssertThrows<ArgumentException> (delegate {
2538 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2541 // invalid type of argument (primitive != primitive)
2542 m = t.GetMethod ("invoke_pass_primitive_2");
2543 AssertThrows<ArgumentException> (delegate {
2544 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2547 // invoking a non-static method as static
2548 m = t.GetMethod ("invoke_pass_ref");
2549 AssertThrows<ArgumentException> (delegate {
2550 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2553 // invoking a method defined in another class
2554 m = t2.GetMethod ("invoke");
2555 AssertThrows<ArgumentException> (delegate {
2556 v = this_obj.InvokeMethod (e.Thread, m, null);
2561 public void InvokeVType () {
2562 Event e = run_until ("invoke1");
2564 StackFrame frame = e.Thread.GetFrames () [0];
2566 var s = frame.GetArgument (1) as StructMirror;
2568 TypeMirror t = s.Type;
2573 // Pass struct as this, receive int
2574 m = t.GetMethod ("invoke_return_int");
2575 v = s.InvokeMethod (e.Thread, m, null);
2576 AssertValue (42, v);
2578 // Pass boxed struct as this
2579 var boxed_this = t.NewInstance () as ObjectMirror;
2580 m = t.GetMethod ("invoke_return_int");
2581 v = boxed_this.InvokeMethod (e.Thread, m, null);
2584 // Pass struct as this, receive intptr
2585 m = t.GetMethod ("invoke_return_intptr");
2586 v = s.InvokeMethod (e.Thread, m, null);
2587 AssertValue (43, v);
2590 m = t.GetMethod ("invoke_static");
2591 v = t.InvokeMethod (e.Thread, m, null);
2594 // Pass generic struct as this
2595 s = frame.GetArgument (2) as StructMirror;
2597 m = t.GetMethod ("invoke_return_int");
2598 v = s.InvokeMethod (e.Thread, m, null);
2599 AssertValue (42, v);
2602 s = frame.GetArgument (1) as StructMirror;
2604 m = t.GetMethods ().First (method => method.Name == ".ctor" && method.GetParameters ().Length == 1);
2605 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2606 AssertValue (1, (v as StructMirror)["i"]);
2608 // Invoke a method which changes state
2609 s = frame.GetArgument (1) as StructMirror;
2611 m = t.GetMethod ("invoke_mutate");
2612 var task = s.InvokeMethodAsyncWithResult (e.Thread, m, null, InvokeOptions.ReturnOutThis);
2613 var out_this = task.Result.OutThis as StructMirror;
2614 AssertValue (5, out_this ["l"]);
2616 // Without the ReturnOutThis flag
2617 s = frame.GetArgument (1) as StructMirror;
2619 m = t.GetMethod ("invoke_mutate");
2620 task = s.InvokeMethodAsyncWithResult (e.Thread, m, null);
2621 out_this = task.Result.OutThis as StructMirror;
2622 Assert.AreEqual (null, out_this);
2625 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("ITest2");
2626 m = cl1.GetMethod ("invoke_iface");
2627 v = s.InvokeMethod (e.Thread, m, null);
2628 AssertValue (42, v);
2631 m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2632 v = s.InvokeMethod (e.Thread, m, null, InvokeOptions.Virtual);
2633 AssertValue ("42", v);
2637 public void BreakpointDuringInvoke () {
2638 Event e = run_until ("invoke1");
2640 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke2");
2641 Assert.IsNotNull (m);
2642 vm.SetBreakpoint (m, 0);
2644 StackFrame frame = e.Thread.GetFrames () [0];
2645 var o = frame.GetThis () as ObjectMirror;
2647 bool failed = false;
2649 bool finished = false;
2650 object wait = new object ();
2652 // Have to invoke in a separate thread as the invoke is suspended until we
2653 // resume after the breakpoint
2654 Thread t = new Thread (delegate () {
2656 o.InvokeMethod (e.Thread, m, null);
2662 Monitor.Pulse (wait);
2668 StackFrame invoke_frame = null;
2671 e = GetNextEvent ();
2672 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
2673 // Check stack trace support and invokes
2674 var frames = e.Thread.GetFrames ();
2675 invoke_frame = frames [0];
2676 Assert.AreEqual ("invoke2", frames [0].Method.Name);
2677 Assert.IsTrue (frames [0].IsDebuggerInvoke);
2678 Assert.AreEqual ("invoke1", frames [1].Method.Name);
2685 Monitor.Wait (wait);
2688 // Check that the invoke frames are no longer valid
2689 AssertThrows<InvalidStackFrameException> (delegate {
2690 invoke_frame.GetThis ();
2693 // Check InvokeOptions.DisableBreakpoints flag
2694 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2698 public void DisabledExceptionDuringInvoke () {
2699 Event e = run_until ("invoke_ex");
2701 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_ex_inner");
2703 StackFrame frame = e.Thread.GetFrames () [0];
2704 var o = frame.GetThis () as ObjectMirror;
2706 var req = vm.CreateExceptionRequest (null);
2709 // Check InvokeOptions.DisableBreakpoints flag
2710 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2716 public void InvokeSingleThreaded () {
2719 Start (new string [] { "dtest-app.exe", "invoke-single-threaded" });
2721 Event e = run_until ("invoke_single_threaded_2");
2723 StackFrame f = e.Thread.GetFrames ()[0];
2725 var obj = f.GetThis () as ObjectMirror;
2727 // Check that the counter value incremented by the other thread does not increase
2728 // during the invoke.
2729 object counter1 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2731 var m = obj.Type.GetMethod ("invoke_return_void");
2732 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2734 object counter2 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2736 Assert.AreEqual ((int)counter1, (int)counter2);
2738 // Test multiple invokes done in succession
2739 m = obj.Type.GetMethod ("invoke_return_void");
2740 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2742 // Test events during single-threaded invokes
2743 vm.EnableEvents (EventType.TypeLoad);
2744 m = obj.Type.GetMethod ("invoke_type_load");
2745 obj.BeginInvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded, delegate {
2749 e = GetNextEvent ();
2750 Assert.AreEqual (EventType.TypeLoad, e.EventType);
2753 List<Value> invoke_results;
2756 public void InvokeMultiple () {
2757 Event e = run_until ("invoke1");
2759 StackFrame frame = e.Thread.GetFrames () [0];
2761 TypeMirror t = frame.Method.DeclaringType;
2762 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2764 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2766 var methods = new MethodMirror [2];
2767 methods [0] = t.GetMethod ("invoke_return_ref");
2768 methods [1] = t.GetMethod ("invoke_return_primitive");
2770 invoke_results = new List<Value> ();
2772 var r = this_obj.BeginInvokeMultiple (e.Thread, methods, null, InvokeOptions.SingleThreaded, invoke_multiple_cb, this_obj);
2773 WaitHandle.WaitAll (new WaitHandle[] { r.AsyncWaitHandle });
2774 this_obj.EndInvokeMultiple (r);
2775 // The callback might still be running
2776 while (invoke_results.Count < 2) {
2779 if (invoke_results [0] is PrimitiveValue) {
2780 AssertValue ("ABC", invoke_results [1]);
2781 AssertValue (42, invoke_results [0]);
2783 AssertValue ("ABC", invoke_results [0]);
2784 AssertValue (42, invoke_results [1]);
2788 void invoke_multiple_cb (IAsyncResult ar) {
2789 ObjectMirror this_obj = (ObjectMirror)ar.AsyncState;
2791 var res = this_obj.EndInvokeMethod (ar);
2792 lock (invoke_results)
2793 invoke_results.Add (res);
2797 public void InvokeAbort () {
2800 Start (new string [] { "dtest-app.exe", "invoke-abort" });
2802 Event e = run_until ("invoke_abort");
2804 StackFrame f = e.Thread.GetFrames ()[0];
2806 var obj = f.GetThis () as ObjectMirror;
2808 var m = t.GetMethod ("invoke_abort_2");
2809 // Invoke multiple times to check that the subsequent invokes are aborted too
2810 var res = (IInvokeAsyncResult)obj.BeginInvokeMultiple (e.Thread, new MethodMirror[] { m, m, m, m }, null, InvokeOptions.None, delegate { }, null);
2813 AssertThrows<CommandException> (delegate {
2814 obj.EndInvokeMethod (res);
2819 public void GetThreads () {
2824 public void Threads () {
2825 Event e = run_until ("threads");
2827 Assert.AreEqual (ThreadState.Running, e.Thread.ThreadState);
2829 Assert.IsTrue (e.Thread.ThreadId > 0);
2831 Assert.AreEqual (e.Thread.TID, e.Thread.TID);
2833 vm.EnableEvents (EventType.ThreadStart, EventType.ThreadDeath);
2837 e = GetNextEvent ();
2838 Assert.IsInstanceOfType (typeof (ThreadStartEvent), e);
2839 var state = e.Thread.ThreadState;
2840 Assert.IsTrue (state == ThreadState.Running || state == ThreadState.Unstarted);
2844 e = GetNextEvent ();
2845 Assert.IsInstanceOfType (typeof (ThreadDeathEvent), e);
2846 Assert.AreEqual (ThreadState.Stopped, e.Thread.ThreadState);
2850 public void Frame_SetValue () {
2851 Event e = run_until ("locals2");
2853 StackFrame frame = e.Thread.GetFrames () [0];
2856 var l = frame.Method.GetLocal ("i");
2857 frame.SetValue (l, vm.CreateValue ((long)55));
2858 AssertValue (55, frame.GetValue (l));
2861 l = frame.Method.GetLocal ("s");
2862 frame.SetValue (l, vm.RootDomain.CreateString ("DEF"));
2863 AssertValue ("DEF", frame.GetValue (l));
2865 // argument as local
2866 l = frame.Method.GetLocal ("arg");
2867 frame.SetValue (l, vm.CreateValue (6));
2868 AssertValue (6, frame.GetValue (l));
2871 var p = frame.Method.GetParameters ()[1];
2872 frame.SetValue (p, vm.CreateValue (7));
2873 AssertValue (7, frame.GetValue (p));
2876 p = frame.Method.GetParameters ()[2];
2877 frame.SetValue (p, vm.RootDomain.CreateString ("DEF"));
2878 AssertValue ("DEF", frame.GetValue (p));
2881 p = frame.Method.GetParameters ()[3];
2882 frame.SetValue (p, vm.RootDomain.CreateString ("DEF2"));
2883 AssertValue ("DEF2", frame.GetValue (p));
2886 p = frame.Method.GetParameters ()[4];
2887 var v = frame.GetValue (p) as StructMirror;
2888 v ["i"] = vm.CreateValue (43);
2889 frame.SetValue (p, v);
2890 v = frame.GetValue (p) as StructMirror;
2891 AssertValue (43, v ["i"]);
2893 // argument checking
2896 AssertThrows<ArgumentNullException> (delegate () {
2897 frame.SetValue ((LocalVariable)null, vm.CreateValue (55));
2901 AssertThrows<ArgumentNullException> (delegate () {
2902 l = frame.Method.GetLocal ("i");
2903 frame.SetValue (l, null);
2906 // value of invalid type
2907 AssertThrows<ArgumentException> (delegate () {
2908 l = frame.Method.GetLocal ("i");
2909 frame.SetValue (l, vm.CreateValue (55));
2915 public void Frame_SetValue_Registers () {
2916 Event e = run_until ("locals6_1");
2918 StackFrame frame = e.Thread.GetFrames () [1];
2921 var l = frame.Method.GetLocal ("j");
2922 frame.SetValue (l, vm.CreateValue (99));
2923 AssertValue (99, frame.GetValue (l));
2925 // Check it during execution
2926 e = run_until ("locals6_2");
2927 frame = e.Thread.GetFrames () [0];
2928 AssertValue (99, frame.GetValue (frame.Method.GetParameters ()[0]));
2930 // Set it while in a frame which clobbers its register
2931 e = run_until ("locals6_3");
2932 frame = e.Thread.GetFrames () [1];
2933 frame.SetValue (l, vm.CreateValue (100));
2934 AssertValue (100, frame.GetValue (l));
2936 // Check it during execution
2937 e = run_until ("locals6_4");
2938 frame = e.Thread.GetFrames () [0];
2939 AssertValue (100, frame.GetValue (frame.Method.GetParameters ()[0]));
2941 // Signed byte value
2942 e = run_until ("locals6_5");
2943 frame = e.Thread.GetFrames () [1];
2944 var l2 = frame.Method.GetLocal ("sb");
2945 frame.SetValue (l2, vm.CreateValue ((sbyte)-99));
2946 AssertValue (-99, frame.GetValue (l2));
2948 // Check it during execution
2949 e = run_until ("locals6_6");
2950 frame = e.Thread.GetFrames () [0];
2951 AssertValue (-99, frame.GetValue (frame.Method.GetParameters ()[0]));
2955 public void InvokeRegress () {
2956 Event e = run_until ("invoke1");
2958 StackFrame frame = e.Thread.GetFrames () [0];
2960 TypeMirror t = frame.Method.DeclaringType;
2961 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2963 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2969 m = t.GetMethod ("invoke_return_void");
2970 v = this_obj.InvokeMethod (e.Thread, m, null);
2973 // Check that the stack frames remain valid during the invoke
2974 Assert.AreEqual ("Tests", (frame.GetThis () as ObjectMirror).Type.Name);
2976 // do another invoke
2977 m = t.GetMethod ("invoke_return_void");
2978 v = this_obj.InvokeMethod (e.Thread, m, null);
2981 // Try a single step after the invoke
2982 var req = create_step (e);
2983 req.Depth = StepDepth.Into;
2984 req.Size = StepSize.Line;
2990 // Step into invoke2
2992 e = GetNextEvent ();
2993 Assert.IsTrue (e is StepEvent);
2994 Assert.AreEqual ("invoke2", (e as StepEvent).Method.Name);
2998 frame = e.Thread.GetFrames () [0];
3002 public void Exceptions () {
3003 Event e = run_until ("exceptions");
3004 var req = vm.CreateExceptionRequest (null);
3009 e = GetNextEvent ();
3010 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3011 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
3013 var frames = e.Thread.GetFrames ();
3014 Assert.AreEqual ("exceptions", frames [0].Method.Name);
3017 // exception type filter
3019 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.ArgumentException"));
3022 // Skip the throwing of the second OverflowException
3025 e = GetNextEvent ();
3026 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3027 Assert.AreEqual ("ArgumentException", (e as ExceptionEvent).Exception.Type.Name);
3030 // exception type filter for subclasses
3031 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.Exception"));
3036 e = GetNextEvent ();
3037 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3038 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
3042 req.IncludeSubclasses = false;
3047 e = GetNextEvent ();
3048 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3049 Assert.AreEqual ("Exception", (e as ExceptionEvent).Exception.Type.Name);
3052 // Implicit exceptions
3053 req = vm.CreateExceptionRequest (null);
3058 e = GetNextEvent ();
3059 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3060 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
3063 // Single stepping after an exception
3064 req = vm.CreateExceptionRequest (null);
3069 e = GetNextEvent ();
3070 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3071 Assert.AreEqual ("Exception", (e as ExceptionEvent).Exception.Type.Name);
3072 frames = e.Thread.GetFrames ();
3073 Assert.AreEqual ("exceptions2", frames [0].Method.Name);
3076 var sreq = create_step (e);
3077 sreq.Depth = StepDepth.Over;
3078 sreq.Size = StepSize.Line;
3082 e = GetNextEvent ();
3083 Assert.IsInstanceOfType (typeof (StepEvent), e);
3084 frames = e.Thread.GetFrames ();
3085 Assert.AreEqual ("exceptions", frames [0].Method.Name);
3088 // Argument checking
3089 AssertThrows<ArgumentException> (delegate {
3090 vm.CreateExceptionRequest (e.Thread.Type);
3095 public void ExceptionFilter () {
3096 Event e = run_until ("exception_filter");
3098 MethodMirror m = entry_point.DeclaringType.GetMethod ("exception_filter_filter");
3099 Assert.IsNotNull (m);
3101 vm.SetBreakpoint (m, 0);
3105 e = GetNextEvent ();
3106 Assert.AreEqual (EventType.Breakpoint, e.EventType);
3107 Assert.IsTrue (e is BreakpointEvent);
3108 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
3110 var frames = e.Thread.GetFrames ();
3112 Assert.IsTrue (frames [0].Location.SourceFile.IndexOf ("dtest-app.cs") != -1);
3113 Assert.AreEqual ("exception_filter_filter", frames [0].Location.Method.Name);
3115 Assert.AreEqual (0, frames [1].Location.Method.MetadataToken);
3116 Assert.AreEqual (0x0f, frames [1].Location.ILOffset);
3118 Assert.AreEqual ("exception_filter_method", frames [2].Location.Method.Name);
3119 Assert.AreEqual (0x06, frames [2].Location.ILOffset);
3121 Assert.AreEqual (0, frames [3].Location.Method.MetadataToken, 0);
3122 Assert.AreEqual (0, frames [3].Location.ILOffset);
3124 Assert.AreEqual ("exception_filter", frames [4].Location.Method.Name);
3128 public void ExceptionFilter2 () {
3131 Start (new string [] { "dtest-excfilter.exe" });
3133 MethodMirror filter_method = entry_point.DeclaringType.GetMethod ("Filter");
3134 Assert.IsNotNull (filter_method);
3136 MethodMirror test_method = entry_point.DeclaringType.GetMethod ("Test");
3137 Assert.IsNotNull (test_method);
3139 vm.SetBreakpoint (filter_method, 0);
3143 var e = GetNextEvent ();
3144 Assert.AreEqual (EventType.Breakpoint, e.EventType);
3145 Assert.IsTrue (e is BreakpointEvent);
3146 Assert.AreEqual (filter_method.Name, (e as BreakpointEvent).Method.Name);
3148 var frames = e.Thread.GetFrames ();
3150 Assert.AreEqual (4, frames.Count ());
3152 Assert.AreEqual (filter_method.Name, frames [0].Location.Method.Name);
3153 Assert.AreEqual (20, frames [0].Location.LineNumber);
3154 Assert.AreEqual (0, frames [0].Location.ILOffset);
3156 Assert.AreEqual (test_method.Name, frames [1].Location.Method.Name);
3157 Assert.AreEqual (37, frames [1].Location.LineNumber);
3158 Assert.AreEqual (0x0b, frames [1].Location.ILOffset);
3160 Assert.AreEqual (test_method.Name, frames [2].Location.Method.Name);
3161 Assert.AreEqual (33, frames [2].Location.LineNumber);
3162 Assert.AreEqual (0x05, frames [2].Location.ILOffset);
3164 Assert.AreEqual (entry_point.Name, frames [3].Location.Method.Name);
3165 Assert.AreEqual (14, frames [3].Location.LineNumber);
3166 Assert.AreEqual (0x00, frames [3].Location.ILOffset);
3174 public void EventSets () {
3176 // Create two filter which both match the same exception
3178 Event e = run_until ("exceptions");
3180 var req = vm.CreateExceptionRequest (null);
3183 var req2 = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.OverflowException"));
3188 var es = vm.GetNextEventSet ();
3189 Assert.AreEqual (2, es.Events.Length);
3192 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3193 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
3196 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3197 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
3204 // Test single threaded invokes during processing of nullref exceptions.
3205 // These won't work if the exception handling is done from the sigsegv signal
3206 // handler, since the sigsegv signal is disabled until control returns from the
3210 [Category ("only3")]
3211 public void NullRefExceptionAndSingleThreadedInvoke () {
3212 Event e = run_until ("exceptions");
3213 var req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.NullReferenceException"));
3218 e = GetNextEvent ();
3219 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3220 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
3222 var ex = (e as ExceptionEvent).Exception;
3223 var tostring_method = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
3224 ex.InvokeMethod (e.Thread, tostring_method, null, InvokeOptions.SingleThreaded);
3228 public void MemberInOtherDomain () {
3231 Start (new string [] { "dtest-app.exe", "domain-test" });
3233 vm.EnableEvents (EventType.AppDomainCreate, EventType.AppDomainUnload, EventType.AssemblyUnload);
3235 Event e = run_until ("domains_print_across");
3237 var frame = e.Thread.GetFrames ()[0];
3238 var inOtherDomain = frame.GetArgument (0) as ObjectMirror;
3239 var crossDomainField = (ObjectMirror) inOtherDomain.GetValue (inOtherDomain.Type.GetField("printMe"));
3240 Assert.AreEqual ("SentinelClass", crossDomainField.Type.Name);
3244 public void Domains () {
3247 Start (new string [] { "dtest-app.exe", "domain-test" });
3249 vm.EnableEvents (EventType.AppDomainCreate, EventType.AppDomainUnload, EventType.AssemblyUnload);
3251 Event e = run_until ("domains");
3255 e = GetNextEvent ();
3256 Assert.IsInstanceOfType (typeof (AppDomainCreateEvent), e);
3258 var domain = (e as AppDomainCreateEvent).Domain;
3260 // Check the object type
3261 e = run_until ("domains_2");
3262 var frame = e.Thread.GetFrames ()[0];
3263 var o = frame.GetArgument (0) as ObjectMirror;
3264 Assert.AreEqual ("CrossDomain", o.Type.Name);
3266 // Do a remoting invoke
3267 var cross_domain_type = o.Type;
3268 var v = o.InvokeMethod (e.Thread, cross_domain_type.GetMethod ("invoke_3"), null);
3269 AssertValue (42, v);
3271 // Run until the callback in the domain
3272 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_in_domain");
3273 Assert.IsNotNull (m);
3274 vm.SetBreakpoint (m, 0);
3278 e = GetNextEvent ();
3279 if (e is BreakpointEvent)
3283 Assert.AreEqual ("invoke_in_domain", (e as BreakpointEvent).Method.Name);
3285 // d_method is from another domain
3286 MethodMirror d_method = (e as BreakpointEvent).Method;
3287 Assert.IsTrue (m != d_method);
3289 var frames = e.Thread.GetFrames ();
3290 Assert.AreEqual ("invoke_in_domain", frames [0].Method.Name);
3291 Assert.AreEqual (domain, frames [0].Domain);
3292 Assert.AreEqual ("invoke", frames [1].Method.Name);
3293 Assert.AreEqual ("domains", frames [2].Method.Name);
3294 Assert.AreEqual (vm.RootDomain, frames [2].Domain);
3296 // Test breakpoints on already JITted methods in other domains
3297 m = entry_point.DeclaringType.GetMethod ("invoke_in_domain_2");
3298 Assert.IsNotNull (m);
3299 vm.SetBreakpoint (m, 0);
3303 e = GetNextEvent ();
3304 if (e is BreakpointEvent)
3308 Assert.AreEqual ("invoke_in_domain_2", (e as BreakpointEvent).Method.Name);
3310 // This is empty when receiving the AppDomainCreateEvent
3311 Assert.AreEqual ("domain", domain.FriendlyName);
3313 // Run until the unload
3316 e = GetNextEvent ();
3317 if (e is AssemblyUnloadEvent) {
3323 Assert.IsInstanceOfType (typeof (AppDomainUnloadEvent), e);
3324 Assert.AreEqual (domain, (e as AppDomainUnloadEvent).Domain);
3326 // Run past the unload
3327 e = run_until ("domains_3");
3329 // Test access to unloaded types
3330 // FIXME: Add an exception type for this
3331 AssertThrows<Exception> (delegate {
3332 d_method.DeclaringType.GetValue (d_method.DeclaringType.GetField ("static_i"));
3335 // Check that .Domain is accessible for stack frames with native transitions
3336 e = run_until ("called_from_invoke");
3337 ThreadMirror.NativeTransitions = true;
3338 foreach (var f in e.Thread.GetFrames ()) {
3344 public void DynamicMethods () {
3345 Event e = run_until ("dyn_call");
3347 var m = e.Thread.GetFrames ()[1].Method;
3348 Assert.AreEqual ("dyn_method", m.Name);
3350 // Test access to IL
3351 var body = m.GetMethodBody ();
3353 ILInstruction ins = body.Instructions [0];
3354 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
3355 Assert.AreEqual ("FOO", ins.Operand);
3359 public void RefEmit () {
3362 Start (new string [] { "dtest-app.exe", "ref-emit-test" });
3364 Event e = run_until ("ref_emit_call");
3366 var m = e.Thread.GetFrames ()[1].Method;
3367 Assert.AreEqual ("ref_emit_method", m.Name);
3369 // Test access to IL
3370 var body = m.GetMethodBody ();
3374 ins = body.Instructions [0];
3375 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
3376 Assert.AreEqual ("FOO", ins.Operand);
3378 ins = body.Instructions [1];
3379 Assert.AreEqual (OpCodes.Call, ins.OpCode);
3380 Assert.IsInstanceOfType (typeof (MethodMirror), ins.Operand);
3381 Assert.AreEqual ("ref_emit_call", (ins.Operand as MethodMirror).Name);
3385 public void IsAttached () {
3386 var f = entry_point.DeclaringType.GetField ("is_attached");
3388 Event e = run_until ("Main");
3390 AssertValue (true, entry_point.DeclaringType.GetValue (f));
3394 public void StackTraceInNative () {
3395 // Check that stack traces can be produced for threads in native code
3398 Start (new string [] { "dtest-app.exe", "frames-in-native" });
3400 var e = run_until ("frames_in_native");
3402 // FIXME: This is racy
3409 StackFrame[] frames = e.Thread.GetFrames ();
3411 int frame_index = -1;
3412 for (int i = 0; i < frames.Length; ++i) {
3413 if (frames [i].Method.Name == "Sleep") {
3419 Assert.IsTrue (frame_index != -1);
3420 Assert.AreEqual ("Sleep", frames [frame_index].Method.Name);
3421 Assert.AreEqual ("frames_in_native", frames [frame_index + 1].Method.Name);
3422 Assert.AreEqual ("Main", frames [frame_index + 2].Method.Name);
3424 // Check that invokes are disabled for such threads
3425 TypeMirror t = frames [frame_index + 1].Method.DeclaringType;
3427 var m = t.GetMethod ("invoke_static_return_void");
3428 AssertThrows<InvalidOperationException> (delegate {
3429 t.InvokeMethod (e.Thread, m, null);
3432 // Check that the frame info is invalidated
3433 run_until ("frames_in_native_2");
3435 AssertThrows<InvalidStackFrameException> (delegate {
3436 Console.WriteLine (frames [frame_index].GetThis ());
3441 public void VirtualMachine_CreateEnumMirror () {
3442 var e = run_until ("o1");
3443 var frame = e.Thread.GetFrames () [0];
3445 object val = frame.GetThis ();
3446 Assert.IsTrue (val is ObjectMirror);
3447 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
3448 ObjectMirror o = (val as ObjectMirror);
3450 FieldInfoMirror field = o.Type.GetField ("field_enum");
3451 Value f = o.GetValue (field);
3452 TypeMirror enumType = (f as EnumMirror).Type;
3454 o.SetValue (field, vm.CreateEnumMirror (enumType, vm.CreateValue (1)));
3455 f = o.GetValue (field);
3456 Assert.AreEqual (1, (f as EnumMirror).Value);
3458 // Argument checking
3459 AssertThrows<ArgumentNullException> (delegate () {
3460 vm.CreateEnumMirror (enumType, null);
3463 AssertThrows<ArgumentNullException> (delegate () {
3464 vm.CreateEnumMirror (null, vm.CreateValue (1));
3468 AssertThrows<ArgumentException> (delegate () {
3469 vm.CreateEnumMirror (enumType, vm.CreateValue (null));
3472 // value of a wrong type
3473 AssertThrows<ArgumentException> (delegate () {
3474 vm.CreateEnumMirror (enumType, vm.CreateValue ((long)1));
3479 public void VirtualMachine_EnableEvents_Breakpoint () {
3480 AssertThrows<ArgumentException> (delegate () {
3481 vm.EnableEvents (EventType.Breakpoint);
3486 public void SingleStepRegress654694 () {
3489 MethodMirror m = entry_point.DeclaringType.GetMethod ("ss_regress_654694");
3490 foreach (Location l in m.Locations) {
3491 if (l.ILOffset > 0 && il_offset == -1)
3492 il_offset = l.ILOffset;
3495 Event e = run_until ("ss_regress_654694");
3497 Assert.IsNotNull (m);
3498 vm.SetBreakpoint (m, il_offset);
3502 e = GetNextEvent ();
3503 Assert.IsTrue (e is BreakpointEvent);
3505 var req = create_step (e);
3506 req.Depth = StepDepth.Over;
3507 req.Size = StepSize.Line;
3512 e = GetNextEvent ();
3513 Assert.IsTrue (e is StepEvent);
3519 public void DebugBreak () {
3520 vm.EnableEvents (EventType.UserBreak);
3525 var e = GetNextEvent ();
3526 Assert.IsTrue (e is UserBreakEvent);
3530 public void DebugLog () {
3531 vm.EnableEvents (EventType.UserLog);
3536 var e = GetNextEvent ();
3537 Assert.IsTrue (e is UserLogEvent);
3538 var le = e as UserLogEvent;
3540 Assert.AreEqual (5, le.Level);
3541 Assert.AreEqual ("A", le.Category);
3542 Assert.AreEqual ("B", le.Message);
3546 public void TypeGetMethodsByNameFlags () {
3548 var assembly = entry_point.DeclaringType.Assembly;
3549 var type = assembly.GetType ("Tests3");
3551 Assert.IsNotNull (type);
3553 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Public, false);
3554 Assert.AreEqual (1, mm.Length, "#1");
3555 Assert.AreEqual ("M1", mm[0].Name, "#2");
3557 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.NonPublic, false);
3558 Assert.AreEqual (1, mm.Length, "#3");
3559 Assert.AreEqual ("M2", mm[0].Name, "#4");
3561 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public, false);
3562 Assert.AreEqual (7, mm.Length, "#5"); //M3 plus Equals, GetHashCode, GetType, ToString, .ctor
3564 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, false);
3565 Assert.AreEqual (2, mm.Length, "#7");
3567 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
3568 Assert.AreEqual (1, mm.Length, "#9");
3570 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
3571 Assert.AreEqual (5, mm.Length, "#11");
3574 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Public, false);
3575 Assert.AreEqual (1, mm.Length, "#12");
3576 Assert.AreEqual ("M1", mm[0].Name, "#13");
3578 mm = type.GetMethodsByNameFlags ("m1", BindingFlags.Static | BindingFlags.Public, true);
3579 Assert.AreEqual (1, mm.Length, "#14");
3580 Assert.AreEqual ("M1", mm[0].Name, "#15");
3582 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, false);
3583 Assert.AreEqual (1, mm.Length, "#16");
3584 Assert.AreEqual ("M1", mm[0].Name, "#17");
3588 [Category ("only88")]
3589 public void TypeLoadSourceFileFilter () {
3590 Event e = run_until ("type_load");
3592 if (!vm.Version.AtLeast (2, 7))
3595 string srcfile = (e as BreakpointEvent).Method.DeclaringType.GetSourceFiles (true)[0];
3597 var req = vm.CreateTypeLoadRequest ();
3598 req.SourceFileFilter = new string [] { srcfile.ToUpper () };
3602 e = GetNextEvent ();
3603 Assert.IsTrue (e is TypeLoadEvent);
3604 Assert.AreEqual ("TypeLoadClass", (e as TypeLoadEvent).Type.FullName);
3608 public void TypeLoadTypeNameFilter () {
3609 Event e = run_until ("type_load");
3611 var req = vm.CreateTypeLoadRequest ();
3612 req.TypeNameFilter = new string [] { "TypeLoadClass2" };
3616 e = GetNextEvent ();
3617 Assert.IsTrue (e is TypeLoadEvent);
3618 Assert.AreEqual ("TypeLoadClass2", (e as TypeLoadEvent).Type.FullName);
3622 public void GetTypesForSourceFile () {
3625 var types = vm.GetTypesForSourceFile ("dtest-app.cs", false);
3626 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
3627 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
3629 types = vm.GetTypesForSourceFile ("DTEST-app.cs", true);
3630 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
3631 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
3635 public void GetTypesNamed () {
3638 var types = vm.GetTypes ("Tests", false);
3639 Assert.AreEqual (1, types.Count);
3640 Assert.AreEqual ("Tests", types [0].FullName);
3642 types = vm.GetTypes ("System.Exception", false);
3643 Assert.AreEqual (1, types.Count);
3644 Assert.AreEqual ("System.Exception", types [0].FullName);
3648 public void String_GetValue () {
3653 var e = run_until ("arg2");
3655 var frame = e.Thread.GetFrames () [0];
3657 val = frame.GetArgument (6);
3658 Assert.AreEqual ('\0'.ToString () + "A", (val as StringMirror).Value);
3662 public void String_GetChars () {
3666 var e = run_until ("arg2");
3668 var frame = e.Thread.GetFrames () [0];
3670 val = frame.GetArgument (0);
3671 Assert.IsTrue (val is StringMirror);
3672 AssertValue ("FOO", val);
3673 var s = (val as StringMirror);
3674 Assert.AreEqual (3, s.Length);
3676 var c = s.GetChars (0, 2);
3677 Assert.AreEqual (2, c.Length);
3678 Assert.AreEqual ('F', c [0]);
3679 Assert.AreEqual ('O', c [1]);
3681 AssertThrows<ArgumentException> (delegate () {
3687 public void GetInterfaces () {
3688 var e = run_until ("arg2");
3690 var frame = e.Thread.GetFrames () [0];
3692 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
3693 var ifaces = cl1.GetInterfaces ();
3694 Assert.AreEqual (1, ifaces.Length);
3695 Assert.AreEqual ("ITest", ifaces [0].Name);
3697 var cl2 = cl1.GetMethod ("Baz").ReturnType;
3698 var ifaces2 = cl2.GetInterfaces ();
3699 Assert.AreEqual (1, ifaces2.Length);
3700 Assert.AreEqual ("ITest`1", ifaces2 [0].Name);
3704 public void GetInterfaceMap () {
3705 var e = run_until ("arg2");
3707 var frame = e.Thread.GetFrames () [0];
3709 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
3710 var iface = cl1.Assembly.GetType ("ITest");
3711 var map = cl1.GetInterfaceMap (iface);
3712 Assert.AreEqual (cl1, map.TargetType);
3713 Assert.AreEqual (iface, map.InterfaceType);
3714 Assert.AreEqual (2, map.InterfaceMethods.Length);
3715 Assert.AreEqual (2, map.TargetMethods.Length);
3719 public void StackAlloc_Breakpoints_Regress2775 () {
3720 // Check that breakpoints on arm don't overwrite stackalloc-ed memory
3721 var e = run_until ("regress_2755");
3723 var frame = e.Thread.GetFrames () [0];
3725 // This breaks at the call site
3726 vm.SetBreakpoint (m, m.Locations [2].ILOffset);
3729 var e2 = GetNextEvent ();
3730 Assert.IsTrue (e2 is BreakpointEvent);
3732 e = run_until ("regress_2755_3");
3733 frame = e.Thread.GetFrames () [1];
3734 var res = frame.GetValue (m.GetLocal ("sum"));
3735 AssertValue (0, res);
3739 public void MethodInfo () {
3740 Event e = run_until ("locals2");
3742 StackFrame frame = e.Thread.GetFrames () [0];
3743 var m = frame.Method;
3745 Assert.IsTrue (m.IsGenericMethod);
3746 Assert.IsFalse (m.IsGenericMethodDefinition);
3748 var args = m.GetGenericArguments ();
3749 Assert.AreEqual (1, args.Length);
3750 Assert.AreEqual ("String", args [0].Name);
3752 var gmd = m.GetGenericMethodDefinition ();
3753 Assert.IsTrue (gmd.IsGenericMethod);
3754 Assert.IsTrue (gmd.IsGenericMethodDefinition);
3755 Assert.AreEqual (gmd, gmd.GetGenericMethodDefinition ());
3757 args = gmd.GetGenericArguments ();
3758 Assert.AreEqual (1, args.Length);
3759 Assert.AreEqual ("T", args [0].Name);
3761 var attrs = m.GetCustomAttributes (true);
3762 Assert.AreEqual (1, attrs.Length);
3763 Assert.AreEqual ("StateMachineAttribute", attrs [0].Constructor.DeclaringType.Name);
3767 public void UnhandledException () {
3770 Start (new string [] { "dtest-app.exe", "unhandled-exception" });
3772 var req = vm.CreateExceptionRequest (null, false, true);
3775 var e = run_until ("unhandled_exception");
3778 var e2 = GetNextEvent ();
3779 Assert.IsTrue (e2 is ExceptionEvent);
3786 public void UnhandledException_2 () {
3789 Start (new string [] { "dtest-app.exe", "unhandled-exception-endinvoke" });
3791 var req = vm.CreateExceptionRequest (null, false, true);
3794 MethodMirror m = entry_point.DeclaringType.GetMethod ("unhandled_exception_endinvoke_2");
3795 Assert.IsNotNull (m);
3796 vm.SetBreakpoint (m, m.ILOffsets [0]);
3798 var e = run_until ("unhandled_exception_endinvoke");
3801 var e2 = GetNextEvent ();
3802 Assert.IsFalse (e2 is ExceptionEvent);
3809 public void UnhandledExceptionUserCode () {
3812 // Exceptions caught in non-user code are treated as unhandled
3813 Start (new string [] { "dtest-app.exe", "unhandled-exception-user" });
3815 var req = vm.CreateExceptionRequest (null, false, true);
3816 req.AssemblyFilter = new List<AssemblyMirror> () { entry_point.DeclaringType.Assembly };
3819 var e = run_until ("unhandled_exception_user");
3822 var e2 = GetNextEvent ();
3823 Assert.IsTrue (e2 is ExceptionEvent);
3830 public void GCWhileSuspended () {
3831 // Check that objects are kept alive during suspensions
3832 Event e = run_until ("gc_suspend_1");
3834 MethodMirror m = entry_point.DeclaringType.GetMethod ("gc_suspend_invoke");
3836 var o = entry_point.DeclaringType.GetValue (entry_point.DeclaringType.GetField ("gc_suspend_field")) as ObjectMirror;
3837 //Console.WriteLine (o);
3839 StackFrame frame = e.Thread.GetFrames () [0];
3840 TypeMirror t = frame.Method.DeclaringType;
3841 for (int i = 0; i < 10; ++i)
3842 t.InvokeMethod (e.Thread, m, new Value [] { });
3844 // This throws an exception if the object is collected
3845 long addr = o.Address;
3847 var o2 = entry_point.DeclaringType.GetValue (entry_point.DeclaringType.GetField ("gc_suspend_field")) as ObjectMirror;
3852 public void MakeGenericMethod () {
3853 Event e = run_until ("bp1");
3855 var intm = vm.RootDomain.GetCorrespondingType (typeof (int));
3856 var stringm = vm.RootDomain.GetCorrespondingType (typeof (string));
3857 var gm = entry_point.DeclaringType.GetMethod ("generic_method");
3858 var res = gm.MakeGenericMethod (new TypeMirror [] { stringm });
3859 var args = res.GetGenericArguments ();
3860 Assert.AreEqual (1, args.Length);
3861 Assert.AreEqual (stringm, args [0]);
3864 AssertThrows<ArgumentNullException> (delegate {
3865 gm.MakeGenericMethod (null);
3867 AssertThrows<ArgumentNullException> (delegate {
3868 gm.MakeGenericMethod (new TypeMirror [] { null });
3870 AssertThrows<ArgumentException> (delegate {
3871 gm.MakeGenericMethod (new TypeMirror [] { stringm, stringm });
3873 AssertThrows<InvalidOperationException> (delegate {
3874 gm.MakeGenericMethod (new TypeMirror [] { intm });
3876 AssertThrows<InvalidOperationException> (delegate {
3877 entry_point.DeclaringType.GetMethod ("Main").MakeGenericMethod (new TypeMirror [] { intm });
3882 public void InspectThreadSuspenedOnWaitOne () {
3884 Start (true, "dtest-app.exe", "wait-one" );
3886 ThreadMirror.NativeTransitions = true;
3888 var evt = run_until ("wait_one");
3889 Assert.IsNotNull (evt, "#1");
3891 var thread = evt.Thread;
3892 Assert.AreEqual (ThreadState.Running, thread.ThreadState, "#1.1");
3894 var frames = thread.GetFrames ();
3895 Assert.IsNotNull (frames, "#2");
3896 Assert.AreEqual (2, frames.Length, "#3");
3897 Assert.AreEqual ("wait_one", frames [0].Method.Name, "#4");
3898 Assert.AreEqual ("Main", frames [1].Method.Name, "#5");
3902 Thread.Sleep (500); //FIXME this is racy, maybe single step? or something?
3905 Assert.AreEqual (ThreadState.WaitSleepJoin, thread.ThreadState, "#6");
3907 frames = thread.GetFrames ();
3908 Assert.AreEqual (8, frames.Length, "#7");
3909 Assert.AreEqual ("WaitOne_internal", frames [0].Method.Name, "#8.0");
3910 Assert.AreEqual ("WaitOneNative", frames [1].Method.Name, "#8.1");
3911 Assert.AreEqual ("InternalWaitOne", frames [2].Method.Name, "#8.2");
3912 Assert.AreEqual ("WaitOne", frames [3].Method.Name, "#8.3");
3913 Assert.AreEqual ("WaitOne", frames [4].Method.Name, "#8.4");
3914 Assert.AreEqual ("WaitOne", frames [5].Method.Name, "#8.5");
3915 Assert.AreEqual ("wait_one", frames [6].Method.Name, "#8.6");
3916 Assert.AreEqual ("Main", frames [7].Method.Name, "#8.7");
3918 var frame = frames [0];
3919 Assert.IsTrue (frame.IsNativeTransition, "#11.1");
3922 Assert.Fail ("Known limitation - can't get info from m2n frames");
3923 } catch (AbsentInformationException) {}
3926 Assert.IsFalse (frame.IsNativeTransition, "#12.1");
3927 var wait_one_this = frame.GetThis ();
3928 Assert.IsNotNull (wait_one_this, "#12.2");
3931 var locals = frame.GetVisibleVariables ();
3932 Assert.AreEqual (1, locals.Count, "#13.1");
3934 var local_0 = frame.GetValue (locals [0]);
3935 Assert.IsNotNull (local_0, "#13.2");
3937 Assert.AreEqual (wait_one_this, local_0, "#14.2");
3941 public void GetMethodBody () {
3942 var bevent = run_until ("Main");
3944 var m = bevent.Method.DeclaringType.GetMethod ("get_IntProperty");
3945 var body = m.GetMethodBody ();
3946 foreach (var ins in body.Instructions) {
3947 if (ins.OpCode == OpCodes.Ldfld) {
3948 var field = (FieldInfoMirror)ins.Operand;
3949 Assert.AreEqual ("field_i", field.Name);
3955 public void EvaluateMethod () {
3956 var bevent = run_until ("evaluate_method_2");
3958 var m = bevent.Method.DeclaringType.GetMethod ("get_IntProperty");
3960 var this_obj = bevent.Thread.GetFrames ()[0].GetThis ();
3961 var v = m.Evaluate (this_obj, null);
3962 AssertValue (42, v);
3966 public void SetIP () {
3967 var bevent = run_until ("set_ip_1");
3969 var invalid_loc = bevent.Thread.GetFrames ()[0].Location;
3971 var req = create_step (bevent);
3972 var e = step_out ();
3974 var frames = e.Thread.GetFrames ();
3975 var locs = frames [0].Method.Locations;
3976 var next_loc = locs.First (l => (l.LineNumber == frames [0].Location.LineNumber + 2));
3978 e.Thread.SetIP (next_loc);
3980 /* Check that i ++; j = 5; was skipped */
3981 bevent = run_until ("set_ip_2");
3982 var f = bevent.Thread.GetFrames ()[1];
3983 AssertValue (2, f.GetValue (f.Method.GetLocal ("i")));
3984 AssertValue (0, f.GetValue (f.Method.GetLocal ("j")));
3987 AssertThrows<ArgumentNullException> (delegate {
3988 e.Thread.SetIP (null);
3991 AssertThrows<ArgumentException> (delegate {
3992 e.Thread.SetIP (invalid_loc);
3997 public void SetIPSingleStep () {
3998 // Check that single stepping after set-ip steps from the new ip
3999 var bevent = run_until ("set_ip_1");
4001 var invalid_loc = bevent.Thread.GetFrames ()[0].Location;
4003 var req = create_step (bevent);
4004 req.Size = StepSize.Line;
4005 var e = step_out ();
4007 var frames = e.Thread.GetFrames ();
4008 var locs = frames [0].Method.Locations;
4009 var prev_loc = locs.First (l => (l.LineNumber == frames [0].Location.LineNumber - 3));
4010 AssertValue (2, frames [0].GetValue (frames [0].Method.GetLocal ("i")));
4012 // Set back the ip to the first i ++; line
4013 e.Thread.SetIP (prev_loc);
4016 var f = e.Thread.GetFrames ()[0];
4017 AssertValue (3, f.GetValue (f.Method.GetLocal ("i")));
4021 public void NewInstanceNoCtor () {
4022 var bevent = run_until ("Main");
4024 var stype = bevent.Method.DeclaringType.Assembly.GetType ("AStruct");
4025 var obj = stype.NewInstance ();
4026 Assert.IsTrue (obj is ObjectMirror);
4027 Assert.AreEqual ("AStruct", (obj as ObjectMirror).Type.Name);
4031 public void StaticCtorFilterInCctor () {
4032 // Check that single stepping when in a cctor only ignores
4033 // other cctors, not the current one
4034 var bevent = run_until ("step_filters");
4036 var assembly = entry_point.DeclaringType.Assembly;
4037 var type = assembly.GetType ("Tests/ClassWithCctor");
4038 var cctor = type.GetMethod (".cctor");
4039 vm.SetBreakpoint (cctor, 0);
4042 var e = vm.GetNextEvent ();
4043 Assert.IsTrue (e is BreakpointEvent);
4045 var req = create_step (e);
4046 req.Filter = StepFilter.StaticCtor;
4048 // Make sure we are still in the cctor
4049 Assert.AreEqual (".cctor", e.Thread.GetFrames ()[0].Location.Method.Name);
4053 public void ThreadpoolIOsinglestep () {
4055 Start ("dtest-app.exe", "threadpool-io");
4056 // This is a regression test for #42625. It tests the
4057 // interaction (particularly in coop GC) of the
4058 // threadpool I/O mechanism and the soft debugger.
4059 Event e = run_until ("threadpool_io");
4060 // run until we sent the task half the bytes it
4061 // expects, so that it blocks waiting for the rest.
4062 e = run_until ("threadpool_bp");
4063 var req = create_step (e);
4064 e = step_out (); // leave threadpool_bp
4065 e = step_out (); // leave threadpool_io