agent/src/os/linux/LinuxDebuggerLocal.c

changeset 0
f90c822e73f8
child 1
2d8a650513c2
equal deleted inserted replaced
-1:000000000000 0:f90c822e73f8
1 /*
2 * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
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 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include <jni.h>
26 #include "libproc.h"
27
28 #include <elf.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <limits.h>
35
36 #if defined(x86_64) && !defined(amd64)
37 #define amd64 1
38 #endif
39
40 #ifdef i386
41 #include "sun_jvm_hotspot_debugger_x86_X86ThreadContext.h"
42 #endif
43
44 #ifdef amd64
45 #include "sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext.h"
46 #endif
47
48 #if defined(sparc) || defined(sparcv9)
49 #include "sun_jvm_hotspot_debugger_sparc_SPARCThreadContext.h"
50 #endif
51
52 static jfieldID p_ps_prochandle_ID = 0;
53 static jfieldID threadList_ID = 0;
54 static jfieldID loadObjectList_ID = 0;
55
56 static jmethodID createClosestSymbol_ID = 0;
57 static jmethodID createLoadObject_ID = 0;
58 static jmethodID getThreadForThreadId_ID = 0;
59 static jmethodID listAdd_ID = 0;
60
61 #define CHECK_EXCEPTION_(value) if ((*env)->ExceptionOccurred(env)) { return value; }
62 #define CHECK_EXCEPTION if ((*env)->ExceptionOccurred(env)) { return;}
63 #define THROW_NEW_DEBUGGER_EXCEPTION_(str, value) { throw_new_debugger_exception(env, str); return value; }
64 #define THROW_NEW_DEBUGGER_EXCEPTION(str) { throw_new_debugger_exception(env, str); return;}
65
66 void throw_new_debugger_exception(JNIEnv* env, const char* errMsg) {
67 (*env)->ThrowNew(env, (*env)->FindClass(env, "sun/jvm/hotspot/debugger/DebuggerException"), errMsg);
68 }
69
70 struct ps_prochandle* get_proc_handle(JNIEnv* env, jobject this_obj) {
71 jlong ptr = (*env)->GetLongField(env, this_obj, p_ps_prochandle_ID);
72 return (struct ps_prochandle*)(intptr_t)ptr;
73 }
74
75 /*
76 * Class: sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal
77 * Method: init0
78 * Signature: ()V
79 */
80 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_init0
81 (JNIEnv *env, jclass cls) {
82 jclass listClass;
83
84 if (init_libproc(getenv("LIBSAPROC_DEBUG") != NULL) != true) {
85 THROW_NEW_DEBUGGER_EXCEPTION("can't initialize libproc");
86 }
87
88 // fields we use
89 p_ps_prochandle_ID = (*env)->GetFieldID(env, cls, "p_ps_prochandle", "J");
90 CHECK_EXCEPTION;
91 threadList_ID = (*env)->GetFieldID(env, cls, "threadList", "Ljava/util/List;");
92 CHECK_EXCEPTION;
93 loadObjectList_ID = (*env)->GetFieldID(env, cls, "loadObjectList", "Ljava/util/List;");
94 CHECK_EXCEPTION;
95
96 // methods we use
97 createClosestSymbol_ID = (*env)->GetMethodID(env, cls, "createClosestSymbol",
98 "(Ljava/lang/String;J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;");
99 CHECK_EXCEPTION;
100 createLoadObject_ID = (*env)->GetMethodID(env, cls, "createLoadObject",
101 "(Ljava/lang/String;JJ)Lsun/jvm/hotspot/debugger/cdbg/LoadObject;");
102 CHECK_EXCEPTION;
103 getThreadForThreadId_ID = (*env)->GetMethodID(env, cls, "getThreadForThreadId",
104 "(J)Lsun/jvm/hotspot/debugger/ThreadProxy;");
105 CHECK_EXCEPTION;
106 // java.util.List method we call
107 listClass = (*env)->FindClass(env, "java/util/List");
108 CHECK_EXCEPTION;
109 listAdd_ID = (*env)->GetMethodID(env, listClass, "add", "(Ljava/lang/Object;)Z");
110 CHECK_EXCEPTION;
111 }
112
113 JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_getAddressSize
114 (JNIEnv *env, jclass cls)
115 {
116 #ifdef _LP64
117 return 8;
118 #else
119 return 4;
120 #endif
121
122 }
123
124
125 static void fillThreadsAndLoadObjects(JNIEnv* env, jobject this_obj, struct ps_prochandle* ph) {
126 int n = 0, i = 0;
127
128 // add threads
129 n = get_num_threads(ph);
130 for (i = 0; i < n; i++) {
131 jobject thread;
132 jobject threadList;
133 lwpid_t lwpid;
134
135 lwpid = get_lwp_id(ph, i);
136 thread = (*env)->CallObjectMethod(env, this_obj, getThreadForThreadId_ID,
137 (jlong)lwpid);
138 CHECK_EXCEPTION;
139 threadList = (*env)->GetObjectField(env, this_obj, threadList_ID);
140 CHECK_EXCEPTION;
141 (*env)->CallBooleanMethod(env, threadList, listAdd_ID, thread);
142 CHECK_EXCEPTION;
143 }
144
145 // add load objects
146 n = get_num_libs(ph);
147 for (i = 0; i < n; i++) {
148 uintptr_t base;
149 const char* name;
150 jobject loadObject;
151 jobject loadObjectList;
152
153 base = get_lib_base(ph, i);
154 name = get_lib_name(ph, i);
155 loadObject = (*env)->CallObjectMethod(env, this_obj, createLoadObject_ID,
156 (*env)->NewStringUTF(env, name), (jlong)0, (jlong)base);
157 CHECK_EXCEPTION;
158 loadObjectList = (*env)->GetObjectField(env, this_obj, loadObjectList_ID);
159 CHECK_EXCEPTION;
160 (*env)->CallBooleanMethod(env, loadObjectList, listAdd_ID, loadObject);
161 CHECK_EXCEPTION;
162 }
163 }
164
165
166 /*
167 * Verify that a named ELF binary file (core or executable) has the same
168 * bitness as ourselves.
169 * Throw an exception if there is a mismatch or other problem.
170 *
171 * If we proceed using a mismatched debugger/debuggee, the best to hope
172 * for is a missing symbol, the worst is a crash searching for debug symbols.
173 */
174 void verifyBitness(JNIEnv *env, const char *binaryName) {
175 int fd = open(binaryName, O_RDONLY);
176 if (fd < 0) {
177 THROW_NEW_DEBUGGER_EXCEPTION("cannot open binary file");
178 }
179 unsigned char elf_ident[EI_NIDENT];
180 int i = read(fd, &elf_ident, sizeof(elf_ident));
181 close(fd);
182
183 if (i < 0) {
184 THROW_NEW_DEBUGGER_EXCEPTION("cannot read binary file");
185 }
186 #ifndef _LP64
187 if (elf_ident[EI_CLASS] == ELFCLASS64) {
188 THROW_NEW_DEBUGGER_EXCEPTION("debuggee is 64 bit, use 64-bit java for debugger");
189 }
190 #else
191 if (elf_ident[EI_CLASS] != ELFCLASS64) {
192 THROW_NEW_DEBUGGER_EXCEPTION("debuggee is 32 bit, use 32 bit java for debugger");
193 }
194 #endif
195 }
196
197
198 /*
199 * Class: sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal
200 * Method: attach0
201 * Signature: (I)V
202 */
203 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_attach0__I
204 (JNIEnv *env, jobject this_obj, jint jpid) {
205
206 // For bitness checking, locate binary at /proc/jpid/exe
207 char buf[PATH_MAX];
208 snprintf((char *) &buf, PATH_MAX, "/proc/%d/exe", jpid);
209 verifyBitness(env, (char *) &buf);
210 CHECK_EXCEPTION;
211
212 struct ps_prochandle* ph;
213 if ( (ph = Pgrab(jpid)) == NULL) {
214 THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the process");
215 }
216 (*env)->SetLongField(env, this_obj, p_ps_prochandle_ID, (jlong)(intptr_t)ph);
217 fillThreadsAndLoadObjects(env, this_obj, ph);
218 }
219
220 /*
221 * Class: sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal
222 * Method: attach0
223 * Signature: (Ljava/lang/String;Ljava/lang/String;)V
224 */
225 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_attach0__Ljava_lang_String_2Ljava_lang_String_2
226 (JNIEnv *env, jobject this_obj, jstring execName, jstring coreName) {
227 const char *execName_cstr;
228 const char *coreName_cstr;
229 jboolean isCopy;
230 struct ps_prochandle* ph;
231
232 execName_cstr = (*env)->GetStringUTFChars(env, execName, &isCopy);
233 CHECK_EXCEPTION;
234 coreName_cstr = (*env)->GetStringUTFChars(env, coreName, &isCopy);
235 CHECK_EXCEPTION;
236
237 verifyBitness(env, execName_cstr);
238 CHECK_EXCEPTION;
239
240 if ( (ph = Pgrab_core(execName_cstr, coreName_cstr)) == NULL) {
241 (*env)->ReleaseStringUTFChars(env, execName, execName_cstr);
242 (*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr);
243 THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the core file");
244 }
245 (*env)->SetLongField(env, this_obj, p_ps_prochandle_ID, (jlong)(intptr_t)ph);
246 (*env)->ReleaseStringUTFChars(env, execName, execName_cstr);
247 (*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr);
248 fillThreadsAndLoadObjects(env, this_obj, ph);
249 }
250
251 /*
252 * Class: sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal
253 * Method: detach0
254 * Signature: ()V
255 */
256 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_detach0
257 (JNIEnv *env, jobject this_obj) {
258 struct ps_prochandle* ph = get_proc_handle(env, this_obj);
259 if (ph != NULL) {
260 Prelease(ph);
261 }
262 }
263
264 /*
265 * Class: sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal
266 * Method: lookupByName0
267 * Signature: (Ljava/lang/String;Ljava/lang/String;)J
268 */
269 JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_lookupByName0
270 (JNIEnv *env, jobject this_obj, jstring objectName, jstring symbolName) {
271 const char *objectName_cstr, *symbolName_cstr;
272 jlong addr;
273 jboolean isCopy;
274 struct ps_prochandle* ph = get_proc_handle(env, this_obj);
275
276 objectName_cstr = NULL;
277 if (objectName != NULL) {
278 objectName_cstr = (*env)->GetStringUTFChars(env, objectName, &isCopy);
279 CHECK_EXCEPTION_(0);
280 }
281 symbolName_cstr = (*env)->GetStringUTFChars(env, symbolName, &isCopy);
282 CHECK_EXCEPTION_(0);
283
284 addr = (jlong) lookup_symbol(ph, objectName_cstr, symbolName_cstr);
285
286 if (objectName_cstr != NULL) {
287 (*env)->ReleaseStringUTFChars(env, objectName, objectName_cstr);
288 }
289 (*env)->ReleaseStringUTFChars(env, symbolName, symbolName_cstr);
290 return addr;
291 }
292
293 /*
294 * Class: sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal
295 * Method: lookupByAddress0
296 * Signature: (J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;
297 */
298 JNIEXPORT jobject JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_lookupByAddress0
299 (JNIEnv *env, jobject this_obj, jlong addr) {
300 uintptr_t offset;
301 const char* sym = NULL;
302
303 struct ps_prochandle* ph = get_proc_handle(env, this_obj);
304 sym = symbol_for_pc(ph, (uintptr_t) addr, &offset);
305 if (sym == NULL) return 0;
306 return (*env)->CallObjectMethod(env, this_obj, createClosestSymbol_ID,
307 (*env)->NewStringUTF(env, sym), (jlong)offset);
308 }
309
310 /*
311 * Class: sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal
312 * Method: readBytesFromProcess0
313 * Signature: (JJ)Lsun/jvm/hotspot/debugger/ReadResult;
314 */
315 JNIEXPORT jbyteArray JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_readBytesFromProcess0
316 (JNIEnv *env, jobject this_obj, jlong addr, jlong numBytes) {
317
318 jboolean isCopy;
319 jbyteArray array;
320 jbyte *bufPtr;
321 ps_err_e err;
322
323 array = (*env)->NewByteArray(env, numBytes);
324 CHECK_EXCEPTION_(0);
325 bufPtr = (*env)->GetByteArrayElements(env, array, &isCopy);
326 CHECK_EXCEPTION_(0);
327
328 err = ps_pdread(get_proc_handle(env, this_obj), (psaddr_t) (uintptr_t)addr, bufPtr, numBytes);
329 (*env)->ReleaseByteArrayElements(env, array, bufPtr, 0);
330 return (err == PS_OK)? array : 0;
331 }
332
333 #if defined(i386) || defined(amd64) || defined(sparc) || defined(sparcv9)
334 JNIEXPORT jlongArray JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_getThreadIntegerRegisterSet0
335 (JNIEnv *env, jobject this_obj, jint lwp_id) {
336
337 struct user_regs_struct gregs;
338 jboolean isCopy;
339 jlongArray array;
340 jlong *regs;
341 int i;
342
343 struct ps_prochandle* ph = get_proc_handle(env, this_obj);
344 if (get_lwp_regs(ph, lwp_id, &gregs) != true) {
345 THROW_NEW_DEBUGGER_EXCEPTION_("get_thread_regs failed for a lwp", 0);
346 }
347
348 #undef NPRGREG
349 #ifdef i386
350 #define NPRGREG sun_jvm_hotspot_debugger_x86_X86ThreadContext_NPRGREG
351 #endif
352 #ifdef amd64
353 #define NPRGREG sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_NPRGREG
354 #endif
355 #if defined(sparc) || defined(sparcv9)
356 #define NPRGREG sun_jvm_hotspot_debugger_sparc_SPARCThreadContext_NPRGREG
357 #endif
358
359 array = (*env)->NewLongArray(env, NPRGREG);
360 CHECK_EXCEPTION_(0);
361 regs = (*env)->GetLongArrayElements(env, array, &isCopy);
362
363 #undef REG_INDEX
364
365 #ifdef i386
366 #define REG_INDEX(reg) sun_jvm_hotspot_debugger_x86_X86ThreadContext_##reg
367
368 regs[REG_INDEX(GS)] = (uintptr_t) gregs.xgs;
369 regs[REG_INDEX(FS)] = (uintptr_t) gregs.xfs;
370 regs[REG_INDEX(ES)] = (uintptr_t) gregs.xes;
371 regs[REG_INDEX(DS)] = (uintptr_t) gregs.xds;
372 regs[REG_INDEX(EDI)] = (uintptr_t) gregs.edi;
373 regs[REG_INDEX(ESI)] = (uintptr_t) gregs.esi;
374 regs[REG_INDEX(FP)] = (uintptr_t) gregs.ebp;
375 regs[REG_INDEX(SP)] = (uintptr_t) gregs.esp;
376 regs[REG_INDEX(EBX)] = (uintptr_t) gregs.ebx;
377 regs[REG_INDEX(EDX)] = (uintptr_t) gregs.edx;
378 regs[REG_INDEX(ECX)] = (uintptr_t) gregs.ecx;
379 regs[REG_INDEX(EAX)] = (uintptr_t) gregs.eax;
380 regs[REG_INDEX(PC)] = (uintptr_t) gregs.eip;
381 regs[REG_INDEX(CS)] = (uintptr_t) gregs.xcs;
382 regs[REG_INDEX(SS)] = (uintptr_t) gregs.xss;
383
384 #endif /* i386 */
385
386 #ifdef amd64
387 #define REG_INDEX(reg) sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_##reg
388
389 regs[REG_INDEX(R15)] = gregs.r15;
390 regs[REG_INDEX(R14)] = gregs.r14;
391 regs[REG_INDEX(R13)] = gregs.r13;
392 regs[REG_INDEX(R12)] = gregs.r12;
393 regs[REG_INDEX(RBP)] = gregs.rbp;
394 regs[REG_INDEX(RBX)] = gregs.rbx;
395 regs[REG_INDEX(R11)] = gregs.r11;
396 regs[REG_INDEX(R10)] = gregs.r10;
397 regs[REG_INDEX(R9)] = gregs.r9;
398 regs[REG_INDEX(R8)] = gregs.r8;
399 regs[REG_INDEX(RAX)] = gregs.rax;
400 regs[REG_INDEX(RCX)] = gregs.rcx;
401 regs[REG_INDEX(RDX)] = gregs.rdx;
402 regs[REG_INDEX(RSI)] = gregs.rsi;
403 regs[REG_INDEX(RDI)] = gregs.rdi;
404 regs[REG_INDEX(RIP)] = gregs.rip;
405 regs[REG_INDEX(CS)] = gregs.cs;
406 regs[REG_INDEX(RSP)] = gregs.rsp;
407 regs[REG_INDEX(SS)] = gregs.ss;
408 regs[REG_INDEX(FSBASE)] = gregs.fs_base;
409 regs[REG_INDEX(GSBASE)] = gregs.gs_base;
410 regs[REG_INDEX(DS)] = gregs.ds;
411 regs[REG_INDEX(ES)] = gregs.es;
412 regs[REG_INDEX(FS)] = gregs.fs;
413 regs[REG_INDEX(GS)] = gregs.gs;
414
415 #endif /* amd64 */
416
417 #if defined(sparc) || defined(sparcv9)
418
419 #define REG_INDEX(reg) sun_jvm_hotspot_debugger_sparc_SPARCThreadContext_##reg
420
421 #ifdef _LP64
422 regs[REG_INDEX(R_PSR)] = gregs.tstate;
423 regs[REG_INDEX(R_PC)] = gregs.tpc;
424 regs[REG_INDEX(R_nPC)] = gregs.tnpc;
425 regs[REG_INDEX(R_Y)] = gregs.y;
426 #else
427 regs[REG_INDEX(R_PSR)] = gregs.psr;
428 regs[REG_INDEX(R_PC)] = gregs.pc;
429 regs[REG_INDEX(R_nPC)] = gregs.npc;
430 regs[REG_INDEX(R_Y)] = gregs.y;
431 #endif
432 regs[REG_INDEX(R_G0)] = 0 ;
433 regs[REG_INDEX(R_G1)] = gregs.u_regs[0];
434 regs[REG_INDEX(R_G2)] = gregs.u_regs[1];
435 regs[REG_INDEX(R_G3)] = gregs.u_regs[2];
436 regs[REG_INDEX(R_G4)] = gregs.u_regs[3];
437 regs[REG_INDEX(R_G5)] = gregs.u_regs[4];
438 regs[REG_INDEX(R_G6)] = gregs.u_regs[5];
439 regs[REG_INDEX(R_G7)] = gregs.u_regs[6];
440 regs[REG_INDEX(R_O0)] = gregs.u_regs[7];
441 regs[REG_INDEX(R_O1)] = gregs.u_regs[8];
442 regs[REG_INDEX(R_O2)] = gregs.u_regs[ 9];
443 regs[REG_INDEX(R_O3)] = gregs.u_regs[10];
444 regs[REG_INDEX(R_O4)] = gregs.u_regs[11];
445 regs[REG_INDEX(R_O5)] = gregs.u_regs[12];
446 regs[REG_INDEX(R_O6)] = gregs.u_regs[13];
447 regs[REG_INDEX(R_O7)] = gregs.u_regs[14];
448 #endif /* sparc */
449
450
451 (*env)->ReleaseLongArrayElements(env, array, regs, JNI_COMMIT);
452 return array;
453 }
454 #endif

mercurial