Search code examples
c++debuggingshellgdb

Use GDB to debug a C++ program called from a shell script


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.

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


Solution

  • There are two options that you can do:

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

    2. 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.

    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.