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
2796N/A#include <limits.h>
2796N/A#include <stdio.h>
2796N/A#include <stdlib.h>
2796N/A#include <string.h>
2796N/A#include <errno.h>
2796N/A#include <sys/types.h>
2796N/A#include <sys/wait.h>
2796N/A#include <sys/ptrace.h>
2796N/A#include <sys/param.h>
2796N/A#include <sys/user.h>
2796N/A#include <elf.h>
2796N/A#include <sys/elf_common.h>
2796N/A#include <sys/link_elf.h>
2796N/A#include <libutil.h>
2796N/A#include "libproc_impl.h"
2796N/A#include "elfmacros.h"
2796N/A
2796N/A// This file has the libproc implementation specific to live process
2796N/A// For core files, refer to ps_core.c
2796N/A
2796N/Astatic inline uintptr_t align(uintptr_t ptr, size_t size) {
2796N/A return (ptr & ~(size - 1));
2796N/A}
2796N/A
2796N/A// ---------------------------------------------
2796N/A// ptrace functions
2796N/A// ---------------------------------------------
2796N/A
2796N/A// read "size" bytes of data from "addr" within the target process.
2796N/A// unlike the standard ptrace() function, process_read_data() can handle
2796N/A// unaligned address - alignment check, if required, should be done
2796N/A// before calling process_read_data.
2796N/A
2796N/Astatic bool process_read_data(struct ps_prochandle* ph, uintptr_t addr, char *buf, size_t size) {
2796N/A int rslt;
2796N/A size_t i, words;
2796N/A uintptr_t end_addr = addr + size;
2796N/A uintptr_t aligned_addr = align(addr, sizeof(int));
2796N/A
2796N/A if (aligned_addr != addr) {
2796N/A char *ptr = (char *)&rslt;
2796N/A errno = 0;
2796N/A rslt = ptrace(PT_READ_D, ph->pid, (caddr_t) aligned_addr, 0);
2796N/A if (errno) {
2796N/A print_debug("ptrace(PT_READ_D, ..) failed for %d bytes @ %lx\n", size, addr);
2796N/A return false;
2796N/A }
2796N/A for (; aligned_addr != addr; aligned_addr++, ptr++);
2796N/A for (; ((intptr_t)aligned_addr % sizeof(int)) && aligned_addr < end_addr;
2796N/A aligned_addr++)
2796N/A *(buf++) = *(ptr++);
2796N/A }
2796N/A
2796N/A words = (end_addr - aligned_addr) / sizeof(int);
2796N/A
2796N/A // assert((intptr_t)aligned_addr % sizeof(int) == 0);
2796N/A for (i = 0; i < words; i++) {
2796N/A errno = 0;
2796N/A rslt = ptrace(PT_READ_D, ph->pid, (caddr_t) aligned_addr, 0);
2796N/A if (errno) {
2796N/A print_debug("ptrace(PT_READ_D, ..) failed for %d bytes @ %lx\n", size, addr);
2796N/A return false;
2796N/A }
2796N/A *(int *)buf = rslt;
2796N/A buf += sizeof(int);
2796N/A aligned_addr += sizeof(int);
2796N/A }
2796N/A
2796N/A if (aligned_addr != end_addr) {
2796N/A char *ptr = (char *)&rslt;
2796N/A errno = 0;
2796N/A rslt = ptrace(PT_READ_D, ph->pid, (caddr_t) aligned_addr, 0);
2796N/A if (errno) {
2796N/A print_debug("ptrace(PT_READ_D, ..) failed for %d bytes @ %lx\n", size, addr);
2796N/A return false;
2796N/A }
2796N/A for (; aligned_addr != end_addr; aligned_addr++)
2796N/A *(buf++) = *(ptr++);
2796N/A }
2796N/A return true;
2796N/A}
2796N/A
2796N/A// null implementation for write
2796N/Astatic bool process_write_data(struct ps_prochandle* ph,
2796N/A uintptr_t addr, const char *buf , size_t size) {
2796N/A return false;
2796N/A}
2796N/A
2796N/A// "user" should be a pointer to a reg
2796N/Astatic bool process_get_lwp_regs(struct ps_prochandle* ph, pid_t pid, struct reg *user) {
2796N/A // we have already attached to all thread 'pid's, just use ptrace call
2796N/A // to get regset now. Note that we don't cache regset upfront for processes.
2796N/A if (ptrace(PT_GETREGS, pid, (caddr_t) user, 0) < 0) {
2796N/A print_debug("ptrace(PTRACE_GETREGS, ...) failed for lwp %d\n", pid);
2796N/A return false;
2796N/A }
2796N/A return true;
2796N/A}
2796N/A
2796N/A// fill in ptrace_lwpinfo for lid
2796N/Astatic bool process_get_lwp_info(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo) {
2796N/A errno = 0;
2796N/A ptrace(PT_LWPINFO, lwp_id, linfo, sizeof(struct ptrace_lwpinfo));
2796N/A
2796N/A return (errno == 0)? true: false;
2796N/A}
2796N/A
4204N/Astatic bool ptrace_continue(pid_t pid, int signal) {
4204N/A // pass the signal to the process so we don't swallow it
4204N/A if (ptrace(PTRACE_CONT, pid, NULL, signal) < 0) {
4204N/A print_debug("ptrace(PTRACE_CONT, ..) failed for %d\n", pid);
4204N/A return false;
4204N/A }
4204N/A return true;
4204N/A}
4204N/A
4204N/A// waits until the ATTACH has stopped the process
4204N/A// by signal SIGSTOP
4204N/Astatic bool ptrace_waitpid(pid_t pid) {
4204N/A int ret;
4204N/A int status;
4204N/A do {
4204N/A // Wait for debuggee to stop.
4204N/A ret = waitpid(pid, &status, 0);
4204N/A if (ret >= 0) {
4204N/A if (WIFSTOPPED(status)) {
4204N/A // Any signal will stop the thread, make sure it is SIGSTOP. Otherwise SIGSTOP
4204N/A // will still be pending and delivered when the process is DETACHED and the process
4204N/A // will go to sleep.
4204N/A if (WSTOPSIG(status) == SIGSTOP) {
4204N/A // Debuggee stopped by SIGSTOP.
4204N/A return true;
4204N/A }
4204N/A if (!ptrace_continue(pid, WSTOPSIG(status))) {
4204N/A print_error("Failed to correctly attach to VM. VM might HANG! [PTRACE_CONT failed, stopped by %d]\n", WSTOPSIG(status));
4204N/A return false;
4204N/A }
4204N/A } else {
4204N/A print_debug("waitpid(): Child process exited/terminated (status = 0x%x)\n", status);
4204N/A return false;
4204N/A }
4204N/A } else {
4204N/A switch (errno) {
4204N/A case EINTR:
4204N/A continue;
4204N/A break;
4204N/A case ECHILD:
4204N/A print_debug("waitpid() failed. Child process pid (%d) does not exist \n", pid);
4204N/A break;
4204N/A case EINVAL:
4204N/A print_debug("waitpid() failed. Invalid options argument.\n");
4204N/A break;
4204N/A default:
4204N/A print_debug("waitpid() failed. Unexpected error %d\n",errno);
4204N/A }
4204N/A return false;
4204N/A }
4204N/A } while(true);
4204N/A}
4204N/A
2796N/A// attach to a process/thread specified by "pid"
2796N/Astatic bool ptrace_attach(pid_t pid) {
2796N/A if (ptrace(PT_ATTACH, pid, NULL, 0) < 0) {
2796N/A print_debug("ptrace(PTRACE_ATTACH, ..) failed for %d\n", pid);
2796N/A return false;
2796N/A } else {
4204N/A return ptrace_waitpid(pid);
2796N/A }
2796N/A}
2796N/A
2796N/A// -------------------------------------------------------
2796N/A// functions for obtaining library information
2796N/A// -------------------------------------------------------
2796N/A
2796N/A// callback for read_thread_info
2796N/Astatic bool add_new_thread(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) {
2796N/A return add_thread_info(ph, pthread_id, lwp_id) != NULL;
2796N/A}
2796N/A
2796N/A#if defined(__FreeBSD__) && __FreeBSD_version < 701000
2796N/A/*
2796N/A * TEXT_START_ADDR from binutils/ld/emulparams/<arch_spec>.sh
2796N/A * Not the most robust but good enough.
2796N/A */
2796N/A
2796N/A#if defined(amd64) || defined(x86_64)
2796N/A#define TEXT_START_ADDR 0x400000
2796N/A#elif defined(i386)
2796N/A#define TEXT_START_ADDR 0x8048000
2796N/A#else
2796N/A#error TEXT_START_ADDR not defined
2796N/A#endif
2796N/A
2796N/A#define BUF_SIZE (PATH_MAX + NAME_MAX + 1)
2796N/A
2796N/Auintptr_t linkmap_addr(struct ps_prochandle *ph) {
2796N/A uintptr_t ehdr_addr, phdr_addr, dyn_addr, dmap_addr, lmap_addr;
2796N/A ELF_EHDR ehdr;
2796N/A ELF_PHDR *phdrs, *phdr;
2796N/A ELF_DYN *dyns, *dyn;
2796N/A struct r_debug dmap;
2796N/A unsigned long hdrs_size;
2796N/A unsigned int i;
2796N/A
2796N/A /* read ELF_EHDR at TEXT_START_ADDR and validate */
2796N/A
2796N/A ehdr_addr = (uintptr_t)TEXT_START_ADDR;
2796N/A
2796N/A if (process_read_data(ph, ehdr_addr, (char *)&ehdr, sizeof(ehdr)) != true) {
2796N/A print_debug("process_read_data failed for ehdr_addr %p\n", ehdr_addr);
2796N/A return (0);
2796N/A }
2796N/A
2796N/A if (!IS_ELF(ehdr) ||
2796N/A ehdr.e_ident[EI_CLASS] != ELF_TARG_CLASS ||
2796N/A ehdr.e_ident[EI_DATA] != ELF_TARG_DATA ||
2796N/A ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
2796N/A ehdr.e_phentsize != sizeof(ELF_PHDR) ||
2796N/A ehdr.e_version != ELF_TARG_VER ||
2796N/A ehdr.e_machine != ELF_TARG_MACH) {
2796N/A print_debug("not an ELF_EHDR at %p\n", ehdr_addr);
2796N/A return (0);
2796N/A }
2796N/A
2796N/A /* allocate space for all ELF_PHDR's and read */
2796N/A
2796N/A phdr_addr = ehdr_addr + ehdr.e_phoff;
2796N/A hdrs_size = ehdr.e_phnum * sizeof(ELF_PHDR);
2796N/A
2796N/A if ((phdrs = malloc(hdrs_size)) == NULL)
2796N/A return (0);
2796N/A
2796N/A if (process_read_data(ph, phdr_addr, (char *)phdrs, hdrs_size) != true) {
2796N/A print_debug("process_read_data failed for phdr_addr %p\n", phdr_addr);
2796N/A return (0);
2796N/A }
2796N/A
2796N/A /* find PT_DYNAMIC section */
2796N/A
2796N/A for (i = 0, phdr = phdrs; i < ehdr.e_phnum; i++, phdr++) {
2796N/A if (phdr->p_type == PT_DYNAMIC)
2796N/A break;
2796N/A }
2796N/A
2796N/A if (i >= ehdr.e_phnum) {
2796N/A print_debug("PT_DYNAMIC section not found!\n");
2796N/A free(phdrs);
2796N/A return (0);
2796N/A }
2796N/A
2796N/A /* allocate space and read in ELF_DYN headers */
2796N/A
2796N/A dyn_addr = phdr->p_vaddr;
2796N/A hdrs_size = phdr->p_memsz;
2796N/A free(phdrs);
2796N/A
2796N/A if ((dyns = malloc(hdrs_size)) == NULL)
2796N/A return (0);
2796N/A
2796N/A if (process_read_data(ph, dyn_addr, (char *)dyns, hdrs_size) != true) {
2796N/A print_debug("process_read_data failed for dyn_addr %p\n", dyn_addr);
2796N/A free(dyns);
2796N/A return (0);
2796N/A }
2796N/A
2796N/A /* find DT_DEBUG */
2796N/A
2796N/A dyn = dyns;
2796N/A while (dyn->d_tag != DT_DEBUG && dyn->d_tag != DT_NULL) {
2796N/A dyn++;
2796N/A }
2796N/A
2796N/A if (dyn->d_tag != DT_DEBUG) {
2796N/A print_debug("failed to find DT_DEBUG\n");
2796N/A free(dyns);
2796N/A return (0);
2796N/A }
2796N/A
2796N/A /* read struct r_debug into dmap */
2796N/A
2796N/A dmap_addr = (uintptr_t)dyn->d_un.d_ptr;
2796N/A free(dyns);
2796N/A
2796N/A if (process_read_data(ph, dmap_addr, (char *)&dmap, sizeof(dmap)) != true) {
2796N/A print_debug("process_read_data failed for dmap_addr %p\n", dmap_addr);
2796N/A return (0);
2796N/A }
2796N/A
2796N/A lmap_addr = (uintptr_t)dmap.r_map;
2796N/A
2796N/A return (lmap_addr);
2796N/A}
2796N/A#endif // __FreeBSD__ && __FreeBSD_version < 701000
2796N/A
2796N/Astatic bool read_lib_info(struct ps_prochandle* ph) {
2796N/A#if defined(__FreeBSD__) && __FreeBSD_version >= 701000
2796N/A struct kinfo_vmentry *freep, *kve;
2796N/A int i, cnt;
2796N/A
2796N/A freep = kinfo_getvmmap(ph->pid, &cnt);
2796N/A if (freep == NULL) {
2796N/A print_debug("can't get vm map for pid\n", ph->pid);
2796N/A return false;
2796N/A }
2796N/A
2796N/A for (i = 0; i < cnt; i++) {
2796N/A kve = &freep[i];
2796N/A if ((kve->kve_flags & KVME_FLAG_COW) &&
2796N/A kve->kve_path != NULL &&
2796N/A strlen(kve->kve_path) > 0) {
2796N/A
2796N/A if (find_lib(ph, kve->kve_path) == false) {
2796N/A lib_info* lib;
2796N/A if ((lib = add_lib_info(ph, kve->kve_path,
2796N/A (uintptr_t) kve->kve_start)) == NULL)
2796N/A continue; // ignore, add_lib_info prints error
2796N/A
2796N/A // we don't need to keep the library open, symtab is already
2796N/A // built. Only for core dump we need to keep the fd open.
2796N/A close(lib->fd);
2796N/A lib->fd = -1;
2796N/A }
2796N/A }
2796N/A }
2796N/A
2796N/A free(freep);
2796N/A
2796N/A return true;
2796N/A#else
2796N/A char *l_name;
2796N/A struct link_map *lmap;
2796N/A uintptr_t lmap_addr;
2796N/A
2796N/A if ((l_name = malloc(BUF_SIZE)) == NULL)
2796N/A return false;
2796N/A
2796N/A if ((lmap = malloc(sizeof(*lmap))) == NULL) {
2796N/A free(l_name);
2796N/A return false;
2796N/A }
2796N/A
2796N/A lmap_addr = linkmap_addr(ph);
2796N/A
2796N/A if (lmap_addr == 0) {
2796N/A free(l_name);
2796N/A free(lmap);
2796N/A return false;
2796N/A }
2796N/A
2796N/A do {
2796N/A if (process_read_data(ph, lmap_addr, (char *)lmap, sizeof(*lmap)) != true) {
2796N/A print_debug("process_read_data failed for lmap_addr %p\n", lmap_addr);
2796N/A free (l_name);
2796N/A free (lmap);
2796N/A return false;
2796N/A }
2796N/A
2796N/A if (process_read_data(ph, (uintptr_t)lmap->l_name, l_name,
2796N/A BUF_SIZE) != true) {
2796N/A print_debug("process_read_data failed for lmap->l_name %p\n",
2796N/A lmap->l_name);
2796N/A free (l_name);
2796N/A free (lmap);
2796N/A return false;
2796N/A }
2796N/A
2796N/A if (find_lib(ph, l_name) == false) {
2796N/A lib_info* lib;
2796N/A if ((lib = add_lib_info(ph, l_name,
2796N/A (uintptr_t) lmap->l_addr)) == NULL)
2796N/A continue; // ignore, add_lib_info prints error
2796N/A
2796N/A // we don't need to keep the library open, symtab is already
2796N/A // built. Only for core dump we need to keep the fd open.
2796N/A close(lib->fd);
2796N/A lib->fd = -1;
2796N/A }
2796N/A lmap_addr = (uintptr_t)lmap->l_next;
2796N/A } while (lmap->l_next != NULL);
2796N/A
2796N/A free (l_name);
2796N/A free (lmap);
2796N/A
2796N/A return true;
2796N/A#endif
2796N/A}
2796N/A
2796N/A// detach a given pid
2796N/Astatic bool ptrace_detach(pid_t pid) {
2796N/A if (pid && ptrace(PT_DETACH, pid, (caddr_t)1, 0) < 0) {
2796N/A print_debug("ptrace(PTRACE_DETACH, ..) failed for %d\n", pid);
2796N/A return false;
2796N/A } else {
2796N/A return true;
2796N/A }
2796N/A}
2796N/A
2796N/Astatic void process_cleanup(struct ps_prochandle* ph) {
2796N/A ptrace_detach(ph->pid);
2796N/A}
2796N/A
2796N/Astatic ps_prochandle_ops process_ops = {
2796N/A .release= process_cleanup,
2796N/A .p_pread= process_read_data,
2796N/A .p_pwrite= process_write_data,
2796N/A .get_lwp_regs= process_get_lwp_regs,
2796N/A .get_lwp_info= process_get_lwp_info
2796N/A};
2796N/A
2796N/A// attach to the process. One and only one exposed stuff
2796N/Astruct ps_prochandle* Pgrab(pid_t pid) {
2796N/A struct ps_prochandle* ph = NULL;
2796N/A thread_info* thr = NULL;
2796N/A
2796N/A if ( (ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle))) == NULL) {
2796N/A print_debug("can't allocate memory for ps_prochandle\n");
2796N/A return NULL;
2796N/A }
2796N/A
2796N/A if (ptrace_attach(pid) != true) {
2796N/A free(ph);
2796N/A return NULL;
2796N/A }
2796N/A
2796N/A // initialize ps_prochandle
2796N/A ph->pid = pid;
2796N/A
2796N/A // initialize vtable
2796N/A ph->ops = &process_ops;
2796N/A
2796N/A // read library info and symbol tables, must do this before attaching threads,
2796N/A // as the symbols in the pthread library will be used to figure out
2796N/A // the list of threads within the same process.
2796N/A if (read_lib_info(ph) != true) {
2796N/A ptrace_detach(pid);
2796N/A free(ph);
2796N/A return NULL;
2796N/A }
2796N/A
2796N/A // read thread info
2796N/A read_thread_info(ph, add_new_thread);
2796N/A
2796N/A return ph;
2796N/A}