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");
116 void DebugStack::print(std::ostream &os, bool everything)
118 os<<"DEBUG STACK FOR THREAD "<<(unsigned long)threadid<<": "<<std::endl;
120 for(int i=0; i<stack_max_i; i++)
122 if(i == stack_i && everything == false)
126 os<<"#"<<i<<" "<<stack[i]<<std::endl;
128 os<<"(Leftover data: #"<<i<<" "<<stack[i]<<")"<<std::endl;
131 if(stack_i == DEBUG_STACK_SIZE)
132 os<<"Probably overflown."<<std::endl;
135 core::map<threadid_t, DebugStack*> g_debug_stacks;
136 JMutex g_debug_stacks_mutex;
138 void debug_stacks_init()
140 g_debug_stacks_mutex.Init();
143 void debug_stacks_print_to(std::ostream &os)
145 JMutexAutoLock lock(g_debug_stacks_mutex);
147 os<<"Debug stacks:"<<std::endl;
149 for(core::map<threadid_t, DebugStack*>::Iterator
150 i = g_debug_stacks.getIterator();
151 i.atEnd() == false; i++)
153 DebugStack *stack = i.getNode()->getValue();
154 stack->print(os, false);
158 void debug_stacks_print()
160 JMutexAutoLock lock(g_debug_stacks_mutex);
162 DEBUGPRINT("Debug stacks:\n");
164 for(core::map<threadid_t, DebugStack*>::Iterator
165 i = g_debug_stacks.getIterator();
166 i.atEnd() == false; i++)
168 DebugStack *stack = i.getNode()->getValue();
170 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
172 if(g_debugstreams[i] != NULL)
173 stack->print(g_debugstreams[i], true);
178 DebugStacker::DebugStacker(const char *text)
180 threadid_t threadid = get_current_thread_id();
182 JMutexAutoLock lock(g_debug_stacks_mutex);
184 core::map<threadid_t, DebugStack*>::Node *n;
185 n = g_debug_stacks.find(threadid);
188 m_stack = n->getValue();
192 /*DEBUGPRINT("Creating new debug stack for thread %x\n",
193 (unsigned int)threadid);*/
194 m_stack = new DebugStack(threadid);
195 g_debug_stacks.insert(threadid, m_stack);
198 if(m_stack->stack_i >= DEBUG_STACK_SIZE)
204 m_overflowed = false;
206 snprintf(m_stack->stack[m_stack->stack_i],
207 DEBUG_STACK_TEXT_SIZE, "%s", text);
209 if(m_stack->stack_i > m_stack->stack_max_i)
210 m_stack->stack_max_i = m_stack->stack_i;
214 DebugStacker::~DebugStacker()
216 JMutexAutoLock lock(g_debug_stacks_mutex);
218 if(m_overflowed == true)
223 if(m_stack->stack_i == 0)
225 threadid_t threadid = m_stack->threadid;
226 /*DEBUGPRINT("Deleting debug stack for thread %x\n",
227 (unsigned int)threadid);*/
229 g_debug_stacks.remove(threadid);
235 #if CATCH_UNHANDLED_EXCEPTIONS == 1
236 void se_trans_func(unsigned int u, EXCEPTION_POINTERS* pExp)
238 dstream<<"In trans_func.\n";
239 if(u == EXCEPTION_ACCESS_VIOLATION)
241 PEXCEPTION_RECORD r = pExp->ExceptionRecord;
242 dstream<<"Access violation at "<<r->ExceptionAddress
243 <<" write?="<<r->ExceptionInformation[0]
244 <<" address="<<r->ExceptionInformation[1]
246 throw FatalSystemException
247 ("Access violation");
249 if(u == EXCEPTION_STACK_OVERFLOW)
251 throw FatalSystemException
254 if(u == EXCEPTION_ILLEGAL_INSTRUCTION)
256 throw FatalSystemException
257 ("Illegal instruction");