Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/agent/src/os/linux/libproc_impl.c
38833 views
/*1* Copyright (c) 2003, 2019, 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 <stdarg.h>24#include <stdio.h>25#include <stdlib.h>26#include <string.h>27#include <fcntl.h>28#ifndef __ANDROID__29#include <thread_db.h>30#else31#include "thread_db.h"32#endif33#include "libproc_impl.h"3435#define SA_ALTROOT "SA_ALTROOT"3637int pathmap_open(const char* name) {38static const char *alt_root = NULL;39static int alt_root_initialized = 0;4041int fd;42char alt_path[PATH_MAX + 1], *alt_path_end;43const char *s;4445if (!alt_root_initialized) {46alt_root_initialized = -1;47alt_root = getenv(SA_ALTROOT);48}4950if (alt_root == NULL) {51return open(name, O_RDONLY);52}5354strcpy(alt_path, alt_root);55alt_path_end = alt_path + strlen(alt_path);5657// Strip path items one by one and try to open file with alt_root prepended58s = name;59while (1) {60strcat(alt_path, s);61s += 1;6263fd = open(alt_path, O_RDONLY);64if (fd >= 0) {65print_debug("path %s substituted for %s\n", alt_path, name);66return fd;67}6869// Linker always put full path to solib to process, so we can rely70// on presence of /. If slash is not present, it means, that SOlib doesn't71// physically exist (e.g. linux-gate.so) and we fail opening it anyway72if ((s = strchr(s, '/')) == NULL) {73break;74}7576*alt_path_end = 0;77}7879return -1;80}8182static bool _libsaproc_debug;8384void print_debug(const char* format,...) {85if (_libsaproc_debug) {86va_list alist;8788va_start(alist, format);89fputs("libsaproc DEBUG: ", stderr);90vfprintf(stderr, format, alist);91va_end(alist);92}93}9495void print_error(const char* format,...) {96va_list alist;97va_start(alist, format);98fputs("ERROR: ", stderr);99vfprintf(stderr, format, alist);100va_end(alist);101}102103bool is_debug() {104return _libsaproc_debug;105}106107// initialize libproc108bool init_libproc(bool debug) {109// init debug mode110_libsaproc_debug = debug;111112// initialize the thread_db library113if (td_init() != TD_OK) {114print_debug("libthread_db's td_init failed\n");115return false;116}117118return true;119}120121static void destroy_lib_info(struct ps_prochandle* ph) {122lib_info* lib = ph->libs;123while (lib) {124lib_info *next = lib->next;125if (lib->symtab) {126destroy_symtab(lib->symtab);127}128free(lib);129lib = next;130}131}132133static void destroy_thread_info(struct ps_prochandle* ph) {134thread_info* thr = ph->threads;135while (thr) {136thread_info *next = thr->next;137free(thr);138thr = next;139}140}141142// ps_prochandle cleanup143144// ps_prochandle cleanup145void Prelease(struct ps_prochandle* ph) {146// do the "derived class" clean-up first147ph->ops->release(ph);148destroy_lib_info(ph);149destroy_thread_info(ph);150free(ph);151}152153lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t base) {154return add_lib_info_fd(ph, libname, -1, base);155}156157lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd, uintptr_t base) {158lib_info* newlib;159160if ( (newlib = (lib_info*) calloc(1, sizeof(struct lib_info))) == NULL) {161print_debug("can't allocate memory for lib_info\n");162return NULL;163}164165if (strlen(libname) >= sizeof(newlib->name)) {166print_debug("libname %s too long\n", libname);167return NULL;168}169strcpy(newlib->name, libname);170171newlib->base = base;172173if (fd == -1) {174if ( (newlib->fd = pathmap_open(newlib->name)) < 0) {175print_debug("can't open shared object %s\n", newlib->name);176free(newlib);177return NULL;178}179} else {180newlib->fd = fd;181}182183// check whether we have got an ELF file. /proc/<pid>/map184// gives out all file mappings and not just shared objects185if (is_elf_file(newlib->fd) == false) {186close(newlib->fd);187free(newlib);188return NULL;189}190191newlib->symtab = build_symtab(newlib->fd, libname);192if (newlib->symtab == NULL) {193print_debug("symbol table build failed for %s\n", newlib->name);194}195196// even if symbol table building fails, we add the lib_info.197// This is because we may need to read from the ELF file for core file198// address read functionality. lookup_symbol checks for NULL symtab.199if (ph->libs) {200ph->lib_tail->next = newlib;201ph->lib_tail = newlib;202} else {203ph->libs = ph->lib_tail = newlib;204}205ph->num_libs++;206207return newlib;208}209210// lookup for a specific symbol211uintptr_t lookup_symbol(struct ps_prochandle* ph, const char* object_name,212const char* sym_name) {213// ignore object_name. search in all libraries214// FIXME: what should we do with object_name?? The library names are obtained215// by parsing /proc/<pid>/maps, which may not be the same as object_name.216// What we need is a utility to map object_name to real file name, something217// dlopen() does by looking at LD_LIBRARY_PATH and /etc/ld.so.cache. For218// now, we just ignore object_name and do a global search for the symbol.219220lib_info* lib = ph->libs;221while (lib) {222if (lib->symtab) {223uintptr_t res = search_symbol(lib->symtab, lib->base, sym_name, NULL);224if (res) return res;225}226lib = lib->next;227}228229print_debug("lookup failed for symbol '%s' in obj '%s'\n",230sym_name, object_name);231return (uintptr_t) NULL;232}233234235const char* symbol_for_pc(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* poffset) {236const char* res = NULL;237lib_info* lib = ph->libs;238while (lib) {239if (lib->symtab && addr >= lib->base) {240res = nearest_symbol(lib->symtab, addr - lib->base, poffset);241if (res) return res;242}243lib = lib->next;244}245return NULL;246}247248// add a thread to ps_prochandle249thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) {250thread_info* newthr;251if ( (newthr = (thread_info*) calloc(1, sizeof(thread_info))) == NULL) {252print_debug("can't allocate memory for thread_info\n");253return NULL;254}255256// initialize thread info257newthr->pthread_id = pthread_id;258newthr->lwp_id = lwp_id;259260// add new thread to the list261newthr->next = ph->threads;262ph->threads = newthr;263ph->num_threads++;264return newthr;265}266267void delete_thread_info(struct ps_prochandle* ph, thread_info* thr_to_be_removed) {268thread_info* current_thr = ph->threads;269270if (thr_to_be_removed == ph->threads) {271ph->threads = ph->threads->next;272} else {273thread_info* previous_thr;274while (current_thr && current_thr != thr_to_be_removed) {275previous_thr = current_thr;276current_thr = current_thr->next;277}278if (current_thr == NULL) {279print_error("Could not find the thread to be removed");280return;281}282previous_thr->next = current_thr->next;283}284ph->num_threads--;285free(current_thr);286}287288// struct used for client data from thread_db callback289struct thread_db_client_data {290struct ps_prochandle* ph;291thread_info_callback callback;292};293294// callback function for libthread_db295static int thread_db_callback(const td_thrhandle_t *th_p, void *data) {296struct thread_db_client_data* ptr = (struct thread_db_client_data*) data;297td_thrinfo_t ti;298td_err_e err;299300memset(&ti, 0, sizeof(ti));301err = td_thr_get_info(th_p, &ti);302if (err != TD_OK) {303print_debug("libthread_db : td_thr_get_info failed, can't get thread info\n");304return err;305}306307print_debug("thread_db : pthread %d (lwp %d)\n", ti.ti_tid, ti.ti_lid);308309if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE) {310print_debug("Skipping pthread %d (lwp %d)\n", ti.ti_tid, ti.ti_lid);311return TD_OK;312}313314if (ptr->callback(ptr->ph, ti.ti_tid, ti.ti_lid) != true)315return TD_ERR;316317return TD_OK;318}319320// read thread_info using libthread_db321bool read_thread_info(struct ps_prochandle* ph, thread_info_callback cb) {322struct thread_db_client_data mydata;323td_thragent_t* thread_agent = NULL;324if (td_ta_new(ph, &thread_agent) != TD_OK) {325print_debug("can't create libthread_db agent\n");326return false;327}328329mydata.ph = ph;330mydata.callback = cb;331332// we use libthread_db iterator to iterate thru list of threads.333if (td_ta_thr_iter(thread_agent, thread_db_callback, &mydata,334TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,335TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS) != TD_OK) {336td_ta_delete(thread_agent);337return false;338}339340// delete thread agent341td_ta_delete(thread_agent);342return true;343}344345346// get number of threads347int get_num_threads(struct ps_prochandle* ph) {348return ph->num_threads;349}350351// get lwp_id of n'th thread352lwpid_t get_lwp_id(struct ps_prochandle* ph, int index) {353int count = 0;354thread_info* thr = ph->threads;355while (thr) {356if (count == index) {357return thr->lwp_id;358}359count++;360thr = thr->next;361}362return -1;363}364365// get regs for a given lwp366bool get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id, struct user_regs_struct* regs) {367return ph->ops->get_lwp_regs(ph, lwp_id, regs);368}369370// get number of shared objects371int get_num_libs(struct ps_prochandle* ph) {372return ph->num_libs;373}374375// get name of n'th solib376const char* get_lib_name(struct ps_prochandle* ph, int index) {377int count = 0;378lib_info* lib = ph->libs;379while (lib) {380if (count == index) {381return lib->name;382}383count++;384lib = lib->next;385}386return NULL;387}388389// get base address of a lib390uintptr_t get_lib_base(struct ps_prochandle* ph, int index) {391int count = 0;392lib_info* lib = ph->libs;393while (lib) {394if (count == index) {395return lib->base;396}397count++;398lib = lib->next;399}400return (uintptr_t)NULL;401}402403bool find_lib(struct ps_prochandle* ph, const char *lib_name) {404lib_info *p = ph->libs;405while (p) {406if (strcmp(p->name, lib_name) == 0) {407return true;408}409p = p->next;410}411return false;412}413414//--------------------------------------------------------------------------415// proc service functions416417// get process id418pid_t ps_getpid(struct ps_prochandle *ph) {419return ph->pid;420}421422// ps_pglobal_lookup() looks up the symbol sym_name in the symbol table423// of the load object object_name in the target process identified by ph.424// It returns the symbol's value as an address in the target process in425// *sym_addr.426427ps_err_e ps_pglobal_lookup(struct ps_prochandle *ph, const char *object_name,428const char *sym_name, psaddr_t *sym_addr) {429*sym_addr = (psaddr_t) lookup_symbol(ph, object_name, sym_name);430return (*sym_addr ? PS_OK : PS_NOSYM);431}432433// read "size" bytes info "buf" from address "addr"434ps_err_e ps_pdread(struct ps_prochandle *ph, psaddr_t addr,435void *buf, size_t size) {436return ph->ops->p_pread(ph, (uintptr_t) addr, buf, size)? PS_OK: PS_ERR;437}438439// write "size" bytes of data to debuggee at address "addr"440ps_err_e ps_pdwrite(struct ps_prochandle *ph, psaddr_t addr,441const void *buf, size_t size) {442return ph->ops->p_pwrite(ph, (uintptr_t)addr, buf, size)? PS_OK: PS_ERR;443}444445// ------------------------------------------------------------------------446// Functions below this point are not yet implemented. They are here only447// to make the linker happy.448449ps_err_e ps_lsetfpregs(struct ps_prochandle *ph, lwpid_t lid, const prfpregset_t *fpregs) {450print_debug("ps_lsetfpregs not implemented\n");451return PS_OK;452}453454ps_err_e ps_lsetregs(struct ps_prochandle *ph, lwpid_t lid, const prgregset_t gregset) {455print_debug("ps_lsetregs not implemented\n");456return PS_OK;457}458459ps_err_e ps_lgetfpregs(struct ps_prochandle *ph, lwpid_t lid, prfpregset_t *fpregs) {460print_debug("ps_lgetfpregs not implemented\n");461return PS_OK;462}463464ps_err_e ps_lgetregs(struct ps_prochandle *ph, lwpid_t lid, prgregset_t gregset) {465print_debug("ps_lgetfpregs not implemented\n");466return PS_OK;467}468469// new libthread_db of NPTL seem to require this symbol470ps_err_e ps_get_thread_area() {471print_debug("ps_get_thread_area not implemented\n");472return PS_OK;473}474475476