1 # Copyright 2018 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 { [prepare_for_testing "failed to prepare" $testfile $srcfile] } {
25 # List the files in DIR on the host (where GDB-under-test runs).
26 # Return a list of two elements:
27 # - 0 on success, -1 on failure
28 # - the list of files on success, empty on failure
30 proc ls_host { dir } {
31 lassign [remote_exec host ls "-1 $dir"] ret output
34 fail "failed to list files on host in $dir"
38 # ls -1 returns a list separated by \r\n. split will return a bunch of
39 # empty entries (it treats a sequence of split characters as separate
40 # fields, plus there is a \r\n at the end of the result). Ignore empty
43 set files [split $output \r\n]
47 lappend filtered $file
54 # Execute "show index-cache stats" and verify the output against expected
57 proc check_cache_stats { expected_hits expected_misses } {
59 " Cache hits .this session.: $expected_hits" \
60 "Cache misses .this session.: $expected_misses" \
63 gdb_test "show index-cache stats" $re "check index-cache stats"
66 # Run CODE using a fresh GDB configured based on the other parameters.
68 proc run_test_with_flags { cache_dir cache_enabled code } {
69 global GDBFLAGS testfile
71 save_vars { GDBFLAGS } {
72 set GDBFLAGS "$GDBFLAGS -iex \"set index-cache directory $cache_dir\""
73 set GDBFLAGS "$GDBFLAGS -iex \"set index-cache $cache_enabled\""
75 clean_restart ${testfile}
81 # Test administrative stuff.
83 proc_with_prefix test_basic_stuff { } {
86 clean_restart ${testfile}
88 # Check that the index cache is disabled by default.
91 " is currently disabled." \
92 "index-cache is disabled by default"
94 # Test that we can enable it and "show index-cache" reflects that.
95 gdb_test_no_output "set index-cache on" "enable index cache"
98 " is currently enabled." \
99 "index-cache is now enabled"
101 # Test the "set/show index-cache directory" commands.
102 gdb_test "set index-cache directory" "Argument required.*" "set index-cache directory without arg"
103 gdb_test_no_output "set index-cache directory /tmp" "change the index cache directory"
105 "show index-cache directory" \
106 "The directory of the index cache is \"/tmp\"." \
107 "show index cache directory"
110 # Test loading a binary with the cache disabled. No file should be created.
112 proc_with_prefix test_cache_disabled { cache_dir } {
113 lassign [ls_host $cache_dir] ret files_before
115 run_test_with_flags $cache_dir off {
116 lassign [ls_host $cache_dir] ret files_after
118 set nfiles_created [expr [llength $files_after] - [llength $files_before]]
119 gdb_assert "$nfiles_created == 0" "no files were created"
121 check_cache_stats 0 0
125 # Test with the cache enabled, we expect to have exactly one file created.
127 proc_with_prefix test_cache_enabled_miss { cache_dir } {
130 lassign [ls_host $cache_dir] ret files_before
132 run_test_with_flags $cache_dir on {
134 lassign [ls_host $cache_dir] ret files_after
135 set nfiles_created [expr [llength $files_after] - [llength $files_before]]
136 gdb_assert "$nfiles_created > 0" "at least one file was created"
138 set build_id [get_build_id [standard_output_file ${testfile}]]
139 if { $build_id == "" } {
140 fail "couldn't get executable build id"
144 set expected_created_file [list "${build_id}.gdb-index"]
145 set found_idx [lsearch -exact $files_after $expected_created_file]
146 gdb_assert "$found_idx >= 0" "expected file is there"
148 remote_exec host rm "-f $cache_dir/$expected_created_file"
150 check_cache_stats 0 1
155 # Test with the cache enabled, this time we should have one file (the
156 # same), but one cache read hit.
158 proc_with_prefix test_cache_enabled_hit { cache_dir } {
159 # Just to populate the cache.
160 run_test_with_flags $cache_dir on {}
162 lassign [ls_host $cache_dir] ret files_before
164 run_test_with_flags $cache_dir on {
165 lassign [ls_host $cache_dir] ret files_after
166 set nfiles_created [expr [llength $files_after] - [llength $files_before]]
167 gdb_assert "$nfiles_created == 0" "no files were created"
169 check_cache_stats 1 0
175 # The cache dir should be on the host (possibly remote), so we can't use the
176 # standard output directory for that (it's on the build machine).
177 lassign [remote_exec host mktemp -d] ret cache_dir
180 fail "couldn't create temporary cache dir"
184 # The ouput of mktemp contains an end of line, remove it.
185 set cache_dir [string trimright $cache_dir \r\n]
187 test_cache_disabled $cache_dir
188 test_cache_enabled_miss $cache_dir
189 test_cache_enabled_hit $cache_dir
191 # Test again with the cache disabled, now that it is populated.
192 test_cache_disabled $cache_dir