From: Andrew Burgess Date: Mon, 17 Nov 2025 14:08:57 +0000 (+0000) Subject: gdb: display a symbol more often in multi-file list output X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=71458a6da11f8afe3f39b47656d9fe5237561c26;p=thirdparty%2Fbinutils-gdb.git gdb: display a symbol more often in multi-file list output 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 --- diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c index 43c1bc7a96f..cf5571c5419 100644 --- a/gdb/cli/cli-cmds.c +++ b/gdb/cli/cli-cmds.c @@ -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)), diff --git a/gdb/testsuite/gdb.base/list-multi-source.exp b/gdb/testsuite/gdb.base/list-multi-source.exp index 66f6582fdff..887ff961933 100644 --- a/gdb/testsuite/gdb.base/list-multi-source.exp +++ b/gdb/testsuite/gdb.base/list-multi-source.exp @@ -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. */}]" \