Debian Codegraph @

Source/Core/Common/SymbolDB.h

raw symbols SCIP
1 // Copyright 2008 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 // This file contains a generic symbol map implementation. For CPU-specific
5 // magic, derive and extend.
6
7 #pragma once
8
9 #include <map>
10 #include <mutex>
11 #include <set>
12 #include <string>
13 #include <string_view>
14 #include <utility>
15 #include <vector>
16
17 #include "Common/Assert.h"
18 #include "Common/CommonTypes.h"
19
20 namespace Core
21 {
22 class CPUThreadGuard;
23 }
24
25 namespace Common
26 {
27 struct SCall
28 {
29 SCall(u32 a, u32 b) : function(a), call_address(b) {}
30 u32 function;
31 u32 call_address;
32 };
33
34 struct Note
35 {
36 std::string name;
37 u32 address = 0;
38 u32 size = 0;
39 int layer = 0;
40
41 Note() = default;
42 };
43
44 struct Symbol
45 {
46 enum class Type
47 {
48 Function,
49 Data,
50 };
51
52 Symbol() = default;
53 explicit Symbol(const std::string& symbol_name) { Rename(symbol_name); }
54
55 void Rename(const std::string& symbol_name);
56
57 std::string name;
58 std::string function_name; // stripped function name
59 std::string object_name; // name of object/source file symbol belongs to
60 std::vector<SCall> callers; // addresses of functions that call this function
61 std::vector<SCall> calls; // addresses of functions that are called by this function
62 u32 hash = 0; // use for HLE function finding
63 u32 address = 0;
64 u32 flags = 0;
65 u32 size = 0;
66 int num_calls = 0;
67 Type type = Type::Function;
68 int index = 0; // only used for coloring the disasm view
69 bool analyzed = false;
70 };
71
72 enum
73 {
74 FFLAG_TIMERINSTRUCTIONS = (1 << 0),
75 FFLAG_LEAF = (1 << 1),
76 FFLAG_ONLYCALLSNICELEAFS = (1 << 2),
77 FFLAG_EVIL = (1 << 3),
78 FFLAG_RFI = (1 << 4),
79 FFLAG_STRAIGHT = (1 << 5)
80 };
81
82 class SymbolDB
83 {
84 public:
85 using XFuncMap = std::map<u32, Symbol>;
86 using XNoteMap = std::map<u32, Note>;
87 using XFuncPtrMap = std::map<u32, std::set<Symbol*>>;
88
89 SymbolDB();
90 virtual ~SymbolDB();
91
92 virtual const Symbol* GetSymbolFromAddr(u32 addr) const { return nullptr; }
93 virtual const Symbol* AddFunction(const Core::CPUThreadGuard& guard, u32 start_addr)
94 {
95 return nullptr;
96 }
97 void AddCompleteSymbol(const Symbol& symbol);
98 bool RenameSymbol(const Symbol& symbol, const std::string& symbol_name);
99 bool RenameSymbol(const Symbol& symbol, const std::string& symbol_name,
100 const std::string& object_name);
101
102 const Symbol* GetSymbolFromName(std::string_view name) const;
103 std::vector<const Symbol*> GetSymbolsFromName(std::string_view name) const;
104 const Symbol* GetSymbolFromHash(u32 hash) const;
105 std::vector<const Symbol*> GetSymbolsFromHash(u32 hash) const;
106
107 template <typename F>
108 void ForEachSymbol(F f) const
109 {
110 std::lock_guard lock(m_mutex);
111 for (const auto& [addr, symbol] : m_functions)
112 f(symbol);
113 }
114
115 template <typename F>
116 void ForEachSymbolWithMutation(F f)
117 {
118 std::lock_guard lock(m_mutex);
119 for (auto& [addr, symbol] : m_functions)
120 {
121 f(symbol);
122 ASSERT_MSG(COMMON, addr == symbol.address, "Symbol address was unexpectedly changed");
123 }
124 }
125
126 template <typename F>
127 void ForEachNote(F f) const
128 {
129 std::lock_guard lock(m_mutex);
130 for (const auto& [addr, note] : m_notes)
131 f(note);
132 }
133
134 template <typename F>
135 void ForEachNoteWithMutation(F f)
136 {
137 std::lock_guard lock(m_mutex);
138 for (auto& [addr, note] : m_notes)
139 {
140 f(note);
141 ASSERT_MSG(COMMON, addr == note.address, "Note address was unexpectedly changed");
142 }
143 }
144
145 bool IsEmpty() const;
146 bool Clear(const char* prefix = "");
147 void List();
148 void Index();
149
150 protected:
151 static void Index(XFuncMap* functions);
152
153 XFuncMap m_functions;
154 XNoteMap m_notes;
155 XFuncPtrMap m_checksum_to_function;
156 std::string m_map_name;
157 mutable std::recursive_mutex m_mutex;
158 };
159 } // namespace Common