StackFrame.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. namespace System.Diagnostics {
  37. [Serializable]
  38. [MonoTODO ("Fix serialization compatibility with MS.NET")]
  39. public class StackFrame {
  40. public const int OFFSET_UNKNOWN = -1;
  41. private int ilOffset = OFFSET_UNKNOWN;
  42. private int nativeOffset = OFFSET_UNKNOWN;
  43. private MethodBase methodBase;
  44. private string fileName;
  45. private int lineNumber;
  46. private int columnNumber;
  47. private string internalMethodName;
  48. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  49. extern static bool get_frame_info (int skip, bool needFileInfo, out MethodBase method,
  50. out int iloffset, out int native_offset,
  51. out string file, out int line, out int column);
  52. public StackFrame ()
  53. {
  54. get_frame_info (2, false, out methodBase, out ilOffset,
  55. out nativeOffset, out fileName, out lineNumber,
  56. out columnNumber);
  57. }
  58. public StackFrame (bool needFileInfo)
  59. {
  60. get_frame_info (2, needFileInfo, out methodBase, out ilOffset,
  61. out nativeOffset, out fileName, out lineNumber,
  62. out columnNumber);
  63. }
  64. public StackFrame (int skipFrames)
  65. {
  66. get_frame_info (skipFrames + 2, false, out methodBase, out ilOffset,
  67. out nativeOffset, out fileName, out lineNumber,
  68. out columnNumber);
  69. }
  70. public StackFrame (int skipFrames, bool needFileInfo)
  71. {
  72. get_frame_info (skipFrames + 2, needFileInfo, out methodBase, out ilOffset,
  73. out nativeOffset, out fileName, out lineNumber,
  74. out columnNumber);
  75. }
  76. // LAMESPEC: According to the MSDN docs, this creates a frame with _only_
  77. // the filename and lineNumber, but MS fills out the frame info as well.
  78. public StackFrame (string fileName, int lineNumber)
  79. {
  80. get_frame_info (2, false, out methodBase, out ilOffset,
  81. out nativeOffset, out fileName, out lineNumber,
  82. out columnNumber);
  83. this.fileName = fileName;
  84. this.lineNumber = lineNumber;
  85. this.columnNumber = 0;
  86. }
  87. // LAMESPEC: According to the MSDN docs, this creates a frame with _only_
  88. // the filename, lineNumber and colNumber, but MS fills out the frame info as well.
  89. public StackFrame (string fileName, int lineNumber, int colNumber)
  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 = colNumber;
  97. }
  98. public virtual int GetFileLineNumber()
  99. {
  100. return lineNumber;
  101. }
  102. public virtual int GetFileColumnNumber()
  103. {
  104. return columnNumber;
  105. }
  106. public virtual string GetFileName()
  107. {
  108. #if NET_2_0
  109. if (SecurityManager.SecurityEnabled && (fileName != null) && (fileName.Length > 0)) {
  110. string fn = Path.GetFullPath (fileName);
  111. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fn).Demand ();
  112. }
  113. #endif
  114. return fileName;
  115. }
  116. public virtual int GetILOffset()
  117. {
  118. return ilOffset;
  119. }
  120. #if ONLY_1_1
  121. [ReflectionPermission (SecurityAction.Demand, TypeInformation = true)]
  122. #endif
  123. public virtual MethodBase GetMethod ()
  124. {
  125. return methodBase;
  126. }
  127. public virtual int GetNativeOffset()
  128. {
  129. return nativeOffset;
  130. }
  131. internal string GetInternalMethodName ()
  132. {
  133. return internalMethodName;
  134. }
  135. public override string ToString ()
  136. {
  137. StringBuilder sb = new StringBuilder ();
  138. if (methodBase == null) {
  139. sb.Append (Locale.GetText ("<unknown method>"));
  140. } else {
  141. sb.Append (methodBase.Name);
  142. }
  143. sb.Append (Locale.GetText (" at "));
  144. if (ilOffset == OFFSET_UNKNOWN) {
  145. sb.Append (Locale.GetText ("<unknown offset>"));
  146. } else {
  147. sb.Append (Locale.GetText ("offset "));
  148. sb.Append (ilOffset);
  149. }
  150. sb.Append (Locale.GetText (" in file:line:column "));
  151. if (fileName == null) {
  152. sb.Append (Locale.GetText ("<filename unknown>"));
  153. } else {
  154. try {
  155. sb.Append (GetFileName ());
  156. }
  157. catch (SecurityException) {
  158. sb.Append (Locale.GetText ("<filename unknown>"));
  159. }
  160. }
  161. sb.AppendFormat (":{0}:{1}", lineNumber, columnNumber);
  162. return sb.ToString ();
  163. }
  164. }
  165. }