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