Merge pull request #4152 from BrzVlad/misc-gc-altstack
[mono.git] / mcs / tools / mono-symbolicate / Test / StackTraceDumper.cs
1 using System;
2 using System.Collections.Generic;
3
4 class StackTraceDumper {
5
6         public static void Main ()
7         {
8                 try {
9                         throw new Exception ("Stacktrace with 1 frame");
10                 } catch (Exception e) {
11                         Console.WriteLine (e);
12                         Console.WriteLine ("Stacktrace:");
13                         Console.WriteLine (new System.Diagnostics.StackTrace(e));
14                 }
15
16                 Catch (() => {throw new Exception ("Stacktrace with 2 frames");});
17
18                 Catch (() => ThrowException ("Stacktrace with 3 frames", 1));
19
20                 Catch (() => ThrowException ("Stacktrace with 4 frames", 2));
21
22                 Catch (() => {
23                         var message = "Stack frame with method overload using ref parameter";
24                         ThrowException (ref message);
25                 });
26
27                 Catch (() => {
28                         int i;
29                         ThrowException ("Stack frame with method overload using out parameter", out i);
30                 });
31
32                 Catch (() => ThrowExceptionGeneric<double> ("Stack frame with 1 generic parameter"));
33
34                 Catch (() => ThrowExceptionGeneric<double,string> ("Stack frame with 2 generic parameters"));
35
36                 Catch (() => ThrowExceptionGeneric (12));
37
38                 Catch (() => InnerClass.ThrowException ("Stack trace with inner class"));
39
40                 Catch (() => InnerGenericClass<string>.ThrowException ("Stack trace with inner generic class"));
41
42                 Catch (() => InnerGenericClass<string>.ThrowException ("Stack trace with inner generic class and method generic parameter", "string"));
43
44                 Catch (() => InnerGenericClass<string>.ThrowException<string> ("Stack trace with inner generic class and generic overload", "string"));
45
46                 Catch (() => InnerGenericClass<string>.InnerInnerGenericClass<int>.ThrowException ("Stack trace with 2 inner generic class and generic overload"));
47
48                 Catch (() => InnerGenericClass<int>.InnerInnerGenericClass<string>.ThrowException ("Stack trace with 2 inner generic class and generic overload"));
49
50                 Catch (() => InnerGenericClass<int>.ThrowException ("Stack trace with nested type argument", "string", null));
51
52                 Catch (() => {
53                         var d = new Dictionary<string, string> ();
54                         d.ContainsKey (null); // ArgumentNullException
55                 });
56
57                 /*
58                 The following test include ambiguous methods we can't resolve. Testing this is hard, so I'm leaving a test behind but disabling it for the time being
59                 In this case the ambiguous methods are:
60                         public static void Foo<K> (int a, bool hard_crash, GenClass<T> arg, List<int> zz)
61                         public static void Foo<K> (int a, bool hard_crash, GenClass<T> arg, List<double> zz)
62
63                 The are ambiguous because the only difference is the instantiation on the last parameter which we can't
64                 figure out from a stacktrace.
65                 */
66                 //Catch (() => ComplicatedTestCase.Run ());
67         }
68
69         public static void Catch (Action action)
70         {
71                 try {
72                         action ();
73                 } catch (Exception e) {
74                         Console.WriteLine();
75                         Console.WriteLine (e);
76                         Console.WriteLine ("Stacktrace:");
77                         Console.WriteLine (new System.Diagnostics.StackTrace (e));
78                 }
79         }
80
81         public static void ThrowException (string message)
82         {
83                 throw new Exception (message);
84         }
85
86         public static void ThrowException (ref string message)
87         {
88                 throw new Exception (message);
89         }
90
91         public static void ThrowException (string message, int i)
92         {
93                 if (i > 1)
94                         ThrowException (message, --i);
95
96                 throw new Exception (message);
97         }
98
99         public static void ThrowException (string message, out int o)
100         {
101                 throw new Exception (message);
102         }
103
104         public static void ThrowExceptionGeneric<T> (string message)
105         {
106                 throw new Exception (message);
107         }
108
109         public static void ThrowExceptionGeneric<T> (T a1)
110         {
111                 throw new Exception ("Stack frame with generic method overload");
112         }
113
114         public static void ThrowExceptionGeneric<T> (List<string> a1)
115         {
116                 throw new Exception ("Stack frame with generic method overload");
117         }
118
119         public static void ThrowExceptionGeneric<T> (List<T> a1)
120         {
121                 throw new Exception ("Stack frame with generic method overload");
122         }
123
124         public static void ThrowExceptionGeneric<T1,T2> (string message)
125         {
126                 throw new Exception (message);
127         }
128
129         class InnerClass {
130                 public static void ThrowException (string message)
131                 {
132                         throw new Exception (message);
133                 }
134         }
135
136         class InnerGenericClass<T> {
137                 public static void ThrowException (string message)
138                 {
139                         throw new Exception (message);
140                 }
141
142                 public static void ThrowException (string message, T arg)
143                 {
144                         Console.WriteLine ("Generic to string:" + arg.ToString());
145                         throw new Exception (message);
146                 }
147
148                 public static void ThrowException<T1> (string message, T1 arg)
149                 {
150                         throw new Exception (message);
151                 }
152
153                 public static void ThrowException<T1> (string message, T1 arg, InnerGenericClass<T1> _ignore)
154                 {
155                         throw new Exception (message as string);
156                 }
157
158                 public class InnerInnerGenericClass<T2> {
159                         public static void ThrowException (T message)
160                         {
161                                 throw new Exception (message as string);
162                         }
163
164                         public static void ThrowException (T2 message)
165                         {
166                                 throw new Exception (message as string);
167                         }
168                 }
169         }
170
171         class GenClass<T> {
172                 public static void Foo (int a, bool hard_crash) {
173                         GenPair<T>.Foo<object> (a, hard_crash, new GenClass<T> (), new List<int> ());
174                 }
175         }
176
177         class GenPair<T> {
178                 public static void Foo<K> (int a, bool hard_crash, GenClass<T> arg, List<int> zz) {
179                         Foo<K,K,K> (a, hard_crash, null, null);
180                 }
181
182                 public static void Foo<K,J,F> (int a, bool hard_crash, GenClass<J> arg, List<int> zz) {
183                         Foo<double> (a, hard_crash, null, new List<double> ());
184                 }
185
186                 public static void Foo<K> (int a, bool hard_crash, GenClass<T> arg, List<double> zz) {
187                         ComplicatedTestCase.ArrayAndRef (a, new int[2], new int[2,2], ref hard_crash);
188                 }
189         }
190
191         class ComplicatedTestCase {
192                 public static int ArrayAndRef (int a, int[] b, int[,] c, ref bool hard_crash) {
193                         Object o = null;
194                         for (int x = 0; x < a; ++x)
195                                 throw new Exception ("Stack trace with ambiguity");
196                         return 99;
197                 }
198
199                 public static void Foo (int a, bool hard_crash) {
200                         GenClass<string>.Foo (a, hard_crash);
201                 }
202
203                 public static void Run () {
204                         Foo (10, false);
205                 }
206         }
207 }