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