/*
* 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 <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <elf.h>
#include <sys/elf_common.h>
#include <sys/link_elf.h>
#include <libutil.h>
#include "libproc_impl.h"
#include "elfmacros.h"
// 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.
int rslt;
if (aligned_addr != addr) {
errno = 0;
if (errno) {
return false;
}
aligned_addr++)
}
// assert((intptr_t)aligned_addr % sizeof(int) == 0);
for (i = 0; i < words; i++) {
errno = 0;
if (errno) {
return false;
}
buf += sizeof(int);
aligned_addr += sizeof(int);
}
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 reg
// 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.
return false;
}
return true;
}
// fill in ptrace_lwpinfo for lid
errno = 0;
return (errno == 0)? true: false;
}
// 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;
do {
// Wait for debuggee to stop.
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:
}
return false;
}
} while(true);
}
return false;
} else {
return ptrace_waitpid(pid);
}
}
// -------------------------------------------------------
// functions for obtaining library information
// -------------------------------------------------------
// callback for read_thread_info
}
/*
* TEXT_START_ADDR from binutils/ld/emulparams/<arch_spec>.sh
* Not the most robust but good enough.
*/
#else
#endif
unsigned long hdrs_size;
unsigned int i;
/* read ELF_EHDR at TEXT_START_ADDR and validate */
return (0);
}
return (0);
}
/* allocate space for all ELF_PHDR's and read */
return (0);
return (0);
}
/* find PT_DYNAMIC section */
break;
}
print_debug("PT_DYNAMIC section not found!\n");
return (0);
}
/* allocate space and read in ELF_DYN headers */
return (0);
return (0);
}
/* find DT_DEBUG */
dyn++;
}
print_debug("failed to find DT_DEBUG\n");
return (0);
}
/* read struct r_debug into dmap */
return (0);
}
return (lmap_addr);
}
#endif // __FreeBSD__ && __FreeBSD_version < 701000
int i, cnt;
return false;
}
for (i = 0; i < cnt; i++) {
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;
#else
char *l_name;
return false;
return false;
}
if (lmap_addr == 0) {
return false;
}
do {
return false;
}
BUF_SIZE) != true) {
print_debug("process_read_data failed for lmap->l_name %p\n",
return false;
}
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;
#endif
}
// detach a given pid
return false;
} else {
return true;
}
}
}
};
// 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.
if (read_lib_info(ph) != true) {
return NULL;
}
// read thread info
return ph;
}