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