-
Notifications
You must be signed in to change notification settings - Fork 0
/
crashdmesg_main.c
308 lines (272 loc) · 10.4 KB
/
crashdmesg_main.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
296
297
298
299
300
301
302
303
304
305
306
307
308
/* ======================================================================
crashdmesg - VMCore Kernel Ring Buffer Dumper
[ crashdmesg_main.c ]
Copyright(c) 2011 by Hiroshi KIHIRA.
====================================================================== */
/* --- Include header files --- */
#include "crashdmesg_common.h"
/* --- Prototypes --- */
static void print_usage(void);
static int parse_option(int argc, char *argv[], File *file);
static int crashdmesg(VMCore *vmcore);
/* ============================================================
main() - MAIN
============================================================ */
int main(int argc, char *argv[])
{
/* --- Variables --- */
char estr[] = "[ERROR] main:";
VMCore vmcore;
memset(&vmcore, 0x00, sizeof(VMCore));
fprintf(stdout, "%s: %s start.\n", APP_NAME, APP_NAME);
/* Check args */
if (parse_option(argc, argv, &vmcore.file)) {
fprintf(stderr, "%s Invalid option.\n", estr);
print_usage();
return RETVAL_FAILURE;
}
fprintf(stdout, "%s: Target file: %s\n", APP_NAME, vmcore.file.filename);
/* Do crashdmesg */
if (crashdmesg(&vmcore)) {
fprintf(stderr, "%s Dump Failed.\n", estr);
return RETVAL_FAILURE;
}
return RETVAL_SUCCESS;
}
/* ============================================================
print_usage() - Print command usage to Stdout
============================================================ */
static void print_usage(void)
{
fprintf(stdout, "%s (%s) - %s\n\n", APP_NAME, APP_FULLNAME, APP_VERSION);
fprintf(stdout, "Usage: %s [vmcore]\n", APP_NAME);
fprintf(stdout, " vmcore VMCore file to dump. [/proc/vmcore]\n");
return;
}
/* ============================================================
parse_option() - Parse and validate commandline option
============================================================ */
static int parse_option(int argc, char *argv[], File *file)
{
/* --- Assert check --- */
assert(argv != NULL);
assert(file != NULL);
if (argc == 2) {
file->filename = argv[1];
}
else if (argc == 1) {
file->filename = DEFAULT_VMCORE;
}
else {
/* Invalid option num */
return RETVAL_FAILURE;
}
return RETVAL_SUCCESS;
}
/* ============================================================
crashdmesg() - Ring buffer dumper Core routine
============================================================ */
static int crashdmesg(VMCore *vmcore)
{
/* --- Variables --- */
char estr[] = "[ERROR] crashdmesg:";
int loop = 0;
Elf64_Phdr phdr_cache;
memset(&phdr_cache, 0x00, sizeof(Elf64_Phdr));
char osrelease[OSRELEASE_LENGTH];
memset(osrelease, 0x00, sizeof(osrelease));
time_t crashtime = 0;
struct tm *ct = NULL;
/* ringbuffer info from vmcoreinfo */
uint64_t log_buf_vaddr = 0;
uint64_t log_end_vaddr = 0;
uint64_t log_buf_len_vaddr = 0;
uint64_t logged_chars_vaddr = 0;
/* Pointer of ring buffer */
off_t ringbuffer1 = 0; /* file offset */
uint32_t ringbuffer1_size = 0;
off_t ringbuffer2 = 0; /* file offset */
uint32_t ringbuffer2_size = 0;
char *ringbuffer = NULL;
/* --- Assert check --- */
assert(vmcore != NULL);
/* Open vmcore file and validate */
fprintf(stdout, "%s: Validate vmcore ELF binary header.\n", APP_NAME);
if (file_open(&vmcore->file)) {
fprintf(stderr, "%s Can not open vmcore file.\n", estr);
return RETVAL_FAILURE;
}
if (elf_validate_elfheader(vmcore)) {
fprintf(stderr, "%s Failed to validate vmcore file.\n", estr);
goto ERROR_CLOSE;
}
/* Search and Read VMCOREINFO */
fprintf(stdout, "%s: Read VMCOREINFO from NOTE segment.\n", APP_NAME);
if (elf_read_vmcoreinfo(vmcore)) {
fprintf(stderr, "%s Can not read VMCOREINFO.\n", estr);
goto ERROR_CLOSE;
}
/* Read additional informations */
if (elf_read_osrelease(vmcore, osrelease, sizeof(osrelease))) {
fprintf(stderr, "%s Can not read OSRELEASE.\n", estr);
goto ERROR_CLOSE;
}
fprintf(stdout, "%s: * OS Release: %s\n", APP_NAME, osrelease);
if (elf_read_crashtime(vmcore, &crashtime)) {
fprintf(stderr, "%s Can not read CRASHTIME.\n", estr);
goto ERROR_CLOSE;
}
fprintf(stdout, "%s: * Crash Time: %ld,\n", APP_NAME, crashtime);
ct = localtime(&crashtime);
if (ct == NULL) {
fprintf(stderr, "%s localtime failed.\n", estr);
return RETVAL_FAILURE;
}
fprintf(stdout, "%s: %04d/%02d/%02d %02d:%02d:%02d\n",
APP_NAME, ct->tm_year+1900, ct->tm_mon+1, ct->tm_mday,
ct->tm_hour, ct->tm_min, ct->tm_sec);
/* Read vaddr of ringbuffer */
fprintf(stdout, "%s: Read Symbol from VMCOREINFO.\n", APP_NAME);
elf_search_vmcoreinfo_symbol(vmcore, "log_buf", &log_buf_vaddr);
elf_search_vmcoreinfo_symbol(vmcore, "log_end", &log_end_vaddr);
elf_search_vmcoreinfo_symbol(vmcore, "log_buf_len", &log_buf_len_vaddr);
elf_search_vmcoreinfo_symbol(vmcore, "logged_chars", &logged_chars_vaddr);
if ((! log_buf_vaddr) || (! log_end_vaddr) ||
(! log_buf_len_vaddr) || (! logged_chars_vaddr)) {
fprintf(stderr, "%s Can not read Symbol from VMCOREINFO.\n", estr);
goto ERROR_CLOSE;
}
fprintf(stdout, "%s: * log_buf: 0x%016lx\n",
APP_NAME, log_buf_vaddr);
fprintf(stdout, "%s: * log_end: 0x%016lx\n",
APP_NAME, log_end_vaddr);
fprintf(stdout, "%s: * log_buf_len: 0x%016lx\n",
APP_NAME, log_buf_len_vaddr);
fprintf(stdout, "%s: * logged_chars: 0x%016lx\n",
APP_NAME, logged_chars_vaddr);
/* Read LOAD segment */
fprintf(stdout, "%s: Read LOAD section about Ring buffer..\n",
APP_NAME);
elf_read_load_uint64(vmcore, &phdr_cache,
log_buf_vaddr, &vmcore->log_buf);
elf_read_load_uint32(vmcore, &phdr_cache,
log_end_vaddr, &vmcore->log_end);
elf_read_load_int32(vmcore, &phdr_cache,
log_buf_len_vaddr, &vmcore->log_buf_len);
elf_read_load_uint32(vmcore, &phdr_cache,
logged_chars_vaddr, &vmcore->logged_chars);
if ((! vmcore->log_buf) || (! vmcore->log_end) ||
(! vmcore->log_buf_len) || (! vmcore->logged_chars)) {
fprintf(stderr, "%s Can not read value from LOAD segment.\n", estr);
goto ERROR_CLOSE;
}
fprintf(stdout, "%s: * log_buf: 0x%016lx\n",
APP_NAME, vmcore->log_buf);
fprintf(stdout, "%s: * log_end: 0x%08x\n",
APP_NAME, vmcore->log_end);
fprintf(stdout, "%s: * log_buf_len: 0x%08x\n",
APP_NAME, vmcore->log_buf_len);
fprintf(stdout, "%s: * logged_chars: 0x%08x\n",
APP_NAME, vmcore->logged_chars);
/* Check log_buf size for safety */
if (vmcore->log_buf_len > MAX_LOGBUF_LIMIT) {
fprintf(stderr, "%s log_buf_len is too big.\n", estr);
goto ERROR_CLOSE;
}
/* Allocate memory for ring buffer */
ringbuffer = malloc(vmcore->log_buf_len);
if (ringbuffer == NULL) {
fprintf(stderr, "%s Can not allocate memory.\n", estr);
goto ERROR_CLOSE;
}
memset(ringbuffer, 0x00, vmcore->log_buf_len);
/* Calculate Dump address */
fprintf(stdout, "%s: Calculating dump area address.\n", APP_NAME);
if ( vmcore->logged_chars < vmcore->log_buf_len ) {
/* ring buffer not filled */
ringbuffer1_size = vmcore->logged_chars;
if (elf_search_load_data(vmcore, &phdr_cache,
vmcore->log_buf, ringbuffer1_size,
&ringbuffer1)) {
fprintf(stderr, "%s Ring buffer not found in vmcore.\n", estr);
goto ERROR_FREE;
}
fprintf(stdout, "%s: Ring buffer Part: 1/1\n", APP_NAME);
fprintf(stdout, "%s: * File Offset: 0x%016lx\n",
APP_NAME, ringbuffer1);
fprintf(stdout, "%s: * Size: 0x%08x\n",
APP_NAME, ringbuffer1_size);
/* DUMP */
if (file_read(&vmcore->file, (void*) ringbuffer,
ringbuffer1, ringbuffer1_size)) {
fprintf(stderr, "%s Can not read ring buffer.\n", estr);
goto ERROR_FREE;
}
fprintf(stdout, "%s: Dump ring buffer.\n", APP_NAME);
fprintf(stdout,
">>>>>>>>>>[ START kernel ring buffer ]>>>>>>>>>>>>>>>>>\n");
for (loop = 0; loop < ringbuffer1_size; loop++) {
fputc(ringbuffer[loop], stdout);
}
fprintf(stdout,
"<<<<<<<<<<[ END kernel ring buffer ]<<<<<<<<<<<<<<<<<\n");
}
else {
/* ring buffer filled */
ringbuffer1_size = vmcore->log_buf_len -
(vmcore->log_end & (vmcore->log_buf_len-1));
ringbuffer2_size = vmcore->log_end & (vmcore->log_buf_len-1);
if ( ((ringbuffer1_size + ringbuffer2_size) != vmcore->log_buf_len) ||
((ringbuffer1_size + ringbuffer2_size) > MAX_LOGBUF_LIMIT) ) {
fprintf(stderr, "%s Dump area size calculation failed.\n", estr);
goto ERROR_FREE;
}
if (elf_search_load_data(vmcore, &phdr_cache,
vmcore->log_buf +
(vmcore->log_end & (vmcore->log_buf_len-1)),
ringbuffer1_size, &ringbuffer1) ||
elf_search_load_data(vmcore, &phdr_cache, vmcore->log_buf,
ringbuffer2_size, &ringbuffer2)) {
fprintf(stderr, "%s Ring buffer not found in vmcore.\n", estr);
goto ERROR_FREE;
}
fprintf(stdout, "%s: Ring buffer Part: 1/2\n", APP_NAME);
fprintf(stdout, "%s: * File Offset: 0x%016lx\n",
APP_NAME, ringbuffer1);
fprintf(stdout, "%s: * Size: 0x%08x\n",
APP_NAME, ringbuffer1_size);
fprintf(stdout, "%s: Ring buffer Part: 2/2\n", APP_NAME);
fprintf(stdout, "%s: * File Offset: 0x%016lx\n",
APP_NAME, ringbuffer2);
fprintf(stdout, "%s: * Size: 0x%08x\n",
APP_NAME, ringbuffer2_size);
/* DUMP */
if (file_read(&vmcore->file, (void*) ringbuffer,
ringbuffer1, ringbuffer1_size) ||
file_read(&vmcore->file, (void*) ringbuffer + ringbuffer1_size,
ringbuffer2, ringbuffer2_size)) {
fprintf(stderr, "%s Can not read ring buffer.\n", estr);
goto ERROR_FREE;
}
fprintf(stdout, "%s: Dump ring buffer.\n", APP_NAME);
fprintf(stdout,
">>>>>>>>>>[ START kernel ring buffer ]>>>>>>>>>>>>>>>>>\n");
for (loop = 0; loop < ringbuffer1_size + ringbuffer2_size ; loop++) {
fputc(ringbuffer[loop], stdout);
}
fprintf(stdout,
"<<<<<<<<<<[ END kernel ring buffer ]<<<<<<<<<<<<<<<<<\n");
}
fprintf(stdout, "%s: Dump complete.\n", APP_NAME);
/* free ringbuffer andclose file */
free(ringbuffer);
file_close(&vmcore->file);
return RETVAL_SUCCESS;
/* Error */
ERROR_FREE:
free(ringbuffer);
ERROR_CLOSE:
file_close(&vmcore->file);
return RETVAL_FAILURE;
}
/* ====================================================================== */