#include <stdio.h>
#include <signal.h>
static volatile sig_atomic_t being_debugged = 1;
static void int3_handler(int signo) { being_debugged = 0; }
int main()
{
signal(SIGTRAP, int3_handler);
__asm__ __volatile__("int3");
if (being_debugged) {
puts("No, I don't want to serve you.");
while (1) {
/* endless loop */ ;
}
}
puts("Yes, real routines go here.");
return 0;
}
The above will give different output when run inside/outside gdb,because gdb captures the sigtrap signal.
How to make my program behaves the same in gdb?
GDB will stop the inferior (being debugged) program when the inferior receives any signal.
If you simply continue
from GDB, the signal will be "swallowed", which is not what you want.
You can ask GDB to continue the program and send it a signal with signal SIGTRAP
.
You can also ask GDB to pass a given signal directly to the inferior, and not stop at all with handle SIGTRAP nostop noprint pass
GDB command. You'll need to do that before you hit the first SIGTRAP
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With