Skip to content

[crashtracker] API to enable/disable crash report collection #1017

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

Closed
wants to merge 2 commits into from
Closed
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
34 changes: 34 additions & 0 deletions crashtracker-ffi/src/collector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,40 @@ use ddcommon_ffi::{wrap_with_void_ffi_result, Slice, VoidResult};
use function_name::named;
pub use spans::*;

#[no_mangle]
#[must_use]
/// Disables the crashtracker.
/// Note that this does not restore the old signal handlers, but rather turns crash-tracking into a
/// no-op, and then chains the old handlers. This means that handlers registered after the
/// crashtracker will continue to work as expected.
///
/// # Preconditions
/// None
/// # Safety
/// None
/// # Atomicity
/// This function is atomic and idempotent. Calling it multiple times is allowed.
pub unsafe extern "C" fn ddog_crasht_disable() -> VoidResult {
datadog_crashtracker::disable_crashtracker();
VoidResult::Ok(true)
}

#[no_mangle]
#[must_use]
/// Enables the crashtracker, if had been previously disabled.
/// If crashtracking has not been initialized, this function will have no effect.
///
/// # Preconditions
/// None
/// # Safety
/// None
/// # Atomicity
/// This function is atomic and idempotent. Calling it multiple times is allowed.
pub unsafe extern "C" fn ddog_crasht_enable() -> VoidResult {
datadog_crashtracker::enable_crashtracker();
VoidResult::Ok(true)
}

#[no_mangle]
#[must_use]
#[named]
Expand Down
3 changes: 2 additions & 1 deletion crashtracker/src/collector/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
#![cfg(unix)]

use super::receiver_manager::Receiver;
use super::{crash_handler::enable_crashtracker, receiver_manager::Receiver};
use crate::{
clear_spans, clear_traces, collector::signal_handler_manager::register_crash_handlers,
crash_info::Metadata, reset_counters, shared::configuration::CrashtrackerReceiverConfig,
Expand Down Expand Up @@ -68,6 +68,7 @@ pub fn init(
update_config(config.clone())?;
Receiver::update_stored_config(receiver_config);
register_crash_handlers(&config)?;
enable_crashtracker();
Ok(())
}

Expand Down
36 changes: 35 additions & 1 deletion crashtracker/src/collector/crash_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use nix::sys::signal;
use std::io::Write;
use std::ptr;
use std::sync::atomic::Ordering::SeqCst;
use std::sync::atomic::{AtomicPtr, AtomicU64};
use std::sync::atomic::{AtomicBool, AtomicPtr, AtomicU64};
use std::time::Instant;

// Note that this file makes use the following async-signal safe functions in a signal handler.
Expand Down Expand Up @@ -100,10 +100,44 @@ pub(crate) extern "C" fn handle_posix_sigaction(
unsafe { chain_signal_handler(signum, sig_info, ucontext) };
}

static CRASHTRACKER_ENABLED: AtomicBool = AtomicBool::new(false);

/// Disables the crashtracker.
/// Note that this does not restore the old signal handlers, but rather turns crash-tracking into a
/// no-op, and then chains the old handlers. This means that handlers registered after the
/// crashtracker will continue to work as expected.
///
/// # Preconditions
/// None
/// # Safety
/// None
/// # Atomicity
/// This function is atomic and idempotent. Calling it multiple times is allowed.
pub fn disable_crashtracker() {
CRASHTRACKER_ENABLED.store(false, SeqCst);
}

/// Enables the crashtracker, if had been previously disabled.
/// If crashtracking has not been initialized, this function will have no effect.
///
/// # Preconditions
/// None
/// # Safety
/// None
/// # Atomicity
/// This function is atomic and idempotent. Calling it multiple times is allowed.
pub fn enable_crashtracker() {
CRASHTRACKER_ENABLED.store(true, SeqCst);
}

fn handle_posix_signal_impl(
sig_info: *const siginfo_t,
ucontext: *const ucontext_t,
) -> anyhow::Result<()> {
if !CRASHTRACKER_ENABLED.load(SeqCst) {
return Ok(());
}

// If this is a SIGSEGV signal, it could be called due to a stack overflow. In that case, since
// this signal allocates to the stack and cannot guarantee it is running without SA_NODEFER, it
// is possible that we will re-emit the signal. Contemporary unices handle this just fine (no
Expand Down
4 changes: 3 additions & 1 deletion crashtracker/src/collector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ pub use additional_tags::{
};
pub use api::*;
pub use counters::{begin_op, end_op, reset_counters, OpTypes};
pub use crash_handler::{update_config, update_metadata};
pub use crash_handler::{
disable_crashtracker, enable_crashtracker, update_config, update_metadata,
};
pub use spans::{clear_spans, clear_traces, insert_span, insert_trace, remove_span, remove_trace};
6 changes: 3 additions & 3 deletions crashtracker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ mod shared;
#[cfg(all(unix, feature = "collector"))]
pub use collector::{
begin_op, clear_additional_tags, clear_spans, clear_traces, consume_and_emit_additional_tags,
default_signals, end_op, init, insert_additional_tag, insert_span, insert_trace, on_fork,
remove_additional_tag, remove_span, remove_trace, reset_counters, update_config,
update_metadata, OpTypes, DEFAULT_SYMBOLS,
default_signals, disable_crashtracker, enable_crashtracker, end_op, init,
insert_additional_tag, insert_span, insert_trace, on_fork, remove_additional_tag, remove_span,
remove_trace, reset_counters, update_config, update_metadata, OpTypes, DEFAULT_SYMBOLS,
};

#[cfg(all(windows, feature = "collector_windows"))]
Expand Down
Loading