utility.h: Change Buffer's interface to be more compatible with SharedBuffer's interf...
[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 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                         continue;
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
117 core::map<threadid_t, DebugStack*> g_debug_stacks;
118 JMutex g_debug_stacks_mutex;
119
120 void debug_stacks_init()
121 {
122         g_debug_stacks_mutex.Init();
123 }
124
125 void debug_stacks_print()
126 {
127         JMutexAutoLock lock(g_debug_stacks_mutex);
128
129         DEBUGPRINT("Debug stacks:\n");
130
131         for(core::map<threadid_t, DebugStack*>::Iterator
132                         i = g_debug_stacks.getIterator();
133                         i.atEnd() == false; i++)
134         {
135                 DebugStack *stack = i.getNode()->getValue();
136
137                 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
138                 {
139                         if(g_debugstreams[i] != NULL)
140                                 stack->print(g_debugstreams[i], true);
141                 }
142         }
143 }
144
145 DebugStacker::DebugStacker(const char *text)
146 {
147         threadid_t threadid = get_current_thread_id();
148
149         JMutexAutoLock lock(g_debug_stacks_mutex);
150
151         core::map<threadid_t, DebugStack*>::Node *n;
152         n = g_debug_stacks.find(threadid);
153         if(n != NULL)
154         {
155                 m_stack = n->getValue();
156         }
157         else
158         {
159                 /*DEBUGPRINT("Creating new debug stack for thread %x\n",
160                                 (unsigned int)threadid);*/
161                 m_stack = new DebugStack(threadid);
162                 g_debug_stacks.insert(threadid, m_stack);
163         }
164
165         if(m_stack->stack_i >= DEBUG_STACK_SIZE)
166         {
167                 m_overflowed = true;
168         }
169         else
170         {
171                 m_overflowed = false;
172
173                 snprintf(m_stack->stack[m_stack->stack_i],
174                                 DEBUG_STACK_TEXT_SIZE, "%s", text);
175                 m_stack->stack_i++;
176                 if(m_stack->stack_i > m_stack->stack_max_i)
177                         m_stack->stack_max_i = m_stack->stack_i;
178         }
179 }
180
181 DebugStacker::~DebugStacker()
182 {
183         JMutexAutoLock lock(g_debug_stacks_mutex);
184         
185         if(m_overflowed == true)
186                 return;
187         
188         m_stack->stack_i--;
189
190         if(m_stack->stack_i == 0)
191         {
192                 threadid_t threadid = m_stack->threadid;
193                 /*DEBUGPRINT("Deleting debug stack for thread %x\n",
194                                 (unsigned int)threadid);*/
195                 delete m_stack;
196                 g_debug_stacks.remove(threadid);
197         }
198 }
199
200
201 #ifdef _MSC_VER
202 #if CATCH_UNHANDLED_EXCEPTIONS == 1
203 void se_trans_func(unsigned int u, EXCEPTION_POINTERS* pExp)
204 {
205         dstream<<"In trans_func.\n";
206         if(u == EXCEPTION_ACCESS_VIOLATION)
207         {
208                 PEXCEPTION_RECORD r = pExp->ExceptionRecord;
209                 dstream<<"Access violation at "<<r->ExceptionAddress
210                                 <<" write?="<<r->ExceptionInformation[0]
211                                 <<" address="<<r->ExceptionInformation[1]
212                                 <<std::endl;
213                 throw FatalSystemException
214                 ("Access violation");
215         }
216         if(u == EXCEPTION_STACK_OVERFLOW)
217         {
218                 throw FatalSystemException
219                 ("Stack overflow");
220         }
221         if(u == EXCEPTION_ILLEGAL_INSTRUCTION)
222         {
223                 throw FatalSystemException
224                 ("Illegal instruction");
225         }
226 }
227 #endif
228 #endif
229
230
231