minix/minix/usr.bin/trace/mem.c
David van Moolenbroek 521fa314e2 Add trace(1): the MINIX3 system call tracer
Change-Id: Ib970c8647409196902ed53d6e9631a1673a4ab2e
2014-11-04 21:46:31 +00:00

62 lines
1.2 KiB
C

#include "inc.h"
/*
* Retrieve 'len' bytes from the memory of the traced process 'pid' at address
* 'addr' and put the result in the buffer pointed to by 'ptr'. Return 0 on
* success, or otherwise -1 with errno set appropriately.
*/
int
mem_get_data(pid_t pid, vir_bytes addr, void * ptr, size_t len)
{
struct ptrace_range pr;
if (len == 0) return 0;
pr.pr_space = TS_DATA;
pr.pr_addr = addr;
pr.pr_size = len;
pr.pr_ptr = ptr;
return ptrace(T_GETRANGE, pid, &pr, 0);
}
/*
* Retrieve 'len' bytes from the kernel structure memory of the traced process
* 'pid' at offset 'addr' and put the result in the buffer pointed to by 'ptr'.
* Return 0 on success, or otherwise -1 with errno set appropriately.
*/
int
mem_get_user(pid_t pid, vir_bytes addr, void * ptr, size_t len)
{
long data;
char *p;
size_t off, chunk;
if (len == 0) return 0;
/* Align access to address. */
off = addr & (sizeof(data) - 1);
addr -= off;
p = ptr;
while (len > 0) {
errno = 0;
data = ptrace(T_GETUSER, pid, (void *)addr, 0);
if (errno != 0) return -1;
chunk = sizeof(data) - off;
if (chunk > len)
chunk = len;
memcpy(p, (char *)&data + off, chunk);
p += chunk;
addr += chunk;
len -= chunk;
off = 0;
}
return 0;
}