# Debugging

### Debugging

"Debugging" refers to the process of finding and fixing errors, or "bugs" in a program. The term comes from Admiral Grace Hopper who, while working on an early computer in the 1940s found a moth stuck in the computer. Now bugs are rarely caused by actual insects, but by errors in our code.

A "debugger" is a program that assists us in the task of debugging. Today we will look at two debuggers for C++, gdb and ddd.

### Compiling For Debugging

A debugger works by reading extra information in your program. This extra information, also called "debug symbols" is not put into our programs by default. In order to make clang++ add this in, we must compile our code with the "-g" option:


$clang++ -g file.cpp -o file  Now the executable "file" is ready to be debugged! ### Using gdb To start debugging, run gdb with the name of your program  gdb file Then you can give gdb various commands to run and investigate your program: • run - runs your program • break location - sets a breakpoint at a given location. The location can be a function name, or a line number. For example "break Pixel::setRed" or "break grayscale.cpp:20". A breakpoint is a place in your program where the debugger will stop excuting when it reaches • step - steps through your program one line at a time • next - like step except it does not enter new functions as they are called • print E - prints E where E is an expression (usually just a variable name) • list - show the code around where the debugger is • continue - continues executing until another breakpoint is reached • delete num - remove breakpoint number num. • delete - removes all breakpoints • where - prints a backtrace, which is a list of the functions that were executed in order • quit - leaves the debugger gdb commands also support tab-completion which is handy when setting breakpoints. With gdb you can step through your program and display values while the program runs. When a program crashes, gdb is especially useful. Run the program through the debugger, and it will stop just before crashing so you can see what line of code caused the problem, what values were in any variables, and so on. You can try debugging the segfaults in this example. ### Using ddd Because gdb is somewhat awkward to use, several front-ends have been developed for it. A front-end is a program that provides a nicer interface to another program. ddd is a GUI program, so it can only be run on the actual lab machines*. We start ddd the same way as gdb: $ ddd file


Using ddd is just like gdb, except it also provides a GUI to use the features.

*it is actually possible to run GUI programs remotely, but is hard to setup, and really slow.

### Exercise

To practice using the debugger, debug this program. This code produces a segfault if the user enters a non-negative input.

• Run the debugger on this code and see what line it segfaults on.
• Print the value of the pointer.
• You can also use breakpoints and steps to see where the value of the pointer becomes corrupted.