StackTrace.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //
  2. // System.Diagnostics.StackTrace.cs
  3. //
  4. // Author:
  5. // Alexander Klyubin ([email protected])
  6. // Dietmar Maurer ([email protected])
  7. //
  8. // (C) 2001
  9. //
  10. using System;
  11. using System.Reflection;
  12. using System.Threading;
  13. using System.Runtime.CompilerServices;
  14. using System.Collections;
  15. namespace System.Diagnostics {
  16. /// <summary>
  17. /// Stack trace.
  18. /// TODO: more information.
  19. /// </summary>
  20. [Serializable]
  21. public class StackTrace {
  22. /// <value>
  23. /// Uses a constant to define the number of methods that are
  24. /// to be omitted from the stack trace.
  25. /// </value>
  26. public const int METHODS_TO_SKIP = 0;
  27. /// <value>
  28. /// Frames. First frame is the last stack frame pushed.
  29. /// </value>
  30. private StackFrame[] frames;
  31. /// <summary>
  32. /// Initializes a new instance of the StackTrace class.
  33. /// </summary>
  34. [MonoTODO]
  35. public StackTrace() {
  36. init_frames (METHODS_TO_SKIP, false);
  37. }
  38. /// <summary>
  39. /// Initializes a new instance of the StackTrace class.
  40. /// </summary>
  41. /// <param name="needFileInfo">
  42. /// TODO:
  43. /// </param>
  44. public StackTrace(bool needFileInfo) {
  45. init_frames (METHODS_TO_SKIP, needFileInfo);
  46. }
  47. /// <summary>
  48. /// Initializes a new instance of the StackTrace class
  49. /// from the current location, in a caller's frame.
  50. /// </summary>
  51. /// <param name="skipFrames">
  52. /// The number of frames up the stack to start the trace
  53. /// from.
  54. /// </param>
  55. public StackTrace(int skipFrames) {
  56. init_frames (skipFrames, false);
  57. }
  58. /// <summary>
  59. /// Initializes a new instance of the StackTrace class
  60. /// from the current location, in a caller's frame.
  61. /// </summary>
  62. /// <param name="skipFrames">
  63. /// The number of frames up the stack to start the trace
  64. /// from.
  65. /// </param>
  66. /// <param name="needFileInfo">
  67. /// TODO:
  68. /// </param>
  69. public StackTrace(int skipFrames, bool needFileInfo) {
  70. init_frames (skipFrames, needFileInfo);
  71. }
  72. void init_frames (int skipFrames, bool needFileInfo)
  73. {
  74. StackFrame sf;
  75. ArrayList al = new ArrayList ();
  76. skipFrames += 2;
  77. while ((sf = new StackFrame (skipFrames, needFileInfo)) != null &&
  78. sf.GetMethod () != null) {
  79. al.Add (sf);
  80. skipFrames++;
  81. };
  82. frames = (StackFrame [])al.ToArray (typeof (StackFrame));
  83. }
  84. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  85. extern static StackFrame [] get_trace (Exception e, int skipFrames, bool needFileInfo);
  86. /// <summary>
  87. /// Initializes a new instance of the StackTrace class.
  88. /// </summary>
  89. /// <param name="e">
  90. /// TODO:
  91. /// </param>
  92. public StackTrace(Exception e) {
  93. frames = get_trace (e, METHODS_TO_SKIP, false);
  94. }
  95. /// <summary>
  96. /// Initializes a new instance of the StackTrace class,
  97. /// using the provided exception object. The resulting stack
  98. /// trace describes the stack at the time of the exception.
  99. /// </summary>
  100. /// <param name="e">
  101. /// TODO:
  102. /// </param>
  103. /// <param name="needFileInfo">
  104. /// TODO:
  105. /// </param>
  106. public StackTrace(Exception e, bool needFileInfo) {
  107. frames = get_trace (e, METHODS_TO_SKIP, needFileInfo);
  108. }
  109. /// <summary>
  110. /// Initializes a new instance of the StackTrace class,
  111. /// using the provided exception object. The resulting stack
  112. /// trace describes the stack at the time of the exception.
  113. /// </summary>
  114. /// <param name="e">
  115. /// Exception.
  116. /// </param>
  117. /// <param name="skipFrames">
  118. /// The number of frames up the stack to start the trace
  119. /// from.
  120. /// </param>
  121. public StackTrace(Exception e, int skipFrames) {
  122. frames = get_trace (e, skipFrames, false);
  123. }
  124. /// <summary>
  125. /// Initializes a new instance of the StackTrace class,
  126. /// using the provided exception object. The resulting stack
  127. /// trace describes the stack at the time of the exception.
  128. /// </summary>
  129. /// <param name="e">
  130. /// Exception.
  131. /// </param>
  132. /// <param name="skipFrames">
  133. /// The number of frames up the stack to start the trace
  134. /// from.
  135. /// </param>
  136. /// <param name="needFileInfo">
  137. /// TODO:
  138. /// </param>
  139. public StackTrace(Exception e, int skipFrames, bool needFileInfo) {
  140. frames = get_trace (e, skipFrames, needFileInfo);
  141. }
  142. /// <summary>
  143. /// Initializes a new instance of the StackTrace class
  144. /// containing a single frame.
  145. /// </summary>
  146. /// <param name="frame">
  147. /// The frame that the StackTrace object should contain.
  148. /// </param>
  149. public StackTrace(StackFrame frame) {
  150. this.frames = new StackFrame[1];
  151. this.frames[0] = frame;
  152. }
  153. /// <summary>
  154. /// Initializes a new instance of the StackTrace class.
  155. /// </summary>
  156. /// <param name="targetThread">
  157. /// TODO:
  158. /// </param>
  159. /// <param name="needFileInfo">
  160. /// TODO:
  161. /// </param>
  162. [MonoTODO]
  163. public StackTrace(Thread targetThread, bool needFileInfo) {
  164. throw new NotImplementedException();
  165. }
  166. /// <summary>
  167. /// Holds the number of frames in the stack trace.
  168. /// </summary>
  169. public virtual int FrameCount {
  170. get {
  171. return (frames == null) ? 0 : frames.Length;
  172. }
  173. }
  174. /// <summary>
  175. /// Gets the specified stack frame.
  176. /// </summary>
  177. /// <param name="index">
  178. /// The index of the stack frame requested.
  179. /// </param>
  180. /// <returns>
  181. /// The specified stack frame. Returns <code>null</code> if
  182. /// frame with specified index does not exist in this stack
  183. /// trace.
  184. /// </returns>
  185. /// <remarks>
  186. /// Stack frames are numbered starting at zero, which is the
  187. /// last stack frame pushed.
  188. /// </remarks>
  189. public virtual StackFrame GetFrame(int index) {
  190. if ((index < 0) || (index >= FrameCount)) {
  191. return null;
  192. }
  193. return frames[index];
  194. }
  195. /// <summary>
  196. /// Builds a readable representation of the stack trace.
  197. /// </summary>
  198. /// <returns>
  199. /// A readable representation of the stack trace.
  200. /// </returns>
  201. public override string ToString() {
  202. string result = "";
  203. for (int i = 0; i < FrameCount; i++) {
  204. StackFrame frame = GetFrame(i);
  205. result += "\n\tat " + FrameToString(frame);
  206. }
  207. return result;
  208. }
  209. public override bool Equals(Object obj) {
  210. if ((obj == null) || (!(obj is StackTrace))) {
  211. return false;
  212. }
  213. StackTrace rhs = (StackTrace) obj;
  214. if (FrameCount != rhs.FrameCount) {
  215. return false;
  216. }
  217. for (int i = 0; i < FrameCount; i++) {
  218. if (!GetFrame(i).Equals(rhs.GetFrame(i))) {
  219. return false;
  220. }
  221. }
  222. return true;
  223. }
  224. public override int GetHashCode() {
  225. return FrameCount;
  226. }
  227. /// <summary>
  228. /// Converts single stack frame to string to be used in
  229. /// ToString method.
  230. /// </summary>
  231. /// <param name="frame">
  232. /// Frame to convert.
  233. /// </param>
  234. /// <returns>
  235. /// A readable representation of stack frame for using
  236. /// ToString.
  237. /// </returns>
  238. private static String FrameToString(StackFrame frame) {
  239. MethodBase method = frame.GetMethod();
  240. if (method != null) {
  241. // Method information available
  242. return method.DeclaringType.FullName
  243. + "." + method.Name + "()";
  244. } else {
  245. // Method information not available
  246. return "<unknown method>";
  247. }
  248. }
  249. }
  250. }