Files
binutils-gdb/gdb/dwarf2/index-cache.h
Aaron Merey af1c8a8d9a gdb/debuginfod: Support on-demand debuginfo downloading
At the beginning of a session, gdb may attempt to download debuginfo
for all shared libraries associated with the process or core file
being debugged.  This can be a waste of time and storage space when much
of the debuginfo ends up not being used during the session.

To reduce the gdb's startup latency and to download only the debuginfo
that is really needed, this patch adds on-demand downloading of debuginfo.

'set debuginfo enabled on' now causes gdb to attempt to download a .gdb_index
for each shared library instead of its full debuginfo.  Each corresponding
separate debuginfo will be deferred until gdb needs to expand symtabs
associated with the debuginfo's index.

Because these indices are significantly smaller than their corresponding
debuginfo, this generally reduces the total amount of data gdb downloads.
Reductions of 80%-95% have been observed when debugging large GUI programs.

    (gdb) set debuginfod enabled on
    (gdb) start
    Downloading section .gdb_index for /lib64/libcurl.so.4
    [...]
    1826        client->server_mhandle = curl_multi_init ();
    (gdb) step
    Downloading separate debug info for /lib64/libcurl.so.4
    Downloading separate debug info for [libcurl dwz]
    Downloading source file /usr/src/debug/curl-7.85.0-6.fc37.x86_64/build-full/lib/../../lib/multi.c
    curl_multi_init () at ../../lib/multi.c:457
    457     {
    (gdb)

Some of the key functions below include dwarf2_has_separate_index which
downloads the separate .gdb_index.  If successful, the shared library
objfile owns the index until the separate debug objfile is downloaded
or confirmed to not be available.

read_full_dwarf_from_debuginfod downloads the full debuginfo and
initializes the separate debug objfile.  It is called by functions
such as dwarf2_gdb_index::expand_symtabs_matching and
dwarf2_base_index_functions::find_pc_sect_compunit_symtab when symtab
expansion is required.
2024-03-19 20:43:12 -04:00

154 lines
4.3 KiB
C++

/* Caching of GDB/DWARF index files.
Copyright (C) 2018-2024 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/>. */
#ifndef DWARF_INDEX_CACHE_H
#define DWARF_INDEX_CACHE_H
#include "dwarf2/index-common.h"
#include "gdbsupport/array-view.h"
#include "symfile.h"
class dwarf2_per_bfd;
class index_cache;
/* Base of the classes used to hold the resources of the indices loaded from
the cache (e.g. mmapped files). */
struct index_cache_resource
{
virtual ~index_cache_resource () = 0;
};
/* Information to be captured in the main thread, and to be used by worker
threads during store (). */
struct index_cache_store_context
{
index_cache_store_context (const index_cache &ic, dwarf2_per_bfd *per_bfd);
/* Store the index in the cache. */
void store () const;
private:
/* Captured value of enabled (). */
bool m_enabled;
/* Captured value of index cache directory. */
std::string m_dir;
/* The per-bfd object that we're caching. */
dwarf2_per_bfd *m_per_bfd;
/* Captured value of build id. */
std::string m_build_id_str;
/* Captured value of dwz build id. */
std::optional<std::string> m_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);
/* Return true if the usage of the cache is enabled. */
bool enabled () const
{
return m_enabled;
}
/* Enable the cache. */
void enable ();
/* Disable the cache. */
void disable ();
/* Look for an index file matching BUILD_ID. If found, return the contents
as an array_view and store the underlying resources (allocated memory,
mapped file, etc) in RESOURCE. The returned array_view is valid as long
as RESOURCE is not destroyed.
If no matching index file is found, return an empty array view. */
gdb::array_view<const gdb_byte>
lookup_gdb_index (const bfd_build_id *build_id,
std::unique_ptr<index_cache_resource> *resource);
/* Look for an index file located at INDEX_PATH in the debuginfod cache.
Unlike lookup_gdb_index, this function does not exit early if the
index cache has not been enabled.
If found, return the contents as an array_view and store the underlying
resources (allocated memory, mapped file, etc) in RESOURCE. The returned
array_view is valid as long as RESOURCE is not destroyed.
If no matching index file is found, return an empty array view. */
gdb::array_view<const gdb_byte>
lookup_gdb_index_debuginfod (const char *index_path,
std::unique_ptr<index_cache_resource> *resource);
/* Return the number of cache hits. */
unsigned int n_hits () const
{ return m_n_hits; }
/* Record a cache hit. */
void hit ()
{
if (enabled ())
m_n_hits++;
}
/* Return the number of cache misses. */
unsigned int n_misses () const
{ return m_n_misses; }
/* Record a cache miss. */
void miss ()
{
if (enabled ())
m_n_misses++;
}
private:
/* Compute the absolute filename where the index of the objfile with build
id BUILD_ID will be stored. SUFFIX is appended at the end of the
filename. */
std::string make_index_filename (const bfd_build_id *build_id,
const char *suffix) const;
/* The base directory where we are storing and looking up index files. */
std::string m_dir;
/* Whether the cache is enabled. */
bool m_enabled = false;
/* Number of cache hits and misses during this GDB session. */
unsigned int m_n_hits = 0;
unsigned int m_n_misses = 0;
};
/* The global instance of the index cache. */
extern index_cache global_index_cache;
#endif /* DWARF_INDEX_CACHE_H */