Using gdb to see through your program flow!
Using GDB was something I wanted to learn since I started programming in GNU/Linux environment. I read through man pages, info pages, and some vague guides on the net over the weeks but couldn’t figure out why wasn’t I able to use it properly.
Then somehow I found this: RMS’s gdb Debugger Tutorial and learned that the I was making a very small mistake every time while compiling the code.
This post is a summary of what I learned and a little guide to use gdb - The GNU Debugger to debug your C code.
- First of all, here is the setup I am using which won’t make a big difference if you are using a similar setup:
* Programming Language: **C**
* Compiler: **gcc** - GNU project C and C++ compiler
* OS: **Ubuntu** (GNU/Linux) 10.04.1 32-bit
Let’s say we have a program called
source.c
ready created using your favorite editor (vim, emacs, gedit, etc).Compile the program using gcc but using the following syntax:
* `gcc -g source.c -o run_source`
* -g is used for producing debugging information. GDB can work with this debugging information. This was the place I was making mistake all the time :).
* -o is used to specify the name of binary to be generated by the compiler gcc which is used to run the program. In this case the binary's name is _run_source_
- Now, as you may be know, you can run the program using
./run_source
but here we want to debug it. So we use:
* `gdb run_source`
- Now we are in GDB environment. You can use the following commands to debug your program:
* `break <function name>` - to set a breakpoint at a function
* `watch <variable name>` - to watch the value of a variable and compare between old and new value
* `list` - to list out the code in proximity of current line
* `run` - to run the program or to initiate the debugging process
* `print <variable name>` - to see the value of a variable at the current state of program
* `s` or `step` - to step through the program line by line
* `n` or `next` - same as step but doesn't enter inside a function when a function call is encountered
* `set var <variable name>` - to change the value of a variable at run time
* `quit` - to quit the debugger environment
There are a lot more commands available for GDB, a sophisticated debugger. I have presented only what I found really useful and have learned to use. You can explore more by exploring the man page for gdb (man gdb
). Of course, you can also refer to RMS’s gdb Debugger Tutorial (linked above) or any other good guide to learn more.
Do tell if this was useful to you. :)
EDIT (2010-11-18): Found a really nice document explaining how to use gdb and other Unix Programming Tools: http://cslibrary.stanford.edu/107/
Plus if you are interested in learning pointer intensive coding (linked lists, binary trees, etc.), a really good resource is Stanford CS Ed Library. I learned a lot from this for my Program Design exam today.
EDIT (2011-01-04): For seeing the values of an array, the following syntax comes in handy: p *array@len