Skip to content

Commit 41caed9

Browse files
committed
Add plumbing for crash tracking / reporting work
* Add `--report-crash` option * Install crash tracker upon profiler startup
1 parent f25eb72 commit 41caed9

File tree

6 files changed

+37
-24
lines changed

6 files changed

+37
-24
lines changed

include/ddprof_cli.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ struct DDProfCLI {
7171
bool remote_symbolization{false};
7272
bool disable_symbolization{false};
7373
bool reorder_events{false}; // reorder events by timestamp
74+
bool report_crash{false};
75+
bool track_crashes{false};
7476
int maximum_pids{-1};
7577

7678
std::string socket_path;

include/ddprof_context.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct DDProfContext {
3636
bool remote_symbolization{false};
3737
bool disable_symbolization{false};
3838
bool reorder_events{false}; // reorder events by timestamp
39+
bool report_crash{false};
3940
int maximum_pids{0};
4041

4142
cpu_set_t cpu_affinity{};

src/ddprof.cc

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "ddprof.hpp"
77

88
#include "cap_display.hpp"
9+
#include "crash_tracker.hpp"
910
#include "ddprof_cmdline.hpp"
1011
#include "ddprof_context.hpp"
1112
#include "ddprof_context_lib.hpp"
@@ -33,26 +34,6 @@
3334

3435
namespace ddprof {
3536
namespace {
36-
/***************************** SIGSEGV Handler *******************************/
37-
void sigsegv_handler(int sig, siginfo_t *si, void *uc) {
38-
// TODO this really shouldn't call printf-family functions...
39-
(void)uc;
40-
#ifdef __GLIBC__
41-
constexpr size_t k_stacktrace_buffer_size = 4096;
42-
static void *buf[k_stacktrace_buffer_size] = {};
43-
size_t const sz = backtrace(buf, std::size(buf));
44-
#endif
45-
(void)fprintf(
46-
stderr, "ddprof[%d]: <%.*s> has encountered an error and will exit\n",
47-
getpid(), static_cast<int>(str_version().size()), str_version().data());
48-
if (sig == SIGSEGV) {
49-
printf("[DDPROF] Fault address: %p\n", si->si_addr);
50-
}
51-
#ifdef __GLIBC__
52-
backtrace_symbols_fd(buf, sz, STDERR_FILENO);
53-
#endif
54-
exit(-1);
55-
}
5637

5738
void display_system_info() {
5839

@@ -92,6 +73,25 @@ DDRes get_process_threads(pid_t pid, std::vector<pid_t> &threads) {
9273
return {};
9374
}
9475

76+
void configure_crash_tracker(const DDProfContext &ctx) {
77+
std::vector<std::string> args = {"--service", ctx.exp_input.service};
78+
if (!ctx.exp_input.url.empty()) {
79+
args.push_back("--url");
80+
args.push_back(ctx.exp_input.url);
81+
}
82+
if (!ctx.exp_input.host.empty()) {
83+
args.push_back("--host");
84+
args.push_back(ctx.exp_input.host);
85+
}
86+
if (!ctx.exp_input.port.empty()) {
87+
args.push_back("--port");
88+
args.push_back(ctx.exp_input.port);
89+
}
90+
91+
args.push_back("--report-crash");
92+
install_crash_tracker("/proc/self/exe", args);
93+
}
94+
9595
} // namespace
9696

9797
DDRes ddprof_setup(DDProfContext &ctx) {
@@ -115,10 +115,7 @@ DDRes ddprof_setup(DDProfContext &ctx) {
115115

116116
// Setup signal handler if defined
117117
if (ctx.params.fault_info) {
118-
struct sigaction sigaction_handlers = {};
119-
sigaction_handlers.sa_sigaction = sigsegv_handler;
120-
sigaction_handlers.sa_flags = SA_SIGINFO;
121-
sigaction(SIGSEGV, &(sigaction_handlers), nullptr);
118+
configure_crash_tracker(ctx);
122119
}
123120

124121
// Set the nice level, but only if it was overridden because 0 is valid

src/ddprof_cli.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,12 @@ int DDProfCLI::parse(int argc, const char *argv[]) {
398398
->default_val(k_default_max_profiled_pids)
399399
->envname("DD_PROFILING_MAXIMUM_PIDS")
400400
->group(""));
401+
402+
extended_options.push_back(
403+
app.add_flag("--report-crash", report_crash, "Report crash to Datadog")
404+
->default_val(false)
405+
->group(""));
406+
401407
// Parse
402408
CLI11_PARSE(app, argc, argv);
403409

src/ddprof_context_lib.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ void copy_cli_values(const DDProfCLI &ddprof_cli, DDProfContext &ctx) {
9090
ctx.params.show_samples = ddprof_cli.show_samples;
9191
ctx.params.timeline = ddprof_cli.timeline;
9292
ctx.params.fault_info = ddprof_cli.fault_info;
93+
ctx.params.report_crash = ddprof_cli.report_crash;
9394
ctx.params.remote_symbolization = ddprof_cli.remote_symbolization;
9495
ctx.params.disable_symbolization = ddprof_cli.disable_symbolization;
9596
ctx.params.reorder_events = ddprof_cli.reorder_events;

src/exe/main.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// Datadog, Inc.
55

66
#include "constants.hpp"
7+
#include "crash_reporter.hpp"
78
#include "daemonize.hpp"
89
#include "ddprof.hpp"
910
#include "ddprof_cli.hpp"
@@ -409,6 +410,11 @@ int main(int argc, char *argv[]) {
409410

410411
} // cli is destroyed here (prevents forks from having an instance of CLI
411412

413+
if (ctx->params.report_crash) {
414+
report_crash(getppid(), ctx->params.pid, ctx->exp_input);
415+
return 0;
416+
}
417+
412418
// Save switch_user since ctx will be destroyed after call to start_profiler
413419
std::string const switch_user = ctx->params.switch_user;
414420

0 commit comments

Comments
 (0)