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] Make C example emit a file #539

Merged
merged 6 commits into from
Jul 19, 2024
Merged
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
174 changes: 162 additions & 12 deletions examples/ffi/crashtracking.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ void example_segfault_handler(int signal) {
exit(-1);
}

void handle_result(ddog_prof_CrashtrackerResult result) {
if (result.tag == DDOG_PROF_PROFILE_RESULT_ERR) {
ddog_CharSlice message = ddog_Error_message(&result.err);
fprintf(stderr, "%.*s\n", (int)message.len, message.ptr);
ddog_Error_drop(&result.err);
exit(EXIT_FAILURE);
}
}

uintptr_t handle_uintptr_t_result(ddog_prof_CrashtrackerUsizeResult result) {
if (result.tag == DDOG_PROF_PROFILE_RESULT_ERR) {
ddog_CharSlice message = ddog_Error_message(&result.err);
fprintf(stderr, "%.*s\n", (int)message.len, message.ptr);
ddog_Error_drop(&result.err);
exit(EXIT_FAILURE);
}
return result.ok;
}

int main(int argc, char **argv) {
if (signal(SIGSEGV, example_segfault_handler) == SIG_ERR) {
perror("Error setting up signal handler");
Expand All @@ -22,14 +41,20 @@ int main(int argc, char **argv) {
.args = {},
.env = {},
.path_to_receiver_binary = DDOG_CHARSLICE_C("SET ME TO THE ACTUAL PATH ON YOUR MACHINE"),
.optional_stderr_filename = {},
.optional_stdout_filename = {},
// E.g. on my machine, where I run ./build-profiling-ffi.sh build-ffi
// .path_to_receiver_binary =
// DDOG_CHARSLICE_C("/Users/daniel.schwartznarbonne/go/src/github.com/DataDog/libdatadog/"
// "build-ffi/bin/libdatadog-crashtracking-receiver"),
.optional_stderr_filename = DDOG_CHARSLICE_C("/tmp/crashreports/stderr.txt"),
.optional_stdout_filename = DDOG_CHARSLICE_C("/tmp/crashreports/stdout.txt"),
};

ddog_prof_CrashtrackerConfiguration config = {
.create_alt_stack = false,
.endpoint = ddog_prof_Endpoint_agent(DDOG_CHARSLICE_C("http://localhost:8126")),
.resolve_frames = DDOG_PROF_STACKTRACE_COLLECTION_WITHOUT_SYMBOLS,
.endpoint = ddog_Endpoint_file(DDOG_CHARSLICE_C("/tmp/crashreports/crashreport.json")),
// Alternatively:
//.endpoint = ddog_prof_Endpoint_agent(DDOG_CHARSLICE_C("http://localhost:8126")),
.resolve_frames = DDOG_PROF_STACKTRACE_COLLECTION_ENABLED_WITH_INPROCESS_SYMBOLS,
};

ddog_prof_CrashtrackerMetadata metadata = {
Expand All @@ -39,14 +64,11 @@ int main(int argc, char **argv) {
.tags = NULL,
};

ddog_prof_CrashtrackerResult result =
ddog_prof_Crashtracker_init_with_receiver(config, receiver_config, metadata);
if (result.tag == DDOG_PROF_PROFILE_RESULT_ERR) {
ddog_CharSlice message = ddog_Error_message(&result.err);
fprintf(stderr, "%.*s\n", (int)message.len, message.ptr);
ddog_Error_drop(&result.err);
return -1;
}
handle_result(ddog_prof_Crashtracker_init_with_receiver(config, receiver_config, metadata));
handle_result(
ddog_prof_Crashtracker_begin_profiling_op(DDOG_PROF_PROFILING_OP_TYPES_SERIALIZING));
handle_uintptr_t_result(ddog_prof_Crashtracker_insert_span_id(0, 42));
handle_uintptr_t_result(ddog_prof_Crashtracker_insert_trace_id(1, 1));

#ifdef EXPLICIT_RAISE_SEGV
// Test raising SEGV explicitly, to ensure chaining works
Expand All @@ -57,5 +79,133 @@ int main(int argc, char **argv) {
char *bug = NULL;
*bug = 42;

// At this point, we expect the following files to be written into /tmp/crashreports
// foo.txt foo.txt.telemetry stderr.txt stdout.txt
return 0;
}

/* Example output file:
{
"counters": {
"unwinding": 0,
"not_profiling": 0,
"serializing": 1,
"collecting_sample": 0
},
"incomplete": false,
"metadata": {
"profiling_library_name": "crashtracking-test",
"profiling_library_version": "12.34.56",
"family": "crashtracking-test",
"tags": []
},
"os_info": {
"os_type": "Macos",
"version": {
"Semantic": [
14,
5,
0
]
},
"edition": null,
"codename": null,
"bitness": "X64",
"architecture": "arm64"
},
"proc_info": {
"pid": 95565
},
"siginfo": {
"signum": 11,
"signame": "SIGSEGV"
},
"span_ids": [
42
],
"stacktrace": [
{
"ip": "0x100f702ac",
"names": [
{
"colno": 5,
"filename":
"/Users/daniel.schwartznarbonne/.cargo/registry/src/index.crates.io-6f17d22bba15001f/backtrace-0.3.71/src/backtrace/libunwind.rs",
"lineno": 105,
"name": "trace"
},
{
"colno": 5,
"filename":
"/Users/daniel.schwartznarbonne/.cargo/registry/src/index.crates.io-6f17d22bba15001f/backtrace-0.3.71/src/backtrace/mod.rs",
"lineno": 66,
"name":
"trace_unsynchronized<datadog_crashtracker::collectors::emit_backtrace_by_frames::{closure_env#0}<std::process::ChildStdin>>"
},
{
"colno": 5,
"filename":
"/Users/daniel.schwartznarbonne/go/src/github.com/DataDog/libdatadog/crashtracker/src/collectors.rs",
"lineno": 33,
"name": "emit_backtrace_by_frames<std::process::ChildStdin>"
}
],
"sp": "0x16f9658c0",
"symbol_address": "0x100f702ac"
},
{
"ip": "0x100f6f518",
"names": [
{
"colno": 18,
"filename":
"/Users/daniel.schwartznarbonne/go/src/github.com/DataDog/libdatadog/crashtracker/src/crash_handler.rs",
"lineno": 379,
"name": "emit_crashreport<std::process::ChildStdin>"
},
{
"colno": 23,
"filename":
"/Users/daniel.schwartznarbonne/go/src/github.com/DataDog/libdatadog/crashtracker/src/crash_handler.rs",
"lineno": 414,
"name": "handle_posix_signal_impl"
},
{
"colno": 13,
"filename":
"/Users/daniel.schwartznarbonne/go/src/github.com/DataDog/libdatadog/crashtracker/src/crash_handler.rs",
"lineno": 264,
"name": "handle_posix_sigaction"
}
],
"sp": "0x16f965940",
"symbol_address": "0x100f6f518"
},
{
"ip": "0x186b9b584",
"names": [
{
"name": "__simple_esappend"
}
],
"sp": "0x16f965ae0",
"symbol_address": "0x186b9b584"
},
{
"ip": "0x10049bd94",
"names": [
{
"name": "_main"
}
],
"sp": "0x16f965b10",
"symbol_address": "0x10049bd94"
}
],
"trace_ids": [
18446744073709551617
],
"timestamp": "2024-07-19T16:52:16.422378Z",
"uuid": "a42add90-0e60-4799-b9f7-cbe0ebec4f27"
}
*/