xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.dwarf2/dw2-dir-file-name.exp (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
1# Copyright 2012-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/>.
15load_lib dwarf.exp
16
17# This test can only be run on targets which support DWARF-2 and use gas.
18if {![dwarf2_support]} {
19    return 0
20}
21
22# This test has hard-wired assumptions that host and build filenames are
23# the same, and assumes POSIX pathname syntax.
24if { [is_remote host] || [ishost *-*-mingw*] } {
25    unsupported "dw2-dir-file-name.exp can only run on local host"
26    return 0
27}
28
29# Find length of addresses in bytes.
30if {[is_64_target]} {
31    set addr_len 8
32} else {
33    set addr_len 4
34}
35
36standard_testfile
37set asmsrcfile [standard_output_file ${testfile}asm.S]
38set asmobjfile [standard_output_file ${testfile}asm.o]
39set srcabsdir [standard_output_file ${testfile}.d]
40set srctmpfile tmp-${testfile}.c
41
42# $srcdir may be relative.
43if {[file pathtype $srcabsdir] != "absolute"} {
44    untested "objdir pathtype is not absolute"
45    return -1
46}
47
48set f [open $asmsrcfile "w"]
49puts $f "/* DO NOT EDIT! GENERATED AUTOMATICALLY! */"
50
51proc out_cu { name cu_dir cu_name line_dir line_name } {
52    global f
53    global addr_len
54
55    puts -nonewline $f "\
56.L${name}_begin:
57	.4byte	.L${name}_end - .L${name}_start	/* Length of Compilation Unit */
58.L${name}_start:
59	.2byte	2				/* DWARF Version */
60	.4byte	.Labbrev1_begin			/* Offset into abbrev section */
61	.byte	${addr_len}			/* Pointer size */
62"
63    if { $cu_dir != "" } {
64	puts $f "  .uleb128 ABBREV_COMP_DIR_NAME /* Abbrev: DW_TAG_compile_unit */"
65    } else {
66	puts $f "  .uleb128 ABBREV_NAME /* Abbrev: DW_TAG_compile_unit */"
67    }
68    puts -nonewline $f "\
69	.ascii	\"GNU C\\0\"			/* DW_AT_producer */
70	.byte	2				/* DW_AT_language (DW_LANG_C) */
71	.4byte	.Lline_${name}_begin		/* DW_AT_stmt_list */
72	.${addr_len}byte ${name}_start		/* DW_AT_low_pc */
73	.${addr_len}byte ${name}_end		/* DW_AT_high_pc */
74"
75    if { $cu_dir != "" } {
76	puts $f "  .ascii $cu_dir /* DW_AT_comp_dir */"
77    }
78    puts -nonewline $f "\
79	.ascii	$cu_name			/* DW_AT_name */
80
81	.uleb128	3			/* Abbrev: DW_TAG_subprogram */
82	.asciz		\"${name}\"		/* DW_AT_name */
83	.${addr_len}byte ${name}_start		/* DW_AT_low_pc */
84	.${addr_len}byte ${name}_end		/* DW_AT_high_pc */
85
86	.byte		0			/* End of children of CU */
87.L${name}_end:
88"
89}
90
91proc out_line { name cu_dir cu_name line_dir line_name } {
92    global f
93    global addr_len
94
95    puts -nonewline $f "\
96.Lline_${name}_begin:
97	.4byte		.Lline_${name}_end - .Lline_${name}_start	/* Initial length */
98.Lline_${name}_start:
99	.2byte		2			/* Version */
100	.4byte		.Lline_${name}_lines - .Lline_${name}_hdr	/* header_length */
101.Lline_${name}_hdr:
102	.byte		1			/* Minimum insn length */
103	.byte		1			/* default_is_stmt */
104	.byte		1			/* line_base */
105	.byte		1			/* line_range */
106	.byte		4			/* opcode_base */
107
108	/* Standard lengths */
109	.byte		0
110	.byte		1
111	.byte		1
112
113	/* Include directories */
114"
115    if { $line_dir != "" } {
116	puts $f "  .ascii $line_dir"
117    }
118    puts -nonewline $f "\
119	.byte		0
120
121	/* File names */
122	.ascii	$line_name
123"
124    if { $line_dir != "" } {
125	puts $f "  .uleb128 1"
126    } else {
127	puts $f "  .uleb128 0"
128    }
129    puts -nonewline $f "\
130	.uleb128	0
131	.uleb128	0
132
133	.byte		0
134
135.Lline_${name}_lines:
136	.byte		3	/* DW_LNS_advance_line */
137	.sleb128	998	/* ... to 999 */
138	.byte		0	/* DW_LNE_set_address */
139	.uleb128	${addr_len}+1
140	.byte		2
141	.${addr_len}byte ${name}_start
142	.byte		1	/* DW_LNS_copy */
143	.byte		3	/* DW_LNS_advance_line */
144	.sleb128	1	/* ... to 1000 */
145	.byte		0	/* DW_LNE_set_address */
146	.uleb128	${addr_len}+1
147	.byte		2
148	.${addr_len}byte ${name}_end
149	.byte		1	/* DW_LNS_copy */
150	.byte		0	/* DW_LNE_end_of_sequence */
151	.uleb128	1
152	.byte		1
153.Lline_${name}_end:
154"
155}
156
157# IFSOME can be optionally _same or _different if >= 2 absolute directories are
158# provided.  Then in the _different case the overriden directories have invalid
159# XDIR value.
160
161proc out_unit { func compdir ldir file ifsame } {
162    set name "compdir_${compdir}_ldir_${ldir}_file_${file}${ifsame}"
163
164    if { $compdir == "missing_" } {
165	set cu_dir {}
166    } elseif { $compdir == "relative" } {
167	set cu_dir {COMPDIR "\0"}
168    } elseif { $compdir == "absolute" } {
169	set cu_dir {BDIR "/" COMPDIR "\0"}
170    } else {
171	error "compdir $compdir"
172    }
173
174    if { $ldir == "missing_" } {
175	set line_dir {}
176    } elseif { $ldir == "relative" } {
177	set line_dir {LDIR "\0"}
178    } elseif { $ldir == "absolute" } {
179	set line_dir {BDIR "/" LDIR "\0"}
180    } else {
181	error "ldir $ldir"
182    }
183
184    if { $file == "basename" } {
185	set cu_name {FILE "\0"}
186    } elseif { $file == "relative" } {
187	set cu_name {FDIR "/" FILE "\0"}
188    } elseif { $file == "absolute" } {
189	set cu_name {BDIR "/" FILE "\0"}
190    } else {
191	error "file $file"
192    }
193    set line_name $cu_name
194
195    if { "$ifsame" == "_different" } {
196	if { $file == "absolute" } {
197	    if { $ldir == "absolute" } {
198		set line_dir {XDIR "\0"}
199	    }
200	    if { $compdir == "absolute" } {
201		set cu_dir {XDIR "\0"}
202	    }
203	} elseif { $ldir == "absolute" } {
204	    if { $compdir == "absolute" } {
205		set cu_dir {XDIR "\0"}
206	    }
207	} else {
208	    error "not enough absolutes"
209	}
210    }
211
212    $func $name $cu_dir $cu_name $line_dir $line_name
213}
214
215proc out_diff { func compdir ldir file } {
216    set abscount 0
217    if { $compdir == "absolute" } {
218	incr abscount
219    }
220    if { $ldir == "absolute" } {
221	incr abscount
222    }
223    if { $file == "absolute" } {
224	incr abscount
225    }
226    if { $abscount <= 1 } {
227	out_unit $func $compdir $ldir $file ""
228    } else {
229	out_unit $func $compdir $ldir $file "_same"
230	out_unit $func $compdir $ldir $file "_different"
231    }
232}
233
234proc out_file { func compdir ldir } {
235    out_diff $func $compdir $ldir "basename"
236    out_diff $func $compdir $ldir "relative"
237    out_diff $func $compdir $ldir "absolute"
238}
239
240proc out_ldir { func compdir } {
241    out_file $func $compdir "missing_"
242    out_file $func $compdir "relative"
243    out_file $func $compdir "absolute"
244}
245
246proc out_compdir { func } {
247    out_ldir $func "missing_"
248    out_ldir $func "relative"
249    out_ldir $func "absolute"
250}
251
252puts -nonewline $f "\
253#define ABBREV_NAME 1
254#define ABBREV_COMP_DIR_NAME 2
255  .section .debug_info
256"
257out_compdir out_cu
258
259puts $f "  .section .debug_line"
260out_compdir out_line
261
262puts -nonewline $f "\
263	.section .debug_abbrev
264.Labbrev1_begin:
265
266	.uleb128	ABBREV_NAME		/* Abbrev code */
267	.uleb128	0x11			/* DW_TAG_compile_unit */
268	.byte		1			/* has_children */
269	.uleb128	0x25			/* DW_AT_producer */
270	.uleb128	0x8			/* DW_FORM_string */
271	.uleb128	0x13			/* DW_AT_language */
272	.uleb128	0xb			/* DW_FORM_data1 */
273	.uleb128	0x10			/* DW_AT_stmt_list */
274	.uleb128	0x6			/* DW_FORM_data4 */
275	.uleb128	0x11			/* DW_AT_low_pc */
276	.uleb128	0x1			/* DW_FORM_addr */
277	.uleb128	0x12			/* DW_AT_high_pc */
278	.uleb128	0x1			/* DW_FORM_addr */
279	.uleb128	0x3			/* DW_AT_name */
280	.uleb128	0x8			/* DW_FORM_string */
281	.byte		0x0			/* Terminator */
282	.byte		0x0			/* Terminator */
283
284	.uleb128	ABBREV_COMP_DIR_NAME	/* Abbrev code */
285	.uleb128	0x11			/* DW_TAG_compile_unit */
286	.byte		1			/* has_children */
287	.uleb128	0x25			/* DW_AT_producer */
288	.uleb128	0x8			/* DW_FORM_string */
289	.uleb128	0x13			/* DW_AT_language */
290	.uleb128	0xb			/* DW_FORM_data1 */
291	.uleb128	0x10			/* DW_AT_stmt_list */
292	.uleb128	0x6			/* DW_FORM_data4 */
293	.uleb128	0x11			/* DW_AT_low_pc */
294	.uleb128	0x1			/* DW_FORM_addr */
295	.uleb128	0x12			/* DW_AT_high_pc */
296	.uleb128	0x1			/* DW_FORM_addr */
297	.uleb128	0x1b			/* DW_AT_comp_dir */
298	.uleb128	0x8			/* DW_FORM_string */
299	.uleb128	0x3			/* DW_AT_name */
300	.uleb128	0x8			/* DW_FORM_string */
301	.byte		0x0			/* Terminator */
302	.byte		0x0			/* Terminator */
303
304	.uleb128	3			/* Abbrev code */
305	.uleb128	0x2e			/* DW_TAG_subprogram */
306	.byte		0			/* has_children */
307	.uleb128	0x3			/* DW_AT_name */
308	.uleb128	0x8			/* DW_FORM_string */
309	.uleb128	0x11			/* DW_AT_low_pc */
310	.uleb128	0x1			/* DW_FORM_addr */
311	.uleb128	0x12			/* DW_AT_high_pc */
312	.uleb128	0x1			/* DW_FORM_addr */
313	.byte		0x0			/* Terminator */
314	.byte		0x0			/* Terminator */
315
316	.byte		0x0			/* Terminator */
317	.byte		0x0			/* Terminator */
318"
319
320close $f
321
322set opts {}
323# Base directory.
324lappend opts "additional_flags=-DBDIR=\"${srcabsdir}\""
325# Incorrect directory which should never be visible from GDB.
326lappend opts "additional_flags=-DXDIR=\"${srcabsdir}/xdir\""
327# CU's DW_AT_comp_dir.
328lappend opts "additional_flags=-DCOMPDIR=\"compdir\""
329# .debug_line's directory.
330lappend opts "additional_flags=-DLDIR=\"ldir\""
331# CU's DW_AT_name and .debug_line's filename relative directory, if needed.
332lappend opts "additional_flags=-DFDIR=\"fdir\""
333# CU's DW_AT_name and .debug_line's filename.
334lappend opts "additional_flags=-DFILE=\"${srctmpfile}\""
335
336# Clang's integrated assembler doesn't support .ascii directives
337# with multiple string literals.
338if { [test_compiler_info clang*] } {
339    lappend opts "additional_flags=-fno-integrated-as"
340}
341
342if { [gdb_compile "${asmsrcfile} ${srcdir}/${subdir}/$srcfile" "${binfile}" executable $opts] != "" } {
343    untested "failed to compile"
344    return -1
345}
346
347set dirs {}
348foreach r {"" /rdir} {
349  foreach x {"" /xdir} {
350      foreach comp {"" /compdir} {
351	  foreach l {"" /ldir} {
352	      foreach f {"" /fdir} {
353		  lappend dirs $r$x$comp$l$f
354	      }
355	  }
356      }
357  }
358}
359
360proc pathexpand {prefix dirlst suffix} {
361    set retlst {}
362    foreach dir $dirlst {
363	lappend retlst "$prefix$dir$suffix"
364    }
365    return $retlst
366}
367
368set filelist [pathexpand $srcabsdir $dirs "/$srctmpfile"]
369set dircreatelist [pathexpand $srcabsdir $dirs ""]
370set dirremovelist [pathexpand $srcabsdir [lreverse $dirs] ""]
371
372remote_exec host "sh -c \"rm -f $filelist\""
373remote_exec host "sh -c \"rmdir $dirremovelist\""
374remote_exec host "sh -c \"mkdir $dircreatelist\""
375remote_exec host "sh -c \"for d in $dircreatelist; do cp ${srcdir}/${subdir}/${srcfile} \\\$d/${srctmpfile}; done\""
376
377clean_restart ${testfile}
378
379if ![runto_main] {
380    return -1
381}
382
383gdb_test "cd ${srcabsdir}/rdir" "Working directory [string_to_regexp ${srcabsdir}]/rdir\\." "cd .../rdir"
384
385proc test { func compdir filename } {
386    with_test_prefix "$func" {
387	# Clear the GDB cache.
388	gdb_test_no_output "set directories" ""
389
390	if {$compdir == ""} {
391	    set absolute "$filename"
392	} else {
393	    set absolute "$compdir/$filename"
394	}
395	if {[string index $absolute 0] != "/"} {
396	    error "not absolute"
397	}
398
399	gdb_breakpoint $func
400	gdb_continue_to_breakpoint $func "$func \\(\\) at .*"
401
402	gdb_test_no_output "set filename-display absolute"
403	verbose -log "expect: ${absolute}"
404	gdb_test "frame" " in $func \\(\\) at [string_to_regexp ${absolute}]:999" "absolute"
405
406	gdb_test_no_output "set filename-display basename"
407	verbose -log "expect: [file tail $filename]"
408	gdb_test "frame" " in $func \\(\\) at [string_to_regexp [file tail $filename]]:999" "basename"
409
410	gdb_test_no_output "set filename-display relative"
411	verbose -log "expect: $filename"
412	gdb_test "frame" " in $func \\(\\) at [string_to_regexp $filename]:999" "relative"
413    }
414}
415
416set bdir "${srcabsdir}"
417set file "${srctmpfile}"
418test "compdir_missing__ldir_missing__file_basename" "$bdir/rdir" "$file"
419test "compdir_missing__ldir_missing__file_relative" "$bdir/rdir" "fdir/$file"
420test "compdir_missing__ldir_missing__file_absolute" "" "$bdir/$file"
421test "compdir_missing__ldir_relative_file_basename" "$bdir/rdir" "ldir/$file"
422test "compdir_missing__ldir_relative_file_relative" "$bdir/rdir" "ldir/fdir/$file"
423test "compdir_missing__ldir_relative_file_absolute" "" "$bdir/$file"
424test "compdir_missing__ldir_absolute_file_basename" "" "$bdir/ldir/$file"
425test "compdir_missing__ldir_absolute_file_relative" "" "$bdir/ldir/fdir/$file"
426test "compdir_missing__ldir_absolute_file_absolute_same" "" "$bdir/$file"
427test "compdir_missing__ldir_absolute_file_absolute_different" "" "$bdir/$file"
428test "compdir_relative_ldir_missing__file_basename" "$bdir/rdir/compdir" "$file"
429test "compdir_relative_ldir_missing__file_relative" "$bdir/rdir/compdir" "fdir/$file"
430test "compdir_relative_ldir_missing__file_absolute" "" "$bdir/$file"
431test "compdir_relative_ldir_relative_file_basename" "$bdir/rdir/compdir" "ldir/$file"
432test "compdir_relative_ldir_relative_file_relative" "$bdir/rdir/compdir" "ldir/fdir/$file"
433test "compdir_relative_ldir_relative_file_absolute" "" "$bdir/$file"
434test "compdir_relative_ldir_absolute_file_basename" "" "$bdir/ldir/$file"
435test "compdir_relative_ldir_absolute_file_relative" "" "$bdir/ldir/fdir/$file"
436test "compdir_relative_ldir_absolute_file_absolute_same" "" "$bdir/$file"
437test "compdir_relative_ldir_absolute_file_absolute_different" "" "$bdir/$file"
438test "compdir_absolute_ldir_missing__file_basename" "$bdir/compdir" "$file"
439test "compdir_absolute_ldir_missing__file_relative" "$bdir/compdir" "fdir/$file"
440test "compdir_absolute_ldir_missing__file_absolute_same" "" "$bdir/$file"
441test "compdir_absolute_ldir_missing__file_absolute_different" "" "$bdir/$file"
442test "compdir_absolute_ldir_relative_file_basename" "$bdir/compdir" "ldir/$file"
443test "compdir_absolute_ldir_relative_file_relative" "$bdir/compdir" "ldir/fdir/$file"
444test "compdir_absolute_ldir_relative_file_absolute_same" "" "$bdir/$file"
445test "compdir_absolute_ldir_relative_file_absolute_different" "" "$bdir/$file"
446test "compdir_absolute_ldir_absolute_file_basename_same" "" "$bdir/ldir/$file"
447test "compdir_absolute_ldir_absolute_file_relative_different" "" "$bdir/ldir/fdir/$file"
448test "compdir_absolute_ldir_absolute_file_absolute_same" "" "$bdir/$file"
449test "compdir_absolute_ldir_absolute_file_absolute_different" "" "$bdir/$file"
450