1 /* 2 * Copyright (c) 2003, 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 <jni.h> 26 #include <unistd.h> 27 #include <fcntl.h> 28 #include <string.h> 29 #include <stdlib.h> 30 #include <stddef.h> 31 #include <elf.h> 32 #include <link.h> 33 #include "libproc_impl.h" 34 #include "salibelf.h" 35 #include "../../../../hotspot/share/include/cds.h" 36 37 // This file has the libproc implementation to read core files. 38 // For live processes, refer to ps_proc.c. Portions of this is adapted 39 // /modelled after Solaris libproc.so (in particular Pcore.c) 40 41 //---------------------------------------------------------------------- 42 // ps_prochandle cleanup helper functions 43 44 // close all file descriptors 45 static void close_files(struct ps_prochandle* ph) { 46 lib_info* lib = NULL; 47 48 // close core file descriptor 49 if (ph->core->core_fd >= 0) 50 close(ph->core->core_fd); 51 52 // close exec file descriptor 53 if (ph->core->exec_fd >= 0) 54 close(ph->core->exec_fd); 55 56 // close interp file descriptor 57 if (ph->core->interp_fd >= 0) 58 close(ph->core->interp_fd); 59 60 // close class share archive file 61 if (ph->core->classes_jsa_fd >= 0) 62 close(ph->core->classes_jsa_fd); 63 64 // close all library file descriptors 65 lib = ph->libs; 66 while (lib) { 67 int fd = lib->fd; 68 if (fd >= 0 && fd != ph->core->exec_fd) { 69 close(fd); 70 } 71 lib = lib->next; 72 } 73 } 74 75 // clean all map_info stuff 76 static void destroy_map_info(struct ps_prochandle* ph) { 77 map_info* map = ph->core->maps; 78 while (map) { 79 map_info* next = map->next; 80 free(map); 81 map = next; 82 } 83 84 if (ph->core->map_array) { 85 free(ph->core->map_array); 86 } 87 88 // Part of the class sharing workaround 89 map = ph->core->class_share_maps; 90 while (map) { 91 map_info* next = map->next; 92 free(map); 93 map = next; 94 } 95 } 96 97 // ps_prochandle operations 98 static void core_release(struct ps_prochandle* ph) { 99 if (ph->core) { 100 close_files(ph); 101 destroy_map_info(ph); 102 free(ph->core); 103 } 104 } 105 106 static map_info* allocate_init_map(int fd, off_t offset, uintptr_t vaddr, size_t memsz) { 107 map_info* map; 108 if ( (map = (map_info*) calloc(1, sizeof(map_info))) == NULL) { 109 print_debug("can't allocate memory for map_info\n"); 110 return NULL; 111 } 112 113 // initialize map 114 map->fd = fd; 115 map->offset = offset; 116 map->vaddr = vaddr; 117 map->memsz = memsz; 118 return map; 119 } 120 121 // add map info with given fd, offset, vaddr and memsz 122 static map_info* add_map_info(struct ps_prochandle* ph, int fd, off_t offset, 123 uintptr_t vaddr, size_t memsz) { 124 map_info* map; 125 if ((map = allocate_init_map(fd, offset, vaddr, memsz)) == NULL) { 126 return NULL; 127 } 128 129 // add this to map list 130 map->next = ph->core->maps; 131 ph->core->maps = map; 132 ph->core->num_maps++; 133 134 return map; 135 } 136 137 // Part of the class sharing workaround 138 static map_info* add_class_share_map_info(struct ps_prochandle* ph, off_t offset, 139 uintptr_t vaddr, size_t memsz) { 140 map_info* map; 141 if ((map = allocate_init_map(ph->core->classes_jsa_fd, 142 offset, vaddr, memsz)) == NULL) { 143 return NULL; 144 } 145 146 map->next = ph->core->class_share_maps; 147 ph->core->class_share_maps = map; 148 return map; 149 } 150 151 // Return the map_info for the given virtual address. We keep a sorted 152 // array of pointers in ph->map_array, so we can binary search. 153 static map_info* core_lookup(struct ps_prochandle *ph, uintptr_t addr) { 154 int mid, lo = 0, hi = ph->core->num_maps - 1; 155 map_info *mp; 156 157 while (hi - lo > 1) { 158 mid = (lo + hi) / 2; 159 if (addr >= ph->core->map_array[mid]->vaddr) { 160 lo = mid; 161 } else { 162 hi = mid; 163 } 164 } 165 166 if (addr < ph->core->map_array[hi]->vaddr) { 167 mp = ph->core->map_array[lo]; 168 } else { 169 mp = ph->core->map_array[hi]; 170 } 171 172 if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) { 173 return (mp); 174 } 175 176 177 // Part of the class sharing workaround 178 // Unfortunately, we have no way of detecting -Xshare state. 179 // Check out the share maps atlast, if we don't find anywhere. 180 // This is done this way so to avoid reading share pages 181 // ahead of other normal maps. For eg. with -Xshare:off we don't 182 // want to prefer class sharing data to data from core. 183 mp = ph->core->class_share_maps; 184 if (mp) { 185 print_debug("can't locate map_info at 0x%lx, trying class share maps\n", addr); 186 } 187 while (mp) { 188 if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) { 189 print_debug("located map_info at 0x%lx from class share maps\n", addr); 190 return (mp); 191 } 192 mp = mp->next; 193 } 194 195 print_debug("can't locate map_info at 0x%lx\n", addr); 196 return (NULL); 197 } 198 199 //--------------------------------------------------------------- 200 // Part of the class sharing workaround: 201 // 202 // With class sharing, pages are mapped from classes.jsa file. 203 // The read-only class sharing pages are mapped as MAP_SHARED, 204 // PROT_READ pages. These pages are not dumped into core dump. 205 // With this workaround, these pages are read from classes.jsa. 206 207 typedef struct CDSFileMapHeaderBase FileMapHeader; 208 209 static bool read_jboolean(struct ps_prochandle* ph, uintptr_t addr, jboolean* pvalue) { 210 jboolean i; 211 if (ps_pdread(ph, (psaddr_t) addr, &i, sizeof(i)) == PS_OK) { 212 *pvalue = i; 213 return true; 214 } else { 215 return false; 216 } 217 } 218 219 static bool read_pointer(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* pvalue) { 220 uintptr_t uip; 221 if (ps_pdread(ph, (psaddr_t) addr, (char *)&uip, sizeof(uip)) == PS_OK) { 222 *pvalue = uip; 223 return true; 224 } else { 225 return false; 226 } 227 } 228 229 // used to read strings from debuggee 230 static bool read_string(struct ps_prochandle* ph, uintptr_t addr, char* buf, size_t size) { 231 size_t i = 0; 232 char c = ' '; 233 234 while (c != '\0') { 235 if (ps_pdread(ph, (psaddr_t) addr, &c, sizeof(char)) != PS_OK) { 236 return false; 237 } 238 if (i < size - 1) { 239 buf[i] = c; 240 } else { 241 // smaller buffer 242 return false; 243 } 244 i++; addr++; 245 } 246 247 buf[i] = '\0'; 248 return true; 249 } 250 251 #define USE_SHARED_SPACES_SYM "UseSharedSpaces" 252 // mangled name of Arguments::SharedArchivePath 253 #define SHARED_ARCHIVE_PATH_SYM "_ZN9Arguments17SharedArchivePathE" 254 #define LIBJVM_NAME "/libjvm.so" 255 256 static bool init_classsharing_workaround(struct ps_prochandle* ph) { 257 lib_info* lib = ph->libs; 258 while (lib != NULL) { 259 // we are iterating over shared objects from the core dump. look for 260 // libjvm.so. 261 const char *jvm_name = 0; 262 if ((jvm_name = strstr(lib->name, LIBJVM_NAME)) != 0) { 263 char classes_jsa[PATH_MAX]; 264 FileMapHeader header; 265 int fd = -1; 266 int m = 0; 267 size_t n = 0; 268 uintptr_t base = 0, useSharedSpacesAddr = 0; 269 uintptr_t sharedArchivePathAddrAddr = 0, sharedArchivePathAddr = 0; 270 jboolean useSharedSpaces = 0; 271 map_info* mi = 0; 272 273 memset(classes_jsa, 0, sizeof(classes_jsa)); 274 jvm_name = lib->name; 275 useSharedSpacesAddr = lookup_symbol(ph, jvm_name, USE_SHARED_SPACES_SYM); 276 if (useSharedSpacesAddr == 0) { 277 print_debug("can't lookup 'UseSharedSpaces' flag\n"); 278 return false; 279 } 280 281 // Hotspot vm types are not exported to build this library. So 282 // using equivalent type jboolean to read the value of 283 // UseSharedSpaces which is same as hotspot type "bool". 284 if (read_jboolean(ph, useSharedSpacesAddr, &useSharedSpaces) != true) { 285 print_debug("can't read the value of 'UseSharedSpaces' flag\n"); 286 return false; 287 } 288 289 if ((int)useSharedSpaces == 0) { 290 print_debug("UseSharedSpaces is false, assuming -Xshare:off!\n"); 291 return true; 292 } 293 294 sharedArchivePathAddrAddr = lookup_symbol(ph, jvm_name, SHARED_ARCHIVE_PATH_SYM); 295 if (sharedArchivePathAddrAddr == 0) { 296 print_debug("can't lookup shared archive path symbol\n"); 297 return false; 298 } 299 300 if (read_pointer(ph, sharedArchivePathAddrAddr, &sharedArchivePathAddr) != true) { 301 print_debug("can't read shared archive path pointer\n"); 302 return false; 303 } 304 305 if (read_string(ph, sharedArchivePathAddr, classes_jsa, sizeof(classes_jsa)) != true) { 306 print_debug("can't read shared archive path value\n"); 307 return false; 308 } 309 310 print_debug("looking for %s\n", classes_jsa); 311 // open the class sharing archive file 312 fd = pathmap_open(classes_jsa); 313 if (fd < 0) { 314 print_debug("can't open %s!\n", classes_jsa); 315 ph->core->classes_jsa_fd = -1; 316 return false; 317 } else { 318 print_debug("opened %s\n", classes_jsa); 319 } 320 321 // read FileMapHeader from the file 322 memset(&header, 0, sizeof(FileMapHeader)); 323 if ((n = read(fd, &header, sizeof(FileMapHeader))) 324 != sizeof(FileMapHeader)) { 325 print_debug("can't read shared archive file map header from %s\n", classes_jsa); 326 close(fd); 327 return false; 328 } 329 330 // check file magic 331 if (header._magic != CDS_ARCHIVE_MAGIC) { 332 print_debug("%s has bad shared archive file magic number 0x%x, expecing 0x%x\n", 333 classes_jsa, header._magic, CDS_ARCHIVE_MAGIC); 334 close(fd); 335 return false; 336 } 337 338 // check version 339 if (header._version != CURRENT_CDS_ARCHIVE_VERSION) { 340 print_debug("%s has wrong shared archive file version %d, expecting %d\n", 341 classes_jsa, header._version, CURRENT_CDS_ARCHIVE_VERSION); 342 close(fd); 343 return false; 344 } 345 346 ph->core->classes_jsa_fd = fd; 347 // add read-only maps from classes.jsa to the list of maps 348 for (m = 0; m < NUM_CDS_REGIONS; m++) { 349 if (header._space[m]._read_only) { 350 base = (uintptr_t) header._space[m]._addr._base; 351 // no need to worry about the fractional pages at-the-end. 352 // possible fractional pages are handled by core_read_data. 353 add_class_share_map_info(ph, (off_t) header._space[m]._file_offset, 354 base, (size_t) header._space[m]._used); 355 print_debug("added a share archive map at 0x%lx\n", base); 356 } 357 } 358 return true; 359 } 360 lib = lib->next; 361 } 362 return true; 363 } 364 365 366 //--------------------------------------------------------------------------- 367 // functions to handle map_info 368 369 // Order mappings based on virtual address. We use this function as the 370 // callback for sorting the array of map_info pointers. 371 static int core_cmp_mapping(const void *lhsp, const void *rhsp) 372 { 373 const map_info *lhs = *((const map_info **)lhsp); 374 const map_info *rhs = *((const map_info **)rhsp); 375 376 if (lhs->vaddr == rhs->vaddr) { 377 return (0); 378 } 379 380 return (lhs->vaddr < rhs->vaddr ? -1 : 1); 381 } 382 383 // we sort map_info by starting virtual address so that we can do 384 // binary search to read from an address. 385 static bool sort_map_array(struct ps_prochandle* ph) { 386 size_t num_maps = ph->core->num_maps; 387 map_info* map = ph->core->maps; 388 int i = 0; 389 390 // allocate map_array 391 map_info** array; 392 if ( (array = (map_info**) malloc(sizeof(map_info*) * num_maps)) == NULL) { 393 print_debug("can't allocate memory for map array\n"); 394 return false; 395 } 396 397 // add maps to array 398 while (map) { 399 array[i] = map; 400 i++; 401 map = map->next; 402 } 403 404 // sort is called twice. If this is second time, clear map array 405 if (ph->core->map_array) { 406 free(ph->core->map_array); 407 } 408 409 ph->core->map_array = array; 410 // sort the map_info array by base virtual address. 411 qsort(ph->core->map_array, ph->core->num_maps, sizeof (map_info*), 412 core_cmp_mapping); 413 414 // print map 415 if (is_debug()) { 416 int j = 0; 417 print_debug("---- sorted virtual address map ----\n"); 418 for (j = 0; j < ph->core->num_maps; j++) { 419 print_debug("base = 0x%lx\tsize = %zu\n", ph->core->map_array[j]->vaddr, 420 ph->core->map_array[j]->memsz); 421 } 422 } 423 424 return true; 425 } 426 427 #ifndef MIN 428 #define MIN(x, y) (((x) < (y))? (x): (y)) 429 #endif 430 431 static bool core_read_data(struct ps_prochandle* ph, uintptr_t addr, char *buf, size_t size) { 432 ssize_t resid = size; 433 int page_size=sysconf(_SC_PAGE_SIZE); 434 while (resid != 0) { 435 map_info *mp = core_lookup(ph, addr); 436 uintptr_t mapoff; 437 ssize_t len, rem; 438 off_t off; 439 int fd; 440 441 if (mp == NULL) { 442 break; /* No mapping for this address */ 443 } 444 445 fd = mp->fd; 446 mapoff = addr - mp->vaddr; 447 len = MIN(resid, mp->memsz - mapoff); 448 off = mp->offset + mapoff; 449 450 if ((len = pread(fd, buf, len, off)) <= 0) { 451 break; 452 } 453 454 resid -= len; 455 addr += len; 456 buf = (char *)buf + len; 457 458 // mappings always start at page boundary. But, may end in fractional 459 // page. fill zeros for possible fractional page at the end of a mapping. 460 rem = mp->memsz % page_size; 461 if (rem > 0) { 462 rem = page_size - rem; 463 len = MIN(resid, rem); 464 resid -= len; 465 addr += len; 466 // we are not assuming 'buf' to be zero initialized. 467 memset(buf, 0, len); 468 buf += len; 469 } 470 } 471 472 if (resid) { 473 print_debug("core read failed for %d byte(s) @ 0x%lx (%d more bytes)\n", 474 size, addr, resid); 475 return false; 476 } else { 477 return true; 478 } 479 } 480 481 // null implementation for write 482 static bool core_write_data(struct ps_prochandle* ph, 483 uintptr_t addr, const char *buf , size_t size) { 484 return false; 485 } 486 487 static bool core_get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id, 488 struct user_regs_struct* regs) { 489 // for core we have cached the lwp regs from NOTE section 490 thread_info* thr = ph->threads; 491 while (thr) { 492 if (thr->lwp_id == lwp_id) { 493 memcpy(regs, &thr->regs, sizeof(struct user_regs_struct)); 494 return true; 495 } 496 thr = thr->next; 497 } 498 return false; 499 } 500 501 static ps_prochandle_ops core_ops = { 502 .release= core_release, 503 .p_pread= core_read_data, 504 .p_pwrite= core_write_data, 505 .get_lwp_regs= core_get_lwp_regs 506 }; 507 508 // read regs and create thread from NT_PRSTATUS entries from core file 509 static bool core_handle_prstatus(struct ps_prochandle* ph, const char* buf, size_t nbytes) { 510 // we have to read prstatus_t from buf 511 // assert(nbytes == sizeof(prstaus_t), "size mismatch on prstatus_t"); 512 prstatus_t* prstat = (prstatus_t*) buf; 513 thread_info* newthr; 514 print_debug("got integer regset for lwp %d\n", prstat->pr_pid); 515 // we set pthread_t to -1 for core dump 516 if((newthr = add_thread_info(ph, (pthread_t) -1, prstat->pr_pid)) == NULL) 517 return false; 518 519 // copy regs 520 memcpy(&newthr->regs, prstat->pr_reg, sizeof(struct user_regs_struct)); 521 522 if (is_debug()) { 523 print_debug("integer regset\n"); 524 #ifdef i386 525 // print the regset 526 print_debug("\teax = 0x%x\n", newthr->regs.eax); 527 print_debug("\tebx = 0x%x\n", newthr->regs.ebx); 528 print_debug("\tecx = 0x%x\n", newthr->regs.ecx); 529 print_debug("\tedx = 0x%x\n", newthr->regs.edx); 530 print_debug("\tesp = 0x%x\n", newthr->regs.esp); 531 print_debug("\tebp = 0x%x\n", newthr->regs.ebp); 532 print_debug("\tesi = 0x%x\n", newthr->regs.esi); 533 print_debug("\tedi = 0x%x\n", newthr->regs.edi); 534 print_debug("\teip = 0x%x\n", newthr->regs.eip); 535 #endif 536 537 #if defined(amd64) || defined(x86_64) 538 // print the regset 539 print_debug("\tr15 = 0x%lx\n", newthr->regs.r15); 540 print_debug("\tr14 = 0x%lx\n", newthr->regs.r14); 541 print_debug("\tr13 = 0x%lx\n", newthr->regs.r13); 542 print_debug("\tr12 = 0x%lx\n", newthr->regs.r12); 543 print_debug("\trbp = 0x%lx\n", newthr->regs.rbp); 544 print_debug("\trbx = 0x%lx\n", newthr->regs.rbx); 545 print_debug("\tr11 = 0x%lx\n", newthr->regs.r11); 546 print_debug("\tr10 = 0x%lx\n", newthr->regs.r10); 547 print_debug("\tr9 = 0x%lx\n", newthr->regs.r9); 548 print_debug("\tr8 = 0x%lx\n", newthr->regs.r8); 549 print_debug("\trax = 0x%lx\n", newthr->regs.rax); 550 print_debug("\trcx = 0x%lx\n", newthr->regs.rcx); 551 print_debug("\trdx = 0x%lx\n", newthr->regs.rdx); 552 print_debug("\trsi = 0x%lx\n", newthr->regs.rsi); 553 print_debug("\trdi = 0x%lx\n", newthr->regs.rdi); 554 print_debug("\torig_rax = 0x%lx\n", newthr->regs.orig_rax); 555 print_debug("\trip = 0x%lx\n", newthr->regs.rip); 556 print_debug("\tcs = 0x%lx\n", newthr->regs.cs); 557 print_debug("\teflags = 0x%lx\n", newthr->regs.eflags); 558 print_debug("\trsp = 0x%lx\n", newthr->regs.rsp); 559 print_debug("\tss = 0x%lx\n", newthr->regs.ss); 560 print_debug("\tfs_base = 0x%lx\n", newthr->regs.fs_base); 561 print_debug("\tgs_base = 0x%lx\n", newthr->regs.gs_base); 562 print_debug("\tds = 0x%lx\n", newthr->regs.ds); 563 print_debug("\tes = 0x%lx\n", newthr->regs.es); 564 print_debug("\tfs = 0x%lx\n", newthr->regs.fs); 565 print_debug("\tgs = 0x%lx\n", newthr->regs.gs); 566 #endif 567 } 568 569 return true; 570 } 571 572 #define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y)) 573 574 // read NT_PRSTATUS entries from core NOTE segment 575 static bool core_handle_note(struct ps_prochandle* ph, ELF_PHDR* note_phdr) { 576 char* buf = NULL; 577 char* p = NULL; 578 size_t size = note_phdr->p_filesz; 579 580 // we are interested in just prstatus entries. we will ignore the rest. 581 // Advance the seek pointer to the start of the PT_NOTE data 582 if (lseek(ph->core->core_fd, note_phdr->p_offset, SEEK_SET) == (off_t)-1) { 583 print_debug("failed to lseek to PT_NOTE data\n"); 584 return false; 585 } 586 587 // Now process the PT_NOTE structures. Each one is preceded by 588 // an Elf{32/64}_Nhdr structure describing its type and size. 589 if ( (buf = (char*) malloc(size)) == NULL) { 590 print_debug("can't allocate memory for reading core notes\n"); 591 goto err; 592 } 593 594 // read notes into buffer 595 if (read(ph->core->core_fd, buf, size) != size) { 596 print_debug("failed to read notes, core file must have been truncated\n"); 597 goto err; 598 } 599 600 p = buf; 601 while (p < buf + size) { 602 ELF_NHDR* notep = (ELF_NHDR*) p; 603 char* descdata = p + sizeof(ELF_NHDR) + ROUNDUP(notep->n_namesz, 4); 604 print_debug("Note header with n_type = %d and n_descsz = %u\n", 605 notep->n_type, notep->n_descsz); 606 607 if (notep->n_type == NT_PRSTATUS) { 608 if (core_handle_prstatus(ph, descdata, notep->n_descsz) != true) { 609 return false; 610 } 611 } else if (notep->n_type == NT_AUXV) { 612 // Get first segment from entry point 613 ELF_AUXV *auxv = (ELF_AUXV *)descdata; 614 while (auxv->a_type != AT_NULL) { 615 if (auxv->a_type == AT_ENTRY) { 616 // Set entry point address to address of dynamic section. 617 // We will adjust it in read_exec_segments(). 618 ph->core->dynamic_addr = auxv->a_un.a_val; 619 break; 620 } 621 auxv++; 622 } 623 } 624 p = descdata + ROUNDUP(notep->n_descsz, 4); 625 } 626 627 free(buf); 628 return true; 629 630 err: 631 if (buf) free(buf); 632 return false; 633 } 634 635 // read all segments from core file 636 static bool read_core_segments(struct ps_prochandle* ph, ELF_EHDR* core_ehdr) { 637 int i = 0; 638 ELF_PHDR* phbuf = NULL; 639 ELF_PHDR* core_php = NULL; 640 641 if ((phbuf = read_program_header_table(ph->core->core_fd, core_ehdr)) == NULL) 642 return false; 643 644 /* 645 * Now iterate through the program headers in the core file. 646 * We're interested in two types of Phdrs: PT_NOTE (which 647 * contains a set of saved /proc structures), and PT_LOAD (which 648 * represents a memory mapping from the process's address space). 649 * 650 * Difference b/w Solaris PT_NOTE and Linux/BSD PT_NOTE: 651 * 652 * In Solaris there are two PT_NOTE segments the first PT_NOTE (if present) 653 * contains /proc structs in the pre-2.6 unstructured /proc format. the last 654 * PT_NOTE has data in new /proc format. 655 * 656 * In Solaris, there is only one pstatus (process status). pstatus contains 657 * integer register set among other stuff. For each LWP, we have one lwpstatus 658 * entry that has integer regset for that LWP. 659 * 660 * Linux threads are actually 'clone'd processes. To support core analysis 661 * of "multithreaded" process, Linux creates more than one pstatus (called 662 * "prstatus") entry in PT_NOTE. Each prstatus entry has integer regset for one 663 * "thread". Please refer to Linux kernel src file 'fs/binfmt_elf.c', in particular 664 * function "elf_core_dump". 665 */ 666 667 for (core_php = phbuf, i = 0; i < core_ehdr->e_phnum; i++) { 668 switch (core_php->p_type) { 669 case PT_NOTE: 670 if (core_handle_note(ph, core_php) != true) { 671 goto err; 672 } 673 break; 674 675 case PT_LOAD: { 676 if (core_php->p_filesz != 0) { 677 if (add_map_info(ph, ph->core->core_fd, core_php->p_offset, 678 core_php->p_vaddr, core_php->p_filesz) == NULL) goto err; 679 } 680 break; 681 } 682 } 683 684 core_php++; 685 } 686 687 free(phbuf); 688 return true; 689 err: 690 free(phbuf); 691 return false; 692 } 693 694 // read segments of a shared object 695 static bool read_lib_segments(struct ps_prochandle* ph, int lib_fd, ELF_EHDR* lib_ehdr, uintptr_t lib_base) { 696 int i = 0; 697 ELF_PHDR* phbuf; 698 ELF_PHDR* lib_php = NULL; 699 700 int page_size = sysconf(_SC_PAGE_SIZE); 701 702 if ((phbuf = read_program_header_table(lib_fd, lib_ehdr)) == NULL) { 703 return false; 704 } 705 706 // we want to process only PT_LOAD segments that are not writable. 707 // i.e., text segments. The read/write/exec (data) segments would 708 // have been already added from core file segments. 709 for (lib_php = phbuf, i = 0; i < lib_ehdr->e_phnum; i++) { 710 if ((lib_php->p_type == PT_LOAD) && !(lib_php->p_flags & PF_W) && (lib_php->p_filesz != 0)) { 711 712 uintptr_t target_vaddr = lib_php->p_vaddr + lib_base; 713 map_info *existing_map = core_lookup(ph, target_vaddr); 714 715 if (existing_map == NULL){ 716 if (add_map_info(ph, lib_fd, lib_php->p_offset, 717 target_vaddr, lib_php->p_memsz) == NULL) { 718 goto err; 719 } 720 } else { 721 // Coredump stores value of p_memsz elf field 722 // rounded up to page boundary. 723 724 if ((existing_map->memsz != page_size) && 725 (existing_map->fd != lib_fd) && 726 (ROUNDUP(existing_map->memsz, page_size) != ROUNDUP(lib_php->p_memsz, page_size))) { 727 728 print_debug("address conflict @ 0x%lx (existing map size = %ld, size = %ld, flags = %d)\n", 729 target_vaddr, existing_map->memsz, lib_php->p_memsz, lib_php->p_flags); 730 goto err; 731 } 732 733 /* replace PT_LOAD segment with library segment */ 734 print_debug("overwrote with new address mapping (memsz %ld -> %ld)\n", 735 existing_map->memsz, ROUNDUP(lib_php->p_memsz, page_size)); 736 737 existing_map->fd = lib_fd; 738 existing_map->offset = lib_php->p_offset; 739 existing_map->memsz = ROUNDUP(lib_php->p_memsz, page_size); 740 } 741 } 742 743 lib_php++; 744 } 745 746 free(phbuf); 747 return true; 748 err: 749 free(phbuf); 750 return false; 751 } 752 753 // process segments from interpreter (ld.so or ld-linux.so) 754 static bool read_interp_segments(struct ps_prochandle* ph) { 755 ELF_EHDR interp_ehdr; 756 757 if (read_elf_header(ph->core->interp_fd, &interp_ehdr) != true) { 758 print_debug("interpreter is not a valid ELF file\n"); 759 return false; 760 } 761 762 if (read_lib_segments(ph, ph->core->interp_fd, &interp_ehdr, ph->core->ld_base_addr) != true) { 763 print_debug("can't read segments of interpreter\n"); 764 return false; 765 } 766 767 return true; 768 } 769 770 // process segments of a a.out 771 static bool read_exec_segments(struct ps_prochandle* ph, ELF_EHDR* exec_ehdr) { 772 int i = 0; 773 ELF_PHDR* phbuf = NULL; 774 ELF_PHDR* exec_php = NULL; 775 776 if ((phbuf = read_program_header_table(ph->core->exec_fd, exec_ehdr)) == NULL) { 777 return false; 778 } 779 780 for (exec_php = phbuf, i = 0; i < exec_ehdr->e_phnum; i++) { 781 switch (exec_php->p_type) { 782 783 // add mappings for PT_LOAD segments 784 case PT_LOAD: { 785 // add only non-writable segments of non-zero filesz 786 if (!(exec_php->p_flags & PF_W) && exec_php->p_filesz != 0) { 787 if (add_map_info(ph, ph->core->exec_fd, exec_php->p_offset, exec_php->p_vaddr, exec_php->p_filesz) == NULL) goto err; 788 } 789 break; 790 } 791 792 // read the interpreter and it's segments 793 case PT_INTERP: { 794 char interp_name[BUF_SIZE + 1]; 795 796 // BUF_SIZE is PATH_MAX + NAME_MAX + 1. 797 if (exec_php->p_filesz > BUF_SIZE) { 798 goto err; 799 } 800 pread(ph->core->exec_fd, interp_name, exec_php->p_filesz, exec_php->p_offset); 801 interp_name[exec_php->p_filesz] = '\0'; 802 print_debug("ELF interpreter %s\n", interp_name); 803 // read interpreter segments as well 804 if ((ph->core->interp_fd = pathmap_open(interp_name)) < 0) { 805 print_debug("can't open runtime loader\n"); 806 goto err; 807 } 808 break; 809 } 810 811 // from PT_DYNAMIC we want to read address of first link_map addr 812 case PT_DYNAMIC: { 813 if (exec_ehdr->e_type == ET_EXEC) { 814 ph->core->dynamic_addr = exec_php->p_vaddr; 815 } else { // ET_DYN 816 // dynamic_addr has entry point of executable. 817 // Thus we should substract it. 818 ph->core->dynamic_addr += exec_php->p_vaddr - exec_ehdr->e_entry; 819 } 820 print_debug("address of _DYNAMIC is 0x%lx\n", ph->core->dynamic_addr); 821 break; 822 } 823 824 } // switch 825 exec_php++; 826 } // for 827 828 free(phbuf); 829 return true; 830 err: 831 free(phbuf); 832 return false; 833 } 834 835 836 #define FIRST_LINK_MAP_OFFSET offsetof(struct r_debug, r_map) 837 #define LD_BASE_OFFSET offsetof(struct r_debug, r_ldbase) 838 #define LINK_MAP_ADDR_OFFSET offsetof(struct link_map, l_addr) 839 #define LINK_MAP_NAME_OFFSET offsetof(struct link_map, l_name) 840 #define LINK_MAP_NEXT_OFFSET offsetof(struct link_map, l_next) 841 842 // read shared library info from runtime linker's data structures. 843 // This work is done by librtlb_db in Solaris 844 static bool read_shared_lib_info(struct ps_prochandle* ph) { 845 uintptr_t addr = ph->core->dynamic_addr; 846 uintptr_t debug_base; 847 uintptr_t first_link_map_addr; 848 uintptr_t ld_base_addr; 849 uintptr_t link_map_addr; 850 uintptr_t lib_base_diff; 851 uintptr_t lib_base; 852 uintptr_t lib_name_addr; 853 char lib_name[BUF_SIZE]; 854 ELF_DYN dyn; 855 ELF_EHDR elf_ehdr; 856 int lib_fd; 857 858 // _DYNAMIC has information of the form 859 // [tag] [data] [tag] [data] ..... 860 // Both tag and data are pointer sized. 861 // We look for dynamic info with DT_DEBUG. This has shared object info. 862 // refer to struct r_debug in link.h 863 864 dyn.d_tag = DT_NULL; 865 while (dyn.d_tag != DT_DEBUG) { 866 if (ps_pdread(ph, (psaddr_t) addr, &dyn, sizeof(ELF_DYN)) != PS_OK) { 867 print_debug("can't read debug info from _DYNAMIC\n"); 868 return false; 869 } 870 addr += sizeof(ELF_DYN); 871 } 872 873 // we have got Dyn entry with DT_DEBUG 874 debug_base = dyn.d_un.d_ptr; 875 // at debug_base we have struct r_debug. This has first link map in r_map field 876 if (ps_pdread(ph, (psaddr_t) debug_base + FIRST_LINK_MAP_OFFSET, 877 &first_link_map_addr, sizeof(uintptr_t)) != PS_OK) { 878 print_debug("can't read first link map address\n"); 879 return false; 880 } 881 882 // read ld_base address from struct r_debug 883 if (ps_pdread(ph, (psaddr_t) debug_base + LD_BASE_OFFSET, &ld_base_addr, 884 sizeof(uintptr_t)) != PS_OK) { 885 print_debug("can't read ld base address\n"); 886 return false; 887 } 888 ph->core->ld_base_addr = ld_base_addr; 889 890 print_debug("interpreter base address is 0x%lx\n", ld_base_addr); 891 892 // now read segments from interp (i.e ld.so or ld-linux.so or ld-elf.so) 893 if (read_interp_segments(ph) != true) { 894 return false; 895 } 896 897 // after adding interpreter (ld.so) mappings sort again 898 if (sort_map_array(ph) != true) { 899 return false; 900 } 901 902 print_debug("first link map is at 0x%lx\n", first_link_map_addr); 903 904 link_map_addr = first_link_map_addr; 905 while (link_map_addr != 0) { 906 // read library base address of the .so. Note that even though <sys/link.h> calls 907 // link_map->l_addr as "base address", this is * not * really base virtual 908 // address of the shared object. This is actually the difference b/w the virtual 909 // address mentioned in shared object and the actual virtual base where runtime 910 // linker loaded it. We use "base diff" in read_lib_segments call below. 911 912 if (ps_pdread(ph, (psaddr_t) link_map_addr + LINK_MAP_ADDR_OFFSET, 913 &lib_base_diff, sizeof(uintptr_t)) != PS_OK) { 914 print_debug("can't read shared object base address diff\n"); 915 return false; 916 } 917 918 // read address of the name 919 if (ps_pdread(ph, (psaddr_t) link_map_addr + LINK_MAP_NAME_OFFSET, 920 &lib_name_addr, sizeof(uintptr_t)) != PS_OK) { 921 print_debug("can't read address of shared object name\n"); 922 return false; 923 } 924 925 // read name of the shared object 926 lib_name[0] = '\0'; 927 if (lib_name_addr != 0 && 928 read_string(ph, (uintptr_t) lib_name_addr, lib_name, sizeof(lib_name)) != true) { 929 print_debug("can't read shared object name\n"); 930 // don't let failure to read the name stop opening the file. If something is really wrong 931 // it will fail later. 932 } 933 934 if (lib_name[0] != '\0') { 935 // ignore empty lib names 936 lib_fd = pathmap_open(lib_name); 937 938 if (lib_fd < 0) { 939 print_debug("can't open shared object %s\n", lib_name); 940 // continue with other libraries... 941 } else { 942 if (read_elf_header(lib_fd, &elf_ehdr)) { 943 lib_base = lib_base_diff + find_base_address(lib_fd, &elf_ehdr); 944 print_debug("reading library %s @ 0x%lx [ 0x%lx ]\n", 945 lib_name, lib_base, lib_base_diff); 946 // while adding library mappings we need to use "base difference". 947 if (! read_lib_segments(ph, lib_fd, &elf_ehdr, lib_base_diff)) { 948 print_debug("can't read shared object's segments\n"); 949 close(lib_fd); 950 return false; 951 } 952 add_lib_info_fd(ph, lib_name, lib_fd, lib_base); 953 // Map info is added for the library (lib_name) so 954 // we need to re-sort it before calling the p_pdread. 955 if (sort_map_array(ph) != true) 956 return false; 957 } else { 958 print_debug("can't read ELF header for shared object %s\n", lib_name); 959 close(lib_fd); 960 // continue with other libraries... 961 } 962 } 963 } 964 965 // read next link_map address 966 if (ps_pdread(ph, (psaddr_t) link_map_addr + LINK_MAP_NEXT_OFFSET, 967 &link_map_addr, sizeof(uintptr_t)) != PS_OK) { 968 print_debug("can't read next link in link_map\n"); 969 return false; 970 } 971 } 972 973 return true; 974 } 975 976 // the one and only one exposed stuff from this file 977 JNIEXPORT struct ps_prochandle* JNICALL 978 Pgrab_core(const char* exec_file, const char* core_file) { 979 ELF_EHDR core_ehdr; 980 ELF_EHDR exec_ehdr; 981 ELF_EHDR lib_ehdr; 982 983 struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle)); 984 if (ph == NULL) { 985 print_debug("can't allocate ps_prochandle\n"); 986 return NULL; 987 } 988 989 if ((ph->core = (struct core_data*) calloc(1, sizeof(struct core_data))) == NULL) { 990 free(ph); 991 print_debug("can't allocate ps_prochandle\n"); 992 return NULL; 993 } 994 995 // initialize ph 996 ph->ops = &core_ops; 997 ph->core->core_fd = -1; 998 ph->core->exec_fd = -1; 999 ph->core->interp_fd = -1; 1000 1001 // open the core file 1002 if ((ph->core->core_fd = open(core_file, O_RDONLY)) < 0) { 1003 print_debug("can't open core file\n"); 1004 goto err; 1005 } 1006 1007 // read core file ELF header 1008 if (read_elf_header(ph->core->core_fd, &core_ehdr) != true || core_ehdr.e_type != ET_CORE) { 1009 print_debug("core file is not a valid ELF ET_CORE file\n"); 1010 goto err; 1011 } 1012 1013 if ((ph->core->exec_fd = open(exec_file, O_RDONLY)) < 0) { 1014 print_debug("can't open executable file\n"); 1015 goto err; 1016 } 1017 1018 if (read_elf_header(ph->core->exec_fd, &exec_ehdr) != true || 1019 ((exec_ehdr.e_type != ET_EXEC) && (exec_ehdr.e_type != ET_DYN))) { 1020 print_debug("executable file is not a valid ELF file\n"); 1021 goto err; 1022 } 1023 1024 // process core file segments 1025 if (read_core_segments(ph, &core_ehdr) != true) { 1026 goto err; 1027 } 1028 1029 // process exec file segments 1030 if (read_exec_segments(ph, &exec_ehdr) != true) { 1031 goto err; 1032 } 1033 1034 // exec file is also treated like a shared object for symbol search 1035 if (add_lib_info_fd(ph, exec_file, ph->core->exec_fd, 1036 (uintptr_t)0 + find_base_address(ph->core->exec_fd, &exec_ehdr)) == NULL) { 1037 goto err; 1038 } 1039 1040 // allocate and sort maps into map_array, we need to do this 1041 // here because read_shared_lib_info needs to read from debuggee 1042 // address space 1043 if (sort_map_array(ph) != true) { 1044 goto err; 1045 } 1046 1047 if (read_shared_lib_info(ph) != true) { 1048 goto err; 1049 } 1050 1051 // sort again because we have added more mappings from shared objects 1052 if (sort_map_array(ph) != true) { 1053 goto err; 1054 } 1055 1056 if (init_classsharing_workaround(ph) != true) { 1057 goto err; 1058 } 1059 1060 return ph; 1061 1062 err: 1063 Prelease(ph); 1064 return NULL; 1065 }