index-cache.exp revision 1.1.1.4 1 1.1.1.4 christos # Copyright 2018-2024 Free Software Foundation, Inc.
2 1.1 christos
3 1.1 christos # This program is free software; you can redistribute it and/or modify
4 1.1 christos # it under the terms of the GNU General Public License as published by
5 1.1 christos # the Free Software Foundation; either version 3 of the License, or
6 1.1 christos # (at your option) any later version.
7 1.1 christos #
8 1.1 christos # This program is distributed in the hope that it will be useful,
9 1.1 christos # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 1.1 christos # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 1.1 christos # GNU General Public License for more details.
12 1.1 christos #
13 1.1 christos # You should have received a copy of the GNU General Public License
14 1.1 christos # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 1.1 christos
16 1.1 christos # This test checks that the index-cache feature generates the expected files at
17 1.1 christos # the expected location.
18 1.1 christos
19 1.1.1.4 christos standard_testfile .c -2.c
20 1.1 christos
21 1.1.1.4 christos if { [build_executable "failed to prepare" $testfile [list $srcfile $srcfile2] \
22 1.1.1.3 christos {debug ldflags=-Wl,--build-id}] } {
23 1.1 christos return
24 1.1 christos }
25 1.1 christos
26 1.1.1.2 christos # The index cache won't be used in certain circumstances, for which we must
27 1.1.1.2 christos # account in this test:
28 1.1.1.2 christos #
29 1.1.1.2 christos # - the binary already has an index section
30 1.1.1.2 christos # - we use the -readnow switch
31 1.1.1.2 christos set has_index_section [exec_has_index_section $binfile]
32 1.1.1.2 christos set uses_readnow [expr [string first "-readnow" $GDBFLAGS] != -1]
33 1.1.1.2 christos set expecting_index_cache_use [expr !$has_index_section && !$uses_readnow]
34 1.1.1.2 christos
35 1.1 christos # List the files in DIR on the host (where GDB-under-test runs).
36 1.1 christos # Return a list of two elements:
37 1.1 christos # - 0 on success, -1 on failure
38 1.1 christos # - the list of files on success, empty on failure
39 1.1 christos
40 1.1 christos proc ls_host { dir } {
41 1.1 christos lassign [remote_exec host ls "-1 $dir"] ret output
42 1.1 christos
43 1.1 christos if { $ret != 0 } {
44 1.1 christos fail "failed to list files on host in $dir"
45 1.1 christos return -1
46 1.1 christos }
47 1.1 christos
48 1.1 christos # ls -1 returns a list separated by \r\n. split will return a bunch of
49 1.1 christos # empty entries (it treats a sequence of split characters as separate
50 1.1 christos # fields, plus there is a \r\n at the end of the result). Ignore empty
51 1.1 christos # list elements.
52 1.1 christos set filtered {}
53 1.1 christos set files [split $output \r\n]
54 1.1 christos
55 1.1 christos foreach file $files {
56 1.1 christos if { $file != "" } {
57 1.1 christos lappend filtered $file
58 1.1 christos }
59 1.1 christos }
60 1.1 christos
61 1.1.1.2 christos return [list 0 $filtered]
62 1.1 christos }
63 1.1 christos
64 1.1 christos # Execute "show index-cache stats" and verify the output against expected
65 1.1 christos # values.
66 1.1 christos
67 1.1 christos proc check_cache_stats { expected_hits expected_misses } {
68 1.1.1.4 christos # This test wants to check the cache, so make sure it has completed
69 1.1.1.4 christos # its work.
70 1.1.1.4 christos gdb_test_no_output "maintenance wait-for-index-cache"
71 1.1.1.4 christos
72 1.1 christos set re [multi_line \
73 1.1 christos " Cache hits .this session.: $expected_hits" \
74 1.1 christos "Cache misses .this session.: $expected_misses" \
75 1.1 christos ]
76 1.1 christos
77 1.1 christos gdb_test "show index-cache stats" $re "check index-cache stats"
78 1.1 christos }
79 1.1 christos
80 1.1 christos # Run CODE using a fresh GDB configured based on the other parameters.
81 1.1 christos
82 1.1 christos proc run_test_with_flags { cache_dir cache_enabled code } {
83 1.1 christos global GDBFLAGS testfile
84 1.1 christos
85 1.1 christos save_vars { GDBFLAGS } {
86 1.1 christos set GDBFLAGS "$GDBFLAGS -iex \"set index-cache directory $cache_dir\""
87 1.1.1.3 christos set GDBFLAGS "$GDBFLAGS -iex \"set index-cache enabled $cache_enabled\""
88 1.1 christos
89 1.1 christos clean_restart ${testfile}
90 1.1 christos
91 1.1 christos uplevel 1 $code
92 1.1 christos }
93 1.1 christos }
94 1.1 christos
95 1.1 christos # Test administrative stuff.
96 1.1 christos
97 1.1 christos proc_with_prefix test_basic_stuff { } {
98 1.1 christos global testfile
99 1.1 christos
100 1.1 christos clean_restart ${testfile}
101 1.1 christos
102 1.1 christos # Check that the index cache is disabled by default.
103 1.1 christos gdb_test \
104 1.1.1.3 christos "show index-cache enabled" \
105 1.1.1.3 christos "The index cache is off." \
106 1.1 christos "index-cache is disabled by default"
107 1.1 christos
108 1.1.1.3 christos # Test that we can enable it and "show index-cache enabled" reflects that.
109 1.1.1.3 christos gdb_test_no_output "set index-cache enabled on" "enable index cache"
110 1.1 christos gdb_test \
111 1.1.1.3 christos "show index-cache enabled" \
112 1.1.1.3 christos "The index cache is on." \
113 1.1 christos "index-cache is now enabled"
114 1.1 christos
115 1.1.1.3 christos with_test_prefix "deprecated commands" {
116 1.1.1.3 christos gdb_test "set index-cache off" ".*is deprecated.*" "disable index cache"
117 1.1.1.3 christos gdb_test \
118 1.1.1.3 christos "show index-cache enabled" \
119 1.1.1.3 christos "The index cache is off." \
120 1.1.1.3 christos "index-cache is now disabled"
121 1.1.1.3 christos gdb_test "set index-cache on" ".*is deprecated.*" "enable index cache"
122 1.1.1.3 christos gdb_test \
123 1.1.1.3 christos "show index-cache enabled" \
124 1.1.1.3 christos "The index cache is on." \
125 1.1.1.3 christos "index-cache is now enabled"
126 1.1.1.3 christos }
127 1.1.1.3 christos
128 1.1 christos # Test the "set/show index-cache directory" commands.
129 1.1 christos gdb_test "set index-cache directory" "Argument required.*" "set index-cache directory without arg"
130 1.1 christos gdb_test_no_output "set index-cache directory /tmp" "change the index cache directory"
131 1.1 christos gdb_test \
132 1.1 christos "show index-cache directory" \
133 1.1 christos "The directory of the index cache is \"/tmp\"." \
134 1.1 christos "show index cache directory"
135 1.1 christos }
136 1.1 christos
137 1.1 christos # Test loading a binary with the cache disabled. No file should be created.
138 1.1 christos
139 1.1.1.2 christos proc_with_prefix test_cache_disabled { cache_dir test_prefix } {
140 1.1.1.2 christos with_test_prefix $test_prefix {
141 1.1.1.2 christos lassign [ls_host $cache_dir] ret files_before
142 1.1 christos
143 1.1.1.2 christos run_test_with_flags $cache_dir off {
144 1.1.1.2 christos lassign [ls_host $cache_dir] ret files_after
145 1.1 christos
146 1.1.1.2 christos set nfiles_created [expr [llength $files_after] - [llength $files_before]]
147 1.1.1.2 christos gdb_assert "$nfiles_created == 0" "no files were created"
148 1.1 christos
149 1.1.1.4 christos # Trigger expansion of symtab containing main, if not already done.
150 1.1.1.4 christos gdb_test "ptype main" "^type = int \\(void\\)"
151 1.1.1.4 christos
152 1.1.1.4 christos # Trigger expansion of symtab not containing main.
153 1.1.1.4 christos gdb_test "ptype foo" "^type = int \\(void\\)"
154 1.1.1.4 christos
155 1.1.1.4 christos # Look for non-existent function.
156 1.1.1.4 christos gdb_test "ptype foobar" "^No symbol \"foobar\" in current context\\."
157 1.1.1.4 christos
158 1.1.1.2 christos check_cache_stats 0 0
159 1.1.1.2 christos }
160 1.1 christos }
161 1.1 christos }
162 1.1 christos
163 1.1.1.2 christos # Test a cache miss. We expect to have at least one file in the cache if the
164 1.1.1.2 christos # index cache is going to be used (see expecting_index_cache_use) and a cache
165 1.1.1.2 christos # miss in the stats. If the cache is not going to be used, we expect to have
166 1.1.1.2 christos # no files and no cache hits nor misses.
167 1.1 christos
168 1.1 christos proc_with_prefix test_cache_enabled_miss { cache_dir } {
169 1.1.1.2 christos global testfile expecting_index_cache_use
170 1.1 christos
171 1.1 christos lassign [ls_host $cache_dir] ret files_before
172 1.1 christos
173 1.1 christos run_test_with_flags $cache_dir on {
174 1.1 christos
175 1.1 christos lassign [ls_host $cache_dir] ret files_after
176 1.1 christos set nfiles_created [expr [llength $files_after] - [llength $files_before]]
177 1.1.1.2 christos if { $expecting_index_cache_use } {
178 1.1.1.2 christos gdb_assert "$nfiles_created > 0" "at least one file was created"
179 1.1.1.2 christos } else {
180 1.1.1.2 christos gdb_assert "$nfiles_created == 0" "no file was created"
181 1.1.1.2 christos }
182 1.1 christos
183 1.1 christos set build_id [get_build_id [standard_output_file ${testfile}]]
184 1.1 christos if { $build_id == "" } {
185 1.1 christos fail "couldn't get executable build id"
186 1.1 christos return
187 1.1 christos }
188 1.1 christos
189 1.1 christos set expected_created_file [list "${build_id}.gdb-index"]
190 1.1 christos set found_idx [lsearch -exact $files_after $expected_created_file]
191 1.1.1.2 christos if { $expecting_index_cache_use } {
192 1.1.1.2 christos gdb_assert "$found_idx >= 0" "expected file is there"
193 1.1.1.2 christos } else {
194 1.1.1.2 christos gdb_assert "$found_idx == -1" "no index cache file generated"
195 1.1.1.2 christos }
196 1.1 christos
197 1.1 christos remote_exec host rm "-f $cache_dir/$expected_created_file"
198 1.1 christos
199 1.1.1.4 christos # Trigger expansion of symtab containing main, if not already done.
200 1.1.1.4 christos gdb_test "ptype main" "^type = int \\(void\\)"
201 1.1.1.4 christos
202 1.1.1.4 christos # Trigger expansion of symtab not containing main.
203 1.1.1.4 christos gdb_test "ptype foo" "^type = int \\(void\\)"
204 1.1.1.4 christos
205 1.1.1.4 christos # Look for non-existent function.
206 1.1.1.4 christos gdb_test "ptype foobar" "^No symbol \"foobar\" in current context\\."
207 1.1.1.4 christos
208 1.1.1.2 christos if { $expecting_index_cache_use } {
209 1.1.1.2 christos check_cache_stats 0 1
210 1.1.1.2 christos } else {
211 1.1.1.2 christos check_cache_stats 0 0
212 1.1.1.2 christos }
213 1.1 christos }
214 1.1 christos }
215 1.1 christos
216 1.1 christos
217 1.1.1.2 christos # Test a cache hit. We should have at least one file in the cache if the index
218 1.1.1.2 christos # cache is going to be used (see expecting_index_cache_use) and a cache hit in
219 1.1.1.2 christos # the stats. If the cache is not going to be used, we expect to have no files
220 1.1.1.2 christos # and no cache hits nor misses.
221 1.1 christos
222 1.1 christos proc_with_prefix test_cache_enabled_hit { cache_dir } {
223 1.1.1.2 christos global expecting_index_cache_use
224 1.1.1.2 christos
225 1.1 christos # Just to populate the cache.
226 1.1.1.4 christos with_test_prefix "populate cache" {
227 1.1.1.4 christos run_test_with_flags $cache_dir on {}
228 1.1.1.4 christos }
229 1.1 christos
230 1.1 christos lassign [ls_host $cache_dir] ret files_before
231 1.1 christos
232 1.1 christos run_test_with_flags $cache_dir on {
233 1.1 christos lassign [ls_host $cache_dir] ret files_after
234 1.1 christos set nfiles_created [expr [llength $files_after] - [llength $files_before]]
235 1.1 christos gdb_assert "$nfiles_created == 0" "no files were created"
236 1.1 christos
237 1.1.1.4 christos # Trigger expansion of symtab containing main, if not already done.
238 1.1.1.4 christos gdb_test "ptype main" "^type = int \\(void\\)"
239 1.1.1.4 christos
240 1.1.1.4 christos # Trigger expansion of symtab not containing main.
241 1.1.1.4 christos gdb_test "ptype foo" "^type = int \\(void\\)"
242 1.1.1.4 christos
243 1.1.1.4 christos # Look for non-existent function.
244 1.1.1.4 christos gdb_test "ptype foobar" "^No symbol \"foobar\" in current context\\."
245 1.1.1.4 christos
246 1.1.1.2 christos if { $expecting_index_cache_use } {
247 1.1.1.2 christos check_cache_stats 1 0
248 1.1.1.2 christos } else {
249 1.1.1.2 christos check_cache_stats 0 0
250 1.1.1.2 christos }
251 1.1 christos }
252 1.1 christos }
253 1.1 christos
254 1.1 christos test_basic_stuff
255 1.1 christos
256 1.1 christos # The cache dir should be on the host (possibly remote), so we can't use the
257 1.1 christos # standard output directory for that (it's on the build machine).
258 1.1 christos lassign [remote_exec host mktemp -d] ret cache_dir
259 1.1 christos
260 1.1 christos if { $ret != 0 } {
261 1.1 christos fail "couldn't create temporary cache dir"
262 1.1 christos return
263 1.1 christos }
264 1.1 christos
265 1.1 christos # The ouput of mktemp contains an end of line, remove it.
266 1.1 christos set cache_dir [string trimright $cache_dir \r\n]
267 1.1 christos
268 1.1.1.2 christos test_cache_disabled $cache_dir "before populate"
269 1.1 christos test_cache_enabled_miss $cache_dir
270 1.1 christos test_cache_enabled_hit $cache_dir
271 1.1 christos
272 1.1 christos # Test again with the cache disabled, now that it is populated.
273 1.1.1.2 christos test_cache_disabled $cache_dir "after populate"
274 1.1 christos
275 1.1.1.4 christos lassign [remote_exec host "sh -c" [quote_for_host rm $cache_dir/*.gdb-index]] ret
276 1.1.1.3 christos if { $ret != 0 && $expecting_index_cache_use } {
277 1.1.1.3 christos fail "couldn't remove files in temporary cache dir"
278 1.1.1.3 christos return
279 1.1.1.3 christos }
280 1.1.1.3 christos
281 1.1.1.3 christos lassign [remote_exec host rmdir "$cache_dir"] ret
282 1.1.1.3 christos if { $ret != 0 } {
283 1.1.1.3 christos fail "couldn't remove temporary cache dir"
284 1.1.1.3 christos return
285 1.1.1.3 christos }
286