-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
CodeWidget.cpp
484 lines (381 loc) · 12.7 KB
/
CodeWidget.cpp
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// Copyright 2018 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include "DolphinQt2/Debugger/CodeWidget.h"
#include <chrono>
#include <QGridLayout>
#include <QGroupBox>
#include <QLineEdit>
#include <QListWidget>
#include <QSettings>
#include <QSplitter>
#include <QTableWidget>
#include <QWidget>
#include "Common/Event.h"
#include "Common/StringUtil.h"
#include "Core/Core.h"
#include "Core/Debugger/Debugger_SymbolMap.h"
#include "Core/HW/CPU.h"
#include "Core/PowerPC/PPCSymbolDB.h"
#include "Core/PowerPC/PowerPC.h"
#include "DolphinQt2/Debugger/CodeViewWidget.h"
#include "DolphinQt2/Settings.h"
CodeWidget::CodeWidget(QWidget* parent) : QDockWidget(parent)
{
setWindowTitle(tr("Code"));
setAllowedAreas(Qt::AllDockWidgetAreas);
QSettings settings;
restoreGeometry(settings.value(QStringLiteral("codewidget/geometry")).toByteArray());
setFloating(settings.value(QStringLiteral("codewidget/floating")).toBool());
connect(&Settings::Instance(), &Settings::CodeVisibilityChanged,
[this](bool visible) { setHidden(!visible); });
connect(&Settings::Instance(), &Settings::DebugModeToggled,
[this](bool enabled) { setHidden(!enabled || !Settings::Instance().IsCodeVisible()); });
connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, &CodeWidget::Update);
setHidden(!Settings::Instance().IsCodeVisible() || !Settings::Instance().IsDebugModeEnabled());
CreateWidgets();
ConnectWidgets();
m_code_splitter->restoreState(
settings.value(QStringLiteral("codewidget/codesplitter")).toByteArray());
m_box_splitter->restoreState(
settings.value(QStringLiteral("codewidget/boxsplitter")).toByteArray());
Update();
}
CodeWidget::~CodeWidget()
{
QSettings settings;
settings.setValue(QStringLiteral("codewidget/geometry"), saveGeometry());
settings.setValue(QStringLiteral("codewidget/floating"), isFloating());
settings.setValue(QStringLiteral("codewidget/codesplitter"), m_code_splitter->saveState());
settings.setValue(QStringLiteral("codewidget/boxplitter"), m_box_splitter->saveState());
}
void CodeWidget::closeEvent(QCloseEvent*)
{
Settings::Instance().SetCodeVisible(false);
}
void CodeWidget::CreateWidgets()
{
auto* layout = new QGridLayout;
m_search_address = new QLineEdit;
m_search_symbols = new QLineEdit;
m_code_view = new CodeViewWidget;
m_search_address->setPlaceholderText(tr("Search Address"));
m_search_symbols->setPlaceholderText(tr("Filter Symbols"));
// Callstack
auto* callstack_box = new QGroupBox(tr("Callstack"));
auto* callstack_layout = new QVBoxLayout;
m_callstack_list = new QListWidget;
callstack_box->setLayout(callstack_layout);
callstack_layout->addWidget(m_callstack_list);
// Symbols
auto* symbols_box = new QGroupBox(tr("Symbols"));
auto* symbols_layout = new QVBoxLayout;
m_symbols_list = new QListWidget;
symbols_box->setLayout(symbols_layout);
symbols_layout->addWidget(m_symbols_list);
// Function calls
auto* function_calls_box = new QGroupBox(tr("Function calls"));
auto* function_calls_layout = new QVBoxLayout;
m_function_calls_list = new QListWidget;
function_calls_box->setLayout(function_calls_layout);
function_calls_layout->addWidget(m_function_calls_list);
// Function callers
auto* function_callers_box = new QGroupBox(tr("Function callers"));
auto* function_callers_layout = new QVBoxLayout;
m_function_callers_list = new QListWidget;
function_callers_box->setLayout(function_callers_layout);
function_callers_layout->addWidget(m_function_callers_list);
m_box_splitter = new QSplitter(Qt::Vertical);
m_box_splitter->addWidget(callstack_box);
m_box_splitter->addWidget(symbols_box);
m_box_splitter->addWidget(function_calls_box);
m_box_splitter->addWidget(function_callers_box);
m_code_splitter = new QSplitter(Qt::Horizontal);
m_code_splitter->addWidget(m_box_splitter);
m_code_splitter->addWidget(m_code_view);
layout->addWidget(m_search_address, 0, 0);
layout->addWidget(m_search_symbols, 0, 1);
layout->addWidget(m_code_splitter, 1, 0, -1, -1);
QWidget* widget = new QWidget(this);
widget->setLayout(layout);
setWidget(widget);
}
void CodeWidget::ConnectWidgets()
{
connect(m_search_address, &QLineEdit::textChanged, this, &CodeWidget::OnSearchAddress);
connect(m_search_symbols, &QLineEdit::textChanged, this, &CodeWidget::OnSearchSymbols);
connect(m_symbols_list, &QListWidget::itemSelectionChanged, this, &CodeWidget::OnSelectSymbol);
connect(m_callstack_list, &QListWidget::itemSelectionChanged, this,
&CodeWidget::OnSelectCallstack);
connect(m_function_calls_list, &QListWidget::itemSelectionChanged, this,
&CodeWidget::OnSelectFunctionCalls);
connect(m_function_callers_list, &QListWidget::itemSelectionChanged, this,
&CodeWidget::OnSelectFunctionCallers);
connect(m_code_view, &CodeViewWidget::SymbolsChanged, this, &CodeWidget::UpdateSymbols);
connect(m_code_view, &CodeViewWidget::BreakpointsChanged, this,
[this] { emit BreakpointsChanged(); });
}
void CodeWidget::OnSearchAddress()
{
bool good = true;
u32 address = m_search_address->text().toUInt(&good, 16);
QPalette palette;
QFont font;
if (!good && !m_search_address->text().isEmpty())
{
font.setBold(true);
palette.setColor(QPalette::Text, Qt::red);
}
m_search_address->setPalette(palette);
m_search_address->setFont(font);
if (good)
m_code_view->SetAddress(address);
Update();
}
void CodeWidget::OnSearchSymbols()
{
m_symbol_filter = m_search_symbols->text();
UpdateSymbols();
}
void CodeWidget::OnSelectSymbol()
{
const auto items = m_symbols_list->selectedItems();
if (items.isEmpty())
return;
Symbol* symbol = g_symbolDB.GetSymbolFromAddr(items[0]->data(Qt::UserRole).toUInt());
m_code_view->SetAddress(items[0]->data(Qt::UserRole).toUInt());
UpdateCallstack();
UpdateFunctionCalls(symbol);
UpdateFunctionCallers(symbol);
m_code_view->setFocus();
}
void CodeWidget::OnSelectCallstack()
{
const auto items = m_callstack_list->selectedItems();
if (items.isEmpty())
return;
m_code_view->SetAddress(items[0]->data(Qt::UserRole).toUInt());
Update();
}
void CodeWidget::OnSelectFunctionCalls()
{
const auto items = m_function_calls_list->selectedItems();
if (items.isEmpty())
return;
m_code_view->SetAddress(items[0]->data(Qt::UserRole).toUInt());
Update();
}
void CodeWidget::OnSelectFunctionCallers()
{
const auto items = m_function_callers_list->selectedItems();
if (items.isEmpty())
return;
m_code_view->SetAddress(items[0]->data(Qt::UserRole).toUInt());
Update();
}
void CodeWidget::Update()
{
Symbol* symbol = g_symbolDB.GetSymbolFromAddr(m_code_view->GetAddress());
UpdateCallstack();
UpdateSymbols();
if (!symbol)
return;
UpdateFunctionCalls(symbol);
UpdateFunctionCallers(symbol);
m_code_view->Update();
m_code_view->setFocus();
}
void CodeWidget::UpdateCallstack()
{
if (Core::GetState() == Core::State::Starting)
return;
m_callstack_list->clear();
std::vector<Dolphin_Debugger::CallstackEntry> stack;
bool success = Dolphin_Debugger::GetCallstack(stack);
if (!success)
{
m_callstack_list->addItem(tr("Invalid callstack"));
return;
}
for (const auto& frame : stack)
{
auto* item =
new QListWidgetItem(QString::fromStdString(frame.Name.substr(0, frame.Name.length() - 1)));
item->setData(Qt::UserRole, frame.vAddress);
m_callstack_list->addItem(item);
}
}
void CodeWidget::UpdateSymbols()
{
QString selection = m_symbols_list->selectedItems().isEmpty() ?
QStringLiteral("") :
m_symbols_list->selectedItems()[0]->text();
m_symbols_list->clear();
for (const auto& symbol : g_symbolDB.Symbols())
{
QString name = QString::fromStdString(symbol.second.name);
auto* item = new QListWidgetItem(name);
if (name == selection)
item->setSelected(true);
// Disable non-function symbols as you can't do anything with them.
if (symbol.second.type != Symbol::Type::Function)
item->setFlags(Qt::NoItemFlags);
item->setData(Qt::UserRole, symbol.second.address);
if (name.indexOf(m_symbol_filter) != -1)
m_symbols_list->addItem(item);
}
m_symbols_list->sortItems();
}
void CodeWidget::UpdateFunctionCalls(Symbol* symbol)
{
m_function_calls_list->clear();
for (const auto& call : symbol->calls)
{
u32 addr = call.function;
Symbol* call_symbol = g_symbolDB.GetSymbolFromAddr(addr);
if (call_symbol)
{
auto* item = new QListWidgetItem(QString::fromStdString(
StringFromFormat("> %s (%08x)", call_symbol->name.c_str(), addr).c_str()));
item->setData(Qt::UserRole, addr);
m_function_calls_list->addItem(item);
}
}
}
void CodeWidget::UpdateFunctionCallers(Symbol* symbol)
{
m_function_callers_list->clear();
for (const auto& caller : symbol->callers)
{
u32 addr = caller.callAddress;
Symbol* caller_symbol = g_symbolDB.GetSymbolFromAddr(addr);
if (caller_symbol)
{
auto* item = new QListWidgetItem(QString::fromStdString(
StringFromFormat("< %s (%08x)", caller_symbol->name.c_str(), addr).c_str()));
item->setData(Qt::UserRole, addr);
m_function_callers_list->addItem(item);
}
}
}
void CodeWidget::Step()
{
if (!CPU::IsStepping())
return;
Common::Event sync_event;
PowerPC::CoreMode old_mode = PowerPC::GetMode();
PowerPC::SetMode(PowerPC::CoreMode::Interpreter);
PowerPC::breakpoints.ClearAllTemporary();
CPU::StepOpcode(&sync_event);
sync_event.WaitFor(std::chrono::milliseconds(20));
PowerPC::SetMode(old_mode);
Core::DisplayMessage(tr("Step successful!").toStdString(), 2000);
Core::SetState(Core::State::Paused);
m_code_view->SetAddress(PC);
Update();
}
void CodeWidget::StepOver()
{
if (!CPU::IsStepping())
return;
UGeckoInstruction inst = PowerPC::HostRead_Instruction(PC);
if (inst.LK)
{
PowerPC::breakpoints.ClearAllTemporary();
PowerPC::breakpoints.Add(PC + 4, true);
CPU::EnableStepping(false);
Core::DisplayMessage(tr("Step over in progress...").toStdString(), 2000);
}
else
{
Step();
}
Core::SetState(Core::State::Paused);
m_code_view->SetAddress(PC);
Update();
}
// Returns true on a rfi, blr or on a bclr that evaluates to true.
static bool WillInstructionReturn(UGeckoInstruction inst)
{
// Is a rfi instruction
if (inst.hex == 0x4C000064u)
return true;
bool counter = (inst.BO_2 >> 2 & 1) != 0 || (CTR != 0) != ((inst.BO_2 >> 1 & 1) != 0);
bool condition = inst.BO_2 >> 4 != 0 || GetCRBit(inst.BI_2) == (inst.BO_2 >> 3 & 1);
bool isBclr = inst.OPCD_7 == 0b010011 && (inst.hex >> 1 & 0b10000) != 0;
return isBclr && counter && condition && !inst.LK_3;
}
void CodeWidget::StepOut()
{
if (!CPU::IsStepping())
return;
Core::SetState(Core::State::Running);
CPU::PauseAndLock(true, false);
PowerPC::breakpoints.ClearAllTemporary();
// Keep stepping until the next return instruction or timeout after five seconds
using clock = std::chrono::steady_clock;
clock::time_point timeout = clock::now() + std::chrono::seconds(5);
PowerPC::CoreMode old_mode = PowerPC::GetMode();
PowerPC::SetMode(PowerPC::CoreMode::Interpreter);
// Loop until either the current instruction is a return instruction with no Link flag
// or a breakpoint is detected so it can step at the breakpoint. If the PC is currently
// on a breakpoint, skip it.
UGeckoInstruction inst = PowerPC::HostRead_Instruction(PC);
do
{
if (WillInstructionReturn(inst))
{
PowerPC::SingleStep();
break;
}
if (inst.LK)
{
// Step over branches
u32 next_pc = PC + 4;
do
{
PowerPC::SingleStep();
} while (PC != next_pc && clock::now() < timeout &&
!PowerPC::breakpoints.IsAddressBreakPoint(PC));
}
else
{
PowerPC::SingleStep();
}
inst = PowerPC::HostRead_Instruction(PC);
} while (clock::now() < timeout && !PowerPC::breakpoints.IsAddressBreakPoint(PC));
PowerPC::SetMode(old_mode);
CPU::PauseAndLock(false, false);
if (PowerPC::breakpoints.IsAddressBreakPoint(PC))
Core::DisplayMessage(tr("Breakpoint encountered! Step out aborted.").toStdString(), 2000);
else if (clock::now() >= timeout)
Core::DisplayMessage(tr("Step out timed out!").toStdString(), 2000);
else
Core::DisplayMessage(tr("Step out successful!").toStdString(), 2000);
Core::SetState(Core::State::Paused);
m_code_view->SetAddress(PC);
Update();
}
void CodeWidget::Skip()
{
PC += 4;
ShowPC();
}
void CodeWidget::ShowPC()
{
m_code_view->SetAddress(PC);
Update();
}
void CodeWidget::SetPC()
{
PC = m_code_view->GetAddress();
Update();
}
void CodeWidget::ToggleBreakpoint()
{
m_code_view->ToggleBreakpoint();
}
void CodeWidget::AddBreakpoint()
{
m_code_view->AddBreakpoint();
}