Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Exception API #201

Merged
merged 5 commits into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/sentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ typedef struct sentry_ucontext_s {
#ifdef _WIN32
EXCEPTION_POINTERS exception_ptrs;
#else
int signum;
siginfo_t *siginfo;
ucontext_t *user_context;
#endif
Expand Down Expand Up @@ -650,6 +651,13 @@ SENTRY_API sentry_user_consent_t sentry_user_consent_get(void);
*/
SENTRY_API sentry_uuid_t sentry_capture_event(sentry_value_t event);

/*
* Captures an exception to be handled by the backend.
cammm marked this conversation as resolved.
Show resolved Hide resolved
*
* This is safe to be called from a crashing thread and may not return.
*/
SENTRY_EXPERIMENTAL_API void sentry_handle_exception(sentry_ucontext_t *uctx);

/*
* Adds the breadcrumb to be sent in case of an event.
*/
Expand Down
9 changes: 9 additions & 0 deletions src/backends/sentry_backend_breakpad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ sentry__breakpad_backend_shutdown(sentry_backend_t *backend)
delete eh;
}

static void sentry__breakpad_backend_except(
sentry_backend_t *backend, sentry_ucontext_t *context)
{
google_breakpad::ExceptionHandler *eh
= (google_breakpad::ExceptionHandler *)backend->data;
eh->HandleSignal(context->signum, context->siginfo, context->user_context);
}

extern "C" {

sentry_backend_t *
Expand All @@ -158,6 +166,7 @@ sentry__backend_new(void)

backend->startup_func = sentry__breakpad_backend_startup;
backend->shutdown_func = sentry__breakpad_backend_shutdown;
backend->except_func = sentry__breakpad_backend_except;
backend->free_func = NULL;
backend->flush_scope_func = NULL;
backend->add_breadcrumb_func = NULL;
Expand Down
13 changes: 13 additions & 0 deletions src/backends/sentry_backend_crashpad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,18 @@ sentry__crashpad_backend_free(sentry_backend_t *backend)
sentry_free(data);
}

static void sentry__crashpad_backend_except(
sentry_backend_t *UNUSED(backend), sentry_ucontext_t *context)
{
#ifdef SENTRY_PLATFORM_WINDOWS
crashpad::CrashpadClient::DumpAndCrash(&context->exception_ptrs);
#else
// TODO: Crashpad has the ability to do this on linux / mac but the
// method interface is not exposed for it, a patch would be required
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is unfortunate :-(

(void)context;
#endif
}

sentry_backend_t *
sentry__backend_new(void)
{
Expand All @@ -259,6 +271,7 @@ sentry__backend_new(void)

backend->startup_func = sentry__crashpad_backend_startup;
backend->shutdown_func = sentry__crashpad_backend_shutdown;
backend->except_func = sentry__crashpad_backend_except;
backend->free_func = sentry__crashpad_backend_free;
backend->flush_scope_func = sentry__crashpad_backend_flush_scope;
backend->add_breadcrumb_func = sentry__crashpad_backend_add_breadcrumb;
Expand Down
29 changes: 21 additions & 8 deletions src/backends/sentry_backend_inproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,11 @@ make_signal_event(
}

static void
handle_signal(int signum, siginfo_t *info, void *user_context)
handle_ucontext(sentry_ucontext_t* uctx)
{
sentry_ucontext_t uctx;
uctx.siginfo = info;
uctx.user_context = (ucontext_t *)user_context;

const struct signal_slot *sig_slot = NULL;
for (int i = 0; i < SIGNAL_COUNT; ++i) {
if (SIGNAL_DEFINITIONS[i].signum == signum) {
if (SIGNAL_DEFINITIONS[i].signum == uctx->signum) {
sig_slot = &SIGNAL_DEFINITIONS[i];
}
}
Expand All @@ -178,7 +174,7 @@ handle_signal(int signum, siginfo_t *info, void *user_context)
// only dumps to disk at the moment.
SENTRY_DEBUG("capturing event from signal");
sentry__end_current_session_with_status(SENTRY_SESSION_STATUS_CRASHED);
sentry_capture_event(make_signal_event(sig_slot, &uctx));
sentry_capture_event(make_signal_event(sig_slot, uctx));

// after capturing the crash event, try to dump all the in-flight data of
// the previous transport
Expand All @@ -192,7 +188,23 @@ handle_signal(int signum, siginfo_t *info, void *user_context)
// forward as we're not restoring the page allocator.
reset_signal_handlers();
sentry__leave_signal_handler();
invoke_signal_handler(signum, info, user_context);
invoke_signal_handler(uctx->signum, uctx->siginfo, (void *)uctx->user_context);
}

static void
handle_signal(int signum, siginfo_t *info, void *user_context)
{
sentry_ucontext_t uctx;
uctx.signum = signum;
uctx.siginfo = info;
uctx.user_context = (ucontext_t *)user_context;
handle_ucontext(&uctx);
}

static void handle_except(
sentry_backend_t *UNUSED(backend), sentry_ucontext_t *uctx)
{
handle_ucontext(uctx);
}

static void
Expand Down Expand Up @@ -221,6 +233,7 @@ sentry__backend_new(void)

backend->startup_func = startup_inproc_backend;
backend->shutdown_func = NULL;
backend->except_func = handle_except;
backend->free_func = free_backend;
backend->flush_scope_func = NULL;
backend->add_breadcrumb_func = NULL;
Expand Down
1 change: 1 addition & 0 deletions src/sentry_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ typedef struct sentry_backend_s {
void (*startup_func)(struct sentry_backend_s *);
void (*shutdown_func)(struct sentry_backend_s *);
void (*free_func)(struct sentry_backend_s *);
void (*except_func)(struct sentry_backend_s *, struct sentry_ucontext_s *);
void (*flush_scope_func)(
struct sentry_backend_s *, const sentry_scope_t *scope);
void (*add_breadcrumb_func)(
Expand Down
8 changes: 8 additions & 0 deletions src/sentry_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,14 @@ sentry_capture_event(sentry_value_t event)
return event_id;
}

void sentry_handle_exception(sentry_ucontext_t *uctx)
{
SENTRY_DEBUG("handling exception");
if (g_options->backend && g_options->backend->except_func) {
g_options->backend->except_func(g_options->backend, uctx);
}
}

sentry_options_t *
sentry_options_new(void)
{
Expand Down