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"]);
1778 // this on static vtype methods
1779 e = run_until ("vtypes3");
1780 e = step_until (e.Thread, "static_foo");
1782 frame = e.Thread.GetFrames () [0];
1784 Assert.AreEqual ("static_foo", (e as StepEvent).Method.Name);
1785 obj = frame.GetThis ();
1786 AssertValue (null, obj);
1788 // vtypes which reference themselves recursively
1789 e = run_until ("vtypes4_2");
1790 frame = e.Thread.GetFrames () [0];
1792 Assert.IsTrue (frame.GetArgument (0) is StructMirror);
1796 public void AssemblyInfo () {
1797 Event e = run_until ("single_stepping");
1799 StackFrame frame = e.Thread.GetFrames () [0];
1801 var aname = frame.Method.DeclaringType.Assembly.GetName ();
1802 Assert.AreEqual ("dtest-app, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", aname.ToString ());
1804 ModuleMirror m = frame.Method.DeclaringType.Module;
1806 Assert.AreEqual ("dtest-app.exe", m.Name);
1807 Assert.AreEqual ("dtest-app.exe", m.ScopeName);
1808 Assert.IsTrue (m.FullyQualifiedName.IndexOf ("dtest-app.exe") != -1);
1809 Guid guid = m.ModuleVersionId;
1810 Assert.AreEqual (frame.Method.DeclaringType.Assembly, m.Assembly);
1811 Assert.AreEqual (frame.Method.DeclaringType.Assembly.ManifestModule, m);
1813 // This is no longer true on 4.0
1814 //Assert.AreEqual ("Assembly", frame.Method.DeclaringType.Assembly.GetAssemblyObject ().Type.Name);
1816 TypeMirror t = vm.RootDomain.Corlib.GetType ("System.Diagnostics.DebuggerDisplayAttribute");
1817 Assert.AreEqual ("DebuggerDisplayAttribute", t.Name);
1821 public void LocalsInfo () {
1822 Event e = run_until ("locals2");
1824 StackFrame frame = e.Thread.GetFrames () [0];
1826 var locals = frame.Method.GetLocals ();
1827 Assert.AreEqual (9, locals.Length);
1828 for (int i = 0; i < 9; ++i) {
1829 if (locals [i].Name == "args") {
1830 Assert.IsTrue (locals [i].IsArg);
1831 Assert.AreEqual ("String[]", locals [i].Type.Name);
1832 } else if (locals [i].Name == "arg") {
1833 Assert.IsTrue (locals [i].IsArg);
1834 Assert.AreEqual ("Int32", locals [i].Type.Name);
1835 } else if (locals [i].Name == "i") {
1836 Assert.IsFalse (locals [i].IsArg);
1837 Assert.AreEqual ("Int64", locals [i].Type.Name);
1838 } else if (locals [i].Name == "j") {
1839 Assert.IsFalse (locals [i].IsArg);
1840 Assert.AreEqual ("Int32", locals [i].Type.Name);
1841 } else if (locals [i].Name == "s") {
1842 Assert.IsFalse (locals [i].IsArg);
1843 Assert.AreEqual ("String", locals [i].Type.Name);
1844 } else if (locals [i].Name == "t") {
1846 Assert.IsTrue (locals [i].IsArg);
1847 Assert.AreEqual ("String", locals [i].Type.Name);
1848 } else if (locals [i].Name == "rs") {
1849 Assert.IsTrue (locals [i].IsArg);
1850 Assert.AreEqual ("String", locals [i].Type.Name);
1851 } else if (locals [i].Name == "astruct") {
1852 } else if (locals [i].Name == "alist") {
1858 var scopes = frame.Method.GetScopes ();
1859 Assert.AreEqual (2, scopes.Length);
1862 Event step_once () {
1864 var e = GetNextEvent ();
1865 Assert.IsTrue (e is StepEvent);
1869 Event step_into () {
1870 step_req.Disable ();
1871 step_req.Depth = StepDepth.Into;
1873 return step_once ();
1876 Event step_over () {
1877 step_req.Disable ();
1878 step_req.Depth = StepDepth.Over;
1880 return step_once ();
1884 step_req.Disable ();
1885 step_req.Depth = StepDepth.Out;
1887 return step_once ();
1890 Event step_once_or_breakpoint () {
1892 var e = GetNextEvent ();
1893 Assert.IsTrue (e is StepEvent || e is BreakpointEvent);
1897 Event step_over_or_breakpoint () {
1898 step_req.Disable ();
1899 step_req.Depth = StepDepth.Over;
1901 return step_once_or_breakpoint ();
1904 Event step_out_or_breakpoint () {
1905 step_req.Disable ();
1906 step_req.Depth = StepDepth.Out;
1908 return step_once_or_breakpoint ();
1912 public void Locals () {
1913 var be = run_until ("locals1");
1915 StackFrame frame = be.Thread.GetFrames () [0];
1916 MethodMirror m1 = frame.Method;
1918 // Compiler generated byref local
1919 foreach (var l in m1.GetLocals ()) {
1920 // The byval flag is hidden from the type
1921 if (l.Name != "ri" && l.Type.Name == "Double")
1922 AssertValue (null, frame.GetValue (l));
1925 be = run_until ("locals2");
1927 frame = be.Thread.GetFrames () [0];
1929 object val = frame.GetValue (frame.Method.GetLocal ("i"));
1930 AssertValue (0, val);
1932 var req = create_step (be);
1939 var e = step_once ();
1940 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1942 // Execute s = "AB";
1944 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1946 frame = e.Thread.GetFrames () [0];
1948 val = frame.GetValue (frame.Method.GetLocal ("i"));
1949 AssertValue (42, val);
1951 LocalVariable[] locals = frame.Method.GetLocals ();
1952 var vals = frame.GetValues (locals);
1953 Assert.AreEqual (locals.Length, vals.Length);
1954 for (int i = 0; i < locals.Length; ++i) {
1955 if (locals [i].Name == "i")
1956 AssertValue (42, vals [i]);
1957 if (locals [i].Name == "s")
1958 AssertValue ("AB", vals [i]);
1959 if (locals [i].Name == "t")
1960 AssertValue ("ABC", vals [i]);
1961 if (locals [i].Name == "alist") {
1965 // Argument checking
1968 AssertThrows<ArgumentNullException> (delegate () {
1969 frame.GetValue ((LocalVariable)null);
1971 // GetValue () local from another method
1972 AssertThrows<ArgumentException> (delegate () {
1973 frame.GetValue (m1.GetLocal ("foo"));
1977 AssertThrows<ArgumentNullException> (delegate () {
1978 frame.GetValue ((ParameterInfoMirror)null);
1980 // GetValue () local from another method
1981 AssertThrows<ArgumentException> (delegate () {
1982 frame.GetValue (m1.GetParameters ()[0]);
1985 // GetValues () null
1986 AssertThrows<ArgumentNullException> (delegate () {
1987 frame.GetValues (null);
1989 // GetValues () embedded null
1990 AssertThrows<ArgumentNullException> (delegate () {
1991 frame.GetValues (new LocalVariable [] { null });
1993 // GetValues () local from another method
1994 AssertThrows<ArgumentException> (delegate () {
1995 frame.GetValues (new LocalVariable [] { m1.GetLocal ("foo") });
1998 AssertThrows<ArgumentException> (delegate () {
1999 val = frame.GetValue (frame.Method.ReturnParameter);
2002 // invalid stack frames
2004 e = GetNextEvent ();
2005 Assert.IsTrue (e is StepEvent);
2006 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
2008 AssertThrows<InvalidStackFrameException> (delegate () {
2009 frame.GetValue (frame.Method.GetLocal ("i"));
2015 be = run_until ("locals7");
2017 req = create_step (be);
2023 // Test that locals are initialized
2024 frame = e.Thread.GetFrames () [0];
2025 val = frame.GetValue (frame.Method.GetLocal ("t"));
2026 AssertValue (0, val);
2030 Assert.AreEqual ("locals7", (e as StepEvent).Method.Name);
2034 Assert.AreEqual ("locals7", (e as StepEvent).Method.Name);
2036 frame = e.Thread.GetFrames () [0];
2037 val = frame.GetValue (frame.Method.GetParameters ()[0]);
2038 AssertValue (22, val);
2039 val = frame.GetValue (frame.Method.GetLocal ("t"));
2040 AssertValue (22, val);
2041 val = frame.GetValue (frame.Method.GetLocal ("t2"));
2042 AssertValue (22, val);
2046 public void GetVisibleVariables () {
2047 Event e = run_until ("locals4");
2050 var locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
2051 Assert.AreEqual (2, locals.Count);
2052 var loc = locals.First (l => l.Name == "i");
2053 Assert.AreEqual ("Int64", loc.Type.Name);
2054 loc = locals.First (l => l.Name == "s");
2055 Assert.AreEqual ("String", loc.Type.Name);
2057 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
2058 Assert.AreEqual ("i", loc.Name);
2059 Assert.AreEqual ("Int64", loc.Type.Name);
2061 e = run_until ("locals5");
2064 locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
2065 Assert.AreEqual (2, locals.Count);
2066 loc = locals.First (l => l.Name == "i");
2067 Assert.AreEqual ("String", loc.Type.Name);
2068 loc = locals.First (l => l.Name == "s");
2069 Assert.AreEqual ("String", loc.Type.Name);
2071 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
2072 Assert.AreEqual ("i", loc.Name);
2073 Assert.AreEqual ("String", loc.Type.Name);
2075 // Variable in another scope
2076 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("j");
2077 Assert.IsNull (loc);
2081 public void Exit () {
2086 var e = GetNextEvent ();
2087 Assert.IsInstanceOfType (typeof (VMDeathEvent), e);
2089 Assert.AreEqual (5, (e as VMDeathEvent).ExitCode);
2092 /* Could be a remote vm with no process */
2095 Assert.AreEqual (5, p.ExitCode);
2098 AssertThrows<VMDisconnectedException> (delegate () {
2107 public void Dispose () {
2112 var e = GetNextEvent ();
2113 Assert.IsInstanceOfType (typeof (VMDisconnectEvent), e);
2116 /* Could be a remote vm with no process */
2119 Assert.AreEqual (3, p.ExitCode);
2122 AssertThrows<VMDisconnectedException> (delegate () {
2131 public void ColumnNumbers () {
2132 Event e = run_until ("line_numbers");
2134 // FIXME: Merge this with LineNumbers () when its fixed
2136 step_req = create_step (e);
2137 step_req.Depth = StepDepth.Into;
2145 e = GetNextEvent ();
2146 Assert.IsTrue (e is StepEvent);
2147 if (e.Thread.GetFrames ()[0].Method.Name == "ln1")
2151 // Do an additional step over so we are not on the beginning line of the method
2152 step_req.Disable ();
2153 step_req.Depth = StepDepth.Over;
2156 e = GetNextEvent ();
2157 Assert.IsTrue (e is StepEvent);
2159 l = e.Thread.GetFrames ()[0].Location;
2161 Assert.AreEqual (3, l.ColumnNumber);
2163 step_req.Disable ();
2167 // Broken by mcs+runtime changes (#5438)
2168 [Category("NotWorking")]
2169 public void LineNumbers () {
2170 Event e = run_until ("line_numbers");
2172 step_req = create_step (e);
2173 step_req.Depth = StepDepth.Into;
2180 e = GetNextEvent ();
2181 Assert.IsTrue (e is StepEvent);
2183 l = e.Thread.GetFrames ()[0].Location;
2185 Assert.IsTrue (l.SourceFile.IndexOf ("dtest-app.cs") != -1);
2186 Assert.AreEqual ("ln1", l.Method.Name);
2189 using (FileStream fs = new FileStream (l.SourceFile, FileMode.Open, FileAccess.Read)) {
2190 MD5 md5 = MD5.Create ();
2191 var hash = md5.ComputeHash (fs);
2193 for (int i = 0; i < 16; ++i)
2194 Assert.AreEqual (hash [i], l.SourceFileHash [i]);
2197 int line_base = l.LineNumber;
2200 e = GetNextEvent ();
2201 Assert.IsTrue (e is StepEvent);
2202 l = e.Thread.GetFrames ()[0].Location;
2203 Assert.AreEqual ("ln2", l.Method.Name);
2204 Assert.AreEqual (line_base + 6, l.LineNumber);
2207 e = GetNextEvent ();
2208 Assert.IsTrue (e is StepEvent);
2209 l = e.Thread.GetFrames ()[0].Location;
2210 Assert.AreEqual ("ln1", l.Method.Name);
2211 Assert.AreEqual (line_base + 1, l.LineNumber);
2214 e = GetNextEvent ();
2215 Assert.IsTrue (e is StepEvent);
2216 l = e.Thread.GetFrames ()[0].Location;
2217 Assert.AreEqual ("ln3", l.Method.Name);
2218 Assert.AreEqual (line_base + 11, l.LineNumber);
2221 e = GetNextEvent ();
2222 Assert.IsTrue (e is StepEvent);
2223 l = e.Thread.GetFrames ()[0].Location;
2224 Assert.AreEqual ("ln3", l.Method.Name);
2225 Assert.IsTrue (l.SourceFile.EndsWith ("FOO"));
2226 Assert.AreEqual (55, l.LineNumber);
2229 e = GetNextEvent ();
2230 Assert.IsTrue (e is StepEvent);
2231 l = e.Thread.GetFrames ()[0].Location;
2232 Assert.AreEqual ("ln1", l.Method.Name);
2233 Assert.AreEqual (line_base + 2, l.LineNumber);
2235 // GetSourceFiles ()
2236 string[] sources = l.Method.DeclaringType.GetSourceFiles ();
2237 Assert.AreEqual (2, sources.Length);
2238 Assert.AreEqual ("dtest-app.cs", sources [0]);
2239 Assert.AreEqual ("FOO", sources [1]);
2241 sources = l.Method.DeclaringType.GetSourceFiles (true);
2242 Assert.AreEqual (2, sources.Length);
2243 Assert.IsTrue (sources [0].EndsWith ("dtest-app.cs"));
2244 Assert.IsTrue (sources [1].EndsWith ("FOO"));
2248 public void Suspend () {
2251 Start (new string [] { "dtest-app.exe", "suspend-test" });
2253 Event e = run_until ("suspend");
2255 ThreadMirror main = e.Thread;
2263 // The debuggee should be suspended while it is running the infinite loop
2265 StackFrame frame = main.GetFrames ()[0];
2266 Assert.AreEqual ("suspend", frame.Method.Name);
2270 // resuming when not suspended
2271 AssertThrows<InvalidOperationException> (delegate () {
2281 public void AssemblyLoad () {
2282 Event e = run_until ("assembly_load");
2284 var load_req = vm.CreateAssemblyLoadRequest ();
2289 e = GetNextEvent ();
2290 Assert.IsInstanceOfType (typeof (AssemblyLoadEvent), e);
2291 Assert.IsTrue ((e as AssemblyLoadEvent).Assembly.Location.EndsWith ("System.dll"));
2293 var frames = e.Thread.GetFrames ();
2294 Assert.IsTrue (frames.Length > 0);
2295 Assert.AreEqual ("assembly_load", frames [0].Method.Name);
2299 public void CreateValue () {
2302 v = vm.CreateValue (1);
2303 Assert.AreEqual (vm, v.VirtualMachine);
2304 Assert.AreEqual (1, v.Value);
2306 v = vm.CreateValue (null);
2307 Assert.AreEqual (vm, v.VirtualMachine);
2308 Assert.AreEqual (null, v.Value);
2310 // Argument checking
2311 AssertThrows <ArgumentException> (delegate () {
2312 v = vm.CreateValue ("FOO");
2317 public void CreateString () {
2318 StringMirror s = vm.RootDomain.CreateString ("ABC");
2320 Assert.AreEqual (vm, s.VirtualMachine);
2321 Assert.AreEqual ("ABC", s.Value);
2322 Assert.AreEqual (vm.RootDomain, s.Domain);
2325 StringBuilder sb = new StringBuilder ();
2326 for (int i = 0; i < 1024; ++i)
2328 s = vm.RootDomain.CreateString (sb.ToString ());
2330 // Argument checking
2331 AssertThrows <ArgumentNullException> (delegate () {
2332 s = vm.RootDomain.CreateString (null);
2337 public void CreateBoxedValue () {
2338 ObjectMirror o = vm.RootDomain.CreateBoxedValue (new PrimitiveValue (vm, 42));
2340 Assert.AreEqual ("Int32", o.Type.Name);
2341 //AssertValue (42, m.GetValue (o.Type.GetField ("m_value")));
2343 // Argument checking
2344 AssertThrows <ArgumentNullException> (delegate () {
2345 vm.RootDomain.CreateBoxedValue (null);
2348 AssertThrows <ArgumentException> (delegate () {
2349 vm.RootDomain.CreateBoxedValue (o);
2354 public void Invoke () {
2355 Event e = run_until ("invoke1");
2357 StackFrame frame = e.Thread.GetFrames () [0];
2359 TypeMirror t = frame.Method.DeclaringType;
2360 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2362 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2368 m = t.GetMethod ("invoke_return_void");
2369 v = this_obj.InvokeMethod (e.Thread, m, null);
2373 m = t.GetMethod ("invoke_return_ref");
2374 v = this_obj.InvokeMethod (e.Thread, m, null);
2375 AssertValue ("ABC", v);
2378 m = t.GetMethod ("invoke_return_null");
2379 v = this_obj.InvokeMethod (e.Thread, m, null);
2380 AssertValue (null, v);
2383 m = t.GetMethod ("invoke_return_primitive");
2384 v = this_obj.InvokeMethod (e.Thread, m, null);
2385 AssertValue (42, v);
2388 m = t.GetMethod ("invoke_return_nullable");
2389 v = this_obj.InvokeMethod (e.Thread, m, null);
2390 Assert.IsInstanceOfType (typeof (StructMirror), v);
2391 var s = v as StructMirror;
2392 AssertValue (42, s.Fields [0]);
2393 AssertValue (true, s.Fields [1]);
2395 // pass nullable as this
2396 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2397 m = s.Type.GetMethod ("ToString");
2398 v = s.InvokeMethod (e.Thread, m, null);
2400 // return nullable null
2401 m = t.GetMethod ("invoke_return_nullable_null");
2402 v = this_obj.InvokeMethod (e.Thread, m, null);
2403 Assert.IsInstanceOfType (typeof (StructMirror), v);
2404 s = v as StructMirror;
2405 AssertValue (0, s.Fields [0]);
2406 AssertValue (false, s.Fields [1]);
2408 // pass nullable as this
2409 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2410 m = s.Type.GetMethod ("ToString");
2411 v = s.InvokeMethod (e.Thread, m, null);
2414 m = t.GetMethod ("invoke_pass_primitive");
2415 Value[] args = new Value [] {
2416 vm.CreateValue ((byte)Byte.MaxValue),
2417 vm.CreateValue ((sbyte)SByte.MaxValue),
2418 vm.CreateValue ((short)1),
2419 vm.CreateValue ((ushort)1),
2420 vm.CreateValue ((int)1),
2421 vm.CreateValue ((uint)1),
2422 vm.CreateValue ((long)1),
2423 vm.CreateValue ((ulong)1),
2424 vm.CreateValue ('A'),
2425 vm.CreateValue (true),
2426 vm.CreateValue (3.14f),
2427 vm.CreateValue (3.14) };
2429 v = this_obj.InvokeMethod (e.Thread, m, args);
2430 AssertValue ((int)Byte.MaxValue + (int)SByte.MaxValue + 1 + 1 + 1 + 1 + 1 + 1 + 'A' + 1 + 3 + 3, v);
2433 m = t.GetMethod ("invoke_pass_ref");
2434 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2435 AssertValue ("ABC", v);
2438 m = t.GetMethod ("invoke_pass_ref");
2439 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (null) });
2440 AssertValue (null, v);
2443 m = t.GetMethod ("invoke_static_pass_ref");
2444 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2445 AssertValue ("ABC", v);
2447 // static invoked using ObjectMirror.InvokeMethod
2448 m = t.GetMethod ("invoke_static_pass_ref");
2449 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2450 AssertValue ("ABC", v);
2452 // method which throws an exception
2454 m = t.GetMethod ("invoke_throws");
2455 v = this_obj.InvokeMethod (e.Thread, m, null);
2457 } catch (InvocationException ex) {
2458 Assert.AreEqual ("Exception", ex.Exception.Type.Name);
2462 m = t.GetMethod ("invoke_out");
2463 var out_task = this_obj.InvokeMethodAsyncWithResult (e.Thread, m, new Value [] { vm.CreateValue (1), vm.CreateValue (null) }, InvokeOptions.ReturnOutArgs);
2464 var out_args = out_task.Result.OutArgs;
2465 AssertValue (5, out_args [0]);
2466 Assert.IsTrue (out_args [1] is ArrayMirror);
2467 Assert.AreEqual (10, (out_args [1] as ArrayMirror).Length);
2469 // without ReturnOutArgs flag
2470 out_task = this_obj.InvokeMethodAsyncWithResult (e.Thread, m, new Value [] { vm.CreateValue (1), vm.CreateValue (null) });
2471 out_args = out_task.Result.OutArgs;
2472 Assert.IsNull (out_args);
2475 m = t.GetMethod (".ctor");
2476 v = t.InvokeMethod (e.Thread, m, null);
2477 Assert.IsInstanceOfType (typeof (ObjectMirror), v);
2478 Assert.AreEqual ("Tests", (v as ObjectMirror).Type.Name);
2481 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("ITest2");
2482 m = cl1.GetMethod ("invoke_iface");
2483 v = this_obj.InvokeMethod (e.Thread, m, null);
2484 AssertValue (42, v);
2487 m = t.BaseType.GetMethod ("virtual_method");
2488 v = this_obj.InvokeMethod (e.Thread, m, null, InvokeOptions.Virtual);
2489 AssertValue ("V2", v);
2491 // virtual call on static method
2492 m = t.GetMethod ("invoke_static_pass_ref");
2493 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") }, InvokeOptions.Virtual);
2494 AssertValue ("ABC", v);
2497 m = t.GetMethod ("invoke_pass_ref");
2498 var task = this_obj.InvokeMethodAsync (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2499 AssertValue ("ABC", task.Result);
2502 m = t.GetMethod ("invoke_static_pass_ref");
2503 task = t.InvokeMethodAsync (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2504 AssertValue ("ABC", task.Result);
2506 // Argument checking
2509 AssertThrows<ArgumentNullException> (delegate {
2510 m = t.GetMethod ("invoke_pass_ref");
2511 v = this_obj.InvokeMethod (null, m, new Value [] { vm.CreateValue (null) });
2515 AssertThrows<ArgumentNullException> (delegate {
2516 v = this_obj.InvokeMethod (e.Thread, null, new Value [] { vm.CreateValue (null) });
2519 // invalid number of arguments
2520 m = t.GetMethod ("invoke_pass_ref");
2521 AssertThrows<ArgumentException> (delegate {
2522 v = this_obj.InvokeMethod (e.Thread, m, null);
2525 // invalid type of argument (ref != primitive)
2526 m = t.GetMethod ("invoke_pass_ref");
2527 AssertThrows<ArgumentException> (delegate {
2528 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2531 // invalid type of argument (primitive != primitive)
2532 m = t.GetMethod ("invoke_pass_primitive_2");
2533 AssertThrows<ArgumentException> (delegate {
2534 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2537 // invoking a non-static method as static
2538 m = t.GetMethod ("invoke_pass_ref");
2539 AssertThrows<ArgumentException> (delegate {
2540 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2543 // invoking a method defined in another class
2544 m = t2.GetMethod ("invoke");
2545 AssertThrows<ArgumentException> (delegate {
2546 v = this_obj.InvokeMethod (e.Thread, m, null);
2551 public void InvokeVType () {
2552 Event e = run_until ("invoke1");
2554 StackFrame frame = e.Thread.GetFrames () [0];
2556 var s = frame.GetArgument (1) as StructMirror;
2558 TypeMirror t = s.Type;
2563 // Pass struct as this, receive int
2564 m = t.GetMethod ("invoke_return_int");
2565 v = s.InvokeMethod (e.Thread, m, null);
2566 AssertValue (42, v);
2568 // Pass boxed struct as this
2569 var boxed_this = t.NewInstance () as ObjectMirror;
2570 m = t.GetMethod ("invoke_return_int");
2571 v = boxed_this.InvokeMethod (e.Thread, m, null);
2574 // Pass struct as this, receive intptr
2575 m = t.GetMethod ("invoke_return_intptr");
2576 v = s.InvokeMethod (e.Thread, m, null);
2577 AssertValue (43, v);
2580 m = t.GetMethod ("invoke_static");
2581 v = t.InvokeMethod (e.Thread, m, null);
2584 // Pass generic struct as this
2585 s = frame.GetArgument (2) as StructMirror;
2587 m = t.GetMethod ("invoke_return_int");
2588 v = s.InvokeMethod (e.Thread, m, null);
2589 AssertValue (42, v);
2592 s = frame.GetArgument (1) as StructMirror;
2594 m = t.GetMethods ().First (method => method.Name == ".ctor" && method.GetParameters ().Length == 1);
2595 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2596 AssertValue (1, (v as StructMirror)["i"]);
2598 // Invoke a method which changes state
2599 s = frame.GetArgument (1) as StructMirror;
2601 m = t.GetMethod ("invoke_mutate");
2602 var task = s.InvokeMethodAsyncWithResult (e.Thread, m, null, InvokeOptions.ReturnOutThis);
2603 var out_this = task.Result.OutThis as StructMirror;
2604 AssertValue (5, out_this ["l"]);
2606 // Without the ReturnOutThis flag
2607 s = frame.GetArgument (1) as StructMirror;
2609 m = t.GetMethod ("invoke_mutate");
2610 task = s.InvokeMethodAsyncWithResult (e.Thread, m, null);
2611 out_this = task.Result.OutThis as StructMirror;
2612 Assert.AreEqual (null, out_this);
2615 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("ITest2");
2616 m = cl1.GetMethod ("invoke_iface");
2617 v = s.InvokeMethod (e.Thread, m, null);
2618 AssertValue (42, v);
2621 m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2622 v = s.InvokeMethod (e.Thread, m, null, InvokeOptions.Virtual);
2623 AssertValue ("42", v);
2627 public void BreakpointDuringInvoke () {
2628 Event e = run_until ("invoke1");
2630 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke2");
2631 Assert.IsNotNull (m);
2632 vm.SetBreakpoint (m, 0);
2634 StackFrame frame = e.Thread.GetFrames () [0];
2635 var o = frame.GetThis () as ObjectMirror;
2637 bool failed = false;
2639 bool finished = false;
2640 object wait = new object ();
2642 // Have to invoke in a separate thread as the invoke is suspended until we
2643 // resume after the breakpoint
2644 Thread t = new Thread (delegate () {
2646 o.InvokeMethod (e.Thread, m, null);
2652 Monitor.Pulse (wait);
2658 StackFrame invoke_frame = null;
2661 e = GetNextEvent ();
2662 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
2663 // Check stack trace support and invokes
2664 var frames = e.Thread.GetFrames ();
2665 invoke_frame = frames [0];
2666 Assert.AreEqual ("invoke2", frames [0].Method.Name);
2667 Assert.IsTrue (frames [0].IsDebuggerInvoke);
2668 Assert.AreEqual ("invoke1", frames [1].Method.Name);
2675 Monitor.Wait (wait);
2678 // Check that the invoke frames are no longer valid
2679 AssertThrows<InvalidStackFrameException> (delegate {
2680 invoke_frame.GetThis ();
2683 // Check InvokeOptions.DisableBreakpoints flag
2684 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2688 public void DisabledExceptionDuringInvoke () {
2689 Event e = run_until ("invoke_ex");
2691 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_ex_inner");
2693 StackFrame frame = e.Thread.GetFrames () [0];
2694 var o = frame.GetThis () as ObjectMirror;
2696 var req = vm.CreateExceptionRequest (null);
2699 // Check InvokeOptions.DisableBreakpoints flag
2700 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2706 public void InvokeSingleThreaded () {
2709 Start (new string [] { "dtest-app.exe", "invoke-single-threaded" });
2711 Event e = run_until ("invoke_single_threaded_2");
2713 StackFrame f = e.Thread.GetFrames ()[0];
2715 var obj = f.GetThis () as ObjectMirror;
2717 // Check that the counter value incremented by the other thread does not increase
2718 // during the invoke.
2719 object counter1 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2721 var m = obj.Type.GetMethod ("invoke_return_void");
2722 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2724 object counter2 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2726 Assert.AreEqual ((int)counter1, (int)counter2);
2728 // Test multiple invokes done in succession
2729 m = obj.Type.GetMethod ("invoke_return_void");
2730 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2732 // Test events during single-threaded invokes
2733 vm.EnableEvents (EventType.TypeLoad);
2734 m = obj.Type.GetMethod ("invoke_type_load");
2735 obj.BeginInvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded, delegate {
2739 e = GetNextEvent ();
2740 Assert.AreEqual (EventType.TypeLoad, e.EventType);
2743 List<Value> invoke_results;
2746 public void InvokeMultiple () {
2747 Event e = run_until ("invoke1");
2749 StackFrame frame = e.Thread.GetFrames () [0];
2751 TypeMirror t = frame.Method.DeclaringType;
2752 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2754 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2756 var methods = new MethodMirror [2];
2757 methods [0] = t.GetMethod ("invoke_return_ref");
2758 methods [1] = t.GetMethod ("invoke_return_primitive");
2760 invoke_results = new List<Value> ();
2762 var r = this_obj.BeginInvokeMultiple (e.Thread, methods, null, InvokeOptions.SingleThreaded, invoke_multiple_cb, this_obj);
2763 WaitHandle.WaitAll (new WaitHandle[] { r.AsyncWaitHandle });
2764 this_obj.EndInvokeMultiple (r);
2765 // The callback might still be running
2766 while (invoke_results.Count < 2) {
2769 if (invoke_results [0] is PrimitiveValue) {
2770 AssertValue ("ABC", invoke_results [1]);
2771 AssertValue (42, invoke_results [0]);
2773 AssertValue ("ABC", invoke_results [0]);
2774 AssertValue (42, invoke_results [1]);
2778 void invoke_multiple_cb (IAsyncResult ar) {
2779 ObjectMirror this_obj = (ObjectMirror)ar.AsyncState;
2781 var res = this_obj.EndInvokeMethod (ar);
2782 lock (invoke_results)
2783 invoke_results.Add (res);
2787 public void InvokeAbort () {
2790 Start (new string [] { "dtest-app.exe", "invoke-abort" });
2792 Event e = run_until ("invoke_abort");
2794 StackFrame f = e.Thread.GetFrames ()[0];
2796 var obj = f.GetThis () as ObjectMirror;
2798 var m = t.GetMethod ("invoke_abort_2");
2799 // Invoke multiple times to check that the subsequent invokes are aborted too
2800 var res = (IInvokeAsyncResult)obj.BeginInvokeMultiple (e.Thread, new MethodMirror[] { m, m, m, m }, null, InvokeOptions.None, delegate { }, null);
2803 AssertThrows<CommandException> (delegate {
2804 obj.EndInvokeMethod (res);
2809 public void GetThreads () {
2814 public void Threads () {
2815 Event e = run_until ("threads");
2817 Assert.AreEqual (ThreadState.Running, e.Thread.ThreadState);
2819 Assert.IsTrue (e.Thread.ThreadId > 0);
2821 Assert.AreEqual (e.Thread.TID, e.Thread.TID);
2823 vm.EnableEvents (EventType.ThreadStart, EventType.ThreadDeath);
2827 e = GetNextEvent ();
2828 Assert.IsInstanceOfType (typeof (ThreadStartEvent), e);
2829 var state = e.Thread.ThreadState;
2830 Assert.IsTrue (state == ThreadState.Running || state == ThreadState.Unstarted);
2834 e = GetNextEvent ();
2835 Assert.IsInstanceOfType (typeof (ThreadDeathEvent), e);
2836 Assert.AreEqual (ThreadState.Stopped, e.Thread.ThreadState);
2840 public void Frame_SetValue () {
2841 Event e = run_until ("locals2");
2843 StackFrame frame = e.Thread.GetFrames () [0];
2846 var l = frame.Method.GetLocal ("i");
2847 frame.SetValue (l, vm.CreateValue ((long)55));
2848 AssertValue (55, frame.GetValue (l));
2851 l = frame.Method.GetLocal ("s");
2852 frame.SetValue (l, vm.RootDomain.CreateString ("DEF"));
2853 AssertValue ("DEF", frame.GetValue (l));
2855 // argument as local
2856 l = frame.Method.GetLocal ("arg");
2857 frame.SetValue (l, vm.CreateValue (6));
2858 AssertValue (6, frame.GetValue (l));
2861 var p = frame.Method.GetParameters ()[1];
2862 frame.SetValue (p, vm.CreateValue (7));
2863 AssertValue (7, frame.GetValue (p));
2866 p = frame.Method.GetParameters ()[2];
2867 frame.SetValue (p, vm.RootDomain.CreateString ("DEF"));
2868 AssertValue ("DEF", frame.GetValue (p));
2871 p = frame.Method.GetParameters ()[3];
2872 frame.SetValue (p, vm.RootDomain.CreateString ("DEF2"));
2873 AssertValue ("DEF2", frame.GetValue (p));
2876 p = frame.Method.GetParameters ()[4];
2877 var v = frame.GetValue (p) as StructMirror;
2878 v ["i"] = vm.CreateValue (43);
2879 frame.SetValue (p, v);
2880 v = frame.GetValue (p) as StructMirror;
2881 AssertValue (43, v ["i"]);
2883 // argument checking
2886 AssertThrows<ArgumentNullException> (delegate () {
2887 frame.SetValue ((LocalVariable)null, vm.CreateValue (55));
2891 AssertThrows<ArgumentNullException> (delegate () {
2892 l = frame.Method.GetLocal ("i");
2893 frame.SetValue (l, null);
2896 // value of invalid type
2897 AssertThrows<ArgumentException> (delegate () {
2898 l = frame.Method.GetLocal ("i");
2899 frame.SetValue (l, vm.CreateValue (55));
2905 public void Frame_SetValue_Registers () {
2906 Event e = run_until ("locals6_1");
2908 StackFrame frame = e.Thread.GetFrames () [1];
2911 var l = frame.Method.GetLocal ("j");
2912 frame.SetValue (l, vm.CreateValue (99));
2913 AssertValue (99, frame.GetValue (l));
2915 // Check it during execution
2916 e = run_until ("locals6_2");
2917 frame = e.Thread.GetFrames () [0];
2918 AssertValue (99, frame.GetValue (frame.Method.GetParameters ()[0]));
2920 // Set it while in a frame which clobbers its register
2921 e = run_until ("locals6_3");
2922 frame = e.Thread.GetFrames () [1];
2923 frame.SetValue (l, vm.CreateValue (100));
2924 AssertValue (100, frame.GetValue (l));
2926 // Check it during execution
2927 e = run_until ("locals6_4");
2928 frame = e.Thread.GetFrames () [0];
2929 AssertValue (100, frame.GetValue (frame.Method.GetParameters ()[0]));
2931 // Signed byte value
2932 e = run_until ("locals6_5");
2933 frame = e.Thread.GetFrames () [1];
2934 var l2 = frame.Method.GetLocal ("sb");
2935 frame.SetValue (l2, vm.CreateValue ((sbyte)-99));
2936 AssertValue (-99, frame.GetValue (l2));
2938 // Check it during execution
2939 e = run_until ("locals6_6");
2940 frame = e.Thread.GetFrames () [0];
2941 AssertValue (-99, frame.GetValue (frame.Method.GetParameters ()[0]));
2945 public void InvokeRegress () {
2946 Event e = run_until ("invoke1");
2948 StackFrame frame = e.Thread.GetFrames () [0];
2950 TypeMirror t = frame.Method.DeclaringType;
2951 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2953 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2959 m = t.GetMethod ("invoke_return_void");
2960 v = this_obj.InvokeMethod (e.Thread, m, null);
2963 // Check that the stack frames remain valid during the invoke
2964 Assert.AreEqual ("Tests", (frame.GetThis () as ObjectMirror).Type.Name);
2966 // do another invoke
2967 m = t.GetMethod ("invoke_return_void");
2968 v = this_obj.InvokeMethod (e.Thread, m, null);
2971 // Try a single step after the invoke
2972 var req = create_step (e);
2973 req.Depth = StepDepth.Into;
2974 req.Size = StepSize.Line;
2980 // Step into invoke2
2982 e = GetNextEvent ();
2983 Assert.IsTrue (e is StepEvent);
2984 Assert.AreEqual ("invoke2", (e as StepEvent).Method.Name);
2988 frame = e.Thread.GetFrames () [0];
2992 public void Exceptions () {
2993 Event e = run_until ("exceptions");
2994 var req = vm.CreateExceptionRequest (null);
2999 e = GetNextEvent ();
3000 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3001 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
3003 var frames = e.Thread.GetFrames ();
3004 Assert.AreEqual ("exceptions", frames [0].Method.Name);
3007 // exception type filter
3009 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.ArgumentException"));
3012 // Skip the throwing of the second OverflowException
3015 e = GetNextEvent ();
3016 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3017 Assert.AreEqual ("ArgumentException", (e as ExceptionEvent).Exception.Type.Name);
3020 // exception type filter for subclasses
3021 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.Exception"));
3026 e = GetNextEvent ();
3027 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3028 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
3032 req.IncludeSubclasses = false;
3037 e = GetNextEvent ();
3038 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3039 Assert.AreEqual ("Exception", (e as ExceptionEvent).Exception.Type.Name);
3042 // Implicit exceptions
3043 req = vm.CreateExceptionRequest (null);
3048 e = GetNextEvent ();
3049 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3050 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
3053 // Single stepping after an exception
3054 req = vm.CreateExceptionRequest (null);
3059 e = GetNextEvent ();
3060 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3061 Assert.AreEqual ("Exception", (e as ExceptionEvent).Exception.Type.Name);
3062 frames = e.Thread.GetFrames ();
3063 Assert.AreEqual ("exceptions2", frames [0].Method.Name);
3066 var sreq = create_step (e);
3067 sreq.Depth = StepDepth.Over;
3068 sreq.Size = StepSize.Line;
3072 e = GetNextEvent ();
3073 Assert.IsInstanceOfType (typeof (StepEvent), e);
3074 frames = e.Thread.GetFrames ();
3075 Assert.AreEqual ("exceptions", frames [0].Method.Name);
3078 // Argument checking
3079 AssertThrows<ArgumentException> (delegate {
3080 vm.CreateExceptionRequest (e.Thread.Type);
3085 public void ExceptionFilter () {
3086 Event e = run_until ("exception_filter");
3088 MethodMirror m = entry_point.DeclaringType.GetMethod ("exception_filter_filter");
3089 Assert.IsNotNull (m);
3091 vm.SetBreakpoint (m, 0);
3095 e = GetNextEvent ();
3096 Assert.AreEqual (EventType.Breakpoint, e.EventType);
3097 Assert.IsTrue (e is BreakpointEvent);
3098 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
3100 var frames = e.Thread.GetFrames ();
3102 Assert.IsTrue (frames [0].Location.SourceFile.IndexOf ("dtest-app.cs") != -1);
3103 Assert.AreEqual ("exception_filter_filter", frames [0].Location.Method.Name);
3105 Assert.AreEqual (0, frames [1].Location.Method.MetadataToken);
3106 Assert.AreEqual (0x0f, frames [1].Location.ILOffset);
3108 Assert.AreEqual ("exception_filter_method", frames [2].Location.Method.Name);
3109 Assert.AreEqual (0x06, frames [2].Location.ILOffset);
3111 Assert.AreEqual (0, frames [3].Location.Method.MetadataToken, 0);
3112 Assert.AreEqual (0, frames [3].Location.ILOffset);
3114 Assert.AreEqual ("exception_filter", frames [4].Location.Method.Name);
3118 public void ExceptionFilter2 () {
3121 Start (new string [] { "dtest-excfilter.exe" });
3123 MethodMirror filter_method = entry_point.DeclaringType.GetMethod ("Filter");
3124 Assert.IsNotNull (filter_method);
3126 MethodMirror test_method = entry_point.DeclaringType.GetMethod ("Test");
3127 Assert.IsNotNull (test_method);
3129 vm.SetBreakpoint (filter_method, 0);
3133 var e = GetNextEvent ();
3134 Assert.AreEqual (EventType.Breakpoint, e.EventType);
3135 Assert.IsTrue (e is BreakpointEvent);
3136 Assert.AreEqual (filter_method.Name, (e as BreakpointEvent).Method.Name);
3138 var frames = e.Thread.GetFrames ();
3140 Assert.AreEqual (4, frames.Count ());
3142 Assert.AreEqual (filter_method.Name, frames [0].Location.Method.Name);
3143 Assert.AreEqual (20, frames [0].Location.LineNumber);
3144 Assert.AreEqual (0, frames [0].Location.ILOffset);
3146 Assert.AreEqual (test_method.Name, frames [1].Location.Method.Name);
3147 Assert.AreEqual (37, frames [1].Location.LineNumber);
3148 Assert.AreEqual (0x0b, frames [1].Location.ILOffset);
3150 Assert.AreEqual (test_method.Name, frames [2].Location.Method.Name);
3151 Assert.AreEqual (33, frames [2].Location.LineNumber);
3152 Assert.AreEqual (0x05, frames [2].Location.ILOffset);
3154 Assert.AreEqual (entry_point.Name, frames [3].Location.Method.Name);
3155 Assert.AreEqual (14, frames [3].Location.LineNumber);
3156 Assert.AreEqual (0x00, frames [3].Location.ILOffset);
3164 public void EventSets () {
3166 // Create two filter which both match the same exception
3168 Event e = run_until ("exceptions");
3170 var req = vm.CreateExceptionRequest (null);
3173 var req2 = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.OverflowException"));
3178 var es = vm.GetNextEventSet ();
3179 Assert.AreEqual (2, es.Events.Length);
3182 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3183 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
3186 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3187 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
3194 // Test single threaded invokes during processing of nullref exceptions.
3195 // These won't work if the exception handling is done from the sigsegv signal
3196 // handler, since the sigsegv signal is disabled until control returns from the
3200 [Category ("only3")]
3201 public void NullRefExceptionAndSingleThreadedInvoke () {
3202 Event e = run_until ("exceptions");
3203 var req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.NullReferenceException"));
3208 e = GetNextEvent ();
3209 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3210 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
3212 var ex = (e as ExceptionEvent).Exception;
3213 var tostring_method = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
3214 ex.InvokeMethod (e.Thread, tostring_method, null, InvokeOptions.SingleThreaded);
3218 public void MemberInOtherDomain () {
3221 Start (new string [] { "dtest-app.exe", "domain-test" });
3223 vm.EnableEvents (EventType.AppDomainCreate, EventType.AppDomainUnload, EventType.AssemblyUnload);
3225 Event e = run_until ("domains_print_across");
3227 var frame = e.Thread.GetFrames ()[0];
3228 var inOtherDomain = frame.GetArgument (0) as ObjectMirror;
3229 var crossDomainField = (ObjectMirror) inOtherDomain.GetValue (inOtherDomain.Type.GetField("printMe"));
3230 Assert.AreEqual ("SentinelClass", crossDomainField.Type.Name);
3234 public void Domains () {
3237 Start (new string [] { "dtest-app.exe", "domain-test" });
3239 vm.EnableEvents (EventType.AppDomainCreate, EventType.AppDomainUnload, EventType.AssemblyUnload);
3241 Event e = run_until ("domains");
3245 e = GetNextEvent ();
3246 Assert.IsInstanceOfType (typeof (AppDomainCreateEvent), e);
3248 var domain = (e as AppDomainCreateEvent).Domain;
3250 // Check the object type
3251 e = run_until ("domains_2");
3252 var frame = e.Thread.GetFrames ()[0];
3253 var o = frame.GetArgument (0) as ObjectMirror;
3254 Assert.AreEqual ("CrossDomain", o.Type.Name);
3256 // Do a remoting invoke
3257 var cross_domain_type = o.Type;
3258 var v = o.InvokeMethod (e.Thread, cross_domain_type.GetMethod ("invoke_3"), null);
3259 AssertValue (42, v);
3261 // Run until the callback in the domain
3262 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_in_domain");
3263 Assert.IsNotNull (m);
3264 vm.SetBreakpoint (m, 0);
3268 e = GetNextEvent ();
3269 if (e is BreakpointEvent)
3273 Assert.AreEqual ("invoke_in_domain", (e as BreakpointEvent).Method.Name);
3275 // d_method is from another domain
3276 MethodMirror d_method = (e as BreakpointEvent).Method;
3277 Assert.IsTrue (m != d_method);
3279 var frames = e.Thread.GetFrames ();
3280 Assert.AreEqual ("invoke_in_domain", frames [0].Method.Name);
3281 Assert.AreEqual (domain, frames [0].Domain);
3282 Assert.AreEqual ("invoke", frames [1].Method.Name);
3283 Assert.AreEqual ("domains", frames [2].Method.Name);
3284 Assert.AreEqual (vm.RootDomain, frames [2].Domain);
3286 // Test breakpoints on already JITted methods in other domains
3287 m = entry_point.DeclaringType.GetMethod ("invoke_in_domain_2");
3288 Assert.IsNotNull (m);
3289 vm.SetBreakpoint (m, 0);
3293 e = GetNextEvent ();
3294 if (e is BreakpointEvent)
3298 Assert.AreEqual ("invoke_in_domain_2", (e as BreakpointEvent).Method.Name);
3300 // This is empty when receiving the AppDomainCreateEvent
3301 Assert.AreEqual ("domain", domain.FriendlyName);
3303 // Run until the unload
3306 e = GetNextEvent ();
3307 if (e is AssemblyUnloadEvent) {
3313 Assert.IsInstanceOfType (typeof (AppDomainUnloadEvent), e);
3314 Assert.AreEqual (domain, (e as AppDomainUnloadEvent).Domain);
3316 // Run past the unload
3317 e = run_until ("domains_3");
3319 // Test access to unloaded types
3320 // FIXME: Add an exception type for this
3321 AssertThrows<Exception> (delegate {
3322 d_method.DeclaringType.GetValue (d_method.DeclaringType.GetField ("static_i"));
3325 // Check that .Domain is accessible for stack frames with native transitions
3326 e = run_until ("called_from_invoke");
3327 ThreadMirror.NativeTransitions = true;
3328 foreach (var f in e.Thread.GetFrames ()) {
3334 public void DynamicMethods () {
3335 Event e = run_until ("dyn_call");
3337 var m = e.Thread.GetFrames ()[1].Method;
3338 Assert.AreEqual ("dyn_method", m.Name);
3340 // Test access to IL
3341 var body = m.GetMethodBody ();
3343 ILInstruction ins = body.Instructions [0];
3344 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
3345 Assert.AreEqual ("FOO", ins.Operand);
3349 public void RefEmit () {
3352 Start (new string [] { "dtest-app.exe", "ref-emit-test" });
3354 Event e = run_until ("ref_emit_call");
3356 var m = e.Thread.GetFrames ()[1].Method;
3357 Assert.AreEqual ("ref_emit_method", m.Name);
3359 // Test access to IL
3360 var body = m.GetMethodBody ();
3364 ins = body.Instructions [0];
3365 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
3366 Assert.AreEqual ("FOO", ins.Operand);
3368 ins = body.Instructions [1];
3369 Assert.AreEqual (OpCodes.Call, ins.OpCode);
3370 Assert.IsInstanceOfType (typeof (MethodMirror), ins.Operand);
3371 Assert.AreEqual ("ref_emit_call", (ins.Operand as MethodMirror).Name);
3375 public void IsAttached () {
3376 var f = entry_point.DeclaringType.GetField ("is_attached");
3378 Event e = run_until ("Main");
3380 AssertValue (true, entry_point.DeclaringType.GetValue (f));
3384 public void StackTraceInNative () {
3385 // Check that stack traces can be produced for threads in native code
3388 Start (new string [] { "dtest-app.exe", "frames-in-native" });
3390 var e = run_until ("frames_in_native");
3392 // FIXME: This is racy
3399 StackFrame[] frames = e.Thread.GetFrames ();
3401 int frame_index = -1;
3402 for (int i = 0; i < frames.Length; ++i) {
3403 if (frames [i].Method.Name == "Sleep") {
3409 Assert.IsTrue (frame_index != -1);
3410 Assert.AreEqual ("Sleep", frames [frame_index].Method.Name);
3411 Assert.AreEqual ("frames_in_native", frames [frame_index + 1].Method.Name);
3412 Assert.AreEqual ("Main", frames [frame_index + 2].Method.Name);
3414 // Check that invokes are disabled for such threads
3415 TypeMirror t = frames [frame_index + 1].Method.DeclaringType;
3417 var m = t.GetMethod ("invoke_static_return_void");
3418 AssertThrows<InvalidOperationException> (delegate {
3419 t.InvokeMethod (e.Thread, m, null);
3422 // Check that the frame info is invalidated
3423 run_until ("frames_in_native_2");
3425 AssertThrows<InvalidStackFrameException> (delegate {
3426 Console.WriteLine (frames [frame_index].GetThis ());
3431 public void VirtualMachine_CreateEnumMirror () {
3432 var e = run_until ("o1");
3433 var frame = e.Thread.GetFrames () [0];
3435 object val = frame.GetThis ();
3436 Assert.IsTrue (val is ObjectMirror);
3437 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
3438 ObjectMirror o = (val as ObjectMirror);
3440 FieldInfoMirror field = o.Type.GetField ("field_enum");
3441 Value f = o.GetValue (field);
3442 TypeMirror enumType = (f as EnumMirror).Type;
3444 o.SetValue (field, vm.CreateEnumMirror (enumType, vm.CreateValue (1)));
3445 f = o.GetValue (field);
3446 Assert.AreEqual (1, (f as EnumMirror).Value);
3448 // Argument checking
3449 AssertThrows<ArgumentNullException> (delegate () {
3450 vm.CreateEnumMirror (enumType, null);
3453 AssertThrows<ArgumentNullException> (delegate () {
3454 vm.CreateEnumMirror (null, vm.CreateValue (1));
3458 AssertThrows<ArgumentException> (delegate () {
3459 vm.CreateEnumMirror (enumType, vm.CreateValue (null));
3462 // value of a wrong type
3463 AssertThrows<ArgumentException> (delegate () {
3464 vm.CreateEnumMirror (enumType, vm.CreateValue ((long)1));
3469 public void VirtualMachine_EnableEvents_Breakpoint () {
3470 AssertThrows<ArgumentException> (delegate () {
3471 vm.EnableEvents (EventType.Breakpoint);
3476 public void SingleStepRegress654694 () {
3479 MethodMirror m = entry_point.DeclaringType.GetMethod ("ss_regress_654694");
3480 foreach (Location l in m.Locations) {
3481 if (l.ILOffset > 0 && il_offset == -1)
3482 il_offset = l.ILOffset;
3485 Event e = run_until ("ss_regress_654694");
3487 Assert.IsNotNull (m);
3488 vm.SetBreakpoint (m, il_offset);
3492 e = GetNextEvent ();
3493 Assert.IsTrue (e is BreakpointEvent);
3495 var req = create_step (e);
3496 req.Depth = StepDepth.Over;
3497 req.Size = StepSize.Line;
3502 e = GetNextEvent ();
3503 Assert.IsTrue (e is StepEvent);
3509 public void DebugBreak () {
3510 vm.EnableEvents (EventType.UserBreak);
3515 var e = GetNextEvent ();
3516 Assert.IsTrue (e is UserBreakEvent);
3520 public void DebugLog () {
3521 vm.EnableEvents (EventType.UserLog);
3526 var e = GetNextEvent ();
3527 Assert.IsTrue (e is UserLogEvent);
3528 var le = e as UserLogEvent;
3530 Assert.AreEqual (5, le.Level);
3531 Assert.AreEqual ("A", le.Category);
3532 Assert.AreEqual ("B", le.Message);
3536 public void TypeGetMethodsByNameFlags () {
3538 var assembly = entry_point.DeclaringType.Assembly;
3539 var type = assembly.GetType ("Tests3");
3541 Assert.IsNotNull (type);
3543 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Public, false);
3544 Assert.AreEqual (1, mm.Length, "#1");
3545 Assert.AreEqual ("M1", mm[0].Name, "#2");
3547 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.NonPublic, false);
3548 Assert.AreEqual (1, mm.Length, "#3");
3549 Assert.AreEqual ("M2", mm[0].Name, "#4");
3551 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public, false);
3552 Assert.AreEqual (7, mm.Length, "#5"); //M3 plus Equals, GetHashCode, GetType, ToString, .ctor
3554 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, false);
3555 Assert.AreEqual (2, mm.Length, "#7");
3557 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
3558 Assert.AreEqual (1, mm.Length, "#9");
3560 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
3561 Assert.AreEqual (5, mm.Length, "#11");
3564 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Public, false);
3565 Assert.AreEqual (1, mm.Length, "#12");
3566 Assert.AreEqual ("M1", mm[0].Name, "#13");
3568 mm = type.GetMethodsByNameFlags ("m1", BindingFlags.Static | BindingFlags.Public, true);
3569 Assert.AreEqual (1, mm.Length, "#14");
3570 Assert.AreEqual ("M1", mm[0].Name, "#15");
3572 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, false);
3573 Assert.AreEqual (1, mm.Length, "#16");
3574 Assert.AreEqual ("M1", mm[0].Name, "#17");
3578 [Category ("only88")]
3579 public void TypeLoadSourceFileFilter () {
3580 Event e = run_until ("type_load");
3582 if (!vm.Version.AtLeast (2, 7))
3585 string srcfile = (e as BreakpointEvent).Method.DeclaringType.GetSourceFiles (true)[0];
3587 var req = vm.CreateTypeLoadRequest ();
3588 req.SourceFileFilter = new string [] { srcfile.ToUpper () };
3592 e = GetNextEvent ();
3593 Assert.IsTrue (e is TypeLoadEvent);
3594 Assert.AreEqual ("TypeLoadClass", (e as TypeLoadEvent).Type.FullName);
3598 public void TypeLoadTypeNameFilter () {
3599 Event e = run_until ("type_load");
3601 var req = vm.CreateTypeLoadRequest ();
3602 req.TypeNameFilter = new string [] { "TypeLoadClass2" };
3606 e = GetNextEvent ();
3607 Assert.IsTrue (e is TypeLoadEvent);
3608 Assert.AreEqual ("TypeLoadClass2", (e as TypeLoadEvent).Type.FullName);
3612 public void GetTypesForSourceFile () {
3615 var types = vm.GetTypesForSourceFile ("dtest-app.cs", false);
3616 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
3617 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
3619 types = vm.GetTypesForSourceFile ("DTEST-app.cs", true);
3620 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
3621 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
3625 public void GetTypesNamed () {
3628 var types = vm.GetTypes ("Tests", false);
3629 Assert.AreEqual (1, types.Count);
3630 Assert.AreEqual ("Tests", types [0].FullName);
3632 types = vm.GetTypes ("System.Exception", false);
3633 Assert.AreEqual (1, types.Count);
3634 Assert.AreEqual ("System.Exception", types [0].FullName);
3638 public void String_GetValue () {
3643 var e = run_until ("arg2");
3645 var frame = e.Thread.GetFrames () [0];
3647 val = frame.GetArgument (6);
3648 Assert.AreEqual ('\0'.ToString () + "A", (val as StringMirror).Value);
3652 public void String_GetChars () {
3656 var e = run_until ("arg2");
3658 var frame = e.Thread.GetFrames () [0];
3660 val = frame.GetArgument (0);
3661 Assert.IsTrue (val is StringMirror);
3662 AssertValue ("FOO", val);
3663 var s = (val as StringMirror);
3664 Assert.AreEqual (3, s.Length);
3666 var c = s.GetChars (0, 2);
3667 Assert.AreEqual (2, c.Length);
3668 Assert.AreEqual ('F', c [0]);
3669 Assert.AreEqual ('O', c [1]);
3671 AssertThrows<ArgumentException> (delegate () {
3677 public void GetInterfaces () {
3678 var e = run_until ("arg2");
3680 var frame = e.Thread.GetFrames () [0];
3682 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
3683 var ifaces = cl1.GetInterfaces ();
3684 Assert.AreEqual (1, ifaces.Length);
3685 Assert.AreEqual ("ITest", ifaces [0].Name);
3687 var cl2 = cl1.GetMethod ("Baz").ReturnType;
3688 var ifaces2 = cl2.GetInterfaces ();
3689 Assert.AreEqual (1, ifaces2.Length);
3690 Assert.AreEqual ("ITest`1", ifaces2 [0].Name);
3694 public void GetInterfaceMap () {
3695 var e = run_until ("arg2");
3697 var frame = e.Thread.GetFrames () [0];
3699 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
3700 var iface = cl1.Assembly.GetType ("ITest");
3701 var map = cl1.GetInterfaceMap (iface);
3702 Assert.AreEqual (cl1, map.TargetType);
3703 Assert.AreEqual (iface, map.InterfaceType);
3704 Assert.AreEqual (2, map.InterfaceMethods.Length);
3705 Assert.AreEqual (2, map.TargetMethods.Length);
3709 public void StackAlloc_Breakpoints_Regress2775 () {
3710 // Check that breakpoints on arm don't overwrite stackalloc-ed memory
3711 var e = run_until ("regress_2755");
3713 var frame = e.Thread.GetFrames () [0];
3715 // This breaks at the call site
3716 vm.SetBreakpoint (m, m.Locations [2].ILOffset);
3719 var e2 = GetNextEvent ();
3720 Assert.IsTrue (e2 is BreakpointEvent);
3722 e = run_until ("regress_2755_3");
3723 frame = e.Thread.GetFrames () [1];
3724 var res = frame.GetValue (m.GetLocal ("sum"));
3725 AssertValue (0, res);
3729 public void MethodInfo () {
3730 Event e = run_until ("locals2");
3732 StackFrame frame = e.Thread.GetFrames () [0];
3733 var m = frame.Method;
3735 Assert.IsTrue (m.IsGenericMethod);
3736 Assert.IsFalse (m.IsGenericMethodDefinition);
3738 var args = m.GetGenericArguments ();
3739 Assert.AreEqual (1, args.Length);
3740 Assert.AreEqual ("String", args [0].Name);
3742 var gmd = m.GetGenericMethodDefinition ();
3743 Assert.IsTrue (gmd.IsGenericMethod);
3744 Assert.IsTrue (gmd.IsGenericMethodDefinition);
3745 Assert.AreEqual (gmd, gmd.GetGenericMethodDefinition ());
3747 args = gmd.GetGenericArguments ();
3748 Assert.AreEqual (1, args.Length);
3749 Assert.AreEqual ("T", args [0].Name);
3751 var attrs = m.GetCustomAttributes (true);
3752 Assert.AreEqual (1, attrs.Length);
3753 Assert.AreEqual ("StateMachineAttribute", attrs [0].Constructor.DeclaringType.Name);
3757 public void UnhandledException () {
3760 Start (new string [] { "dtest-app.exe", "unhandled-exception" });
3762 var req = vm.CreateExceptionRequest (null, false, true);
3765 var e = run_until ("unhandled_exception");
3768 var e2 = GetNextEvent ();
3769 Assert.IsTrue (e2 is ExceptionEvent);
3776 public void UnhandledException_2 () {
3779 Start (new string [] { "dtest-app.exe", "unhandled-exception-endinvoke" });
3781 var req = vm.CreateExceptionRequest (null, false, true);
3784 MethodMirror m = entry_point.DeclaringType.GetMethod ("unhandled_exception_endinvoke_2");
3785 Assert.IsNotNull (m);
3786 vm.SetBreakpoint (m, m.ILOffsets [0]);
3788 var e = run_until ("unhandled_exception_endinvoke");
3791 var e2 = GetNextEvent ();
3792 Assert.IsFalse (e2 is ExceptionEvent);
3799 public void UnhandledExceptionUserCode () {
3802 // Exceptions caught in non-user code are treated as unhandled
3803 Start (new string [] { "dtest-app.exe", "unhandled-exception-user" });
3805 var req = vm.CreateExceptionRequest (null, false, true);
3806 req.AssemblyFilter = new List<AssemblyMirror> () { entry_point.DeclaringType.Assembly };
3809 var e = run_until ("unhandled_exception_user");
3812 var e2 = GetNextEvent ();
3813 Assert.IsTrue (e2 is ExceptionEvent);
3820 public void GCWhileSuspended () {
3821 // Check that objects are kept alive during suspensions
3822 Event e = run_until ("gc_suspend_1");
3824 MethodMirror m = entry_point.DeclaringType.GetMethod ("gc_suspend_invoke");
3826 var o = entry_point.DeclaringType.GetValue (entry_point.DeclaringType.GetField ("gc_suspend_field")) as ObjectMirror;
3827 //Console.WriteLine (o);
3829 StackFrame frame = e.Thread.GetFrames () [0];
3830 TypeMirror t = frame.Method.DeclaringType;
3831 for (int i = 0; i < 10; ++i)
3832 t.InvokeMethod (e.Thread, m, new Value [] { });
3834 // This throws an exception if the object is collected
3835 long addr = o.Address;
3837 var o2 = entry_point.DeclaringType.GetValue (entry_point.DeclaringType.GetField ("gc_suspend_field")) as ObjectMirror;
3842 public void MakeGenericMethod () {
3843 Event e = run_until ("bp1");
3845 var intm = vm.RootDomain.GetCorrespondingType (typeof (int));
3846 var stringm = vm.RootDomain.GetCorrespondingType (typeof (string));
3847 var gm = entry_point.DeclaringType.GetMethod ("generic_method");
3848 var res = gm.MakeGenericMethod (new TypeMirror [] { stringm });
3849 var args = res.GetGenericArguments ();
3850 Assert.AreEqual (1, args.Length);
3851 Assert.AreEqual (stringm, args [0]);
3854 AssertThrows<ArgumentNullException> (delegate {
3855 gm.MakeGenericMethod (null);
3857 AssertThrows<ArgumentNullException> (delegate {
3858 gm.MakeGenericMethod (new TypeMirror [] { null });
3860 AssertThrows<ArgumentException> (delegate {
3861 gm.MakeGenericMethod (new TypeMirror [] { stringm, stringm });
3863 AssertThrows<InvalidOperationException> (delegate {
3864 gm.MakeGenericMethod (new TypeMirror [] { intm });
3866 AssertThrows<InvalidOperationException> (delegate {
3867 entry_point.DeclaringType.GetMethod ("Main").MakeGenericMethod (new TypeMirror [] { intm });
3872 public void InspectThreadSuspenedOnWaitOne () {
3874 Start (true, "dtest-app.exe", "wait-one" );
3876 ThreadMirror.NativeTransitions = true;
3878 var evt = run_until ("wait_one");
3879 Assert.IsNotNull (evt, "#1");
3881 var thread = evt.Thread;
3882 Assert.AreEqual (ThreadState.Running, thread.ThreadState, "#1.1");
3884 var frames = thread.GetFrames ();
3885 Assert.IsNotNull (frames, "#2");
3886 Assert.AreEqual (2, frames.Length, "#3");
3887 Assert.AreEqual ("wait_one", frames [0].Method.Name, "#4");
3888 Assert.AreEqual ("Main", frames [1].Method.Name, "#5");
3892 Thread.Sleep (500); //FIXME this is racy, maybe single step? or something?
3895 Assert.AreEqual (ThreadState.WaitSleepJoin, thread.ThreadState, "#6");
3897 frames = thread.GetFrames ();
3898 Assert.AreEqual (8, frames.Length, "#7");
3899 Assert.AreEqual ("WaitOne_internal", frames [0].Method.Name, "#8.0");
3900 Assert.AreEqual ("WaitOneNative", frames [1].Method.Name, "#8.1");
3901 Assert.AreEqual ("InternalWaitOne", frames [2].Method.Name, "#8.2");
3902 Assert.AreEqual ("WaitOne", frames [3].Method.Name, "#8.3");
3903 Assert.AreEqual ("WaitOne", frames [4].Method.Name, "#8.4");
3904 Assert.AreEqual ("WaitOne", frames [5].Method.Name, "#8.5");
3905 Assert.AreEqual ("wait_one", frames [6].Method.Name, "#8.6");
3906 Assert.AreEqual ("Main", frames [7].Method.Name, "#8.7");
3908 var frame = frames [0];
3909 Assert.IsTrue (frame.IsNativeTransition, "#11.1");
3912 Assert.Fail ("Known limitation - can't get info from m2n frames");
3913 } catch (AbsentInformationException) {}
3916 Assert.IsFalse (frame.IsNativeTransition, "#12.1");
3917 var wait_one_this = frame.GetThis ();
3918 Assert.IsNotNull (wait_one_this, "#12.2");
3921 var locals = frame.GetVisibleVariables ();
3922 Assert.AreEqual (1, locals.Count, "#13.1");
3924 var local_0 = frame.GetValue (locals [0]);
3925 Assert.IsNotNull (local_0, "#13.2");
3927 Assert.AreEqual (wait_one_this, local_0, "#14.2");
3931 public void GetMethodBody () {
3932 var bevent = run_until ("Main");
3934 var m = bevent.Method.DeclaringType.GetMethod ("get_IntProperty");
3935 var body = m.GetMethodBody ();
3936 foreach (var ins in body.Instructions) {
3937 if (ins.OpCode == OpCodes.Ldfld) {
3938 var field = (FieldInfoMirror)ins.Operand;
3939 Assert.AreEqual ("field_i", field.Name);
3945 public void EvaluateMethod () {
3946 var bevent = run_until ("evaluate_method_2");
3948 var m = bevent.Method.DeclaringType.GetMethod ("get_IntProperty");
3950 var this_obj = bevent.Thread.GetFrames ()[0].GetThis ();
3951 var v = m.Evaluate (this_obj, null);
3952 AssertValue (42, v);
3956 public void SetIP () {
3957 var bevent = run_until ("set_ip_1");
3959 var invalid_loc = bevent.Thread.GetFrames ()[0].Location;
3961 var req = create_step (bevent);
3962 var e = step_out ();
3964 var frames = e.Thread.GetFrames ();
3965 var locs = frames [0].Method.Locations;
3966 var next_loc = locs.First (l => (l.LineNumber == frames [0].Location.LineNumber + 2));
3968 e.Thread.SetIP (next_loc);
3970 /* Check that i ++; j = 5; was skipped */
3971 bevent = run_until ("set_ip_2");
3972 var f = bevent.Thread.GetFrames ()[1];
3973 AssertValue (2, f.GetValue (f.Method.GetLocal ("i")));
3974 AssertValue (0, f.GetValue (f.Method.GetLocal ("j")));
3977 AssertThrows<ArgumentNullException> (delegate {
3978 e.Thread.SetIP (null);
3981 AssertThrows<ArgumentException> (delegate {
3982 e.Thread.SetIP (invalid_loc);
3987 public void SetIPSingleStep () {
3988 // Check that single stepping after set-ip steps from the new ip
3989 var bevent = run_until ("set_ip_1");
3991 var invalid_loc = bevent.Thread.GetFrames ()[0].Location;
3993 var req = create_step (bevent);
3994 req.Size = StepSize.Line;
3995 var e = step_out ();
3997 var frames = e.Thread.GetFrames ();
3998 var locs = frames [0].Method.Locations;
3999 var prev_loc = locs.First (l => (l.LineNumber == frames [0].Location.LineNumber - 3));
4000 AssertValue (2, frames [0].GetValue (frames [0].Method.GetLocal ("i")));
4002 // Set back the ip to the first i ++; line
4003 e.Thread.SetIP (prev_loc);
4006 var f = e.Thread.GetFrames ()[0];
4007 AssertValue (3, f.GetValue (f.Method.GetLocal ("i")));
4011 public void NewInstanceNoCtor () {
4012 var bevent = run_until ("Main");
4014 var stype = bevent.Method.DeclaringType.Assembly.GetType ("AStruct");
4015 var obj = stype.NewInstance ();
4016 Assert.IsTrue (obj is ObjectMirror);
4017 Assert.AreEqual ("AStruct", (obj as ObjectMirror).Type.Name);
4021 public void StaticCtorFilterInCctor () {
4022 // Check that single stepping when in a cctor only ignores
4023 // other cctors, not the current one
4024 var bevent = run_until ("step_filters");
4026 var assembly = entry_point.DeclaringType.Assembly;
4027 var type = assembly.GetType ("Tests/ClassWithCctor");
4028 var cctor = type.GetMethod (".cctor");
4029 vm.SetBreakpoint (cctor, 0);
4032 var e = vm.GetNextEvent ();
4033 Assert.IsTrue (e is BreakpointEvent);
4035 var req = create_step (e);
4036 req.Filter = StepFilter.StaticCtor;
4038 // Make sure we are still in the cctor
4039 Assert.AreEqual (".cctor", e.Thread.GetFrames ()[0].Location.Method.Name);
4043 public void ThreadpoolIOsinglestep () {
4045 Start ("dtest-app.exe", "threadpool-io");
4046 // This is a regression test for #42625. It tests the
4047 // interaction (particularly in coop GC) of the
4048 // threadpool I/O mechanism and the soft debugger.
4049 Event e = run_until ("threadpool_io");
4050 // run until we sent the task half the bytes it
4051 // expects, so that it blocks waiting for the rest.
4052 e = run_until ("threadpool_bp");
4053 var req = create_step (e);
4054 e = step_out (); // leave threadpool_bp
4055 e = step_out (); // leave threadpool_io