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