]> git.lizzy.rs Git - minetest-m13.git/blob - src/debug.cpp
Update to 4.6 base
[minetest-m13.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 %lx:\n"
72                         "%s:%d: %s: Assertion '%s' failed.\n",
73                         (unsigned long)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         memset(stack, 0, DEBUG_STACK_SIZE*DEBUG_STACK_TEXT_SIZE);
94 }
95
96 void DebugStack::print(FILE *file, bool everything)
97 {
98         fprintf(file, "DEBUG STACK FOR THREAD %lx:\n",
99                         (unsigned long)threadid);
100
101         for(int i=0; i<stack_max_i; i++)
102         {
103                 if(i == stack_i && everything == false)
104                         break;
105
106                 if(i < stack_i)
107                         fprintf(file, "#%d  %s\n", i, stack[i]);
108                 else
109                         fprintf(file, "(Leftover data: #%d  %s)\n", i, stack[i]);
110         }
111
112         if(stack_i == DEBUG_STACK_SIZE)
113                 fprintf(file, "Probably overflown.\n");
114 }
115
116 void DebugStack::print(std::ostream &os, bool everything)
117 {
118         os<<"DEBUG STACK FOR THREAD "<<(unsigned long)threadid<<": "<<std::endl;
119
120         for(int i=0; i<stack_max_i; i++)
121         {
122                 if(i == stack_i && everything == false)
123                         break;
124
125                 if(i < stack_i)
126                         os<<"#"<<i<<"  "<<stack[i]<<std::endl;
127                 else
128                         os<<"(Leftover data: #"<<i<<"  "<<stack[i]<<")"<<std::endl;
129         }
130
131         if(stack_i == DEBUG_STACK_SIZE)
132                 os<<"Probably overflown."<<std::endl;
133 }
134
135 core::map<threadid_t, DebugStack*> g_debug_stacks;
136 JMutex g_debug_stacks_mutex;
137
138 void debug_stacks_init()
139 {
140         g_debug_stacks_mutex.Init();
141 }
142
143 void debug_stacks_print_to(std::ostream &os)
144 {
145         JMutexAutoLock lock(g_debug_stacks_mutex);
146
147         os<<"Debug stacks:"<<std::endl;
148
149         for(core::map<threadid_t, DebugStack*>::Iterator
150                         i = g_debug_stacks.getIterator();
151                         i.atEnd() == false; i++)
152         {
153                 DebugStack *stack = i.getNode()->getValue();
154                 stack->print(os, false);
155         }
156 }
157
158 void debug_stacks_print()
159 {
160         JMutexAutoLock lock(g_debug_stacks_mutex);
161
162         DEBUGPRINT("Debug stacks:\n");
163
164         for(core::map<threadid_t, DebugStack*>::Iterator
165                         i = g_debug_stacks.getIterator();
166                         i.atEnd() == false; i++)
167         {
168                 DebugStack *stack = i.getNode()->getValue();
169
170                 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
171                 {
172                         if(g_debugstreams[i] != NULL)
173                                 stack->print(g_debugstreams[i], true);
174                 }
175         }
176 }
177
178 DebugStacker::DebugStacker(const char *text)
179 {
180         threadid_t threadid = get_current_thread_id();
181
182         JMutexAutoLock lock(g_debug_stacks_mutex);
183
184         core::map<threadid_t, DebugStack*>::Node *n;
185         n = g_debug_stacks.find(threadid);
186         if(n != NULL)
187         {
188                 m_stack = n->getValue();
189         }
190         else
191         {
192                 /*DEBUGPRINT("Creating new debug stack for thread %x\n",
193                                 (unsigned int)threadid);*/
194                 m_stack = new DebugStack(threadid);
195                 g_debug_stacks.insert(threadid, m_stack);
196         }
197
198         if(m_stack->stack_i >= DEBUG_STACK_SIZE)
199         {
200                 m_overflowed = true;
201         }
202         else
203         {
204                 m_overflowed = false;
205
206                 snprintf(m_stack->stack[m_stack->stack_i],
207                                 DEBUG_STACK_TEXT_SIZE, "%s", text);
208                 m_stack->stack_i++;
209                 if(m_stack->stack_i > m_stack->stack_max_i)
210                         m_stack->stack_max_i = m_stack->stack_i;
211         }
212 }
213
214 DebugStacker::~DebugStacker()
215 {
216         JMutexAutoLock lock(g_debug_stacks_mutex);
217         
218         if(m_overflowed == true)
219                 return;
220         
221         m_stack->stack_i--;
222
223         if(m_stack->stack_i == 0)
224         {
225                 threadid_t threadid = m_stack->threadid;
226                 /*DEBUGPRINT("Deleting debug stack for thread %x\n",
227                                 (unsigned int)threadid);*/
228                 delete m_stack;
229                 g_debug_stacks.remove(threadid);
230         }
231 }
232
233
234 #ifdef _MSC_VER
235 #if CATCH_UNHANDLED_EXCEPTIONS == 1
236 void se_trans_func(unsigned int u, EXCEPTION_POINTERS* pExp)
237 {
238         dstream<<"In trans_func.\n";
239         if(u == EXCEPTION_ACCESS_VIOLATION)
240         {
241                 PEXCEPTION_RECORD r = pExp->ExceptionRecord;
242                 dstream<<"Access violation at "<<r->ExceptionAddress
243                                 <<" write?="<<r->ExceptionInformation[0]
244                                 <<" address="<<r->ExceptionInformation[1]
245                                 <<std::endl;
246                 throw FatalSystemException
247                 ("Access violation");
248         }
249         if(u == EXCEPTION_STACK_OVERFLOW)
250         {
251                 throw FatalSystemException
252                 ("Stack overflow");
253         }
254         if(u == EXCEPTION_ILLEGAL_INSTRUCTION)
255         {
256                 throw FatalSystemException
257                 ("Illegal instruction");
258         }
259 }
260 #endif
261 #endif
262
263
264