raz / commit 88b789e5

commit
88b789e53c77e62703574eca885270552f2f3ca1
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
115239ad
Step 14: line numbers for gdb

The compiler notes the line of each statement, and of each
procedure's first line and end, as it writes the code. The
executable carries a DWARF 4 line table, one sequence per live
procedure, and the one compile unit gdb needs to find it. make gdb
checks that gdb stops at a line and names each caller's line.
Stage 3 done: the plan records its lines.

patch · log

10 files changed, +211 -30

CLAUDE.md modified +3 -3

@@ -12,6 +12,7 @@
1212 make bench # must report at least 400,000 lines a second
1313 make loc # lines of the compiler; the code column is the budget
1414 make tests # the small Pascal tests of scan, x64, link and elf
15+ make gdb # gdb stops at a line of a test and names it
1516
1617 `make check` must pass before every commit. Tests: `test/run/X.jed`
1718 with `X.expect` (stdout), optional `X.status` and `X.args`; modules they
@@ -37,9 +38,8 @@
3738
3839 ## Where things stand
3940
40-Stages 1 and 2 are done (steps 1-10). Stage 3 (steps 11-14) is under
41-way: its report changes are written and approved, and steps 11 to 13
42-are done. Next is step 14, line numbers for gdb.
41+Stages 1 to 3 are done (steps 1-14), waiting for review. Next is step
42+15, `tools/Doc.jed` and the checked examples, on the owner's go-ahead.
4343
4444 `test/libc` runs its programs over `lib/Sys.jed`, the C one, through
4545 a link in its `mods`. A test or example without `.expect` is only

Makefile (history) modified +5 -1

