9 static int open_kmem(int rdwr
) {
10 int fd
= open("/dev/kmem", rdwr
? O_RDWR
: O_RDONLY
);
14 static int seek_kmem(int fd
, off_t offset
) {
15 if(-1==lseek(fd
, offset
, SEEK_SET
)) {
22 static int find_sym(const char *symname
, unsigned long *off
, unsigned long *end
) {
23 FILE *f
= fopen("/proc/kallsyms", "r");
26 size_t l
= strlen(symname
);
28 while(fgets(buf
, sizeof buf
, f
)) {
31 if(1!=sscanf(p
, "%lx", end
)) succ
= 0;
34 while(*p
&& *p
!= ' ') p
++;
36 if(*p
!= 'D' && *p
!= 'T') continue;
38 if(!strncmp(p
, symname
, l
)) {
40 while(isspace(*p
))p
++;
41 if(1!=sscanf(p
, "%lx", off
)) goto ret
;
50 int main(int argc
, char **argv
){
51 unsigned long off
, end
;
52 if(argc
!= 2) return 1;
54 FILE *f
= fopen("dump.bin", "w");
60 if(!find_sym(argv
[1], &off
, &end
)) {
61 printf("couldnt find offsets\n");
65 int fd
= open_kmem(0);
66 if(!seek_kmem(fd
, off
)) {
67 printf("kmem seek failed\n");
71 size_t left
, toread
, tot
= end
- off
;
74 toread
= left
>= sizeof(buf
) ? sizeof(buf
) : left
;
76 if(toread
!= (n
= read(fd
, buf
, toread
))) {
80 fwrite(buf
, toread
, 1, f
);
83 printf("dumped %zu bytes to dump.bin\n", tot
);