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