[asterisk-commits] tilghman: branch tilghman/better_backtrace_1.4 r297953 - in /team/tilghman/be...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Dec 9 14:53:28 CST 2010


Author: tilghman
Date: Thu Dec  9 14:53:26 2010
New Revision: 297953

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=297953
Log:
Yay, everything works...

Modified:
    team/tilghman/better_backtrace_1.4/include/asterisk/utils.h
    team/tilghman/better_backtrace_1.4/main/logger.c
    team/tilghman/better_backtrace_1.4/main/utils.c

Modified: team/tilghman/better_backtrace_1.4/include/asterisk/utils.h
URL: http://svnview.digium.com/svn/asterisk/team/tilghman/better_backtrace_1.4/include/asterisk/utils.h?view=diff&rev=297953&r1=297952&r2=297953
==============================================================================
--- team/tilghman/better_backtrace_1.4/include/asterisk/utils.h (original)
+++ team/tilghman/better_backtrace_1.4/include/asterisk/utils.h Thu Dec  9 14:53:26 2010
@@ -563,4 +563,13 @@
 #define ast_assert(a)
 #endif
 
+/*!\brief Resolve a binary to a full pathname
+ * \param binary Name of the executable to resolve
+ * \param fullpath Buffer to hold the complete pathname
+ * \param fullpath_size Size of \a fullpath
+ * \retval NULL \a binary was not found or the environment variable PATH is not set
+ * \return \a fullpath
+ */
+char *ast_utils_which(const char *binary, char *fullpath, size_t fullpath_size);
+
 #endif /* _ASTERISK_UTILS_H */

