| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Debugger/BreakpointDialog.h" |
| 5 | |
| 6 | #include <QButtonGroup> |
| 7 | #include <QCheckBox> |
| 8 | #include <QDialogButtonBox> |
| 9 | #include <QGridLayout> |
| 10 | #include <QGroupBox> |
| 11 | #include <QHBoxLayout> |
| 12 | #include <QLabel> |
| 13 | #include <QLineEdit> |
| 14 | #include <QPushButton> |
| 15 | #include <QRadioButton> |
| 16 | #include <QVBoxLayout> |
| 17 | |
| 18 | #include "Core/PowerPC/BreakPoints.h" |
| 19 | #include "Core/PowerPC/Expression.h" |
| 20 | #include "DolphinQt/Debugger/BreakpointWidget.h" |
| 21 | #include "DolphinQt/QtUtils/ModalMessageBox.h" |
| 22 | |
| 23 | BreakpointDialog::BreakpointDialog(BreakpointWidget* parent) |
| 24 | : QDialog(parent), m_parent(parent), m_open_mode(OpenMode::New) |
| 25 | { |
| 26 | setWindowTitle(tr("New Breakpoint")); |
| 27 | CreateWidgets(); |
| 28 | ConnectWidgets(); |
| 29 | |
| 30 | OnBPTypeChanged(); |
| 31 | OnAddressTypeChanged(); |
| 32 | } |
| 33 | |
| 34 | BreakpointDialog::BreakpointDialog(BreakpointWidget* parent, const TBreakPoint* breakpoint) |
| 35 | : QDialog(parent), m_parent(parent), m_open_mode(OpenMode::EditBreakPoint) |
| 36 | { |
| 37 | setWindowTitle(tr("Edit Breakpoint")); |
| 38 | CreateWidgets(); |
| 39 | ConnectWidgets(); |
| 40 | |
| 41 | m_instruction_address->setText(QString::number(breakpoint->address, 16)); |
| 42 | if (breakpoint->condition) |
| 43 | m_conditional->setText(QString::fromStdString(breakpoint->condition->GetText())); |
| 44 | |
| 45 | m_do_break->setChecked(breakpoint->break_on_hit && !breakpoint->log_on_hit); |
| 46 | m_do_log->setChecked(!breakpoint->break_on_hit && breakpoint->log_on_hit); |
| 47 | m_do_log_and_break->setChecked(breakpoint->break_on_hit && breakpoint->log_on_hit); |
| 48 | |
| 49 | OnBPTypeChanged(); |
| 50 | OnAddressTypeChanged(); |
| 51 | } |
| 52 | |
| 53 | BreakpointDialog::BreakpointDialog(BreakpointWidget* parent, const TMemCheck* memcheck) |
| 54 | : QDialog(parent), m_parent(parent), m_open_mode(OpenMode::EditMemCheck) |
| 55 | { |
| 56 | setWindowTitle(tr("Edit Breakpoint")); |
| 57 | |
| 58 | CreateWidgets(); |
| 59 | ConnectWidgets(); |
| 60 | |
| 61 | m_memory_address_from->setText(QString::number(memcheck->start_address, 16)); |
| 62 | if (memcheck->is_ranged) |
| 63 | { |
| 64 | m_memory_use_address->setChecked(false); |
| 65 | m_memory_use_range->setChecked(true); |
| 66 | m_memory_address_to->setText(QString::number(memcheck->end_address, 16)); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | m_memory_address_to->setText(QString::number(memcheck->start_address + 1, 16)); |
| 71 | } |
| 72 | if (memcheck->condition) |
| 73 | m_conditional->setText(QString::fromStdString(memcheck->condition->GetText())); |
| 74 | |
| 75 | m_memory_on_read->setChecked(memcheck->is_break_on_read && !memcheck->is_break_on_write); |
| 76 | m_memory_on_write->setChecked(!memcheck->is_break_on_read && memcheck->is_break_on_write); |
| 77 | m_memory_on_read_and_write->setChecked(memcheck->is_break_on_read && memcheck->is_break_on_write); |
| 78 | |
| 79 | m_do_break->setChecked(memcheck->break_on_hit && !memcheck->log_on_hit); |
| 80 | m_do_log->setChecked(!memcheck->break_on_hit && memcheck->log_on_hit); |
| 81 | m_do_log_and_break->setChecked(memcheck->break_on_hit && memcheck->log_on_hit); |
| 82 | |
| 83 | OnBPTypeChanged(); |
| 84 | OnAddressTypeChanged(); |
| 85 | } |
| 86 | |
| 87 | void BreakpointDialog::CreateWidgets() |
| 88 | { |
| 89 | m_buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | |
| 90 | QDialogButtonBox::Help); |
| 91 | auto* type_group = new QButtonGroup(this); |
| 92 | |
| 93 | // Instruction BP |
| 94 | auto* instruction_widget = new QWidget; |
| 95 | auto* instruction_layout = new QGridLayout; |
| 96 | |
| 97 | m_instruction_bp = new QRadioButton(tr("Instruction Breakpoint")); |
| 98 | type_group->addButton(m_instruction_bp); |
| 99 | m_instruction_box = new QGroupBox; |
| 100 | m_instruction_address = new QLineEdit; |
| 101 | |
| 102 | auto* instruction_data_layout = new QHBoxLayout; |
| 103 | m_instruction_box->setLayout(instruction_data_layout); |
| 104 | instruction_data_layout->addWidget(new QLabel(tr("Address:"))); |
| 105 | instruction_data_layout->addWidget(m_instruction_address); |
| 106 | |
| 107 | instruction_layout->addWidget(m_instruction_bp, 0, 0, 1, 1); |
| 108 | instruction_layout->addWidget(m_instruction_box, 1, 0, 1, 2); |
| 109 | instruction_widget->setLayout(instruction_layout); |
| 110 | |
| 111 | // Memory BP |
| 112 | auto* memory_widget = new QWidget; |
| 113 | auto* memory_layout = new QGridLayout; |
| 114 | |
| 115 | m_memory_bp = new QRadioButton(tr("Memory Breakpoint")); |
| 116 | type_group->addButton(m_memory_bp); |
| 117 | m_memory_box = new QGroupBox; |
| 118 | auto* memory_type_group = new QButtonGroup(this); |
| 119 | m_memory_use_address = new QRadioButton(tr("Address")); |
| 120 | m_memory_use_address->setChecked(true); |
| 121 | memory_type_group->addButton(m_memory_use_address); |
| 122 | // i18n: A range of memory addresses |
| 123 | m_memory_use_range = new QRadioButton(tr("Range")); |
| 124 | memory_type_group->addButton(m_memory_use_range); |
| 125 | m_memory_address_from = new QLineEdit; |
| 126 | m_memory_address_to = new QLineEdit; |
| 127 | m_memory_address_from_label = new QLabel; // Set by OnAddressTypeChanged |
| 128 | m_memory_address_to_label = new QLabel(tr("To:")); |
| 129 | // i18n: This is a selectable condition when adding a breakpoint |
| 130 | m_memory_on_read = new QRadioButton(tr("Read")); |
| 131 | // i18n: This is a selectable condition when adding a breakpoint |
| 132 | m_memory_on_write = new QRadioButton(tr("Write")); |
| 133 | // i18n: This is a selectable condition when adding a breakpoint |
| 134 | m_memory_on_read_and_write = new QRadioButton(tr("Read or Write")); |
| 135 | m_memory_on_write->setChecked(true); |
| 136 | // i18n: This is a selectable action when adding a breakpoint |
| 137 | m_do_log = new QRadioButton(tr("Write to Log")); |
| 138 | // i18n: This is a selectable action when adding a breakpoint |
| 139 | m_do_break = new QRadioButton(tr("Break")); |
| 140 | // i18n: This is a selectable action when adding a breakpoint |
| 141 | m_do_log_and_break = new QRadioButton(tr("Write to Log and Break")); |
| 142 | m_do_log_and_break->setChecked(true); |
| 143 | |
| 144 | auto* memory_data_layout = new QGridLayout; |
| 145 | m_memory_box->setLayout(memory_data_layout); |
| 146 | memory_data_layout->addWidget(m_memory_use_address, 0, 0); |
| 147 | memory_data_layout->addWidget(m_memory_use_range, 0, 3); |
| 148 | memory_data_layout->addWidget(m_memory_address_from_label, 1, 0); |
| 149 | memory_data_layout->addWidget(m_memory_address_from, 1, 1); |
| 150 | memory_data_layout->addWidget(m_memory_address_to_label, 1, 2); |
| 151 | memory_data_layout->addWidget(m_memory_address_to, 1, 3); |
| 152 | |
| 153 | // i18n: If a condition is set for a breakpoint, the condition becoming true is a prerequisite for |
| 154 | // triggering the breakpoint. |
| 155 | QGroupBox* condition_box = new QGroupBox(tr("Condition")); |
| 156 | auto* condition_layout = new QHBoxLayout; |
| 157 | condition_box->setLayout(condition_layout); |
| 158 | |
| 159 | memory_data_layout->addWidget(condition_box, 2, 0, 1, -1); |
| 160 | condition_layout->addWidget(m_memory_on_read); |
| 161 | condition_layout->addWidget(m_memory_on_write); |
| 162 | condition_layout->addWidget(m_memory_on_read_and_write); |
| 163 | |
| 164 | memory_layout->addWidget(m_memory_bp, 0, 0); |
| 165 | memory_layout->addWidget(m_memory_box, 1, 0); |
| 166 | memory_widget->setLayout(memory_layout); |
| 167 | |
| 168 | QGroupBox* action_box = new QGroupBox(tr("Action")); |
| 169 | |
| 170 | QHBoxLayout* conditional_layout = new QHBoxLayout; |
| 171 | m_conditional = new QLineEdit(); |
| 172 | // i18n: If a condition is set for a breakpoint, the condition becoming true is a prerequisite for |
| 173 | // triggering the breakpoint. |
| 174 | conditional_layout->addWidget(new QLabel(tr("Condition:"))); |
| 175 | conditional_layout->addWidget(m_conditional); |
| 176 | |
| 177 | auto* action_layout = new QHBoxLayout; |
| 178 | action_layout->addWidget(m_do_log); |
| 179 | action_layout->addWidget(m_do_break); |
| 180 | action_layout->addWidget(m_do_log_and_break); |
| 181 | |
| 182 | auto* action_vlayout = new QVBoxLayout; |
| 183 | action_vlayout->addLayout(conditional_layout); |
| 184 | action_vlayout->addLayout(action_layout); |
| 185 | |
| 186 | action_box->setLayout(action_vlayout); |
| 187 | |
| 188 | auto* layout = new QVBoxLayout; |
| 189 | layout->addWidget(instruction_widget); |
| 190 | layout->addWidget(memory_widget); |
| 191 | layout->addWidget(action_box); |
| 192 | layout->addWidget(m_buttons); |
| 193 | |
| 194 | switch (m_open_mode) |
| 195 | { |
| 196 | case OpenMode::New: |
| 197 | m_instruction_bp->setChecked(true); |
| 198 | m_instruction_address->setFocus(); |
| 199 | break; |
| 200 | case OpenMode::EditBreakPoint: |
| 201 | memory_widget->setVisible(false); |
| 202 | m_instruction_bp->setChecked(true); |
| 203 | m_instruction_address->setEnabled(false); |
| 204 | m_instruction_address->setFocus(); |
| 205 | break; |
| 206 | case OpenMode::EditMemCheck: |
| 207 | instruction_widget->setVisible(false); |
| 208 | m_memory_bp->setChecked(true); |
| 209 | m_memory_address_from->setEnabled(false); |
| 210 | m_memory_address_to->setFocus(); |
| 211 | break; |
| 212 | } |
| 213 | |
| 214 | setLayout(layout); |
| 215 | } |
| 216 | |
| 217 | void BreakpointDialog::ConnectWidgets() |
| 218 | { |
| 219 | connect(m_buttons, &QDialogButtonBox::accepted, this, &BreakpointDialog::accept); |
| 220 | connect(m_buttons, &QDialogButtonBox::rejected, this, &BreakpointDialog::reject); |
| 221 | connect(m_buttons, &QDialogButtonBox::helpRequested, this, &BreakpointDialog::ShowConditionHelp); |
| 222 | |
| 223 | connect(m_instruction_bp, &QRadioButton::toggled, this, &BreakpointDialog::OnBPTypeChanged); |
| 224 | connect(m_memory_bp, &QRadioButton::toggled, this, &BreakpointDialog::OnBPTypeChanged); |
| 225 | |
| 226 | connect(m_memory_use_address, &QRadioButton::toggled, this, |
| 227 | &BreakpointDialog::OnAddressTypeChanged); |
| 228 | connect(m_memory_use_range, &QRadioButton::toggled, this, |
| 229 | &BreakpointDialog::OnAddressTypeChanged); |
| 230 | } |
| 231 | |
| 232 | void BreakpointDialog::OnBPTypeChanged() |
| 233 | { |
| 234 | m_instruction_box->setEnabled(m_instruction_bp->isChecked()); |
| 235 | m_memory_box->setEnabled(m_memory_bp->isChecked()); |
| 236 | } |
| 237 | |
| 238 | void BreakpointDialog::OnAddressTypeChanged() |
| 239 | { |
| 240 | bool ranged = m_memory_use_range->isChecked(); |
| 241 | |
| 242 | m_memory_address_to->setHidden(!ranged); |
| 243 | m_memory_address_to_label->setHidden(!ranged); |
| 244 | |
| 245 | m_memory_address_from_label->setText(ranged ? tr("From:") : tr("Address:")); |
| 246 | } |
| 247 | |
| 248 | void BreakpointDialog::accept() |
| 249 | { |
| 250 | auto invalid_input = [this](const QString& field) { |
| 251 | ModalMessageBox::critical(this, tr("Error"), |
| 252 | tr("Invalid input for the field \"%1\"").arg(field)); |
| 253 | }; |
| 254 | |
| 255 | bool instruction = m_instruction_bp->isChecked(); |
| 256 | bool ranged = m_memory_use_range->isChecked(); |
| 257 | |
| 258 | // Triggers |
| 259 | bool on_read = m_memory_on_read->isChecked() || m_memory_on_read_and_write->isChecked(); |
| 260 | bool on_write = m_memory_on_write->isChecked() || m_memory_on_read_and_write->isChecked(); |
| 261 | |
| 262 | // Actions |
| 263 | bool do_log = m_do_log->isChecked() || m_do_log_and_break->isChecked(); |
| 264 | bool do_break = m_do_break->isChecked() || m_do_log_and_break->isChecked(); |
| 265 | |
| 266 | bool good; |
| 267 | |
| 268 | const QString condition = m_conditional->text().trimmed(); |
| 269 | |
| 270 | if (!condition.isEmpty() && !Expression::TryParse(condition.toUtf8().constData())) |
| 271 | { |
| 272 | // i18n: If a condition is set for a breakpoint, the condition becoming true is a prerequisite |
| 273 | // for triggering the breakpoint. |
| 274 | invalid_input(tr("Condition")); |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | if (instruction) |
| 279 | { |
| 280 | u32 address = m_instruction_address->text().toUInt(&good, 16); |
| 281 | |
| 282 | if (!good) |
| 283 | { |
| 284 | invalid_input(tr("Address")); |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | m_parent->AddBP(address, do_break, do_log, condition); |
| 289 | } |
| 290 | else |
| 291 | { |
| 292 | u32 from = m_memory_address_from->text().toUInt(&good, 16); |
| 293 | |
| 294 | if (!good) |
| 295 | { |
| 296 | invalid_input(ranged ? tr("From") : tr("Address")); |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | if (ranged) |
| 301 | { |
| 302 | u32 to = m_memory_address_to->text().toUInt(&good, 16); |
| 303 | if (!good) |
| 304 | { |
| 305 | invalid_input(tr("To")); |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | m_parent->AddRangedMBP(from, to, on_read, on_write, do_log, do_break, condition); |
| 310 | } |
| 311 | else |
| 312 | { |
| 313 | m_parent->AddAddressMBP(from, on_read, on_write, do_log, do_break, condition); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | QDialog::accept(); |
| 318 | } |
| 319 | |
| 320 | void BreakpointDialog::ShowConditionHelp() |
| 321 | { |
| 322 | const auto message = tr( |
| 323 | "Conditions:\n" |
| 324 | "Sets an expression that is evaluated when a breakpoint is hit. If the expression is false " |
| 325 | "or 0, the breakpoint is ignored until hit again. Statements should be separated by a comma. " |
| 326 | "Only the last statement will be used to determine what to do.\n" |
| 327 | "\n" |
| 328 | "Registers that can be referenced:\n" |
| 329 | "GPRs : r0..r31\n" |
| 330 | "FPRs : f0..f31\n" |
| 331 | "SPRs : xer, lr, ctr, dsisr, dar, dec, sdr1, srr0, srr1, tbl, tbu, pvr, sprg0..sprg3, ear, " |
| 332 | "ibat0u..ibat7u, ibat0l..ibat7l, dbat0u..dbat7u, dbat0l..dbat07, gqr0..gqr7, hid0, hid1, " |
| 333 | "hid2, hid4, iabr, dabr, wpar, dmau, dmal, ecid_u, ecid_m, ecid_l, upmc1..upmc4, usia, sia, " |
| 334 | "l2cr, ictc, mmcr0, mmcr1, pmc1..pmc4, thrm1..thrm3\n" |
| 335 | "Other : pc, msr\n" |
| 336 | "\n" |
| 337 | "Functions:\n" |
| 338 | "Set a register: r1 = 8\n" |
| 339 | "Casts: s8(0xff). Available: s8, u8, s16, u16, s32, u32\n" |
| 340 | "Callstack: callstack(0x80123456), callstack(\"anim\")\n" |
| 341 | "Compare Strings: streq(r3, \"abc\"). Both parameters can be addresses or string constants.\n" |
| 342 | "Read Memory: read_u32(0x80000000). Available: u8, s8, u16, s16, u32, s32, f32, f64\n" |
| 343 | "Write Memory: write_u32(r3, 0x80000000). Available: u8, u16, u32, f32, f64\n" |
| 344 | "*currently writing will always be triggered\n" |
| 345 | "\n" |
| 346 | "Operations:\n" |
| 347 | "Unary: -u, !u, ~u\n" |
| 348 | "Math: * / + -, power: **, remainder: %, shift: <<, >>\n" |
| 349 | "Compare: <, <=, >, >=, ==, !=, &&, ||\n" |
| 350 | "Bitwise: &, |, ^\n" |
| 351 | "\n" |
| 352 | "Examples:\n" |
| 353 | "r4 == 1\n" |
| 354 | "f0 == 1.0 && f2 < 10.0\n" |
| 355 | "r26 <= r0 && ((r5 + 3) & -4) * ((r6 + 3) & -4)* 4 > r0\n" |
| 356 | "p = r3 + 0x8, p == 0x8003510 && read_u32(p) != 0\n" |
| 357 | "Write and break: r4 = 8, 1\n" |
| 358 | "Write and continue: f3 = f1 + f2, 0\n" |
| 359 | "The condition must always be last\n\n" |
| 360 | "Strings should only be used in callstack() or streq() and \"quoted\". Do not assign strings " |
| 361 | "to a variable.\n" |
| 362 | "All variables will be printed in the Memory Interface log, if there's a hit or a NaN " |
| 363 | "result. To check for issues, assign a variable to your equation, so it can be printed.\n\n" |
| 364 | "Note: All values are internally converted to Doubles for calculations. It's possible for " |
| 365 | "them to go out of range or to become NaN. A warning will be given if NaN is returned, and " |
| 366 | "the var that became NaN will be logged."); |
| 367 | // i18n: The title for a dialog that shows help for how to use conditions. If a condition is set |
| 368 | // for a breakpoint, the condition becoming true is a prerequisite for triggering the breakpoint. |
| 369 | ModalMessageBox::information(this, tr("Conditional help"), message); |
| 370 | } |