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 Diag.ProcessStartInfo CreateStartInfo (string[] args) {
67 var pi = new Diag.ProcessStartInfo ();
69 if (runtime != null) {
70 pi.FileName = runtime;
71 } else if (Path.DirectorySeparatorChar == '\\') {
72 string processExe = Diag.Process.GetCurrentProcess ().MainModule.FileName;
73 if (processExe != null) {
74 string fileName = Path.GetFileName (processExe);
75 if (fileName.StartsWith ("mono") && fileName.EndsWith (".exe"))
76 pi.FileName = processExe;
79 if (string.IsNullOrEmpty (pi.FileName))
81 pi.Arguments = String.Join (" ", args);
85 void Start (bool forceExit, params string[] args) {
86 this.forceExit = forceExit;
89 var pi = CreateStartInfo (args);
90 vm = VirtualMachineManager.Launch (pi, new LaunchOptions { AgentArgs = agent_args });
92 var ep = new IPEndPoint (IPAddress.Any, 10000);
93 Console.WriteLine ("Listening on " + ep + "...");
94 vm = VirtualMachineManager.Listen (ep);
97 var load_req = vm.CreateAssemblyLoadRequest ();
100 Event vmstart = GetNextEvent ();
101 Assert.AreEqual (EventType.VMStart, vmstart.EventType);
110 /* Find out the entry point */
114 if (e is AssemblyLoadEvent) {
115 AssemblyLoadEvent ae = (AssemblyLoadEvent)e;
116 entry_point = ae.Assembly.EntryPoint;
117 if (entry_point != null)
127 BreakpointEvent run_until (string name) {
129 MethodMirror m = entry_point.DeclaringType.GetMethod (name);
130 Assert.IsNotNull (m);
131 //Console.WriteLine ("X: " + name + " " + m.ILOffsets.Count + " " + m.Locations.Count);
132 var req = vm.SetBreakpoint (m, m.ILOffsets [0]);
139 if (e is BreakpointEvent)
145 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
146 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
148 return (e as BreakpointEvent);
151 class ReusableBreakpoint {
153 public string method_name;
154 public BreakpointEventRequest req;
155 public BreakpointEvent lastEvent = null;
156 public ReusableBreakpoint (DebuggerTests owner, string method_name)
159 this.method_name = method_name;
160 MethodMirror m = owner.entry_point.DeclaringType.GetMethod (method_name);
161 Assert.IsNotNull (m);
162 req = owner.vm.SetBreakpoint (m, m.ILOffsets [0]);
165 public void Continue ()
167 bool survived = false;
174 e = owner.GetNextEvent ();
175 if (e is BreakpointEvent)
179 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
180 Assert.AreEqual (method_name, (e as BreakpointEvent).Method.Name);
182 lastEvent = e as BreakpointEvent;
186 if (!survived) { // Ensure cleanup if we triggered an assert
192 public void Disable ()
198 /* One of the tests executes a complex tree of recursive functions.
199 The only good way to specify how its behavior should appear from this side
200 is to just run the function tree once over here and record what it does. */
201 public struct RecursiveChaoticPoint
203 public bool breakpoint;
207 public RecursiveChaoticPoint (bool breakpoint, string name, int depth)
209 this.breakpoint = breakpoint;
215 // The breakpoint is placed here in dtest-app.cs
216 public static void ss_recursive_chaotic_trap (int n, List<RecursiveChaoticPoint> trace, ref bool didLast, ref bool didAny)
218 // Depth is calculated as:
219 // Main + single_stepping + ss_recursive_chaotic + (n is 5 at outermost frame and 0 at innermost frame) + ss_recursive_chaotic_trap
220 trace.Add (new RecursiveChaoticPoint (true, "ss_recursive_chaotic_trap", 5 - n + 5));
224 public static void ss_recursive_chaotic_at (string at, int n, List<RecursiveChaoticPoint> trace, ref bool didLast, ref bool didAny)
226 // 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.
228 // Depth is calculated as:
229 // Main + single_stepping + ss_recursive_chaotic + (n is 5 at outermost frame and 0 at innermost frame)
230 trace.Add (new RecursiveChaoticPoint (false, "ss_recursive_chaotic_" + at, 5 - n + 4));
236 public static bool ss_recursive_chaotic_fizz (int n, List<RecursiveChaoticPoint> trace)
238 bool didLast = false, didAny = false;
241 didLast = ss_recursive_chaotic_buzz (next, trace);
242 ss_recursive_chaotic_at ("fizz", n, trace, ref didLast, ref didAny);
243 didLast = ss_recursive_chaotic_fizzbuzz (next, trace);
244 ss_recursive_chaotic_at ("fizz", n, trace, ref didLast, ref didAny);
246 ss_recursive_chaotic_trap (n, trace, ref didLast, ref didAny);
247 ss_recursive_chaotic_at ("fizz", n, trace, ref didLast, ref didAny);
252 public static bool ss_recursive_chaotic_buzz (int n, List<RecursiveChaoticPoint> trace)
254 bool didLast = false, didAny = false;
257 didLast = ss_recursive_chaotic_fizz (next, trace);
258 ss_recursive_chaotic_at ("buzz", n, trace, ref didLast, ref didAny);
259 didLast = ss_recursive_chaotic_fizzbuzz (next, trace);
260 ss_recursive_chaotic_at ("buzz", n, trace, ref didLast, ref didAny);
265 public static bool ss_recursive_chaotic_fizzbuzz (int n, List<RecursiveChaoticPoint> trace)
267 bool didLast = false, didAny = false;
270 didLast = ss_recursive_chaotic_fizz (next, trace);
271 ss_recursive_chaotic_at ("fizzbuzz", n, trace, ref didLast, ref didAny);
272 didLast = ss_recursive_chaotic_buzz (next, trace);
273 ss_recursive_chaotic_at ("fizzbuzz", n, trace, ref didLast, ref didAny);
274 didLast = ss_recursive_chaotic_fizzbuzz (next, trace);
275 ss_recursive_chaotic_at ("fizzbuzz", n, trace, ref didLast, ref didAny);
280 public static void trace_ss_recursive_chaotic (List<RecursiveChaoticPoint> trace)
282 ss_recursive_chaotic_fizz (5, trace);
285 Event single_step (ThreadMirror t) {
286 var req = vm.CreateStepRequest (t);
290 Event e = GetNextEvent ();
291 Assert.IsTrue (e is StepEvent);
298 Event step_until (ThreadMirror t, string method_name) {
302 if ((e as StepEvent).Method.Name == method_name)
308 void check_arg_val (StackFrame frame, int pos, Type type, object eval) {
309 object val = frame.GetArgument (pos);
310 Assert.IsTrue (val is PrimitiveValue);
311 object v = (val as PrimitiveValue).Value;
312 Assert.AreEqual (type, v.GetType ());
314 Assert.IsTrue (Math.Abs ((float)eval - (float)v) < 0.0001);
315 else if (eval is double)
316 Assert.IsTrue (Math.Abs ((double)eval - (double)v) < 0.0001);
318 Assert.AreEqual (eval, v);
321 void AssertValue (object expected, object val) {
322 if (expected is string) {
323 Assert.IsTrue (val is StringMirror);
324 Assert.AreEqual (expected, (val as StringMirror).Value);
325 } else if (val is StructMirror && (val as StructMirror).Type.Name == "IntPtr") {
326 AssertValue (expected, (val as StructMirror).Fields [0]);
328 Assert.IsTrue (val is PrimitiveValue);
329 Assert.AreEqual (expected, (val as PrimitiveValue).Value);
334 public void SetUp () {
335 ThreadMirror.NativeTransitions = false;
336 Start (new string [] { "dtest-app.exe" });
340 public void TearDown () {
344 if (step_req != null)
352 Event e = GetNextEvent ();
354 if (e is VMDeathEvent)
363 public void SimpleBreakpoint () {
366 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp1");
367 Assert.IsNotNull (m);
369 vm.SetBreakpoint (m, 0);
374 Assert.AreEqual (EventType.Breakpoint, e.EventType);
375 Assert.IsTrue (e is BreakpointEvent);
376 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
379 AssertThrows<ArgumentException> (delegate {
381 vm.SetBreakpoint (m, 2);
386 public void BreakpointsSameLocation () {
387 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp2");
388 Assert.IsNotNull (m);
390 vm.SetBreakpoint (m, 0);
391 vm.SetBreakpoint (m, 0);
395 var es = vm.GetNextEventSet ();
396 Assert.AreEqual (2, es.Events.Length);
397 Assert.IsTrue (es [0] is BreakpointEvent);
398 Assert.AreEqual (m, (es [0] as BreakpointEvent).Method);
400 Assert.IsTrue (es [1] is BreakpointEvent);
401 Assert.AreEqual (m.Name, (es [1] as BreakpointEvent).Method.Name);
405 public void BreakpointAlreadyJITted () {
406 Event e = run_until ("bp1");
408 /* Place a breakpoint on bp3 */
409 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp3");
410 Assert.IsNotNull (m);
411 vm.SetBreakpoint (m, 0);
413 /* Same with generic instances */
414 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp7");
415 Assert.IsNotNull (m2);
416 vm.SetBreakpoint (m2, 0);
421 Assert.AreEqual (EventType.Breakpoint, e.EventType);
422 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
426 /* Non-shared instance */
428 Assert.AreEqual (EventType.Breakpoint, e.EventType);
429 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
433 /* Shared instance */
435 Assert.AreEqual (EventType.Breakpoint, e.EventType);
436 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
440 public void ClearBreakpoint () {
443 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
444 Assert.IsNotNull (m);
445 EventRequest req1 = vm.SetBreakpoint (m, 0);
446 EventRequest req2 = vm.SetBreakpoint (m, 0);
448 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
449 Assert.IsNotNull (m2);
450 vm.SetBreakpoint (m2, 0);
455 var es = vm.GetNextEventSet ();
456 Assert.AreEqual (2, es.Events.Length);
457 Assert.AreEqual (EventType.Breakpoint, es [0].EventType);
458 Assert.AreEqual (m.Name, (es [0] as BreakpointEvent).Method.Name);
459 Assert.AreEqual (EventType.Breakpoint, es [1].EventType);
460 Assert.AreEqual (m.Name, (es [1] as BreakpointEvent).Method.Name);
462 /* Clear one of them */
468 Assert.AreEqual (EventType.Breakpoint, e.EventType);
469 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
471 /* Clear the other */
477 Assert.AreEqual (EventType.Breakpoint, e.EventType);
478 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
482 public void ClearAllBreakpoints () {
485 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
486 Assert.IsNotNull (m);
487 vm.SetBreakpoint (m, 0);
489 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
490 Assert.IsNotNull (m2);
491 vm.SetBreakpoint (m2, 0);
493 vm.ClearAllBreakpoints ();
498 Assert.IsTrue (!(e is BreakpointEvent));
499 if (e is VMDeathEvent)
504 public void BreakpointOnGShared () {
507 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp6");
508 Assert.IsNotNull (m);
510 vm.SetBreakpoint (m, 0);
515 Assert.AreEqual (EventType.Breakpoint, e.EventType);
516 Assert.IsTrue (e is BreakpointEvent);
517 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
519 // Breakpoint on an open generic method of a closed generic class (#3422)
520 var frame = e.Thread.GetFrames ()[0];
521 var ginst = frame.GetValue (frame.Method.GetLocal ("gc"));
522 var m2 = (ginst as ObjectMirror).Type.GetMethod ("bp");
523 vm.SetBreakpoint (m2, 0);
528 Assert.AreEqual (EventType.Breakpoint, e.EventType);
529 Assert.IsTrue (e is BreakpointEvent);
530 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
533 // Assert we have stepped to a location
534 void assert_location (Event e, string method) {
535 Assert.IsTrue (e is StepEvent);
536 Assert.AreEqual (method, (e as StepEvent).Method.Name);
539 // Assert we have breakpointed at a location
540 void assert_location_at_breakpoint (Event e, string method) {
541 Assert.IsTrue (e is BreakpointEvent);
542 Assert.AreEqual (method, (e as BreakpointEvent).Method.Name);
545 // Assert we have stepped to or breakpointed at a location
546 void assert_location_allow_breakpoint (Event e, string method) {
548 Assert.AreEqual (method, (e as StepEvent).Method.Name);
549 else if (e is BreakpointEvent)
550 Assert.AreEqual (method, (e as BreakpointEvent).Method.Name);
552 Assert.Fail ("Neither step nor breakpoint event");
555 StepEventRequest create_step (Event e) {
556 var req = vm.CreateStepRequest (e.Thread);
562 public void ClassLocalReflection () {
563 MethodMirror m = entry_point.DeclaringType.Assembly.GetType ("LocalReflectClass").GetMethod ("RunMe");
565 Assert.IsNotNull (m);
567 // foreach (var x in m.Locations) {
568 // Console.WriteLine (x);
572 int method_base_linum = m.Locations [0].LineNumber;
573 foreach (var location in m.Locations)
574 if (location.LineNumber == method_base_linum + 2) {
575 offset = location.ILOffset;
579 var req = vm.SetBreakpoint (m, offset);
586 if (e is BreakpointEvent)
592 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
593 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
595 e = single_step (e.Thread);
597 var frame = e.Thread.GetFrames ()[0];
599 Assert.IsNotNull (frame);
600 var field = frame.Method.GetLocal ("reflectMe");
601 Assert.IsNotNull (field);
602 Value variable = frame.GetValue (field);
604 ObjectMirror thisObj = (ObjectMirror)variable;
605 TypeMirror thisType = thisObj.Type;
606 FieldInfoMirror thisFi = null;
607 foreach (var fi in thisType.GetFields ())
608 if (fi.Name == "someField")
611 var gotVal = thisObj.GetValue (thisFi);
612 // If we got this far, we're good.
616 public void SingleStepping () {
617 Event e = run_until ("single_stepping");
619 var req = create_step (e);
622 // Step over 'bool b = true'
624 assert_location (e, "single_stepping");
631 assert_location (e, "ss1");
638 assert_location (e, "single_stepping");
642 assert_location (e, "single_stepping");
646 assert_location (e, "ss3");
648 // Step back into single_stepping
650 assert_location (e, "single_stepping");
652 // Step into next line
654 assert_location (e, "single_stepping");
656 // Step into ss3_2 ()
658 assert_location (e, "ss3_2");
660 // Step over ss3_2_2 ()
662 assert_location (e, "ss3_2");
664 // Recreate the request
671 // Step back into single_stepping () with the new request
673 assert_location (e, "single_stepping");
677 assert_location (e, "ss4");
682 // Change to StepSize.Line
684 req.Depth = StepDepth.Over;
685 req.Size = StepSize.Line;
688 // Step over ss1 (); ss1 ();
693 req.Depth = StepDepth.Into;
697 assert_location (e, "ss2");
702 e = run_until ("ss5");
704 // Add an assembly filter
705 req.AssemblyFilter = new AssemblyMirror [] { (e as BreakpointEvent).Method.DeclaringType.Assembly };
711 // Step into is_even, skipping the linq stuff
713 assert_location (e, "is_even");
715 // FIXME: Check that single stepping works with lock (obj)
719 e = run_until ("ss6");
721 req = create_step (e);
722 req.Depth = StepDepth.Over;
725 // Check that single stepping works in out-of-line bblocks
728 assert_location (e, "ss6");
731 // Testing stepping in, over and out with exception handlers in same or caller method
733 //stepout in ss7_2, which may not go to catch(instead out to ss7)
734 e = run_until ("ss7_2");
736 assert_location (step_out(), "ss7");
738 //stepout in ss7_2_1, which must go to catch
739 run_until ("ss7_2_1");
740 assert_location (step_out (), "ss7_2");
742 //stepover over ss7_2, which must go to catch
744 assert_location (step_over (), "ss7_2");//move to "try {" line
745 assert_location (step_over (), "ss7_2");//move to "ss7_2_2();" line
746 assert_location (step_over (), "ss7_2");//step over ss7_2_2();, assume we are at "catch" now
747 assert_location (step_over (), "ss7_2");//move to { of catch
748 assert_location (step_over (), "ss7_2");//move to } of catch
749 assert_location (step_over (), "ss7_2");//move to } of method
750 assert_location (step_over (), "ss7");//finish method
752 //stepover over ss7_2_1, which must go to catch
753 run_until ("ss7_2_1");
754 assert_location (step_over (), "ss7_2_1");//move from { of method to "throw new Exception ();"
755 assert_location (step_over (), "ss7_2");//step over exception, being in ss7_2 means we are at catch
757 //stepin in ss7_3, which must go to catch
759 assert_location (step_into (), "ss7_3");//move to "try {"
760 assert_location (step_into (), "ss7_3");//move to "throw new Exception ();"
762 step_req.AssemblyFilter = new AssemblyMirror [] { (e as BreakpointEvent).Method.DeclaringType.Assembly };
763 assert_location (step_into (), "ss7_3");//call "throw new Exception ();", we assume we end up at "catch"
764 assert_location (step_into (), "ss7_3");//move to { of catch
765 assert_location (step_into (), "ss7_3");//move to } of catch
766 assert_location (step_into (), "ss7_3");//move to } of method
767 assert_location (step_into (), "ss7");//move out to ss7
769 //stepover in ss7_2_1, which must go to catch
770 run_until ("ss7_2_1");
771 assert_location (step_into (), "ss7_2_1");//move from { of method to "throw new Exception ();"
772 assert_location (step_into (), "ss7_2");//step in exception, being in ss7_2 means we are at catch
775 // Check that stepping stops between nested calls
776 e = run_until ("ss_nested_2");
777 req = create_step (e);
779 assert_location (e, "ss_nested");
781 assert_location (e, "ss_nested_1");
783 assert_location (e, "ss_nested");
784 // Check that step over steps over nested calls
786 assert_location (e, "ss_nested");
788 assert_location (e, "ss_nested_2");
790 assert_location (e, "ss_nested_2");
792 assert_location (e, "ss_nested_2");
794 assert_location (e, "ss_nested");
796 assert_location (e, "ss_nested_1");
798 assert_location (e, "ss_nested_1");
800 assert_location (e, "ss_nested");
803 // Check DebuggerStepThrough support
804 e = run_until ("ss_step_through");
805 req = create_step (e);
806 req.Filter = StepFilter.DebuggerStepThrough;
808 // Step through step_through_1 ()
810 assert_location (e, "ss_step_through");
811 // Step through StepThroughClass.step_through_2 ()
813 assert_location (e, "ss_step_through");
815 req.Filter = StepFilter.None;
817 assert_location (e, "step_through_3");
820 // Check DebuggerNonUserCode support
821 e = run_until ("ss_non_user_code");
822 req = create_step (e);
823 req.Filter = StepFilter.DebuggerNonUserCode;
825 // Step through non_user_code_1 ()
827 assert_location (e, "ss_non_user_code");
828 // Step through StepThroughClass.non_user_code_2 ()
830 assert_location (e, "ss_non_user_code");
832 req.Filter = StepFilter.None;
834 assert_location (e, "non_user_code_3");
837 // Check that step-over doesn't stop at inner frames with recursive functions
838 e = run_until ("ss_recursive");
839 req = create_step (e);
843 var f = e.Thread.GetFrames () [0];
844 assert_location (e, "ss_recursive");
845 AssertValue (1, f.GetValue (f.Method.GetLocal ("n")));
848 // Check that step-over stops correctly when inner frames with recursive functions contain breakpoints
849 e = run_until ("ss_recursive2");
850 ReusableBreakpoint breakpoint = new ReusableBreakpoint (this, "ss_recursive2_trap");
852 breakpoint.Continue ();
853 e = breakpoint.lastEvent;
854 req = create_step (e);
855 for (int c = 1; c <= 4; c++) {
856 // The first five times we try to step over this function, the breakpoint will stop us
857 assert_location_at_breakpoint (e, "ss_recursive2_trap");
860 req = create_step (e);
861 req.Size = StepSize.Line;
864 e = step_over ();//Stepout gets us to ss_recursive2_trap ();, move to ss_recursive2 (next); line
865 assert_location (e, "ss_recursive2");
867 // Stack should consist of Main + single_stepping + (1 ss_recursive2 frame per loop iteration)
868 Assert.AreEqual (c+2, e.Thread.GetFrames ().Length);
869 e = step_over_or_breakpoint ();
871 // At this point we should have escaped the breakpoints and this will be a normal step stop
872 assert_location (e, "ss_recursive2");
873 Assert.AreEqual (6, e.Thread.GetFrames ().Length);
876 breakpoint.Disable ();
879 // Check that step-out stops correctly when inner frames with recursive functions contain breakpoints
880 e = run_until ("ss_recursive2");
881 breakpoint = new ReusableBreakpoint (this, "ss_recursive2_trap");
883 breakpoint.Continue ();
884 e = breakpoint.lastEvent;
885 req = create_step (e);
886 for (int c = 1; c <= 4; c++) {
887 // The first five times we try to step over this function, the breakpoint will stop us
888 assert_location_at_breakpoint (e, "ss_recursive2_trap");
891 req = create_step (e);
892 req.Size = StepSize.Line;
895 assert_location (e, "ss_recursive2");
897 // Stack should consist of Main + single_stepping + (1 ss_recursive2 frame per loop iteration)
898 Assert.AreEqual (c+2, e.Thread.GetFrames ().Length);
899 e = step_out_or_breakpoint ();
901 for (int c = 3; c >= 1; c--) {
902 assert_location (e, "ss_recursive2");
903 Assert.AreEqual (c + 2, e.Thread.GetFrames ().Length);
909 breakpoint.Disable ();
912 // Test step out with a really complicated call tree
913 List<RecursiveChaoticPoint> trace = new List<RecursiveChaoticPoint>();
914 trace_ss_recursive_chaotic (trace);
915 e = run_until ("ss_recursive_chaotic");
917 breakpoint = new ReusableBreakpoint (this, "ss_recursive_chaotic_trap");
918 breakpoint.Continue ();
919 e = breakpoint.lastEvent;
920 foreach (RecursiveChaoticPoint point in trace)
922 if (point.breakpoint)
923 assert_location_at_breakpoint (e, point.name);
925 assert_location (e, point.name);
926 Assert.AreEqual (point.depth, e.Thread.GetFrames ().Length);
929 req = create_step (e);
930 req.Size = StepSize.Line;
931 e = step_out_or_breakpoint ();
935 breakpoint.Disable ();
938 // Check that single stepping doesn't clobber fp values
939 e = run_until ("ss_fp_clobber");
940 req = create_step (e);
942 f = e.Thread.GetFrames ()[0];
944 if ((e as StepEvent).Method.Name == "ss_fp_clobber_2")
948 f = e.Thread.GetFrames ()[0];
949 AssertValue (7.0, f.GetValue (f.Method.GetParameters ()[0]));
952 e = run_until ("ss_await");
953 e = step_in_await ("ss_await", e);//ss_await_1 ().Wait ();//in
954 e = step_in_await ("MoveNext", e);//{
955 e = step_in_await ("MoveNext", e);//var a = 1;
956 e = step_in_await ("MoveNext", e);//await Task.Delay (10);
957 e = step_in_await ("MoveNext", e);//return a + 2;
958 e = step_in_await ("MoveNext", e);//}
959 e = step_in_await ("ss_await", e);//ss_await_1 ().Wait ();//in
961 e = step_in_await ("ss_await", e);//ss_await_1 ().Wait ();//over
962 e = step_in_await ("MoveNext", e);//{
963 e = step_over_await ("MoveNext", e);//var a = 1;
964 e = step_over_await ("MoveNext", e);//await Task.Delay (10);
965 e = step_over_await ("MoveNext", e);//return a + 2;
966 e = step_over_await ("MoveNext", e);//}
967 e = step_over_await ("ss_await", e);//ss_await_1 ().Wait ();//over
969 e = step_in_await ("ss_await", e);//ss_await_1 ().Wait ();//out before
970 e = step_in_await ("MoveNext", e);//{
971 e = step_out_await ("ss_await", e);//ss_await_1 ().Wait ();//out before
973 e = step_in_await ("ss_await", e);//ss_await_1 ().Wait ();//out after
974 e = step_in_await ("MoveNext", e);//{
975 e = step_in_await ("MoveNext", e);//var a = 1;
976 e = step_in_await ("MoveNext", e);//await Task.Delay (10);
977 e = step_in_await ("MoveNext", e);//return a + 2;
978 e = step_out_await ("ss_await", e);//ss_await_1 ().Wait ();//out after
980 e = step_in_await ("ss_await", e);//ss_await_1_exc (true, true).Wait ();//in
981 e = step_in_await ("MoveNext", e);//{
982 e = step_in_await ("MoveNext", e);//var a = 1;
983 e = step_in_await ("MoveNext", e);//await Task.Delay (10);
984 e = step_in_await ("MoveNext", e);//if (exc)
985 e = step_in_await ("MoveNext", e);//{
986 e = step_in_await ("MoveNext", e);//if (handled)
987 e = step_in_await ("MoveNext", e);//{
988 e = step_in_await ("MoveNext", e);//try {
989 e = step_in_await ("MoveNext", e);//throw new Exception ();
990 e = step_in_await ("MoveNext", e);//catch
991 e = step_in_await ("MoveNext", e);//{
992 e = step_in_await ("MoveNext", e);//}
993 e = step_in_await ("MoveNext", e);//}
994 e = step_in_await ("MoveNext", e);//}
995 e = step_in_await ("MoveNext", e);//return a + 2;
996 e = step_in_await ("MoveNext", e);//}
997 e = step_in_await ("ss_await", e);//ss_await_1_exc (true, true).Wait ();//in
999 e = step_in_await ("ss_await", e);//ss_await_1_exc (true, true).Wait ();//over
1000 e = step_in_await ("MoveNext", e);//{
1001 e = step_over_await ("MoveNext", e);//var a = 1;
1002 e = step_over_await ("MoveNext", e);//await Task.Delay (10);
1003 e = step_over_await ("MoveNext", e);//if (exc)
1004 e = step_over_await ("MoveNext", e);//{
1005 e = step_over_await ("MoveNext", e);//if (handled)
1006 e = step_over_await ("MoveNext", e);//{
1007 e = step_over_await ("MoveNext", e);//try {
1008 e = step_over_await ("MoveNext", e);//throw new Exception ();
1009 e = step_over_await ("MoveNext", e);//catch
1010 e = step_over_await ("MoveNext", e);//{
1011 e = step_over_await ("MoveNext", e);//}
1012 e = step_over_await ("MoveNext", e);//}
1013 e = step_over_await ("MoveNext", e);//}
1014 e = step_over_await ("MoveNext", e);//return a + 2;
1015 e = step_over_await ("MoveNext", e);//}
1016 e = step_over_await ("ss_await", e);//ss_await_1_exc (true, true).Wait ();//over
1018 e = step_in_await ("ss_await", e);//ss_await_1_exc (true, true).Wait ();//out
1019 e = step_in_await ("MoveNext", e);//{
1020 e = step_out_await ("ss_await", e);//ss_await_1_exc (true, true).Wait ();//out
1022 e = step_in_await ("ss_await", e);//try {
1023 e = step_in_await ("ss_await", e);//ss_await_1_exc (true, false).Wait ();//in
1024 e = step_in_await ("MoveNext", e);//{
1025 e = step_in_await ("MoveNext", e);//var a = 1;
1026 e = step_in_await ("MoveNext", e);//await Task.Delay (10);
1027 e = step_in_await ("MoveNext", e);//if (exc)
1028 e = step_in_await ("MoveNext", e);//{
1029 e = step_in_await ("MoveNext", e);//if (handled)
1030 e = step_in_await ("MoveNext", e);//} else {
1031 e = step_in_await ("MoveNext", e);//throw new Exception ();
1032 e = step_in_await ("ss_await", e);//catch
1033 e = step_in_await ("ss_await", e);//{
1034 e = step_in_await ("ss_await", e);//}
1035 e = step_in_await ("ss_await", e);//try {
1037 e = step_in_await ("ss_await", e);//ss_await_1_exc (true, false).Wait ();//over
1038 e = step_in_await ("MoveNext", e);//{
1039 e = step_over_await ("MoveNext", e);//var a = 1;
1040 e = step_over_await ("MoveNext", e);//await Task.Delay (10);
1041 e = step_over_await ("MoveNext", e);//if (exc)
1042 e = step_over_await ("MoveNext", e);//{
1043 e = step_over_await ("MoveNext", e);//if (handled)
1044 e = step_over_await ("MoveNext", e);//} else {
1045 e = step_over_await ("MoveNext", e);//throw new Exception ();
1046 e = step_over_await ("ss_await", e);//catch
1047 e = step_over_await ("ss_await", e);//{
1048 e = step_over_await ("ss_await", e);//}
1049 e = step_over_await ("ss_await", e);//try {
1051 e = step_in_await ("ss_await", e);//ss_await_1_exc (true, false).Wait ();//out
1052 e = step_in_await ("MoveNext", e);//{
1053 e = step_out_await ("ss_await", e);//ss_await_1_exc (true, true).Wait ();//out
1056 Event step_in_await (string method, Event e)
1058 if (step_req != null)
1059 step_req.Disable ();
1061 step_req.AssemblyFilter = new List<AssemblyMirror> () { entry_point.DeclaringType.Assembly };
1062 var ef = step_into ();
1063 assert_location (ef, method);
1067 Event step_over_await (string method, Event e)
1069 if (step_req != null)
1070 step_req.Disable ();
1072 step_req.AssemblyFilter = new List<AssemblyMirror> () { entry_point.DeclaringType.Assembly };
1073 var ef = step_over ();
1074 assert_location (ef, method);
1078 Event step_out_await (string method, Event e)
1080 if (step_req != null)
1081 step_req.Disable ();
1083 step_req.AssemblyFilter = new List<AssemblyMirror> () { entry_point.DeclaringType.Assembly };
1084 var ef = step_out ();
1085 assert_location (ef, method);
1090 public void SingleSteppingNoFrames () {
1092 // Test what happens when starting a single step operation on a thread
1093 // with no managed frames
1095 // Run a delegate on a tp thread
1096 var e = run_until ("ss_no_frames_2");
1098 var this_type = e.Thread.GetFrames ()[0].Method.DeclaringType;
1099 this_type.SetValue (this_type.GetField ("static_i"), vm.CreateValue (56));
1101 var thread = e.Thread;
1102 var e2 = run_until ("ss_no_frames_3");
1103 // The tp thread should be idle now
1104 step_req = vm.CreateStepRequest (thread);
1105 step_req.Depth = StepDepth.Over;
1106 AssertThrows<Exception> (delegate {
1112 public void MethodEntryExit () {
1113 run_until ("single_stepping");
1115 var req1 = vm.CreateMethodEntryRequest ();
1116 var req2 = vm.CreateMethodExitRequest ();
1122 Event e = GetNextEvent ();
1123 Assert.IsTrue (e is MethodEntryEvent);
1124 Assert.AreEqual ("ss1", (e as MethodEntryEvent).Method.Name);
1127 e = GetNextEvent ();
1128 Assert.IsTrue (e is MethodExitEvent);
1129 Assert.AreEqual ("ss1", (e as MethodExitEvent).Method.Name);
1136 public void CountFilter () {
1137 run_until ("single_stepping");
1139 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("ss3");
1140 Assert.IsNotNull (m2);
1141 vm.SetBreakpoint (m2, 0);
1143 var req1 = vm.CreateMethodEntryRequest ();
1147 // Enter ss2, ss1 is skipped
1149 Event e = GetNextEvent ();
1150 Assert.IsTrue (e is MethodEntryEvent);
1151 Assert.AreEqual ("ss2", (e as MethodEntryEvent).Method.Name);
1153 // Breakpoint on ss3, the entry event is no longer reported
1155 e = GetNextEvent ();
1156 Assert.IsTrue (e is BreakpointEvent);
1162 public void Arguments () {
1165 var e = run_until ("arg1");
1167 StackFrame frame = e.Thread.GetFrames () [0];
1169 check_arg_val (frame, 0, typeof (sbyte), SByte.MaxValue - 5);
1170 check_arg_val (frame, 1, typeof (byte), Byte.MaxValue - 5);
1171 check_arg_val (frame, 2, typeof (bool), true);
1172 check_arg_val (frame, 3, typeof (short), Int16.MaxValue - 5);
1173 check_arg_val (frame, 4, typeof (ushort), UInt16.MaxValue - 5);
1174 check_arg_val (frame, 5, typeof (char), 'F');
1175 check_arg_val (frame, 6, typeof (int), Int32.MaxValue - 5);
1176 check_arg_val (frame, 7, typeof (uint), UInt32.MaxValue - 5);
1177 check_arg_val (frame, 8, typeof (long), Int64.MaxValue - 5);
1178 check_arg_val (frame, 9, typeof (ulong), UInt64.MaxValue - 5);
1179 check_arg_val (frame, 10, typeof (float), 1.2345f);
1180 check_arg_val (frame, 11, typeof (double), 6.78910);
1182 e = run_until ("arg2");
1184 frame = e.Thread.GetFrames () [0];
1187 val = frame.GetArgument (0);
1188 AssertValue ("FOO", val);
1189 Assert.AreEqual ("String", (val as ObjectMirror).Type.Name);
1192 val = frame.GetArgument (1);
1193 AssertValue (null, val);
1196 val = frame.GetArgument (2);
1197 AssertValue ("BLA", val);
1200 val = frame.GetArgument (3);
1201 AssertValue (42, val);
1204 val = frame.GetArgument (4);
1205 Assert.IsTrue (val is ObjectMirror);
1206 Assert.AreEqual ("GClass`1", (val as ObjectMirror).Type.Name);
1209 val = frame.GetArgument (5);
1210 Assert.IsTrue (val is ObjectMirror);
1211 Assert.AreEqual ("Object", (val as ObjectMirror).Type.Name);
1213 // this on static methods
1214 val = frame.GetThis ();
1215 AssertValue (null, val);
1217 e = run_until ("arg3");
1219 frame = e.Thread.GetFrames () [0];
1222 val = frame.GetThis ();
1223 Assert.IsTrue (val is ObjectMirror);
1224 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
1226 // objref in register
1227 val = frame.GetArgument (0);
1228 AssertValue ("BLA", val);
1232 public void Arrays () {
1235 var e = run_until ("o2");
1237 StackFrame frame = e.Thread.GetFrames () [0];
1240 val = frame.GetArgument (0);
1241 Assert.IsTrue (val is ArrayMirror);
1242 ArrayMirror arr = val as ArrayMirror;
1243 Assert.AreEqual (2, arr.Length);
1244 AssertValue ("BAR", arr [0]);
1245 AssertValue ("BAZ", arr [1]);
1247 var vals = arr.GetValues (0, 2);
1248 Assert.AreEqual (2, vals.Count);
1249 AssertValue ("BAR", vals [0]);
1250 AssertValue ("BAZ", vals [1]);
1252 arr [0] = vm.RootDomain.CreateString ("ABC");
1253 AssertValue ("ABC", arr [0]);
1255 arr [0] = vm.CreateValue (null);
1256 AssertValue (null, arr [0]);
1258 arr.SetValues (0, new Value [] { vm.RootDomain.CreateString ("D1"), vm.RootDomain.CreateString ("D2") });
1259 AssertValue ("D1", arr [0]);
1260 AssertValue ("D2", arr [1]);
1263 val = frame.GetArgument (1);
1264 Assert.IsTrue (val is ArrayMirror);
1265 arr = val as ArrayMirror;
1266 Assert.AreEqual (2, arr.Length);
1267 AssertValue (42, arr [0]);
1268 AssertValue (43, arr [1]);
1270 // Argument checking
1271 AssertThrows<IndexOutOfRangeException> (delegate () {
1275 AssertThrows<IndexOutOfRangeException> (delegate () {
1276 val = arr [Int32.MinValue];
1279 AssertThrows<IndexOutOfRangeException> (delegate () {
1280 vals = arr.GetValues (0, 3);
1283 AssertThrows<IndexOutOfRangeException> (delegate () {
1284 arr [2] = vm.CreateValue (null);
1287 AssertThrows<IndexOutOfRangeException> (delegate () {
1288 arr [Int32.MinValue] = vm.CreateValue (null);
1291 AssertThrows<IndexOutOfRangeException> (delegate () {
1292 arr.SetValues (0, new Value [] { null, null, null });
1296 val = frame.GetArgument (2);
1297 Assert.IsTrue (val is ArrayMirror);
1298 arr = val as ArrayMirror;
1299 Assert.AreEqual (2, arr.Rank);
1300 Assert.AreEqual (4, arr.Length);
1301 Assert.AreEqual (2, arr.GetLength (0));
1302 Assert.AreEqual (2, arr.GetLength (1));
1303 Assert.AreEqual (0, arr.GetLowerBound (0));
1304 Assert.AreEqual (0, arr.GetLowerBound (1));
1305 vals = arr.GetValues (0, 4);
1306 AssertValue (1, vals [0]);
1307 AssertValue (2, vals [1]);
1308 AssertValue (3, vals [2]);
1309 AssertValue (4, vals [3]);
1311 val = frame.GetArgument (3);
1312 Assert.IsTrue (val is ArrayMirror);
1313 arr = val as ArrayMirror;
1314 Assert.AreEqual (2, arr.Rank);
1315 Assert.AreEqual (4, arr.Length);
1316 Assert.AreEqual (2, arr.GetLength (0));
1317 Assert.AreEqual (2, arr.GetLength (1));
1318 Assert.AreEqual (1, arr.GetLowerBound (0));
1319 Assert.AreEqual (3, arr.GetLowerBound (1));
1321 AssertThrows<ArgumentOutOfRangeException> (delegate () {
1324 AssertThrows<ArgumentOutOfRangeException> (delegate () {
1328 AssertThrows<ArgumentOutOfRangeException> (delegate () {
1329 arr.GetLowerBound (-1);
1331 AssertThrows<ArgumentOutOfRangeException> (delegate () {
1332 arr.GetLowerBound (2);
1335 // arrays treated as generic collections
1336 val = frame.GetArgument (4);
1337 Assert.IsTrue (val is ArrayMirror);
1338 arr = val as ArrayMirror;
1342 public void Object_GetValue () {
1343 var e = run_until ("o1");
1344 var frame = e.Thread.GetFrames () [0];
1346 object val = frame.GetThis ();
1347 Assert.IsTrue (val is ObjectMirror);
1348 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
1349 ObjectMirror o = (val as ObjectMirror);
1351 TypeMirror t = o.Type;
1354 object f = o.GetValue (t.GetField ("field_i"));
1355 AssertValue (42, f);
1356 f = o.GetValue (t.GetField ("field_s"));
1357 AssertValue ("S", f);
1358 f = o.GetValue (t.GetField ("field_enum"));
1359 Assert.IsTrue (f is EnumMirror);
1360 Assert.AreEqual (1, (f as EnumMirror).Value);
1361 Assert.AreEqual ("B", (f as EnumMirror).StringValue);
1363 // Inherited object fields
1364 TypeMirror parent = t.BaseType;
1365 f = o.GetValue (parent.GetField ("base_field_i"));
1366 AssertValue (43, f);
1367 f = o.GetValue (parent.GetField ("base_field_s"));
1368 AssertValue ("T", f);
1371 f = o.GetValue (o.Type.GetField ("static_i"));
1372 AssertValue (55, f);
1374 // generic instances
1375 ObjectMirror o2 = frame.GetValue (frame.Method.GetParameters ()[1]) as ObjectMirror;
1376 Assert.AreEqual ("GClass`1", o2.Type.Name);
1377 TypeMirror t2 = o2.Type;
1378 f = o2.GetValue (t2.GetField ("field"));
1379 AssertValue (42, f);
1381 ObjectMirror o3 = frame.GetValue (frame.Method.GetParameters ()[2]) as ObjectMirror;
1382 Assert.AreEqual ("GClass`1", o3.Type.Name);
1383 TypeMirror t3 = o3.Type;
1384 f = o3.GetValue (t3.GetField ("field"));
1385 AssertValue ("FOO", f);
1387 // Argument checking
1388 AssertThrows<ArgumentNullException> (delegate () {
1394 public void Object_GetValues () {
1395 var e = run_until ("o1");
1396 var frame = e.Thread.GetFrames () [0];
1398 object val = frame.GetThis ();
1399 Assert.IsTrue (val is ObjectMirror);
1400 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
1401 ObjectMirror o = (val as ObjectMirror);
1403 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1405 TypeMirror t = o.Type;
1407 object[] vals = o.GetValues (new FieldInfoMirror [] { t.GetField ("field_i"), t.GetField ("field_s") });
1408 object f = vals [0];
1409 AssertValue (42, f);
1411 AssertValue ("S", f);
1413 // Argument checking
1414 AssertThrows<ArgumentNullException> (delegate () {
1418 AssertThrows<ArgumentNullException> (delegate () {
1419 o.GetValues (new FieldInfoMirror [] { null });
1422 // field of another class
1423 AssertThrows<ArgumentException> (delegate () {
1424 o.GetValue (val2.Type.GetField ("field_j"));
1428 void TestSetValue (ObjectMirror o, string field_name, object val) {
1430 o.SetValue (o.Type.GetField (field_name), vm.RootDomain.CreateString ((string)val));
1432 o.SetValue (o.Type.GetField (field_name), vm.CreateValue (val));
1433 Value f = o.GetValue (o.Type.GetField (field_name));
1434 AssertValue (val, f);
1438 public void Object_SetValues () {
1439 var e = run_until ("o1");
1440 var frame = e.Thread.GetFrames () [0];
1442 object val = frame.GetThis ();
1443 Assert.IsTrue (val is ObjectMirror);
1444 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
1445 ObjectMirror o = (val as ObjectMirror);
1447 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1449 TestSetValue (o, "field_i", 22);
1450 TestSetValue (o, "field_bool1", false);
1451 TestSetValue (o, "field_bool2", true);
1452 TestSetValue (o, "field_char", 'B');
1453 TestSetValue (o, "field_byte", (byte)129);
1454 TestSetValue (o, "field_sbyte", (sbyte)-33);
1455 TestSetValue (o, "field_short", (short)(Int16.MaxValue - 5));
1456 TestSetValue (o, "field_ushort", (ushort)(UInt16.MaxValue - 5));
1457 TestSetValue (o, "field_long", Int64.MaxValue - 5);
1458 TestSetValue (o, "field_ulong", (ulong)(UInt64.MaxValue - 5));
1459 TestSetValue (o, "field_float", 6.28f);
1460 TestSetValue (o, "field_double", 6.28);
1461 TestSetValue (o, "static_i", 23);
1462 TestSetValue (o, "field_s", "CDEF");
1467 f = o.GetValue (o.Type.GetField ("field_intptr"));
1468 Assert.IsInstanceOfType (typeof (StructMirror), f);
1469 AssertValue (Int32.MaxValue - 5, (f as StructMirror).Fields [0]);
1472 FieldInfoMirror field = o.Type.GetField ("field_enum");
1473 f = o.GetValue (field);
1474 (f as EnumMirror).Value = 5;
1475 o.SetValue (field, f);
1476 f = o.GetValue (field);
1477 Assert.AreEqual (5, (f as EnumMirror).Value);
1480 o.SetValue (o.Type.GetField ("field_s"), vm.CreateValue (null));
1481 f = o.GetValue (o.Type.GetField ("field_s"));
1482 AssertValue (null, f);
1485 field = o.Type.GetField ("generic_field_struct");
1486 f = o.GetValue (field);
1487 o.SetValue (field, f);
1490 field = o.Type.GetField ("field_nullable");
1491 f = o.GetValue (field);
1492 AssertValue (0, (f as StructMirror).Fields [0]);
1493 AssertValue (false, (f as StructMirror).Fields [1]);
1494 o.SetValue (field, vm.CreateValue (6));
1495 f = o.GetValue (field);
1496 AssertValue (6, (f as StructMirror).Fields [0]);
1497 AssertValue (true, (f as StructMirror).Fields [1]);
1498 o.SetValue (field, vm.CreateValue (null));
1499 f = o.GetValue (field);
1500 AssertValue (0, (f as StructMirror).Fields [0]);
1501 AssertValue (false, (f as StructMirror).Fields [1]);
1503 // Argument checking
1504 AssertThrows<ArgumentNullException> (delegate () {
1505 o.SetValues (null, new Value [0]);
1508 AssertThrows<ArgumentNullException> (delegate () {
1509 o.SetValues (new FieldInfoMirror [0], null);
1512 AssertThrows<ArgumentNullException> (delegate () {
1513 o.SetValues (new FieldInfoMirror [] { null }, new Value [1] { null });
1516 // vtype with a wrong type
1517 AssertThrows<ArgumentException> (delegate () {
1518 o.SetValue (o.Type.GetField ("field_struct"), o.GetValue (o.Type.GetField ("field_enum")));
1521 // reference type not assignment compatible
1522 AssertThrows<ArgumentException> (delegate () {
1523 o.SetValue (o.Type.GetField ("field_class"), o);
1526 // field of another class
1527 AssertThrows<ArgumentException> (delegate () {
1528 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
1533 public void Type_SetValue () {
1534 var e = run_until ("o1");
1535 var frame = e.Thread.GetFrames () [0];
1538 object val = frame.GetThis ();
1539 Assert.IsTrue (val is ObjectMirror);
1540 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
1541 ObjectMirror o = (val as ObjectMirror);
1543 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1545 o.Type.SetValue (o.Type.GetField ("static_i"), vm.CreateValue (55));
1546 f = o.Type.GetValue (o.Type.GetField ("static_i"));
1547 AssertValue (55, f);
1549 o.Type.SetValue (o.Type.GetField ("static_s"), vm.RootDomain.CreateString ("B"));
1550 f = o.Type.GetValue (o.Type.GetField ("static_s"));
1551 AssertValue ("B", f);
1553 // Argument checking
1554 AssertThrows<ArgumentNullException> (delegate () {
1555 o.Type.SetValue (null, vm.CreateValue (0));
1558 AssertThrows<ArgumentNullException> (delegate () {
1559 o.Type.SetValue (o.Type.GetField ("static_i"), null);
1562 // field of another class
1563 AssertThrows<ArgumentException> (delegate () {
1564 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
1569 public void TypeInfo () {
1570 Event e = run_until ("ti2");
1571 StackFrame frame = e.Thread.GetFrames () [0];
1576 t = frame.Method.GetParameters ()[0].ParameterType;
1578 Assert.AreEqual ("String[]", t.Name);
1579 Assert.AreEqual ("string[]", t.CSharpName);
1580 Assert.AreEqual ("Array", t.BaseType.Name);
1581 Assert.AreEqual (true, t.HasElementType);
1582 Assert.AreEqual (true, t.IsArray);
1583 Assert.AreEqual (1, t.GetArrayRank ());
1584 Assert.AreEqual ("String", t.GetElementType ().Name);
1586 t = frame.Method.GetParameters ()[2].ParameterType;
1588 Assert.AreEqual ("Int32[,]", t.Name);
1590 //Assert.AreEqual ("int[,]", t.CSharpName);
1591 Assert.AreEqual ("Array", t.BaseType.Name);
1592 Assert.AreEqual (true, t.HasElementType);
1593 Assert.AreEqual (true, t.IsArray);
1594 Assert.AreEqual (2, t.GetArrayRank ());
1595 Assert.AreEqual ("Int32", t.GetElementType ().Name);
1598 t = frame.Method.GetParameters ()[3].ParameterType;
1600 //Assert.AreEqual ("Int32&", t.Name);
1601 //Assert.AreEqual (true, t.IsByRef);
1602 //Assert.AreEqual (true, t.HasElementType);
1605 t = frame.Method.GetParameters ()[4].ParameterType;
1607 //Assert.AreEqual ("Int32*", t.Name);
1608 Assert.AreEqual (true, t.IsPointer);
1609 Assert.AreEqual (true, t.HasElementType);
1610 Assert.AreEqual ("Int32", t.GetElementType ().Name);
1611 Assert.AreEqual (false, t.IsPrimitive);
1614 t = frame.Method.GetParameters ()[5].ParameterType;
1615 Assert.AreEqual (true, t.IsPrimitive);
1618 t = frame.Method.GetParameters ()[6].ParameterType;
1619 Assert.AreEqual ("AStruct", t.Name);
1620 Assert.AreEqual (false, t.IsPrimitive);
1621 Assert.AreEqual (true, t.IsValueType);
1622 Assert.AreEqual (false, t.IsClass);
1625 t = frame.Method.GetParameters ()[7].ParameterType;
1626 Assert.AreEqual ("Tests", t.Name);
1627 var nested = (from nt in t.GetNestedTypes () where nt.IsNestedPublic select nt).ToArray ();
1628 Assert.AreEqual (1, nested.Length);
1629 Assert.AreEqual ("NestedClass", nested [0].Name);
1630 Assert.IsTrue (t.BaseType.IsAssignableFrom (t));
1631 Assert.IsTrue (!t.IsAssignableFrom (t.BaseType));
1633 // generic instances
1634 t = frame.Method.GetParameters ()[9].ParameterType;
1635 Assert.AreEqual ("GClass`1", t.Name);
1636 Assert.IsTrue (t.IsGenericType);
1637 Assert.IsFalse (t.IsGenericTypeDefinition);
1639 var args = t.GetGenericArguments ();
1640 Assert.AreEqual (1, args.Length);
1641 Assert.AreEqual ("Int32", args [0].Name);
1643 // generic type definitions
1644 var gtd = t.GetGenericTypeDefinition ();
1645 Assert.AreEqual ("GClass`1", gtd.Name);
1646 Assert.IsTrue (gtd.IsGenericType);
1647 Assert.IsTrue (gtd.IsGenericTypeDefinition);
1648 Assert.AreEqual (gtd, gtd.GetGenericTypeDefinition ());
1650 args = gtd.GetGenericArguments ();
1651 Assert.AreEqual (1, args.Length);
1652 Assert.AreEqual ("T", args [0].Name);
1655 t = frame.Method.GetParameters ()[10].ParameterType;
1656 Assert.AreEqual ("AnEnum", t.Name);
1657 Assert.IsTrue (t.IsEnum);
1658 Assert.AreEqual ("Int32", t.EnumUnderlyingType.Name);
1661 t = frame.Method.GetParameters ()[11].ParameterType;
1662 Assert.AreEqual ("TypedReference", t.Name);
1665 t = frame.Method.GetParameters ()[7].ParameterType;
1667 var props = t.GetProperties ();
1668 Assert.AreEqual (3, props.Length);
1669 foreach (PropertyInfoMirror prop in props) {
1670 ParameterInfoMirror[] indexes = prop.GetIndexParameters ();
1672 if (prop.Name == "IntProperty") {
1673 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1674 Assert.AreEqual ("get_IntProperty", prop.GetGetMethod ().Name);
1675 Assert.AreEqual ("set_IntProperty", prop.GetSetMethod ().Name);
1676 Assert.AreEqual (0, indexes.Length);
1677 } else if (prop.Name == "ReadOnlyProperty") {
1678 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1679 Assert.AreEqual ("get_ReadOnlyProperty", prop.GetGetMethod ().Name);
1680 Assert.AreEqual (null, prop.GetSetMethod ());
1681 Assert.AreEqual (0, indexes.Length);
1682 } else if (prop.Name == "IndexedProperty") {
1683 Assert.AreEqual (1, indexes.Length);
1684 Assert.AreEqual ("Int32", indexes [0].ParameterType.Name);
1688 // custom attributes
1689 t = frame.Method.GetParameters ()[8].ParameterType;
1690 Assert.AreEqual ("Tests2", t.Name);
1691 var attrs = t.GetCustomAttributes (true);
1692 Assert.AreEqual (5, attrs.Length);
1693 foreach (var attr in attrs) {
1694 if (attr.Constructor.DeclaringType.Name == "DebuggerDisplayAttribute") {
1695 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1696 Assert.AreEqual ("Tests", attr.ConstructorArguments [0].Value);
1697 Assert.AreEqual (2, attr.NamedArguments.Count);
1698 Assert.AreEqual ("Name", attr.NamedArguments [0].Property.Name);
1699 Assert.AreEqual ("FOO", attr.NamedArguments [0].TypedValue.Value);
1700 Assert.AreEqual ("Target", attr.NamedArguments [1].Property.Name);
1701 Assert.IsInstanceOfType (typeof (TypeMirror), attr.NamedArguments [1].TypedValue.Value);
1702 Assert.AreEqual ("Int32", (attr.NamedArguments [1].TypedValue.Value as TypeMirror).Name);
1703 } else if (attr.Constructor.DeclaringType.Name == "DebuggerTypeProxyAttribute") {
1704 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1705 Assert.IsInstanceOfType (typeof (TypeMirror), attr.ConstructorArguments [0].Value);
1706 Assert.AreEqual ("Tests", (attr.ConstructorArguments [0].Value as TypeMirror).Name);
1707 } else if (attr.Constructor.DeclaringType.Name == "BAttribute") {
1708 Assert.AreEqual (2, attr.NamedArguments.Count);
1709 Assert.AreEqual ("afield", attr.NamedArguments [0].Field.Name);
1710 Assert.AreEqual ("bfield", attr.NamedArguments [1].Field.Name);
1711 } else if (attr.Constructor.DeclaringType.Name == "ClassInterfaceAttribute") {
1712 // inherited from System.Object
1713 //} else if (attr.Constructor.DeclaringType.Name == "Serializable") {
1714 // inherited from System.Object
1715 } else if (attr.Constructor.DeclaringType.Name == "ComVisibleAttribute") {
1716 // inherited from System.Object
1718 Assert.Fail (attr.Constructor.DeclaringType.Name);
1722 var assembly = entry_point.DeclaringType.Assembly;
1723 var type = assembly.GetType ("Tests4");
1724 Assert.IsFalse (type.IsInitialized);
1728 public void FieldInfo () {
1729 Event e = run_until ("ti2");
1730 StackFrame frame = e.Thread.GetFrames () [0];
1734 t = frame.Method.GetParameters ()[8].ParameterType;
1735 Assert.AreEqual ("Tests2", t.Name);
1737 var fi = t.GetField ("field_j");
1738 var attrs = fi.GetCustomAttributes (true);
1739 Assert.AreEqual (1, attrs.Length);
1740 var attr = attrs [0];
1741 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1742 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1743 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1744 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1748 public void PropertyInfo () {
1749 Event e = run_until ("ti2");
1750 StackFrame frame = e.Thread.GetFrames () [0];
1754 t = frame.Method.GetParameters ()[8].ParameterType;
1755 Assert.AreEqual ("Tests2", t.Name);
1757 var pi = t.GetProperty ("AProperty");
1758 var attrs = pi.GetCustomAttributes (true);
1759 Assert.AreEqual (1, attrs.Length);
1760 var attr = attrs [0];
1761 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1762 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1763 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1764 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1768 [Category ("only5")]
1769 public void Type_GetValue () {
1770 Event e = run_until ("o1");
1771 StackFrame frame = e.Thread.GetFrames () [0];
1773 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1775 TypeMirror t = o.Type;
1777 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1780 object f = t.GetValue (o.Type.GetField ("static_i"));
1781 AssertValue (55, f);
1783 f = t.GetValue (o.Type.GetField ("static_s"));
1784 AssertValue ("A", f);
1786 // literal static fields
1787 f = t.GetValue (o.Type.GetField ("literal_i"));
1788 AssertValue (56, f);
1790 f = t.GetValue (o.Type.GetField ("literal_s"));
1791 AssertValue ("B", f);
1793 // Inherited static fields
1794 TypeMirror parent = t.BaseType;
1795 f = t.GetValue (parent.GetField ("base_static_i"));
1796 AssertValue (57, f);
1798 f = t.GetValue (parent.GetField ("base_static_s"));
1799 AssertValue ("C", f);
1801 // thread static field
1802 f = t.GetValue (t.GetField ("tls_i"), e.Thread);
1803 AssertValue (42, f);
1805 // Argument checking
1806 AssertThrows<ArgumentNullException> (delegate () {
1811 AssertThrows<ArgumentException> (delegate () {
1812 t.GetValue (o.Type.GetField ("field_i"));
1815 // field on another type
1816 AssertThrows<ArgumentException> (delegate () {
1817 t.GetValue (val2.Type.GetField ("static_field_j"));
1820 // special static field
1821 AssertThrows<ArgumentException> (delegate () {
1822 t.GetValue (t.GetField ("tls_i"));
1827 public void Type_GetValues () {
1828 Event e = run_until ("o1");
1829 StackFrame frame = e.Thread.GetFrames () [0];
1831 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1833 TypeMirror t = o.Type;
1836 object[] vals = t.GetValues (new FieldInfoMirror [] { t.GetField ("static_i"), t.GetField ("static_s") });
1837 object f = vals [0];
1838 AssertValue (55, f);
1841 AssertValue ("A", f);
1843 // Argument checking
1844 AssertThrows<ArgumentNullException> (delegate () {
1848 AssertThrows<ArgumentNullException> (delegate () {
1849 t.GetValues (new FieldInfoMirror [] { null });
1854 public void ObjRefs () {
1855 Event e = run_until ("objrefs1");
1856 StackFrame frame = e.Thread.GetFrames () [0];
1858 ObjectMirror o = frame.GetThis () as ObjectMirror;
1859 ObjectMirror child = o.GetValue (o.Type.GetField ("child")) as ObjectMirror;
1861 Assert.IsTrue (child.Address != 0);
1863 // Check that object references are internalized correctly
1864 Assert.AreEqual (o, frame.GetThis ());
1866 run_until ("objrefs2");
1868 // child should be gc'd now
1869 // This is not deterministic
1870 //Assert.IsTrue (child.IsCollected);
1873 * No longer works since Type is read eagerly
1876 AssertThrows<ObjectCollectedException> (delegate () {
1877 TypeMirror t = child.Type;
1881 AssertThrows<ObjectCollectedException> (delegate () {
1882 long addr = child.Address;
1888 public void Type_GetObject () {
1889 Event e = run_until ("o1");
1890 StackFrame frame = e.Thread.GetFrames () [0];
1892 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1894 TypeMirror t = o.Type;
1896 Assert.AreEqual ("RuntimeType", t.GetTypeObject ().Type.Name);
1900 public void VTypes () {
1901 Event e = run_until ("vtypes1");
1902 StackFrame frame = e.Thread.GetFrames () [0];
1905 ObjectMirror o = frame.GetThis () as ObjectMirror;
1906 var obj = o.GetValue (o.Type.GetField ("field_struct"));
1907 Assert.IsTrue (obj is StructMirror);
1908 var s = obj as StructMirror;
1909 Assert.AreEqual ("AStruct", s.Type.Name);
1910 AssertValue (42, s ["i"]);
1912 AssertValue ("S", obj);
1913 AssertValue (43, s ["k"]);
1914 obj = o.GetValue (o.Type.GetField ("field_boxed_struct"));
1915 Assert.IsTrue (obj is StructMirror);
1916 s = obj as StructMirror;
1917 Assert.AreEqual ("AStruct", s.Type.Name);
1918 AssertValue (42, s ["i"]);
1920 // Check decoding of nested structs (#14942)
1921 obj = o.GetValue (o.Type.GetField ("nested_struct"));
1922 o.SetValue (o.Type.GetField ("nested_struct"), obj);
1924 // Check round tripping of boxed struct fields (#12354)
1925 obj = o.GetValue (o.Type.GetField ("boxed_struct_field"));
1926 o.SetValue (o.Type.GetField ("boxed_struct_field"), obj);
1927 obj = o.GetValue (o.Type.GetField ("boxed_struct_field"));
1928 s = obj as StructMirror;
1929 AssertValue (1, s ["key"]);
1931 Assert.IsTrue (obj is StructMirror);
1932 s = obj as StructMirror;
1933 AssertValue (42, s ["m_value"]);
1935 // vtypes as arguments
1936 s = frame.GetArgument (0) as StructMirror;
1937 AssertValue (44, s ["i"]);
1939 AssertValue ("T", obj);
1940 AssertValue (45, s ["k"]);
1942 // vtypes as array entries
1943 var arr = frame.GetArgument (1) as ArrayMirror;
1945 Assert.IsTrue (obj is StructMirror);
1946 s = obj as StructMirror;
1947 AssertValue (1, s ["i"]);
1948 AssertValue ("S1", s ["s"]);
1950 Assert.IsTrue (obj is StructMirror);
1951 s = obj as StructMirror;
1952 AssertValue (2, s ["i"]);
1953 AssertValue ("S2", s ["s"]);
1956 var typedref = frame.GetArgument (2) as StructMirror;
1957 Assert.IsTrue (typedref is StructMirror);
1959 // Argument checking
1960 s = frame.GetArgument (0) as StructMirror;
1961 AssertThrows<ArgumentException> (delegate () {
1965 // generic vtype instances
1966 o = frame.GetThis () as ObjectMirror;
1967 obj = o.GetValue (o.Type.GetField ("generic_field_struct"));
1968 Assert.IsTrue (obj is StructMirror);
1969 s = obj as StructMirror;
1970 Assert.AreEqual ("GStruct`1", s.Type.Name);
1971 AssertValue (42, s ["i"]);
1973 // this on vtype methods
1974 e = run_until ("vtypes2");
1975 e = step_until (e.Thread, "foo");
1977 frame = e.Thread.GetFrames () [0];
1979 Assert.AreEqual ("foo", (e as StepEvent).Method.Name);
1980 obj = frame.GetThis ();
1982 Assert.IsTrue (obj is StructMirror);
1983 s = obj as StructMirror;
1984 AssertValue (44, s ["i"]);
1985 AssertValue ("T", s ["s"]);
1986 AssertValue (45, s ["k"]);
1989 s ["i"] = vm.CreateValue (55);
1991 obj = frame.GetThis ();
1992 Assert.IsTrue (obj is StructMirror);
1993 s = obj as StructMirror;
1994 AssertValue (55, s ["i"]);
1995 AssertValue ("T", s ["s"]);
1996 AssertValue (45, s ["k"]);
1998 // this on static vtype methods
1999 e = run_until ("vtypes3");
2000 e = step_until (e.Thread, "static_foo");
2002 frame = e.Thread.GetFrames () [0];
2004 Assert.AreEqual ("static_foo", (e as StepEvent).Method.Name);
2005 obj = frame.GetThis ();
2006 AssertValue (null, obj);
2008 // vtypes which reference themselves recursively
2009 e = run_until ("vtypes4_2");
2010 frame = e.Thread.GetFrames () [0];
2012 Assert.IsTrue (frame.GetArgument (0) is StructMirror);
2016 public void AssemblyInfo () {
2017 Event e = run_until ("single_stepping");
2019 StackFrame frame = e.Thread.GetFrames () [0];
2021 var aname = frame.Method.DeclaringType.Assembly.GetName ();
2022 Assert.AreEqual ("dtest-app, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", aname.ToString ());
2024 ModuleMirror m = frame.Method.DeclaringType.Module;
2026 Assert.AreEqual ("dtest-app.exe", m.Name);
2027 Assert.AreEqual ("dtest-app.exe", m.ScopeName);
2028 Assert.IsTrue (m.FullyQualifiedName.IndexOf ("dtest-app.exe") != -1);
2029 Guid guid = m.ModuleVersionId;
2030 Assert.AreEqual (frame.Method.DeclaringType.Assembly, m.Assembly);
2031 Assert.AreEqual (frame.Method.DeclaringType.Assembly.ManifestModule, m);
2033 // This is no longer true on 4.0
2034 //Assert.AreEqual ("Assembly", frame.Method.DeclaringType.Assembly.GetAssemblyObject ().Type.Name);
2036 TypeMirror t = vm.RootDomain.Corlib.GetType ("System.Diagnostics.DebuggerDisplayAttribute");
2037 Assert.AreEqual ("DebuggerDisplayAttribute", t.Name);
2041 public void LocalsInfo () {
2042 Event e = run_until ("locals2");
2044 StackFrame frame = e.Thread.GetFrames () [0];
2046 var locals = frame.Method.GetLocals ();
2047 Assert.AreEqual (9, locals.Length);
2048 for (int i = 0; i < 9; ++i) {
2049 if (locals [i].Name == "args") {
2050 Assert.IsTrue (locals [i].IsArg);
2051 Assert.AreEqual ("String[]", locals [i].Type.Name);
2052 } else if (locals [i].Name == "arg") {
2053 Assert.IsTrue (locals [i].IsArg);
2054 Assert.AreEqual ("Int32", locals [i].Type.Name);
2055 } else if (locals [i].Name == "i") {
2056 Assert.IsFalse (locals [i].IsArg);
2057 Assert.AreEqual ("Int64", locals [i].Type.Name);
2058 } else if (locals [i].Name == "j") {
2059 Assert.IsFalse (locals [i].IsArg);
2060 Assert.AreEqual ("Int32", locals [i].Type.Name);
2061 } else if (locals [i].Name == "s") {
2062 Assert.IsFalse (locals [i].IsArg);
2063 Assert.AreEqual ("String", locals [i].Type.Name);
2064 } else if (locals [i].Name == "t") {
2066 Assert.IsTrue (locals [i].IsArg);
2067 Assert.AreEqual ("String", locals [i].Type.Name);
2068 } else if (locals [i].Name == "rs") {
2069 Assert.IsTrue (locals [i].IsArg);
2070 Assert.AreEqual ("String", locals [i].Type.Name);
2071 } else if (locals [i].Name == "astruct") {
2072 } else if (locals [i].Name == "alist") {
2078 var scopes = frame.Method.GetScopes ();
2079 Assert.AreEqual (2, scopes.Length);
2082 Event step_once () {
2084 var e = GetNextEvent ();
2085 Assert.IsTrue (e is StepEvent);
2089 Event step_into () {
2090 step_req.Disable ();
2091 step_req.Depth = StepDepth.Into;
2093 return step_once ();
2096 Event step_over () {
2097 step_req.Disable ();
2098 step_req.Depth = StepDepth.Over;
2100 return step_once ();
2104 step_req.Disable ();
2105 step_req.Depth = StepDepth.Out;
2107 return step_once ();
2110 Event step_once_or_breakpoint () {
2112 var e = GetNextEvent ();
2113 Assert.IsTrue (e is StepEvent || e is BreakpointEvent);
2117 Event step_over_or_breakpoint () {
2118 step_req.Disable ();
2119 step_req.Depth = StepDepth.Over;
2121 return step_once_or_breakpoint ();
2124 Event step_out_or_breakpoint () {
2125 step_req.Disable ();
2126 step_req.Depth = StepDepth.Out;
2128 return step_once_or_breakpoint ();
2132 public void Locals () {
2133 var be = run_until ("locals1");
2135 StackFrame frame = be.Thread.GetFrames () [0];
2136 MethodMirror m1 = frame.Method;
2138 // Compiler generated byref local
2139 foreach (var l in m1.GetLocals ()) {
2140 // The byval flag is hidden from the type
2141 if (l.Name != "ri" && l.Type.Name == "Double")
2142 AssertValue (null, frame.GetValue (l));
2145 be = run_until ("locals2");
2147 frame = be.Thread.GetFrames () [0];
2149 object val = frame.GetValue (frame.Method.GetLocal ("i"));
2150 AssertValue (0, val);
2152 var req = create_step (be);
2159 var e = step_once ();
2160 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
2162 // Execute s = "AB";
2164 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
2166 frame = e.Thread.GetFrames () [0];
2168 val = frame.GetValue (frame.Method.GetLocal ("i"));
2169 AssertValue (42, val);
2171 LocalVariable[] locals = frame.Method.GetLocals ();
2172 var vals = frame.GetValues (locals);
2173 Assert.AreEqual (locals.Length, vals.Length);
2174 for (int i = 0; i < locals.Length; ++i) {
2175 if (locals [i].Name == "i")
2176 AssertValue (42, vals [i]);
2177 if (locals [i].Name == "s")
2178 AssertValue ("AB", vals [i]);
2179 if (locals [i].Name == "t")
2180 AssertValue ("ABC", vals [i]);
2181 if (locals [i].Name == "alist") {
2185 // Argument checking
2188 AssertThrows<ArgumentNullException> (delegate () {
2189 frame.GetValue ((LocalVariable)null);
2191 // GetValue () local from another method
2192 AssertThrows<ArgumentException> (delegate () {
2193 frame.GetValue (m1.GetLocal ("foo"));
2197 AssertThrows<ArgumentNullException> (delegate () {
2198 frame.GetValue ((ParameterInfoMirror)null);
2200 // GetValue () local from another method
2201 AssertThrows<ArgumentException> (delegate () {
2202 frame.GetValue (m1.GetParameters ()[0]);
2205 // GetValues () null
2206 AssertThrows<ArgumentNullException> (delegate () {
2207 frame.GetValues (null);
2209 // GetValues () embedded null
2210 AssertThrows<ArgumentNullException> (delegate () {
2211 frame.GetValues (new LocalVariable [] { null });
2213 // GetValues () local from another method
2214 AssertThrows<ArgumentException> (delegate () {
2215 frame.GetValues (new LocalVariable [] { m1.GetLocal ("foo") });
2218 AssertThrows<ArgumentException> (delegate () {
2219 val = frame.GetValue (frame.Method.ReturnParameter);
2222 // invalid stack frames
2224 e = GetNextEvent ();
2225 Assert.IsTrue (e is StepEvent);
2226 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
2228 AssertThrows<InvalidStackFrameException> (delegate () {
2229 frame.GetValue (frame.Method.GetLocal ("i"));
2235 be = run_until ("locals7");
2237 req = create_step (be);
2243 // Test that locals are initialized
2244 frame = e.Thread.GetFrames () [0];
2245 val = frame.GetValue (frame.Method.GetLocal ("t"));
2246 AssertValue (0, val);
2250 Assert.AreEqual ("locals7", (e as StepEvent).Method.Name);
2254 Assert.AreEqual ("locals7", (e as StepEvent).Method.Name);
2256 frame = e.Thread.GetFrames () [0];
2257 val = frame.GetValue (frame.Method.GetParameters ()[0]);
2258 AssertValue (22, val);
2259 val = frame.GetValue (frame.Method.GetLocal ("t"));
2260 AssertValue (22, val);
2261 val = frame.GetValue (frame.Method.GetLocal ("t2"));
2262 AssertValue (22, val);
2266 public void GetVisibleVariables () {
2267 Event e = run_until ("locals4");
2270 var locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
2271 Assert.AreEqual (2, locals.Count);
2272 var loc = locals.First (l => l.Name == "i");
2273 Assert.AreEqual ("Int64", loc.Type.Name);
2274 loc = locals.First (l => l.Name == "s");
2275 Assert.AreEqual ("String", loc.Type.Name);
2277 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
2278 Assert.AreEqual ("i", loc.Name);
2279 Assert.AreEqual ("Int64", loc.Type.Name);
2281 e = run_until ("locals5");
2284 locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
2285 Assert.AreEqual (2, locals.Count);
2286 loc = locals.First (l => l.Name == "i");
2287 Assert.AreEqual ("String", loc.Type.Name);
2288 loc = locals.First (l => l.Name == "s");
2289 Assert.AreEqual ("String", loc.Type.Name);
2291 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
2292 Assert.AreEqual ("i", loc.Name);
2293 Assert.AreEqual ("String", loc.Type.Name);
2295 // Variable in another scope
2296 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("j");
2297 Assert.IsNull (loc);
2301 public void Exit () {
2306 var e = GetNextEvent ();
2307 Assert.IsInstanceOfType (typeof (VMDeathEvent), e);
2309 Assert.AreEqual (5, (e as VMDeathEvent).ExitCode);
2312 /* Could be a remote vm with no process */
2315 Assert.AreEqual (5, p.ExitCode);
2318 AssertThrows<VMDisconnectedException> (delegate () {
2327 public void Dispose () {
2332 var e = GetNextEvent ();
2333 Assert.IsInstanceOfType (typeof (VMDisconnectEvent), e);
2336 /* Could be a remote vm with no process */
2339 Assert.AreEqual (3, p.ExitCode);
2342 AssertThrows<VMDisconnectedException> (delegate () {
2351 public void ColumnNumbers () {
2352 Event e = run_until ("line_numbers");
2354 // FIXME: Merge this with LineNumbers () when its fixed
2356 step_req = create_step (e);
2357 step_req.Depth = StepDepth.Into;
2365 e = GetNextEvent ();
2366 Assert.IsTrue (e is StepEvent);
2367 if (e.Thread.GetFrames ()[0].Method.Name == "ln1")
2371 // Do an additional step over so we are not on the beginning line of the method
2372 step_req.Disable ();
2373 step_req.Depth = StepDepth.Over;
2376 e = GetNextEvent ();
2377 Assert.IsTrue (e is StepEvent);
2379 l = e.Thread.GetFrames ()[0].Location;
2381 Assert.AreEqual (3, l.ColumnNumber);
2383 step_req.Disable ();
2387 // Broken by mcs+runtime changes (#5438)
2388 [Category("NotWorking")]
2389 public void LineNumbers () {
2390 Event e = run_until ("line_numbers");
2392 step_req = create_step (e);
2393 step_req.Depth = StepDepth.Into;
2400 e = GetNextEvent ();
2401 Assert.IsTrue (e is StepEvent);
2403 l = e.Thread.GetFrames ()[0].Location;
2405 Assert.IsTrue (l.SourceFile.IndexOf ("dtest-app.cs") != -1);
2406 Assert.AreEqual ("ln1", l.Method.Name);
2409 using (FileStream fs = new FileStream (l.SourceFile, FileMode.Open, FileAccess.Read)) {
2410 MD5 md5 = MD5.Create ();
2411 var hash = md5.ComputeHash (fs);
2413 for (int i = 0; i < 16; ++i)
2414 Assert.AreEqual (hash [i], l.SourceFileHash [i]);
2417 int line_base = l.LineNumber;
2420 e = GetNextEvent ();
2421 Assert.IsTrue (e is StepEvent);
2422 l = e.Thread.GetFrames ()[0].Location;
2423 Assert.AreEqual ("ln2", l.Method.Name);
2424 Assert.AreEqual (line_base + 6, l.LineNumber);
2427 e = GetNextEvent ();
2428 Assert.IsTrue (e is StepEvent);
2429 l = e.Thread.GetFrames ()[0].Location;
2430 Assert.AreEqual ("ln1", l.Method.Name);
2431 Assert.AreEqual (line_base + 1, l.LineNumber);
2434 e = GetNextEvent ();
2435 Assert.IsTrue (e is StepEvent);
2436 l = e.Thread.GetFrames ()[0].Location;
2437 Assert.AreEqual ("ln3", l.Method.Name);
2438 Assert.AreEqual (line_base + 11, l.LineNumber);
2441 e = GetNextEvent ();
2442 Assert.IsTrue (e is StepEvent);
2443 l = e.Thread.GetFrames ()[0].Location;
2444 Assert.AreEqual ("ln3", l.Method.Name);
2445 Assert.IsTrue (l.SourceFile.EndsWith ("FOO"));
2446 Assert.AreEqual (55, l.LineNumber);
2449 e = GetNextEvent ();
2450 Assert.IsTrue (e is StepEvent);
2451 l = e.Thread.GetFrames ()[0].Location;
2452 Assert.AreEqual ("ln1", l.Method.Name);
2453 Assert.AreEqual (line_base + 2, l.LineNumber);
2455 // GetSourceFiles ()
2456 string[] sources = l.Method.DeclaringType.GetSourceFiles ();
2457 Assert.AreEqual (2, sources.Length);
2458 Assert.AreEqual ("dtest-app.cs", sources [0]);
2459 Assert.AreEqual ("FOO", sources [1]);
2461 sources = l.Method.DeclaringType.GetSourceFiles (true);
2462 Assert.AreEqual (2, sources.Length);
2463 Assert.IsTrue (sources [0].EndsWith ("dtest-app.cs"));
2464 Assert.IsTrue (sources [1].EndsWith ("FOO"));
2468 public void Suspend () {
2471 Start (new string [] { "dtest-app.exe", "suspend-test" });
2473 Event e = run_until ("suspend");
2475 ThreadMirror main = e.Thread;
2483 // The debuggee should be suspended while it is running the infinite loop
2485 StackFrame frame = main.GetFrames ()[0];
2486 Assert.AreEqual ("suspend", frame.Method.Name);
2490 // resuming when not suspended
2491 AssertThrows<InvalidOperationException> (delegate () {
2501 public void AssemblyLoad () {
2502 Event e = run_until ("assembly_load");
2504 var load_req = vm.CreateAssemblyLoadRequest ();
2509 e = GetNextEvent ();
2510 Assert.IsInstanceOfType (typeof (AssemblyLoadEvent), e);
2511 Assert.IsTrue ((e as AssemblyLoadEvent).Assembly.Location.EndsWith ("System.dll"));
2513 var frames = e.Thread.GetFrames ();
2514 Assert.IsTrue (frames.Length > 0);
2515 Assert.AreEqual ("assembly_load", frames [0].Method.Name);
2519 public void CreateValue () {
2522 v = vm.CreateValue (1);
2523 Assert.AreEqual (vm, v.VirtualMachine);
2524 Assert.AreEqual (1, v.Value);
2526 v = vm.CreateValue (null);
2527 Assert.AreEqual (vm, v.VirtualMachine);
2528 Assert.AreEqual (null, v.Value);
2530 // Argument checking
2531 AssertThrows <ArgumentException> (delegate () {
2532 v = vm.CreateValue ("FOO");
2537 public void CreateString () {
2538 StringMirror s = vm.RootDomain.CreateString ("ABC");
2540 Assert.AreEqual (vm, s.VirtualMachine);
2541 Assert.AreEqual ("ABC", s.Value);
2542 Assert.AreEqual (vm.RootDomain, s.Domain);
2545 StringBuilder sb = new StringBuilder ();
2546 for (int i = 0; i < 1024; ++i)
2548 s = vm.RootDomain.CreateString (sb.ToString ());
2550 // Argument checking
2551 AssertThrows <ArgumentNullException> (delegate () {
2552 s = vm.RootDomain.CreateString (null);
2557 public void CreateBoxedValue () {
2558 ObjectMirror o = vm.RootDomain.CreateBoxedValue (new PrimitiveValue (vm, 42));
2560 Assert.AreEqual ("Int32", o.Type.Name);
2561 //AssertValue (42, m.GetValue (o.Type.GetField ("m_value")));
2563 // Argument checking
2564 AssertThrows <ArgumentNullException> (delegate () {
2565 vm.RootDomain.CreateBoxedValue (null);
2568 AssertThrows <ArgumentException> (delegate () {
2569 vm.RootDomain.CreateBoxedValue (o);
2574 public void Invoke () {
2575 Event e = run_until ("invoke1");
2577 StackFrame frame = e.Thread.GetFrames () [0];
2579 TypeMirror t = frame.Method.DeclaringType;
2580 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2582 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2588 m = t.GetMethod ("invoke_return_void");
2589 v = this_obj.InvokeMethod (e.Thread, m, null);
2593 m = t.GetMethod ("invoke_return_ref");
2594 v = this_obj.InvokeMethod (e.Thread, m, null);
2595 AssertValue ("ABC", v);
2598 m = t.GetMethod ("invoke_return_null");
2599 v = this_obj.InvokeMethod (e.Thread, m, null);
2600 AssertValue (null, v);
2603 m = t.GetMethod ("invoke_return_primitive");
2604 v = this_obj.InvokeMethod (e.Thread, m, null);
2605 AssertValue (42, v);
2608 m = t.GetMethod ("invoke_return_nullable");
2609 v = this_obj.InvokeMethod (e.Thread, m, null);
2610 Assert.IsInstanceOfType (typeof (StructMirror), v);
2611 var s = v as StructMirror;
2612 AssertValue (42, s.Fields [0]);
2613 AssertValue (true, s.Fields [1]);
2615 // pass nullable as this
2616 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2617 m = s.Type.GetMethod ("ToString");
2618 v = s.InvokeMethod (e.Thread, m, null);
2620 // pass nullable as argument
2621 m = t.GetMethod ("invoke_pass_nullable");
2622 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { s });
2623 AssertValue (42, v);
2625 // return nullable null
2626 m = t.GetMethod ("invoke_return_nullable_null");
2627 v = this_obj.InvokeMethod (e.Thread, m, null);
2628 Assert.IsInstanceOfType (typeof (StructMirror), v);
2629 s = v as StructMirror;
2630 AssertValue (0, s.Fields [0]);
2631 AssertValue (false, s.Fields [1]);
2633 // pass nullable as this
2634 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2635 m = s.Type.GetMethod ("ToString");
2636 v = s.InvokeMethod (e.Thread, m, null);
2638 // pass nullable null as argument
2639 m = t.GetMethod ("invoke_pass_nullable_null");
2640 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { s });
2646 m = t.GetMethod ("invoke_pass_primitive");
2647 Value[] args = new Value [] {
2648 vm.CreateValue ((byte)Byte.MaxValue),
2649 vm.CreateValue ((sbyte)SByte.MaxValue),
2650 vm.CreateValue ((short)1),
2651 vm.CreateValue ((ushort)1),
2652 vm.CreateValue ((int)1),
2653 vm.CreateValue ((uint)1),
2654 vm.CreateValue ((long)1),
2655 vm.CreateValue ((ulong)1),
2656 vm.CreateValue ('A'),
2657 vm.CreateValue (true),
2658 vm.CreateValue (3.14f),
2659 vm.CreateValue (3.14) };
2661 v = this_obj.InvokeMethod (e.Thread, m, args);
2662 AssertValue ((int)Byte.MaxValue + (int)SByte.MaxValue + 1 + 1 + 1 + 1 + 1 + 1 + 'A' + 1 + 3 + 3, v);
2665 m = t.GetMethod ("invoke_pass_ref");
2666 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2667 AssertValue ("ABC", v);
2670 m = t.GetMethod ("invoke_pass_ref");
2671 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (null) });
2672 AssertValue (null, v);
2675 m = t.GetMethod ("invoke_static_pass_ref");
2676 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2677 AssertValue ("ABC", v);
2679 // static invoked using ObjectMirror.InvokeMethod
2680 m = t.GetMethod ("invoke_static_pass_ref");
2681 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2682 AssertValue ("ABC", v);
2684 // method which throws an exception
2686 m = t.GetMethod ("invoke_throws");
2687 v = this_obj.InvokeMethod (e.Thread, m, null);
2689 } catch (InvocationException ex) {
2690 Assert.AreEqual ("Exception", ex.Exception.Type.Name);
2694 m = t.GetMethod ("invoke_out");
2695 var out_task = this_obj.InvokeMethodAsyncWithResult (e.Thread, m, new Value [] { vm.CreateValue (1), vm.CreateValue (null) }, InvokeOptions.ReturnOutArgs);
2696 var out_args = out_task.Result.OutArgs;
2697 AssertValue (5, out_args [0]);
2698 Assert.IsTrue (out_args [1] is ArrayMirror);
2699 Assert.AreEqual (10, (out_args [1] as ArrayMirror).Length);
2701 // without ReturnOutArgs flag
2702 out_task = this_obj.InvokeMethodAsyncWithResult (e.Thread, m, new Value [] { vm.CreateValue (1), vm.CreateValue (null) });
2703 out_args = out_task.Result.OutArgs;
2704 Assert.IsNull (out_args);
2707 m = t.GetMethod (".ctor");
2708 v = t.InvokeMethod (e.Thread, m, null);
2709 Assert.IsInstanceOfType (typeof (ObjectMirror), v);
2710 Assert.AreEqual ("Tests", (v as ObjectMirror).Type.Name);
2713 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("ITest2");
2714 m = cl1.GetMethod ("invoke_iface");
2715 v = this_obj.InvokeMethod (e.Thread, m, null);
2716 AssertValue (42, v);
2719 m = t.BaseType.GetMethod ("virtual_method");
2720 v = this_obj.InvokeMethod (e.Thread, m, null, InvokeOptions.Virtual);
2721 AssertValue ("V2", v);
2723 // virtual call on static method
2724 m = t.GetMethod ("invoke_static_pass_ref");
2725 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") }, InvokeOptions.Virtual);
2726 AssertValue ("ABC", v);
2729 m = t.GetMethod ("invoke_pass_ref");
2730 var task = this_obj.InvokeMethodAsync (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2731 AssertValue ("ABC", task.Result);
2734 m = t.GetMethod ("invoke_static_pass_ref");
2735 task = t.InvokeMethodAsync (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2736 AssertValue ("ABC", task.Result);
2738 // Argument checking
2741 AssertThrows<ArgumentNullException> (delegate {
2742 m = t.GetMethod ("invoke_pass_ref");
2743 v = this_obj.InvokeMethod (null, m, new Value [] { vm.CreateValue (null) });
2747 AssertThrows<ArgumentNullException> (delegate {
2748 v = this_obj.InvokeMethod (e.Thread, null, new Value [] { vm.CreateValue (null) });
2751 // invalid number of arguments
2752 m = t.GetMethod ("invoke_pass_ref");
2753 AssertThrows<ArgumentException> (delegate {
2754 v = this_obj.InvokeMethod (e.Thread, m, null);
2757 // invalid type of argument (ref != primitive)
2758 m = t.GetMethod ("invoke_pass_ref");
2759 AssertThrows<ArgumentException> (delegate {
2760 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2763 // invalid type of argument (primitive != primitive)
2764 m = t.GetMethod ("invoke_pass_primitive_2");
2765 AssertThrows<ArgumentException> (delegate {
2766 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2769 // invoking a non-static method as static
2770 m = t.GetMethod ("invoke_pass_ref");
2771 AssertThrows<ArgumentException> (delegate {
2772 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2775 // invoking a method defined in another class
2776 m = t2.GetMethod ("invoke");
2777 AssertThrows<ArgumentException> (delegate {
2778 v = this_obj.InvokeMethod (e.Thread, m, null);
2783 public void InvokeVType () {
2784 Event e = run_until ("invoke1");
2786 StackFrame frame = e.Thread.GetFrames () [0];
2788 var s = frame.GetArgument (1) as StructMirror;
2790 TypeMirror t = s.Type;
2795 // Pass struct as this, receive int
2796 m = t.GetMethod ("invoke_return_int");
2797 v = s.InvokeMethod (e.Thread, m, null);
2798 AssertValue (42, v);
2800 // Pass boxed struct as this
2801 var boxed_this = t.NewInstance () as ObjectMirror;
2802 m = t.GetMethod ("invoke_return_int");
2803 v = boxed_this.InvokeMethod (e.Thread, m, null);
2806 // Pass struct as this, receive intptr
2807 m = t.GetMethod ("invoke_return_intptr");
2808 v = s.InvokeMethod (e.Thread, m, null);
2809 AssertValue (43, v);
2812 m = t.GetMethod ("invoke_static");
2813 v = t.InvokeMethod (e.Thread, m, null);
2816 // Pass generic struct as this
2817 s = frame.GetArgument (2) as StructMirror;
2819 m = t.GetMethod ("invoke_return_int");
2820 v = s.InvokeMethod (e.Thread, m, null);
2821 AssertValue (42, v);
2824 s = frame.GetArgument (1) as StructMirror;
2826 m = t.GetMethods ().First (method => method.Name == ".ctor" && method.GetParameters ().Length == 1);
2827 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2828 AssertValue (1, (v as StructMirror)["i"]);
2830 // Invoke a method which changes state
2831 s = frame.GetArgument (1) as StructMirror;
2833 m = t.GetMethod ("invoke_mutate");
2834 var task = s.InvokeMethodAsyncWithResult (e.Thread, m, null, InvokeOptions.ReturnOutThis);
2835 var out_this = task.Result.OutThis as StructMirror;
2836 AssertValue (5, out_this ["l"]);
2838 // Without the ReturnOutThis flag
2839 s = frame.GetArgument (1) as StructMirror;
2841 m = t.GetMethod ("invoke_mutate");
2842 task = s.InvokeMethodAsyncWithResult (e.Thread, m, null);
2843 out_this = task.Result.OutThis as StructMirror;
2844 Assert.AreEqual (null, out_this);
2847 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("ITest2");
2848 m = cl1.GetMethod ("invoke_iface");
2849 v = s.InvokeMethod (e.Thread, m, null);
2850 AssertValue (42, v);
2853 m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2854 v = s.InvokeMethod (e.Thread, m, null, InvokeOptions.Virtual);
2855 AssertValue ("42", v);
2859 public void BreakpointDuringInvoke () {
2860 Event e = run_until ("invoke1");
2862 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke2");
2863 Assert.IsNotNull (m);
2864 vm.SetBreakpoint (m, 0);
2866 StackFrame frame = e.Thread.GetFrames () [0];
2867 var o = frame.GetThis () as ObjectMirror;
2869 bool failed = false;
2871 bool finished = false;
2872 object wait = new object ();
2874 // Have to invoke in a separate thread as the invoke is suspended until we
2875 // resume after the breakpoint
2876 Thread t = new Thread (delegate () {
2878 o.InvokeMethod (e.Thread, m, null);
2884 Monitor.Pulse (wait);
2890 StackFrame invoke_frame = null;
2893 e = GetNextEvent ();
2894 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
2895 // Check stack trace support and invokes
2896 var frames = e.Thread.GetFrames ();
2897 invoke_frame = frames [0];
2898 Assert.AreEqual ("invoke2", frames [0].Method.Name);
2899 Assert.IsTrue (frames [0].IsDebuggerInvoke);
2900 Assert.AreEqual ("invoke1", frames [1].Method.Name);
2907 Monitor.Wait (wait);
2910 // Check that the invoke frames are no longer valid
2911 AssertThrows<InvalidStackFrameException> (delegate {
2912 invoke_frame.GetThis ();
2915 // Check InvokeOptions.DisableBreakpoints flag
2916 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2920 public void DisabledExceptionDuringInvoke () {
2921 Event e = run_until ("invoke_ex");
2923 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_ex_inner");
2925 StackFrame frame = e.Thread.GetFrames () [0];
2926 var o = frame.GetThis () as ObjectMirror;
2928 var req = vm.CreateExceptionRequest (null);
2931 // Check InvokeOptions.DisableBreakpoints flag
2932 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2938 public void InvokeSingleThreaded () {
2941 Start (new string [] { "dtest-app.exe", "invoke-single-threaded" });
2943 Event e = run_until ("invoke_single_threaded_2");
2945 StackFrame f = e.Thread.GetFrames ()[0];
2947 var obj = f.GetThis () as ObjectMirror;
2949 // Check that the counter value incremented by the other thread does not increase
2950 // during the invoke.
2951 object counter1 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2953 var m = obj.Type.GetMethod ("invoke_return_void");
2954 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2956 object counter2 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2958 Assert.AreEqual ((int)counter1, (int)counter2);
2960 // Test multiple invokes done in succession
2961 m = obj.Type.GetMethod ("invoke_return_void");
2962 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2964 // Test events during single-threaded invokes
2965 vm.EnableEvents (EventType.TypeLoad);
2966 m = obj.Type.GetMethod ("invoke_type_load");
2967 obj.BeginInvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded, delegate {
2971 e = GetNextEvent ();
2972 Assert.AreEqual (EventType.TypeLoad, e.EventType);
2975 List<Value> invoke_results;
2978 public void InvokeMultiple () {
2979 Event e = run_until ("invoke1");
2981 StackFrame frame = e.Thread.GetFrames () [0];
2983 TypeMirror t = frame.Method.DeclaringType;
2984 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2986 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2988 var methods = new MethodMirror [2];
2989 methods [0] = t.GetMethod ("invoke_return_ref");
2990 methods [1] = t.GetMethod ("invoke_return_primitive");
2992 invoke_results = new List<Value> ();
2994 var r = this_obj.BeginInvokeMultiple (e.Thread, methods, null, InvokeOptions.SingleThreaded, invoke_multiple_cb, this_obj);
2995 WaitHandle.WaitAll (new WaitHandle[] { r.AsyncWaitHandle });
2996 this_obj.EndInvokeMultiple (r);
2997 // The callback might still be running
2998 while (invoke_results.Count < 2) {
3001 if (invoke_results [0] is PrimitiveValue) {
3002 AssertValue ("ABC", invoke_results [1]);
3003 AssertValue (42, invoke_results [0]);
3005 AssertValue ("ABC", invoke_results [0]);
3006 AssertValue (42, invoke_results [1]);
3010 void invoke_multiple_cb (IAsyncResult ar) {
3011 ObjectMirror this_obj = (ObjectMirror)ar.AsyncState;
3013 var res = this_obj.EndInvokeMethod (ar);
3014 lock (invoke_results)
3015 invoke_results.Add (res);
3019 public void InvokeAbort () {
3022 Start (new string [] { "dtest-app.exe", "invoke-abort" });
3024 Event e = run_until ("invoke_abort");
3026 StackFrame f = e.Thread.GetFrames ()[0];
3028 var obj = f.GetThis () as ObjectMirror;
3030 var m = t.GetMethod ("invoke_abort_2");
3031 // Invoke multiple times to check that the subsequent invokes are aborted too
3032 var res = (IInvokeAsyncResult)obj.BeginInvokeMultiple (e.Thread, new MethodMirror[] { m, m, m, m }, null, InvokeOptions.None, delegate { }, null);
3035 AssertThrows<CommandException> (delegate {
3036 obj.EndInvokeMethod (res);
3041 public void GetThreads () {
3046 public void Threads () {
3047 Event e = run_until ("threads");
3049 Assert.AreEqual (ThreadState.Running, e.Thread.ThreadState);
3051 Assert.IsTrue (e.Thread.ThreadId > 0);
3053 Assert.AreEqual (e.Thread.TID, e.Thread.TID);
3055 vm.EnableEvents (EventType.ThreadStart, EventType.ThreadDeath);
3059 e = GetNextEvent ();
3060 Assert.IsInstanceOfType (typeof (ThreadStartEvent), e);
3061 var state = e.Thread.ThreadState;
3062 Assert.IsTrue (state == ThreadState.Running || state == ThreadState.Unstarted);
3066 e = GetNextEvent ();
3067 Assert.IsInstanceOfType (typeof (ThreadDeathEvent), e);
3068 Assert.AreEqual (ThreadState.Stopped, e.Thread.ThreadState);
3072 public void Frame_SetValue () {
3073 Event e = run_until ("locals2");
3075 StackFrame frame = e.Thread.GetFrames () [0];
3078 var l = frame.Method.GetLocal ("i");
3079 frame.SetValue (l, vm.CreateValue ((long)55));
3080 AssertValue (55, frame.GetValue (l));
3083 l = frame.Method.GetLocal ("s");
3084 frame.SetValue (l, vm.RootDomain.CreateString ("DEF"));
3085 AssertValue ("DEF", frame.GetValue (l));
3087 // argument as local
3088 l = frame.Method.GetLocal ("arg");
3089 frame.SetValue (l, vm.CreateValue (6));
3090 AssertValue (6, frame.GetValue (l));
3093 var p = frame.Method.GetParameters ()[1];
3094 frame.SetValue (p, vm.CreateValue (7));
3095 AssertValue (7, frame.GetValue (p));
3098 p = frame.Method.GetParameters ()[2];
3099 frame.SetValue (p, vm.RootDomain.CreateString ("DEF"));
3100 AssertValue ("DEF", frame.GetValue (p));
3103 p = frame.Method.GetParameters ()[3];
3104 frame.SetValue (p, vm.RootDomain.CreateString ("DEF2"));
3105 AssertValue ("DEF2", frame.GetValue (p));
3108 p = frame.Method.GetParameters ()[4];
3109 var v = frame.GetValue (p) as StructMirror;
3110 v ["i"] = vm.CreateValue (43);
3111 frame.SetValue (p, v);
3112 v = frame.GetValue (p) as StructMirror;
3113 AssertValue (43, v ["i"]);
3115 // argument checking
3118 AssertThrows<ArgumentNullException> (delegate () {
3119 frame.SetValue ((LocalVariable)null, vm.CreateValue (55));
3123 AssertThrows<ArgumentNullException> (delegate () {
3124 l = frame.Method.GetLocal ("i");
3125 frame.SetValue (l, null);
3128 // value of invalid type
3129 AssertThrows<ArgumentException> (delegate () {
3130 l = frame.Method.GetLocal ("i");
3131 frame.SetValue (l, vm.CreateValue (55));
3137 public void Frame_SetValue_Registers () {
3138 Event e = run_until ("locals6_1");
3140 StackFrame frame = e.Thread.GetFrames () [1];
3143 var l = frame.Method.GetLocal ("j");
3144 frame.SetValue (l, vm.CreateValue (99));
3145 AssertValue (99, frame.GetValue (l));
3147 // Check it during execution
3148 e = run_until ("locals6_2");
3149 frame = e.Thread.GetFrames () [0];
3150 AssertValue (99, frame.GetValue (frame.Method.GetParameters ()[0]));
3152 // Set it while in a frame which clobbers its register
3153 e = run_until ("locals6_3");
3154 frame = e.Thread.GetFrames () [1];
3155 frame.SetValue (l, vm.CreateValue (100));
3156 AssertValue (100, frame.GetValue (l));
3158 // Check it during execution
3159 e = run_until ("locals6_4");
3160 frame = e.Thread.GetFrames () [0];
3161 AssertValue (100, frame.GetValue (frame.Method.GetParameters ()[0]));
3163 // Signed byte value
3164 e = run_until ("locals6_5");
3165 frame = e.Thread.GetFrames () [1];
3166 var l2 = frame.Method.GetLocal ("sb");
3167 frame.SetValue (l2, vm.CreateValue ((sbyte)-99));
3168 AssertValue (-99, frame.GetValue (l2));
3170 // Check it during execution
3171 e = run_until ("locals6_6");
3172 frame = e.Thread.GetFrames () [0];
3173 AssertValue (-99, frame.GetValue (frame.Method.GetParameters ()[0]));
3177 public void InvokeRegress () {
3178 Event e = run_until ("invoke1");
3180 StackFrame frame = e.Thread.GetFrames () [0];
3182 TypeMirror t = frame.Method.DeclaringType;
3183 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
3185 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
3191 m = t.GetMethod ("invoke_return_void");
3192 v = this_obj.InvokeMethod (e.Thread, m, null);
3195 // Check that the stack frames remain valid during the invoke
3196 Assert.AreEqual ("Tests", (frame.GetThis () as ObjectMirror).Type.Name);
3198 // do another invoke
3199 m = t.GetMethod ("invoke_return_void");
3200 v = this_obj.InvokeMethod (e.Thread, m, null);
3203 // Try a single step after the invoke
3204 var req = create_step (e);
3205 req.Depth = StepDepth.Into;
3206 req.Size = StepSize.Line;
3212 // Step into invoke2
3214 e = GetNextEvent ();
3215 Assert.IsTrue (e is StepEvent);
3216 Assert.AreEqual ("invoke2", (e as StepEvent).Method.Name);
3220 frame = e.Thread.GetFrames () [0];
3224 public void Exceptions () {
3225 Event e = run_until ("exceptions");
3226 var req = vm.CreateExceptionRequest (null);
3231 e = GetNextEvent ();
3232 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3233 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
3235 var frames = e.Thread.GetFrames ();
3236 Assert.AreEqual ("exceptions", frames [0].Method.Name);
3239 // exception type filter
3241 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.ArgumentException"));
3244 // Skip the throwing of the second OverflowException
3247 e = GetNextEvent ();
3248 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3249 Assert.AreEqual ("ArgumentException", (e as ExceptionEvent).Exception.Type.Name);
3252 // exception type filter for subclasses
3253 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.Exception"));
3258 e = GetNextEvent ();
3259 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3260 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
3264 req.IncludeSubclasses = false;
3269 e = GetNextEvent ();
3270 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3271 Assert.AreEqual ("Exception", (e as ExceptionEvent).Exception.Type.Name);
3274 // Implicit exceptions
3275 req = vm.CreateExceptionRequest (null);
3280 e = GetNextEvent ();
3281 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3282 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
3285 // Single stepping after an exception
3286 req = vm.CreateExceptionRequest (null);
3291 e = GetNextEvent ();
3292 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3293 Assert.AreEqual ("Exception", (e as ExceptionEvent).Exception.Type.Name);
3294 frames = e.Thread.GetFrames ();
3295 Assert.AreEqual ("exceptions2", frames [0].Method.Name);
3298 var sreq = create_step (e);
3299 sreq.Depth = StepDepth.Over;
3300 sreq.Size = StepSize.Line;
3304 e = GetNextEvent ();
3305 Assert.IsInstanceOfType (typeof (StepEvent), e);
3306 frames = e.Thread.GetFrames ();
3307 Assert.AreEqual ("exceptions", frames [0].Method.Name);
3310 // Argument checking
3311 AssertThrows<ArgumentException> (delegate {
3312 vm.CreateExceptionRequest (e.Thread.Type);
3317 public void ExceptionFilter () {
3318 Event e = run_until ("exception_filter");
3320 MethodMirror m = entry_point.DeclaringType.GetMethod ("exception_filter_filter");
3321 Assert.IsNotNull (m);
3323 vm.SetBreakpoint (m, 0);
3327 e = GetNextEvent ();
3328 Assert.AreEqual (EventType.Breakpoint, e.EventType);
3329 Assert.IsTrue (e is BreakpointEvent);
3330 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
3332 var frames = e.Thread.GetFrames ();
3334 Assert.IsTrue (frames [0].Location.SourceFile.IndexOf ("dtest-app.cs") != -1);
3335 Assert.AreEqual ("exception_filter_filter", frames [0].Location.Method.Name);
3337 Assert.AreEqual (0, frames [1].Location.Method.MetadataToken);
3338 Assert.AreEqual (0x0f, frames [1].Location.ILOffset);
3340 Assert.AreEqual ("exception_filter_method", frames [2].Location.Method.Name);
3341 Assert.AreEqual (0x06, frames [2].Location.ILOffset);
3343 Assert.AreEqual (0, frames [3].Location.Method.MetadataToken, 0);
3344 Assert.AreEqual (0, frames [3].Location.ILOffset);
3346 Assert.AreEqual ("exception_filter", frames [4].Location.Method.Name);
3350 public void ExceptionFilter2 () {
3353 Start (new string [] { "dtest-excfilter.exe" });
3355 MethodMirror filter_method = entry_point.DeclaringType.GetMethod ("Filter");
3356 Assert.IsNotNull (filter_method);
3358 MethodMirror test_method = entry_point.DeclaringType.GetMethod ("Test");
3359 Assert.IsNotNull (test_method);
3361 vm.SetBreakpoint (filter_method, 0);
3365 var e = GetNextEvent ();
3366 Assert.AreEqual (EventType.Breakpoint, e.EventType);
3367 Assert.IsTrue (e is BreakpointEvent);
3368 Assert.AreEqual (filter_method.Name, (e as BreakpointEvent).Method.Name);
3370 var frames = e.Thread.GetFrames ();
3372 Assert.AreEqual (4, frames.Count ());
3374 Assert.AreEqual (filter_method.Name, frames [0].Location.Method.Name);
3375 Assert.AreEqual (20, frames [0].Location.LineNumber);
3376 Assert.AreEqual (0, frames [0].Location.ILOffset);
3378 Assert.AreEqual (test_method.Name, frames [1].Location.Method.Name);
3379 Assert.AreEqual (37, frames [1].Location.LineNumber);
3380 Assert.AreEqual (0x0b, frames [1].Location.ILOffset);
3382 Assert.AreEqual (test_method.Name, frames [2].Location.Method.Name);
3383 Assert.AreEqual (33, frames [2].Location.LineNumber);
3384 Assert.AreEqual (0x05, frames [2].Location.ILOffset);
3386 Assert.AreEqual (entry_point.Name, frames [3].Location.Method.Name);
3387 Assert.AreEqual (14, frames [3].Location.LineNumber);
3388 Assert.AreEqual (0x00, frames [3].Location.ILOffset);
3396 public void EventSets () {
3398 // Create two filter which both match the same exception
3400 Event e = run_until ("exceptions");
3402 var req = vm.CreateExceptionRequest (null);
3405 var req2 = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.OverflowException"));
3410 var es = vm.GetNextEventSet ();
3411 Assert.AreEqual (2, es.Events.Length);
3414 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3415 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
3418 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3419 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
3426 // Test single threaded invokes during processing of nullref exceptions.
3427 // These won't work if the exception handling is done from the sigsegv signal
3428 // handler, since the sigsegv signal is disabled until control returns from the
3432 [Category ("only3")]
3433 public void NullRefExceptionAndSingleThreadedInvoke () {
3434 Event e = run_until ("exceptions");
3435 var req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.NullReferenceException"));
3440 e = GetNextEvent ();
3441 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3442 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
3444 var ex = (e as ExceptionEvent).Exception;
3445 var tostring_method = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
3446 ex.InvokeMethod (e.Thread, tostring_method, null, InvokeOptions.SingleThreaded);
3450 public void MemberInOtherDomain () {
3453 Start (new string [] { "dtest-app.exe", "domain-test" });
3455 vm.EnableEvents (EventType.AppDomainCreate, EventType.AppDomainUnload, EventType.AssemblyUnload);
3457 Event e = run_until ("domains_print_across");
3459 var frame = e.Thread.GetFrames ()[0];
3460 var inOtherDomain = frame.GetArgument (0) as ObjectMirror;
3461 var crossDomainField = (ObjectMirror) inOtherDomain.GetValue (inOtherDomain.Type.GetField("printMe"));
3462 Assert.AreEqual ("SentinelClass", crossDomainField.Type.Name);
3466 public void Domains () {
3469 Start (new string [] { "dtest-app.exe", "domain-test" });
3471 vm.EnableEvents (EventType.AppDomainCreate, EventType.AppDomainUnload, EventType.AssemblyUnload);
3473 Event e = run_until ("domains");
3477 e = GetNextEvent ();
3478 Assert.IsInstanceOfType (typeof (AppDomainCreateEvent), e);
3480 var domain = (e as AppDomainCreateEvent).Domain;
3482 // Check the object type
3483 e = run_until ("domains_2");
3484 var frame = e.Thread.GetFrames ()[0];
3485 var o = frame.GetArgument (0) as ObjectMirror;
3486 Assert.AreEqual ("CrossDomain", o.Type.Name);
3488 // Do a remoting invoke
3489 var cross_domain_type = o.Type;
3490 var v = o.InvokeMethod (e.Thread, cross_domain_type.GetMethod ("invoke_3"), null);
3491 AssertValue (42, v);
3493 // Run until the callback in the domain
3494 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_in_domain");
3495 Assert.IsNotNull (m);
3496 vm.SetBreakpoint (m, 0);
3500 e = GetNextEvent ();
3501 if (e is BreakpointEvent)
3505 Assert.AreEqual ("invoke_in_domain", (e as BreakpointEvent).Method.Name);
3507 // d_method is from another domain
3508 MethodMirror d_method = (e as BreakpointEvent).Method;
3509 Assert.IsTrue (m != d_method);
3510 Assert.AreEqual (domain, d_method.DeclaringType.Assembly.Domain);
3512 var frames = e.Thread.GetFrames ();
3513 Assert.AreEqual ("invoke_in_domain", frames [0].Method.Name);
3514 Assert.AreEqual (domain, frames [0].Domain);
3515 Assert.AreEqual ("invoke", frames [1].Method.Name);
3516 Assert.AreEqual ("domains", frames [2].Method.Name);
3517 Assert.AreEqual (vm.RootDomain, frames [2].Domain);
3519 // Test breakpoints on already JITted methods in other domains
3520 m = entry_point.DeclaringType.GetMethod ("invoke_in_domain_2");
3521 Assert.IsNotNull (m);
3522 vm.SetBreakpoint (m, 0);
3526 e = GetNextEvent ();
3527 if (e is BreakpointEvent)
3531 Assert.AreEqual ("invoke_in_domain_2", (e as BreakpointEvent).Method.Name);
3533 // This is empty when receiving the AppDomainCreateEvent
3534 Assert.AreEqual ("domain", domain.FriendlyName);
3536 // Run until the unload
3539 e = GetNextEvent ();
3540 if (e is AssemblyUnloadEvent) {
3541 AssertThrows<Exception> (delegate () {
3542 var assembly_obj = (e as AssemblyUnloadEvent).Assembly.GetAssemblyObject ();
3549 Assert.IsInstanceOfType (typeof (AppDomainUnloadEvent), e);
3550 Assert.AreEqual (domain, (e as AppDomainUnloadEvent).Domain);
3552 // Run past the unload
3553 e = run_until ("domains_3");
3555 // Test access to unloaded types
3556 // FIXME: Add an exception type for this
3557 AssertThrows<Exception> (delegate {
3558 d_method.DeclaringType.GetValue (d_method.DeclaringType.GetField ("static_i"));
3561 // Check that .Domain is accessible for stack frames with native transitions
3562 e = run_until ("called_from_invoke");
3563 ThreadMirror.NativeTransitions = true;
3564 foreach (var f in e.Thread.GetFrames ()) {
3570 public void DynamicMethods () {
3571 Event e = run_until ("dyn_call");
3573 var m = e.Thread.GetFrames ()[1].Method;
3574 Assert.AreEqual ("dyn_method", m.Name);
3576 // Test access to IL
3577 var body = m.GetMethodBody ();
3579 ILInstruction ins = body.Instructions [0];
3580 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
3581 Assert.AreEqual ("FOO", ins.Operand);
3585 public void RefEmit () {
3588 Start (new string [] { "dtest-app.exe", "ref-emit-test" });
3590 Event e = run_until ("ref_emit_call");
3592 var m = e.Thread.GetFrames ()[1].Method;
3593 Assert.AreEqual ("ref_emit_method", m.Name);
3595 // Test access to IL
3596 var body = m.GetMethodBody ();
3600 ins = body.Instructions [0];
3601 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
3602 Assert.AreEqual ("FOO", ins.Operand);
3604 ins = body.Instructions [1];
3605 Assert.AreEqual (OpCodes.Call, ins.OpCode);
3606 Assert.IsInstanceOfType (typeof (MethodMirror), ins.Operand);
3607 Assert.AreEqual ("ref_emit_call", (ins.Operand as MethodMirror).Name);
3611 public void IsAttached () {
3612 var f = entry_point.DeclaringType.GetField ("is_attached");
3614 Event e = run_until ("Main");
3616 AssertValue (true, entry_point.DeclaringType.GetValue (f));
3620 public void StackTraceInNative () {
3621 // Check that stack traces can be produced for threads in native code
3624 Start (new string [] { "dtest-app.exe", "frames-in-native" });
3626 var e = run_until ("frames_in_native");
3628 // FIXME: This is racy
3635 StackFrame[] frames = e.Thread.GetFrames ();
3637 int frame_index = -1;
3638 for (int i = 0; i < frames.Length; ++i) {
3639 if (frames [i].Method.Name == "Sleep") {
3645 Assert.IsTrue (frame_index != -1);
3646 Assert.AreEqual ("Sleep", frames [frame_index].Method.Name);
3647 Assert.AreEqual ("frames_in_native", frames [frame_index + 1].Method.Name);
3648 Assert.AreEqual ("Main", frames [frame_index + 2].Method.Name);
3650 // Check that invokes are disabled for such threads
3651 TypeMirror t = frames [frame_index + 1].Method.DeclaringType;
3653 var m = t.GetMethod ("invoke_static_return_void");
3654 AssertThrows<InvalidOperationException> (delegate {
3655 t.InvokeMethod (e.Thread, m, null);
3658 // Check that the frame info is invalidated
3659 run_until ("frames_in_native_2");
3661 AssertThrows<InvalidStackFrameException> (delegate {
3662 Console.WriteLine (frames [frame_index].GetThis ());
3667 public void VirtualMachine_CreateEnumMirror () {
3668 var e = run_until ("o1");
3669 var frame = e.Thread.GetFrames () [0];
3671 object val = frame.GetThis ();
3672 Assert.IsTrue (val is ObjectMirror);
3673 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
3674 ObjectMirror o = (val as ObjectMirror);
3676 FieldInfoMirror field = o.Type.GetField ("field_enum");
3677 Value f = o.GetValue (field);
3678 TypeMirror enumType = (f as EnumMirror).Type;
3680 o.SetValue (field, vm.CreateEnumMirror (enumType, vm.CreateValue (1)));
3681 f = o.GetValue (field);
3682 Assert.AreEqual (1, (f as EnumMirror).Value);
3684 // Argument checking
3685 AssertThrows<ArgumentNullException> (delegate () {
3686 vm.CreateEnumMirror (enumType, null);
3689 AssertThrows<ArgumentNullException> (delegate () {
3690 vm.CreateEnumMirror (null, vm.CreateValue (1));
3694 AssertThrows<ArgumentException> (delegate () {
3695 vm.CreateEnumMirror (enumType, vm.CreateValue (null));
3698 // value of a wrong type
3699 AssertThrows<ArgumentException> (delegate () {
3700 vm.CreateEnumMirror (enumType, vm.CreateValue ((long)1));
3705 public void VirtualMachine_EnableEvents_Breakpoint () {
3706 AssertThrows<ArgumentException> (delegate () {
3707 vm.EnableEvents (EventType.Breakpoint);
3712 public void SingleStepRegress654694 () {
3715 MethodMirror m = entry_point.DeclaringType.GetMethod ("ss_regress_654694");
3716 foreach (Location l in m.Locations) {
3717 if (l.ILOffset > 0 && il_offset == -1)
3718 il_offset = l.ILOffset;
3721 Event e = run_until ("ss_regress_654694");
3723 Assert.IsNotNull (m);
3724 vm.SetBreakpoint (m, il_offset);
3728 e = GetNextEvent ();
3729 Assert.IsTrue (e is BreakpointEvent);
3731 var req = create_step (e);
3732 req.Depth = StepDepth.Over;
3733 req.Size = StepSize.Line;
3738 e = GetNextEvent ();
3739 Assert.IsTrue (e is StepEvent);
3745 public void DebugBreak () {
3746 vm.EnableEvents (EventType.UserBreak);
3751 var e = GetNextEvent ();
3752 Assert.IsTrue (e is UserBreakEvent);
3756 public void DebugLog () {
3757 vm.EnableEvents (EventType.UserLog);
3762 var e = GetNextEvent ();
3763 Assert.IsTrue (e is UserLogEvent);
3764 var le = e as UserLogEvent;
3766 Assert.AreEqual (5, le.Level);
3767 Assert.AreEqual ("A", le.Category);
3768 Assert.AreEqual ("B", le.Message);
3772 public void TypeGetMethodsByNameFlags () {
3774 var assembly = entry_point.DeclaringType.Assembly;
3775 var type = assembly.GetType ("Tests3");
3777 Assert.IsNotNull (type);
3779 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Public, false);
3780 Assert.AreEqual (1, mm.Length, "#1");
3781 Assert.AreEqual ("M1", mm[0].Name, "#2");
3783 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.NonPublic, false);
3784 Assert.AreEqual (1, mm.Length, "#3");
3785 Assert.AreEqual ("M2", mm[0].Name, "#4");
3787 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public, false);
3788 Assert.AreEqual (7, mm.Length, "#5"); //M3 plus Equals, GetHashCode, GetType, ToString, .ctor
3790 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, false);
3791 Assert.AreEqual (2, mm.Length, "#7");
3793 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
3794 Assert.AreEqual (1, mm.Length, "#9");
3796 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
3797 Assert.AreEqual (5, mm.Length, "#11");
3800 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Public, false);
3801 Assert.AreEqual (1, mm.Length, "#12");
3802 Assert.AreEqual ("M1", mm[0].Name, "#13");
3804 mm = type.GetMethodsByNameFlags ("m1", BindingFlags.Static | BindingFlags.Public, true);
3805 Assert.AreEqual (1, mm.Length, "#14");
3806 Assert.AreEqual ("M1", mm[0].Name, "#15");
3808 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, false);
3809 Assert.AreEqual (1, mm.Length, "#16");
3810 Assert.AreEqual ("M1", mm[0].Name, "#17");
3814 [Category ("only88")]
3815 public void TypeLoadSourceFileFilter () {
3816 Event e = run_until ("type_load");
3818 if (!vm.Version.AtLeast (2, 7))
3821 string srcfile = (e as BreakpointEvent).Method.DeclaringType.GetSourceFiles (true)[0];
3822 srcfile = srcfile.Replace ("dtest-app.cs", "TypeLoadClass.cs");
3823 Assert.IsTrue (srcfile.Contains ("TypeLoadClass.cs"));
3825 var req = vm.CreateTypeLoadRequest ();
3826 req.SourceFileFilter = new string [] { srcfile.ToUpper () };
3830 e = GetNextEvent ();
3831 Assert.IsTrue (e is TypeLoadEvent);
3832 Assert.AreEqual ("TypeLoadClass", (e as TypeLoadEvent).Type.FullName);
3836 public void TypeLoadTypeNameFilter () {
3837 Event e = run_until ("type_load");
3839 var req = vm.CreateTypeLoadRequest ();
3840 req.TypeNameFilter = new string [] { "TypeLoadClass2" };
3844 e = GetNextEvent ();
3845 Assert.IsTrue (e is TypeLoadEvent);
3846 Assert.AreEqual ("TypeLoadClass2", (e as TypeLoadEvent).Type.FullName);
3850 public void GetTypesForSourceFile () {
3853 var types = vm.GetTypesForSourceFile ("dtest-app.cs", false);
3854 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
3855 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
3857 types = vm.GetTypesForSourceFile ("DTEST-app.cs", true);
3858 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
3859 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
3863 public void GetTypesNamed () {
3866 var types = vm.GetTypes ("Tests", false);
3867 Assert.AreEqual (1, types.Count);
3868 Assert.AreEqual ("Tests", types [0].FullName);
3870 types = vm.GetTypes ("System.Exception", false);
3871 Assert.AreEqual (1, types.Count);
3872 Assert.AreEqual ("System.Exception", types [0].FullName);
3876 public void String_GetValue () {
3881 var e = run_until ("arg2");
3883 var frame = e.Thread.GetFrames () [0];
3885 val = frame.GetArgument (6);
3886 Assert.AreEqual ('\0'.ToString () + "A", (val as StringMirror).Value);
3890 public void String_GetChars () {
3894 var e = run_until ("arg2");
3896 var frame = e.Thread.GetFrames () [0];
3898 val = frame.GetArgument (0);
3899 Assert.IsTrue (val is StringMirror);
3900 AssertValue ("FOO", val);
3901 var s = (val as StringMirror);
3902 Assert.AreEqual (3, s.Length);
3904 var c = s.GetChars (0, 2);
3905 Assert.AreEqual (2, c.Length);
3906 Assert.AreEqual ('F', c [0]);
3907 Assert.AreEqual ('O', c [1]);
3909 AssertThrows<ArgumentException> (delegate () {
3915 public void GetInterfaces () {
3916 var e = run_until ("arg2");
3918 var frame = e.Thread.GetFrames () [0];
3920 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
3921 var ifaces = cl1.GetInterfaces ();
3922 Assert.AreEqual (1, ifaces.Length);
3923 Assert.AreEqual ("ITest", ifaces [0].Name);
3925 var cl2 = cl1.GetMethod ("Baz").ReturnType;
3926 var ifaces2 = cl2.GetInterfaces ();
3927 Assert.AreEqual (1, ifaces2.Length);
3928 Assert.AreEqual ("ITest`1", ifaces2 [0].Name);
3932 public void GetInterfaceMap () {
3933 var e = run_until ("arg2");
3935 var frame = e.Thread.GetFrames () [0];
3937 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
3938 var iface = cl1.Assembly.GetType ("ITest");
3939 var map = cl1.GetInterfaceMap (iface);
3940 Assert.AreEqual (cl1, map.TargetType);
3941 Assert.AreEqual (iface, map.InterfaceType);
3942 Assert.AreEqual (2, map.InterfaceMethods.Length);
3943 Assert.AreEqual (2, map.TargetMethods.Length);
3947 public void StackAlloc_Breakpoints_Regress2775 () {
3948 // Check that breakpoints on arm don't overwrite stackalloc-ed memory
3949 var e = run_until ("regress_2755");
3951 var frame = e.Thread.GetFrames () [0];
3953 // This breaks at the call site
3954 vm.SetBreakpoint (m, m.Locations [2].ILOffset);
3957 var e2 = GetNextEvent ();
3958 Assert.IsTrue (e2 is BreakpointEvent);
3960 e = run_until ("regress_2755_3");
3961 frame = e.Thread.GetFrames () [1];
3962 var res = frame.GetValue (m.GetLocal ("sum"));
3963 AssertValue (0, res);
3967 public void MethodInfo () {
3968 Event e = run_until ("locals2");
3970 StackFrame frame = e.Thread.GetFrames () [0];
3971 var m = frame.Method;
3973 Assert.IsTrue (m.IsGenericMethod);
3974 Assert.IsFalse (m.IsGenericMethodDefinition);
3976 var args = m.GetGenericArguments ();
3977 Assert.AreEqual (1, args.Length);
3978 Assert.AreEqual ("String", args [0].Name);
3980 var gmd = m.GetGenericMethodDefinition ();
3981 Assert.IsTrue (gmd.IsGenericMethod);
3982 Assert.IsTrue (gmd.IsGenericMethodDefinition);
3983 Assert.AreEqual (gmd, gmd.GetGenericMethodDefinition ());
3985 args = gmd.GetGenericArguments ();
3986 Assert.AreEqual (1, args.Length);
3987 Assert.AreEqual ("T", args [0].Name);
3989 var attrs = m.GetCustomAttributes (true);
3990 Assert.AreEqual (1, attrs.Length);
3991 Assert.AreEqual ("StateMachineAttribute", attrs [0].Constructor.DeclaringType.Name);
3995 public void UnhandledException () {
3998 Start (new string [] { "dtest-app.exe", "unhandled-exception" });
4000 var req = vm.CreateExceptionRequest (null, false, true);
4003 var e = run_until ("unhandled_exception");
4006 var e2 = GetNextEvent ();
4007 Assert.IsTrue (e2 is ExceptionEvent);
4014 public void UnhandledException_2 () {
4017 Start (new string [] { "dtest-app.exe", "unhandled-exception-endinvoke" });
4019 var req = vm.CreateExceptionRequest (null, false, true);
4022 MethodMirror m = entry_point.DeclaringType.GetMethod ("unhandled_exception_endinvoke_2");
4023 Assert.IsNotNull (m);
4024 vm.SetBreakpoint (m, m.ILOffsets [0]);
4026 var e = run_until ("unhandled_exception_endinvoke");
4029 var e2 = GetNextEvent ();
4030 Assert.IsFalse (e2 is ExceptionEvent);
4037 public void UnhandledExceptionUserCode () {
4040 // Exceptions caught in non-user code are treated as unhandled
4041 Start (new string [] { "dtest-app.exe", "unhandled-exception-user" });
4043 var req = vm.CreateExceptionRequest (null, false, true);
4044 req.AssemblyFilter = new List<AssemblyMirror> () { entry_point.DeclaringType.Assembly };
4047 var e = run_until ("unhandled_exception_user");
4050 var e2 = GetNextEvent ();
4051 Assert.IsTrue (e2 is ExceptionEvent);
4058 public void GCWhileSuspended () {
4059 // Check that objects are kept alive during suspensions
4060 Event e = run_until ("gc_suspend_1");
4062 MethodMirror m = entry_point.DeclaringType.GetMethod ("gc_suspend_invoke");
4064 var o = entry_point.DeclaringType.GetValue (entry_point.DeclaringType.GetField ("gc_suspend_field")) as ObjectMirror;
4065 //Console.WriteLine (o);
4067 StackFrame frame = e.Thread.GetFrames () [0];
4068 TypeMirror t = frame.Method.DeclaringType;
4069 for (int i = 0; i < 10; ++i)
4070 t.InvokeMethod (e.Thread, m, new Value [] { });
4072 // This throws an exception if the object is collected
4073 long addr = o.Address;
4075 var o2 = entry_point.DeclaringType.GetValue (entry_point.DeclaringType.GetField ("gc_suspend_field")) as ObjectMirror;
4080 public void MakeGenericMethod () {
4081 Event e = run_until ("bp1");
4083 var intm = vm.RootDomain.GetCorrespondingType (typeof (int));
4084 var stringm = vm.RootDomain.GetCorrespondingType (typeof (string));
4085 var gm = entry_point.DeclaringType.GetMethod ("generic_method");
4086 var res = gm.MakeGenericMethod (new TypeMirror [] { stringm });
4087 var args = res.GetGenericArguments ();
4088 Assert.AreEqual (1, args.Length);
4089 Assert.AreEqual (stringm, args [0]);
4092 AssertThrows<ArgumentNullException> (delegate {
4093 gm.MakeGenericMethod (null);
4095 AssertThrows<ArgumentNullException> (delegate {
4096 gm.MakeGenericMethod (new TypeMirror [] { null });
4098 AssertThrows<ArgumentException> (delegate {
4099 gm.MakeGenericMethod (new TypeMirror [] { stringm, stringm });
4101 AssertThrows<InvalidOperationException> (delegate {
4102 gm.MakeGenericMethod (new TypeMirror [] { intm });
4104 AssertThrows<InvalidOperationException> (delegate {
4105 entry_point.DeclaringType.GetMethod ("Main").MakeGenericMethod (new TypeMirror [] { intm });
4110 public void InspectThreadSuspenedOnWaitOne () {
4112 Start (true, "dtest-app.exe", "wait-one" );
4114 ThreadMirror.NativeTransitions = true;
4116 var evt = run_until ("wait_one");
4117 Assert.IsNotNull (evt, "#1");
4119 var thread = evt.Thread;
4120 Assert.AreEqual (ThreadState.Running, thread.ThreadState, "#1.1");
4122 var frames = thread.GetFrames ();
4123 Assert.IsNotNull (frames, "#2");
4124 Assert.AreEqual (2, frames.Length, "#3");
4125 Assert.AreEqual ("wait_one", frames [0].Method.Name, "#4");
4126 Assert.AreEqual ("Main", frames [1].Method.Name, "#5");
4130 Thread.Sleep (500); //FIXME this is racy, maybe single step? or something?
4133 Assert.AreEqual (ThreadState.WaitSleepJoin, thread.ThreadState, "#6");
4135 frames = thread.GetFrames ();
4136 Assert.AreEqual (8, frames.Length, "#7");
4137 Assert.AreEqual ("WaitOne_internal", frames [0].Method.Name, "#8.0");
4138 Assert.AreEqual ("WaitOneNative", frames [1].Method.Name, "#8.1");
4139 Assert.AreEqual ("InternalWaitOne", frames [2].Method.Name, "#8.2");
4140 Assert.AreEqual ("WaitOne", frames [3].Method.Name, "#8.3");
4141 Assert.AreEqual ("WaitOne", frames [4].Method.Name, "#8.4");
4142 Assert.AreEqual ("WaitOne", frames [5].Method.Name, "#8.5");
4143 Assert.AreEqual ("wait_one", frames [6].Method.Name, "#8.6");
4144 Assert.AreEqual ("Main", frames [7].Method.Name, "#8.7");
4146 var frame = frames [0];
4147 Assert.IsTrue (frame.IsNativeTransition, "#11.1");
4150 Assert.Fail ("Known limitation - can't get info from m2n frames");
4151 } catch (AbsentInformationException) {}
4154 Assert.IsFalse (frame.IsNativeTransition, "#12.1");
4155 var wait_one_this = frame.GetThis ();
4156 Assert.IsNotNull (wait_one_this, "#12.2");
4159 var locals = frame.GetVisibleVariables ();
4160 Assert.AreEqual (1, locals.Count, "#13.1");
4162 var local_0 = frame.GetValue (locals [0]);
4163 Assert.IsNotNull (local_0, "#13.2");
4165 Assert.AreEqual (wait_one_this, local_0, "#14.2");
4169 public void GetMethodBody () {
4170 var bevent = run_until ("Main");
4172 var m = bevent.Method.DeclaringType.GetMethod ("get_IntProperty");
4173 var body = m.GetMethodBody ();
4174 foreach (var ins in body.Instructions) {
4175 if (ins.OpCode == OpCodes.Ldfld) {
4176 var field = (FieldInfoMirror)ins.Operand;
4177 Assert.AreEqual ("field_i", field.Name);
4183 public void EvaluateMethod () {
4184 var bevent = run_until ("evaluate_method_2");
4186 var m = bevent.Method.DeclaringType.GetMethod ("get_IntProperty");
4188 var this_obj = bevent.Thread.GetFrames ()[0].GetThis ();
4189 var v = m.Evaluate (this_obj, null);
4190 AssertValue (42, v);
4194 public void SetIP () {
4195 var bevent = run_until ("set_ip_1");
4197 var invalid_loc = bevent.Thread.GetFrames ()[0].Location;
4199 var req = create_step (bevent);
4200 var e = step_out ();
4202 var frames = e.Thread.GetFrames ();
4203 var locs = frames [0].Method.Locations;
4205 var next_loc = locs.First (l => (l.LineNumber == frames [0].Location.LineNumber + 3));
4207 e.Thread.SetIP (next_loc);
4209 /* Check that i ++; j = 5; was skipped */
4210 bevent = run_until ("set_ip_2");
4211 var f = bevent.Thread.GetFrames ()[1];
4212 AssertValue (2, f.GetValue (f.Method.GetLocal ("i")));
4213 AssertValue (0, f.GetValue (f.Method.GetLocal ("j")));
4216 AssertThrows<ArgumentNullException> (delegate {
4217 e.Thread.SetIP (null);
4220 AssertThrows<ArgumentException> (delegate {
4221 e.Thread.SetIP (invalid_loc);
4226 public void SetIPSingleStep () {
4227 // Check that single stepping after set-ip steps from the new ip
4228 var bevent = run_until ("set_ip_1");
4230 var invalid_loc = bevent.Thread.GetFrames ()[0].Location;
4232 var req = create_step (bevent);
4233 req.Size = StepSize.Line;
4234 var e = step_out ();
4236 var frames = e.Thread.GetFrames ();
4237 var locs = frames [0].Method.Locations;
4238 var prev_loc = locs.First (l => (l.LineNumber == frames [0].Location.LineNumber - 1));
4239 AssertValue (2, frames [0].GetValue (frames [0].Method.GetLocal ("i")));
4241 // Set back the ip to the first i ++; line
4242 e.Thread.SetIP (prev_loc);
4245 var f = e.Thread.GetFrames ()[0];
4246 AssertValue (3, f.GetValue (f.Method.GetLocal ("i")));
4250 public void NewInstanceNoCtor () {
4251 var bevent = run_until ("Main");
4253 var stype = bevent.Method.DeclaringType.Assembly.GetType ("AStruct");
4254 var obj = stype.NewInstance ();
4255 Assert.IsTrue (obj is ObjectMirror);
4256 Assert.AreEqual ("AStruct", (obj as ObjectMirror).Type.Name);
4260 public void StaticCtorFilterInCctor () {
4261 // Check that single stepping when in a cctor only ignores
4262 // other cctors, not the current one
4263 var bevent = run_until ("step_filters");
4265 var assembly = entry_point.DeclaringType.Assembly;
4266 var type = assembly.GetType ("Tests/ClassWithCctor");
4267 var cctor = type.GetMethod (".cctor");
4268 vm.SetBreakpoint (cctor, 0);
4271 var e = GetNextEvent ();
4272 Assert.IsTrue (e is BreakpointEvent);
4274 var req = create_step (e);
4275 req.Filter = StepFilter.StaticCtor;
4277 // Make sure we are still in the cctor
4278 Assert.AreEqual (".cctor", e.Thread.GetFrames ()[0].Location.Method.Name);
4282 public void ThreadpoolIOsinglestep () {
4284 Start ("dtest-app.exe", "threadpool-io");
4285 // This is a regression test for #42625. It tests the
4286 // interaction (particularly in coop GC) of the
4287 // threadpool I/O mechanism and the soft debugger.
4288 Event e = run_until ("threadpool_io");
4289 // run until we sent the task half the bytes it
4290 // expects, so that it blocks waiting for the rest.
4291 e = run_until ("threadpool_bp");
4292 var req = create_step (e);
4293 e = step_out (); // leave threadpool_bp
4294 e = step_out (); // leave threadpool_io
4298 // Uses a fixed port
4299 [Category("NotWorking")]
4300 public void Attach () {
4303 // Launch the app using server=y,suspend=n
4304 var pi = CreateStartInfo (new string[] { "--debugger-agent=transport=dt_socket,address=127.0.0.1:10000,server=y,suspend=n", "dtest-app.exe", "attach" });
4305 var process = Diag.Process.Start (pi);
4307 // Wait for the app to reach the Sleep () in attach ().
4308 Thread.Sleep (1000);
4309 var ep = new IPEndPoint (IPAddress.Loopback, 10000);
4310 vm = VirtualMachineManager.Connect (ep);
4312 var load_req = vm.CreateAssemblyLoadRequest ();
4314 vm.EnableEvents (EventType.TypeLoad);
4316 Event vmstart = GetNextEvent ();
4317 Assert.AreEqual (EventType.VMStart, vmstart.EventType);
4319 // Get collected events
4320 bool assembly_load_found = false;
4321 bool type_load_found = false;
4323 Event e = GetNextEvent ();
4326 if (e is AssemblyLoadEvent) {
4327 var assemblyload = e as AssemblyLoadEvent;
4329 var amirror = assemblyload.Assembly;
4331 if (amirror.GetName ().Name == "System.Transactions") {
4332 assembly_load_found = true;
4333 Assert.AreEqual ("domain", amirror.Domain.FriendlyName);
4336 if (amirror.GetName ().Name == "dtest-app")
4337 // Set a bp so we can break the event loop
4338 vm.SetBreakpoint (amirror.EntryPoint.DeclaringType.GetMethod ("attach_break"), 0);
4340 if (e is TypeLoadEvent) {
4341 var typeload = e as TypeLoadEvent;
4343 if (typeload.Type.Name == "GCSettings") {
4344 type_load_found = true;
4345 Assert.AreEqual ("domain", typeload.Type.Assembly.Domain.FriendlyName);
4349 if (e is BreakpointEvent)
4352 Assert.IsTrue (assembly_load_found);
4353 Assert.IsTrue (type_load_found);