From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id 9BC8E3858D33; Tue, 10 Jan 2023 21:30:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9BC8E3858D33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1673386230; bh=BQRHqT9iRlw6/WhTFDTMgsY6p16ApSGEdLjsVpwQ2sQ=; h=From:To:Subject:Date:From; b=RvE3fLYIc3WEzQME9pqkM/Pzg2cYat4+R1EKdshHeZg3fSgR8UB+o5ioE0JO4pkef GGN7MeH2j2JeawK4w8UYBBILJk4GTOvr/Yi8WXUGO1sFTInhW092l9ZnzRg9FGh2+H UePnSgnxobTB70z8LfP/32xgypRe3XX2ip9sXztI= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom Tromey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] gdb/dwarf2: Fix 'rw_pieced_value' for values casted to different type. X-Act-Checkin: binutils-gdb X-Git-Author: Rohr, Stephan X-Git-Refname: refs/heads/master X-Git-Oldrev: 7987c4636abedbf4cd5658ac594e3b936eb43d91 X-Git-Newrev: 38190b2f01051795b15867359a4273e6cb0ade12 Message-Id: <20230110213030.9BC8E3858D33@sourceware.org> Date: Tue, 10 Jan 2023 21:30:30 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D38190b2f0105= 1795b15867359a4273e6cb0ade12 commit 38190b2f01051795b15867359a4273e6cb0ade12 Author: Rohr, Stephan Date: Wed Dec 21 14:12:44 2022 +0100 gdb/dwarf2: Fix 'rw_pieced_value' for values casted to different type. =20 The 'rw_pieced_value' function is executed when fetching a (lazy) variable described by 'DW_OP_piece' or 'DW_OP_bit_piece'. The function checks the 'type' and 'enclosing_type' fields of the value for identity. =20 * The 'type' field describes the type of a value. * In most cases, the 'enclosing_type' field is identical to the 'type' field. * Scenarios where the 'type' and 'enclosing_type' of an object differ are described in 'gdb/value.c'. Possible cases are: * If a value represents a C++ object, then the 'type' field gives the object's compile-time type. If the object actually belongs to some class derived from `type', perhaps with other base classes and additional members, then `type' is just a subobject of the real thing, and the full object is probably larger than `type' would suggest. * If 'type' is a dynamic class (i.e. one with a vtable), then GDB can actually determine the object's run-time type by looking at the run-time type information in the vtable. GDB may then elect to read the entire object. * If the user casts a variable to a different type (e.g. 'print ( []) '), the value's type is updated before reading the value. =20 If a lazy value is fetched, GDB allocates space based on the enclosing type's length and typically reads the 'full' object. This is not implemented for pieced values and causes an internal error if 'type' and 'enclosing_type' of a value are not identical. =20 However, GDB can read the value based on its type. Thus, this patch fixes the previously mentioned cases by removing the check for identity. =20 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=3D28605 =20 gdb/ChangeLog: 2022-04-13 Stephan Rohr =20 * dwarf2/loc.c (rw_pieced_value): Fix check on 'type' and 'enlcosing_type' when reading pieced value 'v'. =20 gdb/testsuite/ChangeLog: 2022-04-13 Stephan Rohr =20 * gdb.dwarf2/shortpiece.exp: Added test cases. Diff: --- gdb/dwarf2/expr.c | 3 -- gdb/testsuite/gdb.dwarf2/shortpiece.exp | 52 +++++++++++++++++++++++++++++= +++- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/gdb/dwarf2/expr.c b/gdb/dwarf2/expr.c index 0c5ce8c2605..eecd18f59c9 100644 --- a/gdb/dwarf2/expr.c +++ b/gdb/dwarf2/expr.c @@ -161,9 +161,6 @@ rw_pieced_value (value *v, value *from, bool check_opti= mized) } else { - if (value_type (v) !=3D value_enclosing_type (v)) - internal_error (_("Should not be able to create a lazy value with " - "an enclosing type")); if (check_optimized) v_contents =3D nullptr; else diff --git a/gdb/testsuite/gdb.dwarf2/shortpiece.exp b/gdb/testsuite/gdb.dw= arf2/shortpiece.exp index e1fb78e6217..f16506adc44 100644 --- a/gdb/testsuite/gdb.dwarf2/shortpiece.exp +++ b/gdb/testsuite/gdb.dwarf2/shortpiece.exp @@ -29,7 +29,7 @@ Dwarf::assemble $asm_file { =20 cu { addr_size 4 } { compile_unit {} { - declare_labels int_label ushort_label struct_label + declare_labels int_label ushort_label struct_label struct_label_2 =20 int_label: DW_TAG_base_type { {DW_AT_byte_size 4 DW_FORM_udata} @@ -59,6 +59,23 @@ Dwarf::assemble $asm_file { } } =20 + struct_label_2: DW_TAG_structure_type { + {DW_AT_name "S_2"} + {DW_AT_byte_size 6 DW_FORM_udata} + } { + DW_TAG_member { + {DW_AT_name "a"} + {DW_AT_type :${int_label}} + {DW_AT_data_member_location 0 DW_FORM_udata} + } + + DW_TAG_member { + {DW_AT_name "b"} + {DW_AT_type :${ushort_label}} + {DW_AT_data_member_location 4 DW_FORM_udata} + } + } + DW_TAG_variable { {DW_AT_name "s1"} {DW_AT_type :${struct_label}} @@ -86,6 +103,20 @@ Dwarf::assemble $asm_file { DW_OP_piece 8 } SPECIAL_expr} } + + DW_TAG_variable { + {DW_AT_name "s3"} + {DW_AT_type :${struct_label_2}} + {DW_AT_external 1 DW_FORM_flag} + {DW_AT_location { + DW_OP_constu 0 + DW_OP_stack_value + DW_OP_piece 4 + DW_OP_constu 1 + DW_OP_stack_value + DW_OP_piece 2 + } SPECIAL_expr} + } } } } @@ -98,3 +129,22 @@ if { [prepare_for_testing "failed to prepare" ${testfil= e} \ gdb_test "p s1" " =3D {a =3D 1, b =3D 0}" gdb_test "p s2" \ "access outside bounds of object referenced via synthetic pointer" + +# When fetching a lazy value, GDB typically tries to fetch the 'full' +# object based on the enclosing type. GDB does not support the reading +# of a pieced value with a (possibly larger) enclosing type. However, +# the user may want to print a value casted to a different type, +# e.g. print ( []) . This cast causes an update of the +# value's type. In case of a pieced value, GDB failed to fetch the +# value's content. +# This test verifies that GDB can print a pieced value casted to a +# different type. +gdb_test "p (int \[\]) s1" " =3D \\{1\\, 0\\}" +gdb_test "p (short \[\]) s1" " =3D \\{1\\, 0\\, 0\\, \\= }" + +# Test for correct output if the size of the original object is not a +# multiple of the array's element size. +gdb_test "p s3" " =3D {a =3D 0, b =3D 1}" +gdb_test "p (int \[\]) s3" [multi_line \ + "warning: array element type size does not divide object size in cast"= \ + "\\$\\d =3D \\{0\\}"]