]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: display a symbol more often in multi-file list output
authorAndrew Burgess <aburgess@redhat.com>
Mon, 17 Nov 2025 14:08:57 +0000 (14:08 +0000)
committerAndrew Burgess <aburgess@redhat.com>
Fri, 5 Dec 2025 10:40:14 +0000 (10:40 +0000)
I noticed that when a command line 'list foo.c:10' displays multiple
files, the symbol would always be shown as "???", e.g.:

  file: "/tmp/foo.c", line number: 10, symbol: "???"

this is because, when the symtab_and_line is created for the
'foo.c:10', the pc and symbol are never filled in.

In this commit, I propose that, when we decide that the above header
line needs to be printed, we should attempt to lookup a symbol for the
relevant line, and if one is found, we can use that.

The symbol lookup is done by first calling find_pc_for_line, and then
using find_symbol_for_pc to find a suitable symbol.

Approved-By: Tom Tromey <tom@tromey.com>
gdb/cli/cli-cmds.c
gdb/testsuite/gdb.base/list-multi-source.exp

index 43c1bc7a96f2e1d57b5c3f3f7213d5e304244f34..cf5571c541974992d5b3d3e84594a41301c360ad 100644 (file)
@@ -2182,6 +2182,14 @@ print_sal_location (const symtab_and_line &sal)
   const char *sym_name = NULL;
   if (sal.symbol != NULL)
     sym_name = sal.symbol->print_name ();
+  else if (CORE_ADDR line_pc;
+          find_pc_for_line (sal.symtab, sal.line, &line_pc))
+    {
+      struct symbol *sym = find_symbol_for_pc (line_pc);
+      if (sym != nullptr)
+       sym_name = sym->print_name ();
+    }
+
   gdb_printf (_("file: \"%ps\", line number: %ps, symbol: \"%s\"\n"),
              styled_string (file_name_style.style (),
                             symtab_to_filename_for_display (sal.symtab)),
index 66f6582fdff99fabdc1f7f3106b2eee984a788de..887ff961933e2d3f981fad1d43249f8ae0941017 100644 (file)
@@ -97,13 +97,13 @@ set last_linenum [expr {$linenum + 4}]
 # List using FILE:LINE for a filename that is ambiguous.
 gdb_test "list foo.c:$linenum" \
     [multi_line \
-        "file: \"\[^\r\n\]+/a/foo.c\", line number: $linenum, symbol: \"\[^\r\n\]+\"" \
+        "file: \"\[^\r\n\]+/a/foo.c\", line number: $linenum, symbol: \"get_value_common\"" \
         "$first_linenum\\s+\[^\r\n\]+" \
         ".*" \
         "$linenum\\s+[string_to_regexp {return 3;      /* List this line.  */}]" \
         ".*" \
         "$last_linenum\\s+\[^\r\n\]+" \
-        "file: \"\[^\r\n\]+/b/foo.c\", line number: $linenum, symbol: \"\[^\r\n\]+\"" \
+        "file: \"\[^\r\n\]+/b/foo.c\", line number: $linenum, symbol: \"get_value_common\"" \
         "$first_linenum\\s+\[^\r\n\]+" \
         ".*" \
         "$linenum\\s+[string_to_regexp {return -3;     /* List this line.  */}]" \