1# This testcase is part of GDB, the GNU debugger.
2
3# Copyright 2022-2023 Free Software Foundation, Inc.
4
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.
9#
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.
14#
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/>.
17
18# Generate binaries imitating different ways source file paths can be passed to
19# compilers.  Test printing macros from those binaries.
20
21load_lib dwarf.exp
22
23if {![dwarf2_support]} {
24    return 0
25}
26
27standard_testfile .c
28
29lassign [function_range main $srcdir/$subdir/$srcfile] \
30    main_start main_len
31
32# Run one test.
33#
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
36#    generate.
37#  - DW_AT_NAME is the string to put in the compilation unit's DW_AT_name
38#    attribute.
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
42#    header
43#  - FILE_NAMES is a list of {name, directory index} pairs describing the files
44#    names to put in the .debug_line section header.
45
46proc do_test { test_name lines_version DW_AT_name main_file_idx directories
47	       file_names } {
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
56	    set ::is_64 $is_64
57	    set 32_or_64 [expr $is_64 ? 64 : 32]
58
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
62
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"
67
68		cu {} {
69		    DW_TAG_compile_unit {
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}
76		    } {
77			declare_labels int_type
78
79			int_type: DW_TAG_base_type {
80			    {DW_AT_byte_size 4 DW_FORM_sdata}
81			    {DW_AT_encoding  @DW_ATE_signed}
82			    {DW_AT_name int}
83			}
84
85			DW_TAG_subprogram {
86			    {MACRO_AT_func {main}}
87			    {type :$int_type}
88			}
89		    }
90		}
91
92		# Define the .debug_line section.
93		lines [list version $::lines_version] "Llines" {
94		    foreach directory $::directories {
95			include_dir $directory
96		    }
97
98		    foreach file_name $::file_names {
99			lassign $file_name name dir_index
100			file_name $name $dir_index
101		    }
102
103		    # A line number program just good enough so that GDB can
104		    # figure out we are stopped in main.
105		    program {
106			DW_LNS_set_file $::main_file_idx
107			DW_LNE_set_address $::main_start
108			line 10
109			DW_LNS_copy
110
111			DW_LNE_set_address "$::main_start + $::main_len"
112			DW_LNE_end_sequence
113		    }
114		}
115
116		# Define the .debug_macro section.
117		macro {
118		    cu_macros: unit {
119			"debug-line-offset-label" $Llines
120			"is-64" $::is_64
121		    } {
122			# A macro defined outside the main file, as if it was defined
123			# on the command line with -D.
124			#
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.
129			define 0 "ONE 1"
130			start_file 0 $::main_file_idx
131			    # A macro defined at line 1 of the main file.
132			    define 1 "TWO 2"
133			end_file
134		    }
135		}
136	    }
137
138	    if { [prepare_for_testing "failed to prepare" ${::testfile}-${test_name}-${32_or_64} \
139		      [list $::srcfile $asm_file] {nodebug}] } {
140		return
141	    }
142
143	    if ![runto_main] {
144		return
145	    }
146
147	    gdb_test "print ONE" " = 1"
148	    gdb_test "print TWO" " = 2"
149	}
150    }
151}
152
153# When adding a test here, please consider adding an equivalent case to the test
154# of the same name in gdb.base.
155
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.
158
159## test.c
160do_test gcc11-ld238-dw5-filename 5 "test.c" 1 {
161    "/tmp/cwd"
162} {
163    {"test.c" 0}
164    {"test.c" 0}
165}
166
167## ./test.c
168do_test gcc11-ld238-dw5-dot-filename 5 "./test.c" 1 {
169    "/tmp/cwd"
170    "."
171} {
172    {"test.c" 1}
173    {"test.c" 1}
174}
175
176## ../cwd/test.c
177do_test gcc11-ld238-dw5-dot-dot-cwd 5 "../cwd/test.c" 1 {
178    "/tmp/cwd"
179    "../cwd"
180} {
181    {"test.c" 1}
182    {"test.c" 1}
183}
184
185## /tmp/cwd/test.c
186do_test gcc11-ld238-dw5-absolute-cwd 5 "/tmp/cwd/test.c" 1 {
187    "/tmp/cwd"
188    "/tmp/cwd"
189} {
190    {"test.c" 1}
191    {"test.c" 0}
192}
193
194## ../other/test.c
195do_test gcc11-ld238-dw5-dot-dot-other 5 "../other/test.c" 1 {
196    "/tmp/cwd"
197    "../other"
198} {
199    {"test.c" 1}
200    {"test.c" 1}
201}
202
203## /tmp/other/test.c
204do_test gcc11-ld238-dw5-absolute-other 5 "/tmp/other/test.c" 1 {
205    "/tmp/cwd"
206    "/tmp/other"
207} {
208    {"test.c" 1}
209    {"test.c" 1}
210}
211
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.
215
216## test.c
217do_test gcc11-ld238-dw4-filename 4 "test.c" 1 {
218} {
219    {"test.c" 0}
220}
221
222## ./test.c
223do_test gcc11-ld238-dw4-dot-filename 4 "./test.c" 1 {
224    "."
225} {
226    {"test.c" 1}
227}
228
229## ../cwd/test.c
230do_test gcc11-ld238-dw4-dot-dot-cwd 4 "../cwd/test.c" 1 {
231    "../cwd"
232} {
233    {"test.c" 1}
234}
235
236## /tmp/cwd/test.c
237do_test gcc11-ld238-dw4-absolute-cwd 4 "/tmp/cwd/test.c" 1 {
238    "/tmp/cwd"
239} {
240    {"test.c" 1}
241}
242
243## ../other/test.c
244do_test gcc11-ld238-dw4-dot-dot-other 4 "../other/test.c" 1 {
245    "../other"
246} {
247    {"test.c" 1}
248}
249
250## /tmp/other/test.c
251do_test gcc11-ld238-dw4-absolute-other 4 "/tmp/other/test.c" 1 {
252    "/tmp/other"
253} {
254    {"test.c" 1}
255}
256
257# The following tests are based on the output of `clang-14 -gdwarf-5
258# -fdebug-macro -g3 <file>` (using its built-in assembler)
259
260## test.c
261do_test clang14-dw5-filename 5 "test.c" 0 {
262    "/tmp/cwd"
263} {
264    {"test.c" 0}
265}
266
267## ./test.c
268do_test clang14-dw5-dot-filename 5 "test.c" 1 {
269    "/tmp/cwd"
270    "."
271} {
272    {"test.c" 0}
273    {"test.c" 1}
274}
275
276## ../cwd/test.c
277do_test clang14-dw5-dot-dot-cwd 5 "../cwd/test.c" 0 {
278    "/tmp/cwd"
279} {
280    {"../cwd/test.c" 0}
281}
282
283## /tmp/cwd/test.c
284do_test clang14-dw5-absolute-cwd  5 "/tmp/cwd/test.c" 1 {
285    "/tmp/cwd"
286} {
287    {"/tmp/cwd/test.c" 0}
288    {"test.c" 0}
289}
290
291## ../other/test.c
292do_test clang14-dw5-dot-dot-other 5 "../other/test.c" 0 {
293    "/tmp/cwd"
294} {
295    {"../other/test.c" 0}
296}
297
298## /tmp/other/test.c
299do_test clang14-dw5-absolute-other 5 "/tmp/other/test.c" 1 {
300    "/tmp/cwd"
301    "/tmp"
302} {
303    {"/tmp/other/test.c" 0}
304    {"other/test.c" 1}
305}
306
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
311# we want to test.
312
313## test.c
314do_test clang14-dw4-filename 4 "test.c" 1 {
315} {
316    {"test.c" 0}
317}
318
319## ./test.c
320do_test clang14-dw4-dot-filename 4 "test.c" 1 {
321    "."
322} {
323    {"test.c" 1}
324}
325
326## ../cwd/test.c
327do_test clang14-dw4-dot-dot-cwd 4 "../cwd/test.c" 1 {
328    "../cwd"
329} {
330    {"test.c" 1}
331}
332
333## /tmp/cwd/test.c
334do_test clang14-dw4-absolute-cwd  4 "/tmp/cwd/test.c" 1 {
335} {
336    {"test.c" 0}
337}
338
339## ../other/test.c
340do_test clang14-dw4-dot-dot-other 4 "../other/test.c" 1 {
341    "../other"
342} {
343    {"test.c" 1}
344}
345
346## /tmp/other/test.c
347do_test clang14-dw4-absolute-other 4 "/tmp/other/test.c" 1 {
348    "/tmp"
349} {
350    {"other/test.c" 1}
351}
352
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.
356
357## test.c
358do_test gcc11-ld234-dw5-filename 3 "test.c" 1 {
359} {
360    {"test.c" 0}
361}
362
363## ./test.c
364do_test gcc11-ld234-dw5-dot-filename 3 "./test.c" 1 {
365    "."
366} {
367    {"test.c" 1}
368}
369
370## ../cwd/test.c
371do_test gcc11-ld234-dw5-dot-dot-cwd 3 "../cwd/test.c" 1 {
372    "../cwd"
373} {
374    {"test.c" 1}
375}
376
377## /tmp/cwd/test.c
378do_test gcc11-ld234-dw5-absolute-cwd 3 "/tmp/cwd/test.c" 1 {
379    "/tmp/cwd"
380} {
381    {"test.c" 1}
382}
383
384## ../other/test.c
385do_test gcc11-ld234-dw5-dot-dot-other 3 "../other/test.c" 1 {
386    "../other"
387} {
388    {"test.c" 1}
389}
390
391## /tmp/other/test.c
392do_test gcc11-ld234-dw5-absolute-other 3 "/tmp/other/test.c" 1 {
393    "/tmp/other"
394} {
395    {"test.c" 1}
396}
397