GDB (GNU Debugger) is one the most popular symbol debuggers available and used on Unix/Linux platform. GDB provides the user with the provision of inspecting and modifying the data and control flow of a program. Using GDB a programmer can execute a program in controlled environment. This means that a programmer can execute the program instruction by instruction, can inspect the values of any variable at any instant, find out whether the if statement has failed, which function is called and with what paramaters and much much more. In a way its just like viewing a movie in slow motion. But also you can change the background, change the actors and their dialogues.
In a way, GDB is a tool for programmers to dissect a program and find out what lies inside. Though GDB is a debugger (which means a tool for removing a bug) but often you would find programmers using GDB even if there is no bug in program. GDB is also very helpful in determining the flow of code. Often in big projects it is often easier to use GDB and trace through the flow of program rather than opening the source files and finding which function call which and how.
GDB (and almost every other symbol debugger) provides the following functionalities:
- Changing and Inspecting Program's Control Flow
- Changing and Inspecting Program's Data
Changing and Inspecting Program's Control Flow
GDB allows a programmer to trace through each and every instruction of the program one by one. GDB can be configured to stop the program's execution at a specific point in the program. When the program execution has stopped the programmer can determine the chain of function calls which have been made to reach at that point or execute instruction one at a time. Also the programmer can execute a function call. For all this GDB has the following features:
- Stopping the program's execution using Breakpoint
- Executing one statement at a time using Single Step Execution feature
- Examining the chain of function calls using Backtrace
- Examining the data of calling functions using Frames
Changing and Inspecting Program's Data
In addition to stooping the program's execution at specific point, GDB also allows to inspect the data (i.e. variables and memory location ) of program. For doing this GDB has the provision for:
- Reading and Modifying the contents of a variable
- Checking at which point a particular variable is read/written using Watchpoint
Should I learn how to use a Debugger ?
The answer is YES. If you don't know how to use a debugger then consider yourself missing a hand or foot. If you know your code too well then you might be able to do away with the debugger for a while, but then you would have to work with someone's else code as well. There would be times when the program crashes at customer's site or in the testing department and they might simply provide you with a core file or memory dump. You would not be able to make sense out of this unless you know how to use a debugger.
Should I learn how to use GDB ?
Its better if you know how to use GDB. Simply because the GDB is one the most popular debuggers available for Unix/Solaris (thanks to Free Software Foundation). Also GDB (interface and commands) are also being adopted for other platforms as well such as Tornado provides a GDB interface for VxWorks environment. Once you know how to work with GDB it becomes very easy to work with other Visual debuggers provided for these platforms.
Being a command line debugger GDB might look a little tough to learn at first but once you get a hang of it, its same as playing Duke Nukem 3D in God mode.
Creating Debug Build: Compiling your programs for GDB