xref: /netbsd-src/external/gpl3/gdb/dist/gdb/testsuite/gdb.base/gdb-index-err.exp (revision f8cf1a9151c7af1cb0bd8b09c13c66bca599c027)
1# Copyright 2023-2024 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# Test some error messages that can arise from the 'save gdb-index'
17# command.
18
19standard_testfile
20
21if {[prepare_for_testing "prepare for test" $testfile $srcfile] == -1} {
22    return -1
23}
24
25# This test isn't going to work when the board file automatically adds
26# an index section, or if the debug information is split into a
27# separate objfile.
28set index_type [get_index_type $binfile "check debug style"]
29if { $index_type ne "cooked" } {
30    unsupported "cannot test without a cooked index"
31    return -1
32}
33
34
35# The name of a directory that doesn't exist.
36set bad_dir [standard_output_file "non-existent"]
37
38# Try to write the index into a non-existent directory.
39foreach_with_prefix flag { "" "-dwarf-5" } {
40    gdb_test "save gdb-index ${flag} ${bad_dir}" \
41	"Error while writing index for `[string_to_regexp $binfile]': `[string_to_regexp $bad_dir]': No such file or directory\\." \
42	"try to write index to non-existent directory"
43}
44
45# Create a text-file.
46set text_file [standard_output_file "text-file"]
47set fd [open $text_file w]
48puts $fd "A line of text.\n"
49close $fd
50
51# Try to write the index into something that is not a directory.
52foreach_with_prefix flag { "" "-dwarf-5" } {
53    gdb_test "save gdb-index ${flag} ${text_file}" \
54	"Error while writing index for `[string_to_regexp $binfile]': `[string_to_regexp $text_file]': Is not a directory\\." \
55	"try to write index to something that's not a directory"
56}
57
58# Create a directory which can't be written too.
59set non_writable_dir [standard_output_file "private"]
60remote_exec host "mkdir -p ${non_writable_dir}"
61remote_exec host "chmod u-w,g-w ${non_writable_dir}"
62
63# Try to write the index into a non-writable directory.
64foreach_with_prefix flag { "" "-dwarf-5" } {
65    set test "try to write index to a non-writable directory"
66    if { [root_user] } {
67	unsupported $test
68	continue
69    }
70
71    gdb_test "save gdb-index ${flag} ${non_writable_dir}" \
72	"Error while writing index for `[string_to_regexp $binfile]': couldn't open `[string_to_regexp $non_writable_dir]/${gdb_test_file_name}.*': Permission denied\\." \
73	$test
74}
75
76# Create copies of the executable, we will add an index section to
77# each of these.
78remote_exec host "cp $binfile ${binfile}.gdb_index"
79remote_exec host "cp $binfile ${binfile}.dwarf_5"
80
81# Create a directory in which we can try to generate the index files.
82set already_indexed_dir [standard_output_file "already_indexed"]
83remote_exec host "mkdir -p $already_indexed_dir"
84
85foreach_with_prefix flag { "" "-dwarf-5" } {
86    if { $flag eq "" } {
87	set extension "gdb_index"
88    } else {
89	set extension "dwarf_5"
90    }
91
92    # Add the index section to the executable.
93    clean_restart ${binfile}.${extension}
94    gdb_assert {[ensure_gdb_index ${binfile}.${extension} ${flag}] == 1} \
95	"add index to executable"
96
97    # Reload the executable (which now has an index), and try to
98    # generate and index from it.  This will fail.
99    clean_restart ${binfile}.${extension}
100    gdb_test "save gdb-index ${flag} $already_indexed_dir" \
101	"Error while writing index for `[string_to_regexp $binfile.$extension]': Cannot use an index to create the index" \
102	"try to generate an index from a binary with an index"
103}
104