nextep: Create data directory only if necessary.
[utils.git] / dumpmem.c
... / ...
CommitLineData
1#include <stdio.h>
2#include <unistd.h>
3#include <stdlib.h>
4#include <sys/ptrace.h>
5#include <errno.h>
6
7unsigned char buf[1024];
8
9int main(int argc, unsigned char **argv)
10{
11 pid_t pid;
12 unsigned long addr1, addr2;
13 int buf;
14
15 if(argc < 4)
16 {
17 fprintf(stderr, "Not enough args\n");
18 return(1);
19 }
20 pid = strtol(argv[1], NULL, 0);
21 addr1 = (unsigned long)strtoll(argv[2], NULL, 0);
22 addr2 = (unsigned long)strtoll(argv[3], NULL, 0);
23 if(ptrace(PTRACE_ATTACH, pid, NULL, NULL))
24 {
25 perror("PTRACE_ATTACH");
26 return(1);
27 }
28 for(; addr1 < addr2; addr1 += sizeof(int))
29 {
30 errno = 0;
31 if(((buf = ptrace(PTRACE_PEEKDATA, pid, (void *)addr1, NULL)) == -1) && errno)
32 {
33 perror("PTRACE_PEEKDATA");
34 if(ptrace(PTRACE_DETACH, pid, NULL, NULL))
35 perror("PTRACE_DETACH");
36 return(1);
37 }
38 if(write(1, &buf, sizeof(buf)) != 4)
39 {
40 perror("write");
41 if(ptrace(PTRACE_DETACH, pid, NULL, NULL))
42 perror("PTRACE_DETACH");
43 return(1);
44 }
45 }
46 if(ptrace(PTRACE_DETACH, pid, NULL, NULL))
47 {
48 perror("PTRACE_DETACH");
49 return(1);
50 }
51 return(0);
52}