Is there a way that we can print gdb output on segmentation fault from C program?
I mean, we just have to catch SIGSEGV and call the function with it right?
That way, if segfault occurs then I won't have to debug my code, it'll directly print the gdb output for me.
Thanks for the reply Bazzy.
Actually, what I want is that I should get the exact output of the gdb at segmentation fault instead of "Command terminated" or "Segmentation fault" as output on the terminal.
I was reading about the backtrace function. man 3 backtrace . Wrote a code to check out the output of backtrace but it's not similar to what gdb shows. Of-course it might be similar to the output of bt on gdb.
Reading symbols for shared libraries ++. done
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0xffffffff
0x00001dc9 in foo () at segcatch.cc:17
17 printf("%d\n",*foo);
Read up on sigaction() if your platform has it. You can get at least the address of the fault
and the EIP (instruction pointer) of the fault. It would be significantly non-trivial to try
to map the EIP back to a line number. You are better off using the addr2line utility (if
using GCC). Give addr2line an executable and an EIP and it will print out the source
file and line number of the EIP.