2 // System.Diagnostics.StackTrace.cs
5 // Alexander Klyubin (klyubin@aqris.com)
6 // Dietmar Maurer (dietmar@ximian.com)
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
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;
39 using System.Threading;
41 namespace System.Diagnostics {
45 [MonoTODO ("Serialized objects are not compatible with .NET")]
46 public class StackTrace {
48 // TraceFormat is Used to specify options for how the
49 // string-representation of a StackTrace should be generated.
50 internal enum TraceFormat
53 TrailingNewLine, // include a trailing new line character
54 NoResourceLookup // to prevent infinite resource recusion
57 public const int METHODS_TO_SKIP = 0;
59 private StackFrame[] frames;
60 private bool debug_info;
64 init_frames (METHODS_TO_SKIP, false);
67 public StackTrace (bool fNeedFileInfo)
69 init_frames (METHODS_TO_SKIP, fNeedFileInfo);
72 public StackTrace (int skipFrames)
74 init_frames (skipFrames, false);
77 public StackTrace (int skipFrames, bool fNeedFileInfo)
79 init_frames (skipFrames, fNeedFileInfo);
82 void init_frames (int skipFrames, bool fNeedFileInfo)
85 throw new ArgumentOutOfRangeException ("< 0", "skipFrames");
88 var l = new List<StackFrame> ();
92 while ((sf = new StackFrame (skipFrames, fNeedFileInfo)) != null &&
93 sf.GetMethod () != null) {
99 debug_info = fNeedFileInfo;
100 frames = l.ToArray ();
103 [MethodImplAttribute(MethodImplOptions.InternalCall)]
104 extern static StackFrame [] get_trace (Exception e, int skipFrames, bool fNeedFileInfo);
106 public StackTrace (Exception e)
107 : this (e, METHODS_TO_SKIP, false)
111 public StackTrace (Exception e, bool fNeedFileInfo)
112 : this (e, METHODS_TO_SKIP, fNeedFileInfo)
116 public StackTrace (Exception e, int skipFrames)
117 : this (e, skipFrames, false)
121 public StackTrace (Exception e, int skipFrames, bool fNeedFileInfo)
122 : this (e, skipFrames, fNeedFileInfo, false)
126 internal StackTrace (Exception e, int skipFrames, bool fNeedFileInfo, bool returnNativeFrames)
129 throw new ArgumentNullException ("e");
131 throw new ArgumentOutOfRangeException ("< 0", "skipFrames");
133 frames = get_trace (e, skipFrames, fNeedFileInfo);
135 if (!returnNativeFrames) {
137 for (int i = 0; i < frames.Length; ++i)
138 if (frames [i].GetMethod () == null)
142 var l = new List<StackFrame> ();
144 for (int i = 0; i < frames.Length; ++i)
145 if (frames [i].GetMethod () != null)
148 frames = l.ToArray ();
153 public StackTrace (StackFrame frame)
155 this.frames = new StackFrame [1];
156 this.frames [0] = frame;
159 [MonoLimitation ("Not possible to create StackTraces from other threads")]
161 public StackTrace (Thread targetThread, bool needFileInfo)
163 if (targetThread == Thread.CurrentThread){
164 init_frames (METHODS_TO_SKIP, needFileInfo);
168 throw new NotImplementedException ();
171 public virtual int FrameCount {
173 return (frames == null) ? 0 : frames.Length;
177 public virtual StackFrame GetFrame (int index)
179 if ((index < 0) || (index >= FrameCount)) {
183 return frames [index];
186 [ComVisibleAttribute (false)]
187 public virtual StackFrame[] GetFrames ()
192 internal bool AddFrames (StringBuilder sb, bool isException = false)
195 string debugInfo, indentation;
196 string unknown = Locale.GetText ("<unknown method>");
201 debugInfo = Locale.GetText (" in {0}:{1} ");
205 debugInfo = Locale.GetText (" in {0}:line {1}");
208 var newline = String.Format ("{0}{1}{2} ", Environment.NewLine, indentation,
209 Locale.GetText ("at"));
212 for (i = 0; i < FrameCount; i++) {
213 StackFrame frame = GetFrame (i);
215 sb.AppendFormat ("{0}{1} ", indentation, Locale.GetText ("at"));
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);
226 sb.AppendFormat (unknown);
228 GetFullNameForStackTrace (sb, frame.GetMethod ());
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 ());
236 sb.AppendFormat (" [0x{0:x5}]", frame.GetILOffset ());
240 sb.AppendFormat (debugInfo, frame.GetSecureFileName (),
241 frame.GetFileLineNumber ());
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)
253 var declaringType = mi.DeclaringType;
254 if (declaringType.IsGenericType && !declaringType.IsGenericTypeDefinition)
255 declaringType = declaringType.GetGenericTypeDefinition ();
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) {
266 sb.Append (declaringType.ToString ());
271 if (mi.IsGenericMethod) {
272 Type[] gen_params = mi.GetGenericArguments ();
274 for (int j = 0; j < gen_params.Length; j++) {
277 sb.Append (gen_params [j].Name);
282 ParameterInfo[] p = mi.GetParametersInternal ();
285 for (int i = 0; i < p.Length; ++i) {
289 Type pt = p[i].ParameterType;
290 if (pt.IsGenericType && ! pt.IsGenericTypeDefinition)
291 pt = pt.GetGenericTypeDefinition ();
293 if (pt.IsClass && !String.IsNullOrEmpty (pt.Namespace)) {
294 sb.Append (pt.Namespace);
298 if (p [i].Name != null) {
300 sb.Append (p [i].Name);
306 public override string ToString ()
308 StringBuilder sb = new StringBuilder ();
310 return sb.ToString ();
313 internal String ToString (TraceFormat traceFormat)