diff --git a/gdb/dwarf2/cooked-index.c b/gdb/dwarf2/cooked-index.c index 7ddcdedf9d0..df82e86b690 100644 --- a/gdb/dwarf2/cooked-index.c +++ b/gdb/dwarf2/cooked-index.c @@ -460,7 +460,7 @@ cooked_index::cooked_index (vec_type &&vec) void cooked_index::start_writing_index (dwarf2_per_bfd *per_bfd) { - index_cache_store_context ctx (global_index_cache); + index_cache_store_context ctx (global_index_cache, per_bfd); /* This must be set after all the finalization tasks have been started, because it may call 'wait'. */ diff --git a/gdb/dwarf2/index-cache.c b/gdb/dwarf2/index-cache.c index e2fb984799c..69f70642dc6 100644 --- a/gdb/dwarf2/index-cache.c +++ b/gdb/dwarf2/index-cache.c @@ -88,18 +88,11 @@ index_cache::disable () /* See index-cache.h. */ -index_cache_store_context::index_cache_store_context (const index_cache &ic) +index_cache_store_context::index_cache_store_context (const index_cache &ic, + dwarf2_per_bfd *per_bfd) : m_enabled (ic.enabled ()) { -} - -/* See dwarf-index-cache.h. */ - -void -index_cache::store (dwarf2_per_bfd *per_bfd, - const index_cache_store_context &ctx) -{ - if (!ctx.m_enabled) + if (!m_enabled) return; /* Get build id of objfile. */ @@ -108,15 +101,13 @@ index_cache::store (dwarf2_per_bfd *per_bfd, { index_cache_debug ("objfile %s has no build id", bfd_get_filename (per_bfd->obfd)); + m_enabled = false; return; } - - std::string build_id_str = build_id_to_string (build_id); + build_id_str = build_id_to_string (build_id); /* Get build id of dwz file, if present. */ - gdb::optional dwz_build_id_str; const dwz_file *dwz = dwarf2_get_dwz_file (per_bfd); - const char *dwz_build_id_ptr = NULL; if (dwz != nullptr) { @@ -126,36 +117,61 @@ index_cache::store (dwarf2_per_bfd *per_bfd, { 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); - dwz_build_id_ptr = dwz_build_id_str->c_str (); } - if (m_dir.empty ()) + 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 (m_dir.c_str ())) + 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 (), - build_id_str.c_str (), dwz_build_id_ptr, + ctx.build_id_str.c_str (), dwz_build_id_ptr, dw_index_kind::GDB_INDEX); } catch (const gdb_exception_error &except) diff --git a/gdb/dwarf2/index-cache.h b/gdb/dwarf2/index-cache.h index 7ea972d5c7f..cfa45435fbd 100644 --- a/gdb/dwarf2/index-cache.h +++ b/gdb/dwarf2/index-cache.h @@ -42,17 +42,24 @@ struct index_cache_store_context { friend class index_cache; - explicit index_cache_store_context (const index_cache &ic); + index_cache_store_context (const index_cache &ic, dwarf2_per_bfd *per_bfd); private: /* Captured value of enabled (). */ bool m_enabled; + + /* Captured value of build id. */ + std::string build_id_str; + + /* Captured value of dwz build id. */ + gdb::optional dwz_build_id_str; }; /* Class to manage the access to the DWARF index cache. */ class index_cache { + friend struct index_cache_store_context; public: /* Change the directory used to save/load index files. */ void set_directory (std::string dir);