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.
30 FILE *g_debugstreams[DEBUGSTREAM_COUNT] = {stderr, NULL};
32 void debugstreams_init(bool disable_stderr, const char *filename)
35 g_debugstreams[0] = NULL;
37 g_debugstreams[0] = stderr;
40 g_debugstreams[1] = fopen(filename, "a");
44 fprintf(g_debugstreams[1], "\n\n-------------\n");
45 fprintf(g_debugstreams[1], " Separator \n");
46 fprintf(g_debugstreams[1], "-------------\n\n");
50 void debugstreams_deinit()
52 if(g_debugstreams[1] != NULL)
53 fclose(g_debugstreams[1]);
56 Debugbuf debugbuf(false);
57 std::ostream dstream(&debugbuf);
58 Debugbuf debugbuf_no_stderr(true);
59 std::ostream dstream_no_stderr(&debugbuf_no_stderr);
66 void assert_fail(const char *assertion, const char *file,
67 unsigned int line, const char *function)
69 DEBUGPRINT("\nIn thread %lx:\n"
70 "%s:%d: %s: Assertion '%s' failed.\n",
71 (unsigned long)get_current_thread_id(),
72 file, line, function, assertion);
77 fclose(g_debugstreams[1]);
86 DebugStack::DebugStack(threadid_t id)
91 memset(stack, 0, DEBUG_STACK_SIZE*DEBUG_STACK_TEXT_SIZE);
94 void DebugStack::print(FILE *file, bool everything)
96 fprintf(file, "DEBUG STACK FOR THREAD %lx:\n",
97 (unsigned long)threadid);
99 for(int i=0; i<stack_max_i; i++)
101 if(i == stack_i && everything == false)
105 fprintf(file, "#%d %s\n", i, stack[i]);
107 fprintf(file, "(Leftover data: #%d %s)\n", i, stack[i]);
110 if(stack_i == DEBUG_STACK_SIZE)
111 fprintf(file, "Probably overflown.\n");
114 void DebugStack::print(std::ostream &os, bool everything)
116 os<<"DEBUG STACK FOR THREAD "<<(unsigned long)threadid<<": "<<std::endl;
118 for(int i=0; i<stack_max_i; i++)
120 if(i == stack_i && everything == false)
124 os<<"#"<<i<<" "<<stack[i]<<std::endl;
126 os<<"(Leftover data: #"<<i<<" "<<stack[i]<<")"<<std::endl;
129 if(stack_i == DEBUG_STACK_SIZE)
130 os<<"Probably overflown."<<std::endl;
133 std::map<threadid_t, DebugStack*> g_debug_stacks;
134 JMutex g_debug_stacks_mutex;
136 void debug_stacks_init()
138 g_debug_stacks_mutex.Init();
141 void debug_stacks_print_to(std::ostream &os)
143 JMutexAutoLock lock(g_debug_stacks_mutex);
145 os<<"Debug stacks:"<<std::endl;
147 for(std::map<threadid_t, DebugStack*>::iterator
148 i = g_debug_stacks.begin();
149 i != g_debug_stacks.end(); ++i)
151 i->second->print(os, false);
155 void debug_stacks_print()
157 JMutexAutoLock lock(g_debug_stacks_mutex);
159 DEBUGPRINT("Debug stacks:\n");
161 for(std::map<threadid_t, DebugStack*>::iterator
162 i = g_debug_stacks.begin();
163 i != g_debug_stacks.end(); ++i)
165 DebugStack *stack = i->second;
167 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
169 if(g_debugstreams[i] != NULL)
170 stack->print(g_debugstreams[i], true);
175 DebugStacker::DebugStacker(const char *text)
177 threadid_t threadid = get_current_thread_id();
179 JMutexAutoLock lock(g_debug_stacks_mutex);
181 std::map<threadid_t, DebugStack*>::iterator n;
182 n = g_debug_stacks.find(threadid);
183 if(n != g_debug_stacks.end())
189 /*DEBUGPRINT("Creating new debug stack for thread %x\n",
190 (unsigned int)threadid);*/
191 m_stack = new DebugStack(threadid);
192 g_debug_stacks[threadid] = m_stack;
195 if(m_stack->stack_i >= DEBUG_STACK_SIZE)
201 m_overflowed = false;
203 snprintf(m_stack->stack[m_stack->stack_i],
204 DEBUG_STACK_TEXT_SIZE, "%s", text);
206 if(m_stack->stack_i > m_stack->stack_max_i)
207 m_stack->stack_max_i = m_stack->stack_i;
211 DebugStacker::~DebugStacker()
213 JMutexAutoLock lock(g_debug_stacks_mutex);
215 if(m_overflowed == true)
220 if(m_stack->stack_i == 0)
222 threadid_t threadid = m_stack->threadid;
223 /*DEBUGPRINT("Deleting debug stack for thread %x\n",
224 (unsigned int)threadid);*/
226 g_debug_stacks.erase(threadid);
232 #if CATCH_UNHANDLED_EXCEPTIONS == 1
233 void se_trans_func(unsigned int u, EXCEPTION_POINTERS* pExp)
235 dstream<<"In trans_func.\n";
236 if(u == EXCEPTION_ACCESS_VIOLATION)
238 PEXCEPTION_RECORD r = pExp->ExceptionRecord;
239 dstream<<"Access violation at "<<r->ExceptionAddress
240 <<" write?="<<r->ExceptionInformation[0]
241 <<" address="<<r->ExceptionInformation[1]
243 throw FatalSystemException
244 ("Access violation");
246 if(u == EXCEPTION_STACK_OVERFLOW)
248 throw FatalSystemException
251 if(u == EXCEPTION_ILLEGAL_INSTRUCTION)
253 throw FatalSystemException
254 ("Illegal instruction");