Merge pull request #8642 from AdmiralCurtiss/debugger_branch_arrows

Qt/CodeViewWidget: Implement branch arrows.
This commit is contained in:
Léo Lam 2020-03-15 16:45:36 +01:00 committed by GitHub
commit 24a01642e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 218 additions and 12 deletions

View File

@ -14,8 +14,10 @@
#include <QKeyEvent>
#include <QMenu>
#include <QMouseEvent>
#include <QPainter>
#include <QResizeEvent>
#include <QScrollBar>
#include <QStyledItemDelegate>
#include <QTableWidgetItem>
#include <QWheelEvent>
@ -30,15 +32,105 @@
#include "DolphinQt/Resources.h"
#include "DolphinQt/Settings.h"
struct CodeViewBranch
{
u32 src_addr;
u32 dst_addr;
u32 indentation = 0;
bool is_link;
};
constexpr u32 WIDTH_PER_BRANCH_ARROW = 16;
class BranchDisplayDelegate : public QStyledItemDelegate
{
public:
BranchDisplayDelegate(CodeViewWidget* parent) : m_parent(parent) {}
private:
CodeViewWidget* m_parent;
void paint(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const override
{
QStyledItemDelegate::paint(painter, option, index);
painter->save();
painter->setClipRect(option.rect);
painter->setPen(m_parent->palette().text().color());
constexpr u32 x_offset_in_branch_for_vertical_line = 10;
const u32 addr = m_parent->AddressForRow(index.row());
for (const CodeViewBranch& branch : m_parent->m_branches)
{
const int y_center = option.rect.top() + option.rect.height() / 2;
const int x_left = option.rect.left() + WIDTH_PER_BRANCH_ARROW * branch.indentation;
const int x_right = x_left + x_offset_in_branch_for_vertical_line;
if (branch.is_link)
{
// just draw an arrow pointing right from the branch instruction for link branches, they
// rarely are close enough to actually see the target and are just visual noise otherwise
if (addr == branch.src_addr)
{
painter->drawLine(x_left, y_center, x_right, y_center);
painter->drawLine(x_right, y_center, x_right - 6, y_center - 3);
painter->drawLine(x_right, y_center, x_right - 6, y_center + 3);
}
}
else
{
const u32 addr_lower = std::min(branch.src_addr, branch.dst_addr);
const u32 addr_higher = std::max(branch.src_addr, branch.dst_addr);
const bool in_range = addr >= addr_lower && addr <= addr_higher;
if (in_range)
{
const bool is_lowest = addr == addr_lower;
const bool is_highest = addr == addr_higher;
const int top = (is_lowest ? y_center : option.rect.top());
const int bottom = (is_highest ? y_center : option.rect.bottom());
// draw vertical part of the branch line
painter->drawLine(x_right, top, x_right, bottom);
if (is_lowest || is_highest)
{
// draw horizontal part of the branch line if this is either the source or destination
painter->drawLine(x_left, y_center, x_right, y_center);
}
if (addr == branch.dst_addr)
{
// draw arrow if this is the destination address
painter->drawLine(x_left, y_center, x_left + 6, y_center - 3);
painter->drawLine(x_left, y_center, x_left + 6, y_center + 3);
}
}
}
}
painter->restore();
}
};
// "Most mouse types work in steps of 15 degrees, in which case the delta value is a multiple of
// 120; i.e., 120 units * 1/8 = 15 degrees." (http://doc.qt.io/qt-5/qwheelevent.html#angleDelta)
constexpr double SCROLL_FRACTION_DEGREES = 15.;
constexpr size_t VALID_BRANCH_LENGTH = 10;
constexpr int CODE_VIEW_COLUMN_BREAKPOINT = 0;
constexpr int CODE_VIEW_COLUMN_ADDRESS = 1;
constexpr int CODE_VIEW_COLUMN_INSTRUCTION = 2;
constexpr int CODE_VIEW_COLUMN_PARAMETERS = 3;
constexpr int CODE_VIEW_COLUMN_DESCRIPTION = 4;
constexpr int CODE_VIEW_COLUMN_BRANCH_ARROWS = 5;
constexpr int CODE_VIEW_COLUMNCOUNT = 6;
CodeViewWidget::CodeViewWidget()
{
setColumnCount(5);
setColumnCount(CODE_VIEW_COLUMNCOUNT);
setShowGrid(false);
setContextMenuPolicy(Qt::CustomContextMenu);
setSelectionMode(QAbstractItemView::SingleSelection);
@ -51,6 +143,7 @@ CodeViewWidget::CodeViewWidget()
horizontalHeader()->hide();
setFont(Settings::Instance().GetDebugFont());
setItemDelegateForColumn(CODE_VIEW_COLUMN_BRANCH_ARROWS, new BranchDisplayDelegate(this));
FontBasedSizing();
@ -68,6 +161,8 @@ CodeViewWidget::CodeViewWidget()
connect(&Settings::Instance(), &Settings::ThemeChanged, this, &CodeViewWidget::Update);
}
CodeViewWidget::~CodeViewWidget() = default;
static u32 GetBranchFromAddress(u32 addr)
{
std::string disasm = PowerPC::debug_interface.Disassemble(addr);
@ -86,10 +181,24 @@ void CodeViewWidget::FontBasedSizing()
const int rowh = fm.height() + 1;
verticalHeader()->setMaximumSectionSize(rowh);
horizontalHeader()->setMinimumSectionSize(rowh + 5);
setColumnWidth(0, rowh + 5);
setColumnWidth(CODE_VIEW_COLUMN_BREAKPOINT, rowh + 5);
Update();
}
u32 CodeViewWidget::AddressForRow(int row) const
{
// m_address is defined as the center row of the table, so we have rowCount/2 instructions above
// it; an instruction is 4 bytes long on GC/Wii so we increment 4 bytes per row
const u32 row_zero_address = m_address - ((rowCount() / 2) * 4);
return row_zero_address + row * 4;
}
static bool IsBranchInstructionWithLink(std::string_view ins)
{
return StringEndsWith(ins, "l") || StringEndsWith(ins, "la") || StringEndsWith(ins, "l+") ||
StringEndsWith(ins, "la+") || StringEndsWith(ins, "l-") || StringEndsWith(ins, "la-");
}
void CodeViewWidget::Update()
{
if (!isVisible())
@ -122,9 +231,11 @@ void CodeViewWidget::Update()
const bool dark_theme = qApp->palette().color(QPalette::Base).valueF() < 0.5;
m_branches.clear();
for (int i = 0; i < rowCount(); i++)
{
const u32 addr = m_address - ((rowCount() / 2) * 4) + i * 4;
const u32 addr = AddressForRow(i);
const 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')));
@ -147,8 +258,9 @@ void CodeViewWidget::Update()
auto* ins_item = new QTableWidgetItem(ins_formatted);
auto* param_item = new QTableWidgetItem(param_formatted);
auto* description_item = new QTableWidgetItem(desc_formatted);
auto* branch_item = new QTableWidgetItem();
for (auto* item : {bp_item, addr_item, ins_item, param_item, description_item})
for (auto* item : {bp_item, addr_item, ins_item, param_item, description_item, branch_item})
{
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
item->setData(Qt::UserRole, addr);
@ -174,8 +286,14 @@ void CodeViewWidget::Update()
if (hex_str.length() == VALID_BRANCH_LENGTH && desc != "---")
{
description_item->setText(tr("--> %1").arg(QString::fromStdString(
PowerPC::debug_interface.GetDescription(GetBranchFromAddress(addr)))));
u32 branch_addr = GetBranchFromAddress(addr);
CodeViewBranch& branch = m_branches.emplace_back();
branch.src_addr = addr;
branch.dst_addr = branch_addr;
branch.is_link = IsBranchInstructionWithLink(ins);
description_item->setText(tr("--> %1").arg(
QString::fromStdString(PowerPC::debug_interface.GetDescription(branch_addr))));
param_item->setForeground(Qt::magenta);
}
@ -189,11 +307,12 @@ void CodeViewWidget::Update()
Resources::GetScaledThemeIcon("debugger_breakpoint").pixmap(QSize(rowh - 2, rowh - 2)));
}
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);
setItem(i, CODE_VIEW_COLUMN_BREAKPOINT, bp_item);
setItem(i, CODE_VIEW_COLUMN_ADDRESS, addr_item);
setItem(i, CODE_VIEW_COLUMN_INSTRUCTION, ins_item);
setItem(i, CODE_VIEW_COLUMN_PARAMETERS, param_item);
setItem(i, CODE_VIEW_COLUMN_DESCRIPTION, description_item);
setItem(i, CODE_VIEW_COLUMN_BRANCH_ARROWS, branch_item);
if (addr == GetAddress())
{
@ -201,13 +320,88 @@ void CodeViewWidget::Update()
}
}
CalculateBranchIndentation();
u32 max_indent = 0;
for (const CodeViewBranch& branch : m_branches)
max_indent = std::max(max_indent, branch.indentation);
resizeColumnsToContents();
setColumnWidth(CODE_VIEW_COLUMN_BRANCH_ARROWS,
static_cast<int>(WIDTH_PER_BRANCH_ARROW * (max_indent + 1)));
g_symbolDB.FillInCallers();
repaint();
m_updating = false;
}
void CodeViewWidget::CalculateBranchIndentation()
{
const size_t rows = rowCount();
const size_t columns = m_branches.size();
if (rows < 1 || columns < 1)
return;
// process in order of how much vertical space the drawn arrow would take up
// so shorter arrows go further to the left
const auto priority = [](const CodeViewBranch& b) {
return b.is_link ? 0 : (std::max(b.src_addr, b.dst_addr) - std::min(b.src_addr, b.dst_addr));
};
std::stable_sort(m_branches.begin(), m_branches.end(),
[&priority](const CodeViewBranch& lhs, const CodeViewBranch& rhs) {
return priority(lhs) < priority(rhs);
});
// build a 2D lookup table representing the columns and rows the arrow could be drawn in
// and try to place all branch arrows in it as far left as possible
std::vector<bool> arrow_space_used(columns * rows, false);
const auto index = [&](u32 column, u32 row) { return column * rows + row; };
const u32 first_visible_addr = AddressForRow(0);
const u32 last_visible_addr = AddressForRow(static_cast<int>(rows - 1));
for (CodeViewBranch& branch : m_branches)
{
const u32 arrow_src_addr = branch.src_addr;
const u32 arrow_dst_addr = branch.is_link ? branch.src_addr : branch.dst_addr;
const u32 arrow_addr_lower = std::min(arrow_src_addr, arrow_dst_addr);
const u32 arrow_addr_higher = std::max(arrow_src_addr, arrow_dst_addr);
const bool is_visible =
last_visible_addr >= arrow_addr_lower || first_visible_addr <= arrow_addr_higher;
if (!is_visible)
continue;
const u32 arrow_first_visible_addr =
std::clamp(arrow_addr_lower, first_visible_addr, last_visible_addr);
const u32 arrow_last_visible_addr =
std::clamp(arrow_addr_higher, first_visible_addr, last_visible_addr);
const u32 arrow_first_visible_row = (arrow_first_visible_addr - first_visible_addr) / 4;
const u32 arrow_last_visible_row = (arrow_last_visible_addr - first_visible_addr) / 4;
const auto free_column = [&]() -> std::optional<u32> {
for (u32 column = 0; column < columns; ++column)
{
const bool column_is_free = [&] {
for (u32 row = arrow_first_visible_row; row <= arrow_last_visible_row; ++row)
{
if (arrow_space_used[index(column, row)])
return false;
}
return true;
}();
if (column_is_free)
return column;
}
return std::nullopt;
}();
if (!free_column)
continue;
branch.indentation = *free_column;
for (u32 row = arrow_first_visible_row; row <= arrow_last_visible_row; ++row)
arrow_space_used[index(*free_column, row)] = true;
}
}
u32 CodeViewWidget::GetAddress() const
{
return m_address;
@ -559,7 +753,7 @@ void CodeViewWidget::mousePressEvent(QMouseEvent* event)
switch (event->button())
{
case Qt::LeftButton:
if (column(item) == 0)
if (column(item) == CODE_VIEW_COLUMN_BREAKPOINT)
ToggleBreakpoint();
else
SetAddress(addr, SetAddressUpdate::WithUpdate);

View File

@ -15,6 +15,9 @@ class QMouseEvent;
class QResizeEvent;
class QShowEvent;
struct CodeViewBranch;
class BranchDisplayDelegate;
class CodeViewWidget : public QTableWidget
{
Q_OBJECT
@ -26,6 +29,7 @@ public:
};
explicit CodeViewWidget();
~CodeViewWidget() override;
u32 GetAddress() const;
u32 GetContextAddress() const;
@ -38,6 +42,8 @@ public:
void ToggleBreakpoint();
void AddBreakpoint();
u32 AddressForRow(int row) const;
signals:
void RequestPPCComparison(u32 addr);
void ShowMemory(u32 address);
@ -79,8 +85,14 @@ private:
void OnReplaceInstruction();
void OnRestoreInstruction();
void CalculateBranchIndentation();
bool m_updating = false;
u32 m_address = 0;
u32 m_context_address = 0;
std::vector<CodeViewBranch> m_branches;
friend class BranchDisplayDelegate;
};