Skip to content

Commit b262f14

Browse files
committed
sentry plugin: bundle crashpad_handler
1 parent 34d07cc commit b262f14

File tree

5 files changed

+91
-3
lines changed

5 files changed

+91
-3
lines changed

CMakeLists.txt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ set(FILESYSTEM_LAYOUTS default meta-flutter)
7575
set(FILESYSTEM_LAYOUT "default" CACHE STRING "Where to look for the icudtl.dat, app.so/libapp.so, flutter asset bundle.")
7676
set_property(CACHE FILESYSTEM_LAYOUT PROPERTY STRINGS ${FILESYSTEM_LAYOUTS})
7777

78-
set(SENTRY_BACKEND "inproc" CACHE STRING "What sentry backend to use, when the sentry plugin is built. Allowed values are inproc, crashpad and breakpad. Crashpad requires the built crashpad_handler executable that's to be present next to flutter-pi.")
78+
set(SENTRY_BACKEND "inproc" CACHE STRING "What sentry backend to use, when the sentry plugin is built. Allowed values are inproc, crashpad and breakpad.")
79+
option(SENTRY_PLUGIN_BUNDLE_CRASHPAD_HANDLER "Bundle the crashpad_handler with the flutter-pi executable." ON)
7980

8081
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
8182

@@ -352,13 +353,25 @@ if (BUILD_CHARSET_CONVERTER_PLUGIN)
352353
endif()
353354

354355
# Sentry Plugin
356+
set(HAVE_BUNDLED_CRASHPAD_HANDLER OFF)
355357
if (BUILD_SENTRY_PLUGIN)
356358
add_subdirectory(third_party/sentry-native)
357359

358360
target_sources(flutterpi_module PRIVATE src/plugins/sentry/sentry.c)
359-
target_link_libraries(flutterpi_module PUBLIC sentry)
361+
target_link_libraries(flutterpi_module PUBLIC sentry::sentry)
362+
363+
if (SENTRY_PLUGIN_BUNDLE_CRASHPAD_HANDLER)
364+
set(HAVE_BUNDLED_CRASHPAD_HANDLER ON)
365+
366+
target_sources(flutterpi_module PRIVATE src/crashpad_handler_trampoline.cc)
367+
# link against the same libraries the crashpad_handler uses
368+
369+
get_target_property(handler_deps crashpad_handler INTERFACE_LINK_LIBRARIES)
370+
target_link_libraries(flutterpi_module PUBLIC ${handler_deps})
371+
endif()
360372
endif()
361373
message(STATUS "Sentry plugin: ${BUILD_SENTRY_PLUGIN}")
374+
message(STATUS "Bundle crashpad_handler: ${HAVE_BUNDLED_CRASHPAD_HANDLER}")
362375

363376
# Needed so dart VM can actually resolve symbols in the same
364377
# executable. (For dart:ffi DynamicLibrary.executable / DynamicLibrary.process)

config.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@
2525
#cmakedefine VULKAN_DEBUG
2626
#cmakedefine ENABLE_MTRACE
2727
#cmakedefine ENABLE_ASAN
28+
#cmakedefine HAVE_BUNDLED_CRASHPAD_HANDLER
2829

2930
#endif

src/crashpad_handler_trampoline.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "config.h"
2+
3+
#ifndef HAVE_BUNDLED_CRASHPAD_HANDLER
4+
#error "This file should only be built when using the bundled crashpad handler"
5+
#endif
6+
7+
#include "handler/handler_main.h"
8+
9+
extern "C" {
10+
11+
int crashpad_handler_main(int argc, char **argv) {
12+
return crashpad::HandlerMain(argc, argv, nullptr);
13+
}
14+
15+
}

src/main.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
1+
2+
#include <stdbool.h>
3+
#include <string.h>
4+
5+
#include "config.h"
6+
17
int flutterpi_app_main(int argc, char **argv);
8+
int crashpad_handler_main(int argc, char **argv);
9+
10+
#ifdef HAVE_BUNDLED_CRASHPAD_HANDLER
11+
static bool running_in_crashpad_mode(int argc, char **argv) {
12+
for (int i = 0; i < argc; i++) {
13+
if (strcmp(argv[i], "--attachment=FlutterpiCrashpadHandlerMode") == 0) {
14+
return true;
15+
}
16+
}
17+
18+
return false;
19+
}
20+
#endif
221

322
int main(int argc, char **argv) {
23+
#ifdef HAVE_BUNDLED_CRASHPAD_HANDLER
24+
if (running_in_crashpad_mode(argc, argv)) {
25+
return crashpad_handler_main(argc, argv);
26+
}
27+
#endif
28+
429
return flutterpi_app_main(argc, argv);
530
}

src/plugins/sentry/sentry.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#define _POSIX_C_SOURCE 200809L
2+
#include <unistd.h>
3+
14
#include <sentry.h>
25

36
#include "flutter-pi.h"
@@ -18,11 +21,34 @@ struct sentry_plugin {
1821
bool sentry_initialized;
1922
};
2023

24+
static int sentry_configure_bundled_crashpad_handler(sentry_options_t *options) {
25+
char *path = malloc(PATH_MAX);
26+
if (path == NULL) {
27+
return ENOMEM;
28+
}
29+
30+
ssize_t n_read = readlink("/proc/self/exe", path, PATH_MAX);
31+
if (n_read == -1) {
32+
free(path);
33+
return errno;
34+
}
35+
36+
sentry_options_set_handler_path_n(options, path, n_read);
37+
38+
free(path);
39+
40+
sentry_options_add_attachment(options, "FlutterpiCrashpadHandlerMode");
41+
42+
return 0;
43+
}
44+
2145
static void on_init_native_sdk(
2246
struct sentry_plugin *plugin,
2347
const struct raw_std_value *arg,
2448
const FlutterPlatformMessageResponseHandle *responsehandle
2549
) {
50+
int ok;
51+
2652
(void) plugin;
2753
(void) arg;
2854
(void) responsehandle;
@@ -118,7 +144,15 @@ static void on_init_native_sdk(
118144
sentry_options_get_auto_session_tracking(options) ? "true" : "false"
119145
);
120146

121-
int ok = sentry_init(options);
147+
#ifdef HAVE_BUNDLED_CRASHPAD_HANDLER
148+
ok = sentry_configure_bundled_crashpad_handler(options);
149+
if (!ok) {
150+
platch_respond_error_std(responsehandle, "1", "Failed to configure bundled Crashpad handler.", &STDNULL);
151+
return;
152+
}
153+
#endif
154+
155+
ok = sentry_init(options);
122156
if (ok != 0) {
123157
platch_respond_error_std(responsehandle, "1", "Failed to initialize Sentry.", &STDNULL);
124158
return;

0 commit comments

Comments
 (0)