Path: blob/master/src/jdk.hotspot.agent/macosx/native/libsaproc/libproc_impl.c
41149 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/23#include "libproc_impl.h"2425static const char* alt_root = NULL;26static int alt_root_len = -1;2728#define SA_ALTROOT "SA_ALTROOT"2930off_t ltell(int fd) {31return lseek(fd, 0, SEEK_CUR);32}3334static void init_alt_root() {35if (alt_root_len == -1) {36alt_root = getenv(SA_ALTROOT);37if (alt_root) {38alt_root_len = strlen(alt_root);39} else {40alt_root_len = 0;41}42}43}4445int pathmap_open(const char* name) {46int fd;47char alt_path[PATH_MAX + 1];4849init_alt_root();5051if (alt_root_len > 0) {52strcpy(alt_path, alt_root);53strcat(alt_path, name);54fd = open(alt_path, O_RDONLY);55if (fd >= 0) {56print_debug("path %s substituted for %s\n", alt_path, name);57return fd;58} else {59print_debug("can't open %s\n", alt_path);60}6162if (strrchr(name, '/')) {63strcpy(alt_path, alt_root);64strcat(alt_path, strrchr(name, '/'));65fd = open(alt_path, O_RDONLY);66if (fd >= 0) {67print_debug("path %s substituted for %s\n", alt_path, name);68return fd;69} else {70print_debug("can't open %s\n", alt_path);71}72}73} else {74fd = open(name, O_RDONLY);75if (fd >= 0) {76return fd;77} else {78print_debug("can't open %s\n", name);79}80}81return -1;82}8384static bool _libsaproc_debug;8586void print_debug(const char* format,...) {87if (_libsaproc_debug) {88va_list alist;8990va_start(alist, format);91fputs("libsaproc DEBUG: ", stderr);92vfprintf(stderr, format, alist);93va_end(alist);94}95}9697void print_error(const char* format,...) {98va_list alist;99va_start(alist, format);100fputs("ERROR: ", stderr);101vfprintf(stderr, format, alist);102va_end(alist);103}104105bool is_debug() {106return _libsaproc_debug;107}108109#ifdef __APPLE__110// get arch offset in file111bool get_arch_off(int fd, cpu_type_t cputype, off_t *offset) {112struct fat_header fatheader;113struct fat_arch fatarch;114off_t img_start = 0;115116off_t pos = ltell(fd);117if (read(fd, (void *)&fatheader, sizeof(struct fat_header)) != sizeof(struct fat_header)) {118return false;119}120if (fatheader.magic == FAT_CIGAM) {121int i;122for (i = 0; i < ntohl(fatheader.nfat_arch); i++) {123if (read(fd, (void *)&fatarch, sizeof(struct fat_arch)) != sizeof(struct fat_arch)) {124return false;125}126if (ntohl(fatarch.cputype) == cputype) {127print_debug("fat offset=%x\n", ntohl(fatarch.offset));128img_start = ntohl(fatarch.offset);129break;130}131}132if (img_start == 0) {133return false;134}135}136lseek(fd, pos, SEEK_SET);137*offset = img_start;138return true;139}140141bool is_macho_file(int fd) {142mach_header_64 fhdr;143off_t x86_64_off;144145if (fd < 0) {146print_debug("Invalid file handle passed to is_macho_file\n");147return false;148}149150off_t pos = ltell(fd);151// check fat header152if (!get_arch_off(fd, CPU_TYPE_X86_64, &x86_64_off)) {153print_debug("failed to get fat header\n");154return false;155}156lseek(fd, x86_64_off, SEEK_SET);157if (read(fd, (void *)&fhdr, sizeof(mach_header_64)) != sizeof(mach_header_64)) {158return false;159}160lseek(fd, pos, SEEK_SET); // restore161print_debug("fhdr.magic %x\n", fhdr.magic);162return (fhdr.magic == MH_MAGIC_64 || fhdr.magic == MH_CIGAM_64);163}164165#endif //__APPLE__166167// initialize libproc168bool init_libproc(bool debug) {169_libsaproc_debug = debug;170#ifndef __APPLE__171// initialize the thread_db library172if (td_init() != TD_OK) {173print_debug("libthread_db's td_init failed\n");174return false;175}176#endif // __APPLE__177return true;178}179180void destroy_lib_info(struct ps_prochandle* ph) {181lib_info* lib = ph->libs;182while (lib) {183lib_info* next = lib->next;184if (lib->symtab) {185destroy_symtab(lib->symtab);186}187free(lib);188lib = next;189}190}191192void destroy_thread_info(struct ps_prochandle* ph) {193sa_thread_info* thr = ph->threads;194while (thr) {195sa_thread_info* n = thr->next;196free(thr);197thr = n;198}199}200201// ps_prochandle cleanup202void Prelease(struct ps_prochandle* ph) {203// do the "derived class" clean-up first204ph->ops->release(ph);205destroy_lib_info(ph);206destroy_thread_info(ph);207free(ph);208}209210lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t base) {211return add_lib_info_fd(ph, libname, -1, base);212}213214lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd, uintptr_t base) {215lib_info* newlib;216print_debug("add_lib_info_fd %s\n", libname);217218if ( (newlib = (lib_info*) calloc(1, sizeof(struct lib_info))) == NULL) {219print_debug("can't allocate memory for lib_info\n");220return NULL;221}222223if (strlen(libname) >= sizeof(newlib->name)) {224print_debug("libname %s too long\n", libname);225free(newlib);226return NULL;227}228strcpy(newlib->name, libname);229230newlib->base = base;231232if (fd == -1) {233if ( (newlib->fd = pathmap_open(newlib->name)) < 0) {234print_debug("can't open shared object %s\n", newlib->name);235free(newlib);236return NULL;237}238} else {239newlib->fd = fd;240}241242#ifdef __APPLE__243// check whether we have got an Macho file.244if (is_macho_file(newlib->fd) == false) {245close(newlib->fd);246free(newlib);247print_debug("not a mach-o file\n");248return NULL;249}250#else251// check whether we have got an ELF file. /proc/<pid>/map252// gives out all file mappings and not just shared objects253if (is_elf_file(newlib->fd) == false) {254close(newlib->fd);255free(newlib);256return NULL;257}258#endif // __APPLE__259260newlib->symtab = build_symtab(newlib->fd, &newlib->memsz);261if (newlib->symtab == NULL) {262print_debug("symbol table build failed for %s\n", newlib->name);263} else {264print_debug("built symbol table for 0x%lx memsz=0x%lx %s\n", newlib, newlib->memsz, newlib->name);265}266267// even if symbol table building fails, we add the lib_info.268// This is because we may need to read from the ELF file or MachO file for core file269// address read functionality. lookup_symbol checks for NULL symtab.270if (ph->libs) {271ph->lib_tail->next = newlib;272ph->lib_tail = newlib;273} else {274ph->libs = ph->lib_tail = newlib;275}276ph->num_libs++;277return newlib;278}279280// lookup for a specific symbol281uintptr_t lookup_symbol(struct ps_prochandle* ph, const char* object_name,282const char* sym_name) {283// ignore object_name. search in all libraries284// FIXME: what should we do with object_name?? The library names are obtained285// by parsing /proc/<pid>/maps, which may not be the same as object_name.286// What we need is a utility to map object_name to real file name, something287// dlopen() does by looking at LD_LIBRARY_PATH and /etc/ld.so.cache. For288// now, we just ignore object_name and do a global search for the symbol.289290lib_info* lib = ph->libs;291while (lib) {292if (lib->symtab) {293uintptr_t res = search_symbol(lib->symtab, lib->base, sym_name, NULL);294if (res) return res;295}296lib = lib->next;297}298299print_debug("lookup failed for symbol '%s' in obj '%s'\n",300sym_name, object_name);301return (uintptr_t) NULL;302}303304const char* symbol_for_pc(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* poffset) {305const char* res = NULL;306lib_info* lib = ph->libs;307print_debug("symbol_for_pc: addr 0x%lx\n", addr);308while (lib) {309print_debug("symbol_for_pc: checking lib 0x%lx 0x%lx %s\n", lib->base, lib->memsz, lib->name);310if (lib->symtab && addr >= lib->base && addr < lib->base + lib->memsz) {311print_debug("symbol_for_pc: address=0x%lx offset=0x%lx found inside lib base=0x%lx memsz=0x%lx %s\n",312addr, addr - lib->base, lib->base, lib->memsz, lib->name);313res = nearest_symbol(lib->symtab, addr - lib->base, poffset);314if (res) return res;315}316lib = lib->next;317}318return NULL;319}320321// add a thread to ps_prochandle322sa_thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) {323sa_thread_info* newthr;324if ( (newthr = (sa_thread_info*) calloc(1, sizeof(sa_thread_info))) == NULL) {325print_debug("can't allocate memory for thread_info\n");326return NULL;327}328329// initialize thread info330newthr->pthread_id = pthread_id;331newthr->lwp_id = lwp_id;332333// add new thread to the list334newthr->next = ph->threads;335ph->threads = newthr;336ph->num_threads++;337return newthr;338}339340#ifndef __APPLE__341// struct used for client data from thread_db callback342struct thread_db_client_data {343struct ps_prochandle* ph;344thread_info_callback callback;345};346347// callback function for libthread_db348static int thread_db_callback(const td_thrhandle_t *th_p, void *data) {349struct thread_db_client_data* ptr = (struct thread_db_client_data*) data;350td_thrinfo_t ti;351td_err_e err;352353memset(&ti, 0, sizeof(ti));354err = td_thr_get_info(th_p, &ti);355if (err != TD_OK) {356print_debug("libthread_db : td_thr_get_info failed, can't get thread info\n");357return err;358}359360print_debug("thread_db : pthread %d (lwp %d)\n", ti.ti_tid, ti.ti_lid);361362if (ptr->callback(ptr->ph, (pthread_t)ti.ti_tid, ti.ti_lid) != true)363return TD_ERR;364365return TD_OK;366}367368// read thread_info using libthread_db369bool read_thread_info(struct ps_prochandle* ph, thread_info_callback cb) {370struct thread_db_client_data mydata;371td_thragent_t* thread_agent = NULL;372if (td_ta_new(ph, &thread_agent) != TD_OK) {373print_debug("can't create libthread_db agent\n");374return false;375}376377mydata.ph = ph;378mydata.callback = cb;379380// we use libthread_db iterator to iterate thru list of threads.381if (td_ta_thr_iter(thread_agent, thread_db_callback, &mydata,382TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,383TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS) != TD_OK) {384td_ta_delete(thread_agent);385return false;386}387388// delete thread agent389td_ta_delete(thread_agent);390return true;391}392393#endif // __APPLE__394395// get number of threads396int get_num_threads(struct ps_prochandle* ph) {397return ph->num_threads;398}399400// get lwp_id of n'th thread401lwpid_t get_lwp_id(struct ps_prochandle* ph, int index) {402int count = 0;403sa_thread_info* thr = ph->threads;404while (thr) {405if (count == index) {406return thr->lwp_id;407}408count++;409thr = thr->next;410}411return 0;412}413414#ifdef __APPLE__415// set lwp_id of n'th thread416bool set_lwp_id(struct ps_prochandle* ph, int index, lwpid_t lwpid) {417int count = 0;418sa_thread_info* thr = ph->threads;419while (thr) {420if (count == index) {421thr->lwp_id = lwpid;422return true;423}424count++;425thr = thr->next;426}427return false;428}429430// get regs of n-th thread, only used in fillThreads the first time called431bool get_nth_lwp_regs(struct ps_prochandle* ph, int index, struct reg* regs) {432int count = 0;433sa_thread_info* thr = ph->threads;434while (thr) {435if (count == index) {436break;437}438count++;439thr = thr->next;440}441if (thr != NULL) {442memcpy(regs, &thr->regs, sizeof(struct reg));443return true;444}445return false;446}447448#endif // __APPLE__449450// get regs for a given lwp451bool get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id, struct reg* regs) {452return ph->ops->get_lwp_regs(ph, lwp_id, regs);453}454455// get number of shared objects456int get_num_libs(struct ps_prochandle* ph) {457return ph->num_libs;458}459460// get name of n'th solib461const char* get_lib_name(struct ps_prochandle* ph, int index) {462int count = 0;463lib_info* lib = ph->libs;464while (lib) {465if (count == index) {466return lib->name;467}468count++;469lib = lib->next;470}471return NULL;472}473474// get base address of a lib475uintptr_t get_lib_base(struct ps_prochandle* ph, int index) {476int count = 0;477lib_info* lib = ph->libs;478while (lib) {479if (count == index) {480return lib->base;481}482count++;483lib = lib->next;484}485return (uintptr_t)NULL;486}487488// get address range of lib489void get_lib_addr_range(struct ps_prochandle* ph, int index, uintptr_t* base, uintptr_t* memsz) {490int count = 0;491lib_info* lib = ph->libs;492while (lib) {493if (count == index) {494*base = lib->base;495*memsz = lib->memsz;496return;497}498count++;499lib = lib->next;500}501}502503bool find_lib(struct ps_prochandle* ph, const char *lib_name) {504lib_info *p = ph->libs;505while (p) {506if (strcmp(p->name, lib_name) == 0) {507return true;508}509p = p->next;510}511return false;512}513514//--------------------------------------------------------------------------515// proc service functions516517// ps_pglobal_lookup() looks up the symbol sym_name in the symbol table518// of the load object object_name in the target process identified by ph.519// It returns the symbol's value as an address in the target process in520// *sym_addr.521522ps_err_e ps_pglobal_lookup(struct ps_prochandle *ph, const char *object_name,523const char *sym_name, psaddr_t *sym_addr) {524*sym_addr = (psaddr_t) lookup_symbol(ph, object_name, sym_name);525return (*sym_addr ? PS_OK : PS_NOSYM);526}527528// read "size" bytes info "buf" from address "addr"529ps_err_e ps_pread(struct ps_prochandle *ph, psaddr_t addr,530void *buf, size_t size) {531return ph->ops->p_pread(ph, (uintptr_t) addr, buf, size)? PS_OK: PS_ERR;532}533534// write "size" bytes of data to debuggee at address "addr"535ps_err_e ps_pwrite(struct ps_prochandle *ph, psaddr_t addr,536const void *buf, size_t size) {537return ph->ops->p_pwrite(ph, (uintptr_t)addr, buf, size)? PS_OK: PS_ERR;538}539540// fill in ptrace_lwpinfo for lid541ps_err_e ps_linfo(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo) {542return ph->ops->get_lwp_info(ph, lwp_id, linfo)? PS_OK: PS_ERR;543}544545// needed for when libthread_db is compiled with TD_DEBUG defined546void547ps_plog (const char *format, ...)548{549va_list alist;550551va_start(alist, format);552vfprintf(stderr, format, alist);553va_end(alist);554}555556#ifndef __APPLE__557// ------------------------------------------------------------------------558// Functions below this point are not yet implemented. They are here only559// to make the linker happy.560561ps_err_e ps_lsetfpregs(struct ps_prochandle *ph, lwpid_t lid, const prfpregset_t *fpregs) {562print_debug("ps_lsetfpregs not implemented\n");563return PS_OK;564}565566ps_err_e ps_lsetregs(struct ps_prochandle *ph, lwpid_t lid, const prgregset_t gregset) {567print_debug("ps_lsetregs not implemented\n");568return PS_OK;569}570571ps_err_e ps_lgetfpregs(struct ps_prochandle *ph, lwpid_t lid, prfpregset_t *fpregs) {572print_debug("ps_lgetfpregs not implemented\n");573return PS_OK;574}575576ps_err_e ps_lgetregs(struct ps_prochandle *ph, lwpid_t lid, prgregset_t gregset) {577print_debug("ps_lgetfpregs not implemented\n");578return PS_OK;579}580581ps_err_e ps_lstop(struct ps_prochandle *ph, lwpid_t lid) {582print_debug("ps_lstop not implemented\n");583return PS_OK;584}585586ps_err_e ps_pcontinue(struct ps_prochandle *ph) {587print_debug("ps_pcontinue not implemented\n");588return PS_OK;589}590#endif // __APPLE__591592593