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(stderr, "%ld - Out of memory\n", time(NULL));
89 static inline size_t __ast_sizeof_region(void *ptr)
92 struct ast_region *reg;
95 pthread_mutex_lock(®lock);
98 if (reg->data == ptr) {
104 pthread_mutex_unlock(®lock);
108 static void __ast_free_region(void *ptr, const char *file, int lineno, const char *func)
110 int hash = HASH(ptr);
111 struct ast_region *reg, *prev = NULL;
112 pthread_mutex_lock(®lock);
115 if (reg->data == ptr) {
117 prev->next = reg->next;
119 regions[hash] = reg->next;
126 pthread_mutex_unlock(®lock);
130 fprintf(stderr, "WARNING: Freeing unused memory at %p, in %s of %s, line %d\n",
131 ptr, func, file, lineno);
133 fprintf(mmlog, "%ld - WARNING: Freeing unused memory at %p, in %s of %s, line %d\n", time(NULL),
134 ptr, func, file, lineno);
138 void *__ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func)
141 ptr = __ast_alloc_region(size * nmemb, FUNC_CALLOC, file, lineno, func);
143 memset(ptr, 0, size * nmemb);
147 void *__ast_malloc(size_t size, const char *file, int lineno, const char *func)
149 return __ast_alloc_region(size, FUNC_MALLOC, file, lineno, func);
152 void __ast_free(void *ptr, const char *file, int lineno, const char *func)
154 __ast_free_region(ptr, file, lineno, func);
157 void *__ast_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func)
162 len = __ast_sizeof_region(ptr);
164 fprintf(stderr, "WARNING: Realloc of unalloced memory at %p, in %s of %s, line %d\n",
165 ptr, func, file, lineno);
167 fprintf(mmlog, "%ld - WARNING: Realloc of unalloced memory at %p, in %s of %s, line %d\n",
168 time(NULL), ptr, func, file, lineno);
172 tmp = __ast_alloc_region(size, FUNC_REALLOC, file, lineno, func);
177 memcpy(tmp, ptr, len);
178 __ast_free_region(ptr, file, lineno, func);
184 char *__ast_strdup(const char *s, const char *file, int lineno, const char *func)
191 ptr = __ast_alloc_region(len, FUNC_STRDUP, file, lineno, func);
197 char *__ast_strndup(const char *s, size_t n, const char *file, int lineno, const char *func)
206 ptr = __ast_alloc_region(len, FUNC_STRDUP, file, lineno, func);
212 static int handle_show_memory(int fd, int argc, char *argv[])
216 struct ast_region *reg;
222 /* try to lock applications list ... */
223 pthread_mutex_lock(®lock);
225 for (x=0;x<SOME_PRIME;x++) {
228 if (!fn || !strcasecmp(fn, reg->file)) {
229 ast_cli(fd, "%10d bytes allocated in %20s at line %5d of %s\n", reg->len, reg->func, reg->lineno, reg->file);
236 ast_cli(fd, "%d bytes allocated %d units total\n", len, count);
237 pthread_mutex_unlock(®lock);
238 return RESULT_SUCCESS;
241 struct file_summary {
245 struct file_summary *next;
248 static int handle_show_memory_summary(int fd, int argc, char *argv[])
252 struct ast_region *reg;
255 struct file_summary *list = NULL, *cur;
260 /* try to lock applications list ... */
261 pthread_mutex_lock(®lock);
263 for (x=0;x<SOME_PRIME;x++) {
266 if (!fn || !strcasecmp(fn, reg->file)) {
269 if ((!fn && !strcmp(cur->fn, reg->file)) || (fn && !strcmp(cur->fn, reg->func)))
274 cur = alloca(sizeof(struct file_summary));
275 memset(cur, 0, sizeof(struct file_summary));
276 strncpy(cur->fn, fn ? reg->func : reg->file, sizeof(cur->fn) - 1);
280 cur->len += reg->len;
286 pthread_mutex_unlock(®lock);
288 /* Dump the whole list */
292 count += list->count;
294 ast_cli(fd, "%10d bytes in %5d allocations in function '%s' of '%s'\n", list->len, list->count, list->fn, fn);
296 ast_cli(fd, "%10d bytes in %5d allocations in file '%s'\n", list->len, list->count, list->fn);
302 ast_cli(fd, "%d bytes allocated %d units total\n", len, count);
303 return RESULT_SUCCESS;
306 static char show_memory_help[] =
307 "Usage: show memory allocations [<file>]\n"
308 " Dumps a list of all segments of allocated memory, optionally\n"
309 "limited to those from a specific file\n";
311 static char show_memory_summary_help[] =
312 "Usage: show memory summary [<file>]\n"
313 " Summarizes heap memory allocations by file, or optionally\n"
314 "by function, if a file is specified\n";
316 static struct ast_cli_entry show_memory_allocations_cli =
317 { { "show", "memory", "allocations", NULL },
318 handle_show_memory, "Display outstanding memory allocations",
321 static struct ast_cli_entry show_memory_summary_cli =
322 { { "show", "memory", "summary", NULL },
323 handle_show_memory_summary, "Summarize outstanding memory allocations",
324 show_memory_summary_help };
327 void __ast_mm_init(void)
329 ast_cli_register(&show_memory_allocations_cli);
330 ast_cli_register(&show_memory_summary_cli);
331 mmlog = fopen("/var/log/asterisk/mmlog", "a+");
333 ast_verbose("Asterisk Malloc Debugger Started (see /var/log/mmlog)\n");
335 fprintf(mmlog, "%ld - New session\n", time(NULL));