OSDN Git Service

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