| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Debugger/CodeViewWidget.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <cmath> |
| 8 | |
| 9 | #include <fmt/format.h> |
| 10 | #include <fmt/ranges.h> |
| 11 | |
| 12 | #include <QApplication> |
| 13 | #include <QClipboard> |
| 14 | #include <QHeaderView> |
| 15 | #include <QInputDialog> |
| 16 | #include <QKeyEvent> |
| 17 | #include <QMenu> |
| 18 | #include <QMessageBox> |
| 19 | #include <QMouseEvent> |
| 20 | #include <QPainter> |
| 21 | #include <QResizeEvent> |
| 22 | #include <QScrollBar> |
| 23 | #include <QStyleHints> |
| 24 | #include <QStyledItemDelegate> |
| 25 | #include <QTableWidgetItem> |
| 26 | #include <QWheelEvent> |
| 27 | |
| 28 | #include "Common/Assert.h" |
| 29 | #include "Common/GekkoDisassembler.h" |
| 30 | #include "Core/Core.h" |
| 31 | #include "Core/Debugger/CodeTrace.h" |
| 32 | #include "Core/Debugger/PPCDebugInterface.h" |
| 33 | #include "Core/PowerPC/MMU.h" |
| 34 | #include "Core/PowerPC/PPCAnalyst.h" |
| 35 | #include "Core/PowerPC/PPCSymbolDB.h" |
| 36 | #include "Core/PowerPC/PowerPC.h" |
| 37 | #include "Core/System.h" |
| 38 | #include "DolphinQt/Debugger/AssembleInstructionDialog.h" |
| 39 | #include "DolphinQt/Debugger/EditSymbolDialog.h" |
| 40 | #include "DolphinQt/Debugger/PatchInstructionDialog.h" |
| 41 | #include "DolphinQt/Host.h" |
| 42 | #include "DolphinQt/QtUtils/FromStdString.h" |
| 43 | #include "DolphinQt/Resources.h" |
| 44 | #include "DolphinQt/Settings.h" |
| 45 | |
| 46 | struct CodeViewBranch |
| 47 | { |
| 48 | u32 src_addr = 0; |
| 49 | u32 dst_addr = 0; |
| 50 | u32 indentation = 0; |
| 51 | bool is_link = false; |
| 52 | }; |
| 53 | |
| 54 | constexpr u32 WIDTH_PER_BRANCH_ARROW = 16; |
| 55 | |
| 56 | class BranchDisplayDelegate : public QStyledItemDelegate |
| 57 | { |
| 58 | public: |
| 59 | BranchDisplayDelegate(CodeViewWidget* parent) : QStyledItemDelegate(parent), m_parent(parent) {} |
| 60 | |
| 61 | private: |
| 62 | CodeViewWidget* m_parent; |
| 63 | |
| 64 | void paint(QPainter* painter, const QStyleOptionViewItem& option, |
| 65 | const QModelIndex& index) const override |
| 66 | { |
| 67 | QStyledItemDelegate::paint(painter, option, index); |
| 68 | |
| 69 | painter->save(); |
| 70 | painter->setClipRect(option.rect); |
| 71 | painter->setPen(m_parent->palette().text().color()); |
| 72 | |
| 73 | constexpr u32 x_offset_in_branch_for_vertical_line = 10; |
| 74 | const u32 addr = m_parent->AddressForRow(index.row()); |
| 75 | for (const CodeViewBranch& branch : m_parent->m_branches) |
| 76 | { |
| 77 | const int y_center = option.rect.top() + option.rect.height() / 2; |
| 78 | const int x_left = option.rect.left() + WIDTH_PER_BRANCH_ARROW * branch.indentation; |
| 79 | const int x_right = x_left + x_offset_in_branch_for_vertical_line; |
| 80 | |
| 81 | if (branch.is_link) |
| 82 | { |
| 83 | // just draw an arrow pointing right from the branch instruction for link branches, they |
| 84 | // rarely are close enough to actually see the target and are just visual noise otherwise |
| 85 | if (addr == branch.src_addr) |
| 86 | { |
| 87 | painter->drawLine(x_left, y_center, x_right, y_center); |
| 88 | painter->drawLine(x_right, y_center, x_right - 6, y_center - 3); |
| 89 | painter->drawLine(x_right, y_center, x_right - 6, y_center + 3); |
| 90 | } |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | const u32 addr_lower = std::min(branch.src_addr, branch.dst_addr); |
| 95 | const u32 addr_higher = std::max(branch.src_addr, branch.dst_addr); |
| 96 | const bool in_range = addr >= addr_lower && addr <= addr_higher; |
| 97 | |
| 98 | if (in_range) |
| 99 | { |
| 100 | const bool is_lowest = addr == addr_lower; |
| 101 | const bool is_highest = addr == addr_higher; |
| 102 | const int top = (is_lowest ? y_center : option.rect.top()); |
| 103 | const int bottom = (is_highest ? y_center : option.rect.bottom()); |
| 104 | |
| 105 | // draw vertical part of the branch line |
| 106 | painter->drawLine(x_right, top, x_right, bottom); |
| 107 | |
| 108 | if (is_lowest || is_highest) |
| 109 | { |
| 110 | // draw horizontal part of the branch line if this is either the source or destination |
| 111 | painter->drawLine(x_left, y_center, x_right, y_center); |
| 112 | } |
| 113 | |
| 114 | if (addr == branch.dst_addr) |
| 115 | { |
| 116 | // draw arrow if this is the destination address |
| 117 | painter->drawLine(x_left, y_center, x_left + 6, y_center - 3); |
| 118 | painter->drawLine(x_left, y_center, x_left + 6, y_center + 3); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | painter->restore(); |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | // "Most mouse types work in steps of 15 degrees, in which case the delta value is a multiple of |
| 129 | // 120; i.e., 120 units * 1/8 = 15 degrees." (http://doc.qt.io/qt-5/qwheelevent.html#angleDelta) |
| 130 | constexpr double SCROLL_FRACTION_DEGREES = 15.; |
| 131 | |
| 132 | constexpr size_t VALID_BRANCH_LENGTH = 10; |
| 133 | |
| 134 | constexpr int CODE_VIEW_COLUMN_BREAKPOINT = 0; |
| 135 | constexpr int CODE_VIEW_COLUMN_ADDRESS = 1; |
| 136 | constexpr int CODE_VIEW_COLUMN_INSTRUCTION = 2; |
| 137 | constexpr int CODE_VIEW_COLUMN_PARAMETERS = 3; |
| 138 | constexpr int CODE_VIEW_COLUMN_DESCRIPTION = 4; |
| 139 | constexpr int CODE_VIEW_COLUMN_NOTE = 5; |
| 140 | constexpr int CODE_VIEW_COLUMN_BRANCH_ARROWS = 6; |
| 141 | constexpr int CODE_VIEW_COLUMNCOUNT = 7; |
| 142 | |
| 143 | CodeViewWidget::CodeViewWidget() |
| 144 | : m_system(Core::System::GetInstance()), m_ppc_symbol_db(m_system.GetPPCSymbolDB()) |
| 145 | { |
| 146 | setColumnCount(CODE_VIEW_COLUMNCOUNT); |
| 147 | setShowGrid(false); |
| 148 | setContextMenuPolicy(Qt::CustomContextMenu); |
| 149 | setSelectionMode(QAbstractItemView::SingleSelection); |
| 150 | setSelectionBehavior(QAbstractItemView::SelectRows); |
| 151 | |
| 152 | setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 153 | setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel); |
| 154 | |
| 155 | verticalHeader()->hide(); |
| 156 | horizontalHeader()->setSectionResizeMode(CODE_VIEW_COLUMN_BREAKPOINT, QHeaderView::Fixed); |
| 157 | horizontalHeader()->setStretchLastSection(true); |
| 158 | setHorizontalHeaderItem(CODE_VIEW_COLUMN_BREAKPOINT, new QTableWidgetItem()); |
| 159 | setHorizontalHeaderItem(CODE_VIEW_COLUMN_ADDRESS, new QTableWidgetItem(tr("Address"))); |
| 160 | // i18n: Short for "Instruction" |
| 161 | setHorizontalHeaderItem(CODE_VIEW_COLUMN_INSTRUCTION, new QTableWidgetItem(tr("Instr."))); |
| 162 | setHorizontalHeaderItem(CODE_VIEW_COLUMN_PARAMETERS, new QTableWidgetItem(tr("Parameters"))); |
| 163 | setHorizontalHeaderItem(CODE_VIEW_COLUMN_DESCRIPTION, new QTableWidgetItem(tr("Symbols"))); |
| 164 | setHorizontalHeaderItem(CODE_VIEW_COLUMN_NOTE, new QTableWidgetItem(tr("Notes"))); |
| 165 | setHorizontalHeaderItem(CODE_VIEW_COLUMN_BRANCH_ARROWS, new QTableWidgetItem(tr("Branches"))); |
| 166 | |
| 167 | setFont(Settings::Instance().GetDebugFont()); |
| 168 | setItemDelegateForColumn(CODE_VIEW_COLUMN_BRANCH_ARROWS, new BranchDisplayDelegate(this)); |
| 169 | |
| 170 | FontBasedSizing(); |
| 171 | |
| 172 | #if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0) |
| 173 | connect(QGuiApplication::styleHints(), &QStyleHints::colorSchemeChanged, this, |
| 174 | [this](Qt::ColorScheme colorScheme) { OnSelectionChanged(); }); |
| 175 | #endif |
| 176 | |
| 177 | connect(this, &CodeViewWidget::customContextMenuRequested, this, &CodeViewWidget::OnContextMenu); |
| 178 | connect(this, &CodeViewWidget::itemSelectionChanged, this, &CodeViewWidget::OnSelectionChanged); |
| 179 | connect(&Settings::Instance(), &Settings::DebugFontChanged, this, |
| 180 | &CodeViewWidget::OnDebugFontChanged); |
| 181 | |
| 182 | connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, [this] { |
| 183 | if (!m_lock_address && Core::GetState(m_system) == Core::State::Paused) |
| 184 | m_address = m_system.GetPPCState().pc; |
| 185 | Update(); |
| 186 | }); |
| 187 | connect(Host::GetInstance(), &Host::UpdateDisasmDialog, this, [this] { |
| 188 | if (!m_lock_address && Core::GetState(m_system) == Core::State::Paused) |
| 189 | m_address = m_system.GetPPCState().pc; |
| 190 | Update(); |
| 191 | }); |
| 192 | connect(Host::GetInstance(), &Host::PPCSymbolsChanged, this, |
| 193 | qOverload<>(&CodeViewWidget::Update)); |
| 194 | connect(Host::GetInstance(), &Host::PPCBreakpointsChanged, this, |
| 195 | qOverload<>(&CodeViewWidget::Update)); |
| 196 | |
| 197 | connect(&Settings::Instance(), &Settings::ThemeChanged, this, |
| 198 | qOverload<>(&CodeViewWidget::Update)); |
| 199 | } |
| 200 | |
| 201 | CodeViewWidget::~CodeViewWidget() = default; |
| 202 | |
| 203 | static u32 GetBranchFromAddress(const Core::CPUThreadGuard& guard, u32 addr) |
| 204 | { |
| 205 | std::string disasm = guard.GetSystem().GetPowerPC().GetDebugInterface().Disassemble(&guard, addr); |
| 206 | size_t pos = disasm.find("->0x"); |
| 207 | |
| 208 | if (pos == std::string::npos) |
| 209 | return 0; |
| 210 | |
| 211 | std::string hex = disasm.substr(pos + 2); |
| 212 | return std::stoul(hex, nullptr, 16); |
| 213 | } |
| 214 | |
| 215 | void CodeViewWidget::FontBasedSizing() |
| 216 | { |
| 217 | // just text width is too small with some fonts, so increase by a bit |
| 218 | constexpr int extra_text_width = 8; |
| 219 | |
| 220 | const QFontMetrics fm(font()); |
| 221 | |
| 222 | const int rowh = fm.height() + 1; |
| 223 | verticalHeader()->setMaximumSectionSize(rowh); |
| 224 | horizontalHeader()->setMinimumSectionSize(rowh + 5); |
| 225 | setColumnWidth(CODE_VIEW_COLUMN_BREAKPOINT, rowh + 5); |
| 226 | setColumnWidth(CODE_VIEW_COLUMN_ADDRESS, |
| 227 | fm.boundingRect(QStringLiteral("80000000")).width() + extra_text_width); |
| 228 | |
| 229 | // The longest instruction is technically 'ps_merge00' (0x10000420u), but those instructions are |
| 230 | // very rare and would needlessly increase the column size, so let's go with 'rlwinm.' instead. |
| 231 | // Similarly, the longest parameter set is 'rtoc, rtoc, r10, 10, 10 (00000800)' (0x5c425294u), |
| 232 | // but one is unlikely to encounter that in practice, so let's use a slightly more reasonable |
| 233 | // 'r31, r31, 16, 16, 31 (ffff0000)'. The user can resize the columns as necessary anyway. |
| 234 | const std::string disas = Common::GekkoDisassembler::Disassemble(0x57ff843fu, 0); |
| 235 | const auto split = disas.find('\t'); |
| 236 | const std::string ins = (split == std::string::npos ? disas : disas.substr(0, split)); |
| 237 | const std::string param = (split == std::string::npos ? "" : disas.substr(split + 1)); |
| 238 | setColumnWidth(CODE_VIEW_COLUMN_INSTRUCTION, |
| 239 | fm.boundingRect(QString::fromStdString(ins)).width() + extra_text_width); |
| 240 | setColumnWidth(CODE_VIEW_COLUMN_PARAMETERS, |
| 241 | fm.boundingRect(QString::fromStdString(param)).width() + extra_text_width); |
| 242 | setColumnWidth(CODE_VIEW_COLUMN_DESCRIPTION, |
| 243 | fm.boundingRect(QChar(u'0')).width() * 25 + extra_text_width); |
| 244 | |
| 245 | Update(); |
| 246 | } |
| 247 | |
| 248 | u32 CodeViewWidget::AddressForRow(int row) const |
| 249 | { |
| 250 | // m_address is defined as the center row of the table, so we have rowCount/2 instructions above |
| 251 | // it; an instruction is 4 bytes long on GC/Wii so we increment 4 bytes per row |
| 252 | const u32 row_zero_address = m_address - ((rowCount() / 2) * 4); |
| 253 | return row_zero_address + row * 4; |
| 254 | } |
| 255 | |
| 256 | static bool IsBranchInstructionWithLink(std::string_view ins) |
| 257 | { |
| 258 | return ins.ends_with('l') || ins.ends_with("la") || ins.ends_with("l+") || ins.ends_with("la+") || |
| 259 | ins.ends_with("l-") || ins.ends_with("la-"); |
| 260 | } |
| 261 | |
| 262 | static bool IsInstructionLoadStore(std::string_view ins) |
| 263 | { |
| 264 | // Could add check for context address being near PC, because we need gprs to be correct for the |
| 265 | // load/store. |
| 266 | return (ins.starts_with('l') && !ins.starts_with("li")) || ins.starts_with("st") || |
| 267 | ins.starts_with("psq_l") || ins.starts_with("psq_s"); |
| 268 | } |
| 269 | |
| 270 | void CodeViewWidget::Update() |
| 271 | { |
| 272 | if (!isVisible()) |
| 273 | return; |
| 274 | |
| 275 | if (m_updating) |
| 276 | return; |
| 277 | |
| 278 | if (Core::GetState(m_system) == Core::State::Paused) |
| 279 | { |
| 280 | Core::CPUThreadGuard guard(m_system); |
| 281 | Update(&guard); |
| 282 | } |
| 283 | else |
| 284 | { |
| 285 | // If the core is running, blank out the view of memory instead of reading anything. |
| 286 | Update(nullptr); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | void CodeViewWidget::Update(const Core::CPUThreadGuard* guard) |
| 291 | { |
| 292 | if (!isVisible()) |
| 293 | return; |
| 294 | |
| 295 | if (m_updating) |
| 296 | return; |
| 297 | |
| 298 | m_updating = true; |
| 299 | |
| 300 | clearSelection(); |
| 301 | if (rowCount() == 0) |
| 302 | setRowCount(1); |
| 303 | |
| 304 | // Calculate (roughly) how many rows will fit in our table |
| 305 | int rows = std::round((height() / static_cast<float>(rowHeight(0))) - 0.25); |
| 306 | |
| 307 | setRowCount(rows); |
| 308 | |
| 309 | const QFontMetrics fm(Settings::Instance().GetDebugFont()); |
| 310 | const int rowh = fm.height() + 1; |
| 311 | |
| 312 | for (int i = 0; i < rows; i++) |
| 313 | setRowHeight(i, rowh); |
| 314 | |
| 315 | auto& power_pc = m_system.GetPowerPC(); |
| 316 | auto& debug_interface = power_pc.GetDebugInterface(); |
| 317 | |
| 318 | const std::optional<u32> pc = |
| 319 | guard ? std::make_optional(power_pc.GetPPCState().pc) : std::nullopt; |
| 320 | |
| 321 | const bool dark_theme = Settings::Instance().IsThemeDark(); |
| 322 | const bool breaking_enabled = power_pc.GetBreakPoints().IsBreakingEnabled(); |
| 323 | |
| 324 | m_branches.clear(); |
| 325 | |
| 326 | for (int i = 0; i < rowCount(); i++) |
| 327 | { |
| 328 | const u32 addr = AddressForRow(i); |
| 329 | const u32 color = debug_interface.GetColor(guard, addr); |
| 330 | auto* bp_item = new QTableWidgetItem; |
| 331 | auto* addr_item = new QTableWidgetItem(QStringLiteral("%1").arg(addr, 8, 16, QLatin1Char('0'))); |
| 332 | |
| 333 | std::string disas = debug_interface.Disassemble(guard, addr); |
| 334 | auto split = disas.find('\t'); |
| 335 | |
| 336 | std::string ins = (split == std::string::npos ? disas : disas.substr(0, split)); |
| 337 | std::string param = (split == std::string::npos ? "" : disas.substr(split + 1)); |
| 338 | const std::string desc = debug_interface.GetDescription(addr); |
| 339 | |
| 340 | const Common::Note* note = m_ppc_symbol_db.GetNoteFromAddr(addr); |
| 341 | std::string note_string; |
| 342 | if (note != nullptr) |
| 343 | note_string = note->name; |
| 344 | |
| 345 | // Adds whitespace and a minimum size to ins and param. Helps to prevent frequent resizing while |
| 346 | // scrolling. |
| 347 | const QString ins_formatted = |
| 348 | QStringLiteral("%1").arg(QString::fromStdString(ins), -7, QLatin1Char(' ')); |
| 349 | const QString param_formatted = |
| 350 | QStringLiteral("%1").arg(QString::fromStdString(param), -19, QLatin1Char(' ')); |
| 351 | const QString desc_formatted = QStringLiteral("%1 ").arg(QtUtils::FromStdString(desc)); |
| 352 | |
| 353 | auto* ins_item = new QTableWidgetItem(ins_formatted); |
| 354 | auto* param_item = new QTableWidgetItem(param_formatted); |
| 355 | auto* description_item = new QTableWidgetItem(desc_formatted); |
| 356 | auto* note_item = new QTableWidgetItem(QString::fromStdString(note_string)); |
| 357 | auto* branch_item = new QTableWidgetItem(); |
| 358 | |
| 359 | for (auto* item : |
| 360 | {bp_item, addr_item, ins_item, param_item, description_item, note_item, branch_item}) |
| 361 | { |
| 362 | item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); |
| 363 | item->setData(Qt::UserRole, addr); |
| 364 | |
| 365 | if (addr == pc && item != bp_item) |
| 366 | { |
| 367 | item->setBackground(QColor(Qt::green)); |
| 368 | item->setForeground(QColor(Qt::black)); |
| 369 | } |
| 370 | else if (color != 0xFFFFFF) |
| 371 | { |
| 372 | item->setBackground(dark_theme ? QColor(color).darker(400) : QColor(color)); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | // look for hex strings to decode branches |
| 377 | std::string hex_str; |
| 378 | size_t pos = param.find("0x"); |
| 379 | if (pos != std::string::npos) |
| 380 | { |
| 381 | hex_str = param.substr(pos); |
| 382 | } |
| 383 | |
| 384 | if (guard && hex_str.length() == VALID_BRANCH_LENGTH && desc != "---") |
| 385 | { |
| 386 | u32 branch_addr = GetBranchFromAddress(*guard, addr); |
| 387 | CodeViewBranch& branch = m_branches.emplace_back(); |
| 388 | branch.src_addr = addr; |
| 389 | branch.dst_addr = branch_addr; |
| 390 | branch.is_link = IsBranchInstructionWithLink(ins); |
| 391 | |
| 392 | description_item->setText( |
| 393 | tr("--> %1").arg(QtUtils::FromStdString(debug_interface.GetDescription(branch_addr)))); |
| 394 | param_item->setForeground(dark_theme ? QColor(255, 135, 255) : Qt::magenta); |
| 395 | } |
| 396 | |
| 397 | if (ins == "blr") |
| 398 | ins_item->setForeground(dark_theme ? QColor(0xa0FFa0) : Qt::darkGreen); |
| 399 | |
| 400 | const TBreakPoint* bp = power_pc.GetBreakPoints().GetRegularBreakpoint(addr); |
| 401 | if (bp != nullptr) |
| 402 | { |
| 403 | auto icon = Resources::GetThemeIcon("debugger_breakpoint").pixmap(QSize(rowh - 2, rowh - 2)); |
| 404 | if (!breaking_enabled || !bp->is_enabled) |
| 405 | { |
| 406 | QPixmap disabled_icon(icon.size()); |
| 407 | disabled_icon.fill(Qt::transparent); |
| 408 | QPainter p(&disabled_icon); |
| 409 | p.setOpacity(0.20); |
| 410 | p.drawPixmap(0, 0, icon); |
| 411 | p.end(); |
| 412 | icon = disabled_icon; |
| 413 | } |
| 414 | bp_item->setData(Qt::DecorationRole, icon); |
| 415 | } |
| 416 | |
| 417 | setItem(i, CODE_VIEW_COLUMN_BREAKPOINT, bp_item); |
| 418 | setItem(i, CODE_VIEW_COLUMN_ADDRESS, addr_item); |
| 419 | setItem(i, CODE_VIEW_COLUMN_INSTRUCTION, ins_item); |
| 420 | setItem(i, CODE_VIEW_COLUMN_PARAMETERS, param_item); |
| 421 | setItem(i, CODE_VIEW_COLUMN_DESCRIPTION, description_item); |
| 422 | setItem(i, CODE_VIEW_COLUMN_NOTE, note_item); |
| 423 | setItem(i, CODE_VIEW_COLUMN_BRANCH_ARROWS, branch_item); |
| 424 | |
| 425 | if (addr == GetAddress()) |
| 426 | { |
| 427 | selectRow(addr_item->row()); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | m_ppc_symbol_db.NoteExists() ? showColumn(CODE_VIEW_COLUMN_NOTE) : |
| 432 | hideColumn(CODE_VIEW_COLUMN_NOTE); |
| 433 | |
| 434 | CalculateBranchIndentation(); |
| 435 | |
| 436 | repaint(); |
| 437 | m_updating = false; |
| 438 | } |
| 439 | |
| 440 | void CodeViewWidget::CalculateBranchIndentation() |
| 441 | { |
| 442 | const u32 rows = rowCount(); |
| 443 | const size_t columns = m_branches.size(); |
| 444 | if (rows < 1 || columns < 1) |
| 445 | return; |
| 446 | |
| 447 | // process in order of how much vertical space the drawn arrow would take up |
| 448 | // so shorter arrows go further to the left |
| 449 | std::ranges::stable_sort(m_branches, {}, [](const CodeViewBranch& b) { |
| 450 | return b.is_link ? 0 : (std::max(b.src_addr, b.dst_addr) - std::min(b.src_addr, b.dst_addr)); |
| 451 | }); |
| 452 | |
| 453 | // build a 2D lookup table representing the columns and rows the arrow could be drawn in |
| 454 | // and try to place all branch arrows in it as far left as possible |
| 455 | std::vector<bool> arrow_space_used(columns * rows, false); |
| 456 | const auto index = [&](u32 column, u32 row) { |
| 457 | ASSERT(row <= rows); |
| 458 | ASSERT(column <= columns); |
| 459 | return column * rows + row; |
| 460 | }; |
| 461 | |
| 462 | const auto add_branch_arrow = [&](CodeViewBranch& branch, u32 first_addr, u32 first_row, |
| 463 | u32 last_addr) { |
| 464 | const u32 arrow_src_addr = branch.src_addr; |
| 465 | const u32 arrow_dst_addr = branch.is_link ? branch.src_addr : branch.dst_addr; |
| 466 | const auto [arrow_addr_lower, arrow_addr_higher] = std::minmax(arrow_src_addr, arrow_dst_addr); |
| 467 | |
| 468 | const bool is_visible = |
| 469 | std::max(arrow_addr_lower, first_addr) <= std::min(arrow_addr_higher, last_addr); |
| 470 | if (!is_visible) |
| 471 | return; |
| 472 | |
| 473 | const u32 arrow_first_visible_addr = std::clamp(arrow_addr_lower, first_addr, last_addr); |
| 474 | const u32 arrow_last_visible_addr = std::clamp(arrow_addr_higher, first_addr, last_addr); |
| 475 | const u32 arrow_first_visible_row = (arrow_first_visible_addr - first_addr) / 4 + first_row; |
| 476 | const u32 arrow_last_visible_row = (arrow_last_visible_addr - first_addr) / 4 + first_row; |
| 477 | |
| 478 | const auto free_column = [&]() -> std::optional<u32> { |
| 479 | for (u32 column = 0; column < columns; ++column) |
| 480 | { |
| 481 | const bool column_is_free = [&] { |
| 482 | for (u32 row = arrow_first_visible_row; row <= arrow_last_visible_row; ++row) |
| 483 | { |
| 484 | if (arrow_space_used[index(column, row)]) |
| 485 | return false; |
| 486 | } |
| 487 | return true; |
| 488 | }(); |
| 489 | if (column_is_free) |
| 490 | return column; |
| 491 | } |
| 492 | return std::nullopt; |
| 493 | }(); |
| 494 | |
| 495 | if (!free_column) |
| 496 | return; |
| 497 | |
| 498 | branch.indentation = *free_column; |
| 499 | for (u32 row = arrow_first_visible_row; row <= arrow_last_visible_row; ++row) |
| 500 | arrow_space_used[index(*free_column, row)] = true; |
| 501 | }; |
| 502 | |
| 503 | const u32 first_visible_addr = AddressForRow(0); |
| 504 | const u32 last_visible_addr = AddressForRow(rows - 1); |
| 505 | |
| 506 | if (first_visible_addr <= last_visible_addr) |
| 507 | { |
| 508 | for (CodeViewBranch& branch : m_branches) |
| 509 | add_branch_arrow(branch, first_visible_addr, 0, last_visible_addr); |
| 510 | } |
| 511 | else |
| 512 | { |
| 513 | // Scrolling defaults to being centered around address 00000000, which means addresses before |
| 514 | // the start are visible (e.g. ffffffa8 - 00000050). We need to do this in two parts, one for |
| 515 | // first_visible_addr to fffffffc, and the second for 00000000 to last_visible_addr. |
| 516 | // That means we need to find the row corresponding to 00000000. |
| 517 | int addr_zero_row = -1; |
| 518 | for (u32 row = 0; row < rows; row++) |
| 519 | { |
| 520 | if (AddressForRow(row) == 0) |
| 521 | { |
| 522 | addr_zero_row = row; |
| 523 | break; |
| 524 | } |
| 525 | } |
| 526 | ASSERT(addr_zero_row != -1); |
| 527 | |
| 528 | for (CodeViewBranch& branch : m_branches) |
| 529 | { |
| 530 | add_branch_arrow(branch, first_visible_addr, 0, 0xfffffffc); |
| 531 | add_branch_arrow(branch, 0x00000000, addr_zero_row, last_visible_addr); |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | u32 CodeViewWidget::GetAddress() const |
| 537 | { |
| 538 | return m_address; |
| 539 | } |
| 540 | |
| 541 | void CodeViewWidget::OnLockAddress(bool lock) |
| 542 | { |
| 543 | m_lock_address = lock; |
| 544 | } |
| 545 | |
| 546 | void CodeViewWidget::SetAddress(u32 address, SetAddressUpdate update) |
| 547 | { |
| 548 | if (m_address == address) |
| 549 | return; |
| 550 | |
| 551 | m_address = address; |
| 552 | switch (update) |
| 553 | { |
| 554 | case SetAddressUpdate::WithoutUpdate: |
| 555 | return; |
| 556 | case SetAddressUpdate::WithUpdate: |
| 557 | // Update the CodeViewWidget |
| 558 | Update(); |
| 559 | break; |
| 560 | case SetAddressUpdate::WithDetailedUpdate: |
| 561 | // Update the CodeWidget's views (code view, function calls/callers, ...) |
| 562 | emit UpdateCodeWidget(); |
| 563 | break; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | void CodeViewWidget::ReplaceAddress(u32 address, ReplaceWith replace) |
| 568 | { |
| 569 | Core::CPUThreadGuard guard(m_system); |
| 570 | |
| 571 | m_system.GetPowerPC().GetDebugInterface().SetPatch( |
| 572 | guard, address, replace == ReplaceWith::BLR ? 0x4e800020 : 0x60000000); |
| 573 | |
| 574 | Update(&guard); |
| 575 | } |
| 576 | |
| 577 | void CodeViewWidget::OnContextMenu() |
| 578 | { |
| 579 | QMenu* menu = new QMenu(this); |
| 580 | menu->setAttribute(Qt::WA_DeleteOnClose, true); |
| 581 | |
| 582 | const bool running = Core::IsRunning(m_system); |
| 583 | const bool paused = Core::GetState(m_system) == Core::State::Paused; |
| 584 | |
| 585 | const u32 addr = GetContextAddress(); |
| 586 | |
| 587 | auto* follow_branch_action = |
| 588 | menu->addAction(tr("Follow &Branch"), this, &CodeViewWidget::OnFollowBranch); |
| 589 | |
| 590 | menu->addSeparator(); |
| 591 | |
| 592 | menu->addAction(tr("&Copy Address"), this, &CodeViewWidget::OnCopyAddress); |
| 593 | auto* copy_address_action = |
| 594 | menu->addAction(tr("Copy &Function"), this, &CodeViewWidget::OnCopyFunction); |
| 595 | auto* copy_line_action = |
| 596 | menu->addAction(tr("Copy Code &Line"), this, &CodeViewWidget::OnCopyCode); |
| 597 | auto* copy_hex_action = menu->addAction(tr("Copy &Hex"), this, &CodeViewWidget::OnCopyHex); |
| 598 | |
| 599 | menu->addAction(tr("Show in &Memory"), this, &CodeViewWidget::OnShowInMemory); |
| 600 | auto* show_target_memory = |
| 601 | menu->addAction(tr("Show Target in Memor&y"), this, &CodeViewWidget::OnShowTargetInMemory); |
| 602 | auto* copy_target_memory = |
| 603 | menu->addAction(tr("Copy Tar&get Address"), this, &CodeViewWidget::OnCopyTargetAddress); |
| 604 | menu->addSeparator(); |
| 605 | |
| 606 | auto* symbol_add_action = |
| 607 | menu->addAction(tr("&Add function symbol"), this, &CodeViewWidget::OnAddFunction); |
| 608 | auto* symbol_edit_action = |
| 609 | menu->addAction(tr("&Edit function symbol"), this, &CodeViewWidget::OnEditSymbol); |
| 610 | |
| 611 | auto* note_add_action = menu->addAction(tr("Add Note"), this, &CodeViewWidget::OnAddNote); |
| 612 | auto* note_edit_action = menu->addAction(tr("Edit Note"), this, &CodeViewWidget::OnEditNote); |
| 613 | |
| 614 | menu->addSeparator(); |
| 615 | |
| 616 | auto* run_to_action = menu->addAction(tr("Run &to Here"), this, &CodeViewWidget::OnRunToHere); |
| 617 | auto* ppc_action = menu->addAction(tr("PPC vs Host"), this, &CodeViewWidget::OnPPCComparison); |
| 618 | auto* insert_blr_action = menu->addAction(tr("&Insert BLR"), this, &CodeViewWidget::OnInsertBLR); |
| 619 | auto* insert_nop_action = menu->addAction(tr("Insert &NOP"), this, &CodeViewWidget::OnInsertNOP); |
| 620 | auto* replace_action = |
| 621 | menu->addAction(tr("Re&place Instruction"), this, &CodeViewWidget::OnReplaceInstruction); |
| 622 | auto* assemble_action = |
| 623 | menu->addAction(tr("Assemble Instruction"), this, &CodeViewWidget::OnAssembleInstruction); |
| 624 | auto* restore_action = |
| 625 | menu->addAction(tr("Restore Instruction"), this, &CodeViewWidget::OnRestoreInstruction); |
| 626 | |
| 627 | QString target; |
| 628 | bool valid_load_store = false; |
| 629 | bool follow_branch_enabled = false; |
| 630 | if (paused) |
| 631 | { |
| 632 | Core::CPUThreadGuard guard(m_system); |
| 633 | const u32 pc = m_system.GetPPCState().pc; |
| 634 | const std::string disasm = m_system.GetPowerPC().GetDebugInterface().Disassemble(&guard, pc); |
| 635 | |
| 636 | if (addr == pc) |
| 637 | { |
| 638 | const auto target_it = std::find(disasm.begin(), disasm.end(), '\t'); |
| 639 | const auto target_end = std::find(target_it, disasm.end(), ','); |
| 640 | |
| 641 | if (target_it != disasm.end() && target_end != disasm.end()) |
| 642 | target = QString::fromStdString(std::string{target_it + 1, target_end}); |
| 643 | } |
| 644 | |
| 645 | valid_load_store = IsInstructionLoadStore(disasm); |
| 646 | |
| 647 | follow_branch_enabled = GetBranchFromAddress(guard, addr); |
| 648 | } |
| 649 | |
| 650 | auto* run_until_menu = menu->addMenu(tr("Run Until (Ignoring Breakpoints)")); |
| 651 | // i18n: One of the options shown below "Run Until (Ignoring Breakpoints)" |
| 652 | run_until_menu->addAction(tr("%1's value is hit").arg(target), this, |
| 653 | [this] { AutoStep(CodeTrace::AutoStop::Always); }); |
| 654 | // i18n: One of the options shown below "Run Until (Ignoring Breakpoints)" |
| 655 | run_until_menu->addAction(tr("%1's value is used").arg(target), this, |
| 656 | [this] { AutoStep(CodeTrace::AutoStop::Used); }); |
| 657 | // i18n: One of the options shown below "Run Until (Ignoring Breakpoints)" |
| 658 | run_until_menu->addAction(tr("%1's value is changed").arg(target), |
| 659 | [this] { AutoStep(CodeTrace::AutoStop::Changed); }); |
| 660 | |
| 661 | run_until_menu->setEnabled(!target.isEmpty()); |
| 662 | follow_branch_action->setEnabled(follow_branch_enabled); |
| 663 | |
| 664 | for (auto* action : |
| 665 | {copy_address_action, copy_line_action, copy_hex_action, symbol_add_action, |
| 666 | symbol_edit_action, note_add_action, note_edit_action, run_to_action, ppc_action, |
| 667 | insert_blr_action, insert_nop_action, replace_action, assemble_action}) |
| 668 | { |
| 669 | action->setEnabled(running); |
| 670 | } |
| 671 | |
| 672 | for (auto* action : {copy_target_memory, show_target_memory}) |
| 673 | { |
| 674 | action->setEnabled(valid_load_store); |
| 675 | } |
| 676 | |
| 677 | auto* note = m_ppc_symbol_db.GetNoteFromAddr(addr); |
| 678 | note_edit_action->setEnabled(note != nullptr); |
| 679 | // A note cannot be added on top of the starting address of another note. |
| 680 | if (note != nullptr && note->address == addr) |
| 681 | note_add_action->setEnabled(false); |
| 682 | |
| 683 | restore_action->setEnabled(running && |
| 684 | m_system.GetPowerPC().GetDebugInterface().HasEnabledPatch(addr)); |
| 685 | |
| 686 | menu->exec(QCursor::pos()); |
| 687 | Update(); |
| 688 | } |
| 689 | |
| 690 | void CodeViewWidget::AutoStep(CodeTrace::AutoStop option) |
| 691 | { |
| 692 | // Autosteps and follows value in the target (left-most) register. The Used and Changed options |
| 693 | // silently follows target through reshuffles in memory and registers and stops on use or update. |
| 694 | |
| 695 | Core::CPUThreadGuard guard(m_system); |
| 696 | |
| 697 | CodeTrace code_trace; |
| 698 | bool repeat = false; |
| 699 | |
| 700 | QMessageBox msgbox(QMessageBox::NoIcon, tr("Run Until"), {}, QMessageBox::Cancel); |
| 701 | QPushButton* run_button = msgbox.addButton(tr("Keep Running"), QMessageBox::AcceptRole); |
| 702 | // Not sure if we want default to be cancel. Spacebar can let you quickly continue autostepping if |
| 703 | // Yes. |
| 704 | |
| 705 | do |
| 706 | { |
| 707 | // Run autostep then update codeview |
| 708 | const AutoStepResults results = code_trace.AutoStepping(guard, repeat, option); |
| 709 | emit Host::GetInstance()->UpdateDisasmDialog(); |
| 710 | repeat = true; |
| 711 | |
| 712 | // Invalid instruction, 0 means no step executed. |
| 713 | if (results.count == 0) |
| 714 | return; |
| 715 | |
| 716 | // Status report |
| 717 | if (results.reg_tracked.empty() && results.mem_tracked.empty()) |
| 718 | { |
| 719 | QMessageBox::warning( |
| 720 | this, tr("Overwritten"), |
| 721 | tr("Target value was overwritten by current instruction.\nInstructions executed: %1") |
| 722 | .arg(QString::number(results.count)), |
| 723 | QMessageBox::Cancel); |
| 724 | return; |
| 725 | } |
| 726 | else if (results.timed_out) |
| 727 | { |
| 728 | // Can keep running and try again after a time out. |
| 729 | msgbox.setText( |
| 730 | tr("<font color='#ff0000'>AutoStepping timed out. Current instruction is irrelevant.")); |
| 731 | } |
| 732 | else |
| 733 | { |
| 734 | msgbox.setText(tr("Value tracked to current instruction.")); |
| 735 | } |
| 736 | |
| 737 | // Mem_tracked needs to track each byte individually, so a tracked word-sized value would have |
| 738 | // four entries. The displayed memory list needs to be shortened so it's not a huge list of |
| 739 | // bytes. Assumes adjacent bytes represent a word or half-word and removes the redundant bytes. |
| 740 | std::set<u32> mem_out; |
| 741 | auto iter = results.mem_tracked.begin(); |
| 742 | |
| 743 | while (iter != results.mem_tracked.end()) |
| 744 | { |
| 745 | const u32 address = *iter; |
| 746 | mem_out.insert(address); |
| 747 | |
| 748 | for (u32 i = 1; i <= 3; i++) |
| 749 | { |
| 750 | if (results.mem_tracked.contains(address + i)) |
| 751 | iter++; |
| 752 | else |
| 753 | break; |
| 754 | } |
| 755 | |
| 756 | iter++; |
| 757 | } |
| 758 | |
| 759 | const QString msgtext = |
| 760 | tr("Instructions executed: %1\nValue contained in:\nRegisters: %2\nMemory: %3") |
| 761 | .arg(QString::number(results.count)) |
| 762 | .arg(QString::fromStdString(fmt::format("{}", fmt::join(results.reg_tracked, ", ")))) |
| 763 | .arg(QString::fromStdString(fmt::format("{:#x}", fmt::join(mem_out, ", ")))); |
| 764 | |
| 765 | msgbox.setInformativeText(msgtext); |
| 766 | msgbox.exec(); |
| 767 | |
| 768 | } while (msgbox.clickedButton() == (QAbstractButton*)run_button); |
| 769 | } |
| 770 | |
| 771 | void CodeViewWidget::OnDebugFontChanged(const QFont& font) |
| 772 | { |
| 773 | setFont(font); |
| 774 | FontBasedSizing(); |
| 775 | } |
| 776 | |
| 777 | void CodeViewWidget::OnCopyAddress() |
| 778 | { |
| 779 | const u32 addr = GetContextAddress(); |
| 780 | |
| 781 | QApplication::clipboard()->setText(QStringLiteral("%1").arg(addr, 8, 16, QLatin1Char('0'))); |
| 782 | } |
| 783 | |
| 784 | void CodeViewWidget::OnCopyTargetAddress() |
| 785 | { |
| 786 | if (Core::GetState(m_system) != Core::State::Paused) |
| 787 | return; |
| 788 | |
| 789 | const u32 addr = GetContextAddress(); |
| 790 | |
| 791 | const std::string code_line = [this, addr] { |
| 792 | Core::CPUThreadGuard guard(m_system); |
| 793 | return m_system.GetPowerPC().GetDebugInterface().Disassemble(&guard, addr); |
| 794 | }(); |
| 795 | |
| 796 | if (!IsInstructionLoadStore(code_line)) |
| 797 | return; |
| 798 | |
| 799 | const std::optional<u32> target_addr = |
| 800 | m_system.GetPowerPC().GetDebugInterface().GetMemoryAddressFromInstruction(code_line); |
| 801 | |
| 802 | if (target_addr) |
| 803 | { |
| 804 | QApplication::clipboard()->setText( |
| 805 | QStringLiteral("%1").arg(*target_addr, 8, 16, QLatin1Char('0'))); |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | void CodeViewWidget::OnShowInMemory() |
| 810 | { |
| 811 | emit ShowMemory(GetContextAddress()); |
| 812 | } |
| 813 | |
| 814 | void CodeViewWidget::OnShowTargetInMemory() |
| 815 | { |
| 816 | if (Core::GetState(m_system) != Core::State::Paused) |
| 817 | return; |
| 818 | |
| 819 | const u32 addr = GetContextAddress(); |
| 820 | |
| 821 | const std::string code_line = [this, addr] { |
| 822 | Core::CPUThreadGuard guard(m_system); |
| 823 | return m_system.GetPowerPC().GetDebugInterface().Disassemble(&guard, addr); |
| 824 | }(); |
| 825 | |
| 826 | if (!IsInstructionLoadStore(code_line)) |
| 827 | return; |
| 828 | |
| 829 | const std::optional<u32> target_addr = |
| 830 | m_system.GetPowerPC().GetDebugInterface().GetMemoryAddressFromInstruction(code_line); |
| 831 | |
| 832 | if (target_addr) |
| 833 | emit ShowMemory(*target_addr); |
| 834 | } |
| 835 | |
| 836 | void CodeViewWidget::OnCopyCode() |
| 837 | { |
| 838 | const u32 addr = GetContextAddress(); |
| 839 | |
| 840 | const std::string text = [this, addr] { |
| 841 | Core::CPUThreadGuard guard(m_system); |
| 842 | return m_system.GetPowerPC().GetDebugInterface().Disassemble(&guard, addr); |
| 843 | }(); |
| 844 | |
| 845 | QApplication::clipboard()->setText(QString::fromStdString(text)); |
| 846 | } |
| 847 | |
| 848 | void CodeViewWidget::OnCopyFunction() |
| 849 | { |
| 850 | const u32 address = GetContextAddress(); |
| 851 | |
| 852 | const Common::Symbol* const symbol = m_ppc_symbol_db.GetSymbolFromAddr(address); |
| 853 | if (!symbol) |
| 854 | return; |
| 855 | |
| 856 | std::string text = symbol->name + "\r\n"; |
| 857 | |
| 858 | { |
| 859 | Core::CPUThreadGuard guard(m_system); |
| 860 | |
| 861 | // we got a function |
| 862 | const u32 start = symbol->address; |
| 863 | const u32 end = start + symbol->size; |
| 864 | for (u32 addr = start; addr != end; addr += 4) |
| 865 | { |
| 866 | const std::string disasm = |
| 867 | m_system.GetPowerPC().GetDebugInterface().Disassemble(&guard, addr); |
| 868 | fmt::format_to(std::back_inserter(text), "{:08x}: {}\r\n", addr, disasm); |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | QApplication::clipboard()->setText(QString::fromStdString(text)); |
| 873 | } |
| 874 | |
| 875 | void CodeViewWidget::OnCopyHex() |
| 876 | { |
| 877 | const u32 addr = GetContextAddress(); |
| 878 | |
| 879 | const u32 instruction = [this, addr] { |
| 880 | Core::CPUThreadGuard guard(m_system); |
| 881 | return m_system.GetPowerPC().GetDebugInterface().ReadInstruction(guard, addr); |
| 882 | }(); |
| 883 | |
| 884 | QApplication::clipboard()->setText( |
| 885 | QStringLiteral("%1").arg(instruction, 8, 16, QLatin1Char('0'))); |
| 886 | } |
| 887 | |
| 888 | void CodeViewWidget::OnRunToHere() |
| 889 | { |
| 890 | const u32 addr = GetContextAddress(); |
| 891 | |
| 892 | m_system.GetPowerPC().GetDebugInterface().RunTo(addr); |
| 893 | } |
| 894 | |
| 895 | void CodeViewWidget::OnPPCComparison() |
| 896 | { |
| 897 | const u32 addr = GetContextAddress(); |
| 898 | |
| 899 | emit RequestPPCComparison(addr, m_system.GetPPCState().msr.IR); |
| 900 | } |
| 901 | |
| 902 | void CodeViewWidget::OnAddFunction() |
| 903 | { |
| 904 | const u32 addr = GetContextAddress(); |
| 905 | const int confirm = |
| 906 | QMessageBox::warning(this, tr("Add Function Symbol"), |
| 907 | tr("Force new function symbol to be made at %1?").arg(addr, 0, 16), |
| 908 | QMessageBox::Ok | QMessageBox::Cancel); |
| 909 | |
| 910 | if (confirm != QMessageBox::Ok) |
| 911 | return; |
| 912 | |
| 913 | Core::CPUThreadGuard guard(m_system); |
| 914 | |
| 915 | m_ppc_symbol_db.AddFunction(guard, addr); |
| 916 | emit Host::GetInstance()->PPCSymbolsChanged(); |
| 917 | } |
| 918 | |
| 919 | void CodeViewWidget::OnInsertBLR() |
| 920 | { |
| 921 | const u32 addr = GetContextAddress(); |
| 922 | |
| 923 | ReplaceAddress(addr, ReplaceWith::BLR); |
| 924 | } |
| 925 | |
| 926 | void CodeViewWidget::OnInsertNOP() |
| 927 | { |
| 928 | const u32 addr = GetContextAddress(); |
| 929 | |
| 930 | ReplaceAddress(addr, ReplaceWith::NOP); |
| 931 | } |
| 932 | |
| 933 | void CodeViewWidget::OnFollowBranch() |
| 934 | { |
| 935 | const u32 addr = GetContextAddress(); |
| 936 | |
| 937 | const u32 branch_addr = [this, addr] { |
| 938 | Core::CPUThreadGuard guard(m_system); |
| 939 | return GetBranchFromAddress(guard, addr); |
| 940 | }(); |
| 941 | |
| 942 | if (!branch_addr) |
| 943 | return; |
| 944 | |
| 945 | SetAddress(branch_addr, SetAddressUpdate::WithDetailedUpdate); |
| 946 | } |
| 947 | |
| 948 | void CodeViewWidget::OnEditSymbol() |
| 949 | { |
| 950 | const u32 addr = GetContextAddress(); |
| 951 | const Common::Symbol* const symbol = m_ppc_symbol_db.GetSymbolFromAddr(addr); |
| 952 | |
| 953 | if (symbol == nullptr) |
| 954 | { |
| 955 | OnAddFunction(); |
| 956 | return; |
| 957 | } |
| 958 | |
| 959 | std::string name = symbol->name; |
| 960 | u32 size = symbol->size; |
| 961 | const u32 symbol_address = symbol->address; |
| 962 | |
| 963 | EditSymbolDialog dialog(this, symbol_address, &size, &name); |
| 964 | |
| 965 | if (dialog.exec() != QDialog::Accepted) |
| 966 | return; |
| 967 | |
| 968 | if (dialog.DeleteRequested()) |
| 969 | { |
| 970 | OnDeleteSymbol(); |
| 971 | return; |
| 972 | } |
| 973 | |
| 974 | if (symbol->name != name) |
| 975 | m_ppc_symbol_db.RenameSymbol(*symbol, name); |
| 976 | |
| 977 | if (symbol->size != size) |
| 978 | { |
| 979 | Core::CPUThreadGuard guard(m_system); |
| 980 | Common::Symbol new_symbol = *symbol; |
| 981 | PPCAnalyst::ReanalyzeFunction(guard, symbol->address, new_symbol, size); |
| 982 | m_ppc_symbol_db.AddCompleteSymbol(new_symbol); |
| 983 | } |
| 984 | |
| 985 | emit Host::GetInstance()->PPCSymbolsChanged(); |
| 986 | } |
| 987 | |
| 988 | void CodeViewWidget::OnDeleteSymbol() |
| 989 | { |
| 990 | const u32 addr = GetContextAddress(); |
| 991 | const Common::Symbol* const symbol = m_ppc_symbol_db.GetSymbolFromAddr(addr); |
| 992 | |
| 993 | if (symbol == nullptr) |
| 994 | return; |
| 995 | |
| 996 | const int confirm = QMessageBox::warning(this, tr("Delete Function Symbol"), |
| 997 | tr("Delete function symbol: %1\nat %2?") |
| 998 | .arg(QString::fromStdString(symbol->name)) |
| 999 | .arg(addr, 0, 16), |
| 1000 | QMessageBox::Ok | QMessageBox::Cancel); |
| 1001 | |
| 1002 | if (confirm != QMessageBox::Ok) |
| 1003 | return; |
| 1004 | |
| 1005 | m_ppc_symbol_db.DeleteFunction(symbol->address); |
| 1006 | |
| 1007 | emit Host::GetInstance()->PPCSymbolsChanged(); |
| 1008 | } |
| 1009 | |
| 1010 | void CodeViewWidget::OnAddNote() |
| 1011 | { |
| 1012 | const u32 note_address = GetContextAddress(); |
| 1013 | std::string name = ""; |
| 1014 | u32 size = 4; |
| 1015 | |
| 1016 | EditSymbolDialog dialog(this, note_address, &size, &name, EditSymbolDialog::Type::Note); |
| 1017 | |
| 1018 | if (dialog.exec() != QDialog::Accepted || dialog.DeleteRequested()) |
| 1019 | return; |
| 1020 | |
| 1021 | m_ppc_symbol_db.AddKnownNote(note_address, size, name); |
| 1022 | m_ppc_symbol_db.DetermineNoteLayers(); |
| 1023 | emit Host::GetInstance()->PPCSymbolsChanged(); |
| 1024 | } |
| 1025 | |
| 1026 | void CodeViewWidget::OnSelectionChanged() |
| 1027 | { |
| 1028 | if (m_address == m_system.GetPPCState().pc) |
| 1029 | { |
| 1030 | setStyleSheet( |
| 1031 | QStringLiteral("QTableView::item:selected {background-color: #00FF00; color: #000000;}")); |
| 1032 | } |
| 1033 | else if (!styleSheet().isEmpty()) |
| 1034 | { |
| 1035 | setStyleSheet(QString{}); |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | void CodeViewWidget::OnEditNote() |
| 1040 | { |
| 1041 | const u32 context_address = GetContextAddress(); |
| 1042 | const Common::Note* const note = m_ppc_symbol_db.GetNoteFromAddr(context_address); |
| 1043 | |
| 1044 | if (note == nullptr) |
| 1045 | return; |
| 1046 | |
| 1047 | std::string name = note->name; |
| 1048 | u32 size = note->size; |
| 1049 | const u32 note_address = note->address; |
| 1050 | |
| 1051 | EditSymbolDialog dialog(this, note_address, &size, &name, EditSymbolDialog::Type::Note); |
| 1052 | |
| 1053 | if (dialog.exec() != QDialog::Accepted) |
| 1054 | return; |
| 1055 | |
| 1056 | if (dialog.DeleteRequested()) |
| 1057 | { |
| 1058 | OnDeleteNote(); |
| 1059 | return; |
| 1060 | } |
| 1061 | |
| 1062 | if (note->name != name || note->size != size) |
| 1063 | { |
| 1064 | m_ppc_symbol_db.AddKnownNote(note_address, size, name); |
| 1065 | m_ppc_symbol_db.DetermineNoteLayers(); |
| 1066 | } |
| 1067 | |
| 1068 | emit Host::GetInstance()->PPCSymbolsChanged(); |
| 1069 | } |
| 1070 | |
| 1071 | void CodeViewWidget::OnDeleteNote() |
| 1072 | { |
| 1073 | const u32 context_address = GetContextAddress(); |
| 1074 | const Common::Note* const note = m_ppc_symbol_db.GetNoteFromAddr(context_address); |
| 1075 | |
| 1076 | if (note == nullptr) |
| 1077 | return; |
| 1078 | |
| 1079 | const int confirm = QMessageBox::warning(this, tr("Delete Note"), |
| 1080 | tr("Delete Note: %1\nat %2?") |
| 1081 | .arg(QString::fromStdString(note->name)) |
| 1082 | .arg(context_address, 0, 16), |
| 1083 | QMessageBox::Ok | QMessageBox::Cancel); |
| 1084 | |
| 1085 | if (confirm != QMessageBox::Ok) |
| 1086 | return; |
| 1087 | |
| 1088 | m_ppc_symbol_db.DeleteNote(note->address); |
| 1089 | |
| 1090 | emit Host::GetInstance()->PPCSymbolsChanged(); |
| 1091 | } |
| 1092 | |
| 1093 | void CodeViewWidget::OnReplaceInstruction() |
| 1094 | { |
| 1095 | DoPatchInstruction(false); |
| 1096 | } |
| 1097 | |
| 1098 | void CodeViewWidget::OnAssembleInstruction() |
| 1099 | { |
| 1100 | DoPatchInstruction(true); |
| 1101 | } |
| 1102 | |
| 1103 | void CodeViewWidget::DoPatchInstruction(bool assemble) |
| 1104 | { |
| 1105 | Core::CPUThreadGuard guard(m_system); |
| 1106 | const u32 addr = GetContextAddress(); |
| 1107 | |
| 1108 | if (!PowerPC::MMU::HostIsInstructionRAMAddress(guard, addr)) |
| 1109 | return; |
| 1110 | |
| 1111 | const PowerPC::TryReadInstResult read_result = |
| 1112 | guard.GetSystem().GetMMU().TryReadInstruction(addr); |
| 1113 | if (!read_result.valid) |
| 1114 | return; |
| 1115 | |
| 1116 | auto& debug_interface = m_system.GetPowerPC().GetDebugInterface(); |
| 1117 | |
| 1118 | if (assemble) |
| 1119 | { |
| 1120 | AssembleInstructionDialog dialog(this, addr, debug_interface.ReadInstruction(guard, addr)); |
| 1121 | if (dialog.exec() == QDialog::Accepted) |
| 1122 | { |
| 1123 | debug_interface.SetPatch(guard, addr, dialog.GetCode()); |
| 1124 | Update(&guard); |
| 1125 | } |
| 1126 | } |
| 1127 | else |
| 1128 | { |
| 1129 | PatchInstructionDialog dialog(this, addr, debug_interface.ReadInstruction(guard, addr)); |
| 1130 | if (dialog.exec() == QDialog::Accepted) |
| 1131 | { |
| 1132 | debug_interface.SetPatch(guard, addr, dialog.GetCode()); |
| 1133 | Update(&guard); |
| 1134 | } |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | void CodeViewWidget::OnRestoreInstruction() |
| 1139 | { |
| 1140 | Core::CPUThreadGuard guard(m_system); |
| 1141 | |
| 1142 | const u32 addr = GetContextAddress(); |
| 1143 | |
| 1144 | m_system.GetPowerPC().GetDebugInterface().UnsetPatch(guard, addr); |
| 1145 | Update(&guard); |
| 1146 | } |
| 1147 | |
| 1148 | void CodeViewWidget::resizeEvent(QResizeEvent*) |
| 1149 | { |
| 1150 | Update(); |
| 1151 | } |
| 1152 | |
| 1153 | void CodeViewWidget::keyPressEvent(QKeyEvent* event) |
| 1154 | { |
| 1155 | switch (event->key()) |
| 1156 | { |
| 1157 | case Qt::Key_Up: |
| 1158 | m_address -= sizeof(u32); |
| 1159 | Update(); |
| 1160 | return; |
| 1161 | case Qt::Key_Down: |
| 1162 | m_address += sizeof(u32); |
| 1163 | Update(); |
| 1164 | return; |
| 1165 | case Qt::Key_PageUp: |
| 1166 | m_address -= rowCount() * sizeof(u32); |
| 1167 | Update(); |
| 1168 | return; |
| 1169 | case Qt::Key_PageDown: |
| 1170 | m_address += rowCount() * sizeof(u32); |
| 1171 | Update(); |
| 1172 | return; |
| 1173 | case Qt::Key_G: |
| 1174 | if (event->modifiers() == Qt::ControlModifier) |
| 1175 | { |
| 1176 | emit ActivateSearch(); |
| 1177 | } |
| 1178 | default: |
| 1179 | QWidget::keyPressEvent(event); |
| 1180 | break; |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | void CodeViewWidget::wheelEvent(QWheelEvent* event) |
| 1185 | { |
| 1186 | auto delta = |
| 1187 | -static_cast<int>(std::round((event->angleDelta().y() / (SCROLL_FRACTION_DEGREES * 8)))); |
| 1188 | |
| 1189 | if (delta == 0) |
| 1190 | return; |
| 1191 | |
| 1192 | m_address += delta * sizeof(u32); |
| 1193 | Update(); |
| 1194 | } |
| 1195 | |
| 1196 | void CodeViewWidget::mousePressEvent(QMouseEvent* event) |
| 1197 | { |
| 1198 | auto* item = itemAt(event->pos()); |
| 1199 | if (item == nullptr) |
| 1200 | return; |
| 1201 | |
| 1202 | const u32 addr = item->data(Qt::UserRole).toUInt(); |
| 1203 | |
| 1204 | m_context_address = addr; |
| 1205 | |
| 1206 | switch (event->button()) |
| 1207 | { |
| 1208 | case Qt::LeftButton: |
| 1209 | if (column(item) == CODE_VIEW_COLUMN_BREAKPOINT) |
| 1210 | ToggleBreakpoint(); |
| 1211 | else |
| 1212 | SetAddress(addr, SetAddressUpdate::WithDetailedUpdate); |
| 1213 | |
| 1214 | Update(); |
| 1215 | break; |
| 1216 | default: |
| 1217 | break; |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | void CodeViewWidget::showEvent(QShowEvent* event) |
| 1222 | { |
| 1223 | Update(); |
| 1224 | } |
| 1225 | |
| 1226 | void CodeViewWidget::ToggleBreakpoint() |
| 1227 | { |
| 1228 | m_system.GetPowerPC().GetBreakPoints().ToggleBreakPoint(GetContextAddress()); |
| 1229 | |
| 1230 | emit Host::GetInstance()->PPCBreakpointsChanged(); |
| 1231 | } |
| 1232 | |
| 1233 | void CodeViewWidget::AddBreakpoint() |
| 1234 | { |
| 1235 | m_system.GetPowerPC().GetBreakPoints().Add(GetContextAddress()); |
| 1236 | |
| 1237 | emit Host::GetInstance()->PPCBreakpointsChanged(); |
| 1238 | } |
| 1239 | |
| 1240 | u32 CodeViewWidget::GetContextAddress() const |
| 1241 | { |
| 1242 | return m_context_address; |
| 1243 | } |