The program below sums prices from the command line and prints their mean. Run it with no prices and it prints count: 0, then an unhandled exception at the hex address $000000000040120A, and exits with status 217. Nothing in that output names a line of your source. Here is the program.
program average;
{ Sums the prices given on the command line and prints the count and the mean. }
uses SysUtils;
var
i, count: integer;
sum: integer;
begin
sum := 0;
count := ParamCount;
for i := 1 to count do
sum := sum + StrToInt(ParamStr(i));
writeln('count: ', count);
writeln('mean: ', sum div count);
end.
Key takeaways
- An unhandled exception prints a hex address;
-glmakes FPC addmain, line 17 of average.pasto it. - gdb stops the program where it breaks and prints the variables, which a line number alone cannot tell you.
- In FPC's default mode
integeris 2 bytes, so six prices that add to 62500 leave the variable at -3036. sum: longintand/with:0:1fix both the wrapped sum and the truncated mean.-Crturns the silent wrap into a range check error that names the line;-Coalone did not catch it on this build.- A checker that reads the program's output turns a wrong number nobody noticed into a failing line.
The address is not a line number
Compile it the usual way and run it with no arguments. This is the default build.
$ fpc -vw average.pas && ./average
count: 0
mean: An unhandled exception occurred at $000000000040120A:
EDivByZero: Division by zero
$000000000040120A
The shell reports exit status 217. The message says something divided by zero, but the address after it is only a position inside the compiled binary. You cannot open it in your editor.
One flag fixes this. With -gl, FPC prints the line next to the address.
$ fpc -vw -gl average.pas && ./average
count: 0
mean: An unhandled exception occurred at $000000000040120A:
EDivByZero: Division by zero
$000000000040120A main, line 17 of average.pas
Now the last line reads main, line 17 of average.pas. Open the file and look at line 17. The only division there is sum div count, and count is 0 when you pass no prices.
Let gdb stop it where it breaks
A line number tells you where the program died, not what the variables held. For that, run the program inside gdb, which halts it at the moment of failure.
Keep the -gl build from the last step. The option -ex run starts the program, and -ex bt prints a backtrace once it stops.
$ gdb --batch -ex run -ex bt ./average
Program received signal SIGFPE, Arithmetic exception.
0x000000000040120a in main () at average.pas:17
17 writeln('mean: ', sum div count);
#0 0x000000000040120a in main () at average.pas:17
Read it from the top. The first line names the signal, SIGFPE, which matches the EDivByZero message you saw before. The second gives the address, the routine main, and the place average.pas:17. The third prints the source line itself, so you do not even need to open the file.
The last line is the backtrace: the chain of routine calls that led to the stop, newest first. Here the chain has a single entry, main.
Stop it one line earlier and look inside
Now give the program real prices and stop it before line 17 runs. The command break 17 sets a breakpoint, a line where gdb pauses before executing it. The option --args hands the prices to your program. Each print shows the current value of an expression.
$ gdb --batch -ex 'break 17' -ex run -ex 'print sum' -ex 'print count' -ex 'print sum div count' --args ./average 12000 15000 9500 10500 8000 7500
Breakpoint 1 at 0x4011d2: file average.pas, line 17.
Breakpoint 1, main () at average.pas:17
17 writeln('mean: ', sum div count);
$1 = -3036
$2 = 6
$3 = -506
gdb stopped before the division ran, and it hands you the values. count is 6, which is right. sum is -3036, and sum div count would give -506. Six positive prices cannot add up to a negative number.
The sum that went negative
Run the same prices without gdb to see what a user sees.
$ fpc -vw average.pas && ./average 12000 15000 9500 10500 8000 7500
count: 6
mean: -506
The exit status is 0. The program does not crash, and it prints a wrong answer.
The cause is the type of sum. In FPC's default mode, integer takes 2 bytes. Ask the compiler with a second small program.
program sizes;
begin
writeln('sizeof(integer) = ', sizeof(integer));
writeln('high(integer) = ', high(integer));
writeln('sizeof(longint) = ', sizeof(longint));
end.
$ fpc -vw sizes.pas && ./sizes
sizeof(integer) = 2
high(integer) = 32767
sizeof(longint) = 4
The largest integer is 32767. The prices are 12000, 15000, 9500, 10500, 8000 and 7500, and they add to 62500. That does not fit, so the variable wrapped around: 62500 minus 65536 leaves -3036. Dividing by 6 gives -506. Compile the same file with -Mobjfpc and integer becomes 4 bytes, with sizeof(integer) = 4 and high(integer) = 2147483647.
The fix is to declare sum: longint. The mean then prints with / instead of div, and :0:1 gives one digit after the point. A test on count makes the no-price run say mean: no data. The full file is in the last section.
Make the silent version stop too
The wrong run exited with status 0, and nobody was told. You can ask FPC to stop on a value that does not fit. The option -Cr turns on range checking. Build the buggy program with it and keep -gl for the line.
$ fpc -vw -gl -Cr average.pas && ./average 12000 15000 9500 10500 8000 7500
An unhandled exception occurred at $00000000004011A0:
ERangeError: Range check error
$00000000004011A0 main, line 15 of average.pas
The run now ends with status 217 and names line 15, which is sum := sum + StrToInt(ParamStr(i));. That is the line where the sum wraps. The option -Co alone did not catch it on this build: the run still printed count: 6 and mean: -506 with no error. Use -Cr.
A check you can run
This program reads the two lines average prints and compares them with the expected answer.
program check_average;
{ Reads the lines average prints and compares them with the expected answer. }
const
expected: array[1..2] of string = ('count: 6', 'mean: 10416.7');
var
line: string;
i, passed: integer;
begin
passed := 0;
i := 0;
while (not eof) and (i < 2) do
begin
readln(line);
i := i + 1;
if line = expected[i] then
begin
passed := passed + 1;
writeln('PASS line ', i, ': ', line);
end
else
writeln('FAIL line ', i, ': expected ', expected[i], ', got ', line);
end;
writeln;
writeln(passed, ' of 2 checks passed');
if passed <> 2 then
ExitCode := 1;
end.
With the buggy source still in place, compile both files and pipe the program's output into the checker.
$ fpc -vw average.pas && fpc -vw check_average.pas && ./average 12000 15000 9500 10500 8000 7500 | ./check_average
PASS line 1: count: 6
FAIL line 2: expected mean: 10416.7, got mean: -506
1 of 2 checks passed
That is the red run. Line 2 fails, and check_average exits with status 1. Now replace average.pas with the fixed source.
program average;
{ Sums the prices given on the command line and prints the count and the mean. }
uses SysUtils;
var
i, count: integer;
sum: longint;
begin
sum := 0;
count := ParamCount;
for i := 1 to count do
sum := sum + StrToInt(ParamStr(i));
writeln('count: ', count);
if count = 0 then
writeln('mean: no data')
else
writeln('mean: ', sum / count:0:1);
end.
Run the same command again.
$ fpc -vw average.pas && ./average 12000 15000 9500 10500 8000 7500 | ./check_average
PASS line 1: count: 6
PASS line 2: mean: 10416.7
2 of 2 checks passed
That is the green run, with exit status 0. The run with no prices, the one that started all this, no longer crashes.
$ fpc -vw average.pas && ./average
count: 0
mean: no data
Rebuild with -gl first when a program stops at a bare hex address, and set a breakpoint one line before the calculation when it exits 0 with the wrong number.