Mercurial > hg > truffle
annotate agent/src/os/linux/LinuxDebuggerLocal.c @ 7580:dd7248d3e151
7152671: RFE: Windows decoder should add some std dirs to the symbol search path
Summary: Added JRE/JDK bin directories to decoder's symbol search path
Reviewed-by: dcubed, sla
author | zgu |
---|---|
date | Wed, 09 Jan 2013 14:46:55 -0500 |
parents | a9fed06c01d2 |
children | 9fae07c31641 |
rev | line source |
---|---|
0 | 1 /* |
6641
a9fed06c01d2
7154641: Servicability agent should work on platforms other than x86, sparc
bpittore
parents:
1552
diff
changeset
|
2 * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved. |
0 | 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
1552
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
0
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
0
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
0
diff
changeset
|
21 * questions. |
0 | 22 * |
23 */ | |
24 | |
25 #include <jni.h> | |
26 #include "libproc.h" | |
27 | |
28 #if defined(x86_64) && !defined(amd64) | |
29 #define amd64 1 | |
30 #endif | |
31 | |
32 #ifdef i386 | |
33 #include "sun_jvm_hotspot_debugger_x86_X86ThreadContext.h" | |
34 #endif | |
35 | |
36 #ifdef amd64 | |
37 #include "sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext.h" | |
38 #endif | |
39 | |
40 #if defined(sparc) || defined(sparcv9) | |
41 #include "sun_jvm_hotspot_debugger_sparc_SPARCThreadContext.h" | |
42 #endif | |
43 | |
44 static jfieldID p_ps_prochandle_ID = 0; | |
45 static jfieldID threadList_ID = 0; | |
46 static jfieldID loadObjectList_ID = 0; | |
47 | |
48 static jmethodID createClosestSymbol_ID = 0; | |
49 static jmethodID createLoadObject_ID = 0; | |
50 static jmethodID getThreadForThreadId_ID = 0; | |
51 static jmethodID listAdd_ID = 0; | |
52 | |
53 #define CHECK_EXCEPTION_(value) if ((*env)->ExceptionOccurred(env)) { return value; } | |
54 #define CHECK_EXCEPTION if ((*env)->ExceptionOccurred(env)) { return;} | |
55 #define THROW_NEW_DEBUGGER_EXCEPTION_(str, value) { throw_new_debugger_exception(env, str); return value; } | |
56 #define THROW_NEW_DEBUGGER_EXCEPTION(str) { throw_new_debugger_exception(env, str); return;} | |
57 | |
6641
a9fed06c01d2
7154641: Servicability agent should work on platforms other than x86, sparc
bpittore
parents:
1552
diff
changeset
|
58 void throw_new_debugger_exception(JNIEnv* env, const char* errMsg) { |
0 | 59 (*env)->ThrowNew(env, (*env)->FindClass(env, "sun/jvm/hotspot/debugger/DebuggerException"), errMsg); |
60 } | |
61 | |
6641
a9fed06c01d2
7154641: Servicability agent should work on platforms other than x86, sparc
bpittore
parents:
1552
diff
changeset
|
62 struct ps_prochandle* get_proc_handle(JNIEnv* env, jobject this_obj) { |
0 | 63 jlong ptr = (*env)->GetLongField(env, this_obj, p_ps_prochandle_ID); |
64 return (struct ps_prochandle*)(intptr_t)ptr; | |
65 } | |
66 | |
67 /* | |
68 * Class: sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal | |
69 * Method: init0 | |
70 * Signature: ()V | |
71 */ | |
72 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_init0 | |
73 (JNIEnv *env, jclass cls) { | |
74 jclass listClass; | |
75 | |
76 if (init_libproc(getenv("LIBSAPROC_DEBUG")) != true) { | |
77 THROW_NEW_DEBUGGER_EXCEPTION("can't initialize libproc"); | |
78 } | |
79 | |
80 // fields we use | |
81 p_ps_prochandle_ID = (*env)->GetFieldID(env, cls, "p_ps_prochandle", "J"); | |
82 CHECK_EXCEPTION; | |
83 threadList_ID = (*env)->GetFieldID(env, cls, "threadList", "Ljava/util/List;"); | |
84 CHECK_EXCEPTION; | |
85 loadObjectList_ID = (*env)->GetFieldID(env, cls, "loadObjectList", "Ljava/util/List;"); | |
86 CHECK_EXCEPTION; | |
87 | |
88 // methods we use | |
89 createClosestSymbol_ID = (*env)->GetMethodID(env, cls, "createClosestSymbol", | |
90 "(Ljava/lang/String;J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;"); | |
91 CHECK_EXCEPTION; | |
92 createLoadObject_ID = (*env)->GetMethodID(env, cls, "createLoadObject", | |
93 "(Ljava/lang/String;JJ)Lsun/jvm/hotspot/debugger/cdbg/LoadObject;"); | |
94 CHECK_EXCEPTION; | |
95 getThreadForThreadId_ID = (*env)->GetMethodID(env, cls, "getThreadForThreadId", | |
96 "(J)Lsun/jvm/hotspot/debugger/ThreadProxy;"); | |
97 CHECK_EXCEPTION; | |
98 // java.util.List method we call | |
99 listClass = (*env)->FindClass(env, "java/util/List"); | |
100 CHECK_EXCEPTION; | |
101 listAdd_ID = (*env)->GetMethodID(env, listClass, "add", "(Ljava/lang/Object;)Z"); | |
102 CHECK_EXCEPTION; | |
103 } | |
104 | |
105 JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_getAddressSize | |
106 (JNIEnv *env, jclass cls) | |
107 { | |
108 #ifdef _LP64 | |
109 return 8; | |
110 #else | |
111 return 4; | |
112 #endif | |
113 | |
114 } | |
115 | |
116 | |
117 static void fillThreadsAndLoadObjects(JNIEnv* env, jobject this_obj, struct ps_prochandle* ph) { | |
118 int n = 0, i = 0; | |
119 | |
120 // add threads | |
121 n = get_num_threads(ph); | |
122 for (i = 0; i < n; i++) { | |
123 jobject thread; | |
124 jobject threadList; | |
125 lwpid_t lwpid; | |
126 | |
127 lwpid = get_lwp_id(ph, i); | |
128 thread = (*env)->CallObjectMethod(env, this_obj, getThreadForThreadId_ID, | |
129 (jlong)lwpid); | |
130 CHECK_EXCEPTION; | |
131 threadList = (*env)->GetObjectField(env, this_obj, threadList_ID); | |
132 CHECK_EXCEPTION; | |
133 (*env)->CallBooleanMethod(env, threadList, listAdd_ID, thread); | |
134 CHECK_EXCEPTION; | |
135 } | |
136 | |
137 // add load objects | |
138 n = get_num_libs(ph); | |
139 for (i = 0; i < n; i++) { | |
140 uintptr_t base; | |
141 const char* name; | |
142 jobject loadObject; | |
143 jobject loadObjectList; | |
144 | |
145 base = get_lib_base(ph, i); | |
146 name = get_lib_name(ph, i); | |
147 loadObject = (*env)->CallObjectMethod(env, this_obj, createLoadObject_ID, | |
148 (*env)->NewStringUTF(env, name), (jlong)0, (jlong)base); | |
149 CHECK_EXCEPTION; | |
150 loadObjectList = (*env)->GetObjectField(env, this_obj, loadObjectList_ID); | |
151 CHECK_EXCEPTION; | |
152 (*env)->CallBooleanMethod(env, loadObjectList, listAdd_ID, loadObject); | |
153 CHECK_EXCEPTION; | |
154 } | |
155 } | |
156 | |
157 /* | |
158 * Class: sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal | |
159 * Method: attach0 | |
160 * Signature: (I)V | |
161 */ | |
162 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_attach0__I | |
163 (JNIEnv *env, jobject this_obj, jint jpid) { | |
164 | |
165 struct ps_prochandle* ph; | |
166 if ( (ph = Pgrab(jpid)) == NULL) { | |
167 THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the process"); | |
168 } | |
169 (*env)->SetLongField(env, this_obj, p_ps_prochandle_ID, (jlong)(intptr_t)ph); | |
170 fillThreadsAndLoadObjects(env, this_obj, ph); | |
171 } | |
172 | |
173 /* | |
174 * Class: sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal | |
175 * Method: attach0 | |
176 * Signature: (Ljava/lang/String;Ljava/lang/String;)V | |
177 */ | |
178 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_attach0__Ljava_lang_String_2Ljava_lang_String_2 | |
179 (JNIEnv *env, jobject this_obj, jstring execName, jstring coreName) { | |
180 const char *execName_cstr; | |
181 const char *coreName_cstr; | |
182 jboolean isCopy; | |
183 struct ps_prochandle* ph; | |
184 | |
185 execName_cstr = (*env)->GetStringUTFChars(env, execName, &isCopy); | |
186 CHECK_EXCEPTION; | |
187 coreName_cstr = (*env)->GetStringUTFChars(env, coreName, &isCopy); | |
188 CHECK_EXCEPTION; | |
189 | |
190 if ( (ph = Pgrab_core(execName_cstr, coreName_cstr)) == NULL) { | |
191 (*env)->ReleaseStringUTFChars(env, execName, execName_cstr); | |
192 (*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr); | |
193 THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the core file"); | |
194 } | |
195 (*env)->SetLongField(env, this_obj, p_ps_prochandle_ID, (jlong)(intptr_t)ph); | |
196 (*env)->ReleaseStringUTFChars(env, execName, execName_cstr); | |
197 (*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr); | |
198 fillThreadsAndLoadObjects(env, this_obj, ph); | |
199 } | |
200 | |
201 /* | |
202 * Class: sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal | |
203 * Method: detach0 | |
204 * Signature: ()V | |
205 */ | |
206 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_detach0 | |
207 (JNIEnv *env, jobject this_obj) { | |
208 struct ps_prochandle* ph = get_proc_handle(env, this_obj); | |
209 if (ph != NULL) { | |
210 Prelease(ph); | |
211 } | |
212 } | |
213 | |
214 /* | |
215 * Class: sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal | |
216 * Method: lookupByName0 | |
217 * Signature: (Ljava/lang/String;Ljava/lang/String;)J | |
218 */ | |
219 JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_lookupByName0 | |
220 (JNIEnv *env, jobject this_obj, jstring objectName, jstring symbolName) { | |
221 const char *objectName_cstr, *symbolName_cstr; | |
222 jlong addr; | |
223 jboolean isCopy; | |
224 struct ps_prochandle* ph = get_proc_handle(env, this_obj); | |
225 | |
226 objectName_cstr = NULL; | |
227 if (objectName != NULL) { | |
228 objectName_cstr = (*env)->GetStringUTFChars(env, objectName, &isCopy); | |
229 CHECK_EXCEPTION_(0); | |
230 } | |
231 symbolName_cstr = (*env)->GetStringUTFChars(env, symbolName, &isCopy); | |
232 CHECK_EXCEPTION_(0); | |
233 | |
234 addr = (jlong) lookup_symbol(ph, objectName_cstr, symbolName_cstr); | |
235 | |
236 if (objectName_cstr != NULL) { | |
237 (*env)->ReleaseStringUTFChars(env, objectName, objectName_cstr); | |
238 } | |
239 (*env)->ReleaseStringUTFChars(env, symbolName, symbolName_cstr); | |
240 return addr; | |
241 } | |
242 | |
243 /* | |
244 * Class: sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal | |
245 * Method: lookupByAddress0 | |
246 * Signature: (J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol; | |
247 */ | |
248 JNIEXPORT jobject JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_lookupByAddress0 | |
249 (JNIEnv *env, jobject this_obj, jlong addr) { | |
250 uintptr_t offset; | |
251 const char* sym = NULL; | |
252 | |
253 struct ps_prochandle* ph = get_proc_handle(env, this_obj); | |
254 sym = symbol_for_pc(ph, (uintptr_t) addr, &offset); | |
255 if (sym == NULL) return 0; | |
256 return (*env)->CallObjectMethod(env, this_obj, createClosestSymbol_ID, | |
257 (*env)->NewStringUTF(env, sym), (jlong)offset); | |
258 } | |
259 | |
260 /* | |
261 * Class: sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal | |
262 * Method: readBytesFromProcess0 | |
263 * Signature: (JJ)Lsun/jvm/hotspot/debugger/ReadResult; | |
264 */ | |
265 JNIEXPORT jbyteArray JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_readBytesFromProcess0 | |
266 (JNIEnv *env, jobject this_obj, jlong addr, jlong numBytes) { | |
267 | |
268 jboolean isCopy; | |
269 jbyteArray array; | |
270 jbyte *bufPtr; | |
271 ps_err_e err; | |
272 | |
273 array = (*env)->NewByteArray(env, numBytes); | |
274 CHECK_EXCEPTION_(0); | |
275 bufPtr = (*env)->GetByteArrayElements(env, array, &isCopy); | |
276 CHECK_EXCEPTION_(0); | |
277 | |
278 err = ps_pdread(get_proc_handle(env, this_obj), (psaddr_t) (uintptr_t)addr, bufPtr, numBytes); | |
279 (*env)->ReleaseByteArrayElements(env, array, bufPtr, 0); | |
280 return (err == PS_OK)? array : 0; | |
281 } | |
282 | |
6641
a9fed06c01d2
7154641: Servicability agent should work on platforms other than x86, sparc
bpittore
parents:
1552
diff
changeset
|
283 #if defined(i386) || defined(ia64) || defined(amd64) || defined(sparc) || defined(sparcv9) |
0 | 284 JNIEXPORT jlongArray JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_getThreadIntegerRegisterSet0 |
285 (JNIEnv *env, jobject this_obj, jint lwp_id) { | |
286 | |
287 struct user_regs_struct gregs; | |
288 jboolean isCopy; | |
289 jlongArray array; | |
290 jlong *regs; | |
291 int i; | |
292 | |
293 struct ps_prochandle* ph = get_proc_handle(env, this_obj); | |
294 if (get_lwp_regs(ph, lwp_id, &gregs) != true) { | |
295 THROW_NEW_DEBUGGER_EXCEPTION_("get_thread_regs failed for a lwp", 0); | |
296 } | |
297 | |
298 #undef NPRGREG | |
299 #ifdef i386 | |
300 #define NPRGREG sun_jvm_hotspot_debugger_x86_X86ThreadContext_NPRGREG | |
301 #endif | |
302 #ifdef ia64 | |
303 #define NPRGREG IA64_REG_COUNT | |
304 #endif | |
305 #ifdef amd64 | |
306 #define NPRGREG sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_NPRGREG | |
307 #endif | |
308 #if defined(sparc) || defined(sparcv9) | |
309 #define NPRGREG sun_jvm_hotspot_debugger_sparc_SPARCThreadContext_NPRGREG | |
310 #endif | |
311 | |
312 array = (*env)->NewLongArray(env, NPRGREG); | |
313 CHECK_EXCEPTION_(0); | |
314 regs = (*env)->GetLongArrayElements(env, array, &isCopy); | |
315 | |
316 #undef REG_INDEX | |
317 | |
318 #ifdef i386 | |
319 #define REG_INDEX(reg) sun_jvm_hotspot_debugger_x86_X86ThreadContext_##reg | |
320 | |
321 regs[REG_INDEX(GS)] = (uintptr_t) gregs.xgs; | |
322 regs[REG_INDEX(FS)] = (uintptr_t) gregs.xfs; | |
323 regs[REG_INDEX(ES)] = (uintptr_t) gregs.xes; | |
324 regs[REG_INDEX(DS)] = (uintptr_t) gregs.xds; | |
325 regs[REG_INDEX(EDI)] = (uintptr_t) gregs.edi; | |
326 regs[REG_INDEX(ESI)] = (uintptr_t) gregs.esi; | |
327 regs[REG_INDEX(FP)] = (uintptr_t) gregs.ebp; | |
328 regs[REG_INDEX(SP)] = (uintptr_t) gregs.esp; | |
329 regs[REG_INDEX(EBX)] = (uintptr_t) gregs.ebx; | |
330 regs[REG_INDEX(EDX)] = (uintptr_t) gregs.edx; | |
331 regs[REG_INDEX(ECX)] = (uintptr_t) gregs.ecx; | |
332 regs[REG_INDEX(EAX)] = (uintptr_t) gregs.eax; | |
333 regs[REG_INDEX(PC)] = (uintptr_t) gregs.eip; | |
334 regs[REG_INDEX(CS)] = (uintptr_t) gregs.xcs; | |
335 regs[REG_INDEX(SS)] = (uintptr_t) gregs.xss; | |
336 | |
337 #endif /* i386 */ | |
338 | |
339 #if ia64 | |
340 regs = (*env)->GetLongArrayElements(env, array, &isCopy); | |
341 for (i = 0; i < NPRGREG; i++ ) { | |
342 regs[i] = 0xDEADDEAD; | |
343 } | |
344 #endif /* ia64 */ | |
345 | |
346 #ifdef amd64 | |
347 #define REG_INDEX(reg) sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_##reg | |
348 | |
349 regs[REG_INDEX(R15)] = gregs.r15; | |
350 regs[REG_INDEX(R14)] = gregs.r14; | |
351 regs[REG_INDEX(R13)] = gregs.r13; | |
352 regs[REG_INDEX(R12)] = gregs.r12; | |
353 regs[REG_INDEX(RBP)] = gregs.rbp; | |
354 regs[REG_INDEX(RBX)] = gregs.rbx; | |
355 regs[REG_INDEX(R11)] = gregs.r11; | |
356 regs[REG_INDEX(R10)] = gregs.r10; | |
357 regs[REG_INDEX(R9)] = gregs.r9; | |
358 regs[REG_INDEX(R8)] = gregs.r8; | |
359 regs[REG_INDEX(RAX)] = gregs.rax; | |
360 regs[REG_INDEX(RCX)] = gregs.rcx; | |
361 regs[REG_INDEX(RDX)] = gregs.rdx; | |
362 regs[REG_INDEX(RSI)] = gregs.rsi; | |
363 regs[REG_INDEX(RDI)] = gregs.rdi; | |
364 regs[REG_INDEX(RIP)] = gregs.rip; | |
365 regs[REG_INDEX(CS)] = gregs.cs; | |
366 regs[REG_INDEX(RSP)] = gregs.rsp; | |
367 regs[REG_INDEX(SS)] = gregs.ss; | |
368 regs[REG_INDEX(FSBASE)] = gregs.fs_base; | |
369 regs[REG_INDEX(GSBASE)] = gregs.gs_base; | |
370 regs[REG_INDEX(DS)] = gregs.ds; | |
371 regs[REG_INDEX(ES)] = gregs.es; | |
372 regs[REG_INDEX(FS)] = gregs.fs; | |
373 regs[REG_INDEX(GS)] = gregs.gs; | |
374 | |
375 #endif /* amd64 */ | |
376 | |
377 #if defined(sparc) || defined(sparcv9) | |
378 | |
379 #define REG_INDEX(reg) sun_jvm_hotspot_debugger_sparc_SPARCThreadContext_##reg | |
380 | |
381 #ifdef _LP64 | |
382 regs[REG_INDEX(R_PSR)] = gregs.tstate; | |
383 regs[REG_INDEX(R_PC)] = gregs.tpc; | |
384 regs[REG_INDEX(R_nPC)] = gregs.tnpc; | |
385 regs[REG_INDEX(R_Y)] = gregs.y; | |
386 #else | |
387 regs[REG_INDEX(R_PSR)] = gregs.psr; | |
388 regs[REG_INDEX(R_PC)] = gregs.pc; | |
389 regs[REG_INDEX(R_nPC)] = gregs.npc; | |
390 regs[REG_INDEX(R_Y)] = gregs.y; | |
391 #endif | |
392 regs[REG_INDEX(R_G0)] = 0 ; | |
393 regs[REG_INDEX(R_G1)] = gregs.u_regs[0]; | |
394 regs[REG_INDEX(R_G2)] = gregs.u_regs[1]; | |
395 regs[REG_INDEX(R_G3)] = gregs.u_regs[2]; | |
396 regs[REG_INDEX(R_G4)] = gregs.u_regs[3]; | |
397 regs[REG_INDEX(R_G5)] = gregs.u_regs[4]; | |
398 regs[REG_INDEX(R_G6)] = gregs.u_regs[5]; | |
399 regs[REG_INDEX(R_G7)] = gregs.u_regs[6]; | |
400 regs[REG_INDEX(R_O0)] = gregs.u_regs[7]; | |
401 regs[REG_INDEX(R_O1)] = gregs.u_regs[8]; | |
402 regs[REG_INDEX(R_O2)] = gregs.u_regs[ 9]; | |
403 regs[REG_INDEX(R_O3)] = gregs.u_regs[10]; | |
404 regs[REG_INDEX(R_O4)] = gregs.u_regs[11]; | |
405 regs[REG_INDEX(R_O5)] = gregs.u_regs[12]; | |
406 regs[REG_INDEX(R_O6)] = gregs.u_regs[13]; | |
407 regs[REG_INDEX(R_O7)] = gregs.u_regs[14]; | |
408 #endif /* sparc */ | |
409 | |
410 | |
411 (*env)->ReleaseLongArrayElements(env, array, regs, JNI_COMMIT); | |
412 return array; | |
413 } | |
6641
a9fed06c01d2
7154641: Servicability agent should work on platforms other than x86, sparc
bpittore
parents:
1552
diff
changeset
|
414 #endif |