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;
28 void AssertThrows<ExType> (Action del) where ExType : Exception {
36 Assert.IsTrue (thrown);
39 // No other way to pass arguments to the tests ?
40 public static bool listening = Environment.GetEnvironmentVariable ("DBG_SUSPEND") != null;
41 public static string runtime = Environment.GetEnvironmentVariable ("DBG_RUNTIME");
42 public static string agent_args = Environment.GetEnvironmentVariable ("DBG_AGENT_ARGS");
44 Event GetNextEvent () {
45 var es = vm.GetNextEventSet ();
46 Assert.AreEqual (1, es.Events.Length);
50 void Start (string[] args) {
52 var pi = new Diag.ProcessStartInfo ();
55 pi.FileName = runtime;
58 pi.Arguments = String.Join (" ", args);
59 vm = VirtualMachineManager.Launch (pi, new LaunchOptions { AgentArgs = agent_args });
61 var ep = new IPEndPoint (IPAddress.Any, 10000);
62 Console.WriteLine ("Listening on " + ep + "...");
63 vm = VirtualMachineManager.Listen (ep);
66 var load_req = vm.CreateAssemblyLoadRequest ();
69 Event vmstart = GetNextEvent ();
70 Assert.AreEqual (EventType.VMStart, vmstart.EventType);
79 /* Find out the entry point */
83 if (e is AssemblyLoadEvent) {
84 AssemblyLoadEvent ae = (AssemblyLoadEvent)e;
85 entry_point = ae.Assembly.EntryPoint;
86 if (entry_point != null)
96 BreakpointEvent run_until (string name) {
98 MethodMirror m = entry_point.DeclaringType.GetMethod (name);
100 vm.SetBreakpoint (m, 0);
107 if (e is BreakpointEvent)
111 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
112 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
114 return (e as BreakpointEvent);
117 Event single_step (ThreadMirror t) {
118 var req = vm.CreateStepRequest (t);
122 Event e = GetNextEvent ();
123 Assert.IsTrue (e is StepEvent);
130 void check_arg_val (StackFrame frame, int pos, Type type, object eval) {
131 object val = frame.GetArgument (pos);
132 Assert.IsTrue (val is PrimitiveValue);
133 object v = (val as PrimitiveValue).Value;
134 Assert.AreEqual (type, v.GetType ());
136 Assert.IsTrue (Math.Abs ((float)eval - (float)v) < 0.0001);
137 else if (eval is double)
138 Assert.IsTrue (Math.Abs ((double)eval - (double)v) < 0.0001);
140 Assert.AreEqual (eval, v);
143 void AssertValue (object expected, object val) {
144 if (expected is string) {
145 Assert.IsTrue (val is StringMirror);
146 Assert.AreEqual (expected, (val as StringMirror).Value);
147 } else if (val is StructMirror && (val as StructMirror).Type.Name == "IntPtr") {
148 AssertValue (expected, (val as StructMirror).Fields [0]);
150 Assert.IsTrue (val is PrimitiveValue);
151 Assert.AreEqual (expected, (val as PrimitiveValue).Value);
156 public void SetUp () {
157 Start (new string [] { "dtest-app.exe" });
161 public void TearDown () {
165 if (step_req != null)
170 Event e = GetNextEvent ();
172 if (e is VMDeathEvent)
180 public void SimpleBreakpoint () {
183 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp1");
184 Assert.IsNotNull (m);
186 vm.SetBreakpoint (m, 0);
191 Assert.AreEqual (EventType.Breakpoint, e.EventType);
192 Assert.IsTrue (e is BreakpointEvent);
193 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
196 AssertThrows<ArgumentException> (delegate {
198 vm.SetBreakpoint (m, 2);
203 public void BreakpointsSameLocation () {
204 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp2");
205 Assert.IsNotNull (m);
207 vm.SetBreakpoint (m, 0);
208 vm.SetBreakpoint (m, 0);
212 var es = vm.GetNextEventSet ();
213 Assert.AreEqual (2, es.Events.Length);
214 Assert.IsTrue (es [0] is BreakpointEvent);
215 Assert.AreEqual (m, (es [0] as BreakpointEvent).Method);
217 Assert.IsTrue (es [1] is BreakpointEvent);
218 Assert.AreEqual (m.Name, (es [1] as BreakpointEvent).Method.Name);
222 public void BreakpointAlreadyJITted () {
223 Event e = run_until ("bp1");
225 /* Place a breakpoint on bp3 */
226 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp3");
227 Assert.IsNotNull (m);
228 vm.SetBreakpoint (m, 0);
230 /* Same with generic instances */
231 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp7");
232 Assert.IsNotNull (m2);
233 vm.SetBreakpoint (m2, 0);
238 Assert.AreEqual (EventType.Breakpoint, e.EventType);
239 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
243 /* Non-shared instance */
245 Assert.AreEqual (EventType.Breakpoint, e.EventType);
246 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
250 /* Shared instance */
252 Assert.AreEqual (EventType.Breakpoint, e.EventType);
253 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
257 public void ClearBreakpoint () {
260 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
261 Assert.IsNotNull (m);
262 EventRequest req1 = vm.SetBreakpoint (m, 0);
263 EventRequest req2 = vm.SetBreakpoint (m, 0);
265 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
266 Assert.IsNotNull (m2);
267 vm.SetBreakpoint (m2, 0);
272 var es = vm.GetNextEventSet ();
273 Assert.AreEqual (2, es.Events.Length);
274 Assert.AreEqual (EventType.Breakpoint, es [0].EventType);
275 Assert.AreEqual (m.Name, (es [0] as BreakpointEvent).Method.Name);
276 Assert.AreEqual (EventType.Breakpoint, es [1].EventType);
277 Assert.AreEqual (m.Name, (es [1] as BreakpointEvent).Method.Name);
279 /* Clear one of them */
285 Assert.AreEqual (EventType.Breakpoint, e.EventType);
286 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
288 /* Clear the other */
294 Assert.AreEqual (EventType.Breakpoint, e.EventType);
295 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
299 public void ClearAllBreakpoints () {
302 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
303 Assert.IsNotNull (m);
304 vm.SetBreakpoint (m, 0);
306 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
307 Assert.IsNotNull (m2);
308 vm.SetBreakpoint (m2, 0);
310 vm.ClearAllBreakpoints ();
315 Assert.IsTrue (!(e is BreakpointEvent));
316 if (e is VMDeathEvent)
321 public void BreakpointOnGShared () {
324 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp6");
325 Assert.IsNotNull (m);
327 vm.SetBreakpoint (m, 0);
332 Assert.AreEqual (EventType.Breakpoint, e.EventType);
333 Assert.IsTrue (e is BreakpointEvent);
334 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
336 // Breakpoint on an open generic method of a closed generic class (#3422)
337 var frame = e.Thread.GetFrames ()[0];
338 var ginst = frame.GetValue (frame.Method.GetLocal ("gc"));
339 var m2 = (ginst as ObjectMirror).Type.GetMethod ("bp");
340 vm.SetBreakpoint (m2, 0);
345 Assert.AreEqual (EventType.Breakpoint, e.EventType);
346 Assert.IsTrue (e is BreakpointEvent);
347 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
351 public void SingleStepping () {
352 Event e = run_until ("single_stepping");
354 var req = vm.CreateStepRequest (e.Thread);
359 // Step over 'bool b = true'
362 Assert.IsTrue (e is StepEvent);
363 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
368 Assert.IsTrue (e is StepEvent);
369 Assert.AreEqual ("ss1", (e as StepEvent).Method.Name);
374 Assert.IsTrue (e is StepEvent);
375 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
377 // Change to step over
379 req.Depth = StepDepth.Over;
385 Assert.IsTrue (e is StepEvent);
386 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
388 // Change to step into
390 req.Depth = StepDepth.Into;
396 Assert.IsTrue (e is StepEvent);
397 Assert.AreEqual ("ss3", (e as StepEvent).Method.Name);
399 // Change to step out
401 req.Depth = StepDepth.Out;
404 // Step back into single_stepping
407 Assert.IsTrue (e is StepEvent);
408 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
410 // Change to step into
412 req.Depth = StepDepth.Into;
415 // Step into ss3_2 ()
418 Assert.IsTrue (e is StepEvent);
419 Assert.AreEqual ("ss3_2", (e as StepEvent).Method.Name);
421 // Change to step over
423 req.Depth = StepDepth.Over;
426 // Step over ss3_2_2 ()
429 Assert.IsTrue (e is StepEvent);
430 Assert.AreEqual ("ss3_2", (e as StepEvent).Method.Name);
432 // Recreate the request
436 // Step back into single_stepping () with the new request
439 Assert.IsTrue (e is StepEvent);
440 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
442 // Change to step into
444 req.Depth = StepDepth.Into;
450 Assert.IsTrue (e is StepEvent);
451 Assert.AreEqual ("ss4", (e as StepEvent).Method.Name);
453 // Change to StepSize.Line
455 req.Depth = StepDepth.Over;
456 req.Size = StepSize.Line;
459 // Step over ss1 (); ss1 ();
462 Assert.IsTrue (e is StepEvent);
466 req.Depth = StepDepth.Into;
471 Assert.IsTrue (e is StepEvent);
472 Assert.AreEqual ("ss2", (e as StepEvent).Method.Name);
477 e = run_until ("ss5");
479 // Add an assembly filter
480 req.AssemblyFilter = new AssemblyMirror [] { (e as BreakpointEvent).Method.DeclaringType.Assembly };
483 // Step into is_even, skipping the linq stuff
486 Assert.IsTrue (e is StepEvent);
487 Assert.AreEqual ("is_even", (e as StepEvent).Method.Name);
489 // FIXME: Check that single stepping works with lock (obj)
494 e = run_until ("ss6");
496 req = vm.CreateStepRequest (e.Thread);
497 req.Depth = StepDepth.Over;
500 // Check that single stepping works in out-of-line bblocks
503 Assert.IsTrue (e is StepEvent);
506 Assert.IsTrue (e is StepEvent);
507 Assert.AreEqual ("ss6", (e as StepEvent).Method.Name);
510 // Check that a step over stops at an EH clause
511 e = run_until ("ss7_2");
512 req = vm.CreateStepRequest (e.Thread);
513 req.Depth = StepDepth.Out;
517 Assert.IsTrue (e is StepEvent);
518 Assert.AreEqual ("ss7", (e as StepEvent).Method.Name);
520 req = vm.CreateStepRequest (e.Thread);
521 req.Depth = StepDepth.Over;
525 Assert.IsTrue (e is StepEvent);
526 Assert.AreEqual ("ss7", (e as StepEvent).Method.Name);
531 public void MethodEntryExit () {
532 run_until ("single_stepping");
534 var req1 = vm.CreateMethodEntryRequest ();
535 var req2 = vm.CreateMethodExitRequest ();
541 Event e = GetNextEvent ();
542 Assert.IsTrue (e is MethodEntryEvent);
543 Assert.AreEqual ("ss1", (e as MethodEntryEvent).Method.Name);
547 Assert.IsTrue (e is MethodExitEvent);
548 Assert.AreEqual ("ss1", (e as MethodExitEvent).Method.Name);
555 public void CountFilter () {
556 run_until ("single_stepping");
558 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("ss3");
559 Assert.IsNotNull (m2);
560 vm.SetBreakpoint (m2, 0);
562 var req1 = vm.CreateMethodEntryRequest ();
566 // Enter ss2, ss1 is skipped
568 Event e = GetNextEvent ();
569 Assert.IsTrue (e is MethodEntryEvent);
570 Assert.AreEqual ("ss2", (e as MethodEntryEvent).Method.Name);
572 // Breakpoint on ss3, the entry event is no longer reported
575 Assert.IsTrue (e is BreakpointEvent);
581 public void Arguments () {
584 var e = run_until ("arg1");
586 StackFrame frame = e.Thread.GetFrames () [0];
588 check_arg_val (frame, 0, typeof (sbyte), SByte.MaxValue - 5);
589 check_arg_val (frame, 1, typeof (byte), Byte.MaxValue - 5);
590 check_arg_val (frame, 2, typeof (bool), true);
591 check_arg_val (frame, 3, typeof (short), Int16.MaxValue - 5);
592 check_arg_val (frame, 4, typeof (ushort), UInt16.MaxValue - 5);
593 check_arg_val (frame, 5, typeof (char), 'F');
594 check_arg_val (frame, 6, typeof (int), Int32.MaxValue - 5);
595 check_arg_val (frame, 7, typeof (uint), UInt32.MaxValue - 5);
596 check_arg_val (frame, 8, typeof (long), Int64.MaxValue - 5);
597 check_arg_val (frame, 9, typeof (ulong), UInt64.MaxValue - 5);
598 check_arg_val (frame, 10, typeof (float), 1.2345f);
599 check_arg_val (frame, 11, typeof (double), 6.78910);
601 e = run_until ("arg2");
603 frame = e.Thread.GetFrames () [0];
606 val = frame.GetArgument (0);
607 AssertValue ("FOO", val);
608 Assert.AreEqual ("String", (val as ObjectMirror).Type.Name);
611 val = frame.GetArgument (1);
612 AssertValue (null, val);
615 val = frame.GetArgument (2);
616 AssertValue ("BLA", val);
619 val = frame.GetArgument (3);
620 AssertValue (42, val);
623 val = frame.GetArgument (4);
624 Assert.IsTrue (val is ObjectMirror);
625 Assert.AreEqual ("GClass`1", (val as ObjectMirror).Type.Name);
628 val = frame.GetArgument (5);
629 Assert.IsTrue (val is ObjectMirror);
630 Assert.AreEqual ("Object", (val as ObjectMirror).Type.Name);
632 // this on static methods
633 val = frame.GetThis ();
634 AssertValue (null, val);
636 e = run_until ("arg3");
638 frame = e.Thread.GetFrames () [0];
641 val = frame.GetThis ();
642 Assert.IsTrue (val is ObjectMirror);
643 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
645 // objref in register
646 val = frame.GetArgument (0);
647 AssertValue ("BLA", val);
651 public void Arrays () {
654 var e = run_until ("o2");
656 StackFrame frame = e.Thread.GetFrames () [0];
659 val = frame.GetArgument (0);
660 Assert.IsTrue (val is ArrayMirror);
661 ArrayMirror arr = val as ArrayMirror;
662 Assert.AreEqual (2, arr.Length);
663 AssertValue ("BAR", arr [0]);
664 AssertValue ("BAZ", arr [1]);
666 var vals = arr.GetValues (0, 2);
667 Assert.AreEqual (2, vals.Count);
668 AssertValue ("BAR", vals [0]);
669 AssertValue ("BAZ", vals [1]);
671 arr [0] = vm.RootDomain.CreateString ("ABC");
672 AssertValue ("ABC", arr [0]);
674 arr [0] = vm.CreateValue (null);
675 AssertValue (null, arr [0]);
677 arr.SetValues (0, new Value [] { vm.RootDomain.CreateString ("D1"), vm.RootDomain.CreateString ("D2") });
678 AssertValue ("D1", arr [0]);
679 AssertValue ("D2", arr [1]);
682 val = frame.GetArgument (1);
683 Assert.IsTrue (val is ArrayMirror);
684 arr = val as ArrayMirror;
685 Assert.AreEqual (2, arr.Length);
686 AssertValue (42, arr [0]);
687 AssertValue (43, arr [1]);
690 AssertThrows<IndexOutOfRangeException> (delegate () {
694 AssertThrows<IndexOutOfRangeException> (delegate () {
695 val = arr [Int32.MinValue];
698 AssertThrows<IndexOutOfRangeException> (delegate () {
699 vals = arr.GetValues (0, 3);
702 AssertThrows<IndexOutOfRangeException> (delegate () {
703 arr [2] = vm.CreateValue (null);
706 AssertThrows<IndexOutOfRangeException> (delegate () {
707 arr [Int32.MinValue] = vm.CreateValue (null);
710 AssertThrows<IndexOutOfRangeException> (delegate () {
711 arr.SetValues (0, new Value [] { null, null, null });
715 val = frame.GetArgument (2);
716 Assert.IsTrue (val is ArrayMirror);
717 arr = val as ArrayMirror;
718 Assert.AreEqual (2, arr.Rank);
719 Assert.AreEqual (4, arr.Length);
720 Assert.AreEqual (2, arr.GetLength (0));
721 Assert.AreEqual (2, arr.GetLength (1));
722 Assert.AreEqual (0, arr.GetLowerBound (0));
723 Assert.AreEqual (0, arr.GetLowerBound (1));
724 vals = arr.GetValues (0, 4);
725 AssertValue (1, vals [0]);
726 AssertValue (2, vals [1]);
727 AssertValue (3, vals [2]);
728 AssertValue (4, vals [3]);
730 val = frame.GetArgument (3);
731 Assert.IsTrue (val is ArrayMirror);
732 arr = val as ArrayMirror;
733 Assert.AreEqual (2, arr.Rank);
734 Assert.AreEqual (4, arr.Length);
735 Assert.AreEqual (2, arr.GetLength (0));
736 Assert.AreEqual (2, arr.GetLength (1));
737 Assert.AreEqual (1, arr.GetLowerBound (0));
738 Assert.AreEqual (3, arr.GetLowerBound (1));
740 AssertThrows<ArgumentOutOfRangeException> (delegate () {
743 AssertThrows<ArgumentOutOfRangeException> (delegate () {
747 AssertThrows<ArgumentOutOfRangeException> (delegate () {
748 arr.GetLowerBound (-1);
750 AssertThrows<ArgumentOutOfRangeException> (delegate () {
751 arr.GetLowerBound (2);
754 // arrays treated as generic collections
755 val = frame.GetArgument (4);
756 Assert.IsTrue (val is ArrayMirror);
757 arr = val as ArrayMirror;
761 public void Object_GetValue () {
762 var e = run_until ("o1");
763 var frame = e.Thread.GetFrames () [0];
765 object val = frame.GetThis ();
766 Assert.IsTrue (val is ObjectMirror);
767 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
768 ObjectMirror o = (val as ObjectMirror);
770 TypeMirror t = o.Type;
773 object f = o.GetValue (t.GetField ("field_i"));
775 f = o.GetValue (t.GetField ("field_s"));
776 AssertValue ("S", f);
777 f = o.GetValue (t.GetField ("field_enum"));
778 Assert.IsTrue (f is EnumMirror);
779 Assert.AreEqual (1, (f as EnumMirror).Value);
780 Assert.AreEqual ("B", (f as EnumMirror).StringValue);
782 // Inherited object fields
783 TypeMirror parent = t.BaseType;
784 f = o.GetValue (parent.GetField ("base_field_i"));
786 f = o.GetValue (parent.GetField ("base_field_s"));
787 AssertValue ("T", f);
790 f = o.GetValue (o.Type.GetField ("static_i"));
794 ObjectMirror o2 = frame.GetValue (frame.Method.GetParameters ()[1]) as ObjectMirror;
795 Assert.AreEqual ("GClass`1", o2.Type.Name);
796 TypeMirror t2 = o2.Type;
797 f = o2.GetValue (t2.GetField ("field"));
800 ObjectMirror o3 = frame.GetValue (frame.Method.GetParameters ()[2]) as ObjectMirror;
801 Assert.AreEqual ("GClass`1", o3.Type.Name);
802 TypeMirror t3 = o3.Type;
803 f = o3.GetValue (t3.GetField ("field"));
804 AssertValue ("FOO", f);
807 AssertThrows<ArgumentNullException> (delegate () {
813 public void Object_GetValues () {
814 var e = run_until ("o1");
815 var frame = e.Thread.GetFrames () [0];
817 object val = frame.GetThis ();
818 Assert.IsTrue (val is ObjectMirror);
819 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
820 ObjectMirror o = (val as ObjectMirror);
822 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
824 TypeMirror t = o.Type;
826 object[] vals = o.GetValues (new FieldInfoMirror [] { t.GetField ("field_i"), t.GetField ("field_s") });
830 AssertValue ("S", f);
833 AssertThrows<ArgumentNullException> (delegate () {
837 AssertThrows<ArgumentNullException> (delegate () {
838 o.GetValues (new FieldInfoMirror [] { null });
841 // field of another class
842 AssertThrows<ArgumentException> (delegate () {
843 o.GetValue (val2.Type.GetField ("field_j"));
847 void TestSetValue (ObjectMirror o, string field_name, object val) {
849 o.SetValue (o.Type.GetField (field_name), vm.RootDomain.CreateString ((string)val));
851 o.SetValue (o.Type.GetField (field_name), vm.CreateValue (val));
852 Value f = o.GetValue (o.Type.GetField (field_name));
853 AssertValue (val, f);
857 public void Object_SetValues () {
858 var e = run_until ("o1");
859 var frame = e.Thread.GetFrames () [0];
861 object val = frame.GetThis ();
862 Assert.IsTrue (val is ObjectMirror);
863 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
864 ObjectMirror o = (val as ObjectMirror);
866 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
868 TestSetValue (o, "field_i", 22);
869 TestSetValue (o, "field_bool1", false);
870 TestSetValue (o, "field_bool2", true);
871 TestSetValue (o, "field_char", 'B');
872 TestSetValue (o, "field_byte", (byte)129);
873 TestSetValue (o, "field_sbyte", (sbyte)-33);
874 TestSetValue (o, "field_short", (short)(Int16.MaxValue - 5));
875 TestSetValue (o, "field_ushort", (ushort)(UInt16.MaxValue - 5));
876 TestSetValue (o, "field_long", Int64.MaxValue - 5);
877 TestSetValue (o, "field_ulong", (ulong)(UInt64.MaxValue - 5));
878 TestSetValue (o, "field_float", 6.28f);
879 TestSetValue (o, "field_double", 6.28);
880 TestSetValue (o, "static_i", 23);
881 TestSetValue (o, "field_s", "CDEF");
886 f = o.GetValue (o.Type.GetField ("field_intptr"));
887 Assert.IsInstanceOfType (typeof (StructMirror), f);
888 AssertValue (Int32.MaxValue - 5, (f as StructMirror).Fields [0]);
891 FieldInfoMirror field = o.Type.GetField ("field_enum");
892 f = o.GetValue (field);
893 (f as EnumMirror).Value = 5;
894 o.SetValue (field, f);
895 f = o.GetValue (field);
896 Assert.AreEqual (5, (f as EnumMirror).Value);
899 o.SetValue (o.Type.GetField ("field_s"), vm.CreateValue (null));
900 f = o.GetValue (o.Type.GetField ("field_s"));
901 AssertValue (null, f);
904 field = o.Type.GetField ("generic_field_struct");
905 f = o.GetValue (field);
906 o.SetValue (field, f);
909 field = o.Type.GetField ("field_nullable");
910 f = o.GetValue (field);
911 AssertValue (0, (f as StructMirror).Fields [0]);
912 AssertValue (false, (f as StructMirror).Fields [1]);
913 o.SetValue (field, vm.CreateValue (6));
914 f = o.GetValue (field);
915 AssertValue (6, (f as StructMirror).Fields [0]);
916 AssertValue (true, (f as StructMirror).Fields [1]);
917 o.SetValue (field, vm.CreateValue (null));
918 f = o.GetValue (field);
919 AssertValue (0, (f as StructMirror).Fields [0]);
920 AssertValue (false, (f as StructMirror).Fields [1]);
923 AssertThrows<ArgumentNullException> (delegate () {
924 o.SetValues (null, new Value [0]);
927 AssertThrows<ArgumentNullException> (delegate () {
928 o.SetValues (new FieldInfoMirror [0], null);
931 AssertThrows<ArgumentNullException> (delegate () {
932 o.SetValues (new FieldInfoMirror [] { null }, new Value [1] { null });
935 // vtype with a wrong type
936 AssertThrows<ArgumentException> (delegate () {
937 o.SetValue (o.Type.GetField ("field_struct"), o.GetValue (o.Type.GetField ("field_enum")));
940 // reference type not assignment compatible
941 AssertThrows<ArgumentException> (delegate () {
942 o.SetValue (o.Type.GetField ("field_class"), o);
945 // field of another class
946 AssertThrows<ArgumentException> (delegate () {
947 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
952 public void Type_SetValue () {
953 var e = run_until ("o1");
954 var frame = e.Thread.GetFrames () [0];
957 object val = frame.GetThis ();
958 Assert.IsTrue (val is ObjectMirror);
959 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
960 ObjectMirror o = (val as ObjectMirror);
962 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
964 o.Type.SetValue (o.Type.GetField ("static_i"), vm.CreateValue (55));
965 f = o.Type.GetValue (o.Type.GetField ("static_i"));
968 o.Type.SetValue (o.Type.GetField ("static_s"), vm.RootDomain.CreateString ("B"));
969 f = o.Type.GetValue (o.Type.GetField ("static_s"));
970 AssertValue ("B", f);
973 AssertThrows<ArgumentNullException> (delegate () {
974 o.Type.SetValue (null, vm.CreateValue (0));
977 AssertThrows<ArgumentNullException> (delegate () {
978 o.Type.SetValue (o.Type.GetField ("static_i"), null);
981 // field of another class
982 AssertThrows<ArgumentException> (delegate () {
983 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
988 public void TypeInfo () {
989 Event e = run_until ("ti2");
990 StackFrame frame = e.Thread.GetFrames () [0];
995 t = frame.Method.GetParameters ()[0].ParameterType;
997 Assert.AreEqual ("String[]", t.Name);
998 Assert.AreEqual ("string[]", t.CSharpName);
999 Assert.AreEqual ("Array", t.BaseType.Name);
1000 Assert.AreEqual (true, t.HasElementType);
1001 Assert.AreEqual (true, t.IsArray);
1002 Assert.AreEqual (1, t.GetArrayRank ());
1003 Assert.AreEqual ("String", t.GetElementType ().Name);
1005 t = frame.Method.GetParameters ()[2].ParameterType;
1007 Assert.AreEqual ("Int32[,]", t.Name);
1009 //Assert.AreEqual ("int[,]", t.CSharpName);
1010 Assert.AreEqual ("Array", t.BaseType.Name);
1011 Assert.AreEqual (true, t.HasElementType);
1012 Assert.AreEqual (true, t.IsArray);
1013 Assert.AreEqual (2, t.GetArrayRank ());
1014 Assert.AreEqual ("Int32", t.GetElementType ().Name);
1017 t = frame.Method.GetParameters ()[3].ParameterType;
1019 //Assert.AreEqual ("Int32&", t.Name);
1020 //Assert.AreEqual (true, t.IsByRef);
1021 //Assert.AreEqual (true, t.HasElementType);
1024 t = frame.Method.GetParameters ()[4].ParameterType;
1026 //Assert.AreEqual ("Int32*", t.Name);
1027 Assert.AreEqual (true, t.IsPointer);
1028 Assert.AreEqual (true, t.HasElementType);
1029 Assert.AreEqual ("Int32", t.GetElementType ().Name);
1030 Assert.AreEqual (false, t.IsPrimitive);
1033 t = frame.Method.GetParameters ()[5].ParameterType;
1034 Assert.AreEqual (true, t.IsPrimitive);
1037 t = frame.Method.GetParameters ()[6].ParameterType;
1038 Assert.AreEqual ("AStruct", t.Name);
1039 Assert.AreEqual (false, t.IsPrimitive);
1040 Assert.AreEqual (true, t.IsValueType);
1041 Assert.AreEqual (false, t.IsClass);
1044 t = frame.Method.GetParameters ()[7].ParameterType;
1045 Assert.AreEqual ("Tests", t.Name);
1046 var nested = (from nt in t.GetNestedTypes () where nt.IsNestedPublic select nt).ToArray ();
1047 Assert.AreEqual (1, nested.Length);
1048 Assert.AreEqual ("NestedClass", nested [0].Name);
1049 Assert.IsTrue (t.BaseType.IsAssignableFrom (t));
1050 Assert.IsTrue (!t.IsAssignableFrom (t.BaseType));
1052 // generic instances
1053 t = frame.Method.GetParameters ()[9].ParameterType;
1054 Assert.AreEqual ("GClass`1", t.Name);
1055 Assert.IsTrue (t.IsGenericType);
1056 Assert.IsFalse (t.IsGenericTypeDefinition);
1058 var args = t.GetGenericArguments ();
1059 Assert.AreEqual (1, args.Length);
1060 Assert.AreEqual ("Int32", args [0].Name);
1062 // generic type definitions
1063 var gtd = t.GetGenericTypeDefinition ();
1064 Assert.AreEqual ("GClass`1", gtd.Name);
1065 Assert.IsTrue (gtd.IsGenericType);
1066 Assert.IsTrue (gtd.IsGenericTypeDefinition);
1067 Assert.AreEqual (gtd, gtd.GetGenericTypeDefinition ());
1069 args = gtd.GetGenericArguments ();
1070 Assert.AreEqual (1, args.Length);
1071 Assert.AreEqual ("T", args [0].Name);
1074 t = frame.Method.GetParameters ()[10].ParameterType;
1075 Assert.AreEqual ("AnEnum", t.Name);
1076 Assert.IsTrue (t.IsEnum);
1077 Assert.AreEqual ("Int32", t.EnumUnderlyingType.Name);
1080 t = frame.Method.GetParameters ()[7].ParameterType;
1082 var props = t.GetProperties ();
1083 Assert.AreEqual (3, props.Length);
1084 foreach (PropertyInfoMirror prop in props) {
1085 ParameterInfoMirror[] indexes = prop.GetIndexParameters ();
1087 if (prop.Name == "IntProperty") {
1088 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1089 Assert.AreEqual ("get_IntProperty", prop.GetGetMethod ().Name);
1090 Assert.AreEqual ("set_IntProperty", prop.GetSetMethod ().Name);
1091 Assert.AreEqual (0, indexes.Length);
1092 } else if (prop.Name == "ReadOnlyProperty") {
1093 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1094 Assert.AreEqual ("get_ReadOnlyProperty", prop.GetGetMethod ().Name);
1095 Assert.AreEqual (null, prop.GetSetMethod ());
1096 Assert.AreEqual (0, indexes.Length);
1097 } else if (prop.Name == "IndexedProperty") {
1098 Assert.AreEqual (1, indexes.Length);
1099 Assert.AreEqual ("Int32", indexes [0].ParameterType.Name);
1103 // custom attributes
1104 t = frame.Method.GetParameters ()[8].ParameterType;
1105 Assert.AreEqual ("Tests2", t.Name);
1106 var attrs = t.GetCustomAttributes (true);
1107 Assert.AreEqual (2, attrs.Length);
1108 foreach (var attr in attrs) {
1109 if (attr.Constructor.DeclaringType.Name == "DebuggerDisplayAttribute") {
1110 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1111 Assert.AreEqual ("Tests", attr.ConstructorArguments [0].Value);
1112 Assert.AreEqual (2, attr.NamedArguments.Count);
1113 Assert.AreEqual ("Name", attr.NamedArguments [0].Property.Name);
1114 Assert.AreEqual ("FOO", attr.NamedArguments [0].TypedValue.Value);
1115 Assert.AreEqual ("Target", attr.NamedArguments [1].Property.Name);
1116 Assert.IsInstanceOfType (typeof (TypeMirror), attr.NamedArguments [1].TypedValue.Value);
1117 Assert.AreEqual ("Int32", (attr.NamedArguments [1].TypedValue.Value as TypeMirror).Name);
1118 } else if (attr.Constructor.DeclaringType.Name == "DebuggerTypeProxyAttribute") {
1119 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1120 Assert.IsInstanceOfType (typeof (TypeMirror), attr.ConstructorArguments [0].Value);
1121 Assert.AreEqual ("Tests", (attr.ConstructorArguments [0].Value as TypeMirror).Name);
1123 Assert.Fail (attr.Constructor.DeclaringType.Name);
1129 public void FieldInfo () {
1130 Event e = run_until ("ti2");
1131 StackFrame frame = e.Thread.GetFrames () [0];
1135 t = frame.Method.GetParameters ()[8].ParameterType;
1136 Assert.AreEqual ("Tests2", t.Name);
1138 var fi = t.GetField ("field_j");
1139 var attrs = fi.GetCustomAttributes (true);
1140 Assert.AreEqual (1, attrs.Length);
1141 var attr = attrs [0];
1142 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1143 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1144 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1145 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1149 public void PropertyInfo () {
1150 Event e = run_until ("ti2");
1151 StackFrame frame = e.Thread.GetFrames () [0];
1155 t = frame.Method.GetParameters ()[8].ParameterType;
1156 Assert.AreEqual ("Tests2", t.Name);
1158 var pi = t.GetProperty ("AProperty");
1159 var attrs = pi.GetCustomAttributes (true);
1160 Assert.AreEqual (1, attrs.Length);
1161 var attr = attrs [0];
1162 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1163 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1164 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1165 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1169 [Category ("only5")]
1170 public void Type_GetValue () {
1171 Event e = run_until ("o1");
1172 StackFrame frame = e.Thread.GetFrames () [0];
1174 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1176 TypeMirror t = o.Type;
1178 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1181 object f = t.GetValue (o.Type.GetField ("static_i"));
1182 AssertValue (55, f);
1184 f = t.GetValue (o.Type.GetField ("static_s"));
1185 AssertValue ("A", f);
1187 // literal static fields
1188 f = t.GetValue (o.Type.GetField ("literal_i"));
1189 AssertValue (56, f);
1191 f = t.GetValue (o.Type.GetField ("literal_s"));
1192 AssertValue ("B", f);
1194 // Inherited static fields
1195 TypeMirror parent = t.BaseType;
1196 f = t.GetValue (parent.GetField ("base_static_i"));
1197 AssertValue (57, f);
1199 f = t.GetValue (parent.GetField ("base_static_s"));
1200 AssertValue ("C", f);
1202 // thread static field
1203 f = t.GetValue (t.GetField ("tls_i"), e.Thread);
1204 AssertValue (42, f);
1206 // Argument checking
1207 AssertThrows<ArgumentNullException> (delegate () {
1212 AssertThrows<ArgumentException> (delegate () {
1213 t.GetValue (o.Type.GetField ("field_i"));
1216 // field on another type
1217 AssertThrows<ArgumentException> (delegate () {
1218 t.GetValue (val2.Type.GetField ("static_field_j"));
1221 // special static field
1222 AssertThrows<ArgumentException> (delegate () {
1223 t.GetValue (t.GetField ("tls_i"));
1228 public void Type_GetValues () {
1229 Event e = run_until ("o1");
1230 StackFrame frame = e.Thread.GetFrames () [0];
1232 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1234 TypeMirror t = o.Type;
1237 object[] vals = t.GetValues (new FieldInfoMirror [] { t.GetField ("static_i"), t.GetField ("static_s") });
1238 object f = vals [0];
1239 AssertValue (55, f);
1242 AssertValue ("A", f);
1244 // Argument checking
1245 AssertThrows<ArgumentNullException> (delegate () {
1249 AssertThrows<ArgumentNullException> (delegate () {
1250 t.GetValues (new FieldInfoMirror [] { null });
1255 public void ObjRefs () {
1256 Event e = run_until ("objrefs1");
1257 StackFrame frame = e.Thread.GetFrames () [0];
1259 ObjectMirror o = frame.GetThis () as ObjectMirror;
1260 ObjectMirror child = o.GetValue (o.Type.GetField ("child")) as ObjectMirror;
1262 Assert.IsTrue (child.Address != 0);
1264 // Check that object references are internalized correctly
1265 Assert.AreEqual (o, frame.GetThis ());
1267 run_until ("objrefs2");
1269 // child should be gc'd now
1270 Assert.IsTrue (child.IsCollected);
1273 * No longer works since Type is read eagerly
1276 AssertThrows<ObjectCollectedException> (delegate () {
1277 TypeMirror t = child.Type;
1281 AssertThrows<ObjectCollectedException> (delegate () {
1282 long addr = child.Address;
1287 public void Type_GetObject () {
1288 Event e = run_until ("o1");
1289 StackFrame frame = e.Thread.GetFrames () [0];
1291 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1293 TypeMirror t = o.Type;
1295 Assert.AreEqual ("MonoType", t.GetTypeObject ().Type.Name);
1299 public void VTypes () {
1300 Event e = run_until ("vtypes1");
1301 StackFrame frame = e.Thread.GetFrames () [0];
1304 ObjectMirror o = frame.GetThis () as ObjectMirror;
1305 var obj = o.GetValue (o.Type.GetField ("field_struct"));
1306 Assert.IsTrue (obj is StructMirror);
1307 var s = obj as StructMirror;
1308 Assert.AreEqual ("AStruct", s.Type.Name);
1309 AssertValue (42, s ["i"]);
1311 AssertValue ("S", obj);
1312 AssertValue (43, s ["k"]);
1313 obj = o.GetValue (o.Type.GetField ("field_boxed_struct"));
1314 Assert.IsTrue (obj is StructMirror);
1315 s = obj as StructMirror;
1316 Assert.AreEqual ("AStruct", s.Type.Name);
1317 AssertValue (42, s ["i"]);
1319 // vtypes as arguments
1320 s = frame.GetArgument (0) as StructMirror;
1321 AssertValue (44, s ["i"]);
1323 AssertValue ("T", obj);
1324 AssertValue (45, s ["k"]);
1326 // vtypes as array entries
1327 var arr = frame.GetArgument (1) as ArrayMirror;
1329 Assert.IsTrue (obj is StructMirror);
1330 s = obj as StructMirror;
1331 AssertValue (1, s ["i"]);
1332 AssertValue ("S1", s ["s"]);
1334 Assert.IsTrue (obj is StructMirror);
1335 s = obj as StructMirror;
1336 AssertValue (2, s ["i"]);
1337 AssertValue ("S2", s ["s"]);
1339 // Argument checking
1340 s = frame.GetArgument (0) as StructMirror;
1341 AssertThrows<ArgumentException> (delegate () {
1345 // generic vtype instances
1346 o = frame.GetThis () as ObjectMirror;
1347 obj = o.GetValue (o.Type.GetField ("generic_field_struct"));
1348 Assert.IsTrue (obj is StructMirror);
1349 s = obj as StructMirror;
1350 Assert.AreEqual ("GStruct`1", s.Type.Name);
1351 AssertValue (42, s ["i"]);
1353 // this on vtype methods
1354 e = run_until ("vtypes2");
1356 e = single_step (e.Thread);
1358 frame = e.Thread.GetFrames () [0];
1360 Assert.AreEqual ("foo", (e as StepEvent).Method.Name);
1361 obj = frame.GetThis ();
1363 Assert.IsTrue (obj is StructMirror);
1364 s = obj as StructMirror;
1365 AssertValue (44, s ["i"]);
1366 AssertValue ("T", s ["s"]);
1367 AssertValue (45, s ["k"]);
1369 // this on static vtype methods
1370 e = run_until ("vtypes3");
1372 e = single_step (e.Thread);
1374 frame = e.Thread.GetFrames () [0];
1376 Assert.AreEqual ("static_foo", (e as StepEvent).Method.Name);
1377 obj = frame.GetThis ();
1378 AssertValue (null, obj);
1382 public void AssemblyInfo () {
1383 Event e = run_until ("single_stepping");
1385 StackFrame frame = e.Thread.GetFrames () [0];
1387 var aname = frame.Method.DeclaringType.Assembly.GetName ();
1388 Assert.AreEqual ("dtest-app, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", aname.ToString ());
1390 ModuleMirror m = frame.Method.DeclaringType.Module;
1392 Assert.AreEqual ("dtest-app.exe", m.Name);
1393 Assert.AreEqual ("dtest-app.exe", m.ScopeName);
1394 Assert.IsTrue (m.FullyQualifiedName.IndexOf ("dtest-app.exe") != -1);
1395 Guid guid = m.ModuleVersionId;
1396 Assert.AreEqual (frame.Method.DeclaringType.Assembly, m.Assembly);
1397 Assert.AreEqual (frame.Method.DeclaringType.Assembly.ManifestModule, m);
1399 // This is no longer true on 4.0
1400 //Assert.AreEqual ("Assembly", frame.Method.DeclaringType.Assembly.GetAssemblyObject ().Type.Name);
1402 TypeMirror t = vm.RootDomain.Corlib.GetType ("System.Diagnostics.DebuggerDisplayAttribute");
1403 Assert.AreEqual ("DebuggerDisplayAttribute", t.Name);
1407 public void LocalsInfo () {
1408 Event e = run_until ("locals2");
1410 StackFrame frame = e.Thread.GetFrames () [0];
1412 var locals = frame.Method.GetLocals ();
1413 Assert.AreEqual (7, locals.Length);
1414 for (int i = 0; i < 7; ++i) {
1415 if (locals [i].Name == "args") {
1416 Assert.IsTrue (locals [i].IsArg);
1417 Assert.AreEqual ("String[]", locals [i].Type.Name);
1418 } else if (locals [i].Name == "arg") {
1419 Assert.IsTrue (locals [i].IsArg);
1420 Assert.AreEqual ("Int32", locals [i].Type.Name);
1421 } else if (locals [i].Name == "i") {
1422 Assert.IsFalse (locals [i].IsArg);
1423 Assert.AreEqual ("Int64", locals [i].Type.Name);
1424 } else if (locals [i].Name == "j") {
1425 Assert.IsFalse (locals [i].IsArg);
1426 Assert.AreEqual ("Int32", locals [i].Type.Name);
1427 } else if (locals [i].Name == "s") {
1428 Assert.IsFalse (locals [i].IsArg);
1429 Assert.AreEqual ("String", locals [i].Type.Name);
1430 } else if (locals [i].Name == "t") {
1432 Assert.IsTrue (locals [i].IsArg);
1433 Assert.AreEqual ("String", locals [i].Type.Name);
1434 } else if (locals [i].Name == "rs") {
1435 Assert.IsTrue (locals [i].IsArg);
1436 Assert.AreEqual ("String", locals [i].Type.Name);
1444 public void Locals () {
1445 var be = run_until ("locals1");
1447 StackFrame frame = be.Thread.GetFrames () [0];
1449 MethodMirror m1 = frame.Method;
1451 be = run_until ("locals2");
1453 frame = be.Thread.GetFrames () [0];
1455 object val = frame.GetValue (frame.Method.GetLocal ("i"));
1456 AssertValue (0, val);
1459 var req = vm.CreateStepRequest (be.Thread);
1463 var e = GetNextEvent ();
1464 Assert.IsTrue (e is StepEvent);
1465 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1467 // Execute s = "AB";
1469 e = GetNextEvent ();
1470 Assert.IsTrue (e is StepEvent);
1471 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1473 frame = e.Thread.GetFrames () [0];
1475 val = frame.GetValue (frame.Method.GetLocal ("i"));
1476 AssertValue (42, val);
1478 LocalVariable[] locals = frame.Method.GetLocals ();
1479 var vals = frame.GetValues (locals);
1480 Assert.AreEqual (locals.Length, vals.Length);
1481 for (int i = 0; i < locals.Length; ++i) {
1482 if (locals [i].Name == "i")
1483 AssertValue (42, vals [i]);
1484 if (locals [i].Name == "s")
1485 AssertValue ("AB", vals [i]);
1486 if (locals [i].Name == "t")
1487 AssertValue ("ABC", vals [i]);
1490 // Argument checking
1493 AssertThrows<ArgumentNullException> (delegate () {
1494 frame.GetValue ((LocalVariable)null);
1496 // GetValue () local from another method
1497 AssertThrows<ArgumentException> (delegate () {
1498 frame.GetValue (m1.GetLocal ("foo"));
1502 AssertThrows<ArgumentNullException> (delegate () {
1503 frame.GetValue ((ParameterInfoMirror)null);
1505 // GetValue () local from another method
1506 AssertThrows<ArgumentException> (delegate () {
1507 frame.GetValue (m1.GetParameters ()[0]);
1510 // GetValues () null
1511 AssertThrows<ArgumentNullException> (delegate () {
1512 frame.GetValues (null);
1514 // GetValues () embedded null
1515 AssertThrows<ArgumentNullException> (delegate () {
1516 frame.GetValues (new LocalVariable [] { null });
1518 // GetValues () local from another method
1519 AssertThrows<ArgumentException> (delegate () {
1520 frame.GetValues (new LocalVariable [] { m1.GetLocal ("foo") });
1523 AssertThrows<ArgumentException> (delegate () {
1524 val = frame.GetValue (frame.Method.ReturnParameter);
1527 // invalid stack frames
1529 e = GetNextEvent ();
1530 Assert.IsTrue (e is StepEvent);
1531 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1533 AssertThrows<InvalidStackFrameException> (delegate () {
1534 frame.GetValue (frame.Method.GetLocal ("i"));
1541 public void GetVisibleVariables () {
1542 Event e = run_until ("locals4");
1545 var locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
1546 Assert.AreEqual (2, locals.Count);
1547 var loc = locals.First (l => l.Name == "i");
1548 Assert.AreEqual ("Int64", loc.Type.Name);
1549 loc = locals.First (l => l.Name == "s");
1550 Assert.AreEqual ("String", loc.Type.Name);
1552 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
1553 Assert.AreEqual ("i", loc.Name);
1554 Assert.AreEqual ("Int64", loc.Type.Name);
1556 e = run_until ("locals5");
1559 locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
1560 Assert.AreEqual (2, locals.Count);
1561 loc = locals.First (l => l.Name == "i");
1562 Assert.AreEqual ("String", loc.Type.Name);
1563 loc = locals.First (l => l.Name == "s");
1564 Assert.AreEqual ("String", loc.Type.Name);
1566 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
1567 Assert.AreEqual ("i", loc.Name);
1568 Assert.AreEqual ("String", loc.Type.Name);
1570 // Variable in another scope
1571 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("j");
1572 Assert.IsNull (loc);
1576 public void Exit () {
1581 var e = GetNextEvent ();
1582 Assert.IsInstanceOfType (typeof (VMDeathEvent), e);
1585 /* Could be a remote vm with no process */
1588 Assert.AreEqual (5, p.ExitCode);
1591 AssertThrows<VMDisconnectedException> (delegate () {
1600 public void Dispose () {
1605 var e = GetNextEvent ();
1606 Assert.IsInstanceOfType (typeof (VMDisconnectEvent), e);
1609 /* Could be a remote vm with no process */
1612 Assert.AreEqual (3, p.ExitCode);
1615 AssertThrows<VMDisconnectedException> (delegate () {
1624 public void ColumnNumbers () {
1625 Event e = run_until ("line_numbers");
1627 // FIXME: Merge this with LineNumbers () when its fixed
1629 step_req = vm.CreateStepRequest (e.Thread);
1630 step_req.Depth = StepDepth.Into;
1637 e = GetNextEvent ();
1638 Assert.IsTrue (e is StepEvent);
1640 l = e.Thread.GetFrames ()[0].Location;
1642 Assert.AreEqual (3, l.ColumnNumber);
1644 step_req.Disable ();
1648 // Broken by mcs+runtime changes (#5438)
1649 [Category("NotWorking")]
1650 public void LineNumbers () {
1651 Event e = run_until ("line_numbers");
1653 step_req = vm.CreateStepRequest (e.Thread);
1654 step_req.Depth = StepDepth.Into;
1661 e = GetNextEvent ();
1662 Assert.IsTrue (e is StepEvent);
1664 l = e.Thread.GetFrames ()[0].Location;
1666 Assert.IsTrue (l.SourceFile.IndexOf ("dtest-app.cs") != -1);
1667 Assert.AreEqual ("ln1", l.Method.Name);
1670 using (FileStream fs = new FileStream (l.SourceFile, FileMode.Open, FileAccess.Read)) {
1671 MD5 md5 = MD5.Create ();
1672 var hash = md5.ComputeHash (fs);
1674 for (int i = 0; i < 16; ++i)
1675 Assert.AreEqual (hash [i], l.SourceFileHash [i]);
1678 int line_base = l.LineNumber;
1681 e = GetNextEvent ();
1682 Assert.IsTrue (e is StepEvent);
1683 l = e.Thread.GetFrames ()[0].Location;
1684 Assert.AreEqual ("ln2", l.Method.Name);
1685 Assert.AreEqual (line_base + 6, l.LineNumber);
1688 e = GetNextEvent ();
1689 Assert.IsTrue (e is StepEvent);
1690 l = e.Thread.GetFrames ()[0].Location;
1691 Assert.AreEqual ("ln1", l.Method.Name);
1692 Assert.AreEqual (line_base + 1, l.LineNumber);
1695 e = GetNextEvent ();
1696 Assert.IsTrue (e is StepEvent);
1697 l = e.Thread.GetFrames ()[0].Location;
1698 Assert.AreEqual ("ln3", l.Method.Name);
1699 Assert.AreEqual (line_base + 11, l.LineNumber);
1702 e = GetNextEvent ();
1703 Assert.IsTrue (e is StepEvent);
1704 l = e.Thread.GetFrames ()[0].Location;
1705 Assert.AreEqual ("ln3", l.Method.Name);
1706 Assert.IsTrue (l.SourceFile.EndsWith ("FOO"));
1707 Assert.AreEqual (55, l.LineNumber);
1710 e = GetNextEvent ();
1711 Assert.IsTrue (e is StepEvent);
1712 l = e.Thread.GetFrames ()[0].Location;
1713 Assert.AreEqual ("ln1", l.Method.Name);
1714 Assert.AreEqual (line_base + 2, l.LineNumber);
1716 // GetSourceFiles ()
1717 string[] sources = l.Method.DeclaringType.GetSourceFiles ();
1718 Assert.AreEqual (2, sources.Length);
1719 Assert.AreEqual ("dtest-app.cs", sources [0]);
1720 Assert.AreEqual ("FOO", sources [1]);
1722 sources = l.Method.DeclaringType.GetSourceFiles (true);
1723 Assert.AreEqual (2, sources.Length);
1724 Assert.IsTrue (sources [0].EndsWith ("dtest-app.cs"));
1725 Assert.IsTrue (sources [1].EndsWith ("FOO"));
1729 public void Suspend () {
1732 Start (new string [] { "dtest-app.exe", "suspend-test" });
1734 Event e = run_until ("suspend");
1736 ThreadMirror main = e.Thread;
1744 // The debuggee should be suspended while it is running the infinite loop
1746 StackFrame frame = main.GetFrames ()[0];
1747 Assert.AreEqual ("suspend", frame.Method.Name);
1751 // resuming when not suspended
1752 AssertThrows<InvalidOperationException> (delegate () {
1762 public void AssemblyLoad () {
1763 Event e = run_until ("assembly_load");
1765 var load_req = vm.CreateAssemblyLoadRequest ();
1770 e = GetNextEvent ();
1771 Assert.IsInstanceOfType (typeof (AssemblyLoadEvent), e);
1772 Assert.IsTrue ((e as AssemblyLoadEvent).Assembly.Location.EndsWith ("System.dll"));
1774 var frames = e.Thread.GetFrames ();
1775 Assert.IsTrue (frames.Length > 0);
1776 Assert.AreEqual ("assembly_load", frames [0].Method.Name);
1780 public void CreateValue () {
1783 v = vm.CreateValue (1);
1784 Assert.AreEqual (vm, v.VirtualMachine);
1785 Assert.AreEqual (1, v.Value);
1787 v = vm.CreateValue (null);
1788 Assert.AreEqual (vm, v.VirtualMachine);
1789 Assert.AreEqual (null, v.Value);
1791 // Argument checking
1792 AssertThrows <ArgumentException> (delegate () {
1793 v = vm.CreateValue ("FOO");
1798 public void CreateString () {
1799 StringMirror s = vm.RootDomain.CreateString ("ABC");
1801 Assert.AreEqual (vm, s.VirtualMachine);
1802 Assert.AreEqual ("ABC", s.Value);
1803 Assert.AreEqual (vm.RootDomain, s.Domain);
1806 StringBuilder sb = new StringBuilder ();
1807 for (int i = 0; i < 1024; ++i)
1809 s = vm.RootDomain.CreateString (sb.ToString ());
1811 // Argument checking
1812 AssertThrows <ArgumentNullException> (delegate () {
1813 s = vm.RootDomain.CreateString (null);
1818 public void CreateBoxedValue () {
1819 ObjectMirror o = vm.RootDomain.CreateBoxedValue (new PrimitiveValue (vm, 42));
1821 Assert.AreEqual ("Int32", o.Type.Name);
1822 //AssertValue (42, m.GetValue (o.Type.GetField ("m_value")));
1824 // Argument checking
1825 AssertThrows <ArgumentNullException> (delegate () {
1826 vm.RootDomain.CreateBoxedValue (null);
1829 AssertThrows <ArgumentException> (delegate () {
1830 vm.RootDomain.CreateBoxedValue (o);
1835 public void Invoke () {
1836 Event e = run_until ("invoke1");
1838 StackFrame frame = e.Thread.GetFrames () [0];
1840 TypeMirror t = frame.Method.DeclaringType;
1841 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
1843 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
1849 m = t.GetMethod ("invoke_return_void");
1850 v = this_obj.InvokeMethod (e.Thread, m, null);
1854 m = t.GetMethod ("invoke_return_ref");
1855 v = this_obj.InvokeMethod (e.Thread, m, null);
1856 AssertValue ("ABC", v);
1859 m = t.GetMethod ("invoke_return_null");
1860 v = this_obj.InvokeMethod (e.Thread, m, null);
1861 AssertValue (null, v);
1864 m = t.GetMethod ("invoke_return_primitive");
1865 v = this_obj.InvokeMethod (e.Thread, m, null);
1866 AssertValue (42, v);
1869 m = t.GetMethod ("invoke_return_nullable");
1870 v = this_obj.InvokeMethod (e.Thread, m, null);
1871 Assert.IsInstanceOfType (typeof (StructMirror), v);
1872 var s = v as StructMirror;
1873 AssertValue (42, s.Fields [0]);
1874 AssertValue (true, s.Fields [1]);
1876 // pass nullable as this
1877 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
1878 m = s.Type.GetMethod ("ToString");
1879 v = s.InvokeMethod (e.Thread, m, null);
1881 // return nullable null
1882 m = t.GetMethod ("invoke_return_nullable_null");
1883 v = this_obj.InvokeMethod (e.Thread, m, null);
1884 Assert.IsInstanceOfType (typeof (StructMirror), v);
1885 s = v as StructMirror;
1886 AssertValue (0, s.Fields [0]);
1887 AssertValue (false, s.Fields [1]);
1889 // pass nullable as this
1890 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
1891 m = s.Type.GetMethod ("ToString");
1892 v = s.InvokeMethod (e.Thread, m, null);
1895 m = t.GetMethod ("invoke_pass_primitive");
1896 Value[] args = new Value [] {
1897 vm.CreateValue ((byte)Byte.MaxValue),
1898 vm.CreateValue ((sbyte)SByte.MaxValue),
1899 vm.CreateValue ((short)1),
1900 vm.CreateValue ((ushort)1),
1901 vm.CreateValue ((int)1),
1902 vm.CreateValue ((uint)1),
1903 vm.CreateValue ((long)1),
1904 vm.CreateValue ((ulong)1),
1905 vm.CreateValue ('A'),
1906 vm.CreateValue (true),
1907 vm.CreateValue (3.14f),
1908 vm.CreateValue (3.14) };
1910 v = this_obj.InvokeMethod (e.Thread, m, args);
1911 AssertValue ((int)Byte.MaxValue + (int)SByte.MaxValue + 1 + 1 + 1 + 1 + 1 + 1 + 'A' + 1 + 3 + 3, v);
1914 m = t.GetMethod ("invoke_pass_ref");
1915 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
1916 AssertValue ("ABC", v);
1919 m = t.GetMethod ("invoke_pass_ref");
1920 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (null) });
1921 AssertValue (null, v);
1924 m = t.GetMethod ("invoke_static_pass_ref");
1925 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
1926 AssertValue ("ABC", v);
1928 // static invoked using ObjectMirror.InvokeMethod
1929 m = t.GetMethod ("invoke_static_pass_ref");
1930 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
1931 AssertValue ("ABC", v);
1933 // method which throws an exception
1935 m = t.GetMethod ("invoke_throws");
1936 v = this_obj.InvokeMethod (e.Thread, m, null);
1938 } catch (InvocationException ex) {
1939 Assert.AreEqual ("Exception", ex.Exception.Type.Name);
1943 m = t.GetMethod (".ctor");
1944 v = t.InvokeMethod (e.Thread, m, null);
1945 Assert.IsInstanceOfType (typeof (ObjectMirror), v);
1946 Assert.AreEqual ("Tests", (v as ObjectMirror).Type.Name);
1948 // Argument checking
1951 AssertThrows<ArgumentNullException> (delegate {
1952 m = t.GetMethod ("invoke_pass_ref");
1953 v = this_obj.InvokeMethod (null, m, new Value [] { vm.CreateValue (null) });
1957 AssertThrows<ArgumentNullException> (delegate {
1958 v = this_obj.InvokeMethod (e.Thread, null, new Value [] { vm.CreateValue (null) });
1961 // invalid number of arguments
1962 m = t.GetMethod ("invoke_pass_ref");
1963 AssertThrows<ArgumentException> (delegate {
1964 v = this_obj.InvokeMethod (e.Thread, m, null);
1967 // invalid type of argument (ref != primitive)
1968 m = t.GetMethod ("invoke_pass_ref");
1969 AssertThrows<ArgumentException> (delegate {
1970 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
1973 // invalid type of argument (primitive != primitive)
1974 m = t.GetMethod ("invoke_pass_primitive_2");
1975 AssertThrows<ArgumentException> (delegate {
1976 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
1979 // invoking a non-static method as static
1980 m = t.GetMethod ("invoke_pass_ref");
1981 AssertThrows<ArgumentException> (delegate {
1982 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
1985 // invoking a method defined in another class
1986 m = t2.GetMethod ("invoke");
1987 AssertThrows<ArgumentException> (delegate {
1988 v = this_obj.InvokeMethod (e.Thread, m, null);
1993 public void InvokeVType () {
1994 Event e = run_until ("invoke1");
1996 StackFrame frame = e.Thread.GetFrames () [0];
1998 var s = frame.GetArgument (1) as StructMirror;
2000 TypeMirror t = s.Type;
2005 // Pass struct as this, receive int
2006 m = t.GetMethod ("invoke_return_int");
2007 v = s.InvokeMethod (e.Thread, m, null);
2008 AssertValue (42, v);
2010 // Pass struct as this, receive intptr
2011 m = t.GetMethod ("invoke_return_intptr");
2012 v = s.InvokeMethod (e.Thread, m, null);
2013 AssertValue (43, v);
2016 m = t.GetMethod ("invoke_static");
2017 v = t.InvokeMethod (e.Thread, m, null);
2020 // Pass generic struct as this
2021 s = frame.GetArgument (2) as StructMirror;
2023 m = t.GetMethod ("invoke_return_int");
2024 v = s.InvokeMethod (e.Thread, m, null);
2025 AssertValue (42, v);
2029 public void BreakpointDuringInvoke () {
2030 Event e = run_until ("invoke1");
2032 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke2");
2033 Assert.IsNotNull (m);
2034 vm.SetBreakpoint (m, 0);
2036 StackFrame frame = e.Thread.GetFrames () [0];
2037 var o = frame.GetThis () as ObjectMirror;
2039 bool failed = false;
2041 bool finished = false;
2042 object wait = new object ();
2044 // Have to invoke in a separate thread as the invoke is suspended until we
2045 // resume after the breakpoint
2046 Thread t = new Thread (delegate () {
2048 o.InvokeMethod (e.Thread, m, null);
2054 Monitor.Pulse (wait);
2060 StackFrame invoke_frame = null;
2063 e = GetNextEvent ();
2064 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
2065 // Check stack trace support and invokes
2066 var frames = e.Thread.GetFrames ();
2067 invoke_frame = frames [0];
2068 Assert.AreEqual ("invoke2", frames [0].Method.Name);
2069 Assert.IsTrue (frames [0].IsDebuggerInvoke);
2070 Assert.AreEqual ("invoke1", frames [1].Method.Name);
2077 Monitor.Wait (wait);
2080 // Check that the invoke frames are no longer valid
2081 AssertThrows<InvalidStackFrameException> (delegate {
2082 invoke_frame.GetThis ();
2085 // Check InvokeOptions.DisableBreakpoints flag
2086 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2090 public void DisabledExceptionDuringInvoke () {
2091 Event e = run_until ("invoke_ex");
2093 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_ex_inner");
2095 StackFrame frame = e.Thread.GetFrames () [0];
2096 var o = frame.GetThis () as ObjectMirror;
2098 var req = vm.CreateExceptionRequest (null);
2101 // Check InvokeOptions.DisableBreakpoints flag
2102 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2108 public void InvokeSingleThreaded () {
2111 Start (new string [] { "dtest-app.exe", "invoke-single-threaded" });
2113 Event e = run_until ("invoke_single_threaded_2");
2115 StackFrame f = e.Thread.GetFrames ()[0];
2117 var obj = f.GetThis () as ObjectMirror;
2119 // Check that the counter value incremented by the other thread does not increase
2120 // during the invoke.
2121 object counter1 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2123 var m = obj.Type.GetMethod ("invoke_return_void");
2124 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2126 object counter2 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2128 Assert.AreEqual ((int)counter1, (int)counter2);
2130 // Test multiple invokes done in succession
2131 m = obj.Type.GetMethod ("invoke_return_void");
2132 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2134 // Test events during single-threaded invokes
2135 vm.EnableEvents (EventType.TypeLoad);
2136 m = obj.Type.GetMethod ("invoke_type_load");
2137 obj.BeginInvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded, delegate {
2141 e = GetNextEvent ();
2142 Assert.AreEqual (EventType.TypeLoad, e.EventType);
2146 public void GetThreads () {
2151 public void Threads () {
2152 Event e = run_until ("threads");
2154 Assert.AreEqual (ThreadState.Running, e.Thread.ThreadState);
2156 Assert.IsTrue (e.Thread.ThreadId > 0);
2158 Assert.AreEqual (e.Thread.TID, e.Thread.TID);
2160 vm.EnableEvents (EventType.ThreadStart, EventType.ThreadDeath);
2164 e = GetNextEvent ();
2165 Assert.IsInstanceOfType (typeof (ThreadStartEvent), e);
2166 var state = e.Thread.ThreadState;
2167 Assert.IsTrue (state == ThreadState.Running || state == ThreadState.Unstarted);
2171 e = GetNextEvent ();
2172 Assert.IsInstanceOfType (typeof (ThreadDeathEvent), e);
2173 Assert.AreEqual (ThreadState.Stopped, e.Thread.ThreadState);
2177 public void Frame_SetValue () {
2178 Event e = run_until ("locals2");
2180 StackFrame frame = e.Thread.GetFrames () [0];
2183 var l = frame.Method.GetLocal ("i");
2184 frame.SetValue (l, vm.CreateValue ((long)55));
2185 AssertValue (55, frame.GetValue (l));
2188 l = frame.Method.GetLocal ("s");
2189 frame.SetValue (l, vm.RootDomain.CreateString ("DEF"));
2190 AssertValue ("DEF", frame.GetValue (l));
2192 // argument as local
2193 l = frame.Method.GetLocal ("arg");
2194 frame.SetValue (l, vm.CreateValue (6));
2195 AssertValue (6, frame.GetValue (l));
2198 var p = frame.Method.GetParameters ()[1];
2199 frame.SetValue (p, vm.CreateValue (7));
2200 AssertValue (7, frame.GetValue (p));
2203 p = frame.Method.GetParameters ()[2];
2204 frame.SetValue (p, vm.RootDomain.CreateString ("DEF"));
2205 AssertValue ("DEF", frame.GetValue (p));
2208 p = frame.Method.GetParameters ()[3];
2209 frame.SetValue (p, vm.RootDomain.CreateString ("DEF2"));
2210 AssertValue ("DEF2", frame.GetValue (p));
2212 // argument checking
2215 AssertThrows<ArgumentNullException> (delegate () {
2216 frame.SetValue ((LocalVariable)null, vm.CreateValue (55));
2220 AssertThrows<ArgumentNullException> (delegate () {
2221 l = frame.Method.GetLocal ("i");
2222 frame.SetValue (l, null);
2225 // value of invalid type
2226 AssertThrows<ArgumentException> (delegate () {
2227 l = frame.Method.GetLocal ("i");
2228 frame.SetValue (l, vm.CreateValue (55));
2234 public void Frame_SetValue_Registers () {
2235 Event e = run_until ("locals6_1");
2237 StackFrame frame = e.Thread.GetFrames () [1];
2240 var l = frame.Method.GetLocal ("j");
2241 frame.SetValue (l, vm.CreateValue (99));
2242 AssertValue (99, frame.GetValue (l));
2244 // Check it during execution
2245 e = run_until ("locals6_2");
2246 frame = e.Thread.GetFrames () [0];
2247 AssertValue (99, frame.GetValue (frame.Method.GetParameters ()[0]));
2249 // Set it while in a frame which clobbers its register
2250 e = run_until ("locals6_3");
2251 frame = e.Thread.GetFrames () [1];
2252 frame.SetValue (l, vm.CreateValue (100));
2253 AssertValue (100, frame.GetValue (l));
2255 // Check it during execution
2256 e = run_until ("locals6_4");
2257 frame = e.Thread.GetFrames () [0];
2258 AssertValue (100, frame.GetValue (frame.Method.GetParameters ()[0]));
2260 // Signed byte value
2261 e = run_until ("locals6_5");
2262 frame = e.Thread.GetFrames () [1];
2263 var l2 = frame.Method.GetLocal ("sb");
2264 frame.SetValue (l2, vm.CreateValue ((sbyte)-99));
2265 AssertValue (-99, frame.GetValue (l2));
2267 // Check it during execution
2268 e = run_until ("locals6_6");
2269 frame = e.Thread.GetFrames () [0];
2270 AssertValue (-99, frame.GetValue (frame.Method.GetParameters ()[0]));
2274 public void InvokeRegress () {
2275 Event e = run_until ("invoke1");
2277 StackFrame frame = e.Thread.GetFrames () [0];
2279 TypeMirror t = frame.Method.DeclaringType;
2280 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2282 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2288 m = t.GetMethod ("invoke_return_void");
2289 v = this_obj.InvokeMethod (e.Thread, m, null);
2292 // Check that the stack frames remain valid during the invoke
2293 Assert.AreEqual ("Tests", (frame.GetThis () as ObjectMirror).Type.Name);
2295 // do another invoke
2296 m = t.GetMethod ("invoke_return_void");
2297 v = this_obj.InvokeMethod (e.Thread, m, null);
2300 // Try a single step after the invoke
2301 var req = vm.CreateStepRequest (e.Thread);
2302 req.Depth = StepDepth.Into;
2303 req.Size = StepSize.Line;
2308 // Step into invoke2
2310 e = GetNextEvent ();
2311 Assert.IsTrue (e is StepEvent);
2312 Assert.AreEqual ("invoke2", (e as StepEvent).Method.Name);
2316 frame = e.Thread.GetFrames () [0];
2320 public void Exceptions () {
2321 Event e = run_until ("exceptions");
2322 var req = vm.CreateExceptionRequest (null);
2327 e = GetNextEvent ();
2328 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2329 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2331 var frames = e.Thread.GetFrames ();
2332 Assert.AreEqual ("exceptions", frames [0].Method.Name);
2335 // exception type filter
2337 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.ArgumentException"));
2340 // Skip the throwing of the second OverflowException
2343 e = GetNextEvent ();
2344 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2345 Assert.AreEqual ("ArgumentException", (e as ExceptionEvent).Exception.Type.Name);
2348 // exception type filter for subclasses
2349 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.Exception"));
2354 e = GetNextEvent ();
2355 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2356 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2359 // Implicit exceptions
2360 req = vm.CreateExceptionRequest (null);
2365 e = GetNextEvent ();
2366 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2367 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
2370 // Single stepping after an exception
2371 req = vm.CreateExceptionRequest (null);
2376 e = GetNextEvent ();
2377 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2378 Assert.AreEqual ("Exception", (e as ExceptionEvent).Exception.Type.Name);
2379 frames = e.Thread.GetFrames ();
2380 Assert.AreEqual ("exceptions2", frames [0].Method.Name);
2383 var sreq = vm.CreateStepRequest (e.Thread);
2384 sreq.Depth = StepDepth.Over;
2385 sreq.Size = StepSize.Line;
2389 e = GetNextEvent ();
2390 Assert.IsInstanceOfType (typeof (StepEvent), e);
2391 frames = e.Thread.GetFrames ();
2392 Assert.AreEqual ("exceptions", frames [0].Method.Name);
2395 // Argument checking
2396 AssertThrows<ArgumentException> (delegate {
2397 vm.CreateExceptionRequest (e.Thread.Type);
2402 public void ExceptionFilter () {
2403 Event e = run_until ("exception_filter");
2405 MethodMirror m = entry_point.DeclaringType.GetMethod ("exception_filter_filter");
2406 Assert.IsNotNull (m);
2408 vm.SetBreakpoint (m, 0);
2412 e = GetNextEvent ();
2413 Assert.AreEqual (EventType.Breakpoint, e.EventType);
2414 Assert.IsTrue (e is BreakpointEvent);
2415 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
2417 var frames = e.Thread.GetFrames ();
2419 Assert.IsTrue (frames [0].Location.SourceFile.IndexOf ("dtest-app.cs") != -1);
2420 Assert.AreEqual ("exception_filter_filter", frames [0].Location.Method.Name);
2422 Assert.AreEqual (0, frames [1].Location.Method.MetadataToken);
2423 Assert.AreEqual (0x0f, frames [1].Location.ILOffset);
2425 Assert.AreEqual ("exception_filter_method", frames [2].Location.Method.Name);
2426 Assert.AreEqual (0x05, frames [2].Location.ILOffset);
2428 Assert.AreEqual (0, frames [3].Location.Method.MetadataToken, 0);
2429 Assert.AreEqual (0, frames [3].Location.ILOffset);
2431 Assert.AreEqual ("exception_filter", frames [4].Location.Method.Name);
2435 public void ExceptionFilter2 () {
2438 Start (new string [] { "dtest-excfilter.exe" });
2440 MethodMirror filter_method = entry_point.DeclaringType.GetMethod ("Filter");
2441 Assert.IsNotNull (filter_method);
2443 MethodMirror test_method = entry_point.DeclaringType.GetMethod ("Test");
2444 Assert.IsNotNull (test_method);
2446 vm.SetBreakpoint (filter_method, 0);
2450 var e = GetNextEvent ();
2451 Assert.AreEqual (EventType.Breakpoint, e.EventType);
2452 Assert.IsTrue (e is BreakpointEvent);
2453 Assert.AreEqual (filter_method.Name, (e as BreakpointEvent).Method.Name);
2455 var frames = e.Thread.GetFrames ();
2457 Assert.AreEqual (4, frames.Count ());
2459 Assert.AreEqual (filter_method.Name, frames [0].Location.Method.Name);
2460 Assert.AreEqual (20, frames [0].Location.LineNumber);
2461 Assert.AreEqual (0, frames [0].Location.ILOffset);
2463 Assert.AreEqual (test_method.Name, frames [1].Location.Method.Name);
2464 Assert.AreEqual (37, frames [1].Location.LineNumber);
2465 Assert.AreEqual (0x0b, frames [1].Location.ILOffset);
2467 Assert.AreEqual (test_method.Name, frames [2].Location.Method.Name);
2468 Assert.AreEqual (33, frames [2].Location.LineNumber);
2469 Assert.AreEqual (0x05, frames [2].Location.ILOffset);
2471 Assert.AreEqual (entry_point.Name, frames [3].Location.Method.Name);
2472 Assert.AreEqual (14, frames [3].Location.LineNumber);
2473 Assert.AreEqual (0x00, frames [3].Location.ILOffset);
2481 public void EventSets () {
2483 // Create two filter which both match the same exception
2485 Event e = run_until ("exceptions");
2487 var req = vm.CreateExceptionRequest (null);
2490 var req2 = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.OverflowException"));
2495 var es = vm.GetNextEventSet ();
2496 Assert.AreEqual (2, es.Events.Length);
2499 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2500 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2503 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2504 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2511 // Test single threaded invokes during processing of nullref exceptions.
2512 // These won't work if the exception handling is done from the sigsegv signal
2513 // handler, since the sigsegv signal is disabled until control returns from the
2517 [Category ("only3")]
2518 public void NullRefExceptionAndSingleThreadedInvoke () {
2519 Event e = run_until ("exceptions");
2520 var req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.NullReferenceException"));
2525 e = GetNextEvent ();
2526 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2527 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
2529 var ex = (e as ExceptionEvent).Exception;
2530 var tostring_method = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2531 ex.InvokeMethod (e.Thread, tostring_method, null, InvokeOptions.SingleThreaded);
2535 public void Domains () {
2538 Start (new string [] { "dtest-app.exe", "domain-test" });
2540 vm.EnableEvents (EventType.AppDomainCreate, EventType.AppDomainUnload, EventType.AssemblyUnload);
2542 Event e = run_until ("domains");
2546 e = GetNextEvent ();
2547 Assert.IsInstanceOfType (typeof (AppDomainCreateEvent), e);
2549 var domain = (e as AppDomainCreateEvent).Domain;
2551 // Check the object type
2552 e = run_until ("domains_2");
2553 var frame = e.Thread.GetFrames ()[0];
2554 var o = frame.GetArgument (0) as ObjectMirror;
2555 Assert.AreEqual ("CrossDomain", o.Type.Name);
2557 // Do a remoting invoke
2558 var cross_domain_type = o.Type;
2559 var v = o.InvokeMethod (e.Thread, cross_domain_type.GetMethod ("invoke_3"), null);
2560 AssertValue (42, v);
2562 // Run until the callback in the domain
2563 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_in_domain");
2564 Assert.IsNotNull (m);
2565 vm.SetBreakpoint (m, 0);
2569 e = GetNextEvent ();
2570 if (e is BreakpointEvent)
2574 Assert.AreEqual ("invoke_in_domain", (e as BreakpointEvent).Method.Name);
2576 // d_method is from another domain
2577 MethodMirror d_method = (e as BreakpointEvent).Method;
2578 Assert.IsTrue (m != d_method);
2580 var frames = e.Thread.GetFrames ();
2581 Assert.AreEqual ("invoke_in_domain", frames [0].Method.Name);
2582 Assert.AreEqual ("invoke", frames [1].Method.Name);
2583 Assert.AreEqual ("domains", frames [2].Method.Name);
2585 // Test breakpoints on already JITted methods in other domains
2586 m = entry_point.DeclaringType.GetMethod ("invoke_in_domain_2");
2587 Assert.IsNotNull (m);
2588 vm.SetBreakpoint (m, 0);
2592 e = GetNextEvent ();
2593 if (e is BreakpointEvent)
2597 Assert.AreEqual ("invoke_in_domain_2", (e as BreakpointEvent).Method.Name);
2599 // This is empty when receiving the AppDomainCreateEvent
2600 Assert.AreEqual ("domain", domain.FriendlyName);
2602 // Run until the unload
2605 e = GetNextEvent ();
2606 if (e is AssemblyUnloadEvent) {
2612 Assert.IsInstanceOfType (typeof (AppDomainUnloadEvent), e);
2613 Assert.AreEqual (domain, (e as AppDomainUnloadEvent).Domain);
2615 // Run past the unload
2616 e = run_until ("domains_3");
2618 // Test access to unloaded types
2619 // FIXME: Add an exception type for this
2620 AssertThrows<Exception> (delegate {
2621 d_method.DeclaringType.GetValue (d_method.DeclaringType.GetField ("static_i"));
2626 public void DynamicMethods () {
2627 Event e = run_until ("dyn_call");
2629 var m = e.Thread.GetFrames ()[1].Method;
2630 Assert.AreEqual ("dyn_method", m.Name);
2632 // Test access to IL
2633 var body = m.GetMethodBody ();
2635 ILInstruction ins = body.Instructions [0];
2636 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
2637 Assert.AreEqual ("FOO", ins.Operand);
2641 public void RefEmit () {
2644 Start (new string [] { "dtest-app.exe", "ref-emit-test" });
2646 Event e = run_until ("ref_emit_call");
2648 var m = e.Thread.GetFrames ()[1].Method;
2649 Assert.AreEqual ("ref_emit_method", m.Name);
2651 // Test access to IL
2652 var body = m.GetMethodBody ();
2656 ins = body.Instructions [0];
2657 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
2658 Assert.AreEqual ("FOO", ins.Operand);
2660 ins = body.Instructions [1];
2661 Assert.AreEqual (OpCodes.Call, ins.OpCode);
2662 Assert.IsInstanceOfType (typeof (MethodMirror), ins.Operand);
2663 Assert.AreEqual ("ref_emit_call", (ins.Operand as MethodMirror).Name);
2667 public void IsAttached () {
2668 var f = entry_point.DeclaringType.GetField ("is_attached");
2670 Event e = run_until ("Main");
2672 AssertValue (true, entry_point.DeclaringType.GetValue (f));
2676 public void StackTraceInNative () {
2677 // Check that stack traces can be produced for threads in native code
2680 Start (new string [] { "dtest-app.exe", "frames-in-native" });
2682 var e = run_until ("frames_in_native");
2684 // FIXME: This is racy
2691 StackFrame[] frames = e.Thread.GetFrames ();
2693 int frame_index = -1;
2694 for (int i = 0; i < frames.Length; ++i) {
2695 if (frames [i].Method.Name == "Sleep") {
2701 Assert.IsTrue (frame_index != -1);
2702 Assert.AreEqual ("Sleep", frames [frame_index].Method.Name);
2703 Assert.AreEqual ("frames_in_native", frames [frame_index + 1].Method.Name);
2704 Assert.AreEqual ("Main", frames [frame_index + 2].Method.Name);
2706 // Check that invokes are disabled for such threads
2707 TypeMirror t = frames [frame_index + 1].Method.DeclaringType;
2710 var m = t.GetMethod ("invoke_static_return_void");
2711 AssertThrows<InvalidOperationException> (delegate {
2712 t.InvokeMethod (e.Thread, m, null);
2717 public void VirtualMachine_CreateEnumMirror () {
2718 var e = run_until ("o1");
2719 var frame = e.Thread.GetFrames () [0];
2721 object val = frame.GetThis ();
2722 Assert.IsTrue (val is ObjectMirror);
2723 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
2724 ObjectMirror o = (val as ObjectMirror);
2726 FieldInfoMirror field = o.Type.GetField ("field_enum");
2727 Value f = o.GetValue (field);
2728 TypeMirror enumType = (f as EnumMirror).Type;
2730 o.SetValue (field, vm.CreateEnumMirror (enumType, vm.CreateValue (1)));
2731 f = o.GetValue (field);
2732 Assert.AreEqual (1, (f as EnumMirror).Value);
2734 // Argument checking
2735 AssertThrows<ArgumentNullException> (delegate () {
2736 vm.CreateEnumMirror (enumType, null);
2739 AssertThrows<ArgumentNullException> (delegate () {
2740 vm.CreateEnumMirror (null, vm.CreateValue (1));
2744 AssertThrows<ArgumentException> (delegate () {
2745 vm.CreateEnumMirror (enumType, vm.CreateValue (null));
2748 // value of a wrong type
2749 AssertThrows<ArgumentException> (delegate () {
2750 vm.CreateEnumMirror (enumType, vm.CreateValue ((long)1));
2755 public void VirtualMachine_EnableEvents_Breakpoint () {
2756 AssertThrows<ArgumentException> (delegate () {
2757 vm.EnableEvents (EventType.Breakpoint);
2762 public void SingleStepRegress654694 () {
2765 MethodMirror m = entry_point.DeclaringType.GetMethod ("ss_regress_654694");
2766 foreach (Location l in m.Locations) {
2767 if (l.ILOffset > 0 && il_offset == -1)
2768 il_offset = l.ILOffset;
2771 Event e = run_until ("ss_regress_654694");
2773 Assert.IsNotNull (m);
2774 vm.SetBreakpoint (m, il_offset);
2778 e = GetNextEvent ();
2779 Assert.IsTrue (e is BreakpointEvent);
2781 var req = vm.CreateStepRequest (e.Thread);
2782 req.Depth = StepDepth.Over;
2783 req.Size = StepSize.Line;
2788 e = GetNextEvent ();
2789 Assert.IsTrue (e is StepEvent);
2795 public void DebugBreak () {
2796 vm.EnableEvents (EventType.UserBreak);
2801 var e = GetNextEvent ();
2802 Assert.IsTrue (e is UserBreakEvent);
2806 public void DebugLog () {
2807 vm.EnableEvents (EventType.UserLog);
2812 var e = GetNextEvent ();
2813 Assert.IsTrue (e is UserLogEvent);
2814 var le = e as UserLogEvent;
2816 Assert.AreEqual (5, le.Level);
2817 Assert.AreEqual ("A", le.Category);
2818 Assert.AreEqual ("B", le.Message);
2822 public void TypeGetMethodsByNameFlags () {
2824 var assembly = entry_point.DeclaringType.Assembly;
2825 var type = assembly.GetType ("Tests3");
2827 Assert.IsNotNull (type);
2829 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Public, false);
2830 Assert.AreEqual (1, mm.Length, "#1");
2831 Assert.AreEqual ("M1", mm[0].Name, "#2");
2833 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.NonPublic, false);
2834 Assert.AreEqual (1, mm.Length, "#3");
2835 Assert.AreEqual ("M2", mm[0].Name, "#4");
2837 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public, false);
2838 Assert.AreEqual (7, mm.Length, "#5"); //M3 plus Equals, GetHashCode, GetType, ToString, .ctor
2840 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, false);
2841 Assert.AreEqual (2, mm.Length, "#7");
2843 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
2844 Assert.AreEqual (1, mm.Length, "#9");
2846 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
2847 Assert.AreEqual (5, mm.Length, "#11");
2850 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Public, false);
2851 Assert.AreEqual (1, mm.Length, "#12");
2852 Assert.AreEqual ("M1", mm[0].Name, "#13");
2854 mm = type.GetMethodsByNameFlags ("m1", BindingFlags.Static | BindingFlags.Public, true);
2855 Assert.AreEqual (1, mm.Length, "#14");
2856 Assert.AreEqual ("M1", mm[0].Name, "#15");
2858 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, false);
2859 Assert.AreEqual (1, mm.Length, "#16");
2860 Assert.AreEqual ("M1", mm[0].Name, "#17");
2864 [Category ("only88")]
2865 public void TypeLoadSourceFileFilter () {
2866 Event e = run_until ("type_load");
2868 if (!vm.Version.AtLeast (2, 7))
2871 string srcfile = (e as BreakpointEvent).Method.DeclaringType.GetSourceFiles (true)[0];
2873 var req = vm.CreateTypeLoadRequest ();
2874 req.SourceFileFilter = new string [] { srcfile.ToUpper () };
2878 e = GetNextEvent ();
2879 Assert.IsTrue (e is TypeLoadEvent);
2880 Assert.AreEqual ("TypeLoadClass", (e as TypeLoadEvent).Type.FullName);
2884 public void TypeLoadTypeNameFilter () {
2885 Event e = run_until ("type_load");
2887 var req = vm.CreateTypeLoadRequest ();
2888 req.TypeNameFilter = new string [] { "TypeLoadClass2" };
2892 e = GetNextEvent ();
2893 Assert.IsTrue (e is TypeLoadEvent);
2894 Assert.AreEqual ("TypeLoadClass2", (e as TypeLoadEvent).Type.FullName);
2898 public void GetTypesForSourceFile () {
2901 var types = vm.GetTypesForSourceFile ("dtest-app.cs", false);
2902 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
2903 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
2905 types = vm.GetTypesForSourceFile ("DTEST-app.cs", true);
2906 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
2907 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
2911 public void GetTypesNamed () {
2914 var types = vm.GetTypes ("Tests", false);
2915 Assert.AreEqual (1, types.Count);
2916 Assert.AreEqual ("Tests", types [0].FullName);
2918 types = vm.GetTypes ("System.Exception", false);
2919 Assert.AreEqual (1, types.Count);
2920 Assert.AreEqual ("System.Exception", types [0].FullName);
2924 public void String_GetChars () {
2928 var e = run_until ("arg2");
2930 var frame = e.Thread.GetFrames () [0];
2932 val = frame.GetArgument (0);
2933 Assert.IsTrue (val is StringMirror);
2934 AssertValue ("FOO", val);
2935 var s = (val as StringMirror);
2936 Assert.AreEqual (3, s.Length);
2938 var c = s.GetChars (0, 2);
2939 Assert.AreEqual (2, c.Length);
2940 Assert.AreEqual ('F', c [0]);
2941 Assert.AreEqual ('O', c [1]);
2943 AssertThrows<ArgumentException> (delegate () {
2949 public void GetInterfaces () {
2950 var e = run_until ("arg2");
2952 var frame = e.Thread.GetFrames () [0];
2954 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
2955 var ifaces = cl1.GetInterfaces ();
2956 Assert.AreEqual (1, ifaces.Length);
2957 Assert.AreEqual ("ITest", ifaces [0].Name);
2959 var cl2 = cl1.GetMethod ("Baz").ReturnType;
2960 var ifaces2 = cl2.GetInterfaces ();
2961 Assert.AreEqual (1, ifaces2.Length);
2962 Assert.AreEqual ("ITest`1", ifaces2 [0].Name);
2966 public void GetInterfaceMap () {
2967 var e = run_until ("arg2");
2969 var frame = e.Thread.GetFrames () [0];
2971 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
2972 var iface = cl1.Assembly.GetType ("ITest");
2973 var map = cl1.GetInterfaceMap (iface);
2974 Assert.AreEqual (cl1, map.TargetType);
2975 Assert.AreEqual (iface, map.InterfaceType);
2976 Assert.AreEqual (2, map.InterfaceMethods.Length);
2977 Assert.AreEqual (2, map.TargetMethods.Length);
2981 public void StackAlloc_Breakpoints_Regress2775 () {
2982 // Check that breakpoints on arm don't overwrite stackalloc-ed memory
2983 var e = run_until ("regress_2755");
2985 var frame = e.Thread.GetFrames () [0];
2987 // This breaks at the call site
2988 vm.SetBreakpoint (m, m.Locations [2].ILOffset);
2991 var e2 = GetNextEvent ();
2992 Assert.IsTrue (e2 is BreakpointEvent);
2994 e = run_until ("regress_2755_3");
2995 frame = e.Thread.GetFrames () [1];
2996 var res = frame.GetValue (m.GetLocal ("sum"));
2997 AssertValue (0, res);
3001 public void MethodInfo () {
3002 Event e = run_until ("locals2");
3004 StackFrame frame = e.Thread.GetFrames () [0];
3005 var m = frame.Method;
3007 Assert.IsTrue (m.IsGenericMethod);
3008 Assert.IsFalse (m.IsGenericMethodDefinition);
3010 var args = m.GetGenericArguments ();
3011 Assert.AreEqual (1, args.Length);
3012 Assert.AreEqual ("String", args [0].Name);
3014 var gmd = m.GetGenericMethodDefinition ();
3015 Assert.IsTrue (gmd.IsGenericMethod);
3016 Assert.IsTrue (gmd.IsGenericMethodDefinition);
3017 Assert.AreEqual (gmd, gmd.GetGenericMethodDefinition ());
3019 args = gmd.GetGenericArguments ();
3020 Assert.AreEqual (1, args.Length);
3021 Assert.AreEqual ("T", args [0].Name);
3025 public void UnhandledException () {
3028 Start (new string [] { "dtest-app.exe", "unhandled-exception" });
3030 var req = vm.CreateExceptionRequest (null, false, true);
3033 var e = run_until ("unhandled_exception");
3036 var e2 = GetNextEvent ();
3037 Assert.IsTrue (e2 is ExceptionEvent);