feat: add support for tty local debugger

This commit is contained in:
CJ van den Berg 2025-07-03 15:58:12 +02:00
parent 829a8d33e9
commit 2f7228cea6
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 35 additions and 3 deletions

View file

@ -1,10 +1,14 @@
#pragma once
#ifdef __cplusplus
#include <csignal>
extern "C" {
#else
#include <signal.h>
#endif
void install_debugger();
void install_remote_debugger();
void install_backtrace();
void install_jitdebugger();

View file

@ -1,6 +1,5 @@
#ifdef __linux__
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <cstring>
@ -31,7 +30,8 @@ static void get_pid_binpath() {
}
static const auto lldb{"/usr/bin/lldb"};
static const auto default_debugger{"/usr/bin/gdbserver"};
static const auto default_debugger{"/usr/bin/gdb"};
static const auto default_remote_debugger{"/usr/bin/gdbserver"};
static auto get_debugger() -> const char * {
const char *debugger = secure_getenv("JITDEBUG");
@ -47,6 +47,20 @@ static auto get_debugger() -> const char * {
}
const char *const debugger = get_debugger();
static auto get_remote_debugger() -> const char * {
const char *debugger = secure_getenv("JITDEBUG");
if (not debugger)
return default_remote_debugger;
if (strcmp(debugger, "on") == 0)
return default_remote_debugger;
if (strcmp(debugger, "1") == 0)
return default_remote_debugger;
if (strcmp(debugger, "true") == 0)
return default_remote_debugger;
return debugger;
}
const char *const remote_debugger = get_remote_debugger();
void start_debugger(const char *dbg, const char **argv) {
#if defined(PR_SET_PTRACER)
prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0); // NOLINT
@ -67,11 +81,21 @@ void start_debugger(const char *dbg, const char **argv) {
extern "C" void sighdl_debugger(int no, siginfo_t * /*sigi*/, void * /*uco*/) {
get_pid_binpath();
const char *argv[] = {// NOLINT
debugger, "--attach", "[::1]:7777", pid_s, nullptr};
debugger, "--pid", pid_s, nullptr};
start_debugger(debugger, argv);
(void)raise(no);
}
extern "C" void sighdl_remote_debugger(int no, siginfo_t * /*sigi*/,
void * /*uco*/) {
get_pid_binpath();
const char *argv[] = {// NOLINT
remote_debugger, "--attach", "[::1]:7777", pid_s,
"-ex", "continue", nullptr};
start_debugger(remote_debugger, argv);
(void)raise(no);
}
extern "C" void sighdl_backtrace(int no, siginfo_t * /*sigi*/, void * /*uco*/) {
get_pid_binpath();
const char *argv[] = {// NOLINT
@ -98,6 +122,9 @@ static void install_crash_handler(void (*hdlr)(int, siginfo_t *, void *)) {
} // namespace
extern "C" void install_debugger() { install_crash_handler(sighdl_debugger); }
extern "C" void install_remote_debugger() {
install_crash_handler(sighdl_remote_debugger);
}
extern "C" void install_backtrace() { install_crash_handler(sighdl_backtrace); }
extern "C" void install_jitdebugger() {
if (secure_getenv("JITDEBUG"))

View file

@ -18,6 +18,7 @@ pub var stack_trace_on_errors: bool = false;
pub const subprocess = if (builtin.os.tag == .windows) @import("subprocess_windows.zig") else @import("subprocess.zig");
pub const install_debugger = c.install_debugger;
pub const install_remote_debugger = c.install_remote_debugger;
pub const install_backtrace = c.install_backtrace;
pub const install_jitdebugger = c.install_jitdebugger;
pub const max_message_size = 8 * 4096;