1# Copyright 2018-2020 Free Software Foundation, Inc. 2 3# This program is free software; you can redistribute it and/or modify 4# it under the terms of the GNU General Public License as published by 5# the Free Software Foundation; either version 3 of the License, or 6# (at your option) any later version. 7# 8# This program is distributed in the hope that it will be useful, 9# but WITHOUT ANY WARRANTY; without even the implied warranty of 10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11# GNU General Public License for more details. 12# 13# You should have received a copy of the GNU General Public License 14# along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16# Test that GDB can support accesing fields of a structure if the 17# fields have non-standard names. Specifically, if the names are 18# reserved C type names like 'double', 'float', 'int', etc. 19# 20# We don't expect to that such structures should be seen in real C 21# code, but in some cases GDB will generate artificial structures, and 22# in some cases, these type names are the obvious choice for field 23# names. 24# 25# One specific example is RISC-V, the 64-bit floating point registers 26# are represented as a structure with the field names 'float' and 27# 'double'. 28 29load_lib dwarf.exp 30 31# This test can only be run on targets which support DWARF-2 and use 32# gas. 33if {![dwarf2_support]} { 34 return 0 35} 36 37standard_testfile dw2-unusual-field-names.c dw2-unusual-field-names.S 38set asm_file [standard_output_file $srcfile2] 39 40# We need to know the size of integer and address types in order to 41# write some of the debugging info we'd like to generate. 42# 43# For that, we ask GDB by debugging our test program. Any program 44# would do, but since we already have one specifically for this 45# testcase, might as well use that. 46if [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] { 47 return -1 48} 49set int_size [get_sizeof "int" -1] 50 51# Rebuild the test binary with the single field within the structure 52# renamed to FIELD_NAME, then test that we can access the field 53# through '.' and through '->'. 54proc run_test { field_name } { 55 global asm_file testfile srcfile subdir srcdir 56 global int_size 57 58 Dwarf::assemble $asm_file { 59 global srcdir subdir srcfile 60 global field_name int_size 61 62 cu {} { 63 DW_TAG_compile_unit { 64 {DW_AT_language @DW_LANG_C} 65 {DW_AT_name dw2-unusual-field-names.c} 66 {DW_AT_comp_dir /tmp} 67 } { 68 declare_labels itype ptype stype 69 70 itype: DW_TAG_base_type { 71 {DW_AT_byte_size $int_size DW_FORM_sdata} 72 {DW_AT_encoding @DW_ATE_signed} 73 {DW_AT_name int} 74 } 75 76 stype: DW_TAG_structure_type { 77 {DW_AT_name "foo"} 78 {DW_AT_byte_size $int_size DW_FORM_sdata} 79 } { 80 member { 81 {name $field_name} 82 {type :$itype} 83 {data_member_location 0 data1} 84 } 85 } 86 87 ptype: DW_TAG_pointer_type { 88 {DW_AT_type :$stype} 89 } 90 91 DW_TAG_variable { 92 {DW_AT_name obj} 93 {DW_AT_type :$stype} 94 {DW_AT_location { 95 DW_OP_addr [gdb_target_symbol obj] 96 } SPECIAL_expr} 97 {external 1 flag} 98 } 99 100 DW_TAG_variable { 101 {DW_AT_name ptr} 102 {DW_AT_type :$ptype} 103 {DW_AT_location { 104 DW_OP_addr [gdb_target_symbol ptr] 105 } SPECIAL_expr} 106 {external 1 flag} 107 } 108 } 109 } 110 } 111 112 if { [prepare_for_testing "failed to prepare" ${testfile} \ 113 [list $srcfile $asm_file] {nodebug}] } { 114 return -1 115 } 116 117 if ![runto_main] { 118 return -1 119 } 120 121 gdb_test "p obj.$field_name" " = 0" \ 122 "access a field named '$field_name' directly" 123 124 gdb_test "p ptr->$field_name" " = 0" \ 125 "access a field named '$field_name' through a pointer" 126 127 gdb_exit 128} 129 130foreach field_name { double float char byte long int short unsigned signed } { 131 run_test $field_name 132} 133