2020-05-27 19:11:04 +00:00
|
|
|
#include <daemon/SentryWriter.h>
|
|
|
|
|
2020-05-29 19:48:32 +00:00
|
|
|
#include <Poco/File.h>
|
|
|
|
#include <Poco/Util/Application.h>
|
2020-07-09 02:00:48 +00:00
|
|
|
#include <Poco/Util/LayeredConfiguration.h>
|
2020-05-29 19:48:32 +00:00
|
|
|
|
2020-06-02 05:29:13 +00:00
|
|
|
#include <common/defines.h>
|
2020-05-27 19:11:04 +00:00
|
|
|
#include <common/getFQDNOrHostName.h>
|
2020-11-29 08:25:38 +00:00
|
|
|
#include <common/getMemoryAmount.h>
|
2020-05-29 19:48:32 +00:00
|
|
|
#include <common/logger_useful.h>
|
2020-07-09 02:00:48 +00:00
|
|
|
|
2021-04-09 00:28:24 +00:00
|
|
|
#include <Common/formatReadable.h>
|
2020-07-31 20:16:31 +00:00
|
|
|
#include <Common/SymbolIndex.h>
|
2020-07-09 02:00:48 +00:00
|
|
|
#include <Common/StackTrace.h>
|
2020-11-29 08:25:38 +00:00
|
|
|
#include <Common/getNumberOfPhysicalCPUCores.h>
|
2020-07-09 02:00:48 +00:00
|
|
|
|
2020-05-27 19:11:04 +00:00
|
|
|
#if !defined(ARCADIA_BUILD)
|
2020-05-29 21:36:47 +00:00
|
|
|
# include "Common/config_version.h"
|
|
|
|
# include <Common/config.h>
|
2020-05-27 19:11:04 +00:00
|
|
|
#endif
|
|
|
|
|
2020-05-27 20:15:33 +00:00
|
|
|
#if USE_SENTRY
|
2020-07-09 02:00:48 +00:00
|
|
|
|
2020-05-30 10:54:57 +00:00
|
|
|
# include <sentry.h> // Y_IGNORE
|
2020-06-11 18:12:48 +00:00
|
|
|
# include <stdio.h>
|
2020-06-16 12:56:28 +00:00
|
|
|
# include <filesystem>
|
2020-05-27 20:15:33 +00:00
|
|
|
|
2020-05-27 19:11:04 +00:00
|
|
|
|
2020-05-29 21:36:47 +00:00
|
|
|
namespace
|
|
|
|
{
|
2020-06-02 07:33:11 +00:00
|
|
|
|
2020-06-01 18:25:25 +00:00
|
|
|
bool initialized = false;
|
|
|
|
bool anonymize = false;
|
2020-11-29 08:31:06 +00:00
|
|
|
std::string server_data_path;
|
2020-05-27 20:15:33 +00:00
|
|
|
|
2020-05-29 21:36:47 +00:00
|
|
|
void setExtras()
|
|
|
|
{
|
2020-05-30 08:02:13 +00:00
|
|
|
if (!anonymize)
|
|
|
|
sentry_set_extra("server_name", sentry_value_new_string(getFQDNOrHostName().c_str()));
|
2020-11-29 08:25:38 +00:00
|
|
|
|
2020-06-16 12:56:28 +00:00
|
|
|
sentry_set_tag("version", VERSION_STRING);
|
2020-05-29 21:36:47 +00:00
|
|
|
sentry_set_extra("version_githash", sentry_value_new_string(VERSION_GITHASH));
|
|
|
|
sentry_set_extra("version_describe", sentry_value_new_string(VERSION_DESCRIBE));
|
|
|
|
sentry_set_extra("version_integer", sentry_value_new_int32(VERSION_INTEGER));
|
|
|
|
sentry_set_extra("version_revision", sentry_value_new_int32(VERSION_REVISION));
|
|
|
|
sentry_set_extra("version_major", sentry_value_new_int32(VERSION_MAJOR));
|
|
|
|
sentry_set_extra("version_minor", sentry_value_new_int32(VERSION_MINOR));
|
|
|
|
sentry_set_extra("version_patch", sentry_value_new_int32(VERSION_PATCH));
|
2020-11-29 08:25:38 +00:00
|
|
|
sentry_set_extra("version_official", sentry_value_new_string(VERSION_OFFICIAL));
|
|
|
|
|
2020-11-29 08:34:42 +00:00
|
|
|
/// Sentry does not support 64-bit integers.
|
2020-11-29 08:25:38 +00:00
|
|
|
sentry_set_extra("total_ram", sentry_value_new_string(formatReadableSizeWithBinarySuffix(getMemoryAmountOrZero()).c_str()));
|
|
|
|
sentry_set_extra("physical_cpu_cores", sentry_value_new_int32(getNumberOfPhysicalCPUCores()));
|
2020-11-29 08:31:06 +00:00
|
|
|
|
|
|
|
if (!server_data_path.empty())
|
|
|
|
sentry_set_extra("disk_free_space", sentry_value_new_string(formatReadableSizeWithBinarySuffix(
|
|
|
|
Poco::File(server_data_path).freeSpace()).c_str()));
|
2020-05-29 21:36:47 +00:00
|
|
|
}
|
2020-06-11 18:12:48 +00:00
|
|
|
|
2020-08-05 14:41:27 +00:00
|
|
|
void sentry_logger(sentry_level_e level, const char * message, va_list args, void *)
|
2020-06-11 18:12:48 +00:00
|
|
|
{
|
|
|
|
auto * logger = &Poco::Logger::get("SentryWriter");
|
|
|
|
size_t size = 1024;
|
|
|
|
char buffer[size];
|
2020-06-12 06:35:31 +00:00
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wformat-nonliteral"
|
|
|
|
#endif
|
2020-06-11 22:00:35 +00:00
|
|
|
if (vsnprintf(buffer, size, message, args) >= 0)
|
|
|
|
{
|
2020-06-12 06:35:31 +00:00
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
#endif
|
2020-06-11 22:00:35 +00:00
|
|
|
switch (level)
|
|
|
|
{
|
2020-06-11 18:12:48 +00:00
|
|
|
case SENTRY_LEVEL_DEBUG:
|
|
|
|
logger->debug(buffer);
|
|
|
|
break;
|
|
|
|
case SENTRY_LEVEL_INFO:
|
|
|
|
logger->information(buffer);
|
|
|
|
break;
|
|
|
|
case SENTRY_LEVEL_WARNING:
|
|
|
|
logger->warning(buffer);
|
|
|
|
break;
|
|
|
|
case SENTRY_LEVEL_ERROR:
|
|
|
|
logger->error(buffer);
|
|
|
|
break;
|
|
|
|
case SENTRY_LEVEL_FATAL:
|
|
|
|
logger->fatal(buffer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-09 02:00:48 +00:00
|
|
|
|
2020-06-11 18:12:48 +00:00
|
|
|
}
|
2020-07-09 02:00:48 +00:00
|
|
|
|
2020-05-27 19:11:04 +00:00
|
|
|
|
2020-05-29 21:36:47 +00:00
|
|
|
void SentryWriter::initialize(Poco::Util::LayeredConfiguration & config)
|
|
|
|
{
|
2020-05-27 20:15:33 +00:00
|
|
|
bool enabled = false;
|
2020-05-27 20:21:53 +00:00
|
|
|
bool debug = config.getBool("send_crash_reports.debug", false);
|
2020-06-01 18:25:25 +00:00
|
|
|
auto * logger = &Poco::Logger::get("SentryWriter");
|
2020-05-27 20:15:33 +00:00
|
|
|
if (config.getBool("send_crash_reports.enabled", false))
|
|
|
|
{
|
2021-05-08 14:43:03 +00:00
|
|
|
if (debug || (strlen(VERSION_OFFICIAL) > 0)) //-V560
|
2020-05-27 20:15:33 +00:00
|
|
|
{
|
|
|
|
enabled = true;
|
|
|
|
}
|
2020-05-27 19:11:04 +00:00
|
|
|
}
|
2020-05-27 20:15:33 +00:00
|
|
|
if (enabled)
|
|
|
|
{
|
2020-11-29 08:31:06 +00:00
|
|
|
server_data_path = config.getString("path", "");
|
2020-06-16 12:56:28 +00:00
|
|
|
const std::filesystem::path & default_tmp_path = std::filesystem::path(config.getString("tmp_path", Poco::Path::temp())) / "sentry";
|
2020-05-29 21:36:47 +00:00
|
|
|
const std::string & endpoint
|
2020-06-16 20:01:15 +00:00
|
|
|
= config.getString("send_crash_reports.endpoint");
|
2020-05-29 21:36:47 +00:00
|
|
|
const std::string & temp_folder_path
|
2020-06-16 12:56:28 +00:00
|
|
|
= config.getString("send_crash_reports.tmp_path", default_tmp_path);
|
2020-05-29 19:48:32 +00:00
|
|
|
Poco::File(temp_folder_path).createDirectories();
|
|
|
|
|
2020-06-16 12:56:28 +00:00
|
|
|
sentry_options_t * options = sentry_options_new(); /// will be freed by sentry_init or sentry_shutdown
|
|
|
|
sentry_options_set_release(options, VERSION_STRING_SHORT);
|
2020-08-05 14:41:27 +00:00
|
|
|
sentry_options_set_logger(options, &sentry_logger, nullptr);
|
2020-05-27 20:21:53 +00:00
|
|
|
if (debug)
|
|
|
|
{
|
|
|
|
sentry_options_set_debug(options, 1);
|
|
|
|
}
|
2020-05-27 20:15:33 +00:00
|
|
|
sentry_options_set_dsn(options, endpoint.c_str());
|
2020-05-29 19:48:32 +00:00
|
|
|
sentry_options_set_database_path(options, temp_folder_path.c_str());
|
2020-05-29 21:36:47 +00:00
|
|
|
if (strstr(VERSION_DESCRIBE, "-stable") || strstr(VERSION_DESCRIBE, "-lts"))
|
|
|
|
{
|
2020-05-27 20:15:33 +00:00
|
|
|
sentry_options_set_environment(options, "prod");
|
2020-05-29 21:36:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-27 20:15:33 +00:00
|
|
|
sentry_options_set_environment(options, "test");
|
|
|
|
}
|
2020-05-30 08:02:13 +00:00
|
|
|
|
2020-05-30 08:24:21 +00:00
|
|
|
const std::string & http_proxy = config.getString("send_crash_reports.http_proxy", "");
|
|
|
|
if (!http_proxy.empty())
|
|
|
|
{
|
|
|
|
sentry_options_set_http_proxy(options, http_proxy.c_str());
|
|
|
|
}
|
|
|
|
|
2020-05-29 19:48:32 +00:00
|
|
|
int init_status = sentry_init(options);
|
|
|
|
if (!init_status)
|
|
|
|
{
|
|
|
|
initialized = true;
|
2020-05-30 08:02:13 +00:00
|
|
|
anonymize = config.getBool("send_crash_reports.anonymize", false);
|
2020-05-29 19:48:32 +00:00
|
|
|
LOG_INFO(
|
2020-06-01 14:15:14 +00:00
|
|
|
logger,
|
2020-05-30 08:02:13 +00:00
|
|
|
"Sending crash reports is initialized with {} endpoint and {} temp folder{}",
|
2020-05-29 19:48:32 +00:00
|
|
|
endpoint,
|
2020-05-30 08:02:13 +00:00
|
|
|
temp_folder_path,
|
2020-06-16 12:56:28 +00:00
|
|
|
anonymize ? " (anonymized)" : "");
|
2020-05-29 19:48:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-16 12:56:28 +00:00
|
|
|
LOG_WARNING(logger, "Sending crash reports failed to initialize with {} status", init_status);
|
2020-05-29 19:48:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-01 14:15:14 +00:00
|
|
|
LOG_INFO(logger, "Sending crash reports is disabled");
|
2020-05-27 20:15:33 +00:00
|
|
|
}
|
2020-05-27 19:11:04 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 21:36:47 +00:00
|
|
|
void SentryWriter::shutdown()
|
|
|
|
{
|
|
|
|
if (initialized)
|
2020-05-27 20:15:33 +00:00
|
|
|
sentry_shutdown();
|
2020-05-27 19:11:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 20:16:31 +00:00
|
|
|
void SentryWriter::onFault(int sig, const std::string & error_message, const StackTrace & stack_trace)
|
2020-05-27 19:11:04 +00:00
|
|
|
{
|
2020-06-01 18:25:25 +00:00
|
|
|
auto * logger = &Poco::Logger::get("SentryWriter");
|
2020-05-27 20:15:33 +00:00
|
|
|
if (initialized)
|
2020-05-27 19:11:04 +00:00
|
|
|
{
|
2020-05-27 20:15:33 +00:00
|
|
|
sentry_value_t event = sentry_value_new_message_event(SENTRY_LEVEL_FATAL, "fault", error_message.c_str());
|
|
|
|
sentry_set_tag("signal", strsignal(sig));
|
|
|
|
sentry_set_extra("signal_number", sentry_value_new_int32(sig));
|
2020-07-31 20:16:31 +00:00
|
|
|
|
|
|
|
#if defined(__ELF__) && !defined(__FreeBSD__)
|
2020-11-30 14:30:55 +00:00
|
|
|
const String & build_id_hex = DB::SymbolIndex::instance()->getBuildIDHex();
|
2020-06-23 21:27:04 +00:00
|
|
|
sentry_set_tag("build_id", build_id_hex.c_str());
|
2020-07-31 20:16:31 +00:00
|
|
|
#endif
|
|
|
|
|
2020-05-27 20:15:33 +00:00
|
|
|
setExtras();
|
|
|
|
|
|
|
|
/// Prepare data for https://develop.sentry.dev/sdk/event-payloads/stacktrace/
|
2020-06-10 16:48:08 +00:00
|
|
|
sentry_value_t sentry_frames = sentry_value_new_list();
|
2020-05-27 20:15:33 +00:00
|
|
|
size_t stack_size = stack_trace.getSize();
|
|
|
|
if (stack_size > 0)
|
2020-05-27 19:11:04 +00:00
|
|
|
{
|
2020-06-16 12:56:28 +00:00
|
|
|
ssize_t offset = stack_trace.getOffset();
|
2020-05-27 20:15:33 +00:00
|
|
|
char instruction_addr[100];
|
2020-06-10 14:51:25 +00:00
|
|
|
StackTrace::Frames frames;
|
2020-06-23 21:15:54 +00:00
|
|
|
StackTrace::symbolize(stack_trace.getFramePointers(), offset, stack_size, frames);
|
2020-06-16 12:56:28 +00:00
|
|
|
for (ssize_t i = stack_size - 1; i >= offset; --i)
|
2020-05-27 19:11:04 +00:00
|
|
|
{
|
2020-06-10 14:51:25 +00:00
|
|
|
const StackTrace::Frame & current_frame = frames[i];
|
2020-06-10 16:48:08 +00:00
|
|
|
sentry_value_t sentry_frame = sentry_value_new_object();
|
2020-06-01 20:48:42 +00:00
|
|
|
UInt64 frame_ptr = reinterpret_cast<UInt64>(current_frame.virtual_addr);
|
2020-06-16 12:56:28 +00:00
|
|
|
|
|
|
|
if (std::snprintf(instruction_addr, sizeof(instruction_addr), "0x%" PRIx64, frame_ptr) >= 0)
|
|
|
|
{
|
|
|
|
sentry_value_set_by_key(sentry_frame, "instruction_addr", sentry_value_new_string(instruction_addr));
|
|
|
|
}
|
2020-05-27 20:15:33 +00:00
|
|
|
|
|
|
|
if (current_frame.symbol.has_value())
|
|
|
|
{
|
2020-06-10 16:48:08 +00:00
|
|
|
sentry_value_set_by_key(sentry_frame, "function", sentry_value_new_string(current_frame.symbol.value().c_str()));
|
2020-05-27 20:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (current_frame.file.has_value())
|
|
|
|
{
|
2020-06-10 16:48:08 +00:00
|
|
|
sentry_value_set_by_key(sentry_frame, "filename", sentry_value_new_string(current_frame.file.value().c_str()));
|
2020-05-27 20:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (current_frame.line.has_value())
|
|
|
|
{
|
2020-06-10 16:48:08 +00:00
|
|
|
sentry_value_set_by_key(sentry_frame, "lineno", sentry_value_new_int32(current_frame.line.value()));
|
2020-05-27 20:15:33 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 16:48:08 +00:00
|
|
|
sentry_value_append(sentry_frames, sentry_frame);
|
2020-05-27 19:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 20:15:33 +00:00
|
|
|
/// Prepare data for https://develop.sentry.dev/sdk/event-payloads/threads/
|
2020-06-16 12:56:28 +00:00
|
|
|
/// Stacktrace is filled only for a single thread that failed
|
2020-05-27 20:15:33 +00:00
|
|
|
sentry_value_t stacktrace = sentry_value_new_object();
|
2020-06-10 16:48:08 +00:00
|
|
|
sentry_value_set_by_key(stacktrace, "frames", sentry_frames);
|
2020-05-27 19:11:04 +00:00
|
|
|
|
2020-05-27 20:15:33 +00:00
|
|
|
sentry_value_t thread = sentry_value_new_object();
|
|
|
|
sentry_value_set_by_key(thread, "stacktrace", stacktrace);
|
2020-05-27 19:11:04 +00:00
|
|
|
|
2020-05-27 20:15:33 +00:00
|
|
|
sentry_value_t values = sentry_value_new_list();
|
|
|
|
sentry_value_append(values, thread);
|
2020-05-27 19:11:04 +00:00
|
|
|
|
2020-05-27 20:15:33 +00:00
|
|
|
sentry_value_t threads = sentry_value_new_object();
|
|
|
|
sentry_value_set_by_key(threads, "values", values);
|
2020-05-27 19:11:04 +00:00
|
|
|
|
2020-05-27 20:15:33 +00:00
|
|
|
sentry_value_set_by_key(event, "threads", threads);
|
2020-05-27 19:11:04 +00:00
|
|
|
|
2020-06-01 14:15:14 +00:00
|
|
|
LOG_INFO(logger, "Sending crash report");
|
2020-05-27 20:15:33 +00:00
|
|
|
sentry_capture_event(event);
|
|
|
|
shutdown();
|
|
|
|
}
|
2020-05-29 19:48:32 +00:00
|
|
|
else
|
|
|
|
{
|
2020-06-01 14:15:14 +00:00
|
|
|
LOG_INFO(logger, "Not sending crash report");
|
2020-05-29 19:48:32 +00:00
|
|
|
}
|
2020-07-09 02:00:48 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 05:29:13 +00:00
|
|
|
#else
|
2020-07-09 02:00:48 +00:00
|
|
|
|
|
|
|
void SentryWriter::initialize(Poco::Util::LayeredConfiguration &) {}
|
|
|
|
void SentryWriter::shutdown() {}
|
2020-07-31 20:16:31 +00:00
|
|
|
void SentryWriter::onFault(int, const std::string &, const StackTrace &) {}
|
2020-07-09 02:00:48 +00:00
|
|
|
|
2020-05-27 20:15:33 +00:00
|
|
|
#endif
|