OSDN Git Service

PR libgcj/31084
[pf3gnuchains/gcc-fork.git] / libjava / include / java-stack.h
1 // java-stack.h - Definitions for unwinding & inspecting the call stack.
2
3 /* Copyright (C) 2005, 2006  Free Software Foundation
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 #ifndef __JV_STACKTRACE_H__
12 #define __JV_STACKTRACE_H__
13
14 #include <stdlib.h>
15 #include <unwind.h>
16
17 #include <gcj/cni.h>
18 #include <gcj/javaprims.h>
19
20 #include <java-interp.h>
21
22 #include <java/lang/Class.h>
23 #include <java/lang/StackTraceElement.h>
24 #include <java/lang/Throwable.h>
25 #include <java/lang/Thread.h>
26 #include <java/util/IdentityHashMap.h>
27
28 #include <gnu/gcj/runtime/NameFinder.h>
29
30 using namespace gnu::gcj::runtime;
31 using namespace java::lang;
32
33 extern "Java"
34 {
35   namespace gnu
36   {
37     namespace classpath
38     {
39         class VMStackWalker;
40     }
41   }
42 }
43
44 #ifdef INTERPRETER
45 struct _Jv_InterpFrameInfo
46 {
47   _Jv_InterpMethod *meth;
48   pc_t pc;
49 };
50 #endif
51
52 union _Jv_FrameInfo
53 {
54 };
55
56 struct _Jv_StackFrame
57 {
58   _Jv_FrameType type;   /* Native or interpreted.  */
59   union {
60 #ifdef INTERPRETER
61     _Jv_InterpFrameInfo interp;
62 #endif
63     struct {
64       jclass proxyClass;
65       _Jv_Method *proxyMethod;
66     };
67     struct {
68       void *ip;
69       void *start_ip;
70     };
71   };
72 //  _Jv_FrameInfo info;   /* Frame-type specific data.  */
73   jclass klass;
74   _Jv_Method *meth;
75 };
76
77 typedef struct _Jv_UnwindState;
78 typedef _Unwind_Reason_Code (*_Jv_TraceFn) (_Jv_UnwindState *);
79
80 struct _Jv_UnwindState
81 {
82   jint length;                   // length of FRAMES
83   jint pos;                      // current position in FRAMES
84   _Jv_StackFrame *frames;        // array of stack frame data to be filled.
85 #ifdef INTERPRETER
86   _Jv_InterpFrame *interp_frame; // current frame in the interpreter stack.
87 #endif
88   _Jv_TraceFn trace_function;    // function to call back after each frame
89                                  // is enumerated. May be NULL.
90   void *trace_data;              // additional state data for trace_function.
91   
92   _Jv_UnwindState (jint ln)
93     {
94       length = ln;
95       pos = 0;
96       frames = NULL;
97 #ifdef INTERPRETER
98       Thread *thread = Thread::currentThread();
99       // Check for NULL currentThread(), in case an exception is created 
100       // very early during the runtime startup.
101       if (thread)
102         interp_frame = (_Jv_InterpFrame *) thread->interp_frame;
103       else
104         interp_frame = NULL;
105 #endif
106       trace_function = NULL;
107       trace_data = NULL;
108     }
109 };
110
111 class _Jv_StackTrace
112 {
113 private:
114   int length;
115   _Jv_StackFrame frames[];
116
117   static java::util::IdentityHashMap *ncodeMap;
118   static void UpdateNCodeMap ();
119   static jclass ClassForFrame (_Jv_StackFrame *frame);
120   static void FillInFrameInfo (_Jv_StackFrame *frame);
121   static void getLineNumberForFrame(_Jv_StackFrame *frame, NameFinder *finder, 
122                                     jstring *sourceFileName, jint *lineNum,
123                                     jstring *methodName);
124   
125   static _Unwind_Reason_Code UnwindTraceFn (struct _Unwind_Context *context, 
126     void *state_ptr);
127     
128   static _Unwind_Reason_Code calling_class_trace_fn (_Jv_UnwindState *state);
129   static _Unwind_Reason_Code non_system_trace_fn (_Jv_UnwindState *state);
130   static _Unwind_Reason_Code accesscontrol_trace_fn (_Jv_UnwindState *state);
131   static _Unwind_Reason_Code stackwalker_trace_fn (_Jv_UnwindState *state);
132   static _Unwind_Reason_Code stackwalker_nnl_trace_fn (_Jv_UnwindState *state);
133
134 public:
135   static _Jv_StackTrace *GetStackTrace (void);
136   static JArray< ::java::lang::StackTraceElement *>*
137     GetStackTraceElements (_Jv_StackTrace *trace, 
138     java::lang::Throwable *throwable);
139   static jclass GetCallingClass (jclass);
140   static void GetCallerInfo (jclass checkClass, jclass *, _Jv_Method **);
141   static ClassLoader *GetFirstNonSystemClassLoader (void);
142   static jobjectArray GetAccessControlStack ();
143   static JArray<jclass> *GetStackWalkerStack ();
144   static jclass GetStackWalkerCallingClass ();
145   static ClassLoader *GetStackWalkerFirstNonNullLoader ();
146
147   friend jclass _Jv_GetMethodDeclaringClass (jmethodID);
148   friend class gnu::classpath::VMStackWalker;
149 };
150
151 // Information about a given address.
152 struct _Jv_AddrInfo
153 {
154   // File name of the defining module.
155   const char *file_name;
156
157   // Base address of the loaded module.
158   void *base;
159
160   // Name of the nearest symbol.
161   const char *sym_name;
162
163   // Address of the nearest symbol.
164   void *sym_addr;
165
166   ~_Jv_AddrInfo (void)
167     {
168       // On systems with a real dladdr(), the file and symbol names given by
169       // _Jv_platform_dladdr() are not dynamically allocated.  On Windows,
170       // they are.
171
172 #ifdef WIN32
173       if (file_name)
174         free ((void *)file_name);
175
176       if (sym_name)
177         free ((void *)sym_name);
178 #endif /* WIN32 */
179     }
180 };
181
182 #endif /* __JV_STACKTRACE_H__ */