Throwable.printStackTrace ()

Andrew Haley aph@pasanda.cygnus.co.uk
Mon Jan 17 08:37:00 GMT 2000


I've checked in code to print stack traces.  It's not yet perfect, but
I figured that it's such a useful debugging tool that it makes no
sense to wait for perfection.

Notes:

1.  I use a glibc specific backtrace() library call to retrieve the
backtrace data.  However, this isn't a very difficult thing to write
and there are versions of this routine for most ABIs in glibc, so I
don't think this is a big portability problem.

2.  I run a copy of addr2line to convert a code pointer to the name of
a routine.  If this program isn't installed or isn't in your path
you'll just get a hex dump.  However, you can capture this dump and
run it through addr2line on another host to get a symbolic stack
trace.  This is probably the best solution for small embedded systems.

It might have been easier to use System.exec() to run addr2line rather
than the low-level system call interface that I use.  However, I
didn't want the possibility of another exception being thrown while
looking up a name for stack trace.  Also, it would be useful to print
out stack traces for other programming languages.

The _Jv_name_finder class (which does the address lookups) is designed
to be extensible, so other ways of converting a pointer to a class and
method name may be added.  It might be possible to use reflective data
to do this, but we'd lose the ability to print the names of library
routines without reflective data.

3.  Symbols in shared objects aren't demangled.  It's fairly easy to
fix this by piping the stack trace through c++filt.  I could do this
automatically if people think it's a good idea.

4.  Exceptions thrown by bytecode classes print a backtrace through
the interpreter, rather than through the bytecode.

5.  Stack traces are limited to 128 elements.  This could easily be
changed, but otherwise accidental infinite recursion would print
megabytes of useless stack dump.

6.  I use the dladdr() library call to find symbols in shared objects.
This is another glibc dependency.

Andrew.

2000-01-14  Andrew Haley  <aph@cygnus.com>

	* java/lang/natThrowable.cc: New file.

	* java/lang/Throwable.java (fillInStackTrace): Make native.
	(printStackTrace): Call native method to do this.
	(Throwable): Call fillInStackTrace.
	(stackTrace): New variable.
	
	* include/jvm.h: Add _Jv_ThisExecutable functions.
	
	* prims.cc: (_Jv_execName): New variable.
	(catch_segv): Call fillInStackTrace.
	(catch_fpe): Ditto.
	(_Jv_ThisExecutable): New functions.
	(JvRunMain): Set the name of this executable.

	* Makefile.am: Add java/lang/natThrowable.cc.
	Add name-finder.cc.
	* Makefile.in: Rebuilt.

	* acconfig.h: Add HAVE_PROC_SELF_EXE.

	* configure.in: Force link with __frame_state_for in
	FORCELIBGCCSPEC.  Add new checks for backtrace.
	* include/config.h.in: Rebuilt.

	* name-finder.cc: New file.
	* include/name-finder.h: New file.
	


More information about the Java-patches mailing list