| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use debug::image; |
| 5 |
use io; |
| 6 |
use path; |
| 7 |
|
| 8 |
// Determines the file path, line number, and column number of a given address |
| 9 |
// in the program image. Returns void if unknown. The return value is statically |
| 10 |
// allocated. |
| 11 |
export fn addr_to_line( |
| 12 |
image: *image::image, |
| 13 |
addr: uintptr, |
| 14 |
) ((const str, uint, uint) | void | io::error | nomem) = { |
| 15 |
const dinfo_offs = match (arange_lookup(image, addr)) { |
| 16 |
case let offs: u64 => |
| 17 |
yield offs; |
| 18 |
case => |
| 19 |
return; // XXX: We could walk .debug_info I guess |
| 20 |
}; |
| 21 |
const dinfo = match (read_debug_info(image, dinfo_offs)?) { |
| 22 |
case let rd: debug_info_reader => |
| 23 |
yield rd; |
| 24 |
case => |
| 25 |
return; |
| 26 |
}; |
| 27 |
defer debug_info_finish(&dinfo); |
| 28 |
|
| 29 |
let comp_dir = ""; |
| 30 |
let stmt_list = 0u64, found = false; |
| 31 |
for (!found) { |
| 32 |
const entry = match (debug_info_next(&dinfo)) { |
| 33 |
case io::EOF => |
| 34 |
return; |
| 35 |
case nomem => |
| 36 |
return nomem; |
| 37 |
case let ent: entry => |
| 38 |
yield ent; |
| 39 |
}; |
| 40 |
defer entry_finish(&entry); |
| 41 |
|
| 42 |
if (entry.tag != DW_TAG_compile_unit) { |
| 43 |
continue; |
| 44 |
}; |
| 45 |
|
| 46 |
for (const field &.. entry.fields) { |
| 47 |
switch (field.attr) { |
| 48 |
case DW_AT_stmt_list => |
| 49 |
stmt_list = field.constant; |
| 50 |
found = true; |
| 51 |
case DW_AT_comp_dir => |
| 52 |
comp_dir = field.string; |
| 53 |
case => void; |
| 54 |
}; |
| 55 |
}; |
| 56 |
}; |
| 57 |
|
| 58 |
const prog = match (exec_line_program(image, stmt_list)) { |
| 59 |
case let prog: line_program => |
| 60 |
yield prog; |
| 61 |
case => |
| 62 |
return; |
| 63 |
}; |
| 64 |
defer line_program_finish(&prog); |
| 65 |
|
| 66 |
let last = line_state { ... }; |
| 67 |
for (const state => line_next(&prog)?) { |
| 68 |
defer last = state; |
| 69 |
|
| 70 |
if (state.file == 1) { |
| 71 |
continue; |
| 72 |
}; |
| 73 |
if (state.addr < addr) { |
| 74 |
continue; |
| 75 |
}; |
| 76 |
|
| 77 |
// If this is the first state we've seen, use it |
| 78 |
if (last.vm_loc != 0) { |
| 79 |
state = last; |
| 80 |
}; |
| 81 |
|
| 82 |
if (state.file == 0) { |
| 83 |
return; |
| 84 |
}; |
| 85 |
|
| 86 |
const file = &prog.head.files[state.file - 1]; |
| 87 |
static let path = path::buffer { ... }; |
| 88 |
|
| 89 |
path::set(&path)!; |
| 90 |
|
| 91 |
if (!path::abs(file.name)) { |
| 92 |
let dir = ""; |
| 93 |
if (file.dir != 0) { |
| 94 |
dir = prog.head.dirs[file.dir - 1]; |
| 95 |
if (!path::abs(dir) && comp_dir != "") { |
| 96 |
path::set(&path, comp_dir, dir)!; |
| 97 |
} else { |
| 98 |
path::set(&path, dir)!; |
| 99 |
}; |
| 100 |
} else if (comp_dir != "") { |
| 101 |
path::set(&path, comp_dir)!; |
| 102 |
}; |
| 103 |
}; |
| 104 |
|
| 105 |
path::push(&path, file.name)!; |
| 106 |
return (path::string(&path), state.line, state.column); |
| 107 |
}; |
| 108 |
}; |