1 # This testcase is part of GDB, the GNU debugger.
3 # Copyright 2022-2024 Free Software Foundation, Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Generate binaries imitating different ways source file paths can be passed to
19 # compilers. Test printing macros from those binaries.
23 require dwarf2_support
27 lassign [function_range main $srcdir/$subdir/$srcfile] \
32 # - TEST_NAME is the name of the test, used to differentiate the binaries.
33 # - LINES_VERSION is the version of the version of the .debug_line section to
35 # - DW_AT_NAME is the string to put in the compilation unit's DW_AT_name
37 # - MAIN_FILE_IDX is the file index the .debug_line and .debug_macro sections
38 # will use to refer to the main file.
39 # - DIRECTORIES is a list of directories to put in the .debug_line section
41 # - FILE_NAMES is a list of {name, directory index} pairs describing the files
42 # names to put in the .debug_line section header.
44 proc do_test { test_name lines_version DW_AT_name main_file_idx directories
46 with_test_prefix "test_name=$test_name" {
47 foreach_with_prefix is_64 {true false} {
48 # So we can access them in Dwarf::assemble...
49 set ::lines_version $lines_version
50 set ::DW_AT_name $DW_AT_name
51 set ::main_file_idx $main_file_idx
52 set ::directories $directories
53 set ::file_names $file_names
55 set 32_or_64 [expr $is_64 ? 64 : 32]
57 set asm_file [standard_output_file ${::testfile}-${test_name}-${32_or_64}.S]
58 Dwarf::assemble $asm_file {
59 declare_labels Llines cu_macros
61 # DW_AT_comp_dir is always the current working directory
62 # from which the compiler was invoked. We pretend the compiler was
63 # always launched from /tmp/cwd.
64 set comp_dir "/tmp/cwd"
68 {DW_AT_producer "My C Compiler"}
69 {DW_AT_language @DW_LANG_C11}
70 {DW_AT_name $::DW_AT_name}
71 {DW_AT_comp_dir $comp_dir}
72 {DW_AT_stmt_list $Llines DW_FORM_sec_offset}
73 {DW_AT_macros $cu_macros DW_FORM_sec_offset}
75 declare_labels int_type
77 int_type: DW_TAG_base_type {
78 {DW_AT_byte_size 4 DW_FORM_sdata}
79 {DW_AT_encoding @DW_ATE_signed}
84 {MACRO_AT_func {main}}
90 # Define the .debug_line section.
91 lines [list version $::lines_version] "Llines" {
92 foreach directory $::directories {
93 include_dir $directory
96 foreach file_name $::file_names {
97 lassign $file_name name dir_index
98 file_name $name $dir_index
101 # A line number program just good enough so that GDB can
102 # figure out we are stopped in main.
104 DW_LNS_set_file $::main_file_idx
105 DW_LNE_set_address $::main_start
109 DW_LNE_set_address "$::main_start + $::main_len"
114 # Define the .debug_macro section.
117 "debug-line-offset-label" $Llines
120 # A macro defined outside the main file, as if it was defined
121 # on the command line with -D.
123 # Clang has this bug where it puts the macros defined on
124 # the command-line after the main file portion (see
125 # PR 29034). We're not trying to replicate that here,
126 # this is not in the scope of this test.
128 define_strp 0 "THREE 3"
129 start_file 0 $::main_file_idx
130 # A macro defined at line 1 of the main file.
137 if { [prepare_for_testing "failed to prepare" ${::testfile}-${test_name}-${32_or_64} \
138 [list $::srcfile $asm_file] {nodebug}] } {
143 gdb_test_multiple "print main" "no complaints" {
144 -wrap -re "During symbol reading: .*" {
157 gdb_test "print ONE" " = 1"
158 gdb_test "print TWO" " = 2"
159 gdb_test "print THREE" " = 3"
164 # When adding a test here, please consider adding an equivalent case to the test
165 # of the same name in gdb.base.
167 # The following tests are based on the output of `gcc -gdwarf-5 -g3 <file>`,
168 # gcc 11 paired with gas from binutils 2.38.
171 do_test gcc11-ld238-dw5-filename 5 "test.c" 1 {
179 do_test gcc11-ld238-dw5-dot-filename 5 "./test.c" 1 {
188 do_test gcc11-ld238-dw5-dot-dot-cwd 5 "../cwd/test.c" 1 {
197 do_test gcc11-ld238-dw5-absolute-cwd 5 "/tmp/cwd/test.c" 1 {
206 do_test gcc11-ld238-dw5-dot-dot-other 5 "../other/test.c" 1 {
215 do_test gcc11-ld238-dw5-absolute-other 5 "/tmp/other/test.c" 1 {
223 # The following tests are based on the output of `gcc -gdwarf-4 -g3 <file>`,
224 # gcc 11 paired with gas from binutils 2.38. With -gdwarf-4, gcc generates a
225 # v4 (pre-standard) .debug_macro section.
228 do_test gcc11-ld238-dw4-filename 4 "test.c" 1 {
234 do_test gcc11-ld238-dw4-dot-filename 4 "./test.c" 1 {
241 do_test gcc11-ld238-dw4-dot-dot-cwd 4 "../cwd/test.c" 1 {
248 do_test gcc11-ld238-dw4-absolute-cwd 4 "/tmp/cwd/test.c" 1 {
255 do_test gcc11-ld238-dw4-dot-dot-other 4 "../other/test.c" 1 {
262 do_test gcc11-ld238-dw4-absolute-other 4 "/tmp/other/test.c" 1 {
268 # The following tests are based on the output of `clang-14 -gdwarf-5
269 # -fdebug-macro -g3 <file>` (using its built-in assembler)
272 do_test clang14-dw5-filename 5 "test.c" 0 {
279 do_test clang14-dw5-dot-filename 5 "test.c" 1 {
288 do_test clang14-dw5-dot-dot-cwd 5 "../cwd/test.c" 0 {
295 do_test clang14-dw5-absolute-cwd 5 "/tmp/cwd/test.c" 1 {
298 {"/tmp/cwd/test.c" 0}
303 do_test clang14-dw5-dot-dot-other 5 "../other/test.c" 0 {
306 {"../other/test.c" 0}
310 do_test clang14-dw5-absolute-other 5 "/tmp/other/test.c" 1 {
314 {"/tmp/other/test.c" 0}
318 # The following tests are based on the output of `clang-14 -gdwarf-4
319 # -fdebug-macro -g3 <file>` (using its built-in assembler). With -gdwarf-4,
320 # clang produces a .debug_macinfo section, not a .debug_macro section. But
321 # this test still creates a .debug_macro section, that's good enough for what
325 do_test clang14-dw4-filename 4 "test.c" 1 {
331 do_test clang14-dw4-dot-filename 4 "test.c" 1 {
338 do_test clang14-dw4-dot-dot-cwd 4 "../cwd/test.c" 1 {
345 do_test clang14-dw4-absolute-cwd 4 "/tmp/cwd/test.c" 1 {
351 do_test clang14-dw4-dot-dot-other 4 "../other/test.c" 1 {
358 do_test clang14-dw4-absolute-other 4 "/tmp/other/test.c" 1 {
364 # The following tests are based on the output of `gcc -gdwarf-5 -g3 <file>`,
365 # gcc 11 paired with gas from binutils 2.34 (Ubuntu 20.04). It generates a v5
366 # .debug_macro section, but a v3 .debug_line section.
369 do_test gcc11-ld234-dw5-filename 3 "test.c" 1 {
375 do_test gcc11-ld234-dw5-dot-filename 3 "./test.c" 1 {
382 do_test gcc11-ld234-dw5-dot-dot-cwd 3 "../cwd/test.c" 1 {
389 do_test gcc11-ld234-dw5-absolute-cwd 3 "/tmp/cwd/test.c" 1 {
396 do_test gcc11-ld234-dw5-dot-dot-other 3 "../other/test.c" 1 {
403 do_test gcc11-ld234-dw5-absolute-other 3 "/tmp/other/test.c" 1 {