coredumpctl.c revision 8d4e028f1868c47864ec873d9f30c3ee961a8849
/*-*- 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 <locale.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <fcntl.h>
#include <unistd.h>
#include <systemd/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"
static enum {
} arg_action = ACTION_LIST;
static int arg_no_pager = false;
static int arg_no_legend = 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) {
return NULL;
}
return set;
}
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"
" --no-pager Do not pipe output into a pager\n"
" --no-legend Do not print the column headers.\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"
" 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"
return 0;
}
int r = -ENOMEM;
unsigned pid;
const char* prefix;
_cleanup_free_ char *p = NULL;
prefix = "";
p = path_make_absolute_cwd(match);
if (!p)
goto fail;
match = p;
prefix = "COREDUMP_EXE=";
}
prefix = "COREDUMP_PID=";
else
prefix = "COREDUMP_COMM=";
if (!pattern)
goto fail;
if (r < 0) {
log_error("Failed to add pattern '%s': %s",
goto fail;
}
return 0;
fail:
return r;
}
enum {
ARG_VERSION = 0x100,
};
int r, c;
{}
};
switch(c) {
case 'h':
return help();
case ARG_VERSION:
return 0;
case ARG_NO_PAGER:
arg_no_pager = true;
break;
case ARG_NO_LEGEND:
arg_no_legend = true;
break;
case 'o':
if (output) {
log_error("cannot set output more than once");
return -EINVAL;
}
if (!output) {
return -errno;
}
break;
case 'F':
if (arg_field) {
log_error("cannot use --field/-F more than once");
return -EINVAL;
}
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;
}
_cleanup_free_ char
const void *d;
size_t l;
char *fn;
assert(j);
SD_JOURNAL_FOREACH_DATA(j, d, l) {
}
log_error("Failed to retrieve necessary fields to find coredump on disk.");
return -ENOENT;
}
p = filename_escape(pid);
if (!p)
return log_oom();
t = filename_escape(tstamp);
if (!t)
return log_oom();
c = filename_escape(comm);
if (!t)
return log_oom();
b = filename_escape(boot_id);
if (!b)
return log_oom();
if (!fn)
return log_oom();
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;
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 r;
}
if (!had_legend && !arg_no_legend)
6, "PID",
5, "UID",
5, "GID",
3, "SIG",
"EXE");
return 0;
}
_cleanup_free_ char
const void *d;
size_t l;
assert(j);
SD_JOURNAL_FOREACH_DATA(j, d, l) {
}
if (need_space)
" 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 (exe)
if (comm)
if (cmdline)
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 (make_coredump_path(j, &coredump) >= 0)
if (message) {
_cleanup_free_ char *m = NULL;
}
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);
SD_JOURNAL_FOREACH(j) {
if (arg_action == ACTION_INFO)
else if (arg_field)
print_field(stdout, j);
else
}
log_notice("No coredumps found");
return -ESRCH;
}
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 r;
}
if (r == 0) {
log_error("No match found");
return -ESRCH;
}
return r;
}
static int dump_core(sd_journal* j) {
const void *data;
int r;
assert(j);
/* We want full data, nothing truncated. */
r = focus(j);
if (r < 0)
return r;
log_error("Refusing to dump core to tty.");
return -ENOTTY;
}
if (r == ENOENT) {
r = make_coredump_path(j, &fn);
if (r < 0)
return r;
if (fd < 0) {
log_error("Coredump neither in journal file nor stored externally on disk.");
else
return -errno;
}
if (r < 0) {
return r;
}
} else if (r < 0) {
return r;
} else {
len -= 9;
if (ret != 1) {
return -errno;
}
}
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) {
char temp[] = "/var/tmp/coredump-XXXXXX";
bool unlink_temp = false;
const char *path;
const void *data;
int r;
assert(j);
r = focus(j);
if (r < 0)
return r;
print_info(stdout, j, false);
if (r < 0) {
return r;
}
len -= 13;
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 == -ENOENT) {
r = make_coredump_path(j, &coredump);
if (r < 0)
return r;
log_error("Coredump neither in journal file nor stored externally on disk.");
else
log_error("Failed to access coredump file: %m");
return -errno;
}
} else if (r < 0) {
return r;
} else {
len -= 9;
if (fd < 0) {
log_error("Failed to create temporary file: %m");
return -errno;
}
unlink_temp = true;
if (sz < 0) {
log_error("Failed to write temporary file: %m");
r = -errno;
goto finish;
}
log_error("Short write to temporary file.");
r = -EIO;
goto finish;
}
}
if (pid < 0) {
log_error("Failed to fork(): %m");
r = -errno;
goto finish;
}
if (pid == 0) {
log_error("Failed to invoke gdb: %m");
_exit(1);
}
if (r < 0) {
log_error("Failed to wait for gdb: %m");
goto finish;
}
if (unlink_temp)
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;
r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
if (r < 0) {
goto end;
}
if (r != 0) {
log_error("Failed to add match '%s': %s",
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 (output)
return r >= 0 ? r : EXIT_FAILURE;
}