| 1 | // Copyright 2009 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "Common/SymbolDB.h" |
| 5 | |
| 6 | #include <cstring> |
| 7 | #include <map> |
| 8 | #include <mutex> |
| 9 | #include <string> |
| 10 | #include <utility> |
| 11 | |
| 12 | #include "Common/CommonTypes.h" |
| 13 | #include "Common/Logging/Log.h" |
| 14 | |
| 15 | namespace Common |
| 16 | { |
| 17 | static std::string GetStrippedFunctionName(const std::string& symbol_name) |
| 18 | { |
| 19 | std::string name = symbol_name.substr(0, symbol_name.find('(')); |
| 20 | size_t position = name.find(' '); |
| 21 | if (position != std::string::npos) |
| 22 | name.erase(position); |
| 23 | return name; |
| 24 | } |
| 25 | |
| 26 | SymbolDB::SymbolDB() = default; |
| 27 | |
| 28 | SymbolDB::~SymbolDB() = default; |
| 29 | |
| 30 | void Symbol::Rename(const std::string& symbol_name) |
| 31 | { |
| 32 | this->name = symbol_name; |
| 33 | this->function_name = GetStrippedFunctionName(symbol_name); |
| 34 | } |
| 35 | |
| 36 | void SymbolDB::List() |
| 37 | { |
| 38 | std::lock_guard lock(m_mutex); |
| 39 | for (const auto& func : m_functions) |
| 40 | { |
| 41 | DEBUG_LOG_FMT(OSHLE, "{} @ {:08x}: {} bytes (hash {:08x}) : {} calls", func.second.name, |
| 42 | func.second.address, func.second.size, func.second.hash, func.second.num_calls); |
| 43 | } |
| 44 | INFO_LOG_FMT(OSHLE, "{} functions known in this program above.", m_functions.size()); |
| 45 | } |
| 46 | |
| 47 | bool SymbolDB::IsEmpty() const |
| 48 | { |
| 49 | std::lock_guard lock(m_mutex); |
| 50 | return m_functions.empty() && m_notes.empty(); |
| 51 | } |
| 52 | |
| 53 | bool SymbolDB::Clear(const char* prefix) |
| 54 | { |
| 55 | std::lock_guard lock(m_mutex); |
| 56 | |
| 57 | // TODO: honor prefix |
| 58 | m_map_name.clear(); |
| 59 | if (IsEmpty()) |
| 60 | return false; |
| 61 | |
| 62 | m_functions.clear(); |
| 63 | m_notes.clear(); |
| 64 | m_checksum_to_function.clear(); |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | void SymbolDB::Index() |
| 69 | { |
| 70 | std::lock_guard lock(m_mutex); |
| 71 | Index(&m_functions); |
| 72 | } |
| 73 | |
| 74 | void SymbolDB::Index(XFuncMap* functions) |
| 75 | { |
| 76 | int i = 0; |
| 77 | for (auto& func : *functions) |
| 78 | { |
| 79 | func.second.index = i++; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | const Symbol* SymbolDB::GetSymbolFromName(std::string_view name) const |
| 84 | { |
| 85 | std::lock_guard lock(m_mutex); |
| 86 | |
| 87 | for (auto& func : m_functions) |
| 88 | { |
| 89 | if (func.second.function_name == name) |
| 90 | return &func.second; |
| 91 | } |
| 92 | |
| 93 | return nullptr; |
| 94 | } |
| 95 | |
| 96 | std::vector<const Symbol*> SymbolDB::GetSymbolsFromName(std::string_view name) const |
| 97 | { |
| 98 | std::lock_guard lock(m_mutex); |
| 99 | std::vector<const Symbol*> symbols; |
| 100 | |
| 101 | for (auto& func : m_functions) |
| 102 | { |
| 103 | if (func.second.function_name == name) |
| 104 | symbols.push_back(&func.second); |
| 105 | } |
| 106 | |
| 107 | return symbols; |
| 108 | } |
| 109 | |
| 110 | const Symbol* SymbolDB::GetSymbolFromHash(u32 hash) const |
| 111 | { |
| 112 | std::lock_guard lock(m_mutex); |
| 113 | |
| 114 | auto iter = m_checksum_to_function.find(hash); |
| 115 | if (iter == m_checksum_to_function.end()) |
| 116 | return nullptr; |
| 117 | |
| 118 | return *iter->second.begin(); |
| 119 | } |
| 120 | |
| 121 | std::vector<const Symbol*> SymbolDB::GetSymbolsFromHash(u32 hash) const |
| 122 | { |
| 123 | std::lock_guard lock(m_mutex); |
| 124 | |
| 125 | const auto iter = m_checksum_to_function.find(hash); |
| 126 | |
| 127 | if (iter == m_checksum_to_function.cend()) |
| 128 | return {}; |
| 129 | |
| 130 | return {iter->second.cbegin(), iter->second.cend()}; |
| 131 | } |
| 132 | |
| 133 | void SymbolDB::AddCompleteSymbol(const Symbol& symbol) |
| 134 | { |
| 135 | std::lock_guard lock(m_mutex); |
| 136 | m_functions[symbol.address] = symbol; |
| 137 | } |
| 138 | |
| 139 | bool SymbolDB::RenameSymbol(const Symbol& symbol, const std::string& symbol_name) |
| 140 | { |
| 141 | std::lock_guard lock(m_mutex); |
| 142 | |
| 143 | auto it = m_functions.find(symbol.address); |
| 144 | if (it == m_functions.end()) |
| 145 | return false; |
| 146 | |
| 147 | it->second.Rename(symbol_name); |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | bool SymbolDB::RenameSymbol(const Symbol& symbol, const std::string& symbol_name, |
| 152 | const std::string& object_name) |
| 153 | { |
| 154 | std::lock_guard lock(m_mutex); |
| 155 | |
| 156 | auto it = m_functions.find(symbol.address); |
| 157 | if (it == m_functions.end()) |
| 158 | return false; |
| 159 | |
| 160 | it->second.Rename(symbol_name); |
| 161 | it->second.object_name = object_name; |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | } // namespace Common |