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;
32 using System.Globalization;
33 using System.Reflection;
34 using System.Runtime.CompilerServices;
35 using System.Runtime.InteropServices;
37 using System.Threading;
39 namespace System.Diagnostics {
42 [MonoTODO ("Fix serialization compatibility with MS.NET")]
43 public class StackTrace {
45 public const int METHODS_TO_SKIP = 0;
47 private StackFrame[] frames;
51 init_frames (METHODS_TO_SKIP, false);
54 public StackTrace (bool needFileInfo)
56 init_frames (METHODS_TO_SKIP, needFileInfo);
59 public StackTrace (int skipFrames)
61 init_frames (skipFrames, false);
64 public StackTrace (int skipFrames, bool needFileInfo)
66 init_frames (skipFrames, needFileInfo);
69 void init_frames (int skipFrames, bool needFileInfo)
72 throw new ArgumentOutOfRangeException ("< 0", "skipFrames");
75 ArrayList al = new ArrayList ();
79 while ((sf = new StackFrame (skipFrames, needFileInfo)) != null &&
80 sf.GetMethod () != null) {
86 frames = (StackFrame [])al.ToArray (typeof (StackFrame));
89 [MethodImplAttribute(MethodImplOptions.InternalCall)]
90 extern static StackFrame [] get_trace (Exception e, int skipFrames, bool needFileInfo);
92 public StackTrace (Exception e)
93 : this (e, METHODS_TO_SKIP, false)
97 public StackTrace (Exception e, bool needFileInfo)
98 : this (e, METHODS_TO_SKIP, needFileInfo)
102 public StackTrace (Exception e, int skipFrames)
103 : this (e, skipFrames, false)
107 public StackTrace (Exception e, int skipFrames, bool needFileInfo)
110 throw new ArgumentNullException ("e");
112 throw new ArgumentOutOfRangeException ("< 0", "skipFrames");
114 frames = get_trace (e, skipFrames, needFileInfo);
117 public StackTrace (StackFrame frame)
119 this.frames = new StackFrame [1];
120 this.frames [0] = frame;
124 public StackTrace (Thread targetThread, bool needFileInfo)
126 throw new NotImplementedException ();
129 public virtual int FrameCount {
131 return (frames == null) ? 0 : frames.Length;
135 public virtual StackFrame GetFrame (int index)
137 if ((index < 0) || (index >= FrameCount)) {
141 return frames [index];
145 [ComVisibleAttribute (false)]
148 // used for CAS implementation (before Fx 2.0)
151 StackFrame[] GetFrames ()
156 public override string ToString ()
158 string newline = String.Format ("{0}\t {1} ", Environment.NewLine, Locale.GetText ("at"));
159 string unknown = Locale.GetText ("<unknown method>");
160 StringBuilder sb = new StringBuilder ();
161 for (int i = 0; i < FrameCount; i++) {
162 StackFrame frame = GetFrame (i);
164 MethodBase method = frame.GetMethod ();
165 if (method != null) {
166 // Method information available
167 sb.AppendFormat ("{0}.{1} ()", method.DeclaringType.FullName, method.Name);
170 // Method information not available
175 return sb.ToString ();