From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Subject: Re: [PATCHv2] gdb: unify trivial_entry_value handling in DW_OP_entry_value
Date: Tue, 24 Nov 2026 11:21:14 +0000 [thread overview]
Message-ID: <xhuyfh5h6tqz9.fsf@aburgess-thinkpadp1gen8.rmtgb.csb> (raw)
In-Reply-To: <fa00bc2a6b9f70d357175b52085514970699d4b5.1791061385.git.aburgess@redhat.com>
Andrew Burgess <aburgess@redhat.com> writes:
> In V2:
>
> - Rebase and retest.
>
> - Spelling / grammar fixes in comments and commit messages
>
> - Reworded the commit message for increased clarity.
>
> - No code changes.
>
> Thanks,
> Andrew
>
> ~~~~~
>
> This commit simplifies the DW_OP_entry_value handling in dwarf2/expr.c
> for the case where trivial_entry_value is true, as a consequence I hit
> a bug in dwarf_expr_context::fetch_result which I have also fixed.
>
> Currently GDB can handle two different DW_OP_entry_value DWARF
> expressions, DW_OP_reg or DW_OP_breg/DW_OP_deref, and the current
> trivial_entry_value is also split in two to mirror these two cases.
>
> The trivial_entry_value function returns true when the inferior is
> stopped at the first instruction of a function. This means that the
> value of the DW_OP_entry_value expression is just the current value of
> that DWARF expression.
>
> This commit moves the trivial_entry_value handling to earlier in the
> DW_OP_entry_value case statement block, and unifies the existing two
> cases.
>
> The new code performs a recursive evaluation of the DW_OP_entry_value
> DWARF expression.
>
> I took the m_stack save and restore idea from how DW_OP_fbreg is
> handled, though I used SCOPE_EXIT for the restore so that the restore
> happens even on the exception case.
>
> Also, unlike the fbreg case I also save and restore m_location as
> DW_OP_entry_value could be in the middle of a complex DWARF expression
> which has already set m_location, and m_pieces as we could also be in
> the middle of building a composite value.
>
> Finally, the result is pulled from m_stack by a call to fetch_result.
>
> The DWARF 5 spec says this about DW_OP_entry_value:
>
> The DW_OP_entry_value operation pushes the value that the described
> location held upon entering the current subprogram. It has two
> operands: an unsigned LEB128 length, followed by a block containing
> a DWARF expression or a register location description (see Section
> 2.6.1.1.3 on page 39). The length operand specifies the length in
> bytes of the block. If the block contains a DWARF expression, the
> DWARF expression is evaluated as if it had been evaluated upon
> entering the current subprogram. The DWARF expression assumes no
> values are present on the DWARF stack initially and results in
> exactly one value being pushed on the DWARF stack when completed. If
> the block contains a register location description,
> DW_OP_entry_value pushes the value that register had upon entering
> the current subprogram.
>
> From this, and from how GDB currently handles DW_OP_breg/DW_OP_deref
> within the DW_OP_entry_value case, I believe that this means there are
> two possible results from a DW_OP_entry_value expression:
>
> (1) The result is left on the DWARF expression stack.
>
> (2) The result is a register location description, in which case the
> result is read from the register.
>
> Based on #1 I initially wanted to pass as_lval=false to fetch_result,
> passing as_lval=false means that m_location is forced to be
> DWARF_VALUE_STACK. However, this causes a problem when m_location is
> initially DWARF_VALUE_REGISTER, changing to DWARF_VALUE_STACK means
> that GDB reads the register number from the stack, which is never what
> we want.
>
> This got me looking at how as_lval is handled in fetch_result, and I'm
> not convinced that the current handling makes much sense. Converting
> from DWARF_VALUE_MEMORY to DWARF_VALUE_STACK is fine, this causes GDB
> to read the result from the expression stack rather than treating the
> value on the stack as a memory location, but for DWARF_VALUE_REGISTER,
> DWARF_VALUE_LITERAL, DWARF_VALUE_OPTIMIZED_OUT, and
> DWARF_VALUE_IMPLICIT_POINTER converting to DWARF_VALUE_STACK doesn't
> really work as there either isn't a value on the stack, or the value
> on the stack is an internal detail, e.g. the DWARF register number for
> DWARF_VALUE_REGISTER.
>
> I did consider changing this existing code block:
>
> if (!as_lval)
> this->m_location = DWARF_VALUE_STACK;
>
> to this:
>
> if (!as_lval && this->m_location == DWARF_VALUE_MEMORY)
> this->m_location = DWARF_VALUE_STACK;
>
> But I figured it would actually be neater if I pushed the as_lval
> handling down into the switch statement. The switch is now structured
> like this:
>
> switch (this->m_location)
> {
> ...
> case DWARF_VALUE_MEMORY:
> if (as_lval)
> {
> ...
> break;
> }
> [[fallthrough]];
> case DWARF_VALUE_STACK:
> ...
> }
>
> So when as_lval is false we treat it as DWARF_VALUE_STACK just like
> before, but for any other location type we leave things unchanged.
> This makes more sense to me, this is the bug fix to which I referred
> at the top of this commit message.
>
> Back in the DW_OP_entry_value handling we can now pass as_lval=false
> to the fetch_result call, and we will correctly handle the two cases I
> identified above, a value on the stack will be treated as an actual
> value, while a register location description will result in the value
> being read from the register.
>
> I don't think this issue with fetch_result ever bit us before as the
> only place where fetch_result could be called with as_lval=false would
> only ever pass DWARF_VALUE_MEMORY or DWARF_VALUE_STACK, in which case
> the old behaviour was fine.
>
> There are no new tests added here. The changes in
> dwarf_expr_context::execute_stack_op are a refactor, and the bug in
> dwarf_expr_context::fetch_result could not previously be reached in a
> way that would cause incorrect behaviour. The fetch_result fix is
> covered by the existing gdb.dwarf2/dw2-entry-value*.exp tests after
> the refactor though.
I've gone ahead and checked this in.
Let me know if any issues are spotted.
Thanks,
Andrew
prev parent reply other threads:[~2026-11-24 11:21 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-04 13:22 [PATCH] " Andrew Burgess
2026-10-03 21:04 ` [PATCHv2] " Andrew Burgess
2026-11-24 11:21 ` Andrew Burgess [this message]
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=xhuyfh5h6tqz9.fsf@aburgess-thinkpadp1gen8.rmtgb.csb \
--to=aburgess@redhat.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