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