ps_proc.c revision 1897
0N/A/*
0N/A * Copyright (c) 2003, 2008, 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 *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A *
0N/A */
0N/A
0N/A#include <stdio.h>
0N/A#include <stdlib.h>
0N/A#include <string.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;
1142N/A }
0N/A for (; aligned_addr != end_addr; aligned_addr++)
0N/A *(buf++) = *(ptr++);
0N/A }
0N/A return true;
0N/A}
0N/A
1142N/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
0N/A#if defined(_LP64) && defined(PTRACE_GETREGS64)
0N/A#define PTRACE_GETREGS_REQ PTRACE_GETREGS64
0N/A#elif defined(PTRACE_GETREGS)
0N/A#define PTRACE_GETREGS_REQ PTRACE_GETREGS
0N/A#elif defined(PT_GETREGS)
0N/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
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 {
0N/A int ret;
0N/A int status;
0N/A do {
0N/A // Wait for debuggee to stop.
0N/A ret = waitpid(pid, &status, 0);
0N/A if (ret == -1 && errno == ECHILD) {
0N/A // try cloned process.
0N/A ret = waitpid(pid, &status, __WALL);
0N/A }
0N/A if (ret >= 0) {
0N/A if (WIFSTOPPED(status)) {
0N/A // Debuggee stopped.
0N/A return true;
0N/A } else {
0N/A print_debug("waitpid(): Child process exited/terminated (status = 0x%x)\n", status);
0N/A return false;
0N/A }
0N/A } else {
0N/A switch (errno) {
0N/A case EINTR:
0N/A continue;
0N/A break;
0N/A case ECHILD:
0N/A print_debug("waitpid() failed. Child process pid (%d) does not exist \n", pid);
0N/A break;
0N/A case EINVAL:
0N/A print_debug("waitpid() failed. Invalid options argument.\n");
0N/A break;
0N/A default:
0N/A print_debug("waitpid() failed. Unexpected error %d\n",errno);
0N/A }
0N/A return false;
0N/A }
0N/A } while(true);
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++;
while(*str&&i<n){
ptrs[i++] = str;
while(*str&&*str!=delim) str++;
while(*str&&*str==delim) *(str++) = new_delim;
}
return i;
}
/*
* fgets without storing '\n' at the end of the string
*/
static char * fgets_no_cr(char * buf, int n, FILE *fp)
{
char * rslt = fgets(buf, n, fp);
if (rslt && buf && *buf){
char *p = strchr(buf, '\0');
if (*--p=='\n') *p='\0';
}
return rslt;
}
// callback for read_thread_info
static bool add_new_thread(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) {
return add_thread_info(ph, pthread_id, lwp_id) != NULL;
}
static bool read_lib_info(struct ps_prochandle* ph) {
char fname[32];
char buf[256];
FILE *fp = NULL;
sprintf(fname, "/proc/%d/maps", ph->pid);
fp = fopen(fname, "r");
if (fp == NULL) {
print_debug("can't open /proc/%d/maps file\n", ph->pid);
return false;
}
while(fgets_no_cr(buf, 256, fp)){
char * word[6];
int nwords = split_n_str(buf, 6, word, ' ', '\0');
if (nwords > 5 && find_lib(ph, word[5]) == false) {
intptr_t base;
lib_info* lib;
#ifdef _LP64
sscanf(word[0], "%lx", &base);
#else
sscanf(word[0], "%x", &base);
#endif
if ((lib = add_lib_info(ph, word[5], (uintptr_t)base)) == NULL)
continue; // ignore, add_lib_info prints error
// we don't need to keep the library open, symtab is already
// built. Only for core dump we need to keep the fd open.
close(lib->fd);
lib->fd = -1;
}
}
fclose(fp);
return true;
}
// detach a given pid
static bool ptrace_detach(pid_t pid) {
if (pid && ptrace(PTRACE_DETACH, pid, NULL, NULL) < 0) {
print_debug("ptrace(PTRACE_DETACH, ..) failed for %d\n", pid);
return false;
} else {
return true;
}
}
// detach all pids of a ps_prochandle
static void detach_all_pids(struct ps_prochandle* ph) {
thread_info* thr = ph->threads;
while (thr) {
ptrace_detach(thr->lwp_id);
thr = thr->next;
}
}
static void process_cleanup(struct ps_prochandle* ph) {
detach_all_pids(ph);
}
static ps_prochandle_ops process_ops = {
.release= process_cleanup,
.p_pread= process_read_data,
.p_pwrite= process_write_data,
.get_lwp_regs= process_get_lwp_regs
};
// attach to the process. One and only one exposed stuff
struct ps_prochandle* Pgrab(pid_t pid) {
struct ps_prochandle* ph = NULL;
thread_info* thr = NULL;
if ( (ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle))) == NULL) {
print_debug("can't allocate memory for ps_prochandle\n");
return NULL;
}
if (ptrace_attach(pid) != true) {
free(ph);
return NULL;
}
// initialize ps_prochandle
ph->pid = pid;
// initialize vtable
ph->ops = &process_ops;
// read library info and symbol tables, must do this before attaching threads,
// as the symbols in the pthread library will be used to figure out
// the list of threads within the same process.
read_lib_info(ph);
// read thread info
read_thread_info(ph, add_new_thread);
// attach to the threads
thr = ph->threads;
while (thr) {
// don't attach to the main thread again
if (ph->pid != thr->lwp_id && ptrace_attach(thr->lwp_id) != true) {
// even if one attach fails, we get return NULL
Prelease(ph);
return NULL;
}
thr = thr->next;
}
return ph;
}