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.
30 FILE *g_debugstreams[DEBUGSTREAM_COUNT] = {stderr, NULL};
32 void debugstreams_init(bool disable_stderr, const char *filename)
35 g_debugstreams[0] = NULL;
38 g_debugstreams[1] = fopen(filename, "a");
42 fprintf(g_debugstreams[1], "\n\n-------------\n");
43 fprintf(g_debugstreams[1], " Separator \n");
44 fprintf(g_debugstreams[1], "-------------\n\n");
48 void debugstreams_deinit()
50 if(g_debugstreams[1] != NULL)
51 fclose(g_debugstreams[1]);
54 Debugbuf debugbuf(false);
55 std::ostream dstream(&debugbuf);
56 Debugbuf debugbuf_no_stderr(true);
57 std::ostream dstream_no_stderr(&debugbuf_no_stderr);
64 void assert_fail(const char *assertion, const char *file,
65 unsigned int line, const char *function)
67 DEBUGPRINT("\nIn thread %x:\n"
68 "%s:%d: %s: Assertion '%s' failed.\n",
69 (unsigned int)get_current_thread_id(),
70 file, line, function, assertion);
75 fclose(g_debugstreams[1]);
86 DebugStack::DebugStack(threadid_t id)
93 void DebugStack::print(FILE *file, bool everything)
95 fprintf(file, "DEBUG STACK FOR THREAD %x:\n",
96 (unsigned int)threadid);
98 for(int i=0; i<stack_max_i; i++)
100 if(i == stack_i && everything == false)
104 fprintf(file, "#%d %s\n", i, stack[i]);
106 fprintf(file, "(Leftover data: #%d %s)\n", i, stack[i]);
109 if(stack_i == DEBUG_STACK_SIZE)
110 fprintf(file, "Probably overflown.\n");
114 core::map<threadid_t, DebugStack*> g_debug_stacks;
115 JMutex g_debug_stacks_mutex;
117 void debug_stacks_init()
119 g_debug_stacks_mutex.Init();
122 void debug_stacks_print()
124 JMutexAutoLock lock(g_debug_stacks_mutex);
126 DEBUGPRINT("Debug stacks:\n");
128 for(core::map<threadid_t, DebugStack*>::Iterator
129 i = g_debug_stacks.getIterator();
130 i.atEnd() == false; i++)
132 DebugStack *stack = i.getNode()->getValue();
134 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
136 if(g_debugstreams[i] != NULL)
137 stack->print(g_debugstreams[i], true);
142 DebugStacker::DebugStacker(const char *text)
144 threadid_t threadid = get_current_thread_id();
146 JMutexAutoLock lock(g_debug_stacks_mutex);
148 core::map<threadid_t, DebugStack*>::Node *n;
149 n = g_debug_stacks.find(threadid);
152 m_stack = n->getValue();
156 /*DEBUGPRINT("Creating new debug stack for thread %x\n",
157 (unsigned int)threadid);*/
158 m_stack = new DebugStack(threadid);
159 g_debug_stacks.insert(threadid, m_stack);
162 if(m_stack->stack_i >= DEBUG_STACK_SIZE)
168 m_overflowed = false;
170 snprintf(m_stack->stack[m_stack->stack_i],
171 DEBUG_STACK_TEXT_SIZE, "%s", text);
173 if(m_stack->stack_i > m_stack->stack_max_i)
174 m_stack->stack_max_i = m_stack->stack_i;
178 DebugStacker::~DebugStacker()
180 JMutexAutoLock lock(g_debug_stacks_mutex);
182 if(m_overflowed == true)
187 if(m_stack->stack_i == 0)
189 threadid_t threadid = m_stack->threadid;
190 /*DEBUGPRINT("Deleting debug stack for thread %x\n",
191 (unsigned int)threadid);*/
193 g_debug_stacks.remove(threadid);
199 void se_trans_func(unsigned int u, EXCEPTION_POINTERS* pExp)
201 dstream<<"In trans_func.\n";
202 if(u == EXCEPTION_ACCESS_VIOLATION)
204 PEXCEPTION_RECORD r = pExp->ExceptionRecord;
205 dstream<<"Access violation at "<<r->ExceptionAddress
206 <<" write?="<<r->ExceptionInformation[0]
207 <<" address="<<r->ExceptionInformation[1]
209 throw FatalSystemException
210 ("Access violation");
212 if(u == EXCEPTION_STACK_OVERFLOW)
214 throw FatalSystemException
217 if(u == EXCEPTION_ILLEGAL_INSTRUCTION)
219 throw FatalSystemException
220 ("Illegal instruction");