1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558 |
- /*
- * Asterisk -- An open source telephony toolkit.
- *
- * Copyright (C) 1999 - 2012, Digium, Inc.
- *
- * Mark Spencer <markster@digium.com>
- *
- * See http://www.asterisk.org for more information about
- * the Asterisk project. Please do not directly contact
- * any of the maintainers of this project for assistance;
- * the project provides a web site, mailing lists and IRC
- * channels for your use.
- *
- * This program is free software, distributed under the terms of
- * the GNU General Public License Version 2. See the LICENSE file
- * at the top of the source tree.
- */
- /*! \file
- *
- * \brief Memory Management
- *
- * \author Mark Spencer <markster@digium.com>
- * \author Richard Mudgett <rmudgett@digium.com>
- */
- /*** MODULEINFO
- <support_level>core</support_level>
- ***/
- #include "asterisk.h"
- #if defined(__AST_DEBUG_MALLOC)
- ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
- #include "asterisk/paths.h" /* use ast_config_AST_LOG_DIR */
- #include <stddef.h>
- #include <time.h>
- #include "asterisk/cli.h"
- #include "asterisk/lock.h"
- #include "asterisk/strings.h"
- #include "asterisk/unaligned.h"
- #include "asterisk/backtrace.h"
- /*!
- * The larger the number the faster memory can be freed.
- * However, more memory then is used for the regions[] hash
- * table.
- */
- #define SOME_PRIME 1567
- enum func_type {
- FUNC_CALLOC = 1,
- FUNC_MALLOC,
- FUNC_REALLOC,
- FUNC_STRDUP,
- FUNC_STRNDUP,
- FUNC_VASPRINTF,
- FUNC_ASPRINTF
- };
- /* Undefine all our macros */
- #undef malloc
- #undef calloc
- #undef realloc
- #undef strdup
- #undef strndup
- #undef free
- #undef vasprintf
- #undef asprintf
- #define FENCE_MAGIC 0xfeedbabe /*!< Allocated memory high/low fence overwrite check. */
- #define FREED_MAGIC 0xdeaddead /*!< Freed memory wipe filler. */
- #define MALLOC_FILLER 0x55 /*!< Malloced memory filler. Must not be zero. */
- static FILE *mmlog;
- struct ast_region {
- AST_LIST_ENTRY(ast_region) node;
- struct ast_bt *bt;
- size_t len;
- unsigned int cache; /* region was allocated as part of a cache pool */
- unsigned int lineno;
- enum func_type which;
- char file[64];
- char func[40];
- /*!
- * \brief Lower guard fence.
- *
- * \note Must be right before data[].
- *
- * \note Padding between fence and data[] is irrelevent because
- * data[] is used to fill in the lower fence check value and not
- * the fence member. The fence member is to ensure that there
- * is space reserved for the fence check value.
- */
- unsigned int fence;
- /*!
- * \brief Location of the requested malloc block to return.
- *
- * \note Must have the same alignment that malloc returns.
- * i.e., It is suitably aligned for any kind of varible.
- */
- unsigned char data[0] __attribute__((aligned));
- };
- /*! Hash table of lists of active allocated memory regions. */
- static struct ast_region *regions[SOME_PRIME];
- /*! Number of freed regions to keep around to delay actually freeing them. */
- #define FREED_MAX_COUNT 1500
- /*! Maximum size of a minnow block */
- #define MINNOWS_MAX_SIZE 50
- struct ast_freed_regions {
- /*! Memory regions that have been freed. */
- struct ast_region *regions[FREED_MAX_COUNT];
- /*! Next index into freed regions[] to use. */
- int index;
- };
- /*! Large memory blocks that have been freed. */
- static struct ast_freed_regions whales;
- /*! Small memory blocks that have been freed. */
- static struct ast_freed_regions minnows;
- enum summary_opts {
- /*! No summary at exit. */
- SUMMARY_OFF,
- /*! Bit set if summary by line at exit. */
- SUMMARY_BY_LINE = (1 << 0),
- /*! Bit set if summary by function at exit. */
- SUMMARY_BY_FUNC = (1 << 1),
- /*! Bit set if summary by file at exit. */
- SUMMARY_BY_FILE = (1 << 2),
- };
- /*! Summary options of unfreed regions at exit. */
- static enum summary_opts atexit_summary;
- /*! Nonzero if the unfreed regions are listed at exit. */
- static int atexit_list;
- /*! Nonzero if the memory allocation backtrace is enabled. */
- static int backtrace_enabled;
- #define HASH(a) (((unsigned long)(a)) % ARRAY_LEN(regions))
- /*! Tracking this mutex will cause infinite recursion, as the mutex tracking
- * code allocates memory */
- AST_MUTEX_DEFINE_STATIC_NOTRACKING(reglock);
- #define astmm_log(...) \
- do { \
- fprintf(stderr, __VA_ARGS__); \
- if (mmlog) { \
- fprintf(mmlog, __VA_ARGS__); \
- fflush(mmlog); \
- } \
- } while (0)
- void *ast_std_malloc(size_t size)
- {
- return malloc(size);
- }
- void *ast_std_calloc(size_t nmemb, size_t size)
- {
- return calloc(nmemb, size);
- }
- void *ast_std_realloc(void *ptr, size_t size)
- {
- return realloc(ptr, size);
- }
- void ast_std_free(void *ptr)
- {
- free(ptr);
- }
- void ast_free_ptr(void *ptr)
- {
- ast_free(ptr);
- }
- static void print_backtrace(struct ast_bt *bt)
- {
- int i = 0;
- struct ast_vector_string *strings;
- if (!bt) {
- return;
- }
- if ((strings = ast_bt_get_symbols(bt->addresses, bt->num_frames))) {
- astmm_log("Memory allocation backtrace:\n");
- for (i = 3; i < AST_VECTOR_SIZE(strings) - 2; i++) {
- astmm_log("#%d: %s\n", i - 3, AST_VECTOR_GET(strings, i));
- }
- ast_bt_free_symbols(strings);
- }
- }
- /*!
- * \internal
- *
- * \note If DO_CRASH is not defined then the function returns.
- *
- * \return Nothing
- */
- static void my_do_crash(void)
- {
- /*
- * Give the logger a chance to get the message out, just in case
- * we abort(), or Asterisk crashes due to whatever problem just
- * happened.
- */
- usleep(1);
- ast_do_crash();
- }
- static void *__ast_alloc_region(size_t size, const enum func_type which, const char *file, int lineno, const char *func, unsigned int cache)
- {
- struct ast_region *reg;
- unsigned int *fence;
- int hash;
- DEBUG_CHAOS_RETURN(DEBUG_CHAOS_ALLOC_CHANCE, NULL);
- if (!(reg = malloc(size + sizeof(*reg) + sizeof(*fence)))) {
- astmm_log("Memory Allocation Failure - '%d' bytes at %s %s() line %d\n",
- (int) size, file, func, lineno);
- return NULL;
- }
- reg->len = size;
- reg->cache = cache;
- reg->lineno = lineno;
- reg->which = which;
- reg->bt = backtrace_enabled ? ast_bt_create() : NULL;
- ast_copy_string(reg->file, file, sizeof(reg->file));
- ast_copy_string(reg->func, func, sizeof(reg->func));
- /*
- * Init lower fence.
- *
- * We use the bytes just preceeding reg->data and not reg->fence
- * because there is likely to be padding between reg->fence and
- * reg->data for reg->data alignment.
- */
- fence = (unsigned int *) (reg->data - sizeof(*fence));
- *fence = FENCE_MAGIC;
- /* Init higher fence. */
- fence = (unsigned int *) (reg->data + reg->len);
- put_unaligned_uint32(fence, FENCE_MAGIC);
- hash = HASH(reg->data);
- ast_mutex_lock(®lock);
- AST_LIST_NEXT(reg, node) = regions[hash];
- regions[hash] = reg;
- ast_mutex_unlock(®lock);
- return reg->data;
- }
- /*!
- * \internal
- * \brief Wipe the region payload data with a known value.
- *
- * \param reg Region block to be wiped.
- *
- * \return Nothing
- */
- static void region_data_wipe(struct ast_region *reg)
- {
- void *end;
- unsigned int *pos;
- /*
- * Wipe the lower fence, the payload, and whatever amount of the
- * higher fence that falls into alignment with the payload.
- */
- end = reg->data + reg->len;
- for (pos = ®->fence; (void *) pos <= end; ++pos) {
- *pos = FREED_MAGIC;
- }
- }
- /*!
- * \internal
- * \brief Check the region payload data for memory corruption.
- *
- * \param reg Region block to be checked.
- *
- * \return Nothing
- */
- static void region_data_check(struct ast_region *reg)
- {
- void *end;
- unsigned int *pos;
- /*
- * Check the lower fence, the payload, and whatever amount of
- * the higher fence that falls into alignment with the payload.
- */
- end = reg->data + reg->len;
- for (pos = ®->fence; (void *) pos <= end; ++pos) {
- if (*pos != FREED_MAGIC) {
- astmm_log("WARNING: Memory corrupted after free of %p allocated at %s %s() line %d\n",
- reg->data, reg->file, reg->func, reg->lineno);
- print_backtrace(reg->bt);
- my_do_crash();
- break;
- }
- }
- }
- /*!
- * \internal
- * \brief Flush the circular array of freed regions.
- *
- * \param freed Already freed region blocks storage.
- *
- * \return Nothing
- */
- static void freed_regions_flush(struct ast_freed_regions *freed)
- {
- int idx;
- struct ast_region *old;
- ast_mutex_lock(®lock);
- for (idx = 0; idx < ARRAY_LEN(freed->regions); ++idx) {
- old = freed->regions[idx];
- freed->regions[idx] = NULL;
- if (old) {
- region_data_check(old);
- free(old);
- }
- }
- freed->index = 0;
- ast_mutex_unlock(®lock);
- }
- /*!
- * \internal
- * \brief Delay freeing a region block.
- *
- * \param freed Already freed region blocks storage.
- * \param reg Region block to be freed.
- *
- * \return Nothing
- */
- static void region_free(struct ast_freed_regions *freed, struct ast_region *reg)
- {
- struct ast_region *old;
- region_data_wipe(reg);
- ast_mutex_lock(®lock);
- old = freed->regions[freed->index];
- freed->regions[freed->index] = reg;
- ++freed->index;
- if (ARRAY_LEN(freed->regions) <= freed->index) {
- freed->index = 0;
- }
- ast_mutex_unlock(®lock);
- if (old) {
- region_data_check(old);
- old->bt = ast_bt_destroy(old->bt);
- free(old);
- }
- }
- /*!
- * \internal
- * \brief Remove a region from the active regions.
- *
- * \param ptr Region payload data pointer.
- *
- * \retval region on success.
- * \retval NULL if not found.
- */
- static struct ast_region *region_remove(void *ptr)
- {
- int hash;
- struct ast_region *reg;
- struct ast_region *prev = NULL;
- hash = HASH(ptr);
- ast_mutex_lock(®lock);
- for (reg = regions[hash]; reg; reg = AST_LIST_NEXT(reg, node)) {
- if (reg->data == ptr) {
- if (prev) {
- AST_LIST_NEXT(prev, node) = AST_LIST_NEXT(reg, node);
- } else {
- regions[hash] = AST_LIST_NEXT(reg, node);
- }
- break;
- }
- prev = reg;
- }
- ast_mutex_unlock(®lock);
- return reg;
- }
- /*!
- * \internal
- * \brief Check the fences of a region.
- *
- * \param reg Region block to check.
- *
- * \return Nothing
- */
- static void region_check_fences(struct ast_region *reg)
- {
- unsigned int *fence;
- /*
- * We use the bytes just preceeding reg->data and not reg->fence
- * because there is likely to be padding between reg->fence and
- * reg->data for reg->data alignment.
- */
- fence = (unsigned int *) (reg->data - sizeof(*fence));
- if (*fence != FENCE_MAGIC) {
- astmm_log("WARNING: Low fence violation of %p allocated at %s %s() line %d\n",
- reg->data, reg->file, reg->func, reg->lineno);
- print_backtrace(reg->bt);
- my_do_crash();
- }
- fence = (unsigned int *) (reg->data + reg->len);
- if (get_unaligned_uint32(fence) != FENCE_MAGIC) {
- astmm_log("WARNING: High fence violation of %p allocated at %s %s() line %d\n",
- reg->data, reg->file, reg->func, reg->lineno);
- print_backtrace(reg->bt);
- my_do_crash();
- }
- }
- /*!
- * \internal
- * \brief Check the fences of all regions currently allocated.
- *
- * \return Nothing
- */
- static void regions_check_all_fences(void)
- {
- int idx;
- struct ast_region *reg;
- ast_mutex_lock(®lock);
- for (idx = 0; idx < ARRAY_LEN(regions); ++idx) {
- for (reg = regions[idx]; reg; reg = AST_LIST_NEXT(reg, node)) {
- region_check_fences(reg);
- }
- }
- ast_mutex_unlock(®lock);
- }
- static void __ast_free_region(void *ptr, const char *file, int lineno, const char *func)
- {
- struct ast_region *reg;
- if (!ptr) {
- return;
- }
- reg = region_remove(ptr);
- if (reg) {
- region_check_fences(reg);
- if (reg->len <= MINNOWS_MAX_SIZE) {
- region_free(&minnows, reg);
- } else {
- region_free(&whales, reg);
- }
- } else {
- /*
- * This memory region is not registered. It could be because of
- * a double free or the memory block was not allocated by the
- * malloc debug code.
- */
- astmm_log("WARNING: Freeing unregistered memory %p by %s %s() line %d\n",
- ptr, file, func, lineno);
- my_do_crash();
- }
- }
- void *__ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func)
- {
- void *ptr;
- ptr = __ast_alloc_region(size * nmemb, FUNC_CALLOC, file, lineno, func, 0);
- if (ptr) {
- memset(ptr, 0, size * nmemb);
- }
- return ptr;
- }
- void *__ast_calloc_cache(size_t nmemb, size_t size, const char *file, int lineno, const char *func)
- {
- void *ptr;
- ptr = __ast_alloc_region(size * nmemb, FUNC_CALLOC, file, lineno, func, 1);
- if (ptr) {
- memset(ptr, 0, size * nmemb);
- }
- return ptr;
- }
- void *__ast_malloc(size_t size, const char *file, int lineno, const char *func)
- {
- void *ptr;
- ptr = __ast_alloc_region(size, FUNC_MALLOC, file, lineno, func, 0);
- if (ptr) {
- /* Make sure that the malloced memory is not zero. */
- memset(ptr, MALLOC_FILLER, size);
- }
- return ptr;
- }
- void __ast_free(void *ptr, const char *file, int lineno, const char *func)
- {
- __ast_free_region(ptr, file, lineno, func);
- }
- /*!
- * \note reglock must be locked before calling.
- */
- static struct ast_region *region_find(void *ptr)
- {
- int hash;
- struct ast_region *reg;
- hash = HASH(ptr);
- for (reg = regions[hash]; reg; reg = AST_LIST_NEXT(reg, node)) {
- if (reg->data == ptr) {
- break;
- }
- }
- return reg;
- }
- void *__ast_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func)
- {
- size_t len;
- struct ast_region *found;
- void *new_mem;
- if (ptr) {
- ast_mutex_lock(®lock);
- found = region_find(ptr);
- if (!found) {
- ast_mutex_unlock(®lock);
- astmm_log("WARNING: Realloc of unregistered memory %p by %s %s() line %d\n",
- ptr, file, func, lineno);
- my_do_crash();
- return NULL;
- }
- len = found->len;
- ast_mutex_unlock(®lock);
- } else {
- found = NULL;
- len = 0;
- }
- if (!size) {
- __ast_free_region(ptr, file, lineno, func);
- return NULL;
- }
- new_mem = __ast_alloc_region(size, FUNC_REALLOC, file, lineno, func, 0);
- if (new_mem) {
- if (found) {
- /* Copy the old data to the new malloced memory. */
- if (size <= len) {
- memcpy(new_mem, ptr, size);
- } else {
- memcpy(new_mem, ptr, len);
- /* Make sure that the added memory is not zero. */
- memset(new_mem + len, MALLOC_FILLER, size - len);
- }
- __ast_free_region(ptr, file, lineno, func);
- } else {
- /* Make sure that the malloced memory is not zero. */
- memset(new_mem, MALLOC_FILLER, size);
- }
- }
- return new_mem;
- }
- char *__ast_strdup(const char *s, const char *file, int lineno, const char *func)
- {
- size_t len;
- void *ptr;
- if (!s)
- return NULL;
- len = strlen(s) + 1;
- if ((ptr = __ast_alloc_region(len, FUNC_STRDUP, file, lineno, func, 0)))
- strcpy(ptr, s);
- return ptr;
- }
- char *__ast_strndup(const char *s, size_t n, const char *file, int lineno, const char *func)
- {
- size_t len;
- char *ptr;
- if (!s) {
- return NULL;
- }
- len = strnlen(s, n);
- if ((ptr = __ast_alloc_region(len + 1, FUNC_STRNDUP, file, lineno, func, 0))) {
- memcpy(ptr, s, len);
- ptr[len] = '\0';
- }
- return ptr;
- }
- int __ast_asprintf(const char *file, int lineno, const char *func, char **strp, const char *fmt, ...)
- {
- int size;
- va_list ap, ap2;
- char s;
- *strp = NULL;
- va_start(ap, fmt);
- va_copy(ap2, ap);
- size = vsnprintf(&s, 1, fmt, ap2);
- va_end(ap2);
- if (!(*strp = __ast_alloc_region(size + 1, FUNC_ASPRINTF, file, lineno, func, 0))) {
- va_end(ap);
- return -1;
- }
- vsnprintf(*strp, size + 1, fmt, ap);
- va_end(ap);
- return size;
- }
- int __ast_vasprintf(char **strp, const char *fmt, va_list ap, const char *file, int lineno, const char *func)
- {
- int size;
- va_list ap2;
- char s;
- *strp = NULL;
- va_copy(ap2, ap);
- size = vsnprintf(&s, 1, fmt, ap2);
- va_end(ap2);
- if (!(*strp = __ast_alloc_region(size + 1, FUNC_VASPRINTF, file, lineno, func, 0))) {
- return -1;
- }
- vsnprintf(*strp, size + 1, fmt, ap);
- return size;
- }
- /*!
- * \internal
- * \brief Count the number of bytes in the specified freed region.
- *
- * \param freed Already freed region blocks storage.
- *
- * \note reglock must be locked before calling.
- *
- * \return Number of bytes in freed region.
- */
- static size_t freed_regions_size(struct ast_freed_regions *freed)
- {
- size_t total_len = 0;
- int idx;
- struct ast_region *old;
- for (idx = 0; idx < ARRAY_LEN(freed->regions); ++idx) {
- old = freed->regions[idx];
- if (old) {
- total_len += old->len;
- }
- }
- return total_len;
- }
- static char *handle_memory_atexit_list(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
- {
- switch (cmd) {
- case CLI_INIT:
- e->command = "memory atexit list {on|off}";
- e->usage =
- "Usage: memory atexit list {on|off}\n"
- " Enable dumping a list of still allocated memory segments at exit.\n";
- return NULL;
- case CLI_GENERATE:
- return NULL;
- }
- if (a->argc != 4) {
- return CLI_SHOWUSAGE;
- }
- if (ast_true(a->argv[3])) {
- atexit_list = 1;
- } else if (ast_false(a->argv[3])) {
- atexit_list = 0;
- } else {
- return CLI_SHOWUSAGE;
- }
- ast_cli(a->fd, "The atexit list is: %s\n", atexit_list ? "On" : "Off");
- return CLI_SUCCESS;
- }
- static char *handle_memory_atexit_summary(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
- {
- char buf[80];
- switch (cmd) {
- case CLI_INIT:
- e->command = "memory atexit summary {off|byline|byfunc|byfile}";
- e->usage =
- "Usage: memory atexit summary {off|byline|byfunc|byfile}\n"
- " Summary of still allocated memory segments at exit options.\n"
- " off - Disable at exit summary.\n"
- " byline - Enable at exit summary by file line number.\n"
- " byfunc - Enable at exit summary by function name.\n"
- " byfile - Enable at exit summary by file.\n"
- "\n"
- " Note: byline, byfunc, and byfile are cumulative enables.\n";
- return NULL;
- case CLI_GENERATE:
- return NULL;
- }
- if (a->argc != 4) {
- return CLI_SHOWUSAGE;
- }
- if (ast_false(a->argv[3])) {
- atexit_summary = SUMMARY_OFF;
- } else if (!strcasecmp(a->argv[3], "byline")) {
- atexit_summary |= SUMMARY_BY_LINE;
- } else if (!strcasecmp(a->argv[3], "byfunc")) {
- atexit_summary |= SUMMARY_BY_FUNC;
- } else if (!strcasecmp(a->argv[3], "byfile")) {
- atexit_summary |= SUMMARY_BY_FILE;
- } else {
- return CLI_SHOWUSAGE;
- }
- if (atexit_summary) {
- buf[0] = '\0';
- if (atexit_summary & SUMMARY_BY_LINE) {
- strcat(buf, "byline");
- }
- if (atexit_summary & SUMMARY_BY_FUNC) {
- if (buf[0]) {
- strcat(buf, " | ");
- }
- strcat(buf, "byfunc");
- }
- if (atexit_summary & SUMMARY_BY_FILE) {
- if (buf[0]) {
- strcat(buf, " | ");
- }
- strcat(buf, "byfile");
- }
- } else {
- strcpy(buf, "Off");
- }
- ast_cli(a->fd, "The atexit summary is: %s\n", buf);
- return CLI_SUCCESS;
- }
- /*!
- * \internal
- * \brief Common summary output at the end of the memory show commands.
- *
- * \param fd CLI output file descriptor.
- * \param whales_len Accumulated size of free large allocations.
- * \param minnows_len Accumulated size of free small allocations.
- * \param total_len Accumulated size of all current allocations.
- * \param selected_len Accumulated size of the selected allocations.
- * \param cache_len Accumulated size of the allocations that are part of a cache.
- * \param count Number of selected allocations.
- *
- * \return Nothing
- */
- static void print_memory_show_common_stats(int fd,
- unsigned int whales_len,
- unsigned int minnows_len,
- unsigned int total_len,
- unsigned int selected_len,
- unsigned int cache_len,
- unsigned int count)
- {
- if (cache_len) {
- ast_cli(fd, "%10u bytes allocated (%u in caches) in %u selected allocations\n\n",
- selected_len, cache_len, count);
- } else {
- ast_cli(fd, "%10u bytes allocated in %u selected allocations\n\n",
- selected_len, count);
- }
- ast_cli(fd, "%10u bytes in all allocations\n", total_len);
- ast_cli(fd, "%10u bytes in deferred free large allocations\n", whales_len);
- ast_cli(fd, "%10u bytes in deferred free small allocations\n", minnows_len);
- ast_cli(fd, "%10u bytes in deferred free allocations\n",
- whales_len + minnows_len);
- ast_cli(fd, "%10u bytes in all allocations and deferred free allocations\n",
- total_len + whales_len + minnows_len);
- }
- static char *handle_memory_show_allocations(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
- {
- const char *fn = NULL;
- struct ast_region *reg;
- unsigned int idx;
- unsigned int whales_len;
- unsigned int minnows_len;
- unsigned int total_len = 0;
- unsigned int selected_len = 0;
- unsigned int cache_len = 0;
- unsigned int count = 0;
- switch (cmd) {
- case CLI_INIT:
- e->command = "memory show allocations";
- e->usage =
- "Usage: memory show allocations [<file>|anomalies]\n"
- " Dumps a list of segments of allocated memory.\n"
- " Defaults to listing all memory allocations.\n"
- " <file> - Restricts output to memory allocated by the file.\n"
- " anomalies - Only check for fence violations.\n";
- return NULL;
- case CLI_GENERATE:
- return NULL;
- }
- if (a->argc == 4) {
- fn = a->argv[3];
- } else if (a->argc != 3) {
- return CLI_SHOWUSAGE;
- }
- /* Look for historical misspelled option as well. */
- if (fn && (!strcasecmp(fn, "anomalies") || !strcasecmp(fn, "anomolies"))) {
- regions_check_all_fences();
- ast_cli(a->fd, "Anomaly check complete.\n");
- return CLI_SUCCESS;
- }
- ast_mutex_lock(®lock);
- for (idx = 0; idx < ARRAY_LEN(regions); ++idx) {
- for (reg = regions[idx]; reg; reg = AST_LIST_NEXT(reg, node)) {
- total_len += reg->len;
- if (fn && strcasecmp(fn, reg->file)) {
- continue;
- }
- region_check_fences(reg);
- ast_cli(a->fd, "%10u bytes allocated%s by %20s() line %5u of %s\n",
- (unsigned int) reg->len, reg->cache ? " (cache)" : "",
- reg->func, reg->lineno, reg->file);
- selected_len += reg->len;
- if (reg->cache) {
- cache_len += reg->len;
- }
- ++count;
- }
- }
- whales_len = freed_regions_size(&whales);
- minnows_len = freed_regions_size(&minnows);
- ast_mutex_unlock(®lock);
- print_memory_show_common_stats(a->fd,
- whales_len, minnows_len, total_len,
- selected_len, cache_len, count);
- return CLI_SUCCESS;
- }
- static char *handle_memory_show_summary(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
- {
- #define my_max(a, b) ((a) >= (b) ? (a) : (b))
- const char *fn = NULL;
- int idx;
- int cmp;
- struct ast_region *reg;
- unsigned int whales_len;
- unsigned int minnows_len;
- unsigned int total_len = 0;
- unsigned int selected_len = 0;
- unsigned int cache_len = 0;
- unsigned int count = 0;
- struct file_summary {
- struct file_summary *next;
- unsigned int len;
- unsigned int cache_len;
- unsigned int count;
- unsigned int lineno;
- char name[my_max(sizeof(reg->file), sizeof(reg->func))];
- } *list = NULL, *cur, **prev;
- switch (cmd) {
- case CLI_INIT:
- e->command = "memory show summary";
- e->usage =
- "Usage: memory show summary [<file>]\n"
- " Summarizes heap memory allocations by file, or optionally\n"
- " by line if a file is specified.\n";
- return NULL;
- case CLI_GENERATE:
- return NULL;
- }
- if (a->argc == 4) {
- fn = a->argv[3];
- } else if (a->argc != 3) {
- return CLI_SHOWUSAGE;
- }
- ast_mutex_lock(®lock);
- for (idx = 0; idx < ARRAY_LEN(regions); ++idx) {
- for (reg = regions[idx]; reg; reg = AST_LIST_NEXT(reg, node)) {
- total_len += reg->len;
- if (fn) {
- if (strcasecmp(fn, reg->file)) {
- continue;
- }
- /* Sort list by func/lineno. Find existing or place to insert. */
- for (prev = &list; (cur = *prev); prev = &cur->next) {
- cmp = strcmp(cur->name, reg->func);
- if (cmp < 0) {
- continue;
- }
- if (cmp > 0) {
- /* Insert before current */
- cur = NULL;
- break;
- }
- cmp = cur->lineno - reg->lineno;
- if (cmp < 0) {
- continue;
- }
- if (cmp > 0) {
- /* Insert before current */
- cur = NULL;
- }
- break;
- }
- } else {
- /* Sort list by filename. Find existing or place to insert. */
- for (prev = &list; (cur = *prev); prev = &cur->next) {
- cmp = strcmp(cur->name, reg->file);
- if (cmp < 0) {
- continue;
- }
- if (cmp > 0) {
- /* Insert before current */
- cur = NULL;
- }
- break;
- }
- }
- if (!cur) {
- cur = ast_alloca(sizeof(*cur));
- memset(cur, 0, sizeof(*cur));
- cur->lineno = reg->lineno;
- ast_copy_string(cur->name, fn ? reg->func : reg->file, sizeof(cur->name));
- cur->next = *prev;
- *prev = cur;
- }
- cur->len += reg->len;
- if (reg->cache) {
- cur->cache_len += reg->len;
- }
- ++cur->count;
- }
- }
- whales_len = freed_regions_size(&whales);
- minnows_len = freed_regions_size(&minnows);
- ast_mutex_unlock(®lock);
- /* Dump the whole list */
- for (cur = list; cur; cur = cur->next) {
- selected_len += cur->len;
- cache_len += cur->cache_len;
- count += cur->count;
- if (cur->cache_len) {
- if (fn) {
- ast_cli(a->fd, "%10u bytes (%10u cache) in %10u allocations by %20s() line %5u of %s\n",
- cur->len, cur->cache_len, cur->count, cur->name, cur->lineno, fn);
- } else {
- ast_cli(a->fd, "%10u bytes (%10u cache) in %10u allocations in file %s\n",
- cur->len, cur->cache_len, cur->count, cur->name);
- }
- } else {
- if (fn) {
- ast_cli(a->fd, "%10u bytes in %10u allocations by %20s() line %5u of %s\n",
- cur->len, cur->count, cur->name, cur->lineno, fn);
- } else {
- ast_cli(a->fd, "%10u bytes in %10u allocations in file %s\n",
- cur->len, cur->count, cur->name);
- }
- }
- }
- print_memory_show_common_stats(a->fd,
- whales_len, minnows_len, total_len,
- selected_len, cache_len, count);
- return CLI_SUCCESS;
- }
- static char *handle_memory_backtrace(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
- {
- switch (cmd) {
- case CLI_INIT:
- e->command = "memory backtrace {on|off}";
- e->usage =
- "Usage: memory backtrace {on|off}\n"
- " Enable dumping an allocation backtrace with memory diagnostics.\n"
- " Note that saving the backtrace data for each allocation\n"
- " can be CPU intensive.\n";
- return NULL;
- case CLI_GENERATE:
- return NULL;
- }
- if (a->argc != 3) {
- return CLI_SHOWUSAGE;
- }
- if (ast_true(a->argv[2])) {
- backtrace_enabled = 1;
- } else if (ast_false(a->argv[2])) {
- backtrace_enabled = 0;
- } else {
- return CLI_SHOWUSAGE;
- }
- ast_cli(a->fd, "The memory backtrace is: %s\n", backtrace_enabled ? "On" : "Off");
- return CLI_SUCCESS;
- }
- static struct ast_cli_entry cli_memory[] = {
- AST_CLI_DEFINE(handle_memory_atexit_list, "Enable memory allocations not freed at exit list."),
- AST_CLI_DEFINE(handle_memory_atexit_summary, "Enable memory allocations not freed at exit summary."),
- AST_CLI_DEFINE(handle_memory_show_allocations, "Display outstanding memory allocations"),
- AST_CLI_DEFINE(handle_memory_show_summary, "Summarize outstanding memory allocations"),
- AST_CLI_DEFINE(handle_memory_backtrace, "Enable dumping an allocation backtrace with memory diagnostics."),
- };
- AST_LIST_HEAD_NOLOCK(region_list, ast_region);
- /*!
- * \internal
- * \brief Convert the allocated regions hash table to a list.
- *
- * \param list Fill list with the allocated regions.
- *
- * \details
- * Take all allocated regions from the regions[] and put them
- * into the list.
- *
- * \note reglock must be locked before calling.
- *
- * \note This function is destructive to the regions[] lists.
- *
- * \return Length of list created.
- */
- static size_t mm_atexit_hash_list(struct region_list *list)
- {
- struct ast_region *reg;
- size_t total_length;
- int idx;
- total_length = 0;
- for (idx = 0; idx < ARRAY_LEN(regions); ++idx) {
- while ((reg = regions[idx])) {
- regions[idx] = AST_LIST_NEXT(reg, node);
- AST_LIST_NEXT(reg, node) = NULL;
- AST_LIST_INSERT_HEAD(list, reg, node);
- ++total_length;
- }
- }
- return total_length;
- }
- /*!
- * \internal
- * \brief Put the regions list into the allocated regions hash table.
- *
- * \param list List to put into the allocated regions hash table.
- *
- * \note reglock must be locked before calling.
- *
- * \return Nothing
- */
- static void mm_atexit_hash_restore(struct region_list *list)
- {
- struct ast_region *reg;
- int hash;
- while ((reg = AST_LIST_REMOVE_HEAD(list, node))) {
- hash = HASH(reg->data);
- AST_LIST_NEXT(reg, node) = regions[hash];
- regions[hash] = reg;
- }
- }
- /*!
- * \internal
- * \brief Sort regions comparision.
- *
- * \param left Region to compare.
- * \param right Region to compare.
- *
- * \retval <0 if left < right
- * \retval =0 if left == right
- * \retval >0 if left > right
- */
- static int mm_atexit_cmp(struct ast_region *left, struct ast_region *right)
- {
- int cmp;
- ptrdiff_t cmp_ptr;
- ssize_t cmp_size;
- /* Sort by filename. */
- cmp = strcmp(left->file, right->file);
- if (cmp) {
- return cmp;
- }
- /* Sort by line number. */
- cmp = left->lineno - right->lineno;
- if (cmp) {
- return cmp;
- }
- /* Sort by allocated size. */
- cmp_size = left->len - right->len;
- if (cmp_size) {
- if (cmp_size < 0) {
- return -1;
- }
- return 1;
- }
- /* Sort by allocated pointers just because. */
- cmp_ptr = left->data - right->data;
- if (cmp_ptr) {
- if (cmp_ptr < 0) {
- return -1;
- }
- return 1;
- }
- return 0;
- }
- /*!
- * \internal
- * \brief Merge the given sorted sublists into sorted order onto the end of the list.
- *
- * \param list Merge sublists onto this list.
- * \param sub1 First sublist to merge.
- * \param sub2 Second sublist to merge.
- *
- * \return Nothing
- */
- static void mm_atexit_list_merge(struct region_list *list, struct region_list *sub1, struct region_list *sub2)
- {
- struct ast_region *reg;
- for (;;) {
- if (AST_LIST_EMPTY(sub1)) {
- /* The remaining sublist goes onto the list. */
- AST_LIST_APPEND_LIST(list, sub2, node);
- break;
- }
- if (AST_LIST_EMPTY(sub2)) {
- /* The remaining sublist goes onto the list. */
- AST_LIST_APPEND_LIST(list, sub1, node);
- break;
- }
- if (mm_atexit_cmp(AST_LIST_FIRST(sub1), AST_LIST_FIRST(sub2)) <= 0) {
- reg = AST_LIST_REMOVE_HEAD(sub1, node);
- } else {
- reg = AST_LIST_REMOVE_HEAD(sub2, node);
- }
- AST_LIST_INSERT_TAIL(list, reg, node);
- }
- }
- /*!
- * \internal
- * \brief Take sublists off of the given list.
- *
- * \param list Source list to remove sublists from the beginning of list.
- * \param sub Array of sublists to fill. (Lists are empty on entry.)
- * \param num_lists Number of lists to remove from the source list.
- * \param size Size of the sublists to remove.
- * \param remaining Remaining number of elements on the source list.
- *
- * \return Nothing
- */
- static void mm_atexit_list_split(struct region_list *list, struct region_list sub[], size_t num_lists, size_t size, size_t *remaining)
- {
- int idx;
- for (idx = 0; idx < num_lists; ++idx) {
- size_t count;
- if (*remaining < size) {
- /* The remaining source list goes onto the sublist. */
- AST_LIST_APPEND_LIST(&sub[idx], list, node);
- *remaining = 0;
- break;
- }
- /* Take a sublist off the beginning of the source list. */
- *remaining -= size;
- for (count = size; count--;) {
- struct ast_region *reg;
- reg = AST_LIST_REMOVE_HEAD(list, node);
- AST_LIST_INSERT_TAIL(&sub[idx], reg, node);
- }
- }
- }
- /*!
- * \internal
- * \brief Sort the regions list using mergesort.
- *
- * \param list Allocated regions list to sort.
- * \param length Length of the list.
- *
- * \return Nothing
- */
- static void mm_atexit_list_sort(struct region_list *list, size_t length)
- {
- /*! Semi-sorted merged list. */
- struct region_list merged = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
- /*! Sublists to merge. (Can only merge two sublists at this time.) */
- struct region_list sub[2] = {
- AST_LIST_HEAD_NOLOCK_INIT_VALUE,
- AST_LIST_HEAD_NOLOCK_INIT_VALUE
- };
- /*! Sublist size. */
- size_t size = 1;
- /*! Remaining elements in the list. */
- size_t remaining;
- /*! Number of sublist merge passes to process the list. */
- int passes;
- for (;;) {
- remaining = length;
- passes = 0;
- while (!AST_LIST_EMPTY(list)) {
- mm_atexit_list_split(list, sub, ARRAY_LEN(sub), size, &remaining);
- mm_atexit_list_merge(&merged, &sub[0], &sub[1]);
- ++passes;
- }
- AST_LIST_APPEND_LIST(list, &merged, node);
- if (passes <= 1) {
- /* The list is now sorted. */
- break;
- }
- /* Double the sublist size to remove for next round. */
- size <<= 1;
- }
- }
- /*!
- * \internal
- * \brief List all regions currently allocated.
- *
- * \param alloced regions list.
- *
- * \return Nothing
- */
- static void mm_atexit_regions_list(struct region_list *alloced)
- {
- struct ast_region *reg;
- AST_LIST_TRAVERSE(alloced, reg, node) {
- astmm_log("%s %s() line %u: %u bytes%s at %p\n",
- reg->file, reg->func, reg->lineno,
- (unsigned int) reg->len, reg->cache ? " (cache)" : "", reg->data);
- }
- }
- /*!
- * \internal
- * \brief Summarize all regions currently allocated.
- *
- * \param alloced Sorted regions list.
- *
- * \return Nothing
- */
- static void mm_atexit_regions_summary(struct region_list *alloced)
- {
- struct ast_region *reg;
- struct ast_region *next;
- struct {
- unsigned int count;
- unsigned int len;
- unsigned int cache_len;
- } by_line, by_func, by_file, total;
- by_line.count = 0;
- by_line.len = 0;
- by_line.cache_len = 0;
- by_func.count = 0;
- by_func.len = 0;
- by_func.cache_len = 0;
- by_file.count = 0;
- by_file.len = 0;
- by_file.cache_len = 0;
- total.count = 0;
- total.len = 0;
- total.cache_len = 0;
- AST_LIST_TRAVERSE(alloced, reg, node) {
- next = AST_LIST_NEXT(reg, node);
- ++by_line.count;
- by_line.len += reg->len;
- if (reg->cache) {
- by_line.cache_len += reg->len;
- }
- if (next && !strcmp(reg->file, next->file) && reg->lineno == next->lineno) {
- continue;
- }
- if (atexit_summary & SUMMARY_BY_LINE) {
- if (by_line.cache_len) {
- astmm_log("%10u bytes (%u in caches) in %u allocations. %s %s() line %u\n",
- by_line.len, by_line.cache_len, by_line.count, reg->file, reg->func, reg->lineno);
- } else {
- astmm_log("%10u bytes in %5u allocations. %s %s() line %u\n",
- by_line.len, by_line.count, reg->file, reg->func, reg->lineno);
- }
- }
- by_func.count += by_line.count;
- by_func.len += by_line.len;
- by_func.cache_len += by_line.cache_len;
- by_line.count = 0;
- by_line.len = 0;
- by_line.cache_len = 0;
- if (next && !strcmp(reg->file, next->file) && !strcmp(reg->func, next->func)) {
- continue;
- }
- if (atexit_summary & SUMMARY_BY_FUNC) {
- if (by_func.cache_len) {
- astmm_log("%10u bytes (%u in caches) in %u allocations. %s %s()\n",
- by_func.len, by_func.cache_len, by_func.count, reg->file, reg->func);
- } else {
- astmm_log("%10u bytes in %5u allocations. %s %s()\n",
- by_func.len, by_func.count, reg->file, reg->func);
- }
- }
- by_file.count += by_func.count;
- by_file.len += by_func.len;
- by_file.cache_len += by_func.cache_len;
- by_func.count = 0;
- by_func.len = 0;
- by_func.cache_len = 0;
- if (next && !strcmp(reg->file, next->file)) {
- continue;
- }
- if (atexit_summary & SUMMARY_BY_FILE) {
- if (by_file.cache_len) {
- astmm_log("%10u bytes (%u in caches) in %u allocations. %s\n",
- by_file.len, by_file.cache_len, by_file.count, reg->file);
- } else {
- astmm_log("%10u bytes in %5u allocations. %s\n",
- by_file.len, by_file.count, reg->file);
- }
- }
- total.count += by_file.count;
- total.len += by_file.len;
- total.cache_len += by_file.cache_len;
- by_file.count = 0;
- by_file.len = 0;
- by_file.cache_len = 0;
- }
- if (total.cache_len) {
- astmm_log("%u bytes (%u in caches) in %u allocations.\n",
- total.len, total.cache_len, total.count);
- } else {
- astmm_log("%u bytes in %u allocations.\n", total.len, total.count);
- }
- }
- /*!
- * \internal
- * \brief Dump the memory allocations atexit.
- *
- * \note reglock must be locked before calling.
- *
- * \return Nothing
- */
- static void mm_atexit_dump(void)
- {
- struct region_list alloced_atexit = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
- size_t length;
- length = mm_atexit_hash_list(&alloced_atexit);
- if (!length) {
- /* Wow! This is amazing! */
- astmm_log("Exiting with all memory freed.\n");
- return;
- }
- mm_atexit_list_sort(&alloced_atexit, length);
- astmm_log("Exiting with the following memory not freed:\n");
- if (atexit_list) {
- mm_atexit_regions_list(&alloced_atexit);
- }
- if (atexit_summary) {
- mm_atexit_regions_summary(&alloced_atexit);
- }
- /*
- * Put the alloced list back into regions[].
- *
- * We have do this because we can get called before all other
- * threads have terminated.
- */
- mm_atexit_hash_restore(&alloced_atexit);
- }
- /*!
- * \internal
- * \return Nothing
- */
- static void mm_atexit_final(void)
- {
- FILE *log;
- /* Only wait if we want atexit allocation dumps. */
- if (atexit_list || atexit_summary) {
- fprintf(stderr, "Waiting 10 seconds to let other threads die.\n");
- sleep(10);
- }
- regions_check_all_fences();
- /* Flush all delayed memory free circular arrays. */
- freed_regions_flush(&whales);
- freed_regions_flush(&minnows);
- /* Peform atexit allocation dumps. */
- if (atexit_list || atexit_summary) {
- ast_mutex_lock(®lock);
- mm_atexit_dump();
- ast_mutex_unlock(®lock);
- }
- /* Close the log file. */
- log = mmlog;
- mmlog = NULL;
- if (log) {
- fclose(log);
- }
- }
- /*!
- * \brief Initialize malloc debug phase 1.
- *
- * \note Must be called first thing in main().
- *
- * \return Nothing
- */
- void __ast_mm_init_phase_1(void)
- {
- atexit(mm_atexit_final);
- }
- /*!
- * \internal
- * \return Nothing
- */
- static void mm_atexit_ast(void)
- {
- ast_cli_unregister_multiple(cli_memory, ARRAY_LEN(cli_memory));
- }
- /*!
- * \brief Initialize malloc debug phase 2.
- *
- * \return Nothing
- */
- void __ast_mm_init_phase_2(void)
- {
- char filename[PATH_MAX];
- ast_cli_register_multiple(cli_memory, ARRAY_LEN(cli_memory));
- snprintf(filename, sizeof(filename), "%s/mmlog", ast_config_AST_LOG_DIR);
- ast_verb(1, "Asterisk Malloc Debugger Started (see %s))\n", filename);
- mmlog = fopen(filename, "a+");
- if (mmlog) {
- fprintf(mmlog, "%ld - New session\n", (long) time(NULL));
- fflush(mmlog);
- } else {
- ast_log(LOG_ERROR, "Could not open malloc debug log file: %s\n", filename);
- }
- ast_register_cleanup(mm_atexit_ast);
- }
- #endif /* defined(__AST_DEBUG_MALLOC) */
|