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

feat: API to add a stack trace to an exception or thread #723

Merged
merged 6 commits into from
Jun 14, 2022
Merged
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
3 changes: 1 addition & 2 deletions examples/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,7 @@ main(int argc, char **argv)
sentry_value_t exc = sentry_value_new_exception(
"ParseIntError", "invalid digit found in string");
if (has_arg(argc, argv, "add-stacktrace")) {
sentry_value_t stacktrace = sentry_value_new_stacktrace(NULL, 0);
sentry_value_set_by_key(exc, "stacktrace", stacktrace);
sentry_value_set_stacktrace(exc, NULL, 0);
}
sentry_value_t event = sentry_value_new_event();
sentry_event_add_exception(event, exc);
Expand Down
15 changes: 13 additions & 2 deletions include/sentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -409,15 +409,26 @@ SENTRY_EXPERIMENTAL_API sentry_value_t sentry_value_new_thread(
*
* See https://develop.sentry.dev/sdk/event-payloads/stacktrace/
*
* The returned object needs to be attached to either an exception
* event, or a thread object.
* The returned object must be attached to either an exception or thread
* object.
*
* If `ips` is NULL the current stack trace is captured, otherwise `len`
* stack trace instruction pointers are attached to the event.
*/
SENTRY_EXPERIMENTAL_API sentry_value_t sentry_value_new_stacktrace(
void **ips, size_t len);

/**
* Sets the Stack Trace conforming to the Stack Trace Interface in a value.
*
* The value argument must be either an exception or thread object.
*
* If `ips` is NULL the current stack trace is captured, otherwise `len` stack
* trace instruction pointers are attached to the event.
*/
SENTRY_EXPERIMENTAL_API void sentry_value_set_stacktrace(
sentry_value_t value, void **ips, size_t len);

/**
* Adds an Exception to an Event value.
*
Expand Down
6 changes: 1 addition & 5 deletions src/backends/sentry_backend_inproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,7 @@ make_signal_event(
}
SENTRY_TRACEF("captured backtrace with %lu frames", frame_count);

sentry_value_t stacktrace
= sentry_value_new_stacktrace(&backtrace[0], frame_count);

sentry_value_set_by_key(exc, "stacktrace", stacktrace);

sentry_value_set_stacktrace(exc, &backtrace[0], frame_count);
sentry_event_add_exception(event, exc);

return event;
Expand Down
10 changes: 7 additions & 3 deletions src/sentry_value.c
Original file line number Diff line number Diff line change
Expand Up @@ -1200,12 +1200,16 @@ sentry_event_add_thread(sentry_value_t event, sentry_value_t thread)
}

void
sentry_event_value_add_stacktrace(sentry_value_t event, void **ips, size_t len)
sentry_value_set_stacktrace(sentry_value_t value, void **ips, size_t len)
{
sentry_value_t stacktrace = sentry_value_new_stacktrace(ips, len);
sentry_value_set_by_key(value, "stacktrace", stacktrace);
}

void
sentry_event_value_add_stacktrace(sentry_value_t event, void **ips, size_t len)
{
sentry_value_t thread = sentry_value_new_object();
sentry_value_set_by_key(thread, "stacktrace", stacktrace);

sentry_value_set_stacktrace(thread, ips, len);
sentry_event_add_thread(event, thread);
}
18 changes: 18 additions & 0 deletions tests/unit/test_value.c
Original file line number Diff line number Diff line change
Expand Up @@ -546,3 +546,21 @@ SENTRY_TEST(value_collections_leak)
TEST_CHECK_INT_EQUAL(sentry_value_refcount(obj), 1);
sentry_value_decref(obj);
}

SENTRY_TEST(value_set_stacktrace)
{
sentry_value_t exc
= sentry_value_new_exception("std::out_of_range", "vector");
sentry_value_set_stacktrace(exc, NULL, 0);

sentry_value_t stacktrace = sentry_value_get_by_key(exc, "stacktrace");
TEST_CHECK(!sentry_value_is_null(stacktrace));
TEST_CHECK(SENTRY_VALUE_TYPE_OBJECT == sentry_value_get_type(stacktrace));

sentry_value_t frames = sentry_value_get_by_key(stacktrace, "frames");
TEST_CHECK(!sentry_value_is_null(frames));
TEST_CHECK(SENTRY_VALUE_TYPE_LIST == sentry_value_get_type(frames));
TEST_CHECK(0 < sentry_value_get_length(frames));

sentry_value_decref(exc);
}
1 change: 1 addition & 0 deletions tests/unit/tests.inc
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,4 @@ XX(value_object_merge_nested)
XX(value_string)
XX(value_unicode)
XX(value_wrong_type)
XX(value_set_stacktrace)