GDB

GNU Debugger (GDB)

Compiling
Compile the code with symbolic debugging information included, gcc with -g switch.

$ gcc -g hello.c -o hello
$ g++ -g hello.cpp -o hello

$ gcc -g hello.c -o hello
$ gdb hello
GNU gdb (GDB) Fedora (6.8.50.20090302-21.fc11)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i586-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
(gdb) r
Starting program: /home/Deepak/Documents/hello
Hello World

Program exited normally.
Missing separate debuginfos, use: debuginfo-install glibc-2.10.1-2.i686
(gdb)
(gdb) q
$



Start in GUI mode using gdb -tui
$ gdb -tui hello
   ┌──hello.c──────────────────────────────────────────────────────────────────┐
   │1       #include<stdio.h>                                                  │
   │2                                                                          │
   │3       int main()                                                         │
   │4       {                                                                  │
B+>│5         printf("Hello World \n");                                        │
   │6         return 0;                                                        │
   │7       }                                                                  │
   │8                                                                          │
   │9                                                                          │
   │10                                                                         │
   │11                                                                         │
   │12                                                                         │
   │13                                                                         │
   └───────────────────────────────────────────────────────────────────────────┘
child process 4492 In: main                            Line: 5    PC: 0x80483bd
(gdb) list hello.c:1
(gdb) break hello.c:5
Breakpoint 1 at    0x80483bd: file    hello.c, line 5.
(gdb) r
Starting program: /home/Deepak/Documents/hello

Breakpoint 1, main () at hello.c:5
(gdb)

Now, notice that we passed the name of the executable on the command line. Another option you have is to just start gdb with nothing else on the command line, then give it the command file hello, and that will cause the executable "hello" to be loaded up.

Command line arguments!
(gdb) run arg1 arg2

First, before you issue the run command, you need to set a breakpoint someplace you'd like to stop. You use the break or b command, and specify a location, which can be a function name, a line number, or a source file and line number.

If you're running in dumb terminal mode, gdb will print the line it will execute next. If you're running in cool GUI mode, the line it will execute next will be highlighted in the source window.
break mainBreak at the beginning of the main() function
break 5Break at line 5 of the current file
break hello.c:5Break at line 5 of hello.c

To list the current breakpoints, use the info command, like so: "info breakpoints" (or the shorter "i b"):
To clear a breakpoint, use the clear command with the breakpoint location. You can also clear a breakpoint by number with the delete command.
Additionally, you can enable or disable breakpoints, though these two commands take a breakpoint number as an argument, not a location! The enabled/disabled status of a breakpoint is visible under the "Enb" column in the breakpoint listing.

# gdb hello
GNU gdb (GDB) Fedora (6.8.50.20090302-21.fc11)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i586-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
(gdb) b main
Breakpoint 1 at 0x80483bd: file hello.c, line 5.
(gdb) b hello.c:6
Breakpoint 2 at 0x80483c9: file hello.c, line 6.
(gdb) i b
Num     Type           Disp Enb Address    What
1       breakpoint     keep y   0x080483bd in main at hello.c:5
2       breakpoint     keep y   0x080483c9 in main at hello.c:6
(gdb) disable 1
(gdb) i b
Num     Type           Disp Enb Address    What
1       breakpoint     keep n   0x080483bd in main at hello.c:5
2       breakpoint     keep y   0x080483c9 in main at hello.c:6
(gdb) clear main
Deleted breakpoint 1
(gdb) i b
Num     Type           Disp Enb Address    What
2       breakpoint     keep y   0x080483c9 in main at hello.c:6
(gdb) delete 2
(gdb) i b
No breakpoints or watchpoints.
(gdb) delete 1
No breakpoint number 1.


Core Dump Analysis
info thread
Thread n (switch to nth thread)

$ gcc -g hello.c -o hello
$ ./hello
Segmentation fault
$ ulimit -c unlimited
$ ./hello
Segmentation fault (core dumped)
$ dir
core.2568  hello  hello.c
$ gdb hello core.2568
GNU gdb (GDB) Fedora (6.8.50.20090302-21.fc11)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i586-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...

