Skip to content

Commit

Permalink
Fix example
Browse files Browse the repository at this point in the history
  • Loading branch information
gleocadie committed Sep 11, 2024
1 parent e097f88 commit 54ed2cf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
30 changes: 20 additions & 10 deletions examples/ffi/crashinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ extern "C" {
#include <vector>

static ddog_CharSlice to_slice_c_char(const char *s) { return {.ptr = s, .len = strlen(s)}; }
static ddog_CharSlice to_slice_string(std::string &s) {
static ddog_CharSlice to_slice_c_char(const char *s, std::size_t size) { return {.ptr = s, .len = size}; }
static ddog_CharSlice to_slice_string(std::string const &s) {
return {.ptr = s.data(), .len = s.length()};
}

Expand All @@ -39,19 +40,28 @@ void check_result(ddog_crasht_Result result, const char *msg) {
void add_stacktrace(std::unique_ptr<ddog_crasht_CrashInfo, Deleter> &crashinfo) {

// Collect things into vectors so they stay alive till the function exits
std::vector<std::string> filenames;
std::vector<std::string> function_names;
for (uintptr_t i = 0; i < 20; ++i) {
filenames.push_back("/path/to/code/file_" + std::to_string(i));
function_names.push_back("func_" + std::to_string(i));
constexpr std::size_t nb_elements = 20;
std::vector<std::pair<std::string, std::string>> functions_and_filenames{nb_elements};
for (uintptr_t i = 0; i < nb_elements; ++i) {
functions_and_filenames.push_back({"func_" + std::to_string(i), "/path/to/code/file_" + std::to_string(i)});
}

std::vector<ddog_crasht_StackFrameNames> names;
for (uintptr_t i = 0; i < 20; ++i) {
std::vector<ddog_crasht_StackFrameNames> names{nb_elements};
for (auto i = 0; i < nb_elements; i++) {
auto const& [function_name, filename] = functions_and_filenames[i];

auto function_name_slice = to_slice_string(function_name);
auto res = ddog_crasht_demangle(function_name_slice, DDOG_CRASHT_DEMANGLE_OPTIONS_COMPLETE);
if (res.tag == DDOG_CRASHT_STRING_WRAPPER_RESULT_OK)
{
auto string_result = res.ok.message;
function_name_slice = to_slice_c_char((const char*)string_result.ptr, string_result.len);
}

names.push_back({.colno = ddog_Option_U32_some(i),
.filename = to_slice_string(filenames[i]),
.filename = to_slice_string(filename),
.lineno = ddog_Option_U32_some(2 * i + 3),
.name = to_slice_string(function_names[i])});
.name = function_name_slice});
}

std::vector<ddog_crasht_StackFrame> trace;
Expand Down
1 change: 0 additions & 1 deletion examples/ffi/crashtracking.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ int main(int argc, char **argv) {
handle_uintptr_t_result(ddog_crasht_insert_span_id(0, 42));
handle_uintptr_t_result(ddog_crasht_insert_trace_id(1, 1));

ddog_crasht_demangle(DDOG_CHARSLICE_C("nopp"), DDOG_CRASHT_DEMANGLE_OPTIONS_COMPLETE);
#ifdef EXPLICIT_RAISE_SEGV
// Test raising SEGV explicitly, to ensure chaining works
// properly in this case
Expand Down

0 comments on commit 54ed2cf

Please sign in to comment.