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
19 public class DebuggerTests
22 MethodMirror entry_point;
23 StepEventRequest step_req;
25 void AssertThrows<ExType> (Action del) where ExType : Exception {
33 Assert.IsTrue (thrown);
36 // No other way to pass arguments to the tests ?
37 public static bool listening = Environment.GetEnvironmentVariable ("DBG_SUSPEND") != null;
38 public static string runtime = Environment.GetEnvironmentVariable ("DBG_RUNTIME");
39 public static string agent_args = Environment.GetEnvironmentVariable ("DBG_AGENT_ARGS");
41 Event GetNextEvent () {
42 var es = vm.GetNextEventSet ();
43 Assert.AreEqual (1, es.Events.Length);
47 void Start (string[] args) {
49 var pi = new Diag.ProcessStartInfo ();
52 pi.FileName = runtime;
55 pi.Arguments = String.Join (" ", args);
56 vm = VirtualMachineManager.Launch (pi, new LaunchOptions { AgentArgs = agent_args });
58 var ep = new IPEndPoint (IPAddress.Any, 10000);
59 Console.WriteLine ("Listening on " + ep + "...");
60 vm = VirtualMachineManager.Listen (ep);
63 var load_req = vm.CreateAssemblyLoadRequest ();
66 Event vmstart = GetNextEvent ();
67 Assert.AreEqual (EventType.VMStart, vmstart.EventType);
76 /* Find out the entry point */
80 if (e is AssemblyLoadEvent) {
81 AssemblyLoadEvent ae = (AssemblyLoadEvent)e;
82 entry_point = ae.Assembly.EntryPoint;
83 if (entry_point != null)
93 BreakpointEvent run_until (string name) {
95 MethodMirror m = entry_point.DeclaringType.GetMethod (name);
97 vm.SetBreakpoint (m, 0);
104 if (e is BreakpointEvent)
108 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
109 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
111 return (e as BreakpointEvent);
114 Event single_step (ThreadMirror t) {
115 var req = vm.CreateStepRequest (t);
119 Event e = GetNextEvent ();
120 Assert.IsTrue (e is StepEvent);
127 void check_arg_val (StackFrame frame, int pos, Type type, object eval) {
128 object val = frame.GetArgument (pos);
129 Assert.IsTrue (val is PrimitiveValue);
130 object v = (val as PrimitiveValue).Value;
131 Assert.AreEqual (type, v.GetType ());
133 Assert.IsTrue (Math.Abs ((float)eval - (float)v) < 0.0001);
134 else if (eval is double)
135 Assert.IsTrue (Math.Abs ((double)eval - (double)v) < 0.0001);
137 Assert.AreEqual (eval, v);
140 void AssertValue (object expected, object val) {
141 if (expected is string) {
142 Assert.IsTrue (val is StringMirror);
143 Assert.AreEqual (expected, (val as StringMirror).Value);
144 } else if (val is StructMirror && (val as StructMirror).Type.Name == "IntPtr") {
145 AssertValue (expected, (val as StructMirror).Fields [0]);
147 Assert.IsTrue (val is PrimitiveValue);
148 Assert.AreEqual (expected, (val as PrimitiveValue).Value);
153 public void SetUp () {
154 Start (new string [] { "dtest-app.exe" });
158 public void TearDown () {
162 if (step_req != null)
167 Event e = GetNextEvent ();
169 if (e is VMDeathEvent)
177 public void SimpleBreakpoint () {
180 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp1");
181 Assert.IsNotNull (m);
183 vm.SetBreakpoint (m, 0);
188 Assert.AreEqual (EventType.Breakpoint, e.EventType);
189 Assert.IsTrue (e is BreakpointEvent);
190 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
193 AssertThrows<ArgumentException> (delegate {
195 vm.SetBreakpoint (m, 1);
200 public void BreakpointsSameLocation () {
201 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp2");
202 Assert.IsNotNull (m);
204 vm.SetBreakpoint (m, 0);
205 vm.SetBreakpoint (m, 0);
209 var es = vm.GetNextEventSet ();
210 Assert.AreEqual (2, es.Events.Length);
211 Assert.IsTrue (es [0] is BreakpointEvent);
212 Assert.AreEqual (m, (es [0] as BreakpointEvent).Method);
214 Assert.IsTrue (es [1] is BreakpointEvent);
215 Assert.AreEqual (m.Name, (es [1] as BreakpointEvent).Method.Name);
219 public void BreakpointAlreadyJITted () {
220 Event e = run_until ("bp1");
222 /* Place a breakpoint on bp3 */
223 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp3");
224 Assert.IsNotNull (m);
225 vm.SetBreakpoint (m, 0);
227 /* Same with generic instances */
228 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp7");
229 Assert.IsNotNull (m2);
230 vm.SetBreakpoint (m2, 0);
235 Assert.AreEqual (EventType.Breakpoint, e.EventType);
236 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
240 /* Non-shared instance */
242 Assert.AreEqual (EventType.Breakpoint, e.EventType);
243 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
247 /* Shared instance */
249 Assert.AreEqual (EventType.Breakpoint, e.EventType);
250 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
254 public void ClearBreakpoint () {
257 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
258 Assert.IsNotNull (m);
259 EventRequest req1 = vm.SetBreakpoint (m, 0);
260 EventRequest req2 = vm.SetBreakpoint (m, 0);
262 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
263 Assert.IsNotNull (m2);
264 vm.SetBreakpoint (m2, 0);
269 var es = vm.GetNextEventSet ();
270 Assert.AreEqual (2, es.Events.Length);
271 Assert.AreEqual (EventType.Breakpoint, es [0].EventType);
272 Assert.AreEqual (m.Name, (es [0] as BreakpointEvent).Method.Name);
273 Assert.AreEqual (EventType.Breakpoint, es [1].EventType);
274 Assert.AreEqual (m.Name, (es [1] as BreakpointEvent).Method.Name);
276 /* Clear one of them */
282 Assert.AreEqual (EventType.Breakpoint, e.EventType);
283 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
285 /* Clear the other */
291 Assert.AreEqual (EventType.Breakpoint, e.EventType);
292 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
296 public void ClearAllBreakpoints () {
299 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
300 Assert.IsNotNull (m);
301 vm.SetBreakpoint (m, 0);
303 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
304 Assert.IsNotNull (m2);
305 vm.SetBreakpoint (m2, 0);
307 vm.ClearAllBreakpoints ();
312 Assert.IsTrue (!(e is BreakpointEvent));
313 if (e is VMDeathEvent)
318 public void BreakpointOnGShared () {
321 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp6");
322 Assert.IsNotNull (m);
324 vm.SetBreakpoint (m, 0);
329 Assert.AreEqual (EventType.Breakpoint, e.EventType);
330 Assert.IsTrue (e is BreakpointEvent);
331 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
333 // Breakpoint on an open generic method of a closed generic class (#3422)
334 var frame = e.Thread.GetFrames ()[0];
335 var ginst = frame.GetValue (frame.Method.GetLocal ("gc"));
336 var m2 = (ginst as ObjectMirror).Type.GetMethod ("bp");
337 vm.SetBreakpoint (m2, 0);
342 Assert.AreEqual (EventType.Breakpoint, e.EventType);
343 Assert.IsTrue (e is BreakpointEvent);
344 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
348 public void SingleStepping () {
349 Event e = run_until ("single_stepping");
351 var req = vm.CreateStepRequest (e.Thread);
356 // Step over 'bool b = true'
359 Assert.IsTrue (e is StepEvent);
360 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
365 Assert.IsTrue (e is StepEvent);
366 Assert.AreEqual ("ss1", (e as StepEvent).Method.Name);
371 Assert.IsTrue (e is StepEvent);
372 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
374 // Change to step over
376 req.Depth = StepDepth.Over;
382 Assert.IsTrue (e is StepEvent);
383 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
385 // Change to step into
387 req.Depth = StepDepth.Into;
393 Assert.IsTrue (e is StepEvent);
394 Assert.AreEqual ("ss3", (e as StepEvent).Method.Name);
396 // Change to step out
398 req.Depth = StepDepth.Out;
401 // Step back into single_stepping
404 Assert.IsTrue (e is StepEvent);
405 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
407 // Change to step into
409 req.Depth = StepDepth.Into;
412 // Step into ss3_2 ()
415 Assert.IsTrue (e is StepEvent);
416 Assert.AreEqual ("ss3_2", (e as StepEvent).Method.Name);
418 // Change to step over
420 req.Depth = StepDepth.Over;
423 // Step over ss3_2_2 ()
426 Assert.IsTrue (e is StepEvent);
427 Assert.AreEqual ("ss3_2", (e as StepEvent).Method.Name);
429 // Recreate the request
433 // Step back into single_stepping () with the new request
436 Assert.IsTrue (e is StepEvent);
437 Assert.AreEqual ("single_stepping", (e as StepEvent).Method.Name);
439 // Change to step into
441 req.Depth = StepDepth.Into;
447 Assert.IsTrue (e is StepEvent);
448 Assert.AreEqual ("ss4", (e as StepEvent).Method.Name);
450 // Change to StepSize.Line
452 req.Depth = StepDepth.Over;
453 req.Size = StepSize.Line;
456 // Step over ss1 (); ss1 ();
459 Assert.IsTrue (e is StepEvent);
463 req.Depth = StepDepth.Into;
468 Assert.IsTrue (e is StepEvent);
469 Assert.AreEqual ("ss2", (e as StepEvent).Method.Name);
474 e = run_until ("ss5");
476 // Add an assembly filter
477 req.AssemblyFilter = new AssemblyMirror [] { (e as BreakpointEvent).Method.DeclaringType.Assembly };
480 // Step into is_even, skipping the linq stuff
483 Assert.IsTrue (e is StepEvent);
484 Assert.AreEqual ("is_even", (e as StepEvent).Method.Name);
486 // FIXME: Check that single stepping works with lock (obj)
491 e = run_until ("ss6");
493 req = vm.CreateStepRequest (e.Thread);
494 req.Depth = StepDepth.Over;
497 // Check that single stepping works in out-of-line bblocks
500 Assert.IsTrue (e is StepEvent);
503 Assert.IsTrue (e is StepEvent);
504 Assert.AreEqual ("ss6", (e as StepEvent).Method.Name);
510 public void MethodEntryExit () {
511 run_until ("single_stepping");
513 var req1 = vm.CreateMethodEntryRequest ();
514 var req2 = vm.CreateMethodExitRequest ();
520 Event e = GetNextEvent ();
521 Assert.IsTrue (e is MethodEntryEvent);
522 Assert.AreEqual ("ss1", (e as MethodEntryEvent).Method.Name);
526 Assert.IsTrue (e is MethodExitEvent);
527 Assert.AreEqual ("ss1", (e as MethodExitEvent).Method.Name);
534 public void CountFilter () {
535 run_until ("single_stepping");
537 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("ss3");
538 Assert.IsNotNull (m2);
539 vm.SetBreakpoint (m2, 0);
541 var req1 = vm.CreateMethodEntryRequest ();
545 // Enter ss2, ss1 is skipped
547 Event e = GetNextEvent ();
548 Assert.IsTrue (e is MethodEntryEvent);
549 Assert.AreEqual ("ss2", (e as MethodEntryEvent).Method.Name);
551 // Breakpoint on ss3, the entry event is no longer reported
554 Assert.IsTrue (e is BreakpointEvent);
560 public void Arguments () {
563 var e = run_until ("arg1");
565 StackFrame frame = e.Thread.GetFrames () [0];
567 check_arg_val (frame, 0, typeof (sbyte), SByte.MaxValue - 5);
568 check_arg_val (frame, 1, typeof (byte), Byte.MaxValue - 5);
569 check_arg_val (frame, 2, typeof (bool), true);
570 check_arg_val (frame, 3, typeof (short), Int16.MaxValue - 5);
571 check_arg_val (frame, 4, typeof (ushort), UInt16.MaxValue - 5);
572 check_arg_val (frame, 5, typeof (char), 'F');
573 check_arg_val (frame, 6, typeof (int), Int32.MaxValue - 5);
574 check_arg_val (frame, 7, typeof (uint), UInt32.MaxValue - 5);
575 check_arg_val (frame, 8, typeof (long), Int64.MaxValue - 5);
576 check_arg_val (frame, 9, typeof (ulong), UInt64.MaxValue - 5);
577 check_arg_val (frame, 10, typeof (float), 1.2345f);
578 check_arg_val (frame, 11, typeof (double), 6.78910);
580 e = run_until ("arg2");
582 frame = e.Thread.GetFrames () [0];
585 val = frame.GetArgument (0);
586 AssertValue ("FOO", val);
587 Assert.AreEqual ("String", (val as ObjectMirror).Type.Name);
590 val = frame.GetArgument (1);
591 AssertValue (null, val);
594 val = frame.GetArgument (2);
595 AssertValue ("BLA", val);
598 val = frame.GetArgument (3);
599 AssertValue (42, val);
602 val = frame.GetArgument (4);
603 Assert.IsTrue (val is ObjectMirror);
604 Assert.AreEqual ("GClass`1", (val as ObjectMirror).Type.Name);
607 val = frame.GetArgument (5);
608 Assert.IsTrue (val is ObjectMirror);
609 Assert.AreEqual ("Object", (val as ObjectMirror).Type.Name);
611 // this on static methods
612 val = frame.GetThis ();
613 AssertValue (null, val);
615 e = run_until ("arg3");
617 frame = e.Thread.GetFrames () [0];
620 val = frame.GetThis ();
621 Assert.IsTrue (val is ObjectMirror);
622 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
624 // objref in register
625 val = frame.GetArgument (0);
626 AssertValue ("BLA", val);
630 public void Arrays () {
633 var e = run_until ("o2");
635 StackFrame frame = e.Thread.GetFrames () [0];
638 val = frame.GetArgument (0);
639 Assert.IsTrue (val is ArrayMirror);
640 ArrayMirror arr = val as ArrayMirror;
641 Assert.AreEqual (2, arr.Length);
642 AssertValue ("BAR", arr [0]);
643 AssertValue ("BAZ", arr [1]);
645 var vals = arr.GetValues (0, 2);
646 Assert.AreEqual (2, vals.Count);
647 AssertValue ("BAR", vals [0]);
648 AssertValue ("BAZ", vals [1]);
650 arr [0] = vm.RootDomain.CreateString ("ABC");
651 AssertValue ("ABC", arr [0]);
653 arr [0] = vm.CreateValue (null);
654 AssertValue (null, arr [0]);
656 arr.SetValues (0, new Value [] { vm.RootDomain.CreateString ("D1"), vm.RootDomain.CreateString ("D2") });
657 AssertValue ("D1", arr [0]);
658 AssertValue ("D2", arr [1]);
661 val = frame.GetArgument (1);
662 Assert.IsTrue (val is ArrayMirror);
663 arr = val as ArrayMirror;
664 Assert.AreEqual (2, arr.Length);
665 AssertValue (42, arr [0]);
666 AssertValue (43, arr [1]);
669 AssertThrows<IndexOutOfRangeException> (delegate () {
673 AssertThrows<IndexOutOfRangeException> (delegate () {
674 val = arr [Int32.MinValue];
677 AssertThrows<IndexOutOfRangeException> (delegate () {
678 vals = arr.GetValues (0, 3);
681 AssertThrows<IndexOutOfRangeException> (delegate () {
682 arr [2] = vm.CreateValue (null);
685 AssertThrows<IndexOutOfRangeException> (delegate () {
686 arr [Int32.MinValue] = vm.CreateValue (null);
689 AssertThrows<IndexOutOfRangeException> (delegate () {
690 arr.SetValues (0, new Value [] { null, null, null });
694 val = frame.GetArgument (2);
695 Assert.IsTrue (val is ArrayMirror);
696 arr = val as ArrayMirror;
697 Assert.AreEqual (2, arr.Rank);
698 Assert.AreEqual (4, arr.Length);
699 Assert.AreEqual (2, arr.GetLength (0));
700 Assert.AreEqual (2, arr.GetLength (1));
701 Assert.AreEqual (0, arr.GetLowerBound (0));
702 Assert.AreEqual (0, arr.GetLowerBound (1));
703 vals = arr.GetValues (0, 4);
704 AssertValue (1, vals [0]);
705 AssertValue (2, vals [1]);
706 AssertValue (3, vals [2]);
707 AssertValue (4, vals [3]);
709 val = frame.GetArgument (3);
710 Assert.IsTrue (val is ArrayMirror);
711 arr = val as ArrayMirror;
712 Assert.AreEqual (2, arr.Rank);
713 Assert.AreEqual (4, arr.Length);
714 Assert.AreEqual (2, arr.GetLength (0));
715 Assert.AreEqual (2, arr.GetLength (1));
716 Assert.AreEqual (1, arr.GetLowerBound (0));
717 Assert.AreEqual (3, arr.GetLowerBound (1));
719 AssertThrows<ArgumentOutOfRangeException> (delegate () {
722 AssertThrows<ArgumentOutOfRangeException> (delegate () {
726 AssertThrows<ArgumentOutOfRangeException> (delegate () {
727 arr.GetLowerBound (-1);
729 AssertThrows<ArgumentOutOfRangeException> (delegate () {
730 arr.GetLowerBound (2);
733 // arrays treated as generic collections
734 val = frame.GetArgument (4);
735 Assert.IsTrue (val is ArrayMirror);
736 arr = val as ArrayMirror;
740 public void Object_GetValue () {
741 var e = run_until ("o1");
742 var frame = e.Thread.GetFrames () [0];
744 object val = frame.GetThis ();
745 Assert.IsTrue (val is ObjectMirror);
746 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
747 ObjectMirror o = (val as ObjectMirror);
749 TypeMirror t = o.Type;
752 object f = o.GetValue (t.GetField ("field_i"));
754 f = o.GetValue (t.GetField ("field_s"));
755 AssertValue ("S", f);
756 f = o.GetValue (t.GetField ("field_enum"));
757 Assert.IsTrue (f is EnumMirror);
758 Assert.AreEqual (1, (f as EnumMirror).Value);
759 Assert.AreEqual ("B", (f as EnumMirror).StringValue);
761 // Inherited object fields
762 TypeMirror parent = t.BaseType;
763 f = o.GetValue (parent.GetField ("base_field_i"));
765 f = o.GetValue (parent.GetField ("base_field_s"));
766 AssertValue ("T", f);
769 f = o.GetValue (o.Type.GetField ("static_i"));
773 ObjectMirror o2 = frame.GetValue (frame.Method.GetParameters ()[1]) as ObjectMirror;
774 Assert.AreEqual ("GClass`1", o2.Type.Name);
775 TypeMirror t2 = o2.Type;
776 f = o2.GetValue (t2.GetField ("field"));
779 ObjectMirror o3 = frame.GetValue (frame.Method.GetParameters ()[2]) as ObjectMirror;
780 Assert.AreEqual ("GClass`1", o3.Type.Name);
781 TypeMirror t3 = o3.Type;
782 f = o3.GetValue (t3.GetField ("field"));
783 AssertValue ("FOO", f);
786 AssertThrows<ArgumentNullException> (delegate () {
792 public void Object_GetValues () {
793 var e = run_until ("o1");
794 var frame = e.Thread.GetFrames () [0];
796 object val = frame.GetThis ();
797 Assert.IsTrue (val is ObjectMirror);
798 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
799 ObjectMirror o = (val as ObjectMirror);
801 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
803 TypeMirror t = o.Type;
805 object[] vals = o.GetValues (new FieldInfoMirror [] { t.GetField ("field_i"), t.GetField ("field_s") });
809 AssertValue ("S", f);
812 AssertThrows<ArgumentNullException> (delegate () {
816 AssertThrows<ArgumentNullException> (delegate () {
817 o.GetValues (new FieldInfoMirror [] { null });
820 // field of another class
821 AssertThrows<ArgumentException> (delegate () {
822 o.GetValue (val2.Type.GetField ("field_j"));
826 void TestSetValue (ObjectMirror o, string field_name, object val) {
828 o.SetValue (o.Type.GetField (field_name), vm.RootDomain.CreateString ((string)val));
830 o.SetValue (o.Type.GetField (field_name), vm.CreateValue (val));
831 Value f = o.GetValue (o.Type.GetField (field_name));
832 AssertValue (val, f);
836 public void Object_SetValues () {
837 var e = run_until ("o1");
838 var frame = e.Thread.GetFrames () [0];
840 object val = frame.GetThis ();
841 Assert.IsTrue (val is ObjectMirror);
842 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
843 ObjectMirror o = (val as ObjectMirror);
845 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
847 TestSetValue (o, "field_i", 22);
848 TestSetValue (o, "field_bool1", false);
849 TestSetValue (o, "field_bool2", true);
850 TestSetValue (o, "field_char", 'B');
851 TestSetValue (o, "field_byte", (byte)129);
852 TestSetValue (o, "field_sbyte", (sbyte)-33);
853 TestSetValue (o, "field_short", (short)(Int16.MaxValue - 5));
854 TestSetValue (o, "field_ushort", (ushort)(UInt16.MaxValue - 5));
855 TestSetValue (o, "field_long", Int64.MaxValue - 5);
856 TestSetValue (o, "field_ulong", (ulong)(UInt64.MaxValue - 5));
857 TestSetValue (o, "field_float", 6.28f);
858 TestSetValue (o, "field_double", 6.28);
859 TestSetValue (o, "static_i", 23);
860 TestSetValue (o, "field_s", "CDEF");
865 f = o.GetValue (o.Type.GetField ("field_intptr"));
866 Assert.IsInstanceOfType (typeof (StructMirror), f);
867 AssertValue (Int32.MaxValue - 5, (f as StructMirror).Fields [0]);
871 FieldInfoMirror field = o.Type.GetField ("field_enum");
872 f = o.GetValue (field);
873 (f as EnumMirror).Value = 5;
874 o.SetValue (field, f);
875 f = o.GetValue (field);
876 Assert.AreEqual (5, (f as EnumMirror).Value);
879 o.SetValue (o.Type.GetField ("field_s"), vm.CreateValue (null));
880 f = o.GetValue (o.Type.GetField ("field_s"));
881 AssertValue (null, f);
884 field = o.Type.GetField ("generic_field_struct");
885 f = o.GetValue (field);
886 o.SetValue (field, f);
889 AssertThrows<ArgumentNullException> (delegate () {
890 o.SetValues (null, new Value [0]);
893 AssertThrows<ArgumentNullException> (delegate () {
894 o.SetValues (new FieldInfoMirror [0], null);
897 AssertThrows<ArgumentNullException> (delegate () {
898 o.SetValues (new FieldInfoMirror [] { null }, new Value [1] { null });
901 // vtype with a wrong type
902 AssertThrows<ArgumentException> (delegate () {
903 o.SetValue (o.Type.GetField ("field_struct"), o.GetValue (o.Type.GetField ("field_enum")));
906 // reference type not assignment compatible
907 AssertThrows<ArgumentException> (delegate () {
908 o.SetValue (o.Type.GetField ("field_class"), o);
911 // field of another class
912 AssertThrows<ArgumentException> (delegate () {
913 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
918 public void Type_SetValue () {
919 var e = run_until ("o1");
920 var frame = e.Thread.GetFrames () [0];
923 object val = frame.GetThis ();
924 Assert.IsTrue (val is ObjectMirror);
925 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
926 ObjectMirror o = (val as ObjectMirror);
928 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
930 o.Type.SetValue (o.Type.GetField ("static_i"), vm.CreateValue (55));
931 f = o.Type.GetValue (o.Type.GetField ("static_i"));
934 o.Type.SetValue (o.Type.GetField ("static_s"), vm.RootDomain.CreateString ("B"));
935 f = o.Type.GetValue (o.Type.GetField ("static_s"));
936 AssertValue ("B", f);
939 AssertThrows<ArgumentNullException> (delegate () {
940 o.Type.SetValue (null, vm.CreateValue (0));
943 AssertThrows<ArgumentNullException> (delegate () {
944 o.Type.SetValue (o.Type.GetField ("static_i"), null);
947 // field of another class
948 AssertThrows<ArgumentException> (delegate () {
949 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
954 public void TypeInfo () {
955 Event e = run_until ("ti2");
956 StackFrame frame = e.Thread.GetFrames () [0];
961 t = frame.Method.GetParameters ()[0].ParameterType;
963 Assert.AreEqual ("String[]", t.Name);
964 Assert.AreEqual ("string[]", t.CSharpName);
965 Assert.AreEqual ("Array", t.BaseType.Name);
966 Assert.AreEqual (true, t.HasElementType);
967 Assert.AreEqual (true, t.IsArray);
968 Assert.AreEqual (1, t.GetArrayRank ());
969 Assert.AreEqual ("String", t.GetElementType ().Name);
971 t = frame.Method.GetParameters ()[2].ParameterType;
973 Assert.AreEqual ("Int32[,]", t.Name);
975 //Assert.AreEqual ("int[,]", t.CSharpName);
976 Assert.AreEqual ("Array", t.BaseType.Name);
977 Assert.AreEqual (true, t.HasElementType);
978 Assert.AreEqual (true, t.IsArray);
979 Assert.AreEqual (2, t.GetArrayRank ());
980 Assert.AreEqual ("Int32", t.GetElementType ().Name);
983 t = frame.Method.GetParameters ()[3].ParameterType;
985 //Assert.AreEqual ("Int32&", t.Name);
986 //Assert.AreEqual (true, t.IsByRef);
987 //Assert.AreEqual (true, t.HasElementType);
990 t = frame.Method.GetParameters ()[4].ParameterType;
992 //Assert.AreEqual ("Int32*", t.Name);
993 Assert.AreEqual (true, t.IsPointer);
994 Assert.AreEqual (true, t.HasElementType);
995 Assert.AreEqual ("Int32", t.GetElementType ().Name);
996 Assert.AreEqual (false, t.IsPrimitive);
999 t = frame.Method.GetParameters ()[5].ParameterType;
1000 Assert.AreEqual (true, t.IsPrimitive);
1003 t = frame.Method.GetParameters ()[6].ParameterType;
1004 Assert.AreEqual ("AStruct", t.Name);
1005 Assert.AreEqual (false, t.IsPrimitive);
1006 Assert.AreEqual (true, t.IsValueType);
1007 Assert.AreEqual (false, t.IsClass);
1010 t = frame.Method.GetParameters ()[7].ParameterType;
1011 Assert.AreEqual ("Tests", t.Name);
1012 var nested = (from nt in t.GetNestedTypes () where nt.IsNestedPublic select nt).ToArray ();
1013 Assert.AreEqual (1, nested.Length);
1014 Assert.AreEqual ("NestedClass", nested [0].Name);
1015 Assert.IsTrue (t.BaseType.IsAssignableFrom (t));
1016 Assert.IsTrue (!t.IsAssignableFrom (t.BaseType));
1018 // generic instances
1019 t = frame.Method.GetParameters ()[9].ParameterType;
1020 Assert.AreEqual ("GClass`1", t.Name);
1021 Assert.IsTrue (t.IsGenericType);
1022 Assert.IsFalse (t.IsGenericTypeDefinition);
1024 var args = t.GetGenericArguments ();
1025 Assert.AreEqual (1, args.Length);
1026 Assert.AreEqual ("Int32", args [0].Name);
1028 // generic type definitions
1029 var gtd = t.GetGenericTypeDefinition ();
1030 Assert.AreEqual ("GClass`1", gtd.Name);
1031 Assert.IsTrue (gtd.IsGenericType);
1032 Assert.IsTrue (gtd.IsGenericTypeDefinition);
1033 Assert.AreEqual (gtd, gtd.GetGenericTypeDefinition ());
1035 args = gtd.GetGenericArguments ();
1036 Assert.AreEqual (1, args.Length);
1037 Assert.AreEqual ("T", args [0].Name);
1040 t = frame.Method.GetParameters ()[10].ParameterType;
1041 Assert.AreEqual ("AnEnum", t.Name);
1042 Assert.IsTrue (t.IsEnum);
1043 Assert.AreEqual ("Int32", t.EnumUnderlyingType.Name);
1046 t = frame.Method.GetParameters ()[7].ParameterType;
1048 var props = t.GetProperties ();
1049 Assert.AreEqual (3, props.Length);
1050 foreach (PropertyInfoMirror prop in props) {
1051 ParameterInfoMirror[] indexes = prop.GetIndexParameters ();
1053 if (prop.Name == "IntProperty") {
1054 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1055 Assert.AreEqual ("get_IntProperty", prop.GetGetMethod ().Name);
1056 Assert.AreEqual ("set_IntProperty", prop.GetSetMethod ().Name);
1057 Assert.AreEqual (0, indexes.Length);
1058 } else if (prop.Name == "ReadOnlyProperty") {
1059 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1060 Assert.AreEqual ("get_ReadOnlyProperty", prop.GetGetMethod ().Name);
1061 Assert.AreEqual (null, prop.GetSetMethod ());
1062 Assert.AreEqual (0, indexes.Length);
1063 } else if (prop.Name == "IndexedProperty") {
1064 Assert.AreEqual (1, indexes.Length);
1065 Assert.AreEqual ("Int32", indexes [0].ParameterType.Name);
1069 // custom attributes
1070 t = frame.Method.GetParameters ()[8].ParameterType;
1071 Assert.AreEqual ("Tests2", t.Name);
1072 var attrs = t.GetCustomAttributes (true);
1073 Assert.AreEqual (2, attrs.Length);
1074 foreach (var attr in attrs) {
1075 if (attr.Constructor.DeclaringType.Name == "DebuggerDisplayAttribute") {
1076 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1077 Assert.AreEqual ("Tests", attr.ConstructorArguments [0].Value);
1078 Assert.AreEqual (2, attr.NamedArguments.Count);
1079 Assert.AreEqual ("Name", attr.NamedArguments [0].Property.Name);
1080 Assert.AreEqual ("FOO", attr.NamedArguments [0].TypedValue.Value);
1081 Assert.AreEqual ("Target", attr.NamedArguments [1].Property.Name);
1082 Assert.IsInstanceOfType (typeof (TypeMirror), attr.NamedArguments [1].TypedValue.Value);
1083 Assert.AreEqual ("Int32", (attr.NamedArguments [1].TypedValue.Value as TypeMirror).Name);
1084 } else if (attr.Constructor.DeclaringType.Name == "DebuggerTypeProxyAttribute") {
1085 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1086 Assert.IsInstanceOfType (typeof (TypeMirror), attr.ConstructorArguments [0].Value);
1087 Assert.AreEqual ("Tests", (attr.ConstructorArguments [0].Value as TypeMirror).Name);
1089 Assert.Fail (attr.Constructor.DeclaringType.Name);
1095 public void FieldInfo () {
1096 Event e = run_until ("ti2");
1097 StackFrame frame = e.Thread.GetFrames () [0];
1101 t = frame.Method.GetParameters ()[8].ParameterType;
1102 Assert.AreEqual ("Tests2", t.Name);
1104 var fi = t.GetField ("field_j");
1105 var attrs = fi.GetCustomAttributes (true);
1106 Assert.AreEqual (1, attrs.Length);
1107 var attr = attrs [0];
1108 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1109 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1110 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1111 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1115 public void PropertyInfo () {
1116 Event e = run_until ("ti2");
1117 StackFrame frame = e.Thread.GetFrames () [0];
1121 t = frame.Method.GetParameters ()[8].ParameterType;
1122 Assert.AreEqual ("Tests2", t.Name);
1124 var pi = t.GetProperty ("AProperty");
1125 var attrs = pi.GetCustomAttributes (true);
1126 Assert.AreEqual (1, attrs.Length);
1127 var attr = attrs [0];
1128 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1129 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1130 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1131 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1135 [Category ("only5")]
1136 public void Type_GetValue () {
1137 Event e = run_until ("o1");
1138 StackFrame frame = e.Thread.GetFrames () [0];
1140 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1142 TypeMirror t = o.Type;
1144 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1147 object f = t.GetValue (o.Type.GetField ("static_i"));
1148 AssertValue (55, f);
1150 f = t.GetValue (o.Type.GetField ("static_s"));
1151 AssertValue ("A", f);
1153 // literal static fields
1154 f = t.GetValue (o.Type.GetField ("literal_i"));
1155 AssertValue (56, f);
1157 f = t.GetValue (o.Type.GetField ("literal_s"));
1158 AssertValue ("B", f);
1160 // Inherited static fields
1161 TypeMirror parent = t.BaseType;
1162 f = t.GetValue (parent.GetField ("base_static_i"));
1163 AssertValue (57, f);
1165 f = t.GetValue (parent.GetField ("base_static_s"));
1166 AssertValue ("C", f);
1168 // thread static field
1169 f = t.GetValue (t.GetField ("tls_i"), e.Thread);
1170 AssertValue (42, f);
1172 // Argument checking
1173 AssertThrows<ArgumentNullException> (delegate () {
1178 AssertThrows<ArgumentException> (delegate () {
1179 t.GetValue (o.Type.GetField ("field_i"));
1182 // field on another type
1183 AssertThrows<ArgumentException> (delegate () {
1184 t.GetValue (val2.Type.GetField ("static_field_j"));
1187 // special static field
1188 AssertThrows<ArgumentException> (delegate () {
1189 t.GetValue (t.GetField ("tls_i"));
1194 public void Type_GetValues () {
1195 Event e = run_until ("o1");
1196 StackFrame frame = e.Thread.GetFrames () [0];
1198 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1200 TypeMirror t = o.Type;
1203 object[] vals = t.GetValues (new FieldInfoMirror [] { t.GetField ("static_i"), t.GetField ("static_s") });
1204 object f = vals [0];
1205 AssertValue (55, f);
1208 AssertValue ("A", f);
1210 // Argument checking
1211 AssertThrows<ArgumentNullException> (delegate () {
1215 AssertThrows<ArgumentNullException> (delegate () {
1216 t.GetValues (new FieldInfoMirror [] { null });
1221 public void ObjRefs () {
1222 Event e = run_until ("objrefs1");
1223 StackFrame frame = e.Thread.GetFrames () [0];
1225 ObjectMirror o = frame.GetThis () as ObjectMirror;
1226 ObjectMirror child = o.GetValue (o.Type.GetField ("child")) as ObjectMirror;
1228 Assert.IsTrue (child.Address != 0);
1230 // Check that object references are internalized correctly
1231 Assert.AreEqual (o, frame.GetThis ());
1233 run_until ("objrefs2");
1235 // child should be gc'd now
1236 Assert.IsTrue (child.IsCollected);
1239 * No longer works since Type is read eagerly
1242 AssertThrows<ObjectCollectedException> (delegate () {
1243 TypeMirror t = child.Type;
1247 AssertThrows<ObjectCollectedException> (delegate () {
1248 long addr = child.Address;
1253 public void Type_GetObject () {
1254 Event e = run_until ("o1");
1255 StackFrame frame = e.Thread.GetFrames () [0];
1257 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1259 TypeMirror t = o.Type;
1261 Assert.AreEqual ("MonoType", t.GetTypeObject ().Type.Name);
1265 public void VTypes () {
1266 Event e = run_until ("vtypes1");
1267 StackFrame frame = e.Thread.GetFrames () [0];
1270 ObjectMirror o = frame.GetThis () as ObjectMirror;
1271 var obj = o.GetValue (o.Type.GetField ("field_struct"));
1272 Assert.IsTrue (obj is StructMirror);
1273 var s = obj as StructMirror;
1274 Assert.AreEqual ("AStruct", s.Type.Name);
1275 AssertValue (42, s ["i"]);
1277 AssertValue ("S", obj);
1278 AssertValue (43, s ["k"]);
1279 obj = o.GetValue (o.Type.GetField ("field_boxed_struct"));
1280 Assert.IsTrue (obj is StructMirror);
1281 s = obj as StructMirror;
1282 Assert.AreEqual ("AStruct", s.Type.Name);
1283 AssertValue (42, s ["i"]);
1285 // vtypes as arguments
1286 s = frame.GetArgument (0) as StructMirror;
1287 AssertValue (44, s ["i"]);
1289 AssertValue ("T", obj);
1290 AssertValue (45, s ["k"]);
1292 // vtypes as array entries
1293 var arr = frame.GetArgument (1) as ArrayMirror;
1295 Assert.IsTrue (obj is StructMirror);
1296 s = obj as StructMirror;
1297 AssertValue (1, s ["i"]);
1298 AssertValue ("S1", s ["s"]);
1300 Assert.IsTrue (obj is StructMirror);
1301 s = obj as StructMirror;
1302 AssertValue (2, s ["i"]);
1303 AssertValue ("S2", s ["s"]);
1305 // Argument checking
1306 s = frame.GetArgument (0) as StructMirror;
1307 AssertThrows<ArgumentException> (delegate () {
1311 // generic vtype instances
1312 o = frame.GetThis () as ObjectMirror;
1313 obj = o.GetValue (o.Type.GetField ("generic_field_struct"));
1314 Assert.IsTrue (obj is StructMirror);
1315 s = obj as StructMirror;
1316 Assert.AreEqual ("GStruct`1", s.Type.Name);
1317 AssertValue (42, s ["i"]);
1319 // this on vtype methods
1320 e = run_until ("vtypes2");
1322 e = single_step (e.Thread);
1324 frame = e.Thread.GetFrames () [0];
1326 Assert.AreEqual ("foo", (e as StepEvent).Method.Name);
1327 obj = frame.GetThis ();
1329 Assert.IsTrue (obj is StructMirror);
1330 s = obj as StructMirror;
1331 AssertValue (44, s ["i"]);
1332 AssertValue ("T", s ["s"]);
1333 AssertValue (45, s ["k"]);
1335 // this on static vtype methods
1336 e = run_until ("vtypes3");
1338 e = single_step (e.Thread);
1340 frame = e.Thread.GetFrames () [0];
1342 Assert.AreEqual ("static_foo", (e as StepEvent).Method.Name);
1343 obj = frame.GetThis ();
1344 AssertValue (null, obj);
1348 public void AssemblyInfo () {
1349 Event e = run_until ("single_stepping");
1351 StackFrame frame = e.Thread.GetFrames () [0];
1353 var aname = frame.Method.DeclaringType.Assembly.GetName ();
1354 Assert.AreEqual ("dtest-app, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", aname.ToString ());
1356 ModuleMirror m = frame.Method.DeclaringType.Module;
1358 Assert.AreEqual ("dtest-app.exe", m.Name);
1359 Assert.AreEqual ("dtest-app.exe", m.ScopeName);
1360 Assert.IsTrue (m.FullyQualifiedName.IndexOf ("dtest-app.exe") != -1);
1361 Guid guid = m.ModuleVersionId;
1362 Assert.AreEqual (frame.Method.DeclaringType.Assembly, m.Assembly);
1363 Assert.AreEqual (frame.Method.DeclaringType.Assembly.ManifestModule, m);
1365 // This is no longer true on 4.0
1366 //Assert.AreEqual ("Assembly", frame.Method.DeclaringType.Assembly.GetAssemblyObject ().Type.Name);
1368 TypeMirror t = vm.RootDomain.Corlib.GetType ("System.Diagnostics.DebuggerDisplayAttribute");
1369 Assert.AreEqual ("DebuggerDisplayAttribute", t.Name);
1373 public void LocalsInfo () {
1374 Event e = run_until ("locals2");
1376 StackFrame frame = e.Thread.GetFrames () [0];
1378 var locals = frame.Method.GetLocals ();
1379 Assert.AreEqual (7, locals.Length);
1380 for (int i = 0; i < 7; ++i) {
1381 if (locals [i].Name == "args") {
1382 Assert.IsTrue (locals [i].IsArg);
1383 Assert.AreEqual ("String[]", locals [i].Type.Name);
1384 } else if (locals [i].Name == "arg") {
1385 Assert.IsTrue (locals [i].IsArg);
1386 Assert.AreEqual ("Int32", locals [i].Type.Name);
1387 } else if (locals [i].Name == "i") {
1388 Assert.IsFalse (locals [i].IsArg);
1389 Assert.AreEqual ("Int64", locals [i].Type.Name);
1390 } else if (locals [i].Name == "j") {
1391 Assert.IsFalse (locals [i].IsArg);
1392 Assert.AreEqual ("Int32", locals [i].Type.Name);
1393 } else if (locals [i].Name == "s") {
1394 Assert.IsFalse (locals [i].IsArg);
1395 Assert.AreEqual ("String", locals [i].Type.Name);
1396 } else if (locals [i].Name == "t") {
1398 Assert.IsTrue (locals [i].IsArg);
1399 Assert.AreEqual ("String", locals [i].Type.Name);
1400 } else if (locals [i].Name == "rs") {
1401 Assert.IsTrue (locals [i].IsArg);
1402 Assert.AreEqual ("String", locals [i].Type.Name);
1410 public void Locals () {
1411 var be = run_until ("locals1");
1413 StackFrame frame = be.Thread.GetFrames () [0];
1415 MethodMirror m1 = frame.Method;
1417 be = run_until ("locals2");
1419 frame = be.Thread.GetFrames () [0];
1421 object val = frame.GetValue (frame.Method.GetLocal ("i"));
1422 AssertValue (0, val);
1425 var req = vm.CreateStepRequest (be.Thread);
1429 var e = GetNextEvent ();
1430 Assert.IsTrue (e is StepEvent);
1431 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1433 // Execute s = "AB";
1435 e = GetNextEvent ();
1436 Assert.IsTrue (e is StepEvent);
1437 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1439 frame = e.Thread.GetFrames () [0];
1441 val = frame.GetValue (frame.Method.GetLocal ("i"));
1442 AssertValue (42, val);
1444 LocalVariable[] locals = frame.Method.GetLocals ();
1445 var vals = frame.GetValues (locals);
1446 Assert.AreEqual (locals.Length, vals.Length);
1447 for (int i = 0; i < locals.Length; ++i) {
1448 if (locals [i].Name == "i")
1449 AssertValue (42, vals [i]);
1450 if (locals [i].Name == "s")
1451 AssertValue ("AB", vals [i]);
1452 if (locals [i].Name == "t")
1453 AssertValue ("ABC", vals [i]);
1456 // Argument checking
1459 AssertThrows<ArgumentNullException> (delegate () {
1460 frame.GetValue ((LocalVariable)null);
1462 // GetValue () local from another method
1463 AssertThrows<ArgumentException> (delegate () {
1464 frame.GetValue (m1.GetLocal ("foo"));
1468 AssertThrows<ArgumentNullException> (delegate () {
1469 frame.GetValue ((ParameterInfoMirror)null);
1471 // GetValue () local from another method
1472 AssertThrows<ArgumentException> (delegate () {
1473 frame.GetValue (m1.GetParameters ()[0]);
1476 // GetValues () null
1477 AssertThrows<ArgumentNullException> (delegate () {
1478 frame.GetValues (null);
1480 // GetValues () embedded null
1481 AssertThrows<ArgumentNullException> (delegate () {
1482 frame.GetValues (new LocalVariable [] { null });
1484 // GetValues () local from another method
1485 AssertThrows<ArgumentException> (delegate () {
1486 frame.GetValues (new LocalVariable [] { m1.GetLocal ("foo") });
1489 AssertThrows<ArgumentException> (delegate () {
1490 val = frame.GetValue (frame.Method.ReturnParameter);
1493 // invalid stack frames
1495 e = GetNextEvent ();
1496 Assert.IsTrue (e is StepEvent);
1497 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1499 AssertThrows<InvalidStackFrameException> (delegate () {
1500 frame.GetValue (frame.Method.GetLocal ("i"));
1507 public void GetVisibleVariables () {
1508 Event e = run_until ("locals4");
1511 var locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
1512 Assert.AreEqual (2, locals.Count);
1513 var loc = locals.First (l => l.Name == "i");
1514 Assert.AreEqual ("Int64", loc.Type.Name);
1515 loc = locals.First (l => l.Name == "s");
1516 Assert.AreEqual ("String", loc.Type.Name);
1518 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
1519 Assert.AreEqual ("i", loc.Name);
1520 Assert.AreEqual ("Int64", loc.Type.Name);
1522 e = run_until ("locals5");
1525 locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
1526 Assert.AreEqual (2, locals.Count);
1527 loc = locals.First (l => l.Name == "i");
1528 Assert.AreEqual ("String", loc.Type.Name);
1529 loc = locals.First (l => l.Name == "s");
1530 Assert.AreEqual ("String", loc.Type.Name);
1532 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
1533 Assert.AreEqual ("i", loc.Name);
1534 Assert.AreEqual ("String", loc.Type.Name);
1536 // Variable in another scope
1537 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("j");
1538 Assert.IsNull (loc);
1542 public void Exit () {
1547 var e = GetNextEvent ();
1548 Assert.IsInstanceOfType (typeof (VMDeathEvent), e);
1551 /* Could be a remote vm with no process */
1554 Assert.AreEqual (5, p.ExitCode);
1557 AssertThrows<VMDisconnectedException> (delegate () {
1566 public void Dispose () {
1571 var e = GetNextEvent ();
1572 Assert.IsInstanceOfType (typeof (VMDisconnectEvent), e);
1575 /* Could be a remote vm with no process */
1578 Assert.AreEqual (3, p.ExitCode);
1581 AssertThrows<VMDisconnectedException> (delegate () {
1590 [Category("only88")]
1591 public void LineNumbers () {
1592 Event e = run_until ("line_numbers");
1594 step_req = vm.CreateStepRequest (e.Thread);
1595 step_req.Depth = StepDepth.Into;
1602 e = GetNextEvent ();
1603 Assert.IsTrue (e is StepEvent);
1605 l = e.Thread.GetFrames ()[0].Location;
1607 Assert.IsTrue (l.SourceFile.IndexOf ("dtest-app.cs") != -1);
1608 Assert.AreEqual ("ln1", l.Method.Name);
1611 using (FileStream fs = new FileStream (l.SourceFile, FileMode.Open, FileAccess.Read)) {
1612 MD5 md5 = MD5.Create ();
1613 var hash = md5.ComputeHash (fs);
1615 for (int i = 0; i < 16; ++i)
1616 Assert.AreEqual (hash [i], l.SourceFileHash [i]);
1619 int line_base = l.LineNumber;
1622 e = GetNextEvent ();
1623 Assert.IsTrue (e is StepEvent);
1624 l = e.Thread.GetFrames ()[0].Location;
1625 Assert.AreEqual ("ln2", l.Method.Name);
1626 Assert.AreEqual (line_base + 6, l.LineNumber);
1629 e = GetNextEvent ();
1630 Assert.IsTrue (e is StepEvent);
1631 l = e.Thread.GetFrames ()[0].Location;
1632 Assert.AreEqual ("ln1", l.Method.Name);
1633 Assert.AreEqual (line_base + 1, l.LineNumber);
1636 e = GetNextEvent ();
1637 Assert.IsTrue (e is StepEvent);
1638 l = e.Thread.GetFrames ()[0].Location;
1639 Assert.AreEqual ("ln3", l.Method.Name);
1640 Assert.AreEqual (line_base + 11, l.LineNumber);
1643 e = GetNextEvent ();
1644 Assert.IsTrue (e is StepEvent);
1645 l = e.Thread.GetFrames ()[0].Location;
1646 Assert.AreEqual ("ln3", l.Method.Name);
1647 Assert.IsTrue (l.SourceFile.EndsWith ("FOO"));
1648 Assert.AreEqual (55, l.LineNumber);
1651 e = GetNextEvent ();
1652 Assert.IsTrue (e is StepEvent);
1653 l = e.Thread.GetFrames ()[0].Location;
1654 Assert.AreEqual ("ln1", l.Method.Name);
1655 Assert.AreEqual (line_base + 2, l.LineNumber);
1657 // GetSourceFiles ()
1658 string[] sources = l.Method.DeclaringType.GetSourceFiles ();
1659 Assert.AreEqual (2, sources.Length);
1660 Assert.AreEqual ("dtest-app.cs", sources [0]);
1661 Assert.AreEqual ("FOO", sources [1]);
1663 sources = l.Method.DeclaringType.GetSourceFiles (true);
1664 Assert.AreEqual (2, sources.Length);
1665 Assert.IsTrue (sources [0].EndsWith ("dtest-app.cs"));
1666 Assert.IsTrue (sources [1].EndsWith ("FOO"));
1670 public void Suspend () {
1673 Start (new string [] { "dtest-app.exe", "suspend-test" });
1675 Event e = run_until ("suspend");
1677 ThreadMirror main = e.Thread;
1685 // The debuggee should be suspended while it is running the infinite loop
1687 StackFrame frame = main.GetFrames ()[0];
1688 Assert.AreEqual ("suspend", frame.Method.Name);
1692 // resuming when not suspended
1693 AssertThrows<InvalidOperationException> (delegate () {
1703 public void AssemblyLoad () {
1704 Event e = run_until ("assembly_load");
1706 var load_req = vm.CreateAssemblyLoadRequest ();
1711 e = GetNextEvent ();
1712 Assert.IsInstanceOfType (typeof (AssemblyLoadEvent), e);
1713 Assert.IsTrue ((e as AssemblyLoadEvent).Assembly.Location.EndsWith ("System.dll"));
1715 var frames = e.Thread.GetFrames ();
1716 Assert.IsTrue (frames.Length > 0);
1717 Assert.AreEqual ("assembly_load", frames [0].Method.Name);
1721 public void CreateValue () {
1724 v = vm.CreateValue (1);
1725 Assert.AreEqual (vm, v.VirtualMachine);
1726 Assert.AreEqual (1, v.Value);
1728 v = vm.CreateValue (null);
1729 Assert.AreEqual (vm, v.VirtualMachine);
1730 Assert.AreEqual (null, v.Value);
1732 // Argument checking
1733 AssertThrows <ArgumentException> (delegate () {
1734 v = vm.CreateValue ("FOO");
1739 public void CreateString () {
1740 StringMirror s = vm.RootDomain.CreateString ("ABC");
1742 Assert.AreEqual (vm, s.VirtualMachine);
1743 Assert.AreEqual ("ABC", s.Value);
1744 Assert.AreEqual (vm.RootDomain, s.Domain);
1747 StringBuilder sb = new StringBuilder ();
1748 for (int i = 0; i < 1024; ++i)
1750 s = vm.RootDomain.CreateString (sb.ToString ());
1752 // Argument checking
1753 AssertThrows <ArgumentNullException> (delegate () {
1754 s = vm.RootDomain.CreateString (null);
1759 public void CreateBoxedValue () {
1760 ObjectMirror o = vm.RootDomain.CreateBoxedValue (new PrimitiveValue (vm, 42));
1762 Assert.AreEqual ("Int32", o.Type.Name);
1763 //AssertValue (42, m.GetValue (o.Type.GetField ("m_value")));
1765 // Argument checking
1766 AssertThrows <ArgumentNullException> (delegate () {
1767 vm.RootDomain.CreateBoxedValue (null);
1770 AssertThrows <ArgumentException> (delegate () {
1771 vm.RootDomain.CreateBoxedValue (o);
1776 public void Invoke () {
1777 Event e = run_until ("invoke1");
1779 StackFrame frame = e.Thread.GetFrames () [0];
1781 TypeMirror t = frame.Method.DeclaringType;
1782 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
1784 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
1790 m = t.GetMethod ("invoke_return_void");
1791 v = this_obj.InvokeMethod (e.Thread, m, null);
1795 m = t.GetMethod ("invoke_return_ref");
1796 v = this_obj.InvokeMethod (e.Thread, m, null);
1797 AssertValue ("ABC", v);
1800 m = t.GetMethod ("invoke_return_null");
1801 v = this_obj.InvokeMethod (e.Thread, m, null);
1802 AssertValue (null, v);
1805 m = t.GetMethod ("invoke_return_primitive");
1806 v = this_obj.InvokeMethod (e.Thread, m, null);
1807 AssertValue (42, v);
1810 m = t.GetMethod ("invoke_return_nullable");
1811 v = this_obj.InvokeMethod (e.Thread, m, null);
1812 Assert.IsInstanceOfType (typeof (StructMirror), v);
1814 // return nullable null
1815 m = t.GetMethod ("invoke_return_nullable_null");
1816 v = this_obj.InvokeMethod (e.Thread, m, null);
1817 AssertValue (null, v);
1820 m = t.GetMethod ("invoke_pass_primitive");
1821 Value[] args = new Value [] {
1822 vm.CreateValue ((byte)Byte.MaxValue),
1823 vm.CreateValue ((sbyte)SByte.MaxValue),
1824 vm.CreateValue ((short)1),
1825 vm.CreateValue ((ushort)1),
1826 vm.CreateValue ((int)1),
1827 vm.CreateValue ((uint)1),
1828 vm.CreateValue ((long)1),
1829 vm.CreateValue ((ulong)1),
1830 vm.CreateValue ('A'),
1831 vm.CreateValue (true),
1832 vm.CreateValue (3.14f),
1833 vm.CreateValue (3.14) };
1835 v = this_obj.InvokeMethod (e.Thread, m, args);
1836 AssertValue ((int)Byte.MaxValue + (int)SByte.MaxValue + 1 + 1 + 1 + 1 + 1 + 1 + 'A' + 1 + 3 + 3, v);
1839 m = t.GetMethod ("invoke_pass_ref");
1840 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
1841 AssertValue ("ABC", v);
1844 m = t.GetMethod ("invoke_pass_ref");
1845 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (null) });
1846 AssertValue (null, v);
1849 m = t.GetMethod ("invoke_static_pass_ref");
1850 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
1851 AssertValue ("ABC", v);
1853 // static invoked using ObjectMirror.InvokeMethod
1854 m = t.GetMethod ("invoke_static_pass_ref");
1855 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
1856 AssertValue ("ABC", v);
1858 // method which throws an exception
1860 m = t.GetMethod ("invoke_throws");
1861 v = this_obj.InvokeMethod (e.Thread, m, null);
1863 } catch (InvocationException ex) {
1864 Assert.AreEqual ("Exception", ex.Exception.Type.Name);
1868 m = t.GetMethod (".ctor");
1869 v = t.InvokeMethod (e.Thread, m, null);
1870 Assert.IsInstanceOfType (typeof (ObjectMirror), v);
1871 Assert.AreEqual ("Tests", (v as ObjectMirror).Type.Name);
1873 // Argument checking
1876 AssertThrows<ArgumentNullException> (delegate {
1877 m = t.GetMethod ("invoke_pass_ref");
1878 v = this_obj.InvokeMethod (null, m, new Value [] { vm.CreateValue (null) });
1882 AssertThrows<ArgumentNullException> (delegate {
1883 v = this_obj.InvokeMethod (e.Thread, null, new Value [] { vm.CreateValue (null) });
1886 // invalid number of arguments
1887 m = t.GetMethod ("invoke_pass_ref");
1888 AssertThrows<ArgumentException> (delegate {
1889 v = this_obj.InvokeMethod (e.Thread, m, null);
1892 // invalid type of argument (ref != primitive)
1893 m = t.GetMethod ("invoke_pass_ref");
1894 AssertThrows<ArgumentException> (delegate {
1895 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
1898 // invalid type of argument (primitive != primitive)
1899 m = t.GetMethod ("invoke_pass_primitive_2");
1900 AssertThrows<ArgumentException> (delegate {
1901 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
1904 // invoking a non-static method as static
1905 m = t.GetMethod ("invoke_pass_ref");
1906 AssertThrows<ArgumentException> (delegate {
1907 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
1910 // invoking a method defined in another class
1911 m = t2.GetMethod ("invoke");
1912 AssertThrows<ArgumentException> (delegate {
1913 v = this_obj.InvokeMethod (e.Thread, m, null);
1918 public void InvokeVType () {
1919 Event e = run_until ("invoke1");
1921 StackFrame frame = e.Thread.GetFrames () [0];
1923 var s = frame.GetArgument (1) as StructMirror;
1925 TypeMirror t = s.Type;
1930 // Pass struct as this, receive int
1931 m = t.GetMethod ("invoke_return_int");
1932 v = s.InvokeMethod (e.Thread, m, null);
1933 AssertValue (42, v);
1935 // Pass struct as this, receive intptr
1936 m = t.GetMethod ("invoke_return_intptr");
1937 v = s.InvokeMethod (e.Thread, m, null);
1938 AssertValue (43, v);
1941 m = t.GetMethod ("invoke_static");
1942 v = t.InvokeMethod (e.Thread, m, null);
1945 // Pass generic struct as this
1946 s = frame.GetArgument (2) as StructMirror;
1948 m = t.GetMethod ("invoke_return_int");
1949 v = s.InvokeMethod (e.Thread, m, null);
1950 AssertValue (42, v);
1954 public void BreakpointDuringInvoke () {
1955 Event e = run_until ("invoke1");
1957 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke2");
1958 Assert.IsNotNull (m);
1959 vm.SetBreakpoint (m, 0);
1961 StackFrame frame = e.Thread.GetFrames () [0];
1962 var o = frame.GetThis () as ObjectMirror;
1964 bool failed = false;
1966 bool finished = false;
1967 object wait = new object ();
1969 // Have to invoke in a separate thread as the invoke is suspended until we
1970 // resume after the breakpoint
1971 Thread t = new Thread (delegate () {
1973 o.InvokeMethod (e.Thread, m, null);
1979 Monitor.Pulse (wait);
1985 StackFrame invoke_frame = null;
1988 e = GetNextEvent ();
1989 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
1990 // Check stack trace support and invokes
1991 var frames = e.Thread.GetFrames ();
1992 invoke_frame = frames [0];
1993 Assert.AreEqual ("invoke2", frames [0].Method.Name);
1994 Assert.IsTrue (frames [0].IsDebuggerInvoke);
1995 Assert.AreEqual ("invoke1", frames [1].Method.Name);
2002 Monitor.Wait (wait);
2005 // Check that the invoke frames are no longer valid
2006 AssertThrows<InvalidStackFrameException> (delegate {
2007 invoke_frame.GetThis ();
2010 // Check InvokeOptions.DisableBreakpoints flag
2011 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2015 public void DisabledExceptionDuringInvoke () {
2016 Event e = run_until ("invoke_ex");
2018 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_ex_inner");
2020 StackFrame frame = e.Thread.GetFrames () [0];
2021 var o = frame.GetThis () as ObjectMirror;
2023 var req = vm.CreateExceptionRequest (null);
2026 // Check InvokeOptions.DisableBreakpoints flag
2027 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2033 public void InvokeSingleThreaded () {
2036 Start (new string [] { "dtest-app.exe", "invoke-single-threaded" });
2038 Event e = run_until ("invoke_single_threaded_2");
2040 StackFrame f = e.Thread.GetFrames ()[0];
2042 var obj = f.GetThis () as ObjectMirror;
2044 // Check that the counter value incremented by the other thread does not increase
2045 // during the invoke.
2046 object counter1 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2048 var m = obj.Type.GetMethod ("invoke_return_void");
2049 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2051 object counter2 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2053 Assert.AreEqual ((int)counter1, (int)counter2);
2055 // Test multiple invokes done in succession
2056 m = obj.Type.GetMethod ("invoke_return_void");
2057 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2059 // Test events during single-threaded invokes
2060 vm.EnableEvents (EventType.TypeLoad);
2061 m = obj.Type.GetMethod ("invoke_type_load");
2062 obj.BeginInvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded, delegate {
2066 e = GetNextEvent ();
2067 Assert.AreEqual (EventType.TypeLoad, e.EventType);
2071 public void GetThreads () {
2076 public void Threads () {
2077 Event e = run_until ("threads");
2079 Assert.AreEqual (ThreadState.Running, e.Thread.ThreadState);
2081 Assert.IsTrue (e.Thread.ThreadId > 0);
2083 Assert.AreEqual (e.Thread.TID, e.Thread.TID);
2085 vm.EnableEvents (EventType.ThreadStart, EventType.ThreadDeath);
2089 e = GetNextEvent ();
2090 Assert.IsInstanceOfType (typeof (ThreadStartEvent), e);
2091 var state = e.Thread.ThreadState;
2092 Assert.IsTrue (state == ThreadState.Running || state == ThreadState.Unstarted);
2096 e = GetNextEvent ();
2097 Assert.IsInstanceOfType (typeof (ThreadDeathEvent), e);
2098 Assert.AreEqual (ThreadState.Stopped, e.Thread.ThreadState);
2102 public void Frame_SetValue () {
2103 Event e = run_until ("locals2");
2105 StackFrame frame = e.Thread.GetFrames () [0];
2108 var l = frame.Method.GetLocal ("i");
2109 frame.SetValue (l, vm.CreateValue ((long)55));
2110 AssertValue (55, frame.GetValue (l));
2113 l = frame.Method.GetLocal ("s");
2114 frame.SetValue (l, vm.RootDomain.CreateString ("DEF"));
2115 AssertValue ("DEF", frame.GetValue (l));
2117 // argument as local
2118 l = frame.Method.GetLocal ("arg");
2119 frame.SetValue (l, vm.CreateValue (6));
2120 AssertValue (6, frame.GetValue (l));
2123 var p = frame.Method.GetParameters ()[1];
2124 frame.SetValue (p, vm.CreateValue (7));
2125 AssertValue (7, frame.GetValue (p));
2128 p = frame.Method.GetParameters ()[2];
2129 frame.SetValue (p, vm.RootDomain.CreateString ("DEF"));
2130 AssertValue ("DEF", frame.GetValue (p));
2133 p = frame.Method.GetParameters ()[3];
2134 frame.SetValue (p, vm.RootDomain.CreateString ("DEF2"));
2135 AssertValue ("DEF2", frame.GetValue (p));
2137 // argument checking
2140 AssertThrows<ArgumentNullException> (delegate () {
2141 frame.SetValue ((LocalVariable)null, vm.CreateValue (55));
2145 AssertThrows<ArgumentNullException> (delegate () {
2146 l = frame.Method.GetLocal ("i");
2147 frame.SetValue (l, null);
2150 // value of invalid type
2151 AssertThrows<ArgumentException> (delegate () {
2152 l = frame.Method.GetLocal ("i");
2153 frame.SetValue (l, vm.CreateValue (55));
2159 public void Frame_SetValue_Registers () {
2160 Event e = run_until ("locals6_1");
2162 StackFrame frame = e.Thread.GetFrames () [1];
2165 var l = frame.Method.GetLocal ("j");
2166 frame.SetValue (l, vm.CreateValue (99));
2167 AssertValue (99, frame.GetValue (l));
2169 // Check it during execution
2170 e = run_until ("locals6_2");
2171 frame = e.Thread.GetFrames () [0];
2172 AssertValue (99, frame.GetValue (frame.Method.GetParameters ()[0]));
2174 // Set it while in a frame which clobbers its register
2175 e = run_until ("locals6_3");
2176 frame = e.Thread.GetFrames () [1];
2177 frame.SetValue (l, vm.CreateValue (100));
2178 AssertValue (100, frame.GetValue (l));
2180 // Check it during execution
2181 e = run_until ("locals6_4");
2182 frame = e.Thread.GetFrames () [0];
2183 AssertValue (100, frame.GetValue (frame.Method.GetParameters ()[0]));
2185 // Signed byte value
2186 e = run_until ("locals6_5");
2187 frame = e.Thread.GetFrames () [1];
2188 var l2 = frame.Method.GetLocal ("sb");
2189 frame.SetValue (l2, vm.CreateValue ((sbyte)-99));
2190 AssertValue (-99, frame.GetValue (l2));
2192 // Check it during execution
2193 e = run_until ("locals6_6");
2194 frame = e.Thread.GetFrames () [0];
2195 AssertValue (-99, frame.GetValue (frame.Method.GetParameters ()[0]));
2199 public void InvokeRegress () {
2200 Event e = run_until ("invoke1");
2202 StackFrame frame = e.Thread.GetFrames () [0];
2204 TypeMirror t = frame.Method.DeclaringType;
2205 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2207 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2213 m = t.GetMethod ("invoke_return_void");
2214 v = this_obj.InvokeMethod (e.Thread, m, null);
2217 // Check that the stack frames remain valid during the invoke
2218 Assert.AreEqual ("Tests", (frame.GetThis () as ObjectMirror).Type.Name);
2220 // do another invoke
2221 m = t.GetMethod ("invoke_return_void");
2222 v = this_obj.InvokeMethod (e.Thread, m, null);
2225 // Try a single step after the invoke
2226 var req = vm.CreateStepRequest (e.Thread);
2227 req.Depth = StepDepth.Into;
2228 req.Size = StepSize.Line;
2233 // Step into invoke2
2235 e = GetNextEvent ();
2236 Assert.IsTrue (e is StepEvent);
2237 Assert.AreEqual ("invoke2", (e as StepEvent).Method.Name);
2241 frame = e.Thread.GetFrames () [0];
2245 public void Exceptions () {
2246 Event e = run_until ("exceptions");
2247 var req = vm.CreateExceptionRequest (null);
2252 e = GetNextEvent ();
2253 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2254 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2256 var frames = e.Thread.GetFrames ();
2257 Assert.AreEqual ("exceptions", frames [0].Method.Name);
2260 // exception type filter
2262 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.ArgumentException"));
2265 // Skip the throwing of the second OverflowException
2268 e = GetNextEvent ();
2269 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2270 Assert.AreEqual ("ArgumentException", (e as ExceptionEvent).Exception.Type.Name);
2273 // exception type filter for subclasses
2274 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.Exception"));
2279 e = GetNextEvent ();
2280 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2281 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2284 // Implicit exceptions
2285 req = vm.CreateExceptionRequest (null);
2290 e = GetNextEvent ();
2291 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2292 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
2295 // Single stepping after an exception
2296 req = vm.CreateExceptionRequest (null);
2301 e = GetNextEvent ();
2302 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2303 Assert.AreEqual ("Exception", (e as ExceptionEvent).Exception.Type.Name);
2304 frames = e.Thread.GetFrames ();
2305 Assert.AreEqual ("exceptions2", frames [0].Method.Name);
2308 var sreq = vm.CreateStepRequest (e.Thread);
2309 sreq.Depth = StepDepth.Over;
2310 sreq.Size = StepSize.Line;
2314 e = GetNextEvent ();
2315 Assert.IsInstanceOfType (typeof (StepEvent), e);
2316 frames = e.Thread.GetFrames ();
2317 Assert.AreEqual ("exceptions", frames [0].Method.Name);
2320 // Argument checking
2321 AssertThrows<ArgumentException> (delegate {
2322 vm.CreateExceptionRequest (e.Thread.Type);
2327 public void ExceptionFilter () {
2328 Event e = run_until ("exception_filter");
2330 MethodMirror m = entry_point.DeclaringType.GetMethod ("exception_filter_filter");
2331 Assert.IsNotNull (m);
2333 vm.SetBreakpoint (m, 0);
2337 e = GetNextEvent ();
2338 Assert.AreEqual (EventType.Breakpoint, e.EventType);
2339 Assert.IsTrue (e is BreakpointEvent);
2340 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
2342 var frames = e.Thread.GetFrames ();
2344 Assert.IsTrue (frames [0].Location.SourceFile.IndexOf ("dtest-app.cs") != -1);
2345 Assert.AreEqual ("exception_filter_filter", frames [0].Location.Method.Name);
2347 Assert.AreEqual (0, frames [1].Location.Method.MetadataToken);
2348 Assert.AreEqual (0x0f, frames [1].Location.ILOffset);
2350 Assert.AreEqual ("exception_filter_method", frames [2].Location.Method.Name);
2351 Assert.AreEqual (0x05, frames [2].Location.ILOffset);
2353 Assert.AreEqual (0, frames [3].Location.Method.MetadataToken, 0);
2354 Assert.AreEqual (0, frames [3].Location.ILOffset);
2356 Assert.AreEqual ("exception_filter", frames [4].Location.Method.Name);
2360 public void ExceptionFilter2 () {
2363 Start (new string [] { "dtest-excfilter.exe" });
2365 MethodMirror filter_method = entry_point.DeclaringType.GetMethod ("Filter");
2366 Assert.IsNotNull (filter_method);
2368 MethodMirror test_method = entry_point.DeclaringType.GetMethod ("Test");
2369 Assert.IsNotNull (test_method);
2371 vm.SetBreakpoint (filter_method, 0);
2375 var e = GetNextEvent ();
2376 Assert.AreEqual (EventType.Breakpoint, e.EventType);
2377 Assert.IsTrue (e is BreakpointEvent);
2378 Assert.AreEqual (filter_method.Name, (e as BreakpointEvent).Method.Name);
2380 var frames = e.Thread.GetFrames ();
2382 Assert.AreEqual (4, frames.Count ());
2384 Assert.AreEqual (filter_method.Name, frames [0].Location.Method.Name);
2385 Assert.AreEqual (20, frames [0].Location.LineNumber);
2386 Assert.AreEqual (0, frames [0].Location.ILOffset);
2388 Assert.AreEqual (test_method.Name, frames [1].Location.Method.Name);
2389 Assert.AreEqual (37, frames [1].Location.LineNumber);
2390 Assert.AreEqual (0x0b, frames [1].Location.ILOffset);
2392 Assert.AreEqual (test_method.Name, frames [2].Location.Method.Name);
2393 Assert.AreEqual (33, frames [2].Location.LineNumber);
2394 Assert.AreEqual (0x05, frames [2].Location.ILOffset);
2396 Assert.AreEqual (entry_point.Name, frames [3].Location.Method.Name);
2397 Assert.AreEqual (14, frames [3].Location.LineNumber);
2398 Assert.AreEqual (0x00, frames [3].Location.ILOffset);
2406 public void EventSets () {
2408 // Create two filter which both match the same exception
2410 Event e = run_until ("exceptions");
2412 var req = vm.CreateExceptionRequest (null);
2415 var req2 = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.OverflowException"));
2420 var es = vm.GetNextEventSet ();
2421 Assert.AreEqual (2, es.Events.Length);
2424 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2425 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2428 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2429 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2436 // Test single threaded invokes during processing of nullref exceptions.
2437 // These won't work if the exception handling is done from the sigsegv signal
2438 // handler, since the sigsegv signal is disabled until control returns from the
2442 [Category ("only3")]
2443 public void NullRefExceptionAndSingleThreadedInvoke () {
2444 Event e = run_until ("exceptions");
2445 var req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.NullReferenceException"));
2450 e = GetNextEvent ();
2451 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2452 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
2454 var ex = (e as ExceptionEvent).Exception;
2455 var tostring_method = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2456 ex.InvokeMethod (e.Thread, tostring_method, null, InvokeOptions.SingleThreaded);
2460 public void Domains () {
2463 Start (new string [] { "dtest-app.exe", "domain-test" });
2465 vm.EnableEvents (EventType.AppDomainCreate, EventType.AppDomainUnload, EventType.AssemblyUnload);
2467 Event e = run_until ("domains");
2471 e = GetNextEvent ();
2472 Assert.IsInstanceOfType (typeof (AppDomainCreateEvent), e);
2474 var domain = (e as AppDomainCreateEvent).Domain;
2476 // Run until the callback in the domain
2477 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_in_domain");
2478 Assert.IsNotNull (m);
2479 vm.SetBreakpoint (m, 0);
2483 e = GetNextEvent ();
2484 if (e is BreakpointEvent)
2488 Assert.AreEqual ("invoke_in_domain", (e as BreakpointEvent).Method.Name);
2490 // d_method is from another domain
2491 MethodMirror d_method = (e as BreakpointEvent).Method;
2492 Assert.IsTrue (m != d_method);
2494 var frames = e.Thread.GetFrames ();
2495 Assert.AreEqual ("invoke_in_domain", frames [0].Method.Name);
2496 Assert.AreEqual ("invoke", frames [1].Method.Name);
2497 Assert.AreEqual ("domains", frames [2].Method.Name);
2499 // Test breakpoints on already JITted methods in other domains
2500 m = entry_point.DeclaringType.GetMethod ("invoke_in_domain_2");
2501 Assert.IsNotNull (m);
2502 vm.SetBreakpoint (m, 0);
2506 e = GetNextEvent ();
2507 if (e is BreakpointEvent)
2511 Assert.AreEqual ("invoke_in_domain_2", (e as BreakpointEvent).Method.Name);
2513 // This is empty when receiving the AppDomainCreateEvent
2514 Assert.AreEqual ("domain", domain.FriendlyName);
2516 // Run until the unload
2519 e = GetNextEvent ();
2520 if (e is AssemblyUnloadEvent) {
2526 Assert.IsInstanceOfType (typeof (AppDomainUnloadEvent), e);
2527 Assert.AreEqual (domain, (e as AppDomainUnloadEvent).Domain);
2529 // Run past the unload
2530 e = run_until ("domains_2");
2532 // Test access to unloaded types
2533 // FIXME: Add an exception type for this
2534 AssertThrows<Exception> (delegate {
2535 d_method.DeclaringType.GetValue (d_method.DeclaringType.GetField ("static_i"));
2540 public void DynamicMethods () {
2541 Event e = run_until ("dyn_call");
2543 var m = e.Thread.GetFrames ()[1].Method;
2544 Assert.AreEqual ("dyn_method", m.Name);
2546 // Test access to IL
2547 var body = m.GetMethodBody ();
2549 ILInstruction ins = body.Instructions [0];
2550 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
2551 Assert.AreEqual ("FOO", ins.Operand);
2555 public void RefEmit () {
2558 Start (new string [] { "dtest-app.exe", "ref-emit-test" });
2560 Event e = run_until ("ref_emit_call");
2562 var m = e.Thread.GetFrames ()[1].Method;
2563 Assert.AreEqual ("ref_emit_method", m.Name);
2565 // Test access to IL
2566 var body = m.GetMethodBody ();
2570 ins = body.Instructions [0];
2571 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
2572 Assert.AreEqual ("FOO", ins.Operand);
2574 ins = body.Instructions [1];
2575 Assert.AreEqual (OpCodes.Call, ins.OpCode);
2576 Assert.IsInstanceOfType (typeof (MethodMirror), ins.Operand);
2577 Assert.AreEqual ("ref_emit_call", (ins.Operand as MethodMirror).Name);
2581 public void IsAttached () {
2582 var f = entry_point.DeclaringType.GetField ("is_attached");
2584 Event e = run_until ("Main");
2586 AssertValue (true, entry_point.DeclaringType.GetValue (f));
2590 public void StackTraceInNative () {
2591 // Check that stack traces can be produced for threads in native code
2594 Start (new string [] { "dtest-app.exe", "frames-in-native" });
2596 var e = run_until ("frames_in_native");
2598 // FIXME: This is racy
2605 StackFrame[] frames = e.Thread.GetFrames ();
2607 int frame_index = -1;
2608 for (int i = 0; i < frames.Length; ++i) {
2609 if (frames [i].Method.Name == "Sleep") {
2615 Assert.IsTrue (frame_index != -1);
2616 Assert.AreEqual ("Sleep", frames [frame_index].Method.Name);
2617 Assert.AreEqual ("frames_in_native", frames [frame_index + 1].Method.Name);
2618 Assert.AreEqual ("Main", frames [frame_index + 2].Method.Name);
2620 // Check that invokes are disabled for such threads
2621 TypeMirror t = frames [frame_index + 1].Method.DeclaringType;
2624 var m = t.GetMethod ("invoke_static_return_void");
2625 AssertThrows<InvalidOperationException> (delegate {
2626 t.InvokeMethod (e.Thread, m, null);
2631 public void VirtualMachine_CreateEnumMirror () {
2632 var e = run_until ("o1");
2633 var frame = e.Thread.GetFrames () [0];
2635 object val = frame.GetThis ();
2636 Assert.IsTrue (val is ObjectMirror);
2637 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
2638 ObjectMirror o = (val as ObjectMirror);
2640 FieldInfoMirror field = o.Type.GetField ("field_enum");
2641 Value f = o.GetValue (field);
2642 TypeMirror enumType = (f as EnumMirror).Type;
2644 o.SetValue (field, vm.CreateEnumMirror (enumType, vm.CreateValue (1)));
2645 f = o.GetValue (field);
2646 Assert.AreEqual (1, (f as EnumMirror).Value);
2648 // Argument checking
2649 AssertThrows<ArgumentNullException> (delegate () {
2650 vm.CreateEnumMirror (enumType, null);
2653 AssertThrows<ArgumentNullException> (delegate () {
2654 vm.CreateEnumMirror (null, vm.CreateValue (1));
2658 AssertThrows<ArgumentException> (delegate () {
2659 vm.CreateEnumMirror (enumType, vm.CreateValue (null));
2662 // value of a wrong type
2663 AssertThrows<ArgumentException> (delegate () {
2664 vm.CreateEnumMirror (enumType, vm.CreateValue ((long)1));
2669 public void VirtualMachine_EnableEvents_Breakpoint () {
2670 AssertThrows<ArgumentException> (delegate () {
2671 vm.EnableEvents (EventType.Breakpoint);
2676 public void SingleStepRegress654694 () {
2679 MethodMirror m = entry_point.DeclaringType.GetMethod ("ss_regress_654694");
2680 foreach (Location l in m.Locations) {
2681 if (l.ILOffset > 0 && il_offset == -1)
2682 il_offset = l.ILOffset;
2685 Event e = run_until ("ss_regress_654694");
2687 Assert.IsNotNull (m);
2688 vm.SetBreakpoint (m, il_offset);
2692 e = GetNextEvent ();
2693 Assert.IsTrue (e is BreakpointEvent);
2695 var req = vm.CreateStepRequest (e.Thread);
2696 req.Depth = StepDepth.Over;
2697 req.Size = StepSize.Line;
2702 e = GetNextEvent ();
2703 Assert.IsTrue (e is StepEvent);
2709 public void DebugBreak () {
2710 vm.EnableEvents (EventType.UserBreak);
2715 var e = GetNextEvent ();
2716 Assert.IsTrue (e is UserBreakEvent);
2720 public void DebugLog () {
2721 vm.EnableEvents (EventType.UserLog);
2726 var e = GetNextEvent ();
2727 Assert.IsTrue (e is UserLogEvent);
2728 var le = e as UserLogEvent;
2730 Assert.AreEqual (5, le.Level);
2731 Assert.AreEqual ("A", le.Category);
2732 Assert.AreEqual ("B", le.Message);
2736 public void TypeGetMethodsByNameFlags () {
2738 var assembly = entry_point.DeclaringType.Assembly;
2739 var type = assembly.GetType ("Tests3");
2741 Assert.IsNotNull (type);
2743 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Public, false);
2744 Assert.AreEqual (1, mm.Length, "#1");
2745 Assert.AreEqual ("M1", mm[0].Name, "#2");
2747 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.NonPublic, false);
2748 Assert.AreEqual (1, mm.Length, "#3");
2749 Assert.AreEqual ("M2", mm[0].Name, "#4");
2751 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public, false);
2752 Assert.AreEqual (7, mm.Length, "#5"); //M3 plus Equals, GetHashCode, GetType, ToString, .ctor
2754 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, false);
2755 Assert.AreEqual (2, mm.Length, "#7");
2757 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
2758 Assert.AreEqual (1, mm.Length, "#9");
2760 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
2761 Assert.AreEqual (5, mm.Length, "#11");
2764 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Public, false);
2765 Assert.AreEqual (1, mm.Length, "#12");
2766 Assert.AreEqual ("M1", mm[0].Name, "#13");
2768 mm = type.GetMethodsByNameFlags ("m1", BindingFlags.Static | BindingFlags.Public, true);
2769 Assert.AreEqual (1, mm.Length, "#14");
2770 Assert.AreEqual ("M1", mm[0].Name, "#15");
2772 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, false);
2773 Assert.AreEqual (1, mm.Length, "#16");
2774 Assert.AreEqual ("M1", mm[0].Name, "#17");
2778 [Category ("only88")]
2779 public void TypeLoadSourceFileFilter () {
2780 Event e = run_until ("type_load");
2782 if (!vm.Version.AtLeast (2, 7))
2785 string srcfile = (e as BreakpointEvent).Method.DeclaringType.GetSourceFiles (true)[0];
2787 var req = vm.CreateTypeLoadRequest ();
2788 req.SourceFileFilter = new string [] { srcfile.ToUpper () };
2792 e = GetNextEvent ();
2793 Assert.IsTrue (e is TypeLoadEvent);
2794 Assert.AreEqual ("TypeLoadClass", (e as TypeLoadEvent).Type.FullName);
2798 public void TypeLoadTypeNameFilter () {
2799 Event e = run_until ("type_load");
2801 var req = vm.CreateTypeLoadRequest ();
2802 req.TypeNameFilter = new string [] { "TypeLoadClass2" };
2806 e = GetNextEvent ();
2807 Assert.IsTrue (e is TypeLoadEvent);
2808 Assert.AreEqual ("TypeLoadClass2", (e as TypeLoadEvent).Type.FullName);
2812 public void GetTypesForSourceFile () {
2815 var types = vm.GetTypesForSourceFile ("dtest-app.cs", false);
2816 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
2817 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
2819 types = vm.GetTypesForSourceFile ("DTEST-app.cs", true);
2820 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
2821 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
2825 public void GetTypesNamed () {
2828 var types = vm.GetTypes ("Tests", false);
2829 Assert.AreEqual (1, types.Count);
2830 Assert.AreEqual ("Tests", types [0].FullName);
2832 types = vm.GetTypes ("System.Exception", false);
2833 Assert.AreEqual (1, types.Count);
2834 Assert.AreEqual ("System.Exception", types [0].FullName);
2838 public void String_GetChars () {
2842 var e = run_until ("arg2");
2844 var frame = e.Thread.GetFrames () [0];
2846 val = frame.GetArgument (0);
2847 Assert.IsTrue (val is StringMirror);
2848 AssertValue ("FOO", val);
2849 var s = (val as StringMirror);
2850 Assert.AreEqual (3, s.Length);
2852 var c = s.GetChars (0, 2);
2853 Assert.AreEqual (2, c.Length);
2854 Assert.AreEqual ('F', c [0]);
2855 Assert.AreEqual ('O', c [1]);
2857 AssertThrows<ArgumentException> (delegate () {
2863 public void GetInterfaces () {
2864 var e = run_until ("arg2");
2866 var frame = e.Thread.GetFrames () [0];
2868 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
2869 var ifaces = cl1.GetInterfaces ();
2870 Assert.AreEqual (1, ifaces.Length);
2871 Assert.AreEqual ("ITest", ifaces [0].Name);
2873 var cl2 = cl1.GetMethod ("Baz").ReturnType;
2874 var ifaces2 = cl2.GetInterfaces ();
2875 Assert.AreEqual (1, ifaces2.Length);
2876 Assert.AreEqual ("ITest`1", ifaces2 [0].Name);
2880 public void GetInterfaceMap () {
2881 var e = run_until ("arg2");
2883 var frame = e.Thread.GetFrames () [0];
2885 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
2886 var iface = cl1.Assembly.GetType ("ITest");
2887 var map = cl1.GetInterfaceMap (iface);
2888 Assert.AreEqual (cl1, map.TargetType);
2889 Assert.AreEqual (iface, map.InterfaceType);
2890 Assert.AreEqual (2, map.InterfaceMethods.Length);
2891 Assert.AreEqual (2, map.TargetMethods.Length);
2895 public void StackAlloc_Breakpoints_Regress2775 () {
2896 // Check that breakpoints on arm don't overwrite stackalloc-ed memory
2897 var e = run_until ("regress_2755");
2899 var frame = e.Thread.GetFrames () [0];
2901 // This breaks at the call site
2902 vm.SetBreakpoint (m, m.Locations [2].ILOffset);
2905 var e2 = GetNextEvent ();
2906 Assert.IsTrue (e2 is BreakpointEvent);
2908 e = run_until ("regress_2755_3");
2909 frame = e.Thread.GetFrames () [1];
2910 var res = frame.GetValue (m.GetLocal ("sum"));
2911 AssertValue (0, res);
2915 public void MethodInfo () {
2916 Event e = run_until ("locals2");
2918 StackFrame frame = e.Thread.GetFrames () [0];
2919 var m = frame.Method;
2921 Assert.IsTrue (m.IsGenericMethod);
2922 Assert.IsFalse (m.IsGenericMethodDefinition);
2924 var args = m.GetGenericArguments ();
2925 Assert.AreEqual (1, args.Length);
2926 Assert.AreEqual ("String", args [0].Name);
2928 var gmd = m.GetGenericMethodDefinition ();
2929 Assert.IsTrue (gmd.IsGenericMethod);
2930 Assert.IsTrue (gmd.IsGenericMethodDefinition);
2931 Assert.AreEqual (gmd, gmd.GetGenericMethodDefinition ());
2933 args = gmd.GetGenericArguments ();
2934 Assert.AreEqual (1, args.Length);
2935 Assert.AreEqual ("T", args [0].Name);