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