Optionally display the value of several variables within the Status command.
[asterisk-bristuff.git] / main / astmm.c
blobf14ab8bdc3a0d81d372bd8e2a68d49a45defb63d
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 "asterisk/paths.h" /* use ast_config_AST_LOG_DIR */
33 #include <time.h>
35 #include "asterisk/cli.h"
36 #include "asterisk/lock.h"
37 #include "asterisk/strings.h"
38 #include "asterisk/unaligned.h"
40 #define SOME_PRIME 563
42 enum func_type {
43 FUNC_CALLOC = 1,
44 FUNC_MALLOC,
45 FUNC_REALLOC,
46 FUNC_STRDUP,
47 FUNC_STRNDUP,
48 FUNC_VASPRINTF,
49 FUNC_ASPRINTF
52 /* Undefine all our macros */
53 #undef malloc
54 #undef calloc
55 #undef realloc
56 #undef strdup
57 #undef strndup
58 #undef free
59 #undef vasprintf
60 #undef asprintf
62 #define FENCE_MAGIC 0xdeadbeef
64 static FILE *mmlog;
66 static struct ast_region {
67 struct ast_region *next;
68 char file[64];
69 char func[40];
70 unsigned int lineno;
71 enum func_type which;
72 unsigned int cache; /* region was allocated as part of a cache pool */
73 size_t len;
74 unsigned int fence;
75 unsigned char data[0];
76 } *regions[SOME_PRIME];
78 #define HASH(a) \
79 (((unsigned long)(a)) % SOME_PRIME)
81 /*! Tracking this mutex will cause infinite recursion, as the mutex tracking
82 * code allocates memory */
83 AST_MUTEX_DEFINE_STATIC_NOTRACKING(reglock);
85 #define astmm_log(...) \
86 do { \
87 fprintf(stderr, __VA_ARGS__); \
88 if (mmlog) { \
89 fprintf(mmlog, __VA_ARGS__); \
90 fflush(mmlog); \
91 } \
92 } while (0)
94 static inline void *__ast_alloc_region(size_t size, const enum func_type which, const char *file, int lineno, const char *func, unsigned int cache)
96 struct ast_region *reg;
97 void *ptr = NULL;
98 unsigned int *fence;
99 int hash;
101 if (!(reg = malloc(size + sizeof(*reg) + sizeof(*fence)))) {
102 astmm_log("Memory Allocation Failure - '%d' bytes in function %s "
103 "at line %d of %s\n", (int) size, func, lineno, file);
106 ast_copy_string(reg->file, file, sizeof(reg->file));
107 ast_copy_string(reg->func, func, sizeof(reg->func));
108 reg->lineno = lineno;
109 reg->len = size;
110 reg->which = which;
111 reg->cache = cache;
112 ptr = reg->data;
113 hash = HASH(ptr);
114 reg->fence = FENCE_MAGIC;
115 fence = (ptr + reg->len);
116 put_unaligned_uint32(fence, FENCE_MAGIC);
118 ast_mutex_lock(&reglock);
119 reg->next = regions[hash];
120 regions[hash] = reg;
121 ast_mutex_unlock(&reglock);
123 return ptr;
126 static inline size_t __ast_sizeof_region(void *ptr)
128 int hash = HASH(ptr);
129 struct ast_region *reg;
130 size_t len = 0;
132 ast_mutex_lock(&reglock);
133 for (reg = regions[hash]; reg; reg = reg->next) {
134 if (reg->data == ptr) {
135 len = reg->len;
136 break;
139 ast_mutex_unlock(&reglock);
141 return len;
144 static void __ast_free_region(void *ptr, const char *file, int lineno, const char *func)
146 int hash = HASH(ptr);
147 struct ast_region *reg, *prev = NULL;
148 unsigned int *fence;
150 ast_mutex_lock(&reglock);
151 for (reg = regions[hash]; reg; reg = reg->next) {
152 if (reg->data == ptr) {
153 if (prev)
154 prev->next = reg->next;
155 else
156 regions[hash] = reg->next;
157 break;
159 prev = reg;
161 ast_mutex_unlock(&reglock);
163 if (reg) {
164 fence = (unsigned int *)(reg->data + reg->len);
165 if (reg->fence != FENCE_MAGIC) {
166 astmm_log("WARNING: Low fence violation at %p, in %s of %s, "
167 "line %d\n", reg->data, reg->func, reg->file, reg->lineno);
169 if (get_unaligned_uint32(fence) != FENCE_MAGIC) {
170 astmm_log("WARNING: High fence violation at %p, in %s of %s, "
171 "line %d\n", reg->data, reg->func, reg->file, reg->lineno);
173 free(reg);
174 } else {
175 astmm_log("WARNING: Freeing unused memory at %p, in %s of %s, line %d\n",
176 ptr, func, file, lineno);
180 void *__ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func)
182 void *ptr;
184 if ((ptr = __ast_alloc_region(size * nmemb, FUNC_CALLOC, file, lineno, func, 0)))
185 memset(ptr, 0, size * nmemb);
187 return ptr;
190 void *__ast_calloc_cache(size_t nmemb, size_t size, const char *file, int lineno, const char *func)
192 void *ptr;
194 if ((ptr = __ast_alloc_region(size * nmemb, FUNC_CALLOC, file, lineno, func, 1)))
195 memset(ptr, 0, size * nmemb);
197 return ptr;
200 void *__ast_malloc(size_t size, const char *file, int lineno, const char *func)
202 return __ast_alloc_region(size, FUNC_MALLOC, file, lineno, func, 0);
205 void __ast_free(void *ptr, const char *file, int lineno, const char *func)
207 __ast_free_region(ptr, file, lineno, func);
210 void *__ast_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func)
212 void *tmp;
213 size_t len = 0;
215 if (ptr && !(len = __ast_sizeof_region(ptr))) {
216 astmm_log("WARNING: Realloc of unalloced memory at %p, in %s of %s, "
217 "line %d\n", ptr, func, file, lineno);
218 return NULL;
221 if (!(tmp = __ast_alloc_region(size, FUNC_REALLOC, file, lineno, func, 0)))
222 return NULL;
224 if (len > size)
225 len = size;
226 if (ptr) {
227 memcpy(tmp, ptr, len);
228 __ast_free_region(ptr, file, lineno, func);
231 return tmp;
234 char *__ast_strdup(const char *s, const char *file, int lineno, const char *func)
236 size_t len;
237 void *ptr;
239 if (!s)
240 return NULL;
242 len = strlen(s) + 1;
243 if ((ptr = __ast_alloc_region(len, FUNC_STRDUP, file, lineno, func, 0)))
244 strcpy(ptr, s);
246 return ptr;
249 char *__ast_strndup(const char *s, size_t n, const char *file, int lineno, const char *func)
251 size_t len;
252 void *ptr;
254 if (!s)
255 return NULL;
257 len = strlen(s) + 1;
258 if (len > n)
259 len = n;
260 if ((ptr = __ast_alloc_region(len, FUNC_STRNDUP, file, lineno, func, 0)))
261 strcpy(ptr, s);
263 return ptr;
266 int __ast_asprintf(const char *file, int lineno, const char *func, char **strp, const char *fmt, ...)
268 int size;
269 va_list ap, ap2;
270 char s;
272 *strp = NULL;
273 va_start(ap, fmt);
274 va_copy(ap2, ap);
275 size = vsnprintf(&s, 1, fmt, ap2);
276 va_end(ap2);
277 if (!(*strp = __ast_alloc_region(size + 1, FUNC_ASPRINTF, file, lineno, func, 0))) {
278 va_end(ap);
279 return -1;
281 vsnprintf(*strp, size + 1, fmt, ap);
282 va_end(ap);
284 return size;
287 int __ast_vasprintf(char **strp, const char *fmt, va_list ap, const char *file, int lineno, const char *func)
289 int size;
290 va_list ap2;
291 char s;
293 *strp = NULL;
294 va_copy(ap2, ap);
295 size = vsnprintf(&s, 1, fmt, ap2);
296 va_end(ap2);
297 if (!(*strp = __ast_alloc_region(size + 1, FUNC_VASPRINTF, file, lineno, func, 0))) {
298 va_end(ap);
299 return -1;
301 vsnprintf(*strp, size + 1, fmt, ap);
303 return size;
306 static char *handle_memory_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
308 char *fn = NULL;
309 struct ast_region *reg;
310 unsigned int x;
311 unsigned int len = 0;
312 unsigned int cache_len = 0;
313 unsigned int count = 0;
314 unsigned int *fence;
316 switch (cmd) {
317 case CLI_INIT:
318 e->command = "memory show allocations";
319 e->usage =
320 "Usage: memory show allocations [<file>]\n"
321 " Dumps a list of all segments of allocated memory, optionally\n"
322 " limited to those from a specific file\n";
323 return NULL;
324 case CLI_GENERATE:
325 return NULL;
329 if (a->argc > 3)
330 fn = a->argv[3];
332 ast_mutex_lock(&reglock);
333 for (x = 0; x < SOME_PRIME; x++) {
334 for (reg = regions[x]; reg; reg = reg->next) {
335 if (!fn || !strcasecmp(fn, reg->file) || !strcasecmp(fn, "anomolies")) {
336 fence = (unsigned int *)(reg->data + reg->len);
337 if (reg->fence != FENCE_MAGIC) {
338 astmm_log("WARNING: Low fence violation at %p, "
339 "in %s of %s, line %d\n", reg->data,
340 reg->func, reg->file, reg->lineno);
342 if (get_unaligned_uint32(fence) != FENCE_MAGIC) {
343 astmm_log("WARNING: High fence violation at %p, in %s of %s, "
344 "line %d\n", reg->data, reg->func, reg->file, reg->lineno);
347 if (!fn || !strcasecmp(fn, reg->file)) {
348 ast_cli(a->fd, "%10d bytes allocated%s in %20s at line %5d of %s\n",
349 (int) reg->len, reg->cache ? " (cache)" : "",
350 reg->func, reg->lineno, reg->file);
351 len += reg->len;
352 if (reg->cache)
353 cache_len += reg->len;
354 count++;
358 ast_mutex_unlock(&reglock);
360 if (cache_len)
361 ast_cli(a->fd, "%d bytes allocated (%d in caches) in %d allocations\n", len, cache_len, count);
362 else
363 ast_cli(a->fd, "%d bytes allocated in %d allocations\n", len, count);
365 return CLI_SUCCESS;
368 static char *handle_memory_show_summary(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
370 char *fn = NULL;
371 int x;
372 struct ast_region *reg;
373 unsigned int len = 0;
374 unsigned int cache_len = 0;
375 int count = 0;
376 struct file_summary {
377 char fn[80];
378 int len;
379 int cache_len;
380 int count;
381 struct file_summary *next;
382 } *list = NULL, *cur;
384 switch (cmd) {
385 case CLI_INIT:
386 e->command = "memory show summary";
387 e->usage =
388 "Usage: memory show summary [<file>]\n"
389 " Summarizes heap memory allocations by file, or optionally\n"
390 "by function, if a file is specified\n";
391 return NULL;
392 case CLI_GENERATE:
393 return NULL;
396 if (a->argc > 3)
397 fn = a->argv[3];
399 ast_mutex_lock(&reglock);
400 for (x = 0; x < SOME_PRIME; x++) {
401 for (reg = regions[x]; reg; reg = reg->next) {
402 if (fn && strcasecmp(fn, reg->file))
403 continue;
405 for (cur = list; cur; cur = cur->next) {
406 if ((!fn && !strcmp(cur->fn, reg->file)) || (fn && !strcmp(cur->fn, reg->func)))
407 break;
409 if (!cur) {
410 cur = alloca(sizeof(*cur));
411 memset(cur, 0, sizeof(*cur));
412 ast_copy_string(cur->fn, fn ? reg->func : reg->file, sizeof(cur->fn));
413 cur->next = list;
414 list = cur;
417 cur->len += reg->len;
418 if (reg->cache)
419 cur->cache_len += reg->len;
420 cur->count++;
423 ast_mutex_unlock(&reglock);
425 /* Dump the whole list */
426 for (cur = list; cur; cur = cur->next) {
427 len += cur->len;
428 cache_len += cur->cache_len;
429 count += cur->count;
430 if (cur->cache_len) {
431 if (fn) {
432 ast_cli(a->fd, "%10d bytes (%10d cache) in %d allocations in function '%s' of '%s'\n",
433 cur->len, cur->cache_len, cur->count, cur->fn, fn);
434 } else {
435 ast_cli(a->fd, "%10d bytes (%10d cache) in %d allocations in file '%s'\n",
436 cur->len, cur->cache_len, cur->count, cur->fn);
438 } else {
439 if (fn) {
440 ast_cli(a->fd, "%10d bytes in %d allocations in function '%s' of '%s'\n",
441 cur->len, cur->count, cur->fn, fn);
442 } else {
443 ast_cli(a->fd, "%10d bytes in %d allocations in file '%s'\n",
444 cur->len, cur->count, cur->fn);
449 if (cache_len)
450 ast_cli(a->fd, "%d bytes allocated (%d in caches) in %d allocations\n", len, cache_len, count);
451 else
452 ast_cli(a->fd, "%d bytes allocated in %d allocations\n", len, count);
454 return CLI_SUCCESS;
457 static struct ast_cli_entry cli_memory[] = {
458 AST_CLI_DEFINE(handle_memory_show, "Display outstanding memory allocations"),
459 AST_CLI_DEFINE(handle_memory_show_summary, "Summarize outstanding memory allocations"),
462 void __ast_mm_init(void)
464 char filename[PATH_MAX];
466 ast_cli_register_multiple(cli_memory, sizeof(cli_memory) / sizeof(struct ast_cli_entry));
468 snprintf(filename, sizeof(filename), "%s/mmlog", ast_config_AST_LOG_DIR);
470 ast_verb(1, "Asterisk Malloc Debugger Started (see %s))\n", filename);
472 if ((mmlog = fopen(filename, "a+"))) {
473 fprintf(mmlog, "%ld - New session\n", (long)time(NULL));
474 fflush(mmlog);
478 #endif