Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
105 views
in Technique[技术] by (71.8m points)

python - Debugging a specific subprocess

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.

  1. Run python under gdb with 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.
  2. Run python under gdb with 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.
  3. Replace the executable in question with a script that runs the original executable under gdb. Doesn't work because of pipes. Input and/or output of gdb get redirected to the pipe. I need them to go to the terminal obviously.

How can I accomplish this?

question from:https://stackoverflow.com/questions/65936457/debugging-a-specific-subprocess

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...