File indexing completed on 2024-05-05 05:44:16

0001 /*
0002     symbolcache.h
0003 
0004     SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB a KDAB Group company <info@kdab.com>
0005     SPDX-FileContributor: Milian Wolff <milian.wolff@kdab.com>
0006 
0007    SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #ifndef SYMBOLCACHE_H
0011 #define SYMBOLCACHE_H
0012 
0013 #include <tsl/robin_map.h>
0014 
0015 #include <string>
0016 #include <vector>
0017 
0018 class SymbolCache
0019 {
0020 public:
0021     struct SymbolCacheEntry
0022     {
0023         SymbolCacheEntry(uint64_t offset = 0, uint64_t value = 0, uint64_t size = 0, const std::string& symname = {})
0024             : offset(offset)
0025             , value(value)
0026             , size(size)
0027             , symname(symname)
0028         {
0029         }
0030 
0031         bool isValid() const
0032         {
0033             return !symname.empty();
0034         }
0035 
0036         // adjusted/absolute st_value, see documentation of the `addr` arg in `dwfl_module_getsym_info`
0037         uint64_t offset;
0038         // unadjusted/relative st_value
0039         uint64_t value;
0040         uint64_t size;
0041         std::string symname;
0042         bool demangled = false;
0043     };
0044     using Symbols = std::vector<SymbolCacheEntry>;
0045 
0046     /// check if @c setSymbolCache was called for @p filePath already
0047     bool hasSymbols(const std::string& filePath) const;
0048     /// take @p cache, sort it and use it for symbol lookups in @p filePath
0049     void setSymbols(const std::string& filePath, Symbols symbols);
0050     /// find the symbol that encompasses @p relAddr in @p filePath
0051     /// if the found symbol wasn't yet demangled, it will be demangled now
0052     SymbolCacheEntry findSymbol(const std::string& filePath, uint64_t relAddr);
0053 
0054 private:
0055     tsl::robin_map<std::string, Symbols> m_symbolCache;
0056 };
0057 
0058 #endif // SYMBOLCACHE_H