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
0N/A#include <stdio.h>
0N/A#include <stdlib.h>
0N/A#include <string.h>
4204N/A#include <signal.h>
0N/A#include <errno.h>
0N/A#include <sys/ptrace.h>
0N/A#include "libproc_impl.h"
0N/A
0N/A#if defined(x86_64) && !defined(amd64)
0N/A#define amd64 1
0N/A#endif
0N/A
0N/A#ifndef __WALL
0N/A#define __WALL 0x40000000 // Copied from /usr/include/linux/wait.h
0N/A#endif
0N/A
0N/A// This file has the libproc implementation specific to live process
0N/A// For core files, refer to ps_core.c
0N/A
0N/Astatic inline uintptr_t align(uintptr_t ptr, size_t size) {
0N/A return (ptr & ~(size - 1));
0N/A}
0N/A
0N/A// ---------------------------------------------
0N/A// ptrace functions
0N/A// ---------------------------------------------
0N/A
0N/A// read "size" bytes of data from "addr" within the target process.
0N/A// unlike the standard ptrace() function, process_read_data() can handle
0N/A// unaligned address - alignment check, if required, should be done
0N/A// before calling process_read_data.
0N/A
0N/Astatic bool process_read_data(struct ps_prochandle* ph, uintptr_t addr, char *buf, size_t size) {
0N/A long rslt;
0N/A size_t i, words;
0N/A uintptr_t end_addr = addr + size;
0N/A uintptr_t aligned_addr = align(addr, sizeof(long));
0N/A
0N/A if (aligned_addr != addr) {
0N/A char *ptr = (char *)&rslt;
0N/A errno = 0;
0N/A rslt = ptrace(PTRACE_PEEKDATA, ph->pid, aligned_addr, 0);
0N/A if (errno) {
0N/A print_debug("ptrace(PTRACE_PEEKDATA, ..) failed for %d bytes @ %lx\n", size, addr);
0N/A return false;
0N/A }
0N/A for (; aligned_addr != addr; aligned_addr++, ptr++);
0N/A for (; ((intptr_t)aligned_addr % sizeof(long)) && aligned_addr < end_addr;
0N/A aligned_addr++)
0N/A *(buf++) = *(ptr++);
0N/A }
0N/A
0N/A words = (end_addr - aligned_addr) / sizeof(long);
0N/A
0N/A // assert((intptr_t)aligned_addr % sizeof(long) == 0);
0N/A for (i = 0; i < words; i++) {
0N/A errno = 0;
0N/A rslt = ptrace(PTRACE_PEEKDATA, ph->pid, aligned_addr, 0);
0N/A if (errno) {
0N/A print_debug("ptrace(PTRACE_PEEKDATA, ..) failed for %d bytes @ %lx\n", size, addr);
0N/A return false;
0N/A }
0N/A *(long *)buf = rslt;
0N/A buf += sizeof(long);
0N/A aligned_addr += sizeof(long);
0N/A }
0N/A
0N/A if (aligned_addr != end_addr) {
0N/A char *ptr = (char *)&rslt;
0N/A errno = 0;
0N/A rslt = ptrace(PTRACE_PEEKDATA, ph->pid, aligned_addr, 0);
0N/A if (errno) {
0N/A print_debug("ptrace(PTRACE_PEEKDATA, ..) failed for %d bytes @ %lx\n", size, addr);
0N/A return false;
0N/A }
0N/A for (; aligned_addr != end_addr; aligned_addr++)
0N/A *(buf++) = *(ptr++);
0N/A }
0N/A return true;
0N/A}
0N/A
0N/A// null implementation for write
0N/Astatic bool process_write_data(struct ps_prochandle* ph,
0N/A uintptr_t addr, const char *buf , size_t size) {
0N/A return false;
0N/A}
0N/A
0N/A// "user" should be a pointer to a user_regs_struct
0N/Astatic bool process_get_lwp_regs(struct ps_prochandle* ph, pid_t pid, struct user_regs_struct *user) {
0N/A // we have already attached to all thread 'pid's, just use ptrace call
0N/A // to get regset now. Note that we don't cache regset upfront for processes.
0N/A// Linux on x86 and sparc are different. On x86 ptrace(PTRACE_GETREGS, ...)
0N/A// uses pointer from 4th argument and ignores 3rd argument. On sparc it uses
0N/A// pointer from 3rd argument and ignores 4th argument
0N/A#if defined(sparc) || defined(sparcv9)
0N/A#define ptrace_getregs(request, pid, addr, data) ptrace(request, pid, addr, data)
0N/A#else
0N/A#define ptrace_getregs(request, pid, addr, data) ptrace(request, pid, data, addr)
0N/A#endif
0N/A
1897N/A#if defined(_LP64) && defined(PTRACE_GETREGS64)
0N/A#define PTRACE_GETREGS_REQ PTRACE_GETREGS64
1897N/A#elif defined(PTRACE_GETREGS)
1897N/A#define PTRACE_GETREGS_REQ PTRACE_GETREGS
1897N/A#elif defined(PT_GETREGS)
1897N/A#define PTRACE_GETREGS_REQ PT_GETREGS
0N/A#endif
0N/A
0N/A#ifdef PTRACE_GETREGS_REQ
0N/A if (ptrace_getregs(PTRACE_GETREGS_REQ, pid, user, NULL) < 0) {
0N/A print_debug("ptrace(PTRACE_GETREGS, ...) failed for lwp %d\n", pid);
0N/A return false;
0N/A }
0N/A return true;
0N/A#else
0N/A print_debug("ptrace(PTRACE_GETREGS, ...) not supported\n");
0N/A return false;
0N/A#endif
0N/A
0N/A}
0N/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 while (true) {
4204N/A // Wait for debuggee to stop.
4204N/A ret = waitpid(pid, &status, 0);
4204N/A if (ret == -1 && errno == ECHILD) {
4204N/A // try cloned process.
4204N/A ret = waitpid(pid, &status, __WALL);
4204N/A }
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 break;
4204N/A }
4204N/A return false;
4204N/A }
4204N/A }
4204N/A}
4204N/A
0N/A// attach to a process/thread specified by "pid"
0N/Astatic bool ptrace_attach(pid_t pid) {
0N/A if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) < 0) {
0N/A print_debug("ptrace(PTRACE_ATTACH, ..) failed for %d\n", pid);
0N/A return false;
0N/A } else {
4204N/A return ptrace_waitpid(pid);
0N/A }
0N/A}
0N/A
0N/A// -------------------------------------------------------
0N/A// functions for obtaining library information
0N/A// -------------------------------------------------------
0N/A
0N/A/*
0N/A * splits a string _str_ into substrings with delimiter _delim_ by replacing old * delimiters with _new_delim_ (ideally, '\0'). the address of each substring
0N/A * is stored in array _ptrs_ as the return value. the maximum capacity of _ptrs_ * array is specified by parameter _n_.
0N/A * RETURN VALUE: total number of substrings (always <= _n_)
0N/A * NOTE: string _str_ is modified if _delim_!=_new_delim_
0N/A */
0N/Astatic int split_n_str(char * str, int n, char ** ptrs, char delim, char new_delim)
0N/A{
0N/A int i;
0N/A for(i = 0; i < n; i++) ptrs[i] = NULL;
0N/A if (str == NULL || n < 1 ) return 0;
0N/A
0N/A i = 0;
0N/A
0N/A // skipping leading blanks
0N/A while(*str&&*str==delim) str++;
0N/A
0N/A while(*str&&i<n){
0N/A ptrs[i++] = str;
0N/A while(*str&&*str!=delim) str++;
0N/A while(*str&&*str==delim) *(str++) = new_delim;
0N/A }
0N/A
0N/A return i;
0N/A}
0N/A
0N/A/*
0N/A * fgets without storing '\n' at the end of the string
0N/A */
0N/Astatic char * fgets_no_cr(char * buf, int n, FILE *fp)
0N/A{
0N/A char * rslt = fgets(buf, n, fp);
0N/A if (rslt && buf && *buf){
0N/A char *p = strchr(buf, '\0');
0N/A if (*--p=='\n') *p='\0';
0N/A }
0N/A return rslt;
0N/A}
0N/A
0N/A// callback for read_thread_info
0N/Astatic bool add_new_thread(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) {
0N/A return add_thread_info(ph, pthread_id, lwp_id) != NULL;
0N/A}
0N/A
0N/Astatic bool read_lib_info(struct ps_prochandle* ph) {
0N/A char fname[32];
0N/A char buf[256];
0N/A FILE *fp = NULL;
0N/A
0N/A sprintf(fname, "/proc/%d/maps", ph->pid);
0N/A fp = fopen(fname, "r");
0N/A if (fp == NULL) {
0N/A print_debug("can't open /proc/%d/maps file\n", ph->pid);
0N/A return false;
0N/A }
0N/A
0N/A while(fgets_no_cr(buf, 256, fp)){
0N/A char * word[6];
0N/A int nwords = split_n_str(buf, 6, word, ' ', '\0');
0N/A if (nwords > 5 && find_lib(ph, word[5]) == false) {
0N/A intptr_t base;
0N/A lib_info* lib;
1601N/A#ifdef _LP64
0N/A sscanf(word[0], "%lx", &base);
1601N/A#else
1601N/A sscanf(word[0], "%x", &base);
1601N/A#endif
0N/A if ((lib = add_lib_info(ph, word[5], (uintptr_t)base)) == NULL)
0N/A continue; // ignore, add_lib_info prints error
0N/A
0N/A // we don't need to keep the library open, symtab is already
0N/A // built. Only for core dump we need to keep the fd open.
0N/A close(lib->fd);
0N/A lib->fd = -1;
0N/A }
0N/A }
0N/A fclose(fp);
0N/A return true;
0N/A}
0N/A
0N/A// detach a given pid
0N/Astatic bool ptrace_detach(pid_t pid) {
0N/A if (pid && ptrace(PTRACE_DETACH, pid, NULL, NULL) < 0) {
0N/A print_debug("ptrace(PTRACE_DETACH, ..) failed for %d\n", pid);
0N/A return false;
0N/A } else {
0N/A return true;
0N/A }
0N/A}
0N/A
0N/A// detach all pids of a ps_prochandle
0N/Astatic void detach_all_pids(struct ps_prochandle* ph) {
0N/A thread_info* thr = ph->threads;
0N/A while (thr) {
0N/A ptrace_detach(thr->lwp_id);
0N/A thr = thr->next;
0N/A }
0N/A}
0N/A
0N/Astatic void process_cleanup(struct ps_prochandle* ph) {
0N/A detach_all_pids(ph);
0N/A}
0N/A
0N/Astatic ps_prochandle_ops process_ops = {
50N/A .release= process_cleanup,
50N/A .p_pread= process_read_data,
50N/A .p_pwrite= process_write_data,
50N/A .get_lwp_regs= process_get_lwp_regs
0N/A};
0N/A
0N/A// attach to the process. One and only one exposed stuff
0N/Astruct ps_prochandle* Pgrab(pid_t pid) {
0N/A struct ps_prochandle* ph = NULL;
0N/A thread_info* thr = NULL;
0N/A
0N/A if ( (ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle))) == NULL) {
0N/A print_debug("can't allocate memory for ps_prochandle\n");
0N/A return NULL;
0N/A }
0N/A
0N/A if (ptrace_attach(pid) != true) {
0N/A free(ph);
0N/A return NULL;
0N/A }
0N/A
0N/A // initialize ps_prochandle
0N/A ph->pid = pid;
0N/A
0N/A // initialize vtable
0N/A ph->ops = &process_ops;
0N/A
0N/A // read library info and symbol tables, must do this before attaching threads,
0N/A // as the symbols in the pthread library will be used to figure out
0N/A // the list of threads within the same process.
0N/A read_lib_info(ph);
0N/A
0N/A // read thread info
0N/A read_thread_info(ph, add_new_thread);
0N/A
0N/A // attach to the threads
0N/A thr = ph->threads;
0N/A while (thr) {
0N/A // don't attach to the main thread again
0N/A if (ph->pid != thr->lwp_id && ptrace_attach(thr->lwp_id) != true) {
0N/A // even if one attach fails, we get return NULL
0N/A Prelease(ph);
0N/A return NULL;
0N/A }
0N/A thr = thr->next;
0N/A }
0N/A return ph;
0N/A}