1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/agent/src/os/bsd/BsdDebuggerLocal.c Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,413 @@ 1.4 +/* 1.5 + * Copyright (c) 2002, 2007, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include <stdlib.h> 1.29 +#include <jni.h> 1.30 +#include "libproc.h" 1.31 + 1.32 +#if defined(x86_64) && !defined(amd64) 1.33 +#define amd64 1 1.34 +#endif 1.35 + 1.36 +#ifdef i386 1.37 +#include "sun_jvm_hotspot_debugger_x86_X86ThreadContext.h" 1.38 +#endif 1.39 + 1.40 +#ifdef amd64 1.41 +#include "sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext.h" 1.42 +#endif 1.43 + 1.44 +#if defined(sparc) || defined(sparcv9) 1.45 +#include "sun_jvm_hotspot_debugger_sparc_SPARCThreadContext.h" 1.46 +#endif 1.47 + 1.48 +static jfieldID p_ps_prochandle_ID = 0; 1.49 +static jfieldID threadList_ID = 0; 1.50 +static jfieldID loadObjectList_ID = 0; 1.51 + 1.52 +static jmethodID createClosestSymbol_ID = 0; 1.53 +static jmethodID createLoadObject_ID = 0; 1.54 +static jmethodID getThreadForThreadId_ID = 0; 1.55 +static jmethodID listAdd_ID = 0; 1.56 + 1.57 +#define CHECK_EXCEPTION_(value) if ((*env)->ExceptionOccurred(env)) { return value; } 1.58 +#define CHECK_EXCEPTION if ((*env)->ExceptionOccurred(env)) { return;} 1.59 +#define THROW_NEW_DEBUGGER_EXCEPTION_(str, value) { throw_new_debugger_exception(env, str); return value; } 1.60 +#define THROW_NEW_DEBUGGER_EXCEPTION(str) { throw_new_debugger_exception(env, str); return;} 1.61 + 1.62 +static void throw_new_debugger_exception(JNIEnv* env, const char* errMsg) { 1.63 + (*env)->ThrowNew(env, (*env)->FindClass(env, "sun/jvm/hotspot/debugger/DebuggerException"), errMsg); 1.64 +} 1.65 + 1.66 +static struct ps_prochandle* get_proc_handle(JNIEnv* env, jobject this_obj) { 1.67 + jlong ptr = (*env)->GetLongField(env, this_obj, p_ps_prochandle_ID); 1.68 + return (struct ps_prochandle*)(intptr_t)ptr; 1.69 +} 1.70 + 1.71 +/* 1.72 + * Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal 1.73 + * Method: init0 1.74 + * Signature: ()V 1.75 + */ 1.76 +JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_init0 1.77 + (JNIEnv *env, jclass cls) { 1.78 + jclass listClass; 1.79 + 1.80 + if (init_libproc(getenv("LIBSAPROC_DEBUG") != NULL) != true) { 1.81 + THROW_NEW_DEBUGGER_EXCEPTION("can't initialize libproc"); 1.82 + } 1.83 + 1.84 + // fields we use 1.85 + p_ps_prochandle_ID = (*env)->GetFieldID(env, cls, "p_ps_prochandle", "J"); 1.86 + CHECK_EXCEPTION; 1.87 + threadList_ID = (*env)->GetFieldID(env, cls, "threadList", "Ljava/util/List;"); 1.88 + CHECK_EXCEPTION; 1.89 + loadObjectList_ID = (*env)->GetFieldID(env, cls, "loadObjectList", "Ljava/util/List;"); 1.90 + CHECK_EXCEPTION; 1.91 + 1.92 + // methods we use 1.93 + createClosestSymbol_ID = (*env)->GetMethodID(env, cls, "createClosestSymbol", 1.94 + "(Ljava/lang/String;J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;"); 1.95 + CHECK_EXCEPTION; 1.96 + createLoadObject_ID = (*env)->GetMethodID(env, cls, "createLoadObject", 1.97 + "(Ljava/lang/String;JJ)Lsun/jvm/hotspot/debugger/cdbg/LoadObject;"); 1.98 + CHECK_EXCEPTION; 1.99 + getThreadForThreadId_ID = (*env)->GetMethodID(env, cls, "getThreadForThreadId", 1.100 + "(J)Lsun/jvm/hotspot/debugger/ThreadProxy;"); 1.101 + CHECK_EXCEPTION; 1.102 + // java.util.List method we call 1.103 + listClass = (*env)->FindClass(env, "java/util/List"); 1.104 + CHECK_EXCEPTION; 1.105 + listAdd_ID = (*env)->GetMethodID(env, listClass, "add", "(Ljava/lang/Object;)Z"); 1.106 + CHECK_EXCEPTION; 1.107 +} 1.108 + 1.109 +JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_getAddressSize 1.110 + (JNIEnv *env, jclass cls) 1.111 +{ 1.112 +#ifdef _LP64 1.113 + return 8; 1.114 +#else 1.115 + return 4; 1.116 +#endif 1.117 + 1.118 +} 1.119 + 1.120 + 1.121 +static void fillThreadsAndLoadObjects(JNIEnv* env, jobject this_obj, struct ps_prochandle* ph) { 1.122 + int n = 0, i = 0; 1.123 + 1.124 + // add threads 1.125 + n = get_num_threads(ph); 1.126 + for (i = 0; i < n; i++) { 1.127 + jobject thread; 1.128 + jobject threadList; 1.129 + lwpid_t lwpid; 1.130 + 1.131 + lwpid = get_lwp_id(ph, i); 1.132 + thread = (*env)->CallObjectMethod(env, this_obj, getThreadForThreadId_ID, 1.133 + (jlong)lwpid); 1.134 + CHECK_EXCEPTION; 1.135 + threadList = (*env)->GetObjectField(env, this_obj, threadList_ID); 1.136 + CHECK_EXCEPTION; 1.137 + (*env)->CallBooleanMethod(env, threadList, listAdd_ID, thread); 1.138 + CHECK_EXCEPTION; 1.139 + } 1.140 + 1.141 + // add load objects 1.142 + n = get_num_libs(ph); 1.143 + for (i = 0; i < n; i++) { 1.144 + uintptr_t base; 1.145 + const char* name; 1.146 + jobject loadObject; 1.147 + jobject loadObjectList; 1.148 + 1.149 + base = get_lib_base(ph, i); 1.150 + name = get_lib_name(ph, i); 1.151 + loadObject = (*env)->CallObjectMethod(env, this_obj, createLoadObject_ID, 1.152 + (*env)->NewStringUTF(env, name), (jlong)0, (jlong)base); 1.153 + CHECK_EXCEPTION; 1.154 + loadObjectList = (*env)->GetObjectField(env, this_obj, loadObjectList_ID); 1.155 + CHECK_EXCEPTION; 1.156 + (*env)->CallBooleanMethod(env, loadObjectList, listAdd_ID, loadObject); 1.157 + CHECK_EXCEPTION; 1.158 + } 1.159 +} 1.160 + 1.161 +/* 1.162 + * Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal 1.163 + * Method: attach0 1.164 + * Signature: (I)V 1.165 + */ 1.166 +JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_attach0__I 1.167 + (JNIEnv *env, jobject this_obj, jint jpid) { 1.168 + 1.169 + struct ps_prochandle* ph; 1.170 + if ( (ph = Pgrab(jpid)) == NULL) { 1.171 + THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the process"); 1.172 + } 1.173 + (*env)->SetLongField(env, this_obj, p_ps_prochandle_ID, (jlong)(intptr_t)ph); 1.174 + fillThreadsAndLoadObjects(env, this_obj, ph); 1.175 +} 1.176 + 1.177 +/* 1.178 + * Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal 1.179 + * Method: attach0 1.180 + * Signature: (Ljava/lang/String;Ljava/lang/String;)V 1.181 + */ 1.182 +JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_attach0__Ljava_lang_String_2Ljava_lang_String_2 1.183 + (JNIEnv *env, jobject this_obj, jstring execName, jstring coreName) { 1.184 + const char *execName_cstr; 1.185 + const char *coreName_cstr; 1.186 + jboolean isCopy; 1.187 + struct ps_prochandle* ph; 1.188 + 1.189 + execName_cstr = (*env)->GetStringUTFChars(env, execName, &isCopy); 1.190 + CHECK_EXCEPTION; 1.191 + coreName_cstr = (*env)->GetStringUTFChars(env, coreName, &isCopy); 1.192 + CHECK_EXCEPTION; 1.193 + 1.194 + if ( (ph = Pgrab_core(execName_cstr, coreName_cstr)) == NULL) { 1.195 + (*env)->ReleaseStringUTFChars(env, execName, execName_cstr); 1.196 + (*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr); 1.197 + THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the core file"); 1.198 + } 1.199 + (*env)->SetLongField(env, this_obj, p_ps_prochandle_ID, (jlong)(intptr_t)ph); 1.200 + (*env)->ReleaseStringUTFChars(env, execName, execName_cstr); 1.201 + (*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr); 1.202 + fillThreadsAndLoadObjects(env, this_obj, ph); 1.203 +} 1.204 + 1.205 +/* 1.206 + * Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal 1.207 + * Method: detach0 1.208 + * Signature: ()V 1.209 + */ 1.210 +JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_detach0 1.211 + (JNIEnv *env, jobject this_obj) { 1.212 + struct ps_prochandle* ph = get_proc_handle(env, this_obj); 1.213 + if (ph != NULL) { 1.214 + Prelease(ph); 1.215 + } 1.216 +} 1.217 + 1.218 +/* 1.219 + * Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal 1.220 + * Method: lookupByName0 1.221 + * Signature: (Ljava/lang/String;Ljava/lang/String;)J 1.222 + */ 1.223 +JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_lookupByName0 1.224 + (JNIEnv *env, jobject this_obj, jstring objectName, jstring symbolName) { 1.225 + const char *objectName_cstr, *symbolName_cstr; 1.226 + jlong addr; 1.227 + jboolean isCopy; 1.228 + struct ps_prochandle* ph = get_proc_handle(env, this_obj); 1.229 + 1.230 + objectName_cstr = NULL; 1.231 + if (objectName != NULL) { 1.232 + objectName_cstr = (*env)->GetStringUTFChars(env, objectName, &isCopy); 1.233 + CHECK_EXCEPTION_(0); 1.234 + } 1.235 + symbolName_cstr = (*env)->GetStringUTFChars(env, symbolName, &isCopy); 1.236 + CHECK_EXCEPTION_(0); 1.237 + 1.238 + addr = (jlong) lookup_symbol(ph, objectName_cstr, symbolName_cstr); 1.239 + 1.240 + if (objectName_cstr != NULL) { 1.241 + (*env)->ReleaseStringUTFChars(env, objectName, objectName_cstr); 1.242 + } 1.243 + (*env)->ReleaseStringUTFChars(env, symbolName, symbolName_cstr); 1.244 + return addr; 1.245 +} 1.246 + 1.247 +/* 1.248 + * Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal 1.249 + * Method: lookupByAddress0 1.250 + * Signature: (J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol; 1.251 + */ 1.252 +JNIEXPORT jobject JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_lookupByAddress0 1.253 + (JNIEnv *env, jobject this_obj, jlong addr) { 1.254 + uintptr_t offset; 1.255 + const char* sym = NULL; 1.256 + 1.257 + struct ps_prochandle* ph = get_proc_handle(env, this_obj); 1.258 + sym = symbol_for_pc(ph, (uintptr_t) addr, &offset); 1.259 + if (sym == NULL) return 0; 1.260 + return (*env)->CallObjectMethod(env, this_obj, createClosestSymbol_ID, 1.261 + (*env)->NewStringUTF(env, sym), (jlong)offset); 1.262 +} 1.263 + 1.264 +/* 1.265 + * Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal 1.266 + * Method: readBytesFromProcess0 1.267 + * Signature: (JJ)Lsun/jvm/hotspot/debugger/ReadResult; 1.268 + */ 1.269 +JNIEXPORT jbyteArray JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_readBytesFromProcess0 1.270 + (JNIEnv *env, jobject this_obj, jlong addr, jlong numBytes) { 1.271 + 1.272 + jboolean isCopy; 1.273 + jbyteArray array; 1.274 + jbyte *bufPtr; 1.275 + ps_err_e err; 1.276 + 1.277 + array = (*env)->NewByteArray(env, numBytes); 1.278 + CHECK_EXCEPTION_(0); 1.279 + bufPtr = (*env)->GetByteArrayElements(env, array, &isCopy); 1.280 + CHECK_EXCEPTION_(0); 1.281 + 1.282 + err = ps_pread(get_proc_handle(env, this_obj), (psaddr_t) (uintptr_t)addr, bufPtr, numBytes); 1.283 + (*env)->ReleaseByteArrayElements(env, array, bufPtr, 0); 1.284 + return (err == PS_OK)? array : 0; 1.285 +} 1.286 + 1.287 +JNIEXPORT jlongArray JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_getThreadIntegerRegisterSet0 1.288 + (JNIEnv *env, jobject this_obj, jint lwp_id) { 1.289 + 1.290 + struct reg gregs; 1.291 + jboolean isCopy; 1.292 + jlongArray array; 1.293 + jlong *regs; 1.294 + 1.295 + struct ps_prochandle* ph = get_proc_handle(env, this_obj); 1.296 + if (get_lwp_regs(ph, lwp_id, &gregs) != true) { 1.297 + THROW_NEW_DEBUGGER_EXCEPTION_("get_thread_regs failed for a lwp", 0); 1.298 + } 1.299 + 1.300 +#undef NPRGREG 1.301 +#ifdef i386 1.302 +#define NPRGREG sun_jvm_hotspot_debugger_x86_X86ThreadContext_NPRGREG 1.303 +#endif 1.304 +#ifdef ia64 1.305 +#define NPRGREG IA64_REG_COUNT 1.306 +#endif 1.307 +#ifdef amd64 1.308 +#define NPRGREG sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_NPRGREG 1.309 +#endif 1.310 +#if defined(sparc) || defined(sparcv9) 1.311 +#define NPRGREG sun_jvm_hotspot_debugger_sparc_SPARCThreadContext_NPRGREG 1.312 +#endif 1.313 + 1.314 + array = (*env)->NewLongArray(env, NPRGREG); 1.315 + CHECK_EXCEPTION_(0); 1.316 + regs = (*env)->GetLongArrayElements(env, array, &isCopy); 1.317 + 1.318 +#undef REG_INDEX 1.319 + 1.320 +#ifdef i386 1.321 +#define REG_INDEX(reg) sun_jvm_hotspot_debugger_x86_X86ThreadContext_##reg 1.322 + 1.323 + regs[REG_INDEX(GS)] = (uintptr_t) gregs.r_gs; 1.324 + regs[REG_INDEX(FS)] = (uintptr_t) gregs.r_fs; 1.325 + regs[REG_INDEX(ES)] = (uintptr_t) gregs.r_es; 1.326 + regs[REG_INDEX(DS)] = (uintptr_t) gregs.r_ds; 1.327 + regs[REG_INDEX(EDI)] = (uintptr_t) gregs.r_edi; 1.328 + regs[REG_INDEX(ESI)] = (uintptr_t) gregs.r_esi; 1.329 + regs[REG_INDEX(FP)] = (uintptr_t) gregs.r_ebp; 1.330 + regs[REG_INDEX(SP)] = (uintptr_t) gregs.r_isp; 1.331 + regs[REG_INDEX(EBX)] = (uintptr_t) gregs.r_ebx; 1.332 + regs[REG_INDEX(EDX)] = (uintptr_t) gregs.r_edx; 1.333 + regs[REG_INDEX(ECX)] = (uintptr_t) gregs.r_ecx; 1.334 + regs[REG_INDEX(EAX)] = (uintptr_t) gregs.r_eax; 1.335 + regs[REG_INDEX(PC)] = (uintptr_t) gregs.r_eip; 1.336 + regs[REG_INDEX(CS)] = (uintptr_t) gregs.r_cs; 1.337 + regs[REG_INDEX(SS)] = (uintptr_t) gregs.r_ss; 1.338 + 1.339 +#endif /* i386 */ 1.340 + 1.341 +#if ia64 1.342 + regs = (*env)->GetLongArrayElements(env, array, &isCopy); 1.343 + int i; 1.344 + for (i = 0; i < NPRGREG; i++ ) { 1.345 + regs[i] = 0xDEADDEAD; 1.346 + } 1.347 +#endif /* ia64 */ 1.348 + 1.349 +#ifdef amd64 1.350 +#define REG_INDEX(reg) sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_##reg 1.351 + 1.352 + regs[REG_INDEX(R15)] = gregs.r_r15; 1.353 + regs[REG_INDEX(R14)] = gregs.r_r14; 1.354 + regs[REG_INDEX(R13)] = gregs.r_r13; 1.355 + regs[REG_INDEX(R12)] = gregs.r_r12; 1.356 + regs[REG_INDEX(RBP)] = gregs.r_rbp; 1.357 + regs[REG_INDEX(RBX)] = gregs.r_rbx; 1.358 + regs[REG_INDEX(R11)] = gregs.r_r11; 1.359 + regs[REG_INDEX(R10)] = gregs.r_r10; 1.360 + regs[REG_INDEX(R9)] = gregs.r_r9; 1.361 + regs[REG_INDEX(R8)] = gregs.r_r8; 1.362 + regs[REG_INDEX(RAX)] = gregs.r_rax; 1.363 + regs[REG_INDEX(RCX)] = gregs.r_rcx; 1.364 + regs[REG_INDEX(RDX)] = gregs.r_rdx; 1.365 + regs[REG_INDEX(RSI)] = gregs.r_rsi; 1.366 + regs[REG_INDEX(RDI)] = gregs.r_rdi; 1.367 + regs[REG_INDEX(RIP)] = gregs.r_rip; 1.368 + regs[REG_INDEX(CS)] = gregs.r_cs; 1.369 + regs[REG_INDEX(RSP)] = gregs.r_rsp; 1.370 + regs[REG_INDEX(SS)] = gregs.r_ss; 1.371 +// regs[REG_INDEX(FSBASE)] = gregs.fs_base; 1.372 +// regs[REG_INDEX(GSBASE)] = gregs.gs_base; 1.373 +// regs[REG_INDEX(DS)] = gregs.ds; 1.374 +// regs[REG_INDEX(ES)] = gregs.es; 1.375 +// regs[REG_INDEX(FS)] = gregs.fs; 1.376 +// regs[REG_INDEX(GS)] = gregs.gs; 1.377 + 1.378 +#endif /* amd64 */ 1.379 + 1.380 +#if defined(sparc) || defined(sparcv9) 1.381 + 1.382 +#define REG_INDEX(reg) sun_jvm_hotspot_debugger_sparc_SPARCThreadContext_##reg 1.383 + 1.384 +#ifdef _LP64 1.385 + regs[REG_INDEX(R_PSR)] = gregs.tstate; 1.386 + regs[REG_INDEX(R_PC)] = gregs.tpc; 1.387 + regs[REG_INDEX(R_nPC)] = gregs.tnpc; 1.388 + regs[REG_INDEX(R_Y)] = gregs.y; 1.389 +#else 1.390 + regs[REG_INDEX(R_PSR)] = gregs.psr; 1.391 + regs[REG_INDEX(R_PC)] = gregs.pc; 1.392 + regs[REG_INDEX(R_nPC)] = gregs.npc; 1.393 + regs[REG_INDEX(R_Y)] = gregs.y; 1.394 +#endif 1.395 + regs[REG_INDEX(R_G0)] = 0 ; 1.396 + regs[REG_INDEX(R_G1)] = gregs.u_regs[0]; 1.397 + regs[REG_INDEX(R_G2)] = gregs.u_regs[1]; 1.398 + regs[REG_INDEX(R_G3)] = gregs.u_regs[2]; 1.399 + regs[REG_INDEX(R_G4)] = gregs.u_regs[3]; 1.400 + regs[REG_INDEX(R_G5)] = gregs.u_regs[4]; 1.401 + regs[REG_INDEX(R_G6)] = gregs.u_regs[5]; 1.402 + regs[REG_INDEX(R_G7)] = gregs.u_regs[6]; 1.403 + regs[REG_INDEX(R_O0)] = gregs.u_regs[7]; 1.404 + regs[REG_INDEX(R_O1)] = gregs.u_regs[8]; 1.405 + regs[REG_INDEX(R_O2)] = gregs.u_regs[ 9]; 1.406 + regs[REG_INDEX(R_O3)] = gregs.u_regs[10]; 1.407 + regs[REG_INDEX(R_O4)] = gregs.u_regs[11]; 1.408 + regs[REG_INDEX(R_O5)] = gregs.u_regs[12]; 1.409 + regs[REG_INDEX(R_O6)] = gregs.u_regs[13]; 1.410 + regs[REG_INDEX(R_O7)] = gregs.u_regs[14]; 1.411 +#endif /* sparc */ 1.412 + 1.413 + 1.414 + (*env)->ReleaseLongArrayElements(env, array, regs, JNI_COMMIT); 1.415 + return array; 1.416 +}