util: Stack backtracing facilities.
[mesa.git] / src / gallium / auxiliary / util / u_debug_memory.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * Memory debugging.
31 *
32 * @author José Fonseca <jrfonseca@tungstengraphics.com>
33 */
34
35 #include "pipe/p_config.h"
36
37 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
38 #include <windows.h>
39 #include <winddi.h>
40 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
41 #include <wdm.h>
42 #else
43 #include <stdio.h>
44 #include <stdlib.h>
45 #endif
46
47 #include "util/u_debug.h"
48 #include "util/u_debug_stack.h"
49 #include "util/u_double_list.h"
50
51
52 #define DEBUG_MEMORY_MAGIC 0x6e34090aU
53 #define DEBUG_MEMORY_STACK 0 /* XXX: disabled until we have symbol lookup */
54
55
56 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY) && !defined(WINCE)
57 #define real_malloc(_size) EngAllocMem(0, _size, 'D3AG')
58 #define real_free(_ptr) EngFreeMem(_ptr)
59 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
60 #define real_malloc(_size) ExAllocatePool(0, _size)
61 #define real_free(_ptr) ExFreePool(_ptr)
62 #else
63 #define real_malloc(_size) malloc(_size)
64 #define real_free(_ptr) free(_ptr)
65 #endif
66
67
68 struct debug_memory_header
69 {
70 struct list_head head;
71
72 unsigned long no;
73 const char *file;
74 unsigned line;
75 const char *function;
76 struct debug_stack_frame backtrace[DEBUG_MEMORY_STACK];
77 size_t size;
78
79 unsigned magic;
80 };
81
82 struct debug_memory_footer
83 {
84 unsigned magic;
85 };
86
87
88 static struct list_head list = { &list, &list };
89
90 static unsigned long last_no = 0;
91
92
93 static INLINE struct debug_memory_header *
94 header_from_data(void *data)
95 {
96 if(data)
97 return (struct debug_memory_header *)((char *)data - sizeof(struct debug_memory_header));
98 else
99 return NULL;
100 }
101
102 static INLINE void *
103 data_from_header(struct debug_memory_header *hdr)
104 {
105 if(hdr)
106 return (void *)((char *)hdr + sizeof(struct debug_memory_header));
107 else
108 return NULL;
109 }
110
111 static INLINE struct debug_memory_footer *
112 footer_from_header(struct debug_memory_header *hdr)
113 {
114 if(hdr)
115 return (struct debug_memory_footer *)((char *)hdr + sizeof(struct debug_memory_header) + hdr->size);
116 else
117 return NULL;
118 }
119
120
121 void *
122 debug_malloc(const char *file, unsigned line, const char *function,
123 size_t size)
124 {
125 struct debug_memory_header *hdr;
126 struct debug_memory_footer *ftr;
127
128 hdr = real_malloc(sizeof(*hdr) + size + sizeof(*ftr));
129 if(!hdr) {
130 debug_printf("%s:%u:%s: out of memory when trying to allocate %lu bytes\n",
131 file, line, function,
132 (long unsigned)size);
133 return NULL;
134 }
135
136 hdr->no = last_no++;
137 hdr->file = file;
138 hdr->line = line;
139 hdr->function = function;
140 hdr->size = size;
141 hdr->magic = DEBUG_MEMORY_MAGIC;
142
143 debug_backtrace_capture(hdr->backtrace, 0, DEBUG_MEMORY_STACK);
144
145 ftr = footer_from_header(hdr);
146 ftr->magic = DEBUG_MEMORY_MAGIC;
147
148 LIST_ADDTAIL(&hdr->head, &list);
149
150 return data_from_header(hdr);
151 }
152
153 void
154 debug_free(const char *file, unsigned line, const char *function,
155 void *ptr)
156 {
157 struct debug_memory_header *hdr;
158 struct debug_memory_footer *ftr;
159
160 if(!ptr)
161 return;
162
163 hdr = header_from_data(ptr);
164 if(hdr->magic != DEBUG_MEMORY_MAGIC) {
165 debug_printf("%s:%u:%s: freeing bad or corrupted memory %p\n",
166 file, line, function,
167 ptr);
168 debug_assert(0);
169 return;
170 }
171
172 ftr = footer_from_header(hdr);
173 if(ftr->magic != DEBUG_MEMORY_MAGIC) {
174 debug_printf("%s:%u:%s: buffer overflow %p\n",
175 hdr->file, hdr->line, hdr->function,
176 ptr);
177 debug_assert(0);
178 }
179
180 LIST_DEL(&hdr->head);
181 hdr->magic = 0;
182 ftr->magic = 0;
183
184 real_free(hdr);
185 }
186
187 void *
188 debug_calloc(const char *file, unsigned line, const char *function,
189 size_t count, size_t size )
190 {
191 void *ptr = debug_malloc( file, line, function, count * size );
192 if( ptr )
193 memset( ptr, 0, count * size );
194 return ptr;
195 }
196
197 void *
198 debug_realloc(const char *file, unsigned line, const char *function,
199 void *old_ptr, size_t old_size, size_t new_size )
200 {
201 struct debug_memory_header *old_hdr, *new_hdr;
202 struct debug_memory_footer *old_ftr, *new_ftr;
203 void *new_ptr;
204
205 if(!old_ptr)
206 return debug_malloc( file, line, function, new_size );
207
208 if(!new_size) {
209 debug_free( file, line, function, old_ptr );
210 return NULL;
211 }
212
213 old_hdr = header_from_data(old_ptr);
214 if(old_hdr->magic != DEBUG_MEMORY_MAGIC) {
215 debug_printf("%s:%u:%s: reallocating bad or corrupted memory %p\n",
216 file, line, function,
217 old_ptr);
218 debug_assert(0);
219 return NULL;
220 }
221
222 old_ftr = footer_from_header(old_hdr);
223 if(old_ftr->magic != DEBUG_MEMORY_MAGIC) {
224 debug_printf("%s:%u:%s: buffer overflow %p\n",
225 old_hdr->file, old_hdr->line, old_hdr->function,
226 old_ptr);
227 debug_assert(0);
228 }
229
230 /* alloc new */
231 new_hdr = real_malloc(sizeof(*new_hdr) + new_size + sizeof(*new_ftr));
232 if(!new_hdr) {
233 debug_printf("%s:%u:%s: out of memory when trying to allocate %lu bytes\n",
234 file, line, function,
235 (long unsigned)new_size);
236 return NULL;
237 }
238 new_hdr->no = old_hdr->no;
239 new_hdr->file = old_hdr->file;
240 new_hdr->line = old_hdr->line;
241 new_hdr->function = old_hdr->function;
242 new_hdr->size = new_size;
243 new_hdr->magic = DEBUG_MEMORY_MAGIC;
244
245 new_ftr = footer_from_header(new_hdr);
246 new_ftr->magic = DEBUG_MEMORY_MAGIC;
247
248 LIST_REPLACE(&old_hdr->head, &new_hdr->head);
249
250 /* copy data */
251 new_ptr = data_from_header(new_hdr);
252 memcpy( new_ptr, old_ptr, old_size < new_size ? old_size : new_size );
253
254 /* free old */
255 old_hdr->magic = 0;
256 old_ftr->magic = 0;
257 real_free(old_hdr);
258
259 return new_ptr;
260 }
261
262 unsigned long
263 debug_memory_begin(void)
264 {
265 return last_no;
266 }
267
268 void
269 debug_memory_end(unsigned long start_no)
270 {
271 size_t total_size = 0;
272 struct list_head *entry;
273
274 if(start_no == last_no)
275 return;
276
277 entry = list.prev;
278 for (; entry != &list; entry = entry->prev) {
279 struct debug_memory_header *hdr;
280 void *ptr;
281 struct debug_memory_footer *ftr;
282
283 hdr = LIST_ENTRY(struct debug_memory_header, entry, head);
284 ptr = data_from_header(hdr);
285 ftr = footer_from_header(hdr);
286
287 if(hdr->magic != DEBUG_MEMORY_MAGIC) {
288 debug_printf("%s:%u:%s: bad or corrupted memory %p\n",
289 hdr->file, hdr->line, hdr->function,
290 ptr);
291 debug_assert(0);
292 }
293
294 if((start_no <= hdr->no && hdr->no < last_no) ||
295 (last_no < start_no && (hdr->no < last_no || start_no <= hdr->no))) {
296 debug_printf("%s:%u:%s: %u bytes at %p not freed\n",
297 hdr->file, hdr->line, hdr->function,
298 hdr->size, ptr);
299 debug_backtrace_dump(hdr->backtrace, DEBUG_MEMORY_STACK);
300 total_size += hdr->size;
301 }
302
303 if(ftr->magic != DEBUG_MEMORY_MAGIC) {
304 debug_printf("%s:%u:%s: buffer overflow %p\n",
305 hdr->file, hdr->line, hdr->function,
306 ptr);
307 debug_assert(0);
308 }
309 }
310
311 if(total_size) {
312 debug_printf("Total of %u KB of system memory apparently leaked\n",
313 (total_size + 1023)/1024);
314 }
315 else {
316 debug_printf("No memory leaks detected.\n");
317 }
318 }