raz / commit a5915c27

commit
a5915c27e21418cc888dd221d7739a808b541f03
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
1d90e30f
Step 9d: the trap report

parse's Body notes each procedure's scalar parameters and variables
(NoteVar, chunk.pas): name, place in the frame, and VarKind, how Rt
shows it (signed, unsigned, BOOL, address, real; size; through the
slot's address for a var parameter). link's MakePlaces adds to the
places each live procedure's range and name and its variables; Rt
reads them. On a failed check, a code-tested stop or a signal, Rt
follows the rbp chain (callerFrame, framePtr) and says each call under
way, at most 20, with its variables (calls, putValue, putReal,
putHex); a stack overflow's first frame without them. Rt.trace says
the same on demand. Thread.start clears rbp in a new thread so the
chain ends there. ChunkName moves to chunk.pas, for link and the
listing.
Report 2026-10.12: sections 10 and 11 say so; Rt.trace. The tests'
expected standard error gains the calls; check.sh and conform/check.sh
compare it with addresses masked (0x?). test/libc/Through drops a
freed pointer. test/run/Trace.

patch · log

36 files changed, +542 -58

PLAN.md (history) modified +1 -1

@@ -152,7 +152,7 @@
152152 encoder knows and a few of the system's, bytes for the rest.
153153 - [x] 9c. `raz -S`: each line of source with its instructions, named
154154 by the encoder as it makes them, not by a disassembler.
155-- [ ] 9d. The trap report: on a failed check or a trap, the calls
155+- [x] 9d. The trap report: on a failed check or a trap, the calls
156156 under way, each with its variables by name and value, from
157157 tables the compiler writes; `Rt.trace` gives the same on
158158 demand.

README.md (history) modified +8 -1

@@ -94,7 +94,14 @@
9494 `Main.raz:12:9: index 7 is out of range for a length of 5`, and ends
9595 the program with status 2. A program stopped by a signal says where it
9696 was and why first: a `nil` pointer, a `nil` procedure, a stack
97-overflow, a bad address or a division fault.
97+overflow, a bad address or a division fault. Either then says the
98+calls under way, each with its variables:
99+
100+ Main.raz:12:9: index 7 is out of range for a length of 5
101+ in Main.get at Main.raz:12:9: i = 7, n = 5
102+ in Main.main at Main.raz:20:5: count = 3
103+
104+`Rt.trace` says the same at any time.
98105
99106 `raz run Main.raz a b` compiles the same way, then runs the program
100107 with the arguments `a b`, writing no file: its output and exit status

conform/check.sh (history) modified +4 -2

@@ -22,7 +22,8 @@
2222 # What a program says on standard error, the compiler's messages
2323 # included, is the implementation's own (appendix A). One that keeps
2424 # its text may give MESSAGES, a directory of X.stderr and X.err, the
25-# standard error of run/X and of compiling err/X, compared when there.
25+# standard error of run/X and of compiling err/X, compared when there,
26+# with each address it shows, 0x and hex digits, as 0x?.
2627
2728 noreals=
2829 if [ "$1" = -noreals ]; then noreals=1; shift; fi
@@ -55,7 +56,8 @@
5556 elif [ $status != $want ]; then
5657 echo "FAIL $f: status $status, not $want"
5758 fail=$((fail + 1))
58- elif [ -f $msg/$name.stderr ] && ! cmp -s $out/$name.err $msg/$name.stderr; then
59+ elif [ -f $msg/$name.stderr ] && ! sed 's/0x[0-9a-f]*/0x?/g' $out/$name.err |
60+ cmp -s - $msg/$name.stderr; then
5961 echo "FAIL $f: standard error differs"
6062 diff $out/$name.err $msg/$name.stderr | head -5
6163 fail=$((fail + 1))

doc/book/PLAN.md added +106 -0

@@ -0,0 +1,106 @@
1+# Stage: the book "Compiling Raz"
2+
3+This is the owner's go-ahead for a new stage: a book, "Compiling Raz",
4+explaining how the raz compiler works. Follow CLAUDE.md's way of working:
5+one commit per step, push it, then stop for review. Don't change the
6+language or the compiler for the book's sake. If the book exposes a bug
7+or a clumsy spot, report it rather than fixing it.
8+
9+## Read first
10+- CLAUDE.md, doc/raz.md (the report), doc/compiler.md, README.md.
11+- src/*.pas, the whole compiler (about 5,000 lines), and enough of lib/
12+ (Rt, Heap, Mem, Thread, Sys) to explain the runtime.
13+- The tiza skill, for drawings.
14+
15+## The reader
16+A working programmer: not good at Pascal, little knowledge of compilers.
17+- Assume they roughly know registers, the stack and memory. Explain only
18+ what raz does with them (frames, calling convention, layout).
19+- Introduce compiler ideas (tokens, grammar, generating code while
20+ parsing, symbol tables) briefly and in friendly terms where they first
21+ appear.
22+- Teach just enough Pascal to read the compiler: one early chapter, then
23+ more as features come up. Teach enough raz the same way.
24+- Keep the two languages visibly apart: ```raz and ```pascal code blocks,
25+ and say which one is on screen.
26+- Don't explain formats that can be looked up. ELF gets a page on what
27+ raz writes and why, plus a pointer to the spec.
28+- Target 40-50k words.
29+
30+## Tone
31+Like Learn You a Haskell and Starting FORTH: tiny steps, talking straight
32+to the reader, every idea shown at once on a real example, drawings that
33+carry meaning. Warm, with an occasional dry aside. Never a joke per
34+paragraph; humor is seasoning. Plain, short sentences.
35+
36+## Mascot: Razmataz
37+A small, very serious creature with a flashy name, which suits a frugal
38+compiler (razzmatazz minus one z).
39+- Its motto, "once is enough", plays on "raz" meaning "one" or "once" in
40+ Polish and Russian. Check that the compiler really works in one pass
41+ before leaning on this; if it doesn't, find the honest version of the
42+ joke.
43+- Razmataz appears in tiza drawings: eating characters in the scanner,
44+ stacking boxes in frames, throwing unused code overboard in linking.
45+
46+## Structure
47+Build up gradually, not unit by unit:
48+1. Introduction and a whirlwind tour: compile `exit 42` (or the smallest
49+ real program) through every stage, shallowly.
50+2. Enough Pascal (and raz) to follow along.
51+3. One chapter per language feature, followed through scanner, parser,
52+ symbols, code generator and machine code: variables and expressions,
53+ control flow, procedures and frames, records and arrays, pointers and
54+ the heap, strings, modules, defer, C interop, threads.
55+4. Closing chapters: x64 encoding, link (tree shaking, layout), elf,
56+ speed (how it reaches 400k lines a second), and where to go next.
57+
58+Refine this order in the outline (step 1).
59+
60+## Format and tooling
61+- Markdown in doc/book/: NN-slug.md per chapter, plus metadata.yaml
62+ (title, author Luis, cover). It must read well as plain text.
63+- `make book` builds build/compiling-raz.epub with pandoc. If pandoc gets
64+ installed locally, update the desktop guide (see the global
65+ CLAUDE.md).
66+- Drawings: .tiza source and a rendered PNG, both committed (CI has no
67+ tiza). Every image has a caption that stands on its own in plain text.
68+ Use ASCII art in code blocks for diagrams that are really structure
69+ (frames, memory layout); those read the same in both formats.
70+- Disassembly is cleaned up: instructions only, the raz source line
71+ beside them as a comment, and bytes only where the bytes are the point.
72+ Show raw objdump output once, early, next to the cleaned version, and
73+ say how to get it.
74+- A checker, written in raz alongside tools/Doc.raz (reuse what exists),
75+ run by `make check`. It must:
76+ - compile and run every raz example marked as runnable and compare the
77+ expected output written in the book;
78+ - confirm every quoted Pascal excerpt still matches src/, quoted by
79+ unit and routine name, never by line number;
80+ - regenerate or verify the cleaned disassembly.
81+ The goal: when the compiler changes, `make check` fails rather than
82+ the book going quietly wrong.
83+- CI (.github/workflows/): on a push to main that touches doc/book/**,
84+ src/** or lib/**, build the epub and replace the file on a standing
85+ prerelease tagged `book` (gh release upload --clobber), so the owner
86+ has a fixed URL to open on an iPad. Tagged releases also attach that
87+ version's epub.
88+
89+## Steps (each one is a commit, then stop for review)
90+1. The outline: doc/book/OUTLINE.md, chapter by chapter, with what each
91+ one teaches, its running example, and the drawings planned.
92+2. The tooling: doc/book/ skeleton, `make book`, the checker in
93+ `make check`, the CI job. One placeholder chapter proves the pipeline
94+ end to end.
95+3. Razmataz's design sheet (tiza) and one sample chapter, the procedures
96+ and frames one, to set tone and depth.
97+4. Then one chapter per step, in order.
98+
99+doc/compiler.md stays as it is: the short guide for people changing the
100+compiler. The book can point to it but never replaces it.
101+
102+## Left to the tooling step
103+- How the checker marks runnable examples and quoted excerpts inside
104+ Markdown.
105+- Whether the cleaned disassembly comes from the compiler's own line
106+ information (`make gdb` suggests it has some) or from objdump.

doc/compiler.md (history) modified +12 -6

@@ -11,12 +11,12 @@
1111 | `src/diag.pas` | 296 | 218 | Messages: positions, source excerpts, colour, counts |
1212 | `src/scan.pas` | 851 | 722 | Symbols, literals, comments, positions |
1313 | `src/sym.pas` | 596 | 497 | Types, objects, scopes, layout |
14-| `src/chunk.pas` | 244 | 192 | The compiled program: chunks, fixups, line notes |
15-| `src/x64.pas` | 1,039 | 877 | The instruction encoder, jumps within a procedure, the assembler |
14+| `src/chunk.pas` | 276 | 217 | The compiled program: chunks, fixups, line notes |
15+| `src/x64.pas` | 1,031 | 870 | The instruction encoder, jumps within a procedure, the assembler |
1616 | `src/gen.pas` | 1,321 | 1,039 | The stack machine, for x86-64: values, operations, checks, calls, frames |
1717 | `src/item.pas` | 1,318 | 1,100 | Items: types, constants, variables, operators, conversions, calls |
18-| `src/parse.pas` | 2,529 | 2,267 | Declarations, statements, expressions, built-ins, recovery, warnings |
19-| `src/link.pas` | 180 | 139 | Tree shaking, layout, patching, the places |
18+| `src/parse.pas` | 2,559 | 2,291 | Declarations, statements, expressions, built-ins, recovery, warnings |
19+| `src/link.pas` | 228 | 180 | Tree shaking, layout, patching, the places |
2020 | `src/elf.pas` | 424 | 351 | Static and dynamic ELF, symbol table, line numbers |
2121 | `src/raz.pas` | 195 | 147 | The command line |
2222
@@ -496,9 +496,15 @@
496496
497497 Between placing text and placing rodata, `MakePlaces` writes the
498498 places, a rodata chunk: for each line note in live code, its address,
499-line, column and file. `Rt` reads them, through `Rt.places`, which the
499+line, column and file; and for the trap report, each live procedure's
500+address range and name, and its variables, which `Body` (`parse.pas`)
501+notes with `NoteVar`: each one's name, its place in the frame and how
502+`Rt` shows it (`VarKind`). `Rt` reads them, through `Rt.places`, which the
500503 entry point sets, to say where a failed check or a signal was: the
501-place with the highest address up to the one it has. Its signal
504+place with the highest address up to the one it has. Then it follows
505+the chain of frames, each `rbp` pointing at its caller's, and says
506+each call under way and its variables (`calls`); `Rt.trace` does the
507+same on demand. A thread's first frame has `rbp` zero, which ends it. Its signal
502508 handlers, set in its body, run on a stack of their own, so that they
503509 can tell a stack overflow, and leave by returning to the faulting
504510 instruction with the handler reset, so that the signal ends the

doc/raz.md (history) modified +11 -3

@@ -1125,8 +1125,13 @@
11251125 ```
11261126 proc atExit*(p: proc): ERROR; (* Rt.Full if 32 are registered *)
11271127 proc exit*(status: INT);
1128+proc trace*; (* the calls under way, on standard error *)
11281129 ```
11291130
1131+`Rt.trace` says on standard error the calls under way, from the one
1132+that calls it out, each with its variables, as a program that stops
1133+does.
1134+
11301135 Only what can be reached from `main` and the module bodies goes into
11311136 the executable: unused procedures, variables and strings are left out.
11321137 The executable names its procedures and carries line numbers, so that
@@ -1144,7 +1149,8 @@
11441149 program, on every target. Before it stops, the program prints to
11451150 standard error where it was, as file, line and column, and why, as far
11461151 as it can tell: a `nil` pointer, a `nil` procedure, a stack overflow, a
1147-bad address or a division fault. Whether the machine catches these or
1152+bad address or a division fault; then the calls under way, each with
1153+its variables of a basic, enumeration, set or pointer type. Whether the machine catches these or
11481154 the compiled code checks for them is the implementation's choice
11491155 (appendix A); that they stop the program isn't.
11501156
@@ -1180,7 +1186,7 @@
11801186
11811187 | Module | What |
11821188 |---|---|
1183-| `Rt` | ending the program, `exit` and `atExit`; `stop`, which ends it with a message; what the compiler's checks call when they fail |
1189+| `Rt` | ending the program, `exit` and `atExit`; `stop`, which ends it with a message; `trace`, the calls under way; what the compiler's checks call when they fail |
11841190 | `Sys` | system calls: `open`, `openRead`, `create`, `read`, `write`, `seek`, `close`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `chmod`, `symlink`, `readLink`, `info`, `openDir`, `nextEntry`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `termSize`, `ready`; the environment, `env`, `workDir`, `setWorkDir`; programs, pipes and signals; time, `clock`, `now`, `sleep`; `random`; and sockets. One file per target, and `Sys.raz` over the C library for the others |
11851191 | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads |
11861192 | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C |
@@ -1735,7 +1741,9 @@
17351741 can't be changed; `+` joins string constants (section 4). `match`
17361742 takes an integer, with constants and ranges as labels (7.4).
17371743 - **2026-10.12.** A code procedure is written in the target's assembly
1738- language, not as bytes (section 8; appendix A).
1744+ language, not as bytes (section 8; appendix A). A program that stops
1745+ says the calls under way and their variables, and `Rt.trace` says
1746+ them on demand (sections 10 and 11).
17391747
17401748 ## Appendix A. What an implementation chooses
17411749

lib/Rt.raz (history) modified +163 -7

@@ -3,6 +3,9 @@
33 failures; and the handlers that say where the program was when a
44 signal stops it.
55 The compiler finds fail, exit, cExit and places by these names.
6+ After a failed check or a signal, and on demand with trace, Rt says
7+ the calls under way and their variables, from the frames' chain of
8+ rbp and the compiler's tables in places.
69 The handlers are Linux's on x86-64, set by system calls: what the
710 kernel hands them is laid out as it is there. *)
811 module Rt;
@@ -14,13 +17,22 @@
1417 SigFpe = 8;
1518 SigSegv = 11;
1619 Low = 10000H; (* no address below this is ever mapped *)
20+ Digits = "0123456789abcdef";
1721 (* SA_SIGINFO, SA_ONSTACK, SA_RESTORER and SA_RESETHAND *)
1822 Flags = 4H + 8000000H + 4000000H + 80000000H;
1923
2024 type
2125 (* The code from address at on comes from line and col of the
22- file whose path is at path in the paths *)
26+ file whose path is at path in the names *)
2327 Place = record at, line, col, path: U32; end;
28+ (* A procedure, from address start to stop, called name, and its
29+ count variables from first on *)
30+ Proc = record start, stop, name, first, count: U32; end;
31+ (* A variable of a procedure, at off from its frame, shown by kind:
32+ 1 a signed integer, 2 an unsigned one, 3 a BOOL, 4 an address,
33+ 5 a real; plus its size times 16, and 256 if the frame holds its
34+ address *)
35+ Local = record name: U32; off: I32; kind: U32; end;
2436 (* What rt_sigaction and sigaltstack take *)
2537 Action = record
2638 handler: proc(sig: INT; info, ctx: ^U8);
@@ -34,9 +46,10 @@
3446 atExits: [MaxAtExit]proc;
3547 numAtExit: INT;
3648 cExit: proc(status: I32); (* C's exit, set first thing in a program that uses C *)
37- (* Set first thing too: the number of places, where the program's
38- code starts and stops, the places, and the paths. The linker
39- makes them. *)
49+ (* Set first thing too: the numbers of places, where the program's
50+ code starts and stops, the numbers of procedures and variables;
51+ the places, procedures and variables, and the paths and names.
52+ The linker makes them. *)
4053 places: ^INT;
4154 line: [256]U8; (* the message being made *)
4255 lineLen: INT;
@@ -123,6 +136,12 @@
123136 lineLen := 0;
124137 end;
125138
139+(* The paths and names, after the places, procedures and variables *)
140+proc names: ^U8;
141+begin
142+ return ^U8(places + 5) + 16 * places[0] + 20 * places[3] + 12 * places[4];
143+end;
144+
126145 (* Puts where the code at address a comes from, as file:line:col: and
127146 a space, if a is in the program's code: the place with the highest
128147 address up to a. *)
@@ -131,13 +150,13 @@
131150 begin
132151 if (places = nil) or (a < UINT(places[1])) or (a >= UINT(places[2])) then return; end;
133152 n := places[0];
134- p := ^Place(places + 3);
153+ p := ^Place(places + 5);
135154 best := nil;
136155 for i := 0 to n - 1 do
137156 if (UINT(p[i].at) <= a) and ((best = nil) or (p[i].at > best^.at)) then best := p + i; end;
138157 end;
139158 if best = nil then return; end;
140- putStr(^U8(p + n) + INT(best^.path));
159+ putStr(names() + INT(best^.path));
141160 put(":");
142161 putInt(INT(best^.line));
143162 put(":");
@@ -145,6 +164,137 @@
145164 put(": ");
146165 end;
147166
167+(* The frame pointer of the procedure that calls it, which a code
168+ procedure leaves as it is *)
169+proc framePtr: UINT;
170+code
171+ mov rax, rbp;
172+ ret;
173+end framePtr;
174+
175+(* The frame of the procedure that called the one calling this *)
176+proc callerFrame: UINT;
177+var q: ^UINT;
178+begin
179+ q := ^UINT(framePtr()); (* callerFrame's own: its caller's frame is in it *)
180+ q := ^UINT(q^);
181+ return q^;
182+end;
183+
184+(* The procedure whose code holds address a, or nil *)
185+proc procAt(a: UINT): ^Proc;
186+var p: ^Proc; i: INT;
187+begin
188+ if (places = nil) or (a < UINT(places[1])) or (a >= UINT(places[2])) then return nil; end;
189+ p := ^Proc(^U8(places + 5) + 16 * places[0]);
190+ for i := 0 to places[3] - 1 do
191+ if (UINT(p[i].start) <= a) and (a < UINT(p[i].stop)) then return p + i; end;
192+ end;
193+ return nil;
194+end;
195+
196+proc putHex(u: UINT);
197+var buf: [16]U8; i: INT;
198+begin
199+ i := 16;
200+ repeat
201+ DEC(i);
202+ buf[i] := Digits[INT(u & 15)];
203+ u := u shr 4;
204+ until u = 0;
205+ put("0x");
206+ put(buf[i..16]);
207+end;
208+
209+(* x with six figures after the point, or as m e n beyond them *)
210+proc putReal(x: F64);
211+var e, i: INT; f: U64;
212+begin
213+ if x # x then put("nan"); return; end;
214+ if x < 0.0 then put("-"); x := -x; end;
215+ if (x > 1.0) and (x * 2.0 = x) then put("inf"); return; end;
216+ e := 0;
217+ while x >= 1.0E15 do x := x / 10.0; INC(e); end;
218+ while (x # 0.0) and (x < 0.001) do x := x * 10.0; DEC(e); end;
219+ f := U64(x * 1000000.0 + 0.5);
220+ putInt(INT(f div 1000000));
221+ put(".");
222+ for i := 1 to 6 do
223+ f := f mod 1000000 * 10;
224+ put(Digits[INT(f div 1000000)..INT(f div 1000000) + 1]);
225+ end;
226+ if e # 0 then put("e"); putInt(e); end;
227+end;
228+
229+(* The value of variable l of the frame at bp *)
230+proc putValue(bp: UINT; l: ^Local);
231+var a: ^U8; w: ^UINT; h: ^U16; s: ^U32; f: ^F32; d: ^F64; v: UINT; bits: INT;
232+begin
233+ a := ^U8(bp) + INT(l^.off);
234+ w := ^UINT(a);
235+ if l^.kind & 256 # 0 then a := ^U8(w^); w := ^UINT(a); end;
236+ h := ^U16(a); s := ^U32(a); f := ^F32(a); d := ^F64(a);
237+ bits := INT(l^.kind shr 4 & 15) * 8;
238+ match bits of
239+ | 8: v := UINT(a^);
240+ | 16: v := UINT(h^);
241+ | 32: v := UINT(s^);
242+ | else: v := w^;
243+ end;
244+ match l^.kind & 15 of
245+ | 1:
246+ if (bits < 64) and (v shr (bits - 1) # 0) then putInt(INT(v) - INT(1) shl bits);
247+ else putInt(INT(v));
248+ end;
249+ | 2: putInt(INT(v));
250+ | 3: if v # 0 then put("true"); else put("false"); end;
251+ | 4: if v = 0 then put("nil"); else putHex(v); end;
252+ | else: if bits = 32 then putReal(F64(f^)); else putReal(d^); end;
253+ end;
254+end;
255+
256+(* Says the calls under way, a line each, from the one at address ip
257+ whose frame is at bp: where each is, and its variables, unless vars
258+ is false for the first. At most 20; a frame that isn't the
259+ program's ends them. *)
260+proc calls(ip, bp: UINT; vars: BOOL);
261+var p: ^Proc; l: ^Local; i, n: INT; q: ^UINT;
262+begin
263+ for n := 1 to 20 do
264+ p := procAt(ip);
265+ if p = nil then return; end;
266+ put(" in ");
267+ putStr(names() + INT(p^.name));
268+ put(" at ");
269+ where(ip);
270+ l := ^Local(^U8(places + 5) + 16 * places[0] + 20 * places[3]) + INT(p^.first);
271+ if not vars or (p^.count = 0) then DEC(lineLen, 2); end; (* where's ": " *)
272+ for i := 0 to INT(p^.count) - 1 do
273+ if vars then
274+ if i > 0 then put(", "); end;
275+ putStr(names() + INT(l[i].name));
276+ put(" = ");
277+ putValue(bp, l + i);
278+ end;
279+ end;
280+ say;
281+ vars := true;
282+ q := ^UINT(bp);
283+ if (bp & 7 # 0) or (q[0] <= bp) then return; end;
284+ ip := q[1] - 1; (* the call, by its return address *)
285+ bp := q[0];
286+ end;
287+end;
288+
289+(* Says on standard error the calls under way, from the one that calls
290+ it, each with its variables, as after a failed check. *)
291+proc trace*;
292+var q: ^UINT;
293+begin
294+ q := ^UINT(framePtr());
295+ calls(q[1] - 1, q[0], true);
296+end;
297+
148298 (* A check failed in the code at address at: says where, and what
149299 failed, on standard error, then ends the program by exit with
150300 status 2. what is 0 for an index a and 1 for a slice a..b, of
@@ -166,6 +316,7 @@
166316 else put("a stack overflow");
167317 end;
168318 say;
319+ calls(at, callerFrame(), true);
169320 act := {}; (* the signal's default action *)
170321 SYSCALL(13, sig, ADR(act), 0, 8); (* rt_sigaction *)
171322 SYSCALL(62, SYSCALL(39), sig); (* kill(getpid(), sig) *)
@@ -189,6 +340,7 @@
189340 putInt(len);
190341 end;
191342 say;
343+ calls(at, callerFrame(), true);
192344 exit(2);
193345 end;
194346
@@ -205,8 +357,10 @@
205357 signal ends the program. info and ctx are the kernel's siginfo and
206358 ucontext. *)
207359 proc onSignal(sig: INT; info, ctx: ^U8);
208-var adr, sp, ip: UINT; q: ^UINT; why: []U8;
360+var adr, sp, ip, bp: UINT; q: ^UINT; why: []U8;
209361 begin
362+ q := ^UINT(ctx + 120); (* uc_mcontext's rbp *)
363+ bp := q^;
210364 q := ^UINT(info + 16); (* si_addr *)
211365 adr := q^;
212366 q := ^UINT(ctx + 160); (* uc_mcontext's rsp, then rip *)
@@ -225,6 +379,8 @@
225379 put("stopped by ");
226380 put(why);
227381 say;
382+ (* the variables of a frame the stack overflowed in may be past it *)
383+ calls(ip, bp, why # "a stack overflow");
228384 end;
229385
230386 begin

lib/Thread.linux-x86_64.raz (history) modified +1 -0

@@ -53,6 +53,7 @@
5353 jnz parent;
5454 pop rax; (* fn *)
5555 pop rdi; (* arg *)
56+ xor ebp, ebp; (* the first frame: Rt's report of the calls stops here *)
5657 call rax;
5758 xor edi, edi;
5859 mov eax, 60; (* exit, this thread only *)

src/chunk.pas modified +32 -0

@@ -44,6 +44,14 @@
4444 path, line, col: longint; { the file's path, interned, the line and column }
4545 end;
4646
47+ { A variable of a procedure, for the trap report: Rt shows it, by
48+ kind, from its place in the frame }
49+ TVarNote = record
50+ chunk, name: longint;
51+ off: longint; { from the frame pointer }
52+ kind: longint; { how Rt shows it: see link.pas }
53+ end;
54+
4755 TFixup = record
4856 at: longint; { the 32-bit field, as an offset in text }
4957 chunk: longint; { the chunk it refers to }
@@ -70,6 +78,8 @@
7078 numLines: longint;
7179 curFile: longint; { the file being compiled, for NoteLine }
7280 curChunk: longint; { the chunk of code being written }
81+ varNotes: ^TVarNote;
82+ numVarNotes: longint;
7383
7484 function NewChunk(sec: TSection; module, name: longint): longint;
7585 procedure BeginChunk(c: longint);
@@ -79,6 +89,8 @@
7989 function ExternChunk(name: longint): longint;
8090 procedure NoteLine(line, col: longint);
8191 procedure AddFixup(at, chunk, addend: longint);
92+procedure NoteVar(chunk, name, off, kind: longint);
93+function ChunkName(c: longint): shortstring;
8294
8395 procedure Put1(b: longint);
8496 procedure Put4(v: longint);
@@ -223,6 +235,26 @@
223235 fixups^[numFixups].chunk := chunk;
224236 fixups^[numFixups].addend := addend;
225237 inc(numFixups);
238+end;
239+
240+procedure NoteVar(chunk, name, off, kind: longint);
241+begin
242+ if numVarNotes and 255 = 0 then varNotes := ReAllocMem(varNotes, (numVarNotes + 256) * SizeOf(TVarNote));
243+ varNotes[numVarNotes].chunk := chunk;
244+ varNotes[numVarNotes].name := name;
245+ varNotes[numVarNotes].off := off;
246+ varNotes[numVarNotes].kind := kind;
247+ inc(numVarNotes);
248+end;
249+
250+{ The name of chunk c: its module's and its own, or literal and its
251+ number for a literal }
252+function ChunkName(c: longint): shortstring;
253+begin
254+ with chunks^[c] do
255+ if name < 0 then ChunkName := 'literal' + IntStr(c)
256+ else if module < 0 then ChunkName := NameOf(name)
257+ else ChunkName := NameOf(module) + '.' + NameOf(name);
226258 end;
227259
228260 initialization

src/link.pas modified +74 -26

@@ -37,17 +37,9 @@
3737
3838 procedure MarkLive(entry: longint);
3939 procedure LinkAll(headerSize, roExtra, dynSize: int64);
40-function ChunkName(c: longint): shortstring;
4140
4241 implementation
4342
44-function ChunkName(c: longint): shortstring;
45-begin
46- if chunks^[c].name < 0 then exit('a literal');
47- if chunks^[c].module < 0 then exit(NameOf(chunks^[c].name));
48- ChunkName := NameOf(chunks^[c].module) + '.' + NameOf(chunks^[c].name);
49-end;
50-
5143 procedure MarkLive(entry: longint);
5244 var stack: PLongs; sp, c, f, t: longint;
5345 begin
@@ -89,16 +81,40 @@
8981 end;
9082
9183 { The places that Rt reads to say where a failed check or a signal
92- was: their number and where text starts and stops (I64s); for each
93- place in live code its address, line, column and the offset of its
94- file's path (U32s); then the paths, each ending in a zero byte. They
95- are in no order, since Rt looks at them only as the program stops. }
84+ was, and what calls were under way: their numbers, the places', where
85+ text starts and stops, the procedures' and their variables' (I64s);
86+ for each place in live code its address, line, column and the offset
87+ of its file's path (U32s); for each live procedure where it starts and
88+ stops, the offset of its name, its first variable and how many it has
89+ (U32s); for each variable the offset of its name, its place in the
90+ frame and its kind, as parse.pas's VarKind (U32s); then the paths and
91+ names, each ending in a zero byte. They are in no order, since Rt
92+ looks at them only as the program stops. }
9693 procedure MakePlaces(start, stop: int64);
97-var files, offs: PLongs; buf: PBytes; i, f, n, nfiles, k: longint; s: shortstring;
94+var
95+ files, offs, first, count: PLongs; buf: PBytes;
96+ i, f, n, nfiles, k, np, nv, c, v, at, names: longint;
97+
98+ procedure Name(const s: shortstring);
99+ begin
100+ PLongint(@buf^[at])^ := names - k;
101+ Move(s[1], buf^[names], length(s));
102+ buf^[names + length(s)] := 0;
103+ names := names + length(s) + 1;
104+ at := at + 4;
105+ end;
106+
98107 begin
99108 files := GetMem(numLines * 4 + 4);
100109 offs := GetMem(numLines * 4 + 4);
101- n := 0; nfiles := 0; k := 0;
110+ first := GetMem(numChunks * 4);
111+ count := GetMem(numChunks * 4);
112+ FillChar(count^, numChunks * 4, 0);
113+ for v := numVarNotes - 1 downto 0 do begin
114+ first^[varNotes[v].chunk] := v;
115+ inc(count^[varNotes[v].chunk]);
116+ end;
117+ n := 0; nfiles := 0; k := 0; np := 0; nv := 0;
102118 for i := 0 to numLines - 1 do
103119 if chunks^[lines^[i].chunk].live then begin
104120 inc(n);
@@ -111,28 +127,60 @@
111127 inc(nfiles);
112128 end;
113129 end;
114- buf := GetMem(24 + 16 * n + k);
130+ for c := 0 to numChunks - 1 do
131+ if chunks^[c].live and (chunks^[c].sec = secText) and (chunks^[c].module >= 0) then begin
132+ inc(np);
133+ k := k + length(ChunkName(c)) + 1;
134+ for v := first^[c] to first^[c] + count^[c] - 1 do k := k + length(NameOf(varNotes[v].name)) + 1;
135+ nv := nv + count^[c];
136+ end;
137+ buf := GetMem(40 + 16 * n + 20 * np + 12 * nv + k);
115138 PInt64(@buf^[0])^ := n;
116139 PInt64(@buf^[8])^ := start;
117140 PInt64(@buf^[16])^ := stop;
118- k := 24;
141+ PInt64(@buf^[24])^ := np;
142+ PInt64(@buf^[32])^ := nv;
143+ at := 40;
119144 for i := 0 to numLines - 1 do
120145 if chunks^[lines^[i].chunk].live then begin
121146 f := 0;
122147 while files^[f] <> lines^[i].path do inc(f);
123- PLongint(@buf^[k])^ := chunks^[lines^[i].chunk].adr + lines^[i].at - chunks^[lines^[i].chunk].start;
124- PLongint(@buf^[k + 4])^ := lines^[i].line;
125- PLongint(@buf^[k + 8])^ := lines^[i].col;
126- PLongint(@buf^[k + 12])^ := offs^[f];
127- k := k + 16;
148+ PLongint(@buf^[at])^ := chunks^[lines^[i].chunk].adr + lines^[i].at - chunks^[lines^[i].chunk].start;
149+ PLongint(@buf^[at + 4])^ := lines^[i].line;
150+ PLongint(@buf^[at + 8])^ := lines^[i].col;
151+ PLongint(@buf^[at + 12])^ := offs^[f];
152+ at := at + 16;
128153 end;
154+ { the pool of paths and names starts at k }
155+ k := 40 + 16 * n + 20 * np + 12 * nv;
156+ names := k;
129157 for f := 0 to nfiles - 1 do begin
130- s := ShownPath(NameOf(files^[f])) + #0;
131- Move(s[1], buf^[k], length(s));
132- k := k + length(s);
158+ i := at;
159+ Name(ShownPath(NameOf(files^[f])));
160+ at := i;
133161 end;
134- SetData(placesChunk, buf, k, 8);
135- FreeMem(buf); FreeMem(files); FreeMem(offs);
162+ v := 0;
163+ for c := 0 to numChunks - 1 do
164+ if chunks^[c].live and (chunks^[c].sec = secText) and (chunks^[c].module >= 0) then begin
165+ PLongint(@buf^[at])^ := chunks^[c].adr;
166+ PLongint(@buf^[at + 4])^ := chunks^[c].adr + chunks^[c].size;
167+ at := at + 8;
168+ Name(ChunkName(c));
169+ PLongint(@buf^[at])^ := v;
170+ PLongint(@buf^[at + 4])^ := count^[c];
171+ at := at + 8;
172+ v := v + count^[c];
173+ end;
174+ for c := 0 to numChunks - 1 do
175+ if chunks^[c].live and (chunks^[c].sec = secText) and (chunks^[c].module >= 0) then
176+ for v := first^[c] to first^[c] + count^[c] - 1 do begin
177+ Name(NameOf(varNotes[v].name));
178+ PLongint(@buf^[at])^ := varNotes[v].off;
179+ PLongint(@buf^[at + 4])^ := varNotes[v].kind;
180+ at := at + 8;
181+ end;
182+ SetData(placesChunk, buf, names, 8);
183+ FreeMem(buf); FreeMem(files); FreeMem(offs); FreeMem(first); FreeMem(count);
136184 end;
137185
138186 { Places the live chunks, leaving roExtra bytes at the end of rodata

src/parse.pas modified +31 -1

@@ -1631,8 +1631,26 @@
16311631
16321632 procedure Declarations(top: boolean); forward;
16331633
1634+{ How Rt shows a variable of o's type in the trap report: 1 a signed
1635+ integer, 2 an unsigned one, 3 a BOOL, 4 an address, 5 a real; plus
1636+ its size times 16, and 256 if the frame holds its address; 0 if it
1637+ isn't shown. }
1638+function VarKind(o: PObj): longint;
1639+var k: longint;
1640+begin
1641+ case o^.typ^.form of
1642+ fInt: k := 2 - ord(o^.typ^.signed);
1643+ fEnum, fSet: k := 2;
1644+ fBool: k := 3;
1645+ fPtr, fProc, fError: k := 4;
1646+ fReal: k := 5;
1647+ else exit(0);
1648+ end;
1649+ VarKind := k + o^.typ^.size * 16 + ord(o^.ind) * 256;
1650+end;
1651+
16341652 procedure Body(p: PObj; sig: PType; line: longint);
1635-var q, params, last, o: PObj; old: PType;
1653+var q, params, last, o, before: PObj; old: PType;
16361654 begin
16371655 OpenScope(NewScope);
16381656 params := nil; last := nil;
@@ -1656,7 +1674,19 @@
16561674 o := q;
16571675 end;
16581676 curProc := p;
1677+ before := curModule^.lastLocal;
16591678 Declarations(false);
1679+ { its parameters and variables, for the trap report }
1680+ if before = nil then o := curModule^.locals else o := before^.link;
1681+ q := params;
1682+ while q <> nil do begin
1683+ if VarKind(q) <> 0 then NoteVar(p^.chunk, q^.name, q^.val, VarKind(q));
1684+ q := q^.next;
1685+ end;
1686+ while o <> nil do begin
1687+ if (o^.cls = oVar) and (VarKind(o) <> 0) then NoteVar(p^.chunk, o^.name, o^.val, VarKind(o));
1688+ o := o^.link;
1689+ end;
16601690 Expect(sBegin);
16611691 old := curResult;
16621692 curResult := sig^.base;

src/x64.pas modified +0 -8

@@ -277,14 +277,6 @@
277277 XN := 'xmm' + IntStr(x);
278278 end;
279279
280-function ChunkName(c: longint): shortstring;
281-begin
282- with chunks^[c] do
283- if name < 0 then ChunkName := 'data' + IntStr(c)
284- else if module < 0 then ChunkName := NameOf(name)
285- else ChunkName := NameOf(module) + '.' + NameOf(name);
286-end;
287-
288280 { A memory operand, with the size of what it holds, as the assembler
289281 reads it }
290282 function MN(const m: TMem; size: longint = 0): shortstring;

test/check.sh (history) modified +4 -2

@@ -8,7 +8,8 @@
88 # X.args its arguments, one per line (optional)
99 # X.status its exit status (optional; 0 if missing), 128 and the
1010 # signal's number for one stopped by a signal
11-# X.stderr its standard error (optional; not compared if missing)
11+# X.stderr its standard error (optional; not compared if missing),
12+# each address in it, 0x and hex digits, written 0x?
1213 # A program without X.expect is compiled, not run. test/libc puts
1314 # lib/Sys.raz, over the C library, before the system call one.
1415 # Programs in test/err must not compile, and must print X.err; there
@@ -54,7 +55,8 @@
5455 elif [ $status != $want ]; then
5556 echo "FAIL $dir/$name: status $status, not $want"
5657 fail=$((fail + 1))
57- elif [ -f $dir/$name.stderr ] && ! cmp -s $out/$name.err $dir/$name.stderr; then
58+ elif [ -f $dir/$name.stderr ] && ! sed 's/0x[0-9a-f]*/0x?/g' $out/$name.err |
59+ cmp -s - $dir/$name.stderr; then
5860 echo "FAIL $dir/$name: standard error differs"
5961 diff $out/$name.err $dir/$name.stderr | head -5
6062 fail=$((fail + 1))

test/libc/NilC.stderr (history) modified +1 -0

@@ -1 +1,2 @@
11 test/libc/NilC.raz:11:5: stopped by a nil pointer
2+ in NilC.main at test/libc/NilC.raz:11:5: p = nil

test/libc/Through.raz (history) modified +1 -0

@@ -25,6 +25,7 @@
2525 Mem.free(Heap.heap, ^U8(p));
2626 end;
2727 Out.int(sum); Out.ln;
28+ p := nil; (* its address would change from run to run *)
2829 i := 3;
2930 a[i] := 1;
3031 Out.str("not reached\n");

test/libc/Through.stderr (history) modified +2 -1

@@ -1 +1,2 @@
1-test/libc/Through.raz:29:7: index 3 is out of range for a length of 3
1+test/libc/Through.raz:30:7: index 3 is out of range for a length of 3
2+ in Through.main at test/libc/Through.raz:30:7: p = nil, list = nil, i = 3, sum = 500500

test/msg/Bounds.stderr (history) modified +1 -0

@@ -1 +1,2 @@
11 conform/run/Bounds.raz:8:11: index 4 is out of range for a length of 4
2+ in Bounds.main at conform/run/Bounds.raz:8:11: i = 4

test/msg/Kinds.stderr (history) modified +1 -0

@@ -1 +1,2 @@
11 conform/run/Kinds.raz:92:16: 5 isn't a position of Color
2+ in Kinds.main at conform/run/Kinds.raz:92:16: s = 5, t = 12, c = 1, r = -1, one = 10

test/msg/SliceBounds.stderr (history) modified +1 -0

@@ -1 +1,2 @@
11 conform/run/SliceBounds.raz:10:23: slice 9..6 is out of range for a length of 8
2+ in SliceBounds.main at conform/run/SliceBounds.raz:10:23: lo = 9

test/msg/SliceIndex.stderr (history) modified +1 -0

@@ -1 +1,2 @@
11 conform/run/SliceIndex.raz:11:11: index 3 is out of range for a length of 3
2+ in SliceIndex.main at conform/run/SliceIndex.raz:11:11: i = 3

test/msg/SlicePtrBackwards.stderr (history) modified +1 -0

@@ -1 +1,2 @@
11 conform/run/SlicePtrBackwards.raz:11:23: slice 2..0 runs backwards
2+ in SlicePtrBackwards.main at conform/run/SlicePtrBackwards.raz:11:23: p = 0x?, hi = 0

test/run/CheckOff.stderr (history) modified +1 -0

@@ -1 +1,2 @@
11 test/run/CheckOff.raz:15:17: index 2 is out of range for a length of 2
2+ in CheckOff.main at test/run/CheckOff.raz:15:17: i = 2

test/run/DivLowest.stderr (history) modified +1 -0

@@ -1 +1,2 @@
11 test/run/DivLowest.raz:13:5: stopped by a division fault
2+ in DivLowest.main at test/run/DivLowest.raz:13:5: a = -9223372036854775808, b = -1

test/run/DivLowestI16.stderr (history) modified +1 -0

@@ -1 +1,2 @@
11 test/run/DivLowestI16.raz:12:5: stopped by a division fault
2+ in DivLowestI16.main at test/run/DivLowestI16.raz:12:5: a = -32768, b = -1

test/run/DivLowestI32.stderr (history) modified +1 -0

@@ -1 +1,2 @@
11 test/run/DivLowestI32.raz:11:5: stopped by a division fault
2+ in DivLowestI32.main at test/run/DivLowestI32.raz:11:5: a = -2147483648

test/run/DivLowestI8.stderr (history) modified +1 -0

@@ -1 +1,2 @@
11 test/run/DivLowestI8.raz:13:5: stopped by a division fault
2+ in DivLowestI8.main at test/run/DivLowestI8.raz:13:5: a = -128, b = -1

test/run/DivZero.stderr (history) modified +1 -0

@@ -1 +1,2 @@
11 test/run/DivZero.raz:9:5: stopped by a division fault
2+ in DivZero.main at test/run/DivZero.raz:9:5: a = 7, b = 0

test/run/NilBig.stderr (history) modified +1 -0

@@ -1 +1,2 @@
11 test/run/NilBig.raz:14:5: stopped by a nil pointer
2+ in NilBig.main at test/run/NilBig.raz:14:5

test/run/NilPointer.stderr (history) modified +1 -0

@@ -1 +1,2 @@
11 test/run/NilPointer.raz:13:5: stopped by a nil pointer
2+ in NilPointer.main at test/run/NilPointer.raz:13:5

test/run/NilProc.stderr (history) modified +1 -0

@@ -1 +1,2 @@
11 test/run/NilProc.raz:8:5: stopped by a nil procedure
2+ in NilProc.main at test/run/NilProc.raz:8:5

test/run/StackOverflow.stderr (history) modified +17 -0

@@ -1 +1,18 @@
11 test/run/StackOverflow.raz:7:1: stopped by a stack overflow
2+ in StackOverflow.down at test/run/StackOverflow.raz:7:1
3+ in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 14
4+ in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 13
5+ in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 12
6+ in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 11
7+ in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 10
8+ in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 9
9+ in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 8
10+ in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 7
11+ in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 6
12+ in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 5
13+ in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 4
14+ in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 3
15+ in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 2
16+ in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 1
17+ in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 0
18+ in StackOverflow.main at test/run/StackOverflow.raz:16:5

test/run/ThreadOverflow.stderr (history) modified +20 -0

@@ -1 +1,21 @@
11 test/run/ThreadOverflow.raz:6:1: stopped by a stack overflow
2+ in ThreadOverflow.down at test/run/ThreadOverflow.raz:6:1
3+ in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1926
4+ in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1925
5+ in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1924
6+ in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1923
7+ in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1922
8+ in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1921
9+ in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1920
10+ in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1919
11+ in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1918
12+ in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1917
13+ in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1916
14+ in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1915
15+ in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1914
16+ in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1913
17+ in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1912
18+ in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1911
19+ in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1910
20+ in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1909
21+ in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1908

test/run/Trace.expect (history) added +1 -0

@@ -0,0 +1 @@
1+go

test/run/Trace.raz (history) added +31 -0

@@ -0,0 +1,31 @@
1+module Trace;
2+(* The calls under way, with their variables: on demand with Rt.trace,
3+ and after a failed check *)
4+import Out, Rt;
5+
6+type Color = (red, green, blue);
7+
8+proc inner(var n: I64; f: F64; ok: BOOL; c: Color; b: U8; s: I16);
9+var a: [2]I64; k: INT;
10+begin
11+ Rt.trace;
12+ if ok and (c = blue) and (b > 0) and (s < 0) and (f < 0.0) then k := INT(n); end;
13+ a[k] := 1;
14+end;
15+
16+proc outer(n: I64);
17+var x: I64; r: F32;
18+begin
19+ r := 0.25;
20+ x := n + 1 + I64(r);
21+ inner(x, -1.5, true, blue, 200, -7);
22+end;
23+
24+proc main*;
25+begin
26+ Out.str("go\n");
27+ Out.flush;
28+ outer(4);
29+end;
30+
31+end Trace.

test/run/Trace.status (history) added +1 -0

@@ -0,0 +1 @@
1+2

test/run/Trace.stderr (history) added +7 -0

@@ -0,0 +1,7 @@
1+ in Trace.inner at test/run/Trace.raz:11:5: n = 5, f = -1.500000, ok = true, c = 2, b = 200, s = -7, k = 0
2+ in Trace.outer at test/run/Trace.raz:21:5: n = 4, x = 5, r = 0.250000
3+ in Trace.main at test/run/Trace.raz:28:5
4+test/run/Trace.raz:13:7: index 5 is out of range for a length of 2
5+ in Trace.inner at test/run/Trace.raz:13:7: n = 5, f = -1.500000, ok = true, c = 2, b = 200, s = -7, k = 5
6+ in Trace.outer at test/run/Trace.raz:21:5: n = 4, x = 5, r = 0.250000
7+ in Trace.main at test/run/Trace.raz:28:5