mirror of
https://sourceware.org/git/binutils-gdb.git
synced 2025-01-24 12:35:55 +08:00
488b3ff1fc
With gdb build with -fsanitize=thread and test-case gdb.base/index-cache.exp I run into: ... (gdb) file build/gdb/testsuite/outputs/gdb.base/index-cache/index-cache Reading symbols from build/gdb/testsuite/outputs/gdb.base/index-cache/index-cache... ================== WARNING: ThreadSanitizer: data race (pid=12261) Write of size 4 at 0x7b4400097d08 by main thread: #0 bfd_open_file bfd/cache.c:584 (gdb+0x148bb92) #1 bfd_cache_lookup_worker bfd/cache.c:261 (gdb+0x148b12a) #2 cache_bseek bfd/cache.c:289 (gdb+0x148b324) #3 bfd_seek bfd/bfdio.c:459 (gdb+0x1489c31) #4 _bfd_generic_get_section_contents bfd/libbfd.c:1069 (gdb+0x14977a4) #5 bfd_get_section_contents bfd/section.c:1606 (gdb+0x149cc7c) #6 gdb_bfd_scan_elf_dyntag(int, bfd*, unsigned long*, unsigned long*) gdb/solib.c:1601 (gdb+0xed8eca) #7 elf_locate_base gdb/solib-svr4.c:705 (gdb+0xec28ac) #8 svr4_iterate_over_objfiles_in_search_order gdb/solib-svr4.c:3430 (gdb+0xeca55d) #9 gdbarch_iterate_over_objfiles_in_search_order(gdbarch*, gdb::function_view<bool (objfile*)>, objfile*) gdb/gdbarch.c:5041 (gdb+0x537cad) #10 find_main_name gdb/symtab.c:6270 (gdb+0xf743a5) #11 main_language() gdb/symtab.c:6313 (gdb+0xf74499) #12 set_initial_language() gdb/symfile.c:1700 (gdb+0xf4285c) #13 symbol_file_add_main_1 gdb/symfile.c:1212 (gdb+0xf40e2a) #14 symbol_file_command(char const*, int) gdb/symfile.c:1681 (gdb+0xf427d1) #15 file_command gdb/exec.c:554 (gdb+0x94f74b) #16 do_simple_func gdb/cli/cli-decode.c:95 (gdb+0x6d9528) #17 cmd_func(cmd_list_element*, char const*, int) gdb/cli/cli-decode.c:2735 (gdb+0x6e0f69) #18 execute_command(char const*, int) gdb/top.c:575 (gdb+0xff303c) #19 command_handler(char const*) gdb/event-top.c:552 (gdb+0x94adde) #20 command_line_handler(std::unique_ptr<char, gdb::xfree_deleter<char> >&&) gdb/event-top.c:788 (gdb+0x94b49b) #21 tui_command_line_handler gdb/tui/tui-interp.c:104 (gdb+0x103479c) #22 gdb_rl_callback_handler gdb/event-top.c:259 (gdb+0x94a383) #23 rl_callback_read_char readline/readline/callback.c:290 (gdb+0x11bde5d) #24 gdb_rl_callback_read_char_wrapper_noexcept gdb/event-top.c:195 (gdb+0x94a182) #25 gdb_rl_callback_read_char_wrapper gdb/event-top.c:234 (gdb+0x94a243) #26 stdin_event_handler gdb/ui.c:155 (gdb+0x1074a40) #27 handle_file_event gdbsupport/event-loop.cc:573 (gdb+0x1d94f02) #28 gdb_wait_for_event gdbsupport/event-loop.cc:694 (gdb+0x1d9563a) #29 gdb_do_one_event(int) gdbsupport/event-loop.cc:264 (gdb+0x1d93a26) #30 start_event_loop gdb/main.c:412 (gdb+0xb5a374) #31 captured_command_loop gdb/main.c:476 (gdb+0xb5a563) #32 captured_main gdb/main.c:1320 (gdb+0xb5c6e3) #33 gdb_main(captured_main_args*) gdb/main.c:1339 (gdb+0xb5c792) #34 main gdb/gdb.c:32 (gdb+0x416776) Previous read of size 1 at 0x7b4400097d08 by thread T12: #0 bfd_check_format_matches bfd/format.c:323 (gdb+0x1492db4) #1 bfd_check_format bfd/format.c:94 (gdb+0x1492104) #2 build_id_bfd_get(bfd*) gdb/build-id.c:42 (gdb+0x6648f7) #3 index_cache::store(dwarf2_per_bfd*, index_cache_store_context*) gdb/dwarf2/index-cache.c:110 (gdb+0x82d205) #4 cooked_index::maybe_write_index(dwarf2_per_bfd*) gdb/dwarf2/cooked-index.c:640 (gdb+0x7f1bf1) #5 operator() gdb/dwarf2/cooked-index.c:470 (gdb+0x7f0f40) #6 _M_invoke /usr/include/c++/7/bits/std_function.h:316 (gdb+0x7f28f7) #7 std::function<void ()>::operator()() const /usr/include/c++/7/bits/std_function.h:706 (gdb+0x700952) #8 void std::__invoke_impl<void, std::function<void ()>&>(std::__invoke_other, std::function<void ()>&) /usr/include/c++/7/bits/invoke.h:60 (gdb+0x7381a0) #9 std::__invoke_result<std::function<void ()>&>::type std::__invoke<std::function<void ()>&>(std::function<void ()>&) /usr/include/c++/7/bits/invoke.h:95 (gdb+0x737e91) #10 std::__future_base::_Task_state<std::function<void ()>, std::allocator<int>, void ()>::_M_run()::{lambda()#1}::operator()() const /usr/include/c++/7/future:1421 (gdb+0x737b59) #11 std::__future_base::_Task_setter<std::unique_ptr<std::__future_base::_Result<void>, std::__future_base::_Result_base::_Deleter>, std::__future_base::_Task_state<std::function<void ()>, std::allocator<int>, void ()>::_M_run()::{lambda()#1}, void>::operator()() const /usr/include/c++/7/future:1362 (gdb+0x738660) #12 std::_Function_handler<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> (), std::__future_base::_Task_setter<std::unique_ptr<std::__future_base::_Result<void>, std::__future_base::_Result_base::_Deleter>, std::__future_base::_Task_state<std::function<void ()>, std::allocator<int>, void ()>::_M_run()::{lambda()#1}, void> >::_M_invoke(std::_Any_data const&) /usr/include/c++/7/bits/std_function.h:302 (gdb+0x73825c) #13 std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>::operator()() const /usr/include/c++/7/bits/std_function.h:706 (gdb+0x733623) #14 std::__future_base::_State_baseV2::_M_do_set(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*) /usr/include/c++/7/future:561 (gdb+0x732bdf) #15 void std::__invoke_impl<void, void (std::__future_base::_State_baseV2::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*>(std::__invoke_memfun_deref, void (std::__future_base::_State_baseV2::*&&)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*&&, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*&&, bool*&&) /usr/include/c++/7/bits/invoke.h:73 (gdb+0x734c4f) #16 std::__invoke_result<void (std::__future_base::_State_baseV2::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*>::type std::__invoke<void (std::__future_base::_State_baseV2::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*>(void (std::__future_base::_State_baseV2::*&&)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*&&, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*&&, bool*&&) /usr/include/c++/7/bits/invoke.h:95 (gdb+0x733bc5) #17 std::call_once<void (std::__future_base::_State_baseV2::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*>(std::once_flag&, void (std::__future_base::_State_baseV2::*&&)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*&&, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*&&, bool*&&)::{lambda()#1}::operator()() const /usr/include/c++/7/mutex:672 (gdb+0x73300d) #18 std::call_once<void (std::__future_base::_State_baseV2::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*>(std::once_flag&, void (std::__future_base::_State_baseV2::*&&)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*&&, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*&&, bool*&&)::{lambda()#2}::operator()() const /usr/include/c++/7/mutex:677 (gdb+0x7330b2) #19 std::call_once<void (std::__future_base::_State_baseV2::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*>(std::once_flag&, void (std::__future_base::_State_baseV2::*&&)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*&&, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*&&, bool*&&)::{lambda()#2}::_FUN() /usr/include/c++/7/mutex:677 (gdb+0x7330f2) #20 pthread_once <null> (libtsan.so.0+0x4457c) #21 __gthread_once /usr/include/c++/7/x86_64-suse-linux/bits/gthr-default.h:699 (gdb+0x72f5dd) #22 void std::call_once<void (std::__future_base::_State_baseV2::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*>(std::once_flag&, void (std::__future_base::_State_baseV2::*&&)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*&&, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*&&, bool*&&) /usr/include/c++/7/mutex:684 (gdb+0x733224) #23 std::__future_base::_State_baseV2::_M_set_result(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>, bool) /usr/include/c++/7/future:401 (gdb+0x732852) #24 std::__future_base::_Task_state<std::function<void ()>, std::allocator<int>, void ()>::_M_run() /usr/include/c++/7/future:1423 (gdb+0x737bef) #25 std::packaged_task<void ()>::operator()() /usr/include/c++/7/future:1556 (gdb+0x1dac5b0) #26 gdb::thread_pool::thread_function() gdbsupport/thread-pool.cc:242 (gdb+0x1dabed2) #27 void std::__invoke_impl<void, void (gdb::thread_pool::*)(), gdb::thread_pool*>(std::__invoke_memfun_deref, void (gdb::thread_pool::*&&)(), gdb::thread_pool*&&) /usr/include/c++/7/bits/invoke.h:73 (gdb+0x1dacf81) #28 std::__invoke_result<void (gdb::thread_pool::*)(), gdb::thread_pool*>::type std::__invoke<void (gdb::thread_pool::*)(), gdb::thread_pool*>(void (gdb::thread_pool::*&&)(), gdb::thread_pool*&&) /usr/include/c++/7/bits/invoke.h:95 (gdb+0x1dac3b2) #29 decltype (__invoke((_S_declval<0ul>)(), (_S_declval<1ul>)())) std:🧵:_Invoker<std::tuple<void (gdb::thread_pool::*)(), gdb::thread_pool*> >::_M_invoke<0ul, 1ul>(std::_Index_tuple<0ul, 1ul>) /usr/include/c++/7/thread:234 (gdb+0x1daf6e4) #30 std:🧵:_Invoker<std::tuple<void (gdb::thread_pool::*)(), gdb::thread_pool*> >::operator()() /usr/include/c++/7/thread:243 (gdb+0x1daf66f) #31 std:🧵:_State_impl<std:🧵:_Invoker<std::tuple<void (gdb::thread_pool::*)(), gdb::thread_pool*> > >::_M_run() /usr/include/c++/7/thread:186 (gdb+0x1daf624) #32 <null> <null> (libstdc++.so.6+0xdcac2) ... SUMMARY: ThreadSanitizer: data race bfd/cache.c:584 in bfd_open_file ... The race happens when issuing the "file $exec" command. The race is between: - a worker thread getting the build id while writing the index cache, and in the process reading bfd::format, and - the main thread calling find_main_name, and in the process setting bfd::cacheable. The two bitfields bfd::cacheable and bfd::format share the same bitfield container. Fix this by capturing the build id in the main thread, and using the captured value in the worker thread. Likewise for the dwz build id, which likely suffers from the same issue. While we're at it, also move the creation of the cache directory to the index_cache_store_context constructor, to: - make sure there's no race between subsequent file commands, and - issue any related warning or error messages during the file command. Tested on x86_64-linux. Approved-By: Tom Tromey <tom@tromey.com> PR symtab/30392 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30392
423 lines
12 KiB
C
423 lines
12 KiB
C
/* Caching of GDB/DWARF index files.
|
|
|
|
Copyright (C) 1994-2023 Free Software Foundation, Inc.
|
|
|
|
This file is part of GDB.
|
|
|
|
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/>. */
|
|
|
|
#include "defs.h"
|
|
#include "dwarf2/index-cache.h"
|
|
|
|
#include "build-id.h"
|
|
#include "cli/cli-cmds.h"
|
|
#include "cli/cli-decode.h"
|
|
#include "command.h"
|
|
#include "gdbsupport/scoped_mmap.h"
|
|
#include "gdbsupport/pathstuff.h"
|
|
#include "dwarf2/index-write.h"
|
|
#include "dwarf2/read.h"
|
|
#include "dwarf2/dwz.h"
|
|
#include "objfiles.h"
|
|
#include "gdbsupport/selftest.h"
|
|
#include <string>
|
|
#include <stdlib.h>
|
|
|
|
/* When set to true, show debug messages about the index cache. */
|
|
static bool debug_index_cache = false;
|
|
|
|
#define index_cache_debug(FMT, ...) \
|
|
debug_prefixed_printf_cond_nofunc (debug_index_cache, "index-cache", \
|
|
FMT, ## __VA_ARGS__)
|
|
|
|
/* The index cache directory, used for "set/show index-cache directory". */
|
|
static std::string index_cache_directory;
|
|
|
|
/* See dwarf-index.cache.h. */
|
|
index_cache global_index_cache;
|
|
|
|
/* set/show index-cache commands. */
|
|
static cmd_list_element *set_index_cache_prefix_list;
|
|
static cmd_list_element *show_index_cache_prefix_list;
|
|
|
|
/* Default destructor of index_cache_resource. */
|
|
index_cache_resource::~index_cache_resource () = default;
|
|
|
|
/* See dwarf-index-cache.h. */
|
|
|
|
void
|
|
index_cache::set_directory (std::string dir)
|
|
{
|
|
gdb_assert (!dir.empty ());
|
|
|
|
m_dir = std::move (dir);
|
|
|
|
index_cache_debug ("now using directory %s", m_dir.c_str ());
|
|
}
|
|
|
|
/* See dwarf-index-cache.h. */
|
|
|
|
void
|
|
index_cache::enable ()
|
|
{
|
|
index_cache_debug ("enabling (%s)", m_dir.c_str ());
|
|
|
|
m_enabled = true;
|
|
}
|
|
|
|
/* See dwarf-index-cache.h. */
|
|
|
|
void
|
|
index_cache::disable ()
|
|
{
|
|
index_cache_debug ("disabling");
|
|
|
|
m_enabled = false;
|
|
}
|
|
|
|
/* See index-cache.h. */
|
|
|
|
index_cache_store_context::index_cache_store_context (const index_cache &ic,
|
|
dwarf2_per_bfd *per_bfd)
|
|
: m_enabled (ic.enabled ())
|
|
{
|
|
if (!m_enabled)
|
|
return;
|
|
|
|
/* Get build id of objfile. */
|
|
const bfd_build_id *build_id = build_id_bfd_get (per_bfd->obfd);
|
|
if (build_id == nullptr)
|
|
{
|
|
index_cache_debug ("objfile %s has no build id",
|
|
bfd_get_filename (per_bfd->obfd));
|
|
m_enabled = false;
|
|
return;
|
|
}
|
|
build_id_str = build_id_to_string (build_id);
|
|
|
|
/* Get build id of dwz file, if present. */
|
|
const dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
|
|
|
|
if (dwz != nullptr)
|
|
{
|
|
const bfd_build_id *dwz_build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
|
|
|
|
if (dwz_build_id == nullptr)
|
|
{
|
|
index_cache_debug ("dwz objfile %s has no build id",
|
|
dwz->filename ());
|
|
m_enabled = false;
|
|
return;
|
|
}
|
|
|
|
dwz_build_id_str = build_id_to_string (dwz_build_id);
|
|
}
|
|
|
|
if (ic.m_dir.empty ())
|
|
{
|
|
warning (_("The index cache directory name is empty, skipping store."));
|
|
m_enabled = false;
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
/* Try to create the containing directory. */
|
|
if (!mkdir_recursive (ic.m_dir.c_str ()))
|
|
{
|
|
warning (_("index cache: could not make cache directory: %s"),
|
|
safe_strerror (errno));
|
|
m_enabled = false;
|
|
return;
|
|
}
|
|
}
|
|
catch (const gdb_exception_error &except)
|
|
{
|
|
index_cache_debug ("couldn't store index cache for objfile %s: %s",
|
|
bfd_get_filename (per_bfd->obfd), except.what ());
|
|
m_enabled = false;
|
|
}
|
|
}
|
|
|
|
/* See dwarf-index-cache.h. */
|
|
|
|
void
|
|
index_cache::store (dwarf2_per_bfd *per_bfd,
|
|
const index_cache_store_context &ctx)
|
|
{
|
|
if (!ctx.m_enabled)
|
|
return;
|
|
|
|
const char *dwz_build_id_ptr = (ctx.dwz_build_id_str.has_value ()
|
|
? ctx.dwz_build_id_str->c_str ()
|
|
: nullptr);
|
|
|
|
try
|
|
{
|
|
index_cache_debug ("writing index cache for objfile %s",
|
|
bfd_get_filename (per_bfd->obfd));
|
|
|
|
/* Write the index itself to the directory, using the build id as the
|
|
filename. */
|
|
write_dwarf_index (per_bfd, m_dir.c_str (),
|
|
ctx.build_id_str.c_str (), dwz_build_id_ptr,
|
|
dw_index_kind::GDB_INDEX);
|
|
}
|
|
catch (const gdb_exception_error &except)
|
|
{
|
|
index_cache_debug ("couldn't store index cache for objfile %s: %s",
|
|
bfd_get_filename (per_bfd->obfd), except.what ());
|
|
}
|
|
}
|
|
|
|
#if HAVE_SYS_MMAN_H
|
|
|
|
/* Hold the resources for an mmapped index file. */
|
|
|
|
struct index_cache_resource_mmap final : public index_cache_resource
|
|
{
|
|
/* Try to mmap FILENAME. Throw an exception on failure, including if the
|
|
file doesn't exist. */
|
|
index_cache_resource_mmap (const char *filename)
|
|
: mapping (mmap_file (filename))
|
|
{}
|
|
|
|
scoped_mmap mapping;
|
|
};
|
|
|
|
/* See dwarf-index-cache.h. */
|
|
|
|
gdb::array_view<const gdb_byte>
|
|
index_cache::lookup_gdb_index (const bfd_build_id *build_id,
|
|
std::unique_ptr<index_cache_resource> *resource)
|
|
{
|
|
if (!enabled ())
|
|
return {};
|
|
|
|
if (m_dir.empty ())
|
|
{
|
|
warning (_("The index cache directory name is empty, skipping cache "
|
|
"lookup."));
|
|
return {};
|
|
}
|
|
|
|
/* Compute where we would expect a gdb index file for this build id to be. */
|
|
std::string filename = make_index_filename (build_id, INDEX4_SUFFIX);
|
|
|
|
try
|
|
{
|
|
index_cache_debug ("trying to read %s",
|
|
filename.c_str ());
|
|
|
|
/* Try to map that file. */
|
|
index_cache_resource_mmap *mmap_resource
|
|
= new index_cache_resource_mmap (filename.c_str ());
|
|
|
|
/* Yay, it worked! Hand the resource to the caller. */
|
|
resource->reset (mmap_resource);
|
|
|
|
return gdb::array_view<const gdb_byte>
|
|
((const gdb_byte *) mmap_resource->mapping.get (),
|
|
mmap_resource->mapping.size ());
|
|
}
|
|
catch (const gdb_exception_error &except)
|
|
{
|
|
index_cache_debug ("couldn't read %s: %s",
|
|
filename.c_str (), except.what ());
|
|
}
|
|
|
|
return {};
|
|
}
|
|
|
|
#else /* !HAVE_SYS_MMAN_H */
|
|
|
|
/* See dwarf-index-cache.h. This is a no-op on unsupported systems. */
|
|
|
|
gdb::array_view<const gdb_byte>
|
|
index_cache::lookup_gdb_index (const bfd_build_id *build_id,
|
|
std::unique_ptr<index_cache_resource> *resource)
|
|
{
|
|
return {};
|
|
}
|
|
|
|
#endif
|
|
|
|
/* See dwarf-index-cache.h. */
|
|
|
|
std::string
|
|
index_cache::make_index_filename (const bfd_build_id *build_id,
|
|
const char *suffix) const
|
|
{
|
|
std::string build_id_str = build_id_to_string (build_id);
|
|
|
|
return m_dir + SLASH_STRING + build_id_str + suffix;
|
|
}
|
|
|
|
/* True when we are executing "show index-cache". This is used to improve the
|
|
printout a little bit. */
|
|
static bool in_show_index_cache_command = false;
|
|
|
|
/* "show index-cache" handler. */
|
|
|
|
static void
|
|
show_index_cache_command (const char *arg, int from_tty)
|
|
{
|
|
/* Note that we are executing "show index-cache". */
|
|
auto restore_flag = make_scoped_restore (&in_show_index_cache_command, true);
|
|
|
|
/* Call all "show index-cache" subcommands. */
|
|
cmd_show_list (show_index_cache_prefix_list, from_tty);
|
|
|
|
gdb_printf ("\n");
|
|
gdb_printf
|
|
(_("The index cache is currently %s.\n"),
|
|
global_index_cache.enabled () ? _("enabled") : _("disabled"));
|
|
}
|
|
|
|
/* "set/show index-cache enabled" set callback. */
|
|
|
|
static void
|
|
set_index_cache_enabled_command (bool value)
|
|
{
|
|
if (value)
|
|
global_index_cache.enable ();
|
|
else
|
|
global_index_cache.disable ();
|
|
}
|
|
|
|
/* "set/show index-cache enabled" get callback. */
|
|
|
|
static bool
|
|
get_index_cache_enabled_command ()
|
|
{
|
|
return global_index_cache.enabled ();
|
|
}
|
|
|
|
/* "set/show index-cache enabled" show callback. */
|
|
|
|
static void
|
|
show_index_cache_enabled_command (ui_file *stream, int from_tty,
|
|
cmd_list_element *cmd, const char *value)
|
|
{
|
|
gdb_printf (stream, _("The index cache is %s.\n"), value);
|
|
}
|
|
|
|
/* "set index-cache directory" handler. */
|
|
|
|
static void
|
|
set_index_cache_directory_command (const char *arg, int from_tty,
|
|
cmd_list_element *element)
|
|
{
|
|
/* Make sure the index cache directory is absolute and tilde-expanded. */
|
|
index_cache_directory = gdb_abspath (index_cache_directory.c_str ());
|
|
global_index_cache.set_directory (index_cache_directory);
|
|
}
|
|
|
|
/* "show index-cache stats" handler. */
|
|
|
|
static void
|
|
show_index_cache_stats_command (const char *arg, int from_tty)
|
|
{
|
|
const char *indent = "";
|
|
|
|
/* If this command is invoked through "show index-cache", make the display a
|
|
bit nicer. */
|
|
if (in_show_index_cache_command)
|
|
{
|
|
indent = " ";
|
|
gdb_printf ("\n");
|
|
}
|
|
|
|
gdb_printf (_("%s Cache hits (this session): %u\n"),
|
|
indent, global_index_cache.n_hits ());
|
|
gdb_printf (_("%sCache misses (this session): %u\n"),
|
|
indent, global_index_cache.n_misses ());
|
|
}
|
|
|
|
void _initialize_index_cache ();
|
|
void
|
|
_initialize_index_cache ()
|
|
{
|
|
/* Set the default index cache directory. */
|
|
std::string cache_dir = get_standard_cache_dir ();
|
|
if (!cache_dir.empty ())
|
|
{
|
|
index_cache_directory = cache_dir;
|
|
global_index_cache.set_directory (std::move (cache_dir));
|
|
}
|
|
else
|
|
warning (_("Couldn't determine a path for the index cache directory."));
|
|
|
|
/* set index-cache */
|
|
add_basic_prefix_cmd ("index-cache", class_files,
|
|
_("Set index-cache options."),
|
|
&set_index_cache_prefix_list,
|
|
false, &setlist);
|
|
|
|
/* show index-cache */
|
|
add_prefix_cmd ("index-cache", class_files, show_index_cache_command,
|
|
_("Show index-cache options."), &show_index_cache_prefix_list,
|
|
false, &showlist);
|
|
|
|
/* set/show index-cache enabled */
|
|
set_show_commands setshow_index_cache_enabled_cmds
|
|
= add_setshow_boolean_cmd ("enabled", class_files,
|
|
_("Enable the index cache."),
|
|
_("Show whether the index cache is enabled."),
|
|
_("When on, enable the use of the index cache."),
|
|
set_index_cache_enabled_command,
|
|
get_index_cache_enabled_command,
|
|
show_index_cache_enabled_command,
|
|
&set_index_cache_prefix_list,
|
|
&show_index_cache_prefix_list);
|
|
|
|
/* set index-cache on */
|
|
cmd_list_element *set_index_cache_on_cmd
|
|
= add_alias_cmd ("on", setshow_index_cache_enabled_cmds.set, class_files,
|
|
false, &set_index_cache_prefix_list);
|
|
deprecate_cmd (set_index_cache_on_cmd, "set index-cache enabled on");
|
|
set_index_cache_on_cmd->default_args = "on";
|
|
|
|
/* set index-cache off */
|
|
cmd_list_element *set_index_cache_off_cmd
|
|
= add_alias_cmd ("off", setshow_index_cache_enabled_cmds.set, class_files,
|
|
false, &set_index_cache_prefix_list);
|
|
deprecate_cmd (set_index_cache_off_cmd, "set index-cache enabled off");
|
|
set_index_cache_off_cmd->default_args = "off";
|
|
|
|
/* set index-cache directory */
|
|
add_setshow_filename_cmd ("directory", class_files, &index_cache_directory,
|
|
_("Set the directory of the index cache."),
|
|
_("Show the directory of the index cache."),
|
|
NULL,
|
|
set_index_cache_directory_command, NULL,
|
|
&set_index_cache_prefix_list,
|
|
&show_index_cache_prefix_list);
|
|
|
|
/* show index-cache stats */
|
|
add_cmd ("stats", class_files, show_index_cache_stats_command,
|
|
_("Show some stats about the index cache."),
|
|
&show_index_cache_prefix_list);
|
|
|
|
/* set debug index-cache */
|
|
add_setshow_boolean_cmd ("index-cache", class_maintenance,
|
|
&debug_index_cache,
|
|
_("Set display of index-cache debug messages."),
|
|
_("Show display of index-cache debug messages."),
|
|
_("\
|
|
When non-zero, debugging output for the index cache is displayed."),
|
|
NULL, NULL,
|
|
&setdebuglist, &showdebuglist);
|
|
}
|