]> git.lizzy.rs Git - dragonfireclient.git/blob - src/debug.cpp
end-of-day.
[dragonfireclient.git] / src / debug.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20
21 #include "debug.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 /*
26         Debug output
27 */
28
29 FILE *g_debugstreams[DEBUGSTREAM_COUNT] = {stderr, NULL};
30
31 void debugstreams_init(bool disable_stderr, const char *filename)
32 {
33         if(disable_stderr)
34                 g_debugstreams[0] = NULL;
35         else
36                 g_debugstreams[0] = stderr;
37
38         if(filename)
39                 g_debugstreams[1] = fopen(filename, "a");
40                 
41         if(g_debugstreams[1])
42         {
43                 fprintf(g_debugstreams[1], "\n\n-------------\n");
44                 fprintf(g_debugstreams[1],     "  Separator  \n");
45                 fprintf(g_debugstreams[1],     "-------------\n\n");
46         }
47         
48         DEBUGPRINT("Debug streams initialized, disable_stderr=%d\n",
49                         disable_stderr);
50 }
51
52 void debugstreams_deinit()
53 {
54         if(g_debugstreams[1] != NULL)
55                 fclose(g_debugstreams[1]);
56 }
57
58 Debugbuf debugbuf(false);
59 std::ostream dstream(&debugbuf);
60 Debugbuf debugbuf_no_stderr(true);
61 std::ostream dstream_no_stderr(&debugbuf_no_stderr);
62 Nullstream dummyout;
63
64 /*
65         Assert
66 */
67
68 void assert_fail(const char *assertion, const char *file,
69                 unsigned int line, const char *function)
70 {
71         DEBUGPRINT("\nIn thread %x:\n"
72                         "%s:%d: %s: Assertion '%s' failed.\n",
73                         (unsigned int)get_current_thread_id(),
74                         file, line, function, assertion);
75         
76         debug_stacks_print();
77
78         if(g_debugstreams[1])
79                 fclose(g_debugstreams[1]);
80
81         abort();
82 }
83
84 /*
85         DebugStack
86 */
87
88 DebugStack::DebugStack(threadid_t id)
89 {
90         threadid = id;
91         stack_i = 0;
92         stack_max_i = 0;
93 }
94
95 void DebugStack::print(FILE *file, bool everything)
96 {
97         fprintf(file, "DEBUG STACK FOR THREAD %x:\n",
98                         (unsigned int)threadid);
99
100         for(int i=0; i<stack_max_i; i++)
101         {
102                 if(i == stack_i && everything == false)
103                         continue;
104
105                 if(i < stack_i)
106                         fprintf(file, "#%d  %s\n", i, stack[i]);
107                 else
108                         fprintf(file, "(Leftover data: #%d  %s)\n", i, stack[i]);
109         }
110
111         if(stack_i == DEBUG_STACK_SIZE)
112                 fprintf(file, "Probably overflown.\n");
113 }
114
115
116 core::map<threadid_t, DebugStack*> g_debug_stacks;
117 JMutex g_debug_stacks_mutex;
118
119 void debug_stacks_init()
120 {
121         g_debug_stacks_mutex.Init();
122 }
123
124 void debug_stacks_print()
125 {
126         JMutexAutoLock lock(g_debug_stacks_mutex);
127
128         DEBUGPRINT("Debug stacks:\n");
129
130         for(core::map<threadid_t, DebugStack*>::Iterator
131                         i = g_debug_stacks.getIterator();
132                         i.atEnd() == false; i++)
133         {
134                 DebugStack *stack = i.getNode()->getValue();
135
136                 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
137                 {
138                         if(g_debugstreams[i] != NULL)
139                                 stack->print(g_debugstreams[i], true);
140                 }
141         }
142 }
143
144 DebugStacker::DebugStacker(const char *text)
145 {
146         threadid_t threadid = get_current_thread_id();
147
148         JMutexAutoLock lock(g_debug_stacks_mutex);
149
150         core::map<threadid_t, DebugStack*>::Node *n;
151         n = g_debug_stacks.find(threadid);
152         if(n != NULL)
153         {
154                 m_stack = n->getValue();
155         }
156         else
157         {
158                 /*DEBUGPRINT("Creating new debug stack for thread %x\n",
159                                 (unsigned int)threadid);*/
160                 m_stack = new DebugStack(threadid);
161                 g_debug_stacks.insert(threadid, m_stack);
162         }
163
164         if(m_stack->stack_i >= DEBUG_STACK_SIZE)
165         {
166                 m_overflowed = true;
167         }
168         else
169         {
170                 m_overflowed = false;
171
172                 snprintf(m_stack->stack[m_stack->stack_i],
173                                 DEBUG_STACK_TEXT_SIZE, "%s", text);
174                 m_stack->stack_i++;
175                 if(m_stack->stack_i > m_stack->stack_max_i)
176                         m_stack->stack_max_i = m_stack->stack_i;
177         }
178 }
179
180 DebugStacker::~DebugStacker()
181 {
182         JMutexAutoLock lock(g_debug_stacks_mutex);
183         
184         if(m_overflowed == true)
185                 return;
186         
187         m_stack->stack_i--;
188
189         if(m_stack->stack_i == 0)
190         {
191                 threadid_t threadid = m_stack->threadid;
192                 /*DEBUGPRINT("Deleting debug stack for thread %x\n",
193                                 (unsigned int)threadid);*/
194                 delete m_stack;
195                 g_debug_stacks.remove(threadid);
196         }
197 }
198
199
200 #ifdef _WIN32
201 void se_trans_func(unsigned int u, EXCEPTION_POINTERS* pExp)
202 {
203         dstream<<"In trans_func.\n";
204         if(u == EXCEPTION_ACCESS_VIOLATION)
205         {
206                 PEXCEPTION_RECORD r = pExp->ExceptionRecord;
207                 dstream<<"Access violation at "<<r->ExceptionAddress
208                                 <<" write?="<<r->ExceptionInformation[0]
209                                 <<" address="<<r->ExceptionInformation[1]
210                                 <<std::endl;
211                 throw FatalSystemException
212                 ("Access violation");
213         }
214         if(u == EXCEPTION_STACK_OVERFLOW)
215         {
216                 throw FatalSystemException
217                 ("Stack overflow");
218         }
219         if(u == EXCEPTION_ILLEGAL_INSTRUCTION)
220         {
221                 throw FatalSystemException
222                 ("Illegal instruction");
223         }
224 }
225 #endif
226
227
228