<p>George Joseph has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/10606">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">backtrace: Refactor ast_bt_get_symbols so it doesn't crash<br><br>We've been seeing crashes in libbfd when we attempt to generate<br>a stack trace from multiple threads. It turns out that libbfd<br>is NOT thread-safe. It can cache the bfd structure and give it to<br>multiple threads without protecting itself. To get around this,<br>we've added a global mutex around the bfd functions and also have<br>refactored the use of those functions to be more efficient and<br>to provide more information about inlined functions.<br><br>Also added a few more tests to test_pbx.c. One just calls<br>ast_assert() and the other calls ast_log_backtrace(). Neither are<br>run by default.<br><br>WARNING: This change necessitated changing the return value of<br>ast_bt_get_symbols() from an array of strings to a VECTOR of<br>strings. However, the use of this function outside Asterisk is not<br>likely.<br><br>ASTERISK-28140<br><br>Change-Id: I79d02862ddaa2423a0809caa4b3b85c128131621<br>---<br>M CHANGES<br>M UPGRADE.txt<br>M include/asterisk/backtrace.h<br>M main/backtrace.c<br>M main/logger.c<br>M main/utils.c<br>M tests/test_pbx.c<br>7 files changed, 263 insertions(+), 147 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/06/10606/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/CHANGES b/CHANGES</span><br><span>index e00c16f..ff57323 100644</span><br><span>--- a/CHANGES</span><br><span>+++ b/CHANGES</span><br><span>@@ -12,6 +12,11 @@</span><br><span> --- Functionality changes from Asterisk 16.0.0 to Asterisk 16.1.0 ------------</span><br><span> ------------------------------------------------------------------------------</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+Core</span><br><span style="color: hsl(120, 100%, 40%);">+------------------</span><br><span style="color: hsl(120, 100%, 40%);">+ * ast_bt_get_symbols() now returns a vector of strings instead</span><br><span style="color: hsl(120, 100%, 40%);">+ of an array of strings.</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> res_pjsip</span><br><span> ------------------</span><br><span> * New options 'trust_connected_line' and 'send_connected_line' have been</span><br><span>diff --git a/UPGRADE.txt b/UPGRADE.txt</span><br><span>index 93e4b52..9df5c84 100644</span><br><span>--- a/UPGRADE.txt</span><br><span>+++ b/UPGRADE.txt</span><br><span>@@ -24,6 +24,13 @@</span><br><span> === UPGRADE-15.txt -- Upgrade info for 14 to 15</span><br><span> ===========================================================</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+From 16.0.0 to 16.1.0:</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+Core</span><br><span style="color: hsl(120, 100%, 40%);">+------------------</span><br><span style="color: hsl(120, 100%, 40%);">+ * ast_bt_get_symbols() now returns a vector of strings instead</span><br><span style="color: hsl(120, 100%, 40%);">+ of an array of strings.</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> New in 16.0.0:</span><br><span> </span><br><span> app_fax:</span><br><span>diff --git a/include/asterisk/backtrace.h b/include/asterisk/backtrace.h</span><br><span>index 95358bf..1aa3381 100644</span><br><span>--- a/include/asterisk/backtrace.h</span><br><span>+++ b/include/asterisk/backtrace.h</span><br><span>@@ -86,11 +86,17 @@</span><br><span> *</span><br><span> * \param addresses A list of addresses, such as the ->addresses structure element of struct ast_bt.</span><br><span> * \param num_frames Number of addresses in the addresses list</span><br><span style="color: hsl(0, 100%, 40%);">- * \retval NULL Unable to allocate memory</span><br><span style="color: hsl(0, 100%, 40%);">- * \return List of strings. Free the entire list with a single ast_std_free call.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \return Vector of strings. Free with</span><br><span style="color: hsl(120, 100%, 40%);">+ * AST_VECTOR_CALLBACK_VOID(messages, ast_std_free);</span><br><span style="color: hsl(120, 100%, 40%);">+ * AST_VECTOR_PTR_FREE(messages);</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \note The first frame in the addresses array will usually point to __ast_bt_create</span><br><span style="color: hsl(120, 100%, 40%);">+ * so when printing the symbols you may wish to start at position 1 rather than 0.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span> * \since 1.6.2.16</span><br><span> */</span><br><span style="color: hsl(0, 100%, 40%);">-char **__ast_bt_get_symbols(void **addresses, size_t num_frames);</span><br><span style="color: hsl(120, 100%, 40%);">+struct ast_vector_string *__ast_bt_get_symbols(void **addresses, size_t num_frames);</span><br><span> </span><br><span> #endif /* HAVE_BKTR */</span><br><span> </span><br><span>diff --git a/main/backtrace.c b/main/backtrace.c</span><br><span>index 7dfcfc3..cbb642a 100644</span><br><span>--- a/main/backtrace.c</span><br><span>+++ b/main/backtrace.c</span><br><span>@@ -31,6 +31,7 @@</span><br><span> #include "asterisk/backtrace.h"</span><br><span> #include "asterisk/utils.h"</span><br><span> #include "asterisk/strings.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk/vector.h"</span><br><span> </span><br><span> #ifdef HAVE_BKTR</span><br><span> #include <execinfo.h></span><br><span>@@ -67,159 +68,203 @@</span><br><span> return NULL;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-char **__ast_bt_get_symbols(void **addresses, size_t num_frames)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- char **strings;</span><br><span style="color: hsl(0, 100%, 40%);">-#if defined(BETTER_BACKTRACES)</span><br><span style="color: hsl(0, 100%, 40%);">- int stackfr;</span><br><span style="color: hsl(0, 100%, 40%);">- bfd *bfdobj; /* bfd.h */</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef BETTER_BACKTRACES</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+struct bfd_data {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_vector_string *return_strings;</span><br><span style="color: hsl(120, 100%, 40%);">+ bfd_vma pc; /* bfd.h */</span><br><span style="color: hsl(120, 100%, 40%);">+ asymbol **syms; /* bfd.h */</span><br><span> Dl_info dli; /* dlfcn.h */</span><br><span style="color: hsl(0, 100%, 40%);">- long allocsize;</span><br><span style="color: hsl(0, 100%, 40%);">- asymbol **syms = NULL; /* bfd.h */</span><br><span style="color: hsl(0, 100%, 40%);">- bfd_vma offset; /* bfd.h */</span><br><span style="color: hsl(0, 100%, 40%);">- const char *lastslash;</span><br><span style="color: hsl(0, 100%, 40%);">- asection *section;</span><br><span style="color: hsl(120, 100%, 40%);">+ const char *libname;</span><br><span style="color: hsl(120, 100%, 40%);">+ int dynamic;</span><br><span style="color: hsl(120, 100%, 40%);">+ int has_syms;</span><br><span style="color: hsl(120, 100%, 40%);">+ char *msg;</span><br><span style="color: hsl(120, 100%, 40%);">+ int found;</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define MSG_BUFF_LEN 1024</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+ * We CANNOT call the ast_ equivalents for pthread_</span><br><span style="color: hsl(120, 100%, 40%);">+ * functions here so we need to undo the redirections</span><br><span style="color: hsl(120, 100%, 40%);">+ * created by lock.h so we can call the pthread_</span><br><span style="color: hsl(120, 100%, 40%);">+ * functions directly.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+#undef pthread_mutex_lock</span><br><span style="color: hsl(120, 100%, 40%);">+#undef pthread_mutex_unlock</span><br><span style="color: hsl(120, 100%, 40%);">+#undef pthread_mutex_t</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void process_section(bfd *bfdobj, asection *section, void *obj)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct bfd_data *data = obj;</span><br><span> const char *file, *func;</span><br><span> unsigned int line;</span><br><span style="color: hsl(0, 100%, 40%);">- char address_str[128];</span><br><span style="color: hsl(0, 100%, 40%);">- char msg[1024];</span><br><span style="color: hsl(0, 100%, 40%);">- size_t strings_size;</span><br><span style="color: hsl(0, 100%, 40%);">- size_t *eachlen;</span><br><span style="color: hsl(0, 100%, 40%);">-#endif</span><br><span style="color: hsl(120, 100%, 40%);">+ bfd_vma offset;</span><br><span style="color: hsl(120, 100%, 40%);">+ bfd_vma vma;</span><br><span style="color: hsl(120, 100%, 40%);">+ bfd_size_type size;</span><br><span style="color: hsl(120, 100%, 40%);">+ bfd_boolean line_found = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ char *fn;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-#if defined(BETTER_BACKTRACES)</span><br><span style="color: hsl(0, 100%, 40%);">- strings_size = num_frames * sizeof(*strings);</span><br><span style="color: hsl(120, 100%, 40%);">+ offset = data->pc - (data->dynamic ? (bfd_vma) data->dli.dli_fbase : 0);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- eachlen = ast_std_calloc(num_frames, sizeof(*eachlen));</span><br><span style="color: hsl(0, 100%, 40%);">- strings = ast_std_calloc(num_frames, sizeof(*strings));</span><br><span style="color: hsl(0, 100%, 40%);">- if (!eachlen || !strings) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_std_free(eachlen);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_std_free(strings);</span><br><span style="color: hsl(0, 100%, 40%);">- return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(bfd_get_section_flags(bfdobj, section) & SEC_ALLOC)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ vma = bfd_get_section_vma(bfdobj, section);</span><br><span style="color: hsl(120, 100%, 40%);">+ size = bfd_get_section_size(section);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (offset < vma || offset >= vma + size) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Not in this section */</span><br><span style="color: hsl(120, 100%, 40%);">+ return;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ line_found = bfd_find_nearest_line(bfdobj, section, data->syms, offset - vma, &file,</span><br><span style="color: hsl(120, 100%, 40%);">+ &func, &line);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!line_found) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /*</span><br><span style="color: hsl(120, 100%, 40%);">+ * We're going to loop after finding the current line using</span><br><span style="color: hsl(120, 100%, 40%);">+ * bfd_find_inliner_info to capture any inlined functions</span><br><span style="color: hsl(120, 100%, 40%);">+ * that don't have their own stack frames.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+ do {</span><br><span style="color: hsl(120, 100%, 40%);">+ data->found++;</span><br><span style="color: hsl(120, 100%, 40%);">+ /* file can possibly be null even with a success result from bfd_find_nearest_line */</span><br><span style="color: hsl(120, 100%, 40%);">+ file = file ? file : "";</span><br><span style="color: hsl(120, 100%, 40%);">+ fn = strrchr(file, '/');</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(data->msg, MSG_BUFF_LEN, "%s %s:%u %s()",</span><br><span style="color: hsl(120, 100%, 40%);">+ data->libname,</span><br><span style="color: hsl(120, 100%, 40%);">+ fn ? fn + 1 : file,</span><br><span style="color: hsl(120, 100%, 40%);">+ line, S_OR(func, "???"));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_VECTOR_APPEND(data->return_strings, ast_strdup(data->msg));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Let's see if there are any inlined functions */</span><br><span style="color: hsl(120, 100%, 40%);">+ } while (bfd_find_inliner_info(bfdobj, &file, &func, &line));</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static struct ast_vector_string *__ast_bt_get_symbols_impl(void **addresses, size_t num_frames)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_vector_string *return_strings;</span><br><span style="color: hsl(120, 100%, 40%);">+ int stackfr;</span><br><span style="color: hsl(120, 100%, 40%);">+ bfd *bfdobj;</span><br><span style="color: hsl(120, 100%, 40%);">+ long allocsize;</span><br><span style="color: hsl(120, 100%, 40%);">+ char msg[MSG_BUFF_LEN];</span><br><span style="color: hsl(120, 100%, 40%);">+ static pthread_mutex_t bfd_mutex = PTHREAD_MUTEX_INITIALIZER;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return_strings = ast_std_malloc(sizeof(struct ast_vector_string));</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!return_strings) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_VECTOR_INIT(return_strings, num_frames);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> for (stackfr = 0; stackfr < num_frames; stackfr++) {</span><br><span style="color: hsl(0, 100%, 40%);">- int found = 0, symbolcount;</span><br><span style="color: hsl(120, 100%, 40%);">+ int symbolcount;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct bfd_data data = {</span><br><span style="color: hsl(120, 100%, 40%);">+ .return_strings = return_strings,</span><br><span style="color: hsl(120, 100%, 40%);">+ .msg = msg,</span><br><span style="color: hsl(120, 100%, 40%);">+ .pc = (bfd_vma)addresses[stackfr],</span><br><span style="color: hsl(120, 100%, 40%);">+ .found = 0,</span><br><span style="color: hsl(120, 100%, 40%);">+ .dynamic = 0,</span><br><span style="color: hsl(120, 100%, 40%);">+ };</span><br><span> </span><br><span> msg[0] = '\0';</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (!dladdr(addresses[stackfr], &dli)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!dladdr((void *)data.pc, &data.dli)) {</span><br><span> continue;</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (strcmp(dli.dli_fname, "asterisk") == 0) {</span><br><span style="color: hsl(0, 100%, 40%);">- char asteriskpath[256];</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(dli.dli_fname = ast_utils_which("asterisk", asteriskpath, sizeof(asteriskpath)))) {</span><br><span style="color: hsl(0, 100%, 40%);">- /* This will fail to find symbols */</span><br><span style="color: hsl(0, 100%, 40%);">- dli.dli_fname = "asterisk";</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(120, 100%, 40%);">+ data.libname = strrchr(data.dli.dli_fname, '/');</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!data.libname) {</span><br><span style="color: hsl(120, 100%, 40%);">+ data.libname = data.dli.dli_fname;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ data.libname++;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- lastslash = strrchr(dli.dli_fname, '/');</span><br><span style="color: hsl(0, 100%, 40%);">- if ((bfdobj = bfd_openr(dli.dli_fname, NULL)) &&</span><br><span style="color: hsl(0, 100%, 40%);">- bfd_check_format(bfdobj, bfd_object) &&</span><br><span style="color: hsl(0, 100%, 40%);">- (allocsize = bfd_get_symtab_upper_bound(bfdobj)) > 0 &&</span><br><span style="color: hsl(0, 100%, 40%);">- (syms = ast_std_malloc(allocsize)) &&</span><br><span style="color: hsl(0, 100%, 40%);">- (symbolcount = bfd_canonicalize_symtab(bfdobj, syms))) {</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (bfdobj->flags & DYNAMIC) {</span><br><span style="color: hsl(0, 100%, 40%);">- offset = addresses[stackfr] - dli.dli_fbase;</span><br><span style="color: hsl(0, 100%, 40%);">- } else {</span><br><span style="color: hsl(0, 100%, 40%);">- offset = addresses[stackfr] - (void *) 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ pthread_mutex_lock(&bfd_mutex);</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Using do while(0) here makes it easier to escape and clean up */</span><br><span style="color: hsl(120, 100%, 40%);">+ do {</span><br><span style="color: hsl(120, 100%, 40%);">+ bfdobj = bfd_openr(data.dli.dli_fname, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!bfdobj) {</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- for (section = bfdobj->sections; section; section = section->next) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(bfd_get_section_flags(bfdobj, section) & SEC_ALLOC) ||</span><br><span style="color: hsl(0, 100%, 40%);">- section->vma > offset ||</span><br><span style="color: hsl(0, 100%, 40%);">- section->size + section->vma < offset) {</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!bfd_find_nearest_line(bfdobj, section, syms, offset - section->vma, &file, &func, &line)) {</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* file can possibly be null even with a success result from bfd_find_nearest_line */</span><br><span style="color: hsl(0, 100%, 40%);">- file = file ? file : "";</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Stack trace output */</span><br><span style="color: hsl(0, 100%, 40%);">- found++;</span><br><span style="color: hsl(0, 100%, 40%);">- if ((lastslash = strrchr(file, '/'))) {</span><br><span style="color: hsl(0, 100%, 40%);">- const char *prevslash;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- for (prevslash = lastslash - 1; *prevslash != '/' && prevslash >= file; prevslash--) {</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- if (prevslash >= file) {</span><br><span style="color: hsl(0, 100%, 40%);">- lastslash = prevslash;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- if (dli.dli_saddr == NULL) {</span><br><span style="color: hsl(0, 100%, 40%);">- address_str[0] = '\0';</span><br><span style="color: hsl(0, 100%, 40%);">- } else {</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(address_str, sizeof(address_str), " (%p+%lX)",</span><br><span style="color: hsl(0, 100%, 40%);">- dli.dli_saddr,</span><br><span style="color: hsl(0, 100%, 40%);">- (unsigned long) (addresses[stackfr] - dli.dli_saddr));</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(msg, sizeof(msg), "%s:%u %s()%s",</span><br><span style="color: hsl(0, 100%, 40%);">- lastslash ? lastslash + 1 : file, line,</span><br><span style="color: hsl(0, 100%, 40%);">- S_OR(func, "???"),</span><br><span style="color: hsl(0, 100%, 40%);">- address_str);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- break; /* out of section iteration */</span><br><span style="color: hsl(120, 100%, 40%);">+ /* bfd_check_format does more than check. It HAS to be called */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!bfd_check_format(bfdobj, bfd_object)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ data.has_syms = !!(bfd_get_file_flags(bfdobj) & HAS_SYMS);</span><br><span style="color: hsl(120, 100%, 40%);">+ data.dynamic = !!(bfd_get_file_flags(bfdobj) & DYNAMIC);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!data.has_syms) {</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ allocsize = data.dynamic ?</span><br><span style="color: hsl(120, 100%, 40%);">+ bfd_get_dynamic_symtab_upper_bound(bfdobj) : bfd_get_symtab_upper_bound(bfdobj);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (allocsize < 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ data.syms = ast_std_malloc(allocsize);</span><br><span style="color: hsl(120, 100%, 40%);">+ symbolcount = data.dynamic ?</span><br><span style="color: hsl(120, 100%, 40%);">+ bfd_canonicalize_dynamic_symtab(bfdobj, data.syms) : bfd_canonicalize_symtab(bfdobj, data.syms);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (symbolcount < 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ bfd_map_over_sections(bfdobj, process_section, &data);</span><br><span style="color: hsl(120, 100%, 40%);">+ } while(0);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> if (bfdobj) {</span><br><span> bfd_close(bfdobj);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_std_free(syms);</span><br><span style="color: hsl(0, 100%, 40%);">- syms = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_std_free(data.syms);</span><br><span style="color: hsl(120, 100%, 40%);">+ data.syms = NULL;</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+ pthread_mutex_unlock(&bfd_mutex);</span><br><span> </span><br><span> /* Default output, if we cannot find the information within BFD */</span><br><span style="color: hsl(0, 100%, 40%);">- if (!found) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (dli.dli_saddr == NULL) {</span><br><span style="color: hsl(0, 100%, 40%);">- address_str[0] = '\0';</span><br><span style="color: hsl(0, 100%, 40%);">- } else {</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(address_str, sizeof(address_str), " (%p+%lX)",</span><br><span style="color: hsl(0, 100%, 40%);">- dli.dli_saddr,</span><br><span style="color: hsl(0, 100%, 40%);">- (unsigned long) (addresses[stackfr] - dli.dli_saddr));</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(msg, sizeof(msg), "%s %s()%s",</span><br><span style="color: hsl(0, 100%, 40%);">- lastslash ? lastslash + 1 : dli.dli_fname,</span><br><span style="color: hsl(0, 100%, 40%);">- S_OR(dli.dli_sname, "<unknown>"),</span><br><span style="color: hsl(0, 100%, 40%);">- address_str);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!ast_strlen_zero(msg)) {</span><br><span style="color: hsl(0, 100%, 40%);">- char **tmp;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- eachlen[stackfr] = strlen(msg) + 1;</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(tmp = ast_std_realloc(strings, strings_size + eachlen[stackfr]))) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_std_free(strings);</span><br><span style="color: hsl(0, 100%, 40%);">- strings = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- break; /* out of stack frame iteration */</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- strings = tmp;</span><br><span style="color: hsl(0, 100%, 40%);">- strings[stackfr] = (char *) strings + strings_size;</span><br><span style="color: hsl(0, 100%, 40%);">- strcpy(strings[stackfr], msg);/* Safe since we just allocated the room. */</span><br><span style="color: hsl(0, 100%, 40%);">- strings_size += eachlen[stackfr];</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!data.found) {</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(msg, sizeof(msg), "%s %s()",</span><br><span style="color: hsl(120, 100%, 40%);">+ data.libname,</span><br><span style="color: hsl(120, 100%, 40%);">+ S_OR(data.dli.dli_sname, "<unknown>"));</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_VECTOR_APPEND(return_strings, ast_strdup(msg));</span><br><span> }</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (strings) {</span><br><span style="color: hsl(0, 100%, 40%);">- /* Recalculate the offset pointers because of the reallocs. */</span><br><span style="color: hsl(0, 100%, 40%);">- strings[0] = (char *) strings + num_frames * sizeof(*strings);</span><br><span style="color: hsl(0, 100%, 40%);">- for (stackfr = 1; stackfr < num_frames; stackfr++) {</span><br><span style="color: hsl(0, 100%, 40%);">- strings[stackfr] = strings[stackfr - 1] + eachlen[stackfr - 1];</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- ast_std_free(eachlen);</span><br><span style="color: hsl(120, 100%, 40%);">+ return return_strings;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-#else /* !defined(BETTER_BACKTRACES) */</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+static struct ast_vector_string *__ast_bt_get_symbols_impl(void **addresses, size_t num_frames)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ char **strings;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_vector_string *return_strings;</span><br><span style="color: hsl(120, 100%, 40%);">+ int i;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return_strings = ast_std_malloc(sizeof(struct ast_vector_string));</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!return_strings) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_VECTOR_INIT(return_strings, num_frames);</span><br><span> </span><br><span> strings = backtrace_symbols(addresses, num_frames);</span><br><span style="color: hsl(0, 100%, 40%);">-#endif /* defined(BETTER_BACKTRACES) */</span><br><span style="color: hsl(0, 100%, 40%);">- return strings;</span><br><span style="color: hsl(120, 100%, 40%);">+ for (i = 0; i < num_frames; i++) {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_VECTOR_APPEND(return_strings, ast_strdup(strings[i]));</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_std_free(strings);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return return_strings;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+#endif /* BETTER_BACKTRACES */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+struct ast_vector_string *__ast_bt_get_symbols(void **addresses, size_t num_frames)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ return __ast_bt_get_symbols_impl(addresses, num_frames);</span><br><span> }</span><br><span> </span><br><span> #endif /* HAVE_BKTR */</span><br><span>diff --git a/main/logger.c b/main/logger.c</span><br><span>index ff781b1..735c970 100644</span><br><span>--- a/main/logger.c</span><br><span>+++ b/main/logger.c</span><br><span>@@ -385,6 +385,9 @@</span><br><span> case LOGTYPE_CONSOLE:</span><br><span> {</span><br><span> char linestr[32];</span><br><span style="color: hsl(120, 100%, 40%);">+ int has_file = !ast_strlen_zero(msg->file);</span><br><span style="color: hsl(120, 100%, 40%);">+ int has_line = (msg->line > 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ int has_func = !ast_strlen_zero(msg->function);</span><br><span> </span><br><span> /*</span><br><span> * Verbose messages are interpreted by console channels in their own</span><br><span>@@ -397,15 +400,18 @@</span><br><span> /* Turn the numeric line number into a string for colorization */</span><br><span> snprintf(linestr, sizeof(linestr), "%d", msg->line);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(buf, size, "[%s] " COLORIZE_FMT "[%d]%s: " COLORIZE_FMT ":" COLORIZE_FMT " " COLORIZE_FMT ": %s",</span><br><span style="color: hsl(0, 100%, 40%);">- msg->date,</span><br><span style="color: hsl(0, 100%, 40%);">- COLORIZE(colors[msg->level], 0, msg->level_name),</span><br><span style="color: hsl(0, 100%, 40%);">- msg->lwp,</span><br><span style="color: hsl(0, 100%, 40%);">- call_identifier_str,</span><br><span style="color: hsl(0, 100%, 40%);">- COLORIZE(COLOR_BRWHITE, 0, msg->file),</span><br><span style="color: hsl(0, 100%, 40%);">- COLORIZE(COLOR_BRWHITE, 0, linestr),</span><br><span style="color: hsl(0, 100%, 40%);">- COLORIZE(COLOR_BRWHITE, 0, msg->function),</span><br><span style="color: hsl(0, 100%, 40%);">- msg->message);</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Build string to print out */</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(buf, size, "[%s] " COLORIZE_FMT "[%d]%s: " COLORIZE_FMT "%s" COLORIZE_FMT " " COLORIZE_FMT "%s %s",</span><br><span style="color: hsl(120, 100%, 40%);">+ msg->date,</span><br><span style="color: hsl(120, 100%, 40%);">+ COLORIZE(colors[msg->level], 0, msg->level_name),</span><br><span style="color: hsl(120, 100%, 40%);">+ msg->lwp,</span><br><span style="color: hsl(120, 100%, 40%);">+ call_identifier_str,</span><br><span style="color: hsl(120, 100%, 40%);">+ COLORIZE(COLOR_BRWHITE, 0, has_file ? msg->file : ""),</span><br><span style="color: hsl(120, 100%, 40%);">+ has_file ? ":" : "",</span><br><span style="color: hsl(120, 100%, 40%);">+ COLORIZE(COLOR_BRWHITE, 0, has_line ? linestr : ""),</span><br><span style="color: hsl(120, 100%, 40%);">+ COLORIZE(COLOR_BRWHITE, 0, has_func ? msg->function : ""),</span><br><span style="color: hsl(120, 100%, 40%);">+ has_func ? ":" : "",</span><br><span style="color: hsl(120, 100%, 40%);">+ msg->message);</span><br><span> }</span><br><span> break;</span><br><span> }</span><br><span>@@ -2063,7 +2069,7 @@</span><br><span> #ifdef HAVE_BKTR</span><br><span> struct ast_bt *bt;</span><br><span> int i = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- char **strings;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_vector_string *strings;</span><br><span> </span><br><span> if (!(bt = ast_bt_create())) {</span><br><span> ast_log(LOG_WARNING, "Unable to allocate space for backtrace structure\n");</span><br><span>@@ -2071,14 +2077,16 @@</span><br><span> }</span><br><span> </span><br><span> if ((strings = ast_bt_get_symbols(bt->addresses, bt->num_frames))) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_verbose("Got %d backtrace record%c\n", bt->num_frames, bt->num_frames != 1 ? 's' : ' ');</span><br><span style="color: hsl(0, 100%, 40%);">- for (i = 3; i < bt->num_frames - 2; i++) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_verbose("#%d: [%p] %s\n", i - 3, bt->addresses[i], strings[i]);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(120, 100%, 40%);">+ int count = AST_VECTOR_SIZE(strings);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- ast_std_free(strings);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(__LOG_ERROR, NULL, 0, NULL, "Got %d backtrace record%c\n", count - 3, count - 3 != 1 ? 's' : ' ');</span><br><span style="color: hsl(120, 100%, 40%);">+ for (i = 3; i < AST_VECTOR_SIZE(strings); i++) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log_safe(__LOG_ERROR, NULL, 0, NULL, "#%2d: [%p] %s\n", i - 3, bt->addresses[i], AST_VECTOR_GET(strings, i));</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_VECTOR_CALLBACK_VOID(strings, ast_std_free);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_VECTOR_PTR_FREE(strings);</span><br><span> } else {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_verbose("Could not allocate memory for backtrace\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Could not allocate memory for backtrace\n");</span><br><span> }</span><br><span> ast_bt_destroy(bt);</span><br><span> #else</span><br><span>diff --git a/main/utils.c b/main/utils.c</span><br><span>index 75f4360..70170c6 100644</span><br><span>--- a/main/utils.c</span><br><span>+++ b/main/utils.c</span><br><span>@@ -967,7 +967,7 @@</span><br><span> #ifdef HAVE_BKTR</span><br><span> static void append_backtrace_information(struct ast_str **str, struct ast_bt *bt)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">- char **symbols;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_vector_string *symbols;</span><br><span> int num_frames;</span><br><span> </span><br><span> if (!bt) {</span><br><span>@@ -981,11 +981,12 @@</span><br><span> if ((symbols = ast_bt_get_symbols(bt->addresses, num_frames))) {</span><br><span> int frame_iterator;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- for (frame_iterator = 0; frame_iterator < num_frames; ++frame_iterator) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_str_append(str, 0, "\t%s\n", symbols[frame_iterator]);</span><br><span style="color: hsl(120, 100%, 40%);">+ for (frame_iterator = 1; frame_iterator < AST_VECTOR_SIZE(symbols); ++frame_iterator) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_str_append(str, 0, "\t%s\n", AST_VECTOR_GET(symbols, frame_iterator));</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- ast_std_free(symbols);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_VECTOR_CALLBACK_VOID(symbols, ast_std_free);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_VECTOR_PTR_FREE(symbols);</span><br><span> } else {</span><br><span> ast_str_append(str, 0, "\tCouldn't retrieve backtrace symbols\n");</span><br><span> }</span><br><span>diff --git a/tests/test_pbx.c b/tests/test_pbx.c</span><br><span>index 155bec9..4a7c71b 100644</span><br><span>--- a/tests/test_pbx.c</span><br><span>+++ b/tests/test_pbx.c</span><br><span>@@ -343,8 +343,50 @@</span><br><span> return AST_TEST_FAIL;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+AST_TEST_DEFINE(call_assert)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ switch (cmd) {</span><br><span style="color: hsl(120, 100%, 40%);">+ case TEST_INIT:</span><br><span style="color: hsl(120, 100%, 40%);">+ info->name = "CALL_ASSERT";</span><br><span style="color: hsl(120, 100%, 40%);">+ info->category = "/DO_NOT_RUN/";</span><br><span style="color: hsl(120, 100%, 40%);">+ info->summary = "Calls ast_asert()K!!! (will only be run if explicitly called)";</span><br><span style="color: hsl(120, 100%, 40%);">+ info->description = "Calls ast_asert()!!! (will only be run if explicitly called). "</span><br><span style="color: hsl(120, 100%, 40%);">+ "This test is mainly used for testing CI and tool failure scenarios.";</span><br><span style="color: hsl(120, 100%, 40%);">+ info->explicit_only = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_TEST_NOT_RUN;</span><br><span style="color: hsl(120, 100%, 40%);">+ case TEST_EXECUTE:</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_assert(0);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_TEST_PASS;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+AST_TEST_DEFINE(call_backtrace)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ switch (cmd) {</span><br><span style="color: hsl(120, 100%, 40%);">+ case TEST_INIT:</span><br><span style="color: hsl(120, 100%, 40%);">+ info->name = "CALL_BACKTRACE";</span><br><span style="color: hsl(120, 100%, 40%);">+ info->category = "/DO_NOT_RUN/";</span><br><span style="color: hsl(120, 100%, 40%);">+ info->summary = "Calls ast_log_backtrace()!!! (will only be run if explicitly called)";</span><br><span style="color: hsl(120, 100%, 40%);">+ info->description = "Calls ast_log_backtrace()!!! (will only be run if explicitly called). "</span><br><span style="color: hsl(120, 100%, 40%);">+ "This test is mainly used for testing CI and tool failure scenarios.";</span><br><span style="color: hsl(120, 100%, 40%);">+ info->explicit_only = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_TEST_NOT_RUN;</span><br><span style="color: hsl(120, 100%, 40%);">+ case TEST_EXECUTE:</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log_backtrace();</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_TEST_PASS;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> static int unload_module(void)</span><br><span> {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_TEST_UNREGISTER(call_backtrace);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_TEST_UNREGISTER(call_assert);</span><br><span> AST_TEST_UNREGISTER(segv);</span><br><span> AST_TEST_UNREGISTER(pattern_match_test);</span><br><span> return 0;</span><br><span>@@ -354,6 +396,8 @@</span><br><span> {</span><br><span> AST_TEST_REGISTER(pattern_match_test);</span><br><span> AST_TEST_REGISTER(segv);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_TEST_REGISTER(call_assert);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_TEST_REGISTER(call_backtrace);</span><br><span> return AST_MODULE_LOAD_SUCCESS;</span><br><span> }</span><br><span> </span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/10606">change 10606</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/10606"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 16 </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: I79d02862ddaa2423a0809caa4b3b85c128131621 </div>
<div style="display:none"> Gerrit-Change-Number: 10606 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: George Joseph <gjoseph@digium.com> </div>