Add backtrace generation to MALLOC_DEBUG memory corruption reports
[asterisk/asterisk.git] / main / backtrace.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2013, Digium, Inc.
5  *
6  * Matt Jordan <mjordan@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  * \brief Asterisk backtrace generation
21  *
22  * This file provides backtrace generation utilities
23  */
24
25 /*** MODULEINFO
26         <support_level>core</support_level>
27  ***/
28
29 #include "asterisk.h"
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
31
32 #include "asterisk/backtrace.h"
33 #include "asterisk/utils.h"
34 #include "asterisk/strings.h"
35
36 #ifdef HAVE_BKTR
37 #include <execinfo.h>
38 #if defined(HAVE_DLADDR) && defined(HAVE_BFD) && defined(BETTER_BACKTRACES)
39 #include <dlfcn.h>
40 #include <bfd.h>
41 #endif
42
43 /* Undefine the overrides for memory allocation. astmm.c uses these functions
44  * as well.
45  */
46 #undef calloc
47 #undef malloc
48 #undef free
49 #undef realloc
50
51 struct ast_bt *__ast_bt_create(void)
52 {
53         struct ast_bt *bt = calloc(1, sizeof(*bt));
54         if (!bt) {
55                 return NULL;
56         }
57         bt->alloced = 1;
58
59         ast_bt_get_addresses(bt);
60
61         return bt;
62 }
63
64 int __ast_bt_get_addresses(struct ast_bt *bt)
65 {
66         bt->num_frames = backtrace(bt->addresses, AST_MAX_BT_FRAMES);
67         return 0;
68 }
69
70 void *__ast_bt_destroy(struct ast_bt *bt)
71 {
72         if (bt->alloced) {
73                 free(bt);
74         }
75         return NULL;
76 }
77
78 char **__ast_bt_get_symbols(void **addresses, size_t num_frames)
79 {
80         char **strings = NULL;
81 #if defined(BETTER_BACKTRACES)
82         int stackfr;
83         bfd *bfdobj;           /* bfd.h */
84         Dl_info dli;           /* dlfcn.h */
85         long allocsize;
86         asymbol **syms = NULL; /* bfd.h */
87         bfd_vma offset;        /* bfd.h */
88         const char *lastslash;
89         asection *section;
90         const char *file, *func;
91         unsigned int line;
92         char address_str[128];
93         char msg[1024];
94         size_t strings_size;
95         size_t *eachlen;
96 #endif
97
98 #if defined(BETTER_BACKTRACES)
99         strings_size = num_frames * sizeof(*strings);
100         eachlen = calloc(num_frames, sizeof(*eachlen));
101
102         if (!(strings = calloc(num_frames, sizeof(*strings)))) {
103                 return NULL;
104         }
105
106         for (stackfr = 0; stackfr < num_frames; stackfr++) {
107                 int found = 0, symbolcount;
108
109                 msg[0] = '\0';
110
111                 if (!dladdr(addresses[stackfr], &dli)) {
112                         continue;
113                 }
114
115                 if (strcmp(dli.dli_fname, "asterisk") == 0) {
116                         char asteriskpath[256];
117                         if (!(dli.dli_fname = ast_utils_which("asterisk", asteriskpath, sizeof(asteriskpath)))) {
118                                 /* This will fail to find symbols */
119                                 dli.dli_fname = "asterisk";
120                         }
121                 }
122
123                 lastslash = strrchr(dli.dli_fname, '/');
124                 if (    (bfdobj = bfd_openr(dli.dli_fname, NULL)) &&
125                                 bfd_check_format(bfdobj, bfd_object) &&
126                                 (allocsize = bfd_get_symtab_upper_bound(bfdobj)) > 0 &&
127                                 (syms = malloc(allocsize)) &&
128                                 (symbolcount = bfd_canonicalize_symtab(bfdobj, syms))) {
129
130                         if (bfdobj->flags & DYNAMIC) {
131                                 offset = addresses[stackfr] - dli.dli_fbase;
132                         } else {
133                                 offset = addresses[stackfr] - (void *)0;
134                         }
135
136                         for (section = bfdobj->sections; section; section = section->next) {
137                                 if (    !bfd_get_section_flags(bfdobj, section) & SEC_ALLOC ||
138                                                 section->vma > offset ||
139                                                 section->size + section->vma < offset) {
140                                         continue;
141                                 }
142
143                                 if (!bfd_find_nearest_line(bfdobj, section, syms, offset - section->vma, &file, &func, &line)) {
144                                         continue;
145                                 }
146
147                                 /* file can possibly be null even with a success result from bfd_find_nearest_line */
148                                 file = file ? file : "";
149
150                                 /* Stack trace output */
151                                 found++;
152                                 if ((lastslash = strrchr(file, '/'))) {
153                                         const char *prevslash;
154                                         for (prevslash = lastslash - 1; *prevslash != '/' && prevslash >= file; prevslash--);
155                                         if (prevslash >= file) {
156                                                 lastslash = prevslash;
157                                         }
158                                 }
159                                 if (dli.dli_saddr == NULL) {
160                                         address_str[0] = '\0';
161                                 } else {
162                                         snprintf(address_str, sizeof(address_str), " (%p+%lX)",
163                                                 dli.dli_saddr,
164                                                 (unsigned long) (addresses[stackfr] - dli.dli_saddr));
165                                 }
166                                 snprintf(msg, sizeof(msg), "%s:%u %s()%s",
167                                         lastslash ? lastslash + 1 : file, line,
168                                         S_OR(func, "???"),
169                                         address_str);
170
171                                 break; /* out of section iteration */
172                         }
173                 }
174                 if (bfdobj) {
175                         bfd_close(bfdobj);
176                         free(syms);
177                 }
178
179                 /* Default output, if we cannot find the information within BFD */
180                 if (!found) {
181                         if (dli.dli_saddr == NULL) {
182                                 address_str[0] = '\0';
183                         } else {
184                                 snprintf(address_str, sizeof(address_str), " (%p+%lX)",
185                                         dli.dli_saddr,
186                                         (unsigned long) (addresses[stackfr] - dli.dli_saddr));
187                         }
188                         snprintf(msg, sizeof(msg), "%s %s()%s",
189                                 lastslash ? lastslash + 1 : dli.dli_fname,
190                                 S_OR(dli.dli_sname, "<unknown>"),
191                                 address_str);
192                 }
193
194                 if (!ast_strlen_zero(msg)) {
195                         char **tmp;
196                         eachlen[stackfr] = strlen(msg);
197                         if (!(tmp = realloc(strings, strings_size + eachlen[stackfr] + 1))) {
198                                 free(strings);
199                                 strings = NULL;
200                                 break; /* out of stack frame iteration */
201                         }
202                         strings = tmp;
203                         strings[stackfr] = (char *) strings + strings_size;
204                         ast_copy_string(strings[stackfr], msg, eachlen[stackfr] + 1);
205                         strings_size += eachlen[stackfr] + 1;
206                 }
207         }
208
209         if (strings) {
210                 /* Recalculate the offset pointers */
211                 strings[0] = (char *) strings + num_frames * sizeof(*strings);
212                 for (stackfr = 1; stackfr < num_frames; stackfr++) {
213                         strings[stackfr] = strings[stackfr - 1] + eachlen[stackfr - 1] + 1;
214                 }
215         }
216 #else /* !defined(BETTER_BACKTRACES) */
217         strings = backtrace_symbols(addresses, num_frames);
218 #endif /* defined(BETTER_BACKTRACES) */
219         return strings;
220 }
221
222 #endif /* HAVE_BKTR */