Index: ChangeLog from Ranjit Mathew * stacktrace.cc (_Jv_StackTrace::GetStackTrace): Pass UnwindTraceFn() to fallback_backtrace(). (_Jv_StackTrace::GetCallerInfo): Enable even for targets using SJLJ EH and use fallback_backtrace() in that case. (_Jv_StackTrace::GetClassContext): Pass UnwindTraceFn() to fallback_backtrace(). (_Jv_StackTrace::GetFirstNonSystemClassLoader): Likewise. * sysdep/generic/backtrace.h (fallback_backtrace): Accept an additional unwind trace function argument. * sysdep/i386/backtrace.h (HAVE_FALLBACK_BACKTRACE): Do not define. (_Unwind_GetIPInfo): Define macro. (_Unwind_GetRegionStart): Likewise. (fallback_backtrace): Accept additional unwind trace function argument. Call it during unwinding. Stop when any of _Jv_RunMain(), _Jv_ThreadStart() or main() is seen during unwinding. Index: stacktrace.cc =================================================================== --- stacktrace.cc (revision 115260) +++ stacktrace.cc (working copy) @@ -156,7 +156,7 @@ _Jv_StackTrace::GetStackTrace(void) #ifdef SJLJ_EXCEPTIONS // The Unwind interface doesn't work with the SJLJ exception model. // Fall back to a platform-specific unwinder. - fallback_backtrace (&state); + fallback_backtrace (UnwindTraceFn, &state); #else /* SJLJ_EXCEPTIONS */ _Unwind_Backtrace (UnwindTraceFn, &state); #endif /* SJLJ_EXCEPTIONS */ @@ -421,7 +421,6 @@ void _Jv_StackTrace::GetCallerInfo (jclass checkClass, jclass *caller_class, _Jv_Method **caller_meth) { -#ifndef SJLJ_EXCEPTIONS int trace_size = 20; _Jv_StackFrame frames[trace_size]; _Jv_UnwindState state (trace_size); @@ -439,15 +438,18 @@ _Jv_StackTrace::GetCallerInfo (jclass ch //JvSynchronized (ncodeMap); UpdateNCodeMap (); +#ifdef SJLJ_EXCEPTIONS + // The Unwind interface doesn't work with the SJLJ exception model. + // Fall back to a platform-specific unwinder. + fallback_backtrace (UnwindTraceFn, &state); +#else /* SJLJ_EXCEPTIONS */ _Unwind_Backtrace (UnwindTraceFn, &state); +#endif /* SJLJ_EXCEPTIONS */ if (caller_class) *caller_class = trace_data.foundClass; if (caller_meth) *caller_meth = trace_data.foundMeth; -#else - return; -#endif } // Return a java array containing the Java classes on the stack above CHECKCLASS. @@ -467,7 +469,7 @@ _Jv_StackTrace::GetClassContext (jclass #ifdef SJLJ_EXCEPTIONS // The Unwind interface doesn't work with the SJLJ exception model. // Fall back to a platform-specific unwinder. - fallback_backtrace (&state); + fallback_backtrace (UnwindTraceFn, &state); #else /* SJLJ_EXCEPTIONS */ _Unwind_Backtrace (UnwindTraceFn, &state); #endif /* SJLJ_EXCEPTIONS */ @@ -544,7 +546,7 @@ _Jv_StackTrace::GetFirstNonSystemClassLo #ifdef SJLJ_EXCEPTIONS // The Unwind interface doesn't work with the SJLJ exception model. // Fall back to a platform-specific unwinder. - fallback_backtrace (&state); + fallback_backtrace (UnwindTraceFn, &state); #else /* SJLJ_EXCEPTIONS */ _Unwind_Backtrace (UnwindTraceFn, &state); #endif /* SJLJ_EXCEPTIONS */ Index: sysdep/generic/backtrace.h =================================================================== --- sysdep/generic/backtrace.h (revision 115260) +++ sysdep/generic/backtrace.h (working copy) @@ -16,7 +16,7 @@ details. */ /* Store return addresses of the current program stack in STATE and return the exact number of values stored. */ void -fallback_backtrace (_Jv_UnwindState *) +fallback_backtrace (_Unwind_Trace_Fn, _Jv_UnwindState *) { } #endif Index: sysdep/i386/backtrace.h =================================================================== --- sysdep/i386/backtrace.h (revision 115261) +++ sysdep/i386/backtrace.h (working copy) @@ -13,21 +13,39 @@ details. */ #include -#define HAVE_FALLBACK_BACKTRACE +extern int main (int, char **); + +/* The context used to keep track of our position while unwinding through + the call stack. */ +struct _Unwind_Context +{ + /* The starting address of the method. */ + _Jv_uintptr_t meth_addr; + + /* The return address in the method. */ + _Jv_uintptr_t ret_addr; +}; + +#undef _Unwind_GetIPInfo +#define _Unwind_GetIPInfo(ctx,ip_before_insn) \ + (*(ip_before_insn) = 1, (ctx)->ret_addr) + +#undef _Unwind_GetRegionStart +#define _Unwind_GetRegionStart(ctx) \ + ((ctx)->meth_addr) + /* Store return addresses of the current program stack in STATE and return the exact number of values stored. */ void -fallback_backtrace (_Jv_UnwindState *state) +fallback_backtrace (_Unwind_Trace_Fn trace_fn, _Jv_UnwindState *state) { register _Jv_uintptr_t *_ebp __asm__ ("ebp"); register _Jv_uintptr_t _esp __asm__ ("esp"); _Jv_uintptr_t rfp; + _Unwind_Context ctx; - int i = state->pos; - for (rfp = *_ebp; - rfp && i < state->length; - rfp = *(_Jv_uintptr_t *)rfp) + for (rfp = *_ebp; rfp; rfp = *(_Jv_uintptr_t *)rfp) { /* Sanity checks to eliminate dubious-looking frame pointer chains. The frame pointer should be a 32-bit word-aligned stack address. @@ -42,12 +60,7 @@ fallback_backtrace (_Jv_UnwindState *sta /* Get the return address in the calling function. This is stored on the stack just before the value of the old frame pointer. */ - _Jv_uintptr_t ret_addr - = *(_Jv_uintptr_t *)(rfp + sizeof (_Jv_uintptr_t)); - - state->frames[i].type = frame_native; - state->frames[i].ip = (void *)(ret_addr - 1); - state->frames[i].start_ip = NULL; + ctx.ret_addr = *(_Jv_uintptr_t *)(rfp + sizeof (_Jv_uintptr_t)); /* Try to locate a "pushl %ebp; movl %esp, %ebp" function prologue by scanning backwards at even addresses below the return address. @@ -56,7 +69,8 @@ fallback_backtrace (_Jv_UnwindState *sta FIXME: This is not robust and will probably give us false positives, but this is about the best we can do if we do not have DWARF-2 unwind information based exception handling. */ - _Jv_uintptr_t scan_addr = (ret_addr & 0xFFFFFFFE) - 2; + ctx.meth_addr = (_Jv_uintptr_t)NULL; + _Jv_uintptr_t scan_addr = (ctx.ret_addr & 0xFFFFFFFE) - 2; _Jv_uintptr_t limit_addr = (scan_addr > 1024 * 1024) ? (scan_addr - 1024 * 1024) : 2; for ( ; scan_addr >= limit_addr; scan_addr -= 2) @@ -65,17 +79,24 @@ fallback_backtrace (_Jv_UnwindState *sta if (scan_bytes[0] == 0x55 && scan_bytes[1] == 0x89 && scan_bytes[2] == 0xE5) { - state->frames[i].start_ip = (void *)scan_addr; + ctx.meth_addr = scan_addr; break; } } - /* No need to unwind beyond JvRunMain(). */ - if (state->frames[i].start_ip == (void *)JvRunMain) + /* Now call the unwinder callback function. */ + if (trace_fn != NULL) + (*trace_fn) (&ctx, state); + + /* No need to unwind beyond _Jv_RunMain(), _Jv_ThreadStart or + main(). */ + void *jv_runmain + = (void *)(void (*)(JvVMInitArgs *, jclass, const char *, int, + const char **, bool))_Jv_RunMain; + if (ctx.meth_addr == (_Jv_uintptr_t)jv_runmain + || ctx.meth_addr == (_Jv_uintptr_t)_Jv_ThreadStart + || (ctx.meth_addr - (_Jv_uintptr_t)main) < 16) break; - - i++; } - state->pos = i; } #endif