public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/3] [gdb/symtab] Fix some issues with macro complaints
@ 2024-06-26 18:50 Tom de Vries
  2024-06-26 18:50 ` [PATCH 1/3] [gdb/symtab] Emit malformed macro definition complaint once Tom de Vries
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Tom de Vries @ 2024-06-26 18:50 UTC (permalink / raw)
  To: gdb-patches

With a minimal test-case test.c:
...
int main (void) { return 0; }
...
compiled like this:
...
$ gcc test.c -g3 -gdwarf-5
...
I get:
...
$ gdb -q -batch -iex "set complaints 100" a.out -ex "p main"
During symbol reading: debug info gives source 2 included from file at zero line 0
During symbol reading: debug info gives command-line macro definition with non-zero line 19: _STDC_PREDEF_H 1
During symbol reading: debug info gives command-line macro definition with non-zero line 38: __STDC_IEC_559__ 1
During symbol reading: debug info gives command-line macro definition with non-zero line 39: __STDC_IEC_60559_BFP__ 201404L
During symbol reading: debug info gives command-line macro definition with non-zero line 48: __STDC_IEC_559_COMPLEX__ 1
During symbol reading: debug info gives command-line macro definition with non-zero line 49: __STDC_IEC_60559_COMPLEX__ 201404L
During symbol reading: debug info gives command-line macro definition with non-zero line 62: __STDC_ISO_10646__ 201706L
$1 = {int (void)} 0x4101ac <main>
...

These complaints seemed unnecessary to me, so I wrote some patches to get rid of
these.

There are three patches in the series:
- [gdb/symtab] Emit malformed macro definition complaint once
- [gdb/symtab] Fix incorrect complaint for imported definition
- [gdb/symtab] Don't complain about stdc-predef.h include

The first patch introduces a dwarf assembly test-case testing a fair amount
of complaints for the .debug_macro section.

That also happens to trigger a duplicate complaint, so a fix for that is
included.

The last two patches get rid of the complaints shown above.

Tom de Vries (3):
  [gdb/symtab] Emit malformed macro definition complaint once
  [gdb/symtab] Fix incorrect complaint for imported definition
  [gdb/symtab] Don't complain about stdc-predef.h include

 gdb/dwarf2/macro.c                            |  42 ++--
 gdb/testsuite/gdb.dwarf2/macro-complaints.exp | 213 ++++++++++++++++++
 gdb/testsuite/lib/dwarf.exp                   |  12 +-
 3 files changed, 250 insertions(+), 17 deletions(-)
 create mode 100644 gdb/testsuite/gdb.dwarf2/macro-complaints.exp


base-commit: cbccccfdf19a388abb10579aea42c8b35c25c933
-- 
2.35.3


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] [gdb/symtab] Emit malformed macro definition complaint once
  2024-06-26 18:50 [PATCH 0/3] [gdb/symtab] Fix some issues with macro complaints Tom de Vries
@ 2024-06-26 18:50 ` Tom de Vries
  2024-06-26 18:50 ` [PATCH 2/3] [gdb/symtab] Fix incorrect complaint for imported definition Tom de Vries
  2024-06-26 18:50 ` [PATCH 3/3] [gdb/symtab] Don't complain about stdc-predef.h include Tom de Vries
  2 siblings, 0 replies; 4+ messages in thread
From: Tom de Vries @ 2024-06-26 18:50 UTC (permalink / raw)
  To: gdb-patches

Add a test-case gdb.dwarf2/macro-complaints.exp, that checks complaints for the
.debug_macro section.

