From patchwork Fri Jan 17 15:12:04 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom de Vries X-Patchwork-Id: 104998 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9E5C63847713 for ; Fri, 17 Jan 2025 15:12:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9E5C63847713 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=CJ7+FtjT; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=wTHq+PZA; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=CJ7+FtjT; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=wTHq+PZA X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2a07:de40:b251:101:10:150:64:1]) by sourceware.org (Postfix) with ESMTPS id CFA853847725 for ; Fri, 17 Jan 2025 15:11:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CFA853847725 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org CFA853847725 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1737126719; cv=none; b=CHp0QMESFkSd1weCQ8LWmUtw1XsZ0u7cr70TUlcuX2LiP7SK7zjBRIkEks/6/Oh+Nlf/nXQuyBEazEjweLy83TFZjubJflgPL0I3OiEVoB6fljkC3Vo2+8pMyxkZOmSvluGpOAnv4Nl0WnYibUiChn9pDWnZVmRgFhgjtZEMN14= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1737126719; c=relaxed/simple; bh=bBJFwpAtmhl3/emxwFvTSFwZHSAyuIQd5hG5XkeRvXo=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:From: To:Subject:Date:Message-ID:MIME-Version; b=wqg7BGm5aHynjNR7qIxUAFx4LIpwgtC+V+5lAuFCOHG3agsZJbWl7FBMyUjejNyACKIkz8h5+99FhbK+WaoDskhEXcYWdSU5uht0y0g0LAJs/ILiIeHX4huLJXluYAOFRVBOnaqgtT96mvI5z6uCuh3KZI8xUi4WGSyHKEiPhtc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CFA853847725 Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 9329621184 for ; Fri, 17 Jan 2025 15:11:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1737126717; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=WANguCxC2FbExVMvATmumSvFzQPc/yi0iVi9gk6zuJs=; b=CJ7+FtjTtyqVmOiXewBOKW2L5kgnzRrRm3PwRQhoh2sE7O9rw/6yefeajVFMG1le2QkD9Z KGnNljpiuc9yn4DeipZ4tl62WM8A9HIq46HjYpd8mfP6sC0dOcU/iLWepbE+5M28OE1Y/a oIULLc2yQtrym95ML9CMT0/m6mO5090= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1737126717; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=WANguCxC2FbExVMvATmumSvFzQPc/yi0iVi9gk6zuJs=; b=wTHq+PZAcKBYSZKzK6p1pc02yRvD7inBEAN8WuAzcMC7MEyl9znoPze8upWY2Fk3qt+OH5 bi4uZ9OvsuvR0IDQ== Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=CJ7+FtjT; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=wTHq+PZA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1737126717; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=WANguCxC2FbExVMvATmumSvFzQPc/yi0iVi9gk6zuJs=; b=CJ7+FtjTtyqVmOiXewBOKW2L5kgnzRrRm3PwRQhoh2sE7O9rw/6yefeajVFMG1le2QkD9Z KGnNljpiuc9yn4DeipZ4tl62WM8A9HIq46HjYpd8mfP6sC0dOcU/iLWepbE+5M28OE1Y/a oIULLc2yQtrym95ML9CMT0/m6mO5090= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1737126717; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=WANguCxC2FbExVMvATmumSvFzQPc/yi0iVi9gk6zuJs=; b=wTHq+PZAcKBYSZKzK6p1pc02yRvD7inBEAN8WuAzcMC7MEyl9znoPze8upWY2Fk3qt+OH5 bi4uZ9OvsuvR0IDQ== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 79B0013332 for ; Fri, 17 Jan 2025 15:11:57 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id gVkqHD1zimfUFgAAD6G6ig (envelope-from ) for ; Fri, 17 Jan 2025 15:11:57 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH v2] [gdb/symtab] Handle DW_OP_entry_value at function entry Date: Fri, 17 Jan 2025 16:12:04 +0100 Message-ID: <20250117151204.15622-1-tdevries@suse.de> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Rspamd-Queue-Id: 9329621184 X-Spam-Score: -3.01 X-Rspamd-Action: no action X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; MIME_TRACE(0.00)[0:+]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FUZZY_BLOCKED(0.00)[rspamd.com]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; RCPT_COUNT_ONE(0.00)[1]; ARC_NA(0.00)[]; RCVD_TLS_ALL(0.00)[]; DKIM_TRACE(0.00)[suse.de:+]; RCVD_COUNT_TWO(0.00)[2]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; TO_DN_NONE(0.00)[]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:106:10:150:64:167:received]; PREVIOUSLY_DELIVERED(0.00)[gdb-patches@sourceware.org]; RCVD_VIA_SMTP_AUTH(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:rdns, imap1.dmz-prg2.suse.org:helo, suse.de:mid, suse.de:dkim, gnu.org:url] X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spam-Level: X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~patchwork=sourceware.org@sourceware.org On riscv64-linux, with test-case gdb.base/vla-optimized-out.exp I ran into: ... (gdb) p sizeof (a)^M $2 = ^M (gdb) FAIL: $exp: o1: printed size of optimized out vla ... The variable a has type 0xbf: ... <1>: Abbrev Number: 12 (DW_TAG_array_type) DW_AT_type : <0xe3> DW_AT_sibling : <0xdc> <2>: Abbrev Number: 13 (DW_TAG_subrange_type) DW_AT_type : <0xdc> DW_AT_upper_bound : 13 byte block: a3 1 5a 23 1 8 20 24 8 20 26 31 1c (DW_OP_entry_value: (DW_OP_reg10 (a0)); DW_OP_plus_uconst: 1; DW_OP_const1u: 32; DW_OP_shl; DW_OP_const1u: 32; DW_OP_shra; DW_OP_lit1; DW_OP_minus) ... which has an upper bound using an DW_OP_entry_value, and since the corresponding call site contains no information to resolve the value of a0 at function entry: ... <2><6b>: Abbrev Number: 6 (DW_TAG_call_site) <6c> DW_AT_call_return_pc: 0x638 <74> DW_AT_call_origin : <0x85> ... evaluting the dwarf expression fails, and we get . My first thought was to try breaking at *f1 instead f1 to see if that would help, but actually the breakpoint resolved to the same address. In other words, the inferior is stopped at function entry. Fix this by resolving DW_OP_entry_value when stopped at function entry by simply evaluating the expression. This handles these two cases (x86_64, using reg rdi): - DW_OP_entry_value: (DW_OP_regx: 5 (rdi)) - DW_OP_entry_value: (DW_OP_bregx: 5 (rdi) 0; DW_OP_deref_size: 4) Tested on x86_64-linux. Tested gdb.base/vla-optimized-out.exp on riscv64-linux. Tested an earlier version of gdb.dwarf2/dw2-entry-value-2.exp on riscv64-linux, but atm I'm running into trouble on that machine (cfarm92) so I haven't tested the current version there. --- gdb/dwarf2/expr.c | 69 +++++++--- gdb/dwarf2/expr.h | 4 + gdb/testsuite/gdb.dwarf2/dw2-entry-value-2.c | 32 +++++ .../gdb.dwarf2/dw2-entry-value-2.exp | 125 ++++++++++++++++++ gdb/testsuite/lib/dwarf.exp | 23 +++- 5 files changed, 235 insertions(+), 18 deletions(-) create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-entry-value-2.c create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-entry-value-2.exp base-commit: 48984d3da79f340b814e43b6576993ea1a927f5a diff --git a/gdb/dwarf2/expr.c b/gdb/dwarf2/expr.c index ee1522b7437..695b30e71b4 100644 --- a/gdb/dwarf2/expr.c +++ b/gdb/dwarf2/expr.c @@ -881,6 +881,34 @@ dwarf_expr_context::read_mem (gdb_byte *buf, CORE_ADDR addr, /* See expr.h. */ +value * +dwarf_expr_context::deref (CORE_ADDR addr, int size, struct type *type) +{ + gdb_byte *buf = (gdb_byte *) alloca (size); + this->read_mem (buf, addr, size); + + if (type == nullptr) + type = this->address_type (); + + /* If the size of the object read from memory is different + from the type length, we need to zero-extend it. */ + if (type->length () != size) + { + gdbarch *arch = this->m_per_objfile->objfile->arch (); + bfd_endian byte_order = gdbarch_byte_order (arch); + ULONGEST datum + = extract_unsigned_integer (buf, size, byte_order); + + buf = (gdb_byte *) alloca (type->length ()); + store_unsigned_integer (buf, type->length (), + byte_order, datum); + } + + return value_from_contents_and_address (type, buf, addr); +} + +/* See expr.h. */ + void dwarf_expr_context::push_dwarf_reg_entry_value (call_site_parameter_kind kind, call_site_parameter_u kind_u, @@ -1916,7 +1944,6 @@ dwarf_expr_context::execute_stack_op (const gdb_byte *op_ptr, case DW_OP_GNU_deref_type: { int addr_size = (op == DW_OP_deref ? this->m_addr_size : *op_ptr++); - gdb_byte *buf = (gdb_byte *) alloca (addr_size); CORE_ADDR addr = fetch_address (0); struct type *type; @@ -1931,21 +1958,7 @@ dwarf_expr_context::execute_stack_op (const gdb_byte *op_ptr, else type = address_type; - this->read_mem (buf, addr, addr_size); - - /* If the size of the object read from memory is different - from the type length, we need to zero-extend it. */ - if (type->length () != addr_size) - { - ULONGEST datum = - extract_unsigned_integer (buf, addr_size, byte_order); - - buf = (gdb_byte *) alloca (type->length ()); - store_unsigned_integer (buf, type->length (), - byte_order, datum); - } - - result_val = value_from_contents_and_address (type, buf, addr); + result_val = this->deref (addr, addr_size, type); break; } @@ -2274,6 +2287,18 @@ dwarf_expr_context::execute_stack_op (const gdb_byte *op_ptr, if (kind_u.dwarf_reg != -1) { op_ptr += len; + + if (m_frame == get_current_frame () + && find_function_type (get_frame_pc (m_frame)) != nullptr) + { + /* We're stopped at the start of the function. + Handle as DW_OP_regx. */ + result_val + = value_from_ulongest (address_type, kind_u.dwarf_reg); + this->m_location = DWARF_VALUE_REGISTER; + break; + } + this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG, kind_u, -1 /* deref_size */); @@ -2288,6 +2313,18 @@ dwarf_expr_context::execute_stack_op (const gdb_byte *op_ptr, if (deref_size == -1) deref_size = this->m_addr_size; op_ptr += len; + + if (m_frame == get_current_frame () + && find_function_type (get_frame_pc (m_frame)) != nullptr) + { + /* We're stopped at the start of the function. + Handle as DW_OP_bregx;DW_OP_deref_size. */ + CORE_ADDR addr + = read_addr_from_reg (this->m_frame, kind_u.dwarf_reg); + result_val = this->deref (addr, deref_size); + break; + } + this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG, kind_u, deref_size); goto no_push; diff --git a/gdb/dwarf2/expr.h b/gdb/dwarf2/expr.h index 1b1653e0768..58ae5ccd8d4 100644 --- a/gdb/dwarf2/expr.h +++ b/gdb/dwarf2/expr.h @@ -256,6 +256,10 @@ struct dwarf_expr_context but with the address being 0. In this situation, we arrange for memory reads to come from the passed-in buffer. */ void read_mem (gdb_byte *buf, CORE_ADDR addr, size_t length); + + /* Deref ADDR with size SIZE and return a value of type TYPE. + If TYPE == nullptr, defaults to this->address_type (). */ + value *deref (CORE_ADDR addr, int size, struct type *type = nullptr); }; /* Return the value of register number REG (a DWARF register number), diff --git a/gdb/testsuite/gdb.dwarf2/dw2-entry-value-2.c b/gdb/testsuite/gdb.dwarf2/dw2-entry-value-2.c new file mode 100644 index 00000000000..57025e0bc73 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-entry-value-2.c @@ -0,0 +1,32 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2025 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 . */ + +int var = 2; + +static +void bar (int *p) +{ + asm ("bar_label: .globl bar_label"); +} + +int +main() +{ + asm ("main_label: .globl main_label"); + bar (&var); + return 0; +} diff --git a/gdb/testsuite/gdb.dwarf2/dw2-entry-value-2.exp b/gdb/testsuite/gdb.dwarf2/dw2-entry-value-2.exp new file mode 100644 index 00000000000..55ecf9cece7 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-entry-value-2.exp @@ -0,0 +1,125 @@ +# Copyright 2025 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 . + +# Check that we can get DW_OP_entry_value at function entry. + +load_lib dwarf.exp + +# This test can only be run on targets which support DWARF-2 and use gas. +require dwarf2_support + +standard_testfile .c -debug.S + +set test "get dwarf regnum for first argument register" +if { [is_x86_64_m64_target] } { + set dwarf_regnum 5 +} elseif { [istarget riscv64*] } { + set dwarf_regnum 10 +} else { + set reason "architecture-specific setting missing" + unsupported "$test ($reason)" + return +} +pass $test + +# Set up the DWARF for the test. + +set asm_file [standard_output_file $srcfile2] +Dwarf::assemble $asm_file { + global srcdir subdir srcfile + + get_func_info main + get_func_info bar + + cu {} { + DW_TAG_compile_unit { + {DW_AT_name $srcfile} + } { + declare_labels integer + + integer: DW_TAG_base_type { + {DW_AT_byte_size 8 DW_FORM_sdata} + {DW_AT_encoding @DW_ATE_signed} + {DW_AT_name integer} + } + + DW_TAG_subprogram { + { DW_AT_name main } + { DW_AT_low_pc $main_start DW_FORM_addr } + { DW_AT_high_pc $main_end DW_FORM_addr } + } { + DW_TAG_variable { + { DW_AT_name argc } + { DW_AT_type :$integer } + { DW_AT_location { + DW_OP_entry_value { + DW_OP_regx $::dwarf_regnum + } + } SPECIAL_expr } + } + } + + DW_TAG_subprogram { + { DW_AT_name bar } + { DW_AT_low_pc $bar_start DW_FORM_addr } + { DW_AT_high_pc $bar_end DW_FORM_addr } + } { + DW_TAG_variable { + { DW_AT_name foo } + { DW_AT_type :$integer } + { DW_AT_location { + DW_OP_entry_value { + DW_OP_bregx $::dwarf_regnum 0 + DW_OP_deref_size 4 + } + DW_OP_stack_value + } SPECIAL_expr } + } + } + } + } +} + +if { [prepare_for_testing "failed to prepare" $testfile \ + [list $srcfile $asm_file] {nodebug}] } { + return -1 +} + +if { ![runto *main] } { + return +} + +with_test_prefix "at main+0" { + gdb_test "p argc" " = 1" + + gdb_test "stepi" +} + +with_test_prefix "at main+1" { + gdb_test "p argc" " = " +} + +gdb_breakpoint "*bar" +gdb_continue_to_breakpoint "bar" + +with_test_prefix "at bar+0" { + gdb_test "p foo" " = 2" + + gdb_test "stepi" +} + +with_test_prefix "at bar+1" { + gdb_test "p foo" " = " +} diff --git a/gdb/testsuite/lib/dwarf.exp b/gdb/testsuite/lib/dwarf.exp index f6348972968..81435ea4118 100644 --- a/gdb/testsuite/lib/dwarf.exp +++ b/gdb/testsuite/lib/dwarf.exp @@ -1244,7 +1244,6 @@ namespace eval Dwarf { # used, as indicated in the header of the section where the location # description is found. # - # (FIXME should use 'info complete' here.) # Each list's first element is the opcode, either short or long # forms are accepted. # FIXME argument handling @@ -1252,9 +1251,18 @@ namespace eval Dwarf { proc _location { body dwarf_version addr_size offset_size } { variable _constants + set collected_lines "" foreach line [split $body \n] { # Ignore blank lines, and allow embedded comments. - if {[lindex $line 0] == "" || [regexp -- {^[ \t]*#} $line]} { + if { [regexp -- {^[ \t]*$} $line] || [regexp -- {^[ \t]*#} $line] } { + continue + } + if { $collected_lines != "" } { + set line "$collected_lines\n$line" + set collected_lines "" + } + if { ! [info complete $line] } { + set collected_lines $line continue } set opcode [_map_name [lindex $line 0] _OP] @@ -1340,6 +1348,17 @@ namespace eval Dwarf { _op .2byte $argvec(label) } + DW_OP_entry_value { + _get_args $line $opcode body + set l1 [new_label "expr_start"] + set l2 [new_label "expr_end"] + _op .uleb128 "$l2 - $l1" "expression" + define_label $l1 + _location $argvec(body) $dwarf_version $addr_size \ + $offset_size + define_label $l2 + } + DW_OP_implicit_value { set l1 [new_label "value_start"] set l2 [new_label "value_end"]