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