this gist contains several iterations of attempting to attach gdb to a flask/wsgi app running on elastic beanstalk. assumption: ssh access (requires setting a key in the config, and then get the IP address from EB console or EC2 instance list), and sudo.
tl;dr: simple working solution is to
-
write out the pid in
/tmp/xpid
-
sleep in a while loop and wait until that file disappears to continue
-
meanwhile, visit the site, which will trigger the pid write: this is the pid of the forked handler which is handling this specific request. run
sudo gdb
(gdb) attach <pid>
- optionally set a useful breakpoint
- continue
-
now remove
/tmp/xpid
-
the app will continue, and any crashes will happen with gdb attached
the complication is that httpd forks a child per request (by default). answers on stackoverflow suggest changing httpd config so that there is only 1 PID target, but I wanted to leave the config alone.
other things attempted (in comments):
- spawn gdbserver target the running pid. this didn't work, I think gdbserver sits and waits for a connection, then exits when the process itself does. I did not see a way to set a breakpoint w/out connecting from the gdb client side.
- conditionless while loop, with a signal handler for SIGHUP to signal exit. I think this
initially didn't work because there is no yield point for the signal to be checked; then
I tried
signal.pause
, but that didn't work either -- the process just hung despite signalling (inside gdb w/signal SIGHUP
)