]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.base/index-cache.exp
Make gdb.base/index-cache.exp work with readnow board (PR 24669)
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / index-cache.exp
1 # Copyright 2018-2019 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 # This test checks that the index-cache feature generates the expected files at
17 # the expected location.
18
19 standard_testfile
20
21 if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } {
22 return
23 }
24
25 # The index cache won't be used in certain circumstances, for which we must
26 # account in this test:
27 #
28 # - the binary already has an index section
29 # - we use the -readnow switch
30 set has_index_section [exec_has_index_section $binfile]
31 set uses_readnow [expr [string first "-readnow" $GDBFLAGS] != -1]
32 set expecting_index_cache_use [expr !$has_index_section && !$uses_readnow]
33
34 # List the files in DIR on the host (where GDB-under-test runs).
35 # Return a list of two elements:
36 # - 0 on success, -1 on failure
37 # - the list of files on success, empty on failure
38
39 proc ls_host { dir } {
40 lassign [remote_exec host ls "-1 $dir"] ret output
41
42 if { $ret != 0 } {
43 fail "failed to list files on host in $dir"
44 return -1
45 }
46
47 # ls -1 returns a list separated by \r\n. split will return a bunch of
48 # empty entries (it treats a sequence of split characters as separate
49 # fields, plus there is a \r\n at the end of the result). Ignore empty
50 # list elements.
51 set filtered {}
52 set files [split $output \r\n]
53
54 foreach file $files {
55 if { $file != "" } {
56 lappend filtered $file
57 }
58 }
59
60 return [list 0 $filtered]
61 }
62
63 # Execute "show index-cache stats" and verify the output against expected
64 # values.
65
66 proc check_cache_stats { expected_hits expected_misses } {
67 set re [multi_line \
68 " Cache hits .this session.: $expected_hits" \
69 "Cache misses .this session.: $expected_misses" \
70 ]
71
72 gdb_test "show index-cache stats" $re "check index-cache stats"
73 }
74
75 # Run CODE using a fresh GDB configured based on the other parameters.
76
77 proc run_test_with_flags { cache_dir cache_enabled code } {
78 global GDBFLAGS testfile
79
80 save_vars { GDBFLAGS } {
81 set GDBFLAGS "$GDBFLAGS -iex \"set index-cache directory $cache_dir\""
82 set GDBFLAGS "$GDBFLAGS -iex \"set index-cache $cache_enabled\""
83
84 clean_restart ${testfile}
85
86 uplevel 1 $code
87 }
88 }
89
90 # Test administrative stuff.
91
92 proc_with_prefix test_basic_stuff { } {
93 global testfile
94
95 clean_restart ${testfile}
96
97 # Check that the index cache is disabled by default.
98 gdb_test \
99 "show index-cache" \
100 " is currently disabled." \
101 "index-cache is disabled by default"
102
103 # Test that we can enable it and "show index-cache" reflects that.
104 gdb_test_no_output "set index-cache on" "enable index cache"
105 gdb_test \
106 "show index-cache" \
107 " is currently enabled." \
108 "index-cache is now enabled"
109
110 # Test the "set/show index-cache directory" commands.
111 gdb_test "set index-cache directory" "Argument required.*" "set index-cache directory without arg"
112 gdb_test_no_output "set index-cache directory /tmp" "change the index cache directory"
113 gdb_test \
114 "show index-cache directory" \
115 "The directory of the index cache is \"/tmp\"." \
116 "show index cache directory"
117 }
118
119 # Test loading a binary with the cache disabled. No file should be created.
120
121 proc_with_prefix test_cache_disabled { cache_dir } {
122 lassign [ls_host $cache_dir] ret files_before
123
124 run_test_with_flags $cache_dir off {
125 lassign [ls_host $cache_dir] ret files_after
126
127 set nfiles_created [expr [llength $files_after] - [llength $files_before]]
128 gdb_assert "$nfiles_created == 0" "no files were created"
129
130 check_cache_stats 0 0
131 }
132 }
133
134 # Test a cache miss. We expect to have at least one file in the cache if the
135 # index cache is going to be used (see expecting_index_cache_use) and a cache
136 # miss in the stats. If the cache is not going to be used, we expect to have
137 # no files and no cache hits nor misses.
138
139 proc_with_prefix test_cache_enabled_miss { cache_dir } {
140 global testfile expecting_index_cache_use
141
142 lassign [ls_host $cache_dir] ret files_before
143
144 run_test_with_flags $cache_dir on {
145
146 lassign [ls_host $cache_dir] ret files_after
147 set nfiles_created [expr [llength $files_after] - [llength $files_before]]
148 if { $expecting_index_cache_use } {
149 gdb_assert "$nfiles_created > 0" "at least one file was created"
150 } else {
151 gdb_assert "$nfiles_created == 0" "no file was created"
152 }
153
154 set build_id [get_build_id [standard_output_file ${testfile}]]
155 if { $build_id == "" } {
156 fail "couldn't get executable build id"
157 return
158 }
159
160 set expected_created_file [list "${build_id}.gdb-index"]
161 set found_idx [lsearch -exact $files_after $expected_created_file]
162 if { $expecting_index_cache_use } {
163 gdb_assert "$found_idx >= 0" "expected file is there"
164 } else {
165 gdb_assert "$found_idx == -1" "no index cache file generated"
166 }
167
168 remote_exec host rm "-f $cache_dir/$expected_created_file"
169
170 if { $expecting_index_cache_use } {
171 check_cache_stats 0 1
172 } else {
173 check_cache_stats 0 0
174 }
175 }
176 }
177
178
179 # Test a cache hit. We should have at least one file in the cache if the index
180 # cache is going to be used (see expecting_index_cache_use) and a cache hit in
181 # the stats. If the cache is not going to be used, we expect to have no files
182 # and no cache hits nor misses.
183
184 proc_with_prefix test_cache_enabled_hit { cache_dir } {
185 global expecting_index_cache_use
186
187 # Just to populate the cache.
188 run_test_with_flags $cache_dir on {}
189
190 lassign [ls_host $cache_dir] ret files_before
191
192 run_test_with_flags $cache_dir on {
193 lassign [ls_host $cache_dir] ret files_after
194 set nfiles_created [expr [llength $files_after] - [llength $files_before]]
195 gdb_assert "$nfiles_created == 0" "no files were created"
196
197 if { $expecting_index_cache_use } {
198 check_cache_stats 1 0
199 } else {
200 check_cache_stats 0 0
201 }
202 }
203 }
204
205 test_basic_stuff
206
207 # The cache dir should be on the host (possibly remote), so we can't use the
208 # standard output directory for that (it's on the build machine).
209 lassign [remote_exec host mktemp -d] ret cache_dir
210
211 if { $ret != 0 } {
212 fail "couldn't create temporary cache dir"
213 return
214 }
215
216 # The ouput of mktemp contains an end of line, remove it.
217 set cache_dir [string trimright $cache_dir \r\n]
218
219 test_cache_disabled $cache_dir
220 test_cache_enabled_miss $cache_dir
221 test_cache_enabled_hit $cache_dir
222
223 # Test again with the cache disabled, now that it is populated.
224 test_cache_disabled $cache_dir
225