put the files in a real repo
[tools.git] / tools / viewmem.c
1 #define _GNU_SOURCE
2
3 #include <errno.h>
4 #include <string.h>
5 #include <sys/types.h>
6 #include <dirent.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <stdlib.h>
12
13 int main(int argc, char *argv[]) {
14   if (argc != 3) {
15     fputs("invocation: ./viewmem /proc/<pid>/mem <addr>\n", stderr);
16     return 1;
17   }
18   char *end;
19   errno = 0;
20   unsigned long long addr = strtoull(argv[2], &end, 0);
21   if (errno != 0 || *end != 0) {
22     fputs("invalid addr\n", stderr);
23     return 1;
24   }
25   int fd = open(argv[1], O_RDWR);
26   if (fd == -1) {
27     fprintf(stderr, "error: can't open %s - %s\n", argv[1], strerror(errno));
28     return 1;
29   }
30   fprintf(stderr, "please press the any key...\n");
31   fd = openat(fd, "", 0, O_RDWR);
32   while (getchar() != '\n');
33   fprintf(stderr, "trying to dump...");
34   errno = 0;
35   lseek(fd, addr, SEEK_SET);
36   if (errno != 0) {
37     fprintf(stderr, "lseek() failed: %s\n", strerror(errno));
38     return 1;
39   }
40   char buf[4096]; // nothing interesting has a different pagesize anyway
41   int i=0;
42   while (read(fd, buf, 4096) == 4096) {
43     write(1, buf, 4096);
44     i++;
45   }
46   fprintf(stderr, "read %i pages\n", i);
47   return 0;
48 }