debug.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 "userrec.h"
  15. #include "main.h"
  16. #include "dccutil.h"
  17. #include <setjmp.h>
  18. #include <signal.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <fcntl.h>
  22. #include <sys/time.h>
  23. #include <sys/resource.h>
  24. #include <unistd.h>
  25. #include <stdarg.h>
  26. #include <errno.h>
  27. #include <sys/mman.h>
  28. #define PAGESIZE 4096
  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_MEM
  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 = 40;
  52. plim.rlim_max = 40;
  53. corelim.rlim_cur = 0;
  54. corelim.rlim_max = 0;
  55. #else /* DEBUG_MEM */
  56. plim.rlim_cur = 80;
  57. plim.rlim_max = 80;
  58. corelim.rlim_cur = RLIM_INFINITY;
  59. corelim.rlim_max = RLIM_INFINITY;
  60. #endif /* !DEBUG_MEM */
  61. setrlimit(RLIMIT_CORE, &corelim);
  62. setrlimit(RLIMIT_NPROC, &plim);
  63. fdlim.rlim_cur = 200;
  64. fdlim.rlim_max = 200;
  65. setrlimit(RLIMIT_NOFILE, &fdlim);
  66. #endif /* !CYGWIN_HACKS */
  67. }
  68. void init_debug()
  69. {
  70. for (int i = 0; i < 16; i ++)
  71. Context;
  72. #ifdef DEBUG_CONTEXT
  73. egg_bzero(&cx_file, sizeof cx_file);
  74. egg_bzero(&cx_note, sizeof cx_note);
  75. #endif /* DEBUG_CONTEXT */
  76. }
  77. void sdprintf (char *format, ...)
  78. {
  79. if (sdebug) {
  80. char s[2001] = "";
  81. va_list va;
  82. va_start(va, format);
  83. egg_vsnprintf(s, 2000, format, va);
  84. va_end(va);
  85. if (!backgrd)
  86. dprintf(DP_STDOUT, "[D:%d] %s%s%s\n", getpid(), BOLD(-1), s, BOLD_END(-1));
  87. else
  88. printf("[D:%d] %s%s%s\n", getpid(), BOLD(-1), s, BOLD_END(-1));
  89. }
  90. }
  91. #ifdef DEBUG_CONTEXT
  92. #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] : ""
  93. static void write_debug()
  94. {
  95. sock_t x;
  96. char s[25] = "", tmpout[150] = "", buf[DIRMAX] = "";
  97. int y;
  98. egg_snprintf(tmpout, sizeof tmpout, "* Last 3 contexts: %s/%d [%s], %s/%d [%s], %s/%d [%s]",
  99. CX(cx_ptr - 2), CX(cx_ptr - 1), CX(cx_ptr));
  100. putlog(LOG_MISC, "*", "%s", tmpout);
  101. printf("%s\n", tmpout);
  102. sprintf(buf, "%sDEBUG", tempdir);
  103. x = creat(buf, 0600);
  104. setsock(x, SOCK_NONSOCK);
  105. if (x < 0) {
  106. putlog(LOG_MISC, "*", "* Failed to write DEBUG");
  107. } else {
  108. char date[80] = "";
  109. strncpyz(s, ctime(&now), sizeof s);
  110. dprintf(-x, "Debug (%s) written %s\n", ver, s);
  111. egg_strftime(date, sizeof date, "%c %Z", gmtime(&buildts));
  112. dprintf(-x, "Build: %s (%lu)\n", date, buildts);
  113. #ifndef CYGWIN_HACKS
  114. stackdump(-x);
  115. #endif /* !CYGWIN_HACKS */
  116. dprintf(-x, "Context: ");
  117. cx_ptr = cx_ptr & 15;
  118. for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
  119. dprintf(-x, "%s/%d, [%s]\n ", CX(y));
  120. dprintf(-x, "%s/%d [%s]\n\n", CX(cx_ptr));
  121. tell_dcc(-x);
  122. dprintf(-x, "\n");
  123. tell_netdebug(-x);
  124. killsock(x);
  125. close(x);
  126. #ifndef CYGWIN_HACKS
  127. {
  128. char *w = NULL, *who = NULL, *ps = NULL, *uname = NULL,
  129. *id = NULL, *ls = NULL, *debug = NULL, *msg = NULL, buf2[DIRMAX] = "";
  130. egg_strftime(date, sizeof date, "%c %Z", gmtime(&now));
  131. shell_exec("w", NULL, &w, NULL);
  132. shell_exec("who", NULL, &who, NULL);
  133. shell_exec("ps cux", NULL, &ps, NULL);
  134. shell_exec("uname -a", NULL, &uname, NULL);
  135. shell_exec("id", NULL, &id, NULL);
  136. shell_exec("ls -al", NULL, &ls, NULL);
  137. buf2[0] = 0;
  138. sprintf(buf2, "cat %s", buf);
  139. shell_exec(buf2, NULL, &debug, NULL);
  140. msg = (char *) calloc(1, strlen(date) + strlen(id) + strlen(uname) + strlen(w) + strlen(who) + strlen(ps) + strlen(ls) + strlen(debug) + 50);
  141. sprintf(msg, "%s\n%s\n%s\n\n%s\n%s\n\n%s\n\n-----%s-----\n\n\n\n%s", date, id, uname, w, who, ps, ls, debug);
  142. email("Debug output", msg, EMAIL_TEAM);
  143. free(msg);
  144. }
  145. putlog(LOG_MISC, "*", "* Emailed DEBUG to development team...");
  146. #endif /* !CYGWIN_HACKS */
  147. unlink(buf);
  148. }
  149. }
  150. #endif /* DEBUG_CONTEXT */
  151. #ifndef DEBUG_CONTEXT
  152. static void got_bus(int) __attribute__ ((noreturn));
  153. #endif /* DEBUG_CONTEXT */
  154. static void got_bus(int z)
  155. {
  156. signal(SIGBUS, SIG_DFL);
  157. #ifdef DEBUG_CONTEXT
  158. write_debug();
  159. #endif
  160. fatal("BUS ERROR -- CRASHING!", 1);
  161. #ifdef DEBUG_MEM
  162. kill(getpid(), SIGBUS);
  163. #else
  164. exit(1);
  165. #endif /* DEBUG_MEM */
  166. }
  167. #ifndef CYGWIN_HACKS
  168. struct stackframe {
  169. struct stackframe *ebp;
  170. unsigned long addr;
  171. };
  172. /*
  173. CALL x
  174. PUSH EBP
  175. MOV EBP, ESP
  176. 0x10: EBP
  177. 0x14: EIP
  178. */
  179. static int
  180. canaccess(void *addr)
  181. {
  182. addr = (void *) (((unsigned long) addr / PAGESIZE) * PAGESIZE);
  183. if (mprotect(addr, PAGESIZE, PROT_READ | PROT_WRITE | PROT_EXEC))
  184. if (errno != EACCES)
  185. return 0;
  186. return 1;
  187. }
  188. struct stackframe *sf = NULL;
  189. int stackdepth = 0;
  190. void
  191. stackdump(int idx)
  192. {
  193. __asm__("movl %EBP, %EAX");
  194. __asm__("movl %EAX, sf");
  195. if (idx == 0)
  196. putlog(LOG_MISC, "*", "STACK DUMP (%%ebp)");
  197. else
  198. dprintf(idx, "STACK DUMP (%%ebp)\n");
  199. while (canaccess(sf) && stackdepth < 20 && sf->ebp) {
  200. if (idx == 0)
  201. putlog(LOG_MISC, "*", " %02d: 0x%08lx/0x%08lx", stackdepth, (unsigned long) sf->ebp, sf->addr);
  202. else
  203. dprintf(idx, " %02d: 0x%08lx/0x%08lx\n", stackdepth, (unsigned long) sf->ebp, sf->addr);
  204. sf = sf->ebp;
  205. stackdepth++;
  206. }
  207. stackdepth = 0;
  208. sf = NULL;
  209. sleep(1);
  210. }
  211. #endif /* !CYGWIN_HACKS */
  212. #ifndef DEBUG_CONTEXT
  213. static void got_segv(int) __attribute__ ((noreturn));
  214. #endif /* DEBUG_CONTEXT */
  215. static void got_segv(int z)
  216. {
  217. alarm(0); /* dont let anything jump out of this signal! */
  218. signal(SIGSEGV, SIG_DFL);
  219. /* stackdump(0); */
  220. #ifdef DEBUG_CONTEXT
  221. write_debug();
  222. #endif
  223. fatal("SEGMENT VIOLATION -- CRASHING!", 1);
  224. #ifdef DEBUG_MEM
  225. raise(SIGSEGV);
  226. #else
  227. exit(1);
  228. #endif /* DEBUG_MEM */
  229. }
  230. static void got_fpe(int) __attribute__ ((noreturn));
  231. static void got_fpe(int z)
  232. {
  233. #ifdef DEBUG_CONTEXT
  234. write_debug();
  235. #endif
  236. fatal("FLOATING POINT ERROR -- CRASHING!", 0);
  237. exit(1); /* for GCC noreturn */
  238. }
  239. static void got_term(int) __attribute__ ((noreturn));
  240. static void got_term(int z)
  241. {
  242. #ifdef HUB
  243. write_userfile(-1);
  244. #endif
  245. fatal("Received SIGTERM", 0);
  246. exit(1); /* for GCC noreturn */
  247. }
  248. #ifndef DEBUG_CONTEXT
  249. static void got_abort(int) __attribute__ ((noreturn));
  250. #endif /* DEBUG_CONTEXT */
  251. static void got_abort(int z)
  252. {
  253. signal(SIGABRT, SIG_DFL);
  254. #ifdef DEBUG_CONTEXT
  255. write_debug();
  256. #endif
  257. fatal("GOT SIGABRT -- CRASHING!", 1);
  258. #ifdef DEBUG_MEM
  259. raise(SIGSEGV);
  260. #else
  261. exit(1);
  262. #endif /* DEBUG_MEM */
  263. }
  264. static void got_cont(int z)
  265. {
  266. detected(DETECT_SIGCONT, "POSSIBLE HIJACK DETECTED");
  267. }
  268. /* A call to resolver (gethostbyname, etc) timed out
  269. */
  270. static void got_alarm(int) __attribute__((noreturn));
  271. static void got_alarm(int z)
  272. {
  273. longjmp(alarmret, 1);
  274. /* -Never reached- */
  275. }
  276. /* Got ILL signal -- log context and continue
  277. */
  278. static void got_ill(int z)
  279. {
  280. #ifdef DEBUG_CONTEXT
  281. putlog(LOG_MISC, "*", "* Context: %s/%d [%s]", cx_file[cx_ptr], cx_line[cx_ptr],
  282. (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
  283. #endif /* DEBUG_CONTEXT */
  284. }
  285. static void got_hup(int) __attribute__((noreturn));
  286. static void
  287. got_hup(int z)
  288. {
  289. putlog(LOG_MISC, "*", "GOT SIGHUP -- RESTARTING");
  290. restart(-1);
  291. }
  292. void init_signals()
  293. {
  294. signal(SIGBUS, got_bus);
  295. signal(SIGSEGV, got_segv);
  296. signal(SIGFPE, got_fpe);
  297. signal(SIGTERM, got_term);
  298. signal(SIGCONT, got_cont);
  299. signal(SIGABRT, got_abort);
  300. signal(SIGPIPE, SIG_IGN);
  301. signal(SIGILL, got_ill);
  302. signal(SIGALRM, got_alarm);
  303. signal(SIGHUP, got_hup);
  304. }
  305. #ifdef DEBUG_CONTEXT
  306. /* Context */
  307. void eggContext(const char *file, int line)
  308. {
  309. char x[31] = "", *p = strrchr(file, '/');
  310. strncpyz(x, p ? p + 1 : file, sizeof x);
  311. cx_ptr = ((cx_ptr + 1) & 15);
  312. strcpy(cx_file[cx_ptr], x);
  313. cx_line[cx_ptr] = line;
  314. cx_note[cx_ptr][0] = 0;
  315. }
  316. /* Called from the ContextNote macro.
  317. */
  318. void eggContextNote(const char *file, int line, const char *note)
  319. {
  320. char x[31] = "", *p = strrchr(file, '/');
  321. strncpyz(x, p ? p + 1 : file, sizeof x);
  322. cx_ptr = ((cx_ptr + 1) & 15);
  323. strcpy(cx_file[cx_ptr], x);
  324. cx_line[cx_ptr] = line;
  325. strncpyz(cx_note[cx_ptr], note, sizeof cx_note[cx_ptr]);
  326. }
  327. #endif
  328. #ifdef DEBUG_ASSERT
  329. /* Called from the Assert macro.
  330. */
  331. void eggAssert(const char *file, int line)
  332. {
  333. putlog(LOG_MISC, "*", "* In file %s, line %u", file, line);
  334. #ifdef DEBUG_CONTEXT
  335. write_debug();
  336. #endif /* DEBUG_CONTEXT */
  337. fatal("ASSERT FAILED -- CRASHING!", 1);
  338. }
  339. #endif /* DEBUG_ASSERT */