1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/profiling/symbolizer/local_symbolizer.h"
18
19 #include <fcntl.h>
20
21 #include <cinttypes>
22 #include <limits>
23 #include <memory>
24 #include <optional>
25 #include <sstream>
26 #include <string>
27 #include <vector>
28
29 #include "perfetto/base/build_config.h"
30 #include "perfetto/base/compiler.h"
31 #include "perfetto/base/logging.h"
32 #include "perfetto/ext/base/file_utils.h"
33 #include "perfetto/ext/base/scoped_file.h"
34 #include "perfetto/ext/base/scoped_mmap.h"
35 #include "perfetto/ext/base/string_utils.h"
36 #include "src/profiling/symbolizer/elf.h"
37 #include "src/profiling/symbolizer/filesystem.h"
38
39 namespace perfetto {
40 namespace profiling {
41
42 // TODO(fmayer): Fix up name. This suggests it always returns a symbolizer or
43 // dies, which isn't the case.
LocalSymbolizerOrDie(std::vector<std::string> binary_path,const char * mode)44 std::unique_ptr<Symbolizer> LocalSymbolizerOrDie(
45 std::vector<std::string> binary_path,
46 const char* mode) {
47 std::unique_ptr<Symbolizer> symbolizer;
48
49 if (!binary_path.empty()) {
50 #if PERFETTO_BUILDFLAG(PERFETTO_LOCAL_SYMBOLIZER)
51 std::unique_ptr<BinaryFinder> finder;
52 if (!mode || strncmp(mode, "find", 4) == 0)
53 finder.reset(new LocalBinaryFinder(std::move(binary_path)));
54 else if (strncmp(mode, "index", 5) == 0)
55 finder.reset(new LocalBinaryIndexer(std::move(binary_path)));
56 else
57 PERFETTO_FATAL("Invalid symbolizer mode [find | index]: %s", mode);
58 symbolizer.reset(new LocalSymbolizer(std::move(finder)));
59 #else
60 base::ignore_result(mode);
61 PERFETTO_FATAL("This build does not support local symbolization.");
62 #endif
63 }
64 return symbolizer;
65 }
66
67 } // namespace profiling
68 } // namespace perfetto
69
70 #if PERFETTO_BUILDFLAG(PERFETTO_LOCAL_SYMBOLIZER)
71 #include "perfetto/ext/base/string_splitter.h"
72 #include "perfetto/ext/base/string_utils.h"
73 #include "perfetto/ext/base/utils.h"
74
75 #include <signal.h>
76 #include <sys/stat.h>
77 #include <sys/types.h>
78
79 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
80 constexpr const char* kDefaultSymbolizer = "llvm-symbolizer.exe";
81 #else
82 constexpr const char* kDefaultSymbolizer = "llvm-symbolizer";
83 #endif
84
85 namespace perfetto {
86 namespace profiling {
87
GetLines(std::function<int64_t (char *,size_t)> fn_read)88 std::vector<std::string> GetLines(
89 std::function<int64_t(char*, size_t)> fn_read) {
90 std::vector<std::string> lines;
91 char buffer[512];
92 int64_t rd = 0;
93 // Cache the partial line of the previous read.
94 std::string last_line;
95 while ((rd = fn_read(buffer, sizeof(buffer))) > 0) {
96 std::string data(buffer, static_cast<size_t>(rd));
97 // Create stream buffer of last partial line + new data
98 std::stringstream stream(last_line + data);
99 std::string line;
100 last_line = "";
101 while (std::getline(stream, line)) {
102 // Return from reading when we read an empty line.
103 if (line.empty()) {
104 return lines;
105 } else if (stream.eof()) {
106 // Cache off the partial line when we hit end of stream.
107 last_line += line;
108 break;
109 } else {
110 lines.push_back(line);
111 }
112 }
113 }
114 if (rd == -1) {
115 PERFETTO_ELOG("Failed to read data from subprocess.");
116 }
117 return lines;
118 }
119
120 namespace {
InRange(const void * base,size_t total_size,const void * ptr,size_t size)121 bool InRange(const void* base,
122 size_t total_size,
123 const void* ptr,
124 size_t size) {
125 return ptr >= base && static_cast<const char*>(ptr) + size <=
126 static_cast<const char*>(base) + total_size;
127 }
128
129 template <typename E>
GetLoadBias(void * mem,size_t size)130 std::optional<uint64_t> GetLoadBias(void* mem, size_t size) {
131 const typename E::Ehdr* ehdr = static_cast<typename E::Ehdr*>(mem);
132 if (!InRange(mem, size, ehdr, sizeof(typename E::Ehdr))) {
133 PERFETTO_ELOG("Corrupted ELF.");
134 return std::nullopt;
135 }
136 for (size_t i = 0; i < ehdr->e_phnum; ++i) {
137 typename E::Phdr* phdr = GetPhdr<E>(mem, ehdr, i);
138 if (!InRange(mem, size, phdr, sizeof(typename E::Phdr))) {
139 PERFETTO_ELOG("Corrupted ELF.");
140 return std::nullopt;
141 }
142 if (phdr->p_type == PT_LOAD && phdr->p_flags & PF_X) {
143 return phdr->p_vaddr - phdr->p_offset;
144 }
145 }
146 return 0u;
147 }
148
149 template <typename E>
GetBuildId(void * mem,size_t size)150 std::optional<std::string> GetBuildId(void* mem, size_t size) {
151 const typename E::Ehdr* ehdr = static_cast<typename E::Ehdr*>(mem);
152 if (!InRange(mem, size, ehdr, sizeof(typename E::Ehdr))) {
153 PERFETTO_ELOG("Corrupted ELF.");
154 return std::nullopt;
155 }
156 for (size_t i = 0; i < ehdr->e_shnum; ++i) {
157 typename E::Shdr* shdr = GetShdr<E>(mem, ehdr, i);
158 if (!InRange(mem, size, shdr, sizeof(typename E::Shdr))) {
159 PERFETTO_ELOG("Corrupted ELF.");
160 return std::nullopt;
161 }
162
163 if (shdr->sh_type != SHT_NOTE)
164 continue;
165
166 auto offset = shdr->sh_offset;
167 while (offset < shdr->sh_offset + shdr->sh_size) {
168 typename E::Nhdr* nhdr =
169 reinterpret_cast<typename E::Nhdr*>(static_cast<char*>(mem) + offset);
170
171 if (!InRange(mem, size, nhdr, sizeof(typename E::Nhdr))) {
172 PERFETTO_ELOG("Corrupted ELF.");
173 return std::nullopt;
174 }
175 if (nhdr->n_type == NT_GNU_BUILD_ID && nhdr->n_namesz == 4) {
176 char* name = reinterpret_cast<char*>(nhdr) + sizeof(*nhdr);
177 if (!InRange(mem, size, name, 4)) {
178 PERFETTO_ELOG("Corrupted ELF.");
179 return std::nullopt;
180 }
181 if (memcmp(name, "GNU", 3) == 0) {
182 const char* value = reinterpret_cast<char*>(nhdr) + sizeof(*nhdr) +
183 base::AlignUp<4>(nhdr->n_namesz);
184
185 if (!InRange(mem, size, value, nhdr->n_descsz)) {
186 PERFETTO_ELOG("Corrupted ELF.");
187 return std::nullopt;
188 }
189 return std::string(value, nhdr->n_descsz);
190 }
191 }
192 offset += sizeof(*nhdr) + base::AlignUp<4>(nhdr->n_namesz) +
193 base::AlignUp<4>(nhdr->n_descsz);
194 }
195 }
196 return std::nullopt;
197 }
198
SplitBuildID(const std::string & hex_build_id)199 std::string SplitBuildID(const std::string& hex_build_id) {
200 if (hex_build_id.size() < 3) {
201 PERFETTO_DFATAL_OR_ELOG("Invalid build-id (< 3 char) %s",
202 hex_build_id.c_str());
203 return {};
204 }
205
206 return hex_build_id.substr(0, 2) + "/" + hex_build_id.substr(2);
207 }
208
IsElf(const char * mem,size_t size)209 bool IsElf(const char* mem, size_t size) {
210 if (size <= EI_MAG3)
211 return false;
212 return (mem[EI_MAG0] == ELFMAG0 && mem[EI_MAG1] == ELFMAG1 &&
213 mem[EI_MAG2] == ELFMAG2 && mem[EI_MAG3] == ELFMAG3);
214 }
215
216 struct BuildIdAndLoadBias {
217 std::string build_id;
218 uint64_t load_bias;
219 };
220
GetBuildIdAndLoadBias(const char * fname,size_t size)221 std::optional<BuildIdAndLoadBias> GetBuildIdAndLoadBias(const char* fname,
222 size_t size) {
223 static_assert(EI_CLASS > EI_MAG3, "mem[EI_MAG?] accesses are in range.");
224 if (size <= EI_CLASS)
225 return std::nullopt;
226 base::ScopedMmap map = base::ReadMmapFilePart(fname, size);
227 if (!map.IsValid()) {
228 PERFETTO_PLOG("Failed to mmap %s", fname);
229 return std::nullopt;
230 }
231 char* mem = static_cast<char*>(map.data());
232
233 if (!IsElf(mem, size))
234 return std::nullopt;
235
236 std::optional<std::string> build_id;
237 std::optional<uint64_t> load_bias;
238 switch (mem[EI_CLASS]) {
239 case ELFCLASS32:
240 build_id = GetBuildId<Elf32>(mem, size);
241 load_bias = GetLoadBias<Elf32>(mem, size);
242 break;
243 case ELFCLASS64:
244 build_id = GetBuildId<Elf64>(mem, size);
245 load_bias = GetLoadBias<Elf64>(mem, size);
246 break;
247 default:
248 return std::nullopt;
249 }
250 if (build_id && load_bias) {
251 return BuildIdAndLoadBias{*build_id, *load_bias};
252 }
253 return std::nullopt;
254 }
255
BuildIdIndex(std::vector<std::string> dirs)256 std::map<std::string, FoundBinary> BuildIdIndex(std::vector<std::string> dirs) {
257 std::map<std::string, FoundBinary> result;
258 WalkDirectories(std::move(dirs), [&result](const char* fname, size_t size) {
259 char magic[EI_MAG3 + 1];
260 // Scope file access. On windows OpenFile opens an exclusive lock.
261 // This lock needs to be released before mapping the file.
262 {
263 base::ScopedFile fd(base::OpenFile(fname, O_RDONLY));
264 if (!fd) {
265 PERFETTO_PLOG("Failed to open %s", fname);
266 return;
267 }
268 ssize_t rd = base::Read(*fd, &magic, sizeof(magic));
269 if (rd != sizeof(magic)) {
270 PERFETTO_PLOG("Failed to read %s", fname);
271 return;
272 }
273 if (!IsElf(magic, static_cast<size_t>(rd))) {
274 PERFETTO_DLOG("%s not an ELF.", fname);
275 return;
276 }
277 }
278 std::optional<BuildIdAndLoadBias> build_id_and_load_bias =
279 GetBuildIdAndLoadBias(fname, size);
280 if (build_id_and_load_bias) {
281 result.emplace(build_id_and_load_bias->build_id,
282 FoundBinary{fname, build_id_and_load_bias->load_bias});
283 }
284 });
285 return result;
286 }
287
288 } // namespace
289
ParseLlvmSymbolizerLine(const std::string & line,std::string * file_name,uint32_t * line_no)290 bool ParseLlvmSymbolizerLine(const std::string& line,
291 std::string* file_name,
292 uint32_t* line_no) {
293 size_t col_pos = line.rfind(':');
294 if (col_pos == std::string::npos || col_pos == 0)
295 return false;
296 size_t row_pos = line.rfind(':', col_pos - 1);
297 if (row_pos == std::string::npos || row_pos == 0)
298 return false;
299 *file_name = line.substr(0, row_pos);
300 auto line_no_str = line.substr(row_pos + 1, col_pos - row_pos - 1);
301
302 std::optional<int32_t> opt_parsed_line_no = base::StringToInt32(line_no_str);
303 if (!opt_parsed_line_no || *opt_parsed_line_no < 0)
304 return false;
305 *line_no = static_cast<uint32_t>(*opt_parsed_line_no);
306 return true;
307 }
308
309 BinaryFinder::~BinaryFinder() = default;
310
LocalBinaryIndexer(std::vector<std::string> roots)311 LocalBinaryIndexer::LocalBinaryIndexer(std::vector<std::string> roots)
312 : buildid_to_file_(BuildIdIndex(std::move(roots))) {}
313
FindBinary(const std::string & abspath,const std::string & build_id)314 std::optional<FoundBinary> LocalBinaryIndexer::FindBinary(
315 const std::string& abspath,
316 const std::string& build_id) {
317 auto it = buildid_to_file_.find(build_id);
318 if (it != buildid_to_file_.end())
319 return it->second;
320 PERFETTO_ELOG("Could not find Build ID: %s (file %s).",
321 base::ToHex(build_id).c_str(), abspath.c_str());
322 return std::nullopt;
323 }
324
325 LocalBinaryIndexer::~LocalBinaryIndexer() = default;
326
LocalBinaryFinder(std::vector<std::string> roots)327 LocalBinaryFinder::LocalBinaryFinder(std::vector<std::string> roots)
328 : roots_(std::move(roots)) {}
329
FindBinary(const std::string & abspath,const std::string & build_id)330 std::optional<FoundBinary> LocalBinaryFinder::FindBinary(
331 const std::string& abspath,
332 const std::string& build_id) {
333 auto p = cache_.emplace(abspath, std::nullopt);
334 if (!p.second)
335 return p.first->second;
336
337 std::optional<FoundBinary>& cache_entry = p.first->second;
338
339 for (const std::string& root_str : roots_) {
340 cache_entry = FindBinaryInRoot(root_str, abspath, build_id);
341 if (cache_entry)
342 return cache_entry;
343 }
344 PERFETTO_ELOG("Could not find %s (Build ID: %s).", abspath.c_str(),
345 base::ToHex(build_id).c_str());
346 return cache_entry;
347 }
348
IsCorrectFile(const std::string & symbol_file,const std::string & build_id)349 std::optional<FoundBinary> LocalBinaryFinder::IsCorrectFile(
350 const std::string& symbol_file,
351 const std::string& build_id) {
352 if (!base::FileExists(symbol_file)) {
353 return std::nullopt;
354 }
355 // Openfile opens the file with an exclusive lock on windows.
356 std::optional<uint64_t> file_size = base::GetFileSize(symbol_file);
357 if (!file_size.has_value()) {
358 PERFETTO_PLOG("Failed to get file size %s", symbol_file.c_str());
359 return std::nullopt;
360 }
361
362 static_assert(sizeof(size_t) <= sizeof(uint64_t));
363 size_t size = static_cast<size_t>(
364 std::min<uint64_t>(std::numeric_limits<size_t>::max(), *file_size));
365
366 if (size == 0) {
367 return std::nullopt;
368 }
369
370 std::optional<BuildIdAndLoadBias> build_id_and_load_bias =
371 GetBuildIdAndLoadBias(symbol_file.c_str(), size);
372 if (!build_id_and_load_bias)
373 return std::nullopt;
374 if (build_id_and_load_bias->build_id != build_id) {
375 return std::nullopt;
376 }
377 return FoundBinary{symbol_file, build_id_and_load_bias->load_bias};
378 }
379
FindBinaryInRoot(const std::string & root_str,const std::string & abspath,const std::string & build_id)380 std::optional<FoundBinary> LocalBinaryFinder::FindBinaryInRoot(
381 const std::string& root_str,
382 const std::string& abspath,
383 const std::string& build_id) {
384 constexpr char kApkPrefix[] = "base.apk!";
385
386 std::string filename;
387 std::string dirname;
388
389 for (base::StringSplitter sp(abspath, '/'); sp.Next();) {
390 if (!dirname.empty())
391 dirname += "/";
392 dirname += filename;
393 filename = sp.cur_token();
394 }
395
396 // Return the first match for the following options:
397 // * absolute path of library file relative to root.
398 // * absolute path of library file relative to root, but with base.apk!
399 // removed from filename.
400 // * only filename of library file relative to root.
401 // * only filename of library file relative to root, but with base.apk!
402 // removed from filename.
403 // * in the subdirectory .build-id: the first two hex digits of the build-id
404 // as subdirectory, then the rest of the hex digits, with ".debug"appended.
405 // See
406 // https://fedoraproject.org/wiki/RolandMcGrath/BuildID#Find_files_by_build_ID
407 //
408 // For example, "/system/lib/base.apk!foo.so" with build id abcd1234,
409 // is looked for at
410 // * $ROOT/system/lib/base.apk!foo.so
411 // * $ROOT/system/lib/foo.so
412 // * $ROOT/base.apk!foo.so
413 // * $ROOT/foo.so
414 // * $ROOT/.build-id/ab/cd1234.debug
415
416 std::optional<FoundBinary> result;
417
418 std::string symbol_file = root_str + "/" + dirname + "/" + filename;
419 result = IsCorrectFile(symbol_file, build_id);
420 if (result) {
421 return result;
422 }
423
424 if (base::StartsWith(filename, kApkPrefix)) {
425 symbol_file = root_str + "/" + dirname + "/" +
426 filename.substr(sizeof(kApkPrefix) - 1);
427 result = IsCorrectFile(symbol_file, build_id);
428 if (result) {
429 return result;
430 }
431 }
432
433 symbol_file = root_str + "/" + filename;
434 result = IsCorrectFile(symbol_file, build_id);
435 if (result) {
436 return result;
437 }
438
439 if (base::StartsWith(filename, kApkPrefix)) {
440 symbol_file = root_str + "/" + filename.substr(sizeof(kApkPrefix) - 1);
441 result = IsCorrectFile(symbol_file, build_id);
442 if (result) {
443 return result;
444 }
445 }
446
447 std::string hex_build_id = base::ToHex(build_id.c_str(), build_id.size());
448 std::string split_hex_build_id = SplitBuildID(hex_build_id);
449 if (!split_hex_build_id.empty()) {
450 symbol_file =
451 root_str + "/" + ".build-id" + "/" + split_hex_build_id + ".debug";
452 result = IsCorrectFile(symbol_file, build_id);
453 if (result) {
454 return result;
455 }
456 }
457
458 return std::nullopt;
459 }
460
461 LocalBinaryFinder::~LocalBinaryFinder() = default;
462
LLVMSymbolizerProcess(const std::string & symbolizer_path)463 LLVMSymbolizerProcess::LLVMSymbolizerProcess(const std::string& symbolizer_path)
464 :
465 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
466 subprocess_(symbolizer_path, {}) {
467 }
468 #else
469 subprocess_(symbolizer_path, {"llvm-symbolizer"}) {
470 }
471 #endif
472
Symbolize(const std::string & binary,uint64_t address)473 std::vector<SymbolizedFrame> LLVMSymbolizerProcess::Symbolize(
474 const std::string& binary,
475 uint64_t address) {
476 std::vector<SymbolizedFrame> result;
477 base::StackString<1024> buffer("\"%s\" 0x%" PRIx64 "\n", binary.c_str(),
478 address);
479 if (subprocess_.Write(buffer.c_str(), buffer.len()) < 0) {
480 PERFETTO_ELOG("Failed to write to llvm-symbolizer.");
481 return result;
482 }
483 auto lines = GetLines([&](char* read_buffer, size_t buffer_size) {
484 return subprocess_.Read(read_buffer, buffer_size);
485 });
486 // llvm-symbolizer writes out records in the form of
487 // Foo(Bar*)
488 // foo.cc:123
489 // This is why we should always get a multiple of two number of lines.
490 PERFETTO_DCHECK(lines.size() % 2 == 0);
491 result.resize(lines.size() / 2);
492 for (size_t i = 0; i < lines.size(); ++i) {
493 SymbolizedFrame& cur = result[i / 2];
494 if (i % 2 == 0) {
495 cur.function_name = lines[i];
496 } else {
497 if (!ParseLlvmSymbolizerLine(lines[i], &cur.file_name, &cur.line)) {
498 PERFETTO_ELOG("Failed to parse llvm-symbolizer line: %s",
499 lines[i].c_str());
500 cur.file_name = "";
501 cur.line = 0;
502 }
503 }
504 }
505
506 for (auto it = result.begin(); it != result.end();) {
507 if (it->function_name == "??")
508 it = result.erase(it);
509 else
510 ++it;
511 }
512 return result;
513 }
Symbolize(const std::string & mapping_name,const std::string & build_id,uint64_t load_bias,const std::vector<uint64_t> & addresses)514 std::vector<std::vector<SymbolizedFrame>> LocalSymbolizer::Symbolize(
515 const std::string& mapping_name,
516 const std::string& build_id,
517 uint64_t load_bias,
518 const std::vector<uint64_t>& addresses) {
519 std::optional<FoundBinary> binary =
520 finder_->FindBinary(mapping_name, build_id);
521 if (!binary)
522 return {};
523 uint64_t load_bias_correction = 0;
524 if (binary->load_bias > load_bias) {
525 // On Android 10, there was a bug in libunwindstack that would incorrectly
526 // calculate the load_bias, and thus the relative PC. This would end up in
527 // frames that made no sense. We can fix this up after the fact if we
528 // detect this situation.
529 load_bias_correction = binary->load_bias - load_bias;
530 PERFETTO_LOG("Correcting load bias by %" PRIu64 " for %s",
531 load_bias_correction, mapping_name.c_str());
532 }
533 std::vector<std::vector<SymbolizedFrame>> result;
534 result.reserve(addresses.size());
535 for (uint64_t address : addresses)
536 result.emplace_back(llvm_symbolizer_.Symbolize(
537 binary->file_name, address + load_bias_correction));
538 return result;
539 }
540
LocalSymbolizer(const std::string & symbolizer_path,std::unique_ptr<BinaryFinder> finder)541 LocalSymbolizer::LocalSymbolizer(const std::string& symbolizer_path,
542 std::unique_ptr<BinaryFinder> finder)
543 : llvm_symbolizer_(symbolizer_path), finder_(std::move(finder)) {}
544
LocalSymbolizer(std::unique_ptr<BinaryFinder> finder)545 LocalSymbolizer::LocalSymbolizer(std::unique_ptr<BinaryFinder> finder)
546 : LocalSymbolizer(kDefaultSymbolizer, std::move(finder)) {}
547
548 LocalSymbolizer::~LocalSymbolizer() = default;
549
550 } // namespace profiling
551 } // namespace perfetto
552
553 #endif // PERFETTO_BUILDFLAG(PERFETTO_LOCAL_SYMBOLIZER)
554