1 # Copyright 2018-2022 Free Software Foundation, Inc.
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.
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.
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/>.
16 # This test checks that the index-cache feature generates the expected files at
17 # the expected location.
21 if { [build_executable "failed to prepare" $testfile $srcfile \
22 {debug additional_flags=-Wl,--build-id}] } {
26 # The index cache won't be used in certain circumstances, for which we must
27 # account in this test:
29 # - the binary already has an index section
30 # - we use the -readnow switch
31 set has_index_section [exec_has_index_section $binfile]
32 set uses_readnow [expr [string first "-readnow" $GDBFLAGS] != -1]
33 set expecting_index_cache_use [expr !$has_index_section && !$uses_readnow]
35 # List the files in DIR on the host (where GDB-under-test runs).
36 # Return a list of two elements:
37 # - 0 on success, -1 on failure
38 # - the list of files on success, empty on failure
40 proc ls_host { dir } {
41 lassign [remote_exec host ls "-1 $dir"] ret output
44 fail "failed to list files on host in $dir"
48 # ls -1 returns a list separated by \r\n. split will return a bunch of
49 # empty entries (it treats a sequence of split characters as separate
50 # fields, plus there is a \r\n at the end of the result). Ignore empty
53 set files [split $output \r\n]
57 lappend filtered $file
61 return [list 0 $filtered]
64 # Execute "show index-cache stats" and verify the output against expected
67 proc check_cache_stats { expected_hits expected_misses } {
69 " Cache hits .this session.: $expected_hits" \
70 "Cache misses .this session.: $expected_misses" \
73 gdb_test "show index-cache stats" $re "check index-cache stats"
76 # Run CODE using a fresh GDB configured based on the other parameters.
78 proc run_test_with_flags { cache_dir cache_enabled code } {
79 global GDBFLAGS testfile
81 save_vars { GDBFLAGS } {
82 set GDBFLAGS "$GDBFLAGS -iex \"set index-cache directory $cache_dir\""
83 set GDBFLAGS "$GDBFLAGS -iex \"set index-cache enabled $cache_enabled\""
85 clean_restart ${testfile}
91 # Test administrative stuff.
93 proc_with_prefix test_basic_stuff { } {
96 clean_restart ${testfile}
98 # Check that the index cache is disabled by default.
100 "show index-cache enabled" \
101 "The index cache is off." \
102 "index-cache is disabled by default"
104 # Test that we can enable it and "show index-cache enabled" reflects that.
105 gdb_test_no_output "set index-cache enabled on" "enable index cache"
107 "show index-cache enabled" \
108 "The index cache is on." \
109 "index-cache is now enabled"
111 with_test_prefix "deprecated commands" {
112 gdb_test "set index-cache off" ".*is deprecated.*" "disable index cache"
114 "show index-cache enabled" \
115 "The index cache is off." \
116 "index-cache is now disabled"
117 gdb_test "set index-cache on" ".*is deprecated.*" "enable index cache"
119 "show index-cache enabled" \
120 "The index cache is on." \
121 "index-cache is now enabled"
124 # Test the "set/show index-cache directory" commands.
125 gdb_test "set index-cache directory" "Argument required.*" "set index-cache directory without arg"
126 gdb_test_no_output "set index-cache directory /tmp" "change the index cache directory"
128 "show index-cache directory" \
129 "The directory of the index cache is \"/tmp\"." \
130 "show index cache directory"
133 # Test loading a binary with the cache disabled. No file should be created.
135 proc_with_prefix test_cache_disabled { cache_dir test_prefix } {
136 with_test_prefix $test_prefix {
137 lassign [ls_host $cache_dir] ret files_before
139 run_test_with_flags $cache_dir off {
140 lassign [ls_host $cache_dir] ret files_after
142 set nfiles_created [expr [llength $files_after] - [llength $files_before]]
143 gdb_assert "$nfiles_created == 0" "no files were created"
145 check_cache_stats 0 0
150 # Test a cache miss. We expect to have at least one file in the cache if the
151 # index cache is going to be used (see expecting_index_cache_use) and a cache
152 # miss in the stats. If the cache is not going to be used, we expect to have
153 # no files and no cache hits nor misses.
155 proc_with_prefix test_cache_enabled_miss { cache_dir } {
156 global testfile expecting_index_cache_use
158 lassign [ls_host $cache_dir] ret files_before
160 run_test_with_flags $cache_dir on {
162 lassign [ls_host $cache_dir] ret files_after
163 set nfiles_created [expr [llength $files_after] - [llength $files_before]]
164 if { $expecting_index_cache_use } {
165 gdb_assert "$nfiles_created > 0" "at least one file was created"
167 gdb_assert "$nfiles_created == 0" "no file was created"
170 set build_id [get_build_id [standard_output_file ${testfile}]]
171 if { $build_id == "" } {
172 fail "couldn't get executable build id"
176 set expected_created_file [list "${build_id}.gdb-index"]
177 set found_idx [lsearch -exact $files_after $expected_created_file]
178 if { $expecting_index_cache_use } {
179 gdb_assert "$found_idx >= 0" "expected file is there"
181 gdb_assert "$found_idx == -1" "no index cache file generated"
184 remote_exec host rm "-f $cache_dir/$expected_created_file"
186 if { $expecting_index_cache_use } {
187 check_cache_stats 0 1
189 check_cache_stats 0 0
195 # Test a cache hit. We should have at least one file in the cache if the index
196 # cache is going to be used (see expecting_index_cache_use) and a cache hit in
197 # the stats. If the cache is not going to be used, we expect to have no files
198 # and no cache hits nor misses.
200 proc_with_prefix test_cache_enabled_hit { cache_dir } {
201 global expecting_index_cache_use
203 # Just to populate the cache.
204 run_test_with_flags $cache_dir on {}
206 lassign [ls_host $cache_dir] ret files_before
208 run_test_with_flags $cache_dir on {
209 lassign [ls_host $cache_dir] ret files_after
210 set nfiles_created [expr [llength $files_after] - [llength $files_before]]
211 gdb_assert "$nfiles_created == 0" "no files were created"
213 if { $expecting_index_cache_use } {
214 check_cache_stats 1 0
216 check_cache_stats 0 0
223 # The cache dir should be on the host (possibly remote), so we can't use the
224 # standard output directory for that (it's on the build machine).
225 lassign [remote_exec host mktemp -d] ret cache_dir
228 fail "couldn't create temporary cache dir"
232 # The ouput of mktemp contains an end of line, remove it.
233 set cache_dir [string trimright $cache_dir \r\n]
235 test_cache_disabled $cache_dir "before populate"
236 test_cache_enabled_miss $cache_dir
237 test_cache_enabled_hit $cache_dir
239 # Test again with the cache disabled, now that it is populated.
240 test_cache_disabled $cache_dir "after populate"
242 lassign [remote_exec host sh "-c \"rm $cache_dir/*.gdb-index\""] ret
243 if { $ret != 0 && $expecting_index_cache_use } {
244 fail "couldn't remove files in temporary cache dir"
248 lassign [remote_exec host rmdir "$cache_dir"] ret
250 fail "couldn't remove temporary cache dir"