[asterisk-commits] tilghman: branch tilghman/malloc_hold r199276 - in /team/tilghman/malloc_hold...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Jun 5 10:24:47 CDT 2009


Author: tilghman
Date: Fri Jun  5 10:24:43 2009
New Revision: 199276

URL: http://svn.asterisk.org/svn-view/asterisk?view=rev&rev=199276
Log:
Blocked revisions 198725 via svnmerge

........
  r198725 | tilghman | 2009-06-01 15:33:50 -0500 (Mon, 01 Jun 2009) | 8 lines
  
  Add INCrement and DECrement functions
  (closes issue #15025)
   Reported by: greenfieldtech
   Patches: 
         func_math.c.patch_v4 uploaded by greenfieldtech (license 369)
         slightly modified by me
   Tested by: greenfieldtech, lmadsen
........

Modified:
    team/tilghman/malloc_hold/   (props changed)
    team/tilghman/malloc_hold/build_tools/cflags.xml
    team/tilghman/malloc_hold/include/asterisk/config.h
    team/tilghman/malloc_hold/main/astmm.c
    team/tilghman/malloc_hold/main/config.c

Propchange: team/tilghman/malloc_hold/
------------------------------------------------------------------------------
    automerge = *

Propchange: team/tilghman/malloc_hold/
------------------------------------------------------------------------------
    automerge-email = tilghman at mail.jeffandtilghman.com

Propchange: team/tilghman/malloc_hold/
------------------------------------------------------------------------------
    svnmerge-integrated = /branches/1.4:1-199274

Modified: team/tilghman/malloc_hold/build_tools/cflags.xml
URL: http://svn.asterisk.org/svn-view/asterisk/team/tilghman/malloc_hold/build_tools/cflags.xml?view=diff&rev=199276&r1=199275&r2=199276
==============================================================================
--- team/tilghman/malloc_hold/build_tools/cflags.xml (original)
+++ team/tilghman/malloc_hold/build_tools/cflags.xml Fri Jun  5 10:24:43 2009
@@ -11,6 +11,9 @@
 		</member>
 		<member name="MALLOC_DEBUG" displayname="Keep Track of Memory Allocations">
 		</member>
+		<member name="MALLOC_HOLD" displayname="Delay freeing memory, to find possible memory corruptors">
+			<depend>MALLOC_DEBUG</depend>
+		</member>
 		<member name="RADIO_RELAX" displayname="Relax DTMF for Radio Applications">
 		</member>
 		<member name="STATIC_BUILD" displayname="Build static binaries">

Modified: team/tilghman/malloc_hold/include/asterisk/config.h
URL: http://svn.asterisk.org/svn-view/asterisk/team/tilghman/malloc_hold/include/asterisk/config.h?view=diff&rev=199276&r1=199275&r2=199276
==============================================================================
--- team/tilghman/malloc_hold/include/asterisk/config.h (original)
+++ team/tilghman/malloc_hold/include/asterisk/config.h Fri Jun  5 10:24:43 2009
@@ -189,7 +189,12 @@
 struct ast_variable *ast_category_detach_variables(struct ast_category *cat);
 void ast_category_rename(struct ast_category *cat, const char *name);
 
+#ifdef MALLOC_DEBUG
+#define ast_variable_new(a,b)	_ast_variable_new(a,b,__FILE__,__LINE__,__PRETTY_FUNCTION__)
+struct ast_variable *_ast_variable_new(const char *name, const char *value, const char *file, int line, const char *func);
+#else
 struct ast_variable *ast_variable_new(const char *name, const char *value);
+#endif
 void ast_variable_append(struct ast_category *category, struct ast_variable *variable);
 int ast_variable_delete(struct ast_category *category, char *variable, char *match);
 int ast_variable_update(struct ast_category *category, const char *variable, 

Modified: team/tilghman/malloc_hold/main/astmm.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/tilghman/malloc_hold/main/astmm.c?view=diff&rev=199276&r1=199275&r2=199276
==============================================================================
--- team/tilghman/malloc_hold/main/astmm.c (original)
+++ team/tilghman/malloc_hold/main/astmm.c Fri Jun  5 10:24:43 2009
@@ -40,6 +40,9 @@
 #include "asterisk/lock.h"
 #include "asterisk/strings.h"
 #include "asterisk/unaligned.h"
+#if ((defined(AST_DEVMODE)) && (defined(linux)))
+#include <execinfo.h>
+#endif
 
 #define SOME_PRIME 563
 
@@ -50,7 +53,8 @@
 	FUNC_STRDUP,
 	FUNC_STRNDUP,
 	FUNC_VASPRINTF,
-	FUNC_ASPRINTF
+	FUNC_ASPRINTF,
+	FUNC_FREE,
 };
 
 /* Undefine all our macros */
@@ -81,6 +85,9 @@
 
 static struct ast_region {
 	struct ast_region *next;
+	void *freebt[16];
+	unsigned int freetime;
+	unsigned int btcount;
 	size_t len;
 	char file[40];
 	char func[40];
@@ -160,7 +167,88 @@
 	int hash = HASH(ptr);
 	struct ast_region *reg, *prev = NULL;
 	unsigned int *fence;
-
+#ifdef MALLOC_HOLD
+	time_t now;
+#endif
+
+	if (ptr == NULL) {
+		return;
+	}
+
+#ifdef MALLOC_HOLD
+	now = time(NULL);
+
+	/* Mark this segment as freed */
+	ast_mutex_lock(&reglock);
+	for (reg = regions[hash]; reg; reg = reg->next) {
+		if (reg->data == ptr) {
+			fence = (unsigned int *)(reg->data + reg->len);
+			if (reg->fence != FENCE_MAGIC) {
+				astmm_log("WARNING: Low fence violation at %p, in %s of %s, "
+					"line %d\n", reg->data, reg->func, reg->file, reg->lineno);
+			}
+			if (get_unaligned_uint32(fence) != FENCE_MAGIC) {
+				astmm_log("WARNING: High fence violation at %p, in %s of %s, "
+					"line %d\n", reg->data, reg->func, reg->file, reg->lineno);
+			}
+
+			memset(ptr, reg->len, 0);
+			reg->which = FUNC_FREE;
+			reg->freetime = now;
+#if defined(linux) && defined(AST_DEVMODE) && defined(NOT)
+			reg->btcount = backtrace(reg->freebt, 16);
+#endif
+		}
+	}
+
+	/* Delete any segment which is free for at least 120 seconds */
+	for (reg = regions[hash]; reg; reg = reg->next) {
+		if (reg->which == FUNC_FREE && reg->freetime < now - 120) {
+			struct ast_region *tbf = reg;
+			unsigned char *ptr;
+			if (prev) {
+				prev->next = reg->next;
+				reg = prev;
+			} else {
+				reg = regions[hash] = reg->next;
+			}
+
+			for (ptr = tbf->data; ptr < tbf->data + tbf->len; ptr++) {
+				if (*ptr != 0) {
+#if defined(linux) && defined(AST_DEVMODE) && defined(NOT)
+					char **strings;
+					int i;
+#endif
+					astmm_log("WARNING: memory written to after being freed "
+						"at %p, (originally in %s of %s, line %d)\n",
+						tbf->data, tbf->func, tbf->file, tbf->lineno);
+#if defined(linux) && defined(AST_DEVMODE) && defined(NOT)
+					if ((strings = backtrace_symbols(tbf->freebt, tbf->btcount))) {
+						astmm_log("%d backtrace records\n", tbf->btcount);
+						for (i = 0; i < tbf->btcount; i++) {
+#if __WORDSIZE == 32
+							astmm_log("#%d: [%08X] %s\n", i, (unsigned int)tbf->freebt[i], strings[i]);
+#elif __WORDSIZE == 64
+							astmm_log("#%d: [%016lX] %s\n", i, (unsigned long)tbf->freebt[i], strings[i]);
+#endif
+						}
+						free(strings);
+					}
+#endif
+					break;
+				}
+			}
+			free(tbf);
+
+			if (reg == NULL) {
+				break;
+			}
+		} else {
+			prev = reg;
+		}
+	}
+	ast_mutex_unlock(&reglock);
+#else
 	ast_mutex_lock(&reglock);
 	for (reg = regions[hash]; reg; reg = reg->next) {
 		if (reg->data == ptr) {
@@ -189,6 +277,7 @@
 		astmm_log("WARNING: Freeing unused memory at %p, in %s of %s, line %d\n",	
 			ptr, func, file, lineno);
 	}
+#endif
 }
 
 void *__ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func) 
@@ -490,7 +579,12 @@
 		ast_verbose("Asterisk Malloc Debugger Started (see %s))\n", filename);
 	
 	if ((mmlog = fopen(filename, "a+"))) {
-		fprintf(mmlog, "%ld - New session\n", (long)time(NULL));
+		time_t now = time(NULL);
+		struct tm tm;
+		char datetime[80];
+		localtime_r(&now, &tm);
+		strftime(datetime, sizeof(datetime), "%Y-%m-%d %H:%M:%S", &tm);
+		fprintf(mmlog, "%s - New session\n", datetime);
 		fflush(mmlog);
 	}
 }

