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
;
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 public const int METHODS_TO_SKIP
= 0;
50 private StackFrame
[] frames
;
51 private bool debug_info
;
55 init_frames (METHODS_TO_SKIP
, false);
58 public StackTrace (bool fNeedFileInfo
)
60 init_frames (METHODS_TO_SKIP
, fNeedFileInfo
);
63 public StackTrace (int skipFrames
)
65 init_frames (skipFrames
, false);
68 public StackTrace (int skipFrames
, bool fNeedFileInfo
)
70 init_frames (skipFrames
, fNeedFileInfo
);
73 void init_frames (int skipFrames
, bool fNeedFileInfo
)
76 throw new ArgumentOutOfRangeException ("< 0", "skipFrames");
79 ArrayList al
= new ArrayList ();
83 while ((sf
= new StackFrame (skipFrames
, fNeedFileInfo
)) != null &&
84 sf
.GetMethod () != null) {
90 debug_info
= fNeedFileInfo
;
91 frames
= (StackFrame
[])al
.ToArray (typeof (StackFrame
));
94 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
95 extern static StackFrame
[] get_trace (Exception e
, int skipFrames
, bool fNeedFileInfo
);
97 public StackTrace (Exception e
)
98 : this (e
, METHODS_TO_SKIP
, false)
102 public StackTrace (Exception e
, bool fNeedFileInfo
)
103 : this (e
, METHODS_TO_SKIP
, fNeedFileInfo
)
107 public StackTrace (Exception e
, int skipFrames
)
108 : this (e
, skipFrames
, false)
112 public StackTrace (Exception e
, int skipFrames
, bool fNeedFileInfo
)
113 : this (e
, skipFrames
, fNeedFileInfo
, false)
117 internal StackTrace (Exception e
, int skipFrames
, bool fNeedFileInfo
, bool returnNativeFrames
)
120 throw new ArgumentNullException ("e");
122 throw new ArgumentOutOfRangeException ("< 0", "skipFrames");
124 frames
= get_trace (e
, skipFrames
, fNeedFileInfo
);
126 if (!returnNativeFrames
) {
128 for (int i
= 0; i
< frames
.Length
; ++i
)
129 if (frames
[i
].GetMethod () == null)
133 ArrayList al
= new ArrayList ();
135 for (int i
= 0; i
< frames
.Length
; ++i
)
136 if (frames
[i
].GetMethod () != null)
139 frames
= (StackFrame
[])al
.ToArray (typeof (StackFrame
));
144 public StackTrace (StackFrame frame
)
146 this.frames
= new StackFrame
[1];
147 this.frames
[0] = frame
;
150 [MonoLimitation ("Not possible to create StackTraces from other threads")]
151 public StackTrace (Thread targetThread
, bool needFileInfo
)
153 if (targetThread
== Thread
.CurrentThread
){
154 init_frames (METHODS_TO_SKIP
, needFileInfo
);
158 throw new NotImplementedException ();
161 public virtual int FrameCount
{
163 return (frames
== null) ? 0 : frames
.Length
;
167 public virtual StackFrame
GetFrame (int index
)
169 if ((index
< 0) || (index
>= FrameCount
)) {
173 return frames
[index
];
176 [ComVisibleAttribute (false)]
177 public virtual StackFrame
[] GetFrames ()
182 public override string ToString ()
184 string newline
= String
.Format ("{0} {1} ", Environment
.NewLine
, Locale
.GetText ("at"));
185 string unknown
= Locale
.GetText ("<unknown method>");
186 string debuginfo
= Locale
.GetText (" in {0}:line {1}");
187 StringBuilder sb
= new StringBuilder ();
188 for (int i
= 0; i
< FrameCount
; i
++) {
189 StackFrame frame
= GetFrame (i
);
193 sb
.AppendFormat (" {0} ", Locale
.GetText ("at"));
194 MethodBase method
= frame
.GetMethod ();
195 if (method
!= null) {
196 // Method information available
197 sb
.AppendFormat ("{0}.{1}", method
.DeclaringType
.FullName
, method
.Name
);
198 /* Append parameter information */
200 ParameterInfo
[] p
= method
.GetParameters ();
201 for (int j
= 0; j
< p
.Length
; ++j
) {
204 Type pt
= p
[j
].ParameterType
;
205 bool byref
= pt
.IsByRef
;
207 pt
= pt
.GetElementType ();
208 if (pt
.IsClass
&& pt
.Namespace
!= String
.Empty
) {
209 sb
.Append (pt
.Namespace
);
214 sb
.Append (" ByRef");
215 sb
.AppendFormat (" {0}", p
[j
].Name
);
220 // Method information not available
225 // we were asked for debugging informations
226 // but that doesn't mean we have the debug information available
227 string fname
= frame
.GetSecureFileName ();
228 if (fname
!= "<filename unknown>")
229 sb
.AppendFormat (debuginfo
, fname
, frame
.GetFileLineNumber ());
232 return sb
.ToString ();