Monday, January 28, 2013

How to use gdb debugger ? (Debugging with GDB)

Steps to use gdb :
1) Compile your source code (xyz.c file) using -g option as follows
$ gcc -g xyz.c

2) Execute your ./a.out file as follows
$ gdb ./a.out

3) you will get gdb command line environment and then to start debugging you need to run the program.So, before running ,you need to set the breakpoint to start debug.
gdb$ b xyz.c.15  
Here 15 indicates the line number in your source code file.

4) After setting the breakpoint ,run the program as follows
gdb$ r
If the program needs command line arguments,then enter
gdb$ r arg1 arg2......so on

5) Now ,it starts execution and stops at the given breakpoint.

6) Next,you can run the program line by line as follows
gdb$ n 
or
gdb$ s
Here ,option s also works similar to n(next line),but if you want to trace the program completely(including functions),then use "s" option.

7) Whenever you get segmentation fault,you can actually trace back from where this fault came by "bt"(backtrack) option.
gdb$ bt

8) And also ,you can print the value of any variable at any stage using "p" (print ) option as follows.
For example,if you have a variable like 'i'
gdb$ p i
or
gdb$ print i

There are lot more options which you can try similarly,for more information about options,you can refer to man page of gdb
$ man gdb

thank you for reading !!!
cheers !
sGk

No comments:

Post a Comment