Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/Debugger/DisassemblyManager.h
3186 views
1
// Copyright (c) 2012- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#pragma once
19
20
#include <string>
21
#include <string_view>
22
23
#include "ppsspp_config.h"
24
#include <mutex>
25
#include "Common/CommonTypes.h"
26
#include "Core/Debugger/SymbolMap.h"
27
#include "Core/MIPS/MIPSAnalyst.h"
28
29
#if PPSSPP_ARCH(AMD64)
30
typedef u64 HashType;
31
#else
32
typedef u32 HashType;
33
#endif
34
35
enum DisassemblyLineType { DISTYPE_OPCODE, DISTYPE_MACRO, DISTYPE_DATA, DISTYPE_OTHER };
36
37
struct DisassemblyLineInfo
38
{
39
DisassemblyLineType type;
40
MIPSAnalyst::MipsOpcodeInfo info;
41
std::string name;
42
std::string params;
43
u32 totalSize;
44
};
45
46
enum DisasmLineType { LINE_UP, LINE_DOWN, LINE_RIGHT };
47
48
struct BranchLine
49
{
50
u32 first;
51
u32 second;
52
DisasmLineType type;
53
int laneIndex;
54
55
bool operator<(const BranchLine& other) const
56
{
57
return first < other.first;
58
}
59
};
60
61
class DisassemblyEntry
62
{
63
public:
64
virtual ~DisassemblyEntry() { };
65
virtual void recheck() = 0;
66
virtual int getNumLines() = 0;
67
virtual int getLineNum(u32 address, bool findStart) = 0;
68
virtual u32 getLineAddress(int line) = 0;
69
virtual u32 getTotalSize() = 0;
70
virtual bool disassemble(u32 address, DisassemblyLineInfo& dest, bool insertSymbols, DebugInterface *cpuDebug) = 0;
71
virtual void getBranchLines(u32 start, u32 size, std::vector<BranchLine>& dest) { };
72
};
73
74
class DisassemblyFunction: public DisassemblyEntry
75
{
76
public:
77
DisassemblyFunction(u32 _address, u32 _size);
78
~DisassemblyFunction();
79
void recheck() override;
80
int getNumLines() override;
81
int getLineNum(u32 address, bool findStart) override;
82
u32 getLineAddress(int line) override;
83
u32 getTotalSize() override { return size; };
84
bool disassemble(u32 address, DisassemblyLineInfo& dest, bool insertSymbols, DebugInterface *cpuDebug) override;
85
void getBranchLines(u32 start, u32 size, std::vector<BranchLine>& dest) override;
86
87
private:
88
void generateBranchLines();
89
void load();
90
void clear();
91
void addOpcodeSequence(u32 start, u32 end);
92
93
u32 address;
94
u32 size;
95
HashType hash;
96
std::vector<BranchLine> lines;
97
std::map<u32,DisassemblyEntry*> entries;
98
std::vector<u32> lineAddresses;
99
std::recursive_mutex lock_;
100
};
101
102
class DisassemblyOpcode: public DisassemblyEntry
103
{
104
public:
105
DisassemblyOpcode(u32 _address, int _num): address(_address), num(_num) { }
106
void recheck() override { };
107
int getNumLines() override { return num; };
108
int getLineNum(u32 address, bool findStart) override { return (address - this->address) / 4; };
109
u32 getLineAddress(int line) override { return address + line * 4; };
110
u32 getTotalSize() override { return num * 4; };
111
bool disassemble(u32 address, DisassemblyLineInfo& dest, bool insertSymbols, DebugInterface *cpuDebug) override;
112
void getBranchLines(u32 start, u32 size, std::vector<BranchLine>& dest) override;
113
114
private:
115
u32 address;
116
int num;
117
};
118
119
120
class DisassemblyMacro: public DisassemblyEntry
121
{
122
public:
123
DisassemblyMacro(u32 _address): address(_address) { }
124
125
void setMacroLi(u32 _immediate, u8 _rt);
126
void setMacroMemory(std::string_view _name, u32 _immediate, u8 _rt, int _dataSize);
127
128
void recheck() override { };
129
int getNumLines() override { return 1; };
130
int getLineNum(u32 address, bool findStart) override { return 0; };
131
u32 getLineAddress(int line) override { return address; };
132
u32 getTotalSize() override { return numOpcodes * 4; };
133
bool disassemble(u32 address, DisassemblyLineInfo& dest, bool insertSymbols, DebugInterface *cpuDebug) override;
134
private:
135
enum MacroType { MACRO_LI, MACRO_MEMORYIMM };
136
137
MacroType type;
138
std::string name;
139
u32 immediate;
140
u32 address;
141
u32 numOpcodes;
142
u8 rt;
143
int dataSize;
144
};
145
146
147
class DisassemblyData: public DisassemblyEntry
148
{
149
public:
150
DisassemblyData(u32 _address, u32 _size, DataType _type);
151
152
void recheck() override;
153
int getNumLines() override { return (int)lines.size(); };
154
int getLineNum(u32 address, bool findStart) override;
155
u32 getLineAddress(int line) override { return lineAddresses[line]; };
156
u32 getTotalSize() override { return size; };
157
bool disassemble(u32 address, DisassemblyLineInfo& dest, bool insertSymbols, DebugInterface *cpuDebug) override;
158
159
private:
160
void createLines();
161
162
struct DataEntry
163
{
164
std::string text;
165
u32 size;
166
int lineNum;
167
};
168
169
u32 address;
170
u32 size;
171
HashType hash;
172
DataType type;
173
std::map<u32,DataEntry> lines;
174
std::vector<u32> lineAddresses;
175
std::recursive_mutex lock_;
176
};
177
178
class DisassemblyComment: public DisassemblyEntry
179
{
180
public:
181
DisassemblyComment(u32 _address, u32 _size, std::string_view name, std::string_view param);
182
183
void recheck() override { };
184
int getNumLines() override { return 1; };
185
int getLineNum(u32 address, bool findStart) override { return 0; };
186
u32 getLineAddress(int line) override { return address; };
187
u32 getTotalSize() override { return size; };
188
bool disassemble(u32 address, DisassemblyLineInfo& dest, bool insertSymbols, DebugInterface *cpuDebug) override;
189
190
private:
191
u32 address;
192
u32 size;
193
std::string name;
194
std::string param;
195
};
196
197
class DebugInterface;
198
199
class DisassemblyManager {
200
public:
201
~DisassemblyManager();
202
203
void clear();
204
205
void setCpu(DebugInterface *cpu);
206
void setMaxParamChars(int num) { maxParamChars = num; clear(); };
207
void getLine(u32 address, bool insertSymbols, DisassemblyLineInfo &dest, DebugInterface *cpuDebug);
208
void analyze(u32 address, u32 size);
209
std::vector<BranchLine> getBranchLines(u32 start, u32 size);
210
211
u32 getStartAddress(u32 address);
212
u32 getNthPreviousAddress(u32 address, int n = 1);
213
u32 getNthNextAddress(u32 address, int n = 1);
214
215
DebugInterface *getCpu() { return cpu_; };
216
int getMaxParamChars() { return maxParamChars; };
217
218
private:
219
std::map<u32,DisassemblyEntry*> entries;
220
std::recursive_mutex entriesLock_;
221
DebugInterface *cpu_;
222
int maxParamChars = 29;
223
};
224
225
extern DisassemblyManager g_disassemblyManager;
226
227
bool isInInterval(u32 start, u32 size, u32 value);
228
bool IsLikelyStringAt(uint32_t addr);
229
230
std::string DisassembleRange(u32 start, u32 size, bool displaySymbols, MIPSDebugInterface *debugger);
231
bool GetDisasmAddressText(u32 address, char *dest, size_t bufSize, bool abbreviateLabels, bool showData, bool displaySymbols);
232
233