Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find signal handler function using GDB?

Tags:

c

signals

gdb

I am working on a C project that has bunch of files. Now I want to find the signal handler functions, but no success while surfing the project tree.

The first way I think about approaching this problem is running the binary with GDB.

Is there a way I can ask GDB to break as soon as a signal (e.g. Ctrl-C) is received?

like image 271
bagrat Avatar asked Oct 14 '25 23:10

bagrat


1 Answers

Here is some reference:

http://nirbhay.in/2012/09/debug-signal-handlers-using-gdb/

Let's do some experiment with this program:

/*
  @file : sig.c
*/

#include <stdio.h>
#include <signal.h>
#include <unistd.h>

void signalhandler(int signum) {
  printf("\n SIGINT caught : %d", signum);
}

int main() {
  signal(SIGINT, signalhandler);

  while (1) {
    printf("\n looping : inside main()");
    sleep(1);
  }
}

in this case you can do this way:

(gdb) handle SIGINT stop pass

after that, you step forward to get the signal handler function. Here I got:

$ gdb ./a

...

(gdb) handle SIGINT stop pass
SIGINT is used by the debugger.
Are you sure you want to change it? (y or n) y
Signal        Stop  Print   Pass to program Description
SIGINT        Yes   Yes Yes     Interrupt
(gdb) r
Starting program: /home/arc/a 

 looping : inside main()
 looping : inside main()
^C
Program received signal SIGINT, Interrupt.
0x00007ffff7aef900 in __nanosleep_nocancel () from /usr/lib/libc.so.6
(gdb) s
Single stepping until exit from function __nanosleep_nocancel,
which has no line number information.
0x0000000000400596 in signalhandler(int) ()
like image 180
ibrohimislam Avatar answered Oct 17 '25 13:10

ibrohimislam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!