Skip to content

Commit

Permalink
Add support for callback functions.
Browse files Browse the repository at this point in the history
Allow the user to specify a callback function that will be invoked
after all threads are paused but before the coredump is generated.

Update the version to 1.3.1.
  • Loading branch information
madscientist committed Dec 16, 2017
1 parent 48bb1a8 commit 0224d1f
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 15 deletions.
20 changes: 10 additions & 10 deletions configure
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /bin/sh
# Guess values for system-dependent variables and create Makefiles.
# Generated by GNU Autoconf 2.69 for coredumper 1.3.
# Generated by GNU Autoconf 2.69 for coredumper 1.3.1.
#
# Report bugs to <[email protected]>.
#
Expand Down Expand Up @@ -590,8 +590,8 @@ MAKEFLAGS=
# Identity of this package.
PACKAGE_NAME='coredumper'
PACKAGE_TARNAME='coredumper'
PACKAGE_VERSION='1.3'
PACKAGE_STRING='coredumper 1.3'
PACKAGE_VERSION='1.3.1'
PACKAGE_STRING='coredumper 1.3.1'
PACKAGE_BUGREPORT='[email protected]'
PACKAGE_URL=''

Expand Down Expand Up @@ -1337,7 +1337,7 @@ if test "$ac_init_help" = "long"; then
# Omit some internal or obsolete options to make the list less imposing.
# This message is too long to be a string in the A/UX 3.1 sh.
cat <<_ACEOF
\`configure' configures coredumper 1.3 to adapt to many kinds of systems.
\`configure' configures coredumper 1.3.1 to adapt to many kinds of systems.

Usage: $0 [OPTION]... [VAR=VALUE]...

Expand Down Expand Up @@ -1408,7 +1408,7 @@ fi

if test -n "$ac_init_help"; then
case $ac_init_help in
short | recursive ) echo "Configuration of coredumper 1.3:";;
short | recursive ) echo "Configuration of coredumper 1.3.1:";;
esac
cat <<\_ACEOF

Expand Down Expand Up @@ -1521,7 +1521,7 @@ fi
test -n "$ac_init_help" && exit $ac_status
if $ac_init_version; then
cat <<\_ACEOF
coredumper configure 1.3
coredumper configure 1.3.1
generated by GNU Autoconf 2.69

Copyright (C) 2012 Free Software Foundation, Inc.
Expand Down Expand Up @@ -2011,7 +2011,7 @@ cat >config.log <<_ACEOF
This file contains any messages produced by compilers while
running configure, to aid debugging if configure makes a mistake.

It was created by coredumper $as_me 1.3, which was
It was created by coredumper $as_me 1.3.1, which was
generated by GNU Autoconf 2.69. Invocation command line was

$ $0 $@
Expand Down Expand Up @@ -2877,7 +2877,7 @@ fi

# Define the identity of the package.
PACKAGE='coredumper'
VERSION='1.3'
VERSION='1.3.1'


cat >>confdefs.h <<_ACEOF
Expand Down Expand Up @@ -16664,7 +16664,7 @@ cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
# report actual input values of CONFIG_FILES etc. instead of their
# values after options handling.
ac_log="
This file was extended by coredumper $as_me 1.3, which was
This file was extended by coredumper $as_me 1.3.1, which was
generated by GNU Autoconf 2.69. Invocation command line was

CONFIG_FILES = $CONFIG_FILES
Expand Down Expand Up @@ -16730,7 +16730,7 @@ _ACEOF
cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`"
ac_cs_version="\\
coredumper config.status 1.3
coredumper config.status 1.3.1
configured by $0, generated by GNU Autoconf 2.69,
with options \\"\$ac_cs_config\\"

Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# make sure we're interpreted by some minimal autoconf
AC_PREREQ(2.57)

AC_INIT(coredumper, 1.3, [email protected])
AC_INIT(coredumper, 1.3.1, [email protected])
# The argument here is just something that should be in the current directory
# (for sanity checking)
AC_CONFIG_SRCDIR(README)
Expand Down
16 changes: 14 additions & 2 deletions man/CoreDumpParameters.man
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'\" t
.TH COREDUMPERPARAMETERS 3 "Mar 11, 2008"
.SH NAME
ClearCoreDumpParameters, SetCoreDumpLimited, SetCoreDumpCompressed, SetCoreDumpLimitedByPriority, SetCoreDumpNotes \- functions to initialize and set fields in a CoreDumpParameters structure.
ClearCoreDumpParameters, SetCoreDumpLimited, SetCoreDumpCompressed, SetCoreDumpLimitedByPriority, SetCoreDumpNotes, SetCoreDumpCallback \- functions to initialize and set fields in a CoreDumpParameters structure.
.SH SYNOPSIS
.ad l
.PP
Expand All @@ -18,12 +18,14 @@ ClearCoreDumpParameters, SetCoreDumpLimited, SetCoreDumpCompressed, SetCoreDumpL
selected_compressor );
.HP 21
.BI int\~Set\%Core\%Dump\%Notes(\:struct\~Core\%Dump\%Parameters\ \:* params ,\ \:struct\~Coredumper\%Note\~\:* notes ,\ \:int\~ \:note_count );
.HP 24
.BI int\~Set\%Core\%Dump\%Callback(\:struct\~Core\%Dump\%Parameters\ \:* params ,\ \:int\~(* callback_fn )(void*),\ \:void*\~ \:callback_arg );
.ad b
.SH DESCRIPTION
These functions must be used to set the attributes in a CoreDumpParameters
structure. Combinations of these functions may be used together. For instance
to produce a compressed core dump with additional notes you should call
\fBClearCoreDumpParameters\fP(), \fBSetCoreDumpCompressed\fP() and
\fBClearCoreDumpParameters\fP(), \fBSetCoreDumpCompressed\fP(), and
\fBSetCoreDumpNotes\fP().
.PP
The \fBClearCoreDumpParameters\fP() function clears the given
Expand Down Expand Up @@ -73,6 +75,16 @@ type is a user chosen value. The description_size specifies the amount of bytes
to write from the description pointer in memory to the core dump note. The
description will be padded to be 4 byte aligned.
.PP
The \fBSetCoreDumpCallback\fP() function sets the attributes in
.IR params
to add a callback function that will be invoked after all threads in the
process have been suspended but before any actual core is dumped. The
argument provided will be passed to the callback function and is otherwise
unused by the coredumper library.
.PP
If the callback function returns 0, then the core will continue to be dumped.
If the callback function returns non-0, then the core dump will be failed.
.PP
.SH RETURN VALUE
.PP
On success 0 will be returned. On error \-1 will be returned and
Expand Down
7 changes: 7 additions & 0 deletions src/coredumper.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,10 @@ int SetCoreDumpNotes(struct CoreDumpParameters *params,
SetCoreDumpParameter(params, note_count, note_count);
return 0;
}

