Merge pull request #1668 from alexanderkyte/bug1856
[mono.git] / mcs / class / corlib / System.Diagnostics / StackTrace.cs
1 //
2 // System.Diagnostics.StackTrace.cs
3 //
4 // Author:
5 //      Alexander Klyubin (klyubin@aqris.com)
6 //      Dietmar Maurer (dietmar@ximian.com)
7 //
8 // (C) 2001
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Collections.Generic;
32 using System.Globalization;
33 using System.Reflection;
34 using System.Runtime.CompilerServices;
35 using System.Runtime.InteropServices;
36 using System.Security;
37 using System.Security.Permissions;
38 using System.Text;
39 using System.Threading;
40
41 namespace System.Diagnostics {
42
43         [Serializable]
44         [ComVisible (true)]
45         [MonoTODO ("Serialized objects are not compatible with .NET")]
46         public class StackTrace {
47
48         // TraceFormat is Used to specify options for how the 
49         // string-representation of a StackTrace should be generated.
50         internal enum TraceFormat 
51         {
52             Normal,
53             TrailingNewLine,        // include a trailing new line character
54             NoResourceLookup    // to prevent infinite resource recusion
55         }
56
57                 public const int METHODS_TO_SKIP = 0;
58
59                 private StackFrame[] frames;
60                 private bool debug_info;
61
62                 public StackTrace ()
63                 {
64                         init_frames (METHODS_TO_SKIP, false);
65                 }
66
67                 public StackTrace (bool fNeedFileInfo)
68                 {
69                         init_frames (METHODS_TO_SKIP, fNeedFileInfo);
70                 }
71
72                 public StackTrace (int skipFrames)
73                 {
74                         init_frames (skipFrames, false);
75                 }
76
77                 public StackTrace (int skipFrames, bool fNeedFileInfo)
78                 {
79                         init_frames (skipFrames, fNeedFileInfo);
80                 }
81
82                 void init_frames (int skipFrames, bool fNeedFileInfo)
83                 {
84                         if (skipFrames < 0)
85                                 throw new ArgumentOutOfRangeException ("< 0", "skipFrames");
86
87                         StackFrame sf;
88                         var l = new List<StackFrame> ();
89
90                         skipFrames += 2;
91                         
92                         while ((sf = new StackFrame (skipFrames, fNeedFileInfo)) != null &&
93                                sf.GetMethod () != null) {
94                                 
95                                 l.Add (sf);
96                                 skipFrames++;
97                         };
98
99                         debug_info = fNeedFileInfo;
100                         frames = l.ToArray ();
101                 }
102                 
103                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
104                 extern static StackFrame [] get_trace (Exception e, int skipFrames, bool fNeedFileInfo);
105
106                 public StackTrace (Exception e)
107                         : this (e, METHODS_TO_SKIP, false)
108                 {
109                 }
110
111                 public StackTrace (Exception e, bool fNeedFileInfo)
112                         : this (e, METHODS_TO_SKIP, fNeedFileInfo)
113                 {
114                 }
115
116                 public StackTrace (Exception e, int skipFrames)
117                         : this (e, skipFrames, false)
118                 {
119                 }
120
121                 public StackTrace (Exception e, int skipFrames, bool fNeedFileInfo)
122                         : this (e, skipFrames, fNeedFileInfo, false)
123                 {
124                 }
125
126                 internal StackTrace (Exception e, int skipFrames, bool fNeedFileInfo, bool returnNativeFrames)
127                 {
128                         if (e == null)
129                                 throw new ArgumentNullException ("e");
130                         if (skipFrames < 0)
131                                 throw new ArgumentOutOfRangeException ("< 0", "skipFrames");
132
133                         frames = get_trace (e, skipFrames, fNeedFileInfo);
134
135                         if (!returnNativeFrames) {
136                                 bool resize = false;
137                                 for (int i = 0; i < frames.Length; ++i)
138                                         if (frames [i].GetMethod () == null)
139                                                 resize = true;
140
141                                 if (resize) {
142                                         var l = new List<StackFrame> ();
143
144                                         for (int i = 0; i < frames.Length; ++i)
145                                                 if (frames [i].GetMethod () != null)
146                                                         l.Add (frames [i]);
147
148                                         frames = l.ToArray ();
149                                 }
150                         }
151                 }
152
153                 public StackTrace (StackFrame frame)
154                 {
155                         this.frames = new StackFrame [1];
156                         this.frames [0] = frame;
157                 }
158
159                 [MonoLimitation ("Not possible to create StackTraces from other threads")]
160                 [Obsolete]
161                 public StackTrace (Thread targetThread, bool needFileInfo)
162                 {
163                         if (targetThread == Thread.CurrentThread){
164                                 init_frames (METHODS_TO_SKIP, needFileInfo);
165                                 return;
166                         }
167                         
168                         throw new NotImplementedException ();
169                 }
170
171                 public virtual int FrameCount {
172                         get {
173                                 return (frames == null) ? 0 : frames.Length;
174                         }
175                 }
176
177                 public virtual StackFrame GetFrame (int index)
178                 {
179                         if ((index < 0) || (index >= FrameCount)) {
180                                 return null;
181                         }
182
183                         return frames [index];
184                 }
185
186                 [ComVisibleAttribute (false)]
187                 public virtual StackFrame[] GetFrames ()
188                 {
189                         return frames;
190                 }
191
192                 internal bool AddFrames (StringBuilder sb, bool isException = false)
193                 {
194                         bool printOffset;
195                         string debugInfo, indentation;
196                         string unknown = Locale.GetText ("<unknown method>");
197
198                         if (isException) {
199                                 printOffset = true;
200                                 indentation = "  ";
201                                 debugInfo = Locale.GetText (" in {0}:{1} ");
202                         } else {
203                                 printOffset = false;
204                                 indentation = "   ";
205                                 debugInfo = Locale.GetText (" in {0}:line {1}");
206                         }
207
208                         var newline = String.Format ("{0}{1}{2} ", Environment.NewLine, indentation,
209                                         Locale.GetText ("at"));
210
211                         int i;
212                         for (i = 0; i < FrameCount; i++) {
213                                 StackFrame frame = GetFrame (i);
214                                 if (i == 0)
215                                         sb.AppendFormat ("{0}{1} ", indentation, Locale.GetText ("at"));
216                                 else
217                                         sb.Append (newline);
218
219                                 if (frame.GetMethod () == null) {
220                                         string internal_name = frame.GetInternalMethodName ();
221                                         if (internal_name != null)
222                                                 sb.Append (internal_name);
223                                         else if (printOffset)
224                                                 sb.AppendFormat ("<0x{0:x5} + 0x{1:x5}> {2}", frame.GetMethodAddress (), frame.GetNativeOffset (), unknown);
225                                         else
226                                                 sb.AppendFormat (unknown);
227                                 } else {
228                                         GetFullNameForStackTrace (sb, frame.GetMethod ());
229
230                                         if (printOffset) {
231                                                 if (frame.GetILOffset () == -1) {
232                                                         sb.AppendFormat (" <0x{0:x5} + 0x{1:x5}>", frame.GetMethodAddress (), frame.GetNativeOffset ());
233                                                         if (frame.GetMethodIndex () != 0xffffff)
234                                                                 sb.AppendFormat (" {0}", frame.GetMethodIndex ());
235                                                 } else {
236                                                         sb.AppendFormat (" [0x{0:x5}]", frame.GetILOffset ());
237                                                 }
238                                         }
239
240                                         sb.AppendFormat (debugInfo, frame.GetSecureFileName (),
241                                                          frame.GetFileLineNumber ());
242                                 }
243                         }
244
245                         return i != 0;
246                 }
247
248                 // This method is also used with reflection by mono-symbolicate tool.
249                 // mono-symbolicate tool uses this method to check which method matches
250                 // the stack frame method signature.
251                 static void GetFullNameForStackTrace (StringBuilder sb, MethodBase mi)
252                 {
253                         var declaringType = mi.DeclaringType;
254                         if (declaringType.IsGenericType && !declaringType.IsGenericTypeDefinition)
255                                 declaringType = declaringType.GetGenericTypeDefinition ();
256
257                         // Get generic definition
258                         var bindingflags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
259                         foreach (var m in declaringType.GetMethods (bindingflags)) {
260                                 if (m.MetadataToken == mi.MetadataToken) {
261                                         mi = m;
262                                         break;
263                                 }
264                         }
265
266                         sb.Append (declaringType.ToString ());
267
268                         sb.Append (".");
269                         sb.Append (mi.Name);
270
271                         if (mi.IsGenericMethod) {
272                                 Type[] gen_params = mi.GetGenericArguments ();
273                                 sb.Append ("[");
274                                 for (int j = 0; j < gen_params.Length; j++) {
275                                         if (j > 0)
276                                                 sb.Append (",");
277                                         sb.Append (gen_params [j].Name);
278                                 }
279                                 sb.Append ("]");
280                         }
281
282                         ParameterInfo[] p = mi.GetParametersInternal ();
283
284                         sb.Append (" (");
285                         for (int i = 0; i < p.Length; ++i) {
286                                 if (i > 0)
287                                         sb.Append (", ");
288
289                                 Type pt = p[i].ParameterType;
290                                 if (pt.IsGenericType && ! pt.IsGenericTypeDefinition)
291                                         pt = pt.GetGenericTypeDefinition ();
292
293                                 if (pt.IsClass && !String.IsNullOrEmpty (pt.Namespace)) {
294                                         sb.Append (pt.Namespace);
295                                         sb.Append (".");
296                                 }
297                                 sb.Append (pt.Name);
298                                 if (p [i].Name != null) {
299                                         sb.Append (" ");
300                                         sb.Append (p [i].Name);
301                                 }
302                         }
303                         sb.Append (")");
304                 }
305
306                 public override string ToString ()
307                 {
308                         StringBuilder sb = new StringBuilder ();
309                         AddFrames (sb);
310                         return sb.ToString ();
311                 }
312
313                 internal String ToString (TraceFormat traceFormat)
314                 {
315                         // TODO:
316                         return ToString ();
317                 }
318         }
319 }