coredumpctl.c revision 1b12a7b5896f94bdf33b3a6661ebabd761ea6adc
/*-*- 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"
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...] [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"
" -F --field=FIELD List all values a certain field takes\n"
" gdb Start gdb for the first matching coredump\n"
" list List available coredumps\n"
" dump PID Print coredump to stdout\n"
" dump PATH Print coredump to stdout\n"
return 0;
}
int r = -ENOMEM;
unsigned pid;
const char* prefix;
char _cleanup_free_ *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':
help();
return 0;
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 (field) {
log_error("cannot use --field/-F more than once");
return -EINVAL;
}
break;
case '?':
return -EINVAL;
default:
log_error("Unknown option code %c", c);
return -EINVAL;
}
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,
const char **var) {
size_t ident;
if (len < ident)
return 0;
return 0;
return 0;
if (!*var)
return log_oom();
return 0;
}
const void *d;
size_t l;
SD_JOURNAL_FOREACH_DATA(j, d, l)
if (value)
}
const char _cleanup_free_
const void *d;
size_t l;
usec_t t;
char buf[FORMAT_TIMESTAMP_MAX];
int r;
SD_JOURNAL_FOREACH_DATA(j, d, l) {
if (!exe)
if (!exe)
}
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");
6, pid,
5, uid,
5, gid,
3, sgnl,
exe);
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 (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 < 0) {
return r;
}
len -= 9;
if (ret != 1) {
return -errno;
}
r = sd_journal_previous(j);
if (r >= 0)
log_warning("More than one entry matches, ignoring rest.\n");
return 0;
}
static int run_gdb(sd_journal *j) {
char path[] = "/var/tmp/coredump-XXXXXX";
const void *data;
int r;
assert(j);
r = focus(j);
if (r < 0)
return r;
print_entry(stdout, j, false);
if (r < 0) {
return r;
}
len -= 13;
if (!exe)
return log_oom();
log_error("Binary already deleted.");
return -ENOENT;
}
if (r < 0) {
return r;
}
len -= 9;
if (fd < 0) {
log_error("Failed to create temporary file: %m");
return -errno;
}
if (sz < 0) {
r = -errno;
goto finish;
}
log_error("Short write to temporary file.");
r = -EIO;
goto finish;
}
fd = -1;
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;
}
return r;
}
sd_journal *j = NULL;
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;
}
}
switch(arg_action) {
case ACTION_LIST:
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:
if (j)
sd_journal_close(j);
pager_close();
if (output)
return r >= 0 ? r : EXIT_FAILURE;
}