| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Debugger/JITWidget.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <optional> |
| 8 | #include <ranges> |
| 9 | #include <utility> |
| 10 | |
| 11 | #include <QHBoxLayout> |
| 12 | #include <QHeaderView> |
| 13 | #include <QLineEdit> |
| 14 | #include <QMenu> |
| 15 | #include <QPlainTextEdit> |
| 16 | #include <QPushButton> |
| 17 | #include <QSignalBlocker> |
| 18 | #include <QSortFilterProxyModel> |
| 19 | #include <QSplitter> |
| 20 | #include <QTableView> |
| 21 | #include <QVBoxLayout> |
| 22 | |
| 23 | #include <fmt/ostream.h> |
| 24 | |
| 25 | #include "Common/CommonFuncs.h" |
| 26 | #include "Common/GekkoDisassembler.h" |
| 27 | #include "Core/Core.h" |
| 28 | #include "Core/PowerPC/JitCommon/JitCache.h" |
| 29 | #include "Core/PowerPC/JitInterface.h" |
| 30 | #include "Core/PowerPC/MMU.h" |
| 31 | #include "Core/PowerPC/PPCSymbolDB.h" |
| 32 | #include "Core/System.h" |
| 33 | |
| 34 | #include "DolphinQt/Debugger/JitBlockTableModel.h" |
| 35 | #include "DolphinQt/Host.h" |
| 36 | #include "DolphinQt/QtUtils/ClickableStatusBar.h" |
| 37 | #include "DolphinQt/QtUtils/FromStdString.h" |
| 38 | #include "DolphinQt/QtUtils/ModalMessageBox.h" |
| 39 | #include "DolphinQt/Settings.h" |
| 40 | #include "UICommon/UICommon.h" |
| 41 | |
| 42 | class JitBlockProxyModel final : public QSortFilterProxyModel |
| 43 | { |
| 44 | friend JITWidget; |
| 45 | |
| 46 | public: |
| 47 | explicit JitBlockProxyModel(QObject* parent = nullptr); |
| 48 | ~JitBlockProxyModel() override; |
| 49 | |
| 50 | JitBlockProxyModel(const JitBlockProxyModel&) = delete; |
| 51 | JitBlockProxyModel(JitBlockProxyModel&&) = delete; |
| 52 | JitBlockProxyModel& operator=(const JitBlockProxyModel&) = delete; |
| 53 | JitBlockProxyModel& operator=(JitBlockProxyModel&&) = delete; |
| 54 | |
| 55 | bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override; |
| 56 | [[noreturn]] void setSourceModel(QAbstractItemModel* source_model) override; |
| 57 | void setSourceModel(JitBlockTableModel* source_model); |
| 58 | JitBlockTableModel* sourceModel() const; |
| 59 | |
| 60 | const JitBlock& GetJitBlock(const QModelIndex& index); |
| 61 | |
| 62 | // Always connected slots (external signals) |
| 63 | void OnSymbolTextChanged(const QString& text); |
| 64 | template <std::optional<u32> JitBlockProxyModel::* member> |
| 65 | void OnAddressTextChanged(const QString& text); |
| 66 | |
| 67 | private: |
| 68 | std::optional<u32> m_em_address_min, m_em_address_max, m_pm_address_covered; |
| 69 | QString m_symbol_name = {}; |
| 70 | }; |
| 71 | |
| 72 | const JitBlock& JitBlockProxyModel::GetJitBlock(const QModelIndex& index) |
| 73 | { |
| 74 | return sourceModel()->GetJitBlock(mapToSource(index)); |
| 75 | } |
| 76 | |
| 77 | void JitBlockProxyModel::OnSymbolTextChanged(const QString& text) |
| 78 | { |
| 79 | m_symbol_name = text; |
| 80 | invalidateRowsFilter(); |
| 81 | } |
| 82 | |
| 83 | template <std::optional<u32> JitBlockProxyModel::* member> |
| 84 | void JitBlockProxyModel::OnAddressTextChanged(const QString& text) |
| 85 | { |
| 86 | bool ok = false; |
| 87 | if (const u32 value = text.toUInt(&ok, 16); ok) |
| 88 | this->*member = value; |
| 89 | else |
| 90 | this->*member = std::nullopt; |
| 91 | invalidateRowsFilter(); |
| 92 | } |
| 93 | |
| 94 | bool JitBlockProxyModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const |
| 95 | { |
| 96 | if (source_parent.isValid()) [[unlikely]] |
| 97 | return false; |
| 98 | if (!m_symbol_name.isEmpty()) |
| 99 | { |
| 100 | if (const QVariant& symbol_name_v = *sourceModel()->GetSymbolList()[source_row]; |
| 101 | !symbol_name_v.isValid() || !static_cast<const QString*>(symbol_name_v.data()) |
| 102 | ->contains(m_symbol_name, Qt::CaseInsensitive)) |
| 103 | { |
| 104 | return false; |
| 105 | } |
| 106 | } |
| 107 | const JitBlock& block = sourceModel()->GetJitBlockRefs()[source_row]; |
| 108 | if (m_em_address_min.has_value()) |
| 109 | { |
| 110 | if (block.effectiveAddress < m_em_address_min.value()) |
| 111 | return false; |
| 112 | } |
| 113 | if (m_em_address_max.has_value()) |
| 114 | { |
| 115 | if (block.effectiveAddress > m_em_address_max.value()) |
| 116 | return false; |
| 117 | } |
| 118 | if (m_pm_address_covered.has_value()) |
| 119 | { |
| 120 | if (!block.physical_addresses.contains(m_pm_address_covered.value())) |
| 121 | return false; |
| 122 | } |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | // Virtual setSourceModel is forbidden for type-safety reasons. |
| 127 | void JitBlockProxyModel::setSourceModel(QAbstractItemModel* source_model) |
| 128 | { |
| 129 | Crash(); |
| 130 | } |
| 131 | |
| 132 | void JitBlockProxyModel::setSourceModel(JitBlockTableModel* source_model) |
| 133 | { |
| 134 | QSortFilterProxyModel::setSourceModel(source_model); |
| 135 | } |
| 136 | |
| 137 | JitBlockTableModel* JitBlockProxyModel::sourceModel() const |
| 138 | { |
| 139 | return static_cast<JitBlockTableModel*>(QSortFilterProxyModel::sourceModel()); |
| 140 | } |
| 141 | |
| 142 | JitBlockProxyModel::JitBlockProxyModel(QObject* parent) : QSortFilterProxyModel(parent) |
| 143 | { |
| 144 | } |
| 145 | |
| 146 | JitBlockProxyModel::~JitBlockProxyModel() = default; |
| 147 | |
| 148 | void JITWidget::UpdateProfilingButton() |
| 149 | { |
| 150 | const QSignalBlocker blocker(m_toggle_profiling_button); |
| 151 | const bool enabled = Config::Get(Config::MAIN_DEBUG_JIT_ENABLE_PROFILING); |
| 152 | m_toggle_profiling_button->setText(enabled ? tr("Stop Profiling") : tr("Start Profiling")); |
| 153 | m_toggle_profiling_button->setChecked(enabled); |
| 154 | } |
| 155 | |
| 156 | void JITWidget::UpdateOtherButtons(Core::State state) |
| 157 | { |
| 158 | const bool jit_exists = m_system.GetJitInterface().GetCore() != nullptr; |
| 159 | m_clear_cache_button->setEnabled(jit_exists); |
| 160 | m_wipe_profiling_button->setEnabled(jit_exists); |
| 161 | } |
| 162 | |
| 163 | void JITWidget::UpdateDebugFont(const QFont& font) |
| 164 | { |
| 165 | m_table_view->setFont(font); |
| 166 | m_ppc_asm_widget->setFont(font); |
| 167 | m_host_near_asm_widget->setFont(font); |
| 168 | m_host_far_asm_widget->setFont(font); |
| 169 | } |
| 170 | |
| 171 | void JITWidget::ClearDisassembly() |
| 172 | { |
| 173 | m_ppc_asm_widget->clear(); |
| 174 | m_host_near_asm_widget->clear(); |
| 175 | m_host_far_asm_widget->clear(); |
| 176 | m_status_bar->clearMessage(); |
| 177 | } |
| 178 | |
| 179 | void JITWidget::ShowFreeMemoryStatus() |
| 180 | { |
| 181 | const std::vector memory_stats = m_system.GetJitInterface().GetMemoryStats(); |
| 182 | QString message = tr("Free memory:"); |
| 183 | for (const auto& [name, stats] : memory_stats) |
| 184 | { |
| 185 | const auto& [free_size, fragmentation_ratio] = stats; |
| 186 | // i18n: Of each memory region, %1 is its remaining size displayed in an appropriate scale |
| 187 | // of bytes (e.g. MiB), %2 is its untranslated name, and %3 is its fragmentation percentage. |
| 188 | message.append(tr(" %1 %2 (%3% fragmented)") |
| 189 | .arg(QString::fromStdString(UICommon::FormatSize(free_size, 2))) |
| 190 | .arg(QtUtils::FromStdString(name)) |
| 191 | .arg(fragmentation_ratio * 100.0, 0, 'f', 2)); |
| 192 | } |
| 193 | m_status_bar->showMessage(message); |
| 194 | } |
| 195 | |
| 196 | void JITWidget::UpdateContent(Core::State state) |
| 197 | { |
| 198 | ClearDisassembly(); |
| 199 | if (state == Core::State::Paused) |
| 200 | ShowFreeMemoryStatus(); |
| 201 | } |
| 202 | |
| 203 | static void DisassembleCodeBuffer(const JitBlock& block, PPCSymbolDB& ppc_symbol_db, |
| 204 | std::ostream& stream) |
| 205 | { |
| 206 | // Instructions are 4 byte aligned, so next_address = 1 will never produce a false-negative. |
| 207 | for (u32 next_address = 1; const auto& [address, inst] : block.original_buffer) |
| 208 | { |
| 209 | if (address != next_address) |
| 210 | { |
| 211 | stream << ppc_symbol_db.GetDescription(address) << '\n'; |
| 212 | next_address = address; |
| 213 | } |
| 214 | fmt::print(stream, "0x{:08x}\t{}\n", address, |
| 215 | Common::GekkoDisassembler::Disassemble(inst.hex, address)); |
| 216 | next_address += sizeof(UGeckoInstruction); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | void JITWidget::CrossDisassemble(const JitBlock& block) |
| 221 | { |
| 222 | // TODO C++20: std::ostringstream::view() + QtUtils::FromStdString + std::ostream::seekp(0) would |
| 223 | // save a lot of wasted allocation here, but compiler support for the first thing isn't here yet. |
| 224 | std::ostringstream stream; |
| 225 | DisassembleCodeBuffer(block, m_system.GetPPCSymbolDB(), stream); |
| 226 | m_ppc_asm_widget->setPlainText(QString::fromStdString(std::move(stream).str())); |
| 227 | |
| 228 | auto& jit_interface = m_system.GetJitInterface(); |
| 229 | |
| 230 | const auto host_near_instruction_count = jit_interface.DisassembleNearCode(block, stream); |
| 231 | m_host_near_asm_widget->setPlainText(QString::fromStdString(std::move(stream).str())); |
| 232 | |
| 233 | const auto host_far_instruction_count = jit_interface.DisassembleFarCode(block, stream); |
| 234 | m_host_far_asm_widget->setPlainText(QString::fromStdString(std::move(stream).str())); |
| 235 | |
| 236 | // i18n: "near" and "far" refer to the near code cache and far code cache of Dolphin's JITs. |
| 237 | // %1 and %2 are instruction counts from the near and far code caches, respectively. %3 is a |
| 238 | // percentage calculated from how inefficient (in other words, "blown-up") a given JIT block's |
| 239 | // recompilation was when considering the host instruction count vs the PPC instruction count. |
| 240 | m_status_bar->showMessage(tr("Host instruction count: %1 near %2 far (%3% blowup)") |
| 241 | .arg(host_near_instruction_count) |
| 242 | .arg(host_far_instruction_count) |
| 243 | .arg(static_cast<double>(100 * (host_near_instruction_count + |
| 244 | host_far_instruction_count)) / |
| 245 | block.originalSize - |
| 246 | 100.0, |
| 247 | 0, 'f', 2)); |
| 248 | } |
| 249 | |
| 250 | void JITWidget::CrossDisassemble(const QModelIndex& index) |
| 251 | { |
| 252 | if (index.isValid()) |
| 253 | { |
| 254 | CrossDisassemble(m_table_proxy->GetJitBlock(index)); |
| 255 | return; |
| 256 | } |
| 257 | UpdateContent(Core::GetState(m_system)); |
| 258 | } |
| 259 | |
| 260 | void JITWidget::CrossDisassemble() |
| 261 | { |
| 262 | CrossDisassemble(m_table_view->currentIndex()); |
| 263 | } |
| 264 | |
| 265 | void JITWidget::TableEraseBlocks() |
| 266 | { |
| 267 | auto* const selection_model = m_table_view->selectionModel(); |
| 268 | QModelIndexList index_list = selection_model->selectedRows(); |
| 269 | selection_model->clear(); // Side effect: currentChanged will be emitted (this is intended). |
| 270 | |
| 271 | std::ranges::transform(index_list, index_list.begin(), [this](const QModelIndex& index) { |
| 272 | return m_table_proxy->mapToSource(index); |
| 273 | }); |
| 274 | std::ranges::sort(index_list, std::less{}); // QModelIndex is incompatible with std::ranges::less |
| 275 | for (const QModelIndex& index : std::ranges::reverse_view{index_list}) |
| 276 | { |
| 277 | if (!index.isValid()) |
| 278 | continue; |
| 279 | m_table_model->removeRow(index.row()); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | void JITWidget::LoadQSettings() |
| 284 | { |
| 285 | auto& settings = Settings::GetQSettings(); |
| 286 | |
| 287 | restoreGeometry(settings.value(QStringLiteral("jitwidget/geometry")).toByteArray()); |
| 288 | setHidden(!Settings::Instance().IsJITVisible() || !Settings::Instance().IsDebugModeEnabled()); |
| 289 | // macOS: setFloating() needs to be after setHidden() for proper window presentation |
| 290 | // according to Settings |
| 291 | setFloating(settings.value(QStringLiteral("jitwidget/floating")).toBool()); |
| 292 | m_table_view->horizontalHeader()->restoreState( |
| 293 | settings.value(QStringLiteral("jitwidget/tableheader/state")).toByteArray()); |
| 294 | m_table_splitter->restoreState( |
| 295 | settings.value(QStringLiteral("jitwidget/tablesplitter")).toByteArray()); |
| 296 | m_disasm_splitter->restoreState( |
| 297 | settings.value(QStringLiteral("jitwidget/disasmsplitter")).toByteArray()); |
| 298 | } |
| 299 | |
| 300 | void JITWidget::SaveQSettings() const |
| 301 | { |
| 302 | auto& settings = Settings::GetQSettings(); |
| 303 | |
| 304 | settings.setValue(QStringLiteral("jitwidget/geometry"), saveGeometry()); |
| 305 | settings.setValue(QStringLiteral("jitwidget/floating"), isFloating()); |
| 306 | settings.setValue(QStringLiteral("jitwidget/tableheader/state"), |
| 307 | m_table_view->horizontalHeader()->saveState()); |
| 308 | settings.setValue(QStringLiteral("jitwidget/tablesplitter"), m_table_splitter->saveState()); |
| 309 | settings.setValue(QStringLiteral("jitwidget/disasmsplitter"), m_disasm_splitter->saveState()); |
| 310 | } |
| 311 | |
| 312 | void JITWidget::ConnectSlots() |
| 313 | { |
| 314 | auto* const host = Host::GetInstance(); |
| 315 | connect(host, &Host::JitCacheInvalidation, this, &JITWidget::OnJitCacheInvalidation); |
| 316 | connect(host, &Host::UpdateDisasmDialog, this, &JITWidget::OnUpdateDisasmDialog); |
| 317 | connect(host, &Host::PPCSymbolsChanged, this, &JITWidget::OnPPCSymbolsUpdated); |
| 318 | connect(host, &Host::PPCBreakpointsChanged, this, &JITWidget::OnPPCBreakpointsChanged); |
| 319 | auto* const settings = &Settings::Instance(); |
| 320 | connect(settings, &Settings::ConfigChanged, this, &JITWidget::OnConfigChanged); |
| 321 | connect(settings, &Settings::DebugFontChanged, this, &JITWidget::OnDebugFontChanged); |
| 322 | connect(settings, &Settings::EmulationStateChanged, this, &JITWidget::OnEmulationStateChanged); |
| 323 | } |
| 324 | |
| 325 | void JITWidget::DisconnectSlots() |
| 326 | { |
| 327 | auto* const host = Host::GetInstance(); |
| 328 | disconnect(host, &Host::JitCacheInvalidation, this, &JITWidget::OnJitCacheInvalidation); |
| 329 | disconnect(host, &Host::UpdateDisasmDialog, this, &JITWidget::OnUpdateDisasmDialog); |
| 330 | disconnect(host, &Host::PPCSymbolsChanged, this, &JITWidget::OnPPCSymbolsUpdated); |
| 331 | disconnect(host, &Host::PPCBreakpointsChanged, this, &JITWidget::OnPPCBreakpointsChanged); |
| 332 | auto* const settings = &Settings::Instance(); |
| 333 | disconnect(settings, &Settings::ConfigChanged, this, &JITWidget::OnConfigChanged); |
| 334 | disconnect(settings, &Settings::DebugFontChanged, this, &JITWidget::OnDebugFontChanged); |
| 335 | disconnect(settings, &Settings::EmulationStateChanged, this, &JITWidget::OnEmulationStateChanged); |
| 336 | } |
| 337 | |
| 338 | void JITWidget::Show() |
| 339 | { |
| 340 | ConnectSlots(); |
| 341 | // Handle every slot that may have missed a signal while this widget was hidden. |
| 342 | // OnJitCacheInvalidation() can be skipped. |
| 343 | // OnUpdateDisasmDialog() can be skipped. |
| 344 | // OnPPCSymbolsUpdated() can be skipped. |
| 345 | // OnPPCBreakpointsChanged() can be skipped. |
| 346 | OnConfigChanged(); |
| 347 | OnDebugFontChanged(Settings::Instance().GetDebugFont()); |
| 348 | OnEmulationStateChanged(Core::GetState(m_system)); |
| 349 | } |
| 350 | |
| 351 | void JITWidget::Hide() |
| 352 | { |
| 353 | DisconnectSlots(); |
| 354 | ClearDisassembly(); |
| 355 | } |
| 356 | |
| 357 | void JITWidget::OnRequestPPCComparison(u32 address, bool translate_address) |
| 358 | { |
| 359 | Settings::Instance().SetJITVisible(true); |
| 360 | raise(); |
| 361 | |
| 362 | if (translate_address) |
| 363 | { |
| 364 | const std::optional<u32> pm_address = m_system.GetMMU().GetTranslatedAddress(address); |
| 365 | if (!pm_address.has_value()) |
| 366 | { |
| 367 | ModalMessageBox::warning( |
| 368 | this, tr("Error"), |
| 369 | tr("Effective address %1 has no physical address translation.").arg(address, 0, 16)); |
| 370 | return; |
| 371 | } |
| 372 | address = pm_address.value(); |
| 373 | } |
| 374 | m_pm_address_covered_line_edit->setText(QString::number(address, 16)); |
| 375 | } |
| 376 | |
| 377 | void JITWidget::OnVisibilityToggled(bool visible) |
| 378 | { |
| 379 | setHidden(!visible); |
| 380 | } |
| 381 | |
| 382 | void JITWidget::OnDebugModeToggled(bool enabled) |
| 383 | { |
| 384 | setHidden(!enabled || !Settings::Instance().IsJITVisible()); |
| 385 | } |
| 386 | |
| 387 | void JITWidget::OnToggleProfiling(bool enabled) |
| 388 | { |
| 389 | Config::SetBaseOrCurrent(Config::MAIN_DEBUG_JIT_ENABLE_PROFILING, enabled); |
| 390 | } |
| 391 | |
| 392 | void JITWidget::OnClearCache() |
| 393 | { |
| 394 | m_system.GetJitInterface().ClearCache(Core::CPUThreadGuard{m_system}); |
| 395 | } |
| 396 | |
| 397 | void JITWidget::OnWipeProfiling() |
| 398 | { |
| 399 | m_system.GetJitInterface().WipeBlockProfilingData(Core::CPUThreadGuard{m_system}); |
| 400 | } |
| 401 | |
| 402 | void JITWidget::OnTableCurrentChanged(const QModelIndex& current, const QModelIndex& previous) |
| 403 | { |
| 404 | CrossDisassemble(current); |
| 405 | } |
| 406 | |
| 407 | void JITWidget::OnTableDoubleClicked(const QModelIndex& index) |
| 408 | { |
| 409 | emit SetCodeAddress(m_table_proxy->GetJitBlock(index).effectiveAddress); |
| 410 | } |
| 411 | |
| 412 | void JITWidget::OnTableContextMenu(const QPoint& pos) |
| 413 | { |
| 414 | // There needs to be an option somewhere for a user to recover from hiding every column. |
| 415 | if (m_table_view->horizontalHeader()->hiddenSectionCount() == Column::NumberOfColumns) |
| 416 | { |
| 417 | m_column_visibility_menu->exec(m_table_view->viewport()->mapToGlobal(pos)); |
| 418 | return; |
| 419 | } |
| 420 | m_table_context_menu->exec(m_table_view->viewport()->mapToGlobal(pos)); |
| 421 | } |
| 422 | |
| 423 | void JITWidget::OnTableHeaderContextMenu(const QPoint& pos) |
| 424 | { |
| 425 | m_column_visibility_menu->exec(m_table_view->horizontalHeader()->mapToGlobal(pos)); |
| 426 | } |
| 427 | |
| 428 | void JITWidget::OnTableMenuViewCode() |
| 429 | { |
| 430 | // TODO: CodeWidget doesn't support it yet, but eventually signal if the address should be |
| 431 | // translated with ((block.feature_flags & CPUEmuFeatureFlags::FEATURE_FLAG_MSR_IR) != 0). |
| 432 | if (const QModelIndex& index = m_table_view->currentIndex(); index.isValid()) |
| 433 | emit SetCodeAddress(m_table_proxy->GetJitBlock(index).effectiveAddress); |
| 434 | } |
| 435 | |
| 436 | void JITWidget::OnTableMenuEraseBlocks() |
| 437 | { |
| 438 | TableEraseBlocks(); // Side effect: currentChanged will be emitted (this is intended). |
| 439 | // Because currentChanged has been emitted, OnTableCurrentChanged has already handled the rest. |
| 440 | } |
| 441 | |
| 442 | void JITWidget::OnStatusBarPressed() |
| 443 | { |
| 444 | if (Core::GetState(m_system) == Core::State::Paused) |
| 445 | ShowFreeMemoryStatus(); |
| 446 | } |
| 447 | |
| 448 | void JITWidget::OnJitCacheInvalidation() |
| 449 | { |
| 450 | if (Core::GetState(m_system) != Core::State::Paused) |
| 451 | return; |
| 452 | ClearDisassembly(); |
| 453 | ShowFreeMemoryStatus(); |
| 454 | } |
| 455 | |
| 456 | void JITWidget::OnUpdateDisasmDialog() |
| 457 | { |
| 458 | if (Core::GetState(m_system) != Core::State::Paused) |
| 459 | return; |
| 460 | CrossDisassemble(); |
| 461 | } |
| 462 | |
| 463 | void JITWidget::OnPPCSymbolsUpdated() |
| 464 | { |
| 465 | if (Core::GetState(m_system) != Core::State::Paused) |
| 466 | return; |
| 467 | CrossDisassemble(); |
| 468 | } |
| 469 | |
| 470 | void JITWidget::OnPPCBreakpointsChanged() |
| 471 | { |
| 472 | // Whatever row(s) might have been selected could no longer exist, because adding or removing |
| 473 | // breakpoints can invalidate JIT blocks. We must clear the selection to avoid stale indices. |
| 474 | auto* const selection_model = m_table_view->selectionModel(); |
| 475 | selection_model->clear(); // Side effect: currentChanged will be emitted (this is intended). |
| 476 | // Because currentChanged has been emitted, OnTableCurrentChanged has already handled the rest. |
| 477 | } |
| 478 | |
| 479 | void JITWidget::OnConfigChanged() |
| 480 | { |
| 481 | UpdateProfilingButton(); |
| 482 | } |
| 483 | |
| 484 | void JITWidget::OnDebugFontChanged(const QFont& font) |
| 485 | { |
| 486 | UpdateDebugFont(font); |
| 487 | } |
| 488 | |
| 489 | void JITWidget::OnEmulationStateChanged(Core::State state) |
| 490 | { |
| 491 | UpdateOtherButtons(state); |
| 492 | UpdateContent(state); |
| 493 | } |
| 494 | |
| 495 | void JITWidget::closeEvent(QCloseEvent*) |
| 496 | { |
| 497 | Settings::Instance().SetJITVisible(false); |
| 498 | } |
| 499 | |
| 500 | void JITWidget::showEvent(QShowEvent*) |
| 501 | { |
| 502 | emit ShowSignal(); |
| 503 | Show(); |
| 504 | } |
| 505 | |
| 506 | void JITWidget::hideEvent(QHideEvent*) |
| 507 | { |
| 508 | emit HideSignal(); |
| 509 | Hide(); |
| 510 | } |
| 511 | |
| 512 | JITWidget::JITWidget(Core::System& system, QWidget* parent) : QDockWidget(parent), m_system(system) |
| 513 | { |
| 514 | setWindowTitle(tr("JIT Blocks")); |
| 515 | setObjectName(QStringLiteral("jitwidget")); |
| 516 | setAllowedAreas(Qt::AllDockWidgetAreas); |
| 517 | |
| 518 | auto* const settings = &Settings::Instance(); |
| 519 | connect(settings, &Settings::JITVisibilityChanged, this, &JITWidget::OnVisibilityToggled); |
| 520 | connect(settings, &Settings::DebugModeToggled, this, &JITWidget::OnDebugModeToggled); |
| 521 | |
| 522 | m_table_view = new QTableView(nullptr); |
| 523 | m_table_proxy = new JitBlockProxyModel(m_table_view); |
| 524 | m_table_model = new JitBlockTableModel(m_system, m_system.GetJitInterface(), |
| 525 | m_system.GetPPCSymbolDB(), m_table_proxy); |
| 526 | |
| 527 | connect(this, &JITWidget::HideSignal, m_table_model, &JitBlockTableModel::OnHideSignal); |
| 528 | connect(this, &JITWidget::ShowSignal, m_table_model, &JitBlockTableModel::OnShowSignal); |
| 529 | |
| 530 | m_table_proxy->setSourceModel(m_table_model); |
| 531 | m_table_proxy->setSortRole(UserRole::SortRole); |
| 532 | m_table_proxy->setSortCaseSensitivity(Qt::CaseInsensitive); |
| 533 | |
| 534 | m_table_view->setModel(m_table_proxy); |
| 535 | m_table_view->setSortingEnabled(true); |
| 536 | m_table_view->sortByColumn(Column::EffectiveAddress, Qt::AscendingOrder); |
| 537 | m_table_view->setSelectionMode(QAbstractItemView::ExtendedSelection); |
| 538 | m_table_view->setSelectionBehavior(QAbstractItemView::SelectRows); |
| 539 | m_table_view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
| 540 | m_table_view->setContextMenuPolicy(Qt::CustomContextMenu); |
| 541 | m_table_view->setEditTriggers(QAbstractItemView::NoEditTriggers); |
| 542 | m_table_view->setCornerButtonEnabled(false); |
| 543 | m_table_view->verticalHeader()->hide(); |
| 544 | connect(m_table_view, &QTableView::doubleClicked, this, &JITWidget::OnTableDoubleClicked); |
| 545 | connect(m_table_view, &QTableView::customContextMenuRequested, this, |
| 546 | &JITWidget::OnTableContextMenu); |
| 547 | |
| 548 | auto* const horizontal_header = m_table_view->horizontalHeader(); |
| 549 | horizontal_header->setContextMenuPolicy(Qt::CustomContextMenu); |
| 550 | horizontal_header->setStretchLastSection(true); |
| 551 | horizontal_header->setSectionsMovable(true); |
| 552 | horizontal_header->setFirstSectionMovable(true); |
| 553 | connect(horizontal_header, &QHeaderView::sortIndicatorChanged, m_table_model, |
| 554 | &JitBlockTableModel::OnSortIndicatorChanged); |
| 555 | connect(horizontal_header, &QHeaderView::customContextMenuRequested, this, |
| 556 | &JITWidget::OnTableHeaderContextMenu); |
| 557 | |
| 558 | auto* const selection_model = m_table_view->selectionModel(); |
| 559 | connect(selection_model, &QItemSelectionModel::currentChanged, this, |
| 560 | &JITWidget::OnTableCurrentChanged); |
| 561 | |
| 562 | auto* const controls_layout = new QHBoxLayout(nullptr); |
| 563 | const auto address_filter_routine = [&](QLineEdit* line_edit, const QString& placeholder_text, |
| 564 | void (JitBlockProxyModel::*slot)(const QString&)) { |
| 565 | line_edit->setPlaceholderText(placeholder_text); |
| 566 | connect(line_edit, &QLineEdit::textChanged, m_table_proxy, slot); |
| 567 | controls_layout->addWidget(line_edit); |
| 568 | }; |
| 569 | address_filter_routine( |
| 570 | new QLineEdit(nullptr), tr("Min Effective Address"), |
| 571 | &JitBlockProxyModel::OnAddressTextChanged<&JitBlockProxyModel::m_em_address_min>); |
| 572 | address_filter_routine( |
| 573 | new QLineEdit(nullptr), tr("Max Effective Address"), |
| 574 | &JitBlockProxyModel::OnAddressTextChanged<&JitBlockProxyModel::m_em_address_max>); |
| 575 | address_filter_routine( |
| 576 | m_pm_address_covered_line_edit = new QLineEdit(nullptr), tr("Recompiles Physical Address"), |
| 577 | &JitBlockProxyModel::OnAddressTextChanged<&JitBlockProxyModel::m_pm_address_covered>); |
| 578 | |
| 579 | auto* const symbol_name_line_edit = new QLineEdit(nullptr); |
| 580 | symbol_name_line_edit->setPlaceholderText(tr("Symbol Name")); |
| 581 | connect(symbol_name_line_edit, &QLineEdit::textChanged, m_table_model, |
| 582 | &JitBlockTableModel::OnFilterSymbolTextChanged); |
| 583 | connect(symbol_name_line_edit, &QLineEdit::textChanged, m_table_proxy, |
| 584 | &JitBlockProxyModel::OnSymbolTextChanged); |
| 585 | controls_layout->addWidget(symbol_name_line_edit); |
| 586 | |
| 587 | m_toggle_profiling_button = new QPushButton(nullptr); |
| 588 | m_toggle_profiling_button->setToolTip( |
| 589 | tr("Toggle software JIT block profiling (will clear the JIT cache).")); |
| 590 | m_toggle_profiling_button->setCheckable(true); |
| 591 | connect(m_toggle_profiling_button, &QPushButton::toggled, this, &JITWidget::OnToggleProfiling); |
| 592 | controls_layout->addWidget(m_toggle_profiling_button); |
| 593 | |
| 594 | m_clear_cache_button = new QPushButton(tr("Clear Cache"), nullptr); |
| 595 | connect(m_clear_cache_button, &QPushButton::clicked, this, &JITWidget::OnClearCache); |
| 596 | controls_layout->addWidget(m_clear_cache_button); |
| 597 | |
| 598 | m_wipe_profiling_button = new QPushButton(tr("Wipe Profiling"), nullptr); |
| 599 | m_wipe_profiling_button->setToolTip(tr("Re-initialize software JIT block profiling data.")); |
| 600 | connect(m_wipe_profiling_button, &QPushButton::clicked, this, &JITWidget::OnWipeProfiling); |
| 601 | controls_layout->addWidget(m_wipe_profiling_button); |
| 602 | |
| 603 | m_disasm_splitter = new QSplitter(Qt::Horizontal, nullptr); |
| 604 | const auto text_box_routine = [&](QPlainTextEdit* text_edit, const QString& placeholder_text) { |
| 605 | text_edit->setWordWrapMode(QTextOption::NoWrap); |
| 606 | text_edit->setPlaceholderText(placeholder_text); |
| 607 | text_edit->setReadOnly(true); |
| 608 | m_disasm_splitter->addWidget(text_edit); |
| 609 | }; |
| 610 | text_box_routine(m_ppc_asm_widget = new QPlainTextEdit(nullptr), tr("PPC Instruction Coverage")); |
| 611 | text_box_routine(m_host_near_asm_widget = new QPlainTextEdit(nullptr), |
| 612 | tr("Host Near Code Cache")); |
| 613 | text_box_routine(m_host_far_asm_widget = new QPlainTextEdit(nullptr), tr("Host Far Code Cache")); |
| 614 | |
| 615 | m_table_splitter = new QSplitter(Qt::Vertical, nullptr); |
| 616 | m_table_splitter->addWidget(m_table_view); |
| 617 | m_table_splitter->addWidget(m_disasm_splitter); |
| 618 | |
| 619 | m_status_bar = new ClickableStatusBar(nullptr); |
| 620 | m_status_bar->setSizeGripEnabled(false); |
| 621 | connect(m_status_bar, &ClickableStatusBar::pressed, this, &JITWidget::OnStatusBarPressed); |
| 622 | |
| 623 | m_table_context_menu = new QMenu(this); |
| 624 | m_table_context_menu->addAction(tr("View &Code"), this, &JITWidget::OnTableMenuViewCode); |
| 625 | m_table_context_menu->addAction(tr("&Erase Block(s)"), this, &JITWidget::OnTableMenuEraseBlocks); |
| 626 | |
| 627 | LoadQSettings(); |
| 628 | |
| 629 | m_column_visibility_menu = new QMenu(this); |
| 630 | // These table header display names have abbreviated counterparts in JitBlockTableModel.cpp |
| 631 | static constexpr std::array<const char*, Column::NumberOfColumns> headers = { |
| 632 | QT_TR_NOOP("PPC Feature Flags"), |
| 633 | // i18n: "Effective" means this memory address might be translated within the MMU. |
| 634 | QT_TR_NOOP("Effective Address"), |
| 635 | QT_TR_NOOP("Code Buffer Size"), |
| 636 | // i18n: This means to say it is a count of PPC instructions recompiled more than once. |
| 637 | QT_TR_NOOP("Repeat Instructions"), |
| 638 | // i18n: "Near Code" refers to the near code cache of Dolphin's JITs. |
| 639 | QT_TR_NOOP("Host Near Code Size"), |
| 640 | // i18n: "Far Code" refers to the far code cache of Dolphin's JITs. |
| 641 | QT_TR_NOOP("Host Far Code Size"), |
| 642 | QT_TR_NOOP("Run Count"), |
| 643 | // i18n: "Cycles" means instruction cycles. |
| 644 | QT_TR_NOOP("Cycles Spent"), |
| 645 | // i18n: "Cycles" means instruction cycles. |
| 646 | QT_TR_NOOP("Cycles Average"), |
| 647 | // i18n: "Cycles" means instruction cycles. |
| 648 | QT_TR_NOOP("Cycles Percent"), |
| 649 | // i18n: "ns" is an abbreviation of nanoseconds. |
| 650 | QT_TR_NOOP("Time Spent (ns)"), |
| 651 | // i18n: "ns" is an abbreviation of nanoseconds. |
| 652 | QT_TR_NOOP("Time Average (ns)"), |
| 653 | QT_TR_NOOP("Time Percent"), |
| 654 | // i18n: "Symbol" means debugging symbol (its name in particular). |
| 655 | QT_TR_NOOP("Symbol"), |
| 656 | }; |
| 657 | for (int column = 0; column < Column::NumberOfColumns; ++column) |
| 658 | { |
| 659 | auto* const action = |
| 660 | m_column_visibility_menu->addAction(tr(headers[column]), [this, column](bool enabled) { |
| 661 | m_table_view->setColumnHidden(column, !enabled); |
| 662 | }); |
| 663 | action->setChecked(!m_table_view->isColumnHidden(column)); |
| 664 | action->setCheckable(true); |
| 665 | } |
| 666 | |
| 667 | auto* const main_layout = new QVBoxLayout(nullptr); |
| 668 | main_layout->setContentsMargins(2, 2, 2, 2); |
| 669 | main_layout->setSpacing(0); |
| 670 | main_layout->addLayout(controls_layout); |
| 671 | main_layout->addWidget(m_table_splitter); |
| 672 | main_layout->addWidget(m_status_bar); |
| 673 | |
| 674 | auto* const main_widget = new QWidget(nullptr); |
| 675 | main_widget->setLayout(main_layout); |
| 676 | setWidget(main_widget); |
| 677 | } |
| 678 | |
| 679 | JITWidget::~JITWidget() |
| 680 | { |
| 681 | SaveQSettings(); |
| 682 | } |