Asterisk - The Open Source Telephony Project GIT-master-70eff7f
Loading...
Searching...
No Matches
backtrace.c
Go to the documentation of this file.
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#ifndef bfd_get_section_size
63#define bfd_get_section_size(x) bfd_section_size(x)
64#endif
65#ifndef bfd_get_section_vma
66#define bfd_get_section_vma(x, y) bfd_section_vma(y)
67#endif
68#ifndef bfd_get_section_flags
69#define bfd_get_section_flags(x, y) bfd_section_flags(y)
70#endif
71#endif
72
73#include <pthread.h>
74
75#include <stdbool.h>
76
77/* simple definition of S_OR so we don't have include strings.h */
78#define S_OR(a, b) (a && a[0] != '\0') ? a : b
79
81{
82 struct ast_bt *bt = calloc(1, sizeof(*bt));
83
84 if (!bt) {
85 return NULL;
86 }
87 bt->alloced = 1;
88
90
91 return bt;
92}
93
95{
96 bt->num_frames = backtrace(bt->addresses, AST_MAX_BT_FRAMES);
97 return 0;
98}
99
100void *__ast_bt_destroy(struct ast_bt *bt)
101{
102 if (bt && bt->alloced) {
103 free(bt);
104 }
105 return NULL;
106}
107
108#ifdef BETTER_BACKTRACES
109
110struct bfd_data {
111 struct ast_vector_string *return_strings;
112 bfd_vma pc; /* bfd.h */
113 asymbol **syms; /* bfd.h */
114 Dl_info dli; /* dlfcn.h */
115 const char *libname;
116 int dynamic;
117 int has_syms;
118 char *msg;
119 int found;
120};
121
122#define MSG_BUFF_LEN 1024
123
124static void process_section(bfd *bfdobj, asection *section, void *obj)
125{
126 struct bfd_data *data = obj;
127 const char *file, *func;
128 unsigned int line;
129 bfd_vma offset;
130 bfd_vma vma;
131 bfd_size_type size;
132 bool line_found = 0;
133 const char *fn;
134 int inlined = 0;
135
136 offset = data->pc - (data->dynamic ? (bfd_vma)(uintptr_t) data->dli.dli_fbase : 0);
137
138 if (!(bfd_get_section_flags(bfdobj, section) & SEC_ALLOC)) {
139 return;
140 }
141
142 vma = bfd_get_section_vma(bfdobj, section);
143 size = bfd_get_section_size(section);
144
145 if (offset < vma || offset >= vma + size) {
146 /* Not in this section */
147 return;
148 }
149
150 line_found = bfd_find_nearest_line(bfdobj, section, data->syms, offset - vma, &file,
151 &func, &line);
152 if (!line_found) {
153 return;
154 }
155
156 /*
157 * If we find a line, we will want to continue calling bfd_find_inliner_info
158 * to capture any inlined functions that don't have their own stack frames.
159 */
160 do {
161 data->found++;
162 /* file can possibly be null even with a success result from bfd_find_nearest_line */
163 file = file ? file : "";
164 fn = strrchr(file, '/');
165#define FMT_INLINED "[%s] %s %s:%u %s()"
166#define FMT_NOT_INLINED "[%p] %s %s:%u %s()"
167
168 snprintf(data->msg, MSG_BUFF_LEN, inlined ? FMT_INLINED : FMT_NOT_INLINED,
169 inlined ? "inlined" : (char *)(uintptr_t) data->pc,
170 data->libname,
171 fn ? fn + 1 : file,
172 line, S_OR(func, "???"));
173
174 if (AST_VECTOR_APPEND(data->return_strings, strdup(data->msg))) {
175 return;
176 }
177
178 inlined++;
179 /* Let's see if there are any inlined functions */
180 } while (bfd_find_inliner_info(bfdobj, &file, &func, &line));
181}
182
183struct ast_vector_string *__ast_bt_get_symbols(void **addresses, size_t num_frames)
184{
185 struct ast_vector_string *return_strings;
186 int stackfr;
187 bfd *bfdobj;
188 long allocsize;
189 char msg[MSG_BUFF_LEN];
190 static pthread_mutex_t bfd_mutex = PTHREAD_MUTEX_INITIALIZER;
191
192 return_strings = malloc(sizeof(struct ast_vector_string));
193 if (!return_strings) {
194 return NULL;
195 }
196 if (AST_VECTOR_INIT(return_strings, num_frames)) {
197 free(return_strings);
198 return NULL;
199 }
200
201 for (stackfr = 0; stackfr < num_frames; stackfr++) {
202 int symbolcount;
203 struct bfd_data data = {
204 .return_strings = return_strings,
205 .msg = msg,
206 .pc = (bfd_vma)(uintptr_t) addresses[stackfr],
207 .found = 0,
208 .dynamic = 0,
209 };
210
211 msg[0] = '\0';
212
213 if (!dladdr((void *)(uintptr_t) data.pc, &data.dli)) {
214 continue;
215 }
216 data.libname = strrchr(data.dli.dli_fname, '/');
217 if (!data.libname) {
218 data.libname = data.dli.dli_fname;
219 } else {
220 data.libname++;
221 }
222
223 pthread_mutex_lock(&bfd_mutex);
224 /* Using do while(0) here makes it easier to escape and clean up */
225 do {
226 bfdobj = bfd_openr(data.dli.dli_fname, NULL);
227 if (!bfdobj) {
228 break;
229 }
230
231 /* bfd_check_format does more than check. It HAS to be called */
232 if (!bfd_check_format(bfdobj, bfd_object)) {
233 break;
234 }
235
236 data.has_syms = !!(bfd_get_file_flags(bfdobj) & HAS_SYMS);
237 data.dynamic = !!(bfd_get_file_flags(bfdobj) & DYNAMIC);
238
239 if (!data.has_syms) {
240 break;
241 }
242
243 allocsize = data.dynamic ?
244 bfd_get_dynamic_symtab_upper_bound(bfdobj) : bfd_get_symtab_upper_bound(bfdobj);
245 if (allocsize < 0) {
246 break;
247 }
248
249 data.syms = malloc(allocsize);
250 if (!data.syms) {
251 break;
252 }
253
254 symbolcount = data.dynamic ?
255 bfd_canonicalize_dynamic_symtab(bfdobj, data.syms) : bfd_canonicalize_symtab(bfdobj, data.syms);
256 if (symbolcount < 0) {
257 break;
258 }
259
260 bfd_map_over_sections(bfdobj, process_section, &data);
261 } while(0);
262
263 if (bfdobj) {
264 bfd_close(bfdobj);
265 free(data.syms);
266 data.syms = NULL;
267 }
268 pthread_mutex_unlock(&bfd_mutex);
269
270 /* Default output, if we cannot find the information within BFD */
271 if (!data.found) {
272 snprintf(msg, sizeof(msg), "%s %s()",
273 data.libname,
274 S_OR(data.dli.dli_sname, "<unknown>"));
275 AST_VECTOR_APPEND(return_strings, strdup(msg));
276 }
277 }
278
279 return return_strings;
280}
281
282#else
283struct ast_vector_string *__ast_bt_get_symbols(void **addresses, size_t num_frames)
284{
285 char **strings;
286 struct ast_vector_string *return_strings;
287 int i;
288
289 return_strings = malloc(sizeof(struct ast_vector_string));
290 if (!return_strings) {
291 return NULL;
292 }
293 if (AST_VECTOR_INIT(return_strings, num_frames)) {
294 free(return_strings);
295 return NULL;
296 }
297
298 strings = backtrace_symbols(addresses, num_frames);
299 if (strings) {
300 for (i = 0; i < num_frames; i++) {
301 AST_VECTOR_APPEND(return_strings, strdup(strings[i]));
302 }
303 free(strings);
304 }
305
306 return return_strings;
307}
308#endif /* BETTER_BACKTRACES */
309
311{
313 AST_VECTOR_PTR_FREE(symbols);
314}
315
316#endif /* HAVE_BKTR */
Asterisk main include file. File version handling, generic pbx functions.
void * __ast_bt_destroy(struct ast_bt *bt)
Definition backtrace.c:100
#define S_OR(a, b)
Definition backtrace.c:78
void __ast_bt_free_symbols(struct ast_vector_string *symbols)
Definition backtrace.c:310
struct ast_bt * __ast_bt_create(void)
Definition backtrace.c:80
struct ast_vector_string * __ast_bt_get_symbols(void **addresses, size_t num_frames)
Definition backtrace.c:283
int __ast_bt_get_addresses(struct ast_bt *bt)
Definition backtrace.c:94
Asterisk backtrace generation.
#define AST_MAX_BT_FRAMES
Definition backtrace.h:29
#define ast_bt_get_addresses(bt)
Definition backtrace.h:38
char * malloc()
void free()
#define pthread_mutex_lock
Definition lock.h:632
#define pthread_mutex_t
Definition lock.h:629
#define pthread_mutex_unlock
Definition lock.h:633
#define NULL
Definition resample.c:96
A structure to hold backtrace information. This structure provides an easy means to store backtrace i...
Definition backtrace.h:50
void * addresses[AST_MAX_BT_FRAMES]
Definition backtrace.h:52
unsigned int alloced
Definition backtrace.h:56
int num_frames
Definition backtrace.h:54
String vector definitions.
Definition vector.h:55
Vector container support.
#define AST_VECTOR_PTR_FREE(vec)
Deallocates this vector pointer.
Definition vector.h:200
#define AST_VECTOR_INIT(vec, size)
Initialize a vector.
Definition vector.h:124
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition vector.h:267
#define AST_VECTOR_CALLBACK_VOID(vec, callback,...)
Execute a callback on every element in a vector disregarding callback return.
Definition vector.h:890