2 using System.Collections.Generic;
3 using System.Threading;
5 using System.Reflection;
8 using Mono.Debugger.Soft;
9 using Diag = System.Diagnostics;
12 using System.Security.Cryptography;
14 using NUnit.Framework;
16 #pragma warning disable 0219
22 public class DebuggerTests
25 MethodMirror entry_point;
26 StepEventRequest step_req;
29 void AssertThrows<ExType> (Action del) where ExType : Exception {
37 Assert.IsTrue (thrown);
40 // No other way to pass arguments to the tests ?
41 public static bool listening = Environment.GetEnvironmentVariable ("DBG_SUSPEND") != null;
42 public static string runtime = Environment.GetEnvironmentVariable ("DBG_RUNTIME");
43 public static string agent_args = Environment.GetEnvironmentVariable ("DBG_AGENT_ARGS");
45 // Not currently used, but can be useful when debugging individual tests.
46 void StackTraceDump (Event e)
49 foreach (var frame in e.Thread.GetFrames ())
52 Console.WriteLine ("Frame " + i + ", " + frame.Method.Name);
56 Event GetNextEvent () {
57 var es = vm.GetNextEventSet ();
58 Assert.AreEqual (1, es.Events.Length);
62 void Start (params string[] args) {
66 void Start (bool forceExit, params string[] args) {
67 this.forceExit = forceExit;
70 var pi = new Diag.ProcessStartInfo ();
72 if (runtime != null) {
73 pi.FileName = runtime;
74 } else if (Path.DirectorySeparatorChar == '\\') {
75 string processExe = Diag.Process.GetCurrentProcess ().MainModule.FileName;
76 if (processExe != null) {
77 string fileName = Path.GetFileName (processExe);
78 if (fileName.StartsWith ("mono") && fileName.EndsWith (".exe"))
79 pi.FileName = processExe;
82 if (string.IsNullOrEmpty (pi.FileName))
84 pi.Arguments = String.Join (" ", args);
85 vm = VirtualMachineManager.Launch (pi, new LaunchOptions { AgentArgs = agent_args });
87 var ep = new IPEndPoint (IPAddress.Any, 10000);
88 Console.WriteLine ("Listening on " + ep + "...");
89 vm = VirtualMachineManager.Listen (ep);
92 var load_req = vm.CreateAssemblyLoadRequest ();
95 Event vmstart = GetNextEvent ();
96 Assert.AreEqual (EventType.VMStart, vmstart.EventType);
105 /* Find out the entry point */
109 if (e is AssemblyLoadEvent) {
110 AssemblyLoadEvent ae = (AssemblyLoadEvent)e;
111 entry_point = ae.Assembly.EntryPoint;
112 if (entry_point != null)
122 BreakpointEvent run_until (string name) {
124 MethodMirror m = entry_point.DeclaringType.GetMethod (name);
125 Assert.IsNotNull (m);
126 //Console.WriteLine ("X: " + name + " " + m.ILOffsets.Count + " " + m.Locations.Count);
127 var req = vm.SetBreakpoint (m, m.ILOffsets [0]);
134 if (e is BreakpointEvent)
140 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
141 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
143 return (e as BreakpointEvent);
146 class ReusableBreakpoint {
148 public string method_name;
149 public BreakpointEventRequest req;
150 public BreakpointEvent lastEvent = null;
151 public ReusableBreakpoint (DebuggerTests owner, string method_name)
154 this.method_name = method_name;
155 MethodMirror m = owner.entry_point.DeclaringType.GetMethod (method_name);
156 Assert.IsNotNull (m);
157 req = owner.vm.SetBreakpoint (m, m.ILOffsets [0]);
160 public void Continue ()
162 bool survived = false;
169 e = owner.GetNextEvent ();
170 if (e is BreakpointEvent)
174 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
175 Assert.AreEqual (method_name, (e as BreakpointEvent).Method.Name);
177 lastEvent = e as BreakpointEvent;
181 if (!survived) { // Ensure cleanup if we triggered an assert
187 public void Disable ()
193 /* One of the tests executes a complex tree of recursive functions.
194 The only good way to specify how its behavior should appear from this side
195 is to just run the function tree once over here and record what it does. */
196 public struct RecursiveChaoticPoint
198 public bool breakpoint;
202 public RecursiveChaoticPoint (bool breakpoint, string name, int depth)
204 this.breakpoint = breakpoint;
210 // The breakpoint is placed here in dtest-app.cs
211 public static void ss_recursive_chaotic_trap (int n, List<RecursiveChaoticPoint> trace, ref bool didLast, ref bool didAny)
213 // Depth is calculated as:
214 // Main + single_stepping + ss_recursive_chaotic + (n is 5 at outermost frame and 0 at innermost frame) + ss_recursive_chaotic_trap
215 trace.Add (new RecursiveChaoticPoint (true, "ss_recursive_chaotic_trap", 5 - n + 5));
219 public static void ss_recursive_chaotic_at (string at, int n, List<RecursiveChaoticPoint> trace, ref bool didLast, ref bool didAny)
221 // This will be called after every return from a function. The other function will return whether "step out" is currently active, and it will be passed in here as didLast.
223 // Depth is calculated as:
224 // Main + single_stepping + ss_recursive_chaotic + (n is 5 at outermost frame and 0 at innermost frame)
225 trace.Add (new RecursiveChaoticPoint (false, "ss_recursive_chaotic_" + at, 5 - n + 4));
231 public static bool ss_recursive_chaotic_fizz (int n, List<RecursiveChaoticPoint> trace)
233 bool didLast = false, didAny = false;
236 didLast = ss_recursive_chaotic_buzz (next, trace);
237 ss_recursive_chaotic_at ("fizz", n, trace, ref didLast, ref didAny);
238 didLast = ss_recursive_chaotic_fizzbuzz (next, trace);
239 ss_recursive_chaotic_at ("fizz", n, trace, ref didLast, ref didAny);
241 ss_recursive_chaotic_trap (n, trace, ref didLast, ref didAny);
242 ss_recursive_chaotic_at ("fizz", n, trace, ref didLast, ref didAny);
247 public static bool ss_recursive_chaotic_buzz (int n, List<RecursiveChaoticPoint> trace)
249 bool didLast = false, didAny = false;
252 didLast = ss_recursive_chaotic_fizz (next, trace);
253 ss_recursive_chaotic_at ("buzz", n, trace, ref didLast, ref didAny);
254 didLast = ss_recursive_chaotic_fizzbuzz (next, trace);
255 ss_recursive_chaotic_at ("buzz", n, trace, ref didLast, ref didAny);
260 public static bool ss_recursive_chaotic_fizzbuzz (int n, List<RecursiveChaoticPoint> trace)
262 bool didLast = false, didAny = false;
265 didLast = ss_recursive_chaotic_fizz (next, trace);
266 ss_recursive_chaotic_at ("fizzbuzz", n, trace, ref didLast, ref didAny);
267 didLast = ss_recursive_chaotic_buzz (next, trace);
268 ss_recursive_chaotic_at ("fizzbuzz", n, trace, ref didLast, ref didAny);
269 didLast = ss_recursive_chaotic_fizzbuzz (next, trace);
270 ss_recursive_chaotic_at ("fizzbuzz", n, trace, ref didLast, ref didAny);
275 public static void trace_ss_recursive_chaotic (List<RecursiveChaoticPoint> trace)
277 ss_recursive_chaotic_fizz (5, trace);
280 Event single_step (ThreadMirror t) {
281 var req = vm.CreateStepRequest (t);
285 Event e = GetNextEvent ();
286 Assert.IsTrue (e is StepEvent);
293 Event step_until (ThreadMirror t, string method_name) {
297 if ((e as StepEvent).Method.Name == method_name)
303 void check_arg_val (StackFrame frame, int pos, Type type, object eval) {
304 object val = frame.GetArgument (pos);
305 Assert.IsTrue (val is PrimitiveValue);
306 object v = (val as PrimitiveValue).Value;
307 Assert.AreEqual (type, v.GetType ());
309 Assert.IsTrue (Math.Abs ((float)eval - (float)v) < 0.0001);
310 else if (eval is double)
311 Assert.IsTrue (Math.Abs ((double)eval - (double)v) < 0.0001);
313 Assert.AreEqual (eval, v);
316 void AssertValue (object expected, object val) {
317 if (expected is string) {
318 Assert.IsTrue (val is StringMirror);
319 Assert.AreEqual (expected, (val as StringMirror).Value);
320 } else if (val is StructMirror && (val as StructMirror).Type.Name == "IntPtr") {
321 AssertValue (expected, (val as StructMirror).Fields [0]);
323 Assert.IsTrue (val is PrimitiveValue);
324 Assert.AreEqual (expected, (val as PrimitiveValue).Value);
329 public void SetUp () {
330 ThreadMirror.NativeTransitions = false;
331 Start (new string [] { "dtest-app.exe" });
335 public void TearDown () {
339 if (step_req != null)
347 Event e = GetNextEvent ();
349 if (e is VMDeathEvent)
358 public void SimpleBreakpoint () {
361 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp1");
362 Assert.IsNotNull (m);
364 vm.SetBreakpoint (m, 0);
369 Assert.AreEqual (EventType.Breakpoint, e.EventType);
370 Assert.IsTrue (e is BreakpointEvent);
371 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
374 AssertThrows<ArgumentException> (delegate {
376 vm.SetBreakpoint (m, 2);
381 public void BreakpointsSameLocation () {
382 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp2");
383 Assert.IsNotNull (m);
385 vm.SetBreakpoint (m, 0);
386 vm.SetBreakpoint (m, 0);
390 var es = vm.GetNextEventSet ();
391 Assert.AreEqual (2, es.Events.Length);
392 Assert.IsTrue (es [0] is BreakpointEvent);
393 Assert.AreEqual (m, (es [0] as BreakpointEvent).Method);
395 Assert.IsTrue (es [1] is BreakpointEvent);
396 Assert.AreEqual (m.Name, (es [1] as BreakpointEvent).Method.Name);
400 public void BreakpointAlreadyJITted () {
401 Event e = run_until ("bp1");
403 /* Place a breakpoint on bp3 */
404 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp3");
405 Assert.IsNotNull (m);
406 vm.SetBreakpoint (m, 0);
408 /* Same with generic instances */
409 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp7");
410 Assert.IsNotNull (m2);
411 vm.SetBreakpoint (m2, 0);
416 Assert.AreEqual (EventType.Breakpoint, e.EventType);
417 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
421 /* Non-shared instance */
423 Assert.AreEqual (EventType.Breakpoint, e.EventType);
424 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
428 /* Shared instance */
430 Assert.AreEqual (EventType.Breakpoint, e.EventType);
431 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
435 public void ClearBreakpoint () {
438 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
439 Assert.IsNotNull (m);
440 EventRequest req1 = vm.SetBreakpoint (m, 0);
441 EventRequest req2 = vm.SetBreakpoint (m, 0);
443 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
444 Assert.IsNotNull (m2);
445 vm.SetBreakpoint (m2, 0);
450 var es = vm.GetNextEventSet ();
451 Assert.AreEqual (2, es.Events.Length);
452 Assert.AreEqual (EventType.Breakpoint, es [0].EventType);
453 Assert.AreEqual (m.Name, (es [0] as BreakpointEvent).Method.Name);
454 Assert.AreEqual (EventType.Breakpoint, es [1].EventType);
455 Assert.AreEqual (m.Name, (es [1] as BreakpointEvent).Method.Name);
457 /* Clear one of them */
463 Assert.AreEqual (EventType.Breakpoint, e.EventType);
464 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
466 /* Clear the other */
472 Assert.AreEqual (EventType.Breakpoint, e.EventType);
473 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
477 public void ClearAllBreakpoints () {
480 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
481 Assert.IsNotNull (m);
482 vm.SetBreakpoint (m, 0);
484 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
485 Assert.IsNotNull (m2);
486 vm.SetBreakpoint (m2, 0);
488 vm.ClearAllBreakpoints ();
493 Assert.IsTrue (!(e is BreakpointEvent));
494 if (e is VMDeathEvent)
499 public void BreakpointOnGShared () {
502 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp6");
503 Assert.IsNotNull (m);
505 vm.SetBreakpoint (m, 0);
510 Assert.AreEqual (EventType.Breakpoint, e.EventType);
511 Assert.IsTrue (e is BreakpointEvent);
512 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
514 // Breakpoint on an open generic method of a closed generic class (#3422)
515 var frame = e.Thread.GetFrames ()[0];
516 var ginst = frame.GetValue (frame.Method.GetLocal ("gc"));
517 var m2 = (ginst as ObjectMirror).Type.GetMethod ("bp");
518 vm.SetBreakpoint (m2, 0);
523 Assert.AreEqual (EventType.Breakpoint, e.EventType);
524 Assert.IsTrue (e is BreakpointEvent);
525 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
528 // Assert we have stepped to a location
529 void assert_location (Event e, string method) {
530 Assert.IsTrue (e is StepEvent);
531 Assert.AreEqual (method, (e as StepEvent).Method.Name);
534 // Assert we have breakpointed at a location
535 void assert_location_at_breakpoint (Event e, string method) {
536 Assert.IsTrue (e is BreakpointEvent);
537 Assert.AreEqual (method, (e as BreakpointEvent).Method.Name);
540 // Assert we have stepped to or breakpointed at a location
541 void assert_location_allow_breakpoint (Event e, string method) {
543 Assert.AreEqual (method, (e as StepEvent).Method.Name);
544 else if (e is BreakpointEvent)
545 Assert.AreEqual (method, (e as BreakpointEvent).Method.Name);
547 Assert.Fail ("Neither step nor breakpoint event");
550 StepEventRequest create_step (Event e) {
551 var req = vm.CreateStepRequest (e.Thread);
557 public void ClassLocalReflection () {
558 MethodMirror m = entry_point.DeclaringType.Assembly.GetType ("LocalReflectClass").GetMethod ("RunMe");
560 Assert.IsNotNull (m);
562 // foreach (var x in m.Locations) {
563 // Console.WriteLine (x);
567 int method_base_linum = m.Locations [0].LineNumber;
568 foreach (var location in m.Locations)
569 if (location.LineNumber == method_base_linum + 2) {
570 offset = location.ILOffset;
574 var req = vm.SetBreakpoint (m, offset);
581 if (e is BreakpointEvent)
587 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
588 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
590 e = single_step (e.Thread);
592 var frame = e.Thread.GetFrames ()[0];
594 Assert.IsNotNull (frame);
595 var field = frame.Method.GetLocal ("reflectMe");
596 Assert.IsNotNull (field);
597 Value variable = frame.GetValue (field);
599 ObjectMirror thisObj = (ObjectMirror)variable;
600 TypeMirror thisType = thisObj.Type;
601 FieldInfoMirror thisFi = null;
602 foreach (var fi in thisType.GetFields ())
603 if (fi.Name == "someField")
606 var gotVal = thisObj.GetValue (thisFi);
607 // If we got this far, we're good.
611 public void SingleStepping () {
612 Event e = run_until ("single_stepping");
614 var req = create_step (e);
617 // Step over 'bool b = true'
619 assert_location (e, "single_stepping");
626 assert_location (e, "ss1");
633 assert_location (e, "single_stepping");
637 assert_location (e, "single_stepping");
641 assert_location (e, "ss3");
643 // Step back into single_stepping
645 assert_location (e, "single_stepping");
647 // Step into next line
649 assert_location (e, "single_stepping");
651 // Step into ss3_2 ()
653 assert_location (e, "ss3_2");
655 // Step over ss3_2_2 ()
657 assert_location (e, "ss3_2");
659 // Recreate the request
666 // Step back into single_stepping () with the new request
668 assert_location (e, "single_stepping");
672 assert_location (e, "ss4");
677 // Change to StepSize.Line
679 req.Depth = StepDepth.Over;
680 req.Size = StepSize.Line;
683 // Step over ss1 (); ss1 ();
688 req.Depth = StepDepth.Into;
692 assert_location (e, "ss2");
697 e = run_until ("ss5");
699 // Add an assembly filter
700 req.AssemblyFilter = new AssemblyMirror [] { (e as BreakpointEvent).Method.DeclaringType.Assembly };
706 // Step into is_even, skipping the linq stuff
708 assert_location (e, "is_even");
710 // FIXME: Check that single stepping works with lock (obj)
714 e = run_until ("ss6");
716 req = create_step (e);
717 req.Depth = StepDepth.Over;
720 // Check that single stepping works in out-of-line bblocks
723 assert_location (e, "ss6");
726 // Check that a step over stops at an EH clause
727 e = run_until ("ss7_2");
728 req = create_step (e);
729 req.Depth = StepDepth.Out;
732 assert_location (e, "ss7");
734 req = create_step (e);
735 req.Depth = StepDepth.Over;
738 assert_location (e, "ss7");
741 // Check that stepping stops between nested calls
742 e = run_until ("ss_nested_2");
744 assert_location (e, "ss_nested");
746 assert_location (e, "ss_nested_1");
748 assert_location (e, "ss_nested");
749 // Check that step over steps over nested calls
751 assert_location (e, "ss_nested");
753 assert_location (e, "ss_nested_1");
755 assert_location (e, "ss_nested_1");
757 assert_location (e, "ss_nested");
760 // Check DebuggerStepThrough support
761 e = run_until ("ss_step_through");
762 req = create_step (e);
763 req.Filter = StepFilter.DebuggerStepThrough;
765 // Step through step_through_1 ()
767 assert_location (e, "ss_step_through");
768 // Step through StepThroughClass.step_through_2 ()
770 assert_location (e, "ss_step_through");
772 req.Filter = StepFilter.None;
774 assert_location (e, "step_through_3");
777 // Check DebuggerNonUserCode support
778 e = run_until ("ss_non_user_code");
779 req = create_step (e);
780 req.Filter = StepFilter.DebuggerNonUserCode;
782 // Step through non_user_code_1 ()
784 assert_location (e, "ss_non_user_code");
785 // Step through StepThroughClass.non_user_code_2 ()
787 assert_location (e, "ss_non_user_code");
789 req.Filter = StepFilter.None;
791 assert_location (e, "non_user_code_3");
794 // Check that step-over doesn't stop at inner frames with recursive functions
795 e = run_until ("ss_recursive");
796 req = create_step (e);
800 var f = e.Thread.GetFrames () [0];
801 assert_location (e, "ss_recursive");
802 AssertValue (1, f.GetValue (f.Method.GetLocal ("n")));
805 // Check that step-over stops correctly when inner frames with recursive functions contain breakpoints
806 e = run_until ("ss_recursive2");
807 ReusableBreakpoint breakpoint = new ReusableBreakpoint (this, "ss_recursive2_trap");
809 breakpoint.Continue ();
810 e = breakpoint.lastEvent;
811 req = create_step (e);
812 for (int c = 1; c <= 4; c++) {
813 // The first five times we try to step over this function, the breakpoint will stop us
814 assert_location_at_breakpoint (e, "ss_recursive2_trap");
817 req = create_step (e);
818 req.Size = StepSize.Line;
821 assert_location (e, "ss_recursive2");
823 // Stack should consist of Main + single_stepping + (1 ss_recursive2 frame per loop iteration)
824 Assert.AreEqual (c+2, e.Thread.GetFrames ().Length);
825 e = step_over_or_breakpoint ();
827 // At this point we should have escaped the breakpoints and this will be a normal step stop
828 assert_location (e, "ss_recursive2");
829 Assert.AreEqual (6, e.Thread.GetFrames ().Length);
832 breakpoint.Disable ();
835 // Check that step-out stops correctly when inner frames with recursive functions contain breakpoints
836 e = run_until ("ss_recursive2");
837 breakpoint = new ReusableBreakpoint (this, "ss_recursive2_trap");
839 breakpoint.Continue ();
840 e = breakpoint.lastEvent;
841 req = create_step (e);
842 for (int c = 1; c <= 4; c++) {
843 // The first five times we try to step over this function, the breakpoint will stop us
844 assert_location_at_breakpoint (e, "ss_recursive2_trap");
847 req = create_step (e);
848 req.Size = StepSize.Line;
851 assert_location (e, "ss_recursive2");
853 // Stack should consist of Main + single_stepping + (1 ss_recursive2 frame per loop iteration)
854 Assert.AreEqual (c+2, e.Thread.GetFrames ().Length);
855 e = step_out_or_breakpoint ();
857 for (int c = 3; c >= 1; c--) {
858 assert_location (e, "ss_recursive2");
859 Assert.AreEqual (c + 2, e.Thread.GetFrames ().Length);
865 breakpoint.Disable ();
868 // Test step out with a really complicated call tree
869 List<RecursiveChaoticPoint> trace = new List<RecursiveChaoticPoint>();
870 trace_ss_recursive_chaotic (trace);
871 e = run_until ("ss_recursive_chaotic");
873 breakpoint = new ReusableBreakpoint (this, "ss_recursive_chaotic_trap");
874 breakpoint.Continue ();
875 e = breakpoint.lastEvent;
876 foreach (RecursiveChaoticPoint point in trace)
878 if (point.breakpoint)
879 assert_location_at_breakpoint (e, point.name);
881 assert_location (e, point.name);
882 Assert.AreEqual (point.depth, e.Thread.GetFrames ().Length);
885 req = create_step (e);
886 req.Size = StepSize.Line;
887 e = step_out_or_breakpoint ();
891 breakpoint.Disable ();
894 // Check that single stepping doesn't clobber fp values
895 e = run_until ("ss_fp_clobber");
896 req = create_step (e);
898 f = e.Thread.GetFrames ()[0];
900 if ((e as StepEvent).Method.Name == "ss_fp_clobber_2")
904 f = e.Thread.GetFrames ()[0];
905 AssertValue (7.0, f.GetValue (f.Method.GetParameters ()[0]));
910 public void SingleSteppingNoFrames () {
912 // Test what happens when starting a single step operation on a thread
913 // with no managed frames
915 // Run a delegate on a tp thread
916 var e = run_until ("ss_no_frames_2");
918 var this_type = e.Thread.GetFrames ()[0].Method.DeclaringType;
919 this_type.SetValue (this_type.GetField ("static_i"), vm.CreateValue (56));
921 var thread = e.Thread;
922 var e2 = run_until ("ss_no_frames_3");
923 // The tp thread should be idle now
924 step_req = vm.CreateStepRequest (thread);
925 step_req.Depth = StepDepth.Over;
926 AssertThrows<Exception> (delegate {
932 public void MethodEntryExit () {
933 run_until ("single_stepping");
935 var req1 = vm.CreateMethodEntryRequest ();
936 var req2 = vm.CreateMethodExitRequest ();
942 Event e = GetNextEvent ();
943 Assert.IsTrue (e is MethodEntryEvent);
944 Assert.AreEqual ("ss1", (e as MethodEntryEvent).Method.Name);
948 Assert.IsTrue (e is MethodExitEvent);
949 Assert.AreEqual ("ss1", (e as MethodExitEvent).Method.Name);
956 public void CountFilter () {
957 run_until ("single_stepping");
959 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("ss3");
960 Assert.IsNotNull (m2);
961 vm.SetBreakpoint (m2, 0);
963 var req1 = vm.CreateMethodEntryRequest ();
967 // Enter ss2, ss1 is skipped
969 Event e = GetNextEvent ();
970 Assert.IsTrue (e is MethodEntryEvent);
971 Assert.AreEqual ("ss2", (e as MethodEntryEvent).Method.Name);
973 // Breakpoint on ss3, the entry event is no longer reported
976 Assert.IsTrue (e is BreakpointEvent);
982 public void Arguments () {
985 var e = run_until ("arg1");
987 StackFrame frame = e.Thread.GetFrames () [0];
989 check_arg_val (frame, 0, typeof (sbyte), SByte.MaxValue - 5);
990 check_arg_val (frame, 1, typeof (byte), Byte.MaxValue - 5);
991 check_arg_val (frame, 2, typeof (bool), true);
992 check_arg_val (frame, 3, typeof (short), Int16.MaxValue - 5);
993 check_arg_val (frame, 4, typeof (ushort), UInt16.MaxValue - 5);
994 check_arg_val (frame, 5, typeof (char), 'F');
995 check_arg_val (frame, 6, typeof (int), Int32.MaxValue - 5);
996 check_arg_val (frame, 7, typeof (uint), UInt32.MaxValue - 5);
997 check_arg_val (frame, 8, typeof (long), Int64.MaxValue - 5);
998 check_arg_val (frame, 9, typeof (ulong), UInt64.MaxValue - 5);
999 check_arg_val (frame, 10, typeof (float), 1.2345f);
1000 check_arg_val (frame, 11, typeof (double), 6.78910);
1002 e = run_until ("arg2");
1004 frame = e.Thread.GetFrames () [0];
1007 val = frame.GetArgument (0);
1008 AssertValue ("FOO", val);
1009 Assert.AreEqual ("String", (val as ObjectMirror).Type.Name);
1012 val = frame.GetArgument (1);
1013 AssertValue (null, val);
1016 val = frame.GetArgument (2);
1017 AssertValue ("BLA", val);
1020 val = frame.GetArgument (3);
1021 AssertValue (42, val);
1024 val = frame.GetArgument (4);
1025 Assert.IsTrue (val is ObjectMirror);
1026 Assert.AreEqual ("GClass`1", (val as ObjectMirror).Type.Name);
1029 val = frame.GetArgument (5);
1030 Assert.IsTrue (val is ObjectMirror);
1031 Assert.AreEqual ("Object", (val as ObjectMirror).Type.Name);
1033 // this on static methods
1034 val = frame.GetThis ();
1035 AssertValue (null, val);
1037 e = run_until ("arg3");
1039 frame = e.Thread.GetFrames () [0];
1042 val = frame.GetThis ();
1043 Assert.IsTrue (val is ObjectMirror);
1044 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
1046 // objref in register
1047 val = frame.GetArgument (0);
1048 AssertValue ("BLA", val);
1052 public void Arrays () {
1055 var e = run_until ("o2");
1057 StackFrame frame = e.Thread.GetFrames () [0];
1060 val = frame.GetArgument (0);
1061 Assert.IsTrue (val is ArrayMirror);
1062 ArrayMirror arr = val as ArrayMirror;
1063 Assert.AreEqual (2, arr.Length);
1064 AssertValue ("BAR", arr [0]);
1065 AssertValue ("BAZ", arr [1]);
1067 var vals = arr.GetValues (0, 2);
1068 Assert.AreEqual (2, vals.Count);
1069 AssertValue ("BAR", vals [0]);
1070 AssertValue ("BAZ", vals [1]);
1072 arr [0] = vm.RootDomain.CreateString ("ABC");
1073 AssertValue ("ABC", arr [0]);
1075 arr [0] = vm.CreateValue (null);
1076 AssertValue (null, arr [0]);
1078 arr.SetValues (0, new Value [] { vm.RootDomain.CreateString ("D1"), vm.RootDomain.CreateString ("D2") });
1079 AssertValue ("D1", arr [0]);
1080 AssertValue ("D2", arr [1]);
1083 val = frame.GetArgument (1);
1084 Assert.IsTrue (val is ArrayMirror);
1085 arr = val as ArrayMirror;
1086 Assert.AreEqual (2, arr.Length);
1087 AssertValue (42, arr [0]);
1088 AssertValue (43, arr [1]);
1090 // Argument checking
1091 AssertThrows<IndexOutOfRangeException> (delegate () {
1095 AssertThrows<IndexOutOfRangeException> (delegate () {
1096 val = arr [Int32.MinValue];
1099 AssertThrows<IndexOutOfRangeException> (delegate () {
1100 vals = arr.GetValues (0, 3);
1103 AssertThrows<IndexOutOfRangeException> (delegate () {
1104 arr [2] = vm.CreateValue (null);
1107 AssertThrows<IndexOutOfRangeException> (delegate () {
1108 arr [Int32.MinValue] = vm.CreateValue (null);
1111 AssertThrows<IndexOutOfRangeException> (delegate () {
1112 arr.SetValues (0, new Value [] { null, null, null });
1116 val = frame.GetArgument (2);
1117 Assert.IsTrue (val is ArrayMirror);
1118 arr = val as ArrayMirror;
1119 Assert.AreEqual (2, arr.Rank);
1120 Assert.AreEqual (4, arr.Length);
1121 Assert.AreEqual (2, arr.GetLength (0));
1122 Assert.AreEqual (2, arr.GetLength (1));
1123 Assert.AreEqual (0, arr.GetLowerBound (0));
1124 Assert.AreEqual (0, arr.GetLowerBound (1));
1125 vals = arr.GetValues (0, 4);
1126 AssertValue (1, vals [0]);
1127 AssertValue (2, vals [1]);
1128 AssertValue (3, vals [2]);
1129 AssertValue (4, vals [3]);
1131 val = frame.GetArgument (3);
1132 Assert.IsTrue (val is ArrayMirror);
1133 arr = val as ArrayMirror;
1134 Assert.AreEqual (2, arr.Rank);
1135 Assert.AreEqual (4, arr.Length);
1136 Assert.AreEqual (2, arr.GetLength (0));
1137 Assert.AreEqual (2, arr.GetLength (1));
1138 Assert.AreEqual (1, arr.GetLowerBound (0));
1139 Assert.AreEqual (3, arr.GetLowerBound (1));
1141 AssertThrows<ArgumentOutOfRangeException> (delegate () {
1144 AssertThrows<ArgumentOutOfRangeException> (delegate () {
1148 AssertThrows<ArgumentOutOfRangeException> (delegate () {
1149 arr.GetLowerBound (-1);
1151 AssertThrows<ArgumentOutOfRangeException> (delegate () {
1152 arr.GetLowerBound (2);
1155 // arrays treated as generic collections
1156 val = frame.GetArgument (4);
1157 Assert.IsTrue (val is ArrayMirror);
1158 arr = val as ArrayMirror;
1162 public void Object_GetValue () {
1163 var e = run_until ("o1");
1164 var frame = e.Thread.GetFrames () [0];
1166 object val = frame.GetThis ();
1167 Assert.IsTrue (val is ObjectMirror);
1168 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
1169 ObjectMirror o = (val as ObjectMirror);
1171 TypeMirror t = o.Type;
1174 object f = o.GetValue (t.GetField ("field_i"));
1175 AssertValue (42, f);
1176 f = o.GetValue (t.GetField ("field_s"));
1177 AssertValue ("S", f);
1178 f = o.GetValue (t.GetField ("field_enum"));
1179 Assert.IsTrue (f is EnumMirror);
1180 Assert.AreEqual (1, (f as EnumMirror).Value);
1181 Assert.AreEqual ("B", (f as EnumMirror).StringValue);
1183 // Inherited object fields
1184 TypeMirror parent = t.BaseType;
1185 f = o.GetValue (parent.GetField ("base_field_i"));
1186 AssertValue (43, f);
1187 f = o.GetValue (parent.GetField ("base_field_s"));
1188 AssertValue ("T", f);
1191 f = o.GetValue (o.Type.GetField ("static_i"));
1192 AssertValue (55, f);
1194 // generic instances
1195 ObjectMirror o2 = frame.GetValue (frame.Method.GetParameters ()[1]) as ObjectMirror;
1196 Assert.AreEqual ("GClass`1", o2.Type.Name);
1197 TypeMirror t2 = o2.Type;
1198 f = o2.GetValue (t2.GetField ("field"));
1199 AssertValue (42, f);
1201 ObjectMirror o3 = frame.GetValue (frame.Method.GetParameters ()[2]) as ObjectMirror;
1202 Assert.AreEqual ("GClass`1", o3.Type.Name);
1203 TypeMirror t3 = o3.Type;
1204 f = o3.GetValue (t3.GetField ("field"));
1205 AssertValue ("FOO", f);
1207 // Argument checking
1208 AssertThrows<ArgumentNullException> (delegate () {
1214 public void Object_GetValues () {
1215 var e = run_until ("o1");
1216 var frame = e.Thread.GetFrames () [0];
1218 object val = frame.GetThis ();
1219 Assert.IsTrue (val is ObjectMirror);
1220 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
1221 ObjectMirror o = (val as ObjectMirror);
1223 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1225 TypeMirror t = o.Type;
1227 object[] vals = o.GetValues (new FieldInfoMirror [] { t.GetField ("field_i"), t.GetField ("field_s") });
1228 object f = vals [0];
1229 AssertValue (42, f);
1231 AssertValue ("S", f);
1233 // Argument checking
1234 AssertThrows<ArgumentNullException> (delegate () {
1238 AssertThrows<ArgumentNullException> (delegate () {
1239 o.GetValues (new FieldInfoMirror [] { null });
1242 // field of another class
1243 AssertThrows<ArgumentException> (delegate () {
1244 o.GetValue (val2.Type.GetField ("field_j"));
1248 void TestSetValue (ObjectMirror o, string field_name, object val) {
1250 o.SetValue (o.Type.GetField (field_name), vm.RootDomain.CreateString ((string)val));
1252 o.SetValue (o.Type.GetField (field_name), vm.CreateValue (val));
1253 Value f = o.GetValue (o.Type.GetField (field_name));
1254 AssertValue (val, f);
1258 public void Object_SetValues () {
1259 var e = run_until ("o1");
1260 var frame = e.Thread.GetFrames () [0];
1262 object val = frame.GetThis ();
1263 Assert.IsTrue (val is ObjectMirror);
1264 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
1265 ObjectMirror o = (val as ObjectMirror);
1267 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1269 TestSetValue (o, "field_i", 22);
1270 TestSetValue (o, "field_bool1", false);
1271 TestSetValue (o, "field_bool2", true);
1272 TestSetValue (o, "field_char", 'B');
1273 TestSetValue (o, "field_byte", (byte)129);
1274 TestSetValue (o, "field_sbyte", (sbyte)-33);
1275 TestSetValue (o, "field_short", (short)(Int16.MaxValue - 5));
1276 TestSetValue (o, "field_ushort", (ushort)(UInt16.MaxValue - 5));
1277 TestSetValue (o, "field_long", Int64.MaxValue - 5);
1278 TestSetValue (o, "field_ulong", (ulong)(UInt64.MaxValue - 5));
1279 TestSetValue (o, "field_float", 6.28f);
1280 TestSetValue (o, "field_double", 6.28);
1281 TestSetValue (o, "static_i", 23);
1282 TestSetValue (o, "field_s", "CDEF");
1287 f = o.GetValue (o.Type.GetField ("field_intptr"));
1288 Assert.IsInstanceOfType (typeof (StructMirror), f);
1289 AssertValue (Int32.MaxValue - 5, (f as StructMirror).Fields [0]);
1292 FieldInfoMirror field = o.Type.GetField ("field_enum");
1293 f = o.GetValue (field);
1294 (f as EnumMirror).Value = 5;
1295 o.SetValue (field, f);
1296 f = o.GetValue (field);
1297 Assert.AreEqual (5, (f as EnumMirror).Value);
1300 o.SetValue (o.Type.GetField ("field_s"), vm.CreateValue (null));
1301 f = o.GetValue (o.Type.GetField ("field_s"));
1302 AssertValue (null, f);
1305 field = o.Type.GetField ("generic_field_struct");
1306 f = o.GetValue (field);
1307 o.SetValue (field, f);
1310 field = o.Type.GetField ("field_nullable");
1311 f = o.GetValue (field);
1312 AssertValue (0, (f as StructMirror).Fields [0]);
1313 AssertValue (false, (f as StructMirror).Fields [1]);
1314 o.SetValue (field, vm.CreateValue (6));
1315 f = o.GetValue (field);
1316 AssertValue (6, (f as StructMirror).Fields [0]);
1317 AssertValue (true, (f as StructMirror).Fields [1]);
1318 o.SetValue (field, vm.CreateValue (null));
1319 f = o.GetValue (field);
1320 AssertValue (0, (f as StructMirror).Fields [0]);
1321 AssertValue (false, (f as StructMirror).Fields [1]);
1323 // Argument checking
1324 AssertThrows<ArgumentNullException> (delegate () {
1325 o.SetValues (null, new Value [0]);
1328 AssertThrows<ArgumentNullException> (delegate () {
1329 o.SetValues (new FieldInfoMirror [0], null);
1332 AssertThrows<ArgumentNullException> (delegate () {
1333 o.SetValues (new FieldInfoMirror [] { null }, new Value [1] { null });
1336 // vtype with a wrong type
1337 AssertThrows<ArgumentException> (delegate () {
1338 o.SetValue (o.Type.GetField ("field_struct"), o.GetValue (o.Type.GetField ("field_enum")));
1341 // reference type not assignment compatible
1342 AssertThrows<ArgumentException> (delegate () {
1343 o.SetValue (o.Type.GetField ("field_class"), o);
1346 // field of another class
1347 AssertThrows<ArgumentException> (delegate () {
1348 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
1353 public void Type_SetValue () {
1354 var e = run_until ("o1");
1355 var frame = e.Thread.GetFrames () [0];
1358 object val = frame.GetThis ();
1359 Assert.IsTrue (val is ObjectMirror);
1360 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
1361 ObjectMirror o = (val as ObjectMirror);
1363 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1365 o.Type.SetValue (o.Type.GetField ("static_i"), vm.CreateValue (55));
1366 f = o.Type.GetValue (o.Type.GetField ("static_i"));
1367 AssertValue (55, f);
1369 o.Type.SetValue (o.Type.GetField ("static_s"), vm.RootDomain.CreateString ("B"));
1370 f = o.Type.GetValue (o.Type.GetField ("static_s"));
1371 AssertValue ("B", f);
1373 // Argument checking
1374 AssertThrows<ArgumentNullException> (delegate () {
1375 o.Type.SetValue (null, vm.CreateValue (0));
1378 AssertThrows<ArgumentNullException> (delegate () {
1379 o.Type.SetValue (o.Type.GetField ("static_i"), null);
1382 // field of another class
1383 AssertThrows<ArgumentException> (delegate () {
1384 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
1389 public void TypeInfo () {
1390 Event e = run_until ("ti2");
1391 StackFrame frame = e.Thread.GetFrames () [0];
1396 t = frame.Method.GetParameters ()[0].ParameterType;
1398 Assert.AreEqual ("String[]", t.Name);
1399 Assert.AreEqual ("string[]", t.CSharpName);
1400 Assert.AreEqual ("Array", t.BaseType.Name);
1401 Assert.AreEqual (true, t.HasElementType);
1402 Assert.AreEqual (true, t.IsArray);
1403 Assert.AreEqual (1, t.GetArrayRank ());
1404 Assert.AreEqual ("String", t.GetElementType ().Name);
1406 t = frame.Method.GetParameters ()[2].ParameterType;
1408 Assert.AreEqual ("Int32[,]", t.Name);
1410 //Assert.AreEqual ("int[,]", t.CSharpName);
1411 Assert.AreEqual ("Array", t.BaseType.Name);
1412 Assert.AreEqual (true, t.HasElementType);
1413 Assert.AreEqual (true, t.IsArray);
1414 Assert.AreEqual (2, t.GetArrayRank ());
1415 Assert.AreEqual ("Int32", t.GetElementType ().Name);
1418 t = frame.Method.GetParameters ()[3].ParameterType;
1420 //Assert.AreEqual ("Int32&", t.Name);
1421 //Assert.AreEqual (true, t.IsByRef);
1422 //Assert.AreEqual (true, t.HasElementType);
1425 t = frame.Method.GetParameters ()[4].ParameterType;
1427 //Assert.AreEqual ("Int32*", t.Name);
1428 Assert.AreEqual (true, t.IsPointer);
1429 Assert.AreEqual (true, t.HasElementType);
1430 Assert.AreEqual ("Int32", t.GetElementType ().Name);
1431 Assert.AreEqual (false, t.IsPrimitive);
1434 t = frame.Method.GetParameters ()[5].ParameterType;
1435 Assert.AreEqual (true, t.IsPrimitive);
1438 t = frame.Method.GetParameters ()[6].ParameterType;
1439 Assert.AreEqual ("AStruct", t.Name);
1440 Assert.AreEqual (false, t.IsPrimitive);
1441 Assert.AreEqual (true, t.IsValueType);
1442 Assert.AreEqual (false, t.IsClass);
1445 t = frame.Method.GetParameters ()[7].ParameterType;
1446 Assert.AreEqual ("Tests", t.Name);
1447 var nested = (from nt in t.GetNestedTypes () where nt.IsNestedPublic select nt).ToArray ();
1448 Assert.AreEqual (1, nested.Length);
1449 Assert.AreEqual ("NestedClass", nested [0].Name);
1450 Assert.IsTrue (t.BaseType.IsAssignableFrom (t));
1451 Assert.IsTrue (!t.IsAssignableFrom (t.BaseType));
1453 // generic instances
1454 t = frame.Method.GetParameters ()[9].ParameterType;
1455 Assert.AreEqual ("GClass`1", t.Name);
1456 Assert.IsTrue (t.IsGenericType);
1457 Assert.IsFalse (t.IsGenericTypeDefinition);
1459 var args = t.GetGenericArguments ();
1460 Assert.AreEqual (1, args.Length);
1461 Assert.AreEqual ("Int32", args [0].Name);
1463 // generic type definitions
1464 var gtd = t.GetGenericTypeDefinition ();
1465 Assert.AreEqual ("GClass`1", gtd.Name);
1466 Assert.IsTrue (gtd.IsGenericType);
1467 Assert.IsTrue (gtd.IsGenericTypeDefinition);
1468 Assert.AreEqual (gtd, gtd.GetGenericTypeDefinition ());
1470 args = gtd.GetGenericArguments ();
1471 Assert.AreEqual (1, args.Length);
1472 Assert.AreEqual ("T", args [0].Name);
1475 t = frame.Method.GetParameters ()[10].ParameterType;
1476 Assert.AreEqual ("AnEnum", t.Name);
1477 Assert.IsTrue (t.IsEnum);
1478 Assert.AreEqual ("Int32", t.EnumUnderlyingType.Name);
1481 t = frame.Method.GetParameters ()[11].ParameterType;
1482 Assert.AreEqual ("TypedReference", t.Name);
1485 t = frame.Method.GetParameters ()[7].ParameterType;
1487 var props = t.GetProperties ();
1488 Assert.AreEqual (3, props.Length);
1489 foreach (PropertyInfoMirror prop in props) {
1490 ParameterInfoMirror[] indexes = prop.GetIndexParameters ();
1492 if (prop.Name == "IntProperty") {
1493 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1494 Assert.AreEqual ("get_IntProperty", prop.GetGetMethod ().Name);
1495 Assert.AreEqual ("set_IntProperty", prop.GetSetMethod ().Name);
1496 Assert.AreEqual (0, indexes.Length);
1497 } else if (prop.Name == "ReadOnlyProperty") {
1498 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1499 Assert.AreEqual ("get_ReadOnlyProperty", prop.GetGetMethod ().Name);
1500 Assert.AreEqual (null, prop.GetSetMethod ());
1501 Assert.AreEqual (0, indexes.Length);
1502 } else if (prop.Name == "IndexedProperty") {
1503 Assert.AreEqual (1, indexes.Length);
1504 Assert.AreEqual ("Int32", indexes [0].ParameterType.Name);
1508 // custom attributes
1509 t = frame.Method.GetParameters ()[8].ParameterType;
1510 Assert.AreEqual ("Tests2", t.Name);
1511 var attrs = t.GetCustomAttributes (true);
1512 Assert.AreEqual (5, attrs.Length);
1513 foreach (var attr in attrs) {
1514 if (attr.Constructor.DeclaringType.Name == "DebuggerDisplayAttribute") {
1515 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1516 Assert.AreEqual ("Tests", attr.ConstructorArguments [0].Value);
1517 Assert.AreEqual (2, attr.NamedArguments.Count);
1518 Assert.AreEqual ("Name", attr.NamedArguments [0].Property.Name);
1519 Assert.AreEqual ("FOO", attr.NamedArguments [0].TypedValue.Value);
1520 Assert.AreEqual ("Target", attr.NamedArguments [1].Property.Name);
1521 Assert.IsInstanceOfType (typeof (TypeMirror), attr.NamedArguments [1].TypedValue.Value);
1522 Assert.AreEqual ("Int32", (attr.NamedArguments [1].TypedValue.Value as TypeMirror).Name);
1523 } else if (attr.Constructor.DeclaringType.Name == "DebuggerTypeProxyAttribute") {
1524 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1525 Assert.IsInstanceOfType (typeof (TypeMirror), attr.ConstructorArguments [0].Value);
1526 Assert.AreEqual ("Tests", (attr.ConstructorArguments [0].Value as TypeMirror).Name);
1527 } else if (attr.Constructor.DeclaringType.Name == "BAttribute") {
1528 Assert.AreEqual (2, attr.NamedArguments.Count);
1529 Assert.AreEqual ("afield", attr.NamedArguments [0].Field.Name);
1530 Assert.AreEqual ("bfield", attr.NamedArguments [1].Field.Name);
1531 } else if (attr.Constructor.DeclaringType.Name == "ClassInterfaceAttribute") {
1532 // inherited from System.Object
1533 //} else if (attr.Constructor.DeclaringType.Name == "Serializable") {
1534 // inherited from System.Object
1535 } else if (attr.Constructor.DeclaringType.Name == "ComVisibleAttribute") {
1536 // inherited from System.Object
1538 Assert.Fail (attr.Constructor.DeclaringType.Name);
1542 var assembly = entry_point.DeclaringType.Assembly;
1543 var type = assembly.GetType ("Tests4");
1544 Assert.IsFalse (type.IsInitialized);
1548 public void FieldInfo () {
1549 Event e = run_until ("ti2");
1550 StackFrame frame = e.Thread.GetFrames () [0];
1554 t = frame.Method.GetParameters ()[8].ParameterType;
1555 Assert.AreEqual ("Tests2", t.Name);
1557 var fi = t.GetField ("field_j");
1558 var attrs = fi.GetCustomAttributes (true);
1559 Assert.AreEqual (1, attrs.Length);
1560 var attr = attrs [0];
1561 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1562 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1563 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1564 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1568 public void PropertyInfo () {
1569 Event e = run_until ("ti2");
1570 StackFrame frame = e.Thread.GetFrames () [0];
1574 t = frame.Method.GetParameters ()[8].ParameterType;
1575 Assert.AreEqual ("Tests2", t.Name);
1577 var pi = t.GetProperty ("AProperty");
1578 var attrs = pi.GetCustomAttributes (true);
1579 Assert.AreEqual (1, attrs.Length);
1580 var attr = attrs [0];
1581 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1582 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1583 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1584 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1588 [Category ("only5")]
1589 public void Type_GetValue () {
1590 Event e = run_until ("o1");
1591 StackFrame frame = e.Thread.GetFrames () [0];
1593 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1595 TypeMirror t = o.Type;
1597 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1600 object f = t.GetValue (o.Type.GetField ("static_i"));
1601 AssertValue (55, f);
1603 f = t.GetValue (o.Type.GetField ("static_s"));
1604 AssertValue ("A", f);
1606 // literal static fields
1607 f = t.GetValue (o.Type.GetField ("literal_i"));
1608 AssertValue (56, f);
1610 f = t.GetValue (o.Type.GetField ("literal_s"));
1611 AssertValue ("B", f);
1613 // Inherited static fields
1614 TypeMirror parent = t.BaseType;
1615 f = t.GetValue (parent.GetField ("base_static_i"));
1616 AssertValue (57, f);
1618 f = t.GetValue (parent.GetField ("base_static_s"));
1619 AssertValue ("C", f);
1621 // thread static field
1622 f = t.GetValue (t.GetField ("tls_i"), e.Thread);
1623 AssertValue (42, f);
1625 // Argument checking
1626 AssertThrows<ArgumentNullException> (delegate () {
1631 AssertThrows<ArgumentException> (delegate () {
1632 t.GetValue (o.Type.GetField ("field_i"));
1635 // field on another type
1636 AssertThrows<ArgumentException> (delegate () {
1637 t.GetValue (val2.Type.GetField ("static_field_j"));
1640 // special static field
1641 AssertThrows<ArgumentException> (delegate () {
1642 t.GetValue (t.GetField ("tls_i"));
1647 public void Type_GetValues () {
1648 Event e = run_until ("o1");
1649 StackFrame frame = e.Thread.GetFrames () [0];
1651 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1653 TypeMirror t = o.Type;
1656 object[] vals = t.GetValues (new FieldInfoMirror [] { t.GetField ("static_i"), t.GetField ("static_s") });
1657 object f = vals [0];
1658 AssertValue (55, f);
1661 AssertValue ("A", f);
1663 // Argument checking
1664 AssertThrows<ArgumentNullException> (delegate () {
1668 AssertThrows<ArgumentNullException> (delegate () {
1669 t.GetValues (new FieldInfoMirror [] { null });
1674 public void ObjRefs () {
1675 Event e = run_until ("objrefs1");
1676 StackFrame frame = e.Thread.GetFrames () [0];
1678 ObjectMirror o = frame.GetThis () as ObjectMirror;
1679 ObjectMirror child = o.GetValue (o.Type.GetField ("child")) as ObjectMirror;
1681 Assert.IsTrue (child.Address != 0);
1683 // Check that object references are internalized correctly
1684 Assert.AreEqual (o, frame.GetThis ());
1686 run_until ("objrefs2");
1688 // child should be gc'd now
1689 // This is not deterministic
1690 //Assert.IsTrue (child.IsCollected);
1693 * No longer works since Type is read eagerly
1696 AssertThrows<ObjectCollectedException> (delegate () {
1697 TypeMirror t = child.Type;
1701 AssertThrows<ObjectCollectedException> (delegate () {
1702 long addr = child.Address;
1708 public void Type_GetObject () {
1709 Event e = run_until ("o1");
1710 StackFrame frame = e.Thread.GetFrames () [0];
1712 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1714 TypeMirror t = o.Type;
1716 Assert.AreEqual ("RuntimeType", t.GetTypeObject ().Type.Name);
1720 public void VTypes () {
1721 Event e = run_until ("vtypes1");
1722 StackFrame frame = e.Thread.GetFrames () [0];
1725 ObjectMirror o = frame.GetThis () as ObjectMirror;
1726 var obj = o.GetValue (o.Type.GetField ("field_struct"));
1727 Assert.IsTrue (obj is StructMirror);
1728 var s = obj as StructMirror;
1729 Assert.AreEqual ("AStruct", s.Type.Name);
1730 AssertValue (42, s ["i"]);
1732 AssertValue ("S", obj);
1733 AssertValue (43, s ["k"]);
1734 obj = o.GetValue (o.Type.GetField ("field_boxed_struct"));
1735 Assert.IsTrue (obj is StructMirror);
1736 s = obj as StructMirror;
1737 Assert.AreEqual ("AStruct", s.Type.Name);
1738 AssertValue (42, s ["i"]);
1740 // Check decoding of nested structs (#14942)
1741 obj = o.GetValue (o.Type.GetField ("nested_struct"));
1742 o.SetValue (o.Type.GetField ("nested_struct"), obj);
1744 // Check round tripping of boxed struct fields (#12354)
1745 obj = o.GetValue (o.Type.GetField ("boxed_struct_field"));
1746 o.SetValue (o.Type.GetField ("boxed_struct_field"), obj);
1747 obj = o.GetValue (o.Type.GetField ("boxed_struct_field"));
1748 s = obj as StructMirror;
1749 AssertValue (1, s ["key"]);
1751 Assert.IsTrue (obj is StructMirror);
1752 s = obj as StructMirror;
1753 AssertValue (42, s ["m_value"]);
1755 // vtypes as arguments
1756 s = frame.GetArgument (0) as StructMirror;
1757 AssertValue (44, s ["i"]);
1759 AssertValue ("T", obj);
1760 AssertValue (45, s ["k"]);
1762 // vtypes as array entries
1763 var arr = frame.GetArgument (1) as ArrayMirror;
1765 Assert.IsTrue (obj is StructMirror);
1766 s = obj as StructMirror;
1767 AssertValue (1, s ["i"]);
1768 AssertValue ("S1", s ["s"]);
1770 Assert.IsTrue (obj is StructMirror);
1771 s = obj as StructMirror;
1772 AssertValue (2, s ["i"]);
1773 AssertValue ("S2", s ["s"]);
1776 var typedref = frame.GetArgument (2) as StructMirror;
1777 Assert.IsTrue (typedref is StructMirror);
1779 // Argument checking
1780 s = frame.GetArgument (0) as StructMirror;
1781 AssertThrows<ArgumentException> (delegate () {
1785 // generic vtype instances
1786 o = frame.GetThis () as ObjectMirror;
1787 obj = o.GetValue (o.Type.GetField ("generic_field_struct"));
1788 Assert.IsTrue (obj is StructMirror);
1789 s = obj as StructMirror;
1790 Assert.AreEqual ("GStruct`1", s.Type.Name);
1791 AssertValue (42, s ["i"]);
1793 // this on vtype methods
1794 e = run_until ("vtypes2");
1795 e = step_until (e.Thread, "foo");
1797 frame = e.Thread.GetFrames () [0];
1799 Assert.AreEqual ("foo", (e as StepEvent).Method.Name);
1800 obj = frame.GetThis ();
1802 Assert.IsTrue (obj is StructMirror);
1803 s = obj as StructMirror;
1804 AssertValue (44, s ["i"]);
1805 AssertValue ("T", s ["s"]);
1806 AssertValue (45, s ["k"]);
1809 s ["i"] = vm.CreateValue (55);
1811 obj = frame.GetThis ();
1812 Assert.IsTrue (obj is StructMirror);
1813 s = obj as StructMirror;
1814 AssertValue (55, s ["i"]);
1815 AssertValue ("T", s ["s"]);
1816 AssertValue (45, s ["k"]);
1818 // this on static vtype methods
1819 e = run_until ("vtypes3");
1820 e = step_until (e.Thread, "static_foo");
1822 frame = e.Thread.GetFrames () [0];
1824 Assert.AreEqual ("static_foo", (e as StepEvent).Method.Name);
1825 obj = frame.GetThis ();
1826 AssertValue (null, obj);
1828 // vtypes which reference themselves recursively
1829 e = run_until ("vtypes4_2");
1830 frame = e.Thread.GetFrames () [0];
1832 Assert.IsTrue (frame.GetArgument (0) is StructMirror);
1836 public void AssemblyInfo () {
1837 Event e = run_until ("single_stepping");
1839 StackFrame frame = e.Thread.GetFrames () [0];
1841 var aname = frame.Method.DeclaringType.Assembly.GetName ();
1842 Assert.AreEqual ("dtest-app, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", aname.ToString ());
1844 ModuleMirror m = frame.Method.DeclaringType.Module;
1846 Assert.AreEqual ("dtest-app.exe", m.Name);
1847 Assert.AreEqual ("dtest-app.exe", m.ScopeName);
1848 Assert.IsTrue (m.FullyQualifiedName.IndexOf ("dtest-app.exe") != -1);
1849 Guid guid = m.ModuleVersionId;
1850 Assert.AreEqual (frame.Method.DeclaringType.Assembly, m.Assembly);
1851 Assert.AreEqual (frame.Method.DeclaringType.Assembly.ManifestModule, m);
1853 // This is no longer true on 4.0
1854 //Assert.AreEqual ("Assembly", frame.Method.DeclaringType.Assembly.GetAssemblyObject ().Type.Name);
1856 TypeMirror t = vm.RootDomain.Corlib.GetType ("System.Diagnostics.DebuggerDisplayAttribute");
1857 Assert.AreEqual ("DebuggerDisplayAttribute", t.Name);
1861 public void LocalsInfo () {
1862 Event e = run_until ("locals2");
1864 StackFrame frame = e.Thread.GetFrames () [0];
1866 var locals = frame.Method.GetLocals ();
1867 Assert.AreEqual (9, locals.Length);
1868 for (int i = 0; i < 9; ++i) {
1869 if (locals [i].Name == "args") {
1870 Assert.IsTrue (locals [i].IsArg);
1871 Assert.AreEqual ("String[]", locals [i].Type.Name);
1872 } else if (locals [i].Name == "arg") {
1873 Assert.IsTrue (locals [i].IsArg);
1874 Assert.AreEqual ("Int32", locals [i].Type.Name);
1875 } else if (locals [i].Name == "i") {
1876 Assert.IsFalse (locals [i].IsArg);
1877 Assert.AreEqual ("Int64", locals [i].Type.Name);
1878 } else if (locals [i].Name == "j") {
1879 Assert.IsFalse (locals [i].IsArg);
1880 Assert.AreEqual ("Int32", locals [i].Type.Name);
1881 } else if (locals [i].Name == "s") {
1882 Assert.IsFalse (locals [i].IsArg);
1883 Assert.AreEqual ("String", locals [i].Type.Name);
1884 } else if (locals [i].Name == "t") {
1886 Assert.IsTrue (locals [i].IsArg);
1887 Assert.AreEqual ("String", locals [i].Type.Name);
1888 } else if (locals [i].Name == "rs") {
1889 Assert.IsTrue (locals [i].IsArg);
1890 Assert.AreEqual ("String", locals [i].Type.Name);
1891 } else if (locals [i].Name == "astruct") {
1892 } else if (locals [i].Name == "alist") {
1898 var scopes = frame.Method.GetScopes ();
1899 Assert.AreEqual (2, scopes.Length);
1902 Event step_once () {
1904 var e = GetNextEvent ();
1905 Assert.IsTrue (e is StepEvent);
1909 Event step_into () {
1910 step_req.Disable ();
1911 step_req.Depth = StepDepth.Into;
1913 return step_once ();
1916 Event step_over () {
1917 step_req.Disable ();
1918 step_req.Depth = StepDepth.Over;
1920 return step_once ();
1924 step_req.Disable ();
1925 step_req.Depth = StepDepth.Out;
1927 return step_once ();
1930 Event step_once_or_breakpoint () {
1932 var e = GetNextEvent ();
1933 Assert.IsTrue (e is StepEvent || e is BreakpointEvent);
1937 Event step_over_or_breakpoint () {
1938 step_req.Disable ();
1939 step_req.Depth = StepDepth.Over;
1941 return step_once_or_breakpoint ();
1944 Event step_out_or_breakpoint () {
1945 step_req.Disable ();
1946 step_req.Depth = StepDepth.Out;
1948 return step_once_or_breakpoint ();
1952 public void Locals () {
1953 var be = run_until ("locals1");
1955 StackFrame frame = be.Thread.GetFrames () [0];
1956 MethodMirror m1 = frame.Method;
1958 // Compiler generated byref local
1959 foreach (var l in m1.GetLocals ()) {
1960 // The byval flag is hidden from the type
1961 if (l.Name != "ri" && l.Type.Name == "Double")
1962 AssertValue (null, frame.GetValue (l));
1965 be = run_until ("locals2");
1967 frame = be.Thread.GetFrames () [0];
1969 object val = frame.GetValue (frame.Method.GetLocal ("i"));
1970 AssertValue (0, val);
1972 var req = create_step (be);
1979 var e = step_once ();
1980 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1982 // Execute s = "AB";
1984 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1986 frame = e.Thread.GetFrames () [0];
1988 val = frame.GetValue (frame.Method.GetLocal ("i"));
1989 AssertValue (42, val);
1991 LocalVariable[] locals = frame.Method.GetLocals ();
1992 var vals = frame.GetValues (locals);
1993 Assert.AreEqual (locals.Length, vals.Length);
1994 for (int i = 0; i < locals.Length; ++i) {
1995 if (locals [i].Name == "i")
1996 AssertValue (42, vals [i]);
1997 if (locals [i].Name == "s")
1998 AssertValue ("AB", vals [i]);
1999 if (locals [i].Name == "t")
2000 AssertValue ("ABC", vals [i]);
2001 if (locals [i].Name == "alist") {
2005 // Argument checking
2008 AssertThrows<ArgumentNullException> (delegate () {
2009 frame.GetValue ((LocalVariable)null);
2011 // GetValue () local from another method
2012 AssertThrows<ArgumentException> (delegate () {
2013 frame.GetValue (m1.GetLocal ("foo"));
2017 AssertThrows<ArgumentNullException> (delegate () {
2018 frame.GetValue ((ParameterInfoMirror)null);
2020 // GetValue () local from another method
2021 AssertThrows<ArgumentException> (delegate () {
2022 frame.GetValue (m1.GetParameters ()[0]);
2025 // GetValues () null
2026 AssertThrows<ArgumentNullException> (delegate () {
2027 frame.GetValues (null);
2029 // GetValues () embedded null
2030 AssertThrows<ArgumentNullException> (delegate () {
2031 frame.GetValues (new LocalVariable [] { null });
2033 // GetValues () local from another method
2034 AssertThrows<ArgumentException> (delegate () {
2035 frame.GetValues (new LocalVariable [] { m1.GetLocal ("foo") });
2038 AssertThrows<ArgumentException> (delegate () {
2039 val = frame.GetValue (frame.Method.ReturnParameter);
2042 // invalid stack frames
2044 e = GetNextEvent ();
2045 Assert.IsTrue (e is StepEvent);
2046 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
2048 AssertThrows<InvalidStackFrameException> (delegate () {
2049 frame.GetValue (frame.Method.GetLocal ("i"));
2055 be = run_until ("locals7");
2057 req = create_step (be);
2063 // Test that locals are initialized
2064 frame = e.Thread.GetFrames () [0];
2065 val = frame.GetValue (frame.Method.GetLocal ("t"));
2066 AssertValue (0, val);
2070 Assert.AreEqual ("locals7", (e as StepEvent).Method.Name);
2074 Assert.AreEqual ("locals7", (e as StepEvent).Method.Name);
2076 frame = e.Thread.GetFrames () [0];
2077 val = frame.GetValue (frame.Method.GetParameters ()[0]);
2078 AssertValue (22, val);
2079 val = frame.GetValue (frame.Method.GetLocal ("t"));
2080 AssertValue (22, val);
2081 val = frame.GetValue (frame.Method.GetLocal ("t2"));
2082 AssertValue (22, val);
2086 public void GetVisibleVariables () {
2087 Event e = run_until ("locals4");
2090 var locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
2091 Assert.AreEqual (2, locals.Count);
2092 var loc = locals.First (l => l.Name == "i");
2093 Assert.AreEqual ("Int64", loc.Type.Name);
2094 loc = locals.First (l => l.Name == "s");
2095 Assert.AreEqual ("String", loc.Type.Name);
2097 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
2098 Assert.AreEqual ("i", loc.Name);
2099 Assert.AreEqual ("Int64", loc.Type.Name);
2101 e = run_until ("locals5");
2104 locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
2105 Assert.AreEqual (2, locals.Count);
2106 loc = locals.First (l => l.Name == "i");
2107 Assert.AreEqual ("String", loc.Type.Name);
2108 loc = locals.First (l => l.Name == "s");
2109 Assert.AreEqual ("String", loc.Type.Name);
2111 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
2112 Assert.AreEqual ("i", loc.Name);
2113 Assert.AreEqual ("String", loc.Type.Name);
2115 // Variable in another scope
2116 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("j");
2117 Assert.IsNull (loc);
2121 public void Exit () {
2126 var e = GetNextEvent ();
2127 Assert.IsInstanceOfType (typeof (VMDeathEvent), e);
2129 Assert.AreEqual (5, (e as VMDeathEvent).ExitCode);
2132 /* Could be a remote vm with no process */
2135 Assert.AreEqual (5, p.ExitCode);
2138 AssertThrows<VMDisconnectedException> (delegate () {
2147 public void Dispose () {
2152 var e = GetNextEvent ();
2153 Assert.IsInstanceOfType (typeof (VMDisconnectEvent), e);
2156 /* Could be a remote vm with no process */
2159 Assert.AreEqual (3, p.ExitCode);
2162 AssertThrows<VMDisconnectedException> (delegate () {
2171 public void ColumnNumbers () {
2172 Event e = run_until ("line_numbers");
2174 // FIXME: Merge this with LineNumbers () when its fixed
2176 step_req = create_step (e);
2177 step_req.Depth = StepDepth.Into;
2185 e = GetNextEvent ();
2186 Assert.IsTrue (e is StepEvent);
2187 if (e.Thread.GetFrames ()[0].Method.Name == "ln1")
2191 // Do an additional step over so we are not on the beginning line of the method
2192 step_req.Disable ();
2193 step_req.Depth = StepDepth.Over;
2196 e = GetNextEvent ();
2197 Assert.IsTrue (e is StepEvent);
2199 l = e.Thread.GetFrames ()[0].Location;
2201 Assert.AreEqual (3, l.ColumnNumber);
2203 step_req.Disable ();
2207 // Broken by mcs+runtime changes (#5438)
2208 [Category("NotWorking")]
2209 public void LineNumbers () {
2210 Event e = run_until ("line_numbers");
2212 step_req = create_step (e);
2213 step_req.Depth = StepDepth.Into;
2220 e = GetNextEvent ();
2221 Assert.IsTrue (e is StepEvent);
2223 l = e.Thread.GetFrames ()[0].Location;
2225 Assert.IsTrue (l.SourceFile.IndexOf ("dtest-app.cs") != -1);
2226 Assert.AreEqual ("ln1", l.Method.Name);
2229 using (FileStream fs = new FileStream (l.SourceFile, FileMode.Open, FileAccess.Read)) {
2230 MD5 md5 = MD5.Create ();
2231 var hash = md5.ComputeHash (fs);
2233 for (int i = 0; i < 16; ++i)
2234 Assert.AreEqual (hash [i], l.SourceFileHash [i]);
2237 int line_base = l.LineNumber;
2240 e = GetNextEvent ();
2241 Assert.IsTrue (e is StepEvent);
2242 l = e.Thread.GetFrames ()[0].Location;
2243 Assert.AreEqual ("ln2", l.Method.Name);
2244 Assert.AreEqual (line_base + 6, l.LineNumber);
2247 e = GetNextEvent ();
2248 Assert.IsTrue (e is StepEvent);
2249 l = e.Thread.GetFrames ()[0].Location;
2250 Assert.AreEqual ("ln1", l.Method.Name);
2251 Assert.AreEqual (line_base + 1, l.LineNumber);
2254 e = GetNextEvent ();
2255 Assert.IsTrue (e is StepEvent);
2256 l = e.Thread.GetFrames ()[0].Location;
2257 Assert.AreEqual ("ln3", l.Method.Name);
2258 Assert.AreEqual (line_base + 11, l.LineNumber);
2261 e = GetNextEvent ();
2262 Assert.IsTrue (e is StepEvent);
2263 l = e.Thread.GetFrames ()[0].Location;
2264 Assert.AreEqual ("ln3", l.Method.Name);
2265 Assert.IsTrue (l.SourceFile.EndsWith ("FOO"));
2266 Assert.AreEqual (55, l.LineNumber);
2269 e = GetNextEvent ();
2270 Assert.IsTrue (e is StepEvent);
2271 l = e.Thread.GetFrames ()[0].Location;
2272 Assert.AreEqual ("ln1", l.Method.Name);
2273 Assert.AreEqual (line_base + 2, l.LineNumber);
2275 // GetSourceFiles ()
2276 string[] sources = l.Method.DeclaringType.GetSourceFiles ();
2277 Assert.AreEqual (2, sources.Length);
2278 Assert.AreEqual ("dtest-app.cs", sources [0]);
2279 Assert.AreEqual ("FOO", sources [1]);
2281 sources = l.Method.DeclaringType.GetSourceFiles (true);
2282 Assert.AreEqual (2, sources.Length);
2283 Assert.IsTrue (sources [0].EndsWith ("dtest-app.cs"));
2284 Assert.IsTrue (sources [1].EndsWith ("FOO"));
2288 public void Suspend () {
2291 Start (new string [] { "dtest-app.exe", "suspend-test" });
2293 Event e = run_until ("suspend");
2295 ThreadMirror main = e.Thread;
2303 // The debuggee should be suspended while it is running the infinite loop
2305 StackFrame frame = main.GetFrames ()[0];
2306 Assert.AreEqual ("suspend", frame.Method.Name);
2310 // resuming when not suspended
2311 AssertThrows<InvalidOperationException> (delegate () {
2321 public void AssemblyLoad () {
2322 Event e = run_until ("assembly_load");
2324 var load_req = vm.CreateAssemblyLoadRequest ();
2329 e = GetNextEvent ();
2330 Assert.IsInstanceOfType (typeof (AssemblyLoadEvent), e);
2331 Assert.IsTrue ((e as AssemblyLoadEvent).Assembly.Location.EndsWith ("System.dll"));
2333 var frames = e.Thread.GetFrames ();
2334 Assert.IsTrue (frames.Length > 0);
2335 Assert.AreEqual ("assembly_load", frames [0].Method.Name);
2339 public void CreateValue () {
2342 v = vm.CreateValue (1);
2343 Assert.AreEqual (vm, v.VirtualMachine);
2344 Assert.AreEqual (1, v.Value);
2346 v = vm.CreateValue (null);
2347 Assert.AreEqual (vm, v.VirtualMachine);
2348 Assert.AreEqual (null, v.Value);
2350 // Argument checking
2351 AssertThrows <ArgumentException> (delegate () {
2352 v = vm.CreateValue ("FOO");
2357 public void CreateString () {
2358 StringMirror s = vm.RootDomain.CreateString ("ABC");
2360 Assert.AreEqual (vm, s.VirtualMachine);
2361 Assert.AreEqual ("ABC", s.Value);
2362 Assert.AreEqual (vm.RootDomain, s.Domain);
2365 StringBuilder sb = new StringBuilder ();
2366 for (int i = 0; i < 1024; ++i)
2368 s = vm.RootDomain.CreateString (sb.ToString ());
2370 // Argument checking
2371 AssertThrows <ArgumentNullException> (delegate () {
2372 s = vm.RootDomain.CreateString (null);
2377 public void CreateBoxedValue () {
2378 ObjectMirror o = vm.RootDomain.CreateBoxedValue (new PrimitiveValue (vm, 42));
2380 Assert.AreEqual ("Int32", o.Type.Name);
2381 //AssertValue (42, m.GetValue (o.Type.GetField ("m_value")));
2383 // Argument checking
2384 AssertThrows <ArgumentNullException> (delegate () {
2385 vm.RootDomain.CreateBoxedValue (null);
2388 AssertThrows <ArgumentException> (delegate () {
2389 vm.RootDomain.CreateBoxedValue (o);
2394 public void Invoke () {
2395 Event e = run_until ("invoke1");
2397 StackFrame frame = e.Thread.GetFrames () [0];
2399 TypeMirror t = frame.Method.DeclaringType;
2400 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2402 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2408 m = t.GetMethod ("invoke_return_void");
2409 v = this_obj.InvokeMethod (e.Thread, m, null);
2413 m = t.GetMethod ("invoke_return_ref");
2414 v = this_obj.InvokeMethod (e.Thread, m, null);
2415 AssertValue ("ABC", v);
2418 m = t.GetMethod ("invoke_return_null");
2419 v = this_obj.InvokeMethod (e.Thread, m, null);
2420 AssertValue (null, v);
2423 m = t.GetMethod ("invoke_return_primitive");
2424 v = this_obj.InvokeMethod (e.Thread, m, null);
2425 AssertValue (42, v);
2428 m = t.GetMethod ("invoke_return_nullable");
2429 v = this_obj.InvokeMethod (e.Thread, m, null);
2430 Assert.IsInstanceOfType (typeof (StructMirror), v);
2431 var s = v as StructMirror;
2432 AssertValue (42, s.Fields [0]);
2433 AssertValue (true, s.Fields [1]);
2435 // pass nullable as this
2436 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2437 m = s.Type.GetMethod ("ToString");
2438 v = s.InvokeMethod (e.Thread, m, null);
2440 // return nullable null
2441 m = t.GetMethod ("invoke_return_nullable_null");
2442 v = this_obj.InvokeMethod (e.Thread, m, null);
2443 Assert.IsInstanceOfType (typeof (StructMirror), v);
2444 s = v as StructMirror;
2445 AssertValue (0, s.Fields [0]);
2446 AssertValue (false, s.Fields [1]);
2448 // pass nullable as this
2449 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2450 m = s.Type.GetMethod ("ToString");
2451 v = s.InvokeMethod (e.Thread, m, null);
2454 m = t.GetMethod ("invoke_pass_primitive");
2455 Value[] args = new Value [] {
2456 vm.CreateValue ((byte)Byte.MaxValue),
2457 vm.CreateValue ((sbyte)SByte.MaxValue),
2458 vm.CreateValue ((short)1),
2459 vm.CreateValue ((ushort)1),
2460 vm.CreateValue ((int)1),
2461 vm.CreateValue ((uint)1),
2462 vm.CreateValue ((long)1),
2463 vm.CreateValue ((ulong)1),
2464 vm.CreateValue ('A'),
2465 vm.CreateValue (true),
2466 vm.CreateValue (3.14f),
2467 vm.CreateValue (3.14) };
2469 v = this_obj.InvokeMethod (e.Thread, m, args);
2470 AssertValue ((int)Byte.MaxValue + (int)SByte.MaxValue + 1 + 1 + 1 + 1 + 1 + 1 + 'A' + 1 + 3 + 3, v);
2473 m = t.GetMethod ("invoke_pass_ref");
2474 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2475 AssertValue ("ABC", v);
2478 m = t.GetMethod ("invoke_pass_ref");
2479 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (null) });
2480 AssertValue (null, v);
2483 m = t.GetMethod ("invoke_static_pass_ref");
2484 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2485 AssertValue ("ABC", v);
2487 // static invoked using ObjectMirror.InvokeMethod
2488 m = t.GetMethod ("invoke_static_pass_ref");
2489 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2490 AssertValue ("ABC", v);
2492 // method which throws an exception
2494 m = t.GetMethod ("invoke_throws");
2495 v = this_obj.InvokeMethod (e.Thread, m, null);
2497 } catch (InvocationException ex) {
2498 Assert.AreEqual ("Exception", ex.Exception.Type.Name);
2502 m = t.GetMethod ("invoke_out");
2503 var out_task = this_obj.InvokeMethodAsyncWithResult (e.Thread, m, new Value [] { vm.CreateValue (1), vm.CreateValue (null) }, InvokeOptions.ReturnOutArgs);
2504 var out_args = out_task.Result.OutArgs;
2505 AssertValue (5, out_args [0]);
2506 Assert.IsTrue (out_args [1] is ArrayMirror);
2507 Assert.AreEqual (10, (out_args [1] as ArrayMirror).Length);
2509 // without ReturnOutArgs flag
2510 out_task = this_obj.InvokeMethodAsyncWithResult (e.Thread, m, new Value [] { vm.CreateValue (1), vm.CreateValue (null) });
2511 out_args = out_task.Result.OutArgs;
2512 Assert.IsNull (out_args);
2515 m = t.GetMethod (".ctor");
2516 v = t.InvokeMethod (e.Thread, m, null);
2517 Assert.IsInstanceOfType (typeof (ObjectMirror), v);
2518 Assert.AreEqual ("Tests", (v as ObjectMirror).Type.Name);
2521 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("ITest2");
2522 m = cl1.GetMethod ("invoke_iface");
2523 v = this_obj.InvokeMethod (e.Thread, m, null);
2524 AssertValue (42, v);
2527 m = t.BaseType.GetMethod ("virtual_method");
2528 v = this_obj.InvokeMethod (e.Thread, m, null, InvokeOptions.Virtual);
2529 AssertValue ("V2", v);
2531 // virtual call on static method
2532 m = t.GetMethod ("invoke_static_pass_ref");
2533 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") }, InvokeOptions.Virtual);
2534 AssertValue ("ABC", v);
2537 m = t.GetMethod ("invoke_pass_ref");
2538 var task = this_obj.InvokeMethodAsync (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2539 AssertValue ("ABC", task.Result);
2542 m = t.GetMethod ("invoke_static_pass_ref");
2543 task = t.InvokeMethodAsync (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2544 AssertValue ("ABC", task.Result);
2546 // Argument checking
2549 AssertThrows<ArgumentNullException> (delegate {
2550 m = t.GetMethod ("invoke_pass_ref");
2551 v = this_obj.InvokeMethod (null, m, new Value [] { vm.CreateValue (null) });
2555 AssertThrows<ArgumentNullException> (delegate {
2556 v = this_obj.InvokeMethod (e.Thread, null, new Value [] { vm.CreateValue (null) });
2559 // invalid number of arguments
2560 m = t.GetMethod ("invoke_pass_ref");
2561 AssertThrows<ArgumentException> (delegate {
2562 v = this_obj.InvokeMethod (e.Thread, m, null);
2565 // invalid type of argument (ref != primitive)
2566 m = t.GetMethod ("invoke_pass_ref");
2567 AssertThrows<ArgumentException> (delegate {
2568 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2571 // invalid type of argument (primitive != primitive)
2572 m = t.GetMethod ("invoke_pass_primitive_2");
2573 AssertThrows<ArgumentException> (delegate {
2574 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2577 // invoking a non-static method as static
2578 m = t.GetMethod ("invoke_pass_ref");
2579 AssertThrows<ArgumentException> (delegate {
2580 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2583 // invoking a method defined in another class
2584 m = t2.GetMethod ("invoke");
2585 AssertThrows<ArgumentException> (delegate {
2586 v = this_obj.InvokeMethod (e.Thread, m, null);
2591 public void InvokeVType () {
2592 Event e = run_until ("invoke1");
2594 StackFrame frame = e.Thread.GetFrames () [0];
2596 var s = frame.GetArgument (1) as StructMirror;
2598 TypeMirror t = s.Type;
2603 // Pass struct as this, receive int
2604 m = t.GetMethod ("invoke_return_int");
2605 v = s.InvokeMethod (e.Thread, m, null);
2606 AssertValue (42, v);
2608 // Pass boxed struct as this
2609 var boxed_this = t.NewInstance () as ObjectMirror;
2610 m = t.GetMethod ("invoke_return_int");
2611 v = boxed_this.InvokeMethod (e.Thread, m, null);
2614 // Pass struct as this, receive intptr
2615 m = t.GetMethod ("invoke_return_intptr");
2616 v = s.InvokeMethod (e.Thread, m, null);
2617 AssertValue (43, v);
2620 m = t.GetMethod ("invoke_static");
2621 v = t.InvokeMethod (e.Thread, m, null);
2624 // Pass generic struct as this
2625 s = frame.GetArgument (2) as StructMirror;
2627 m = t.GetMethod ("invoke_return_int");
2628 v = s.InvokeMethod (e.Thread, m, null);
2629 AssertValue (42, v);
2632 s = frame.GetArgument (1) as StructMirror;
2634 m = t.GetMethods ().First (method => method.Name == ".ctor" && method.GetParameters ().Length == 1);
2635 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2636 AssertValue (1, (v as StructMirror)["i"]);
2638 // Invoke a method which changes state
2639 s = frame.GetArgument (1) as StructMirror;
2641 m = t.GetMethod ("invoke_mutate");
2642 var task = s.InvokeMethodAsyncWithResult (e.Thread, m, null, InvokeOptions.ReturnOutThis);
2643 var out_this = task.Result.OutThis as StructMirror;
2644 AssertValue (5, out_this ["l"]);
2646 // Without the ReturnOutThis flag
2647 s = frame.GetArgument (1) as StructMirror;
2649 m = t.GetMethod ("invoke_mutate");
2650 task = s.InvokeMethodAsyncWithResult (e.Thread, m, null);
2651 out_this = task.Result.OutThis as StructMirror;
2652 Assert.AreEqual (null, out_this);
2655 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("ITest2");
2656 m = cl1.GetMethod ("invoke_iface");
2657 v = s.InvokeMethod (e.Thread, m, null);
2658 AssertValue (42, v);
2661 m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2662 v = s.InvokeMethod (e.Thread, m, null, InvokeOptions.Virtual);
2663 AssertValue ("42", v);
2667 public void BreakpointDuringInvoke () {
2668 Event e = run_until ("invoke1");
2670 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke2");
2671 Assert.IsNotNull (m);
2672 vm.SetBreakpoint (m, 0);
2674 StackFrame frame = e.Thread.GetFrames () [0];
2675 var o = frame.GetThis () as ObjectMirror;
2677 bool failed = false;
2679 bool finished = false;
2680 object wait = new object ();
2682 // Have to invoke in a separate thread as the invoke is suspended until we
2683 // resume after the breakpoint
2684 Thread t = new Thread (delegate () {
2686 o.InvokeMethod (e.Thread, m, null);
2692 Monitor.Pulse (wait);
2698 StackFrame invoke_frame = null;
2701 e = GetNextEvent ();
2702 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
2703 // Check stack trace support and invokes
2704 var frames = e.Thread.GetFrames ();
2705 invoke_frame = frames [0];
2706 Assert.AreEqual ("invoke2", frames [0].Method.Name);
2707 Assert.IsTrue (frames [0].IsDebuggerInvoke);
2708 Assert.AreEqual ("invoke1", frames [1].Method.Name);
2715 Monitor.Wait (wait);
2718 // Check that the invoke frames are no longer valid
2719 AssertThrows<InvalidStackFrameException> (delegate {
2720 invoke_frame.GetThis ();
2723 // Check InvokeOptions.DisableBreakpoints flag
2724 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2728 public void DisabledExceptionDuringInvoke () {
2729 Event e = run_until ("invoke_ex");
2731 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_ex_inner");
2733 StackFrame frame = e.Thread.GetFrames () [0];
2734 var o = frame.GetThis () as ObjectMirror;
2736 var req = vm.CreateExceptionRequest (null);
2739 // Check InvokeOptions.DisableBreakpoints flag
2740 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2746 public void InvokeSingleThreaded () {
2749 Start (new string [] { "dtest-app.exe", "invoke-single-threaded" });
2751 Event e = run_until ("invoke_single_threaded_2");
2753 StackFrame f = e.Thread.GetFrames ()[0];
2755 var obj = f.GetThis () as ObjectMirror;
2757 // Check that the counter value incremented by the other thread does not increase
2758 // during the invoke.
2759 object counter1 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2761 var m = obj.Type.GetMethod ("invoke_return_void");
2762 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2764 object counter2 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2766 Assert.AreEqual ((int)counter1, (int)counter2);
2768 // Test multiple invokes done in succession
2769 m = obj.Type.GetMethod ("invoke_return_void");
2770 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2772 // Test events during single-threaded invokes
2773 vm.EnableEvents (EventType.TypeLoad);
2774 m = obj.Type.GetMethod ("invoke_type_load");
2775 obj.BeginInvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded, delegate {
2779 e = GetNextEvent ();
2780 Assert.AreEqual (EventType.TypeLoad, e.EventType);
2783 List<Value> invoke_results;
2786 public void InvokeMultiple () {
2787 Event e = run_until ("invoke1");
2789 StackFrame frame = e.Thread.GetFrames () [0];
2791 TypeMirror t = frame.Method.DeclaringType;
2792 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2794 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2796 var methods = new MethodMirror [2];
2797 methods [0] = t.GetMethod ("invoke_return_ref");
2798 methods [1] = t.GetMethod ("invoke_return_primitive");
2800 invoke_results = new List<Value> ();
2802 var r = this_obj.BeginInvokeMultiple (e.Thread, methods, null, InvokeOptions.SingleThreaded, invoke_multiple_cb, this_obj);
2803 WaitHandle.WaitAll (new WaitHandle[] { r.AsyncWaitHandle });
2804 this_obj.EndInvokeMultiple (r);
2805 // The callback might still be running
2806 while (invoke_results.Count < 2) {
2809 if (invoke_results [0] is PrimitiveValue) {
2810 AssertValue ("ABC", invoke_results [1]);
2811 AssertValue (42, invoke_results [0]);
2813 AssertValue ("ABC", invoke_results [0]);
2814 AssertValue (42, invoke_results [1]);
2818 void invoke_multiple_cb (IAsyncResult ar) {
2819 ObjectMirror this_obj = (ObjectMirror)ar.AsyncState;
2821 var res = this_obj.EndInvokeMethod (ar);
2822 lock (invoke_results)
2823 invoke_results.Add (res);
2827 public void InvokeAbort () {
2830 Start (new string [] { "dtest-app.exe", "invoke-abort" });
2832 Event e = run_until ("invoke_abort");
2834 StackFrame f = e.Thread.GetFrames ()[0];
2836 var obj = f.GetThis () as ObjectMirror;
2838 var m = t.GetMethod ("invoke_abort_2");
2839 // Invoke multiple times to check that the subsequent invokes are aborted too
2840 var res = (IInvokeAsyncResult)obj.BeginInvokeMultiple (e.Thread, new MethodMirror[] { m, m, m, m }, null, InvokeOptions.None, delegate { }, null);
2843 AssertThrows<CommandException> (delegate {
2844 obj.EndInvokeMethod (res);
2849 public void GetThreads () {
2854 public void Threads () {
2855 Event e = run_until ("threads");
2857 Assert.AreEqual (ThreadState.Running, e.Thread.ThreadState);
2859 Assert.IsTrue (e.Thread.ThreadId > 0);
2861 Assert.AreEqual (e.Thread.TID, e.Thread.TID);
2863 vm.EnableEvents (EventType.ThreadStart, EventType.ThreadDeath);
2867 e = GetNextEvent ();
2868 Assert.IsInstanceOfType (typeof (ThreadStartEvent), e);
2869 var state = e.Thread.ThreadState;
2870 Assert.IsTrue (state == ThreadState.Running || state == ThreadState.Unstarted);
2874 e = GetNextEvent ();
2875 Assert.IsInstanceOfType (typeof (ThreadDeathEvent), e);
2876 Assert.AreEqual (ThreadState.Stopped, e.Thread.ThreadState);
2880 public void Frame_SetValue () {
2881 Event e = run_until ("locals2");
2883 StackFrame frame = e.Thread.GetFrames () [0];
2886 var l = frame.Method.GetLocal ("i");
2887 frame.SetValue (l, vm.CreateValue ((long)55));
2888 AssertValue (55, frame.GetValue (l));
2891 l = frame.Method.GetLocal ("s");
2892 frame.SetValue (l, vm.RootDomain.CreateString ("DEF"));
2893 AssertValue ("DEF", frame.GetValue (l));
2895 // argument as local
2896 l = frame.Method.GetLocal ("arg");
2897 frame.SetValue (l, vm.CreateValue (6));
2898 AssertValue (6, frame.GetValue (l));
2901 var p = frame.Method.GetParameters ()[1];
2902 frame.SetValue (p, vm.CreateValue (7));
2903 AssertValue (7, frame.GetValue (p));
2906 p = frame.Method.GetParameters ()[2];
2907 frame.SetValue (p, vm.RootDomain.CreateString ("DEF"));
2908 AssertValue ("DEF", frame.GetValue (p));
2911 p = frame.Method.GetParameters ()[3];
2912 frame.SetValue (p, vm.RootDomain.CreateString ("DEF2"));
2913 AssertValue ("DEF2", frame.GetValue (p));
2916 p = frame.Method.GetParameters ()[4];
2917 var v = frame.GetValue (p) as StructMirror;
2918 v ["i"] = vm.CreateValue (43);
2919 frame.SetValue (p, v);
2920 v = frame.GetValue (p) as StructMirror;
2921 AssertValue (43, v ["i"]);
2923 // argument checking
2926 AssertThrows<ArgumentNullException> (delegate () {
2927 frame.SetValue ((LocalVariable)null, vm.CreateValue (55));
2931 AssertThrows<ArgumentNullException> (delegate () {
2932 l = frame.Method.GetLocal ("i");
2933 frame.SetValue (l, null);
2936 // value of invalid type
2937 AssertThrows<ArgumentException> (delegate () {
2938 l = frame.Method.GetLocal ("i");
2939 frame.SetValue (l, vm.CreateValue (55));
2945 public void Frame_SetValue_Registers () {
2946 Event e = run_until ("locals6_1");
2948 StackFrame frame = e.Thread.GetFrames () [1];
2951 var l = frame.Method.GetLocal ("j");
2952 frame.SetValue (l, vm.CreateValue (99));
2953 AssertValue (99, frame.GetValue (l));
2955 // Check it during execution
2956 e = run_until ("locals6_2");
2957 frame = e.Thread.GetFrames () [0];
2958 AssertValue (99, frame.GetValue (frame.Method.GetParameters ()[0]));
2960 // Set it while in a frame which clobbers its register
2961 e = run_until ("locals6_3");
2962 frame = e.Thread.GetFrames () [1];
2963 frame.SetValue (l, vm.CreateValue (100));
2964 AssertValue (100, frame.GetValue (l));
2966 // Check it during execution
2967 e = run_until ("locals6_4");
2968 frame = e.Thread.GetFrames () [0];
2969 AssertValue (100, frame.GetValue (frame.Method.GetParameters ()[0]));
2971 // Signed byte value
2972 e = run_until ("locals6_5");
2973 frame = e.Thread.GetFrames () [1];
2974 var l2 = frame.Method.GetLocal ("sb");
2975 frame.SetValue (l2, vm.CreateValue ((sbyte)-99));
2976 AssertValue (-99, frame.GetValue (l2));
2978 // Check it during execution
2979 e = run_until ("locals6_6");
2980 frame = e.Thread.GetFrames () [0];
2981 AssertValue (-99, frame.GetValue (frame.Method.GetParameters ()[0]));
2985 public void InvokeRegress () {
2986 Event e = run_until ("invoke1");
2988 StackFrame frame = e.Thread.GetFrames () [0];
2990 TypeMirror t = frame.Method.DeclaringType;
2991 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2993 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2999 m = t.GetMethod ("invoke_return_void");
3000 v = this_obj.InvokeMethod (e.Thread, m, null);
3003 // Check that the stack frames remain valid during the invoke
3004 Assert.AreEqual ("Tests", (frame.GetThis () as ObjectMirror).Type.Name);
3006 // do another invoke
3007 m = t.GetMethod ("invoke_return_void");
3008 v = this_obj.InvokeMethod (e.Thread, m, null);
3011 // Try a single step after the invoke
3012 var req = create_step (e);
3013 req.Depth = StepDepth.Into;
3014 req.Size = StepSize.Line;
3020 // Step into invoke2
3022 e = GetNextEvent ();
3023 Assert.IsTrue (e is StepEvent);
3024 Assert.AreEqual ("invoke2", (e as StepEvent).Method.Name);
3028 frame = e.Thread.GetFrames () [0];
3032 public void Exceptions () {
3033 Event e = run_until ("exceptions");
3034 var req = vm.CreateExceptionRequest (null);
3039 e = GetNextEvent ();
3040 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3041 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
3043 var frames = e.Thread.GetFrames ();
3044 Assert.AreEqual ("exceptions", frames [0].Method.Name);
3047 // exception type filter
3049 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.ArgumentException"));
3052 // Skip the throwing of the second OverflowException
3055 e = GetNextEvent ();
3056 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3057 Assert.AreEqual ("ArgumentException", (e as ExceptionEvent).Exception.Type.Name);
3060 // exception type filter for subclasses
3061 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.Exception"));
3066 e = GetNextEvent ();
3067 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3068 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
3072 req.IncludeSubclasses = false;
3077 e = GetNextEvent ();
3078 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3079 Assert.AreEqual ("Exception", (e as ExceptionEvent).Exception.Type.Name);
3082 // Implicit exceptions
3083 req = vm.CreateExceptionRequest (null);
3088 e = GetNextEvent ();
3089 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3090 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
3093 // Single stepping after an exception
3094 req = vm.CreateExceptionRequest (null);
3099 e = GetNextEvent ();
3100 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3101 Assert.AreEqual ("Exception", (e as ExceptionEvent).Exception.Type.Name);
3102 frames = e.Thread.GetFrames ();
3103 Assert.AreEqual ("exceptions2", frames [0].Method.Name);
3106 var sreq = create_step (e);
3107 sreq.Depth = StepDepth.Over;
3108 sreq.Size = StepSize.Line;
3112 e = GetNextEvent ();
3113 Assert.IsInstanceOfType (typeof (StepEvent), e);
3114 frames = e.Thread.GetFrames ();
3115 Assert.AreEqual ("exceptions", frames [0].Method.Name);
3118 // Argument checking
3119 AssertThrows<ArgumentException> (delegate {
3120 vm.CreateExceptionRequest (e.Thread.Type);
3125 public void ExceptionFilter () {
3126 Event e = run_until ("exception_filter");
3128 MethodMirror m = entry_point.DeclaringType.GetMethod ("exception_filter_filter");
3129 Assert.IsNotNull (m);
3131 vm.SetBreakpoint (m, 0);
3135 e = GetNextEvent ();
3136 Assert.AreEqual (EventType.Breakpoint, e.EventType);
3137 Assert.IsTrue (e is BreakpointEvent);
3138 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
3140 var frames = e.Thread.GetFrames ();
3142 Assert.IsTrue (frames [0].Location.SourceFile.IndexOf ("dtest-app.cs") != -1);
3143 Assert.AreEqual ("exception_filter_filter", frames [0].Location.Method.Name);
3145 Assert.AreEqual (0, frames [1].Location.Method.MetadataToken);
3146 Assert.AreEqual (0x0f, frames [1].Location.ILOffset);
3148 Assert.AreEqual ("exception_filter_method", frames [2].Location.Method.Name);
3149 Assert.AreEqual (0x06, frames [2].Location.ILOffset);
3151 Assert.AreEqual (0, frames [3].Location.Method.MetadataToken, 0);
3152 Assert.AreEqual (0, frames [3].Location.ILOffset);
3154 Assert.AreEqual ("exception_filter", frames [4].Location.Method.Name);
3158 public void ExceptionFilter2 () {
3161 Start (new string [] { "dtest-excfilter.exe" });
3163 MethodMirror filter_method = entry_point.DeclaringType.GetMethod ("Filter");
3164 Assert.IsNotNull (filter_method);
3166 MethodMirror test_method = entry_point.DeclaringType.GetMethod ("Test");
3167 Assert.IsNotNull (test_method);
3169 vm.SetBreakpoint (filter_method, 0);
3173 var e = GetNextEvent ();
3174 Assert.AreEqual (EventType.Breakpoint, e.EventType);
3175 Assert.IsTrue (e is BreakpointEvent);
3176 Assert.AreEqual (filter_method.Name, (e as BreakpointEvent).Method.Name);
3178 var frames = e.Thread.GetFrames ();
3180 Assert.AreEqual (4, frames.Count ());
3182 Assert.AreEqual (filter_method.Name, frames [0].Location.Method.Name);
3183 Assert.AreEqual (20, frames [0].Location.LineNumber);
3184 Assert.AreEqual (0, frames [0].Location.ILOffset);
3186 Assert.AreEqual (test_method.Name, frames [1].Location.Method.Name);
3187 Assert.AreEqual (37, frames [1].Location.LineNumber);
3188 Assert.AreEqual (0x0b, frames [1].Location.ILOffset);
3190 Assert.AreEqual (test_method.Name, frames [2].Location.Method.Name);
3191 Assert.AreEqual (33, frames [2].Location.LineNumber);
3192 Assert.AreEqual (0x05, frames [2].Location.ILOffset);
3194 Assert.AreEqual (entry_point.Name, frames [3].Location.Method.Name);
3195 Assert.AreEqual (14, frames [3].Location.LineNumber);
3196 Assert.AreEqual (0x00, frames [3].Location.ILOffset);
3204 public void EventSets () {
3206 // Create two filter which both match the same exception
3208 Event e = run_until ("exceptions");
3210 var req = vm.CreateExceptionRequest (null);
3213 var req2 = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.OverflowException"));
3218 var es = vm.GetNextEventSet ();
3219 Assert.AreEqual (2, es.Events.Length);
3222 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3223 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
3226 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3227 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
3234 // Test single threaded invokes during processing of nullref exceptions.
3235 // These won't work if the exception handling is done from the sigsegv signal
3236 // handler, since the sigsegv signal is disabled until control returns from the
3240 [Category ("only3")]
3241 public void NullRefExceptionAndSingleThreadedInvoke () {
3242 Event e = run_until ("exceptions");
3243 var req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.NullReferenceException"));
3248 e = GetNextEvent ();
3249 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
3250 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
3252 var ex = (e as ExceptionEvent).Exception;
3253 var tostring_method = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
3254 ex.InvokeMethod (e.Thread, tostring_method, null, InvokeOptions.SingleThreaded);
3258 public void MemberInOtherDomain () {
3261 Start (new string [] { "dtest-app.exe", "domain-test" });
3263 vm.EnableEvents (EventType.AppDomainCreate, EventType.AppDomainUnload, EventType.AssemblyUnload);
3265 Event e = run_until ("domains_print_across");
3267 var frame = e.Thread.GetFrames ()[0];
3268 var inOtherDomain = frame.GetArgument (0) as ObjectMirror;
3269 var crossDomainField = (ObjectMirror) inOtherDomain.GetValue (inOtherDomain.Type.GetField("printMe"));
3270 Assert.AreEqual ("SentinelClass", crossDomainField.Type.Name);
3274 public void Domains () {
3277 Start (new string [] { "dtest-app.exe", "domain-test" });
3279 vm.EnableEvents (EventType.AppDomainCreate, EventType.AppDomainUnload, EventType.AssemblyUnload);
3281 Event e = run_until ("domains");
3285 e = GetNextEvent ();
3286 Assert.IsInstanceOfType (typeof (AppDomainCreateEvent), e);
3288 var domain = (e as AppDomainCreateEvent).Domain;
3290 // Check the object type
3291 e = run_until ("domains_2");
3292 var frame = e.Thread.GetFrames ()[0];
3293 var o = frame.GetArgument (0) as ObjectMirror;
3294 Assert.AreEqual ("CrossDomain", o.Type.Name);
3296 // Do a remoting invoke
3297 var cross_domain_type = o.Type;
3298 var v = o.InvokeMethod (e.Thread, cross_domain_type.GetMethod ("invoke_3"), null);
3299 AssertValue (42, v);
3301 // Run until the callback in the domain
3302 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_in_domain");
3303 Assert.IsNotNull (m);
3304 vm.SetBreakpoint (m, 0);
3308 e = GetNextEvent ();
3309 if (e is BreakpointEvent)
3313 Assert.AreEqual ("invoke_in_domain", (e as BreakpointEvent).Method.Name);
3315 // d_method is from another domain
3316 MethodMirror d_method = (e as BreakpointEvent).Method;
3317 Assert.IsTrue (m != d_method);
3319 var frames = e.Thread.GetFrames ();
3320 Assert.AreEqual ("invoke_in_domain", frames [0].Method.Name);
3321 Assert.AreEqual (domain, frames [0].Domain);
3322 Assert.AreEqual ("invoke", frames [1].Method.Name);
3323 Assert.AreEqual ("domains", frames [2].Method.Name);
3324 Assert.AreEqual (vm.RootDomain, frames [2].Domain);
3326 // Test breakpoints on already JITted methods in other domains
3327 m = entry_point.DeclaringType.GetMethod ("invoke_in_domain_2");
3328 Assert.IsNotNull (m);
3329 vm.SetBreakpoint (m, 0);
3333 e = GetNextEvent ();
3334 if (e is BreakpointEvent)
3338 Assert.AreEqual ("invoke_in_domain_2", (e as BreakpointEvent).Method.Name);
3340 // This is empty when receiving the AppDomainCreateEvent
3341 Assert.AreEqual ("domain", domain.FriendlyName);
3343 // Run until the unload
3346 e = GetNextEvent ();
3347 if (e is AssemblyUnloadEvent) {
3348 AssertThrows<Exception> (delegate () {
3349 var assembly_obj = (e as AssemblyUnloadEvent).Assembly.GetAssemblyObject ();
3356 Assert.IsInstanceOfType (typeof (AppDomainUnloadEvent), e);
3357 Assert.AreEqual (domain, (e as AppDomainUnloadEvent).Domain);
3359 // Run past the unload
3360 e = run_until ("domains_3");
3362 // Test access to unloaded types
3363 // FIXME: Add an exception type for this
3364 AssertThrows<Exception> (delegate {
3365 d_method.DeclaringType.GetValue (d_method.DeclaringType.GetField ("static_i"));
3368 // Check that .Domain is accessible for stack frames with native transitions
3369 e = run_until ("called_from_invoke");
3370 ThreadMirror.NativeTransitions = true;
3371 foreach (var f in e.Thread.GetFrames ()) {
3377 public void DynamicMethods () {
3378 Event e = run_until ("dyn_call");
3380 var m = e.Thread.GetFrames ()[1].Method;
3381 Assert.AreEqual ("dyn_method", m.Name);
3383 // Test access to IL
3384 var body = m.GetMethodBody ();
3386 ILInstruction ins = body.Instructions [0];
3387 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
3388 Assert.AreEqual ("FOO", ins.Operand);
3392 public void RefEmit () {
3395 Start (new string [] { "dtest-app.exe", "ref-emit-test" });
3397 Event e = run_until ("ref_emit_call");
3399 var m = e.Thread.GetFrames ()[1].Method;
3400 Assert.AreEqual ("ref_emit_method", m.Name);
3402 // Test access to IL
3403 var body = m.GetMethodBody ();
3407 ins = body.Instructions [0];
3408 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
3409 Assert.AreEqual ("FOO", ins.Operand);
3411 ins = body.Instructions [1];
3412 Assert.AreEqual (OpCodes.Call, ins.OpCode);
3413 Assert.IsInstanceOfType (typeof (MethodMirror), ins.Operand);
3414 Assert.AreEqual ("ref_emit_call", (ins.Operand as MethodMirror).Name);
3418 public void IsAttached () {
3419 var f = entry_point.DeclaringType.GetField ("is_attached");
3421 Event e = run_until ("Main");
3423 AssertValue (true, entry_point.DeclaringType.GetValue (f));
3427 public void StackTraceInNative () {
3428 // Check that stack traces can be produced for threads in native code
3431 Start (new string [] { "dtest-app.exe", "frames-in-native" });
3433 var e = run_until ("frames_in_native");
3435 // FIXME: This is racy
3442 StackFrame[] frames = e.Thread.GetFrames ();
3444 int frame_index = -1;
3445 for (int i = 0; i < frames.Length; ++i) {
3446 if (frames [i].Method.Name == "Sleep") {
3452 Assert.IsTrue (frame_index != -1);
3453 Assert.AreEqual ("Sleep", frames [frame_index].Method.Name);
3454 Assert.AreEqual ("frames_in_native", frames [frame_index + 1].Method.Name);
3455 Assert.AreEqual ("Main", frames [frame_index + 2].Method.Name);
3457 // Check that invokes are disabled for such threads
3458 TypeMirror t = frames [frame_index + 1].Method.DeclaringType;
3460 var m = t.GetMethod ("invoke_static_return_void");
3461 AssertThrows<InvalidOperationException> (delegate {
3462 t.InvokeMethod (e.Thread, m, null);
3465 // Check that the frame info is invalidated
3466 run_until ("frames_in_native_2");
3468 AssertThrows<InvalidStackFrameException> (delegate {
3469 Console.WriteLine (frames [frame_index].GetThis ());
3474 public void VirtualMachine_CreateEnumMirror () {
3475 var e = run_until ("o1");
3476 var frame = e.Thread.GetFrames () [0];
3478 object val = frame.GetThis ();
3479 Assert.IsTrue (val is ObjectMirror);
3480 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
3481 ObjectMirror o = (val as ObjectMirror);
3483 FieldInfoMirror field = o.Type.GetField ("field_enum");
3484 Value f = o.GetValue (field);
3485 TypeMirror enumType = (f as EnumMirror).Type;
3487 o.SetValue (field, vm.CreateEnumMirror (enumType, vm.CreateValue (1)));
3488 f = o.GetValue (field);
3489 Assert.AreEqual (1, (f as EnumMirror).Value);
3491 // Argument checking
3492 AssertThrows<ArgumentNullException> (delegate () {
3493 vm.CreateEnumMirror (enumType, null);
3496 AssertThrows<ArgumentNullException> (delegate () {
3497 vm.CreateEnumMirror (null, vm.CreateValue (1));
3501 AssertThrows<ArgumentException> (delegate () {
3502 vm.CreateEnumMirror (enumType, vm.CreateValue (null));
3505 // value of a wrong type
3506 AssertThrows<ArgumentException> (delegate () {
3507 vm.CreateEnumMirror (enumType, vm.CreateValue ((long)1));
3512 public void VirtualMachine_EnableEvents_Breakpoint () {
3513 AssertThrows<ArgumentException> (delegate () {
3514 vm.EnableEvents (EventType.Breakpoint);
3519 public void SingleStepRegress654694 () {
3522 MethodMirror m = entry_point.DeclaringType.GetMethod ("ss_regress_654694");
3523 foreach (Location l in m.Locations) {
3524 if (l.ILOffset > 0 && il_offset == -1)
3525 il_offset = l.ILOffset;
3528 Event e = run_until ("ss_regress_654694");
3530 Assert.IsNotNull (m);
3531 vm.SetBreakpoint (m, il_offset);
3535 e = GetNextEvent ();
3536 Assert.IsTrue (e is BreakpointEvent);
3538 var req = create_step (e);
3539 req.Depth = StepDepth.Over;
3540 req.Size = StepSize.Line;
3545 e = GetNextEvent ();
3546 Assert.IsTrue (e is StepEvent);
3552 public void DebugBreak () {
3553 vm.EnableEvents (EventType.UserBreak);
3558 var e = GetNextEvent ();
3559 Assert.IsTrue (e is UserBreakEvent);
3563 public void DebugLog () {
3564 vm.EnableEvents (EventType.UserLog);
3569 var e = GetNextEvent ();
3570 Assert.IsTrue (e is UserLogEvent);
3571 var le = e as UserLogEvent;
3573 Assert.AreEqual (5, le.Level);
3574 Assert.AreEqual ("A", le.Category);
3575 Assert.AreEqual ("B", le.Message);
3579 public void TypeGetMethodsByNameFlags () {
3581 var assembly = entry_point.DeclaringType.Assembly;
3582 var type = assembly.GetType ("Tests3");
3584 Assert.IsNotNull (type);
3586 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Public, false);
3587 Assert.AreEqual (1, mm.Length, "#1");
3588 Assert.AreEqual ("M1", mm[0].Name, "#2");
3590 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.NonPublic, false);
3591 Assert.AreEqual (1, mm.Length, "#3");
3592 Assert.AreEqual ("M2", mm[0].Name, "#4");
3594 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public, false);
3595 Assert.AreEqual (7, mm.Length, "#5"); //M3 plus Equals, GetHashCode, GetType, ToString, .ctor
3597 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, false);
3598 Assert.AreEqual (2, mm.Length, "#7");
3600 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
3601 Assert.AreEqual (1, mm.Length, "#9");
3603 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
3604 Assert.AreEqual (5, mm.Length, "#11");
3607 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Public, false);
3608 Assert.AreEqual (1, mm.Length, "#12");
3609 Assert.AreEqual ("M1", mm[0].Name, "#13");
3611 mm = type.GetMethodsByNameFlags ("m1", BindingFlags.Static | BindingFlags.Public, true);
3612 Assert.AreEqual (1, mm.Length, "#14");
3613 Assert.AreEqual ("M1", mm[0].Name, "#15");
3615 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, false);
3616 Assert.AreEqual (1, mm.Length, "#16");
3617 Assert.AreEqual ("M1", mm[0].Name, "#17");
3621 [Category ("only88")]
3622 public void TypeLoadSourceFileFilter () {
3623 Event e = run_until ("type_load");
3625 if (!vm.Version.AtLeast (2, 7))
3628 string srcfile = (e as BreakpointEvent).Method.DeclaringType.GetSourceFiles (true)[0];
3629 srcfile = srcfile.Replace ("dtest-app.cs", "TypeLoadClass.cs");
3630 Assert.IsTrue (srcfile.Contains ("TypeLoadClass.cs"));
3632 var req = vm.CreateTypeLoadRequest ();
3633 req.SourceFileFilter = new string [] { srcfile.ToUpper () };
3637 e = GetNextEvent ();
3638 Assert.IsTrue (e is TypeLoadEvent);
3639 Assert.AreEqual ("TypeLoadClass", (e as TypeLoadEvent).Type.FullName);
3643 public void TypeLoadTypeNameFilter () {
3644 Event e = run_until ("type_load");
3646 var req = vm.CreateTypeLoadRequest ();
3647 req.TypeNameFilter = new string [] { "TypeLoadClass2" };
3651 e = GetNextEvent ();
3652 Assert.IsTrue (e is TypeLoadEvent);
3653 Assert.AreEqual ("TypeLoadClass2", (e as TypeLoadEvent).Type.FullName);
3657 public void GetTypesForSourceFile () {
3660 var types = vm.GetTypesForSourceFile ("dtest-app.cs", false);
3661 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
3662 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
3664 types = vm.GetTypesForSourceFile ("DTEST-app.cs", true);
3665 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
3666 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
3670 public void GetTypesNamed () {
3673 var types = vm.GetTypes ("Tests", false);
3674 Assert.AreEqual (1, types.Count);
3675 Assert.AreEqual ("Tests", types [0].FullName);
3677 types = vm.GetTypes ("System.Exception", false);
3678 Assert.AreEqual (1, types.Count);
3679 Assert.AreEqual ("System.Exception", types [0].FullName);
3683 public void String_GetValue () {
3688 var e = run_until ("arg2");
3690 var frame = e.Thread.GetFrames () [0];
3692 val = frame.GetArgument (6);
3693 Assert.AreEqual ('\0'.ToString () + "A", (val as StringMirror).Value);
3697 public void String_GetChars () {
3701 var e = run_until ("arg2");
3703 var frame = e.Thread.GetFrames () [0];
3705 val = frame.GetArgument (0);
3706 Assert.IsTrue (val is StringMirror);
3707 AssertValue ("FOO", val);
3708 var s = (val as StringMirror);
3709 Assert.AreEqual (3, s.Length);
3711 var c = s.GetChars (0, 2);
3712 Assert.AreEqual (2, c.Length);
3713 Assert.AreEqual ('F', c [0]);
3714 Assert.AreEqual ('O', c [1]);
3716 AssertThrows<ArgumentException> (delegate () {
3722 public void GetInterfaces () {
3723 var e = run_until ("arg2");
3725 var frame = e.Thread.GetFrames () [0];
3727 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
3728 var ifaces = cl1.GetInterfaces ();
3729 Assert.AreEqual (1, ifaces.Length);
3730 Assert.AreEqual ("ITest", ifaces [0].Name);
3732 var cl2 = cl1.GetMethod ("Baz").ReturnType;
3733 var ifaces2 = cl2.GetInterfaces ();
3734 Assert.AreEqual (1, ifaces2.Length);
3735 Assert.AreEqual ("ITest`1", ifaces2 [0].Name);
3739 public void GetInterfaceMap () {
3740 var e = run_until ("arg2");
3742 var frame = e.Thread.GetFrames () [0];
3744 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
3745 var iface = cl1.Assembly.GetType ("ITest");
3746 var map = cl1.GetInterfaceMap (iface);
3747 Assert.AreEqual (cl1, map.TargetType);
3748 Assert.AreEqual (iface, map.InterfaceType);
3749 Assert.AreEqual (2, map.InterfaceMethods.Length);
3750 Assert.AreEqual (2, map.TargetMethods.Length);
3754 public void StackAlloc_Breakpoints_Regress2775 () {
3755 // Check that breakpoints on arm don't overwrite stackalloc-ed memory
3756 var e = run_until ("regress_2755");
3758 var frame = e.Thread.GetFrames () [0];
3760 // This breaks at the call site
3761 vm.SetBreakpoint (m, m.Locations [2].ILOffset);
3764 var e2 = GetNextEvent ();
3765 Assert.IsTrue (e2 is BreakpointEvent);
3767 e = run_until ("regress_2755_3");
3768 frame = e.Thread.GetFrames () [1];
3769 var res = frame.GetValue (m.GetLocal ("sum"));
3770 AssertValue (0, res);
3774 public void MethodInfo () {
3775 Event e = run_until ("locals2");
3777 StackFrame frame = e.Thread.GetFrames () [0];
3778 var m = frame.Method;
3780 Assert.IsTrue (m.IsGenericMethod);
3781 Assert.IsFalse (m.IsGenericMethodDefinition);
3783 var args = m.GetGenericArguments ();
3784 Assert.AreEqual (1, args.Length);
3785 Assert.AreEqual ("String", args [0].Name);
3787 var gmd = m.GetGenericMethodDefinition ();
3788 Assert.IsTrue (gmd.IsGenericMethod);
3789 Assert.IsTrue (gmd.IsGenericMethodDefinition);
3790 Assert.AreEqual (gmd, gmd.GetGenericMethodDefinition ());
3792 args = gmd.GetGenericArguments ();
3793 Assert.AreEqual (1, args.Length);
3794 Assert.AreEqual ("T", args [0].Name);
3796 var attrs = m.GetCustomAttributes (true);
3797 Assert.AreEqual (1, attrs.Length);
3798 Assert.AreEqual ("StateMachineAttribute", attrs [0].Constructor.DeclaringType.Name);
3802 public void UnhandledException () {
3805 Start (new string [] { "dtest-app.exe", "unhandled-exception" });
3807 var req = vm.CreateExceptionRequest (null, false, true);
3810 var e = run_until ("unhandled_exception");
3813 var e2 = GetNextEvent ();
3814 Assert.IsTrue (e2 is ExceptionEvent);
3821 public void UnhandledException_2 () {
3824 Start (new string [] { "dtest-app.exe", "unhandled-exception-endinvoke" });
3826 var req = vm.CreateExceptionRequest (null, false, true);
3829 MethodMirror m = entry_point.DeclaringType.GetMethod ("unhandled_exception_endinvoke_2");
3830 Assert.IsNotNull (m);
3831 vm.SetBreakpoint (m, m.ILOffsets [0]);
3833 var e = run_until ("unhandled_exception_endinvoke");
3836 var e2 = GetNextEvent ();
3837 Assert.IsFalse (e2 is ExceptionEvent);
3844 public void UnhandledExceptionUserCode () {
3847 // Exceptions caught in non-user code are treated as unhandled
3848 Start (new string [] { "dtest-app.exe", "unhandled-exception-user" });
3850 var req = vm.CreateExceptionRequest (null, false, true);
3851 req.AssemblyFilter = new List<AssemblyMirror> () { entry_point.DeclaringType.Assembly };
3854 var e = run_until ("unhandled_exception_user");
3857 var e2 = GetNextEvent ();
3858 Assert.IsTrue (e2 is ExceptionEvent);
3865 public void GCWhileSuspended () {
3866 // Check that objects are kept alive during suspensions
3867 Event e = run_until ("gc_suspend_1");
3869 MethodMirror m = entry_point.DeclaringType.GetMethod ("gc_suspend_invoke");
3871 var o = entry_point.DeclaringType.GetValue (entry_point.DeclaringType.GetField ("gc_suspend_field")) as ObjectMirror;
3872 //Console.WriteLine (o);
3874 StackFrame frame = e.Thread.GetFrames () [0];
3875 TypeMirror t = frame.Method.DeclaringType;
3876 for (int i = 0; i < 10; ++i)
3877 t.InvokeMethod (e.Thread, m, new Value [] { });
3879 // This throws an exception if the object is collected
3880 long addr = o.Address;
3882 var o2 = entry_point.DeclaringType.GetValue (entry_point.DeclaringType.GetField ("gc_suspend_field")) as ObjectMirror;
3887 public void MakeGenericMethod () {
3888 Event e = run_until ("bp1");
3890 var intm = vm.RootDomain.GetCorrespondingType (typeof (int));
3891 var stringm = vm.RootDomain.GetCorrespondingType (typeof (string));
3892 var gm = entry_point.DeclaringType.GetMethod ("generic_method");
3893 var res = gm.MakeGenericMethod (new TypeMirror [] { stringm });
3894 var args = res.GetGenericArguments ();
3895 Assert.AreEqual (1, args.Length);
3896 Assert.AreEqual (stringm, args [0]);
3899 AssertThrows<ArgumentNullException> (delegate {
3900 gm.MakeGenericMethod (null);
3902 AssertThrows<ArgumentNullException> (delegate {
3903 gm.MakeGenericMethod (new TypeMirror [] { null });
3905 AssertThrows<ArgumentException> (delegate {
3906 gm.MakeGenericMethod (new TypeMirror [] { stringm, stringm });
3908 AssertThrows<InvalidOperationException> (delegate {
3909 gm.MakeGenericMethod (new TypeMirror [] { intm });
3911 AssertThrows<InvalidOperationException> (delegate {
3912 entry_point.DeclaringType.GetMethod ("Main").MakeGenericMethod (new TypeMirror [] { intm });
3917 public void InspectThreadSuspenedOnWaitOne () {
3919 Start (true, "dtest-app.exe", "wait-one" );
3921 ThreadMirror.NativeTransitions = true;
3923 var evt = run_until ("wait_one");
3924 Assert.IsNotNull (evt, "#1");
3926 var thread = evt.Thread;
3927 Assert.AreEqual (ThreadState.Running, thread.ThreadState, "#1.1");
3929 var frames = thread.GetFrames ();
3930 Assert.IsNotNull (frames, "#2");
3931 Assert.AreEqual (2, frames.Length, "#3");
3932 Assert.AreEqual ("wait_one", frames [0].Method.Name, "#4");
3933 Assert.AreEqual ("Main", frames [1].Method.Name, "#5");
3937 Thread.Sleep (500); //FIXME this is racy, maybe single step? or something?
3940 Assert.AreEqual (ThreadState.WaitSleepJoin, thread.ThreadState, "#6");
3942 frames = thread.GetFrames ();
3943 Assert.AreEqual (8, frames.Length, "#7");
3944 Assert.AreEqual ("WaitOne_internal", frames [0].Method.Name, "#8.0");
3945 Assert.AreEqual ("WaitOneNative", frames [1].Method.Name, "#8.1");
3946 Assert.AreEqual ("InternalWaitOne", frames [2].Method.Name, "#8.2");
3947 Assert.AreEqual ("WaitOne", frames [3].Method.Name, "#8.3");
3948 Assert.AreEqual ("WaitOne", frames [4].Method.Name, "#8.4");
3949 Assert.AreEqual ("WaitOne", frames [5].Method.Name, "#8.5");
3950 Assert.AreEqual ("wait_one", frames [6].Method.Name, "#8.6");
3951 Assert.AreEqual ("Main", frames [7].Method.Name, "#8.7");
3953 var frame = frames [0];
3954 Assert.IsTrue (frame.IsNativeTransition, "#11.1");
3957 Assert.Fail ("Known limitation - can't get info from m2n frames");
3958 } catch (AbsentInformationException) {}
3961 Assert.IsFalse (frame.IsNativeTransition, "#12.1");
3962 var wait_one_this = frame.GetThis ();
3963 Assert.IsNotNull (wait_one_this, "#12.2");
3966 var locals = frame.GetVisibleVariables ();
3967 Assert.AreEqual (1, locals.Count, "#13.1");
3969 var local_0 = frame.GetValue (locals [0]);
3970 Assert.IsNotNull (local_0, "#13.2");
3972 Assert.AreEqual (wait_one_this, local_0, "#14.2");
3976 public void GetMethodBody () {
3977 var bevent = run_until ("Main");
3979 var m = bevent.Method.DeclaringType.GetMethod ("get_IntProperty");
3980 var body = m.GetMethodBody ();
3981 foreach (var ins in body.Instructions) {
3982 if (ins.OpCode == OpCodes.Ldfld) {
3983 var field = (FieldInfoMirror)ins.Operand;
3984 Assert.AreEqual ("field_i", field.Name);
3990 public void EvaluateMethod () {
3991 var bevent = run_until ("evaluate_method_2");
3993 var m = bevent.Method.DeclaringType.GetMethod ("get_IntProperty");
3995 var this_obj = bevent.Thread.GetFrames ()[0].GetThis ();
3996 var v = m.Evaluate (this_obj, null);
3997 AssertValue (42, v);
4001 public void SetIP () {
4002 var bevent = run_until ("set_ip_1");
4004 var invalid_loc = bevent.Thread.GetFrames ()[0].Location;
4006 var req = create_step (bevent);
4007 var e = step_out ();
4009 var frames = e.Thread.GetFrames ();
4010 var locs = frames [0].Method.Locations;
4011 var next_loc = locs.First (l => (l.LineNumber == frames [0].Location.LineNumber + 2));
4013 e.Thread.SetIP (next_loc);
4015 /* Check that i ++; j = 5; was skipped */
4016 bevent = run_until ("set_ip_2");
4017 var f = bevent.Thread.GetFrames ()[1];
4018 AssertValue (2, f.GetValue (f.Method.GetLocal ("i")));
4019 AssertValue (0, f.GetValue (f.Method.GetLocal ("j")));
4022 AssertThrows<ArgumentNullException> (delegate {
4023 e.Thread.SetIP (null);
4026 AssertThrows<ArgumentException> (delegate {
4027 e.Thread.SetIP (invalid_loc);
4032 public void SetIPSingleStep () {
4033 // Check that single stepping after set-ip steps from the new ip
4034 var bevent = run_until ("set_ip_1");
4036 var invalid_loc = bevent.Thread.GetFrames ()[0].Location;
4038 var req = create_step (bevent);
4039 req.Size = StepSize.Line;
4040 var e = step_out ();
4042 var frames = e.Thread.GetFrames ();
4043 var locs = frames [0].Method.Locations;
4044 var prev_loc = locs.First (l => (l.LineNumber == frames [0].Location.LineNumber - 3));
4045 AssertValue (2, frames [0].GetValue (frames [0].Method.GetLocal ("i")));
4047 // Set back the ip to the first i ++; line
4048 e.Thread.SetIP (prev_loc);
4051 var f = e.Thread.GetFrames ()[0];
4052 AssertValue (3, f.GetValue (f.Method.GetLocal ("i")));
4056 public void NewInstanceNoCtor () {
4057 var bevent = run_until ("Main");
4059 var stype = bevent.Method.DeclaringType.Assembly.GetType ("AStruct");
4060 var obj = stype.NewInstance ();
4061 Assert.IsTrue (obj is ObjectMirror);
4062 Assert.AreEqual ("AStruct", (obj as ObjectMirror).Type.Name);
4066 public void StaticCtorFilterInCctor () {
4067 // Check that single stepping when in a cctor only ignores
4068 // other cctors, not the current one
4069 var bevent = run_until ("step_filters");
4071 var assembly = entry_point.DeclaringType.Assembly;
4072 var type = assembly.GetType ("Tests/ClassWithCctor");
4073 var cctor = type.GetMethod (".cctor");
4074 vm.SetBreakpoint (cctor, 0);
4077 var e = vm.GetNextEvent ();
4078 Assert.IsTrue (e is BreakpointEvent);
4080 var req = create_step (e);
4081 req.Filter = StepFilter.StaticCtor;
4083 // Make sure we are still in the cctor
4084 Assert.AreEqual (".cctor", e.Thread.GetFrames ()[0].Location.Method.Name);
4088 public void ThreadpoolIOsinglestep () {
4090 Start ("dtest-app.exe", "threadpool-io");
4091 // This is a regression test for #42625. It tests the
4092 // interaction (particularly in coop GC) of the
4093 // threadpool I/O mechanism and the soft debugger.
4094 Event e = run_until ("threadpool_io");
4095 // run until we sent the task half the bytes it
4096 // expects, so that it blocks waiting for the rest.
4097 e = run_until ("threadpool_bp");
4098 var req = create_step (e);
4099 e = step_out (); // leave threadpool_bp
4100 e = step_out (); // leave threadpool_io