xref: /aosp_15_r20/external/perfetto/src/profiling/symbolizer/local_symbolizer.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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 <charconv>
22 #include <cinttypes>
23 #include <limits>
24 #include <memory>
25 #include <optional>
26 #include <sstream>
27 #include <string>
28 #include <vector>
29 
30 #include "perfetto/base/build_config.h"
31 #include "perfetto/base/compiler.h"
32 #include "perfetto/base/logging.h"
33 #include "perfetto/ext/base/file_utils.h"
34 #include "perfetto/ext/base/scoped_file.h"
35 #include "perfetto/ext/base/scoped_mmap.h"
36 #include "perfetto/ext/base/string_utils.h"
37 #include "src/profiling/symbolizer/elf.h"
38 #include "src/profiling/symbolizer/filesystem.h"
39 
40 namespace perfetto {
41 namespace profiling {
42 
43 // TODO(fmayer): Fix up name. This suggests it always returns a symbolizer or
44 // dies, which isn't the case.
LocalSymbolizerOrDie(std::vector<std::string> binary_path,const char * mode)45 std::unique_ptr<Symbolizer> LocalSymbolizerOrDie(
46     std::vector<std::string> binary_path,
47     const char* mode) {
48   std::unique_ptr<Symbolizer> symbolizer;
49 
50   if (!binary_path.empty()) {
51 #if PERFETTO_BUILDFLAG(PERFETTO_LOCAL_SYMBOLIZER)
52     std::unique_ptr<BinaryFinder> finder;
53     if (!mode || strncmp(mode, "find", 4) == 0)
54       finder.reset(new LocalBinaryFinder(std::move(binary_path)));
55     else if (strncmp(mode, "index", 5) == 0)
56       finder.reset(new LocalBinaryIndexer(std::move(binary_path)));
57     else
58       PERFETTO_FATAL("Invalid symbolizer mode [find | index]: %s", mode);
59     symbolizer.reset(new LocalSymbolizer(std::move(finder)));
60 #else
61     base::ignore_result(mode);
62     PERFETTO_FATAL("This build does not support local symbolization.");
63 #endif
64   }
65   return symbolizer;
66 }
67 
68 }  // namespace profiling
69 }  // namespace perfetto
70 
71 #if PERFETTO_BUILDFLAG(PERFETTO_LOCAL_SYMBOLIZER)
72 #include "perfetto/ext/base/string_splitter.h"
73 #include "perfetto/ext/base/string_utils.h"
74 #include "perfetto/ext/base/utils.h"
75 
76 #include <signal.h>
77 #include <sys/stat.h>
78 #include <sys/types.h>
79 
80 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
81 constexpr const char* kDefaultSymbolizer = "llvm-symbolizer.exe";
82 #else
83 constexpr const char* kDefaultSymbolizer = "llvm-symbolizer";
84 #endif
85 
86 namespace perfetto {
87 namespace profiling {
88 
89 namespace {
90 
GetLine(std::function<int64_t (char *,size_t)> fn_read)91 std::string GetLine(std::function<int64_t(char*, size_t)> fn_read) {
92   std::string line;
93   char buffer[512];
94   int64_t rd = 0;
95   while ((rd = fn_read(buffer, sizeof(buffer))) > 0) {
96     std::string data(buffer, static_cast<size_t>(rd));
97     line += data;
98     if (line.back() == '\n') {
99       break;
100     }
101     // There should be no intermediate new lines in the read data.
102     PERFETTO_DCHECK(line.find('\n') == std::string::npos);
103   }
104   if (rd == -1) {
105     PERFETTO_ELOG("Failed to read data from subprocess.");
106   }
107   return line;
108 }
109 
InRange(const void * base,size_t total_size,const void * ptr,size_t size)110 bool InRange(const void* base,
111              size_t total_size,
112              const void* ptr,
113              size_t size) {
114   return ptr >= base && static_cast<const char*>(ptr) + size <=
115                             static_cast<const char*>(base) + total_size;
116 }
117 
118 template <typename E>
GetElfLoadBias(void * mem,size_t size)119 std::optional<uint64_t> GetElfLoadBias(void* mem, size_t size) {
120   const typename E::Ehdr* ehdr = static_cast<typename E::Ehdr*>(mem);
121   if (!InRange(mem, size, ehdr, sizeof(typename E::Ehdr))) {
122     PERFETTO_ELOG("Corrupted ELF.");
123     return std::nullopt;
124   }
125   for (size_t i = 0; i < ehdr->e_phnum; ++i) {
126     typename E::Phdr* phdr = GetPhdr<E>(mem, ehdr, i);
127     if (!InRange(mem, size, phdr, sizeof(typename E::Phdr))) {
128       PERFETTO_ELOG("Corrupted ELF.");
129       return std::nullopt;
130     }
131     if (phdr->p_type == PT_LOAD && phdr->p_flags & PF_X) {
132       return phdr->p_vaddr - phdr->p_offset;
133     }
134   }
135   return 0u;
136 }
137 
138 template <typename E>
GetElfBuildId(void * mem,size_t size)139 std::optional<std::string> GetElfBuildId(void* mem, size_t size) {
140   const typename E::Ehdr* ehdr = static_cast<typename E::Ehdr*>(mem);
141   if (!InRange(mem, size, ehdr, sizeof(typename E::Ehdr))) {
142     PERFETTO_ELOG("Corrupted ELF.");
143     return std::nullopt;
144   }
145   for (size_t i = 0; i < ehdr->e_shnum; ++i) {
146     typename E::Shdr* shdr = GetShdr<E>(mem, ehdr, i);
147     if (!InRange(mem, size, shdr, sizeof(typename E::Shdr))) {
148       PERFETTO_ELOG("Corrupted ELF.");
149       return std::nullopt;
150     }
151 
152     if (shdr->sh_type != SHT_NOTE)
153       continue;
154 
155     auto offset = shdr->sh_offset;
156     while (offset < shdr->sh_offset + shdr->sh_size) {
157       typename E::Nhdr* nhdr =
158           reinterpret_cast<typename E::Nhdr*>(static_cast<char*>(mem) + offset);
159 
160       if (!InRange(mem, size, nhdr, sizeof(typename E::Nhdr))) {
161         PERFETTO_ELOG("Corrupted ELF.");
162         return std::nullopt;
163       }
164       if (nhdr->n_type == NT_GNU_BUILD_ID && nhdr->n_namesz == 4) {
165         char* name = reinterpret_cast<char*>(nhdr) + sizeof(*nhdr);
166         if (!InRange(mem, size, name, 4)) {
167           PERFETTO_ELOG("Corrupted ELF.");
168           return std::nullopt;
169         }
170         if (memcmp(name, "GNU", 3) == 0) {
171           const char* value = reinterpret_cast<char*>(nhdr) + sizeof(*nhdr) +
172                               base::AlignUp<4>(nhdr->n_namesz);
173 
174           if (!InRange(mem, size, value, nhdr->n_descsz)) {
175             PERFETTO_ELOG("Corrupted ELF.");
176             return std::nullopt;
177           }
178           return std::string(value, nhdr->n_descsz);
179         }
180       }
181       offset += sizeof(*nhdr) + base::AlignUp<4>(nhdr->n_namesz) +
182                 base::AlignUp<4>(nhdr->n_descsz);
183     }
184   }
185   return std::nullopt;
186 }
187 
SplitBuildID(const std::string & hex_build_id)188 std::string SplitBuildID(const std::string& hex_build_id) {
189   if (hex_build_id.size() < 3) {
190     PERFETTO_DFATAL_OR_ELOG("Invalid build-id (< 3 char) %s",
191                             hex_build_id.c_str());
192     return {};
193   }
194 
195   return hex_build_id.substr(0, 2) + "/" + hex_build_id.substr(2);
196 }
197 
IsElf(const char * mem,size_t size)198 bool IsElf(const char* mem, size_t size) {
199   if (size <= EI_MAG3)
200     return false;
201   return (mem[EI_MAG0] == ELFMAG0 && mem[EI_MAG1] == ELFMAG1 &&
202           mem[EI_MAG2] == ELFMAG2 && mem[EI_MAG3] == ELFMAG3);
203 }
204 
205 constexpr uint32_t kMachO64Magic = 0xfeedfacf;
206 
IsMachO64(const char * mem,size_t size)207 bool IsMachO64(const char* mem, size_t size) {
208   if (size < sizeof(kMachO64Magic))
209     return false;
210   return memcmp(mem, &kMachO64Magic, sizeof(kMachO64Magic)) == 0;
211 }
212 
213 struct mach_header_64 {
214   uint32_t magic;      /* mach magic number identifier */
215   int32_t cputype;     /* cpu specifier */
216   int32_t cpusubtype;  /* machine specifier */
217   uint32_t filetype;   /* type of file */
218   uint32_t ncmds;      /* number of load commands */
219   uint32_t sizeofcmds; /* the size of all the load commands */
220   uint32_t flags;      /* flags */
221   uint32_t reserved;   /* reserved */
222 };
223 
224 struct load_command {
225   uint32_t cmd;     /* type of load command */
226   uint32_t cmdsize; /* total size of command in bytes */
227 };
228 
229 struct segment_64_command {
230   uint32_t cmd;      /* LC_SEGMENT_64 */
231   uint32_t cmdsize;  /* includes sizeof section_64 structs */
232   char segname[16];  /* segment name */
233   uint64_t vmaddr;   /* memory address of this segment */
234   uint64_t vmsize;   /* memory size of this segment */
235   uint64_t fileoff;  /* file offset of this segment */
236   uint64_t filesize; /* amount to map from the file */
237   uint32_t maxprot;  /* maximum VM protection */
238   uint32_t initprot; /* initial VM protection */
239   uint32_t nsects;   /* number of sections in segment */
240   uint32_t flags;    /* flags */
241 };
242 
243 struct BinaryInfo {
244   std::string build_id;
245   uint64_t load_bias;
246   BinaryType type;
247 };
248 
GetMachOBinaryInfo(char * mem,size_t size)249 std::optional<BinaryInfo> GetMachOBinaryInfo(char* mem, size_t size) {
250   if (size < sizeof(mach_header_64))
251     return {};
252 
253   mach_header_64 header;
254   memcpy(&header, mem, sizeof(mach_header_64));
255 
256   if (size < sizeof(mach_header_64) + header.sizeofcmds)
257     return {};
258 
259   std::optional<std::string> build_id;
260   uint64_t load_bias = 0;
261 
262   char* pcmd = mem + sizeof(mach_header_64);
263   char* pcmds_end = pcmd + header.sizeofcmds;
264   while (pcmd < pcmds_end) {
265     load_command cmd_header;
266     memcpy(&cmd_header, pcmd, sizeof(load_command));
267 
268     constexpr uint32_t LC_SEGMENT_64 = 0x19;
269     constexpr uint32_t LC_UUID = 0x1b;
270 
271     switch (cmd_header.cmd) {
272       case LC_UUID: {
273         build_id = std::string(pcmd + sizeof(load_command),
274                                cmd_header.cmdsize - sizeof(load_command));
275         break;
276       }
277       case LC_SEGMENT_64: {
278         segment_64_command seg_cmd;
279         memcpy(&seg_cmd, pcmd, sizeof(segment_64_command));
280         if (strcmp(seg_cmd.segname, "__TEXT") == 0) {
281           load_bias = seg_cmd.vmaddr;
282         }
283         break;
284       }
285       default:
286         break;
287     }
288 
289     pcmd += cmd_header.cmdsize;
290   }
291 
292   if (build_id) {
293     constexpr uint32_t MH_DSYM = 0xa;
294     BinaryType type = header.filetype == MH_DSYM ? BinaryType::kMachODsym
295                                                  : BinaryType::kMachO;
296     return BinaryInfo{*build_id, load_bias, type};
297   }
298   return {};
299 }
300 
GetBinaryInfo(const char * fname,size_t size)301 std::optional<BinaryInfo> GetBinaryInfo(const char* fname, size_t size) {
302   static_assert(EI_CLASS > EI_MAG3, "mem[EI_MAG?] accesses are in range.");
303   if (size <= EI_CLASS)
304     return std::nullopt;
305   base::ScopedMmap map = base::ReadMmapFilePart(fname, size);
306   if (!map.IsValid()) {
307     PERFETTO_PLOG("Failed to mmap %s", fname);
308     return std::nullopt;
309   }
310   char* mem = static_cast<char*>(map.data());
311 
312   std::optional<std::string> build_id;
313   std::optional<uint64_t> load_bias;
314   if (IsElf(mem, size)) {
315     switch (mem[EI_CLASS]) {
316       case ELFCLASS32:
317         build_id = GetElfBuildId<Elf32>(mem, size);
318         load_bias = GetElfLoadBias<Elf32>(mem, size);
319         break;
320       case ELFCLASS64:
321         build_id = GetElfBuildId<Elf64>(mem, size);
322         load_bias = GetElfLoadBias<Elf64>(mem, size);
323         break;
324       default:
325         return std::nullopt;
326     }
327     if (build_id && load_bias) {
328       return BinaryInfo{*build_id, *load_bias, BinaryType::kElf};
329     }
330   } else if (IsMachO64(mem, size)) {
331     return GetMachOBinaryInfo(mem, size);
332   }
333   return std::nullopt;
334 }
335 
BuildIdIndex(std::vector<std::string> dirs)336 std::map<std::string, FoundBinary> BuildIdIndex(std::vector<std::string> dirs) {
337   std::map<std::string, FoundBinary> result;
338   WalkDirectories(std::move(dirs), [&result](const char* fname, size_t size) {
339     static_assert(EI_MAG3 + 1 == sizeof(kMachO64Magic));
340     char magic[EI_MAG3 + 1];
341     // Scope file access. On windows OpenFile opens an exclusive lock.
342     // This lock needs to be released before mapping the file.
343     {
344       base::ScopedFile fd(base::OpenFile(fname, O_RDONLY));
345       if (!fd) {
346         PERFETTO_PLOG("Failed to open %s", fname);
347         return;
348       }
349       ssize_t rd = base::Read(*fd, &magic, sizeof(magic));
350       if (rd != sizeof(magic)) {
351         PERFETTO_PLOG("Failed to read %s", fname);
352         return;
353       }
354       if (!IsElf(magic, static_cast<size_t>(rd)) &&
355           !IsMachO64(magic, static_cast<size_t>(rd))) {
356         PERFETTO_DLOG("%s not an ELF or Mach-O 64.", fname);
357         return;
358       }
359     }
360     std::optional<BinaryInfo> binary_info = GetBinaryInfo(fname, size);
361     if (!binary_info) {
362       PERFETTO_DLOG("Failed to extract build id from %s.", fname);
363       return;
364     }
365     auto it = result.emplace(
366         binary_info->build_id,
367         FoundBinary{fname, binary_info->load_bias, binary_info->type});
368 
369     // If there was already an existing FoundBinary, the emplace wouldn't insert
370     // anything. But, for Mac binaries, we prefer dSYM files over the original
371     // binary, so make sure these overwrite the FoundBinary entry.
372     bool has_existing = it.second == false;
373     if (has_existing) {
374       if (it.first->second.type == BinaryType::kMachO &&
375           binary_info->type == BinaryType::kMachODsym) {
376         PERFETTO_LOG("Overwriting index entry for %s to %s.",
377                      base::ToHex(binary_info->build_id).c_str(), fname);
378         it.first->second =
379             FoundBinary{fname, binary_info->load_bias, binary_info->type};
380       } else {
381         PERFETTO_DLOG("Ignoring %s, index entry for %s already exists.", fname,
382                       base::ToHex(binary_info->build_id).c_str());
383       }
384     } else {
385       PERFETTO_LOG("Indexed: %s (%s)", fname,
386                    base::ToHex(binary_info->build_id).c_str());
387     }
388   });
389   return result;
390 }
391 
ParseJsonString(const char * & it,const char * end,std::string * out)392 bool ParseJsonString(const char*& it, const char* end, std::string* out) {
393   *out = "";
394   if (it == end) {
395     return false;
396   }
397   if (*it++ != '"') {
398     return false;
399   }
400   while (true) {
401     if (it == end) {
402       return false;
403     }
404     char c = *it++;
405     if (c == '"') {
406       return true;
407     }
408     if (c == '\\') {
409       if (it == end) {
410         return false;
411       }
412       c = *it++;
413       switch (c) {
414         case '"':
415         case '\\':
416         case '/':
417           out->push_back(c);
418           break;
419         case 'b':
420           out->push_back('\b');
421           break;
422         case 'f':
423           out->push_back('\f');
424           break;
425         case 'n':
426           out->push_back('\n');
427           break;
428         case 'r':
429           out->push_back('\r');
430           break;
431         case 't':
432           out->push_back('\t');
433           break;
434         // Pass-through \u escape codes without re-encoding to utf-8, for
435         // simplicity.
436         case 'u':
437           out->push_back('\\');
438           out->push_back('u');
439           break;
440         default:
441           return false;
442       }
443     } else {
444       out->push_back(c);
445     }
446   }
447 }
448 
ParseJsonNumber(const char * & it,const char * end,double * out)449 bool ParseJsonNumber(const char*& it, const char* end, double* out) {
450   bool is_minus = false;
451   double ret = 0;
452   if (it == end) {
453     return false;
454   }
455   if (*it == '-') {
456     ++it;
457     is_minus = true;
458   }
459   while (true) {
460     if (it == end) {
461       return false;
462     }
463     char c = *it++;
464     if (isdigit(c)) {
465       ret = ret * 10 + (c - '0');
466     } else if (c == 'e') {
467       // Scientific syntax is not supported.
468       return false;
469     } else {
470       // Unwind the iterator to point at the end of the number.
471       it--;
472       break;
473     }
474   }
475   *out = is_minus ? -ret : ret;
476   return true;
477 }
478 
ParseJsonArray(const char * & it,const char * end,std::function<bool (const char * &,const char *)> process_value)479 bool ParseJsonArray(
480     const char*& it,
481     const char* end,
482     std::function<bool(const char*&, const char*)> process_value) {
483   if (it == end) {
484     return false;
485   }
486   char c = *it++;
487   if (c != '[') {
488     return false;
489   }
490   while (true) {
491     if (!process_value(it, end)) {
492       return false;
493     }
494     if (it == end) {
495       return false;
496     }
497     c = *it++;
498     if (c == ']') {
499       return true;
500     }
501     if (c != ',') {
502       return false;
503     }
504   }
505 }
506 
ParseJsonObject(const char * & it,const char * end,std::function<bool (const char * &,const char *,const std::string &)> process_value)507 bool ParseJsonObject(
508     const char*& it,
509     const char* end,
510     std::function<bool(const char*&, const char*, const std::string&)>
511         process_value) {
512   if (it == end) {
513     return false;
514   }
515   char c = *it++;
516   if (c != '{') {
517     return false;
518   }
519   while (true) {
520     std::string key;
521     if (!ParseJsonString(it, end, &key)) {
522       return false;
523     }
524     if (*it++ != ':') {
525       return false;
526     }
527     if (!process_value(it, end, key)) {
528       return false;
529     }
530     if (it == end) {
531       return false;
532     }
533     c = *it++;
534     if (c == '}') {
535       return true;
536     }
537     if (c != ',') {
538       return false;
539     }
540   }
541 }
542 
SkipJsonValue(const char * & it,const char * end)543 bool SkipJsonValue(const char*& it, const char* end) {
544   if (it == end) {
545     return false;
546   }
547   char c = *it;
548   if (c == '"') {
549     std::string ignored;
550     return ParseJsonString(it, end, &ignored);
551   }
552   if (isdigit(c) || c == '-') {
553     double ignored;
554     return ParseJsonNumber(it, end, &ignored);
555   }
556   if (c == '[') {
557     return ParseJsonArray(it, end, [](const char*& it, const char* end) {
558       return SkipJsonValue(it, end);
559     });
560   }
561   if (c == '{') {
562     return ParseJsonObject(
563         it, end, [](const char*& it, const char* end, const std::string&) {
564           return SkipJsonValue(it, end);
565         });
566   }
567   return false;
568 }
569 
570 }  // namespace
571 
ParseLlvmSymbolizerJsonLine(const std::string & line,std::vector<SymbolizedFrame> * result)572 bool ParseLlvmSymbolizerJsonLine(const std::string& line,
573                                  std::vector<SymbolizedFrame>* result) {
574   // Parse Json of the format:
575   // ```
576   // {"Address":"0x1b72f","ModuleName":"...","Symbol":[{"Column":0,
577   // "Discriminator":0,"FileName":"...","FunctionName":"...","Line":0,
578   // "StartAddress":"","StartFileName":"...","StartLine":0},...]}
579   // ```
580   const char* it = line.data();
581   const char* end = it + line.size();
582   return ParseJsonObject(
583       it, end, [&](const char*& it, const char* end, const std::string& key) {
584         if (key == "Symbol") {
585           return ParseJsonArray(it, end, [&](const char*& it, const char* end) {
586             SymbolizedFrame frame;
587             if (!ParseJsonObject(
588                     it, end,
589                     [&](const char*& it, const char* end,
590                         const std::string& key) {
591                       if (key == "FileName") {
592                         return ParseJsonString(it, end, &frame.file_name);
593                       }
594                       if (key == "FunctionName") {
595                         return ParseJsonString(it, end, &frame.function_name);
596                       }
597                       if (key == "Line") {
598                         double number;
599                         if (!ParseJsonNumber(it, end, &number)) {
600                           return false;
601                         }
602                         frame.line = static_cast<unsigned int>(number);
603                         return true;
604                       }
605                       return SkipJsonValue(it, end);
606                     })) {
607               return false;
608             }
609             // Use "??" for empty filenames, to match non-JSON output.
610             if (frame.file_name.empty()) {
611               frame.file_name = "??";
612             }
613             result->push_back(frame);
614             return true;
615           });
616         }
617         if (key == "Error") {
618           std::string message;
619           if (!ParseJsonObject(it, end,
620                                [&](const char*& it, const char* end,
621                                    const std::string& key) {
622                                  if (key == "Message") {
623                                    return ParseJsonString(it, end, &message);
624                                  }
625                                  return SkipJsonValue(it, end);
626                                })) {
627             return false;
628           }
629           PERFETTO_ELOG("Failed to symbolize: %s.", message.c_str());
630           return true;
631         }
632         return SkipJsonValue(it, end);
633       });
634 }
635 
636 BinaryFinder::~BinaryFinder() = default;
637 
LocalBinaryIndexer(std::vector<std::string> roots)638 LocalBinaryIndexer::LocalBinaryIndexer(std::vector<std::string> roots)
639     : buildid_to_file_(BuildIdIndex(std::move(roots))) {}
640 
FindBinary(const std::string & abspath,const std::string & build_id)641 std::optional<FoundBinary> LocalBinaryIndexer::FindBinary(
642     const std::string& abspath,
643     const std::string& build_id) {
644   auto it = buildid_to_file_.find(build_id);
645   if (it != buildid_to_file_.end())
646     return it->second;
647   PERFETTO_ELOG("Could not find Build ID: %s (file %s).",
648                 base::ToHex(build_id).c_str(), abspath.c_str());
649   return std::nullopt;
650 }
651 
652 LocalBinaryIndexer::~LocalBinaryIndexer() = default;
653 
LocalBinaryFinder(std::vector<std::string> roots)654 LocalBinaryFinder::LocalBinaryFinder(std::vector<std::string> roots)
655     : roots_(std::move(roots)) {}
656 
FindBinary(const std::string & abspath,const std::string & build_id)657 std::optional<FoundBinary> LocalBinaryFinder::FindBinary(
658     const std::string& abspath,
659     const std::string& build_id) {
660   auto p = cache_.emplace(abspath, std::nullopt);
661   if (!p.second)
662     return p.first->second;
663 
664   std::optional<FoundBinary>& cache_entry = p.first->second;
665 
666   // Try the absolute path first.
667   if (base::StartsWith(abspath, "/")) {
668     cache_entry = IsCorrectFile(abspath, build_id);
669     if (cache_entry)
670       return cache_entry;
671   }
672 
673   for (const std::string& root_str : roots_) {
674     cache_entry = FindBinaryInRoot(root_str, abspath, build_id);
675     if (cache_entry)
676       return cache_entry;
677   }
678   PERFETTO_ELOG("Could not find %s (Build ID: %s).", abspath.c_str(),
679                 base::ToHex(build_id).c_str());
680   return cache_entry;
681 }
682 
IsCorrectFile(const std::string & symbol_file,const std::string & build_id)683 std::optional<FoundBinary> LocalBinaryFinder::IsCorrectFile(
684     const std::string& symbol_file,
685     const std::string& build_id) {
686   if (!base::FileExists(symbol_file)) {
687     return std::nullopt;
688   }
689   // Openfile opens the file with an exclusive lock on windows.
690   std::optional<uint64_t> file_size = base::GetFileSize(symbol_file);
691   if (!file_size.has_value()) {
692     PERFETTO_PLOG("Failed to get file size %s", symbol_file.c_str());
693     return std::nullopt;
694   }
695 
696   static_assert(sizeof(size_t) <= sizeof(uint64_t));
697   size_t size = static_cast<size_t>(
698       std::min<uint64_t>(std::numeric_limits<size_t>::max(), *file_size));
699 
700   if (size == 0) {
701     return std::nullopt;
702   }
703 
704   std::optional<BinaryInfo> binary_info =
705       GetBinaryInfo(symbol_file.c_str(), size);
706   if (!binary_info)
707     return std::nullopt;
708   if (binary_info->build_id != build_id) {
709     return std::nullopt;
710   }
711   return FoundBinary{symbol_file, binary_info->load_bias, binary_info->type};
712 }
713 
FindBinaryInRoot(const std::string & root_str,const std::string & abspath,const std::string & build_id)714 std::optional<FoundBinary> LocalBinaryFinder::FindBinaryInRoot(
715     const std::string& root_str,
716     const std::string& abspath,
717     const std::string& build_id) {
718   constexpr char kApkPrefix[] = "base.apk!";
719 
720   std::string filename;
721   std::string dirname;
722 
723   for (base::StringSplitter sp(abspath, '/'); sp.Next();) {
724     if (!dirname.empty())
725       dirname += "/";
726     dirname += filename;
727     filename = sp.cur_token();
728   }
729 
730   // Return the first match for the following options:
731   // * absolute path of library file relative to root.
732   // * absolute path of library file relative to root, but with base.apk!
733   //   removed from filename.
734   // * only filename of library file relative to root.
735   // * only filename of library file relative to root, but with base.apk!
736   //   removed from filename.
737   // * in the subdirectory .build-id: the first two hex digits of the build-id
738   //   as subdirectory, then the rest of the hex digits, with ".debug"appended.
739   //   See
740   //   https://fedoraproject.org/wiki/RolandMcGrath/BuildID#Find_files_by_build_ID
741   //
742   // For example, "/system/lib/base.apk!foo.so" with build id abcd1234,
743   // is looked for at
744   // * $ROOT/system/lib/base.apk!foo.so
745   // * $ROOT/system/lib/foo.so
746   // * $ROOT/base.apk!foo.so
747   // * $ROOT/foo.so
748   // * $ROOT/.build-id/ab/cd1234.debug
749 
750   std::optional<FoundBinary> result;
751 
752   std::string symbol_file = root_str + "/" + dirname + "/" + filename;
753   result = IsCorrectFile(symbol_file, build_id);
754   if (result) {
755     return result;
756   }
757 
758   if (base::StartsWith(filename, kApkPrefix)) {
759     symbol_file = root_str + "/" + dirname + "/" +
760                   filename.substr(sizeof(kApkPrefix) - 1);
761     result = IsCorrectFile(symbol_file, build_id);
762     if (result) {
763       return result;
764     }
765   }
766 
767   symbol_file = root_str + "/" + filename;
768   result = IsCorrectFile(symbol_file, build_id);
769   if (result) {
770     return result;
771   }
772 
773   if (base::StartsWith(filename, kApkPrefix)) {
774     symbol_file = root_str + "/" + filename.substr(sizeof(kApkPrefix) - 1);
775     result = IsCorrectFile(symbol_file, build_id);
776     if (result) {
777       return result;
778     }
779   }
780 
781   std::string hex_build_id = base::ToHex(build_id.c_str(), build_id.size());
782   std::string split_hex_build_id = SplitBuildID(hex_build_id);
783   if (!split_hex_build_id.empty()) {
784     symbol_file =
785         root_str + "/" + ".build-id" + "/" + split_hex_build_id + ".debug";
786     result = IsCorrectFile(symbol_file, build_id);
787     if (result) {
788       return result;
789     }
790   }
791 
792   return std::nullopt;
793 }
794 
795 LocalBinaryFinder::~LocalBinaryFinder() = default;
796 
LLVMSymbolizerProcess(const std::string & symbolizer_path)797 LLVMSymbolizerProcess::LLVMSymbolizerProcess(const std::string& symbolizer_path)
798     :
799 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
800       subprocess_(symbolizer_path, {"--output-style=JSON"}) {
801 }
802 #else
803       subprocess_(symbolizer_path, {"llvm-symbolizer", "--output-style=JSON"}) {
804 }
805 #endif
806 
Symbolize(const std::string & binary,uint64_t address)807 std::vector<SymbolizedFrame> LLVMSymbolizerProcess::Symbolize(
808     const std::string& binary,
809     uint64_t address) {
810   std::vector<SymbolizedFrame> result;
811   base::StackString<1024> buffer("\"%s\" 0x%" PRIx64 "\n", binary.c_str(),
812                                  address);
813   if (subprocess_.Write(buffer.c_str(), buffer.len()) < 0) {
814     PERFETTO_ELOG("Failed to write to llvm-symbolizer.");
815     return result;
816   }
817   auto line = GetLine([&](char* read_buffer, size_t buffer_size) {
818     return subprocess_.Read(read_buffer, buffer_size);
819   });
820   // llvm-symbolizer writes out records as one JSON per line.
821   if (!ParseLlvmSymbolizerJsonLine(line, &result)) {
822     PERFETTO_ELOG("Failed to parse llvm-symbolizer JSON: %s", line.c_str());
823     return {};
824   }
825   return result;
826 }
Symbolize(const std::string & mapping_name,const std::string & build_id,uint64_t load_bias,const std::vector<uint64_t> & addresses)827 std::vector<std::vector<SymbolizedFrame>> LocalSymbolizer::Symbolize(
828     const std::string& mapping_name,
829     const std::string& build_id,
830     uint64_t load_bias,
831     const std::vector<uint64_t>& addresses) {
832   std::optional<FoundBinary> binary =
833       finder_->FindBinary(mapping_name, build_id);
834   if (!binary)
835     return {};
836   uint64_t load_bias_correction = 0;
837   if (binary->load_bias > load_bias) {
838     // On Android 10, there was a bug in libunwindstack that would incorrectly
839     // calculate the load_bias, and thus the relative PC. This would end up in
840     // frames that made no sense. We can fix this up after the fact if we
841     // detect this situation.
842     load_bias_correction = binary->load_bias - load_bias;
843     PERFETTO_LOG("Correcting load bias by %" PRIu64 " for %s",
844                  load_bias_correction, mapping_name.c_str());
845   }
846   std::vector<std::vector<SymbolizedFrame>> result;
847   result.reserve(addresses.size());
848   for (uint64_t address : addresses)
849     result.emplace_back(llvm_symbolizer_.Symbolize(
850         binary->file_name, address + load_bias_correction));
851   return result;
852 }
853 
LocalSymbolizer(const std::string & symbolizer_path,std::unique_ptr<BinaryFinder> finder)854 LocalSymbolizer::LocalSymbolizer(const std::string& symbolizer_path,
855                                  std::unique_ptr<BinaryFinder> finder)
856     : llvm_symbolizer_(symbolizer_path), finder_(std::move(finder)) {}
857 
LocalSymbolizer(std::unique_ptr<BinaryFinder> finder)858 LocalSymbolizer::LocalSymbolizer(std::unique_ptr<BinaryFinder> finder)
859     : LocalSymbolizer(kDefaultSymbolizer, std::move(finder)) {}
860 
861 LocalSymbolizer::~LocalSymbolizer() = default;
862 
863 }  // namespace profiling
864 }  // namespace perfetto
865 
866 #endif  // PERFETTO_BUILDFLAG(PERFETTO_LOCAL_SYMBOLIZER)
867