coredumpctl.c revision b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2012 Zbigniew Jędrzejewski-Szmek
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <fcntl.h>
#include <getopt.h>
#include <locale.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "sd-journal.h"
#include "compress.h"
#include "fd-util.h"
#include "journal-internal.h"
#include "log.h"
#include "macro.h"
#include "pager.h"
#include "path-util.h"
#include "process-util.h"
#include "set.h"
#include "sigbus.h"
#include "signal-util.h"
#include "string-util.h"
#include "terminal-util.h"
#include "util.h"
#include "user-util.h"
static enum {
} arg_action = ACTION_LIST;
static const char *arg_directory = NULL;
static int arg_no_pager = false;
static int arg_no_legend = false;
static int arg_one = false;
static Set *new_matches(void) {
char *tmp;
int r;
if (!set) {
log_oom();
return NULL;
}
if (!tmp) {
log_oom();
return NULL;
}
if (r < 0) {
log_error_errno(r, "failed to add to set: %m");
return NULL;
}
return set;
}
_cleanup_free_ char *p = NULL;
const char* prefix;
int r;
prefix = "";
r = path_make_absolute_cwd(match, &p);
if (r < 0)
goto fail;
match = p;
prefix = "COREDUMP_EXE=";
prefix = "COREDUMP_PID=";
else
prefix = "COREDUMP_COMM=";
if (!pattern) {
r = -ENOMEM;
goto fail;
}
if (r < 0)
goto fail;
return 0;
fail:
return log_error_errno(r, "Failed to add match: %m");
}
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"
" -D --directory=DIR Use journal files from directory\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"
}
enum {
ARG_VERSION = 0x100,
};
int r, c;
{}
};
switch(c) {
case 'h':
help();
return 0;
case ARG_VERSION:
return version();
case ARG_NO_PAGER:
arg_no_pager = true;
break;
case ARG_NO_LEGEND:
arg_no_legend = true;
break;
case 'o':
if (arg_output) {
log_error("cannot set output more than once");
return -EINVAL;
}
if (!arg_output)
break;
case 'F':
if (arg_field) {
log_error("cannot use --field/-F more than once");
return -EINVAL;
}
break;
case '1':
arg_one = true;
break;
case 'D':
break;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
else {
return -EINVAL;
}
}
log_error("Option --field/-F only makes sense with list");
return -EINVAL;
}
if (r != 0)
return r;
optind++;
}
return 0;
}
const char *name,
char **var) {
size_t ident;
char *v;
if (len < ident)
return 0;
return 0;
return 0;
if (!v)
return log_oom();
*var = v;
return 0;
}
const void *d;
size_t l;
assert(j);
SD_JOURNAL_FOREACH_DATA(j, d, l)
if (value)
}
_cleanup_free_ char
const void *d;
size_t l;
usec_t t;
char buf[FORMAT_TIMESTAMP_MAX];
int r;
bool present;
assert(j);
SD_JOURNAL_FOREACH_DATA(j, d, l) {
}
log_warning("Empty coredump log entry");
return -EINVAL;
}
r = sd_journal_get_realtime_usec(j, &t);
if (r < 0)
return log_error_errno(r, "Failed to get realtime timestamp: %m");
if (!had_legend && !arg_no_legend)
FORMAT_TIMESTAMP_WIDTH, "TIME",
6, "PID",
5, "UID",
5, "GID",
3, "SIG",
1, "PRESENT",
"EXE");
return 0;
}
_cleanup_free_ char
const void *d;
size_t l;
int r;
assert(j);
SD_JOURNAL_FOREACH_DATA(j, d, l) {
}
if (need_space)
if (comm)
" PID: %s%s%s (%s)\n",
else
" PID: %s%s%s\n",
if (uid) {
uid_t n;
_cleanup_free_ char *u = NULL;
u = uid_to_name(n);
" UID: %s (%s)\n",
uid, u);
} else {
" UID: %s\n",
uid);
}
}
if (gid) {
gid_t n;
_cleanup_free_ char *g = NULL;
g = gid_to_name(n);
" GID: %s (%s)\n",
gid, g);
} else {
" GID: %s\n",
gid);
}
}
if (sgnl) {
int sig;
else
}
if (timestamp) {
usec_t u;
r = safe_atou64(timestamp, &u);
if (r >= 0) {
" Timestamp: %s (%s)\n",
} else
}
if (cmdline)
if (exe)
if (cgroup)
if (unit)
if (user_unit)
if (slice)
if (session)
if (owner_uid) {
uid_t n;
_cleanup_free_ char *u = NULL;
u = uid_to_name(n);
" Owner UID: %s (%s)\n",
owner_uid, u);
} else {
" Owner UID: %s\n",
}
}
if (boot_id)
if (machine_id)
if (hostname)
if (message) {
_cleanup_free_ char *m = NULL;
}
return 0;
}
static int focus(sd_journal *j) {
int r;
r = sd_journal_seek_tail(j);
if (r == 0)
r = sd_journal_previous(j);
if (r < 0)
return log_error_errno(r, "Failed to search journal: %m");
if (r == 0) {
log_error("No match found.");
return -ESRCH;
}
return r;
}
assert(j);
if (arg_action == ACTION_INFO)
else if (arg_field)
print_field(stdout, j);
else
}
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 {
print_entry(j, n_found++);
log_notice("No coredumps found.");
return -ESRCH;
}
}
return 0;
}
const char *data;
int r;
/* Prefer uncompressed file to journal (probably cached) to
* compressed file (probably uncached). */
if (r < 0 && r != -ENOENT)
log_warning_errno(r, "Failed to retrieve COREDUMP_FILENAME: %m");
else if (r == 0)
"File %s is not readable: %m", filename);
}
if (path) {
}
return 0;
} else {
if (fd < 0) {
if (!temp)
return log_oom();
if (fdt < 0)
}
if (r == 0) {
data += 9;
len -= 9;
if (sz < 0) {
r = log_error_errno(errno,
"Failed to write temporary file: %m");
goto error;
}
log_error("Short write to temporary file.");
r = -EIO;
goto error;
}
} else if (filename) {
_cleanup_close_ int fdf;
if (fdf < 0) {
r = log_error_errno(errno,
"Failed to open %s: %m",
filename);
goto error;
}
if (r < 0) {
goto error;
}
#else
log_error("Cannot decompress file. Compiled without compression support.");
r = -EOPNOTSUPP;
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) {
*unlink_temp = true;
}
return 0;
if (temp) {
}
return r;
}
}
static int dump_core(sd_journal* j) {
int r;
assert(j);
r = focus(j);
if (r < 0)
return r;
if (on_tty() && !arg_output) {
log_error("Refusing to dump core to tty.");
return -ENOTTY;
}
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.");
return 0;
}
static int run_gdb(sd_journal *j) {
bool unlink_path = false;
const char *data;
int r;
assert(j);
r = focus(j);
if (r < 0)
return r;
print_info(stdout, j, false);
if (r < 0)
return log_error_errno(r, "Failed to retrieve COREDUMP_EXE field: %m");
if (!exe)
return log_oom();
log_error("Binary already deleted.");
return -ENOENT;
}
if (!path_is_absolute(exe)) {
log_error("Binary is not an absolute path.");
return -ENOENT;
}
if (r < 0)
return log_error_errno(r, "Failed to retrieve core: %m");
if (pid < 0) {
goto finish;
}
if (pid == 0) {
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
_exit(1);
}
if (r < 0) {
goto finish;
}
if (unlink_path) {
}
return r;
}
const char* match;
int r = 0;
log_open();
matches = new_matches();
if (!matches) {
r = -ENOMEM;
goto end;
}
if (r < 0)
goto end;
if (arg_action == ACTION_NONE)
goto end;
if (arg_directory) {
r = sd_journal_open_directory(&j, arg_directory, 0);
if (r < 0) {
goto end;
}
} else {
r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
if (r < 0) {
log_error_errno(r, "Failed to open journal: %m");
goto end;
}
}
/* We want full data, nothing truncated. */
if (r != 0) {
log_error_errno(r, "Failed to add match '%s': %m",
match);
goto end;
}
}
_cleanup_free_ char *filter;
}
switch(arg_action) {
case ACTION_LIST:
case ACTION_INFO:
if (!arg_no_pager)
pager_open(false);
r = dump_list(j);
break;
case ACTION_DUMP:
r = dump_core(j);
break;
case ACTION_GDB:
r = run_gdb(j);
break;
default:
assert_not_reached("Shouldn't be here");
}
end:
pager_close();
if (arg_output)
return r >= 0 ? r : EXIT_FAILURE;
}