StackFrame.cs 6.8 KB

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