| 1 | // Copyright 2020 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Debugger/ThreadWidget.h" |
| 5 | |
| 6 | #include <bit> |
| 7 | |
| 8 | #include <QGroupBox> |
| 9 | #include <QHeaderView> |
| 10 | #include <QLabel> |
| 11 | #include <QLineEdit> |
| 12 | #include <QMenu> |
| 13 | #include <QTableWidget> |
| 14 | #include <QVBoxLayout> |
| 15 | |
| 16 | #include "Core/Core.h" |
| 17 | #include "Core/PowerPC/MMU.h" |
| 18 | #include "Core/PowerPC/PowerPC.h" |
| 19 | #include "Core/System.h" |
| 20 | #include "DolphinQt/Host.h" |
| 21 | #include "DolphinQt/QtUtils/FromStdString.h" |
| 22 | #include "DolphinQt/Settings.h" |
| 23 | |
| 24 | ThreadWidget::ThreadWidget(QWidget* parent) : QDockWidget(parent) |
| 25 | { |
| 26 | setWindowTitle(tr("Threads")); |
| 27 | setObjectName(QStringLiteral("threads")); |
| 28 | |
| 29 | setHidden(!Settings::Instance().IsThreadsVisible() || !Settings::Instance().IsDebugModeEnabled()); |
| 30 | |
| 31 | setAllowedAreas(Qt::AllDockWidgetAreas); |
| 32 | |
| 33 | CreateWidgets(); |
| 34 | |
| 35 | auto& settings = Settings::GetQSettings(); |
| 36 | |
| 37 | restoreGeometry(settings.value(QStringLiteral("threadwidget/geometry")).toByteArray()); |
| 38 | // macOS: setHidden() needs to be evaluated before setFloating() for proper window presentation |
| 39 | // according to Settings |
| 40 | setFloating(settings.value(QStringLiteral("threadwidget/floating")).toBool()); |
| 41 | |
| 42 | ConnectWidgets(); |
| 43 | |
| 44 | connect(Host::GetInstance(), &Host::UpdateDisasmDialog, this, &ThreadWidget::Update); |
| 45 | |
| 46 | connect(&Settings::Instance(), &Settings::ThreadsVisibilityChanged, this, |
| 47 | [this](bool visible) { setHidden(!visible); }); |
| 48 | |
| 49 | connect(&Settings::Instance(), &Settings::DebugModeToggled, this, [this](bool enabled) { |
| 50 | setHidden(!enabled || !Settings::Instance().IsThreadsVisible()); |
| 51 | }); |
| 52 | } |
| 53 | |
| 54 | ThreadWidget::~ThreadWidget() |
| 55 | { |
| 56 | auto& settings = Settings::GetQSettings(); |
| 57 | |
| 58 | settings.setValue(QStringLiteral("threadwidget/geometry"), saveGeometry()); |
| 59 | settings.setValue(QStringLiteral("threadwidget/floating"), isFloating()); |
| 60 | } |
| 61 | |
| 62 | void ThreadWidget::closeEvent(QCloseEvent*) |
| 63 | { |
| 64 | Settings::Instance().SetThreadsVisible(false); |
| 65 | } |
| 66 | |
| 67 | void ThreadWidget::showEvent(QShowEvent* event) |
| 68 | { |
| 69 | Update(); |
| 70 | } |
| 71 | |
| 72 | void ThreadWidget::CreateWidgets() |
| 73 | { |
| 74 | m_state = new QGroupBox(tr("State")); |
| 75 | |
| 76 | auto* state_layout = new QHBoxLayout; |
| 77 | m_state->setLayout(state_layout); |
| 78 | state_layout->addWidget(CreateContextGroup()); |
| 79 | state_layout->addWidget(CreateActiveThreadQueueGroup()); |
| 80 | state_layout->setContentsMargins(2, 2, 2, 2); |
| 81 | state_layout->setSpacing(1); |
| 82 | |
| 83 | auto* widget = new QWidget; |
| 84 | auto* layout = new QVBoxLayout; |
| 85 | widget->setLayout(layout); |
| 86 | layout->addWidget(m_state); |
| 87 | layout->addWidget(CreateThreadGroup()); |
| 88 | layout->addWidget(CreateThreadContextGroup()); |
| 89 | layout->addWidget(CreateThreadCallstackGroup()); |
| 90 | layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding)); |
| 91 | setWidget(widget); |
| 92 | |
| 93 | Update(); |
| 94 | UpdateThreadContext({}); |
| 95 | } |
| 96 | |
| 97 | void ThreadWidget::ConnectWidgets() |
| 98 | { |
| 99 | connect(m_thread_table->selectionModel(), &QItemSelectionModel::selectionChanged, |
| 100 | [this](const QItemSelection& selected, const QItemSelection& deselected) { |
| 101 | const auto indexes = selected.indexes(); |
| 102 | const int row = indexes.empty() ? -1 : indexes.first().row(); |
| 103 | OnSelectionChanged(row); |
| 104 | }); |
| 105 | connect(m_context_table, &QTableWidget::customContextMenuRequested, |
| 106 | [this] { ShowContextMenu(m_context_table); }); |
| 107 | connect(m_callstack_table, &QTableWidget::customContextMenuRequested, |
| 108 | [this] { ShowContextMenu(m_callstack_table); }); |
| 109 | } |
| 110 | |
| 111 | void ThreadWidget::ShowContextMenu(QTableWidget* table) |
| 112 | { |
| 113 | const auto* item = static_cast<QTableWidgetItem*>(table->currentItem()); |
| 114 | if (item == nullptr) |
| 115 | return; |
| 116 | |
| 117 | bool ok; |
| 118 | const u32 addr = item->text().toUInt(&ok, 16); |
| 119 | if (!ok) |
| 120 | return; |
| 121 | |
| 122 | QMenu* menu = new QMenu(this); |
| 123 | menu->setAttribute(Qt::WA_DeleteOnClose, true); |
| 124 | |
| 125 | const QString watch_name = QStringLiteral("thread_context_%1").arg(addr, 8, 16, QLatin1Char('0')); |
| 126 | menu->addAction(tr("Add &breakpoint"), this, [this, addr] { emit RequestBreakpoint(addr); }); |
| 127 | menu->addAction(tr("Add memory breakpoint"), this, |
| 128 | [this, addr] { emit RequestMemoryBreakpoint(addr); }); |
| 129 | menu->addAction(tr("Add to &watch"), this, |
| 130 | [this, addr, watch_name] { emit RequestWatch(watch_name, addr); }); |
| 131 | menu->addAction(tr("View &memory"), this, [this, addr] { emit RequestViewInMemory(addr); }); |
| 132 | menu->addAction(tr("View &code"), this, [this, addr] { emit RequestViewInCode(addr); }); |
| 133 | menu->exec(QCursor::pos()); |
| 134 | } |
| 135 | |
| 136 | QLineEdit* ThreadWidget::CreateLineEdit() const |
| 137 | { |
| 138 | QLineEdit* line_edit = new QLineEdit(QStringLiteral("00000000")); |
| 139 | line_edit->setReadOnly(true); |
| 140 | line_edit->setAlignment(Qt::AlignVCenter | Qt::AlignRight); |
| 141 | return line_edit; |
| 142 | } |
| 143 | |
| 144 | QGroupBox* ThreadWidget::CreateContextGroup() |
| 145 | { |
| 146 | QGroupBox* context_group = new QGroupBox(tr("Thread context")); |
| 147 | QGridLayout* context_layout = new QGridLayout; |
| 148 | context_layout->setColumnStretch(0, 1); |
| 149 | context_layout->setColumnStretch(1, 1); |
| 150 | context_group->setLayout(context_layout); |
| 151 | context_layout->addWidget(new QLabel(tr("Current context")), 0, 0); |
| 152 | m_current_context = CreateLineEdit(); |
| 153 | context_layout->addWidget(m_current_context, 0, 1); |
| 154 | context_layout->addWidget(new QLabel(tr("Current thread")), 1, 0); |
| 155 | m_current_thread = CreateLineEdit(); |
| 156 | context_layout->addWidget(m_current_thread, 1, 1); |
| 157 | context_layout->addWidget(new QLabel(tr("Default thread")), 2, 0); |
| 158 | m_default_thread = CreateLineEdit(); |
| 159 | context_layout->addWidget(m_default_thread, 2, 1); |
| 160 | context_layout->setSpacing(1); |
| 161 | return context_group; |
| 162 | } |
| 163 | |
| 164 | QGroupBox* ThreadWidget::CreateActiveThreadQueueGroup() |
| 165 | { |
| 166 | QGroupBox* thread_queue_group = new QGroupBox(tr("Active thread queue")); |
| 167 | auto* thread_queue_layout = new QGridLayout; |
| 168 | thread_queue_layout->setColumnStretch(0, 1); |
| 169 | thread_queue_layout->setColumnStretch(1, 1); |
| 170 | thread_queue_group->setLayout(thread_queue_layout); |
| 171 | thread_queue_layout->addWidget(new QLabel(tr("Head")), 0, 0); |
| 172 | m_queue_head = CreateLineEdit(); |
| 173 | thread_queue_layout->addWidget(m_queue_head, 0, 1); |
| 174 | thread_queue_layout->addWidget(new QLabel(tr("Tail")), 1, 0); |
| 175 | m_queue_tail = CreateLineEdit(); |
| 176 | thread_queue_layout->addWidget(m_queue_tail, 1, 1); |
| 177 | thread_queue_layout->setSpacing(1); |
| 178 | return thread_queue_group; |
| 179 | } |
| 180 | |
| 181 | QGroupBox* ThreadWidget::CreateThreadGroup() |
| 182 | { |
| 183 | QGroupBox* thread_group = new QGroupBox(tr("Active threads")); |
| 184 | QGridLayout* thread_layout = new QGridLayout; |
| 185 | thread_group->setLayout(thread_layout); |
| 186 | |
| 187 | m_thread_table = new QTableWidget(); |
| 188 | QStringList header{tr("Address"), |
| 189 | tr("State"), |
| 190 | tr("Detached"), |
| 191 | tr("Suspended"), |
| 192 | QStringLiteral("%1\n(%2)").arg(tr("Base priority"), tr("Effective priority")), |
| 193 | QStringLiteral("%1\n%2").arg(tr("Stack end"), tr("Stack start")), |
| 194 | tr("errno"), |
| 195 | tr("Specific")}; |
| 196 | m_thread_table->setColumnCount(header.size()); |
| 197 | |
| 198 | m_thread_table->setHorizontalHeaderLabels(header); |
| 199 | m_thread_table->setTabKeyNavigation(false); |
| 200 | m_thread_table->verticalHeader()->setVisible(false); |
| 201 | m_thread_table->setEditTriggers(QAbstractItemView::NoEditTriggers); |
| 202 | m_thread_table->setSelectionBehavior(QAbstractItemView::SelectRows); |
| 203 | m_thread_table->setSelectionMode(QAbstractItemView::SingleSelection); |
| 204 | m_thread_table->setWordWrap(false); |
| 205 | |
| 206 | thread_layout->addWidget(m_thread_table, 0, 0); |
| 207 | thread_layout->setSpacing(1); |
| 208 | return thread_group; |
| 209 | } |
| 210 | |
| 211 | QGroupBox* ThreadWidget::CreateThreadContextGroup() |
| 212 | { |
| 213 | QGroupBox* thread_context_group = new QGroupBox(tr("Selected thread context")); |
| 214 | QGridLayout* thread_context_layout = new QGridLayout; |
| 215 | thread_context_group->setLayout(thread_context_layout); |
| 216 | |
| 217 | m_context_table = new QTableWidget(); |
| 218 | m_context_table->setColumnCount(8); // GPR, FPR, PSF, (GQR+others) |
| 219 | m_context_table->setRowCount(32); |
| 220 | m_context_table->setTabKeyNavigation(false); |
| 221 | m_context_table->horizontalHeader()->setVisible(false); |
| 222 | m_context_table->verticalHeader()->setVisible(false); |
| 223 | m_context_table->setEditTriggers(QAbstractItemView::NoEditTriggers); |
| 224 | m_context_table->setSelectionBehavior(QAbstractItemView::SelectItems); |
| 225 | m_context_table->setSelectionMode(QAbstractItemView::SingleSelection); |
| 226 | m_context_table->setContextMenuPolicy(Qt::CustomContextMenu); |
| 227 | |
| 228 | thread_context_layout->addWidget(m_context_table, 0, 0); |
| 229 | thread_context_layout->setSpacing(1); |
| 230 | return thread_context_group; |
| 231 | } |
| 232 | |
| 233 | QGroupBox* ThreadWidget::CreateThreadCallstackGroup() |
| 234 | { |
| 235 | QGroupBox* thread_callstack_group = new QGroupBox(tr("Selected thread callstack")); |
| 236 | QGridLayout* thread_callstack_layout = new QGridLayout; |
| 237 | thread_callstack_group->setLayout(thread_callstack_layout); |
| 238 | |
| 239 | m_callstack_table = new QTableWidget(); |
| 240 | QStringList header{tr("Address"), tr("Back Chain"), tr("LR Save"), tr("Description")}; |
| 241 | m_callstack_table->setColumnCount(header.size()); |
| 242 | |
| 243 | m_callstack_table->setHorizontalHeaderLabels(header); |
| 244 | m_callstack_table->setRowCount(0); |
| 245 | m_callstack_table->setTabKeyNavigation(false); |
| 246 | m_callstack_table->verticalHeader()->setVisible(false); |
| 247 | m_callstack_table->setEditTriggers(QAbstractItemView::NoEditTriggers); |
| 248 | m_callstack_table->setSelectionBehavior(QAbstractItemView::SelectItems); |
| 249 | m_callstack_table->setSelectionMode(QAbstractItemView::SingleSelection); |
| 250 | m_callstack_table->setContextMenuPolicy(Qt::CustomContextMenu); |
| 251 | |
| 252 | thread_callstack_layout->addWidget(m_callstack_table, 0, 0); |
| 253 | thread_callstack_layout->setSpacing(1); |
| 254 | return thread_callstack_group; |
| 255 | } |
| 256 | |
| 257 | void ThreadWidget::Update() |
| 258 | { |
| 259 | if (!isVisible()) |
| 260 | return; |
| 261 | |
| 262 | auto& system = Core::System::GetInstance(); |
| 263 | const auto emu_state = Core::GetState(system); |
| 264 | if (emu_state == Core::State::Stopping || emu_state == Core::State::Uninitialized) |
| 265 | { |
| 266 | m_thread_table->setRowCount(0); |
| 267 | UpdateThreadContext({}); |
| 268 | |
| 269 | const Core::CPUThreadGuard guard(system); |
| 270 | UpdateThreadCallstack(guard, {}); |
| 271 | } |
| 272 | if (emu_state != Core::State::Paused) |
| 273 | return; |
| 274 | |
| 275 | const auto format_hex = [](u32 value) { |
| 276 | return QStringLiteral("%1").arg(value, 8, 16, QLatin1Char('0')); |
| 277 | }; |
| 278 | const auto format_hex_from = [&format_hex](const Core::CPUThreadGuard& guard, u32 addr) { |
| 279 | addr = |
| 280 | PowerPC::MMU::HostIsRAMAddress(guard, addr) ? PowerPC::MMU::HostRead<u32>(guard, addr) : 0; |
| 281 | return format_hex(addr); |
| 282 | }; |
| 283 | const auto get_state = [](u16 thread_state) { |
| 284 | QString state_name; |
| 285 | switch (thread_state) |
| 286 | { |
| 287 | case 1: |
| 288 | state_name = tr("READY"); |
| 289 | break; |
| 290 | case 2: |
| 291 | state_name = tr("RUNNING"); |
| 292 | break; |
| 293 | case 4: |
| 294 | state_name = tr("WAITING"); |
| 295 | break; |
| 296 | case 8: |
| 297 | state_name = tr("MORIBUND"); |
| 298 | break; |
| 299 | default: |
| 300 | state_name = tr("UNKNOWN"); |
| 301 | } |
| 302 | return QStringLiteral("%1 (%2)").arg(QString::number(thread_state), state_name); |
| 303 | }; |
| 304 | const auto get_priority = [](u16 base, u16 effective) { |
| 305 | return QStringLiteral("%1 (%2)").arg(QString::number(base), QString::number(effective)); |
| 306 | }; |
| 307 | const auto get_stack = [](u32 end, u32 start) { |
| 308 | return QStringLiteral("%1\n%2") |
| 309 | .arg(end, 8, 16, QLatin1Char('0')) |
| 310 | .arg(start, 8, 16, QLatin1Char('0')); |
| 311 | }; |
| 312 | |
| 313 | { |
| 314 | const Core::CPUThreadGuard guard(system); |
| 315 | |
| 316 | // YAGCD - Section 4.2.1.4 Dolphin OS Globals |
| 317 | m_current_context->setText(format_hex_from(guard, 0x800000D4)); |
| 318 | m_current_thread->setText(format_hex_from(guard, 0x800000E4)); |
| 319 | m_default_thread->setText(format_hex_from(guard, 0x800000D8)); |
| 320 | |
| 321 | m_queue_head->setText(format_hex_from(guard, 0x800000DC)); |
| 322 | m_queue_tail->setText(format_hex_from(guard, 0x800000E0)); |
| 323 | |
| 324 | // Thread group |
| 325 | m_threads = guard.GetSystem().GetPowerPC().GetDebugInterface().GetThreads(guard); |
| 326 | |
| 327 | int i = 0; |
| 328 | m_thread_table->setRowCount(i); |
| 329 | for (const auto& thread : m_threads) |
| 330 | { |
| 331 | m_thread_table->insertRow(i); |
| 332 | m_thread_table->setItem(i, 0, new QTableWidgetItem(format_hex(thread->GetAddress()))); |
| 333 | m_thread_table->setItem(i, 1, new QTableWidgetItem(get_state(thread->GetState()))); |
| 334 | m_thread_table->setItem(i, 2, new QTableWidgetItem(QString::number(thread->IsDetached()))); |
| 335 | m_thread_table->setItem(i, 3, new QTableWidgetItem(QString::number(thread->IsSuspended()))); |
| 336 | m_thread_table->setItem(i, 4, |
| 337 | new QTableWidgetItem(get_priority(thread->GetBasePriority(), |
| 338 | thread->GetEffectivePriority()))); |
| 339 | m_thread_table->setItem( |
| 340 | i, 5, new QTableWidgetItem(get_stack(thread->GetStackEnd(), thread->GetStackStart()))); |
| 341 | m_thread_table->setItem(i, 6, new QTableWidgetItem(QString::number(thread->GetErrno()))); |
| 342 | m_thread_table->setItem( |
| 343 | i, 7, new QTableWidgetItem(QString::fromStdString(thread->GetSpecific(guard)))); |
| 344 | i += 1; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | m_thread_table->resizeColumnsToContents(); |
| 349 | m_thread_table->resizeRowsToContents(); |
| 350 | |
| 351 | // Thread's context group |
| 352 | UpdateThreadContext({}); |
| 353 | } |
| 354 | |
| 355 | void ThreadWidget::UpdateThreadContext(const Common::Debug::PartialContext& context) |
| 356 | { |
| 357 | const auto format_hex = [](const std::optional<u32>& value) { |
| 358 | if (!value) |
| 359 | return QString{}; |
| 360 | return QStringLiteral("%1").arg(*value, 8, 16, QLatin1Char('0')); |
| 361 | }; |
| 362 | const auto format_hex_idx = [](const auto& table, std::size_t index) { |
| 363 | if (!table || index >= table->size()) |
| 364 | return QString{}; |
| 365 | return QStringLiteral("%1").arg(table->at(index), 8, 16, QLatin1Char('0')); |
| 366 | }; |
| 367 | const auto format_f64_as_u64_idx = [](const auto& table, std::size_t index) { |
| 368 | if (!table || index >= table->size()) |
| 369 | return QString{}; |
| 370 | return QStringLiteral("%1").arg(std::bit_cast<u64>(table->at(index)), 16, 16, QLatin1Char('0')); |
| 371 | }; |
| 372 | |
| 373 | m_context_table->setRowCount(0); |
| 374 | for (int i = 0; i < 32; i++) |
| 375 | { |
| 376 | m_context_table->insertRow(i); |
| 377 | m_context_table->setItem(i, 0, new QTableWidgetItem(QStringLiteral("GPR%1").arg(i))); |
| 378 | m_context_table->setItem(i, 1, new QTableWidgetItem(format_hex_idx(context.gpr, i))); |
| 379 | m_context_table->setItem(i, 2, new QTableWidgetItem(QStringLiteral("FPR%1").arg(i))); |
| 380 | m_context_table->setItem(i, 3, new QTableWidgetItem(format_f64_as_u64_idx(context.fpr, i))); |
| 381 | m_context_table->setItem(i, 4, new QTableWidgetItem(QStringLiteral("PSF%1").arg(i))); |
| 382 | m_context_table->setItem(i, 5, new QTableWidgetItem(format_f64_as_u64_idx(context.psf, i))); |
| 383 | |
| 384 | if (i < 8) |
| 385 | { |
| 386 | m_context_table->setItem(i, 6, new QTableWidgetItem(QStringLiteral("GQR%1").arg(i))); |
| 387 | m_context_table->setItem(i, 7, new QTableWidgetItem(format_hex_idx(context.gqr, i))); |
| 388 | continue; |
| 389 | } |
| 390 | switch (i) |
| 391 | { |
| 392 | case 8: |
| 393 | m_context_table->setItem(i, 6, new QTableWidgetItem(QStringLiteral("CR"))); |
| 394 | m_context_table->setItem(i, 7, new QTableWidgetItem(format_hex(context.cr))); |
| 395 | break; |
| 396 | case 9: |
| 397 | m_context_table->setItem(i, 6, new QTableWidgetItem(QStringLiteral("LR"))); |
| 398 | m_context_table->setItem(i, 7, new QTableWidgetItem(format_hex(context.lr))); |
| 399 | break; |
| 400 | case 10: |
| 401 | m_context_table->setItem(i, 6, new QTableWidgetItem(QStringLiteral("CTR"))); |
| 402 | m_context_table->setItem(i, 7, new QTableWidgetItem(format_hex(context.ctr))); |
| 403 | break; |
| 404 | case 11: |
| 405 | m_context_table->setItem(i, 6, new QTableWidgetItem(QStringLiteral("XER"))); |
| 406 | m_context_table->setItem(i, 7, new QTableWidgetItem(format_hex(context.xer))); |
| 407 | break; |
| 408 | case 12: |
| 409 | m_context_table->setItem(i, 6, new QTableWidgetItem(QStringLiteral("FPSCR"))); |
| 410 | m_context_table->setItem(i, 7, new QTableWidgetItem(format_hex(context.fpscr))); |
| 411 | break; |
| 412 | case 13: |
| 413 | m_context_table->setItem(i, 6, new QTableWidgetItem(QStringLiteral("SRR0"))); |
| 414 | m_context_table->setItem(i, 7, new QTableWidgetItem(format_hex(context.srr0))); |
| 415 | break; |
| 416 | case 14: |
| 417 | m_context_table->setItem(i, 6, new QTableWidgetItem(QStringLiteral("SRR1"))); |
| 418 | m_context_table->setItem(i, 7, new QTableWidgetItem(format_hex(context.srr1))); |
| 419 | break; |
| 420 | case 15: |
| 421 | m_context_table->setItem(i, 6, new QTableWidgetItem(QStringLiteral("DUMMY"))); |
| 422 | m_context_table->setItem(i, 7, new QTableWidgetItem(format_hex(context.dummy))); |
| 423 | break; |
| 424 | case 16: |
| 425 | m_context_table->setItem(i, 6, new QTableWidgetItem(QStringLiteral("STATE"))); |
| 426 | m_context_table->setItem(i, 7, new QTableWidgetItem(format_hex(context.state))); |
| 427 | break; |
| 428 | default: |
| 429 | for (int j = 6; j <= 7; j++) |
| 430 | { |
| 431 | auto* disabled_item = new QTableWidgetItem(); |
| 432 | auto flags = disabled_item->flags(); |
| 433 | flags &= ~Qt::ItemIsSelectable; |
| 434 | flags &= ~Qt::ItemIsEditable; |
| 435 | flags &= ~Qt::ItemIsEnabled; |
| 436 | disabled_item->setFlags(flags); |
| 437 | disabled_item->setBackground(Qt::gray); |
| 438 | m_context_table->setItem(i, j, disabled_item); |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | m_context_table->resizeColumnsToContents(); |
| 443 | } |
| 444 | |
| 445 | void ThreadWidget::UpdateThreadCallstack(const Core::CPUThreadGuard& guard, |
| 446 | const Common::Debug::PartialContext& context) |
| 447 | { |
| 448 | m_callstack_table->setRowCount(0); |
| 449 | |
| 450 | if (!context.gpr) |
| 451 | return; |
| 452 | |
| 453 | const auto format_hex = [](u32 value) { |
| 454 | return QStringLiteral("%1").arg(value, 8, 16, QLatin1Char('0')); |
| 455 | }; |
| 456 | |
| 457 | u32 sp = context.gpr->at(1); |
| 458 | for (int i = 0; i < 16; i++) |
| 459 | { |
| 460 | if (sp == 0 || sp == 0xffffffff || !PowerPC::MMU::HostIsRAMAddress(guard, sp)) |
| 461 | break; |
| 462 | m_callstack_table->insertRow(i); |
| 463 | m_callstack_table->setItem(i, 0, new QTableWidgetItem(format_hex(sp))); |
| 464 | if (PowerPC::MMU::HostIsRAMAddress(guard, sp + 4)) |
| 465 | { |
| 466 | const u32 lr_save = PowerPC::MMU::HostRead<u32>(guard, sp + 4); |
| 467 | m_callstack_table->setItem(i, 2, new QTableWidgetItem(format_hex(lr_save))); |
| 468 | m_callstack_table->setItem( |
| 469 | i, 3, |
| 470 | new QTableWidgetItem(QtUtils::FromStdString( |
| 471 | guard.GetSystem().GetPowerPC().GetDebugInterface().GetDescription(lr_save)))); |
| 472 | } |
| 473 | else |
| 474 | { |
| 475 | m_callstack_table->setItem(i, 2, new QTableWidgetItem(QStringLiteral("--------"))); |
| 476 | } |
| 477 | sp = PowerPC::MMU::HostRead<u32>(guard, sp); |
| 478 | m_callstack_table->setItem(i, 1, new QTableWidgetItem(format_hex(sp))); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | void ThreadWidget::OnSelectionChanged(int row) |
| 483 | { |
| 484 | Core::CPUThreadGuard guard(Core::System::GetInstance()); |
| 485 | Common::Debug::PartialContext context; |
| 486 | |
| 487 | if (row >= 0 && size_t(row) < m_threads.size()) |
| 488 | context = m_threads[row]->GetContext(guard); |
| 489 | |
| 490 | UpdateThreadContext(context); |
| 491 | UpdateThreadCallstack(guard, context); |
| 492 | } |