debug a currently running program with GDB

April 9th, 2007 mysurface Posted in Developer, gcc, gdb | Hits: 31966 | 2 Comments »

Here is a quick tips of GDB. There is a time when I was working on a module that was written in c/c++ hangs in the middle of execution. I have no clue how it happens, and it happens quite rare. I keep guessing and try to feed in more debug print lines to search for the cause of the hangs. Spend few days with no luck, but suddenly I thought of GDB.

Instead of initiate a program in GDB, I can ask GDB to debug the currently running program by giving it the process ID. As simple as the line bellow,

gdb -p 12456

After that, just type where in GDB to show where it hangs at.

Of cause, if you wanna track the lines in specific source code that actually trigger the error, you should compile your code by specified -g for more debug info.

gcc -o myprog{,.c} -g

How to check the currently running program’s process ID?

pgrep myprog

More tips of GDB?
Check out Some tips of GDB.

More examples of gcc?
Check out Compile c and c++ source code.

2 Responses to “debug a currently running program with GDB”

  1. [...] linux.byexamples.com uses aizatto’s related post. [...]

  2. The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought youd have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention.

Leave a Reply