chiark / gitweb /
coredumpctl: include timestamp information in "coredumpctl info" output
[elogind.git] / src / journal / coredumpctl.c
index ea459469ee686dddbd15a2f35a8dc37f1e0cd495..56b7becfcd42d8058feea2c9b28f869a5fd25b7a 100644 (file)
@@ -45,12 +45,12 @@ static enum {
         ACTION_DUMP,
         ACTION_GDB,
 } arg_action = ACTION_LIST;
-
-static FILE* output = NULL;
 static const char* arg_field = NULL;
-
 static int arg_no_pager = false;
 static int arg_no_legend = false;
+static int arg_one = false;
+
+static FILE* output = NULL;
 
 static Set *new_matches(void) {
         Set *set;
@@ -85,15 +85,16 @@ static int help(void) {
         printf("%s [OPTIONS...]\n\n"
                "List or retrieve coredumps from the journal.\n\n"
                "Flags:\n"
-               "  -o --output=FILE   Write output to FILE\n"
+               "  -h --help          Show this help\n"
+               "     --version       Print version string\n"
                "     --no-pager      Do not pipe output into a pager\n"
-               "     --no-legend     Do not print the column headers.\n\n"
+               "     --no-legend     Do not print the column headers.\n"
+               "  -1                 Show information about most recent entry only\n"
+               "  -F --field=FIELD   List all values a certain field takes\n"
+               "  -o --output=FILE   Write output to FILE\n\n"
 
                "Commands:\n"
-               "  -h --help          Show this help\n"
-               "  --version          Print version string\n"
-               "  -F --field=FIELD   List all values a certain field takes\n"
-               "  list [MATCHES...]  List available coredumps\n"
+               "  list [MATCHES...]  List available coredumps (default)\n"
                "  info [MATCHES...]  Show detailed information about one or more coredumps\n"
                "  dump [MATCHES...]  Print first matching coredump to stdout\n"
                "  gdb [MATCHES...]   Start gdb for the first matching coredump\n"
@@ -165,7 +166,7 @@ static int parse_argv(int argc, char *argv[], Set *matches) {
         assert(argc >= 0);
         assert(argv);
 
-        while ((c = getopt_long(argc, argv, "ho:F:", options, NULL)) >= 0)
+        while ((c = getopt_long(argc, argv, "ho:F:1", options, NULL)) >= 0)
                 switch(c) {
 
                 case 'h':
@@ -208,6 +209,10 @@ static int parse_argv(int argc, char *argv[], Set *matches) {
                         arg_field = optarg;
                         break;
 
+                case '1':
+                        arg_one = true;
+                        break;
+
                 case '?':
                         return -EINVAL;
 
@@ -275,7 +280,7 @@ static int retrieve(const void *data,
         return 0;
 }
 
-#define filename_escape(s) xescape((s), "./")
+#define filename_escape(s) xescape((s), "./ ")
 
 static int make_coredump_path(sd_journal *j, char **ret) {
         _cleanup_free_ char
@@ -379,15 +384,15 @@ static int print_list(FILE* file, sd_journal *j, int had_legend) {
 
         if (!had_legend && !arg_no_legend)
                 fprintf(file, "%-*s %*s %*s %*s %*s %s\n",
-                        FORMAT_TIMESTAMP_MAX-1, "TIME",
+                        FORMAT_TIMESTAMP_WIDTH, "TIME",
                         6, "PID",
                         5, "UID",
                         5, "GID",
                         3, "SIG",
                            "EXE");
 
-        fprintf(file, "%*s %*s %*s %*s %*s %s\n",
-                FORMAT_TIMESTAMP_MAX-1, buf,
+        fprintf(file, "%-*s %*s %*s %*s %*s %s\n",
+                FORMAT_TIMESTAMP_WIDTH, buf,
                 6, strna(pid),
                 5, strna(uid),
                 5, strna(gid),
@@ -403,9 +408,11 @@ static int print_info(FILE *file, sd_journal *j, bool need_space) {
                 *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL,
                 *unit = NULL, *user_unit = NULL, *session = NULL,
                 *boot_id = NULL, *machine_id = NULL, *hostname = NULL,
-                *coredump = NULL, *slice = NULL, *cgroup = NULL, *owner_uid = NULL;
+                *coredump = NULL, *slice = NULL, *cgroup = NULL,
+                *owner_uid = NULL, *message = NULL, *timestamp = NULL;
         const void *d;
         size_t l;
+        int r;
 
         assert(file);
         assert(j);
@@ -424,9 +431,11 @@ static int print_info(FILE *file, sd_journal *j, bool need_space) {
                 retrieve(d, l, "COREDUMP_OWNER_UID", &owner_uid);
                 retrieve(d, l, "COREDUMP_SLICE", &slice);
                 retrieve(d, l, "COREDUMP_CGROUP", &cgroup);
+                retrieve(d, l, "COREDUMP_TIMESTAMP", &timestamp);
                 retrieve(d, l, "_BOOT_ID", &boot_id);
                 retrieve(d, l, "_MACHINE_ID", &machine_id);
                 retrieve(d, l, "_HOSTNAME", &hostname);
+                retrieve(d, l, "MESSAGE", &message);
         }
 
         if (need_space)
@@ -479,6 +488,22 @@ static int print_info(FILE *file, sd_journal *j, bool need_space) {
                         fprintf(file, "        Signal: %s\n", sgnl);
         }
 
+        if (timestamp) {
+                usec_t u;
+
+                r = safe_atou64(timestamp, &u);
+                if (r >= 0) {
+                        char absolute[FORMAT_TIMESTAMP_MAX], relative[FORMAT_TIMESPAN_MAX];
+
+                        fprintf(file,
+                                "     Timestamp: %s (%s)\n",
+                                format_timestamp(absolute, sizeof(absolute), u),
+                                format_timestamp_relative(relative, sizeof(relative), u));
+
+                } else
+                        fprintf(file, "     Timestamp: %s\n", timestamp);
+        }
+
         if (exe)
                 fprintf(file, "    Executable: %s%s%s\n", ansi_highlight(), exe, ansi_highlight_off());
         if (comm)
@@ -522,31 +547,12 @@ static int print_info(FILE *file, sd_journal *j, bool need_space) {
                 if (access(coredump, F_OK) >= 0)
                         fprintf(file, "      Coredump: %s\n", coredump);
 
-        return 0;
-}
-
-static int dump_list(sd_journal *j) {
-        int found = 0;
-
-        assert(j);
-
-        /* The coredumps are likely to compressed, and for just
-         * listing them we don't need to decompress them, so let's
-         * pick a fairly low data threshold here */
-        sd_journal_set_data_threshold(j, 4096);
+        if (message) {
+                _cleanup_free_ char *m = NULL;
 
-        SD_JOURNAL_FOREACH(j) {
-                if (arg_action == ACTION_INFO)
-                        print_info(stdout, j, found++);
-                else if (arg_field)
-                        print_field(stdout, j);
-                else
-                        print_list(stdout, j, found++);
-        }
+                m = strreplace(message, "\n", "\n                ");
 
-        if (!arg_field && !found) {
-                log_notice("No coredumps found");
-                return -ESRCH;
+                fprintf(file, "       Message: %s\n", strstrip(m ?: message));
         }
 
         return 0;
@@ -563,12 +569,53 @@ static int focus(sd_journal *j) {
                 return r;
         }
         if (r == 0) {
-                log_error("No match found");
+                log_error("No match found.");
                 return -ESRCH;
         }
         return r;
 }
 
+static void print_entry(sd_journal *j, unsigned n_found) {
+        assert(j);
+
+        if (arg_action == ACTION_INFO)
+                print_info(stdout, j, n_found);
+        else if (arg_field)
+                print_field(stdout, j);
+        else
+                print_list(stdout, j, n_found);
+}
+
+static int dump_list(sd_journal *j) {
+        unsigned n_found = 0;
+        int r;
+
+        assert(j);
+
+        /* The coredumps are likely to compressed, and for just
+         * listing them we don't need to decompress them, so let's
+         * pick a fairly low data threshold here */
+        sd_journal_set_data_threshold(j, 4096);
+
+        if (arg_one) {
+                r = focus(j);
+                if (r < 0)
+                        return r;
+
+                print_entry(j, 0);
+        } else {
+                SD_JOURNAL_FOREACH(j)
+                        print_entry(j, n_found++);
+
+                if (!arg_field && n_found <= 0) {
+                        log_notice("No coredumps found.");
+                        return -ESRCH;
+                }
+        }
+
+        return 0;
+}
+
 static int dump_core(sd_journal* j) {
         const void *data;
         size_t len, ret;
@@ -696,7 +743,7 @@ static int run_gdb(sd_journal *j) {
                         if (errno == ENOENT)
                                 log_error("Coredump neither in journal file nor stored externally on disk.");
                         else
-                                log_error("Failed to access coredump fiile: %s", strerror(-r));
+                                log_error("Failed to access coredump file: %m");
 
                         return -errno;
                 }