StackFrame.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // System.Diagnostics.StackFrame.cs
  3. //
  4. // Author:
  5. // Alexander Klyubin ([email protected])
  6. // Dietmar Maurer ([email protected])
  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. using System.IO;
  31. using System.Reflection;
  32. using System.Runtime.CompilerServices;
  33. using System.Security;
  34. using System.Security.Permissions;
  35. using System.Text;
  36. using System.Runtime.InteropServices;
  37. namespace System.Diagnostics {
  38. [Serializable]
  39. [ComVisible (true)]
  40. [MonoTODO ("Serialized objects are not compatible with MS.NET")]
  41. [StructLayout (LayoutKind.Sequential)]
  42. public class StackFrame {
  43. public const int OFFSET_UNKNOWN = -1;
  44. #region Keep in sync with object-internals.h
  45. private int ilOffset = OFFSET_UNKNOWN;
  46. private int nativeOffset = OFFSET_UNKNOWN;
  47. private long methodAddress;
  48. private uint methodIndex;
  49. private MethodBase methodBase;
  50. private string fileName;
  51. private int lineNumber;
  52. private int columnNumber;
  53. #pragma warning disable 649
  54. private string internalMethodName;
  55. #pragma warning restore 649
  56. #endregion
  57. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  58. extern static int GetILOffsetFromFile (string path, int methodToken, uint methodIndex, int nativeOffset);
  59. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  60. extern static bool get_frame_info (int skip, bool needFileInfo, out MethodBase method,
  61. out int iloffset, out int native_offset,
  62. out string file, out int line, out int column);
  63. public StackFrame ()
  64. {
  65. get_frame_info (2, false, out methodBase, out ilOffset,
  66. out nativeOffset, out fileName, out lineNumber,
  67. out columnNumber);
  68. }
  69. public StackFrame (bool fNeedFileInfo)
  70. {
  71. get_frame_info (2, fNeedFileInfo, out methodBase, out ilOffset,
  72. out nativeOffset, out fileName, out lineNumber,
  73. out columnNumber);
  74. }
  75. public StackFrame (int skipFrames)
  76. {
  77. get_frame_info (skipFrames + 2, false, out methodBase, out ilOffset,
  78. out nativeOffset, out fileName, out lineNumber,
  79. out columnNumber);
  80. }
  81. public StackFrame (int skipFrames, bool fNeedFileInfo)
  82. {
  83. get_frame_info (skipFrames + 2, fNeedFileInfo, out methodBase, out ilOffset,
  84. out nativeOffset, out fileName, out lineNumber,
  85. out columnNumber);
  86. }
  87. // LAMESPEC: According to the MSDN docs, this creates a frame with _only_
  88. // the filename and lineNumber, but MS fills out the frame info as well.
  89. public StackFrame (string fileName, int lineNumber)
  90. {
  91. get_frame_info (2, false, out methodBase, out ilOffset,
  92. out nativeOffset, out fileName, out lineNumber,
  93. out columnNumber);
  94. this.fileName = fileName;
  95. this.lineNumber = lineNumber;
  96. this.columnNumber = 0;
  97. }
  98. // LAMESPEC: According to the MSDN docs, this creates a frame with _only_
  99. // the filename, lineNumber and colNumber, but MS fills out the frame info as well.
  100. public StackFrame (string fileName, int lineNumber, int colNumber)
  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 = colNumber;
  108. }
  109. public virtual int GetFileLineNumber()
  110. {
  111. return lineNumber;
  112. }
  113. public virtual int GetFileColumnNumber()
  114. {
  115. return columnNumber;
  116. }
  117. public virtual string GetFileName()
  118. {
  119. #if !NET_2_1
  120. if (SecurityManager.SecurityEnabled && (fileName != null) && (fileName.Length > 0)) {
  121. string fn = Path.GetFullPath (fileName);
  122. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fn).Demand ();
  123. }
  124. #endif
  125. return fileName;
  126. }
  127. internal string GetSecureFileName ()
  128. {
  129. string filename = "<filename unknown>";
  130. if (fileName == null)
  131. return filename;
  132. try {
  133. filename = GetFileName ();
  134. }
  135. catch (SecurityException) {
  136. // CAS check failure
  137. }
  138. return filename;
  139. }
  140. public virtual int GetILOffset()
  141. {
  142. return ilOffset;
  143. }
  144. public virtual MethodBase GetMethod ()
  145. {
  146. return methodBase;
  147. }
  148. public virtual int GetNativeOffset()
  149. {
  150. return nativeOffset;
  151. }
  152. internal long GetMethodAddress ()
  153. {
  154. return methodAddress;
  155. }
  156. internal uint GetMethodIndex ()
  157. {
  158. return methodIndex;
  159. }
  160. internal string GetInternalMethodName ()
  161. {
  162. return internalMethodName;
  163. }
  164. public override string ToString ()
  165. {
  166. StringBuilder sb = new StringBuilder ();
  167. if (methodBase == null) {
  168. sb.Append (Locale.GetText ("<unknown method>"));
  169. } else {
  170. sb.Append (methodBase.Name);
  171. }
  172. sb.Append (Locale.GetText (" at "));
  173. if (ilOffset == OFFSET_UNKNOWN) {
  174. sb.Append (Locale.GetText ("<unknown offset>"));
  175. } else {
  176. sb.Append (Locale.GetText ("offset "));
  177. sb.Append (ilOffset);
  178. }
  179. sb.Append (Locale.GetText (" in file:line:column "));
  180. sb.Append (GetSecureFileName ());
  181. sb.AppendFormat (":{0}:{1}", lineNumber, columnNumber);
  182. return sb.ToString ();
  183. }
  184. }
  185. }