From d4fdd69c48c02d33dd5fc6f256c5ea3c43178fe6 Mon Sep 17 00:00:00 2001 From: kamil Date: Wed, 12 Mar 2025 11:37:23 +0100 Subject: [PATCH] Get breakpoint filename and line number from DWARF --- debugus.c | 9 +++++---- libelfin_wrap.cpp | 34 ++++++++++++++++++++++++---------- libelfin_wrap.h | 7 +++++-- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/debugus.c b/debugus.c index dcc60d3..ee26696 100644 --- a/debugus.c +++ b/debugus.c @@ -152,6 +152,7 @@ typedef struct { HashTable brks; uintptr_t program_load_offset; HashTable js_descs; + PLibelfinBinding plibelfin; } 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 TRAP_BRKPT: 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) { - LOG_INF("Hit breakpoint at 0x%"PRIxPTR", %s:%zu\n", dbg_get_rip(dbg), - ai->file, (size_t)ai->line); + LOG_INF("Hit breakpoint at 0x%"PRIxPTR", %s:%zu\n", dbg_get_rip(dbg), ai->file, (size_t)ai->line); libelfin_wrap_free_info(ai); } else { LOG_INF("Hit breakpoint at 0x%"PRIxPTR"\n", dbg_get_rip(dbg)); @@ -538,7 +538,7 @@ void dbg_libelfin_wrap_init(Dbg *dbg) return; } - libelfin_wrap_init(fileno(bin)); + dbg->plibelfin = libelfin_wrap_get_binding(fileno(bin)); fclose(bin); } @@ -561,6 +561,7 @@ void dbg_deinit(Dbg *dbg) js_freestate(dbg->js); hashtable_deinit(&dbg->brks); hashtable_deinit(&dbg->js_descs); + libelfin_wrap_free_binding(dbg->plibelfin); } void dbg_loop(Dbg *dbg) diff --git a/libelfin_wrap.cpp b/libelfin_wrap.cpp index 3d6682a..6048b07 100644 --- a/libelfin_wrap.cpp +++ b/libelfin_wrap.cpp @@ -1,25 +1,40 @@ +#include #include #include #include "libelfin_wrap.h" -static elf::elf gelf; -static dwarf::dwarf gdwarf; - -DEBUGUS_EXTERNC void libelfin_wrap_init(int fd) +class LibelfinBinding { - gelf = elf::elf{elf::create_mmap_loader(fd)}; - gdwarf = dwarf::dwarf{dwarf::elf::create_loader(gelf)}; + public: + 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)) { auto < = cu.get_line_table(); auto it = lt.find_address(rip); if (it == lt.end()) { - printf("ADDR NOT FOUND\n"); return NULL; } else { 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; } diff --git a/libelfin_wrap.h b/libelfin_wrap.h index dc2d05b..4715021 100644 --- a/libelfin_wrap.h +++ b/libelfin_wrap.h @@ -18,8 +18,11 @@ typedef struct { uint64_t addr; } AddrInfo; -DEBUGUS_EXTERNC void libelfin_wrap_init(int fd); -DEBUGUS_EXTERNC AddrInfo *libelfin_wrap_info_from_rip(uint64_t rip); +typedef void * PLibelfinBinding; + +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); #endif // LIBELFIN_WRAP_H_