int SetCoreDumpCallback(struct CoreDumpParameters *params,
int (*fn)(void*), void *arg) {
SetCoreDumpParameter(params, callback_fn, fn);
SetCoreDumpParameter(params, callback_arg, arg);
return 0;
}
29 changes: 29 additions & 0 deletions src/coredumper_unittest.c
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,16 @@ static int MyWriteCompressedCoreDump(
return MyWriteCoreDumpWith(&params, file_name);
}

static int MyCallback(void *arg)
{
int *count = arg;
if (*count < 0) {
return 1;
}
++(*count);
return 0;
}

/* Do not declare this function static, so that the compiler does not get
* tempted to inline it. We want to be able to see some stack traces.
*/
Expand Down Expand Up @@ -828,6 +838,25 @@ void TestCoreDump() {
CheckExtraNotesWithReadElf(input, output, core_test);
assert(!unlink(core_test));

/* Check callback function handling */
puts("Checking callback functions");
ClearCoreDumpParameters(&note_params);
int count = 0;
assert(!SetCoreDumpCallback(&note_params, MyCallback, &count));
assert(!SetCoreDumpLimited(&note_params, 0x10000));
rc = (loop?MyWriteCoreDumpWith:WriteCoreDumpWith)
(&note_params, core_test);
assert(!rc);
CheckWithReadElf(input, output, core_test, "", "cat", "");
assert(count == 1);
assert(!unlink(core_test));
count = -1;
rc = (loop?MyWriteCoreDumpWith:WriteCoreDumpWith)
(&note_params, core_test);
assert(rc);
assert(count == -1);
assert(unlink(core_test) == -1 && errno == ENOENT);

/* Create a full-size core file */
puts("Checking uncompressed core files");
rc = (loop?MyWriteCoreDump:WriteCoreDump)(core_test);
Expand Down
13 changes: 11 additions & 2 deletions src/elfcore.c
Original file line number Diff line number Diff line change
Expand Up @@ -1673,6 +1673,17 @@ int InternalGetCoreDump(void *frame, int num_threads, pid_t *pids,
int pair[2];
int main_pid = ((Frame *)frame)->tid;

const struct CoreDumpParameters *params =
va_arg(ap, const struct CoreDumpParameters *);

int (*callback_fn)(void*) = GetCoreDumpParameter(params, callback_fn);
if (callback_fn) {
void *arg = GetCoreDumpParameter(params, callback_arg);
if (callback_fn(arg) != 0) {
goto error;
}
}

/* Get thread status */
memset(puser, 0, sizeof(struct core_user));
memset(thread_regs, 0, threads * sizeof(struct regs));
Expand Down Expand Up @@ -1889,8 +1900,6 @@ int InternalGetCoreDump(void *frame, int num_threads, pid_t *pids,
int pagesize = sys_sysconf(_SC_PAGESIZE);
struct kernel_sigset_t old_signals, blocked_signals;

const struct CoreDumpParameters *params =
va_arg(ap, const struct CoreDumpParameters *);
const char *file_name =
va_arg(ap, const char *);
size_t max_length =
Expand Down
13 changes: 13 additions & 0 deletions src/google/coredumper.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ struct CoreDumpParameters {
const struct CoredumperNote *notes;
/* The amount of notes in the notes array. */
int note_count;
/* Callback function */
int (*callback_fn)(void*);
/* Callback argument */
void *callback_arg;
};

/* The core file is limited in size and max_length denotes the maximum size. If
Expand Down Expand Up @@ -247,6 +251,15 @@ int SetCoreDumpLimitedByPriority(struct CoreDumpParameters *params,
int SetCoreDumpNotes(struct CoreDumpParameters *params,
struct CoredumperNote *notes, int note_count);

/* Sets the coredumper parameters to provide a callback function.
* The callback will be invoked after all threads have been suspended but
* before any coredump operation is invoked. The callback argument will be
* provided. If the return value is 0 then the coredump will continue; if the
* value is something other than 0 then the coredump will not be generated.
*/
int SetCoreDumpCallback(struct CoreDumpParameters *params,
int (*fn)(void*), void *arg);

#ifdef __cplusplus
}
#endif
Expand Down
1 change: 1 addition & 0 deletions src/libcoredumper.sym
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ SetCoreDumpCompressed
SetCoreDumpLimited
SetCoreDumpLimitedByPriority
SetCoreDumpNotes
SetCoreDumpCallback

0 comments on commit 0224d1f

Please sign in to comment.