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 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 if (cmdLine_cstr == NULL) { 749 return; 750 } 751 752 // some older versions of libproc.so crash when trying to attach 32 bit 753 // debugger to 64 bit core file. check and throw error. 754 #ifndef _LP64 755 errno = 0; 756 strtol(cmdLine_cstr, NULL, 10); 757 if (errno) { 758 // core file 759 int core_fd; 760 if ((core_fd = open64(cmdLine_cstr, O_RDONLY)) >= 0) { 761 Elf32_Ehdr e32; 762 if (pread64(core_fd, &e32, sizeof (e32), 0) == sizeof (e32) && 763 memcmp(&e32.e_ident[EI_MAG0], ELFMAG, SELFMAG) == 0 && 764 e32.e_type == ET_CORE && e32.e_ident[EI_CLASS] == ELFCLASS64) { 765 close(core_fd); 766 env->ReleaseStringUTFChars(cmdLine, cmdLine_cstr); 767 THROW_NEW_DEBUGGER_EXCEPTION("debuggee is 64 bit, use java -d64 for debugger"); 768 } 769 close(core_fd); 770 } 771 // all other conditions are handled by libproc.so. 772 } 773 #endif 774 775 // connect to process/core 776 ps_prochandle_t* ph = proc_arg_grab(cmdLine_cstr, (isProcess? PR_ARG_PIDS : PR_ARG_CORES), PGRAB_FORCE, &gcode, NULL); 777 778 env->ReleaseStringUTFChars(cmdLine, cmdLine_cstr); 779 780 if (! ph) { 781 if (gcode > 0 && gcode < sizeof(proc_arg_grab_errmsgs)/sizeof(const char*)) { 782 snprintf(errMsg, ERR_MSG_SIZE, "Attach failed : %s", proc_arg_grab_errmsgs[gcode]); 783 THROW_NEW_DEBUGGER_EXCEPTION(errMsg); 784 } else { 785 if (_libsaproc_debug && gcode == G_STRANGE) { 786 perror("libsaproc DEBUG: "); 787 } 788 if (isProcess) { 789 THROW_NEW_DEBUGGER_EXCEPTION("Not able to attach to process!"); 790 } else { 791 THROW_NEW_DEBUGGER_EXCEPTION("Not able to attach to core file!"); 792 } 793 } 794 } 795 796 // even though libproc.so supports 64 bit debugger and 32 bit debuggee, we don't 797 // support such cross-bit-debugging. check for that combination and throw error. 798 #ifdef _LP64 799 int data_model; 800 if (ps_pdmodel(ph, &data_model) != PS_OK) { 801 Prelease(ph, PRELEASE_CLEAR); 802 THROW_NEW_DEBUGGER_EXCEPTION("can't determine debuggee data model (ILP32? or LP64?)"); 803 } 804 if (data_model == PR_MODEL_ILP32) { 805 Prelease(ph, PRELEASE_CLEAR); 806 THROW_NEW_DEBUGGER_EXCEPTION("debuggee is 32 bit, use 32 bit java for debugger"); 807 } 808 #endif 809 810 env->SetLongField(this_obj, p_ps_prochandle_ID, (jlong)(uintptr_t)ph); 811 812 Debugger dbg; 813 dbg.env = env; 814 dbg.this_obj = this_obj; 815 jthrowable exception = 0; 816 if (! isProcess) { 817 /* 818 * With class sharing, shared perm. gen heap is allocated in with MAP_SHARED|PROT_READ. 819 * These pages are mapped from the file "classes.jsa". MAP_SHARED pages are not dumped 820 * in Solaris core.To read shared heap pages, we have to read classes.jsa file. 821 */ 822 Pobject_iter(ph, init_classsharing_workaround, &dbg); 823 exception = env->ExceptionOccurred(); 824 if (exception) { 825 env->ExceptionClear(); 826 detach_internal(env, this_obj); 827 env->Throw(exception); 828 return; 829 } 830 } 831 832 /* 833 * Iterate over the process mappings looking 834 * for libthread and then dlopen the appropriate 835 * libthread_db and get function pointers. 836 */ 837 Pobject_iter(ph, init_libthread_db_ptrs, &dbg); 838 exception = env->ExceptionOccurred(); 839 if (exception) { 840 env->ExceptionClear(); 841 if (!sa_ignore_threaddb) { 842 detach_internal(env, this_obj); 843 env->Throw(exception); 844 } 845 return; 846 } 847 848 // init libthread_db and create thread_db agent 849 p_td_init_t p_td_init = (p_td_init_t) env->GetLongField(this_obj, p_td_init_ID); 850 if (p_td_init == 0) { 851 if (!sa_ignore_threaddb) { 852 detach_internal(env, this_obj); 853 } 854 HANDLE_THREADDB_FAILURE("Did not find libthread in target process/core!"); 855 } 856 857 te = p_td_init(); 858 if (te != TD_OK) { 859 if (!sa_ignore_threaddb) { 860 detach_internal(env, this_obj); 861 } 862 snprintf(errMsg, ERR_MSG_SIZE, "Can't initialize thread_db! td_init failed: %d", te); 863 HANDLE_THREADDB_FAILURE(errMsg); 864 } 865 866 p_td_ta_new_t p_td_ta_new = (p_td_ta_new_t) env->GetLongField(this_obj, p_td_ta_new_ID); 867 868 td_thragent_t *p_td_thragent_t = 0; 869 te = p_td_ta_new(ph, &p_td_thragent_t); 870 if (te != TD_OK) { 871 if (!sa_ignore_threaddb) { 872 detach_internal(env, this_obj); 873 } 874 snprintf(errMsg, ERR_MSG_SIZE, "Can't create thread_db agent! td_ta_new failed: %d", te); 875 HANDLE_THREADDB_FAILURE(errMsg); 876 } 877 env->SetLongField(this_obj, p_td_thragent_t_ID, (jlong)(uintptr_t) p_td_thragent_t); 878 879 } 880 881 /* 882 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 883 * Method: attach0 884 * Signature: (Ljava/lang/String;)V 885 * Description: process detach 886 */ 887 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_attach0__Ljava_lang_String_2 888 (JNIEnv *env, jobject this_obj, jstring pid) { 889 attach_internal(env, this_obj, pid, JNI_TRUE); 890 } 891 892 /* 893 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 894 * Method: attach0 895 * Signature: (Ljava/lang/String;Ljava/lang/String;)V 896 * Description: core file detach 897 */ 898 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_attach0__Ljava_lang_String_2Ljava_lang_String_2 899 (JNIEnv *env, jobject this_obj, jstring executable, jstring corefile) { 900 // ignore executable file name, libproc.so can detect a.out name anyway. 901 attach_internal(env, this_obj, corefile, JNI_FALSE); 902 } 903 904 905 /* 906 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 907 * Method: detach0 908 * Signature: ()V 909 * Description: process/core file detach 910 */ 911 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_detach0 912 (JNIEnv *env, jobject this_obj) { 913 detach_internal(env, this_obj); 914 } 915 916 /* 917 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 918 * Method: getRemoteProcessAddressSize0 919 * Signature: ()I 920 * Description: get process/core address size 921 */ 922 JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_getRemoteProcessAddressSize0 923 (JNIEnv *env, jobject this_obj) { 924 jlong p_ps_prochandle; 925 p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); 926 int data_model = PR_MODEL_ILP32; 927 ps_pdmodel((struct ps_prochandle*) p_ps_prochandle, &data_model); 928 print_debug("debuggee is %d bit\n", data_model == PR_MODEL_ILP32? 32 : 64); 929 return (jint) data_model == PR_MODEL_ILP32? 32 : 64; 930 } 931 932 /* 933 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 934 * Method: getPageSize0 935 * Signature: ()I 936 * Description: get process/core page size 937 */ 938 JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_getPageSize0 939 (JNIEnv *env, jobject this_obj) { 940 941 /* 942 We are not yet attached to a java process or core file. getPageSize is called from 943 the constructor of ProcDebuggerLocal. The following won't work! 944 945 jlong p_ps_prochandle; 946 p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); 947 CHECK_EXCEPTION_(-1); 948 struct ps_prochandle* prochandle = (struct ps_prochandle*) p_ps_prochandle; 949 return (Pstate(prochandle) == PS_DEAD) ? Pgetauxval(prochandle, AT_PAGESZ) 950 : getpagesize(); 951 952 So even though core may have been generated with a different page size settings, for now 953 call getpagesize. 954 */ 955 956 return getpagesize(); 957 } 958 959 /* 960 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 961 * Method: getThreadIntegerRegisterSet0 962 * Signature: (J)[J 963 * Description: get gregset for a given thread specified by thread id 964 */ 965 JNIEXPORT jlongArray JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_getThreadIntegerRegisterSet0 966 (JNIEnv *env, jobject this_obj, jlong tid) { 967 char errMsg[ERR_MSG_SIZE]; 968 td_err_e te; 969 // map the thread id to thread handle 970 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); 971 972 td_thragent_t* p_td_thragent_t = (td_thragent_t*) env->GetLongField(this_obj, p_td_thragent_t_ID); 973 if (p_td_thragent_t == 0) { 974 return 0; 975 } 976 977 td_thrhandle_t thr_handle; 978 te = p_td_ta_map_id2thr(p_td_thragent_t, (thread_t) tid, &thr_handle); 979 if (te != TD_OK) { 980 snprintf(errMsg, ERR_MSG_SIZE, "can't map thread id to thread handle! td_ta_map_id2thr failed: %d", te); 981 THROW_NEW_DEBUGGER_EXCEPTION_(errMsg, 0); 982 } 983 984 p_td_thr_getgregs_t p_td_thr_getgregs = (p_td_thr_getgregs_t) env->GetLongField(this_obj, p_td_thr_getgregs_ID); 985 prgregset_t gregs; 986 p_td_thr_getgregs(&thr_handle, gregs); 987 988 jlongArray res = env->NewLongArray(NPRGREG); 989 CHECK_EXCEPTION_(0); 990 jboolean isCopy; 991 jlong* ptr = env->GetLongArrayElements(res, &isCopy); 992 CHECK_EXCEPTION_(NULL); 993 for (int i = 0; i < NPRGREG; i++) { 994 ptr[i] = (jlong) (uintptr_t) gregs[i]; 995 } 996 env->ReleaseLongArrayElements(res, ptr, JNI_COMMIT); 997 return res; 998 } 999 1000 /* 1001 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 1002 * Method: fillThreadList0 1003 * Signature: (Ljava/util/List;)V 1004 * Description: fills thread list of the debuggee process/core 1005 */ 1006 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_fillThreadList0 1007 (JNIEnv *env, jobject this_obj, jobject list) { 1008 1009 td_thragent_t* p_td_thragent_t = (td_thragent_t*) env->GetLongField(this_obj, p_td_thragent_t_ID); 1010 if (p_td_thragent_t == 0) { 1011 return; 1012 } 1013 1014 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); 1015 1016 DebuggerWithObject dbgo; 1017 dbgo.env = env; 1018 dbgo.this_obj = this_obj; 1019 dbgo.obj = list; 1020 1021 p_td_ta_thr_iter(p_td_thragent_t, fill_thread_list, &dbgo, 1022 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY, TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS); 1023 } 1024 1025 /* 1026 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 1027 * Method: fillCFrameList0 1028 * Signature: ([J)Lsun/jvm/hotspot/debugger/proc/ProcCFrame; 1029 * Description: fills CFrame list for a given thread 1030 */ 1031 JNIEXPORT jobject JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_fillCFrameList0 1032 (JNIEnv *env, jobject this_obj, jlongArray regsArray) { 1033 jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); 1034 1035 DebuggerWith2Objects dbgo2; 1036 dbgo2.env = env; 1037 dbgo2.this_obj = this_obj; 1038 dbgo2.obj = NULL; 1039 dbgo2.obj2 = NULL; 1040 1041 jboolean isCopy; 1042 jlong* ptr = env->GetLongArrayElements(regsArray, &isCopy); 1043 CHECK_EXCEPTION_(0); 1044 1045 prgregset_t gregs; 1046 for (int i = 0; i < NPRGREG; i++) { 1047 gregs[i] = (uintptr_t) ptr[i]; 1048 } 1049 1050 env->ReleaseLongArrayElements(regsArray, ptr, JNI_ABORT); 1051 CHECK_EXCEPTION_(0); 1052 1053 Pstack_iter((struct ps_prochandle*) p_ps_prochandle, gregs, 1054 wrapper_fill_cframe_list, &dbgo2); 1055 return dbgo2.obj; 1056 } 1057 1058 /* 1059 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 1060 * Method: fillLoadObjectList0 1061 * Signature: (Ljava/util/List;)V 1062 * Description: fills shared objects of the debuggee process/core 1063 */ 1064 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_fillLoadObjectList0 1065 (JNIEnv *env, jobject this_obj, jobject list) { 1066 DebuggerWithObject dbgo; 1067 dbgo.env = env; 1068 dbgo.this_obj = this_obj; 1069 dbgo.obj = list; 1070 1071 jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); 1072 Pobject_iter((struct ps_prochandle*) p_ps_prochandle, fill_load_object_list, &dbgo); 1073 } 1074 1075 /* 1076 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 1077 * Method: readBytesFromProcess0 1078 * Signature: (JJ)[B 1079 * Description: read bytes from debuggee process/core 1080 */ 1081 JNIEXPORT jbyteArray JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_readBytesFromProcess0 1082 (JNIEnv *env, jobject this_obj, jlong address, jlong numBytes) { 1083 1084 jbyteArray array = env->NewByteArray(numBytes); 1085 CHECK_EXCEPTION_(0); 1086 jboolean isCopy; 1087 jbyte* bufPtr = env->GetByteArrayElements(array, &isCopy); 1088 CHECK_EXCEPTION_(0); 1089 1090 jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); 1091 ps_err_e ret = ps_pread((struct ps_prochandle*) p_ps_prochandle, 1092 (psaddr_t)address, bufPtr, (size_t)numBytes); 1093 1094 if (ret != PS_OK) { 1095 // part of the class sharing workaround. try shared heap area 1096 int classes_jsa_fd = env->GetIntField(this_obj, classes_jsa_fd_ID); 1097 if (classes_jsa_fd != -1 && address != (jlong)0) { 1098 print_debug("read failed at 0x%lx, attempting shared heap area\n", (long) address); 1099 1100 struct FileMapHeader* pheader = (struct FileMapHeader*) env->GetLongField(this_obj, p_file_map_header_ID); 1101 // walk through the shared mappings -- we just have 9 of them. 1102 // so, linear walking is okay. 1103 for (int m = 0; m < NUM_SHARED_MAPS; m++) { 1104 1105 // We can skip the non-read-only maps. These are mapped as MAP_PRIVATE 1106 // and hence will be read by libproc. Besides, the file copy may be 1107 // stale because the process might have modified those pages. 1108 if (pheader->_space[m]._read_only) { 1109 jlong baseAddress = (jlong) (uintptr_t) pheader->_space[m]._addr._base; 1110 size_t usedSize = pheader->_space[m]._used; 1111 if (address >= baseAddress && address < (baseAddress + usedSize)) { 1112 // the given address falls in this shared heap area 1113 print_debug("found shared map at 0x%lx\n", (long) baseAddress); 1114 1115 1116 // If more data is asked than actually mapped from file, we need to zero fill 1117 // till the end-of-page boundary. But, java array new does that for us. we just 1118 // need to read as much as data available. 1119 1120 #define MIN2(x, y) (((x) < (y))? (x) : (y)) 1121 1122 jlong diff = address - baseAddress; 1123 jlong bytesToRead = MIN2(numBytes, usedSize - diff); 1124 off_t offset = pheader->_space[m]._file_offset + off_t(diff); 1125 ssize_t bytesRead = pread(classes_jsa_fd, bufPtr, bytesToRead, offset); 1126 if (bytesRead != bytesToRead) { 1127 env->ReleaseByteArrayElements(array, bufPtr, JNI_ABORT); 1128 print_debug("shared map read failed\n"); 1129 return jbyteArray(0); 1130 } else { 1131 print_debug("shared map read succeeded\n"); 1132 env->ReleaseByteArrayElements(array, bufPtr, 0); 1133 return array; 1134 } 1135 } // is in current map 1136 } // is read only map 1137 } // for shared maps 1138 } // classes_jsa_fd != -1 1139 env->ReleaseByteArrayElements(array, bufPtr, JNI_ABORT); 1140 return jbyteArray(0); 1141 } else { 1142 env->ReleaseByteArrayElements(array, bufPtr, 0); 1143 return array; 1144 } 1145 } 1146 1147 /* 1148 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 1149 * Method: writeBytesToProcess0 1150 * Signature: (JJ[B)V 1151 * Description: write bytes into debugger process 1152 */ 1153 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_writeBytesToProcess0 1154 (JNIEnv *env, jobject this_obj, jlong address, jlong numBytes, jbyteArray data) { 1155 char errMsg[ERR_MSG_SIZE]; 1156 ps_err_e pe; 1157 jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); 1158 jboolean isCopy; 1159 jbyte* ptr = env->GetByteArrayElements(data, &isCopy); 1160 CHECK_EXCEPTION; 1161 1162 pe = ps_pwrite((struct ps_prochandle*) p_ps_prochandle, address, ptr, numBytes); 1163 if (pe != PS_OK) { 1164 snprintf(errMsg, ERR_MSG_SIZE, "Process write failed! ps_pwrite failed: %d", pe); 1165 env->ReleaseByteArrayElements(data, ptr, JNI_ABORT); 1166 THROW_NEW_DEBUGGER_EXCEPTION(errMsg); 1167 } 1168 1169 env->ReleaseByteArrayElements(data, ptr, JNI_ABORT); 1170 } 1171 1172 /* 1173 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 1174 * Method: suspend0 1175 * Signature: ()V 1176 */ 1177 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_suspend0 1178 (JNIEnv *env, jobject this_obj) { 1179 jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); 1180 // for now don't check return value. revisit this again. 1181 Pstop((struct ps_prochandle*) p_ps_prochandle, 1000); 1182 } 1183 1184 /* 1185 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 1186 * Method: resume0 1187 * Signature: ()V 1188 */ 1189 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_resume0 1190 (JNIEnv *env, jobject this_obj) { 1191 jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); 1192 // for now don't check return value. revisit this again. 1193 Psetrun((struct ps_prochandle*) p_ps_prochandle, 0, PRCFAULT|PRSTOP); 1194 } 1195 1196 /* 1197 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 1198 * Method: lookupByName0 1199 * Signature: (Ljava/lang/String;Ljava/lang/String;)J 1200 * Description: symbol lookup by name 1201 */ 1202 JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_lookupByName0 1203 (JNIEnv *env, jobject this_obj, jstring objectName, jstring symbolName) { 1204 jlong p_ps_prochandle; 1205 p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); 1206 1207 jboolean isCopy; 1208 const char* objectName_cstr = NULL; 1209 if (objectName != NULL) { 1210 objectName_cstr = env->GetStringUTFChars(objectName, &isCopy); 1211 CHECK_EXCEPTION_(0); 1212 } else { 1213 objectName_cstr = PR_OBJ_EVERY; 1214 } 1215 1216 const char* symbolName_cstr = env->GetStringUTFChars(symbolName, &isCopy); 1217 CHECK_EXCEPTION_(0); 1218 1219 psaddr_t symbol_addr = (psaddr_t) 0; 1220 ps_pglobal_lookup((struct ps_prochandle*) p_ps_prochandle, objectName_cstr, 1221 symbolName_cstr, &symbol_addr); 1222 1223 if (symbol_addr == 0) { 1224 print_debug("lookup for %s in %s failed\n", symbolName_cstr, objectName_cstr); 1225 } 1226 1227 if (objectName_cstr != PR_OBJ_EVERY) { 1228 env->ReleaseStringUTFChars(objectName, objectName_cstr); 1229 } 1230 env->ReleaseStringUTFChars(symbolName, symbolName_cstr); 1231 return (jlong) (uintptr_t) symbol_addr; 1232 } 1233 1234 /* 1235 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 1236 * Method: lookupByAddress0 1237 * Signature: (J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol; 1238 * Description: lookup symbol name for a given address 1239 */ 1240 JNIEXPORT jobject JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_lookupByAddress0 1241 (JNIEnv *env, jobject this_obj, jlong address) { 1242 jlong p_ps_prochandle; 1243 p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); 1244 1245 char nameBuf[SYMBOL_BUF_SIZE + 1]; 1246 GElf_Sym sym; 1247 int res = Plookup_by_addr((struct ps_prochandle*) p_ps_prochandle, (uintptr_t) address, 1248 nameBuf, sizeof(nameBuf), &sym, NULL); 1249 1250 if (res != 0) { // failed 1251 return 0; 1252 } 1253 1254 jstring resSym = env->NewStringUTF(nameBuf); 1255 CHECK_EXCEPTION_(0); 1256 1257 return env->CallObjectMethod(this_obj, createClosestSymbol_ID, resSym, (address - sym.st_value)); 1258 } 1259 1260 /* 1261 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 1262 * Method: demangle0 1263 * Signature: (Ljava/lang/String;)Ljava/lang/String; 1264 */ 1265 JNIEXPORT jstring JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_demangle0 1266 (JNIEnv *env, jobject this_object, jstring name) { 1267 jboolean isCopy; 1268 const char* ptr = env->GetStringUTFChars(name, &isCopy); 1269 CHECK_EXCEPTION_(NULL); 1270 char buf[2*SYMBOL_BUF_SIZE + 1]; 1271 jstring res = 0; 1272 if (cplus_demangle((char*) ptr, buf, sizeof(buf)) != DEMANGLE_ESPACE) { 1273 res = env->NewStringUTF(buf); 1274 } else { 1275 res = name; 1276 } 1277 env->ReleaseStringUTFChars(name, ptr); 1278 return res; 1279 } 1280 1281 /* 1282 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal 1283 * Method: initIDs 1284 * Signature: ()V 1285 * Description: get JNI ids for fields and methods of ProcDebuggerLocal class 1286 */ 1287 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_initIDs 1288 (JNIEnv *env, jclass clazz) { 1289 _libsaproc_debug = getenv("LIBSAPROC_DEBUG") != NULL; 1290 if (_libsaproc_debug) { 1291 // propagate debug mode to libproc.so 1292 static const char* var = "LIBPROC_DEBUG=1"; 1293 putenv((char*)var); 1294 } 1295 1296 void* libproc_handle = dlopen("libproc.so", RTLD_LAZY | RTLD_GLOBAL); 1297 if (libproc_handle == 0) 1298 THROW_NEW_DEBUGGER_EXCEPTION("can't load libproc.so, if you are using Solaris 5.7 or below, copy libproc.so from 5.8!"); 1299 1300 p_ps_prochandle_ID = env->GetFieldID(clazz, "p_ps_prochandle", "J"); 1301 CHECK_EXCEPTION; 1302 1303 libthread_db_handle_ID = env->GetFieldID(clazz, "libthread_db_handle", "J"); 1304 CHECK_EXCEPTION; 1305 1306 p_td_thragent_t_ID = env->GetFieldID(clazz, "p_td_thragent_t", "J"); 1307 CHECK_EXCEPTION; 1308 1309 p_td_init_ID = env->GetFieldID(clazz, "p_td_init", "J"); 1310 CHECK_EXCEPTION; 1311 1312 p_td_ta_new_ID = env->GetFieldID(clazz, "p_td_ta_new", "J"); 1313 CHECK_EXCEPTION; 1314 1315 p_td_ta_delete_ID = env->GetFieldID(clazz, "p_td_ta_delete", "J"); 1316 CHECK_EXCEPTION; 1317 1318 p_td_ta_thr_iter_ID = env->GetFieldID(clazz, "p_td_ta_thr_iter", "J"); 1319 CHECK_EXCEPTION; 1320 1321 p_td_thr_get_info_ID = env->GetFieldID(clazz, "p_td_thr_get_info", "J"); 1322 CHECK_EXCEPTION; 1323 1324 p_td_ta_map_id2thr_ID = env->GetFieldID(clazz, "p_td_ta_map_id2thr", "J"); 1325 CHECK_EXCEPTION; 1326 1327 p_td_thr_getgregs_ID = env->GetFieldID(clazz, "p_td_thr_getgregs", "J"); 1328 CHECK_EXCEPTION; 1329 1330 getThreadForThreadId_ID = env->GetMethodID(clazz, 1331 "getThreadForThreadId", "(J)Lsun/jvm/hotspot/debugger/ThreadProxy;"); 1332 CHECK_EXCEPTION; 1333 1334 pcRegIndex_ID = env->GetFieldID(clazz, "pcRegIndex", "I"); 1335 CHECK_EXCEPTION; 1336 1337 fpRegIndex_ID = env->GetFieldID(clazz, "fpRegIndex", "I"); 1338 CHECK_EXCEPTION; 1339 1340 createSenderFrame_ID = env->GetMethodID(clazz, 1341 "createSenderFrame", "(Lsun/jvm/hotspot/debugger/proc/ProcCFrame;JJ)Lsun/jvm/hotspot/debugger/proc/ProcCFrame;"); 1342 CHECK_EXCEPTION; 1343 1344 createLoadObject_ID = env->GetMethodID(clazz, 1345 "createLoadObject", "(Ljava/lang/String;JJ)Lsun/jvm/hotspot/debugger/cdbg/LoadObject;"); 1346 CHECK_EXCEPTION; 1347 1348 createClosestSymbol_ID = env->GetMethodID(clazz, 1349 "createClosestSymbol", "(Ljava/lang/String;J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;"); 1350 CHECK_EXCEPTION; 1351 1352 jclass list_clazz = env->FindClass("java/util/List"); 1353 CHECK_EXCEPTION; 1354 listAdd_ID = env->GetMethodID(list_clazz, "add", "(Ljava/lang/Object;)Z"); 1355 CHECK_EXCEPTION; 1356 1357 // part of the class sharing workaround 1358 classes_jsa_fd_ID = env->GetFieldID(clazz, "classes_jsa_fd", "I"); 1359 CHECK_EXCEPTION; 1360 p_file_map_header_ID = env->GetFieldID(clazz, "p_file_map_header", "J"); 1361 CHECK_EXCEPTION; 1362 }