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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2012 Zbigniew Jędrzejewski-Szmek
systemd is free software; you can redistribute it and/or modify it
under the terms 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.
systemd is distributed in the hope that 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.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <fcntl.h>
#include <unistd.h>
#include <systemd/sd-journal.h>
#include "build.h"
#include "set.h"
#include "util.h"
#include "log.h"
#include "path-util.h"
#include "pager.h"
static enum {
ACTION_NONE,
ACTION_LIST,
ACTION_DUMP,
ACTION_GDB,
} arg_action = ACTION_LIST;
static Set *matches = NULL;
static FILE* output = NULL;
static int arg_no_pager = false;
static Set *new_matches(void) {
Set *set;
char *tmp;
int r;
set = set_new(trivial_hash_func, trivial_compare_func);
if (!set) {
log_oom();
return NULL;
}
tmp = strdup("MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1");
if (!tmp) {
log_oom();
set_clear_free(set);
return NULL;
}
r = set_put(set, tmp);
if (r < 0) {
log_error("failed to add to set: %s", strerror(-r));
free(tmp);
set_clear_free(set);
return NULL;
}
return set;
}
static int help(void) {
printf("%s [OPTIONS...] [MATCHES...]\n\n"
"List or retrieve coredumps from the journal.\n\n"
"Flags:\n"
" -o --output=FILE Write output to FILE\n"
" --no-pager Do not pipe output into a pager\n"
"Commands:\n"
" -h --help Show this help\n"
" --version Print version string\n"
" list List available coredumps\n"
" dump PID Print coredump to stdout\n"
" dump PATH Print coredump to stdout\n"
, program_invocation_short_name);
return 0;
}
static int add_match(Set *set, const char *match) {
int r = -ENOMEM;
unsigned pid;
const char* prefix;
char *pattern = NULL;
char _cleanup_free_ *p = NULL;
if (strchr(match, '='))
prefix = "";
else if (strchr(match, '/')) {
p = path_make_absolute_cwd(match);
if (!p)
goto fail;
match = p;
prefix = "COREDUMP_EXE=";
}
else if (safe_atou(match, &pid) == 0)
prefix = "COREDUMP_PID=";
else
prefix = "COREDUMP_COMM=";
pattern = strjoin(prefix, match, NULL);
if (!pattern)
goto fail;
r = set_put(set, pattern);
if (r < 0) {
log_error("failed to add pattern '%s': %s",
pattern, strerror(-r));
goto fail;
}
log_debug("Added pattern: %s", pattern);
return 0;
fail:
free(pattern);
log_error("failed to add match: %s", strerror(-r));
return r;
}
static int parse_argv(int argc, char *argv[]) {
enum {
ARG_VERSION = 0x100,
ARG_NO_PAGER,
};
int r, c;
static const struct option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "version" , no_argument, NULL, ARG_VERSION },
{ "no-pager", no_argument, NULL, ARG_NO_PAGER },
{ "output", required_argument, NULL, 'o' },
};
assert(argc >= 0);
assert(argv);
while ((c = getopt_long(argc, argv, "ho:", options, NULL)) >= 0)
switch(c) {
case 'h':
help();
arg_action = ACTION_NONE;
return 0;
case ARG_VERSION:
puts(PACKAGE_STRING);
puts(DISTRIBUTION);
puts(SYSTEMD_FEATURES);
arg_action = ACTION_NONE;
return 0;
case ARG_NO_PAGER:
arg_no_pager = true;
break;
case 'o':
if (output) {
log_error("cannot set output more than once");
return -EINVAL;
}
output = fopen(optarg, "we");
if (!output) {
log_error("writing to '%s': %m", optarg);
return -errno;
}
break;
default:
log_error("Unknown option code %c", c);
return -EINVAL;
}
if (optind < argc) {
const char *cmd = argv[optind++];
if(streq(cmd, "list"))
arg_action = ACTION_LIST;
else if (streq(cmd, "dump"))
arg_action = ACTION_DUMP;
else if (streq(cmd, "gdb"))
arg_action = ACTION_GDB;
else {
log_error("Unknown action '%s'", cmd);
return -EINVAL;
}
}
while (optind < argc) {
r = add_match(matches, argv[optind]);
if (r != 0)
return r;
optind++;
}
return 0;
}
static int retrieve(const void *data,
size_t len,
const char *name,
const char **var) {
size_t field;
field = strlen(name) + 1; /* name + "=" */
if (len < field)
return 0;
if (memcmp(data, name, field - 1) != 0)
return 0;
if (((const char*) data)[field - 1] != '=')
return 0;
*var = strndup((const char*)data + field, len - field);
if (!var)
return log_oom();
return 0;
}
static int print_entry(FILE* file, sd_journal *j, int had_header) {
const char _cleanup_free_
*pid = NULL, *uid = NULL, *gid = NULL,
*sgnl = NULL, *exe = NULL;
const void *d;
size_t l;
usec_t t;
char buf[FORMAT_TIMESTAMP_MAX];
int r;
SD_JOURNAL_FOREACH_DATA(j, d, l) {
retrieve(d, l, "COREDUMP_PID", &pid);
retrieve(d, l, "COREDUMP_PID", &pid);
retrieve(d, l, "COREDUMP_UID", &uid);
retrieve(d, l, "COREDUMP_GID", &gid);
retrieve(d, l, "COREDUMP_SIGNAL", &sgnl);
retrieve(d, l, "COREDUMP_EXE", &exe);
if (!exe)
retrieve(d, l, "COREDUMP_COMM", &exe);
if (!exe)
retrieve(d, l, "COREDUMP_CMDLINE", &exe);
}
if (!pid && !uid && !gid && !sgnl && !exe) {
log_warning("Empty coredump log entry");
return -EINVAL;
}
r = sd_journal_get_realtime_usec(j, &t);
if (r < 0) {
log_error("Failed to get realtime timestamp: %s", strerror(-r));
return r;
}
format_timestamp(buf, sizeof(buf), t);
if (!had_header)
fprintf(file, "%-*s %*s %*s %*s %*s %s\n",
FORMAT_TIMESTAMP_MAX-1, "TIME",
6, "PID",
5, "UID",
5, "GID",
3, "SIG",
"EXE");
fprintf(file, "%*s %*s %*s %*s %*s %s\n",
FORMAT_TIMESTAMP_MAX-1, buf,
6, pid,
5, uid,
5, gid,
3, sgnl,
exe);
return 0;
}
static int dump_list(sd_journal *j) {
int found = 0;
assert(j);
SD_JOURNAL_FOREACH(j)
print_entry(stdout, j, found++);
if (!found) {
log_notice("No coredumps found");
return -ESRCH;
}
return 0;
}
static int focus(sd_journal *j) {
int r;
r = sd_journal_seek_tail(j);
if (r == 0)
r = sd_journal_previous(j);
if (r < 0) {
log_error("Failed to search journal: %s", strerror(-r));
return r;
}
if (r == 0) {
log_error("No match found");
return -ESRCH;
}
return r;
}
static int dump_core(sd_journal* j) {
const void *data;
size_t len, ret;
int r;
assert(j);
r = focus(j);
if (r < 0)
return r;
print_entry(output ? stdout : stderr, j, false);
if (on_tty() && !output) {
log_error("Refusing to dump core to tty");
return -ENOTTY;
}
r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len);
if (r < 0) {
log_error("Failed to retrieve COREDUMP field: %s", strerror(-r));
return r;
}
assert(len >= 9);
data = (const uint8_t*) data + 9;
len -= 9;
ret = fwrite(data, len, 1, output ? output : stdout);
if (ret != 1) {
log_error("dumping coredump: %m (%zu)", ret);
return -errno;
}
r = sd_journal_previous(j);
if (r >= 0)
log_warning("More than one entry matches, ignoring rest.\n");
return 0;
}
static int run_gdb(sd_journal *j) {
char path[] = "/var/tmp/coredump-XXXXXX";
const void *data;
size_t len;
ssize_t sz;
pid_t pid;
_cleanup_free_ char *exe = NULL;
int r;
_cleanup_close_ int fd = -1;
siginfo_t st;
assert(j);
r = focus(j);
if (r < 0)
return r;
print_entry(stdout, j, false);
r = sd_journal_get_data(j, "COREDUMP_EXE", (const void**) &data, &len);
if (r < 0) {
log_error("Failed to retrieve COREDUMP_EXE field: %s", strerror(-r));
return r;
}
assert(len >= 13);
data = (const uint8_t*) data + 13;
len -= 13;
exe = strndup(data, len);
if (!exe)
return log_oom();
if (endswith(exe, " (deleted)")) {
log_error("Binary already deleted.");
return -ENOENT;
}
r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len);
if (r < 0) {
log_error("Failed to retrieve COREDUMP field: %s", strerror(-r));
return r;
}
assert(len >= 9);
data = (const uint8_t*) data + 9;
len -= 9;
fd = mkostemp(path, O_WRONLY);
if (fd < 0) {
log_error("Failed to create temporary file: %m");
return -errno;
}
sz = write(fd, data, len);
if (sz < 0) {
log_error("Failed to write temporary file: %s", strerror(errno));
r = -errno;
goto finish;
}
if (sz != (ssize_t) len) {
log_error("Short write to temporary file.");
r = -EIO;
goto finish;
}
close_nointr_nofail(fd);
fd = -1;
pid = fork();
if (pid < 0) {
log_error("Failed to fork(): %m");
r = -errno;
goto finish;
}
if (pid == 0) {
execlp("gdb", "gdb", exe, path, NULL);
log_error("Failed to invoke gdb: %m");
_exit(1);
}
r = wait_for_terminate(pid, &st);
if (r < 0) {
log_error("Failed to wait for gdb: %m");
goto finish;
}
r = st.si_code == CLD_EXITED ? st.si_status : 255;
finish:
unlink(path);
return r;
}
int main(int argc, char *argv[]) {
sd_journal *j = NULL;
const char* match;
Iterator it;
int r = 0;
log_parse_environment();
log_open();
matches = new_matches();
if (!matches)
goto end;
if (parse_argv(argc, argv))
goto end;
if (arg_action == ACTION_NONE)
goto end;
r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
if (r < 0) {
log_error("Failed to open journal: %s", strerror(-r));
goto end;
}
SET_FOREACH(match, matches, it) {
r = sd_journal_add_match(j, match, strlen(match));
if (r != 0) {
log_error("Failed to add match '%s': %s",
match, strerror(-r));
goto end;
}
}
switch(arg_action) {
case ACTION_LIST:
if (!arg_no_pager)
pager_open();
r = dump_list(j);
break;
case ACTION_DUMP:
r = dump_core(j);
break;
case ACTION_GDB:
r = run_gdb(j);
break;
default:
assert_not_reached("Shouldn't be here");
}
end:
if (j)
sd_journal_close(j);
set_free_free(matches);
pager_close();
if (output)
fclose(output);
return r >= 0 ? r : EXIT_FAILURE;
}
|