2 This file is part of systemd.
4 Copyright 2012 Zbigniew Jędrzejewski-Szmek
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include "sd-journal.h"
29 #include "alloc-util.h"
34 #include "journal-internal.h"
38 #include "parse-util.h"
39 #include "path-util.h"
40 #include "process-util.h"
43 #include "signal-util.h"
44 #include "string-util.h"
45 #include "terminal-util.h"
46 #include "user-util.h"
55 } arg_action
= ACTION_LIST
;
56 static const char* arg_field
= NULL
;
57 static const char *arg_directory
= NULL
;
58 static bool arg_no_pager
= false;
59 static int arg_no_legend
= false;
60 static int arg_one
= false;
61 static FILE* arg_output
= NULL
;
63 static Set
*new_matches(void) {
74 tmp
= strdup("MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1");
81 r
= set_consume(set
, tmp
);
83 log_error_errno(r
, "failed to add to set: %m");
91 static int add_match(Set
*set
, const char *match
) {
92 _cleanup_free_
char *p
= NULL
;
98 if (strchr(match
, '='))
100 else if (strchr(match
, '/')) {
101 r
= path_make_absolute_cwd(match
, &p
);
105 prefix
= "COREDUMP_EXE=";
106 } else if (parse_pid(match
, &pid
) >= 0)
107 prefix
= "COREDUMP_PID=";
109 prefix
= "COREDUMP_COMM=";
111 pattern
= strjoin(prefix
, match
, NULL
);
117 log_debug("Adding pattern: %s", pattern
);
118 r
= set_consume(set
, pattern
);
124 return log_error_errno(r
, "Failed to add match: %m");
127 static void help(void) {
128 printf("%s [OPTIONS...]\n\n"
129 "List or retrieve coredumps from the journal.\n\n"
131 " -h --help Show this help\n"
132 " --version Print version string\n"
133 " --no-pager Do not pipe output into a pager\n"
134 " --no-legend Do not print the column headers.\n"
135 " -1 Show information about most recent entry only\n"
136 " -F --field=FIELD List all values a certain field takes\n"
137 " -o --output=FILE Write output to FILE\n\n"
138 " -D --directory=DIR Use journal files from directory\n\n"
141 " list [MATCHES...] List available coredumps (default)\n"
142 " info [MATCHES...] Show detailed information about one or more coredumps\n"
143 " dump [MATCHES...] Print first matching coredump to stdout\n"
144 " gdb [MATCHES...] Start gdb for the first matching coredump\n"
145 , program_invocation_short_name
);
148 static int parse_argv(int argc
, char *argv
[], Set
*matches
) {
157 static const struct option options
[] = {
158 { "help", no_argument
, NULL
, 'h' },
159 { "version" , no_argument
, NULL
, ARG_VERSION
},
160 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
161 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
162 { "output", required_argument
, NULL
, 'o' },
163 { "field", required_argument
, NULL
, 'F' },
164 { "directory", required_argument
, NULL
, 'D' },
171 while ((c
= getopt_long(argc
, argv
, "ho:F:1D:", options
, NULL
)) >= 0)
175 arg_action
= ACTION_NONE
;
180 arg_action
= ACTION_NONE
;
188 arg_no_legend
= true;
193 log_error("cannot set output more than once");
197 arg_output
= fopen(optarg
, "we");
199 return log_error_errno(errno
, "writing to '%s': %m", optarg
);
205 log_error("cannot use --field/-F more than once");
216 arg_directory
= optarg
;
223 assert_not_reached("Unhandled option");
227 const char *cmd
= argv
[optind
++];
228 if (streq(cmd
, "list"))
229 arg_action
= ACTION_LIST
;
230 else if (streq(cmd
, "dump"))
231 arg_action
= ACTION_DUMP
;
232 else if (streq(cmd
, "gdb"))
233 arg_action
= ACTION_GDB
;
234 else if (streq(cmd
, "info"))
235 arg_action
= ACTION_INFO
;
237 log_error("Unknown action '%s'", cmd
);
242 if (arg_field
&& arg_action
!= ACTION_LIST
) {
243 log_error("Option --field/-F only makes sense with list");
247 while (optind
< argc
) {
248 r
= add_match(matches
, argv
[optind
]);
257 static int retrieve(const void *data
,
265 ident
= strlen(name
) + 1; /* name + "=" */
270 if (memcmp(data
, name
, ident
- 1) != 0)
273 if (((const char*) data
)[ident
- 1] != '=')
276 v
= strndup((const char*)data
+ ident
, len
- ident
);
286 static void print_field(FILE* file
, sd_journal
*j
) {
287 _cleanup_free_
char *value
= NULL
;
296 SD_JOURNAL_FOREACH_DATA(j
, d
, l
)
297 retrieve(d
, l
, arg_field
, &value
);
300 fprintf(file
, "%s\n", value
);
303 static int print_list(FILE* file
, sd_journal
*j
, int had_legend
) {
305 *pid
= NULL
, *uid
= NULL
, *gid
= NULL
,
306 *sgnl
= NULL
, *exe
= NULL
, *comm
= NULL
, *cmdline
= NULL
,
307 *filename
= NULL
, *coredump
= NULL
;
311 char buf
[FORMAT_TIMESTAMP_MAX
];
318 SD_JOURNAL_FOREACH_DATA(j
, d
, l
) {
319 retrieve(d
, l
, "COREDUMP_PID", &pid
);
320 retrieve(d
, l
, "COREDUMP_UID", &uid
);
321 retrieve(d
, l
, "COREDUMP_GID", &gid
);
322 retrieve(d
, l
, "COREDUMP_SIGNAL", &sgnl
);
323 retrieve(d
, l
, "COREDUMP_EXE", &exe
);
324 retrieve(d
, l
, "COREDUMP_COMM", &comm
);
325 retrieve(d
, l
, "COREDUMP_CMDLINE", &cmdline
);
326 retrieve(d
, l
, "COREDUMP_FILENAME", &filename
);
327 retrieve(d
, l
, "COREDUMP", &coredump
);
330 if (!pid
&& !uid
&& !gid
&& !sgnl
&& !exe
&& !comm
&& !cmdline
&& !filename
) {
331 log_warning("Empty coredump log entry");
335 r
= sd_journal_get_realtime_usec(j
, &t
);
337 return log_error_errno(r
, "Failed to get realtime timestamp: %m");
339 format_timestamp(buf
, sizeof(buf
), t
);
340 present
= (filename
&& access(filename
, F_OK
) == 0) || coredump
;
342 if (!had_legend
&& !arg_no_legend
)
343 fprintf(file
, "%-*s %*s %*s %*s %*s %*s %s\n",
344 FORMAT_TIMESTAMP_WIDTH
, "TIME",
352 fprintf(file
, "%-*s %*s %*s %*s %*s %*s %s\n",
353 FORMAT_TIMESTAMP_WIDTH
, buf
,
358 1, present
? "*" : "",
359 strna(exe
?: (comm
?: cmdline
)));
364 static int print_info(FILE *file
, sd_journal
*j
, bool need_space
) {
366 *pid
= NULL
, *uid
= NULL
, *gid
= NULL
,
367 *sgnl
= NULL
, *exe
= NULL
, *comm
= NULL
, *cmdline
= NULL
,
368 *unit
= NULL
, *user_unit
= NULL
, *session
= NULL
,
369 *boot_id
= NULL
, *machine_id
= NULL
, *hostname
= NULL
,
370 *slice
= NULL
, *cgroup
= NULL
, *owner_uid
= NULL
,
371 *message
= NULL
, *timestamp
= NULL
, *filename
= NULL
,
380 SD_JOURNAL_FOREACH_DATA(j
, d
, l
) {
381 retrieve(d
, l
, "COREDUMP_PID", &pid
);
382 retrieve(d
, l
, "COREDUMP_UID", &uid
);
383 retrieve(d
, l
, "COREDUMP_GID", &gid
);
384 retrieve(d
, l
, "COREDUMP_SIGNAL", &sgnl
);
385 retrieve(d
, l
, "COREDUMP_EXE", &exe
);
386 retrieve(d
, l
, "COREDUMP_COMM", &comm
);
387 retrieve(d
, l
, "COREDUMP_CMDLINE", &cmdline
);
388 retrieve(d
, l
, "COREDUMP_UNIT", &unit
);
389 retrieve(d
, l
, "COREDUMP_USER_UNIT", &user_unit
);
390 retrieve(d
, l
, "COREDUMP_SESSION", &session
);
391 retrieve(d
, l
, "COREDUMP_OWNER_UID", &owner_uid
);
392 retrieve(d
, l
, "COREDUMP_SLICE", &slice
);
393 retrieve(d
, l
, "COREDUMP_CGROUP", &cgroup
);
394 retrieve(d
, l
, "COREDUMP_TIMESTAMP", ×tamp
);
395 retrieve(d
, l
, "COREDUMP_FILENAME", &filename
);
396 retrieve(d
, l
, "COREDUMP", &coredump
);
397 retrieve(d
, l
, "_BOOT_ID", &boot_id
);
398 retrieve(d
, l
, "_MACHINE_ID", &machine_id
);
399 retrieve(d
, l
, "_HOSTNAME", &hostname
);
400 retrieve(d
, l
, "MESSAGE", &message
);
408 " PID: %s%s%s (%s)\n",
409 ansi_highlight(), strna(pid
), ansi_normal(), comm
);
413 ansi_highlight(), strna(pid
), ansi_normal());
418 if (parse_uid(uid
, &n
) >= 0) {
419 _cleanup_free_
char *u
= NULL
;
435 if (parse_gid(gid
, &n
) >= 0) {
436 _cleanup_free_
char *g
= NULL
;
452 if (safe_atoi(sgnl
, &sig
) >= 0)
453 fprintf(file
, " Signal: %s (%s)\n", sgnl
, signal_to_string(sig
));
455 fprintf(file
, " Signal: %s\n", sgnl
);
461 r
= safe_atou64(timestamp
, &u
);
463 char absolute
[FORMAT_TIMESTAMP_MAX
], relative
[FORMAT_TIMESPAN_MAX
];
466 " Timestamp: %s (%s)\n",
467 format_timestamp(absolute
, sizeof(absolute
), u
),
468 format_timestamp_relative(relative
, sizeof(relative
), u
));
471 fprintf(file
, " Timestamp: %s\n", timestamp
);
475 fprintf(file
, " Command Line: %s\n", cmdline
);
477 fprintf(file
, " Executable: %s%s%s\n", ansi_highlight(), exe
, ansi_normal());
479 fprintf(file
, " Control Group: %s\n", cgroup
);
481 fprintf(file
, " Unit: %s\n", unit
);
483 fprintf(file
, " User Unit: %s\n", user_unit
);
485 fprintf(file
, " Slice: %s\n", slice
);
487 fprintf(file
, " Session: %s\n", session
);
491 if (parse_uid(owner_uid
, &n
) >= 0) {
492 _cleanup_free_
char *u
= NULL
;
496 " Owner UID: %s (%s)\n",
505 fprintf(file
, " Boot ID: %s\n", boot_id
);
507 fprintf(file
, " Machine ID: %s\n", machine_id
);
509 fprintf(file
, " Hostname: %s\n", hostname
);
512 fprintf(file
, " Storage: %s%s\n", filename
,
513 access(filename
, F_OK
) < 0 ? " (inaccessible)" : "");
515 fprintf(file
, " Storage: journal\n");
517 fprintf(file
, " Storage: none\n");
520 _cleanup_free_
char *m
= NULL
;
522 m
= strreplace(message
, "\n", "\n ");
524 fprintf(file
, " Message: %s\n", strstrip(m
?: message
));
530 static int focus(sd_journal
*j
) {
533 r
= sd_journal_seek_tail(j
);
535 r
= sd_journal_previous(j
);
537 return log_error_errno(r
, "Failed to search journal: %m");
539 log_error("No match found.");
545 static void print_entry(sd_journal
*j
, unsigned n_found
) {
548 if (arg_action
== ACTION_INFO
)
549 print_info(stdout
, j
, n_found
);
551 print_field(stdout
, j
);
553 print_list(stdout
, j
, n_found
);
556 static int dump_list(sd_journal
*j
) {
557 unsigned n_found
= 0;
562 /* The coredumps are likely to compressed, and for just
563 * listing them we don't need to decompress them, so let's
564 * pick a fairly low data threshold here */
565 sd_journal_set_data_threshold(j
, 4096);
574 SD_JOURNAL_FOREACH(j
)
575 print_entry(j
, n_found
++);
577 if (!arg_field
&& n_found
<= 0) {
578 log_notice("No coredumps found.");
586 static int save_core(sd_journal
*j
, int fd
, char **path
, bool *unlink_temp
) {
588 _cleanup_free_
char *filename
= NULL
;
591 _cleanup_close_
int fdt
= -1;
594 assert((fd
>= 0) != !!path
);
595 assert(!!path
== !!unlink_temp
);
597 /* We want full data, nothing truncated. */
598 sd_journal_set_data_threshold(j
, 0);
600 /* Look for a coredump on disk first. */
601 r
= sd_journal_get_data(j
, "COREDUMP_FILENAME", (const void**) &data
, &len
);
602 if (r
< 0 && r
!= -ENOENT
)
603 return log_error_errno(r
, "Failed to retrieve COREDUMP_FILENAME: %m");
605 retrieve(data
, len
, "COREDUMP_FILENAME", &filename
);
608 if (access(filename
, R_OK
) < 0)
609 return log_error_errno(errno
, "File \"%s\" is not readable: %m", filename
);
611 if (path
&& !endswith(filename
, ".xz") && !endswith(filename
, ".lz4")) {
622 /* Create a temporary file to write the uncompressed core to. */
624 r
= var_tmp_dir(&vt
);
626 return log_error_errno(r
, "Failed to acquire temporary directory path: %m");
628 temp
= strjoin(vt
, "/coredump-XXXXXX", NULL
);
632 fdt
= mkostemp_safe(temp
);
634 return log_error_errno(fdt
, "Failed to create temporary file: %m");
635 log_debug("Created temporary file %s", temp
);
641 #if defined(HAVE_XZ) || defined(HAVE_LZ4)
642 _cleanup_close_
int fdf
;
644 fdf
= open(filename
, O_RDONLY
| O_CLOEXEC
);
646 r
= log_error_errno(errno
, "Failed to open %s: %m", filename
);
650 r
= decompress_stream(filename
, fdf
, fd
, -1);
652 log_error_errno(r
, "Failed to decompress %s: %m", filename
);
656 log_error("Cannot decompress file. Compiled without compression support.");
663 r
= sd_journal_get_data(j
, "COREDUMP", (const void**) &data
, &len
);
665 return log_error_errno(r
,
666 r
== -ENOENT
? "Core file was not saved for this entry." :
667 "Failed to retrieve COREDUMP field: %m");
673 sz
= write(fd
, data
, len
);
675 r
= log_error_errno(errno
, "Failed to write output: %m");
678 if (sz
!= (ssize_t
) len
) {
679 log_error("Short write to output.");
694 log_debug("Removed temporary file %s", temp
);
699 static int dump_core(sd_journal
* j
) {
708 print_info(arg_output
? stdout
: stderr
, j
, false);
710 if (on_tty() && !arg_output
) {
711 log_error("Refusing to dump core to tty.");
715 r
= save_core(j
, arg_output
? fileno(arg_output
) : STDOUT_FILENO
, NULL
, NULL
);
717 return log_error_errno(r
, "Coredump retrieval failed: %m");
719 r
= sd_journal_previous(j
);
721 log_warning("More than one entry matches, ignoring rest.");
726 static int run_gdb(sd_journal
*j
) {
727 _cleanup_free_
char *exe
= NULL
, *path
= NULL
;
728 bool unlink_path
= false;
741 print_info(stdout
, j
, false);
744 r
= sd_journal_get_data(j
, "COREDUMP_EXE", (const void**) &data
, &len
);
746 return log_error_errno(r
, "Failed to retrieve COREDUMP_EXE field: %m");
748 assert(len
> strlen("COREDUMP_EXE="));
749 data
+= strlen("COREDUMP_EXE=");
750 len
-= strlen("COREDUMP_EXE=");
752 exe
= strndup(data
, len
);
756 if (endswith(exe
, " (deleted)")) {
757 log_error("Binary already deleted.");
761 if (!path_is_absolute(exe
)) {
762 log_error("Binary is not an absolute path.");
766 r
= save_core(j
, -1, &path
, &unlink_path
);
768 return log_error_errno(r
, "Failed to retrieve core: %m");
772 r
= log_error_errno(errno
, "Failed to fork(): %m");
776 (void) reset_all_signal_handlers();
777 (void) reset_signal_mask();
779 execlp("gdb", "gdb", exe
, path
, NULL
);
781 log_error_errno(errno
, "Failed to invoke gdb: %m");
785 r
= wait_for_terminate(pid
, &st
);
787 log_error_errno(r
, "Failed to wait for gdb: %m");
791 r
= st
.si_code
== CLD_EXITED
? st
.si_status
: 255;
795 log_debug("Removed temporary file %s", path
);
802 int main(int argc
, char *argv
[]) {
803 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
807 _cleanup_set_free_free_ Set
*matches
= NULL
;
809 setlocale(LC_ALL
, "");
810 log_parse_environment();
813 matches
= new_matches();
819 r
= parse_argv(argc
, argv
, matches
);
823 if (arg_action
== ACTION_NONE
)
829 r
= sd_journal_open_directory(&j
, arg_directory
, 0);
831 log_error_errno(r
, "Failed to open journals in directory: %s: %m", arg_directory
);
835 r
= sd_journal_open(&j
, SD_JOURNAL_LOCAL_ONLY
);
837 log_error_errno(r
, "Failed to open journal: %m");
842 SET_FOREACH(match
, matches
, it
) {
843 r
= sd_journal_add_match(j
, match
, strlen(match
));
845 log_error_errno(r
, "Failed to add match '%s': %m",
851 if (_unlikely_(log_get_max_level() >= LOG_DEBUG
)) {
852 _cleanup_free_
char *filter
;
854 filter
= journal_make_match_string(j
);
855 log_debug("Journal filter: %s", filter
);
862 pager_open(arg_no_pager
, false);
875 assert_not_reached("Shouldn't be here");
884 return r
>= 0 ? r
: EXIT_FAILURE
;