Skip to content

Commit

Permalink
bin: new stacktrace for SIGSEGV
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Silva <[email protected]>
  • Loading branch information
edsiper committed Apr 6, 2018
1 parent 3cefdef commit 3db3bbf
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/fluent-bit.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,62 @@
#include <fluent-bit/flb_plugin_proxy.h>
#include <fluent-bit/flb_parser.h>

/* Libbacktrace support */
#ifdef FLB_HAVE_LIBBACKTRACE
#include <backtrace.h>
#include <backtrace-supported.h>

struct flb_stacktrace {
struct backtrace_state *state;
int error;
int line;
};

struct flb_stacktrace flb_st;

static void flb_stacktrace_error_callback(void *data,
const char *msg, int errnum)
{
struct flb_stacktrace *ctx = data;
fprintf(stderr, "ERROR: %s (%d)", msg, errnum);
ctx->error = 1;
}

static int flb_stacktrace_print_callback(void *data, uintptr_t pc,
const char *filename, int lineno,
const char *function)
{
struct flb_stacktrace *p = data;

fprintf(stdout, "#%-2i 0x%-17lx in %s() at %s:%d\n",
p->line,
(unsigned long) pc,
function == NULL ? "???" : function,
filename == NULL ? "???" : filename + sizeof(FLB_SOURCE_DIR),
lineno);
p->line++;
return 0;
}

static inline void flb_stacktrace_init(char *prog)
{
memset(&flb_st, '\0', sizeof(struct flb_stacktrace));
flb_st.state = backtrace_create_state(prog,
BACKTRACE_SUPPORTS_THREADS,
flb_stacktrace_error_callback, NULL);
}

void flb_stacktrace_print()
{
struct flb_stacktrace *ctx;

ctx = &flb_st;
backtrace_full(ctx->state, 3, flb_stacktrace_print_callback,
flb_stacktrace_error_callback, ctx);
}

#endif

#ifdef FLB_HAVE_MTRACE
#include <mcheck.h>
#endif
Expand Down Expand Up @@ -160,6 +216,11 @@ static void flb_signal_handler(int signal)
case SIGTERM:
flb_engine_exit(config);
break;
case SIGSEGV:
#ifdef FLB_HAVE_LIBBACKTRACE
flb_stacktrace_print();
abort();
#endif
default:
break;
}
Expand All @@ -173,6 +234,7 @@ static void flb_signal_init()
signal(SIGHUP, &flb_signal_handler);
#endif
signal(SIGTERM, &flb_signal_handler);
signal(SIGSEGV, &flb_signal_handler);
}

static int input_set_property(struct flb_input_instance *in, char *kv)
Expand Down Expand Up @@ -493,6 +555,10 @@ int main(int argc, char **argv)
struct flb_output_instance *out = NULL;
struct flb_filter_instance *filter = NULL;

#ifdef FLB_HAVE_LIBBACKTRACE
flb_stacktrace_init(argv[0]);
#endif

#ifndef _WIN32
/* Setup long-options */
static const struct option long_opts[] = {
Expand Down

0 comments on commit 3db3bbf

Please sign in to comment.