Home | History | Annotate | Line # | Download | only in gdb.dwarf2
      1 # Copyright 2017-2025 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 load_lib dwarf.exp
     17 
     18 # This test can only be run on targets which support DWARF-2 and use gas.
     19 require dwarf2_support
     20 
     21 standard_testfile main.c -dw.S
     22 
     23 # Make some DWARF for the test.
     24 set asm_file [standard_output_file $srcfile2]
     25 Dwarf::assemble $asm_file {
     26     global pair
     27 
     28     cu { addr_size 4 } {
     29 	compile_unit {} {
     30 	    declare_labels int_label ushort_label struct_label struct_label_2
     31 
     32 	    int_label: DW_TAG_base_type {
     33 		{DW_AT_byte_size 4 DW_FORM_udata}
     34 		{DW_AT_encoding @DW_ATE_unsigned}
     35 		{DW_AT_name "myint"}
     36 	    }
     37 	    ushort_label: DW_TAG_base_type {
     38 		{DW_AT_byte_size 2 DW_FORM_udata}
     39 		{DW_AT_encoding @DW_ATE_unsigned}
     40 		{DW_AT_name "myushort"}
     41 	    }
     42 
     43 	    struct_label: DW_TAG_structure_type {
     44 		{DW_AT_name "S"}
     45 		{DW_AT_byte_size 8 DW_FORM_udata}
     46 	    } {
     47 		DW_TAG_member {
     48 		    {DW_AT_name "a"}
     49 		    {DW_AT_type :${int_label}}
     50 		    {DW_AT_data_member_location 0 DW_FORM_udata}
     51 		}
     52 
     53 		DW_TAG_member {
     54 		    {DW_AT_name "b"}
     55 		    {DW_AT_type :${ushort_label}}
     56 		    {DW_AT_data_member_location 4 DW_FORM_udata}
     57 		}
     58 	    }
     59 
     60 	    struct_label_2: DW_TAG_structure_type {
     61 		{DW_AT_name "S_2"}
     62 		{DW_AT_byte_size 6 DW_FORM_udata}
     63 	    } {
     64 		DW_TAG_member {
     65 		    {DW_AT_name "a"}
     66 		    {DW_AT_type :${int_label}}
     67 		    {DW_AT_data_member_location 0 DW_FORM_udata}
     68 		}
     69 
     70 		DW_TAG_member {
     71 		    {DW_AT_name "b"}
     72 		    {DW_AT_type :${ushort_label}}
     73 		    {DW_AT_data_member_location 4 DW_FORM_udata}
     74 		}
     75 	    }
     76 
     77 	    DW_TAG_variable {
     78 		{DW_AT_name "s1"}
     79 		{DW_AT_type :${struct_label}}
     80 		{DW_AT_external 1 DW_FORM_flag}
     81 		{DW_AT_location {
     82 		    DW_OP_constu 1
     83 		    DW_OP_stack_value
     84 		    DW_OP_piece 4
     85 		    DW_OP_constu 0
     86 		    DW_OP_stack_value
     87 		    DW_OP_piece 2
     88 		} SPECIAL_expr}
     89 	    }
     90 
     91 	    DW_TAG_variable {
     92 		{DW_AT_name "s2"}
     93 		{DW_AT_type :${struct_label}}
     94 		{DW_AT_external 1 DW_FORM_flag}
     95 		{DW_AT_location {
     96 		    DW_OP_constu 1
     97 		    DW_OP_stack_value
     98 		    DW_OP_piece 4
     99 		    DW_OP_constu 0
    100 		    DW_OP_stack_value
    101 		    DW_OP_piece 8
    102 		} SPECIAL_expr}
    103 	    }
    104 
    105 	    DW_TAG_variable {
    106 		{DW_AT_name "s3"}
    107 		{DW_AT_type :${struct_label_2}}
    108 		{DW_AT_external 1 DW_FORM_flag}
    109 		{DW_AT_location {
    110 		    DW_OP_constu 0
    111 		    DW_OP_stack_value
    112 		    DW_OP_piece 4
    113 		    DW_OP_constu 1
    114 		    DW_OP_stack_value
    115 		    DW_OP_piece 2
    116 		} SPECIAL_expr}
    117 	    }
    118 	}
    119     }
    120 }
    121 
    122 if { [prepare_for_testing "failed to prepare" ${testfile} \
    123 			  [list $srcfile $asm_file] {nodebug}] } {
    124     return -1
    125 }
    126 
    127 gdb_test "p s1" " = {a = 1, b = 0}"
    128 gdb_test "p s2" \
    129     "access outside bounds of object referenced via synthetic pointer"
    130 
    131 # When fetching a lazy value, GDB typically tries to fetch the 'full'
    132 # object based on the enclosing type.  GDB does not support the reading
    133 # of a pieced value with a (possibly larger) enclosing type.  However,
    134 # the user may want to print a value casted to a different type,
    135 # e.g. print (<type> []) <variable>.  This cast causes an update of the
    136 # value's type.  In case of a pieced value, GDB failed to fetch the
    137 # value's content.
    138 # This test verifies that GDB can print a pieced value casted to a
    139 # different type.
    140 gdb_test "p (int \[\]) s1" " = \\{1, <optimized out>\\}"
    141 set re_little [string_to_regexp "{1, 0, 0, <optimized out>}"]
    142 set re_big  [string_to_regexp "{0, 1, 0, <optimized out>}"]
    143 gdb_test {p (short []) s1} " = ($re_little|$re_big)"
    144 
    145 # Test for correct output if the size of the original object is not a
    146 # multiple of the array's element size.
    147 gdb_test "p s3" " = {a = 0, b = 1}"
    148 gdb_test "p (int \[\]) s3" [multi_line \
    149 				"warning: array element type size does not divide object size in cast" \
    150 				"\\$\\d = \\{0\\}"]
    151