| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#include "debug.h" |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#include "sysdep.h" |
| 15 |
#include <stdio.h> |
| 16 |
#include <ctype.h> |
| 17 |
#include <stdarg.h> |
| 18 |
|
| 19 |
|
| 20 |
#include <strings.h> |
| 21 |
|
| 22 |
#include "alan.version.h" |
| 23 |
|
| 24 |
#ifdef USE_READLINE |
| 25 |
#include "readline.h" |
| 26 |
#endif |
| 27 |
|
| 28 |
#include "compatibility.h" |
| 29 |
#include "lists.h" |
| 30 |
#include "inter.h" |
| 31 |
#include "current.h" |
| 32 |
#include "options.h" |
| 33 |
#include "utils.h" |
| 34 |
#include "instance.h" |
| 35 |
#include "memory.h" |
| 36 |
#include "output.h" |
| 37 |
#include "class.h" |
| 38 |
#include "event.h" |
| 39 |
#include "exe.h" |
| 40 |
|
| 41 |
#ifdef HAVE_GLK |
| 42 |
#include "glk.h" |
| 43 |
#define MAP_STDIO_TO_GLK |
| 44 |
#include "glkio.h" |
| 45 |
#endif |
| 46 |
|
| 47 |
#define BREAKPOINTMAX 50 |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
int breakpointCount = 0; |
| 52 |
Breakpoint breakpoint[BREAKPOINTMAX]; |
| 53 |
|
| 54 |
#define debugPrefix "adbg: " |
| 55 |
|
| 56 |
|
| 57 |
static void showAttributes(AttributeEntry *attributes) |
| 58 |
{ |
| 59 |
AttributeEntry *at; |
| 60 |
int i; |
| 61 |
char str[80]; |
| 62 |
|
| 63 |
if (attributes == 0) |
| 64 |
return; |
| 65 |
|
| 66 |
i = 1; |
| 67 |
for (at = attributes; !isEndOfArray(at); at++) { |
| 68 |
sprintf(str, "$i$t%s[%d] = %d", (char *) pointerTo(at->id), at->code, (int)at->value); |
| 69 |
output(str); |
| 70 |
i++; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
static void showContents(int cnt) |
| 77 |
{ |
| 78 |
int i; |
| 79 |
char str[80]; |
| 80 |
Abool found = false; |
| 81 |
|
| 82 |
output("$iContains:"); |
| 83 |
for (i = 1; i <= header->instanceMax; i++) { |
| 84 |
if (isIn(i, cnt, DIRECT)) { |
| 85 |
if (!found) |
| 86 |
found = true; |
| 87 |
output("$i$t"); |
| 88 |
say(i); |
| 89 |
sprintf(str, "[%d] ", i); |
| 90 |
output(str); |
| 91 |
} |
| 92 |
} |
| 93 |
if (!found) |
| 94 |
output("nothing"); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
static char *idOfInstance(int instance) { |
| 100 |
int base = header->instanceTableAddress+ |
| 101 |
header->instanceMax*sizeof(InstanceEntry)/sizeof(Aword)+1; |
| 102 |
return (char *)&memory[memory[base+instance-1]]; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
static void sayInstanceNumberAndName(int ins) { |
| 108 |
char buf[1000]; |
| 109 |
|
| 110 |
sprintf(buf, "[%d] %s (\"$$", ins, idOfInstance(ins)); |
| 111 |
output(buf); |
| 112 |
say(ins); |
| 113 |
output("$$\")"); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
static void sayLocationOfInstance(int ins, char *prefix) { |
| 119 |
if (admin[ins].location == 0) |
| 120 |
return; |
| 121 |
else { |
| 122 |
if (prefix) output(prefix); |
| 123 |
if (isALocation(admin[ins].location)) { |
| 124 |
output("at"); |
| 125 |
sayInstanceNumberAndName(admin[ins].location); |
| 126 |
sayLocationOfInstance(admin[ins].location, prefix); |
| 127 |
} else if (isAContainer(admin[ins].location)) { |
| 128 |
if (isAObject(admin[ins].location)) |
| 129 |
output("in"); |
| 130 |
else if (isAActor(admin[ins].location)) |
| 131 |
output("carried by"); |
| 132 |
sayInstanceNumberAndName(admin[ins].location); |
| 133 |
sayLocationOfInstance(admin[ins].location, prefix); |
| 134 |
} else |
| 135 |
output("Illegal location!"); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
static void listInstance(int ins) { |
| 141 |
output("$i"); |
| 142 |
sayInstanceNumberAndName(ins); |
| 143 |
if (instances[ins].container) |
| 144 |
output("(container)"); |
| 145 |
sayLocationOfInstance(ins, ", "); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
static void listInstances(char *pattern) |
| 151 |
{ |
| 152 |
int ins; |
| 153 |
bool found = false; |
| 154 |
|
| 155 |
for (ins = 1; ins <= header->instanceMax; ins++) { |
| 156 |
if (pattern == NULL || (pattern != NULL && match(pattern, idOfInstance(ins)))) { |
| 157 |
if (!found) { |
| 158 |
output("Instances:"); |
| 159 |
found = true; |
| 160 |
} |
| 161 |
listInstance(ins); |
| 162 |
} |
| 163 |
} |
| 164 |
if (pattern != NULL && !found) |
| 165 |
output("No instances matched the pattern."); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
static void showInstance(int ins) |
| 170 |
{ |
| 171 |
char str[80]; |
| 172 |
|
| 173 |
if (ins > header->instanceMax || ins < 1) { |
| 174 |
sprintf(str, "Instance index %d is out of range.", ins); |
| 175 |
output(str); |
| 176 |
return; |
| 177 |
} |
| 178 |
|
| 179 |
output("The"); |
| 180 |
sayInstanceNumberAndName(ins); |
| 181 |
if (instances[ins].parent) { |
| 182 |
sprintf(str, "Isa %s[%d]", idOfClass(instances[ins].parent), instances[ins].parent); |
| 183 |
output(str); |
| 184 |
} |
| 185 |
|
| 186 |
if (!isA(ins, header->locationClassId) || (isA(ins, header->locationClassId) && admin[ins].location != 0)) { |
| 187 |
sprintf(str, "$iLocation:"); |
| 188 |
output(str); |
| 189 |
needSpace = true; |
| 190 |
sayLocationOfInstance(ins, NULL); |
| 191 |
} |
| 192 |
|
| 193 |
output("$iAttributes:"); |
| 194 |
showAttributes(admin[ins].attributes); |
| 195 |
|
| 196 |
if (instances[ins].container) |
| 197 |
showContents(ins); |
| 198 |
|
| 199 |
if (isA(ins, header->actorClassId)) { |
| 200 |
if (admin[ins].script == 0) |
| 201 |
output("$iIs idle"); |
| 202 |
else { |
| 203 |
sprintf(str, "$iExecuting script: %d, Step: %d", admin[ins].script, admin[ins].step); |
| 204 |
output(str); |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
static void listObjects(void) |
| 212 |
{ |
| 213 |
int obj; |
| 214 |
|
| 215 |
output("Objects:"); |
| 216 |
for (obj = 1; obj <= header->instanceMax; obj++) |
| 217 |
if (isAObject(obj)) |
| 218 |
listInstance(obj); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
static void showObject(int obj) |
| 224 |
{ |
| 225 |
char str[80]; |
| 226 |
|
| 227 |
|
| 228 |
if (!isAObject(obj)) { |
| 229 |
sprintf(str, "Instance %d is not an object", obj); |
| 230 |
output(str); |
| 231 |
return; |
| 232 |
} |
| 233 |
|
| 234 |
showInstance(obj); |
| 235 |
|
| 236 |
} |
| 237 |
|
| 238 |
#ifdef UNDEF_WHEN_NEEDED |
| 239 |
|
| 240 |
static void showcnts(void) |
| 241 |
{ |
| 242 |
char str[80]; |
| 243 |
int cnt; |
| 244 |
|
| 245 |
output("Containers:"); |
| 246 |
for (cnt = 1; cnt <= header->containerMax; cnt++) { |
| 247 |
sprintf(str, "$i%3d: ", cnt); |
| 248 |
output(str); |
| 249 |
if (containers[cnt].owner != 0) |
| 250 |
say(containers[cnt].owner); |
| 251 |
} |
| 252 |
|
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
static void showContainer(int cnt) |
| 257 |
{ |
| 258 |
char str[80]; |
| 259 |
|
| 260 |
if (cnt < 1 || cnt > header->containerMax) { |
| 261 |
sprintf(str, "Container number out of range. Between 1 and %d, please.", header->containerMax); |
| 262 |
output(str); |
| 263 |
return; |
| 264 |
} |
| 265 |
|
| 266 |
sprintf(str, "Container %d :", cnt); |
| 267 |
output(str); |
| 268 |
if (containers[cnt].owner != 0) { |
| 269 |
cnt = containers[cnt].owner; |
| 270 |
say(cnt); |
| 271 |
sprintf(str, "$iLocation: %d", where(cnt, true)); |
| 272 |
output(str); |
| 273 |
} |
| 274 |
showContents(cnt); |
| 275 |
} |
| 276 |
#endif |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
static int sourceFileNumber(char *fileName) { |
| 281 |
SourceFileEntry *entries = pointerTo(header->sourceFileTable); |
| 282 |
int n; |
| 283 |
|
| 284 |
for (n = 0; *(Aword*)&entries[n] != EOF; n++) { |
| 285 |
char *entryName; |
| 286 |
entryName = getStringFromFile(entries[n].fpos, entries[n].len); |
| 287 |
if (strcmp(entryName, fileName) == 0) return n; |
| 288 |
entryName = baseNameStart(entryName); |
| 289 |
if (strcmp(entryName, fileName) == 0) return n; |
| 290 |
} |
| 291 |
return -1; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
static void printClassName(int c) { |
| 298 |
output(idOfClass(c)); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
static void showClassInheritance(int c) { |
| 304 |
char str[80]; |
| 305 |
|
| 306 |
if (classes[c].parent != 0) { |
| 307 |
output(", Isa"); |
| 308 |
printClassName(classes[c].parent); |
| 309 |
sprintf(str, "[%d]", classes[c].parent); |
| 310 |
output(str); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
static void showClass(int cla) |
| 317 |
{ |
| 318 |
char str[80]; |
| 319 |
|
| 320 |
if (cla < 1) { |
| 321 |
sprintf(str, "Class index %d is out of range.", cla); |
| 322 |
output(str); |
| 323 |
return; |
| 324 |
} |
| 325 |
|
| 326 |
output("$t"); |
| 327 |
printClassName(cla); |
| 328 |
sprintf(str, "[%d]", cla); |
| 329 |
output(str); |
| 330 |
showClassInheritance(cla); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
static void listClass(int c) |
| 336 |
{ |
| 337 |
char str[80]; |
| 338 |
|
| 339 |
sprintf(str, "%3d: ", c); |
| 340 |
output(str); |
| 341 |
printClassName(c); |
| 342 |
showClassInheritance(c); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
static void showClassHierarchy(int this, int depth) |
| 348 |
{ |
| 349 |
int i; |
| 350 |
int child; |
| 351 |
|
| 352 |
output("$i"); |
| 353 |
for (i=0; i < depth; i++) |
| 354 |
output("$t"); |
| 355 |
listClass(this); |
| 356 |
for (child = 1; child <= header->classMax; child++) { |
| 357 |
if (classes[child].parent == this) { |
| 358 |
showClassHierarchy(child, depth+1); |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
static void listLocations(void) |
| 366 |
{ |
| 367 |
int loc; |
| 368 |
|
| 369 |
output("Locations:"); |
| 370 |
for (loc = 1; loc <= header->instanceMax; loc++) |
| 371 |
if (isALocation(loc)) |
| 372 |
listInstance(loc); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
static void showLocation(int loc) |
| 378 |
{ |
| 379 |
char str[80]; |
| 380 |
|
| 381 |
|
| 382 |
if (!isALocation(loc)) { |
| 383 |
sprintf(str, "Instance %d is not a location.", loc); |
| 384 |
output(str); |
| 385 |
return; |
| 386 |
} |
| 387 |
|
| 388 |
output("The "); |
| 389 |
say(loc); |
| 390 |
sprintf(str, "(%d) Isa location :", loc); |
| 391 |
output(str); |
| 392 |
|
| 393 |
output("$iAttributes ="); |
| 394 |
showAttributes(admin[loc].attributes); |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
static void listActors(void) |
| 400 |
{ |
| 401 |
int act; |
| 402 |
|
| 403 |
output("Actors:"); |
| 404 |
for (act = 1; act <= header->instanceMax; act++) |
| 405 |
if (isAActor(act)) |
| 406 |
listInstance(act); |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
static void showActor(int act) |
| 412 |
{ |
| 413 |
char str[80]; |
| 414 |
|
| 415 |
if (!isAActor(act)) { |
| 416 |
sprintf(str, "Instance %d is not an actor.", act); |
| 417 |
output(str); |
| 418 |
return; |
| 419 |
} |
| 420 |
|
| 421 |
showInstance(act); |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
static void showEvents(void) |
| 427 |
{ |
| 428 |
int event, i; |
| 429 |
char str[80]; |
| 430 |
bool scheduled; |
| 431 |
|
| 432 |
output("Events:"); |
| 433 |
for (event = 1; event <= header->eventMax; event++) { |
| 434 |
sprintf(str, "$i%d [%s]:", event, (char *)pointerTo(events[event].id)); |
| 435 |
output(str); |
| 436 |
scheduled = false; |
| 437 |
for (i = 0; i < eventQueueTop; i++) |
| 438 |
if ((scheduled = (eventQueue[i].event == event))) |
| 439 |
break; |
| 440 |
if (scheduled) { |
| 441 |
sprintf(str, "Scheduled for +%d, at ", eventQueue[i].after); |
| 442 |
output(str); |
| 443 |
say(eventQueue[i].where); |
| 444 |
} else |
| 445 |
output("Not scheduled."); |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
char *sourceFileName(int fileNumber) { |
| 452 |
SourceFileEntry *entries = pointerTo(header->sourceFileTable); |
| 453 |
|
| 454 |
return getStringFromFile(entries[fileNumber].fpos, entries[fileNumber].len); |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
char *readSourceLine(int file, int line) { |
| 460 |
int count; |
| 461 |
#define SOURCELINELENGTH 1000 |
| 462 |
static char buffer[SOURCELINELENGTH]; |
| 463 |
|
| 464 |
#ifdef HAVE_GLK |
| 465 |
frefid_t sourceFileRef; |
| 466 |
strid_t sourceFile; |
| 467 |
|
| 468 |
sourceFileRef = glk_fileref_create_by_name(fileusage_TextMode, sourceFileName(file), 0); |
| 469 |
sourceFile = glk_stream_open_file(sourceFileRef, filemode_Read, 0); |
| 470 |
#else |
| 471 |
FILE *sourceFile; |
| 472 |
|
| 473 |
sourceFile = fopen(sourceFileName(file), "r"); |
| 474 |
#endif |
| 475 |
|
| 476 |
if (sourceFile != NULL) { |
| 477 |
for (count = 0; count < line; count++) { |
| 478 |
if (fgets(buffer, SOURCELINELENGTH, sourceFile) == 0) |
| 479 |
return NULL; |
| 480 |
|
| 481 |
while (strchr(buffer, '\n') == NULL) |
| 482 |
if (fgets(buffer, SOURCELINELENGTH, sourceFile) == 0) |
| 483 |
return buffer; |
| 484 |
} |
| 485 |
fclose(sourceFile); |
| 486 |
return buffer; |
| 487 |
} |
| 488 |
return NULL; |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
void showSourceLine(int fileNumber, int line) { |
| 493 |
char *buffer = readSourceLine(fileNumber, line); |
| 494 |
if (buffer != NULL) { |
| 495 |
if (buffer[strlen(buffer)-1] == '\n') |
| 496 |
buffer[strlen(buffer)-1] = '\0'; |
| 497 |
printf("<%05d>: %s", line, buffer); |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
static void listFiles() { |
| 504 |
SourceFileEntry *entry; |
| 505 |
int i = 0; |
| 506 |
for (entry = pointerTo(header->sourceFileTable); *((Aword*)entry) != EOF; entry++) { |
| 507 |
printf(" %2d : %s\n", i, sourceFileName(i)); |
| 508 |
i++; |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
static void listLines() { |
| 515 |
SourceLineEntry *entry; |
| 516 |
for (entry = pointerTo(header->sourceLineTable); *((Aword*)entry) != EOF; entry++) |
| 517 |
printf(" %s:%d\n", sourceFileName(entry->file), entry->line); |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
static int findSourceLineIndex(SourceLineEntry *entry, int file, int line) { |
| 523 |
|
| 524 |
int i = 0; |
| 525 |
|
| 526 |
while (!isEndOfArray(&entry[i]) && entry[i].file != file) |
| 527 |
i++; |
| 528 |
while (!isEndOfArray(&entry[i]) && entry[i].file == file && entry[i].line < line) |
| 529 |
i++; |
| 530 |
if (isEndOfArray(entry) || entry[i].file != file) |
| 531 |
return i-1; |
| 532 |
else |
| 533 |
return i; |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
static void listBreakpoints() { |
| 539 |
int i; |
| 540 |
bool found = false; |
| 541 |
|
| 542 |
for (i = 0; i < BREAKPOINTMAX; i++) |
| 543 |
if (breakpoint[i].line != 0) { |
| 544 |
if (!found) |
| 545 |
printf("Breakpoints set:\n"); |
| 546 |
found = true; |
| 547 |
printf(" %s:%d\n", sourceFileName(breakpoint[i].file), breakpoint[i].line); |
| 548 |
} |
| 549 |
if (!found) |
| 550 |
printf("No breakpoints set\n"); |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
int breakpointIndex(int file, int line) { |
| 556 |
int i; |
| 557 |
|
| 558 |
for (i = 0; i < BREAKPOINTMAX; i++) |
| 559 |
if (breakpoint[i].line == line && breakpoint[i].file == file) |
| 560 |
return i; |
| 561 |
return -1; |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
static int availableBreakpointSlot() { |
| 567 |
int i; |
| 568 |
|
| 569 |
for (i = 0; i < BREAKPOINTMAX; i++) |
| 570 |
if (breakpoint[i].line == 0) |
| 571 |
return i; |
| 572 |
return -1; |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
static void setBreakpoint(int file, int line) { |
| 578 |
int i = breakpointIndex(file, line); |
| 579 |
|
| 580 |
if (i != -1) |
| 581 |
printf("Breakpoint already set at %s:%d\n", sourceFileName(file), line); |
| 582 |
else { |
| 583 |
i = availableBreakpointSlot(); |
| 584 |
if (i == -1) |
| 585 |
printf("No room for more breakpoints. Delete one first.\n"); |
| 586 |
else { |
| 587 |
int lineIndex = findSourceLineIndex(pointerTo(header->sourceLineTable), file, line); |
| 588 |
SourceLineEntry *entry = pointerTo(header->sourceLineTable); |
| 589 |
char leadingText[100] = "Breakpoint"; |
| 590 |
if (entry[lineIndex].file == EOF) { |
| 591 |
printf("Line %d not available\n", line); |
| 592 |
} else { |
| 593 |
if (entry[lineIndex].line != line) |
| 594 |
sprintf(leadingText, "Line %d not available, breakpoint instead", line); |
| 595 |
breakpoint[i].file = entry[lineIndex].file; |
| 596 |
breakpoint[i].line = entry[lineIndex].line; |
| 597 |
printf("%s set at %s:%d\n", leadingText, sourceFileName(entry[lineIndex].file), entry[lineIndex].line); |
| 598 |
showSourceLine(entry[lineIndex].file, entry[lineIndex].line); |
| 599 |
printf("\n"); |
| 600 |
} |
| 601 |
} |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
static void deleteBreakpoint(int line, int file) { |
| 608 |
int i = breakpointIndex(file, line); |
| 609 |
|
| 610 |
if (i == -1) |
| 611 |
printf("No breakpoint set at %s:%d\n", sourceFileName(file), line); |
| 612 |
else { |
| 613 |
breakpoint[i].line = 0; |
| 614 |
printf("Breakpoint at %s:%d deleted\n", sourceFileName(file), line); |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
static bool saved_traceSection, saved_traceInstruction, saved_capitilize, saved_tracePush, saved_traceStack, saved_traceSource; |
| 621 |
static int loc; |
| 622 |
|
| 623 |
|
| 624 |
void saveInfo(void) |
| 625 |
{ |
| 626 |
|
| 627 |
saved_capitilize = capitalize; capitalize = false; |
| 628 |
saved_traceSection = traceSectionOption; traceSectionOption = false; |
| 629 |
saved_traceSource = traceSourceOption; traceSourceOption = false; |
| 630 |
saved_traceInstruction = traceInstructionOption; traceInstructionOption = false; |
| 631 |
saved_tracePush = tracePushOption; tracePushOption = false; |
| 632 |
saved_traceStack = traceStackOption; traceStackOption = false; |
| 633 |
loc = current.location; current.location = where(HERO, DIRECT); |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
void restoreInfo(void) |
| 639 |
{ |
| 640 |
|
| 641 |
capitalize = saved_capitilize; |
| 642 |
traceSectionOption = saved_traceSection; |
| 643 |
traceInstructionOption = saved_traceInstruction; |
| 644 |
traceSourceOption = saved_traceSource; |
| 645 |
tracePushOption = saved_tracePush; |
| 646 |
traceStackOption = saved_traceStack; |
| 647 |
current.location = loc; |
| 648 |
} |
| 649 |
|
| 650 |
#define HELP_COMMAND 'H' |
| 651 |
#define QUIT_COMMAND 'Q' |
| 652 |
#define EXIT_COMMAND 'X' |
| 653 |
#define GO_COMMAND 'G' |
| 654 |
#define FILES_COMMAND 'F' |
| 655 |
#define LINES_COMMAND 'l' |
| 656 |
#define INSTANCES_COMMAND 'I' |
| 657 |
#define CLASSES_COMMAND 'C' |
| 658 |
#define OBJECTS_COMMAND 'O' |
| 659 |
#define ACTORS_COMMAND 'A' |
| 660 |
#define LOCATIONS_COMMAND 'L' |
| 661 |
#define EVENTS_COMMAND 'E' |
| 662 |
#define BREAK_COMMAND 'B' |
| 663 |
#define DELETE_COMMAND 'D' |
| 664 |
#define TRACE_COMMAND 'R' |
| 665 |
#define SECTION_TRACE_COMMAND 'T' |
| 666 |
#define INSTRUCTION_TRACE_COMMAND 'S' |
| 667 |
#define NEXT_COMMAND 'N' |
| 668 |
#define UNKNOWN_COMMAND '?' |
| 669 |
#define AMBIGUOUS_COMMAND '-' |
| 670 |
#define TRACE_SOURCE_COMMAND 's' |
| 671 |
#define TRACE_SECTION_COMMAND 'e' |
| 672 |
#define TRACE_INSTRUCTION_COMMAND 'i' |
| 673 |
#define TRACE_PUSH_COMMAND 'p' |
| 674 |
#define TRACE_STACK_COMMAND 't' |
| 675 |
|
| 676 |
typedef struct DebugParseEntry { |
| 677 |
char *command; |
| 678 |
char *parameter; |
| 679 |
char code; |
| 680 |
char *helpText; |
| 681 |
} DebugParseEntry; |
| 682 |
|
| 683 |
static DebugParseEntry commandEntries[] = { |
| 684 |
{"help", "", HELP_COMMAND, "this help"}, |
| 685 |
{"?", "", HELP_COMMAND, "d:o"}, |
| 686 |
{"break", "[[file:]n]", BREAK_COMMAND, "set breakpoint at source line [n] (optionally in [file])"}, |
| 687 |
{"delete", "[[file:]n]", DELETE_COMMAND, "delete breakpoint at source line [n] (optionally in [file])"}, |
| 688 |
{"files", "", FILES_COMMAND, "list source files"}, |
| 689 |
{"events", "", EVENTS_COMMAND, "list events"}, |
| 690 |
{"classes", "", CLASSES_COMMAND, "list class hierarchy"}, |
| 691 |
{"instances", "[n]", INSTANCES_COMMAND, "list instance(s), all, wildcard, number or name"}, |
| 692 |
{"objects", "[n]", OBJECTS_COMMAND, "list instance(s) that are objects"}, |
| 693 |
{"actors", "[n]", ACTORS_COMMAND, "list instance(s) that are actors"}, |
| 694 |
{"locations", "[n]", LOCATIONS_COMMAND, "list instances that are locations"}, |
| 695 |
{"trace", "('source'|'section'|'instruction'|'push'|'stack')", TRACE_COMMAND, "toggle various traces"}, |
| 696 |
{"next", "", NEXT_COMMAND, "continue game and stop at next source line"}, |
| 697 |
{"go", "", GO_COMMAND, "go another player turn"}, |
| 698 |
{"exit", "", EXIT_COMMAND, "exit to game, enter 'debug' to get back"}, |
| 699 |
{"x", "", EXIT_COMMAND, "d:o"}, |
| 700 |
{"quit", "", QUIT_COMMAND, "quit game"}, |
| 701 |
{NULL, NULL} |
| 702 |
}; |
| 703 |
|
| 704 |
static DebugParseEntry traceSubcommand[] = { |
| 705 |
{"source", "", TRACE_SOURCE_COMMAND, ""}, |
| 706 |
{"section", "", TRACE_SECTION_COMMAND, ""}, |
| 707 |
{"instructions", "", TRACE_INSTRUCTION_COMMAND, ""}, |
| 708 |
{"pushs", "", TRACE_PUSH_COMMAND, ""}, |
| 709 |
{"stacks", "", TRACE_STACK_COMMAND, ""}, |
| 710 |
{NULL, NULL} |
| 711 |
}; |
| 712 |
|
| 713 |
|
| 714 |
static char *spaces(int length) { |
| 715 |
static char buf[200]; |
| 716 |
int i; |
| 717 |
|
| 718 |
for (i = 0; i<length; i++) |
| 719 |
buf[i] = ' '; |
| 720 |
buf[i] = '\0'; |
| 721 |
return buf; |
| 722 |
} |
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
static char *padding(DebugParseEntry *entry, int maxLength) { |
| 727 |
return spaces(maxLength-strlen(entry->command)-strlen(entry->parameter)); |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
static void handleHelpCommand() { |
| 733 |
if (!regressionTestOption) |
| 734 |
output(alan.longHeader); |
| 735 |
|
| 736 |
DebugParseEntry *entry = commandEntries; |
| 737 |
|
| 738 |
int maxLength = 0; |
| 739 |
for (entry = commandEntries; entry->command != NULL; entry++) { |
| 740 |
if (strlen(entry->command)+strlen(entry->parameter) > maxLength) |
| 741 |
maxLength = strlen(entry->command)+strlen(entry->parameter); |
| 742 |
} |
| 743 |
|
| 744 |
output("$nADBG Commands (can be abbreviated):"); |
| 745 |
for (entry = commandEntries; entry->command != NULL; entry++) { |
| 746 |
char buf[200]; |
| 747 |
sprintf(buf, "$i%s %s %s$n$t$t-- %s", entry->command, entry->parameter, padding(entry, maxLength), entry->helpText); |
| 748 |
output(buf); |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
static DebugParseEntry *findEntry(char *command, DebugParseEntry *entry) { |
| 755 |
while (entry->command != NULL) { |
| 756 |
if (strncasecmp(command, entry->command, strlen(command)) == 0) |
| 757 |
return entry; |
| 758 |
entry++; |
| 759 |
} |
| 760 |
return NULL; |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
static char parseDebugCommand(char *command) { |
| 766 |
DebugParseEntry *entry = findEntry(command, commandEntries); |
| 767 |
if (entry != NULL) { |
| 768 |
if (strlen(command) < strlen(entry->command)) { |
| 769 |
|
| 770 |
if (findEntry(command, entry+1) != NULL) |
| 771 |
|
| 772 |
return AMBIGUOUS_COMMAND; |
| 773 |
} |
| 774 |
return entry->code; |
| 775 |
} else |
| 776 |
return UNKNOWN_COMMAND; |
| 777 |
} |
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
static void readCommand(char buf[]) { |
| 782 |
char c; |
| 783 |
|
| 784 |
capitalize = false; |
| 785 |
if (anyOutput) newline(); |
| 786 |
do { |
| 787 |
output("adbg> "); |
| 788 |
|
| 789 |
#ifdef USE_READLINE |
| 790 |
if (!readline(buf)) { |
| 791 |
#else |
| 792 |
if (fgets(buf, 255, stdin) == NULL) { |
| 793 |
#endif |
| 794 |
newline(); |
| 795 |
quitGame(); |
| 796 |
} |
| 797 |
lin = 1; |
| 798 |
c = buf[0]; |
| 799 |
} while (c == '\0'); |
| 800 |
} |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
static void displaySourceLocation(int line, int fileNumber) { |
| 805 |
char *cause; |
| 806 |
if (anyOutput) newline(); |
| 807 |
if (breakpointIndex(fileNumber, line) != -1) |
| 808 |
cause = "Breakpoint hit at"; |
| 809 |
else |
| 810 |
cause = "Stepping to"; |
| 811 |
printf("%s %s %s:%d\n", debugPrefix, cause, sourceFileName(fileNumber), line); |
| 812 |
showSourceLine(fileNumber, line); |
| 813 |
printf("\n"); |
| 814 |
anyOutput = false; |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
static void toggleSectionTrace() { |
| 820 |
if ((saved_traceSection = !saved_traceSection)) |
| 821 |
printf("Section trace on."); |
| 822 |
else |
| 823 |
printf("Section trace off."); |
| 824 |
} |
| 825 |
|
| 826 |
|
| 827 |
static void toggleInstructionTrace() { |
| 828 |
if ((saved_traceInstruction = !saved_traceInstruction)) |
| 829 |
printf("Instruction trace on."); |
| 830 |
else |
| 831 |
printf("Instruction trace off."); |
| 832 |
} |
| 833 |
|
| 834 |
|
| 835 |
static void toggleSourceTrace() { |
| 836 |
if ((saved_traceSource = !saved_traceSource)) |
| 837 |
printf("Source code trace on."); |
| 838 |
else |
| 839 |
printf("Source code trace off."); |
| 840 |
} |
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
static void togglePushTrace() { |
| 845 |
if ((saved_tracePush = !saved_tracePush)) |
| 846 |
printf("Stack Push trace on."); |
| 847 |
else |
| 848 |
printf("Stack Push trace off."); |
| 849 |
} |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
static void toggleStackTrace() { |
| 854 |
if ((saved_traceStack = !saved_traceStack)) |
| 855 |
printf("Full stack trace on."); |
| 856 |
else |
| 857 |
printf("Full stack trace off."); |
| 858 |
} |
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
static int parseTraceCommand() { |
| 863 |
char *subcommand = strtok(NULL, ""); |
| 864 |
DebugParseEntry *entry; |
| 865 |
if (subcommand == 0) |
| 866 |
return UNKNOWN_COMMAND; |
| 867 |
else { |
| 868 |
entry = findEntry(subcommand, traceSubcommand); |
| 869 |
if (entry != NULL) { |
| 870 |
if (strlen(subcommand) < strlen(entry->command)) { |
| 871 |
if (findEntry(subcommand, entry+1) != NULL) |
| 872 |
return AMBIGUOUS_COMMAND; |
| 873 |
} |
| 874 |
return entry->code; |
| 875 |
} else |
| 876 |
return UNKNOWN_COMMAND; |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
static char *printTraceState(bool state) { |
| 883 |
if (state) |
| 884 |
return "on - Traces"; |
| 885 |
else |
| 886 |
return "off - Doesn't trace"; |
| 887 |
} |
| 888 |
|
| 889 |
|
| 890 |
static void printTrace(void) { |
| 891 |
printf("Trace section : %s entry to every section (check, description, event, actor, ...)\n", printTraceState(saved_traceSection)); |
| 892 |
printf("Trace source : %s every source line executed\n", printTraceState(saved_traceSource)); |
| 893 |
printf("Trace instruction : %s every Amachine instruction executed\n", printTraceState(saved_traceInstruction)); |
| 894 |
printf("Trace push : %s every push onto the Amachine stack\n", printTraceState(saved_tracePush)); |
| 895 |
printf("Trace stack : %s the complete stack every time\n", printTraceState(saved_traceStack)); |
| 896 |
} |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
static void handleTraceCommand() { |
| 901 |
char subcommand = parseTraceCommand(); |
| 902 |
|
| 903 |
switch (subcommand) { |
| 904 |
case TRACE_SECTION_COMMAND: toggleSectionTrace(); break; |
| 905 |
case TRACE_SOURCE_COMMAND: toggleSourceTrace(); break; |
| 906 |
case TRACE_INSTRUCTION_COMMAND: toggleInstructionTrace(); break; |
| 907 |
case TRACE_PUSH_COMMAND: togglePushTrace(); break; |
| 908 |
case TRACE_STACK_COMMAND: toggleStackTrace(); break; |
| 909 |
case AMBIGUOUS_COMMAND: output("Ambiguous Trace subcommand abbreviation. ? for help."); break; |
| 910 |
default: printTrace(); |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
static void handleBreakCommand(int fileNumber) { |
| 917 |
char *parameter = strtok(NULL, ":"); |
| 918 |
if (parameter != NULL && isalpha((int)parameter[0])) { |
| 919 |
fileNumber = sourceFileNumber(parameter); |
| 920 |
if (fileNumber == -1) { |
| 921 |
printf("No such file: '%s'\n", parameter); |
| 922 |
return; |
| 923 |
} |
| 924 |
parameter = strtok(NULL, ""); |
| 925 |
} |
| 926 |
if (parameter == NULL) |
| 927 |
listBreakpoints(); |
| 928 |
else |
| 929 |
setBreakpoint(fileNumber, atoi(parameter)); |
| 930 |
} |
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
static void handleDeleteCommand(bool calledFromBreakpoint, int line, int fileNumber) { |
| 935 |
char *parameter = strtok(NULL, ""); |
| 936 |
if (parameter == NULL) { |
| 937 |
if (calledFromBreakpoint) |
| 938 |
deleteBreakpoint(line, fileNumber); |
| 939 |
else |
| 940 |
printf("No current breakpoint to delete\n"); |
| 941 |
} else |
| 942 |
deleteBreakpoint(atoi(parameter), fileNumber); |
| 943 |
} |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
static void handleNextCommand(bool calledFromBreakpoint) { |
| 948 |
stopAtNextLine = true; |
| 949 |
debugOption = false; |
| 950 |
if (!calledFromBreakpoint) |
| 951 |
current.sourceLine = 0; |
| 952 |
restoreInfo(); |
| 953 |
} |
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
static void handleLocationsCommand() { |
| 958 |
char *parameter = strtok(NULL, ""); |
| 959 |
if (parameter == 0) |
| 960 |
listLocations(); |
| 961 |
else |
| 962 |
showLocation(atoi(parameter)); |
| 963 |
} |
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
static void handleActorsCommand() { |
| 968 |
char *parameter = strtok(NULL, ""); |
| 969 |
if (parameter == NULL) |
| 970 |
listActors(); |
| 971 |
else |
| 972 |
showActor(atoi(parameter)); |
| 973 |
} |
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
static void handleClassesCommand() { |
| 978 |
char *parameter = strtok(NULL, ""); |
| 979 |
if (parameter == NULL || strchr(parameter, '*') != 0) { |
| 980 |
output("Classes:"); |
| 981 |
showClassHierarchy(1, 0); |
| 982 |
listInstances(parameter); |
| 983 |
} else if (isdigit((int)parameter[0])) |
| 984 |
showClass(atoi(parameter)); |
| 985 |
else { |
| 986 |
printf("You have to give a class index to display. You can't use names (yet)."); |
| 987 |
} |
| 988 |
} |
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
static void handleObjectsCommand() { |
| 993 |
char *parameter = strtok(NULL, ""); |
| 994 |
if (parameter == NULL) |
| 995 |
listObjects(); |
| 996 |
else |
| 997 |
showObject(atoi(parameter)); |
| 998 |
} |
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
static void handleInstancesCommand() { |
| 1003 |
char *parameter = strtok(NULL, ""); |
| 1004 |
int i; |
| 1005 |
|
| 1006 |
if (parameter == NULL || strchr(parameter, '*') != 0) |
| 1007 |
listInstances(parameter); |
| 1008 |
else if (isdigit((int)parameter[0])) |
| 1009 |
showInstance(atoi(parameter)); |
| 1010 |
else { |
| 1011 |
for (i = 1; i<header->instanceMax; i++) |
| 1012 |
if (strcmp(parameter, idOfInstance(i)) == 0) { |
| 1013 |
showInstance(i); |
| 1014 |
return; |
| 1015 |
} |
| 1016 |
printf("No instance named '%s'.", parameter); |
| 1017 |
} |
| 1018 |
} |
| 1019 |
|
| 1020 |
|
| 1021 |
static bool exactSameVersion() { |
| 1022 |
return header->version[3] == alan.version.version |
| 1023 |
&& header->version[2] == alan.version.revision |
| 1024 |
&& header->version[1] == alan.version.correction |
| 1025 |
&& header->version[0] == alan.version.state[0]; |
| 1026 |
} |
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
void debug(bool calledFromBreakpoint, int line, int fileNumber) |
| 1031 |
{ |
| 1032 |
static bool warned = false; |
| 1033 |
|
| 1034 |
saveInfo(); |
| 1035 |
|
| 1036 |
#ifdef HAVE_GLK |
| 1037 |
glk_set_style(style_Preformatted); |
| 1038 |
#endif |
| 1039 |
|
| 1040 |
if (calledFromBreakpoint) |
| 1041 |
displaySourceLocation(line, fileNumber); |
| 1042 |
else { |
| 1043 |
if (!exactSameVersion() && !warned && !regressionTestOption) { |
| 1044 |
printf("<WARNING: You are debugging a game which has version %s.>\n", |
| 1045 |
decodedGameVersion(header->version)); |
| 1046 |
printf("<That is not exactly the same as this interpreter (%s).>\n", alan.version.string); |
| 1047 |
printf("<This might cause a lot of trouble. Cross your fingers...>\n"); |
| 1048 |
warned = true; |
| 1049 |
} |
| 1050 |
} |
| 1051 |
|
| 1052 |
while (true) { |
| 1053 |
|
| 1054 |
char commandLine[200]; |
| 1055 |
readCommand(commandLine); |
| 1056 |
char *command = strtok(commandLine, " "); |
| 1057 |
char commandCode = parseDebugCommand(command); |
| 1058 |
|
| 1059 |
switch (commandCode) { |
| 1060 |
case AMBIGUOUS_COMMAND: output("Ambiguous ADBG command abbreviation. ? for help."); break; |
| 1061 |
case ACTORS_COMMAND: handleActorsCommand(); break; |
| 1062 |
case BREAK_COMMAND: handleBreakCommand(fileNumber); break; |
| 1063 |
case CLASSES_COMMAND: handleClassesCommand(); break; |
| 1064 |
case DELETE_COMMAND: handleDeleteCommand(calledFromBreakpoint, line, fileNumber); break; |
| 1065 |
case EVENTS_COMMAND: showEvents(); break; |
| 1066 |
case EXIT_COMMAND: debugOption = false; restoreInfo(); goto exit_debug; |
| 1067 |
case FILES_COMMAND: listFiles(); break; |
| 1068 |
case LINES_COMMAND: listLines(); break; |
| 1069 |
case GO_COMMAND: restoreInfo(); goto exit_debug; |
| 1070 |
case HELP_COMMAND: handleHelpCommand(); break; |
| 1071 |
case INSTANCES_COMMAND: handleInstancesCommand(); break; |
| 1072 |
case TRACE_COMMAND: handleTraceCommand(); break; |
| 1073 |
case INSTRUCTION_TRACE_COMMAND: toggleInstructionTrace(); break; |
| 1074 |
case LOCATIONS_COMMAND: handleLocationsCommand(); break; |
| 1075 |
case NEXT_COMMAND: handleNextCommand(calledFromBreakpoint); goto exit_debug; |
| 1076 |
case OBJECTS_COMMAND: handleObjectsCommand(); break; |
| 1077 |
case QUIT_COMMAND: terminate(0); break; |
| 1078 |
case SECTION_TRACE_COMMAND: toggleSectionTrace(); break; |
| 1079 |
default: output("Unknown ADBG command. ? for help."); break; |
| 1080 |
} |
| 1081 |
} |
| 1082 |
|
| 1083 |
exit_debug: |
| 1084 |
#ifdef HAVE_GLK |
| 1085 |
glk_set_style(style_Normal); |
| 1086 |
#endif |
| 1087 |
; |
| 1088 |
} |
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
void traceSay(int item) |
| 1099 |
{ |
| 1100 |
saveInfo(); |
| 1101 |
needSpace = false; |
| 1102 |
col = 1; |
| 1103 |
if (item == 0) |
| 1104 |
printf("$null$"); |
| 1105 |
else |
| 1106 |
say(item); |
| 1107 |
needSpace = false; |
| 1108 |
col = 1; |
| 1109 |
restoreInfo(); |
| 1110 |
} |