This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Re: [MinGW] RFA: Make Stack Traces Work for Interpreted Code on Windows
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Bryce McKinlay wrote:
> Ranjit Mathew wrote:
>>> I think you could just overload them with #defines in backtrace.h?
>>> Failing that, I think that adding a couple of #ifdef's in UnwindTraceFn
>>> is not as bad as the code duplication.
>>>
>> That worked pretty nicely, thank you. It reduces unnecessary
>> duplication of code too. The attached patch implements it and
>> was tested using a crossed-native compiler for i686-pc-mingw32.
>> I still get usable stack traces for compiled and interpreted
>> code.
>>
>> OK?
>
> It occurs to me that you could now actually #define fallback_backtrace
> as _Unwind_Backtrace, and get rid of some #ifdefs in stacktrace.cc. Do
> you think that is worth doing? Otherwise, this is OK for trunk.
I've applied the attached patch to trunk. It defines _Unwind_Backtrace()
as fallback_backtrace() if SJLJ EH is in use. It was tested on
i686-pc-mingw32.
Even after this patch everything is not OK with the interpreter
and unwinding. While I get proper stack traces for programmes
that throw uncaught exceptions, I get a crashed interpreter for
programmes where the exception is caught. Some of the frames just
before _Jv_InterpMethod::run() have wild return addresses (even within
GDB) and I do not immediately understand why. I'll investigate this
further.
- -------------------------------- 8< --------------------------------
#13 0x0043bcd0 in _Jv_InterpMethod::run (retp=0x22fd80, args=0x22fda0,
meth=0x2cd6f60) at /extra/src/gcc/gcc/libjava/interpret.cc:1221
#14 0xf8836606 in ?? ()
#15 0x0022fd80 in ?? ()
#16 0x0022fda0 in ?? ()
#17 0x02cd6f60 in ?? ()
#18 0x02cd6f60 in ?? ()
#19 0x02a1f098 in ?? ()
#20 0x02a139d0 in ?? ()
#21 0x0022fd98 in ?? ()
#22 0x00567e02 in _ZN4java4lang7reflect8Modifier8isPublicEJbi (mod=44138424)
at /extra/src/gcc/gcc/libjava/java/lang/reflect/Modifier.java:257
#23 0x005e2be8 in gnu::java::lang::MainThread::call_main (this=0x2cc0fa0)
at /extra/src/gcc/gcc/libjava/gnu/java/lang/natMainThread.cc:50
#24 0x0042fdff in _ZN3gnu4java4lang10MainThread3runEJvv (this=0x2cc0fa0)
at /extra/src/gcc/gcc/libjava/gnu/java/lang/MainThread.java:108
#25 0x004550f4 in _Jv_ThreadRun (thread=0x2cc0fa0)
at /extra/src/gcc/gcc/libjava/java/lang/natThread.cc:302
#26 0x00405136 in _Jv_RunMain (vm_args=0x22ff48, klass=0x0,
name=0x3f2488 "Ex12", argc=1, argv=0x3f2504, is_jar=false)
at /extra/src/gcc/gcc/libjava/prims.cc:1542
#27 0x004015e8 in main (argc=12, argv=0x7801392e)
at /extra/src/gcc/gcc/libjava/gij.cc:333
- -------------------------------- 8< --------------------------------
Thanks,
Ranjit.
- --
Ranjit Mathew Email: rmathew AT gmail DOT com
Bangalore, INDIA. Web: http://rmathew.com/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFEt9O4Yb1hx2wRS48RAgyMAJ92tLmo2x3Q3t6jDGrLAEZriwGD5wCfTik8
4tPj7wleovKU8ZLejmbWOpU=
=BJrv
-----END PGP SIGNATURE-----
Index: ChangeLog
from Ranjit Mathew <rmathew@gcc.gnu.org>
* stacktrace.cc (_Jv_StackTrace::GetStackTrace): Unconditionally use
_Unwind_Backtrace().
(_Jv_StackTrace::GetCallerInfo): Enable even for targets using SJLJ
EH.
(_Jv_StackTrace::GetClassContext): Unconditionally use
_Unwind_Backtrace().
(_Jv_StackTrace::GetFirstNonSystemClassLoader): Likewise.
* sysdep/i386/backtrace.h (HAVE_FALLBACK_BACKTRACE): Do not define.
(_Unwind_GetIPInfo): Define macro if SJLJ EH is in use.
(_Unwind_GetRegionStart): Likewise.
(_Unwind_Backtrace): 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.
* sysdep/generic/backtrace.h (fallback_backtrace): Accept an
additional unwind trace function argument.
Index: stacktrace.cc
===================================================================
--- stacktrace.cc (revision 115260)
+++ stacktrace.cc (working copy)
@@ -153,13 +153,7 @@ _Jv_StackTrace::GetStackTrace(void)
_Jv_UnwindState state (trace_size);
state.frames = (_Jv_StackFrame *) &frames;
-#ifdef SJLJ_EXCEPTIONS
- // The Unwind interface doesn't work with the SJLJ exception model.
- // Fall back to a platform-specific unwinder.
- fallback_backtrace (&state);
-#else /* SJLJ_EXCEPTIONS */
_Unwind_Backtrace (UnwindTraceFn, &state);
-#endif /* SJLJ_EXCEPTIONS */
// Copy the trace and return it.
int traceSize = sizeof (_Jv_StackTrace) +
@@ -421,7 +415,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);
@@ -445,9 +438,6 @@ _Jv_StackTrace::GetCallerInfo (jclass ch
*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.
@@ -464,13 +454,7 @@ _Jv_StackTrace::GetClassContext (jclass
//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 (&state);
-#else /* SJLJ_EXCEPTIONS */
_Unwind_Backtrace (UnwindTraceFn, &state);
-#endif /* SJLJ_EXCEPTIONS */
// Count the number of Java frames on the stack.
int jframe_count = 0;
@@ -541,13 +525,7 @@ _Jv_StackTrace::GetFirstNonSystemClassLo
//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 (&state);
-#else /* SJLJ_EXCEPTIONS */
_Unwind_Backtrace (UnwindTraceFn, &state);
-#endif /* SJLJ_EXCEPTIONS */
if (state.trace_data)
return (ClassLoader *) state.trace_data;
Index: sysdep/i386/backtrace.h
===================================================================
--- sysdep/i386/backtrace.h (revision 115261)
+++ sysdep/i386/backtrace.h (working copy)
@@ -13,21 +13,46 @@ details. */
#include <java-stack.h>
-#define HAVE_FALLBACK_BACKTRACE
+extern int main (int, char **);
-/* Store return addresses of the current program stack in
- STATE and return the exact number of values stored. */
-void
-fallback_backtrace (_Jv_UnwindState *state)
+/* 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;
+};
+
+#ifdef SJLJ_EXCEPTIONS
+
+#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)
+
+#undef _Unwind_Backtrace
+#define _Unwind_Backtrace(trace_fn,state_ptr) \
+ (fallback_backtrace (trace_fn, state_ptr))
+
+#endif /* SJLJ_EXCEPTIONS */
+
+/* Unwind through the call stack calling TRACE_FN with STATE for each stack
+ frame. Returns the reason why the unwinding was stopped. */
+_Unwind_Reason_Code
+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 +67,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 +76,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 +86,26 @@ 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;
+
+ return _URC_NO_REASON;
}
#endif
Index: sysdep/generic/backtrace.h
===================================================================
--- sysdep/generic/backtrace.h (revision 115260)
+++ sysdep/generic/backtrace.h (working copy)
@@ -1,6 +1,6 @@
// backtrace.h - Fallback backtrace implementation. default implementation.
-/* Copyright (C) 2005 Free Software Foundation
+/* Copyright (C) 2005, 2006 Free Software Foundation
This file is part of libgcj.
@@ -13,10 +13,11 @@ details. */
#include <java-stack.h>
-/* Store return addresses of the current program stack in
- STATE and return the exact number of values stored. */
-void
-fallback_backtrace (_Jv_UnwindState *)
+/* Unwind through the call stack calling TRACE_FN with STATE for every stack
+ frame. Returns the reason why the unwinding was stopped. */
+_Unwind_Reason_Code
+fallback_backtrace (_Unwind_Trace_Fn, _Jv_UnwindState *)
{
+ return _URC_NO_REASON;
}
#endif