StackTrace.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // System.Diagnostics.StackTrace.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.Collections;
  31. using System.Globalization;
  32. using System.Reflection;
  33. using System.Runtime.CompilerServices;
  34. using System.Runtime.InteropServices;
  35. using System.Security.Permissions;
  36. using System.Text;
  37. using System.Threading;
  38. namespace System.Diagnostics {
  39. [Serializable]
  40. [MonoTODO ("Fix serialization compatibility with MS.NET")]
  41. public class StackTrace {
  42. public const int METHODS_TO_SKIP = 0;
  43. private StackFrame[] frames;
  44. public StackTrace ()
  45. {
  46. init_frames (METHODS_TO_SKIP, false);
  47. }
  48. public StackTrace (bool needFileInfo)
  49. {
  50. init_frames (METHODS_TO_SKIP, needFileInfo);
  51. }
  52. public StackTrace (int skipFrames)
  53. {
  54. init_frames (skipFrames, false);
  55. }
  56. public StackTrace (int skipFrames, bool needFileInfo)
  57. {
  58. init_frames (skipFrames, needFileInfo);
  59. }
  60. void init_frames (int skipFrames, bool needFileInfo)
  61. {
  62. if (skipFrames < 0)
  63. throw new ArgumentOutOfRangeException ("< 0", "skipFrames");
  64. StackFrame sf;
  65. ArrayList al = new ArrayList ();
  66. skipFrames += 2;
  67. while ((sf = new StackFrame (skipFrames, needFileInfo)) != null &&
  68. sf.GetMethod () != null) {
  69. al.Add (sf);
  70. skipFrames++;
  71. };
  72. frames = (StackFrame [])al.ToArray (typeof (StackFrame));
  73. }
  74. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  75. extern static StackFrame [] get_trace (Exception e, int skipFrames, bool needFileInfo);
  76. public StackTrace (Exception e)
  77. : this (e, METHODS_TO_SKIP, false)
  78. {
  79. }
  80. public StackTrace (Exception e, bool needFileInfo)
  81. : this (e, METHODS_TO_SKIP, needFileInfo)
  82. {
  83. }
  84. public StackTrace (Exception e, int skipFrames)
  85. : this (e, skipFrames, false)
  86. {
  87. }
  88. public StackTrace (Exception e, int skipFrames, bool needFileInfo)
  89. : this (e, skipFrames, needFileInfo, false)
  90. {
  91. }
  92. internal StackTrace (Exception e, int skipFrames, bool needFileInfo, bool returnNativeFrames)
  93. {
  94. if (e == null)
  95. throw new ArgumentNullException ("e");
  96. if (skipFrames < 0)
  97. throw new ArgumentOutOfRangeException ("< 0", "skipFrames");
  98. frames = get_trace (e, skipFrames, needFileInfo);
  99. if (!returnNativeFrames) {
  100. bool resize = false;
  101. for (int i = 0; i < frames.Length; ++i)
  102. if (frames [i].GetMethod () == null)
  103. resize = true;
  104. if (resize) {
  105. ArrayList al = new ArrayList ();
  106. for (int i = 0; i < frames.Length; ++i)
  107. if (frames [i].GetMethod () != null)
  108. al.Add (frames [i]);
  109. frames = (StackFrame [])al.ToArray (typeof (StackFrame));
  110. }
  111. }
  112. }
  113. #if ONLY_1_1
  114. [ReflectionPermission (SecurityAction.Demand, TypeInformation = true)]
  115. #endif
  116. public StackTrace (StackFrame frame)
  117. {
  118. this.frames = new StackFrame [1];
  119. this.frames [0] = frame;
  120. }
  121. #if ONLY_1_1
  122. [ReflectionPermission (SecurityAction.Demand, TypeInformation = true)]
  123. #endif
  124. [MonoTODO]
  125. public StackTrace (Thread targetThread, bool needFileInfo)
  126. {
  127. throw new NotImplementedException ();
  128. }
  129. public virtual int FrameCount {
  130. get {
  131. return (frames == null) ? 0 : frames.Length;
  132. }
  133. }
  134. public virtual StackFrame GetFrame (int index)
  135. {
  136. if ((index < 0) || (index >= FrameCount)) {
  137. return null;
  138. }
  139. return frames [index];
  140. }
  141. #if NET_2_0
  142. [ComVisibleAttribute (false)]
  143. public virtual
  144. #else
  145. // used for CAS implementation (before Fx 2.0)
  146. internal
  147. #endif
  148. StackFrame[] GetFrames ()
  149. {
  150. return frames;
  151. }
  152. public override string ToString ()
  153. {
  154. string newline = String.Format ("{0}\t {1} ", Environment.NewLine, Locale.GetText ("at"));
  155. string unknown = Locale.GetText ("<unknown method>");
  156. StringBuilder sb = new StringBuilder ();
  157. for (int i = 0; i < FrameCount; i++) {
  158. StackFrame frame = GetFrame (i);
  159. sb.Append (newline);
  160. MethodBase method = frame.GetMethod ();
  161. if (method != null) {
  162. // Method information available
  163. sb.AppendFormat ("{0}.{1} ()", method.DeclaringType.FullName, method.Name);
  164. }
  165. else {
  166. // Method information not available
  167. sb.Append (unknown);
  168. }
  169. }
  170. return sb.ToString ();
  171. }
  172. }
  173. }