binutils-gdb/gdb/testsuite/gdb.base/index-cache.exp

194 lines
5.6 KiB
Plaintext
Raw Normal View History

Add DWARF index cache New in v3: - Remove things related to the dwarf-5 format. - Fix compilation on mingw (scoped_mmap.c). GDB can generate indexes for DWARF debug information, which, when integrated in the original binary, can speed up loading object files. This can be done using the gdb-add-index script or directly by the linker itself. However, not many people know about this. And even among those who do, because it requires additional steps, I don't know a lot of people who actually go through that trouble. To help make using the DWARF index more transparent, this patch introduces a DWARF index cache. When enabled, loading an index-less binary in GDB will automatically save an index file in ~/.cache/gdb. When loading that same object file again, the index file will be looked up and used to load the DWARF index. You therefore get the benefit of the DWARF index without having to do additional manual steps or modifying your build system. When an index section is already present in the file, GDB will prefer that one over looking up the cache. When doing my edit-compile-debug cycle, I often debug multiple times the same build, so the cache helps reducing the load time of the debug sessions after the first one. - The saved index file is exactly the same as the output of the "save gdb-index" command. It is therefore the exact same content that would be found in the .gdb_index or .debug_names section. We just leave it as a standalone file instead of merging it in the binary. - The cache is just a directory with files named after the object file's build-id. It is not possible to save/load the index for an object file without build-id in the cache. - The cache uses the gdb index format. The problem with the dwarf-5 format is that we can generate an addendum to the .debug_str section that you're supposed to integrate to the original binary. This complicates a little bit loading the data from the cached index files, so I would leave this for later. - The size taken up by ~/.cache/gdb is not limited. I was thinking we could add configurable limit (like ccache does), but that would come after. Also, maybe a command to flush the cache. - The cache is disabled by default. I think once it's been out there and tested for a while, it could be turned on by default, so that everybody can enjoy it. - The code was made to follow the XDG specification: if the XDG_CACHE_HOME environment variable, it is used, otherwise it falls back to ~/.cache/gdb. It is possible to change it using "set index-cache directory". On other OSes than GNU/Linux, ~/.cache may not be the best place to put such data. On macOS it should probably default to ~/Library/Caches/... On Windows, %LocalAppData%/... I don't intend to do this part, but further patches are welcome. - I think that we need to be careful that multiple instances of GDB don't interfere with each other (not far fetched at all if you run GDB in some automated script) and the cache is always coherent (either the file is not found, or it is found and entirely valid). Writing the file directly to its final location seems like a recipe for failure. One GDB could read a file in the index while it is being written by another GDB. To mitigate this, I made write_psymtabs_to_index write to temporary files and rename them once it's done. Two GDB instances writing the index for the same file should not step on each other's toes (the last file to be renamed will stay). A GDB looking up a file will only see a complete file or no file. Also, if GDB crashes while generating the index file, it will leave a work-in-progress file, but it won't be picked up by other instances looking up in the cache. gdb/ChangeLog: * common/pathstuff.h (get_standard_cache_dir): New. * common/pathstuff.c (get_standard_cache_dir): New. * build-id.h (build_id_to_string): New. * dwarf-index-common.h (INDEX4_SUFFIX, INDEX5_SUFFIX, DEBUG_STR_SUFFIX): Move to here. * dwarf-index-write.c (INDEX4_SUFFIX, INDEX5_SUFFIX, DEBUG_STR_SUFFIX): Move from there. (write_psymtabs_to_index): Make non-static, add basename parameter. Write to temporary files, rename when done. (save_gdb_index_command): Adjust call to write_psymtabs_to_index. * dwarf2read.h (dwarf2_per_objfile) <index_cache_res>: New field. * dwarf2read.c (dwz_file) <index_cache_res>: New field. (get_gdb_index_contents_from_cache): New. (get_gdb_index_contents_from_cache_dwz): New. (dwarf2_initialize_objfile): Read index from cache. (dwarf2_build_psymtabs): Save to index. * dwarf-index-cache.h: New file. * dwarf-index-cache.c: New file. * dwarf-index-write.h: New file. gdb/testsuite/ChangeLog: * boards/index-cache-gdb.exp: New file. * gdb.dwarf2/index-cache.exp: New file. * gdb.dwarf2/index-cache.c: New file. * gdb.base/maint.exp: Check if we are using the index cache.
2018-08-08 00:14:20 +02:00
# Copyright 2018 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