debug.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. char* hexize(const unsigned char* data, size_t len) {
  96. static char buffers[5][513] = { "", "", "", "", "" };
  97. static int n = 0;
  98. char *buf = buffers[n++];
  99. buf[0] = 0;
  100. for (size_t i = 0; i < len; ++i) {
  101. if (i == 0)
  102. sprintf(buf, "%.2X", (int) (data[i]));
  103. else
  104. sprintf(buf, "%s %.2X", buf, (int) (data[i]));
  105. }
  106. buf[len * 3] = 0;
  107. if (n == 5) n = 0;
  108. return buf;
  109. }
  110. void printstr(unsigned char *str, int len)
  111. {
  112. #ifdef no
  113. static char *outstr;
  114. int i, n, c, usehex;
  115. char *s, *outend;
  116. int max_strlen = 64;
  117. int xflag = 0;
  118. outstr = (char *) my_calloc(1, 2 * max_strlen);
  119. outend = outstr + max_strlen * 2 - 10;
  120. n = (((max_strlen) < (len)) ? (max_strlen) : (len));
  121. usehex = 0;
  122. if (xflag > 1)
  123. usehex = 1;
  124. else if (xflag) {
  125. for (i = 0; i < n; i++) {
  126. c = str[i];
  127. if (len < 0 && c == '\0')
  128. break;
  129. if (!isprint(c) && !egg_isspace(c)) {
  130. usehex = 1;
  131. break;
  132. }
  133. }
  134. }
  135. s = outstr;
  136. *s++ = '\"';
  137. if (usehex) {
  138. for (i = 0; i < n; i++) {
  139. c = str[i];
  140. if (len < 0 && c == '\0')
  141. break;
  142. sprintf(s, "\\x%02x", c);
  143. s += 4;
  144. if (s > outend)
  145. break;
  146. }
  147. }
  148. else {
  149. for (i = 0; i < n; i++) {
  150. c = str[i];
  151. if (len < 0 && c == '\0')
  152. break;
  153. switch (c) {
  154. case '\"': case '\'': case '\\':
  155. *s++ = '\\'; *s++ = c; break;
  156. case '\f':
  157. *s++ = '\\'; *s++ = 'f'; break;
  158. case '\n':
  159. *s++ = '\\'; *s++ = 'n'; break;
  160. case '\r':
  161. *s++ = '\\'; *s++ = 'r'; break;
  162. case '\t':
  163. *s++ = '\\'; *s++ = 't'; break;
  164. case '\v':
  165. *s++ = '\\'; *s++ = 'v'; break;
  166. default:
  167. if (egg_isprint(c))
  168. *s++ = c;
  169. else if (i < n - 1 && egg_isdigit(str[i + 1])) {
  170. sprintf(s, "\\%03o", c);
  171. s += 4;
  172. }
  173. else {
  174. sprintf(s, "\\%o", c);
  175. s += strlen(s);
  176. }
  177. break;
  178. }
  179. if (s > outend)
  180. break;
  181. }
  182. }
  183. *s++ = '\"';
  184. if (i < len || (len < 0 && (i == n || s > outend))) {
  185. *s++ = '.'; *s++ = '.'; *s++ = '.';
  186. }
  187. *s = '\0';
  188. printf("%s\n", outstr);
  189. #endif
  190. }
  191. #ifdef DEBUG_CONTEXT
  192. #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] : ""
  193. static void write_debug()
  194. {
  195. char tmpout[150] = "";
  196. simple_snprintf(tmpout, sizeof tmpout, "* Last 3 contexts: %s/%d [%s], %s/%d [%s], %s/%d [%s]",
  197. CX(cx_ptr - 2), CX(cx_ptr - 1), CX(cx_ptr));
  198. putlog(LOG_MISC, "*", "%s (Paste to bryan)", tmpout);
  199. printf("%s\n", tmpout);
  200. }
  201. #endif /* DEBUG_CONTEXT */
  202. #ifndef DEBUG_CONTEXT
  203. static void got_bus(int) __attribute__ ((noreturn));
  204. #endif /* DEBUG_CONTEXT */
  205. static void got_bus(int z)
  206. {
  207. signal(SIGBUS, SIG_DFL);
  208. #ifdef DEBUG_CONTEXT
  209. write_debug();
  210. #endif
  211. fatal("BUS ERROR -- CRASHING!", 1);
  212. #ifdef DEBUG
  213. raise(SIGBUS);
  214. #else
  215. exit(1);
  216. #endif /* DEBUG */
  217. }
  218. #ifndef CYGWIN_HACKS
  219. #ifdef __i386__
  220. #ifndef PAGESIZE
  221. #define PAGESIZE 4096
  222. #endif
  223. struct stackframe {
  224. struct stackframe *ebp;
  225. unsigned long addr;
  226. };
  227. /*
  228. CALL x
  229. PUSH EBP
  230. MOV EBP, ESP
  231. 0x10: EBP
  232. 0x14: EIP
  233. */
  234. static int
  235. canaccess(void *addr)
  236. {
  237. addr = (void *) (((unsigned long) addr / PAGESIZE) * PAGESIZE);
  238. if (mprotect(addr, PAGESIZE, PROT_READ | PROT_WRITE | PROT_EXEC))
  239. if (errno != EACCES)
  240. return 0;
  241. return 1;
  242. }
  243. struct stackframe *sf = NULL;
  244. int stackdepth = 0;
  245. void
  246. stackdump(int idx)
  247. {
  248. __asm__("movl %EBP, %EAX");
  249. __asm__("movl %EAX, sf");
  250. if (idx == 0)
  251. putlog(LOG_MISC, "*", "STACK DUMP");
  252. else
  253. dprintf(idx, "STACK DUMP\n");
  254. while (canaccess(sf) && stackdepth < 20 && sf->ebp) {
  255. if (idx == 0)
  256. putlog(LOG_MISC, "*", " %02d: 0x%08lx/0x%08lx", stackdepth, (unsigned long) sf->ebp, sf->addr);
  257. else
  258. dprintf(idx, " %02d: 0x%08lx/0x%08lx\n", stackdepth, (unsigned long) sf->ebp, sf->addr);
  259. sf = sf->ebp;
  260. stackdepth++;
  261. }
  262. stackdepth = 0;
  263. sf = NULL;
  264. sleep(1);
  265. }
  266. #endif /* __i386__ */
  267. #endif /* !CYGWIN_HACKS */
  268. #ifndef DEBUG_CONTEXT
  269. static void got_segv(int) __attribute__ ((noreturn));
  270. #endif /* DEBUG_CONTEXT */
  271. static void got_segv(int z)
  272. {
  273. segfaulted = 1;
  274. alarm(0); /* dont let anything jump out of this signal! */
  275. signal(SIGSEGV, SIG_DFL);
  276. /* stackdump(0); */
  277. #ifdef DEBUG_CONTEXT
  278. write_debug();
  279. #endif
  280. fatal("SEGMENT VIOLATION -- CRASHING!", 1);
  281. #ifdef DEBUG
  282. raise(SIGSEGV);
  283. #else
  284. exit(1);
  285. #endif /* DEBUG */
  286. }
  287. static void got_fpe(int) __attribute__ ((noreturn));
  288. static void got_fpe(int z)
  289. {
  290. #ifdef DEBUG_CONTEXT
  291. write_debug();
  292. #endif
  293. fatal("FLOATING POINT ERROR -- CRASHING!", 0);
  294. exit(1); /* for GCC noreturn */
  295. }
  296. static void got_term(int) __attribute__ ((noreturn));
  297. static void got_term(int z)
  298. {
  299. if (conf.bot->hub)
  300. write_userfile(-1);
  301. fatal("Received SIGTERM", 0);
  302. exit(1); /* for GCC noreturn */
  303. }
  304. #ifndef DEBUG_CONTEXT
  305. static void got_abort(int) __attribute__ ((noreturn));
  306. #endif /* DEBUG_CONTEXT */
  307. static void got_abort(int z)
  308. {
  309. signal(SIGABRT, SIG_DFL);
  310. #ifdef DEBUG_CONTEXT
  311. write_debug();
  312. #endif
  313. fatal("GOT SIGABRT -- CRASHING!", 1);
  314. #ifdef DEBUG
  315. raise(SIGSEGV);
  316. #else
  317. exit(1);
  318. #endif /* DEBUG */
  319. }
  320. #ifndef CYGWIN_HACKS
  321. static void got_cont(int z)
  322. {
  323. detected(DETECT_HIJACK, "POSSIBLE HIJACK DETECTED (!! MAY BE BOX REBOOT !!)");
  324. }
  325. #endif /* !CYGWIN_HACKS */
  326. static void got_alarm(int) __attribute__((noreturn));
  327. static void got_alarm(int z)
  328. {
  329. sdprintf("SIGALARM");
  330. longjmp(alarmret, 1);
  331. /* -Never reached- */
  332. }
  333. /* Got ILL signal -- log context and continue
  334. */
  335. static void got_ill(int z)
  336. {
  337. #ifdef DEBUG_CONTEXT
  338. putlog(LOG_MISC, "*", "* Context: %s/%d [%s]", cx_file[cx_ptr], cx_line[cx_ptr],
  339. (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
  340. #endif /* DEBUG_CONTEXT */
  341. }
  342. static void
  343. got_hup(int z)
  344. {
  345. signal(SIGHUP, got_hup);
  346. putlog(LOG_MISC, "*", "GOT SIGHUP -- RESTARTING");
  347. do_restart = 1;
  348. // restart(-1);
  349. }
  350. static void
  351. got_usr1(int z)
  352. {
  353. signal(SIGUSR1, got_usr1);
  354. putlog(LOG_DEBUG, "*", "GOT SIGUSR1 -- RECHECKING BINARY");
  355. do_restart = 2;
  356. // reload_bin_data();
  357. }
  358. void init_signals()
  359. {
  360. signal(SIGBUS, got_bus);
  361. signal(SIGSEGV, got_segv);
  362. signal(SIGFPE, got_fpe);
  363. signal(SIGTERM, got_term);
  364. #ifndef CYGWIN_HACKS
  365. signal(SIGCONT, got_cont);
  366. #endif /* !CYGWIN_HACKS */
  367. signal(SIGABRT, got_abort);
  368. signal(SIGPIPE, SIG_IGN);
  369. signal(SIGILL, got_ill);
  370. signal(SIGALRM, got_alarm);
  371. signal(SIGHUP, got_hup);
  372. signal(SIGUSR1, got_usr1);
  373. }
  374. #ifdef DEBUG_CONTEXT
  375. /* Context */
  376. void eggContext(const char *file, int line)
  377. {
  378. char x[31] = "", *p = strrchr(file, '/');
  379. strlcpy(x, p ? p + 1 : file, sizeof x);
  380. cx_ptr = ((cx_ptr + 1) & 15);
  381. strcpy(cx_file[cx_ptr], x);
  382. cx_line[cx_ptr] = line;
  383. cx_note[cx_ptr][0] = 0;
  384. }
  385. /* Called from the ContextNote macro.
  386. */
  387. void eggContextNote(const char *file, int line, const char *note)
  388. {
  389. char x[31] = "", *p = strrchr(file, '/');
  390. strlcpy(x, p ? p + 1 : file, sizeof x);
  391. cx_ptr = ((cx_ptr + 1) & 15);
  392. strcpy(cx_file[cx_ptr], x);
  393. cx_line[cx_ptr] = line;
  394. strlcpy(cx_note[cx_ptr], note, sizeof cx_note[cx_ptr]);
  395. }
  396. #endif