1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include "sd-journal.h"
11 #include "sd-messages.h"
13 #include "alloc-util.h"
14 #include "bus-error.h"
19 #include "format-table.h"
21 #include "glob-util.h"
22 #include "journal-internal.h"
23 #include "journal-util.h"
26 #include "main-func.h"
28 #include "parse-argument.h"
29 #include "parse-util.h"
30 #include "path-util.h"
31 #include "pretty-print.h"
32 #include "process-util.h"
33 #include "rlimit-util.h"
35 #include "signal-util.h"
36 #include "string-util.h"
38 #include "terminal-util.h"
39 #include "tmpfile-util.h"
40 #include "user-util.h"
44 #define SHORT_BUS_CALL_TIMEOUT_USEC (3 * USEC_PER_SEC)
46 static usec_t arg_since
= USEC_INFINITY
, arg_until
= USEC_INFINITY
;
47 static const char* arg_field
= NULL
;
48 static const char *arg_debugger
= NULL
;
49 static char **arg_debugger_args
= NULL
;
50 static const char *arg_directory
= NULL
;
51 static char **arg_file
= NULL
;
52 static JsonFormatFlags arg_json_format_flags
= JSON_FORMAT_OFF
;
53 static PagerFlags arg_pager_flags
= 0;
54 static int arg_legend
= true;
55 static size_t arg_rows_max
= SIZE_MAX
;
56 static const char* arg_output
= NULL
;
57 static bool arg_reverse
= false;
58 static bool arg_quiet
= false;
59 static bool arg_all
= false;
61 STATIC_DESTRUCTOR_REGISTER(arg_debugger_args
, strv_freep
);
62 STATIC_DESTRUCTOR_REGISTER(arg_file
, strv_freep
);
64 static int add_match(sd_journal
*j
, const char *match
) {
65 _cleanup_free_
char *p
= NULL
;
66 const char* prefix
, *pattern
;
70 if (strchr(match
, '='))
72 else if (strchr(match
, '/')) {
73 r
= path_make_absolute_cwd(match
, &p
);
75 return log_error_errno(r
, "path_make_absolute_cwd(\"%s\"): %m", match
);
78 prefix
= "COREDUMP_EXE=";
79 } else if (parse_pid(match
, &pid
) >= 0)
80 prefix
= "COREDUMP_PID=";
82 prefix
= "COREDUMP_COMM=";
84 pattern
= strjoina(prefix
, match
);
85 log_debug("Adding match: %s", pattern
);
86 r
= sd_journal_add_match(j
, pattern
, 0);
88 return log_error_errno(r
, "Failed to add match \"%s\": %m", match
);
93 static int add_matches(sd_journal
*j
, char **matches
) {
97 r
= sd_journal_add_match(j
, "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR
, 0);
99 return log_error_errno(r
, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR
);
101 r
= sd_journal_add_match(j
, "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR
, 0);
103 return log_error_errno(r
, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR
);
105 STRV_FOREACH(match
, matches
) {
106 r
= add_match(j
, *match
);
114 static int acquire_journal(sd_journal
**ret
, char **matches
) {
115 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
121 r
= sd_journal_open_directory(&j
, arg_directory
, 0);
123 return log_error_errno(r
, "Failed to open journals in directory: %s: %m", arg_directory
);
124 } else if (arg_file
) {
125 r
= sd_journal_open_files(&j
, (const char**)arg_file
, 0);
127 return log_error_errno(r
, "Failed to open journal files: %m");
129 r
= sd_journal_open(&j
, arg_all
? 0 : SD_JOURNAL_LOCAL_ONLY
);
131 return log_error_errno(r
, "Failed to open journal: %m");
134 r
= journal_access_check_and_warn(j
, arg_quiet
, true);
138 r
= add_matches(j
, matches
);
143 _cleanup_free_
char *filter
= NULL
;
145 filter
= journal_make_match_string(j
);
146 log_debug("Journal filter: %s", filter
);
154 static int verb_help(int argc
, char **argv
, void *userdata
) {
155 _cleanup_free_
char *link
= NULL
;
158 r
= terminal_urlify_man("coredumpctl", "1", &link
);
162 printf("%1$s [OPTIONS...] COMMAND ...\n\n"
163 "%5$sList or retrieve coredumps from the journal.%6$s\n"
164 "\n%3$sCommands:%4$s\n"
165 " list [MATCHES...] List available coredumps (default)\n"
166 " info [MATCHES...] Show detailed information about one or more coredumps\n"
167 " dump [MATCHES...] Print first matching coredump to stdout\n"
168 " debug [MATCHES...] Start a debugger for the first matching coredump\n"
169 "\n%3$sOptions:%4$s\n"
170 " -h --help Show this help\n"
171 " --version Print version string\n"
172 " --no-pager Do not pipe output into a pager\n"
173 " --no-legend Do not print the column headers\n"
174 " --json=pretty|short|off\n"
175 " Generate JSON output\n"
176 " --debugger=DEBUGGER Use the given debugger\n"
177 " -A --debugger-arguments=ARGS Pass the given arguments to the debugger\n"
178 " -n INT Show maximum number of rows\n"
179 " -1 Show information about most recent entry only\n"
180 " -S --since=DATE Only print coredumps since the date\n"
181 " -U --until=DATE Only print coredumps until the date\n"
182 " -r --reverse Show the newest entries first\n"
183 " -F --field=FIELD List all values a certain field takes\n"
184 " -o --output=FILE Write output to FILE\n"
185 " --file=PATH Use journal file\n"
186 " -D --directory=DIR Use journal files from directory\n\n"
187 " -q --quiet Do not show info messages and privilege warning\n"
188 " --all Look at all journal files instead of local ones\n"
189 "\nSee the %2$s for details.\n",
190 program_invocation_short_name
,
200 static int parse_argv(int argc
, char *argv
[]) {
213 static const struct option options
[] = {
214 { "help", no_argument
, NULL
, 'h' },
215 { "version" , no_argument
, NULL
, ARG_VERSION
},
216 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
217 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
218 { "debugger", required_argument
, NULL
, ARG_DEBUGGER
},
219 { "debugger-arguments", required_argument
, NULL
, 'A' },
220 { "output", required_argument
, NULL
, 'o' },
221 { "field", required_argument
, NULL
, 'F' },
222 { "file", required_argument
, NULL
, ARG_FILE
},
223 { "directory", required_argument
, NULL
, 'D' },
224 { "reverse", no_argument
, NULL
, 'r' },
225 { "since", required_argument
, NULL
, 'S' },
226 { "until", required_argument
, NULL
, 'U' },
227 { "quiet", no_argument
, NULL
, 'q' },
228 { "json", required_argument
, NULL
, ARG_JSON
},
229 { "all", no_argument
, NULL
, ARG_ALL
},
236 while ((c
= getopt_long(argc
, argv
, "hA:o:F:1D:rS:U:qn:", options
, NULL
)) >= 0)
239 return verb_help(0, NULL
, NULL
);
245 arg_pager_flags
|= PAGER_DISABLE
;
253 arg_debugger
= optarg
;
257 _cleanup_strv_free_
char **l
= NULL
;
258 r
= strv_split_full(&l
, optarg
, WHITESPACE
, EXTRACT_UNQUOTE
);
260 return log_error_errno(r
, "Failed to parse debugger arguments '%s': %m", optarg
);
261 strv_free_and_replace(arg_debugger_args
, l
);
266 r
= glob_extend(&arg_file
, optarg
, GLOB_NOCHECK
);
268 return log_error_errno(r
, "Failed to add paths: %m");
273 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
274 "Cannot set output more than once.");
280 r
= parse_timestamp(optarg
, &arg_since
);
282 return log_error_errno(r
, "Failed to parse timestamp '%s': %m", optarg
);
286 r
= parse_timestamp(optarg
, &arg_until
);
288 return log_error_errno(r
, "Failed to parse timestamp '%s': %m", optarg
);
293 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
294 "Cannot use --field/-F more than once.");
306 r
= safe_atou(optarg
, &n
);
308 return log_error_errno(r
< 0 ? r
: SYNTHETIC_ERRNO(EINVAL
),
309 "Invalid numeric parameter to -n: %s", optarg
);
316 arg_directory
= optarg
;
328 r
= parse_json_argument(optarg
, &arg_json_format_flags
);
342 assert_not_reached();
345 if (arg_since
!= USEC_INFINITY
&& arg_until
!= USEC_INFINITY
&&
346 arg_since
> arg_until
)
347 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
348 "--since= must be before --until=.");
353 static int retrieve(const void *data
,
361 ident
= strlen(name
) + 1; /* name + "=" */
366 if (memcmp(data
, name
, ident
- 1) != 0)
369 if (((const char*) data
)[ident
- 1] != '=')
372 v
= strndup((const char*)data
+ ident
, len
- ident
);
376 free_and_replace(*var
, v
);
380 static int print_field(FILE* file
, sd_journal
*j
) {
389 /* A (user-specified) field may appear more than once for a given entry.
390 * We will print all of the occurrences.
391 * This is different below for fields that systemd-coredump uses,
392 * because they cannot meaningfully appear more than once.
394 SD_JOURNAL_FOREACH_DATA(j
, d
, l
) {
395 _cleanup_free_
char *value
= NULL
;
398 r
= retrieve(d
, l
, arg_field
, &value
);
402 fprintf(file
, "%s\n", value
);
408 #define RETRIEVE(d, l, name, arg) \
410 int _r = retrieve(d, l, name, &arg); \
417 static void analyze_coredump_file(
419 const char **ret_state
,
420 const char **ret_color
,
421 uint64_t *ret_size
) {
423 _cleanup_close_
int fd
= -1;
432 fd
= open(path
, O_PATH
|O_CLOEXEC
);
434 if (errno
== ENOENT
) {
435 *ret_state
= "missing";
436 *ret_color
= ansi_grey();
437 *ret_size
= UINT64_MAX
;
443 r
= access_fd(fd
, R_OK
);
444 if (ERRNO_IS_PRIVILEGE(r
)) {
445 *ret_state
= "inaccessible";
446 *ret_color
= ansi_highlight_yellow();
447 *ret_size
= UINT64_MAX
;
453 if (fstat(fd
, &st
) < 0)
456 if (!S_ISREG(st
.st_mode
))
459 *ret_state
= "present";
461 *ret_size
= st
.st_size
;
465 *ret_state
= "error";
466 *ret_color
= ansi_highlight_red();
467 *ret_size
= UINT64_MAX
;
470 static int print_list(FILE* file
, sd_journal
*j
, Table
*t
) {
472 *mid
= NULL
, *pid
= NULL
, *uid
= NULL
, *gid
= NULL
,
473 *sgnl
= NULL
, *exe
= NULL
, *comm
= NULL
, *cmdline
= NULL
,
474 *filename
= NULL
, *truncated
= NULL
, *coredump
= NULL
;
478 int r
, signal_as_int
= 0;
479 const char *present
= NULL
, *color
= NULL
;
480 uint64_t size
= UINT64_MAX
;
481 bool normal_coredump
;
482 uid_t uid_as_int
= UID_INVALID
;
483 gid_t gid_as_int
= GID_INVALID
;
484 pid_t pid_as_int
= 0;
490 SD_JOURNAL_FOREACH_DATA(j
, d
, l
) {
491 RETRIEVE(d
, l
, "MESSAGE_ID", mid
);
492 RETRIEVE(d
, l
, "COREDUMP_PID", pid
);
493 RETRIEVE(d
, l
, "COREDUMP_UID", uid
);
494 RETRIEVE(d
, l
, "COREDUMP_GID", gid
);
495 RETRIEVE(d
, l
, "COREDUMP_SIGNAL", sgnl
);
496 RETRIEVE(d
, l
, "COREDUMP_EXE", exe
);
497 RETRIEVE(d
, l
, "COREDUMP_COMM", comm
);
498 RETRIEVE(d
, l
, "COREDUMP_CMDLINE", cmdline
);
499 RETRIEVE(d
, l
, "COREDUMP_FILENAME", filename
);
500 RETRIEVE(d
, l
, "COREDUMP_TRUNCATED", truncated
);
501 RETRIEVE(d
, l
, "COREDUMP", coredump
);
504 if (!pid
&& !uid
&& !gid
&& !sgnl
&& !exe
&& !comm
&& !cmdline
&& !filename
)
505 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
), "Empty coredump log entry");
507 (void) parse_uid(uid
, &uid_as_int
);
508 (void) parse_gid(gid
, &gid_as_int
);
509 (void) parse_pid(pid
, &pid_as_int
);
510 signal_as_int
= signal_from_string(sgnl
);
512 r
= sd_journal_get_realtime_usec(j
, &ts
);
514 return log_error_errno(r
, "Failed to get realtime timestamp: %m");
516 normal_coredump
= streq_ptr(mid
, SD_MESSAGE_COREDUMP_STR
);
519 analyze_coredump_file(filename
, &present
, &color
, &size
);
522 else if (normal_coredump
) {
528 if (STRPTR_IN_SET(present
, "present", "journal") && truncated
&& parse_boolean(truncated
) > 0)
529 present
= "truncated";
534 TABLE_PID
, pid_as_int
,
535 TABLE_UID
, uid_as_int
,
536 TABLE_GID
, gid_as_int
,
537 TABLE_SIGNAL
, normal_coredump
? signal_as_int
: 0,
538 TABLE_STRING
, present
,
539 TABLE_SET_COLOR
, color
,
540 TABLE_STRING
, exe
?: comm
?: cmdline
,
543 return table_log_add_error(r
);
548 static int print_info(FILE *file
, sd_journal
*j
, bool need_space
) {
550 *mid
= NULL
, *pid
= NULL
, *uid
= NULL
, *gid
= NULL
,
551 *sgnl
= NULL
, *exe
= NULL
, *comm
= NULL
, *cmdline
= NULL
,
552 *unit
= NULL
, *user_unit
= NULL
, *session
= NULL
,
553 *boot_id
= NULL
, *machine_id
= NULL
, *hostname
= NULL
,
554 *slice
= NULL
, *cgroup
= NULL
, *owner_uid
= NULL
,
555 *message
= NULL
, *timestamp
= NULL
, *filename
= NULL
,
556 *truncated
= NULL
, *coredump
= NULL
,
557 *pkgmeta_name
= NULL
, *pkgmeta_version
= NULL
, *pkgmeta_json
= NULL
;
560 bool normal_coredump
;
566 (void) sd_journal_set_data_threshold(j
, 0);
568 SD_JOURNAL_FOREACH_DATA(j
, d
, l
) {
569 RETRIEVE(d
, l
, "MESSAGE_ID", mid
);
570 RETRIEVE(d
, l
, "COREDUMP_PID", pid
);
571 RETRIEVE(d
, l
, "COREDUMP_UID", uid
);
572 RETRIEVE(d
, l
, "COREDUMP_GID", gid
);
573 RETRIEVE(d
, l
, "COREDUMP_SIGNAL", sgnl
);
574 RETRIEVE(d
, l
, "COREDUMP_EXE", exe
);
575 RETRIEVE(d
, l
, "COREDUMP_COMM", comm
);
576 RETRIEVE(d
, l
, "COREDUMP_CMDLINE", cmdline
);
577 RETRIEVE(d
, l
, "COREDUMP_HOSTNAME", hostname
);
578 RETRIEVE(d
, l
, "COREDUMP_UNIT", unit
);
579 RETRIEVE(d
, l
, "COREDUMP_USER_UNIT", user_unit
);
580 RETRIEVE(d
, l
, "COREDUMP_SESSION", session
);
581 RETRIEVE(d
, l
, "COREDUMP_OWNER_UID", owner_uid
);
582 RETRIEVE(d
, l
, "COREDUMP_SLICE", slice
);
583 RETRIEVE(d
, l
, "COREDUMP_CGROUP", cgroup
);
584 RETRIEVE(d
, l
, "COREDUMP_TIMESTAMP", timestamp
);
585 RETRIEVE(d
, l
, "COREDUMP_FILENAME", filename
);
586 RETRIEVE(d
, l
, "COREDUMP_TRUNCATED", truncated
);
587 RETRIEVE(d
, l
, "COREDUMP", coredump
);
588 RETRIEVE(d
, l
, "COREDUMP_PACKAGE_NAME", pkgmeta_name
);
589 RETRIEVE(d
, l
, "COREDUMP_PACKAGE_VERSION", pkgmeta_version
);
590 RETRIEVE(d
, l
, "COREDUMP_PACKAGE_JSON", pkgmeta_json
);
591 RETRIEVE(d
, l
, "_BOOT_ID", boot_id
);
592 RETRIEVE(d
, l
, "_MACHINE_ID", machine_id
);
593 RETRIEVE(d
, l
, "MESSAGE", message
);
599 normal_coredump
= streq_ptr(mid
, SD_MESSAGE_COREDUMP_STR
);
603 " PID: %s%s%s (%s)\n",
604 ansi_highlight(), strna(pid
), ansi_normal(), comm
);
608 ansi_highlight(), strna(pid
), ansi_normal());
613 if (parse_uid(uid
, &n
) >= 0) {
614 _cleanup_free_
char *u
= NULL
;
630 if (parse_gid(gid
, &n
) >= 0) {
631 _cleanup_free_
char *g
= NULL
;
646 const char *name
= normal_coredump
? "Signal" : "Reason";
648 if (normal_coredump
&& safe_atoi(sgnl
, &sig
) >= 0)
649 fprintf(file
, " %s: %s (%s)\n", name
, sgnl
, signal_to_string(sig
));
651 fprintf(file
, " %s: %s\n", name
, sgnl
);
657 r
= safe_atou64(timestamp
, &u
);
659 fprintf(file
, " Timestamp: %s (%s)\n",
660 FORMAT_TIMESTAMP(u
), FORMAT_TIMESTAMP_RELATIVE(u
));
663 fprintf(file
, " Timestamp: %s\n", timestamp
);
667 fprintf(file
, " Command Line: %s\n", cmdline
);
669 fprintf(file
, " Executable: %s%s%s\n", ansi_highlight(), exe
, ansi_normal());
671 fprintf(file
, " Control Group: %s\n", cgroup
);
673 fprintf(file
, " Unit: %s\n", unit
);
675 fprintf(file
, " User Unit: %s\n", user_unit
);
677 fprintf(file
, " Slice: %s\n", slice
);
679 fprintf(file
, " Session: %s\n", session
);
683 if (parse_uid(owner_uid
, &n
) >= 0) {
684 _cleanup_free_
char *u
= NULL
;
688 " Owner UID: %s (%s)\n",
697 fprintf(file
, " Boot ID: %s\n", boot_id
);
699 fprintf(file
, " Machine ID: %s\n", machine_id
);
701 fprintf(file
, " Hostname: %s\n", hostname
);
704 const char *state
= NULL
, *color
= NULL
;
705 uint64_t size
= UINT64_MAX
;
707 analyze_coredump_file(filename
, &state
, &color
, &size
);
709 if (STRPTR_IN_SET(state
, "present", "journal") && truncated
&& parse_boolean(truncated
) > 0)
713 " Storage: %s%s (%s)%s\n",
719 if (size
!= UINT64_MAX
)
720 fprintf(file
, " Disk Size: %s\n", FORMAT_BYTES(size
));
723 fprintf(file
, " Storage: journal\n");
725 fprintf(file
, " Storage: none\n");
727 if (pkgmeta_name
&& pkgmeta_version
)
728 fprintf(file
, " Package: %s/%s\n", pkgmeta_name
, pkgmeta_version
);
730 /* Print out the build-id of the 'main' ELF module, by matching the JSON key
731 * with the 'exe' field. */
732 if (exe
&& pkgmeta_json
) {
733 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
735 r
= json_parse(pkgmeta_json
, 0, &v
, NULL
, NULL
);
737 log_warning_errno(r
, "json_parse on %s failed, ignoring: %m", pkgmeta_json
);
739 const char *module_name
;
740 JsonVariant
*module_json
;
742 JSON_VARIANT_OBJECT_FOREACH(module_name
, module_json
, v
) {
743 JsonVariant
*build_id
;
745 /* We only print the build-id for the 'main' ELF module */
746 if (!path_equal_filename(module_name
, exe
))
749 build_id
= json_variant_by_key(module_json
, "buildId");
751 fprintf(file
, " build-id: %s\n", json_variant_string(build_id
));
759 _cleanup_free_
char *m
= NULL
;
761 m
= strreplace(message
, "\n", "\n ");
763 fprintf(file
, " Message: %s\n", strstrip(m
?: message
));
769 static int focus(sd_journal
*j
) {
772 r
= sd_journal_seek_tail(j
);
774 r
= sd_journal_previous(j
);
776 return log_error_errno(r
, "Failed to search journal: %m");
778 return log_error_errno(SYNTHETIC_ERRNO(ESRCH
),
783 static int print_entry(
791 return print_list(stdout
, j
, t
);
793 return print_field(stdout
, j
);
795 return print_info(stdout
, j
, n_found
> 0);
798 static int dump_list(int argc
, char **argv
, void *userdata
) {
799 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
800 _cleanup_(table_unrefp
) Table
*t
= NULL
;
805 verb_is_info
= argc
>= 1 && streq(argv
[0], "info");
807 r
= acquire_journal(&j
, argv
+ 1);
811 /* The coredumps are likely compressed, and for just listing them we don't need to decompress them,
812 * so let's pick a fairly low data threshold here */
813 (void) sd_journal_set_data_threshold(j
, 4096);
815 if (!verb_is_info
&& !arg_field
) {
816 t
= table_new("time", "pid", "uid", "gid", "sig", "corefile", "exe", "size");
820 (void) table_set_align_percent(t
, TABLE_HEADER_CELL(1), 100);
821 (void) table_set_align_percent(t
, TABLE_HEADER_CELL(2), 100);
822 (void) table_set_align_percent(t
, TABLE_HEADER_CELL(3), 100);
823 (void) table_set_align_percent(t
, TABLE_HEADER_CELL(7), 100);
825 (void) table_set_empty_string(t
, "-");
827 pager_open(arg_pager_flags
);
829 /* "info" without pattern implies "-1" */
830 if ((arg_rows_max
== 1 && arg_reverse
) || (verb_is_info
&& argc
== 1)) {
835 r
= print_entry(j
, 0, t
);
839 if (arg_since
!= USEC_INFINITY
&& !arg_reverse
)
840 r
= sd_journal_seek_realtime_usec(j
, arg_since
);
841 else if (arg_until
!= USEC_INFINITY
&& arg_reverse
)
842 r
= sd_journal_seek_realtime_usec(j
, arg_until
);
843 else if (arg_reverse
)
844 r
= sd_journal_seek_tail(j
);
846 r
= sd_journal_seek_head(j
);
848 return log_error_errno(r
, "Failed to seek to date: %m");
852 r
= sd_journal_next(j
);
854 r
= sd_journal_previous(j
);
856 return log_error_errno(r
, "Failed to iterate through journal: %m");
860 if (arg_until
!= USEC_INFINITY
&& !arg_reverse
) {
863 r
= sd_journal_get_realtime_usec(j
, &usec
);
865 return log_error_errno(r
, "Failed to determine timestamp: %m");
866 if (usec
> arg_until
)
870 if (arg_since
!= USEC_INFINITY
&& arg_reverse
) {
873 r
= sd_journal_get_realtime_usec(j
, &usec
);
875 return log_error_errno(r
, "Failed to determine timestamp: %m");
876 if (usec
< arg_since
)
880 r
= print_entry(j
, n_found
++, t
);
884 if (arg_rows_max
!= SIZE_MAX
&& n_found
>= arg_rows_max
)
888 if (!arg_field
&& n_found
<= 0) {
890 log_notice("No coredumps found.");
896 r
= table_print_with_pager(t
, arg_json_format_flags
, arg_pager_flags
, arg_legend
);
904 static int save_core(sd_journal
*j
, FILE *file
, char **path
, bool *unlink_temp
) {
906 _cleanup_free_
char *filename
= NULL
;
909 _cleanup_close_
int fdt
= -1;
912 assert(!(file
&& path
)); /* At most one can be specified */
913 assert(!!path
== !!unlink_temp
); /* Those must be specified together */
915 /* Look for a coredump on disk first. */
916 r
= sd_journal_get_data(j
, "COREDUMP_FILENAME", (const void**) &data
, &len
);
918 r
= retrieve(data
, len
, "COREDUMP_FILENAME", &filename
);
923 if (access(filename
, R_OK
) < 0)
924 return log_error_errno(errno
, "File \"%s\" is not readable: %m", filename
);
926 if (path
&& !ENDSWITH_SET(filename
, ".xz", ".lz4", ".zst")) {
927 *path
= TAKE_PTR(filename
);
934 return log_error_errno(r
, "Failed to retrieve COREDUMP_FILENAME field: %m");
935 /* Check that we can have a COREDUMP field. We still haven't set a high
936 * data threshold, so we'll get a few kilobytes at most.
939 r
= sd_journal_get_data(j
, "COREDUMP", (const void**) &data
, &len
);
941 return log_error_errno(r
, "Coredump entry has no core attached (neither internally in the journal nor externally on disk).");
943 return log_error_errno(r
, "Failed to retrieve COREDUMP field: %m");
949 /* Create a temporary file to write the uncompressed core to. */
951 r
= var_tmp_dir(&vt
);
953 return log_error_errno(r
, "Failed to acquire temporary directory path: %m");
955 temp
= path_join(vt
, "coredump-XXXXXX");
959 fdt
= mkostemp_safe(temp
);
961 return log_error_errno(fdt
, "Failed to create temporary file: %m");
962 log_debug("Created temporary file %s", temp
);
966 /* If neither path or file are specified, we will write to stdout. Let's now check
967 * if stdout is connected to a tty. We checked that the file exists, or that the
968 * core might be stored in the journal. In this second case, if we found the entry,
969 * in all likelihood we will be able to access the COREDUMP= field. In either case,
970 * we stop before doing any "real" work, i.e. before starting decompression or
971 * reading from the file or creating temporary files.
975 return log_error_errno(SYNTHETIC_ERRNO(ENOTTY
),
976 "Refusing to dump core to tty"
977 " (use shell redirection or specify --output).");
986 _cleanup_close_
int fdf
= -1;
988 fdf
= open(filename
, O_RDONLY
| O_CLOEXEC
);
990 r
= log_error_errno(errno
, "Failed to open %s: %m", filename
);
994 r
= decompress_stream(filename
, fdf
, fd
, -1);
996 log_error_errno(r
, "Failed to decompress %s: %m", filename
);
1000 r
= log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1001 "Cannot decompress file. Compiled without compression support.");
1007 /* We want full data, nothing truncated. */
1008 sd_journal_set_data_threshold(j
, 0);
1010 r
= sd_journal_get_data(j
, "COREDUMP", (const void**) &data
, &len
);
1012 return log_error_errno(r
, "Failed to retrieve COREDUMP field: %m");
1018 sz
= write(fd
, data
, len
);
1020 r
= log_error_errno(errno
, "Failed to write output: %m");
1023 if (sz
!= (ssize_t
) len
) {
1024 log_error("Short write to output.");
1032 *unlink_temp
= true;
1038 (void) unlink(temp
);
1039 log_debug("Removed temporary file %s", temp
);
1044 static int dump_core(int argc
, char **argv
, void *userdata
) {
1045 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1046 _cleanup_fclose_
FILE *f
= NULL
;
1050 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1051 "Option --field/-F only makes sense with list");
1053 r
= acquire_journal(&j
, argv
+ 1);
1062 f
= fopen(arg_output
, "we");
1064 return log_error_errno(errno
, "Failed to open \"%s\" for writing: %m", arg_output
);
1067 print_info(f
? stdout
: stderr
, j
, false);
1069 r
= save_core(j
, f
, NULL
, NULL
);
1073 r
= sd_journal_previous(j
);
1074 if (r
> 0 && !arg_quiet
)
1075 log_notice("More than one entry matches, ignoring rest.");
1080 static int run_debug(int argc
, char **argv
, void *userdata
) {
1081 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1082 _cleanup_free_
char *exe
= NULL
, *path
= NULL
;
1083 _cleanup_strv_free_
char **debugger_call
= NULL
;
1084 bool unlink_path
= false;
1085 const char *data
, *fork_name
;
1090 if (!arg_debugger
) {
1093 env_debugger
= getenv("SYSTEMD_DEBUGGER");
1095 arg_debugger
= env_debugger
;
1097 arg_debugger
= "gdb";
1100 r
= strv_extend(&debugger_call
, arg_debugger
);
1104 r
= strv_extend_strv(&debugger_call
, arg_debugger_args
, false);
1109 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1110 "Option --field/-F only makes sense with list");
1112 r
= acquire_journal(&j
, argv
+ 1);
1121 print_info(stdout
, j
, false);
1122 fputs("\n", stdout
);
1125 r
= sd_journal_get_data(j
, "COREDUMP_EXE", (const void**) &data
, &len
);
1127 return log_error_errno(r
, "Failed to retrieve COREDUMP_EXE field: %m");
1129 assert(len
> STRLEN("COREDUMP_EXE="));
1130 data
+= STRLEN("COREDUMP_EXE=");
1131 len
-= STRLEN("COREDUMP_EXE=");
1133 exe
= strndup(data
, len
);
1137 if (endswith(exe
, " (deleted)"))
1138 return log_error_errno(SYNTHETIC_ERRNO(ENOENT
),
1139 "Binary already deleted.");
1141 if (!path_is_absolute(exe
))
1142 return log_error_errno(SYNTHETIC_ERRNO(ENOENT
),
1143 "Binary is not an absolute path.");
1145 r
= save_core(j
, NULL
, &path
, &unlink_path
);
1149 r
= strv_extend_strv(&debugger_call
, STRV_MAKE(exe
, "-c", path
), false);
1153 /* Don't interfere with gdb and its handling of SIGINT. */
1154 (void) ignore_signals(SIGINT
);
1156 fork_name
= strjoina("(", debugger_call
[0], ")");
1158 r
= safe_fork(fork_name
, FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_CLOSE_ALL_FDS
|FORK_RLIMIT_NOFILE_SAFE
|FORK_LOG
|FORK_FLUSH_STDIO
, &pid
);
1162 execvp(debugger_call
[0], debugger_call
);
1164 log_error_errno(errno
, "Failed to invoke %s: %m", debugger_call
[0]);
1165 _exit(EXIT_FAILURE
);
1168 r
= wait_for_terminate_and_check(debugger_call
[0], pid
, WAIT_LOG_ABNORMAL
);
1171 (void) default_signals(SIGINT
);
1174 log_debug("Removed temporary file %s", path
);
1175 (void) unlink(path
);
1181 static int check_units_active(void) {
1182 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1183 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1184 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1185 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1187 const char *id
, *state
, *substate
;
1192 r
= sd_bus_default_system(&bus
);
1194 log_debug("D-Bus is not running, skipping active unit check");
1198 return log_error_errno(r
, "Failed to acquire bus: %m");
1200 r
= sd_bus_message_new_method_call(
1203 "org.freedesktop.systemd1",
1204 "/org/freedesktop/systemd1",
1205 "org.freedesktop.systemd1.Manager",
1206 "ListUnitsByPatterns");
1208 return bus_log_create_error(r
);
1210 r
= sd_bus_message_append_strv(m
, NULL
);
1212 return bus_log_create_error(r
);
1214 r
= sd_bus_message_append_strv(m
, STRV_MAKE("systemd-coredump@*.service"));
1216 return bus_log_create_error(r
);
1218 r
= sd_bus_call(bus
, m
, SHORT_BUS_CALL_TIMEOUT_USEC
, &error
, &reply
);
1220 return log_error_errno(r
, "Failed to check if any systemd-coredump@.service units are running: %s",
1221 bus_error_message(&error
, r
));
1223 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssssssouso)");
1225 return bus_log_parse_error(r
);
1227 while ((r
= sd_bus_message_read(
1228 reply
, "(ssssssouso)",
1229 &id
, NULL
, NULL
, &state
, &substate
,
1230 NULL
, NULL
, NULL
, NULL
, NULL
)) > 0) {
1231 bool found
= !STR_IN_SET(state
, "inactive", "dead", "failed");
1232 log_debug("Unit %s is %s/%s, %scounting it.", id
, state
, substate
, found
? "" : "not ");
1236 return bus_log_parse_error(r
);
1238 r
= sd_bus_message_exit_container(reply
);
1240 return bus_log_parse_error(r
);
1245 static int coredumpctl_main(int argc
, char *argv
[]) {
1247 static const Verb verbs
[] = {
1248 { "list", VERB_ANY
, VERB_ANY
, VERB_DEFAULT
, dump_list
},
1249 { "info", VERB_ANY
, VERB_ANY
, 0, dump_list
},
1250 { "dump", VERB_ANY
, VERB_ANY
, 0, dump_core
},
1251 { "debug", VERB_ANY
, VERB_ANY
, 0, run_debug
},
1252 { "gdb", VERB_ANY
, VERB_ANY
, 0, run_debug
},
1253 { "help", VERB_ANY
, 1, 0, verb_help
},
1257 return dispatch_verb(argc
, argv
, verbs
, NULL
);
1260 static int run(int argc
, char *argv
[]) {
1261 int r
, units_active
;
1263 setlocale(LC_ALL
, "");
1266 /* The journal merging logic potentially needs a lot of fds. */
1267 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE
);
1269 r
= parse_argv(argc
, argv
);
1275 units_active
= check_units_active(); /* error is treated the same as 0 */
1277 r
= coredumpctl_main(argc
, argv
);
1279 if (units_active
> 0)
1280 printf("%s-- Notice: %d systemd-coredump@.service %s, output may be incomplete.%s\n",
1281 ansi_highlight_red(),
1282 units_active
, units_active
== 1 ? "unit is running" : "units are running",
1288 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);