3 Copyright (C) 2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser 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.
22 #include "exceptions.h"
29 #include <jmutexautolock.h>
35 #define DEBUGSTREAM_COUNT 2
37 FILE *g_debugstreams[DEBUGSTREAM_COUNT] = {stderr, NULL};
39 #define DEBUGPRINT(...)\
41 for(int i=0; i<DEBUGSTREAM_COUNT; i++)\
43 if(g_debugstreams[i] != NULL){\
44 fprintf(g_debugstreams[i], __VA_ARGS__);\
45 fflush(g_debugstreams[i]);\
50 void debugstreams_init(bool disable_stderr, const char *filename)
53 g_debugstreams[0] = NULL;
55 g_debugstreams[0] = stderr;
58 g_debugstreams[1] = fopen(filename, "a");
62 fprintf(g_debugstreams[1], "\n\n-------------\n");
63 fprintf(g_debugstreams[1], " Separator \n");
64 fprintf(g_debugstreams[1], "-------------\n\n");
68 void debugstreams_deinit()
70 if(g_debugstreams[1] != NULL)
71 fclose(g_debugstreams[1]);
74 class Debugbuf : public std::streambuf
77 Debugbuf(bool disable_stderr)
79 m_disable_stderr = disable_stderr;
84 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
86 if(g_debugstreams[i] == stderr && m_disable_stderr)
88 if(g_debugstreams[i] != NULL)
89 (void)fwrite(&c, 1, 1, g_debugstreams[i]);
91 fflush(g_debugstreams[i]);
96 std::streamsize xsputn(const char *s, std::streamsize n)
98 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
100 if(g_debugstreams[i] == stderr && m_disable_stderr)
102 if(g_debugstreams[i] != NULL)
103 (void)fwrite(s, 1, n, g_debugstreams[i]);
104 //TODO: Is this slow?
105 fflush(g_debugstreams[i]);
112 bool m_disable_stderr;
115 Debugbuf debugbuf(false);
116 std::ostream dstream(&debugbuf);
117 Debugbuf debugbuf_no_stderr(true);
118 std::ostream dstream_no_stderr(&debugbuf_no_stderr);
125 void assert_fail(const char *assertion, const char *file,
126 unsigned int line, const char *function)
128 DEBUGPRINT("\nIn thread %lx:\n"
129 "%s:%d: %s: Assertion '%s' failed.\n",
130 (unsigned long)get_current_thread_id(),
131 file, line, function, assertion);
133 debug_stacks_print();
135 if(g_debugstreams[1])
136 fclose(g_debugstreams[1]);
147 DebugStack(threadid_t id);
148 void print(FILE *file, bool everything);
149 void print(std::ostream &os, bool everything);
152 char stack[DEBUG_STACK_SIZE][DEBUG_STACK_TEXT_SIZE];
153 int stack_i; // Points to the lowest empty position
154 int stack_max_i; // Highest i that was seen
157 DebugStack::DebugStack(threadid_t id)
162 memset(stack, 0, DEBUG_STACK_SIZE*DEBUG_STACK_TEXT_SIZE);
165 void DebugStack::print(FILE *file, bool everything)
167 fprintf(file, "DEBUG STACK FOR THREAD %lx:\n",
168 (unsigned long)threadid);
170 for(int i=0; i<stack_max_i; i++)
172 if(i == stack_i && everything == false)
176 fprintf(file, "#%d %s\n", i, stack[i]);
178 fprintf(file, "(Leftover data: #%d %s)\n", i, stack[i]);
181 if(stack_i == DEBUG_STACK_SIZE)
182 fprintf(file, "Probably overflown.\n");
185 void DebugStack::print(std::ostream &os, bool everything)
187 os<<"DEBUG STACK FOR THREAD "<<(unsigned long)threadid<<": "<<std::endl;
189 for(int i=0; i<stack_max_i; i++)
191 if(i == stack_i && everything == false)
195 os<<"#"<<i<<" "<<stack[i]<<std::endl;
197 os<<"(Leftover data: #"<<i<<" "<<stack[i]<<")"<<std::endl;
200 if(stack_i == DEBUG_STACK_SIZE)
201 os<<"Probably overflown."<<std::endl;
204 std::map<threadid_t, DebugStack*> g_debug_stacks;
205 JMutex g_debug_stacks_mutex;
207 void debug_stacks_init()
209 g_debug_stacks_mutex.Init();
212 void debug_stacks_print_to(std::ostream &os)
214 JMutexAutoLock lock(g_debug_stacks_mutex);
216 os<<"Debug stacks:"<<std::endl;
218 for(std::map<threadid_t, DebugStack*>::iterator
219 i = g_debug_stacks.begin();
220 i != g_debug_stacks.end(); ++i)
222 i->second->print(os, false);
226 void debug_stacks_print()
228 JMutexAutoLock lock(g_debug_stacks_mutex);
230 DEBUGPRINT("Debug stacks:\n");
232 for(std::map<threadid_t, DebugStack*>::iterator
233 i = g_debug_stacks.begin();
234 i != g_debug_stacks.end(); ++i)
236 DebugStack *stack = i->second;
238 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
240 if(g_debugstreams[i] != NULL)
241 stack->print(g_debugstreams[i], true);
246 DebugStacker::DebugStacker(const char *text)
248 threadid_t threadid = get_current_thread_id();
250 JMutexAutoLock lock(g_debug_stacks_mutex);
252 std::map<threadid_t, DebugStack*>::iterator n;
253 n = g_debug_stacks.find(threadid);
254 if(n != g_debug_stacks.end())
260 /*DEBUGPRINT("Creating new debug stack for thread %x\n",
261 (unsigned int)threadid);*/
262 m_stack = new DebugStack(threadid);
263 g_debug_stacks[threadid] = m_stack;
266 if(m_stack->stack_i >= DEBUG_STACK_SIZE)
272 m_overflowed = false;
274 snprintf(m_stack->stack[m_stack->stack_i],
275 DEBUG_STACK_TEXT_SIZE, "%s", text);
277 if(m_stack->stack_i > m_stack->stack_max_i)
278 m_stack->stack_max_i = m_stack->stack_i;
282 DebugStacker::~DebugStacker()
284 JMutexAutoLock lock(g_debug_stacks_mutex);
286 if(m_overflowed == true)
291 if(m_stack->stack_i == 0)
293 threadid_t threadid = m_stack->threadid;
294 /*DEBUGPRINT("Deleting debug stack for thread %x\n",
295 (unsigned int)threadid);*/
297 g_debug_stacks.erase(threadid);
303 #if CATCH_UNHANDLED_EXCEPTIONS == 1
304 void se_trans_func(unsigned int u, EXCEPTION_POINTERS* pExp)
306 dstream<<"In trans_func.\n";
307 if(u == EXCEPTION_ACCESS_VIOLATION)
309 PEXCEPTION_RECORD r = pExp->ExceptionRecord;
310 dstream<<"Access violation at "<<r->ExceptionAddress
311 <<" write?="<<r->ExceptionInformation[0]
312 <<" address="<<r->ExceptionInformation[1]
314 throw FatalSystemException
315 ("Access violation");
317 if(u == EXCEPTION_STACK_OVERFLOW)
319 throw FatalSystemException
322 if(u == EXCEPTION_ILLEGAL_INSTRUCTION)
324 throw FatalSystemException
325 ("Illegal instruction");