debug.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * debug.c -- handles:
  3. * signal handling
  4. * Context handling
  5. * debug funtions
  6. *
  7. */
  8. #include "common.h"
  9. #include "debug.h"
  10. #include "chanprog.h"
  11. #include "net.h"
  12. #include "shell.h"
  13. #include "color.h"
  14. #include "binary.h"
  15. #include "userrec.h"
  16. #include "main.h"
  17. #include "dccutil.h"
  18. #include <setjmp.h>
  19. #include <signal.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <fcntl.h>
  23. #include <sys/time.h>
  24. #include <sys/resource.h>
  25. #include <unistd.h>
  26. #include <stdarg.h>
  27. #include <errno.h>
  28. #include <sys/mman.h>
  29. bool sdebug = 0; /* enable debug output? */
  30. #ifdef DEBUG_CONTEXT
  31. /* Context storage for fatal crashes */
  32. char cx_file[16][16];
  33. char cx_note[16][16];
  34. int cx_line[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  35. int cx_ptr = 0;
  36. #endif /* DEBUG_CONTEXT */
  37. void setlimits()
  38. {
  39. #ifndef CYGWIN_HACKS
  40. struct rlimit plim, fdlim, corelim;
  41. #ifndef DEBUG
  42. /* struct rsslim, stacklim;
  43. rsslim.rlim_cur = 30720;
  44. rsslim.rlim_max = 30720;
  45. setrlimit(RLIMIT_RSS, &rsslim);
  46. stacklim.rlim_cur = 30720;
  47. stacklim.rlim_max = 30720;
  48. setrlimit(RLIMIT_STACK, &stacklim);
  49. */
  50. /* do NOT dump a core. */
  51. plim.rlim_cur = 60;
  52. plim.rlim_max = 60;
  53. corelim.rlim_cur = 0;
  54. corelim.rlim_max = 0;
  55. #else /* DEBUG */
  56. plim.rlim_cur = 500;
  57. plim.rlim_max = 500;
  58. corelim.rlim_cur = RLIM_INFINITY;
  59. corelim.rlim_max = RLIM_INFINITY;
  60. #endif /* !DEBUG */
  61. setrlimit(RLIMIT_CORE, &corelim);
  62. #ifndef __sun__
  63. setrlimit(RLIMIT_NPROC, &plim);
  64. #endif
  65. fdlim.rlim_cur = 300;
  66. fdlim.rlim_max = 300;
  67. setrlimit(RLIMIT_NOFILE, &fdlim);
  68. #endif /* !CYGWIN_HACKS */
  69. }
  70. void init_debug()
  71. {
  72. for (int i = 0; i < 16; i ++)
  73. Context;
  74. #ifdef DEBUG_CONTEXT
  75. egg_bzero(&cx_file, sizeof cx_file);
  76. egg_bzero(&cx_note, sizeof cx_note);
  77. #endif /* DEBUG_CONTEXT */
  78. }
  79. void sdprintf (const char *format, ...)
  80. {
  81. if (sdebug) {
  82. char s[2001] = "";
  83. va_list va;
  84. va_start(va, format);
  85. egg_vsnprintf(s, sizeof(s), format, va);
  86. va_end(va);
  87. remove_crlf(s);
  88. if (!backgrd)
  89. dprintf(DP_STDOUT, "[D:%d] %s%s%s\n", mypid, BOLD(-1), s, BOLD_END(-1));
  90. else
  91. printf("[D:%d] %s%s%s\n", mypid, BOLD(-1), s, BOLD_END(-1));
  92. }
  93. }
  94. #ifdef DEBUG_CONTEXT
  95. #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] : ""
  96. static void write_debug()
  97. {
  98. char tmpout[150] = "";
  99. simple_snprintf(tmpout, sizeof tmpout, "* Last 3 contexts: %s/%d [%s], %s/%d [%s], %s/%d [%s]",
  100. CX(cx_ptr - 2), CX(cx_ptr - 1), CX(cx_ptr));
  101. putlog(LOG_MISC, "*", "%s (Paste to bryan)", tmpout);
  102. printf("%s\n", tmpout);
  103. }
  104. #endif /* DEBUG_CONTEXT */
  105. #ifndef DEBUG_CONTEXT
  106. static void got_bus(int) __attribute__ ((noreturn));
  107. #endif /* DEBUG_CONTEXT */
  108. static void got_bus(int z)
  109. {
  110. signal(SIGBUS, SIG_DFL);
  111. #ifdef DEBUG_CONTEXT
  112. write_debug();
  113. #endif
  114. fatal("BUS ERROR -- CRASHING!", 1);
  115. #ifdef DEBUG
  116. raise(SIGBUS);
  117. #else
  118. exit(1);
  119. #endif /* DEBUG */
  120. }
  121. #ifndef CYGWIN_HACKS
  122. #ifdef __i386__
  123. #ifndef PAGESIZE
  124. #define PAGESIZE 4096
  125. #endif
  126. struct stackframe {
  127. struct stackframe *ebp;
  128. unsigned long addr;
  129. };
  130. /*
  131. CALL x
  132. PUSH EBP
  133. MOV EBP, ESP
  134. 0x10: EBP
  135. 0x14: EIP
  136. */
  137. static int
  138. canaccess(void *addr)
  139. {
  140. addr = (void *) (((unsigned long) addr / PAGESIZE) * PAGESIZE);
  141. if (mprotect(addr, PAGESIZE, PROT_READ | PROT_WRITE | PROT_EXEC))
  142. if (errno != EACCES)
  143. return 0;
  144. return 1;
  145. }
  146. struct stackframe *sf = NULL;
  147. int stackdepth = 0;
  148. void
  149. stackdump(int idx)
  150. {
  151. __asm__("movl %EBP, %EAX");
  152. __asm__("movl %EAX, sf");
  153. if (idx == 0)
  154. putlog(LOG_MISC, "*", "STACK DUMP");
  155. else
  156. dprintf(idx, "STACK DUMP\n");
  157. while (canaccess(sf) && stackdepth < 20 && sf->ebp) {
  158. if (idx == 0)
  159. putlog(LOG_MISC, "*", " %02d: 0x%08lx/0x%08lx", stackdepth, (unsigned long) sf->ebp, sf->addr);
  160. else
  161. dprintf(idx, " %02d: 0x%08lx/0x%08lx\n", stackdepth, (unsigned long) sf->ebp, sf->addr);
  162. sf = sf->ebp;
  163. stackdepth++;
  164. }
  165. stackdepth = 0;
  166. sf = NULL;
  167. sleep(1);
  168. }
  169. #endif /* __i386__ */
  170. #endif /* !CYGWIN_HACKS */
  171. #ifndef DEBUG_CONTEXT
  172. static void got_segv(int) __attribute__ ((noreturn));
  173. #endif /* DEBUG_CONTEXT */
  174. static void got_segv(int z)
  175. {
  176. alarm(0); /* dont let anything jump out of this signal! */
  177. signal(SIGSEGV, SIG_DFL);
  178. /* stackdump(0); */
  179. #ifdef DEBUG_CONTEXT
  180. write_debug();
  181. #endif
  182. fatal("SEGMENT VIOLATION -- CRASHING!", 1);
  183. #ifdef DEBUG
  184. raise(SIGSEGV);
  185. #else
  186. exit(1);
  187. #endif /* DEBUG */
  188. }
  189. static void got_fpe(int) __attribute__ ((noreturn));
  190. static void got_fpe(int z)
  191. {
  192. #ifdef DEBUG_CONTEXT
  193. write_debug();
  194. #endif
  195. fatal("FLOATING POINT ERROR -- CRASHING!", 0);
  196. exit(1); /* for GCC noreturn */
  197. }
  198. static void got_term(int) __attribute__ ((noreturn));
  199. static void got_term(int z)
  200. {
  201. if (conf.bot->hub)
  202. write_userfile(-1);
  203. fatal("Received SIGTERM", 0);
  204. exit(1); /* for GCC noreturn */
  205. }
  206. #ifndef DEBUG_CONTEXT
  207. static void got_abort(int) __attribute__ ((noreturn));
  208. #endif /* DEBUG_CONTEXT */
  209. static void got_abort(int z)
  210. {
  211. signal(SIGABRT, SIG_DFL);
  212. #ifdef DEBUG_CONTEXT
  213. write_debug();
  214. #endif
  215. fatal("GOT SIGABRT -- CRASHING!", 1);
  216. #ifdef DEBUG
  217. raise(SIGSEGV);
  218. #else
  219. exit(1);
  220. #endif /* DEBUG */
  221. }
  222. static void got_cont(int z)
  223. {
  224. detected(DETECT_SIGCONT, "POSSIBLE HIJACK DETECTED (!! MAY BE BOX REBOOT !!)");
  225. }
  226. static void got_alarm(int) __attribute__((noreturn));
  227. static void got_alarm(int z)
  228. {
  229. sdprintf("SIGALARM");
  230. longjmp(alarmret, 1);
  231. /* -Never reached- */
  232. }
  233. /* Got ILL signal -- log context and continue
  234. */
  235. static void got_ill(int z)
  236. {
  237. #ifdef DEBUG_CONTEXT
  238. putlog(LOG_MISC, "*", "* Context: %s/%d [%s]", cx_file[cx_ptr], cx_line[cx_ptr],
  239. (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
  240. #endif /* DEBUG_CONTEXT */
  241. }
  242. static void
  243. got_hup(int z)
  244. {
  245. signal(SIGHUP, got_hup);
  246. putlog(LOG_MISC, "*", "GOT SIGHUP -- RESTARTING");
  247. do_restart = 1;
  248. // restart(-1);
  249. }
  250. static void
  251. got_usr1(int z)
  252. {
  253. signal(SIGUSR1, got_usr1);
  254. putlog(LOG_DEBUG, "*", "GOT SIGUSR1 -- RECHECKING BINARY");
  255. do_restart = 2;
  256. // reload_bin_data();
  257. }
  258. void init_signals()
  259. {
  260. signal(SIGBUS, got_bus);
  261. signal(SIGSEGV, got_segv);
  262. signal(SIGFPE, got_fpe);
  263. signal(SIGTERM, got_term);
  264. signal(SIGCONT, got_cont);
  265. signal(SIGABRT, got_abort);
  266. signal(SIGPIPE, SIG_IGN);
  267. signal(SIGILL, got_ill);
  268. signal(SIGALRM, got_alarm);
  269. signal(SIGHUP, got_hup);
  270. signal(SIGUSR1, got_usr1);
  271. }
  272. #ifdef DEBUG_CONTEXT
  273. /* Context */
  274. void eggContext(const char *file, int line)
  275. {
  276. char x[31] = "", *p = strrchr(file, '/');
  277. strlcpy(x, p ? p + 1 : file, sizeof x);
  278. cx_ptr = ((cx_ptr + 1) & 15);
  279. strcpy(cx_file[cx_ptr], x);
  280. cx_line[cx_ptr] = line;
  281. cx_note[cx_ptr][0] = 0;
  282. }
  283. /* Called from the ContextNote macro.
  284. */
  285. void eggContextNote(const char *file, int line, const char *note)
  286. {
  287. char x[31] = "", *p = strrchr(file, '/');
  288. strlcpy(x, p ? p + 1 : file, sizeof x);
  289. cx_ptr = ((cx_ptr + 1) & 15);
  290. strcpy(cx_file[cx_ptr], x);
  291. cx_line[cx_ptr] = line;
  292. strlcpy(cx_note[cx_ptr], note, sizeof cx_note[cx_ptr]);
  293. }
  294. #endif