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;
36 using System.Security.Permissions;
38 using System.Threading;
40 namespace System.Diagnostics {
43 [MonoTODO ("Fix serialization compatibility with MS.NET")]
44 public class StackTrace {
46 public const int METHODS_TO_SKIP = 0;
48 private StackFrame[] frames;
52 init_frames (METHODS_TO_SKIP, false);
55 public StackTrace (bool needFileInfo)
57 init_frames (METHODS_TO_SKIP, needFileInfo);
60 public StackTrace (int skipFrames)
62 init_frames (skipFrames, false);
65 public StackTrace (int skipFrames, bool needFileInfo)
67 init_frames (skipFrames, needFileInfo);
70 void init_frames (int skipFrames, bool needFileInfo)
73 throw new ArgumentOutOfRangeException ("< 0", "skipFrames");
76 ArrayList al = new ArrayList ();
80 while ((sf = new StackFrame (skipFrames, needFileInfo)) != null &&
81 sf.GetMethod () != null) {
87 frames = (StackFrame [])al.ToArray (typeof (StackFrame));
90 [MethodImplAttribute(MethodImplOptions.InternalCall)]
91 extern static StackFrame [] get_trace (Exception e, int skipFrames, bool needFileInfo);
93 public StackTrace (Exception e)
94 : this (e, METHODS_TO_SKIP, false)
98 public StackTrace (Exception e, bool needFileInfo)
99 : this (e, METHODS_TO_SKIP, needFileInfo)
103 public StackTrace (Exception e, int skipFrames)
104 : this (e, skipFrames, false)
108 public StackTrace (Exception e, int skipFrames, bool needFileInfo)
111 throw new ArgumentNullException ("e");
113 throw new ArgumentOutOfRangeException ("< 0", "skipFrames");
115 frames = get_trace (e, skipFrames, needFileInfo);
119 [ReflectionPermission (SecurityAction.Demand, TypeInformation = true)]
121 public StackTrace (StackFrame frame)
123 this.frames = new StackFrame [1];
124 this.frames [0] = frame;
128 [ReflectionPermission (SecurityAction.Demand, TypeInformation = true)]
131 public StackTrace (Thread targetThread, bool needFileInfo)
133 throw new NotImplementedException ();
136 public virtual int FrameCount {
138 return (frames == null) ? 0 : frames.Length;
142 public virtual StackFrame GetFrame (int index)
144 if ((index < 0) || (index >= FrameCount)) {
148 return frames [index];
152 [ComVisibleAttribute (false)]
155 // used for CAS implementation (before Fx 2.0)
158 StackFrame[] GetFrames ()
163 public override string ToString ()
165 string newline = String.Format ("{0}\t {1} ", Environment.NewLine, Locale.GetText ("at"));
166 string unknown = Locale.GetText ("<unknown method>");
167 StringBuilder sb = new StringBuilder ();
168 for (int i = 0; i < FrameCount; i++) {
169 StackFrame frame = GetFrame (i);
171 MethodBase method = frame.GetMethod ();
172 if (method != null) {
173 // Method information available
174 sb.AppendFormat ("{0}.{1} ()", method.DeclaringType.FullName, method.Name);
177 // Method information not available
182 return sb.ToString ();