debug.cc 7.0 KB

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