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 Event GetNextEvent () {
46 var es = vm.GetNextEventSet ();
47 Assert.AreEqual (1, es.Events.Length);
51 void Start (params string[] args) {
55 void Start (bool forceExit, params string[] args) {
56 this.forceExit = forceExit;
59 var pi = new Diag.ProcessStartInfo ();
62 pi.FileName = runtime;
65 pi.Arguments = String.Join (" ", args);
66 vm = VirtualMachineManager.Launch (pi, new LaunchOptions { AgentArgs = agent_args });
68 var ep = new IPEndPoint (IPAddress.Any, 10000);
69 Console.WriteLine ("Listening on " + ep + "...");
70 vm = VirtualMachineManager.Listen (ep);
73 var load_req = vm.CreateAssemblyLoadRequest ();
76 Event vmstart = GetNextEvent ();
77 Assert.AreEqual (EventType.VMStart, vmstart.EventType);
86 /* Find out the entry point */
90 if (e is AssemblyLoadEvent) {
91 AssemblyLoadEvent ae = (AssemblyLoadEvent)e;
92 entry_point = ae.Assembly.EntryPoint;
93 if (entry_point != null)
103 BreakpointEvent run_until (string name) {
105 MethodMirror m = entry_point.DeclaringType.GetMethod (name);
106 Assert.IsNotNull (m);
107 //Console.WriteLine ("X: " + name + " " + m.ILOffsets.Count + " " + m.Locations.Count);
108 var req = vm.SetBreakpoint (m, m.ILOffsets [0]);
115 if (e is BreakpointEvent)
121 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
122 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
124 return (e as BreakpointEvent);
127 Event single_step (ThreadMirror t) {
128 var req = vm.CreateStepRequest (t);
132 Event e = GetNextEvent ();
133 Assert.IsTrue (e is StepEvent);
140 Event step_until (ThreadMirror t, string method_name) {
144 if ((e as StepEvent).Method.Name == method_name)
150 void check_arg_val (StackFrame frame, int pos, Type type, object eval) {
151 object val = frame.GetArgument (pos);
152 Assert.IsTrue (val is PrimitiveValue);
153 object v = (val as PrimitiveValue).Value;
154 Assert.AreEqual (type, v.GetType ());
156 Assert.IsTrue (Math.Abs ((float)eval - (float)v) < 0.0001);
157 else if (eval is double)
158 Assert.IsTrue (Math.Abs ((double)eval - (double)v) < 0.0001);
160 Assert.AreEqual (eval, v);
163 void AssertValue (object expected, object val) {
164 if (expected is string) {
165 Assert.IsTrue (val is StringMirror);
166 Assert.AreEqual (expected, (val as StringMirror).Value);
167 } else if (val is StructMirror && (val as StructMirror).Type.Name == "IntPtr") {
168 AssertValue (expected, (val as StructMirror).Fields [0]);
170 Assert.IsTrue (val is PrimitiveValue);
171 Assert.AreEqual (expected, (val as PrimitiveValue).Value);
176 public void SetUp () {
177 ThreadMirror.NativeTransitions = false;
178 Start (new string [] { "dtest-app.exe" });
182 public void TearDown () {
186 if (step_req != null)
194 Event e = GetNextEvent ();
196 if (e is VMDeathEvent)
205 public void SimpleBreakpoint () {
208 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp1");
209 Assert.IsNotNull (m);
211 vm.SetBreakpoint (m, 0);
216 Assert.AreEqual (EventType.Breakpoint, e.EventType);
217 Assert.IsTrue (e is BreakpointEvent);
218 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
221 AssertThrows<ArgumentException> (delegate {
223 vm.SetBreakpoint (m, 2);
228 public void BreakpointsSameLocation () {
229 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp2");
230 Assert.IsNotNull (m);
232 vm.SetBreakpoint (m, 0);
233 vm.SetBreakpoint (m, 0);
237 var es = vm.GetNextEventSet ();
238 Assert.AreEqual (2, es.Events.Length);
239 Assert.IsTrue (es [0] is BreakpointEvent);
240 Assert.AreEqual (m, (es [0] as BreakpointEvent).Method);
242 Assert.IsTrue (es [1] is BreakpointEvent);
243 Assert.AreEqual (m.Name, (es [1] as BreakpointEvent).Method.Name);
247 public void BreakpointAlreadyJITted () {
248 Event e = run_until ("bp1");
250 /* Place a breakpoint on bp3 */
251 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp3");
252 Assert.IsNotNull (m);
253 vm.SetBreakpoint (m, 0);
255 /* Same with generic instances */
256 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp7");
257 Assert.IsNotNull (m2);
258 vm.SetBreakpoint (m2, 0);
263 Assert.AreEqual (EventType.Breakpoint, e.EventType);
264 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
268 /* Non-shared instance */
270 Assert.AreEqual (EventType.Breakpoint, e.EventType);
271 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
275 /* Shared instance */
277 Assert.AreEqual (EventType.Breakpoint, e.EventType);
278 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
282 public void ClearBreakpoint () {
285 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
286 Assert.IsNotNull (m);
287 EventRequest req1 = vm.SetBreakpoint (m, 0);
288 EventRequest req2 = vm.SetBreakpoint (m, 0);
290 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
291 Assert.IsNotNull (m2);
292 vm.SetBreakpoint (m2, 0);
297 var es = vm.GetNextEventSet ();
298 Assert.AreEqual (2, es.Events.Length);
299 Assert.AreEqual (EventType.Breakpoint, es [0].EventType);
300 Assert.AreEqual (m.Name, (es [0] as BreakpointEvent).Method.Name);
301 Assert.AreEqual (EventType.Breakpoint, es [1].EventType);
302 Assert.AreEqual (m.Name, (es [1] as BreakpointEvent).Method.Name);
304 /* Clear one of them */
310 Assert.AreEqual (EventType.Breakpoint, e.EventType);
311 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
313 /* Clear the other */
319 Assert.AreEqual (EventType.Breakpoint, e.EventType);
320 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
324 public void ClearAllBreakpoints () {
327 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
328 Assert.IsNotNull (m);
329 vm.SetBreakpoint (m, 0);
331 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
332 Assert.IsNotNull (m2);
333 vm.SetBreakpoint (m2, 0);
335 vm.ClearAllBreakpoints ();
340 Assert.IsTrue (!(e is BreakpointEvent));
341 if (e is VMDeathEvent)
346 public void BreakpointOnGShared () {
349 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp6");
350 Assert.IsNotNull (m);
352 vm.SetBreakpoint (m, 0);
357 Assert.AreEqual (EventType.Breakpoint, e.EventType);
358 Assert.IsTrue (e is BreakpointEvent);
359 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
361 // Breakpoint on an open generic method of a closed generic class (#3422)
362 var frame = e.Thread.GetFrames ()[0];
363 var ginst = frame.GetValue (frame.Method.GetLocal ("gc"));
364 var m2 = (ginst as ObjectMirror).Type.GetMethod ("bp");
365 vm.SetBreakpoint (m2, 0);
370 Assert.AreEqual (EventType.Breakpoint, e.EventType);
371 Assert.IsTrue (e is BreakpointEvent);
372 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
375 void assert_location (Event e, string method) {
376 Assert.IsTrue (e is StepEvent);
377 Assert.AreEqual (method, (e as StepEvent).Method.Name);
380 StepEventRequest create_step (Event e) {
381 var req = vm.CreateStepRequest (e.Thread);
387 public void SingleStepping () {
388 Event e = run_until ("single_stepping");
390 var req = create_step (e);
393 // Step over 'bool b = true'
395 assert_location (e, "single_stepping");
402 assert_location (e, "ss1");
409 assert_location (e, "single_stepping");
413 assert_location (e, "single_stepping");
417 assert_location (e, "ss3");
419 // Step back into single_stepping
421 assert_location (e, "single_stepping");
423 // Step into ss3_2 ()
425 assert_location (e, "ss3_2");
427 // Step over ss3_2_2 ()
429 assert_location (e, "ss3_2");
431 // Recreate the request
438 // Step back into single_stepping () with the new request
440 assert_location (e, "single_stepping");
444 assert_location (e, "ss4");
449 // Change to StepSize.Line
451 req.Depth = StepDepth.Over;
452 req.Size = StepSize.Line;
455 // Step over ss1 (); ss1 ();
460 req.Depth = StepDepth.Into;
464 assert_location (e, "ss2");
469 e = run_until ("ss5");
471 // Add an assembly filter
472 req.AssemblyFilter = new AssemblyMirror [] { (e as BreakpointEvent).Method.DeclaringType.Assembly };
478 // Step into is_even, skipping the linq stuff
480 assert_location (e, "is_even");
482 // FIXME: Check that single stepping works with lock (obj)
486 e = run_until ("ss6");
488 req = create_step (e);
489 req.Depth = StepDepth.Over;
492 // Check that single stepping works in out-of-line bblocks
495 assert_location (e, "ss6");
498 // Check that a step over stops at an EH clause
499 e = run_until ("ss7_2");
500 req = create_step (e);
501 req.Depth = StepDepth.Out;
504 assert_location (e, "ss7");
506 req = create_step (e);
507 req.Depth = StepDepth.Over;
510 assert_location (e, "ss7");
513 // Check that stepping stops between nested calls
514 e = run_until ("ss_nested_2");
516 assert_location (e, "ss_nested");
518 assert_location (e, "ss_nested_1");
520 Console.WriteLine ("A: " + e.Thread.GetFrames ()[0].Location);
521 assert_location (e, "ss_nested");
522 // Check that step over steps over nested calls
524 assert_location (e, "ss_nested");
526 assert_location (e, "ss_nested_3");
530 public void MethodEntryExit () {
531 run_until ("single_stepping");
533 var req1 = vm.CreateMethodEntryRequest ();
534 var req2 = vm.CreateMethodExitRequest ();
540 Event e = GetNextEvent ();
541 Assert.IsTrue (e is MethodEntryEvent);
542 Assert.AreEqual ("ss1", (e as MethodEntryEvent).Method.Name);
546 Assert.IsTrue (e is MethodExitEvent);
547 Assert.AreEqual ("ss1", (e as MethodExitEvent).Method.Name);
554 public void CountFilter () {
555 run_until ("single_stepping");
557 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("ss3");
558 Assert.IsNotNull (m2);
559 vm.SetBreakpoint (m2, 0);
561 var req1 = vm.CreateMethodEntryRequest ();
565 // Enter ss2, ss1 is skipped
567 Event e = GetNextEvent ();
568 Assert.IsTrue (e is MethodEntryEvent);
569 Assert.AreEqual ("ss2", (e as MethodEntryEvent).Method.Name);
571 // Breakpoint on ss3, the entry event is no longer reported
574 Assert.IsTrue (e is BreakpointEvent);
580 public void Arguments () {
583 var e = run_until ("arg1");
585 StackFrame frame = e.Thread.GetFrames () [0];
587 check_arg_val (frame, 0, typeof (sbyte), SByte.MaxValue - 5);
588 check_arg_val (frame, 1, typeof (byte), Byte.MaxValue - 5);
589 check_arg_val (frame, 2, typeof (bool), true);
590 check_arg_val (frame, 3, typeof (short), Int16.MaxValue - 5);
591 check_arg_val (frame, 4, typeof (ushort), UInt16.MaxValue - 5);
592 check_arg_val (frame, 5, typeof (char), 'F');
593 check_arg_val (frame, 6, typeof (int), Int32.MaxValue - 5);
594 check_arg_val (frame, 7, typeof (uint), UInt32.MaxValue - 5);
595 check_arg_val (frame, 8, typeof (long), Int64.MaxValue - 5);
596 check_arg_val (frame, 9, typeof (ulong), UInt64.MaxValue - 5);
597 check_arg_val (frame, 10, typeof (float), 1.2345f);
598 check_arg_val (frame, 11, typeof (double), 6.78910);
600 e = run_until ("arg2");
602 frame = e.Thread.GetFrames () [0];
605 val = frame.GetArgument (0);
606 AssertValue ("FOO", val);
607 Assert.AreEqual ("String", (val as ObjectMirror).Type.Name);
610 val = frame.GetArgument (1);
611 AssertValue (null, val);
614 val = frame.GetArgument (2);
615 AssertValue ("BLA", val);
618 val = frame.GetArgument (3);
619 AssertValue (42, val);
622 val = frame.GetArgument (4);
623 Assert.IsTrue (val is ObjectMirror);
624 Assert.AreEqual ("GClass`1", (val as ObjectMirror).Type.Name);
627 val = frame.GetArgument (5);
628 Assert.IsTrue (val is ObjectMirror);
629 Assert.AreEqual ("Object", (val as ObjectMirror).Type.Name);
631 // this on static methods
632 val = frame.GetThis ();
633 AssertValue (null, val);
635 e = run_until ("arg3");
637 frame = e.Thread.GetFrames () [0];
640 val = frame.GetThis ();
641 Assert.IsTrue (val is ObjectMirror);
642 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
644 // objref in register
645 val = frame.GetArgument (0);
646 AssertValue ("BLA", val);
650 public void Arrays () {
653 var e = run_until ("o2");
655 StackFrame frame = e.Thread.GetFrames () [0];
658 val = frame.GetArgument (0);
659 Assert.IsTrue (val is ArrayMirror);
660 ArrayMirror arr = val as ArrayMirror;
661 Assert.AreEqual (2, arr.Length);
662 AssertValue ("BAR", arr [0]);
663 AssertValue ("BAZ", arr [1]);
665 var vals = arr.GetValues (0, 2);
666 Assert.AreEqual (2, vals.Count);
667 AssertValue ("BAR", vals [0]);
668 AssertValue ("BAZ", vals [1]);
670 arr [0] = vm.RootDomain.CreateString ("ABC");
671 AssertValue ("ABC", arr [0]);
673 arr [0] = vm.CreateValue (null);
674 AssertValue (null, arr [0]);
676 arr.SetValues (0, new Value [] { vm.RootDomain.CreateString ("D1"), vm.RootDomain.CreateString ("D2") });
677 AssertValue ("D1", arr [0]);
678 AssertValue ("D2", arr [1]);
681 val = frame.GetArgument (1);
682 Assert.IsTrue (val is ArrayMirror);
683 arr = val as ArrayMirror;
684 Assert.AreEqual (2, arr.Length);
685 AssertValue (42, arr [0]);
686 AssertValue (43, arr [1]);
689 AssertThrows<IndexOutOfRangeException> (delegate () {
693 AssertThrows<IndexOutOfRangeException> (delegate () {
694 val = arr [Int32.MinValue];
697 AssertThrows<IndexOutOfRangeException> (delegate () {
698 vals = arr.GetValues (0, 3);
701 AssertThrows<IndexOutOfRangeException> (delegate () {
702 arr [2] = vm.CreateValue (null);
705 AssertThrows<IndexOutOfRangeException> (delegate () {
706 arr [Int32.MinValue] = vm.CreateValue (null);
709 AssertThrows<IndexOutOfRangeException> (delegate () {
710 arr.SetValues (0, new Value [] { null, null, null });
714 val = frame.GetArgument (2);
715 Assert.IsTrue (val is ArrayMirror);
716 arr = val as ArrayMirror;
717 Assert.AreEqual (2, arr.Rank);
718 Assert.AreEqual (4, arr.Length);
719 Assert.AreEqual (2, arr.GetLength (0));
720 Assert.AreEqual (2, arr.GetLength (1));
721 Assert.AreEqual (0, arr.GetLowerBound (0));
722 Assert.AreEqual (0, arr.GetLowerBound (1));
723 vals = arr.GetValues (0, 4);
724 AssertValue (1, vals [0]);
725 AssertValue (2, vals [1]);
726 AssertValue (3, vals [2]);
727 AssertValue (4, vals [3]);
729 val = frame.GetArgument (3);
730 Assert.IsTrue (val is ArrayMirror);
731 arr = val as ArrayMirror;
732 Assert.AreEqual (2, arr.Rank);
733 Assert.AreEqual (4, arr.Length);
734 Assert.AreEqual (2, arr.GetLength (0));
735 Assert.AreEqual (2, arr.GetLength (1));
736 Assert.AreEqual (1, arr.GetLowerBound (0));
737 Assert.AreEqual (3, arr.GetLowerBound (1));
739 AssertThrows<ArgumentOutOfRangeException> (delegate () {
742 AssertThrows<ArgumentOutOfRangeException> (delegate () {
746 AssertThrows<ArgumentOutOfRangeException> (delegate () {
747 arr.GetLowerBound (-1);
749 AssertThrows<ArgumentOutOfRangeException> (delegate () {
750 arr.GetLowerBound (2);
753 // arrays treated as generic collections
754 val = frame.GetArgument (4);
755 Assert.IsTrue (val is ArrayMirror);
756 arr = val as ArrayMirror;
760 public void Object_GetValue () {
761 var e = run_until ("o1");
762 var frame = e.Thread.GetFrames () [0];
764 object val = frame.GetThis ();
765 Assert.IsTrue (val is ObjectMirror);
766 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
767 ObjectMirror o = (val as ObjectMirror);
769 TypeMirror t = o.Type;
772 object f = o.GetValue (t.GetField ("field_i"));
774 f = o.GetValue (t.GetField ("field_s"));
775 AssertValue ("S", f);
776 f = o.GetValue (t.GetField ("field_enum"));
777 Assert.IsTrue (f is EnumMirror);
778 Assert.AreEqual (1, (f as EnumMirror).Value);
779 Assert.AreEqual ("B", (f as EnumMirror).StringValue);
781 // Inherited object fields
782 TypeMirror parent = t.BaseType;
783 f = o.GetValue (parent.GetField ("base_field_i"));
785 f = o.GetValue (parent.GetField ("base_field_s"));
786 AssertValue ("T", f);
789 f = o.GetValue (o.Type.GetField ("static_i"));
793 ObjectMirror o2 = frame.GetValue (frame.Method.GetParameters ()[1]) as ObjectMirror;
794 Assert.AreEqual ("GClass`1", o2.Type.Name);
795 TypeMirror t2 = o2.Type;
796 f = o2.GetValue (t2.GetField ("field"));
799 ObjectMirror o3 = frame.GetValue (frame.Method.GetParameters ()[2]) as ObjectMirror;
800 Assert.AreEqual ("GClass`1", o3.Type.Name);
801 TypeMirror t3 = o3.Type;
802 f = o3.GetValue (t3.GetField ("field"));
803 AssertValue ("FOO", f);
806 AssertThrows<ArgumentNullException> (delegate () {
812 public void Object_GetValues () {
813 var e = run_until ("o1");
814 var frame = e.Thread.GetFrames () [0];
816 object val = frame.GetThis ();
817 Assert.IsTrue (val is ObjectMirror);
818 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
819 ObjectMirror o = (val as ObjectMirror);
821 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
823 TypeMirror t = o.Type;
825 object[] vals = o.GetValues (new FieldInfoMirror [] { t.GetField ("field_i"), t.GetField ("field_s") });
829 AssertValue ("S", f);
832 AssertThrows<ArgumentNullException> (delegate () {
836 AssertThrows<ArgumentNullException> (delegate () {
837 o.GetValues (new FieldInfoMirror [] { null });
840 // field of another class
841 AssertThrows<ArgumentException> (delegate () {
842 o.GetValue (val2.Type.GetField ("field_j"));
846 void TestSetValue (ObjectMirror o, string field_name, object val) {
848 o.SetValue (o.Type.GetField (field_name), vm.RootDomain.CreateString ((string)val));
850 o.SetValue (o.Type.GetField (field_name), vm.CreateValue (val));
851 Value f = o.GetValue (o.Type.GetField (field_name));
852 AssertValue (val, f);
856 public void Object_SetValues () {
857 var e = run_until ("o1");
858 var frame = e.Thread.GetFrames () [0];
860 object val = frame.GetThis ();
861 Assert.IsTrue (val is ObjectMirror);
862 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
863 ObjectMirror o = (val as ObjectMirror);
865 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
867 TestSetValue (o, "field_i", 22);
868 TestSetValue (o, "field_bool1", false);
869 TestSetValue (o, "field_bool2", true);
870 TestSetValue (o, "field_char", 'B');
871 TestSetValue (o, "field_byte", (byte)129);
872 TestSetValue (o, "field_sbyte", (sbyte)-33);
873 TestSetValue (o, "field_short", (short)(Int16.MaxValue - 5));
874 TestSetValue (o, "field_ushort", (ushort)(UInt16.MaxValue - 5));
875 TestSetValue (o, "field_long", Int64.MaxValue - 5);
876 TestSetValue (o, "field_ulong", (ulong)(UInt64.MaxValue - 5));
877 TestSetValue (o, "field_float", 6.28f);
878 TestSetValue (o, "field_double", 6.28);
879 TestSetValue (o, "static_i", 23);
880 TestSetValue (o, "field_s", "CDEF");
885 f = o.GetValue (o.Type.GetField ("field_intptr"));
886 Assert.IsInstanceOfType (typeof (StructMirror), f);
887 AssertValue (Int32.MaxValue - 5, (f as StructMirror).Fields [0]);
890 FieldInfoMirror field = o.Type.GetField ("field_enum");
891 f = o.GetValue (field);
892 (f as EnumMirror).Value = 5;
893 o.SetValue (field, f);
894 f = o.GetValue (field);
895 Assert.AreEqual (5, (f as EnumMirror).Value);
898 o.SetValue (o.Type.GetField ("field_s"), vm.CreateValue (null));
899 f = o.GetValue (o.Type.GetField ("field_s"));
900 AssertValue (null, f);
903 field = o.Type.GetField ("generic_field_struct");
904 f = o.GetValue (field);
905 o.SetValue (field, f);
908 field = o.Type.GetField ("field_nullable");
909 f = o.GetValue (field);
910 AssertValue (0, (f as StructMirror).Fields [0]);
911 AssertValue (false, (f as StructMirror).Fields [1]);
912 o.SetValue (field, vm.CreateValue (6));
913 f = o.GetValue (field);
914 AssertValue (6, (f as StructMirror).Fields [0]);
915 AssertValue (true, (f as StructMirror).Fields [1]);
916 o.SetValue (field, vm.CreateValue (null));
917 f = o.GetValue (field);
918 AssertValue (0, (f as StructMirror).Fields [0]);
919 AssertValue (false, (f as StructMirror).Fields [1]);
922 AssertThrows<ArgumentNullException> (delegate () {
923 o.SetValues (null, new Value [0]);
926 AssertThrows<ArgumentNullException> (delegate () {
927 o.SetValues (new FieldInfoMirror [0], null);
930 AssertThrows<ArgumentNullException> (delegate () {
931 o.SetValues (new FieldInfoMirror [] { null }, new Value [1] { null });
934 // vtype with a wrong type
935 AssertThrows<ArgumentException> (delegate () {
936 o.SetValue (o.Type.GetField ("field_struct"), o.GetValue (o.Type.GetField ("field_enum")));
939 // reference type not assignment compatible
940 AssertThrows<ArgumentException> (delegate () {
941 o.SetValue (o.Type.GetField ("field_class"), o);
944 // field of another class
945 AssertThrows<ArgumentException> (delegate () {
946 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
951 public void Type_SetValue () {
952 var e = run_until ("o1");
953 var frame = e.Thread.GetFrames () [0];
956 object val = frame.GetThis ();
957 Assert.IsTrue (val is ObjectMirror);
958 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
959 ObjectMirror o = (val as ObjectMirror);
961 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
963 o.Type.SetValue (o.Type.GetField ("static_i"), vm.CreateValue (55));
964 f = o.Type.GetValue (o.Type.GetField ("static_i"));
967 o.Type.SetValue (o.Type.GetField ("static_s"), vm.RootDomain.CreateString ("B"));
968 f = o.Type.GetValue (o.Type.GetField ("static_s"));
969 AssertValue ("B", f);
972 AssertThrows<ArgumentNullException> (delegate () {
973 o.Type.SetValue (null, vm.CreateValue (0));
976 AssertThrows<ArgumentNullException> (delegate () {
977 o.Type.SetValue (o.Type.GetField ("static_i"), null);
980 // field of another class
981 AssertThrows<ArgumentException> (delegate () {
982 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
987 public void TypeInfo () {
988 Event e = run_until ("ti2");
989 StackFrame frame = e.Thread.GetFrames () [0];
994 t = frame.Method.GetParameters ()[0].ParameterType;
996 Assert.AreEqual ("String[]", t.Name);
997 Assert.AreEqual ("string[]", t.CSharpName);
998 Assert.AreEqual ("Array", t.BaseType.Name);
999 Assert.AreEqual (true, t.HasElementType);
1000 Assert.AreEqual (true, t.IsArray);
1001 Assert.AreEqual (1, t.GetArrayRank ());
1002 Assert.AreEqual ("String", t.GetElementType ().Name);
1004 t = frame.Method.GetParameters ()[2].ParameterType;
1006 Assert.AreEqual ("Int32[,]", t.Name);
1008 //Assert.AreEqual ("int[,]", t.CSharpName);
1009 Assert.AreEqual ("Array", t.BaseType.Name);
1010 Assert.AreEqual (true, t.HasElementType);
1011 Assert.AreEqual (true, t.IsArray);
1012 Assert.AreEqual (2, t.GetArrayRank ());
1013 Assert.AreEqual ("Int32", t.GetElementType ().Name);
1016 t = frame.Method.GetParameters ()[3].ParameterType;
1018 //Assert.AreEqual ("Int32&", t.Name);
1019 //Assert.AreEqual (true, t.IsByRef);
1020 //Assert.AreEqual (true, t.HasElementType);
1023 t = frame.Method.GetParameters ()[4].ParameterType;
1025 //Assert.AreEqual ("Int32*", t.Name);
1026 Assert.AreEqual (true, t.IsPointer);
1027 Assert.AreEqual (true, t.HasElementType);
1028 Assert.AreEqual ("Int32", t.GetElementType ().Name);
1029 Assert.AreEqual (false, t.IsPrimitive);
1032 t = frame.Method.GetParameters ()[5].ParameterType;
1033 Assert.AreEqual (true, t.IsPrimitive);
1036 t = frame.Method.GetParameters ()[6].ParameterType;
1037 Assert.AreEqual ("AStruct", t.Name);
1038 Assert.AreEqual (false, t.IsPrimitive);
1039 Assert.AreEqual (true, t.IsValueType);
1040 Assert.AreEqual (false, t.IsClass);
1043 t = frame.Method.GetParameters ()[7].ParameterType;
1044 Assert.AreEqual ("Tests", t.Name);
1045 var nested = (from nt in t.GetNestedTypes () where nt.IsNestedPublic select nt).ToArray ();
1046 Assert.AreEqual (1, nested.Length);
1047 Assert.AreEqual ("NestedClass", nested [0].Name);
1048 Assert.IsTrue (t.BaseType.IsAssignableFrom (t));
1049 Assert.IsTrue (!t.IsAssignableFrom (t.BaseType));
1051 // generic instances
1052 t = frame.Method.GetParameters ()[9].ParameterType;
1053 Assert.AreEqual ("GClass`1", t.Name);
1054 Assert.IsTrue (t.IsGenericType);
1055 Assert.IsFalse (t.IsGenericTypeDefinition);
1057 var args = t.GetGenericArguments ();
1058 Assert.AreEqual (1, args.Length);
1059 Assert.AreEqual ("Int32", args [0].Name);
1061 // generic type definitions
1062 var gtd = t.GetGenericTypeDefinition ();
1063 Assert.AreEqual ("GClass`1", gtd.Name);
1064 Assert.IsTrue (gtd.IsGenericType);
1065 Assert.IsTrue (gtd.IsGenericTypeDefinition);
1066 Assert.AreEqual (gtd, gtd.GetGenericTypeDefinition ());
1068 args = gtd.GetGenericArguments ();
1069 Assert.AreEqual (1, args.Length);
1070 Assert.AreEqual ("T", args [0].Name);
1073 t = frame.Method.GetParameters ()[10].ParameterType;
1074 Assert.AreEqual ("AnEnum", t.Name);
1075 Assert.IsTrue (t.IsEnum);
1076 Assert.AreEqual ("Int32", t.EnumUnderlyingType.Name);
1079 t = frame.Method.GetParameters ()[7].ParameterType;
1081 var props = t.GetProperties ();
1082 Assert.AreEqual (3, props.Length);
1083 foreach (PropertyInfoMirror prop in props) {
1084 ParameterInfoMirror[] indexes = prop.GetIndexParameters ();
1086 if (prop.Name == "IntProperty") {
1087 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1088 Assert.AreEqual ("get_IntProperty", prop.GetGetMethod ().Name);
1089 Assert.AreEqual ("set_IntProperty", prop.GetSetMethod ().Name);
1090 Assert.AreEqual (0, indexes.Length);
1091 } else if (prop.Name == "ReadOnlyProperty") {
1092 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1093 Assert.AreEqual ("get_ReadOnlyProperty", prop.GetGetMethod ().Name);
1094 Assert.AreEqual (null, prop.GetSetMethod ());
1095 Assert.AreEqual (0, indexes.Length);
1096 } else if (prop.Name == "IndexedProperty") {
1097 Assert.AreEqual (1, indexes.Length);
1098 Assert.AreEqual ("Int32", indexes [0].ParameterType.Name);
1102 // custom attributes
1103 t = frame.Method.GetParameters ()[8].ParameterType;
1104 Assert.AreEqual ("Tests2", t.Name);
1105 var attrs = t.GetCustomAttributes (true);
1106 Assert.AreEqual (3, attrs.Length);
1107 foreach (var attr in attrs) {
1108 if (attr.Constructor.DeclaringType.Name == "DebuggerDisplayAttribute") {
1109 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1110 Assert.AreEqual ("Tests", attr.ConstructorArguments [0].Value);
1111 Assert.AreEqual (2, attr.NamedArguments.Count);
1112 Assert.AreEqual ("Name", attr.NamedArguments [0].Property.Name);
1113 Assert.AreEqual ("FOO", attr.NamedArguments [0].TypedValue.Value);
1114 Assert.AreEqual ("Target", attr.NamedArguments [1].Property.Name);
1115 Assert.IsInstanceOfType (typeof (TypeMirror), attr.NamedArguments [1].TypedValue.Value);
1116 Assert.AreEqual ("Int32", (attr.NamedArguments [1].TypedValue.Value as TypeMirror).Name);
1117 } else if (attr.Constructor.DeclaringType.Name == "DebuggerTypeProxyAttribute") {
1118 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1119 Assert.IsInstanceOfType (typeof (TypeMirror), attr.ConstructorArguments [0].Value);
1120 Assert.AreEqual ("Tests", (attr.ConstructorArguments [0].Value as TypeMirror).Name);
1121 } else if (attr.Constructor.DeclaringType.Name == "BAttribute") {
1122 Assert.AreEqual (2, attr.NamedArguments.Count);
1123 Assert.AreEqual ("afield", attr.NamedArguments [0].Field.Name);
1124 Assert.AreEqual ("bfield", attr.NamedArguments [1].Field.Name);
1126 Assert.Fail (attr.Constructor.DeclaringType.Name);
1130 var assembly = entry_point.DeclaringType.Assembly;
1131 var type = assembly.GetType ("Tests4");
1132 Assert.IsFalse (type.IsInitialized);
1136 public void FieldInfo () {
1137 Event e = run_until ("ti2");
1138 StackFrame frame = e.Thread.GetFrames () [0];
1142 t = frame.Method.GetParameters ()[8].ParameterType;
1143 Assert.AreEqual ("Tests2", t.Name);
1145 var fi = t.GetField ("field_j");
1146 var attrs = fi.GetCustomAttributes (true);
1147 Assert.AreEqual (1, attrs.Length);
1148 var attr = attrs [0];
1149 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1150 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1151 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1152 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1156 public void PropertyInfo () {
1157 Event e = run_until ("ti2");
1158 StackFrame frame = e.Thread.GetFrames () [0];
1162 t = frame.Method.GetParameters ()[8].ParameterType;
1163 Assert.AreEqual ("Tests2", t.Name);
1165 var pi = t.GetProperty ("AProperty");
1166 var attrs = pi.GetCustomAttributes (true);
1167 Assert.AreEqual (1, attrs.Length);
1168 var attr = attrs [0];
1169 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1170 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1171 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1172 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1176 [Category ("only5")]
1177 public void Type_GetValue () {
1178 Event e = run_until ("o1");
1179 StackFrame frame = e.Thread.GetFrames () [0];
1181 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1183 TypeMirror t = o.Type;
1185 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1188 object f = t.GetValue (o.Type.GetField ("static_i"));
1189 AssertValue (55, f);
1191 f = t.GetValue (o.Type.GetField ("static_s"));
1192 AssertValue ("A", f);
1194 // literal static fields
1195 f = t.GetValue (o.Type.GetField ("literal_i"));
1196 AssertValue (56, f);
1198 f = t.GetValue (o.Type.GetField ("literal_s"));
1199 AssertValue ("B", f);
1201 // Inherited static fields
1202 TypeMirror parent = t.BaseType;
1203 f = t.GetValue (parent.GetField ("base_static_i"));
1204 AssertValue (57, f);
1206 f = t.GetValue (parent.GetField ("base_static_s"));
1207 AssertValue ("C", f);
1209 // thread static field
1210 f = t.GetValue (t.GetField ("tls_i"), e.Thread);
1211 AssertValue (42, f);
1213 // Argument checking
1214 AssertThrows<ArgumentNullException> (delegate () {
1219 AssertThrows<ArgumentException> (delegate () {
1220 t.GetValue (o.Type.GetField ("field_i"));
1223 // field on another type
1224 AssertThrows<ArgumentException> (delegate () {
1225 t.GetValue (val2.Type.GetField ("static_field_j"));
1228 // special static field
1229 AssertThrows<ArgumentException> (delegate () {
1230 t.GetValue (t.GetField ("tls_i"));
1235 public void Type_GetValues () {
1236 Event e = run_until ("o1");
1237 StackFrame frame = e.Thread.GetFrames () [0];
1239 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1241 TypeMirror t = o.Type;
1244 object[] vals = t.GetValues (new FieldInfoMirror [] { t.GetField ("static_i"), t.GetField ("static_s") });
1245 object f = vals [0];
1246 AssertValue (55, f);
1249 AssertValue ("A", f);
1251 // Argument checking
1252 AssertThrows<ArgumentNullException> (delegate () {
1256 AssertThrows<ArgumentNullException> (delegate () {
1257 t.GetValues (new FieldInfoMirror [] { null });
1262 public void ObjRefs () {
1263 Event e = run_until ("objrefs1");
1264 StackFrame frame = e.Thread.GetFrames () [0];
1266 ObjectMirror o = frame.GetThis () as ObjectMirror;
1267 ObjectMirror child = o.GetValue (o.Type.GetField ("child")) as ObjectMirror;
1269 Assert.IsTrue (child.Address != 0);
1271 // Check that object references are internalized correctly
1272 Assert.AreEqual (o, frame.GetThis ());
1274 run_until ("objrefs2");
1276 // child should be gc'd now
1277 // This is not deterministic
1278 //Assert.IsTrue (child.IsCollected);
1281 * No longer works since Type is read eagerly
1284 AssertThrows<ObjectCollectedException> (delegate () {
1285 TypeMirror t = child.Type;
1289 AssertThrows<ObjectCollectedException> (delegate () {
1290 long addr = child.Address;
1296 public void Type_GetObject () {
1297 Event e = run_until ("o1");
1298 StackFrame frame = e.Thread.GetFrames () [0];
1300 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1302 TypeMirror t = o.Type;
1304 Assert.AreEqual ("MonoType", t.GetTypeObject ().Type.Name);
1308 public void VTypes () {
1309 Event e = run_until ("vtypes1");
1310 StackFrame frame = e.Thread.GetFrames () [0];
1313 ObjectMirror o = frame.GetThis () as ObjectMirror;
1314 var obj = o.GetValue (o.Type.GetField ("field_struct"));
1315 Assert.IsTrue (obj is StructMirror);
1316 var s = obj as StructMirror;
1317 Assert.AreEqual ("AStruct", s.Type.Name);
1318 AssertValue (42, s ["i"]);
1320 AssertValue ("S", obj);
1321 AssertValue (43, s ["k"]);
1322 obj = o.GetValue (o.Type.GetField ("field_boxed_struct"));
1323 Assert.IsTrue (obj is StructMirror);
1324 s = obj as StructMirror;
1325 Assert.AreEqual ("AStruct", s.Type.Name);
1326 AssertValue (42, s ["i"]);
1328 // Check decoding of nested structs (#14942)
1329 obj = o.GetValue (o.Type.GetField ("nested_struct"));
1330 o.SetValue (o.Type.GetField ("nested_struct"), obj);
1332 // Check round tripping of boxed struct fields (#12354)
1333 obj = o.GetValue (o.Type.GetField ("boxed_struct_field"));
1334 o.SetValue (o.Type.GetField ("boxed_struct_field"), obj);
1335 obj = o.GetValue (o.Type.GetField ("boxed_struct_field"));
1336 s = obj as StructMirror;
1337 AssertValue (1, s ["key"]);
1339 Assert.IsTrue (obj is StructMirror);
1340 s = obj as StructMirror;
1341 AssertValue (42, s ["m_value"]);
1343 // vtypes as arguments
1344 s = frame.GetArgument (0) as StructMirror;
1345 AssertValue (44, s ["i"]);
1347 AssertValue ("T", obj);
1348 AssertValue (45, s ["k"]);
1350 // vtypes as array entries
1351 var arr = frame.GetArgument (1) as ArrayMirror;
1353 Assert.IsTrue (obj is StructMirror);
1354 s = obj as StructMirror;
1355 AssertValue (1, s ["i"]);
1356 AssertValue ("S1", s ["s"]);
1358 Assert.IsTrue (obj is StructMirror);
1359 s = obj as StructMirror;
1360 AssertValue (2, s ["i"]);
1361 AssertValue ("S2", s ["s"]);
1363 // Argument checking
1364 s = frame.GetArgument (0) as StructMirror;
1365 AssertThrows<ArgumentException> (delegate () {
1369 // generic vtype instances
1370 o = frame.GetThis () as ObjectMirror;
1371 obj = o.GetValue (o.Type.GetField ("generic_field_struct"));
1372 Assert.IsTrue (obj is StructMirror);
1373 s = obj as StructMirror;
1374 Assert.AreEqual ("GStruct`1", s.Type.Name);
1375 AssertValue (42, s ["i"]);
1377 // this on vtype methods
1378 e = run_until ("vtypes2");
1379 e = step_until (e.Thread, "foo");
1381 frame = e.Thread.GetFrames () [0];
1383 Assert.AreEqual ("foo", (e as StepEvent).Method.Name);
1384 obj = frame.GetThis ();
1386 Assert.IsTrue (obj is StructMirror);
1387 s = obj as StructMirror;
1388 AssertValue (44, s ["i"]);
1389 AssertValue ("T", s ["s"]);
1390 AssertValue (45, s ["k"]);
1392 // this on static vtype methods
1393 e = run_until ("vtypes3");
1394 e = step_until (e.Thread, "static_foo");
1396 frame = e.Thread.GetFrames () [0];
1398 Assert.AreEqual ("static_foo", (e as StepEvent).Method.Name);
1399 obj = frame.GetThis ();
1400 AssertValue (null, obj);
1404 public void AssemblyInfo () {
1405 Event e = run_until ("single_stepping");
1407 StackFrame frame = e.Thread.GetFrames () [0];
1409 var aname = frame.Method.DeclaringType.Assembly.GetName ();
1410 Assert.AreEqual ("dtest-app, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", aname.ToString ());
1412 ModuleMirror m = frame.Method.DeclaringType.Module;
1414 Assert.AreEqual ("dtest-app.exe", m.Name);
1415 Assert.AreEqual ("dtest-app.exe", m.ScopeName);
1416 Assert.IsTrue (m.FullyQualifiedName.IndexOf ("dtest-app.exe") != -1);
1417 Guid guid = m.ModuleVersionId;
1418 Assert.AreEqual (frame.Method.DeclaringType.Assembly, m.Assembly);
1419 Assert.AreEqual (frame.Method.DeclaringType.Assembly.ManifestModule, m);
1421 // This is no longer true on 4.0
1422 //Assert.AreEqual ("Assembly", frame.Method.DeclaringType.Assembly.GetAssemblyObject ().Type.Name);
1424 TypeMirror t = vm.RootDomain.Corlib.GetType ("System.Diagnostics.DebuggerDisplayAttribute");
1425 Assert.AreEqual ("DebuggerDisplayAttribute", t.Name);
1429 public void LocalsInfo () {
1430 Event e = run_until ("locals2");
1432 StackFrame frame = e.Thread.GetFrames () [0];
1434 var locals = frame.Method.GetLocals ();
1435 Assert.AreEqual (7, locals.Length);
1436 for (int i = 0; i < 7; ++i) {
1437 if (locals [i].Name == "args") {
1438 Assert.IsTrue (locals [i].IsArg);
1439 Assert.AreEqual ("String[]", locals [i].Type.Name);
1440 } else if (locals [i].Name == "arg") {
1441 Assert.IsTrue (locals [i].IsArg);
1442 Assert.AreEqual ("Int32", locals [i].Type.Name);
1443 } else if (locals [i].Name == "i") {
1444 Assert.IsFalse (locals [i].IsArg);
1445 Assert.AreEqual ("Int64", locals [i].Type.Name);
1446 } else if (locals [i].Name == "j") {
1447 Assert.IsFalse (locals [i].IsArg);
1448 Assert.AreEqual ("Int32", locals [i].Type.Name);
1449 } else if (locals [i].Name == "s") {
1450 Assert.IsFalse (locals [i].IsArg);
1451 Assert.AreEqual ("String", locals [i].Type.Name);
1452 } else if (locals [i].Name == "t") {
1454 Assert.IsTrue (locals [i].IsArg);
1455 Assert.AreEqual ("String", locals [i].Type.Name);
1456 } else if (locals [i].Name == "rs") {
1457 Assert.IsTrue (locals [i].IsArg);
1458 Assert.AreEqual ("String", locals [i].Type.Name);
1465 Event step_once () {
1467 var e = GetNextEvent ();
1468 Assert.IsTrue (e is StepEvent);
1472 Event step_into () {
1473 step_req.Disable ();
1474 step_req.Depth = StepDepth.Into;
1476 return step_once ();
1479 Event step_over () {
1480 step_req.Disable ();
1481 step_req.Depth = StepDepth.Over;
1483 return step_once ();
1487 step_req.Disable ();
1488 step_req.Depth = StepDepth.Out;
1490 return step_once ();
1494 public void Locals () {
1495 var be = run_until ("locals1");
1497 StackFrame frame = be.Thread.GetFrames () [0];
1498 MethodMirror m1 = frame.Method;
1500 // Compiler generated byref local
1501 foreach (var l in m1.GetLocals ()) {
1502 // The byval flag is hidden from the type
1503 if (l.Name != "ri" && l.Type.Name == "Double")
1504 AssertValue (null, frame.GetValue (l));
1507 be = run_until ("locals2");
1509 frame = be.Thread.GetFrames () [0];
1511 object val = frame.GetValue (frame.Method.GetLocal ("i"));
1512 AssertValue (0, val);
1514 var req = create_step (be);
1521 var e = step_once ();
1522 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1524 // Execute s = "AB";
1526 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1528 frame = e.Thread.GetFrames () [0];
1530 val = frame.GetValue (frame.Method.GetLocal ("i"));
1531 AssertValue (42, val);
1533 LocalVariable[] locals = frame.Method.GetLocals ();
1534 var vals = frame.GetValues (locals);
1535 Assert.AreEqual (locals.Length, vals.Length);
1536 for (int i = 0; i < locals.Length; ++i) {
1537 if (locals [i].Name == "i")
1538 AssertValue (42, vals [i]);
1539 if (locals [i].Name == "s")
1540 AssertValue ("AB", vals [i]);
1541 if (locals [i].Name == "t")
1542 AssertValue ("ABC", vals [i]);
1545 // Argument checking
1548 AssertThrows<ArgumentNullException> (delegate () {
1549 frame.GetValue ((LocalVariable)null);
1551 // GetValue () local from another method
1552 AssertThrows<ArgumentException> (delegate () {
1553 frame.GetValue (m1.GetLocal ("foo"));
1557 AssertThrows<ArgumentNullException> (delegate () {
1558 frame.GetValue ((ParameterInfoMirror)null);
1560 // GetValue () local from another method
1561 AssertThrows<ArgumentException> (delegate () {
1562 frame.GetValue (m1.GetParameters ()[0]);
1565 // GetValues () null
1566 AssertThrows<ArgumentNullException> (delegate () {
1567 frame.GetValues (null);
1569 // GetValues () embedded null
1570 AssertThrows<ArgumentNullException> (delegate () {
1571 frame.GetValues (new LocalVariable [] { null });
1573 // GetValues () local from another method
1574 AssertThrows<ArgumentException> (delegate () {
1575 frame.GetValues (new LocalVariable [] { m1.GetLocal ("foo") });
1578 AssertThrows<ArgumentException> (delegate () {
1579 val = frame.GetValue (frame.Method.ReturnParameter);
1582 // invalid stack frames
1584 e = GetNextEvent ();
1585 Assert.IsTrue (e is StepEvent);
1586 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1588 AssertThrows<InvalidStackFrameException> (delegate () {
1589 frame.GetValue (frame.Method.GetLocal ("i"));
1595 be = run_until ("locals7");
1597 req = create_step (be);
1603 // Test that locals are initialized
1604 frame = e.Thread.GetFrames () [0];
1605 val = frame.GetValue (frame.Method.GetLocal ("t"));
1606 AssertValue (0, val);
1610 Assert.AreEqual ("locals7", (e as StepEvent).Method.Name);
1614 Assert.AreEqual ("locals7", (e as StepEvent).Method.Name);
1616 frame = e.Thread.GetFrames () [0];
1617 val = frame.GetValue (frame.Method.GetParameters ()[0]);
1618 AssertValue (22, val);
1619 val = frame.GetValue (frame.Method.GetLocal ("t"));
1620 AssertValue (22, val);
1621 val = frame.GetValue (frame.Method.GetLocal ("t2"));
1622 AssertValue (22, val);
1626 public void GetVisibleVariables () {
1627 Event e = run_until ("locals4");
1630 var locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
1631 Assert.AreEqual (2, locals.Count);
1632 var loc = locals.First (l => l.Name == "i");
1633 Assert.AreEqual ("Int64", loc.Type.Name);
1634 loc = locals.First (l => l.Name == "s");
1635 Assert.AreEqual ("String", loc.Type.Name);
1637 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
1638 Assert.AreEqual ("i", loc.Name);
1639 Assert.AreEqual ("Int64", loc.Type.Name);
1641 e = run_until ("locals5");
1644 locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
1645 Assert.AreEqual (2, locals.Count);
1646 loc = locals.First (l => l.Name == "i");
1647 Assert.AreEqual ("String", loc.Type.Name);
1648 loc = locals.First (l => l.Name == "s");
1649 Assert.AreEqual ("String", loc.Type.Name);
1651 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
1652 Assert.AreEqual ("i", loc.Name);
1653 Assert.AreEqual ("String", loc.Type.Name);
1655 // Variable in another scope
1656 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("j");
1657 Assert.IsNull (loc);
1661 public void Exit () {
1666 var e = GetNextEvent ();
1667 Assert.IsInstanceOfType (typeof (VMDeathEvent), e);
1670 /* Could be a remote vm with no process */
1673 Assert.AreEqual (5, p.ExitCode);
1676 AssertThrows<VMDisconnectedException> (delegate () {
1685 public void Dispose () {
1690 var e = GetNextEvent ();
1691 Assert.IsInstanceOfType (typeof (VMDisconnectEvent), e);
1694 /* Could be a remote vm with no process */
1697 Assert.AreEqual (3, p.ExitCode);
1700 AssertThrows<VMDisconnectedException> (delegate () {
1709 public void ColumnNumbers () {
1710 Event e = run_until ("line_numbers");
1712 // FIXME: Merge this with LineNumbers () when its fixed
1714 step_req = create_step (e);
1715 step_req.Depth = StepDepth.Into;
1723 e = GetNextEvent ();
1724 Assert.IsTrue (e is StepEvent);
1725 if (e.Thread.GetFrames ()[0].Method.Name == "ln1")
1729 // Do an additional step over so we are not on the beginning line of the method
1730 step_req.Disable ();
1731 step_req.Depth = StepDepth.Over;
1734 e = GetNextEvent ();
1735 Assert.IsTrue (e is StepEvent);
1737 l = e.Thread.GetFrames ()[0].Location;
1739 Assert.AreEqual (3, l.ColumnNumber);
1741 step_req.Disable ();
1745 // Broken by mcs+runtime changes (#5438)
1746 [Category("NotWorking")]
1747 public void LineNumbers () {
1748 Event e = run_until ("line_numbers");
1750 step_req = create_step (e);
1751 step_req.Depth = StepDepth.Into;
1758 e = GetNextEvent ();
1759 Assert.IsTrue (e is StepEvent);
1761 l = e.Thread.GetFrames ()[0].Location;
1763 Assert.IsTrue (l.SourceFile.IndexOf ("dtest-app.cs") != -1);
1764 Assert.AreEqual ("ln1", l.Method.Name);
1767 using (FileStream fs = new FileStream (l.SourceFile, FileMode.Open, FileAccess.Read)) {
1768 MD5 md5 = MD5.Create ();
1769 var hash = md5.ComputeHash (fs);
1771 for (int i = 0; i < 16; ++i)
1772 Assert.AreEqual (hash [i], l.SourceFileHash [i]);
1775 int line_base = l.LineNumber;
1778 e = GetNextEvent ();
1779 Assert.IsTrue (e is StepEvent);
1780 l = e.Thread.GetFrames ()[0].Location;
1781 Assert.AreEqual ("ln2", l.Method.Name);
1782 Assert.AreEqual (line_base + 6, l.LineNumber);
1785 e = GetNextEvent ();
1786 Assert.IsTrue (e is StepEvent);
1787 l = e.Thread.GetFrames ()[0].Location;
1788 Assert.AreEqual ("ln1", l.Method.Name);
1789 Assert.AreEqual (line_base + 1, l.LineNumber);
1792 e = GetNextEvent ();
1793 Assert.IsTrue (e is StepEvent);
1794 l = e.Thread.GetFrames ()[0].Location;
1795 Assert.AreEqual ("ln3", l.Method.Name);
1796 Assert.AreEqual (line_base + 11, l.LineNumber);
1799 e = GetNextEvent ();
1800 Assert.IsTrue (e is StepEvent);
1801 l = e.Thread.GetFrames ()[0].Location;
1802 Assert.AreEqual ("ln3", l.Method.Name);
1803 Assert.IsTrue (l.SourceFile.EndsWith ("FOO"));
1804 Assert.AreEqual (55, l.LineNumber);
1807 e = GetNextEvent ();
1808 Assert.IsTrue (e is StepEvent);
1809 l = e.Thread.GetFrames ()[0].Location;
1810 Assert.AreEqual ("ln1", l.Method.Name);
1811 Assert.AreEqual (line_base + 2, l.LineNumber);
1813 // GetSourceFiles ()
1814 string[] sources = l.Method.DeclaringType.GetSourceFiles ();
1815 Assert.AreEqual (2, sources.Length);
1816 Assert.AreEqual ("dtest-app.cs", sources [0]);
1817 Assert.AreEqual ("FOO", sources [1]);
1819 sources = l.Method.DeclaringType.GetSourceFiles (true);
1820 Assert.AreEqual (2, sources.Length);
1821 Assert.IsTrue (sources [0].EndsWith ("dtest-app.cs"));
1822 Assert.IsTrue (sources [1].EndsWith ("FOO"));
1826 public void Suspend () {
1829 Start (new string [] { "dtest-app.exe", "suspend-test" });
1831 Event e = run_until ("suspend");
1833 ThreadMirror main = e.Thread;
1841 // The debuggee should be suspended while it is running the infinite loop
1843 StackFrame frame = main.GetFrames ()[0];
1844 Assert.AreEqual ("suspend", frame.Method.Name);
1848 // resuming when not suspended
1849 AssertThrows<InvalidOperationException> (delegate () {
1859 public void AssemblyLoad () {
1860 Event e = run_until ("assembly_load");
1862 var load_req = vm.CreateAssemblyLoadRequest ();
1867 e = GetNextEvent ();
1868 Assert.IsInstanceOfType (typeof (AssemblyLoadEvent), e);
1869 Assert.IsTrue ((e as AssemblyLoadEvent).Assembly.Location.EndsWith ("System.dll"));
1871 var frames = e.Thread.GetFrames ();
1872 Assert.IsTrue (frames.Length > 0);
1873 Assert.AreEqual ("assembly_load", frames [0].Method.Name);
1877 public void CreateValue () {
1880 v = vm.CreateValue (1);
1881 Assert.AreEqual (vm, v.VirtualMachine);
1882 Assert.AreEqual (1, v.Value);
1884 v = vm.CreateValue (null);
1885 Assert.AreEqual (vm, v.VirtualMachine);
1886 Assert.AreEqual (null, v.Value);
1888 // Argument checking
1889 AssertThrows <ArgumentException> (delegate () {
1890 v = vm.CreateValue ("FOO");
1895 public void CreateString () {
1896 StringMirror s = vm.RootDomain.CreateString ("ABC");
1898 Assert.AreEqual (vm, s.VirtualMachine);
1899 Assert.AreEqual ("ABC", s.Value);
1900 Assert.AreEqual (vm.RootDomain, s.Domain);
1903 StringBuilder sb = new StringBuilder ();
1904 for (int i = 0; i < 1024; ++i)
1906 s = vm.RootDomain.CreateString (sb.ToString ());
1908 // Argument checking
1909 AssertThrows <ArgumentNullException> (delegate () {
1910 s = vm.RootDomain.CreateString (null);
1915 public void CreateBoxedValue () {
1916 ObjectMirror o = vm.RootDomain.CreateBoxedValue (new PrimitiveValue (vm, 42));
1918 Assert.AreEqual ("Int32", o.Type.Name);
1919 //AssertValue (42, m.GetValue (o.Type.GetField ("m_value")));
1921 // Argument checking
1922 AssertThrows <ArgumentNullException> (delegate () {
1923 vm.RootDomain.CreateBoxedValue (null);
1926 AssertThrows <ArgumentException> (delegate () {
1927 vm.RootDomain.CreateBoxedValue (o);
1932 public void Invoke () {
1933 Event e = run_until ("invoke1");
1935 StackFrame frame = e.Thread.GetFrames () [0];
1937 TypeMirror t = frame.Method.DeclaringType;
1938 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
1940 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
1946 m = t.GetMethod ("invoke_return_void");
1947 v = this_obj.InvokeMethod (e.Thread, m, null);
1951 m = t.GetMethod ("invoke_return_ref");
1952 v = this_obj.InvokeMethod (e.Thread, m, null);
1953 AssertValue ("ABC", v);
1956 m = t.GetMethod ("invoke_return_null");
1957 v = this_obj.InvokeMethod (e.Thread, m, null);
1958 AssertValue (null, v);
1961 m = t.GetMethod ("invoke_return_primitive");
1962 v = this_obj.InvokeMethod (e.Thread, m, null);
1963 AssertValue (42, v);
1966 m = t.GetMethod ("invoke_return_nullable");
1967 v = this_obj.InvokeMethod (e.Thread, m, null);
1968 Assert.IsInstanceOfType (typeof (StructMirror), v);
1969 var s = v as StructMirror;
1970 AssertValue (42, s.Fields [0]);
1971 AssertValue (true, s.Fields [1]);
1973 // pass nullable as this
1974 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
1975 m = s.Type.GetMethod ("ToString");
1976 v = s.InvokeMethod (e.Thread, m, null);
1978 // return nullable null
1979 m = t.GetMethod ("invoke_return_nullable_null");
1980 v = this_obj.InvokeMethod (e.Thread, m, null);
1981 Assert.IsInstanceOfType (typeof (StructMirror), v);
1982 s = v as StructMirror;
1983 AssertValue (0, s.Fields [0]);
1984 AssertValue (false, s.Fields [1]);
1986 // pass nullable as this
1987 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
1988 m = s.Type.GetMethod ("ToString");
1989 v = s.InvokeMethod (e.Thread, m, null);
1992 m = t.GetMethod ("invoke_pass_primitive");
1993 Value[] args = new Value [] {
1994 vm.CreateValue ((byte)Byte.MaxValue),
1995 vm.CreateValue ((sbyte)SByte.MaxValue),
1996 vm.CreateValue ((short)1),
1997 vm.CreateValue ((ushort)1),
1998 vm.CreateValue ((int)1),
1999 vm.CreateValue ((uint)1),
2000 vm.CreateValue ((long)1),
2001 vm.CreateValue ((ulong)1),
2002 vm.CreateValue ('A'),
2003 vm.CreateValue (true),
2004 vm.CreateValue (3.14f),
2005 vm.CreateValue (3.14) };
2007 v = this_obj.InvokeMethod (e.Thread, m, args);
2008 AssertValue ((int)Byte.MaxValue + (int)SByte.MaxValue + 1 + 1 + 1 + 1 + 1 + 1 + 'A' + 1 + 3 + 3, v);
2011 m = t.GetMethod ("invoke_pass_ref");
2012 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2013 AssertValue ("ABC", v);
2016 m = t.GetMethod ("invoke_pass_ref");
2017 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (null) });
2018 AssertValue (null, v);
2021 m = t.GetMethod ("invoke_static_pass_ref");
2022 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2023 AssertValue ("ABC", v);
2025 // static invoked using ObjectMirror.InvokeMethod
2026 m = t.GetMethod ("invoke_static_pass_ref");
2027 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2028 AssertValue ("ABC", v);
2030 // method which throws an exception
2032 m = t.GetMethod ("invoke_throws");
2033 v = this_obj.InvokeMethod (e.Thread, m, null);
2035 } catch (InvocationException ex) {
2036 Assert.AreEqual ("Exception", ex.Exception.Type.Name);
2040 m = t.GetMethod (".ctor");
2041 v = t.InvokeMethod (e.Thread, m, null);
2042 Assert.IsInstanceOfType (typeof (ObjectMirror), v);
2043 Assert.AreEqual ("Tests", (v as ObjectMirror).Type.Name);
2046 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("ITest2");
2047 m = cl1.GetMethod ("invoke_iface");
2048 v = this_obj.InvokeMethod (e.Thread, m, null);
2049 AssertValue (42, v);
2051 // Argument checking
2054 AssertThrows<ArgumentNullException> (delegate {
2055 m = t.GetMethod ("invoke_pass_ref");
2056 v = this_obj.InvokeMethod (null, m, new Value [] { vm.CreateValue (null) });
2060 AssertThrows<ArgumentNullException> (delegate {
2061 v = this_obj.InvokeMethod (e.Thread, null, new Value [] { vm.CreateValue (null) });
2064 // invalid number of arguments
2065 m = t.GetMethod ("invoke_pass_ref");
2066 AssertThrows<ArgumentException> (delegate {
2067 v = this_obj.InvokeMethod (e.Thread, m, null);
2070 // invalid type of argument (ref != primitive)
2071 m = t.GetMethod ("invoke_pass_ref");
2072 AssertThrows<ArgumentException> (delegate {
2073 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2076 // invalid type of argument (primitive != primitive)
2077 m = t.GetMethod ("invoke_pass_primitive_2");
2078 AssertThrows<ArgumentException> (delegate {
2079 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2082 // invoking a non-static method as static
2083 m = t.GetMethod ("invoke_pass_ref");
2084 AssertThrows<ArgumentException> (delegate {
2085 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2088 // invoking a method defined in another class
2089 m = t2.GetMethod ("invoke");
2090 AssertThrows<ArgumentException> (delegate {
2091 v = this_obj.InvokeMethod (e.Thread, m, null);
2096 public void InvokeVType () {
2097 Event e = run_until ("invoke1");
2099 StackFrame frame = e.Thread.GetFrames () [0];
2101 var s = frame.GetArgument (1) as StructMirror;
2103 TypeMirror t = s.Type;
2108 // Pass struct as this, receive int
2109 m = t.GetMethod ("invoke_return_int");
2110 v = s.InvokeMethod (e.Thread, m, null);
2111 AssertValue (42, v);
2113 // Pass struct as this, receive intptr
2114 m = t.GetMethod ("invoke_return_intptr");
2115 v = s.InvokeMethod (e.Thread, m, null);
2116 AssertValue (43, v);
2119 m = t.GetMethod ("invoke_static");
2120 v = t.InvokeMethod (e.Thread, m, null);
2123 // Pass generic struct as this
2124 s = frame.GetArgument (2) as StructMirror;
2126 m = t.GetMethod ("invoke_return_int");
2127 v = s.InvokeMethod (e.Thread, m, null);
2128 AssertValue (42, v);
2132 public void BreakpointDuringInvoke () {
2133 Event e = run_until ("invoke1");
2135 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke2");
2136 Assert.IsNotNull (m);
2137 vm.SetBreakpoint (m, 0);
2139 StackFrame frame = e.Thread.GetFrames () [0];
2140 var o = frame.GetThis () as ObjectMirror;
2142 bool failed = false;
2144 bool finished = false;
2145 object wait = new object ();
2147 // Have to invoke in a separate thread as the invoke is suspended until we
2148 // resume after the breakpoint
2149 Thread t = new Thread (delegate () {
2151 o.InvokeMethod (e.Thread, m, null);
2157 Monitor.Pulse (wait);
2163 StackFrame invoke_frame = null;
2166 e = GetNextEvent ();
2167 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
2168 // Check stack trace support and invokes
2169 var frames = e.Thread.GetFrames ();
2170 invoke_frame = frames [0];
2171 Assert.AreEqual ("invoke2", frames [0].Method.Name);
2172 Assert.IsTrue (frames [0].IsDebuggerInvoke);
2173 Assert.AreEqual ("invoke1", frames [1].Method.Name);
2180 Monitor.Wait (wait);
2183 // Check that the invoke frames are no longer valid
2184 AssertThrows<InvalidStackFrameException> (delegate {
2185 invoke_frame.GetThis ();
2188 // Check InvokeOptions.DisableBreakpoints flag
2189 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2193 public void DisabledExceptionDuringInvoke () {
2194 Event e = run_until ("invoke_ex");
2196 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_ex_inner");
2198 StackFrame frame = e.Thread.GetFrames () [0];
2199 var o = frame.GetThis () as ObjectMirror;
2201 var req = vm.CreateExceptionRequest (null);
2204 // Check InvokeOptions.DisableBreakpoints flag
2205 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2211 public void InvokeSingleThreaded () {
2214 Start (new string [] { "dtest-app.exe", "invoke-single-threaded" });
2216 Event e = run_until ("invoke_single_threaded_2");
2218 StackFrame f = e.Thread.GetFrames ()[0];
2220 var obj = f.GetThis () as ObjectMirror;
2222 // Check that the counter value incremented by the other thread does not increase
2223 // during the invoke.
2224 object counter1 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2226 var m = obj.Type.GetMethod ("invoke_return_void");
2227 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2229 object counter2 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2231 Assert.AreEqual ((int)counter1, (int)counter2);
2233 // Test multiple invokes done in succession
2234 m = obj.Type.GetMethod ("invoke_return_void");
2235 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2237 // Test events during single-threaded invokes
2238 vm.EnableEvents (EventType.TypeLoad);
2239 m = obj.Type.GetMethod ("invoke_type_load");
2240 obj.BeginInvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded, delegate {
2244 e = GetNextEvent ();
2245 Assert.AreEqual (EventType.TypeLoad, e.EventType);
2248 List<Value> invoke_results;
2251 public void InvokeMultiple () {
2252 Event e = run_until ("invoke1");
2254 StackFrame frame = e.Thread.GetFrames () [0];
2256 TypeMirror t = frame.Method.DeclaringType;
2257 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2259 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2261 var methods = new MethodMirror [2];
2262 methods [0] = t.GetMethod ("invoke_return_ref");
2263 methods [1] = t.GetMethod ("invoke_return_primitive");
2265 invoke_results = new List<Value> ();
2267 var r = this_obj.BeginInvokeMultiple (e.Thread, methods, null, InvokeOptions.SingleThreaded, invoke_multiple_cb, this_obj);
2268 WaitHandle.WaitAll (new WaitHandle[] { r.AsyncWaitHandle });
2269 this_obj.EndInvokeMultiple (r);
2270 // The callback might still be running
2271 while (invoke_results.Count < 2) {
2274 if (invoke_results [0] is PrimitiveValue) {
2275 AssertValue ("ABC", invoke_results [1]);
2276 AssertValue (42, invoke_results [0]);
2278 AssertValue ("ABC", invoke_results [0]);
2279 AssertValue (42, invoke_results [1]);
2283 void invoke_multiple_cb (IAsyncResult ar) {
2284 ObjectMirror this_obj = (ObjectMirror)ar.AsyncState;
2286 var res = this_obj.EndInvokeMethod (ar);
2287 lock (invoke_results)
2288 invoke_results.Add (res);
2292 public void GetThreads () {
2297 public void Threads () {
2298 Event e = run_until ("threads");
2300 Assert.AreEqual (ThreadState.Running, e.Thread.ThreadState);
2302 Assert.IsTrue (e.Thread.ThreadId > 0);
2304 Assert.AreEqual (e.Thread.TID, e.Thread.TID);
2306 vm.EnableEvents (EventType.ThreadStart, EventType.ThreadDeath);
2310 e = GetNextEvent ();
2311 Assert.IsInstanceOfType (typeof (ThreadStartEvent), e);
2312 var state = e.Thread.ThreadState;
2313 Assert.IsTrue (state == ThreadState.Running || state == ThreadState.Unstarted);
2317 e = GetNextEvent ();
2318 Assert.IsInstanceOfType (typeof (ThreadDeathEvent), e);
2319 Assert.AreEqual (ThreadState.Stopped, e.Thread.ThreadState);
2323 public void Frame_SetValue () {
2324 Event e = run_until ("locals2");
2326 StackFrame frame = e.Thread.GetFrames () [0];
2329 var l = frame.Method.GetLocal ("i");
2330 frame.SetValue (l, vm.CreateValue ((long)55));
2331 AssertValue (55, frame.GetValue (l));
2334 l = frame.Method.GetLocal ("s");
2335 frame.SetValue (l, vm.RootDomain.CreateString ("DEF"));
2336 AssertValue ("DEF", frame.GetValue (l));
2338 // argument as local
2339 l = frame.Method.GetLocal ("arg");
2340 frame.SetValue (l, vm.CreateValue (6));
2341 AssertValue (6, frame.GetValue (l));
2344 var p = frame.Method.GetParameters ()[1];
2345 frame.SetValue (p, vm.CreateValue (7));
2346 AssertValue (7, frame.GetValue (p));
2349 p = frame.Method.GetParameters ()[2];
2350 frame.SetValue (p, vm.RootDomain.CreateString ("DEF"));
2351 AssertValue ("DEF", frame.GetValue (p));
2354 p = frame.Method.GetParameters ()[3];
2355 frame.SetValue (p, vm.RootDomain.CreateString ("DEF2"));
2356 AssertValue ("DEF2", frame.GetValue (p));
2358 // argument checking
2361 AssertThrows<ArgumentNullException> (delegate () {
2362 frame.SetValue ((LocalVariable)null, vm.CreateValue (55));
2366 AssertThrows<ArgumentNullException> (delegate () {
2367 l = frame.Method.GetLocal ("i");
2368 frame.SetValue (l, null);
2371 // value of invalid type
2372 AssertThrows<ArgumentException> (delegate () {
2373 l = frame.Method.GetLocal ("i");
2374 frame.SetValue (l, vm.CreateValue (55));
2380 public void Frame_SetValue_Registers () {
2381 Event e = run_until ("locals6_1");
2383 StackFrame frame = e.Thread.GetFrames () [1];
2386 var l = frame.Method.GetLocal ("j");
2387 frame.SetValue (l, vm.CreateValue (99));
2388 AssertValue (99, frame.GetValue (l));
2390 // Check it during execution
2391 e = run_until ("locals6_2");
2392 frame = e.Thread.GetFrames () [0];
2393 AssertValue (99, frame.GetValue (frame.Method.GetParameters ()[0]));
2395 // Set it while in a frame which clobbers its register
2396 e = run_until ("locals6_3");
2397 frame = e.Thread.GetFrames () [1];
2398 frame.SetValue (l, vm.CreateValue (100));
2399 AssertValue (100, frame.GetValue (l));
2401 // Check it during execution
2402 e = run_until ("locals6_4");
2403 frame = e.Thread.GetFrames () [0];
2404 AssertValue (100, frame.GetValue (frame.Method.GetParameters ()[0]));
2406 // Signed byte value
2407 e = run_until ("locals6_5");
2408 frame = e.Thread.GetFrames () [1];
2409 var l2 = frame.Method.GetLocal ("sb");
2410 frame.SetValue (l2, vm.CreateValue ((sbyte)-99));
2411 AssertValue (-99, frame.GetValue (l2));
2413 // Check it during execution
2414 e = run_until ("locals6_6");
2415 frame = e.Thread.GetFrames () [0];
2416 AssertValue (-99, frame.GetValue (frame.Method.GetParameters ()[0]));
2420 public void InvokeRegress () {
2421 Event e = run_until ("invoke1");
2423 StackFrame frame = e.Thread.GetFrames () [0];
2425 TypeMirror t = frame.Method.DeclaringType;
2426 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2428 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2434 m = t.GetMethod ("invoke_return_void");
2435 v = this_obj.InvokeMethod (e.Thread, m, null);
2438 // Check that the stack frames remain valid during the invoke
2439 Assert.AreEqual ("Tests", (frame.GetThis () as ObjectMirror).Type.Name);
2441 // do another invoke
2442 m = t.GetMethod ("invoke_return_void");
2443 v = this_obj.InvokeMethod (e.Thread, m, null);
2446 // Try a single step after the invoke
2447 var req = create_step (e);
2448 req.Depth = StepDepth.Into;
2449 req.Size = StepSize.Line;
2455 // Step into invoke2
2457 e = GetNextEvent ();
2458 Assert.IsTrue (e is StepEvent);
2459 Assert.AreEqual ("invoke2", (e as StepEvent).Method.Name);
2463 frame = e.Thread.GetFrames () [0];
2467 public void Exceptions () {
2468 Event e = run_until ("exceptions");
2469 var req = vm.CreateExceptionRequest (null);
2474 e = GetNextEvent ();
2475 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2476 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2478 var frames = e.Thread.GetFrames ();
2479 Assert.AreEqual ("exceptions", frames [0].Method.Name);
2482 // exception type filter
2484 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.ArgumentException"));
2487 // Skip the throwing of the second OverflowException
2490 e = GetNextEvent ();
2491 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2492 Assert.AreEqual ("ArgumentException", (e as ExceptionEvent).Exception.Type.Name);
2495 // exception type filter for subclasses
2496 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.Exception"));
2501 e = GetNextEvent ();
2502 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2503 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2506 // Implicit exceptions
2507 req = vm.CreateExceptionRequest (null);
2512 e = GetNextEvent ();
2513 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2514 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
2517 // Single stepping after an exception
2518 req = vm.CreateExceptionRequest (null);
2523 e = GetNextEvent ();
2524 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2525 Assert.AreEqual ("Exception", (e as ExceptionEvent).Exception.Type.Name);
2526 frames = e.Thread.GetFrames ();
2527 Assert.AreEqual ("exceptions2", frames [0].Method.Name);
2530 var sreq = create_step (e);
2531 sreq.Depth = StepDepth.Over;
2532 sreq.Size = StepSize.Line;
2536 e = GetNextEvent ();
2537 Assert.IsInstanceOfType (typeof (StepEvent), e);
2538 frames = e.Thread.GetFrames ();
2539 Assert.AreEqual ("exceptions", frames [0].Method.Name);
2542 // Argument checking
2543 AssertThrows<ArgumentException> (delegate {
2544 vm.CreateExceptionRequest (e.Thread.Type);
2549 public void ExceptionFilter () {
2550 Event e = run_until ("exception_filter");
2552 MethodMirror m = entry_point.DeclaringType.GetMethod ("exception_filter_filter");
2553 Assert.IsNotNull (m);
2555 vm.SetBreakpoint (m, 0);
2559 e = GetNextEvent ();
2560 Assert.AreEqual (EventType.Breakpoint, e.EventType);
2561 Assert.IsTrue (e is BreakpointEvent);
2562 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
2564 var frames = e.Thread.GetFrames ();
2566 Assert.IsTrue (frames [0].Location.SourceFile.IndexOf ("dtest-app.cs") != -1);
2567 Assert.AreEqual ("exception_filter_filter", frames [0].Location.Method.Name);
2569 Assert.AreEqual (0, frames [1].Location.Method.MetadataToken);
2570 Assert.AreEqual (0x0f, frames [1].Location.ILOffset);
2572 Assert.AreEqual ("exception_filter_method", frames [2].Location.Method.Name);
2573 Assert.AreEqual (0x06, frames [2].Location.ILOffset);
2575 Assert.AreEqual (0, frames [3].Location.Method.MetadataToken, 0);
2576 Assert.AreEqual (0, frames [3].Location.ILOffset);
2578 Assert.AreEqual ("exception_filter", frames [4].Location.Method.Name);
2582 public void ExceptionFilter2 () {
2585 Start (new string [] { "dtest-excfilter.exe" });
2587 MethodMirror filter_method = entry_point.DeclaringType.GetMethod ("Filter");
2588 Assert.IsNotNull (filter_method);
2590 MethodMirror test_method = entry_point.DeclaringType.GetMethod ("Test");
2591 Assert.IsNotNull (test_method);
2593 vm.SetBreakpoint (filter_method, 0);
2597 var e = GetNextEvent ();
2598 Assert.AreEqual (EventType.Breakpoint, e.EventType);
2599 Assert.IsTrue (e is BreakpointEvent);
2600 Assert.AreEqual (filter_method.Name, (e as BreakpointEvent).Method.Name);
2602 var frames = e.Thread.GetFrames ();
2604 Assert.AreEqual (4, frames.Count ());
2606 Assert.AreEqual (filter_method.Name, frames [0].Location.Method.Name);
2607 Assert.AreEqual (20, frames [0].Location.LineNumber);
2608 Assert.AreEqual (0, frames [0].Location.ILOffset);
2610 Assert.AreEqual (test_method.Name, frames [1].Location.Method.Name);
2611 Assert.AreEqual (37, frames [1].Location.LineNumber);
2612 Assert.AreEqual (0x0b, frames [1].Location.ILOffset);
2614 Assert.AreEqual (test_method.Name, frames [2].Location.Method.Name);
2615 Assert.AreEqual (33, frames [2].Location.LineNumber);
2616 Assert.AreEqual (0x05, frames [2].Location.ILOffset);
2618 Assert.AreEqual (entry_point.Name, frames [3].Location.Method.Name);
2619 Assert.AreEqual (14, frames [3].Location.LineNumber);
2620 Assert.AreEqual (0x00, frames [3].Location.ILOffset);
2628 public void EventSets () {
2630 // Create two filter which both match the same exception
2632 Event e = run_until ("exceptions");
2634 var req = vm.CreateExceptionRequest (null);
2637 var req2 = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.OverflowException"));
2642 var es = vm.GetNextEventSet ();
2643 Assert.AreEqual (2, es.Events.Length);
2646 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2647 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2650 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2651 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2658 // Test single threaded invokes during processing of nullref exceptions.
2659 // These won't work if the exception handling is done from the sigsegv signal
2660 // handler, since the sigsegv signal is disabled until control returns from the
2664 [Category ("only3")]
2665 public void NullRefExceptionAndSingleThreadedInvoke () {
2666 Event e = run_until ("exceptions");
2667 var req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.NullReferenceException"));
2672 e = GetNextEvent ();
2673 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2674 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
2676 var ex = (e as ExceptionEvent).Exception;
2677 var tostring_method = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2678 ex.InvokeMethod (e.Thread, tostring_method, null, InvokeOptions.SingleThreaded);
2682 public void Domains () {
2685 Start (new string [] { "dtest-app.exe", "domain-test" });
2687 vm.EnableEvents (EventType.AppDomainCreate, EventType.AppDomainUnload, EventType.AssemblyUnload);
2689 Event e = run_until ("domains");
2693 e = GetNextEvent ();
2694 Assert.IsInstanceOfType (typeof (AppDomainCreateEvent), e);
2696 var domain = (e as AppDomainCreateEvent).Domain;
2698 // Check the object type
2699 e = run_until ("domains_2");
2700 var frame = e.Thread.GetFrames ()[0];
2701 var o = frame.GetArgument (0) as ObjectMirror;
2702 Assert.AreEqual ("CrossDomain", o.Type.Name);
2704 // Do a remoting invoke
2705 var cross_domain_type = o.Type;
2706 var v = o.InvokeMethod (e.Thread, cross_domain_type.GetMethod ("invoke_3"), null);
2707 AssertValue (42, v);
2709 // Run until the callback in the domain
2710 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_in_domain");
2711 Assert.IsNotNull (m);
2712 vm.SetBreakpoint (m, 0);
2716 e = GetNextEvent ();
2717 if (e is BreakpointEvent)
2721 Assert.AreEqual ("invoke_in_domain", (e as BreakpointEvent).Method.Name);
2723 // d_method is from another domain
2724 MethodMirror d_method = (e as BreakpointEvent).Method;
2725 Assert.IsTrue (m != d_method);
2727 var frames = e.Thread.GetFrames ();
2728 Assert.AreEqual ("invoke_in_domain", frames [0].Method.Name);
2729 Assert.AreEqual ("invoke", frames [1].Method.Name);
2730 Assert.AreEqual ("domains", frames [2].Method.Name);
2732 // Test breakpoints on already JITted methods in other domains
2733 m = entry_point.DeclaringType.GetMethod ("invoke_in_domain_2");
2734 Assert.IsNotNull (m);
2735 vm.SetBreakpoint (m, 0);
2739 e = GetNextEvent ();
2740 if (e is BreakpointEvent)
2744 Assert.AreEqual ("invoke_in_domain_2", (e as BreakpointEvent).Method.Name);
2746 // This is empty when receiving the AppDomainCreateEvent
2747 Assert.AreEqual ("domain", domain.FriendlyName);
2749 // Run until the unload
2752 e = GetNextEvent ();
2753 if (e is AssemblyUnloadEvent) {
2759 Assert.IsInstanceOfType (typeof (AppDomainUnloadEvent), e);
2760 Assert.AreEqual (domain, (e as AppDomainUnloadEvent).Domain);
2762 // Run past the unload
2763 e = run_until ("domains_3");
2765 // Test access to unloaded types
2766 // FIXME: Add an exception type for this
2767 AssertThrows<Exception> (delegate {
2768 d_method.DeclaringType.GetValue (d_method.DeclaringType.GetField ("static_i"));
2773 public void DynamicMethods () {
2774 Event e = run_until ("dyn_call");
2776 var m = e.Thread.GetFrames ()[1].Method;
2777 Assert.AreEqual ("dyn_method", m.Name);
2779 // Test access to IL
2780 var body = m.GetMethodBody ();
2782 ILInstruction ins = body.Instructions [0];
2783 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
2784 Assert.AreEqual ("FOO", ins.Operand);
2788 public void RefEmit () {
2791 Start (new string [] { "dtest-app.exe", "ref-emit-test" });
2793 Event e = run_until ("ref_emit_call");
2795 var m = e.Thread.GetFrames ()[1].Method;
2796 Assert.AreEqual ("ref_emit_method", m.Name);
2798 // Test access to IL
2799 var body = m.GetMethodBody ();
2803 ins = body.Instructions [0];
2804 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
2805 Assert.AreEqual ("FOO", ins.Operand);
2807 ins = body.Instructions [1];
2808 Assert.AreEqual (OpCodes.Call, ins.OpCode);
2809 Assert.IsInstanceOfType (typeof (MethodMirror), ins.Operand);
2810 Assert.AreEqual ("ref_emit_call", (ins.Operand as MethodMirror).Name);
2814 public void IsAttached () {
2815 var f = entry_point.DeclaringType.GetField ("is_attached");
2817 Event e = run_until ("Main");
2819 AssertValue (true, entry_point.DeclaringType.GetValue (f));
2823 public void StackTraceInNative () {
2824 // Check that stack traces can be produced for threads in native code
2827 Start (new string [] { "dtest-app.exe", "frames-in-native" });
2829 var e = run_until ("frames_in_native");
2831 // FIXME: This is racy
2838 StackFrame[] frames = e.Thread.GetFrames ();
2840 int frame_index = -1;
2841 for (int i = 0; i < frames.Length; ++i) {
2842 if (frames [i].Method.Name == "Sleep") {
2848 Assert.IsTrue (frame_index != -1);
2849 Assert.AreEqual ("Sleep", frames [frame_index].Method.Name);
2850 Assert.AreEqual ("frames_in_native", frames [frame_index + 1].Method.Name);
2851 Assert.AreEqual ("Main", frames [frame_index + 2].Method.Name);
2853 // Check that invokes are disabled for such threads
2854 TypeMirror t = frames [frame_index + 1].Method.DeclaringType;
2856 var m = t.GetMethod ("invoke_static_return_void");
2857 AssertThrows<InvalidOperationException> (delegate {
2858 t.InvokeMethod (e.Thread, m, null);
2861 // Check that the frame info is invalidated
2862 run_until ("frames_in_native_2");
2864 AssertThrows<InvalidStackFrameException> (delegate {
2865 Console.WriteLine (frames [frame_index].GetThis ());
2870 public void VirtualMachine_CreateEnumMirror () {
2871 var e = run_until ("o1");
2872 var frame = e.Thread.GetFrames () [0];
2874 object val = frame.GetThis ();
2875 Assert.IsTrue (val is ObjectMirror);
2876 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
2877 ObjectMirror o = (val as ObjectMirror);
2879 FieldInfoMirror field = o.Type.GetField ("field_enum");
2880 Value f = o.GetValue (field);
2881 TypeMirror enumType = (f as EnumMirror).Type;
2883 o.SetValue (field, vm.CreateEnumMirror (enumType, vm.CreateValue (1)));
2884 f = o.GetValue (field);
2885 Assert.AreEqual (1, (f as EnumMirror).Value);
2887 // Argument checking
2888 AssertThrows<ArgumentNullException> (delegate () {
2889 vm.CreateEnumMirror (enumType, null);
2892 AssertThrows<ArgumentNullException> (delegate () {
2893 vm.CreateEnumMirror (null, vm.CreateValue (1));
2897 AssertThrows<ArgumentException> (delegate () {
2898 vm.CreateEnumMirror (enumType, vm.CreateValue (null));
2901 // value of a wrong type
2902 AssertThrows<ArgumentException> (delegate () {
2903 vm.CreateEnumMirror (enumType, vm.CreateValue ((long)1));
2908 public void VirtualMachine_EnableEvents_Breakpoint () {
2909 AssertThrows<ArgumentException> (delegate () {
2910 vm.EnableEvents (EventType.Breakpoint);
2915 public void SingleStepRegress654694 () {
2918 MethodMirror m = entry_point.DeclaringType.GetMethod ("ss_regress_654694");
2919 foreach (Location l in m.Locations) {
2920 if (l.ILOffset > 0 && il_offset == -1)
2921 il_offset = l.ILOffset;
2924 Event e = run_until ("ss_regress_654694");
2926 Assert.IsNotNull (m);
2927 vm.SetBreakpoint (m, il_offset);
2931 e = GetNextEvent ();
2932 Assert.IsTrue (e is BreakpointEvent);
2934 var req = create_step (e);
2935 req.Depth = StepDepth.Over;
2936 req.Size = StepSize.Line;
2941 e = GetNextEvent ();
2942 Assert.IsTrue (e is StepEvent);
2948 public void DebugBreak () {
2949 vm.EnableEvents (EventType.UserBreak);
2954 var e = GetNextEvent ();
2955 Assert.IsTrue (e is UserBreakEvent);
2959 public void DebugLog () {
2960 vm.EnableEvents (EventType.UserLog);
2965 var e = GetNextEvent ();
2966 Assert.IsTrue (e is UserLogEvent);
2967 var le = e as UserLogEvent;
2969 Assert.AreEqual (5, le.Level);
2970 Assert.AreEqual ("A", le.Category);
2971 Assert.AreEqual ("B", le.Message);
2975 public void TypeGetMethodsByNameFlags () {
2977 var assembly = entry_point.DeclaringType.Assembly;
2978 var type = assembly.GetType ("Tests3");
2980 Assert.IsNotNull (type);
2982 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Public, false);
2983 Assert.AreEqual (1, mm.Length, "#1");
2984 Assert.AreEqual ("M1", mm[0].Name, "#2");
2986 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.NonPublic, false);
2987 Assert.AreEqual (1, mm.Length, "#3");
2988 Assert.AreEqual ("M2", mm[0].Name, "#4");
2990 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public, false);
2991 Assert.AreEqual (7, mm.Length, "#5"); //M3 plus Equals, GetHashCode, GetType, ToString, .ctor
2993 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, false);
2994 Assert.AreEqual (2, mm.Length, "#7");
2996 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
2997 Assert.AreEqual (1, mm.Length, "#9");
2999 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
3000 Assert.AreEqual (5, mm.Length, "#11");
3003 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Public, false);
3004 Assert.AreEqual (1, mm.Length, "#12");
3005 Assert.AreEqual ("M1", mm[0].Name, "#13");
3007 mm = type.GetMethodsByNameFlags ("m1", BindingFlags.Static | BindingFlags.Public, true);
3008 Assert.AreEqual (1, mm.Length, "#14");
3009 Assert.AreEqual ("M1", mm[0].Name, "#15");
3011 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, false);
3012 Assert.AreEqual (1, mm.Length, "#16");
3013 Assert.AreEqual ("M1", mm[0].Name, "#17");
3017 [Category ("only88")]
3018 public void TypeLoadSourceFileFilter () {
3019 Event e = run_until ("type_load");
3021 if (!vm.Version.AtLeast (2, 7))
3024 string srcfile = (e as BreakpointEvent).Method.DeclaringType.GetSourceFiles (true)[0];
3026 var req = vm.CreateTypeLoadRequest ();
3027 req.SourceFileFilter = new string [] { srcfile.ToUpper () };
3031 e = GetNextEvent ();
3032 Assert.IsTrue (e is TypeLoadEvent);
3033 Assert.AreEqual ("TypeLoadClass", (e as TypeLoadEvent).Type.FullName);
3037 public void TypeLoadTypeNameFilter () {
3038 Event e = run_until ("type_load");
3040 var req = vm.CreateTypeLoadRequest ();
3041 req.TypeNameFilter = new string [] { "TypeLoadClass2" };
3045 e = GetNextEvent ();
3046 Assert.IsTrue (e is TypeLoadEvent);
3047 Assert.AreEqual ("TypeLoadClass2", (e as TypeLoadEvent).Type.FullName);
3051 public void GetTypesForSourceFile () {
3054 var types = vm.GetTypesForSourceFile ("dtest-app.cs", false);
3055 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
3056 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
3058 types = vm.GetTypesForSourceFile ("DTEST-app.cs", true);
3059 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
3060 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
3064 public void GetTypesNamed () {
3067 var types = vm.GetTypes ("Tests", false);
3068 Assert.AreEqual (1, types.Count);
3069 Assert.AreEqual ("Tests", types [0].FullName);
3071 types = vm.GetTypes ("System.Exception", false);
3072 Assert.AreEqual (1, types.Count);
3073 Assert.AreEqual ("System.Exception", types [0].FullName);
3077 public void String_GetChars () {
3081 var e = run_until ("arg2");
3083 var frame = e.Thread.GetFrames () [0];
3085 val = frame.GetArgument (0);
3086 Assert.IsTrue (val is StringMirror);
3087 AssertValue ("FOO", val);
3088 var s = (val as StringMirror);
3089 Assert.AreEqual (3, s.Length);
3091 var c = s.GetChars (0, 2);
3092 Assert.AreEqual (2, c.Length);
3093 Assert.AreEqual ('F', c [0]);
3094 Assert.AreEqual ('O', c [1]);
3096 AssertThrows<ArgumentException> (delegate () {
3102 public void GetInterfaces () {
3103 var e = run_until ("arg2");
3105 var frame = e.Thread.GetFrames () [0];
3107 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
3108 var ifaces = cl1.GetInterfaces ();
3109 Assert.AreEqual (1, ifaces.Length);
3110 Assert.AreEqual ("ITest", ifaces [0].Name);
3112 var cl2 = cl1.GetMethod ("Baz").ReturnType;
3113 var ifaces2 = cl2.GetInterfaces ();
3114 Assert.AreEqual (1, ifaces2.Length);
3115 Assert.AreEqual ("ITest`1", ifaces2 [0].Name);
3119 public void GetInterfaceMap () {
3120 var e = run_until ("arg2");
3122 var frame = e.Thread.GetFrames () [0];
3124 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
3125 var iface = cl1.Assembly.GetType ("ITest");
3126 var map = cl1.GetInterfaceMap (iface);
3127 Assert.AreEqual (cl1, map.TargetType);
3128 Assert.AreEqual (iface, map.InterfaceType);
3129 Assert.AreEqual (2, map.InterfaceMethods.Length);
3130 Assert.AreEqual (2, map.TargetMethods.Length);
3134 public void StackAlloc_Breakpoints_Regress2775 () {
3135 // Check that breakpoints on arm don't overwrite stackalloc-ed memory
3136 var e = run_until ("regress_2755");
3138 var frame = e.Thread.GetFrames () [0];
3140 // This breaks at the call site
3141 vm.SetBreakpoint (m, m.Locations [2].ILOffset);
3144 var e2 = GetNextEvent ();
3145 Assert.IsTrue (e2 is BreakpointEvent);
3147 e = run_until ("regress_2755_3");
3148 frame = e.Thread.GetFrames () [1];
3149 var res = frame.GetValue (m.GetLocal ("sum"));
3150 AssertValue (0, res);
3154 public void MethodInfo () {
3155 Event e = run_until ("locals2");
3157 StackFrame frame = e.Thread.GetFrames () [0];
3158 var m = frame.Method;
3160 Assert.IsTrue (m.IsGenericMethod);
3161 Assert.IsFalse (m.IsGenericMethodDefinition);
3163 var args = m.GetGenericArguments ();
3164 Assert.AreEqual (1, args.Length);
3165 Assert.AreEqual ("String", args [0].Name);
3167 var gmd = m.GetGenericMethodDefinition ();
3168 Assert.IsTrue (gmd.IsGenericMethod);
3169 Assert.IsTrue (gmd.IsGenericMethodDefinition);
3170 Assert.AreEqual (gmd, gmd.GetGenericMethodDefinition ());
3172 args = gmd.GetGenericArguments ();
3173 Assert.AreEqual (1, args.Length);
3174 Assert.AreEqual ("T", args [0].Name);
3176 var attrs = m.GetCustomAttributes (true);
3177 Assert.AreEqual (1, attrs.Length);
3178 Assert.AreEqual ("StateMachineAttribute", attrs [0].Constructor.DeclaringType.Name);
3182 public void UnhandledException () {
3185 Start (new string [] { "dtest-app.exe", "unhandled-exception" });
3187 var req = vm.CreateExceptionRequest (null, false, true);
3190 var e = run_until ("unhandled_exception");
3193 var e2 = GetNextEvent ();
3194 Assert.IsTrue (e2 is ExceptionEvent);
3201 public void UnhandledException_2 () {
3204 Start (new string [] { "dtest-app.exe", "unhandled-exception-endinvoke" });
3206 var req = vm.CreateExceptionRequest (null, false, true);
3209 MethodMirror m = entry_point.DeclaringType.GetMethod ("unhandled_exception_endinvoke_2");
3210 Assert.IsNotNull (m);
3211 vm.SetBreakpoint (m, m.ILOffsets [0]);
3213 var e = run_until ("unhandled_exception_endinvoke");
3216 var e2 = GetNextEvent ();
3217 Assert.IsFalse (e2 is ExceptionEvent);
3225 public void UnhandledExceptionUserCode () {
3228 // Exceptions caught in non-user code are treated as unhandled
3229 Start (new string [] { "dtest-app.exe", "unhandled-exception-user" });
3231 var req = vm.CreateExceptionRequest (null, false, true);
3232 req.AssemblyFilter = new List<AssemblyMirror> () { entry_point.DeclaringType.Assembly };
3235 var e = run_until ("unhandled_exception_user");
3238 var e2 = GetNextEvent ();
3239 Assert.IsTrue (e2 is ExceptionEvent);
3247 public void GCWhileSuspended () {
3248 // Check that objects are kept alive during suspensions
3249 Event e = run_until ("gc_suspend_1");
3251 MethodMirror m = entry_point.DeclaringType.GetMethod ("gc_suspend_invoke");
3253 var o = entry_point.DeclaringType.GetValue (entry_point.DeclaringType.GetField ("gc_suspend_field")) as ObjectMirror;
3254 //Console.WriteLine (o);
3256 StackFrame frame = e.Thread.GetFrames () [0];
3257 TypeMirror t = frame.Method.DeclaringType;
3258 for (int i = 0; i < 10; ++i)
3259 t.InvokeMethod (e.Thread, m, new Value [] { });
3261 // This throws an exception if the object is collected
3262 long addr = o.Address;
3264 var o2 = entry_point.DeclaringType.GetValue (entry_point.DeclaringType.GetField ("gc_suspend_field")) as ObjectMirror;
3269 public void MakeGenericMethod () {
3270 Event e = run_until ("bp1");
3272 var intm = vm.RootDomain.GetCorrespondingType (typeof (int));
3273 var stringm = vm.RootDomain.GetCorrespondingType (typeof (string));
3274 var gm = entry_point.DeclaringType.GetMethod ("generic_method");
3275 var res = gm.MakeGenericMethod (new TypeMirror [] { stringm });
3276 var args = res.GetGenericArguments ();
3277 Assert.AreEqual (1, args.Length);
3278 Assert.AreEqual (stringm, args [0]);
3281 AssertThrows<ArgumentNullException> (delegate {
3282 gm.MakeGenericMethod (null);
3284 AssertThrows<ArgumentNullException> (delegate {
3285 gm.MakeGenericMethod (new TypeMirror [] { null });
3287 AssertThrows<ArgumentException> (delegate {
3288 gm.MakeGenericMethod (new TypeMirror [] { stringm, stringm });
3290 AssertThrows<InvalidOperationException> (delegate {
3291 gm.MakeGenericMethod (new TypeMirror [] { intm });
3293 AssertThrows<InvalidOperationException> (delegate {
3294 entry_point.DeclaringType.GetMethod ("Main").MakeGenericMethod (new TypeMirror [] { intm });
3299 public void InspectThreadSuspenedOnWaitOne () {
3301 Start (true, "dtest-app.exe", "wait-one" );
3303 ThreadMirror.NativeTransitions = true;
3305 var evt = run_until ("wait_one");
3306 Assert.IsNotNull (evt, "#1");
3308 var thread = evt.Thread;
3309 Assert.AreEqual (ThreadState.Running, thread.ThreadState, "#1.1");
3311 var frames = thread.GetFrames ();
3312 Assert.IsNotNull (frames, "#2");
3313 Assert.AreEqual (2, frames.Length, "#3");
3314 Assert.AreEqual ("wait_one", frames [0].Method.Name, "#4");
3315 Assert.AreEqual ("Main", frames [1].Method.Name, "#5");
3319 Thread.Sleep (500); //FIXME this is racy, maybe single step? or something?
3322 Assert.AreEqual (ThreadState.WaitSleepJoin, thread.ThreadState, "#6");
3324 frames = thread.GetFrames ();
3325 Assert.AreEqual (4, frames.Length, "#7");
3326 Assert.AreEqual ("WaitOne_internal", frames [0].Method.Name, "#8");
3327 Assert.AreEqual ("WaitOne", frames [1].Method.Name, "#8.1");
3328 Assert.AreEqual ("wait_one", frames [2].Method.Name, "#9");
3329 Assert.AreEqual ("Main", frames [3].Method.Name, "#10");
3332 var frame = frames [0];
3333 Assert.IsTrue (frame.IsNativeTransition, "#11.1");
3336 Assert.Fail ("Known limitation - can't get info from m2n frames");
3337 } catch (AbsentInformationException) {}
3340 Assert.IsFalse (frame.IsNativeTransition, "#12.1");
3341 var wait_one_this = frame.GetThis ();
3342 Assert.IsNotNull (wait_one_this, "#12.2");
3345 var locals = frame.GetVisibleVariables ();
3346 Assert.AreEqual (1, locals.Count, "#13.1");
3348 var local_0 = frame.GetValue (locals [0]);
3349 Assert.IsNotNull (local_0, "#13.2");
3351 Assert.AreEqual (wait_one_this, local_0, "#14.2");