From patchwork Mon May 16 18:28:06 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Galvan X-Patchwork-Id: 12296 Received: (qmail 15022 invoked by alias); 16 May 2016 18:28:27 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 15004 invoked by uid 89); 16 May 2016 18:28:26 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.1 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy=arrays, *result, adjustments, 21697 X-HELO: mail-qk0-f173.google.com Received: from mail-qk0-f173.google.com (HELO mail-qk0-f173.google.com) (209.85.220.173) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Mon, 16 May 2016 18:28:16 +0000 Received: by mail-qk0-f173.google.com with SMTP id x7so101518567qkd.3 for ; Mon, 16 May 2016 11:28:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id; bh=2BsVh9nY7UVRnyMbxiQtIco0S/cvEQddd29NcinfrLg=; b=BHLlEC7ofMP1mM/3M97yvXSmeyriOgq1zAQTssQYD/6VJ2Km+eENR0WWgJweAwUjcu GRgrAAda6aYEpWaCt69EBBNTkUf7S4/Whg2FQToGZXlipSPDQHdTPwYYghq0+JOP+vnD OCWB1Lkxf+JDJKmYySE0Zkw8EQLbzshyfcZ3UXIfGEX2JwBNOl++KgpooV1/Oq3x85TP 5kkb9tMvnxTlvCLU1412rCvZe6i05Iud+w739U9w0dFE3k7m7edv3ZfXXTrJns4Bc/yN 3t7KG8Qf+3IM9x9qy63Cdp90Y00SE4rBoUjv6mF2XSEzz3H10YtvAwlHI/RFOOFL0S1T mkbA== X-Gm-Message-State: AOPr4FUQB2ghIvwgzVQy03uuQFBuYsM+WraozKCm9Resfoz43hhTOGa9srlU2UXU2rHdDw7A X-Received: by 10.55.76.134 with SMTP id z128mr29900336qka.90.1463423294041; Mon, 16 May 2016 11:28:14 -0700 (PDT) Received: from martin-galvan.dominio.tallertechnologies.com ([200.69.202.173]) by smtp.gmail.com with ESMTPSA id y85sm15301726qha.32.2016.05.16.11.28.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 16 May 2016 11:28:13 -0700 (PDT) From: Martin Galvan To: gdb-patches@sourceware.org, palves@redhat.com, daniel.gutson@tallertechnologies.com Subject: [PING 2][PATCH][PR gdb/19893] Fix handling of synthetic C++ references Date: Mon, 16 May 2016 15:28:06 -0300 Message-Id: <1463423286-16860-1-git-send-email-martin.galvan@tallertechnologies.com> https://sourceware.org/bugzilla/show_bug.cgi?id=19893 I've traced the main source of the problem to pieced_value_funcs.coerce_ref not being implemented. Since gdb always assumes references are implemented as pointers, this causes it to think that it's dealing with a NULL pointer, thus breaking any operations involving synthetic references. What I did here was implementing pieced_value_funcs.coerce_ref using some of the synthetic pointer handling code from indirect_pieced_value, as Pedro suggested. I also made a few adjustments to the reference printing code so that it correctly shows the "" string. I also wrote some unit tests based on Dwarf::assemble; these took a while to make because in most cases I needed a synthetic reference to a physical variable. Additionally, I started working on a unit test for classes that have a vtable, but ran into a few issues so that'll probably go in a future patch. One thing that should definitely be fixed is that proc function_range (called for MACRO_AT_func) will always try to compile/link using gcc with the default options instead of g++, thus breaking C++ compilations that require e.g. libstdc++. As always, I'm open to feedback of any kind. Thanks! gdb/ChangeLog: 2016-05-16 Martin Galvan * dwarf2loc.c (coerce_pieced_ref, indirect_synthetic_pointer, fetch_const_value_from_synthetic_pointer): New functions. (indirect_pieced_value): Move lower half to indirect_synthetic_pointer. (pieced_value_funcs): Implement coerce_ref. * valops.c (value_addr): Call coerce_ref for synthetic references. * valprint.c (valprint_check_validity): Return true for synthetic references. (generic_val_print_ref): Don't print '@address' for synthetic references. gdb/testsuite/ChangeLog: 2016-05-16 Martin Galvan * gdb.dwarf2/implref.exp: Rename to... * gdb.dwarf2/implref-const.exp: ...this. * gdb.dwarf2/implref-array.c: New file. * gdb.dwarf2/implref-array.exp: Likewise. * gdb.dwarf2/implref-global.c: Likewise. * gdb.dwarf2/implref-global.exp: Likewise. * gdb.dwarf2/implref-struct.c: Likewise. * gdb.dwarf2/implref-struct.exp: Likewise. --- gdb/dwarf2loc.c | 113 +++++++++++++----- gdb/testsuite/gdb.dwarf2/implref-array.c | 27 +++++ gdb/testsuite/gdb.dwarf2/implref-array.exp | 168 +++++++++++++++++++++++++++ gdb/testsuite/gdb.dwarf2/implref-const.exp | 120 +++++++++++++++++++ gdb/testsuite/gdb.dwarf2/implref-global.c | 27 +++++ gdb/testsuite/gdb.dwarf2/implref-global.exp | 124 ++++++++++++++++++++ gdb/testsuite/gdb.dwarf2/implref-struct.c | 43 +++++++ gdb/testsuite/gdb.dwarf2/implref-struct.exp | 173 ++++++++++++++++++++++++++++ gdb/testsuite/gdb.dwarf2/implref.exp | 98 ---------------- gdb/valops.c | 18 ++- gdb/valprint.c | 20 +++- 11 files changed, 796 insertions(+), 135 deletions(-) create mode 100644 gdb/testsuite/gdb.dwarf2/implref-array.c create mode 100644 gdb/testsuite/gdb.dwarf2/implref-array.exp create mode 100644 gdb/testsuite/gdb.dwarf2/implref-const.exp create mode 100644 gdb/testsuite/gdb.dwarf2/implref-global.c create mode 100644 gdb/testsuite/gdb.dwarf2/implref-global.exp create mode 100644 gdb/testsuite/gdb.dwarf2/implref-struct.c create mode 100644 gdb/testsuite/gdb.dwarf2/implref-struct.exp delete mode 100644 gdb/testsuite/gdb.dwarf2/implref.exp diff --git a/gdb/dwarf2loc.c b/gdb/dwarf2loc.c index ba6ed42..829d5e6 100644 --- a/gdb/dwarf2loc.c +++ b/gdb/dwarf2loc.c @@ -2055,6 +2055,66 @@ get_frame_address_in_block_wrapper (void *baton) return get_frame_address_in_block ((struct frame_info *) baton); } +static struct value * +fetch_const_value_from_synthetic_pointer (sect_offset die, LONGEST byte_offset, + struct dwarf2_per_cu_data *per_cu, + struct type *type) +{ + struct value *result = NULL; + struct obstack temp_obstack; + struct cleanup *cleanup; + const gdb_byte *bytes; + LONGEST len; + obstack_init (&temp_obstack); + cleanup = make_cleanup_obstack_free (&temp_obstack); + bytes = dwarf2_fetch_constant_bytes (die, per_cu, &temp_obstack, &len); + + if (bytes != NULL) + { + if (byte_offset >= 0 + && byte_offset + TYPE_LENGTH (TYPE_TARGET_TYPE (type)) <= len) + { + bytes += byte_offset; + result = value_from_contents (TYPE_TARGET_TYPE (type), bytes); + } + else + invalid_synthetic_pointer (); + } + else + result = allocate_optimized_out_value (TYPE_TARGET_TYPE (type)); + + do_cleanups (cleanup); + + return result; +} + +/* Fetch the value pointed at by a synthetic pointer. */ +static struct value * +indirect_synthetic_pointer (sect_offset die, LONGEST byte_offset, + struct dwarf2_per_cu_data *per_cu, + struct frame_info *frame, struct type *type) +{ + struct value *result = NULL; + + /* Fetch the location expression of the DIE we're pointing to. */ + struct dwarf2_locexpr_baton baton + = dwarf2_fetch_die_loc_sect_off (die, per_cu, + get_frame_address_in_block_wrapper, frame); + + /* If pointed-to DIE has a DW_AT_location, evaluate it and return the + resulting value. Otherwise, it may have a DW_AT_const_value instead, + or it may've been optimized out. */ + if (baton.data != NULL) + result = dwarf2_evaluate_loc_desc_full (TYPE_TARGET_TYPE (type), frame, + baton.data, baton.size, + baton.per_cu, byte_offset); + else + result = fetch_const_value_from_synthetic_pointer (die, byte_offset, per_cu, + type); + + return result; +} + /* An implementation of an lval_funcs method to indirect through a pointer. This handles the synthetic pointer case when needed. */ @@ -2109,6 +2169,7 @@ indirect_pieced_value (struct value *value) break; } + gdb_assert (piece); frame = get_selected_frame (_("No frame selected.")); /* This is an offset requested by GDB, such as value subscripts. @@ -2126,44 +2187,36 @@ indirect_pieced_value (struct value *value) TYPE_LENGTH (type), byte_order); byte_offset += piece->v.ptr.offset; - gdb_assert (piece); - baton - = dwarf2_fetch_die_loc_sect_off (piece->v.ptr.die, c->per_cu, - get_frame_address_in_block_wrapper, - frame); + return indirect_synthetic_pointer (piece->v.ptr.die, byte_offset, c->per_cu, + frame, type); +} - if (baton.data != NULL) - return dwarf2_evaluate_loc_desc_full (TYPE_TARGET_TYPE (type), frame, - baton.data, baton.size, baton.per_cu, - byte_offset); +static struct value *coerce_pieced_ref (const struct value *value) +{ + struct value *result = NULL; + struct type *type = check_typedef (value_type (value)); + if (value_bits_synthetic_pointer (value, value_embedded_offset (value), + TARGET_CHAR_BIT * TYPE_LENGTH (type))) { - struct obstack temp_obstack; - struct cleanup *cleanup; - const gdb_byte *bytes; - LONGEST len; - struct value *result; + const struct piece_closure *closure + = (struct piece_closure *) value_computed_closure (value); - obstack_init (&temp_obstack); - cleanup = make_cleanup_obstack_free (&temp_obstack); + /* gdb represents synthetic pointers as pieced values with a single + piece. */ + gdb_assert (closure != NULL); + gdb_assert (closure->n_pieces == 1); - bytes = dwarf2_fetch_constant_bytes (piece->v.ptr.die, c->per_cu, - &temp_obstack, &len); - if (bytes == NULL) - result = allocate_optimized_out_value (TYPE_TARGET_TYPE (type)); - else - { - if (byte_offset < 0 - || byte_offset + TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > len) - invalid_synthetic_pointer (); - bytes += byte_offset; - result = value_from_contents (TYPE_TARGET_TYPE (type), bytes); + struct frame_info *frame = get_selected_frame (_("No frame selected.")); + + result = indirect_synthetic_pointer (closure->pieces->v.ptr.die, + closure->pieces->v.ptr.offset, + closure->per_cu, frame, type); } + /* Else: not a synthetic reference; do nothing. */ - do_cleanups (cleanup); return result; } -} static void * copy_pieced_value_closure (const struct value *v) @@ -2200,7 +2253,7 @@ static const struct lval_funcs pieced_value_funcs = { read_pieced_value, write_pieced_value, indirect_pieced_value, - NULL, /* coerce_ref */ + coerce_pieced_ref, check_pieced_synthetic_pointer, copy_pieced_value_closure, free_pieced_value_closure diff --git a/gdb/testsuite/gdb.dwarf2/implref-array.c b/gdb/testsuite/gdb.dwarf2/implref-array.c new file mode 100644 index 0000000..2470412 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/implref-array.c @@ -0,0 +1,27 @@ +/* Copyright (C) 2016 Free Software Foundation, Inc. + + This file is part of GDB. + + 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 . */ + +/* Test program for synthetic C++ references to arrays. */ + +int array[5] = {0, 1, 2, 3, 4}; + +int +main (void) +{ + asm ("main_label: .globl main_label"); + return 0; +} diff --git a/gdb/testsuite/gdb.dwarf2/implref-array.exp b/gdb/testsuite/gdb.dwarf2/implref-array.exp new file mode 100644 index 0000000..4569de7 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/implref-array.exp @@ -0,0 +1,168 @@ +# Copyright 2016 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 . + +# Test a C++ reference marked with DW_OP_GNU_implicit_pointer. +# The referenced value is a global array whose location is a DW_OP_addr. + +if [skip_cplus_tests] { + continue +} + +load_lib dwarf.exp + +# This test can only be run on targets which support DWARF-2 and use gas. +if ![dwarf2_support] { + return 0 +} + +# We'll place the output of Dwarf::assemble in implref-array.S. +standard_testfile .c .S + +# ${testfile} is now "implref-array". srcfile2 is "implref-array.S". +set executable ${testfile} +set asm_file [standard_output_file ${srcfile2}] + +# We need to know the size of integer and address types in order +# to write some of the debugging info we'd like to generate. +# +# For that, we ask GDB by debugging our implref-array program. +# Any program would do, but since we already have implref-array +# specifically for this testcase, might as well use that. +if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } { + untested ${testfile}.exp + return -1 +} + +set array_length [get_valueof "/u" "sizeof(array) / sizeof(array\[0\])" 5] + +# Create the DWARF. We need a regular variable which represents the array, and +# a reference to it that'll be marked with DW_OP_GNU_implicit_pointer. +# The variable must be global so that its name is an exported symbol that we +# can reference from the DWARF using gdb_target_symbol. +Dwarf::assemble ${asm_file} { + global srcdir subdir srcfile array_length + + cu {} { + DW_TAG_compile_unit { + {DW_AT_language @DW_LANG_C_plus_plus} + } { + declare_labels int_label sizetype_label array_label variable_label ref_label + set int_size [get_sizeof "int" 4] + set upper_bound [expr ${array_length} - 1] + + # gdb always assumes references are implemented as pointers. + set addr_size [get_sizeof "void *" 4] + + int_label: DW_TAG_base_type { + {DW_AT_byte_size ${int_size} DW_FORM_udata} + {DW_AT_encoding @DW_ATE_signed} + {DW_AT_name "int"} + } + + sizetype_label: DW_TAG_base_type { + {DW_AT_byte_size ${int_size} DW_FORM_udata} + {DW_AT_encoding @DW_ATE_unsigned} + {DW_AT_name "sizetype"} + } + + array_label: DW_TAG_array_type { + {DW_AT_type :${int_label}} + } { + DW_TAG_subrange_type { + {DW_AT_type :${sizetype_label}} + {DW_AT_lower_bound 0 DW_FORM_udata} + {DW_AT_upper_bound ${upper_bound} DW_FORM_udata} + } + } + + ref_label: DW_TAG_reference_type { + {DW_AT_byte_size ${addr_size} DW_FORM_udata} + {DW_AT_type :${array_label}} + } + + variable_label: DW_TAG_variable { + {DW_AT_name "array"} + {DW_AT_type :${array_label}} + {DW_AT_external 1 DW_FORM_flag} + {DW_AT_location {DW_OP_addr [gdb_target_symbol "array"]} SPECIAL_expr} + } + + DW_TAG_subprogram { + {MACRO_AT_func { "main" "${srcdir}/${subdir}/${srcfile}" }} + {DW_AT_type :${int_label}} + {DW_AT_external 1 DW_FORM_flag} + } { + DW_TAG_variable { + {DW_AT_name "ref"} + {DW_AT_type :${ref_label}} + {DW_AT_location {DW_OP_GNU_implicit_pointer ${variable_label} 0} SPECIAL_expr} + } + } + } + } +} + +if [prepare_for_testing ${testfile}.exp ${executable} [list ${asm_file} ${srcfile}] {}] { + return -1 +} + +# DW_OP_GNU_implicit_pointer implementation requires a valid frame. +if ![runto_main] { + return -1 +} + +# This matches e.g. '(int (&)[5])' +set ref_type [format {\(int \(&\)\[%d\]\)} ${array_length}] + +# This matches e.g. '(int (*)[5])' +set ptr_type [format {\(int \(\*\)\[%d\]\)} ${array_length}] + +# Contents of the array. Trim leading/trailing whitespace, '{' and '}' +# since they confuse TCL to no end. +set contents [get_valueof "" "array" "\{0, 1, 2, 3, 4\}"] +set contents [string trim ${contents}] +set contents [string trim ${contents} "{}"] + +# Doing 'print ref' should show us e.g. '(int (&)[5]) : {0, 1, 2, 3, 4}'. +gdb_test "print ref" " = ${ref_type} : \\{${contents}\\}" "print ref" + +# Doing 'print &ref' should show us e.g. '(int (*)[5]) 0xdeadbeef '. +gdb_test "print &ref" " = ${ptr_type} \[\[:xdigit:\]x\]+ " "print &ref" + +# gdb assumes C++ references are implemented as pointers, and print &(&ref) +# shows us the underlying pointer's address. Since in this case there's no +# physical pointer, gdb should tell us so. +gdb_test "print &(&ref)" "Attempt to take address of value not located in memory." "print &(&ref)" + +# Test assignment through the synthetic reference. +set first_value 10 +gdb_test_no_output "set (ref\[0\] = ${first_value})" "set (ref\[0\] = ${first_value})" + +# This matches e.g. '{10, 1, 2, 3, 4}' +set new_contents [format {\{%d[\d,\s]+\}} ${first_value}] + +# Doing 'print ref' should now show us e.g. +# '(int (&)[5]) : {10, 1, 2, 3, 4}'. +gdb_test "print ref" " = ${ref_type} : ${new_contents}" "print ref (after assignment)" +gdb_test "print array" " = ${new_contents}" "print array (after assignment)" + +# Test treating the array as a pointer. +set second_value 20 +set new_contents [format {\{%d, %d[\d,\s]+\}} ${first_value} ${second_value}] + +gdb_test "print *ref" " = ${first_value}" "print *ref" +gdb_test_no_output "set (*(ref + 1) = ${second_value})" "set (*(ref + 1) = ${second_value})" +gdb_test "print ref\[1\]" " = ${second_value}" "print ref\[1\]" +gdb_test "print array" " = ${new_contents}" "print array (after second assignment)" diff --git a/gdb/testsuite/gdb.dwarf2/implref-const.exp b/gdb/testsuite/gdb.dwarf2/implref-const.exp new file mode 100644 index 0000000..cfae3d6 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/implref-const.exp @@ -0,0 +1,120 @@ +# Copyright 2016 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 . + +# Test a C++ reference marked with DW_OP_GNU_implicit_pointer. +# The referenced value is a DW_AT_const_value. + +if [skip_cplus_tests] { + continue +} + +load_lib dwarf.exp + +# This test can only be run on targets which support DWARF-2 and use gas. +if ![dwarf2_support] { + return 0 +} + +# We'll place the output of Dwarf::assemble in implref-const.S. +standard_testfile main.c .S + +# ${testfile} is now "implref-const". srcfile2 is "implref-const.S". +set executable ${testfile} +set asm_file [standard_output_file ${srcfile2}] + +# We need to know the size of integer and address types in order +# to write some of the debugging info we'd like to generate. +# +# For that, we ask GDB by debugging our implref-const program. +# Any program would do, but since we already have implref-const +# specifically for this testcase, might as well use that. +if [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] { + untested ${testfile}.exp + return -1 +} + +# Create the DWARF. We need a regular variable and a reference to it that'll +# be marked with DW_OP_GNU_implicit_pointer. +Dwarf::assemble ${asm_file} { + global srcdir subdir srcfile + + cu {} { + DW_TAG_compile_unit { + {DW_AT_language @DW_LANG_C_plus_plus} + } { + declare_labels int_label const_label variable_label ref_label + set int_size [get_sizeof "int" 4] + + # gdb always assumes references are implemented as pointers. + set addr_size [get_sizeof "void *" 4] + set var_value 42 + + int_label: DW_TAG_base_type { + {DW_AT_byte_size ${int_size} DW_FORM_udata} + {DW_AT_encoding @DW_ATE_signed} + {DW_AT_name "int"} + } + + ref_label: DW_TAG_reference_type { + {DW_AT_byte_size ${addr_size} DW_FORM_udata} + {DW_AT_type :${int_label}} + } + + const_label: DW_TAG_const_type { + {DW_AT_type :${ref_label}} + } + + DW_TAG_subprogram { + {MACRO_AT_func { "main" "${srcdir}/${subdir}/${srcfile}" }} + {DW_AT_type :${int_label}} + {DW_AT_external 1 DW_FORM_flag} + } { + variable_label: DW_TAG_variable { + {DW_AT_name "var"} + {DW_AT_type :${int_label}} + {DW_AT_const_value ${var_value} DW_FORM_udata} + } + + DW_TAG_variable { + {DW_AT_name "ref"} + {DW_AT_type :${const_label}} + {DW_AT_location {DW_OP_GNU_implicit_pointer ${variable_label} 0} SPECIAL_expr} + } + } + } + } +} + +if [prepare_for_testing ${testfile}.exp ${executable} [list ${asm_file} ${srcfile}] {}] { + return -1 +} + +# DW_OP_GNU_implicit_pointer implementation requires a valid frame. +if ![runto_main] { + return -1 +} + +# Doing 'print ref' should show us e.g. '(int &) : 42'. +set value_output {\(int &\) : \d+} +gdb_test "print ref" " = ${value_output}" "print ref" + +# The variable isn't located in memory, thus we can't take its address. +gdb_test "print &var" "Can't take address of \"var\" which isn't an lvalue." "print &var" +gdb_test "print &ref" "Attempt to take address of value not located in memory." "print &ref" + +# gdb assumes C++ references are implemented as pointers, and print &(&ref) +# shows us the underlying pointer's address. +# Since in this case there's no physical pointer, gdb should tell us so. +gdb_test "print &(&ref)" "Attempt to take address of value not located in memory." "print &(&ref)" diff --git a/gdb/testsuite/gdb.dwarf2/implref-global.c b/gdb/testsuite/gdb.dwarf2/implref-global.c new file mode 100644 index 0000000..7814a30 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/implref-global.c @@ -0,0 +1,27 @@ +/* Copyright (C) 2016 Free Software Foundation, Inc. + + This file is part of GDB. + + 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 . */ + +/* Test program for synthetic C++ references to global variables. */ + +int global_var = 42; + +int +main (void) +{ + asm ("main_label: .globl main_label"); + return 0; +} diff --git a/gdb/testsuite/gdb.dwarf2/implref-global.exp b/gdb/testsuite/gdb.dwarf2/implref-global.exp new file mode 100644 index 0000000..3694b03 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/implref-global.exp @@ -0,0 +1,124 @@ +# Copyright 2016 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 . + +# Test a C++ reference marked with DW_OP_GNU_implicit_pointer. +# The referenced value is a global variable whose location is a DW_OP_addr. + +if [skip_cplus_tests] { + continue +} + +load_lib dwarf.exp + +# This test can only be run on targets which support DWARF-2 and use gas. +if ![dwarf2_support] { + return 0 +} + +# We'll place the output of Dwarf::assemble in implref-global.S. +standard_testfile .c .S + +# ${testfile} is now "implref-global". srcfile2 is "implref-global.S". +set executable ${testfile} +set asm_file [standard_output_file ${srcfile2}] + +# We need to know the size of integer and address types in order +# to write some of the debugging info we'd like to generate. +# +# For that, we ask GDB by debugging our implref-global program. +# Any program would do, but since we already have implref-global +# specifically for this testcase, might as well use that. +if [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] { + untested ${testfile}.exp + return -1 +} + +# Create the DWARF. We need a regular variable and a reference to it that'll +# be marked with DW_OP_GNU_implicit_pointer. The variable must be global so +# that its name is an exported symbol that we can reference from the DWARF +# using gdb_target_symbol. +Dwarf::assemble ${asm_file} { + global srcdir subdir srcfile + + cu {} { + DW_TAG_compile_unit { + {DW_AT_language @DW_LANG_C_plus_plus} + } { + declare_labels int_label variable_label ref_label + set int_size [get_sizeof "int" 4] + + # gdb always assumes references are implemented as pointers. + set addr_size [get_sizeof "void *" 4] + + int_label: DW_TAG_base_type { + {DW_AT_byte_size ${int_size} DW_FORM_udata} + {DW_AT_encoding @DW_ATE_signed} + {DW_AT_name "int"} + } + + ref_label: DW_TAG_reference_type { + {DW_AT_byte_size ${addr_size} DW_FORM_udata} + {DW_AT_type :${int_label}} + } + + variable_label: DW_TAG_variable { + {DW_AT_name "global_var"} + {DW_AT_type :${int_label}} + {DW_AT_external 1 DW_FORM_flag} + {DW_AT_location {DW_OP_addr [gdb_target_symbol "global_var"]} SPECIAL_expr} + } + + DW_TAG_subprogram { + {MACRO_AT_func { "main" "${srcdir}/${subdir}/${srcfile}" }} + {DW_AT_type :${int_label}} + {DW_AT_external 1 DW_FORM_flag} + } { + DW_TAG_variable { + {DW_AT_name "ref"} + {DW_AT_type :${ref_label}} + {DW_AT_location {DW_OP_GNU_implicit_pointer ${variable_label} 0} SPECIAL_expr} + } + } + } + } +} + +if [prepare_for_testing ${testfile}.exp ${executable} [list ${asm_file} ${srcfile}] {}] { + return -1 +} + +# DW_OP_GNU_implicit_pointer implementation requires a valid frame. +if ![runto_main] { + return -1 +} + +# Doing 'print ref' should show us e.g. '(int &) : 42'. +set value_output {\(int &\) : \d+} +gdb_test "print ref" " = ${value_output}" "print ref" + +# Doing 'print &ref' should show us e.g. '(int *) 0xdeadbeef '. +set addr_output {\(int \*\) [[:xdigit:]x]+ } +gdb_test "print &ref" " = ${addr_output}" "print &ref" + +# gdb assumes C++ references are implemented as pointers, and print &(&ref) +# shows us the underlying pointer's address. Since in this case there's no +# physical pointer, gdb should tell us so. +gdb_test "print &(&ref)" "Attempt to take address of value not located in memory." "print &(&ref)" + +# Test assignment through the synthetic reference. +set new_value 10 +gdb_test_no_output "set (ref = ${new_value})" "set (ref = ${new_value})" +gdb_test "print ref" " = \\(int &\\) : ${new_value}" "print ref (after assignment)" +gdb_test "print global_var" " = ${new_value}" "print global_var (after assignment)" diff --git a/gdb/testsuite/gdb.dwarf2/implref-struct.c b/gdb/testsuite/gdb.dwarf2/implref-struct.c new file mode 100644 index 0000000..c241264 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/implref-struct.c @@ -0,0 +1,43 @@ +/* Copyright (C) 2016 Free Software Foundation, Inc. + + This file is part of GDB. + + 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 . */ + +/* Test program for synthetic C++ references to structs. */ + +struct S { + int a; + int b; + int c; +}; + +struct S s1 = { + .a = 0, + .b = 1, + .c = 2 +}; + +struct S s2 = { + .a = 10, + .b = 11, + .c = 12 +}; + +int +main (void) +{ + asm ("main_label: .globl main_label"); + return 0; +} diff --git a/gdb/testsuite/gdb.dwarf2/implref-struct.exp b/gdb/testsuite/gdb.dwarf2/implref-struct.exp new file mode 100644 index 0000000..cb2be7b --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/implref-struct.exp @@ -0,0 +1,173 @@ +# Copyright 2016 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 . + +# Test a C++ reference marked with DW_OP_GNU_implicit_pointer. +# The referenced value is a global struct whose location is a DW_OP_addr. + +if [skip_cplus_tests] { + continue +} + +load_lib dwarf.exp + +# This test can only be run on targets which support DWARF-2 and use gas. +if ![dwarf2_support] { + return 0 +} + +# We'll place the output of Dwarf::assemble in implref-struct.S. +standard_testfile .c .S + +# ${testfile} is now "implref-struct". srcfile2 is "implref-struct.S". +set executable ${testfile} +set asm_file [standard_output_file ${srcfile2}] + +# We need to know the size of integer and address types in order +# to write some of the debugging info we'd like to generate. +# +# For that, we ask GDB by debugging our implref-struct program. +# Any program would do, but since we already have implref-struct +# specifically for this testcase, might as well use that. +if [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile} {debug c++}] { + untested ${testfile}.exp + return -1 +} + +# Create the DWARF. We need a regular variable for the struct and a reference +# to it that'll be marked with DW_OP_GNU_implicit_pointer. The variable must be +# global so that its name is an exported symbol that can we can reference from +# the DWARF using gdb_target_symbol. +Dwarf::assemble ${asm_file} { + global srcdir subdir srcfile + + cu {} { + DW_TAG_compile_unit { + {DW_AT_language @DW_LANG_C_plus_plus} + } { + declare_labels int_label struct_label variable_label ref_label + set int_size [get_sizeof "int" 4] + + # gdb always assumes references are implemented as pointers. + set addr_size [get_sizeof "void *" 4] + set S_size [get_sizeof "S" 12] + + # The compiler shouldn't introduce structure padding here. + set b_offset 4 + set c_offset 8 + + int_label: DW_TAG_base_type { + {DW_AT_byte_size ${int_size} DW_FORM_udata} + {DW_AT_encoding @DW_ATE_signed} + {DW_AT_name "int"} + } + + struct_label: DW_TAG_structure_type { + {DW_AT_name "S"} + {DW_AT_byte_size ${S_size} 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 :${int_label}} + {DW_AT_data_member_location ${b_offset} DW_FORM_udata} + } + + DW_TAG_member { + {DW_AT_name "c"} + {DW_AT_type :${int_label}} + {DW_AT_data_member_location ${c_offset} DW_FORM_udata} + } + } + + ref_label: DW_TAG_reference_type { + {DW_AT_byte_size ${addr_size} DW_FORM_udata} + {DW_AT_type :${struct_label}} + } + + variable_label: DW_TAG_variable { + {DW_AT_name "s1"} + {DW_AT_type :${struct_label}} + {DW_AT_external 1 DW_FORM_flag} + {DW_AT_location {DW_OP_addr [gdb_target_symbol "s1"]} SPECIAL_expr} + } + + DW_TAG_variable { + {DW_AT_name "s2"} + {DW_AT_type :${struct_label}} + {DW_AT_external 1 DW_FORM_flag} + {DW_AT_location {DW_OP_addr [gdb_target_symbol "s2"]} SPECIAL_expr} + } + + DW_TAG_subprogram { + {MACRO_AT_func { "main" "${srcdir}/${subdir}/${srcfile}" }} + {DW_AT_type :${int_label}} + {DW_AT_external 1 DW_FORM_flag} + } { + DW_TAG_variable { + {DW_AT_name "ref"} + {DW_AT_type :${ref_label}} + {DW_AT_location {DW_OP_GNU_implicit_pointer ${variable_label} 0} SPECIAL_expr} + } + } + } + } +} + +if [prepare_for_testing ${testfile}.exp ${executable} [list ${asm_file} ${srcfile}] {}] { + return -1 +} + +# DW_OP_GNU_implicit_pointer implementation requires a valid frame. +if ![runto_main] { + return -1 +} + +# Returns the struct members. +proc get_members {var default} { + set members [get_valueof "" ${var} ${default}] + + # Trim leading/trailing whitespace, '{' and '}' since they confuse TCL + # to no end. + set members [string trim ${members}] + set members [string trim ${members} "{}"] + + return ${members} +} + +# Values of the struct members. +set s1_members [get_members "s1" "\{a = 0, b = 1, c = 2\}"] +set s2_members [get_members "s2" "\{a = 10, b = 11, c = 12\}"] + +# Doing 'print ref' should show us e.g. '(S &) : {a = 0, b = 1, c = 2}'. +gdb_test "print ref" " = \\(S &\\) : \\{${s1_members}\\}" "print ref" + +# Doing 'print &ref' should show us e.g. '(S *) 0xdeadbeef '. +set addr_output {\(S \*\) [[:xdigit:]x]+ } +gdb_test "print &ref" " = ${addr_output}" "print &ref" + +# gdb assumes C++ references are implemented as pointers, and print &(&ref) +# shows us the underlying pointer's address. Since in this case there's no +# physical pointer, gdb should tell us so. +gdb_test "print &(&ref)" "Attempt to take address of value not located in memory." "print &(&ref)" + +# Test assignment through the synthetic reference. +gdb_test_no_output "set (ref = s2)" "set (ref = s2)" +gdb_test "print ref" " = \\(S &\\) : \\{${s2_members}\\}" "print ref (after assignment)" +gdb_test "print s1" " = \\{${s2_members}\\}" "print s1 (after assignment)" diff --git a/gdb/testsuite/gdb.dwarf2/implref.exp b/gdb/testsuite/gdb.dwarf2/implref.exp deleted file mode 100644 index ca4766e..0000000 --- a/gdb/testsuite/gdb.dwarf2/implref.exp +++ /dev/null @@ -1,98 +0,0 @@ -# Copyright 2016 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 . - -# Test C++ references marked with DW_OP_GNU_implicit_pointer. - -# TODO: Add more test statements after fixing bug #19893: -# https://sourceware.org/bugzilla/show_bug.cgi?id=19893. - -load_lib dwarf.exp - -# This test can only be run on targets which support DWARF-2 and use gas. -if {![dwarf2_support]} { - return 0 -} - -# We'll place the output of Dwarf::assemble in implref.S. -standard_testfile main.c .S - -# ${testfile} is now "implref". srcfile2 is "implref.S". -set executable ${testfile} -set asm_file [standard_output_file ${srcfile2}] - -# Create the DWARF. We need a regular variable and a reference to it that'll -# be marked with DW_OP_GNU_implicit_pointer. -Dwarf::assemble $asm_file { - global srcdir subdir srcfile - - cu { version 3 addr_size 4 } { - DW_TAG_compile_unit { - {DW_AT_producer "GNU C++ 4.8.4"} - {DW_AT_language @DW_LANG_C_plus_plus} - } { - declare_labels int_label const_label variable_label ref_label - - int_label: DW_TAG_base_type { - {DW_AT_byte_size 4 DW_FORM_udata} - {DW_AT_encoding @DW_ATE_signed} - {DW_AT_name "int"} - } - - ref_label: DW_TAG_reference_type { - {DW_AT_byte_size 4 DW_FORM_udata} - {DW_AT_type :${int_label}} - } - - const_label: DW_TAG_const_type { - {DW_AT_type :${ref_label}} - } - - DW_TAG_subprogram { - {MACRO_AT_func { "main" "${srcdir}/${subdir}/${srcfile}" }} - {DW_AT_type :${int_label}} - {DW_AT_external 1 DW_FORM_flag} - } { - variable_label: DW_TAG_variable { - {DW_AT_name "var"} - {DW_AT_type :${int_label}} - {DW_AT_const_value 42 DW_FORM_udata} - } - - DW_TAG_variable { - {DW_AT_name "ref"} - {DW_AT_type :${const_label}} - {DW_AT_location {DW_OP_GNU_implicit_pointer ${variable_label} 0} SPECIAL_expr} - } - } - } - } -} - -if [prepare_for_testing ${testfile}.exp ${executable} "${asm_file} ${srcfile}" {}] { - return -1 -} - -# DW_OP_GNU_implicit_pointer implementation requires a valid frame. -if ![runto_main] { - return -1 -} - -gdb_test "print ref" " = \\(int &\\) " "print ref" -gdb_test "print &ref" " = \\(int \\*\\) " "print &ref" - -# gdb assumes C++ references are implemented as pointers, and print &(&ref) -# shows us the underlying pointer's address. -# Since in this case there's no physical pointer, gdb should tell us so. -gdb_test "print &\(&ref\)" "Attempt to take address of value not located in memory." "print &(&ref)" diff --git a/gdb/valops.c b/gdb/valops.c index bbbbd00..d38e982 100644 --- a/gdb/valops.c +++ b/gdb/valops.c @@ -1465,16 +1465,21 @@ value_addr (struct value *arg1) if (TYPE_CODE (type) == TYPE_CODE_REF) { + if (value_bits_synthetic_pointer (arg1, value_embedded_offset (arg1), + TARGET_CHAR_BIT * TYPE_LENGTH (type))) + arg1 = coerce_ref (arg1); + else + { /* Copy the value, but change the type from (T&) to (T*). We keep the same location information, which is efficient, and allows &(&X) to get the location containing the reference. Do the same to its enclosing type for consistency. */ - struct type *type_ptr - = lookup_pointer_type (TYPE_TARGET_TYPE (type)); - struct type *enclosing_type - = check_typedef (value_enclosing_type (arg1)); - struct type *enclosing_type_ptr - = lookup_pointer_type (TYPE_TARGET_TYPE (enclosing_type)); + struct type *type_ptr = + lookup_pointer_type (TYPE_TARGET_TYPE (type)); + struct type *enclosing_type = + check_typedef (value_enclosing_type (arg1)); + struct type *enclosing_type_ptr = + lookup_pointer_type (TYPE_TARGET_TYPE (enclosing_type)); arg2 = value_copy (arg1); deprecated_set_value_type (arg2, type_ptr); @@ -1482,6 +1487,7 @@ value_addr (struct value *arg1) return arg2; } + } if (TYPE_CODE (type) == TYPE_CODE_FUNC) return value_coerce_function (arg1); diff --git a/gdb/valprint.c b/gdb/valprint.c index 720942b..71808fd 100644 --- a/gdb/valprint.c +++ b/gdb/valprint.c @@ -336,7 +336,9 @@ valprint_check_validity (struct ui_file *stream, TARGET_CHAR_BIT * TYPE_LENGTH (type))) { fputs_filtered (_(""), stream); - return 0; + + /* C++ references should be valid even if they're synthetic. */ + return TYPE_CODE (type) == TYPE_CODE_REF; } if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type))) @@ -495,11 +497,27 @@ generic_val_print_ref (struct type *type, const gdb_byte *valaddr, if (options->addressprint) { + const int is_synthetic = + value_bits_synthetic_pointer (original_value, + TARGET_CHAR_BIT * embedded_offset, + TARGET_CHAR_BIT * TYPE_LENGTH (type)); + if (!is_synthetic) + { CORE_ADDR addr = extract_typed_address (valaddr + embedded_offset, type); fprintf_filtered (stream, "@"); fputs_filtered (paddress (gdbarch, addr), stream); + } + /* Else, we have a synthetic reference. Don't print '@address'; we'll + show '' instead through valprint_check_validity. + Notice however that 'set print object on' will still show '@address'. + This happens because value_bits_synthetic_pointer returns false if + original value is not 'lval_computed'. While that's usually the case, + if options->objectprint is true, c_value_print will call value_addr + on the reference, which coerces synthetic references and returns a + 'not_lval'. */ + if (options->deref_ref) fputs_filtered (": ", stream); }