[asterisk-commits] mmichelson: branch mmichelson/lock_backtraces r114984 - in /team/mmichelson/l...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu May 1 13:30:22 CDT 2008


Author: mmichelson
Date: Thu May  1 13:30:21 2008
New Revision: 114984

URL: http://svn.digium.com/view/asterisk?view=rev&rev=114984
Log:
Establish a base API for creating and destroying backtraces.

I put this in logger.[hc] for now because the original ast_backtrace()
function was defined there. It may end up moving to utils.[hc] or somewhere
else later though.


Modified:
    team/mmichelson/lock_backtraces/include/asterisk/logger.h
    team/mmichelson/lock_backtraces/main/logger.c

Modified: team/mmichelson/lock_backtraces/include/asterisk/logger.h
URL: http://svn.digium.com/view/asterisk/team/mmichelson/lock_backtraces/include/asterisk/logger.h?view=diff&rev=114984&r1=114983&r2=114984
==============================================================================
--- team/mmichelson/lock_backtraces/include/asterisk/logger.h (original)
+++ team/mmichelson/lock_backtraces/include/asterisk/logger.h Thu May  1 13:30:21 2008
@@ -217,6 +217,26 @@
 	} \
 } while (0)
 
+
+#define AST_MAX_BT_FRAMES 32
+/* I'd like to call this ast_backtrace, but that name is already taken by a function */
+/* \brief
+ * Backtraces in code deal with some funky pointers, and so it's easier to store the info in 
+ * an opaque structure and have function calls which act on the structure.
+ */
+struct ast_bt {
+	void *addresses[AST_MAX_BT_FRAMES];
+	char **symbols;
+	int num_frames;
+	int alloced;
+};
+
+struct ast_bt *ast_bt_create(void);
+
+inline int ast_bt_populate(struct ast_bt *bt);
+
+void *ast_bt_destroy(struct ast_bt *bt);
+
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif

Modified: team/mmichelson/lock_backtraces/main/logger.c
URL: http://svn.digium.com/view/asterisk/team/mmichelson/lock_backtraces/main/logger.c?view=diff&rev=114984&r1=114983&r2=114984
==============================================================================
--- team/mmichelson/lock_backtraces/main/logger.c (original)
+++ team/mmichelson/lock_backtraces/main/logger.c Thu May  1 13:30:21 2008
@@ -1127,6 +1127,62 @@
 	return;
 }
 
+/* \brief
+ * Allocates memory for an ast_bt and stores addresses and symbols.
+ *
+ * \return Returns NULL on failure, or the allocated ast_bt on success
+ */
+struct ast_bt *ast_bt_create(void) 
+{
+	struct ast_bt *bt = ast_calloc(1, sizeof(*bt));
+	if (!bt) {
+		ast_log(LOG_ERROR, "Unable to allocate memory for backtrace structure!\n");
+		return NULL;
+	}
+
+	bt->alloced = 1;
+
+	if (ast_bt_populate(bt))
+		return NULL;
+
+	return bt;
+}
+
+/* \brief
+ * Fill an allocated ast_bt with addresses and symbols
+ * This is inlined so that this function will not show up in the backtrace record.
+ *
+ * \retval 0 Success
+ * \retval -1 Failure
+ */
+inline int ast_bt_populate(struct ast_bt *bt)
+{
+	bt->num_frames = backtrace(bt->addresses, AST_MAX_BT_FRAMES);
+	if (!(bt->symbols = backtrace_symbols(bt->addresses, bt->num_frames))) {
+		ast_log(LOG_WARNING, "Unable to retrieve symbols for backtrace\n");
+		return -1;
+	}
+
+	return 0;
+}
+
+/* \brief
+ * Free the dynamically allocated portions of the ast_bt structure provided.
+ * \retval NULL.
+ */
+void *ast_bt_destroy(struct ast_bt *bt)
+{
+	if (bt->symbols) {
+		free(bt->symbols);
+	}
+
+	if (bt->alloced) {
+		ast_free(bt);
+	}
+
+	return NULL;
+}
+
 void ast_backtrace(void)
 {
 #ifdef HAVE_BKTR




More information about the asterisk-commits mailing list