00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "asterisk.h"
00027
00028 #ifdef __AST_DEBUG_MALLOC
00029
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 157636 $")
00031
00032 #include "asterisk/paths.h"
00033 #include <stddef.h>
00034 #include <time.h>
00035
00036 #include "asterisk/cli.h"
00037 #include "asterisk/lock.h"
00038 #include "asterisk/strings.h"
00039 #include "asterisk/unaligned.h"
00040
00041 #define SOME_PRIME 563
00042
00043 enum func_type {
00044 FUNC_CALLOC = 1,
00045 FUNC_MALLOC,
00046 FUNC_REALLOC,
00047 FUNC_STRDUP,
00048 FUNC_STRNDUP,
00049 FUNC_VASPRINTF,
00050 FUNC_ASPRINTF
00051 };
00052
00053
00054 #undef malloc
00055 #undef calloc
00056 #undef realloc
00057 #undef strdup
00058 #undef strndup
00059 #undef free
00060 #undef vasprintf
00061 #undef asprintf
00062
00063 #define FENCE_MAGIC 0xdeadbeef
00064
00065 static FILE *mmlog;
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 static struct ast_region {
00080 struct ast_region *next;
00081 size_t len;
00082 char file[64];
00083 char func[40];
00084 unsigned int lineno;
00085 enum func_type which;
00086 unsigned int cache;
00087 unsigned int fence;
00088 unsigned char data[0];
00089 } *regions[SOME_PRIME];
00090
00091 #define HASH(a) \
00092 (((unsigned long)(a)) % SOME_PRIME)
00093
00094
00095
00096 AST_MUTEX_DEFINE_STATIC_NOTRACKING(reglock);
00097
00098 #define astmm_log(...) \
00099 do { \
00100 fprintf(stderr, __VA_ARGS__); \
00101 if (mmlog) { \
00102 fprintf(mmlog, __VA_ARGS__); \
00103 fflush(mmlog); \
00104 } \
00105 } while (0)
00106
00107 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)
00108 {
00109 struct ast_region *reg;
00110 void *ptr = NULL;
00111 unsigned int *fence;
00112 int hash;
00113
00114 if (!(reg = malloc(size + sizeof(*reg) + sizeof(*fence)))) {
00115 astmm_log("Memory Allocation Failure - '%d' bytes in function %s "
00116 "at line %d of %s\n", (int) size, func, lineno, file);
00117 return NULL;
00118 }
00119
00120 ast_copy_string(reg->file, file, sizeof(reg->file));
00121 ast_copy_string(reg->func, func, sizeof(reg->func));
00122 reg->lineno = lineno;
00123 reg->len = size;
00124 reg->which = which;
00125 reg->cache = cache;
00126 ptr = reg->data;
00127 hash = HASH(ptr);
00128 reg->fence = FENCE_MAGIC;
00129 fence = (ptr + reg->len);
00130 put_unaligned_uint32(fence, FENCE_MAGIC);
00131
00132 ast_mutex_lock(®lock);
00133 reg->next = regions[hash];
00134 regions[hash] = reg;
00135 ast_mutex_unlock(®lock);
00136
00137 return ptr;
00138 }
00139
00140 static inline size_t __ast_sizeof_region(void *ptr)
00141 {
00142 int hash = HASH(ptr);
00143 struct ast_region *reg;
00144 size_t len = 0;
00145
00146 ast_mutex_lock(®lock);
00147 for (reg = regions[hash]; reg; reg = reg->next) {
00148 if (reg->data == ptr) {
00149 len = reg->len;
00150 break;
00151 }
00152 }
00153 ast_mutex_unlock(®lock);
00154
00155 return len;
00156 }
00157
00158 static void __ast_free_region(void *ptr, const char *file, int lineno, const char *func)
00159 {
00160 int hash = HASH(ptr);
00161 struct ast_region *reg, *prev = NULL;
00162 unsigned int *fence;
00163
00164 ast_mutex_lock(®lock);
00165 for (reg = regions[hash]; reg; reg = reg->next) {
00166 if (reg->data == ptr) {
00167 if (prev)
00168 prev->next = reg->next;
00169 else
00170 regions[hash] = reg->next;
00171 break;
00172 }
00173 prev = reg;
00174 }
00175 ast_mutex_unlock(®lock);
00176
00177 if (reg) {
00178 fence = (unsigned int *)(reg->data + reg->len);
00179 if (reg->fence != FENCE_MAGIC) {
00180 astmm_log("WARNING: Low fence violation at %p, in %s of %s, "
00181 "line %d\n", reg->data, reg->func, reg->file, reg->lineno);
00182 }
00183 if (get_unaligned_uint32(fence) != FENCE_MAGIC) {
00184 astmm_log("WARNING: High fence violation at %p, in %s of %s, "
00185 "line %d\n", reg->data, reg->func, reg->file, reg->lineno);
00186 }
00187 free(reg);
00188 } else {
00189 astmm_log("WARNING: Freeing unused memory at %p, in %s of %s, line %d\n",
00190 ptr, func, file, lineno);
00191 }
00192 }
00193
00194 void *__ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func)
00195 {
00196 void *ptr;
00197
00198 if ((ptr = __ast_alloc_region(size * nmemb, FUNC_CALLOC, file, lineno, func, 0)))
00199 memset(ptr, 0, size * nmemb);
00200
00201 return ptr;
00202 }
00203
00204 void *__ast_calloc_cache(size_t nmemb, size_t size, const char *file, int lineno, const char *func)
00205 {
00206 void *ptr;
00207
00208 if ((ptr = __ast_alloc_region(size * nmemb, FUNC_CALLOC, file, lineno, func, 1)))
00209 memset(ptr, 0, size * nmemb);
00210
00211 return ptr;
00212 }
00213
00214 void *__ast_malloc(size_t size, const char *file, int lineno, const char *func)
00215 {
00216 return __ast_alloc_region(size, FUNC_MALLOC, file, lineno, func, 0);
00217 }
00218
00219 void __ast_free(void *ptr, const char *file, int lineno, const char *func)
00220 {
00221 __ast_free_region(ptr, file, lineno, func);
00222 }
00223
00224 void *__ast_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func)
00225 {
00226 void *tmp;
00227 size_t len = 0;
00228
00229 if (ptr && !(len = __ast_sizeof_region(ptr))) {
00230 astmm_log("WARNING: Realloc of unalloced memory at %p, in %s of %s, "
00231 "line %d\n", ptr, func, file, lineno);
00232 return NULL;
00233 }
00234
00235 if (!(tmp = __ast_alloc_region(size, FUNC_REALLOC, file, lineno, func, 0)))
00236 return NULL;
00237
00238 if (len > size)
00239 len = size;
00240 if (ptr) {
00241 memcpy(tmp, ptr, len);
00242 __ast_free_region(ptr, file, lineno, func);
00243 }
00244
00245 return tmp;
00246 }
00247
00248 char *__ast_strdup(const char *s, const char *file, int lineno, const char *func)
00249 {
00250 size_t len;
00251 void *ptr;
00252
00253 if (!s)
00254 return NULL;
00255
00256 len = strlen(s) + 1;
00257 if ((ptr = __ast_alloc_region(len, FUNC_STRDUP, file, lineno, func, 0)))
00258 strcpy(ptr, s);
00259
00260 return ptr;
00261 }
00262
00263 char *__ast_strndup(const char *s, size_t n, const char *file, int lineno, const char *func)
00264 {
00265 size_t len;
00266 void *ptr;
00267
00268 if (!s)
00269 return NULL;
00270
00271 len = strlen(s) + 1;
00272 if (len > n)
00273 len = n;
00274 if ((ptr = __ast_alloc_region(len, FUNC_STRNDUP, file, lineno, func, 0)))
00275 strcpy(ptr, s);
00276
00277 return ptr;
00278 }
00279
00280 int __ast_asprintf(const char *file, int lineno, const char *func, char **strp, const char *fmt, ...)
00281 {
00282 int size;
00283 va_list ap, ap2;
00284 char s;
00285
00286 *strp = NULL;
00287 va_start(ap, fmt);
00288 va_copy(ap2, ap);
00289 size = vsnprintf(&s, 1, fmt, ap2);
00290 va_end(ap2);
00291 if (!(*strp = __ast_alloc_region(size + 1, FUNC_ASPRINTF, file, lineno, func, 0))) {
00292 va_end(ap);
00293 return -1;
00294 }
00295 vsnprintf(*strp, size + 1, fmt, ap);
00296 va_end(ap);
00297
00298 return size;
00299 }
00300
00301 int __ast_vasprintf(char **strp, const char *fmt, va_list ap, const char *file, int lineno, const char *func)
00302 {
00303 int size;
00304 va_list ap2;
00305 char s;
00306
00307 *strp = NULL;
00308 va_copy(ap2, ap);
00309 size = vsnprintf(&s, 1, fmt, ap2);
00310 va_end(ap2);
00311 if (!(*strp = __ast_alloc_region(size + 1, FUNC_VASPRINTF, file, lineno, func, 0))) {
00312 va_end(ap);
00313 return -1;
00314 }
00315 vsnprintf(*strp, size + 1, fmt, ap);
00316
00317 return size;
00318 }
00319
00320 static char *handle_memory_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00321 {
00322 char *fn = NULL;
00323 struct ast_region *reg;
00324 unsigned int x;
00325 unsigned int len = 0;
00326 unsigned int cache_len = 0;
00327 unsigned int count = 0;
00328 unsigned int *fence;
00329
00330 switch (cmd) {
00331 case CLI_INIT:
00332 e->command = "memory show allocations";
00333 e->usage =
00334 "Usage: memory show allocations [<file>]\n"
00335 " Dumps a list of all segments of allocated memory, optionally\n"
00336 " limited to those from a specific file\n";
00337 return NULL;
00338 case CLI_GENERATE:
00339 return NULL;
00340 }
00341
00342
00343 if (a->argc > 3)
00344 fn = a->argv[3];
00345
00346 ast_mutex_lock(®lock);
00347 for (x = 0; x < SOME_PRIME; x++) {
00348 for (reg = regions[x]; reg; reg = reg->next) {
00349 if (!fn || !strcasecmp(fn, reg->file) || !strcasecmp(fn, "anomolies")) {
00350 fence = (unsigned int *)(reg->data + reg->len);
00351 if (reg->fence != FENCE_MAGIC) {
00352 astmm_log("WARNING: Low fence violation at %p, "
00353 "in %s of %s, line %d\n", reg->data,
00354 reg->func, reg->file, reg->lineno);
00355 }
00356 if (get_unaligned_uint32(fence) != FENCE_MAGIC) {
00357 astmm_log("WARNING: High fence violation at %p, in %s of %s, "
00358 "line %d\n", reg->data, reg->func, reg->file, reg->lineno);
00359 }
00360 }
00361 if (!fn || !strcasecmp(fn, reg->file)) {
00362 ast_cli(a->fd, "%10d bytes allocated%s in %20s at line %5d of %s\n",
00363 (int) reg->len, reg->cache ? " (cache)" : "",
00364 reg->func, reg->lineno, reg->file);
00365 len += reg->len;
00366 if (reg->cache)
00367 cache_len += reg->len;
00368 count++;
00369 }
00370 }
00371 }
00372 ast_mutex_unlock(®lock);
00373
00374 if (cache_len)
00375 ast_cli(a->fd, "%d bytes allocated (%d in caches) in %d allocations\n", len, cache_len, count);
00376 else
00377 ast_cli(a->fd, "%d bytes allocated in %d allocations\n", len, count);
00378
00379 return CLI_SUCCESS;
00380 }
00381
00382 static char *handle_memory_show_summary(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00383 {
00384 char *fn = NULL;
00385 int x;
00386 struct ast_region *reg;
00387 unsigned int len = 0;
00388 unsigned int cache_len = 0;
00389 int count = 0;
00390 struct file_summary {
00391 char fn[80];
00392 int len;
00393 int cache_len;
00394 int count;
00395 struct file_summary *next;
00396 } *list = NULL, *cur;
00397
00398 switch (cmd) {
00399 case CLI_INIT:
00400 e->command = "memory show summary";
00401 e->usage =
00402 "Usage: memory show summary [<file>]\n"
00403 " Summarizes heap memory allocations by file, or optionally\n"
00404 "by function, if a file is specified\n";
00405 return NULL;
00406 case CLI_GENERATE:
00407 return NULL;
00408 }
00409
00410 if (a->argc > 3)
00411 fn = a->argv[3];
00412
00413 ast_mutex_lock(®lock);
00414 for (x = 0; x < SOME_PRIME; x++) {
00415 for (reg = regions[x]; reg; reg = reg->next) {
00416 if (fn && strcasecmp(fn, reg->file))
00417 continue;
00418
00419 for (cur = list; cur; cur = cur->next) {
00420 if ((!fn && !strcmp(cur->fn, reg->file)) || (fn && !strcmp(cur->fn, reg->func)))
00421 break;
00422 }
00423 if (!cur) {
00424 cur = alloca(sizeof(*cur));
00425 memset(cur, 0, sizeof(*cur));
00426 ast_copy_string(cur->fn, fn ? reg->func : reg->file, sizeof(cur->fn));
00427 cur->next = list;
00428 list = cur;
00429 }
00430
00431 cur->len += reg->len;
00432 if (reg->cache)
00433 cur->cache_len += reg->len;
00434 cur->count++;
00435 }
00436 }
00437 ast_mutex_unlock(®lock);
00438
00439
00440 for (cur = list; cur; cur = cur->next) {
00441 len += cur->len;
00442 cache_len += cur->cache_len;
00443 count += cur->count;
00444 if (cur->cache_len) {
00445 if (fn) {
00446 ast_cli(a->fd, "%10d bytes (%10d cache) in %d allocations in function '%s' of '%s'\n",
00447 cur->len, cur->cache_len, cur->count, cur->fn, fn);
00448 } else {
00449 ast_cli(a->fd, "%10d bytes (%10d cache) in %d allocations in file '%s'\n",
00450 cur->len, cur->cache_len, cur->count, cur->fn);
00451 }
00452 } else {
00453 if (fn) {
00454 ast_cli(a->fd, "%10d bytes in %d allocations in function '%s' of '%s'\n",
00455 cur->len, cur->count, cur->fn, fn);
00456 } else {
00457 ast_cli(a->fd, "%10d bytes in %d allocations in file '%s'\n",
00458 cur->len, cur->count, cur->fn);
00459 }
00460 }
00461 }
00462
00463 if (cache_len)
00464 ast_cli(a->fd, "%d bytes allocated (%d in caches) in %d allocations\n", len, cache_len, count);
00465 else
00466 ast_cli(a->fd, "%d bytes allocated in %d allocations\n", len, count);
00467
00468 return CLI_SUCCESS;
00469 }
00470
00471 static struct ast_cli_entry cli_memory[] = {
00472 AST_CLI_DEFINE(handle_memory_show, "Display outstanding memory allocations"),
00473 AST_CLI_DEFINE(handle_memory_show_summary, "Summarize outstanding memory allocations"),
00474 };
00475
00476 void __ast_mm_init(void)
00477 {
00478 char filename[PATH_MAX];
00479 size_t pad = sizeof(struct ast_region) - offsetof(struct ast_region, data);
00480
00481 if (pad) {
00482 ast_log(LOG_ERROR, "struct ast_region has %d bytes of padding! This must be eliminated for low-fence checking to work properly!\n", (int) pad);
00483 }
00484
00485 ast_cli_register_multiple(cli_memory, sizeof(cli_memory) / sizeof(struct ast_cli_entry));
00486
00487 snprintf(filename, sizeof(filename), "%s/mmlog", ast_config_AST_LOG_DIR);
00488
00489 ast_verb(1, "Asterisk Malloc Debugger Started (see %s))\n", filename);
00490
00491 if ((mmlog = fopen(filename, "a+"))) {
00492 fprintf(mmlog, "%ld - New session\n", (long)time(NULL));
00493 fflush(mmlog);
00494 }
00495 }
00496
00497 #endif