|
| 1 | +// Copyright 2026-Present Datadog, Inc. https://www.datadoghq.com/ |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | +// FFI test for ddog_crasht_report_unhandled_exception. |
| 5 | +// |
| 6 | +// This test initializes the crashtracker (without a live signal handler), |
| 7 | +// builds a small runtime StackTrace, calls report_unhandled_exception, and |
| 8 | +// verifies that a crash report file is produced in the current directory. |
| 9 | +// |
| 10 | +// Usage: |
| 11 | +// crashtracking_unhandled_exception [receiver_binary_path] |
| 12 | +// |
| 13 | +// The receiver binary path may also be supplied via the |
| 14 | +// DDOG_CRASHT_TEST_RECEIVER environment variable. When run through |
| 15 | +// `cargo ffi-test` the variable is set automatically. |
| 16 | + |
| 17 | +#include <datadog/common.h> |
| 18 | +#include <datadog/crashtracker.h> |
| 19 | +#include <stdio.h> |
| 20 | +#include <stdlib.h> |
| 21 | +#include <string.h> |
| 22 | + |
| 23 | +static ddog_CharSlice slice(const char *s) { |
| 24 | + return (ddog_CharSlice){.ptr = s, .len = strlen(s)}; |
| 25 | +} |
| 26 | + |
| 27 | +static void handle_void(ddog_VoidResult result, const char *ctx) { |
| 28 | + if (result.tag != DDOG_VOID_RESULT_OK) { |
| 29 | + ddog_CharSlice msg = ddog_Error_message(&result.err); |
| 30 | + fprintf(stderr, "FAIL [%s]: %.*s\n", ctx, (int)msg.len, msg.ptr); |
| 31 | + ddog_Error_drop(&result.err); |
| 32 | + exit(EXIT_FAILURE); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +static void push_named_frame(ddog_crasht_Handle_StackTrace *trace, |
| 37 | + const char *function_name, uintptr_t ip) { |
| 38 | + ddog_crasht_StackFrame_NewResult fr = ddog_crasht_StackFrame_new(); |
| 39 | + if (fr.tag != DDOG_CRASHT_STACK_FRAME_NEW_RESULT_OK) { |
| 40 | + ddog_CharSlice msg = ddog_Error_message(&fr.err); |
| 41 | + fprintf(stderr, "FAIL [StackFrame_new]: %.*s\n", (int)msg.len, msg.ptr); |
| 42 | + ddog_Error_drop(&fr.err); |
| 43 | + exit(EXIT_FAILURE); |
| 44 | + } |
| 45 | + |
| 46 | + ddog_crasht_Handle_StackFrame *frame = |
| 47 | + (ddog_crasht_Handle_StackFrame *)malloc(sizeof(*frame)); |
| 48 | + if (!frame) { |
| 49 | + fputs("FAIL [malloc frame]\n", stderr); |
| 50 | + exit(EXIT_FAILURE); |
| 51 | + } |
| 52 | + *frame = fr.ok; |
| 53 | + |
| 54 | + handle_void(ddog_crasht_StackFrame_with_function(frame, slice(function_name)), |
| 55 | + "StackFrame_with_function"); |
| 56 | + if (ip != 0) { |
| 57 | + handle_void(ddog_crasht_StackFrame_with_ip(frame, ip), |
| 58 | + "StackFrame_with_ip"); |
| 59 | + } |
| 60 | + |
| 61 | + /* push_frame consumes the frame */ |
| 62 | + handle_void(ddog_crasht_StackTrace_push_frame(trace, frame, /*incomplete=*/true), |
| 63 | + "StackTrace_push_frame"); |
| 64 | + free(frame); |
| 65 | +} |
| 66 | + |
| 67 | +// Entry point |
| 68 | +int main(int argc, char **argv) { |
| 69 | + const char *receiver_path = NULL; |
| 70 | + if (argc >= 2) { |
| 71 | + receiver_path = argv[1]; |
| 72 | + } else { |
| 73 | + receiver_path = getenv("DDOG_CRASHT_TEST_RECEIVER"); |
| 74 | + } |
| 75 | + if (!receiver_path || receiver_path[0] == '\0') { |
| 76 | + fputs("FAIL: receiver binary path not provided.\n" |
| 77 | + " Pass it as argv[1] or set DDOG_CRASHT_TEST_RECEIVER.\n", |
| 78 | + stderr); |
| 79 | + return EXIT_FAILURE; |
| 80 | + } |
| 81 | + |
| 82 | + static const char output_file[] = "crashreport_unhandled_exception.json"; |
| 83 | + static const char stderr_file[] = "crashreport_unhandled_exception.stderr"; |
| 84 | + static const char stdout_file[] = "crashreport_unhandled_exception.stdout"; |
| 85 | + |
| 86 | + // Forward the dynamic-linker search path to the receiver process. |
| 87 | + // The receiver is execve'd with an explicit environment so it does not |
| 88 | + // inherit the parent's env automatically. The variable name differs by OS: |
| 89 | + // Linux / ELF → LD_LIBRARY_PATH |
| 90 | + // macOS → DYLD_LIBRARY_PATH |
| 91 | +#ifdef __APPLE__ |
| 92 | + const char *ld_search_path_var = "DYLD_LIBRARY_PATH"; |
| 93 | +#else |
| 94 | + const char *ld_search_path_var = "LD_LIBRARY_PATH"; |
| 95 | +#endif |
| 96 | + const char *ld_library_path = getenv(ld_search_path_var); |
| 97 | + ddog_crasht_EnvVar env_vars[1]; |
| 98 | + ddog_crasht_Slice_EnvVar env_slice = {.ptr = NULL, .len = 0}; |
| 99 | + if (ld_library_path && ld_library_path[0] != '\0') { |
| 100 | + env_vars[0].key = slice(ld_search_path_var); |
| 101 | + env_vars[0].val = slice(ld_library_path); |
| 102 | + env_slice.ptr = env_vars; |
| 103 | + env_slice.len = 1; |
| 104 | + } |
| 105 | + |
| 106 | + ddog_crasht_ReceiverConfig receiver_config = { |
| 107 | + .path_to_receiver_binary = slice(receiver_path), |
| 108 | + .optional_stderr_filename = slice(stderr_file), |
| 109 | + .optional_stdout_filename = slice(stdout_file), |
| 110 | + .env = env_slice, |
| 111 | + }; |
| 112 | + |
| 113 | + struct ddog_Endpoint *endpoint = |
| 114 | + ddog_endpoint_from_filename(slice(output_file)); |
| 115 | + |
| 116 | + struct ddog_crasht_Slice_CInt signals = ddog_crasht_default_signals(); |
| 117 | + ddog_crasht_Config config = { |
| 118 | + .create_alt_stack = false, |
| 119 | + .endpoint = endpoint, |
| 120 | + .resolve_frames = DDOG_CRASHT_STACKTRACE_COLLECTION_DISABLED, |
| 121 | + .signals = {.ptr = signals.ptr, .len = signals.len}, |
| 122 | + }; |
| 123 | + |
| 124 | + ddog_crasht_Metadata metadata = { |
| 125 | + .library_name = slice("crashtracking-ffi-test"), |
| 126 | + .library_version = slice("0.0.0"), |
| 127 | + .family = slice("native"), |
| 128 | + .tags = NULL, |
| 129 | + }; |
| 130 | + |
| 131 | + handle_void(ddog_crasht_init(config, receiver_config, metadata), |
| 132 | + "ddog_crasht_init"); |
| 133 | + ddog_endpoint_drop(endpoint); |
| 134 | + |
| 135 | + // Build a runtime StackTrace with two synthetic frames. |
| 136 | + ddog_crasht_StackTrace_NewResult tr = ddog_crasht_StackTrace_new(); |
| 137 | + if (tr.tag != DDOG_CRASHT_STACK_TRACE_NEW_RESULT_OK) { |
| 138 | + ddog_CharSlice msg = ddog_Error_message(&tr.err); |
| 139 | + fprintf(stderr, "FAIL [StackTrace_new]: %.*s\n", (int)msg.len, msg.ptr); |
| 140 | + ddog_Error_drop(&tr.err); |
| 141 | + return EXIT_FAILURE; |
| 142 | + } |
| 143 | + |
| 144 | + ddog_crasht_Handle_StackTrace *trace = |
| 145 | + (ddog_crasht_Handle_StackTrace *)malloc(sizeof(*trace)); |
| 146 | + if (!trace) { |
| 147 | + fputs("FAIL [malloc trace]\n", stderr); |
| 148 | + return EXIT_FAILURE; |
| 149 | + } |
| 150 | + *trace = tr.ok; |
| 151 | + |
| 152 | + push_named_frame(trace, "com.example.MyApp.processRequest", 0x1000); |
| 153 | + push_named_frame(trace, "com.example.runtime.EventLoop.run", 0x2000); |
| 154 | + push_named_frame(trace, "com.example.runtime.main", 0x3000); |
| 155 | + |
| 156 | + handle_void(ddog_crasht_StackTrace_set_complete(trace), |
| 157 | + "StackTrace_set_complete"); |
| 158 | + |
| 159 | + // Report the unhandled exception. This call: |
| 160 | + // - spawns the receiver process, |
| 161 | + // - sends the crash report over the socket, |
| 162 | + // - waits for the receiver to finish writing the report, |
| 163 | + // - returns Ok on success. |
| 164 | + handle_void( |
| 165 | + ddog_crasht_report_unhandled_exception( |
| 166 | + slice("com.example.UncaughtRuntimeException"), |
| 167 | + slice("Something went very wrong in the runtime"), |
| 168 | + trace), |
| 169 | + "ddog_crasht_report_unhandled_exception"); |
| 170 | + |
| 171 | + free(trace); |
| 172 | + |
| 173 | + // Verify a report file was produced. |
| 174 | + FILE *f = fopen(output_file, "r"); |
| 175 | + if (!f) { |
| 176 | + fprintf(stderr, "FAIL: expected crash report at '%s' but file not found\n", |
| 177 | + output_file); |
| 178 | + return EXIT_FAILURE; |
| 179 | + } |
| 180 | + fclose(f); |
| 181 | + |
| 182 | + printf("PASS: crash report written to '%s'\n", output_file); |
| 183 | + return EXIT_SUCCESS; |
| 184 | +} |
| 185 | + |
0 commit comments