StackFrame.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Text;
  5. using System.Reflection;
  6. namespace System.Diagnostics
  7. {
  8. /// <summary>
  9. /// There is no good reason for the methods of this class to be virtual.
  10. /// </summary>
  11. public partial class StackFrame
  12. {
  13. /// <summary>
  14. /// Reflection information for the method if available, null otherwise.
  15. /// </summary>
  16. private MethodBase? _method;
  17. /// <summary>
  18. /// Native offset of the current instruction within the current method if available,
  19. /// OFFSET_UNKNOWN otherwise.
  20. /// </summary>
  21. private int _nativeOffset;
  22. /// <summary>
  23. /// IL offset of the current instruction within the current method if available,
  24. /// OFFSET_UNKNOWN otherwise.
  25. /// </summary>
  26. private int _ilOffset;
  27. /// <summary>
  28. /// Source file name representing the current code location if available, null otherwise.
  29. /// </summary>
  30. private string? _fileName;
  31. /// <summary>
  32. /// Line number representing the current code location if available, 0 otherwise.
  33. /// </summary>
  34. private int _lineNumber;
  35. /// <summary>
  36. /// Column number representing the current code location if available, 0 otherwise.
  37. /// </summary>
  38. private int _columnNumber;
  39. /// <summary>
  40. /// This flag is set to true when the frame represents a rethrow marker.
  41. /// </summary>
  42. private bool _isLastFrameFromForeignExceptionStackTrace;
  43. private void InitMembers()
  44. {
  45. _nativeOffset = OFFSET_UNKNOWN;
  46. _ilOffset = OFFSET_UNKNOWN;
  47. }
  48. /// <summary>
  49. /// Constructs a StackFrame corresponding to the active stack frame.
  50. /// </summary>
  51. public StackFrame()
  52. {
  53. InitMembers();
  54. BuildStackFrame(StackTrace.METHODS_TO_SKIP, false);
  55. }
  56. /// <summary>
  57. /// Constructs a StackFrame corresponding to the active stack frame.
  58. /// </summary>
  59. public StackFrame(bool needFileInfo)
  60. {
  61. InitMembers();
  62. BuildStackFrame(StackTrace.METHODS_TO_SKIP, needFileInfo);
  63. }
  64. /// <summary>
  65. /// Constructs a StackFrame corresponding to a calling stack frame.
  66. /// </summary>
  67. public StackFrame(int skipFrames)
  68. {
  69. InitMembers();
  70. BuildStackFrame(skipFrames + StackTrace.METHODS_TO_SKIP, false);
  71. }
  72. /// <summary>
  73. /// Constructs a StackFrame corresponding to a calling stack frame.
  74. /// </summary>
  75. public StackFrame(int skipFrames, bool needFileInfo)
  76. {
  77. InitMembers();
  78. BuildStackFrame(skipFrames + StackTrace.METHODS_TO_SKIP, needFileInfo);
  79. }
  80. /// <summary>
  81. /// Constructs a "fake" stack frame, just containing the given file
  82. /// name and line number. Use when you don't want to use the
  83. /// debugger's line mapping logic.
  84. /// </summary>
  85. public StackFrame(string? fileName, int lineNumber)
  86. {
  87. InitMembers();
  88. BuildStackFrame(StackTrace.METHODS_TO_SKIP, false);
  89. _fileName = fileName;
  90. _lineNumber = lineNumber;
  91. }
  92. /// <summary>
  93. /// Constructs a "fake" stack frame, just containing the given file
  94. /// name, line number and column number. Use when you don't want to
  95. /// use the debugger's line mapping logic.
  96. /// </summary>
  97. public StackFrame(string? fileName, int lineNumber, int colNumber)
  98. : this (fileName, lineNumber)
  99. {
  100. _columnNumber = colNumber;
  101. }
  102. /// <summary>
  103. /// Constant returned when the native or IL offset is unknown
  104. /// </summary>
  105. public const int OFFSET_UNKNOWN = -1;
  106. internal bool IsLastFrameFromForeignExceptionStackTrace => _isLastFrameFromForeignExceptionStackTrace;
  107. /// <summary>
  108. /// Returns the method the frame is executing
  109. /// </summary>
  110. public virtual MethodBase? GetMethod()
  111. {
  112. return _method;
  113. }
  114. /// <summary>
  115. /// Returns the offset from the start of the native (jitted) code for the
  116. /// method being executed
  117. /// </summary>
  118. public virtual int GetNativeOffset()
  119. {
  120. return _nativeOffset;
  121. }
  122. /// <summary>
  123. /// Returns the offset from the start of the IL code for the
  124. /// method being executed. This offset may be approximate depending
  125. /// on whether the jitter is generating debuggable code or not.
  126. /// </summary>
  127. public virtual int GetILOffset()
  128. {
  129. return _ilOffset;
  130. }
  131. /// <summary>
  132. /// Returns the file name containing the code being executed. This
  133. /// information is normally extracted from the debugging symbols
  134. /// for the executable.
  135. /// </summary>
  136. public virtual string? GetFileName()
  137. {
  138. return _fileName;
  139. }
  140. /// <summary>
  141. /// Returns the line number in the file containing the code being executed.
  142. /// This information is normally extracted from the debugging symbols
  143. /// for the executable.
  144. /// </summary>
  145. public virtual int GetFileLineNumber()
  146. {
  147. return _lineNumber;
  148. }
  149. /// <summary>
  150. /// Returns the column number in the line containing the code being executed.
  151. /// This information is normally extracted from the debugging symbols
  152. /// for the executable.
  153. /// </summary>
  154. public virtual int GetFileColumnNumber()
  155. {
  156. return _columnNumber;
  157. }
  158. /// <summary>
  159. /// Builds a readable representation of the stack frame
  160. /// </summary>
  161. public override string ToString()
  162. {
  163. StringBuilder sb = new StringBuilder(255);
  164. bool includeFileInfoIfAvailable;
  165. if (_method != null)
  166. {
  167. sb.Append(_method.Name);
  168. // deal with the generic portion of the method
  169. if (_method is MethodInfo methodInfo && methodInfo.IsGenericMethod)
  170. {
  171. Type[] typars = methodInfo.GetGenericArguments();
  172. sb.Append('<');
  173. int k = 0;
  174. bool fFirstTyParam = true;
  175. while (k < typars.Length)
  176. {
  177. if (fFirstTyParam == false)
  178. sb.Append(',');
  179. else
  180. fFirstTyParam = false;
  181. sb.Append(typars[k].Name);
  182. k++;
  183. }
  184. sb.Append('>');
  185. }
  186. includeFileInfoIfAvailable = true;
  187. }
  188. else
  189. {
  190. includeFileInfoIfAvailable = AppendStackFrameWithoutMethodBase(sb);
  191. }
  192. if (includeFileInfoIfAvailable)
  193. {
  194. sb.Append(" at offset ");
  195. if (_nativeOffset == OFFSET_UNKNOWN)
  196. sb.Append("<offset unknown>");
  197. else
  198. sb.Append(_nativeOffset);
  199. sb.Append(" in file:line:column ");
  200. sb.Append(_fileName ?? "<filename unknown>");
  201. sb.Append(':');
  202. sb.Append(_lineNumber);
  203. sb.Append(':');
  204. sb.Append(_columnNumber);
  205. }
  206. else
  207. {
  208. sb.Append("<null>");
  209. }
  210. sb.Append(Environment.NewLine);
  211. return sb.ToString();
  212. }
  213. }
  214. }