| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Debugger/BreakpointWidget.h" |
| 5 | |
| 6 | #include <QApplication> |
| 7 | #include <QHeaderView> |
| 8 | #include <QInputDialog> |
| 9 | #include <QMenu> |
| 10 | #include <QMouseEvent> |
| 11 | #include <QPainter> |
| 12 | #include <QSignalBlocker> |
| 13 | #include <QStyleOptionViewItem> |
| 14 | #include <QStyledItemDelegate> |
| 15 | #include <QTableWidget> |
| 16 | #include <QToolBar> |
| 17 | #include <QVBoxLayout> |
| 18 | |
| 19 | #include "Common/Contains.h" |
| 20 | #include "Common/FileUtil.h" |
| 21 | #include "Common/IniFile.h" |
| 22 | #include "Core/ConfigManager.h" |
| 23 | #include "Core/Core.h" |
| 24 | #include "Core/PowerPC/BreakPoints.h" |
| 25 | #include "Core/PowerPC/Expression.h" |
| 26 | #include "Core/PowerPC/PPCSymbolDB.h" |
| 27 | #include "Core/PowerPC/PowerPC.h" |
| 28 | #include "Core/System.h" |
| 29 | |
| 30 | #include "DolphinQt/Debugger/BreakpointDialog.h" |
| 31 | #include "DolphinQt/Debugger/MemoryWidget.h" |
| 32 | #include "DolphinQt/Host.h" |
| 33 | #include "DolphinQt/Resources.h" |
| 34 | #include "DolphinQt/Settings.h" |
| 35 | |
| 36 | // Qt constants |
| 37 | namespace |
| 38 | { |
| 39 | enum CustomRole |
| 40 | { |
| 41 | ADDRESS_ROLE = Qt::UserRole, |
| 42 | IS_MEMCHECK_ROLE |
| 43 | }; |
| 44 | |
| 45 | enum TableColumns |
| 46 | { |
| 47 | ENABLED_COLUMN = 0, |
| 48 | TYPE_COLUMN = 1, |
| 49 | SYMBOL_COLUMN = 2, |
| 50 | ADDRESS_COLUMN = 3, |
| 51 | END_ADDRESS_COLUMN = 4, |
| 52 | BREAK_COLUMN = 5, |
| 53 | LOG_COLUMN = 6, |
| 54 | READ_COLUMN = 7, |
| 55 | WRITE_COLUMN = 8, |
| 56 | CONDITION_COLUMN = 9, |
| 57 | }; |
| 58 | } // namespace |
| 59 | |
| 60 | // Fix icons not centering properly in a QTableWidget. |
| 61 | class CustomDelegate : public QStyledItemDelegate |
| 62 | { |
| 63 | public: |
| 64 | CustomDelegate(BreakpointWidget* parent) : QStyledItemDelegate(parent) {} |
| 65 | |
| 66 | private: |
| 67 | void paint(QPainter* painter, const QStyleOptionViewItem& option, |
| 68 | const QModelIndex& index) const override |
| 69 | { |
| 70 | Q_ASSERT(index.isValid()); |
| 71 | |
| 72 | // Fetch normal drawing logic. |
| 73 | QStyleOptionViewItem opt = option; |
| 74 | initStyleOption(&opt, index); |
| 75 | |
| 76 | // Disable drawing icon the normal way. |
| 77 | opt.icon = QIcon(); |
| 78 | opt.decorationSize = QSize(0, 0); |
| 79 | |
| 80 | // Default draw command for paint. |
| 81 | QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter, 0); |
| 82 | |
| 83 | // Draw pixmap at the center of the tablewidget cell |
| 84 | QPixmap pix = qvariant_cast<QPixmap>(index.data(Qt::DecorationRole)); |
| 85 | if (!pix.isNull()) |
| 86 | { |
| 87 | const QRect r = option.rect; |
| 88 | const QSize size = pix.deviceIndependentSize().toSize(); |
| 89 | const QPoint p = QPoint((r.width() - size.width()) / 2, (r.height() - size.height()) / 2); |
| 90 | painter->drawPixmap(r.topLeft() + p, pix); |
| 91 | } |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | BreakpointWidget::BreakpointWidget(QWidget* parent) |
| 96 | : QDockWidget(parent), m_system(Core::System::GetInstance()) |
| 97 | { |
| 98 | setWindowTitle(tr("Breakpoints")); |
| 99 | setObjectName(QStringLiteral("breakpoints")); |
| 100 | |
| 101 | setHidden(!Settings::Instance().IsBreakpointsVisible() || |
| 102 | !Settings::Instance().IsDebugModeEnabled()); |
| 103 | |
| 104 | setAllowedAreas(Qt::AllDockWidgetAreas); |
| 105 | |
| 106 | CreateWidgets(); |
| 107 | |
| 108 | auto& settings = Settings::GetQSettings(); |
| 109 | |
| 110 | restoreGeometry(settings.value(QStringLiteral("breakpointwidget/geometry")).toByteArray()); |
| 111 | // macOS: setHidden() needs to be evaluated before setFloating() for proper window presentation |
| 112 | // according to Settings |
| 113 | setFloating(settings.value(QStringLiteral("breakpointwidget/floating")).toBool()); |
| 114 | |
| 115 | connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, [this](Core::State state) { |
| 116 | UpdateButtonsEnabled(); |
| 117 | if (state == Core::State::Uninitialized) |
| 118 | Update(); |
| 119 | }); |
| 120 | |
| 121 | connect(m_table, &QTableWidget::itemChanged, this, &BreakpointWidget::OnItemChanged); |
| 122 | connect(&Settings::Instance(), &Settings::DebugFontChanged, this, |
| 123 | &BreakpointWidget::OnDebugFontChanged); |
| 124 | |
| 125 | connect(&Settings::Instance(), &Settings::BreakpointsVisibilityChanged, this, |
| 126 | [this](bool visible) { setHidden(!visible); }); |
| 127 | |
| 128 | connect(&Settings::Instance(), &Settings::DebugModeToggled, this, [this](bool enabled) { |
| 129 | setHidden(!enabled || !Settings::Instance().IsBreakpointsVisible()); |
| 130 | }); |
| 131 | |
| 132 | connect(&Settings::Instance(), &Settings::ThemeChanged, this, [this] { |
| 133 | UpdateIcons(); |
| 134 | Update(); |
| 135 | }); |
| 136 | |
| 137 | connect(Host::GetInstance(), &Host::PPCSymbolsChanged, this, &BreakpointWidget::Update); |
| 138 | connect(Host::GetInstance(), &Host::PPCBreakpointsChanged, this, &BreakpointWidget::Update); |
| 139 | |
| 140 | UpdateIcons(); |
| 141 | } |
| 142 | |
| 143 | BreakpointWidget::~BreakpointWidget() |
| 144 | { |
| 145 | auto& settings = Settings::GetQSettings(); |
| 146 | |
| 147 | settings.setValue(QStringLiteral("breakpointwidget/geometry"), saveGeometry()); |
| 148 | settings.setValue(QStringLiteral("breakpointwidget/floating"), isFloating()); |
| 149 | } |
| 150 | |
| 151 | void BreakpointWidget::CreateWidgets() |
| 152 | { |
| 153 | m_toolbar = new QToolBar; |
| 154 | m_toolbar->setContentsMargins(0, 0, 0, 0); |
| 155 | m_toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); |
| 156 | |
| 157 | m_table = new QTableWidget; |
| 158 | m_table->setItemDelegate(new CustomDelegate(this)); |
| 159 | m_table->setTabKeyNavigation(false); |
| 160 | m_table->setContentsMargins(0, 0, 0, 0); |
| 161 | m_table->setColumnCount(10); |
| 162 | m_table->setSelectionMode(QAbstractItemView::NoSelection); |
| 163 | m_table->verticalHeader()->hide(); |
| 164 | |
| 165 | OnDebugFontChanged(Settings::Instance().GetDebugFont()); |
| 166 | |
| 167 | connect(m_table, &QTableWidget::itemClicked, this, &BreakpointWidget::OnClicked); |
| 168 | connect(m_table, &QTableWidget::customContextMenuRequested, this, |
| 169 | &BreakpointWidget::OnContextMenu); |
| 170 | |
| 171 | m_table->setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu); |
| 172 | |
| 173 | auto* layout = new QVBoxLayout; |
| 174 | |
| 175 | layout->addWidget(m_toolbar); |
| 176 | layout->addWidget(m_table); |
| 177 | layout->setContentsMargins(2, 2, 2, 2); |
| 178 | layout->setSpacing(0); |
| 179 | |
| 180 | m_enabled = m_toolbar->addAction(tr("Disable"), this, &BreakpointWidget::OnToggleBreaking); |
| 181 | m_new = m_toolbar->addAction(tr("New"), this, &BreakpointWidget::OnNewBreakpoint); |
| 182 | m_clear = m_toolbar->addAction(tr("Clear"), this, &BreakpointWidget::OnClear); |
| 183 | |
| 184 | m_load = m_toolbar->addAction(tr("Load"), this, &BreakpointWidget::OnLoad); |
| 185 | m_save = m_toolbar->addAction(tr("Save"), this, &BreakpointWidget::OnSave); |
| 186 | |
| 187 | m_load->setEnabled(false); |
| 188 | m_save->setEnabled(false); |
| 189 | |
| 190 | QWidget* widget = new QWidget; |
| 191 | widget->setLayout(layout); |
| 192 | |
| 193 | setWidget(widget); |
| 194 | } |
| 195 | |
| 196 | void BreakpointWidget::UpdateIcons() |
| 197 | { |
| 198 | if (m_system.GetPowerPC().GetBreakPoints().IsBreakingEnabled()) |
| 199 | m_enabled->setIcon(Resources::GetThemeIcon("pause")); |
| 200 | else |
| 201 | m_enabled->setIcon(Resources::GetThemeIcon("play")); |
| 202 | m_new->setIcon(Resources::GetThemeIcon("debugger_add_breakpoint")); |
| 203 | m_clear->setIcon(Resources::GetThemeIcon("debugger_clear")); |
| 204 | m_load->setIcon(Resources::GetThemeIcon("debugger_load")); |
| 205 | m_save->setIcon(Resources::GetThemeIcon("debugger_save")); |
| 206 | } |
| 207 | |
| 208 | void BreakpointWidget::closeEvent(QCloseEvent*) |
| 209 | { |
| 210 | Settings::Instance().SetBreakpointsVisible(false); |
| 211 | } |
| 212 | |
| 213 | void BreakpointWidget::showEvent(QShowEvent* event) |
| 214 | { |
| 215 | UpdateButtonsEnabled(); |
| 216 | Update(); |
| 217 | } |
| 218 | |
| 219 | void BreakpointWidget::OnClicked(QTableWidgetItem* item) |
| 220 | { |
| 221 | if (!item) |
| 222 | return; |
| 223 | |
| 224 | if (item->column() == SYMBOL_COLUMN || item->column() == ADDRESS_COLUMN || |
| 225 | item->column() == END_ADDRESS_COLUMN) |
| 226 | { |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | const u32 address = static_cast<u32>(m_table->item(item->row(), 0)->data(ADDRESS_ROLE).toUInt()); |
| 231 | |
| 232 | if (item->column() == ENABLED_COLUMN) |
| 233 | { |
| 234 | if (item->data(IS_MEMCHECK_ROLE).toBool()) |
| 235 | m_system.GetPowerPC().GetMemChecks().ToggleEnable(address); |
| 236 | else |
| 237 | m_system.GetPowerPC().GetBreakPoints().ToggleEnable(address); |
| 238 | |
| 239 | emit Host::GetInstance()->PPCBreakpointsChanged(); |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | std::optional<QString> string = std::nullopt; |
| 244 | |
| 245 | if (item->column() == CONDITION_COLUMN) |
| 246 | { |
| 247 | bool ok; |
| 248 | QString new_text = QInputDialog::getMultiLineText( |
| 249 | this, tr("Edit Conditional"), tr("Edit conditional expression"), item->text(), &ok); |
| 250 | |
| 251 | if (!ok || item->text() == new_text) |
| 252 | return; |
| 253 | |
| 254 | // If new_text is empty, leaving string as nullopt will clear the conditional. |
| 255 | if (!new_text.isEmpty()) |
| 256 | string = new_text; |
| 257 | } |
| 258 | |
| 259 | if (m_table->item(item->row(), 0)->data(IS_MEMCHECK_ROLE).toBool()) |
| 260 | EditMBP(address, item->column(), string); |
| 261 | else |
| 262 | EditBreakpoint(address, item->column(), string); |
| 263 | } |
| 264 | |
| 265 | void BreakpointWidget::UpdateButtonsEnabled() |
| 266 | { |
| 267 | if (!isVisible()) |
| 268 | return; |
| 269 | |
| 270 | const bool is_running = Core::IsRunning(m_system); |
| 271 | m_load->setEnabled(is_running); |
| 272 | m_save->setEnabled(is_running); |
| 273 | } |
| 274 | |
| 275 | void BreakpointWidget::Update() |
| 276 | { |
| 277 | if (!isVisible()) |
| 278 | return; |
| 279 | |
| 280 | const QSignalBlocker blocker(m_table); |
| 281 | |
| 282 | m_table->clear(); |
| 283 | m_table->setHorizontalHeaderLabels({tr("Active"), tr("Type"), tr("Function"), tr("Address"), |
| 284 | // i18n: The address where a breakpoint ends |
| 285 | tr("End Addr"), tr("Break"), tr("Log"), tr("Read"), |
| 286 | tr("Write"), tr("Condition")}); |
| 287 | m_table->horizontalHeader()->setStretchLastSection(true); |
| 288 | |
| 289 | // Get row height for icons |
| 290 | m_table->setRowCount(1); |
| 291 | const int height = m_table->rowHeight(0); |
| 292 | |
| 293 | int i = 0; |
| 294 | m_table->setRowCount(i); |
| 295 | |
| 296 | // Create icon based on row height, downscaled for whitespace padding. |
| 297 | const int downscale = static_cast<int>(0.8 * height); |
| 298 | QPixmap enabled_icon = |
| 299 | Resources::GetThemeIcon("debugger_breakpoint").pixmap(QSize(downscale, downscale)); |
| 300 | |
| 301 | auto& power_pc = m_system.GetPowerPC(); |
| 302 | auto& breakpoints = power_pc.GetBreakPoints(); |
| 303 | |
| 304 | if (!breakpoints.IsBreakingEnabled()) |
| 305 | { |
| 306 | // Use QPainter to draw a transparent hole in the center |
| 307 | QImage image = enabled_icon.toImage().convertToFormat(QImage::Format_ARGB32_Premultiplied); |
| 308 | QPainter painter(&image); |
| 309 | painter.setCompositionMode(QPainter::CompositionMode_Source); |
| 310 | painter.setRenderHint(QPainter::Antialiasing, true); |
| 311 | painter.setPen(Qt::NoPen); |
| 312 | painter.setBrush(Qt::transparent); |
| 313 | |
| 314 | const float icon_radius = static_cast<float>(image.height()) / 2.0f; |
| 315 | const float gap_radius = icon_radius / 2.0f; |
| 316 | const QPointF center(icon_radius, icon_radius); |
| 317 | |
| 318 | painter.drawEllipse(center, gap_radius, gap_radius); |
| 319 | painter.end(); |
| 320 | |
| 321 | enabled_icon = QPixmap::fromImage(image); |
| 322 | } |
| 323 | |
| 324 | const auto create_item = [](const QString& string = {}) { |
| 325 | QTableWidgetItem* item = new QTableWidgetItem(string); |
| 326 | item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); |
| 327 | return item; |
| 328 | }; |
| 329 | |
| 330 | QTableWidgetItem empty_item = QTableWidgetItem(); |
| 331 | empty_item.setFlags(Qt::NoItemFlags); |
| 332 | QTableWidgetItem icon_item = QTableWidgetItem(); |
| 333 | icon_item.setData(Qt::DecorationRole, enabled_icon); |
| 334 | QTableWidgetItem disabled_item = QTableWidgetItem(); |
| 335 | disabled_item.setFlags(Qt::NoItemFlags); |
| 336 | |
| 337 | const QColor disabled_color = |
| 338 | Settings::Instance().IsThemeDark() ? QColor(75, 75, 75) : QColor(225, 225, 225); |
| 339 | disabled_item.setBackground(disabled_color); |
| 340 | |
| 341 | auto& memchecks = power_pc.GetMemChecks(); |
| 342 | auto& ppc_symbol_db = power_pc.GetSymbolDB(); |
| 343 | |
| 344 | // Breakpoints |
| 345 | for (const auto& bp : breakpoints.GetBreakPoints()) |
| 346 | { |
| 347 | m_table->setRowCount(i + 1); |
| 348 | |
| 349 | auto* active = create_item(); |
| 350 | |
| 351 | active->setData(ADDRESS_ROLE, bp.address); |
| 352 | active->setData(IS_MEMCHECK_ROLE, false); |
| 353 | if (bp.is_enabled) |
| 354 | active->setData(Qt::DecorationRole, enabled_icon); |
| 355 | |
| 356 | m_table->setItem(i, ENABLED_COLUMN, active); |
| 357 | m_table->setItem(i, TYPE_COLUMN, create_item(QStringLiteral("BP"))); |
| 358 | |
| 359 | auto* symbol_item = create_item(); |
| 360 | |
| 361 | if (const Common::Symbol* const symbol = ppc_symbol_db.GetSymbolFromAddr(bp.address)) |
| 362 | symbol_item->setText( |
| 363 | QString::fromStdString(symbol->name).simplified().remove(QStringLiteral(" "))); |
| 364 | |
| 365 | m_table->setItem(i, SYMBOL_COLUMN, symbol_item); |
| 366 | |
| 367 | auto* address_item = create_item(QStringLiteral("%1").arg(bp.address, 8, 16, QLatin1Char('0'))); |
| 368 | address_item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable); |
| 369 | |
| 370 | m_table->setItem(i, ADDRESS_COLUMN, address_item); |
| 371 | m_table->setItem(i, BREAK_COLUMN, bp.break_on_hit ? icon_item.clone() : empty_item.clone()); |
| 372 | m_table->setItem(i, LOG_COLUMN, bp.log_on_hit ? icon_item.clone() : empty_item.clone()); |
| 373 | |
| 374 | m_table->setItem(i, END_ADDRESS_COLUMN, disabled_item.clone()); |
| 375 | m_table->setItem(i, READ_COLUMN, disabled_item.clone()); |
| 376 | m_table->setItem(i, WRITE_COLUMN, disabled_item.clone()); |
| 377 | |
| 378 | m_table->setItem( |
| 379 | i, CONDITION_COLUMN, |
| 380 | create_item(bp.condition ? QString::fromStdString(bp.condition->GetText()) : QString())); |
| 381 | |
| 382 | // Color rows that are effectively disabled. |
| 383 | if (!bp.is_enabled || (!bp.log_on_hit && !bp.break_on_hit)) |
| 384 | { |
| 385 | for (int col = 0; col < m_table->columnCount(); col++) |
| 386 | m_table->item(i, col)->setBackground(disabled_color); |
| 387 | } |
| 388 | |
| 389 | i++; |
| 390 | } |
| 391 | |
| 392 | m_table->sortItems(ADDRESS_COLUMN); |
| 393 | |
| 394 | // Memory Breakpoints |
| 395 | for (const auto& mbp : memchecks.GetMemChecks()) |
| 396 | { |
| 397 | m_table->setRowCount(i + 1); |
| 398 | auto* active = create_item(); |
| 399 | active->setData(ADDRESS_ROLE, mbp.start_address); |
| 400 | active->setData(IS_MEMCHECK_ROLE, true); |
| 401 | if (mbp.is_enabled) |
| 402 | active->setData(Qt::DecorationRole, enabled_icon); |
| 403 | |
| 404 | m_table->setItem(i, ENABLED_COLUMN, active); |
| 405 | m_table->setItem(i, TYPE_COLUMN, create_item(QStringLiteral("MBP"))); |
| 406 | |
| 407 | auto* symbol_item = create_item(); |
| 408 | |
| 409 | if (const Common::Symbol* const symbol = ppc_symbol_db.GetSymbolFromAddr(mbp.start_address)) |
| 410 | { |
| 411 | symbol_item->setText( |
| 412 | QString::fromStdString(symbol->name).simplified().remove(QStringLiteral(" "))); |
| 413 | } |
| 414 | |
| 415 | m_table->setItem(i, SYMBOL_COLUMN, symbol_item); |
| 416 | |
| 417 | auto* start_address_item = |
| 418 | create_item(QStringLiteral("%1").arg(mbp.start_address, 8, 16, QLatin1Char('0'))); |
| 419 | start_address_item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable); |
| 420 | |
| 421 | m_table->setItem(i, ADDRESS_COLUMN, start_address_item); |
| 422 | |
| 423 | auto* end_address_item = |
| 424 | create_item(QStringLiteral("%1").arg(mbp.end_address, 8, 16, QLatin1Char('0'))); |
| 425 | end_address_item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable); |
| 426 | end_address_item->setData(ADDRESS_ROLE, mbp.end_address); |
| 427 | |
| 428 | m_table->setItem(i, END_ADDRESS_COLUMN, end_address_item); |
| 429 | |
| 430 | m_table->setItem(i, BREAK_COLUMN, mbp.break_on_hit ? icon_item.clone() : empty_item.clone()); |
| 431 | m_table->setItem(i, LOG_COLUMN, mbp.log_on_hit ? icon_item.clone() : empty_item.clone()); |
| 432 | m_table->setItem(i, READ_COLUMN, mbp.is_break_on_read ? icon_item.clone() : empty_item.clone()); |
| 433 | m_table->setItem(i, WRITE_COLUMN, |
| 434 | mbp.is_break_on_write ? icon_item.clone() : empty_item.clone()); |
| 435 | |
| 436 | m_table->setItem( |
| 437 | i, CONDITION_COLUMN, |
| 438 | create_item(mbp.condition ? QString::fromStdString(mbp.condition->GetText()) : QString())); |
| 439 | |
| 440 | // Color rows that are effectively disabled. |
| 441 | if (!mbp.is_enabled || (!mbp.is_break_on_write && !mbp.is_break_on_read) || |
| 442 | (!mbp.break_on_hit && !mbp.log_on_hit)) |
| 443 | { |
| 444 | for (int col = 0; col < m_table->columnCount(); col++) |
| 445 | m_table->item(i, col)->setBackground(disabled_color); |
| 446 | } |
| 447 | |
| 448 | i++; |
| 449 | } |
| 450 | |
| 451 | m_table->resizeColumnToContents(ENABLED_COLUMN); |
| 452 | m_table->resizeColumnToContents(TYPE_COLUMN); |
| 453 | m_table->resizeColumnToContents(BREAK_COLUMN); |
| 454 | m_table->resizeColumnToContents(LOG_COLUMN); |
| 455 | m_table->resizeColumnToContents(READ_COLUMN); |
| 456 | m_table->resizeColumnToContents(WRITE_COLUMN); |
| 457 | } |
| 458 | |
| 459 | void BreakpointWidget::OnClear() |
| 460 | { |
| 461 | m_system.GetPowerPC().GetDebugInterface().ClearAllBreakpoints(); |
| 462 | { |
| 463 | const QSignalBlocker blocker(Settings::Instance()); |
| 464 | m_system.GetPowerPC().GetDebugInterface().ClearAllMemChecks(); |
| 465 | } |
| 466 | |
| 467 | m_table->setRowCount(0); |
| 468 | |
| 469 | emit Host::GetInstance()->PPCBreakpointsChanged(); |
| 470 | } |
| 471 | |
| 472 | void BreakpointWidget::OnNewBreakpoint() |
| 473 | { |
| 474 | BreakpointDialog* dialog = new BreakpointDialog(this); |
| 475 | dialog->setAttribute(Qt::WA_DeleteOnClose, true); |
| 476 | dialog->exec(); |
| 477 | } |
| 478 | |
| 479 | void BreakpointWidget::OnEditBreakpoint(u32 address, bool is_instruction_bp) |
| 480 | { |
| 481 | if (is_instruction_bp) |
| 482 | { |
| 483 | auto* dialog = new BreakpointDialog( |
| 484 | this, m_system.GetPowerPC().GetBreakPoints().GetRegularBreakpoint(address)); |
| 485 | dialog->setAttribute(Qt::WA_DeleteOnClose, true); |
| 486 | dialog->exec(); |
| 487 | } |
| 488 | else |
| 489 | { |
| 490 | auto* dialog = |
| 491 | new BreakpointDialog(this, m_system.GetPowerPC().GetMemChecks().GetMemCheck(address)); |
| 492 | dialog->setAttribute(Qt::WA_DeleteOnClose, true); |
| 493 | dialog->exec(); |
| 494 | } |
| 495 | |
| 496 | emit Host::GetInstance()->PPCBreakpointsChanged(); |
| 497 | } |
| 498 | |
| 499 | void BreakpointWidget::OnToggleBreaking() |
| 500 | { |
| 501 | auto& breakpoints = m_system.GetPowerPC().GetBreakPoints(); |
| 502 | auto& memchecks = m_system.GetPowerPC().GetMemChecks(); |
| 503 | // Memcheck's HasAny() will report no memchecks while breaking is disabled, so only check when |
| 504 | // breaking is true. |
| 505 | bool has_memory_bp; |
| 506 | |
| 507 | // Currently toggles all code and memory breakpoints. Could be split if needed. |
| 508 | if (breakpoints.IsBreakingEnabled()) |
| 509 | { |
| 510 | has_memory_bp = memchecks.HasAny(); |
| 511 | breakpoints.EnableBreaking(false); |
| 512 | memchecks.EnableBreaking(false); |
| 513 | m_enabled->setText(tr("Enable")); |
| 514 | m_enabled->setIcon(Resources::GetThemeIcon("play")); |
| 515 | } |
| 516 | else |
| 517 | { |
| 518 | breakpoints.EnableBreaking(true); |
| 519 | memchecks.EnableBreaking(true); |
| 520 | has_memory_bp = memchecks.HasAny(); |
| 521 | m_enabled->setText(tr("Disable")); |
| 522 | m_enabled->setIcon(Resources::GetThemeIcon("pause")); |
| 523 | } |
| 524 | |
| 525 | if (has_memory_bp || !breakpoints.GetBreakPoints().empty()) |
| 526 | emit Host::GetInstance()->PPCBreakpointsChanged(); |
| 527 | } |
| 528 | |
| 529 | void BreakpointWidget::OnLoad() |
| 530 | { |
| 531 | Common::IniFile ini; |
| 532 | if (!ini.Load(File::GetUserPath(D_GAMESETTINGS_IDX) + SConfig::GetInstance().GetGameID() + ".ini", |
| 533 | false)) |
| 534 | { |
| 535 | return; |
| 536 | } |
| 537 | |
| 538 | BreakPoints::TBreakPointsStr new_bps; |
| 539 | if (ini.GetLines("BreakPoints", &new_bps, false)) |
| 540 | { |
| 541 | auto& breakpoints = m_system.GetPowerPC().GetBreakPoints(); |
| 542 | breakpoints.Clear(); |
| 543 | breakpoints.AddFromStrings(new_bps); |
| 544 | } |
| 545 | |
| 546 | MemChecks::TMemChecksStr new_mcs; |
| 547 | if (ini.GetLines("MemoryBreakPoints", &new_mcs, false)) |
| 548 | { |
| 549 | auto& memchecks = m_system.GetPowerPC().GetMemChecks(); |
| 550 | memchecks.Clear(); |
| 551 | const QSignalBlocker blocker(Settings::Instance()); |
| 552 | memchecks.AddFromStrings(new_mcs); |
| 553 | } |
| 554 | |
| 555 | emit Host::GetInstance()->PPCBreakpointsChanged(); |
| 556 | } |
| 557 | |
| 558 | void BreakpointWidget::OnSave() |
| 559 | { |
| 560 | Common::IniFile ini; |
| 561 | ini.Load(File::GetUserPath(D_GAMESETTINGS_IDX) + SConfig::GetInstance().GetGameID() + ".ini", |
| 562 | false); |
| 563 | ini.SetLines("BreakPoints", m_system.GetPowerPC().GetBreakPoints().GetStrings()); |
| 564 | ini.SetLines("MemoryBreakPoints", m_system.GetPowerPC().GetMemChecks().GetStrings()); |
| 565 | ini.Save(File::GetUserPath(D_GAMESETTINGS_IDX) + SConfig::GetInstance().GetGameID() + ".ini"); |
| 566 | } |
| 567 | |
| 568 | void BreakpointWidget::OnContextMenu(const QPoint& pos) |
| 569 | { |
| 570 | const auto row = m_table->rowAt(pos.y()); |
| 571 | const auto& selected_item = m_table->item(row, 0); |
| 572 | if (selected_item == nullptr) |
| 573 | return; |
| 574 | |
| 575 | const auto bp_address = static_cast<u32>(selected_item->data(ADDRESS_ROLE).toUInt()); |
| 576 | const auto is_memory_breakpoint = selected_item->data(IS_MEMCHECK_ROLE).toBool(); |
| 577 | |
| 578 | auto* menu = new QMenu(this); |
| 579 | menu->setAttribute(Qt::WA_DeleteOnClose, true); |
| 580 | |
| 581 | if (!is_memory_breakpoint) |
| 582 | { |
| 583 | const auto& inst_breakpoints = m_system.GetPowerPC().GetBreakPoints().GetBreakPoints(); |
| 584 | if (!Common::Contains(inst_breakpoints, bp_address, &TBreakPoint::address)) |
| 585 | return; |
| 586 | |
| 587 | menu->addAction(tr("Show in Code"), [this, bp_address] { emit ShowCode(bp_address); }); |
| 588 | menu->addAction(tr("Edit..."), [this, bp_address] { OnEditBreakpoint(bp_address, true); }); |
| 589 | menu->addAction(tr("Delete"), [this, &bp_address] { |
| 590 | m_system.GetPowerPC().GetBreakPoints().Remove(bp_address); |
| 591 | emit Host::GetInstance()->PPCBreakpointsChanged(); |
| 592 | }); |
| 593 | } |
| 594 | else |
| 595 | { |
| 596 | const auto& memory_breakpoints = m_system.GetPowerPC().GetMemChecks().GetMemChecks(); |
| 597 | if (!Common::Contains(memory_breakpoints, bp_address, &TMemCheck::start_address)) |
| 598 | return; |
| 599 | |
| 600 | menu->addAction(tr("Show in Memory"), [this, bp_address] { emit ShowMemory(bp_address); }); |
| 601 | menu->addAction(tr("Edit..."), [this, bp_address] { OnEditBreakpoint(bp_address, false); }); |
| 602 | menu->addAction(tr("Delete"), [this, &bp_address] { |
| 603 | const QSignalBlocker blocker(Settings::Instance()); |
| 604 | m_system.GetPowerPC().GetMemChecks().Remove(bp_address); |
| 605 | emit Host::GetInstance()->PPCBreakpointsChanged(); |
| 606 | }); |
| 607 | } |
| 608 | |
| 609 | menu->exec(QCursor::pos()); |
| 610 | } |
| 611 | |
| 612 | void BreakpointWidget::OnItemChanged(QTableWidgetItem* item) |
| 613 | { |
| 614 | if (item->column() != ADDRESS_COLUMN && item->column() != END_ADDRESS_COLUMN) |
| 615 | return; |
| 616 | |
| 617 | bool ok; |
| 618 | const u32 new_address = item->text().toUInt(&ok, 16); |
| 619 | if (!ok) |
| 620 | return; |
| 621 | |
| 622 | const bool is_code_bp = !m_table->item(item->row(), 0)->data(IS_MEMCHECK_ROLE).toBool(); |
| 623 | const u32 base_address = |
| 624 | static_cast<u32>(m_table->item(item->row(), 0)->data(ADDRESS_ROLE).toUInt()); |
| 625 | |
| 626 | if (is_code_bp) |
| 627 | { |
| 628 | if (item->column() != ADDRESS_COLUMN || new_address == base_address) |
| 629 | return; |
| 630 | |
| 631 | EditBreakpoint(base_address, item->column(), item->text()); |
| 632 | } |
| 633 | else |
| 634 | { |
| 635 | const u32 end_address = static_cast<u32>( |
| 636 | m_table->item(item->row(), END_ADDRESS_COLUMN)->data(ADDRESS_ROLE).toUInt()); |
| 637 | |
| 638 | // Need to check that the start/base address is always <= end_address. |
| 639 | if ((item->column() == ADDRESS_COLUMN && new_address == base_address) || |
| 640 | (item->column() == END_ADDRESS_COLUMN && new_address == end_address)) |
| 641 | { |
| 642 | return; |
| 643 | } |
| 644 | |
| 645 | if ((item->column() == ADDRESS_COLUMN && new_address <= end_address) || |
| 646 | (item->column() == END_ADDRESS_COLUMN && new_address >= base_address)) |
| 647 | { |
| 648 | EditMBP(base_address, item->column(), item->text()); |
| 649 | } |
| 650 | else |
| 651 | { |
| 652 | // Removes invalid text from cell. |
| 653 | Update(); |
| 654 | } |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | void BreakpointWidget::OnDebugFontChanged(const QFont& font) |
| 659 | { |
| 660 | m_table->setFont(font); |
| 661 | m_table->verticalHeader()->setDefaultSectionSize(m_table->fontMetrics().height()); |
| 662 | Update(); |
| 663 | } |
| 664 | |
| 665 | void BreakpointWidget::AddBP(u32 addr) |
| 666 | { |
| 667 | AddBP(addr, true, true, {}); |
| 668 | } |
| 669 | |
| 670 | void BreakpointWidget::AddBP(u32 addr, bool break_on_hit, bool log_on_hit, const QString& condition) |
| 671 | { |
| 672 | m_system.GetPowerPC().GetBreakPoints().Add( |
| 673 | addr, break_on_hit, log_on_hit, |
| 674 | !condition.isEmpty() ? Expression::TryParse(condition.toUtf8().constData()) : std::nullopt); |
| 675 | |
| 676 | emit Host::GetInstance()->PPCBreakpointsChanged(); |
| 677 | } |
| 678 | |
| 679 | void BreakpointWidget::EditBreakpoint(u32 address, int edit, std::optional<QString> string) |
| 680 | { |
| 681 | TBreakPoint bp; |
| 682 | const TBreakPoint* old_bp = m_system.GetPowerPC().GetBreakPoints().GetRegularBreakpoint(address); |
| 683 | bp.is_enabled = edit == ENABLED_COLUMN ? !old_bp->is_enabled : old_bp->is_enabled; |
| 684 | bp.log_on_hit = edit == LOG_COLUMN ? !old_bp->log_on_hit : old_bp->log_on_hit; |
| 685 | bp.break_on_hit = edit == BREAK_COLUMN ? !old_bp->break_on_hit : old_bp->break_on_hit; |
| 686 | |
| 687 | if (edit == ADDRESS_COLUMN && string.has_value()) |
| 688 | { |
| 689 | bool ok; |
| 690 | const u32 new_address = string.value().toUInt(&ok, 16); |
| 691 | if (!ok) |
| 692 | return; |
| 693 | |
| 694 | bp.address = new_address; |
| 695 | } |
| 696 | else |
| 697 | { |
| 698 | bp.address = address; |
| 699 | } |
| 700 | |
| 701 | if (edit == CONDITION_COLUMN && string.has_value()) |
| 702 | bp.condition = Expression::TryParse(string.value().toUtf8().constData()); |
| 703 | else if (old_bp->condition.has_value() && edit != CONDITION_COLUMN) |
| 704 | bp.condition = Expression::TryParse(old_bp->condition.value().GetText()); |
| 705 | |
| 706 | // Unlike MBPs it Add() for TBreakpoint doesn't check to see if it already exists. |
| 707 | m_system.GetPowerPC().GetBreakPoints().Remove(address); |
| 708 | m_system.GetPowerPC().GetBreakPoints().Add(std::move(bp)); |
| 709 | |
| 710 | emit Host::GetInstance()->PPCBreakpointsChanged(); |
| 711 | } |
| 712 | |
| 713 | void BreakpointWidget::AddAddressMBP(u32 addr, bool on_read, bool on_write, bool do_log, |
| 714 | bool do_break, const QString& condition) |
| 715 | { |
| 716 | TMemCheck check; |
| 717 | |
| 718 | check.start_address = addr; |
| 719 | check.end_address = addr; |
| 720 | check.is_ranged = false; |
| 721 | check.is_break_on_read = on_read; |
| 722 | check.is_break_on_write = on_write; |
| 723 | check.log_on_hit = do_log; |
| 724 | check.break_on_hit = do_break; |
| 725 | check.condition = |
| 726 | !condition.isEmpty() ? Expression::TryParse(condition.toUtf8().constData()) : std::nullopt; |
| 727 | { |
| 728 | const QSignalBlocker blocker(Settings::Instance()); |
| 729 | m_system.GetPowerPC().GetMemChecks().Add(std::move(check)); |
| 730 | } |
| 731 | |
| 732 | emit Host::GetInstance()->PPCBreakpointsChanged(); |
| 733 | } |
| 734 | |
| 735 | void BreakpointWidget::AddRangedMBP(u32 from, u32 to, bool on_read, bool on_write, bool do_log, |
| 736 | bool do_break, const QString& condition) |
| 737 | { |
| 738 | TMemCheck check; |
| 739 | |
| 740 | check.start_address = from; |
| 741 | check.end_address = to; |
| 742 | check.is_ranged = true; |
| 743 | check.is_break_on_read = on_read; |
| 744 | check.is_break_on_write = on_write; |
| 745 | check.log_on_hit = do_log; |
| 746 | check.break_on_hit = do_break; |
| 747 | check.condition = |
| 748 | !condition.isEmpty() ? Expression::TryParse(condition.toUtf8().constData()) : std::nullopt; |
| 749 | { |
| 750 | const QSignalBlocker blocker(Settings::Instance()); |
| 751 | m_system.GetPowerPC().GetMemChecks().Add(std::move(check)); |
| 752 | } |
| 753 | |
| 754 | emit Host::GetInstance()->PPCBreakpointsChanged(); |
| 755 | } |
| 756 | |
| 757 | void BreakpointWidget::EditMBP(u32 address, int edit, std::optional<QString> string) |
| 758 | { |
| 759 | bool address_changed = false; |
| 760 | |
| 761 | TMemCheck mbp; |
| 762 | const TMemCheck* old_mbp = m_system.GetPowerPC().GetMemChecks().GetMemCheck(address); |
| 763 | mbp.is_enabled = edit == ENABLED_COLUMN ? !old_mbp->is_enabled : old_mbp->is_enabled; |
| 764 | mbp.log_on_hit = edit == LOG_COLUMN ? !old_mbp->log_on_hit : old_mbp->log_on_hit; |
| 765 | mbp.break_on_hit = edit == BREAK_COLUMN ? !old_mbp->break_on_hit : old_mbp->break_on_hit; |
| 766 | mbp.is_break_on_read = |
| 767 | edit == READ_COLUMN ? !old_mbp->is_break_on_read : old_mbp->is_break_on_read; |
| 768 | mbp.is_break_on_write = |
| 769 | edit == WRITE_COLUMN ? !old_mbp->is_break_on_write : old_mbp->is_break_on_write; |
| 770 | |
| 771 | if ((edit == ADDRESS_COLUMN || edit == END_ADDRESS_COLUMN) && string.has_value()) |
| 772 | { |
| 773 | bool ok; |
| 774 | const u32 new_address = string.value().toUInt(&ok, 16); |
| 775 | if (!ok) |
| 776 | return; |
| 777 | |
| 778 | if (edit == ADDRESS_COLUMN) |
| 779 | { |
| 780 | mbp.start_address = new_address; |
| 781 | mbp.end_address = old_mbp->end_address; |
| 782 | address_changed = true; |
| 783 | } |
| 784 | else if (edit == END_ADDRESS_COLUMN) |
| 785 | { |
| 786 | // Will update existing mbp, so does not use address_changed bool. |
| 787 | mbp.start_address = old_mbp->start_address; |
| 788 | mbp.end_address = new_address; |
| 789 | } |
| 790 | } |
| 791 | else |
| 792 | { |
| 793 | mbp.start_address = old_mbp->start_address; |
| 794 | mbp.end_address = old_mbp->end_address; |
| 795 | } |
| 796 | |
| 797 | mbp.is_ranged = mbp.start_address != mbp.end_address; |
| 798 | |
| 799 | if (edit == CONDITION_COLUMN && string.has_value()) |
| 800 | mbp.condition = Expression::TryParse(string.value().toUtf8().constData()); |
| 801 | else if (old_mbp->condition.has_value() && edit != CONDITION_COLUMN) |
| 802 | mbp.condition = Expression::TryParse(old_mbp->condition.value().GetText()); |
| 803 | |
| 804 | { |
| 805 | const QSignalBlocker blocker(Settings::Instance()); |
| 806 | m_system.GetPowerPC().GetMemChecks().Add(std::move(mbp)); |
| 807 | if (address_changed) |
| 808 | m_system.GetPowerPC().GetMemChecks().Remove(address); |
| 809 | } |
| 810 | |
| 811 | emit Host::GetInstance()->PPCBreakpointsChanged(); |
| 812 | } |