RE: [PATCH] Fix macro info lookup for binaries containing DWARFv5 line table

Tomar, Sourabh Singh via Gdb-patches
2021-05-12T17:31:16Z

[AMD Public Use]

Hello Simon,

Thanks for the review comments.

There was some configuration issue with my git send-mail thing*, apparently the patch arrived but missed
the reply to your review comments.

I'll try to address them here:

Comments:
> Sorry for the delay, there are just so many patches to look at.

No Problem, thanks for taking out time for reviewing this!

>Ok, I think you're right. We want line_header::file_file_name to return the path to the file, relative to the compilation directory. In DWARF5, at least with clang, the file at index 0 appears to always be >relative to the compilation directory (directory at index 0).
If I do:
$ clang-11 test.c -o test -gdwarf-5 -fdebug-macro && llvm-dwarfdump -F -color -debug-line test
I get:
....
If I move up a directory:
$ clang-11 gdb/test.c -o test -gdwarf-5 -fdebug-macro && llvm-dwarfdump -F -color -debug-line test
...
> So it looks like by returning the filename as-is, we'll indeed return a path to the file relative to the compilation directory.

Yes, thanks for acknowledging the issue.

> I made a test (took gdb.base/template.exp and modified it) with:
> standard_testfile
> file copy -force $srcdir/$subdir/$srcfile [standard_output_file $srcfile]
> gdb_compile outputs/gdb.base/template/template.c $binfile executable {debug}
> This copies the source file in the "output" directory of the test, and compiles it from there using a relative path. You can run it with clang & DWARF5 with:
> $ make check TESTS="gdb.base/template.exp" RUNTESTFLAGS="--target_board=unix/gdb:debug_flags=-gdwarf-5 CC_FOR_TARGET=clang"
> So I think you could make a test based on this. Note that the test should work with gcc too, where you need to use -g3 in order to get macro information. A few tests (gdb.base/info-macros.exp, >gdb.base/macscp.exp, gdb.base/style.exp,
>gdb.linespec/macro-relative.exp) already deal with this, you can take a look. Speaking of gdb.linespec/macro-relative.exp, it would be worth looking into it, it kind of looks related, but I am not sure.

Thanks for the pointers, I've added a minimal test case that passes after this patch.

> For next time, could you please try to send your patch using git-send-email? It makes it easier to comment inline.

Sure, patch is sent through git-send-email

Review comment addressed in this patch:
> Use present tense (e.g. Update, not Updated).
> Watch the indentation.
> You can shorten this comment to:
> /* The file at index 0 (valid starting with DWARF 5) is already relative
> to the compilation directory. */
> Use `file != 0`.

gdb/ChangeLog
2021-05-12 Sourabh Singh Tomar <SourabhSingh.Tomar@amd.com>

* dwarf2/line-header.c: Updated the comment and extended
check in function `line_header::file_file_name` for
DWARFv5 line tables.

gdb/testsuite/ChangeLog
2021-05-12 Sourabh Singh Tomar <SourabhSingh.Tomar@amd.com>

* gdb.base/dwarf5-macro.exp: Add new tests.
* gdb.base/dwarf5-macro.c: Likewise.

Thanks,
Sourabh Singh tomar

-----Original Message-----
From: Tomar, Sourabh Singh <SourabhSingh.Tomar@amd.com>
Sent: Wednesday, May 12, 2021 10:47 PM
To: gdb-patches@sourceware.org
Cc: Tomar, Sourabh Singh <SourabhSingh.Tomar@amd.com>
Subject: [PATCH] Fix macro info lookup for binaries containing DWARFv5 line table

Consider following C test case:
```
cat test.c
int main() {
int a = OUT;
}
```
Compiled as:
`clang -gdwarf-5 -fdebug-macro test.c`

Loading this bin into GDB and trying to access macro info, GDB is unable to extract and display macro info correctly.
```
$gdb a.out
(gdb) start
(gdb) info macro OUT
Temporary breakpoint 1 at 0x2016a8: file test.c, line 3.
Starting program: /home/a.out
(gdb) info macro OUT
The symbol `OUT' has no definition as a C/C++ preprocessor macro at /home/test.c:-1 ```

The problems starts when processing file and dir indexes from DWARFv5 line table. DWARFv5 specifies:
- The current directory is explicitly present in the
directories field.
- The current compilation filename is explicitly present and
has index 0.
dir entry 0 contains the absoute path(Not including source filename).
file entry 0 contains the source file name and refers dir 0.

As one may notice that dir index `0` will always contain absoulte path(Not including the file name as), this causes GDB to append the path + filename to construt macro info `main_file->filename`. This leads to overriding of the macro filename which will cause macro info lookup failure due to mismatch in `main_file->filename` and `sal.symtab->filename`.

This problem does not pops up with GCC compiled binaries, since GCC does not emits DWARFv5 compliant line tables.

Testing:
- NO regressing observed in `gdb.base` and associated macro based tests.

gdb/ChangeLog
2021-05-12 Sourabh Singh Tomar <SourabhSingh.Tomar@amd.com>

* dwarf2/line-header.c: Updated the comment and extended
check in function `line_header::file_file_name` for
DWARFv5 line tables.

