Fastest way to get the TID (Thread Information Blo

2019-02-19 20:26发布

问题:

I have a very compute-intensive module in which I've added stack-tracing to be able to find specific problems. Although the application is allowed to run slower when this stack-tracing is enabled, it must not run 10 times slower. That's why I am not using the StackWalk routines from DBGHELP.DLL, but I walk the stack myself using the frame pointers (so I don't use the Frame Pointer Omission compiler option).

In most cases, getting the call stack works correctly and is very fast, but in some cases, my logic fails because one of the frame pointers points to an address outside the stack (not much, just a little bit).

I know that this is probably an error somewhere, but to be able to make my code safer, I need a way to check whether a frame-pointer points to a memory location within the stack of the current thread or not. The application is 64-bit and runs under Windows.

The code at How to get thread stack information on Windows? probably solves the problem, but since this calls other functions, it probably makes my code run much slower (to be honest, I didn't test it).

I also found some inline assembly code that should perform the trick (http://nasutechtips.blogspot.com/2011/01/thread-information-block-tib-and-fs.html), but inline assembly isn't supported by Microsoft's 64-bit C++ compiler.

Also the intrinsic __readfsqword doesn't seem to work on 64-bit.

Any other suggestions on how to get the TIB on 64-bit as fast as possible?

回答1:

Found it, the following code does the trick:

#include <windows.h>
#include <winnt.h>

struct _TEB
   {
   NT_TIB NtTib;
   // Ignore rest of struct
   };

void *startOfStack;
startOfStack = NtCurrentTeb()->NtTib.StackBase;
std::cout << "startOfStack = " << startOfStack << std::endl;

I looked to the definition of NtCurrentTeb() in winnt.h and it looks like this is a very low level function, so it will probably be fast enough to get what I want.



回答2:

it should be __readgsqword instead of __readfsqword on 64-bit.



标签: windows 64bit