-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathpstore_probe.c
367 lines (313 loc) · 11.6 KB
/
pstore_probe.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
*
* Copyright 2015 Intel Corporation
*
* This program is free software; you can redistribute it and/or modify it under
* the terms and conditions of the GNU Lesser General Public License, as
* published by the Free Software Foundation; either version 2.1 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <unistd.h>
#include <inttypes.h>
#include "log.h"
#include "telemetry.h"
#include "oops_parser.h"
#include "nica/hashmap.h"
char *pstore_dump_path = PSTOREDIR;
static uint32_t version = 1;
static bool send_data(char *backtrace, char *class, uint32_t severity)
{
struct telem_ref *handle = NULL;
int ret;
if ((ret = tm_create_record(&handle, severity, class, version)) < 0) {
telem_log(LOG_ERR, "Failed to create record: %s",
strerror(-ret));
return false;
}
if ((ret = tm_set_payload(handle, backtrace)) < 0) {
telem_log(LOG_ERR, "Failed to set payload: %s", strerror(-ret));
tm_free_record(handle);
return false;
}
if ((ret = tm_send_record(handle)) < 0) {
telem_log(LOG_ERR, "Failed to send record: %s", strerror(-ret));
tm_free_record(handle);
return false;
}
tm_free_record(handle);
return true;
}
void handle_complete_oops_message(struct oops_log_msg *msg)
{
nc_string *payload;
#ifdef DEBUG
for (int i = 0; i < msg->length; i++) {
telem_debug("DEBUG: %s\n", msg->lines[i]);
}
#endif
payload = parse_payload(msg);
telem_debug("DEBUG: Payload Parsed :%s\n", payload->str);
send_data(payload->str, (char *)msg->pattern->classification, (uint32_t)msg->pattern->severity);
nc_string_free(payload);
}
void handle_crash_dump(char *dump, size_t size)
{
char *lnend;
long sz = (long)size;
oops_parser_init(handle_complete_oops_message);
while (sz > 0) {
lnend = memchr(dump, '\n', (size_t)sz);
if (lnend != NULL) {
*lnend = '\0';
parse_single_line(dump, (size_t)(lnend - dump));
lnend++;
sz -= (lnend - dump);
dump = lnend;
}
}
oops_parser_cleanup();
return;
}
char *read_contents(char *filename, size_t *bytes)
{
char *dump_file = NULL;
FILE *fp = NULL;
char *contents = NULL;
long sz = 0;
size_t size, bytes_read;
char *hdr_end;
size_t hdr_len;
if (asprintf(&dump_file, "%s/%s", pstore_dump_path, filename) < 0) {
goto end;
}
fp = fopen(dump_file, "r");
if (fp == NULL) {
telem_log(LOG_ERR, "Failed to open pstore dump file %s:%s\n", dump_file, strerror(errno));
goto end;
}
if (fseek(fp, 0, SEEK_END) < 0) {
telem_log(LOG_ERR, "Fseek failed for file %s:%s\n", dump_file, strerror(errno));
goto end;
}
sz = ftell(fp);
if (sz == -1L) {
telem_perror("ftell failed");
size = 0;
goto end;
}
size = (size_t)sz;
if (fseek(fp, 0, SEEK_SET) < 0) {
telem_log(LOG_ERR, "Fseek failed for file %s:%s\n", dump_file, strerror(errno));
goto end;
}
contents = malloc(size);
if (!contents) {
telem_log(LOG_ERR, "Call to malloc failed");
goto end;
}
memset(contents, 0, size);
bytes_read = fread(contents, sizeof(char), size, fp);
if (bytes_read < size) {
telem_log(LOG_ERR, "Error while reading file %s:%s\n", dump_file, strerror(errno));
free(contents);
contents = NULL;
goto end;
}
hdr_end = memchr(contents, '\n', size);
if (hdr_end) {
//*hdr_end = '\0';
// TODO: Check for valid header in the format Oops#1 Part4
hdr_len = (size_t)(hdr_end + 1 - contents);
memmove(contents, hdr_end + 1, size - hdr_len);
size -= hdr_len;
} else {
// TODO: Handle this case
}
*bytes = size;
end:
//Delete to file to recover space
if (unlink(dump_file) == -1) {
telem_log(LOG_ERR, "Failed to unlink file %s: %s\n", dump_file, strerror(errno));
}
if (fp) {
fclose(fp);
}
free(dump_file);
return contents;
}
/*
* Ids are part of the file name.
* Format : dmesg-efi-143994262108001
* return (timestamp * 100 + part) * 1000 + count
*/
void parse_id(uint64_t id, int *count, int *part)
{
*count = (int)(id % 1000);
id /= 1000;
*part = (int)(id % 100);
}
struct chunk_list {
char *contents;
int count;
int part;
size_t size;
struct chunk_list *next;
};
static void print_usage(char *prog)
{
printf("%s: Usage\n", prog);
printf(" -f, --config_file This overides the other parameters\n");
printf(" -h, --help Display this help message\n");
}
int main(int argc, char **argv)
{
DIR *pstore_dir;
struct dirent *entry;
struct chunk_list *elem, *head;
NcHashmapIter iter;
void *key, *value;
int max_part;
size_t totalsize = 0;
char *crash_dump = NULL;
int c;
int opt_index = 0;
struct option opts[] = {
{ "config_file", 1, NULL, 'f' },
{ "help", 0, NULL, 'h' },
{ NULL, 0, NULL, 0 }
};
while ((c = getopt_long(argc, argv, "f:h", opts, &opt_index)) != -1) {
switch (c) {
case 'f':
if (tm_set_config_file(optarg) != 0) {
telem_log(LOG_ERR, "Configuration file"
" path not valid\n");
exit(EXIT_FAILURE);
}
break;
case 'h':
print_usage(argv[0]);
exit(EXIT_SUCCESS);
case '?':
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
}
/*
* Hash table used to store chunks belonging to a oops counter
* key -> count, value->head of linked list of chunks
*/
NcHashmap *hash = nc_hashmap_new(nc_simple_hash, nc_simple_compare);
pstore_dir = opendir(pstore_dump_path);
if (pstore_dir == NULL) {
telem_perror("Could not open pstore dump directory");
exit(EXIT_FAILURE);
}
while ((entry = readdir(pstore_dir)) != NULL) {
// Process only crash files. Skip . and ..
if (entry->d_type != DT_REG) {
continue;
}
int part = 0, count = 0;
uint64_t id = 0;
// Look for only dmesg logs. Ignore other types of pstore dumps for now.
if (sscanf(entry->d_name, "dmesg-efi-%" PRIu64, &id) == 1) {
parse_id(id, &count, &part);
telem_debug("dmesg-efi Extracted count :%d, part : %d\n",
count, part);
} else if (sscanf(entry->d_name, "dmesg-ramoops-%" PRIu64, &id) == 1) {
count = (int)(id + 1);
part = 1;
telem_debug("dmesg-ramoops Extracted count :%d, part : %d\n",
count, part);
} else {
continue;
}
elem = malloc(sizeof(*elem));
if (!elem) {
exit(EXIT_FAILURE);
}
elem->part = part - 1;
elem->count = count - 1;
elem->size = 0;
elem->contents = read_contents(entry->d_name, &(elem->size));
elem->next = NULL;
void *head = nc_hashmap_get(hash, NC_HASH_KEY(elem->count));
if (head != NULL) {
elem->next = (struct chunk_list *)head;
}
nc_hashmap_put(hash, NC_HASH_KEY(elem->count), elem);
}
closedir(pstore_dir);
nc_hashmap_iter_init(hash, &iter);
while (nc_hashmap_iter_next(&iter, (void **)&key, (void **)&value)) {
telem_debug("DEBUG: Count in the hash: %d\n", NC_UNHASH_KEY(key));
head = (struct chunk_list *)value;
elem = head;
max_part = -1;
totalsize = 0;
while (elem) {
if (elem->part > max_part) {
max_part = elem->part;
}
totalsize += elem->size;
elem = elem->next;
}
assert(max_part > 0);
// Make a contiguous array of chunks with the chunks ordered
struct chunk_list **parts = (struct chunk_list **)malloc(sizeof(*parts) * ((size_t)max_part + 1));
if (!parts) {
exit(EXIT_FAILURE);
}
memset(parts, 0, sizeof(*parts) * (size_t)(max_part + 1));
// Copy chunks in their correct position
for (elem = head; elem; elem = elem->next) {
parts[elem->part] = elem;
}
/*
* Extra step to check if all chunks are present. Else give up and move to
* the next oops.
*/
int part;
for (part = 0; part <= max_part; part++) {
elem = parts[part];
if (elem == NULL || elem->size == 0) {
/* Parts are missing */
telem_log(LOG_DEBUG, "Parts are missing from pstore\n");
break;
}
}
if (part < (max_part + 1)) {
free(parts);
continue;
}
/* Combine all the fragments to get a contiguous log message */
crash_dump = malloc(totalsize);
if (crash_dump == NULL) {
exit(EXIT_FAILURE);
}
size_t offset = 0;
for (int part = max_part; part >= 0; part--) {
elem = parts[part];
memcpy(crash_dump + offset, elem->contents, elem->size);
offset += elem->size;
}
handle_crash_dump(crash_dump, totalsize);
free(crash_dump);
free(parts);
}
return 0;
}