| 1 | #include "DebuggerData.h" |
| 2 | #include "Convert.h" |
| 3 | #include "ranges.h" |
| 4 | #include <qglobal.h> |
| 5 | #include <QStringList> |
| 6 | #include <QDebug> |
| 7 | #include <algorithm> |
| 8 | // class MemoryLayout |
| 9 | |
| 10 | static const char* const TypeNames[] = { "breakpoint", "memread", "memwrite", "ioread", "iowrite", |
| 11 | "condition" }; |
| 12 | |
| 13 | MemoryLayout::MemoryLayout() |
| 14 | { |
| 15 | for (int p = 0; p < 4; ++p) { |
| 16 | primarySlot[p] = 0; |
| 17 | secondarySlot[p] = -1; |
| 18 | mapperSegment[p] = 0; |
| 19 | isSubslotted[p] = false; |
| 20 | for (int q = 0; q < 4; ++q) { |
| 21 | mapperSize[p][q] = 0; |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | |
| 27 | // class Breakpoints |
| 28 | |
| 29 | static const char *BreakpointSetCodes[] = { |
| 30 | "set_bp", |
| 31 | "set_watchpoint read_mem", |
| 32 | "set_watchpoint write_mem", |
| 33 | "set_watchpoint read_io", |
| 34 | "set_watchpoint write_io", |
| 35 | "set_condition" |
| 36 | }; |
| 37 | |
| 38 | static QString getNextArgument(QString& data, int& pos) |
| 39 | { |
| 40 | QString result; |
| 41 | while (data[pos] == ' ' || data[pos] == '\t') ++pos; |
| 42 | while (true) { |
| 43 | if (data[pos] == ' ' || data[pos] == '\t') break; |
| 44 | if (data[pos] == '}' || data[pos] == ']') break; |
| 45 | result += data[pos]; |
| 46 | ++pos; |
| 47 | } |
| 48 | return result; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | bool Breakpoint::operator==(const Breakpoint &bp) const |
| 53 | { |
| 54 | if (bp.type != type) return false; |
| 55 | |
| 56 | // compare address |
| 57 | if (type != Breakpoint::CONDITION) { |
| 58 | if (bp.range->start != range->start) return false; |
| 59 | if (type != Breakpoint::BREAKPOINT) { |
| 60 | if (bp.range->end != range->end) return false; |
| 61 | } |
| 62 | // compare slot |
| 63 | if (bp.slot.ps != slot.ps || bp.slot.ss != slot.ss || bp.segment != segment) return false; |
| 64 | } |
| 65 | // compare condition |
| 66 | return bp.condition == condition; |
| 67 | } |
| 68 | |
| 69 | void Breakpoints::clear() |
| 70 | { |
| 71 | breakpoints.clear(); |
| 72 | } |
| 73 | |
| 74 | void Breakpoints::setMemoryLayout(MemoryLayout* ml) |
| 75 | { |
| 76 | memLayout = ml; |
| 77 | } |
| 78 | |
| 79 | QString Breakpoints::createSetCommand(Breakpoint::Type type, std::optional<AddressRange> range, |
| 80 | Slot slot, std::optional<uint8_t> segment, |
| 81 | QString condition) |
| 82 | { |
| 83 | QString cmd("debug %1 %2 %3"); |
| 84 | QString addr, cond; |
| 85 | condition = condition.trimmed(); |
| 86 | |
| 87 | if (type == Breakpoint::CONDITION) { |
| 88 | // conditions don't have an address |
| 89 | cond = QString("{%1}").arg(condition); |
| 90 | } else if (range) { |
| 91 | if (type == Breakpoint::BREAKPOINT || !range->end) { |
| 92 | // breakpoint (these never have a range) or watchpoint without range |
| 93 | addr = QString::number(range->start); |
| 94 | } else { |
| 95 | // some type of watchpoint (with a valid range) |
| 96 | addr = QString("{%1 %2}").arg(range->start).arg(*range->end); |
| 97 | } |
| 98 | |
| 99 | cond = QString("{ [ %1_in_slot %2 %3 %4 ] %5}") |
| 100 | .arg(type == Breakpoint::WATCHPOINT_MEMREAD |
| 101 | || type == Breakpoint::WATCHPOINT_MEMWRITE ? "watch" : "pc") |
| 102 | .arg(slot.ps ? QString::number(*slot.ps) : "X") |
| 103 | .arg(slot.ss ? QString::number(*slot.ss) : "X") |
| 104 | .arg(segment ? QString::number(*segment) : "X") |
| 105 | .arg(condition.isEmpty() ? QString() : QString("&& ( %1 ) ").arg(condition)); |
| 106 | } |
| 107 | return cmd.arg(BreakpointSetCodes[type]) |
| 108 | .arg(addr) |
| 109 | .arg(escapeXML(cond)); |
| 110 | } |
| 111 | |
| 112 | QString Breakpoints::createRemoveCommand(const QString& id) |
| 113 | { |
| 114 | // find breakpoint |
| 115 | QString cmd("debug remove_"); |
| 116 | if (id.startsWith("bp")) |
| 117 | cmd += "bp "; |
| 118 | else if (id.startsWith("wp")) |
| 119 | cmd += "watchpoint "; |
| 120 | else |
| 121 | cmd += "condition "; |
| 122 | cmd += id; |
| 123 | return cmd; |
| 124 | } |
| 125 | |
| 126 | void Breakpoints::setBreakpoints(const QString& str) |
| 127 | { |
| 128 | breakpoints.clear(); |
| 129 | |
| 130 | QStringList bps = str.split('\n'); |
| 131 | for (auto& bp : bps) { |
| 132 | if (bp.trimmed().isEmpty()) continue; |
| 133 | |
| 134 | Breakpoint newBp; |
| 135 | |
| 136 | // set id |
| 137 | int p = 0; |
| 138 | newBp.id = getNextArgument(bp, p); |
| 139 | |
| 140 | // determine type |
| 141 | if (bp.startsWith("bp#")) { |
| 142 | newBp.type = Breakpoint::BREAKPOINT; |
| 143 | } else if (bp.startsWith("wp#")) { |
| 144 | // determine watchpoint type |
| 145 | static const char* const WpTypeNames[] = {"read_mem", "write_mem", "read_io", "write_io"}; |
| 146 | QString wpType = getNextArgument(bp, p); |
| 147 | if (auto it = ranges::find(WpTypeNames, wpType); it != std::end(WpTypeNames)) { |
| 148 | newBp.type = static_cast<Breakpoint::Type>(std::distance(WpTypeNames, it) + 1); |
| 149 | } else { |
| 150 | continue; |
| 151 | } |
| 152 | } else if (bp.startsWith("cond#")) { |
| 153 | newBp.type = Breakpoint::CONDITION; |
| 154 | } else { // unknown |
| 155 | continue; |
| 156 | } |
| 157 | |
| 158 | // get address |
| 159 | p++; |
| 160 | if (newBp.type != Breakpoint::CONDITION) { |
| 161 | if (bp[p] == '{') { |
| 162 | p++; |
| 163 | auto s = getNextArgument(bp, p); |
| 164 | auto start = stringToValue<uint16_t>(s); |
| 165 | if (!start) continue; |
| 166 | newBp.range = AddressRange(*start); |
| 167 | int q = bp.indexOf('}', p); |
| 168 | auto end = stringToValue<uint16_t>(bp.mid(p, q - p)); |
| 169 | newBp.range->end = end; |
| 170 | p = q + 1; |
| 171 | } else { |
| 172 | auto s = getNextArgument(bp, p); |
| 173 | auto start = stringToValue<uint16_t>(s); |
| 174 | if (!start) continue; |
| 175 | newBp.range = AddressRange(*start); |
| 176 | } |
| 177 | } |
| 178 | // check and clip command (skip non-default commands) |
| 179 | int q = bp.lastIndexOf('{'); |
| 180 | if (bp.mid(q).simplified() != "{debug break}") continue; |
| 181 | |
| 182 | newBp.condition = unescapeXML(bp.mid(p, q - p).trimmed()); |
| 183 | parseCondition(newBp); |
| 184 | insertBreakpoint(newBp); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | QString Breakpoints::mergeBreakpoints(const QString& str) |
| 189 | { |
| 190 | // copy breakpoints |
| 191 | auto oldBps = breakpoints; |
| 192 | // parse new list |
| 193 | setBreakpoints(str); |
| 194 | // check old list against new one |
| 195 | QStringList mergeSet; |
| 196 | for (const auto& old : oldBps) { |
| 197 | auto newit = breakpoints.begin(); |
| 198 | for (/**/; newit != breakpoints.end(); ++newit) { |
| 199 | // check for identical data |
| 200 | if (old == *newit) break; |
| 201 | } |
| 202 | if (newit == breakpoints.end()) { |
| 203 | // create command to set this breakpoint again |
| 204 | QString cmd = createSetCommand(old.type, old.range, old.slot, old.segment, old.condition); |
| 205 | mergeSet << cmd; |
| 206 | } |
| 207 | } |
| 208 | return mergeSet.join(" ; "); |
| 209 | } |
| 210 | |
| 211 | void Breakpoints::parseCondition(Breakpoint& bp) |
| 212 | { |
| 213 | bp.slot = {}; |
| 214 | bp.segment = {}; |
| 215 | |
| 216 | // first split off braces |
| 217 | if (bp.condition[0] == '{' && bp.condition.endsWith('}')) { |
| 218 | if (bp.type != Breakpoint::CONDITION) { |
| 219 | // check for slot argument |
| 220 | QRegExp rx(R"(^\{\s*\[\s*(pc|watch)_in_slot\s([X0123])\s([X0123])\s(X|\d{1,3})\s*\]\s*(&&\s*\((.+)\)\s*)?\}$)"); |
| 221 | if (rx.indexIn(bp.condition) == 0) { |
| 222 | bp.slot.ps = stringToValue<uint8_t>(rx.cap(2)); |
| 223 | bp.slot.ss = stringToValue<uint8_t>(rx.cap(3)); |
| 224 | bp.segment = stringToValue<uint8_t>(rx.cap(4)); |
| 225 | bp.condition = rx.cap(6).trimmed(); |
| 226 | } else { |
| 227 | bp.condition.chop(1); |
| 228 | bp.condition = bp.condition.mid(1).trimmed(); |
| 229 | } |
| 230 | } else { |
| 231 | bp.condition.chop(1); |
| 232 | bp.condition = bp.condition.mid(1).trimmed(); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | bool Breakpoints::inCurrentSlot(const Breakpoint& bp) |
| 238 | { |
| 239 | if (!memLayout) return true; |
| 240 | if (bp.type == Breakpoint::CONDITION) return true; |
| 241 | assert(bp.range); |
| 242 | |
| 243 | int page = (bp.range->start & 0xC000) >> 14; |
| 244 | if (!bp.slot.ps) return true; |
| 245 | if (*bp.slot.ps == memLayout->primarySlot[page]) { |
| 246 | if (memLayout->isSubslotted[*bp.slot.ps & 3]) { |
| 247 | if (!bp.slot.ss) return true; |
| 248 | if (*bp.slot.ss == memLayout->secondarySlot[page]) { |
| 249 | if (memLayout->mapperSize[*bp.slot.ps & 3][*bp.slot.ss & 3] > 0) { |
| 250 | if (!bp.segment || *bp.segment == memLayout->mapperSegment[page]) { |
| 251 | return true; |
| 252 | } |
| 253 | } else { |
| 254 | return true; |
| 255 | } |
| 256 | } |
| 257 | } else if (bp.slot.ss) { |
| 258 | return false; |
| 259 | } else if (memLayout->mapperSize[*bp.slot.ps & 3][0] > 0) { |
| 260 | if (!bp.segment || *bp.segment == memLayout->mapperSegment[page]) { |
| 261 | return true; |
| 262 | } |
| 263 | } else { |
| 264 | return true; |
| 265 | } |
| 266 | } |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | void Breakpoints::insertBreakpoint(Breakpoint& bp) |
| 271 | { |
| 272 | auto it = ranges::upper_bound(breakpoints, bp.range->start, {}, [](auto& bp) { return bp.range ? bp.range->start : -1; }); |
| 273 | breakpoints.insert(it, bp); |
| 274 | } |
| 275 | |
| 276 | int Breakpoints::breakpointCount() |
| 277 | { |
| 278 | return breakpoints.size(); |
| 279 | } |
| 280 | |
| 281 | bool Breakpoints::isBreakpoint(uint16_t addr, QString* id, bool checkSlot) |
| 282 | { |
| 283 | if (std::optional<uint16_t> index = findBreakpoint(addr)) { |
| 284 | for (auto i = *index; i < breakpointCount() && breakpoints[i].range && breakpoints[i].range->start == addr; i++) { |
| 285 | const auto& bp = breakpoints[i]; |
| 286 | if ((!checkSlot || inCurrentSlot(bp)) && bp.type == Breakpoint::BREAKPOINT) { |
| 287 | if (id) *id = bp.id; |
| 288 | return true; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | bool Breakpoints::isWatchpoint(quint16 addr, QString* id, bool checkSlot) |
| 296 | { |
| 297 | for (const auto& bp : breakpoints) { |
| 298 | if (bp.type == Breakpoint::WATCHPOINT_MEMREAD || bp.type == Breakpoint::WATCHPOINT_MEMWRITE) { |
| 299 | assert(bp.range); |
| 300 | if (bp.range->contains(addr)) { |
| 301 | if (!checkSlot || inCurrentSlot(bp)) { |
| 302 | if (id) *id = bp.id; |
| 303 | return true; |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | std::optional<uint16_t> |
| 312 | Breakpoints::findBreakpoint(uint16_t addr) |
| 313 | { |
| 314 | if (auto i = ranges::lower_bound(breakpoints, addr, |
| 315 | [](auto& bp, auto addr) { return bp.range ? bp.range->start < addr : true; }); |
| 316 | i != breakpoints.end() && i->range && i->range->start == addr) { |
| 317 | return std::distance(breakpoints.begin(), i); |
| 318 | } |
| 319 | return {}; |
| 320 | } |
| 321 | |
| 322 | const Breakpoint& Breakpoints::getBreakpoint(int index) |
| 323 | { |
| 324 | assert((long unsigned) index < breakpoints.size()); |
| 325 | return breakpoints[index]; |
| 326 | } |
| 327 | |
| 328 | void Breakpoints::saveBreakpoints(QXmlStreamWriter& xml) |
| 329 | { |
| 330 | // write symbols |
| 331 | for (const auto& bp : breakpoints) { |
| 332 | xml.writeStartElement("Breakpoint"); |
| 333 | |
| 334 | // type |
| 335 | xml.writeAttribute("type", TypeNames[bp.type]); |
| 336 | |
| 337 | // id |
| 338 | xml.writeAttribute("id", bp.id); |
| 339 | |
| 340 | // slot/segment |
| 341 | xml.writeAttribute("primarySlot", QString(bp.slot.ps ? QChar('0' + *bp.slot.ps) : '*')); |
| 342 | xml.writeAttribute("secondarySlot", QString(bp.slot.ss ? QChar('0' + *bp.slot.ss) : '*')); |
| 343 | xml.writeAttribute("segment", QString::number(*bp.segment)); |
| 344 | |
| 345 | // address |
| 346 | if (bp.type == Breakpoint::BREAKPOINT) { |
| 347 | assert(bp.range); |
| 348 | xml.writeTextElement("address", QString::number(bp.range->start)); |
| 349 | } else if (bp.type != Breakpoint::CONDITION) { |
| 350 | assert(bp.range); |
| 351 | xml.writeTextElement("regionStart", QString::number(bp.range->start)); |
| 352 | if (bp.range->end) xml.writeTextElement("regionEnd", QString::number(*bp.range->end)); |
| 353 | } |
| 354 | |
| 355 | // condition |
| 356 | xml.writeTextElement("condition", bp.condition); |
| 357 | |
| 358 | // complete |
| 359 | xml.writeEndElement(); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | void Breakpoints::loadBreakpoints(QXmlStreamReader& xml) |
| 364 | { |
| 365 | Breakpoint bp; |
| 366 | while (!xml.atEnd()) { |
| 367 | xml.readNext(); |
| 368 | // exit if closing of main tag |
| 369 | if (xml.isEndElement()) { |
| 370 | if (xml.name() == "Breakpoints") { |
| 371 | break; |
| 372 | } else if (xml.name() == "Breakpoint") { |
| 373 | insertBreakpoint(bp); |
| 374 | } |
| 375 | } |
| 376 | // begin tag |
| 377 | if (xml.isStartElement()) { |
| 378 | if (xml.name() == "Breakpoint") { |
| 379 | // set type |
| 380 | QString label = xml.attributes().value("type").toString().toLower(); |
| 381 | if (auto it = ranges::find(TypeNames, label); it != std::end(TypeNames)) { |
| 382 | bp.type = static_cast<Breakpoint::Type>(std::distance(TypeNames, it)); |
| 383 | } else { |
| 384 | bp.type = Breakpoint::BREAKPOINT; |
| 385 | } |
| 386 | |
| 387 | // id |
| 388 | bp.id = xml.attributes().value("id").toString(); |
| 389 | |
| 390 | // slot/segment |
| 391 | char c = xml.attributes().value("primarySlot").at(0).toLatin1(); |
| 392 | bp.slot.ps = make_if(c >= '0' && c <= '3', c - '0'); |
| 393 | c = xml.attributes().value("secondarySlot").at(0).toLatin1(); |
| 394 | bp.slot.ss = make_if(c >= '0' && c <= '3', c - '0'); |
| 395 | bp.segment = stringToValue<uint8_t>(xml.attributes().value("segment") |
| 396 | .toString()); |
| 397 | |
| 398 | } else if (xml.name() == "address" || xml.name() == "regionStart") { |
| 399 | // read symbol name |
| 400 | auto s = xml.readElementText(); |
| 401 | auto start = stringToValue<uint16_t>(s); |
| 402 | if (!start) continue; |
| 403 | bp.range = AddressRange(*start); |
| 404 | if (bp.type == Breakpoint::BREAKPOINT) bp.range->end = {}; |
| 405 | } else if (xml.name() == "regionEnd") { |
| 406 | // read symbol name |
| 407 | assert(bp.range); |
| 408 | bp.range->end = stringToValue<uint16_t>(xml.readElementText()); |
| 409 | } else if (xml.name() == "condition") { |
| 410 | bp.condition = xml.readElementText().trimmed(); |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | } |