3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 FILE *g_debugstreams[DEBUGSTREAM_COUNT] = {stderr, NULL};
31 void debugstreams_init(bool disable_stderr, const char *filename)
34 g_debugstreams[0] = NULL;
36 g_debugstreams[0] = stderr;
39 g_debugstreams[1] = fopen(filename, "a");
43 fprintf(g_debugstreams[1], "\n\n-------------\n");
44 fprintf(g_debugstreams[1], " Separator \n");
45 fprintf(g_debugstreams[1], "-------------\n\n");
48 DEBUGPRINT("Debug streams initialized, disable_stderr=%d\n",
52 void debugstreams_deinit()
54 if(g_debugstreams[1] != NULL)
55 fclose(g_debugstreams[1]);
58 Debugbuf debugbuf(false);
59 std::ostream dstream(&debugbuf);
60 Debugbuf debugbuf_no_stderr(true);
61 std::ostream dstream_no_stderr(&debugbuf_no_stderr);
68 void assert_fail(const char *assertion, const char *file,
69 unsigned int line, const char *function)
71 DEBUGPRINT("\nIn thread %lx:\n"
72 "%s:%d: %s: Assertion '%s' failed.\n",
73 (unsigned long)get_current_thread_id(),
74 file, line, function, assertion);
79 fclose(g_debugstreams[1]);
88 DebugStack::DebugStack(threadid_t id)
93 memset(stack, 0, DEBUG_STACK_SIZE*DEBUG_STACK_TEXT_SIZE);
96 void DebugStack::print(FILE *file, bool everything)
98 fprintf(file, "DEBUG STACK FOR THREAD %lx:\n",
99 (unsigned long)threadid);
101 for(int i=0; i<stack_max_i; i++)
103 if(i == stack_i && everything == false)
107 fprintf(file, "#%d %s\n", i, stack[i]);
109 fprintf(file, "(Leftover data: #%d %s)\n", i, stack[i]);
112 if(stack_i == DEBUG_STACK_SIZE)
113 fprintf(file, "Probably overflown.\n");
117 core::map<threadid_t, DebugStack*> g_debug_stacks;
118 JMutex g_debug_stacks_mutex;
120 void debug_stacks_init()
122 g_debug_stacks_mutex.Init();
125 void debug_stacks_print()
127 JMutexAutoLock lock(g_debug_stacks_mutex);
129 DEBUGPRINT("Debug stacks:\n");
131 for(core::map<threadid_t, DebugStack*>::Iterator
132 i = g_debug_stacks.getIterator();
133 i.atEnd() == false; i++)
135 DebugStack *stack = i.getNode()->getValue();
137 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
139 if(g_debugstreams[i] != NULL)
140 stack->print(g_debugstreams[i], true);
145 DebugStacker::DebugStacker(const char *text)
147 threadid_t threadid = get_current_thread_id();
149 JMutexAutoLock lock(g_debug_stacks_mutex);
151 core::map<threadid_t, DebugStack*>::Node *n;
152 n = g_debug_stacks.find(threadid);
155 m_stack = n->getValue();
159 /*DEBUGPRINT("Creating new debug stack for thread %x\n",
160 (unsigned int)threadid);*/
161 m_stack = new DebugStack(threadid);
162 g_debug_stacks.insert(threadid, m_stack);
165 if(m_stack->stack_i >= DEBUG_STACK_SIZE)
171 m_overflowed = false;
173 snprintf(m_stack->stack[m_stack->stack_i],
174 DEBUG_STACK_TEXT_SIZE, "%s", text);
176 if(m_stack->stack_i > m_stack->stack_max_i)
177 m_stack->stack_max_i = m_stack->stack_i;
181 DebugStacker::~DebugStacker()
183 JMutexAutoLock lock(g_debug_stacks_mutex);
185 if(m_overflowed == true)
190 if(m_stack->stack_i == 0)
192 threadid_t threadid = m_stack->threadid;
193 /*DEBUGPRINT("Deleting debug stack for thread %x\n",
194 (unsigned int)threadid);*/
196 g_debug_stacks.remove(threadid);
202 #if CATCH_UNHANDLED_EXCEPTIONS == 1
203 void se_trans_func(unsigned int u, EXCEPTION_POINTERS* pExp)
205 dstream<<"In trans_func.\n";
206 if(u == EXCEPTION_ACCESS_VIOLATION)
208 PEXCEPTION_RECORD r = pExp->ExceptionRecord;
209 dstream<<"Access violation at "<<r->ExceptionAddress
210 <<" write?="<<r->ExceptionInformation[0]
211 <<" address="<<r->ExceptionInformation[1]
213 throw FatalSystemException
214 ("Access violation");
216 if(u == EXCEPTION_STACK_OVERFLOW)
218 throw FatalSystemException
221 if(u == EXCEPTION_ILLEGAL_INSTRUCTION)
223 throw FatalSystemException
224 ("Illegal instruction");