]> git.lizzy.rs Git - minetest.git/blob - src/debug.cpp
better debug output in segfaults and stack overflows in windows
[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 #include "porting.h"
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
37         if(filename)
38                 g_debugstreams[1] = fopen(filename, "a");
39                 
40         if(g_debugstreams[1])
41         {
42                 fprintf(g_debugstreams[1], "\n\n-------------\n");
43                 fprintf(g_debugstreams[1],     "  Separator  \n");
44                 fprintf(g_debugstreams[1],     "-------------\n\n");
45         }
46 }
47
48 void debugstreams_deinit()
49 {
50         if(g_debugstreams[1] != NULL)
51                 fclose(g_debugstreams[1]);
52 }
53
54 Debugbuf debugbuf(false);
55 std::ostream dstream(&debugbuf);
56 Debugbuf debugbuf_no_stderr(true);
57 std::ostream dstream_no_stderr(&debugbuf_no_stderr);
58 Nullstream dummyout;
59
60 /*
61         Assert
62 */
63
64 void assert_fail(const char *assertion, const char *file,
65                 unsigned int line, const char *function)
66 {
67         DEBUGPRINT("\nIn thread %x:\n"
68                         "%s:%d: %s: Assertion '%s' failed.\n",
69                         (unsigned int)get_current_thread_id(),
70                         file, line, function, assertion);
71         
72         debug_stacks_print();
73
74         if(g_debugstreams[1])
75                 fclose(g_debugstreams[1]);
76
77         //sleep_ms(3000);
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 }
92
93 void DebugStack::print(FILE *file, bool everything)
94 {
95         fprintf(file, "DEBUG STACK FOR THREAD %x:\n",
96                         (unsigned int)threadid);
97
98         for(int i=0; i<stack_max_i; i++)
99         {
100                 if(i == stack_i && everything == false)
101                         continue;
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
114 core::map<threadid_t, DebugStack*> g_debug_stacks;
115 JMutex g_debug_stacks_mutex;
116
117 void debug_stacks_init()
118 {
119         g_debug_stacks_mutex.Init();
120 }
121
122 void debug_stacks_print()
123 {
124         JMutexAutoLock lock(g_debug_stacks_mutex);
125
126         DEBUGPRINT("Debug stacks:\n");
127
128         for(core::map<threadid_t, DebugStack*>::Iterator
129                         i = g_debug_stacks.getIterator();
130                         i.atEnd() == false; i++)
131         {
132                 DebugStack *stack = i.getNode()->getValue();
133
134                 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
135                 {
136                         if(g_debugstreams[i] != NULL)
137                                 stack->print(g_debugstreams[i], true);
138                 }
139         }
140 }
141
142 DebugStacker::DebugStacker(const char *text)
143 {
144         threadid_t threadid = get_current_thread_id();
145
146         JMutexAutoLock lock(g_debug_stacks_mutex);
147
148         core::map<threadid_t, DebugStack*>::Node *n;
149         n = g_debug_stacks.find(threadid);
150         if(n != NULL)
151         {
152                 m_stack = n->getValue();
153         }
154         else
155         {
156                 /*DEBUGPRINT("Creating new debug stack for thread %x\n",
157                                 (unsigned int)threadid);*/
158                 m_stack = new DebugStack(threadid);
159                 g_debug_stacks.insert(threadid, m_stack);
160         }
161
162         if(m_stack->stack_i >= DEBUG_STACK_SIZE)
163         {
164                 m_overflowed = true;
165         }
166         else
167         {
168                 m_overflowed = false;
169
170                 snprintf(m_stack->stack[m_stack->stack_i],
171                                 DEBUG_STACK_TEXT_SIZE, "%s", text);
172                 m_stack->stack_i++;
173                 if(m_stack->stack_i > m_stack->stack_max_i)
174                         m_stack->stack_max_i = m_stack->stack_i;
175         }
176 }
177
178 DebugStacker::~DebugStacker()
179 {
180         JMutexAutoLock lock(g_debug_stacks_mutex);
181         
182         if(m_overflowed == true)
183                 return;
184         
185         m_stack->stack_i--;
186
187         if(m_stack->stack_i == 0)
188         {
189                 threadid_t threadid = m_stack->threadid;
190                 /*DEBUGPRINT("Deleting debug stack for thread %x\n",
191                                 (unsigned int)threadid);*/
192                 delete m_stack;
193                 g_debug_stacks.remove(threadid);
194         }
195 }
196
197
198 #ifdef _WIN32
199 void se_trans_func(unsigned int u, EXCEPTION_POINTERS* pExp)
200 {
201         dstream<<"In trans_func.\n";
202         if(u == EXCEPTION_ACCESS_VIOLATION)
203         {
204                 PEXCEPTION_RECORD r = pExp->ExceptionRecord;
205                 dstream<<"Access violation at "<<r->ExceptionAddress
206                                 <<" write?="<<r->ExceptionInformation[0]
207                                 <<" address="<<r->ExceptionInformation[1]
208                                 <<std::endl;
209                 throw FatalSystemException
210                 ("Access violation");
211         }
212         if(u == EXCEPTION_STACK_OVERFLOW)
213         {
214                 throw FatalSystemException
215                 ("Stack overflow");
216         }
217         if(u == EXCEPTION_ILLEGAL_INSTRUCTION)
218         {
219                 throw FatalSystemException
220                 ("Illegal instruction");
221         }
222 }
223 #endif
224
225
226