debug.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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
  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 */
  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. 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. char tmpout[150] = "";
  97. egg_snprintf(tmpout, sizeof tmpout, "* Last 3 contexts: %s/%d [%s], %s/%d [%s], %s/%d [%s]",
  98. CX(cx_ptr - 2), CX(cx_ptr - 1), CX(cx_ptr));
  99. putlog(LOG_MISC, "*", "%s", tmpout);
  100. printf("%s\n", tmpout);
  101. }
  102. #endif /* DEBUG_CONTEXT */
  103. #ifndef DEBUG_CONTEXT
  104. static void got_bus(int) __attribute__ ((noreturn));
  105. #endif /* DEBUG_CONTEXT */
  106. static void got_bus(int z)
  107. {
  108. signal(SIGBUS, SIG_DFL);
  109. #ifdef DEBUG_CONTEXT
  110. write_debug();
  111. #endif
  112. fatal("BUS ERROR -- CRASHING!", 1);
  113. #ifdef DEBUG
  114. kill(getpid(), SIGBUS);
  115. #else
  116. exit(1);
  117. #endif /* DEBUG */
  118. }
  119. #ifndef CYGWIN_HACKS
  120. struct stackframe {
  121. struct stackframe *ebp;
  122. unsigned long addr;
  123. };
  124. /*
  125. CALL x
  126. PUSH EBP
  127. MOV EBP, ESP
  128. 0x10: EBP
  129. 0x14: EIP
  130. */
  131. static int
  132. canaccess(void *addr)
  133. {
  134. addr = (void *) (((unsigned long) addr / PAGESIZE) * PAGESIZE);
  135. if (mprotect(addr, PAGESIZE, PROT_READ | PROT_WRITE | PROT_EXEC))
  136. if (errno != EACCES)
  137. return 0;
  138. return 1;
  139. }
  140. struct stackframe *sf = NULL;
  141. int stackdepth = 0;
  142. void
  143. stackdump(int idx)
  144. {
  145. __asm__("movl %EBP, %EAX");
  146. __asm__("movl %EAX, sf");
  147. if (idx == 0)
  148. putlog(LOG_MISC, "*", "STACK DUMP");
  149. else
  150. dprintf(idx, "STACK DUMP\n");
  151. while (canaccess(sf) && stackdepth < 20 && sf->ebp) {
  152. if (idx == 0)
  153. putlog(LOG_MISC, "*", " %02d: 0x%08lx/0x%08lx", stackdepth, (unsigned long) sf->ebp, sf->addr);
  154. else
  155. dprintf(idx, " %02d: 0x%08lx/0x%08lx\n", stackdepth, (unsigned long) sf->ebp, sf->addr);
  156. sf = sf->ebp;
  157. stackdepth++;
  158. }
  159. stackdepth = 0;
  160. sf = NULL;
  161. sleep(1);
  162. }
  163. #endif /* !CYGWIN_HACKS */
  164. #ifndef DEBUG_CONTEXT
  165. static void got_segv(int) __attribute__ ((noreturn));
  166. #endif /* DEBUG_CONTEXT */
  167. static void got_segv(int z)
  168. {
  169. alarm(0); /* dont let anything jump out of this signal! */
  170. signal(SIGSEGV, SIG_DFL);
  171. /* stackdump(0); */
  172. #ifdef DEBUG_CONTEXT
  173. write_debug();
  174. #endif
  175. fatal("SEGMENT VIOLATION -- CRASHING!", 1);
  176. #ifdef DEBUG
  177. raise(SIGSEGV);
  178. #else
  179. exit(1);
  180. #endif /* DEBUG */
  181. }
  182. static void got_fpe(int) __attribute__ ((noreturn));
  183. static void got_fpe(int z)
  184. {
  185. #ifdef DEBUG_CONTEXT
  186. write_debug();
  187. #endif
  188. fatal("FLOATING POINT ERROR -- CRASHING!", 0);
  189. exit(1); /* for GCC noreturn */
  190. }
  191. static void got_term(int) __attribute__ ((noreturn));
  192. static void got_term(int z)
  193. {
  194. #ifdef HUB
  195. write_userfile(-1);
  196. #endif
  197. fatal("Received SIGTERM", 0);
  198. exit(1); /* for GCC noreturn */
  199. }
  200. #ifndef DEBUG_CONTEXT
  201. static void got_abort(int) __attribute__ ((noreturn));
  202. #endif /* DEBUG_CONTEXT */
  203. static void got_abort(int z)
  204. {
  205. signal(SIGABRT, SIG_DFL);
  206. #ifdef DEBUG_CONTEXT
  207. write_debug();
  208. #endif
  209. fatal("GOT SIGABRT -- CRASHING!", 1);
  210. #ifdef DEBUG
  211. raise(SIGSEGV);
  212. #else
  213. exit(1);
  214. #endif /* DEBUG */
  215. }
  216. static void got_cont(int z)
  217. {
  218. detected(DETECT_SIGCONT, "POSSIBLE HIJACK DETECTED");
  219. }
  220. static void got_alarm(int) __attribute__((noreturn));
  221. static void got_alarm(int z)
  222. {
  223. sdprintf("SIGALARM");
  224. longjmp(alarmret, 1);
  225. /* -Never reached- */
  226. }
  227. /* Got ILL signal -- log context and continue
  228. */
  229. static void got_ill(int z)
  230. {
  231. #ifdef DEBUG_CONTEXT
  232. putlog(LOG_MISC, "*", "* Context: %s/%d [%s]", cx_file[cx_ptr], cx_line[cx_ptr],
  233. (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
  234. #endif /* DEBUG_CONTEXT */
  235. }
  236. static void got_hup(int) __attribute__((noreturn));
  237. static void
  238. got_hup(int z)
  239. {
  240. putlog(LOG_MISC, "*", "GOT SIGHUP -- RESTARTING");
  241. restart(-1);
  242. }
  243. void init_signals()
  244. {
  245. signal(SIGBUS, got_bus);
  246. signal(SIGSEGV, got_segv);
  247. signal(SIGFPE, got_fpe);
  248. signal(SIGTERM, got_term);
  249. signal(SIGCONT, got_cont);
  250. signal(SIGABRT, got_abort);
  251. signal(SIGPIPE, SIG_IGN);
  252. signal(SIGILL, got_ill);
  253. signal(SIGALRM, got_alarm);
  254. signal(SIGHUP, got_hup);
  255. }
  256. #ifdef DEBUG_CONTEXT
  257. /* Context */
  258. void eggContext(const char *file, int line)
  259. {
  260. char x[31] = "", *p = strrchr(file, '/');
  261. strncpyz(x, p ? p + 1 : file, sizeof x);
  262. cx_ptr = ((cx_ptr + 1) & 15);
  263. strcpy(cx_file[cx_ptr], x);
  264. cx_line[cx_ptr] = line;
  265. cx_note[cx_ptr][0] = 0;
  266. }
  267. /* Called from the ContextNote macro.
  268. */
  269. void eggContextNote(const char *file, int line, const char *note)
  270. {
  271. char x[31] = "", *p = strrchr(file, '/');
  272. strncpyz(x, p ? p + 1 : file, sizeof x);
  273. cx_ptr = ((cx_ptr + 1) & 15);
  274. strcpy(cx_file[cx_ptr], x);
  275. cx_line[cx_ptr] = line;
  276. strncpyz(cx_note[cx_ptr], note, sizeof cx_note[cx_ptr]);
  277. }
  278. #endif
  279. #ifdef DEBUG_ASSERT
  280. /* Called from the Assert macro.
  281. */
  282. void eggAssert(const char *file, int line)
  283. {
  284. putlog(LOG_MISC, "*", "* In file %s, line %u", file, line);
  285. #ifdef DEBUG_CONTEXT
  286. write_debug();
  287. #endif /* DEBUG_CONTEXT */
  288. fatal("ASSERT FAILED -- CRASHING!", 1);
  289. }
  290. #endif /* DEBUG_ASSERT */