Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH 0/3] [gdb/contrib] Some dwarf-to-dwarf-assembler.py fixes
@ 2026-06-08 12:54 Tom de Vries
  2026-06-08 12:54 ` [PATCH 1/3] [gdb/contrib] Fix KeyError in dwarf-to-dwarf-assembler.py Tom de Vries
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Tom de Vries @ 2026-06-08 12:54 UTC (permalink / raw)
  To: gdb-patches

This patch series contains 3 gdb/contrib/dwarf-to-dwarf-assembler.py fixes.

I've tested it using:
...
$ for f in $(find outputs/ -type f -executable); do \
    echo "FILE: $f"; \
    src/gdb/contrib/dwarf-to-dwarf-assembler.py $f; \
  done > LOG 2>&1
$ egrep -c "Traceback" LOG
0
...

Tom de Vries (3):
  [gdb/contrib] Fix KeyError in dwarf-to-dwarf-assembler.py
  [gdb/contrib] Fix UnicodeDecodeError in dwarf-to-dwarf-assembler.py
  [gdb/contrib] Fix ELFParseError in dwarf-to-dwarf-assembler.py

 gdb/contrib/dwarf-to-dwarf-assembler.py | 33 ++++++++++++++++++++++---
 1 file changed, 29 insertions(+), 4 deletions(-)


base-commit: 748350ae9e20d4698527c57bb3d3d76a33242cab
-- 
2.51.0


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

* [PATCH 1/3] [gdb/contrib] Fix KeyError in dwarf-to-dwarf-assembler.py
  2026-06-08 12:54 [PATCH 0/3] [gdb/contrib] Some dwarf-to-dwarf-assembler.py fixes Tom de Vries
@ 2026-06-08 12:54 ` Tom de Vries
  2026-06-09 17:04   ` Tom Tromey
  2026-06-08 12:54 ` [PATCH 2/3] [gdb/contrib] Fix UnicodeDecodeError " Tom de Vries
  2026-06-08 12:54 ` [PATCH 3/3] [gdb/contrib] Fix ELFParseError " Tom de Vries
  2 siblings, 1 reply; 11+ messages in thread
From: Tom de Vries @ 2026-06-08 12:54 UTC (permalink / raw)
  To: gdb-patches

Executable outputs/gdb.threads/stepi-over-clone/stepi-over-clone contains the
following DWARF (using readelf -w):
...
    <31c5>   DW_AT_const_value : 8 byte block: ff ff ff ff ff ff ff ff
...
or more explicit using llvm-dwarfdump -v:
...
                DW_AT_const_value [DW_FORM_block1] \
		  (<0x08> ff ff ff ff ff ff ff ff )
...

Before commit 18ad8c1a54d ("Emit DWARF expressions from
dwarf-to-dwarf-assembler.py"), the dwarf-to-dwarf-assembler.py script
generated:
...
DW_AT_const_value { MANUAL: Fill expr list } SPECIAL_expr
...
for this.

But since the commit, we run into a KeyError.

The problem is that we're trying to interpret some random data as a DWARF
expression.

Fix this by special-casing DW_AT_const_value, and generating instead:
...
DW_AT_const_value "\xff\xff\xff\xff\xff\xff\xff\xff" DW_FORM_block1
...
which mimics the style I found in gdb.dwarf2/info-locals-optimized-out.exp:
...
                DW_TAG_variable {
                    DW_AT_name const_bytes
                    DW_AT_type :$int_type_label
                    DW_AT_const_value "\x01\x01\x01\x01" DW_FORM_block1
		}
...

Likewise for DW_AT_discr_list.

Another case for which the KeyError triggers, is DWARF from
outputs/gdb.ada/array_of_variant/p-minimal:
...
 <2><14a4>: Abbrev Number: 20 (DW_TAG_structure_type)
    <14a5>   DW_AT_name        : p__payload_t
    <14a9>   DW_AT_byte_size   : 18 byte block: \
                                 fd 63 14 0 0 97 94 1 99 45 0 0 0 23 7 9 fc 1a \
				 (DW_OP_GNU_variable_value: <0x1463>; \
				  DW_OP_push_object_address; \
				  DW_OP_deref_size: 1; \
				  DW_OP_call4: <0x1421>; \
				  DW_OP_plus_uconst: 7; \
				  DW_OP_const1s: -4; DW_OP_and)
...
because the pyelftools version that I'm using doesn't support
DW_OP_GNU_variable_value yet (supported starting v0.33).

Fix this by catching the KeyError, and falling back to the string case:
...
  DW_AT_byte_size \
  "\xfd\x63\x14\x00\x00\x97\x94\x01\x99\x45\x00\x00\x00\x23\x07\x09\xfc\x1a" \
  DW_FORM_exprloc # Failed to print op as DWARF expression: 0xfd
...
---
 gdb/contrib/dwarf-to-dwarf-assembler.py | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/gdb/contrib/dwarf-to-dwarf-assembler.py b/gdb/contrib/dwarf-to-dwarf-assembler.py
index 314a9e9587b..fa32bc4bbab 100755
--- a/gdb/contrib/dwarf-to-dwarf-assembler.py
+++ b/gdb/contrib/dwarf-to-dwarf-assembler.py
@@ -226,9 +226,23 @@ class DWARFAttribute:
         elif self.name == "DW_AT_encoding" and isinstance(self.value, int):
             s += "@" + ATE_NAME[self.value]
         elif self.form in EXPR_ATTRIBUTE_FORMS:
-            # This returns a complete description that is already
-            # indented.
-            return self._format_expr_value(s, indent_count)
+            postfix = ""
+            if self.name not in ["DW_AT_const_value", "DW_AT_discr_list"]:
+                # Try to print as a DWARF expression.
+                try:
+                    # This returns a complete description that is already
+                    # indented.
+                    return self._format_expr_value(s, indent_count)
+                except KeyError as e:
+                    # Fall through to basic printing.
+                    postfix = " # Failed to print op as DWARF expression: " + hex(
+                        int(str(e))
+                    )
+            # Print the data as a string in "\x01\x02\x03\x04" format.
+            result = ""
+            for op in self.value:
+                result += f"\\x{op:02x}"
+            return indent(s, indent_count) + '"' + result + '" ' + self.form + postfix
         else:
             s += self._format_value(offset_die_lookup)
 
-- 
2.51.0


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

* [PATCH 2/3] [gdb/contrib] Fix UnicodeDecodeError in dwarf-to-dwarf-assembler.py
  2026-06-08 12:54 [PATCH 0/3] [gdb/contrib] Some dwarf-to-dwarf-assembler.py fixes Tom de Vries
  2026-06-08 12:54 ` [PATCH 1/3] [gdb/contrib] Fix KeyError in dwarf-to-dwarf-assembler.py Tom de Vries
