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;
42 namespace System.Diagnostics {
46 [MonoTODO ("Serialized objects are not compatible with .NET")]
47 public class StackTrace {
49 // TraceFormat is Used to specify options for how the
50 // string-representation of a StackTrace should be generated.
51 internal enum TraceFormat
54 TrailingNewLine, // include a trailing new line character
55 NoResourceLookup // to prevent infinite resource recusion
58 public const int METHODS_TO_SKIP = 0;
60 private StackFrame[] frames;
61 readonly StackTrace[] captured_traces;
62 private bool debug_info;
64 private static Dictionary<string, Func<StackTrace, string>> metadataHandlers;
68 metadataHandlers = new Dictionary<string, Func<StackTrace, string>> ();
70 InitMetadataHandlers ();
73 [MethodImplAttribute (MethodImplOptions.NoInlining)]
76 init_frames (METHODS_TO_SKIP, false);
79 [MethodImplAttribute (MethodImplOptions.NoInlining)]
80 public StackTrace (bool fNeedFileInfo)
82 init_frames (METHODS_TO_SKIP, fNeedFileInfo);
85 [MethodImplAttribute (MethodImplOptions.NoInlining)]
86 public StackTrace (int skipFrames)
88 init_frames (skipFrames, false);
91 [MethodImplAttribute (MethodImplOptions.NoInlining)]
92 public StackTrace (int skipFrames, bool fNeedFileInfo)
94 init_frames (skipFrames, fNeedFileInfo);
97 [MethodImplAttribute (MethodImplOptions.NoInlining)]
98 void init_frames (int skipFrames, bool fNeedFileInfo)
101 throw new ArgumentOutOfRangeException ("< 0", "skipFrames");
104 var l = new List<StackFrame> ();
108 while ((sf = new StackFrame (skipFrames, fNeedFileInfo)) != null &&
109 sf.GetMethod () != null) {
115 debug_info = fNeedFileInfo;
116 frames = l.ToArray ();
119 [MethodImplAttribute(MethodImplOptions.InternalCall)]
120 extern static StackFrame [] get_trace (Exception e, int skipFrames, bool fNeedFileInfo);
122 public StackTrace (Exception e)
123 : this (e, METHODS_TO_SKIP, false)
127 public StackTrace (Exception e, bool fNeedFileInfo)
128 : this (e, METHODS_TO_SKIP, fNeedFileInfo)
132 public StackTrace (Exception e, int skipFrames)
133 : this (e, skipFrames, false)
137 public StackTrace (Exception e, int skipFrames, bool fNeedFileInfo)
140 throw new ArgumentNullException ("e");
142 throw new ArgumentOutOfRangeException ("< 0", "skipFrames");
144 frames = get_trace (e, skipFrames, fNeedFileInfo);
146 captured_traces = e.captured_traces;
149 public StackTrace (StackFrame frame)
151 this.frames = new StackFrame [1];
152 this.frames [0] = frame;
155 [MonoLimitation ("Not possible to create StackTraces from other threads")]
157 public StackTrace (Thread targetThread, bool needFileInfo)
159 if (targetThread == Thread.CurrentThread){
160 init_frames (METHODS_TO_SKIP, needFileInfo);
164 throw new NotImplementedException ();
167 internal StackTrace (StackFrame[] frames) {
168 this.frames = frames;
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 bool AddFrames (StringBuilder sb)
195 string debugInfo, indentation;
196 string unknown = Locale.GetText ("<unknown method>");
199 debugInfo = Locale.GetText (" in {0}:{1} ");
201 var newline = String.Format ("{0}{1}{2} ", Environment.NewLine, indentation,
202 Locale.GetText ("at"));
205 for (i = 0; i < FrameCount; i++) {
206 StackFrame frame = GetFrame (i);
208 sb.AppendFormat ("{0}{1} ", indentation, Locale.GetText ("at"));
212 if (frame.GetMethod () == null) {
213 string internal_name = frame.GetInternalMethodName ();
214 if (internal_name != null)
215 sb.Append (internal_name);
217 sb.AppendFormat ("<0x{0:x5} + 0x{1:x5}> {2}", frame.GetMethodAddress (), frame.GetNativeOffset (), unknown);
219 GetFullNameForStackTrace (sb, frame.GetMethod ());
221 if (frame.GetILOffset () == -1) {
222 sb.AppendFormat (" <0x{0:x5} + 0x{1:x5}>", frame.GetMethodAddress (), frame.GetNativeOffset ());
223 if (frame.GetMethodIndex () != 0xffffff)
224 sb.AppendFormat (" {0}", frame.GetMethodIndex ());
226 sb.AppendFormat (" [0x{0:x5}]", frame.GetILOffset ());
229 sb.AppendFormat (debugInfo, frame.GetSecureFileName (),
230 frame.GetFileLineNumber ());
237 public static void GetFullNameForStackTrace (StringBuilder sb, MethodBase mi)
239 var declaringType = mi.DeclaringType;
240 if (declaringType.IsGenericType && !declaringType.IsGenericTypeDefinition)
241 declaringType = declaringType.GetGenericTypeDefinition ();
243 // Get generic definition
244 const BindingFlags bindingflags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
245 foreach (var m in declaringType.GetMethods (bindingflags)) {
246 if (m.MetadataToken == mi.MetadataToken) {
252 sb.Append (declaringType.ToString ());
257 if (mi.IsGenericMethod) {
258 Type[] gen_params = mi.GetGenericArguments ();
260 for (int j = 0; j < gen_params.Length; j++) {
263 sb.Append (gen_params [j].Name);
268 ParameterInfo[] p = mi.GetParameters ();
271 for (int i = 0; i < p.Length; ++i) {
275 Type pt = p[i].ParameterType;
276 if (pt.IsGenericType && ! pt.IsGenericTypeDefinition)
277 pt = pt.GetGenericTypeDefinition ();
279 sb.Append (pt.ToString());
281 if (p [i].Name != null) {
283 sb.Append (p [i].Name);
289 public override string ToString ()
291 StringBuilder sb = new StringBuilder ();
294 // Add traces captured using ExceptionDispatchInfo
296 if (captured_traces != null) {
297 foreach (var t in captured_traces) {
298 if (!t.AddFrames (sb))
301 sb.Append (Environment.NewLine);
302 sb.Append ("--- End of stack trace from previous location where exception was thrown ---");
303 sb.Append (Environment.NewLine);
309 foreach (var handler in metadataHandlers) {
310 var lines = handler.Value (this);
311 using (var reader = new StringReader (lines)) {
313 while ((line = reader.ReadLine()) != null) {
315 sb.Append (string.Format ("[{0}] {1}", handler.Key, line));
320 return sb.ToString ();
324 internal String ToString (TraceFormat traceFormat)
330 static void InitMetadataHandlers ()
332 string aotid = Assembly.GetAotId ();
333 if (aotid != "00000000-0000-0000-0000-000000000000")
334 AddMetadataHandler ("AOTID", st => { return aotid; });
336 AddMetadataHandler ("MVID", st => {
337 var mvidLines = new Dictionary<Guid, List<int>> ();
338 var frames = st.GetFrames ();
339 for (var lineNumber = 0; lineNumber < frames.Length; lineNumber++) {
340 var mvid = frames[lineNumber].GetMethod ().Module.ModuleVersionId;
341 if (!mvidLines.ContainsKey (mvid))
342 mvidLines.Add (mvid, new List<int> ());
344 mvidLines[mvid].Add (lineNumber);
347 var sb = new StringBuilder ();
348 foreach (var kv in mvidLines) {
349 var mvid = kv.Key.ToString ().ToUpper ();
350 sb.AppendLine (string.Format ("{0} {1}", mvid, string.Join (",", kv.Value)));
353 return sb.ToString ();
357 private static void AddMetadataHandler (string id, Func<StackTrace, string> handler)
359 metadataHandlers.Add (id, handler);