debug.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 = MAX_SOCKETS;
  86. fdlim.rlim_max = MAX_SOCKETS;
  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. bzero(&cx_file, sizeof cx_file);
  96. 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:%lu] %s%s%s\n", (unsigned long) mypid, BOLD(-1), s, BOLD_END(-1));
  115. else
  116. printf("[D:%lu] %s%s%s\n", (unsigned long) mypid, BOLD(-1), s, BOLD_END(-1));
  117. #ifdef DEBUG
  118. }
  119. logfile(LOG_DEBUG, s);
  120. #else
  121. }
  122. #endif
  123. }
  124. #ifdef DEBUG_CONTEXT
  125. #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] : ""
  126. static void write_debug()
  127. {
  128. char tmpout[150] = "";
  129. simple_snprintf(tmpout, sizeof tmpout, "* Last 3 contexts: %s/%d [%s], %s/%d [%s], %s/%d [%s]",
  130. CX(cx_ptr - 2), CX(cx_ptr - 1), CX(cx_ptr));
  131. putlog(LOG_MISC, "*", "%s (Paste to bryan)", tmpout);
  132. printf("%s\n", tmpout);
  133. }
  134. #endif /* DEBUG_CONTEXT */
  135. #ifndef DEBUG
  136. static void got_bus(int) __attribute__ ((noreturn));
  137. #endif /* DEBUG */
  138. static void got_bus(int z)
  139. {
  140. signal(SIGBUS, SIG_DFL);
  141. #ifdef DEBUG_CONTEXT
  142. write_debug();
  143. #endif
  144. fatal("BUS ERROR -- CRASHING!", 1);
  145. #ifdef DEBUG
  146. raise(SIGBUS);
  147. #else
  148. exit(1);
  149. #endif /* DEBUG */
  150. }
  151. #ifndef DEBUG
  152. static void got_segv(int) __attribute__ ((noreturn));
  153. #endif /* DEBUG */
  154. static void got_segv(int z)
  155. {
  156. segfaulted = 1;
  157. alarm(0); /* dont let anything jump out of this signal! */
  158. signal(SIGSEGV, SIG_DFL);
  159. #ifdef DEBUG_CONTEXT
  160. write_debug();
  161. #endif
  162. fatal("SEGMENT VIOLATION -- CRASHING!", 1);
  163. #ifdef DEBUG
  164. raise(SIGSEGV);
  165. #else
  166. exit(1);
  167. #endif /* DEBUG */
  168. }
  169. static void got_fpe(int) __attribute__ ((noreturn));
  170. static void got_fpe(int z)
  171. {
  172. #ifdef DEBUG_CONTEXT
  173. write_debug();
  174. #endif
  175. fatal("FLOATING POINT ERROR -- CRASHING!", 0);
  176. exit(1); /* for GCC noreturn */
  177. }
  178. static void got_term(int) __attribute__ ((noreturn));
  179. static void got_term(int z)
  180. {
  181. if (conf.bot->hub)
  182. write_userfile(-1);
  183. fatal("Received SIGTERM", 0);
  184. exit(1); /* for GCC noreturn */
  185. }
  186. #ifndef DEBUG
  187. static void got_abort(int) __attribute__ ((noreturn));
  188. #endif /* DEBUG */
  189. static void got_abort(int z)
  190. {
  191. signal(SIGABRT, SIG_DFL);
  192. #ifdef DEBUG_CONTEXT
  193. write_debug();
  194. #endif
  195. fatal("GOT SIGABRT -- CRASHING!", 1);
  196. #ifdef DEBUG
  197. raise(SIGSEGV);
  198. #else
  199. exit(1);
  200. #endif /* DEBUG */
  201. }
  202. #ifndef CYGWIN_HACKS
  203. static void got_cont(int z)
  204. {
  205. detected(DETECT_HIJACK, "POSSIBLE HIJACK DETECTED (!! MAY BE BOX REBOOT !!)");
  206. }
  207. #endif /* !CYGWIN_HACKS */
  208. static void got_alarm(int) __attribute__((noreturn));
  209. static void got_alarm(int z)
  210. {
  211. sdprintf("SIGALARM");
  212. longjmp(alarmret, 1);
  213. /* -Never reached- */
  214. }
  215. /* Got ILL signal -- log context and continue
  216. */
  217. static void got_ill(int z)
  218. {
  219. #ifdef DEBUG_CONTEXT
  220. putlog(LOG_MISC, "*", "* Context: %s/%d [%s]", cx_file[cx_ptr], cx_line[cx_ptr],
  221. (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
  222. #endif /* DEBUG_CONTEXT */
  223. }
  224. static void
  225. got_hup(int z)
  226. {
  227. signal(SIGHUP, got_hup);
  228. putlog(LOG_MISC, "*", "GOT SIGHUP -- RESTARTING");
  229. do_restart = 1;
  230. // restart(-1);
  231. }
  232. static void
  233. got_usr1(int z)
  234. {
  235. signal(SIGUSR1, got_usr1);
  236. putlog(LOG_DEBUG, "*", "GOT SIGUSR1 -- RECHECKING BINARY");
  237. do_restart = 2;
  238. // reload_bin_data();
  239. }
  240. void init_signals()
  241. {
  242. signal(SIGBUS, got_bus);
  243. signal(SIGSEGV, got_segv);
  244. signal(SIGFPE, got_fpe);
  245. signal(SIGTERM, got_term);
  246. #ifndef CYGWIN_HACKS
  247. signal(SIGCONT, got_cont);
  248. #endif /* !CYGWIN_HACKS */
  249. signal(SIGABRT, got_abort);
  250. signal(SIGPIPE, SIG_IGN);
  251. signal(SIGILL, got_ill);
  252. signal(SIGALRM, got_alarm);
  253. signal(SIGHUP, got_hup);
  254. signal(SIGUSR1, got_usr1);
  255. }
  256. #ifdef DEBUG_CONTEXT
  257. /* Context */
  258. void eggContext(const char *file, int line)
  259. {
  260. char x[31] = "", *p = strrchr(file, '/');
  261. strlcpy(x, p ? p + 1 : file, sizeof x);
  262. cx_ptr = ((cx_ptr + 1) & 15);
  263. strcpy(cx_file[cx_ptr], x);
  264. cx_line[cx_ptr] = line;
  265. cx_note[cx_ptr][0] = 0;
  266. }
  267. /* Called from the ContextNote macro.
  268. */
  269. void eggContextNote(const char *file, int line, const char *note)
  270. {
  271. char x[31] = "", *p = strrchr(file, '/');
  272. strlcpy(x, p ? p + 1 : file, sizeof x);
  273. cx_ptr = ((cx_ptr + 1) & 15);
  274. strcpy(cx_file[cx_ptr], x);
  275. cx_line[cx_ptr] = line;
  276. strlcpy(cx_note[cx_ptr], note, sizeof cx_note[cx_ptr]);
  277. }
  278. #endif