I have a Python script that launches several external programs as subprocesses and uses pipes to communicate with them. The system runs on Linux. I want to debug one of the children, a specific executable program. How do I attach gdb to it? I have tried several methods but none work.
set follow-fork-mode child
. Doesn't work because this is not the only child. The first child gets debugged, rather than the one I want.set detach-on-fork off
. Infeasible because this is not the only child. I need to manually tell gdb to continue debugging the parent process, until the program of interest is launched. Theoretically doable, but too much error-prone manual work. If all this manual work can be automated, this is a viable method.How can I accomplish this?
How can I accomplish this?
You can attach a process without starting it from GDB, using gdb -p $pid
. You may need to disable YAMA for this to work.
If your process crashes before you had a chance to attach it, this answer shows one way to debug such process.
Update:
Unfortunately inserting bits of code and rebuilding the program changes the behaviour.
In that case, mv $exe $exe.real
, and put a wrapper executable in its place. The wrapper should be something like:
#include <unistd.h>
int main(int argc, char *argv[])
{
char argv0[PATH_MAX];
volatile int spin = 1;
strcpy(argv0, argv[0]);
strcat(argv0, ".real");
while (spin) sleep(1);
execvp(argv0, argv); // Should not return
abort(); // Unreachable
}
Attach GDB to the wrapper, set spin = 0
and continue to debug $exe.real
.