From owner-freebsd-questions Mon Feb 4 11: 5:44 2002 Delivered-To: freebsd-questions@freebsd.org Received: from bingnet2.cc.binghamton.edu (bingnet2.cc.binghamton.edu [128.226.1.18]) by hub.freebsd.org (Postfix) with ESMTP id B51B537B429 for ; Mon, 4 Feb 2002 11:05:31 -0800 (PST) Received: from onyx (onyx.cs.binghamton.edu [128.226.140.171]) by bingnet2.cc.binghamton.edu (8.11.6/8.11.6) with ESMTP id g14J5Uq13694 for ; Mon, 4 Feb 2002 14:05:31 -0500 (EST) Date: Mon, 4 Feb 2002 14:04:20 -0500 (EST) From: Zhihui Zhang X-Sender: zzhang@onyx To: freebsd-questions@freebsd.org Subject: A simple ptrace program Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG 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 #include #include #include #include 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