michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "prlog.h" michael@0: #include "prthread.h" michael@0: #include "private/pprthred.h" michael@0: #include "primpl.h" michael@0: michael@0: PR_IMPLEMENT(PRWord *) michael@0: PR_GetGCRegisters(PRThread *t, int isCurrent, int *np) michael@0: { michael@0: return _MD_HomeGCRegisters(t, isCurrent, np); michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRStatus) michael@0: PR_ThreadScanStackPointers(PRThread* t, michael@0: PRScanStackFun scanFun, void* scanClosure) michael@0: { michael@0: PRThread* current = PR_GetCurrentThread(); michael@0: PRWord *sp, *esp, *p0; michael@0: int n; michael@0: void **ptd; michael@0: PRStatus status; michael@0: PRUint32 index; michael@0: int stack_end; michael@0: michael@0: /* michael@0: ** Store the thread's registers in the thread structure so the GC michael@0: ** can scan them. Then scan them. michael@0: */ michael@0: p0 = _MD_HomeGCRegisters(t, t == current, &n); michael@0: status = scanFun(t, (void**)p0, n, scanClosure); michael@0: if (status != PR_SUCCESS) michael@0: return status; michael@0: michael@0: /* Scan the C stack for pointers into the GC heap */ michael@0: #if defined(XP_PC) && defined(WIN16) michael@0: /* michael@0: ** Under WIN16, the stack of the current thread is always mapped into michael@0: ** the "task stack" (at SS:xxxx). So, if t is the current thread, scan michael@0: ** the "task stack". Otherwise, scan the "cached stack" of the inactive michael@0: ** thread... michael@0: */ michael@0: if (t == current) { michael@0: sp = (PRWord*) &stack_end; michael@0: esp = (PRWord*) _pr_top_of_task_stack; michael@0: michael@0: PR_ASSERT(sp <= esp); michael@0: } else { michael@0: sp = (PRWord*) PR_GetSP(t); michael@0: esp = (PRWord*) t->stack->stackTop; michael@0: michael@0: PR_ASSERT((t->stack->stackSize == 0) || michael@0: ((sp > (PRWord*)t->stack->stackBottom) && michael@0: (sp <= (PRWord*)t->stack->stackTop))); michael@0: } michael@0: #else /* ! WIN16 */ michael@0: #ifdef HAVE_STACK_GROWING_UP michael@0: if (t == current) { michael@0: esp = (PRWord*) &stack_end; michael@0: } else { michael@0: esp = (PRWord*) PR_GetSP(t); michael@0: } michael@0: sp = (PRWord*) t->stack->stackTop; michael@0: if (t->stack->stackSize) { michael@0: PR_ASSERT((esp > (PRWord*)t->stack->stackTop) && michael@0: (esp < (PRWord*)t->stack->stackBottom)); michael@0: } michael@0: #else /* ! HAVE_STACK_GROWING_UP */ michael@0: if (t == current) { michael@0: sp = (PRWord*) &stack_end; michael@0: } else { michael@0: sp = (PRWord*) PR_GetSP(t); michael@0: } michael@0: esp = (PRWord*) t->stack->stackTop; michael@0: if (t->stack->stackSize) { michael@0: PR_ASSERT((sp > (PRWord*)t->stack->stackBottom) && michael@0: (sp < (PRWord*)t->stack->stackTop)); michael@0: } michael@0: #endif /* ! HAVE_STACK_GROWING_UP */ michael@0: #endif /* ! WIN16 */ michael@0: michael@0: #if defined(WIN16) michael@0: { michael@0: prword_t scan; michael@0: prword_t limit; michael@0: michael@0: scan = (prword_t) sp; michael@0: limit = (prword_t) esp; michael@0: while (scan < limit) { michael@0: prword_t *test; michael@0: michael@0: test = *((prword_t **)scan); michael@0: status = scanFun(t, (void**)&test, 1, scanClosure); michael@0: if (status != PR_SUCCESS) michael@0: return status; michael@0: scan += sizeof(char); michael@0: } michael@0: } michael@0: #else michael@0: if (sp < esp) { michael@0: status = scanFun(t, (void**)sp, esp - sp, scanClosure); michael@0: if (status != PR_SUCCESS) michael@0: return status; michael@0: } michael@0: #endif michael@0: michael@0: /* michael@0: ** Mark all of the per-thread-data items attached to this thread michael@0: ** michael@0: ** The execution environment better be accounted for otherwise it michael@0: ** will be collected michael@0: */ michael@0: status = scanFun(t, (void**)&t->environment, 1, scanClosure); michael@0: if (status != PR_SUCCESS) michael@0: return status; michael@0: michael@0: /* if thread is not allocated on stack, this is redundant. */ michael@0: ptd = t->privateData; michael@0: for (index = 0; index < t->tpdLength; index++, ptd++) { michael@0: status = scanFun(t, (void**)ptd, 1, scanClosure); michael@0: if (status != PR_SUCCESS) michael@0: return status; michael@0: } michael@0: michael@0: return PR_SUCCESS; michael@0: } michael@0: michael@0: /* transducer for PR_EnumerateThreads */ michael@0: typedef struct PRScanStackData { michael@0: PRScanStackFun scanFun; michael@0: void* scanClosure; michael@0: } PRScanStackData; michael@0: michael@0: static PRStatus PR_CALLBACK michael@0: pr_ScanStack(PRThread* t, int i, void* arg) michael@0: { michael@0: PRScanStackData* data = (PRScanStackData*)arg; michael@0: return PR_ThreadScanStackPointers(t, data->scanFun, data->scanClosure); michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRStatus) michael@0: PR_ScanStackPointers(PRScanStackFun scanFun, void* scanClosure) michael@0: { michael@0: PRScanStackData data; michael@0: data.scanFun = scanFun; michael@0: data.scanClosure = scanClosure; michael@0: return PR_EnumerateThreads(pr_ScanStack, &data); michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRUword) michael@0: PR_GetStackSpaceLeft(PRThread* t) michael@0: { michael@0: PRThread *current = PR_GetCurrentThread(); michael@0: PRWord *sp, *esp; michael@0: int stack_end; michael@0: michael@0: #if defined(WIN16) michael@0: /* michael@0: ** Under WIN16, the stack of the current thread is always mapped into michael@0: ** the "task stack" (at SS:xxxx). So, if t is the current thread, scan michael@0: ** the "task stack". Otherwise, scan the "cached stack" of the inactive michael@0: ** thread... michael@0: */ michael@0: if (t == current) { michael@0: sp = (PRWord*) &stack_end; michael@0: esp = (PRWord*) _pr_top_of_task_stack; michael@0: michael@0: PR_ASSERT(sp <= esp); michael@0: } else { michael@0: sp = (PRWord*) PR_GetSP(t); michael@0: esp = (PRWord*) t->stack->stackTop; michael@0: michael@0: PR_ASSERT((t->stack->stackSize == 0) || michael@0: ((sp > (PRWord*)t->stack->stackBottom) && michael@0: (sp <= (PRWord*)t->stack->stackTop))); michael@0: } michael@0: #else /* ! WIN16 */ michael@0: #ifdef HAVE_STACK_GROWING_UP michael@0: if (t == current) { michael@0: esp = (PRWord*) &stack_end; michael@0: } else { michael@0: esp = (PRWord*) PR_GetSP(t); michael@0: } michael@0: sp = (PRWord*) t->stack->stackTop; michael@0: if (t->stack->stackSize) { michael@0: PR_ASSERT((esp > (PRWord*)t->stack->stackTop) && michael@0: (esp < (PRWord*)t->stack->stackBottom)); michael@0: } michael@0: #else /* ! HAVE_STACK_GROWING_UP */ michael@0: if (t == current) { michael@0: sp = (PRWord*) &stack_end; michael@0: } else { michael@0: sp = (PRWord*) PR_GetSP(t); michael@0: } michael@0: esp = (PRWord*) t->stack->stackTop; michael@0: if (t->stack->stackSize) { michael@0: PR_ASSERT((sp > (PRWord*)t->stack->stackBottom) && michael@0: (sp < (PRWord*)t->stack->stackTop)); michael@0: } michael@0: #endif /* ! HAVE_STACK_GROWING_UP */ michael@0: #endif /* ! WIN16 */ michael@0: return (PRUword)t->stack->stackSize - ((PRWord)esp - (PRWord)sp); michael@0: }