From 83fb8a2fa871dfdfae93de61148f719f560399cf Mon Sep 17 00:00:00 2001 From: Bruno Garcia Date: Tue, 5 Mar 2019 15:37:34 +0100 Subject: [PATCH 1/5] meta: ignore sentry cli local config --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ca0b67c33..a260112a9 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,6 @@ example *.dylib *.dSYM *.mp -*.dmp \ No newline at end of file +*.dmp +.sentryclirc +.venv/ \ No newline at end of file From d4a2ef33b51e10143191d4ed85b1e5c05b3ad304 Mon Sep 17 00:00:00 2001 From: Bruno Garcia Date: Wed, 6 Mar 2019 15:49:44 +0100 Subject: [PATCH 2/5] feat: store event to run folder --- .vscode/settings.json | 4 +++- Makefile | 2 +- example.c | 2 +- src/sentry.cpp | 39 ++++++++++++++++++++++++++++++++++----- 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 8ace5c6fe..3d99b4efe 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -65,6 +65,8 @@ "optional": "cpp", "unordered_map": "cpp", "unordered_set": "cpp", - "mpack.c": "cpp" + "mpack.c": "cpp", + "numeric": "cpp", + "random": "cpp" } } \ No newline at end of file diff --git a/Makefile b/Makefile index 0b38ba750..e73a6f793 100644 --- a/Makefile +++ b/Makefile @@ -13,4 +13,4 @@ example: example.c libsentry.dylib gcc -g -o example example.c -I ./include -L . -lsentry build-example: example clean: - rm -rf example libsentry.dylib completed new pending *.dSYM *.mp *.dmp *.dat \ No newline at end of file + rm -rf example libsentry.dylib crashpad-db *.dSYM *.mp diff --git a/example.c b/example.c index f0ae58b00..283851911 100644 --- a/example.c +++ b/example.c @@ -9,7 +9,7 @@ int main(void) { option.environment = "Production"; option.release = "5fd7a6cd"; option.dist = "12345"; - option.database_path = "."; + option.database_path = "crashpad-db"; option.debug = 1; sentry_init(&option); diff --git a/src/sentry.cpp b/src/sentry.cpp index 46ca8d48e..54e755b64 100644 --- a/src/sentry.cpp +++ b/src/sentry.cpp @@ -3,10 +3,13 @@ #elif defined(SENTRY_BREAKPAD) #include "breakpad_wrapper.hpp" #endif +#include #include +#include #include #include "internal.hpp" #include "print_macros.hpp" +#include "random" #include "sentry.h" #include "vendor/mpack.h" @@ -36,6 +39,8 @@ struct SentryEvent { std::map user; std::map tags; std::map extra; + std::string run_id; + std::string run_path; }; static SentryEvent sentry_event = { @@ -143,7 +148,10 @@ static int minidump_url_from_dsn(char *dsn, std::string &minidump_url_out) { static void serialize(const SentryEvent *event) { mpack_writer_t writer; // TODO: cycle event file - mpack_writer_init_filename(&writer, SENTRY_EVENT_FILE_NAME); + // Path must exist otherwise mpack will fail to write. + auto dest_path = (event->run_path + SENTRY_EVENT_FILE_NAME).c_str(); + SENTRY_PRINT_DEBUG_ARGS("Serializing to file: %s\n", dest_path); + mpack_writer_init_filename(&writer, dest_path); mpack_start_map(&writer, 8); mpack_write_cstr(&writer, "release"); mpack_write_cstr_or_nil(&writer, event->release); @@ -206,6 +214,7 @@ static void serialize(const SentryEvent *event) { int sentry_init(const sentry_options_t *options) { sentry_options = options; + if (options->dsn == nullptr) { SENTRY_PRINT_ERROR("Not DSN specified. Sentry SDK will be disabled.\n"); return SENTRY_ERROR_NO_DSN; @@ -219,10 +228,8 @@ int sentry_init(const sentry_options_t *options) { return err; } - if (options->debug) { - SENTRY_PRINT_DEBUG_ARGS("Initializing with minidump endpoint: %s\n", - minidump_url.c_str()); - } + SENTRY_PRINT_DEBUG_ARGS("Initializing with minidump endpoint: %s\n", + minidump_url.c_str()); err = init(options, minidump_url.c_str()); @@ -238,6 +245,28 @@ int sentry_init(const sentry_options_t *options) { sentry_event.dist = options->dist; } + std::random_device seed; + std::default_random_engine engine(seed()); + std::uniform_int_distribution uniform_dist(0, INT32_MAX); + std::time_t result = std::time(nullptr); + std::stringstream ss; + ss << result << "-" << uniform_dist(engine); + sentry_event.run_id = ss.str(); + + /* Make sure run dir exists before serializer needs to write to it */ + /* TODO: Write proper x-plat mkdir */ + auto run_path = std::string(options->database_path); + mkdir(run_path.c_str(), 0777); + run_path = run_path + "/" + "sentry-runs/"; + mkdir(run_path.c_str(), 0777); + sentry_event.run_path = run_path + sentry_event.run_id + "/"; + auto rv = mkdir(sentry_event.run_path.c_str(), 0777); + if (rv != 0 && rv != EEXIST) { + SENTRY_PRINT_ERROR_ARGS("Failed to create sentry_runs directory '%s'\n", + sentry_event.run_path.c_str()); + return rv; + } + return 0; } From 7cdb1da840caba8c7e9ce1387fca1091b3edfb9a Mon Sep 17 00:00:00 2001 From: Bruno Garcia Date: Wed, 6 Mar 2019 18:18:24 +0100 Subject: [PATCH 3/5] feat: Upload sentry event as attachment --- .DS_Store | Bin 0 -> 6148 bytes src/crashpad_wrapper.cpp | 16 +++++++++++----- src/crashpad_wrapper.hpp | 4 +++- src/sentry.cpp | 5 +++-- 4 files changed, 17 insertions(+), 8 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..c70a829b519419ecc44693ab408550ff426e679a GIT binary patch literal 6148 zcmeH~yGlbr5QhKJ6afvSu&~_DPRJW9A(mFBvhxzrK%$34!F1;pd;_1*2e9&=ol$c1 z5U>%E8QA^zGPAqqgR>U^GJc$nfFXcBld5Q;!?d}xYsZ~?R4HTBnByA9n4;6f#ERqJ;Q>pm3!AmK8>g@H z44z+q65E=0@S^jE1%V(C1cE>i*hYY7wpxGBH1Z%21cJaP0o@-GlWKNtOv~26tULjz zdW|l|vc4rWCb?$U#*{rYu~4doia#;L!r7kOFS|CT77p>phxp7Nj~6MkbNp1?A=xzY zAP@xF1a_@D)bsy@zf9*Nza0{_AP@xp83E}{s>zri annotations; + std::map fileAttachments = { + {"sentry-event.mp", base::FilePath(event_file)}}; + // Optional arguments to pass to the handler std::vector arguments; arguments.push_back("--no-rate-limit"); CrashpadClient client; - bool success = client.StartHandler(handler, database, database, url, - annotations, arguments, - /* restartable */ true, - /* asynchronous_start */ false); + bool success = client.StartHandlerWithAttachments( + handler, database, database, url, annotations, fileAttachments, + arguments, + /* restartable */ true, + /* asynchronous_start */ false); if (success) { SENTRY_PRINT_DEBUG("Started client handler.\n"); diff --git a/src/crashpad_wrapper.hpp b/src/crashpad_wrapper.hpp index 22811d0f6..00e3c522e 100644 --- a/src/crashpad_wrapper.hpp +++ b/src/crashpad_wrapper.hpp @@ -2,7 +2,9 @@ namespace sentry { namespace crashpad { -int init(const sentry_options_t *options, const char *minidump_url); +int init(const sentry_options_t *options, + const char *minidump_url, + const char *event_file); int set_annotation(const char *key, const char *value); int remove_annotation(const char *key); } // namespace crashpad diff --git a/src/sentry.cpp b/src/sentry.cpp index 54e755b64..784f4b735 100644 --- a/src/sentry.cpp +++ b/src/sentry.cpp @@ -231,8 +231,6 @@ int sentry_init(const sentry_options_t *options) { SENTRY_PRINT_DEBUG_ARGS("Initializing with minidump endpoint: %s\n", minidump_url.c_str()); - err = init(options, minidump_url.c_str()); - if (options->environment != nullptr) { sentry_event.environment = options->environment; } @@ -267,6 +265,9 @@ int sentry_init(const sentry_options_t *options) { return rv; } + err = init(options, minidump_url.c_str(), + (sentry_event.run_path + SENTRY_EVENT_FILE_NAME).c_str()); + return 0; } From 4f701aa576edb3b7adfc3cebe218cc2883a47435 Mon Sep 17 00:00:00 2001 From: Bruno Garcia Date: Thu, 7 Mar 2019 10:53:31 +0100 Subject: [PATCH 4/5] ref: const event file name --- src/crashpad_wrapper.cpp | 2 +- src/internal.hpp | 2 ++ src/sentry.cpp | 2 -- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/crashpad_wrapper.cpp b/src/crashpad_wrapper.cpp index 6b7e9e897..7f41d1d82 100644 --- a/src/crashpad_wrapper.cpp +++ b/src/crashpad_wrapper.cpp @@ -32,7 +32,7 @@ int init(const sentry_options_t *options, // Optional annotations passed via --annotations to the handler std::map annotations; std::map fileAttachments = { - {"sentry-event.mp", base::FilePath(event_file)}}; + {SENTRY_EVENT_FILE_NAME, base::FilePath(event_file)}}; // Optional arguments to pass to the handler std::vector arguments; diff --git a/src/internal.hpp b/src/internal.hpp index 3913d2995..b3a13d1dd 100644 --- a/src/internal.hpp +++ b/src/internal.hpp @@ -4,4 +4,6 @@ const sentry_options_t *sentry__get_options(void); +static const char *SENTRY_EVENT_FILE_NAME = "sentry-event.mp"; + #endif \ No newline at end of file diff --git a/src/sentry.cpp b/src/sentry.cpp index 784f4b735..336fb3f44 100644 --- a/src/sentry.cpp +++ b/src/sentry.cpp @@ -19,8 +19,6 @@ using namespace sentry::crashpad; using namespace sentry::breakpad; #endif -static const char *SENTRY_EVENT_FILE_NAME = "sentry-event.mp"; - struct SentryDsn { const char *scheme; const char *public_key; From 5f8284bb2566f8d5af79feb17060de3cd86f4bf2 Mon Sep 17 00:00:00 2001 From: Bruno Garcia Date: Thu, 7 Mar 2019 10:58:09 +0100 Subject: [PATCH 5/5] fix: access modifier --- src/sentry.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sentry.cpp b/src/sentry.cpp index 336fb3f44..ec813a186 100644 --- a/src/sentry.cpp +++ b/src/sentry.cpp @@ -252,11 +252,11 @@ int sentry_init(const sentry_options_t *options) { /* Make sure run dir exists before serializer needs to write to it */ /* TODO: Write proper x-plat mkdir */ auto run_path = std::string(options->database_path); - mkdir(run_path.c_str(), 0777); + mkdir(run_path.c_str(), 0700); run_path = run_path + "/" + "sentry-runs/"; - mkdir(run_path.c_str(), 0777); + mkdir(run_path.c_str(), 0700); sentry_event.run_path = run_path + sentry_event.run_id + "/"; - auto rv = mkdir(sentry_event.run_path.c_str(), 0777); + auto rv = mkdir(sentry_event.run_path.c_str(), 0700); if (rv != 0 && rv != EEXIST) { SENTRY_PRINT_ERROR_ARGS("Failed to create sentry_runs directory '%s'\n", sentry_event.run_path.c_str());