How to debug a running C++ program in Linux?

c++, debugging, gdb

Solution

I can't find `-p` option in gdb man or their documentation, but it does work! I've tried it many times with older versions on RedHat and 7.0.1 on Debian.

I'm not sure how exactly it finds the exe by PID (maybe `/proc/<PID>/exe`), but it does. Since it's not described in their documentation, perhaps it not the most recommended way, but I haven't had any problems with it.

There's no noticeable difference between `gdb -p <PID>` and running gdb and in the their shell typing `attach <PID>`.

I personally prefer `ps xa| grep myprogram` for getting the PID

Problem

I have a question about debugging a running C++ program in Linux. If a programming is already running and can't be interrupted, how to do that. I can find three ways, but I don't know too much about details, I am grateful if any one can elaborate it deeper. 1) we can use GDB by specifying the process ID ``` gdb -p PID ``` In this case, what's the difference between this and `attach PID`? 2) We can use pstat, however, I am using Ubuntu, no pstat, but only mpstat it seems that mpstat does not provide too much information, and not so many options. 3) check the details information under directory ./proc In this case, just go to the directory with the PID. However, should this be done mannually?

Original source