2009-10-08 Sebastien Pouliot <sebastien@ximian.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 #if NET_2_0
39 using System.Runtime.InteropServices;
40 #endif
41
42 namespace System.Diagnostics {
43
44         [Serializable]
45 #if NET_2_0
46         [ComVisible (true)]
47 #endif
48         [MonoTODO ("Serialized objects are not compatible with MS.NET")]
49         public class StackFrame {
50
51                 public const int OFFSET_UNKNOWN = -1;
52
53                 #region Keep in sync with object-internals.h
54                 private int ilOffset = OFFSET_UNKNOWN;
55                 private int nativeOffset = OFFSET_UNKNOWN;
56                 private MethodBase methodBase;
57                 private string fileName;
58                 private int lineNumber;
59                 private int columnNumber;
60         #pragma warning disable 649
61                 private string internalMethodName;
62                 #pragma warning restore 649
63                 #endregion
64
65                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
66                 extern static bool get_frame_info (int skip, bool needFileInfo, out MethodBase method,
67                                                    out int iloffset, out int native_offset,
68                                                    out string file, out int line, out int column);
69
70                 public StackFrame ()
71                 {
72                         get_frame_info (2, false, out methodBase, out ilOffset,
73                                         out nativeOffset, out fileName, out lineNumber,
74                                         out columnNumber);                      
75                 }
76                 
77                 public StackFrame (bool fNeedFileInfo)
78                 {
79                         get_frame_info (2, fNeedFileInfo, out methodBase, out ilOffset,
80                                         out nativeOffset, out fileName, out lineNumber,
81                                         out columnNumber);                      
82                 }
83                 
84                 public StackFrame (int skipFrames)
85                 {
86                         get_frame_info (skipFrames + 2, false, out methodBase, out ilOffset,
87                                         out nativeOffset, out fileName, out lineNumber,
88                                         out columnNumber);                      
89                 }
90                 
91                 public StackFrame (int skipFrames, bool fNeedFileInfo) 
92                 {
93                         get_frame_info (skipFrames + 2, fNeedFileInfo, out methodBase, out ilOffset,
94                                         out nativeOffset, out fileName, out lineNumber,
95                                         out columnNumber);
96                 }
97                 
98                 // LAMESPEC: According to the MSDN docs, this creates a frame with _only_
99                 // the filename and lineNumber, but MS fills out the frame info as well.
100                 public StackFrame (string fileName, int lineNumber)
101                 {
102                         get_frame_info (2, false, out methodBase, out ilOffset,
103                                         out nativeOffset, out fileName, out lineNumber,
104                                         out columnNumber);
105                         this.fileName = fileName;
106                         this.lineNumber = lineNumber;
107                         this.columnNumber = 0;
108                 }
109                 
110                 // LAMESPEC: According to the MSDN docs, this creates a frame with _only_
111                 // the filename, lineNumber and colNumber, but MS fills out the frame info as well.
112                 public StackFrame (string fileName, int lineNumber, int colNumber)
113                 {
114                         get_frame_info (2, false, out methodBase, out ilOffset,
115                                         out nativeOffset, out fileName, out lineNumber,
116                                         out columnNumber);
117                         this.fileName = fileName;
118                         this.lineNumber = lineNumber;
119                         this.columnNumber = colNumber;
120                 }
121                                   
122                 public virtual int GetFileLineNumber()
123                 {
124                         return lineNumber;
125                 }
126                 
127                 public virtual int GetFileColumnNumber()
128                 {
129                         return columnNumber;
130                 }
131                 
132                 public virtual string GetFileName()
133                 {
134 #if NET_2_0 && !NET_2_1
135                         if (SecurityManager.SecurityEnabled && (fileName != null) && (fileName.Length > 0)) {
136                                 string fn = Path.GetFullPath (fileName);
137                                 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fn).Demand ();
138                         }
139 #endif
140                         return fileName;
141                 }
142
143                 internal string GetSecureFileName ()
144                 {
145                         string filename = "<filename unknown>";
146                         if (fileName == null)
147                                 return filename;
148 #if !NET_2_1 || MONOTOUCH
149                         try {
150                                 filename = GetFileName ();
151                         }
152                         catch (SecurityException) {
153                                 // CAS check failure
154                         }
155 #else
156                         // Silverlight always return <filename unknown> but that's not very useful for debugging
157                         // OTOH we do not want to share any details about the original file system (even if they
158                         // are likely available in the debugging symbols files)
159                         try {
160                                 filename = Path.GetFileName (fileName);
161                         }
162                         catch (ArgumentException) {
163                                 // e.g. invalid chars in filename
164                         }
165 #endif
166                         return filename;
167                 }
168                 
169                 public virtual int GetILOffset()
170                 {
171                         return ilOffset;
172                 }
173                 
174 #if ONLY_1_1
175                 [ReflectionPermission (SecurityAction.Demand, TypeInformation = true)]
176 #endif
177                 public virtual MethodBase GetMethod ()
178                 {
179                         return methodBase;
180                 }
181                 
182                 public virtual int GetNativeOffset()
183                 {
184                         return nativeOffset;                        
185                 }
186
187                 internal string GetInternalMethodName ()
188                 {
189                         return internalMethodName;
190                 }
191
192                 public override string ToString ()
193                 {
194                         StringBuilder sb = new StringBuilder ();
195
196                         if (methodBase == null) {
197                                 sb.Append (Locale.GetText ("<unknown method>"));
198                         } else {
199                                 sb.Append (methodBase.Name);
200                         }
201
202                         sb.Append (Locale.GetText (" at "));
203
204                         if (ilOffset == OFFSET_UNKNOWN) {
205                                 sb.Append (Locale.GetText ("<unknown offset>"));
206                         } else {
207                                 sb.Append (Locale.GetText ("offset "));
208                                 sb.Append (ilOffset);
209                         }
210
211                         sb.Append (Locale.GetText (" in file:line:column "));
212                         sb.Append (GetSecureFileName ());
213                         sb.AppendFormat (":{0}:{1}", lineNumber, columnNumber);
214                         return sb.ToString ();
215                 }
216         }
217 }