duke@435: /* dsamersoff@5758: * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: duke@435: #include duke@435: #include duke@435: #include duke@435: #include duke@435: #include duke@435: #include duke@435: #include duke@435: #include duke@435: #include "libproc_impl.h" duke@435: #include "salibelf.h" duke@435: duke@435: // This file has the libproc implementation to read core files. duke@435: // For live processes, refer to ps_proc.c. Portions of this is adapted duke@435: // /modelled after Solaris libproc.so (in particular Pcore.c) duke@435: duke@435: //---------------------------------------------------------------------- duke@435: // ps_prochandle cleanup helper functions duke@435: duke@435: // close all file descriptors dsamersoff@5832: static void close_files(struct ps_prochandle* ph) { dsamersoff@5832: lib_info* lib = NULL; duke@435: dsamersoff@5832: // close core file descriptor dsamersoff@5832: if (ph->core->core_fd >= 0) dsamersoff@5832: close(ph->core->core_fd); duke@435: dsamersoff@5832: // close exec file descriptor dsamersoff@5832: if (ph->core->exec_fd >= 0) dsamersoff@5832: close(ph->core->exec_fd); duke@435: dsamersoff@5832: // close interp file descriptor dsamersoff@5832: if (ph->core->interp_fd >= 0) dsamersoff@5832: close(ph->core->interp_fd); duke@435: dsamersoff@5832: // close class share archive file dsamersoff@5832: if (ph->core->classes_jsa_fd >= 0) dsamersoff@5832: close(ph->core->classes_jsa_fd); duke@435: dsamersoff@5832: // close all library file descriptors dsamersoff@5832: lib = ph->libs; dsamersoff@5832: while (lib) { dsamersoff@5832: int fd = lib->fd; dsamersoff@5832: if (fd >= 0 && fd != ph->core->exec_fd) { dsamersoff@5832: close(fd); dsamersoff@5832: } dsamersoff@5832: lib = lib->next; dsamersoff@5832: } duke@435: } duke@435: duke@435: // clean all map_info stuff duke@435: static void destroy_map_info(struct ps_prochandle* ph) { duke@435: map_info* map = ph->core->maps; duke@435: while (map) { dsamersoff@5832: map_info* next = map->next; dsamersoff@5832: free(map); dsamersoff@5832: map = next; duke@435: } duke@435: duke@435: if (ph->core->map_array) { dsamersoff@5832: free(ph->core->map_array); duke@435: } duke@435: duke@435: // Part of the class sharing workaround duke@435: map = ph->core->class_share_maps; duke@435: while (map) { dsamersoff@5832: map_info* next = map->next; dsamersoff@5832: free(map); dsamersoff@5832: map = next; duke@435: } duke@435: } duke@435: duke@435: // ps_prochandle operations duke@435: static void core_release(struct ps_prochandle* ph) { dsamersoff@5832: if (ph->core) { dsamersoff@5832: close_files(ph); dsamersoff@5832: destroy_map_info(ph); dsamersoff@5832: free(ph->core); dsamersoff@5832: } duke@435: } duke@435: duke@435: static map_info* allocate_init_map(int fd, off_t offset, uintptr_t vaddr, size_t memsz) { dsamersoff@5832: map_info* map; dsamersoff@5832: if ( (map = (map_info*) calloc(1, sizeof(map_info))) == NULL) { dsamersoff@5832: print_debug("can't allocate memory for map_info\n"); dsamersoff@5832: return NULL; dsamersoff@5832: } duke@435: dsamersoff@5832: // initialize map dsamersoff@5832: map->fd = fd; dsamersoff@5832: map->offset = offset; dsamersoff@5832: map->vaddr = vaddr; dsamersoff@5832: map->memsz = memsz; dsamersoff@5832: return map; duke@435: } duke@435: duke@435: // add map info with given fd, offset, vaddr and memsz duke@435: static map_info* add_map_info(struct ps_prochandle* ph, int fd, off_t offset, duke@435: uintptr_t vaddr, size_t memsz) { dsamersoff@5832: map_info* map; dsamersoff@5832: if ((map = allocate_init_map(fd, offset, vaddr, memsz)) == NULL) { dsamersoff@5832: return NULL; dsamersoff@5832: } duke@435: dsamersoff@5832: // add this to map list dsamersoff@5832: map->next = ph->core->maps; dsamersoff@5832: ph->core->maps = map; dsamersoff@5832: ph->core->num_maps++; duke@435: dsamersoff@5832: return map; duke@435: } duke@435: duke@435: // Part of the class sharing workaround dsamersoff@5832: static map_info* add_class_share_map_info(struct ps_prochandle* ph, off_t offset, duke@435: uintptr_t vaddr, size_t memsz) { dsamersoff@5832: map_info* map; dsamersoff@5832: if ((map = allocate_init_map(ph->core->classes_jsa_fd, dsamersoff@5832: offset, vaddr, memsz)) == NULL) { dsamersoff@5832: return NULL; dsamersoff@5832: } duke@435: dsamersoff@5832: map->next = ph->core->class_share_maps; dsamersoff@5832: ph->core->class_share_maps = map; dsamersoff@5832: return map; duke@435: } duke@435: duke@435: // Return the map_info for the given virtual address. We keep a sorted duke@435: // array of pointers in ph->map_array, so we can binary search. dsamersoff@5832: static map_info* core_lookup(struct ps_prochandle *ph, uintptr_t addr) { dsamersoff@5832: int mid, lo = 0, hi = ph->core->num_maps - 1; dsamersoff@5832: map_info *mp; duke@435: dsamersoff@5832: while (hi - lo > 1) { dsamersoff@5832: mid = (lo + hi) / 2; dsamersoff@5832: if (addr >= ph->core->map_array[mid]->vaddr) { dsamersoff@5832: lo = mid; dsamersoff@5832: } else { dsamersoff@5832: hi = mid; dsamersoff@5832: } dsamersoff@5832: } duke@435: dsamersoff@5832: if (addr < ph->core->map_array[hi]->vaddr) { dsamersoff@5832: mp = ph->core->map_array[lo]; dsamersoff@5832: } else { dsamersoff@5832: mp = ph->core->map_array[hi]; dsamersoff@5832: } duke@435: dsamersoff@5832: if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) { dsamersoff@5832: return (mp); dsamersoff@5832: } dsamersoff@5832: dsamersoff@5832: dsamersoff@5832: // Part of the class sharing workaround dsamersoff@5832: // Unfortunately, we have no way of detecting -Xshare state. dsamersoff@5832: // Check out the share maps atlast, if we don't find anywhere. dsamersoff@5832: // This is done this way so to avoid reading share pages dsamersoff@5832: // ahead of other normal maps. For eg. with -Xshare:off we don't dsamersoff@5832: // want to prefer class sharing data to data from core. dsamersoff@5832: mp = ph->core->class_share_maps; dsamersoff@5832: if (mp) { dsamersoff@5832: print_debug("can't locate map_info at 0x%lx, trying class share maps\n", addr); dsamersoff@5832: } dsamersoff@5832: while (mp) { dsamersoff@5832: if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) { dsamersoff@5832: print_debug("located map_info at 0x%lx from class share maps\n", addr); duke@435: return (mp); dsamersoff@5832: } dsamersoff@5832: mp = mp->next; dsamersoff@5832: } duke@435: dsamersoff@5832: print_debug("can't locate map_info at 0x%lx\n", addr); dsamersoff@5832: return (NULL); duke@435: } duke@435: duke@435: //--------------------------------------------------------------- duke@435: // Part of the class sharing workaround: duke@435: // sla@5061: // With class sharing, pages are mapped from classes.jsa file. duke@435: // The read-only class sharing pages are mapped as MAP_SHARED, duke@435: // PROT_READ pages. These pages are not dumped into core dump. sla@5061: // With this workaround, these pages are read from classes.jsa. duke@435: duke@435: // FIXME: !HACK ALERT! duke@435: // The format of sharing achive file header is needed to read shared heap duke@435: // file mappings. For now, I am hard coding portion of FileMapHeader here. duke@435: // Refer to filemap.hpp. duke@435: duke@435: // FileMapHeader describes the shared space data in the file to be duke@435: // mapped. This structure gets written to a file. It is not a class, duke@435: // so that the compilers don't add any compiler-private data to it. duke@435: duke@435: #define NUM_SHARED_MAPS 4 duke@435: duke@435: // Refer to FileMapInfo::_current_version in filemap.hpp duke@435: #define CURRENT_ARCHIVE_VERSION 1 duke@435: duke@435: struct FileMapHeader { duke@435: int _magic; // identify file type. duke@435: int _version; // (from enum, above.) duke@435: size_t _alignment; // how shared archive should be aligned duke@435: duke@435: struct space_info { duke@435: int _file_offset; // sizeof(this) rounded to vm page size duke@435: char* _base; // copy-on-write base address duke@435: size_t _capacity; // for validity checking duke@435: size_t _used; // for setting space top on read duke@435: duke@435: // 4991491 NOTICE These are C++ bool's in filemap.hpp and must match up with dsamersoff@5832: // the C type matching the C++ bool type on any given platform. dsamersoff@5832: // We assume the corresponding C type is char but licensees dsamersoff@5832: // may need to adjust the type of these fields. duke@435: char _read_only; // read only space? duke@435: char _allow_exec; // executable code in space? duke@435: coleenp@4037: } _space[NUM_SHARED_MAPS]; duke@435: duke@435: // Ignore the rest of the FileMapHeader. We don't need those fields here. duke@435: }; duke@435: swamyv@964: static bool read_jboolean(struct ps_prochandle* ph, uintptr_t addr, jboolean* pvalue) { dsamersoff@5832: jboolean i; dsamersoff@5832: if (ps_pdread(ph, (psaddr_t) addr, &i, sizeof(i)) == PS_OK) { dsamersoff@5832: *pvalue = i; dsamersoff@5832: return true; dsamersoff@5832: } else { dsamersoff@5832: return false; dsamersoff@5832: } duke@435: } duke@435: duke@435: static bool read_pointer(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* pvalue) { dsamersoff@5832: uintptr_t uip; dsamersoff@5832: if (ps_pdread(ph, (psaddr_t) addr, (char *)&uip, sizeof(uip)) == PS_OK) { dsamersoff@5832: *pvalue = uip; dsamersoff@5832: return true; dsamersoff@5832: } else { dsamersoff@5832: return false; dsamersoff@5832: } duke@435: } duke@435: duke@435: // used to read strings from debuggee duke@435: static bool read_string(struct ps_prochandle* ph, uintptr_t addr, char* buf, size_t size) { dsamersoff@5832: size_t i = 0; dsamersoff@5832: char c = ' '; duke@435: dsamersoff@5832: while (c != '\0') { dsamersoff@5832: if (ps_pdread(ph, (psaddr_t) addr, &c, sizeof(char)) != PS_OK) { dsamersoff@5832: return false; dsamersoff@5832: } dsamersoff@5832: if (i < size - 1) { dsamersoff@5832: buf[i] = c; dsamersoff@5832: } else { dsamersoff@5832: // smaller buffer dsamersoff@5832: return false; dsamersoff@5832: } dsamersoff@5832: i++; addr++; dsamersoff@5832: } duke@435: dsamersoff@5832: buf[i] = '\0'; dsamersoff@5832: return true; duke@435: } duke@435: duke@435: #define USE_SHARED_SPACES_SYM "UseSharedSpaces" duke@435: // mangled name of Arguments::SharedArchivePath duke@435: #define SHARED_ARCHIVE_PATH_SYM "_ZN9Arguments17SharedArchivePathE" dsamersoff@5832: #define LIBJVM_NAME "/libjvm.so" duke@435: duke@435: static bool init_classsharing_workaround(struct ps_prochandle* ph) { dsamersoff@5832: lib_info* lib = ph->libs; dsamersoff@5832: while (lib != NULL) { dsamersoff@5832: // we are iterating over shared objects from the core dump. look for dsamersoff@5832: // libjvm.so. dsamersoff@5832: const char *jvm_name = 0; dsamersoff@5832: if ((jvm_name = strstr(lib->name, LIBJVM_NAME)) != 0) { dsamersoff@5832: char classes_jsa[PATH_MAX]; dsamersoff@5832: struct FileMapHeader header; dsamersoff@5832: int fd = -1; dsamersoff@5832: int m = 0; dsamersoff@5832: size_t n = 0; dsamersoff@5832: uintptr_t base = 0, useSharedSpacesAddr = 0; dsamersoff@5832: uintptr_t sharedArchivePathAddrAddr = 0, sharedArchivePathAddr = 0; dsamersoff@5832: jboolean useSharedSpaces = 0; dsamersoff@5832: map_info* mi = 0; duke@435: dsamersoff@5832: memset(classes_jsa, 0, sizeof(classes_jsa)); dsamersoff@5832: jvm_name = lib->name; dsamersoff@5832: useSharedSpacesAddr = lookup_symbol(ph, jvm_name, USE_SHARED_SPACES_SYM); dsamersoff@5832: if (useSharedSpacesAddr == 0) { dsamersoff@5832: print_debug("can't lookup 'UseSharedSpaces' flag\n"); dsamersoff@5832: return false; dsamersoff@5832: } duke@435: dsamersoff@5832: // Hotspot vm types are not exported to build this library. So dsamersoff@5832: // using equivalent type jboolean to read the value of dsamersoff@5832: // UseSharedSpaces which is same as hotspot type "bool". dsamersoff@5832: if (read_jboolean(ph, useSharedSpacesAddr, &useSharedSpaces) != true) { dsamersoff@5832: print_debug("can't read the value of 'UseSharedSpaces' flag\n"); dsamersoff@5832: return false; dsamersoff@5832: } duke@435: dsamersoff@5832: if ((int)useSharedSpaces == 0) { dsamersoff@5832: print_debug("UseSharedSpaces is false, assuming -Xshare:off!\n"); dsamersoff@5832: return true; dsamersoff@5832: } duke@435: dsamersoff@5832: sharedArchivePathAddrAddr = lookup_symbol(ph, jvm_name, SHARED_ARCHIVE_PATH_SYM); dsamersoff@5832: if (sharedArchivePathAddrAddr == 0) { dsamersoff@5832: print_debug("can't lookup shared archive path symbol\n"); dsamersoff@5832: return false; dsamersoff@5832: } duke@435: dsamersoff@5832: if (read_pointer(ph, sharedArchivePathAddrAddr, &sharedArchivePathAddr) != true) { dsamersoff@5832: print_debug("can't read shared archive path pointer\n"); dsamersoff@5832: return false; dsamersoff@5832: } duke@435: dsamersoff@5832: if (read_string(ph, sharedArchivePathAddr, classes_jsa, sizeof(classes_jsa)) != true) { dsamersoff@5832: print_debug("can't read shared archive path value\n"); dsamersoff@5832: return false; dsamersoff@5832: } duke@435: dsamersoff@5832: print_debug("looking for %s\n", classes_jsa); dsamersoff@5832: // open the class sharing archive file dsamersoff@5832: fd = pathmap_open(classes_jsa); dsamersoff@5832: if (fd < 0) { dsamersoff@5832: print_debug("can't open %s!\n", classes_jsa); dsamersoff@5832: ph->core->classes_jsa_fd = -1; dsamersoff@5832: return false; dsamersoff@5832: } else { dsamersoff@5832: print_debug("opened %s\n", classes_jsa); dsamersoff@5832: } duke@435: dsamersoff@5832: // read FileMapHeader from the file dsamersoff@5832: memset(&header, 0, sizeof(struct FileMapHeader)); dsamersoff@5832: if ((n = read(fd, &header, sizeof(struct FileMapHeader))) dsamersoff@5832: != sizeof(struct FileMapHeader)) { dsamersoff@5832: print_debug("can't read shared archive file map header from %s\n", classes_jsa); dsamersoff@5832: close(fd); dsamersoff@5832: return false; dsamersoff@5832: } duke@435: dsamersoff@5832: // check file magic dsamersoff@5832: if (header._magic != 0xf00baba2) { dsamersoff@5832: print_debug("%s has bad shared archive file magic number 0x%x, expecing 0xf00baba2\n", dsamersoff@5832: classes_jsa, header._magic); dsamersoff@5832: close(fd); dsamersoff@5832: return false; dsamersoff@5832: } duke@435: dsamersoff@5832: // check version dsamersoff@5832: if (header._version != CURRENT_ARCHIVE_VERSION) { dsamersoff@5832: print_debug("%s has wrong shared archive file version %d, expecting %d\n", dsamersoff@5832: classes_jsa, header._version, CURRENT_ARCHIVE_VERSION); dsamersoff@5832: close(fd); dsamersoff@5832: return false; dsamersoff@5832: } duke@435: dsamersoff@5832: ph->core->classes_jsa_fd = fd; dsamersoff@5832: // add read-only maps from classes.jsa to the list of maps dsamersoff@5832: for (m = 0; m < NUM_SHARED_MAPS; m++) { dsamersoff@5832: if (header._space[m]._read_only) { dsamersoff@5832: base = (uintptr_t) header._space[m]._base; dsamersoff@5832: // no need to worry about the fractional pages at-the-end. dsamersoff@5832: // possible fractional pages are handled by core_read_data. dsamersoff@5832: add_class_share_map_info(ph, (off_t) header._space[m]._file_offset, dsamersoff@5832: base, (size_t) header._space[m]._used); dsamersoff@5832: print_debug("added a share archive map at 0x%lx\n", base); dsamersoff@5832: } duke@435: } dsamersoff@5832: return true; duke@435: } dsamersoff@5832: lib = lib->next; dsamersoff@5832: } dsamersoff@5832: return true; duke@435: } duke@435: duke@435: duke@435: //--------------------------------------------------------------------------- duke@435: // functions to handle map_info duke@435: duke@435: // Order mappings based on virtual address. We use this function as the duke@435: // callback for sorting the array of map_info pointers. duke@435: static int core_cmp_mapping(const void *lhsp, const void *rhsp) duke@435: { dsamersoff@5832: const map_info *lhs = *((const map_info **)lhsp); dsamersoff@5832: const map_info *rhs = *((const map_info **)rhsp); duke@435: dsamersoff@5832: if (lhs->vaddr == rhs->vaddr) { dsamersoff@5832: return (0); dsamersoff@5832: } duke@435: dsamersoff@5832: return (lhs->vaddr < rhs->vaddr ? -1 : 1); duke@435: } duke@435: duke@435: // we sort map_info by starting virtual address so that we can do duke@435: // binary search to read from an address. duke@435: static bool sort_map_array(struct ps_prochandle* ph) { dsamersoff@5832: size_t num_maps = ph->core->num_maps; dsamersoff@5832: map_info* map = ph->core->maps; dsamersoff@5832: int i = 0; duke@435: dsamersoff@5832: // allocate map_array dsamersoff@5832: map_info** array; dsamersoff@5832: if ( (array = (map_info**) malloc(sizeof(map_info*) * num_maps)) == NULL) { dsamersoff@5832: print_debug("can't allocate memory for map array\n"); dsamersoff@5832: return false; dsamersoff@5832: } duke@435: dsamersoff@5832: // add maps to array dsamersoff@5832: while (map) { dsamersoff@5832: array[i] = map; dsamersoff@5832: i++; dsamersoff@5832: map = map->next; dsamersoff@5832: } duke@435: dsamersoff@5832: // sort is called twice. If this is second time, clear map array dsamersoff@5832: if (ph->core->map_array) { dsamersoff@5832: free(ph->core->map_array); dsamersoff@5832: } duke@435: dsamersoff@5832: ph->core->map_array = array; dsamersoff@5832: // sort the map_info array by base virtual address. dsamersoff@5832: qsort(ph->core->map_array, ph->core->num_maps, sizeof (map_info*), dsamersoff@5832: core_cmp_mapping); duke@435: dsamersoff@5832: // print map dsamersoff@5832: if (is_debug()) { dsamersoff@5832: int j = 0; dsamersoff@5832: print_debug("---- sorted virtual address map ----\n"); dsamersoff@5832: for (j = 0; j < ph->core->num_maps; j++) { dsamersoff@5832: print_debug("base = 0x%lx\tsize = %zu\n", ph->core->map_array[j]->vaddr, dsamersoff@5832: ph->core->map_array[j]->memsz); dsamersoff@5832: } dsamersoff@5832: } dsamersoff@5832: dsamersoff@5832: return true; duke@435: } duke@435: duke@435: #ifndef MIN duke@435: #define MIN(x, y) (((x) < (y))? (x): (y)) duke@435: #endif duke@435: duke@435: static bool core_read_data(struct ps_prochandle* ph, uintptr_t addr, char *buf, size_t size) { duke@435: ssize_t resid = size; duke@435: int page_size=sysconf(_SC_PAGE_SIZE); duke@435: while (resid != 0) { duke@435: map_info *mp = core_lookup(ph, addr); duke@435: uintptr_t mapoff; duke@435: ssize_t len, rem; duke@435: off_t off; duke@435: int fd; duke@435: dsamersoff@5832: if (mp == NULL) { duke@435: break; /* No mapping for this address */ dsamersoff@5832: } duke@435: duke@435: fd = mp->fd; duke@435: mapoff = addr - mp->vaddr; duke@435: len = MIN(resid, mp->memsz - mapoff); duke@435: off = mp->offset + mapoff; duke@435: dsamersoff@5832: if ((len = pread(fd, buf, len, off)) <= 0) { duke@435: break; dsamersoff@5832: } duke@435: duke@435: resid -= len; duke@435: addr += len; duke@435: buf = (char *)buf + len; duke@435: duke@435: // mappings always start at page boundary. But, may end in fractional duke@435: // page. fill zeros for possible fractional page at the end of a mapping. duke@435: rem = mp->memsz % page_size; duke@435: if (rem > 0) { duke@435: rem = page_size - rem; duke@435: len = MIN(resid, rem); duke@435: resid -= len; duke@435: addr += len; duke@435: // we are not assuming 'buf' to be zero initialized. duke@435: memset(buf, 0, len); duke@435: buf += len; duke@435: } duke@435: } duke@435: duke@435: if (resid) { duke@435: print_debug("core read failed for %d byte(s) @ 0x%lx (%d more bytes)\n", duke@435: size, addr, resid); duke@435: return false; duke@435: } else { duke@435: return true; duke@435: } duke@435: } duke@435: duke@435: // null implementation for write duke@435: static bool core_write_data(struct ps_prochandle* ph, duke@435: uintptr_t addr, const char *buf , size_t size) { duke@435: return false; duke@435: } duke@435: duke@435: static bool core_get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id, duke@435: struct user_regs_struct* regs) { duke@435: // for core we have cached the lwp regs from NOTE section duke@435: thread_info* thr = ph->threads; duke@435: while (thr) { duke@435: if (thr->lwp_id == lwp_id) { duke@435: memcpy(regs, &thr->regs, sizeof(struct user_regs_struct)); duke@435: return true; duke@435: } duke@435: thr = thr->next; duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: static ps_prochandle_ops core_ops = { dcubed@485: .release= core_release, dcubed@485: .p_pread= core_read_data, dcubed@485: .p_pwrite= core_write_data, dcubed@485: .get_lwp_regs= core_get_lwp_regs duke@435: }; duke@435: duke@435: // read regs and create thread from NT_PRSTATUS entries from core file duke@435: static bool core_handle_prstatus(struct ps_prochandle* ph, const char* buf, size_t nbytes) { duke@435: // we have to read prstatus_t from buf duke@435: // assert(nbytes == sizeof(prstaus_t), "size mismatch on prstatus_t"); duke@435: prstatus_t* prstat = (prstatus_t*) buf; duke@435: thread_info* newthr; duke@435: print_debug("got integer regset for lwp %d\n", prstat->pr_pid); duke@435: // we set pthread_t to -1 for core dump duke@435: if((newthr = add_thread_info(ph, (pthread_t) -1, prstat->pr_pid)) == NULL) duke@435: return false; duke@435: duke@435: // copy regs duke@435: memcpy(&newthr->regs, prstat->pr_reg, sizeof(struct user_regs_struct)); duke@435: duke@435: if (is_debug()) { duke@435: print_debug("integer regset\n"); duke@435: #ifdef i386 duke@435: // print the regset duke@435: print_debug("\teax = 0x%x\n", newthr->regs.eax); duke@435: print_debug("\tebx = 0x%x\n", newthr->regs.ebx); duke@435: print_debug("\tecx = 0x%x\n", newthr->regs.ecx); duke@435: print_debug("\tedx = 0x%x\n", newthr->regs.edx); duke@435: print_debug("\tesp = 0x%x\n", newthr->regs.esp); duke@435: print_debug("\tebp = 0x%x\n", newthr->regs.ebp); duke@435: print_debug("\tesi = 0x%x\n", newthr->regs.esi); duke@435: print_debug("\tedi = 0x%x\n", newthr->regs.edi); duke@435: print_debug("\teip = 0x%x\n", newthr->regs.eip); duke@435: #endif duke@435: duke@435: #if defined(amd64) || defined(x86_64) duke@435: // print the regset duke@435: print_debug("\tr15 = 0x%lx\n", newthr->regs.r15); duke@435: print_debug("\tr14 = 0x%lx\n", newthr->regs.r14); duke@435: print_debug("\tr13 = 0x%lx\n", newthr->regs.r13); duke@435: print_debug("\tr12 = 0x%lx\n", newthr->regs.r12); duke@435: print_debug("\trbp = 0x%lx\n", newthr->regs.rbp); duke@435: print_debug("\trbx = 0x%lx\n", newthr->regs.rbx); duke@435: print_debug("\tr11 = 0x%lx\n", newthr->regs.r11); duke@435: print_debug("\tr10 = 0x%lx\n", newthr->regs.r10); duke@435: print_debug("\tr9 = 0x%lx\n", newthr->regs.r9); duke@435: print_debug("\tr8 = 0x%lx\n", newthr->regs.r8); duke@435: print_debug("\trax = 0x%lx\n", newthr->regs.rax); duke@435: print_debug("\trcx = 0x%lx\n", newthr->regs.rcx); duke@435: print_debug("\trdx = 0x%lx\n", newthr->regs.rdx); duke@435: print_debug("\trsi = 0x%lx\n", newthr->regs.rsi); duke@435: print_debug("\trdi = 0x%lx\n", newthr->regs.rdi); duke@435: print_debug("\torig_rax = 0x%lx\n", newthr->regs.orig_rax); duke@435: print_debug("\trip = 0x%lx\n", newthr->regs.rip); duke@435: print_debug("\tcs = 0x%lx\n", newthr->regs.cs); duke@435: print_debug("\teflags = 0x%lx\n", newthr->regs.eflags); duke@435: print_debug("\trsp = 0x%lx\n", newthr->regs.rsp); duke@435: print_debug("\tss = 0x%lx\n", newthr->regs.ss); duke@435: print_debug("\tfs_base = 0x%lx\n", newthr->regs.fs_base); duke@435: print_debug("\tgs_base = 0x%lx\n", newthr->regs.gs_base); duke@435: print_debug("\tds = 0x%lx\n", newthr->regs.ds); duke@435: print_debug("\tes = 0x%lx\n", newthr->regs.es); duke@435: print_debug("\tfs = 0x%lx\n", newthr->regs.fs); duke@435: print_debug("\tgs = 0x%lx\n", newthr->regs.gs); duke@435: #endif duke@435: } duke@435: duke@435: return true; duke@435: } duke@435: duke@435: #define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y)) duke@435: duke@435: // read NT_PRSTATUS entries from core NOTE segment duke@435: static bool core_handle_note(struct ps_prochandle* ph, ELF_PHDR* note_phdr) { duke@435: char* buf = NULL; duke@435: char* p = NULL; duke@435: size_t size = note_phdr->p_filesz; duke@435: duke@435: // we are interested in just prstatus entries. we will ignore the rest. duke@435: // Advance the seek pointer to the start of the PT_NOTE data duke@435: if (lseek(ph->core->core_fd, note_phdr->p_offset, SEEK_SET) == (off_t)-1) { duke@435: print_debug("failed to lseek to PT_NOTE data\n"); duke@435: return false; duke@435: } duke@435: duke@435: // Now process the PT_NOTE structures. Each one is preceded by duke@435: // an Elf{32/64}_Nhdr structure describing its type and size. duke@435: if ( (buf = (char*) malloc(size)) == NULL) { duke@435: print_debug("can't allocate memory for reading core notes\n"); duke@435: goto err; duke@435: } duke@435: duke@435: // read notes into buffer duke@435: if (read(ph->core->core_fd, buf, size) != size) { duke@435: print_debug("failed to read notes, core file must have been truncated\n"); duke@435: goto err; duke@435: } duke@435: duke@435: p = buf; duke@435: while (p < buf + size) { duke@435: ELF_NHDR* notep = (ELF_NHDR*) p; duke@435: char* descdata = p + sizeof(ELF_NHDR) + ROUNDUP(notep->n_namesz, 4); duke@435: print_debug("Note header with n_type = %d and n_descsz = %u\n", duke@435: notep->n_type, notep->n_descsz); duke@435: duke@435: if (notep->n_type == NT_PRSTATUS) { dsamersoff@5832: if (core_handle_prstatus(ph, descdata, notep->n_descsz) != true) { dsamersoff@5832: return false; dsamersoff@5832: } duke@435: } duke@435: p = descdata + ROUNDUP(notep->n_descsz, 4); duke@435: } duke@435: duke@435: free(buf); duke@435: return true; duke@435: duke@435: err: duke@435: if (buf) free(buf); duke@435: return false; duke@435: } duke@435: duke@435: // read all segments from core file duke@435: static bool read_core_segments(struct ps_prochandle* ph, ELF_EHDR* core_ehdr) { duke@435: int i = 0; duke@435: ELF_PHDR* phbuf = NULL; duke@435: ELF_PHDR* core_php = NULL; duke@435: duke@435: if ((phbuf = read_program_header_table(ph->core->core_fd, core_ehdr)) == NULL) duke@435: return false; duke@435: duke@435: /* duke@435: * Now iterate through the program headers in the core file. duke@435: * We're interested in two types of Phdrs: PT_NOTE (which duke@435: * contains a set of saved /proc structures), and PT_LOAD (which duke@435: * represents a memory mapping from the process's address space). duke@435: * dsamersoff@5832: * Difference b/w Solaris PT_NOTE and Linux/BSD PT_NOTE: duke@435: * duke@435: * In Solaris there are two PT_NOTE segments the first PT_NOTE (if present) duke@435: * contains /proc structs in the pre-2.6 unstructured /proc format. the last duke@435: * PT_NOTE has data in new /proc format. duke@435: * duke@435: * In Solaris, there is only one pstatus (process status). pstatus contains duke@435: * integer register set among other stuff. For each LWP, we have one lwpstatus duke@435: * entry that has integer regset for that LWP. duke@435: * duke@435: * Linux threads are actually 'clone'd processes. To support core analysis duke@435: * of "multithreaded" process, Linux creates more than one pstatus (called duke@435: * "prstatus") entry in PT_NOTE. Each prstatus entry has integer regset for one duke@435: * "thread". Please refer to Linux kernel src file 'fs/binfmt_elf.c', in particular duke@435: * function "elf_core_dump". duke@435: */ duke@435: duke@435: for (core_php = phbuf, i = 0; i < core_ehdr->e_phnum; i++) { duke@435: switch (core_php->p_type) { duke@435: case PT_NOTE: dsamersoff@5832: if (core_handle_note(ph, core_php) != true) { dsamersoff@5832: goto err; dsamersoff@5832: } duke@435: break; duke@435: duke@435: case PT_LOAD: { duke@435: if (core_php->p_filesz != 0) { duke@435: if (add_map_info(ph, ph->core->core_fd, core_php->p_offset, duke@435: core_php->p_vaddr, core_php->p_filesz) == NULL) goto err; duke@435: } duke@435: break; duke@435: } duke@435: } duke@435: duke@435: core_php++; duke@435: } duke@435: duke@435: free(phbuf); duke@435: return true; duke@435: err: duke@435: free(phbuf); duke@435: return false; duke@435: } duke@435: duke@435: // read segments of a shared object duke@435: static bool read_lib_segments(struct ps_prochandle* ph, int lib_fd, ELF_EHDR* lib_ehdr, uintptr_t lib_base) { dsamersoff@5758: int i = 0; dsamersoff@5758: ELF_PHDR* phbuf; dsamersoff@5758: ELF_PHDR* lib_php = NULL; duke@435: dsamersoff@5961: int page_size = sysconf(_SC_PAGE_SIZE); duke@435: dsamersoff@5758: if ((phbuf = read_program_header_table(lib_fd, lib_ehdr)) == NULL) { dsamersoff@5758: return false; dsamersoff@5758: } dsamersoff@5758: dsamersoff@5758: // we want to process only PT_LOAD segments that are not writable. dsamersoff@5758: // i.e., text segments. The read/write/exec (data) segments would dsamersoff@5758: // have been already added from core file segments. dsamersoff@5758: for (lib_php = phbuf, i = 0; i < lib_ehdr->e_phnum; i++) { dsamersoff@5758: if ((lib_php->p_type == PT_LOAD) && !(lib_php->p_flags & PF_W) && (lib_php->p_filesz != 0)) { dsamersoff@5758: dsamersoff@5758: uintptr_t target_vaddr = lib_php->p_vaddr + lib_base; dsamersoff@5758: map_info *existing_map = core_lookup(ph, target_vaddr); dsamersoff@5758: dsamersoff@5758: if (existing_map == NULL){ dsamersoff@5758: if (add_map_info(ph, lib_fd, lib_php->p_offset, dsamersoff@5961: target_vaddr, lib_php->p_memsz) == NULL) { dsamersoff@5758: goto err; dsamersoff@5758: } dsamersoff@5758: } else { dsamersoff@5961: // Coredump stores value of p_memsz elf field dsamersoff@5961: // rounded up to page boundary. dsamersoff@5961: dsamersoff@5758: if ((existing_map->memsz != page_size) && dsamersoff@5758: (existing_map->fd != lib_fd) && dsamersoff@5961: (ROUNDUP(existing_map->memsz, page_size) != ROUNDUP(lib_php->p_memsz, page_size))) { dsamersoff@5758: dsamersoff@5961: print_debug("address conflict @ 0x%lx (existing map size = %ld, size = %ld, flags = %d)\n", dsamersoff@5961: target_vaddr, existing_map->memsz, lib_php->p_memsz, lib_php->p_flags); dsamersoff@5758: goto err; dsamersoff@5758: } dsamersoff@5758: dsamersoff@5758: /* replace PT_LOAD segment with library segment */ dsamersoff@5758: print_debug("overwrote with new address mapping (memsz %ld -> %ld)\n", dsamersoff@5961: existing_map->memsz, ROUNDUP(lib_php->p_memsz, page_size)); dsamersoff@5758: dsamersoff@5758: existing_map->fd = lib_fd; dsamersoff@5758: existing_map->offset = lib_php->p_offset; dsamersoff@5961: existing_map->memsz = ROUNDUP(lib_php->p_memsz, page_size); duke@435: } dsamersoff@5758: } duke@435: dsamersoff@5758: lib_php++; dsamersoff@5758: } dsamersoff@5758: dsamersoff@5758: free(phbuf); dsamersoff@5758: return true; duke@435: err: dsamersoff@5758: free(phbuf); dsamersoff@5758: return false; duke@435: } duke@435: duke@435: // process segments from interpreter (ld.so or ld-linux.so) duke@435: static bool read_interp_segments(struct ps_prochandle* ph) { duke@435: ELF_EHDR interp_ehdr; duke@435: duke@435: if (read_elf_header(ph->core->interp_fd, &interp_ehdr) != true) { duke@435: print_debug("interpreter is not a valid ELF file\n"); duke@435: return false; duke@435: } duke@435: duke@435: if (read_lib_segments(ph, ph->core->interp_fd, &interp_ehdr, ph->core->ld_base_addr) != true) { duke@435: print_debug("can't read segments of interpreter\n"); duke@435: return false; duke@435: } duke@435: duke@435: return true; duke@435: } duke@435: duke@435: // process segments of a a.out duke@435: static bool read_exec_segments(struct ps_prochandle* ph, ELF_EHDR* exec_ehdr) { duke@435: int i = 0; duke@435: ELF_PHDR* phbuf = NULL; duke@435: ELF_PHDR* exec_php = NULL; duke@435: duke@435: if ((phbuf = read_program_header_table(ph->core->exec_fd, exec_ehdr)) == NULL) duke@435: return false; duke@435: duke@435: for (exec_php = phbuf, i = 0; i < exec_ehdr->e_phnum; i++) { duke@435: switch (exec_php->p_type) { duke@435: duke@435: // add mappings for PT_LOAD segments duke@435: case PT_LOAD: { duke@435: // add only non-writable segments of non-zero filesz duke@435: if (!(exec_php->p_flags & PF_W) && exec_php->p_filesz != 0) { duke@435: if (add_map_info(ph, ph->core->exec_fd, exec_php->p_offset, exec_php->p_vaddr, exec_php->p_filesz) == NULL) goto err; duke@435: } duke@435: break; duke@435: } duke@435: duke@435: // read the interpreter and it's segments duke@435: case PT_INTERP: { duke@435: char interp_name[BUF_SIZE]; duke@435: duke@435: pread(ph->core->exec_fd, interp_name, MIN(exec_php->p_filesz, BUF_SIZE), exec_php->p_offset); duke@435: print_debug("ELF interpreter %s\n", interp_name); duke@435: // read interpreter segments as well duke@435: if ((ph->core->interp_fd = pathmap_open(interp_name)) < 0) { duke@435: print_debug("can't open runtime loader\n"); duke@435: goto err; duke@435: } duke@435: break; duke@435: } duke@435: duke@435: // from PT_DYNAMIC we want to read address of first link_map addr duke@435: case PT_DYNAMIC: { duke@435: ph->core->dynamic_addr = exec_php->p_vaddr; duke@435: print_debug("address of _DYNAMIC is 0x%lx\n", ph->core->dynamic_addr); duke@435: break; duke@435: } duke@435: duke@435: } // switch duke@435: exec_php++; duke@435: } // for duke@435: duke@435: free(phbuf); duke@435: return true; duke@435: err: duke@435: free(phbuf); duke@435: return false; duke@435: } duke@435: duke@435: duke@435: #define FIRST_LINK_MAP_OFFSET offsetof(struct r_debug, r_map) duke@435: #define LD_BASE_OFFSET offsetof(struct r_debug, r_ldbase) duke@435: #define LINK_MAP_ADDR_OFFSET offsetof(struct link_map, l_addr) duke@435: #define LINK_MAP_NAME_OFFSET offsetof(struct link_map, l_name) duke@435: #define LINK_MAP_NEXT_OFFSET offsetof(struct link_map, l_next) duke@435: duke@435: // read shared library info from runtime linker's data structures. duke@435: // This work is done by librtlb_db in Solaris duke@435: static bool read_shared_lib_info(struct ps_prochandle* ph) { dsamersoff@5832: uintptr_t addr = ph->core->dynamic_addr; dsamersoff@5832: uintptr_t debug_base; dsamersoff@5832: uintptr_t first_link_map_addr; dsamersoff@5832: uintptr_t ld_base_addr; dsamersoff@5832: uintptr_t link_map_addr; dsamersoff@5832: uintptr_t lib_base_diff; dsamersoff@5832: uintptr_t lib_base; dsamersoff@5832: uintptr_t lib_name_addr; dsamersoff@5832: char lib_name[BUF_SIZE]; dsamersoff@5832: ELF_DYN dyn; dsamersoff@5832: ELF_EHDR elf_ehdr; dsamersoff@5832: int lib_fd; duke@435: dsamersoff@5832: // _DYNAMIC has information of the form dsamersoff@5832: // [tag] [data] [tag] [data] ..... dsamersoff@5832: // Both tag and data are pointer sized. dsamersoff@5832: // We look for dynamic info with DT_DEBUG. This has shared object info. dsamersoff@5832: // refer to struct r_debug in link.h duke@435: dsamersoff@5832: dyn.d_tag = DT_NULL; dsamersoff@5832: while (dyn.d_tag != DT_DEBUG) { dsamersoff@5832: if (ps_pdread(ph, (psaddr_t) addr, &dyn, sizeof(ELF_DYN)) != PS_OK) { dsamersoff@5832: print_debug("can't read debug info from _DYNAMIC\n"); dsamersoff@5832: return false; dsamersoff@5832: } dsamersoff@5832: addr += sizeof(ELF_DYN); dsamersoff@5832: } duke@435: dsamersoff@5832: // we have got Dyn entry with DT_DEBUG dsamersoff@5832: debug_base = dyn.d_un.d_ptr; dsamersoff@5832: // at debug_base we have struct r_debug. This has first link map in r_map field dsamersoff@5832: if (ps_pdread(ph, (psaddr_t) debug_base + FIRST_LINK_MAP_OFFSET, duke@435: &first_link_map_addr, sizeof(uintptr_t)) != PS_OK) { dsamersoff@5832: print_debug("can't read first link map address\n"); dsamersoff@5832: return false; dsamersoff@5832: } dsamersoff@5832: dsamersoff@5832: // read ld_base address from struct r_debug dsamersoff@5832: if (ps_pdread(ph, (psaddr_t) debug_base + LD_BASE_OFFSET, &ld_base_addr, dsamersoff@5832: sizeof(uintptr_t)) != PS_OK) { dsamersoff@5832: print_debug("can't read ld base address\n"); dsamersoff@5832: return false; dsamersoff@5832: } dsamersoff@5832: ph->core->ld_base_addr = ld_base_addr; dsamersoff@5832: dsamersoff@5832: print_debug("interpreter base address is 0x%lx\n", ld_base_addr); dsamersoff@5832: dsamersoff@5832: // now read segments from interp (i.e ld.so or ld-linux.so or ld-elf.so) dsamersoff@5832: if (read_interp_segments(ph) != true) { duke@435: return false; dsamersoff@5832: } duke@435: dsamersoff@5832: // after adding interpreter (ld.so) mappings sort again dsamersoff@5832: if (sort_map_array(ph) != true) { dsamersoff@5832: return false; dsamersoff@5832: } duke@435: duke@435: print_debug("first link map is at 0x%lx\n", first_link_map_addr); duke@435: duke@435: link_map_addr = first_link_map_addr; duke@435: while (link_map_addr != 0) { duke@435: // read library base address of the .so. Note that even though calls duke@435: // link_map->l_addr as "base address", this is * not * really base virtual duke@435: // address of the shared object. This is actually the difference b/w the virtual duke@435: // address mentioned in shared object and the actual virtual base where runtime duke@435: // linker loaded it. We use "base diff" in read_lib_segments call below. duke@435: duke@435: if (ps_pdread(ph, (psaddr_t) link_map_addr + LINK_MAP_ADDR_OFFSET, duke@435: &lib_base_diff, sizeof(uintptr_t)) != PS_OK) { duke@435: print_debug("can't read shared object base address diff\n"); duke@435: return false; duke@435: } duke@435: duke@435: // read address of the name duke@435: if (ps_pdread(ph, (psaddr_t) link_map_addr + LINK_MAP_NAME_OFFSET, duke@435: &lib_name_addr, sizeof(uintptr_t)) != PS_OK) { duke@435: print_debug("can't read address of shared object name\n"); duke@435: return false; duke@435: } duke@435: duke@435: // read name of the shared object never@1820: lib_name[0] = '\0'; never@1820: if (lib_name_addr != 0 && never@1820: read_string(ph, (uintptr_t) lib_name_addr, lib_name, sizeof(lib_name)) != true) { duke@435: print_debug("can't read shared object name\n"); never@1820: // don't let failure to read the name stop opening the file. If something is really wrong never@1820: // it will fail later. duke@435: } duke@435: duke@435: if (lib_name[0] != '\0') { duke@435: // ignore empty lib names duke@435: lib_fd = pathmap_open(lib_name); duke@435: duke@435: if (lib_fd < 0) { duke@435: print_debug("can't open shared object %s\n", lib_name); duke@435: // continue with other libraries... duke@435: } else { duke@435: if (read_elf_header(lib_fd, &elf_ehdr)) { duke@435: lib_base = lib_base_diff + find_base_address(lib_fd, &elf_ehdr); duke@435: print_debug("reading library %s @ 0x%lx [ 0x%lx ]\n", duke@435: lib_name, lib_base, lib_base_diff); duke@435: // while adding library mappings we need to use "base difference". duke@435: if (! read_lib_segments(ph, lib_fd, &elf_ehdr, lib_base_diff)) { duke@435: print_debug("can't read shared object's segments\n"); duke@435: close(lib_fd); duke@435: return false; duke@435: } duke@435: add_lib_info_fd(ph, lib_name, lib_fd, lib_base); duke@435: // Map info is added for the library (lib_name) so duke@435: // we need to re-sort it before calling the p_pdread. duke@435: if (sort_map_array(ph) != true) duke@435: return false; duke@435: } else { duke@435: print_debug("can't read ELF header for shared object %s\n", lib_name); duke@435: close(lib_fd); duke@435: // continue with other libraries... duke@435: } duke@435: } duke@435: } duke@435: dsamersoff@5832: // read next link_map address dsamersoff@5832: if (ps_pdread(ph, (psaddr_t) link_map_addr + LINK_MAP_NEXT_OFFSET, dsamersoff@5832: &link_map_addr, sizeof(uintptr_t)) != PS_OK) { dsamersoff@5832: print_debug("can't read next link in link_map\n"); dsamersoff@5832: return false; dsamersoff@5832: } dsamersoff@5832: } duke@435: dsamersoff@5832: return true; duke@435: } duke@435: duke@435: // the one and only one exposed stuff from this file duke@435: struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) { dsamersoff@5832: ELF_EHDR core_ehdr; dsamersoff@5832: ELF_EHDR exec_ehdr; dsamersoff@5832: ELF_EHDR lib_ehdr; duke@435: dsamersoff@5832: struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle)); dsamersoff@5832: if (ph == NULL) { dsamersoff@5832: print_debug("can't allocate ps_prochandle\n"); dsamersoff@5832: return NULL; dsamersoff@5832: } duke@435: dsamersoff@5832: if ((ph->core = (struct core_data*) calloc(1, sizeof(struct core_data))) == NULL) { dsamersoff@5832: free(ph); dsamersoff@5832: print_debug("can't allocate ps_prochandle\n"); dsamersoff@5832: return NULL; dsamersoff@5832: } duke@435: dsamersoff@5832: // initialize ph dsamersoff@5832: ph->ops = &core_ops; dsamersoff@5832: ph->core->core_fd = -1; dsamersoff@5832: ph->core->exec_fd = -1; dsamersoff@5832: ph->core->interp_fd = -1; duke@435: dsamersoff@5832: // open the core file dsamersoff@5832: if ((ph->core->core_fd = open(core_file, O_RDONLY)) < 0) { dsamersoff@5832: print_debug("can't open core file\n"); dsamersoff@5832: goto err; dsamersoff@5832: } duke@435: dsamersoff@5832: // read core file ELF header dsamersoff@5832: if (read_elf_header(ph->core->core_fd, &core_ehdr) != true || core_ehdr.e_type != ET_CORE) { dsamersoff@5832: print_debug("core file is not a valid ELF ET_CORE file\n"); dsamersoff@5832: goto err; dsamersoff@5832: } duke@435: dsamersoff@5832: if ((ph->core->exec_fd = open(exec_file, O_RDONLY)) < 0) { dsamersoff@5832: print_debug("can't open executable file\n"); dsamersoff@5832: goto err; dsamersoff@5832: } duke@435: dsamersoff@5832: if (read_elf_header(ph->core->exec_fd, &exec_ehdr) != true || exec_ehdr.e_type != ET_EXEC) { dsamersoff@5832: print_debug("executable file is not a valid ELF ET_EXEC file\n"); dsamersoff@5832: goto err; dsamersoff@5832: } duke@435: dsamersoff@5832: // process core file segments dsamersoff@5832: if (read_core_segments(ph, &core_ehdr) != true) { dsamersoff@5832: goto err; dsamersoff@5832: } duke@435: dsamersoff@5832: // process exec file segments dsamersoff@5832: if (read_exec_segments(ph, &exec_ehdr) != true) { dsamersoff@5832: goto err; dsamersoff@5832: } duke@435: dsamersoff@5832: // exec file is also treated like a shared object for symbol search dsamersoff@5832: if (add_lib_info_fd(ph, exec_file, ph->core->exec_fd, dsamersoff@5832: (uintptr_t)0 + find_base_address(ph->core->exec_fd, &exec_ehdr)) == NULL) { dsamersoff@5832: goto err; dsamersoff@5832: } duke@435: dsamersoff@5832: // allocate and sort maps into map_array, we need to do this dsamersoff@5832: // here because read_shared_lib_info needs to read from debuggee dsamersoff@5832: // address space dsamersoff@5832: if (sort_map_array(ph) != true) { dsamersoff@5832: goto err; dsamersoff@5832: } duke@435: dsamersoff@5832: if (read_shared_lib_info(ph) != true) { dsamersoff@5832: goto err; dsamersoff@5832: } duke@435: dsamersoff@5832: // sort again because we have added more mappings from shared objects dsamersoff@5832: if (sort_map_array(ph) != true) { dsamersoff@5832: goto err; dsamersoff@5832: } duke@435: dsamersoff@5832: if (init_classsharing_workaround(ph) != true) { dsamersoff@5832: goto err; dsamersoff@5832: } duke@435: dsamersoff@5832: return ph; duke@435: duke@435: err: dsamersoff@5832: Prelease(ph); dsamersoff@5832: return NULL; duke@435: }