memzap

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

linux_ops.c (815B)


      1 /* See LICENSE file for copyright and license details. */
      2 
      3 #include "proto.h"
      4 
      5 int
      6 traceme(void)
      7 {
      8 	int ret;
      9 
     10 	ret = ptrace(PTRACE_TRACEME, 0, NULL, NULL);
     11 	if (ret < 0)
     12 		err(1, "ptrace");
     13 	return ret;
     14 }
     15 
     16 int
     17 single_step(pid_t pid)
     18 {
     19 	int ret;
     20 
     21 	ret = ptrace(PTRACE_SINGLESTEP, pid, NULL, NULL);
     22 	if (ret < 0)
     23 		err(1, "ptrace");
     24 	return ret;
     25 }
     26 
     27 void
     28 readmem(pid_t pid, void *buf, void *offset, size_t size)
     29 {
     30 	char tbuf[PATH_MAX];
     31 	int fdmem;
     32 	int r;
     33 	ssize_t rs;
     34 	size_t s;
     35 
     36 	snprintf(tbuf, sizeof(tbuf), "/proc/%jd/mem", (intmax_t)pid);
     37 	fdmem = open(tbuf, O_RDONLY);
     38 	if (fdmem < 0)
     39 		err(1, "open");
     40 
     41 	r = lseek(fdmem, (off_t)offset, SEEK_SET);
     42 	if (r < 0)
     43 		err(1, "lseek");
     44 
     45 	s = 0;
     46 	while (s < size) {
     47 		rs = read(fdmem, buf + s, size - s);
     48 		if (rs < 0)
     49 			err(1, "read");
     50 		s += rs;
     51 	}
     52 
     53 	close(fdmem);
     54 }