758f2d3291f9786fdadd5dfe7c362fef67e8e0b8
[mono.git] / mcs / class / Mono.Debugger.Soft / Test / dtest-app.cs
1 /*
2  * dtest-app.cs:
3  *
4  *   Application program used by the debugger tests.
5  */
6 using System;
7 using System.Runtime.CompilerServices;
8 using System.Reflection;
9 using System.Reflection.Emit;
10 using System.Diagnostics;
11 using System.Threading;
12 using System.Collections.Generic;
13 using System.Linq;
14
15 public class TestsBase
16 {
17 #pragma warning disable 0414
18 #pragma warning disable 0169
19         public int base_field_i;
20         public string base_field_s;
21         static int base_static_i = 57;
22         static string base_static_s = "C";
23 #pragma warning restore 0414
24 #pragma warning restore 0169
25 }
26
27 public enum AnEnum {
28         A = 0,
29         B= 1
30 }
31
32 [DebuggerDisplay ("Tests", Name="FOO", Target=typeof (int))]
33 [DebuggerTypeProxy (typeof (Tests))]
34 public class Tests2 {
35         [DebuggerBrowsableAttribute (DebuggerBrowsableState.Collapsed)]
36         public int field_j;
37         public static int static_field_j;
38
39         [DebuggerBrowsableAttribute (DebuggerBrowsableState.Collapsed)]
40         public int AProperty {
41                 get {
42                         return 0;
43                 }
44         }
45
46         public void invoke () {
47         }
48 }
49
50 public struct AStruct {
51         public int i;
52         public string s;
53         public byte k;
54         public IntPtr j;
55
56         [MethodImplAttribute (MethodImplOptions.NoInlining)]
57         public int foo (int val) {
58                 return val;
59         }
60
61         [MethodImplAttribute (MethodImplOptions.NoInlining)]
62         public static int static_foo (int val) {
63                 return val;
64         }
65
66         [MethodImplAttribute (MethodImplOptions.NoInlining)]
67         public int invoke_return_int () {
68                 return i;
69         }
70
71         [MethodImplAttribute (MethodImplOptions.NoInlining)]
72         public static int invoke_static () {
73                 return 5;
74         }
75
76         [MethodImplAttribute (MethodImplOptions.NoInlining)]
77         public IntPtr invoke_return_intptr () {
78                 return j;
79         }
80 }
81
82 public class GClass<T> {
83         public T field;
84         public static T static_field;
85
86         [MethodImplAttribute (MethodImplOptions.NoInlining)]
87         public GClass () {
88         }
89 }
90
91 public struct GStruct<T> {
92         public T i;
93
94         public int j;
95
96         [MethodImplAttribute (MethodImplOptions.NoInlining)]
97         public int invoke_return_int () {
98                 return j;
99         }
100 }
101
102 public class Tests : TestsBase
103 {
104 #pragma warning disable 0414
105         int field_i;
106         string field_s;
107         AnEnum field_enum;
108         bool field_bool1, field_bool2;
109         char field_char;
110         byte field_byte;
111         sbyte field_sbyte;
112         short field_short;
113         ushort field_ushort;
114         long field_long;
115         ulong field_ulong;
116         float field_float;
117         double field_double;
118         Thread field_class;
119         IntPtr field_intptr;
120         static int static_i = 55;
121         static string static_s = "A";
122         public const int literal_i = 56;
123         public const string literal_s = "B";
124         public object child;
125         public AStruct field_struct;
126         public object field_boxed_struct;
127         public GStruct<int> generic_field_struct;
128         [ThreadStatic]
129         public static int tls_i;
130         public static bool is_attached = Debugger.IsAttached;
131
132 #pragma warning restore 0414
133
134         public class NestedClass {
135         }
136
137         public int IntProperty {
138                 get {
139                         return field_i;
140                 }
141                 set {
142                         field_i = value;
143                 }
144         }
145
146         public int ReadOnlyProperty {
147                 get {
148                         return field_i;
149                 }
150         }
151
152         public int this [int index] {
153                 get {
154                         return field_i;
155                 }
156         }
157
158         public static int Main (String[] args) {
159                 if (args.Length > 0 && args [0] == "suspend-test")
160                         /* This contains an infinite loop, so execute it conditionally */
161                         suspend ();
162                 breakpoints ();
163                 single_stepping ();
164                 arguments ();
165                 objects ();
166                 objrefs ();
167                 vtypes ();
168                 locals ();
169                 line_numbers ();
170                 type_info ();
171                 assembly_load ();
172                 invoke ();
173                 exceptions ();
174                 threads ();
175                 dynamic_methods ();
176                 if (args.Length > 0 && args [0] == "domain-test")
177                         /* This takes a lot of time, so execute it conditionally */
178                         domains ();
179                 if (args.Length > 0 && args [0] == "ref-emit-test")
180                         ref_emit ();
181                 if (args.Length > 0 && args [0] == "frames-in-native")
182                         frames_in_native ();
183                 if (args.Length >0 && args [0] == "invoke-single-threaded")
184                         new Tests ().invoke_single_threaded ();
185                 return 3;
186         }
187
188         public static void breakpoints () {
189                 /* Call these early so it is JITted by the time a breakpoint is placed on it */
190                 bp3 ();
191                 bp7<int> ();
192                 bp7<string> ();
193
194                 bp1 ();
195                 bp2 ();
196                 bp3 ();
197                 bp4 ();
198                 bp4 ();
199                 bp4 ();
200                 bp5 ();
201                 bp6<string> ();
202                 bp7<int> ();
203                 bp7<string> ();
204         }
205
206         [MethodImplAttribute (MethodImplOptions.NoInlining)]
207         public static void bp1 () {
208         }
209
210         [MethodImplAttribute (MethodImplOptions.NoInlining)]
211         public static void bp2 () {
212         }
213
214         [MethodImplAttribute (MethodImplOptions.NoInlining)]
215         public static void bp3 () {
216         }
217
218         [MethodImplAttribute (MethodImplOptions.NoInlining)]
219         public static void bp4 () {
220         }
221
222         [MethodImplAttribute (MethodImplOptions.NoInlining)]
223         public static void bp5 () {
224         }
225
226         [MethodImplAttribute (MethodImplOptions.NoInlining)]
227         public static void bp6<T> () {
228         }
229
230         [MethodImplAttribute (MethodImplOptions.NoInlining)]
231         public static void bp7<T> () {
232         }
233
234         [MethodImplAttribute (MethodImplOptions.NoInlining)]
235         public static void single_stepping () {
236                 ss1 ();
237                 ss2 ();
238                 ss3 ();
239                 ss3_2 ();
240                 ss4 ();
241                 ss5 (new int [] { 1, 2, 3 }, new Func<int, bool> (is_even));
242                 try {
243                         ss6 (true);
244                 } catch {
245                 }
246         }
247
248         [MethodImplAttribute (MethodImplOptions.NoInlining)]
249         public static void ss1 () {
250         }
251
252         [MethodImplAttribute (MethodImplOptions.NoInlining)]
253         public static void ss2 () {
254         }
255
256         [MethodImplAttribute (MethodImplOptions.NoInlining)]
257         public static int ss3 () {
258                 int sum = 0;
259
260                 for (int i = 0; i < 10; ++i)
261                         sum += i;
262
263                 return sum;
264         }
265
266         [MethodImplAttribute (MethodImplOptions.NoInlining)]
267         public static void ss3_2 () {
268                 ss3_2_2 ();
269         }
270
271         [MethodImplAttribute (MethodImplOptions.NoInlining)]
272         public static void ss3_2_2 () {
273         }
274
275         [MethodImplAttribute (MethodImplOptions.NoInlining)]
276         public static int ss4 () {
277                 ss1 (); ss1 ();
278                 ss2 ();
279                 return 0;
280         }
281
282         [MethodImplAttribute (MethodImplOptions.NoInlining)]
283         public static void ss5 (int[] arr, Func<int, bool> selector) {
284                 // Call into linq which calls back into this assembly
285                 arr.Count (selector);
286         }
287
288         [MethodImplAttribute (MethodImplOptions.NoInlining)]
289         public static void ss6 (bool b) {
290                 if (b) {
291                         ss7 ();
292                         throw new Exception ();
293                 }
294         }
295
296         [MethodImplAttribute (MethodImplOptions.NoInlining)]
297         public static void ss7 () {
298         }
299
300         [MethodImplAttribute (MethodImplOptions.NoInlining)]
301         public static bool is_even (int i) {
302                 return i % 2 == 0;
303         }
304
305         /*
306                 lock (static_s) {
307                         Console.WriteLine ("HIT!");
308                 }
309                 return 0;
310         }
311         */
312
313         [MethodImplAttribute (MethodImplOptions.NoInlining)]
314         public static void arguments () {
315                 arg1 (SByte.MaxValue - 5, Byte.MaxValue - 5, true, Int16.MaxValue - 5, UInt16.MaxValue - 5, 'F', Int32.MaxValue - 5, UInt32.MaxValue - 5, Int64.MaxValue - 5, UInt64.MaxValue - 5, 1.2345f, 6.78910, new IntPtr (Int32.MaxValue - 5), new UIntPtr (UInt32.MaxValue - 5));
316                 int i = 42;
317                 arg2 ("FOO", null, "BLA", ref i, new GClass <int> { field = 42 }, new object ());
318                 Tests t = new Tests () { field_i = 42, field_s = "S" };
319                 t.arg3 ("BLA");
320         }
321
322         [MethodImplAttribute (MethodImplOptions.NoInlining)]
323         public static int arg1 (sbyte sb, byte b, bool bl, short s, ushort us, char c, int i, uint ui, long l, ulong ul, float f, double d, IntPtr ip, UIntPtr uip) {
324                 return (int)(sb + b + (bl ? 0 : 1) + s + us + (int)c + i + ui + l + (long)ul + f + d + (int)ip + (int)uip);
325         }
326
327         [MethodImplAttribute (MethodImplOptions.NoInlining)]
328         public static string arg2 (string s, string s3, object o, ref int i, GClass <int> gc, object o2) {
329                 return s + (s3 != null ? "" : "") + o + i + gc.field + o2;
330         }
331
332         [MethodImplAttribute (MethodImplOptions.NoInlining)]
333         public object arg3 (string s) {
334                 return s + s + s + s + this;
335         }
336
337         [MethodImplAttribute (MethodImplOptions.NoInlining)]
338         public static void objects () {
339                 Tests t = new Tests () { field_i = 42, field_bool1 = true, field_bool2 = false, field_char = 'A', field_byte = 129, field_sbyte = -33, field_short = Int16.MaxValue - 5, field_ushort = UInt16.MaxValue - 5, field_long = Int64.MaxValue - 5, field_ulong = UInt64.MaxValue - 5, field_float = 3.14f, field_double = 3.14f, field_s = "S", base_field_i = 43, base_field_s = "T", field_enum = AnEnum.B, field_class = null, field_intptr = new IntPtr (Int32.MaxValue - 5) };
340                 t.o1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
341                 o2 (new string [] { "BAR", "BAZ" }, new int[] { 42, 43 }, new int [,] { { 1, 2 }, { 3, 4 }}, (int[,])Array.CreateInstance (typeof (int), new int [] { 2, 2}, new int [] { 1, 3}), new int[] { 0 });
342         }
343
344         [MethodImplAttribute (MethodImplOptions.NoInlining)]
345         public object o1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
346                 if (t == null || gc1 == null || gc2 == null)
347                         return null;
348                 else
349                         return this;
350         }
351
352         [MethodImplAttribute (MethodImplOptions.NoInlining)]
353         public static string o2 (string[] s2, int[] s3, int[,] s4, int[,] s5, IList<int> s6) {
354                 return s2 [0] + s3 [0] + s4 [0, 0] + s6 [0];
355         }
356
357         [MethodImplAttribute (MethodImplOptions.NoInlining)]
358         public static void objrefs () {
359                 Tests t = new Tests () {};
360                 set_child (t);
361                 t.objrefs1 ();
362                 t.child = null;
363                 GC.Collect ();
364                 objrefs2 ();
365         }
366
367         [MethodImplAttribute (MethodImplOptions.NoInlining)]
368         public static void set_child (Tests t) {
369                 t.child = new Tests ();
370         }
371
372         [MethodImplAttribute (MethodImplOptions.NoInlining)]
373         public void objrefs1 () {
374         }
375
376         [MethodImplAttribute (MethodImplOptions.NoInlining)]
377         public static void objrefs2 () {
378         }
379
380         public static void vtypes () {
381                 Tests t = new Tests () { field_struct = new AStruct () { i = 42, s = "S", k = 43 }, generic_field_struct = new GStruct<int> () { i = 42 }, field_boxed_struct = new AStruct () { i = 42 }};
382                 AStruct s = new AStruct { i = 44, s = "T", k = 45 };
383                 AStruct[] arr = new AStruct[] { 
384                         new AStruct () { i = 1, s = "S1" },
385                         new AStruct () { i = 2, s = "S2" } };
386                 t.vtypes1 (s, arr);
387                 vtypes2 (s);
388                 vtypes3 (s);
389         }
390
391         [MethodImplAttribute (MethodImplOptions.NoInlining)]
392         public object vtypes1 (AStruct s, AStruct[] arr) {
393                 if (arr != null)
394                         return this;
395                 else
396                         return null;
397         }
398
399         [MethodImplAttribute (MethodImplOptions.NoInlining)]
400         public static void vtypes2 (AStruct s) {
401                 s.foo (5);
402         }
403
404         [MethodImplAttribute (MethodImplOptions.NoInlining)]
405         public static void vtypes3 (AStruct s) {
406                 AStruct.static_foo (5);
407         }
408
409         [MethodImplAttribute (MethodImplOptions.NoInlining)]
410         public static void locals () {
411                 locals1 (null);
412                 locals2 (null, 5);
413                 locals3 ();
414         }
415
416         [MethodImplAttribute (MethodImplOptions.NoInlining)]
417         public static void locals1 (string[] args) {
418                 long foo = 42;
419
420                 for (int j = 0; j < 10; ++j) {
421                         foo ++;
422                 }
423         }
424
425         [MethodImplAttribute (MethodImplOptions.NoInlining)]
426         public static void locals2 (string[] args, int arg) {
427                 long i = 42;
428                 string s = "AB";
429
430                 for (int j = 0; j < 10; ++j) {
431                         if (s != null)
432                                 i ++;
433                 }
434         }
435
436         [MethodImplAttribute (MethodImplOptions.NoInlining)]
437         public static void locals3 () {
438                 string s = "B";
439                 s.ToString ();
440
441                 {
442                         long i = 42;
443                         i ++;
444                         locals4 ();
445                 }
446                 {
447                         string i = "A";
448                         i.ToString ();
449                         locals5 ();
450                 }
451                 {
452                         long j = 42;
453                         j ++;
454                 }
455         }
456
457         [MethodImplAttribute (MethodImplOptions.NoInlining)]
458         public static void locals4 () {
459         }
460
461         [MethodImplAttribute (MethodImplOptions.NoInlining)]
462         public static void locals5 () {
463         }
464
465         [MethodImplAttribute (MethodImplOptions.NoInlining)]
466         public static void line_numbers () {
467                 LineNumbers.ln1 ();
468         }
469
470         [MethodImplAttribute (MethodImplOptions.NoInlining)]
471         public static void suspend () {
472                 long i = 5;
473
474                 while (true) {
475                         i ++;
476                 }
477         }
478
479         [MethodImplAttribute (MethodImplOptions.NoInlining)]
480         public static void type_info () {
481                 Tests t = new Tests () { field_i = 42, field_s = "S", base_field_i = 43, base_field_s = "T", field_enum = AnEnum.B };
482                 t.ti1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
483                 int val = 0;
484                 unsafe {
485                         AStruct s = new AStruct () { i = 42, s = "S", k = 43 };
486
487                         ti2 (new string [] { "BAR", "BAZ" }, new int[] { 42, 43 }, new int [,] { { 1, 2 }, { 3, 4 }}, ref val, (int*)IntPtr.Zero, 5, s, new Tests (), new Tests2 (), new GClass <int> (), AnEnum.B);
488                 }
489         }
490
491         [MethodImplAttribute (MethodImplOptions.NoInlining)]
492         public object ti1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
493                 if (t == null || gc1 == null || gc2 == null)
494                         return null;
495                 else
496                         return this;
497         }
498
499         [MethodImplAttribute (MethodImplOptions.NoInlining)]
500         public static unsafe string ti2 (string[] s2, int[] s3, int[,] s4, ref int ri, int* ptr, int i, AStruct s, Tests t, Tests2 t2, GClass<int> g, AnEnum ae) {
501                 return s2 [0] + s3 [0] + s4 [0, 0];
502         }
503
504         [MethodImplAttribute (MethodImplOptions.NoInlining)]
505         public static void assembly_load () {
506                 assembly_load_2 ();
507         }
508
509         [MethodImplAttribute (MethodImplOptions.NoInlining)]
510         public static void assembly_load_2 () {
511                 // This will load System.dll while holding the loader lock
512                 new Foo ();
513         }
514
515         [MethodImplAttribute (MethodImplOptions.NoInlining)]
516         public static void invoke () {
517                 new Tests ().invoke1 (new Tests2 (), new AStruct () { i = 42, j = (IntPtr)43 }, new GStruct<int> { j = 42 });
518                 new Tests ().invoke_ex ();
519         }
520
521         [MethodImplAttribute (MethodImplOptions.NoInlining)]
522         public void invoke1 (Tests2 t, AStruct s, GStruct<int> g) {
523                 invoke2 ();
524         }
525
526         [MethodImplAttribute (MethodImplOptions.NoInlining)]
527         public void invoke2 () {
528         }
529
530         [MethodImplAttribute (MethodImplOptions.NoInlining)]
531         public void invoke_ex () {
532                 invoke_ex_inner ();
533         }
534
535         [MethodImplAttribute (MethodImplOptions.NoInlining)]
536         public void invoke_ex_inner () {
537                 try {
538                         throw new Exception ();
539                 } catch {
540                 }
541         }
542
543         int counter;
544
545         [MethodImplAttribute (MethodImplOptions.NoInlining)]
546         public void invoke_single_threaded () {
547                 // Spawn a thread incrementing a counter
548                 bool finished = false;
549
550                 new Thread (delegate () {
551                                 while (!finished)
552                                         counter ++;
553                 }).Start ();
554
555                 Thread.Sleep (100);
556
557                 invoke_single_threaded_2 ();
558
559                 finished = true;
560         }
561
562         [MethodImplAttribute (MethodImplOptions.NoInlining)]
563         public void invoke_single_threaded_2 () {
564         }
565
566         public void invoke_return_void () {
567         }
568
569         public string invoke_return_ref () {
570                 return "ABC";
571         }
572
573         public object invoke_return_null () {
574                 return null;
575         }
576
577         public int invoke_return_primitive () {
578                 return 42;
579         }
580
581         public void invoke_type_load () {
582                 new Class3 ();
583         }
584
585         class Class3 {
586         }
587
588         public long invoke_pass_primitive (byte ub, sbyte sb, short ss, ushort us, int i, uint ui, long l, ulong ul, char c, bool b, float f, double d) {
589                 return ub + sb + ss + us + i + ui + l + (long)ul + (int)c + (b ? 1 : 0) + (int)f + (int)d;
590         }
591
592         public int invoke_pass_primitive2 (bool b) {
593                 return b ? 1 : 0;
594         }
595
596         public string invoke_pass_ref (string s) {
597                 return s;
598         }
599
600         public static string invoke_static_pass_ref (string s) {
601                 return s;
602         }
603
604         public static void invoke_static_return_void () {
605         }
606
607         public static void invoke_throws () {
608                 throw new Exception ();
609         }
610
611         [MethodImplAttribute (MethodImplOptions.NoInlining)]
612         public static void exceptions () {
613                 try {
614                         throw new OverflowException ();
615                 } catch (Exception) {
616                 }
617                 try {
618                         throw new OverflowException ();
619                 } catch (Exception) {
620                 }
621                 try {
622                         throw new ArgumentException ();
623                 } catch (Exception) {
624                 }
625                 try {
626                         throw new OverflowException ();
627                 } catch (Exception) {
628                 }
629
630                 object o = null;
631                 try {
632                         o.GetType ();
633                 } catch (Exception) {
634                 }
635         }
636
637         [MethodImplAttribute (MethodImplOptions.NoInlining)]
638         public static void threads () {
639                 Thread t = new Thread (delegate () {});
640
641                 t.Start ();
642                 t.Join ();
643         }
644
645         [MethodImplAttribute (MethodImplOptions.NoInlining)]
646         public static void domains () {
647                 AppDomain domain = AppDomain.CreateDomain ("domain");
648
649                 CrossDomain o = (CrossDomain)domain.CreateInstanceAndUnwrap (
650                                    typeof (CrossDomain).Assembly.FullName, "CrossDomain");
651
652                 o.invoke_2 ();
653
654                 o.invoke ();
655
656                 o.invoke_2 ();
657
658                 AppDomain.Unload (domain);
659
660                 domains_2 ();
661         }
662
663         [MethodImplAttribute (MethodImplOptions.NoInlining)]
664         public static void domains_2 () {
665         }
666
667         [MethodImplAttribute (MethodImplOptions.NoInlining)]
668         public static void invoke_in_domain () {
669         }
670
671         [MethodImplAttribute (MethodImplOptions.NoInlining)]
672         public static void invoke_in_domain_2 () {
673         }
674
675         [MethodImplAttribute (MethodImplOptions.NoInlining)]
676         public static void dynamic_methods () {
677                 var m = new DynamicMethod ("dyn_method", typeof (void), new Type []  { typeof (int) }, typeof (object).Module);
678                 var ig = m.GetILGenerator ();
679
680                 ig.Emit (OpCodes.Ldstr, "FOO");
681                 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("dyn_call"));
682                 ig.Emit (OpCodes.Ret);
683
684                 var del = (Action<int>)m.CreateDelegate (typeof (Action<int>));
685
686                 del (0);
687         }
688
689         public static void dyn_call (string s) {
690         }
691
692         [MethodImplAttribute (MethodImplOptions.NoInlining)]
693         public static void ref_emit () {
694                 AssemblyName assemblyName = new AssemblyName ();
695                 assemblyName.Name = "foo";
696
697                 AssemblyBuilder assembly =
698                         Thread.GetDomain ().DefineDynamicAssembly (
699                                                                                                            assemblyName, AssemblyBuilderAccess.RunAndSave);
700
701                 ModuleBuilder module = assembly.DefineDynamicModule ("foo.dll");
702
703                 TypeBuilder tb = module.DefineType ("foo", TypeAttributes.Public, typeof (object));
704                 MethodBuilder mb = tb.DefineMethod ("ref_emit_method", MethodAttributes.Public|MethodAttributes.Static, CallingConventions.Standard, typeof (void), new Type [] { });
705                 ILGenerator ig = mb.GetILGenerator ();
706                 ig.Emit (OpCodes.Ldstr, "FOO");
707                 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("ref_emit_call"));
708                 ig.Emit (OpCodes.Ret);
709
710                 Type t = tb.CreateType ();
711
712                 t.GetMethod ("ref_emit_method").Invoke (null, null);
713         }
714
715         [MethodImplAttribute (MethodImplOptions.NoInlining)]
716         public static void ref_emit_call (string s) {
717         }
718
719         [MethodImplAttribute (MethodImplOptions.NoInlining)]
720         public static void frames_in_native () {
721                 Thread.Sleep (500);
722         }
723 }
724
725 public class CrossDomain : MarshalByRefObject
726 {
727         public void invoke () {
728                 Tests.invoke_in_domain ();
729         }
730
731         public void invoke_2 () {
732                 Tests.invoke_in_domain_2 ();
733         }
734 }       
735
736 public class Foo
737 {
738         public ProcessStartInfo info;
739 }
740
741 // Class used for line number info testing, don't change its layout
742 public class LineNumbers
743 {
744         [MethodImplAttribute (MethodImplOptions.NoInlining)]
745         public static void ln1 () {
746                 ln2 ();
747                 ln3 ();
748         }
749
750         [MethodImplAttribute (MethodImplOptions.NoInlining)]
751         public static void ln2 () {
752         }
753
754         [MethodImplAttribute (MethodImplOptions.NoInlining)]
755         public static void ln3 () {
756         }
757 }