subdaemon: better GDB support.

It was annoying me, so I made it much nicer to use.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2018-09-04 12:23:32 +09:30 committed by Christian Decker
parent ae61f645ab
commit 7b9341e762
3 changed files with 15 additions and 18 deletions

View File

@ -1,3 +1,4 @@
#include <ccan/tal/str/str.h>
#include <common/dev_disconnect.h> #include <common/dev_disconnect.h>
#include <common/status.h> #include <common/status.h>
#include <common/subdaemon.h> #include <common/subdaemon.h>
@ -24,11 +25,6 @@ static void status_backtrace_exit(void)
status_failed(STATUS_FAIL_INTERNAL_ERROR, "FATAL SIGNAL"); 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[]) void subdaemon_setup(int argc, char *argv[])
{ {
if (argc == 2 && streq(argv[1], "--version")) { 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. */ /* From debugger, set debugger_spin to 0. */
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
if (streq(argv[i], "--debugger")) { if (streq(argv[i], "--debugger")) {
fprintf(stderr, "gdb -ex 'attach %u' -ex 'p debugger_connected=1' %s\n", char *cmd = tal_fmt(NULL, "${DEBUG_TERM:-gnome-terminal --} gdb -ex 'attach %u' %s &", getpid(), argv[0]);
getpid(), argv[0]); fprintf(stderr, "Running %s\n", cmd);
while (!debugger_connected) system(cmd);
usleep(10000); /* Continue in the debugger. */
kill(getpid(), SIGSTOP);
} }
if (strstarts(argv[i], "--dev-disconnect=")) { if (strstarts(argv[i], "--dev-disconnect=")) {
dev_disconnect_init(atoi(argv[i] dev_disconnect_init(atoi(argv[i]

View File

@ -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 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 You can debug crashing subdaemons with the argument
`--dev-debugger=lightning_channeld`, where `channeld` is the subdaemon name. `--dev-debugger=channeld`, where `channeld` is the subdaemon name. It
It will print out (to stderr) a command such as: 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' \ It will also print out (to stderr) the gdb command for manual connection. The
lightningd/lightning_hsmd subdaemon will be stopped (it sends itself a SIGSTOP); you'll need to
`continue` in gdb.
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.
Database Database
-------- --------

View File

@ -406,7 +406,7 @@ static void dev_register_opts(struct lightningd *ld)
opt_register_noarg("--dev-fail-on-subdaemon-fail", opt_set_bool, opt_register_noarg("--dev-fail-on-subdaemon-fail", opt_set_bool,
&ld->dev_subdaemon_fail, opt_hidden); &ld->dev_subdaemon_fail, opt_hidden);
opt_register_arg("--dev-debugger=<subdaemon>", opt_subd_debug, NULL, opt_register_arg("--dev-debugger=<subdaemon>", opt_subd_debug, NULL,
ld, "Wait for gdb attach at start of <subdaemon>"); ld, "Invoke gdb at start of <subdaemon>");
opt_register_arg("--dev-broadcast-interval=<ms>", opt_set_uintval, opt_register_arg("--dev-broadcast-interval=<ms>", opt_set_uintval,
opt_show_uintval, &ld->config.broadcast_interval, opt_show_uintval, &ld->config.broadcast_interval,
"Time between gossip broadcasts in milliseconds"); "Time between gossip broadcasts in milliseconds");