Debugging with gdb

GDB is the GNU project debugger which can be very helpful when you’re trying to figure out what goes wrong in the application you’re developing. With gdb, you can start a program, specify points or conditions where the program’s execution will be stopped so that you can examine its current condition, and experiment correcting values to discover bugs that maybe lurking in your code. gdb’s online documentation can be found here.

To use gdb, you need to compile your program with the -g option to include debugging information. For instance, using gcc, you can compile program myprog.c as follows:

[user] gcc -g myprog.c

To start gdb, run gdb program in the command prompt, where program is the name of your executable file. For instance,

[user] gdb a.out

First, this will display gdb’s front material describing its copyright and non-warranty, among other information. Then you will be put in gdb prompt (gdb).

Start the program by issuing the command run in the (gdb) prompt. If you have not yet issued any break points, the program will continue running until it finishes or it encounters fatal error. To quit gdb, just issue the quit command.

The main purpose of gdb is to allow programmers to stop the program before it terminates so that its current condition can be examined and potential sources of trouble can be known. This is where breakpoints, catchpoints, and watchpoints become handy. A breakpoint makes your program stops whenever a certain point is reached. On the other hand, a watchpoint is a special breakpoint that stops the program when the value of an expression changes. A catchpoint is another kind of breakpoint that stops the program when a certain kind of event occurs.

To setup a breakpoint, use the break command followed by the location. The location can be a function name, a line number, or an address of an instruction. For example,

(gdb) break 10

will set a breakpoint at line number 10, and

(gdb) break myfunc

will set a breakpoint at the start of function myfunc.

To examine your source file within gdb, you can use the list command. list prints 10 lines by default from the source file. It also prints the line numbers which you can use in the break command. Some of the useful list options are as follows:

(gdb) list linenum

will print lines centered around linenum.

(gdb) list startline, numlines

will print numlines starting from startline in the current source file.

(gdb) list function

will print lines centered around the beginning of function

(gdb) list

will print the next 10 lines.

After a break, you can continue the program execution using the continue or c command. You can also run the program until the next line is reached with the step or s command. To step by machine execution instead of source line, use the stepi or si command. The next or n command will execute the next line including any function call. To execute just the next machine instruction, use nexti or ni.

The above commands should keep you started. If you need help, just type help in the (gdb) prompt. You can further refine help by appending any command after help, such as

(gdb) help break

That’s it! And happy debugging.

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.