Skip to content


Gdb Series – Attach gdb to a program that is already running – Part 1

Overview

This article describes how to attach gdb (GNU Project debugger) to a program that is already running using the gdb [1] command, which is a source-level debugger, capable of breaking programs at any specific line, displaying variable values, and determining where errors occurred. Currently, it works for C, C++, Fortran, Modula 2 and Java programs.

Step 2.1 Attach gdb to a program that is already running

This is useful for debugging processes that started up, but crashed when you perform a particular task.

Make sure the GNU Debugger is installed.

$ sudo apt-get install gdb

Find the process ID of <program>:

$ pidof <program>

Start gdb:

$ gdb 2>&1 | tee gdb-<program>.txt
(gdb) handle SIG33 pass nostop noprint
(gdb) set pagination 0
(gdb) attach <PID>

(If the program is running as root, use sudo gdb instead of just gdb above.)

Continue the <program>:

(gdb) continue

The program will continue running. Now you can perform any actions necessary to reproduce the crash. If the program hangs but doesn’t crash you can press ctrl+c in gdb while the program is frozen and then continue with the next step.

Step 2.2 Retrieve a backtrace

(gdb) backtrace full
(gdb) info registers
(gdb) x/16i $pc
(gdb) thread apply all backtrace
(gdb) quit

Attach the complete output from GDB, contained in gdb-<program>.txt, in your bug report or analyze the file accordingly.

Note that you can also set logging to a file like this:

(gdb) set logging file gdb-<program>.txt
(gdb) set logging on

References

• [1] – man gdb • https://wiki.ubuntu.com/Backtrace| Generating Backtraces

 

Posted in Knowledge Base.

Tagged with , , , , , , .


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.