Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "PlatformMacros.h"
8 :
9 : #if defined(GP_OS_darwin)
10 : #include <mach/task.h>
11 : #include <mach/thread_act.h>
12 : #include <pthread.h>
13 : #elif defined(GP_OS_windows)
14 : #include <windows.h>
15 : #endif
16 :
17 : #include "StackTop.h"
18 :
19 72 : void *GetStackTop(void *guess) {
20 : #if defined(GP_OS_darwin)
21 : pthread_t thread = pthread_self();
22 : return pthread_get_stackaddr_np(thread);
23 : #elif defined(GP_OS_windows)
24 : #if defined(_MSC_VER) && defined(GP_ARCH_x86)
25 : // offset 0x18 from the FS segment register gives a pointer to
26 : // the thread information block for the current thread
27 : NT_TIB* pTib;
28 : __asm {
29 : MOV EAX, FS:[18h]
30 : MOV pTib, EAX
31 : }
32 : return static_cast<void*>(pTib->StackBase);
33 : #elif defined(__GNUC__) && defined(GP_ARCH_x86)
34 : // offset 0x18 from the FS segment register gives a pointer to
35 : // the thread information block for the current thread
36 : NT_TIB* pTib;
37 : asm ( "movl %%fs:0x18, %0\n"
38 : : "=r" (pTib)
39 : );
40 : return static_cast<void*>(pTib->StackBase);
41 : #elif defined(GP_ARCH_amd64)
42 : PNT_TIB64 pTib = reinterpret_cast<PNT_TIB64>(NtCurrentTeb());
43 : return reinterpret_cast<void*>(pTib->StackBase);
44 : #else
45 : #error Need a way to get the stack bounds on this platform (Windows)
46 : #endif
47 : #else
48 72 : return guess;
49 : #endif
50 : }
|