OSDN Git Service

358bab7f13b39149565ba988dc96d696d50ab4ef
[pf3gnuchains/gcc-fork.git] / libjava / java / lang / natVMThrowable.cc
1 // natVMThrowable.cc - native helper methods for Throwable
2
3 /* Copyright (C) 2000, 2002  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 <aph@cygnus.com>
13  * @author Mark Wielaard <mark@klomp.org>
14  *
15  * Native helper methods for VM specific Throwable support.
16  */
17
18 #include <config.h>
19
20 #include <string.h>
21
22 #include <jvm.h>
23 #include <gcj/cni.h>
24 #include <gnu/gcj/RawData.h>
25 #include <java/lang/Object.h>
26 #include <java-threads.h>
27 #include <java/lang/Throwable.h>
28 #include <java/lang/VMThrowable.h>
29
30 #include <sys/types.h>
31
32 #include <stdlib.h>
33
34 #include <unistd.h>
35
36 #ifdef HAVE_EXECINFO_H
37 #include <execinfo.h>
38 #endif
39
40 /* FIXME: size of the stack trace is limited to 128 elements.  It's
41    undoubtedly sensible to limit the stack trace, but 128 is rather
42    arbitrary.  It may be better to configure this.  */
43
44 java::lang::VMThrowable *
45 java::lang::VMThrowable::fillInStackTrace (java::lang::Throwable* t)
46 {
47   if (! trace_enabled)
48     return NULL;
49 #if defined (HAVE_BACKTRACE)
50   VMThrowable* state = new VMThrowable;
51   void *p[128];
52   
53   // We subtract 1 from the number of elements because we don't want
54   // to include the calls to fillInStackTrace in the trace.
55   int n = backtrace (p, 128) - 1;  
56
57   void **addrs;
58   if (n > 0)
59     {
60       state->length = n;
61       addrs = (void **) _Jv_Malloc (n * sizeof p[0]);
62       while (n--)
63         addrs[n] = p[n];
64     }
65   else
66     addrs = NULL;
67
68   state->stackTraceAddrs = reinterpret_cast<gnu::gcj::RawData *> (addrs);
69
70   return state;
71 #endif
72   return NULL;
73 }