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"
28 #include "jthread/jmutex.h"
29 #include "jthread/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()
211 void debug_stacks_print_to(std::ostream &os)
213 JMutexAutoLock lock(g_debug_stacks_mutex);
215 os<<"Debug stacks:"<<std::endl;
217 for(std::map<threadid_t, DebugStack*>::iterator
218 i = g_debug_stacks.begin();
219 i != g_debug_stacks.end(); ++i)
221 i->second->print(os, false);
225 void debug_stacks_print()
227 JMutexAutoLock lock(g_debug_stacks_mutex);
229 DEBUGPRINT("Debug stacks:\n");
231 for(std::map<threadid_t, DebugStack*>::iterator
232 i = g_debug_stacks.begin();
233 i != g_debug_stacks.end(); ++i)
235 DebugStack *stack = i->second;
237 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
239 if(g_debugstreams[i] != NULL)
240 stack->print(g_debugstreams[i], true);
245 DebugStacker::DebugStacker(const char *text)
247 threadid_t threadid = get_current_thread_id();
249 JMutexAutoLock lock(g_debug_stacks_mutex);
251 std::map<threadid_t, DebugStack*>::iterator n;
252 n = g_debug_stacks.find(threadid);
253 if(n != g_debug_stacks.end())
259 /*DEBUGPRINT("Creating new debug stack for thread %x\n",
260 (unsigned int)threadid);*/
261 m_stack = new DebugStack(threadid);
262 g_debug_stacks[threadid] = m_stack;
265 if(m_stack->stack_i >= DEBUG_STACK_SIZE)
271 m_overflowed = false;
273 snprintf(m_stack->stack[m_stack->stack_i],
274 DEBUG_STACK_TEXT_SIZE, "%s", text);
276 if(m_stack->stack_i > m_stack->stack_max_i)
277 m_stack->stack_max_i = m_stack->stack_i;
281 DebugStacker::~DebugStacker()
283 JMutexAutoLock lock(g_debug_stacks_mutex);
285 if(m_overflowed == true)
290 if(m_stack->stack_i == 0)
292 threadid_t threadid = m_stack->threadid;
293 /*DEBUGPRINT("Deleting debug stack for thread %x\n",
294 (unsigned int)threadid);*/
296 g_debug_stacks.erase(threadid);
302 #if CATCH_UNHANDLED_EXCEPTIONS == 1
303 void se_trans_func(unsigned int u, EXCEPTION_POINTERS* pExp)
305 dstream<<"In trans_func.\n";
306 if(u == EXCEPTION_ACCESS_VIOLATION)
308 PEXCEPTION_RECORD r = pExp->ExceptionRecord;
309 dstream<<"Access violation at "<<r->ExceptionAddress
310 <<" write?="<<r->ExceptionInformation[0]
311 <<" address="<<r->ExceptionInformation[1]
313 throw FatalSystemException
314 ("Access violation");
316 if(u == EXCEPTION_STACK_OVERFLOW)
318 throw FatalSystemException
321 if(u == EXCEPTION_ILLEGAL_INSTRUCTION)
323 throw FatalSystemException
324 ("Illegal instruction");