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