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);
562 // foreach (var x in m.Locations) {
563 // Console.WriteLine (x);
567 int method_base_linum = m.Locations [0].LineNumber;
568 foreach (var location in m.Locations)
569 if (location.LineNumber == method_base_linum + 2) {
570 offset = location.ILOffset;
574 var req = vm.SetBreakpoint (m, offset);
581 if (e is BreakpointEvent)
587 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
588 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
590 e = single_step (e.Thread);
592 var frame = e.Thread.GetFrames ()[0];
594 Assert.IsNotNull (frame);
595 var field = frame.Method.GetLocal ("reflectMe");
596 Assert.IsNotNull (field);
597 Value variable = frame.GetValue (field);
599 ObjectMirror thisObj = (ObjectMirror)variable;
600 TypeMirror thisType = thisObj.Type;
601 FieldInfoMirror thisFi = null;
602 foreach (var fi in thisType.GetFields ())
603 if (fi.Name == "someField")
606 var gotVal = thisObj.GetValue (thisFi);
607 // If we got this far, we're good.
611 public void SingleStepping () {
612 Event e = run_until ("single_stepping");
614 var req = create_step (e);
617 // Step over 'bool b = true'
619 assert_location (e, "single_stepping");
626 assert_location (e, "ss1");
633 assert_location (e, "single_stepping");
637 assert_location (e, "single_stepping");
641 assert_location (e, "ss3");
643 // Step back into single_stepping
645 assert_location (e, "single_stepping");
647 // Step into next line
649 assert_location (e, "single_stepping");
651 // Step into ss3_2 ()
653 assert_location (e, "ss3_2");
655 // Step over ss3_2_2 ()
657 assert_location (e, "ss3_2");
659 // Recreate the request
666 // Step back into single_stepping () with the new request
668 assert_location (e, "single_stepping");
672 assert_location (e, "ss4");
677 // Change to StepSize.Line
679 req.Depth = StepDepth.Over;
680 req.Size = StepSize.Line;
683 // Step over ss1 (); ss1 ();
688 req.Depth = StepDepth.Into;
692 assert_location (e, "ss2");
697 e = run_until ("ss5");
699 // Add an assembly filter
700 req.AssemblyFilter = new AssemblyMirror [] { (e as BreakpointEvent).Method.DeclaringType.Assembly };
706 // Step into is_even, skipping the linq stuff
708 assert_location (e, "is_even");
710 // FIXME: Check that single stepping works with lock (obj)
714 e = run_until ("ss6");
716 req = create_step (e);
717 req.Depth = StepDepth.Over;
720 // Check that single stepping works in out-of-line bblocks
723 assert_location (e, "ss6");
726 // Check that a step over stops at an EH clause
727 e = run_until ("ss7_2");
728 req = create_step (e);
729 req.Depth = StepDepth.Out;
732 assert_location (e, "ss7");
734 req = create_step (e);
735 req.Depth = StepDepth.Over;
738 assert_location (e, "ss7");
741 // Check that stepping stops between nested calls
742 e = run_until ("ss_nested_2");
744 assert_location (e, "ss_nested");
746 assert_location (e, "ss_nested_1");
748 assert_location (e, "ss_nested");
749 // Check that step over steps over nested calls
751 assert_location (e, "ss_nested");
753 assert_location (e, "ss_nested_1");
755 assert_location (e, "ss_nested_1");
757 assert_location (e, "ss_nested");
760 // Check DebuggerStepThrough support
761 e = run_until ("ss_step_through");
762 req = create_step (e);
763 req.Filter = StepFilter.DebuggerStepThrough;
765 // Step through step_through_1 ()
767 assert_location (e, "ss_step_through");
768 // Step through StepThroughClass.step_through_2 ()
770 assert_location (e, "ss_step_through");
772 req.Filter = StepFilter.None;
774 assert_location (e, "step_through_3");
777 // Check DebuggerNonUserCode support
778 e = run_until ("ss_non_user_code");
779 req = create_step (e);
780 req.Filter = StepFilter.DebuggerNonUserCode;
782 // Step through non_user_code_1 ()
784 assert_location (e, "ss_non_user_code");
785 // Step through StepThroughClass.non_user_code_2 ()
787 assert_location (e, "ss_non_user_code");
789 req.Filter = StepFilter.None;
791 assert_location (e, "non_user_code_3");
794 // Check that step-over doesn't stop at inner frames with recursive functions
795 e = run_until ("ss_recursive");
796 req = create_step (e);
800 var f = e.Thread.GetFrames () [0];
801 assert_location (e, "ss_recursive");
802 AssertValue (1, f.GetValue (f.Method.GetLocal ("n")));
805 // Check that step-over stops correctly when inner frames with recursive functions contain breakpoints
806 e = run_until ("ss_recursive2");
807 ReusableBreakpoint breakpoint = new ReusableBreakpoint (this, "ss_recursive2_trap");
809 breakpoint.Continue ();
810 e = breakpoint.lastEvent;
811 req = create_step (e);
812 for (int c = 1; c <= 4; c++) {
813 // The first five times we try to step over this function, the breakpoint will stop us
814 assert_location_at_breakpoint (e, "ss_recursive2_trap");
817 req = create_step (e);
818 req.Size = StepSize.Line;
821 assert_location (e, "ss_recursive2");
823 // Stack should consist of Main + single_stepping + (1 ss_recursive2 frame per loop iteration)
824 Assert.AreEqual (c+2, e.Thread.GetFrames ().Length);
825 e = step_over_or_breakpoint ();
827 // At this point we should have escaped the breakpoints and this will be a normal step stop
828 assert_location (e, "ss_recursive2");
829 Assert.AreEqual (6, e.Thread.GetFrames ().Length);
832 breakpoint.Disable ();
835 // Check that step-out stops correctly when inner frames with recursive functions contain breakpoints
836 e = run_until ("ss_recursive2");
837 breakpoint = new ReusableBreakpoint (this, "ss_recursive2_trap");
839 breakpoint.Continue ();
840 e = breakpoint.lastEvent;
841 req = create_step (e);
842 for (int c = 1; c <= 4; c++) {
843 // The first five times we try to step over this function, the breakpoint will stop us
844 assert_location_at_breakpoint (e, "ss_recursive2_trap");
847 req = create_step (e);
848 req.Size = StepSize.Line;
851 assert_location (e, "ss_recursive2");
853 // Stack should consist of Main + single_stepping + (1 ss_recursive2 frame per loop iteration)
854 Assert.AreEqual (c+2, e.Thread.GetFrames ().Length);
855 e = step_out_or_breakpoint ();
857 for (int c = 3; c >= 1; c--) {
858 assert_location (e, "ss_recursive2");
859 Assert.AreEqual (c + 2, e.Thread.GetFrames ().Length);
865 breakpoint.Disable ();
868 // Test step out with a really complicated call tree
869 List<RecursiveChaoticPoint> trace = new List<RecursiveChaoticPoint>();
870 trace_ss_recursive_chaotic (trace);
871 e = run_until ("ss_recursive_chaotic");
873 breakpoint = new ReusableBreakpoint (this, "ss_recursive_chaotic_trap");
874 breakpoint.Continue ();
875 e = breakpoint.lastEvent;
876 foreach (RecursiveChaoticPoint point in trace)
878 if (point.breakpoint)
879 assert_location_at_breakpoint (e, point.name);
881 assert_location (e, point.name);
882 Assert.AreEqual (point.depth, e.Thread.GetFrames ().Length);
885 req = create_step (e);
886 req.Size = StepSize.Line;
887 e = step_out_or_breakpoint ();
891 breakpoint.Disable ();
894 // Check that single stepping doesn't clobber fp values
895 e = run_until ("ss_fp_clobber");
896 req = create_step (e);
898 f = e.Thread.GetFrames ()[0];
900 if ((e as StepEvent).Method.Name == "ss_fp_clobber_2")
904 f = e.Thread.GetFrames ()[0];
905 AssertValue (7.0, f.GetValue (f.Method.GetParameters ()[0]));
910 public void MethodEntryExit () {
911 run_until ("single_stepping");
913 var req1 = vm.CreateMethodEntryRequest ();
914 var req2 = vm.CreateMethodExitRequest ();
920 Event e = GetNextEvent ();
921 Assert.IsTrue (e is MethodEntryEvent);
922 Assert.AreEqual ("ss1", (e as MethodEntryEvent).Method.Name);
926 Assert.IsTrue (e is MethodExitEvent);
927 Assert.AreEqual ("ss1", (e as MethodExitEvent).Method.Name);
934 public void CountFilter () {
935 run_until ("single_stepping");
937 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("ss3");
938 Assert.IsNotNull (m2);
939 vm.SetBreakpoint (m2, 0);
941 var req1 = vm.CreateMethodEntryRequest ();
945 // Enter ss2, ss1 is skipped
947 Event e = GetNextEvent ();
948 Assert.IsTrue (e is MethodEntryEvent);
949 Assert.AreEqual ("ss2", (e as MethodEntryEvent).Method.Name);
951 // Breakpoint on ss3, the entry event is no longer reported
954 Assert.IsTrue (e is BreakpointEvent);
960 public void Arguments () {
963 var e = run_until ("arg1");
965 StackFrame frame = e.Thread.GetFrames () [0];
967 check_arg_val (frame, 0, typeof (sbyte), SByte.MaxValue - 5);
968 check_arg_val (frame, 1, typeof (byte), Byte.MaxValue - 5);
969 check_arg_val (frame, 2, typeof (bool), true);
970 check_arg_val (frame, 3, typeof (short), Int16.MaxValue - 5);
971 check_arg_val (frame, 4, typeof (ushort), UInt16.MaxValue - 5);
972 check_arg_val (frame, 5, typeof (char), 'F');
973 check_arg_val (frame, 6, typeof (int), Int32.MaxValue - 5);
974 check_arg_val (frame, 7, typeof (uint), UInt32.MaxValue - 5);
975 check_arg_val (frame, 8, typeof (long), Int64.MaxValue - 5);
976 check_arg_val (frame, 9, typeof (ulong), UInt64.MaxValue - 5);
977 check_arg_val (frame, 10, typeof (float), 1.2345f);
978 check_arg_val (frame, 11, typeof (double), 6.78910);
980 e = run_until ("arg2");
982 frame = e.Thread.GetFrames () [0];
985 val = frame.GetArgument (0);
986 AssertValue ("FOO", val);
987 Assert.AreEqual ("String", (val as ObjectMirror).Type.Name);
990 val = frame.GetArgument (1);
991 AssertValue (null, val);
994 val = frame.GetArgument (2);
995 AssertValue ("BLA", val);
998 val = frame.GetArgument (3);
999 AssertValue (42, val);
1002 val = frame.GetArgument (4);
1003 Assert.IsTrue (val is ObjectMirror);
1004 Assert.AreEqual ("GClass`1", (val as ObjectMirror).Type.Name);
1007 val = frame.GetArgument (5);
1008 Assert.IsTrue (val is ObjectMirror);
1009 Assert.AreEqual ("Object", (val as ObjectMirror).Type.Name);
1011 // this on static methods
1012 val = frame.GetThis ();
1013 AssertValue (null, val);
1015 e = run_until ("arg3");
1017 frame = e.Thread.GetFrames () [0];
1020 val = frame.GetThis ();
1021 Assert.IsTrue (val is ObjectMirror);
1022 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
1024 // objref in register
1025 val = frame.GetArgument (0);
1026 AssertValue ("BLA", val);
1030 public void Arrays () {
1033 var e = run_until ("o2");
1035 StackFrame frame = e.Thread.GetFrames () [0];
1038 val = frame.GetArgument (0);
1039 Assert.IsTrue (val is ArrayMirror);
1040 ArrayMirror arr = val as ArrayMirror;
1041 Assert.AreEqual (2, arr.Length);
1042 AssertValue ("BAR", arr [0]);
1043 AssertValue ("BAZ", arr [1]);
1045 var vals = arr.GetValues (0, 2);
1046 Assert.AreEqual (2, vals.Count);
1047 AssertValue ("BAR", vals [0]);
1048 AssertValue ("BAZ", vals [1]);
1050 arr [0] = vm.RootDomain.CreateString ("ABC");
1051 AssertValue ("ABC", arr [0]);
1053 arr [0] = vm.CreateValue (null);
1054 AssertValue (null, arr [0]);
1056 arr.SetValues (0, new Value [] { vm.RootDomain.CreateString ("D1"), vm.RootDomain.CreateString ("D2") });
1057 AssertValue ("D1", arr [0]);
1058 AssertValue ("D2", arr [1]);
1061 val = frame.GetArgument (1);
1062 Assert.IsTrue (val is ArrayMirror);
1063 arr = val as ArrayMirror;
1064 Assert.AreEqual (2, arr.Length);
1065 AssertValue (42, arr [0]);
1066 AssertValue (43, arr [1]);
1068 // Argument checking
1069 AssertThrows<IndexOutOfRangeException> (delegate () {
1073 AssertThrows<IndexOutOfRangeException> (delegate () {
1074 val = arr [Int32.MinValue];
1077 AssertThrows<IndexOutOfRangeException> (delegate () {
1078 vals = arr.GetValues (0, 3);
1081 AssertThrows<IndexOutOfRangeException> (delegate () {
1082 arr [2] = vm.CreateValue (null);
1085 AssertThrows<IndexOutOfRangeException> (delegate () {
1086 arr [Int32.MinValue] = vm.CreateValue (null);
1089 AssertThrows<IndexOutOfRangeException> (delegate () {
1090 arr.SetValues (0, new Value [] { null, null, null });
1094 val = frame.GetArgument (2);
1095 Assert.IsTrue (val is ArrayMirror);
1096 arr = val as ArrayMirror;
1097 Assert.AreEqual (2, arr.Rank);
1098 Assert.AreEqual (4, arr.Length);
1099 Assert.AreEqual (2, arr.GetLength (0));
1100 Assert.AreEqual (2, arr.GetLength (1));
1101 Assert.AreEqual (0, arr.GetLowerBound (0));
1102 Assert.AreEqual (0, arr.GetLowerBound (1));
1103 vals = arr.GetValues (0, 4);
1104 AssertValue (1, vals [0]);
1105 AssertValue (2, vals [1]);
1106 AssertValue (3, vals [2]);
1107 AssertValue (4, vals [3]);
1109 val = frame.GetArgument (3);
1110 Assert.IsTrue (val is ArrayMirror);
1111 arr = val as ArrayMirror;
1112 Assert.AreEqual (2, arr.Rank);
1113 Assert.AreEqual (4, arr.Length);
1114 Assert.AreEqual (2, arr.GetLength (0));
1115 Assert.AreEqual (2, arr.GetLength (1));
1116 Assert.AreEqual (1, arr.GetLowerBound (0));
1117 Assert.AreEqual (3, arr.GetLowerBound (1));
1119 AssertThrows<ArgumentOutOfRangeException> (delegate () {
1122 AssertThrows<ArgumentOutOfRangeException> (delegate () {
1126 AssertThrows<ArgumentOutOfRangeException> (delegate () {
1127 arr.GetLowerBound (-1);
1129 AssertThrows<ArgumentOutOfRangeException> (delegate () {
1130 arr.GetLowerBound (2);
1133 // arrays treated as generic collections
1134 val = frame.GetArgument (4);
1135 Assert.IsTrue (val is ArrayMirror);
1136 arr = val as ArrayMirror;
1140 public void Object_GetValue () {
1141 var e = run_until ("o1");
1142 var frame = e.Thread.GetFrames () [0];
1144 object val = frame.GetThis ();
1145 Assert.IsTrue (val is ObjectMirror);
1146 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
1147 ObjectMirror o = (val as ObjectMirror);
1149 TypeMirror t = o.Type;
1152 object f = o.GetValue (t.GetField ("field_i"));
1153 AssertValue (42, f);
1154 f = o.GetValue (t.GetField ("field_s"));
1155 AssertValue ("S", f);
1156 f = o.GetValue (t.GetField ("field_enum"));
1157 Assert.IsTrue (f is EnumMirror);
1158 Assert.AreEqual (1, (f as EnumMirror).Value);
1159 Assert.AreEqual ("B", (f as EnumMirror).StringValue);
1161 // Inherited object fields
1162 TypeMirror parent = t.BaseType;
1163 f = o.GetValue (parent.GetField ("base_field_i"));
1164 AssertValue (43, f);
1165 f = o.GetValue (parent.GetField ("base_field_s"));
1166 AssertValue ("T", f);
1169 f = o.GetValue (o.Type.GetField ("static_i"));
1170 AssertValue (55, f);
1172 // generic instances
1173 ObjectMirror o2 = frame.GetValue (frame.Method.GetParameters ()[1]) as ObjectMirror;
1174 Assert.AreEqual ("GClass`1", o2.Type.Name);
1175 TypeMirror t2 = o2.Type;
1176 f = o2.GetValue (t2.GetField ("field"));
1177 AssertValue (42, f);
1179 ObjectMirror o3 = frame.GetValue (frame.Method.GetParameters ()[2]) as ObjectMirror;
1180 Assert.AreEqual ("GClass`1", o3.Type.Name);
1181 TypeMirror t3 = o3.Type;
1182 f = o3.GetValue (t3.GetField ("field"));
1183 AssertValue ("FOO", f);
1185 // Argument checking
1186 AssertThrows<ArgumentNullException> (delegate () {
1192 public void Object_GetValues () {
1193 var e = run_until ("o1");
1194 var frame = e.Thread.GetFrames () [0];
1196 object val = frame.GetThis ();
1197 Assert.IsTrue (val is ObjectMirror);
1198 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
1199 ObjectMirror o = (val as ObjectMirror);
1201 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1203 TypeMirror t = o.Type;
1205 object[] vals = o.GetValues (new FieldInfoMirror [] { t.GetField ("field_i"), t.GetField ("field_s") });
1206 object f = vals [0];
1207 AssertValue (42, f);
1209 AssertValue ("S", f);
1211 // Argument checking
1212 AssertThrows<ArgumentNullException> (delegate () {
1216 AssertThrows<ArgumentNullException> (delegate () {
1217 o.GetValues (new FieldInfoMirror [] { null });
1220 // field of another class
1221 AssertThrows<ArgumentException> (delegate () {
1222 o.GetValue (val2.Type.GetField ("field_j"));
1226 void TestSetValue (ObjectMirror o, string field_name, object val) {
1228 o.SetValue (o.Type.GetField (field_name), vm.RootDomain.CreateString ((string)val));
1230 o.SetValue (o.Type.GetField (field_name), vm.CreateValue (val));
1231 Value f = o.GetValue (o.Type.GetField (field_name));
1232 AssertValue (val, f);
1236 public void Object_SetValues () {
1237 var e = run_until ("o1");
1238 var frame = e.Thread.GetFrames () [0];
1240 object val = frame.GetThis ();
1241 Assert.IsTrue (val is ObjectMirror);
1242 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
1243 ObjectMirror o = (val as ObjectMirror);
1245 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1247 TestSetValue (o, "field_i", 22);
1248 TestSetValue (o, "field_bool1", false);
1249 TestSetValue (o, "field_bool2", true);
1250 TestSetValue (o, "field_char", 'B');
1251 TestSetValue (o, "field_byte", (byte)129);
1252 TestSetValue (o, "field_sbyte", (sbyte)-33);
1253 TestSetValue (o, "field_short", (short)(Int16.MaxValue - 5));
1254 TestSetValue (o, "field_ushort", (ushort)(UInt16.MaxValue - 5));
1255 TestSetValue (o, "field_long", Int64.MaxValue - 5);
1256 TestSetValue (o, "field_ulong", (ulong)(UInt64.MaxValue - 5));
1257 TestSetValue (o, "field_float", 6.28f);
1258 TestSetValue (o, "field_double", 6.28);
1259 TestSetValue (o, "static_i", 23);
1260 TestSetValue (o, "field_s", "CDEF");
1265 f = o.GetValue (o.Type.GetField ("field_intptr"));
1266 Assert.IsInstanceOfType (typeof (StructMirror), f);
1267 AssertValue (Int32.MaxValue - 5, (f as StructMirror).Fields [0]);
1270 FieldInfoMirror field = o.Type.GetField ("field_enum");
1271 f = o.GetValue (field);
1272 (f as EnumMirror).Value = 5;
1273 o.SetValue (field, f);
1274 f = o.GetValue (field);
1275 Assert.AreEqual (5, (f as EnumMirror).Value);
1278 o.SetValue (o.Type.GetField ("field_s"), vm.CreateValue (null));
1279 f = o.GetValue (o.Type.GetField ("field_s"));
1280 AssertValue (null, f);
1283 field = o.Type.GetField ("generic_field_struct");
1284 f = o.GetValue (field);
1285 o.SetValue (field, f);
1288 field = o.Type.GetField ("field_nullable");
1289 f = o.GetValue (field);
1290 AssertValue (0, (f as StructMirror).Fields [0]);
1291 AssertValue (false, (f as StructMirror).Fields [1]);
1292 o.SetValue (field, vm.CreateValue (6));
1293 f = o.GetValue (field);
1294 AssertValue (6, (f as StructMirror).Fields [0]);
1295 AssertValue (true, (f as StructMirror).Fields [1]);
1296 o.SetValue (field, vm.CreateValue (null));
1297 f = o.GetValue (field);
1298 AssertValue (0, (f as StructMirror).Fields [0]);
1299 AssertValue (false, (f as StructMirror).Fields [1]);
1301 // Argument checking
1302 AssertThrows<ArgumentNullException> (delegate () {
1303 o.SetValues (null, new Value [0]);
1306 AssertThrows<ArgumentNullException> (delegate () {
1307 o.SetValues (new FieldInfoMirror [0], null);
1310 AssertThrows<ArgumentNullException> (delegate () {
1311 o.SetValues (new FieldInfoMirror [] { null }, new Value [1] { null });
1314 // vtype with a wrong type
1315 AssertThrows<ArgumentException> (delegate () {
1316 o.SetValue (o.Type.GetField ("field_struct"), o.GetValue (o.Type.GetField ("field_enum")));
1319 // reference type not assignment compatible
1320 AssertThrows<ArgumentException> (delegate () {
1321 o.SetValue (o.Type.GetField ("field_class"), o);
1324 // field of another class
1325 AssertThrows<ArgumentException> (delegate () {
1326 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
1331 public void Type_SetValue () {
1332 var e = run_until ("o1");
1333 var frame = e.Thread.GetFrames () [0];
1336 object val = frame.GetThis ();
1337 Assert.IsTrue (val is ObjectMirror);
1338 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
1339 ObjectMirror o = (val as ObjectMirror);
1341 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1343 o.Type.SetValue (o.Type.GetField ("static_i"), vm.CreateValue (55));
1344 f = o.Type.GetValue (o.Type.GetField ("static_i"));
1345 AssertValue (55, f);
1347 o.Type.SetValue (o.Type.GetField ("static_s"), vm.RootDomain.CreateString ("B"));
1348 f = o.Type.GetValue (o.Type.GetField ("static_s"));
1349 AssertValue ("B", f);
1351 // Argument checking
1352 AssertThrows<ArgumentNullException> (delegate () {
1353 o.Type.SetValue (null, vm.CreateValue (0));
1356 AssertThrows<ArgumentNullException> (delegate () {
1357 o.Type.SetValue (o.Type.GetField ("static_i"), null);
1360 // field of another class
1361 AssertThrows<ArgumentException> (delegate () {
1362 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
1367 public void TypeInfo () {
1368 Event e = run_until ("ti2");
1369 StackFrame frame = e.Thread.GetFrames () [0];
1374 t = frame.Method.GetParameters ()[0].ParameterType;
1376 Assert.AreEqual ("String[]", t.Name);
1377 Assert.AreEqual ("string[]", t.CSharpName);
1378 Assert.AreEqual ("Array", t.BaseType.Name);
1379 Assert.AreEqual (true, t.HasElementType);
1380 Assert.AreEqual (true, t.IsArray);
1381 Assert.AreEqual (1, t.GetArrayRank ());
1382 Assert.AreEqual ("String", t.GetElementType ().Name);
1384 t = frame.Method.GetParameters ()[2].ParameterType;
1386 Assert.AreEqual ("Int32[,]", t.Name);
1388 //Assert.AreEqual ("int[,]", t.CSharpName);
1389 Assert.AreEqual ("Array", t.BaseType.Name);
1390 Assert.AreEqual (true, t.HasElementType);
1391 Assert.AreEqual (true, t.IsArray);
1392 Assert.AreEqual (2, t.GetArrayRank ());
1393 Assert.AreEqual ("Int32", t.GetElementType ().Name);
1396 t = frame.Method.GetParameters ()[3].ParameterType;
1398 //Assert.AreEqual ("Int32&", t.Name);
1399 //Assert.AreEqual (true, t.IsByRef);
1400 //Assert.AreEqual (true, t.HasElementType);
1403 t = frame.Method.GetParameters ()[4].ParameterType;
1405 //Assert.AreEqual ("Int32*", t.Name);
1406 Assert.AreEqual (true, t.IsPointer);
1407 Assert.AreEqual (true, t.HasElementType);
1408 Assert.AreEqual ("Int32", t.GetElementType ().Name);
1409 Assert.AreEqual (false, t.IsPrimitive);
1412 t = frame.Method.GetParameters ()[5].ParameterType;
1413 Assert.AreEqual (true, t.IsPrimitive);
1416 t = frame.Method.GetParameters ()[6].ParameterType;
1417 Assert.AreEqual ("AStruct", t.Name);
1418 Assert.AreEqual (false, t.IsPrimitive);
1419 Assert.AreEqual (true, t.IsValueType);
1420 Assert.AreEqual (false, t.IsClass);
1423 t = frame.Method.GetParameters ()[7].ParameterType;
1424 Assert.AreEqual ("Tests", t.Name);
1425 var nested = (from nt in t.GetNestedTypes () where nt.IsNestedPublic select nt).ToArray ();
1426 Assert.AreEqual (1, nested.Length);
1427 Assert.AreEqual ("NestedClass", nested [0].Name);
1428 Assert.IsTrue (t.BaseType.IsAssignableFrom (t));
1429 Assert.IsTrue (!t.IsAssignableFrom (t.BaseType));
1431 // generic instances
1432 t = frame.Method.GetParameters ()[9].ParameterType;
1433 Assert.AreEqual ("GClass`1", t.Name);
1434 Assert.IsTrue (t.IsGenericType);
1435 Assert.IsFalse (t.IsGenericTypeDefinition);
1437 var args = t.GetGenericArguments ();
1438 Assert.AreEqual (1, args.Length);
1439 Assert.AreEqual ("Int32", args [0].Name);
1441 // generic type definitions
1442 var gtd = t.GetGenericTypeDefinition ();
1443 Assert.AreEqual ("GClass`1", gtd.Name);
1444 Assert.IsTrue (gtd.IsGenericType);
1445 Assert.IsTrue (gtd.IsGenericTypeDefinition);
1446 Assert.AreEqual (gtd, gtd.GetGenericTypeDefinition ());
1448 args = gtd.GetGenericArguments ();
1449 Assert.AreEqual (1, args.Length);
1450 Assert.AreEqual ("T", args [0].Name);
1453 t = frame.Method.GetParameters ()[10].ParameterType;
1454 Assert.AreEqual ("AnEnum", t.Name);
1455 Assert.IsTrue (t.IsEnum);
1456 Assert.AreEqual ("Int32", t.EnumUnderlyingType.Name);
1459 t = frame.Method.GetParameters ()[11].ParameterType;
1460 Assert.AreEqual ("TypedReference", t.Name);
1463 t = frame.Method.GetParameters ()[7].ParameterType;
1465 var props = t.GetProperties ();
1466 Assert.AreEqual (3, props.Length);
1467 foreach (PropertyInfoMirror prop in props) {
1468 ParameterInfoMirror[] indexes = prop.GetIndexParameters ();
1470 if (prop.Name == "IntProperty") {
1471 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1472 Assert.AreEqual ("get_IntProperty", prop.GetGetMethod ().Name);
1473 Assert.AreEqual ("set_IntProperty", prop.GetSetMethod ().Name);
1474 Assert.AreEqual (0, indexes.Length);
1475 } else if (prop.Name == "ReadOnlyProperty") {
1476 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1477 Assert.AreEqual ("get_ReadOnlyProperty", prop.GetGetMethod ().Name);
1478 Assert.AreEqual (null, prop.GetSetMethod ());
1479 Assert.AreEqual (0, indexes.Length);
1480 } else if (prop.Name == "IndexedProperty") {
1481 Assert.AreEqual (1, indexes.Length);
1482 Assert.AreEqual ("Int32", indexes [0].ParameterType.Name);
1486 // custom attributes
1487 t = frame.Method.GetParameters ()[8].ParameterType;
1488 Assert.AreEqual ("Tests2", t.Name);
1489 var attrs = t.GetCustomAttributes (true);
1490 Assert.AreEqual (5, attrs.Length);
1491 foreach (var attr in attrs) {
1492 if (attr.Constructor.DeclaringType.Name == "DebuggerDisplayAttribute") {
1493 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1494 Assert.AreEqual ("Tests", attr.ConstructorArguments [0].Value);
1495 Assert.AreEqual (2, attr.NamedArguments.Count);
1496 Assert.AreEqual ("Name", attr.NamedArguments [0].Property.Name);
1497 Assert.AreEqual ("FOO", attr.NamedArguments [0].TypedValue.Value);
1498 Assert.AreEqual ("Target", attr.NamedArguments [1].Property.Name);
1499 Assert.IsInstanceOfType (typeof (TypeMirror), attr.NamedArguments [1].TypedValue.Value);
1500 Assert.AreEqual ("Int32", (attr.NamedArguments [1].TypedValue.Value as TypeMirror).Name);
1501 } else if (attr.Constructor.DeclaringType.Name == "DebuggerTypeProxyAttribute") {
1502 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1503 Assert.IsInstanceOfType (typeof (TypeMirror), attr.ConstructorArguments [0].Value);
1504 Assert.AreEqual ("Tests", (attr.ConstructorArguments [0].Value as TypeMirror).Name);
1505 } else if (attr.Constructor.DeclaringType.Name == "BAttribute") {
1506 Assert.AreEqual (2, attr.NamedArguments.Count);
1507 Assert.AreEqual ("afield", attr.NamedArguments [0].Field.Name);
1508 Assert.AreEqual ("bfield", attr.NamedArguments [1].Field.Name);
1509 } else if (attr.Constructor.DeclaringType.Name == "ClassInterfaceAttribute") {
1510 // inherited from System.Object
1511 //} else if (attr.Constructor.DeclaringType.Name == "Serializable") {
1512 // inherited from System.Object
1513 } else if (attr.Constructor.DeclaringType.Name == "ComVisibleAttribute") {
1514 // inherited from System.Object
1516 Assert.Fail (attr.Constructor.DeclaringType.Name);
1520 var assembly = entry_point.DeclaringType.Assembly;
1521 var type = assembly.GetType ("Tests4");
1522 Assert.IsFalse (type.IsInitialized);
1526 public void FieldInfo () {
1527 Event e = run_until ("ti2");
1528 StackFrame frame = e.Thread.GetFrames () [0];
1532 t = frame.Method.GetParameters ()[8].ParameterType;
1533 Assert.AreEqual ("Tests2", t.Name);
1535 var fi = t.GetField ("field_j");
1536 var attrs = fi.GetCustomAttributes (true);
1537 Assert.AreEqual (1, attrs.Length);
1538 var attr = attrs [0];
1539 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1540 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1541 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1542 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1546 public void PropertyInfo () {
1547 Event e = run_until ("ti2");
1548 StackFrame frame = e.Thread.GetFrames () [0];
1552 t = frame.Method.GetParameters ()[8].ParameterType;
1553 Assert.AreEqual ("Tests2", t.Name);
1555 var pi = t.GetProperty ("AProperty");
1556 var attrs = pi.GetCustomAttributes (true);
1557 Assert.AreEqual (1, attrs.Length);
1558 var attr = attrs [0];
1559 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1560 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1561 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1562 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1566 [Category ("only5")]
1567 public void Type_GetValue () {
1568 Event e = run_until ("o1");
1569 StackFrame frame = e.Thread.GetFrames () [0];
1571 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1573 TypeMirror t = o.Type;
1575 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1578 object f = t.GetValue (o.Type.GetField ("static_i"));
1579 AssertValue (55, f);
1581 f = t.GetValue (o.Type.GetField ("static_s"));
1582 AssertValue ("A", f);
1584 // literal static fields
1585 f = t.GetValue (o.Type.GetField ("literal_i"));
1586 AssertValue (56, f);
1588 f = t.GetValue (o.Type.GetField ("literal_s"));
1589 AssertValue ("B", f);
1591 // Inherited static fields
1592 TypeMirror parent = t.BaseType;
1593 f = t.GetValue (parent.GetField ("base_static_i"));
1594 AssertValue (57, f);
1596 f = t.GetValue (parent.GetField ("base_static_s"));
1597 AssertValue ("C", f);
1599 // thread static field
1600 f = t.GetValue (t.GetField ("tls_i"), e.Thread);
1601 AssertValue (42, f);
1603 // Argument checking
1604 AssertThrows<ArgumentNullException> (delegate () {
1609 AssertThrows<ArgumentException> (delegate () {
1610 t.GetValue (o.Type.GetField ("field_i"));
1613 // field on another type
1614 AssertThrows<ArgumentException> (delegate () {
1615 t.GetValue (val2.Type.GetField ("static_field_j"));
1618 // special static field
1619 AssertThrows<ArgumentException> (delegate () {
1620 t.GetValue (t.GetField ("tls_i"));
1625 public void Type_GetValues () {
1626 Event e = run_until ("o1");
1627 StackFrame frame = e.Thread.GetFrames () [0];
1629 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1631 TypeMirror t = o.Type;
1634 object[] vals = t.GetValues (new FieldInfoMirror [] { t.GetField ("static_i"), t.GetField ("static_s") });
1635 object f = vals [0];
1636 AssertValue (55, f);
1639 AssertValue ("A", f);
1641 // Argument checking
1642 AssertThrows<ArgumentNullException> (delegate () {
1646 AssertThrows<ArgumentNullException> (delegate () {
1647 t.GetValues (new FieldInfoMirror [] { null });
1652 public void ObjRefs () {
1653 Event e = run_until ("objrefs1");
1654 StackFrame frame = e.Thread.GetFrames () [0];
1656 ObjectMirror o = frame.GetThis () as ObjectMirror;
1657 ObjectMirror child = o.GetValue (o.Type.GetField ("child")) as ObjectMirror;
1659 Assert.IsTrue (child.Address != 0);
1661 // Check that object references are internalized correctly
1662 Assert.AreEqual (o, frame.GetThis ());
1664 run_until ("objrefs2");
1666 // child should be gc'd now
1667 // This is not deterministic
1668 //Assert.IsTrue (child.IsCollected);
1671 * No longer works since Type is read eagerly
1674 AssertThrows<ObjectCollectedException> (delegate () {
1675 TypeMirror t = child.Type;
1679 AssertThrows<ObjectCollectedException> (delegate () {
1680 long addr = child.Address;
1686 public void Type_GetObject () {
1687 Event e = run_until ("o1");
1688 StackFrame frame = e.Thread.GetFrames () [0];
1690 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1692 TypeMirror t = o.Type;
1694 Assert.AreEqual ("RuntimeType", t.GetTypeObject ().Type.Name);
1698 public void VTypes () {
1699 Event e = run_until ("vtypes1");
1700 StackFrame frame = e.Thread.GetFrames () [0];
1703 ObjectMirror o = frame.GetThis () as ObjectMirror;
1704 var obj = o.GetValue (o.Type.GetField ("field_struct"));
1705 Assert.IsTrue (obj is StructMirror);
1706 var s = obj as StructMirror;
1707 Assert.AreEqual ("AStruct", s.Type.Name);
1708 AssertValue (42, s ["i"]);
1710 AssertValue ("S", obj);
1711 AssertValue (43, s ["k"]);
1712 obj = o.GetValue (o.Type.GetField ("field_boxed_struct"));
1713 Assert.IsTrue (obj is StructMirror);
1714 s = obj as StructMirror;
1715 Assert.AreEqual ("AStruct", s.Type.Name);
1716 AssertValue (42, s ["i"]);
1718 // Check decoding of nested structs (#14942)
1719 obj = o.GetValue (o.Type.GetField ("nested_struct"));
1720 o.SetValue (o.Type.GetField ("nested_struct"), obj);
1722 // Check round tripping of boxed struct fields (#12354)
1723 obj = o.GetValue (o.Type.GetField ("boxed_struct_field"));
1724 o.SetValue (o.Type.GetField ("boxed_struct_field"), obj);
1725 obj = o.GetValue (o.Type.GetField ("boxed_struct_field"));
1726 s = obj as StructMirror;
1727 AssertValue (1, s ["key"]);
1729 Assert.IsTrue (obj is StructMirror);
1730 s = obj as StructMirror;
1731 AssertValue (42, s ["m_value"]);
1733 // vtypes as arguments
1734 s = frame.GetArgument (0) as StructMirror;
1735 AssertValue (44, s ["i"]);
1737 AssertValue ("T", obj);
1738 AssertValue (45, s ["k"]);
1740 // vtypes as array entries
1741 var arr = frame.GetArgument (1) as ArrayMirror;
1743 Assert.IsTrue (obj is StructMirror);
1744 s = obj as StructMirror;
1745 AssertValue (1, s ["i"]);
1746 AssertValue ("S1", s ["s"]);
1748 Assert.IsTrue (obj is StructMirror);
1749 s = obj as StructMirror;
1750 AssertValue (2, s ["i"]);
1751 AssertValue ("S2", s ["s"]);
1754 var typedref = frame.GetArgument (2) as StructMirror;
1755 Assert.IsTrue (typedref is StructMirror);
1757 // Argument checking
1758 s = frame.GetArgument (0) as StructMirror;
1759 AssertThrows<ArgumentException> (delegate () {
1763 // generic vtype instances
1764 o = frame.GetThis () as ObjectMirror;
1765 obj = o.GetValue (o.Type.GetField ("generic_field_struct"));
1766 Assert.IsTrue (obj is StructMirror);
1767 s = obj as StructMirror;
1768 Assert.AreEqual ("GStruct`1", s.Type.Name);
1769 AssertValue (42, s ["i"]);
1771 // this on vtype methods
1772 e = run_until ("vtypes2");
1773 e = step_until (e.Thread, "foo");
1775 frame = e.Thread.GetFrames () [0];
1777 Assert.AreEqual ("foo", (e as StepEvent).Method.Name);
1778 obj = frame.GetThis ();
1780 Assert.IsTrue (obj is StructMirror);
1781 s = obj as StructMirror;
1782 AssertValue (44, s ["i"]);
1783 AssertValue ("T", s ["s"]);
1784 AssertValue (45, s ["k"]);
1787 s ["i"] = vm.CreateValue (55);
1789 obj = frame.GetThis ();
1790 Assert.IsTrue (obj is StructMirror);
1791 s = obj as StructMirror;
1792 AssertValue (55, s ["i"]);
1793 AssertValue ("T", s ["s"]);
1794 AssertValue (45, s ["k"]);
1796 // this on static vtype methods
1797 e = run_until ("vtypes3");
1798 e = step_until (e.Thread, "static_foo");
1800 frame = e.Thread.GetFrames () [0];
1802 Assert.AreEqual ("static_foo", (e as StepEvent).Method.Name);
1803 obj = frame.GetThis ();
1804 AssertValue (null, obj);
1806 // vtypes which reference themselves recursively
1807 e = run_until ("vtypes4_2");
1808 frame = e.Thread.GetFrames () [0];
1810 Assert.IsTrue (frame.GetArgument (0) is StructMirror);
1814 public void AssemblyInfo () {
1815 Event e = run_until ("single_stepping");
1817 StackFrame frame = e.Thread.GetFrames () [0];
1819 var aname = frame.Method.DeclaringType.Assembly.GetName ();
1820 Assert.AreEqual ("dtest-app, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", aname.ToString ());
1822 ModuleMirror m = frame.Method.DeclaringType.Module;
1824 Assert.AreEqual ("dtest-app.exe", m.Name);
1825 Assert.AreEqual ("dtest-app.exe", m.ScopeName);
1826 Assert.IsTrue (m.FullyQualifiedName.IndexOf ("dtest-app.exe") != -1);
1827 Guid guid = m.ModuleVersionId;
1828 Assert.AreEqual (frame.Method.DeclaringType.Assembly, m.Assembly);
1829 Assert.AreEqual (frame.Method.DeclaringType.Assembly.ManifestModule, m);
1831 // This is no longer true on 4.0
1832 //Assert.AreEqual ("Assembly", frame.Method.DeclaringType.Assembly.GetAssemblyObject ().Type.Name);
1834 TypeMirror t = vm.RootDomain.Corlib.GetType ("System.Diagnostics.DebuggerDisplayAttribute");
1835 Assert.AreEqual ("DebuggerDisplayAttribute", t.Name);
1839 public void LocalsInfo () {
1840 Event e = run_until ("locals2");
1842 StackFrame frame = e.Thread.GetFrames () [0];
1844 var locals = frame.Method.GetLocals ();
1845 Assert.AreEqual (9, locals.Length);
1846 for (int i = 0; i < 9; ++i) {
1847 if (locals [i].Name == "args") {
1848 Assert.IsTrue (locals [i].IsArg);
1849 Assert.AreEqual ("String[]", locals [i].Type.Name);
1850 } else if (locals [i].Name == "arg") {
1851 Assert.IsTrue (locals [i].IsArg);
1852 Assert.AreEqual ("Int32", locals [i].Type.Name);
1853 } else if (locals [i].Name == "i") {
1854 Assert.IsFalse (locals [i].IsArg);
1855 Assert.AreEqual ("Int64", locals [i].Type.Name);
1856 } else if (locals [i].Name == "j") {
1857 Assert.IsFalse (locals [i].IsArg);
1858 Assert.AreEqual ("Int32", locals [i].Type.Name);
1859 } else if (locals [i].Name == "s") {
1860 Assert.IsFalse (locals [i].IsArg);
1861 Assert.AreEqual ("String", locals [i].Type.Name);
1862 } else if (locals [i].Name == "t") {
1864 Assert.IsTrue (locals [i].IsArg);
1865 Assert.AreEqual ("String", locals [i].Type.Name);
1866 } else if (locals [i].Name == "rs") {
1867 Assert.IsTrue (locals [i].IsArg);
1868 Assert.AreEqual ("String", locals [i].Type.Name);
1869 } else if (locals [i].Name == "astruct") {
1870 } else if (locals [i].Name == "alist") {
1876 var scopes = frame.Method.GetScopes ();
1877 Assert.AreEqual (2, scopes.Length);
1880 Event step_once () {
1882 var e = GetNextEvent ();
1883 Assert.IsTrue (e is StepEvent);
1887 Event step_into () {
1888 step_req.Disable ();
1889 step_req.Depth = StepDepth.Into;
1891 return step_once ();
1894 Event step_over () {
1895 step_req.Disable ();
1896 step_req.Depth = StepDepth.Over;
1898 return step_once ();
1902 step_req.Disable ();
1903 step_req.Depth = StepDepth.Out;
1905 return step_once ();
1908 Event step_once_or_breakpoint () {
1910 var e = GetNextEvent ();
1911 Assert.IsTrue (e is StepEvent || e is BreakpointEvent);
1915 Event step_over_or_breakpoint () {
1916 step_req.Disable ();
1917 step_req.Depth = StepDepth.Over;
1919 return step_once_or_breakpoint ();
1922 Event step_out_or_breakpoint () {
1923 step_req.Disable ();
1924 step_req.Depth = StepDepth.Out;
1926 return step_once_or_breakpoint ();
1930 public void Locals () {
1931 var be = run_until ("locals1");
1933 StackFrame frame = be.Thread.GetFrames () [0];
1934 MethodMirror m1 = frame.Method;
1936 // Compiler generated byref local
1937 foreach (var l in m1.GetLocals ()) {
1938 // The byval flag is hidden from the type
1939 if (l.Name != "ri" && l.Type.Name == "Double")
1940 AssertValue (null, frame.GetValue (l));
1943 be = run_until ("locals2");
1945 frame = be.Thread.GetFrames () [0];
1947 object val = frame.GetValue (frame.Method.GetLocal ("i"));
1948 AssertValue (0, val);
1950 var req = create_step (be);
1957 var e = step_once ();
1958 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1960 // Execute s = "AB";
1962 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1964 frame = e.Thread.GetFrames () [0];
1966 val = frame.GetValue (frame.Method.GetLocal ("i"));
1967 AssertValue (42, val);
1969 LocalVariable[] locals = frame.Method.GetLocals ();
1970 var vals = frame.GetValues (locals);
1971 Assert.AreEqual (locals.Length, vals.Length);
1972 for (int i = 0; i < locals.Length; ++i) {
1973 if (locals [i].Name == "i")
1974 AssertValue (42, vals [i]);
1975 if (locals [i].Name == "s")
1976 AssertValue ("AB", vals [i]);
1977 if (locals [i].Name == "t")
1978 AssertValue ("ABC", vals [i]);
1979 if (locals [i].Name == "alist") {
1983 // Argument checking
1986 AssertThrows<ArgumentNullException> (delegate () {
1987 frame.GetValue ((LocalVariable)null);
1989 // GetValue () local from another method
1990 AssertThrows<ArgumentException> (delegate () {
1991 frame.GetValue (m1.GetLocal ("foo"));
1995 AssertThrows<ArgumentNullException> (delegate () {
1996 frame.GetValue ((ParameterInfoMirror)null);
1998 // GetValue () local from another method
1999 AssertThrows<ArgumentException> (delegate () {
2000 frame.GetValue (m1.GetParameters ()[0]);
2003 // GetValues () null
2004 AssertThrows<ArgumentNullException> (delegate () {
2005 frame.GetValues (null);
2007 // GetValues () embedded null
2008 AssertThrows<ArgumentNullException> (delegate () {
2009 frame.GetValues (new LocalVariable [] { null });
2011 // GetValues () local from another method
2012 AssertThrows<ArgumentException> (delegate () {
2013 frame.GetValues (new LocalVariable [] { m1.GetLocal ("foo") });
2016 AssertThrows<ArgumentException> (delegate () {
2017 val = frame.GetValue (frame.Method.ReturnParameter);
2020 // invalid stack frames
2022 e = GetNextEvent ();
2023 Assert.IsTrue (e is StepEvent);
2024 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
2026 AssertThrows<InvalidStackFrameException> (delegate () {
2027 frame.GetValue (frame.Method.GetLocal ("i"));
2033 be = run_until ("locals7");
2035 req = create_step (be);
2041 // Test that locals are initialized
2042 frame = e.Thread.GetFrames () [0];
2043 val = frame.GetValue (frame.Method.GetLocal ("t"));
2044 AssertValue (0, val);
2048 Assert.AreEqual ("locals7", (e as StepEvent).Method.Name);
2052 Assert.AreEqual ("locals7", (e as StepEvent).Method.Name);
2054 frame = e.Thread.GetFrames () [0];
2055 val = frame.GetValue (frame.Method.GetParameters ()[0]);
2056 AssertValue (22, val);
2057 val = frame.GetValue (frame.Method.GetLocal ("t"));
2058 AssertValue (22, val);
2059 val = frame.GetValue (frame.Method.GetLocal ("t2"));
2060 AssertValue (22, val);
2064 public void GetVisibleVariables () {
2065 Event e = run_until ("locals4");
2068 var locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
2069 Assert.AreEqual (2, locals.Count);
2070 var loc = locals.First (l => l.Name == "i");
2071 Assert.AreEqual ("Int64", loc.Type.Name);
2072 loc = locals.First (l => l.Name == "s");
2073 Assert.AreEqual ("String", loc.Type.Name);
2075 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
2076 Assert.AreEqual ("i", loc.Name);
2077 Assert.AreEqual ("Int64", loc.Type.Name);
2079 e = run_until ("locals5");
2082 locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
2083 Assert.AreEqual (2, locals.Count);
2084 loc = locals.First (l => l.Name == "i");
2085 Assert.AreEqual ("String", loc.Type.Name);
2086 loc = locals.First (l => l.Name == "s");
2087 Assert.AreEqual ("String", loc.Type.Name);
2089 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
2090 Assert.AreEqual ("i", loc.Name);
2091 Assert.AreEqual ("String", loc.Type.Name);
2093 // Variable in another scope
2094 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("j");
2095 Assert.IsNull (loc);
2099 public void Exit () {
2104 var e = GetNextEvent ();
2105 Assert.IsInstanceOfType (typeof (VMDeathEvent), e);
2107 Assert.AreEqual (5, (e as VMDeathEvent).ExitCode);
2110 /* Could be a remote vm with no process */
2113 Assert.AreEqual (5, p.ExitCode);
2116 AssertThrows<VMDisconnectedException> (delegate () {
2125 public void Dispose () {
2130 var e = GetNextEvent ();
2131 Assert.IsInstanceOfType (typeof (VMDisconnectEvent), e);
2134 /* Could be a remote vm with no process */
2137 Assert.AreEqual (3, p.ExitCode);
2140 AssertThrows<VMDisconnectedException> (delegate () {
2149 public void ColumnNumbers () {
2150 Event e = run_until ("line_numbers");
2152 // FIXME: Merge this with LineNumbers () when its fixed
2154 step_req = create_step (e);
2155 step_req.Depth = StepDepth.Into;
2163 e = GetNextEvent ();
2164 Assert.IsTrue (e is StepEvent);
2165 if (e.Thread.GetFrames ()[0].Method.Name == "ln1")
2169 // Do an additional step over so we are not on the beginning line of the method
2170 step_req.Disable ();
2171 step_req.Depth = StepDepth.Over;
2174 e = GetNextEvent ();
2175 Assert.IsTrue (e is StepEvent);
2177 l = e.Thread.GetFrames ()[0].Location;
2179 Assert.AreEqual (3, l.ColumnNumber);
2181 step_req.Disable ();
2185 // Broken by mcs+runtime changes (#5438)
2186 [Category("NotWorking")]
2187 public void LineNumbers () {
2188 Event e = run_until ("line_numbers");
2190 step_req = create_step (e);
2191 step_req.Depth = StepDepth.Into;
2198 e = GetNextEvent ();
2199 Assert.IsTrue (e is StepEvent);
2201 l = e.Thread.GetFrames ()[0].Location;
2203 Assert.IsTrue (l.SourceFile.IndexOf ("dtest-app.cs") != -1);
2204 Assert.AreEqual ("ln1", l.Method.Name);
2207 using (FileStream fs = new FileStream (l.SourceFile, FileMode.Open, FileAccess.Read)) {
2208 MD5 md5 = MD5.Create ();
2209 var hash = md5.ComputeHash (fs);
2211 for (int i = 0; i < 16; ++i)
2212 Assert.AreEqual (hash [i], l.SourceFileHash [i]);
2215 int line_base = l.LineNumber;
2218 e = GetNextEvent ();
2219 Assert.IsTrue (e is StepEvent);
2220 l = e.Thread.GetFrames ()[0].Location;
2221 Assert.AreEqual ("ln2", l.Method.Name);
2222 Assert.AreEqual (line_base + 6, l.LineNumber);
2225 e = GetNextEvent ();
2226 Assert.IsTrue (e is StepEvent);
2227 l = e.Thread.GetFrames ()[0].Location;
2228 Assert.AreEqual ("ln1", l.Method.Name);
2229 Assert.AreEqual (line_base + 1, l.LineNumber);
2232 e = GetNextEvent ();
2233 Assert.IsTrue (e is StepEvent);
2234 l = e.Thread.GetFrames ()[0].Location;
2235 Assert.AreEqual ("ln3", l.Method.Name);
2236 Assert.AreEqual (line_base + 11, l.LineNumber);
2239 e = GetNextEvent ();
2240 Assert.IsTrue (e is StepEvent);
2241 l = e.Thread.GetFrames ()[0].Location;
2242 Assert.AreEqual ("ln3", l.Method.Name);
2243 Assert.IsTrue (l.SourceFile.EndsWith ("FOO"));
2244 Assert.AreEqual (55, l.LineNumber);
2247 e = GetNextEvent ();
2248 Assert.IsTrue (e is StepEvent);
2249 l = e.Thread.GetFrames ()[0].Location;
2250 Assert.AreEqual ("ln1", l.Method.Name);
2251 Assert.AreEqual (line_base + 2, l.LineNumber);
2253 // GetSourceFiles ()
2254 string[] sources = l.Method.DeclaringType.GetSourceFiles ();
2255 Assert.AreEqual (2, sources.Length);
2256 Assert.AreEqual ("dtest-app.cs", sources [0]);
2257 Assert.AreEqual ("FOO", sources [1]);
2259 sources = l.Method.DeclaringType.GetSourceFiles (true);
2260 Assert.AreEqual (2, sources.Length);
2261 Assert.IsTrue (sources [0].EndsWith ("dtest-app.cs"));
2262 Assert.IsTrue (sources [1].EndsWith ("FOO"));
2266 public void Suspend () {
2269 Start (new string [] { "dtest-app.exe", "suspend-test" });
2271 Event e = run_until ("suspend");
2273 ThreadMirror main = e.Thread;
2281 // The debuggee should be suspended while it is running the infinite loop
2283 StackFrame frame = main.GetFrames ()[0];
2284 Assert.AreEqual ("suspend", frame.Method.Name);
2288 // resuming when not suspended
2289 AssertThrows<InvalidOperationException> (delegate () {
2299 public void AssemblyLoad () {
2300 Event e = run_until ("assembly_load");
2302 var load_req = vm.CreateAssemblyLoadRequest ();
2307 e = GetNextEvent ();
2308 Assert.IsInstanceOfType (typeof (AssemblyLoadEvent), e);
2309 Assert.IsTrue ((e as AssemblyLoadEvent).Assembly.Location.EndsWith ("System.dll"));
2311 var frames = e.Thread.GetFrames ();
2312 Assert.IsTrue (frames.Length > 0);
2313 Assert.AreEqual ("assembly_load", frames [0].Method.Name);
2317 public void CreateValue () {
2320 v = vm.CreateValue (1);
2321 Assert.AreEqual (vm, v.VirtualMachine);
2322 Assert.AreEqual (1, v.Value);
2324 v = vm.CreateValue (null);
2325 Assert.AreEqual (vm, v.VirtualMachine);
2326 Assert.AreEqual (null, v.Value);
2328 // Argument checking
2329 AssertThrows <ArgumentException> (delegate () {
2330 v = vm.CreateValue ("FOO");
2335 public void CreateString () {
2336 StringMirror s = vm.RootDomain.CreateString ("ABC");
2338 Assert.AreEqual (vm, s.VirtualMachine);
2339 Assert.AreEqual ("ABC", s.Value);
2340 Assert.AreEqual (vm.RootDomain, s.Domain);
2343 StringBuilder sb = new StringBuilder ();
2344 for (int i = 0; i < 1024; ++i)
2346 s = vm.RootDomain.CreateString (sb.ToString ());
2348 // Argument checking
2349 AssertThrows <ArgumentNullException> (delegate () {
2350 s = vm.RootDomain.CreateString (null);
2355 public void CreateBoxedValue () {
2356 ObjectMirror o = vm.RootDomain.CreateBoxedValue (new PrimitiveValue (vm, 42));
2358 Assert.AreEqual ("Int32", o.Type.Name);
2359 //AssertValue (42, m.GetValue (o.Type.GetField ("m_value")));
2361 // Argument checking
2362 AssertThrows <ArgumentNullException> (delegate () {
2363 vm.RootDomain.CreateBoxedValue (null);
2366 AssertThrows <ArgumentException> (delegate () {
2367 vm.RootDomain.CreateBoxedValue (o);
2372 public void Invoke () {
2373 Event e = run_until ("invoke1");
2375 StackFrame frame = e.Thread.GetFrames () [0];
2377 TypeMirror t = frame.Method.DeclaringType;
2378 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2380 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2386 m = t.GetMethod ("invoke_return_void");
2387 v = this_obj.InvokeMethod (e.Thread, m, null);
2391 m = t.GetMethod ("invoke_return_ref");
2392 v = this_obj.InvokeMethod (e.Thread, m, null);
2393 AssertValue ("ABC", v);
2396 m = t.GetMethod ("invoke_return_null");
2397 v = this_obj.InvokeMethod (e.Thread, m, null);
2398 AssertValue (null, v);
2401 m = t.GetMethod ("invoke_return_primitive");
2402 v = this_obj.InvokeMethod (e.Thread, m, null);
2403 AssertValue (42, v);
2406 m = t.GetMethod ("invoke_return_nullable");
2407 v = this_obj.InvokeMethod (e.Thread, m, null);
2408 Assert.IsInstanceOfType (typeof (StructMirror), v);
2409 var s = v as StructMirror;
2410 AssertValue (42, s.Fields [0]);
2411 AssertValue (true, s.Fields [1]);
2413 // pass nullable as this
2414 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2415 m = s.Type.GetMethod ("ToString");
2416 v = s.InvokeMethod (e.Thread, m, null);
2418 // return nullable null
2419 m = t.GetMethod ("invoke_return_nullable_null");
2420 v = this_obj.InvokeMethod (e.Thread, m, null);
2421 Assert.IsInstanceOfType (typeof (StructMirror), v);
2422 s = v as StructMirror;
2423 AssertValue (0, s.Fields [0]);
2424 AssertValue (false, s.Fields [1]);
2426 // pass nullable as this
2427 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2428 m = s.Type.GetMethod ("ToString");
2429 v = s.InvokeMethod (e.Thread, m, null);
2432 m = t.GetMethod ("invoke_pass_primitive");
2433 Value[] args = new Value [] {
2434 vm.CreateValue ((byte)Byte.MaxValue),
2435 vm.CreateValue ((sbyte)SByte.MaxValue),
2436 vm.CreateValue ((short)1),
2437 vm.CreateValue ((ushort)1),
2438 vm.CreateValue ((int)1),
2439 vm.CreateValue ((uint)1),
2440 vm.CreateValue ((long)1),
2441 vm.CreateValue ((ulong)1),
2442 vm.CreateValue ('A'),
2443 vm.CreateValue (true),
2444 vm.CreateValue (3.14f),
2445 vm.CreateValue (3.14) };
2447 v = this_obj.InvokeMethod (e.Thread, m, args);
2448 AssertValue ((int)Byte.MaxValue + (int)SByte.MaxValue + 1 + 1 + 1 + 1 + 1 + 1 + 'A' + 1 + 3 + 3, v);
2451 m = t.GetMethod ("invoke_pass_ref");
2452 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2453 AssertValue ("ABC", v);
2456 m = t.GetMethod ("invoke_pass_ref");
2457 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (null) });
2458 AssertValue (null, v);
2461 m = t.GetMethod ("invoke_static_pass_ref");
2462 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2463 AssertValue ("ABC", v);
2465 // static invoked using ObjectMirror.InvokeMethod
2466 m = t.GetMethod ("invoke_static_pass_ref");
2467 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2468 AssertValue ("ABC", v);
2470 // method which throws an exception
2472 m = t.GetMethod ("invoke_throws");
2473 v = this_obj.InvokeMethod (e.Thread, m, null);
2475 } catch (InvocationException ex) {
2476 Assert.AreEqual ("Exception", ex.Exception.Type.Name);
2480 m = t.GetMethod ("invoke_out");
2481 var out_task = this_obj.InvokeMethodAsyncWithResult (e.Thread, m, new Value [] { vm.CreateValue (1), vm.CreateValue (null) }, InvokeOptions.ReturnOutArgs);
2482 var out_args = out_task.Result.OutArgs;
2483 AssertValue (5, out_args [0]);
2484 Assert.IsTrue (out_args [1] is ArrayMirror);
2485 Assert.AreEqual (10, (out_args [1] as ArrayMirror).Length);
2487 // without ReturnOutArgs flag
2488 out_task = this_obj.InvokeMethodAsyncWithResult (e.Thread, m, new Value [] { vm.CreateValue (1), vm.CreateValue (null) });
2489 out_args = out_task.Result.OutArgs;
2490 Assert.IsNull (out_args);
2493 m = t.GetMethod (".ctor");
2494 v = t.InvokeMethod (e.Thread, m, null);
2495 Assert.IsInstanceOfType (typeof (ObjectMirror), v);
2496 Assert.AreEqual ("Tests", (v as ObjectMirror).Type.Name);
2499 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("ITest2");
2500 m = cl1.GetMethod ("invoke_iface");
2501 v = this_obj.InvokeMethod (e.Thread, m, null);
2502 AssertValue (42, v);
2505 m = t.BaseType.GetMethod ("virtual_method");
2506 v = this_obj.InvokeMethod (e.Thread, m, null, InvokeOptions.Virtual);
2507 AssertValue ("V2", v);
2509 // virtual call on static method
2510 m = t.GetMethod ("invoke_static_pass_ref");
2511 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") }, InvokeOptions.Virtual);
2512 AssertValue ("ABC", v);
2515 m = t.GetMethod ("invoke_pass_ref");
2516 var task = this_obj.InvokeMethodAsync (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2517 AssertValue ("ABC", task.Result);
2520 m = t.GetMethod ("invoke_static_pass_ref");
2521 task = t.InvokeMethodAsync (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2522 AssertValue ("ABC", task.Result);
2524 // Argument checking
2527 AssertThrows<ArgumentNullException> (delegate {
2528 m = t.GetMethod ("invoke_pass_ref");
2529 v = this_obj.InvokeMethod (null, m, new Value [] { vm.CreateValue (null) });
2533 AssertThrows<ArgumentNullException> (delegate {
2534 v = this_obj.InvokeMethod (e.Thread, null, new Value [] { vm.CreateValue (null) });
2537 // invalid number of arguments
2538 m = t.GetMethod ("invoke_pass_ref");
2539 AssertThrows<ArgumentException> (delegate {
2540 v = this_obj.InvokeMethod (e.Thread, m, null);
2543 // invalid type of argument (ref != primitive)
2544 m = t.GetMethod ("invoke_pass_ref");
2545 AssertThrows<ArgumentException> (delegate {
2546 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2549 // invalid type of argument (primitive != primitive)
2550 m = t.GetMethod ("invoke_pass_primitive_2");
2551 AssertThrows<ArgumentException> (delegate {
2552 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2555 // invoking a non-static method as static
2556 m = t.GetMethod ("invoke_pass_ref");
2557 AssertThrows<ArgumentException> (delegate {
2558 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2561 // invoking a method defined in another class
2562 m = t2.GetMethod ("invoke");
2563 AssertThrows<ArgumentException> (delegate {
2564 v = this_obj.InvokeMethod (e.Thread, m, null);
2569 public void InvokeVType () {
2570 Event e = run_until ("invoke1");
2572 StackFrame frame = e.Thread.GetFrames () [0];
2574 var s = frame.GetArgument (1) as StructMirror;
2576 TypeMirror t = s.Type;
2581 // Pass struct as this, receive int
2582 m = t.GetMethod ("invoke_return_int");
2583 v = s.InvokeMethod (e.Thread, m, null);
2584 AssertValue (42, v);
2586 // Pass boxed struct as this
2587 var boxed_this = t.NewInstance () as ObjectMirror;
2588 m = t.GetMethod ("invoke_return_int");
2589 v = boxed_this.InvokeMethod (e.Thread, m, null);
2592 // Pass struct as this, receive intptr
2593 m = t.GetMethod ("invoke_return_intptr");
2594 v = s.InvokeMethod (e.Thread, m, null);
2595 AssertValue (43, v);
2598 m = t.GetMethod ("invoke_static");
2599 v = t.InvokeMethod (e.Thread, m, null);
2602 // Pass generic struct as this
2603 s = frame.GetArgument (2) as StructMirror;
2605 m = t.GetMethod ("invoke_return_int");
2606 v = s.InvokeMethod (e.Thread, m, null);
2607 AssertValue (42, v);
2610 s = frame.GetArgument (1) as StructMirror;
2612 m = t.GetMethods ().First (method => method.Name == ".ctor" && method.GetParameters ().Length == 1);
2613 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2614 AssertValue (1, (v as StructMirror)["i"]);
2616 // Invoke a method which changes state
2617 s = frame.GetArgument (1) as StructMirror;
2619 m = t.GetMethod ("invoke_mutate");
2620 var task = s.InvokeMethodAsyncWithResult (e.Thread, m, null, InvokeOptions.ReturnOutThis);
2621 var out_this = task.Result.OutThis as StructMirror;
2622 AssertValue (5, out_this ["l"]);
2624 // Without the ReturnOutThis flag
2625 s = frame.GetArgument (1) as StructMirror;
2627 m = t.GetMethod ("invoke_mutate");
2628 task = s.InvokeMethodAsyncWithResult (e.Thread, m, null);
2629 out_this = task.Result.OutThis as StructMirror;
2630 Assert.AreEqual (null, out_this);
2633 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("ITest2");
2634 m = cl1.GetMethod ("invoke_iface");
2635 v = s.InvokeMethod (e.Thread, m, null);
2636 AssertValue (42, v);
2639 m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2640 v = s.InvokeMethod (e.Thread, m, null, InvokeOptions.Virtual);
2641 AssertValue ("42", v);
2645 public void BreakpointDuringInvoke () {
2646 Event e = run_until ("invoke1");
2648 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke2");
2649 Assert.IsNotNull (m);
2650 vm.SetBreakpoint (m, 0);
2652 StackFrame frame = e.Thread.GetFrames () [0];
2653 var o = frame.GetThis () as ObjectMirror;
2655 bool failed = false;
2657 bool finished = false;
2658 object wait = new object ();
2660 // Have to invoke in a separate thread as the invoke is suspended until we
2661 // resume after the breakpoint
2662 Thread t = new Thread (delegate () {
2664 o.InvokeMethod (e.Thread, m, null);
2670 Monitor.Pulse (wait);
2676 StackFrame invoke_frame = null;
2679 e = GetNextEvent ();
2680 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
2681 // Check stack trace support and invokes
2682 var frames = e.Thread.GetFrames ();
2683 invoke_frame = frames [0];
2684 Assert.AreEqual ("invoke2", frames [0].Method.Name);
2685 Assert.IsTrue (frames [0].IsDebuggerInvoke);
2686 Assert.AreEqual ("invoke1", frames [1].Method.Name);
2693 Monitor.Wait (wait);
2696 // Check that the invoke frames are no longer valid
2697 AssertThrows<InvalidStackFrameException> (delegate {
2698 invoke_frame.GetThis ();
2701 // Check InvokeOptions.DisableBreakpoints flag
2702 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2706 public void DisabledExceptionDuringInvoke () {
2707 Event e = run_until ("invoke_ex");
2709 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_ex_inner");
2711 StackFrame frame = e.Thread.GetFrames () [0];
2712 var o = frame.GetThis () as ObjectMirror;
2714 var req = vm.CreateExceptionRequest (null);
2717 // Check InvokeOptions.DisableBreakpoints flag
2718 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2724 public void InvokeSingleThreaded () {
2727 Start (new string [] { "dtest-app.exe", "invoke-single-threaded" });
2729 Event e = run_until ("invoke_single_threaded_2");
2731 StackFrame f = e.Thread.GetFrames ()[0];
2733 var obj = f.GetThis () as ObjectMirror;
2735 // Check that the counter value incremented by the other thread does not increase
2736 // during the invoke.
2737 object counter1 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2739 var m = obj.Type.GetMethod ("invoke_return_void");
2740 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2742 object counter2 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2744 Assert.AreEqual ((int)counter1, (int)counter2);
2746 // Test multiple invokes done in succession
2747 m = obj.Type.GetMethod ("invoke_return_void");
2748 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2750 // Test events during single-threaded invokes
2751 vm.EnableEvents (EventType.TypeLoad);
2752 m = obj.Type.GetMethod ("invoke_type_load");
2753 obj.BeginInvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded, delegate {
2757 e = GetNextEvent ();
2758 Assert.AreEqual (EventType.TypeLoad, e.EventType);
2761 List<Value> invoke_results;
2764 public void InvokeMultiple () {
2765 Event e = run_until ("invoke1");
2767 StackFrame frame = e.Thread.GetFrames () [0];
2769 TypeMirror t = frame.Method.DeclaringType;
2770 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2772 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2774 var methods = new MethodMirror [2];
2775 methods [0] = t.GetMethod ("invoke_return_ref");
2776 methods [1] = t.GetMethod ("invoke_return_primitive");
2778 invoke_results = new List<Value> ();
2780 var r = this_obj.BeginInvokeMultiple (e.Thread, methods, null, InvokeOptions.SingleThreaded, invoke_multiple_cb, this_obj);
2781 WaitHandle.WaitAll (new WaitHandle[] { r.AsyncWaitHandle });
2782 this_obj.EndInvokeMultiple (r);
2783 // The callback might still be running
2784 while (invoke_results.Count < 2) {
2787 if (invoke_results [0] is PrimitiveValue) {
2788 AssertValue ("ABC", invoke_results [1]);
2789 AssertValue (42, invoke_results [0]);
2791 AssertValue ("ABC", invoke_results [0]);
2792 AssertValue (42, invoke_results [1]);
2796 void invoke_multiple_cb (IAsyncResult ar) {
2797 ObjectMirror this_obj = (ObjectMirror)ar.AsyncState;
2799 var res = this_obj.EndInvokeMethod (ar);
2800 lock (invoke_results)
2801 invoke_results.Add (res);
2805 public void InvokeAbort () {
2808 Start (new string [] { "dtest-app.exe", "invoke-abort" });
2810 Event e = run_until ("invoke_abort");
2812 StackFrame f = e.Thread.GetFrames ()[0];
2814 var obj = f.GetThis () as ObjectMirror;
2816 var m = t.GetMethod ("invoke_abort_2");
2817 // Invoke multiple times to check that the subsequent invokes are aborted too
2818 var res = (IInvokeAsyncResult)obj.BeginInvokeMultiple (e.Thread, new MethodMirror[] { m, m, m, m }, null, InvokeOptions.None, delegate { }, null);
2821 AssertThrows<CommandException> (delegate {
2822 obj.EndInvokeMethod (res);
2827 public void GetThreads () {
2832 public void Threads () {
2833 Event e = run_until ("threads");
2835 Assert.AreEqual (ThreadState.Running, e.Thread.ThreadState);
2837 Assert.IsTrue (e.Thread.ThreadId > 0);
2839 Assert.AreEqual (e.Thread.TID, e.Thread.TID);
2841 vm.EnableEvents (EventType.ThreadStart, EventType.ThreadDeath);
2845 e = GetNextEvent ();
2846 Assert.IsInstanceOfType (typeof (ThreadStartEvent), e);
2847 var state = e.Thread.ThreadState;
2848 Assert.IsTrue (state == ThreadState.Running || state == ThreadState.Unstarted);
2852 e = GetNextEvent ();
2853 Assert.IsInstanceOfType (typeof (ThreadDeathEvent), e);
2854 Assert.AreEqual (ThreadState.Stopped, e.Thread.ThreadState);
2858 public void Frame_SetValue () {
2859 Event e = run_until ("locals2");
2861 StackFrame frame = e.Thread.GetFrames () [0];
2864 var l = frame.Method.GetLocal ("i");
2865 frame.SetValue (l, vm.CreateValue ((long)55));
2866 AssertValue (55, frame.GetValue (l));
2869 l = frame.Method.GetLocal ("s");
2870 frame.SetValue (l, vm.RootDomain.CreateString ("DEF"));
2871 AssertValue ("DEF", frame.GetValue (l));
2873 // argument as local
2874 l = frame.Method.GetLocal ("arg");
2875 frame.SetValue (l, vm.CreateValue (6));
2876 AssertValue (6, frame.GetValue (l));
2879 var p = frame.Method.GetParameters ()[1];
2880 frame.SetValue (p, vm.CreateValue (7));
2881 AssertValue (7, frame.GetValue (p));
2884 p = frame.Method.GetParameters ()[2];
2885 frame.SetValue (p, vm.RootDomain.CreateString ("DEF"));
2886 AssertValue ("DEF", frame.GetValue (p));
2889 p = frame.Method.GetParameters ()[3];
2890 frame.SetValue (p, vm.RootDomain.CreateString ("DEF2"));
2891 AssertValue ("DEF2", frame.GetValue (p));
2894 p = frame.Method.GetParameters ()[4];
2895 var v = frame.GetValue (p) as StructMirror;
2896 v ["i"] = vm.CreateValue (43);
2897 frame.SetValue (p, v);
2898 v = frame.GetValue (p) as StructMirror;
2899 AssertValue (43, v ["i"]);
2901 // argument checking
2904 AssertThrows<ArgumentNullException> (delegate () {
2905 frame.SetValue ((LocalVariable)null, vm.CreateValue (55));
2909 AssertThrows<ArgumentNullException> (delegate () {
2910 l = frame.Method.GetLocal ("i");
2911 frame.SetValue (l, null);
2914 // value of invalid type
2915 AssertThrows<ArgumentException> (delegate () {
2916 l = frame.Method.GetLocal ("i");
2917 frame.SetValue (l, vm.CreateValue (55));
2923 public void Frame_SetValue_Registers () {
2924 Event e = run_until ("locals6_1");
2926 StackFrame frame = e.Thread.GetFrames () [1];
2929 var l = frame.Method.GetLocal ("j");
2930 frame.SetValue (l, vm.CreateValue (99));
2931 AssertValue (99, frame.GetValue (l));
2933 // Check it during execution
2934 e = run_until ("locals6_2");
2935 frame = e.Thread.GetFrames () [0];
2936 AssertValue (99, frame.GetValue (frame.Method.GetParameters ()[0]));
2938 // Set it while in a frame which clobbers its register
2939 e = run_until ("locals6_3");
2940 frame = e.Thread.GetFrames () [1];
2941 frame.SetValue (l, vm.CreateValue (100));
2942 AssertValue (100, frame.GetValue (l));
2944 // Check it during execution
2945 e = run_until ("locals6_4");
2946 frame = e.Thread.GetFrames () [0];
2947 AssertValue (100, frame.GetValue (frame.Method.GetParameters ()[0]));
2949 // Signed byte value
2950 e = run_until ("locals6_5");
2951 frame = e.Thread.GetFrames () [1];
2952 var l2 = frame.Method.GetLocal ("sb");
2953 frame.SetValue (l2, vm.CreateValue ((sbyte)-99));
2954 AssertValue (-99, frame.GetValue (l2));
2956 // Check it during execution
2957 e = run_until ("locals6_6");
2958 frame = e.Thread.GetFrames () [0];
2959 AssertValue (-99, frame.GetValue (frame.Method.GetParameters ()[0]));
2963 public void InvokeRegress () {
2964 Event e = run_until ("invoke1");
2966 StackFrame frame = e.Thread.GetFrames () [0];
2968 TypeMirror t = frame.Method.DeclaringType;
2969 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2971 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2977 m = t.GetMethod ("invoke_return_void");
2978 v = this_obj.InvokeMethod (e.Thread, m, null);
2981 // Check that the stack frames remain valid during the invoke
2982 Assert.AreEqual ("Tests", (frame.GetThis () as ObjectMirror).Type.Name);
2984 // do another invoke
2985 m = t.GetMethod ("invoke_return_void");
2986 v = this_obj.InvokeMethod (e.Thread, m, null);
2989 // Try a single step after the invoke
2990 var req = create_step (e);
2991 req.Depth = StepDepth.Into;
2992 req.Size = StepSize.Line;
2998 // Step into invoke2
3000 e = GetNextEvent ();
3001 Assert.IsTrue (e is StepEvent);
3002 Assert.AreEqual ("invoke2", (e as StepEvent).Method.Name);
3006 frame = e.Thread.GetFrames () [0];
3010 public void Exceptions () {
3011 Event e = run_until ("exceptions");
3012 var req = vm.CreateExceptionRequest (null);
3017 e = GetNextEvent ();
3018 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3019 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
3021 var frames = e.Thread.GetFrames ();
3022 Assert.AreEqual ("exceptions", frames [0].Method.Name);
3025 // exception type filter
3027 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.ArgumentException"));
3030 // Skip the throwing of the second OverflowException
3033 e = GetNextEvent ();
3034 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3035 Assert.AreEqual ("ArgumentException", (e as ExceptionEvent).Exception.Type.Name);
3038 // exception type filter for subclasses
3039 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.Exception"));
3044 e = GetNextEvent ();
3045 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3046 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
3050 req.IncludeSubclasses = false;
3055 e = GetNextEvent ();
3056 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3057 Assert.AreEqual ("Exception", (e as ExceptionEvent).Exception.Type.Name);
3060 // Implicit exceptions
3061 req = vm.CreateExceptionRequest (null);
3066 e = GetNextEvent ();
3067 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3068 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
3071 // Single stepping after an exception
3072 req = vm.CreateExceptionRequest (null);
3077 e = GetNextEvent ();
3078 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3079 Assert.AreEqual ("Exception", (e as ExceptionEvent).Exception.Type.Name);
3080 frames = e.Thread.GetFrames ();
3081 Assert.AreEqual ("exceptions2", frames [0].Method.Name);
3084 var sreq = create_step (e);
3085 sreq.Depth = StepDepth.Over;
3086 sreq.Size = StepSize.Line;
3090 e = GetNextEvent ();
3091 Assert.IsInstanceOfType (typeof (StepEvent), e);
3092 frames = e.Thread.GetFrames ();
3093 Assert.AreEqual ("exceptions", frames [0].Method.Name);
3096 // Argument checking
3097 AssertThrows<ArgumentException> (delegate {
3098 vm.CreateExceptionRequest (e.Thread.Type);
3103 public void ExceptionFilter () {
3104 Event e = run_until ("exception_filter");
3106 MethodMirror m = entry_point.DeclaringType.GetMethod ("exception_filter_filter");
3107 Assert.IsNotNull (m);
3109 vm.SetBreakpoint (m, 0);
3113 e = GetNextEvent ();
3114 Assert.AreEqual (EventType.Breakpoint, e.EventType);
3115 Assert.IsTrue (e is BreakpointEvent);
3116 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
3118 var frames = e.Thread.GetFrames ();
3120 Assert.IsTrue (frames [0].Location.SourceFile.IndexOf ("dtest-app.cs") != -1);
3121 Assert.AreEqual ("exception_filter_filter", frames [0].Location.Method.Name);
3123 Assert.AreEqual (0, frames [1].Location.Method.MetadataToken);
3124 Assert.AreEqual (0x0f, frames [1].Location.ILOffset);
3126 Assert.AreEqual ("exception_filter_method", frames [2].Location.Method.Name);
3127 Assert.AreEqual (0x06, frames [2].Location.ILOffset);
3129 Assert.AreEqual (0, frames [3].Location.Method.MetadataToken, 0);
3130 Assert.AreEqual (0, frames [3].Location.ILOffset);
3132 Assert.AreEqual ("exception_filter", frames [4].Location.Method.Name);
3136 public void ExceptionFilter2 () {
3139 Start (new string [] { "dtest-excfilter.exe" });
3141 MethodMirror filter_method = entry_point.DeclaringType.GetMethod ("Filter");
3142 Assert.IsNotNull (filter_method);
3144 MethodMirror test_method = entry_point.DeclaringType.GetMethod ("Test");
3145 Assert.IsNotNull (test_method);
3147 vm.SetBreakpoint (filter_method, 0);
3151 var e = GetNextEvent ();
3152 Assert.AreEqual (EventType.Breakpoint, e.EventType);
3153 Assert.IsTrue (e is BreakpointEvent);
3154 Assert.AreEqual (filter_method.Name, (e as BreakpointEvent).Method.Name);
3156 var frames = e.Thread.GetFrames ();
3158 Assert.AreEqual (4, frames.Count ());
3160 Assert.AreEqual (filter_method.Name, frames [0].Location.Method.Name);
3161 Assert.AreEqual (20, frames [0].Location.LineNumber);
3162 Assert.AreEqual (0, frames [0].Location.ILOffset);
3164 Assert.AreEqual (test_method.Name, frames [1].Location.Method.Name);
3165 Assert.AreEqual (37, frames [1].Location.LineNumber);
3166 Assert.AreEqual (0x0b, frames [1].Location.ILOffset);
3168 Assert.AreEqual (test_method.Name, frames [2].Location.Method.Name);
3169 Assert.AreEqual (33, frames [2].Location.LineNumber);
3170 Assert.AreEqual (0x05, frames [2].Location.ILOffset);
3172 Assert.AreEqual (entry_point.Name, frames [3].Location.Method.Name);
3173 Assert.AreEqual (14, frames [3].Location.LineNumber);
3174 Assert.AreEqual (0x00, frames [3].Location.ILOffset);
3182 public void EventSets () {
3184 // Create two filter which both match the same exception
3186 Event e = run_until ("exceptions");
3188 var req = vm.CreateExceptionRequest (null);
3191 var req2 = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.OverflowException"));
3196 var es = vm.GetNextEventSet ();
3197 Assert.AreEqual (2, es.Events.Length);
3200 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3201 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
3204 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3205 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
3212 // Test single threaded invokes during processing of nullref exceptions.
3213 // These won't work if the exception handling is done from the sigsegv signal
3214 // handler, since the sigsegv signal is disabled until control returns from the
3218 [Category ("only3")]
3219 public void NullRefExceptionAndSingleThreadedInvoke () {
3220 Event e = run_until ("exceptions");
3221 var req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.NullReferenceException"));
3226 e = GetNextEvent ();
3227 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3228 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
3230 var ex = (e as ExceptionEvent).Exception;
3231 var tostring_method = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
3232 ex.InvokeMethod (e.Thread, tostring_method, null, InvokeOptions.SingleThreaded);
3236 public void MemberInOtherDomain () {
3239 Start (new string [] { "dtest-app.exe", "domain-test" });
3241 vm.EnableEvents (EventType.AppDomainCreate, EventType.AppDomainUnload, EventType.AssemblyUnload);
3243 Event e = run_until ("domains_print_across");
3245 var frame = e.Thread.GetFrames ()[0];
3246 var inOtherDomain = frame.GetArgument (0) as ObjectMirror;
3247 var crossDomainField = (ObjectMirror) inOtherDomain.GetValue (inOtherDomain.Type.GetField("printMe"));
3248 Assert.AreEqual ("SentinelClass", crossDomainField.Type.Name);
3252 public void Domains () {
3255 Start (new string [] { "dtest-app.exe", "domain-test" });
3257 vm.EnableEvents (EventType.AppDomainCreate, EventType.AppDomainUnload, EventType.AssemblyUnload);
3259 Event e = run_until ("domains");
3263 e = GetNextEvent ();
3264 Assert.IsInstanceOfType (typeof (AppDomainCreateEvent), e);
3266 var domain = (e as AppDomainCreateEvent).Domain;
3268 // Check the object type
3269 e = run_until ("domains_2");
3270 var frame = e.Thread.GetFrames ()[0];
3271 var o = frame.GetArgument (0) as ObjectMirror;
3272 Assert.AreEqual ("CrossDomain", o.Type.Name);
3274 // Do a remoting invoke
3275 var cross_domain_type = o.Type;
3276 var v = o.InvokeMethod (e.Thread, cross_domain_type.GetMethod ("invoke_3"), null);
3277 AssertValue (42, v);
3279 // Run until the callback in the domain
3280 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_in_domain");
3281 Assert.IsNotNull (m);
3282 vm.SetBreakpoint (m, 0);
3286 e = GetNextEvent ();
3287 if (e is BreakpointEvent)
3291 Assert.AreEqual ("invoke_in_domain", (e as BreakpointEvent).Method.Name);
3293 // d_method is from another domain
3294 MethodMirror d_method = (e as BreakpointEvent).Method;
3295 Assert.IsTrue (m != d_method);
3297 var frames = e.Thread.GetFrames ();
3298 Assert.AreEqual ("invoke_in_domain", frames [0].Method.Name);
3299 Assert.AreEqual (domain, frames [0].Domain);
3300 Assert.AreEqual ("invoke", frames [1].Method.Name);
3301 Assert.AreEqual ("domains", frames [2].Method.Name);
3302 Assert.AreEqual (vm.RootDomain, frames [2].Domain);
3304 // Test breakpoints on already JITted methods in other domains
3305 m = entry_point.DeclaringType.GetMethod ("invoke_in_domain_2");
3306 Assert.IsNotNull (m);
3307 vm.SetBreakpoint (m, 0);
3311 e = GetNextEvent ();
3312 if (e is BreakpointEvent)
3316 Assert.AreEqual ("invoke_in_domain_2", (e as BreakpointEvent).Method.Name);
3318 // This is empty when receiving the AppDomainCreateEvent
3319 Assert.AreEqual ("domain", domain.FriendlyName);
3321 // Run until the unload
3324 e = GetNextEvent ();
3325 if (e is AssemblyUnloadEvent) {
3331 Assert.IsInstanceOfType (typeof (AppDomainUnloadEvent), e);
3332 Assert.AreEqual (domain, (e as AppDomainUnloadEvent).Domain);
3334 // Run past the unload
3335 e = run_until ("domains_3");
3337 // Test access to unloaded types
3338 // FIXME: Add an exception type for this
3339 AssertThrows<Exception> (delegate {
3340 d_method.DeclaringType.GetValue (d_method.DeclaringType.GetField ("static_i"));
3343 // Check that .Domain is accessible for stack frames with native transitions
3344 e = run_until ("called_from_invoke");
3345 ThreadMirror.NativeTransitions = true;
3346 foreach (var f in e.Thread.GetFrames ()) {
3352 public void DynamicMethods () {
3353 Event e = run_until ("dyn_call");
3355 var m = e.Thread.GetFrames ()[1].Method;
3356 Assert.AreEqual ("dyn_method", m.Name);
3358 // Test access to IL
3359 var body = m.GetMethodBody ();
3361 ILInstruction ins = body.Instructions [0];
3362 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
3363 Assert.AreEqual ("FOO", ins.Operand);
3367 public void RefEmit () {
3370 Start (new string [] { "dtest-app.exe", "ref-emit-test" });
3372 Event e = run_until ("ref_emit_call");
3374 var m = e.Thread.GetFrames ()[1].Method;
3375 Assert.AreEqual ("ref_emit_method", m.Name);
3377 // Test access to IL
3378 var body = m.GetMethodBody ();
3382 ins = body.Instructions [0];
3383 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
3384 Assert.AreEqual ("FOO", ins.Operand);
3386 ins = body.Instructions [1];
3387 Assert.AreEqual (OpCodes.Call, ins.OpCode);
3388 Assert.IsInstanceOfType (typeof (MethodMirror), ins.Operand);
3389 Assert.AreEqual ("ref_emit_call", (ins.Operand as MethodMirror).Name);
3393 public void IsAttached () {
3394 var f = entry_point.DeclaringType.GetField ("is_attached");
3396 Event e = run_until ("Main");
3398 AssertValue (true, entry_point.DeclaringType.GetValue (f));
3402 public void StackTraceInNative () {
3403 // Check that stack traces can be produced for threads in native code
3406 Start (new string [] { "dtest-app.exe", "frames-in-native" });
3408 var e = run_until ("frames_in_native");
3410 // FIXME: This is racy
3417 StackFrame[] frames = e.Thread.GetFrames ();
3419 int frame_index = -1;
3420 for (int i = 0; i < frames.Length; ++i) {
3421 if (frames [i].Method.Name == "Sleep") {
3427 Assert.IsTrue (frame_index != -1);
3428 Assert.AreEqual ("Sleep", frames [frame_index].Method.Name);
3429 Assert.AreEqual ("frames_in_native", frames [frame_index + 1].Method.Name);
3430 Assert.AreEqual ("Main", frames [frame_index + 2].Method.Name);
3432 // Check that invokes are disabled for such threads
3433 TypeMirror t = frames [frame_index + 1].Method.DeclaringType;
3435 var m = t.GetMethod ("invoke_static_return_void");
3436 AssertThrows<InvalidOperationException> (delegate {
3437 t.InvokeMethod (e.Thread, m, null);
3440 // Check that the frame info is invalidated
3441 run_until ("frames_in_native_2");
3443 AssertThrows<InvalidStackFrameException> (delegate {
3444 Console.WriteLine (frames [frame_index].GetThis ());
3449 public void VirtualMachine_CreateEnumMirror () {
3450 var e = run_until ("o1");
3451 var frame = e.Thread.GetFrames () [0];
3453 object val = frame.GetThis ();
3454 Assert.IsTrue (val is ObjectMirror);
3455 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
3456 ObjectMirror o = (val as ObjectMirror);
3458 FieldInfoMirror field = o.Type.GetField ("field_enum");
3459 Value f = o.GetValue (field);
3460 TypeMirror enumType = (f as EnumMirror).Type;
3462 o.SetValue (field, vm.CreateEnumMirror (enumType, vm.CreateValue (1)));
3463 f = o.GetValue (field);
3464 Assert.AreEqual (1, (f as EnumMirror).Value);
3466 // Argument checking
3467 AssertThrows<ArgumentNullException> (delegate () {
3468 vm.CreateEnumMirror (enumType, null);
3471 AssertThrows<ArgumentNullException> (delegate () {
3472 vm.CreateEnumMirror (null, vm.CreateValue (1));
3476 AssertThrows<ArgumentException> (delegate () {
3477 vm.CreateEnumMirror (enumType, vm.CreateValue (null));
3480 // value of a wrong type
3481 AssertThrows<ArgumentException> (delegate () {
3482 vm.CreateEnumMirror (enumType, vm.CreateValue ((long)1));
3487 public void VirtualMachine_EnableEvents_Breakpoint () {
3488 AssertThrows<ArgumentException> (delegate () {
3489 vm.EnableEvents (EventType.Breakpoint);
3494 public void SingleStepRegress654694 () {
3497 MethodMirror m = entry_point.DeclaringType.GetMethod ("ss_regress_654694");
3498 foreach (Location l in m.Locations) {
3499 if (l.ILOffset > 0 && il_offset == -1)
3500 il_offset = l.ILOffset;
3503 Event e = run_until ("ss_regress_654694");
3505 Assert.IsNotNull (m);
3506 vm.SetBreakpoint (m, il_offset);
3510 e = GetNextEvent ();
3511 Assert.IsTrue (e is BreakpointEvent);
3513 var req = create_step (e);
3514 req.Depth = StepDepth.Over;
3515 req.Size = StepSize.Line;
3520 e = GetNextEvent ();
3521 Assert.IsTrue (e is StepEvent);
3527 public void DebugBreak () {
3528 vm.EnableEvents (EventType.UserBreak);
3533 var e = GetNextEvent ();
3534 Assert.IsTrue (e is UserBreakEvent);
3538 public void DebugLog () {
3539 vm.EnableEvents (EventType.UserLog);
3544 var e = GetNextEvent ();
3545 Assert.IsTrue (e is UserLogEvent);
3546 var le = e as UserLogEvent;
3548 Assert.AreEqual (5, le.Level);
3549 Assert.AreEqual ("A", le.Category);
3550 Assert.AreEqual ("B", le.Message);
3554 public void TypeGetMethodsByNameFlags () {
3556 var assembly = entry_point.DeclaringType.Assembly;
3557 var type = assembly.GetType ("Tests3");
3559 Assert.IsNotNull (type);
3561 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Public, false);
3562 Assert.AreEqual (1, mm.Length, "#1");
3563 Assert.AreEqual ("M1", mm[0].Name, "#2");
3565 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.NonPublic, false);
3566 Assert.AreEqual (1, mm.Length, "#3");
3567 Assert.AreEqual ("M2", mm[0].Name, "#4");
3569 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public, false);
3570 Assert.AreEqual (7, mm.Length, "#5"); //M3 plus Equals, GetHashCode, GetType, ToString, .ctor
3572 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, false);
3573 Assert.AreEqual (2, mm.Length, "#7");
3575 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
3576 Assert.AreEqual (1, mm.Length, "#9");
3578 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
3579 Assert.AreEqual (5, mm.Length, "#11");
3582 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Public, false);
3583 Assert.AreEqual (1, mm.Length, "#12");
3584 Assert.AreEqual ("M1", mm[0].Name, "#13");
3586 mm = type.GetMethodsByNameFlags ("m1", BindingFlags.Static | BindingFlags.Public, true);
3587 Assert.AreEqual (1, mm.Length, "#14");
3588 Assert.AreEqual ("M1", mm[0].Name, "#15");
3590 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, false);
3591 Assert.AreEqual (1, mm.Length, "#16");
3592 Assert.AreEqual ("M1", mm[0].Name, "#17");
3596 [Category ("only88")]
3597 public void TypeLoadSourceFileFilter () {
3598 Event e = run_until ("type_load");
3600 if (!vm.Version.AtLeast (2, 7))
3603 string srcfile = (e as BreakpointEvent).Method.DeclaringType.GetSourceFiles (true)[0];
3604 srcfile = srcfile.Replace ("dtest-app.cs", "TypeLoadClass.cs");
3605 Assert.IsTrue (srcfile.Contains ("TypeLoadClass.cs"));
3607 var req = vm.CreateTypeLoadRequest ();
3608 req.SourceFileFilter = new string [] { srcfile.ToUpper () };
3612 e = GetNextEvent ();
3613 Assert.IsTrue (e is TypeLoadEvent);
3614 Assert.AreEqual ("TypeLoadClass", (e as TypeLoadEvent).Type.FullName);
3618 public void TypeLoadTypeNameFilter () {
3619 Event e = run_until ("type_load");
3621 var req = vm.CreateTypeLoadRequest ();
3622 req.TypeNameFilter = new string [] { "TypeLoadClass2" };
3626 e = GetNextEvent ();
3627 Assert.IsTrue (e is TypeLoadEvent);
3628 Assert.AreEqual ("TypeLoadClass2", (e as TypeLoadEvent).Type.FullName);
3632 public void GetTypesForSourceFile () {
3635 var types = vm.GetTypesForSourceFile ("dtest-app.cs", false);
3636 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
3637 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
3639 types = vm.GetTypesForSourceFile ("DTEST-app.cs", true);
3640 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
3641 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
3645 public void GetTypesNamed () {
3648 var types = vm.GetTypes ("Tests", false);
3649 Assert.AreEqual (1, types.Count);
3650 Assert.AreEqual ("Tests", types [0].FullName);
3652 types = vm.GetTypes ("System.Exception", false);
3653 Assert.AreEqual (1, types.Count);
3654 Assert.AreEqual ("System.Exception", types [0].FullName);
3658 public void String_GetValue () {
3663 var e = run_until ("arg2");
3665 var frame = e.Thread.GetFrames () [0];
3667 val = frame.GetArgument (6);
3668 Assert.AreEqual ('\0'.ToString () + "A", (val as StringMirror).Value);
3672 public void String_GetChars () {
3676 var e = run_until ("arg2");
3678 var frame = e.Thread.GetFrames () [0];
3680 val = frame.GetArgument (0);
3681 Assert.IsTrue (val is StringMirror);
3682 AssertValue ("FOO", val);
3683 var s = (val as StringMirror);
3684 Assert.AreEqual (3, s.Length);
3686 var c = s.GetChars (0, 2);
3687 Assert.AreEqual (2, c.Length);
3688 Assert.AreEqual ('F', c [0]);
3689 Assert.AreEqual ('O', c [1]);
3691 AssertThrows<ArgumentException> (delegate () {
3697 public void GetInterfaces () {
3698 var e = run_until ("arg2");
3700 var frame = e.Thread.GetFrames () [0];
3702 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
3703 var ifaces = cl1.GetInterfaces ();
3704 Assert.AreEqual (1, ifaces.Length);
3705 Assert.AreEqual ("ITest", ifaces [0].Name);
3707 var cl2 = cl1.GetMethod ("Baz").ReturnType;
3708 var ifaces2 = cl2.GetInterfaces ();
3709 Assert.AreEqual (1, ifaces2.Length);
3710 Assert.AreEqual ("ITest`1", ifaces2 [0].Name);
3714 public void GetInterfaceMap () {
3715 var e = run_until ("arg2");
3717 var frame = e.Thread.GetFrames () [0];
3719 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
3720 var iface = cl1.Assembly.GetType ("ITest");
3721 var map = cl1.GetInterfaceMap (iface);
3722 Assert.AreEqual (cl1, map.TargetType);
3723 Assert.AreEqual (iface, map.InterfaceType);
3724 Assert.AreEqual (2, map.InterfaceMethods.Length);
3725 Assert.AreEqual (2, map.TargetMethods.Length);
3729 public void StackAlloc_Breakpoints_Regress2775 () {
3730 // Check that breakpoints on arm don't overwrite stackalloc-ed memory
3731 var e = run_until ("regress_2755");
3733 var frame = e.Thread.GetFrames () [0];
3735 // This breaks at the call site
3736 vm.SetBreakpoint (m, m.Locations [2].ILOffset);
3739 var e2 = GetNextEvent ();
3740 Assert.IsTrue (e2 is BreakpointEvent);
3742 e = run_until ("regress_2755_3");
3743 frame = e.Thread.GetFrames () [1];
3744 var res = frame.GetValue (m.GetLocal ("sum"));
3745 AssertValue (0, res);
3749 public void MethodInfo () {
3750 Event e = run_until ("locals2");
3752 StackFrame frame = e.Thread.GetFrames () [0];
3753 var m = frame.Method;
3755 Assert.IsTrue (m.IsGenericMethod);
3756 Assert.IsFalse (m.IsGenericMethodDefinition);
3758 var args = m.GetGenericArguments ();
3759 Assert.AreEqual (1, args.Length);
3760 Assert.AreEqual ("String", args [0].Name);
3762 var gmd = m.GetGenericMethodDefinition ();
3763 Assert.IsTrue (gmd.IsGenericMethod);
3764 Assert.IsTrue (gmd.IsGenericMethodDefinition);
3765 Assert.AreEqual (gmd, gmd.GetGenericMethodDefinition ());
3767 args = gmd.GetGenericArguments ();
3768 Assert.AreEqual (1, args.Length);
3769 Assert.AreEqual ("T", args [0].Name);
3771 var attrs = m.GetCustomAttributes (true);
3772 Assert.AreEqual (1, attrs.Length);
3773 Assert.AreEqual ("StateMachineAttribute", attrs [0].Constructor.DeclaringType.Name);
3777 public void UnhandledException () {
3780 Start (new string [] { "dtest-app.exe", "unhandled-exception" });
3782 var req = vm.CreateExceptionRequest (null, false, true);
3785 var e = run_until ("unhandled_exception");
3788 var e2 = GetNextEvent ();
3789 Assert.IsTrue (e2 is ExceptionEvent);
3796 public void UnhandledException_2 () {
3799 Start (new string [] { "dtest-app.exe", "unhandled-exception-endinvoke" });
3801 var req = vm.CreateExceptionRequest (null, false, true);
3804 MethodMirror m = entry_point.DeclaringType.GetMethod ("unhandled_exception_endinvoke_2");
3805 Assert.IsNotNull (m);
3806 vm.SetBreakpoint (m, m.ILOffsets [0]);
3808 var e = run_until ("unhandled_exception_endinvoke");
3811 var e2 = GetNextEvent ();
3812 Assert.IsFalse (e2 is ExceptionEvent);
3819 public void UnhandledExceptionUserCode () {
3822 // Exceptions caught in non-user code are treated as unhandled
3823 Start (new string [] { "dtest-app.exe", "unhandled-exception-user" });
3825 var req = vm.CreateExceptionRequest (null, false, true);
3826 req.AssemblyFilter = new List<AssemblyMirror> () { entry_point.DeclaringType.Assembly };
3829 var e = run_until ("unhandled_exception_user");
3832 var e2 = GetNextEvent ();
3833 Assert.IsTrue (e2 is ExceptionEvent);
3840 public void GCWhileSuspended () {
3841 // Check that objects are kept alive during suspensions
3842 Event e = run_until ("gc_suspend_1");
3844 MethodMirror m = entry_point.DeclaringType.GetMethod ("gc_suspend_invoke");
3846 var o = entry_point.DeclaringType.GetValue (entry_point.DeclaringType.GetField ("gc_suspend_field")) as ObjectMirror;
3847 //Console.WriteLine (o);
3849 StackFrame frame = e.Thread.GetFrames () [0];
3850 TypeMirror t = frame.Method.DeclaringType;
3851 for (int i = 0; i < 10; ++i)
3852 t.InvokeMethod (e.Thread, m, new Value [] { });
3854 // This throws an exception if the object is collected
3855 long addr = o.Address;
3857 var o2 = entry_point.DeclaringType.GetValue (entry_point.DeclaringType.GetField ("gc_suspend_field")) as ObjectMirror;
3862 public void MakeGenericMethod () {
3863 Event e = run_until ("bp1");
3865 var intm = vm.RootDomain.GetCorrespondingType (typeof (int));
3866 var stringm = vm.RootDomain.GetCorrespondingType (typeof (string));
3867 var gm = entry_point.DeclaringType.GetMethod ("generic_method");
3868 var res = gm.MakeGenericMethod (new TypeMirror [] { stringm });
3869 var args = res.GetGenericArguments ();
3870 Assert.AreEqual (1, args.Length);
3871 Assert.AreEqual (stringm, args [0]);
3874 AssertThrows<ArgumentNullException> (delegate {
3875 gm.MakeGenericMethod (null);
3877 AssertThrows<ArgumentNullException> (delegate {
3878 gm.MakeGenericMethod (new TypeMirror [] { null });
3880 AssertThrows<ArgumentException> (delegate {
3881 gm.MakeGenericMethod (new TypeMirror [] { stringm, stringm });
3883 AssertThrows<InvalidOperationException> (delegate {
3884 gm.MakeGenericMethod (new TypeMirror [] { intm });
3886 AssertThrows<InvalidOperationException> (delegate {
3887 entry_point.DeclaringType.GetMethod ("Main").MakeGenericMethod (new TypeMirror [] { intm });
3892 public void InspectThreadSuspenedOnWaitOne () {
3894 Start (true, "dtest-app.exe", "wait-one" );
3896 ThreadMirror.NativeTransitions = true;
3898 var evt = run_until ("wait_one");
3899 Assert.IsNotNull (evt, "#1");
3901 var thread = evt.Thread;
3902 Assert.AreEqual (ThreadState.Running, thread.ThreadState, "#1.1");
3904 var frames = thread.GetFrames ();
3905 Assert.IsNotNull (frames, "#2");
3906 Assert.AreEqual (2, frames.Length, "#3");
3907 Assert.AreEqual ("wait_one", frames [0].Method.Name, "#4");
3908 Assert.AreEqual ("Main", frames [1].Method.Name, "#5");
3912 Thread.Sleep (500); //FIXME this is racy, maybe single step? or something?
3915 Assert.AreEqual (ThreadState.WaitSleepJoin, thread.ThreadState, "#6");
3917 frames = thread.GetFrames ();
3918 Assert.AreEqual (8, frames.Length, "#7");
3919 Assert.AreEqual ("WaitOne_internal", frames [0].Method.Name, "#8.0");
3920 Assert.AreEqual ("WaitOneNative", frames [1].Method.Name, "#8.1");
3921 Assert.AreEqual ("InternalWaitOne", frames [2].Method.Name, "#8.2");
3922 Assert.AreEqual ("WaitOne", frames [3].Method.Name, "#8.3");
3923 Assert.AreEqual ("WaitOne", frames [4].Method.Name, "#8.4");
3924 Assert.AreEqual ("WaitOne", frames [5].Method.Name, "#8.5");
3925 Assert.AreEqual ("wait_one", frames [6].Method.Name, "#8.6");
3926 Assert.AreEqual ("Main", frames [7].Method.Name, "#8.7");
3928 var frame = frames [0];
3929 Assert.IsTrue (frame.IsNativeTransition, "#11.1");
3932 Assert.Fail ("Known limitation - can't get info from m2n frames");
3933 } catch (AbsentInformationException) {}
3936 Assert.IsFalse (frame.IsNativeTransition, "#12.1");
3937 var wait_one_this = frame.GetThis ();
3938 Assert.IsNotNull (wait_one_this, "#12.2");
3941 var locals = frame.GetVisibleVariables ();
3942 Assert.AreEqual (1, locals.Count, "#13.1");
3944 var local_0 = frame.GetValue (locals [0]);
3945 Assert.IsNotNull (local_0, "#13.2");
3947 Assert.AreEqual (wait_one_this, local_0, "#14.2");
3951 public void GetMethodBody () {
3952 var bevent = run_until ("Main");
3954 var m = bevent.Method.DeclaringType.GetMethod ("get_IntProperty");
3955 var body = m.GetMethodBody ();
3956 foreach (var ins in body.Instructions) {
3957 if (ins.OpCode == OpCodes.Ldfld) {
3958 var field = (FieldInfoMirror)ins.Operand;
3959 Assert.AreEqual ("field_i", field.Name);
3965 public void EvaluateMethod () {
3966 var bevent = run_until ("evaluate_method_2");
3968 var m = bevent.Method.DeclaringType.GetMethod ("get_IntProperty");
3970 var this_obj = bevent.Thread.GetFrames ()[0].GetThis ();
3971 var v = m.Evaluate (this_obj, null);
3972 AssertValue (42, v);
3976 public void SetIP () {
3977 var bevent = run_until ("set_ip_1");
3979 var invalid_loc = bevent.Thread.GetFrames ()[0].Location;
3981 var req = create_step (bevent);
3982 var e = step_out ();
3984 var frames = e.Thread.GetFrames ();
3985 var locs = frames [0].Method.Locations;
3986 var next_loc = locs.First (l => (l.LineNumber == frames [0].Location.LineNumber + 2));
3988 e.Thread.SetIP (next_loc);
3990 /* Check that i ++; j = 5; was skipped */
3991 bevent = run_until ("set_ip_2");
3992 var f = bevent.Thread.GetFrames ()[1];
3993 AssertValue (2, f.GetValue (f.Method.GetLocal ("i")));
3994 AssertValue (0, f.GetValue (f.Method.GetLocal ("j")));
3997 AssertThrows<ArgumentNullException> (delegate {
3998 e.Thread.SetIP (null);
4001 AssertThrows<ArgumentException> (delegate {
4002 e.Thread.SetIP (invalid_loc);
4007 public void SetIPSingleStep () {
4008 // Check that single stepping after set-ip steps from the new ip
4009 var bevent = run_until ("set_ip_1");
4011 var invalid_loc = bevent.Thread.GetFrames ()[0].Location;
4013 var req = create_step (bevent);
4014 req.Size = StepSize.Line;
4015 var e = step_out ();
4017 var frames = e.Thread.GetFrames ();
4018 var locs = frames [0].Method.Locations;
4019 var prev_loc = locs.First (l => (l.LineNumber == frames [0].Location.LineNumber - 3));
4020 AssertValue (2, frames [0].GetValue (frames [0].Method.GetLocal ("i")));
4022 // Set back the ip to the first i ++; line
4023 e.Thread.SetIP (prev_loc);
4026 var f = e.Thread.GetFrames ()[0];
4027 AssertValue (3, f.GetValue (f.Method.GetLocal ("i")));
4031 public void NewInstanceNoCtor () {
4032 var bevent = run_until ("Main");
4034 var stype = bevent.Method.DeclaringType.Assembly.GetType ("AStruct");
4035 var obj = stype.NewInstance ();
4036 Assert.IsTrue (obj is ObjectMirror);
4037 Assert.AreEqual ("AStruct", (obj as ObjectMirror).Type.Name);
4041 public void StaticCtorFilterInCctor () {
4042 // Check that single stepping when in a cctor only ignores
4043 // other cctors, not the current one
4044 var bevent = run_until ("step_filters");
4046 var assembly = entry_point.DeclaringType.Assembly;
4047 var type = assembly.GetType ("Tests/ClassWithCctor");
4048 var cctor = type.GetMethod (".cctor");
4049 vm.SetBreakpoint (cctor, 0);
4052 var e = vm.GetNextEvent ();
4053 Assert.IsTrue (e is BreakpointEvent);
4055 var req = create_step (e);
4056 req.Filter = StepFilter.StaticCtor;
4058 // Make sure we are still in the cctor
4059 Assert.AreEqual (".cctor", e.Thread.GetFrames ()[0].Location.Method.Name);
4063 public void ThreadpoolIOsinglestep () {
4065 Start ("dtest-app.exe", "threadpool-io");
4066 // This is a regression test for #42625. It tests the
4067 // interaction (particularly in coop GC) of the
4068 // threadpool I/O mechanism and the soft debugger.
4069 Event e = run_until ("threadpool_io");
4070 // run until we sent the task half the bytes it
4071 // expects, so that it blocks waiting for the rest.
4072 e = run_until ("threadpool_bp");
4073 var req = create_step (e);
4074 e = step_out (); // leave threadpool_bp
4075 e = step_out (); // leave threadpool_io