@ 2026-06-08 12:54 ` Tom de Vries
  2026-06-09 17:07   ` Tom Tromey
  2026-06-08 12:54 ` [PATCH 3/3] [gdb/contrib] Fix ELFParseError " Tom de Vries
  2 siblings, 1 reply; 11+ messages in thread
From: Tom de Vries @ 2026-06-08 12:54 UTC (permalink / raw)
  To: gdb-patches

When running dwarf-to-dwarf-assembler.py on outputs/gdb.rust/unicode/unicode,
I run into a UnicodeDecodeError for (using readelf -w):
...
 <4><9cf>: Abbrev Number: 10 (DW_TAG_variable)
    <9d3>   DW_AT_name        : (indirect string, offset: 0xab4): 𝕯
...
or more explicit using llvm-dwarfdump -v:
...
0x000009cf:         DW_TAG_variable [10]   (0x000009c2)
                      DW_AT_name [DW_FORM_strp] ( .debug_str[0x00000ab4] \
                                                  = "\360\235\225\257")
...

Fix this by printing:
...
                        DW_TAG_variable {
			    ...
                            DW_AT_name "\xf0\x9d\x95\xaf" DW_FORM_strp
...

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33970
---
 gdb/contrib/dwarf-to-dwarf-assembler.py | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/gdb/contrib/dwarf-to-dwarf-assembler.py b/gdb/contrib/dwarf-to-dwarf-assembler.py
index fa32bc4bbab..c1818f9fdb8 100755
--- a/gdb/contrib/dwarf-to-dwarf-assembler.py
+++ b/gdb/contrib/dwarf-to-dwarf-assembler.py
@@ -174,7 +174,14 @@ class DWARFAttribute:
             else:
                 return str(self.value)
         elif isinstance(self.value, bytes):
-            return self._format_str(self.value.decode("ascii"))
+            try:
+                return self._format_str(self.value.decode("ascii"))
+            except UnicodeDecodeError:
+                # Print the data as a string in "\x01\x02\x03\x04" format.
+                result = ""
+                for byte in self.value:
+                    result += f"\\x{byte:02x}"
+                return '"' + result + '"'
         elif isinstance(self.value, str):
             return self._format_str(self.value)
         elif isinstance(self.value, ListContainer):
-- 
2.51.0


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

* [PATCH 3/3] [gdb/contrib] Fix ELFParseError in dwarf-to-dwarf-assembler.py
  2026-06-08 12:54 [PATCH 0/3] [gdb/contrib] Some dwarf-to-dwarf-assembler.py fixes Tom de Vries
  2026-06-08 12:54 ` [PATCH 1/3] [gdb/contrib] Fix KeyError in dwarf-to-dwarf-assembler.py Tom de Vries
  2026-06-08 12:54 ` [PATCH 2/3] [gdb/contrib] Fix UnicodeDecodeError " Tom de Vries
@ 2026-06-08 12:54 ` Tom de Vries
  2026-06-08 13:18   ` Tom de Vries
  2026-06-09 17:08   ` Tom Tromey
  2 siblings, 2 replies; 11+ messages in thread