@@ -27,6 +27,10 @@
2727 check: jed
2828 test/check.sh
2929
30+# line numbers: gdb stops at a line and names it
31+gdb: jed
32+ test/gdb.sh
33+
3034 # the compiler's speed, on a generated program of about 350,000 lines,
3135 # the best of three runs; fails below 400,000 lines a second
3236 bench: jed | build
@@ -44,4 +48,4 @@
4448 loc:
4549 @python3 test/loc.py src/*.pas
4650
47-.PHONY: units tests check bench loc clean
51+.PHONY: units tests check gdb bench loc clean

PLAN.md (history) modified +10 -10

@@ -55,20 +55,20 @@
5555
5656 ## The compiler, in reading order
5757
58-Lines after stage 2, all of them and those of code (neither blank nor
58+Lines after stage 3, all of them and those of code (neither blank nor
5959 only comment), as `make loc` counts them:
6060
6161 | File | Lines | Code | What |
6262 |---|---|---|---|
6363 | `src/scan.pas` | 548 | 471 | Symbols, literals, comments, error messages |
64-| `src/sym.pas` | 492 | 410 | Types, objects, scopes, layout |
65-| `src/x64.pas` | 620 | 497 | Code buffer, chunks, fixups, instruction encoder |
66-| `src/gen.pas` | 1,354 | 1,193 | Items: loading, storing, copying, frames, calls |
67-| `src/parse.pas` | 1,754 | 1,626 | Declarations, statements, expressions, built-ins |
68-| `src/link.pas` | 123 | 92 | Tree shaking, layout, patching |
69-| `src/elf.pas` | 185 | 147 | Static ELF, symbol table |
70-| `src/jed.pas` | 111 | 83 | The command line, the entry point |
71-| total | 5,187 | 4,519 | |
64+| `src/sym.pas` | 508 | 422 | Types, objects, scopes, layout |
65+| `src/x64.pas` | 680 | 546 | Code buffer, chunks, fixups, line notes, instruction encoder |
66+| `src/gen.pas` | 1,414 | 1,240 | Items: loading, storing, copying, frames, calls |
67+| `src/parse.pas` | 1,816 | 1,685 | Declarations, statements, expressions, built-ins |
68+| `src/link.pas` | 131 | 95 | Tree shaking, layout, patching |
69+| `src/elf.pas` | 424 | 350 | Static and dynamic ELF, symbol table, line numbers |
70+| `src/jed.pas` | 183 | 145 | The command line, the entry points |
71+| total | 5,704 | 4,954 | |
7272
7373 ## Steps
7474
@@ -124,7 +124,7 @@
124124 and a `Mutex` through `futex`. `link static;` marks a module that
125125 needs a program without C; the compiler refuses a program that
126126 uses both.
127-- [ ] 14. Line numbers for gdb: `.debug_line`, with the least of
127+- [x] 14. Line numbers for gdb: `.debug_line`, with the least of
128128 `.debug_info` and `.debug_abbrev` that gdb needs.
129129
130130 Tools

doc/jed.md modified +2 -0

@@ -796,6 +796,8 @@
796796
797797 Only what can be reached from `main` and the module bodies goes into
798798 the executable: unused procedures, variables and strings are left out.
799+The executable names its procedures and carries line numbers, so that
800+gdb can stop at a line of the source and show where it is.
799801
800802 **Defined behaviour.** Every variable starts at zero, integers wrap,
801803 and indexing arrays and slices is checked, so a program's behaviour is

src/elf.pas modified +120 -5

@@ -3,7 +3,9 @@
33 Writes an ELF64 executable for x86-64 Linux: the ELF header, the
44 program headers, the linked text and rodata, and section headers
55 with a symbol table, so that objdump and gdb can name each procedure
6- and variable.
6+ and variable. For gdb's line numbers there is DWARF 4: a line table
7+ (.debug_line), and the one compile unit that points to it
8+ (.debug_info, .debug_abbrev). There are no types or variables.
79
810 A program that uses C is linked dynamically, in the least way the
911 loader accepts: it names its loader and libraries, and each C symbol
@@ -13,7 +15,8 @@
1315
1416 Depends on: link for the images and addresses, x64 for the chunks,
1517 scan for names and errors, BaseUnix to write the file.
16- Leaves out: position independent executables, symbol versions. }
18+ Leaves out: position independent executables, symbol versions, the
19+ rest of DWARF. }
1720
1821 unit elf;
1922
@@ -26,6 +29,7 @@
2629 var
2730 needed: array[0..31] of longint; { the libraries to load, names interned }
2831 numNeeded: longint; { none: a static program }
32+ mainPath: shortstring; { the main module's file, for gdb }
2933
3034 procedure WriteElf(const path: shortstring; entry: longint);
3135
@@ -102,6 +106,112 @@
102106 PutN(0, 1);
103107 end;
104108
109+{ LEB128, unsigned and signed }
110+procedure PutU(v: qword);
111+begin
112+ while v >= $80 do begin
113+ PutN(v and $7F or $80, 1);
114+ v := v shr 7;
115+ end;
116+ PutN(v, 1);
117+end;
118+
119+procedure PutS(v: int64);
120+begin
121+ while (v < -64) or (v >= 64) do begin
122+ PutN(v and $7F or $80, 1);
123+ v := SarInt64(v, 7);
124+ end;
125+ PutN(v and $7F, 1);
126+end;
127+
128+{ The line table: one sequence for each live procedure with lines,
129+ rows made only with the standard opcodes, no special ones. Then the
130+ compile unit, which names the table, and its abbreviation. Section
131+ offsets go in off, sizes in size: line, info, abbrev. }
132+procedure PutDebug(var off, size: array of int64);
133+var
134+ files: array[0..255] of longint; nfiles, i, f, c, cf, cl: longint;
135+ at: int64; cwd: array[0..255] of char;
136+begin
137+ nfiles := 0;
138+ for i := 0 to numLines - 1 do
139+ if chunks^[lines^[i].chunk].live then begin
140+ f := 0;
141+ while (f < nfiles) and (files[f] <> lines^[i].path) do inc(f);
142+ if (f = nfiles) and (nfiles <= High(files)) then begin
143+ files[f] := lines^[i].path;
144+ inc(nfiles);
145+ end;
146+ end;
147+
148+ off[0] := len;
149+ PutN(0, 4); { unit_length, patched below }
150+ PutN(4, 2); { version }
151+ PutN(0, 4); { header_length, patched below }
152+ PutN(1, 1); PutN(1, 1); { minimum_instruction_length, maximum_operations }
153+ PutN(1, 1); { default_is_stmt }
154+ PutN(-5 and $FF, 1); PutN(14, 1); PutN(13, 1); { line_base, line_range, opcode_base }
155+ for i := 1 to 12 do PutN(ord(i in [2, 3, 4, 5, 9, 12]), 1); { their operands }
156+ PutN(0, 1); { no include directories }
157+ for f := 0 to nfiles - 1 do begin
158+ PutStr(NameOf(files[f]));
159+ PutN(0, 3); { directory, time, length }
160+ end;
161+ PutN(0, 1);
162+ Patch(off[0] + 6, len - off[0] - 10, 4);
163+ i := 0;
164+ while i < numLines do begin
165+ c := lines^[i].chunk;
166+ if not chunks^[c].live then begin inc(i); continue end;
167+ PutN(0, 1); PutU(9); PutN(2, 1); { DW_LNE_set_address }
168+ PutN(chunks^[c].adr + lines^[i].at - chunks^[c].start, 8);
169+ at := lines^[i].at; cf := 1; cl := 1;
170+ while (i < numLines) and (lines^[i].chunk = c) do begin
171+ if lines^[i].at > at then begin
172+ PutN(2, 1); PutU(lines^[i].at - at); { DW_LNS_advance_pc }
173+ at := lines^[i].at;
174+ end;
175+ f := 1;
176+ while (f <= nfiles) and (files[f - 1] <> lines^[i].path) do inc(f);
177+ if f <> cf then begin PutN(4, 1); PutU(f); cf := f end; { DW_LNS_set_file }
178+ if lines^[i].line <> cl then begin { DW_LNS_advance_line }
179+ PutN(3, 1); PutS(lines^[i].line - cl);
180+ cl := lines^[i].line;
181+ end;
182+ PutN(1, 1); { DW_LNS_copy: a row }
183+ inc(i);
184+ end;
185+ PutN(2, 1); PutU(chunks^[c].start + chunks^[c].size - at);
186+ PutN(0, 1); PutU(1); PutN(1, 1); { DW_LNE_end_sequence }
187+ end;
188+ Patch(off[0], len - off[0] - 4, 4);
189+ size[0] := len - off[0];
190+
191+ off[1] := len;
192+ PutN(0, 4); PutN(4, 2); PutN(0, 4); PutN(8, 1); { length, version, abbrev, address size }
193+ PutU(1);
194+ PutStr(mainPath);
195+ if FpGetcwd(@cwd, SizeOf(cwd)) = nil then cwd[0] := #0;
196+ PutStr(PChar(@cwd));
197+ PutStr('jed');
198+ PutN(0, 4); { the line table, at 0 }
199+ PutN(Base + textOff, 8); PutN(textSize, 8); { the code, all of it }
200+ Patch(off[1], len - off[1] - 4, 4);
201+ size[1] := len - off[1];
202+
203+ off[2] := len;
204+ PutU(1); PutU($11); PutN(0, 1); { 1: a compile unit, no children }
205+ PutU($03); PutU($08); { name, a string }
206+ PutU($1B); PutU($08); { comp_dir, a string }
207+ PutU($25); PutU($08); { producer, a string }
208+ PutU($10); PutU($17); { stmt_list, a section offset }
209+ PutU($11); PutU($01); { low_pc, an address }
210+ PutU($12); PutU($07); { high_pc, a length in 8 bytes }
211+ PutN(0, 2); PutN(0, 1);
212+ size[2] := len - off[2];
213+end;
214+
105215 const
106216 Interp = '/lib64/ld-linux-x86-64.so.2';
107217 { program header types }
@@ -192,10 +302,11 @@
192302 procedure WriteElf(const path: shortstring; entry: longint);
193303 const
194304 ShNames = #0'.text'#0'.rodata'#0'.bss'#0'.symtab'#0'.strtab'#0'.shstrtab'#0 +
195- '.dynamic'#0'.dynstr'#0'.dynsym'#0;
305+ '.dynamic'#0'.dynstr'#0'.dynsym'#0'.debug_line'#0'.debug_info'#0'.debug_abbrev'#0;
196306 var
197307 nph, nsh, c, nsyms, fd: longint;
198308 roExtra, dynSize, symOff, strOff, shstrOff, shOff, strsLen: int64;
309+ dbgOff, dbgSize: array[0..2] of int64;
199310 strs: PBytes;
200311 s, p: shortstring;
201312 sec: longint;
@@ -226,8 +337,8 @@
226337 PutN(64, 8); { program headers follow }
227338 PutN(0, 8); { section headers: patched below }
228339 PutN(0, 4);
229- nsh := 7;
230- if dynamic then nsh := 10;
340+ nsh := 10;
341+ if dynamic then nsh := 13;
231342 PutN(64, 2); PutN(56, 2); PutN(nph, 2);
232343 PutN(64, 2); PutN(nsh, 2); PutN(6, 2);
233344
@@ -280,6 +391,7 @@
280391 shstrOff := len;
281392 s := ShNames;
282393 PutBytes(@s[1], length(s));
394+ PutDebug(dbgOff, dbgSize);
283395
284396 PadTo((len + 7) div 8 * 8);
285397 shOff := len;
@@ -296,6 +408,9 @@
296408 Shdr(55, 3, 2, extraAdr + strAt, extraAdr + strAt - Base, strLen, 0, 0, 1, 0);
297409 Shdr(63, 11, 2, extraAdr + symAt, extraAdr + symAt - Base, nsym * 24, 8, 1, 8, 24);
298410 end;
411+ Shdr(71, 1, 0, 0, dbgOff[0], dbgSize[0], 0, 0, 1, 0);
412+ Shdr(83, 1, 0, 0, dbgOff[1], dbgSize[1], 0, 0, 1, 0);
413+ Shdr(95, 1, 0, 0, dbgOff[2], dbgSize[2], 0, 0, 1, 0);
299414
300415 p := path + #0;
301416 fd := FpOpen(@p[1], O_WRONLY or O_CREAT or O_TRUNC, &755);

src/gen.pas modified +4 -2

@@ -113,7 +113,7 @@
113113 procedure KeepTemps;
114114
115115 procedure BeginProc(sig: PType; params: PObj);
116-procedure EnterBody(c: longint);
116+procedure EnterBody(c, line: longint);
117117 procedure ReturnValue(var x: TItem);
118118 procedure ReturnField(f: PObj; var x: TItem);
119119 procedure ReturnJump;
@@ -1258,10 +1258,12 @@
12581258 end;
12591259 end;
12601260
1261-procedure EnterBody(c: longint);
1261+{ The prologue, which is for the procedure's first line. }
1262+procedure EnterBody(c, line: longint);
12621263 var i, top: longint;
12631264 begin
12641265 BeginChunk(c);
1266+ NoteLine(line);
12651267 Push(RBP);
12661268 MovRR(RBP, RSP);
12671269 Put1($48); Put1($81); Put1($EC); { sub rsp, imm32 }

src/jed.pas modified +1 -0

@@ -178,5 +178,6 @@
178178 Ud2;
179179 EndChunk(start);
180180
181+ mainPath := path;
181182 WriteElf(outPath, entry);
182183 end.

src/parse.pas modified +14 -8

@@ -1145,6 +1145,7 @@
11451145 procedure Statement;
11461146 var x, y: TItem; called: boolean; m: int64; c: TCall;
11471147 begin
1148+ NoteLine(CurLine);
11481149 m := TempMark;
11491150 case tok of
11501151 sIdent: begin
@@ -1242,7 +1243,7 @@
12421243
12431244 procedure Declarations(top: boolean); forward;
12441245
1245-procedure Body(p: PObj; sig: PType);
1246+procedure Body(p: PObj; sig: PType; line: longint);
12461247 var q, params, last, o: PObj; old: PType;
12471248 begin
12481249 OpenScope(NewScope);
@@ -1268,8 +1269,9 @@
12681269 Expect(sBegin, 'begin');
12691270 old := curResult;
12701271 curResult := sig^.base;
1271- EnterBody(p^.chunk);
1272+ EnterBody(p^.chunk, line);
12721273 Statements;
1274+ NoteLine(CurLine);
12731275 Expect(sEnd, 'end');
12741276 EndProc(p^.chunk);
12751277 curResult := old;
@@ -1308,8 +1310,9 @@
13081310 end;
13091311
13101312 procedure ProcDecl;
1311-var n: longint; exp: boolean; sig: PType; p: PObj;
1313+var n, line: longint; exp: boolean; sig: PType; p: PObj;
13121314 begin
1315+ line := CurLine;
13131316 Next;
13141317 IdentDef(n, exp);
13151318 sig := Signature;
@@ -1339,7 +1342,7 @@
13391342 CodeBody(p, sig);
13401343 exit;
13411344 end;
1342- if checkMode then SkipBody else Body(p, sig);
1345+ if checkMode then SkipBody else Body(p, sig, line);
13431346 if tok = sIdent then begin
13441347 if id <> n then Mark('the procedure is ' + NameOf(n));
13451348 Next;
@@ -1583,11 +1586,12 @@
15831586 var
15841587 st: TScanState; saved: array[0..MaxDepth - 1] of longint; d: longint;
15851588 oldMod: PModule; oldFile: longint; oldCheck: boolean; m, im: PModule;
1586- imp: PImport; lib: PLib; alias, mname: longint; o: PObj; body: longint; iargs: TArgs;
1589+ imp: PImport; lib: PLib; alias, mname, line, oldPath: longint; o: PObj; body: longint; iargs: TArgs;
15871590 begin
15881591 SaveScan(st);
15891592 SaveScopes(d, saved);
1590- oldMod := curModule; oldFile := fileChunk; oldCheck := checkMode;
1593+ oldMod := curModule; oldFile := fileChunk; oldCheck := checkMode; oldPath := curFile;
1594+ curFile := Name(path);
15911595 if not Open(path) then Fatal('can''t read ' + path);
15921596 New(m);
15931597 FillChar(m^, SizeOf(TModule), 0);
@@ -1659,14 +1663,16 @@
16591663 if tok = sBegin then begin
16601664 if checkMode then SkipBody
16611665 else begin
1666+ line := CurLine;
16621667 Next;
16631668 body := NewChunk(secText, m^.sname, nameBody);
16641669 m^.body := body;
16651670 BeginProc(tBodySig, nil);
16661671 deferSlot := 0;
16671672 curResult := nil;
1668- EnterBody(body);
1673+ EnterBody(body, line);
16691674 Statements;
1675+ NoteLine(CurLine);
16701676 Expect(sEnd, 'end');
16711677 EndProc(body);
16721678 end;
@@ -1686,7 +1692,7 @@
16861692 if lastModule = nil then firstModule := m else lastModule^.next := m;
16871693 lastModule := m;
16881694 end;
1689- curModule := oldMod; fileChunk := oldFile; checkMode := oldCheck;
1695+ curModule := oldMod; fileChunk := oldFile; checkMode := oldCheck; curFile := oldPath;
16901696 RestoreScopes(d, saved);
16911697 RestoreScan(st);
16921698 CompileFile := m;

src/x64.pas modified +36 -1

@@ -61,6 +61,12 @@
6161 ext: boolean; { bss: the address of a C symbol, its name }
6262 end;
6363
64+ { A source line whose code starts at a place in text, for gdb }
65+ TLine = record
66+ at, chunk: longint; { the place in text, and its chunk }
67+ path, line: longint; { the file's path, interned, and the line }
68+ end;
69+
6470 TFixup = record
6571 at: longint; { the rel32 field, as an offset in text }
6672 chunk: longint; { the chunk it refers to }
@@ -69,6 +75,8 @@
6975
7076 PChunks = ^TChunkArray;
7177 TChunkArray = array[0..MaxInt div SizeOf(TChunk) - 1] of TChunk;
78+ PLines = ^TLineArray;
79+ TLineArray = array[0..MaxInt div SizeOf(TLine) - 1] of TLine;
7280 PFixups = ^TFixupArray;
7381 TFixupArray = array[0..MaxInt div SizeOf(TFixup) - 1] of TFixup;
7482
@@ -81,6 +89,9 @@
8189 numChunks: longint;
8290 fixups: PFixups;
8391 numFixups: longint;
92+ lines: PLines;
93+ numLines: longint;
94+ curFile: longint; { the file being compiled, for NoteLine }
8495
8596 function NewChunk(sec: TSection; module, name: longint): longint;
8697 procedure BeginChunk(c: longint);
@@ -88,6 +99,7 @@
8899 procedure SetData(c: longint; p: PBytes; n, align: longint);
89100 procedure SetBss(c: longint; size, align: longint);
90101 function ExternChunk(name: longint): longint;
102+procedure NoteLine(line: longint);
91103
92104 procedure Put1(b: longint);
93105 procedure Put4(v: longint);
@@ -152,7 +164,8 @@
152164 implementation
153165
154166 var
155- codeCap, roCap, chunkCap, fixCap: longint;
167+ codeCap, roCap, chunkCap, fixCap, lineCap: longint;
168+ curChunk: longint;
156169
157170 { Buffers and chunks }
158171
@@ -214,6 +227,7 @@
214227 begin
215228 { code is aligned to 16 bytes, padded with int3 }
216229 while pc and 15 <> 0 do Put1($CC);
230+ curChunk := c;
217231 chunks^[c].start := pc;
218232 chunks^[c].align := 16;
219233 chunks^[c].firstFix := numFixups;
@@ -244,6 +258,24 @@
244258 chunks^[c].align := align;
245259 end;
246260
261+{ The code from here on is for line of the current file. A line with
262+ no code of its own gives way to the next. }
263+procedure NoteLine(line: longint);
264+begin
265+ if (numLines > 0) and (lines^[numLines - 1].at = pc) then dec(numLines);
266+ if (numLines > 0) and (lines^[numLines - 1].chunk = curChunk)
267+ and (lines^[numLines - 1].line = line) and (lines^[numLines - 1].path = curFile) then exit;
268+ if numLines = lineCap then begin
269+ lineCap := lineCap * 2;
270+ lines := ReAllocMem(lines, lineCap * SizeOf(TLine));
271+ end;
272+ lines^[numLines].at := pc;
273+ lines^[numLines].chunk := curChunk;
274+ lines^[numLines].path := curFile;
275+ lines^[numLines].line := line;
276+ inc(numLines);
277+end;
278+
247279 { The quadword that holds the address of C's symbol name, filled in
248280 when the program starts; one for each name. }
249281 function ExternChunk(name: longint): longint;
@@ -642,4 +674,7 @@
642674 fixCap := 1 shl 12;
643675 fixups := GetMem(fixCap * SizeOf(TFixup));
644676 numFixups := 0;
677+ lineCap := 1 shl 12;
678+ lines := GetMem(lineCap * SizeOf(TLine));
679+ numLines := 0;
645680 end.

test/gdb.sh (history) added (executable) +16 -0

@@ -0,0 +1,16 @@
1+#!/bin/sh
2+# Line numbers for gdb: stops a test program at a line of its source,
3+# by file and line, and checks that gdb names it, and the line of each
4+# caller.
5+out=build/check
6+mkdir -p $out
7+./jed -I test/run/mods -o $out/Procs test/run/Procs.jed || exit 1
8+gdb -batch -ex 'break test/run/Procs.jed:13' -ex run -ex bt $out/Procs > $out/gdb.out 2>&1
9+for want in 'Breakpoint 1, Procs.odd () at test/run/Procs.jed:13' \
10+ 'in Procs.even () at test/run/Procs.jed:20' \
11+ 'in Procs.main () at test/run/Procs.jed:'; do
12+ if ! grep -qF "$want" $out/gdb.out; then
13+ echo "FAIL gdb: no \"$want\""; cat $out/gdb.out; exit 1
14+ fi
15+done
16+echo "gdb finds the lines"