For one malformed macro definition, I get two identical complaints:
...
During symbol reading: macro debug info contains a malformed macro definition:^M
`M1_11_MALFORMED(ARG'^M
During symbol reading: macro debug info contains a malformed macro definition:^M
`M1_11_MALFORMED(ARG'^M
...

Fix this by bailing out after the first one.

[ This causes a memory leak, addressed by this proposed patch (
https://sourceware.org/pipermail/gdb-patches/2024-June/210218.html ).  So, the
idea is to commit this series after that one. ]

Tested on aarch64-linux.
---
 gdb/dwarf2/macro.c                            |   5 +-
 gdb/testsuite/gdb.dwarf2/macro-complaints.exp | 194 ++++++++++++++++++
 2 files changed, 198 insertions(+), 1 deletion(-)
 create mode 100644 gdb/testsuite/gdb.dwarf2/macro-complaints.exp

diff --git a/gdb/dwarf2/macro.c b/gdb/dwarf2/macro.c
index bc781c2cb92..495f30195fb 100644
--- a/gdb/dwarf2/macro.c
+++ b/gdb/dwarf2/macro.c
@@ -178,7 +178,10 @@ parse_macro_definition (struct macro_source_file *file, int line,
 	p++;
 
       if (! *p || p == arg_start)
-	dwarf2_macro_malformed_definition_complaint (body);
+	{
+	  dwarf2_macro_malformed_definition_complaint (body);
+	  return;
+	}
       else
 	{
 	  /* Make sure argv has room for the new argument.  */
diff --git a/gdb/testsuite/gdb.dwarf2/macro-complaints.exp b/gdb/testsuite/gdb.dwarf2/macro-complaints.exp
new file mode 100644
index 00000000000..4e74c153a83
--- /dev/null
+++ b/gdb/testsuite/gdb.dwarf2/macro-complaints.exp
@@ -0,0 +1,194 @@
+# This testcase is part of GDB, the GNU debugger.
+
+# Copyright 2024 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/>.
+
+# Test complaints about .debug_macro section.
+
+load_lib dwarf.exp
+
+require dwarf2_support
+
+standard_testfile main.c .S
+
+lassign [function_range main $srcdir/$subdir/$srcfile] \
+    main_start main_len
+
+set asm_file [standard_output_file $srcfile2]
+
+set line [gdb_get_line_number "return 0;"]
+
+Dwarf::assemble $asm_file {
+    declare_labels L cu_macro1
+
+    cu {} {
+	DW_TAG_compile_unit {
+	    {DW_AT_name $::srcfile}
+	    {DW_AT_macros $cu_macro1 DW_FORM_sec_offset}
+	    {DW_AT_stmt_list $L DW_FORM_sec_offset}
+	} {
+	    declare_labels int_type
+
+	    int_type: DW_TAG_base_type {
+		{DW_AT_byte_size 4 DW_FORM_sdata}
+		{DW_AT_encoding  @DW_ATE_signed}
+		{DW_AT_name int}
+	    }
+	    DW_TAG_subprogram {
+		{MACRO_AT_func {main}}
+		{type :$int_type}
+	    }
+	}
+    }
+
+    lines {version 2} L {
+	file_name $::srcfile 1
+	program {
+	    DW_LNE_set_address $::main_start
+	    line $::line
+	    DW_LNS_copy
+	    DW_LNE_set_address "$::main_start + $::main_len"
+	    DW_LNE_end_sequence
+	}
+    }
+
+    # Define the .debug_macro section.
+    macro {
+	cu_macro1: unit {
+	    "debug-line-offset-label" $L
+	} {
+	    define 0 "M1_01_BUILTIN_OK 1"
+	    define 1 "M1_02_BUILTIN_BADLINE 1"
+
+	    start_file 0 1
+
+	    define 1 "M1_03_OK 1"
+	    define 0 "M1_04_BADLINE 1"
+
+	    start_file 1 1234
+	    define 1 "M1_05_BADFILE 1"
+	    end_file
+
+	    define 1 "M1_06_OK "
+	    define 1 "M1_07_MALFORMED"
+	    define 1 "M1_08_OK() 1"
+	    define 1 "M1_09_OK(ARG) (ARG)"
+	    define 1 "M1_10_OK(ARG1,ARG2) (ARG1+ARG2)"
+
+	    define 1 "M1_11_MALFORMED(ARG"
+	    define 1 "M1_12_MALFORMED(ARG,"
+	    define 1 "M1_13_MALFORMED(ARG,)"
+	    define 1 "M1_14_MALFORMED()1"
+
+	    end_file
+	}
+    }
+}
+
+if { [prepare_for_testing "failed to prepare" $testfile \
+	  [list $srcfile $asm_file] {nodebug}] } {
+    return
+}
+
+set re_complaint1 \
+    "debug info gives command-line macro definition with non-zero line 1: M1_02_BUILTIN_BADLINE 1"
+
+set re_complaint2 \
+    "debug info gives in-file macro definition with zero line 0: M1_04_BADLINE 1"
+
+set re_complaint3 \
+    [string_to_regexp "bad file number in macro information (1234)"]
+
+set re_complaint4 \
+    [multi_line \
+	 "macro debug info contains a malformed macro definition:" \
+	 "`M1_07_MALFORMED'"]
+
+set re_complaint5 \
+    [multi_line \
+	 "macro debug info contains a malformed macro definition:" \
+	 [string_to_regexp "`M1_11_MALFORMED(ARG'"]]
+
+set re_complaint6 \
+    [multi_line \
+	 "macro debug info contains a malformed macro definition:" \
+	 [string_to_regexp "`M1_12_MALFORMED(ARG,'"]]
+
+set re_complaint7 \
+    [multi_line \
+	 "macro debug info contains a malformed macro definition:" \
+	 [string_to_regexp "`M1_13_MALFORMED(ARG,)'"]]
+
+set re_complaint8 \
+    [multi_line \
+	 "macro debug info contains a malformed macro definition:" \
+	 [string_to_regexp "`M1_14_MALFORMED()1'"]]
+
+set prefix \
+    "During symbol reading"
+
+set re \
+    [multi_line \
+	 "$prefix: $re_complaint1" \
+	 "$prefix: $re_complaint2" \
+	 "$prefix: $re_complaint3" \
+	 "$prefix: $re_complaint4" \
+	 "$prefix: $re_complaint5" \
+	 "$prefix: $re_complaint6" \
+	 "$prefix: $re_complaint7" \
+	 "$prefix: $re_complaint8" \
+	 [string cat [string_to_regexp {$}] "$decimal = \[^\r\n\]+"]]
+
+with_complaints 10 {
+    gdb_test "print main" ^$re "complaints"
+}
+
+set re_explicit \
+    [multi_line \
+	 "Defined at $srcfile:0" \
+	 "-DM1_01_BUILTIN_OK=1" \
+	 "Defined at $srcfile:1" \
+	 "#define M1_02_BUILTIN_BADLINE 1" \
+	 "Defined at $srcfile:1" \
+	 "#define M1_03_OK 1" \
+	 "Defined at $srcfile:0" \
+	 "-DM1_04_BADLINE=1" \
+	 "Defined at <bad macro file number 1234>:1" \
+	 "  included at $srcfile:1" \
+	 "#define M1_05_BADFILE 1" \
+	 "Defined at $srcfile:1" \
+	 "#define M1_06_OK " \
+	 "Defined at $srcfile:1" \
+	 "#define M1_07_MALFORMED " \
+	 "Defined at $srcfile:1" \
+	 [string_to_regexp "#define M1_08_OK() 1"] \
+	 "Defined at $srcfile:1" \
+	 [string_to_regexp "#define M1_09_OK(ARG) (ARG)"] \
+	 "Defined at $srcfile:1" \
+	 [string_to_regexp "#define M1_10_OK(ARG1, ARG2) (ARG1+ARG2)"] \
+	 "Defined at $srcfile:1" \
+	 [string_to_regexp "#define M1_13_MALFORMED(ARG) "]]
+
+set re_implicit \
+    [multi_line \
+	 "Defined at $srcfile:-1" \
+	 "#define __FILE__ \"$srcfile\"" \
+	 "Defined at $srcfile:-1" \
+	 "#define __LINE__ $line"]
+
+gdb_test "info macros $line" \
+    [multi_line \
+	 $re_explicit \
+	 $re_implicit]
-- 
2.35.3


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/3] [gdb/symtab] Fix incorrect complaint for imported definition
  2024-06-26 18:50 [PATCH 0/3] [gdb/symtab] Fix some issues with macro complaints Tom de Vries
  2024-06-26 18:50 ` [PATCH 1/3] [gdb/symtab] Emit malformed macro definition complaint once Tom de Vries
@ 2024-06-26 18:50 ` Tom de Vries
  2024-06-26 18:50 ` [PATCH 3/3] [gdb/symtab] Don't complain about stdc-predef.h include Tom de Vries
  2 siblings, 0 replies; 4+ messages in thread
From: Tom de Vries @ 2024-06-26 18:50 UTC (permalink / raw)
  To: gdb-patches

With a minimal test-case test.c:
...
int main (void) { return 0; }
...
compiled like this:
...
$ gcc test.c -g3 -gdwarf-5
...
I get:
...
$ gdb -q -batch -iex "set complaints 100" a.out -ex "p main"
During symbol reading: debug info gives source 2 included from file at zero line 0
During symbol reading: debug info gives command-line macro definition with non-zero line 19: _STDC_PREDEF_H 1
During symbol reading: debug info gives command-line macro definition with non-zero line 38: __STDC_IEC_559__ 1
During symbol reading: debug info gives command-line macro definition with non-zero line 39: __STDC_IEC_60559_BFP__ 201404L
During symbol reading: debug info gives command-line macro definition with non-zero line 48: __STDC_IEC_559_COMPLEX__ 1
During symbol reading: debug info gives command-line macro definition with non-zero line 49: __STDC_IEC_60559_COMPLEX__ 201404L
During symbol reading: debug info gives command-line macro definition with non-zero line 62: __STDC_ISO_10646__ 201706L
$1 = {int (void)} 0x4101ac <main>
...

The complaint about _STDC_PREDEF_H is incorrect.

The macro is defined in this macro unit:
...

  Offset:                      0xb1c
  Version:                     5
  Offset size:                 4

 DW_MACRO_define_strp - lineno : 19 macro : _STDC_PREDEF_H 1
...
which is imported from here:
...
Contents of the .debug_macro section:

  Offset:                      0
  Version:                     5
  Offset size:                 4
  Offset into .debug_line:     0

 DW_MACRO_import - offset : 0x1a
 DW_MACRO_start_file - lineno: 0 filenum: 1
 DW_MACRO_start_file - lineno: 0 filenum: 2
 DW_MACRO_import - offset : 0xb1c
 DW_MACRO_end_file
 DW_MACRO_end_file
...

The import happens after the first start_file, which means that the imported
definition for _STDC_PREDEF_H is not a "command-line macro definition".

This before/after first start_file concept is tracked in the variable
at_commandline, and the problem is that it's reset when processing an import.

Fix this by not resetting at_commandline when processing an import.

Tested on aarch64-linux.
---
 gdb/dwarf2/macro.c                            | 28 ++++++++++---------
 gdb/testsuite/gdb.dwarf2/macro-complaints.exp | 19 +++++++++++--
 gdb/testsuite/lib/dwarf.exp                   | 12 +++++++-
 3 files changed, 43 insertions(+), 16 deletions(-)

diff --git a/gdb/dwarf2/macro.c b/gdb/dwarf2/macro.c
index 495f30195fb..f72201ebc08 100644
--- a/gdb/dwarf2/macro.c
+++ b/gdb/dwarf2/macro.c
@@ -442,11 +442,11 @@ dwarf_decode_macro_bytes (dwarf2_per_objfile *per_objfile,
 			  struct dwarf2_section_info *str_section,
 			  struct dwarf2_section_info *str_offsets_section,
 			  std::optional<ULONGEST> str_offsets_base,
-			  htab_t include_hash, struct dwarf2_cu *cu)
+			  htab_t include_hash, struct dwarf2_cu *cu,
+			  int &at_commandline)
 {
   struct objfile *objfile = per_objfile->objfile;
   enum dwarf_macro_record_type macinfo_type;
-  int at_commandline;
   const gdb_byte *opcode_definitions[256];
 
   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
@@ -457,15 +457,6 @@ dwarf_decode_macro_bytes (dwarf2_per_objfile *per_objfile,
       return;
     }
 
-  /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
-     GDB is still reading the definitions from command line.  First
-     DW_MACINFO_start_file will need to be ignored as it was already executed
-     to create CURRENT_FILE for the main source holding also the command line
-     definitions.  On first met DW_MACINFO_start_file this flag is reset to
-     normally execute all the remaining DW_MACINFO_start_file macinfos.  */
-
-  at_commandline = 1;
-
   do
     {
       /* Do we at least have room for a macinfo type byte?  */
@@ -757,7 +748,8 @@ dwarf_decode_macro_bytes (dwarf2_per_objfile *per_objfile,
 					  current_file, lh, section,
 					  section_is_gnu, is_dwz, offset_size,
 					  str_section, str_offsets_section,
-					  str_offsets_base, include_hash, cu);
+					  str_offsets_base, include_hash, cu,
+					  at_commandline);
 
 		htab_remove_elt (include_hash, (void *) new_mac_ptr);
 	      }
@@ -959,8 +951,18 @@ dwarf_decode_macros (dwarf2_per_objfile *per_objfile,
   mac_ptr = section->buffer + offset;
   slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
   *slot = (void *) mac_ptr;
+
+  /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
+     GDB is still reading the definitions from command line.  First
+     DW_MACINFO_start_file will need to be ignored as it was already executed
+     to create CURRENT_FILE for the main source holding also the command line
+     definitions.  On first met DW_MACINFO_start_file this flag is reset to
+     normally execute all the remaining DW_MACINFO_start_file macinfos.  */
+  int at_commandline = 1;
+
   dwarf_decode_macro_bytes (per_objfile, builder, abfd, mac_ptr, mac_end,
 			    current_file, lh, section, section_is_gnu, 0,
 			    offset_size, str_section, str_offsets_section,
-			    str_offsets_base, include_hash.get (), cu);
+			    str_offsets_base, include_hash.get (), cu,
+			    at_commandline);
 }
diff --git a/gdb/testsuite/gdb.dwarf2/macro-complaints.exp b/gdb/testsuite/gdb.dwarf2/macro-complaints.exp
index 4e74c153a83..1a2dde36de0 100644
--- a/gdb/testsuite/gdb.dwarf2/macro-complaints.exp
+++ b/gdb/testsuite/gdb.dwarf2/macro-complaints.exp
@@ -31,7 +31,7 @@ set asm_file [standard_output_file $srcfile2]
 set line [gdb_get_line_number "return 0;"]
 
 Dwarf::assemble $asm_file {
-    declare_labels L cu_macro1
+    declare_labels L cu_macro1 cu_macro2
 
     cu {} {
 	DW_TAG_compile_unit {
@@ -92,8 +92,15 @@ Dwarf::assemble $asm_file {
 	    define 1 "M1_13_MALFORMED(ARG,)"
 	    define 1 "M1_14_MALFORMED()1"
 
+	    import $cu_macro2
 	    end_file
 	}
+
+	cu_macro2: unit {
+	} {
+	    define 1 "M2_1_IMPORTED_OK 1"
+	    define 0 "M2_2_IMPORTED_BAD 2"
+	}
     }
 }
 
@@ -136,6 +143,9 @@ set re_complaint8 \
 	 "macro debug info contains a malformed macro definition:" \
 	 [string_to_regexp "`M1_14_MALFORMED()1'"]]
 
+set re_complaint9 \
+    "debug info gives in-file macro definition with zero line 0: M2_2_IMPORTED_BAD 2"
+
 set prefix \
     "During symbol reading"
 
@@ -149,6 +159,7 @@ set re \
 	 "$prefix: $re_complaint6" \
 	 "$prefix: $re_complaint7" \
 	 "$prefix: $re_complaint8" \
+	 "$prefix: $re_complaint9" \
 	 [string cat [string_to_regexp {$}] "$decimal = \[^\r\n\]+"]]
 
 with_complaints 10 {
@@ -179,7 +190,11 @@ set re_explicit \
 	 "Defined at $srcfile:1" \
 	 [string_to_regexp "#define M1_10_OK(ARG1, ARG2) (ARG1+ARG2)"] \
 	 "Defined at $srcfile:1" \
-	 [string_to_regexp "#define M1_13_MALFORMED(ARG) "]]
+	 [string_to_regexp "#define M1_13_MALFORMED(ARG) "] \
+	 "Defined at $srcfile:1" \
+	 "#define M2_1_IMPORTED_OK 1" \
+	 "Defined at $srcfile:0" \
+	 "-DM2_2_IMPORTED_BAD=2"]
 
 set re_implicit \
     [multi_line \
diff --git a/gdb/testsuite/lib/dwarf.exp b/gdb/testsuite/lib/dwarf.exp
index 995cdcac941..466ae033503 100644
--- a/gdb/testsuite/lib/dwarf.exp
+++ b/gdb/testsuite/lib/dwarf.exp
@@ -2249,9 +2249,10 @@ namespace eval Dwarf {
 	with_override Dwarf::define_strp Dwarf::_macro_unit_define_strp {
 	with_override Dwarf::define Dwarf::_macro_unit_define {
 	with_override Dwarf::start_file Dwarf::_macro_unit_start_file {
+	with_override Dwarf::import Dwarf::_macro_unit_import {
 	with_override Dwarf::end_file Dwarf::_macro_unit_end_file {
 	    uplevel $body
-	}}}}
+	}}}}}
 
 	_op .byte 0x0 "# End macro unit"
     }
