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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
/*
* Copyright (C) Dmitry Volyntsev
* Copyright (C) NGINX, Inc.
*
* addr2line impementaton based upon the work by Jeff Muizelaar.
*
* A hacky replacement for backtrace_symbols in glibc
*
* backtrace_symbols in glibc looks up symbols using dladdr which is limited in
* the symbols that it sees. libbacktracesymbols opens the executable and
* shared libraries using libbfd and will look up backtrace information using
* the symbol table and the dwarf line information.
*
* Derived from addr2line.c from GNU Binutils by Jeff Muizelaar
*
* Copyright 2007 Jeff Muizelaar
*
* addr2line.c -- convert addresses to line number and function name
* Copyright 1997, 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
* Contributed by Ulrich Lauther <Ulrich.Lauther@mchp.siemens.de>
*
* This file was part of GNU Binutils.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#define _GNU_SOURCE
#include <njs_main.h>
#include <njs_addr2line.h>
#include <bfd.h>
#include <link.h>
typedef struct {
const char *file;
ElfW(Addr) address;
ElfW(Addr) base;
void *hdr;
} njs_file_match_t;
typedef struct {
bfd_vma pc;
const char *filename;
const char *functionname;
unsigned int line;
njs_bool_t found;
asymbol **syms;
} njs_translate_address_t;
static u_char *njs_process_file(u_char *buf, u_char *end, bfd_vma *addr,
const char *file_name);
static long njs_read_symtab(bfd *abfd, asymbol ***syms);
static u_char *njs_translate_address(u_char *buf, u_char *end, bfd_vma *addr,
bfd *abfd, asymbol **syms);
static void njs_find_address_in_section(bfd *abfd, asection *section,
void *data);
static int njs_find_matching_file(struct dl_phdr_info *info, size_t size,
void *data);
u_char *
_njs_addr2line(u_char *buf, u_char *end, void *address)
{
bfd_vma addr;
const char *fname;
njs_file_match_t match = { .address = (ElfW(Addr)) address };
bfd_init();
dl_iterate_phdr(njs_find_matching_file, &match);
fname = "/proc/self/exe";
if (match.file != NULL && njs_strlen(match.file)) {
fname = match.file;
}
addr = (ElfW(Addr)) address - match.base;
return njs_process_file(buf, end, &addr, fname);
}
static u_char *
njs_process_file(u_char *buf, u_char *end, bfd_vma *addr, const char *file_name)
{
bfd *abfd;
char **matching;
u_char *p;
asymbol **syms;
abfd = bfd_openr(file_name, NULL);
if (abfd == NULL) {
njs_stderror("%s: failed to open while looking for addr2line",
file_name);
return NULL;
}
if (bfd_check_format(abfd, bfd_archive)) {
njs_stderror("%s: can not get addresses from archive", file_name);
return NULL;
}
if (!bfd_check_format_matches(abfd, bfd_object, &matching)) {
njs_stderror("%s: bfd_check_format_matches() failed",
bfd_get_filename(abfd));
return NULL;
}
if (njs_read_symtab(abfd, &syms) <= 0) {
njs_stderror("%s: njs_read_symtab() failed",
bfd_get_filename(abfd));
return NULL;
}
p = njs_translate_address(buf, end, addr, abfd, syms);
if (syms != NULL) {
free(syms);
syms = NULL;
}
bfd_close(abfd);
return p;
}
static long
njs_read_symtab(bfd *abfd, asymbol ***syms)
{
long symcount;
unsigned size;
if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0) {
return 0;
}
symcount = bfd_read_minisymbols(abfd, 0, (PTR) syms, &size);
if (symcount == 0) {
symcount = bfd_read_minisymbols(abfd, 1 /* dynamic */,
(PTR) syms, &size);
}
return symcount;
}
static u_char *
njs_translate_address(u_char *buf, u_char *end, bfd_vma *addr, bfd *abfd,
asymbol **syms)
{
char *h;
const char *name;
njs_translate_address_t ctx;
ctx.pc = *addr;
ctx.found = 0;
ctx.syms = syms;
bfd_map_over_sections(abfd, njs_find_address_in_section, &ctx);
if (!ctx.found) {
return njs_sprintf(buf, end, "\?\? \t\?\?:0 [0x%p]", addr);
}
name = ctx.functionname;
if (name == NULL || *name == '\0') {
name = "??";
}
if (ctx.filename != NULL) {
h = strrchr(ctx.filename, '/');
if (h != NULL) {
ctx.filename = h + 1;
}
}
return njs_sprintf(buf, end, "%s() %s:%ud [0x%p]", name,
ctx.filename ? ctx.filename : "??", ctx.line, addr);
}
static void
njs_find_address_in_section(bfd *abfd, asection *section, void *data)
{
bfd_vma vma;
bfd_size_type size;
njs_translate_address_t *ctx;
ctx = data;
if (ctx->found) {
return;
}
if ((bfd_section_flags(section) & SEC_ALLOC) == 0) {
return;
}
vma = bfd_section_vma(section);
if (ctx->pc < vma) {
return;
}
size = bfd_section_size(section);
if (ctx->pc >= vma + size) {
return;
}
ctx->found = bfd_find_nearest_line(abfd, section, ctx->syms, ctx->pc - vma,
&ctx->filename, &ctx->functionname,
&ctx->line);
}
static int
njs_find_matching_file(struct dl_phdr_info *info, size_t size, void *data)
{
long n;
const ElfW(Phdr) *phdr;
ElfW(Addr) load_base = info->dlpi_addr;
njs_file_match_t *match = data;
/*
* This code is modeled from Gfind_proc_info-lsb.c:callback()
* from libunwind.
*/
phdr = info->dlpi_phdr;
for (n = info->dlpi_phnum; --n >= 0; phdr++) {
if (phdr->p_type == PT_LOAD) {
ElfW(Addr) vaddr = phdr->p_vaddr + load_base;
if (match->address >= vaddr
&& match->address < vaddr + phdr->p_memsz)
{
/* we found a match */
match->file = info->dlpi_name;
match->base = info->dlpi_addr;
}
}
}
return 0;
}
|