1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
|
/*
* File: dbg-asrt.cc
* Summary: Assertions and crashing.
* Written by: Linley Henzell and Jesse Jones
*/
#include "AppHdr.h"
#include "debug.h"
#include <errno.h>
#include "clua.h"
#include "coord.h"
#include "coordit.h"
#include "crash.h"
#include "dbg-crsh.h"
#include "dbg-scan.h"
#include "dbg-util.h"
#include "directn.h"
#include "dlua.h"
#include "dungeon.h"
#include "env.h"
#include "initfile.h"
#include "jobs.h"
#include "libutil.h"
#include "mapmark.h"
#include "message.h"
#include "monster.h"
#include "mon-util.h"
#include "options.h"
#include "religion.h"
#include "spl-cast.h"
#include "spl-util.h"
#include "state.h"
#include "travel.h"
#include "hiscores.h"
#ifdef ASSERTS
static std::string _assert_msg;
#endif
static void _dump_compilation_info(FILE* file)
{
std::string comp_info = compilation_info();
if (!comp_info.empty())
{
fprintf(file, "Compilation info:\n");
fprintf(file, "<<<<<<<<<<<\n");
fprintf(file, "%s", comp_info.c_str());
fprintf(file, ">>>>>>>>>>>\n\n");
}
}
static void _dump_level_info(FILE* file)
{
CrawlHashTable &props = env.properties;
fprintf(file, "Place info:\n");
fprintf(file, "absdepth0 = %d, branch = %d, level_type = %d, "
"type_name = %s\n\n",
you.absdepth0, (int) you.where_are_you, (int) you.level_type,
you.level_type_name.c_str());
std::string place = level_id::current().describe();
std::string orig_place;
if (!props.exists(LEVEL_ID_KEY))
orig_place = "ABSENT";
else
orig_place = props[LEVEL_ID_KEY].get_string();
fprintf(file, "Level id: %s\n", place.c_str());
if (place != orig_place)
fprintf(file, "Level id when level was generated: %s\n",
orig_place.c_str());
debug_dump_levgen();
}
static void _dump_player(FILE *file)
{
// Only dump player info during arena mode if the player is likely
// the cause of the crash.
if ((crawl_state.game_is_arena() || crawl_state.arena_suspended)
&& !in_bounds(you.pos()) && you.hp > 0 && you.hp_max > 0
&& you.strength() > 0 && you.intel() > 0 && you.dex() > 0)
{
// Arena mode can change behavior of the rest of the code and/or lead
// to asserts.
crawl_state.type = GAME_TYPE_NORMAL;
crawl_state.arena_suspended = false;
return;
}
// Arena mode can change behavior of the rest of the code and/or lead
// to asserts.
crawl_state.arena_suspended = false;
fprintf(file, "Player:\n");
fprintf(file, "{{{{{{{{{{{\n");
bool name_overrun = true;
for (int i = 0; i < 30; ++i)
{
if (you.class_name[i] == '\0')
{
name_overrun = false;
break;
}
}
if (name_overrun)
{
fprintf(file, "job_name runs past end of buffer.\n");
you.class_name[29] = '\0';
}
fprintf(file, "Name: [%s]\n", you.your_name.c_str());
fprintf(file, "Species: %s\n", species_name(you.species).c_str());
fprintf(file, "Job: %s\n\n", get_job_name(you.char_class));
fprintf(file, "class_name: %s\n\n", you.class_name);
fprintf(file, "HP: %d/%d; base: %d/%d\n", you.hp, you.hp_max,
you.base_hp, you.base_hp2);
fprintf(file, "MP: %d/%d; base: %d/%d\n",
you.magic_points, you.max_magic_points,
you.base_magic_points, you.base_magic_points2);
fprintf(file, "Stats: %d (%d) %d (%d) %d (%d)\n",
you.strength(), you.max_strength(), you.intel(), you.max_intel(),
you.dex(), you.max_dex());
fprintf(file, "Position: %s, god:%s (%d), turn_is_over: %d, "
"banished: %d\n",
debug_coord_str(you.pos()).c_str(),
god_name(you.religion).c_str(), (int) you.religion,
(int) you.turn_is_over, (int) you.banished);
if (in_bounds(you.pos()))
{
const dungeon_feature_type feat = grd(you.pos());
fprintf(file, "Standing on/in/over feature: %s\n",
raw_feature_description(feat, NUM_TRAPS, true).c_str());
}
fprintf(file, "\n");
if (you.running.runmode != RMODE_NOT_RUNNING)
{
fprintf(file, "Runrest:\n");
fprintf(file, " mode: %d\n", you.running.runmode);
fprintf(file, " mp: %d\n", you.running.mp);
fprintf(file, " hp: %d\n", you.running.hp);
fprintf(file, " pos: %s\n\n",
debug_coord_str(you.running.pos).c_str());
}
if (you.delay_queue.size() > 0)
{
fprintf(file, "Delayed (%u):\n",
(unsigned int)you.delay_queue.size());
for (unsigned int i = 0; i < you.delay_queue.size(); ++i)
{
const delay_queue_item &item = you.delay_queue[i];
fprintf(file, " type: %d", item.type);
if (item.type <= DELAY_NOT_DELAYED
|| item.type >= NUM_DELAYS)
{
fprintf(file, " <invalid>");
}
fprintf(file, "\n");
fprintf(file, " duration: %d\n", item.duration);
fprintf(file, " parm1: %d\n", item.parm1);
fprintf(file, " parm2: %d\n", item.parm2);
fprintf(file, " started: %d\n\n", (int) item.started);
}
fprintf(file, "\n");
}
fprintf(file, "Spell bugs:\n");
for (size_t i = 0; i < you.spells.size(); ++i)
{
const spell_type spell = you.spells[i];
if (spell == SPELL_NO_SPELL)
continue;
if (!is_valid_spell(spell))
{
fprintf(file, " spell slot #%d: invalid spell #%d\n",
(int)i, (int)spell);
continue;
}
const unsigned int flags = get_spell_flags(spell);
if (flags & SPFLAG_MONSTER)
fprintf(file, " spell slot #%d: monster only spell %s\n",
(int)i, spell_title(spell));
else if (flags & SPFLAG_TESTING)
fprintf(file, " spell slot #%d: testing spell %s\n",
(int)i, spell_title(spell));
else if (count_bits(get_spell_disciplines(spell)) == 0)
fprintf(file, " spell slot #%d: school-less spell %s\n",
(int)i, spell_title(spell));
}
fprintf(file, "\n");
fprintf(file, "Durations:\n");
for (int i = 0; i < NUM_DURATIONS; ++i)
if (you.duration[i] != 0)
fprintf(file, " #%d: %d\n", i, you.duration[i]);
fprintf(file, "\n");
fprintf(file, "Attributes:\n");
for (int i = 0; i < NUM_ATTRIBUTES; ++i)
if (you.attribute[i] != 0)
fprintf(file, " #%d: %d\n", i, you.attribute[i]);
fprintf(file, "\n");
fprintf(file, "Mutations:\n");
for (int i = 0; i < NUM_MUTATIONS; ++i)
if (you.mutation[i] > 0)
fprintf(file, " #%d: %d\n", i, you.mutation[i]);
fprintf(file, "\n");
fprintf(file, "Demon mutations:\n");
for (int i = 0; i < NUM_MUTATIONS; ++i)
if (you.innate_mutations[i] > 0)
fprintf(file, " #%d: %d\n", i, you.innate_mutations[i]);
fprintf(file, "\n");
fprintf(file, "Inventory bugs:\n");
for (int i = 0; i < ENDOFPACK; ++i)
{
item_def &item(you.inv[i]);
if (item.base_type == OBJ_UNASSIGNED && item.quantity != 0)
{
fprintf(file, " slot #%d: unassigned item has quant %d\n",
i, item.quantity);
continue;
}
else if (item.base_type != OBJ_UNASSIGNED && item.quantity < 1)
{
const int orig_quant = item.quantity;
item.quantity = 1;
fprintf(file, " slot #%d: otherwise valid item '%s' has "
"invalid quantity %d\n",
i, item.name(DESC_PLAIN, false, true).c_str(),
orig_quant);
item.quantity = orig_quant;
continue;
}
else if (!item.defined())
continue;
const std::string name = item.name(DESC_PLAIN, false, true);
if (item.link != i)
{
fprintf(file, " slot #%d: item '%s' has invalid link %d\n",
i, name.c_str(), item.link);
}
if (item.slot < 0 || item.slot > 127)
{
fprintf(file, " slot #%d: item '%s' has invalid slot %d\n",
i, name.c_str(), item.slot);
}
if (!item.pos.equals(-1, -1))
{
fprintf(file, " slot #%d: item '%s' has invalid pos %s\n",
i, name.c_str(), debug_coord_str(item.pos).c_str());
}
}
fprintf(file, "\n");
fprintf(file, "Equipment:\n");
for (int i = 0; i < NUM_EQUIP; ++i)
{
char eq = you.equip[i];
if (eq == -1)
continue;
fprintf(file, " eq slot #%d, inv slot #%d", i, (int) eq);
if (eq < 0 || eq >= ENDOFPACK)
{
fprintf(file, " <invalid>\n");
continue;
}
fprintf(file, ": %s\n",
you.inv[eq].name(DESC_PLAIN, false, true).c_str());
}
fprintf(file, "\n");
if (in_bounds(you.pos()) && monster_at(you.pos()))
{
fprintf(file, "Standing on same square as: ");
const unsigned short midx = mgrd(you.pos());
if (invalid_monster_index(midx))
fprintf(file, "invalid monster index %d\n", (int) midx);
else
{
const monsters *mon = &menv[midx];
fprintf(file, "%s:\n", debug_mon_str(mon).c_str());
debug_dump_mon(mon, true);
}
fprintf(file, "\n");
}
fprintf(file, "}}}}}}}}}}}\n\n");
}
static void _debug_marker_scan()
{
std::vector<map_marker*> markers = env.markers.get_all();
for (unsigned int i = 0; i < markers.size(); ++i)
{
map_marker* marker = markers[i];
if (marker == NULL)
{
mprf(MSGCH_ERROR, "Marker #%d is NULL", i);
continue;
}
map_marker_type type = marker->get_type();
if (type < MAT_FEATURE || type >= NUM_MAP_MARKER_TYPES)
{
mprf(MSGCH_ERROR, "Makrer #%d at (%d, %d) has invalid type %d",
i, marker->pos.x, marker->pos.y, (int) type);
}
if (!in_bounds(marker->pos))
{
mprf(MSGCH_ERROR, "Marker #%d, type %d at (%d, %d) out of bounds",
i, (int) type, marker->pos.x, marker->pos.y);
continue;
}
bool found = false;
std::vector<map_marker*> at_pos
= env.markers.get_markers_at(marker->pos);
for (unsigned int j = 0; j < at_pos.size(); ++j)
{
map_marker* tmp = at_pos[j];
if (tmp == NULL)
continue;
if (tmp == marker)
{
found = true;
break;
}
}
if (!found)
mprf(MSGCH_ERROR, "Marker #%d, type %d at (%d, %d) unlinked",
i, (int) type, marker->pos.x, marker->pos.y);
}
const coord_def start(MAPGEN_BORDER, MAPGEN_BORDER);
const coord_def end(GXM - MAPGEN_BORDER - 2, GYM - MAPGEN_BORDER - 2);
for (rectangle_iterator ri(start, end); ri; ++ri)
{
std::vector<map_marker*> at_pos = env.markers.get_markers_at(*ri);
for (unsigned int i = 0; i < at_pos.size(); ++i)
{
map_marker *marker = at_pos[i];
if (marker == NULL)
{
mprf(MSGCH_ERROR, "Marker #%d at (%d, %d) NULL",
i, ri->x, ri->y);
continue;
}
if (marker->pos != *ri)
{
mprf(MSGCH_ERROR, "Marker #%d, type %d at (%d, %d) "
"thinks it's at (%d, %d)",
i, (int) marker->get_type(), ri->x, ri->y,
marker->pos.x, marker->pos.y);
if (!in_bounds(marker->pos))
{
mpr("Further, it thinks it's out of bounds.",
MSGCH_ERROR);
}
}
}
}
} // _debug_marker_scan()
static void _debug_dump_markers()
{
std::vector<map_marker*> markers = env.markers.get_all();
for (unsigned int i = 0; i < markers.size(); ++i)
{
map_marker* marker = markers[i];
if (marker == NULL || marker->get_type() == MAT_LUA_MARKER)
continue;
mprf(MSGCH_DIAGNOSTICS, "Marker %d at (%d, %d): %s",
i, marker->pos.x, marker->pos.y,
marker->debug_describe().c_str());
}
}
static void _debug_dump_lua_markers(FILE *file)
{
std::vector<map_marker*> markers = env.markers.get_all();
for (unsigned int i = 0; i < markers.size(); ++i)
{
map_marker* marker = markers[i];
if (marker == NULL || marker->get_type() != MAT_LUA_MARKER)
continue;
map_lua_marker* lua_marker = dynamic_cast<map_lua_marker*>(marker);
std::string result = lua_marker->debug_to_string();
if (result.size() > 0 && result[result.size() - 1] == '\n')
result = result.substr(0, result.size() - 1);
fprintf(file, "Lua marker %d at (%d, %d):\n",
i, marker->pos.x, marker->pos.y);
fprintf(file, "{{{{\n");
fprintf(file, "%s", result.c_str());
fprintf(file, "}}}}\n");
}
}
static void _debug_dump_lua_persist(FILE* file)
{
lua_stack_cleaner cln(dlua);
std::string result;
if (!dlua.callfn("persist_to_string", 0, 1))
result = make_stringf("error (persist_to_string): %s",
dlua.error.c_str());
else if (lua_isstring(dlua, -1))
result = lua_tostring(dlua, -1);
else
result = "persist_to_string() returned nothing";
fprintf(file, "%s", result.c_str());
}
static void _dump_ver_stuff(FILE* file)
{
fprintf(file, "Version: %s %s\n", CRAWL, Version::Long().c_str());
#if defined(UNIX)
fprintf(file, "Platform: unix");
# if defined(TARGET_OS_MACOSX)
fprintf(file, " (OS X)");
# endif
fprintf(file, "\n");
#elif defined(TARGET_OS_WINDOWS)
fprintf(file, "Platform: Windows\n");
#elif defined(TARGET_OS_DOS)
fprintf(file, "Platform: DOS\n");
#endif // UNIX
fprintf(file, "Bits: %d\n", (int)sizeof(void*)*8);
fprintf(file, "Game mode: %s\n",
gametype_to_str(crawl_state.type).c_str());
#ifdef USE_TILE
fprintf(file, "Tiles: yes\n\n");
#else
fprintf(file, "Tiles: no\n\n");
#endif
}
static void _dump_command_line(FILE *file)
{
fprintf(file, "Command line:");
for (int i = 0, size = crawl_state.command_line_arguments.size();
i < size; ++i)
{
fprintf(file, " %s", crawl_state.command_line_arguments[i].c_str());
}
if (crawl_state.command_line_arguments.empty())
fprintf(file, " (unknown)");
fprintf(file, "\n\n");
}
// Dump any game options that could affect stability.
static void _dump_options(FILE *file)
{
fprintf(file, "RC options:\n");
fprintf(file, "restart_after_game = %s\n",
Options.restart_after_game? "true" : "false");
fprintf(file, "\n\n");
}
// Defined in stuff.cc. Not a part of crawl_state, since that's a
// global C++ instance which is free'd by exit() hooks when exit()
// is called, and we don't want to reference free'd memory.
extern bool CrawlIsExiting;
void do_crash_dump()
{
if (CrawlIsExiting)
{
// We crashed during exit() callbacks, so it's likely that
// any global C++ instances we could reference would be
// free'd and invalid, plus their content likely wouldn't help
// tracking it down anyway. Thus, just do the bare bones
// info to stderr and quit.
fprintf(stderr, "Crashed while calling exit()!!!!\n");
_dump_ver_stuff(stderr);
dump_crash_info(stderr);
write_stack_trace(stderr, 0);
return;
}
std::string dir = (!Options.morgue_dir.empty() ? Options.morgue_dir :
!SysEnv.crawl_dir.empty() ? SysEnv.crawl_dir
: "");
if (!dir.empty() && dir[dir.length() - 1] != FILE_SEPARATOR)
dir += FILE_SEPARATOR;
char name[180];
snprintf(name, sizeof(name), "%scrash-%s-%s.txt", dir.c_str(),
you.your_name.c_str(), make_file_time(time(NULL)).c_str());
fprintf(stderr, "\nWriting crash info to %s\n", name);
errno = 0;
FILE* file = crawl_state.test ? stderr : freopen(name, "w+", stderr);
if (file == NULL || errno != 0)
{
fprintf(stdout, "\nUnable to open file '%s' for writing: %s\n",
name, strerror(errno));
file = stdout;
}
// Unbuffer the file, since if we recursively crash buffered lines
// won't make it to the file.
setvbuf(file, NULL, _IONBF, 0);
set_msg_dump_file(file);
#ifdef ASSERTS
if (!_assert_msg.empty())
fprintf(file, "%s\n\n", _assert_msg.c_str());
#endif
_dump_ver_stuff(file);
_dump_command_line(file);
_dump_options(file);
// First get the immediate cause of the crash and the stack trace,
// since that's most important and later attempts to get more information
// might themselves cause crashes.
dump_crash_info(file);
write_stack_trace(file, 0);
fprintf(file, "\n");
// Next information on how the binary was compiled
_dump_compilation_info(file);
// Next information about the level the player is on, plus level
// generation info if the crash happened during level generation.
_dump_level_info(file);
// Dumping information on marker inconsistancy is unlikely to crash,
// as is dumping the descriptions of non-Lua markers.
fprintf(file, "Markers:\n");
fprintf(file, "<<<<<<<<<<<<<<<<<<<<<<\n");
_debug_marker_scan();
_debug_dump_markers();
fprintf(file, ">>>>>>>>>>>>>>>>>>>>>>\n");
// Dumping current messages is unlikely to crash.
if (file != stdout)
{
fprintf(file, "\nMessages:\n");
fprintf(file, "<<<<<<<<<<<<<<<<<<<<<<\n");
std::string messages = get_last_messages(NUM_STORED_MESSAGES);
fprintf(file, "%s", messages.c_str());
fprintf(file, ">>>>>>>>>>>>>>>>>>>>>>\n");
}
// Dumping the player state and crawl state is next least likely to cause
// another crash, so do that next.
crawl_state.dump();
_dump_player(file);
// Next item and monster scans. Any messages will be sent straight to
// the file because of set_msg_dump_file()
#ifdef DEBUG_ITEM_SCAN
debug_item_scan();
#endif
#ifdef DEBUG_MONS_SCAN
debug_mons_scan();
#endif
// If anything has screwed up the Lua runtime stacks then trying to
// print those stacks will likely crash, so do this after the others.
fprintf(file, "clua stack:\n");
clua.print_stack();
fprintf(file, "dlua stack:\n");
dlua.print_stack();
// Lastly try to dump the Lua persistent data and the contents of the Lua
// markers, since actually running Lua code has the greatest chance of
// crashing.
fprintf(file, "Lua persistent data:\n");
fprintf(file, "<<<<<<<<<<<<<<<<<<<<<<\n");
_debug_dump_lua_persist(file);
fprintf(file, ">>>>>>>>>>>>>>>>>>>>>>\n\n");
fprintf(file, "Lua marker contents:\n");
fprintf(file, "<<<<<<<<<<<<<<<<<<<<<<\n");
_debug_dump_lua_markers(file);
fprintf(file, ">>>>>>>>>>>>>>>>>>>>>>\n");
set_msg_dump_file(NULL);
#ifdef ASSERTS
mark_milestone("crash", _assert_msg);
#endif
if (file != stderr)
fclose(file);
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// Assertions and such
#ifdef ASSERTS
//---------------------------------------------------------------
// BreakStrToDebugger
//---------------------------------------------------------------
static void _BreakStrToDebugger(const char *mesg)
{
#if defined(TARGET_OS_MACOSX) || defined(TARGET_COMPILER_MINGW)
fprintf(stderr, "%s", mesg);
// raise(SIGINT); // this is what DebugStr() does on OS X according to Tech Note 2030
int* p = NULL; // but this gives us a stack crawl...
*p = 0;
#else
fprintf(stderr, "%s\n", mesg);
abort();
#endif
}
//---------------------------------------------------------------
//
// AssertFailed
//
//---------------------------------------------------------------
void AssertFailed(const char *expr, const char *file, int line)
{
char mesg[512];
const char *fileName = file + strlen(file); // strip off path
while (fileName > file && fileName[-1] != '\\')
--fileName;
snprintf(mesg, sizeof(mesg), "ASSERT(%s) in '%s' at line %d failed.",
expr, fileName, line);
_assert_msg = mesg;
_BreakStrToDebugger(mesg);
}
//---------------------------------------------------------------
//
// DEBUGSTR
//
//---------------------------------------------------------------
void DEBUGSTR(const char *format, ...)
{
char mesg[2048];
va_list args;
va_start(args, format);
vsnprintf(mesg, sizeof(mesg), format, args);
va_end(args);
_BreakStrToDebugger(mesg);
}
#endif
|