Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: "Tomar, Sourabh Singh via Gdb-patches" <gdb-patches@sourceware.org>
To: "gdb-patches@sourceware.org" <gdb-patches@sourceware.org>,
	"simon.marchi@polymtl.ca" <simon.marchi@polymtl.ca>
Subject: RE: [PATCH] Fix macro info lookup for binaries containing DWARFv5 line table
Date: Wed, 12 May 2021 17:31:07 +0000	[thread overview]
Message-ID: <DM4PR12MB52958A4A19DF19FCA3DE84369D529@DM4PR12MB5295.namprd12.prod.outlook.com> (raw)
In-Reply-To: <20210512171655.9463-1-SourabhSingh.Tomar@amd.com>

[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

  reply	other threads:[~2021-05-12 17:31 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-12 17:16 Sourabh Singh Tomar via Gdb-patches
2021-05-12 17:31 ` Tomar, Sourabh Singh via Gdb-patches [this message]
2021-05-13 15:47 ` Tom Tromey
2021-05-13 18:20 ` Simon Marchi via Gdb-patches
2021-05-14 14:56 ` Keith Seitz via Gdb-patches
2021-05-14 18:21   ` Tomar, Sourabh Singh via Gdb-patches
2021-05-14 18:50     ` Simon Marchi via Gdb-patches
2021-05-14 19:20       ` Tomar, Sourabh Singh via Gdb-patches
2021-05-14 20:56         ` Simon Marchi via Gdb-patches
2021-05-24 11:36   ` Tomar, Sourabh Singh via Gdb-patches
2021-05-24 18:47     ` Keith Seitz via Gdb-patches
2021-06-08 18:48       ` Keith Seitz via Gdb-patches
2021-06-22 17:01         ` [RFC] PING [Re: [PATCH] Fix macro info lookup for binaries containing DWARFv5 line table] Keith Seitz via Gdb-patches
2021-06-22 20:52           ` Simon Marchi via Gdb-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DM4PR12MB52958A4A19DF19FCA3DE84369D529@DM4PR12MB5295.namprd12.prod.outlook.com \
    --to=gdb-patches@sourceware.org \
    --cc=SourabhSingh.Tomar@amd.com \
    --cc=simon.marchi@polymtl.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox