2 // System.Diagnostics.StackTrace.cs
5 // Alexander Klyubin (klyubin@aqris.com)
6 // Dietmar Maurer (dietmar@ximian.com)
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Reflection;
36 using System.Threading;
37 using System.Runtime.CompilerServices;
38 using System.Collections;
40 namespace System.Diagnostics {
43 /// TODO: more information.
46 [MonoTODO ("Fix serialization compatibility with MS.NET")]
47 public class StackTrace {
49 /// Uses a constant to define the number of methods that are
50 /// to be omitted from the stack trace.
52 public const int METHODS_TO_SKIP = 0;
55 /// Frames. First frame is the last stack frame pushed.
57 private StackFrame[] frames;
60 /// Initializes a new instance of the StackTrace class.
64 init_frames (METHODS_TO_SKIP, false);
68 /// Initializes a new instance of the StackTrace class.
70 /// <param name="needFileInfo">
73 public StackTrace(bool needFileInfo) {
74 init_frames (METHODS_TO_SKIP, needFileInfo);
78 /// Initializes a new instance of the StackTrace class
79 /// from the current location, in a caller's frame.
81 /// <param name="skipFrames">
82 /// The number of frames up the stack to start the trace
85 public StackTrace(int skipFrames) {
86 init_frames (skipFrames, false);
90 /// Initializes a new instance of the StackTrace class
91 /// from the current location, in a caller's frame.
93 /// <param name="skipFrames">
94 /// The number of frames up the stack to start the trace
97 /// <param name="needFileInfo">
100 public StackTrace(int skipFrames, bool needFileInfo) {
101 init_frames (skipFrames, needFileInfo);
104 void init_frames (int skipFrames, bool needFileInfo)
107 ArrayList al = new ArrayList ();
111 while ((sf = new StackFrame (skipFrames, needFileInfo)) != null &&
112 sf.GetMethod () != null) {
118 frames = (StackFrame [])al.ToArray (typeof (StackFrame));
121 [MethodImplAttribute(MethodImplOptions.InternalCall)]
122 extern static StackFrame [] get_trace (Exception e, int skipFrames, bool needFileInfo);
125 /// Initializes a new instance of the StackTrace class.
130 public StackTrace(Exception e) {
131 frames = get_trace (e, METHODS_TO_SKIP, false);
135 /// Initializes a new instance of the StackTrace class,
136 /// using the provided exception object. The resulting stack
137 /// trace describes the stack at the time of the exception.
142 /// <param name="needFileInfo">
145 public StackTrace(Exception e, bool needFileInfo) {
146 frames = get_trace (e, METHODS_TO_SKIP, needFileInfo);
150 /// Initializes a new instance of the StackTrace class,
151 /// using the provided exception object. The resulting stack
152 /// trace describes the stack at the time of the exception.
157 /// <param name="skipFrames">
158 /// The number of frames up the stack to start the trace
161 public StackTrace(Exception e, int skipFrames) {
162 frames = get_trace (e, skipFrames, false);
166 /// Initializes a new instance of the StackTrace class,
167 /// using the provided exception object. The resulting stack
168 /// trace describes the stack at the time of the exception.
173 /// <param name="skipFrames">
174 /// The number of frames up the stack to start the trace
177 /// <param name="needFileInfo">
180 public StackTrace(Exception e, int skipFrames, bool needFileInfo) {
181 frames = get_trace (e, skipFrames, needFileInfo);
185 /// Initializes a new instance of the StackTrace class
186 /// containing a single frame.
188 /// <param name="frame">
189 /// The frame that the StackTrace object should contain.
191 public StackTrace(StackFrame frame) {
192 this.frames = new StackFrame[1];
193 this.frames[0] = frame;
197 /// Initializes a new instance of the StackTrace class.
199 /// <param name="targetThread">
202 /// <param name="needFileInfo">
206 public StackTrace(Thread targetThread, bool needFileInfo) {
207 throw new NotImplementedException();
211 /// Holds the number of frames in the stack trace.
213 public virtual int FrameCount {
215 return (frames == null) ? 0 : frames.Length;
220 /// Gets the specified stack frame.
222 /// <param name="index">
223 /// The index of the stack frame requested.
226 /// The specified stack frame. Returns <code>null</code> if
227 /// frame with specified index does not exist in this stack
231 /// Stack frames are numbered starting at zero, which is the
232 /// last stack frame pushed.
234 public virtual StackFrame GetFrame(int index) {
235 if ((index < 0) || (index >= FrameCount)) {
239 return frames[index];
243 /// Builds a readable representation of the stack trace.
246 /// A readable representation of the stack trace.
248 public override string ToString() {
250 for (int i = 0; i < FrameCount; i++) {
251 StackFrame frame = GetFrame(i);
252 result += "\n\tat " + FrameToString(frame);
259 // These are not on the Framework
263 public override bool Equals(Object obj) {
264 if ((obj == null) || (!(obj is StackTrace))) {
268 StackTrace rhs = (StackTrace) obj;
270 if (FrameCount != rhs.FrameCount) {
274 for (int i = 0; i < FrameCount; i++) {
275 if (!GetFrame(i).Equals(rhs.GetFrame(i))) {
283 public override int GetHashCode() {
289 /// Converts single stack frame to string to be used in
292 /// <param name="frame">
293 /// Frame to convert.
296 /// A readable representation of stack frame for using
299 private static String FrameToString(StackFrame frame) {
300 MethodBase method = frame.GetMethod();
301 if (method != null) {
302 // Method information available
303 return method.DeclaringType.FullName
304 + "." + method.Name + "()";
306 // Method information not available
307 return "<unknown method>";