2 // System.Diagnostics.StackFrame.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.
32 using System
.Reflection
;
33 using System
.Runtime
.CompilerServices
;
34 using System
.Security
;
35 using System
.Security
.Permissions
;
37 using System
.Runtime
.InteropServices
;
39 namespace System
.Diagnostics
{
43 [MonoTODO ("Serialized objects are not compatible with MS.NET")]
44 [StructLayout (LayoutKind
.Sequential
)]
45 public class StackFrame
{
47 public const int OFFSET_UNKNOWN
= -1;
49 #region Keep in sync with object-internals.h
50 private int ilOffset
= OFFSET_UNKNOWN
;
51 private int nativeOffset
= OFFSET_UNKNOWN
;
52 private long methodAddress
;
53 private uint methodIndex
;
54 private MethodBase methodBase
;
55 private string fileName
;
56 private int lineNumber
;
57 private int columnNumber
;
58 #pragma warning disable 649
59 private string internalMethodName
;
60 #pragma warning restore 649
63 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
64 extern static bool get_frame_info (int skip
, bool needFileInfo
, out MethodBase method
,
65 out int iloffset
, out int native_offset
,
66 out string file
, out int line
, out int column
);
70 get_frame_info (2, false, out methodBase
, out ilOffset
,
71 out nativeOffset
, out fileName
, out lineNumber
,
75 public StackFrame (bool fNeedFileInfo
)
77 get_frame_info (2, fNeedFileInfo
, out methodBase
, out ilOffset
,
78 out nativeOffset
, out fileName
, out lineNumber
,
82 public StackFrame (int skipFrames
)
84 get_frame_info (skipFrames
+ 2, false, out methodBase
, out ilOffset
,
85 out nativeOffset
, out fileName
, out lineNumber
,
89 public StackFrame (int skipFrames
, bool fNeedFileInfo
)
91 get_frame_info (skipFrames
+ 2, fNeedFileInfo
, out methodBase
, out ilOffset
,
92 out nativeOffset
, out fileName
, out lineNumber
,
96 // LAMESPEC: According to the MSDN docs, this creates a frame with _only_
97 // the filename and lineNumber, but MS fills out the frame info as well.
98 public StackFrame (string fileName
, int lineNumber
)
100 get_frame_info (2, false, out methodBase
, out ilOffset
,
101 out nativeOffset
, out fileName
, out lineNumber
,
103 this.fileName
= fileName
;
104 this.lineNumber
= lineNumber
;
105 this.columnNumber
= 0;
108 // LAMESPEC: According to the MSDN docs, this creates a frame with _only_
109 // the filename, lineNumber and colNumber, but MS fills out the frame info as well.
110 public StackFrame (string fileName
, int lineNumber
, int colNumber
)
112 get_frame_info (2, false, out methodBase
, out ilOffset
,
113 out nativeOffset
, out fileName
, out lineNumber
,
115 this.fileName
= fileName
;
116 this.lineNumber
= lineNumber
;
117 this.columnNumber
= colNumber
;
120 public virtual int GetFileLineNumber()
125 public virtual int GetFileColumnNumber()
130 public virtual string GetFileName()
133 if (SecurityManager
.SecurityEnabled
&& (fileName
!= null) && (fileName
.Length
> 0)) {
134 string fn
= Path
.GetFullPath (fileName
);
135 new FileIOPermission (FileIOPermissionAccess
.PathDiscovery
, fn
).Demand ();
141 internal string GetSecureFileName ()
143 string filename
= "<filename unknown>";
144 if (fileName
== null)
147 filename
= GetFileName ();
149 catch (SecurityException
) {
155 public virtual int GetILOffset()
160 public virtual MethodBase
GetMethod ()
165 public virtual int GetNativeOffset()
170 internal long GetMethodAddress ()
172 return methodAddress
;
175 internal uint GetMethodIndex ()
180 internal string GetInternalMethodName ()
182 return internalMethodName
;
185 public override string ToString ()
187 StringBuilder sb
= new StringBuilder ();
189 if (methodBase
== null) {
190 sb
.Append (Locale
.GetText ("<unknown method>"));
192 sb
.Append (methodBase
.Name
);
195 sb
.Append (Locale
.GetText (" at "));
197 if (ilOffset
== OFFSET_UNKNOWN
) {
198 sb
.Append (Locale
.GetText ("<unknown offset>"));
200 sb
.Append (Locale
.GetText ("offset "));
201 sb
.Append (ilOffset
);
204 sb
.Append (Locale
.GetText (" in file:line:column "));
205 sb
.Append (GetSecureFileName ());
206 sb
.AppendFormat (":{0}:{1}", lineNumber
, columnNumber
);
207 return sb
.ToString ();