debug a currently running program with GDB

April 9th, 2007 mysurface Posted in Developer, gcc, gdb | Hits: 45807 | 7 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.

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

  1. […] 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.

  3. I seldom leave a response, but i did some searching
    and wound up here debug a currently running program with GDB
    ? Linux by Examples. And I actually do have 2 questions for you if you usually
    do not mind. Could it be simply me or does it look like a
    few of these responses look like coming from brain dead folks?
    :-P And, if you are writing at other sites, I would like to follow everything new
    you have to post. Could you list of all of all your community sites like your linkedin profile, Facebook page or twitter feed?

  4. Yes, PeeDee, 9d would have worked much better if it had said ‘A Constable’s condition’ but it didn’t.

  5. What’s up, I desire to subscribe for this blog to get most recent updates, thus where can i do it please help out.

  6. What’s up to every body, it’s my first pay a visit of this website; this website contains awesome and
    in fact fine information for visitors.

  7. Wow, amazing weblog layout! How lengthy have you been running a blog
    for? you make blogging look easy. The whole look of your web site is magnificent, as neatly as the content material!

Leave a Reply