From ca8b0363b4ebdd2bdf4adbef9e8bbb230c720c1f Mon Sep 17 00:00:00 2001 From: Balazs Scheidler Date: Sun, 6 Oct 2024 10:31:09 +0200 Subject: [PATCH] syslog-ng-ctl: add support for "attach debugger" Signed-off-by: Balazs Scheidler --- lib/debugger/debugger-main.c | 5 +++++ lib/debugger/debugger-main.h | 1 + lib/mainloop-control.c | 17 +++++++++++++++++ syslog-ng-ctl/commands/attach.c | 4 +++- 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/debugger/debugger-main.c b/lib/debugger/debugger-main.c index 52660609f..7f2dc932d 100644 --- a/lib/debugger/debugger-main.c +++ b/lib/debugger/debugger-main.c @@ -38,6 +38,11 @@ _pipe_hook(LogPipe *s, LogMessage *msg, const LogPathOptions *path_options) return debugger_stop_at_breakpoint(current_debugger, s, msg); } +gboolean +debugger_is_running(void) +{ + return current_debugger != NULL; +} static void _install_hook(gpointer user_data) diff --git a/lib/debugger/debugger-main.h b/lib/debugger/debugger-main.h index 0a3873822..09862bd97 100644 --- a/lib/debugger/debugger-main.h +++ b/lib/debugger/debugger-main.h @@ -28,6 +28,7 @@ #include "debugger/debugger.h" #include "cfg.h" +gboolean debugger_is_running(void); void debugger_start(MainLoop *main_loop, GlobalConfig *cfg); void debugger_stop(void); diff --git a/lib/mainloop-control.c b/lib/mainloop-control.c index 81080a22d..b0404e4d7 100644 --- a/lib/mainloop-control.c +++ b/lib/mainloop-control.c @@ -32,6 +32,7 @@ #include "cfg-walker.h" #include "logpipe.h" #include "console.h" +#include "debugger/debugger-main.h" #include @@ -121,9 +122,12 @@ _wait_until_console_is_released_or_peer_disappears(ControlConnection *cc, gint m static void control_connection_attach(ControlConnection *cc, GString *command, gpointer user_data, gboolean *cancelled) { + MainLoop *main_loop = (MainLoop *) user_data; gchar **cmds = g_strsplit(command->str, " ", 4); + GString *result = g_string_sized_new(128); gint n_seconds = -1; + gboolean start_debugger = FALSE; struct { gboolean log_stderr; @@ -149,6 +153,10 @@ control_connection_attach(ControlConnection *cc, GString *command, gpointer user if (cmds[3] && !_control_process_log_level(cmds[3], result)) goto exit; } + else if (g_str_equal(cmds[1], "DEBUGGER")) + { + start_debugger = TRUE; + } else { g_string_assign(result, "FAIL This version of syslog-ng only supports attaching to STDIO"); @@ -174,7 +182,16 @@ control_connection_attach(ControlConnection *cc, GString *command, gpointer user } console_acquire_from_fds(fds); + if (start_debugger && !debugger_is_running()) + { + //cfg_load_module(self->current_configuration, "mod-python"); + debugger_start(main_loop, main_loop_get_current_config(main_loop)); + } _wait_until_console_is_released_or_peer_disappears(cc, n_seconds, cancelled); + if (start_debugger && debugger_is_running()) + { + debugger_stop(); + } g_string_assign(result, "OK [console output ends here]"); log_stderr = old_values.log_stderr; msg_set_log_level(old_values.log_level); diff --git a/syslog-ng-ctl/commands/attach.c b/syslog-ng-ctl/commands/attach.c index f1e1f4b65..2bd18ac24 100644 --- a/syslog-ng-ctl/commands/attach.c +++ b/syslog-ng-ctl/commands/attach.c @@ -47,7 +47,7 @@ GOptionEntry attach_options[] = { { "seconds", 0, 0, G_OPTION_ARG_INT, &attach_options_seconds, "amount of time to attach for", NULL }, { "log-level", 0, 0, G_OPTION_ARG_CALLBACK, _store_log_level, "change syslog-ng log level", "" }, - { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &attach_commands, "attach mode: logs, stdio", NULL }, + { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &attach_commands, "attach mode: logs, debugger, stdio", NULL }, { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL } }; @@ -73,6 +73,8 @@ slng_attach(int argc, char *argv[], const gchar *mode, GOptionContext *ctx) g_string_append(command, " STDIO"); else if (g_str_equal(attach_mode, "logs")) g_string_append(command, " LOGS"); + else if (g_str_equal(attach_mode, "debugger")) + g_string_append(command, " DEBUGGER"); else { fprintf(stderr, "Unknown attach mode\n");