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