Used for scoped thread switching. using (var switcher = new ThreadSwitcher(thread)) { // Invoke DbgEng.dll interface function } Use this class for accessing thread information from DbgEng.dll interfaces to insure correct thread information access. For performance reasons, after using scope, previous thread won't be set until it is needed. Always use this class to insure correctness.
Inheritance: IDisposable
Esempio n. 1
0
        /// <summary>
        /// Gets the thread context of the specified thread.
        /// </summary>
        /// <param name="thread">The thread.</param>
        public ThreadContext GetThreadContext(Thread thread)
        {
            using (ThreadSwitcher switcher = new ThreadSwitcher(StateCache, thread))
            using (MarshalArrayReader<ThreadContext> threadContextBuffer = ThreadContext.CreateArrayMarshaler(thread.Process, 1))
            {
                Advanced.GetThreadContext(threadContextBuffer.Pointer, (uint)(threadContextBuffer.Count * threadContextBuffer.Size));

                return threadContextBuffer.Elements.FirstOrDefault();
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Gets the environment block address of the specified thread.
 /// </summary>
 /// <param name="thread">The thread.</param>
 public ulong GetThreadEnvironmentBlockAddress(Thread thread)
 {
     using (ThreadSwitcher switcher = new ThreadSwitcher(StateCache, thread))
     {
         return SystemObjects.GetCurrentThreadTeb();
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Gets the stack trace from the specified context.
        /// </summary>
        /// <param name="thread">The thread.</param>
        /// <param name="contextAddress">The context address.</param>
        /// <param name="contextSize">Size of the context.</param>
        /// <returns></returns>
        private StackTrace GetStackTraceFromContext(Thread thread, IntPtr contextAddress, uint contextSize)
        {
            #if false
            if (thread.Process.DumpFileMemoryReader != null)
            {
                if (contextAddress == IntPtr.Zero)
                {
                    using (ThreadSwitcher switcher = new ThreadSwitcher(StateCache, thread))
                    using (MarshalArrayReader<ThreadContext> threadContextBuffer = ThreadContext.CreateArrayMarshaler(thread.Process, 1))
                    {
                        Advanced.GetThreadContext(threadContextBuffer.Pointer, (uint)(threadContextBuffer.Count * threadContextBuffer.Size));
                        return ReadStackTraceFromContext(thread, threadContextBuffer.Pointer);
                    }
                }
                else
                {
                    return ReadStackTraceFromContext(thread, contextAddress);
                }
            }
            #endif

            const int MaxCallStack = 10240;
            using (ThreadSwitcher switcher = new ThreadSwitcher(StateCache, thread))
            using (MarshalArrayReader<_DEBUG_STACK_FRAME_EX> frameBuffer = new RegularMarshalArrayReader<_DEBUG_STACK_FRAME_EX>(MaxCallStack))
            using (MarshalArrayReader<ThreadContext> threadContextBuffer = ThreadContext.CreateArrayMarshaler(thread.Process, MaxCallStack))
            {
                uint framesCount;

                Control.GetContextStackTraceEx(contextAddress, contextSize, frameBuffer.Pointer, (uint)frameBuffer.Count, threadContextBuffer.Pointer, (uint)(threadContextBuffer.Size * threadContextBuffer.Count), (uint)threadContextBuffer.Size, out framesCount);
                return new StackTrace(thread, frameBuffer.Elements.Take((int)framesCount).ToArray(), threadContextBuffer.Elements.Take((int)framesCount).ToArray());
            }
        }