chiark / gitweb /
journalctl: static variables immediately configured via command line arguments should...
[elogind.git] / src / journal / coredumpctl.c
index 3560534bf6069618a084e2342ffe6eec68198be4..756e793a4fed14bea20771f5e38ed7a8248fae44 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include <locale.h>
 #include <stdio.h>
 #include <string.h>
 #include <getopt.h>
 #include <fcntl.h>
 #include <unistd.h>
 
-#include <systemd/sd-journal.h>
-
+#include "sd-journal.h"
 #include "build.h"
 #include "set.h"
 #include "util.h"
 #include "log.h"
 #include "path-util.h"
 #include "pager.h"
+#include "macro.h"
+#include "journal-internal.h"
+#include "copy.h"
+#include "compress.h"
+#include "sigbus.h"
 
 static enum {
         ACTION_NONE,
+        ACTION_INFO,
         ACTION_LIST,
         ACTION_DUMP,
         ACTION_GDB,
 } arg_action = ACTION_LIST;
-
-static Set *matches = NULL;
-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* arg_output = NULL;
 
 static Set *new_matches(void) {
         Set *set;
         char *tmp;
         int r;
 
-        set = set_new(trivial_hash_func, trivial_compare_func);
+        set = set_new(NULL);
         if (!set) {
                 log_oom();
                 return NULL;
@@ -64,10 +70,9 @@ static Set *new_matches(void) {
                 return NULL;
         }
 
-        r = set_put(set, tmp);
+        r = set_consume(set, tmp);
         if (r < 0) {
-                log_error("failed to add to set: %s", strerror(-r));
-                free(tmp);
+                log_error_errno(r, "failed to add to set: %m");
                 set_free(set);
                 return NULL;
         }
@@ -75,30 +80,12 @@ static Set *new_matches(void) {
         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;
+        _cleanup_free_ char *p = NULL;
 
         if (strchr(match, '='))
                 prefix = "";
@@ -119,25 +106,43 @@ static int add_match(Set *set, const char *match) {
         if (!pattern)
                 goto fail;
 
-        r = set_put(set, pattern);
+        log_debug("Adding pattern: %s", pattern);
+        r = set_consume(set, pattern);
         if (r < 0) {
-                log_error("failed to add pattern '%s': %s",
-                          pattern, strerror(-r));
+                log_error_errno(r, "Failed to add pattern: %m");
                 goto fail;
         }
-        log_debug("Added pattern: %s", pattern);
 
         return 0;
 fail:
-        free(pattern);
-        log_error("failed to add match: %s", strerror(-r));
-        return r;
+        return log_error_errno(r, "Failed to add match: %m");
 }
 
-static int parse_argv(int argc, char *argv[]) {
+static void help(void) {
+        printf("%s [OPTIONS...]\n\n"
+               "List or retrieve coredumps from the journal.\n\n"
+               "Flags:\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"
+               "  -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"
+               "  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"
+               , program_invocation_short_name);
+}
+
+static int parse_argv(int argc, char *argv[], Set *matches) {
         enum {
                 ARG_VERSION = 0x100,
                 ARG_NO_PAGER,
+                ARG_NO_LEGEND,
         };
 
         int r, c;
@@ -146,67 +151,89 @@ static int parse_argv(int argc, char *argv[]) {
                 { "help",         no_argument,       NULL, 'h'           },
                 { "version" ,     no_argument,       NULL, ARG_VERSION   },
                 { "no-pager",     no_argument,       NULL, ARG_NO_PAGER  },
+                { "no-legend",    no_argument,       NULL, ARG_NO_LEGEND },
                 { "output",       required_argument, NULL, 'o'           },
-                { NULL,           0,                 NULL, 0             }
+                { "field",        required_argument, NULL, 'F'           },
+                {}
         };
 
         assert(argc >= 0);
         assert(argv);
 
-        while ((c = getopt_long(argc, argv, "ho:", options, NULL)) >= 0)
+        while ((c = getopt_long(argc, argv, "ho:F:1", options, NULL)) >= 0)
                 switch(c) {
+
                 case 'h':
-                        help();
                         arg_action = ACTION_NONE;
+                        help();
                         return 0;
 
                 case ARG_VERSION:
+                        arg_action = ACTION_NONE;
                         puts(PACKAGE_STRING);
-                        puts(DISTRIBUTION);
                         puts(SYSTEMD_FEATURES);
-                        arg_action = ACTION_NONE;
                         return 0;
 
                 case ARG_NO_PAGER:
                         arg_no_pager = true;
                         break;
 
+                case ARG_NO_LEGEND:
+                        arg_no_legend = true;
+                        break;
+
                 case 'o':
-                        if (output) {
+                        if (arg_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;
+                        arg_output = fopen(optarg, "we");
+                        if (!arg_output)
+                                return log_error_errno(errno, "writing to '%s': %m", optarg);
+
+                        break;
+
+                case 'F':
+                        if (arg_field) {
+                                log_error("cannot use --field/-F more than once");
+                                return -EINVAL;
                         }
+                        arg_field = optarg;
+                        break;
 
+                case '1':
+                        arg_one = true;
                         break;
 
                 case '?':
                         return -EINVAL;
 
                 default:
-                        log_error("Unknown option code %c", c);
-                        return -EINVAL;
+                        assert_not_reached("Unhandled option");
                 }
 
         if (optind < argc) {
                 const char *cmd = argv[optind++];
-                if(streq(cmd, "list"))
+                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 if (streq(cmd, "info"))
+                        arg_action = ACTION_INFO;
                 else {
                         log_error("Unknown action '%s'", cmd);
                         return -EINVAL;
                 }
         }
 
+        if (arg_field && arg_action != ACTION_LIST) {
+                log_error("Option --field/-F only makes sense with list");
+                return -EINVAL;
+        }
+
         while (optind < argc) {
                 r = add_match(matches, argv[optind]);
                 if (r != 0)
@@ -220,95 +247,263 @@ static int parse_argv(int argc, char *argv[]) {
 static int retrieve(const void *data,
                     size_t len,
                     const char *name,
-                    const char **var) {
+                    char **var) {
 
-        size_t field;
+        size_t ident;
+        char *v;
 
-        field = strlen(name) + 1; /* name + "=" */
+        ident = strlen(name) + 1; /* name + "=" */
 
-        if (len < field)
+        if (len < ident)
                 return 0;
 
-        if (memcmp(data, name, field - 1) != 0)
+        if (memcmp(data, name, ident - 1) != 0)
                 return 0;
 
-        if (((const char*) data)[field - 1] != '=')
+        if (((const char*) data)[ident - 1] != '=')
                 return 0;
 
-        *var = strndup((const char*)data + field, len - field);
-        if (!var)
+        v = strndup((const char*)data + ident, len - ident);
+        if (!v)
                 return log_oom();
 
+        free(*var);
+        *var = v;
+
         return 0;
 }
 
-static int print_entry(FILE* file, sd_journal *j, int had_header) {
-        const char _cleanup_free_
+static void print_field(FILE* file, sd_journal *j) {
+        _cleanup_free_ char *value = NULL;
+        const void *d;
+        size_t l;
+
+        assert(file);
+        assert(j);
+
+        assert(arg_field);
+
+        SD_JOURNAL_FOREACH_DATA(j, d, l)
+                retrieve(d, l, arg_field, &value);
+
+        if (value)
+                fprintf(file, "%s\n", value);
+}
+
+static int print_list(FILE* file, sd_journal *j, int had_legend) {
+        _cleanup_free_ char
                 *pid = NULL, *uid = NULL, *gid = NULL,
-                *sgnl = NULL, *exe = NULL;
+                *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL,
+                *filename = NULL;
         const void *d;
         size_t l;
         usec_t t;
         char buf[FORMAT_TIMESTAMP_MAX];
         int r;
+        bool present;
+
+        assert(file);
+        assert(j);
 
         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);
+                retrieve(d, l, "COREDUMP_COMM", &comm);
+                retrieve(d, l, "COREDUMP_CMDLINE", &cmdline);
+                retrieve(d, l, "COREDUMP_FILENAME", &filename);
         }
 
-        if (!pid && !uid && !gid && !sgnl && !exe) {
+        if (!pid && !uid && !gid && !sgnl && !exe && !comm && !cmdline && !filename) {
                 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;
-        }
+        if (r < 0)
+                return log_error_errno(r, "Failed to get realtime timestamp: %m");
 
         format_timestamp(buf, sizeof(buf), t);
+        present = filename && access(filename, F_OK) == 0;
 
-        if (!had_header)
-                fprintf(file, "%-*s %*s %*s %*s %*s %s\n",
-                        FORMAT_TIMESTAMP_MAX-1, "TIME",
+        if (!had_legend && !arg_no_legend)
+                fprintf(file, "%-*s %*s %*s %*s %*s %*s %s\n",
+                        FORMAT_TIMESTAMP_WIDTH, "TIME",
                         6, "PID",
                         5, "UID",
                         5, "GID",
                         3, "SIG",
+                        1, "PRESENT",
                            "EXE");
 
-        fprintf(file, "%*s %*s %*s %*s %*s %s\n",
-                FORMAT_TIMESTAMP_MAX-1, buf,
-                6, pid,
-                5, uid,
-                5, gid,
-                3, sgnl,
-                exe);
+        fprintf(file, "%-*s %*s %*s %*s %*s %*s %s\n",
+                FORMAT_TIMESTAMP_WIDTH, buf,
+                6, strna(pid),
+                5, strna(uid),
+                5, strna(gid),
+                3, strna(sgnl),
+                1, present ? "*" : "",
+                strna(exe ?: (comm ?: cmdline)));
 
         return 0;
 }
 
-static int dump_list(sd_journal *j) {
-        int found = 0;
+static int print_info(FILE *file, sd_journal *j, bool need_space) {
+        _cleanup_free_ char
+                *pid = NULL, *uid = NULL, *gid = NULL,
+                *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL,
+                *unit = NULL, *user_unit = NULL, *session = NULL,
+                *boot_id = NULL, *machine_id = NULL, *hostname = NULL,
+                *slice = NULL, *cgroup = NULL, *owner_uid = NULL,
+                *message = NULL, *timestamp = NULL, *filename = NULL;
+        const void *d;
+        size_t l;
+        int r;
 
+        assert(file);
         assert(j);
 
-        SD_JOURNAL_FOREACH(j)
-                print_entry(stdout, j, found++);
+        SD_JOURNAL_FOREACH_DATA(j, d, l) {
+                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);
+                retrieve(d, l, "COREDUMP_COMM", &comm);
+                retrieve(d, l, "COREDUMP_CMDLINE", &cmdline);
+                retrieve(d, l, "COREDUMP_UNIT", &unit);
+                retrieve(d, l, "COREDUMP_USER_UNIT", &user_unit);
+                retrieve(d, l, "COREDUMP_SESSION", &session);
+                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, "COREDUMP_FILENAME", &filename);
+                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 (!found) {
-                log_notice("No coredumps found");
-                return -ESRCH;
+        if (need_space)
+                fputs("\n", file);
+
+        if (comm)
+                fprintf(file,
+                        "           PID: %s%s%s (%s)\n",
+                        ansi_highlight(), strna(pid), ansi_highlight_off(), comm);
+        else
+                fprintf(file,
+                        "           PID: %s%s%s\n",
+                        ansi_highlight(), strna(pid), ansi_highlight_off());
+
+        if (uid) {
+                uid_t n;
+
+                if (parse_uid(uid, &n) >= 0) {
+                        _cleanup_free_ char *u = NULL;
+
+                        u = uid_to_name(n);
+                        fprintf(file,
+                                "           UID: %s (%s)\n",
+                                uid, u);
+                } else {
+                        fprintf(file,
+                                "           UID: %s\n",
+                                uid);
+                }
+        }
+
+        if (gid) {
+                gid_t n;
+
+                if (parse_gid(gid, &n) >= 0) {
+                        _cleanup_free_ char *g = NULL;
+
+                        g = gid_to_name(n);
+                        fprintf(file,
+                                "           GID: %s (%s)\n",
+                                gid, g);
+                } else {
+                        fprintf(file,
+                                "           GID: %s\n",
+                                gid);
+                }
+        }
+
+        if (sgnl) {
+                int sig;
+
+                if (safe_atoi(sgnl, &sig) >= 0)
+                        fprintf(file, "        Signal: %s (%s)\n", sgnl, signal_to_string(sig));
+                else
+                        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 (cmdline)
+                fprintf(file, "  Command Line: %s\n", cmdline);
+        if (exe)
+                fprintf(file, "    Executable: %s%s%s\n", ansi_highlight(), exe, ansi_highlight_off());
+        if (cgroup)
+                fprintf(file, " Control Group: %s\n", cgroup);
+        if (unit)
+                fprintf(file, "          Unit: %s\n", unit);
+        if (user_unit)
+                fprintf(file, "     User Unit: %s\n", unit);
+        if (slice)
+                fprintf(file, "         Slice: %s\n", slice);
+        if (session)
+                fprintf(file, "       Session: %s\n", session);
+        if (owner_uid) {
+                uid_t n;
+
+                if (parse_uid(owner_uid, &n) >= 0) {
+                        _cleanup_free_ char *u = NULL;
+
+                        u = uid_to_name(n);
+                        fprintf(file,
+                                "     Owner UID: %s (%s)\n",
+                                owner_uid, u);
+                } else {
+                        fprintf(file,
+                                "     Owner UID: %s\n",
+                                owner_uid);
+                }
+        }
+        if (boot_id)
+                fprintf(file, "       Boot ID: %s\n", boot_id);
+        if (machine_id)
+                fprintf(file, "    Machine ID: %s\n", machine_id);
+        if (hostname)
+                fprintf(file, "      Hostname: %s\n", hostname);
+
+        if (filename && access(filename, F_OK) == 0)
+                fprintf(file, "      Coredump: %s\n", filename);
+
+        if (message) {
+                _cleanup_free_ char *m = NULL;
+
+                m = strreplace(message, "\n", "\n                ");
+
+                fprintf(file, "       Message: %s\n", strstrip(m ?: message));
         }
 
         return 0;
@@ -320,68 +515,203 @@ static int focus(sd_journal *j) {
         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)
+                return log_error_errno(r, "Failed to search journal: %m");
         if (r == 0) {
-                log_error("No match found");
+                log_error("No match found.");
                 return -ESRCH;
         }
         return r;
 }
 
-static int dump_core(sd_journal* j) {
-        const void *data;
-        size_t len, ret;
+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);
 
-        r = focus(j);
-        if (r < 0)
-                return r;
+        /* 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);
 
-        print_entry(output ? stdout : stderr, j, false);
+        if (arg_one) {
+                r = focus(j);
+                if (r < 0)
+                        return r;
 
-        if (on_tty() && !output) {
-                log_error("Refusing to dump core to tty");
-                return -ENOTTY;
+                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;
+                }
         }
 
-        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 0;
+}
+
+static int save_core(sd_journal *j, int fd, char **path, bool *unlink_temp) {
+        const char *data;
+        _cleanup_free_ char *filename = NULL;
+        size_t len;
+        int r;
+
+        assert((fd >= 0) != !!path);
+        assert(!!path == !!unlink_temp);
+
+        /* Prefer uncompressed file to journal (probably cached) to
+         * compressed file (probably uncached). */
+        r = sd_journal_get_data(j, "COREDUMP_FILENAME", (const void**) &data, &len);
+        if (r < 0 && r != -ENOENT)
+                log_warning_errno(r, "Failed to retrieve COREDUMP_FILENAME: %m");
+        else if (r == 0)
+                retrieve(data, len, "COREDUMP_FILENAME", &filename);
+
+        if (filename && access(filename, R_OK) < 0) {
+                log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING,
+                         "File %s is not readable: %m", filename);
+                free(filename);
+                filename = NULL;
+        }
+
+        if (filename && !endswith(filename, ".xz") && !endswith(filename, ".lz4")) {
+                if (path) {
+                        *path = filename;
+                        filename = NULL;
+                }
+
+                return 0;
+        } else {
+                _cleanup_close_ int fdt = -1;
+                char *temp = NULL;
+
+                if (fd < 0) {
+                        temp = strdup("/var/tmp/coredump-XXXXXX");
+                        if (!temp)
+                                return log_oom();
+
+                        fdt = mkostemp_safe(temp, O_WRONLY|O_CLOEXEC);
+                        if (fdt < 0)
+                                return log_error_errno(errno, "Failed to create temporary file: %m");
+                        log_debug("Created temporary file %s", temp);
+
+                        fd = fdt;
+                }
+
+                r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len);
+                if (r == 0) {
+                        ssize_t sz;
+
+                        assert(len >= 9);
+                        data += 9;
+                        len -= 9;
+
+                        sz = write(fdt, data, len);
+                        if (sz < 0) {
+                                log_error_errno(errno, "Failed to write temporary file: %m");
+                                r = -errno;
+                                goto error;
+                        }
+                        if (sz != (ssize_t) len) {
+                                log_error("Short write to temporary file.");
+                                r = -EIO;
+                                goto error;
+                        }
+                } else if (filename) {
+#if defined(HAVE_XZ) || defined(HAVE_LZ4)
+                        _cleanup_close_ int fdf;
+
+                        fdf = open(filename, O_RDONLY | O_CLOEXEC);
+                        if (fdf < 0) {
+                                log_error_errno(errno, "Failed to open %s: %m", filename);
+                                r = -errno;
+                                goto error;
+                        }
+
+                        r = decompress_stream(filename, fdf, fd, -1);
+                        if (r < 0) {
+                                log_error_errno(r, "Failed to decompress %s: %m", filename);
+                                goto error;
+                        }
+#else
+                        log_error("Cannot decompress file. Compiled without compression support.");
+                        r = -ENOTSUP;
+                        goto error;
+#endif
+                } else {
+                        if (r == -ENOENT)
+                                log_error("Cannot retrieve coredump from journal nor disk.");
+                        else
+                                log_error_errno(r, "Failed to retrieve COREDUMP field: %m");
+                        goto error;
+                }
+
+                if (temp) {
+                        *path = temp;
+                        *unlink_temp = true;
+                }
+
+                return 0;
+
+error:
+                if (temp) {
+                        unlink(temp);
+                        log_debug("Removed temporary file %s", temp);
+                }
                 return r;
         }
+}
+
+static int dump_core(sd_journal* j) {
+        int r;
+
+        assert(j);
+
+        r = focus(j);
+        if (r < 0)
+                return r;
 
-        assert(len >= 9);
-        data = (const uint8_t*) data + 9;
-        len -= 9;
+        print_info(arg_output ? stdout : stderr, j, false);
 
-        ret = fwrite(data, len, 1, output ? output : stdout);
-        if (ret != 1) {
-                log_error("dumping coredump: %m (%zu)", ret);
-                return -errno;
+        if (on_tty() && !arg_output) {
+                log_error("Refusing to dump core to tty.");
+                return -ENOTTY;
         }
 
+        r = save_core(j, arg_output ? fileno(arg_output) : STDOUT_FILENO, NULL, NULL);
+        if (r < 0)
+                return log_error_errno(r, "Coredump retrieval failed: %m");
+
         r = sd_journal_previous(j);
         if (r >= 0)
-                log_warning("More than one entry matches, ignoring rest.\n");
+                log_warning("More than one entry matches, ignoring rest.");
 
         return 0;
 }
 
 static int run_gdb(sd_journal *j) {
-        char path[] = "/var/tmp/coredump-XXXXXX";
-        const void *data;
+        _cleanup_free_ char *exe = NULL, *path = NULL;
+        bool unlink_path = false;
+        const char *data;
+        siginfo_t st;
         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);
 
@@ -389,17 +719,16 @@ static int run_gdb(sd_journal *j) {
         if (r < 0)
                 return r;
 
-        print_entry(stdout, j, false);
+        print_info(stdout, j, false);
+        fputs("\n", stdout);
 
         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;
-        }
+        if (r < 0)
+                return log_error_errno(r, "Failed to retrieve COREDUMP_EXE field: %m");
 
-        assert(len >= 13);
-        data = (const uint8_t*) data + 13;
-        len -= 13;
+        assert(len > strlen("COREDUMP_EXE="));
+        data += strlen("COREDUMP_EXE=");
+        len -= strlen("COREDUMP_EXE=");
 
         exe = strndup(data, len);
         if (!exe)
@@ -410,101 +739,102 @@ static int run_gdb(sd_journal *j) {
                 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;
+        if (!path_is_absolute(exe)) {
+                log_error("Binary is not an absolute path.");
+                return -ENOENT;
         }
 
-        close_nointr_nofail(fd);
-        fd = -1;
+        r = save_core(j, -1, &path, &unlink_path);
+        if (r < 0)
+                return log_error_errno(r, "Failed to retrieve core: %m");
 
         pid = fork();
         if (pid < 0) {
-                log_error("Failed to fork(): %m");
+                log_error_errno(errno, "Failed to fork(): %m");
                 r = -errno;
                 goto finish;
         }
         if (pid == 0) {
                 execlp("gdb", "gdb", exe, path, NULL);
-                log_error("Failed to invoke gdb: %m");
+
+                log_error_errno(errno, "Failed to invoke gdb: %m");
                 _exit(1);
         }
 
         r = wait_for_terminate(pid, &st);
         if (r < 0) {
-                log_error("Failed to wait for gdb: %m");
+                log_error_errno(errno, "Failed to wait for gdb: %m");
                 goto finish;
         }
 
         r = st.si_code == CLD_EXITED ? st.si_status : 255;
 
 finish:
-        unlink(path);
+        if (unlink_path) {
+                log_debug("Removed temporary file %s", path);
+                unlink(path);
+        }
+
         return r;
 }
 
 int main(int argc, char *argv[]) {
-        sd_journal *j = NULL;
+        _cleanup_journal_close_ sd_journal*j = NULL;
         const char* match;
         Iterator it;
         int r = 0;
+        _cleanup_set_free_free_ Set *matches = NULL;
 
+        setlocale(LC_ALL, "");
         log_parse_environment();
         log_open();
 
         matches = new_matches();
-        if (!matches)
+        if (!matches) {
+                r = -ENOMEM;
                 goto end;
+        }
 
-        if (parse_argv(argc, argv))
+        r = parse_argv(argc, argv, matches);
+        if (r < 0)
                 goto end;
 
         if (arg_action == ACTION_NONE)
                 goto end;
 
+        sigbus_install();
+
         r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
         if (r < 0) {
-                log_error("Failed to open journal: %s", strerror(-r));
+                log_error_errno(r, "Failed to open journal: %m");
                 goto end;
         }
 
+        /* We want full data, nothing truncated. */
+        sd_journal_set_data_threshold(j, 0);
+
         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));
+                        log_error_errno(r, "Failed to add match '%s': %m",
+                                        match);
                         goto end;
                 }
         }
 
+        if (_unlikely_(log_get_max_level() >= LOG_PRI(LOG_DEBUG))) {
+                _cleanup_free_ char *filter;
+
+                filter = journal_make_match_string(j);
+                log_debug("Journal filter: %s", filter);
+        }
+
         switch(arg_action) {
 
         case ACTION_LIST:
+        case ACTION_INFO:
                 if (!arg_no_pager)
-                        pager_open();
+                        pager_open(false);
 
                 r = dump_list(j);
                 break;
@@ -522,15 +852,10 @@ int main(int argc, char *argv[]) {
         }
 
 end:
-        if (j)
-                sd_journal_close(j);
-
-        set_free_free(matches);
-
         pager_close();
 
-        if (output)
-                fclose(output);
+        if (arg_output)
+                fclose(arg_output);
 
         return r >= 0 ? r : EXIT_FAILURE;
 }