commit 6c668ce4b667ea2dd1ffa6095f432a5238d6d4b5
parent 06ca031645eab753df49d17d83d38f276c88fae4
Author: sin <sin@2f30.org>
Date: Mon, 4 Mar 2013 14:27:56 +0000
memzap: Add some comments throughout the code
Diffstat:
1 file changed, 18 insertions(+), 0 deletions(-)
diff --git a/memzap.c b/memzap.c
@@ -121,6 +121,7 @@ main(int argc, char *argv[])
if (!WIFSTOPPED(stat))
goto out_mmap;
+ /* Create .mdiff file and initialize the header */
snprintf(mdiff_path, sizeof(mdiff_path),
"%s.mdiff", *argv);
fd = create_mdiff(mdiff_path);
@@ -130,20 +131,28 @@ main(int argc, char *argv[])
hdr.endianness = 0;
hdr.version = 1;
mdiff_start_diff(fd);
+
+ /* Trace the child process and diff the memory regions
+ * per instruction */
do {
+ /* Read in the Nth generation of the traced region */
readmem(pid, buf, addr, len);
if (!once) {
dump_base_image(*argv, buf, len);
once = true;
}
+ /* Build a memory region that tracks this buffer */
mr_old = build_mem_region(buf, len);
if (!mr_old)
errx(1, "Failed to build memory region\n");
+ /* Build an rbtree that tracks this memory region */
mt_old = build_mem_tree(mr_old);
if (!mt_old)
errx(1, "Failed to build memory tree\n");
+ /* Single step the child in order to get the next
+ * generation */
single_step(pid);
wait(&stat);
if (!WIFSTOPPED(stat)) {
@@ -152,23 +161,32 @@ main(int argc, char *argv[])
break;
}
+ /* Read in the (N + 1)th generation of the traced
+ * region */
readmem(pid, buf_new, addr, len);
+ /* Build a memory region that tracks this buffer */
mr_new = build_mem_region(buf_new, len);
if (!mr_new)
errx(1, "Failed to build memory region\n");
+
+ /* Diff the original copy with the updated copy */
rdiff = diff_mem_region(mt_old, mr_new);
if (rdiff->nmrdiffs) {
+ /* If we've found differences, then apply them on
+ * the Nth generation */
apply_diff(mr_old, rdiff);
hdr.nregions += mdiff_append_rdiff(fd, rdiff);
}
+ /* Free these and go up for another run */
free_mem_region(mr_old);
free_mem_tree(mt_old);
free_mem_region(mr_new);
free_mem_region_diff(rdiff);
} while(1);
+ /* Insert header at the start of the .mdiff file */
mdiff_insert_hdr(fd, &hdr);
close_mdiff(fd);
out_mmap: