raz / site / content / tour / 12-checks.txt

131 lines · 3.3 KiB · raw · history · last change a5145f64

1title: When it goes wrong
2part: Go
3summary: Every index is checked, a failure says where and with what values, and how to read it.
4---
5```raz Fail fail
6```
7
8Every index and slice is checked. A failed check says where it failed and with what values, then the calls under way with their variables, and ends the program with status 2. The three `0`s are what it printed before the fourth pass went one past the end.
9
10## Reading the message
11
12The first line is the place of the offending expression: file, line and column, then what failed and the numbers involved. Each `in` line after it is a call still under way, innermost first, with the basic-typed variables it held. That is usually enough to see the bug without a debugger.
13
14## Other failures
15
16The same shape of message tells of a `nil` pointer, a division by zero, a bad slice and a stack overflow. Each of them stops the program with status 2, and never runs on with a wrong value.
17
18```raz Nilp fail
19module Nilp;
20import Out;
21import SYSTEM;
22
23type Node = record val: INT; next: ^Node; end;
24
25proc last(n: ^Node): INT;
26begin
27 while n^.next # nil do n := n^.next; end;
28 return n^.val;
29end;
30
31proc main*;
32var a, b: Node;
33begin
34 a.val := 1; b.val := 2;
35 a.next := SYSTEM.ADR(b);
36 Out.int(last(SYSTEM.ADR(a))); Out.ln;
37 Out.int(last(nil)); Out.ln;
38end;
39
40end Nilp.
41```
42
43```raz Zero fail
44module Zero;
45import Out;
46
47proc ratio(a, b: INT): INT;
48begin
49 return a div b;
50end;
51
52proc main*;
53var n: INT;
54begin
55 Out.int(ratio(10, 3)); Out.ln;
56 Out.int(ratio(10, n)); Out.ln; (* n is zero: every variable starts at zero *)
57end;
58
59end Zero.
60```
61
62```raz Slicing fail
63module Slicing;
64import Out;
65
66proc main*;
67var a: [5]INT; s: []INT; hi: INT;
68begin
69 hi := 7;
70 s := a[2..5];
71 Out.int(LEN(s)); Out.ln;
72 s := a[2..hi]; (* past the end *)
73 Out.int(LEN(s)); Out.ln;
74end;
75
76end Slicing.
77```
78
79In every case the output before the failure was written first. `Out`'s buffer is flushed by the failure, so what the program printed is never lost to the crash.
80
81## A constant can fail at compile time
82
83A division by a zero constant, or a constant index out of range, is not a run-time failure at all; the compiler says it at the line, so you never ship it.
84
85```raz Early
86module Early;
87import Out;
88
89proc main*;
90var a: [3]INT;
91begin
92 a[3] := 1;
93 Out.int(10 div 0); Out.ln;
94end;
95
96end Early.
97```
98
99```sh
100$ raz Early.raz
101```
102
103## Looking at where you are
104
105`Rt.trace` prints the calls under way, as a failure does, whenever you ask. It writes to standard error and the program goes on. It is the cheapest debugging tool there is.
106
107```raz Where run
108module Where;
109import Rt;
110
111proc inner(depth: INT);
112begin
113 if depth = 0 then Rt.trace; return; end;
114 inner(depth - 1);
115end;
116
117proc main*;
118begin
119 inner(2);
120end;
121
122end Where.
123```
124
125## Leaving the checks out
126
127A module that is sure of its indexes may say `check off;` after its imports and links, for speed. The module's own indexing and slicing is then unchecked, so an index out of range reads or writes whatever it reaches. Every other check stays (`nil` and division), and callers of the module keep theirs. Use it for a measured inner loop, never as a first step.
128
129## For gdb and the profiler
130
131The program carries names and line numbers, so `gdb` can break at `Fail.raz:6` and show raz source. `RAZ_PROFILE=1` in the environment makes a program sample itself every millisecond and say at the end where its time went, by procedure and by line.