-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemdump.c
More file actions
37 lines (29 loc) · 781 Bytes
/
memdump.c
File metadata and controls
37 lines (29 loc) · 781 Bytes
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
/* vim: set ft=c showbreak=»\ noexpandtab fileencoding=utf-8 nomodified wrap textwidth=0 foldmethod=marker foldmarker={{{,}}} foldcolumn=4 ruler showcmd lcs=tab\:|- list: tabstop=8 linebreak */
// ,,g = gcc, exactly one space after "set"
#if defined(__PC__)
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
int memdump(uintptr_t addr, size_t size, const char *fname) {
FILE *f = fopen(fname, "ab");
if (!f) {
perror("fopen");
return -1;
};
void *ptr = (void *)addr;
if (0 >= fprintf(f,"<==== 0x%08x + 0x%04x ====>",addr,size)) {
perror("fprintf");
fclose(f);
return -1;
};
size_t written = fwrite(ptr, 1, size, f);
if (written != size) {
perror("fwrite");
fclose(f);
return -1;
};
fclose(f);
return 0;
};
#endif