Official way to get the Thread Information/Environ

2019-03-21 11:22发布

In Windows, it's long been common, if undocumented, knowledge that the Thread Information Block (TIB) of the current thread can be found at FS:0. But that only works on Intel CPUs, where the FS register exists in the first place. Now I wanna get to the TIB on an ARM-based Windows system (Windows Phone and maybe Windows RT). Is there an API for that, please?

EDIT: I want to get the thread stack base for crash reporting purposes.

Information about TIB/TEB: http://www.microsoft.com/msj/archive/S2CE.aspx

3条回答
爷的心禁止访问
2楼-- · 2019-03-21 11:26

Igor nailed it. But FYI, in ARM assembly it goes like this:

mrc p15, 0, r12, c13, c0, 2 ; r12 now points at TEB/TIB
ldr r12, [r12, #4] ; r12 now holds stack base
查看更多
做个烂人
3楼-- · 2019-03-21 11:42

To answer your posted question, you can use NtQueryInformationThread() to retrieve a THREAD_BASIC_INFORMATION structure, which contains a pointer to the thread's TIB in its TebBaseAddress member.

查看更多
看我几分像从前
4楼-- · 2019-03-21 11:52

The macro NtCurrentTeb() is available in winnt.h for all supported architectures, including ARM (Windows RT):

#if defined(_M_ARM) && !defined(__midl) && !defined(_M_CEE_PURE)

__forceinline
struct _TEB *
NtCurrentTeb (
    VOID
    )
{
    return (struct _TEB *)(ULONG_PTR)_MoveFromCoprocessor(CP15_TPIDRURW);
}
查看更多
登录 后发表回答