Modified: team/tilghman/malloc_hold/main/config.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/tilghman/malloc_hold/main/config.c?view=diff&rev=199276&r1=199275&r2=199276
==============================================================================
--- team/tilghman/malloc_hold/main/config.c (original)
+++ team/tilghman/malloc_hold/main/config.c Fri Jun  5 10:24:43 2009
@@ -183,12 +183,26 @@
 	int max_include_level;
 };
 
-struct ast_variable *ast_variable_new(const char *name, const char *value) 
+#ifdef MALLOC_DEBUG
+struct ast_variable *_ast_variable_new(const char *name, const char *value, const char *file, int line, const char *func)
+#else
+struct ast_variable *ast_variable_new(const char *name, const char *value)
+#endif
 {
 	struct ast_variable *variable;
 	int name_len = strlen(name) + 1;	
 
-	if ((variable = ast_calloc(1, name_len + strlen(value) + 1 + sizeof(*variable)))) {
+	if ((variable =
+#ifdef MALLOC_DEBUG
+		__ast_calloc
+#else
+		ast_calloc
+#endif
+			(1, name_len + strlen(value) + 1 + sizeof(*variable)
+#ifdef MALLOC_DEBUG
+			, file, line, func
+#endif
+		))) {
 		variable->name = variable->stuff;
 		variable->value = variable->stuff + name_len;		
 		strcpy(variable->name,name);
@@ -618,8 +632,13 @@
 	cfg->current = (struct ast_category *) cat;
 }
 
+#ifdef MALLOC_DEBUG
+static int process_text_line(struct ast_config *cfg, struct ast_category **cat, char *buf, int lineno, const char *configfile, int withcomments,
+				char **comment_buffer, int *comment_buffer_size, char **lline_buffer, int *lline_buffer_size, const char *file, int line, const char *func)
+#else
 static int process_text_line(struct ast_config *cfg, struct ast_category **cat, char *buf, int lineno, const char *configfile, int withcomments,
 				char **comment_buffer, int *comment_buffer_size, char **lline_buffer, int *lline_buffer_size)
+#endif
 {
 	char *c;
 	char *cur = buf;
@@ -780,7 +799,11 @@
 				c++;
 			} else
 				object = 0;
+#ifdef MALLOC_DEBUG
+			if ((v = _ast_variable_new(ast_strip(cur), ast_strip(c), file, line, func))) {
+#else
 			if ((v = ast_variable_new(ast_strip(cur), ast_strip(c)))) {
+#endif
 				v->lineno = lineno;
 				v->object = object;
 				/* Put and reset comments */
@@ -957,7 +980,11 @@
 				if (process_buf) {
 					char *buf = ast_strip(process_buf);
 					if (!ast_strlen_zero(buf)) {
+#ifdef MALLOC_DEBUG
+						if (process_text_line(cfg, &cat, buf, lineno, fn, withcomments, &comment_buffer, &comment_buffer_size, &lline_buffer, &lline_buffer_size, filename, lineno, table)) {
+#else
 						if (process_text_line(cfg, &cat, buf, lineno, fn, withcomments, &comment_buffer, &comment_buffer_size, &lline_buffer, &lline_buffer_size)) {
+#endif
 							cfg = NULL;
 							break;
 						}




More information about the asterisk-commits mailing list