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