-
Notifications
You must be signed in to change notification settings - Fork 1
/
umemdump.c
99 lines (85 loc) · 2.08 KB
/
umemdump.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <fcntl.h>
#include <errno.h>
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr,"usage: %s pid\n",argv[0]);
exit(EXIT_FAILURE);
}
pid_t target_pid;
target_pid = atoi(argv[1]);
char maps_fn[PATH_MAX];
snprintf(maps_fn,PATH_MAX,"/proc/%d/maps",target_pid);
FILE *maps_fp = NULL;
maps_fp = fopen(maps_fn,"r");
if (maps_fp == NULL) {
fprintf(stderr,"can't open %s : %s\n",maps_fn,strerror(errno));
exit(EXIT_FAILURE);
}
char mem_fn[PATH_MAX];
snprintf(mem_fn,PATH_MAX,"/proc/%d/mem",target_pid);
int mem_fd = -1;
mem_fd = open(mem_fn,O_RDONLY);
if (mem_fd < 0) {
fprintf(stderr,"can't open %s : %s\n",mem_fn,strerror(errno));
exit(EXIT_FAILURE);
}
unsigned long long region_start;
unsigned long long region_end;
char line[PATH_MAX];
char *strtemp;
long pagesize = sysconf(_SC_PAGESIZE);
unsigned char buf[pagesize];
ssize_t nr;
off_t offset;
int is_vvar;
int is_vsyscall;
while (fgets(line,PATH_MAX,maps_fp) != NULL) {
if (strstr(line,"[vvar]") != NULL)
is_vvar = 1;
else
is_vvar = 0;
if (strstr(line,"[vsyscall]") != NULL)
is_vsyscall = 1;
else
is_vsyscall = 0;
strtemp = strtok(line,"-");
region_start = strtoull(strtemp,NULL,16);
strtemp = strtok(NULL," ");
region_end = strtoull(strtemp,NULL,16);
offset = region_start;
while (offset < (off_t)region_end) {
nr = pread(mem_fd,buf,pagesize,offset);
if (nr != pagesize) {
if (is_vvar) {
fprintf(stderr,
"in [vvar] region,skip it\n");
goto next_region;
}
if (is_vsyscall) {
fprintf(stderr,
"in [vsyscall] region,skip it\n");
goto next_region;
}
fprintf(stderr,
"can't read %s offset %ld: %s\n",
mem_fn,offset,strerror(errno));
exit(EXIT_FAILURE);
}
if (fwrite(buf,pagesize,1,stdout) != 1) {
fprintf(stderr,"can't write to stdout: %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
offset += pagesize;
}
next_region:
;
}
exit(EXIT_SUCCESS);
}