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.
| @@ -12,6 +12,7 @@ | ||
| 12 | 12 | make bench # must report at least 400,000 lines a second |
| 13 | 13 | make loc # lines of the compiler; the code column is the budget |
| 14 | 14 | 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 | |
| 15 | 16 | |
| 16 | 17 | `make check` must pass before every commit. Tests: `test/run/X.jed` |
| 17 | 18 | with `X.expect` (stdout), optional `X.status` and `X.args`; modules they |
| @@ -37,9 +38,8 @@ | ||
| 37 | 38 | |
| 38 | 39 | ## Where things stand |
| 39 | 40 | |
| 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. | |
| 43 | 43 | |
| 44 | 44 | `test/libc` runs its programs over `lib/Sys.jed`, the C one, through |
| 45 | 45 | a link in its `mods`. A test or example without `.expect` is only |
| @@ -27,6 +27,10 @@ | ||
| 27 | 27 | check: jed |
| 28 | 28 | test/check.sh |
| 29 | 29 | |
| 30 | +# line numbers: gdb stops at a line and names it | |
| 31 | +gdb: jed | |
| 32 | + test/gdb.sh | |
| 33 | + | |
| 30 | 34 | # the compiler's speed, on a generated program of about 350,000 lines, |
| 31 | 35 | # the best of three runs; fails below 400,000 lines a second |
| 32 | 36 | bench: jed | build |
| @@ -44,4 +48,4 @@ | ||
| 44 | 48 | loc: |
| 45 | 49 | @python3 test/loc.py src/*.pas |
| 46 | 50 | |
| 47 | -.PHONY: units tests check bench loc clean | |
| 51 | +.PHONY: units tests check gdb bench loc clean |
| @@ -55,20 +55,20 @@ | ||
| 55 | 55 | |
| 56 | 56 | ## The compiler, in reading order |
| 57 | 57 | |
| 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 | |
| 59 | 59 | only comment), as `make loc` counts them: |
| 60 | 60 | |
| 61 | 61 | | File | Lines | Code | What | |
| 62 | 62 | |---|---|---|---| |
| 63 | 63 | | `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 | | | |
| 72 | 72 | |
| 73 | 73 | ## Steps |
| 74 | 74 | |
| @@ -124,7 +124,7 @@ | ||
| 124 | 124 | and a `Mutex` through `futex`. `link static;` marks a module that |
| 125 | 125 | needs a program without C; the compiler refuses a program that |
| 126 | 126 | 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 | |
| 128 | 128 | `.debug_info` and `.debug_abbrev` that gdb needs. |
| 129 | 129 | |
| 130 | 130 | Tools |
| @@ -796,6 +796,8 @@ | ||
| 796 | 796 | |
| 797 | 797 | Only what can be reached from `main` and the module bodies goes into |
| 798 | 798 | 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. | |
| 799 | 801 | |
| 800 | 802 | **Defined behaviour.** Every variable starts at zero, integers wrap, |
| 801 | 803 | and indexing arrays and slices is checked, so a program's behaviour is |
| @@ -3,7 +3,9 @@ | ||
| 3 | 3 | Writes an ELF64 executable for x86-64 Linux: the ELF header, the |
| 4 | 4 | program headers, the linked text and rodata, and section headers |
| 5 | 5 | 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. | |
| 7 | 9 | |
| 8 | 10 | A program that uses C is linked dynamically, in the least way the |
| 9 | 11 | loader accepts: it names its loader and libraries, and each C symbol |
| @@ -13,7 +15,8 @@ | ||
| 13 | 15 | |
| 14 | 16 | Depends on: link for the images and addresses, x64 for the chunks, |
| 15 | 17 | 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. } | |
| 17 | 20 | |
| 18 | 21 | unit elf; |
| 19 | 22 | |
| @@ -26,6 +29,7 @@ | ||
| 26 | 29 | var |
| 27 | 30 | needed: array[0..31] of longint; { the libraries to load, names interned } |
| 28 | 31 | numNeeded: longint; { none: a static program } |
| 32 | + mainPath: shortstring; { the main module's file, for gdb } | |
| 29 | 33 | |
| 30 | 34 | procedure WriteElf(const path: shortstring; entry: longint); |
| 31 | 35 | |
| @@ -102,6 +106,112 @@ | ||
| 102 | 106 | PutN(0, 1); |
| 103 | 107 | end; |
| 104 | 108 | |
| 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 | + | |
| 105 | 215 | const |
| 106 | 216 | Interp = '/lib64/ld-linux-x86-64.so.2'; |
| 107 | 217 | { program header types } |
| @@ -192,10 +302,11 @@ | ||
| 192 | 302 | procedure WriteElf(const path: shortstring; entry: longint); |
| 193 | 303 | const |
| 194 | 304 | 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; | |
| 196 | 306 | var |
| 197 | 307 | nph, nsh, c, nsyms, fd: longint; |
| 198 | 308 | roExtra, dynSize, symOff, strOff, shstrOff, shOff, strsLen: int64; |
| 309 | + dbgOff, dbgSize: array[0..2] of int64; | |
| 199 | 310 | strs: PBytes; |
| 200 | 311 | s, p: shortstring; |
| 201 | 312 | sec: longint; |
| @@ -226,8 +337,8 @@ | ||
| 226 | 337 | PutN(64, 8); { program headers follow } |
| 227 | 338 | PutN(0, 8); { section headers: patched below } |
| 228 | 339 | PutN(0, 4); |
| 229 | - nsh := 7; | |
| 230 | - if dynamic then nsh := 10; | |
| 340 | + nsh := 10; | |
| 341 | + if dynamic then nsh := 13; | |
| 231 | 342 | PutN(64, 2); PutN(56, 2); PutN(nph, 2); |
| 232 | 343 | PutN(64, 2); PutN(nsh, 2); PutN(6, 2); |
| 233 | 344 | |
| @@ -280,6 +391,7 @@ | ||
| 280 | 391 | shstrOff := len; |
| 281 | 392 | s := ShNames; |
| 282 | 393 | PutBytes(@s[1], length(s)); |
| 394 | + PutDebug(dbgOff, dbgSize); | |
| 283 | 395 | |
| 284 | 396 | PadTo((len + 7) div 8 * 8); |
| 285 | 397 | shOff := len; |
| @@ -296,6 +408,9 @@ | ||
| 296 | 408 | Shdr(55, 3, 2, extraAdr + strAt, extraAdr + strAt - Base, strLen, 0, 0, 1, 0); |
| 297 | 409 | Shdr(63, 11, 2, extraAdr + symAt, extraAdr + symAt - Base, nsym * 24, 8, 1, 8, 24); |
| 298 | 410 | 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); | |
| 299 | 414 | |
| 300 | 415 | p := path + #0; |
| 301 | 416 | fd := FpOpen(@p[1], O_WRONLY or O_CREAT or O_TRUNC, &755); |
| @@ -113,7 +113,7 @@ | ||
| 113 | 113 | procedure KeepTemps; |
| 114 | 114 | |
| 115 | 115 | procedure BeginProc(sig: PType; params: PObj); |
| 116 | -procedure EnterBody(c: longint); | |
| 116 | +procedure EnterBody(c, line: longint); | |
| 117 | 117 | procedure ReturnValue(var x: TItem); |
| 118 | 118 | procedure ReturnField(f: PObj; var x: TItem); |
| 119 | 119 | procedure ReturnJump; |
| @@ -1258,10 +1258,12 @@ | ||
| 1258 | 1258 | end; |
| 1259 | 1259 | end; |
| 1260 | 1260 | |
| 1261 | -procedure EnterBody(c: longint); | |
| 1261 | +{ The prologue, which is for the procedure's first line. } | |
| 1262 | +procedure EnterBody(c, line: longint); | |
| 1262 | 1263 | var i, top: longint; |
| 1263 | 1264 | begin |
| 1264 | 1265 | BeginChunk(c); |
| 1266 | + NoteLine(line); | |
| 1265 | 1267 | Push(RBP); |
| 1266 | 1268 | MovRR(RBP, RSP); |
| 1267 | 1269 | Put1($48); Put1($81); Put1($EC); { sub rsp, imm32 } |
| @@ -178,5 +178,6 @@ | ||
| 178 | 178 | Ud2; |
| 179 | 179 | EndChunk(start); |
| 180 | 180 | |
| 181 | + mainPath := path; | |
| 181 | 182 | WriteElf(outPath, entry); |
| 182 | 183 | end. |
| @@ -1145,6 +1145,7 @@ | ||
| 1145 | 1145 | procedure Statement; |
| 1146 | 1146 | var x, y: TItem; called: boolean; m: int64; c: TCall; |
| 1147 | 1147 | begin |
| 1148 | + NoteLine(CurLine); | |
| 1148 | 1149 | m := TempMark; |
| 1149 | 1150 | case tok of |
| 1150 | 1151 | sIdent: begin |
| @@ -1242,7 +1243,7 @@ | ||
| 1242 | 1243 | |
| 1243 | 1244 | procedure Declarations(top: boolean); forward; |
| 1244 | 1245 | |
| 1245 | -procedure Body(p: PObj; sig: PType); | |
| 1246 | +procedure Body(p: PObj; sig: PType; line: longint); | |
| 1246 | 1247 | var q, params, last, o: PObj; old: PType; |
| 1247 | 1248 | begin |
| 1248 | 1249 | OpenScope(NewScope); |
| @@ -1268,8 +1269,9 @@ | ||
| 1268 | 1269 | Expect(sBegin, 'begin'); |
| 1269 | 1270 | old := curResult; |
| 1270 | 1271 | curResult := sig^.base; |
| 1271 | - EnterBody(p^.chunk); | |
| 1272 | + EnterBody(p^.chunk, line); | |
| 1272 | 1273 | Statements; |
| 1274 | + NoteLine(CurLine); | |
| 1273 | 1275 | Expect(sEnd, 'end'); |
| 1274 | 1276 | EndProc(p^.chunk); |
| 1275 | 1277 | curResult := old; |
| @@ -1308,8 +1310,9 @@ | ||
| 1308 | 1310 | end; |
| 1309 | 1311 | |
| 1310 | 1312 | procedure ProcDecl; |
| 1311 | -var n: longint; exp: boolean; sig: PType; p: PObj; | |
| 1313 | +var n, line: longint; exp: boolean; sig: PType; p: PObj; | |
| 1312 | 1314 | begin |
| 1315 | + line := CurLine; | |
| 1313 | 1316 | Next; |
| 1314 | 1317 | IdentDef(n, exp); |
| 1315 | 1318 | sig := Signature; |
| @@ -1339,7 +1342,7 @@ | ||
| 1339 | 1342 | CodeBody(p, sig); |
| 1340 | 1343 | exit; |
| 1341 | 1344 | end; |
| 1342 | - if checkMode then SkipBody else Body(p, sig); | |
| 1345 | + if checkMode then SkipBody else Body(p, sig, line); | |
| 1343 | 1346 | if tok = sIdent then begin |
| 1344 | 1347 | if id <> n then Mark('the procedure is ' + NameOf(n)); |
| 1345 | 1348 | Next; |
| @@ -1583,11 +1586,12 @@ | ||
| 1583 | 1586 | var |
| 1584 | 1587 | st: TScanState; saved: array[0..MaxDepth - 1] of longint; d: longint; |
| 1585 | 1588 | 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; | |
| 1587 | 1590 | begin |
| 1588 | 1591 | SaveScan(st); |
| 1589 | 1592 | SaveScopes(d, saved); |
| 1590 | - oldMod := curModule; oldFile := fileChunk; oldCheck := checkMode; | |
| 1593 | + oldMod := curModule; oldFile := fileChunk; oldCheck := checkMode; oldPath := curFile; | |
| 1594 | + curFile := Name(path); | |
| 1591 | 1595 | if not Open(path) then Fatal('can''t read ' + path); |
| 1592 | 1596 | New(m); |
| 1593 | 1597 | FillChar(m^, SizeOf(TModule), 0); |
| @@ -1659,14 +1663,16 @@ | ||
| 1659 | 1663 | if tok = sBegin then begin |
| 1660 | 1664 | if checkMode then SkipBody |
| 1661 | 1665 | else begin |
| 1666 | + line := CurLine; | |
| 1662 | 1667 | Next; |
| 1663 | 1668 | body := NewChunk(secText, m^.sname, nameBody); |
| 1664 | 1669 | m^.body := body; |
| 1665 | 1670 | BeginProc(tBodySig, nil); |
| 1666 | 1671 | deferSlot := 0; |
| 1667 | 1672 | curResult := nil; |
| 1668 | - EnterBody(body); | |
| 1673 | + EnterBody(body, line); | |
| 1669 | 1674 | Statements; |
| 1675 | + NoteLine(CurLine); | |
| 1670 | 1676 | Expect(sEnd, 'end'); |
| 1671 | 1677 | EndProc(body); |
| 1672 | 1678 | end; |
| @@ -1686,7 +1692,7 @@ | ||
| 1686 | 1692 | if lastModule = nil then firstModule := m else lastModule^.next := m; |
| 1687 | 1693 | lastModule := m; |
| 1688 | 1694 | end; |
| 1689 | - curModule := oldMod; fileChunk := oldFile; checkMode := oldCheck; | |
| 1695 | + curModule := oldMod; fileChunk := oldFile; checkMode := oldCheck; curFile := oldPath; | |
| 1690 | 1696 | RestoreScopes(d, saved); |
| 1691 | 1697 | RestoreScan(st); |
| 1692 | 1698 | CompileFile := m; |
| @@ -61,6 +61,12 @@ | ||
| 61 | 61 | ext: boolean; { bss: the address of a C symbol, its name } |
| 62 | 62 | end; |
| 63 | 63 | |
| 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 | + | |
| 64 | 70 | TFixup = record |
| 65 | 71 | at: longint; { the rel32 field, as an offset in text } |
| 66 | 72 | chunk: longint; { the chunk it refers to } |
| @@ -69,6 +75,8 @@ | ||
| 69 | 75 | |
| 70 | 76 | PChunks = ^TChunkArray; |
| 71 | 77 | TChunkArray = array[0..MaxInt div SizeOf(TChunk) - 1] of TChunk; |
| 78 | + PLines = ^TLineArray; | |
| 79 | + TLineArray = array[0..MaxInt div SizeOf(TLine) - 1] of TLine; | |
| 72 | 80 | PFixups = ^TFixupArray; |
| 73 | 81 | TFixupArray = array[0..MaxInt div SizeOf(TFixup) - 1] of TFixup; |
| 74 | 82 | |
| @@ -81,6 +89,9 @@ | ||
| 81 | 89 | numChunks: longint; |
| 82 | 90 | fixups: PFixups; |
| 83 | 91 | numFixups: longint; |
| 92 | + lines: PLines; | |
| 93 | + numLines: longint; | |
| 94 | + curFile: longint; { the file being compiled, for NoteLine } | |
| 84 | 95 | |
| 85 | 96 | function NewChunk(sec: TSection; module, name: longint): longint; |
| 86 | 97 | procedure BeginChunk(c: longint); |
| @@ -88,6 +99,7 @@ | ||
| 88 | 99 | procedure SetData(c: longint; p: PBytes; n, align: longint); |
| 89 | 100 | procedure SetBss(c: longint; size, align: longint); |
| 90 | 101 | function ExternChunk(name: longint): longint; |
| 102 | +procedure NoteLine(line: longint); | |
| 91 | 103 | |
| 92 | 104 | procedure Put1(b: longint); |
| 93 | 105 | procedure Put4(v: longint); |
| @@ -152,7 +164,8 @@ | ||
| 152 | 164 | implementation |
| 153 | 165 | |
| 154 | 166 | var |
| 155 | - codeCap, roCap, chunkCap, fixCap: longint; | |
| 167 | + codeCap, roCap, chunkCap, fixCap, lineCap: longint; | |
| 168 | + curChunk: longint; | |
| 156 | 169 | |
| 157 | 170 | { Buffers and chunks } |
| 158 | 171 | |
| @@ -214,6 +227,7 @@ | ||
| 214 | 227 | begin |
| 215 | 228 | { code is aligned to 16 bytes, padded with int3 } |
| 216 | 229 | while pc and 15 <> 0 do Put1($CC); |
| 230 | + curChunk := c; | |
| 217 | 231 | chunks^[c].start := pc; |
| 218 | 232 | chunks^[c].align := 16; |
| 219 | 233 | chunks^[c].firstFix := numFixups; |
| @@ -244,6 +258,24 @@ | ||
| 244 | 258 | chunks^[c].align := align; |
| 245 | 259 | end; |
| 246 | 260 | |
| 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 | + | |
| 247 | 279 | { The quadword that holds the address of C's symbol name, filled in |
| 248 | 280 | when the program starts; one for each name. } |
| 249 | 281 | function ExternChunk(name: longint): longint; |
| @@ -642,4 +674,7 @@ | ||
| 642 | 674 | fixCap := 1 shl 12; |
| 643 | 675 | fixups := GetMem(fixCap * SizeOf(TFixup)); |
| 644 | 676 | numFixups := 0; |
| 677 | + lineCap := 1 shl 12; | |
| 678 | + lines := GetMem(lineCap * SizeOf(TLine)); | |
| 679 | + numLines := 0; | |
| 645 | 680 | end. |
| @@ -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" |