Qt/Debugger: Implement "Code" widget

This commit is contained in:
spycrab
2018-02-14 23:25:01 +01:00
parent b289be43eb
commit 0a5f0efe18
21 changed files with 1696 additions and 13 deletions

View File

@ -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;

View File

@ -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 <algorithm>
#include <cmath>
#include <QApplication>
#include <QClipboard>
#include <QHeaderView>
#include <QInputDialog>
#include <QKeyEvent>
#include <QMenu>
#include <QMouseEvent>
#include <QResizeEvent>
#include <QScrollBar>
#include <QTableWidgetItem>
#include <QWheelEvent>
#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<float>(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;
}

View File

@ -0,0 +1,72 @@
// Copyright 2018 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#pragma once
#include <vector>
#include <QTableWidget>
#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<ReplStruct> m_repl_list;
bool m_updating = false;
u32 m_address = 0;
u32 m_context_address = 0;
};

View File

@ -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 <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()
{
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();
}

View File

@ -0,0 +1,68 @@
// Copyright 2018 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#pragma once
#include <QDockWidget>
#include <QString>
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;
};