| 1 | // Copyright 2024 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Debugger/BranchWatchDialog.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <optional> |
| 8 | #include <ranges> |
| 9 | #include <utility> |
| 10 | |
| 11 | #include <QApplication> |
| 12 | #include <QCheckBox> |
| 13 | #include <QClipboard> |
| 14 | #include <QGridLayout> |
| 15 | #include <QGroupBox> |
| 16 | #include <QHeaderView> |
| 17 | #include <QLineEdit> |
| 18 | #include <QMenu> |
| 19 | #include <QMenuBar> |
| 20 | #include <QPushButton> |
| 21 | #include <QShortcut> |
| 22 | #include <QSortFilterProxyModel> |
| 23 | #include <QStatusBar> |
| 24 | #include <QString> |
| 25 | #include <QTableView> |
| 26 | #include <QTimer> |
| 27 | #include <QToolBar> |
| 28 | #include <QVBoxLayout> |
| 29 | #include <QVariant> |
| 30 | #include <fmt/format.h> |
| 31 | |
| 32 | #include "Common/CommonFuncs.h" |
| 33 | #include "Common/CommonTypes.h" |
| 34 | #include "Common/FileUtil.h" |
| 35 | #include "Common/IOFile.h" |
| 36 | #include "Core/ConfigManager.h" |
| 37 | #include "Core/Core.h" |
| 38 | #include "Core/Debugger/BranchWatch.h" |
| 39 | #include "Core/Debugger/PPCDebugInterface.h" |
| 40 | #include "Core/PowerPC/Gekko.h" |
| 41 | #include "Core/PowerPC/PowerPC.h" |
| 42 | #include "Core/System.h" |
| 43 | #include "DolphinQt/Debugger/BranchWatchTableModel.h" |
| 44 | #include "DolphinQt/Debugger/CodeWidget.h" |
| 45 | #include "DolphinQt/Host.h" |
| 46 | #include "DolphinQt/QtUtils/DolphinFileDialog.h" |
| 47 | #include "DolphinQt/QtUtils/ModalMessageBox.h" |
| 48 | #include "DolphinQt/Resources.h" |
| 49 | #include "DolphinQt/Settings.h" |
| 50 | |
| 51 | class BranchWatchProxyModel final : public QSortFilterProxyModel |
| 52 | { |
| 53 | friend BranchWatchDialog; |
| 54 | |
| 55 | public: |
| 56 | explicit BranchWatchProxyModel(const Core::BranchWatch& branch_watch, QObject* parent = nullptr) |
| 57 | : QSortFilterProxyModel(parent), m_branch_watch(branch_watch) |
| 58 | { |
| 59 | } |
| 60 | ~BranchWatchProxyModel() override = default; |
| 61 | |
| 62 | BranchWatchProxyModel(const BranchWatchProxyModel&) = delete; |
| 63 | BranchWatchProxyModel(BranchWatchProxyModel&&) = delete; |
| 64 | BranchWatchProxyModel& operator=(const BranchWatchProxyModel&) = delete; |
| 65 | BranchWatchProxyModel& operator=(BranchWatchProxyModel&&) = delete; |
| 66 | |
| 67 | BranchWatchTableModel* sourceModel() const |
| 68 | { |
| 69 | return static_cast<BranchWatchTableModel*>(QSortFilterProxyModel::sourceModel()); |
| 70 | } |
| 71 | void setSourceModel(BranchWatchTableModel* source_model) |
| 72 | { |
| 73 | QSortFilterProxyModel::setSourceModel(source_model); |
| 74 | } |
| 75 | |
| 76 | // Virtual setSourceModel is forbidden for type-safety reasons. See sourceModel(). |
| 77 | [[noreturn]] void setSourceModel(QAbstractItemModel* source_model) override { Crash(); } |
| 78 | bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override; |
| 79 | |
| 80 | template <bool BranchWatchProxyModel::* member> |
| 81 | void OnToggled(bool enabled) |
| 82 | { |
| 83 | this->*member = enabled; |
| 84 | invalidateRowsFilter(); |
| 85 | } |
| 86 | template <QString BranchWatchProxyModel::* member> |
| 87 | void OnSymbolTextChanged(const QString& text) |
| 88 | { |
| 89 | this->*member = text; |
| 90 | invalidateRowsFilter(); |
| 91 | } |
| 92 | template <std::optional<u32> BranchWatchProxyModel::* member> |
| 93 | void OnAddressTextChanged(const QString& text) |
| 94 | { |
| 95 | bool ok = false; |
| 96 | if (const u32 value = text.toUInt(&ok, 16); ok) |
| 97 | this->*member = value; |
| 98 | else |
| 99 | this->*member = std::nullopt; |
| 100 | invalidateRowsFilter(); |
| 101 | } |
| 102 | |
| 103 | bool IsBranchTypeAllowed(UGeckoInstruction inst) const; |
| 104 | void SetInspected(const QModelIndex& index) const; |
| 105 | const Core::BranchWatchSelectionValueType& |
| 106 | GetBranchWatchSelection(const QModelIndex& index) const; |
| 107 | |
| 108 | private: |
| 109 | const Core::BranchWatch& m_branch_watch; |
| 110 | |
| 111 | QString m_origin_symbol_name = {}, m_destin_symbol_name = {}; |
| 112 | std::optional<u32> m_origin_min, m_origin_max, m_destin_min, m_destin_max; |
| 113 | bool m_b = {}, m_bl = {}, m_bc = {}, m_bcl = {}, m_blr = {}, m_blrl = {}, m_bclr = {}, |
| 114 | m_bclrl = {}, m_bctr = {}, m_bctrl = {}, m_bcctr = {}, m_bcctrl = {}; |
| 115 | bool m_cond_true = {}, m_cond_false = {}; |
| 116 | }; |
| 117 | |
| 118 | bool BranchWatchProxyModel::filterAcceptsRow(int source_row, const QModelIndex&) const |
| 119 | { |
| 120 | const Core::BranchWatch::Selection::value_type& value = m_branch_watch.GetSelection()[source_row]; |
| 121 | if (value.condition) |
| 122 | { |
| 123 | if (!m_cond_true) |
| 124 | return false; |
| 125 | } |
| 126 | else if (!m_cond_false) |
| 127 | return false; |
| 128 | |
| 129 | const Core::BranchWatchCollectionKey& k = value.collection_ptr->first; |
| 130 | if (!IsBranchTypeAllowed(k.original_inst)) |
| 131 | return false; |
| 132 | |
| 133 | if (m_origin_min.has_value() && k.origin_addr < m_origin_min.value()) |
| 134 | return false; |
| 135 | if (m_origin_max.has_value() && k.origin_addr > m_origin_max.value()) |
| 136 | return false; |
| 137 | if (m_destin_min.has_value() && k.destin_addr < m_destin_min.value()) |
| 138 | return false; |
| 139 | if (m_destin_max.has_value() && k.destin_addr > m_destin_max.value()) |
| 140 | return false; |
| 141 | |
| 142 | if (!m_origin_symbol_name.isEmpty()) |
| 143 | { |
| 144 | if (const QVariant& symbol_name_v = sourceModel()->GetSymbolList()[source_row].origin_name; |
| 145 | !symbol_name_v.isValid() || !static_cast<const QString*>(symbol_name_v.data()) |
| 146 | ->contains(m_origin_symbol_name, Qt::CaseInsensitive)) |
| 147 | return false; |
| 148 | } |
| 149 | if (!m_destin_symbol_name.isEmpty()) |
| 150 | { |
| 151 | if (const QVariant& symbol_name_v = sourceModel()->GetSymbolList()[source_row].destin_name; |
| 152 | !symbol_name_v.isValid() || !static_cast<const QString*>(symbol_name_v.data()) |
| 153 | ->contains(m_destin_symbol_name, Qt::CaseInsensitive)) |
| 154 | return false; |
| 155 | } |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | bool BranchWatchProxyModel::IsBranchTypeAllowed(UGeckoInstruction inst) const |
| 160 | { |
| 161 | switch (inst.OPCD) |
| 162 | { |
| 163 | case 18: |
| 164 | return inst.LK ? m_bl : m_b; |
| 165 | case 16: |
| 166 | return inst.LK ? m_bcl : m_bc; |
| 167 | case 19: |
| 168 | switch (inst.SUBOP10) |
| 169 | { |
| 170 | case 16: |
| 171 | if ((inst.BO & 0b10100) == 0b10100) // 1z1zz - Branch always |
| 172 | return inst.LK ? m_blrl : m_blr; |
| 173 | return inst.LK ? m_bclrl : m_bclr; |
| 174 | case 528: |
| 175 | if ((inst.BO & 0b10100) == 0b10100) // 1z1zz - Branch always |
| 176 | return inst.LK ? m_bctrl : m_bctr; |
| 177 | return inst.LK ? m_bcctrl : m_bcctr; |
| 178 | } |
| 179 | } |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | void BranchWatchProxyModel::SetInspected(const QModelIndex& index) const |
| 184 | { |
| 185 | sourceModel()->SetInspected(mapToSource(index)); |
| 186 | } |
| 187 | |
| 188 | const Core::BranchWatchSelectionValueType& |
| 189 | BranchWatchProxyModel::GetBranchWatchSelection(const QModelIndex& index) const |
| 190 | { |
| 191 | return sourceModel()->GetBranchWatchSelection(mapToSource(index)); |
| 192 | } |
| 193 | |
| 194 | BranchWatchDialog::BranchWatchDialog(Core::System& system, Core::BranchWatch& branch_watch, |
| 195 | PPCSymbolDB& ppc_symbol_db, CodeWidget* code_widget, |
| 196 | QWidget* parent) |
| 197 | : QDialog(parent), m_system(system), m_branch_watch(branch_watch), m_code_widget(code_widget) |
| 198 | { |
| 199 | setWindowTitle(tr("Branch Watch Tool")); |
| 200 | setWindowFlags(windowFlags() | Qt::WindowMinMaxButtonsHint); |
| 201 | |
| 202 | // Branch Watch Table |
| 203 | m_table_view = new QTableView(nullptr); |
| 204 | m_table_proxy = new BranchWatchProxyModel(m_branch_watch, m_table_view); |
| 205 | m_table_model = new BranchWatchTableModel(m_system, m_branch_watch, ppc_symbol_db, m_table_proxy); |
| 206 | |
| 207 | m_table_proxy->setSourceModel(m_table_model); |
| 208 | m_table_proxy->setSortRole(UserRole::SortRole); |
| 209 | m_table_proxy->setSortCaseSensitivity(Qt::CaseInsensitive); |
| 210 | |
| 211 | m_table_view->setModel(m_table_proxy); |
| 212 | m_table_view->setSortingEnabled(true); |
| 213 | m_table_view->sortByColumn(Column::Origin, Qt::AscendingOrder); |
| 214 | m_table_view->setSelectionMode(QAbstractItemView::ExtendedSelection); |
| 215 | m_table_view->setSelectionBehavior(QAbstractItemView::SelectRows); |
| 216 | m_table_view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
| 217 | m_table_view->setContextMenuPolicy(Qt::CustomContextMenu); |
| 218 | m_table_view->setEditTriggers(QAbstractItemView::NoEditTriggers); |
| 219 | m_table_view->setCornerButtonEnabled(false); |
| 220 | m_table_view->verticalHeader()->hide(); |
| 221 | m_table_view->setColumnWidth(Column::Instruction, 50); |
| 222 | m_table_view->setColumnWidth(Column::Condition, 50); |
| 223 | m_table_view->setColumnWidth(Column::OriginSymbol, 250); |
| 224 | m_table_view->setColumnWidth(Column::DestinSymbol, 250); |
| 225 | // The default column width (100 units) is fine for the rest. |
| 226 | |
| 227 | auto* const horizontal_header = m_table_view->horizontalHeader(); |
| 228 | horizontal_header->setContextMenuPolicy(Qt::CustomContextMenu); |
| 229 | horizontal_header->setStretchLastSection(true); |
| 230 | horizontal_header->setSectionsMovable(true); |
| 231 | horizontal_header->setFirstSectionMovable(true); |
| 232 | |
| 233 | connect(m_table_view, &QTableView::clicked, this, &BranchWatchDialog::OnTableClicked); |
| 234 | connect(m_table_view, &QTableView::customContextMenuRequested, this, |
| 235 | &BranchWatchDialog::OnTableContextMenu); |
| 236 | connect(horizontal_header, &QHeaderView::customContextMenuRequested, this, |
| 237 | &BranchWatchDialog::OnTableHeaderContextMenu); |
| 238 | connect(new QShortcut(QKeySequence(Qt::Key_Delete), this), &QShortcut::activated, this, |
| 239 | &BranchWatchDialog::OnTableDeleteKeypress); |
| 240 | |
| 241 | // Status Bar |
| 242 | m_status_bar = new QStatusBar(nullptr); |
| 243 | m_status_bar->setSizeGripEnabled(false); |
| 244 | |
| 245 | // Controls Toolbar |
| 246 | m_control_toolbar = new QToolBar(nullptr); |
| 247 | { |
| 248 | // Tool Controls |
| 249 | m_btn_start_pause = new QPushButton(tr("Start Branch Watch"), nullptr); |
| 250 | connect(m_btn_start_pause, &QPushButton::toggled, this, &BranchWatchDialog::OnStartPause); |
| 251 | m_btn_start_pause->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); |
| 252 | m_btn_start_pause->setCheckable(true); |
| 253 | |
| 254 | m_btn_clear_watch = new QPushButton(tr("Clear Branch Watch"), nullptr); |
| 255 | connect(m_btn_clear_watch, &QPushButton::clicked, this, &BranchWatchDialog::OnClearBranchWatch); |
| 256 | m_btn_clear_watch->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); |
| 257 | |
| 258 | m_btn_path_was_taken = new QPushButton(tr("Code Path Was Taken"), nullptr); |
| 259 | connect(m_btn_path_was_taken, &QPushButton::clicked, this, |
| 260 | &BranchWatchDialog::OnCodePathWasTaken); |
| 261 | m_btn_path_was_taken->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); |
| 262 | |
| 263 | m_btn_path_not_taken = new QPushButton(tr("Code Path Not Taken"), nullptr); |
| 264 | connect(m_btn_path_not_taken, &QPushButton::clicked, this, |
| 265 | &BranchWatchDialog::OnCodePathNotTaken); |
| 266 | m_btn_path_not_taken->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); |
| 267 | |
| 268 | auto* const layout = new QGridLayout(nullptr); |
| 269 | layout->addWidget(m_btn_start_pause, 0, 0); |
| 270 | layout->addWidget(m_btn_clear_watch, 1, 0); |
| 271 | layout->addWidget(m_btn_path_was_taken, 0, 1); |
| 272 | layout->addWidget(m_btn_path_not_taken, 1, 1); |
| 273 | |
| 274 | auto* const group_box = new QGroupBox(tr("Tool Controls"), nullptr); |
| 275 | group_box->setLayout(layout); |
| 276 | group_box->setAlignment(Qt::AlignHCenter); |
| 277 | |
| 278 | m_control_toolbar->addWidget(group_box); |
| 279 | } |
| 280 | { |
| 281 | // Spacer |
| 282 | auto* const widget = new QWidget(nullptr); |
| 283 | widget->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred); |
| 284 | m_control_toolbar->addWidget(widget); |
| 285 | } |
| 286 | { |
| 287 | // Branch Type Filter Options |
| 288 | auto* const layout = new QGridLayout(nullptr); |
| 289 | |
| 290 | const auto routine = [this, layout](const QString& text, const QString& tooltip, int row, |
| 291 | int column, void (BranchWatchProxyModel::*slot)(bool)) { |
| 292 | auto* const check_box = new QCheckBox(text, nullptr); |
| 293 | check_box->setToolTip(tooltip); |
| 294 | layout->addWidget(check_box, row, column); |
| 295 | connect(check_box, &QCheckBox::toggled, [this, slot](bool checked) { |
| 296 | (m_table_proxy->*slot)(checked); |
| 297 | UpdateStatus(); |
| 298 | }); |
| 299 | check_box->setChecked(true); |
| 300 | }; |
| 301 | |
| 302 | // clang-format off |
| 303 | routine(QStringLiteral("b" ), tr("Branch" ), 0, 0, &BranchWatchProxyModel::OnToggled<&BranchWatchProxyModel::m_b >); |
| 304 | routine(QStringLiteral("bl" ), tr("Branch (LR saved)" ), 0, 1, &BranchWatchProxyModel::OnToggled<&BranchWatchProxyModel::m_bl >); |
| 305 | routine(QStringLiteral("bc" ), tr("Branch Conditional" ), 0, 2, &BranchWatchProxyModel::OnToggled<&BranchWatchProxyModel::m_bc >); |
| 306 | routine(QStringLiteral("bcl" ), tr("Branch Conditional (LR saved)" ), 0, 3, &BranchWatchProxyModel::OnToggled<&BranchWatchProxyModel::m_bcl >); |
| 307 | routine(QStringLiteral("blr" ), tr("Branch to Link Register" ), 1, 0, &BranchWatchProxyModel::OnToggled<&BranchWatchProxyModel::m_blr >); |
| 308 | routine(QStringLiteral("blrl" ), tr("Branch to Link Register (LR saved)" ), 1, 1, &BranchWatchProxyModel::OnToggled<&BranchWatchProxyModel::m_blrl >); |
| 309 | routine(QStringLiteral("bclr" ), tr("Branch Conditional to Link Register" ), 1, 2, &BranchWatchProxyModel::OnToggled<&BranchWatchProxyModel::m_bclr >); |
| 310 | routine(QStringLiteral("bclrl" ), tr("Branch Conditional to Link Register (LR saved)" ), 1, 3, &BranchWatchProxyModel::OnToggled<&BranchWatchProxyModel::m_bclrl >); |
| 311 | routine(QStringLiteral("bctr" ), tr("Branch to Count Register" ), 2, 0, &BranchWatchProxyModel::OnToggled<&BranchWatchProxyModel::m_bctr >); |
| 312 | routine(QStringLiteral("bctrl" ), tr("Branch to Count Register (LR saved)" ), 2, 1, &BranchWatchProxyModel::OnToggled<&BranchWatchProxyModel::m_bctrl >); |
| 313 | routine(QStringLiteral("bcctr" ), tr("Branch Conditional to Count Register" ), 2, 2, &BranchWatchProxyModel::OnToggled<&BranchWatchProxyModel::m_bcctr >); |
| 314 | routine(QStringLiteral("bcctrl"), tr("Branch Conditional to Count Register (LR saved)"), 2, 3, &BranchWatchProxyModel::OnToggled<&BranchWatchProxyModel::m_bcctrl>); |
| 315 | // clang-format on |
| 316 | |
| 317 | auto* const group_box = new QGroupBox(tr("Branch Type"), nullptr); |
| 318 | group_box->setLayout(layout); |
| 319 | group_box->setAlignment(Qt::AlignHCenter); |
| 320 | |
| 321 | m_act_branch_type_filters = m_control_toolbar->addWidget(group_box); |
| 322 | } |
| 323 | { |
| 324 | // Origin and Destination Filter Options |
| 325 | auto* const layout = new QGridLayout(nullptr); |
| 326 | |
| 327 | const auto routine = [this, layout](const QString& placeholder_text, int row, int column, |
| 328 | int width, |
| 329 | void (BranchWatchProxyModel::*slot)(const QString&)) { |
| 330 | auto* const line_edit = new QLineEdit(nullptr); |
| 331 | layout->addWidget(line_edit, row, column, 1, width); |
| 332 | connect(line_edit, &QLineEdit::textChanged, [this, slot](const QString& text) { |
| 333 | (m_table_proxy->*slot)(text); |
| 334 | UpdateStatus(); |
| 335 | }); |
| 336 | line_edit->setPlaceholderText(placeholder_text); |
| 337 | return line_edit; |
| 338 | }; |
| 339 | |
| 340 | // clang-format off |
| 341 | routine(tr("Origin Symbol" ), 0, 0, 1, &BranchWatchProxyModel::OnSymbolTextChanged<&BranchWatchProxyModel::m_origin_symbol_name>); |
| 342 | routine(tr("Origin Min" ), 1, 0, 1, &BranchWatchProxyModel::OnAddressTextChanged<&BranchWatchProxyModel::m_origin_min>)->setMaxLength(8); |
| 343 | routine(tr("Origin Max" ), 2, 0, 1, &BranchWatchProxyModel::OnAddressTextChanged<&BranchWatchProxyModel::m_origin_max>)->setMaxLength(8); |
| 344 | routine(tr("Destination Symbol"), 0, 1, 1, &BranchWatchProxyModel::OnSymbolTextChanged<&BranchWatchProxyModel::m_destin_symbol_name>); |
| 345 | routine(tr("Destination Min" ), 1, 1, 1, &BranchWatchProxyModel::OnAddressTextChanged<&BranchWatchProxyModel::m_destin_min>)->setMaxLength(8); |
| 346 | routine(tr("Destination Max" ), 2, 1, 1, &BranchWatchProxyModel::OnAddressTextChanged<&BranchWatchProxyModel::m_destin_max>)->setMaxLength(8); |
| 347 | // clang-format on |
| 348 | |
| 349 | auto* const group_box = new QGroupBox(tr("Origin and Destination"), nullptr); |
| 350 | group_box->setLayout(layout); |
| 351 | group_box->setAlignment(Qt::AlignHCenter); |
| 352 | |
| 353 | m_act_origin_destin_filters = m_control_toolbar->addWidget(group_box); |
| 354 | } |
| 355 | { |
| 356 | // Condition Filter Options |
| 357 | auto* const layout = new QVBoxLayout(nullptr); |
| 358 | layout->setAlignment(Qt::AlignHCenter); |
| 359 | |
| 360 | const auto routine = [this, layout](const QString& text, |
| 361 | void (BranchWatchProxyModel::*slot)(bool)) { |
| 362 | auto* const check_box = new QCheckBox(text, nullptr); |
| 363 | layout->addWidget(check_box); |
| 364 | connect(check_box, &QCheckBox::toggled, [this, slot](bool checked) { |
| 365 | (m_table_proxy->*slot)(checked); |
| 366 | UpdateStatus(); |
| 367 | }); |
| 368 | check_box->setChecked(true); |
| 369 | return check_box; |
| 370 | }; |
| 371 | |
| 372 | routine(tr("true"), &BranchWatchProxyModel::OnToggled<&BranchWatchProxyModel::m_cond_true>) |
| 373 | ->setToolTip(tr("This will also filter unconditional branches.\n" |
| 374 | "To filter for or against unconditional branches,\n" |
| 375 | "use the Branch Type filter options.")); |
| 376 | routine(tr("false"), &BranchWatchProxyModel::OnToggled<&BranchWatchProxyModel::m_cond_false>); |
| 377 | |
| 378 | auto* const group_box = new QGroupBox(tr("Condition"), nullptr); |
| 379 | group_box->setLayout(layout); |
| 380 | group_box->setAlignment(Qt::AlignHCenter); |
| 381 | |
| 382 | m_act_condition_filters = m_control_toolbar->addWidget(group_box); |
| 383 | } |
| 384 | { |
| 385 | // Misc. Controls |
| 386 | m_btn_was_overwritten = new QPushButton(tr("Branch Was Overwritten"), nullptr); |
| 387 | connect(m_btn_was_overwritten, &QPushButton::clicked, this, |
| 388 | &BranchWatchDialog::OnBranchWasOverwritten); |
| 389 | m_btn_was_overwritten->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); |
| 390 | |
| 391 | m_btn_not_overwritten = new QPushButton(tr("Branch Not Overwritten"), nullptr); |
| 392 | connect(m_btn_not_overwritten, &QPushButton::clicked, this, |
| 393 | &BranchWatchDialog::OnBranchNotOverwritten); |
| 394 | m_btn_not_overwritten->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); |
| 395 | |
| 396 | m_btn_wipe_recent_hits = new QPushButton(tr("Wipe Recent Hits"), nullptr); |
| 397 | connect(m_btn_wipe_recent_hits, &QPushButton::clicked, this, |
| 398 | &BranchWatchDialog::OnWipeRecentHits); |
| 399 | m_btn_wipe_recent_hits->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); |
| 400 | m_btn_wipe_recent_hits->setEnabled(false); |
| 401 | |
| 402 | auto* const layout = new QVBoxLayout(nullptr); |
| 403 | layout->addWidget(m_btn_was_overwritten); |
| 404 | layout->addWidget(m_btn_not_overwritten); |
| 405 | layout->addWidget(m_btn_wipe_recent_hits); |
| 406 | |
| 407 | auto* const group_box = new QGroupBox(tr("Misc. Controls"), nullptr); |
| 408 | group_box->setLayout(layout); |
| 409 | group_box->setAlignment(Qt::AlignHCenter); |
| 410 | |
| 411 | m_act_misc_controls = m_control_toolbar->addWidget(group_box); |
| 412 | } |
| 413 | |
| 414 | // Table Context Menus |
| 415 | auto* const delete_action = new QAction(tr("&Delete"), this); |
| 416 | connect(delete_action, &QAction::triggered, this, &BranchWatchDialog::OnTableDelete); |
| 417 | |
| 418 | m_act_invert_condition = new QAction(tr("Invert &Condition"), this); |
| 419 | connect(m_act_invert_condition, &QAction::triggered, this, |
| 420 | &BranchWatchDialog::OnTableInvertCondition); |
| 421 | |
| 422 | m_act_invert_decrement_check = new QAction(tr("Invert &Decrement Check"), this); |
| 423 | connect(m_act_invert_decrement_check, &QAction::triggered, this, |
| 424 | &BranchWatchDialog::OnTableInvertDecrementCheck); |
| 425 | |
| 426 | m_act_make_unconditional = new QAction(tr("Make &Unconditional"), this); |
| 427 | connect(m_act_make_unconditional, &QAction::triggered, this, |
| 428 | &BranchWatchDialog::OnTableMakeUnconditional); |
| 429 | |
| 430 | m_act_copy_address = new QAction(tr("&Copy Address"), this); |
| 431 | connect(m_act_copy_address, &QAction::triggered, this, &BranchWatchDialog::OnTableCopyAddress); |
| 432 | |
| 433 | m_act_insert_nop = new QAction(tr("Insert &NOP"), this); |
| 434 | connect(m_act_insert_nop, &QAction::triggered, this, &BranchWatchDialog::OnTableSetNOP); |
| 435 | |
| 436 | m_act_insert_blr = new QAction(tr("Insert &BLR"), this); |
| 437 | connect(m_act_insert_blr, &QAction::triggered, this, &BranchWatchDialog::OnTableSetBLR); |
| 438 | |
| 439 | m_mnu_set_breakpoint = new QMenu(tr("Set Brea&kpoint"), this); |
| 440 | m_act_break_on_hit = m_mnu_set_breakpoint->addAction( |
| 441 | tr("&Break on Hit"), this, &BranchWatchDialog::OnTableSetBreakpointBreak); |
| 442 | m_act_log_on_hit = m_mnu_set_breakpoint->addAction(tr("&Log on Hit"), this, |
| 443 | &BranchWatchDialog::OnTableSetBreakpointLog); |
| 444 | m_act_both_on_hit = m_mnu_set_breakpoint->addAction(tr("Break &and Log on Hit"), this, |
| 445 | &BranchWatchDialog::OnTableSetBreakpointBoth); |
| 446 | |
| 447 | m_mnu_table_context_instruction = new QMenu(this); |
| 448 | m_mnu_table_context_instruction->addActions( |
| 449 | {delete_action, m_act_invert_condition, m_act_invert_decrement_check}); |
| 450 | |
| 451 | m_mnu_table_context_condition = new QMenu(this); |
| 452 | m_mnu_table_context_condition->addActions({delete_action, m_act_make_unconditional}); |
| 453 | |
| 454 | m_mnu_table_context_origin = new QMenu(this); |
| 455 | m_mnu_table_context_origin->addActions( |
| 456 | {delete_action, m_act_insert_nop, m_act_copy_address, m_mnu_set_breakpoint->menuAction()}); |
| 457 | |
| 458 | m_mnu_table_context_destin = new QMenu(this); |
| 459 | m_mnu_table_context_destin->addActions( |
| 460 | {delete_action, m_act_insert_blr, m_act_copy_address, m_mnu_set_breakpoint->menuAction()}); |
| 461 | |
| 462 | m_mnu_table_context_symbol = new QMenu(this); |
| 463 | m_mnu_table_context_symbol->addActions( |
| 464 | {delete_action, m_act_insert_blr, m_act_copy_address, m_mnu_set_breakpoint->menuAction()}); |
| 465 | |
| 466 | m_mnu_table_context_other = new QMenu(this); |
| 467 | m_mnu_table_context_other->addAction(delete_action); |
| 468 | |
| 469 | LoadQSettings(); |
| 470 | |
| 471 | // Column Visibility Menu |
| 472 | m_mnu_column_visibility = new QMenu(this); |
| 473 | { |
| 474 | static constexpr std::array<const char*, Column::NumberOfColumns> headers = { |
| 475 | QT_TR_NOOP("Instruction"), QT_TR_NOOP("Condition"), QT_TR_NOOP("Origin"), |
| 476 | QT_TR_NOOP("Destination"), QT_TR_NOOP("Recent Hits"), QT_TR_NOOP("Total Hits"), |
| 477 | QT_TR_NOOP("Origin Symbol"), QT_TR_NOOP("Destination Symbol")}; |
| 478 | |
| 479 | for (int column = 0; column < Column::NumberOfColumns; ++column) |
| 480 | { |
| 481 | auto* const action = |
| 482 | m_mnu_column_visibility->addAction(tr(headers[column]), [this, column](bool enabled) { |
| 483 | m_table_view->setColumnHidden(column, !enabled); |
| 484 | }); |
| 485 | action->setChecked(!m_table_view->isColumnHidden(column)); |
| 486 | action->setCheckable(true); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | // Toolbar Visibility Menu |
| 491 | auto* const toolbar_visibility_menu = new QMenu(this); |
| 492 | { |
| 493 | const auto routine = [toolbar_visibility_menu](const QString& text, QAction* toolbar_action) { |
| 494 | auto* const menu_action = |
| 495 | toolbar_visibility_menu->addAction(text, toolbar_action, &QAction::setVisible); |
| 496 | menu_action->setChecked(toolbar_action->isVisible()); |
| 497 | menu_action->setCheckable(true); |
| 498 | }; |
| 499 | routine(tr("&Branch Type"), m_act_branch_type_filters); |
| 500 | routine(tr("&Origin and Destination"), m_act_origin_destin_filters); |
| 501 | routine(tr("&Condition"), m_act_condition_filters); |
| 502 | routine(tr("&Misc. Controls"), m_act_misc_controls); |
| 503 | } |
| 504 | |
| 505 | // Menu Bar |
| 506 | auto* const menu_bar = new QMenuBar(nullptr); |
| 507 | menu_bar->setNativeMenuBar(false); |
| 508 | { |
| 509 | auto* const menu = menu_bar->addMenu(tr("&File")); |
| 510 | menu->addAction(tr("&Save Branch Watch"), this, &BranchWatchDialog::OnSave); |
| 511 | menu->addAction(tr("Save Branch Watch &As..."), this, &BranchWatchDialog::OnSaveAs); |
| 512 | menu->addAction(tr("&Load Branch Watch"), this, &BranchWatchDialog::OnLoad); |
| 513 | menu->addAction(tr("Load Branch Watch &From..."), this, &BranchWatchDialog::OnLoadFrom); |
| 514 | m_act_autosave = menu->addAction(tr("A&uto Save")); |
| 515 | m_act_autosave->setCheckable(true); |
| 516 | connect(m_act_autosave, &QAction::toggled, this, &BranchWatchDialog::OnToggleAutoSave); |
| 517 | } |
| 518 | { |
| 519 | auto* const menu = menu_bar->addMenu(tr("&Tool")); |
| 520 | menu->setToolTipsVisible(true); |
| 521 | menu->addAction(tr("Hide &Controls"), this, &BranchWatchDialog::OnHideShowControls) |
| 522 | ->setCheckable(true); |
| 523 | auto* const act_ignore_apploader = menu->addAction(tr("Ignore &Apploader Branch Hits")); |
| 524 | act_ignore_apploader->setToolTip( |
| 525 | tr("This only applies to the initial boot of the emulated software.")); |
| 526 | act_ignore_apploader->setChecked(m_system.IsBranchWatchIgnoreApploader()); |
| 527 | act_ignore_apploader->setCheckable(true); |
| 528 | connect(act_ignore_apploader, &QAction::toggled, this, |
| 529 | &BranchWatchDialog::OnToggleIgnoreApploader); |
| 530 | menu->addMenu(m_mnu_column_visibility)->setText(tr("Column &Visibility")); |
| 531 | menu->addMenu(toolbar_visibility_menu)->setText(tr("&Toolbar Visibility")); |
| 532 | menu->addAction(tr("Wipe &Inspection Data"), this, &BranchWatchDialog::OnWipeInspection); |
| 533 | menu->addAction(tr("&Help"), this, &BranchWatchDialog::OnHelp); |
| 534 | } |
| 535 | |
| 536 | connect(m_timer = new QTimer(this), &QTimer::timeout, this, &BranchWatchDialog::OnTimeout); |
| 537 | connect(m_table_proxy, &BranchWatchProxyModel::layoutChanged, this, |
| 538 | &BranchWatchDialog::UpdateStatus); |
| 539 | |
| 540 | auto* const main_layout = new QVBoxLayout(nullptr); |
| 541 | main_layout->setMenuBar(menu_bar); |
| 542 | main_layout->addWidget(m_control_toolbar); |
| 543 | main_layout->addWidget(m_table_view); |
| 544 | main_layout->addWidget(m_status_bar); |
| 545 | setLayout(main_layout); |
| 546 | } |
| 547 | |
| 548 | BranchWatchDialog::~BranchWatchDialog() |
| 549 | { |
| 550 | SaveQSettings(); |
| 551 | } |
| 552 | |
| 553 | static constexpr int BRANCH_WATCH_TOOL_TIMER_DELAY_MS = 100; |
| 554 | |
| 555 | static bool TimerCondition(const Core::BranchWatch& branch_watch, Core::State state) |
| 556 | { |
| 557 | return branch_watch.GetRecordingActive() && state > Core::State::Paused; |
| 558 | } |
| 559 | |
| 560 | void BranchWatchDialog::hideEvent(QHideEvent* event) |
| 561 | { |
| 562 | Hide(); |
| 563 | QDialog::hideEvent(event); |
| 564 | } |
| 565 | |
| 566 | void BranchWatchDialog::showEvent(QShowEvent* event) |
| 567 | { |
| 568 | Show(); |
| 569 | QDialog::showEvent(event); |
| 570 | } |
| 571 | |
| 572 | void BranchWatchDialog::OnStartPause(bool checked) const |
| 573 | { |
| 574 | m_branch_watch.SetRecordingActive(Core::CPUThreadGuard{m_system}, checked); |
| 575 | if (checked) |
| 576 | { |
| 577 | m_btn_start_pause->setText(tr("Pause Branch Watch")); |
| 578 | if (Core::GetState(m_system) > Core::State::Paused) |
| 579 | m_timer->start(BRANCH_WATCH_TOOL_TIMER_DELAY_MS); |
| 580 | } |
| 581 | else |
| 582 | { |
| 583 | m_btn_start_pause->setText(tr("Start Branch Watch")); |
| 584 | if (m_timer->isActive()) |
| 585 | m_timer->stop(); |
| 586 | } |
| 587 | Update(); |
| 588 | } |
| 589 | |
| 590 | void BranchWatchDialog::OnClearBranchWatch() |
| 591 | { |
| 592 | { |
| 593 | const Core::CPUThreadGuard guard{m_system}; |
| 594 | m_table_model->OnClearBranchWatch(guard); |
| 595 | AutoSave(guard); |
| 596 | } |
| 597 | m_btn_wipe_recent_hits->setEnabled(false); |
| 598 | UpdateStatus(); |
| 599 | } |
| 600 | |
| 601 | static std::string GetSnapshotDefaultFilepath() |
| 602 | { |
| 603 | return fmt::format("{}{}.txt", File::GetUserPath(D_DUMPDEBUG_BRANCHWATCH_IDX), |
| 604 | SConfig::GetInstance().GetGameID()); |
| 605 | } |
| 606 | |
| 607 | void BranchWatchDialog::OnSave() |
| 608 | { |
| 609 | if (!m_branch_watch.CanSave()) |
| 610 | { |
| 611 | ModalMessageBox::warning(this, tr("Error"), tr("There is nothing to save!")); |
| 612 | return; |
| 613 | } |
| 614 | |
| 615 | Save(Core::CPUThreadGuard{m_system}, GetSnapshotDefaultFilepath()); |
| 616 | } |
| 617 | |
| 618 | void BranchWatchDialog::OnSaveAs() |
| 619 | { |
| 620 | if (!m_branch_watch.CanSave()) |
| 621 | { |
| 622 | ModalMessageBox::warning(this, tr("Error"), tr("There is nothing to save!")); |
| 623 | return; |
| 624 | } |
| 625 | |
| 626 | const QString filepath = DolphinFileDialog::getSaveFileName( |
| 627 | this, tr("Save Branch Watch Snapshot"), |
| 628 | QString::fromStdString(File::GetUserPath(D_DUMPDEBUG_BRANCHWATCH_IDX)), |
| 629 | tr("Text file (*.txt);;All Files (*)")); |
| 630 | if (filepath.isEmpty()) |
| 631 | return; |
| 632 | |
| 633 | Save(Core::CPUThreadGuard{m_system}, filepath.toStdString()); |
| 634 | } |
| 635 | |
| 636 | void BranchWatchDialog::OnLoad() |
| 637 | { |
| 638 | Load(Core::CPUThreadGuard{m_system}, GetSnapshotDefaultFilepath()); |
| 639 | } |
| 640 | |
| 641 | void BranchWatchDialog::OnLoadFrom() |
| 642 | { |
| 643 | const QString filepath = DolphinFileDialog::getOpenFileName( |
| 644 | this, tr("Load Branch Watch Snapshot"), |
| 645 | QString::fromStdString(File::GetUserPath(D_DUMPDEBUG_BRANCHWATCH_IDX)), |
| 646 | tr("Text file (*.txt);;All Files (*)"), nullptr, QFileDialog::Option::ReadOnly); |
| 647 | if (filepath.isEmpty()) |
| 648 | return; |
| 649 | |
| 650 | Load(Core::CPUThreadGuard{m_system}, filepath.toStdString()); |
| 651 | } |
| 652 | |
| 653 | void BranchWatchDialog::OnCodePathWasTaken() |
| 654 | { |
| 655 | { |
| 656 | const Core::CPUThreadGuard guard{m_system}; |
| 657 | m_table_model->OnCodePathWasTaken(guard); |
| 658 | AutoSave(guard); |
| 659 | } |
| 660 | m_btn_wipe_recent_hits->setEnabled(true); |
| 661 | UpdateStatus(); |
| 662 | } |
| 663 | |
| 664 | void BranchWatchDialog::OnCodePathNotTaken() |
| 665 | { |
| 666 | { |
| 667 | const Core::CPUThreadGuard guard{m_system}; |
| 668 | m_table_model->OnCodePathNotTaken(guard); |
| 669 | AutoSave(guard); |
| 670 | } |
| 671 | UpdateStatus(); |
| 672 | } |
| 673 | |
| 674 | void BranchWatchDialog::OnBranchWasOverwritten() |
| 675 | { |
| 676 | { |
| 677 | const Core::CPUThreadGuard guard{m_system}; |
| 678 | m_table_model->OnBranchWasOverwritten(guard); |
| 679 | AutoSave(guard); |
| 680 | } |
| 681 | UpdateStatus(); |
| 682 | } |
| 683 | |
| 684 | void BranchWatchDialog::OnBranchNotOverwritten() |
| 685 | { |
| 686 | { |
| 687 | const Core::CPUThreadGuard guard{m_system}; |
| 688 | m_table_model->OnBranchNotOverwritten(guard); |
| 689 | AutoSave(guard); |
| 690 | } |
| 691 | UpdateStatus(); |
| 692 | } |
| 693 | |
| 694 | void BranchWatchDialog::OnWipeRecentHits() const |
| 695 | { |
| 696 | m_table_model->OnWipeRecentHits(); |
| 697 | } |
| 698 | |
| 699 | void BranchWatchDialog::OnWipeInspection() const |
| 700 | { |
| 701 | m_table_model->OnWipeInspection(); |
| 702 | } |
| 703 | |
| 704 | void BranchWatchDialog::OnTimeout() const |
| 705 | { |
| 706 | Update(); |
| 707 | } |
| 708 | |
| 709 | void BranchWatchDialog::OnEmulationStateChanged(Core::State new_state) const |
| 710 | { |
| 711 | const bool core_initialized = new_state != Core::State::Uninitialized; |
| 712 | RefreshVisibleContextMenuActions(core_initialized); |
| 713 | m_btn_was_overwritten->setEnabled(core_initialized); |
| 714 | m_btn_not_overwritten->setEnabled(core_initialized); |
| 715 | if (TimerCondition(m_branch_watch, new_state)) |
| 716 | m_timer->start(BRANCH_WATCH_TOOL_TIMER_DELAY_MS); |
| 717 | else if (m_timer->isActive()) |
| 718 | m_timer->stop(); |
| 719 | Update(); |
| 720 | } |
| 721 | |
| 722 | void BranchWatchDialog::OnThemeChanged() |
| 723 | { |
| 724 | UpdateIcons(); |
| 725 | } |
| 726 | |
| 727 | void BranchWatchDialog::OnHelp() |
| 728 | { |
| 729 | ModalMessageBox::information( |
| 730 | this, tr("Branch Watch Tool Help (1/4)"), |
| 731 | tr("Branch Watch is a code-searching tool that can isolate branches tracked by the emulated " |
| 732 | "CPU by testing candidate branches with simple criteria. If you are familiar with Cheat " |
| 733 | "Engine's Ultimap, Branch Watch is similar to that." |
| 734 | "\n\n" |
| 735 | "Press the \"Start Branch Watch\" button to activate Branch Watch. Branch Watch persists " |
| 736 | "across emulation sessions, and a snapshot of your progress can be saved to and loaded " |
| 737 | "from the User Directory to persist after Dolphin Emulator is closed. \"Save As...\" and " |
| 738 | "\"Load From...\" actions are also available, and auto-saving can be enabled to save a " |
| 739 | "snapshot at every step of a search. The \"Pause Branch Watch\" button will halt Branch " |
| 740 | "Watch from tracking further branch hits until it is told to resume. Press the \"Clear " |
| 741 | "Branch Watch\" button to clear all candidates and return to the blacklist phase.")); |
| 742 | ModalMessageBox::information( |
| 743 | this, tr("Branch Watch Tool Help (2/4)"), |
| 744 | tr("Branch Watch starts in the blacklist phase, meaning no candidates have been chosen yet, " |
| 745 | "but candidates found so far can be excluded from the candidacy by pressing the \"Code " |
| 746 | "Path Not Taken\", \"Branch Was Overwritten\", and \"Branch Not Overwritten\" buttons. " |
| 747 | "Once the \"Code Path Was Taken\" button is pressed for the first time, Branch Watch will " |
| 748 | "switch to the reduction phase, and the table will populate with all eligible " |
| 749 | "candidates.")); |
| 750 | ModalMessageBox::information( |
| 751 | this, tr("Branch Watch Tool Help (3/4)"), |
| 752 | tr("Once in the reduction phase, it is time to start narrowing down the candidates shown in " |
| 753 | "the table. Further reduce the candidates by checking whether a code path was or was not " |
| 754 | "taken since the last time it was checked. It is also possible to reduce the candidates " |
| 755 | "by determining whether a branch instruction has or has not been overwritten since it was " |
| 756 | "first hit. Filter the candidates by branch kind, branch condition, origin or destination " |
| 757 | "address, and origin or destination symbol name." |
| 758 | "\n\n" |
| 759 | "After enough passes and experimentation, you may be able to find function calls and " |
| 760 | "conditional code paths that are only taken when an action is performed in the emulated " |
| 761 | "software.")); |
| 762 | ModalMessageBox::information( |
| 763 | this, tr("Branch Watch Tool Help (4/4)"), |
| 764 | tr("Rows in the table can be left-clicked on the origin, destination, and symbol columns to " |
| 765 | "view the associated address in Code View. Right-clicking the selected row(s) will bring " |
| 766 | "up a context menu." |
| 767 | "\n\n" |
| 768 | "If the origin, destination, or symbol columns are right-clicked, an action copy the " |
| 769 | "relevant address(es) to the clipboard will be available, and an action to set a " |
| 770 | "breakpoint at the relevant address(es) will be available. Note that, for the origin / " |
| 771 | "destination symbol columns, these actions will only be enabled if every row in the " |
| 772 | "selection has a symbol." |
| 773 | "\n\n" |
| 774 | "If the instruction column of a row selection is right-clicked, an action to invert the " |
| 775 | "branch instruction's condition and an action to invert the branch instruction's " |
| 776 | "decrement check will be available, but only if the branch instruction is a conditional " |
| 777 | "one." |
| 778 | "\n\n" |
| 779 | "If the condition column of a row selection is right-clicked, an action to make the " |
| 780 | "branch instruction unconditional will be available, but only if the branch instruction " |
| 781 | "is a conditional one." |
| 782 | "\n\n" |
| 783 | "If the origin column of a row selection is right-clicked, an action to replace the " |
| 784 | "branch instruction at the origin(s) with a NOP instruction (No Operation) will be " |
| 785 | "available." |
| 786 | "\n\n" |
| 787 | "If the destination column of a row selection is right-clicked, an action to replace the " |
| 788 | "instruction at the destination(s) with a BLR instruction (Branch to Link Register) will " |
| 789 | "be available, but will only be enabled if the branch instruction at every origin updates " |
| 790 | "the link register." |
| 791 | "\n\n" |
| 792 | "If the origin / destination symbol column of a row selection is right-clicked, an action " |
| 793 | "to replace the instruction at the start of the symbol(s) with a BLR instruction will be " |
| 794 | "available, but will only be enabled if every row in the selection has a symbol." |
| 795 | "\n\n" |
| 796 | "All context menus have the action to delete the selected row(s) from the candidates.")); |
| 797 | } |
| 798 | |
| 799 | void BranchWatchDialog::OnToggleAutoSave(bool checked) |
| 800 | { |
| 801 | if (!checked) |
| 802 | return; |
| 803 | |
| 804 | const QString filepath = DolphinFileDialog::getSaveFileName( |
| 805 | // i18n: If the user selects a file, Branch Watch will save to that file. |
| 806 | // If the user presses Cancel, Branch Watch will save to a file in the user folder. |
| 807 | this, tr("Select Branch Watch Snapshot Auto-Save File (for user folder location, cancel)"), |
| 808 | QString::fromStdString(File::GetUserPath(D_DUMPDEBUG_BRANCHWATCH_IDX)), |
| 809 | tr("Text file (*.txt);;All Files (*)")); |
| 810 | if (filepath.isEmpty()) |
| 811 | m_autosave_filepath = std::nullopt; |
| 812 | else |
| 813 | m_autosave_filepath = filepath.toStdString(); |
| 814 | } |
| 815 | |
| 816 | void BranchWatchDialog::OnHideShowControls(bool checked) const |
| 817 | { |
| 818 | if (checked) |
| 819 | m_control_toolbar->hide(); |
| 820 | else |
| 821 | m_control_toolbar->show(); |
| 822 | } |
| 823 | |
| 824 | void BranchWatchDialog::OnToggleIgnoreApploader(bool checked) const |
| 825 | { |
| 826 | m_system.SetIsBranchWatchIgnoreApploader(checked); |
| 827 | } |
| 828 | |
| 829 | void BranchWatchDialog::OnTableClicked(const QModelIndex& index) const |
| 830 | { |
| 831 | const QVariant v = m_table_proxy->data(index, UserRole::ClickRole); |
| 832 | switch (index.column()) |
| 833 | { |
| 834 | case Column::OriginSymbol: |
| 835 | case Column::DestinSymbol: |
| 836 | if (!v.isValid()) |
| 837 | return; |
| 838 | [[fallthrough]]; |
| 839 | case Column::Origin: |
| 840 | case Column::Destination: |
| 841 | m_code_widget->SetAddress(v.value<u32>(), CodeViewWidget::SetAddressUpdate::WithDetailedUpdate); |
| 842 | return; |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | void BranchWatchDialog::OnTableContextMenu(const QPoint& pos) const |
| 847 | { |
| 848 | if (m_table_view->horizontalHeader()->hiddenSectionCount() == Column::NumberOfColumns) |
| 849 | { |
| 850 | m_mnu_column_visibility->exec(m_table_view->viewport()->mapToGlobal(pos)); |
| 851 | return; |
| 852 | } |
| 853 | const QModelIndex index = m_table_view->indexAt(pos); |
| 854 | if (!index.isValid()) |
| 855 | return; |
| 856 | m_index_list_temp = m_table_view->selectionModel()->selectedRows(index.column()); |
| 857 | GetTableContextMenu(index)->exec(m_table_view->viewport()->mapToGlobal(pos)); |
| 858 | m_index_list_temp.clear(); |
| 859 | m_index_list_temp.shrink_to_fit(); |
| 860 | } |
| 861 | |
| 862 | void BranchWatchDialog::OnTableHeaderContextMenu(const QPoint& pos) const |
| 863 | { |
| 864 | m_mnu_column_visibility->exec(m_table_view->horizontalHeader()->mapToGlobal(pos)); |
| 865 | } |
| 866 | |
| 867 | void BranchWatchDialog::OnTableDelete() const |
| 868 | { |
| 869 | std::ranges::transform( |
| 870 | m_index_list_temp, m_index_list_temp.begin(), |
| 871 | [this](const QModelIndex& index) { return m_table_proxy->mapToSource(index); }); |
| 872 | std::ranges::sort(m_index_list_temp, std::less{}); |
| 873 | for (const auto& index : std::ranges::reverse_view{m_index_list_temp}) |
| 874 | { |
| 875 | if (!index.isValid()) |
| 876 | continue; |
| 877 | m_table_model->removeRow(index.row()); |
| 878 | } |
| 879 | UpdateStatus(); |
| 880 | } |
| 881 | |
| 882 | void BranchWatchDialog::OnTableDeleteKeypress() const |
| 883 | { |
| 884 | m_index_list_temp = m_table_view->selectionModel()->selectedRows(); |
| 885 | OnTableDelete(); |
| 886 | m_index_list_temp.clear(); |
| 887 | m_index_list_temp.shrink_to_fit(); |
| 888 | } |
| 889 | |
| 890 | void BranchWatchDialog::OnTableSetBLR() const |
| 891 | { |
| 892 | SetStubPatches(0x4e800020); |
| 893 | } |
| 894 | |
| 895 | void BranchWatchDialog::OnTableSetNOP() const |
| 896 | { |
| 897 | SetStubPatches(0x60000000); |
| 898 | } |
| 899 | |
| 900 | void BranchWatchDialog::OnTableInvertCondition() const |
| 901 | { |
| 902 | SetEditPatches([](u32 hex) { |
| 903 | UGeckoInstruction inst = hex; |
| 904 | inst.BO ^= 0b01000; |
| 905 | return inst.hex; |
| 906 | }); |
| 907 | } |
| 908 | |
| 909 | void BranchWatchDialog::OnTableInvertDecrementCheck() const |
| 910 | { |
| 911 | SetEditPatches([](u32 hex) { |
| 912 | UGeckoInstruction inst = hex; |
| 913 | inst.BO ^= 0b00010; |
| 914 | return inst.hex; |
| 915 | }); |
| 916 | } |
| 917 | |
| 918 | void BranchWatchDialog::OnTableMakeUnconditional() const |
| 919 | { |
| 920 | SetEditPatches([](u32 hex) { |
| 921 | UGeckoInstruction inst = hex; |
| 922 | inst.BO = 0b10100; // 1z1zz - Branch always |
| 923 | return inst.hex; |
| 924 | }); |
| 925 | } |
| 926 | |
| 927 | void BranchWatchDialog::OnTableCopyAddress() const |
| 928 | { |
| 929 | auto iter = m_index_list_temp.begin(); |
| 930 | if (iter == m_index_list_temp.end()) |
| 931 | return; |
| 932 | |
| 933 | QString text; |
| 934 | text.reserve(m_index_list_temp.size() * 9 - 1); |
| 935 | while (true) |
| 936 | { |
| 937 | text.append(QString::number(m_table_proxy->data(*iter, UserRole::ClickRole).value<u32>(), 16)); |
| 938 | if (++iter == m_index_list_temp.end()) |
| 939 | break; |
| 940 | text.append(QChar::fromLatin1('\n')); |
| 941 | } |
| 942 | QApplication::clipboard()->setText(text); |
| 943 | } |
| 944 | |
| 945 | void BranchWatchDialog::OnTableSetBreakpointBreak() const |
| 946 | { |
| 947 | SetBreakpoints(true, false); |
| 948 | } |
| 949 | |
| 950 | void BranchWatchDialog::OnTableSetBreakpointLog() const |
| 951 | { |
| 952 | SetBreakpoints(false, true); |
| 953 | } |
| 954 | |
| 955 | void BranchWatchDialog::OnTableSetBreakpointBoth() const |
| 956 | { |
| 957 | SetBreakpoints(true, true); |
| 958 | } |
| 959 | |
| 960 | void BranchWatchDialog::ConnectSlots() |
| 961 | { |
| 962 | const auto* const settings = &Settings::Instance(); |
| 963 | connect(settings, &Settings::EmulationStateChanged, this, |
| 964 | &BranchWatchDialog::OnEmulationStateChanged); |
| 965 | connect(settings, &Settings::ThemeChanged, this, &BranchWatchDialog::OnThemeChanged); |
| 966 | connect(settings, &Settings::DebugFontChanged, m_table_model, &BranchWatchTableModel::setFont); |
| 967 | const auto* const host = Host::GetInstance(); |
| 968 | connect(host, &Host::PPCSymbolsChanged, m_table_model, &BranchWatchTableModel::UpdateSymbols); |
| 969 | } |
| 970 | |
| 971 | void BranchWatchDialog::DisconnectSlots() |
| 972 | { |
| 973 | const auto* const settings = &Settings::Instance(); |
| 974 | disconnect(settings, &Settings::EmulationStateChanged, this, |
| 975 | &BranchWatchDialog::OnEmulationStateChanged); |
| 976 | disconnect(settings, &Settings::ThemeChanged, this, &BranchWatchDialog::OnThemeChanged); |
| 977 | disconnect(settings, &Settings::DebugFontChanged, m_table_model, |
| 978 | &BranchWatchTableModel::OnDebugFontChanged); |
| 979 | const auto* const host = Host::GetInstance(); |
| 980 | disconnect(host, &Host::PPCSymbolsChanged, m_table_model, |
| 981 | &BranchWatchTableModel::OnPPCSymbolsChanged); |
| 982 | } |
| 983 | |
| 984 | void BranchWatchDialog::Show() |
| 985 | { |
| 986 | ConnectSlots(); |
| 987 | // Hit every slot that may have missed a signal while this widget was hidden. |
| 988 | OnEmulationStateChanged(Core::GetState(m_system)); |
| 989 | OnThemeChanged(); |
| 990 | m_table_model->OnDebugFontChanged(Settings::Instance().GetDebugFont()); |
| 991 | m_table_model->OnPPCSymbolsChanged(); |
| 992 | } |
| 993 | |
| 994 | void BranchWatchDialog::Hide() |
| 995 | { |
| 996 | DisconnectSlots(); |
| 997 | if (m_timer->isActive()) |
| 998 | m_timer->stop(); |
| 999 | } |
| 1000 | |
| 1001 | void BranchWatchDialog::LoadQSettings() |
| 1002 | { |
| 1003 | const auto& settings = Settings::GetQSettings(); |
| 1004 | restoreGeometry(settings.value(QStringLiteral("branchwatchdialog/geometry")).toByteArray()); |
| 1005 | m_table_view->horizontalHeader()->restoreState( // Restore column visibility state. |
| 1006 | settings.value(QStringLiteral("branchwatchdialog/tableheader/state")).toByteArray()); |
| 1007 | m_act_branch_type_filters->setVisible( |
| 1008 | !settings.value(QStringLiteral("branchwatchdialog/toolbar/branch_type_hidden")).toBool()); |
| 1009 | m_act_origin_destin_filters->setVisible( |
| 1010 | !settings.value(QStringLiteral("branchwatchdialog/toolbar/origin_destin_hidden")).toBool()); |
| 1011 | m_act_condition_filters->setVisible( |
| 1012 | !settings.value(QStringLiteral("branchwatchdialog/toolbar/condition_hidden")).toBool()); |
| 1013 | m_act_misc_controls->setVisible( |
| 1014 | !settings.value(QStringLiteral("branchwatchdialog/toolbar/misc_controls_hidden")).toBool()); |
| 1015 | } |
| 1016 | |
| 1017 | void BranchWatchDialog::SaveQSettings() const |
| 1018 | { |
| 1019 | auto& settings = Settings::GetQSettings(); |
| 1020 | settings.setValue(QStringLiteral("branchwatchdialog/geometry"), saveGeometry()); |
| 1021 | settings.setValue(QStringLiteral("branchwatchdialog/tableheader/state"), |
| 1022 | m_table_view->horizontalHeader()->saveState()); |
| 1023 | settings.setValue(QStringLiteral("branchwatchdialog/toolbar/branch_type_hidden"), |
| 1024 | !m_act_branch_type_filters->isVisible()); |
| 1025 | settings.setValue(QStringLiteral("branchwatchdialog/toolbar/origin_destin_hidden"), |
| 1026 | !m_act_origin_destin_filters->isVisible()); |
| 1027 | settings.setValue(QStringLiteral("branchwatchdialog/toolbar/condition_hidden"), |
| 1028 | !m_act_condition_filters->isVisible()); |
| 1029 | settings.setValue(QStringLiteral("branchwatchdialog/toolbar/misc_controls_hidden"), |
| 1030 | !m_act_misc_controls->isVisible()); |
| 1031 | } |
| 1032 | |
| 1033 | void BranchWatchDialog::Update() const |
| 1034 | { |
| 1035 | if (m_branch_watch.GetRecordingPhase() == Core::BranchWatch::Phase::Blacklist) |
| 1036 | UpdateStatus(); |
| 1037 | m_table_model->UpdateHits(); |
| 1038 | } |
| 1039 | |
| 1040 | void BranchWatchDialog::UpdateStatus() const |
| 1041 | { |
| 1042 | switch (m_branch_watch.GetRecordingPhase()) |
| 1043 | { |
| 1044 | case Core::BranchWatch::Phase::Blacklist: |
| 1045 | { |
| 1046 | const std::size_t candidate_size = m_branch_watch.GetCollectionSize(); |
| 1047 | const std::size_t blacklist_size = m_branch_watch.GetBlacklistSize(); |
| 1048 | if (blacklist_size == 0) |
| 1049 | { |
| 1050 | m_status_bar->showMessage(tr("Candidates: %1").arg(candidate_size)); |
| 1051 | return; |
| 1052 | } |
| 1053 | m_status_bar->showMessage(tr("Candidates: %1 | Excluded: %2 | Remaining: %3") |
| 1054 | .arg(candidate_size) |
| 1055 | .arg(blacklist_size) |
| 1056 | .arg(candidate_size - blacklist_size)); |
| 1057 | return; |
| 1058 | } |
| 1059 | case Core::BranchWatch::Phase::Reduction: |
| 1060 | { |
| 1061 | const std::size_t candidate_size = m_branch_watch.GetSelection().size(); |
| 1062 | if (candidate_size == 0) |
| 1063 | { |
| 1064 | m_status_bar->showMessage(tr("Zero candidates remaining.")); |
| 1065 | return; |
| 1066 | } |
| 1067 | const std::size_t remaining_size = m_table_proxy->rowCount(); |
| 1068 | m_status_bar->showMessage(tr("Candidates: %1 | Filtered: %2 | Remaining: %3") |
| 1069 | .arg(candidate_size) |
| 1070 | .arg(candidate_size - remaining_size) |
| 1071 | .arg(remaining_size)); |
| 1072 | return; |
| 1073 | } |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | void BranchWatchDialog::UpdateIcons() |
| 1078 | { |
| 1079 | m_icn_full = Resources::GetThemeIcon("debugger_breakpoint"); |
| 1080 | m_icn_partial = Resources::GetThemeIcon("stop"); |
| 1081 | } |
| 1082 | |
| 1083 | void BranchWatchDialog::Save(const Core::CPUThreadGuard& guard, const std::string& filepath) |
| 1084 | { |
| 1085 | File::IOFile file(filepath, "w"); |
| 1086 | if (!file.IsOpen()) |
| 1087 | { |
| 1088 | ModalMessageBox::warning( |
| 1089 | this, tr("Error"), |
| 1090 | tr("Failed to save Branch Watch snapshot \"%1\"").arg(QString::fromStdString(filepath))); |
| 1091 | return; |
| 1092 | } |
| 1093 | |
| 1094 | m_table_model->Save(guard, file.GetHandle()); |
| 1095 | } |
| 1096 | |
| 1097 | void BranchWatchDialog::Load(const Core::CPUThreadGuard& guard, const std::string& filepath) |
| 1098 | { |
| 1099 | File::IOFile file(filepath, "r"); |
| 1100 | if (!file.IsOpen()) |
| 1101 | { |
| 1102 | ModalMessageBox::warning( |
| 1103 | this, tr("Error"), |
| 1104 | tr("Failed to open Branch Watch snapshot \"%1\"").arg(QString::fromStdString(filepath))); |
| 1105 | return; |
| 1106 | } |
| 1107 | |
| 1108 | m_table_model->Load(guard, file.GetHandle()); |
| 1109 | m_btn_wipe_recent_hits->setEnabled(m_branch_watch.GetRecordingPhase() == |
| 1110 | Core::BranchWatch::Phase::Reduction); |
| 1111 | } |
| 1112 | |
| 1113 | void BranchWatchDialog::AutoSave(const Core::CPUThreadGuard& guard) |
| 1114 | { |
| 1115 | if (!m_act_autosave->isChecked() || !m_branch_watch.CanSave()) |
| 1116 | return; |
| 1117 | Save(guard, m_autosave_filepath ? m_autosave_filepath.value() : GetSnapshotDefaultFilepath()); |
| 1118 | } |
| 1119 | |
| 1120 | void BranchWatchDialog::SetStubPatches(u32 value) const |
| 1121 | { |
| 1122 | auto& debug_interface = m_system.GetPowerPC().GetDebugInterface(); |
| 1123 | for (const Core::CPUThreadGuard guard(m_system); const QModelIndex& index : m_index_list_temp) |
| 1124 | { |
| 1125 | debug_interface.SetPatch(guard, m_table_proxy->data(index, UserRole::ClickRole).value<u32>(), |
| 1126 | value); |
| 1127 | m_table_proxy->SetInspected(index); |
| 1128 | } |
| 1129 | // TODO: This is not ideal. What I need is a signal for when memory has been changed by the GUI, |
| 1130 | // but I cannot find one. UpdateDisasmDialog comes close, but does too much in one signal. For |
| 1131 | // example, CodeViewWidget will scroll to the current PC when UpdateDisasmDialog is signaled. This |
| 1132 | // seems like a pervasive issue. For example, modifying an instruction in the CodeViewWidget will |
| 1133 | // not reflect in the MemoryViewWidget, and vice versa. Neither of these widgets changing memory |
| 1134 | // will reflect in the JITWidget, either. At the very least, we can make sure the CodeWidget |
| 1135 | // is updated in an acceptable way. |
| 1136 | m_code_widget->Update(); |
| 1137 | } |
| 1138 | |
| 1139 | void BranchWatchDialog::SetEditPatches(u32 (*transform)(u32)) const |
| 1140 | { |
| 1141 | auto& debug_interface = m_system.GetPowerPC().GetDebugInterface(); |
| 1142 | for (const Core::CPUThreadGuard guard(m_system); const QModelIndex& index : m_index_list_temp) |
| 1143 | { |
| 1144 | const Core::BranchWatchCollectionKey& k = |
| 1145 | m_table_proxy->GetBranchWatchSelection(index).collection_ptr->first; |
| 1146 | // This function assumes patches apply to the origin address, unlike SetStubPatches. |
| 1147 | debug_interface.SetPatch(guard, k.origin_addr, transform(k.original_inst.hex)); |
| 1148 | m_table_proxy->SetInspected(index); |
| 1149 | } |
| 1150 | // TODO: Same issue as SetStubPatches. |
| 1151 | m_code_widget->Update(); |
| 1152 | } |
| 1153 | |
| 1154 | void BranchWatchDialog::SetBreakpoints(bool break_on_hit, bool log_on_hit) const |
| 1155 | { |
| 1156 | auto& breakpoints = m_system.GetPowerPC().GetBreakPoints(); |
| 1157 | for (const QModelIndex& index : m_index_list_temp) |
| 1158 | { |
| 1159 | const u32 address = m_table_proxy->data(index, UserRole::ClickRole).value<u32>(); |
| 1160 | breakpoints.Add(address, break_on_hit, log_on_hit, {}); |
| 1161 | } |
| 1162 | emit Host::GetInstance()->PPCBreakpointsChanged(); |
| 1163 | } |
| 1164 | |
| 1165 | void BranchWatchDialog::SetBreakpointMenuActionsIcons() const |
| 1166 | { |
| 1167 | qsizetype bp_break_count = 0, bp_log_count = 0, bp_both_count = 0; |
| 1168 | for (auto& breakpoints = m_system.GetPowerPC().GetBreakPoints(); |
| 1169 | const QModelIndex& index : m_index_list_temp) |
| 1170 | { |
| 1171 | if (const TBreakPoint* bp = breakpoints.GetRegularBreakpoint( |
| 1172 | m_table_proxy->data(index, UserRole::ClickRole).value<u32>())) |
| 1173 | { |
| 1174 | if (bp->break_on_hit && bp->log_on_hit) |
| 1175 | { |
| 1176 | bp_both_count += 1; |
| 1177 | continue; |
| 1178 | } |
| 1179 | bp_break_count += bp->break_on_hit; |
| 1180 | bp_log_count += bp->log_on_hit; |
| 1181 | } |
| 1182 | } |
| 1183 | const qsizetype selected_row_count = m_index_list_temp.size(); |
| 1184 | m_act_break_on_hit->setIconVisibleInMenu(bp_break_count != 0); |
| 1185 | m_act_break_on_hit->setIcon(bp_break_count == selected_row_count ? m_icn_full : m_icn_partial); |
| 1186 | m_act_log_on_hit->setIconVisibleInMenu(bp_log_count != 0); |
| 1187 | m_act_log_on_hit->setIcon(bp_log_count == selected_row_count ? m_icn_full : m_icn_partial); |
| 1188 | m_act_both_on_hit->setIconVisibleInMenu(bp_both_count != 0); |
| 1189 | m_act_both_on_hit->setIcon(bp_both_count == selected_row_count ? m_icn_full : m_icn_partial); |
| 1190 | } |
| 1191 | |
| 1192 | QMenu* BranchWatchDialog::GetTableContextMenu(const QModelIndex& index) const |
| 1193 | { |
| 1194 | const bool core_initialized = Core::GetState(m_system) != Core::State::Uninitialized; |
| 1195 | switch (index.column()) |
| 1196 | { |
| 1197 | case Column::Instruction: |
| 1198 | RefreshContextMenuActions_Instruction(core_initialized); |
| 1199 | return m_mnu_table_context_instruction; |
| 1200 | case Column::Condition: |
| 1201 | RefreshContextMenuActions_Condition(core_initialized); |
| 1202 | return m_mnu_table_context_condition; |
| 1203 | case Column::Origin: |
| 1204 | RefreshContextMenuActions_Origin(core_initialized); |
| 1205 | return m_mnu_table_context_origin; |
| 1206 | case Column::Destination: |
| 1207 | RefreshContextMenuActions_Destin(core_initialized); |
| 1208 | return m_mnu_table_context_destin; |
| 1209 | case Column::RecentHits: |
| 1210 | case Column::TotalHits: |
| 1211 | return m_mnu_table_context_other; |
| 1212 | case Column::OriginSymbol: |
| 1213 | case Column::DestinSymbol: |
| 1214 | RefreshContextMenuActions_Symbol(core_initialized); |
| 1215 | return m_mnu_table_context_symbol; |
| 1216 | } |
| 1217 | static_assert(Column::NumberOfColumns == 8); |
| 1218 | std::unreachable(); |
| 1219 | } |
| 1220 | |
| 1221 | void BranchWatchDialog::RefreshVisibleContextMenuActions(bool core_initialized) const |
| 1222 | { |
| 1223 | if (m_mnu_table_context_instruction->isVisible()) |
| 1224 | RefreshContextMenuActions_Instruction(core_initialized); |
| 1225 | else if (m_mnu_table_context_condition->isVisible()) |
| 1226 | RefreshContextMenuActions_Condition(core_initialized); |
| 1227 | else if (m_mnu_table_context_origin->isVisible()) |
| 1228 | RefreshContextMenuActions_Origin(core_initialized); |
| 1229 | else if (m_mnu_table_context_destin->isVisible()) |
| 1230 | RefreshContextMenuActions_Destin(core_initialized); |
| 1231 | else if (m_mnu_table_context_symbol->isVisible()) |
| 1232 | RefreshContextMenuActions_Symbol(core_initialized); |
| 1233 | } |
| 1234 | |
| 1235 | void BranchWatchDialog::RefreshContextMenuActions_Instruction(bool core_initialized) const |
| 1236 | { |
| 1237 | const bool all_branches_conditional = // Taking advantage of short-circuit evaluation here. |
| 1238 | core_initialized && std::ranges::all_of(m_index_list_temp, [this](const QModelIndex& index) { |
| 1239 | return BranchIsConditional( |
| 1240 | m_table_proxy->GetBranchWatchSelection(index).collection_ptr->first.original_inst); |
| 1241 | }); |
| 1242 | m_act_invert_condition->setEnabled(all_branches_conditional); |
| 1243 | m_act_invert_decrement_check->setEnabled(all_branches_conditional); |
| 1244 | } |
| 1245 | |
| 1246 | void BranchWatchDialog::RefreshContextMenuActions_Condition(bool core_initialized) const |
| 1247 | { |
| 1248 | const bool all_branches_conditional = // Taking advantage of short-circuit evaluation here. |
| 1249 | core_initialized && std::ranges::all_of(m_index_list_temp, [this](const QModelIndex& index) { |
| 1250 | return BranchIsConditional( |
| 1251 | m_table_proxy->GetBranchWatchSelection(index).collection_ptr->first.original_inst); |
| 1252 | }); |
| 1253 | m_act_make_unconditional->setEnabled(all_branches_conditional); |
| 1254 | } |
| 1255 | |
| 1256 | void BranchWatchDialog::RefreshContextMenuActions_Origin(bool core_initialized) const |
| 1257 | { |
| 1258 | SetBreakpointMenuActionsIcons(); |
| 1259 | m_act_insert_nop->setEnabled(core_initialized); |
| 1260 | m_act_copy_address->setEnabled(true); |
| 1261 | m_mnu_set_breakpoint->setEnabled(true); |
| 1262 | } |
| 1263 | |
| 1264 | void BranchWatchDialog::RefreshContextMenuActions_Destin(bool core_initialized) const |
| 1265 | { |
| 1266 | SetBreakpointMenuActionsIcons(); |
| 1267 | const bool all_branches_save_lr = // Taking advantage of short-circuit evaluation here. |
| 1268 | core_initialized && std::ranges::all_of(m_index_list_temp, [this](const QModelIndex& index) { |
| 1269 | return m_table_proxy->GetBranchWatchSelection(index).collection_ptr->first.original_inst.LK; |
| 1270 | }); |
| 1271 | m_act_insert_blr->setEnabled(all_branches_save_lr); |
| 1272 | m_act_copy_address->setEnabled(true); |
| 1273 | m_mnu_set_breakpoint->setEnabled(true); |
| 1274 | } |
| 1275 | |
| 1276 | void BranchWatchDialog::RefreshContextMenuActions_Symbol(bool core_initialized) const |
| 1277 | { |
| 1278 | SetBreakpointMenuActionsIcons(); |
| 1279 | const bool all_symbols_valid = |
| 1280 | std::ranges::all_of(m_index_list_temp, [this](const QModelIndex& index) { |
| 1281 | return m_table_proxy->data(index, UserRole::ClickRole).isValid(); |
| 1282 | }); |
| 1283 | m_act_insert_blr->setEnabled(core_initialized && all_symbols_valid); |
| 1284 | m_act_copy_address->setEnabled(all_symbols_valid); |
| 1285 | m_mnu_set_breakpoint->setEnabled(all_symbols_valid); |
| 1286 | } |