dolphin/Source/Core/DolphinQt/Debugger/CodeWidget.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

598 lines
17 KiB
C++
Raw Normal View History

2018-02-14 15:25:01 -07:00
// Copyright 2018 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2018-02-14 15:25:01 -07:00
2018-07-06 16:40:15 -06:00
#include "DolphinQt/Debugger/CodeWidget.h"
2018-02-14 15:25:01 -07:00
#include <chrono>
#include <fmt/format.h>
2018-02-14 15:25:01 -07:00
#include <QGridLayout>
#include <QGroupBox>
#include <QGuiApplication>
#include <QLabel>
2018-02-14 15:25:01 -07:00
#include <QLineEdit>
#include <QListWidget>
#include <QPushButton>
2018-02-14 15:25:01 -07:00
#include <QSplitter>
#include <QStyleHints>
2018-02-14 15:25:01 -07:00
#include <QTableWidget>
#include <QWidget>
#include "Common/Event.h"
#include "Core/Core.h"
#include "Core/Debugger/Debugger_SymbolMap.h"
#include "Core/HW/CPU.h"
#include "Core/PowerPC/MMU.h"
2018-02-14 15:25:01 -07:00
#include "Core/PowerPC/PPCSymbolDB.h"
#include "Core/PowerPC/PowerPC.h"
2023-01-09 21:14:32 -07:00
#include "Core/System.h"
2018-07-06 16:40:15 -06:00
#include "DolphinQt/Host.h"
#include "DolphinQt/Settings.h"
2018-02-14 15:25:01 -07:00
static const QString BOX_SPLITTER_STYLESHEET = QStringLiteral(
"QSplitter::handle { border-top: 1px dashed black; width: 1px; margin-left: 10px; "
"margin-right: 10px; }");
2023-03-28 15:52:36 -06:00
CodeWidget::CodeWidget(QWidget* parent) : QDockWidget(parent), m_system(Core::System::GetInstance())
2018-02-14 15:25:01 -07:00
{
setWindowTitle(tr("Code"));
2018-04-19 03:32:00 -06:00
setObjectName(QStringLiteral("code"));
setHidden(!Settings::Instance().IsCodeVisible() || !Settings::Instance().IsDebugModeEnabled());
2018-02-14 15:25:01 -07:00
setAllowedAreas(Qt::AllDockWidgetAreas);
CreateWidgets();
auto& settings = Settings::GetQSettings();
2018-02-14 15:25:01 -07:00
restoreGeometry(settings.value(QStringLiteral("codewidget/geometry")).toByteArray());
// macOS: setHidden() needs to be evaluated before setFloating() for proper window presentation
// according to Settings
2018-02-14 15:25:01 -07:00
setFloating(settings.value(QStringLiteral("codewidget/floating")).toBool());
connect(&Settings::Instance(), &Settings::CodeVisibilityChanged, this,
2018-02-14 15:25:01 -07:00
[this](bool visible) { setHidden(!visible); });
connect(Host::GetInstance(), &Host::UpdateDisasmDialog, this, [this] {
if (Core::GetState() == Core::State::Paused)
2023-03-28 15:52:36 -06:00
SetAddress(m_system.GetPPCState().pc, CodeViewWidget::SetAddressUpdate::WithoutUpdate);
Update();
});
connect(Host::GetInstance(), &Host::NotifyMapLoaded, this, &CodeWidget::UpdateSymbols);
connect(&Settings::Instance(), &Settings::DebugModeToggled, this,
2018-02-14 15:25:01 -07:00
[this](bool enabled) { setHidden(!enabled || !Settings::Instance().IsCodeVisible()); });
connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, &CodeWidget::Update);
ConnectWidgets();
m_code_splitter->restoreState(
settings.value(QStringLiteral("codewidget/codesplitter")).toByteArray());
m_box_splitter->restoreState(
settings.value(QStringLiteral("codewidget/boxsplitter")).toByteArray());
}
CodeWidget::~CodeWidget()
{
auto& settings = Settings::GetQSettings();
2018-02-14 15:25:01 -07:00
settings.setValue(QStringLiteral("codewidget/geometry"), saveGeometry());
settings.setValue(QStringLiteral("codewidget/floating"), isFloating());
settings.setValue(QStringLiteral("codewidget/codesplitter"), m_code_splitter->saveState());
2020-03-20 13:51:26 -06:00
settings.setValue(QStringLiteral("codewidget/boxsplitter"), m_box_splitter->saveState());
2018-02-14 15:25:01 -07:00
}
void CodeWidget::closeEvent(QCloseEvent*)
{
Settings::Instance().SetCodeVisible(false);
}
void CodeWidget::showEvent(QShowEvent* event)
{
Update();
}
2018-02-14 15:25:01 -07:00
void CodeWidget::CreateWidgets()
{
auto* layout = new QGridLayout;
2018-08-02 13:50:23 -06:00
layout->setContentsMargins(2, 2, 2, 2);
layout->setSpacing(0);
2018-02-14 15:25:01 -07:00
m_search_address = new QLineEdit;
m_code_diff = new QPushButton(tr("Diff"));
2018-02-14 15:25:01 -07:00
m_code_view = new CodeViewWidget;
m_search_address->setPlaceholderText(tr("Search Address"));
m_box_splitter = new QSplitter(Qt::Vertical);
m_box_splitter->setStyleSheet(BOX_SPLITTER_STYLESHEET);
auto add_search_line_edit = [this](const QString& name, QListWidget* list_widget) {
auto* widget = new QWidget;
2023-02-02 17:12:45 -07:00
auto* line_layout = new QGridLayout;
auto* label = new QLabel(name);
auto* search_line_edit = new QLineEdit;
2023-02-02 17:12:45 -07:00
widget->setLayout(line_layout);
line_layout->addWidget(label, 0, 0);
line_layout->addWidget(search_line_edit, 0, 1);
line_layout->addWidget(list_widget, 1, 0, -1, -1);
m_box_splitter->addWidget(widget);
return search_line_edit;
};
2018-02-14 15:25:01 -07:00
// Callstack
m_callstack_list = new QListWidget;
m_search_callstack = add_search_line_edit(tr("Callstack"), m_callstack_list);
2018-02-14 15:25:01 -07:00
// Symbols
m_symbols_list = new QListWidget;
m_search_symbols = add_search_line_edit(tr("Symbols"), m_symbols_list);
2018-02-14 15:25:01 -07:00
// Function calls
m_function_calls_list = new QListWidget;
m_search_calls = add_search_line_edit(tr("Calls"), m_function_calls_list);
2018-02-14 15:25:01 -07:00
// Function callers
m_function_callers_list = new QListWidget;
m_search_callers = add_search_line_edit(tr("Callers"), m_function_callers_list);
2018-02-14 15:25:01 -07:00
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_code_diff, 0, 2);
2018-02-14 15:25:01 -07:00
layout->addWidget(m_code_splitter, 1, 0, -1, -1);
QWidget* widget = new QWidget(this);
widget->setLayout(layout);
setWidget(widget);
}
void CodeWidget::ConnectWidgets()
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
connect(QGuiApplication::styleHints(), &QStyleHints::colorSchemeChanged, this,
[this](Qt::ColorScheme colorScheme) {
m_box_splitter->setStyleSheet(BOX_SPLITTER_STYLESHEET);
});
#endif
2018-02-14 15:25:01 -07:00
connect(m_search_address, &QLineEdit::textChanged, this, &CodeWidget::OnSearchAddress);
connect(m_search_address, &QLineEdit::returnPressed, this, &CodeWidget::OnSearchAddress);
2018-02-14 15:25:01 -07:00
connect(m_search_symbols, &QLineEdit::textChanged, this, &CodeWidget::OnSearchSymbols);
connect(m_search_calls, &QLineEdit::textChanged, this, [this]() {
const Common::Symbol* symbol = g_symbolDB.GetSymbolFromAddr(m_code_view->GetAddress());
if (symbol)
UpdateFunctionCalls(symbol);
});
connect(m_search_callers, &QLineEdit::textChanged, this, [this]() {
const Common::Symbol* symbol = g_symbolDB.GetSymbolFromAddr(m_code_view->GetAddress());
if (symbol)
UpdateFunctionCallers(symbol);
});
connect(m_search_callstack, &QLineEdit::textChanged, this, &CodeWidget::UpdateCallstack);
connect(m_code_diff, &QPushButton::pressed, this, &CodeWidget::OnDiff);
2018-02-14 15:25:01 -07:00
connect(m_symbols_list, &QListWidget::itemPressed, this, &CodeWidget::OnSelectSymbol);
connect(m_callstack_list, &QListWidget::itemPressed, this, &CodeWidget::OnSelectCallstack);
connect(m_function_calls_list, &QListWidget::itemPressed, this,
2018-02-14 15:25:01 -07:00
&CodeWidget::OnSelectFunctionCalls);
connect(m_function_callers_list, &QListWidget::itemPressed, this,
2018-02-14 15:25:01 -07:00
&CodeWidget::OnSelectFunctionCallers);
connect(m_code_view, &CodeViewWidget::SymbolsChanged, this, [this]() {
UpdateCallstack();
UpdateSymbols();
const Common::Symbol* symbol = g_symbolDB.GetSymbolFromAddr(m_code_view->GetAddress());
if (symbol)
{
UpdateFunctionCalls(symbol);
UpdateFunctionCallers(symbol);
}
});
2018-02-14 15:25:01 -07:00
connect(m_code_view, &CodeViewWidget::BreakpointsChanged, this,
[this] { emit BreakpointsChanged(); });
connect(m_code_view, &CodeViewWidget::UpdateCodeWidget, this, &CodeWidget::Update);
2018-04-09 07:31:20 -06:00
connect(m_code_view, &CodeViewWidget::RequestPPCComparison, this,
&CodeWidget::RequestPPCComparison);
connect(m_code_view, &CodeViewWidget::ShowMemory, this, &CodeWidget::ShowMemory);
2018-02-14 15:25:01 -07:00
}
void CodeWidget::OnDiff()
{
if (!m_diff_dialog)
m_diff_dialog = new CodeDiffDialog(this);
m_diff_dialog->setWindowFlag(Qt::WindowMinimizeButtonHint);
m_diff_dialog->show();
m_diff_dialog->raise();
m_diff_dialog->activateWindow();
}
2018-02-14 15:25:01 -07:00
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, CodeViewWidget::SetAddressUpdate::WithUpdate);
2018-02-14 15:25:01 -07:00
Update();
2018-06-30 15:47:16 -06:00
m_search_address->setFocus();
2018-02-14 15:25:01 -07:00
}
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;
const u32 address = items[0]->data(Qt::UserRole).toUInt();
const Common::Symbol* symbol = g_symbolDB.GetSymbolFromAddr(address);
2018-02-14 15:25:01 -07:00
m_code_view->SetAddress(address, CodeViewWidget::SetAddressUpdate::WithUpdate);
2018-02-14 15:25:01 -07:00
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(),
CodeViewWidget::SetAddressUpdate::WithUpdate);
2018-02-14 15:25:01 -07:00
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(),
CodeViewWidget::SetAddressUpdate::WithUpdate);
2018-02-14 15:25:01 -07:00
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(),
CodeViewWidget::SetAddressUpdate::WithUpdate);
2018-02-14 15:25:01 -07:00
Update();
}
void CodeWidget::SetAddress(u32 address, CodeViewWidget::SetAddressUpdate update)
{
m_code_view->SetAddress(address, update);
if (update == CodeViewWidget::SetAddressUpdate::WithUpdate ||
update == CodeViewWidget::SetAddressUpdate::WithDetailedUpdate)
{
Settings::Instance().SetCodeVisible(true);
raise();
m_code_view->setFocus();
}
}
2018-02-14 15:25:01 -07:00
void CodeWidget::Update()
{
if (!isVisible())
return;
const Common::Symbol* symbol = g_symbolDB.GetSymbolFromAddr(m_code_view->GetAddress());
2018-02-14 15:25:01 -07:00
UpdateCallstack();
m_code_view->Update();
m_code_view->setFocus();
2018-02-14 15:25:01 -07:00
if (!symbol)
return;
UpdateFunctionCalls(symbol);
UpdateFunctionCallers(symbol);
}
void CodeWidget::UpdateCallstack()
{
m_callstack_list->clear();
if (Core::GetState() != Core::State::Paused)
return;
2018-02-14 15:25:01 -07:00
std::vector<Dolphin_Debugger::CallstackEntry> stack;
2023-03-28 15:52:36 -06:00
const bool success = [this, &stack] {
Core::CPUThreadGuard guard(m_system);
return Dolphin_Debugger::GetCallstack(m_system, guard, stack);
}();
2018-02-14 15:25:01 -07:00
if (!success)
{
m_callstack_list->addItem(tr("Invalid callstack"));
return;
}
const QString filter = m_search_callstack->text();
2018-02-14 15:25:01 -07:00
for (const auto& frame : stack)
{
const QString name = QString::fromStdString(frame.Name.substr(0, frame.Name.length() - 1));
if (name.toUpper().indexOf(filter.toUpper()) == -1)
continue;
2018-02-14 15:25:01 -07:00
auto* item = new QListWidgetItem(name);
item->setData(Qt::UserRole, frame.vAddress);
2018-02-14 15:25:01 -07:00
m_callstack_list->addItem(item);
}
}
void CodeWidget::UpdateSymbols()
{
const QString selection = m_symbols_list->selectedItems().isEmpty() ?
QString{} :
m_symbols_list->selectedItems()[0]->text();
2018-02-14 15:25:01 -07:00
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 != Common::Symbol::Type::Function)
2018-02-14 15:25:01 -07:00
item->setFlags(Qt::NoItemFlags);
item->setData(Qt::UserRole, symbol.second.address);
if (name.toUpper().indexOf(m_symbol_filter.toUpper()) != -1)
2018-02-14 15:25:01 -07:00
m_symbols_list->addItem(item);
}
m_symbols_list->sortItems();
}
void CodeWidget::UpdateFunctionCalls(const Common::Symbol* symbol)
2018-02-14 15:25:01 -07:00
{
m_function_calls_list->clear();
const QString filter = m_search_calls->text();
2018-02-14 15:25:01 -07:00
for (const auto& call : symbol->calls)
{
const u32 addr = call.function;
const Common::Symbol* call_symbol = g_symbolDB.GetSymbolFromAddr(addr);
2018-02-14 15:25:01 -07:00
if (call_symbol)
{
const QString name =
QString::fromStdString(fmt::format("> {} ({:08x})", call_symbol->name, addr));
2018-02-14 15:25:01 -07:00
if (name.toUpper().indexOf(filter.toUpper()) == -1)
continue;
auto* item = new QListWidgetItem(name);
item->setData(Qt::UserRole, addr);
2018-02-14 15:25:01 -07:00
m_function_calls_list->addItem(item);
}
}
}
void CodeWidget::UpdateFunctionCallers(const Common::Symbol* symbol)
2018-02-14 15:25:01 -07:00
{
m_function_callers_list->clear();
const QString filter = m_search_callers->text();
2018-02-14 15:25:01 -07:00
for (const auto& caller : symbol->callers)
{
const u32 addr = caller.call_address;
const Common::Symbol* caller_symbol = g_symbolDB.GetSymbolFromAddr(addr);
2018-02-14 15:25:01 -07:00
if (caller_symbol)
{
const QString name =
QString::fromStdString(fmt::format("< {} ({:08x})", caller_symbol->name, addr));
2018-02-14 15:25:01 -07:00
if (name.toUpper().indexOf(filter.toUpper()) == -1)
continue;
auto* item = new QListWidgetItem(name);
item->setData(Qt::UserRole, addr);
2018-02-14 15:25:01 -07:00
m_function_callers_list->addItem(item);
}
}
}
void CodeWidget::Step()
{
2023-03-28 15:52:36 -06:00
auto& cpu = m_system.GetCPU();
if (!cpu.IsStepping())
2018-02-14 15:25:01 -07:00
return;
Common::Event sync_event;
auto& power_pc = m_system.GetPowerPC();
PowerPC::CoreMode old_mode = power_pc.GetMode();
power_pc.SetMode(PowerPC::CoreMode::Interpreter);
power_pc.GetBreakPoints().ClearAllTemporary();
cpu.StepOpcode(&sync_event);
2018-02-14 15:25:01 -07:00
sync_event.WaitFor(std::chrono::milliseconds(20));
power_pc.SetMode(old_mode);
2018-02-14 15:25:01 -07:00
Core::DisplayMessage(tr("Step successful!").toStdString(), 2000);
// Will get a UpdateDisasmDialog(), don't update the GUI here.
2018-02-14 15:25:01 -07:00
}
void CodeWidget::StepOver()
{
2023-03-28 15:52:36 -06:00
auto& cpu = m_system.GetCPU();
if (!cpu.IsStepping())
2018-02-14 15:25:01 -07:00
return;
const UGeckoInstruction inst = [&] {
2023-03-28 15:52:36 -06:00
Core::CPUThreadGuard guard(m_system);
return PowerPC::MMU::HostRead_Instruction(guard, m_system.GetPPCState().pc);
}();
2018-02-14 15:25:01 -07:00
if (inst.LK)
{
auto& breakpoints = m_system.GetPowerPC().GetBreakPoints();
breakpoints.ClearAllTemporary();
breakpoints.Add(m_system.GetPPCState().pc + 4, true);
cpu.EnableStepping(false);
2018-02-14 15:25:01 -07:00
Core::DisplayMessage(tr("Step over in progress...").toStdString(), 2000);
}
else
{
Step();
}
}
// Returns true on a rfi, blr or on a bclr that evaluates to true.
2023-03-28 15:52:36 -06:00
static bool WillInstructionReturn(Core::System& system, UGeckoInstruction inst)
2018-02-14 15:25:01 -07:00
{
// Is a rfi instruction
if (inst.hex == 0x4C000064u)
return true;
2023-03-28 15:52:36 -06:00
const auto& ppc_state = system.GetPPCState();
bool counter = (inst.BO_2 >> 2 & 1) != 0 || (CTR(ppc_state) != 0) != ((inst.BO_2 >> 1 & 1) != 0);
bool condition = inst.BO_2 >> 4 != 0 || ppc_state.cr.GetBit(inst.BI_2) == (inst.BO_2 >> 3 & 1);
2018-02-14 15:25:01 -07:00
bool isBclr = inst.OPCD_7 == 0b010011 && (inst.hex >> 1 & 0b10000) != 0;
return isBclr && counter && condition && !inst.LK_3;
}
void CodeWidget::StepOut()
{
2023-03-28 15:52:36 -06:00
auto& cpu = m_system.GetCPU();
if (!cpu.IsStepping())
2018-02-14 15:25:01 -07:00
return;
// 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);
auto& power_pc = m_system.GetPowerPC();
auto& ppc_state = power_pc.GetPPCState();
auto& breakpoints = power_pc.GetBreakPoints();
2018-02-14 15:25:01 -07:00
{
2023-03-28 15:52:36 -06:00
Core::CPUThreadGuard guard(m_system);
breakpoints.ClearAllTemporary();
PowerPC::CoreMode old_mode = power_pc.GetMode();
power_pc.SetMode(PowerPC::CoreMode::Interpreter);
2018-02-14 15:25:01 -07:00
// 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.
2023-03-28 15:52:36 -06:00
UGeckoInstruction inst = PowerPC::MMU::HostRead_Instruction(guard, ppc_state.pc);
do
2018-02-14 15:25:01 -07:00
{
2023-03-28 15:52:36 -06:00
if (WillInstructionReturn(m_system, inst))
2018-02-14 15:25:01 -07:00
{
power_pc.SingleStep();
break;
}
2018-02-14 15:25:01 -07:00
if (inst.LK)
{
// Step over branches
2023-03-28 15:52:36 -06:00
u32 next_pc = ppc_state.pc + 4;
do
{
power_pc.SingleStep();
2023-03-28 15:52:36 -06:00
} while (ppc_state.pc != next_pc && clock::now() < timeout &&
!breakpoints.IsAddressBreakPoint(ppc_state.pc));
}
else
{
power_pc.SingleStep();
}
2018-02-14 15:25:01 -07:00
2023-03-28 15:52:36 -06:00
inst = PowerPC::MMU::HostRead_Instruction(guard, ppc_state.pc);
} while (clock::now() < timeout && !breakpoints.IsAddressBreakPoint(ppc_state.pc));
power_pc.SetMode(old_mode);
}
2018-02-14 15:25:01 -07:00
emit Host::GetInstance()->UpdateDisasmDialog();
if (breakpoints.IsAddressBreakPoint(ppc_state.pc))
2018-02-14 15:25:01 -07:00
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);
}
void CodeWidget::Skip()
{
2023-03-28 15:52:36 -06:00
m_system.GetPPCState().pc += 4;
2018-02-14 15:25:01 -07:00
ShowPC();
}
void CodeWidget::ShowPC()
{
2023-03-28 15:52:36 -06:00
m_code_view->SetAddress(m_system.GetPPCState().pc, CodeViewWidget::SetAddressUpdate::WithUpdate);
2018-02-14 15:25:01 -07:00
Update();
}
void CodeWidget::SetPC()
{
2023-03-28 15:52:36 -06:00
m_system.GetPPCState().pc = m_code_view->GetAddress();
2018-02-14 15:25:01 -07:00
Update();
}
void CodeWidget::ToggleBreakpoint()
{
m_code_view->ToggleBreakpoint();
}
void CodeWidget::AddBreakpoint()
{
m_code_view->AddBreakpoint();
}