@@ -2292,6 +2293,15 @@ namespace eval Dwarf {
 	_op .uleb128 $file_idx
     }
 
+    # Emit a DW_MACRO_import entry.
+
+    proc _macro_unit_import { offset } {
+	_op .byte 0x7 "DW_MACRO_import"
+
+	variable _mu_offset_size
+	_op_offset $_mu_offset_size "$offset" "import"
+    }
+
     # Emit a DW_MACRO_end_file entry.
 
     proc _macro_unit_end_file {} {
-- 
2.35.3


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 3/3] [gdb/symtab] Don't complain about stdc-predef.h include
  2024-06-26 18:50 [PATCH 0/3] [gdb/symtab] Fix some issues with macro complaints Tom de Vries
  2024-06-26 18:50 ` [PATCH 1/3] [gdb/symtab] Emit malformed macro definition complaint once Tom de Vries
  2024-06-26 18:50 ` [PATCH 2/3] [gdb/symtab] Fix incorrect complaint for imported definition Tom de Vries
@ 2024-06-26 18:50 ` Tom de Vries
  2 siblings, 0 replies; 4+ messages in thread
From: Tom de Vries @ 2024-06-26 18:50 UTC (permalink / raw)
  To: gdb-patches

With a minimal test-case test.c:
...
int main (void) { return 0; }
...
compiled like this:
...
$ gcc test.c -g3 -gdwarf-5
...
I get:
...
$ gdb -q -batch -iex "set complaints 100" a.out -ex "p main"
During symbol reading: debug info gives source 2 included from file at zero line 0
$1 = {int (void)} 0x4101ac <main>
...

The complaint is about the second start_file here:
...
Contents of the .debug_macro section:

  Offset:                      0
  Version:                     5
  Offset size:                 4
  Offset into .debug_line:     0

 DW_MACRO_import - offset : 0x1a
 DW_MACRO_start_file - lineno: 0 filenum: 1
 DW_MACRO_start_file - lineno: 0 filenum: 2
 DW_MACRO_import - offset : 0xb1c
 DW_MACRO_end_file
 DW_MACRO_end_file
...
where filenum 2 refers to /usr/include/stdc-predef:
...
 The Directory Table
  Entry Name
  0     /home/vries/gdb
  1     /usr/include

 The File Name Table
  Entry Dir     Name
  0     0       test.c
  1     0       test.c
  2     1       stdc-predef.h
...

The "DW_MACRO_start_file - lineno: 0 filenum: 2" entry represents an implicit
include of /usr/include/stdc-predef, so the line number of 0 is correct.

But the complaints mechanism expects entries with line number zero to occur
only before the first start_file, which is the entry for "the source file
submitted to the compiler for compilation".

And moving the "DW_MACRO_start_file - lineno: 0 filenum: 2" entry to before
the first start_file doesn't work, because it would make it the
first start_file.

The standard requires that:
- the first start_file entry has lineno 0, and
- all define and undefine entries representing compilation options:
  - appear before the first start_file entry, and
  - have lineno 0.

Both these conditions are met, so the generated dwarf is standard compliant.

OTOH, the complaint is useful because it's likely to catch cases of incorrect
dwarf assembly.

So, fix this by simply not complaining about this corner-case.

Tested on aarch64-linux.
---
 gdb/dwarf2/macro.c                            | 9 +++++++--
 gdb/testsuite/gdb.dwarf2/macro-complaints.exp | 4 ++++
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/gdb/dwarf2/macro.c b/gdb/dwarf2/macro.c
index f72201ebc08..cedf12a7d29 100644
--- a/gdb/dwarf2/macro.c
+++ b/gdb/dwarf2/macro.c
@@ -457,8 +457,11 @@ dwarf_decode_macro_bytes (dwarf2_per_objfile *per_objfile,
       return;
     }
 
+  int prev_at_commandline = at_commandline;
   do
     {
+      int next_at_commandline = at_commandline;
+
       /* Do we at least have room for a macinfo type byte?  */
       if (mac_ptr >= mac_end)
 	{
@@ -637,7 +640,7 @@ dwarf_decode_macro_bytes (dwarf2_per_objfile *per_objfile,
 	    file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
 	    mac_ptr += bytes_read;
 
-	    if ((line == 0 && !at_commandline)
+	    if ((line == 0 && !at_commandline && !prev_at_commandline)
 		|| (line != 0 && at_commandline))
 	      complaint (_("debug info gives source %d included "
 			   "from %s at %s line %d"),
@@ -648,7 +651,7 @@ dwarf_decode_macro_bytes (dwarf2_per_objfile *per_objfile,
 	      {
 		/* This DW_MACRO_start_file was executed in the
 		   pass one.  */
-		at_commandline = 0;
+		next_at_commandline = 0;
 	      }
 	    else
 	      current_file = macro_start_file (builder, file, line,
@@ -782,6 +785,8 @@ dwarf_decode_macro_bytes (dwarf2_per_objfile *per_objfile,
 	  break;
 	}
       DIAGNOSTIC_POP
+      prev_at_commandline = at_commandline;
+      at_commandline = next_at_commandline;
     } while (macinfo_type != 0);
 }
 
diff --git a/gdb/testsuite/gdb.dwarf2/macro-complaints.exp b/gdb/testsuite/gdb.dwarf2/macro-complaints.exp
index 1a2dde36de0..56f597730b1 100644
--- a/gdb/testsuite/gdb.dwarf2/macro-complaints.exp
+++ b/gdb/testsuite/gdb.dwarf2/macro-complaints.exp
@@ -55,6 +55,7 @@ Dwarf::assemble $asm_file {
 
     lines {version 2} L {
 	file_name $::srcfile 1
+	file_name stdc-predef.h 1
 	program {
 	    DW_LNE_set_address $::main_start
 	    line $::line
@@ -74,6 +75,9 @@ Dwarf::assemble $asm_file {
 
 	    start_file 0 1
 
+	    start_file 0 2
+	    end_file
+
 	    define 1 "M1_03_OK 1"
 	    define 0 "M1_04_BADLINE 1"
 
-- 
2.35.3


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-06-26 18:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-26 18:50 [PATCH 0/3] [gdb/symtab] Fix some issues with macro complaints Tom de Vries
2024-06-26 18:50 ` [PATCH 1/3] [gdb/symtab] Emit malformed macro definition complaint once Tom de Vries
2024-06-26 18:50 ` [PATCH 2/3] [gdb/symtab] Fix incorrect complaint for imported definition Tom de Vries
2024-06-26 18:50 ` [PATCH 3/3] [gdb/symtab] Don't complain about stdc-predef.h include Tom de Vries

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).