CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/Debugger/DisassemblyManager.h
Views: 1401
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 "ppsspp_config.h"
21
#include <mutex>
22
#include "Common/CommonTypes.h"
23
#include "Core/Debugger/SymbolMap.h"
24
#include "Core/MIPS/MIPSAnalyst.h"
25
26
#if PPSSPP_ARCH(AMD64)
27
typedef u64 HashType;
28
#else
29
typedef u32 HashType;
30
#endif
31
32
enum DisassemblyLineType { DISTYPE_OPCODE, DISTYPE_MACRO, DISTYPE_DATA, DISTYPE_OTHER };
33
34
struct DisassemblyLineInfo
35
{
36
DisassemblyLineType type;
37
MIPSAnalyst::MipsOpcodeInfo info;
38
std::string name;
39
std::string params;
40
u32 totalSize;
41
};
42
43
enum LineType { LINE_UP, LINE_DOWN, LINE_RIGHT };
44
45
struct BranchLine
46
{
47
u32 first;
48
u32 second;
49
LineType type;
50
int laneIndex;
51
52
bool operator<(const BranchLine& other) const
53
{
54
return first < other.first;
55
}
56
};
57
58
class DisassemblyEntry
59
{
60
public:
61
virtual ~DisassemblyEntry() { };
62
virtual void recheck() = 0;
63
virtual int getNumLines() = 0;
64
virtual int getLineNum(u32 address, bool findStart) = 0;
65
virtual u32 getLineAddress(int line) = 0;
66
virtual u32 getTotalSize() = 0;
67
virtual bool disassemble(u32 address, DisassemblyLineInfo& dest, bool insertSymbols, DebugInterface *cpuDebug) = 0;
68
virtual void getBranchLines(u32 start, u32 size, std::vector<BranchLine>& dest) { };
69
};
70
71
class DisassemblyFunction: public DisassemblyEntry
72
{
73
public:
74
DisassemblyFunction(u32 _address, u32 _size);
75
~DisassemblyFunction();
76
void recheck() override;
77
int getNumLines() override;
78
int getLineNum(u32 address, bool findStart) override;
79
u32 getLineAddress(int line) override;
80
u32 getTotalSize() override { return size; };
81
bool disassemble(u32 address, DisassemblyLineInfo& dest, bool insertSymbols, DebugInterface *cpuDebug) override;
82
void getBranchLines(u32 start, u32 size, std::vector<BranchLine>& dest) override;
83
84
private:
85
void generateBranchLines();
86
void load();
87
void clear();
88
void addOpcodeSequence(u32 start, u32 end);
89
90
u32 address;
91
u32 size;
92
HashType hash;
93
std::vector<BranchLine> lines;
94
std::map<u32,DisassemblyEntry*> entries;
95
std::vector<u32> lineAddresses;
96
std::recursive_mutex lock_;
97
};
98
99
class DisassemblyOpcode: public DisassemblyEntry
100
{
101
public:
102
DisassemblyOpcode(u32 _address, int _num): address(_address), num(_num) { }
103
void recheck() override { };
104
int getNumLines() override { return num; };
105
int getLineNum(u32 address, bool findStart) override { return (address - this->address) / 4; };
106
u32 getLineAddress(int line) override { return address + line * 4; };
107
u32 getTotalSize() override { return num * 4; };
108
bool disassemble(u32 address, DisassemblyLineInfo& dest, bool insertSymbols, DebugInterface *cpuDebug) override;
109
void getBranchLines(u32 start, u32 size, std::vector<BranchLine>& dest) override;
110
111
private:
112
u32 address;
113
int num;
114
};
115
116
117
class DisassemblyMacro: public DisassemblyEntry
118
{
119
public:
120
DisassemblyMacro(u32 _address): address(_address) { }
121
122
void setMacroLi(u32 _immediate, u8 _rt);
123
void setMacroMemory(const std::string &_name, u32 _immediate, u8 _rt, int _dataSize);
124
125
void recheck() override { };
126
int getNumLines() override { return 1; };
127
int getLineNum(u32 address, bool findStart) override { return 0; };
128
u32 getLineAddress(int line) override { return address; };
129
u32 getTotalSize() override { return numOpcodes * 4; };
130
bool disassemble(u32 address, DisassemblyLineInfo& dest, bool insertSymbols, DebugInterface *cpuDebug) override;
131
private:
132
enum MacroType { MACRO_LI, MACRO_MEMORYIMM };
133
134
MacroType type;
135
std::string name;
136
u32 immediate;
137
u32 address;
138
u32 numOpcodes;
139
u8 rt;
140
int dataSize;
141
};
142
143
144
class DisassemblyData: public DisassemblyEntry
145
{
146
public:
147
DisassemblyData(u32 _address, u32 _size, DataType _type);
148
149
void recheck() override;
150
int getNumLines() override { return (int)lines.size(); };
151
int getLineNum(u32 address, bool findStart) override;
152
u32 getLineAddress(int line) override { return lineAddresses[line]; };
153
u32 getTotalSize() override { return size; };
154
bool disassemble(u32 address, DisassemblyLineInfo& dest, bool insertSymbols, DebugInterface *cpuDebug) override;
155
156
private:
157
void createLines();
158
159
struct DataEntry
160
{
161
std::string text;
162
u32 size;
163
int lineNum;
164
};
165
166
u32 address;
167
u32 size;
168
HashType hash;
169
DataType type;
170
std::map<u32,DataEntry> lines;
171
std::vector<u32> lineAddresses;
172
std::recursive_mutex lock_;
173
};
174
175
class DisassemblyComment: public DisassemblyEntry
176
{
177
public:
178
DisassemblyComment(u32 _address, u32 _size, std::string name, std::string param);
179
180
void recheck() override { };
181
int getNumLines() override { return 1; };
182
int getLineNum(u32 address, bool findStart) override { return 0; };
183
u32 getLineAddress(int line) override { return address; };
184
u32 getTotalSize() override { return size; };
185
bool disassemble(u32 address, DisassemblyLineInfo& dest, bool insertSymbols, DebugInterface *cpuDebug) override;
186
187
private:
188
u32 address;
189
u32 size;
190
std::string name;
191
std::string param;
192
};
193
194
class DebugInterface;
195
196
class DisassemblyManager
197
{
198
public:
199
~DisassemblyManager();
200
201
void clear();
202
203
void setCpu(DebugInterface* _cpu) { cpu = _cpu; };
204
void setMaxParamChars(int num) { maxParamChars = num; clear(); };
205
void getLine(u32 address, bool insertSymbols, DisassemblyLineInfo &dest, DebugInterface *cpuDebug = nullptr);
206
void analyze(u32 address, u32 size);
207
std::vector<BranchLine> getBranchLines(u32 start, u32 size);
208
209
u32 getStartAddress(u32 address);
210
u32 getNthPreviousAddress(u32 address, int n = 1);
211
u32 getNthNextAddress(u32 address, int n = 1);
212
213
static DebugInterface* getCpu() { return cpu; };
214
static int getMaxParamChars() { return maxParamChars; };
215
private:
216
static std::map<u32,DisassemblyEntry*> entries;
217
static std::recursive_mutex entriesLock_;
218
static DebugInterface* cpu;
219
static int maxParamChars;
220
};
221
222
bool isInInterval(u32 start, u32 size, u32 value);
223
bool IsLikelyStringAt(uint32_t addr);
224
225