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 public override string ToString ()
194 string newline = String.Format ("{0} {1} ", Environment.NewLine, Locale.GetText ("at"));
195 string unknown = Locale.GetText ("<unknown method>");
196 string debuginfo = Locale.GetText (" in {0}:line {1}");
197 StringBuilder sb = new StringBuilder ();
198 for (int i = 0; i < FrameCount; i++) {
199 StackFrame frame = GetFrame (i);
203 sb.AppendFormat (" {0} ", Locale.GetText ("at"));
204 MethodBase method = frame.GetMethod ();
205 if (method != null) {
206 // Method information available
207 sb.AppendFormat ("{0}.{1}", method.DeclaringType.FullName, method.Name);
208 /* Append parameter information */
210 ParameterInfo[] p = method.GetParametersInternal ();
211 for (int j = 0; j < p.Length; ++j) {
214 Type pt = p[j].ParameterType;
215 bool byref = pt.IsByRef;
217 pt = pt.GetElementType ();
218 if (pt.IsClass && pt.Namespace != String.Empty) {
219 sb.Append (pt.Namespace);
224 sb.Append (" ByRef");
225 sb.AppendFormat (" {0}", p [j].Name);
230 // Method information not available
235 // we were asked for debugging informations
236 // but that doesn't mean we have the debug information available
237 string fname = frame.GetSecureFileName ();
238 if (fname != "<filename unknown>")
239 sb.AppendFormat (debuginfo, fname, frame.GetFileLineNumber ());
242 return sb.ToString ();
245 internal String ToString (TraceFormat traceFormat)