1.1 --- a/agent/src/os/bsd/libproc_impl.h Wed Mar 13 17:34:29 2013 -0400 1.2 +++ b/agent/src/os/bsd/libproc_impl.h Thu Mar 14 00:33:08 2013 -0700 1.3 @@ -30,6 +30,60 @@ 1.4 #include "libproc.h" 1.5 #include "symtab.h" 1.6 1.7 +#ifdef __APPLE__ 1.8 +#include <inttypes.h> // for PRIx64, 32, ... 1.9 +#include <pthread.h> 1.10 +#include <mach-o/loader.h> 1.11 +#include <mach-o/nlist.h> 1.12 +#include <mach-o/fat.h> 1.13 + 1.14 +#ifndef register_t 1.15 +#define register_t uint64_t 1.16 +#endif 1.17 + 1.18 +/*** registers copied from bsd/amd64 */ 1.19 +typedef struct reg { 1.20 + register_t r_r15; 1.21 + register_t r_r14; 1.22 + register_t r_r13; 1.23 + register_t r_r12; 1.24 + register_t r_r11; 1.25 + register_t r_r10; 1.26 + register_t r_r9; 1.27 + register_t r_r8; 1.28 + register_t r_rdi; 1.29 + register_t r_rsi; 1.30 + register_t r_rbp; 1.31 + register_t r_rbx; 1.32 + register_t r_rdx; 1.33 + register_t r_rcx; 1.34 + register_t r_rax; 1.35 + uint32_t r_trapno; // not used 1.36 + uint16_t r_fs; 1.37 + uint16_t r_gs; 1.38 + uint32_t r_err; // not used 1.39 + uint16_t r_es; // not used 1.40 + uint16_t r_ds; // not used 1.41 + register_t r_rip; 1.42 + register_t r_cs; 1.43 + register_t r_rflags; 1.44 + register_t r_rsp; 1.45 + register_t r_ss; // not used 1.46 +} reg; 1.47 + 1.48 +// convenient defs 1.49 +typedef struct mach_header_64 mach_header_64; 1.50 +typedef struct load_command load_command; 1.51 +typedef struct segment_command_64 segment_command_64; 1.52 +typedef struct thread_command thread_command; 1.53 +typedef struct dylib_command dylib_command; 1.54 +typedef struct symtab_command symtab_command; 1.55 +typedef struct nlist_64 nlist_64; 1.56 +#else 1.57 +#include <thread_db.h> 1.58 +#include "salibelf.h" 1.59 +#endif // __APPLE__ 1.60 + 1.61 // data structures in this file mimic those of Solaris 8.0 - libproc's Pcontrol.h 1.62 1.63 #define BUF_SIZE (PATH_MAX + NAME_MAX + 1) 1.64 @@ -44,12 +98,12 @@ 1.65 } lib_info; 1.66 1.67 // list of threads 1.68 -typedef struct thread_info { 1.69 - lwpid_t lwp_id; 1.70 - pthread_t pthread_id; // not used cores, always -1 1.71 +typedef struct sa_thread_info { 1.72 + lwpid_t lwp_id; // same as pthread_t 1.73 + pthread_t pthread_id; // 1.74 struct reg regs; // not for process, core uses for caching regset 1.75 - struct thread_info* next; 1.76 -} thread_info; 1.77 + struct sa_thread_info* next; 1.78 +} sa_thread_info; 1.79 1.80 // list of virtual memory maps 1.81 typedef struct map_info { 1.82 @@ -91,6 +145,7 @@ 1.83 // part of the class sharing workaround 1.84 map_info* class_share_maps;// class share maps in a linked list 1.85 map_info** map_array; // sorted (by vaddr) array of map_info pointers 1.86 + char exec_path[4096]; // file name java 1.87 }; 1.88 1.89 struct ps_prochandle { 1.90 @@ -100,12 +155,11 @@ 1.91 lib_info* libs; // head of lib list 1.92 lib_info* lib_tail; // tail of lib list - to append at the end 1.93 int num_threads; 1.94 - thread_info* threads; // head of thread list 1.95 + sa_thread_info* threads; // head of thread list 1.96 struct core_data* core; // data only used for core dumps, NULL for process 1.97 }; 1.98 1.99 int pathmap_open(const char* name); 1.100 - 1.101 void print_debug(const char* format,...); 1.102 void print_error(const char* format,...); 1.103 bool is_debug(); 1.104 @@ -122,10 +176,45 @@ 1.105 lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd, 1.106 uintptr_t base); 1.107 1.108 -// adds a new thread to threads list, returns NULL on failure 1.109 -thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id); 1.110 +sa_thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id); 1.111 +// a test for ELF signature without using libelf 1.112 1.113 -// a test for ELF signature without using libelf 1.114 +#ifdef __APPLE__ 1.115 +// a test for Mach-O signature 1.116 +bool is_macho_file(int fd); 1.117 +// skip fat head to get image start offset of cpu_type_t 1.118 +// return false if any error happens, else value in offset. 1.119 +bool get_arch_off(int fd, cpu_type_t cputype, off_t *offset); 1.120 +#else 1.121 bool is_elf_file(int fd); 1.122 +#endif // __APPLE__ 1.123 1.124 +lwpid_t get_lwp_id(struct ps_prochandle* ph, int index); 1.125 +bool set_lwp_id(struct ps_prochandle* ph, int index, lwpid_t lwpid); 1.126 +bool get_nth_lwp_regs(struct ps_prochandle* ph, int index, struct reg* regs); 1.127 + 1.128 +// ps_pglobal_lookup() looks up the symbol sym_name in the symbol table 1.129 +// of the load object object_name in the target process identified by ph. 1.130 +// It returns the symbol's value as an address in the target process in 1.131 +// *sym_addr. 1.132 + 1.133 +ps_err_e ps_pglobal_lookup(struct ps_prochandle *ph, const char *object_name, 1.134 + const char *sym_name, psaddr_t *sym_addr); 1.135 + 1.136 +// read "size" bytes info "buf" from address "addr" 1.137 +ps_err_e ps_pread(struct ps_prochandle *ph, psaddr_t addr, 1.138 + void *buf, size_t size); 1.139 + 1.140 +// write "size" bytes of data to debuggee at address "addr" 1.141 +ps_err_e ps_pwrite(struct ps_prochandle *ph, psaddr_t addr, 1.142 + const void *buf, size_t size); 1.143 + 1.144 +// fill in ptrace_lwpinfo for lid 1.145 +ps_err_e ps_linfo(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo); 1.146 + 1.147 +// needed for when libthread_db is compiled with TD_DEBUG defined 1.148 +void ps_plog (const char *format, ...); 1.149 + 1.150 +// untility, tells the position in file 1.151 +off_t ltell(int fd); 1.152 #endif //_LIBPROC_IMPL_H_