| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#define _ASTERISK_LOCK_H |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
#define ASTMM_LIBC ASTMM_IGNORE |
| 40 |
|
| 41 |
#include "asterisk.h" |
| 42 |
#include "asterisk/backtrace.h" |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 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 |
|
| 76 |
#define S_OR(a, b) (a && a[0] != '\0') ? a : b |
| 77 |
|
| 78 |
struct ast_bt *__ast_bt_create(void) |
| 79 |
{ |
| 80 |
struct ast_bt *bt = calloc(1, sizeof(*bt)); |
| 81 |
|
| 82 |
if (!bt) { |
| 83 |
return NULL; |
| 84 |
} |
| 85 |
bt->alloced = 1; |
| 86 |
|
| 87 |
ast_bt_get_addresses(bt); |
| 88 |
|
| 89 |
return bt; |
| 90 |
} |
| 91 |
|
| 92 |
int __ast_bt_get_addresses(struct ast_bt *bt) |
| 93 |
{ |
| 94 |
bt->num_frames = backtrace(bt->addresses, AST_MAX_BT_FRAMES); |
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
void *__ast_bt_destroy(struct ast_bt *bt) |
| 99 |
{ |
| 100 |
if (bt && bt->alloced) { |
| 101 |
free(bt); |
| 102 |
} |
| 103 |
return NULL; |
| 104 |
} |
| 105 |
|
| 106 |
#ifdef BETTER_BACKTRACES |
| 107 |
|
| 108 |
struct bfd_data { |
| 109 |
struct ast_vector_string *return_strings; |
| 110 |
bfd_vma pc; |
| 111 |
asymbol **syms; |
| 112 |
Dl_info dli; |
| 113 |
const char *libname; |
| 114 |
int dynamic; |
| 115 |
int has_syms; |
| 116 |
char *msg; |
| 117 |
int found; |
| 118 |
}; |
| 119 |
|
| 120 |
#define MSG_BUFF_LEN 1024 |
| 121 |
|
| 122 |
static void process_section(bfd *bfdobj, asection *section, void *obj) |
| 123 |
{ |
| 124 |
struct bfd_data *data = obj; |
| 125 |
const char *file, *func; |
| 126 |
unsigned int line; |
| 127 |
bfd_vma offset; |
| 128 |
bfd_vma vma; |
| 129 |
bfd_size_type size; |
| 130 |
bfd_boolean line_found = 0; |
| 131 |
const char *fn; |
| 132 |
int inlined = 0; |
| 133 |
|
| 134 |
offset = data->pc - (data->dynamic ? (bfd_vma)(uintptr_t) data->dli.dli_fbase : 0); |
| 135 |
|
| 136 |
if (!(bfd_get_section_flags(bfdobj, section) & SEC_ALLOC)) { |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
vma = bfd_get_section_vma(bfdobj, section); |
| 141 |
size = bfd_get_section_size(section); |
| 142 |
|
| 143 |
if (offset < vma || offset >= vma + size) { |
| 144 |
|
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
line_found = bfd_find_nearest_line(bfdobj, section, data->syms, offset - vma, &file, |
| 149 |
&func, &line); |
| 150 |
if (!line_found) { |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
do { |
| 159 |
data->found++; |
| 160 |
|
| 161 |
file = file ? file : ""; |
| 162 |
fn = strrchr(file, '/'); |
| 163 |
#define FMT_INLINED "[%s] %s %s:%u %s()" |
| 164 |
#define FMT_NOT_INLINED "[%p] %s %s:%u %s()" |
| 165 |
|
| 166 |
snprintf(data->msg, MSG_BUFF_LEN, inlined ? FMT_INLINED : FMT_NOT_INLINED, |
| 167 |
inlined ? "inlined" : (char *)(uintptr_t) data->pc, |
| 168 |
data->libname, |
| 169 |
fn ? fn + 1 : file, |
| 170 |
line, S_OR(func, "???")); |
| 171 |
|
| 172 |
if (AST_VECTOR_APPEND(data->return_strings, strdup(data->msg))) { |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
inlined++; |
| 177 |
|
| 178 |
} while (bfd_find_inliner_info(bfdobj, &file, &func, &line)); |
| 179 |
} |
| 180 |
|
| 181 |
struct ast_vector_string *__ast_bt_get_symbols(void **addresses, size_t num_frames) |
| 182 |
{ |
| 183 |
struct ast_vector_string *return_strings; |
| 184 |
int stackfr; |
| 185 |
bfd *bfdobj; |
| 186 |
long allocsize; |
| 187 |
char msg[MSG_BUFF_LEN]; |
| 188 |
static pthread_mutex_t bfd_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 189 |
|
| 190 |
return_strings = malloc(sizeof(struct ast_vector_string)); |
| 191 |
if (!return_strings) { |
| 192 |
return NULL; |
| 193 |
} |
| 194 |
if (AST_VECTOR_INIT(return_strings, num_frames)) { |
| 195 |
free(return_strings); |
| 196 |
return NULL; |
| 197 |
} |
| 198 |
|
| 199 |
for (stackfr = 0; stackfr < num_frames; stackfr++) { |
| 200 |
int symbolcount; |
| 201 |
struct bfd_data data = { |
| 202 |
.return_strings = return_strings, |
| 203 |
.msg = msg, |
| 204 |
.pc = (bfd_vma)(uintptr_t) addresses[stackfr], |
| 205 |
.found = 0, |
| 206 |
.dynamic = 0, |
| 207 |
}; |
| 208 |
|
| 209 |
msg[0] = '\0'; |
| 210 |
|
| 211 |
if (!dladdr((void *)(uintptr_t) data.pc, &data.dli)) { |
| 212 |
continue; |
| 213 |
} |
| 214 |
data.libname = strrchr(data.dli.dli_fname, '/'); |
| 215 |
if (!data.libname) { |
| 216 |
data.libname = data.dli.dli_fname; |
| 217 |
} else { |
| 218 |
data.libname++; |
| 219 |
} |
| 220 |
|
| 221 |
pthread_mutex_lock(&bfd_mutex); |
| 222 |
|
| 223 |
do { |
| 224 |
bfdobj = bfd_openr(data.dli.dli_fname, NULL); |
| 225 |
if (!bfdobj) { |
| 226 |
break; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
if (!bfd_check_format(bfdobj, bfd_object)) { |
| 231 |
break; |
| 232 |
} |
| 233 |
|
| 234 |
data.has_syms = !!(bfd_get_file_flags(bfdobj) & HAS_SYMS); |
| 235 |
data.dynamic = !!(bfd_get_file_flags(bfdobj) & DYNAMIC); |
| 236 |
|
| 237 |
if (!data.has_syms) { |
| 238 |
break; |
| 239 |
} |
| 240 |
|
| 241 |
allocsize = data.dynamic ? |
| 242 |
bfd_get_dynamic_symtab_upper_bound(bfdobj) : bfd_get_symtab_upper_bound(bfdobj); |
| 243 |
if (allocsize < 0) { |
| 244 |
break; |
| 245 |
} |
| 246 |
|
| 247 |
data.syms = malloc(allocsize); |
| 248 |
if (!data.syms) { |
| 249 |
break; |
| 250 |
} |
| 251 |
|
| 252 |
symbolcount = data.dynamic ? |
| 253 |
bfd_canonicalize_dynamic_symtab(bfdobj, data.syms) : bfd_canonicalize_symtab(bfdobj, data.syms); |
| 254 |
if (symbolcount < 0) { |
| 255 |
break; |
| 256 |
} |
| 257 |
|
| 258 |
bfd_map_over_sections(bfdobj, process_section, &data); |
| 259 |
} while(0); |
| 260 |
|
| 261 |
if (bfdobj) { |
| 262 |
bfd_close(bfdobj); |
| 263 |
free(data.syms); |
| 264 |
data.syms = NULL; |
| 265 |
} |
| 266 |
pthread_mutex_unlock(&bfd_mutex); |
| 267 |
|
| 268 |
|
| 269 |
if (!data.found) { |
| 270 |
snprintf(msg, sizeof(msg), "%s %s()", |
| 271 |
data.libname, |
| 272 |
S_OR(data.dli.dli_sname, "<unknown>")); |
| 273 |
AST_VECTOR_APPEND(return_strings, strdup(msg)); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
return return_strings; |
| 278 |
} |
| 279 |
|
| 280 |
#else |
| 281 |
struct ast_vector_string *__ast_bt_get_symbols(void **addresses, size_t num_frames) |
| 282 |
{ |
| 283 |
char **strings; |
| 284 |
struct ast_vector_string *return_strings; |
| 285 |
int i; |
| 286 |
|
| 287 |
return_strings = malloc(sizeof(struct ast_vector_string)); |
| 288 |
if (!return_strings) { |
| 289 |
return NULL; |
| 290 |
} |
| 291 |
if (AST_VECTOR_INIT(return_strings, num_frames)) { |
| 292 |
free(return_strings); |
| 293 |
return NULL; |
| 294 |
} |
| 295 |
|
| 296 |
strings = backtrace_symbols(addresses, num_frames); |
| 297 |
if (strings) { |
| 298 |
for (i = 0; i < num_frames; i++) { |
| 299 |
AST_VECTOR_APPEND(return_strings, strdup(strings[i])); |
| 300 |
} |
| 301 |
free(strings); |
| 302 |
} |
| 303 |
|
| 304 |
return return_strings; |
| 305 |
} |
| 306 |
#endif |
| 307 |
|
| 308 |
void __ast_bt_free_symbols(struct ast_vector_string *symbols) |
| 309 |
{ |
| 310 |
AST_VECTOR_CALLBACK_VOID(symbols, free); |
| 311 |
AST_VECTOR_PTR_FREE(symbols); |
| 312 |
} |
| 313 |
|
| 314 |
#endif |