You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
5.9 KiB

24 years ago
  1. /* Copyright (C) 2000 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  13. #include <my_global.h>
  14. #include "stacktrace.h"
  15. #include <signal.h>
  16. #include <my_pthread.h>
  17. #ifdef HAVE_STACKTRACE
  18. #include <unistd.h>
  19. #define PTR_SANE(p) ((p) && (char*)(p) >= heap_start && (char*)(p) <= heap_end)
  20. char *heap_start;
  21. void safe_print_str(const char* name, const char* val, int max_len)
  22. {
  23. char *heap_end= (char*) sbrk(0);
  24. fprintf(stderr, "%s at %p ", name, val);
  25. if (!PTR_SANE(val))
  26. {
  27. fprintf(stderr, " is invalid pointer\n");
  28. return;
  29. }
  30. fprintf(stderr, "= ");
  31. for (; max_len && PTR_SANE(val) && *val; --max_len)
  32. fputc(*val++, stderr);
  33. fputc('\n', stderr);
  34. }
  35. #ifdef TARGET_OS_LINUX
  36. #define SIGRETURN_FRAME_COUNT 2
  37. #if defined(__alpha__) && defined(__GNUC__)
  38. /*
  39. The only way to backtrace without a symbol table on alpha
  40. is to find stq fp,N(sp), and the first byte
  41. of the instruction opcode will give us the value of N. From this
  42. we can find where the old value of fp is stored
  43. */
  44. #define MAX_INSTR_IN_FUNC 10000
  45. inline uchar** find_prev_fp(uint32* pc, uchar** fp)
  46. {
  47. int i;
  48. for (i = 0; i < MAX_INSTR_IN_FUNC; ++i,--pc)
  49. {
  50. uchar* p = (uchar*)pc;
  51. if (p[2] == 222 && p[3] == 35)
  52. {
  53. return (uchar**)((uchar*)fp - *(short int*)p);
  54. }
  55. }
  56. return 0;
  57. }
  58. inline uint32* find_prev_pc(uint32* pc, uchar** fp)
  59. {
  60. int i;
  61. for (i = 0; i < MAX_INSTR_IN_FUNC; ++i,--pc)
  62. {
  63. char* p = (char*)pc;
  64. if (p[1] == 0 && p[2] == 94 && p[3] == -73)
  65. {
  66. uint32* prev_pc = (uint32*)*((fp+p[0]/sizeof(fp)));
  67. return prev_pc;
  68. }
  69. }
  70. return 0;
  71. }
  72. #endif /* defined(__alpha__) && defined(__GNUC__) */
  73. void print_stacktrace(gptr stack_bottom, ulong thread_stack)
  74. {
  75. uchar** fp;
  76. uint frame_count = 0;
  77. #if defined(__alpha__) && defined(__GNUC__)
  78. uint32* pc;
  79. #endif
  80. LINT_INIT(fp);
  81. fprintf(stderr,"\
  82. Attempting backtrace. You can use the following information to find out\n\
  83. where mysqld died. If you see no messages after this, something went\n\
  84. terribly wrong...\n");
  85. #ifdef __i386__
  86. __asm __volatile__ ("movl %%ebp,%0"
  87. :"=r"(fp)
  88. :"r"(fp));
  89. if (!fp)
  90. {
  91. fprintf(stderr, "frame pointer (ebp) is NULL, did you compile with\n\
  92. -fomit-frame-pointer? Aborting backtrace!\n");
  93. return;
  94. }
  95. #endif
  96. #if defined(__alpha__) && defined(__GNUC__)
  97. __asm __volatile__ ("mov $30,%0"
  98. :"=r"(fp)
  99. :"r"(fp));
  100. if (!fp)
  101. {
  102. fprintf(stderr, "frame pointer (fp) is NULL, did you compile with\n\
  103. -fomit-frame-pointer? Aborting backtrace!\n");
  104. return;
  105. }
  106. #endif /* __alpha__ */
  107. if (!stack_bottom || (gptr) stack_bottom > (gptr) &fp)
  108. {
  109. ulong tmp= min(0x10000,thread_stack);
  110. /* Assume that the stack starts at the previous even 65K */
  111. stack_bottom= (gptr) (((ulong) &fp + tmp) &
  112. ~(ulong) 0xFFFF);
  113. fprintf(stderr, "Cannot determine thread, fp=%p, backtrace may not be correct.\n", fp);
  114. }
  115. if (fp > (uchar**) stack_bottom ||
  116. fp < (uchar**) stack_bottom - thread_stack)
  117. {
  118. fprintf(stderr, "Bogus stack limit or frame pointer,\
  119. fp=%p, stack_bottom=%p, thread_stack=%ld, aborting backtrace.\n",
  120. fp, stack_bottom, thread_stack);
  121. return;
  122. }
  123. fprintf(stderr, "Stack range sanity check OK, backtrace follows:\n");
  124. #if defined(__alpha__) && defined(__GNUC__)
  125. fprintf(stderr, "Warning: Alpha stacks are difficult -\
  126. will be taking some wild guesses, stack trace may be incorrect or \
  127. terminate abruptly\n");
  128. /* On Alpha, we need to get pc */
  129. __asm __volatile__ ("bsr %0, do_next; do_next: "
  130. :"=r"(pc)
  131. :"r"(pc));
  132. #endif /* __alpha__ */
  133. while (fp < (uchar**) stack_bottom)
  134. {
  135. #ifdef __i386__
  136. uchar** new_fp = (uchar**)*fp;
  137. fprintf(stderr, "%p\n", frame_count == SIGRETURN_FRAME_COUNT ?
  138. *(fp+17) : *(fp+1));
  139. #endif /* __386__ */
  140. #if defined(__alpha__) && defined(__GNUC__)
  141. uchar** new_fp = find_prev_fp(pc, fp);
  142. if (frame_count == SIGRETURN_FRAME_COUNT - 1)
  143. {
  144. new_fp += 90;
  145. }
  146. if (fp && pc)
  147. {
  148. pc = find_prev_pc(pc, fp);
  149. if (pc)
  150. fprintf(stderr, "%p\n", pc);
  151. else
  152. {
  153. fprintf(stderr, "Not smart enough to deal with the rest\
  154. of this stack\n");
  155. goto end;
  156. }
  157. }
  158. else
  159. {
  160. fprintf(stderr, "Not smart enough to deal with the rest of this stack\n");
  161. goto end;
  162. }
  163. #endif /* defined(__alpha__) && defined(__GNUC__) */
  164. if (new_fp <= fp )
  165. {
  166. fprintf(stderr, "New value of fp=%p failed sanity check,\
  167. terminating stack trace!\n", new_fp);
  168. goto end;
  169. }
  170. fp = new_fp;
  171. ++frame_count;
  172. }
  173. fprintf(stderr, "Stack trace seems successful - bottom reached\n");
  174. end:
  175. fprintf(stderr, "Please read http://dev.mysql.com/doc/mysql/en/Using_stack_trace.html and follow instructions on how to resolve the stack trace. Resolved\n\
  176. stack trace is much more helpful in diagnosing the problem, so please do \n\
  177. resolve it\n");
  178. }
  179. #endif /* TARGET_OS_LINUX */
  180. #endif /* HAVE_STACKTRACE */
  181. /* Produce a core for the thread */
  182. #ifdef NOT_USED /* HAVE_LINUXTHREADS */
  183. void write_core(int sig)
  184. {
  185. signal(sig, SIG_DFL);
  186. if (fork() != 0) exit(1); /* Abort main program */
  187. /* Core will be written at exit */
  188. }
  189. #else
  190. void write_core(int sig)
  191. {
  192. signal(sig, SIG_DFL);
  193. pthread_kill(pthread_self(), sig);
  194. #if defined(P_MYID) && !defined(SCO)
  195. /* On Solaris, the above kill is not enough */
  196. sigsend(P_PID,P_MYID,sig);
  197. #endif
  198. }
  199. #endif