aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.base/index-cache.exp
blob: 4e583abf013fbad552757205951e816abeadf3ed (plain)
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
#   Copyright 2018-2019 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 { [prepare_for_testing "failed to prepare" $testfile $srcfile] } {
    return
}

# 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 "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 $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" \
	" is currently disabled." \
	"index-cache is disabled by default"

    # Test that we can enable it and "show index-cache" reflects that.
    gdb_test_no_output "set index-cache on" "enable index cache"
    gdb_test \
	"show index-cache" \
	" is currently enabled." \
	"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 } {
    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 with the cache enabled, we expect to have exactly one file created.

proc_with_prefix test_cache_enabled_miss { cache_dir } {
    global testfile

    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" "at least one 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]
	gdb_assert "$found_idx >= 0" "expected file is there"

	remote_exec host rm "-f $cache_dir/$expected_created_file"

	check_cache_stats 0 1
    }
}


# Test with the cache enabled, this time we should have one file (the
# same), but one cache read hit.

proc_with_prefix test_cache_enabled_hit { cache_dir } {
    # 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"

	check_cache_stats 1 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
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