| 1 | #include "BreakpointViewer.h" |
| 2 | #include "Convert.h" |
| 3 | #include "CommClient.h" |
| 4 | #include "DebugSession.h" |
| 5 | #include "OpenMSXConnection.h" |
| 6 | #include "Settings.h" |
| 7 | #include "ScopedAssign.h" |
| 8 | #include "ranges.h" |
| 9 | #include <QPainter> |
| 10 | #include <QPaintEvent> |
| 11 | #include <QMessageBox> |
| 12 | #include <QToolTip> |
| 13 | #include <QComboBox> |
| 14 | #include <cassert> |
| 15 | |
| 16 | static constexpr int UNDEFINED_ROW = -1; |
| 17 | |
| 18 | enum TableColumns { |
| 19 | ENABLED = 0, |
| 20 | WP_TYPE = 1, |
| 21 | LOCATION = 2, |
| 22 | T_CONDITION = 3, |
| 23 | SLOT = 4, |
| 24 | SEGMENT = 5, |
| 25 | BREAKPOINT_ID = 6, |
| 26 | ADDRESS = 7, |
| 27 | }; |
| 28 | |
| 29 | static QString locationString(const AddressRange& range, int adrLen) |
| 30 | { |
| 31 | return QString("%1%2%3") |
| 32 | .arg(hexValue(range.start, adrLen)) |
| 33 | .arg(range.end ? ":" : "") |
| 34 | .arg(range.end ? hexValue(*range.end, adrLen) : ""); |
| 35 | } |
| 36 | |
| 37 | BreakpointViewer::BreakpointViewer(DebugSession& session, QWidget* parent) |
| 38 | : QTabWidget(parent), |
| 39 | ui(new Ui::BreakpointViewer), |
| 40 | debugSession(session) |
| 41 | { |
| 42 | setupUi(this); |
| 43 | |
| 44 | connect(btnAddBp, &QPushButton::clicked, this, &BreakpointViewer::on_btnAddBp_clicked); |
| 45 | connect(btnRemoveBp, &QPushButton::clicked, this, &BreakpointViewer::on_btnRemoveBp_clicked); |
| 46 | connect(btnAddWp, &QPushButton::clicked, this, &BreakpointViewer::on_btnAddWp_clicked); |
| 47 | connect(btnRemoveWp, &QPushButton::clicked, this, &BreakpointViewer::on_btnRemoveWp_clicked); |
| 48 | connect(btnAddCn, &QPushButton::clicked, this, &BreakpointViewer::on_btnAddCn_clicked); |
| 49 | connect(btnRemoveCn, &QPushButton::clicked, this, &BreakpointViewer::on_btnRemoveCn_clicked); |
| 50 | |
| 51 | bpTableWidget->horizontalHeader()->setHighlightSections(false); |
| 52 | bpTableWidget->sortByColumn(LOCATION, Qt::AscendingOrder); |
| 53 | bpTableWidget->setColumnHidden(WP_TYPE, true); |
| 54 | bpTableWidget->setColumnHidden(BREAKPOINT_ID, true); |
| 55 | bpTableWidget->setColumnHidden(ADDRESS, true); |
| 56 | bpTableWidget->resizeColumnsToContents(); |
| 57 | bpTableWidget->setSortingEnabled(true); |
| 58 | connect(bpTableWidget, &QTableWidget::itemPressed, this, &BreakpointViewer::on_itemPressed); |
| 59 | connect(bpTableWidget, &QTableWidget::itemChanged, this, &BreakpointViewer::changeBpTableItem); |
| 60 | connect(bpTableWidget->horizontalHeader(), &QHeaderView::sectionClicked, this, |
| 61 | &BreakpointViewer::on_headerClicked); |
| 62 | |
| 63 | wpTableWidget->horizontalHeader()->setHighlightSections(false); |
| 64 | wpTableWidget->setColumnHidden(BREAKPOINT_ID, true); |
| 65 | wpTableWidget->setColumnHidden(ADDRESS, true); |
| 66 | wpTableWidget->sortByColumn(LOCATION, Qt::AscendingOrder); |
| 67 | wpTableWidget->resizeColumnsToContents(); |
| 68 | wpTableWidget->setSortingEnabled(true); |
| 69 | connect(wpTableWidget, &QTableWidget::itemChanged, this, |
| 70 | &BreakpointViewer::changeWpTableItem); |
| 71 | |
| 72 | cnTableWidget->horizontalHeader()->setHighlightSections(false); |
| 73 | cnTableWidget->setColumnHidden(WP_TYPE, true); |
| 74 | cnTableWidget->setColumnHidden(BREAKPOINT_ID, true); |
| 75 | cnTableWidget->setColumnHidden(LOCATION, true); |
| 76 | cnTableWidget->setColumnHidden(SLOT, true); |
| 77 | cnTableWidget->setColumnHidden(SEGMENT, true); |
| 78 | cnTableWidget->setColumnHidden(ADDRESS, true); |
| 79 | cnTableWidget->sortByColumn(T_CONDITION, Qt::AscendingOrder); |
| 80 | cnTableWidget->resizeColumnsToContents(); |
| 81 | cnTableWidget->setSortingEnabled(true); |
| 82 | connect(cnTableWidget, &QTableWidget::itemChanged, this, |
| 83 | &BreakpointViewer::changeCnTableItem); |
| 84 | |
| 85 | tables[BreakpointType::BREAKPOINT] = bpTableWidget; |
| 86 | tables[BreakpointType::WATCHPOINT] = wpTableWidget; |
| 87 | tables[BreakpointType::CONDITION] = cnTableWidget; |
| 88 | } |
| 89 | |
| 90 | // TODO: move the createSetCommand to a session manager |
| 91 | void BreakpointViewer::createBreakpoint(BreakpointType type, int row) |
| 92 | { |
| 93 | if (type == BreakpointType::CONDITION) _createCondition(row); |
| 94 | else _createBreakpoint(type, row); |
| 95 | } |
| 96 | |
| 97 | void BreakpointViewer::_handleSyncError(const QString& error) |
| 98 | { |
| 99 | int choice = QMessageBox::warning(nullptr, tr("Synchronization error"), |
| 100 | tr("Error message from openMSX:\n\"%1\"\n\nClick on Reset to resynchronize with openMSX") |
| 101 | .arg(error.trimmed()), QMessageBox::Reset | QMessageBox::Cancel); |
| 102 | if (choice == QMessageBox::Reset) { |
| 103 | emit contentsUpdated(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void BreakpointViewer::_handleKeyAlreadyExists() |
| 108 | { |
| 109 | int choice = QMessageBox::warning(nullptr, tr("Reference error"), |
| 110 | tr("A breakpoint with the same name already exists locally. This is probably " |
| 111 | "a synchronization problem.\n\nClick on Reset to resynchronize with openMSX"), |
| 112 | QMessageBox::Reset | QMessageBox::Cancel); |
| 113 | if (choice == QMessageBox::Reset) { |
| 114 | emit contentsUpdated(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void BreakpointViewer::_handleKeyNotFound() |
| 119 | { |
| 120 | int choice = QMessageBox::warning(nullptr, tr("Reference error"), |
| 121 | tr("Breakpoint was not found locally. This is probably a synchronization problem.\n\n" |
| 122 | "Click on Reset to resynchronize with openMSX"), |
| 123 | QMessageBox::Reset | QMessageBox::Cancel); |
| 124 | if (choice == QMessageBox::Reset) { |
| 125 | emit contentsUpdated(); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void BreakpointViewer::_createBreakpoint(BreakpointType type, int row) |
| 130 | { |
| 131 | assert(type != BreakpointType::CONDITION); |
| 132 | auto* table = tables[type]; |
| 133 | auto* model = table->model(); |
| 134 | auto* combo = (QComboBox*) table->indexWidget(model->index(row, WP_TYPE)); |
| 135 | Breakpoint::Type wtype = type == BreakpointType::WATCHPOINT ? readComboBox(row) : Breakpoint::BREAKPOINT; |
| 136 | |
| 137 | QString location = table->item(row, LOCATION)->text(); |
| 138 | auto range = parseLocationField({}, type, location, combo ? combo->currentText() : ""); |
| 139 | setBreakpointChecked(type, row, range ? Qt::Checked : Qt::Unchecked); |
| 140 | if (!range) return; |
| 141 | |
| 142 | QString condition = table->item(row, T_CONDITION)->text(); |
| 143 | auto slot = parseSlotField({}, table->item(row, SLOT)->text()); |
| 144 | auto segment = parseSegmentField({}, table->item(row, SEGMENT)->text()); |
| 145 | const QString cmdStr = Breakpoints::createSetCommand(wtype, range, slot, segment, condition); |
| 146 | |
| 147 | auto* command = new Command(cmdStr, |
| 148 | [=] (const QString& id) { |
| 149 | // update breakpoint id |
| 150 | setTextField(type, row, BREAKPOINT_ID, id); |
| 151 | |
| 152 | disableRefresh = false; |
| 153 | emit contentsUpdated(); |
| 154 | }, |
| 155 | [=] (const QString& error) { |
| 156 | disableRefresh = false; |
| 157 | _handleSyncError(error); |
| 158 | } |
| 159 | ); |
| 160 | |
| 161 | CommClient::instance().sendCommand(command); |
| 162 | } |
| 163 | |
| 164 | // TODO: move the createRemoveCommand to a session manager |
| 165 | void BreakpointViewer::replaceBreakpoint(BreakpointType type, int row) |
| 166 | { |
| 167 | auto* table = tables[type]; |
| 168 | auto* item = table->item(row, BREAKPOINT_ID); |
| 169 | QString id = item->text(); |
| 170 | // remove and create breakpoint without calling refresh in between. |
| 171 | disableRefresh = true; |
| 172 | |
| 173 | // replacing is the same as removing an old breakpoint and then create a new one |
| 174 | const QString cmdStr = breakpoints->createRemoveCommand(id); |
| 175 | auto* command = new Command(cmdStr, |
| 176 | [this, type, row] (const QString& /*result*/) { |
| 177 | createBreakpoint(type, row); |
| 178 | }, |
| 179 | [this](const QString& error) { |
| 180 | // restore default behaviour on error |
| 181 | disableRefresh = false; |
| 182 | _handleSyncError(error); |
| 183 | } |
| 184 | ); |
| 185 | |
| 186 | CommClient::instance().sendCommand(command); |
| 187 | } |
| 188 | |
| 189 | // TODO: move the createRemoveCommand to a session manager |
| 190 | void BreakpointViewer::removeBreakpoint(BreakpointType type, int row, bool removeLocal) |
| 191 | { |
| 192 | auto* table = tables[type]; |
| 193 | |
| 194 | auto* item = table->item(row, BREAKPOINT_ID); |
| 195 | assert(!item->text().isEmpty()); |
| 196 | QString id = item->text(); |
| 197 | const QString cmdStr = Breakpoints::createRemoveCommand(id); |
| 198 | |
| 199 | auto* command = new Command(cmdStr, |
| 200 | [=] (const QString& /*result*/) { |
| 201 | if (removeLocal) { |
| 202 | auto* table = tables[type]; |
| 203 | auto sa = ScopedAssign(userMode, false); |
| 204 | table->removeRow(row); |
| 205 | } |
| 206 | emit contentsUpdated(); |
| 207 | }, |
| 208 | [this] (const QString& error) { _handleSyncError(error); } |
| 209 | ); |
| 210 | |
| 211 | CommClient::instance().sendCommand(command); |
| 212 | } |
| 213 | |
| 214 | // TODO: move the createSetCommand to a session manager |
| 215 | void BreakpointViewer::_createCondition(int row) |
| 216 | { |
| 217 | BreakpointType type = BreakpointType::CONDITION; |
| 218 | QString condition = cnTableWidget->item(row, T_CONDITION)->text(); |
| 219 | if (condition.isEmpty()) { |
| 220 | setBreakpointChecked(BreakpointType::CONDITION, row, Qt::Unchecked); |
| 221 | return; |
| 222 | } else { |
| 223 | setBreakpointChecked(BreakpointType::CONDITION, row, Qt::Checked); |
| 224 | } |
| 225 | const QString cmdStr = Breakpoints::createSetCommand(Breakpoint::CONDITION, {}, {}, {}, condition); |
| 226 | |
| 227 | auto* command = new Command(cmdStr, |
| 228 | [=] (const QString& id) { |
| 229 | setBreakpointChecked(type, row, Qt::Checked); |
| 230 | |
| 231 | // update breakpoint id |
| 232 | setTextField(type, row, BREAKPOINT_ID, id); |
| 233 | // restore default behaviour if replacing a Breakpoint |
| 234 | disableRefresh = false; |
| 235 | }, |
| 236 | [this] (const QString& error) { _handleSyncError(error); } |
| 237 | ); |
| 238 | |
| 239 | CommClient::instance().sendCommand(command); |
| 240 | } |
| 241 | |
| 242 | void BreakpointViewer::setBreakpointChecked(BreakpointType type, int row, Qt::CheckState state) |
| 243 | { |
| 244 | auto sa = ScopedAssign(userMode, false); |
| 245 | |
| 246 | auto* table = tables[type]; |
| 247 | bool oldValue = table->isSortingEnabled(); |
| 248 | auto* item = table->item(row, ENABLED); |
| 249 | assert(item); |
| 250 | |
| 251 | table->setSortingEnabled(false); |
| 252 | item->setCheckState(state); |
| 253 | table->setSortingEnabled(oldValue); |
| 254 | } |
| 255 | |
| 256 | void BreakpointViewer::setTextField(BreakpointType type, int row, int column, const QString& value, const QString& tooltip) |
| 257 | { |
| 258 | auto sa = ScopedAssign(userMode, false); |
| 259 | |
| 260 | auto* table = tables[type]; |
| 261 | bool oldValue = table->isSortingEnabled(); |
| 262 | auto* item = table->item(row, column); |
| 263 | assert(item); |
| 264 | |
| 265 | table->setSortingEnabled(false); |
| 266 | item->setText(value); |
| 267 | item->setToolTip(tooltip); |
| 268 | table->setSortingEnabled(oldValue); |
| 269 | } |
| 270 | |
| 271 | Slot BreakpointViewer::parseSlotField(std::optional<int> index, const QString& field) |
| 272 | { |
| 273 | QStringList s = field.simplified().split("/", Qt::SplitBehaviorFlags::SkipEmptyParts); |
| 274 | |
| 275 | std::optional<int8_t> ps; |
| 276 | if (s.size() > 0 && s[0].compare("X", Qt::CaseInsensitive)) { |
| 277 | ps = stringToValue<int8_t>(s[0]); |
| 278 | if (!ps || !(0 <= *ps && *ps <= 3)) { |
| 279 | if (!s[0].isEmpty()) { |
| 280 | return {}; |
| 281 | } |
| 282 | // restore value |
| 283 | ps = index ? breakpoints->getBreakpoint(*index).slot.ps : std::nullopt; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | std::optional<int8_t> ss; |
| 288 | if (s.size() == 2 && s[1].compare("X", Qt::CaseInsensitive)) { |
| 289 | ss = stringToValue<int8_t>(s[1]); |
| 290 | if (!ss || !(0 <= *ss && *ss <= 3)) { |
| 291 | if (!s[1].isEmpty()) { |
| 292 | return {}; |
| 293 | } |
| 294 | // restore value |
| 295 | ss = index ? breakpoints->getBreakpoint(*index).slot.ss : std::nullopt; |
| 296 | } |
| 297 | } |
| 298 | return Slot{ps, ss}; |
| 299 | } |
| 300 | |
| 301 | std::optional<uint8_t> BreakpointViewer::parseSegmentField(std::optional<int> index, const QString& field) |
| 302 | { |
| 303 | QString s = field.simplified(); |
| 304 | |
| 305 | if (s.compare("X", Qt::CaseInsensitive)) { |
| 306 | auto value = stringToValue<uint8_t>(s); |
| 307 | return value ? value |
| 308 | : (index ? breakpoints->getBreakpoint(*index).segment : std::nullopt); |
| 309 | } |
| 310 | return {}; |
| 311 | } |
| 312 | |
| 313 | std::optional<AddressRange> parseAddressRange(const QString& location) |
| 314 | { |
| 315 | auto address = stringToValue<uint16_t>(location); |
| 316 | if (address) return AddressRange{*address}; |
| 317 | return {}; |
| 318 | } |
| 319 | |
| 320 | std::optional<AddressRange> BreakpointViewer::parseSymbolOrValue(const QString& field) const |
| 321 | { |
| 322 | if (Symbol* s = debugSession.symbolTable().getAddressSymbol(field)) { |
| 323 | return AddressRange{uint16_t(s->value())}; |
| 324 | } |
| 325 | return parseAddressRange(field); |
| 326 | } |
| 327 | |
| 328 | QString BreakpointViewer::findSymbolOrValue(uint16_t address) const |
| 329 | { |
| 330 | if (Symbol* s = debugSession.symbolTable().getAddressSymbol(address)) { |
| 331 | return s->text(); |
| 332 | } |
| 333 | return hexValue(address, 4); |
| 334 | } |
| 335 | |
| 336 | static const char* ComboTypeNames[] = { "read_mem", "write_mem", "read_io", "write_io" }; |
| 337 | |
| 338 | std::optional<AddressRange> BreakpointViewer::parseLocationField( |
| 339 | std::optional<int> bpIndex, BreakpointType type, const QString& field, const QString& comboTxt) |
| 340 | { |
| 341 | if (type == BreakpointType::BREAKPOINT) { |
| 342 | auto value = parseSymbolOrValue(field); |
| 343 | return value ? AddressRange{*value} |
| 344 | : (bpIndex ? breakpoints->getBreakpoint(*bpIndex).range : std::optional<AddressRange>()); |
| 345 | } |
| 346 | |
| 347 | QStringList s = field.simplified().split(":", Qt::SplitBehaviorFlags::SkipEmptyParts); |
| 348 | auto begin = s.size() >= 1 ? stringToValue<uint16_t>(s[0]) : std::nullopt; |
| 349 | auto end = s.size() == 2 ? stringToValue<uint16_t>(s[1]) : std::nullopt; |
| 350 | auto it = ranges::find(ComboTypeNames, comboTxt); |
| 351 | auto wType = static_cast<Breakpoint::Type>(std::distance(ComboTypeNames, it) + 1); |
| 352 | if ((wType == Breakpoint::WATCHPOINT_IOREAD || wType == Breakpoint::WATCHPOINT_IOWRITE) |
| 353 | && ((begin && *begin > 0xFF) || (end && *end > 0xFF))) { |
| 354 | return {}; |
| 355 | } |
| 356 | if (begin) { |
| 357 | if (end && *end > *begin) { |
| 358 | return AddressRange{*begin, end}; |
| 359 | } |
| 360 | if (!end || *end == *begin) { |
| 361 | return AddressRange{*begin}; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | if (!bpIndex) return {}; |
| 366 | return breakpoints->getBreakpoint(*bpIndex).range; |
| 367 | } |
| 368 | |
| 369 | void BreakpointViewer::changeTableItem(BreakpointType type, QTableWidgetItem* item) |
| 370 | { |
| 371 | if (!userMode) return; |
| 372 | auto* table = tables[type]; |
| 373 | int row = table->row(item); |
| 374 | |
| 375 | // trying to modify a bp instead of creating a new one |
| 376 | bool createBp = false; |
| 377 | auto bpIndex = findBreakpointIndex(type, row); |
| 378 | |
| 379 | // check if breakpoint is enabled |
| 380 | auto* enabledItem = table->item(row, ENABLED); |
| 381 | bool enabled = enabledItem->checkState() == Qt::Checked; |
| 382 | |
| 383 | switch (table->column(item)) { |
| 384 | case ENABLED: |
| 385 | createBp = enabled; |
| 386 | break; |
| 387 | case WP_TYPE: |
| 388 | assert(table == wpTableWidget); |
| 389 | item = table->item(row, LOCATION); |
| 390 | [[fallthrough]]; |
| 391 | case LOCATION: { |
| 392 | auto* model = table->model(); |
| 393 | auto* combo = (QComboBox*) table->indexWidget(model->index(row, WP_TYPE)); |
| 394 | int adrLen; |
| 395 | |
| 396 | if (type == BreakpointType::CONDITION) { |
| 397 | return; |
| 398 | } else if (type == BreakpointType::WATCHPOINT) { |
| 399 | auto wType = static_cast<Breakpoint::Type>(combo->currentIndex() + 1); |
| 400 | adrLen = (wType == Breakpoint::WATCHPOINT_IOREAD || wType == Breakpoint::WATCHPOINT_IOWRITE) |
| 401 | ? 2 : 4; |
| 402 | } else { |
| 403 | adrLen = 4; |
| 404 | } |
| 405 | |
| 406 | if (auto range = parseLocationField(bpIndex, type, item->text(), combo ? combo->currentText() : "")) { |
| 407 | auto rangeStr = locationString(*range, adrLen); |
| 408 | // Use a symbolic address in the location field if available |
| 409 | QString location = debugSession.symbolTable().getAddressSymbol(item->text()) ? item->text() : rangeStr; |
| 410 | setTextField(type, row, LOCATION, location, location != rangeStr ? rangeStr : ""); |
| 411 | setTextField(type, row, ADDRESS, QString("%1").arg(range->start)); |
| 412 | } else { |
| 413 | enabled = false; |
| 414 | setTextField(type, row, LOCATION, ""); |
| 415 | setTextField(type, row, ADDRESS, ""); |
| 416 | setBreakpointChecked(type, row, Qt::Unchecked); |
| 417 | } |
| 418 | if (!enabled) return; |
| 419 | break; |
| 420 | } |
| 421 | case SLOT: { |
| 422 | auto slot = parseSlotField(bpIndex, item->text()); |
| 423 | auto [ps, ss] = slot; |
| 424 | setTextField(type, row, SLOT, QString("%1/%2") |
| 425 | .arg(ps ? QChar('0' + *ps) : QChar('X')) |
| 426 | .arg(ss ? QChar('0' + *ss) : QChar('X'))); |
| 427 | if (!enabled) return; |
| 428 | break; |
| 429 | } |
| 430 | case SEGMENT: { |
| 431 | auto segment = parseSegmentField(bpIndex, item->text()); |
| 432 | setTextField(type, row, SEGMENT, segment ? QString::number(*segment) : "X"); |
| 433 | if (!enabled) return; |
| 434 | break; |
| 435 | } |
| 436 | case T_CONDITION: { |
| 437 | setTextField(type, row, T_CONDITION, item->text().simplified()); |
| 438 | |
| 439 | if (type == BreakpointType::CONDITION) { |
| 440 | setBreakpointChecked(type, row, item->text().isEmpty() ? Qt::Unchecked : item->checkState()); |
| 441 | } |
| 442 | if (!enabled) return; |
| 443 | break; |
| 444 | } |
| 445 | case BREAKPOINT_ID: |
| 446 | return; |
| 447 | default: |
| 448 | qWarning() << "Unknown table column" << table->column(item); |
| 449 | assert(false); |
| 450 | } |
| 451 | |
| 452 | if (enabled) { |
| 453 | if (createBp) { |
| 454 | createBreakpoint(type, row); |
| 455 | } else { |
| 456 | replaceBreakpoint(type, row); |
| 457 | } |
| 458 | } else { |
| 459 | if (bpIndex) removeBreakpoint(type, row, false); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | void BreakpointViewer::disableSorting(BreakpointType type) |
| 464 | { |
| 465 | auto unsort = [](auto* table) { |
| 466 | table->sortByColumn(-1, Qt::AscendingOrder); |
| 467 | }; |
| 468 | |
| 469 | if (type == BreakpointType::ALL) { |
| 470 | for (auto* table : tables) { |
| 471 | unsort(table); |
| 472 | } |
| 473 | } else { |
| 474 | unsort(tables[type]); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | void BreakpointViewer::changeBpTableItem(QTableWidgetItem* item) |
| 479 | { |
| 480 | if (!userMode) return; |
| 481 | disableSorting(BreakpointType::BREAKPOINT); |
| 482 | changeTableItem(BreakpointType::BREAKPOINT, item); |
| 483 | } |
| 484 | |
| 485 | void BreakpointViewer::changeWpTableItem(QTableWidgetItem* item) |
| 486 | { |
| 487 | if (!userMode) return; |
| 488 | disableSorting(BreakpointType::WATCHPOINT); |
| 489 | changeTableItem(BreakpointType::WATCHPOINT, item); |
| 490 | } |
| 491 | |
| 492 | void BreakpointViewer::changeCnTableItem(QTableWidgetItem* item) |
| 493 | { |
| 494 | if (!userMode) return; |
| 495 | disableSorting(BreakpointType::CONDITION); |
| 496 | changeTableItem(BreakpointType::CONDITION, item); |
| 497 | } |
| 498 | |
| 499 | void BreakpointViewer::setBreakpoints(Breakpoints* bps) |
| 500 | { |
| 501 | breakpoints = bps; |
| 502 | } |
| 503 | |
| 504 | void BreakpointViewer::setRunState() |
| 505 | { |
| 506 | runState = true; |
| 507 | } |
| 508 | |
| 509 | void BreakpointViewer::setBreakState() |
| 510 | { |
| 511 | runState = false; |
| 512 | } |
| 513 | |
| 514 | void BreakpointViewer::stretchTable(BreakpointType type) |
| 515 | { |
| 516 | auto stretch = [](auto* table) { |
| 517 | // stretching will not work without sorting |
| 518 | table->setSortingEnabled(true); |
| 519 | table->resizeColumnsToContents(); |
| 520 | }; |
| 521 | |
| 522 | if (type == BreakpointType::ALL) { |
| 523 | for (auto* table : tables) { |
| 524 | stretch(table); |
| 525 | } |
| 526 | } else { |
| 527 | stretch(tables[type]); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | std::optional<int> BreakpointViewer::findBreakpointIndex(BreakpointType type, int row) const |
| 532 | { |
| 533 | auto* table = tables[type]; |
| 534 | if (table->rowCount() <= row) return {}; |
| 535 | const QString& id = table->item(row, BREAKPOINT_ID)->text(); |
| 536 | if (id.isEmpty()) return {}; |
| 537 | |
| 538 | for (int bpIndex = 0; bpIndex < breakpoints->breakpointCount(); ++bpIndex) { |
| 539 | const Breakpoint& bp = breakpoints->getBreakpoint(bpIndex); |
| 540 | if (bp.id == id) return bpIndex; |
| 541 | } |
| 542 | return {}; |
| 543 | } |
| 544 | |
| 545 | std::optional<int> BreakpointViewer::findBreakpointRow(BreakpointType type, const QString& id) const |
| 546 | { |
| 547 | auto* table = tables[type]; |
| 548 | |
| 549 | for (int row = 0; row < table->rowCount(); ++row) { |
| 550 | const QString& id_ = table->item(row, BREAKPOINT_ID)->text(); |
| 551 | if (id_ == id) return row; |
| 552 | } |
| 553 | return {}; |
| 554 | } |
| 555 | |
| 556 | void BreakpointViewer::refreshTableRow(int bpIndex, BreakpointType type, int row) |
| 557 | { |
| 558 | bool preserveSymbol = Settings::get().preserveBreakpointSymbol(); |
| 559 | const Breakpoint& bp = breakpoints->getBreakpoint(bpIndex); |
| 560 | const auto* location = tables[type]->item(row, LOCATION); |
| 561 | auto* addressItem = tables[type]->item(row, ADDRESS); |
| 562 | |
| 563 | if (type == BreakpointType::BREAKPOINT) { |
| 564 | // symbol changed address? |
| 565 | bool ok; |
| 566 | Symbol* symbol = debugSession.symbolTable().getAddressSymbol(location->text()); |
| 567 | uint16_t address = addressItem->text().toInt(&ok); |
| 568 | assert(ok); |
| 569 | |
| 570 | // preserve symbol name? |
| 571 | if (preserveSymbol && symbol && symbol->value() != address) { |
| 572 | addressItem->setText(QString("%1").arg(symbol->value())); |
| 573 | replaceBreakpoint(type, row); |
| 574 | } else { |
| 575 | fillTableRow(type, row, bpIndex); |
| 576 | if (location->text().toUpper() == hexValue(bp.range->start, 4)) { |
| 577 | fillTableRowLocation(type, row, findSymbolOrValue(bp.range->start)); |
| 578 | } |
| 579 | } |
| 580 | } else { |
| 581 | fillTableRow(type, row, bpIndex); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | void BreakpointViewer::refresh() |
| 586 | { |
| 587 | // don't reload if self-inflicted update |
| 588 | if (disableRefresh) return; |
| 589 | |
| 590 | // store unused items position by disabling ordering |
| 591 | disableSorting(); |
| 592 | |
| 593 | for (int bpIndex = 0; bpIndex < breakpoints->breakpointCount(); ++bpIndex) { |
| 594 | const Breakpoint& bp = breakpoints->getBreakpoint(bpIndex); |
| 595 | |
| 596 | BreakpointType type = bp.type == Breakpoint::BREAKPOINT ? BreakpointType::BREAKPOINT |
| 597 | : (bp.type == Breakpoint::CONDITION ? BreakpointType::CONDITION |
| 598 | : BreakpointType::WATCHPOINT); |
| 599 | auto row = findBreakpointRow(type, bp.id); |
| 600 | |
| 601 | if (row) { |
| 602 | auto sa = ScopedAssign(userMode, false); |
| 603 | refreshTableRow(bpIndex, type, *row); |
| 604 | } else { |
| 605 | // new breakpoints created on OpenMSX will go through here |
| 606 | auto row = createTableRow(type); |
| 607 | fillTableRow(type, row, bpIndex); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | // remove remaining unpaired BreakpointRef whose breakpoints were replaced or deleted |
| 612 | for (const auto& type : {BreakpointType::BREAKPOINT, BreakpointType::WATCHPOINT, BreakpointType::CONDITION}) { |
| 613 | const auto& table = tables[type]; |
| 614 | for (int row = 0; row < table->rowCount();) { |
| 615 | auto* item = table->item(row, ENABLED); |
| 616 | auto bpIndex = findBreakpointIndex(type, row); |
| 617 | if (item->checkState() == Qt::Checked && !bpIndex) { |
| 618 | table->removeRow(row); |
| 619 | continue; |
| 620 | } |
| 621 | row++; |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | stretchTable(BreakpointType::ALL); |
| 626 | } |
| 627 | |
| 628 | void BreakpointViewer::changeCurrentWpType(int row, int /*selected*/) |
| 629 | { |
| 630 | if (!userMode) return; |
| 631 | auto* item = wpTableWidget->item(row, WP_TYPE); |
| 632 | changeTableItem(BreakpointType::WATCHPOINT, item); |
| 633 | } |
| 634 | |
| 635 | void BreakpointViewer::createComboBox(int row) |
| 636 | { |
| 637 | auto sa = ScopedAssign(userMode, false); |
| 638 | |
| 639 | auto* combo = new QComboBox(); |
| 640 | combo->setEditable(false); |
| 641 | combo->insertItems(0, QStringList(std::begin(ComboTypeNames), std::end(ComboTypeNames))); |
| 642 | |
| 643 | auto* model = wpTableWidget->model(); |
| 644 | auto index = model->index(row, WP_TYPE); |
| 645 | |
| 646 | wpTableWidget->setIndexWidget(index, combo); |
| 647 | |
| 648 | connect(combo, qOverload<int>(&QComboBox::currentIndexChanged), |
| 649 | [this, row](int index){ changeCurrentWpType(row, index); }); |
| 650 | } |
| 651 | |
| 652 | Breakpoint::Type BreakpointViewer::readComboBox(int row) |
| 653 | { |
| 654 | auto* model = wpTableWidget->model(); |
| 655 | auto* combo = (QComboBox*) wpTableWidget->indexWidget(model->index(row, WP_TYPE)); |
| 656 | return static_cast<Breakpoint::Type>(combo->currentIndex() + 1); |
| 657 | } |
| 658 | |
| 659 | int BreakpointViewer::createTableRow(BreakpointType type, int row) |
| 660 | { |
| 661 | auto sa = ScopedAssign(userMode, false); |
| 662 | auto& table = tables[type]; |
| 663 | |
| 664 | if (row == -1) { |
| 665 | row = table->rowCount(); |
| 666 | table->setRowCount(row + 1); |
| 667 | table->selectRow(row); |
| 668 | } else { |
| 669 | table->insertRow(row); |
| 670 | table->selectRow(row); |
| 671 | } |
| 672 | |
| 673 | // enabled |
| 674 | auto* item0 = new QTableWidgetItem(); |
| 675 | item0->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsSelectable | Qt::ItemIsEnabled); |
| 676 | item0->setTextAlignment(Qt::AlignCenter); |
| 677 | table->setItem(row, ENABLED, item0); |
| 678 | |
| 679 | // watchpoint type |
| 680 | auto* item1 = new QTableWidgetItem(); |
| 681 | item1->setTextAlignment(Qt::AlignCenter); |
| 682 | table->setItem(row, WP_TYPE, item1); |
| 683 | |
| 684 | if (type == BreakpointType::WATCHPOINT) { |
| 685 | createComboBox(row); |
| 686 | } |
| 687 | |
| 688 | // location |
| 689 | auto* item2 = new QTableWidgetItem(); |
| 690 | item2->setTextAlignment(Qt::AlignCenter); |
| 691 | table->setItem(row, LOCATION, item2); |
| 692 | |
| 693 | if (type == BreakpointType::CONDITION) { |
| 694 | item2->setFlags(Qt::NoItemFlags); |
| 695 | item2->setText("0"); |
| 696 | } else { |
| 697 | item2->setText(""); |
| 698 | } |
| 699 | |
| 700 | // condition |
| 701 | auto* item3 = new QTableWidgetItem(); |
| 702 | item3->setTextAlignment(Qt::AlignCenter); |
| 703 | item3->setText(""); |
| 704 | table->setItem(row, T_CONDITION, item3); |
| 705 | |
| 706 | // ps/ss |
| 707 | auto* item4 = new QTableWidgetItem(); |
| 708 | item4->setTextAlignment(Qt::AlignCenter); |
| 709 | item4->setText("X/X"); |
| 710 | table->setItem(row, SLOT, item4); |
| 711 | |
| 712 | // segment |
| 713 | auto* item5 = new QTableWidgetItem(); |
| 714 | item5->setTextAlignment(Qt::AlignCenter); |
| 715 | item5->setText("X"); |
| 716 | table->setItem(row, SEGMENT, item5); |
| 717 | |
| 718 | // breakpoint id |
| 719 | auto* item6 = new QTableWidgetItem(); |
| 720 | item6->setFlags(Qt::NoItemFlags); |
| 721 | item6->setText(""); |
| 722 | table->setItem(row, BREAKPOINT_ID, item6); |
| 723 | |
| 724 | // address |
| 725 | auto* item7 = new QTableWidgetItem(); |
| 726 | item7->setFlags(Qt::NoItemFlags); |
| 727 | item7->setText(""); |
| 728 | table->setItem(row, ADDRESS, item7); |
| 729 | |
| 730 | return row; |
| 731 | } |
| 732 | |
| 733 | void BreakpointViewer::fillTableRowLocation(BreakpointType type, int row, const QString& location) |
| 734 | { |
| 735 | auto* table = tables[type]; |
| 736 | auto* item = table->item(row, LOCATION); |
| 737 | item->setText(location); |
| 738 | } |
| 739 | |
| 740 | void BreakpointViewer::fillTableRow(BreakpointType type, int row, int bpIndex) |
| 741 | { |
| 742 | auto sa = ScopedAssign(userMode, false); |
| 743 | |
| 744 | const auto& bp = breakpoints->getBreakpoint(bpIndex); |
| 745 | auto& table = tables[type]; |
| 746 | |
| 747 | // enabled status |
| 748 | setBreakpointChecked(type, row, Qt::Checked); |
| 749 | |
| 750 | // watchpoint type |
| 751 | if (type == BreakpointType::WATCHPOINT) { |
| 752 | auto* model = table->model(); |
| 753 | auto* combo = (QComboBox*) table->indexWidget(model->index(row, WP_TYPE)); |
| 754 | combo->setCurrentIndex(static_cast<int>(bp.type) - 1); |
| 755 | } |
| 756 | |
| 757 | // location |
| 758 | auto* item2 = table->item(row, LOCATION); |
| 759 | QString location; |
| 760 | |
| 761 | if (bp.type == Breakpoint::BREAKPOINT) { |
| 762 | location = findSymbolOrValue(bp.range->start); |
| 763 | } |
| 764 | if (location.isEmpty()) { |
| 765 | int adrLen = (bp.type == Breakpoint::WATCHPOINT_IOREAD || bp.type == Breakpoint::WATCHPOINT_IOWRITE) ? 2 : 4; |
| 766 | location = locationString(*bp.range, adrLen); |
| 767 | } |
| 768 | item2->setText(location); |
| 769 | |
| 770 | auto* item3 = table->item(row, T_CONDITION); |
| 771 | item3->setText(bp.condition); |
| 772 | |
| 773 | // slot |
| 774 | QString slot = QString("%1/%2") |
| 775 | .arg(bp.slot.ps ? QChar('0' + *bp.slot.ps) : QChar('X')) |
| 776 | .arg(bp.slot.ss ? QChar('0' + *bp.slot.ss) : QChar('X')); |
| 777 | |
| 778 | auto* item4 = table->item(row, SLOT); |
| 779 | item4->setText(slot); |
| 780 | |
| 781 | // segment |
| 782 | QString segment = bp.segment ? QString::number(*bp.segment) : "X"; |
| 783 | |
| 784 | auto* item5 = table->item(row, SEGMENT); |
| 785 | item5->setText(segment); |
| 786 | |
| 787 | // breakpoint id |
| 788 | auto* item6 = table->item(row, BREAKPOINT_ID); |
| 789 | item6->setText(bp.id); |
| 790 | |
| 791 | // address |
| 792 | auto* item7 = table->item(row, ADDRESS); |
| 793 | item7->setText(bp.range ? QString("%1").arg(bp.range->start) : ""); |
| 794 | } |
| 795 | |
| 796 | std::optional<Breakpoint> BreakpointViewer::parseTableRow(BreakpointType type, int row) |
| 797 | { |
| 798 | Breakpoint bp; |
| 799 | |
| 800 | auto* table = tables[type]; |
| 801 | auto* model = table->model(); |
| 802 | auto* combo = (QComboBox*) table->indexWidget(model->index(row, WP_TYPE)); |
| 803 | bp.type = type == BreakpointType::WATCHPOINT ? readComboBox(row) : Breakpoint::BREAKPOINT; |
| 804 | |
| 805 | QString location = table->item(row, LOCATION)->text(); |
| 806 | auto range = parseLocationField({}, type, location, combo ? combo->currentText() : ""); |
| 807 | if (range) bp.range = *range; |
| 808 | |
| 809 | bp.condition = table->item(row, T_CONDITION)->text(); |
| 810 | bp.slot = parseSlotField({}, table->item(row, SLOT)->text()); |
| 811 | |
| 812 | auto segment = parseSegmentField({}, table->item(row, SEGMENT)->text()); |
| 813 | if (segment) bp.segment = *segment; |
| 814 | |
| 815 | return bp; |
| 816 | } |
| 817 | |
| 818 | |
| 819 | void BreakpointViewer::onSymbolTableChanged() |
| 820 | { |
| 821 | for (int row = 0; row < bpTableWidget->rowCount(); ++row) { |
| 822 | auto bpIndex = findBreakpointIndex(BreakpointType::BREAKPOINT, row); |
| 823 | auto* item = bpTableWidget->item(row, LOCATION); |
| 824 | |
| 825 | if (bpIndex && !item->text().isEmpty()) { |
| 826 | if (Symbol* symbol = debugSession.symbolTable().getAddressSymbol(item->text())) { |
| 827 | setTextField(BreakpointType::BREAKPOINT, row, LOCATION, symbol->text()); |
| 828 | } else { |
| 829 | const Breakpoint& bp = breakpoints->getBreakpoint(*bpIndex); |
| 830 | setTextField(BreakpointType::BREAKPOINT, row, LOCATION, findSymbolOrValue(bp.range->start)); |
| 831 | } |
| 832 | } |
| 833 | } |
| 834 | stretchTable(BreakpointType::ALL); |
| 835 | } |
| 836 | |
| 837 | |
| 838 | void BreakpointViewer::onAddBtnClicked(BreakpointType type) |
| 839 | { |
| 840 | auto sa = ScopedAssign(userMode, false); |
| 841 | |
| 842 | auto* table = tables[type]; |
| 843 | table->sortByColumn(-1, Qt::AscendingOrder); |
| 844 | table->setSortingEnabled(false); |
| 845 | |
| 846 | int row = createTableRow(type, 0); |
| 847 | setBreakpointChecked(type, row, Qt::Unchecked); |
| 848 | stretchTable(type); |
| 849 | table->setSortingEnabled(true); |
| 850 | } |
| 851 | |
| 852 | void BreakpointViewer::onRemoveBtnClicked(BreakpointType type) |
| 853 | { |
| 854 | auto* table = tables[type]; |
| 855 | |
| 856 | if (table->currentRow() == -1) |
| 857 | return; |
| 858 | |
| 859 | auto sa = ScopedAssign(userMode, false); |
| 860 | auto* item = table->item(table->currentRow(), ENABLED); |
| 861 | if (item->checkState() == Qt::Checked) { |
| 862 | removeBreakpoint(type, table->currentRow(), true); |
| 863 | } else { |
| 864 | table->removeRow(table->currentRow()); |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | void BreakpointViewer::on_btnAddBp_clicked() |
| 869 | { |
| 870 | onAddBtnClicked(BreakpointType::BREAKPOINT); |
| 871 | } |
| 872 | |
| 873 | void BreakpointViewer::on_btnRemoveBp_clicked() |
| 874 | { |
| 875 | onRemoveBtnClicked(BreakpointType::BREAKPOINT); |
| 876 | } |
| 877 | |
| 878 | void BreakpointViewer::on_btnAddWp_clicked() |
| 879 | { |
| 880 | onAddBtnClicked(BreakpointType::WATCHPOINT); |
| 881 | } |
| 882 | |
| 883 | void BreakpointViewer::on_btnRemoveWp_clicked() |
| 884 | { |
| 885 | onRemoveBtnClicked(BreakpointType::WATCHPOINT); |
| 886 | } |
| 887 | |
| 888 | void BreakpointViewer::on_btnAddCn_clicked() |
| 889 | { |
| 890 | onAddBtnClicked(BreakpointType::CONDITION); |
| 891 | } |
| 892 | |
| 893 | void BreakpointViewer::on_btnRemoveCn_clicked() |
| 894 | { |
| 895 | onRemoveBtnClicked(BreakpointType::CONDITION); |
| 896 | } |
| 897 | |
| 898 | void BreakpointViewer::on_itemPressed(QTableWidgetItem* /*item*/) |
| 899 | { |
| 900 | bpTableWidget->setSortingEnabled(false); |
| 901 | } |
| 902 | |
| 903 | void BreakpointViewer::on_headerClicked(int /*index*/) |
| 904 | { |
| 905 | bpTableWidget->setSortingEnabled(true); |
| 906 | } |