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 %x:\n"
72 "%s:%d: %s: Assertion '%s' failed.\n",
73 (unsigned int)get_current_thread_id(),
74 file, line, function, assertion);
79 fclose(g_debugstreams[1]);
88 DebugStack::DebugStack(threadid_t id)
95 void DebugStack::print(FILE *file, bool everything)
97 fprintf(file, "DEBUG STACK FOR THREAD %x:\n",
98 (unsigned int)threadid);
100 for(int i=0; i<stack_max_i; i++)
102 if(i == stack_i && everything == false)
106 fprintf(file, "#%d %s\n", i, stack[i]);
108 fprintf(file, "(Leftover data: #%d %s)\n", i, stack[i]);
111 if(stack_i == DEBUG_STACK_SIZE)
112 fprintf(file, "Probably overflown.\n");
116 core::map<threadid_t, DebugStack*> g_debug_stacks;
117 JMutex g_debug_stacks_mutex;
119 void debug_stacks_init()
121 g_debug_stacks_mutex.Init();
124 void debug_stacks_print()
126 JMutexAutoLock lock(g_debug_stacks_mutex);
128 DEBUGPRINT("Debug stacks:\n");
130 for(core::map<threadid_t, DebugStack*>::Iterator
131 i = g_debug_stacks.getIterator();
132 i.atEnd() == false; i++)
134 DebugStack *stack = i.getNode()->getValue();
136 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
138 if(g_debugstreams[i] != NULL)
139 stack->print(g_debugstreams[i], true);
144 DebugStacker::DebugStacker(const char *text)
146 threadid_t threadid = get_current_thread_id();
148 JMutexAutoLock lock(g_debug_stacks_mutex);
150 core::map<threadid_t, DebugStack*>::Node *n;
151 n = g_debug_stacks.find(threadid);
154 m_stack = n->getValue();
158 /*DEBUGPRINT("Creating new debug stack for thread %x\n",
159 (unsigned int)threadid);*/
160 m_stack = new DebugStack(threadid);
161 g_debug_stacks.insert(threadid, m_stack);
164 if(m_stack->stack_i >= DEBUG_STACK_SIZE)
170 m_overflowed = false;
172 snprintf(m_stack->stack[m_stack->stack_i],
173 DEBUG_STACK_TEXT_SIZE, "%s", text);
175 if(m_stack->stack_i > m_stack->stack_max_i)
176 m_stack->stack_max_i = m_stack->stack_i;
180 DebugStacker::~DebugStacker()
182 JMutexAutoLock lock(g_debug_stacks_mutex);
184 if(m_overflowed == true)
189 if(m_stack->stack_i == 0)
191 threadid_t threadid = m_stack->threadid;
192 /*DEBUGPRINT("Deleting debug stack for thread %x\n",
193 (unsigned int)threadid);*/
195 g_debug_stacks.remove(threadid);
201 void se_trans_func(unsigned int u, EXCEPTION_POINTERS* pExp)
203 dstream<<"In trans_func.\n";
204 if(u == EXCEPTION_ACCESS_VIOLATION)
206 PEXCEPTION_RECORD r = pExp->ExceptionRecord;
207 dstream<<"Access violation at "<<r->ExceptionAddress
208 <<" write?="<<r->ExceptionInformation[0]
209 <<" address="<<r->ExceptionInformation[1]
211 throw FatalSystemException
212 ("Access violation");
214 if(u == EXCEPTION_STACK_OVERFLOW)
216 throw FatalSystemException
219 if(u == EXCEPTION_ILLEGAL_INSTRUCTION)
221 throw FatalSystemException
222 ("Illegal instruction");