add ExtenSpy variant of ChanSpy
[asterisk-bristuff.git] / astmm.c
blob1a1c171ec591fb1e0987ab34c014560a6847421e
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
19 /*! \file
21 * \brief Memory Management
23 * \author Mark Spencer <markster@digium.com>
26 #ifdef __AST_DEBUG_MALLOC
28 #include "asterisk.h"
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include <stdio.h>
33 #include <string.h>
34 #include <time.h>
36 #include "asterisk/cli.h"
37 #include "asterisk/logger.h"
38 #include "asterisk/options.h"
39 #include "asterisk/lock.h"
40 #include "asterisk/strings.h"
41 #include "asterisk/unaligned.h"
43 #define SOME_PRIME 563
45 enum func_type {
46 FUNC_CALLOC = 1,
47 FUNC_MALLOC,
48 FUNC_REALLOC,
49 FUNC_STRDUP,
50 FUNC_STRNDUP,
51 FUNC_VASPRINTF,
52 FUNC_ASPRINTF
55 /* Undefine all our macros */
56 #undef malloc
57 #undef calloc
58 #undef realloc
59 #undef strdup
60 #undef strndup
61 #undef free
62 #undef vasprintf
63 #undef asprintf
65 #define FENCE_MAGIC 0xdeadbeef
67 static FILE *mmlog;
69 static struct ast_region {
70 struct ast_region *next;
71 char file[40];
72 char func[40];
73 int lineno;
74 enum func_type which;
75 size_t len;
76 unsigned int fence;
77 unsigned char data[0];
78 } *regions[SOME_PRIME];
80 #define HASH(a) \
81 (((unsigned long)(a)) % SOME_PRIME)
83 AST_MUTEX_DEFINE_STATIC(reglock);
84 AST_MUTEX_DEFINE_STATIC(showmemorylock);
86 static inline void *__ast_alloc_region(size_t size, const enum func_type which, const char *file, int lineno, const char *func)
88 struct ast_region *reg;
89 void *ptr = NULL;
90 unsigned int *fence;
91 int hash;
92 reg = malloc(size + sizeof(*reg) + sizeof(*fence));
93 ast_mutex_lock(&reglock);
94 if (reg) {
95 ast_copy_string(reg->file, file, sizeof(reg->file));
96 reg->file[sizeof(reg->file) - 1] = '\0';
97 ast_copy_string(reg->func, func, sizeof(reg->func));
98 reg->func[sizeof(reg->func) - 1] = '\0';
99 reg->lineno = lineno;
100 reg->len = size;
101 reg->which = which;
102 ptr = reg->data;
103 hash = HASH(ptr);
104 reg->next = regions[hash];
105 regions[hash] = reg;
106 reg->fence = FENCE_MAGIC;
107 fence = (ptr + reg->len);
108 put_unaligned_uint32(fence, FENCE_MAGIC);
110 ast_mutex_unlock(&reglock);
111 if (!reg) {
112 fprintf(stderr, "Memory Allocation Failure - '%d' bytes in function %s at line %d of %s\n", (int) size, func, lineno, file);
113 if (mmlog) {
114 fprintf(stderr, "%ld - Memory Allocation Failure - '%d' bytes in function %s at line %d of %s\n", time(NULL), (int) size, func, lineno, file);
115 fflush(mmlog);
118 return ptr;
121 static inline size_t __ast_sizeof_region(void *ptr)
123 int hash = HASH(ptr);
124 struct ast_region *reg;
125 size_t len = 0;
127 ast_mutex_lock(&reglock);
128 reg = regions[hash];
129 while (reg) {
130 if (reg->data == ptr) {
131 len = reg->len;
132 break;
134 reg = reg->next;
136 ast_mutex_unlock(&reglock);
137 return len;
140 static void __ast_free_region(void *ptr, const char *file, int lineno, const char *func)
142 int hash = HASH(ptr);
143 struct ast_region *reg, *prev = NULL;
144 unsigned int *fence;
146 ast_mutex_lock(&reglock);
147 reg = regions[hash];
148 while (reg) {
149 if (reg->data == ptr) {
150 if (prev) {
151 prev->next = reg->next;
152 } else {
153 regions[hash] = reg->next;
155 break;
157 prev = reg;
158 reg = reg->next;
160 ast_mutex_unlock(&reglock);
161 if (reg) {
162 fence = (unsigned int *)(reg->data + reg->len);
163 if (reg->fence != FENCE_MAGIC) {
164 fprintf(stderr, "WARNING: Low fence violation at %p, in %s of %s, line %d\n", reg->data, reg->func, reg->file, reg->lineno);
165 if (mmlog) {
166 fprintf(mmlog, "%ld - WARNING: Low fence violation at %p, in %s of %s, line %d\n", time(NULL), reg->data, reg->func, reg->file, reg->lineno);
167 fflush(mmlog);
170 if (get_unaligned_uint32(fence) != FENCE_MAGIC) {
171 fprintf(stderr, "WARNING: High fence violation at %p, in %s of %s, line %d\n", reg->data, reg->func, reg->file, reg->lineno);
172 if (mmlog) {
173 fprintf(mmlog, "%ld - WARNING: High fence violation at %p, in %s of %s, line %d\n", time(NULL), reg->data, reg->func, reg->file, reg->lineno);
174 fflush(mmlog);
177 free(reg);
178 } else {
179 fprintf(stderr, "WARNING: Freeing unused memory at %p, in %s of %s, line %d\n", ptr, func, file, lineno);
180 if (mmlog) {
181 fprintf(mmlog, "%ld - WARNING: Freeing unused memory at %p, in %s of %s, line %d\n", time(NULL), ptr, func, file, lineno);
182 fflush(mmlog);
187 void *__ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func)
189 void *ptr;
190 if ((ptr = __ast_alloc_region(size * nmemb, FUNC_CALLOC, file, lineno, func)))
191 memset(ptr, 0, size * nmemb);
192 return ptr;
195 void *__ast_malloc(size_t size, const char *file, int lineno, const char *func)
197 return __ast_alloc_region(size, FUNC_MALLOC, file, lineno, func);
200 void __ast_free(void *ptr, const char *file, int lineno, const char *func)
202 __ast_free_region(ptr, file, lineno, func);
205 void *__ast_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func)
207 void *tmp;
208 size_t len = 0;
209 if (ptr && !(len = __ast_sizeof_region(ptr))) {
210 fprintf(stderr, "WARNING: Realloc of unalloced memory at %p, in %s of %s, line %d\n", ptr, func, file, lineno);
211 if (mmlog) {
212 fprintf(mmlog, "%ld - WARNING: Realloc of unalloced memory at %p, in %s of %s, line %d\n", time(NULL), ptr, func, file, lineno);
213 fflush(mmlog);
215 return NULL;
217 if ((tmp = __ast_alloc_region(size, FUNC_REALLOC, file, lineno, func))) {
218 if (len > size)
219 len = size;
220 if (ptr) {
221 memcpy(tmp, ptr, len);
222 __ast_free_region(ptr, file, lineno, func);
225 return tmp;
228 char *__ast_strdup(const char *s, const char *file, int lineno, const char *func)
230 size_t len;
231 void *ptr;
232 if (!s)
233 return NULL;
234 len = strlen(s) + 1;
235 if ((ptr = __ast_alloc_region(len, FUNC_STRDUP, file, lineno, func)))
236 strcpy(ptr, s);
237 return ptr;
240 char *__ast_strndup(const char *s, size_t n, const char *file, int lineno, const char *func)
242 size_t len;
243 void *ptr;
244 if (!s)
245 return NULL;
246 len = strlen(s) + 1;
247 if (len > n)
248 len = n;
249 if ((ptr = __ast_alloc_region(len, FUNC_STRNDUP, file, lineno, func)))
250 strcpy(ptr, s);
251 return ptr;
254 int __ast_asprintf(const char *file, int lineno, const char *func, char **strp, const char *fmt, ...)
256 int size;
257 va_list ap, ap2;
258 char s;
260 *strp = NULL;
261 va_start(ap, fmt);
262 va_copy(ap2, ap);
263 size = vsnprintf(&s, 1, fmt, ap2);
264 va_end(ap2);
265 if (!(*strp = __ast_alloc_region(size + 1, FUNC_ASPRINTF, file, lineno, func))) {
266 va_end(ap);
267 return -1;
269 vsnprintf(*strp, size + 1, fmt, ap);
270 va_end(ap);
272 return size;
275 int __ast_vasprintf(char **strp, const char *fmt, va_list ap, const char *file, int lineno, const char *func)
277 int size;
278 va_list ap2;
279 char s;
281 *strp = NULL;
282 va_copy(ap2, ap);
283 size = vsnprintf(&s, 1, fmt, ap2);
284 va_end(ap2);
285 if (!(*strp = __ast_alloc_region(size + 1, FUNC_VASPRINTF, file, lineno, func))) {
286 va_end(ap);
287 return -1;
289 vsnprintf(*strp, size + 1, fmt, ap);
291 return size;
294 static int handle_show_memory(int fd, int argc, char *argv[])
296 char *fn = NULL;
297 int x;
298 struct ast_region *reg;
299 unsigned int len = 0;
300 int count = 0;
301 unsigned int *fence;
302 if (argc > 3)
303 fn = argv[3];
305 /* try to lock applications list ... */
306 ast_mutex_lock(&showmemorylock);
308 for (x = 0; x < SOME_PRIME; x++) {
309 reg = regions[x];
310 while (reg) {
311 if (!fn || !strcasecmp(fn, reg->file) || !strcasecmp(fn, "anomolies")) {
312 fence = (unsigned int *)(reg->data + reg->len);
313 if (reg->fence != FENCE_MAGIC) {
314 fprintf(stderr, "WARNING: Low fence violation at %p, in %s of %s, line %d\n", reg->data, reg->func, reg->file, reg->lineno);
315 if (mmlog) {
316 fprintf(mmlog, "%ld - WARNING: Low fence violation at %p, in %s of %s, line %d\n", time(NULL), reg->data, reg->func, reg-> file, reg->lineno);
317 fflush(mmlog);
320 if (get_unaligned_uint32(fence) != FENCE_MAGIC) {
321 fprintf(stderr, "WARNING: High fence violation at %p, in %s of %s, line %d\n", reg->data, reg->func, reg->file, reg->lineno);
322 if (mmlog) {
323 fprintf(mmlog, "%ld - WARNING: High fence violation at %p, in %s of %s, line %d\n", time(NULL), reg->data, reg->func, reg->file, reg->lineno);
324 fflush(mmlog);
328 if (!fn || !strcasecmp(fn, reg->file)) {
329 ast_cli(fd, "%10d bytes allocated in %20s at line %5d of %s\n", (int) reg->len, reg->func, reg->lineno, reg->file);
330 len += reg->len;
331 count++;
333 reg = reg->next;
336 ast_cli(fd, "%d bytes allocated %d units total\n", len, count);
337 ast_mutex_unlock(&showmemorylock);
338 return RESULT_SUCCESS;
341 struct file_summary {
342 char fn[80];
343 int len;
344 int count;
345 struct file_summary *next;
348 static int handle_show_memory_summary(int fd, int argc, char *argv[])
350 char *fn = NULL;
351 int x;
352 struct ast_region *reg;
353 unsigned int len = 0;
354 int count = 0;
355 struct file_summary *list = NULL, *cur;
357 if (argc > 3)
358 fn = argv[3];
360 /* try to lock applications list ... */
361 ast_mutex_lock(&reglock);
363 for (x = 0; x < SOME_PRIME; x++) {
364 reg = regions[x];
365 while (reg) {
366 if (!fn || !strcasecmp(fn, reg->file)) {
367 cur = list;
368 while (cur) {
369 if ((!fn && !strcmp(cur->fn, reg->file)) || (fn && !strcmp(cur->fn, reg->func)))
370 break;
371 cur = cur->next;
373 if (!cur) {
374 cur = alloca(sizeof(*cur));
375 memset(cur, 0, sizeof(*cur));
376 ast_copy_string(cur->fn, fn ? reg->func : reg->file, sizeof(cur->fn));
377 cur->next = list;
378 list = cur;
380 cur->len += reg->len;
381 cur->count++;
383 reg = reg->next;
386 ast_mutex_unlock(&reglock);
388 /* Dump the whole list */
389 while (list) {
390 cur = list;
391 len += list->len;
392 count += list->count;
393 if (fn) {
394 ast_cli(fd, "%10d bytes in %5d allocations in function '%s' of '%s'\n", list->len, list->count, list->fn, fn);
395 } else {
396 ast_cli(fd, "%10d bytes in %5d allocations in file '%s'\n", list->len, list->count, list->fn);
398 list = list->next;
399 #if 0
400 free(cur);
401 #endif
403 ast_cli(fd, "%d bytes allocated %d units total\n", len, count);
404 return RESULT_SUCCESS;
407 static char show_memory_help[] =
408 "Usage: show memory allocations [<file>]\n"
409 " Dumps a list of all segments of allocated memory, optionally\n"
410 "limited to those from a specific file\n";
412 static char show_memory_summary_help[] =
413 "Usage: show memory summary [<file>]\n"
414 " Summarizes heap memory allocations by file, or optionally\n"
415 "by function, if a file is specified\n";
417 static struct ast_cli_entry show_memory_allocations_cli =
418 { { "show", "memory", "allocations", NULL },
419 handle_show_memory, "Display outstanding memory allocations",
420 show_memory_help };
422 static struct ast_cli_entry show_memory_summary_cli =
423 { { "show", "memory", "summary", NULL },
424 handle_show_memory_summary, "Summarize outstanding memory allocations",
425 show_memory_summary_help };
427 void __ast_mm_init(void)
429 char filename[80] = "";
430 ast_cli_register(&show_memory_allocations_cli);
431 ast_cli_register(&show_memory_summary_cli);
433 snprintf(filename, sizeof(filename), "%s/mmlog", (char *)ast_config_AST_LOG_DIR);
434 mmlog = fopen(filename, "a+");
435 if (option_verbose)
436 ast_verbose("Asterisk Malloc Debugger Started (see %s))\n", filename);
437 if (mmlog) {
438 fprintf(mmlog, "%ld - New session\n", time(NULL));
439 fflush(mmlog);
443 #endif