mscorlib(4.0.0.0) API with additions
StackTrace.cs
2 using System.Reflection;
5 using System.Security;
7 using System.Text;
8 using System.Threading;
9 
10 namespace System.Diagnostics
11 {
13  [Serializable]
14  [ComVisible(true)]
15  [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode = true)]
16  public class StackTrace
17  {
18  internal enum TraceFormat
19  {
20  Normal,
21  TrailingNewLine,
22  NoResourceLookup
23  }
24 
25  private StackFrame[] frames;
26 
27  private int m_iNumOfFrames;
28 
30  public const int METHODS_TO_SKIP = 0;
31 
32  private int m_iMethodsToSkip;
33 
36  public virtual int FrameCount => m_iNumOfFrames;
37 
39  public StackTrace()
40  {
41  m_iNumOfFrames = 0;
42  m_iMethodsToSkip = 0;
43  CaptureStackTrace(0, fNeedFileInfo: false, null, null);
44  }
45 
49  public StackTrace(bool fNeedFileInfo)
50  {
51  m_iNumOfFrames = 0;
52  m_iMethodsToSkip = 0;
53  CaptureStackTrace(0, fNeedFileInfo, null, null);
54  }
55 
59  public StackTrace(int skipFrames)
60  {
61  if (skipFrames < 0)
62  {
63  throw new ArgumentOutOfRangeException("skipFrames", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
64  }
65  m_iNumOfFrames = 0;
66  m_iMethodsToSkip = 0;
67  CaptureStackTrace(skipFrames + 0, fNeedFileInfo: false, null, null);
68  }
69 
75  public StackTrace(int skipFrames, bool fNeedFileInfo)
76  {
77  if (skipFrames < 0)
78  {
79  throw new ArgumentOutOfRangeException("skipFrames", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
80  }
81  m_iNumOfFrames = 0;
82  m_iMethodsToSkip = 0;
83  CaptureStackTrace(skipFrames + 0, fNeedFileInfo, null, null);
84  }
85 
90  {
91  if (e == null)
92  {
93  throw new ArgumentNullException("e");
94  }
95  m_iNumOfFrames = 0;
96  m_iMethodsToSkip = 0;
97  CaptureStackTrace(0, fNeedFileInfo: false, null, e);
98  }
99 
105  public StackTrace(Exception e, bool fNeedFileInfo)
106  {
107  if (e == null)
108  {
109  throw new ArgumentNullException("e");
110  }
111  m_iNumOfFrames = 0;
112  m_iMethodsToSkip = 0;
113  CaptureStackTrace(0, fNeedFileInfo, null, e);
114  }
115 
121  public StackTrace(Exception e, int skipFrames)
122  {
123  if (e == null)
124  {
125  throw new ArgumentNullException("e");
126  }
127  if (skipFrames < 0)
128  {
129  throw new ArgumentOutOfRangeException("skipFrames", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
130  }
131  m_iNumOfFrames = 0;
132  m_iMethodsToSkip = 0;
133  CaptureStackTrace(skipFrames + 0, fNeedFileInfo: false, null, e);
134  }
135 
143  public StackTrace(Exception e, int skipFrames, bool fNeedFileInfo)
144  {
145  if (e == null)
146  {
147  throw new ArgumentNullException("e");
148  }
149  if (skipFrames < 0)
150  {
151  throw new ArgumentOutOfRangeException("skipFrames", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
152  }
153  m_iNumOfFrames = 0;
154  m_iMethodsToSkip = 0;
155  CaptureStackTrace(skipFrames + 0, fNeedFileInfo, null, e);
156  }
157 
160  public StackTrace(StackFrame frame)
161  {
162  frames = new StackFrame[1];
163  frames[0] = frame;
164  m_iMethodsToSkip = 0;
165  m_iNumOfFrames = 1;
166  }
167 
173  [Obsolete("This constructor has been deprecated. Please use a constructor that does not require a Thread parameter. http://go.microsoft.com/fwlink/?linkid=14202")]
174  public StackTrace(Thread targetThread, bool needFileInfo)
175  {
176  m_iNumOfFrames = 0;
177  m_iMethodsToSkip = 0;
178  CaptureStackTrace(0, needFileInfo, targetThread, null);
179  }
180 
181  [MethodImpl(MethodImplOptions.InternalCall)]
182  [SecuritySafeCritical]
183  internal static extern void GetStackFramesInternal(StackFrameHelper sfh, int iSkip, bool fNeedFileInfo, Exception e);
184 
185  internal static int CalculateFramesToSkip(StackFrameHelper StackF, int iNumFrames)
186  {
187  int num = 0;
188  string strB = "System.Diagnostics";
189  for (int i = 0; i < iNumFrames; i++)
190  {
191  MethodBase methodBase = StackF.GetMethodBase(i);
192  if (methodBase != null)
193  {
194  Type declaringType = methodBase.DeclaringType;
195  if (declaringType == null)
196  {
197  break;
198  }
199  string @namespace = declaringType.Namespace;
200  if (@namespace == null || string.Compare(@namespace, strB, StringComparison.Ordinal) != 0)
201  {
202  break;
203  }
204  }
205  num++;
206  }
207  return num;
208  }
209 
210  private void CaptureStackTrace(int iSkip, bool fNeedFileInfo, Thread targetThread, Exception e)
211  {
212  m_iMethodsToSkip += iSkip;
213  using (StackFrameHelper stackFrameHelper = new StackFrameHelper(targetThread))
214  {
215  stackFrameHelper.InitializeSourceInfo(0, fNeedFileInfo, e);
216  m_iNumOfFrames = stackFrameHelper.GetNumberOfFrames();
217  if (m_iMethodsToSkip > m_iNumOfFrames)
218  {
219  m_iMethodsToSkip = m_iNumOfFrames;
220  }
221  if (m_iNumOfFrames != 0)
222  {
223  frames = new StackFrame[m_iNumOfFrames];
224  for (int i = 0; i < m_iNumOfFrames; i++)
225  {
226  bool dummyFlag = true;
227  bool dummyFlag2 = true;
228  StackFrame stackFrame = new StackFrame(dummyFlag, dummyFlag2);
229  stackFrame.SetMethodBase(stackFrameHelper.GetMethodBase(i));
230  stackFrame.SetOffset(stackFrameHelper.GetOffset(i));
231  stackFrame.SetILOffset(stackFrameHelper.GetILOffset(i));
232  stackFrame.SetIsLastFrameFromForeignExceptionStackTrace(stackFrameHelper.IsLastFrameFromForeignExceptionStackTrace(i));
233  if (fNeedFileInfo)
234  {
235  stackFrame.SetFileName(stackFrameHelper.GetFilename(i));
236  stackFrame.SetLineNumber(stackFrameHelper.GetLineNumber(i));
237  stackFrame.SetColumnNumber(stackFrameHelper.GetColumnNumber(i));
238  }
239  frames[i] = stackFrame;
240  }
241  if (e == null)
242  {
243  m_iMethodsToSkip += CalculateFramesToSkip(stackFrameHelper, m_iNumOfFrames);
244  }
245  m_iNumOfFrames -= m_iMethodsToSkip;
246  if (m_iNumOfFrames < 0)
247  {
248  m_iNumOfFrames = 0;
249  }
250  }
251  else
252  {
253  frames = null;
254  }
255  }
256  }
257 
261  public virtual StackFrame GetFrame(int index)
262  {
263  if (frames != null && index < m_iNumOfFrames && index >= 0)
264  {
265  return frames[index + m_iMethodsToSkip];
266  }
267  return null;
268  }
269 
272  [ComVisible(false)]
273  public virtual StackFrame[] GetFrames()
274  {
275  if (frames == null || m_iNumOfFrames <= 0)
276  {
277  return null;
278  }
279  StackFrame[] array = new StackFrame[m_iNumOfFrames];
280  Array.Copy(frames, m_iMethodsToSkip, array, 0, m_iNumOfFrames);
281  return array;
282  }
283 
286  public override string ToString()
287  {
288  return ToString(TraceFormat.TrailingNewLine);
289  }
290 
291  internal string ToString(TraceFormat traceFormat)
292  {
293  bool flag = true;
294  string arg = "at";
295  string format = "in {0}:line {1}";
296  if (traceFormat != TraceFormat.NoResourceLookup)
297  {
298  arg = Environment.GetResourceString("Word_At");
299  format = Environment.GetResourceString("StackTrace_InFileLineNumber");
300  }
301  bool flag2 = true;
302  StringBuilder stringBuilder = new StringBuilder(255);
303  for (int i = 0; i < m_iNumOfFrames; i++)
304  {
305  StackFrame frame = GetFrame(i);
306  MethodBase method = frame.GetMethod();
307  if (!(method != null))
308  {
309  continue;
310  }
311  if (flag2)
312  {
313  flag2 = false;
314  }
315  else
316  {
317  stringBuilder.Append(Environment.NewLine);
318  }
319  stringBuilder.AppendFormat(CultureInfo.InvariantCulture, " {0} ", arg);
320  Type declaringType = method.DeclaringType;
321  if (declaringType != null)
322  {
323  stringBuilder.Append(declaringType.FullName.Replace('+', '.'));
324  stringBuilder.Append(".");
325  }
326  stringBuilder.Append(method.Name);
327  if (method is MethodInfo && ((MethodInfo)method).IsGenericMethod)
328  {
329  Type[] genericArguments = ((MethodInfo)method).GetGenericArguments();
330  stringBuilder.Append("[");
331  int j = 0;
332  bool flag3 = true;
333  for (; j < genericArguments.Length; j++)
334  {
335  if (!flag3)
336  {
337  stringBuilder.Append(",");
338  }
339  else
340  {
341  flag3 = false;
342  }
343  stringBuilder.Append(genericArguments[j].Name);
344  }
345  stringBuilder.Append("]");
346  }
347  stringBuilder.Append("(");
348  ParameterInfo[] parameters = method.GetParameters();
349  bool flag4 = true;
350  for (int k = 0; k < parameters.Length; k++)
351  {
352  if (!flag4)
353  {
354  stringBuilder.Append(", ");
355  }
356  else
357  {
358  flag4 = false;
359  }
360  string str = "<UnknownType>";
361  if (parameters[k].ParameterType != null)
362  {
363  str = parameters[k].ParameterType.Name;
364  }
365  stringBuilder.Append(str + " " + parameters[k].Name);
366  }
367  stringBuilder.Append(")");
368  if (flag && frame.GetILOffset() != -1)
369  {
370  string text = null;
371  try
372  {
373  text = frame.GetFileName();
374  }
375  catch (NotSupportedException)
376  {
377  flag = false;
378  }
379  catch (SecurityException)
380  {
381  flag = false;
382  }
383  if (text != null)
384  {
385  stringBuilder.Append(' ');
386  stringBuilder.AppendFormat(CultureInfo.InvariantCulture, format, text, frame.GetFileLineNumber());
387  }
388  }
389  if (frame.GetIsLastFrameFromForeignExceptionStackTrace())
390  {
391  stringBuilder.Append(Environment.NewLine);
392  stringBuilder.Append(Environment.GetResourceString("Exception_EndStackTraceFromPreviousThrow"));
393  }
394  }
395  if (traceFormat == TraceFormat.TrailingNewLine)
396  {
397  stringBuilder.Append(Environment.NewLine);
398  }
399  return stringBuilder.ToString();
400  }
401 
402  private static string GetManagedStackTraceStringHelper(bool fNeedFileInfo)
403  {
404  StackTrace stackTrace = new StackTrace(0, fNeedFileInfo);
405  return stackTrace.ToString();
406  }
407  }
408 }
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
Discovers the attributes of a parameter and provides access to parameter metadata.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Describes a set of security permissions applied to code. This class cannot be inherited.
StackTrace()
Initializes a new instance of the T:System.Diagnostics.StackTrace class from the caller's frame.
Definition: StackTrace.cs:39
abstract Type DeclaringType
Gets the class that declares this member.
Definition: MemberInfo.cs:36
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
const int METHODS_TO_SKIP
Defines the default for the number of methods to omit from the stack trace. This field is constant.
Definition: StackTrace.cs:30
Discovers the attributes of a method and provides access to method metadata.
Definition: MethodInfo.cs:13
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
Represents a stack trace, which is an ordered collection of one or more stack frames.
Definition: StackTrace.cs:16
virtual int FrameCount
Gets the number of frames in the stack trace.
Definition: StackTrace.cs:36
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
StackTrace(StackFrame frame)
Initializes a new instance of the T:System.Diagnostics.StackTrace class that contains a single frame.
Definition: StackTrace.cs:160
StackTrace(Exception e, int skipFrames, bool fNeedFileInfo)
Initializes a new instance of the T:System.Diagnostics.StackTrace class using the provided exception ...
Definition: StackTrace.cs:143
virtual StackFrame [] GetFrames()
Returns a copy of all stack frames in the current stack trace.
Definition: StackTrace.cs:273
StackTrace(int skipFrames)
Initializes a new instance of the T:System.Diagnostics.StackTrace class from the caller's frame,...
Definition: StackTrace.cs:59
StackTrace(int skipFrames, bool fNeedFileInfo)
Initializes a new instance of the T:System.Diagnostics.StackTrace class from the caller's frame,...
Definition: StackTrace.cs:75
StringBuilder AppendFormat(string format, object arg0)
Appends the string returned by processing a composite format string, which contains zero or more form...
StackTrace(Exception e)
Initializes a new instance of the T:System.Diagnostics.StackTrace class using the provided exception ...
Definition: StackTrace.cs:89
StackTrace(Exception e, int skipFrames)
Initializes a new instance of the T:System.Diagnostics.StackTrace class using the provided exception ...
Definition: StackTrace.cs:121
SecurityAction
Specifies the security actions that can be performed using declarative security.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
StringBuilder Append(char value, int repeatCount)
Appends a specified number of copies of the string representation of a Unicode character to this inst...
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
override string ToString()
Builds a readable representation of the stack trace.
Definition: StackTrace.cs:286
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
StackTrace(Exception e, bool fNeedFileInfo)
Initializes a new instance of the T:System.Diagnostics.StackTrace class, using the provided exception...
Definition: StackTrace.cs:105
Provides information about methods and constructors.
Definition: MethodBase.cs:19
MethodImplOptions
Defines the details of how a method is implemented.
abstract string Name
Gets the name of the current member.
Definition: MemberInfo.cs:27
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
StackTrace(Thread targetThread, bool needFileInfo)
Initializes a new instance of the T:System.Diagnostics.StackTrace class for a specific thread,...
Definition: StackTrace.cs:174
static void Copy(Array sourceArray, Array destinationArray, int length)
Copies a range of elements from an T:System.Array starting at the first element and pastes them into ...
Definition: Array.cs:1275
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
Definition: Exception.cs:22
Specifies that the class can be serialized.
virtual Type ParameterType
Gets the Type of this parameter.
Provides information about a T:System.Diagnostics.StackFrame, which represents a function call on the...
Definition: StackFrame.cs:14
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
The exception that is thrown when a security error is detected.
virtual StackFrame GetFrame(int index)
Gets the specified stack frame.
Definition: StackTrace.cs:261
abstract ParameterInfo [] GetParameters()
When overridden in a derived class, gets the parameters of the specified method or constructor.
StackTrace(bool fNeedFileInfo)
Initializes a new instance of the T:System.Diagnostics.StackTrace class from the caller's frame,...
Definition: StackTrace.cs:49
Creates and controls a thread, sets its priority, and gets its status.
Definition: Thread.cs:18