-
Notifications
You must be signed in to change notification settings - Fork 0
/
dumpphys.c
295 lines (268 loc) · 8.07 KB
/
dumpphys.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
* Dump the (uncompressed) physical memory from a core out to stdout.
* Alternatively, search for a vmcoreinfo note inside that physical memory and
* output it if we find it.
*
* gcc -g -o dumphys{,.c} -lkdumpfile
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <fcntl.h>
#include <assert.h>
#include <string.h>
#include <getopt.h>
#include <libkdumpfile/kdumpfile.h>
#include <unistd.h>
#define GB (1UL << 30)
#define MB (1UL << 20)
void fail(const char *fmt, ...)
{
va_list vl;
va_start(vl, fmt);
vfprintf(stderr, fmt, vl);
va_end(vl);
exit(EXIT_FAILURE);
}
void perror_fail(const char *msg)
{
perror(msg);
exit(EXIT_FAILURE);
}
struct memory_info {
kdump_num_t max_pfn;
kdump_num_t page_size;
kdump_num_t page_shift;
kdump_bmp_t *pagemap;
};
void get_memory_info(kdump_ctx_t *ctx, struct memory_info *meminfo)
{
kdump_status ks;
kdump_attr_t bitmap_attr;
ks = kdump_get_number_attr(ctx, "max_pfn", &meminfo->max_pfn);
if (ks != KDUMP_OK)
fail("kdump_get_number_attr(max_pfn): %s\n",
kdump_get_err(ctx));
ks = kdump_get_number_attr(ctx, KDUMP_ATTR_PAGE_SIZE, &meminfo->page_size);
if (ks != KDUMP_OK)
fail("kdump_get_number_attr(KDUMP_ATTR_PAGE_SIZE): %s\n",
kdump_get_err(ctx));
if (meminfo->page_size == 0) {
fprintf(stderr, "warning: page_size set to zero, using a default of 4096\n");
meminfo->page_size = 4096;
}
ks = kdump_get_number_attr(ctx, KDUMP_ATTR_PAGE_SHIFT, &meminfo->page_shift);
if (ks != KDUMP_OK)
fail("kdump_get_number_attr(KDUMP_ATTR_PAGE_SHIFT): %s\n",
kdump_get_err(ctx));
ks = kdump_get_attr(ctx, KDUMP_ATTR_FILE_PAGEMAP, &bitmap_attr);
if (ks != KDUMP_OK)
fail("kdump_get_attr(KDUMP_ATTR_FILE_PAGEMAP): %s\n",
kdump_get_err(ctx));
assert(bitmap_attr.type == KDUMP_BITMAP);
meminfo->pagemap = bitmap_attr.val.bitmap;
}
typedef int (*page_fn)(void *, uint64_t addr, uint64_t len, uint8_t *buf);
int dump_page(void *arg, uint64_t addr, uint64_t len, uint8_t *buf)
{
int *pFd = arg;
int rv = write(*pFd, buf, len);
return (rv < 0) ? -1 : 0;
}
struct vmcoreinfo_arg {
int fd;
bool should_continue;
int found_count;
bool search_within_page;
kdump_ctx_t *ctx;
};
int check_vmcoreinfo(void *varg, uint64_t addr, uint64_t len, uint8_t *buf)
{
struct vmcoreinfo_arg *arg = varg;
void *found;
if (!arg->search_within_page && strncmp("OSRELEASE=", (char *)buf, 10) == 0) {
int vmcoreinfo_len = strlen((char *)buf);
write(arg->fd, (char *)buf, vmcoreinfo_len);
arg->found_count += 1;
if (arg->should_continue)
write(arg->fd, "---\n", 4);
else
return -1;
} else if (arg->search_within_page){
if(!(found = memmem(buf, len, "OSRELEASE=", 10)))
return 0;
char *str;
kdump_status st = kdump_read_string(arg->ctx, KDUMP_MACHPHYSADDR,
addr + (found - (void *)buf), &str);
if (st != KDUMP_OK) {
fprintf(stderr, "error reading string: %s\n", kdump_strerror(st));
return -1;
}
write(arg->fd, str, strlen(str));
free(str);
if (arg->should_continue)
write(arg->fd, "---\n", 4);
else
return -1;
}
return 0;
}
int count_pages(kdump_ctx_t *ctx, struct memory_info *mi, page_fn fn, void *arg, bool verbose, bool persist)
{
kdump_status ks;
kdump_addr_t addr = 0;
bool nodata = false;
kdump_addr_t max_addr = mi->max_pfn << mi->page_shift;
uint64_t pages_read = 0;
char *buf = malloc(mi->page_size);
while (1) {
kdump_addr_t begin, end;
ks = kdump_bmp_find_set(mi->pagemap, &addr);
if (ks == KDUMP_ERR_NODATA)
break;
if (ks != KDUMP_OK) {
fail("kdump_bmp_find_set: %s", kdump_bmp_get_err(mi->pagemap));
}
begin = addr;
ks = kdump_bmp_find_clear(mi->pagemap, &addr);
if (ks != KDUMP_OK) {
fail("kdump_bmp_find_set: %s", kdump_bmp_get_err(mi->pagemap));
}
end = addr;
if (verbose)
fprintf(stderr, "Data present range: page frames 0x%lx - 0x%lx\n", begin, end);
for (kdump_addr_t offset = begin; offset < end; offset++) {
size_t len = mi->page_size;
ks = kdump_read(ctx, KDUMP_MACHPHYSADDR, offset << mi->page_shift, buf, &len);
if (ks != KDUMP_OK) {
fprintf(stderr, "kdump_read: %s\n", kdump_get_err(ctx));
if (!persist)
exit(EXIT_FAILURE);
continue;
}
pages_read += 1;
int rv = fn(arg, offset << mi->page_shift, len, (uint8_t *)buf);
if (rv < 0) {
free(buf);
return rv;
}
}
}
if (verbose)
fprintf(stderr, "Processed %lu present pages (total: %lu). That's %lu MiB / %lu MiB\n",
pages_read, mi->max_pfn,
(pages_read << mi->page_shift) / MB, (mi->max_pfn << mi->page_shift) / MB);
return 0;
}
void help(void)
{
puts(
"usage: dumpphys [OPTIONS] -c VMCORE [-o OUTPUT]\n"
"\n"
"Dumps raw memory contents from a vmcore (ELF or kdump) to stdout, or to the\n"
"file indicated by OUTPUT. Alternatively, if --vmcoreinfo is provided, searches\n"
"for any page that looks like a vmcoreinfo page and outputs the first one to\n"
"stdout or the file indicated by OUTPUT.\n"
"\n"
" -c, --core VMCORE specifies the vmcore to read (required)\n"
" -o, --output OUTPUT specifies where to write output (default: stdout)\n"
" --vmcoreinfo, -i if flag is active, searches for vmcoreinfo rather than\n"
" dumping all memory contents.\n"
" -I same as -i, but keeps searching after finding one\n"
" --flexible, -f when searching for vmcoreinfo, also search outside of\n"
" page boundaries. Useful for older kernels\n"
" --verbose, -v prints information about progress to stderr\n"
" --persist, -p continue trying to read pages of data even after we\n"
" encounter a read error\n"
" --help, -h print this message and exit\n"
"\n"
"Stephen Brennan <stephen.s.brennan@oracle.com>"
);
exit(EXIT_SUCCESS);
}
int main(int argc, char **argv)
{
kdump_ctx_t *ctx;
kdump_status ks;
struct memory_info mi;
int in_fd = -1;
int rv;
page_fn op = dump_page;
bool verbose = false;
bool persist = false;
struct vmcoreinfo_arg via = {
.fd = STDOUT_FILENO,
.should_continue = false,
.search_within_page = false,
};
int opt;
const char *shopt = "c:o:iIvhpf";
static struct option lopt[] = {
{"core", required_argument, NULL, 'c'},
{"output", required_argument, NULL, 'o'},
{"vmcoreinfo", no_argument, NULL, 'i'},
{"verbose", no_argument, NULL, 'v'},
{"help", no_argument, NULL, 'h'},
{"persist", no_argument, NULL, 'p'},
{"flexible", no_argument, NULL, 'f'},
{0},
};
while ((opt = getopt_long(argc, argv, shopt, lopt, NULL)) != -1) {
switch (opt) {
case 'h':
help();
break;
case 'c':
in_fd = open(optarg, O_RDONLY);
if (in_fd < 0)
perror_fail("open vmcore");
break;
case 'o':
via.fd = open(optarg, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (via.fd < 0)
perror_fail("open output");
break;
case 'I':
via.should_continue = true;
case 'i':
op = check_vmcoreinfo;
break;
case 'v':
verbose = true;
break;
case 'p':
persist = true;
break;
case 'f':
via.search_within_page = true;
break;
default:
fprintf(stderr, "Invalid argument\n");
exit(EXIT_FAILURE);
}
}
if (in_fd < 0)
fail("--core is a required argument! See -h for help output.");
ctx = kdump_new();
if (!ctx)
fail("kdump_new() failed\n");
via.ctx = ctx;
ks = kdump_set_number_attr(ctx, KDUMP_ATTR_FILE_FD, in_fd);
if (ks != KDUMP_OK)
fail("kdump_set_number_attr(KDUMP_ATTR_FILE_FD): %s\n",
kdump_get_err(ctx));
ks = kdump_set_string_attr(ctx, KDUMP_ATTR_OSTYPE, "linux");
if (ks != KDUMP_OK)
fail("kdump_set_string_attr(KDUMP_ATTR_OSTYPE): %s\n",
kdump_get_err(ctx));
get_memory_info(ctx, &mi);
rv = count_pages(ctx, &mi, op, &via, verbose, persist);
if (op == check_vmcoreinfo && via.found_count > 1)
fprintf(stderr, "found %d vmcoreinfo-like notes\n", via.found_count);
else if (rv == 0 && via.found_count == 0 && op == check_vmcoreinfo)
fprintf(stderr, "error: could not find anything that looks like vmcoreinfo\n");
kdump_free(ctx);
return rv ? EXIT_FAILURE : EXIT_SUCCESS;
}