Tuesday, January 17, 2012

Gnu Not Unix Debugger

To load a compiler with GDB we need to compile it with specifying the GDB argument.  My Guru for this is Vivek Ramachand awesome guy :-)
bala@bala-laptop:~$ gcc -ggdb -o add add.c

To learn more about GDB we need to load it with GDB
bala@bala-laptop:~$ gdb ./add

We can List the source file with the list command.
(gdb) list 1
1    #include
2    int add(int x, int y)
3    {

Now to Run the program we need type run along with the parameter.
(gdb) run 3 4
Starting program: /home/bala/add 3 4
sum of 3+4 = 7
Program exited normally.


Disassemble a program we need to give the key word disassemble with the function name.
(gdb) disassemble main
Dump of assembler code for function main:
   0x00000000004006a5 <+47>:    add    $0x8,%rax
   0x00000000004006b4 <+62>:    callq  0x400548
   0x00000000004006c6 <+80>:    add    $0x10,%rax
   0x00000000004006d5 <+95>:    callq  0x400548

We can also disassemble the add function.
(gdb) disassemble add
Dump of assembler code for function add:
   0x0000000000400654 <+0>:    push   %rbp
   0x0000000000400655 <+1>:    mov    %rsp,%rbp
   0x0000000000400658 <+4>:    mov    %edi,-0x14(%rbp)
   0x000000000040065b <+7>:    mov    %esi,-0x18(%rbp)

Breakpoint can be set by giving the key word break and line number.
(gdb) break 5
Breakpoint 1 at 0x400665: file add.c, line 5.
If we need to know more about how to set breakpoint.
(gdb) help break
Set breakpoint at specified line or function.
LOCATION may be a line number, function name, or "*" and an address.

 (gdb) run 4 5
Starting program: /home/bala/add 4 5
Breakpoint 1, add (x=4, y=5) at add.c:5
5    z=x+y;

(gdb) print x
$1 = 4
(gdb) print y
$2 = 5

Registers information can be got by the command info registers
(gdb) info registers
rax            0x4    4
rbx            0x0    0
rcx            0x7ffff7b32500    140737349100800
rdx            0x5    5
rsi            0x5    5
rdi            0x4    4
rbp            0x7fffffffe230    0x7fffffffe230
rsp            0x7fffffffe230    0x7fffffffe230  - This is the STACK pointer
rip            0x400665    0x400665 - This is the instruction pointer
eflags         0x202    [ IF ]
cs             0x33    51
ss             0x2b    43
ds             0x0    0
es             0x0    0
fs             0x0    0
gs             0x0    0


STACK information can be got by x command,
(gdb) help x
Examine memory: x/FMT ADDRESS.
ADDRESS is an expression for the memory address to examine.
FMT is a repeat count followed by a format letter and a size letter.
Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal), t(binary), f(float), a(address), i(instruction), c(char) and s(string). Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes). The specified number of objects of the specified size are printed according to the format. Defaults for format and size letters are those previously used.
Default count is 1.  Default address is following last thing printed with this command or "print".

(gdb) x/10xb 0x7fffffffe230
0x7fffffffe230:    0xe0    0xe2    0xff    0xff    0xff    0x7f    0x00    0x00
0x7fffffffe238:    0x0d    0x07

(gdb) We are generally interested in word (4 bytes) as push and pop happens here
(gdb) x/20xw 0x7fffffffe230
0x7fffffffe230:    0xffffe2e0    0x00007fff    0x0040070d    0x00000000
0x7fffffffe240:    0xffffe3c8    0x00007fff    0x005657f0    0x00000003
0x7fffffffe250:    0x000000bf    0x00000000    0x00000005    0x00000004

Steping into a function
(gdb) s
6    return z;
(gdb) list 4
1    #include
2    int add(int x, int y)
3    {
4    int z=10;
5    z=x+y;
6    return z;
7    }
8    main(int argc, char **argv)
9    {
10    int a = atoi(argv[1]);

(gdb) s
7    }
(gdb) s
main (argc=3, argv=0x7fffffffe3c8) at add.c:20
20    printf("sum of %d+%d = %d\n",a,b,c);
(gdb) s
sum of 4+5 = 9
21    exit(0);

(gdb) s
Program exited normally.

Continue can be used to run the complete program after it hit a breakpoint 
(gdb) continue
sum of 7+8 = 15
Program exited normally.


Remove Breakpoint
(gdb) info b - listing all the breakpoint in a code
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000400665 in add at add.c:5
    breakpoint already hit 1 time
(gdb) clear 5 - clearing breakpoint set by us.
Deleted breakpoints 1
(gdb) info b
No breakpoints or watchpoints.

No comments:

Post a Comment