Skip to content

Perform operations after crash #720

Description

@arijith96

Description

When does the problem happen

  • During build
  • During run-time
  • When capturing a hard crash

Environment

  • OS: [e.g. Windows 10, 64-bit] Windows 10, 64-bit
  • Compiler: [e.g. MSVC 19] MSVC 19
  • CMake version and config: [e.g. 3.17.2, SENTRY_BACKEND=inproc] 3.16.4, SENTRY_BACKEND=crashpad_handler

Steps To Reproduce

I wanted to know how I could perform some operations right after a crash has occurred, since I want to do somethings like the following:

  • trim a log file to include only the last 100 lines and then write it into a file and upload that file along with the crash report to sentry
  • zip a certain file and send it along the crash report

I came across before_send() but as stated in the docs, but it states that crashpad does not invoke this when a crash occurs. Please note that my primary intention is for crashes only and not for any message/error events.

Activity

  1. Swatinem commented on May 30, 2022

    @Swatinem
    Contributor

    The docs are partly out of date. We got the before_send hook working on Windows and Linux. However macOS is the odd one out here that still does not support this.

    Switching to the breakpad handler should also provide this functionality, as with that backend, the hook is being invoked on all platforms.

  2. arijith96 commented on May 30, 2022

    @arijith96
    Author

    Okay, thank you for the update. Will close this issue as soon as I have tried and verified this.

  3. p0358 commented on May 30, 2022

    @p0358

    Is there any reliable way to tell whether the before_send hook was called on an actual crash or with some other non-crashing random kind of event?

    Looking at

    sentry_value_t event = sentry_value_new_event();
    if (options->before_send_func) {
    SENTRY_TRACE("invoking `before_send` hook");
    event = options->before_send_func(
    event, NULL, options->before_send_data);
    }
    sentry_value_decref(event);

    and
    sentry__prepare_event(const sentry_options_t *options, sentry_value_t event,
    sentry_uuid_t *event_id)
    {
    sentry_envelope_t *envelope = NULL;
    if (event_is_considered_error(event)) {
    sentry__record_errors_on_current_session(1);
    }
    bool should_skip = !sentry__roll_dice(options->sample_rate);
    if (should_skip) {
    SENTRY_DEBUG("throwing away event due to sample rate");
    goto fail;
    }
    SENTRY_WITH_SCOPE (scope) {
    SENTRY_TRACE("merging scope into event");
    sentry_scope_mode_t mode = SENTRY_SCOPE_ALL;
    if (!options->symbolize_stacktraces) {
    mode &= ~SENTRY_SCOPE_STACKTRACES;
    }
    sentry__scope_apply_to_event(scope, options, event, mode);
    }
    if (options->before_send_func) {
    SENTRY_TRACE("invoking `before_send` hook");
    event
    = options->before_send_func(event, NULL, options->before_send_data);

    it seems like one way would be to check if the event passed to the hook function is empty/null (as with crash) or not empty (as with other kind of event), is that the expected way to go about this?

    And the telling those apart itself is definitely needed, since a random silent event shouldn't generate stuff like extra diagnostic logging or displaying a message to the user about crash

  4. p0358 commented on May 30, 2022

    @p0358

    It seems like the native handler generates a generic event only to discard it after the before_send function...

  5. Swatinem commented on Jun 1, 2022

    @Swatinem
    Contributor

    Depending on the crash backend, we may also have a full event object (in case of inproc).

    We could think about exposing a new hook that is only called in case of crash.

  6. supervacuus commented on Sep 29, 2022

    @supervacuus
    Collaborator

    @arijith96, were you able to verify this? feel free to reopen if you still have issues with this.

    @p0358, we introduced an on_crash hook with 0.5.0. Depending on the backend, you will still get different behavior concerning enrichment and filtering, but it allows you to distinguish between crashes and other events.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions