ps_proc.c revision 4204
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include "libproc_impl.h"
#define amd64 1
#endif
#ifndef __WALL
#endif
// This file has the libproc implementation specific to live process
// For core files, refer to ps_core.c
}
// ---------------------------------------------
// ptrace functions
// ---------------------------------------------
// read "size" bytes of data from "addr" within the target process.
// unlike the standard ptrace() function, process_read_data() can handle
// unaligned address - alignment check, if required, should be done
// before calling process_read_data.
long rslt;
if (aligned_addr != addr) {
errno = 0;
if (errno) {
return false;
}
aligned_addr++)
}
// assert((intptr_t)aligned_addr % sizeof(long) == 0);
for (i = 0; i < words; i++) {
errno = 0;
if (errno) {
return false;
}
buf += sizeof(long);
aligned_addr += sizeof(long);
}
if (aligned_addr != end_addr) {
errno = 0;
if (errno) {
return false;
}
}
return true;
}
// null implementation for write
return false;
}
// "user" should be a pointer to a user_regs_struct
static bool process_get_lwp_regs(struct ps_prochandle* ph, pid_t pid, struct user_regs_struct *user) {
// we have already attached to all thread 'pid's, just use ptrace call
// to get regset now. Note that we don't cache regset upfront for processes.
// Linux on x86 and sparc are different. On x86 ptrace(PTRACE_GETREGS, ...)
// uses pointer from 4th argument and ignores 3rd argument. On sparc it uses
// pointer from 3rd argument and ignores 4th argument
#else
#endif
#if defined(_LP64) && defined(PTRACE_GETREGS64)
#define PTRACE_GETREGS_REQ PTRACE_GETREGS64
#elif defined(PTRACE_GETREGS)
#define PTRACE_GETREGS_REQ PTRACE_GETREGS
#elif defined(PT_GETREGS)
#define PTRACE_GETREGS_REQ PT_GETREGS
#endif
#ifdef PTRACE_GETREGS_REQ
return false;
}
return true;
#else
print_debug("ptrace(PTRACE_GETREGS, ...) not supported\n");
return false;
#endif
}
// pass the signal to the process so we don't swallow it
return false;
}
return true;
}
// waits until the ATTACH has stopped the process
// by signal SIGSTOP
int ret;
int status;
while (true) {
// Wait for debuggee to stop.
// try cloned process.
}
if (ret >= 0) {
if (WIFSTOPPED(status)) {
// Any signal will stop the thread, make sure it is SIGSTOP. Otherwise SIGSTOP
// will still be pending and delivered when the process is DETACHED and the process
// will go to sleep.
// Debuggee stopped by SIGSTOP.
return true;
}
print_error("Failed to correctly attach to VM. VM might HANG! [PTRACE_CONT failed, stopped by %d]\n", WSTOPSIG(status));
return false;
}
} else {
return false;
}
} else {
switch (errno) {
case EINTR:
continue;
break;
case ECHILD:
break;
case EINVAL:
print_debug("waitpid() failed. Invalid options argument.\n");
break;
default:
break;
}
return false;
}
}
}
return false;
} else {
return ptrace_waitpid(pid);
}
}
// -------------------------------------------------------
// functions for obtaining library information
// -------------------------------------------------------
/*
* splits a string _str_ into substrings with delimiter _delim_ by replacing old * delimiters with _new_delim_ (ideally, '\0'). the address of each substring
* is stored in array _ptrs_ as the return value. the maximum capacity of _ptrs_ * array is specified by parameter _n_.
* RETURN VALUE: total number of substrings (always <= _n_)
* NOTE: string _str_ is modified if _delim_!=_new_delim_
*/
{
int i;
i = 0;
// skipping leading blanks
while(*str&&i<n){
}
return i;
}
/*
* fgets without storing '\n' at the end of the string
*/
{
if (*--p=='\n') *p='\0';
}
return rslt;
}
// callback for read_thread_info
}
char fname[32];
char buf[256];
return false;
}
char * word[6];
#ifdef _LP64
#else
#endif
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.
}
}
return true;
}
// detach a given pid
return false;
} else {
return true;
}
}
// detach all pids of a ps_prochandle
while (thr) {
}
}
}
static ps_prochandle_ops process_ops = {
};
// attach to the process. One and only one exposed stuff
print_debug("can't allocate memory for ps_prochandle\n");
return NULL;
}
if (ptrace_attach(pid) != true) {
return NULL;
}
// initialize ps_prochandle
// initialize vtable
// 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 thread info
// attach to the threads
while (thr) {
// don't attach to the main thread again
// even if one attach fails, we get return NULL
return NULL;
}
}
return ph;
}