Skip to content
Merged
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
- Crashpad: add error log for oversized envelopes (HTTP 413 Content Too Large). ([#1706](https://github.com/getsentry/sentry-native/pull/1706), [crashpad#155](https://github.com/getsentry/crashpad/pull/155))
- Crashpad: support modifying attachments after `sentry_init` on macOS. ([#1705](https://github.com/getsentry/sentry-native/pull/1705), [crashpad#153](https://github.com/getsentry/crashpad/pull/153))
- Add `cache_dir` envelope header for external crash reporters. ([#1698](https://github.com/getsentry/sentry-native/pull/1698))
- Add `sentry_attachment_set_type` and `SENTRY_ATTACHMENT_TYPE_*` macros for standard Sentry attachment types. ([#1700](https://github.com/getsentry/sentry-native/pull/1700))
- Deprecate `sentry_options_add_view_hierarchy*` in favor of `sentry_attach_file*` with `sentry_attachment_set_type`.

**Fixes**:

Expand Down
14 changes: 8 additions & 6 deletions examples/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -683,12 +683,6 @@ main(int argc, char **argv)
sentry_options_set_crashpad_wait_for_upload(options, true);
}

if (has_arg(argc, argv, "attach-view-hierarchy")) {
// assuming the example / test is run directly from the cmake build
// directory
sentry_options_add_view_hierarchy(options, "./view-hierarchy.json");
}

if (has_arg(argc, argv, "test-logger")) {
// Set up the test logger for integration tests
sentry_options_set_logger(options, test_logger_callback, NULL);
Expand Down Expand Up @@ -862,6 +856,14 @@ main(int argc, char **argv)
= sentry_attach_bytes("\xc0\xff\xee", 3, "bytes.bin");
sentry_attachment_set_content_type(bytes, "application/octet-stream");
}
if (has_arg(argc, argv, "attach-view-hierarchy")) {
// assuming the example / test is run directly from the cmake build
// directory
sentry_attachment_t *view_hierarchy
= sentry_attach_file("./view-hierarchy.json");
sentry_attachment_set_type(
view_hierarchy, SENTRY_ATTACHMENT_TYPE_VIEW_HIERARCHY);
}

if (sentry_options_get_enable_logs(options)) {
if (has_arg(argc, argv, "capture-log")) {
Expand Down
28 changes: 28 additions & 0 deletions include/sentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -1603,8 +1603,12 @@ SENTRY_API void sentry_options_add_attachment_n(
* API Users on windows are encouraged to use
* `sentry_options_add_view_hierarchyw` instead.
*/
SENTRY_DEPRECATED("Use `sentry_attach_*` with `sentry_attachment_set_type` and "
"`SENTRY_ATTACHMENT_TYPE_VIEW_HIERARCHY` instead")
SENTRY_API void sentry_options_add_view_hierarchy(
sentry_options_t *opts, const char *path);
SENTRY_DEPRECATED("Use `sentry_attach_*` with `sentry_attachment_set_type` and "
"`SENTRY_ATTACHMENT_TYPE_VIEW_HIERARCHY` instead")
SENTRY_API void sentry_options_add_view_hierarchy_n(
sentry_options_t *opts, const char *path, size_t path_len);

Expand Down Expand Up @@ -1771,8 +1775,12 @@ SENTRY_API void sentry_options_add_attachmentw_n(
/**
* Wide char version of `sentry_options_add_view_hierarchy`.
*/
SENTRY_DEPRECATED("Use `sentry_attach_*` with `sentry_attachment_set_type` and "
"`SENTRY_ATTACHMENT_TYPE_VIEW_HIERARCHY` instead")
SENTRY_API void sentry_options_add_view_hierarchyw(
sentry_options_t *opts, const wchar_t *path);
SENTRY_DEPRECATED("Use `sentry_attach_*` with `sentry_attachment_set_type` and "
"`SENTRY_ATTACHMENT_TYPE_VIEW_HIERARCHY` instead")
SENTRY_API void sentry_options_add_view_hierarchyw_n(
sentry_options_t *opts, const wchar_t *path, size_t path_len);

Expand Down Expand Up @@ -2757,6 +2765,26 @@ SENTRY_API sentry_attachment_t *sentry_scope_attach_bytesw_n(
const wchar_t *filename, size_t filename_len);
#endif

#define SENTRY_ATTACHMENT_TYPE_GENERIC "event.attachment"
#define SENTRY_ATTACHMENT_TYPE_MINIDUMP "event.minidump"
#define SENTRY_ATTACHMENT_TYPE_VIEW_HIERARCHY "event.view_hierarchy"

/**
* Sets the attachment type.
*
* Well-known attachment types are exposed as `SENTRY_ATTACHMENT_TYPE_*`
* macros. Pass `NULL` or an empty string to clear the attachment type.
*
Comment thread
jpnurmi marked this conversation as resolved.
* Also sets the content type for known attachment types unless explicitly set.
*
* See:
* https://develop.sentry.dev/sdk/telemetry/attachments/#attachment-types
*/
SENTRY_API void sentry_attachment_set_type(
sentry_attachment_t *attachment, const char *type);
SENTRY_API void sentry_attachment_set_type_n(
sentry_attachment_t *attachment, const char *type, size_t type_len);

/**
* Sets the content type of attachment.
*/
Expand Down
51 changes: 36 additions & 15 deletions src/backends/native/sentry_crash_daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ __asan_default_options(void)
*/
static bool
write_attachment_to_envelope(int fd, const char *file_path,
const char *filename, const char *content_type)
const char *filename, const char *attachment_type, const char *content_type)
{
#if defined(SENTRY_PLATFORM_UNIX)
int attach_fd = open(file_path, O_RDONLY);
Expand Down Expand Up @@ -118,16 +118,24 @@ write_attachment_to_envelope(int fd, const char *file_path,
if (content_type) {
header_written = snprintf(header, sizeof(header),
"{\"type\":\"attachment\",\"length\":%lld,"
"\"attachment_type\":\"event.attachment\","
"\"attachment_type\":\"%s\","
"\"content_type\":\"%s\","
"\"filename\":\"%s\"}\n",
file_size, content_type, filename ? filename : "attachment");
file_size,
sentry__string_empty(attachment_type)
? SENTRY_ATTACHMENT_TYPE_GENERIC
: attachment_type,
content_type, filename ? filename : "attachment");
} else {
header_written = snprintf(header, sizeof(header),
"{\"type\":\"attachment\",\"length\":%lld,"
"\"attachment_type\":\"event.attachment\","
"\"attachment_type\":\"%s\","
"\"filename\":\"%s\"}\n",
file_size, filename ? filename : "attachment");
file_size,
sentry__string_empty(attachment_type)
? SENTRY_ATTACHMENT_TYPE_GENERIC
: attachment_type,
filename ? filename : "attachment");
}

if (header_written < 0 || header_written >= (int)sizeof(header)) {
Expand Down Expand Up @@ -204,7 +212,6 @@ attachment_is_placeholder(const sentry_options_t *options, const char *path)
if (!attachment.path) {
return false;
}
attachment.type = ATTACHMENT;
bool is_placeholder
= sentry__attachment_is_placeholder(&attachment, options);
sentry__path_free(attachment.path);
Expand Down Expand Up @@ -252,6 +259,8 @@ add_attachment_refs(sentry_envelope_t *envelope,
= sentry_value_as_string(sentry_value_get_by_key(info, "path"));
const char *filename
= sentry_value_as_string(sentry_value_get_by_key(info, "filename"));
const char *attachment_type = sentry_value_as_string(
sentry_value_get_by_key(info, "attachment_type"));
const char *content_type = sentry_value_as_string(
sentry_value_get_by_key(info, "content_type"));
if (sentry__string_empty(path) || sentry__string_empty(filename)) {
Expand All @@ -267,7 +276,9 @@ add_attachment_refs(sentry_envelope_t *envelope,
sentry__path_free(attachment.filename);
continue;
}
attachment.type = ATTACHMENT;
attachment.type
= (char *)((attachment_type && *attachment_type) ? attachment_type
: NULL);
attachment.content_type
= sentry__string_empty(content_type) ? NULL : (char *)content_type;
if (!sentry__attachment_is_placeholder(&attachment, options)) {
Expand Down Expand Up @@ -2472,20 +2483,25 @@ write_envelope_with_native_stacktrace(const sentry_options_t *options,
= sentry_value_get_by_key(attach_info, "path");
sentry_value_t filename_val
= sentry_value_get_by_key(attach_info, "filename");
sentry_value_t attachment_type_val
= sentry_value_get_by_key(
attach_info, "attachment_type");
sentry_value_t content_type_val
= sentry_value_get_by_key(
attach_info, "content_type");

const char *path = sentry_value_as_string(path_val);
const char *filename
= sentry_value_as_string(filename_val);
const char *attachment_type
= sentry_value_as_string(attachment_type_val);
const char *content_type
= sentry_value_as_string(content_type_val);

if (path && filename
&& !attachment_is_placeholder(options, path)) {
write_attachment_to_envelope(
fd, path, filename, content_type);
write_attachment_to_envelope(fd, path, filename,
attachment_type, content_type);
}
}
sentry_value_decref(attach_list);
Expand All @@ -2500,7 +2516,7 @@ write_envelope_with_native_stacktrace(const sentry_options_t *options,
= sentry__path_join_str(run_folder, "screenshot.png");
if (screenshot_path) {
write_attachment_to_envelope(
fd, screenshot_path->path, "screenshot.png", "image/png");
fd, screenshot_path->path, "screenshot.png", NULL, "image/png");
sentry__path_free(screenshot_path);
}
}
Expand All @@ -2511,7 +2527,7 @@ write_envelope_with_native_stacktrace(const sentry_options_t *options,
= sentry__path_join_str(run_folder, "session-replay.mp4");
if (replay_path) {
write_attachment_to_envelope(
fd, replay_path->path, "session-replay.mp4", "video/mp4");
fd, replay_path->path, "session-replay.mp4", NULL, "video/mp4");
sentry__path_free(replay_path);
}
}
Expand Down Expand Up @@ -2719,20 +2735,25 @@ write_envelope_with_minidump(const sentry_options_t *options,
= sentry_value_get_by_key(attach_info, "path");
sentry_value_t filename_val
= sentry_value_get_by_key(attach_info, "filename");
sentry_value_t attachment_type_val
= sentry_value_get_by_key(
attach_info, "attachment_type");
sentry_value_t content_type_val
= sentry_value_get_by_key(
attach_info, "content_type");

const char *path = sentry_value_as_string(path_val);
const char *filename
= sentry_value_as_string(filename_val);
const char *attachment_type
= sentry_value_as_string(attachment_type_val);
const char *content_type
= sentry_value_as_string(content_type_val);

if (path && filename
&& !attachment_is_placeholder(options, path)) {
write_attachment_to_envelope(
fd, path, filename, content_type);
write_attachment_to_envelope(fd, path, filename,
attachment_type, content_type);
}
}
sentry_value_decref(attach_list);
Expand All @@ -2747,7 +2768,7 @@ write_envelope_with_minidump(const sentry_options_t *options,
= sentry__path_join_str(run_folder, "screenshot.png");
if (screenshot_path) {
write_attachment_to_envelope(
fd, screenshot_path->path, "screenshot.png", "image/png");
fd, screenshot_path->path, "screenshot.png", NULL, "image/png");
sentry__path_free(screenshot_path);
}
}
Expand All @@ -2758,7 +2779,7 @@ write_envelope_with_minidump(const sentry_options_t *options,
= sentry__path_join_str(run_folder, "session-replay.mp4");
if (replay_path) {
write_attachment_to_envelope(
fd, replay_path->path, "session-replay.mp4", "video/mp4");
fd, replay_path->path, "session-replay.mp4", NULL, "video/mp4");
sentry__path_free(replay_path);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/backends/sentry_backend_breakpad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,14 @@ breakpad_backend_callback(const google_breakpad::MinidumpDescriptor &descriptor,
envelope, dump_path, "attachment");
if (item) {
sentry__envelope_item_set_header(item, "attachment_type",
sentry_value_new_string("event.minidump"));
sentry_value_new_string(SENTRY_ATTACHMENT_TYPE_MINIDUMP));

sentry__envelope_item_set_header(item, "filename",
sentry_value_new_string(sentry__path_filename(dump_path)));
} else if (options->enable_large_attachments) {
sentry_attachment_t tmp = {};
tmp.path = dump_path;
tmp.type = MINIDUMP;
tmp.type = (char *)SENTRY_ATTACHMENT_TYPE_MINIDUMP;
if (!sentry__cache_attachment_ref(
envelope, &tmp, options->run->cache_path, nullptr)) {
SENTRY_SIGNAL_SAFE_LOG(
Expand Down
8 changes: 4 additions & 4 deletions src/backends/sentry_backend_crashpad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,8 @@ report_to_envelope(const crashpad::CrashReportDatabase::Report &report,
} else if (strcmp(filename, "__sentry-breadcrumb2") == 0) {
breadcrumbs2 = read_msgpack_file(path);
} else {
sentry__attachments_add_path(&attachments,
sentry__path_clone(path), ATTACHMENT, nullptr);
sentry__attachments_add_path(
&attachments, sentry__path_clone(path), nullptr, nullptr);
}
}
sentry__pathiter_free(iter);
Expand All @@ -541,8 +541,8 @@ report_to_envelope(const crashpad::CrashReportDatabase::Report &report,
sentry_value_set_by_key(event, "breadcrumbs",
sentry__value_merge_breadcrumbs(
breadcrumbs1, breadcrumbs2, options->max_breadcrumbs));
sentry__attachments_add_path(
&attachments, minidump_path, MINIDUMP, nullptr);
sentry__attachments_add_path(&attachments, minidump_path,
SENTRY_ATTACHMENT_TYPE_MINIDUMP, nullptr);

if (sentry__envelope_add_event(envelope, event)) {
sentry__envelope_add_attachments(envelope, attachments, options);
Expand Down
4 changes: 4 additions & 0 deletions src/backends/sentry_backend_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,10 @@ native_backend_write_attachments(const sentry_path_t *event_path)
it->filename ? it->filename : it->path);
sentry_value_set_by_key(
attach_info, "filename", sentry_value_new_string(filename));
if (it->type && *it->type) {
sentry_value_set_by_key(attach_info, "attachment_type",
sentry_value_new_string(it->type));
}
if (it->content_type) {
sentry_value_set_by_key(attach_info, "content_type",
sentry_value_new_string(it->content_type));
Expand Down
Loading
Loading