would like to know best guide or document for gdb for different architectures x86 , power pc etc..
would like to understand how to debug segmentation faults because of stack corruption ..
understand utilities ELF , objdump etc..
please guide me
You debug "stack faults" by seeing what's happening before the fault. It's difficult to make sense of the stack fault itself, for the same reason a museum is more useful before it burns down.
Register information isn't too useful without knowing what's in those registers. That's why gdb likes to have debugging information -- to tell it how to get variables, not just random cryptic registers.
An example using gdb debugging a crash fault:
$ cat gdb.c
#include <stdio.h>
#include <string.h>
int main(void) {
char *strings[]={"a","B"}; // What happens when we forget to make element 3 NULL?
char buf[64];
int n;
for(n=0; strings[n] != NULL; n++)
{
char *val=strings[n];
strcpy(buf, "123:");
strcat(buf, val);
printf("%s\n", buf);
}
}
$ gcc -ggdb gdb.c
$ ./a.out
123:a
123:B
123:B
Segmentation fault
$ gdb ./a.out
# It's not running yet. Set a breakpoint so it doesn't run, crash,
# and quit before we can see anything.
(gdb) break main
Breakpoint 1 at 0x400592: file gdb.c, line 5.
(gdb) run
Starting program: /home/tyler/code/c/1shot/a.out
warning: Could not load shared library symbols for linux-vdso.so.1.
Do you need "set solib-search-path" or "set sysroot"?
Breakpoint 1, main () at gdb.c:5
5 char *strings[]={"a","B"};
# Run it one step at a time, to see line numbers.
(gdb) step
9 for(n=0; strings[n] != NULL; n++)
(gdb) step
11 char *val=strings[n];
# Let's stop at this line every loop.
(gdb) break gdb.c:11
Breakpoint 2 at 0x4005ab: file gdb.c, line 11.
# What registers the relevant values are in can vary.
# That's why gdb needs debugging info -- so you can ask it for
# VARIABLES.
(gdb) print strings[n]
$1 = 0x4006a4 "a"
# That looks ok. That won't crash. Let it run.
(gdb) continue
Continuing.
123:a
Breakpoint 2, main () at gdb.c:11
11 char *val=strings[n];
(gdb) print strings[n]
$2 = 0x4006a6 "B"
# Still OK.
(gdb) continue
Continuing.
123:B
Breakpoint 2, main () at gdb.c:11
11 char *val=strings[n];
(gdb) print strings[n]
$3 = 0x4006a6 "B"
# Um. The array has two elements, but this is the third loop?
# This value "looks ok" but is actually garbage.
# You cannot tell this without knowing the program of course...
(gdb) continue
Continuing.
123:B
Breakpoint 2, main () at gdb.c:11
11 char *val=strings[n];
(gdb) print strings[n]
# We are now two elements beyond the end of the array.
# The first just "happened" to work by coincidence, but THIS WILL
# crash, and this is why:
$4 = 0x300000000 <error: Cannot access memory at address 0x300000000>
(gdb) continue
Continuing.
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7ab6360 in ?? () from /lib64/libc.so.6
(gdb) quit
A debugging session is active.
Inferior 1 [process 9589] will be killed.
Quit anyway? (y or n) y
$
Debugging a crash without finding the context around it isn't too useful, you have to see what happens BEFORE the crash.