Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Subject: [PATCH 1/3] [gdb/contrib] Fix KeyError in dwarf-to-dwarf-assembler.py
Date: Mon,  8 Jun 2026 14:54:18 +0200	[thread overview]
Message-ID: <20260608125420.2318026-2-tdevries@suse.de> (raw)
In-Reply-To: <20260608125420.2318026-1-tdevries@suse.de>

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


  reply	other threads:[~2026-06-08 12:55 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2026-06-09 17:04   ` [PATCH 1/3] [gdb/contrib] Fix KeyError in dwarf-to-dwarf-assembler.py 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

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=20260608125420.2318026-2-tdevries@suse.de \
    --to=tdevries@suse.de \
    --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