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