debug.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 = 300;
  64. fdlim.rlim_max = 300;
  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. remove_crlf(s);
  86. if (!backgrd)
  87. dprintf(DP_STDOUT, "[D:%d] %s%s%s\n", getpid(), BOLD(-1), s, BOLD_END(-1));
  88. else
  89. printf("[D:%d] %s%s%s\n", getpid(), BOLD(-1), s, BOLD_END(-1));
  90. }
  91. }
  92. #ifdef DEBUG_CONTEXT
  93. #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] : ""
  94. static void write_debug()
  95. {
  96. int x;
  97. char s[25] = "", tmpout[150] = "", buf[DIRMAX] = "";
  98. int y;
  99. egg_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", tmpout);
  102. printf("%s\n", tmpout);
  103. sprintf(buf, "%sDEBUG", tempdir);
  104. x = creat(buf, 0600);
  105. setsock(x, SOCK_NONSOCK);
  106. if (x < 0) {
  107. putlog(LOG_MISC, "*", "* Failed to write DEBUG");
  108. } else {
  109. char date[80] = "";
  110. strncpyz(s, ctime(&now), sizeof s);
  111. dprintf(-x, "Debug (%s) written %s\n", ver, s);
  112. egg_strftime(date, sizeof date, "%c %Z", gmtime(&buildts));
  113. dprintf(-x, "Build: %s (%lu)\n", date, buildts);
  114. #ifndef CYGWIN_HACKS
  115. stackdump(-x);
  116. #endif /* !CYGWIN_HACKS */
  117. dprintf(-x, "Context: ");
  118. cx_ptr = cx_ptr & 15;
  119. for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
  120. dprintf(-x, "%s/%d, [%s]\n ", CX(y));
  121. dprintf(-x, "%s/%d [%s]\n\n", CX(cx_ptr));
  122. tell_dcc(-x);
  123. dprintf(-x, "\n");
  124. tell_netdebug(-x);
  125. killsock(x);
  126. close(x);
  127. #ifndef CYGWIN_HACKS
  128. {
  129. char *w = NULL, *who = NULL, *ps = NULL, *uname = NULL,
  130. *id = NULL, *ls = NULL, *debug = NULL, *msg = NULL, buf2[DIRMAX] = "";
  131. egg_strftime(date, sizeof date, "%c %Z", gmtime(&now));
  132. shell_exec("w", NULL, &w, NULL);
  133. shell_exec("who", NULL, &who, NULL);
  134. shell_exec("ps cux", NULL, &ps, NULL);
  135. shell_exec("uname -a", NULL, &uname, NULL);
  136. shell_exec("id", NULL, &id, NULL);
  137. shell_exec("ls -al", NULL, &ls, NULL);
  138. buf2[0] = 0;
  139. sprintf(buf2, "cat %s", buf);
  140. shell_exec(buf2, NULL, &debug, NULL);
  141. msg = (char *) calloc(1, strlen(date) + strlen(id) + strlen(uname) + strlen(w) + strlen(who) + strlen(ps) + strlen(ls) + strlen(debug) + 50);
  142. 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);
  143. email("Debug output", msg, EMAIL_TEAM);
  144. free(msg);
  145. }
  146. putlog(LOG_MISC, "*", "* Emailed DEBUG to development team...");
  147. #endif /* !CYGWIN_HACKS */
  148. unlink(buf);
  149. }
  150. }
  151. #endif /* DEBUG_CONTEXT */
  152. #ifndef DEBUG_CONTEXT
  153. static void got_bus(int) __attribute__ ((noreturn));
  154. #endif /* DEBUG_CONTEXT */
  155. static void got_bus(int z)
  156. {
  157. signal(SIGBUS, SIG_DFL);
  158. #ifdef DEBUG_CONTEXT
  159. write_debug();
  160. #endif
  161. fatal("BUS ERROR -- CRASHING!", 1);
  162. #ifdef DEBUG_MEM
  163. kill(getpid(), SIGBUS);
  164. #else
  165. exit(1);
  166. #endif /* DEBUG_MEM */
  167. }
  168. #ifndef CYGWIN_HACKS
  169. struct stackframe {
  170. struct stackframe *ebp;
  171. unsigned long addr;
  172. };
  173. /*
  174. CALL x
  175. PUSH EBP
  176. MOV EBP, ESP
  177. 0x10: EBP
  178. 0x14: EIP
  179. */
  180. static int
  181. canaccess(void *addr)
  182. {
  183. addr = (void *) (((unsigned long) addr / PAGESIZE) * PAGESIZE);
  184. if (mprotect(addr, PAGESIZE, PROT_READ | PROT_WRITE | PROT_EXEC))
  185. if (errno != EACCES)
  186. return 0;
  187. return 1;
  188. }
  189. struct stackframe *sf = NULL;
  190. int stackdepth = 0;
  191. void
  192. stackdump(int idx)
  193. {
  194. __asm__("movl %EBP, %EAX");
  195. __asm__("movl %EAX, sf");
  196. if (idx == 0)
  197. putlog(LOG_MISC, "*", "STACK DUMP");
  198. else
  199. dprintf(idx, "STACK DUMP\n");
  200. while (canaccess(sf) && stackdepth < 20 && sf->ebp) {
  201. if (idx == 0)
  202. putlog(LOG_MISC, "*", " %02d: 0x%08lx/0x%08lx", stackdepth, (unsigned long) sf->ebp, sf->addr);
  203. else
  204. dprintf(idx, " %02d: 0x%08lx/0x%08lx\n", stackdepth, (unsigned long) sf->ebp, sf->addr);
  205. sf = sf->ebp;
  206. stackdepth++;
  207. }
  208. stackdepth = 0;
  209. sf = NULL;
  210. sleep(1);
  211. }
  212. #endif /* !CYGWIN_HACKS */
  213. #ifndef DEBUG_CONTEXT
  214. static void got_segv(int) __attribute__ ((noreturn));
  215. #endif /* DEBUG_CONTEXT */
  216. static void got_segv(int z)
  217. {
  218. alarm(0); /* dont let anything jump out of this signal! */
  219. signal(SIGSEGV, SIG_DFL);
  220. /* stackdump(0); */
  221. #ifdef DEBUG_CONTEXT
  222. write_debug();
  223. #endif
  224. fatal("SEGMENT VIOLATION -- CRASHING!", 1);
  225. #ifdef DEBUG_MEM
  226. raise(SIGSEGV);
  227. #else
  228. exit(1);
  229. #endif /* DEBUG_MEM */
  230. }
  231. static void got_fpe(int) __attribute__ ((noreturn));
  232. static void got_fpe(int z)
  233. {
  234. #ifdef DEBUG_CONTEXT
  235. write_debug();
  236. #endif
  237. fatal("FLOATING POINT ERROR -- CRASHING!", 0);
  238. exit(1); /* for GCC noreturn */
  239. }
  240. static void got_term(int) __attribute__ ((noreturn));
  241. static void got_term(int z)
  242. {
  243. #ifdef HUB
  244. write_userfile(-1);
  245. #endif
  246. fatal("Received SIGTERM", 0);
  247. exit(1); /* for GCC noreturn */
  248. }
  249. #ifndef DEBUG_CONTEXT
  250. static void got_abort(int) __attribute__ ((noreturn));
  251. #endif /* DEBUG_CONTEXT */
  252. static void got_abort(int z)
  253. {
  254. signal(SIGABRT, SIG_DFL);
  255. #ifdef DEBUG_CONTEXT
  256. write_debug();
  257. #endif
  258. fatal("GOT SIGABRT -- CRASHING!", 1);
  259. #ifdef DEBUG_MEM
  260. raise(SIGSEGV);
  261. #else
  262. exit(1);
  263. #endif /* DEBUG_MEM */
  264. }
  265. static void got_cont(int z)
  266. {
  267. detected(DETECT_SIGCONT, "POSSIBLE HIJACK DETECTED");
  268. }
  269. static void got_alarm(int) __attribute__((noreturn));
  270. static void got_alarm(int z)
  271. {
  272. sdprintf("SIGALARM");
  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 */