1 # This testcase is part of GDB, the GNU debugger.
3 # Copyright 2022-2023 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 if {![dwarf2_support]} {
29 lassign [function_range main $srcdir/$subdir/$srcfile] \
34 # - TEST_NAME is the name of the test, used to differentiate the binaries.
35 # - LINES_VERSION is the version of the version of the .debug_line section to
37 # - DW_AT_NAME is the string to put in the compilation unit's DW_AT_name
39 # - MAIN_FILE_IDX is the file index the .debug_line and .debug_macro sections
40 # will use to refer to the main file.
41 # - DIRECTORIES is a list of directories to put in the .debug_line section
43 # - FILE_NAMES is a list of {name, directory index} pairs describing the files
44 # names to put in the .debug_line section header.
46 proc do_test { test_name lines_version DW_AT_name main_file_idx directories
48 with_test_prefix "test_name=$test_name" {
49 foreach_with_prefix is_64 {true false} {
50 # So we can access them in Dwarf::assemble...
51 set ::lines_version $lines_version
52 set ::DW_AT_name $DW_AT_name
53 set ::main_file_idx $main_file_idx
54 set ::directories $directories
55 set ::file_names $file_names
57 set 32_or_64 [expr $is_64 ? 64 : 32]
59 set asm_file [standard_output_file ${::testfile}-${test_name}-${32_or_64}.S]
60 Dwarf::assemble $asm_file {
61 declare_labels Llines cu_macros
63 # DW_AT_comp_dir is always the current working directory
64 # from which the compiler was invoked. We pretend the compiler was
65 # always launched from /tmp/cwd.
66 set comp_dir "/tmp/cwd"
70 {DW_AT_producer "My C Compiler"}
71 {DW_AT_language @DW_LANG_C11}
72 {DW_AT_name $::DW_AT_name}
73 {DW_AT_comp_dir $comp_dir}
74 {DW_AT_stmt_list $Llines DW_FORM_sec_offset}
75 {DW_AT_macros $cu_macros DW_FORM_sec_offset}
77 declare_labels int_type
79 int_type: DW_TAG_base_type {
80 {DW_AT_byte_size 4 DW_FORM_sdata}
81 {DW_AT_encoding @DW_ATE_signed}
86 {MACRO_AT_func {main}}
92 # Define the .debug_line section.
93 lines [list version $::lines_version] "Llines" {
94 foreach directory $::directories {
95 include_dir $directory
98 foreach file_name $::file_names {
99 lassign $file_name name dir_index
100 file_name $name $dir_index
103 # A line number program just good enough so that GDB can
104 # figure out we are stopped in main.
106 DW_LNS_set_file $::main_file_idx
107 DW_LNE_set_address $::main_start
111 DW_LNE_set_address "$::main_start + $::main_len"
116 # Define the .debug_macro section.
119 "debug-line-offset-label" $Llines
122 # A macro defined outside the main file, as if it was defined
123 # on the command line with -D.
125 # Clang has this bug where it puts the macros defined on
126 # the command-line after the main file portion (see
127 # PR 29034). We're not trying to replicate that here,
128 # this is not in the scope of this test.
130 start_file 0 $::main_file_idx
131 # A macro defined at line 1 of the main file.
138 if { [prepare_for_testing "failed to prepare" ${::testfile}-${test_name}-${32_or_64} \
139 [list $::srcfile $asm_file] {nodebug}] } {
147 gdb_test "print ONE" " = 1"
148 gdb_test "print TWO" " = 2"
153 # When adding a test here, please consider adding an equivalent case to the test
154 # of the same name in gdb.base.
156 # The following tests are based on the output of `gcc -gdwarf-5 -g3 <file>`,
157 # gcc 11 paired with gas from binutils 2.38.
160 do_test gcc11-ld238-dw5-filename 5 "test.c" 1 {
168 do_test gcc11-ld238-dw5-dot-filename 5 "./test.c" 1 {
177 do_test gcc11-ld238-dw5-dot-dot-cwd 5 "../cwd/test.c" 1 {
186 do_test gcc11-ld238-dw5-absolute-cwd 5 "/tmp/cwd/test.c" 1 {
195 do_test gcc11-ld238-dw5-dot-dot-other 5 "../other/test.c" 1 {
204 do_test gcc11-ld238-dw5-absolute-other 5 "/tmp/other/test.c" 1 {
212 # The following tests are based on the output of `gcc -gdwarf-4 -g3 <file>`,
213 # gcc 11 paired with gas from binutils 2.38. With -gdwarf-4, gcc generates a
214 # v4 (pre-standard) .debug_macro section.
217 do_test gcc11-ld238-dw4-filename 4 "test.c" 1 {
223 do_test gcc11-ld238-dw4-dot-filename 4 "./test.c" 1 {
230 do_test gcc11-ld238-dw4-dot-dot-cwd 4 "../cwd/test.c" 1 {
237 do_test gcc11-ld238-dw4-absolute-cwd 4 "/tmp/cwd/test.c" 1 {
244 do_test gcc11-ld238-dw4-dot-dot-other 4 "../other/test.c" 1 {
251 do_test gcc11-ld238-dw4-absolute-other 4 "/tmp/other/test.c" 1 {
257 # The following tests are based on the output of `clang-14 -gdwarf-5
258 # -fdebug-macro -g3 <file>` (using its built-in assembler)
261 do_test clang14-dw5-filename 5 "test.c" 0 {
268 do_test clang14-dw5-dot-filename 5 "test.c" 1 {
277 do_test clang14-dw5-dot-dot-cwd 5 "../cwd/test.c" 0 {
284 do_test clang14-dw5-absolute-cwd 5 "/tmp/cwd/test.c" 1 {
287 {"/tmp/cwd/test.c" 0}
292 do_test clang14-dw5-dot-dot-other 5 "../other/test.c" 0 {
295 {"../other/test.c" 0}
299 do_test clang14-dw5-absolute-other 5 "/tmp/other/test.c" 1 {
303 {"/tmp/other/test.c" 0}
307 # The following tests are based on the output of `clang-14 -gdwarf-4
308 # -fdebug-macro -g3 <file>` (using its built-in assembler). With -gdwarf-4,
309 # clang produces a .debug_macinfo section, not a .debug_macro section. But
310 # this test still creates a .debug_macro section, that's good enough for what
314 do_test clang14-dw4-filename 4 "test.c" 1 {
320 do_test clang14-dw4-dot-filename 4 "test.c" 1 {
327 do_test clang14-dw4-dot-dot-cwd 4 "../cwd/test.c" 1 {
334 do_test clang14-dw4-absolute-cwd 4 "/tmp/cwd/test.c" 1 {
340 do_test clang14-dw4-dot-dot-other 4 "../other/test.c" 1 {
347 do_test clang14-dw4-absolute-other 4 "/tmp/other/test.c" 1 {
353 # The following tests are based on the output of `gcc -gdwarf-5 -g3 <file>`,
354 # gcc 11 paired with gas from binutils 2.34 (Ubuntu 20.04). It generates a v5
355 # .debug_macro section, but a v3 .debug_line section.
358 do_test gcc11-ld234-dw5-filename 3 "test.c" 1 {
364 do_test gcc11-ld234-dw5-dot-filename 3 "./test.c" 1 {
371 do_test gcc11-ld234-dw5-dot-dot-cwd 3 "../cwd/test.c" 1 {
378 do_test gcc11-ld234-dw5-absolute-cwd 3 "/tmp/cwd/test.c" 1 {
385 do_test gcc11-ld234-dw5-dot-dot-other 3 "../other/test.c" 1 {
392 do_test gcc11-ld234-dw5-absolute-other 3 "/tmp/other/test.c" 1 {