memzap

replay memory writes
git clone git://git.2f30.org/memzap
Log | Files | Refs | README | LICENSE

commit 094b772eb61826a92baa02275769ac137991ad65
parent 5b8fa897dd2f03a55fc1509aef50e86f5e4d6e34
Author: sin <sin@2f30.org>
Date:   Fri,  1 Mar 2013 12:53:46 +0000

ops: Add traceme() and single_step()

Add these two wrappers to make things easier when porting to
OpenBSD.  The ptrace request names are slightly different on
OpenBSD and we don't want to use #ifdefs.

Diffstat:
Mdata.h | 2++
Mlinux_ops.c | 22++++++++++++++++++++++
Mmemzap.c | 14++++----------
3 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/data.h b/data.h @@ -134,6 +134,8 @@ void dump_mem_region_diff(struct mem_region_diff *rdiff); void free_mem_region_diff(struct mem_region_diff *rdiff); /* *_ops.c */ +int traceme(void); +int single_step(pid_t pid); void readmem(pid_t pid, void *buf, off_t offset, size_t size); #endif diff --git a/linux_ops.c b/linux_ops.c @@ -2,6 +2,28 @@ #include "data.h" +int +traceme(void) +{ + int ret; + + ret = ptrace(PTRACE_TRACEME, 0, NULL, NULL); + if (ret < 0) + err(1, "ptrace"); + return ret; +} + +int +single_step(pid_t pid) +{ + int ret; + + ret = ptrace(PTRACE_SINGLESTEP, pid, NULL, NULL); + if (ret < 0) + err(1, "ptrace"); + return ret; +} + void readmem(pid_t pid, void *buf, off_t offset, size_t size) { diff --git a/memzap.c b/memzap.c @@ -10,7 +10,7 @@ main(int argc, char *argv[]) struct mem_region *mr_old, *mr_new; struct mem_tree *mt_old; struct mem_region_diff *rdiff; - int ret, stat; + int stat; pid_t pid; unsigned char *buf, *buf_new; char *addr; @@ -36,9 +36,7 @@ main(int argc, char *argv[]) switch (pid) { case 0: - ret = ptrace(PTRACE_TRACEME, 0, NULL, NULL); - if (ret < 0) - err(1, "ptrace"); + traceme(); execl(argv[1], argv[1], (char *)NULL); _Exit(1); default: @@ -56,9 +54,7 @@ main(int argc, char *argv[]) printf("Single stepping child with pid %jd\n", (intmax_t)pid); - ret = ptrace(PTRACE_SINGLESTEP, pid, NULL, NULL); - if (ret < 0) - err(1, "ptrace"); + single_step(pid); waitpid(pid, &stat, WSTOPPED); if (!WIFSTOPPED(stat)) @@ -74,9 +70,7 @@ main(int argc, char *argv[]) if (!mt_old) errx(1, "Failed to build memory tree\n"); - ret = ptrace(PTRACE_SINGLESTEP, pid, NULL, NULL); - if (ret < 0) - err(1, "ptrace"); + single_step(pid); waitpid(pid, &stat, WSTOPPED); if (!WIFSTOPPED(stat)) {