Merge pull request #217 from QuickJack/master
[mono.git] / mcs / class / corlib / System.Diagnostics / StackFrame.cs
1 //
2 // System.Diagnostics.StackFrame.cs
3 //
4 // Author:
5 //      Alexander Klyubin (klyubin@aqris.com)
6 //      Dietmar Maurer (dietmar@ximian.com)
7 //
8 // (C) 2001
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System.IO;
32 using System.Reflection;
33 using System.Runtime.CompilerServices;
34 using System.Security;
35 using System.Security.Permissions;
36 using System.Text;
37 using System.Runtime.InteropServices;
38
39 namespace System.Diagnostics {
40
41         [Serializable]
42         [ComVisible (true)]
43         [MonoTODO ("Serialized objects are not compatible with MS.NET")]
44         [StructLayout (LayoutKind.Sequential)]
45         public class StackFrame {
46
47                 public const int OFFSET_UNKNOWN = -1;
48
49                 #region Keep in sync with object-internals.h
50                 private int ilOffset = OFFSET_UNKNOWN;
51                 private int nativeOffset = OFFSET_UNKNOWN;
52                 private MethodBase methodBase;
53                 private string fileName;
54                 private int lineNumber;
55                 private int columnNumber;
56         #pragma warning disable 649
57                 private string internalMethodName;
58                 #pragma warning restore 649
59                 #endregion
60
61                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
62                 extern static bool get_frame_info (int skip, bool needFileInfo, out MethodBase method,
63                                                    out int iloffset, out int native_offset,
64                                                    out string file, out int line, out int column);
65
66                 public StackFrame ()
67                 {
68                         get_frame_info (2, false, out methodBase, out ilOffset,
69                                         out nativeOffset, out fileName, out lineNumber,
70                                         out columnNumber);                      
71                 }
72                 
73                 public StackFrame (bool fNeedFileInfo)
74                 {
75                         get_frame_info (2, fNeedFileInfo, out methodBase, out ilOffset,
76                                         out nativeOffset, out fileName, out lineNumber,
77                                         out columnNumber);                      
78                 }
79                 
80                 public StackFrame (int skipFrames)
81                 {
82                         get_frame_info (skipFrames + 2, false, out methodBase, out ilOffset,
83                                         out nativeOffset, out fileName, out lineNumber,
84                                         out columnNumber);                      
85                 }
86                 
87                 public StackFrame (int skipFrames, bool fNeedFileInfo) 
88                 {
89                         get_frame_info (skipFrames + 2, fNeedFileInfo, out methodBase, out ilOffset,
90                                         out nativeOffset, out fileName, out lineNumber,
91                                         out columnNumber);
92                 }
93                 
94                 // LAMESPEC: According to the MSDN docs, this creates a frame with _only_
95                 // the filename and lineNumber, but MS fills out the frame info as well.
96                 public StackFrame (string fileName, int lineNumber)
97                 {
98                         get_frame_info (2, false, out methodBase, out ilOffset,
99                                         out nativeOffset, out fileName, out lineNumber,
100                                         out columnNumber);
101                         this.fileName = fileName;
102                         this.lineNumber = lineNumber;
103                         this.columnNumber = 0;
104                 }
105                 
106                 // LAMESPEC: According to the MSDN docs, this creates a frame with _only_
107                 // the filename, lineNumber and colNumber, but MS fills out the frame info as well.
108                 public StackFrame (string fileName, int lineNumber, int colNumber)
109                 {
110                         get_frame_info (2, false, out methodBase, out ilOffset,
111                                         out nativeOffset, out fileName, out lineNumber,
112                                         out columnNumber);
113                         this.fileName = fileName;
114                         this.lineNumber = lineNumber;
115                         this.columnNumber = colNumber;
116                 }
117                                   
118                 public virtual int GetFileLineNumber()
119                 {
120                         return lineNumber;
121                 }
122                 
123                 public virtual int GetFileColumnNumber()
124                 {
125                         return columnNumber;
126                 }
127                 
128                 public virtual string GetFileName()
129                 {
130 #if !NET_2_1
131                         if (SecurityManager.SecurityEnabled && (fileName != null) && (fileName.Length > 0)) {
132                                 string fn = Path.GetFullPath (fileName);
133                                 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fn).Demand ();
134                         }
135 #endif
136                         return fileName;
137                 }
138
139                 internal string GetSecureFileName ()
140                 {
141                         string filename = "<filename unknown>";
142                         if (fileName == null)
143                                 return filename;
144 #if !MOONLIGHT
145                         try {
146                                 filename = GetFileName ();
147                         }
148                         catch (SecurityException) {
149                                 // CAS check failure
150                         }
151 #else
152                         // Silverlight always return <filename unknown> but that's not very useful for debugging
153                         // OTOH we do not want to share any details about the original file system (even if they
154                         // are likely available in the debugging symbols files) from the browser's plugin (but
155                         // compiling stuff from smcs is fine since it's outside the sandbox)
156                         try {
157                                 if (SecurityManager.SecurityEnabled)
158                                         filename = Path.GetFileName (fileName);
159                         }
160                         catch (ArgumentException) {
161                                 // e.g. invalid chars in filename
162                         }
163 #endif
164                         return filename;
165                 }
166                 
167                 public virtual int GetILOffset()
168                 {
169                         return ilOffset;
170                 }
171                 
172                 public virtual MethodBase GetMethod ()
173                 {
174                         return methodBase;
175                 }
176                 
177                 public virtual int GetNativeOffset()
178                 {
179                         return nativeOffset;                        
180                 }
181
182                 internal string GetInternalMethodName ()
183                 {
184                         return internalMethodName;
185                 }
186
187                 public override string ToString ()
188                 {
189                         StringBuilder sb = new StringBuilder ();
190
191                         if (methodBase == null) {
192                                 sb.Append (Locale.GetText ("<unknown method>"));
193                         } else {
194                                 sb.Append (methodBase.Name);
195                         }
196
197                         sb.Append (Locale.GetText (" at "));
198
199                         if (ilOffset == OFFSET_UNKNOWN) {
200                                 sb.Append (Locale.GetText ("<unknown offset>"));
201                         } else {
202                                 sb.Append (Locale.GetText ("offset "));
203                                 sb.Append (ilOffset);
204                         }
205
206                         sb.Append (Locale.GetText (" in file:line:column "));
207                         sb.Append (GetSecureFileName ());
208                         sb.AppendFormat (":{0}:{1}", lineNumber, columnNumber);
209                         return sb.ToString ();
210                 }
211         }
212 }