debug.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. Minetest-c55
  3. Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "debug.h"
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. /*
  20. Debug output
  21. */
  22. FILE *g_debugstreams[DEBUGSTREAM_COUNT] = {stderr, NULL};
  23. void debugstreams_init(bool disable_stderr, const char *filename)
  24. {
  25. if(disable_stderr)
  26. g_debugstreams[0] = NULL;
  27. else
  28. g_debugstreams[0] = stderr;
  29. if(filename)
  30. g_debugstreams[1] = fopen(filename, "a");
  31. if(g_debugstreams[1])
  32. {
  33. fprintf(g_debugstreams[1], "\n\n-------------\n");
  34. fprintf(g_debugstreams[1], " Separator \n");
  35. fprintf(g_debugstreams[1], "-------------\n\n");
  36. }
  37. DEBUGPRINT("Debug streams initialized, disable_stderr=%d\n",
  38. disable_stderr);
  39. }
  40. void debugstreams_deinit()
  41. {
  42. if(g_debugstreams[1] != NULL)
  43. fclose(g_debugstreams[1]);
  44. }
  45. Debugbuf debugbuf(false);
  46. std::ostream dstream(&debugbuf);
  47. Debugbuf debugbuf_no_stderr(true);
  48. std::ostream dstream_no_stderr(&debugbuf_no_stderr);
  49. Nullstream dummyout;
  50. /*
  51. Assert
  52. */
  53. void assert_fail(const char *assertion, const char *file,
  54. unsigned int line, const char *function)
  55. {
  56. DEBUGPRINT("\nIn thread %lx:\n"
  57. "%s:%d: %s: Assertion '%s' failed.\n",
  58. (unsigned long)get_current_thread_id(),
  59. file, line, function, assertion);
  60. debug_stacks_print();
  61. if(g_debugstreams[1])
  62. fclose(g_debugstreams[1]);
  63. abort();
  64. }
  65. /*
  66. DebugStack
  67. */
  68. DebugStack::DebugStack(threadid_t id)
  69. {
  70. threadid = id;
  71. stack_i = 0;
  72. stack_max_i = 0;
  73. memset(stack, 0, DEBUG_STACK_SIZE*DEBUG_STACK_TEXT_SIZE);
  74. }
  75. void DebugStack::print(FILE *file, bool everything)
  76. {
  77. fprintf(file, "DEBUG STACK FOR THREAD %lx:\n",
  78. (unsigned long)threadid);
  79. for(int i=0; i<stack_max_i; i++)
  80. {
  81. if(i == stack_i && everything == false)
  82. break;
  83. if(i < stack_i)
  84. fprintf(file, "#%d %s\n", i, stack[i]);
  85. else
  86. fprintf(file, "(Leftover data: #%d %s)\n", i, stack[i]);
  87. }
  88. if(stack_i == DEBUG_STACK_SIZE)
  89. fprintf(file, "Probably overflown.\n");
  90. }
  91. void DebugStack::print(std::ostream &os, bool everything)
  92. {
  93. os<<"DEBUG STACK FOR THREAD "<<(unsigned long)threadid<<": "<<std::endl;
  94. for(int i=0; i<stack_max_i; i++)
  95. {
  96. if(i == stack_i && everything == false)
  97. break;
  98. if(i < stack_i)
  99. os<<"#"<<i<<" "<<stack[i]<<std::endl;
  100. else
  101. os<<"(Leftover data: #"<<i<<" "<<stack[i]<<")"<<std::endl;
  102. }
  103. if(stack_i == DEBUG_STACK_SIZE)
  104. os<<"Probably overflown."<<std::endl;
  105. }
  106. core::map<threadid_t, DebugStack*> g_debug_stacks;
  107. JMutex g_debug_stacks_mutex;
  108. void debug_stacks_init()
  109. {
  110. g_debug_stacks_mutex.Init();
  111. }
  112. void debug_stacks_print_to(std::ostream &os)
  113. {
  114. JMutexAutoLock lock(g_debug_stacks_mutex);
  115. os<<"Debug stacks:"<<std::endl;
  116. for(core::map<threadid_t, DebugStack*>::Iterator
  117. i = g_debug_stacks.getIterator();
  118. i.atEnd() == false; i++)
  119. {
  120. DebugStack *stack = i.getNode()->getValue();
  121. stack->print(os, false);
  122. }
  123. }
  124. void debug_stacks_print()
  125. {
  126. JMutexAutoLock lock(g_debug_stacks_mutex);
  127. DEBUGPRINT("Debug stacks:\n");
  128. for(core::map<threadid_t, DebugStack*>::Iterator
  129. i = g_debug_stacks.getIterator();
  130. i.atEnd() == false; i++)
  131. {
  132. DebugStack *stack = i.getNode()->getValue();
  133. for(int i=0; i<DEBUGSTREAM_COUNT; i++)
  134. {
  135. if(g_debugstreams[i] != NULL)
  136. stack->print(g_debugstreams[i], true);
  137. }
  138. }
  139. }
  140. DebugStacker::DebugStacker(const char *text)
  141. {
  142. threadid_t threadid = get_current_thread_id();
  143. JMutexAutoLock lock(g_debug_stacks_mutex);
  144. core::map<threadid_t, DebugStack*>::Node *n;
  145. n = g_debug_stacks.find(threadid);
  146. if(n != NULL)
  147. {
  148. m_stack = n->getValue();
  149. }
  150. else
  151. {
  152. /*DEBUGPRINT("Creating new debug stack for thread %x\n",
  153. (unsigned int)threadid);*/
  154. m_stack = new DebugStack(threadid);
  155. g_debug_stacks.insert(threadid, m_stack);
  156. }
  157. if(m_stack->stack_i >= DEBUG_STACK_SIZE)
  158. {
  159. m_overflowed = true;
  160. }
  161. else
  162. {
  163. m_overflowed = false;
  164. snprintf(m_stack->stack[m_stack->stack_i],
  165. DEBUG_STACK_TEXT_SIZE, "%s", text);
  166. m_stack->stack_i++;
  167. if(m_stack->stack_i > m_stack->stack_max_i)
  168. m_stack->stack_max_i = m_stack->stack_i;
  169. }
  170. }
  171. DebugStacker::~DebugStacker()
  172. {
  173. JMutexAutoLock lock(g_debug_stacks_mutex);
  174. if(m_overflowed == true)
  175. return;
  176. m_stack->stack_i--;
  177. if(m_stack->stack_i == 0)
  178. {
  179. threadid_t threadid = m_stack->threadid;
  180. /*DEBUGPRINT("Deleting debug stack for thread %x\n",
  181. (unsigned int)threadid);*/
  182. delete m_stack;
  183. g_debug_stacks.remove(threadid);
  184. }
  185. }
  186. #ifdef _MSC_VER
  187. #if CATCH_UNHANDLED_EXCEPTIONS == 1
  188. void se_trans_func(unsigned int u, EXCEPTION_POINTERS* pExp)
  189. {
  190. dstream<<"In trans_func.\n";
  191. if(u == EXCEPTION_ACCESS_VIOLATION)
  192. {
  193. PEXCEPTION_RECORD r = pExp->ExceptionRecord;
  194. dstream<<"Access violation at "<<r->ExceptionAddress
  195. <<" write?="<<r->ExceptionInformation[0]
  196. <<" address="<<r->ExceptionInformation[1]
  197. <<std::endl;
  198. throw FatalSystemException
  199. ("Access violation");
  200. }
  201. if(u == EXCEPTION_STACK_OVERFLOW)
  202. {
  203. throw FatalSystemException
  204. ("Stack overflow");
  205. }
  206. if(u == EXCEPTION_ILLEGAL_INSTRUCTION)
  207. {
  208. throw FatalSystemException
  209. ("Illegal instruction");
  210. }
  211. }
  212. #endif
  213. #endif