|
A debugger allows you to run your program with extra capabilities that allow you to pause your program, inspect where it is and see the values of variables. After that, if you want to, you can let the program continue from the point where you paused it.
For example, if you suspect that your program is in an infinite loop or infinite recursion, run it using a debugger. Then stop it (using a control-C for gdb) and take a look at where it is.
We will see memory faults later. A debugger will pause the program automatically if an event such as a memory fault occurs.
A debugger is an excellent tool for localizing and diagnosing certain kinds of errors. It is not a panacea, but it deserves a place in your toolbox.
An integrated development environment, such as Microsoft Visual Studio, Eclipse, XCode and many others, has a debugger built into it, and you can find out how to use it by reading the documentation for the development environment. Here, I describe a simple free-standing debugger called gdb that is available with Linux. You have seen gdb before for diagnosing infinite loops. This page gives more detail. But look at that prior page for an example.
First, before using gdb, compile your program with option -g. That will cause information for the debugger to be written into the machine language translation of your program.
After compiling your program, suppose that you have an executable file called myprog. Then issue Linux command
gdb ./myprogThe debugger will start and give you a prompt asking what you want to do. To run the program, write the command line that you would normally use, but replace the command name with run. For example, gdb command
run stuff.txt >output.txtruns your executable program as if run by command
./myprog stuff.txt >output.txtexcept that the debugger is in control of it.
Typing control-C will not stop the program, but only pause it. Then gdb will show a prompt, asking you what you want to do. Here is a short list of some commands that gdb allows.
This is short for backtrace. It shows
frames
of currently running functions, with one line
for each frame, ordered from most
recently created frames to least recently created.
If there are a lot of frames, gdb pauses after each pageful. Press a space to see the next page, or q to stop showing frames. |
up
You are always inspecting a single frame.
Initially, the current frame is the top one
(frame 0). Command up moves to the
next higher-numbered frame. Command
up 2 moves up two frames. Use
any positive integer after up, as in
up 3 |
down
Command down is similar to up, but it moves to a lower-numbered frame. |
print E
Show the value of expression E. Gdb knows most of the syntax that C++ allows for expressions, but do not use function calls. Variables in the expression can either be variables in the current frame or global variables. |
break fun
Pause the program each time it calls function fun. |
continue
Resume running the program after pausing it. You can use abbreviation cont. |
(empty line)
Just type the enter key, without a command, to repeat the previous command. |
help
Ask for help. You will get a list of topics. Command
help topicdescribes commands under the given topic. |
quit
Stop gdb (and the program being debugged). |
How do you start the gdb debugger on file ./hailstone? Answer
When using gdb, how do you stop your program if you suspect that it is in an infinite loop? Answer
When using gdb, how do you make the program pause whenever it enters function grape? Answer
When using gdb, how do you resume a program after it pauses? Answer
How do you exit gdb? Answer
|