public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: "Tomar, Sourabh Singh" <SourabhSingh.Tomar@amd.com>
To: "Tomar, Sourabh Singh via Gdb-patches" <gdb-patches@sourceware.org>
Cc: "George, Jini Susan" <JiniSusan.George@amd.com>,
	"Sharma, Alok Kumar" <AlokKumar.Sharma@amd.com>,
	"Kumar N, Bhuvanendra" <Bhuvanendra.KumarN@amd.com>,
	"Achra, Nitika" <Nitika.Achra@amd.com>,
	"E, Nagajyothi" <Nagajyothi.E@amd.com>
Subject: [PATCH] gdb-Fix-macro-info-lookup-for-binaries-containing-DWARFv5
Date: Fri, 26 Mar 2021 15:12:36 +0000	[thread overview]
Message-ID: <MW2PR12MB2474794D7AAA9F3DCF5A0EBD9D619@MW2PR12MB2474.namprd12.prod.outlook.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2574 bytes --]

[AMD Public Use]

Hello Everyone,

Requesting review on this patch, Please have a look.

Problem Description/Summary:
- clang emits DWARFv5 conformant line tables(ie. .debug_line section) which makes 2 things explicitly:
1. The current directory is explicitly present in the directories field and has index 0.
2. The current compilation filename is explicitly present and has index 0.

This leads to a subtle issue in GDB while accessing macro information:
Consider the following program:
```
#define OUT 4
int main() { int a = OUT;}
```
Compiled as `clang -gdwarf-5 -fdebug-macro test.c`
GDB session:
```
$gdb a.out -q
....
2       int main() { int a = OUT;}
(gdb) info macro OUT
The symbol `OUT' has no definition as a C/C++ preprocessor macro
at /home/test.c:-1
```

This issue cannot be reproduced using GCC compiler as GCC doesnot emit DWARFv5 compliant tables even
At `-gdwarf-5` flag.

Issue is stemming from line-header.c. When performing a file name lookup for macro filename GDB 
mistakenly appends FULL path after checking following condition, which as **one may notice** for 
directory index `0` will always evaluates `true` for DWARv5 line tables and `false` for DWARFv4 line tables.
```
If (dir != NULL)
return gdb::unique_xmalloc_ptr<char> (concat (dir, SLASH_STRING,
                                                          fe->name,
                                                          (char *) NULL));
```
Result of this, macro `filename` getting populated with fullname. Eventually macro lookup
results in failure due to mismatch in `main_file->filename` and   `sal.symtab->filename`.

This patch attempts to fix above problem by extending the check for file `0`, i.e for file `0`
do not append the fullname.

Testing:
- NO regressions in GDB testsuite before and after the fix.

We also tried add a test case for this fix, but was unsuccessful.
GDB test suite uses full path to the test files and this specific problem is 
not reproducible when user specifies full path. i.e
Not reproducible cases:
1. `clang -gdwarf-5 -fdebug-macro ./test.c` // macro visible inside GDB
2. `clang -gdwarf-5 -fdebug-macro /ABSOLUTE_PATH/test.c` // macro visible inside GDB

Reproducible:
1. `clang -gdwarf-5 -fdebug-macro test.c` //log shown above.

gdb/ChangeLog
2021-03-26  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.

Thank You,
Sourabh.

[-- Attachment #2: gdb-Fix-macro-info-lookup-for-binaries-containing-DWARFv.patch --]
[-- Type: application/octet-stream, Size: 4578 bytes --]

From c80a3e56e9f943844478fb34351a8c3decba413e Mon Sep 17 00:00:00 2001
From: Sourabh Singh Tomar <SourabhSingh.Tomar@amd.com>
Date: Wed, 24 Mar 2021 16:23:33 +0530
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.

2021-03-26  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/ChangeLog            |  6 ++++++
 gdb/dwarf2/line-header.c | 30 +++++++++++++++++++++++++++---
 2 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 173b08bf04b..1580690a2d7 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,9 @@
+2021-03-26  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.
+
 2021-03-25  Pedro Alves  <pedro@palves.net>
 
 	* gdb.server/bkpt-other-inferior.exp: Only enable remote output
diff --git a/gdb/dwarf2/line-header.c b/gdb/dwarf2/line-header.c
index 7575297f966..dad44550531 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,29 @@ line_header::file_file_name (int file) const
       if (!IS_ABSOLUTE_PATH (fe->name))
 	{
 	  const char *dir = fe->include_dir (this);
-	  if (dir != NULL)
+     /* clang  compiler emits DWARFv5 compliant line tables i.e
+	 For any C file compiled as:
+	clang -gdwarf-5 -fdebug-macro test.c
+
+	```
+	line table:
+	include_directories[  0] = "/home/"
+	file_names[  0]:
+	name: = "test.c"
+	dir_index: 0
+
+	macro table:
+	Referring to file index 0.
+	```
+	Now the 0 dir index will never be NULL for DWARFv5.
+	Do Not append the path to file name since this will override
+	the macro filename which will cause macro info lookup failure
+	due to mismatch in `main_file->filename` and
+	`sal.symtab->filename`.
+	Note that previously this check will always be `true` for
+	DWARFv5, since index 0 will never be NULL*/
+
+	  if (dir != NULL && file)
 	    return gdb::unique_xmalloc_ptr<char> (concat (dir, SLASH_STRING,
 							  fe->name,
 							  (char *) NULL));
@@ -100,7 +123,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);
-- 
2.17.1


             reply	other threads:[~2021-03-26 15:12 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-26 15:12 Tomar, Sourabh Singh [this message]
2021-04-03  6:50 ` Tomar, Sourabh Singh
2021-04-26 19:29 ` Simon Marchi

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=MW2PR12MB2474794D7AAA9F3DCF5A0EBD9D619@MW2PR12MB2474.namprd12.prod.outlook.com \
    --to=sourabhsingh.tomar@amd.com \
    --cc=AlokKumar.Sharma@amd.com \
    --cc=Bhuvanendra.KumarN@amd.com \
    --cc=JiniSusan.George@amd.com \
    --cc=Nagajyothi.E@amd.com \
    --cc=Nitika.Achra@amd.com \
    --cc=gdb-patches@sourceware.org \
    /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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).