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