This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System;
35 using System.Reflection;
36 using System.Runtime.CompilerServices;
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) : this() {
106                         get_frame_info (2, needFileInfo, out methodBase, out ilOffset,
107                                         out nativeOffset, out fileName, out lineNumber,
108                                         out columnNumber);                      
109                 }
110                 
111                 /// <summary>
112                 ///   Initializes a new StackFrame object corresponding to the
113                 ///   active stack frame.
114                 /// </summary>
115                 /// <param name="skipFrames">
116                 ///   The number of frames up the stack to skip.
117                 /// </param>
118                 public StackFrame(int skipFrames) {
119                         get_frame_info (skipFrames + 2, false, out methodBase, out ilOffset,
120                                         out nativeOffset, out fileName, out lineNumber,
121                                         out columnNumber);                      
122                 }
123                 
124                 /// <summary>
125                 ///   Initializes a new StackFrame object corresponding to the
126                 ///   active stack frame.
127                 /// </summary>
128                 /// <param name="skipFrames">
129                 ///   The number of frames up the stack to skip.
130                 /// </param>
131                 /// <param name="needFileInfo">
132                 ///   TODO:
133                 /// </param>
134                 public StackFrame(int skipFrames, bool needFileInfo) {
135                         get_frame_info (skipFrames + 2, needFileInfo, out methodBase, out ilOffset,
136                                         out nativeOffset, out fileName, out lineNumber,
137                                         out columnNumber);
138                 }
139                 
140                 /// <summary>
141                 ///   Constructs a fake stack frame that just contains the
142                 ///   given file name and line number. Use this constructor
143                 ///   when you do not want to use the debugger's line mapping
144                 ///   logic.
145                 /// </summary>
146                 /// <param name="fileName">
147                 ///   The given file name.
148                 /// </param>
149                 /// <param name="lineNumber">
150                 ///   The line number in the specified file.
151                 /// </param>
152                                 // LAMESPEC: According to the MSDN docs, this creates a
153                                 // fake stack frame. But MS fills out the frame info as well
154                 public StackFrame(string fileName, int lineNumber) {
155                                         get_frame_info (2, false, out methodBase, out ilOffset,
156                                                                         out nativeOffset, out fileName, out lineNumber,
157                                                                         out columnNumber);
158                                         this.fileName = fileName;
159                                         this.lineNumber = lineNumber;
160                                         this.columnNumber = 0;
161                                 }
162                 
163                 /// <summary>
164                 ///   Constructs a fake stack frame that just contains the
165                 ///   given file name and line number. Use this constructor
166                 ///   when you do not want to use the debugger's line mapping
167                 ///   logic.
168                 /// </summary>
169                 /// <param name="fileName">
170                 ///   The given file name.
171                 /// </param>
172                 /// <param name="lineNumber">
173                 ///   The line number in the specified file.
174                 /// </param>
175                 /// <param name="colNumber">
176                 ///   The column number in the specified file.
177                 /// </param>
178                                 // LAMESPEC: According to the MSDN docs, this creates a
179                                 // fake stack frame. But MS fills out the frame info as well
180                 public StackFrame(string fileName,
181                                   int lineNumber,
182                                   int colNumber) {
183                                         get_frame_info (2, false, out methodBase, out ilOffset,
184                                                                         out nativeOffset, out fileName, out lineNumber,
185                                                                         out columnNumber);
186                                         this.fileName = fileName;
187                                         this.lineNumber = lineNumber;
188                                         this.columnNumber = colNumber;
189                 }
190                                   
191                               
192                 /// <summary>
193                 ///   Gets the line number in the file containing the code
194                 ///   being executed. This information is typically extracted
195                 ///   from the debugging symbols for the executable.
196                 /// </summary>
197                 /// <returns>
198                 ///   The file line number or zero if it cannot be determined.
199                 /// </returns>
200                 public virtual int GetFileLineNumber()
201                 {
202                         return lineNumber;
203                 }
204                 
205                 /// <summary>
206                 ///   Gets the column number in the file containing the code
207                 ///   being executed. This information is typically extracted
208                 ///   from the debugging symbols for the executable.
209                 /// </summary>
210                 /// <returns>
211                 ///   The file column number or zero if it cannot be determined.
212                 /// </returns>
213                 public virtual int GetFileColumnNumber()
214                 {
215                         return columnNumber;
216                 }
217                 
218                 /// <summary>
219                 ///   Gets the file name containing the code being executed.
220                 ///   This information is typically extracted from the
221                 ///   debugging symbols for the executable.
222                 /// </summary>
223                 /// <returns>
224                 ///   The file name or null if it cannot be determined.
225                 /// </returns> 
226                 public virtual string GetFileName()
227                 {
228                         return fileName;
229                 }
230                 
231                 /// <summary>
232                 ///   Gets the offset from the start of the IL code for the
233                 ///   method being executed. This offset may be approximate
234                 ///   depending on whether the JIT compiler is generating
235                 ///   debugging code or not.
236                 /// </summary>
237                 /// <returns>
238                 ///   The offset from the start of the IL code for the method
239                 ///   being executed.
240                 /// </returns>
241                 public virtual int GetILOffset()
242                 {
243                         return ilOffset;
244                 }
245                 
246                 /// <summary>
247                 ///   Gets the method in which the frame is executing.
248                 /// </summary>
249                 /// <returns>
250                 ///   The method the frame is executing in.
251                 /// </returns>
252                 public virtual MethodBase GetMethod()
253                 {
254                         return methodBase;
255                 }
256                 
257                 /// <summary>
258                 ///   Gets the offset from the start of the native
259                 ///   (JIT-compiled) code for the method being executed.
260                 /// </summary>
261                 /// <returns>
262                 ///   The offset from the start of the native (JIT-compiled)
263                 ///   code or the method being executed.
264                 /// </returns>
265                 public virtual int GetNativeOffset()
266                 {
267                         return nativeOffset;                        
268                 }
269                 
270                 /// <summary>
271                 ///   Builds a readable representation of the stack frame.
272                 /// </summary>
273                 /// <returns>
274                 ///   A readable representation of the stack frame.
275                 /// </returns>
276                 public override string ToString() {
277                         string methodNameString =
278                                 (GetMethod() == null)
279                                         ? "<unknown method>"
280                                           : GetMethod().Name;
281                         string offsetString =
282                                 (GetILOffset() == OFFSET_UNKNOWN)
283                                         ? "<unknown offset>"
284                                           : "offset " + GetILOffset();
285                         string fileNameString =
286                                 (GetFileName() == null)
287                                         ? "<filename unknown>" : GetFileName();
288                         return methodNameString + " at " + offsetString
289                                 + " in file:line:column " + fileNameString
290                                 + ":" + GetFileLineNumber()
291                                 + ":" + GetFileColumnNumber();
292                 }
293                 
294                 //
295                 // These are not on the Framework
296                 //
297 #if false
298                 public override bool Equals(Object obj) {
299                         if ((obj == null) || (!(obj is StackFrame))) {
300                                 return false;
301                         }
302                         
303                         StackFrame rhs = (StackFrame) obj;
304                         
305                         if (!ObjectsEqual(GetMethod(), rhs.GetMethod())) {
306                                 return false;
307                         }
308                         
309                         if (!ObjectsEqual(GetFileName(), rhs.GetFileName())) {
310                                 return false;
311                         }
312                         
313                         if (GetFileLineNumber() != rhs.GetFileLineNumber()) {
314                                 return false;
315                         }
316                         
317                         if (GetFileColumnNumber() != rhs.GetFileColumnNumber()) {
318                                 return false;
319                         }
320                         
321                         if (GetILOffset() != rhs.GetILOffset()) {
322                                 return false;
323                         }
324                         
325                         if (GetNativeOffset() != rhs.GetNativeOffset()) {
326                                 return false;
327                         }
328                         
329                         return true;
330                         
331                 }
332                 
333                 public override int GetHashCode() {
334                         return GetFileLineNumber();
335                 }
336 #endif
337                 
338                 /// <summary>
339                 ///   Checks whether two objects are equal.
340                 ///   The objects are assumed equal if and only if either
341                 ///   both of the references are <code>null</code> or they
342                 ///   equal via <code>Equals</code> method.
343                 /// </summary>
344                 /// <param name="obj1">
345                 ///   First object.
346                 /// </param>
347                 /// <param name="obj2">
348                 ///   Second object.
349                 /// </param>
350                 /// <returns>
351                 ///   <code>true</code> if the two objects are equal,
352                 ///   </code>false</code> otherwise.
353                 /// </returns>
354                 private static bool ObjectsEqual(Object obj1, Object obj2) {
355                         if (obj1 == null) {
356                                 return (obj2 == null);
357                         } else {
358                                 return obj1.Equals(obj2);
359                         }
360                 }
361          }
362 }