提交 6a27cf69 编写于 作者: D dsamersoff

8025250: SA: Sync linux and bsd versions of ps_core file

Summary: linux/ps_core.c and bsd/ps_core.c share most of code, but it has different formatting, comments etc.
Reviewed-by: sla, minqi
上级 4abc6fb5
...@@ -44,6 +44,7 @@ ...@@ -44,6 +44,7 @@
// close all file descriptors // close all file descriptors
static void close_files(struct ps_prochandle* ph) { static void close_files(struct ps_prochandle* ph) {
lib_info* lib = NULL; lib_info* lib = NULL;
// close core file descriptor // close core file descriptor
if (ph->core->core_fd >= 0) if (ph->core->core_fd >= 0)
close(ph->core->core_fd); close(ph->core->core_fd);
...@@ -149,8 +150,7 @@ static map_info* add_class_share_map_info(struct ps_prochandle* ph, off_t offset ...@@ -149,8 +150,7 @@ static map_info* add_class_share_map_info(struct ps_prochandle* ph, off_t offset
// Return the map_info for the given virtual address. We keep a sorted // Return the map_info for the given virtual address. We keep a sorted
// array of pointers in ph->map_array, so we can binary search. // array of pointers in ph->map_array, so we can binary search.
static map_info* core_lookup(struct ps_prochandle *ph, uintptr_t addr) static map_info* core_lookup(struct ps_prochandle *ph, uintptr_t addr) {
{
int mid, lo = 0, hi = ph->core->num_maps - 1; int mid, lo = 0, hi = ph->core->num_maps - 1;
map_info *mp; map_info *mp;
...@@ -230,9 +230,9 @@ struct FileMapHeader { ...@@ -230,9 +230,9 @@ struct FileMapHeader {
size_t _used; // for setting space top on read size_t _used; // for setting space top on read
// 4991491 NOTICE These are C++ bool's in filemap.hpp and must match up with // 4991491 NOTICE These are C++ bool's in filemap.hpp and must match up with
// the C type matching the C++ bool type on any given platform. For // the C type matching the C++ bool type on any given platform.
// Hotspot on BSD we assume the corresponding C type is char but // We assume the corresponding C type is char but licensees
// licensees on BSD versions may need to adjust the type of these fields. // may need to adjust the type of these fields.
char _read_only; // read only space? char _read_only; // read only space?
char _allow_exec; // executable code in space? char _allow_exec; // executable code in space?
...@@ -286,10 +286,12 @@ static bool read_string(struct ps_prochandle* ph, uintptr_t addr, char* buf, siz ...@@ -286,10 +286,12 @@ static bool read_string(struct ps_prochandle* ph, uintptr_t addr, char* buf, siz
#define USE_SHARED_SPACES_SYM "_UseSharedSpaces" #define USE_SHARED_SPACES_SYM "_UseSharedSpaces"
// mangled name of Arguments::SharedArchivePath // mangled name of Arguments::SharedArchivePath
#define SHARED_ARCHIVE_PATH_SYM "_ZN9Arguments17SharedArchivePathE" #define SHARED_ARCHIVE_PATH_SYM "_ZN9Arguments17SharedArchivePathE"
#define LIBJVM_NAME "/libjvm.dylib"
#else #else
#define USE_SHARED_SPACES_SYM "UseSharedSpaces" #define USE_SHARED_SPACES_SYM "UseSharedSpaces"
// mangled name of Arguments::SharedArchivePath // mangled name of Arguments::SharedArchivePath
#define SHARED_ARCHIVE_PATH_SYM "__ZN9Arguments17SharedArchivePathE" #define SHARED_ARCHIVE_PATH_SYM "__ZN9Arguments17SharedArchivePathE"
#define LIBJVM_NAME "/libjvm.so"
#endif // __APPLE_ #endif // __APPLE_
static bool init_classsharing_workaround(struct ps_prochandle* ph) { static bool init_classsharing_workaround(struct ps_prochandle* ph) {
...@@ -300,12 +302,7 @@ static bool init_classsharing_workaround(struct ps_prochandle* ph) { ...@@ -300,12 +302,7 @@ static bool init_classsharing_workaround(struct ps_prochandle* ph) {
// we are iterating over shared objects from the core dump. look for // we are iterating over shared objects from the core dump. look for
// libjvm.so. // libjvm.so.
const char *jvm_name = 0; const char *jvm_name = 0;
#ifdef __APPLE__ if ((jvm_name = strstr(lib->name, LIBJVM_NAME)) != 0) {
if ((jvm_name = strstr(lib->name, "/libjvm.dylib")) != 0)
#else
if ((jvm_name = strstr(lib->name, "/libjvm.so")) != 0)
#endif // __APPLE__
{
char classes_jsa[PATH_MAX]; char classes_jsa[PATH_MAX];
struct FileMapHeader header; struct FileMapHeader header;
int fd = -1; int fd = -1;
...@@ -399,8 +396,8 @@ static bool init_classsharing_workaround(struct ps_prochandle* ph) { ...@@ -399,8 +396,8 @@ static bool init_classsharing_workaround(struct ps_prochandle* ph) {
} }
} }
return true; return true;
} }
lib = lib->next; lib = lib->next;
} }
return true; return true;
} }
...@@ -432,8 +429,8 @@ static bool sort_map_array(struct ps_prochandle* ph) { ...@@ -432,8 +429,8 @@ static bool sort_map_array(struct ps_prochandle* ph) {
// allocate map_array // allocate map_array
map_info** array; map_info** array;
if ( (array = (map_info**) malloc(sizeof(map_info*) * num_maps)) == NULL) { if ( (array = (map_info**) malloc(sizeof(map_info*) * num_maps)) == NULL) {
print_debug("can't allocate memory for map array\n"); print_debug("can't allocate memory for map array\n");
return false; return false;
} }
// add maps to array // add maps to array
...@@ -450,7 +447,7 @@ static bool sort_map_array(struct ps_prochandle* ph) { ...@@ -450,7 +447,7 @@ static bool sort_map_array(struct ps_prochandle* ph) {
ph->core->map_array = array; ph->core->map_array = array;
// sort the map_info array by base virtual address. // sort the map_info array by base virtual address.
qsort(ph->core->map_array, ph->core->num_maps, sizeof (map_info*), qsort(ph->core->map_array, ph->core->num_maps, sizeof (map_info*),
core_cmp_mapping); core_cmp_mapping);
// print map // print map
if (is_debug()) { if (is_debug()) {
...@@ -458,7 +455,7 @@ static bool sort_map_array(struct ps_prochandle* ph) { ...@@ -458,7 +455,7 @@ static bool sort_map_array(struct ps_prochandle* ph) {
print_debug("---- sorted virtual address map ----\n"); print_debug("---- sorted virtual address map ----\n");
for (j = 0; j < ph->core->num_maps; j++) { for (j = 0; j < ph->core->num_maps; j++) {
print_debug("base = 0x%lx\tsize = %d\n", ph->core->map_array[j]->vaddr, print_debug("base = 0x%lx\tsize = %d\n", ph->core->map_array[j]->vaddr,
ph->core->map_array[j]->memsz); ph->core->map_array[j]->memsz);
} }
} }
...@@ -1091,9 +1088,9 @@ static bool core_handle_note(struct ps_prochandle* ph, ELF_PHDR* note_phdr) { ...@@ -1091,9 +1088,9 @@ static bool core_handle_note(struct ps_prochandle* ph, ELF_PHDR* note_phdr) {
notep->n_type, notep->n_descsz); notep->n_type, notep->n_descsz);
if (notep->n_type == NT_PRSTATUS) { if (notep->n_type == NT_PRSTATUS) {
if (core_handle_prstatus(ph, descdata, notep->n_descsz) != true) { if (core_handle_prstatus(ph, descdata, notep->n_descsz) != true) {
return false; return false;
} }
} }
p = descdata + ROUNDUP(notep->n_descsz, 4); p = descdata + ROUNDUP(notep->n_descsz, 4);
} }
...@@ -1121,7 +1118,7 @@ static bool read_core_segments(struct ps_prochandle* ph, ELF_EHDR* core_ehdr) { ...@@ -1121,7 +1118,7 @@ static bool read_core_segments(struct ps_prochandle* ph, ELF_EHDR* core_ehdr) {
* contains a set of saved /proc structures), and PT_LOAD (which * contains a set of saved /proc structures), and PT_LOAD (which
* represents a memory mapping from the process's address space). * represents a memory mapping from the process's address space).
* *
* Difference b/w Solaris PT_NOTE and BSD PT_NOTE: * Difference b/w Solaris PT_NOTE and Linux/BSD PT_NOTE:
* *
* In Solaris there are two PT_NOTE segments the first PT_NOTE (if present) * In Solaris there are two PT_NOTE segments the first PT_NOTE (if present)
* contains /proc structs in the pre-2.6 unstructured /proc format. the last * contains /proc structs in the pre-2.6 unstructured /proc format. the last
...@@ -1167,32 +1164,61 @@ err: ...@@ -1167,32 +1164,61 @@ err:
// read segments of a shared object // read segments of a shared object
static bool read_lib_segments(struct ps_prochandle* ph, int lib_fd, ELF_EHDR* lib_ehdr, uintptr_t lib_base) { static bool read_lib_segments(struct ps_prochandle* ph, int lib_fd, ELF_EHDR* lib_ehdr, uintptr_t lib_base) {
int i = 0; int i = 0;
ELF_PHDR* phbuf; ELF_PHDR* phbuf;
ELF_PHDR* lib_php = NULL; ELF_PHDR* lib_php = NULL;
if ((phbuf = read_program_header_table(lib_fd, lib_ehdr)) == NULL) int page_size=sysconf(_SC_PAGE_SIZE);
return false;
// we want to process only PT_LOAD segments that are not writable. if ((phbuf = read_program_header_table(lib_fd, lib_ehdr)) == NULL) {
// i.e., text segments. The read/write/exec (data) segments would return false;
// have been already added from core file segments. }
for (lib_php = phbuf, i = 0; i < lib_ehdr->e_phnum; i++) {
if ((lib_php->p_type == PT_LOAD) && !(lib_php->p_flags & PF_W) && (lib_php->p_filesz != 0)) { // we want to process only PT_LOAD segments that are not writable.
if (add_map_info(ph, lib_fd, lib_php->p_offset, lib_php->p_vaddr + lib_base, lib_php->p_filesz) == NULL) // i.e., text segments. The read/write/exec (data) segments would
goto err; // have been already added from core file segments.
for (lib_php = phbuf, i = 0; i < lib_ehdr->e_phnum; i++) {
if ((lib_php->p_type == PT_LOAD) && !(lib_php->p_flags & PF_W) && (lib_php->p_filesz != 0)) {
uintptr_t target_vaddr = lib_php->p_vaddr + lib_base;
map_info *existing_map = core_lookup(ph, target_vaddr);
if (existing_map == NULL){
if (add_map_info(ph, lib_fd, lib_php->p_offset,
target_vaddr, lib_php->p_filesz) == NULL) {
goto err;
}
} else {
if ((existing_map->memsz != page_size) &&
(existing_map->fd != lib_fd) &&
(existing_map->memsz != lib_php->p_filesz)){
print_debug("address conflict @ 0x%lx (size = %ld, flags = %d\n)",
target_vaddr, lib_php->p_filesz, lib_php->p_flags);
goto err;
}
/* replace PT_LOAD segment with library segment */
print_debug("overwrote with new address mapping (memsz %ld -> %ld)\n",
existing_map->memsz, lib_php->p_filesz);
existing_map->fd = lib_fd;
existing_map->offset = lib_php->p_offset;
existing_map->memsz = lib_php->p_filesz;
} }
lib_php++; }
}
free(phbuf); lib_php++;
return true; }
free(phbuf);
return true;
err: err:
free(phbuf); free(phbuf);
return false; return false;
} }
// process segments from interpreter (ld-elf.so.1) // process segments from interpreter (ld.so or ld-linux.so or ld-elf.so)
static bool read_interp_segments(struct ps_prochandle* ph) { static bool read_interp_segments(struct ps_prochandle* ph) {
ELF_EHDR interp_ehdr; ELF_EHDR interp_ehdr;
...@@ -1303,32 +1329,34 @@ static bool read_shared_lib_info(struct ps_prochandle* ph) { ...@@ -1303,32 +1329,34 @@ static bool read_shared_lib_info(struct ps_prochandle* ph) {
debug_base = dyn.d_un.d_ptr; debug_base = dyn.d_un.d_ptr;
// at debug_base we have struct r_debug. This has first link map in r_map field // at debug_base we have struct r_debug. This has first link map in r_map field
if (ps_pread(ph, (psaddr_t) debug_base + FIRST_LINK_MAP_OFFSET, if (ps_pread(ph, (psaddr_t) debug_base + FIRST_LINK_MAP_OFFSET,
&first_link_map_addr, sizeof(uintptr_t)) != PS_OK) { &first_link_map_addr, sizeof(uintptr_t)) != PS_OK) {
print_debug("can't read first link map address\n"); print_debug("can't read first link map address\n");
return false; return false;
} }
// read ld_base address from struct r_debug // read ld_base address from struct r_debug
// XXX: There is no r_ldbase member on BSD #if 0 // There is no r_ldbase member on BSD
/*
if (ps_pread(ph, (psaddr_t) debug_base + LD_BASE_OFFSET, &ld_base_addr, if (ps_pread(ph, (psaddr_t) debug_base + LD_BASE_OFFSET, &ld_base_addr,
sizeof(uintptr_t)) != PS_OK) { sizeof(uintptr_t)) != PS_OK) {
print_debug("can't read ld base address\n"); print_debug("can't read ld base address\n");
return false; return false;
} }
ph->core->ld_base_addr = ld_base_addr; ph->core->ld_base_addr = ld_base_addr;
*/ #else
ph->core->ld_base_addr = 0; ph->core->ld_base_addr = 0;
#endif
print_debug("interpreter base address is 0x%lx\n", ld_base_addr); print_debug("interpreter base address is 0x%lx\n", ld_base_addr);
// now read segments from interp (i.e ld-elf.so.1) // now read segments from interp (i.e ld.so or ld-linux.so or ld-elf.so)
if (read_interp_segments(ph) != true) if (read_interp_segments(ph) != true) {
return false; return false;
}
// after adding interpreter (ld.so) mappings sort again // after adding interpreter (ld.so) mappings sort again
if (sort_map_array(ph) != true) if (sort_map_array(ph) != true) {
return false; return false;
}
print_debug("first link map is at 0x%lx\n", first_link_map_addr); print_debug("first link map is at 0x%lx\n", first_link_map_addr);
...@@ -1380,8 +1408,9 @@ static bool read_shared_lib_info(struct ps_prochandle* ph) { ...@@ -1380,8 +1408,9 @@ static bool read_shared_lib_info(struct ps_prochandle* ph) {
add_lib_info_fd(ph, lib_name, lib_fd, lib_base); add_lib_info_fd(ph, lib_name, lib_fd, lib_base);
// Map info is added for the library (lib_name) so // Map info is added for the library (lib_name) so
// we need to re-sort it before calling the p_pdread. // we need to re-sort it before calling the p_pdread.
if (sort_map_array(ph) != true) if (sort_map_array(ph) != true) {
return false; return false;
}
} else { } else {
print_debug("can't read ELF header for shared object %s\n", lib_name); print_debug("can't read ELF header for shared object %s\n", lib_name);
close(lib_fd); close(lib_fd);
...@@ -1392,7 +1421,7 @@ static bool read_shared_lib_info(struct ps_prochandle* ph) { ...@@ -1392,7 +1421,7 @@ static bool read_shared_lib_info(struct ps_prochandle* ph) {
// read next link_map address // read next link_map address
if (ps_pread(ph, (psaddr_t) link_map_addr + LINK_MAP_NEXT_OFFSET, if (ps_pread(ph, (psaddr_t) link_map_addr + LINK_MAP_NEXT_OFFSET,
&link_map_addr, sizeof(uintptr_t)) != PS_OK) { &link_map_addr, sizeof(uintptr_t)) != PS_OK) {
print_debug("can't read next link in link_map\n"); print_debug("can't read next link in link_map\n");
return false; return false;
} }
...@@ -1408,7 +1437,7 @@ struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) { ...@@ -1408,7 +1437,7 @@ struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) {
struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle)); struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle));
if (ph == NULL) { if (ph == NULL) {
print_debug("cant allocate ps_prochandle\n"); print_debug("can't allocate ps_prochandle\n");
return NULL; return NULL;
} }
...@@ -1444,38 +1473,45 @@ struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) { ...@@ -1444,38 +1473,45 @@ struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) {
} }
if (read_elf_header(ph->core->exec_fd, &exec_ehdr) != true || exec_ehdr.e_type != ET_EXEC) { if (read_elf_header(ph->core->exec_fd, &exec_ehdr) != true || exec_ehdr.e_type != ET_EXEC) {
print_debug("executable file is not a valid ELF ET_EXEC file\n"); print_debug("executable file is not a valid ELF ET_EXEC file\n");
goto err; goto err;
} }
// process core file segments // process core file segments
if (read_core_segments(ph, &core_ehdr) != true) if (read_core_segments(ph, &core_ehdr) != true) {
goto err; goto err;
}
// process exec file segments // process exec file segments
if (read_exec_segments(ph, &exec_ehdr) != true) if (read_exec_segments(ph, &exec_ehdr) != true) {
goto err; goto err;
}
// exec file is also treated like a shared object for symbol search // exec file is also treated like a shared object for symbol search
if (add_lib_info_fd(ph, exec_file, ph->core->exec_fd, if (add_lib_info_fd(ph, exec_file, ph->core->exec_fd,
(uintptr_t)0 + find_base_address(ph->core->exec_fd, &exec_ehdr)) == NULL) (uintptr_t)0 + find_base_address(ph->core->exec_fd, &exec_ehdr)) == NULL) {
goto err; goto err;
}
// allocate and sort maps into map_array, we need to do this // allocate and sort maps into map_array, we need to do this
// here because read_shared_lib_info needs to read from debuggee // here because read_shared_lib_info needs to read from debuggee
// address space // address space
if (sort_map_array(ph) != true) if (sort_map_array(ph) != true) {
goto err; goto err;
}
if (read_shared_lib_info(ph) != true) if (read_shared_lib_info(ph) != true) {
goto err; goto err;
}
// sort again because we have added more mappings from shared objects // sort again because we have added more mappings from shared objects
if (sort_map_array(ph) != true) if (sort_map_array(ph) != true) {
goto err; goto err;
}
if (init_classsharing_workaround(ph) != true) if (init_classsharing_workaround(ph) != true) {
goto err; goto err;
}
print_debug("Leave Pgrab_core\n"); print_debug("Leave Pgrab_core\n");
return ph; return ph;
......
...@@ -41,155 +41,158 @@ ...@@ -41,155 +41,158 @@
// ps_prochandle cleanup helper functions // ps_prochandle cleanup helper functions
// close all file descriptors // close all file descriptors
static void close_elf_files(struct ps_prochandle* ph) { static void close_files(struct ps_prochandle* ph) {
lib_info* lib = NULL; lib_info* lib = NULL;
// close core file descriptor // close core file descriptor
if (ph->core->core_fd >= 0) if (ph->core->core_fd >= 0)
close(ph->core->core_fd); close(ph->core->core_fd);
// close exec file descriptor // close exec file descriptor
if (ph->core->exec_fd >= 0) if (ph->core->exec_fd >= 0)
close(ph->core->exec_fd); close(ph->core->exec_fd);
// close interp file descriptor // close interp file descriptor
if (ph->core->interp_fd >= 0) if (ph->core->interp_fd >= 0)
close(ph->core->interp_fd); close(ph->core->interp_fd);
// close class share archive file // close class share archive file
if (ph->core->classes_jsa_fd >= 0) if (ph->core->classes_jsa_fd >= 0)
close(ph->core->classes_jsa_fd); close(ph->core->classes_jsa_fd);
// close all library file descriptors // close all library file descriptors
lib = ph->libs; lib = ph->libs;
while (lib) { while (lib) {
int fd = lib->fd; int fd = lib->fd;
if (fd >= 0 && fd != ph->core->exec_fd) close(fd); if (fd >= 0 && fd != ph->core->exec_fd) {
lib = lib->next; close(fd);
} }
lib = lib->next;
}
} }
// clean all map_info stuff // clean all map_info stuff
static void destroy_map_info(struct ps_prochandle* ph) { static void destroy_map_info(struct ps_prochandle* ph) {
map_info* map = ph->core->maps; map_info* map = ph->core->maps;
while (map) { while (map) {
map_info* next = map->next; map_info* next = map->next;
free(map); free(map);
map = next; map = next;
} }
if (ph->core->map_array) { if (ph->core->map_array) {
free(ph->core->map_array); free(ph->core->map_array);
} }
// Part of the class sharing workaround // Part of the class sharing workaround
map = ph->core->class_share_maps; map = ph->core->class_share_maps;
while (map) { while (map) {
map_info* next = map->next; map_info* next = map->next;
free(map); free(map);
map = next; map = next;
} }
} }
// ps_prochandle operations // ps_prochandle operations
static void core_release(struct ps_prochandle* ph) { static void core_release(struct ps_prochandle* ph) {
if (ph->core) { if (ph->core) {
close_elf_files(ph); close_files(ph);
destroy_map_info(ph); destroy_map_info(ph);
free(ph->core); free(ph->core);
} }
} }
static map_info* allocate_init_map(int fd, off_t offset, uintptr_t vaddr, size_t memsz) { static map_info* allocate_init_map(int fd, off_t offset, uintptr_t vaddr, size_t memsz) {
map_info* map; map_info* map;
if ( (map = (map_info*) calloc(1, sizeof(map_info))) == NULL) { if ( (map = (map_info*) calloc(1, sizeof(map_info))) == NULL) {
print_debug("can't allocate memory for map_info\n"); print_debug("can't allocate memory for map_info\n");
return NULL; return NULL;
} }
// initialize map // initialize map
map->fd = fd; map->fd = fd;
map->offset = offset; map->offset = offset;
map->vaddr = vaddr; map->vaddr = vaddr;
map->memsz = memsz; map->memsz = memsz;
return map; return map;
} }
// add map info with given fd, offset, vaddr and memsz // add map info with given fd, offset, vaddr and memsz
static map_info* add_map_info(struct ps_prochandle* ph, int fd, off_t offset, static map_info* add_map_info(struct ps_prochandle* ph, int fd, off_t offset,
uintptr_t vaddr, size_t memsz) { uintptr_t vaddr, size_t memsz) {
map_info* map; map_info* map;
if ((map = allocate_init_map(fd, offset, vaddr, memsz)) == NULL) { if ((map = allocate_init_map(fd, offset, vaddr, memsz)) == NULL) {
return NULL; return NULL;
} }
// add this to map list // add this to map list
map->next = ph->core->maps; map->next = ph->core->maps;
ph->core->maps = map; ph->core->maps = map;
ph->core->num_maps++; ph->core->num_maps++;
return map; return map;
} }
// Part of the class sharing workaround // Part of the class sharing workaround
static void add_class_share_map_info(struct ps_prochandle* ph, off_t offset, static map_info* add_class_share_map_info(struct ps_prochandle* ph, off_t offset,
uintptr_t vaddr, size_t memsz) { uintptr_t vaddr, size_t memsz) {
map_info* map; map_info* map;
if ((map = allocate_init_map(ph->core->classes_jsa_fd, if ((map = allocate_init_map(ph->core->classes_jsa_fd,
offset, vaddr, memsz)) == NULL) { offset, vaddr, memsz)) == NULL) {
return; return NULL;
} }
map->next = ph->core->class_share_maps; map->next = ph->core->class_share_maps;
ph->core->class_share_maps = map; ph->core->class_share_maps = map;
return map;
} }
// Return the map_info for the given virtual address. We keep a sorted // Return the map_info for the given virtual address. We keep a sorted
// array of pointers in ph->map_array, so we can binary search. // array of pointers in ph->map_array, so we can binary search.
static map_info* core_lookup(struct ps_prochandle *ph, uintptr_t addr) static map_info* core_lookup(struct ps_prochandle *ph, uintptr_t addr) {
{ int mid, lo = 0, hi = ph->core->num_maps - 1;
int mid, lo = 0, hi = ph->core->num_maps - 1; map_info *mp;
map_info *mp;
while (hi - lo > 1) {
while (hi - lo > 1) { mid = (lo + hi) / 2;
mid = (lo + hi) / 2; if (addr >= ph->core->map_array[mid]->vaddr) {
if (addr >= ph->core->map_array[mid]->vaddr) lo = mid;
lo = mid; } else {
else hi = mid;
hi = mid; }
} }
if (addr < ph->core->map_array[hi]->vaddr) if (addr < ph->core->map_array[hi]->vaddr) {
mp = ph->core->map_array[lo]; mp = ph->core->map_array[lo];
else } else {
mp = ph->core->map_array[hi]; mp = ph->core->map_array[hi];
}
if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) {
return (mp); return (mp);
}
// Part of the class sharing workaround // Part of the class sharing workaround
// Unfortunately, we have no way of detecting -Xshare state. // Unfortunately, we have no way of detecting -Xshare state.
// Check out the share maps atlast, if we don't find anywhere. // Check out the share maps atlast, if we don't find anywhere.
// This is done this way so to avoid reading share pages // This is done this way so to avoid reading share pages
// ahead of other normal maps. For eg. with -Xshare:off we don't // ahead of other normal maps. For eg. with -Xshare:off we don't
// want to prefer class sharing data to data from core. // want to prefer class sharing data to data from core.
mp = ph->core->class_share_maps; mp = ph->core->class_share_maps;
if (mp) { if (mp) {
print_debug("can't locate map_info at 0x%lx, trying class share maps\n", print_debug("can't locate map_info at 0x%lx, trying class share maps\n", addr);
addr); }
} while (mp) {
while (mp) { if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) {
if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) { print_debug("located map_info at 0x%lx from class share maps\n", addr);
print_debug("located map_info at 0x%lx from class share maps\n", return (mp);
addr); }
return (mp); mp = mp->next;
} }
mp = mp->next;
}
print_debug("can't locate map_info at 0x%lx\n", addr); print_debug("can't locate map_info at 0x%lx\n", addr);
return (NULL); return (NULL);
} }
//--------------------------------------------------------------- //---------------------------------------------------------------
...@@ -226,9 +229,9 @@ struct FileMapHeader { ...@@ -226,9 +229,9 @@ struct FileMapHeader {
size_t _used; // for setting space top on read size_t _used; // for setting space top on read
// 4991491 NOTICE These are C++ bool's in filemap.hpp and must match up with // 4991491 NOTICE These are C++ bool's in filemap.hpp and must match up with
// the C type matching the C++ bool type on any given platform. For // the C type matching the C++ bool type on any given platform.
// Hotspot on Linux we assume the corresponding C type is char but // We assume the corresponding C type is char but licensees
// licensees on Linux versions may need to adjust the type of these fields. // may need to adjust the type of these fields.
char _read_only; // read only space? char _read_only; // read only space?
char _allow_exec; // executable code in space? char _allow_exec; // executable code in space?
...@@ -238,154 +241,159 @@ struct FileMapHeader { ...@@ -238,154 +241,159 @@ struct FileMapHeader {
}; };
static bool read_jboolean(struct ps_prochandle* ph, uintptr_t addr, jboolean* pvalue) { static bool read_jboolean(struct ps_prochandle* ph, uintptr_t addr, jboolean* pvalue) {
jboolean i; jboolean i;
if (ps_pdread(ph, (psaddr_t) addr, &i, sizeof(i)) == PS_OK) { if (ps_pdread(ph, (psaddr_t) addr, &i, sizeof(i)) == PS_OK) {
*pvalue = i; *pvalue = i;
return true; return true;
} else { } else {
return false; return false;
} }
} }
static bool read_pointer(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* pvalue) { static bool read_pointer(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* pvalue) {
uintptr_t uip; uintptr_t uip;
if (ps_pdread(ph, (psaddr_t) addr, &uip, sizeof(uip)) == PS_OK) { if (ps_pdread(ph, (psaddr_t) addr, (char *)&uip, sizeof(uip)) == PS_OK) {
*pvalue = uip; *pvalue = uip;
return true; return true;
} else { } else {
return false; return false;
} }
} }
// used to read strings from debuggee // used to read strings from debuggee
static bool read_string(struct ps_prochandle* ph, uintptr_t addr, char* buf, size_t size) { static bool read_string(struct ps_prochandle* ph, uintptr_t addr, char* buf, size_t size) {
size_t i = 0; size_t i = 0;
char c = ' '; char c = ' ';
while (c != '\0') { while (c != '\0') {
if (ps_pdread(ph, (psaddr_t) addr, &c, sizeof(char)) != PS_OK) if (ps_pdread(ph, (psaddr_t) addr, &c, sizeof(char)) != PS_OK) {
return false; return false;
if (i < size - 1) }
buf[i] = c; if (i < size - 1) {
else // smaller buffer buf[i] = c;
return false; } else {
i++; addr++; // smaller buffer
} return false;
}
i++; addr++;
}
buf[i] = '\0'; buf[i] = '\0';
return true; return true;
} }
#define USE_SHARED_SPACES_SYM "UseSharedSpaces" #define USE_SHARED_SPACES_SYM "UseSharedSpaces"
// mangled name of Arguments::SharedArchivePath // mangled name of Arguments::SharedArchivePath
#define SHARED_ARCHIVE_PATH_SYM "_ZN9Arguments17SharedArchivePathE" #define SHARED_ARCHIVE_PATH_SYM "_ZN9Arguments17SharedArchivePathE"
#define LIBJVM_NAME "/libjvm.so"
static bool init_classsharing_workaround(struct ps_prochandle* ph) { static bool init_classsharing_workaround(struct ps_prochandle* ph) {
lib_info* lib = ph->libs; lib_info* lib = ph->libs;
while (lib != NULL) { while (lib != NULL) {
// we are iterating over shared objects from the core dump. look for // we are iterating over shared objects from the core dump. look for
// libjvm.so. // libjvm.so.
const char *jvm_name = 0; const char *jvm_name = 0;
if ((jvm_name = strstr(lib->name, "/libjvm.so")) != 0) { if ((jvm_name = strstr(lib->name, LIBJVM_NAME)) != 0) {
char classes_jsa[PATH_MAX]; char classes_jsa[PATH_MAX];
struct FileMapHeader header; struct FileMapHeader header;
size_t n = 0; int fd = -1;
int fd = -1, m = 0; int m = 0;
uintptr_t base = 0, useSharedSpacesAddr = 0; size_t n = 0;
uintptr_t sharedArchivePathAddrAddr = 0, sharedArchivePathAddr = 0; uintptr_t base = 0, useSharedSpacesAddr = 0;
jboolean useSharedSpaces = 0; uintptr_t sharedArchivePathAddrAddr = 0, sharedArchivePathAddr = 0;
map_info* mi = 0; jboolean useSharedSpaces = 0;
map_info* mi = 0;
memset(classes_jsa, 0, sizeof(classes_jsa));
jvm_name = lib->name; memset(classes_jsa, 0, sizeof(classes_jsa));
useSharedSpacesAddr = lookup_symbol(ph, jvm_name, USE_SHARED_SPACES_SYM); jvm_name = lib->name;
if (useSharedSpacesAddr == 0) { useSharedSpacesAddr = lookup_symbol(ph, jvm_name, USE_SHARED_SPACES_SYM);
print_debug("can't lookup 'UseSharedSpaces' flag\n"); if (useSharedSpacesAddr == 0) {
return false; print_debug("can't lookup 'UseSharedSpaces' flag\n");
} return false;
}
// Hotspot vm types are not exported to build this library. So // Hotspot vm types are not exported to build this library. So
// using equivalent type jboolean to read the value of // using equivalent type jboolean to read the value of
// UseSharedSpaces which is same as hotspot type "bool". // UseSharedSpaces which is same as hotspot type "bool".
if (read_jboolean(ph, useSharedSpacesAddr, &useSharedSpaces) != true) { if (read_jboolean(ph, useSharedSpacesAddr, &useSharedSpaces) != true) {
print_debug("can't read the value of 'UseSharedSpaces' flag\n"); print_debug("can't read the value of 'UseSharedSpaces' flag\n");
return false; return false;
} }
if ((int)useSharedSpaces == 0) { if ((int)useSharedSpaces == 0) {
print_debug("UseSharedSpaces is false, assuming -Xshare:off!\n"); print_debug("UseSharedSpaces is false, assuming -Xshare:off!\n");
return true; return true;
} }
sharedArchivePathAddrAddr = lookup_symbol(ph, jvm_name, SHARED_ARCHIVE_PATH_SYM); sharedArchivePathAddrAddr = lookup_symbol(ph, jvm_name, SHARED_ARCHIVE_PATH_SYM);
if (sharedArchivePathAddrAddr == 0) { if (sharedArchivePathAddrAddr == 0) {
print_debug("can't lookup shared archive path symbol\n"); print_debug("can't lookup shared archive path symbol\n");
return false; return false;
} }
if (read_pointer(ph, sharedArchivePathAddrAddr, &sharedArchivePathAddr) != true) { if (read_pointer(ph, sharedArchivePathAddrAddr, &sharedArchivePathAddr) != true) {
print_debug("can't read shared archive path pointer\n"); print_debug("can't read shared archive path pointer\n");
return false; return false;
} }
if (read_string(ph, sharedArchivePathAddr, classes_jsa, sizeof(classes_jsa)) != true) { if (read_string(ph, sharedArchivePathAddr, classes_jsa, sizeof(classes_jsa)) != true) {
print_debug("can't read shared archive path value\n"); print_debug("can't read shared archive path value\n");
return false; return false;
} }
print_debug("looking for %s\n", classes_jsa); print_debug("looking for %s\n", classes_jsa);
// open the class sharing archive file // open the class sharing archive file
fd = pathmap_open(classes_jsa); fd = pathmap_open(classes_jsa);
if (fd < 0) { if (fd < 0) {
print_debug("can't open %s!\n", classes_jsa); print_debug("can't open %s!\n", classes_jsa);
ph->core->classes_jsa_fd = -1; ph->core->classes_jsa_fd = -1;
return false; return false;
} else { } else {
print_debug("opened %s\n", classes_jsa); print_debug("opened %s\n", classes_jsa);
} }
// read FileMapHeader from the file // read FileMapHeader from the file
memset(&header, 0, sizeof(struct FileMapHeader)); memset(&header, 0, sizeof(struct FileMapHeader));
if ((n = read(fd, &header, sizeof(struct FileMapHeader))) if ((n = read(fd, &header, sizeof(struct FileMapHeader)))
!= sizeof(struct FileMapHeader)) { != sizeof(struct FileMapHeader)) {
print_debug("can't read shared archive file map header from %s\n", classes_jsa); print_debug("can't read shared archive file map header from %s\n", classes_jsa);
close(fd); close(fd);
return false; return false;
} }
// check file magic // check file magic
if (header._magic != 0xf00baba2) { if (header._magic != 0xf00baba2) {
print_debug("%s has bad shared archive file magic number 0x%x, expecing 0xf00baba2\n", print_debug("%s has bad shared archive file magic number 0x%x, expecing 0xf00baba2\n",
classes_jsa, header._magic); classes_jsa, header._magic);
close(fd); close(fd);
return false; return false;
} }
// check version // check version
if (header._version != CURRENT_ARCHIVE_VERSION) { if (header._version != CURRENT_ARCHIVE_VERSION) {
print_debug("%s has wrong shared archive file version %d, expecting %d\n", print_debug("%s has wrong shared archive file version %d, expecting %d\n",
classes_jsa, header._version, CURRENT_ARCHIVE_VERSION); classes_jsa, header._version, CURRENT_ARCHIVE_VERSION);
close(fd); close(fd);
return false; return false;
} }
ph->core->classes_jsa_fd = fd; ph->core->classes_jsa_fd = fd;
// add read-only maps from classes.jsa to the list of maps // add read-only maps from classes.jsa to the list of maps
for (m = 0; m < NUM_SHARED_MAPS; m++) { for (m = 0; m < NUM_SHARED_MAPS; m++) {
if (header._space[m]._read_only) { if (header._space[m]._read_only) {
base = (uintptr_t) header._space[m]._base; base = (uintptr_t) header._space[m]._base;
// no need to worry about the fractional pages at-the-end. // no need to worry about the fractional pages at-the-end.
// possible fractional pages are handled by core_read_data. // possible fractional pages are handled by core_read_data.
add_class_share_map_info(ph, (off_t) header._space[m]._file_offset, add_class_share_map_info(ph, (off_t) header._space[m]._file_offset,
base, (size_t) header._space[m]._used); base, (size_t) header._space[m]._used);
print_debug("added a share archive map at 0x%lx\n", base); print_debug("added a share archive map at 0x%lx\n", base);
} }
}
return true;
} }
lib = lib->next; return true;
} }
return true; lib = lib->next;
}
return true;
} }
...@@ -396,54 +404,58 @@ static bool init_classsharing_workaround(struct ps_prochandle* ph) { ...@@ -396,54 +404,58 @@ static bool init_classsharing_workaround(struct ps_prochandle* ph) {
// callback for sorting the array of map_info pointers. // callback for sorting the array of map_info pointers.
static int core_cmp_mapping(const void *lhsp, const void *rhsp) static int core_cmp_mapping(const void *lhsp, const void *rhsp)
{ {
const map_info *lhs = *((const map_info **)lhsp); const map_info *lhs = *((const map_info **)lhsp);
const map_info *rhs = *((const map_info **)rhsp); const map_info *rhs = *((const map_info **)rhsp);
if (lhs->vaddr == rhs->vaddr) if (lhs->vaddr == rhs->vaddr) {
return (0); return (0);
}
return (lhs->vaddr < rhs->vaddr ? -1 : 1); return (lhs->vaddr < rhs->vaddr ? -1 : 1);
} }
// we sort map_info by starting virtual address so that we can do // we sort map_info by starting virtual address so that we can do
// binary search to read from an address. // binary search to read from an address.
static bool sort_map_array(struct ps_prochandle* ph) { static bool sort_map_array(struct ps_prochandle* ph) {
size_t num_maps = ph->core->num_maps; size_t num_maps = ph->core->num_maps;
map_info* map = ph->core->maps; map_info* map = ph->core->maps;
int i = 0; int i = 0;
// allocate map_array // allocate map_array
map_info** array; map_info** array;
if ( (array = (map_info**) malloc(sizeof(map_info*) * num_maps)) == NULL) { if ( (array = (map_info**) malloc(sizeof(map_info*) * num_maps)) == NULL) {
print_debug("can't allocate memory for map array\n"); print_debug("can't allocate memory for map array\n");
return false; return false;
} }
// add maps to array // add maps to array
while (map) { while (map) {
array[i] = map; array[i] = map;
i++; i++;
map = map->next; map = map->next;
} }
// sort is called twice. If this is second time, clear map array // sort is called twice. If this is second time, clear map array
if (ph->core->map_array) free(ph->core->map_array); if (ph->core->map_array) {
ph->core->map_array = array; free(ph->core->map_array);
// sort the map_info array by base virtual address. }
qsort(ph->core->map_array, ph->core->num_maps, sizeof (map_info*),
core_cmp_mapping);
// print map ph->core->map_array = array;
if (is_debug()) { // sort the map_info array by base virtual address.
int j = 0; qsort(ph->core->map_array, ph->core->num_maps, sizeof (map_info*),
print_debug("---- sorted virtual address map ----\n"); core_cmp_mapping);
for (j = 0; j < ph->core->num_maps; j++) {
print_debug("base = 0x%lx\tsize = %zu\n", ph->core->map_array[j]->vaddr, // print map
ph->core->map_array[j]->memsz); if (is_debug()) {
} int j = 0;
} print_debug("---- sorted virtual address map ----\n");
for (j = 0; j < ph->core->num_maps; j++) {
print_debug("base = 0x%lx\tsize = %zu\n", ph->core->map_array[j]->vaddr,
ph->core->map_array[j]->memsz);
}
}
return true; return true;
} }
#ifndef MIN #ifndef MIN
...@@ -460,16 +472,18 @@ static bool core_read_data(struct ps_prochandle* ph, uintptr_t addr, char *buf, ...@@ -460,16 +472,18 @@ static bool core_read_data(struct ps_prochandle* ph, uintptr_t addr, char *buf,
off_t off; off_t off;
int fd; int fd;
if (mp == NULL) if (mp == NULL) {
break; /* No mapping for this address */ break; /* No mapping for this address */
}
fd = mp->fd; fd = mp->fd;
mapoff = addr - mp->vaddr; mapoff = addr - mp->vaddr;
len = MIN(resid, mp->memsz - mapoff); len = MIN(resid, mp->memsz - mapoff);
off = mp->offset + mapoff; off = mp->offset + mapoff;
if ((len = pread(fd, buf, len, off)) <= 0) if ((len = pread(fd, buf, len, off)) <= 0) {
break; break;
}
resid -= len; resid -= len;
addr += len; addr += len;
...@@ -625,8 +639,9 @@ static bool core_handle_note(struct ps_prochandle* ph, ELF_PHDR* note_phdr) { ...@@ -625,8 +639,9 @@ static bool core_handle_note(struct ps_prochandle* ph, ELF_PHDR* note_phdr) {
notep->n_type, notep->n_descsz); notep->n_type, notep->n_descsz);
if (notep->n_type == NT_PRSTATUS) { if (notep->n_type == NT_PRSTATUS) {
if (core_handle_prstatus(ph, descdata, notep->n_descsz) != true) if (core_handle_prstatus(ph, descdata, notep->n_descsz) != true) {
return false; return false;
}
} }
p = descdata + ROUNDUP(notep->n_descsz, 4); p = descdata + ROUNDUP(notep->n_descsz, 4);
} }
...@@ -654,7 +669,7 @@ static bool read_core_segments(struct ps_prochandle* ph, ELF_EHDR* core_ehdr) { ...@@ -654,7 +669,7 @@ static bool read_core_segments(struct ps_prochandle* ph, ELF_EHDR* core_ehdr) {
* contains a set of saved /proc structures), and PT_LOAD (which * contains a set of saved /proc structures), and PT_LOAD (which
* represents a memory mapping from the process's address space). * represents a memory mapping from the process's address space).
* *
* Difference b/w Solaris PT_NOTE and Linux PT_NOTE: * Difference b/w Solaris PT_NOTE and Linux/BSD PT_NOTE:
* *
* In Solaris there are two PT_NOTE segments the first PT_NOTE (if present) * In Solaris there are two PT_NOTE segments the first PT_NOTE (if present)
* contains /proc structs in the pre-2.6 unstructured /proc format. the last * contains /proc structs in the pre-2.6 unstructured /proc format. the last
...@@ -674,7 +689,9 @@ static bool read_core_segments(struct ps_prochandle* ph, ELF_EHDR* core_ehdr) { ...@@ -674,7 +689,9 @@ static bool read_core_segments(struct ps_prochandle* ph, ELF_EHDR* core_ehdr) {
for (core_php = phbuf, i = 0; i < core_ehdr->e_phnum; i++) { for (core_php = phbuf, i = 0; i < core_ehdr->e_phnum; i++) {
switch (core_php->p_type) { switch (core_php->p_type) {
case PT_NOTE: case PT_NOTE:
if (core_handle_note(ph, core_php) != true) goto err; if (core_handle_note(ph, core_php) != true) {
goto err;
}
break; break;
case PT_LOAD: { case PT_LOAD: {
...@@ -832,60 +849,62 @@ err: ...@@ -832,60 +849,62 @@ err:
// read shared library info from runtime linker's data structures. // read shared library info from runtime linker's data structures.
// This work is done by librtlb_db in Solaris // This work is done by librtlb_db in Solaris
static bool read_shared_lib_info(struct ps_prochandle* ph) { static bool read_shared_lib_info(struct ps_prochandle* ph) {
uintptr_t addr = ph->core->dynamic_addr; uintptr_t addr = ph->core->dynamic_addr;
uintptr_t debug_base; uintptr_t debug_base;
uintptr_t first_link_map_addr; uintptr_t first_link_map_addr;
uintptr_t ld_base_addr; uintptr_t ld_base_addr;
uintptr_t link_map_addr; uintptr_t link_map_addr;
uintptr_t lib_base_diff; uintptr_t lib_base_diff;
uintptr_t lib_base; uintptr_t lib_base;
uintptr_t lib_name_addr; uintptr_t lib_name_addr;
char lib_name[BUF_SIZE]; char lib_name[BUF_SIZE];
ELF_DYN dyn; ELF_DYN dyn;
ELF_EHDR elf_ehdr; ELF_EHDR elf_ehdr;
int lib_fd; int lib_fd;
// _DYNAMIC has information of the form // _DYNAMIC has information of the form
// [tag] [data] [tag] [data] ..... // [tag] [data] [tag] [data] .....
// Both tag and data are pointer sized. // Both tag and data are pointer sized.
// We look for dynamic info with DT_DEBUG. This has shared object info. // We look for dynamic info with DT_DEBUG. This has shared object info.
// refer to struct r_debug in link.h // refer to struct r_debug in link.h
dyn.d_tag = DT_NULL; dyn.d_tag = DT_NULL;
while (dyn.d_tag != DT_DEBUG) { while (dyn.d_tag != DT_DEBUG) {
if (ps_pdread(ph, (psaddr_t) addr, &dyn, sizeof(ELF_DYN)) != PS_OK) { if (ps_pdread(ph, (psaddr_t) addr, &dyn, sizeof(ELF_DYN)) != PS_OK) {
print_debug("can't read debug info from _DYNAMIC\n"); print_debug("can't read debug info from _DYNAMIC\n");
return false; return false;
} }
addr += sizeof(ELF_DYN); addr += sizeof(ELF_DYN);
} }
// we have got Dyn entry with DT_DEBUG // we have got Dyn entry with DT_DEBUG
debug_base = dyn.d_un.d_ptr; debug_base = dyn.d_un.d_ptr;
// at debug_base we have struct r_debug. This has first link map in r_map field // at debug_base we have struct r_debug. This has first link map in r_map field
if (ps_pdread(ph, (psaddr_t) debug_base + FIRST_LINK_MAP_OFFSET, if (ps_pdread(ph, (psaddr_t) debug_base + FIRST_LINK_MAP_OFFSET,
&first_link_map_addr, sizeof(uintptr_t)) != PS_OK) { &first_link_map_addr, sizeof(uintptr_t)) != PS_OK) {
print_debug("can't read first link map address\n"); print_debug("can't read first link map address\n");
return false; return false;
} }
// read ld_base address from struct r_debug // read ld_base address from struct r_debug
if (ps_pdread(ph, (psaddr_t) debug_base + LD_BASE_OFFSET, &ld_base_addr, if (ps_pdread(ph, (psaddr_t) debug_base + LD_BASE_OFFSET, &ld_base_addr,
sizeof(uintptr_t)) != PS_OK) { sizeof(uintptr_t)) != PS_OK) {
print_debug("can't read ld base address\n"); print_debug("can't read ld base address\n");
return false; return false;
} }
ph->core->ld_base_addr = ld_base_addr; ph->core->ld_base_addr = ld_base_addr;
print_debug("interpreter base address is 0x%lx\n", ld_base_addr); print_debug("interpreter base address is 0x%lx\n", ld_base_addr);
// now read segments from interp (i.e ld.so or ld-linux.so) // now read segments from interp (i.e ld.so or ld-linux.so or ld-elf.so)
if (read_interp_segments(ph) != true) if (read_interp_segments(ph) != true) {
return false; return false;
}
// after adding interpreter (ld.so) mappings sort again // after adding interpreter (ld.so) mappings sort again
if (sort_map_array(ph) != true) if (sort_map_array(ph) != true) {
return false; return false;
}
print_debug("first link map is at 0x%lx\n", first_link_map_addr); print_debug("first link map is at 0x%lx\n", first_link_map_addr);
...@@ -950,95 +969,102 @@ static bool read_shared_lib_info(struct ps_prochandle* ph) { ...@@ -950,95 +969,102 @@ static bool read_shared_lib_info(struct ps_prochandle* ph) {
} }
} }
// read next link_map address // read next link_map address
if (ps_pdread(ph, (psaddr_t) link_map_addr + LINK_MAP_NEXT_OFFSET, if (ps_pdread(ph, (psaddr_t) link_map_addr + LINK_MAP_NEXT_OFFSET,
&link_map_addr, sizeof(uintptr_t)) != PS_OK) { &link_map_addr, sizeof(uintptr_t)) != PS_OK) {
print_debug("can't read next link in link_map\n"); print_debug("can't read next link in link_map\n");
return false; return false;
} }
} }
return true; return true;
} }
// the one and only one exposed stuff from this file // the one and only one exposed stuff from this file
struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) { struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) {
ELF_EHDR core_ehdr; ELF_EHDR core_ehdr;
ELF_EHDR exec_ehdr; ELF_EHDR exec_ehdr;
ELF_EHDR lib_ehdr; ELF_EHDR lib_ehdr;
struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle)); struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle));
if (ph == NULL) { if (ph == NULL) {
print_debug("can't allocate ps_prochandle\n"); print_debug("can't allocate ps_prochandle\n");
return NULL; return NULL;
} }
if ((ph->core = (struct core_data*) calloc(1, sizeof(struct core_data))) == NULL) { if ((ph->core = (struct core_data*) calloc(1, sizeof(struct core_data))) == NULL) {
free(ph); free(ph);
print_debug("can't allocate ps_prochandle\n"); print_debug("can't allocate ps_prochandle\n");
return NULL; return NULL;
} }
// initialize ph // initialize ph
ph->ops = &core_ops; ph->ops = &core_ops;
ph->core->core_fd = -1; ph->core->core_fd = -1;
ph->core->exec_fd = -1; ph->core->exec_fd = -1;
ph->core->interp_fd = -1; ph->core->interp_fd = -1;
// open the core file // open the core file
if ((ph->core->core_fd = open(core_file, O_RDONLY)) < 0) { if ((ph->core->core_fd = open(core_file, O_RDONLY)) < 0) {
print_debug("can't open core file\n"); print_debug("can't open core file\n");
goto err; goto err;
} }
// read core file ELF header // read core file ELF header
if (read_elf_header(ph->core->core_fd, &core_ehdr) != true || core_ehdr.e_type != ET_CORE) { if (read_elf_header(ph->core->core_fd, &core_ehdr) != true || core_ehdr.e_type != ET_CORE) {
print_debug("core file is not a valid ELF ET_CORE file\n"); print_debug("core file is not a valid ELF ET_CORE file\n");
goto err; goto err;
} }
if ((ph->core->exec_fd = open(exec_file, O_RDONLY)) < 0) { if ((ph->core->exec_fd = open(exec_file, O_RDONLY)) < 0) {
print_debug("can't open executable file\n"); print_debug("can't open executable file\n");
goto err; goto err;
} }
if (read_elf_header(ph->core->exec_fd, &exec_ehdr) != true || exec_ehdr.e_type != ET_EXEC) { if (read_elf_header(ph->core->exec_fd, &exec_ehdr) != true || exec_ehdr.e_type != ET_EXEC) {
print_debug("executable file is not a valid ELF ET_EXEC file\n"); print_debug("executable file is not a valid ELF ET_EXEC file\n");
goto err; goto err;
} }
// process core file segments // process core file segments
if (read_core_segments(ph, &core_ehdr) != true) if (read_core_segments(ph, &core_ehdr) != true) {
goto err; goto err;
}
// process exec file segments // process exec file segments
if (read_exec_segments(ph, &exec_ehdr) != true) if (read_exec_segments(ph, &exec_ehdr) != true) {
goto err; goto err;
}
// exec file is also treated like a shared object for symbol search // exec file is also treated like a shared object for symbol search
if (add_lib_info_fd(ph, exec_file, ph->core->exec_fd, if (add_lib_info_fd(ph, exec_file, ph->core->exec_fd,
(uintptr_t)0 + find_base_address(ph->core->exec_fd, &exec_ehdr)) == NULL) (uintptr_t)0 + find_base_address(ph->core->exec_fd, &exec_ehdr)) == NULL) {
goto err; goto err;
}
// allocate and sort maps into map_array, we need to do this // allocate and sort maps into map_array, we need to do this
// here because read_shared_lib_info needs to read from debuggee // here because read_shared_lib_info needs to read from debuggee
// address space // address space
if (sort_map_array(ph) != true) if (sort_map_array(ph) != true) {
goto err; goto err;
}
if (read_shared_lib_info(ph) != true) if (read_shared_lib_info(ph) != true) {
goto err; goto err;
}
// sort again because we have added more mappings from shared objects // sort again because we have added more mappings from shared objects
if (sort_map_array(ph) != true) if (sort_map_array(ph) != true) {
goto err; goto err;
}
if (init_classsharing_workaround(ph) != true) if (init_classsharing_workaround(ph) != true) {
goto err; goto err;
}
return ph; return ph;
err: err:
Prelease(ph); Prelease(ph);
return NULL; return NULL;
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册