tinkerbeast
tinkerbeast

Reputation: 2077

How to tell if SIGSEGV is from data or code when debugging a core using gdb?

Consider the following pieces of code -

int x = NULL;
int y = *x;

And -

int (*x)() = NULL;
int y = x();

Both cases will raise a SIGSEGV. However, the first is due to data segment access and the second due to code segment access.

How do I know whether the SIGSEGV originated from code or data segment access when debugging the generated core file using gdb?

Also assume that I may not have the symbol files for the generated core.

Upvotes: -1

Views: 36

Answers (1)

Employed Russian
Employed Russian

Reputation: 213879

Running the two examples you provided shows obvious difference:

First example:

Core was generated by `./a.out'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x000055555b8f6139 in main () at t1.c:4
4         return *px;

Second example:

Core was generated by `./t2'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000000000000000 in ?? ()
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x0000562fd4631144 in main () at t2.c:4

In the second example, it's clear that we jumped to invalid code address.

In the first example, you could also look at the instruction which caused the fault:

(gdb) x/i $pc
=> 0x55555b8f6139 <main+16>:    mov    (%rax),%eax
(gdb) p/x $rax
$1 = 0x0

Upvotes: 1

Related Questions