| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Debugger/CodeWidget.h" |
| 5 | |
| 6 | #include <chrono> |
| 7 | |
| 8 | #include <fmt/format.h> |
| 9 | |
| 10 | #include <QGridLayout> |
| 11 | #include <QGroupBox> |
| 12 | #include <QGuiApplication> |
| 13 | #include <QLabel> |
| 14 | #include <QLineEdit> |
| 15 | #include <QListWidget> |
| 16 | #include <QPushButton> |
| 17 | #include <QSplitter> |
| 18 | #include <QStyleHints> |
| 19 | #include <QTabWidget> |
| 20 | #include <QTableWidget> |
| 21 | #include <QToolButton> |
| 22 | #include <QVBoxLayout> |
| 23 | #include <QWidget> |
| 24 | |
| 25 | #include "Common/Event.h" |
| 26 | #include "Core/Core.h" |
| 27 | #include "Core/Debugger/Debugger_SymbolMap.h" |
| 28 | #include "Core/HW/CPU.h" |
| 29 | #include "Core/PowerPC/MMU.h" |
| 30 | #include "Core/PowerPC/PPCSymbolDB.h" |
| 31 | #include "Core/PowerPC/PowerPC.h" |
| 32 | #include "Core/System.h" |
| 33 | #include "DolphinQt/Debugger/BranchWatchDialog.h" |
| 34 | #include "DolphinQt/Host.h" |
| 35 | #include "DolphinQt/Resources.h" |
| 36 | #include "DolphinQt/Settings.h" |
| 37 | |
| 38 | static const QString BOX_SPLITTER_STYLESHEET = QStringLiteral( |
| 39 | "QSplitter::handle { border-top: 1px dashed black; width: 1px; margin-left: 10px; " |
| 40 | "margin-right: 10px; }"); |
| 41 | |
| 42 | CodeWidget::CodeWidget(QWidget* parent) |
| 43 | : QDockWidget(parent), m_system(Core::System::GetInstance()), |
| 44 | m_ppc_symbol_db(m_system.GetPPCSymbolDB()) |
| 45 | { |
| 46 | setWindowTitle(tr("Code")); |
| 47 | setObjectName(QStringLiteral("code")); |
| 48 | |
| 49 | setHidden(!Settings::Instance().IsCodeVisible() || !Settings::Instance().IsDebugModeEnabled()); |
| 50 | |
| 51 | setAllowedAreas(Qt::AllDockWidgetAreas); |
| 52 | |
| 53 | CreateWidgets(); |
| 54 | |
| 55 | auto& settings = Settings::GetQSettings(); |
| 56 | |
| 57 | restoreGeometry(settings.value(QStringLiteral("codewidget/geometry")).toByteArray()); |
| 58 | // macOS: setHidden() needs to be evaluated before setFloating() for proper window presentation |
| 59 | // according to Settings |
| 60 | setFloating(settings.value(QStringLiteral("codewidget/floating")).toBool()); |
| 61 | |
| 62 | connect(&Settings::Instance(), &Settings::CodeVisibilityChanged, this, |
| 63 | [this](bool visible) { setHidden(!visible); }); |
| 64 | |
| 65 | connect(Host::GetInstance(), &Host::UpdateDisasmDialog, this, [this] { |
| 66 | if (!m_lock_btn->isChecked() && Core::GetState(m_system) == Core::State::Paused) |
| 67 | SetAddress(m_system.GetPPCState().pc, CodeViewWidget::SetAddressUpdate::WithoutUpdate); |
| 68 | Update(); |
| 69 | }); |
| 70 | |
| 71 | connect(&Settings::Instance(), &Settings::DebugModeToggled, this, |
| 72 | [this](bool enabled) { setHidden(!enabled || !Settings::Instance().IsCodeVisible()); }); |
| 73 | |
| 74 | connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, &CodeWidget::Update); |
| 75 | |
| 76 | ConnectWidgets(); |
| 77 | |
| 78 | m_code_splitter->restoreState( |
| 79 | settings.value(QStringLiteral("codewidget/codesplitter")).toByteArray()); |
| 80 | m_box_splitter->restoreState( |
| 81 | settings.value(QStringLiteral("codewidget/boxsplitter")).toByteArray()); |
| 82 | } |
| 83 | |
| 84 | CodeWidget::~CodeWidget() |
| 85 | { |
| 86 | auto& settings = Settings::GetQSettings(); |
| 87 | |
| 88 | settings.setValue(QStringLiteral("codewidget/geometry"), saveGeometry()); |
| 89 | settings.setValue(QStringLiteral("codewidget/floating"), isFloating()); |
| 90 | settings.setValue(QStringLiteral("codewidget/codesplitter"), m_code_splitter->saveState()); |
| 91 | settings.setValue(QStringLiteral("codewidget/boxsplitter"), m_box_splitter->saveState()); |
| 92 | } |
| 93 | |
| 94 | void CodeWidget::closeEvent(QCloseEvent*) |
| 95 | { |
| 96 | Settings::Instance().SetCodeVisible(false); |
| 97 | } |
| 98 | |
| 99 | void CodeWidget::showEvent(QShowEvent* event) |
| 100 | { |
| 101 | Update(); |
| 102 | } |
| 103 | |
| 104 | void CodeWidget::CreateWidgets() |
| 105 | { |
| 106 | auto* layout = new QHBoxLayout; |
| 107 | |
| 108 | layout->setContentsMargins(2, 2, 2, 2); |
| 109 | layout->setSpacing(0); |
| 110 | |
| 111 | auto* top_layout = new QHBoxLayout; |
| 112 | m_search_address = new QLineEdit; |
| 113 | m_search_address->setPlaceholderText(tr("Search Address")); |
| 114 | |
| 115 | m_lock_btn = new QToolButton(); |
| 116 | m_lock_btn->setIcon(Resources::GetThemeIcon("pause")); |
| 117 | m_lock_btn->setCheckable(true); |
| 118 | m_lock_btn->setMinimumSize(24, 24); |
| 119 | m_lock_btn->setToolTip(tr("When enabled, prevents automatic updates to the code view.")); |
| 120 | m_branch_watch = new QPushButton(tr("Branch Watch")); |
| 121 | |
| 122 | top_layout->addWidget(m_search_address); |
| 123 | top_layout->addWidget(m_lock_btn); |
| 124 | top_layout->addWidget(m_branch_watch); |
| 125 | |
| 126 | auto* right_layout = new QVBoxLayout; |
| 127 | m_code_view = new CodeViewWidget; |
| 128 | right_layout->addLayout(top_layout); |
| 129 | right_layout->addWidget(m_code_view); |
| 130 | |
| 131 | m_box_splitter = new QSplitter(Qt::Vertical); |
| 132 | m_box_splitter->setStyleSheet(BOX_SPLITTER_STYLESHEET); |
| 133 | |
| 134 | auto add_search_line_edit = [this](const QString& name, QWidget* list_widget) { |
| 135 | auto* widget = new QWidget; |
| 136 | auto* line_layout = new QGridLayout; |
| 137 | auto* label = new QLabel(name); |
| 138 | auto* search_line_edit = new QLineEdit; |
| 139 | |
| 140 | widget->setLayout(line_layout); |
| 141 | line_layout->addWidget(label, 0, 0); |
| 142 | line_layout->addWidget(search_line_edit, 0, 1); |
| 143 | line_layout->addWidget(list_widget, 1, 0, -1, -1); |
| 144 | m_box_splitter->addWidget(widget); |
| 145 | return search_line_edit; |
| 146 | }; |
| 147 | |
| 148 | // Callstack |
| 149 | m_callstack_list = new QListWidget; |
| 150 | m_search_callstack = add_search_line_edit(tr("Callstack"), m_callstack_list); |
| 151 | |
| 152 | // Symbols |
| 153 | auto* symbols_tab = new QTabWidget; |
| 154 | m_symbols_list = new QListWidget; |
| 155 | m_note_list = new QListWidget; |
| 156 | symbols_tab->addTab(m_symbols_list, tr("Symbols")); |
| 157 | symbols_tab->addTab(m_note_list, tr("Notes")); |
| 158 | m_search_symbols = add_search_line_edit(tr("Symbols"), symbols_tab); |
| 159 | |
| 160 | // Function calls |
| 161 | m_function_calls_list = new QListWidget; |
| 162 | m_search_calls = add_search_line_edit(tr("Calls"), m_function_calls_list); |
| 163 | |
| 164 | // Function callers |
| 165 | m_function_callers_list = new QListWidget; |
| 166 | m_search_callers = add_search_line_edit(tr("Callers"), m_function_callers_list); |
| 167 | |
| 168 | m_code_splitter = new QSplitter(Qt::Horizontal); |
| 169 | |
| 170 | // right_layout is the searchbar area and the codeview. |
| 171 | QWidget* right_widget = new QWidget; |
| 172 | right_widget->setLayout(right_layout); |
| 173 | |
| 174 | m_code_splitter->addWidget(m_box_splitter); |
| 175 | m_code_splitter->addWidget(right_widget); |
| 176 | |
| 177 | layout->addWidget(m_code_splitter); |
| 178 | |
| 179 | // Corrects button height mis-aligning the layout. Note: Margin only populates values after this |
| 180 | // point. |
| 181 | const int height_fix = |
| 182 | m_branch_watch->sizeHint().height() - m_search_address->sizeHint().height(); |
| 183 | auto margins = right_layout->contentsMargins(); |
| 184 | margins.setTop(margins.top() - height_fix / 2); |
| 185 | right_layout->setContentsMargins(margins); |
| 186 | right_layout->setSpacing(right_layout->spacing() - height_fix / 2); |
| 187 | |
| 188 | QWidget* widget = new QWidget(this); |
| 189 | widget->setLayout(layout); |
| 190 | setWidget(widget); |
| 191 | } |
| 192 | |
| 193 | void CodeWidget::ConnectWidgets() |
| 194 | { |
| 195 | #if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0) |
| 196 | connect(QGuiApplication::styleHints(), &QStyleHints::colorSchemeChanged, this, |
| 197 | [this](Qt::ColorScheme colorScheme) { |
| 198 | m_box_splitter->setStyleSheet(BOX_SPLITTER_STYLESHEET); |
| 199 | }); |
| 200 | #endif |
| 201 | |
| 202 | connect(m_search_address, &QLineEdit::textChanged, this, &CodeWidget::OnSearchAddress); |
| 203 | connect(m_search_address, &QLineEdit::returnPressed, this, &CodeWidget::OnSearchAddress); |
| 204 | connect(m_lock_btn, &QPushButton::toggled, m_code_view, &CodeViewWidget::OnLockAddress); |
| 205 | connect(m_search_symbols, &QLineEdit::textChanged, this, &CodeWidget::OnSearchSymbols); |
| 206 | connect(m_search_calls, &QLineEdit::textChanged, this, [this] { |
| 207 | if (const Common::Symbol* symbol = m_ppc_symbol_db.GetSymbolFromAddr(m_code_view->GetAddress())) |
| 208 | UpdateFunctionCalls(symbol); |
| 209 | }); |
| 210 | connect(m_search_callers, &QLineEdit::textChanged, this, [this] { |
| 211 | if (const Common::Symbol* symbol = m_ppc_symbol_db.GetSymbolFromAddr(m_code_view->GetAddress())) |
| 212 | UpdateFunctionCallers(symbol); |
| 213 | }); |
| 214 | connect(m_search_callstack, &QLineEdit::textChanged, this, &CodeWidget::UpdateCallstack); |
| 215 | |
| 216 | connect(m_branch_watch, &QPushButton::clicked, this, &CodeWidget::OnBranchWatchDialog); |
| 217 | connect(m_note_list, &QListWidget::itemPressed, this, &CodeWidget::OnSelectNote); |
| 218 | connect(m_symbols_list, &QListWidget::itemPressed, this, &CodeWidget::OnSelectSymbol); |
| 219 | connect(m_callstack_list, &QListWidget::itemPressed, this, &CodeWidget::OnSelectCallstack); |
| 220 | connect(m_function_calls_list, &QListWidget::itemPressed, this, |
| 221 | &CodeWidget::OnSelectFunctionCalls); |
| 222 | connect(m_function_callers_list, &QListWidget::itemPressed, this, |
| 223 | &CodeWidget::OnSelectFunctionCallers); |
| 224 | |
| 225 | connect(Host::GetInstance(), &Host::PPCSymbolsChanged, this, &CodeWidget::OnPPCSymbolsChanged); |
| 226 | connect(m_code_view, &CodeViewWidget::UpdateCodeWidget, this, &CodeWidget::Update); |
| 227 | |
| 228 | connect(m_code_view, &CodeViewWidget::RequestPPCComparison, this, |
| 229 | &CodeWidget::RequestPPCComparison); |
| 230 | connect(m_code_view, &CodeViewWidget::ShowMemory, this, &CodeWidget::ShowMemory); |
| 231 | connect(m_code_view, &CodeViewWidget::ActivateSearch, this, &CodeWidget::ActivateSearchAddress); |
| 232 | } |
| 233 | |
| 234 | void CodeWidget::OnBranchWatchDialog() |
| 235 | { |
| 236 | if (m_branch_watch_dialog == nullptr) |
| 237 | { |
| 238 | m_branch_watch_dialog = new BranchWatchDialog(m_system, m_system.GetPowerPC().GetBranchWatch(), |
| 239 | m_ppc_symbol_db, this, this); |
| 240 | } |
| 241 | m_branch_watch_dialog->show(); |
| 242 | m_branch_watch_dialog->raise(); |
| 243 | m_branch_watch_dialog->activateWindow(); |
| 244 | } |
| 245 | |
| 246 | void CodeWidget::OnSetCodeAddress(u32 address) |
| 247 | { |
| 248 | SetAddress(address, CodeViewWidget::SetAddressUpdate::WithDetailedUpdate); |
| 249 | } |
| 250 | |
| 251 | void CodeWidget::OnPPCSymbolsChanged() |
| 252 | { |
| 253 | UpdateSymbols(); |
| 254 | UpdateNotes(); |
| 255 | UpdateCallstack(); |
| 256 | |
| 257 | const Common::Symbol* symbol = m_ppc_symbol_db.GetSymbolFromAddr(m_code_view->GetAddress()); |
| 258 | UpdateFunctionCalls(symbol); |
| 259 | UpdateFunctionCallers(symbol); |
| 260 | } |
| 261 | |
| 262 | void CodeWidget::ActivateSearchAddress() |
| 263 | { |
| 264 | m_search_address->setFocus(); |
| 265 | m_search_address->selectAll(); |
| 266 | } |
| 267 | |
| 268 | void CodeWidget::OnSearchAddress() |
| 269 | { |
| 270 | bool good = true; |
| 271 | u32 address = m_search_address->text().toUInt(&good, 16); |
| 272 | |
| 273 | QPalette palette; |
| 274 | QFont font; |
| 275 | |
| 276 | if (!good && !m_search_address->text().isEmpty()) |
| 277 | { |
| 278 | font.setBold(true); |
| 279 | palette.setColor(QPalette::Text, Qt::red); |
| 280 | } |
| 281 | |
| 282 | m_search_address->setPalette(palette); |
| 283 | m_search_address->setFont(font); |
| 284 | |
| 285 | if (good) |
| 286 | m_code_view->SetAddress(address, CodeViewWidget::SetAddressUpdate::WithUpdate); |
| 287 | |
| 288 | Update(); |
| 289 | |
| 290 | m_search_address->setFocus(); |
| 291 | } |
| 292 | |
| 293 | void CodeWidget::OnSearchSymbols() |
| 294 | { |
| 295 | m_symbol_filter = m_search_symbols->text(); |
| 296 | UpdateSymbols(); |
| 297 | UpdateNotes(); |
| 298 | } |
| 299 | |
| 300 | void CodeWidget::OnSelectSymbol() |
| 301 | { |
| 302 | const auto items = m_symbols_list->selectedItems(); |
| 303 | if (items.isEmpty()) |
| 304 | return; |
| 305 | |
| 306 | const u32 address = items[0]->data(Qt::UserRole).toUInt(); |
| 307 | const Common::Symbol* const symbol = m_ppc_symbol_db.GetSymbolFromAddr(address); |
| 308 | |
| 309 | m_code_view->SetAddress(address, CodeViewWidget::SetAddressUpdate::WithUpdate); |
| 310 | UpdateCallstack(); |
| 311 | UpdateFunctionCalls(symbol); |
| 312 | UpdateFunctionCallers(symbol); |
| 313 | |
| 314 | m_code_view->setFocus(); |
| 315 | } |
| 316 | |
| 317 | void CodeWidget::OnSelectNote() |
| 318 | { |
| 319 | const auto items = m_note_list->selectedItems(); |
| 320 | if (items.isEmpty()) |
| 321 | return; |
| 322 | |
| 323 | const u32 address = items[0]->data(Qt::UserRole).toUInt(); |
| 324 | |
| 325 | m_code_view->SetAddress(address, CodeViewWidget::SetAddressUpdate::WithUpdate); |
| 326 | } |
| 327 | |
| 328 | void CodeWidget::OnSelectCallstack() |
| 329 | { |
| 330 | const auto items = m_callstack_list->selectedItems(); |
| 331 | if (items.isEmpty()) |
| 332 | return; |
| 333 | |
| 334 | m_code_view->SetAddress(items[0]->data(Qt::UserRole).toUInt(), |
| 335 | CodeViewWidget::SetAddressUpdate::WithUpdate); |
| 336 | Update(); |
| 337 | } |
| 338 | |
| 339 | void CodeWidget::OnSelectFunctionCalls() |
| 340 | { |
| 341 | const auto items = m_function_calls_list->selectedItems(); |
| 342 | if (items.isEmpty()) |
| 343 | return; |
| 344 | |
| 345 | m_code_view->SetAddress(items[0]->data(Qt::UserRole).toUInt(), |
| 346 | CodeViewWidget::SetAddressUpdate::WithUpdate); |
| 347 | Update(); |
| 348 | } |
| 349 | |
| 350 | void CodeWidget::OnSelectFunctionCallers() |
| 351 | { |
| 352 | const auto items = m_function_callers_list->selectedItems(); |
| 353 | if (items.isEmpty()) |
| 354 | return; |
| 355 | |
| 356 | m_code_view->SetAddress(items[0]->data(Qt::UserRole).toUInt(), |
| 357 | CodeViewWidget::SetAddressUpdate::WithUpdate); |
| 358 | Update(); |
| 359 | } |
| 360 | |
| 361 | void CodeWidget::SetAddress(u32 address, CodeViewWidget::SetAddressUpdate update) |
| 362 | { |
| 363 | m_code_view->SetAddress(address, update); |
| 364 | |
| 365 | if (update == CodeViewWidget::SetAddressUpdate::WithUpdate || |
| 366 | update == CodeViewWidget::SetAddressUpdate::WithDetailedUpdate) |
| 367 | { |
| 368 | Settings::Instance().SetCodeVisible(true); |
| 369 | raise(); |
| 370 | m_code_view->setFocus(); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | void CodeWidget::Update() |
| 375 | { |
| 376 | if (!isVisible()) |
| 377 | return; |
| 378 | |
| 379 | const Common::Symbol* const symbol = m_ppc_symbol_db.GetSymbolFromAddr(m_code_view->GetAddress()); |
| 380 | |
| 381 | UpdateCallstack(); |
| 382 | |
| 383 | m_code_view->Update(); |
| 384 | m_code_view->setFocus(); |
| 385 | |
| 386 | UpdateFunctionCalls(symbol); |
| 387 | UpdateFunctionCallers(symbol); |
| 388 | } |
| 389 | |
| 390 | void CodeWidget::UpdateCallstack() |
| 391 | { |
| 392 | m_callstack_list->clear(); |
| 393 | |
| 394 | if (Core::GetState(m_system) != Core::State::Paused) |
| 395 | return; |
| 396 | |
| 397 | std::vector<Dolphin_Debugger::CallstackEntry> stack; |
| 398 | if (!Dolphin_Debugger::GetCallstack(Core::CPUThreadGuard{m_system}, stack)) |
| 399 | { |
| 400 | m_callstack_list->addItem(tr("Invalid callstack")); |
| 401 | return; |
| 402 | } |
| 403 | |
| 404 | const QString filter = m_search_callstack->text(); |
| 405 | |
| 406 | for (const auto& frame : stack) |
| 407 | { |
| 408 | const QString name = QString::fromStdString(frame.Name.substr(0, frame.Name.length() - 1)); |
| 409 | |
| 410 | if (!name.contains(filter, Qt::CaseInsensitive)) |
| 411 | continue; |
| 412 | |
| 413 | auto* item = new QListWidgetItem(name); |
| 414 | item->setData(Qt::UserRole, frame.vAddress); |
| 415 | m_callstack_list->addItem(item); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | void CodeWidget::UpdateSymbols() |
| 420 | { |
| 421 | const QString selection = m_symbols_list->selectedItems().isEmpty() ? |
| 422 | QString{} : |
| 423 | m_symbols_list->selectedItems()[0]->text(); |
| 424 | m_symbols_list->clear(); |
| 425 | |
| 426 | m_ppc_symbol_db.ForEachSymbol([&](const Common::Symbol& symbol) { |
| 427 | QString name = QString::fromStdString(symbol.name); |
| 428 | |
| 429 | // If the symbol has an object name, add it to the entry name. |
| 430 | if (!symbol.object_name.empty()) |
| 431 | { |
| 432 | name += QString::fromStdString(fmt::format(" ({})", symbol.object_name)); |
| 433 | } |
| 434 | |
| 435 | auto* item = new QListWidgetItem(name); |
| 436 | if (name == selection) |
| 437 | item->setSelected(true); |
| 438 | |
| 439 | // Disable non-function symbols as you can't do anything with them. |
| 440 | if (symbol.type != Common::Symbol::Type::Function) |
| 441 | item->setFlags(Qt::NoItemFlags); |
| 442 | |
| 443 | item->setData(Qt::UserRole, symbol.address); |
| 444 | |
| 445 | if (name.contains(m_symbol_filter, Qt::CaseInsensitive)) |
| 446 | m_symbols_list->addItem(item); |
| 447 | }); |
| 448 | |
| 449 | m_symbols_list->sortItems(); |
| 450 | } |
| 451 | |
| 452 | void CodeWidget::UpdateNotes() |
| 453 | { |
| 454 | const QString selection = m_note_list->selectedItems().isEmpty() ? |
| 455 | QStringLiteral("") : |
| 456 | m_note_list->selectedItems()[0]->text(); |
| 457 | m_note_list->clear(); |
| 458 | |
| 459 | m_ppc_symbol_db.ForEachNote([&](const Common::Note& note) { |
| 460 | const QString name = QString::fromStdString(note.name); |
| 461 | |
| 462 | auto* item = new QListWidgetItem(name); |
| 463 | if (name == selection) |
| 464 | item->setSelected(true); |
| 465 | |
| 466 | item->setData(Qt::UserRole, note.address); |
| 467 | |
| 468 | if (name.toUpper().indexOf(m_symbol_filter.toUpper()) != -1) |
| 469 | m_note_list->addItem(item); |
| 470 | }); |
| 471 | |
| 472 | m_note_list->sortItems(); |
| 473 | } |
| 474 | |
| 475 | void CodeWidget::UpdateFunctionCalls(const Common::Symbol* symbol) |
| 476 | { |
| 477 | m_function_calls_list->clear(); |
| 478 | if (symbol == nullptr) |
| 479 | return; |
| 480 | |
| 481 | const QString filter = m_search_calls->text(); |
| 482 | |
| 483 | for (const auto& call : symbol->calls) |
| 484 | { |
| 485 | const u32 addr = call.function; |
| 486 | const Common::Symbol* const call_symbol = m_ppc_symbol_db.GetSymbolFromAddr(addr); |
| 487 | |
| 488 | if (call_symbol) |
| 489 | { |
| 490 | QString name; |
| 491 | |
| 492 | if (!call_symbol->object_name.empty()) |
| 493 | { |
| 494 | name = QString::fromStdString( |
| 495 | fmt::format("< {} ({}, {:08x})", call_symbol->name, call_symbol->object_name, addr)); |
| 496 | } |
| 497 | else |
| 498 | { |
| 499 | name = QString::fromStdString(fmt::format("< {} ({:08x})", call_symbol->name, addr)); |
| 500 | } |
| 501 | |
| 502 | if (!name.contains(filter, Qt::CaseInsensitive)) |
| 503 | continue; |
| 504 | |
| 505 | auto* item = new QListWidgetItem(name); |
| 506 | item->setData(Qt::UserRole, addr); |
| 507 | m_function_calls_list->addItem(item); |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | void CodeWidget::UpdateFunctionCallers(const Common::Symbol* symbol) |
| 513 | { |
| 514 | m_function_callers_list->clear(); |
| 515 | if (symbol == nullptr) |
| 516 | return; |
| 517 | |
| 518 | const QString filter = m_search_callers->text(); |
| 519 | |
| 520 | for (const auto& caller : symbol->callers) |
| 521 | { |
| 522 | const u32 addr = caller.call_address; |
| 523 | const Common::Symbol* const caller_symbol = m_ppc_symbol_db.GetSymbolFromAddr(addr); |
| 524 | |
| 525 | if (caller_symbol) |
| 526 | { |
| 527 | QString name; |
| 528 | |
| 529 | if (!caller_symbol->object_name.empty()) |
| 530 | { |
| 531 | name = QString::fromStdString(fmt::format("< {} ({}, {:08x})", caller_symbol->name, |
| 532 | caller_symbol->object_name, addr)); |
| 533 | } |
| 534 | else |
| 535 | { |
| 536 | name = QString::fromStdString(fmt::format("< {} ({:08x})", caller_symbol->name, addr)); |
| 537 | } |
| 538 | |
| 539 | if (!name.contains(filter, Qt::CaseInsensitive)) |
| 540 | continue; |
| 541 | |
| 542 | auto* item = new QListWidgetItem(name); |
| 543 | item->setData(Qt::UserRole, addr); |
| 544 | m_function_callers_list->addItem(item); |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | void CodeWidget::Step() |
| 550 | { |
| 551 | auto& cpu = m_system.GetCPU(); |
| 552 | |
| 553 | if (!cpu.IsStepping()) |
| 554 | return; |
| 555 | |
| 556 | Common::Event sync_event; |
| 557 | |
| 558 | auto& power_pc = m_system.GetPowerPC(); |
| 559 | PowerPC::CoreMode old_mode = power_pc.GetMode(); |
| 560 | power_pc.SetMode(PowerPC::CoreMode::Interpreter); |
| 561 | cpu.StepOpcode(&sync_event); |
| 562 | sync_event.WaitFor(std::chrono::milliseconds(20)); |
| 563 | power_pc.SetMode(old_mode); |
| 564 | Core::DisplayMessage(tr("Step successful!").toStdString(), 2000); |
| 565 | // Will get a UpdateDisasmDialog(), don't update the GUI here. |
| 566 | |
| 567 | // TODO: Step doesn't cause EmulationStateChanged to be emitted, so it has to call this manually. |
| 568 | if (m_branch_watch_dialog != nullptr) |
| 569 | m_branch_watch_dialog->Update(); |
| 570 | } |
| 571 | |
| 572 | void CodeWidget::StepOver() |
| 573 | { |
| 574 | auto& cpu = m_system.GetCPU(); |
| 575 | |
| 576 | if (!cpu.IsStepping()) |
| 577 | return; |
| 578 | |
| 579 | const UGeckoInstruction inst = [&] { |
| 580 | Core::CPUThreadGuard guard(m_system); |
| 581 | return PowerPC::MMU::HostRead_Instruction(guard, m_system.GetPPCState().pc); |
| 582 | }(); |
| 583 | |
| 584 | if (inst.LK) |
| 585 | { |
| 586 | auto& breakpoints = m_system.GetPowerPC().GetBreakPoints(); |
| 587 | breakpoints.SetTemporary(m_system.GetPPCState().pc + 4); |
| 588 | cpu.SetStepping(false); |
| 589 | Core::DisplayMessage(tr("Step over in progress...").toStdString(), 2000); |
| 590 | } |
| 591 | else |
| 592 | { |
| 593 | Step(); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | // Returns true on a rfi, blr or on a bclr that evaluates to true. |
| 598 | static bool WillInstructionReturn(Core::System& system, UGeckoInstruction inst) |
| 599 | { |
| 600 | // Is a rfi instruction |
| 601 | if (inst.hex == 0x4C000064u) |
| 602 | return true; |
| 603 | const auto& ppc_state = system.GetPPCState(); |
| 604 | bool counter = (inst.BO_2 >> 2 & 1) != 0 || (CTR(ppc_state) != 0) != ((inst.BO_2 >> 1 & 1) != 0); |
| 605 | bool condition = inst.BO_2 >> 4 != 0 || ppc_state.cr.GetBit(inst.BI_2) == (inst.BO_2 >> 3 & 1); |
| 606 | bool isBclr = inst.OPCD_7 == 0b010011 && inst.XO == 16; |
| 607 | return isBclr && counter && condition && !inst.LK_3; |
| 608 | } |
| 609 | |
| 610 | void CodeWidget::StepOut() |
| 611 | { |
| 612 | auto& cpu = m_system.GetCPU(); |
| 613 | |
| 614 | if (!cpu.IsStepping()) |
| 615 | return; |
| 616 | |
| 617 | // Keep stepping until the next return instruction or timeout after five seconds |
| 618 | using clock = std::chrono::steady_clock; |
| 619 | clock::time_point timeout = clock::now() + std::chrono::seconds(5); |
| 620 | |
| 621 | auto& power_pc = m_system.GetPowerPC(); |
| 622 | { |
| 623 | auto& ppc_state = power_pc.GetPPCState(); |
| 624 | Core::CPUThreadGuard guard(m_system); |
| 625 | |
| 626 | PowerPC::CoreMode old_mode = power_pc.GetMode(); |
| 627 | power_pc.SetMode(PowerPC::CoreMode::Interpreter); |
| 628 | |
| 629 | // Loop until either the current instruction is a return instruction with no Link flag |
| 630 | // or a breakpoint is detected so it can step at the breakpoint. If the PC is currently |
| 631 | // on a breakpoint, skip it. |
| 632 | UGeckoInstruction inst = PowerPC::MMU::HostRead_Instruction(guard, ppc_state.pc); |
| 633 | do |
| 634 | { |
| 635 | if (WillInstructionReturn(m_system, inst)) |
| 636 | { |
| 637 | power_pc.SingleStep(); |
| 638 | break; |
| 639 | } |
| 640 | |
| 641 | if (inst.LK) |
| 642 | { |
| 643 | // Step over branches |
| 644 | u32 next_pc = ppc_state.pc + 4; |
| 645 | do |
| 646 | { |
| 647 | power_pc.SingleStep(); |
| 648 | } while (ppc_state.pc != next_pc && clock::now() < timeout && !power_pc.CheckBreakPoints()); |
| 649 | } |
| 650 | else |
| 651 | { |
| 652 | power_pc.SingleStep(); |
| 653 | } |
| 654 | |
| 655 | inst = PowerPC::MMU::HostRead_Instruction(guard, ppc_state.pc); |
| 656 | } while (clock::now() < timeout && !power_pc.CheckBreakPoints()); |
| 657 | |
| 658 | power_pc.SetMode(old_mode); |
| 659 | } |
| 660 | |
| 661 | emit Host::GetInstance()->UpdateDisasmDialog(); |
| 662 | |
| 663 | if (power_pc.CheckBreakPoints()) |
| 664 | Core::DisplayMessage(tr("Breakpoint encountered! Step out aborted.").toStdString(), 2000); |
| 665 | else if (clock::now() >= timeout) |
| 666 | Core::DisplayMessage(tr("Step out timed out!").toStdString(), 2000); |
| 667 | else |
| 668 | Core::DisplayMessage(tr("Step out successful!").toStdString(), 2000); |
| 669 | } |
| 670 | |
| 671 | void CodeWidget::Skip() |
| 672 | { |
| 673 | m_system.GetPPCState().pc += 4; |
| 674 | ShowPC(); |
| 675 | } |
| 676 | |
| 677 | void CodeWidget::ShowPC() |
| 678 | { |
| 679 | m_code_view->SetAddress(m_system.GetPPCState().pc, CodeViewWidget::SetAddressUpdate::WithUpdate); |
| 680 | Update(); |
| 681 | } |
| 682 | |
| 683 | void CodeWidget::SetPC() |
| 684 | { |
| 685 | m_system.GetPPCState().pc = m_code_view->GetAddress(); |
| 686 | Update(); |
| 687 | } |
| 688 | |
| 689 | void CodeWidget::ToggleBreakpoint() |
| 690 | { |
| 691 | m_code_view->ToggleBreakpoint(); |
| 692 | } |
| 693 | |
| 694 | void CodeWidget::AddBreakpoint() |
| 695 | { |
| 696 | m_code_view->AddBreakpoint(); |
| 697 | } |