1 /* 2 * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef _LIBPROC_IMPL_H_ 26 #define _LIBPROC_IMPL_H_ 27 28 #include <unistd.h> 29 #include <limits.h> 30 #include "libproc.h" 31 #include "symtab.h" 32 33 #ifdef __APPLE__ 34 #include <inttypes.h> // for PRIx64, 32, ... 35 #include <pthread.h> 36 #include <mach-o/loader.h> 37 #include <mach-o/nlist.h> 38 #include <mach-o/fat.h> 39 #include <mach-o/stab.h> 40 41 #ifndef register_t 42 #define register_t uint64_t 43 #endif 44 45 /*** registers copied from bsd/amd64 */ 46 typedef struct reg { 47 register_t r_r15; 48 register_t r_r14; 49 register_t r_r13; 50 register_t r_r12; 51 register_t r_r11; 52 register_t r_r10; 53 register_t r_r9; 54 register_t r_r8; 55 register_t r_rdi; 56 register_t r_rsi; 57 register_t r_rbp; 58 register_t r_rbx; 59 register_t r_rdx; 60 register_t r_rcx; 61 register_t r_rax; 62 uint32_t r_trapno; // not used 63 uint16_t r_fs; 64 uint16_t r_gs; 65 uint32_t r_err; // not used 66 uint16_t r_es; // not used 67 uint16_t r_ds; // not used 68 register_t r_rip; 69 register_t r_cs; 70 register_t r_rflags; 71 register_t r_rsp; 72 register_t r_ss; // not used 73 } reg; 74 75 // convenient defs 76 typedef struct mach_header_64 mach_header_64; 77 typedef struct load_command load_command; 78 typedef struct segment_command_64 segment_command_64; 79 typedef struct thread_command thread_command; 80 typedef struct dylib_command dylib_command; 81 typedef struct symtab_command symtab_command; 82 typedef struct nlist_64 nlist_64; 83 #else 84 #include <thread_db.h> 85 #include "salibelf.h" 86 #endif // __APPLE__ 87 88 // data structures in this file mimic those of Solaris 8.0 - libproc's Pcontrol.h 89 90 #define BUF_SIZE (PATH_MAX + NAME_MAX + 1) 91 92 // list of shared objects 93 typedef struct lib_info { 94 char name[BUF_SIZE]; 95 uintptr_t base; 96 struct symtab* symtab; 97 int fd; // file descriptor for lib 98 struct lib_info* next; 99 size_t memsz; 100 } lib_info; 101 102 // list of threads 103 typedef struct sa_thread_info { 104 lwpid_t lwp_id; // same as pthread_t 105 pthread_t pthread_id; // 106 struct reg regs; // not for process, core uses for caching regset 107 struct sa_thread_info* next; 108 } sa_thread_info; 109 110 // list of virtual memory maps 111 typedef struct map_info { 112 int fd; // file descriptor 113 uint64_t offset; // file offset of this mapping 114 uint64_t vaddr; // starting virtual address 115 size_t memsz; // size of the mapping 116 uint32_t flags; // access flags 117 struct map_info* next; 118 } map_info; 119 120 // vtable for ps_prochandle 121 typedef struct ps_prochandle_ops { 122 // "derived class" clean-up 123 void (*release)(struct ps_prochandle* ph); 124 // read from debuggee 125 bool (*p_pread)(struct ps_prochandle *ph, 126 uintptr_t addr, char *buf, size_t size); 127 // write into debuggee 128 bool (*p_pwrite)(struct ps_prochandle *ph, 129 uintptr_t addr, const char *buf , size_t size); 130 // get integer regset of a thread 131 bool (*get_lwp_regs)(struct ps_prochandle* ph, lwpid_t lwp_id, struct reg* regs); 132 // get info on thread 133 bool (*get_lwp_info)(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo); 134 } ps_prochandle_ops; 135 136 // the ps_prochandle 137 138 struct core_data { 139 int core_fd; // file descriptor of core file 140 int exec_fd; // file descriptor of exec file 141 int interp_fd; // file descriptor of interpreter (ld-elf.so.1) 142 // part of the class sharing workaround 143 int classes_jsa_fd; // file descriptor of class share archive 144 uintptr_t dynamic_addr; // address of dynamic section of a.out 145 uintptr_t ld_base_addr; // base address of ld.so 146 size_t num_maps; // number of maps. 147 map_info* maps; // maps in a linked list 148 // part of the class sharing workaround 149 map_info* class_share_maps;// class share maps in a linked list 150 map_info** map_array; // sorted (by vaddr) array of map_info pointers 151 char exec_path[4096]; // file name java 152 }; 153 154 struct ps_prochandle { 155 ps_prochandle_ops* ops; // vtable ptr 156 pid_t pid; 157 int num_libs; 158 lib_info* libs; // head of lib list 159 lib_info* lib_tail; // tail of lib list - to append at the end 160 int num_threads; 161 sa_thread_info* threads; // head of thread list 162 struct core_data* core; // data only used for core dumps, NULL for process 163 }; 164 165 int pathmap_open(const char* name); 166 void print_debug(const char* format,...); 167 void print_error(const char* format,...); 168 bool is_debug(); 169 170 typedef bool (*thread_info_callback)(struct ps_prochandle* ph, pthread_t pid, lwpid_t lwpid); 171 172 // reads thread info using libthread_db and calls above callback for each thread 173 bool read_thread_info(struct ps_prochandle* ph, thread_info_callback cb); 174 175 // adds a new shared object to lib list, returns NULL on failure 176 lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t base); 177 178 // adds a new shared object to lib list, supply open lib file descriptor as well 179 lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd, uintptr_t base); 180 181 sa_thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id); 182 // a test for ELF signature without using libelf 183 184 #ifdef __APPLE__ 185 // a test for Mach-O signature 186 bool is_macho_file(int fd); 187 // skip fat head to get image start offset of cpu_type_t 188 // return false if any error happens, else value in offset. 189 bool get_arch_off(int fd, cpu_type_t cputype, off_t *offset); 190 #else 191 bool is_elf_file(int fd); 192 #endif // __APPLE__ 193 194 lwpid_t get_lwp_id(struct ps_prochandle* ph, int index); 195 bool set_lwp_id(struct ps_prochandle* ph, int index, lwpid_t lwpid); 196 bool get_nth_lwp_regs(struct ps_prochandle* ph, int index, struct reg* regs); 197 198 // ps_pglobal_lookup() looks up the symbol sym_name in the symbol table 199 // of the load object object_name in the target process identified by ph. 200 // It returns the symbol's value as an address in the target process in 201 // *sym_addr. 202 203 ps_err_e ps_pglobal_lookup(struct ps_prochandle *ph, const char *object_name, 204 const char *sym_name, psaddr_t *sym_addr); 205 206 // read "size" bytes info "buf" from address "addr" 207 ps_err_e ps_pread(struct ps_prochandle *ph, psaddr_t addr, 208 void *buf, size_t size); 209 210 // write "size" bytes of data to debuggee at address "addr" 211 ps_err_e ps_pwrite(struct ps_prochandle *ph, psaddr_t addr, 212 const void *buf, size_t size); 213 214 // fill in ptrace_lwpinfo for lid 215 ps_err_e ps_linfo(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo); 216 217 // needed for when libthread_db is compiled with TD_DEBUG defined 218 void ps_plog (const char *format, ...); 219 220 // untility, tells the position in file 221 off_t ltell(int fd); 222 #endif //_LIBPROC_IMPL_H_