示例#1
0
        //
        // Initializes the stack trace helper. If fNeedFileInfo is true, initializes rgFilename,
        // rgiLineNumber and rgiColumnNumber fields using the portable PDB reader if not already
        // done by GetStackFramesInternal (on Windows for old PDB format).
        //
        internal void InitializeSourceInfo(int iSkip, bool fNeedFileInfo, Exception exception)
        {
            StackTrace.GetStackFramesInternal(this, iSkip, fNeedFileInfo, exception);

            if (!fNeedFileInfo)
            {
                return;
            }

            // Check if this function is being reentered because of an exception in the code below
            if (t_reentrancy > 0)
            {
                return;
            }

            t_reentrancy++;
            try
            {
                if (s_symbolsMethodInfo == null)
                {
                    s_symbolsType = Type.GetType("System.Diagnostics.StackTraceSymbols, System.Diagnostics.StackTrace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", throwOnError: false);
                    if (s_symbolsType == null)
                    {
                        return;
                    }

                    s_symbolsMethodInfo = s_symbolsType.GetMethod("GetSourceLineInfo");
                    if (s_symbolsMethodInfo == null)
                    {
                        return;
                    }
                }

                if (getSourceLineInfo == null)
                {
                    // Create an instance of System.Diagnostics.Stacktrace.Symbols
                    object target = Activator.CreateInstance(s_symbolsType);

                    // Create an instance delegate for the GetSourceLineInfo method
                    getSourceLineInfo = (GetSourceLineInfoDelegate)s_symbolsMethodInfo.CreateDelegate(typeof(GetSourceLineInfoDelegate), target);
                }

                for (int index = 0; index < iFrameCount; index++)
                {
                    // If there was some reason not to try get get the symbols from the portable PDB reader like the module was
                    // ENC or the source/line info was already retrieved, the method token is 0.
                    if (rgiMethodToken[index] != 0)
                    {
                        getSourceLineInfo(rgAssemblyPath[index], rgLoadedPeAddress[index], rgiLoadedPeSize[index],
                                          rgInMemoryPdbAddress[index], rgiInMemoryPdbSize[index], rgiMethodToken[index],
                                          rgiILOffset[index], out rgFilename[index], out rgiLineNumber[index], out rgiColumnNumber[index]);
                    }
                }
            }
            finally
            {
                t_reentrancy--;
            }
        }
