]> git.lizzy.rs Git - minetest.git/blob - src/debug.cpp
Fix red background missing in deathscreen
[minetest.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 "jthread/jmutex.h"
29 #include "jthread/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 }
210
211 void debug_stacks_print_to(std::ostream &os)
212 {
213         JMutexAutoLock lock(g_debug_stacks_mutex);
214
215         os<<"Debug stacks:"<<std::endl;
216
217         for(std::map<threadid_t, DebugStack*>::iterator
218                         i = g_debug_stacks.begin();
219                         i != g_debug_stacks.end(); ++i)
220         {
221                 i->second->print(os, false);
222         }
223 }
224
225 void debug_stacks_print()
226 {
227         JMutexAutoLock lock(g_debug_stacks_mutex);
228
229         DEBUGPRINT("Debug stacks:\n");
230
231         for(std::map<threadid_t, DebugStack*>::iterator
232                         i = g_debug_stacks.begin();
233                         i != g_debug_stacks.end(); ++i)
234         {
235                 DebugStack *stack = i->second;
236
237                 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
238                 {
239                         if(g_debugstreams[i] != NULL)
240                                 stack->print(g_debugstreams[i], true);
241                 }
242         }
243 }
244
245 DebugStacker::DebugStacker(const char *text)
246 {
247         threadid_t threadid = get_current_thread_id();
248
249         JMutexAutoLock lock(g_debug_stacks_mutex);
250
251         std::map<threadid_t, DebugStack*>::iterator n;
252         n = g_debug_stacks.find(threadid);
253         if(n != g_debug_stacks.end())
254         {
255                 m_stack = n->second;
256         }
257         else
258         {
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;
263         }
264
265         if(m_stack->stack_i >= DEBUG_STACK_SIZE)
266         {
267                 m_overflowed = true;
268         }
269         else
270         {
271                 m_overflowed = false;
272
273                 snprintf(m_stack->stack[m_stack->stack_i],
274                                 DEBUG_STACK_TEXT_SIZE, "%s", text);
275                 m_stack->stack_i++;
276                 if(m_stack->stack_i > m_stack->stack_max_i)
277                         m_stack->stack_max_i = m_stack->stack_i;
278         }
279 }
280
281 DebugStacker::~DebugStacker()
282 {
283         JMutexAutoLock lock(g_debug_stacks_mutex);
284         
285         if(m_overflowed == true)
286                 return;
287         
288         m_stack->stack_i--;
289
290         if(m_stack->stack_i == 0)
291         {
292                 threadid_t threadid = m_stack->threadid;
293                 /*DEBUGPRINT("Deleting debug stack for thread %x\n",
294                                 (unsigned int)threadid);*/
295                 delete m_stack;
296                 g_debug_stacks.erase(threadid);
297         }
298 }
299
300
301 #ifdef _MSC_VER
302 #if CATCH_UNHANDLED_EXCEPTIONS == 1
303 void se_trans_func(unsigned int u, EXCEPTION_POINTERS* pExp)
304 {
305         dstream<<"In trans_func.\n";
306         if(u == EXCEPTION_ACCESS_VIOLATION)
307         {
308                 PEXCEPTION_RECORD r = pExp->ExceptionRecord;
309                 dstream<<"Access violation at "<<r->ExceptionAddress
310                                 <<" write?="<<r->ExceptionInformation[0]
311                                 <<" address="<<r->ExceptionInformation[1]
312                                 <<std::endl;
313                 throw FatalSystemException
314                 ("Access violation");
315         }
316         if(u == EXCEPTION_STACK_OVERFLOW)
317         {
318                 throw FatalSystemException
319                 ("Stack overflow");
320         }
321         if(u == EXCEPTION_ILLEGAL_INSTRUCTION)
322         {
323                 throw FatalSystemException
324                 ("Illegal instruction");
325         }
326 }
327 #endif
328 #endif
329
330
331