| 1 | /* Caching of GDB/DWARF index files. |
| 2 | |
| 3 | Copyright (C) 2018-2020 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #ifndef DWARF_INDEX_CACHE_H |
| 21 | #define DWARF_INDEX_CACHE_H |
| 22 | |
| 23 | #include "dwarf-index-common.h" |
| 24 | #include "gdbsupport/array-view.h" |
| 25 | #include "symfile.h" |
| 26 | |
| 27 | /* Base of the classes used to hold the resources of the indices loaded from |
| 28 | the cache (e.g. mmapped files). */ |
| 29 | |
| 30 | struct index_cache_resource |
| 31 | { |
| 32 | virtual ~index_cache_resource () = 0; |
| 33 | }; |
| 34 | |
| 35 | /* Class to manage the access to the DWARF index cache. */ |
| 36 | |
| 37 | class index_cache |
| 38 | { |
| 39 | public: |
| 40 | /* Change the directory used to save/load index files. */ |
| 41 | void set_directory (std::string dir); |
| 42 | |
| 43 | /* Return true if the usage of the cache is enabled. */ |
| 44 | bool enabled () const |
| 45 | { |
| 46 | return m_enabled; |
| 47 | } |
| 48 | |
| 49 | /* Enable the cache. */ |
| 50 | void enable (); |
| 51 | |
| 52 | /* Disable the cache. */ |
| 53 | void disable (); |
| 54 | |
| 55 | /* Store an index for the specified object file in the cache. */ |
| 56 | void store (struct dwarf2_per_objfile *dwarf2_per_objfile); |
| 57 | |
| 58 | /* Look for an index file matching BUILD_ID. If found, return the contents |
| 59 | as an array_view and store the underlying resources (allocated memory, |
| 60 | mapped file, etc) in RESOURCE. The returned array_view is valid as long |
| 61 | as RESOURCE is not destroyed. |
| 62 | |
| 63 | If no matching index file is found, return an empty array view. */ |
| 64 | gdb::array_view<const gdb_byte> |
| 65 | lookup_gdb_index (const bfd_build_id *build_id, |
| 66 | std::unique_ptr<index_cache_resource> *resource); |
| 67 | |
| 68 | /* Return the number of cache hits. */ |
| 69 | unsigned int n_hits () const |
| 70 | { return m_n_hits; } |
| 71 | |
| 72 | /* Record a cache hit. */ |
| 73 | void hit () |
| 74 | { |
| 75 | if (enabled ()) |
| 76 | m_n_hits++; |
| 77 | } |
| 78 | |
| 79 | /* Return the number of cache misses. */ |
| 80 | unsigned int n_misses () const |
| 81 | { return m_n_misses; } |
| 82 | |
| 83 | /* Record a cache miss. */ |
| 84 | void miss () |
| 85 | { |
| 86 | if (enabled ()) |
| 87 | m_n_misses++; |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | |
| 92 | /* Compute the absolute filename where the index of the objfile with build |
| 93 | id BUILD_ID will be stored. SUFFIX is appended at the end of the |
| 94 | filename. */ |
| 95 | std::string make_index_filename (const bfd_build_id *build_id, |
| 96 | const char *suffix) const; |
| 97 | |
| 98 | /* The base directory where we are storing and looking up index files. */ |
| 99 | std::string m_dir; |
| 100 | |
| 101 | /* Whether the cache is enabled. */ |
| 102 | bool m_enabled = false; |
| 103 | |
| 104 | /* Number of cache hits and misses during this GDB session. */ |
| 105 | unsigned int m_n_hits = 0; |
| 106 | unsigned int m_n_misses = 0; |
| 107 | }; |
| 108 | |
| 109 | /* The global instance of the index cache. */ |
| 110 | extern index_cache global_index_cache; |
| 111 | |
| 112 | #endif /* DWARF_INDEX_CACHE_H */ |