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

[crashtracker] Enable collecting to a UNIX socket #420

Merged
merged 15 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
6 changes: 3 additions & 3 deletions bin_tests/src/bin/crashtracker_bin_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,21 @@ mod unix {
})
};

crashtracker::init(
crashtracker::init_with_receiver(
CrashtrackerConfiguration {
additional_files: vec![],
create_alt_stack: true,
resolve_frames: crashtracker::StacktraceCollection::WithoutSymbols,
endpoint,
timeout,
},
Some(CrashtrackerReceiverConfig::new(
CrashtrackerReceiverConfig::new(
vec![],
env::vars().collect(),
receiver_binary,
Some(stderr_filename),
Some(stdout_filename),
)?),
)?,
CrashtrackerMetadata {
profiling_library_name: "libdatadog".to_owned(),
profiling_library_version: "1.0.0".to_owned(),
Expand Down
2 changes: 1 addition & 1 deletion bin_tests/src/bin/crashtracker_receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ fn main() {}

#[cfg(unix)]
fn main() -> anyhow::Result<()> {
datadog_crashtracker::receiver_entry_point()
datadog_crashtracker::receiver_entry_point_stdin()
}
2 changes: 1 addition & 1 deletion crashtracker/libdatadog-crashtracking-receiver.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <stdlib.h>

int main(void) {
ddog_prof_CrashtrackerResult new_result = ddog_prof_Crashtracker_receiver_entry_point();
ddog_prof_CrashtrackerResult new_result = ddog_prof_Crashtracker_receiver_entry_point_stdin();
if (new_result.tag != DDOG_PROF_CRASHTRACKER_RESULT_OK) {
ddog_CharSlice message = ddog_Error_message(&new_result.err);
fprintf(stderr, "%.*s", (int)message.len, message.ptr);
Expand Down
73 changes: 50 additions & 23 deletions crashtracker/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::{
configuration::CrashtrackerReceiverConfig,
counters::reset_counters,
crash_handler::{
ensure_receiver, register_crash_handlers, restore_old_handlers, shutdown_receiver,
update_receiver_after_fork,
ensure_receiver, ensure_socket, register_crash_handlers, restore_old_handlers,
shutdown_receiver, update_receiver_after_fork,
},
crash_info::CrashtrackerMetadata,
update_config, update_metadata, CrashtrackerConfiguration,
Expand Down Expand Up @@ -81,19 +81,42 @@ pub fn on_fork(
/// ATOMICITY:
/// This function is not atomic. A crash during its execution may lead to
/// unexpected crash-handling behaviour.
pub fn init(
pub fn init_with_receiver(
config: CrashtrackerConfiguration,
receiver_config: Option<CrashtrackerReceiverConfig>,
receiver_config: CrashtrackerReceiverConfig,
metadata: CrashtrackerMetadata,
) -> anyhow::Result<()> {
// Setup the receiver first, so that if there is a crash detected it has
// somewhere to go.
let create_alt_stack = config.create_alt_stack;
update_metadata(metadata)?;
update_config(config)?;
if let Some(receiver_config) = &receiver_config {
ensure_receiver(receiver_config)?;
}
ensure_receiver(&receiver_config)?;
register_crash_handlers(create_alt_stack)?;
Ok(())
}

/// Initialize the crash-tracking infrastructure.
///
/// PRECONDITIONS:
/// None.
/// SAFETY:
/// Crash-tracking functions are not reentrant.
/// No other crash-handler functions should be called concurrently.
/// ATOMICITY:
/// This function is not atomic. A crash during its execution may lead to
/// unexpected crash-handling behaviour.
pub fn init_with_unix_socket(
config: CrashtrackerConfiguration,
socket_path: &str,
metadata: CrashtrackerMetadata,
) -> anyhow::Result<()> {
// Setup the receiver first, so that if there is a crash detected it has
// somewhere to go.
let create_alt_stack = config.create_alt_stack;
update_metadata(metadata)?;
update_config(config)?;
ensure_socket(socket_path)?;
register_crash_handlers(create_alt_stack)?;
Ok(())
}
Expand All @@ -106,7 +129,7 @@ pub fn init(
// look in /tmp/crashreports for the crash reports and output files
#[ignore]
#[test]
fn test_crash() {
fn test_crash() -> anyhow::Result<()> {
use crate::{begin_profiling_op, StacktraceCollection};
use chrono::Utc;
use ddcommon::parse_uri;
Expand All @@ -130,27 +153,28 @@ fn test_crash() {
let stderr_filename = Some(format!("{dir}/stderr_{time}.txt"));
let stdout_filename = Some(format!("{dir}/stdout_{time}.txt"));
let timeout = Duration::from_secs(30);
let receiver_config = Some(
CrashtrackerReceiverConfig::new(
vec![],
vec![],
path_to_receiver_binary,
stderr_filename,
stdout_filename,
)
.expect("Not to fail"),
);
let config =
CrashtrackerConfiguration::new(vec![], create_alt_stack, endpoint, resolve_frames, timeout)
.expect("not to fail");
let receiver_config = CrashtrackerReceiverConfig::new(
vec![],
vec![],
path_to_receiver_binary,
stderr_filename,
stdout_filename,
)?;
let config = CrashtrackerConfiguration::new(
vec![],
create_alt_stack,
endpoint,
resolve_frames,
timeout,
)?;
let metadata = CrashtrackerMetadata::new(
"libname".to_string(),
"version".to_string(),
"family".to_string(),
vec![],
);
init(config, receiver_config, metadata).expect("not to fail");
begin_profiling_op(crate::ProfilingOpTypes::CollectingSample).expect("Not to fail");
init_with_receiver(config, receiver_config, metadata)?;
begin_profiling_op(crate::ProfilingOpTypes::CollectingSample)?;

let tag = Tag::new("apple", "banana").expect("tag");
let metadata2 = CrashtrackerMetadata::new(
Expand All @@ -161,7 +185,10 @@ fn test_crash() {
);
update_metadata(metadata2).expect("metadata");

std::thread::sleep(Duration::from_secs(2));

let p: *const u32 = std::ptr::null();
let q = unsafe { *p };
assert_eq!(q, 3);
Ok(())
}
Loading
Loading