diff options
Diffstat (limited to 'src/mscorlib/src/System/Diagnostics/Stacktrace.cs')
-rw-r--r-- | src/mscorlib/src/System/Diagnostics/Stacktrace.cs | 224 |
1 files changed, 105 insertions, 119 deletions
diff --git a/src/mscorlib/src/System/Diagnostics/Stacktrace.cs b/src/mscorlib/src/System/Diagnostics/Stacktrace.cs index cd88f5108f..cdedb66ed8 100644 --- a/src/mscorlib/src/System/Diagnostics/Stacktrace.cs +++ b/src/mscorlib/src/System/Diagnostics/Stacktrace.cs @@ -2,21 +2,22 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -namespace System.Diagnostics { - using System; - using System.Collections; - using System.Text; - using System.Threading; - using System.Security; - using System.IO; - using System.Reflection; - using System.Runtime.InteropServices; - using System.Runtime.CompilerServices; - using System.Globalization; - using System.Runtime.Serialization; - using System.Runtime.Versioning; - using System.Diagnostics.Contracts; - +using System; +using System.Collections; +using System.Text; +using System.Threading; +using System.Security; +using System.IO; +using System.Reflection; +using System.Runtime.InteropServices; +using System.Runtime.CompilerServices; +using System.Globalization; +using System.Runtime.Serialization; +using System.Runtime.Versioning; +using System.Diagnostics.Contracts; + +namespace System.Diagnostics +{ // READ ME: // Modifying the order or fields of this object may require other changes // to the unmanaged definition of the StackFrameHelper class, in @@ -55,7 +56,7 @@ namespace System.Diagnostics { #pragma warning restore 414 private delegate void GetSourceLineInfoDelegate(string assemblyPath, IntPtr loadedPeAddress, int loadedPeSize, - IntPtr inMemoryPdbAddress, int inMemoryPdbSize, int methodToken, int ilOffset, + IntPtr inMemoryPdbAddress, int inMemoryPdbSize, int methodToken, int ilOffset, out string sourceFile, out int sourceLine, out int sourceColumn); private static Type s_symbolsType = null; @@ -170,16 +171,16 @@ namespace System.Diagnostics { } } - public virtual MethodBase GetMethodBase(int i) - { + public virtual MethodBase GetMethodBase(int i) + { // There may be a better way to do this. // we got RuntimeMethodHandles here and we need to go to MethodBase // but we don't know whether the reflection info has been initialized // or not. So we call GetMethods and GetConstructors on the type // and then we fetch the proper MethodBase!! IntPtr mh = rgMethodHandle[i]; - - if (mh.IsNull()) + + if (mh.IsNull()) return null; IRuntimeMethodInfo mhReal = RuntimeMethodHandle.GetTypicalMethodDefinition(new RuntimeMethodInfoStub(mh, this)); @@ -187,32 +188,32 @@ namespace System.Diagnostics { return RuntimeType.GetMethodBase(mhReal); } - public virtual int GetOffset(int i) { return rgiOffset[i];} - public virtual int GetILOffset(int i) { return rgiILOffset[i];} - public virtual String GetFilename(int i) { return rgFilename == null ? null : rgFilename[i];} - public virtual int GetLineNumber(int i) { return rgiLineNumber == null ? 0 : rgiLineNumber[i];} - public virtual int GetColumnNumber(int i) { return rgiColumnNumber == null ? 0 : rgiColumnNumber[i];} + public virtual int GetOffset(int i) { return rgiOffset[i]; } + public virtual int GetILOffset(int i) { return rgiILOffset[i]; } + public virtual String GetFilename(int i) { return rgFilename == null ? null : rgFilename[i]; } + public virtual int GetLineNumber(int i) { return rgiLineNumber == null ? 0 : rgiLineNumber[i]; } + public virtual int GetColumnNumber(int i) { return rgiColumnNumber == null ? 0 : rgiColumnNumber[i]; } - public virtual bool IsLastFrameFromForeignExceptionStackTrace(int i) - { - return (rgiLastFrameFromForeignExceptionStackTrace == null)?false:rgiLastFrameFromForeignExceptionStackTrace[i]; - } + public virtual bool IsLastFrameFromForeignExceptionStackTrace(int i) + { + return (rgiLastFrameFromForeignExceptionStackTrace == null) ? false : rgiLastFrameFromForeignExceptionStackTrace[i]; + } + + public virtual int GetNumberOfFrames() { return iFrameCount; } - public virtual int GetNumberOfFrames() { return iFrameCount;} - // // serialization implementation // [OnSerializing] - void OnSerializing(StreamingContext context) + private void OnSerializing(StreamingContext context) { // this is called in the process of serializing this object. // For compatibility with Everett we need to assign the rgMethodBase field as that is the field // that will be serialized rgMethodBase = (rgMethodHandle == null) ? null : new MethodBase[rgMethodHandle.Length]; - if (rgMethodHandle != null) + if (rgMethodHandle != null) { - for (int i = 0; i < rgMethodHandle.Length; i++) + for (int i = 0; i < rgMethodHandle.Length; i++) { if (!rgMethodHandle[i].IsNull()) rgMethodBase[i] = RuntimeType.GetMethodBase(new RuntimeMethodInfoStub(rgMethodHandle[i], this)); @@ -221,20 +222,20 @@ namespace System.Diagnostics { } [OnSerialized] - void OnSerialized(StreamingContext context) + private void OnSerialized(StreamingContext context) { // after we are done serializing null the rgMethodBase field rgMethodBase = null; } [OnDeserialized] - void OnDeserialized(StreamingContext context) + private void OnDeserialized(StreamingContext context) { // after we are done deserializing we need to transform the rgMethodBase in rgMethodHandle rgMethodHandle = (rgMethodBase == null) ? null : new IntPtr[rgMethodBase.Length]; - if (rgMethodBase != null) + if (rgMethodBase != null) { - for (int i = 0; i < rgMethodBase.Length; i++) + for (int i = 0; i < rgMethodBase.Length; i++) { if (rgMethodBase[i] != null) rgMethodHandle[i] = rgMethodBase[i].MethodHandle.Value; @@ -243,8 +244,8 @@ namespace System.Diagnostics { rgMethodBase = null; } } - - + + // Class which represents a description of a stack trace // There is no good reason for the methods of this class to be virtual. // In order to ensure trusted code can trust the data it gets from a @@ -274,42 +275,40 @@ namespace System.Diagnostics { m_iMethodsToSkip = 0; CaptureStackTrace(METHODS_TO_SKIP, fNeedFileInfo, null, null); } - + // Constructs a stack trace from the current location, in a caller's // frame // public StackTrace(int skipFrames) { - if (skipFrames < 0) - throw new ArgumentOutOfRangeException(nameof(skipFrames), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException(nameof(skipFrames), + SR.ArgumentOutOfRange_NeedNonNegNum); Contract.EndContractBlock(); - + m_iNumOfFrames = 0; m_iMethodsToSkip = 0; - - CaptureStackTrace(skipFrames+METHODS_TO_SKIP, false, null, null); + + CaptureStackTrace(skipFrames + METHODS_TO_SKIP, false, null, null); } - + // Constructs a stack trace from the current location, in a caller's // frame // public StackTrace(int skipFrames, bool fNeedFileInfo) { - if (skipFrames < 0) - throw new ArgumentOutOfRangeException(nameof(skipFrames), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException(nameof(skipFrames), + SR.ArgumentOutOfRange_NeedNonNegNum); Contract.EndContractBlock(); - + m_iNumOfFrames = 0; m_iMethodsToSkip = 0; - - CaptureStackTrace(skipFrames+METHODS_TO_SKIP, fNeedFileInfo, null, null); + + CaptureStackTrace(skipFrames + METHODS_TO_SKIP, fNeedFileInfo, null, null); } - - + + // Constructs a stack trace from the current location. public StackTrace(Exception e) { @@ -334,7 +333,7 @@ namespace System.Diagnostics { m_iMethodsToSkip = 0; CaptureStackTrace(METHODS_TO_SKIP, fNeedFileInfo, null, e); } - + // Constructs a stack trace from the current location, in a caller's // frame // @@ -344,16 +343,16 @@ namespace System.Diagnostics { throw new ArgumentNullException(nameof(e)); if (skipFrames < 0) - throw new ArgumentOutOfRangeException(nameof(skipFrames), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException(nameof(skipFrames), + SR.ArgumentOutOfRange_NeedNonNegNum); Contract.EndContractBlock(); - + m_iNumOfFrames = 0; m_iMethodsToSkip = 0; - - CaptureStackTrace(skipFrames+METHODS_TO_SKIP, false, null, e); + + CaptureStackTrace(skipFrames + METHODS_TO_SKIP, false, null, e); } - + // Constructs a stack trace from the current location, in a caller's // frame // @@ -363,17 +362,17 @@ namespace System.Diagnostics { throw new ArgumentNullException(nameof(e)); if (skipFrames < 0) - throw new ArgumentOutOfRangeException(nameof(skipFrames), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException(nameof(skipFrames), + SR.ArgumentOutOfRange_NeedNonNegNum); Contract.EndContractBlock(); - + m_iNumOfFrames = 0; m_iMethodsToSkip = 0; - - CaptureStackTrace(skipFrames+METHODS_TO_SKIP, fNeedFileInfo, null, e); + + CaptureStackTrace(skipFrames + METHODS_TO_SKIP, fNeedFileInfo, null, e); } - - + + // Constructs a "fake" stack trace, just containing a single frame. // Does not have the overhead of a full stack trace. // @@ -386,26 +385,14 @@ namespace System.Diagnostics { } - // Constructs a stack trace for the given thread - // - [Obsolete("This constructor has been deprecated. Please use a constructor that does not require a Thread parameter. http://go.microsoft.com/fwlink/?linkid=14202")] - public StackTrace(Thread targetThread, bool needFileInfo) - { - m_iNumOfFrames = 0; - m_iMethodsToSkip = 0; - - CaptureStackTrace(METHODS_TO_SKIP, needFileInfo, targetThread, null); - - } - [MethodImplAttribute(MethodImplOptions.InternalCall)] internal static extern void GetStackFramesInternal(StackFrameHelper sfh, int iSkip, bool fNeedFileInfo, Exception e); - + internal static int CalculateFramesToSkip(StackFrameHelper StackF, int iNumFrames) { int iRetVal = 0; String PackageName = "System.Diagnostics"; - + // Check if this method is part of the System.Diagnostics // package. If so, increment counter keeping track of // System.Diagnostics functions @@ -413,22 +400,22 @@ namespace System.Diagnostics { { MethodBase mb = StackF.GetMethodBase(i); if (mb != null) - { + { Type t = mb.DeclaringType; - if (t == null) + if (t == null) break; String ns = t.Namespace; - if (ns == null) + if (ns == null) break; if (String.Compare(ns, PackageName, StringComparison.Ordinal) != 0) break; } iRetVal++; } - + return iRetVal; } - + // Retrieves an object with stack trace information encoded. // It leaves out the first "iSkip" lines of the stacktrace. // @@ -459,7 +446,7 @@ namespace System.Diagnostics { sfTemp.SetOffset(StackF.GetOffset(i)); sfTemp.SetILOffset(StackF.GetILOffset(i)); - sfTemp.SetIsLastFrameFromForeignExceptionStackTrace(StackF.IsLastFrameFromForeignExceptionStackTrace(i)); + sfTemp.SetIsLastFrameFromForeignExceptionStackTrace(StackF.IsLastFrameFromForeignExceptionStackTrace(i)); if (fNeedFileInfo) { @@ -488,23 +475,23 @@ namespace System.Diagnostics { frames = null; } } - + // Property to get the number of frames in the stack trace // public virtual int FrameCount { - get { return m_iNumOfFrames;} + get { return m_iNumOfFrames; } } - - + + // Returns a given stack frame. Stack frames are numbered starting at // zero, which is the last stack frame pushed. // public virtual StackFrame GetFrame(int index) { if ((frames != null) && (index < m_iNumOfFrames) && (index >= 0)) - return frames[index+m_iMethodsToSkip]; - + return frames[index + m_iMethodsToSkip]; + return null; } @@ -513,18 +500,18 @@ namespace System.Diagnostics { // The nth element of this array is the same as GetFrame(n). // The length of the array is the same as FrameCount. // - public virtual StackFrame [] GetFrames() + public virtual StackFrame[] GetFrames() { if (frames == null || m_iNumOfFrames <= 0) return null; - + // We have to return a subset of the array. Unfortunately this // means we have to allocate a new array and copy over. - StackFrame [] array = new StackFrame[m_iNumOfFrames]; + StackFrame[] array = new StackFrame[m_iNumOfFrames]; Array.Copy(frames, m_iMethodsToSkip, array, 0, m_iNumOfFrames); return array; } - + // Builds a readable representation of the stack trace // public override String ToString() @@ -535,13 +522,13 @@ namespace System.Diagnostics { // TraceFormat is Used to specify options for how the // string-representation of a StackTrace should be generated. - internal enum TraceFormat + internal enum TraceFormat { Normal, TrailingNewLine, // include a trailing new line character NoResourceLookup // to prevent infinite resource recusion } - + // Builds a readable representation of the stack trace, specifying // the format for backwards compatibility. internal String ToString(TraceFormat traceFormat) @@ -550,12 +537,12 @@ namespace System.Diagnostics { String word_At = "at"; String inFileLineNum = "in {0}:line {1}"; - if(traceFormat != TraceFormat.NoResourceLookup) + if (traceFormat != TraceFormat.NoResourceLookup) { - word_At = Environment.GetResourceString("Word_At"); - inFileLineNum = Environment.GetResourceString("StackTrace_InFileLineNumber"); + word_At = SR.Word_At; + inFileLineNum = SR.StackTrace_InFileLineNumber; } - + bool fFirstFrame = true; StringBuilder sb = new StringBuilder(255); for (int iFrameIndex = 0; iFrameIndex < m_iNumOfFrames; iFrameIndex++) @@ -569,11 +556,11 @@ namespace System.Diagnostics { fFirstFrame = false; else sb.Append(Environment.NewLine); - + sb.AppendFormat(CultureInfo.InvariantCulture, " {0} ", word_At); Type t = mb.DeclaringType; - // if there is a type (non global method) print it + // if there is a type (non global method) print it if (t != null) { // Append t.FullName, replacing '+' with '.' @@ -592,7 +579,7 @@ namespace System.Diagnostics { { Type[] typars = ((MethodInfo)mb).GetGenericArguments(); sb.Append('['); - int k=0; + int k = 0; bool fFirstTyParam = true; while (k < typars.Length) { @@ -603,8 +590,8 @@ namespace System.Diagnostics { sb.Append(typars[k].Name); k++; - } - sb.Append(']'); + } + sb.Append(']'); } ParameterInfo[] pi = null; @@ -635,7 +622,7 @@ namespace System.Diagnostics { sb.Append(typeName); sb.Append(' '); sb.Append(pi[j].Name); - } + } sb.Append(')'); } @@ -645,7 +632,7 @@ namespace System.Diagnostics { // If we don't have a PDB or PDB-reading is disabled for the module, // then the file name will be null. String fileName = null; - + // Getting the filename from a StackFrame is a privileged operation - we won't want // to disclose full path names to arbitrarily untrusted code. Rather than just omit // this we could probably trim to just the filename so it's still mostly usefull. @@ -660,7 +647,7 @@ namespace System.Diagnostics { displayFilenames = false; } - if (fileName != null) + if (fileName != null) { // tack on " in c:\tmp\MyFile.cs:line 5" sb.Append(' '); @@ -671,15 +658,15 @@ namespace System.Diagnostics { if (sf.GetIsLastFrameFromForeignExceptionStackTrace()) { sb.Append(Environment.NewLine); - sb.Append(Environment.GetResourceString("Exception_EndStackTraceFromPreviousThrow")); + sb.Append(SR.Exception_EndStackTraceFromPreviousThrow); } } } - if(traceFormat == TraceFormat.TrailingNewLine) + if (traceFormat == TraceFormat.TrailingNewLine) sb.Append(Environment.NewLine); - - return sb.ToString(); + + return sb.ToString(); } // This helper is called from within the EE to construct a string representation @@ -693,5 +680,4 @@ namespace System.Diagnostics { return st.ToString(); } } - } |