|
|
|
@ -11,8 +11,6 @@
|
|
|
|
|
* the GNU General Public License
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __AST_DEBUG_MALLOC
|
|
|
|
|
|
|
|
|
|
#include <malloc.h>
|
|
|
|
@ -31,10 +29,10 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|
|
|
|
|
|
|
|
|
#define SOME_PRIME 563
|
|
|
|
|
|
|
|
|
|
#define FUNC_CALLOC 1
|
|
|
|
|
#define FUNC_MALLOC 2
|
|
|
|
|
#define FUNC_CALLOC 1
|
|
|
|
|
#define FUNC_MALLOC 2
|
|
|
|
|
#define FUNC_REALLOC 3
|
|
|
|
|
#define FUNC_STRDUP 4
|
|
|
|
|
#define FUNC_STRDUP 4
|
|
|
|
|
#define FUNC_STRNDUP 5
|
|
|
|
|
#define FUNC_VASPRINTF 6
|
|
|
|
|
|
|
|
|
@ -68,7 +66,7 @@ AST_MUTEX_DEFINE_STATIC(showmemorylock);
|
|
|
|
|
static inline void *__ast_alloc_region(size_t size, int which, const char *file, int lineno, const char *func)
|
|
|
|
|
{
|
|
|
|
|
struct ast_region *reg;
|
|
|
|
|
void *ptr=NULL;
|
|
|
|
|
void *ptr = NULL;
|
|
|
|
|
int hash;
|
|
|
|
|
reg = malloc(size + sizeof(struct ast_region));
|
|
|
|
|
ast_mutex_lock(®lock);
|
|
|
|
@ -104,7 +102,7 @@ static inline size_t __ast_sizeof_region(void *ptr)
|
|
|
|
|
|
|
|
|
|
ast_mutex_lock(®lock);
|
|
|
|
|
reg = regions[hash];
|
|
|
|
|
while(reg) {
|
|
|
|
|
while (reg) {
|
|
|
|
|
if (reg->data == ptr) {
|
|
|
|
|
len = reg->len;
|
|
|
|
|
break;
|
|
|
|
@ -121,13 +119,13 @@ static void __ast_free_region(void *ptr, const char *file, int lineno, const cha
|
|
|
|
|
struct ast_region *reg, *prev = NULL;
|
|
|
|
|
ast_mutex_lock(®lock);
|
|
|
|
|
reg = regions[hash];
|
|
|
|
|
while(reg) {
|
|
|
|
|
while (reg) {
|
|
|
|
|
if (reg->data == ptr) {
|
|
|
|
|
if (prev)
|
|
|
|
|
if (prev) {
|
|
|
|
|
prev->next = reg->next;
|
|
|
|
|
else
|
|
|
|
|
} else {
|
|
|
|
|
regions[hash] = reg->next;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
prev = reg;
|
|
|
|
@ -137,11 +135,9 @@ static void __ast_free_region(void *ptr, const char *file, int lineno, const cha
|
|
|
|
|
if (reg) {
|
|
|
|
|
free(reg);
|
|
|
|
|
} else {
|
|
|
|
|
fprintf(stderr, "WARNING: Freeing unused memory at %p, in %s of %s, line %d\n",
|
|
|
|
|
ptr, func, file, lineno);
|
|
|
|
|
fprintf(stderr, "WARNING: Freeing unused memory at %p, in %s of %s, line %d\n", ptr, func, file, lineno);
|
|
|
|
|
if (mmlog) {
|
|
|
|
|
fprintf(mmlog, "%ld - WARNING: Freeing unused memory at %p, in %s of %s, line %d\n", time(NULL),
|
|
|
|
|
ptr, func, file, lineno);
|
|
|
|
|
fprintf(mmlog, "%ld - WARNING: Freeing unused memory at %p, in %s of %s, line %d\n", time(NULL), ptr, func, file, lineno);
|
|
|
|
|
fflush(mmlog);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -169,15 +165,13 @@ void __ast_free(void *ptr, const char *file, int lineno, const char *func)
|
|
|
|
|
void *__ast_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func)
|
|
|
|
|
{
|
|
|
|
|
void *tmp;
|
|
|
|
|
size_t len=0;
|
|
|
|
|
size_t len = 0;
|
|
|
|
|
if (ptr) {
|
|
|
|
|
len = __ast_sizeof_region(ptr);
|
|
|
|
|
if (!len) {
|
|
|
|
|
fprintf(stderr, "WARNING: Realloc of unalloced memory at %p, in %s of %s, line %d\n",
|
|
|
|
|
ptr, func, file, lineno);
|
|
|
|
|
fprintf(stderr, "WARNING: Realloc of unalloced memory at %p, in %s of %s, line %d\n", ptr, func, file, lineno);
|
|
|
|
|
if (mmlog) {
|
|
|
|
|
fprintf(mmlog, "%ld - WARNING: Realloc of unalloced memory at %p, in %s of %s, line %d\n",
|
|
|
|
|
time(NULL), ptr, func, file, lineno);
|
|
|
|
|
fprintf(mmlog, "%ld - WARNING: Realloc of unalloced memory at %p, in %s of %s, line %d\n", time(NULL), ptr, func, file, lineno);
|
|
|
|
|
fflush(mmlog);
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
@ -232,10 +226,11 @@ int __ast_vasprintf(char **strp, const char *fmt, va_list ap, const char *file,
|
|
|
|
|
n = vsnprintf(*strp, size, fmt, ap);
|
|
|
|
|
if (n > -1 && n < size)
|
|
|
|
|
return n;
|
|
|
|
|
if (n > -1) /* glibc 2.1 */
|
|
|
|
|
if (n > -1) { /* glibc 2.1 */
|
|
|
|
|
size = n+1;
|
|
|
|
|
else /* glibc 2.0 */
|
|
|
|
|
} else { /* glibc 2.0 */
|
|
|
|
|
size *= 2;
|
|
|
|
|
}
|
|
|
|
|
if ((*strp = __ast_realloc(*strp, size, file, lineno, func)) == NULL)
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
@ -246,17 +241,17 @@ static int handle_show_memory(int fd, int argc, char *argv[])
|
|
|
|
|
char *fn = NULL;
|
|
|
|
|
int x;
|
|
|
|
|
struct ast_region *reg;
|
|
|
|
|
unsigned int len=0;
|
|
|
|
|
unsigned int len = 0;
|
|
|
|
|
int count = 0;
|
|
|
|
|
if (argc >3)
|
|
|
|
|
if (argc > 3)
|
|
|
|
|
fn = argv[3];
|
|
|
|
|
|
|
|
|
|
/* try to lock applications list ... */
|
|
|
|
|
ast_mutex_lock(&showmemorylock);
|
|
|
|
|
|
|
|
|
|
for (x=0;x<SOME_PRIME;x++) {
|
|
|
|
|
for (x = 0; x < SOME_PRIME; x++) {
|
|
|
|
|
reg = regions[x];
|
|
|
|
|
while(reg) {
|
|
|
|
|
while (reg) {
|
|
|
|
|
if (!fn || !strcasecmp(fn, reg->file)) {
|
|
|
|
|
ast_cli(fd, "%10d bytes allocated in %20s at line %5d of %s\n", reg->len, reg->func, reg->lineno, reg->file);
|
|
|
|
|
len += reg->len;
|
|
|
|
@ -282,22 +277,22 @@ static int handle_show_memory_summary(int fd, int argc, char *argv[])
|
|
|
|
|
char *fn = NULL;
|
|
|
|
|
int x;
|
|
|
|
|
struct ast_region *reg;
|
|
|
|
|
unsigned int len=0;
|
|
|
|
|
unsigned int len = 0;
|
|
|
|
|
int count = 0;
|
|
|
|
|
struct file_summary *list = NULL, *cur;
|
|
|
|
|
|
|
|
|
|
if (argc >3)
|
|
|
|
|
if (argc > 3)
|
|
|
|
|
fn = argv[3];
|
|
|
|
|
|
|
|
|
|
/* try to lock applications list ... */
|
|
|
|
|
ast_mutex_lock(®lock);
|
|
|
|
|
|
|
|
|
|
for (x=0;x<SOME_PRIME;x++) {
|
|
|
|
|
for (x = 0; x < SOME_PRIME; x++) {
|
|
|
|
|
reg = regions[x];
|
|
|
|
|
while(reg) {
|
|
|
|
|
while (reg) {
|
|
|
|
|
if (!fn || !strcasecmp(fn, reg->file)) {
|
|
|
|
|
cur = list;
|
|
|
|
|
while(cur) {
|
|
|
|
|
while (cur) {
|
|
|
|
|
if ((!fn && !strcmp(cur->fn, reg->file)) || (fn && !strcmp(cur->fn, reg->func)))
|
|
|
|
|
break;
|
|
|
|
|
cur = cur->next;
|
|
|
|
@ -318,14 +313,15 @@ static int handle_show_memory_summary(int fd, int argc, char *argv[])
|
|
|
|
|
ast_mutex_unlock(®lock);
|
|
|
|
|
|
|
|
|
|
/* Dump the whole list */
|
|
|
|
|
while(list) {
|
|
|
|
|
while (list) {
|
|
|
|
|
cur = list;
|
|
|
|
|
len += list->len;
|
|
|
|
|
count += list->count;
|
|
|
|
|
if (fn)
|
|
|
|
|
if (fn) {
|
|
|
|
|
ast_cli(fd, "%10d bytes in %5d allocations in function '%s' of '%s'\n", list->len, list->count, list->fn, fn);
|
|
|
|
|
else
|
|
|
|
|
} else {
|
|
|
|
|
ast_cli(fd, "%10d bytes in %5d allocations in file '%s'\n", list->len, list->count, list->fn);
|
|
|
|
|
}
|
|
|
|
|
list = list->next;
|
|
|
|
|
#if 0
|
|
|
|
|
free(cur);
|
|
|
|
@ -355,14 +351,16 @@ static struct ast_cli_entry show_memory_summary_cli =
|
|
|
|
|
handle_show_memory_summary, "Summarize outstanding memory allocations",
|
|
|
|
|
show_memory_summary_help };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void __ast_mm_init(void)
|
|
|
|
|
{
|
|
|
|
|
char filename[80] = "";
|
|
|
|
|
ast_cli_register(&show_memory_allocations_cli);
|
|
|
|
|
ast_cli_register(&show_memory_summary_cli);
|
|
|
|
|
mmlog = fopen("/var/log/asterisk/mmlog", "a+");
|
|
|
|
|
|
|
|
|
|
snprintf(filename, sizeof(filename), "%s/mmlog", (char *)ast_config_AST_LOG_DIR)
|
|
|
|
|
mmlog = fopen(filename, "a+");
|
|
|
|
|
if (option_verbose)
|
|
|
|
|
ast_verbose("Asterisk Malloc Debugger Started (see /var/log/asterisk/mmlog)\n");
|
|
|
|
|
ast_verbose("Asterisk Malloc Debugger Started (see %s))\n", filename);
|
|
|
|
|
if (mmlog) {
|
|
|
|
|
fprintf(mmlog, "%ld - New session\n", time(NULL));
|
|
|
|
|
fflush(mmlog);
|
|
|
|
|