diff --git a/common/subdaemon.c b/common/subdaemon.c index 3a602105c..61abfd6f8 100644 --- a/common/subdaemon.c +++ b/common/subdaemon.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -24,11 +25,6 @@ static void status_backtrace_exit(void) status_failed(STATUS_FAIL_INTERNAL_ERROR, "FATAL SIGNAL"); } -#if DEVELOPER -extern volatile bool debugger_connected; -volatile bool debugger_connected; -#endif - void subdaemon_setup(int argc, char *argv[]) { if (argc == 2 && streq(argv[1], "--version")) { @@ -45,10 +41,11 @@ void subdaemon_setup(int argc, char *argv[]) /* From debugger, set debugger_spin to 0. */ for (int i = 1; i < argc; i++) { if (streq(argv[i], "--debugger")) { - fprintf(stderr, "gdb -ex 'attach %u' -ex 'p debugger_connected=1' %s\n", - getpid(), argv[0]); - while (!debugger_connected) - usleep(10000); + char *cmd = tal_fmt(NULL, "${DEBUG_TERM:-gnome-terminal --} gdb -ex 'attach %u' %s &", getpid(), argv[0]); + fprintf(stderr, "Running %s\n", cmd); + system(cmd); + /* Continue in the debugger. */ + kill(getpid(), SIGSTOP); } if (strstarts(argv[i], "--dev-disconnect=")) { dev_disconnect_init(atoi(argv[i] diff --git a/doc/HACKING.md b/doc/HACKING.md index fa35e5200..d0e2c1eea 100644 --- a/doc/HACKING.md +++ b/doc/HACKING.md @@ -92,15 +92,15 @@ Debugging You can build c-lightning with DEVELOPER=1 to use dev commands listed in ``cli/lightning-cli help``. ``./configure --enable-developer`` will do that. You can log console messages with log_info() in lightningd and status_trace() in other subdaemons. You can debug crashing subdaemons with the argument -`--dev-debugger=lightning_channeld`, where `channeld` is the subdaemon name. -It will print out (to stderr) a command such as: +`--dev-debugger=channeld`, where `channeld` is the subdaemon name. It +will run `gnome-terminal` by default with a gdb attached to the +subdaemon when it starts. You can change the terminal used by setting +the `DEBUG_TERM` environment variable, such as `DEBUG_TERM="xterm -e"` +or `DEBUG_TERM="konsole -e"`. - gdb -ex 'attach 22398' -ex 'p debugger_connected=1' \ - lightningd/lightning_hsmd - -Run this command to start debugging. -You may need to type `return` one more time to exit the infinite while -loop, otherwise you can type `continue` to begin. +It will also print out (to stderr) the gdb command for manual connection. The +subdaemon will be stopped (it sends itself a SIGSTOP); you'll need to +`continue` in gdb. Database -------- diff --git a/lightningd/options.c b/lightningd/options.c index aa23ea218..bed2029f3 100644 --- a/lightningd/options.c +++ b/lightningd/options.c @@ -406,7 +406,7 @@ static void dev_register_opts(struct lightningd *ld) opt_register_noarg("--dev-fail-on-subdaemon-fail", opt_set_bool, &ld->dev_subdaemon_fail, opt_hidden); opt_register_arg("--dev-debugger=", opt_subd_debug, NULL, - ld, "Wait for gdb attach at start of "); + ld, "Invoke gdb at start of "); opt_register_arg("--dev-broadcast-interval=", opt_set_uintval, opt_show_uintval, &ld->config.broadcast_interval, "Time between gossip broadcasts in milliseconds");