| 1 | // Copyright 2024 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Debugger/BranchWatchTableModel.h" |
| 5 | |
| 6 | #include <array> |
| 7 | #include <cstddef> |
| 8 | #include <utility> |
| 9 | |
| 10 | #include <QBrush> |
| 11 | |
| 12 | #include "Common/Assert.h" |
| 13 | #include "Common/GekkoDisassembler.h" |
| 14 | #include "Core/Debugger/BranchWatch.h" |
| 15 | #include "Core/PowerPC/PPCSymbolDB.h" |
| 16 | |
| 17 | QVariant BranchWatchTableModel::data(const QModelIndex& index, int role) const |
| 18 | { |
| 19 | if (!index.isValid()) |
| 20 | return QVariant(); |
| 21 | |
| 22 | switch (role) |
| 23 | { |
| 24 | case Qt::DisplayRole: |
| 25 | return DisplayRoleData(index); |
| 26 | case Qt::FontRole: |
| 27 | return FontRoleData(index); |
| 28 | case Qt::TextAlignmentRole: |
| 29 | return TextAlignmentRoleData(index); |
| 30 | case Qt::ForegroundRole: |
| 31 | return ForegroundRoleData(index); |
| 32 | case UserRole::ClickRole: |
| 33 | return ClickRoleData(index); |
| 34 | case UserRole::SortRole: |
| 35 | return SortRoleData(index); |
| 36 | } |
| 37 | return QVariant(); |
| 38 | } |
| 39 | |
| 40 | QVariant BranchWatchTableModel::headerData(int section, Qt::Orientation orientation, int role) const |
| 41 | { |
| 42 | if (orientation == Qt::Vertical || role != Qt::DisplayRole) |
| 43 | return QVariant(); |
| 44 | |
| 45 | static constexpr std::array<const char*, Column::NumberOfColumns> headers = { |
| 46 | QT_TR_NOOP("Instr."), QT_TR_NOOP("Cond."), |
| 47 | QT_TR_NOOP("Origin"), QT_TR_NOOP("Destination"), |
| 48 | QT_TR_NOOP("Recent Hits"), QT_TR_NOOP("Total Hits"), |
| 49 | QT_TR_NOOP("Origin Symbol"), QT_TR_NOOP("Destination Symbol")}; |
| 50 | return tr(headers[section]); |
| 51 | } |
| 52 | |
| 53 | int BranchWatchTableModel::rowCount(const QModelIndex& parent) const |
| 54 | { |
| 55 | if (parent.isValid()) |
| 56 | return 0; |
| 57 | return static_cast<int>(m_branch_watch.GetSelection().size()); |
| 58 | } |
| 59 | |
| 60 | int BranchWatchTableModel::columnCount(const QModelIndex& parent) const |
| 61 | { |
| 62 | if (parent.isValid()) |
| 63 | return 0; |
| 64 | return Column::NumberOfColumns; |
| 65 | } |
| 66 | |
| 67 | bool BranchWatchTableModel::removeRows(int row, int count, const QModelIndex& parent) |
| 68 | { |
| 69 | if (parent.isValid() || row < 0) |
| 70 | return false; |
| 71 | if (count <= 0) |
| 72 | return true; |
| 73 | |
| 74 | auto& selection = m_branch_watch.GetSelection(); |
| 75 | beginRemoveRows(parent, row, row + count - 1); // Last is inclusive in Qt! |
| 76 | selection.erase(selection.begin() + row, selection.begin() + row + count); |
| 77 | m_symbol_list.remove(row, count); |
| 78 | endRemoveRows(); |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | void BranchWatchTableModel::OnClearBranchWatch(const Core::CPUThreadGuard& guard) |
| 83 | { |
| 84 | emit layoutAboutToBeChanged(); |
| 85 | m_branch_watch.Clear(guard); |
| 86 | m_symbol_list.clear(); |
| 87 | emit layoutChanged(); |
| 88 | } |
| 89 | |
| 90 | void BranchWatchTableModel::OnCodePathWasTaken(const Core::CPUThreadGuard& guard) |
| 91 | { |
| 92 | emit layoutAboutToBeChanged(); |
| 93 | m_branch_watch.IsolateHasExecuted(guard); |
| 94 | PrefetchSymbols(); |
| 95 | emit layoutChanged(); |
| 96 | } |
| 97 | |
| 98 | void BranchWatchTableModel::OnCodePathNotTaken(const Core::CPUThreadGuard& guard) |
| 99 | { |
| 100 | emit layoutAboutToBeChanged(); |
| 101 | m_branch_watch.IsolateNotExecuted(guard); |
| 102 | PrefetchSymbols(); |
| 103 | emit layoutChanged(); |
| 104 | } |
| 105 | |
| 106 | void BranchWatchTableModel::OnBranchWasOverwritten(const Core::CPUThreadGuard& guard) |
| 107 | { |
| 108 | emit layoutAboutToBeChanged(); |
| 109 | m_branch_watch.IsolateWasOverwritten(guard); |
| 110 | PrefetchSymbols(); |
| 111 | emit layoutChanged(); |
| 112 | } |
| 113 | |
| 114 | void BranchWatchTableModel::OnBranchNotOverwritten(const Core::CPUThreadGuard& guard) |
| 115 | { |
| 116 | emit layoutAboutToBeChanged(); |
| 117 | m_branch_watch.IsolateNotOverwritten(guard); |
| 118 | PrefetchSymbols(); |
| 119 | emit layoutChanged(); |
| 120 | } |
| 121 | |
| 122 | void BranchWatchTableModel::OnWipeRecentHits() |
| 123 | { |
| 124 | const int row_count = rowCount(); |
| 125 | if (row_count <= 0) |
| 126 | return; |
| 127 | static const QList<int> roles = {Qt::DisplayRole}; |
| 128 | m_branch_watch.UpdateHitsSnapshot(); |
| 129 | const int last = row_count - 1; |
| 130 | emit dataChanged(createIndex(0, Column::RecentHits), createIndex(last, Column::RecentHits), |
| 131 | roles); |
| 132 | } |
| 133 | |
| 134 | void BranchWatchTableModel::OnWipeInspection() |
| 135 | { |
| 136 | const int row_count = rowCount(); |
| 137 | if (row_count <= 0) |
| 138 | return; |
| 139 | static const QList<int> roles = {Qt::FontRole, Qt::ForegroundRole}; |
| 140 | m_branch_watch.ClearSelectionInspection(); |
| 141 | const int last = row_count - 1; |
| 142 | emit dataChanged(createIndex(0, Column::Origin), createIndex(last, Column::Destination), roles); |
| 143 | emit dataChanged(createIndex(0, Column::OriginSymbol), createIndex(last, Column::DestinSymbol), |
| 144 | roles); |
| 145 | } |
| 146 | |
| 147 | void BranchWatchTableModel::OnDebugFontChanged(const QFont& font) |
| 148 | { |
| 149 | setFont(font); |
| 150 | } |
| 151 | |
| 152 | void BranchWatchTableModel::OnPPCSymbolsChanged() |
| 153 | { |
| 154 | UpdateSymbols(); |
| 155 | } |
| 156 | |
| 157 | void BranchWatchTableModel::Save(const Core::CPUThreadGuard& guard, std::FILE* file) const |
| 158 | { |
| 159 | m_branch_watch.Save(guard, file); |
| 160 | } |
| 161 | |
| 162 | void BranchWatchTableModel::Load(const Core::CPUThreadGuard& guard, std::FILE* file) |
| 163 | { |
| 164 | emit layoutAboutToBeChanged(); |
| 165 | m_branch_watch.Load(guard, file); |
| 166 | PrefetchSymbols(); |
| 167 | emit layoutChanged(); |
| 168 | } |
| 169 | |
| 170 | void BranchWatchTableModel::UpdateSymbols() |
| 171 | { |
| 172 | const int row_count = rowCount(); |
| 173 | if (row_count <= 0) |
| 174 | return; |
| 175 | static const QList<int> roles = {Qt::DisplayRole}; |
| 176 | PrefetchSymbols(); |
| 177 | const int last = row_count - 1; |
| 178 | emit dataChanged(createIndex(0, Column::OriginSymbol), createIndex(last, Column::DestinSymbol), |
| 179 | roles); |
| 180 | } |
| 181 | |
| 182 | void BranchWatchTableModel::UpdateHits() |
| 183 | { |
| 184 | const int row_count = rowCount(); |
| 185 | if (row_count <= 0) |
| 186 | return; |
| 187 | static const QList<int> roles = {Qt::DisplayRole}; |
| 188 | const int last = row_count - 1; |
| 189 | emit dataChanged(createIndex(0, Column::RecentHits), createIndex(last, Column::TotalHits), roles); |
| 190 | } |
| 191 | |
| 192 | void BranchWatchTableModel::SetInspected(const QModelIndex& index) |
| 193 | { |
| 194 | const int row = index.row(); |
| 195 | switch (index.column()) |
| 196 | { |
| 197 | case Column::Instruction: |
| 198 | SetInspected(index, Core::BranchWatchSelectionInspection::InvertBranchOption); |
| 199 | return; |
| 200 | case Column::Condition: |
| 201 | SetInspected(index, Core::BranchWatchSelectionInspection::MakeUnconditional); |
| 202 | return; |
| 203 | case Column::Origin: |
| 204 | SetOriginInspected(m_branch_watch.GetSelection()[row].collection_ptr->first.origin_addr); |
| 205 | return; |
| 206 | case Column::Destination: |
| 207 | SetDestinInspected(m_branch_watch.GetSelection()[row].collection_ptr->first.destin_addr, false); |
| 208 | return; |
| 209 | case Column::OriginSymbol: |
| 210 | SetSymbolInspected(m_symbol_list[row].origin_addr.value<u32>(), false); |
| 211 | return; |
| 212 | case Column::DestinSymbol: |
| 213 | SetSymbolInspected(m_symbol_list[row].destin_addr.value<u32>(), false); |
| 214 | return; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void BranchWatchTableModel::SetInspected(const QModelIndex& index, |
| 219 | Core::BranchWatchSelectionInspection inspection) |
| 220 | { |
| 221 | static const QList<int> roles = {Qt::FontRole, Qt::ForegroundRole}; |
| 222 | m_branch_watch.SetSelectedInspected(index.row(), inspection); |
| 223 | emit dataChanged(index, index, roles); |
| 224 | } |
| 225 | |
| 226 | void BranchWatchTableModel::SetOriginInspected(u32 origin_addr) |
| 227 | { |
| 228 | const Core::BranchWatch::Selection& selection = m_branch_watch.GetSelection(); |
| 229 | for (std::size_t i = 0; i < selection.size(); ++i) |
| 230 | { |
| 231 | if (selection[i].collection_ptr->first.origin_addr != origin_addr) |
| 232 | continue; |
| 233 | SetInspected(createIndex(static_cast<int>(i), Column::Origin), |
| 234 | Core::BranchWatchSelectionInspection::SetOriginNOP); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | void BranchWatchTableModel::SetDestinInspected(u32 destin_addr, bool nested) |
| 239 | { |
| 240 | const Core::BranchWatch::Selection& selection = m_branch_watch.GetSelection(); |
| 241 | for (std::size_t i = 0; i < selection.size(); ++i) |
| 242 | { |
| 243 | if (selection[i].collection_ptr->first.destin_addr != destin_addr) |
| 244 | continue; |
| 245 | SetInspected(createIndex(static_cast<int>(i), Column::Destination), |
| 246 | Core::BranchWatchSelectionInspection::SetDestinBLR); |
| 247 | } |
| 248 | |
| 249 | if (nested) |
| 250 | return; |
| 251 | SetSymbolInspected(destin_addr, true); |
| 252 | } |
| 253 | |
| 254 | void BranchWatchTableModel::SetSymbolInspected(u32 symbol_addr, bool nested) |
| 255 | { |
| 256 | for (qsizetype i = 0; i < m_symbol_list.size(); ++i) |
| 257 | { |
| 258 | const SymbolListValueType& value = m_symbol_list[i]; |
| 259 | if (value.origin_addr.isValid() && value.origin_addr.value<u32>() == symbol_addr) |
| 260 | { |
| 261 | SetInspected(createIndex(static_cast<int>(i), Column::OriginSymbol), |
| 262 | Core::BranchWatchSelectionInspection::SetOriginSymbolBLR); |
| 263 | } |
| 264 | if (value.destin_addr.isValid() && value.destin_addr.value<u32>() == symbol_addr) |
| 265 | { |
| 266 | SetInspected(createIndex(static_cast<int>(i), Column::DestinSymbol), |
| 267 | Core::BranchWatchSelectionInspection::SetDestinSymbolBLR); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | if (nested) |
| 272 | return; |
| 273 | SetDestinInspected(symbol_addr, true); |
| 274 | } |
| 275 | |
| 276 | const Core::BranchWatchSelectionValueType& |
| 277 | BranchWatchTableModel::GetBranchWatchSelection(const QModelIndex& index) const |
| 278 | { |
| 279 | ASSERT(index.isValid()); |
| 280 | return m_branch_watch.GetSelection()[index.row()]; |
| 281 | } |
| 282 | |
| 283 | void BranchWatchTableModel::PrefetchSymbols() |
| 284 | { |
| 285 | if (m_branch_watch.GetRecordingPhase() != Core::BranchWatch::Phase::Reduction) |
| 286 | return; |
| 287 | |
| 288 | const Core::BranchWatch::Selection& selection = m_branch_watch.GetSelection(); |
| 289 | m_symbol_list.clear(); |
| 290 | m_symbol_list.reserve(selection.size()); |
| 291 | for (const Core::BranchWatch::Selection::value_type& value : selection) |
| 292 | { |
| 293 | const Core::BranchWatch::Collection::value_type* const kv = value.collection_ptr; |
| 294 | m_symbol_list.emplace_back(m_ppc_symbol_db.GetSymbolFromAddr(kv->first.origin_addr), |
| 295 | m_ppc_symbol_db.GetSymbolFromAddr(kv->first.destin_addr)); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | static QVariant GetValidSymbolStringVariant(const QVariant& symbol_name_v) |
| 300 | { |
| 301 | if (symbol_name_v.isValid()) |
| 302 | return symbol_name_v; |
| 303 | return QStringLiteral(" --- "); |
| 304 | } |
| 305 | |
| 306 | static QString GetInstructionMnemonic(u32 hex) |
| 307 | { |
| 308 | const std::string disas = Common::GekkoDisassembler::Disassemble(hex, 0); |
| 309 | const std::string::size_type split = disas.find('\t'); |
| 310 | // I wish I could disassemble just the mnemonic! |
| 311 | if (split == std::string::npos) |
| 312 | return QString::fromStdString(disas); |
| 313 | return QString::fromLatin1(disas.data(), split); |
| 314 | } |
| 315 | |
| 316 | static QString GetConditionString(const Core::BranchWatch::Selection::value_type& value, |
| 317 | const Core::BranchWatch::Collection::value_type* kv) |
| 318 | { |
| 319 | if (value.condition == false) |
| 320 | return BranchWatchTableModel::tr("false"); |
| 321 | if (BranchIsConditional(kv->first.original_inst)) |
| 322 | return BranchWatchTableModel::tr("true"); |
| 323 | return QStringLiteral(""); |
| 324 | } |
| 325 | |
| 326 | QVariant BranchWatchTableModel::DisplayRoleData(const QModelIndex& index) const |
| 327 | { |
| 328 | switch (index.column()) |
| 329 | { |
| 330 | case Column::OriginSymbol: |
| 331 | return GetValidSymbolStringVariant(m_symbol_list[index.row()].origin_name); |
| 332 | case Column::DestinSymbol: |
| 333 | return GetValidSymbolStringVariant(m_symbol_list[index.row()].destin_name); |
| 334 | } |
| 335 | const Core::BranchWatch::Selection::value_type& value = |
| 336 | m_branch_watch.GetSelection()[index.row()]; |
| 337 | const Core::BranchWatch::Collection::value_type* kv = value.collection_ptr; |
| 338 | switch (index.column()) |
| 339 | { |
| 340 | case Column::Instruction: |
| 341 | return GetInstructionMnemonic(kv->first.original_inst.hex); |
| 342 | case Column::Condition: |
| 343 | return GetConditionString(value, kv); |
| 344 | case Column::Origin: |
| 345 | return QString::number(kv->first.origin_addr, 16); |
| 346 | case Column::Destination: |
| 347 | return QString::number(kv->first.destin_addr, 16); |
| 348 | case Column::RecentHits: |
| 349 | return QString::number(kv->second.total_hits - kv->second.hits_snapshot); |
| 350 | case Column::TotalHits: |
| 351 | return QString::number(kv->second.total_hits); |
| 352 | } |
| 353 | static_assert(Column::NumberOfColumns == 8); |
| 354 | std::unreachable(); |
| 355 | } |
| 356 | |
| 357 | QVariant BranchWatchTableModel::FontRoleData(const QModelIndex& index) const |
| 358 | { |
| 359 | m_font.setBold([&]() -> bool { |
| 360 | using Inspection = Core::BranchWatchSelectionInspection; |
| 361 | const auto get_bit_test = [this, &index](Inspection inspection_mask) -> bool { |
| 362 | const Inspection inspection = m_branch_watch.GetSelection()[index.row()].inspection; |
| 363 | return (inspection & inspection_mask) != Inspection{}; |
| 364 | }; |
| 365 | switch (index.column()) |
| 366 | { |
| 367 | case Column::Instruction: |
| 368 | return get_bit_test(Inspection::InvertBranchOption); |
| 369 | case Column::Condition: |
| 370 | return get_bit_test(Inspection::MakeUnconditional); |
| 371 | case Column::Origin: |
| 372 | return get_bit_test(Inspection::SetOriginNOP); |
| 373 | case Column::Destination: |
| 374 | return get_bit_test(Inspection::SetDestinBLR); |
| 375 | case Column::OriginSymbol: |
| 376 | return get_bit_test(Inspection::SetOriginSymbolBLR); |
| 377 | case Column::DestinSymbol: |
| 378 | return get_bit_test(Inspection::SetDestinSymbolBLR); |
| 379 | } |
| 380 | // Importantly, this code path avoids subscripting the selection to get an inspection value. |
| 381 | return false; |
| 382 | }()); |
| 383 | return m_font; |
| 384 | } |
| 385 | |
| 386 | QVariant BranchWatchTableModel::TextAlignmentRoleData(const QModelIndex& index) const |
| 387 | { |
| 388 | // Qt enums become QFlags when operators are used. QVariant's constructors don't support QFlags. |
| 389 | switch (index.column()) |
| 390 | { |
| 391 | case Column::Condition: |
| 392 | case Column::Origin: |
| 393 | case Column::Destination: |
| 394 | return Qt::AlignCenter; |
| 395 | case Column::RecentHits: |
| 396 | case Column::TotalHits: |
| 397 | return QVariant::fromValue(Qt::AlignRight | Qt::AlignVCenter); |
| 398 | case Column::Instruction: |
| 399 | case Column::OriginSymbol: |
| 400 | case Column::DestinSymbol: |
| 401 | return QVariant::fromValue(Qt::AlignLeft | Qt::AlignVCenter); |
| 402 | } |
| 403 | static_assert(Column::NumberOfColumns == 8); |
| 404 | std::unreachable(); |
| 405 | } |
| 406 | |
| 407 | QVariant BranchWatchTableModel::ForegroundRoleData(const QModelIndex& index) const |
| 408 | { |
| 409 | using Inspection = Core::BranchWatchSelectionInspection; |
| 410 | const auto get_brush_v = [this, &index](Inspection inspection_mask) -> QVariant { |
| 411 | const Inspection inspection = m_branch_watch.GetSelection()[index.row()].inspection; |
| 412 | return (inspection & inspection_mask) != Inspection{} ? QBrush(Qt::red) : QVariant(); |
| 413 | }; |
| 414 | switch (index.column()) |
| 415 | { |
| 416 | case Column::Instruction: |
| 417 | return get_brush_v(Inspection::InvertBranchOption); |
| 418 | case Column::Condition: |
| 419 | return get_brush_v(Inspection::MakeUnconditional); |
| 420 | case Column::Origin: |
| 421 | return get_brush_v(Inspection::SetOriginNOP); |
| 422 | case Column::Destination: |
| 423 | return get_brush_v(Inspection::SetDestinBLR); |
| 424 | case Column::OriginSymbol: |
| 425 | return get_brush_v(Inspection::SetOriginSymbolBLR); |
| 426 | case Column::DestinSymbol: |
| 427 | return get_brush_v(Inspection::SetDestinSymbolBLR); |
| 428 | } |
| 429 | // Importantly, this code path avoids subscripting the selection to get an inspection value. |
| 430 | return QVariant(); |
| 431 | } |
| 432 | |
| 433 | QVariant BranchWatchTableModel::ClickRoleData(const QModelIndex& index) const |
| 434 | { |
| 435 | switch (index.column()) |
| 436 | { |
| 437 | case Column::OriginSymbol: |
| 438 | return m_symbol_list[index.row()].origin_addr; |
| 439 | case Column::DestinSymbol: |
| 440 | return m_symbol_list[index.row()].destin_addr; |
| 441 | } |
| 442 | const Core::BranchWatch::Collection::value_type* kv = |
| 443 | m_branch_watch.GetSelection()[index.row()].collection_ptr; |
| 444 | switch (index.column()) |
| 445 | { |
| 446 | case Column::Instruction: |
| 447 | return kv->first.original_inst.hex; |
| 448 | case Column::Origin: |
| 449 | return kv->first.origin_addr; |
| 450 | case Column::Destination: |
| 451 | return kv->first.destin_addr; |
| 452 | } |
| 453 | return QVariant(); |
| 454 | } |
| 455 | |
| 456 | // 0 == false, 1 == true, 2 == unconditional |
| 457 | static int GetConditionInteger(const Core::BranchWatch::Selection::value_type& value, |
| 458 | const Core::BranchWatch::Collection::value_type* kv) |
| 459 | { |
| 460 | if (value.condition == false) |
| 461 | return 0; |
| 462 | if (BranchIsConditional(kv->first.original_inst)) |
| 463 | return 1; |
| 464 | return 2; |
| 465 | } |
| 466 | |
| 467 | QVariant BranchWatchTableModel::SortRoleData(const QModelIndex& index) const |
| 468 | { |
| 469 | switch (index.column()) |
| 470 | { |
| 471 | case Column::OriginSymbol: |
| 472 | return m_symbol_list[index.row()].origin_name; |
| 473 | case Column::DestinSymbol: |
| 474 | return m_symbol_list[index.row()].destin_name; |
| 475 | } |
| 476 | const Core::BranchWatch::Selection::value_type& selection_value = |
| 477 | m_branch_watch.GetSelection()[index.row()]; |
| 478 | const Core::BranchWatch::Collection::value_type* kv = selection_value.collection_ptr; |
| 479 | switch (index.column()) |
| 480 | { |
| 481 | // QVariant's ctor only supports (unsigned) int and (unsigned) long long for some stupid reason. |
| 482 | // std::size_t is unsigned long on some platforms, which results in an ambiguous conversion. |
| 483 | case Column::Instruction: |
| 484 | return GetInstructionMnemonic(kv->first.original_inst.hex); |
| 485 | case Column::Condition: |
| 486 | return GetConditionInteger(selection_value, kv); |
| 487 | case Column::Origin: |
| 488 | return kv->first.origin_addr; |
| 489 | case Column::Destination: |
| 490 | return kv->first.destin_addr; |
| 491 | case Column::RecentHits: |
| 492 | return qulonglong{kv->second.total_hits - kv->second.hits_snapshot}; |
| 493 | case Column::TotalHits: |
| 494 | return qulonglong{kv->second.total_hits}; |
| 495 | } |
| 496 | static_assert(Column::NumberOfColumns == 8); |
| 497 | std::unreachable(); |
| 498 | } |