]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/coredump/coredumpctl.c
coredumpctl: Add debug as a gdb alias, and add lldb support (closes #8722) (#8744)
[thirdparty/systemd.git] / src / coredump / coredumpctl.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
5de0409e
ZJS
2/***
3 This file is part of systemd.
4
5 Copyright 2012 Zbigniew Jędrzejewski-Szmek
5de0409e
ZJS
6***/
7
3f6fd1ba
LP
8#include <fcntl.h>
9#include <getopt.h>
a9cdc94f 10#include <locale.h>
5de0409e
ZJS
11#include <stdio.h>
12#include <string.h>
ada45c78 13#include <unistd.h>
5de0409e 14
012f2b7d 15#include "sd-bus.h"
2cf4172a 16#include "sd-journal.h"
2b044526 17#include "sd-messages.h"
3f6fd1ba 18
b5efdb8a 19#include "alloc-util.h"
012f2b7d
ZJS
20#include "bus-error.h"
21#include "bus-util.h"
3f6fd1ba 22#include "compress.h"
3ffd4af2 23#include "fd-util.h"
0d39fa9c 24#include "fileio.h"
992e8f22 25#include "fs-util.h"
3f6fd1ba 26#include "journal-internal.h"
b9aaa7f4 27#include "journal-util.h"
5de0409e 28#include "log.h"
763c7aa2 29#include "macro.h"
3f6fd1ba 30#include "pager.h"
6bedfcbb 31#include "parse-util.h"
3f6fd1ba 32#include "path-util.h"
0b452006 33#include "process-util.h"
3f6fd1ba 34#include "sigbus.h"
24882e06 35#include "signal-util.h"
07630cea 36#include "string-util.h"
5ab9ed07 37#include "strv.h"
3f6fd1ba 38#include "terminal-util.h"
b1d4f8e1 39#include "user-util.h"
6bedfcbb 40#include "util.h"
5ce97d33 41#include "verbs.h"
5de0409e 42
501551e8
LP
43#define SHORT_BUS_CALL_TIMEOUT_USEC (3 * USEC_PER_SEC)
44
32485d09 45static usec_t arg_since = USEC_INFINITY, arg_until = USEC_INFINITY;
e15758cc 46static const char* arg_field = NULL;
c5896b6a 47static const char *arg_debugger = NULL;
b73e9a02 48static const char *arg_directory = NULL;
ea4b98e6 49static bool arg_no_pager = false;
9a340880 50static int arg_no_legend = false;
0c51aada 51static int arg_one = false;
3774cf57 52static FILE* arg_output = NULL;
df65f77b 53static bool arg_reverse = false;
b9aaa7f4 54static bool arg_quiet = false;
5de0409e 55
5ab9ed07 56static int add_match(sd_journal *j, const char *match) {
7fd1b19b 57 _cleanup_free_ char *p = NULL;
43bfe750 58 const char* prefix, *pattern;
0f474365
LP
59 pid_t pid;
60 int r;
5de0409e
ZJS
61
62 if (strchr(match, '='))
63 prefix = "";
64 else if (strchr(match, '/')) {
0f474365
LP
65 r = path_make_absolute_cwd(match, &p);
66 if (r < 0)
5ab9ed07
ZJS
67 return log_error_errno(r, "path_make_absolute_cwd(\"%s\"): %m", match);
68
5de0409e
ZJS
69 match = p;
70 prefix = "COREDUMP_EXE=";
0f474365 71 } else if (parse_pid(match, &pid) >= 0)
5de0409e
ZJS
72 prefix = "COREDUMP_PID=";
73 else
74 prefix = "COREDUMP_COMM=";
75
e8fb0238 76 pattern = strjoina(prefix, match);
5ab9ed07
ZJS
77 log_debug("Adding match: %s", pattern);
78 r = sd_journal_add_match(j, pattern, 0);
79 if (r < 0)
80 return log_error_errno(r, "Failed to add match \"%s\": %m", match);
43bfe750 81
5ab9ed07
ZJS
82 return 0;
83}
84
5ce97d33 85static int add_matches(sd_journal *j, char **matches) {
5ab9ed07
ZJS
86 char **match;
87 int r;
5de0409e 88
2b044526 89 r = sd_journal_add_match(j, "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR, 0);
0f474365 90 if (r < 0)
2b044526 91 return log_error_errno(r, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR);
5ab9ed07 92
a7581ff9
ZJS
93 r = sd_journal_add_match(j, "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR, 0);
94 if (r < 0)
95 return log_error_errno(r, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR);
96
5ce97d33 97 STRV_FOREACH(match, matches) {
5ab9ed07
ZJS
98 r = add_match(j, *match);
99 if (r < 0)
100 return r;
101 }
5de0409e
ZJS
102
103 return 0;
5de0409e
ZJS
104}
105
5ce97d33
YW
106static int acquire_journal(sd_journal **ret, char **matches) {
107 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
108 int r;
109
110 assert(ret);
111
112 if (arg_directory) {
113 r = sd_journal_open_directory(&j, arg_directory, 0);
114 if (r < 0)
115 return log_error_errno(r, "Failed to open journals in directory: %s: %m", arg_directory);
116 } else {
117 r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
118 if (r < 0)
119 return log_error_errno(r, "Failed to open journal: %m");
120 }
121
122 r = journal_access_check_and_warn(j, arg_quiet, true);
123 if (r < 0)
124 return r;
125
126 r = add_matches(j, matches);
127 if (r < 0)
128 return r;
129
130 if (DEBUG_LOGGING) {
131 _cleanup_free_ char *filter;
132
133 filter = journal_make_match_string(j);
134 log_debug("Journal filter: %s", filter);
135 }
136
1cc6c93a 137 *ret = TAKE_PTR(j);
5ce97d33
YW
138
139 return 0;
140}
141
142static int help(void) {
601185b4
ZJS
143 printf("%s [OPTIONS...]\n\n"
144 "List or retrieve coredumps from the journal.\n\n"
145 "Flags:\n"
c5896b6a
RG
146 " -h --help Show this help\n"
147 " --version Print version string\n"
148 " --no-pager Do not pipe output into a pager\n"
149 " --no-legend Do not print the column headers\n"
150 " --debugger=DEBUGGER Use the given debugger\n"
151 " -1 Show information about most recent entry only\n"
152 " -S --since=DATE Only print coredumps since the date\n"
153 " -U --until=DATE Only print coredumps until the date\n"
154 " -r --reverse Show the newest entries first\n"
155 " -F --field=FIELD List all values a certain field takes\n"
156 " -o --output=FILE Write output to FILE\n"
157 " -D --directory=DIR Use journal files from directory\n\n"
158 " -q --quiet Do not show info messages and privilege warning\n"
601185b4
ZJS
159 "Commands:\n"
160 " list [MATCHES...] List available coredumps (default)\n"
161 " info [MATCHES...] Show detailed information about one or more coredumps\n"
162 " dump [MATCHES...] Print first matching coredump to stdout\n"
c5896b6a 163 " debug [MATCHES...] Start a debugger for the first matching coredump\n"
601185b4 164 , program_invocation_short_name);
5ce97d33
YW
165
166 return 0;
601185b4
ZJS
167}
168
5ab9ed07 169static int parse_argv(int argc, char *argv[]) {
5de0409e
ZJS
170 enum {
171 ARG_VERSION = 0x100,
172 ARG_NO_PAGER,
9a340880 173 ARG_NO_LEGEND,
c5896b6a 174 ARG_DEBUGGER,
5de0409e
ZJS
175 };
176
32485d09 177 int c, r;
5de0409e
ZJS
178
179 static const struct option options[] = {
57ce4bd4
ZJS
180 { "help", no_argument, NULL, 'h' },
181 { "version" , no_argument, NULL, ARG_VERSION },
182 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
9a340880 183 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
c5896b6a 184 { "debugger", required_argument, NULL, ARG_DEBUGGER },
57ce4bd4 185 { "output", required_argument, NULL, 'o' },
4f76ae1b 186 { "field", required_argument, NULL, 'F' },
b73e9a02 187 { "directory", required_argument, NULL, 'D' },
df65f77b 188 { "reverse", no_argument, NULL, 'r' },
32485d09
GS
189 { "since", required_argument, NULL, 'S' },
190 { "until", required_argument, NULL, 'U' },
b9aaa7f4 191 { "quiet", no_argument, NULL, 'q' },
eb9da376 192 {}
5de0409e
ZJS
193 };
194
195 assert(argc >= 0);
196 assert(argv);
197
b9aaa7f4 198 while ((c = getopt_long(argc, argv, "ho:F:1D:rS:U:q", options, NULL)) >= 0)
5de0409e
ZJS
199 switch(c) {
200 case 'h':
5ce97d33 201 return help();
5de0409e
ZJS
202
203 case ARG_VERSION:
3f6fd1ba 204 return version();
5de0409e
ZJS
205
206 case ARG_NO_PAGER:
207 arg_no_pager = true;
208 break;
209
9a340880
ZJS
210 case ARG_NO_LEGEND:
211 arg_no_legend = true;
212 break;
213
c5896b6a
RG
214 case ARG_DEBUGGER:
215 arg_debugger = optarg;
216 break;
217
5de0409e 218 case 'o':
3774cf57 219 if (arg_output) {
5de0409e
ZJS
220 log_error("cannot set output more than once");
221 return -EINVAL;
222 }
223
3774cf57
LP
224 arg_output = fopen(optarg, "we");
225 if (!arg_output)
4a62c710 226 return log_error_errno(errno, "writing to '%s': %m", optarg);
5de0409e
ZJS
227
228 break;
57ce4bd4 229
32485d09
GS
230 case 'S':
231 r = parse_timestamp(optarg, &arg_since);
232 if (r < 0)
233 return log_error_errno(r, "Failed to parse timestamp: %s", optarg);
234 break;
235
236 case 'U':
237 r = parse_timestamp(optarg, &arg_until);
238 if (r < 0)
239 return log_error_errno(r, "Failed to parse timestamp: %s", optarg);
240 break;
241
4f76ae1b 242 case 'F':
a276ae74 243 if (arg_field) {
4f76ae1b
ZJS
244 log_error("cannot use --field/-F more than once");
245 return -EINVAL;
246 }
a276ae74 247 arg_field = optarg;
4f76ae1b
ZJS
248 break;
249
0c51aada
LP
250 case '1':
251 arg_one = true;
252 break;
253
b73e9a02
SW
254 case 'D':
255 arg_directory = optarg;
256 break;
257
df65f77b
NK
258 case 'r':
259 arg_reverse = true;
260 break;
261
b9aaa7f4
ZJS
262 case 'q':
263 arg_quiet = true;
264 break;
265
57ce4bd4
ZJS
266 case '?':
267 return -EINVAL;
268
5de0409e 269 default:
eb9da376 270 assert_not_reached("Unhandled option");
5de0409e
ZJS
271 }
272
32485d09
GS
273 if (arg_since != USEC_INFINITY && arg_until != USEC_INFINITY &&
274 arg_since > arg_until) {
275 log_error("--since= must be before --until=.");
276 return -EINVAL;
277 }
278
5ce97d33 279 return 1;
5de0409e
ZJS
280}
281
8bc8ab83
LP
282static int retrieve(const void *data,
283 size_t len,
284 const char *name,
a276ae74 285 char **var) {
5de0409e 286
4f76ae1b 287 size_t ident;
a276ae74 288 char *v;
8bc8ab83 289
4f76ae1b 290 ident = strlen(name) + 1; /* name + "=" */
8bc8ab83 291
4f76ae1b 292 if (len < ident)
8bc8ab83 293 return 0;
5de0409e 294
4f76ae1b 295 if (memcmp(data, name, ident - 1) != 0)
8bc8ab83
LP
296 return 0;
297
4f76ae1b 298 if (((const char*) data)[ident - 1] != '=')
8bc8ab83 299 return 0;
5de0409e 300
a276ae74
LP
301 v = strndup((const char*)data + ident, len - ident);
302 if (!v)
5de0409e
ZJS
303 return log_oom();
304
a276ae74
LP
305 free(*var);
306 *var = v;
307
062b99e8 308 return 1;
5de0409e
ZJS
309}
310
062b99e8 311static int print_field(FILE* file, sd_journal *j) {
4f76ae1b
ZJS
312 const void *d;
313 size_t l;
314
e15758cc
LP
315 assert(file);
316 assert(j);
317
a276ae74 318 assert(arg_field);
4f76ae1b 319
062b99e8
ZJS
320 /* A (user-specified) field may appear more than once for a given entry.
321 * We will print all of the occurences.
322 * This is different below for fields that systemd-coredump uses,
323 * because they cannot meaningfully appear more than once.
324 */
325 SD_JOURNAL_FOREACH_DATA(j, d, l) {
326 _cleanup_free_ char *value = NULL;
327 int r;
328
329 r = retrieve(d, l, arg_field, &value);
330 if (r < 0)
331 return r;
332 if (r > 0)
333 fprintf(file, "%s\n", value);
334 }
a276ae74 335
062b99e8 336 return 0;
4f76ae1b
ZJS
337}
338
062b99e8
ZJS
339#define RETRIEVE(d, l, name, arg) \
340 { \
341 int _r = retrieve(d, l, name, &arg); \
342 if (_r < 0) \
343 return _r; \
344 if (_r > 0) \
345 continue; \
346 }
347
e15758cc 348static int print_list(FILE* file, sd_journal *j, int had_legend) {
a276ae74 349 _cleanup_free_ char
a7581ff9 350 *mid = NULL, *pid = NULL, *uid = NULL, *gid = NULL,
9fe13294 351 *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL,
cc4419ed 352 *filename = NULL, *truncated = NULL, *coredump = NULL;
8bc8ab83
LP
353 const void *d;
354 size_t l;
684341b0
LP
355 usec_t t;
356 char buf[FORMAT_TIMESTAMP_MAX];
357 int r;
04de5879 358 const char *present;
a7581ff9 359 bool normal_coredump;
8bc8ab83 360
e15758cc
LP
361 assert(file);
362 assert(j);
363
8bc8ab83 364 SD_JOURNAL_FOREACH_DATA(j, d, l) {
a7581ff9 365 RETRIEVE(d, l, "MESSAGE_ID", mid);
062b99e8
ZJS
366 RETRIEVE(d, l, "COREDUMP_PID", pid);
367 RETRIEVE(d, l, "COREDUMP_UID", uid);
368 RETRIEVE(d, l, "COREDUMP_GID", gid);
369 RETRIEVE(d, l, "COREDUMP_SIGNAL", sgnl);
370 RETRIEVE(d, l, "COREDUMP_EXE", exe);
371 RETRIEVE(d, l, "COREDUMP_COMM", comm);
372 RETRIEVE(d, l, "COREDUMP_CMDLINE", cmdline);
373 RETRIEVE(d, l, "COREDUMP_FILENAME", filename);
cc4419ed 374 RETRIEVE(d, l, "COREDUMP_TRUNCATED", truncated);
062b99e8 375 RETRIEVE(d, l, "COREDUMP", coredump);
8bc8ab83 376 }
5de0409e 377
9fe13294 378 if (!pid && !uid && !gid && !sgnl && !exe && !comm && !cmdline && !filename) {
684341b0
LP
379 log_warning("Empty coredump log entry");
380 return -EINVAL;
381 }
382
383 r = sd_journal_get_realtime_usec(j, &t);
23bbb0de
MS
384 if (r < 0)
385 return log_error_errno(r, "Failed to get realtime timestamp: %m");
5de0409e 386
684341b0
LP
387 format_timestamp(buf, sizeof(buf), t);
388
9a340880 389 if (!had_legend && !arg_no_legend)
cc4419ed 390 fprintf(file, "%-*s %*s %*s %*s %*s %-*s %s\n",
c3f84106 391 FORMAT_TIMESTAMP_WIDTH, "TIME",
5de0409e
ZJS
392 6, "PID",
393 5, "UID",
394 5, "GID",
684341b0 395 3, "SIG",
cc4419ed 396 9, "COREFILE",
684341b0 397 "EXE");
5de0409e 398
a7581ff9
ZJS
399 normal_coredump = streq_ptr(mid, SD_MESSAGE_COREDUMP_STR);
400
04de5879
ZJS
401 if (filename)
402 if (access(filename, R_OK) == 0)
403 present = "present";
404 else if (errno == ENOENT)
405 present = "missing";
406 else
407 present = "error";
408 else if (coredump)
409 present = "journal";
a7581ff9 410 else if (normal_coredump)
04de5879 411 present = "none";
a7581ff9
ZJS
412 else
413 present = "-";
04de5879 414
32a1575f 415 if (STR_IN_SET(present, "present", "journal") && truncated && parse_boolean(truncated) > 0)
cc4419ed
ZJS
416 present = "truncated";
417
04de5879 418 fprintf(file, "%-*s %*s %*s %*s %*s %-*s %s\n",
c3f84106 419 FORMAT_TIMESTAMP_WIDTH, buf,
a276ae74
LP
420 6, strna(pid),
421 5, strna(uid),
422 5, strna(gid),
a7581ff9 423 3, normal_coredump ? strna(sgnl) : "-",
cc4419ed 424 9, present,
a276ae74 425 strna(exe ?: (comm ?: cmdline)));
684341b0
LP
426
427 return 0;
5de0409e
ZJS
428}
429
e15758cc
LP
430static int print_info(FILE *file, sd_journal *j, bool need_space) {
431 _cleanup_free_ char
a7581ff9 432 *mid = NULL, *pid = NULL, *uid = NULL, *gid = NULL,
e15758cc
LP
433 *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL,
434 *unit = NULL, *user_unit = NULL, *session = NULL,
a035f819 435 *boot_id = NULL, *machine_id = NULL, *hostname = NULL,
9fe13294 436 *slice = NULL, *cgroup = NULL, *owner_uid = NULL,
47f50642 437 *message = NULL, *timestamp = NULL, *filename = NULL,
cc4419ed 438 *truncated = NULL, *coredump = NULL;
e15758cc
LP
439 const void *d;
440 size_t l;
a7581ff9 441 bool normal_coredump;
4b8cbe9a 442 int r;
e15758cc
LP
443
444 assert(file);
445 assert(j);
446
447 SD_JOURNAL_FOREACH_DATA(j, d, l) {
a7581ff9 448 RETRIEVE(d, l, "MESSAGE_ID", mid);
062b99e8
ZJS
449 RETRIEVE(d, l, "COREDUMP_PID", pid);
450 RETRIEVE(d, l, "COREDUMP_UID", uid);
451 RETRIEVE(d, l, "COREDUMP_GID", gid);
452 RETRIEVE(d, l, "COREDUMP_SIGNAL", sgnl);
453 RETRIEVE(d, l, "COREDUMP_EXE", exe);
454 RETRIEVE(d, l, "COREDUMP_COMM", comm);
455 RETRIEVE(d, l, "COREDUMP_CMDLINE", cmdline);
456 RETRIEVE(d, l, "COREDUMP_UNIT", unit);
457 RETRIEVE(d, l, "COREDUMP_USER_UNIT", user_unit);
458 RETRIEVE(d, l, "COREDUMP_SESSION", session);
459 RETRIEVE(d, l, "COREDUMP_OWNER_UID", owner_uid);
460 RETRIEVE(d, l, "COREDUMP_SLICE", slice);
461 RETRIEVE(d, l, "COREDUMP_CGROUP", cgroup);
462 RETRIEVE(d, l, "COREDUMP_TIMESTAMP", timestamp);
463 RETRIEVE(d, l, "COREDUMP_FILENAME", filename);
cc4419ed 464 RETRIEVE(d, l, "COREDUMP_TRUNCATED", truncated);
062b99e8
ZJS
465 RETRIEVE(d, l, "COREDUMP", coredump);
466 RETRIEVE(d, l, "_BOOT_ID", boot_id);
467 RETRIEVE(d, l, "_MACHINE_ID", machine_id);
468 RETRIEVE(d, l, "_HOSTNAME", hostname);
469 RETRIEVE(d, l, "MESSAGE", message);
e15758cc
LP
470 }
471
472 if (need_space)
473 fputs("\n", file);
474
a7581ff9
ZJS
475 normal_coredump = streq_ptr(mid, SD_MESSAGE_COREDUMP_STR);
476
81cef14f
LP
477 if (comm)
478 fprintf(file,
479 " PID: %s%s%s (%s)\n",
1fc464f6 480 ansi_highlight(), strna(pid), ansi_normal(), comm);
81cef14f
LP
481 else
482 fprintf(file,
483 " PID: %s%s%s\n",
1fc464f6 484 ansi_highlight(), strna(pid), ansi_normal());
a035f819
LP
485
486 if (uid) {
487 uid_t n;
488
489 if (parse_uid(uid, &n) >= 0) {
490 _cleanup_free_ char *u = NULL;
491
492 u = uid_to_name(n);
493 fprintf(file,
494 " UID: %s (%s)\n",
495 uid, u);
496 } else {
497 fprintf(file,
498 " UID: %s\n",
499 uid);
500 }
501 }
502
503 if (gid) {
504 gid_t n;
505
506 if (parse_gid(gid, &n) >= 0) {
507 _cleanup_free_ char *g = NULL;
508
509 g = gid_to_name(n);
510 fprintf(file,
511 " GID: %s (%s)\n",
512 gid, g);
513 } else {
514 fprintf(file,
515 " GID: %s\n",
516 gid);
517 }
518 }
e15758cc
LP
519
520 if (sgnl) {
521 int sig;
a7581ff9 522 const char *name = normal_coredump ? "Signal" : "Reason";
e15758cc 523
a7581ff9
ZJS
524 if (normal_coredump && safe_atoi(sgnl, &sig) >= 0)
525 fprintf(file, " %s: %s (%s)\n", name, sgnl, signal_to_string(sig));
e15758cc 526 else
a7581ff9 527 fprintf(file, " %s: %s\n", name, sgnl);
e15758cc
LP
528 }
529
4b8cbe9a
LP
530 if (timestamp) {
531 usec_t u;
532
533 r = safe_atou64(timestamp, &u);
534 if (r >= 0) {
535 char absolute[FORMAT_TIMESTAMP_MAX], relative[FORMAT_TIMESPAN_MAX];
536
537 fprintf(file,
538 " Timestamp: %s (%s)\n",
539 format_timestamp(absolute, sizeof(absolute), u),
540 format_timestamp_relative(relative, sizeof(relative), u));
541
542 } else
543 fprintf(file, " Timestamp: %s\n", timestamp);
544 }
545
e15758cc
LP
546 if (cmdline)
547 fprintf(file, " Command Line: %s\n", cmdline);
81cef14f 548 if (exe)
1fc464f6 549 fprintf(file, " Executable: %s%s%s\n", ansi_highlight(), exe, ansi_normal());
a035f819
LP
550 if (cgroup)
551 fprintf(file, " Control Group: %s\n", cgroup);
e15758cc
LP
552 if (unit)
553 fprintf(file, " Unit: %s\n", unit);
554 if (user_unit)
554ed50f 555 fprintf(file, " User Unit: %s\n", user_unit);
a035f819
LP
556 if (slice)
557 fprintf(file, " Slice: %s\n", slice);
e15758cc
LP
558 if (session)
559 fprintf(file, " Session: %s\n", session);
a035f819
LP
560 if (owner_uid) {
561 uid_t n;
562
563 if (parse_uid(owner_uid, &n) >= 0) {
564 _cleanup_free_ char *u = NULL;
565
566 u = uid_to_name(n);
567 fprintf(file,
568 " Owner UID: %s (%s)\n",
569 owner_uid, u);
570 } else {
571 fprintf(file,
572 " Owner UID: %s\n",
573 owner_uid);
574 }
575 }
e15758cc
LP
576 if (boot_id)
577 fprintf(file, " Boot ID: %s\n", boot_id);
578 if (machine_id)
579 fprintf(file, " Machine ID: %s\n", machine_id);
580 if (hostname)
581 fprintf(file, " Hostname: %s\n", hostname);
582
cc4419ed 583 if (filename) {
32a1575f
LP
584 bool inacc, trunc;
585
586 inacc = access(filename, R_OK) < 0;
587 trunc = truncated && parse_boolean(truncated) > 0;
cc4419ed
ZJS
588
589 if (inacc || trunc)
590 fprintf(file, " Storage: %s%s (%s%s%s)%s\n",
591 ansi_highlight_red(),
592 filename,
593 inacc ? "inaccessible" : "",
594 inacc && trunc ? ", " : "",
595 trunc ? "truncated" : "",
596 ansi_normal());
597 else
598 fprintf(file, " Storage: %s\n", filename);
599 }
600
47f50642
ZJS
601 else if (coredump)
602 fprintf(file, " Storage: journal\n");
603 else
604 fprintf(file, " Storage: none\n");
e15758cc 605
8d4e028f
LP
606 if (message) {
607 _cleanup_free_ char *m = NULL;
608
609 m = strreplace(message, "\n", "\n ");
610
611 fprintf(file, " Message: %s\n", strstrip(m ?: message));
612 }
613
e15758cc
LP
614 return 0;
615}
616
ada45c78 617static int focus(sd_journal *j) {
5de0409e
ZJS
618 int r;
619
5de0409e
ZJS
620 r = sd_journal_seek_tail(j);
621 if (r == 0)
622 r = sd_journal_previous(j);
23bbb0de
MS
623 if (r < 0)
624 return log_error_errno(r, "Failed to search journal: %m");
8bc8ab83 625 if (r == 0) {
0c51aada 626 log_error("No match found.");
8bc8ab83
LP
627 return -ESRCH;
628 }
ada45c78
LP
629 return r;
630}
5de0409e 631
5ce97d33 632static int print_entry(sd_journal *j, unsigned n_found, bool verb_is_info) {
0c51aada
LP
633 assert(j);
634
5ce97d33 635 if (verb_is_info)
062b99e8 636 return print_info(stdout, j, n_found);
0c51aada 637 else if (arg_field)
062b99e8 638 return print_field(stdout, j);
0c51aada 639 else
062b99e8 640 return print_list(stdout, j, n_found);
0c51aada
LP
641}
642
5ce97d33
YW
643static int dump_list(int argc, char **argv, void *userdata) {
644 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
0c51aada 645 unsigned n_found = 0;
5ce97d33 646 bool verb_is_info;
0c51aada
LP
647 int r;
648
5ce97d33
YW
649 verb_is_info = (argc >= 1 && streq(argv[0], "info"));
650
651 r = acquire_journal(&j, argv + 1);
652 if (r < 0)
653 return r;
654
ee5324aa 655 (void) pager_open(arg_no_pager, false);
0c51aada
LP
656
657 /* The coredumps are likely to compressed, and for just
658 * listing them we don't need to decompress them, so let's
659 * pick a fairly low data threshold here */
660 sd_journal_set_data_threshold(j, 4096);
661
662 if (arg_one) {
663 r = focus(j);
664 if (r < 0)
665 return r;
666
5ce97d33 667 return print_entry(j, 0, verb_is_info);
0c51aada 668 } else {
32485d09
GS
669 if (arg_since != USEC_INFINITY && !arg_reverse)
670 r = sd_journal_seek_realtime_usec(j, arg_since);
671 else if (arg_until != USEC_INFINITY && arg_reverse)
672 r = sd_journal_seek_realtime_usec(j, arg_until);
673 else if (arg_reverse)
674 r = sd_journal_seek_tail(j);
675 else
676 r = sd_journal_seek_head(j);
677 if (r < 0)
678 return log_error_errno(r, "Failed to seek to date: %m");
679
680 for (;;) {
681 if (!arg_reverse)
682 r = sd_journal_next(j);
683 else
684 r = sd_journal_previous(j);
685
686 if (r < 0)
687 return log_error_errno(r, "Failed to iterate through journal: %m");
688
689 if (r == 0)
690 break;
691
692 if (arg_until != USEC_INFINITY && !arg_reverse) {
693 usec_t usec;
694
695 r = sd_journal_get_realtime_usec(j, &usec);
df65f77b 696 if (r < 0)
32485d09
GS
697 return log_error_errno(r, "Failed to determine timestamp: %m");
698 if (usec > arg_until)
699 continue;
df65f77b 700 }
32485d09
GS
701
702 if (arg_since != USEC_INFINITY && arg_reverse) {
703 usec_t usec;
704
705 r = sd_journal_get_realtime_usec(j, &usec);
df65f77b 706 if (r < 0)
32485d09
GS
707 return log_error_errno(r, "Failed to determine timestamp: %m");
708 if (usec < arg_since)
709 continue;
df65f77b 710 }
32485d09 711
5ce97d33 712 r = print_entry(j, n_found++, verb_is_info);
32485d09
GS
713 if (r < 0)
714 return r;
062b99e8 715 }
0c51aada
LP
716
717 if (!arg_field && n_found <= 0) {
b9aaa7f4
ZJS
718 if (!arg_quiet)
719 log_notice("No coredumps found.");
0c51aada
LP
720 return -ESRCH;
721 }
722 }
723
724 return 0;
725}
726
bb7c5bad 727static int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp) {
9fe13294
ZJS
728 const char *data;
729 _cleanup_free_ char *filename = NULL;
730 size_t len;
bb7c5bad 731 int r, fd;
fc6cec86
ZJS
732 _cleanup_close_ int fdt = -1;
733 char *temp = NULL;
ada45c78 734
bb7c5bad
ZJS
735 assert(!(file && path)); /* At most one can be specified */
736 assert(!!path == !!unlink_temp); /* Those must be specified together */
47f50642 737
fc6cec86 738 /* Look for a coredump on disk first. */
9fe13294 739 r = sd_journal_get_data(j, "COREDUMP_FILENAME", (const void**) &data, &len);
bb7c5bad 740 if (r == 0)
9fe13294 741 retrieve(data, len, "COREDUMP_FILENAME", &filename);
bb7c5bad
ZJS
742 else {
743 if (r != -ENOENT)
744 return log_error_errno(r, "Failed to retrieve COREDUMP_FILENAME field: %m");
745 /* Check that we can have a COREDUMP field. We still haven't set a high
746 * data threshold, so we'll get a few kilobytes at most.
747 */
748
749 r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len);
750 if (r == -ENOENT)
751 return log_error_errno(r, "Coredump entry has no core attached (neither internally in the journal nor externally on disk).");
752 if (r < 0)
753 return log_error_errno(r, "Failed to retrieve COREDUMP field: %m");
754 }
93b73b06 755
954d3a51
ZJS
756 if (filename) {
757 if (access(filename, R_OK) < 0)
758 return log_error_errno(errno, "File \"%s\" is not readable: %m", filename);
5de0409e 759
954d3a51 760 if (path && !endswith(filename, ".xz") && !endswith(filename, ".lz4")) {
ae2a15bc 761 *path = TAKE_PTR(filename);
a276ae74 762
954d3a51
ZJS
763 return 0;
764 }
fc6cec86 765 }
a276ae74 766
bb7c5bad 767 if (path) {
fc6cec86 768 const char *vt;
992e8f22 769
fc6cec86 770 /* Create a temporary file to write the uncompressed core to. */
992e8f22 771
fc6cec86
ZJS
772 r = var_tmp_dir(&vt);
773 if (r < 0)
774 return log_error_errno(r, "Failed to acquire temporary directory path: %m");
9fe13294 775
605405c6 776 temp = strjoin(vt, "/coredump-XXXXXX");
fc6cec86
ZJS
777 if (!temp)
778 return log_oom();
9fe13294 779
fc6cec86
ZJS
780 fdt = mkostemp_safe(temp);
781 if (fdt < 0)
782 return log_error_errno(fdt, "Failed to create temporary file: %m");
783 log_debug("Created temporary file %s", temp);
9fe13294 784
fc6cec86 785 fd = fdt;
bb7c5bad
ZJS
786 } else {
787 /* If neither path or file are specified, we will write to stdout. Let's now check
788 * if stdout is connected to a tty. We checked that the file exists, or that the
789 * core might be stored in the journal. In this second case, if we found the entry,
790 * in all likelyhood we will be able to access the COREDUMP= field. In either case,
791 * we stop before doing any "real" work, i.e. before starting decompression or
792 * reading from the file or creating temporary files.
793 */
794 if (!file) {
795 if (on_tty())
796 return log_error_errno(ENOTTY, "Refusing to dump core to tty"
797 " (use shell redirection or specify --output).");
798 file = stdout;
799 }
800
801 fd = fileno(file);
fc6cec86
ZJS
802 }
803
804 if (filename) {
349cc4a5 805#if HAVE_XZ || HAVE_LZ4
fc6cec86 806 _cleanup_close_ int fdf;
9fe13294 807
fc6cec86
ZJS
808 fdf = open(filename, O_RDONLY | O_CLOEXEC);
809 if (fdf < 0) {
810 r = log_error_errno(errno, "Failed to open %s: %m", filename);
2fb8159f 811 goto error;
fc6cec86
ZJS
812 }
813
814 r = decompress_stream(filename, fdf, fd, -1);
815 if (r < 0) {
816 log_error_errno(r, "Failed to decompress %s: %m", filename);
9fe13294
ZJS
817 goto error;
818 }
fc6cec86
ZJS
819#else
820 log_error("Cannot decompress file. Compiled without compression support.");
821 r = -EOPNOTSUPP;
822 goto error;
823#endif
824 } else {
825 ssize_t sz;
a276ae74 826
bb7c5bad
ZJS
827 /* We want full data, nothing truncated. */
828 sd_journal_set_data_threshold(j, 0);
829
fc6cec86
ZJS
830 r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len);
831 if (r < 0)
bb7c5bad 832 return log_error_errno(r, "Failed to retrieve COREDUMP field: %m");
fc6cec86
ZJS
833
834 assert(len >= 9);
835 data += 9;
836 len -= 9;
837
954d3a51 838 sz = write(fd, data, len);
fc6cec86 839 if (sz < 0) {
954d3a51 840 r = log_error_errno(errno, "Failed to write output: %m");
fc6cec86 841 goto error;
a276ae74 842 }
fc6cec86 843 if (sz != (ssize_t) len) {
954d3a51 844 log_error("Short write to output.");
fc6cec86
ZJS
845 r = -EIO;
846 goto error;
847 }
848 }
a276ae74 849
fc6cec86
ZJS
850 if (temp) {
851 *path = temp;
852 *unlink_temp = true;
853 }
854 return 0;
9fe13294
ZJS
855
856error:
fc6cec86
ZJS
857 if (temp) {
858 unlink(temp);
859 log_debug("Removed temporary file %s", temp);
9fe13294 860 }
fc6cec86 861 return r;
9fe13294 862}
a276ae74 863
5ce97d33
YW
864static int dump_core(int argc, char **argv, void *userdata) {
865 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
9fe13294
ZJS
866 int r;
867
5ce97d33
YW
868 if (arg_field) {
869 log_error("Option --field/-F only makes sense with list");
870 return -EINVAL;
871 }
872
873 r = acquire_journal(&j, argv + 1);
874 if (r < 0)
875 return r;
9fe13294
ZJS
876
877 r = focus(j);
878 if (r < 0)
ada45c78 879 return r;
ada45c78 880
3774cf57 881 print_info(arg_output ? stdout : stderr, j, false);
9fe13294 882
bb7c5bad 883 r = save_core(j, arg_output, NULL, NULL);
23bbb0de 884 if (r < 0)
bb7c5bad 885 return r;
5de0409e
ZJS
886
887 r = sd_journal_previous(j);
b9aaa7f4
ZJS
888 if (r > 0 && !arg_quiet)
889 log_notice("More than one entry matches, ignoring rest.");
5de0409e
ZJS
890
891 return 0;
892}
893
c5896b6a 894static int run_debug(int argc, char **argv, void *userdata) {
5ce97d33 895 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
de8f6e54 896 _cleanup_free_ char *exe = NULL, *path = NULL;
9fe13294 897 bool unlink_path = false;
c5896b6a 898 const char *data, *fork_name;
ada45c78 899 size_t len;
ada45c78 900 pid_t pid;
ada45c78 901 int r;
ada45c78 902
c5896b6a
RG
903 if (!arg_debugger) {
904 char *env_debugger;
905
906 env_debugger = getenv("SYSTEMD_DEBUGGER");
907 if (env_debugger)
908 arg_debugger = env_debugger;
909 else
910 arg_debugger = "gdb";
911 }
912
5ce97d33
YW
913 if (arg_field) {
914 log_error("Option --field/-F only makes sense with list");
915 return -EINVAL;
916 }
917
918 r = acquire_journal(&j, argv + 1);
919 if (r < 0)
920 return r;
ada45c78
LP
921
922 r = focus(j);
923 if (r < 0)
924 return r;
925
e15758cc
LP
926 print_info(stdout, j, false);
927 fputs("\n", stdout);
ada45c78
LP
928
929 r = sd_journal_get_data(j, "COREDUMP_EXE", (const void**) &data, &len);
23bbb0de
MS
930 if (r < 0)
931 return log_error_errno(r, "Failed to retrieve COREDUMP_EXE field: %m");
ada45c78 932
fbd0b64f
LP
933 assert(len > STRLEN("COREDUMP_EXE="));
934 data += STRLEN("COREDUMP_EXE=");
935 len -= STRLEN("COREDUMP_EXE=");
ada45c78
LP
936
937 exe = strndup(data, len);
938 if (!exe)
939 return log_oom();
940
941 if (endswith(exe, " (deleted)")) {
942 log_error("Binary already deleted.");
943 return -ENOENT;
944 }
945
a276ae74
LP
946 if (!path_is_absolute(exe)) {
947 log_error("Binary is not an absolute path.");
948 return -ENOENT;
949 }
950
bb7c5bad 951 r = save_core(j, NULL, &path, &unlink_path);
23bbb0de 952 if (r < 0)
bb7c5bad 953 return r;
ada45c78 954
3e7bc89b
FB
955 /* Don't interfere with gdb and its handling of SIGINT. */
956 (void) ignore_signals(SIGINT, -1);
957
c5896b6a
RG
958 fork_name = strjoina("(", arg_debugger, ")");
959
960 r = safe_fork(fork_name, FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_LOG, &pid);
b6e1fff1 961 if (r < 0)
ada45c78 962 goto finish;
4c253ed1 963 if (r == 0) {
c5896b6a 964 execlp(arg_debugger, arg_debugger, exe, "-c", path, NULL);
0b1f3c76 965 log_open();
c5896b6a 966 log_error_errno(errno, "Failed to invoke %s: %m", arg_debugger);
a45d7127 967 _exit(EXIT_FAILURE);
ada45c78
LP
968 }
969
c5896b6a 970 r = wait_for_terminate_and_check(arg_debugger, pid, WAIT_LOG_ABNORMAL);
ada45c78
LP
971
972finish:
3e7bc89b
FB
973 (void) default_signals(SIGINT, -1);
974
9fe13294
ZJS
975 if (unlink_path) {
976 log_debug("Removed temporary file %s", path);
977 unlink(path);
978 }
a276ae74 979
ada45c78
LP
980 return r;
981}
982
012f2b7d
ZJS
983static int check_units_active(void) {
984 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
985 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
986 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
987 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
988 int c = 0, r;
7bbf2d84 989 const char *id, *state, *substate;
012f2b7d 990
b9aaa7f4
ZJS
991 if (arg_quiet)
992 return false;
993
012f2b7d
ZJS
994 r = sd_bus_default_system(&bus);
995 if (r < 0)
996 return log_error_errno(r, "Failed to acquire bus: %m");
997
998 r = sd_bus_message_new_method_call(
999 bus,
1000 &m,
1001 "org.freedesktop.systemd1",
1002 "/org/freedesktop/systemd1",
1003 "org.freedesktop.systemd1.Manager",
1004 "ListUnitsByPatterns");
1005 if (r < 0)
1006 return bus_log_create_error(r);
1007
1008 r = sd_bus_message_append_strv(m, NULL);
1009 if (r < 0)
1010 return bus_log_create_error(r);
1011
1012 r = sd_bus_message_append_strv(m, STRV_MAKE("systemd-coredump@*.service"));
1013 if (r < 0)
1014 return bus_log_create_error(r);
1015
501551e8 1016 r = sd_bus_call(bus, m, SHORT_BUS_CALL_TIMEOUT_USEC, &error, &reply);
012f2b7d
ZJS
1017 if (r < 0)
1018 return log_error_errno(r, "Failed to check if any systemd-coredump@.service units are running: %s",
1019 bus_error_message(&error, r));
1020
1021 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)");
1022 if (r < 0)
1023 return bus_log_parse_error(r);
1024
1025 while ((r = sd_bus_message_read(
1026 reply, "(ssssssouso)",
7bbf2d84
ZJS
1027 &id, NULL, NULL, &state, &substate,
1028 NULL, NULL, NULL, NULL, NULL)) > 0) {
eb5877a0 1029 bool found = !STR_IN_SET(state, "inactive", "dead", "failed");
7bbf2d84
ZJS
1030 log_debug("Unit %s is %s/%s, %scounting it.", id, state, substate, found ? "" : "not ");
1031 c += found;
1032 }
012f2b7d
ZJS
1033 if (r < 0)
1034 return bus_log_parse_error(r);
1035
1036 r = sd_bus_message_exit_container(reply);
1037 if (r < 0)
1038 return bus_log_parse_error(r);
1039
1040 return c;
1041}
1042
5ce97d33
YW
1043static int coredumpctl_main(int argc, char *argv[]) {
1044
1045 static const Verb verbs[] = {
c5896b6a
RG
1046 { "list", VERB_ANY, VERB_ANY, VERB_DEFAULT, dump_list },
1047 { "info", VERB_ANY, VERB_ANY, 0, dump_list },
1048 { "dump", VERB_ANY, VERB_ANY, 0, dump_core },
1049 { "debug", VERB_ANY, VERB_ANY, 0, run_debug },
1050 { "gdb", VERB_ANY, VERB_ANY, 0, run_debug },
5ce97d33
YW
1051 {}
1052 };
1053
1054 return dispatch_verb(argc, argv, verbs, NULL);
1055}
1056
5de0409e 1057int main(int argc, char *argv[]) {
5ce97d33 1058 int r, units_active;
5de0409e 1059
a9cdc94f 1060 setlocale(LC_ALL, "");
5de0409e
ZJS
1061 log_parse_environment();
1062 log_open();
1063
5ab9ed07 1064 r = parse_argv(argc, argv);
5ce97d33 1065 if (r <= 0)
5de0409e
ZJS
1066 goto end;
1067
2cf4172a
LP
1068 sigbus_install();
1069
012f2b7d
ZJS
1070 units_active = check_units_active(); /* error is treated the same as 0 */
1071
5ce97d33 1072 r = coredumpctl_main(argc, argv);
5de0409e 1073
012f2b7d
ZJS
1074 if (units_active > 0)
1075 printf("%s-- Notice: %d systemd-coredump@.service %s, output may be incomplete.%s\n",
1076 ansi_highlight_red(),
1077 units_active, units_active == 1 ? "unit is running" : "units are running",
1078 ansi_normal());
5de0409e 1079end:
5de0409e
ZJS
1080 pager_close();
1081
f5c4b520 1082 safe_fclose(arg_output);
5de0409e 1083
ada45c78 1084 return r >= 0 ? r : EXIT_FAILURE;
5de0409e 1085}