示例#2
0
        //
        // Initializes the stack trace helper. If fNeedFileInfo is true, initializes rgFilename,
        // rgiLineNumber and rgiColumnNumber fields using the portable PDB reader if not already
        // done by GetStackFramesInternal (on Windows for old PDB format).
        //

        internal void InitializeSourceInfo(int iSkip, bool fNeedFileInfo, Exception?exception)
        {
            StackTrace.GetStackFramesInternal(this, iSkip, fNeedFileInfo, exception);

            if (!fNeedFileInfo)
            {
                return;
            }

            // Check if this function is being reentered because of an exception in the code below
            if (t_reentrancy > 0)
            {
                return;
            }

            t_reentrancy++;
            try
            {
                if (s_getSourceLineInfo == null)
                {
                    Type?symbolsType = Type.GetType(
                        "System.Diagnostics.StackTraceSymbols, System.Diagnostics.StackTrace, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
                        throwOnError: false);

                    if (symbolsType == null)
                    {
                        return;
                    }

                    Type[] parameterTypes = new Type[]
                    {
                        typeof(Assembly), typeof(string), typeof(IntPtr), typeof(int), typeof(bool), typeof(IntPtr),
                        typeof(int), typeof(int), typeof(int),
                        typeof(string).MakeByRefType(), typeof(int).MakeByRefType(), typeof(int).MakeByRefType()
                    };
                    MethodInfo?symbolsMethodInfo = symbolsType.GetMethod("GetSourceLineInfo", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, parameterTypes, null);
                    if (symbolsMethodInfo == null)
                    {
                        return;
                    }

                    // Create an instance of System.Diagnostics.Stacktrace.Symbols
                    object?target = Activator.CreateInstance(symbolsType);

                    // Create an instance delegate for the GetSourceLineInfo method
                    GetSourceLineInfoDelegate getSourceLineInfo = symbolsMethodInfo.CreateDelegate <GetSourceLineInfoDelegate>(target);

                    // We could race with another thread. It doesn't matter if we win or lose, the losing instance will be GC'ed and all threads including this one will
                    // use the winning instance
                    Interlocked.CompareExchange(ref s_getSourceLineInfo, getSourceLineInfo, null);
                }

                for (int index = 0; index < iFrameCount; index++)
                {
                    // If there was some reason not to try get the symbols from the portable PDB reader like the module was
                    // ENC or the source/line info was already retrieved, the method token is 0.
                    if (rgiMethodToken ![index] != 0)
示例#3
0
        private void CaptureStackTrace(int iSkip, bool fNeedFileInfo, Thread targetThread, Exception e)
        {
            this.m_iMethodsToSkip = this.m_iMethodsToSkip + iSkip;
            StackFrameHelper stackFrameHelper = new StackFrameHelper(fNeedFileInfo, targetThread);

            StackTrace.GetStackFramesInternal(stackFrameHelper, 0, e);
            this.m_iNumOfFrames = stackFrameHelper.GetNumberOfFrames();
            if (this.m_iMethodsToSkip > this.m_iNumOfFrames)
            {
                this.m_iMethodsToSkip = this.m_iNumOfFrames;
            }
            if (this.m_iNumOfFrames != 0)
            {
                this.frames = new StackFrame[this.m_iNumOfFrames];
                for (int i = 0; i < this.m_iNumOfFrames; ++i)
                {
                    StackFrame stackFrame = new StackFrame(true, true);
                    stackFrame.SetMethodBase(stackFrameHelper.GetMethodBase(i));
                    stackFrame.SetOffset(stackFrameHelper.GetOffset(i));
                    stackFrame.SetILOffset(stackFrameHelper.GetILOffset(i));
                    stackFrame.SetIsLastFrameFromForeignExceptionStackTrace(stackFrameHelper.IsLastFrameFromForeignExceptionStackTrace(i));
                    if (fNeedFileInfo)
                    {
                        stackFrame.SetFileName(stackFrameHelper.GetFilename(i));
                        stackFrame.SetLineNumber(stackFrameHelper.GetLineNumber(i));
                        stackFrame.SetColumnNumber(stackFrameHelper.GetColumnNumber(i));
                    }
                    this.frames[i] = stackFrame;
                }
                if (e == null)
                {
                    this.m_iMethodsToSkip = this.m_iMethodsToSkip + StackTrace.CalculateFramesToSkip(stackFrameHelper, this.m_iNumOfFrames);
                }
                this.m_iNumOfFrames = this.m_iNumOfFrames - this.m_iMethodsToSkip;
                if (this.m_iNumOfFrames >= 0)
                {
                    return;
                }
                this.m_iNumOfFrames = 0;
            }
            else
            {
                this.frames = (StackFrame[])null;
            }
        }
示例#4
0
        private void BuildStackFrame(int skipFrames, bool fNeedFileInfo)
        {
            StackFrameHelper sfh = new StackFrameHelper(fNeedFileInfo, null);

            StackTrace.GetStackFramesInternal(sfh, 0, null);
            int numberOfFrames = sfh.GetNumberOfFrames();

            skipFrames += StackTrace.CalculateFramesToSkip(sfh, numberOfFrames);
            if ((numberOfFrames - skipFrames) > 0)
            {
                this.method   = sfh.GetMethodBase(skipFrames);
                this.offset   = sfh.GetOffset(skipFrames);
                this.ILOffset = sfh.GetILOffset(skipFrames);
                if (fNeedFileInfo)
                {
                    this.strFileName   = sfh.GetFilename(skipFrames);
                    this.iLineNumber   = sfh.GetLineNumber(skipFrames);
                    this.iColumnNumber = sfh.GetColumnNumber(skipFrames);
                }
            }
        }
示例#5
0
        private void BuildStackFrame(int skipFrames, bool fNeedFileInfo)
        {
            StackFrameHelper StackF = new StackFrameHelper(fNeedFileInfo, null);

            StackTrace.GetStackFramesInternal(StackF, 0, null);

            int iNumOfFrames = StackF.GetNumberOfFrames();

            skipFrames += StackTrace.CalculateFramesToSkip(StackF, iNumOfFrames);

            if ((iNumOfFrames - skipFrames) > 0)
            {
                method   = StackF.GetMethodBase(skipFrames);
                offset   = StackF.GetOffset(skipFrames);
                ILOffset = StackF.GetILOffset(skipFrames);
                if (fNeedFileInfo)
                {
                    strFileName   = StackF.GetFilename(skipFrames);
                    iLineNumber   = StackF.GetLineNumber(skipFrames);
                    iColumnNumber = StackF.GetColumnNumber(skipFrames);
                }
            }
        }
示例#6
0
        //
        // Initializes the stack trace helper. If fNeedFileInfo is true, initializes rgFilename,
        // rgiLineNumber and rgiColumnNumber fields using the portable PDB reader if not already
        // done by GetStackFramesInternal (on Windows for old PDB format).
        //
        internal void InitializeSourceInfo(int iSkip, bool fNeedFileInfo, Exception exception)
        {
            StackTrace.GetStackFramesInternal(this, iSkip, fNeedFileInfo, exception);

            if (!fNeedFileInfo)
            {
                return;
            }

            // Check if this function is being reentered because of an exception in the code below
            if (t_reentrancy > 0)
            {
                return;
            }

            t_reentrancy++;
            try
            {
                if (s_getSourceLineInfo == null)
                {
                    Type symbolsType = Type.GetType(
                        "System.Diagnostics.StackTraceSymbols, System.Diagnostics.StackTrace, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
                        throwOnError: false);

                    if (symbolsType == null)
                    {
                        return;
                    }

                    MethodInfo symbolsMethodInfo = symbolsType.GetMethod("GetSourceLineInfo", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                    if (symbolsMethodInfo == null)
                    {
                        return;
                    }

                    // Create an instance of System.Diagnostics.Stacktrace.Symbols
                    object target = Activator.CreateInstance(symbolsType);

                    // Create an instance delegate for the GetSourceLineInfo method
                    GetSourceLineInfoDelegate getSourceLineInfo = (GetSourceLineInfoDelegate)symbolsMethodInfo.CreateDelegate(typeof(GetSourceLineInfoDelegate), target);

                    // We could race with another thread. It doesn't matter if we win or lose, the losing instance will be GC'ed and all threads including this one will
                    // use the winning instance
                    Interlocked.CompareExchange(ref s_getSourceLineInfo, getSourceLineInfo, null);
                }

                for (int index = 0; index < iFrameCount; index++)
                {
                    // If there was some reason not to try get the symbols from the portable PDB reader like the module was
                    // ENC or the source/line info was already retrieved, the method token is 0.
                    if (rgiMethodToken[index] != 0)
                    {
                        s_getSourceLineInfo(rgAssemblyPath[index], rgLoadedPeAddress[index], rgiLoadedPeSize[index],
                                            rgInMemoryPdbAddress[index], rgiInMemoryPdbSize[index], rgiMethodToken[index],
                                            rgiILOffset[index], out rgFilename[index], out rgiLineNumber[index], out rgiColumnNumber[index]);
                    }
                }
            }
            catch
            {
            }
            finally
            {
                t_reentrancy--;
            }
        }
示例#7
0
        internal void InitializeSourceInfo(int iSkip, bool fNeedFileInfo, Exception exception)
        {
            StackTrace.GetStackFramesInternal(this, iSkip, fNeedFileInfo, exception);
            if (!fNeedFileInfo)
            {
                return;
            }

            // For back compat we opt-out of using Portable PDBs before 4.7.2. See the comments in
            // RuntimeFeature for more details.
            //
            // Even if our compat policy for enabling the feature changes, make sure that
            // RuntimeFeature.IsSupported accurately encapsulates that policy. Our API contract with
            // tools is that we will accurately tell them whether or not Portable PDB is supported.
            if (!RuntimeFeature.IsSupported(RuntimeFeature.PortablePdb))
            {
                return;
            }

            // Check if this function is being reentered because of an exception in the code below
            if (t_reentrancy > 0)
            {
                return;
            }

            t_reentrancy++;
            try
            {
                // need private reflection below + unmanaged code for the portable PDB access itself
                // PERF: these demands are somewhat expensive so do the quick check first. We are aiming for
                // ~50k traces/s at 5 frames/trace on decent 2017 era hardware to maintain rough performance
                // parity with 4.7 implementation that didn't have Portable PDB support
                if (!CodeAccessSecurityEngine.QuickCheckForAllDemands())
                {
                    new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Assert();
                    new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert();
                }

                if (s_getSourceLineInfo == null)
                {
                    Type symbolsType = Type.GetType(
                        "System.Diagnostics.StackTraceSymbols, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
                        throwOnError: false);

                    if (symbolsType == null)
                    {
                        return;
                    }

                    MethodInfo symbolsMethodInfo = symbolsType.GetMethod("GetSourceLineInfoWithoutCasAssert",
                                                                         new Type[] { typeof(string),
                                                                                      typeof(IntPtr),
                                                                                      typeof(int),
                                                                                      typeof(IntPtr),
                                                                                      typeof(int),
                                                                                      typeof(int),
                                                                                      typeof(int),
                                                                                      typeof(string).MakeByRefType(),
                                                                                      typeof(int).MakeByRefType(),
                                                                                      typeof(int).MakeByRefType() });

                    // We can't take a servicing dependency that System.Core.dll has been upgraded. If for whatever
                    // wacky reason we still have the old version of System.Core.dll fallback to the original less
                    // performant implementation of the method.
                    if (symbolsMethodInfo == null)
                    {
                        symbolsMethodInfo = symbolsType.GetMethod("GetSourceLineInfo",
                                                                  new Type[] { typeof(string),
                                                                               typeof(IntPtr),
                                                                               typeof(int),
                                                                               typeof(IntPtr),
                                                                               typeof(int),
                                                                               typeof(int),
                                                                               typeof(int),
                                                                               typeof(string).MakeByRefType(),
                                                                               typeof(int).MakeByRefType(),
                                                                               typeof(int).MakeByRefType() });
                    }

                    if (symbolsMethodInfo == null)
                    {
                        return;
                    }

                    // Create an instance of System.Diagnostics.Stacktrace.Symbols
                    object target = Activator.CreateInstance(symbolsType);

                    // Create an instance delegate for the GetSourceLineInfo method
                    GetSourceLineInfoDelegate getSourceLineInfo = (GetSourceLineInfoDelegate)symbolsMethodInfo.CreateDelegate(typeof(GetSourceLineInfoDelegate), target);

                    // We could ---- with another thread. It doesn't matter if we win or lose, the losing instance will be GC'ed and all threads including this one will
                    // use the winning instance
                    Interlocked.CompareExchange(ref s_getSourceLineInfo, getSourceLineInfo, null);
                }

                for (int index = 0; index < iFrameCount; index++)
                {
                    // If there was some reason not to try get get the symbols from the portable PDB reader like the module was
                    // ENC or the source/line info was already retrieved, the method token is 0.
                    if (rgiMethodToken[index] != 0)
                    {
                        s_getSourceLineInfo(rgAssemblyPath[index], rgLoadedPeAddress[index], rgiLoadedPeSize[index],
                                            rgInMemoryPdbAddress[index], rgiInMemoryPdbSize[index], rgiMethodToken[index],
                                            rgiILOffset[index], out rgFilename[index], out rgiLineNumber[index], out rgiColumnNumber[index]);
                    }
                }
            }
            catch
            {
            }
            finally
            {
                t_reentrancy--;
            }
        }
示例#8
0
 internal void InitializeSourceInfo(int iSkip, bool fNeedFileInfo, Exception exception)
 {
     StackTrace.GetStackFramesInternal(this, iSkip, fNeedFileInfo, exception);
     if (!fNeedFileInfo)
     {
         return;
     }
     if (!RuntimeFeature.IsSupported("PortablePdb"))
     {
         return;
     }
     if (StackFrameHelper.t_reentrancy > 0)
     {
         return;
     }
     StackFrameHelper.t_reentrancy++;
     try
     {
         if (!CodeAccessSecurityEngine.QuickCheckForAllDemands())
         {
             new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Assert();
             new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert();
         }
         if (StackFrameHelper.s_getSourceLineInfo == null)
         {
             Type type = Type.GetType("System.Diagnostics.StackTraceSymbols, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", false);
             if (type == null)
             {
                 return;
             }
             MethodInfo method = type.GetMethod("GetSourceLineInfoWithoutCasAssert", new Type[]
             {
                 typeof(string),
                 typeof(IntPtr),
                 typeof(int),
                 typeof(IntPtr),
                 typeof(int),
                 typeof(int),
                 typeof(int),
                 typeof(string).MakeByRefType(),
                 typeof(int).MakeByRefType(),
                 typeof(int).MakeByRefType()
             });
             if (method == null)
             {
                 method = type.GetMethod("GetSourceLineInfo", new Type[]
                 {
                     typeof(string),
                     typeof(IntPtr),
                     typeof(int),
                     typeof(IntPtr),
                     typeof(int),
                     typeof(int),
                     typeof(int),
                     typeof(string).MakeByRefType(),
                     typeof(int).MakeByRefType(),
                     typeof(int).MakeByRefType()
                 });
             }
             if (method == null)
             {
                 return;
             }
             object target = Activator.CreateInstance(type);
             StackFrameHelper.GetSourceLineInfoDelegate value = (StackFrameHelper.GetSourceLineInfoDelegate)method.CreateDelegate(typeof(StackFrameHelper.GetSourceLineInfoDelegate), target);
             Interlocked.CompareExchange <StackFrameHelper.GetSourceLineInfoDelegate>(ref StackFrameHelper.s_getSourceLineInfo, value, null);
         }
         for (int i = 0; i < this.iFrameCount; i++)
         {
             if (this.rgiMethodToken[i] != 0)
             {
                 StackFrameHelper.s_getSourceLineInfo(this.rgAssemblyPath[i], this.rgLoadedPeAddress[i], this.rgiLoadedPeSize[i], this.rgInMemoryPdbAddress[i], this.rgiInMemoryPdbSize[i], this.rgiMethodToken[i], this.rgiILOffset[i], out this.rgFilename[i], out this.rgiLineNumber[i], out this.rgiColumnNumber[i]);
             }
         }
     }
     catch
     {
     }
     finally
     {
         StackFrameHelper.t_reentrancy--;
     }
 }