debug.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. char get_buf[GET_BUFS][SGRAB + 5];
  51. size_t current_get_buf = 0;
  52. void setlimits()
  53. {
  54. #ifndef CYGWIN_HACKS
  55. struct rlimit plim, fdlim, corelim;
  56. #ifndef DEBUG
  57. /* struct rsslim, stacklim;
  58. rsslim.rlim_cur = 30720;
  59. rsslim.rlim_max = 30720;
  60. setrlimit(RLIMIT_RSS, &rsslim);
  61. stacklim.rlim_cur = 30720;
  62. stacklim.rlim_max = 30720;
  63. setrlimit(RLIMIT_STACK, &stacklim);
  64. */
  65. /* do NOT dump a core. */
  66. plim.rlim_cur = 60;
  67. plim.rlim_max = 60;
  68. corelim.rlim_cur = 0;
  69. corelim.rlim_max = 0;
  70. #else /* DEBUG */
  71. plim.rlim_cur = 500;
  72. plim.rlim_max = 500;
  73. corelim.rlim_cur = RLIM_INFINITY;
  74. corelim.rlim_max = RLIM_INFINITY;
  75. #endif /* !DEBUG */
  76. setrlimit(RLIMIT_CORE, &corelim);
  77. #ifndef __sun__
  78. setrlimit(RLIMIT_NPROC, &plim);
  79. #endif
  80. fdlim.rlim_cur = MAX_SOCKETS;
  81. fdlim.rlim_max = MAX_SOCKETS;
  82. setrlimit(RLIMIT_NOFILE, &fdlim);
  83. #endif /* !CYGWIN_HACKS */
  84. }
  85. void init_debug()
  86. {
  87. }
  88. void sdprintf (const char *format, ...)
  89. {
  90. char s[2001] = "";
  91. va_list va;
  92. va_start(va, format);
  93. egg_vsnprintf(s, sizeof(s), format, va);
  94. va_end(va);
  95. remove_crlf(s);
  96. simple_snprintf(get_buf[current_get_buf], sizeof(get_buf[current_get_buf]), "dbg: %s", s);
  97. get_buf_inc();
  98. #ifdef DEBUG
  99. if (sdebug) {
  100. #endif
  101. if (!backgrd)
  102. dprintf(DP_STDOUT, "[D:%lu] %s%s%s\n", (unsigned long) mypid, BOLD(-1), s, BOLD_END(-1));
  103. else
  104. printf("[D:%lu] %s%s%s\n", (unsigned long) mypid, BOLD(-1), s, BOLD_END(-1));
  105. #ifdef DEBUG
  106. }
  107. logfile(LOG_DEBUG, s);
  108. #endif
  109. }
  110. static void write_debug()
  111. {
  112. putlog(LOG_MISC, "*", "** Paste to bryan:");
  113. const size_t cur_buf = (current_get_buf == 0) ? GET_BUFS - 1 : current_get_buf - 1;
  114. for (size_t i = 0; i < GET_BUFS; i++)
  115. putlog(LOG_MISC, "*", "%c %02zu: %s", i == cur_buf ? '*' : '_', i, get_buf[i]);
  116. putlog(LOG_MISC, "*", "** end");
  117. }
  118. #ifndef DEBUG
  119. static void got_bus(int) __attribute__ ((noreturn));
  120. #endif /* DEBUG */
  121. static void got_bus(int z)
  122. {
  123. signal(SIGBUS, SIG_DFL);
  124. write_debug();
  125. fatal("BUS ERROR -- CRASHING!", 1);
  126. #ifdef DEBUG
  127. raise(SIGBUS);
  128. #else
  129. exit(1);
  130. #endif /* DEBUG */
  131. }
  132. #ifndef DEBUG
  133. static void got_segv(int) __attribute__ ((noreturn));
  134. #endif /* DEBUG */
  135. static void got_segv(int z)
  136. {
  137. segfaulted = 1;
  138. alarm(0); /* dont let anything jump out of this signal! */
  139. signal(SIGSEGV, SIG_DFL);
  140. write_debug();
  141. fatal("SEGMENT VIOLATION -- CRASHING!", 1);
  142. #ifdef DEBUG
  143. char gdb[1024] = "", btfile[256] = "", std_in[101] = "", *out = NULL;
  144. unsigned int core = 0;
  145. simple_snprintf(btfile, sizeof(btfile), ".gdb-backtrace-%d", getpid());
  146. FILE *f = fopen(btfile, "w");
  147. if (f) {
  148. strlcpy(std_in, "bt 100\nbt 100 full\n", sizeof(stdin));
  149. // simple_snprintf(stdin, sizeof(stdin), "detach\n");
  150. // simple_snprintf(stdin, sizeof(stdin), "q\n");
  151. simple_snprintf(gdb, sizeof(gdb), "gdb %s %d", binname, getpid());
  152. shell_exec(gdb, std_in, &out, NULL);
  153. fprintf(f, "%s\n", out);
  154. fclose(f);
  155. free(out);
  156. }
  157. //enabling core dumps
  158. struct rlimit limit;
  159. if (!getrlimit(RLIMIT_CORE, &limit)) {
  160. limit.rlim_cur = limit.rlim_max;
  161. if(!setrlimit(RLIMIT_CORE, &limit)) core = limit.rlim_cur;
  162. }
  163. raise(SIGSEGV);
  164. #else
  165. exit(1);
  166. #endif /* DEBUG */
  167. }
  168. static void got_fpe(int) __attribute__ ((noreturn));
  169. static void got_fpe(int z)
  170. {
  171. write_debug();
  172. fatal("FLOATING POINT ERROR -- CRASHING!", 0);
  173. exit(1); /* for GCC noreturn */
  174. }
  175. static void got_term(int) __attribute__ ((noreturn));
  176. static void got_term(int z)
  177. {
  178. if (conf.bot->hub)
  179. write_userfile(-1);
  180. fatal("Received SIGTERM", 0);
  181. exit(1); /* for GCC noreturn */
  182. }
  183. #ifndef DEBUG
  184. static void got_abort(int) __attribute__ ((noreturn));
  185. #endif /* DEBUG */
  186. static void got_abort(int z)
  187. {
  188. signal(SIGABRT, SIG_DFL);
  189. write_debug();
  190. fatal("GOT SIGABRT -- CRASHING!", 1);
  191. #ifdef DEBUG
  192. raise(SIGSEGV);
  193. #else
  194. exit(1);
  195. #endif /* DEBUG */
  196. }
  197. #ifndef CYGWIN_HACKS
  198. static void got_cont(int z)
  199. {
  200. detected(DETECT_HIJACK, "POSSIBLE HIJACK DETECTED (!! MAY BE BOX REBOOT !!)");
  201. }
  202. #endif /* !CYGWIN_HACKS */
  203. static void got_alarm(int) __attribute__((noreturn));
  204. static void got_alarm(int z)
  205. {
  206. sdprintf("SIGALARM");
  207. longjmp(alarmret, 1);
  208. /* -Never reached- */
  209. }
  210. /* Got ILL signal -- log context and continue
  211. */
  212. static void got_ill(int z)
  213. {
  214. write_debug();
  215. }
  216. static void
  217. got_hup(int z)
  218. {
  219. signal(SIGHUP, got_hup);
  220. putlog(LOG_MISC, "*", "GOT SIGHUP -- RESTARTING");
  221. do_restart = 1;
  222. // restart(-1);
  223. }
  224. static void
  225. got_usr1(int z)
  226. {
  227. signal(SIGUSR1, got_usr1);
  228. putlog(LOG_DEBUG, "*", "GOT SIGUSR1 -- RECHECKING BINARY");
  229. do_restart = 2;
  230. // reload_bin_data();
  231. }
  232. void init_signals()
  233. {
  234. signal(SIGBUS, got_bus);
  235. signal(SIGSEGV, got_segv);
  236. signal(SIGFPE, got_fpe);
  237. signal(SIGTERM, got_term);
  238. #ifndef CYGWIN_HACKS
  239. signal(SIGCONT, got_cont);
  240. #endif /* !CYGWIN_HACKS */
  241. signal(SIGABRT, got_abort);
  242. signal(SIGPIPE, SIG_IGN);
  243. signal(SIGILL, got_ill);
  244. signal(SIGALRM, got_alarm);
  245. signal(SIGHUP, got_hup);
  246. signal(SIGUSR1, got_usr1);
  247. }