a1156467f703e2c60731ba8b65769996d9389c94
[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 struct ast_bt *__ast_bt_create(void)
44 {
45         struct ast_bt *bt = ast_std_calloc(1, sizeof(*bt));
46
47         if (!bt) {
48                 return NULL;
49         }
50         bt->alloced = 1;
51
52         ast_bt_get_addresses(bt);
53
54         return bt;
55 }
56
57 int __ast_bt_get_addresses(struct ast_bt *bt)
58 {
59         bt->num_frames = backtrace(bt->addresses, AST_MAX_BT_FRAMES);
60         return 0;
61 }
62
63 void *__ast_bt_destroy(struct ast_bt *bt)
64 {
65         if (bt && bt->alloced) {
66                 ast_std_free(bt);
67         }
68         return NULL;
69 }
70
71 char **__ast_bt_get_symbols(void **addresses, size_t num_frames)
72 {
73         char **strings;
74 #if defined(BETTER_BACKTRACES)
75         int stackfr;
76         bfd *bfdobj;           /* bfd.h */
77         Dl_info dli;           /* dlfcn.h */
78         long allocsize;
79         asymbol **syms = NULL; /* bfd.h */
80         bfd_vma offset;        /* bfd.h */
81         const char *lastslash;
82         asection *section;
83         const char *file, *func;
84         unsigned int line;
85         char address_str[128];
86         char msg[1024];
87         size_t strings_size;
88         size_t *eachlen;
89 #endif
90
91 #if defined(BETTER_BACKTRACES)
92         strings_size = num_frames * sizeof(*strings);
93
94         eachlen = ast_std_calloc(num_frames, sizeof(*eachlen));
95         strings = ast_std_calloc(num_frames, sizeof(*strings));
96         if (!eachlen || !strings) {
97                 ast_std_free(eachlen);
98                 ast_std_free(strings);
99                 return NULL;
100         }
101
102         for (stackfr = 0; stackfr < num_frames; stackfr++) {
103                 int found = 0, symbolcount;
104
105                 msg[0] = '\0';
106
107                 if (!dladdr(addresses[stackfr], &dli)) {
108                         continue;
109                 }
110
111                 if (strcmp(dli.dli_fname, "asterisk") == 0) {
112                         char asteriskpath[256];
113
114                         if (!(dli.dli_fname = ast_utils_which("asterisk", asteriskpath, sizeof(asteriskpath)))) {
115                                 /* This will fail to find symbols */
116                                 dli.dli_fname = "asterisk";
117                         }
118                 }
119
120                 lastslash = strrchr(dli.dli_fname, '/');
121                 if ((bfdobj = bfd_openr(dli.dli_fname, NULL)) &&
122                         bfd_check_format(bfdobj, bfd_object) &&
123                         (allocsize = bfd_get_symtab_upper_bound(bfdobj)) > 0 &&
124                         (syms = ast_std_malloc(allocsize)) &&
125                         (symbolcount = bfd_canonicalize_symtab(bfdobj, syms))) {
126
127                         if (bfdobj->flags & DYNAMIC) {
128                                 offset = addresses[stackfr] - dli.dli_fbase;
129                         } else {
130                                 offset = addresses[stackfr] - (void *) 0;
131                         }
132
133                         for (section = bfdobj->sections; section; section = section->next) {
134                                 if (!bfd_get_section_flags(bfdobj, section) & SEC_ALLOC ||
135                                         section->vma > offset ||
136                                         section->size + section->vma < offset) {
137                                         continue;
138                                 }
139
140                                 if (!bfd_find_nearest_line(bfdobj, section, syms, offset - section->vma, &file, &func, &line)) {
141                                         continue;
142                                 }
143
144                                 /* file can possibly be null even with a success result from bfd_find_nearest_line */
145                                 file = file ? file : "";
146
147                                 /* Stack trace output */
148                                 found++;
149                                 if ((lastslash = strrchr(file, '/'))) {
150                                         const char *prevslash;
151
152                                         for (prevslash = lastslash - 1; *prevslash != '/' && prevslash >= file; prevslash--) {
153                                         }
154                                         if (prevslash >= file) {
155                                                 lastslash = prevslash;
156                                         }
157                                 }
158                                 if (dli.dli_saddr == NULL) {
159                                         address_str[0] = '\0';
160                                 } else {
161                                         snprintf(address_str, sizeof(address_str), " (%p+%lX)",
162                                                 dli.dli_saddr,
163                                                 (unsigned long) (addresses[stackfr] - dli.dli_saddr));
164                                 }
165                                 snprintf(msg, sizeof(msg), "%s:%u %s()%s",
166                                         lastslash ? lastslash + 1 : file, line,
167                                         S_OR(func, "???"),
168                                         address_str);
169
170                                 break; /* out of section iteration */
171                         }
172                 }
173                 if (bfdobj) {
174                         bfd_close(bfdobj);
175                         ast_std_free(syms);
176                 }
177
178                 /* Default output, if we cannot find the information within BFD */
179                 if (!found) {
180                         if (dli.dli_saddr == NULL) {
181                                 address_str[0] = '\0';
182                         } else {
183                                 snprintf(address_str, sizeof(address_str), " (%p+%lX)",
184                                         dli.dli_saddr,
185                                         (unsigned long) (addresses[stackfr] - dli.dli_saddr));
186                         }
187                         snprintf(msg, sizeof(msg), "%s %s()%s",
188                                 lastslash ? lastslash + 1 : dli.dli_fname,
189                                 S_OR(dli.dli_sname, "<unknown>"),
190                                 address_str);
191                 }
192
193                 if (!ast_strlen_zero(msg)) {
194                         char **tmp;
195
196                         eachlen[stackfr] = strlen(msg) + 1;
197                         if (!(tmp = ast_std_realloc(strings, strings_size + eachlen[stackfr]))) {
198                                 ast_std_free(strings);
199                                 strings = NULL;
200                                 break; /* out of stack frame iteration */
201                         }
202                         strings = tmp;
203                         strings[stackfr] = (char *) strings + strings_size;
204                         strcpy(strings[stackfr], msg);/* Safe since we just allocated the room. */
205                         strings_size += eachlen[stackfr];
206                 }
207         }
208
209         if (strings) {
210                 /* Recalculate the offset pointers because of the reallocs. */
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];
214                 }
215         }
216         ast_std_free(eachlen);
217
218 #else /* !defined(BETTER_BACKTRACES) */
219
220         strings = backtrace_symbols(addresses, num_frames);
221 #endif /* defined(BETTER_BACKTRACES) */
222         return strings;
223 }
224
225 #endif /* HAVE_BKTR */