backtrace: Refactor ast_bt_get_symbols so it doesn't crash
[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 /*
30  * Block automatic include of asterisk/lock.h to allow use of pthread_mutex
31  * functions directly.  We don't need or want the lock.h overhead.
32  */
33 #define _ASTERISK_LOCK_H
34
35 /*
36  * The astmm ast_ memory management functions can cause ast_bt_get_symbols
37  * to be invoked so we must not use them.
38  */
39 #define ASTMM_LIBC ASTMM_IGNORE
40
41 #include "asterisk.h"
42 #include "asterisk/backtrace.h"
43
44 /*
45  * As stated above, the vector macros call the ast_ functions so
46  * we need to remap those back to the libc ones.
47  */
48 #undef ast_free
49 #undef ast_calloc
50 #undef ast_malloc
51 #define ast_free(x) free(x)
52 #define ast_calloc(n, x) calloc(n, x)
53 #define ast_malloc(x) malloc(x)
54
55 #include "asterisk/vector.h"
56
57 #ifdef HAVE_BKTR
58 #include <execinfo.h>
59 #if defined(HAVE_DLADDR) && defined(HAVE_BFD) && defined(BETTER_BACKTRACES)
60 #include <dlfcn.h>
61 #include <bfd.h>
62 #endif
63
64 #include <pthread.h>
65
66 /* simple definition of S_OR so we don't have include strings.h */
67 #define S_OR(a, b) (a && a[0] != '\0') ? a : b
68
69 struct ast_bt *__ast_bt_create(void)
70 {
71         struct ast_bt *bt = calloc(1, sizeof(*bt));
72
73         if (!bt) {
74                 return NULL;
75         }
76         bt->alloced = 1;
77
78         ast_bt_get_addresses(bt);
79
80         return bt;
81 }
82
83 int __ast_bt_get_addresses(struct ast_bt *bt)
84 {
85         bt->num_frames = backtrace(bt->addresses, AST_MAX_BT_FRAMES);
86         return 0;
87 }
88
89 void *__ast_bt_destroy(struct ast_bt *bt)
90 {
91         if (bt && bt->alloced) {
92                 free(bt);
93         }
94         return NULL;
95 }
96
97 #ifdef BETTER_BACKTRACES
98
99 struct bfd_data {
100         struct ast_vector_string *return_strings;
101         bfd_vma pc;            /* bfd.h */
102         asymbol **syms;        /* bfd.h */
103         Dl_info dli;           /* dlfcn.h */
104         const char *libname;
105         int dynamic;
106         int has_syms;
107         char *msg;
108         int found;
109 };
110
111 #define MSG_BUFF_LEN 1024
112
113 static void process_section(bfd *bfdobj, asection *section, void *obj)
114 {
115         struct bfd_data *data = obj;
116         const char *file, *func;
117         unsigned int line;
118         bfd_vma offset;
119         bfd_vma vma;
120         bfd_size_type size;
121         bfd_boolean line_found = 0;
122         char *fn;
123         int inlined = 0;
124
125         offset = data->pc - (data->dynamic ? (bfd_vma) data->dli.dli_fbase : 0);
126
127         if (!(bfd_get_section_flags(bfdobj, section) & SEC_ALLOC)) {
128                 return;
129         }
130
131         vma = bfd_get_section_vma(bfdobj, section);
132         size = bfd_get_section_size(section);
133
134         if (offset < vma || offset >= vma + size) {
135                 /* Not in this section */
136                 return;
137         }
138
139         line_found = bfd_find_nearest_line(bfdobj, section, data->syms, offset - vma, &file,
140                 &func, &line);
141         if (!line_found) {
142                 return;
143         }
144
145         /*
146          * If we find a line, we will want to continue calling bfd_find_inliner_info
147          * to capture any inlined functions that don't have their own stack frames.
148          */
149         do {
150                 data->found++;
151                 /* file can possibly be null even with a success result from bfd_find_nearest_line */
152                 file = file ? file : "";
153                 fn = strrchr(file, '/');
154 #define FMT_INLINED "[%s] %s %s:%u %s()"
155 #define FMT_NOT_INLINED "[%p] %s %s:%u %s()"
156
157                 snprintf(data->msg, MSG_BUFF_LEN, inlined ? FMT_INLINED : FMT_NOT_INLINED,
158                         inlined ? "inlined" : (char *)data->pc,
159                         data->libname,
160                         fn ? fn + 1 : file,
161                         line, S_OR(func, "???"));
162
163                 if (AST_VECTOR_APPEND(data->return_strings, strdup(data->msg))) {
164                         return;
165                 }
166
167                 inlined++;
168                 /* Let's see if there are any inlined functions */
169         } while (bfd_find_inliner_info(bfdobj, &file, &func, &line));
170 }
171
172 struct ast_vector_string *__ast_bt_get_symbols(void **addresses, size_t num_frames)
173 {
174         struct ast_vector_string *return_strings;
175         int stackfr;
176         bfd *bfdobj;
177         long allocsize;
178         char msg[MSG_BUFF_LEN];
179         static pthread_mutex_t bfd_mutex = PTHREAD_MUTEX_INITIALIZER;
180
181         return_strings = malloc(sizeof(struct ast_vector_string));
182         if (!return_strings) {
183                 return NULL;
184         }
185         if (AST_VECTOR_INIT(return_strings, num_frames)) {
186                 free(return_strings);
187                 return NULL;
188         }
189
190         for (stackfr = 0; stackfr < num_frames; stackfr++) {
191                 int symbolcount;
192                 struct bfd_data data = {
193                         .return_strings = return_strings,
194                         .msg = msg,
195                         .pc = (bfd_vma)addresses[stackfr],
196                         .found = 0,
197                         .dynamic = 0,
198                 };
199
200                 msg[0] = '\0';
201
202                 if (!dladdr((void *)data.pc, &data.dli)) {
203                         continue;
204                 }
205                 data.libname = strrchr(data.dli.dli_fname, '/');
206                 if (!data.libname) {
207                         data.libname = data.dli.dli_fname;
208                 } else {
209                         data.libname++;
210                 }
211
212                 pthread_mutex_lock(&bfd_mutex);
213                 /* Using do while(0) here makes it easier to escape and clean up */
214                 do {
215                         bfdobj = bfd_openr(data.dli.dli_fname, NULL);
216                         if (!bfdobj) {
217                                 break;
218                         }
219
220                         /* bfd_check_format does more than check.  It HAS to be called */
221                         if (!bfd_check_format(bfdobj, bfd_object)) {
222                                 break;
223                         }
224
225                         data.has_syms = !!(bfd_get_file_flags(bfdobj) & HAS_SYMS);
226                         data.dynamic = !!(bfd_get_file_flags(bfdobj) & DYNAMIC);
227
228                         if (!data.has_syms) {
229                                 break;
230                         }
231
232                         allocsize = data.dynamic ?
233                                 bfd_get_dynamic_symtab_upper_bound(bfdobj) : bfd_get_symtab_upper_bound(bfdobj);
234                         if (allocsize < 0) {
235                                 break;
236                         }
237
238                         data.syms = malloc(allocsize);
239                         if (!data.syms) {
240                                 break;
241                         }
242
243                         symbolcount = data.dynamic ?
244                                 bfd_canonicalize_dynamic_symtab(bfdobj, data.syms) : bfd_canonicalize_symtab(bfdobj, data.syms);
245                         if (symbolcount < 0) {
246                                 break;
247                         }
248
249                         bfd_map_over_sections(bfdobj, process_section, &data);
250                 } while(0);
251
252                 if (bfdobj) {
253                         bfd_close(bfdobj);
254                         free(data.syms);
255                         data.syms = NULL;
256                 }
257                 pthread_mutex_unlock(&bfd_mutex);
258
259                 /* Default output, if we cannot find the information within BFD */
260                 if (!data.found) {
261                         snprintf(msg, sizeof(msg), "%s %s()",
262                                 data.libname,
263                                 S_OR(data.dli.dli_sname, "<unknown>"));
264                         AST_VECTOR_APPEND(return_strings, strdup(msg));
265                 }
266         }
267
268         return return_strings;
269 }
270
271 #else
272 struct ast_vector_string *__ast_bt_get_symbols(void **addresses, size_t num_frames)
273 {
274         char **strings;
275         struct ast_vector_string *return_strings;
276         int i;
277
278         return_strings = malloc(sizeof(struct ast_vector_string));
279         if (!return_strings) {
280                 return NULL;
281         }
282         if (AST_VECTOR_INIT(return_strings, num_frames)) {
283                 free(return_strings);
284                 return NULL;
285         }
286
287         strings = backtrace_symbols(addresses, num_frames);
288         if (strings) {
289                 for (i = 0; i < num_frames; i++) {
290                         AST_VECTOR_APPEND(return_strings, strdup(strings[i]));
291                 }
292                 free(strings);
293         }
294
295         return return_strings;
296 }
297 #endif /* BETTER_BACKTRACES */
298
299 void __ast_bt_free_symbols(struct ast_vector_string *symbols)
300 {
301         AST_VECTOR_CALLBACK_VOID(symbols, free);
302         AST_VECTOR_PTR_FREE(symbols);
303 }
304
305 #endif /* HAVE_BKTR */