[sgen] Evacuate from emptier blocks to fuller ones
[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 long methodAddress;
53                 private uint methodIndex;
54                 private MethodBase methodBase;
55                 private string fileName;
56                 private int lineNumber;
57                 private int columnNumber;
58         #pragma warning disable 649
59                 private string internalMethodName;
60                 #pragma warning restore 649
61                 #endregion
62
63                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
64                 extern static int GetILOffsetFromFile (string path, int methodToken, uint methodIndex, int nativeOffset);
65
66                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
67                 extern static bool get_frame_info (int skip, bool needFileInfo, out MethodBase method,
68                                                    out int iloffset, out int native_offset,
69                                                    out string file, out int line, out int column);
70
71                 public StackFrame ()
72                 {
73                         get_frame_info (2, false, out methodBase, out ilOffset,
74                                         out nativeOffset, out fileName, out lineNumber,
75                                         out columnNumber);                      
76                 }
77                 
78                 public StackFrame (bool fNeedFileInfo)
79                 {
80                         get_frame_info (2, fNeedFileInfo, out methodBase, out ilOffset,
81                                         out nativeOffset, out fileName, out lineNumber,
82                                         out columnNumber);                      
83                 }
84                 
85                 public StackFrame (int skipFrames)
86                 {
87                         get_frame_info (skipFrames + 2, false, out methodBase, out ilOffset,
88                                         out nativeOffset, out fileName, out lineNumber,
89                                         out columnNumber);                      
90                 }
91                 
92                 public StackFrame (int skipFrames, bool fNeedFileInfo) 
93                 {
94                         get_frame_info (skipFrames + 2, fNeedFileInfo, out methodBase, out ilOffset,
95                                         out nativeOffset, out fileName, out lineNumber,
96                                         out columnNumber);
97                 }
98                 
99                 // LAMESPEC: According to the MSDN docs, this creates a frame with _only_
100                 // the filename and lineNumber, but MS fills out the frame info as well.
101                 public StackFrame (string fileName, int lineNumber)
102                 {
103                         get_frame_info (2, false, out methodBase, out ilOffset,
104                                         out nativeOffset, out fileName, out lineNumber,
105                                         out columnNumber);
106                         this.fileName = fileName;
107                         this.lineNumber = lineNumber;
108                         this.columnNumber = 0;
109                 }
110                 
111                 // LAMESPEC: According to the MSDN docs, this creates a frame with _only_
112                 // the filename, lineNumber and colNumber, but MS fills out the frame info as well.
113                 public StackFrame (string fileName, int lineNumber, int colNumber)
114                 {
115                         get_frame_info (2, false, out methodBase, out ilOffset,
116                                         out nativeOffset, out fileName, out lineNumber,
117                                         out columnNumber);
118                         this.fileName = fileName;
119                         this.lineNumber = lineNumber;
120                         this.columnNumber = colNumber;
121                 }
122                                   
123                 public virtual int GetFileLineNumber()
124                 {
125                         return lineNumber;
126                 }
127                 
128                 public virtual int GetFileColumnNumber()
129                 {
130                         return columnNumber;
131                 }
132                 
133                 public virtual string GetFileName()
134                 {
135 #if !NET_2_1
136                         if (SecurityManager.SecurityEnabled && (fileName != null) && (fileName.Length > 0)) {
137                                 string fn = Path.GetFullPath (fileName);
138                                 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fn).Demand ();
139                         }
140 #endif
141                         return fileName;
142                 }
143
144                 internal string GetSecureFileName ()
145                 {
146                         string filename = "<filename unknown>";
147                         if (fileName == null)
148                                 return filename;
149                         try {
150                                 filename = GetFileName ();
151                         }
152                         catch (SecurityException) {
153                                 // CAS check failure
154                         }
155                         return filename;
156                 }
157                 
158                 public virtual int GetILOffset()
159                 {
160                         return ilOffset;
161                 }
162                 
163                 public virtual MethodBase GetMethod ()
164                 {
165                         return methodBase;
166                 }
167                 
168                 public virtual int GetNativeOffset()
169                 {
170                         return nativeOffset;                        
171                 }
172
173                 internal long GetMethodAddress ()
174                 {
175                         return methodAddress;
176                 }
177
178                 internal uint GetMethodIndex ()
179                 {
180                         return methodIndex;
181                 }
182
183                 internal string GetInternalMethodName ()
184                 {
185                         return internalMethodName;
186                 }
187
188                 public override string ToString ()
189                 {
190                         StringBuilder sb = new StringBuilder ();
191
192                         if (methodBase == null) {
193                                 sb.Append (Locale.GetText ("<unknown method>"));
194                         } else {
195                                 sb.Append (methodBase.Name);
196                         }
197
198                         sb.Append (Locale.GetText (" at "));
199
200                         if (ilOffset == OFFSET_UNKNOWN) {
201                                 sb.Append (Locale.GetText ("<unknown offset>"));
202                         } else {
203                                 sb.Append (Locale.GetText ("offset "));
204                                 sb.Append (ilOffset);
205                         }
206
207                         sb.Append (Locale.GetText (" in file:line:column "));
208                         sb.Append (GetSecureFileName ());
209                         sb.AppendFormat (":{0}:{1}", lineNumber, columnNumber);
210                         return sb.ToString ();
211                 }
212         }
213 }