使用 GDB 调试从 shell 脚本调用的 C++ 程序

2022-01-20 00:00:00 shell debugging gdb c++

我有一个极其复杂的 shell 脚本,它在其中调用一个我想通过 GDB 调试的 C++ 程序.将这个 c++ 程序与 shell 分开是非常困难的,因为它有很多分支和很多环境变量设置.

I have a extremely complicated shell script, within which it calls a C++ program I want to debug via GDB. It is extremely hard to separate this c++ program from the shell since it has a lot of branches and a lot of environmental variables setting.

有没有办法在这个 shell 脚本上调用 GDB?看起来 gdb 需要我直接调用 C++ 程序.

Is there a way to invoke GDB on this shell script? Looks like gdb requires me to call on a C++ program directly.

推荐答案

有两种选择:

  1. 直接在 shell 脚本中调用 GDB.这意味着您没有重定向标准输入和标准输出.

  1. Invoke GDB directly within the shell script. This would imply that you don't have standard in and standard out redirected.

运行 shell 脚本,然后将调试器附加到已经运行的 C++ 进程,如下所示: gdb progname 1234 其中 1234 是正在运行的进程 IDC++ 进程.

Run the shell script and then attach the debugger to the already running C++ process like so: gdb progname 1234 where 1234 is the process ID of the running C++ process.

如果您需要在程序开始运行之前做一些事情,那么选项 1 会是更好的选择,否则选项 2 是更简洁的方式.

If you need to do things before the program starts running then option 1 would be the better choice, otherwise option 2 is the cleaner way.

相关文章