Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling signals with gdb

I'm debugging a C++ app for Ubuntu 10.04 that sometimes receives a SIGKILL signal. I want to catch the signal and stop it from killing the execution, just to see if I can get some useful info of the app's state at that precise moment.

Reading the gdb documentation I found the handle command, so I tried to apply it to the SIGKILL signal:

(gdb) handle SIGKILL stop nopass
Signal        Stop  Print   Pass to program Description
SIGKILL       Yes   Yes     No              Killed

So, as I understand this correctly:

stop
    GDB should stop your program when this signal happens. This implies the print keyword as well. 
print
    GDB should print a message when this signal happens. 
nopass
    GDB should not allow your program to see this signal. 

once the SIGKILL signal is emitted, gdb should somehow catch it, print the message, stop the execution and don't let the app kill itself, right?

The problem is that this doesn't happen and the app terminates.

Do you know how could I catch the signal?

Useful Info:

  • The piece of code that is running when the signal is emitted is executed in another thread.
  • gdb version: 4.4.3
  • g++ version: 7.1
like image 613
Adri C.S. Avatar asked May 23 '14 10:05

Adri C.S.


People also ask

Does GDB use ptrace?

ptrace is used by debuggers (such as gdb and dbx), by tracing tools like strace and ltrace, and by code coverage tools. ptrace is also used by specialized programs to patch running programs, to avoid unfixed bugs or to overcome security features.

Can you use GDB for C++?

For C and C++ programs, gdb and ddd are debuggers that you can use.

What does Ctrl C do in GDB?

If you hit Ctrl-C now, the program will terminate because GDB sends the SIGINT to the program as we specified.

Can you attach GDB 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.


1 Answers

From unix signal(7) man page:

  The  signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.

So the debugger can set the handler but that doesn't make any sense. The OS directly performs the needed action. If SIGKILL could be handled from application the OS has no chance to terminate a broken application. For that reason SIGKILL is a bit special :-)

like image 155
Klaus Avatar answered Sep 27 '22 21:09

Klaus