Steps Introduction
- Finding the Process ID: First, identify the Process ID (PID) of the program to debug. Commands such as
ps,top, orpgrepcan locate the PID. For example, to find the PID of a program namedmyapp, use:
bashpgrep myapp
- Launching GDB for Debugging: After obtaining the PID, launch GDB and attach to the process using the following command:
bashgdb -p [PID]
Where [PID] should be replaced with the actual process ID.
Example
Assume a running program named myapp with PID 1234. The following steps demonstrate debugging this process:
- First, determine the process ID:
bashpgrep myapp
Output might be:
shell1234
- Next, launch GDB and attach to the process using:
bashgdb -p 1234
- At this point, GDB will attach to the process with PID 1234. In the GDB command prompt, you can perform various debugging operations, such as setting breakpoints and inspecting variable values.
Notes
- Ensure sufficient permissions to attach to the process. For other processes or specific system processes, sudo privileges may be required.
- The process will pause execution during debugging; confirm this is acceptable for production applications.