Seva Alekseyev
Seva Alekseyev

Reputation: 61388

Official way to get the Thread Information/Environment Block (TIB/TEB)

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

Upvotes: 8

Views: 5719

Answers (3)

Seva Alekseyev
Seva Alekseyev

Reputation: 61388

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

Upvotes: 4

Igor Skochinsky
Igor Skochinsky

Reputation: 25318

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);
}

Upvotes: 12

Remy Lebeau
Remy Lebeau

Reputation: 597941

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.

Upvotes: 10

Related Questions