Get breakpoint filename and line number from DWARF

This commit is contained in:
2025-03-12 11:37:23 +01:00
parent dc66bc84ae
commit d4fdd69c48
3 changed files with 34 additions and 16 deletions

View File

@@ -152,6 +152,7 @@ typedef struct {
HashTable brks; HashTable brks;
uintptr_t program_load_offset; uintptr_t program_load_offset;
HashTable js_descs; HashTable js_descs;
PLibelfinBinding plibelfin;
} Dbg; } Dbg;
siginfo_t dbg_get_siginfo(Dbg *dbg) siginfo_t dbg_get_siginfo(Dbg *dbg)
@@ -178,10 +179,9 @@ void dbg_handle_sigtrap(Dbg *dbg, siginfo_t info)
case SI_KERNEL: case SI_KERNEL:
case TRAP_BRKPT: case TRAP_BRKPT:
dbg_set_rip(dbg, dbg_get_rip(dbg) - 1); dbg_set_rip(dbg, dbg_get_rip(dbg) - 1);
ai = libelfin_wrap_info_from_rip(dbg_get_rip(dbg)); ai = libelfin_wrap_info_from_rip(dbg->plibelfin, dbg_get_rip(dbg) - (uint64_t)dbg->program_load_offset);
if (ai != NULL) { if (ai != NULL) {
LOG_INF("Hit breakpoint at 0x%"PRIxPTR", %s:%zu\n", dbg_get_rip(dbg), LOG_INF("Hit breakpoint at 0x%"PRIxPTR", %s:%zu\n", dbg_get_rip(dbg), ai->file, (size_t)ai->line);
ai->file, (size_t)ai->line);
libelfin_wrap_free_info(ai); libelfin_wrap_free_info(ai);
} else { } else {
LOG_INF("Hit breakpoint at 0x%"PRIxPTR"\n", dbg_get_rip(dbg)); LOG_INF("Hit breakpoint at 0x%"PRIxPTR"\n", dbg_get_rip(dbg));
@@ -538,7 +538,7 @@ void dbg_libelfin_wrap_init(Dbg *dbg)
return; return;
} }
libelfin_wrap_init(fileno(bin)); dbg->plibelfin = libelfin_wrap_get_binding(fileno(bin));
fclose(bin); fclose(bin);
} }
@@ -561,6 +561,7 @@ void dbg_deinit(Dbg *dbg)
js_freestate(dbg->js); js_freestate(dbg->js);
hashtable_deinit(&dbg->brks); hashtable_deinit(&dbg->brks);
hashtable_deinit(&dbg->js_descs); hashtable_deinit(&dbg->js_descs);
libelfin_wrap_free_binding(dbg->plibelfin);
} }
void dbg_loop(Dbg *dbg) void dbg_loop(Dbg *dbg)

View File

@@ -1,25 +1,40 @@
#include <inttypes.h>
#include <libelfin/dwarf/dwarf++.hh> #include <libelfin/dwarf/dwarf++.hh>
#include <libelfin/elf/elf++.hh> #include <libelfin/elf/elf++.hh>
#include "libelfin_wrap.h" #include "libelfin_wrap.h"
static elf::elf gelf; class LibelfinBinding
static dwarf::dwarf gdwarf;
DEBUGUS_EXTERNC void libelfin_wrap_init(int fd)
{ {
gelf = elf::elf{elf::create_mmap_loader(fd)}; public:
gdwarf = dwarf::dwarf{dwarf::elf::create_loader(gelf)}; LibelfinBinding(int fd)
: elf(elf::create_mmap_loader(fd)),
dwarf(dwarf::elf::create_loader(this->elf))
{
}
elf::elf elf;
dwarf::dwarf dwarf;
};
DEBUGUS_EXTERNC PLibelfinBinding libelfin_wrap_get_binding(int fd)
{
return (PLibelfinBinding)new LibelfinBinding(fd);
} }
DEBUGUS_EXTERNC AddrInfo *libelfin_wrap_info_from_rip(uint64_t rip) DEBUGUS_EXTERNC void libelfin_wrap_free_binding(PLibelfinBinding pbind)
{ {
for (auto &cu : gdwarf.compilation_units()) { delete (LibelfinBinding *)pbind;
}
DEBUGUS_EXTERNC AddrInfo *libelfin_wrap_info_from_rip(PLibelfinBinding pbind, uint64_t rip)
{
LibelfinBinding *bind = (LibelfinBinding *)pbind;
for (auto &cu : bind->dwarf.compilation_units()) {
if (die_pc_range(cu.root()).contains(rip)) { if (die_pc_range(cu.root()).contains(rip)) {
auto &lt = cu.get_line_table(); auto &lt = cu.get_line_table();
auto it = lt.find_address(rip); auto it = lt.find_address(rip);
if (it == lt.end()) { if (it == lt.end()) {
printf("ADDR NOT FOUND\n");
return NULL; return NULL;
} else { } else {
AddrInfo *ai = (AddrInfo *)malloc(sizeof(*ai)); AddrInfo *ai = (AddrInfo *)malloc(sizeof(*ai));
@@ -31,7 +46,6 @@ DEBUGUS_EXTERNC AddrInfo *libelfin_wrap_info_from_rip(uint64_t rip)
} }
} }
} }
printf("NO CUS\n");
return NULL; return NULL;
} }

View File

@@ -18,8 +18,11 @@ typedef struct {
uint64_t addr; uint64_t addr;
} AddrInfo; } AddrInfo;
DEBUGUS_EXTERNC void libelfin_wrap_init(int fd); typedef void * PLibelfinBinding;
DEBUGUS_EXTERNC AddrInfo *libelfin_wrap_info_from_rip(uint64_t rip);
DEBUGUS_EXTERNC PLibelfinBinding libelfin_wrap_get_binding(int fd);
DEBUGUS_EXTERNC void libelfin_wrap_free_binding(PLibelfinBinding pbind);
DEBUGUS_EXTERNC AddrInfo *libelfin_wrap_info_from_rip(PLibelfinBinding pbind, uint64_t rip);
DEBUGUS_EXTERNC void libelfin_wrap_free_info(AddrInfo *ai); DEBUGUS_EXTERNC void libelfin_wrap_free_info(AddrInfo *ai);
#endif // LIBELFIN_WRAP_H_ #endif // LIBELFIN_WRAP_H_