Modified: team/tilghman/better_backtrace_1.4/main/logger.c
URL: http://svnview.digium.com/svn/asterisk/team/tilghman/better_backtrace_1.4/main/logger.c?view=diff&rev=297953&r1=297952&r2=297953
==============================================================================
--- team/tilghman/better_backtrace_1.4/main/logger.c (original)
+++ team/tilghman/better_backtrace_1.4/main/logger.c Thu Dec  9 14:53:26 2010
@@ -833,48 +833,57 @@
 void ast_backtrace(void)
 {
 #ifdef linux
-#ifdef AST_DEVMODE
-	int count=0, i=0;
+#  ifdef AST_DEVMODE
+	int stackcount = 0, stackfr;
 	void **addresses;
-#if defined(HAVE_DLADDR) && defined(HAVE_BFD)
-	bfd *bfdobj;
-	Dl_info dli;
+#    if defined(HAVE_DLADDR) && defined(HAVE_BFD)
+	bfd *bfdobj;           /* bfd.h */
+	Dl_info dli;           /* dlfcn.h */
 	long allocsize;
-	asymbol **syms = NULL;
-	bfd_vma offset;
-	const char *last_slash;
-#if 0
+	asymbol **syms = NULL; /* bfd.h */
+	bfd_vma offset;        /* bfd.h */
+	const char *lastslash;
 	asection *section;
 	const char *file, *func;
 	unsigned int line;
-#endif
-	int j;
-#else
+	char address_str[128];
+#    else
 	char **strings;
-#endif
+#    endif
 
 	if ((addresses = ast_calloc(MAX_BACKTRACE_FRAMES, sizeof(*addresses)))) {
-		count = backtrace(addresses, MAX_BACKTRACE_FRAMES);
-#if defined(HAVE_DLADDR) && defined(HAVE_BFD)
-		ast_log(LOG_DEBUG, "Got %d backtrace record%c\n", count, count != 1 ? 's' : ' ');
-		for (i = 0; i < count; i++) {
+		stackcount = backtrace(addresses, MAX_BACKTRACE_FRAMES);
+#    if defined(HAVE_DLADDR) && defined(HAVE_BFD)
+		ast_log(LOG_DEBUG, "Got %d backtrace record%c\n", stackcount, stackcount != 1 ? 's' : ' ');
+		for (stackfr = 0; stackfr < stackcount; stackfr++) {
 			int found = 0, symbolcount;
-			int bestmatch = -1;
-			long bestdiff = LONG_MAX;
-
-			if (!dladdr(addresses[i], &dli)) {
+
+			if (!dladdr(addresses[stackfr], &dli)) {
 				continue;
 			}
-			last_slash = strrchr(dli.dli_fname, '/');
+
+			if (strcmp(dli.dli_fname, "asterisk") == 0) {
+				char asteriskpath[256];
+				if (!(dli.dli_fname = ast_utils_which("asterisk", asteriskpath, sizeof(asteriskpath)))) {
+					/* This will fail to find symbols */
+					ast_log(LOG_DEBUG, "Failed to find asterisk binary for debug symbols.\n");
+					dli.dli_fname = "asterisk";
+				}
+			}
+
+			lastslash = strrchr(dli.dli_fname, '/');
 			if (	(bfdobj = bfd_openr(dli.dli_fname, NULL)) &&
 					bfd_check_format(bfdobj, bfd_object) &&
 					(allocsize = bfd_get_symtab_upper_bound(bfdobj)) > 0 &&
 					(syms = ast_malloc(allocsize)) &&
 					(symbolcount = bfd_canonicalize_symtab(bfdobj, syms))) {
 
-				offset = addresses[i] - dli.dli_fbase;
-
-#if 0
+				if (bfdobj->flags & DYNAMIC) {
+					offset = addresses[stackfr] - dli.dli_fbase;
+				} else {
+					offset = addresses[stackfr] - (void *) 0;
+				}
+
 				for (section = bfdobj->sections; section; section = section->next) {
 					if (	!bfd_get_section_flags(bfdobj, section) & SEC_ALLOC ||
 							section->vma > offset ||
@@ -883,48 +892,42 @@
 					}
 
 					if (!bfd_find_nearest_line(bfdobj, section, syms, offset - section->vma, &file, &func, &line)) {
-						break;
+						continue;
 					}
 
-					/* TODO Stack trace output */
+					/* Stack trace output */
 					found++;
-#if __WORDSIZE == 32
-					ast_log(LOG_DEBUG, "#%d: [%08lX] %s:%u %s (%08lX+%lX)\n", i,
-						(unsigned long) addresses[i],
-						file, line,
+					lastslash = strrchr(file, '/');
+#      if __WORDSIZE == 32
+					if (dli.dli_saddr == NULL) {
+						address_str[0] = '\0';
+					} else {
+						snprintf(address_str, sizeof(address_str), " (%08lX+%lX)",
+							(unsigned long) dli.dli_saddr,
+							(unsigned long) (addresses[stackfr] - dli.dli_saddr));
+					}
+					ast_log(LOG_DEBUG, "#%d: [%08lX] %s:%u %s()%s\n", stackfr,
+						(unsigned long) addresses[stackfr],
+						lastslash ? lastslash + 1 : file, line,
 						S_OR(func, "???"),
-						(unsigned long) dli.dli_saddr,
-						(unsigned long) (addresses[i] - dli.dli_saddr));
-#elif __WORDSIZE == 64
-					ast_log(LOG_DEBUG, "#%d: [%016lX] %s:%u %s (%16lX+%lX)\n", i,
-						(unsigned long) addresses[i],
-						file, line,
+						address_str);
+#      elif __WORDSIZE == 64
+					if (dli.dli_saddr == NULL) {
+						address_str[0] = '\0';
+					} else {
+						snprintf(address_str, sizeof(address_str), " (%016lX+%lX)",
+							(unsigned long) dli.dli_saddr,
+							(unsigned long) (addresses[stackfr] - dli.dli_saddr));
+					}
+					ast_log(LOG_DEBUG, "#%d: [%016lX] %s:%u %s()%s\n", stackfr,
+						(unsigned long) addresses[stackfr],
+						lastslash ? lastslash + 1 : file, line,
 						S_OR(func, "???"),
-						(unsigned long) dli.dli_saddr,
-						(unsigned long) (addresses[i] - dli.dli_saddr));
-#endif
+						address_str);
+#      endif
 
 					break;
 				}
-#else
-				for (j = 0; j < symbolcount; j++) {
-					asymbol *s = syms[j];
-					if (s->value + s->section->vma > offset) {
-						/* Past the symbol we want */
-						continue;
-					}
-					if (offset - (s->value + s->section->vma) < bestdiff) {
-						bestmatch = j;
-						bestdiff = offset - (s->value + s->section->vma);
-					}
-				}
-				found++;
-				ast_log(LOG_DEBUG, "#%d: [%08lX] %s %s (%08lX+%lX) %d\n", i,
-					(unsigned long) addresses[i],
-					last_slash ? last_slash + 1 : dli.dli_fname, syms[bestmatch]->name,
-					(unsigned long) dli.dli_saddr,
-					(unsigned long) (addresses[i] - dli.dli_saddr), j);
-#endif
 			}
 			if (bfdobj) {
 				bfd_close(bfdobj);
@@ -935,43 +938,55 @@
 
 			/* Default output, if we cannot find the information within BFD */
 			if (!found) {
-#if __WORDSIZE == 32
-				ast_log(LOG_DEBUG, "#%d: [%08lX] %s %s (%08lX+%lX)\n", i,
-					(unsigned long) addresses[i],
-					last_slash ? last_slash + 1 : dli.dli_fname,
+#      if __WORDSIZE == 32
+				if (dli.dli_saddr == NULL) {
+					address_str[0] = '\0';
+				} else {
+					snprintf(address_str, sizeof(address_str), " (%08lX+%lX)",
+						(unsigned long) dli.dli_saddr,
+						(unsigned long) (addresses[stackfr] - dli.dli_saddr));
+				}
+				ast_log(LOG_DEBUG, "#%d: [%08lX] %s %s()%s\n", stackfr,
+					(unsigned long) addresses[stackfr],
+					lastslash ? lastslash + 1 : dli.dli_fname,
 					S_OR(dli.dli_sname, "<unknown>"),
-					(unsigned long) dli.dli_saddr,
-					(unsigned long) (addresses[i] - dli.dli_saddr));
-#elif __WORDSIZE == 64
-				ast_log(LOG_DEBUG, "#%d: [%016lX] %s %s (%08lX+%lX)\n", i,
-					(unsigned long) addresses[i],
-					last_slash ? last_slash + 1 : dli.dli_fname,
+					address_str);
+#      elif __WORDSIZE == 64
+				if (dli.dli_saddr == NULL) {
+					address_str[0] = '\0';
+				} else {
+					snprintf(address_str, sizeof(address_str), " (%016lX+%lX)",
+						(unsigned long) dli.dli_saddr,
+						(unsigned long) (addresses[stackfr] - dli.dli_saddr));
+				}
+				ast_log(LOG_DEBUG, "#%d: [%016lX] %s %s()%s\n", stackfr,
+					(unsigned long) addresses[stackfr],
+					lastslash ? lastslash + 1 : dli.dli_fname,
 					S_OR(dli.dli_sname, "<unknown>"),
-					(unsigned long) dli.dli_saddr,
-					(unsigned long) (addresses[i] - dli.dli_saddr));
-#endif
-			}
-		}
-#else
-		if ((strings = backtrace_symbols(addresses, count))) {
-			ast_log(LOG_DEBUG, "Got %d backtrace record%c\n", count, count != 1 ? 's' : ' ');
-			for (i=0; i < count ; i++) {
-#if __WORDSIZE == 32
-				ast_log(LOG_DEBUG, "#%d: [%08X] %s\n", i, (unsigned int)addresses[i], strings[i]);
-#elif __WORDSIZE == 64
-				ast_log(LOG_DEBUG, "#%d: [%016lX] %s\n", i, (unsigned long)addresses[i], strings[i]);
-#endif
+					address_str);
+#      endif
+			}
+		}
+#    else /* !defined(HAVE_DLADDR) || !defined(HAVE_BFD) */
+		if ((strings = backtrace_symbols(addresses, stackcount))) {
+			ast_log(LOG_DEBUG, "Got %d backtrace record%c\n", stackcount, stackcount != 1 ? 's' : ' ');
+			for (stackfr = 0; stackfr < stackcount ; stackfr++) {
+#      if __WORDSIZE == 32
+				ast_log(LOG_DEBUG, "#%d: [%08X] %s\n", i, (unsigned int)addresses[stackfr], strings[stackfr]);
+#      elif __WORDSIZE == 64
+				ast_log(LOG_DEBUG, "#%d: [%016lX] %s\n", i, (unsigned long)addresses[stackfr], strings[stackfr]);
+#      endif
 			}
 			free(strings);
 		} else {
 			ast_log(LOG_DEBUG, "Could not allocate memory for backtrace\n");
 		}
-#endif
+#    endif /* defined(HAVE_DLADDR) && defined(HAVE_BFD) */
 		free(addresses);
 	}
-#else
+#  else /* !defined(AST_DEVMODE) */
 	ast_log(LOG_WARNING, "Must run configure with '--enable-dev-mode' for stack backtraces.\n");
-#endif
+#  endif /* defined(AST_DEVMODE) */
 #else /* ndef linux */
 	ast_log(LOG_WARNING, "Inline stack backtraces are only available on the Linux platform.\n");
 #endif

Modified: team/tilghman/better_backtrace_1.4/main/utils.c
URL: http://svnview.digium.com/svn/asterisk/team/tilghman/better_backtrace_1.4/main/utils.c?view=diff&rev=297953&r1=297952&r2=297953
==============================================================================
--- team/tilghman/better_backtrace_1.4/main/utils.c (original)
+++ team/tilghman/better_backtrace_1.4/main/utils.c Thu Dec  9 14:53:26 2010
@@ -38,6 +38,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#include <sys/stat.h>
 
 #define AST_API_MODULE		/* ensure that inlinable API functions will be built in lock.h if required */
 #include "asterisk/lock.h"
@@ -1441,3 +1442,22 @@
 	return res;
 }
 #endif
+
+char *ast_utils_which(const char *binary, char *fullpath, size_t fullpath_size)
+{
+	const char *envPATH = getenv("PATH");
+	char *tpath, *path;
+	struct stat unused;
+	if (!envPATH) {
+		return NULL;
+	}
+	tpath = ast_strdupa(envPATH);
+	while ((path = strsep(&tpath, ":"))) {
+		snprintf(fullpath, fullpath_size, "%s/%s", path, binary);
+		if (!stat(fullpath, &unused)) {
+			return fullpath;
+		}
+	}
+	return NULL;
+}
+




More information about the asterisk-commits mailing list