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;
37 g_debugstreams[1] = fopen(filename, "a");
41 fprintf(g_debugstreams[1], "\n\n-------------\n");
42 fprintf(g_debugstreams[1], " Separator \n");
43 fprintf(g_debugstreams[1], "-------------\n\n");
47 void debugstreams_deinit()
49 if(g_debugstreams[1] != NULL)
50 fclose(g_debugstreams[1]);
53 Debugbuf debugbuf(false);
54 std::ostream dstream(&debugbuf);
55 Debugbuf debugbuf_no_stderr(true);
56 std::ostream dstream_no_stderr(&debugbuf_no_stderr);
63 void assert_fail(const char *assertion, const char *file,
64 unsigned int line, const char *function)
66 DEBUGPRINT("\nIn thread %x:\n"
67 "%s:%d: %s: Assertion '%s' failed.\n",
68 (unsigned int)get_current_thread_id(),
69 file, line, function, assertion);
74 fclose(g_debugstreams[1]);
83 DebugStack::DebugStack(threadid_t id)
90 void DebugStack::print(FILE *file, bool everything)
92 fprintf(file, "DEBUG STACK FOR THREAD %x:\n",
93 (unsigned int)threadid);
95 for(int i=0; i<stack_max_i; i++)
97 if(i == stack_i && everything == false)
101 fprintf(file, "#%d %s\n", i, stack[i]);
103 fprintf(file, "(Leftover data: #%d %s)\n", i, stack[i]);
106 if(stack_i == DEBUG_STACK_SIZE)
107 fprintf(file, "Probably overflown.\n");
111 core::map<threadid_t, DebugStack*> g_debug_stacks;
112 JMutex g_debug_stacks_mutex;
114 void debug_stacks_init()
116 g_debug_stacks_mutex.Init();
119 void debug_stacks_print()
121 JMutexAutoLock lock(g_debug_stacks_mutex);
123 DEBUGPRINT("Debug stacks:\n");
125 for(core::map<threadid_t, DebugStack*>::Iterator
126 i = g_debug_stacks.getIterator();
127 i.atEnd() == false; i++)
129 DebugStack *stack = i.getNode()->getValue();
131 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
133 if(g_debugstreams[i] != NULL)
134 stack->print(g_debugstreams[i], true);
139 DebugStacker::DebugStacker(const char *text)
141 threadid_t threadid = get_current_thread_id();
143 JMutexAutoLock lock(g_debug_stacks_mutex);
145 core::map<threadid_t, DebugStack*>::Node *n;
146 n = g_debug_stacks.find(threadid);
149 m_stack = n->getValue();
153 /*DEBUGPRINT("Creating new debug stack for thread %x\n",
154 (unsigned int)threadid);*/
155 m_stack = new DebugStack(threadid);
156 g_debug_stacks.insert(threadid, m_stack);
159 if(m_stack->stack_i >= DEBUG_STACK_SIZE)
165 m_overflowed = false;
167 snprintf(m_stack->stack[m_stack->stack_i],
168 DEBUG_STACK_TEXT_SIZE, "%s", text);
170 if(m_stack->stack_i > m_stack->stack_max_i)
171 m_stack->stack_max_i = m_stack->stack_i;
175 DebugStacker::~DebugStacker()
177 JMutexAutoLock lock(g_debug_stacks_mutex);
179 if(m_overflowed == true)
184 if(m_stack->stack_i == 0)
186 threadid_t threadid = m_stack->threadid;
187 /*DEBUGPRINT("Deleting debug stack for thread %x\n",
188 (unsigned int)threadid);*/
190 g_debug_stacks.remove(threadid);
196 void se_trans_func(unsigned int u, EXCEPTION_POINTERS* pExp)
198 dstream<<"In trans_func.\n";
199 if(u == EXCEPTION_ACCESS_VIOLATION)
201 PEXCEPTION_RECORD r = pExp->ExceptionRecord;
202 dstream<<"Access violation at "<<r->ExceptionAddress
203 <<" write?="<<r->ExceptionInformation[0]
204 <<" address="<<r->ExceptionInformation[1]
206 throw FatalSystemException
207 ("Access violation");
209 if(u == EXCEPTION_STACK_OVERFLOW)
211 throw FatalSystemException
214 if(u == EXCEPTION_ILLEGAL_INSTRUCTION)
216 throw FatalSystemException
217 ("Illegal instruction");