gdb/testsuite/ChangeLog
2021-05-12 Sourabh Singh Tomar <SourabhSingh.Tomar@amd.com>

* gdb.base/dwarf5-macro.exp: Add new tests.
* gdb.base/dwarf5-macro.c: Likewise.

Change-Id: I09f39c8680fca382523f4ac72bcbd1018851b480
---
gdb/ChangeLog | 5 +++
gdb/dwarf2/line-header.c | 11 +++++--
gdb/testsuite/ChangeLog | 5 +++
gdb/testsuite/gdb.base/dwarf5-macro.c | 3 ++
gdb/testsuite/gdb.base/dwarf5-macro.exp | 43 +++++++++++++++++++++++++
5 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 gdb/testsuite/gdb.base/dwarf5-macro.c
create mode 100644 gdb/testsuite/gdb.base/dwarf5-macro.exp

diff --git a/gdb/ChangeLog b/gdb/ChangeLog index d564621f4c6..98650ff8a60 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,8 @@
+2021-05-12 Sourabh Singh Tomar <SourabhSingh.Tomar@amd.com>
+
+ * dwarf2/line-header.c: Update the comment and extended
+ check in function `line_header::file_file_name` for
+ DWARFv5 line tables.
2021-05-12 George Barrett <bob@bob131.so>

* NEWS (Guile API): Note the addition of the new procedure.
diff --git a/gdb/dwarf2/line-header.c b/gdb/dwarf2/line-header.c index 7575297f966..d79051d326e 100644
--- a/gdb/dwarf2/line-header.c
+++ b/gdb/dwarf2/line-header.c
@@ -64,7 +64,8 @@ gdb::unique_xmalloc_ptr<char> line_header::file_file_name (int file) const {
/* Is the file number a valid index into the line header's file name
- table? Remember that file numbers start with one, not zero. */
+ table? Remember that file numbers start with zero for DWARFv5 and one
+ for DWARFv4 . */
if (is_valid_file_index (file))
{
const file_entry *fe = file_name_at (file); @@ -72,7 +73,10 @@ line_header::file_file_name (int file) const
if (!IS_ABSOLUTE_PATH (fe->name))
{
const char *dir = fe->include_dir (this);
- if (dir != NULL)
+ /* The file at index 0 (valid starting with DWARF 5) is already
+ relative to the compilation directory. */
+
+ if (file != 0)
return gdb::unique_xmalloc_ptr<char> (concat (dir, SLASH_STRING,
fe->name,
(char *) NULL));
@@ -100,7 +104,8 @@ gdb::unique_xmalloc_ptr<char> line_header::file_full_name (int file, const char *comp_dir) const {
/* Is the file number a valid index into the line header's file name
- table? Remember that file numbers start with one, not zero. */
+ table? Remember that file numbers start with zero for DWARFv5 and one
+ for DWARFv4 . */
if (is_valid_file_index (file))
{
gdb::unique_xmalloc_ptr<char> relative = file_file_name (file); diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index e0f64965d94..66b395b1e4c 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2021-05-12 Sourabh Singh Tomar <SourabhSingh.Tomar@amd.com>
+
+ * gdb.base/dwarf5-macro.exp: Add new tests.
+ * gdb.base/dwarf5-macro.c: Likewise.
+
2021-05-12 George Barrett <bob@bob131.so>

* gdb.guile/scm-value.exp (test_value_in_inferior): Add test for diff --git a/gdb/testsuite/gdb.base/dwarf5-macro.c b/gdb/testsuite/gdb.base/dwarf5-macro.c
new file mode 100644
index 00000000000..0a7dc98c620
--- /dev/null
+++ b/gdb/testsuite/gdb.base/dwarf5-macro.c
@@ -0,0 +1,3 @@
+#define HELLO_GDB 1
+int main() {
+}
diff --git a/gdb/testsuite/gdb.base/dwarf5-macro.exp b/gdb/testsuite/gdb.base/dwarf5-macro.exp
new file mode 100644
index 00000000000..cf622cebe32
--- /dev/null
+++ b/gdb/testsuite/gdb.base/dwarf5-macro.exp
@@ -0,0 +1,43 @@
+# Copyright 2020-2021 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or #
+(at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, # but
+WITHOUT ANY WARRANTY; without even the implied warranty of #
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU
+General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License #
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+standard_testfile
+set additional_flags {debug}
+file copy -force $srcdir/$subdir/$srcfile [standard_output_file
+$srcfile] gdb_compile outputs/gdb.base/dwarf5-macro/dwarf5-macro.c
+$binfile executable {debug}
+
+get_compiler_info
+if { [test_compiler_info "gcc-*"] } {
+ set options "additional_flags=-g3"
+} elseif { [test_compiler_info "clang-*"] } {
+ set options "additional_flags=-gdwarf-5 -fdebug-macro"
+}
+
+if { [prepare_for_testing "failed to prepare" \
+ ${testfile} ${srcfile}] } {
+ return
+}
+
+if { ![runto_main] } {
+ untested "could not run to main"
+ return
+}
+
+set filepat {dwarf5-macro.[ch]}
+set definition {}
+set location {}
+set nonzero {[1-9][0-9]*}
+gdb_test "info macro HELLO_GDB" \
+ ".*define HELLO_GDB 1"
--
2.17.1