From: Tom de Vries @ 2026-06-08 12:54 UTC (permalink / raw)
  To: gdb-patches

When running dwarf-to-dwarf-assembler.py on
outputs/gdb.dwarf2/implptr-64bit/implptr-64bit-d2-o8-a4-r4-t0, I run into:
...
elftools.common.exceptions.ELFParseError: expected 8, found 5
...
for the DWARF:
...
 <2><335>: Abbrev Number: 8 (DW_TAG_variable)
    <336>   DW_AT_name        : p
    <338>   DW_AT_location    : 6 byte block: f2 14 3 0 0 0 \
                                (DW_OP_GNU_implicit_pointer: <0x314> 0)
...

Pyelftools expects that DW_OP_GNU_implicit_pointer has an 8-byte reference
operand (because it's a 64-bit DWARF unit), but instead it has a 4-byte
reference.

The DWARF is correct, it's a cornercase of using DW_OP_GNU_implicit_pointer in
DWARF v2, where DW_FORM_ref_addr references have the same size as an address
on the target system, something that was changed in DWARF v3.

Handle this by catching the error and printing:
...
DW_AT_location "\xf2\x14\x03\x00\x00\x00" DW_FORM_block \
  # Failed to print DWARF expression: expected 8, found 5
...
---
 gdb/contrib/dwarf-to-dwarf-assembler.py | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/gdb/contrib/dwarf-to-dwarf-assembler.py b/gdb/contrib/dwarf-to-dwarf-assembler.py
index c1818f9fdb8..797d5a46d72 100755
--- a/gdb/contrib/dwarf-to-dwarf-assembler.py
+++ b/gdb/contrib/dwarf-to-dwarf-assembler.py
@@ -48,6 +48,7 @@ from io import BytesIO, IOBase
 from logging import getLogger
 from typing import Annotated, Optional
 
+from elftools.common.exceptions import ELFParseError
 from elftools.construct.lib.container import ListContainer
 from elftools.dwarf.compileunit import CompileUnit as RawCompileUnit
 from elftools.dwarf.die import DIE as RawDIE
@@ -245,6 +246,9 @@ class DWARFAttribute:
                     postfix = " # Failed to print op as DWARF expression: " + hex(
                         int(str(e))
                     )
+                except ELFParseError as e:
+                    # Fall through to basic printing.
+                    postfix = " # Failed to print DWARF expression: " + str(e)
             # Print the data as a string in "\x01\x02\x03\x04" format.
             result = ""
             for op in self.value:
-- 
2.51.0


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

* Re: [PATCH 3/3] [gdb/contrib] Fix ELFParseError in dwarf-to-dwarf-assembler.py
  2026-06-08 12:54 ` [PATCH 3/3] [gdb/contrib] Fix ELFParseError " Tom de Vries
@ 2026-06-08 13:18   ` Tom de Vries
  2026-06-09 17:08   ` Tom Tromey
  1 sibling, 0 replies; 11+ messages in thread
From: Tom de Vries @ 2026-06-08 13:18 UTC (permalink / raw)
  To: gdb-patches

On 6/8/26 2:54 PM, Tom de Vries wrote:
> Pyelftools expects that DW_OP_GNU_implicit_pointer has an 8-byte reference
> operand (because it's a 64-bit DWARF unit), but instead it has a 4-byte
> reference.
> 
> The DWARF is correct, it's a cornercase of using DW_OP_GNU_implicit_pointer in
> DWARF v2, where DW_FORM_ref_addr references have the same size as an address
> on the target system, something that was changed in DWARF v3.

I've filed this ( https://github.com/eliben/pyelftools/issues/663 ).

Thanks,
- Tom

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

* Re: [PATCH 1/3] [gdb/contrib] Fix KeyError in dwarf-to-dwarf-assembler.py
  2026-06-08 12:54 ` [PATCH 1/3] [gdb/contrib] Fix KeyError in dwarf-to-dwarf-assembler.py Tom de Vries
@ 2026-06-09 17:04   ` Tom Tromey
  2026-06-10  7:57     ` Tom de Vries
  0 siblings, 1 reply; 11+ messages in thread
From: Tom Tromey @ 2026-06-09 17:04 UTC (permalink / raw)
  To: Tom de Vries; +Cc: gdb-patches

>>>>> "Tom" == Tom de Vries <tdevries@suse.de> writes:

Tom> Fix this by special-casing DW_AT_const_value, and generating instead:

Seems reasonable but I wonder if EXPR_ATTRIBUTE_FORMS should just be
DW_FORM_exprloc.

Tom

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

* Re: [PATCH 2/3] [gdb/contrib] Fix UnicodeDecodeError in dwarf-to-dwarf-assembler.py
  2026-06-08 12:54 ` [PATCH 2/3] [gdb/contrib] Fix UnicodeDecodeError " Tom de Vries
@ 2026-06-09 17:07   ` Tom Tromey
  2026-06-10  9:12     ` Tom de Vries
  0 siblings, 1 reply; 11+ messages in thread
From: Tom Tromey @ 2026-06-09 17:07 UTC (permalink / raw)
  To: Tom de Vries; +Cc: gdb-patches

>>>>> "Tom" == Tom de Vries <tdevries@suse.de> writes:

Tom> +                return self._format_str(self.value.decode("ascii"))

Probably we should decode using UTF-8 here, maybe with some fallback
setting, like errors='replace', see

https://docs.python.org/3/library/stdtypes.html#bytes.decode

The choice of encoding is a bit tricky since technically DWARF only uses
Unicode when some other flag is seen.  However I think it's normally
fine and expected.

Tom

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

* Re: [PATCH 3/3] [gdb/contrib] Fix ELFParseError in dwarf-to-dwarf-assembler.py
  2026-06-08 12:54 ` [PATCH 3/3] [gdb/contrib] Fix ELFParseError " Tom de Vries
  2026-06-08 13:18   ` Tom de Vries
@ 2026-06-09 17:08   ` Tom Tromey
  1 sibling, 0 replies; 11+ messages in thread
From: Tom Tromey @ 2026-06-09 17:08 UTC (permalink / raw)
  To: Tom de Vries; +Cc: gdb-patches

>>>>> "Tom" == Tom de Vries <tdevries@suse.de> writes:

Tom> The DWARF is correct, it's a cornercase of using DW_OP_GNU_implicit_pointer in
Tom> DWARF v2, where DW_FORM_ref_addr references have the same size as an address
Tom> on the target system, something that was changed in DWARF v3.

Ugh.

Tom> Handle this by catching the error and printing:

Thanks.
Approved-By: Tom Tromey <tom@tromey.com>

Tom

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

* Re: [PATCH 1/3] [gdb/contrib] Fix KeyError in dwarf-to-dwarf-assembler.py
  2026-06-09 17:04   ` Tom Tromey
@ 2026-06-10  7:57     ` Tom de Vries
  2026-06-10 13:23       ` Tom Tromey
  0 siblings, 1 reply; 11+ messages in thread
From: Tom de Vries @ 2026-06-10  7:57 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

On 6/9/26 7:04 PM, Tom Tromey wrote:
>>>>>> "Tom" == Tom de Vries <tdevries@suse.de> writes:
> 
> Tom> Fix this by special-casing DW_AT_const_value, and generating instead:
> 
> Seems reasonable but I wonder if EXPR_ATTRIBUTE_FORMS should just be
> DW_FORM_exprloc.

The question is whether there are cases where DW_FORM_block* is used 
instead of DW_FORM_exprloc.

At least our testsuite does so, for something like this:
...
   DW_AT_location {} DW_FORM_block1
...

Thanks,
- Tom

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

* Re: [PATCH 2/3] [gdb/contrib] Fix UnicodeDecodeError in dwarf-to-dwarf-assembler.py
  2026-06-09 17:07   ` Tom Tromey
@ 2026-06-10  9:12     ` Tom de Vries
  0 siblings, 0 replies; 11+ messages in thread
From: Tom de Vries @ 2026-06-10  9:12 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

On 6/9/26 7:07 PM, Tom Tromey wrote:
>>>>>> "Tom" == Tom de Vries <tdevries@suse.de> writes:
> 
> Tom> +                return self._format_str(self.value.decode("ascii"))
> 
> Probably we should decode using UTF-8 here, maybe with some fallback
> setting, like errors='replace', see
> 
> https://docs.python.org/3/library/stdtypes.html#bytes.decode
> 
> The choice of encoding is a bit tricky since technically DWARF only uses
> Unicode when some other flag is seen.  However I think it's normally
> fine and expected.
> 

Thanks for the review.

I've pushed this:
...
-            return self._format_str(self.value.decode("ascii"))
+            return self._format_str(self.value.decode("UTF-8", 
"backslashreplace"))
...

Thanks,
- Tom

> Tom


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

* Re: [PATCH 1/3] [gdb/contrib] Fix KeyError in dwarf-to-dwarf-assembler.py
  2026-06-10  7:57     ` Tom de Vries
@ 2026-06-10 13:23       ` Tom Tromey
  0 siblings, 0 replies; 11+ messages in thread
From: Tom Tromey @ 2026-06-10 13:23 UTC (permalink / raw)
  To: Tom de Vries; +Cc: Tom Tromey, gdb-patches

Tom> The question is whether there are cases where DW_FORM_block* is used
Tom> instead of DW_FORM_exprloc.

Yeah.

Tom> At least our testsuite does so, for something like this:
Tom> ...
Tom>   DW_AT_location {} DW_FORM_block1
Tom> ...

I don't recall, maybe that was ok in older versions of DWARF.
I think DWARF 5 says to use exprloc here.

Tom

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

end of thread, other threads:[~2026-06-10 13:28 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-06-08 12:54 [PATCH 0/3] [gdb/contrib] Some dwarf-to-dwarf-assembler.py fixes Tom de Vries
2026-06-08 12:54 ` [PATCH 1/3] [gdb/contrib] Fix KeyError in dwarf-to-dwarf-assembler.py Tom de Vries
2026-06-09 17:04   ` Tom Tromey
2026-06-10  7:57     ` Tom de Vries
2026-06-10 13:23       ` Tom Tromey
2026-06-08 12:54 ` [PATCH 2/3] [gdb/contrib] Fix UnicodeDecodeError " Tom de Vries
2026-06-09 17:07   ` Tom Tromey
2026-06-10  9:12     ` Tom de Vries
2026-06-08 12:54 ` [PATCH 3/3] [gdb/contrib] Fix ELFParseError " Tom de Vries
2026-06-08 13:18   ` Tom de Vries
2026-06-09 17:08   ` Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox