Source:NetHack 5.0.0/src/report.c
Jump to navigation
Jump to search
Below is the full text to report.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/report.c#line12]], for example.
The NetHack General Public License applies to screenshots, source code and other content from NetHack.
This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.
1. /* NetHack 5.0 report.c $NHDT-Date: 1777240823 2026/04/26 22:00:23 $ $NHDT-Branch: to500 $:$NHDT-Revision: 1.19 $ */ 2. /* Copyright (c) Kenneth Lorber, Kensington, Maryland, 2024 */ 3. /* NetHack may be freely redistributed. See license for details. */ 4. 5. #include "hack.h" 6. 7. /* NB: CRASHREPORT implies PANICTRACE */ 8. 9. # ifndef NO_SIGNAL 10. #include <signal.h> 11. # endif 12. # ifndef LONG_MAX 13. #include <limits.h> 14. # endif 15. #include "dlb.h" 16. #include <fcntl.h> 17. 18. #include <errno.h> 19. # ifdef PANICTRACE_LIBC 20. #include <execinfo.h> 21. # endif 22. 23. #ifdef CRASHREPORT 24. # ifdef WIN32 25. # define HASH_PRAGMA_START 26. # define HASH_PRAGMA_END 27. # else 28. # define HASH_PRAGMA_START \ 29. _Pragma("GCC diagnostic push"); \ 30. _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") 31. # define HASH_PRAGMA_END _Pragma("GCC diagnostic pop"); 32. # endif 33. # ifdef MACOS 34. #include <CommonCrypto/CommonDigest.h> 35. # define HASH_CONTEXTPTR(CTXP) \ 36. unsigned char tmp[CC_MD4_DIGEST_LENGTH]; \ 37. CC_MD4_CTX CTXP ## _; \ 38. CC_MD4_CTX *CTXP = &CTXP ## _ 39. # define HASH_INIT(ctxp) !CC_MD4_Init(ctxp) 40. # define HASH_UPDATE(ctx, ptr, len) !CC_MD4_Update(ctx, ptr, len) 41. # define HASH_FINISH(ctxp) !CC_MD4_Final(tmp, ctxp) 42. # define HASH_RESULT_SIZE(ctxp) CC_MD4_DIGEST_LENGTH 43. # define HASH_RESULT(ctx, inp) *inp = (unsigned char *) ctx 44. # define HASH_CLEANUP(ctxp) 45. # define HASH_OFLAGS O_RDONLY 46. # define HASH_BINFILE_DECL char *binfile = argv[0]; 47. # if (NH_DEVEL_STATUS == NH_STATUS_BETA) 48. # define HASH_BINFILE() \ 49. if (!binfile || !*binfile) { \ 50. /* If this triggers, investigate CFBundleGetMainBundle */ \ 51. /* or CFBundleCopyExecutableURL. */ \ 52. raw_print( \ 53. "BETA warning: crashreport_init called without useful info"); \ 54. goto skip; \ 55. } 56. # else /* BETA */ 57. # define HASH_BINFILE() \ 58. if (!binfile || !*binfile) { \ 59. goto skip; \ 60. } 61. # endif /* BETA */ 62. # endif /* MACOS */ 63. 64. # ifdef __linux__ 65. #include "nhmd4.h" 66. /* v0 is just to suppress compiler warnings about unreachable code */ 67. # define HASH_CONTEXTPTR(CTXP) \ 68. volatile int v0 = 0; \ 69. unsigned char tmp[NHMD4_DIGEST_LENGTH]; \ 70. NHMD4_CTX CTXP ## _; \ 71. NHMD4_CTX *CTXP = &CTXP ## _ 72. # define HASH_INIT(ctxp) (nhmd4_init(ctxp), v0) 73. # define HASH_UPDATE(ctx, ptr, len) (nhmd4_update(ctx, ptr, len), v0) 74. # define HASH_FINISH(ctxp) (nhmd4_final(ctxp, tmp), v0) 75. # define HASH_RESULT_SIZE(ctxp) NHMD4_RESULTLEN 76. # define HASH_RESULT(ctx, inp) *inp = tmp 77. # define HASH_CLEANUP(ctxp) 78. # define HASH_OFLAGS O_RDONLY 79. # define HASH_BINFILE_DECL char binfile[PATH_MAX+1]; 80. # define HASH_BINFILE() \ 81. int len = readlink("/proc/self/exe", binfile, sizeof binfile - 1); \ 82. if (len > 0) { \ 83. binfile[len] = '\0'; \ 84. } else { \ 85. goto skip; \ 86. } 87. # endif // __linux__ 88. 89. # ifdef WIN32 90. /* WIN32 takes too much code and is dependent on OS includes we can't 91. * pull in here, so we call out to code in sys/windows/windsys.c */ 92. # define HASH_CONTEXTPTR(CTXP) 93. # define HASH_INIT(ctxp) win32_cr_helper('i', ctxp, NULL, 0) 94. # define HASH_UPDATE(ctxp, ptr, len) win32_cr_helper('u', ctxp, ptr, len) 95. # define HASH_FINISH(ctxp) win32_cr_helper('f', ctxp, NULL, 0) 96. # define HASH_CLEANUP(ctxp) win32_cr_helper('c', ctxp, NULL, 0) 97. # define HASH_RESULT_SIZE(ctxp) win32_cr_helper('s', ctxp, NULL, 0) 98. # define HASH_RESULT(ctxp, inp) win32_cr_helper('r', ctxp, inp, 0) 99. # define HASH_OFLAGS _O_RDONLY | _O_BINARY 100. # define HASH_BINFILE_DECL char *binfile; 101. # define HASH_BINFILE() \ 102. if (win32_cr_helper('b', NULL, &binfile, 0)) { \ 103. goto skip; \ 104. } 105. # endif // WIN32 106. 107. /* Binary ID - Use only as a hint to contact.html for recognizing our own 108. binaries. This is easily spoofed! */ 109. static char bid[40]; 110. 111. /* ARGSUSED */ 112. void 113. crashreport_init(int argc, char *argv[]) 114. { 115. static int once = 0; 116. if (once++) /* NetHackW.exe calls us twice */ 117. return; 118. HASH_BINFILE_DECL; 119. HASH_PRAGMA_START 120. HASH_CONTEXTPTR(ctxp); 121. if (HASH_INIT(ctxp)) 122. goto skip; 123. HASH_BINFILE(); /* Does "goto skip" on error. */ 124. 125. int fd = open(binfile, HASH_OFLAGS, 0); 126. if (fd == -1) { 127. # ifdef BETA 128. raw_printf("open e=%s", strerror(errno)); 129. # endif 130. goto skip; 131. } 132. 133. int segsize; 134. unsigned char segment[4096]; 135. 136. while (0 < (segsize = read(fd, segment, sizeof segment))) { 137. if (HASH_UPDATE(ctxp, segment, segsize)) 138. goto skip; 139. } 140. if (segsize < 0) { 141. close(fd); 142. goto skip; 143. } 144. if (HASH_FINISH(ctxp)) 145. goto skip; 146. close(fd); 147. 148. static const char hexdigits[] = "0123456789abcdef"; 149. char *p = bid; 150. unsigned char *in; 151. HASH_RESULT(ctxp, &in); 152. uint8 cnt = (uint8) HASH_RESULT_SIZE(ctxp); 153. /* Just in case, make sure not to overflow the bid buffer. 154. Divide size by 2 because each octet in the hash uses two slots 155. in bid[] when formatted as a pair of hexadecimal digits. */ 156. if (cnt >= (uint8) sizeof bid / 2) 157. cnt = (uint8) sizeof bid / 2 - 1; 158. while (cnt) { 159. /* sprintf(p, "%02x", *in++), p += 2; */ 160. *p++ = hexdigits[(*in >> 4) & 0x0f]; 161. *p++ = hexdigits[*in++ & 0x0f]; 162. --cnt; 163. } 164. *p = '\0'; 165. HASH_CLEANUP(ctxp); 166. return; 167. 168. skip: 169. Strcpy(bid, "unknown"); 170. HASH_CLEANUP(ctxp); 171. HASH_PRAGMA_END 172. nhUse(argc); 173. nhUse(argv); 174. } 175. 176. #undef HASH_CONTEXTPTR 177. #undef HASH_INIT 178. #undef HASH_UPDATE 179. #undef HASH_FINISH 180. #undef HASH_CLEANUP 181. #undef HASH_RESULT 182. #undef HASH_RESULT_SIZE 183. #undef HASH_PRAGMA_START 184. #undef HASH_PRAGMA_END 185. #undef HASH_BINFILE_DECL 186. #undef HASH_BINFILE 187. 188. void 189. crashreport_bidshow(void) 190. { 191. # if defined(WIN32) && !defined(WIN32CON) 192. if (0 == win32_cr_helper('D', ctxp, bid, 0)) 193. # endif 194. { 195. raw_print(bid); 196. # ifdef WIN32notyet 197. wait_synch(); 198. # endif 199. } 200. } 201. 202. /* Build a URL with a query string and try to launch a new browser window 203. * to report from panic() or impossible(). Requires libc support for 204. * the stacktrace. Uses memory on the stack to avoid memory allocation 205. * (on most platforms) (but libc can still do anything it wants). */ 206. 207. // No theoretial limit for URL length but reality is messy. 208. // This should work on all modern platforms. 209. # ifndef MAX_URL 210. # define MAX_URL 8192 211. # endif 212. # ifndef SWR_FRAMES 213. # define SWR_FRAMES 20 214. # endif 215. 216. // mark holds the initial eos; if we can't get the value in 217. // then we can remove the whole item if desired. For other 218. // semantics, caller can handle mark. 219. #define SWR_ADD(str) \ 220. utmp = strlen(str); \ 221. mark = uend; \ 222. if (utmp >= urem) \ 223. goto full; \ 224. memcpy(uend, str, utmp); \ 225. uend += utmp; urem -= utmp; \ 226. *uend = '\0'; 227. 228. // NB: on overflow this rolls us back to mark, so if we don't 229. // want to roll back to the last SWR_ADD, update mark before 230. // calling this macro. 231. #define SWR_ADD_URIcoded(str) \ 232. if (swr_add_uricoded(str, &uend, &urem, mark)) \ 233. goto full; 234. 235. /* On overflow, truncate to markp (but only if markp != NULL). */ 236. boolean 237. swr_add_uricoded( 238. const char *in, 239. char **out, 240. int *remaining, 241. char *markp) 242. { 243. while (*in) { 244. if (isalnum(*in) || strchr("_-.~", *in)) { 245. **out = *in; 246. (*out)++; 247. (*remaining)--; 248. } else if (*in == ' ') { 249. **out = '+'; 250. (*out)++; 251. (*remaining)--; 252. } else { 253. if (*remaining <= 3) { 254. if (markp) 255. *out = markp, *remaining = 0; 256. **out = '\0'; 257. return TRUE; 258. } else { 259. char chr[40]; /* [4] should suffice */ 260. int x; 261. 262. Sprintf(chr, "%%%02X", *in); 263. x = (int) strlen(chr); 264. if (x <= *remaining) { 265. Strcpy(*out, chr); 266. *out += x; 267. *remaining -= x; 268. } 269. } 270. } 271. in++; 272. if (!*remaining) { 273. if (markp) 274. *out = markp, *remaining = 0; 275. **out = '\0'; 276. return TRUE; 277. } 278. **out = '\0'; 279. } 280. return FALSE; /* normal return */ 281. } 282. 283. static char url[MAX_URL]; // XXX too bad this isn't allocated as needed 284. static int urem = MAX_URL; // adjusted for gc.crash_urlmax below 285. static char *uend = url; 286. static int utmp; // used inside macros 287. static char *mark; // holds previous terminator (generally) 288. 289. boolean 290. submit_web_report(int cos, const char *msg, const char *why) 291. { 292. urem = (gc.crash_urlmax < 0 || gc.crash_urlmax > MAX_URL) 293. ? MAX_URL : min(MAX_URL,gc.crash_urlmax); 294. char temp[200]; 295. char temp2[200]; 296. int countpp = 0; /* pre and post traceback lines */ 297. // URL loaded for creating reports to the NetHack DevTeam 298. // CRASHREPORTURL=https://nethack.org/links/cr-5.0.0.html 299. if (!sysopt.crashreporturl) 300. return FALSE; 301. SWR_ADD(sysopt.crashreporturl); 302. /* 303. * Note: all snprintf() calls here changed to sprintf() to avoid 304. * complaints from static analyzer. All but one were unnecessary 305. * since they were formatting int or unsigned into a large buffer. 306. */ 307. /* cos - operation, v - version */ 308. Sprintf(temp, "?cos=%d&v=1", cos); 309. SWR_ADD(temp); 310. 311. /* msg==NULL for #bugreport */ 312. if (msg) { 313. SWR_ADD("&subject="); 314. Sprintf(temp, "%.40s report for NetHack %.40s", 315. msg, version_string(temp2, sizeof temp2 )); 316. SWR_ADD_URIcoded(temp); 317. } 318. 319. SWR_ADD("&gitver="); 320. SWR_ADD_URIcoded(getversionstring(temp2, sizeof temp2)); 321. 322. if (gc.crash_name) { 323. SWR_ADD("&name="); 324. SWR_ADD_URIcoded(gc.crash_name); 325. } 326. 327. if(gc.crash_email) { 328. SWR_ADD("&email="); 329. SWR_ADD_URIcoded(gc.crash_email); 330. } 331. // hardware: leave for user 332. // software: leave for user 333. // comments: leave for user 334. 335. SWR_ADD("&details="); 336. if (why) { 337. SWR_ADD_URIcoded(why); 338. SWR_ADD_URIcoded("\n"); 339. mark = uend; 340. countpp++; 341. } 342. 343. SWR_ADD_URIcoded("bid: "); 344. SWR_ADD_URIcoded(bid); 345. SWR_ADD_URIcoded("\n"); 346. mark = uend; 347. countpp++; 348. 349. int count = 0; 350. if (cos == 1) { 351. # ifdef WIN32 352. count = win32_cr_gettrace(SWR_FRAMES, uend, MAX_URL - (uend - url)); 353. uend = eos(url); 354. # else 355. void *bt[SWR_FRAMES]; 356. int x; 357. char **info; 358. 359. count = backtrace(bt, SIZE(bt)); 360. info = backtrace_symbols(bt, count); 361. for (x = 0; x < count; x++) { 362. copynchars(temp, info[x], (int) sizeof temp - 1 - 1); /* \n\0 */ 363. /* try to remove up to 16 blank spaces by removing 8 twice */ 364. (void) strsubst(temp, " ", ""); 365. (void) strsubst(temp, " ", ""); 366. (void) strncat(temp, "\n", sizeof temp - 1); 367. # if 0 // __linux__ 368. // not needed for MacOS 369. // XXX is it actually needed for linux? TBD 370. Sprintf(temp2, "[%02lu]\n", (unsigned long) x); 371. uend--; // remove the \n we added above 372. SWR_ADD_URIcoded(temp2); 373. # endif // linux 374. SWR_ADD_URIcoded(temp); 375. mark = uend; 376. } 377. # endif // !WIN32 378. } 379. 380. # ifdef DUMPLOG_CORE 381. // config.h turns this on, but make it easy to turn off if needed 382. if (cos == 1) { 383. int k; 384. SWR_ADD_URIcoded("Latest messages:\n"); 385. mark=uend; 386. countpp++; 387. for (k = 0; k < 5; k++) { 388. const char *line = get_saved_pline(k); 389. if (!line) 390. break; 391. SWR_ADD_URIcoded(line); 392. SWR_ADD_URIcoded("\n"); 393. countpp++; 394. mark = uend; 395. } 396. } 397. # endif /* DUMPLOG_CORE */ 398. 399. // detailrows: Guess since we can't know the 400. // width of the window. 401. SWR_ADD("&detailrows="); 402. Sprintf(temp, "%d", min(count + countpp, 30)); 403. SWR_ADD_URIcoded(temp); 404. 405. full: 406. ; 407. //printf("URL=%ld '%s'\n",strlen(url),url); 408. # ifdef WIN32 409. int *rv = win32_cr_shellexecute(url); 410. // XXX TESTING 411. printf("ShellExecute returned: %p\n",rv); // >32 is ok 412. # else /* !WIN32 */ 413. const char *xargv[] = { 414. CRASHREPORT, 415. url, 416. NULL 417. }; 418. int pid = fork(); 419. extern char **environ; 420. if (pid == 0) { 421. char err[400]; 422. # ifdef CRASHREPORT_EXEC_NOSTDERR 423. int devnull; 424. /* Keep the output clean - firefox spews useless errors on 425. * my system. */ 426. (void) close(2); 427. devnull = open("/dev/null", O_WRONLY); 428. # endif 429. 430. (void) execve(CRASHREPORT, (char * const *) xargv, environ); 431. Sprintf(err, "Can't start " CRASHREPORT ": %.*s", 432. (int) (sizeof err 433. - sizeof "Can't start " CRASHREPORT ": "), 434. strerror(errno)); 435. raw_print(err); 436. # ifdef CRASHREPORT_EXEC_NOSTDERR 437. (void) close(devnull); 438. # endif 439. exit(1); 440. } else { 441. int status; 442. errno = 0; 443. (void) waitpid(pid, &status, 0); 444. if (status) { /* XXX check could be more precise */ 445. # ifdef BETA 446. /* Not useful at the moment. XXX */ 447. char err[100]; 448. 449. Sprintf(err, "pid=%d e=%d status=%0x", wpid, errno, status); 450. raw_print(err); 451. # endif 452. return FALSE; 453. } 454. } 455. /* free(info); -- Don't risk it. */ 456. # endif /* !WIN32 */ 457. return TRUE; 458. } 459. 460. int 461. dobugreport(void) 462. { 463. if (!submit_web_report(2, NULL, "#bugreport command")) { 464. pline("Unable to send bug report. Please visit %s instead.", 465. (sysopt.crashreporturl && *sysopt.crashreporturl) 466. ? sysopt.crashreporturl 467. : DEVTEAM_URL 468. ); 469. } 470. return ECMD_OK; 471. } 472. 473. #undef SWR_ADD 474. #undef SWR_ADD_URIcoded 475. #undef SWR_FRAMES 476. #undef SWR_HDR 477. #undef SWR_LINES 478. 479. #endif /* CRASHREPORT */ 480. 481. #ifdef PANICTRACE 482. 483. /*ARGSUSED*/ 484. boolean 485. NH_panictrace_libc(void) 486. { 487. # if 0 /* XXX how did this get left here? */ 488. if (submit_web_report("Panic", why)) 489. return TRUE; 490. # endif 491. 492. # ifdef PANICTRACE_LIBC 493. void *bt[20]; 494. int count, x; 495. char **info, buf[BUFSZ]; 496. 497. raw_print(" Generating more information you may report:\n"); 498. count = backtrace(bt, SIZE(bt)); 499. info = backtrace_symbols(bt, count); 500. for (x = 0; x < count; x++) { 501. copynchars(buf, info[x], (int) sizeof buf - 1); 502. /* try to remove up to 16 blank spaces by removing 8 twice */ 503. (void) strsubst(buf, " ", ""); 504. (void) strsubst(buf, " ", ""); 505. raw_printf("[%02lu] %s", (unsigned long) x, buf); 506. } 507. /* free(info); -- Don't risk it. */ 508. return TRUE; 509. # else 510. return FALSE; 511. # endif /* !PANICTRACE_LIBC */ 512. } 513. 514. /* 515. * fooPATH file system path for foo 516. * fooVAR (possibly const) variable containing fooPATH 517. */ 518. # ifdef PANICTRACE_GDB 519. # ifdef SYSCF 520. # define GDBVAR sysopt.gdbpath 521. # define GREPVAR sysopt.greppath 522. # else /* SYSCF */ 523. # define GDBVAR GDBPATH 524. # define GREPVAR GREPPATH 525. # endif /* SYSCF */ 526. # endif /* PANICTRACE_GDB */ 527. 528. boolean 529. NH_panictrace_gdb(void) 530. { 531. # ifdef PANICTRACE_GDB 532. /* A (more) generic method to get a stack trace - invoke 533. * gdb on ourself. */ 534. const char *gdbpath = GDBVAR; 535. const char *greppath = GREPVAR; 536. char buf[BUFSZ]; 537. FILE *gdb; 538. 539. if (gdbpath == NULL || gdbpath[0] == 0) 540. return FALSE; 541. if (greppath == NULL || greppath[0] == 0) 542. return FALSE; 543. 544. Snprintf(buf, sizeof buf, "%s -n -q %s %d 2>&1 | %s '^#'", 545. gdbpath, ARGV0, getpid(), greppath); 546. gdb = popen(buf, "w"); 547. if (gdb) { 548. raw_print(" Generating more information you may report:\n"); 549. (void) fprintf(gdb, "bt\nquit\ny"); 550. (void) fflush(gdb); 551. sleep(4); /* ugly */ 552. (void) pclose(gdb); 553. return TRUE; 554. } else { 555. return FALSE; 556. } 557. # else 558. return FALSE; 559. # endif /* !PANICTRACE_GDB */ 560. } 561. 562. #ifdef DUMPLOG_CORE 563. #define USED_if_dumplog 564. #else 565. #define USED_if_dumplog UNUSED 566. #endif 567. 568. /* lineno==0 gives the most recent message (e.g. 569. "Do you want to call panic..." if called from #panic) */ 570. const char * 571. get_saved_pline(int lineno USED_if_dumplog) 572. { 573. #ifdef DUMPLOG_CORE 574. int p; 575. int limit = DUMPLOG_MSG_COUNT; 576. 577. if (lineno >= DUMPLOG_MSG_COUNT) 578. return NULL; 579. p = (gs.saved_pline_index - 1) % DUMPLOG_MSG_COUNT; 580. 581. while (limit--) { 582. if (gs.saved_plines[p]) { /* valid line */ 583. if (lineno--) { 584. p = (p - 1 + DUMPLOG_MSG_COUNT) % DUMPLOG_MSG_COUNT; 585. } else { 586. return gs.saved_plines[p]; 587. } 588. } 589. } 590. #endif /* DUMPLOG_CORE */ 591. return NULL; 592. } 593. 594. #undef USED_if_dumplog 595. 596. # ifndef NO_SIGNAL 597. /* called as signal() handler, so sent at least one arg */ 598. /*ARGUSED*/ 599. void 600. panictrace_handler(int sig_unused UNUSED) 601. { 602. #define SIG_MSG "\nSignal received.\n" 603. int f2; 604. 605. # ifdef CURSES_GRAPHICS 606. if (iflags.window_inited && WINDOWPORT(curses)) { 607. extern void curses_uncurse_terminal(void); /* wincurs.h */ 608. 609. /* it is risky calling this during a program-terminating signal, 610. but without it the subsequent backtrace is useless because 611. that ends up being scrawled all over the screen; call is 612. here rather than in NH_abort() because panic() calls both 613. exit_nhwindows(), which makes this same call under curses, 614. then NH_abort() and we don't want to call this twice */ 615. curses_uncurse_terminal(); 616. } 617. # endif 618. 619. f2 = (int) write(2, SIG_MSG, sizeof SIG_MSG - 1); 620. nhUse(f2); /* what could we do if write to fd#2 (stderr) fails */ 621. NH_abort(NULL); /* ... and we're already in the process of quitting? */ 622. } 623. 624. void 625. panictrace_setsignals(boolean set) 626. { 627. #define SETSIGNAL(sig) \ 628. (void) signal(sig, set ? (SIG_RET_TYPE) panictrace_handler : SIG_DFL); 629. # ifdef SIGILL 630. SETSIGNAL(SIGILL); 631. # endif 632. # ifdef SIGTRAP 633. SETSIGNAL(SIGTRAP); 634. # endif 635. # ifdef SIGIOT 636. SETSIGNAL(SIGIOT); 637. # endif 638. # ifdef SIGBUS 639. SETSIGNAL(SIGBUS); 640. # endif 641. # ifdef SIGFPE 642. SETSIGNAL(SIGFPE); 643. # endif 644. # ifdef SIGSEGV 645. SETSIGNAL(SIGSEGV); 646. # endif 647. # ifdef SIGSTKFLT 648. SETSIGNAL(SIGSTKFLT); 649. # endif 650. # ifdef SIGSYS 651. SETSIGNAL(SIGSYS); 652. # endif 653. # ifdef SIGEMT 654. SETSIGNAL(SIGEMT); 655. # endif 656. #undef SETSIGNAL 657. } 658. # endif /* NO_SIGNAL */ 659. #endif /* PANICTRACE */ 660. 661. /* 662. * FIXME: this should have a lot of '#undef's for onefile support. 663. */ 664. 665. /*report.c*/