Patch from Ben S. Stahlhood II (bstahlhood@gmail.com)
[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
38 namespace System.Diagnostics {
39         /// <summary>
40         ///   Stack frame.
41         /// </summary>
42
43         [Serializable]
44         [MonoTODO ("Fix serialization compatibility with MS.NET")]
45         public class StackFrame {
46                 /// <value>
47                 ///   Constant returned when the native or IL offset is unknown.
48                 /// </value>
49                 public const int OFFSET_UNKNOWN = -1;
50                 
51                 /// <value>
52                 ///   Offset from the start of the IL code for the method
53                 ///   being executed.
54                 /// </value>
55                 private int ilOffset = OFFSET_UNKNOWN;
56                 
57                 /// <value>
58                 ///   Offset from the start of the native code for the method
59                 ///   being executed.
60                 /// </value>
61                 private int nativeOffset = OFFSET_UNKNOWN;
62
63                 /// <value>
64                 ///   Method associated with this stack frame.
65                 /// </value>
66                 private MethodBase methodBase;
67                 
68                 /// <value>
69                 ///   File name.
70                 /// </value>
71                 private string fileName;
72                 
73                 /// <value>
74                 ///   Line number.
75                 /// </value>
76                 private int lineNumber;
77                 
78                 /// <value>
79                 ///   Column number.
80                 /// </value>
81                 private int columnNumber;
82
83                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
84                 extern static bool get_frame_info (int skip, bool needFileInfo, out MethodBase method,
85                                                    out int iloffset, out int native_offset,
86                                                    out string file, out int line, out int column);
87
88                 /// <summary>
89                 ///   Initializes a new StackFrame object corresponding to the
90                 ///   active stack frame.
91                 /// </summary>
92                 public StackFrame() {
93                         get_frame_info (2, false, out methodBase, out ilOffset,
94                                         out nativeOffset, out fileName, out lineNumber,
95                                         out columnNumber);                      
96                 }
97                 
98                 /// <summary>
99                 ///   Initializes a new StackFrame object corresponding to the
100                 ///   active stack frame.
101                 /// </summary>
102                 /// <param name="needFileInfo">
103                 ///   TODO:
104                 /// </param>
105                 public StackFrame (bool needFileInfo)
106                 {
107                         get_frame_info (2, needFileInfo, out methodBase, out ilOffset,
108                                         out nativeOffset, out fileName, out lineNumber,
109                                         out columnNumber);                      
110                 }
111                 
112                 /// <summary>
113                 ///   Initializes a new StackFrame object corresponding to the
114                 ///   active stack frame.
115                 /// </summary>
116                 /// <param name="skipFrames">
117                 ///   The number of frames up the stack to skip.
118                 /// </param>
119                 public StackFrame(int skipFrames) {
120                         get_frame_info (skipFrames + 2, false, out methodBase, out ilOffset,
121                                         out nativeOffset, out fileName, out lineNumber,
122                                         out columnNumber);                      
123                 }
124                 
125                 /// <summary>
126                 ///   Initializes a new StackFrame object corresponding to the
127                 ///   active stack frame.
128                 /// </summary>
129                 /// <param name="skipFrames">
130                 ///   The number of frames up the stack to skip.
131                 /// </param>
132                 /// <param name="needFileInfo">
133                 ///   TODO:
134                 /// </param>
135                 public StackFrame(int skipFrames, bool needFileInfo) {
136                         get_frame_info (skipFrames + 2, needFileInfo, out methodBase, out ilOffset,
137                                         out nativeOffset, out fileName, out lineNumber,
138                                         out columnNumber);
139                 }
140                 
141                 /// <summary>
142                 ///   Constructs a fake stack frame that just contains the
143                 ///   given file name and line number. Use this constructor
144                 ///   when you do not want to use the debugger's line mapping
145                 ///   logic.
146                 /// </summary>
147                 /// <param name="fileName">
148                 ///   The given file name.
149                 /// </param>
150                 /// <param name="lineNumber">
151                 ///   The line number in the specified file.
152                 /// </param>
153                                 // LAMESPEC: According to the MSDN docs, this creates a
154                                 // fake stack frame. But MS fills out the frame info as well
155                 public StackFrame(string fileName, int lineNumber) {
156                                         get_frame_info (2, false, out methodBase, out ilOffset,
157                                                                         out nativeOffset, out fileName, out lineNumber,
158                                                                         out columnNumber);
159                                         this.fileName = fileName;
160                                         this.lineNumber = lineNumber;
161                                         this.columnNumber = 0;
162                                 }
163                 
164                 /// <summary>
165                 ///   Constructs a fake stack frame that just contains the
166                 ///   given file name and line number. Use this constructor
167                 ///   when you do not want to use the debugger's line mapping
168                 ///   logic.
169                 /// </summary>
170                 /// <param name="fileName">
171                 ///   The given file name.
172                 /// </param>
173                 /// <param name="lineNumber">
174                 ///   The line number in the specified file.
175                 /// </param>
176                 /// <param name="colNumber">
177                 ///   The column number in the specified file.
178                 /// </param>
179                                 // LAMESPEC: According to the MSDN docs, this creates a
180                                 // fake stack frame. But MS fills out the frame info as well
181                 public StackFrame(string fileName,
182                                   int lineNumber,
183                                   int colNumber) {
184                                         get_frame_info (2, false, out methodBase, out ilOffset,
185                                                                         out nativeOffset, out fileName, out lineNumber,
186                                                                         out columnNumber);
187                                         this.fileName = fileName;
188                                         this.lineNumber = lineNumber;
189                                         this.columnNumber = colNumber;
190                 }
191                                   
192                               
193                 /// <summary>
194                 ///   Gets the line number in the file containing the code
195                 ///   being executed. This information is typically extracted
196                 ///   from the debugging symbols for the executable.
197                 /// </summary>
198                 /// <returns>
199                 ///   The file line number or zero if it cannot be determined.
200                 /// </returns>
201                 public virtual int GetFileLineNumber()
202                 {
203                         return lineNumber;
204                 }
205                 
206                 /// <summary>
207                 ///   Gets the column number in the file containing the code
208                 ///   being executed. This information is typically extracted
209                 ///   from the debugging symbols for the executable.
210                 /// </summary>
211                 /// <returns>
212                 ///   The file column number or zero if it cannot be determined.
213                 /// </returns>
214                 public virtual int GetFileColumnNumber()
215                 {
216                         return columnNumber;
217                 }
218                 
219                 /// <summary>
220                 ///   Gets the file name containing the code being executed.
221                 ///   This information is typically extracted from the
222                 ///   debugging symbols for the executable.
223                 /// </summary>
224                 /// <returns>
225                 ///   The file name or null if it cannot be determined.
226                 /// </returns> 
227                 public virtual string GetFileName()
228                 {
229                         if (SecurityManager.SecurityEnabled && (fileName != null) && (fileName.Length > 0)) {
230                                 string fn = Path.GetFullPath (fileName);
231                                 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fn).Demand ();
232                         }
233                         return fileName;
234                 }
235                 
236                 /// <summary>
237                 ///   Gets the offset from the start of the IL code for the
238                 ///   method being executed. This offset may be approximate
239                 ///   depending on whether the JIT compiler is generating
240                 ///   debugging code or not.
241                 /// </summary>
242                 /// <returns>
243                 ///   The offset from the start of the IL code for the method
244                 ///   being executed.
245                 /// </returns>
246                 public virtual int GetILOffset()
247                 {
248                         return ilOffset;
249                 }
250                 
251                 /// <summary>
252                 ///   Gets the method in which the frame is executing.
253                 /// </summary>
254                 /// <returns>
255                 ///   The method the frame is executing in.
256                 /// </returns>
257                 public virtual MethodBase GetMethod()
258                 {
259                         return methodBase;
260                 }
261                 
262                 /// <summary>
263                 ///   Gets the offset from the start of the native
264                 ///   (JIT-compiled) code for the method being executed.
265                 /// </summary>
266                 /// <returns>
267                 ///   The offset from the start of the native (JIT-compiled)
268                 ///   code or the method being executed.
269                 /// </returns>
270                 public virtual int GetNativeOffset()
271                 {
272                         return nativeOffset;                        
273                 }
274                 
275                 /// <summary>
276                 ///   Builds a readable representation of the stack frame.
277                 /// </summary>
278                 /// <returns>
279                 ///   A readable representation of the stack frame.
280                 /// </returns>
281                 public override string ToString ()
282                 {
283                         StringBuilder sb = new StringBuilder ();
284
285                         if (methodBase == null) {
286                                 sb.Append (Locale.GetText ("<unknown method>"));
287                         } else {
288                                 sb.Append (methodBase.Name);
289                         }
290
291                         sb.Append (Locale.GetText (" at "));
292
293                         if (ilOffset == OFFSET_UNKNOWN) {
294                                 sb.Append (Locale.GetText ("<unknown offset>"));
295                         } else {
296                                 sb.Append (Locale.GetText ("offset "));
297                                 sb.Append (ilOffset);
298                         }
299
300                         sb.Append (Locale.GetText (" in file:line:column "));
301
302                         if (fileName == null) {
303                                 sb.Append (Locale.GetText ("<filename unknown>"));
304                         } else {
305                                 try {
306                                         // need security check
307                                         sb.Append (GetFileName ());
308                                 }
309                                 catch (SecurityException) {
310                                         sb.Append (Locale.GetText ("<filename unknown>"));
311                                 }
312                         }
313
314                         sb.AppendFormat (":{0}:{1}", lineNumber, columnNumber);
315                         return sb.ToString ();
316                 }
317                 
318                 /// <summary>
319                 ///   Checks whether two objects are equal.
320                 ///   The objects are assumed equal if and only if either
321                 ///   both of the references are <code>null</code> or they
322                 ///   equal via <code>Equals</code> method.
323                 /// </summary>
324                 /// <param name="obj1">
325                 ///   First object.
326                 /// </param>
327                 /// <param name="obj2">
328                 ///   Second object.
329                 /// </param>
330                 /// <returns>
331                 ///   <code>true</code> if the two objects are equal,
332                 ///   </code>false</code> otherwise.
333                 /// </returns>
334                 private static bool ObjectsEqual(Object obj1, Object obj2) {
335                         if (obj1 == null) {
336                                 return (obj2 == null);
337                         } else {
338                                 return obj1.Equals(obj2);
339                         }
340                 }
341          }
342 }