Debugging is one of the most important, and often most easily skimmed over, concepts in programming. We should first define what we mean by bug. The term "bug" may have its origins in engineering of the late 1800s; however, it entered mainstream use in computer science when a moth actually flew into an early computer and shorted one of the circuits. Debugging referred to literally removing the moth. The term bug has come to mean anything that goes wrong in a computing system (whether it is hardware or software). Compiler errors are not bugs because they occur before the program runs. In fact, compiler errors often help the programmer prevent bugs. Unhandled runtime errors and design errors are bugs. (Note we use the term "unhandled" error. This is because some errors at runtime are "caught" and "handled" by the program itself. Error handling is a more advanced topic that will be covered in Unit 2.) As we mentioned in the previous section, runtime errors and design errors are often hard to diagnose. Debugging tools are present in most modern programming environments. The best way to debug a program is to set a breakpoint somewhere near the code you want to debug. A breakpoint tells the debugger to execute the program until it reaches a specified line and then to pause the program so its current state (e.g., values in variables) can be examined. To set a breakpoint in most programming environments, simply click the left hand margin of the editor. To try this, copy the following code into the Workspace template in the editor and set a breakpoint on Line 7:
{$APPTYPE CONSOLE}
program Workspace;
var
Dividend,Divisor : Integer;
begin
{$I Debug.inc}
Writeln('This program will divide two integers.');
Write('Please enter the dividend: ');
Readln(Dividend);
Write('Please enter the divisor: ');
Readln(Divisor);
Writeln(Dividend,' divided by ',Divisor,' equals ',
Dividend div Divisor,' and ',Dividend mod Divisor,'/',Divisor);
Readln;
end.
Your editor should now look like this:
Note the line {$I Debug.inc} . This is a workaround for a bug in the Lazarus environment. Do not set a breakpoint above this line or the debugger will not work properly. If you are using Delphi then this line is not necessary and can be removed. To run the program press F9 (if you are using the {$I Debug.inc} fix then press enter once after the program starts). You will notice that it pauses at the breakpoint:
Now press F8. This is the "Step Over" command. It executes a single line of code and stops at the next line.
Using breakpoints and the Step Over command you can step through each line of code and watch it as it executes. Now continue pressing F8 until the program returns to the prompt. You will notice that the focus returns back to the program since it is waiting for user input. Now enter 5 for the dividend. As soon as you entered the number the debugger stopped on the next line. Now click the editor and hover the mouse cursor over the identifier Dividend on line 9. After a few seconds a box should pop up showing that the value 5 is stored in Dividend.
Now say we want to stop execution of the program and edit it. All you have to do is press Ctrl+F2 or Ctrl+F9 followed by Enter (the former stops the execution immediately and the latter triggers a compile which means that execution must be stopped first). Although learning to use the F-keys will speed up the write-compile-debug cycle, all of these commands could have also been accessed from the Run menu. Throughout the next several chapters we will make use of the debugging tools.