2796N/A/*
4204N/A * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
2796N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2796N/A *
2796N/A * This code is free software; you can redistribute it and/or modify it
2796N/A * under the terms of the GNU General Public License version 2 only, as
2796N/A * published by the Free Software Foundation.
2796N/A *
2796N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2796N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2796N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2796N/A * version 2 for more details (a copy is included in the LICENSE file that
2796N/A * accompanied this code).
2796N/A *
2796N/A * You should have received a copy of the GNU General Public License version
2796N/A * 2 along with this work; if not, write to the Free Software Foundation,
2796N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2796N/A *
2796N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2796N/A * or visit www.oracle.com if you need additional information or have any
2796N/A * questions.
2796N/A *
2796N/A */
2796N/A#include <stdarg.h>
2796N/A#include <stdio.h>
2796N/A#include <stdlib.h>
2796N/A#include <string.h>
2796N/A#include <fcntl.h>
2796N/A#include <thread_db.h>
2796N/A#include "libproc_impl.h"
2796N/A
2796N/Astatic const char* alt_root = NULL;
2796N/Astatic int alt_root_len = -1;
2796N/A
2796N/A#define SA_ALTROOT "SA_ALTROOT"
2796N/A
2796N/Astatic void init_alt_root() {
2796N/A if (alt_root_len == -1) {
2796N/A alt_root = getenv(SA_ALTROOT);
2796N/A if (alt_root) {
2796N/A alt_root_len = strlen(alt_root);
2796N/A } else {
2796N/A alt_root_len = 0;
2796N/A }
2796N/A }
2796N/A}
2796N/A
2796N/Aint pathmap_open(const char* name) {
2796N/A int fd;
2796N/A char alt_path[PATH_MAX + 1];
2796N/A
2796N/A init_alt_root();
2796N/A fd = open(name, O_RDONLY);
2796N/A if (fd >= 0) {
2796N/A return fd;
2796N/A }
2796N/A
2796N/A if (alt_root_len > 0) {
2796N/A strcpy(alt_path, alt_root);
2796N/A strcat(alt_path, name);
2796N/A fd = open(alt_path, O_RDONLY);
2796N/A if (fd >= 0) {
2796N/A print_debug("path %s substituted for %s\n", alt_path, name);
2796N/A return fd;
2796N/A }
2796N/A
2796N/A if (strrchr(name, '/')) {
2796N/A strcpy(alt_path, alt_root);
2796N/A strcat(alt_path, strrchr(name, '/'));
2796N/A fd = open(alt_path, O_RDONLY);
2796N/A if (fd >= 0) {
2796N/A print_debug("path %s substituted for %s\n", alt_path, name);
2796N/A return fd;
2796N/A }
2796N/A }
2796N/A }
2796N/A
2796N/A return -1;
2796N/A}
2796N/A
2796N/Astatic bool _libsaproc_debug;
2796N/A
2796N/Avoid print_debug(const char* format,...) {
2796N/A if (_libsaproc_debug) {
2796N/A va_list alist;
2796N/A
2796N/A va_start(alist, format);
2796N/A fputs("libsaproc DEBUG: ", stderr);
2796N/A vfprintf(stderr, format, alist);
2796N/A va_end(alist);
2796N/A }
2796N/A}
2796N/A
4204N/Avoid print_error(const char* format,...) {
4204N/A va_list alist;
4204N/A va_start(alist, format);
4204N/A fputs("ERROR: ", stderr);
4204N/A vfprintf(stderr, format, alist);
4204N/A va_end(alist);
4204N/A}
4204N/A
2796N/Abool is_debug() {
2796N/A return _libsaproc_debug;
2796N/A}
2796N/A
2796N/A// initialize libproc
2796N/Abool init_libproc(bool debug) {
2796N/A // init debug mode
2796N/A _libsaproc_debug = debug;
2796N/A
2796N/A // initialize the thread_db library
2796N/A if (td_init() != TD_OK) {
2796N/A print_debug("libthread_db's td_init failed\n");
2796N/A return false;
2796N/A }
2796N/A
2796N/A return true;
2796N/A}
2796N/A
2796N/Astatic void destroy_lib_info(struct ps_prochandle* ph) {
2796N/A lib_info* lib = ph->libs;
2796N/A while (lib) {
2796N/A lib_info *next = lib->next;
2796N/A if (lib->symtab) {
2796N/A destroy_symtab(lib->symtab);
2796N/A }
2796N/A free(lib);
2796N/A lib = next;
2796N/A }
2796N/A}
2796N/A
2796N/Astatic void destroy_thread_info(struct ps_prochandle* ph) {
2796N/A thread_info* thr = ph->threads;
2796N/A while (thr) {
2796N/A thread_info *next = thr->next;
2796N/A free(thr);
2796N/A thr = next;
2796N/A }
2796N/A}
2796N/A
2796N/A// ps_prochandle cleanup
2796N/A
2796N/A// ps_prochandle cleanup
2796N/Avoid Prelease(struct ps_prochandle* ph) {
2796N/A // do the "derived class" clean-up first
2796N/A ph->ops->release(ph);
2796N/A destroy_lib_info(ph);
2796N/A destroy_thread_info(ph);
2796N/A free(ph);
2796N/A}
2796N/A
2796N/Alib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t base) {
2796N/A return add_lib_info_fd(ph, libname, -1, base);
2796N/A}
2796N/A
2796N/Alib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd, uintptr_t base) {
2796N/A lib_info* newlib;
2796N/A
2796N/A if ( (newlib = (lib_info*) calloc(1, sizeof(struct lib_info))) == NULL) {
2796N/A print_debug("can't allocate memory for lib_info\n");
2796N/A return NULL;
2796N/A }
2796N/A
2796N/A strncpy(newlib->name, libname, sizeof(newlib->name));
2796N/A newlib->base = base;
2796N/A
2796N/A if (fd == -1) {
2796N/A if ( (newlib->fd = pathmap_open(newlib->name)) < 0) {
2796N/A print_debug("can't open shared object %s\n", newlib->name);
2796N/A free(newlib);
2796N/A return NULL;
2796N/A }
2796N/A } else {
2796N/A newlib->fd = fd;
2796N/A }
2796N/A
2796N/A // check whether we have got an ELF file. /proc/<pid>/map
2796N/A // gives out all file mappings and not just shared objects
2796N/A if (is_elf_file(newlib->fd) == false) {
2796N/A close(newlib->fd);
2796N/A free(newlib);
2796N/A return NULL;
2796N/A }
2796N/A
2796N/A newlib->symtab = build_symtab(newlib->fd);
2796N/A if (newlib->symtab == NULL) {
2796N/A print_debug("symbol table build failed for %s\n", newlib->name);
2796N/A }
2796N/A else {
2796N/A print_debug("built symbol table for %s\n", newlib->name);
2796N/A }
2796N/A
2796N/A // even if symbol table building fails, we add the lib_info.
2796N/A // This is because we may need to read from the ELF file for core file
2796N/A // address read functionality. lookup_symbol checks for NULL symtab.
2796N/A if (ph->libs) {
2796N/A ph->lib_tail->next = newlib;
2796N/A ph->lib_tail = newlib;
2796N/A } else {
2796N/A ph->libs = ph->lib_tail = newlib;
2796N/A }
2796N/A ph->num_libs++;
2796N/A
2796N/A return newlib;
2796N/A}
2796N/A
2796N/A// lookup for a specific symbol
2796N/Auintptr_t lookup_symbol(struct ps_prochandle* ph, const char* object_name,
2796N/A const char* sym_name) {
2796N/A // ignore object_name. search in all libraries
2796N/A // FIXME: what should we do with object_name?? The library names are obtained
2796N/A // by parsing /proc/<pid>/maps, which may not be the same as object_name.
2796N/A // What we need is a utility to map object_name to real file name, something
2796N/A // dlopen() does by looking at LD_LIBRARY_PATH and /etc/ld.so.cache. For
2796N/A // now, we just ignore object_name and do a global search for the symbol.
2796N/A
2796N/A lib_info* lib = ph->libs;
2796N/A while (lib) {
2796N/A if (lib->symtab) {
2796N/A uintptr_t res = search_symbol(lib->symtab, lib->base, sym_name, NULL);
2796N/A if (res) return res;
2796N/A }
2796N/A lib = lib->next;
2796N/A }
2796N/A
2796N/A print_debug("lookup failed for symbol '%s' in obj '%s'\n",
2796N/A sym_name, object_name);
2796N/A return (uintptr_t) NULL;
2796N/A}
2796N/A
2796N/A
2796N/Aconst char* symbol_for_pc(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* poffset) {
2796N/A const char* res = NULL;
2796N/A lib_info* lib = ph->libs;
2796N/A while (lib) {
2796N/A if (lib->symtab && addr >= lib->base) {
2796N/A res = nearest_symbol(lib->symtab, addr - lib->base, poffset);
2796N/A if (res) return res;
2796N/A }
2796N/A lib = lib->next;
2796N/A }
2796N/A return NULL;
2796N/A}
2796N/A
2796N/A// add a thread to ps_prochandle
2796N/Athread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) {
2796N/A thread_info* newthr;
2796N/A if ( (newthr = (thread_info*) calloc(1, sizeof(thread_info))) == NULL) {
2796N/A print_debug("can't allocate memory for thread_info\n");
2796N/A return NULL;
2796N/A }
2796N/A
2796N/A // initialize thread info
2796N/A newthr->pthread_id = pthread_id;
2796N/A newthr->lwp_id = lwp_id;
2796N/A
2796N/A // add new thread to the list
2796N/A newthr->next = ph->threads;
2796N/A ph->threads = newthr;
2796N/A ph->num_threads++;
2796N/A return newthr;
2796N/A}
2796N/A
2796N/A
2796N/A// struct used for client data from thread_db callback
2796N/Astruct thread_db_client_data {
2796N/A struct ps_prochandle* ph;
2796N/A thread_info_callback callback;
2796N/A};
2796N/A
2796N/A// callback function for libthread_db
2796N/Astatic int thread_db_callback(const td_thrhandle_t *th_p, void *data) {
2796N/A struct thread_db_client_data* ptr = (struct thread_db_client_data*) data;
2796N/A td_thrinfo_t ti;
2796N/A td_err_e err;
2796N/A
2796N/A memset(&ti, 0, sizeof(ti));
2796N/A err = td_thr_get_info(th_p, &ti);
2796N/A if (err != TD_OK) {
2796N/A print_debug("libthread_db : td_thr_get_info failed, can't get thread info\n");
2796N/A return err;
2796N/A }
2796N/A
2796N/A print_debug("thread_db : pthread %d (lwp %d)\n", ti.ti_tid, ti.ti_lid);
2796N/A
2796N/A if (ptr->callback(ptr->ph, (pthread_t)ti.ti_tid, ti.ti_lid) != true)
2796N/A return TD_ERR;
2796N/A
2796N/A return TD_OK;
2796N/A}
2796N/A
2796N/A// read thread_info using libthread_db
2796N/Abool read_thread_info(struct ps_prochandle* ph, thread_info_callback cb) {
2796N/A struct thread_db_client_data mydata;
2796N/A td_thragent_t* thread_agent = NULL;
2796N/A if (td_ta_new(ph, &thread_agent) != TD_OK) {
2796N/A print_debug("can't create libthread_db agent\n");
2796N/A return false;
2796N/A }
2796N/A
2796N/A mydata.ph = ph;
2796N/A mydata.callback = cb;
2796N/A
2796N/A // we use libthread_db iterator to iterate thru list of threads.
2796N/A if (td_ta_thr_iter(thread_agent, thread_db_callback, &mydata,
2796N/A TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
2796N/A TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS) != TD_OK) {
2796N/A td_ta_delete(thread_agent);
2796N/A return false;
2796N/A }
2796N/A
2796N/A // delete thread agent
2796N/A td_ta_delete(thread_agent);
2796N/A return true;
2796N/A}
2796N/A
2796N/A
2796N/A// get number of threads
2796N/Aint get_num_threads(struct ps_prochandle* ph) {
2796N/A return ph->num_threads;
2796N/A}
2796N/A
2796N/A// get lwp_id of n'th thread
2796N/Alwpid_t get_lwp_id(struct ps_prochandle* ph, int index) {
2796N/A int count = 0;
2796N/A thread_info* thr = ph->threads;
2796N/A while (thr) {
2796N/A if (count == index) {
2796N/A return thr->lwp_id;
2796N/A }
2796N/A count++;
2796N/A thr = thr->next;
2796N/A }
2796N/A return -1;
2796N/A}
2796N/A
2796N/A// get regs for a given lwp
2796N/Abool get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id, struct reg* regs) {
2796N/A return ph->ops->get_lwp_regs(ph, lwp_id, regs);
2796N/A}
2796N/A
2796N/A// get number of shared objects
2796N/Aint get_num_libs(struct ps_prochandle* ph) {
2796N/A return ph->num_libs;
2796N/A}
2796N/A
2796N/A// get name of n'th solib
2796N/Aconst char* get_lib_name(struct ps_prochandle* ph, int index) {
2796N/A int count = 0;
2796N/A lib_info* lib = ph->libs;
2796N/A while (lib) {
2796N/A if (count == index) {
2796N/A return lib->name;
2796N/A }
2796N/A count++;
2796N/A lib = lib->next;
2796N/A }
2796N/A return NULL;
2796N/A}
2796N/A
2796N/A// get base address of a lib
2796N/Auintptr_t get_lib_base(struct ps_prochandle* ph, int index) {
2796N/A int count = 0;
2796N/A lib_info* lib = ph->libs;
2796N/A while (lib) {
2796N/A if (count == index) {
2796N/A return lib->base;
2796N/A }
2796N/A count++;
2796N/A lib = lib->next;
2796N/A }
2796N/A return (uintptr_t)NULL;
2796N/A}
2796N/A
2796N/Abool find_lib(struct ps_prochandle* ph, const char *lib_name) {
2796N/A lib_info *p = ph->libs;
2796N/A while (p) {
2796N/A if (strcmp(p->name, lib_name) == 0) {
2796N/A return true;
2796N/A }
2796N/A p = p->next;
2796N/A }
2796N/A return false;
2796N/A}
2796N/A
2796N/A//--------------------------------------------------------------------------
2796N/A// proc service functions
2796N/A
2796N/A// ps_pglobal_lookup() looks up the symbol sym_name in the symbol table
2796N/A// of the load object object_name in the target process identified by ph.
2796N/A// It returns the symbol's value as an address in the target process in
2796N/A// *sym_addr.
2796N/A
2796N/Aps_err_e ps_pglobal_lookup(struct ps_prochandle *ph, const char *object_name,
2796N/A const char *sym_name, psaddr_t *sym_addr) {
2796N/A *sym_addr = (psaddr_t) lookup_symbol(ph, object_name, sym_name);
2796N/A return (*sym_addr ? PS_OK : PS_NOSYM);
2796N/A}
2796N/A
2796N/A// read "size" bytes info "buf" from address "addr"
2796N/Aps_err_e ps_pread(struct ps_prochandle *ph, psaddr_t addr,
2796N/A void *buf, size_t size) {
2796N/A return ph->ops->p_pread(ph, (uintptr_t) addr, buf, size)? PS_OK: PS_ERR;
2796N/A}
2796N/A
2796N/A// write "size" bytes of data to debuggee at address "addr"
2796N/Aps_err_e ps_pwrite(struct ps_prochandle *ph, psaddr_t addr,
2796N/A const void *buf, size_t size) {
2796N/A return ph->ops->p_pwrite(ph, (uintptr_t)addr, buf, size)? PS_OK: PS_ERR;
2796N/A}
2796N/A
2796N/A// fill in ptrace_lwpinfo for lid
2796N/Aps_err_e ps_linfo(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo) {
2796N/A return ph->ops->get_lwp_info(ph, lwp_id, linfo)? PS_OK: PS_ERR;
2796N/A}
2796N/A
2796N/A// needed for when libthread_db is compiled with TD_DEBUG defined
2796N/Avoid
2796N/Aps_plog (const char *format, ...)
2796N/A{
2796N/A va_list alist;
2796N/A
2796N/A va_start(alist, format);
2796N/A vfprintf(stderr, format, alist);
2796N/A va_end(alist);
2796N/A}
2796N/A
2796N/A// ------------------------------------------------------------------------
2796N/A// Functions below this point are not yet implemented. They are here only
2796N/A// to make the linker happy.
2796N/A
2796N/Aps_err_e ps_lsetfpregs(struct ps_prochandle *ph, lwpid_t lid, const prfpregset_t *fpregs) {
2796N/A print_debug("ps_lsetfpregs not implemented\n");
2796N/A return PS_OK;
2796N/A}
2796N/A
2796N/Aps_err_e ps_lsetregs(struct ps_prochandle *ph, lwpid_t lid, const prgregset_t gregset) {
2796N/A print_debug("ps_lsetregs not implemented\n");
2796N/A return PS_OK;
2796N/A}
2796N/A
2796N/Aps_err_e ps_lgetfpregs(struct ps_prochandle *ph, lwpid_t lid, prfpregset_t *fpregs) {
2796N/A print_debug("ps_lgetfpregs not implemented\n");
2796N/A return PS_OK;
2796N/A}
2796N/A
2796N/Aps_err_e ps_lgetregs(struct ps_prochandle *ph, lwpid_t lid, prgregset_t gregset) {
2796N/A print_debug("ps_lgetfpregs not implemented\n");
2796N/A return PS_OK;
2796N/A}
2796N/A
2796N/Aps_err_e ps_lstop(struct ps_prochandle *ph, lwpid_t lid) {
2796N/A print_debug("ps_lstop not implemented\n");
2796N/A return PS_OK;
2796N/A}
2796N/A
2796N/Aps_err_e ps_pcontinue(struct ps_prochandle *ph) {
2796N/A print_debug("ps_pcontinue not implemented\n");
2796N/A return PS_OK;
2796N/A}