How to attach gdb with running process

In this article we will discuss how to debug a running application.

Many time we need to debug a running application by attching debugger with process at runtime.

[showads ad=inside_post]

 

To attach the gdb with a running process, use the following command

gdb <PROCESS NAME> <PROCESS ID>

To search the process information like Process Id etc, use following command,

ps -elf | grep <Process Name>

For example,

we have a ruuning process “sample” and we need to attach the gdb debugger with it. SO, let’s serach the process info i.e.

ps -elf | grep sample

It will give the process info i.e. process id etc i.e.

0 S varun 2007 1989 87 80 ... 00:00:14 ./sample

Here Process id is “2007” and Process name is “sample”.

Now attach the gdb debugger with process using following command ,

gdb sample 2007

It will give gdb command prompt i.e.

Reading symbols from /lib64/ld-linux-x86-64.so.2...Reading symbols from /usr/lib/debug//lib/x86_64-linux-gnu/ld-2.21.so...done.
done.
__lll_lock_wait_private () at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:95
95 ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S: No such file or directory.
(gdb)

PS: Sometimes it requires root permission. So use “sudo” to run this command i.e.

sudo gdb sample 2007

 

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top