Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GDB attaching to a process where executable is deleted

Tags:

linux

gdb

I have running process but it's executable file has got deleted. If I try to attach gdb I got following error

/home/vivek/binary/releases/20120328101511/bin/app.exe (deleted): No such file or directory.

How can I attach gdb to this process ?

Sample Test case: Source code:

#include<stdio.h>
#include<stdlib.h>
int main(){
  for (;;){
    printf("Sleeping");
    sleep(1);
  }
}

compile it

 gcc main.cc -o a.out
 gcc main.cc -o b.out

Run ./a.out

Now from different terminal delete a.out. And fire gdb attach pgrep a.out file b.out It doesn't work.

GDB shows following error:

/tmp/temp/a.out (deleted): No such file or directory.
A program is being debugged already.  Kill it? (y or n) n
Program not killed.
like image 405
Vivek Goel Avatar asked Apr 02 '12 09:04

Vivek Goel


People also ask

Can GDB attach to a running process?

With GDB it is always possible to debug a running process by attaching to it. It is possible to debug a DLL this way. The limitation of this approach is that the DLL must run long enough to perform the attach operation.

How to detach a process from GDB?

When you have finished debugging the attached process, you can use the detach command to release it from GDB control. Detaching the process continues its execution. After the detach command, that process and GDB become completely independent once more, and you are ready to attach another process or start one with run .


2 Answers

Try using /proc/<pid>/exe as the executable. It appears as a symbolic link these days, however, in the past it was possible to extract the deleted executable from it.

See Detecting deleted executables.

We can use following command to attach gdb

gdb <path-to-binary> <pid>
like image 159
Maxim Egorushkin Avatar answered Sep 22 '22 15:09

Maxim Egorushkin


You can't. GDB needs the symbol data that's in the executable and is not being loaded by the OS when running the program.

like image 43
littleadv Avatar answered Sep 21 '22 15:09

littleadv