Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/agent/src/os/bsd/libproc_impl.c
38833 views
/*1* Copyright (c) 2003, 2013, 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}5960if (strrchr(name, '/')) {61strcpy(alt_path, alt_root);62strcat(alt_path, strrchr(name, '/'));63fd = open(alt_path, O_RDONLY);64if (fd >= 0) {65print_debug("path %s substituted for %s\n", alt_path, name);66return fd;67}68}69} else {70fd = open(name, O_RDONLY);71if (fd >= 0) {72return fd;73}74}75return -1;76}7778static bool _libsaproc_debug;7980void print_debug(const char* format,...) {81if (_libsaproc_debug) {82va_list alist;8384va_start(alist, format);85fputs("libsaproc DEBUG: ", stderr);86vfprintf(stderr, format, alist);87va_end(alist);88}89}9091void print_error(const char* format,...) {92va_list alist;93va_start(alist, format);94fputs("ERROR: ", stderr);95vfprintf(stderr, format, alist);96va_end(alist);97}9899bool is_debug() {100return _libsaproc_debug;101}102103#ifdef __APPLE__104// get arch offset in file105bool get_arch_off(int fd, cpu_type_t cputype, off_t *offset) {106struct fat_header fatheader;107struct fat_arch fatarch;108off_t img_start = 0;109110off_t pos = ltell(fd);111if (read(fd, (void *)&fatheader, sizeof(struct fat_header)) != sizeof(struct fat_header)) {112return false;113}114if (fatheader.magic == FAT_CIGAM) {115int i;116for (i = 0; i < ntohl(fatheader.nfat_arch); i++) {117if (read(fd, (void *)&fatarch, sizeof(struct fat_arch)) != sizeof(struct fat_arch)) {118return false;119}120if (ntohl(fatarch.cputype) == cputype) {121print_debug("fat offset=%x\n", ntohl(fatarch.offset));122img_start = ntohl(fatarch.offset);123break;124}125}126if (img_start == 0) {127return false;128}129}130lseek(fd, pos, SEEK_SET);131*offset = img_start;132return true;133}134135bool is_macho_file(int fd) {136mach_header_64 fhdr;137off_t x86_64_off;138139if (fd < 0) {140print_debug("Invalid file handle passed to is_macho_file\n");141return false;142}143144off_t pos = ltell(fd);145// check fat header146if (!get_arch_off(fd, CPU_TYPE_X86_64, &x86_64_off)) {147print_debug("failed to get fat header\n");148return false;149}150lseek(fd, x86_64_off, SEEK_SET);151if (read(fd, (void *)&fhdr, sizeof(mach_header_64)) != sizeof(mach_header_64)) {152return false;153}154lseek(fd, pos, SEEK_SET); // restore155print_debug("fhdr.magic %x\n", fhdr.magic);156return (fhdr.magic == MH_MAGIC_64 || fhdr.magic == MH_CIGAM_64);157}158159#endif //__APPLE__160161// initialize libproc162bool init_libproc(bool debug) {163_libsaproc_debug = debug;164#ifndef __APPLE__165// initialize the thread_db library166if (td_init() != TD_OK) {167print_debug("libthread_db's td_init failed\n");168return false;169}170#endif // __APPLE__171return true;172}173174void destroy_lib_info(struct ps_prochandle* ph) {175lib_info* lib = ph->libs;176while (lib) {177lib_info* next = lib->next;178if (lib->symtab) {179destroy_symtab(lib->symtab);180}181free(lib);182lib = next;183}184}185186void destroy_thread_info(struct ps_prochandle* ph) {187sa_thread_info* thr = ph->threads;188while (thr) {189sa_thread_info* n = thr->next;190free(thr);191thr = n;192}193}194195// ps_prochandle cleanup196void Prelease(struct ps_prochandle* ph) {197// do the "derived class" clean-up first198ph->ops->release(ph);199destroy_lib_info(ph);200destroy_thread_info(ph);201free(ph);202}203204lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t base) {205return add_lib_info_fd(ph, libname, -1, base);206}207208lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd, uintptr_t base) {209lib_info* newlib;210print_debug("add_lib_info_fd %s\n", libname);211212if ( (newlib = (lib_info*) calloc(1, sizeof(struct lib_info))) == NULL) {213print_debug("can't allocate memory for lib_info\n");214return NULL;215}216217if (strlen(libname) >= sizeof(newlib->name)) {218print_debug("libname %s too long\n", libname);219return NULL;220}221strcpy(newlib->name, libname);222223newlib->base = base;224225if (fd == -1) {226if ( (newlib->fd = pathmap_open(newlib->name)) < 0) {227print_debug("can't open shared object %s\n", newlib->name);228free(newlib);229return NULL;230}231} else {232newlib->fd = fd;233}234235#ifdef __APPLE__236// check whether we have got an Macho file.237if (is_macho_file(newlib->fd) == false) {238close(newlib->fd);239free(newlib);240print_debug("not a mach-o file\n");241return NULL;242}243#else244// check whether we have got an ELF file. /proc/<pid>/map245// gives out all file mappings and not just shared objects246if (is_elf_file(newlib->fd) == false) {247close(newlib->fd);248free(newlib);249return NULL;250}251#endif // __APPLE__252253newlib->symtab = build_symtab(newlib->fd);254if (newlib->symtab == NULL) {255print_debug("symbol table build failed for %s\n", newlib->name);256} else {257print_debug("built symbol table for %s\n", newlib->name);258}259260// even if symbol table building fails, we add the lib_info.261// This is because we may need to read from the ELF file or MachO file for core file262// address read functionality. lookup_symbol checks for NULL symtab.263if (ph->libs) {264ph->lib_tail->next = newlib;265ph->lib_tail = newlib;266} else {267ph->libs = ph->lib_tail = newlib;268}269ph->num_libs++;270return newlib;271}272273// lookup for a specific symbol274uintptr_t lookup_symbol(struct ps_prochandle* ph, const char* object_name,275const char* sym_name) {276// ignore object_name. search in all libraries277// FIXME: what should we do with object_name?? The library names are obtained278// by parsing /proc/<pid>/maps, which may not be the same as object_name.279// What we need is a utility to map object_name to real file name, something280// dlopen() does by looking at LD_LIBRARY_PATH and /etc/ld.so.cache. For281// now, we just ignore object_name and do a global search for the symbol.282283lib_info* lib = ph->libs;284while (lib) {285if (lib->symtab) {286uintptr_t res = search_symbol(lib->symtab, lib->base, sym_name, NULL);287if (res) return res;288}289lib = lib->next;290}291292print_debug("lookup failed for symbol '%s' in obj '%s'\n",293sym_name, object_name);294return (uintptr_t) NULL;295}296297const char* symbol_for_pc(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* poffset) {298const char* res = NULL;299lib_info* lib = ph->libs;300while (lib) {301if (lib->symtab && addr >= lib->base) {302res = nearest_symbol(lib->symtab, addr - lib->base, poffset);303if (res) return res;304}305lib = lib->next;306}307return NULL;308}309310// add a thread to ps_prochandle311sa_thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) {312sa_thread_info* newthr;313if ( (newthr = (sa_thread_info*) calloc(1, sizeof(sa_thread_info))) == NULL) {314print_debug("can't allocate memory for thread_info\n");315return NULL;316}317318// initialize thread info319newthr->pthread_id = pthread_id;320newthr->lwp_id = lwp_id;321322// add new thread to the list323newthr->next = ph->threads;324ph->threads = newthr;325ph->num_threads++;326return newthr;327}328329#ifndef __APPLE__330// struct used for client data from thread_db callback331struct thread_db_client_data {332struct ps_prochandle* ph;333thread_info_callback callback;334};335336// callback function for libthread_db337static int thread_db_callback(const td_thrhandle_t *th_p, void *data) {338struct thread_db_client_data* ptr = (struct thread_db_client_data*) data;339td_thrinfo_t ti;340td_err_e err;341342memset(&ti, 0, sizeof(ti));343err = td_thr_get_info(th_p, &ti);344if (err != TD_OK) {345print_debug("libthread_db : td_thr_get_info failed, can't get thread info\n");346return err;347}348349print_debug("thread_db : pthread %d (lwp %d)\n", ti.ti_tid, ti.ti_lid);350351if (ptr->callback(ptr->ph, (pthread_t)ti.ti_tid, ti.ti_lid) != true)352return TD_ERR;353354return TD_OK;355}356357// read thread_info using libthread_db358bool read_thread_info(struct ps_prochandle* ph, thread_info_callback cb) {359struct thread_db_client_data mydata;360td_thragent_t* thread_agent = NULL;361if (td_ta_new(ph, &thread_agent) != TD_OK) {362print_debug("can't create libthread_db agent\n");363return false;364}365366mydata.ph = ph;367mydata.callback = cb;368369// we use libthread_db iterator to iterate thru list of threads.370if (td_ta_thr_iter(thread_agent, thread_db_callback, &mydata,371TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,372TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS) != TD_OK) {373td_ta_delete(thread_agent);374return false;375}376377// delete thread agent378td_ta_delete(thread_agent);379return true;380}381382#endif // __APPLE__383384// get number of threads385int get_num_threads(struct ps_prochandle* ph) {386return ph->num_threads;387}388389// get lwp_id of n'th thread390lwpid_t get_lwp_id(struct ps_prochandle* ph, int index) {391int count = 0;392sa_thread_info* thr = ph->threads;393while (thr) {394if (count == index) {395return thr->lwp_id;396}397count++;398thr = thr->next;399}400return 0;401}402403#ifdef __APPLE__404// set lwp_id of n'th thread405bool set_lwp_id(struct ps_prochandle* ph, int index, lwpid_t lwpid) {406int count = 0;407sa_thread_info* thr = ph->threads;408while (thr) {409if (count == index) {410thr->lwp_id = lwpid;411return true;412}413count++;414thr = thr->next;415}416return false;417}418419// get regs of n-th thread, only used in fillThreads the first time called420bool get_nth_lwp_regs(struct ps_prochandle* ph, int index, struct reg* regs) {421int count = 0;422sa_thread_info* thr = ph->threads;423while (thr) {424if (count == index) {425break;426}427count++;428thr = thr->next;429}430if (thr != NULL) {431memcpy(regs, &thr->regs, sizeof(struct reg));432return true;433}434return false;435}436437#endif // __APPLE__438439// get regs for a given lwp440bool get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id, struct reg* regs) {441return ph->ops->get_lwp_regs(ph, lwp_id, regs);442}443444// get number of shared objects445int get_num_libs(struct ps_prochandle* ph) {446return ph->num_libs;447}448449// get name of n'th solib450const char* get_lib_name(struct ps_prochandle* ph, int index) {451int count = 0;452lib_info* lib = ph->libs;453while (lib) {454if (count == index) {455return lib->name;456}457count++;458lib = lib->next;459}460return NULL;461}462463// get base address of a lib464uintptr_t get_lib_base(struct ps_prochandle* ph, int index) {465int count = 0;466lib_info* lib = ph->libs;467while (lib) {468if (count == index) {469return lib->base;470}471count++;472lib = lib->next;473}474return (uintptr_t)NULL;475}476477bool find_lib(struct ps_prochandle* ph, const char *lib_name) {478lib_info *p = ph->libs;479while (p) {480if (strcmp(p->name, lib_name) == 0) {481return true;482}483p = p->next;484}485return false;486}487488//--------------------------------------------------------------------------489// proc service functions490491// ps_pglobal_lookup() looks up the symbol sym_name in the symbol table492// of the load object object_name in the target process identified by ph.493// It returns the symbol's value as an address in the target process in494// *sym_addr.495496ps_err_e ps_pglobal_lookup(struct ps_prochandle *ph, const char *object_name,497const char *sym_name, psaddr_t *sym_addr) {498*sym_addr = (psaddr_t) lookup_symbol(ph, object_name, sym_name);499return (*sym_addr ? PS_OK : PS_NOSYM);500}501502// read "size" bytes info "buf" from address "addr"503ps_err_e ps_pread(struct ps_prochandle *ph, psaddr_t addr,504void *buf, size_t size) {505return ph->ops->p_pread(ph, (uintptr_t) addr, buf, size)? PS_OK: PS_ERR;506}507508// write "size" bytes of data to debuggee at address "addr"509ps_err_e ps_pwrite(struct ps_prochandle *ph, psaddr_t addr,510const void *buf, size_t size) {511return ph->ops->p_pwrite(ph, (uintptr_t)addr, buf, size)? PS_OK: PS_ERR;512}513514// fill in ptrace_lwpinfo for lid515ps_err_e ps_linfo(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo) {516return ph->ops->get_lwp_info(ph, lwp_id, linfo)? PS_OK: PS_ERR;517}518519// needed for when libthread_db is compiled with TD_DEBUG defined520void521ps_plog (const char *format, ...)522{523va_list alist;524525va_start(alist, format);526vfprintf(stderr, format, alist);527va_end(alist);528}529530#ifndef __APPLE__531// ------------------------------------------------------------------------532// Functions below this point are not yet implemented. They are here only533// to make the linker happy.534535ps_err_e ps_lsetfpregs(struct ps_prochandle *ph, lwpid_t lid, const prfpregset_t *fpregs) {536print_debug("ps_lsetfpregs not implemented\n");537return PS_OK;538}539540ps_err_e ps_lsetregs(struct ps_prochandle *ph, lwpid_t lid, const prgregset_t gregset) {541print_debug("ps_lsetregs not implemented\n");542return PS_OK;543}544545ps_err_e ps_lgetfpregs(struct ps_prochandle *ph, lwpid_t lid, prfpregset_t *fpregs) {546print_debug("ps_lgetfpregs not implemented\n");547return PS_OK;548}549550ps_err_e ps_lgetregs(struct ps_prochandle *ph, lwpid_t lid, prgregset_t gregset) {551print_debug("ps_lgetfpregs not implemented\n");552return PS_OK;553}554555ps_err_e ps_lstop(struct ps_prochandle *ph, lwpid_t lid) {556print_debug("ps_lstop not implemented\n");557return PS_OK;558}559560ps_err_e ps_pcontinue(struct ps_prochandle *ph) {561print_debug("ps_pcontinue not implemented\n");562return PS_OK;563}564#endif // __APPLE__565566567