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
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…