1 /* 2 * Copyright (c) 2002, 2019, 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 "salibproc.h" 26 #include "sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal.h" 27 #include <thread_db.h> 28 #include <strings.h> 29 #include <limits.h> 30 #include <demangle.h> 31 #include <stdarg.h> 32 #include <stdlib.h> 33 #include <errno.h> 34 #include "cds.h" 35 36 #define CHECK_EXCEPTION_(value) if(env->ExceptionOccurred()) { return value; } 37 #define CHECK_EXCEPTION if(env->ExceptionOccurred()) { return;} 38 #define THROW_NEW_DEBUGGER_EXCEPTION_(str, value) { throwNewDebuggerException(env, str); return value; } 39 #define THROW_NEW_DEBUGGER_EXCEPTION(str) { throwNewDebuggerException(env, str); return;} 40 41 #define SYMBOL_BUF_SIZE 256 42 #define ERR_MSG_SIZE (PATH_MAX + 256) 43 44 // debug modes 45 static int _libsaproc_debug = 0; 46 47 static void print_debug(const char* format,...) { 48 if (_libsaproc_debug) { 49 va_list alist; 50 51 va_start(alist, format); 52 fputs("libsaproc DEBUG: ", stderr); 53 vfprintf(stderr, format, alist); 54 va_end(alist); 55 } 56 } 57 58 struct Debugger { 59 JNIEnv* env; 60 jobject this_obj; 61 }; 62 63 struct DebuggerWithObject : Debugger { 64 jobject obj; 65 }; 66 67 struct DebuggerWith2Objects : DebuggerWithObject { 68 jobject obj2; 69 }; 70 71 /* 72 * Portions of user thread level detail gathering code is from pstack source 73 * code. See pstack.c in Solaris 2.8 user commands source code. 74 */ 75 76 static void throwNewDebuggerException(JNIEnv* env, const char* errMsg) { 77 jclass clazz = env->FindClass("sun/jvm/hotspot/debugger/DebuggerException"); 78 CHECK_EXCEPTION; 79 env->ThrowNew(clazz, errMsg); 80 } 81 82 // JNI ids for some fields, methods 83 84 // libproc handler pointer 85 static jfieldID p_ps_prochandle_ID = 0; 86 87 // libthread.so dlopen handle, thread agent ptr and function pointers 88 static jfieldID libthread_db_handle_ID = 0; 89 static jfieldID p_td_thragent_t_ID = 0; 90 static jfieldID p_td_init_ID = 0; 91 static jfieldID p_td_ta_new_ID = 0; 92 static jfieldID p_td_ta_delete_ID = 0; 93 static jfieldID p_td_ta_thr_iter_ID = 0; 94 static jfieldID p_td_thr_get_info_ID = 0; 95 static jfieldID p_td_ta_map_id2thr_ID = 0; 96 static jfieldID p_td_thr_getgregs_ID = 0; 97 98 // reg index fields 99 static jfieldID pcRegIndex_ID = 0; 100 static jfieldID fpRegIndex_ID = 0; 101 102 // part of the class sharing workaround 103 static jfieldID classes_jsa_fd_ID = 0; 104 static jfieldID p_file_map_header_ID = 0; 105 106 // method ids 107 108 static jmethodID getThreadForThreadId_ID = 0; 109 static jmethodID createSenderFrame_ID = 0; 110 static jmethodID createLoadObject_ID = 0; 111 static jmethodID createClosestSymbol_ID = 0; 112 static jmethodID listAdd_ID = 0; 113 114 /* 115 * Functions we need from libthread_db 116 */ 117 typedef td_err_e 118 (*p_td_init_t)(void); 119 typedef td_err_e 120 (*p_td_ta_new_t)(void *, td_thragent_t **); 121 typedef td_err_e 122 (*p_td_ta_delete_t)(td_thragent_t *); 123 typedef td_err_e 124 (*p_td_ta_thr_iter_t)(const td_thragent_t *, td_thr_iter_f *, void *, 125 td_thr_state_e, int, sigset_t *, unsigned); 126 typedef td_err_e 127 (*p_td_thr_get_info_t)(const td_thrhandle_t *, td_thrinfo_t *); 128 typedef td_err_e 129 (*p_td_ta_map_id2thr_t)(const td_thragent_t *, thread_t, td_thrhandle_t *); 130 typedef td_err_e 131 (*p_td_thr_getgregs_t)(const td_thrhandle_t *, prgregset_t); 132 133 static void 134 clear_libthread_db_ptrs(JNIEnv* env, jobject this_obj) { 135 // release libthread_db agent, if we had created 136 p_td_ta_delete_t p_td_ta_delete = 0; 137 p_td_ta_delete = (p_td_ta_delete_t) env->GetLongField(this_obj, p_td_ta_delete_ID); 138 139 td_thragent_t *p_td_thragent_t = 0; 140 p_td_thragent_t = (td_thragent_t*) env->GetLongField(this_obj, p_td_thragent_t_ID); 141 if (p_td_thragent_t != 0 && p_td_ta_delete != 0) { 142 p_td_ta_delete(p_td_thragent_t); 143 } 144 145 // dlclose libthread_db.so 146 void* libthread_db_handle = (void*) env->GetLongField(this_obj, libthread_db_handle_ID); 147 if (libthread_db_handle != 0) { 148 dlclose(libthread_db_handle); 149 } 150 151 env->SetLongField(this_obj, libthread_db_handle_ID, (jlong)0); 152 env->SetLongField(this_obj, p_td_init_ID, (jlong)0); 153 env->SetLongField(this_obj, p_td_ta_new_ID, (jlong)0); 154 env->SetLongField(this_obj, p_td_ta_delete_ID, (jlong)0); 155 env->SetLongField(this_obj, p_td_ta_thr_iter_ID, (jlong)0); 156 env->SetLongField(this_obj, p_td_thr_get_info_ID, (jlong)0); 157 env->SetLongField(this_obj, p_td_ta_map_id2thr_ID, (jlong)0); 158 env->SetLongField(this_obj, p_td_thr_getgregs_ID, (jlong)0); 159 } 160 161 162 static void detach_internal(JNIEnv* env, jobject this_obj) { 163 // clear libthread_db stuff 164 clear_libthread_db_ptrs(env, this_obj); 165 166 // release ptr to ps_prochandle 167 jlong p_ps_prochandle; 168 p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); 169 if (p_ps_prochandle != 0L) { 170 Prelease((struct ps_prochandle*) p_ps_prochandle, PRELEASE_CLEAR); 171 } 172 173 // part of the class sharing workaround 174 int classes_jsa_fd = env->GetIntField(this_obj, classes_jsa_fd_ID); 175 if (classes_jsa_fd != -1) { 176 close(classes_jsa_fd); 177 CDSFileMapHeaderBase* pheader = (CDSFileMapHeaderBase*) env->GetLongField(this_obj, p_file_map_header_ID); 178 if (pheader != NULL) { 179 free(pheader); 180 } 181 } 182 } 183 184 // Is it okay to ignore libthread_db failure? Set env var to ignore 185 // libthread_db failure. You can still debug, but will miss threads 186 // related functionality. 187 static bool sa_ignore_threaddb = (getenv("SA_IGNORE_THREADDB") != 0); 188 189 #define HANDLE_THREADDB_FAILURE(msg) \ 190 if (sa_ignore_threaddb) { \ 191 printf("libsaproc WARNING: %s\n", msg); \ 192 return; \ 193 } else { \ 194 THROW_NEW_DEBUGGER_EXCEPTION(msg); \ 195 } 196 197 #define HANDLE_THREADDB_FAILURE_(msg, ret) \ 198 if (sa_ignore_threaddb) { \ 199 printf("libsaproc WARNING: %s\n", msg); \ 200 return ret; \ 201 } else { \ 202 THROW_NEW_DEBUGGER_EXCEPTION_(msg, ret); \ 203 } 204 205 static const char * alt_root = NULL; 206 static int alt_root_len = -1; 207 208 #define SA_ALTROOT "SA_ALTROOT" 209 210 static void init_alt_root() { 211 if (alt_root_len == -1) { 212 alt_root = getenv(SA_ALTROOT); 213 if (alt_root) 214 alt_root_len = strlen(alt_root); 215 else 216 alt_root_len = 0; 217 } 218 } 219 220 // This function is a complete substitute for the open system call 221 // since it's also used to override open calls from libproc to 222 // implement as a pathmap style facility for the SA. If libproc 223 // starts using other interfaces then this might have to extended to 224 // cover other calls. 225 extern "C" JNIEXPORT int JNICALL 226 libsaproc_open(const char * name, int oflag, ...) { 227 if (oflag == O_RDONLY) { 228 init_alt_root(); 229 230 if (_libsaproc_debug) { 231 printf("libsaproc DEBUG: libsaproc_open %s\n", name); 232 } 233 234 if (alt_root_len > 0) { 235 int fd = -1; 236 char alt_path[PATH_MAX+1]; 237 238 strcpy(alt_path, alt_root); 239 strcat(alt_path, name); 240 fd = open(alt_path, O_RDONLY); 241 if (fd >= 0) { 242 if (_libsaproc_debug) { 243 printf("libsaproc DEBUG: libsaproc_open substituted %s\n", alt_path); 244 } 245 return fd; 246 } 247 248 if (strrchr(name, '/')) { 249 strcpy(alt_path, alt_root); 250 strcat(alt_path, strrchr(name, '/')); 251 fd = open(alt_path, O_RDONLY); 252 if (fd >= 0) { 253 if (_libsaproc_debug) { 254 printf("libsaproc DEBUG: libsaproc_open substituted %s\n", alt_path); 255 } 256 return fd; 257 } 258 } 259 } 260 } 261 262 { 263 mode_t mode; 264 va_list ap; 265 va_start(ap, oflag); 266 mode = va_arg(ap, mode_t); 267 va_end(ap); 268 269 return open(name, oflag, mode); 270 } 271 } 272 273 274 static void * pathmap_dlopen(const char * name, int mode) { 275 init_alt_root(); 276 277 if (_libsaproc_debug) { 278 printf("libsaproc DEBUG: pathmap_dlopen %s\n", name); 279 } 280 281 void * handle = NULL; 282 if (alt_root_len > 0) { 283 char alt_path[PATH_MAX+1]; 284 strcpy(alt_path, alt_root); 285 strcat(alt_path, name); 286 handle = dlopen(alt_path, mode); 287 if (_libsaproc_debug && handle) { 288 printf("libsaproc DEBUG: pathmap_dlopen substituted %s\n", alt_path); 289 } 290 291 if (handle == NULL && strrchr(name, '/')) { 292 strcpy(alt_path, alt_root); 293 strcat(alt_path, strrchr(name, '/')); 294 handle = dlopen(alt_path, mode); 295 if (_libsaproc_debug && handle) { 296 printf("libsaproc DEBUG: pathmap_dlopen substituted %s\n", alt_path); 297 } 298 } 299 } 300 if (handle == NULL) { 301 handle = dlopen(name, mode); 302 } 303 if (_libsaproc_debug) { 304 printf("libsaproc DEBUG: pathmap_dlopen %s return 0x%lx\n", name, (unsigned long) handle); 305 } 306 return handle; 307 } 308 309 // libproc and libthread_db callback functions 310 311 extern "C" { 312 313 static int 314 init_libthread_db_ptrs(void *cd, const prmap_t *pmp, const char *object_name) { 315 Debugger* dbg = (Debugger*) cd; 316 JNIEnv* env = dbg->env; 317 jobject this_obj = dbg->this_obj; 318 struct ps_prochandle* ph = (struct ps_prochandle*) env->GetLongField(this_obj, p_ps_prochandle_ID); 319 320 char *s1 = 0, *s2 = 0; 321 char libthread_db[PATH_MAX]; 322 323 if (strstr(object_name, "/libthread.so.") == NULL) 324 return (0); 325 326 /* 327 * We found a libthread. 328 * dlopen() the matching libthread_db and get the thread agent handle. 329 */ 330 if (Pstatus(ph)->pr_dmodel == PR_MODEL_NATIVE) { 331 (void) strcpy(libthread_db, object_name); 332 s1 = (char*) strstr(object_name, ".so."); 333 s2 = (char*) strstr(libthread_db, ".so."); 334 (void) strcpy(s2, "_db"); 335 s2 += 3; 336 (void) strcpy(s2, s1); 337 } else { 338 #ifdef _LP64 339 /* 340 * The victim process is 32-bit, we are 64-bit. 341 * We have to find the 64-bit version of libthread_db 342 * that matches the victim's 32-bit version of libthread. 343 */ 344 (void) strcpy(libthread_db, object_name); 345 s1 = (char*) strstr(object_name, "/libthread.so."); 346 s2 = (char*) strstr(libthread_db, "/libthread.so."); 347 (void) strcpy(s2, "/64"); 348 s2 += 3; 349 (void) strcpy(s2, s1); 350 s1 = (char*) strstr(s1, ".so."); 351 s2 = (char*) strstr(s2, ".so."); 352 (void) strcpy(s2, "_db"); 353 s2 += 3; 354 (void) strcpy(s2, s1); 355 #else 356 return (0); 357 #endif /* _LP64 */ 358 } 359 360 void* libthread_db_handle = 0; 361 if ((libthread_db_handle = pathmap_dlopen(libthread_db, RTLD_LAZY|RTLD_LOCAL)) == NULL) { 362 char errMsg[PATH_MAX + 256]; 363 sprintf(errMsg, "Can't load %s!", libthread_db); 364 HANDLE_THREADDB_FAILURE_(errMsg, 0); 365 } 366 env->SetLongField(this_obj, libthread_db_handle_ID, (jlong)(uintptr_t)libthread_db_handle); 367 368 void* tmpPtr = 0; 369 tmpPtr = dlsym(libthread_db_handle, "td_init"); 370 if (tmpPtr == 0) { 371 HANDLE_THREADDB_FAILURE_("dlsym failed on td_init!", 0); 372 } 373 env->SetLongField(this_obj, p_td_init_ID, (jlong)(uintptr_t) tmpPtr); 374 375 tmpPtr =dlsym(libthread_db_handle, "td_ta_new"); 376 if (tmpPtr == 0) { 377 HANDLE_THREADDB_FAILURE_("dlsym failed on td_ta_new!", 0); 378 } 379 env->SetLongField(this_obj, p_td_ta_new_ID, (jlong)(uintptr_t) tmpPtr); 380 381 tmpPtr = dlsym(libthread_db_handle, "td_ta_delete"); 382 if (tmpPtr == 0) { 383 HANDLE_THREADDB_FAILURE_("dlsym failed on td_ta_delete!", 0); 384 } 385 env->SetLongField(this_obj, p_td_ta_delete_ID, (jlong)(uintptr_t) tmpPtr); 386 387 tmpPtr = dlsym(libthread_db_handle, "td_ta_thr_iter"); 388 if (tmpPtr == 0) { 389 HANDLE_THREADDB_FAILURE_("dlsym failed on td_ta_thr_iter!", 0); 390 } 391 env->SetLongField(this_obj, p_td_ta_thr_iter_ID, (jlong)(uintptr_t) tmpPtr); 392 393 tmpPtr = dlsym(libthread_db_handle, "td_thr_get_info"); 394 if (tmpPtr == 0) { 395 HANDLE_THREADDB_FAILURE_("dlsym failed on td_thr_get_info!", 0); 396 } 397 env->SetLongField(this_obj, p_td_thr_get_info_ID, (jlong)(uintptr_t) tmpPtr); 398 399 tmpPtr = dlsym(libthread_db_handle, "td_ta_map_id2thr"); 400 if (tmpPtr == 0) { 401 HANDLE_THREADDB_FAILURE_("dlsym failed on td_ta_map_id2thr!", 0); 402 } 403 env->SetLongField(this_obj, p_td_ta_map_id2thr_ID, (jlong)(uintptr_t) tmpPtr); 404 405 tmpPtr = dlsym(libthread_db_handle, "td_thr_getgregs"); 406 if (tmpPtr == 0) { 407 HANDLE_THREADDB_FAILURE_("dlsym failed on td_thr_getgregs!", 0); 408 } 409 env->SetLongField(this_obj, p_td_thr_getgregs_ID, (jlong)(uintptr_t) tmpPtr); 410 411 return 1; 412 } 413 414 static int 415 fill_thread_list(const td_thrhandle_t *p_td_thragent_t, void* cd) { 416 DebuggerWithObject* dbgo = (DebuggerWithObject*) cd; 417 JNIEnv* env = dbgo->env; 418 jobject this_obj = dbgo->this_obj; 419 jobject list = dbgo->obj; 420 421 td_thrinfo_t thrinfo; 422 p_td_thr_get_info_t p_td_thr_get_info = (p_td_thr_get_info_t) env->GetLongField(this_obj, p_td_thr_get_info_ID); 423 424 if (p_td_thr_get_info(p_td_thragent_t, &thrinfo) != TD_OK) 425 return (0); 426 427 jobject threadProxy = env->CallObjectMethod(this_obj, getThreadForThreadId_ID, (jlong)(uintptr_t) thrinfo.ti_tid); 428 CHECK_EXCEPTION_(1); 429 env->CallBooleanMethod(list, listAdd_ID, threadProxy); 430 CHECK_EXCEPTION_(1); 431 return 0; 432 } 433 434 static int 435 fill_load_object_list(void *cd, const prmap_t* pmp, const char* obj_name) { 436 437 if (obj_name) { 438 DebuggerWithObject* dbgo = (DebuggerWithObject*) cd; 439 JNIEnv* env = dbgo->env; 440 jobject this_obj = dbgo->this_obj; 441 jobject list = dbgo->obj; 442 443 jstring objectName = env->NewStringUTF(obj_name); 444 CHECK_EXCEPTION_(1); 445 446 jlong mapSize = (jlong) pmp->pr_size; 447 jobject sharedObject = env->CallObjectMethod(this_obj, createLoadObject_ID, 448 objectName, mapSize, (jlong)(uintptr_t)pmp->pr_vaddr); 449 CHECK_EXCEPTION_(1); 450 env->CallBooleanMethod(list, listAdd_ID, sharedObject); 451 CHECK_EXCEPTION_(1); 452 } 453 454 return 0; 455 } 456 457 // Pstack_iter() proc_stack_f callback prior to Nevada-B159 458 static int 459 fill_cframe_list(void *cd, const prgregset_t regs, uint_t argc, const long *argv) { 460 DebuggerWith2Objects* dbgo2 = (DebuggerWith2Objects*) cd; 461 JNIEnv* env = dbgo2->env; 462 jobject this_obj = dbgo2->this_obj; 463 jobject curFrame = dbgo2->obj2; 464 465 jint pcRegIndex = env->GetIntField(this_obj, pcRegIndex_ID); 466 jint fpRegIndex = env->GetIntField(this_obj, fpRegIndex_ID); 467 468 jlong pc = (jlong) (uintptr_t) regs[pcRegIndex]; 469 jlong fp = (jlong) (uintptr_t) regs[fpRegIndex]; 470 471 dbgo2->obj2 = env->CallObjectMethod(this_obj, createSenderFrame_ID, 472 curFrame, pc, fp); 473 CHECK_EXCEPTION_(1); 474 if (dbgo2->obj == 0) { 475 dbgo2->obj = dbgo2->obj2; 476 } 477 return 0; 478 } 479 480 // Pstack_iter() proc_stack_f callback in Nevada-B159 or later 481 /*ARGSUSED*/ 482 static int 483 wrapper_fill_cframe_list(void *cd, const prgregset_t regs, uint_t argc, 484 const long *argv, int frame_flags, int sig) { 485 return(fill_cframe_list(cd, regs, argc, argv)); 486 } 487 488 //--------------------------------------------------------------- 489 // Part of the class sharing workaround: 490 // 491 // With class sharing, pages are mapped from classes.jsa file. 492 // The read-only class sharing pages are mapped as MAP_SHARED, 493 // PROT_READ pages. These pages are not dumped into core dump. 494 // With this workaround, these pages are read from classes.jsa. 495 496 static bool 497 read_jboolean(struct ps_prochandle* ph, psaddr_t addr, jboolean* pvalue) { 498 jboolean i; 499 if (ps_pread(ph, addr, &i, sizeof(i)) == PS_OK) { 500 *pvalue = i; 501 return true; 502 } else { 503 return false; 504 } 505 } 506 507 static bool 508 read_pointer(struct ps_prochandle* ph, psaddr_t addr, uintptr_t* pvalue) { 509 uintptr_t uip; 510 if (ps_pread(ph, addr, &uip, sizeof(uip)) == PS_OK) { 511 *pvalue = uip; 512 return true; 513 } else { 514 return false; 515 } 516 } 517 518 static bool 519 read_string(struct ps_prochandle* ph, psaddr_t addr, char* buf, size_t size) { 520 char ch = ' '; 521 size_t i = 0; 522 523 while (ch != '\0') { 524 if (ps_pread(ph, addr, &ch, sizeof(ch)) != PS_OK) 525 return false; 526 527 if (i < size - 1) { 528 buf[i] = ch; 529 } else { // smaller buffer 530 return false; 531 } 532 533 i++; addr++; 534 } 535 536 buf[i] = '\0'; 537 return true; 538 } 539 540 #define USE_SHARED_SPACES_SYM "UseSharedSpaces" 541 #define SHARED_BASE_ADDRESS_SYM "SharedBaseAddress" 542 // mangled symbol name for Arguments::SharedArchivePath 543 #define SHARED_ARCHIVE_PATH_SYM "__1cJArgumentsRSharedArchivePath_" 544 545 static uintptr_t sharedBaseAddress = 0; 546 static int 547 init_classsharing_workaround(void *cd, const prmap_t* pmap, const char* obj_name) { 548 Debugger* dbg = (Debugger*) cd; 549 JNIEnv* env = dbg->env; 550 jobject this_obj = dbg->this_obj; 551 const char* jvm_name = 0; 552 if ((jvm_name = strstr(obj_name, "libjvm.so")) != NULL) { 553 jvm_name = obj_name; 554 } else { 555 return 0; 556 } 557 558 struct ps_prochandle* ph = (struct ps_prochandle*) env->GetLongField(this_obj, p_ps_prochandle_ID); 559 560 // initialize classes.jsa file descriptor field. 561 dbg->env->SetIntField(this_obj, classes_jsa_fd_ID, -1); 562 563 // check whether class sharing is on by reading variable "UseSharedSpaces" 564 psaddr_t useSharedSpacesAddr = 0; 565 ps_pglobal_lookup(ph, jvm_name, USE_SHARED_SPACES_SYM, &useSharedSpacesAddr); 566 if (useSharedSpacesAddr == 0) { 567 THROW_NEW_DEBUGGER_EXCEPTION_("can't find 'UseSharedSpaces' flag\n", 1); 568 } 569 570 // read the value of the flag "UseSharedSpaces" 571 // Since hotspot types are not available to build this library. So 572 // equivalent type "jboolean" is used to read the value of "UseSharedSpaces" 573 // which is same as hotspot type "bool". 574 jboolean value = 0; 575 if (read_jboolean(ph, useSharedSpacesAddr, &value) != true) { 576 THROW_NEW_DEBUGGER_EXCEPTION_("can't read 'UseSharedSpaces' flag", 1); 577 } else if ((int)value == 0) { 578 print_debug("UseSharedSpaces is false, assuming -Xshare:off!\n"); 579 return 1; 580 } 581 582 psaddr_t sharedBaseAddressAddr = 0; 583 ps_pglobal_lookup(ph, jvm_name, SHARED_ARCHIVE_PATH_SYM, &sharedBaseAddressAddr); 584 if (sharedBaseAddressAddr == 0) { 585 print_debug("can't find symbol 'SharedBaseAddress'\n"); 586 THROW_NEW_DEBUGGER_EXCEPTION_("can't find 'SharedBaseAddress' flag\n", 1); 587 } 588 589 sharedBaseAddress = 0; 590 if (read_pointer(ph, sharedBaseAddressAddr, &sharedBaseAddress) != true) { 591 print_debug("can't read the value of 'SharedBaseAddress' flag\n"); 592 THROW_NEW_DEBUGGER_EXCEPTION_("can't get SharedBaseAddress from debuggee", 1); 593 } 594 595 char classes_jsa[PATH_MAX]; 596 psaddr_t sharedArchivePathAddrAddr = 0; 597 ps_pglobal_lookup(ph, jvm_name, SHARED_ARCHIVE_PATH_SYM, &sharedArchivePathAddrAddr); 598 if (sharedArchivePathAddrAddr == 0) { 599 print_debug("can't find symbol 'Arguments::SharedArchivePath'\n"); 600 THROW_NEW_DEBUGGER_EXCEPTION_("can't get shared archive path from debuggee", 1); 601 } 602 603 uintptr_t sharedArchivePathAddr = 0; 604 if (read_pointer(ph, sharedArchivePathAddrAddr, &sharedArchivePathAddr) != true) { 605 print_debug("can't find read pointer 'Arguments::SharedArchivePath'\n"); 606 THROW_NEW_DEBUGGER_EXCEPTION_("can't get shared archive path from debuggee", 1); 607 } 608 609 if (read_string(ph, (psaddr_t)sharedArchivePathAddr, classes_jsa, sizeof(classes_jsa)) != true) { 610 print_debug("can't find read 'Arguments::SharedArchivePath' value\n"); 611 THROW_NEW_DEBUGGER_EXCEPTION_("can't get shared archive path from debuggee", 1); 612 } 613 614 print_debug("looking for %s\n", classes_jsa); 615 616 // open the classes.jsa 617 int fd = libsaproc_open(classes_jsa, O_RDONLY); 618 if (fd < 0) { 619 char errMsg[ERR_MSG_SIZE]; 620 sprintf(errMsg, "can't open shared archive file %s", classes_jsa); 621 THROW_NEW_DEBUGGER_EXCEPTION_(errMsg, 1); 622 } else { 623 print_debug("opened shared archive file %s\n", classes_jsa); 624 } 625 626 // parse classes.jsa 627 CDSFileMapHeaderBase* pheader = (CDSFileMapHeaderBase*) malloc(sizeof(CDSFileMapHeaderBase)); 628 if (pheader == NULL) { 629 close(fd); 630 THROW_NEW_DEBUGGER_EXCEPTION_("can't allocate memory for shared file map header", 1); 631 } 632 633 memset(pheader, 0, sizeof(CDSFileMapHeaderBase)); 634 // read CDSFileMapHeaderBase 635 size_t n = read(fd, pheader, sizeof(CDSFileMapHeaderBase)); 636 if (n != sizeof(CDSFileMapHeaderBase)) { 637 char errMsg[ERR_MSG_SIZE]; 638 sprintf(errMsg, "unable to read shared archive file map header from %s", classes_jsa); 639 close(fd); 640 free(pheader); 641 THROW_NEW_DEBUGGER_EXCEPTION_(errMsg, 1); 642 } 643 644 // check file magic 645 if (pheader->_magic != CDS_ARCHIVE_MAGIC) { 646 char errMsg[ERR_MSG_SIZE]; 647 sprintf(errMsg, "%s has bad shared archive magic 0x%x, expecting 0x%x", 648 classes_jsa, pheader->_magic, CDS_ARCHIVE_MAGIC); 649 close(fd); 650 free(pheader); 651 THROW_NEW_DEBUGGER_EXCEPTION_(errMsg, 1); 652 } 653 654 // check version 655 if (pheader->_version != CURRENT_CDS_ARCHIVE_VERSION) { 656 char errMsg[ERR_MSG_SIZE]; 657 sprintf(errMsg, "%s has wrong shared archive version %d, expecting %d", 658 classes_jsa, pheader->_version, CURRENT_CDS_ARCHIVE_VERSION); 659 close(fd); 660 free(pheader); 661 THROW_NEW_DEBUGGER_EXCEPTION_(errMsg, 1); 662 } 663 664 if (_libsaproc_debug) { 665 for (int m = 0; m < NUM_CDS_REGIONS; m++) { 666 jlong mapping_offset = pheader->_space[m]._mapping_offset; 667 jlong baseAddress = (mapping_offset == 0) ? 0 : (mapping_offset + (jlong)sharedBaseAddress); 668 print_debug("shared file offset %d mapped at 0x%lx, size = %ld, read only? = %d\n", 669 pheader->_space[m]._file_offset, baseAddress, 670 pheader->_space[m]._used, pheader->_space[m]._read_only); 671 } 672 } 673 674 // FIXME: For now, omitting other checks such as VM version etc. 675 676 // store class archive file fd and map header in debugger object fields 677 dbg->env->SetIntField(this_obj, classes_jsa_fd_ID, fd); 678 dbg->env->SetLongField(this_obj, p_file_map_header_ID, (jlong)(uintptr_t) pheader); 679 return 1; 680 } 681 682 } // extern "C" 683 684 // error messages for proc_arg_grab failure codes. The messages are 685 // modified versions of comments against corresponding #defines in 686 // libproc.h. 687 static const char* proc_arg_grab_errmsgs[] = { 688 "", 689 /* G_NOPROC */ "No such process", 690 /* G_NOCORE */ "No such core file", 691 /* G_NOPROCORCORE */ "No such process or core", 692 /* G_NOEXEC */ "Cannot locate executable file", 693 /* G_ZOMB */ "Zombie processs", 694 /* G_PERM */ "No permission to attach", 695 /* G_BUSY */ "Another process has already attached", 696 /* G_SYS */ "System process - can not attach", 697 /* G_SELF */ "Process is self - can't debug myself!", 698 /* G_INTR */ "Interrupt received while grabbing", 699 /* G_LP64 */ "debuggee is 64 bit, use java -d64 for debugger", 700 /* G_FORMAT */ "File is not an ELF format core file - corrupted core?", 701 /* G_ELF */ "Libelf error while parsing an ELF file", 702 /* G_NOTE */ "Required PT_NOTE Phdr not present - corrupted core?", 703 }; 704 705 static void attach_internal(JNIEnv* env, jobject this_obj, jstring cmdLine, jboolean isProcess) { 706 jboolean isCopy; 707 int gcode; 708 const char* cmdLine_cstr = env->GetStringUTFChars(cmdLine, &isCopy); 709 char errMsg[ERR_MSG_SIZE]; 710 td_err_e te; 711 CHECK_EXCEPTION; 712 if (cmdLine_cstr == NULL) { 713 return; 714 } 715 716 // some older versions of libproc.so crash when trying to attach 32 bit 717 // debugger to 64 bit core file. check and throw error. 718 #ifndef _LP64 719 errno = 0; 720 strtol(cmdLine_cstr, NULL, 10); 721 if (errno) { 722 // core file 723 int core_fd; 724 if ((core_fd = open64(cmdLine_cstr, O_RDONLY)) >= 0) { 725 Elf32_Ehdr e32; 726 if (pread64(core_fd, &e32, sizeof (e32), 0) == sizeof (e32) && 727 memcmp(&e32.e_ident[EI_MAG0], ELFMAG, SELFMAG) == 0 && 728 e32.e_type == ET_CORE && e32.e_ident[EI_CLASS] == ELFCLASS64) { 729 close(core_fd); 730 env->ReleaseStringUTFChars(cmdLine, cmdLine_cstr); 731 THROW_NEW_DEBUGGER_EXCEPTION("debuggee is 64 bit, use java -d64 for debugger"); 732 } 733 close(core_fd); 734 } 735 // all other conditions are handled by libproc.so. 736 } 737 #endif 738 739 // connect to process/core 740 ps_prochandle_t* ph = proc_arg_grab(cmdLine_cstr, (isProcess? PR_ARG_PIDS : PR_ARG_CORES), PGRAB_FORCE, &gcode, NULL); 741 742 env->ReleaseStringUTFChars(cmdLine, cmdLine_cstr); 743 744 if (! ph) { 745 if (gcode > 0 && gcode < sizeof(proc_arg_grab_errmsgs)/sizeof(const char*)) { 746 snprintf(errMsg, ERR_MSG_SIZE, "Attach failed : %s", proc_arg_grab_errmsgs[gcode]); 747 THROW_NEW_DEBUGGER_EXCEPTION(errMsg); 748 } else { 749 if (_libsaproc_debug && gcode == G_STRANGE) { 750 perror("libsaproc DEBUG: "); 751 } 752 if (isProcess) { 753 THROW_NEW_DEBUGGER_EXCEPTION("Not able to attach to process!"); 754 } else { 755 THROW_NEW_DEBUGGER_EXCEPTION("Not able to attach to core file!"); 756 } 757 } 758 } 759 760 // even though libproc.so supports 64 bit debugger and 32 bit debuggee, we don't 761 // support such cross-bit-debugging. check for that combination and throw error. 762 #ifdef _LP64 763 int data_model; 764 if (ps_pdmodel(ph, &data_model) != PS_OK) { 765 Prelease(ph, PRELEASE_CLEAR); 766 THROW_NEW_DEBUGGER_EXCEPTION("can't determine debuggee data model (ILP32? or LP64?)"); 767 } 768 if (data_model == PR_MODEL_ILP32) { 769 Prelease(ph, PRELEASE_CLEAR); 770 THROW_NEW_DEBUGGER_EXCEPTION("debuggee is 32 bit, use 32 bit java for debugger"); 771 } 772 #endif 773 774 env->SetLongField(this_obj, p_ps_prochandle_ID, (jlong)(uintptr_t)ph); 775 776 Debugger dbg; 777 dbg.env = env; 778 dbg.this_obj = this_obj; 779 jthrowable exception = 0; 780 if (! isProcess) { 781 /* 782 * With class sharing, shared perm. gen heap is allocated in with MAP_SHARED|PROT_READ. 783 * These pages are mapped from the file "classes.jsa". MAP_SHARED pages are not dumped 784 * in Solaris core.To read shared heap pages, we have to read classes.jsa file. 785 */ 786 Pobject_iter(ph, init_classsharing_workaround, &dbg); 787 exception = env->ExceptionOccurred(); 788 if (exception) { 789 env->ExceptionClear(); 790 detach_internal(env, this_obj); 791 env->Throw(exception); 792 return; 793 } 794 } 795 796 /* 797 * Iterate over the process mappings looking 798 * for libthread and then dlopen the appropriate 799 * libthread_db and get function pointers. 800 */ 801 Pobject_iter(ph, init_libthread_db_ptrs, &dbg); 802 exception = env->ExceptionOccurred(); 803 if (exception) { 804 env->ExceptionClear(); 805 if (!sa_ignore_threaddb) { 806 detach_internal(env, this_obj); 807 env->Throw(exception); 808 } 809 return; 810 } 811 812 // init libthread_db and create thread_db agent 813 p_td_init_t p_td_init = (p_td_init_t) env->GetLongField(this_obj, p_td_init_ID); 814 if (p_td_init == 0) { 815 if (!sa_ignore_threaddb) { 816 detach_internal(env, this_obj); 817 } 818 HANDLE_THREADDB_FAILURE("Did not find libthread in target process/core!"); 819 } 820 821 te = p_td_init(); 822 if (te != TD_OK) { 823 if (!sa_ignore_threaddb) { 824 detach_internal(env, this_obj); 825 } 826 snprintf(errMsg, ERR_MSG_SIZE, "Can't initialize thread_db! td_init failed: %d", te); 827 HANDLE_THREADDB_FAILURE(errMsg); 828 } 829 830 p_td_ta_new_t p_td_ta_new = (p_td_ta_new_t) env->GetLongField(this_obj, p_td_ta_new_ID); 831 832 td_thragent_t *p_td_thragent_t = 0; 833 te = p_td_ta_new(ph, &p_td_thragent_t); 834 if (te != TD_OK) { 835 if (!sa_ignore_threaddb) { 836 detach_internal(env, this_obj); 837 } 838 snprintf(errMsg, ERR_MSG_SIZE, "Can't create thread_db agent! td_ta_new failed: %d", te); 839 HANDLE_THREADDB_FAILURE(errMsg); 840 } 841 env->SetLongField(this_obj, p_td_thragent_t_ID, (jlong)(uintptr_t) p_td_thragent_t); 842 843 } 844 845 /* 846 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 847 * Method: attach0 848 * Signature: (Ljava/lang/String;)V 849 * Description: process detach 850 */ 851 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_attach0__Ljava_lang_String_2 852 (JNIEnv *env, jobject this_obj, jstring pid) { 853 attach_internal(env, this_obj, pid, JNI_TRUE); 854 } 855 856 /* 857 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 858 * Method: attach0 859 * Signature: (Ljava/lang/String;Ljava/lang/String;)V 860 * Description: core file detach 861 */ 862 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_attach0__Ljava_lang_String_2Ljava_lang_String_2 863 (JNIEnv *env, jobject this_obj, jstring executable, jstring corefile) { 864 // ignore executable file name, libproc.so can detect a.out name anyway. 865 attach_internal(env, this_obj, corefile, JNI_FALSE); 866 } 867 868 869 /* 870 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 871 * Method: detach0 872 * Signature: ()V 873 * Description: process/core file detach 874 */ 875 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_detach0 876 (JNIEnv *env, jobject this_obj) { 877 detach_internal(env, this_obj); 878 } 879 880 /* 881 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 882 * Method: getRemoteProcessAddressSize0 883 * Signature: ()I 884 * Description: get process/core address size 885 */ 886 JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_getRemoteProcessAddressSize0 887 (JNIEnv *env, jobject this_obj) { 888 jlong p_ps_prochandle; 889 p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); 890 int data_model = PR_MODEL_ILP32; 891 ps_pdmodel((struct ps_prochandle*) p_ps_prochandle, &data_model); 892 print_debug("debuggee is %d bit\n", data_model == PR_MODEL_ILP32? 32 : 64); 893 return (jint) data_model == PR_MODEL_ILP32? 32 : 64; 894 } 895 896 /* 897 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 898 * Method: getPageSize0 899 * Signature: ()I 900 * Description: get process/core page size 901 */ 902 JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_getPageSize0 903 (JNIEnv *env, jobject this_obj) { 904 905 /* 906 We are not yet attached to a java process or core file. getPageSize is called from 907 the constructor of ProcDebuggerLocal. The following won't work! 908 909 jlong p_ps_prochandle; 910 p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); 911 CHECK_EXCEPTION_(-1); 912 struct ps_prochandle* prochandle = (struct ps_prochandle*) p_ps_prochandle; 913 return (Pstate(prochandle) == PS_DEAD) ? Pgetauxval(prochandle, AT_PAGESZ) 914 : getpagesize(); 915 916 So even though core may have been generated with a different page size settings, for now 917 call getpagesize. 918 */ 919 920 return getpagesize(); 921 } 922 923 /* 924 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 925 * Method: getThreadIntegerRegisterSet0 926 * Signature: (J)[J 927 * Description: get gregset for a given thread specified by thread id 928 */ 929 JNIEXPORT jlongArray JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_getThreadIntegerRegisterSet0 930 (JNIEnv *env, jobject this_obj, jlong tid) { 931 char errMsg[ERR_MSG_SIZE]; 932 td_err_e te; 933 // map the thread id to thread handle 934 p_td_ta_map_id2thr_t p_td_ta_map_id2thr = (p_td_ta_map_id2thr_t) env->GetLongField(this_obj, p_td_ta_map_id2thr_ID); 935 936 td_thragent_t* p_td_thragent_t = (td_thragent_t*) env->GetLongField(this_obj, p_td_thragent_t_ID); 937 if (p_td_thragent_t == 0) { 938 return 0; 939 } 940 941 td_thrhandle_t thr_handle; 942 te = p_td_ta_map_id2thr(p_td_thragent_t, (thread_t) tid, &thr_handle); 943 if (te != TD_OK) { 944 snprintf(errMsg, ERR_MSG_SIZE, "can't map thread id to thread handle! td_ta_map_id2thr failed: %d", te); 945 THROW_NEW_DEBUGGER_EXCEPTION_(errMsg, 0); 946 } 947 948 p_td_thr_getgregs_t p_td_thr_getgregs = (p_td_thr_getgregs_t) env->GetLongField(this_obj, p_td_thr_getgregs_ID); 949 prgregset_t gregs; 950 p_td_thr_getgregs(&thr_handle, gregs); 951 952 jlongArray res = env->NewLongArray(NPRGREG); 953 CHECK_EXCEPTION_(0); 954 jboolean isCopy; 955 jlong* ptr = env->GetLongArrayElements(res, &isCopy); 956 CHECK_EXCEPTION_(NULL); 957 for (int i = 0; i < NPRGREG; i++) { 958 ptr[i] = (jlong) (uintptr_t) gregs[i]; 959 } 960 env->ReleaseLongArrayElements(res, ptr, JNI_COMMIT); 961 return res; 962 } 963 964 /* 965 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 966 * Method: fillThreadList0 967 * Signature: (Ljava/util/List;)V 968 * Description: fills thread list of the debuggee process/core 969 */ 970 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_fillThreadList0 971 (JNIEnv *env, jobject this_obj, jobject list) { 972 973 td_thragent_t* p_td_thragent_t = (td_thragent_t*) env->GetLongField(this_obj, p_td_thragent_t_ID); 974 if (p_td_thragent_t == 0) { 975 return; 976 } 977 978 p_td_ta_thr_iter_t p_td_ta_thr_iter = (p_td_ta_thr_iter_t) env->GetLongField(this_obj, p_td_ta_thr_iter_ID); 979 980 DebuggerWithObject dbgo; 981 dbgo.env = env; 982 dbgo.this_obj = this_obj; 983 dbgo.obj = list; 984 985 p_td_ta_thr_iter(p_td_thragent_t, fill_thread_list, &dbgo, 986 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY, TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS); 987 } 988 989 /* 990 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 991 * Method: fillCFrameList0 992 * Signature: ([J)Lsun/jvm/hotspot/debugger/proc/ProcCFrame; 993 * Description: fills CFrame list for a given thread 994 */ 995 JNIEXPORT jobject JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_fillCFrameList0 996 (JNIEnv *env, jobject this_obj, jlongArray regsArray) { 997 jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); 998 999 DebuggerWith2Objects dbgo2; 1000 dbgo2.env = env; 1001 dbgo2.this_obj = this_obj; 1002 dbgo2.obj = NULL; 1003 dbgo2.obj2 = NULL; 1004 1005 jboolean isCopy; 1006 jlong* ptr = env->GetLongArrayElements(regsArray, &isCopy); 1007 CHECK_EXCEPTION_(0); 1008 1009 prgregset_t gregs; 1010 for (int i = 0; i < NPRGREG; i++) { 1011 gregs[i] = (uintptr_t) ptr[i]; 1012 } 1013 1014 env->ReleaseLongArrayElements(regsArray, ptr, JNI_ABORT); 1015 CHECK_EXCEPTION_(0); 1016 1017 Pstack_iter((struct ps_prochandle*) p_ps_prochandle, gregs, 1018 wrapper_fill_cframe_list, &dbgo2); 1019 return dbgo2.obj; 1020 } 1021 1022 /* 1023 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 1024 * Method: fillLoadObjectList0 1025 * Signature: (Ljava/util/List;)V 1026 * Description: fills shared objects of the debuggee process/core 1027 */ 1028 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_fillLoadObjectList0 1029 (JNIEnv *env, jobject this_obj, jobject list) { 1030 DebuggerWithObject dbgo; 1031 dbgo.env = env; 1032 dbgo.this_obj = this_obj; 1033 dbgo.obj = list; 1034 1035 jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); 1036 Pobject_iter((struct ps_prochandle*) p_ps_prochandle, fill_load_object_list, &dbgo); 1037 } 1038 1039 /* 1040 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 1041 * Method: readBytesFromProcess0 1042 * Signature: (JJ)[B 1043 * Description: read bytes from debuggee process/core 1044 */ 1045 JNIEXPORT jbyteArray JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_readBytesFromProcess0 1046 (JNIEnv *env, jobject this_obj, jlong address, jlong numBytes) { 1047 1048 jbyteArray array = env->NewByteArray(numBytes); 1049 CHECK_EXCEPTION_(0); 1050 jboolean isCopy; 1051 jbyte* bufPtr = env->GetByteArrayElements(array, &isCopy); 1052 CHECK_EXCEPTION_(0); 1053 1054 jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); 1055 ps_err_e ret = ps_pread((struct ps_prochandle*) p_ps_prochandle, 1056 (psaddr_t)address, bufPtr, (size_t)numBytes); 1057 1058 if (ret != PS_OK) { 1059 // part of the class sharing workaround. try shared heap area 1060 int classes_jsa_fd = env->GetIntField(this_obj, classes_jsa_fd_ID); 1061 if (classes_jsa_fd != -1 && address != (jlong)0) { 1062 print_debug("read failed at 0x%lx, attempting shared heap area\n", (long) address); 1063 1064 CDSFileMapHeaderBase* pheader = (CDSFileMapHeaderBase*) env->GetLongField(this_obj, p_file_map_header_ID); 1065 // walk through the shared mappings -- we just have 9 of them. 1066 // so, linear walking is okay. 1067 for (int m = 0; m < NUM_CDS_REGIONS; m++) { 1068 1069 // We can skip the non-read-only maps. These are mapped as MAP_PRIVATE 1070 // and hence will be read by libproc. Besides, the file copy may be 1071 // stale because the process might have modified those pages. 1072 if (pheader->_space[m]._read_only) { 1073 jlong mapping_offset = (jlong) (uintptr_t) pheader->_space[m]._mapping_offset; 1074 jlong baseAddress = mapping_offset + (jlong)sharedBaseAddress; 1075 size_t usedSize = pheader->_space[m]._used; 1076 if (mapping_offset != 0 && address >= baseAddress && address < (baseAddress + usedSize)) { 1077 // the given address falls in this shared metadata area 1078 print_debug("found shared map at 0x%lx\n", (long) baseAddress); 1079 1080 1081 // If more data is asked than actually mapped from file, we need to zero fill 1082 // till the end-of-page boundary. But, java array new does that for us. we just 1083 // need to read as much as data available. 1084 1085 #define MIN2(x, y) (((x) < (y))? (x) : (y)) 1086 1087 jlong diff = address - baseAddress; 1088 jlong bytesToRead = MIN2(numBytes, usedSize - diff); 1089 off_t offset = pheader->_space[m]._file_offset + off_t(diff); 1090 ssize_t bytesRead = pread(classes_jsa_fd, bufPtr, bytesToRead, offset); 1091 if (bytesRead != bytesToRead) { 1092 env->ReleaseByteArrayElements(array, bufPtr, JNI_ABORT); 1093 print_debug("shared map read failed\n"); 1094 return jbyteArray(0); 1095 } else { 1096 print_debug("shared map read succeeded\n"); 1097 env->ReleaseByteArrayElements(array, bufPtr, 0); 1098 return array; 1099 } 1100 } // is in current map 1101 } // is read only map 1102 } // for shared maps 1103 } // classes_jsa_fd != -1 1104 env->ReleaseByteArrayElements(array, bufPtr, JNI_ABORT); 1105 return jbyteArray(0); 1106 } else { 1107 env->ReleaseByteArrayElements(array, bufPtr, 0); 1108 return array; 1109 } 1110 } 1111 1112 /* 1113 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 1114 * Method: writeBytesToProcess0 1115 * Signature: (JJ[B)V 1116 * Description: write bytes into debugger process 1117 */ 1118 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_writeBytesToProcess0 1119 (JNIEnv *env, jobject this_obj, jlong address, jlong numBytes, jbyteArray data) { 1120 char errMsg[ERR_MSG_SIZE]; 1121 ps_err_e pe; 1122 jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); 1123 jboolean isCopy; 1124 jbyte* ptr = env->GetByteArrayElements(data, &isCopy); 1125 CHECK_EXCEPTION; 1126 1127 pe = ps_pwrite((struct ps_prochandle*) p_ps_prochandle, address, ptr, numBytes); 1128 if (pe != PS_OK) { 1129 snprintf(errMsg, ERR_MSG_SIZE, "Process write failed! ps_pwrite failed: %d", pe); 1130 env->ReleaseByteArrayElements(data, ptr, JNI_ABORT); 1131 THROW_NEW_DEBUGGER_EXCEPTION(errMsg); 1132 } 1133 1134 env->ReleaseByteArrayElements(data, ptr, JNI_ABORT); 1135 } 1136 1137 /* 1138 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 1139 * Method: suspend0 1140 * Signature: ()V 1141 */ 1142 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_suspend0 1143 (JNIEnv *env, jobject this_obj) { 1144 jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); 1145 // for now don't check return value. revisit this again. 1146 Pstop((struct ps_prochandle*) p_ps_prochandle, 1000); 1147 } 1148 1149 /* 1150 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 1151 * Method: resume0 1152 * Signature: ()V 1153 */ 1154 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_resume0 1155 (JNIEnv *env, jobject this_obj) { 1156 jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); 1157 // for now don't check return value. revisit this again. 1158 Psetrun((struct ps_prochandle*) p_ps_prochandle, 0, PRCFAULT|PRSTOP); 1159 } 1160 1161 /* 1162 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 1163 * Method: lookupByName0 1164 * Signature: (Ljava/lang/String;Ljava/lang/String;)J 1165 * Description: symbol lookup by name 1166 */ 1167 JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_lookupByName0 1168 (JNIEnv *env, jobject this_obj, jstring objectName, jstring symbolName) { 1169 jlong p_ps_prochandle; 1170 p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); 1171 1172 jboolean isCopy; 1173 const char* objectName_cstr = NULL; 1174 if (objectName != NULL) { 1175 objectName_cstr = env->GetStringUTFChars(objectName, &isCopy); 1176 CHECK_EXCEPTION_(0); 1177 } else { 1178 objectName_cstr = PR_OBJ_EVERY; 1179 } 1180 1181 const char* symbolName_cstr = env->GetStringUTFChars(symbolName, &isCopy); 1182 CHECK_EXCEPTION_(0); 1183 1184 psaddr_t symbol_addr = (psaddr_t) 0; 1185 ps_pglobal_lookup((struct ps_prochandle*) p_ps_prochandle, objectName_cstr, 1186 symbolName_cstr, &symbol_addr); 1187 1188 if (symbol_addr == 0) { 1189 print_debug("lookup for %s in %s failed\n", symbolName_cstr, objectName_cstr); 1190 } 1191 1192 if (objectName_cstr != PR_OBJ_EVERY) { 1193 env->ReleaseStringUTFChars(objectName, objectName_cstr); 1194 } 1195 env->ReleaseStringUTFChars(symbolName, symbolName_cstr); 1196 return (jlong) (uintptr_t) symbol_addr; 1197 } 1198 1199 /* 1200 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 1201 * Method: lookupByAddress0 1202 * Signature: (J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol; 1203 * Description: lookup symbol name for a given address 1204 */ 1205 JNIEXPORT jobject JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_lookupByAddress0 1206 (JNIEnv *env, jobject this_obj, jlong address) { 1207 jlong p_ps_prochandle; 1208 p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); 1209 1210 char nameBuf[SYMBOL_BUF_SIZE + 1]; 1211 GElf_Sym sym; 1212 int res = Plookup_by_addr((struct ps_prochandle*) p_ps_prochandle, (uintptr_t) address, 1213 nameBuf, sizeof(nameBuf), &sym, NULL); 1214 1215 if (res != 0) { // failed 1216 return 0; 1217 } 1218 1219 jstring resSym = env->NewStringUTF(nameBuf); 1220 CHECK_EXCEPTION_(0); 1221 1222 return env->CallObjectMethod(this_obj, createClosestSymbol_ID, resSym, (address - sym.st_value)); 1223 } 1224 1225 /* 1226 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 1227 * Method: demangle0 1228 * Signature: (Ljava/lang/String;)Ljava/lang/String; 1229 */ 1230 JNIEXPORT jstring JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_demangle0 1231 (JNIEnv *env, jobject this_object, jstring name) { 1232 jboolean isCopy; 1233 const char* ptr = env->GetStringUTFChars(name, &isCopy); 1234 CHECK_EXCEPTION_(NULL); 1235 char buf[2*SYMBOL_BUF_SIZE + 1]; 1236 jstring res = 0; 1237 if (cplus_demangle((char*) ptr, buf, sizeof(buf)) != DEMANGLE_ESPACE) { 1238 res = env->NewStringUTF(buf); 1239 } else { 1240 res = name; 1241 } 1242 env->ReleaseStringUTFChars(name, ptr); 1243 return res; 1244 } 1245 1246 /* 1247 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 1248 * Method: initIDs 1249 * Signature: ()V 1250 * Description: get JNI ids for fields and methods of ProcDebuggerLocal class 1251 */ 1252 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_initIDs 1253 (JNIEnv *env, jclass clazz) { 1254 _libsaproc_debug = getenv("LIBSAPROC_DEBUG") != NULL; 1255 if (_libsaproc_debug) { 1256 // propagate debug mode to libproc.so 1257 static const char* var = "LIBPROC_DEBUG=1"; 1258 putenv((char*)var); 1259 } 1260 1261 void* libproc_handle = dlopen("libproc.so", RTLD_LAZY | RTLD_GLOBAL); 1262 if (libproc_handle == 0) 1263 THROW_NEW_DEBUGGER_EXCEPTION("can't load libproc.so, if you are using Solaris 5.7 or below, copy libproc.so from 5.8!"); 1264 1265 p_ps_prochandle_ID = env->GetFieldID(clazz, "p_ps_prochandle", "J"); 1266 CHECK_EXCEPTION; 1267 1268 libthread_db_handle_ID = env->GetFieldID(clazz, "libthread_db_handle", "J"); 1269 CHECK_EXCEPTION; 1270 1271 p_td_thragent_t_ID = env->GetFieldID(clazz, "p_td_thragent_t", "J"); 1272 CHECK_EXCEPTION; 1273 1274 p_td_init_ID = env->GetFieldID(clazz, "p_td_init", "J"); 1275 CHECK_EXCEPTION; 1276 1277 p_td_ta_new_ID = env->GetFieldID(clazz, "p_td_ta_new", "J"); 1278 CHECK_EXCEPTION; 1279 1280 p_td_ta_delete_ID = env->GetFieldID(clazz, "p_td_ta_delete", "J"); 1281 CHECK_EXCEPTION; 1282 1283 p_td_ta_thr_iter_ID = env->GetFieldID(clazz, "p_td_ta_thr_iter", "J"); 1284 CHECK_EXCEPTION; 1285 1286 p_td_thr_get_info_ID = env->GetFieldID(clazz, "p_td_thr_get_info", "J"); 1287 CHECK_EXCEPTION; 1288 1289 p_td_ta_map_id2thr_ID = env->GetFieldID(clazz, "p_td_ta_map_id2thr", "J"); 1290 CHECK_EXCEPTION; 1291 1292 p_td_thr_getgregs_ID = env->GetFieldID(clazz, "p_td_thr_getgregs", "J"); 1293 CHECK_EXCEPTION; 1294 1295 getThreadForThreadId_ID = env->GetMethodID(clazz, 1296 "getThreadForThreadId", "(J)Lsun/jvm/hotspot/debugger/ThreadProxy;"); 1297 CHECK_EXCEPTION; 1298 1299 pcRegIndex_ID = env->GetFieldID(clazz, "pcRegIndex", "I"); 1300 CHECK_EXCEPTION; 1301 1302 fpRegIndex_ID = env->GetFieldID(clazz, "fpRegIndex", "I"); 1303 CHECK_EXCEPTION; 1304 1305 createSenderFrame_ID = env->GetMethodID(clazz, 1306 "createSenderFrame", "(Lsun/jvm/hotspot/debugger/proc/ProcCFrame;JJ)Lsun/jvm/hotspot/debugger/proc/ProcCFrame;"); 1307 CHECK_EXCEPTION; 1308 1309 createLoadObject_ID = env->GetMethodID(clazz, 1310 "createLoadObject", "(Ljava/lang/String;JJ)Lsun/jvm/hotspot/debugger/cdbg/LoadObject;"); 1311 CHECK_EXCEPTION; 1312 1313 createClosestSymbol_ID = env->GetMethodID(clazz, 1314 "createClosestSymbol", "(Ljava/lang/String;J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;"); 1315 CHECK_EXCEPTION; 1316 1317 jclass list_clazz = env->FindClass("java/util/List"); 1318 CHECK_EXCEPTION; 1319 listAdd_ID = env->GetMethodID(list_clazz, "add", "(Ljava/lang/Object;)Z"); 1320 CHECK_EXCEPTION; 1321 1322 // part of the class sharing workaround 1323 classes_jsa_fd_ID = env->GetFieldID(clazz, "classes_jsa_fd", "I"); 1324 CHECK_EXCEPTION; 1325 p_file_map_header_ID = env->GetFieldID(clazz, "p_file_map_header", "J"); 1326 CHECK_EXCEPTION; 1327 }