Thu, 30 Aug 2012 11:20:01 -0400
7154641: Servicability agent should work on platforms other than x86, sparc
Summary: Added capability to load support classes for other cpus
Reviewed-by: coleenp, bobv, sla
Contributed-by: Bill Pittore <bill.pittore@oracle.com>
1 /*
2 * Copyright (c) 2002, 2012, 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 */
25 #include <jni.h>
26 #include "libproc.h"
28 #if defined(x86_64) && !defined(amd64)
29 #define amd64 1
30 #endif
32 #ifdef i386
33 #include "sun_jvm_hotspot_debugger_x86_X86ThreadContext.h"
34 #endif
36 #ifdef amd64
37 #include "sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext.h"
38 #endif
40 #if defined(sparc) || defined(sparcv9)
41 #include "sun_jvm_hotspot_debugger_sparc_SPARCThreadContext.h"
42 #endif
44 static jfieldID p_ps_prochandle_ID = 0;
45 static jfieldID threadList_ID = 0;
46 static jfieldID loadObjectList_ID = 0;
48 static jmethodID createClosestSymbol_ID = 0;
49 static jmethodID createLoadObject_ID = 0;
50 static jmethodID getThreadForThreadId_ID = 0;
51 static jmethodID listAdd_ID = 0;
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;}
58 void throw_new_debugger_exception(JNIEnv* env, const char* errMsg) {
59 (*env)->ThrowNew(env, (*env)->FindClass(env, "sun/jvm/hotspot/debugger/DebuggerException"), errMsg);
60 }
62 struct ps_prochandle* get_proc_handle(JNIEnv* env, jobject this_obj) {
63 jlong ptr = (*env)->GetLongField(env, this_obj, p_ps_prochandle_ID);
64 return (struct ps_prochandle*)(intptr_t)ptr;
65 }
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;
76 if (init_libproc(getenv("LIBSAPROC_DEBUG")) != true) {
77 THROW_NEW_DEBUGGER_EXCEPTION("can't initialize libproc");
78 }
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;
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 }
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
114 }
117 static void fillThreadsAndLoadObjects(JNIEnv* env, jobject this_obj, struct ps_prochandle* ph) {
118 int n = 0, i = 0;
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;
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 }
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;
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 }
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) {
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 }
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;
185 execName_cstr = (*env)->GetStringUTFChars(env, execName, &isCopy);
186 CHECK_EXCEPTION;
187 coreName_cstr = (*env)->GetStringUTFChars(env, coreName, &isCopy);
188 CHECK_EXCEPTION;
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 }
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 }
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);
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);
234 addr = (jlong) lookup_symbol(ph, objectName_cstr, symbolName_cstr);
236 if (objectName_cstr != NULL) {
237 (*env)->ReleaseStringUTFChars(env, objectName, objectName_cstr);
238 }
239 (*env)->ReleaseStringUTFChars(env, symbolName, symbolName_cstr);
240 return addr;
241 }
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;
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 }
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) {
268 jboolean isCopy;
269 jbyteArray array;
270 jbyte *bufPtr;
271 ps_err_e err;
273 array = (*env)->NewByteArray(env, numBytes);
274 CHECK_EXCEPTION_(0);
275 bufPtr = (*env)->GetByteArrayElements(env, array, &isCopy);
276 CHECK_EXCEPTION_(0);
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 }
283 #if defined(i386) || defined(ia64) || defined(amd64) || defined(sparc) || defined(sparcv9)
284 JNIEXPORT jlongArray JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_getThreadIntegerRegisterSet0
285 (JNIEnv *env, jobject this_obj, jint lwp_id) {
287 struct user_regs_struct gregs;
288 jboolean isCopy;
289 jlongArray array;
290 jlong *regs;
291 int i;
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 }
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
312 array = (*env)->NewLongArray(env, NPRGREG);
313 CHECK_EXCEPTION_(0);
314 regs = (*env)->GetLongArrayElements(env, array, &isCopy);
316 #undef REG_INDEX
318 #ifdef i386
319 #define REG_INDEX(reg) sun_jvm_hotspot_debugger_x86_X86ThreadContext_##reg
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;
337 #endif /* i386 */
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 */
346 #ifdef amd64
347 #define REG_INDEX(reg) sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_##reg
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;
375 #endif /* amd64 */
377 #if defined(sparc) || defined(sparcv9)
379 #define REG_INDEX(reg) sun_jvm_hotspot_debugger_sparc_SPARCThreadContext_##reg
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 */
411 (*env)->ReleaseLongArrayElements(env, array, regs, JNI_COMMIT);
412 return array;
413 }
414 #endif