debug.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2008 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*
  21. * debug.c -- handles:
  22. * signal handling
  23. * Context handling
  24. * debug funtions
  25. *
  26. */
  27. #include "common.h"
  28. #include "debug.h"
  29. #include "chanprog.h"
  30. #include "net.h"
  31. #include "shell.h"
  32. #include "color.h"
  33. #include "binary.h"
  34. #include "userrec.h"
  35. #include "main.h"
  36. #include "dccutil.h"
  37. #include <setjmp.h>
  38. #include <signal.h>
  39. #include <sys/types.h>
  40. #include <sys/stat.h>
  41. #include <fcntl.h>
  42. #include <sys/time.h>
  43. #include <sys/resource.h>
  44. #include <unistd.h>
  45. #include <stdarg.h>
  46. #include <errno.h>
  47. #include <sys/mman.h>
  48. bool sdebug = 0; /* enable debug output? */
  49. bool segfaulted = 0;
  50. #ifdef DEBUG_CONTEXT
  51. /* Context storage for fatal crashes */
  52. char cx_file[16][16];
  53. char cx_note[16][16];
  54. int cx_line[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  55. int cx_ptr = 0;
  56. #endif /* DEBUG_CONTEXT */
  57. void setlimits()
  58. {
  59. #ifndef CYGWIN_HACKS
  60. struct rlimit plim, fdlim, corelim;
  61. #ifndef DEBUG
  62. /* struct rsslim, stacklim;
  63. rsslim.rlim_cur = 30720;
  64. rsslim.rlim_max = 30720;
  65. setrlimit(RLIMIT_RSS, &rsslim);
  66. stacklim.rlim_cur = 30720;
  67. stacklim.rlim_max = 30720;
  68. setrlimit(RLIMIT_STACK, &stacklim);
  69. */
  70. /* do NOT dump a core. */
  71. plim.rlim_cur = 60;
  72. plim.rlim_max = 60;
  73. corelim.rlim_cur = 0;
  74. corelim.rlim_max = 0;
  75. #else /* DEBUG */
  76. plim.rlim_cur = 500;
  77. plim.rlim_max = 500;
  78. corelim.rlim_cur = RLIM_INFINITY;
  79. corelim.rlim_max = RLIM_INFINITY;
  80. #endif /* !DEBUG */
  81. setrlimit(RLIMIT_CORE, &corelim);
  82. #ifndef __sun__
  83. setrlimit(RLIMIT_NPROC, &plim);
  84. #endif
  85. fdlim.rlim_cur = 300;
  86. fdlim.rlim_max = 300;
  87. setrlimit(RLIMIT_NOFILE, &fdlim);
  88. #endif /* !CYGWIN_HACKS */
  89. }
  90. void init_debug()
  91. {
  92. for (int i = 0; i < 16; i ++)
  93. Context;
  94. #ifdef DEBUG_CONTEXT
  95. egg_bzero(&cx_file, sizeof cx_file);
  96. egg_bzero(&cx_note, sizeof cx_note);
  97. #endif /* DEBUG_CONTEXT */
  98. }
  99. void sdprintf (const char *format, ...)
  100. {
  101. if (sdebug) {
  102. char s[2001] = "";
  103. va_list va;
  104. va_start(va, format);
  105. egg_vsnprintf(s, sizeof(s), format, va);
  106. va_end(va);
  107. remove_crlf(s);
  108. if (!backgrd)
  109. dprintf(DP_STDOUT, "[D:%d] %s%s%s\n", mypid, BOLD(-1), s, BOLD_END(-1));
  110. else
  111. printf("[D:%d] %s%s%s\n", mypid, BOLD(-1), s, BOLD_END(-1));
  112. }
  113. }
  114. #ifdef NOTUSED
  115. char* hexize(const unsigned char* data, size_t len) {
  116. static char buffers[5][513] = { "", "", "", "", "" };
  117. static int n = 0;
  118. char *buf = buffers[n++];
  119. buf[0] = 0;
  120. for (size_t i = 0; i < len; ++i) {
  121. if (i == 0)
  122. sprintf(buf, "%.2X", (int) (data[i]));
  123. else
  124. sprintf(buf, "%s %.2X", buf, (int) (data[i]));
  125. }
  126. buf[len * 3] = 0;
  127. if (n == 5) n = 0;
  128. return buf;
  129. }
  130. #endif
  131. void printstr(unsigned char *str, int len)
  132. {
  133. #ifdef no
  134. static char *outstr;
  135. int i, n, c, usehex;
  136. char *s, *outend;
  137. int max_strlen = 64;
  138. int xflag = 0;
  139. outstr = (char *) my_calloc(1, 2 * max_strlen);
  140. outend = outstr + max_strlen * 2 - 10;
  141. n = (((max_strlen) < (len)) ? (max_strlen) : (len));
  142. usehex = 0;
  143. if (xflag > 1)
  144. usehex = 1;
  145. else if (xflag) {
  146. for (i = 0; i < n; i++) {
  147. c = str[i];
  148. if (len < 0 && c == '\0')
  149. break;
  150. if (!isprint(c) && !egg_isspace(c)) {
  151. usehex = 1;
  152. break;
  153. }
  154. }
  155. }
  156. s = outstr;
  157. *s++ = '\"';
  158. if (usehex) {
  159. for (i = 0; i < n; i++) {
  160. c = str[i];
  161. if (len < 0 && c == '\0')
  162. break;
  163. sprintf(s, "\\x%02x", c);
  164. s += 4;
  165. if (s > outend)
  166. break;
  167. }
  168. }
  169. else {
  170. for (i = 0; i < n; i++) {
  171. c = str[i];
  172. if (len < 0 && c == '\0')
  173. break;
  174. switch (c) {
  175. case '\"': case '\'': case '\\':
  176. *s++ = '\\'; *s++ = c; break;
  177. case '\f':
  178. *s++ = '\\'; *s++ = 'f'; break;
  179. case '\n':
  180. *s++ = '\\'; *s++ = 'n'; break;
  181. case '\r':
  182. *s++ = '\\'; *s++ = 'r'; break;
  183. case '\t':
  184. *s++ = '\\'; *s++ = 't'; break;
  185. case '\v':
  186. *s++ = '\\'; *s++ = 'v'; break;
  187. default:
  188. if (egg_isprint(c))
  189. *s++ = c;
  190. else if (i < n - 1 && egg_isdigit(str[i + 1])) {
  191. sprintf(s, "\\%03o", c);
  192. s += 4;
  193. }
  194. else {
  195. sprintf(s, "\\%o", c);
  196. s += strlen(s);
  197. }
  198. break;
  199. }
  200. if (s > outend)
  201. break;
  202. }
  203. }
  204. *s++ = '\"';
  205. if (i < len || (len < 0 && (i == n || s > outend))) {
  206. *s++ = '.'; *s++ = '.'; *s++ = '.';
  207. }
  208. *s = '\0';
  209. printf("%s\n", outstr);
  210. #endif
  211. }
  212. #ifdef DEBUG_CONTEXT
  213. #define CX(ptr) cx_file[ptr] && cx_file[ptr][0] ? cx_file[ptr] : "", cx_line[ptr], cx_note[ptr] && cx_note[ptr][0] ? cx_note[ptr] : ""
  214. static void write_debug()
  215. {
  216. char tmpout[150] = "";
  217. simple_snprintf(tmpout, sizeof tmpout, "* Last 3 contexts: %s/%d [%s], %s/%d [%s], %s/%d [%s]",
  218. CX(cx_ptr - 2), CX(cx_ptr - 1), CX(cx_ptr));
  219. putlog(LOG_MISC, "*", "%s (Paste to bryan)", tmpout);
  220. printf("%s\n", tmpout);
  221. }
  222. #endif /* DEBUG_CONTEXT */
  223. #ifndef DEBUG_CONTEXT
  224. static void got_bus(int) __attribute__ ((noreturn));
  225. #endif /* DEBUG_CONTEXT */
  226. static void got_bus(int z)
  227. {
  228. signal(SIGBUS, SIG_DFL);
  229. #ifdef DEBUG_CONTEXT
  230. write_debug();
  231. #endif
  232. fatal("BUS ERROR -- CRASHING!", 1);
  233. #ifdef DEBUG
  234. raise(SIGBUS);
  235. #else
  236. exit(1);
  237. #endif /* DEBUG */
  238. }
  239. #ifndef CYGWIN_HACKS
  240. #ifdef __i386__
  241. #ifndef PAGESIZE
  242. #define PAGESIZE 4096
  243. #endif
  244. struct stackframe {
  245. struct stackframe *ebp;
  246. unsigned long addr;
  247. };
  248. /*
  249. CALL x
  250. PUSH EBP
  251. MOV EBP, ESP
  252. 0x10: EBP
  253. 0x14: EIP
  254. */
  255. static int
  256. canaccess(void *addr)
  257. {
  258. addr = (void *) (((unsigned long) addr / PAGESIZE) * PAGESIZE);
  259. if (mprotect(addr, PAGESIZE, PROT_READ | PROT_WRITE | PROT_EXEC))
  260. if (errno != EACCES)
  261. return 0;
  262. return 1;
  263. }
  264. struct stackframe *sf = NULL;
  265. int stackdepth = 0;
  266. void
  267. stackdump(int idx)
  268. {
  269. __asm__("movl %EBP, %EAX");
  270. __asm__("movl %EAX, sf");
  271. if (idx == 0)
  272. putlog(LOG_MISC, "*", "STACK DUMP");
  273. else
  274. dprintf(idx, "STACK DUMP\n");
  275. while (canaccess(sf) && stackdepth < 20 && sf->ebp) {
  276. if (idx == 0)
  277. putlog(LOG_MISC, "*", " %02d: 0x%08lx/0x%08lx", stackdepth, (unsigned long) sf->ebp, sf->addr);
  278. else
  279. dprintf(idx, " %02d: 0x%08lx/0x%08lx\n", stackdepth, (unsigned long) sf->ebp, sf->addr);
  280. sf = sf->ebp;
  281. stackdepth++;
  282. }
  283. stackdepth = 0;
  284. sf = NULL;
  285. sleep(1);
  286. }
  287. #endif /* __i386__ */
  288. #endif /* !CYGWIN_HACKS */
  289. #ifndef DEBUG_CONTEXT
  290. static void got_segv(int) __attribute__ ((noreturn));
  291. #endif /* DEBUG_CONTEXT */
  292. static void got_segv(int z)
  293. {
  294. segfaulted = 1;
  295. alarm(0); /* dont let anything jump out of this signal! */
  296. signal(SIGSEGV, SIG_DFL);
  297. /* stackdump(0); */
  298. #ifdef DEBUG_CONTEXT
  299. write_debug();
  300. #endif
  301. fatal("SEGMENT VIOLATION -- CRASHING!", 1);
  302. #ifdef DEBUG
  303. raise(SIGSEGV);
  304. #else
  305. exit(1);
  306. #endif /* DEBUG */
  307. }
  308. static void got_fpe(int) __attribute__ ((noreturn));
  309. static void got_fpe(int z)
  310. {
  311. #ifdef DEBUG_CONTEXT
  312. write_debug();
  313. #endif
  314. fatal("FLOATING POINT ERROR -- CRASHING!", 0);
  315. exit(1); /* for GCC noreturn */
  316. }
  317. static void got_term(int) __attribute__ ((noreturn));
  318. static void got_term(int z)
  319. {
  320. if (conf.bot->hub)
  321. write_userfile(-1);
  322. fatal("Received SIGTERM", 0);
  323. exit(1); /* for GCC noreturn */
  324. }
  325. #ifndef DEBUG_CONTEXT
  326. static void got_abort(int) __attribute__ ((noreturn));
  327. #endif /* DEBUG_CONTEXT */
  328. static void got_abort(int z)
  329. {
  330. signal(SIGABRT, SIG_DFL);
  331. #ifdef DEBUG_CONTEXT
  332. write_debug();
  333. #endif
  334. fatal("GOT SIGABRT -- CRASHING!", 1);
  335. #ifdef DEBUG
  336. raise(SIGSEGV);
  337. #else
  338. exit(1);
  339. #endif /* DEBUG */
  340. }
  341. #ifndef CYGWIN_HACKS
  342. static void got_cont(int z)
  343. {
  344. detected(DETECT_HIJACK, "POSSIBLE HIJACK DETECTED (!! MAY BE BOX REBOOT !!)");
  345. }
  346. #endif /* !CYGWIN_HACKS */
  347. static void got_alarm(int) __attribute__((noreturn));
  348. static void got_alarm(int z)
  349. {
  350. sdprintf("SIGALARM");
  351. longjmp(alarmret, 1);
  352. /* -Never reached- */
  353. }
  354. /* Got ILL signal -- log context and continue
  355. */
  356. static void got_ill(int z)
  357. {
  358. #ifdef DEBUG_CONTEXT
  359. putlog(LOG_MISC, "*", "* Context: %s/%d [%s]", cx_file[cx_ptr], cx_line[cx_ptr],
  360. (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
  361. #endif /* DEBUG_CONTEXT */
  362. }
  363. static void
  364. got_hup(int z)
  365. {
  366. signal(SIGHUP, got_hup);
  367. putlog(LOG_MISC, "*", "GOT SIGHUP -- RESTARTING");
  368. do_restart = 1;
  369. // restart(-1);
  370. }
  371. static void
  372. got_usr1(int z)
  373. {
  374. signal(SIGUSR1, got_usr1);
  375. putlog(LOG_DEBUG, "*", "GOT SIGUSR1 -- RECHECKING BINARY");
  376. do_restart = 2;
  377. // reload_bin_data();
  378. }
  379. void init_signals()
  380. {
  381. signal(SIGBUS, got_bus);
  382. signal(SIGSEGV, got_segv);
  383. signal(SIGFPE, got_fpe);
  384. signal(SIGTERM, got_term);
  385. #ifndef CYGWIN_HACKS
  386. signal(SIGCONT, got_cont);
  387. #endif /* !CYGWIN_HACKS */
  388. signal(SIGABRT, got_abort);
  389. signal(SIGPIPE, SIG_IGN);
  390. signal(SIGILL, got_ill);
  391. signal(SIGALRM, got_alarm);
  392. signal(SIGHUP, got_hup);
  393. signal(SIGUSR1, got_usr1);
  394. }
  395. #ifdef DEBUG_CONTEXT
  396. /* Context */
  397. void eggContext(const char *file, int line)
  398. {
  399. char x[31] = "", *p = strrchr(file, '/');
  400. strlcpy(x, p ? p + 1 : file, sizeof x);
  401. cx_ptr = ((cx_ptr + 1) & 15);
  402. strcpy(cx_file[cx_ptr], x);
  403. cx_line[cx_ptr] = line;
  404. cx_note[cx_ptr][0] = 0;
  405. }
  406. /* Called from the ContextNote macro.
  407. */
  408. void eggContextNote(const char *file, int line, const char *note)
  409. {
  410. char x[31] = "", *p = strrchr(file, '/');
  411. strlcpy(x, p ? p + 1 : file, sizeof x);
  412. cx_ptr = ((cx_ptr + 1) & 15);
  413. strcpy(cx_file[cx_ptr], x);
  414. cx_line[cx_ptr] = line;
  415. strlcpy(cx_note[cx_ptr], note, sizeof cx_note[cx_ptr]);
  416. }
  417. #endif