agent/src/os/bsd/libproc_impl.c
Index
Unified diffs
Context diffs
Sdiffs
Wdiffs
Patch
New
Old
Previous File
Next File
hotspot-8003348 Cdiff agent/src/os/bsd/libproc_impl.c
agent/src/os/bsd/libproc_impl.c
Print this page
8003348: SA can not read core file on OS X
*** 19,41 ****
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
- #include <stdarg.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
- #include <thread_db.h>
#include "libproc_impl.h"
static const char* alt_root = NULL;
static int alt_root_len = -1;
#define SA_ALTROOT "SA_ALTROOT"
static void init_alt_root() {
if (alt_root_len == -1) {
alt_root = getenv(SA_ALTROOT);
if (alt_root) {
alt_root_len = strlen(alt_root);
--- 19,39 ----
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "libproc_impl.h"
static const char* alt_root = NULL;
static int alt_root_len = -1;
#define SA_ALTROOT "SA_ALTROOT"
+ off_t ltell(int fd) {
+ return lseek(fd, 0, SEEK_CUR);
+ }
+
static void init_alt_root() {
if (alt_root_len == -1) {
alt_root = getenv(SA_ALTROOT);
if (alt_root) {
alt_root_len = strlen(alt_root);
*** 48,61 ****
int pathmap_open(const char* name) {
int fd;
char alt_path[PATH_MAX + 1];
init_alt_root();
- fd = open(name, O_RDONLY);
- if (fd >= 0) {
- return fd;
- }
if (alt_root_len > 0) {
strcpy(alt_path, alt_root);
strcat(alt_path, name);
fd = open(alt_path, O_RDONLY);
--- 46,55 ----
*** 71,82 ****
if (fd >= 0) {
print_debug("path %s substituted for %s\n", alt_path, name);
return fd;
}
}
}
!
return -1;
}
static bool _libsaproc_debug;
--- 65,80 ----
if (fd >= 0) {
print_debug("path %s substituted for %s\n", alt_path, name);
return fd;
}
}
+ } else {
+ fd = open(name, O_RDONLY);
+ if (fd >= 0) {
+ return fd;
}
! }
return -1;
}
static bool _libsaproc_debug;
*** 101,148 ****
bool is_debug() {
return _libsaproc_debug;
}
// initialize libproc
bool init_libproc(bool debug) {
- // init debug mode
_libsaproc_debug = debug;
!
// initialize the thread_db library
if (td_init() != TD_OK) {
print_debug("libthread_db's td_init failed\n");
return false;
}
!
return true;
}
! static void destroy_lib_info(struct ps_prochandle* ph) {
lib_info* lib = ph->libs;
while (lib) {
! lib_info *next = lib->next;
if (lib->symtab) {
destroy_symtab(lib->symtab);
}
free(lib);
lib = next;
}
}
! static void destroy_thread_info(struct ps_prochandle* ph) {
! thread_info* thr = ph->threads;
while (thr) {
! thread_info *next = thr->next;
free(thr);
! thr = next;
}
}
// ps_prochandle cleanup
-
- // ps_prochandle cleanup
void Prelease(struct ps_prochandle* ph) {
// do the "derived class" clean-up first
ph->ops->release(ph);
destroy_lib_info(ph);
destroy_thread_info(ph);
--- 99,201 ----
bool is_debug() {
return _libsaproc_debug;
}
+ #ifdef __APPLE__
+ // get arch offset in file
+ bool get_arch_off(int fd, cpu_type_t cputype, off_t *offset) {
+ struct fat_header fatheader;
+ struct fat_arch fatarch;
+ off_t img_start = 0;
+
+ off_t pos = ltell(fd);
+ if (read(fd, (void *)&fatheader, sizeof(struct fat_header)) != sizeof(struct fat_header)) {
+ return false;
+ }
+ if (fatheader.magic == FAT_CIGAM) {
+ int i;
+ for (i = 0; i < ntohl(fatheader.nfat_arch); i++) {
+ if (read(fd, (void *)&fatarch, sizeof(struct fat_arch)) != sizeof(struct fat_arch)) {
+ return false;
+ }
+ if (ntohl(fatarch.cputype) == cputype) {
+ print_debug("fat offset=%x\n", ntohl(fatarch.offset));
+ img_start = ntohl(fatarch.offset);
+ break;
+ }
+ }
+ if (img_start == 0) {
+ return false;
+ }
+ }
+ lseek(fd, pos, SEEK_SET);
+ *offset = img_start;
+ return true;
+ }
+
+ bool is_macho_file(int fd) {
+ mach_header_64 fhdr;
+ off_t x86_64_off;
+
+ if (fd < 0) {
+ print_debug("Invalid file handle passed to is_macho_file\n");
+ return false;
+ }
+
+ off_t pos = ltell(fd);
+ // check fat header
+ if (!get_arch_off(fd, CPU_TYPE_X86_64, &x86_64_off)) {
+ print_debug("failed to get fat header\n");
+ return false;
+ }
+ lseek(fd, x86_64_off, SEEK_SET);
+ if (read(fd, (void *)&fhdr, sizeof(mach_header_64)) != sizeof(mach_header_64)) {
+ return false;
+ }
+ lseek(fd, pos, SEEK_SET); // restore
+ print_debug("fhdr.magic %x\n", fhdr.magic);
+ return (fhdr.magic == MH_MAGIC_64 || fhdr.magic == MH_CIGAM_64);
+ }
+
+ #endif //__APPLE__
+
// initialize libproc
bool init_libproc(bool debug) {
_libsaproc_debug = debug;
! #ifndef __APPLE__
// initialize the thread_db library
if (td_init() != TD_OK) {
print_debug("libthread_db's td_init failed\n");
return false;
}
! #endif // __APPLE__
return true;
}
! void destroy_lib_info(struct ps_prochandle* ph) {
lib_info* lib = ph->libs;
while (lib) {
! lib_info* next = lib->next;
if (lib->symtab) {
destroy_symtab(lib->symtab);
}
free(lib);
lib = next;
}
}
! void destroy_thread_info(struct ps_prochandle* ph) {
! sa_thread_info* thr = ph->threads;
while (thr) {
! sa_thread_info* n = thr->next;
free(thr);
! thr = n;
}
}
// ps_prochandle cleanup
void Prelease(struct ps_prochandle* ph) {
// do the "derived class" clean-up first
ph->ops->release(ph);
destroy_lib_info(ph);
destroy_thread_info(ph);
*** 153,162 ****
--- 206,216 ----
return add_lib_info_fd(ph, libname, -1, base);
}
lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd, uintptr_t base) {
lib_info* newlib;
+ print_debug("add_lib_info_fd %s\n", libname);
if ( (newlib = (lib_info*) calloc(1, sizeof(struct lib_info))) == NULL) {
print_debug("can't allocate memory for lib_info\n");
return NULL;
}
*** 172,208 ****
}
} else {
newlib->fd = fd;
}
// check whether we have got an ELF file. /proc/<pid>/map
// gives out all file mappings and not just shared objects
if (is_elf_file(newlib->fd) == false) {
close(newlib->fd);
free(newlib);
return NULL;
}
newlib->symtab = build_symtab(newlib->fd);
if (newlib->symtab == NULL) {
print_debug("symbol table build failed for %s\n", newlib->name);
! }
! else {
print_debug("built symbol table for %s\n", newlib->name);
}
// even if symbol table building fails, we add the lib_info.
! // This is because we may need to read from the ELF file for core file
// address read functionality. lookup_symbol checks for NULL symtab.
if (ph->libs) {
ph->lib_tail->next = newlib;
ph->lib_tail = newlib;
} else {
ph->libs = ph->lib_tail = newlib;
}
ph->num_libs++;
-
return newlib;
}
// lookup for a specific symbol
uintptr_t lookup_symbol(struct ps_prochandle* ph, const char* object_name,
--- 226,270 ----
}
} else {
newlib->fd = fd;
}
+ #ifdef __APPLE__
+ // check whether we have got an Macho file.
+ if (is_macho_file(newlib->fd) == false) {
+ close(newlib->fd);
+ free(newlib);
+ print_debug("not a mach-o file\n");
+ return NULL;
+ }
+ #else
// check whether we have got an ELF file. /proc/<pid>/map
// gives out all file mappings and not just shared objects
if (is_elf_file(newlib->fd) == false) {
close(newlib->fd);
free(newlib);
return NULL;
}
+ #endif // __APPLE__
newlib->symtab = build_symtab(newlib->fd);
if (newlib->symtab == NULL) {
print_debug("symbol table build failed for %s\n", newlib->name);
! } else {
print_debug("built symbol table for %s\n", newlib->name);
}
// even if symbol table building fails, we add the lib_info.
! // This is because we may need to read from the ELF file or MachO file for core file
// address read functionality. lookup_symbol checks for NULL symtab.
if (ph->libs) {
ph->lib_tail->next = newlib;
ph->lib_tail = newlib;
} else {
ph->libs = ph->lib_tail = newlib;
}
ph->num_libs++;
return newlib;
}
// lookup for a specific symbol
uintptr_t lookup_symbol(struct ps_prochandle* ph, const char* object_name,
*** 226,236 ****
print_debug("lookup failed for symbol '%s' in obj '%s'\n",
sym_name, object_name);
return (uintptr_t) NULL;
}
-
const char* symbol_for_pc(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* poffset) {
const char* res = NULL;
lib_info* lib = ph->libs;
while (lib) {
if (lib->symtab && addr >= lib->base) {
--- 288,297 ----
*** 241,253 ****
}
return NULL;
}
// add a thread to ps_prochandle
! thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) {
! thread_info* newthr;
! if ( (newthr = (thread_info*) calloc(1, sizeof(thread_info))) == NULL) {
print_debug("can't allocate memory for thread_info\n");
return NULL;
}
// initialize thread info
--- 302,314 ----
}
return NULL;
}
// add a thread to ps_prochandle
! sa_thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) {
! sa_thread_info* newthr;
! if ( (newthr = (sa_thread_info*) calloc(1, sizeof(sa_thread_info))) == NULL) {
print_debug("can't allocate memory for thread_info\n");
return NULL;
}
// initialize thread info
*** 259,269 ****
ph->threads = newthr;
ph->num_threads++;
return newthr;
}
!
// struct used for client data from thread_db callback
struct thread_db_client_data {
struct ps_prochandle* ph;
thread_info_callback callback;
};
--- 320,330 ----
ph->threads = newthr;
ph->num_threads++;
return newthr;
}
! #ifndef __APPLE__
// struct used for client data from thread_db callback
struct thread_db_client_data {
struct ps_prochandle* ph;
thread_info_callback callback;
};
*** 312,341 ****
// delete thread agent
td_ta_delete(thread_agent);
return true;
}
// get number of threads
int get_num_threads(struct ps_prochandle* ph) {
return ph->num_threads;
}
// get lwp_id of n'th thread
lwpid_t get_lwp_id(struct ps_prochandle* ph, int index) {
int count = 0;
! thread_info* thr = ph->threads;
while (thr) {
if (count == index) {
return thr->lwp_id;
}
count++;
thr = thr->next;
}
! return -1;
}
// get regs for a given lwp
bool get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id, struct reg* regs) {
return ph->ops->get_lwp_regs(ph, lwp_id, regs);
}
--- 373,439 ----
// delete thread agent
td_ta_delete(thread_agent);
return true;
}
+ #endif // __APPLE__
// get number of threads
int get_num_threads(struct ps_prochandle* ph) {
return ph->num_threads;
}
// get lwp_id of n'th thread
lwpid_t get_lwp_id(struct ps_prochandle* ph, int index) {
int count = 0;
! sa_thread_info* thr = ph->threads;
while (thr) {
if (count == index) {
return thr->lwp_id;
}
count++;
thr = thr->next;
}
! return 0;
}
+ #ifdef __APPLE__
+ // set lwp_id of n'th thread
+ bool set_lwp_id(struct ps_prochandle* ph, int index, lwpid_t lwpid) {
+ int count = 0;
+ sa_thread_info* thr = ph->threads;
+ while (thr) {
+ if (count == index) {
+ thr->lwp_id = lwpid;
+ return true;
+ }
+ count++;
+ thr = thr->next;
+ }
+ return false;
+ }
+
+ // get regs of n-th thread, only used in fillThreads the first time called
+ bool get_nth_lwp_regs(struct ps_prochandle* ph, int index, struct reg* regs) {
+ int count = 0;
+ sa_thread_info* thr = ph->threads;
+ while (thr) {
+ if (count == index) {
+ break;
+ }
+ count++;
+ thr = thr->next;
+ }
+ if (thr != NULL) {
+ memcpy(regs, &thr->regs, sizeof(struct reg));
+ return true;
+ }
+ return false;
+ }
+
+ #endif // __APPLE__
+
// get regs for a given lwp
bool get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id, struct reg* regs) {
return ph->ops->get_lwp_regs(ph, lwp_id, regs);
}
*** 423,432 ****
--- 521,531 ----
va_start(alist, format);
vfprintf(stderr, format, alist);
va_end(alist);
}
+ #ifndef __APPLE__
// ------------------------------------------------------------------------
// Functions below this point are not yet implemented. They are here only
// to make the linker happy.
ps_err_e ps_lsetfpregs(struct ps_prochandle *ph, lwpid_t lid, const prfpregset_t *fpregs) {
*** 456,460 ****
--- 555,560 ----
ps_err_e ps_pcontinue(struct ps_prochandle *ph) {
print_debug("ps_pcontinue not implemented\n");
return PS_OK;
}
+ #endif // __APPLE__
agent/src/os/bsd/libproc_impl.c
Index
Unified diffs
Context diffs
Sdiffs
Wdiffs
Patch
New
Old
Previous File
Next File