Date: Mon, 4 Feb 2002 14:04:20 -0500 (EST) From: Zhihui Zhang <zzhang@cs.binghamton.edu> To: freebsd-questions@freebsd.org Subject: A simple ptrace program Message-ID: <Pine.SOL.4.21.0202041356170.28423-100000@onyx>
index | next in thread | raw e-mail
Yesterday I read the book "Unix operating system" for a while and decided
to give the ptrace example in the book a try on FreeBSD 4.4. This program
is very simple, it basically tries to write into an array of its child
process before its child has a chance to run. But the result is not what
I expected. Maybe the semantics of ptrace changes over the time? Please
help me with this code:
mymachine# cat trace.c
/*
* Filename: trace.c
*/
int data[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
main()
{
int i;
for (i = 0; i < 8; i++)
printf("trace: addr = 0x%x: data[%d] = %d\n",
&data[i], i, data[i]);
printf("ptrace data addr 0x%x\n", &data[0]);
}
/*
* Filename: debug.c
*/
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ptrace.h>
main(int argc, char * argv[])
{
caddr_t addr;
int i, pid, ret, data, status;
if (argc != 2) {
printf("Usage: debug address\n");
exit(0);
}
sscanf(argv[1], "%x", &addr);
printf("Child array address is 0x%x\n", addr);
if ((pid = fork()) == 0) {
ptrace(PT_TRACE_ME, 0, 0, 0);
execl("./trace", "trace", 0); /* l - list */
printf("Fail to execl() child process!\n");
exit(1);
}
ret = waitpid(pid, &status, 0);
printf("ret = %d, status = %d\n", ret, status);
for (i = 0; i < 8; i++) {
/*
* Write value of i into address addr in proc pid.
*/
if (ptrace(PT_WRITE_D, pid, addr, i) == -1)
exit(1);
if (ptrace(PT_READ_D, pid, addr, data) == -1)
exit(1);
printf("debug: addr = 0x%x, data[%d] = %d\n", addr, i, data);
addr += sizeof(int);
}
/*
* Traced process should resume execution.
*/
if (ptrace(PT_CONTINUE, pid, (caddr_t)1, 0) == -1) {
printf("ptrace error = %d\n", errno);
exit(0);
}
} /* end of main() */
mymachine# cc -o trace trace.c
mymachine# cc -o debug debug.c
mymachine# trace
trace: addr = 0x80495c0: data[0] = 0
trace: addr = 0x80495c4: data[1] = 1
trace: addr = 0x80495c8: data[2] = 2
trace: addr = 0x80495cc: data[3] = 3
trace: addr = 0x80495d0: data[4] = 4
trace: addr = 0x80495d4: data[5] = 5
trace: addr = 0x80495d8: data[6] = 6
trace: addr = 0x80495dc: data[7] = 7
ptrace data addr 0x80495c0
mymachine# debug 0x80495c0
Child array address is 0x80495c0
ret = 15512, status = 1407
debug: addr = 0x80495c0, data[0] = -1077937164
debug: addr = 0x80495c4, data[1] = -1077937164
debug: addr = 0x80495c8, data[2] = -1077937164
debug: addr = 0x80495cc, data[3] = -1077937164
debug: addr = 0x80495d0, data[4] = -1077937164
debug: addr = 0x80495d4, data[5] = -1077937164
debug: addr = 0x80495d8, data[6] = -1077937164
debug: addr = 0x80495dc, data[7] = -1077937164
As you can see, the child (trace) does not seem to be running at all.
-Zhihui
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message
help
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.SOL.4.21.0202041356170.28423-100000>
