debug.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. bool segfaulted = 0;
  31. #ifdef DEBUG_CONTEXT
  32. /* Context storage for fatal crashes */
  33. char cx_file[16][16];
  34. char cx_note[16][16];
  35. int cx_line[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  36. int cx_ptr = 0;
  37. #endif /* DEBUG_CONTEXT */
  38. void setlimits()
  39. {
  40. #ifndef CYGWIN_HACKS
  41. struct rlimit plim, fdlim, corelim;
  42. #ifndef DEBUG
  43. /* struct rsslim, stacklim;
  44. rsslim.rlim_cur = 30720;
  45. rsslim.rlim_max = 30720;
  46. setrlimit(RLIMIT_RSS, &rsslim);
  47. stacklim.rlim_cur = 30720;
  48. stacklim.rlim_max = 30720;
  49. setrlimit(RLIMIT_STACK, &stacklim);
  50. */
  51. /* do NOT dump a core. */
  52. plim.rlim_cur = 60;
  53. plim.rlim_max = 60;
  54. corelim.rlim_cur = 0;
  55. corelim.rlim_max = 0;
  56. #else /* DEBUG */
  57. plim.rlim_cur = 500;
  58. plim.rlim_max = 500;
  59. corelim.rlim_cur = RLIM_INFINITY;
  60. corelim.rlim_max = RLIM_INFINITY;
  61. #endif /* !DEBUG */
  62. setrlimit(RLIMIT_CORE, &corelim);
  63. #ifndef __sun__
  64. setrlimit(RLIMIT_NPROC, &plim);
  65. #endif
  66. fdlim.rlim_cur = 300;
  67. fdlim.rlim_max = 300;
  68. setrlimit(RLIMIT_NOFILE, &fdlim);
  69. #endif /* !CYGWIN_HACKS */
  70. }
  71. void init_debug()
  72. {
  73. for (int i = 0; i < 16; i ++)
  74. Context;
  75. #ifdef DEBUG_CONTEXT
  76. egg_bzero(&cx_file, sizeof cx_file);
  77. egg_bzero(&cx_note, sizeof cx_note);
  78. #endif /* DEBUG_CONTEXT */
  79. }
  80. void sdprintf (const char *format, ...)
  81. {
  82. if (sdebug) {
  83. char s[2001] = "";
  84. va_list va;
  85. va_start(va, format);
  86. egg_vsnprintf(s, sizeof(s), format, va);
  87. va_end(va);
  88. remove_crlf(s);
  89. if (!backgrd)
  90. dprintf(DP_STDOUT, "[D:%d] %s%s%s\n", mypid, BOLD(-1), s, BOLD_END(-1));
  91. else
  92. printf("[D:%d] %s%s%s\n", mypid, BOLD(-1), s, BOLD_END(-1));
  93. }
  94. }
  95. void printstr(unsigned char *str, int len)
  96. {
  97. #ifdef no
  98. static char *outstr;
  99. int i, n, c, usehex;
  100. char *s, *outend;
  101. int max_strlen = 64;
  102. int xflag = 0;
  103. outstr = (char *) my_calloc(1, 2 * max_strlen);
  104. outend = outstr + max_strlen * 2 - 10;
  105. n = (((max_strlen) < (len)) ? (max_strlen) : (len));
  106. usehex = 0;
  107. if (xflag > 1)
  108. usehex = 1;
  109. else if (xflag) {
  110. for (i = 0; i < n; i++) {
  111. c = str[i];
  112. if (len < 0 && c == '\0')
  113. break;
  114. if (!isprint(c) && !egg_isspace(c)) {
  115. usehex = 1;
  116. break;
  117. }
  118. }
  119. }
  120. s = outstr;
  121. *s++ = '\"';
  122. if (usehex) {
  123. for (i = 0; i < n; i++) {
  124. c = str[i];
  125. if (len < 0 && c == '\0')
  126. break;
  127. sprintf(s, "\\x%02x", c);
  128. s += 4;
  129. if (s > outend)
  130. break;
  131. }
  132. }
  133. else {
  134. for (i = 0; i < n; i++) {
  135. c = str[i];
  136. if (len < 0 && c == '\0')
  137. break;
  138. switch (c) {
  139. case '\"': case '\'': case '\\':
  140. *s++ = '\\'; *s++ = c; break;
  141. case '\f':
  142. *s++ = '\\'; *s++ = 'f'; break;
  143. case '\n':
  144. *s++ = '\\'; *s++ = 'n'; break;
  145. case '\r':
  146. *s++ = '\\'; *s++ = 'r'; break;
  147. case '\t':
  148. *s++ = '\\'; *s++ = 't'; break;
  149. case '\v':
  150. *s++ = '\\'; *s++ = 'v'; break;
  151. default:
  152. if (egg_isprint(c))
  153. *s++ = c;
  154. else if (i < n - 1 && egg_isdigit(str[i + 1])) {
  155. sprintf(s, "\\%03o", c);
  156. s += 4;
  157. }
  158. else {
  159. sprintf(s, "\\%o", c);
  160. s += strlen(s);
  161. }
  162. break;
  163. }
  164. if (s > outend)
  165. break;
  166. }
  167. }
  168. *s++ = '\"';
  169. if (i < len || (len < 0 && (i == n || s > outend))) {
  170. *s++ = '.'; *s++ = '.'; *s++ = '.';
  171. }
  172. *s = '\0';
  173. printf("%s\n", outstr);
  174. #endif
  175. }
  176. #ifdef DEBUG_CONTEXT
  177. #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] : ""
  178. static void write_debug()
  179. {
  180. char tmpout[150] = "";
  181. simple_snprintf(tmpout, sizeof tmpout, "* Last 3 contexts: %s/%d [%s], %s/%d [%s], %s/%d [%s]",
  182. CX(cx_ptr - 2), CX(cx_ptr - 1), CX(cx_ptr));
  183. putlog(LOG_MISC, "*", "%s (Paste to bryan)", tmpout);
  184. printf("%s\n", tmpout);
  185. }
  186. #endif /* DEBUG_CONTEXT */
  187. #ifndef DEBUG_CONTEXT
  188. static void got_bus(int) __attribute__ ((noreturn));
  189. #endif /* DEBUG_CONTEXT */
  190. static void got_bus(int z)
  191. {
  192. signal(SIGBUS, SIG_DFL);
  193. #ifdef DEBUG_CONTEXT
  194. write_debug();
  195. #endif
  196. fatal("BUS ERROR -- CRASHING!", 1);
  197. #ifdef DEBUG
  198. raise(SIGBUS);
  199. #else
  200. exit(1);
  201. #endif /* DEBUG */
  202. }
  203. #ifndef CYGWIN_HACKS
  204. #ifdef __i386__
  205. #ifndef PAGESIZE
  206. #define PAGESIZE 4096
  207. #endif
  208. struct stackframe {
  209. struct stackframe *ebp;
  210. unsigned long addr;
  211. };
  212. /*
  213. CALL x
  214. PUSH EBP
  215. MOV EBP, ESP
  216. 0x10: EBP
  217. 0x14: EIP
  218. */
  219. static int
  220. canaccess(void *addr)
  221. {
  222. addr = (void *) (((unsigned long) addr / PAGESIZE) * PAGESIZE);
  223. if (mprotect(addr, PAGESIZE, PROT_READ | PROT_WRITE | PROT_EXEC))
  224. if (errno != EACCES)
  225. return 0;
  226. return 1;
  227. }
  228. struct stackframe *sf = NULL;
  229. int stackdepth = 0;
  230. void
  231. stackdump(int idx)
  232. {
  233. __asm__("movl %EBP, %EAX");
  234. __asm__("movl %EAX, sf");
  235. if (idx == 0)
  236. putlog(LOG_MISC, "*", "STACK DUMP");
  237. else
  238. dprintf(idx, "STACK DUMP\n");
  239. while (canaccess(sf) && stackdepth < 20 && sf->ebp) {
  240. if (idx == 0)
  241. putlog(LOG_MISC, "*", " %02d: 0x%08lx/0x%08lx", stackdepth, (unsigned long) sf->ebp, sf->addr);
  242. else
  243. dprintf(idx, " %02d: 0x%08lx/0x%08lx\n", stackdepth, (unsigned long) sf->ebp, sf->addr);
  244. sf = sf->ebp;
  245. stackdepth++;
  246. }
  247. stackdepth = 0;
  248. sf = NULL;
  249. sleep(1);
  250. }
  251. #endif /* __i386__ */
  252. #endif /* !CYGWIN_HACKS */
  253. #ifndef DEBUG_CONTEXT
  254. static void got_segv(int) __attribute__ ((noreturn));
  255. #endif /* DEBUG_CONTEXT */
  256. static void got_segv(int z)
  257. {
  258. segfaulted = 1;
  259. alarm(0); /* dont let anything jump out of this signal! */
  260. signal(SIGSEGV, SIG_DFL);
  261. /* stackdump(0); */
  262. #ifdef DEBUG_CONTEXT
  263. write_debug();
  264. #endif
  265. fatal("SEGMENT VIOLATION -- CRASHING!", 1);
  266. #ifdef DEBUG
  267. raise(SIGSEGV);
  268. #else
  269. exit(1);
  270. #endif /* DEBUG */
  271. }
  272. static void got_fpe(int) __attribute__ ((noreturn));
  273. static void got_fpe(int z)
  274. {
  275. #ifdef DEBUG_CONTEXT
  276. write_debug();
  277. #endif
  278. fatal("FLOATING POINT ERROR -- CRASHING!", 0);
  279. exit(1); /* for GCC noreturn */
  280. }
  281. static void got_term(int) __attribute__ ((noreturn));
  282. static void got_term(int z)
  283. {
  284. if (conf.bot->hub)
  285. write_userfile(-1);
  286. fatal("Received SIGTERM", 0);
  287. exit(1); /* for GCC noreturn */
  288. }
  289. #ifndef DEBUG_CONTEXT
  290. static void got_abort(int) __attribute__ ((noreturn));
  291. #endif /* DEBUG_CONTEXT */
  292. static void got_abort(int z)
  293. {
  294. signal(SIGABRT, SIG_DFL);
  295. #ifdef DEBUG_CONTEXT
  296. write_debug();
  297. #endif
  298. fatal("GOT SIGABRT -- CRASHING!", 1);
  299. #ifdef DEBUG
  300. raise(SIGSEGV);
  301. #else
  302. exit(1);
  303. #endif /* DEBUG */
  304. }
  305. #ifndef CYGWIN_HACKS
  306. static void got_cont(int z)
  307. {
  308. detected(DETECT_SIGCONT, "POSSIBLE HIJACK DETECTED (!! MAY BE BOX REBOOT !!)");
  309. }
  310. #endif /* !CYGWIN_HACKS */
  311. static void got_alarm(int) __attribute__((noreturn));
  312. static void got_alarm(int z)
  313. {
  314. sdprintf("SIGALARM");
  315. longjmp(alarmret, 1);
  316. /* -Never reached- */
  317. }
  318. /* Got ILL signal -- log context and continue
  319. */
  320. static void got_ill(int z)
  321. {
  322. #ifdef DEBUG_CONTEXT
  323. putlog(LOG_MISC, "*", "* Context: %s/%d [%s]", cx_file[cx_ptr], cx_line[cx_ptr],
  324. (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
  325. #endif /* DEBUG_CONTEXT */
  326. }
  327. static void
  328. got_hup(int z)
  329. {
  330. signal(SIGHUP, got_hup);
  331. putlog(LOG_MISC, "*", "GOT SIGHUP -- RESTARTING");
  332. do_restart = 1;
  333. // restart(-1);
  334. }
  335. static void
  336. got_usr1(int z)
  337. {
  338. signal(SIGUSR1, got_usr1);
  339. putlog(LOG_DEBUG, "*", "GOT SIGUSR1 -- RECHECKING BINARY");
  340. do_restart = 2;
  341. // reload_bin_data();
  342. }
  343. void init_signals()
  344. {
  345. signal(SIGBUS, got_bus);
  346. signal(SIGSEGV, got_segv);
  347. signal(SIGFPE, got_fpe);
  348. signal(SIGTERM, got_term);
  349. #ifndef CYGWIN_HACKS
  350. signal(SIGCONT, got_cont);
  351. #endif /* !CYGWIN_HACKS */
  352. signal(SIGABRT, got_abort);
  353. signal(SIGPIPE, SIG_IGN);
  354. signal(SIGILL, got_ill);
  355. signal(SIGALRM, got_alarm);
  356. signal(SIGHUP, got_hup);
  357. signal(SIGUSR1, got_usr1);
  358. }
  359. #ifdef DEBUG_CONTEXT
  360. /* Context */
  361. void eggContext(const char *file, int line)
  362. {
  363. char x[31] = "", *p = strrchr(file, '/');
  364. strlcpy(x, p ? p + 1 : file, sizeof x);
  365. cx_ptr = ((cx_ptr + 1) & 15);
  366. strcpy(cx_file[cx_ptr], x);
  367. cx_line[cx_ptr] = line;
  368. cx_note[cx_ptr][0] = 0;
  369. }
  370. /* Called from the ContextNote macro.
  371. */
  372. void eggContextNote(const char *file, int line, const char *note)
  373. {
  374. char x[31] = "", *p = strrchr(file, '/');
  375. strlcpy(x, p ? p + 1 : file, sizeof x);
  376. cx_ptr = ((cx_ptr + 1) & 15);
  377. strcpy(cx_file[cx_ptr], x);
  378. cx_line[cx_ptr] = line;
  379. strlcpy(cx_note[cx_ptr], note, sizeof cx_note[cx_ptr]);
  380. }
  381. #endif