1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/agent/src/os/linux/ps_core.c Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,1011 @@ 1.4 +/* 1.5 + * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include <jni.h> 1.29 +#include <unistd.h> 1.30 +#include <fcntl.h> 1.31 +#include <string.h> 1.32 +#include <stdlib.h> 1.33 +#include <stddef.h> 1.34 +#include <elf.h> 1.35 +#include <link.h> 1.36 +#include "libproc_impl.h" 1.37 +#include "salibelf.h" 1.38 + 1.39 +// This file has the libproc implementation to read core files. 1.40 +// For live processes, refer to ps_proc.c. Portions of this is adapted 1.41 +// /modelled after Solaris libproc.so (in particular Pcore.c) 1.42 + 1.43 +//---------------------------------------------------------------------- 1.44 +// ps_prochandle cleanup helper functions 1.45 + 1.46 +// close all file descriptors 1.47 +static void close_elf_files(struct ps_prochandle* ph) { 1.48 + lib_info* lib = NULL; 1.49 + 1.50 + // close core file descriptor 1.51 + if (ph->core->core_fd >= 0) 1.52 + close(ph->core->core_fd); 1.53 + 1.54 + // close exec file descriptor 1.55 + if (ph->core->exec_fd >= 0) 1.56 + close(ph->core->exec_fd); 1.57 + 1.58 + // close interp file descriptor 1.59 + if (ph->core->interp_fd >= 0) 1.60 + close(ph->core->interp_fd); 1.61 + 1.62 + // close class share archive file 1.63 + if (ph->core->classes_jsa_fd >= 0) 1.64 + close(ph->core->classes_jsa_fd); 1.65 + 1.66 + // close all library file descriptors 1.67 + lib = ph->libs; 1.68 + while (lib) { 1.69 + int fd = lib->fd; 1.70 + if (fd >= 0 && fd != ph->core->exec_fd) close(fd); 1.71 + lib = lib->next; 1.72 + } 1.73 +} 1.74 + 1.75 +// clean all map_info stuff 1.76 +static void destroy_map_info(struct ps_prochandle* ph) { 1.77 + map_info* map = ph->core->maps; 1.78 + while (map) { 1.79 + map_info* next = map->next; 1.80 + free(map); 1.81 + map = next; 1.82 + } 1.83 + 1.84 + if (ph->core->map_array) { 1.85 + free(ph->core->map_array); 1.86 + } 1.87 + 1.88 + // Part of the class sharing workaround 1.89 + map = ph->core->class_share_maps; 1.90 + while (map) { 1.91 + map_info* next = map->next; 1.92 + free(map); 1.93 + map = next; 1.94 + } 1.95 +} 1.96 + 1.97 +// ps_prochandle operations 1.98 +static void core_release(struct ps_prochandle* ph) { 1.99 + if (ph->core) { 1.100 + close_elf_files(ph); 1.101 + destroy_map_info(ph); 1.102 + free(ph->core); 1.103 + } 1.104 +} 1.105 + 1.106 +static map_info* allocate_init_map(int fd, off_t offset, uintptr_t vaddr, size_t memsz) { 1.107 + map_info* map; 1.108 + if ( (map = (map_info*) calloc(1, sizeof(map_info))) == NULL) { 1.109 + print_debug("can't allocate memory for map_info\n"); 1.110 + return NULL; 1.111 + } 1.112 + 1.113 + // initialize map 1.114 + map->fd = fd; 1.115 + map->offset = offset; 1.116 + map->vaddr = vaddr; 1.117 + map->memsz = memsz; 1.118 + return map; 1.119 +} 1.120 + 1.121 +// add map info with given fd, offset, vaddr and memsz 1.122 +static map_info* add_map_info(struct ps_prochandle* ph, int fd, off_t offset, 1.123 + uintptr_t vaddr, size_t memsz) { 1.124 + map_info* map; 1.125 + if ((map = allocate_init_map(fd, offset, vaddr, memsz)) == NULL) { 1.126 + return NULL; 1.127 + } 1.128 + 1.129 + // add this to map list 1.130 + map->next = ph->core->maps; 1.131 + ph->core->maps = map; 1.132 + ph->core->num_maps++; 1.133 + 1.134 + return map; 1.135 +} 1.136 + 1.137 +// Part of the class sharing workaround 1.138 +static map_info* add_class_share_map_info(struct ps_prochandle* ph, off_t offset, 1.139 + uintptr_t vaddr, size_t memsz) { 1.140 + map_info* map; 1.141 + if ((map = allocate_init_map(ph->core->classes_jsa_fd, 1.142 + offset, vaddr, memsz)) == NULL) { 1.143 + return NULL; 1.144 + } 1.145 + 1.146 + map->next = ph->core->class_share_maps; 1.147 + ph->core->class_share_maps = map; 1.148 +} 1.149 + 1.150 +// Return the map_info for the given virtual address. We keep a sorted 1.151 +// array of pointers in ph->map_array, so we can binary search. 1.152 +static map_info* core_lookup(struct ps_prochandle *ph, uintptr_t addr) 1.153 +{ 1.154 + int mid, lo = 0, hi = ph->core->num_maps - 1; 1.155 + map_info *mp; 1.156 + 1.157 + while (hi - lo > 1) { 1.158 + mid = (lo + hi) / 2; 1.159 + if (addr >= ph->core->map_array[mid]->vaddr) 1.160 + lo = mid; 1.161 + else 1.162 + hi = mid; 1.163 + } 1.164 + 1.165 + if (addr < ph->core->map_array[hi]->vaddr) 1.166 + mp = ph->core->map_array[lo]; 1.167 + else 1.168 + mp = ph->core->map_array[hi]; 1.169 + 1.170 + if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) 1.171 + return (mp); 1.172 + 1.173 + 1.174 + // Part of the class sharing workaround 1.175 + // Unfortunately, we have no way of detecting -Xshare state. 1.176 + // Check out the share maps atlast, if we don't find anywhere. 1.177 + // This is done this way so to avoid reading share pages 1.178 + // ahead of other normal maps. For eg. with -Xshare:off we don't 1.179 + // want to prefer class sharing data to data from core. 1.180 + mp = ph->core->class_share_maps; 1.181 + if (mp) { 1.182 + print_debug("can't locate map_info at 0x%lx, trying class share maps\n", 1.183 + addr); 1.184 + } 1.185 + while (mp) { 1.186 + if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) { 1.187 + print_debug("located map_info at 0x%lx from class share maps\n", 1.188 + addr); 1.189 + return (mp); 1.190 + } 1.191 + mp = mp->next; 1.192 + } 1.193 + 1.194 + print_debug("can't locate map_info at 0x%lx\n", addr); 1.195 + return (NULL); 1.196 +} 1.197 + 1.198 +//--------------------------------------------------------------- 1.199 +// Part of the class sharing workaround: 1.200 +// 1.201 +// With class sharing, pages are mapped from classes[_g].jsa file. 1.202 +// The read-only class sharing pages are mapped as MAP_SHARED, 1.203 +// PROT_READ pages. These pages are not dumped into core dump. 1.204 +// With this workaround, these pages are read from classes[_g].jsa. 1.205 + 1.206 +// FIXME: !HACK ALERT! 1.207 +// The format of sharing achive file header is needed to read shared heap 1.208 +// file mappings. For now, I am hard coding portion of FileMapHeader here. 1.209 +// Refer to filemap.hpp. 1.210 + 1.211 +// FileMapHeader describes the shared space data in the file to be 1.212 +// mapped. This structure gets written to a file. It is not a class, 1.213 +// so that the compilers don't add any compiler-private data to it. 1.214 + 1.215 +// Refer to CompactingPermGenGen::n_regions in compactingPermGenGen.hpp 1.216 +#define NUM_SHARED_MAPS 4 1.217 + 1.218 +// Refer to FileMapInfo::_current_version in filemap.hpp 1.219 +#define CURRENT_ARCHIVE_VERSION 1 1.220 + 1.221 +struct FileMapHeader { 1.222 + int _magic; // identify file type. 1.223 + int _version; // (from enum, above.) 1.224 + size_t _alignment; // how shared archive should be aligned 1.225 + 1.226 + struct space_info { 1.227 + int _file_offset; // sizeof(this) rounded to vm page size 1.228 + char* _base; // copy-on-write base address 1.229 + size_t _capacity; // for validity checking 1.230 + size_t _used; // for setting space top on read 1.231 + 1.232 + // 4991491 NOTICE These are C++ bool's in filemap.hpp and must match up with 1.233 + // the C type matching the C++ bool type on any given platform. For 1.234 + // Hotspot on Linux we assume the corresponding C type is char but 1.235 + // licensees on Linux versions may need to adjust the type of these fields. 1.236 + char _read_only; // read only space? 1.237 + char _allow_exec; // executable code in space? 1.238 + 1.239 + } _space[NUM_SHARED_MAPS]; // was _space[CompactingPermGenGen::n_regions]; 1.240 + 1.241 + // Ignore the rest of the FileMapHeader. We don't need those fields here. 1.242 +}; 1.243 + 1.244 +static bool read_int(struct ps_prochandle* ph, uintptr_t addr, int* pvalue) { 1.245 + int i; 1.246 + if (ps_pdread(ph, (psaddr_t) addr, &i, sizeof(i)) == PS_OK) { 1.247 + *pvalue = i; 1.248 + return true; 1.249 + } else { 1.250 + return false; 1.251 + } 1.252 +} 1.253 + 1.254 +static bool read_pointer(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* pvalue) { 1.255 + uintptr_t uip; 1.256 + if (ps_pdread(ph, (psaddr_t) addr, &uip, sizeof(uip)) == PS_OK) { 1.257 + *pvalue = uip; 1.258 + return true; 1.259 + } else { 1.260 + return false; 1.261 + } 1.262 +} 1.263 + 1.264 +// used to read strings from debuggee 1.265 +static bool read_string(struct ps_prochandle* ph, uintptr_t addr, char* buf, size_t size) { 1.266 + size_t i = 0; 1.267 + char c = ' '; 1.268 + 1.269 + while (c != '\0') { 1.270 + if (ps_pdread(ph, (psaddr_t) addr, &c, sizeof(char)) != PS_OK) 1.271 + return false; 1.272 + if (i < size - 1) 1.273 + buf[i] = c; 1.274 + else // smaller buffer 1.275 + return false; 1.276 + i++; addr++; 1.277 + } 1.278 + 1.279 + buf[i] = '\0'; 1.280 + return true; 1.281 +} 1.282 + 1.283 +#define USE_SHARED_SPACES_SYM "UseSharedSpaces" 1.284 +// mangled name of Arguments::SharedArchivePath 1.285 +#define SHARED_ARCHIVE_PATH_SYM "_ZN9Arguments17SharedArchivePathE" 1.286 + 1.287 +static bool init_classsharing_workaround(struct ps_prochandle* ph) { 1.288 + lib_info* lib = ph->libs; 1.289 + while (lib != NULL) { 1.290 + // we are iterating over shared objects from the core dump. look for 1.291 + // libjvm[_g].so. 1.292 + const char *jvm_name = 0; 1.293 + if ((jvm_name = strstr(lib->name, "/libjvm.so")) != 0 || 1.294 + (jvm_name = strstr(lib->name, "/libjvm_g.so")) != 0) { 1.295 + char classes_jsa[PATH_MAX]; 1.296 + struct FileMapHeader header; 1.297 + size_t n = 0; 1.298 + int fd = -1, m = 0; 1.299 + uintptr_t base = 0, useSharedSpacesAddr = 0; 1.300 + uintptr_t sharedArchivePathAddrAddr = 0, sharedArchivePathAddr = 0; 1.301 + int useSharedSpaces = 0; 1.302 + map_info* mi = 0; 1.303 + 1.304 + memset(classes_jsa, 0, sizeof(classes_jsa)); 1.305 + jvm_name = lib->name; 1.306 + useSharedSpacesAddr = lookup_symbol(ph, jvm_name, USE_SHARED_SPACES_SYM); 1.307 + if (useSharedSpacesAddr == 0) { 1.308 + print_debug("can't lookup 'UseSharedSpaces' flag\n"); 1.309 + return false; 1.310 + } 1.311 + 1.312 + if (read_int(ph, useSharedSpacesAddr, &useSharedSpaces) != true) { 1.313 + print_debug("can't read the value of 'UseSharedSpaces' flag\n"); 1.314 + return false; 1.315 + } 1.316 + 1.317 + if (useSharedSpaces == 0) { 1.318 + print_debug("UseSharedSpaces is false, assuming -Xshare:off!\n"); 1.319 + return true; 1.320 + } 1.321 + 1.322 + sharedArchivePathAddrAddr = lookup_symbol(ph, jvm_name, SHARED_ARCHIVE_PATH_SYM); 1.323 + if (sharedArchivePathAddrAddr == 0) { 1.324 + print_debug("can't lookup shared archive path symbol\n"); 1.325 + return false; 1.326 + } 1.327 + 1.328 + if (read_pointer(ph, sharedArchivePathAddrAddr, &sharedArchivePathAddr) != true) { 1.329 + print_debug("can't read shared archive path pointer\n"); 1.330 + return false; 1.331 + } 1.332 + 1.333 + if (read_string(ph, sharedArchivePathAddr, classes_jsa, sizeof(classes_jsa)) != true) { 1.334 + print_debug("can't read shared archive path value\n"); 1.335 + return false; 1.336 + } 1.337 + 1.338 + print_debug("looking for %s\n", classes_jsa); 1.339 + // open the class sharing archive file 1.340 + fd = pathmap_open(classes_jsa); 1.341 + if (fd < 0) { 1.342 + print_debug("can't open %s!\n", classes_jsa); 1.343 + ph->core->classes_jsa_fd = -1; 1.344 + return false; 1.345 + } else { 1.346 + print_debug("opened %s\n", classes_jsa); 1.347 + } 1.348 + 1.349 + // read FileMapHeader from the file 1.350 + memset(&header, 0, sizeof(struct FileMapHeader)); 1.351 + if ((n = read(fd, &header, sizeof(struct FileMapHeader))) 1.352 + != sizeof(struct FileMapHeader)) { 1.353 + print_debug("can't read shared archive file map header from %s\n", classes_jsa); 1.354 + close(fd); 1.355 + return false; 1.356 + } 1.357 + 1.358 + // check file magic 1.359 + if (header._magic != 0xf00baba2) { 1.360 + print_debug("%s has bad shared archive file magic number 0x%x, expecing 0xf00baba2\n", 1.361 + classes_jsa, header._magic); 1.362 + close(fd); 1.363 + return false; 1.364 + } 1.365 + 1.366 + // check version 1.367 + if (header._version != CURRENT_ARCHIVE_VERSION) { 1.368 + print_debug("%s has wrong shared archive file version %d, expecting %d\n", 1.369 + classes_jsa, header._version, CURRENT_ARCHIVE_VERSION); 1.370 + close(fd); 1.371 + return false; 1.372 + } 1.373 + 1.374 + ph->core->classes_jsa_fd = fd; 1.375 + // add read-only maps from classes[_g].jsa to the list of maps 1.376 + for (m = 0; m < NUM_SHARED_MAPS; m++) { 1.377 + if (header._space[m]._read_only) { 1.378 + base = (uintptr_t) header._space[m]._base; 1.379 + // no need to worry about the fractional pages at-the-end. 1.380 + // possible fractional pages are handled by core_read_data. 1.381 + add_class_share_map_info(ph, (off_t) header._space[m]._file_offset, 1.382 + base, (size_t) header._space[m]._used); 1.383 + print_debug("added a share archive map at 0x%lx\n", base); 1.384 + } 1.385 + } 1.386 + return true; 1.387 + } 1.388 + lib = lib->next; 1.389 + } 1.390 + return true; 1.391 +} 1.392 + 1.393 + 1.394 +//--------------------------------------------------------------------------- 1.395 +// functions to handle map_info 1.396 + 1.397 +// Order mappings based on virtual address. We use this function as the 1.398 +// callback for sorting the array of map_info pointers. 1.399 +static int core_cmp_mapping(const void *lhsp, const void *rhsp) 1.400 +{ 1.401 + const map_info *lhs = *((const map_info **)lhsp); 1.402 + const map_info *rhs = *((const map_info **)rhsp); 1.403 + 1.404 + if (lhs->vaddr == rhs->vaddr) 1.405 + return (0); 1.406 + 1.407 + return (lhs->vaddr < rhs->vaddr ? -1 : 1); 1.408 +} 1.409 + 1.410 +// we sort map_info by starting virtual address so that we can do 1.411 +// binary search to read from an address. 1.412 +static bool sort_map_array(struct ps_prochandle* ph) { 1.413 + size_t num_maps = ph->core->num_maps; 1.414 + map_info* map = ph->core->maps; 1.415 + int i = 0; 1.416 + 1.417 + // allocate map_array 1.418 + map_info** array; 1.419 + if ( (array = (map_info**) malloc(sizeof(map_info*) * num_maps)) == NULL) { 1.420 + print_debug("can't allocate memory for map array\n"); 1.421 + return false; 1.422 + } 1.423 + 1.424 + // add maps to array 1.425 + while (map) { 1.426 + array[i] = map; 1.427 + i++; 1.428 + map = map->next; 1.429 + } 1.430 + 1.431 + // sort is called twice. If this is second time, clear map array 1.432 + if (ph->core->map_array) free(ph->core->map_array); 1.433 + ph->core->map_array = array; 1.434 + // sort the map_info array by base virtual address. 1.435 + qsort(ph->core->map_array, ph->core->num_maps, sizeof (map_info*), 1.436 + core_cmp_mapping); 1.437 + 1.438 + // print map 1.439 + if (is_debug()) { 1.440 + int j = 0; 1.441 + print_debug("---- sorted virtual address map ----\n"); 1.442 + for (j = 0; j < ph->core->num_maps; j++) { 1.443 + print_debug("base = 0x%lx\tsize = %d\n", ph->core->map_array[j]->vaddr, 1.444 + ph->core->map_array[j]->memsz); 1.445 + } 1.446 + } 1.447 + 1.448 + return true; 1.449 +} 1.450 + 1.451 +#ifndef MIN 1.452 +#define MIN(x, y) (((x) < (y))? (x): (y)) 1.453 +#endif 1.454 + 1.455 +static bool core_read_data(struct ps_prochandle* ph, uintptr_t addr, char *buf, size_t size) { 1.456 + ssize_t resid = size; 1.457 + int page_size=sysconf(_SC_PAGE_SIZE); 1.458 + while (resid != 0) { 1.459 + map_info *mp = core_lookup(ph, addr); 1.460 + uintptr_t mapoff; 1.461 + ssize_t len, rem; 1.462 + off_t off; 1.463 + int fd; 1.464 + 1.465 + if (mp == NULL) 1.466 + break; /* No mapping for this address */ 1.467 + 1.468 + fd = mp->fd; 1.469 + mapoff = addr - mp->vaddr; 1.470 + len = MIN(resid, mp->memsz - mapoff); 1.471 + off = mp->offset + mapoff; 1.472 + 1.473 + if ((len = pread(fd, buf, len, off)) <= 0) 1.474 + break; 1.475 + 1.476 + resid -= len; 1.477 + addr += len; 1.478 + buf = (char *)buf + len; 1.479 + 1.480 + // mappings always start at page boundary. But, may end in fractional 1.481 + // page. fill zeros for possible fractional page at the end of a mapping. 1.482 + rem = mp->memsz % page_size; 1.483 + if (rem > 0) { 1.484 + rem = page_size - rem; 1.485 + len = MIN(resid, rem); 1.486 + resid -= len; 1.487 + addr += len; 1.488 + // we are not assuming 'buf' to be zero initialized. 1.489 + memset(buf, 0, len); 1.490 + buf += len; 1.491 + } 1.492 + } 1.493 + 1.494 + if (resid) { 1.495 + print_debug("core read failed for %d byte(s) @ 0x%lx (%d more bytes)\n", 1.496 + size, addr, resid); 1.497 + return false; 1.498 + } else { 1.499 + return true; 1.500 + } 1.501 +} 1.502 + 1.503 +// null implementation for write 1.504 +static bool core_write_data(struct ps_prochandle* ph, 1.505 + uintptr_t addr, const char *buf , size_t size) { 1.506 + return false; 1.507 +} 1.508 + 1.509 +static bool core_get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id, 1.510 + struct user_regs_struct* regs) { 1.511 + // for core we have cached the lwp regs from NOTE section 1.512 + thread_info* thr = ph->threads; 1.513 + while (thr) { 1.514 + if (thr->lwp_id == lwp_id) { 1.515 + memcpy(regs, &thr->regs, sizeof(struct user_regs_struct)); 1.516 + return true; 1.517 + } 1.518 + thr = thr->next; 1.519 + } 1.520 + return false; 1.521 +} 1.522 + 1.523 +static ps_prochandle_ops core_ops = { 1.524 + release: core_release, 1.525 + p_pread: core_read_data, 1.526 + p_pwrite: core_write_data, 1.527 + get_lwp_regs: core_get_lwp_regs 1.528 +}; 1.529 + 1.530 +// read regs and create thread from NT_PRSTATUS entries from core file 1.531 +static bool core_handle_prstatus(struct ps_prochandle* ph, const char* buf, size_t nbytes) { 1.532 + // we have to read prstatus_t from buf 1.533 + // assert(nbytes == sizeof(prstaus_t), "size mismatch on prstatus_t"); 1.534 + prstatus_t* prstat = (prstatus_t*) buf; 1.535 + thread_info* newthr; 1.536 + print_debug("got integer regset for lwp %d\n", prstat->pr_pid); 1.537 + // we set pthread_t to -1 for core dump 1.538 + if((newthr = add_thread_info(ph, (pthread_t) -1, prstat->pr_pid)) == NULL) 1.539 + return false; 1.540 + 1.541 + // copy regs 1.542 + memcpy(&newthr->regs, prstat->pr_reg, sizeof(struct user_regs_struct)); 1.543 + 1.544 + if (is_debug()) { 1.545 + print_debug("integer regset\n"); 1.546 +#ifdef i386 1.547 + // print the regset 1.548 + print_debug("\teax = 0x%x\n", newthr->regs.eax); 1.549 + print_debug("\tebx = 0x%x\n", newthr->regs.ebx); 1.550 + print_debug("\tecx = 0x%x\n", newthr->regs.ecx); 1.551 + print_debug("\tedx = 0x%x\n", newthr->regs.edx); 1.552 + print_debug("\tesp = 0x%x\n", newthr->regs.esp); 1.553 + print_debug("\tebp = 0x%x\n", newthr->regs.ebp); 1.554 + print_debug("\tesi = 0x%x\n", newthr->regs.esi); 1.555 + print_debug("\tedi = 0x%x\n", newthr->regs.edi); 1.556 + print_debug("\teip = 0x%x\n", newthr->regs.eip); 1.557 +#endif 1.558 + 1.559 +#if defined(amd64) || defined(x86_64) 1.560 + // print the regset 1.561 + print_debug("\tr15 = 0x%lx\n", newthr->regs.r15); 1.562 + print_debug("\tr14 = 0x%lx\n", newthr->regs.r14); 1.563 + print_debug("\tr13 = 0x%lx\n", newthr->regs.r13); 1.564 + print_debug("\tr12 = 0x%lx\n", newthr->regs.r12); 1.565 + print_debug("\trbp = 0x%lx\n", newthr->regs.rbp); 1.566 + print_debug("\trbx = 0x%lx\n", newthr->regs.rbx); 1.567 + print_debug("\tr11 = 0x%lx\n", newthr->regs.r11); 1.568 + print_debug("\tr10 = 0x%lx\n", newthr->regs.r10); 1.569 + print_debug("\tr9 = 0x%lx\n", newthr->regs.r9); 1.570 + print_debug("\tr8 = 0x%lx\n", newthr->regs.r8); 1.571 + print_debug("\trax = 0x%lx\n", newthr->regs.rax); 1.572 + print_debug("\trcx = 0x%lx\n", newthr->regs.rcx); 1.573 + print_debug("\trdx = 0x%lx\n", newthr->regs.rdx); 1.574 + print_debug("\trsi = 0x%lx\n", newthr->regs.rsi); 1.575 + print_debug("\trdi = 0x%lx\n", newthr->regs.rdi); 1.576 + print_debug("\torig_rax = 0x%lx\n", newthr->regs.orig_rax); 1.577 + print_debug("\trip = 0x%lx\n", newthr->regs.rip); 1.578 + print_debug("\tcs = 0x%lx\n", newthr->regs.cs); 1.579 + print_debug("\teflags = 0x%lx\n", newthr->regs.eflags); 1.580 + print_debug("\trsp = 0x%lx\n", newthr->regs.rsp); 1.581 + print_debug("\tss = 0x%lx\n", newthr->regs.ss); 1.582 + print_debug("\tfs_base = 0x%lx\n", newthr->regs.fs_base); 1.583 + print_debug("\tgs_base = 0x%lx\n", newthr->regs.gs_base); 1.584 + print_debug("\tds = 0x%lx\n", newthr->regs.ds); 1.585 + print_debug("\tes = 0x%lx\n", newthr->regs.es); 1.586 + print_debug("\tfs = 0x%lx\n", newthr->regs.fs); 1.587 + print_debug("\tgs = 0x%lx\n", newthr->regs.gs); 1.588 +#endif 1.589 + } 1.590 + 1.591 + return true; 1.592 +} 1.593 + 1.594 +#define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y)) 1.595 + 1.596 +// read NT_PRSTATUS entries from core NOTE segment 1.597 +static bool core_handle_note(struct ps_prochandle* ph, ELF_PHDR* note_phdr) { 1.598 + char* buf = NULL; 1.599 + char* p = NULL; 1.600 + size_t size = note_phdr->p_filesz; 1.601 + 1.602 + // we are interested in just prstatus entries. we will ignore the rest. 1.603 + // Advance the seek pointer to the start of the PT_NOTE data 1.604 + if (lseek(ph->core->core_fd, note_phdr->p_offset, SEEK_SET) == (off_t)-1) { 1.605 + print_debug("failed to lseek to PT_NOTE data\n"); 1.606 + return false; 1.607 + } 1.608 + 1.609 + // Now process the PT_NOTE structures. Each one is preceded by 1.610 + // an Elf{32/64}_Nhdr structure describing its type and size. 1.611 + if ( (buf = (char*) malloc(size)) == NULL) { 1.612 + print_debug("can't allocate memory for reading core notes\n"); 1.613 + goto err; 1.614 + } 1.615 + 1.616 + // read notes into buffer 1.617 + if (read(ph->core->core_fd, buf, size) != size) { 1.618 + print_debug("failed to read notes, core file must have been truncated\n"); 1.619 + goto err; 1.620 + } 1.621 + 1.622 + p = buf; 1.623 + while (p < buf + size) { 1.624 + ELF_NHDR* notep = (ELF_NHDR*) p; 1.625 + char* descdata = p + sizeof(ELF_NHDR) + ROUNDUP(notep->n_namesz, 4); 1.626 + print_debug("Note header with n_type = %d and n_descsz = %u\n", 1.627 + notep->n_type, notep->n_descsz); 1.628 + 1.629 + if (notep->n_type == NT_PRSTATUS) { 1.630 + if (core_handle_prstatus(ph, descdata, notep->n_descsz) != true) 1.631 + return false; 1.632 + } 1.633 + p = descdata + ROUNDUP(notep->n_descsz, 4); 1.634 + } 1.635 + 1.636 + free(buf); 1.637 + return true; 1.638 + 1.639 +err: 1.640 + if (buf) free(buf); 1.641 + return false; 1.642 +} 1.643 + 1.644 +// read all segments from core file 1.645 +static bool read_core_segments(struct ps_prochandle* ph, ELF_EHDR* core_ehdr) { 1.646 + int i = 0; 1.647 + ELF_PHDR* phbuf = NULL; 1.648 + ELF_PHDR* core_php = NULL; 1.649 + 1.650 + if ((phbuf = read_program_header_table(ph->core->core_fd, core_ehdr)) == NULL) 1.651 + return false; 1.652 + 1.653 + /* 1.654 + * Now iterate through the program headers in the core file. 1.655 + * We're interested in two types of Phdrs: PT_NOTE (which 1.656 + * contains a set of saved /proc structures), and PT_LOAD (which 1.657 + * represents a memory mapping from the process's address space). 1.658 + * 1.659 + * Difference b/w Solaris PT_NOTE and Linux PT_NOTE: 1.660 + * 1.661 + * In Solaris there are two PT_NOTE segments the first PT_NOTE (if present) 1.662 + * contains /proc structs in the pre-2.6 unstructured /proc format. the last 1.663 + * PT_NOTE has data in new /proc format. 1.664 + * 1.665 + * In Solaris, there is only one pstatus (process status). pstatus contains 1.666 + * integer register set among other stuff. For each LWP, we have one lwpstatus 1.667 + * entry that has integer regset for that LWP. 1.668 + * 1.669 + * Linux threads are actually 'clone'd processes. To support core analysis 1.670 + * of "multithreaded" process, Linux creates more than one pstatus (called 1.671 + * "prstatus") entry in PT_NOTE. Each prstatus entry has integer regset for one 1.672 + * "thread". Please refer to Linux kernel src file 'fs/binfmt_elf.c', in particular 1.673 + * function "elf_core_dump". 1.674 + */ 1.675 + 1.676 + for (core_php = phbuf, i = 0; i < core_ehdr->e_phnum; i++) { 1.677 + switch (core_php->p_type) { 1.678 + case PT_NOTE: 1.679 + if (core_handle_note(ph, core_php) != true) goto err; 1.680 + break; 1.681 + 1.682 + case PT_LOAD: { 1.683 + if (core_php->p_filesz != 0) { 1.684 + if (add_map_info(ph, ph->core->core_fd, core_php->p_offset, 1.685 + core_php->p_vaddr, core_php->p_filesz) == NULL) goto err; 1.686 + } 1.687 + break; 1.688 + } 1.689 + } 1.690 + 1.691 + core_php++; 1.692 + } 1.693 + 1.694 + free(phbuf); 1.695 + return true; 1.696 +err: 1.697 + free(phbuf); 1.698 + return false; 1.699 +} 1.700 + 1.701 +// read segments of a shared object 1.702 +static bool read_lib_segments(struct ps_prochandle* ph, int lib_fd, ELF_EHDR* lib_ehdr, uintptr_t lib_base) { 1.703 + int i = 0; 1.704 + ELF_PHDR* phbuf; 1.705 + ELF_PHDR* lib_php = NULL; 1.706 + 1.707 + if ((phbuf = read_program_header_table(lib_fd, lib_ehdr)) == NULL) 1.708 + return false; 1.709 + 1.710 + // we want to process only PT_LOAD segments that are not writable. 1.711 + // i.e., text segments. The read/write/exec (data) segments would 1.712 + // have been already added from core file segments. 1.713 + for (lib_php = phbuf, i = 0; i < lib_ehdr->e_phnum; i++) { 1.714 + if ((lib_php->p_type == PT_LOAD) && !(lib_php->p_flags & PF_W) && (lib_php->p_filesz != 0)) { 1.715 + if (add_map_info(ph, lib_fd, lib_php->p_offset, lib_php->p_vaddr + lib_base, lib_php->p_filesz) == NULL) 1.716 + goto err; 1.717 + } 1.718 + lib_php++; 1.719 + } 1.720 + 1.721 + free(phbuf); 1.722 + return true; 1.723 +err: 1.724 + free(phbuf); 1.725 + return false; 1.726 +} 1.727 + 1.728 +// process segments from interpreter (ld.so or ld-linux.so) 1.729 +static bool read_interp_segments(struct ps_prochandle* ph) { 1.730 + ELF_EHDR interp_ehdr; 1.731 + 1.732 + if (read_elf_header(ph->core->interp_fd, &interp_ehdr) != true) { 1.733 + print_debug("interpreter is not a valid ELF file\n"); 1.734 + return false; 1.735 + } 1.736 + 1.737 + if (read_lib_segments(ph, ph->core->interp_fd, &interp_ehdr, ph->core->ld_base_addr) != true) { 1.738 + print_debug("can't read segments of interpreter\n"); 1.739 + return false; 1.740 + } 1.741 + 1.742 + return true; 1.743 +} 1.744 + 1.745 +// process segments of a a.out 1.746 +static bool read_exec_segments(struct ps_prochandle* ph, ELF_EHDR* exec_ehdr) { 1.747 + int i = 0; 1.748 + ELF_PHDR* phbuf = NULL; 1.749 + ELF_PHDR* exec_php = NULL; 1.750 + 1.751 + if ((phbuf = read_program_header_table(ph->core->exec_fd, exec_ehdr)) == NULL) 1.752 + return false; 1.753 + 1.754 + for (exec_php = phbuf, i = 0; i < exec_ehdr->e_phnum; i++) { 1.755 + switch (exec_php->p_type) { 1.756 + 1.757 + // add mappings for PT_LOAD segments 1.758 + case PT_LOAD: { 1.759 + // add only non-writable segments of non-zero filesz 1.760 + if (!(exec_php->p_flags & PF_W) && exec_php->p_filesz != 0) { 1.761 + if (add_map_info(ph, ph->core->exec_fd, exec_php->p_offset, exec_php->p_vaddr, exec_php->p_filesz) == NULL) goto err; 1.762 + } 1.763 + break; 1.764 + } 1.765 + 1.766 + // read the interpreter and it's segments 1.767 + case PT_INTERP: { 1.768 + char interp_name[BUF_SIZE]; 1.769 + 1.770 + pread(ph->core->exec_fd, interp_name, MIN(exec_php->p_filesz, BUF_SIZE), exec_php->p_offset); 1.771 + print_debug("ELF interpreter %s\n", interp_name); 1.772 + // read interpreter segments as well 1.773 + if ((ph->core->interp_fd = pathmap_open(interp_name)) < 0) { 1.774 + print_debug("can't open runtime loader\n"); 1.775 + goto err; 1.776 + } 1.777 + break; 1.778 + } 1.779 + 1.780 + // from PT_DYNAMIC we want to read address of first link_map addr 1.781 + case PT_DYNAMIC: { 1.782 + ph->core->dynamic_addr = exec_php->p_vaddr; 1.783 + print_debug("address of _DYNAMIC is 0x%lx\n", ph->core->dynamic_addr); 1.784 + break; 1.785 + } 1.786 + 1.787 + } // switch 1.788 + exec_php++; 1.789 + } // for 1.790 + 1.791 + free(phbuf); 1.792 + return true; 1.793 +err: 1.794 + free(phbuf); 1.795 + return false; 1.796 +} 1.797 + 1.798 + 1.799 +#define FIRST_LINK_MAP_OFFSET offsetof(struct r_debug, r_map) 1.800 +#define LD_BASE_OFFSET offsetof(struct r_debug, r_ldbase) 1.801 +#define LINK_MAP_ADDR_OFFSET offsetof(struct link_map, l_addr) 1.802 +#define LINK_MAP_NAME_OFFSET offsetof(struct link_map, l_name) 1.803 +#define LINK_MAP_NEXT_OFFSET offsetof(struct link_map, l_next) 1.804 + 1.805 +// read shared library info from runtime linker's data structures. 1.806 +// This work is done by librtlb_db in Solaris 1.807 +static bool read_shared_lib_info(struct ps_prochandle* ph) { 1.808 + uintptr_t addr = ph->core->dynamic_addr; 1.809 + uintptr_t debug_base; 1.810 + uintptr_t first_link_map_addr; 1.811 + uintptr_t ld_base_addr; 1.812 + uintptr_t link_map_addr; 1.813 + uintptr_t lib_base_diff; 1.814 + uintptr_t lib_base; 1.815 + uintptr_t lib_name_addr; 1.816 + char lib_name[BUF_SIZE]; 1.817 + ELF_DYN dyn; 1.818 + ELF_EHDR elf_ehdr; 1.819 + int lib_fd; 1.820 + 1.821 + // _DYNAMIC has information of the form 1.822 + // [tag] [data] [tag] [data] ..... 1.823 + // Both tag and data are pointer sized. 1.824 + // We look for dynamic info with DT_DEBUG. This has shared object info. 1.825 + // refer to struct r_debug in link.h 1.826 + 1.827 + dyn.d_tag = DT_NULL; 1.828 + while (dyn.d_tag != DT_DEBUG) { 1.829 + if (ps_pdread(ph, (psaddr_t) addr, &dyn, sizeof(ELF_DYN)) != PS_OK) { 1.830 + print_debug("can't read debug info from _DYNAMIC\n"); 1.831 + return false; 1.832 + } 1.833 + addr += sizeof(ELF_DYN); 1.834 + } 1.835 + 1.836 + // we have got Dyn entry with DT_DEBUG 1.837 + debug_base = dyn.d_un.d_ptr; 1.838 + // at debug_base we have struct r_debug. This has first link map in r_map field 1.839 + if (ps_pdread(ph, (psaddr_t) debug_base + FIRST_LINK_MAP_OFFSET, 1.840 + &first_link_map_addr, sizeof(uintptr_t)) != PS_OK) { 1.841 + print_debug("can't read first link map address\n"); 1.842 + return false; 1.843 + } 1.844 + 1.845 + // read ld_base address from struct r_debug 1.846 + if (ps_pdread(ph, (psaddr_t) debug_base + LD_BASE_OFFSET, &ld_base_addr, 1.847 + sizeof(uintptr_t)) != PS_OK) { 1.848 + print_debug("can't read ld base address\n"); 1.849 + return false; 1.850 + } 1.851 + ph->core->ld_base_addr = ld_base_addr; 1.852 + 1.853 + print_debug("interpreter base address is 0x%lx\n", ld_base_addr); 1.854 + 1.855 + // now read segments from interp (i.e ld.so or ld-linux.so) 1.856 + if (read_interp_segments(ph) != true) 1.857 + return false; 1.858 + 1.859 + // after adding interpreter (ld.so) mappings sort again 1.860 + if (sort_map_array(ph) != true) 1.861 + return false; 1.862 + 1.863 + print_debug("first link map is at 0x%lx\n", first_link_map_addr); 1.864 + 1.865 + link_map_addr = first_link_map_addr; 1.866 + while (link_map_addr != 0) { 1.867 + // read library base address of the .so. Note that even though <sys/link.h> calls 1.868 + // link_map->l_addr as "base address", this is * not * really base virtual 1.869 + // address of the shared object. This is actually the difference b/w the virtual 1.870 + // address mentioned in shared object and the actual virtual base where runtime 1.871 + // linker loaded it. We use "base diff" in read_lib_segments call below. 1.872 + 1.873 + if (ps_pdread(ph, (psaddr_t) link_map_addr + LINK_MAP_ADDR_OFFSET, 1.874 + &lib_base_diff, sizeof(uintptr_t)) != PS_OK) { 1.875 + print_debug("can't read shared object base address diff\n"); 1.876 + return false; 1.877 + } 1.878 + 1.879 + // read address of the name 1.880 + if (ps_pdread(ph, (psaddr_t) link_map_addr + LINK_MAP_NAME_OFFSET, 1.881 + &lib_name_addr, sizeof(uintptr_t)) != PS_OK) { 1.882 + print_debug("can't read address of shared object name\n"); 1.883 + return false; 1.884 + } 1.885 + 1.886 + // read name of the shared object 1.887 + if (read_string(ph, (uintptr_t) lib_name_addr, lib_name, sizeof(lib_name)) != true) { 1.888 + print_debug("can't read shared object name\n"); 1.889 + return false; 1.890 + } 1.891 + 1.892 + if (lib_name[0] != '\0') { 1.893 + // ignore empty lib names 1.894 + lib_fd = pathmap_open(lib_name); 1.895 + 1.896 + if (lib_fd < 0) { 1.897 + print_debug("can't open shared object %s\n", lib_name); 1.898 + // continue with other libraries... 1.899 + } else { 1.900 + if (read_elf_header(lib_fd, &elf_ehdr)) { 1.901 + lib_base = lib_base_diff + find_base_address(lib_fd, &elf_ehdr); 1.902 + print_debug("reading library %s @ 0x%lx [ 0x%lx ]\n", 1.903 + lib_name, lib_base, lib_base_diff); 1.904 + // while adding library mappings we need to use "base difference". 1.905 + if (! read_lib_segments(ph, lib_fd, &elf_ehdr, lib_base_diff)) { 1.906 + print_debug("can't read shared object's segments\n"); 1.907 + close(lib_fd); 1.908 + return false; 1.909 + } 1.910 + add_lib_info_fd(ph, lib_name, lib_fd, lib_base); 1.911 + // Map info is added for the library (lib_name) so 1.912 + // we need to re-sort it before calling the p_pdread. 1.913 + if (sort_map_array(ph) != true) 1.914 + return false; 1.915 + } else { 1.916 + print_debug("can't read ELF header for shared object %s\n", lib_name); 1.917 + close(lib_fd); 1.918 + // continue with other libraries... 1.919 + } 1.920 + } 1.921 + } 1.922 + 1.923 + // read next link_map address 1.924 + if (ps_pdread(ph, (psaddr_t) link_map_addr + LINK_MAP_NEXT_OFFSET, 1.925 + &link_map_addr, sizeof(uintptr_t)) != PS_OK) { 1.926 + print_debug("can't read next link in link_map\n"); 1.927 + return false; 1.928 + } 1.929 + } 1.930 + 1.931 + return true; 1.932 +} 1.933 + 1.934 +// the one and only one exposed stuff from this file 1.935 +struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) { 1.936 + ELF_EHDR core_ehdr; 1.937 + ELF_EHDR exec_ehdr; 1.938 + ELF_EHDR lib_ehdr; 1.939 + 1.940 + struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle)); 1.941 + if (ph == NULL) { 1.942 + print_debug("can't allocate ps_prochandle\n"); 1.943 + return NULL; 1.944 + } 1.945 + 1.946 + if ((ph->core = (struct core_data*) calloc(1, sizeof(struct core_data))) == NULL) { 1.947 + free(ph); 1.948 + print_debug("can't allocate ps_prochandle\n"); 1.949 + return NULL; 1.950 + } 1.951 + 1.952 + // initialize ph 1.953 + ph->ops = &core_ops; 1.954 + ph->core->core_fd = -1; 1.955 + ph->core->exec_fd = -1; 1.956 + ph->core->interp_fd = -1; 1.957 + 1.958 + // open the core file 1.959 + if ((ph->core->core_fd = open(core_file, O_RDONLY)) < 0) { 1.960 + print_debug("can't open core file\n"); 1.961 + goto err; 1.962 + } 1.963 + 1.964 + // read core file ELF header 1.965 + if (read_elf_header(ph->core->core_fd, &core_ehdr) != true || core_ehdr.e_type != ET_CORE) { 1.966 + print_debug("core file is not a valid ELF ET_CORE file\n"); 1.967 + goto err; 1.968 + } 1.969 + 1.970 + if ((ph->core->exec_fd = open(exec_file, O_RDONLY)) < 0) { 1.971 + print_debug("can't open executable file\n"); 1.972 + goto err; 1.973 + } 1.974 + 1.975 + if (read_elf_header(ph->core->exec_fd, &exec_ehdr) != true || exec_ehdr.e_type != ET_EXEC) { 1.976 + print_debug("executable file is not a valid ELF ET_EXEC file\n"); 1.977 + goto err; 1.978 + } 1.979 + 1.980 + // process core file segments 1.981 + if (read_core_segments(ph, &core_ehdr) != true) 1.982 + goto err; 1.983 + 1.984 + // process exec file segments 1.985 + if (read_exec_segments(ph, &exec_ehdr) != true) 1.986 + goto err; 1.987 + 1.988 + // exec file is also treated like a shared object for symbol search 1.989 + if (add_lib_info_fd(ph, exec_file, ph->core->exec_fd, 1.990 + (uintptr_t)0 + find_base_address(ph->core->exec_fd, &exec_ehdr)) == NULL) 1.991 + goto err; 1.992 + 1.993 + // allocate and sort maps into map_array, we need to do this 1.994 + // here because read_shared_lib_info needs to read from debuggee 1.995 + // address space 1.996 + if (sort_map_array(ph) != true) 1.997 + goto err; 1.998 + 1.999 + if (read_shared_lib_info(ph) != true) 1.1000 + goto err; 1.1001 + 1.1002 + // sort again because we have added more mappings from shared objects 1.1003 + if (sort_map_array(ph) != true) 1.1004 + goto err; 1.1005 + 1.1006 + if (init_classsharing_workaround(ph) != true) 1.1007 + goto err; 1.1008 + 1.1009 + return ph; 1.1010 + 1.1011 +err: 1.1012 + Prelease(ph); 1.1013 + return NULL; 1.1014 +}