Debian Codegraph @

Source/Core/DolphinQt/Debugger/WatchWidget.cpp

raw symbols SCIP
1 // Copyright 2017 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "DolphinQt/Debugger/WatchWidget.h"
5
6 #include <QHeaderView>
7 #include <QInputDialog>
8 #include <QMenu>
9 #include <QTableWidget>
10 #include <QToolBar>
11 #include <QVBoxLayout>
12
13 #include "Common/FileUtil.h"
14 #include "Common/IniFile.h"
15 #include "Core/ConfigManager.h"
16 #include "Core/Core.h"
17 #include "Core/PowerPC/MMU.h"
18 #include "Core/PowerPC/PowerPC.h"
19 #include "Core/System.h"
20
21 #include "DolphinQt/Host.h"
22 #include "DolphinQt/QtUtils/ModalMessageBox.h"
23 #include "DolphinQt/Resources.h"
24 #include "DolphinQt/Settings.h"
25
26 WatchWidget::WatchWidget(QWidget* parent)
27 : QDockWidget(parent), m_system(Core::System::GetInstance())
28 {
29 // i18n: This kind of "watch" is used for watching emulated memory.
30 // It's not related to timekeeping devices.
31 setWindowTitle(tr("Watch"));
32 setObjectName(QStringLiteral("watch"));
33
34 setHidden(!Settings::Instance().IsWatchVisible() || !Settings::Instance().IsDebugModeEnabled());
35
36 setAllowedAreas(Qt::AllDockWidgetAreas);
37
38 CreateWidgets();
39
40 auto& settings = Settings::GetQSettings();
41
42 restoreGeometry(settings.value(QStringLiteral("watchwidget/geometry")).toByteArray());
43 // macOS: setHidden() needs to be evaluated before setFloating() for proper window presentation
44 // according to Settings
45 setFloating(settings.value(QStringLiteral("watchwidget/floating")).toBool());
46
47 ConnectWidgets();
48
49 connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, [this](Core::State state) {
50 UpdateButtonsEnabled();
51 if (state != Core::State::Starting)
52 Update();
53 });
54
55 connect(Host::GetInstance(), &Host::UpdateDisasmDialog, this, &WatchWidget::Update);
56
57 connect(&Settings::Instance(), &Settings::WatchVisibilityChanged, this,
58 [this](bool visible) { setHidden(!visible); });
59
60 connect(&Settings::Instance(), &Settings::DebugModeToggled, this,
61 [this](bool enabled) { setHidden(!enabled || !Settings::Instance().IsWatchVisible()); });
62
63 connect(&Settings::Instance(), &Settings::ThemeChanged, this, &WatchWidget::UpdateIcons);
64 UpdateIcons();
65 }
66
67 WatchWidget::~WatchWidget()
68 {
69 auto& settings = Settings::GetQSettings();
70
71 settings.setValue(QStringLiteral("watchwidget/geometry"), saveGeometry());
72 settings.setValue(QStringLiteral("watchwidget/floating"), isFloating());
73 }
74
75 void WatchWidget::CreateWidgets()
76 {
77 m_toolbar = new QToolBar;
78 m_toolbar->setContentsMargins(0, 0, 0, 0);
79 m_toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
80
81 m_table = new QTableWidget;
82 m_table->setTabKeyNavigation(false);
83
84 m_table->setContentsMargins(0, 0, 0, 0);
85 m_table->setColumnCount(NUM_COLUMNS);
86 m_table->verticalHeader()->setHidden(true);
87 m_table->setContextMenuPolicy(Qt::CustomContextMenu);
88 m_table->setSelectionMode(QAbstractItemView::ExtendedSelection);
89 m_table->setSelectionBehavior(QAbstractItemView::SelectRows);
90 m_table->setHorizontalHeaderLabels(
91 {tr("Label"), tr("Address"), tr("Hexadecimal"),
92 // i18n: The base 10 numeral system. Not related to non-integer numbers
93 tr("Decimal"),
94 // i18n: Data type used in computing
95 tr("String"),
96 // i18n: Floating-point (non-integer) number
97 tr("Float"), tr("Locked")});
98 m_table->setRowCount(1);
99 SetEmptyRow(0);
100
101 m_new = m_toolbar->addAction(tr("New"), this, &WatchWidget::OnNewWatch);
102 m_delete = m_toolbar->addAction(tr("Delete"), this, &WatchWidget::OnDelete);
103 m_clear = m_toolbar->addAction(tr("Clear"), this, &WatchWidget::OnClear);
104 m_load = m_toolbar->addAction(tr("Load"), this, &WatchWidget::OnLoad);
105 m_save = m_toolbar->addAction(tr("Save"), this, &WatchWidget::OnSave);
106
107 m_new->setEnabled(false);
108 m_delete->setEnabled(false);
109 m_clear->setEnabled(false);
110 m_load->setEnabled(false);
111 m_save->setEnabled(false);
112
113 auto* layout = new QVBoxLayout;
114 layout->setContentsMargins(2, 2, 2, 2);
115 layout->setSpacing(0);
116 layout->addWidget(m_toolbar);
117 layout->addWidget(m_table);
118
119 QWidget* widget = new QWidget;
120 widget->setLayout(layout);
121
122 setWidget(widget);
123 }
124
125 void WatchWidget::ConnectWidgets()
126 {
127 connect(m_table, &QTableWidget::customContextMenuRequested, this, &WatchWidget::ShowContextMenu);
128 connect(m_table, &QTableWidget::itemChanged, this, &WatchWidget::OnItemChanged);
129 }
130
131 void WatchWidget::UpdateIcons()
132 {
133 // TODO: Create a "debugger_add_watch" icon
134 m_new->setIcon(Resources::GetThemeIcon("debugger_add_breakpoint"));
135 m_delete->setIcon(Resources::GetThemeIcon("debugger_delete"));
136 m_clear->setIcon(Resources::GetThemeIcon("debugger_clear"));
137 m_load->setIcon(Resources::GetThemeIcon("debugger_load"));
138 m_save->setIcon(Resources::GetThemeIcon("debugger_save"));
139 }
140
141 void WatchWidget::UpdateButtonsEnabled()
142 {
143 if (!isVisible())
144 return;
145
146 const bool is_enabled = Core::IsRunning(m_system);
147 m_new->setEnabled(is_enabled);
148 m_delete->setEnabled(is_enabled);
149 m_clear->setEnabled(is_enabled);
150 m_load->setEnabled(is_enabled);
151 m_save->setEnabled(is_enabled);
152 }
153
154 void WatchWidget::Update()
155 {
156 if (!isVisible())
157 return;
158
159 m_updating = true;
160
161 if (Core::GetState(m_system) != Core::State::Paused)
162 {
163 m_table->setDisabled(true);
164 m_updating = false;
165 return;
166 }
167
168 m_table->setDisabled(false);
169 m_table->clearContents();
170
171 Core::CPUThreadGuard guard(m_system);
172 auto& debug_interface = guard.GetSystem().GetPowerPC().GetDebugInterface();
173
174 int size = static_cast<int>(debug_interface.GetWatches().size());
175
176 m_table->setRowCount(size + 1);
177
178 for (int i = 0; i < size; i++)
179 {
180 const auto& entry = debug_interface.GetWatch(i);
181
182 auto* label = new QTableWidgetItem(QString::fromStdString(entry.name));
183 auto* address =
184 new QTableWidgetItem(QStringLiteral("%1").arg(entry.address, 8, 16, QLatin1Char('0')));
185 auto* hex = new QTableWidgetItem;
186 auto* decimal = new QTableWidgetItem;
187 auto* string = new QTableWidgetItem;
188 auto* floatValue = new QTableWidgetItem;
189
190 auto* lockValue = new QTableWidgetItem;
191 lockValue->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable);
192
193 std::array<QTableWidgetItem*, NUM_COLUMNS> items = {label, address, hex, decimal,
194 string, floatValue, lockValue};
195
196 QBrush brush = QPalette().brush(QPalette::Text);
197
198 const bool core_is_running = Core::IsRunning(m_system);
199 if (!core_is_running || !PowerPC::MMU::HostIsRAMAddress(guard, entry.address))
200 brush.setColor(Qt::red);
201
202 if (core_is_running)
203 {
204 if (PowerPC::MMU::HostIsRAMAddress(guard, entry.address))
205 {
206 hex->setText(QStringLiteral("%1").arg(PowerPC::MMU::HostRead<u32>(guard, entry.address), 8,
207 16, QLatin1Char('0')));
208 decimal->setText(QString::number(PowerPC::MMU::HostRead<u32>(guard, entry.address)));
209 string->setText(
210 QString::fromStdString(PowerPC::MMU::HostGetString(guard, entry.address, 32)));
211 floatValue->setText(QString::number(PowerPC::MMU::HostRead<float>(guard, entry.address)));
212 lockValue->setCheckState(entry.locked ? Qt::Checked : Qt::Unchecked);
213 }
214 }
215
216 address->setForeground(brush);
217 string->setFlags(Qt::ItemIsEnabled);
218
219 for (int column = 0; column < NUM_COLUMNS; column++)
220 {
221 auto* item = items[column];
222 item->setData(Qt::UserRole, i);
223 item->setData(Qt::UserRole + 1, column);
224 m_table->setItem(i, column, item);
225 }
226 }
227
228 SetEmptyRow(size);
229
230 m_updating = false;
231 }
232
233 void WatchWidget::SetEmptyRow(int row)
234 {
235 auto* label = new QTableWidgetItem;
236 label->setData(Qt::UserRole, -1);
237
238 m_table->setItem(row, 0, label);
239
240 for (int i = 1; i < NUM_COLUMNS; i++)
241 {
242 auto* no_edit = new QTableWidgetItem;
243 no_edit->setFlags(Qt::ItemIsEnabled);
244 m_table->setItem(row, i, no_edit);
245 }
246 }
247
248 void WatchWidget::closeEvent(QCloseEvent*)
249 {
250 Settings::Instance().SetWatchVisible(false);
251 }
252
253 void WatchWidget::showEvent(QShowEvent* event)
254 {
255 UpdateButtonsEnabled();
256 Update();
257 }
258
259 void WatchWidget::OnDelete()
260 {
261 if (m_table->selectedItems().empty())
262 return;
263
264 DeleteSelectedWatches();
265 }
266
267 void WatchWidget::OnClear()
268 {
269 m_system.GetPowerPC().GetDebugInterface().ClearWatches();
270 Update();
271 }
272
273 void WatchWidget::OnNewWatch()
274 {
275 const QString text =
276 QInputDialog::getText(this, tr("Input"), tr("Enter address to watch:"), QLineEdit::Normal,
277 QString{}, nullptr, Qt::WindowCloseButtonHint);
278 bool good;
279 const uint address = text.toUInt(&good, 16);
280
281 if (!good)
282 {
283 ModalMessageBox::warning(this, tr("Error"), tr("Invalid watch address: %1").arg(text));
284 return;
285 }
286
287 const QString name = QStringLiteral("mem_%1").arg(address, 8, 16, QLatin1Char('0'));
288 AddWatch(name, address);
289 }
290
291 void WatchWidget::OnLoad()
292 {
293 Common::IniFile ini;
294
295 std::vector<std::string> watches;
296
297 if (!ini.Load(File::GetUserPath(D_GAMESETTINGS_IDX) + SConfig::GetInstance().GetGameID() + ".ini",
298 false))
299 {
300 return;
301 }
302
303 Core::CPUThreadGuard guard(m_system);
304
305 if (ini.GetLines("Watches", &watches, false))
306 {
307 auto& debug_interface = guard.GetSystem().GetPowerPC().GetDebugInterface();
308 for (const auto& watch : debug_interface.GetWatches())
309 {
310 debug_interface.UnsetPatch(guard, watch.address);
311 }
312 debug_interface.ClearWatches();
313 debug_interface.LoadWatchesFromStrings(watches);
314 }
315
316 Update();
317 }
318
319 void WatchWidget::OnSave()
320 {
321 Common::IniFile ini;
322 ini.Load(File::GetUserPath(D_GAMESETTINGS_IDX) + SConfig::GetInstance().GetGameID() + ".ini",
323 false);
324 ini.SetLines("Watches", m_system.GetPowerPC().GetDebugInterface().SaveWatchesToStrings());
325 ini.Save(File::GetUserPath(D_GAMESETTINGS_IDX) + SConfig::GetInstance().GetGameID() + ".ini");
326 }
327
328 void WatchWidget::ShowContextMenu()
329 {
330 QMenu* menu = new QMenu(this);
331 menu->setAttribute(Qt::WA_DeleteOnClose, true);
332
333 if (!m_table->selectedItems().empty())
334 {
335 const std::size_t count = m_table->selectionModel()->selectedRows().count();
336 if (count > 1)
337 {
338 // i18n: This kind of "watch" is used for watching emulated memory.
339 // It's not related to timekeeping devices.
340 menu->addAction(tr("&Delete Watches"), this, [this] { DeleteSelectedWatches(); });
341 // i18n: This kind of "watch" is used for watching emulated memory.
342 // It's not related to timekeeping devices.
343 menu->addAction(tr("&Lock Watches"), this, [this] { LockSelectedWatches(); });
344 // i18n: This kind of "watch" is used for watching emulated memory.
345 // It's not related to timekeeping devices.
346 menu->addAction(tr("&Unlock Watches"), this, [this] { UnlockSelectedWatches(); });
347 }
348 else if (count == 1)
349 {
350 auto row_variant = m_table->selectedItems()[0]->data(Qt::UserRole);
351
352 if (!row_variant.isNull())
353 {
354 int row = row_variant.toInt();
355
356 if (row >= 0)
357 {
358 menu->addAction(tr("Show in Memory"), this, [this, row] { ShowInMemory(row); });
359 // i18n: This kind of "watch" is used for watching emulated memory.
360 // It's not related to timekeeping devices.
361 menu->addAction(tr("&Delete Watch"), this, [this, row] { DeleteWatchAndUpdate(row); });
362 menu->addAction(tr("&Add Memory Breakpoint"), this,
363 [this, row] { AddWatchBreakpoint(row); });
364 }
365 }
366 }
367 }
368
369 menu->addSeparator();
370
371 menu->addAction(tr("Update"), this, &WatchWidget::Update);
372
373 menu->exec(QCursor::pos());
374 }
375
376 void WatchWidget::OnItemChanged(QTableWidgetItem* item)
377 {
378 if (m_updating || item->data(Qt::UserRole).isNull())
379 return;
380
381 int row = item->data(Qt::UserRole).toInt();
382 int column = item->data(Qt::UserRole + 1).toInt();
383
384 if (row == -1)
385 {
386 if (!item->text().isEmpty())
387 {
388 AddWatch(item->text(), 0);
389
390 Update();
391 return;
392 }
393 }
394 else
395 {
396 switch (column)
397 {
398 case COLUMN_INDEX_LABEL:
399 if (item->text().isEmpty())
400 DeleteWatchAndUpdate(row);
401 else
402 m_system.GetPowerPC().GetDebugInterface().UpdateWatchName(row, item->text().toStdString());
403 break;
404 case COLUMN_INDEX_ADDRESS:
405 case COLUMN_INDEX_HEX:
406 case COLUMN_INDEX_DECIMAL:
407 {
408 bool good;
409 const bool column_uses_hex_formatting =
410 column == COLUMN_INDEX_ADDRESS || column == COLUMN_INDEX_HEX;
411 quint32 value = item->text().toUInt(&good, column_uses_hex_formatting ? 16 : 10);
412
413 if (good)
414 {
415 Core::CPUThreadGuard guard(m_system);
416
417 auto& debug_interface = m_system.GetPowerPC().GetDebugInterface();
418 if (column == COLUMN_INDEX_ADDRESS)
419 {
420 const auto& watch = debug_interface.GetWatch(row);
421 debug_interface.UnsetPatch(guard, watch.address);
422 debug_interface.UpdateWatchAddress(row, value);
423 if (watch.locked)
424 LockWatchAddress(guard, value);
425 }
426 else
427 {
428 PowerPC::MMU::HostWrite<u32>(guard, value, debug_interface.GetWatch(row).address);
429 }
430 }
431 else
432 {
433 ModalMessageBox::critical(this, tr("Error"), tr("Invalid input provided"));
434 }
435 break;
436 }
437 case COLUMN_INDEX_LOCK:
438 {
439 auto& debug_interface = m_system.GetPowerPC().GetDebugInterface();
440 debug_interface.UpdateWatchLockedState(row, item->checkState() == Qt::Checked);
441 const auto& watch = debug_interface.GetWatch(row);
442 Core::CPUThreadGuard guard(m_system);
443 if (watch.locked)
444 LockWatchAddress(guard, watch.address);
445 else
446 debug_interface.UnsetPatch(guard, watch.address);
447 break;
448 }
449 }
450
451 Update();
452 }
453 }
454
455 void WatchWidget::LockWatchAddress(const Core::CPUThreadGuard& guard, u32 address)
456 {
457 const std::string memory_data_as_string = PowerPC::MMU::HostGetString(guard, address, 4);
458
459 std::vector<u8> bytes;
460 for (const char c : memory_data_as_string)
461 {
462 bytes.push_back(static_cast<u8>(c));
463 }
464
465 m_system.GetPowerPC().GetDebugInterface().SetFramePatch(guard, address, bytes);
466 }
467
468 void WatchWidget::DeleteSelectedWatches()
469 {
470 {
471 Core::CPUThreadGuard guard(m_system);
472 std::vector<int> row_indices;
473 for (const auto& index : m_table->selectionModel()->selectedRows())
474 {
475 const auto* item = m_table->item(index.row(), index.column());
476 const auto row_variant = item->data(Qt::UserRole);
477 if (row_variant.isNull())
478 continue;
479
480 row_indices.push_back(row_variant.toInt());
481 }
482
483 // Sort greatest to smallest, so we don't stomp on existing indices
484 std::ranges::sort(row_indices, std::ranges::greater{});
485 for (const int row : row_indices)
486 {
487 DeleteWatch(guard, row);
488 }
489 }
490
491 Update();
492 }
493
494 void WatchWidget::DeleteWatch(const Core::CPUThreadGuard& guard, int row)
495 {
496 auto& debug_interface = m_system.GetPowerPC().GetDebugInterface();
497 debug_interface.UnsetPatch(guard, debug_interface.GetWatch(row).address);
498 debug_interface.RemoveWatch(row);
499 }
500
501 void WatchWidget::DeleteWatchAndUpdate(int row)
502 {
503 {
504 Core::CPUThreadGuard guard(m_system);
505 DeleteWatch(guard, row);
506 }
507
508 Update();
509 }
510
511 void WatchWidget::AddWatchBreakpoint(int row)
512 {
513 emit RequestMemoryBreakpoint(m_system.GetPowerPC().GetDebugInterface().GetWatch(row).address);
514 }
515
516 void WatchWidget::ShowInMemory(int row)
517 {
518 emit ShowMemory(m_system.GetPowerPC().GetDebugInterface().GetWatch(row).address);
519 }
520
521 void WatchWidget::AddWatch(const QString& name, u32 addr)
522 {
523 m_system.GetPowerPC().GetDebugInterface().SetWatch(addr, name.toStdString());
524 Update();
525 }
526
527 void WatchWidget::LockSelectedWatches()
528 {
529 {
530 Core::CPUThreadGuard guard(m_system);
531 auto& debug_interface = m_system.GetPowerPC().GetDebugInterface();
532 for (const auto& index : m_table->selectionModel()->selectedRows())
533 {
534 const auto* item = m_table->item(index.row(), index.column());
535 const auto row_variant = item->data(Qt::UserRole);
536 if (row_variant.isNull())
537 continue;
538 const int row = row_variant.toInt();
539 const auto& watch = debug_interface.GetWatch(row);
540 if (watch.locked)
541 continue;
542 debug_interface.UpdateWatchLockedState(row, true);
543 LockWatchAddress(guard, watch.address);
544 }
545 }
546
547 Update();
548 }
549
550 void WatchWidget::UnlockSelectedWatches()
551 {
552 {
553 auto& debug_interface = m_system.GetPowerPC().GetDebugInterface();
554 Core::CPUThreadGuard guard(m_system);
555 for (const auto& index : m_table->selectionModel()->selectedRows())
556 {
557 const auto* item = m_table->item(index.row(), index.column());
558 const auto row_variant = item->data(Qt::UserRole);
559 if (row_variant.isNull())
560 continue;
561 const int row = row_variant.toInt();
562 const auto& watch = debug_interface.GetWatch(row);
563 if (!watch.locked)
564 continue;
565 debug_interface.UpdateWatchLockedState(row, false);
566 debug_interface.UnsetPatch(guard, watch.address);
567 }
568 }
569
570 Update();
571 }