2 * Asterisk -- A telephony toolkit for Linux.
6 * Copyright (C) 2002, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
16 #ifdef __AST_DEBUG_MALLOC
23 #include <asterisk/cli.h>
24 #include <asterisk/logger.h>
25 #include <asterisk/options.h>
27 #define SOME_PRIME 563
31 #define FUNC_REALLOC 3
33 #define FUNC_STRNDUP 5
35 /* Undefine all our macros */
45 static struct ast_region {
46 struct ast_region *next;
52 unsigned char data[0];
53 } *regions[SOME_PRIME];
56 (((unsigned long)(a)) % SOME_PRIME)
58 static pthread_mutex_t reglock = PTHREAD_MUTEX_INITIALIZER;
60 static inline void *__ast_alloc_region(size_t size, int which, const char *file, int lineno, const char *func)
62 struct ast_region *reg;
65 reg = malloc(size + sizeof(struct ast_region));
66 pthread_mutex_lock(®lock);
68 strncpy(reg->file, file, sizeof(reg->file) - 1);
69 reg->file[sizeof(reg->file) - 1] = '\0';
70 strncpy(reg->func, func, sizeof(reg->func) - 1);
71 reg->func[sizeof(reg->func) - 1] = '\0';
77 reg->next = regions[hash];
80 pthread_mutex_unlock(®lock);
82 fprintf(stderr, "Out of memory :(\n");
84 fprintf(mmlog, "%ld - Out of memory\n", time(NULL));
91 static inline size_t __ast_sizeof_region(void *ptr)
94 struct ast_region *reg;
97 pthread_mutex_lock(®lock);
100 if (reg->data == ptr) {
106 pthread_mutex_unlock(®lock);
110 static void __ast_free_region(void *ptr, const char *file, int lineno, const char *func)
112 int hash = HASH(ptr);
113 struct ast_region *reg, *prev = NULL;
114 pthread_mutex_lock(®lock);
117 if (reg->data == ptr) {
119 prev->next = reg->next;
121 regions[hash] = reg->next;
128 pthread_mutex_unlock(®lock);
132 fprintf(stderr, "WARNING: Freeing unused memory at %p, in %s of %s, line %d\n",
133 ptr, func, file, lineno);
135 fprintf(mmlog, "%ld - WARNING: Freeing unused memory at %p, in %s of %s, line %d\n", time(NULL),
136 ptr, func, file, lineno);
142 void *__ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func)
145 ptr = __ast_alloc_region(size * nmemb, FUNC_CALLOC, file, lineno, func);
147 memset(ptr, 0, size * nmemb);
151 void *__ast_malloc(size_t size, const char *file, int lineno, const char *func)
153 return __ast_alloc_region(size, FUNC_MALLOC, file, lineno, func);
156 void __ast_free(void *ptr, const char *file, int lineno, const char *func)
158 __ast_free_region(ptr, file, lineno, func);
161 void *__ast_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func)
166 len = __ast_sizeof_region(ptr);
168 fprintf(stderr, "WARNING: Realloc of unalloced memory at %p, in %s of %s, line %d\n",
169 ptr, func, file, lineno);
171 fprintf(mmlog, "%ld - WARNING: Realloc of unalloced memory at %p, in %s of %s, line %d\n",
172 time(NULL), ptr, func, file, lineno);
178 tmp = __ast_alloc_region(size, FUNC_REALLOC, file, lineno, func);
183 memcpy(tmp, ptr, len);
184 __ast_free_region(ptr, file, lineno, func);
190 char *__ast_strdup(const char *s, const char *file, int lineno, const char *func)
197 ptr = __ast_alloc_region(len, FUNC_STRDUP, file, lineno, func);
203 char *__ast_strndup(const char *s, size_t n, const char *file, int lineno, const char *func)
212 ptr = __ast_alloc_region(len, FUNC_STRDUP, file, lineno, func);
218 static int handle_show_memory(int fd, int argc, char *argv[])
222 struct ast_region *reg;
228 /* try to lock applications list ... */
229 pthread_mutex_lock(®lock);
231 for (x=0;x<SOME_PRIME;x++) {
234 if (!fn || !strcasecmp(fn, reg->file)) {
235 ast_cli(fd, "%10d bytes allocated in %20s at line %5d of %s\n", reg->len, reg->func, reg->lineno, reg->file);
242 ast_cli(fd, "%d bytes allocated %d units total\n", len, count);
243 pthread_mutex_unlock(®lock);
244 return RESULT_SUCCESS;
247 struct file_summary {
251 struct file_summary *next;
254 static int handle_show_memory_summary(int fd, int argc, char *argv[])
258 struct ast_region *reg;
261 struct file_summary *list = NULL, *cur;
266 /* try to lock applications list ... */
267 pthread_mutex_lock(®lock);
269 for (x=0;x<SOME_PRIME;x++) {
272 if (!fn || !strcasecmp(fn, reg->file)) {
275 if ((!fn && !strcmp(cur->fn, reg->file)) || (fn && !strcmp(cur->fn, reg->func)))
280 cur = alloca(sizeof(struct file_summary));
281 memset(cur, 0, sizeof(struct file_summary));
282 strncpy(cur->fn, fn ? reg->func : reg->file, sizeof(cur->fn) - 1);
286 cur->len += reg->len;
292 pthread_mutex_unlock(®lock);
294 /* Dump the whole list */
298 count += list->count;
300 ast_cli(fd, "%10d bytes in %5d allocations in function '%s' of '%s'\n", list->len, list->count, list->fn, fn);
302 ast_cli(fd, "%10d bytes in %5d allocations in file '%s'\n", list->len, list->count, list->fn);
308 ast_cli(fd, "%d bytes allocated %d units total\n", len, count);
309 return RESULT_SUCCESS;
312 static char show_memory_help[] =
313 "Usage: show memory allocations [<file>]\n"
314 " Dumps a list of all segments of allocated memory, optionally\n"
315 "limited to those from a specific file\n";
317 static char show_memory_summary_help[] =
318 "Usage: show memory summary [<file>]\n"
319 " Summarizes heap memory allocations by file, or optionally\n"
320 "by function, if a file is specified\n";
322 static struct ast_cli_entry show_memory_allocations_cli =
323 { { "show", "memory", "allocations", NULL },
324 handle_show_memory, "Display outstanding memory allocations",
327 static struct ast_cli_entry show_memory_summary_cli =
328 { { "show", "memory", "summary", NULL },
329 handle_show_memory_summary, "Summarize outstanding memory allocations",
330 show_memory_summary_help };
333 void __ast_mm_init(void)
335 ast_cli_register(&show_memory_allocations_cli);
336 ast_cli_register(&show_memory_summary_cli);
337 mmlog = fopen("/var/log/asterisk/mmlog", "a+");
339 ast_verbose("Asterisk Malloc Debugger Started (see /var/log/asterisk/mmlog)\n");
341 fprintf(mmlog, "%ld - New session\n", time(NULL));