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