debug.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. #ifndef DEBUG
  102. if (sdebug) {
  103. #endif
  104. char s[2001] = "";
  105. va_list va;
  106. va_start(va, format);
  107. egg_vsnprintf(s, sizeof(s), format, va);
  108. va_end(va);
  109. remove_crlf(s);
  110. #ifdef DEBUG
  111. if (sdebug) {
  112. #endif
  113. if (!backgrd)
  114. dprintf(DP_STDOUT, "[D:%d] %s%s%s\n", mypid, BOLD(-1), s, BOLD_END(-1));
  115. else
  116. printf("[D:%d] %s%s%s\n", mypid, BOLD(-1), s, BOLD_END(-1));
  117. #ifdef DEBUG
  118. }
  119. logfile(LOG_DEBUG, s);
  120. #else
  121. }
  122. #endif
  123. }
  124. #ifdef NOTUSED
  125. char* hexize(const unsigned char* data, size_t len) {
  126. static char buffers[5][513] = { "", "", "", "", "" };
  127. static int n = 0;
  128. char *buf = buffers[n++];
  129. buf[0] = 0;
  130. for (size_t i = 0; i < len; ++i) {
  131. if (i == 0)
  132. sprintf(buf, "%.2X", (int) (data[i]));
  133. else
  134. sprintf(buf, "%s %.2X", buf, (int) (data[i]));
  135. }
  136. buf[len * 3] = 0;
  137. if (n == 5) n = 0;
  138. return buf;
  139. }
  140. #endif
  141. void printstr(unsigned char *str, int len)
  142. {
  143. #ifdef no
  144. static char *outstr;
  145. int i, n, c, usehex;
  146. char *s, *outend;
  147. int max_strlen = 64;
  148. int xflag = 0;
  149. outstr = (char *) my_calloc(1, 2 * max_strlen);
  150. outend = outstr + max_strlen * 2 - 10;
  151. n = (((max_strlen) < (len)) ? (max_strlen) : (len));
  152. usehex = 0;
  153. if (xflag > 1)
  154. usehex = 1;
  155. else if (xflag) {
  156. for (i = 0; i < n; i++) {
  157. c = str[i];
  158. if (len < 0 && c == '\0')
  159. break;
  160. if (!isprint(c) && !egg_isspace(c)) {
  161. usehex = 1;
  162. break;
  163. }
  164. }
  165. }
  166. s = outstr;
  167. *s++ = '\"';
  168. if (usehex) {
  169. for (i = 0; i < n; i++) {
  170. c = str[i];
  171. if (len < 0 && c == '\0')
  172. break;
  173. sprintf(s, "\\x%02x", c);
  174. s += 4;
  175. if (s > outend)
  176. break;
  177. }
  178. }
  179. else {
  180. for (i = 0; i < n; i++) {
  181. c = str[i];
  182. if (len < 0 && c == '\0')
  183. break;
  184. switch (c) {
  185. case '\"': case '\'': case '\\':
  186. *s++ = '\\'; *s++ = c; break;
  187. case '\f':
  188. *s++ = '\\'; *s++ = 'f'; break;
  189. case '\n':
  190. *s++ = '\\'; *s++ = 'n'; break;
  191. case '\r':
  192. *s++ = '\\'; *s++ = 'r'; break;
  193. case '\t':
  194. *s++ = '\\'; *s++ = 't'; break;
  195. case '\v':
  196. *s++ = '\\'; *s++ = 'v'; break;
  197. default:
  198. if (egg_isprint(c))
  199. *s++ = c;
  200. else if (i < n - 1 && egg_isdigit(str[i + 1])) {
  201. sprintf(s, "\\%03o", c);
  202. s += 4;
  203. }
  204. else {
  205. sprintf(s, "\\%o", c);
  206. s += strlen(s);
  207. }
  208. break;
  209. }
  210. if (s > outend)
  211. break;
  212. }
  213. }
  214. *s++ = '\"';
  215. if (i < len || (len < 0 && (i == n || s > outend))) {
  216. *s++ = '.'; *s++ = '.'; *s++ = '.';
  217. }
  218. *s = '\0';
  219. printf("%s\n", outstr);
  220. #endif
  221. }
  222. #ifdef DEBUG_CONTEXT
  223. #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] : ""
  224. static void write_debug()
  225. {
  226. char tmpout[150] = "";
  227. simple_snprintf(tmpout, sizeof tmpout, "* Last 3 contexts: %s/%d [%s], %s/%d [%s], %s/%d [%s]",
  228. CX(cx_ptr - 2), CX(cx_ptr - 1), CX(cx_ptr));
  229. putlog(LOG_MISC, "*", "%s (Paste to bryan)", tmpout);
  230. printf("%s\n", tmpout);
  231. }
  232. #endif /* DEBUG_CONTEXT */
  233. #ifndef DEBUG
  234. static void got_bus(int) __attribute__ ((noreturn));
  235. #endif /* DEBUG */
  236. static void got_bus(int z)
  237. {
  238. signal(SIGBUS, SIG_DFL);
  239. #ifdef DEBUG_CONTEXT
  240. write_debug();
  241. #endif
  242. fatal("BUS ERROR -- CRASHING!", 1);
  243. #ifdef DEBUG
  244. raise(SIGBUS);
  245. #else
  246. exit(1);
  247. #endif /* DEBUG */
  248. }
  249. #ifndef DEBUG
  250. static void got_segv(int) __attribute__ ((noreturn));
  251. #endif /* DEBUG */
  252. static void got_segv(int z)
  253. {
  254. segfaulted = 1;
  255. alarm(0); /* dont let anything jump out of this signal! */
  256. signal(SIGSEGV, SIG_DFL);
  257. #ifdef DEBUG_CONTEXT
  258. write_debug();
  259. #endif
  260. fatal("SEGMENT VIOLATION -- CRASHING!", 1);
  261. #ifdef DEBUG
  262. raise(SIGSEGV);
  263. #else
  264. exit(1);
  265. #endif /* DEBUG */
  266. }
  267. static void got_fpe(int) __attribute__ ((noreturn));
  268. static void got_fpe(int z)
  269. {
  270. #ifdef DEBUG_CONTEXT
  271. write_debug();
  272. #endif
  273. fatal("FLOATING POINT ERROR -- CRASHING!", 0);
  274. exit(1); /* for GCC noreturn */
  275. }
  276. static void got_term(int) __attribute__ ((noreturn));
  277. static void got_term(int z)
  278. {
  279. if (conf.bot->hub)
  280. write_userfile(-1);
  281. fatal("Received SIGTERM", 0);
  282. exit(1); /* for GCC noreturn */
  283. }
  284. #ifndef DEBUG
  285. static void got_abort(int) __attribute__ ((noreturn));
  286. #endif /* DEBUG */
  287. static void got_abort(int z)
  288. {
  289. signal(SIGABRT, SIG_DFL);
  290. #ifdef DEBUG_CONTEXT
  291. write_debug();
  292. #endif
  293. fatal("GOT SIGABRT -- CRASHING!", 1);
  294. #ifdef DEBUG
  295. raise(SIGSEGV);
  296. #else
  297. exit(1);
  298. #endif /* DEBUG */
  299. }
  300. #ifndef CYGWIN_HACKS
  301. static void got_cont(int z)
  302. {
  303. detected(DETECT_HIJACK, "POSSIBLE HIJACK DETECTED (!! MAY BE BOX REBOOT !!)");
  304. }
  305. #endif /* !CYGWIN_HACKS */
  306. static void got_alarm(int) __attribute__((noreturn));
  307. static void got_alarm(int z)
  308. {
  309. sdprintf("SIGALARM");
  310. longjmp(alarmret, 1);
  311. /* -Never reached- */
  312. }
  313. /* Got ILL signal -- log context and continue
  314. */
  315. static void got_ill(int z)
  316. {
  317. #ifdef DEBUG_CONTEXT
  318. putlog(LOG_MISC, "*", "* Context: %s/%d [%s]", cx_file[cx_ptr], cx_line[cx_ptr],
  319. (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
  320. #endif /* DEBUG_CONTEXT */
  321. }
  322. static void
  323. got_hup(int z)
  324. {
  325. signal(SIGHUP, got_hup);
  326. putlog(LOG_MISC, "*", "GOT SIGHUP -- RESTARTING");
  327. do_restart = 1;
  328. // restart(-1);
  329. }
  330. static void
  331. got_usr1(int z)
  332. {
  333. signal(SIGUSR1, got_usr1);
  334. putlog(LOG_DEBUG, "*", "GOT SIGUSR1 -- RECHECKING BINARY");
  335. do_restart = 2;
  336. // reload_bin_data();
  337. }
  338. void init_signals()
  339. {
  340. signal(SIGBUS, got_bus);
  341. signal(SIGSEGV, got_segv);
  342. signal(SIGFPE, got_fpe);
  343. signal(SIGTERM, got_term);
  344. #ifndef CYGWIN_HACKS
  345. signal(SIGCONT, got_cont);
  346. #endif /* !CYGWIN_HACKS */
  347. signal(SIGABRT, got_abort);
  348. signal(SIGPIPE, SIG_IGN);
  349. signal(SIGILL, got_ill);
  350. signal(SIGALRM, got_alarm);
  351. signal(SIGHUP, got_hup);
  352. signal(SIGUSR1, got_usr1);
  353. }
  354. #ifdef DEBUG_CONTEXT
  355. /* Context */
  356. void eggContext(const char *file, int line)
  357. {
  358. char x[31] = "", *p = strrchr(file, '/');
  359. strlcpy(x, p ? p + 1 : file, sizeof x);
  360. cx_ptr = ((cx_ptr + 1) & 15);
  361. strcpy(cx_file[cx_ptr], x);
  362. cx_line[cx_ptr] = line;
  363. cx_note[cx_ptr][0] = 0;
  364. }
  365. /* Called from the ContextNote macro.
  366. */
  367. void eggContextNote(const char *file, int line, const char *note)
  368. {
  369. char x[31] = "", *p = strrchr(file, '/');
  370. strlcpy(x, p ? p + 1 : file, sizeof x);
  371. cx_ptr = ((cx_ptr + 1) & 15);
  372. strcpy(cx_file[cx_ptr], x);
  373. cx_line[cx_ptr] = line;
  374. strlcpy(cx_note[cx_ptr], note, sizeof cx_note[cx_ptr]);
  375. }
  376. #endif