-
Notifications
You must be signed in to change notification settings - Fork 111
/
legacy_addr2line.cc
247 lines (221 loc) · 8.18 KB
/
legacy_addr2line.cc
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
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Class to derive inline stack.
#include "addr2line.h"
#include <string.h>
#include "base/logging.h"
#include "symbolize/bytereader.h"
#include "symbolize/dwarf2reader.h"
#include "symbolize/dwarf3ranges.h"
#include "symbolize/addr2line_inlinestack.h"
#include "symbolize/functioninfo.h"
#include "symbolize/elf_reader.h"
#include "symbol_map.h"
namespace {
void GetSection(const devtools_crosstool_autofdo::SectionMap §ions,
const char *section_name, const char **data_p, size_t *size_p) {
const char *data;
size_t size;
devtools_crosstool_autofdo::SectionMap::const_iterator section =
sections.find(section_name);
if (section == sections.end()) {
data = NULL;
size = 0;
} else {
data = section->second.first;
size = section->second.second;
}
if (data_p)
*data_p = data;
if (size_p)
*size_p = size;
}
} // namespace
namespace devtools_crosstool_autofdo {
Addr2line *Addr2line::Create(absl::string_view binary_name) {
Addr2line *addr2line = new Google3Addr2line(std::string(binary_name));
if (!addr2line->Prepare()) {
delete addr2line;
return NULL;
} else {
return addr2line;
}
}
Google3Addr2line::Google3Addr2line(const std::string& binary_name)
: Addr2line(binary_name), line_map_(new AddressToLineMap()),
inline_stack_handler_(NULL), elf_(new ElfReader(binary_name)) {}
Google3Addr2line::~Google3Addr2line() {
delete line_map_;
delete elf_;
if (inline_stack_handler_) {
delete inline_stack_handler_;
}
}
bool Google3Addr2line::Prepare() {
ByteReader reader(ENDIANNESS_LITTLE);
int width;
if (elf_->IsElf32File()) {
width = 4;
} else if (elf_->IsElf64File()) {
width = 8;
} else {
LOG(ERROR) << "'" << binary_name_ << "' is not an ELF file";
return false;
}
reader.SetAddressSize(width);
SectionMap sections;
const char *debug_section_names[] = {
".debug_line", ".debug_abbrev", ".debug_info", ".debug_str",
".debug_ranges", ".debug_addr", ".debug_rnglists", ".debug_line_str"
};
for (const char *section_name : debug_section_names) {
size_t section_size;
const char *section_data = elf_->GetSectionByName(section_name,
§ion_size);
if (section_data == NULL)
continue;
sections[section_name] = std::make_pair(section_data, section_size);
}
size_t debug_info_size = 0;
size_t debug_addr_size = 0;
size_t debug_ranges_size = 0;
size_t debug_rnglists_size = 0;
const char *debug_info_data = NULL;
const char *debug_addr_data = NULL;
const char *debug_ranges_data = NULL;
const char *debug_rnglists_data = NULL;
bool is_rnglists_section = false;
GetSection(sections, ".debug_info", &debug_info_data, &debug_info_size);
if (debug_info_data == NULL)
LOG(WARNING) << "File '" << binary_name_ << "' does not have .debug_info section.";
GetSection(sections, ".debug_addr", &debug_addr_data, &debug_addr_size);
GetSection(sections, ".debug_ranges", &debug_ranges_data, &debug_ranges_size);
GetSection(sections, ".debug_rnglists", &debug_rnglists_data, &debug_rnglists_size);
if (debug_ranges_data == NULL && debug_rnglists_data == NULL) {
LOG(WARNING) << "File '" << binary_name_ << "' does not have .debug_ranges nor .debug_rnglists sections.";
}
AddressRangeList debug_ranges(debug_ranges_data,
debug_ranges_size,
debug_rnglists_data,
debug_rnglists_size,
&reader,
debug_addr_data,
debug_addr_size);
inline_stack_handler_ = new InlineStackHandler(
&debug_ranges, sections, &reader, nullptr,
elf_->VaddrOfFirstLoadSegment());
// Extract the line information
// If .debug_info section is available, we will locate .debug_line using
// .debug_info. Otherwise, we'll iterate through .debug_line section,
// assuming that compilation units are stored continuously in it.
if (debug_info_size > 0) {
size_t debug_info_pos = 0;
while (debug_info_pos < debug_info_size) {
DirectoryVector dirs;
FileVector files;
CULineInfoHandler handler(&files, &dirs, line_map_, nullptr);
inline_stack_handler_->set_directory_names(&dirs);
inline_stack_handler_->set_file_names(&files);
inline_stack_handler_->set_line_handler(&handler);
CompilationUnit compilation_unit(
binary_name_, sections, debug_info_pos, &reader,
inline_stack_handler_);
debug_info_pos += compilation_unit.Start();
if (compilation_unit.malformed()) {
LOG(WARNING) << "File '" << binary_name_ << "' has mangled "
<< ".debug_info section.";
// If the compilation unit is malformed, we do not know how
// big it is, so it is only safe to give up.
break;
}
}
} else {
const char *data;
size_t size;
GetSection(sections, ".debug_line", &data, &size);
if (data) {
size_t pos = 0;
while (pos < size) {
DirectoryVector dirs;
FileVector files;
CULineInfoHandler handler(&files, &dirs, line_map_);
LineInfo line(data + pos, size - pos, &reader, &handler);
uint64_t read = line.Start();
if (line.malformed()) {
// If the debug_line section is malformed, we should stop
LOG(WARNING) << "File '" << binary_name_ << "' has mangled "
<< ".debug_line section.";
break;
}
if (!read) break;
pos += read;
}
}
else
LOG(WARNING) << "File '" << binary_name_ << "' does not have .debug_line section.";
}
inline_stack_handler_->PopulateSubprogramsByAddress();
return true;
}
void Google3Addr2line::GetInlineStack(uint64_t address,
SourceStack *stack) const {
AddressToLineMap::const_iterator iter = line_map_->upper_bound(address);
if (iter == line_map_->begin())
return;
--iter;
if (iter->second == 0)
return;
const LineIdentifier &LI = line_map_->GetLogical(iter->second);
if (LI.line == 0)
return;
const SubprogramInfo *subprog =
inline_stack_handler_->GetSubprogramForAddress(address);
const char *function_name = NULL;
uint32_t start_line = 0;
if (subprog != NULL) {
const SubprogramInfo *declaration =
inline_stack_handler_->GetDeclaration(subprog);
function_name = declaration->name().c_str();
start_line =
inline_stack_handler_->GetAbstractOrigin(subprog)->callsite_line();
if (start_line == 0)
start_line = declaration->callsite_line();
}
stack->push_back(SourceInfo(function_name,
LI.file.first,
LI.file.second,
start_line,
LI.line,
LI.discriminator));
while (subprog != NULL && subprog->inlined()) {
const SubprogramInfo *canonical_parent =
inline_stack_handler_->GetDeclaration(subprog->parent());
CHECK(subprog->parent() != NULL);
uint32_t start_line = inline_stack_handler_->GetAbstractOrigin(
subprog->parent())->callsite_line();
if (start_line == 0)
start_line = canonical_parent->callsite_line();
if (start_line == 0)
start_line = subprog->callsite_line();
stack->push_back(SourceInfo(
canonical_parent->name().c_str(),
subprog->callsite_directory(),
subprog->callsite_filename(),
start_line,
subprog->callsite_line(),
subprog->callsite_discr()));
subprog = subprog->parent();
}
}
} // namespace autofdo