| 1 | <!doctype html> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | <meta charset="utf-8"> |
| 5 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 6 | <title>When it goes wrong - Raz, Set, Go</title> |
| 7 | <meta name="description" content="raz: a small systems language in Wirth's tradition, for x86-64 Linux."> |
| 8 | <meta name="theme-color" content="#E07A5F"> |
| 9 | <meta property="og:title" content="When it goes wrong - Raz, Set, Go"> |
| 10 | <meta property="og:description" content="A small systems language for x86-64 Linux. No run time, no GC, one report."> |
| 11 | <meta property="og:image" content="https://raz.lucela.org/social.png"> |
| 12 | <meta name="twitter:card" content="summary_large_image"> |
| 13 | <link rel="icon" href="../favicon.svg" type="image/svg+xml"> |
| 14 | <link rel="icon" href="../favicon-32.png" type="image/png" sizes="32x32"> |
| 15 | <link rel="apple-touch-icon" href="../apple-touch-icon.png"> |
| 16 | <link rel="stylesheet" href="../style.css"> |
| 17 | </head> |
| 18 | <body> |
| 19 | <header><nav><a class="mark" href="../index.html"><img src="../logo.svg" width="26" height="26" alt="">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">More Raz</a><a href="../spec.html">Report</a><a href="../source/index.html">Source</a><a href="../index.html#download">Download</a></nav></header> |
| 20 | <main> |
| 21 | <div class="tour"><aside><h2>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'> |
| 22 | <li><a href="hello.html">Hello, world</a></li> |
| 23 | <li><a href="modules.html">Modules</a></li> |
| 24 | <li><a href="integers.html">Integers</a></li> |
| 25 | <li><a href="values.html">Values and conversions</a></li> |
| 26 | <li><a href="procedures.html">Procedures and several results</a></li> |
| 27 | </ol> |
| 28 | <h3>Set</h3><ol start='6'> |
| 29 | <li><a href="slices.html">Arrays, slices and strings</a></li> |
| 30 | <li><a href="records.html">Records and pointers</a></li> |
| 31 | <li><a href="variants.html">Enumerations, sets, variants and match</a></li> |
| 32 | <li><a href="memory.html">Memory and allocators</a></li> |
| 33 | </ol> |
| 34 | <h3>Go</h3><ol start='10'> |
| 35 | <li><a href="errors.html">Errors</a></li> |
| 36 | <li><a href="defer.html">Defer</a></li> |
| 37 | <li><a href="checks.html" aria-current="page">When it goes wrong</a></li> |
| 38 | <li><a href="generics.html">Generic modules</a></li> |
| 39 | <li><a href="c.html">Talking to C</a></li> |
| 40 | <li><a href="threads.html">Threads</a></li> |
| 41 | </ol></aside><article><p class="kicker">Go · lesson 12 of 15</p><h1>When it goes wrong</h1> |
| 42 | <div class="file"><span>Fail.raz</span></div><pre class="code"><code><span class="kw">module</span> Fail; |
| 43 | <span class="kw">import</span> Out; |
| 44 | |
| 45 | <span class="kw">proc</span> show(a: []<span class="ty">INT</span>; i: <span class="ty">INT</span>); |
| 46 | <span class="kw">begin</span> |
| 47 | Out.int(a[i]); |
| 48 | Out.ln; |
| 49 | <span class="kw">end</span>; |
| 50 | |
| 51 | <span class="kw">proc</span> main*; |
| 52 | <span class="kw">var</span> a: [<span class="number">3</span>]<span class="ty">INT</span>; i: <span class="ty">INT</span>; |
| 53 | <span class="kw">begin</span> |
| 54 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">3</span> <span class="kw">do</span> |
| 55 | show(a, i); |
| 56 | <span class="kw">end</span>; |
| 57 | <span class="kw">end</span>; |
| 58 | |
| 59 | <span class="kw">end</span> Fail.</code></pre> |
| 60 | <pre class="output"><code>0 |
| 61 | 0 |
| 62 | 0 |
| 63 | <span class="err">Fail.raz:6:15: index 3 is out of range for a length of 3 |
| 64 | in Fail.show at Fail.raz:6:15: i = 3 |
| 65 | in Fail.main at Fail.raz:14:9: i = 3 |
| 66 | </span></code></pre> |
| 67 | <p>Every 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 <code>0</code>s are what it printed before the fourth pass went one past the end.</p> |
| 68 | <h2>Reading the message</h2> |
| 69 | <p>The first line is the place of the offending expression: file, line and column, then what failed and the numbers involved. Each <code>in</code> 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.</p> |
| 70 | <h2>Other failures</h2> |
| 71 | <p>The same shape of message tells of a <code>nil</code> 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.</p> |
| 72 | <div class="file"><span>Nilp.raz</span></div><pre class="code"><code><span class="kw">module</span> Nilp; |
| 73 | <span class="kw">import</span> Out; |
| 74 | <span class="kw">import</span> SYSTEM; |
| 75 | |
| 76 | <span class="kw">type</span> Node = <span class="kw">record</span> val: <span class="ty">INT</span>; next: ^Node; <span class="kw">end</span>; |
| 77 | |
| 78 | <span class="kw">proc</span> last(n: ^Node): <span class="ty">INT</span>; |
| 79 | <span class="kw">begin</span> |
| 80 | <span class="kw">while</span> n^.next # <span class="ty">nil</span> <span class="kw">do</span> n := n^.next; <span class="kw">end</span>; |
| 81 | <span class="kw">return</span> n^.val; |
| 82 | <span class="kw">end</span>; |
| 83 | |
| 84 | <span class="kw">proc</span> main*; |
| 85 | <span class="kw">var</span> a, b: Node; |
| 86 | <span class="kw">begin</span> |
| 87 | a.val := <span class="number">1</span>; b.val := <span class="number">2</span>; |
| 88 | a.next := SYSTEM.<span class="ty">ADR</span>(b); |
| 89 | Out.int(last(SYSTEM.<span class="ty">ADR</span>(a))); Out.ln; |
| 90 | Out.int(last(<span class="ty">nil</span>)); Out.ln; |
| 91 | <span class="kw">end</span>; |
| 92 | |
| 93 | <span class="kw">end</span> Nilp.</code></pre> |
| 94 | <pre class="output"><code>2 |
| 95 | <span class="err">Nilp.raz:9:11: stopped by a nil pointer |
| 96 | in Nilp.last at Nilp.raz:9:11: n = nil |
| 97 | in Nilp.main at Nilp.raz:19:5 |
| 98 | </span></code></pre> |
| 99 | <div class="file"><span>Zero.raz</span></div><pre class="code"><code><span class="kw">module</span> Zero; |
| 100 | <span class="kw">import</span> Out; |
| 101 | |
| 102 | <span class="kw">proc</span> ratio(a, b: <span class="ty">INT</span>): <span class="ty">INT</span>; |
| 103 | <span class="kw">begin</span> |
| 104 | <span class="kw">return</span> a <span class="kw">div</span> b; |
| 105 | <span class="kw">end</span>; |
| 106 | |
| 107 | <span class="kw">proc</span> main*; |
| 108 | <span class="kw">var</span> n: <span class="ty">INT</span>; |
| 109 | <span class="kw">begin</span> |
| 110 | Out.int(ratio(<span class="number">10</span>, <span class="number">3</span>)); Out.ln; |
| 111 | Out.int(ratio(<span class="number">10</span>, n)); Out.ln; <span class="comment">(* n is zero: every variable starts at zero *)</span> |
| 112 | <span class="kw">end</span>; |
| 113 | |
| 114 | <span class="kw">end</span> Zero.</code></pre> |
| 115 | <pre class="output"><code>3 |
| 116 | <span class="err">Zero.raz:13:5: stopped by a division fault |
| 117 | in Zero.main at Zero.raz:13:5: n = 0 |
| 118 | </span></code></pre> |
| 119 | <div class="file"><span>Slicing.raz</span></div><pre class="code"><code><span class="kw">module</span> Slicing; |
| 120 | <span class="kw">import</span> Out; |
| 121 | |
| 122 | <span class="kw">proc</span> main*; |
| 123 | <span class="kw">var</span> a: [<span class="number">5</span>]<span class="ty">INT</span>; s: []<span class="ty">INT</span>; hi: <span class="ty">INT</span>; |
| 124 | <span class="kw">begin</span> |
| 125 | hi := <span class="number">7</span>; |
| 126 | s := a[<span class="number">2</span>..<span class="number">5</span>]; |
| 127 | Out.int(<span class="ty">LEN</span>(s)); Out.ln; |
| 128 | s := a[<span class="number">2</span>..hi]; <span class="comment">(* past the end *)</span> |
| 129 | Out.int(<span class="ty">LEN</span>(s)); Out.ln; |
| 130 | <span class="kw">end</span>; |
| 131 | |
| 132 | <span class="kw">end</span> Slicing.</code></pre> |
| 133 | <pre class="output"><code>3 |
| 134 | <span class="err">Slicing.raz:10:12: slice 2..7 is out of range for a length of 5 |
| 135 | in Slicing.main at Slicing.raz:10:12: hi = 7 |
| 136 | </span></code></pre> |
| 137 | <p>In every case the output before the failure was written first. <code>Out</code>'s buffer is flushed by the failure, so what the program printed is never lost to the crash.</p> |
| 138 | <h2>A constant can fail at compile time</h2> |
| 139 | <p>A 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.</p> |
| 140 | <div class="file"><span>Early.raz</span></div><pre class="code"><code><span class="kw">module</span> Early; |
| 141 | <span class="kw">import</span> Out; |
| 142 | |
| 143 | <span class="kw">proc</span> main*; |
| 144 | <span class="kw">var</span> a: [<span class="number">3</span>]<span class="ty">INT</span>; |
| 145 | <span class="kw">begin</span> |
| 146 | a[<span class="number">3</span>] := <span class="number">1</span>; |
| 147 | Out.int(<span class="number">10</span> <span class="kw">div</span> <span class="number">0</span>); Out.ln; |
| 148 | <span class="kw">end</span>; |
| 149 | |
| 150 | <span class="kw">end</span> Early.</code></pre> |
| 151 | <pre class="output term"><code><span class="cmd">$ raz Early.raz</span> |
| 152 | Early.raz:7:7: error: index 3 is out of range for a length of 3 |
| 153 | 7 | a[3] := 1; |
| 154 | | ^ |
| 155 | Early.raz:8:13: error: division by zero |
| 156 | 8 | Out.int(10 div 0); Out.ln; |
| 157 | | ^^^^^^^^ |
| 158 | raz: 2 errors</code></pre> |
| 159 | <h2>Looking at where you are</h2> |
| 160 | <p><code>Rt.trace</code> 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.</p> |
| 161 | <div class="file"><span>Where.raz</span></div><pre class="code"><code><span class="kw">module</span> Where; |
| 162 | <span class="kw">import</span> Rt; |
| 163 | |
| 164 | <span class="kw">proc</span> inner(depth: <span class="ty">INT</span>); |
| 165 | <span class="kw">begin</span> |
| 166 | <span class="kw">if</span> depth = <span class="number">0</span> <span class="kw">then</span> Rt.trace; <span class="kw">return</span>; <span class="kw">end</span>; |
| 167 | inner(depth - <span class="number">1</span>); |
| 168 | <span class="kw">end</span>; |
| 169 | |
| 170 | <span class="kw">proc</span> main*; |
| 171 | <span class="kw">begin</span> |
| 172 | inner(<span class="number">2</span>); |
| 173 | <span class="kw">end</span>; |
| 174 | |
| 175 | <span class="kw">end</span> Where.</code></pre> |
| 176 | <pre class="output"><code><span class="err"> in Where.inner at Where.raz:6:23: depth = 0 |
| 177 | in Where.inner at Where.raz:7:5: depth = 1 |
| 178 | and 1 more the same |
| 179 | in Where.main at Where.raz:12:5 |
| 180 | </span></code></pre> |
| 181 | <h2>Leaving the checks out</h2> |
| 182 | <p>A module that is sure of its indexes may say <code>check off;</code> 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 (<code>nil</code> and division), and callers of the module keep theirs. Use it for a measured inner loop, never as a first step.</p> |
| 183 | <h2>For gdb and the profiler</h2> |
| 184 | <p>The program carries names and line numbers, so <code>gdb</code> can break at <code>Fail.raz:6</code> and show raz source. <code>RAZ_PROFILE=1</code> in the environment makes a program sample itself every millisecond and say at the end where its time went, by procedure and by line.</p><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="defer.html">← Defer</a><a href="index.html">Contents</a><a class="next" href="generics.html">Generic modules →</a></div></article></div> |
| 185 | </main> |
| 186 | <footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer> |
| 187 | </body> |
| 188 | </html> |