warning: Can't read pathname for load map: Input/output error.
Reading symbols from /lib/libc-2.10.1.so...Reading symbols from /usr/lib/debug/lib/libc-2.10.1.so.debug...done.
done.
Loaded symbols for /lib/libc-2.10.1.so
Reading symbols from /lib/ld-2.10.1.so...Reading symbols from /usr/lib/debug/lib/ld-2.10.1.so.debug...done.
done.
Loaded symbols for /lib/ld-2.10.1.so
Core was generated by `./hello'.
Program terminated with signal 11, Segmentation fault.
#0  0x080483d9 in main () at hello.c:6
6      *ptr = 1234;

(gdb) bt
#0  0x080483d9 in main () at hello.c:6
(gdb) info registers
eax            0x0    0
ecx            0x77fee063    2013192291
edx            0x1    1
ebx            0xa81ff4    11018228
esp            0xbfb81f30    0xbfb81f30
ebp            0xbfb81f58    0xbfb81f58
esi            0x0    0
edi            0x0    0
eip            0x80483d9    0x80483d9 <main+21>
eflags         0x210286    [ PF SF IF RF ID ]
cs             0x73    115
ss             0x7b    123
ds             0x7b    123
es             0x7b    123
fs             0x0    0
gs             0x33    51
(gdb) info all-registers
eax            0x0    0
ecx            0x77fee063    2013192291
edx            0x1    1
ebx            0xa81ff4    11018228
esp            0xbfb81f30    0xbfb81f30
ebp            0xbfb81f58    0xbfb81f58
esi            0x0    0
edi            0x0    0
eip            0x80483d9    0x80483d9 <main+21>
eflags         0x210286    [ PF SF IF RF ID ]
cs             0x73    115
ss             0x7b    123
ds             0x7b    123
es             0x7b    123
fs             0x0    0
gs             0x33    51
st0            0    (raw 0x00000000000000000000)
st1            0    (raw 0x00000000000000000000)
st2            0    (raw 0x00000000000000000000)
st3            0    (raw 0x00000000000000000000)
st4            0    (raw 0x00000000000000000000)
st5            0    (raw 0x00000000000000000000)
st6            0    (raw 0x00000000000000000000)
---Type <return> to continue, or q <return> to quit---
st7            0    (raw 0x00000000000000000000)
fctrl          0x0    0
fstat          0x0    0
ftag           0x0    0
fiseg          0x0    0
fioff          0x0    0
foseg          0x0    0
fooff          0x0    0
fop            0x0    0
xmm0           {v4_float = {0x0, 0x0, 0x0, 0x0}, v2_double = {0x0, 0x0},
  v16_int8 = {0x0 <repeats 16 times>}, v8_int16 = {0x0, 0x0, 0x0, 0x0, 0x0,
    0x0, 0x0, 0x0}, v4_int32 = {0x0, 0x0, 0x0, 0x0}, v2_int64 = {0x0, 0x0},
  uint128 = 0x00000000000000000000000000000000}
xmm1           {v4_float = {0x0, 0x0, 0x0, 0x0}, v2_double = {0x0, 0x0},
  v16_int8 = {0x0 <repeats 16 times>}, v8_int16 = {0x0, 0x0, 0x0, 0x0, 0x0,
    0x0, 0x0, 0x0}, v4_int32 = {0x0, 0x0, 0x0, 0x0}, v2_int64 = {0x0, 0x0},
  uint128 = 0x00000000000000000000000000000000}
xmm2           {v4_float = {0x0, 0x0, 0x0, 0x0}, v2_double = {0x0, 0x0},
  v16_int8 = {0x0 <repeats 16 times>}, v8_int16 = {0x0, 0x0, 0x0, 0x0, 0x0,
    0x0, 0x0, 0x0}, v4_int32 = {0x0, 0x0, 0x0, 0x0}, v2_int64 = {0x0, 0x0},
  uint128 = 0x00000000000000000000000000000000}
xmm3           {v4_float = {0x0, 0x0, 0x0, 0x0}, v2_double = {0x0, 0x0},
  v16_int8 = {0x0 <repeats 16 times>}, v8_int16 = {0x0, 0x0, 0x0, 0x0, 0x0,
---Type <return> to continue, or q <return> to quit---q
0x0,Quit
(gdb) bt
#0  0x080483d9 in main () at hello.c:6
(gdb) backtrace
#0  0x080483d9 in main () at hello.c:6
(gdb)


http://betterexplained.com/articles/debugging-with-gdb/ 
Comments