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 readonly StackTrace[] captured_traces;
61 private bool debug_info;
65 init_frames (METHODS_TO_SKIP, false);
68 public StackTrace (bool fNeedFileInfo)
70 init_frames (METHODS_TO_SKIP, fNeedFileInfo);
73 public StackTrace (int skipFrames)
75 init_frames (skipFrames, false);
78 public StackTrace (int skipFrames, bool fNeedFileInfo)
80 init_frames (skipFrames, fNeedFileInfo);
83 void init_frames (int skipFrames, bool fNeedFileInfo)
86 throw new ArgumentOutOfRangeException ("< 0", "skipFrames");
89 var l = new List<StackFrame> ();
93 while ((sf = new StackFrame (skipFrames, fNeedFileInfo)) != null &&
94 sf.GetMethod () != null) {
100 debug_info = fNeedFileInfo;
101 frames = l.ToArray ();
104 [MethodImplAttribute(MethodImplOptions.InternalCall)]
105 extern static StackFrame [] get_trace (Exception e, int skipFrames, bool fNeedFileInfo);
107 public StackTrace (Exception e)
108 : this (e, METHODS_TO_SKIP, false)
112 public StackTrace (Exception e, bool fNeedFileInfo)
113 : this (e, METHODS_TO_SKIP, fNeedFileInfo)
117 public StackTrace (Exception e, int skipFrames)
118 : this (e, skipFrames, false)
122 public StackTrace (Exception e, int skipFrames, bool fNeedFileInfo)
125 throw new ArgumentNullException ("e");
127 throw new ArgumentOutOfRangeException ("< 0", "skipFrames");
129 frames = get_trace (e, skipFrames, fNeedFileInfo);
131 captured_traces = e.captured_traces;
134 public StackTrace (StackFrame frame)
136 this.frames = new StackFrame [1];
137 this.frames [0] = frame;
140 [MonoLimitation ("Not possible to create StackTraces from other threads")]
142 public StackTrace (Thread targetThread, bool needFileInfo)
144 if (targetThread == Thread.CurrentThread){
145 init_frames (METHODS_TO_SKIP, needFileInfo);
149 throw new NotImplementedException ();
152 internal StackTrace (StackFrame[] frames) {
153 this.frames = frames;
156 public virtual int FrameCount {
158 return (frames == null) ? 0 : frames.Length;
162 public virtual StackFrame GetFrame (int index)
164 if ((index < 0) || (index >= FrameCount)) {
168 return frames [index];
171 [ComVisibleAttribute (false)]
172 public virtual StackFrame[] GetFrames ()
177 bool AddFrames (StringBuilder sb)
180 string debugInfo, indentation;
181 string unknown = Locale.GetText ("<unknown method>");
184 debugInfo = Locale.GetText (" in {0}:{1} ");
186 var newline = String.Format ("{0}{1}{2} ", Environment.NewLine, indentation,
187 Locale.GetText ("at"));
190 for (i = 0; i < FrameCount; i++) {
191 StackFrame frame = GetFrame (i);
193 sb.AppendFormat ("{0}{1} ", indentation, Locale.GetText ("at"));
197 if (frame.GetMethod () == null) {
198 string internal_name = frame.GetInternalMethodName ();
199 if (internal_name != null)
200 sb.Append (internal_name);
202 sb.AppendFormat ("<0x{0:x5} + 0x{1:x5}> {2}", frame.GetMethodAddress (), frame.GetNativeOffset (), unknown);
204 GetFullNameForStackTrace (sb, frame.GetMethod ());
206 if (frame.GetILOffset () == -1) {
207 sb.AppendFormat (" <0x{0:x5} + 0x{1:x5}>", frame.GetMethodAddress (), frame.GetNativeOffset ());
208 if (frame.GetMethodIndex () != 0xffffff)
209 sb.AppendFormat (" {0}", frame.GetMethodIndex ());
211 sb.AppendFormat (" [0x{0:x5}]", frame.GetILOffset ());
214 sb.AppendFormat (debugInfo, frame.GetSecureFileName (),
215 frame.GetFileLineNumber ());
222 // This method is also used with reflection by mono-symbolicate tool.
223 // mono-symbolicate tool uses this method to check which method matches
224 // the stack frame method signature.
225 static void GetFullNameForStackTrace (StringBuilder sb, MethodBase mi)
227 var declaringType = mi.DeclaringType;
228 if (declaringType.IsGenericType && !declaringType.IsGenericTypeDefinition)
229 declaringType = declaringType.GetGenericTypeDefinition ();
231 // Get generic definition
232 var bindingflags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
233 foreach (var m in declaringType.GetMethods (bindingflags)) {
234 if (m.MetadataToken == mi.MetadataToken) {
240 sb.Append (declaringType.ToString ());
245 if (mi.IsGenericMethod) {
246 Type[] gen_params = mi.GetGenericArguments ();
248 for (int j = 0; j < gen_params.Length; j++) {
251 sb.Append (gen_params [j].Name);
256 ParameterInfo[] p = mi.GetParametersInternal ();
259 for (int i = 0; i < p.Length; ++i) {
263 Type pt = p[i].ParameterType;
264 if (pt.IsGenericType && ! pt.IsGenericTypeDefinition)
265 pt = pt.GetGenericTypeDefinition ();
267 if (pt.IsClass && !String.IsNullOrEmpty (pt.Namespace)) {
268 sb.Append (pt.Namespace);
272 if (p [i].Name != null) {
274 sb.Append (p [i].Name);
280 public override string ToString ()
282 StringBuilder sb = new StringBuilder ();
285 // Add traces captured using ExceptionDispatchInfo
287 if (captured_traces != null) {
288 foreach (var t in captured_traces) {
289 if (!t.AddFrames (sb))
292 sb.Append (Environment.NewLine);
293 sb.Append ("--- End of stack trace from previous location where exception was thrown ---");
294 sb.Append (Environment.NewLine);
299 return sb.ToString ();
302 internal String ToString (TraceFormat traceFormat)