[v1,gdb/python] Bploc should try to return full path

Message ID 20250110221006.184094-1-simon.farre.cx@gmail.com
State New
Headers
Series [v1,gdb/python] Bploc should try to return full path |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Test passed

Commit Message

Simon Farre Jan. 10, 2025, 10:10 p.m. UTC
  Compilers often emit relative paths in the line number program,
relative to the build directory for that compilation unit (if it's
DWARF>=4 I think).

Therefore use symtab->fullname() when not null as this seemingly
has attempted path normalization for the symtab and only
fall back on symtab->filename which will never be null if that fails.

This has a much better UX. Applications may choose to expose
this name as a clickable link to some file, at which point
a non-normalized and non-absolute path would lead nowhere.

When I wrote this feature the first time, I don't think this
relative-to-cu-scheme was as prevalent in the output of gcc/clang
for DWARF.
---
 gdb/python/py-breakpoint.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
  

Comments

Andrew Burgess Jan. 13, 2025, 4:28 p.m. UTC | #1
Simon Farre <simon.farre.cx@gmail.com> writes:

> Compilers often emit relative paths in the line number program,
> relative to the build directory for that compilation unit (if it's
> DWARF>=4 I think).
>
> Therefore use symtab->fullname() when not null as this seemingly
> has attempted path normalization for the symtab and only
> fall back on symtab->filename which will never be null if that fails.

I think some of this code is showing its pre-C++ origins, but I wonder
if you'd be better using symtab_to_fullname() instead of accessing
symtab::fullname() directly?

It's not entirely clear, but I think, this will always return
non-nullptr, and will have set symtab::fullname() if needed.

Thanks,
Andrew
  

Patch

diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c
index 75f50e1f423..37152d32c0b 100644
--- a/gdb/python/py-breakpoint.c
+++ b/gdb/python/py-breakpoint.c
@@ -1643,9 +1643,10 @@  bplocpy_get_source_location (PyObject *py_self, void *closure)
       gdbpy_ref<> tup (PyTuple_New (2));
       if (tup == nullptr)
 	return nullptr;
-      /* symtab->filename is never NULL. */
-      gdbpy_ref<> filename
-	= host_string_to_python_string (self->bp_loc->symtab->filename);
+      const char *full = self->bp_loc->symtab->fullname ();
+      gdbpy_ref<> filename = full != nullptr
+        ? host_string_to_python_string (full)
+        : host_string_to_python_string (self->bp_loc->symtab->filename);
       if (filename == nullptr)
 	return nullptr;
       auto line = gdb_py_object_from_ulongest (self->bp_loc->line_number);