| 1 | // natThrowable.cc - Superclass for all exceptions.
|
|---|
| 2 |
|
|---|
| 3 | /* Copyright (C) 2000 Free Software Foundation, Inc
|
|---|
| 4 |
|
|---|
| 5 | This file is part of libgcj.
|
|---|
| 6 |
|
|---|
| 7 | This software is copyrighted work licensed under the terms of the
|
|---|
| 8 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|---|
| 9 | details. */
|
|---|
| 10 |
|
|---|
| 11 | /**
|
|---|
| 12 | * @author Andrew Haley <[email protected]>
|
|---|
| 13 | * @date Jan 6 2000
|
|---|
| 14 | */
|
|---|
| 15 |
|
|---|
| 16 | #include <config.h>
|
|---|
| 17 |
|
|---|
| 18 | #include <string.h>
|
|---|
| 19 |
|
|---|
| 20 | #include <gcj/cni.h>
|
|---|
| 21 | #include <jvm.h>
|
|---|
| 22 | #include <java/lang/Object.h>
|
|---|
| 23 | #include <java-threads.h>
|
|---|
| 24 | #include <java/lang/Throwable.h>
|
|---|
| 25 | #include <java/io/PrintStream.h>
|
|---|
| 26 | #include <java/io/PrintWriter.h>
|
|---|
| 27 | #include <java/io/IOException.h>
|
|---|
| 28 |
|
|---|
| 29 | #include <sys/types.h>
|
|---|
| 30 |
|
|---|
| 31 | #include <stdlib.h>
|
|---|
| 32 | #include <stdio.h>
|
|---|
| 33 |
|
|---|
| 34 | #include <unistd.h>
|
|---|
| 35 |
|
|---|
| 36 | #ifdef HAVE_EXECINFO_H
|
|---|
| 37 | #include <execinfo.h>
|
|---|
| 38 | #endif
|
|---|
| 39 |
|
|---|
| 40 | #include <name-finder.h>
|
|---|
| 41 |
|
|---|
| 42 | /* FIXME: size of the stack trace is limited to 128 elements. It's
|
|---|
| 43 | undoubtedly sensible to limit the stack trace, but 128 is rather
|
|---|
| 44 | arbitrary. It may be better to configure this. */
|
|---|
| 45 |
|
|---|
| 46 | java::lang::Throwable *
|
|---|
| 47 | java::lang::Throwable::fillInStackTrace (void)
|
|---|
| 48 | {
|
|---|
| 49 | if (! trace_enabled)
|
|---|
| 50 | return this;
|
|---|
| 51 | #if defined (HAVE_BACKTRACE)
|
|---|
| 52 | void *p[128];
|
|---|
| 53 |
|
|---|
| 54 | // We subtract 1 from the number of elements because we don't want
|
|---|
| 55 | // to include the call to fillInStackTrace in the trace.
|
|---|
| 56 | int n = backtrace (p, 128) - 1;
|
|---|
| 57 |
|
|---|
| 58 | if (n > 0)
|
|---|
| 59 | {
|
|---|
| 60 | // We copy the array below to deal with alignment issues.
|
|---|
| 61 | stackTrace = JvNewByteArray (n * sizeof p[0]);
|
|---|
| 62 | memcpy (elements (stackTrace), p+1, (n * sizeof p[0]));
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | #endif
|
|---|
| 66 |
|
|---|
| 67 | return this;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | void
|
|---|
| 71 | java::lang::Throwable::printRawStackTrace (java::io::PrintWriter *wr)
|
|---|
| 72 | {
|
|---|
| 73 | wr->println (toString ());
|
|---|
| 74 | #ifdef HAVE_BACKTRACE
|
|---|
| 75 | if (!stackTrace)
|
|---|
| 76 | return;
|
|---|
| 77 |
|
|---|
| 78 | int depth = stackTrace->length / sizeof (void *);
|
|---|
| 79 | void *p[depth];
|
|---|
| 80 | memcpy (p, elements (stackTrace), sizeof p);
|
|---|
| 81 |
|
|---|
| 82 | _Jv_name_finder finder (_Jv_ThisExecutable ());
|
|---|
| 83 |
|
|---|
| 84 | for (int i = 0; i < depth; i++)
|
|---|
| 85 | {
|
|---|
| 86 | bool found = finder.lookup (p[i]);
|
|---|
| 87 | wr->print (JvNewStringLatin1 (" at "));
|
|---|
| 88 | wr->print (JvNewStringLatin1 (finder.hex));
|
|---|
| 89 | if (found)
|
|---|
| 90 | {
|
|---|
| 91 | wr->print (JvNewStringLatin1 (": "));
|
|---|
| 92 | wr->print (JvNewStringLatin1 (finder.method_name));
|
|---|
| 93 | if (finder.file_name[0])
|
|---|
| 94 | {
|
|---|
| 95 | wr->print (JvNewStringLatin1 (" ("));
|
|---|
| 96 | wr->print (JvNewStringLatin1 (finder.file_name));
|
|---|
| 97 | wr->print (JvNewStringLatin1 (")"));
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 | wr->println ();
|
|---|
| 101 | }
|
|---|
| 102 | #endif /* HAVE_BACKTRACE */
|
|---|
| 103 | wr->flush ();
|
|---|
| 104 | }
|
|---|