]> git.lizzy.rs Git - dragonfireclient.git/blob - src/debug.cpp
Omnicleanup: header cleanup, add ModApiUtil shared between game and mainmenu
[dragonfireclient.git] / src / debug.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 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 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.
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 Lesser General Public License for more details.
14
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.
18 */
19
20
21 #include "debug.h"
22 #include "exceptions.h"
23 #include "threads.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <cstring>
27 #include <map>
28 #include <jmutex.h>
29 #include <jmutexautolock.h>
30
31 /*
32         Debug output
33 */
34
35 #define DEBUGSTREAM_COUNT 2
36
37 FILE *g_debugstreams[DEBUGSTREAM_COUNT] = {stderr, NULL};
38
39 #define DEBUGPRINT(...)\
40 {\
41         for(int i=0; i<DEBUGSTREAM_COUNT; i++)\
42         {\
43                 if(g_debugstreams[i] != NULL){\
44                         fprintf(g_debugstreams[i], __VA_ARGS__);\
45                         fflush(g_debugstreams[i]);\
46                 }\
47         }\
48 }
49
50 void debugstreams_init(bool disable_stderr, const char *filename)
51 {
52         if(disable_stderr)
53                 g_debugstreams[0] = NULL;
54         else
55                 g_debugstreams[0] = stderr;
56
57         if(filename)
58                 g_debugstreams[1] = fopen(filename, "a");
59                 
60         if(g_debugstreams[1])
61         {
62                 fprintf(g_debugstreams[1], "\n\n-------------\n");
63                 fprintf(g_debugstreams[1],     "  Separator  \n");
64                 fprintf(g_debugstreams[1],     "-------------\n\n");
65         }
66 }
67
68 void debugstreams_deinit()
69 {
70         if(g_debugstreams[1] != NULL)
71                 fclose(g_debugstreams[1]);
72 }
73
74 class Debugbuf : public std::streambuf
75 {
76 public:
77         Debugbuf(bool disable_stderr)
78         {
79                 m_disable_stderr = disable_stderr;
80         }
81
82         int overflow(int c)
83         {
84                 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
85                 {
86                         if(g_debugstreams[i] == stderr && m_disable_stderr)
87                                 continue;
88                         if(g_debugstreams[i] != NULL)
89                                 (void)fwrite(&c, 1, 1, g_debugstreams[i]);
90                         //TODO: Is this slow?
91                         fflush(g_debugstreams[i]);
92                 }
93                 
94                 return c;
95         }
96         std::streamsize xsputn(const char *s, std::streamsize n)
97         {
98                 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
99                 {
100                         if(g_debugstreams[i] == stderr && m_disable_stderr)
101                                 continue;
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]);
106                 }
107
108                 return n;
109         }
110         
111 private:
112         bool m_disable_stderr;
113 };
114
115 Debugbuf debugbuf(false);
116 std::ostream dstream(&debugbuf);
117 Debugbuf debugbuf_no_stderr(true);
118 std::ostream dstream_no_stderr(&debugbuf_no_stderr);
119 Nullstream dummyout;
120
121 /*
122         Assert
123 */
124
125 void assert_fail(const char *assertion, const char *file,
126                 unsigned int line, const char *function)
127 {
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);
132         
133         debug_stacks_print();
134
135         if(g_debugstreams[1])
136                 fclose(g_debugstreams[1]);
137
138         abort();
139 }
140
141 /*
142         DebugStack
143 */
144
145 struct DebugStack
146 {
147         DebugStack(threadid_t id);
148         void print(FILE *file, bool everything);
149         void print(std::ostream &os, bool everything);
150         
151         threadid_t threadid;
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
155 };
156
157 DebugStack::DebugStack(threadid_t id)
158 {
159         threadid = id;
160         stack_i = 0;
161         stack_max_i = 0;
162         memset(stack, 0, DEBUG_STACK_SIZE*DEBUG_STACK_TEXT_SIZE);
163 }
164
165 void DebugStack::print(FILE *file, bool everything)
166 {
167         fprintf(file, "DEBUG STACK FOR THREAD %lx:\n",
168                         (unsigned long)threadid);
169
170         for(int i=0; i<stack_max_i; i++)
171         {
172                 if(i == stack_i && everything == false)
173                         break;
174
175                 if(i < stack_i)
176                         fprintf(file, "#%d  %s\n", i, stack[i]);
177                 else
178                         fprintf(file, "(Leftover data: #%d  %s)\n", i, stack[i]);
179         }
180
181         if(stack_i == DEBUG_STACK_SIZE)
182                 fprintf(file, "Probably overflown.\n");
183 }
184
185 void DebugStack::print(std::ostream &os, bool everything)
186 {
187         os<<"DEBUG STACK FOR THREAD "<<(unsigned long)threadid<<": "<<std::endl;
188
189         for(int i=0; i<stack_max_i; i++)
190         {
191                 if(i == stack_i && everything == false)
192                         break;
193
194                 if(i < stack_i)
195                         os<<"#"<<i<<"  "<<stack[i]<<std::endl;
196                 else
197                         os<<"(Leftover data: #"<<i<<"  "<<stack[i]<<")"<<std::endl;
198         }
199
200         if(stack_i == DEBUG_STACK_SIZE)
201                 os<<"Probably overflown."<<std::endl;
202 }
203
204 std::map<threadid_t, DebugStack*> g_debug_stacks;
205 JMutex g_debug_stacks_mutex;
206
207 void debug_stacks_init()
208 {
209         g_debug_stacks_mutex.Init();
210 }
211
212 void debug_stacks_print_to(std::ostream &os)
213 {
214         JMutexAutoLock lock(g_debug_stacks_mutex);
215
216         os<<"Debug stacks:"<<std::endl;
217
218         for(std::map<threadid_t, DebugStack*>::iterator
219                         i = g_debug_stacks.begin();
220                         i != g_debug_stacks.end(); ++i)
221         {
222                 i->second->print(os, false);
223         }
224 }
225
226 void debug_stacks_print()
227 {
228         JMutexAutoLock lock(g_debug_stacks_mutex);
229
230         DEBUGPRINT("Debug stacks:\n");
231
232         for(std::map<threadid_t, DebugStack*>::iterator
233                         i = g_debug_stacks.begin();
234                         i != g_debug_stacks.end(); ++i)
235         {
236                 DebugStack *stack = i->second;
237
238                 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
239                 {
240                         if(g_debugstreams[i] != NULL)
241                                 stack->print(g_debugstreams[i], true);
242                 }
243         }
244 }
245
246 DebugStacker::DebugStacker(const char *text)
247 {
248         threadid_t threadid = get_current_thread_id();
249
250         JMutexAutoLock lock(g_debug_stacks_mutex);
251
252         std::map<threadid_t, DebugStack*>::iterator n;
253         n = g_debug_stacks.find(threadid);
254         if(n != g_debug_stacks.end())
255         {
256                 m_stack = n->second;
257         }
258         else
259         {
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;
264         }
265
266         if(m_stack->stack_i >= DEBUG_STACK_SIZE)
267         {
268                 m_overflowed = true;
269         }
270         else
271         {
272                 m_overflowed = false;
273
274                 snprintf(m_stack->stack[m_stack->stack_i],
275                                 DEBUG_STACK_TEXT_SIZE, "%s", text);
276                 m_stack->stack_i++;
277                 if(m_stack->stack_i > m_stack->stack_max_i)
278                         m_stack->stack_max_i = m_stack->stack_i;
279         }
280 }
281
282 DebugStacker::~DebugStacker()
283 {
284         JMutexAutoLock lock(g_debug_stacks_mutex);
285         
286         if(m_overflowed == true)
287                 return;
288         
289         m_stack->stack_i--;
290
291         if(m_stack->stack_i == 0)
292         {
293                 threadid_t threadid = m_stack->threadid;
294                 /*DEBUGPRINT("Deleting debug stack for thread %x\n",
295                                 (unsigned int)threadid);*/
296                 delete m_stack;
297                 g_debug_stacks.erase(threadid);
298         }
299 }
300
301
302 #ifdef _MSC_VER
303 #if CATCH_UNHANDLED_EXCEPTIONS == 1
304 void se_trans_func(unsigned int u, EXCEPTION_POINTERS* pExp)
305 {
306         dstream<<"In trans_func.\n";
307         if(u == EXCEPTION_ACCESS_VIOLATION)
308         {
309                 PEXCEPTION_RECORD r = pExp->ExceptionRecord;
310                 dstream<<"Access violation at "<<r->ExceptionAddress
311                                 <<" write?="<<r->ExceptionInformation[0]
312                                 <<" address="<<r->ExceptionInformation[1]
313                                 <<std::endl;
314                 throw FatalSystemException
315                 ("Access violation");
316         }
317         if(u == EXCEPTION_STACK_OVERFLOW)
318         {
319                 throw FatalSystemException
320                 ("Stack overflow");
321         }
322         if(u == EXCEPTION_ILLEGAL_INSTRUCTION)
323         {
324                 throw FatalSystemException
325                 ("Illegal instruction");
326         }
327 }
328 #endif
329 #endif
330
331
332