From 0a5f0efe189f2150a26b9b88e577aff25e65f1f1 Mon Sep 17 00:00:00 2001 From: spycrab Date: Wed, 14 Feb 2018 23:25:01 +0100 Subject: [PATCH] Qt/Debugger: Implement "Code" widget --- Source/Core/DolphinQt2/CMakeLists.txt | 3 + .../Config/Mapping/HotkeyDebugging.cpp | 47 ++ .../Config/Mapping/HotkeyDebugging.h | 25 + .../Config/Mapping/MappingWindow.cpp | 2 + .../DolphinQt2/Debugger/BreakpointWidget.h | 3 +- .../DolphinQt2/Debugger/CodeViewWidget.cpp | 544 ++++++++++++++++++ .../Core/DolphinQt2/Debugger/CodeViewWidget.h | 72 +++ .../Core/DolphinQt2/Debugger/CodeWidget.cpp | 481 ++++++++++++++++ Source/Core/DolphinQt2/Debugger/CodeWidget.h | 68 +++ Source/Core/DolphinQt2/DolphinQt2.vcxproj | 13 +- Source/Core/DolphinQt2/Host.cpp | 12 +- Source/Core/DolphinQt2/HotkeyScheduler.cpp | 26 +- Source/Core/DolphinQt2/HotkeyScheduler.h | 11 + Source/Core/DolphinQt2/MainWindow.cpp | 32 +- Source/Core/DolphinQt2/MainWindow.h | 2 + Source/Core/DolphinQt2/MenuBar.cpp | 259 +++++++++ Source/Core/DolphinQt2/MenuBar.h | 24 + Source/Core/DolphinQt2/Settings.cpp | 33 ++ Source/Core/DolphinQt2/Settings.h | 8 + Source/Core/DolphinQt2/ToolBar.cpp | 29 +- Source/Core/DolphinQt2/ToolBar.h | 15 + 21 files changed, 1696 insertions(+), 13 deletions(-) create mode 100644 Source/Core/DolphinQt2/Config/Mapping/HotkeyDebugging.cpp create mode 100644 Source/Core/DolphinQt2/Config/Mapping/HotkeyDebugging.h create mode 100644 Source/Core/DolphinQt2/Debugger/CodeViewWidget.cpp create mode 100644 Source/Core/DolphinQt2/Debugger/CodeViewWidget.h create mode 100644 Source/Core/DolphinQt2/Debugger/CodeWidget.cpp create mode 100644 Source/Core/DolphinQt2/Debugger/CodeWidget.h diff --git a/Source/Core/DolphinQt2/CMakeLists.txt b/Source/Core/DolphinQt2/CMakeLists.txt index 42c1fecb85..3f1bd87cfb 100644 --- a/Source/Core/DolphinQt2/CMakeLists.txt +++ b/Source/Core/DolphinQt2/CMakeLists.txt @@ -56,6 +56,7 @@ set(SRCS Config/Mapping/GCPadEmu.cpp Config/Mapping/GCPadWiiUConfigDialog.cpp Config/Mapping/Hotkey3D.cpp + Config/Mapping/HotkeyDebugging.cpp Config/Mapping/HotkeyGeneral.cpp Config/Mapping/HotkeyGraphics.cpp Config/Mapping/HotkeyStates.cpp @@ -75,6 +76,8 @@ set(SRCS Config/PropertiesDialog.cpp Config/SettingsWindow.cpp Debugger/BreakpointWidget.cpp + Debugger/CodeViewWidget.cpp + Debugger/CodeWidget.cpp Debugger/NewBreakpointDialog.cpp Debugger/RegisterColumn.cpp Debugger/RegisterWidget.cpp diff --git a/Source/Core/DolphinQt2/Config/Mapping/HotkeyDebugging.cpp b/Source/Core/DolphinQt2/Config/Mapping/HotkeyDebugging.cpp new file mode 100644 index 0000000000..d7a86625ab --- /dev/null +++ b/Source/Core/DolphinQt2/Config/Mapping/HotkeyDebugging.cpp @@ -0,0 +1,47 @@ +// Copyright 2018 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include "DolphinQt2/Config/Mapping/HotkeyDebugging.h" + +#include +#include +#include + +#include "Core/HotkeyManager.h" + +HotkeyDebugging::HotkeyDebugging(MappingWindow* window) : MappingWidget(window) +{ + CreateMainLayout(); +} + +void HotkeyDebugging::CreateMainLayout() +{ + m_main_layout = new QHBoxLayout(); + + m_main_layout->addWidget( + CreateGroupBox(tr("Stepping"), HotkeyManagerEmu::GetHotkeyGroup(HKGP_STEPPING))); + + auto* vbox = new QVBoxLayout(); + vbox->addWidget(CreateGroupBox(tr("Program Counter"), HotkeyManagerEmu::GetHotkeyGroup(HKGP_PC))); + vbox->addWidget( + CreateGroupBox(tr("Breakpoint"), HotkeyManagerEmu::GetHotkeyGroup(HKGP_BREAKPOINT))); + m_main_layout->addItem(vbox); + + setLayout(m_main_layout); +} + +InputConfig* HotkeyDebugging::GetConfig() +{ + return HotkeyManagerEmu::GetConfig(); +} + +void HotkeyDebugging::LoadSettings() +{ + HotkeyManagerEmu::LoadConfig(); +} + +void HotkeyDebugging::SaveSettings() +{ + HotkeyManagerEmu::GetConfig()->SaveConfig(); +} diff --git a/Source/Core/DolphinQt2/Config/Mapping/HotkeyDebugging.h b/Source/Core/DolphinQt2/Config/Mapping/HotkeyDebugging.h new file mode 100644 index 0000000000..aecc5e546e --- /dev/null +++ b/Source/Core/DolphinQt2/Config/Mapping/HotkeyDebugging.h @@ -0,0 +1,25 @@ +// Copyright 2018 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#pragma once + +#include "DolphinQt2/Config/Mapping/MappingWidget.h" + +class QHBoxLayout; + +class HotkeyDebugging final : public MappingWidget +{ +public: + explicit HotkeyDebugging(MappingWindow* window); + + InputConfig* GetConfig() override; + +private: + void LoadSettings() override; + void SaveSettings() override; + void CreateMainLayout(); + + // Main + QHBoxLayout* m_main_layout; +}; diff --git a/Source/Core/DolphinQt2/Config/Mapping/MappingWindow.cpp b/Source/Core/DolphinQt2/Config/Mapping/MappingWindow.cpp index 3ceaaa5b87..64c415658c 100644 --- a/Source/Core/DolphinQt2/Config/Mapping/MappingWindow.cpp +++ b/Source/Core/DolphinQt2/Config/Mapping/MappingWindow.cpp @@ -22,6 +22,7 @@ #include "DolphinQt2/Config/Mapping/GCMicrophone.h" #include "DolphinQt2/Config/Mapping/GCPadEmu.h" #include "DolphinQt2/Config/Mapping/Hotkey3D.h" +#include "DolphinQt2/Config/Mapping/HotkeyDebugging.h" #include "DolphinQt2/Config/Mapping/HotkeyGeneral.h" #include "DolphinQt2/Config/Mapping/HotkeyGraphics.h" #include "DolphinQt2/Config/Mapping/HotkeyStates.h" @@ -278,6 +279,7 @@ void MappingWindow::SetMappingType(MappingWindow::Type type) widget = new HotkeyGeneral(this); AddWidget(tr("General"), widget); AddWidget(tr("TAS Tools"), new HotkeyTAS(this)); + AddWidget(tr("Debugging"), new HotkeyDebugging(this)); AddWidget(tr("Wii and Wii Remote"), new HotkeyWii(this)); AddWidget(tr("Graphics"), new HotkeyGraphics(this)); AddWidget(tr("3D"), new Hotkey3D(this)); diff --git a/Source/Core/DolphinQt2/Debugger/BreakpointWidget.h b/Source/Core/DolphinQt2/Debugger/BreakpointWidget.h index 0a2761f0a5..b48814df19 100644 --- a/Source/Core/DolphinQt2/Debugger/BreakpointWidget.h +++ b/Source/Core/DolphinQt2/Debugger/BreakpointWidget.h @@ -25,6 +25,7 @@ public: bool do_break = true); void AddRangedMBP(u32 from, u32 to, bool do_read = true, bool do_write = true, bool do_log = true, bool do_break = true); + void Update(); protected: void closeEvent(QCloseEvent*) override; @@ -38,8 +39,6 @@ private: void OnLoad(); void OnSave(); - void Update(); - QToolBar* m_toolbar; QTableWidget* m_table; QAction* m_load; diff --git a/Source/Core/DolphinQt2/Debugger/CodeViewWidget.cpp b/Source/Core/DolphinQt2/Debugger/CodeViewWidget.cpp new file mode 100644 index 0000000000..e792727deb --- /dev/null +++ b/Source/Core/DolphinQt2/Debugger/CodeViewWidget.cpp @@ -0,0 +1,544 @@ +// Copyright 2018 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include "DolphinQt2/Debugger/CodeViewWidget.h" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Common/StringUtil.h" +#include "Core/Core.h" +#include "Core/Debugger/PPCDebugInterface.h" +#include "Core/Host.h" +#include "Core/PowerPC/PPCAnalyst.h" +#include "Core/PowerPC/PPCSymbolDB.h" +#include "Core/PowerPC/PowerPC.h" +#include "DolphinQt2/Debugger/CodeWidget.h" +#include "DolphinQt2/QtUtils/ActionHelper.h" +#include "DolphinQt2/Settings.h" + +constexpr size_t VALID_BRANCH_LENGTH = 10; + +CodeViewWidget::CodeViewWidget() +{ + setColumnCount(5); + setShowGrid(false); + setContextMenuPolicy(Qt::CustomContextMenu); + setSelectionMode(QAbstractItemView::SingleSelection); + verticalScrollBar()->setHidden(true); + + for (int i = 0; i < columnCount(); i++) + { + horizontalHeader()->setSectionResizeMode(i, i == 0 ? QHeaderView::Fixed : + QHeaderView::ResizeToContents); + } + + verticalHeader()->hide(); + horizontalHeader()->hide(); + horizontalHeader()->setStretchLastSection(true); + horizontalHeader()->resizeSection(0, 32); + + setFont(Settings::Instance().GetDebugFont()); + + Update(); + + connect(this, &CodeViewWidget::customContextMenuRequested, this, &CodeViewWidget::OnContextMenu); + connect(&Settings::Instance(), &Settings::DebugFontChanged, this, &QWidget::setFont); + connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, [this] { + m_address = PC; + Update(); + }); +} + +static u32 GetBranchFromAddress(u32 addr) +{ + std::string disasm = PowerPC::debug_interface.Disassemble(addr); + size_t pos = disasm.find("->0x"); + + if (pos == std::string::npos) + return 0; + + std::string hex = disasm.substr(pos + 2); + return std::stoul(hex, nullptr, 16); +} + +void CodeViewWidget::Update() +{ + if (m_updating) + return; + + m_updating = true; + + clearSelection(); + if (rowCount() == 0) + setRowCount(1); + + // Calculate (roughly) how many rows will fit in our table + int rows = std::round((height() / static_cast(rowHeight(0))) - 0.25); + + setRowCount(rows); + + for (int i = 0; i < rows; i++) + setRowHeight(i, 24); + + u32 pc = PowerPC::ppcState.pc; + + if (PowerPC::debug_interface.IsBreakpoint(pc)) + Core::SetState(Core::State::Paused); + + for (int i = 0; i < rowCount(); i++) + { + u32 addr = m_address - ((rowCount() / 2) * 4) + i * 4; + u32 color = PowerPC::debug_interface.GetColor(addr); + auto* bp_item = new QTableWidgetItem; + auto* addr_item = new QTableWidgetItem(QStringLiteral("%1").arg(addr, 8, 16, QLatin1Char('0'))); + + std::string disas = PowerPC::debug_interface.Disassemble(addr); + auto split = disas.find('\t'); + + std::string ins = (split == std::string::npos ? disas : disas.substr(0, split)); + std::string param = (split == std::string::npos ? "" : disas.substr(split + 1)); + std::string desc = PowerPC::debug_interface.GetDescription(addr); + + auto* ins_item = new QTableWidgetItem(QString::fromStdString(ins)); + auto* param_item = new QTableWidgetItem(QString::fromStdString(param)); + auto* description_item = new QTableWidgetItem(QString::fromStdString(desc)); + + // look for hex strings to decode branches + std::string hex_str; + size_t pos = param.find("0x"); + if (pos != std::string::npos) + { + hex_str = param.substr(pos); + } + + if (hex_str.length() == VALID_BRANCH_LENGTH && desc != "---") + { + description_item->setText(tr("--> %1").arg(QString::fromStdString( + PowerPC::debug_interface.GetDescription(GetBranchFromAddress(addr))))); + param_item->setForeground(Qt::magenta); + } + + if (ins == "blr") + ins_item->setForeground(Qt::darkGreen); + + for (auto* item : {bp_item, addr_item, ins_item, param_item, description_item}) + { + item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); + item->setData(Qt::UserRole, addr); + + if (color != 0xFFFFFF) + item->setBackground(QColor(color).darker(200)); + + if (addr == pc && item != bp_item) + { + item->setBackground(Qt::darkGreen); + } + } + + if (PowerPC::debug_interface.IsBreakpoint(addr)) + { + bp_item->setBackground(Qt::red); + } + + setItem(i, 0, bp_item); + setItem(i, 1, addr_item); + setItem(i, 2, ins_item); + setItem(i, 3, param_item); + setItem(i, 4, description_item); + + if (addr == GetAddress()) + { + addr_item->setSelected(true); + } + } + + g_symbolDB.FillInCallers(); + + repaint(); + m_updating = false; +} + +u32 CodeViewWidget::GetAddress() const +{ + return m_address; +} + +void CodeViewWidget::SetAddress(u32 address) +{ + if (m_address == address) + return; + + m_address = address; + Update(); +} + +void CodeViewWidget::ReplaceAddress(u32 address, bool blr) +{ + auto found = std::find_if(m_repl_list.begin(), m_repl_list.end(), + [address](ReplStruct r) { return r.address == address; }); + + if (found != m_repl_list.end()) + { + PowerPC::debug_interface.WriteExtraMemory(0, (*found).old_value, address); + m_repl_list.erase(found); + } + else + { + ReplStruct repl; + + repl.address = address; + repl.old_value = PowerPC::debug_interface.ReadInstruction(address); + + m_repl_list.push_back(repl); + + PowerPC::debug_interface.Patch(address, blr ? 0x60000000 : 0x4e800020); + } + + Update(); +} + +void CodeViewWidget::OnContextMenu() +{ + QMenu* menu = new QMenu(this); + + bool running = Core::GetState() != Core::State::Uninitialized; + + const u32 addr = GetContextAddress(); + + bool has_symbol = g_symbolDB.GetSymbolFromAddr(addr); + + auto* follow_branch_action = + AddAction(menu, tr("Follow &branch"), this, &CodeViewWidget::OnFollowBranch); + + menu->addSeparator(); + + AddAction(menu, tr("&Copy address"), this, &CodeViewWidget::OnCopyAddress); + auto* copy_address_action = + AddAction(menu, tr("Copy &function"), this, &CodeViewWidget::OnCopyFunction); + auto* copy_line_action = + AddAction(menu, tr("Copy code &line"), this, &CodeViewWidget::OnCopyCode); + auto* copy_hex_action = AddAction(menu, tr("Copy &hex"), this, &CodeViewWidget::OnCopyHex); + menu->addSeparator(); + + auto* symbol_rename_action = + AddAction(menu, tr("&Rename symbol"), this, &CodeViewWidget::OnRenameSymbol); + auto* symbol_size_action = + AddAction(menu, tr("Set symbol &size"), this, &CodeViewWidget::OnSetSymbolSize); + auto* symbol_end_action = + AddAction(menu, tr("Set symbol &end address"), this, &CodeViewWidget::OnSetSymbolEndAddress); + menu->addSeparator(); + + AddAction(menu, tr("Run &To Here"), this, &CodeViewWidget::OnRunToHere); + auto* function_action = + AddAction(menu, tr("&Add function"), this, &CodeViewWidget::OnAddFunction); + auto* ppc_action = AddAction(menu, tr("PPC vs x86"), this, &CodeViewWidget::OnPPCComparison); + auto* insert_blr_action = AddAction(menu, tr("&Insert blr"), this, &CodeViewWidget::OnInsertBLR); + auto* insert_nop_action = AddAction(menu, tr("Insert &nop"), this, &CodeViewWidget::OnInsertNOP); + auto* replace_action = + AddAction(menu, tr("Re&place instruction"), this, &CodeViewWidget::OnReplaceInstruction); + + follow_branch_action->setEnabled(running && GetBranchFromAddress(addr)); + + for (auto* action : {copy_address_action, copy_line_action, copy_hex_action, function_action, + ppc_action, insert_blr_action, insert_nop_action, replace_action}) + action->setEnabled(running); + + for (auto* action : {symbol_rename_action, symbol_size_action, symbol_end_action}) + action->setEnabled(has_symbol); + + menu->exec(QCursor::pos()); + Update(); +} + +void CodeViewWidget::OnCopyAddress() +{ + const u32 addr = GetContextAddress(); + + QApplication::clipboard()->setText(QStringLiteral("%1").arg(addr, 8, 16, QLatin1Char('0'))); +} + +void CodeViewWidget::OnCopyCode() +{ + const u32 addr = GetContextAddress(); + + QApplication::clipboard()->setText( + QString::fromStdString(PowerPC::debug_interface.Disassemble(addr))); +} + +void CodeViewWidget::OnCopyFunction() +{ + const u32 address = GetContextAddress(); + + Symbol* symbol = g_symbolDB.GetSymbolFromAddr(address); + if (!symbol) + return; + + std::string text = symbol->name + "\r\n"; + // we got a function + u32 start = symbol->address; + u32 end = start + symbol->size; + for (u32 addr = start; addr != end; addr += 4) + { + std::string disasm = PowerPC::debug_interface.Disassemble(addr); + text += StringFromFormat("%08x: ", addr) + disasm + "\r\n"; + } + + QApplication::clipboard()->setText(QString::fromStdString(text)); +} + +void CodeViewWidget::OnCopyHex() +{ + const u32 addr = GetContextAddress(); + const u32 instruction = PowerPC::debug_interface.ReadInstruction(addr); + + QApplication::clipboard()->setText( + QStringLiteral("%1").arg(instruction, 8, 16, QLatin1Char('0'))); +} + +void CodeViewWidget::OnRunToHere() +{ + const u32 addr = GetContextAddress(); + + PowerPC::debug_interface.SetBreakpoint(addr); + PowerPC::debug_interface.RunToBreakpoint(); + Update(); +} + +void CodeViewWidget::OnPPCComparison() +{ + const u32 addr = GetContextAddress(); + + emit RequestPPCComparison(addr); +} + +void CodeViewWidget::OnAddFunction() +{ + const u32 addr = GetContextAddress(); + + g_symbolDB.AddFunction(addr); + emit SymbolsChanged(); + Update(); +} + +void CodeViewWidget::OnInsertBLR() +{ + const u32 addr = GetContextAddress(); + + ReplaceAddress(addr, 0); +} + +void CodeViewWidget::OnInsertNOP() +{ + const u32 addr = GetContextAddress(); + + ReplaceAddress(addr, 1); +} + +void CodeViewWidget::OnFollowBranch() +{ + const u32 addr = GetContextAddress(); + + u32 branch_addr = GetBranchFromAddress(addr); + + if (!branch_addr) + return; + + SetAddress(branch_addr); +} + +void CodeViewWidget::OnRenameSymbol() +{ + const u32 addr = GetContextAddress(); + + Symbol* symbol = g_symbolDB.GetSymbolFromAddr(addr); + + if (!symbol) + return; + + bool good; + QString name = + QInputDialog::getText(this, tr("Rename symbol"), tr("Symbol name:"), QLineEdit::Normal, + QString::fromStdString(symbol->name), &good); + + if (good && !name.isEmpty()) + { + symbol->Rename(name.toStdString()); + emit SymbolsChanged(); + Update(); + } +} + +void CodeViewWidget::OnSetSymbolSize() +{ + const u32 addr = GetContextAddress(); + + Symbol* symbol = g_symbolDB.GetSymbolFromAddr(addr); + + if (!symbol) + return; + + bool good; + int size = + QInputDialog::getInt(this, tr("Rename symbol"), + tr("Set symbol size (%1):").arg(QString::fromStdString(symbol->name)), + symbol->size, 1, 0xFFFF, 1, &good); + + if (!good) + return; + + PPCAnalyst::ReanalyzeFunction(symbol->address, *symbol, size); + emit SymbolsChanged(); + Update(); +} + +void CodeViewWidget::OnSetSymbolEndAddress() +{ + const u32 addr = GetContextAddress(); + + Symbol* symbol = g_symbolDB.GetSymbolFromAddr(addr); + + if (!symbol) + return; + + bool good; + QString name = QInputDialog::getText( + this, tr("Set symbol end address"), + tr("Symbol (%1) end address:").arg(QString::fromStdString(symbol->name)), QLineEdit::Normal, + QStringLiteral("%1").arg(addr + symbol->size, 8, 16, QLatin1Char('0')), &good); + + u32 address = name.toUInt(&good, 16); + + if (!good) + return; + + PPCAnalyst::ReanalyzeFunction(symbol->address, *symbol, address - symbol->address); + emit SymbolsChanged(); + Update(); +} + +void CodeViewWidget::OnReplaceInstruction() +{ + const u32 addr = GetContextAddress(); + + if (!PowerPC::HostIsInstructionRAMAddress(addr)) + return; + + const PowerPC::TryReadInstResult read_result = PowerPC::TryReadInstruction(addr); + if (!read_result.valid) + return; + + bool good; + QString name = QInputDialog::getText( + this, tr("Change instruction"), tr("New instruction:"), QLineEdit::Normal, + QStringLiteral("%1").arg(read_result.hex, 8, 16, QLatin1Char('0')), &good); + + u32 code = name.toUInt(&good, 16); + + if (good) + { + PowerPC::debug_interface.Patch(addr, code); + Update(); + } +} + +void CodeViewWidget::resizeEvent(QResizeEvent*) +{ + Update(); +} + +void CodeViewWidget::keyPressEvent(QKeyEvent* event) +{ + switch (event->key()) + { + case Qt::Key_Up: + m_address -= 3 * sizeof(u32); + Update(); + return; + case Qt::Key_Down: + m_address += 3 * sizeof(u32); + Update(); + return; + case Qt::Key_PageUp: + m_address -= rowCount() * sizeof(u32); + Update(); + return; + case Qt::Key_PageDown: + m_address += rowCount() * sizeof(u32); + Update(); + return; + default: + QWidget::keyPressEvent(event); + break; + } +} + +void CodeViewWidget::wheelEvent(QWheelEvent* event) +{ + int delta = event->delta() > 0 ? -1 : 1; + + m_address += delta * 3 * sizeof(u32); + Update(); +} + +void CodeViewWidget::mousePressEvent(QMouseEvent* event) +{ + auto* item = itemAt(event->pos()); + if (item == nullptr) + return; + + const u32 addr = item->data(Qt::UserRole).toUInt(); + + m_context_address = addr; + + switch (event->button()) + { + case Qt::LeftButton: + if (column(item) == 0) + ToggleBreakpoint(); + else + SetAddress(addr); + + Update(); + break; + default: + break; + } +} + +void CodeViewWidget::ToggleBreakpoint() +{ + if (PowerPC::debug_interface.IsBreakpoint(GetContextAddress())) + PowerPC::breakpoints.Remove(GetContextAddress()); + else + PowerPC::breakpoints.Add(GetContextAddress()); + + emit BreakpointsChanged(); + Update(); +} + +void CodeViewWidget::AddBreakpoint() +{ + PowerPC::breakpoints.Add(GetContextAddress()); + + emit BreakpointsChanged(); + Update(); +} + +u32 CodeViewWidget::GetContextAddress() const +{ + return m_context_address; +} diff --git a/Source/Core/DolphinQt2/Debugger/CodeViewWidget.h b/Source/Core/DolphinQt2/Debugger/CodeViewWidget.h new file mode 100644 index 0000000000..4189588d07 --- /dev/null +++ b/Source/Core/DolphinQt2/Debugger/CodeViewWidget.h @@ -0,0 +1,72 @@ +// Copyright 2018 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#pragma once + +#include + +#include + +#include "Common/CommonTypes.h" + +class QKeyEvent; +class QMouseEvent; +class QResizeEvent; + +class CodeViewWidget : public QTableWidget +{ + Q_OBJECT +public: + explicit CodeViewWidget(); + + u32 GetAddress() const; + u32 GetContextAddress() const; + void SetAddress(u32 address); + + void Update(); + + void ToggleBreakpoint(); + void AddBreakpoint(); +signals: + void RequestPPCComparison(u32 addr); + void SymbolsChanged(); + void BreakpointsChanged(); + +private: + void ReplaceAddress(u32 address, bool blr); + + void resizeEvent(QResizeEvent*) override; + void keyPressEvent(QKeyEvent* event) override; + void mousePressEvent(QMouseEvent* event) override; + void wheelEvent(QWheelEvent* event) override; + + void OnContextMenu(); + + void OnFollowBranch(); + void OnCopyAddress(); + void OnCopyFunction(); + void OnCopyCode(); + void OnCopyHex(); + void OnRenameSymbol(); + void OnSetSymbolSize(); + void OnSetSymbolEndAddress(); + void OnRunToHere(); + void OnAddFunction(); + void OnPPCComparison(); + void OnInsertBLR(); + void OnInsertNOP(); + void OnReplaceInstruction(); + + struct ReplStruct + { + u32 address; + u32 old_value; + }; + + std::vector m_repl_list; + bool m_updating = false; + + u32 m_address = 0; + u32 m_context_address = 0; +}; diff --git a/Source/Core/DolphinQt2/Debugger/CodeWidget.cpp b/Source/Core/DolphinQt2/Debugger/CodeWidget.cpp new file mode 100644 index 0000000000..60cc600ea1 --- /dev/null +++ b/Source/Core/DolphinQt2/Debugger/CodeWidget.cpp @@ -0,0 +1,481 @@ +// Copyright 2018 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include "DolphinQt2/Debugger/CodeWidget.h" + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#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() +{ + m_callstack_list->clear(); + + std::vector 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(); +} diff --git a/Source/Core/DolphinQt2/Debugger/CodeWidget.h b/Source/Core/DolphinQt2/Debugger/CodeWidget.h new file mode 100644 index 0000000000..67216c41b3 --- /dev/null +++ b/Source/Core/DolphinQt2/Debugger/CodeWidget.h @@ -0,0 +1,68 @@ +// Copyright 2018 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#pragma once + +#include +#include + +class CodeViewWidget; +class QCloseEvent; +class QLineEdit; +class QSplitter; +class QListWidget; +class QTableWidget; +struct Symbol; + +class CodeWidget : public QDockWidget +{ + Q_OBJECT +public: + explicit CodeWidget(QWidget* parent = nullptr); + ~CodeWidget(); + + void Step(); + void StepOver(); + void StepOut(); + void Skip(); + void ShowPC(); + void SetPC(); + + void ToggleBreakpoint(); + void AddBreakpoint(); + +signals: + void BreakpointsChanged(); + +private: + void CreateWidgets(); + void ConnectWidgets(); + void Update(); + void UpdateCallstack(); + void UpdateSymbols(); + void UpdateFunctionCalls(Symbol* symbol); + void UpdateFunctionCallers(Symbol* symbol); + + void OnSearchAddress(); + void OnSearchSymbols(); + void OnSelectSymbol(); + void OnSelectCallstack(); + void OnSelectFunctionCallers(); + void OnSelectFunctionCalls(); + + void closeEvent(QCloseEvent*) override; + + QLineEdit* m_search_address; + QLineEdit* m_search_symbols; + + QListWidget* m_callstack_list; + QListWidget* m_symbols_list; + QListWidget* m_function_calls_list; + QListWidget* m_function_callers_list; + CodeViewWidget* m_code_view; + QSplitter* m_box_splitter; + QSplitter* m_code_splitter; + + QString m_symbol_filter; +}; diff --git a/Source/Core/DolphinQt2/DolphinQt2.vcxproj b/Source/Core/DolphinQt2/DolphinQt2.vcxproj index 3382d1f459..2776acb2b8 100644 --- a/Source/Core/DolphinQt2/DolphinQt2.vcxproj +++ b/Source/Core/DolphinQt2/DolphinQt2.vcxproj @@ -91,6 +91,8 @@ + + @@ -107,7 +109,7 @@ - + @@ -133,6 +135,8 @@ + + @@ -209,6 +213,7 @@ + @@ -229,6 +234,8 @@ + + @@ -275,7 +282,7 @@ - + @@ -291,6 +298,8 @@ + + diff --git a/Source/Core/DolphinQt2/Host.cpp b/Source/Core/DolphinQt2/Host.cpp index 715872c995..dd9e57f421 100644 --- a/Source/Core/DolphinQt2/Host.cpp +++ b/Source/Core/DolphinQt2/Host.cpp @@ -9,7 +9,10 @@ #include "Common/Common.h" #include "Core/ConfigManager.h" +#include "Core/Core.h" +#include "Core/Debugger/PPCDebugInterface.h" #include "Core/Host.h" +#include "Core/PowerPC/PowerPC.h" #include "DolphinQt2/Settings.h" #include "VideoCommon/RenderBase.h" @@ -90,10 +93,16 @@ bool Host_RendererIsFullscreen() { return Host::GetInstance()->GetRenderFullscreen(); } + void Host_YieldToUI() { qApp->processEvents(QEventLoop::ExcludeUserInputEvents); } + +void Host_UpdateDisasmDialog() +{ +} + void Host_UpdateProgressDialog(const char* caption, int position, int total) { } @@ -114,9 +123,6 @@ bool Host_UINeedsControllerState() void Host_NotifyMapLoaded() { } -void Host_UpdateDisasmDialog() -{ -} void Host_ShowVideoConfig(void* parent, const std::string& backend_name) { } diff --git a/Source/Core/DolphinQt2/HotkeyScheduler.cpp b/Source/Core/DolphinQt2/HotkeyScheduler.cpp index c70736968b..19785b2ab0 100644 --- a/Source/Core/DolphinQt2/HotkeyScheduler.cpp +++ b/Source/Core/DolphinQt2/HotkeyScheduler.cpp @@ -194,7 +194,31 @@ void HotkeyScheduler::Run() IsHotkey(HK_TRIGGER_SYNC_BUTTON, true)); } - // TODO Debugging shortcuts (Separate PR) + if (IsHotkey(HK_STEP)) + emit Step(); + + if (IsHotkey(HK_STEP_OVER)) + emit StepOver(); + + if (IsHotkey(HK_STEP_OUT)) + emit StepOut(); + + if (IsHotkey(HK_SKIP)) + emit Skip(); + + if (IsHotkey(HK_SHOW_PC)) + emit ShowPC(); + + if (IsHotkey(HK_SET_PC)) + emit Skip(); + + if (IsHotkey(HK_BP_TOGGLE)) + emit ToggleBreakpoint(); + + if (IsHotkey(HK_BP_ADD)) + emit AddBreakpoint(); + + // TODO: HK_MBP_ADD if (SConfig::GetInstance().bWii) { diff --git a/Source/Core/DolphinQt2/HotkeyScheduler.h b/Source/Core/DolphinQt2/HotkeyScheduler.h index 8a32d672f7..13f0f7972d 100644 --- a/Source/Core/DolphinQt2/HotkeyScheduler.h +++ b/Source/Core/DolphinQt2/HotkeyScheduler.h @@ -33,6 +33,17 @@ signals: void ToggleReadOnlyMode(); void ConnectWiiRemote(int id); + void Step(); + void StepOver(); + void StepOut(); + void Skip(); + + void ShowPC(); + void SetPC(); + + void ToggleBreakpoint(); + void AddBreakpoint(); + private: void Run(); diff --git a/Source/Core/DolphinQt2/MainWindow.cpp b/Source/Core/DolphinQt2/MainWindow.cpp index 811d4c79af..e993b07cde 100644 --- a/Source/Core/DolphinQt2/MainWindow.cpp +++ b/Source/Core/DolphinQt2/MainWindow.cpp @@ -50,6 +50,7 @@ #include "DolphinQt2/Config/Mapping/MappingWindow.h" #include "DolphinQt2/Config/SettingsWindow.h" #include "DolphinQt2/Debugger/BreakpointWidget.h" +#include "DolphinQt2/Debugger/CodeWidget.h" #include "DolphinQt2/Debugger/RegisterWidget.h" #include "DolphinQt2/Debugger/WatchWidget.h" #include "DolphinQt2/FIFOPlayerWindow.h" @@ -93,6 +94,7 @@ MainWindow::MainWindow(std::unique_ptr boot_parameters) : QMainW ConnectRenderWidget(); ConnectStack(); ConnectMenuBar(); + ConnectHotkeys(); InitCoreCallbacks(); @@ -121,8 +123,6 @@ void MainWindow::InitControllers() Wiimote::Initialize(Wiimote::InitializeMode::DO_NOT_WAIT_FOR_WIIMOTES); m_hotkey_scheduler = new HotkeyScheduler(); m_hotkey_scheduler->Start(); - - ConnectHotkeys(); } void MainWindow::ShutdownControllers() @@ -195,11 +195,14 @@ void MainWindow::CreateComponents() m_register_widget = new RegisterWidget(this); m_watch_widget = new WatchWidget(this); m_breakpoint_widget = new BreakpointWidget(this); + m_code_widget = new CodeWidget(this); connect(m_watch_widget, &WatchWidget::RequestMemoryBreakpoint, [this](u32 addr) { m_breakpoint_widget->AddAddressMBP(addr); }); connect(m_register_widget, &RegisterWidget::RequestMemoryBreakpoint, [this](u32 addr) { m_breakpoint_widget->AddAddressMBP(addr); }); + connect(m_code_widget, &CodeWidget::BreakpointsChanged, m_breakpoint_widget, + &BreakpointWidget::Update); #if defined(HAVE_XRANDR) && HAVE_XRANDR m_graphics_window = new GraphicsWindow( @@ -312,11 +315,27 @@ void MainWindow::ConnectHotkeys() Movie::SetReadOnly(read_only); emit ReadOnlyModeChanged(read_only); }); + + connect(m_hotkey_scheduler, &HotkeyScheduler::Step, m_code_widget, &CodeWidget::Step); + connect(m_hotkey_scheduler, &HotkeyScheduler::StepOver, m_code_widget, &CodeWidget::StepOver); + connect(m_hotkey_scheduler, &HotkeyScheduler::StepOut, m_code_widget, &CodeWidget::StepOut); + connect(m_hotkey_scheduler, &HotkeyScheduler::Skip, m_code_widget, &CodeWidget::Skip); + + connect(m_hotkey_scheduler, &HotkeyScheduler::ShowPC, m_code_widget, &CodeWidget::ShowPC); + connect(m_hotkey_scheduler, &HotkeyScheduler::SetPC, m_code_widget, &CodeWidget::SetPC); + + connect(m_hotkey_scheduler, &HotkeyScheduler::ToggleBreakpoint, m_code_widget, + &CodeWidget::ToggleBreakpoint); + connect(m_hotkey_scheduler, &HotkeyScheduler::AddBreakpoint, m_code_widget, + &CodeWidget::AddBreakpoint); } void MainWindow::ConnectToolBar() { addToolBar(m_tool_bar); + + connect(m_tool_bar, &ToolBar::OpenPressed, this, &MainWindow::Open); + connect(m_tool_bar, &ToolBar::OpenPressed, this, &MainWindow::Open); connect(m_tool_bar, &ToolBar::PlayPressed, this, [this]() { Play(); }); connect(m_tool_bar, &ToolBar::PausePressed, this, &MainWindow::Pause); @@ -326,6 +345,13 @@ void MainWindow::ConnectToolBar() connect(m_tool_bar, &ToolBar::SettingsPressed, this, &MainWindow::ShowSettingsWindow); connect(m_tool_bar, &ToolBar::ControllersPressed, this, &MainWindow::ShowControllersWindow); connect(m_tool_bar, &ToolBar::GraphicsPressed, this, &MainWindow::ShowGraphicsWindow); + + connect(m_tool_bar, &ToolBar::StepPressed, m_code_widget, &CodeWidget::Step); + connect(m_tool_bar, &ToolBar::StepOverPressed, m_code_widget, &CodeWidget::StepOver); + connect(m_tool_bar, &ToolBar::StepOutPressed, m_code_widget, &CodeWidget::StepOut); + connect(m_tool_bar, &ToolBar::SkipPressed, m_code_widget, &CodeWidget::Skip); + connect(m_tool_bar, &ToolBar::ShowPCPressed, m_code_widget, &CodeWidget::ShowPC); + connect(m_tool_bar, &ToolBar::SetPCPressed, m_code_widget, &CodeWidget::SetPC); } void MainWindow::ConnectGameList() @@ -352,11 +378,13 @@ void MainWindow::ConnectStack() setTabPosition(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea, QTabWidget::North); addDockWidget(Qt::RightDockWidgetArea, m_log_widget); addDockWidget(Qt::RightDockWidgetArea, m_log_config_widget); + addDockWidget(Qt::RightDockWidgetArea, m_code_widget); addDockWidget(Qt::RightDockWidgetArea, m_register_widget); addDockWidget(Qt::RightDockWidgetArea, m_watch_widget); addDockWidget(Qt::RightDockWidgetArea, m_breakpoint_widget); tabifyDockWidget(m_log_widget, m_log_config_widget); + tabifyDockWidget(m_log_widget, m_code_widget); tabifyDockWidget(m_log_widget, m_register_widget); tabifyDockWidget(m_log_widget, m_watch_widget); tabifyDockWidget(m_log_widget, m_breakpoint_widget); diff --git a/Source/Core/DolphinQt2/MainWindow.h b/Source/Core/DolphinQt2/MainWindow.h index 8c57a91e74..261636f192 100644 --- a/Source/Core/DolphinQt2/MainWindow.h +++ b/Source/Core/DolphinQt2/MainWindow.h @@ -19,6 +19,7 @@ class BreakpointWidget; struct BootParameters; +class CodeWidget; class FIFOPlayerWindow; class HotkeyScheduler; class LogConfigWidget; @@ -155,6 +156,7 @@ private: std::array m_wii_tas_input_windows{}; BreakpointWidget* m_breakpoint_widget; + CodeWidget* m_code_widget; LogWidget* m_log_widget; LogConfigWidget* m_log_config_widget; FIFOPlayerWindow* m_fifo_window; diff --git a/Source/Core/DolphinQt2/MenuBar.cpp b/Source/Core/DolphinQt2/MenuBar.cpp index 2563160f25..a98ffb8a0e 100644 --- a/Source/Core/DolphinQt2/MenuBar.cpp +++ b/Source/Core/DolphinQt2/MenuBar.cpp @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include #include #include @@ -17,15 +19,22 @@ #include "Common/FileUtil.h" #include "Common/StringUtil.h" +#include "Core/Boot/Boot.h" #include "Core/CommonTitles.h" #include "Core/ConfigManager.h" #include "Core/Core.h" +#include "Core/Debugger/RSO.h" +#include "Core/HLE/HLE.h" #include "Core/HW/WiiSaveCrypted.h" #include "Core/HW/Wiimote.h" +#include "Core/Host.h" #include "Core/IOS/ES/ES.h" #include "Core/IOS/IOS.h" #include "Core/IOS/USB/Bluetooth/BTEmu.h" #include "Core/Movie.h" +#include "Core/PowerPC/PPCAnalyst.h" +#include "Core/PowerPC/PPCSymbolDB.h" +#include "Core/PowerPC/SignatureDB/SignatureDB.h" #include "Core/State.h" #include "Core/TitleDatabase.h" #include "Core/WiiUtils.h" @@ -46,6 +55,7 @@ MenuBar::MenuBar(QWidget* parent) : QMenuBar(parent) AddOptionsMenu(); AddToolsMenu(); AddViewMenu(); + AddSymbolsMenu(); AddHelpMenu(); connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, @@ -83,6 +93,9 @@ void MenuBar::OnEmulationStateChanged(Core::State state) m_recording_stop->setEnabled(false); m_recording_play->setEnabled(!running); + // Symbols + m_symbols->setEnabled(running); + UpdateStateSlotMenu(); UpdateToolsMenu(running); @@ -91,9 +104,21 @@ void MenuBar::OnEmulationStateChanged(Core::State state) void MenuBar::OnDebugModeToggled(bool enabled) { + // Options + m_boot_to_pause->setVisible(enabled); + m_automatic_start->setVisible(enabled); + m_change_font->setVisible(enabled); + + // View + m_show_code->setVisible(enabled); m_show_registers->setVisible(enabled); m_show_watch->setVisible(enabled); m_show_breakpoints->setVisible(enabled); + + if (enabled) + addMenu(m_symbols); + else + removeAction(m_symbols->menuAction()); } void MenuBar::AddFileMenu() @@ -289,6 +314,14 @@ void MenuBar::AddViewMenu() view_menu->addSeparator(); + m_show_code = view_menu->addAction(tr("&Code")); + m_show_code->setCheckable(true); + m_show_code->setChecked(Settings::Instance().IsCodeVisible()); + + connect(m_show_code, &QAction::toggled, &Settings::Instance(), &Settings::SetCodeVisible); + connect(&Settings::Instance(), &Settings::CodeVisibilityChanged, m_show_code, + &QAction::setChecked); + m_show_registers = view_menu->addAction(tr("&Registers")); m_show_registers->setCheckable(true); m_show_registers->setChecked(Settings::Instance().IsRegistersVisible()); @@ -334,6 +367,25 @@ void MenuBar::AddOptionsMenu() AddAction(options_menu, tr("&Audio Settings"), this, &MenuBar::ConfigureAudio); AddAction(options_menu, tr("&Controller Settings"), this, &MenuBar::ConfigureControllers); AddAction(options_menu, tr("&Hotkey Settings"), this, &MenuBar::ConfigureHotkeys); + + options_menu->addSeparator(); + + // Debugging mode only + m_boot_to_pause = options_menu->addAction(tr("Boot To Pause")); + m_boot_to_pause->setCheckable(true); + m_boot_to_pause->setChecked(SConfig::GetInstance().bBootToPause); + + connect(m_boot_to_pause, &QAction::toggled, this, + [this](bool enable) { SConfig::GetInstance().bBootToPause = enable; }); + + m_automatic_start = options_menu->addAction(tr("&Automatic Start")); + m_automatic_start->setCheckable(true); + m_automatic_start->setChecked(SConfig::GetInstance().bAutomaticStart); + + connect(m_automatic_start, &QAction::toggled, this, + [this](bool enable) { SConfig::GetInstance().bAutomaticStart = enable; }); + + m_change_font = AddAction(options_menu, tr("Font..."), this, &MenuBar::ChangeDebugFont); } void MenuBar::AddHelpMenu() @@ -537,6 +589,35 @@ void MenuBar::AddMovieMenu() [](bool value) { SConfig::GetInstance().m_DumpAudio = value; }); } +void MenuBar::AddSymbolsMenu() +{ + m_symbols = addMenu(tr("Symbols")); + + AddAction(m_symbols, tr("&Clear Symbols"), this, &MenuBar::ClearSymbols); + + auto* generate = m_symbols->addMenu(tr("Generate Symbols From")); + AddAction(generate, tr("Address"), this, &MenuBar::GenerateSymbolsFromAddress); + AddAction(generate, tr("Signature Database"), this, &MenuBar::GenerateSymbolsFromSignatureDB); + AddAction(generate, tr("RSO Modules"), this, &MenuBar::GenerateSymbolsFromRSO); + m_symbols->addSeparator(); + + AddAction(m_symbols, tr("&Load Symbol Map"), this, &MenuBar::LoadSymbolMap); + AddAction(m_symbols, tr("&Save Symbol Map"), this, &MenuBar::SaveSymbolMap); + m_symbols->addSeparator(); + + AddAction(m_symbols, tr("&Load &Other Map File..."), this, &MenuBar::LoadOtherSymbolMap); + AddAction(m_symbols, tr("Save Symbol Map &As..."), this, &MenuBar::SaveSymbolMapAs); + m_symbols->addSeparator(); + + AddAction(m_symbols, tr("Save Code"), this, &MenuBar::SaveCode); + m_symbols->addSeparator(); + + AddAction(m_symbols, tr("&Create Signature File..."), this, &MenuBar::CreateSignatureFile); + m_symbols->addSeparator(); + + AddAction(m_symbols, tr("&Patch HLE Functions"), this, &MenuBar::PatchHLEFunctions); +} + void MenuBar::UpdateToolsMenu(bool emulation_started) { m_boot_sysmenu->setEnabled(!emulation_started); @@ -716,3 +797,181 @@ void MenuBar::OnReadOnlyModeChanged(bool read_only) { m_recording_read_only->setChecked(read_only); } + +void MenuBar::ChangeDebugFont() +{ + bool okay; + QFont font = QFontDialog::getFont(&okay, Settings::Instance().GetDebugFont(), this, + tr("Pick a debug font")); + + if (okay) + Settings::Instance().SetDebugFont(font); +} + +void MenuBar::ClearSymbols() +{ + auto result = QMessageBox::warning(this, tr("Confirmation"), + tr("Do you want to clear the list of symbol names?"), + QMessageBox::Yes | QMessageBox::Cancel); + + if (result == QMessageBox::Cancel) + return; + + g_symbolDB.Clear(); + Host_NotifyMapLoaded(); +} + +void MenuBar::GenerateSymbolsFromAddress() +{ + PPCAnalyst::FindFunctions(0x80000000, 0x81800000, &g_symbolDB); + Host_NotifyMapLoaded(); +} + +void MenuBar::GenerateSymbolsFromSignatureDB() +{ + PPCAnalyst::FindFunctions(0x80000000, 0x81800000, &g_symbolDB); + SignatureDB db(SignatureDB::HandlerType::DSY); + if (db.Load(File::GetSysDirectory() + TOTALDB)) + { + db.Apply(&g_symbolDB); + QMessageBox::information( + this, tr("Information"), + tr("Generated symbol names from '%1'").arg(QString::fromStdString(TOTALDB))); + db.List(); + } + else + { + QMessageBox::critical( + this, tr("Error"), + tr("'%1' not found, no symbol names generated").arg(QString::fromStdString(TOTALDB))); + } + + Host_NotifyMapLoaded(); +} + +void MenuBar::GenerateSymbolsFromRSO() +{ + QString text = QInputDialog::getText(this, tr("Input"), tr("Enter the RSO module address:")); + bool good; + uint address = text.toUInt(&good, 16); + + if (!good) + { + QMessageBox::warning(this, tr("Error"), tr("Invalid RSO module address: %1").arg(text)); + return; + } + + RSOChainView rso_chain; + if (rso_chain.Load(static_cast(address))) + { + rso_chain.Apply(&g_symbolDB); + Host_NotifyMapLoaded(); + } + else + { + QMessageBox::warning(this, tr("Error"), tr("Failed to load RSO module at %1").arg(text)); + } +} + +void MenuBar::LoadSymbolMap() +{ + std::string existing_map_file, writable_map_file; + bool map_exists = CBoot::FindMapFile(&existing_map_file, &writable_map_file); + + if (!map_exists) + { + g_symbolDB.Clear(); + PPCAnalyst::FindFunctions(0x81300000, 0x81800000, &g_symbolDB); + SignatureDB db(SignatureDB::HandlerType::DSY); + if (db.Load(File::GetSysDirectory() + TOTALDB)) + db.Apply(&g_symbolDB); + + QMessageBox::warning(this, tr("Warning"), + tr("'%1' not found, scanning for common functions instead") + .arg(QString::fromStdString(writable_map_file))); + } + else + { + g_symbolDB.LoadMap(existing_map_file); + QMessageBox::information( + this, tr("Information"), + tr("Loaded symbols from '%1'").arg(QString::fromStdString(existing_map_file.c_str()))); + } + + HLE::PatchFunctions(); + Host_NotifyMapLoaded(); +} + +void MenuBar::SaveSymbolMap() +{ + std::string existing_map_file, writable_map_file; + CBoot::FindMapFile(&existing_map_file, &writable_map_file); + + g_symbolDB.SaveSymbolMap(writable_map_file); +} + +void MenuBar::LoadOtherSymbolMap() +{ + QString file = QFileDialog::getOpenFileName(this, tr("Load map file"), + QString::fromStdString(File::GetUserPath(D_MAPS_IDX)), + tr("Dolphin Map File (*.map)")); + + if (file.isEmpty()) + return; + + g_symbolDB.LoadMap(file.toStdString()); + HLE::PatchFunctions(); + Host_NotifyMapLoaded(); +} + +void MenuBar::SaveSymbolMapAs() +{ + const std::string& title_id_str = SConfig::GetInstance().m_debugger_game_id; + QString file = QFileDialog::getSaveFileName( + this, tr("Save map file"), + QString::fromStdString(File::GetUserPath(D_MAPS_IDX) + "/" + title_id_str + ".map"), + tr("Dolphin Map File (*.map)")); + + if (file.isEmpty()) + return; + + g_symbolDB.SaveSymbolMap(file.toStdString()); +} + +void MenuBar::SaveCode() +{ + std::string existing_map_file, writable_map_file; + CBoot::FindMapFile(&existing_map_file, &writable_map_file); + + const std::string path = + writable_map_file.substr(0, writable_map_file.find_last_of(".")) + "_code.map"; + + g_symbolDB.SaveCodeMap(path); +} + +void MenuBar::CreateSignatureFile() +{ + QString text = QInputDialog::getText( + this, tr("Input"), tr("Only export symbols with prefix:\n(Blank for all symbols)")); + + if (text.isEmpty()) + return; + + std::string prefix = text.toStdString(); + + QString file = QFileDialog::getSaveFileName(this, tr("Save signature file")); + + if (file.isEmpty()) + return; + + std::string save_path = file.toStdString(); + SignatureDB db(save_path); + db.Populate(&g_symbolDB, prefix); + db.Save(save_path); + db.List(); +} + +void MenuBar::PatchHLEFunctions() +{ + HLE::PatchFunctions(); +} diff --git a/Source/Core/DolphinQt2/MenuBar.h b/Source/Core/DolphinQt2/MenuBar.h index 3e80ed8cf9..5527d6489f 100644 --- a/Source/Core/DolphinQt2/MenuBar.h +++ b/Source/Core/DolphinQt2/MenuBar.h @@ -113,12 +113,27 @@ private: void AddToolsMenu(); void AddHelpMenu(); void AddMovieMenu(); + void AddSymbolsMenu(); void InstallWAD(); void ImportWiiSave(); void ExportWiiSaves(); void CheckNAND(); void NANDExtractCertificates(); + void ChangeDebugFont(); + + // Debugging UI + void ClearSymbols(); + void GenerateSymbolsFromAddress(); + void GenerateSymbolsFromSignatureDB(); + void GenerateSymbolsFromRSO(); + void LoadSymbolMap(); + void LoadOtherSymbolMap(); + void SaveSymbolMap(); + void SaveSymbolMapAs(); + void SaveCode(); + void CreateSignatureFile(); + void PatchHLEFunctions(); void OnSelectionChanged(QSharedPointer game_file); void OnRecordingStatusChanged(bool recording); @@ -164,8 +179,17 @@ private: QAction* m_recording_stop; QAction* m_recording_read_only; + // Options + QAction* m_boot_to_pause; + QAction* m_automatic_start; + QAction* m_change_font; + // View + QAction* m_show_code; QAction* m_show_registers; QAction* m_show_watch; QAction* m_show_breakpoints; + + // Symbols + QMenu* m_symbols; }; diff --git a/Source/Core/DolphinQt2/Settings.cpp b/Source/Core/DolphinQt2/Settings.cpp index 7809c1b2fa..4e8581b9de 100644 --- a/Source/Core/DolphinQt2/Settings.cpp +++ b/Source/Core/DolphinQt2/Settings.cpp @@ -279,3 +279,36 @@ void Settings::SetControllerStateNeeded(bool needed) { m_controller_state_needed = needed; } + +void Settings::SetCodeVisible(bool enabled) +{ + if (IsCodeVisible() != enabled) + { + QSettings().setValue(QStringLiteral("debugger/showcode"), enabled); + + emit CodeVisibilityChanged(enabled); + } +} + +bool Settings::IsCodeVisible() const +{ + return QSettings().value(QStringLiteral("debugger/showcode")).toBool(); +} + +void Settings::SetDebugFont(QFont font) +{ + if (GetDebugFont() != font) + { + QSettings().setValue(QStringLiteral("debugger/font"), font); + + emit DebugFontChanged(font); + } +} + +QFont Settings::GetDebugFont() const +{ + QFont default_font = QFont(QStringLiteral("Monospace")); + default_font.setStyleHint(QFont::TypeWriter); + + return QSettings().value(QStringLiteral("debugger/font"), default_font).value(); +} diff --git a/Source/Core/DolphinQt2/Settings.h b/Source/Core/DolphinQt2/Settings.h index 9a6950c784..c85c070ef7 100644 --- a/Source/Core/DolphinQt2/Settings.h +++ b/Source/Core/DolphinQt2/Settings.h @@ -6,6 +6,7 @@ #include +#include #include #include @@ -24,6 +25,7 @@ enum class Language; class GameListModel; class InputConfig; +class QFont; // UI settings to be stored in the config directory. class Settings final : public QObject @@ -91,6 +93,10 @@ public: bool IsWatchVisible() const; void SetBreakpointsVisible(bool enabled); bool IsBreakpointsVisible() const; + void SetCodeVisible(bool enabled); + bool IsCodeVisible() const; + QFont GetDebugFont() const; + void SetDebugFont(QFont font); // Other GameListModel* GetGameListModel() const; @@ -110,7 +116,9 @@ signals: void EnableCheatsChanged(bool enabled); void WatchVisibilityChanged(bool visible); void BreakpointsVisibilityChanged(bool visible); + void CodeVisibilityChanged(bool visible); void DebugModeToggled(bool enabled); + void DebugFontChanged(QFont font); private: bool m_controller_state_needed = false; diff --git a/Source/Core/DolphinQt2/ToolBar.cpp b/Source/Core/DolphinQt2/ToolBar.cpp index fe13a74c90..031937945f 100644 --- a/Source/Core/DolphinQt2/ToolBar.cpp +++ b/Source/Core/DolphinQt2/ToolBar.cpp @@ -28,7 +28,11 @@ ToolBar::ToolBar(QWidget* parent) : QToolBar(parent) connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, [this](Core::State state) { OnEmulationStateChanged(state); }); + + connect(&Settings::Instance(), &Settings::DebugModeToggled, this, &ToolBar::OnDebugModeToggled); + OnEmulationStateChanged(Core::GetState()); + OnDebugModeToggled(Settings::Instance().IsDebugModeEnabled()); } void ToolBar::OnEmulationStateChanged(Core::State state) @@ -45,8 +49,25 @@ void ToolBar::OnEmulationStateChanged(Core::State state) m_pause_action->setVisible(playing); } +void ToolBar::OnDebugModeToggled(bool enabled) +{ + m_step_action->setVisible(enabled); + m_step_over_action->setVisible(enabled); + m_step_out_action->setVisible(enabled); + m_skip_action->setVisible(enabled); + m_show_pc_action->setVisible(enabled); + m_set_pc_action->setVisible(enabled); +} + void ToolBar::MakeActions() { + m_step_action = AddAction(this, tr("Step"), this, &ToolBar::StepPressed); + m_step_over_action = AddAction(this, tr("Step Over"), this, &ToolBar::StepOverPressed); + m_step_out_action = AddAction(this, tr("Step Out"), this, &ToolBar::StepOutPressed); + m_skip_action = AddAction(this, tr("Skip"), this, &ToolBar::SkipPressed); + m_show_pc_action = AddAction(this, tr("Show PC"), this, &ToolBar::ShowPCPressed); + m_set_pc_action = AddAction(this, tr("Set PC"), this, &ToolBar::SetPCPressed); + m_open_action = AddAction(this, tr("Open"), this, &ToolBar::OpenPressed); m_play_action = AddAction(this, tr("Play"), this, &ToolBar::PlayPressed); m_pause_action = AddAction(this, tr("Pause"), this, &ToolBar::PausePressed); @@ -63,9 +84,11 @@ void ToolBar::MakeActions() // Ensure every button has about the same width std::vector items; - for (const auto& action : {m_open_action, m_play_action, m_pause_action, m_stop_action, - m_stop_action, m_fullscreen_action, m_screenshot_action, - m_config_action, m_graphics_action, m_controllers_action}) + for (const auto& action : + {m_open_action, m_play_action, m_pause_action, m_stop_action, m_stop_action, + m_fullscreen_action, m_screenshot_action, m_config_action, m_graphics_action, + m_controllers_action, m_step_action, m_step_over_action, m_step_out_action, m_skip_action, + m_show_pc_action, m_set_pc_action}) { items.emplace_back(widgetForAction(action)); } diff --git a/Source/Core/DolphinQt2/ToolBar.h b/Source/Core/DolphinQt2/ToolBar.h index 709fdee4d3..3b8301acdb 100644 --- a/Source/Core/DolphinQt2/ToolBar.h +++ b/Source/Core/DolphinQt2/ToolBar.h @@ -32,8 +32,16 @@ signals: void ControllersPressed(); void GraphicsPressed(); + void StepPressed(); + void StepOverPressed(); + void StepOutPressed(); + void SkipPressed(); + void ShowPCPressed(); + void SetPCPressed(); + private: void OnEmulationStateChanged(Core::State state); + void OnDebugModeToggled(bool enabled); void MakeActions(); void UpdateIcons(); @@ -47,4 +55,11 @@ private: QAction* m_config_action; QAction* m_controllers_action; QAction* m_graphics_action; + + QAction* m_step_action; + QAction* m_step_over_action; + QAction* m_step_out_action; + QAction* m_skip_action; + QAction* m_show_pc_action; + QAction* m_set_pc_action; };