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;
38 namespace System.Diagnostics {
41 [MonoTODO ("Fix serialization compatibility with MS.NET")]
42 public class StackFrame {
44 public const int OFFSET_UNKNOWN = -1;
46 private int ilOffset = OFFSET_UNKNOWN;
47 private int nativeOffset = OFFSET_UNKNOWN;
48 private MethodBase methodBase;
49 private string fileName;
50 private int lineNumber;
51 private int columnNumber;
52 private string internalMethodName;
54 [MethodImplAttribute(MethodImplOptions.InternalCall)]
55 extern static bool get_frame_info (int skip, bool needFileInfo, out MethodBase method,
56 out int iloffset, out int native_offset,
57 out string file, out int line, out int column);
61 get_frame_info (2, false, out methodBase, out ilOffset,
62 out nativeOffset, out fileName, out lineNumber,
66 public StackFrame (bool needFileInfo)
68 get_frame_info (2, needFileInfo, out methodBase, out ilOffset,
69 out nativeOffset, out fileName, out lineNumber,
73 public StackFrame (int skipFrames)
75 get_frame_info (skipFrames + 2, false, out methodBase, out ilOffset,
76 out nativeOffset, out fileName, out lineNumber,
80 public StackFrame (int skipFrames, bool needFileInfo)
82 get_frame_info (skipFrames + 2, needFileInfo, out methodBase, out ilOffset,
83 out nativeOffset, out fileName, out lineNumber,
87 // LAMESPEC: According to the MSDN docs, this creates a frame with _only_
88 // the filename and lineNumber, but MS fills out the frame info as well.
89 public StackFrame (string fileName, int lineNumber)
91 get_frame_info (2, false, out methodBase, out ilOffset,
92 out nativeOffset, out fileName, out lineNumber,
94 this.fileName = fileName;
95 this.lineNumber = lineNumber;
96 this.columnNumber = 0;
99 // LAMESPEC: According to the MSDN docs, this creates a frame with _only_
100 // the filename, lineNumber and colNumber, but MS fills out the frame info as well.
101 public StackFrame (string fileName, int lineNumber, int colNumber)
103 get_frame_info (2, false, out methodBase, out ilOffset,
104 out nativeOffset, out fileName, out lineNumber,
106 this.fileName = fileName;
107 this.lineNumber = lineNumber;
108 this.columnNumber = colNumber;
111 public virtual int GetFileLineNumber()
116 public virtual int GetFileColumnNumber()
121 public virtual string GetFileName()
124 if (SecurityManager.SecurityEnabled && (fileName != null) && (fileName.Length > 0)) {
125 string fn = Path.GetFullPath (fileName);
126 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fn).Demand ();
132 public virtual int GetILOffset()
138 [ReflectionPermission (SecurityAction.Demand, TypeInformation = true)]
140 public virtual MethodBase GetMethod ()
145 public virtual int GetNativeOffset()
150 internal string GetInternalMethodName ()
152 return internalMethodName;
155 public override string ToString ()
157 StringBuilder sb = new StringBuilder ();
159 if (methodBase == null) {
160 sb.Append (Locale.GetText ("<unknown method>"));
162 sb.Append (methodBase.Name);
165 sb.Append (Locale.GetText (" at "));
167 if (ilOffset == OFFSET_UNKNOWN) {
168 sb.Append (Locale.GetText ("<unknown offset>"));
170 sb.Append (Locale.GetText ("offset "));
171 sb.Append (ilOffset);
174 sb.Append (Locale.GetText (" in file:line:column "));
176 if (fileName == null) {
177 sb.Append (Locale.GetText ("<filename unknown>"));
180 sb.Append (GetFileName ());
182 catch (SecurityException) {
183 sb.Append (Locale.GetText ("<filename unknown>"));
187 sb.AppendFormat (":{0}:{1}", lineNumber, columnNumber);
188 return sb.ToString ();