• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright (C) 2019 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include "perfetto/base/build_config.h"
19 
20 // This translation unit is built only on Linux. See //gn/BUILD.gn.
21 #if PERFETTO_BUILDFLAG(PERFETTO_LOCAL_SYMBOLIZER)
22 
23 #include "src/profiling/symbolizer/local_symbolizer.h"
24 
25 #include "perfetto/ext/base/string_splitter.h"
26 #include "perfetto/ext/base/string_utils.h"
27 #include "perfetto/ext/base/utils.h"
28 
29 #include <elf.h>
30 #include <inttypes.h>
31 #include <sys/mman.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <sys/wait.h>
35 #include <unistd.h>
36 
37 namespace perfetto {
38 namespace profiling {
39 
40 namespace {
41 
GetLines(FILE * f)42 std::vector<std::string> GetLines(FILE* f) {
43   std::vector<std::string> lines;
44   size_t n = 0;
45   char* line = nullptr;
46   ssize_t rd = 0;
47   do {
48     rd = getline(&line, &n, f);
49     // Do not read empty line that terminates the output.
50     if (rd > 1) {
51       // Remove newline character.
52       PERFETTO_DCHECK(line[rd - 1] == '\n');
53       line[rd - 1] = '\0';
54       lines.emplace_back(line);
55     }
56     free(line);
57     line = nullptr;
58     n = 0;
59   } while (rd > 1);
60   return lines;
61 }
62 
63 struct Elf32 {
64   using Ehdr = Elf32_Ehdr;
65   using Shdr = Elf32_Shdr;
66   using Nhdr = Elf32_Nhdr;
67 };
68 
69 struct Elf64 {
70   using Ehdr = Elf64_Ehdr;
71   using Shdr = Elf64_Shdr;
72   using Nhdr = Elf64_Nhdr;
73 };
74 
75 template <typename E>
GetShdr(void * mem,const typename E::Ehdr * ehdr,size_t i)76 typename E::Shdr* GetShdr(void* mem, const typename E::Ehdr* ehdr, size_t i) {
77   return reinterpret_cast<typename E::Shdr*>(
78       static_cast<char*>(mem) + ehdr->e_shoff + i * sizeof(typename E::Shdr));
79 }
80 
InRange(const void * base,size_t total_size,const void * ptr,size_t size)81 bool InRange(const void* base,
82              size_t total_size,
83              const void* ptr,
84              size_t size) {
85   return ptr >= base && static_cast<const char*>(ptr) + size <=
86                             static_cast<const char*>(base) + total_size;
87 }
88 
89 template <typename E>
GetBuildId(void * mem,size_t size)90 base::Optional<std::string> GetBuildId(void* mem, size_t size) {
91   const typename E::Ehdr* ehdr = static_cast<typename E::Ehdr*>(mem);
92   if (!InRange(mem, size, ehdr, sizeof(typename E::Ehdr))) {
93     PERFETTO_ELOG("Corrupted ELF.");
94     return base::nullopt;
95   }
96   for (size_t i = 0; i < ehdr->e_shnum; ++i) {
97     typename E::Shdr* shdr = GetShdr<E>(mem, ehdr, i);
98     if (!InRange(mem, size, shdr, sizeof(typename E::Shdr))) {
99       PERFETTO_ELOG("Corrupted ELF.");
100       return base::nullopt;
101     }
102 
103     if (shdr->sh_type != SHT_NOTE)
104       continue;
105 
106     auto offset = shdr->sh_offset;
107     while (offset < shdr->sh_offset + shdr->sh_size) {
108       typename E::Nhdr* nhdr =
109           reinterpret_cast<typename E::Nhdr*>(static_cast<char*>(mem) + offset);
110 
111       if (!InRange(mem, size, nhdr, sizeof(typename E::Nhdr))) {
112         PERFETTO_ELOG("Corrupted ELF.");
113         return base::nullopt;
114       }
115       if (nhdr->n_type == NT_GNU_BUILD_ID && nhdr->n_namesz == 4) {
116         char* name = reinterpret_cast<char*>(nhdr) + sizeof(*nhdr);
117         if (!InRange(mem, size, name, 4)) {
118           PERFETTO_ELOG("Corrupted ELF.");
119           return base::nullopt;
120         }
121         if (memcmp(name, "GNU", 3) == 0) {
122           const char* value = reinterpret_cast<char*>(nhdr) + sizeof(*nhdr) +
123                               base::AlignUp<4>(nhdr->n_namesz);
124 
125           if (!InRange(mem, size, value, nhdr->n_descsz)) {
126             PERFETTO_ELOG("Corrupted ELF.");
127             return base::nullopt;
128           }
129           return std::string(value, nhdr->n_descsz);
130         }
131       }
132       offset += sizeof(*nhdr) + base::AlignUp<4>(nhdr->n_namesz) +
133                 base::AlignUp<4>(nhdr->n_descsz);
134     }
135   }
136   return base::nullopt;
137 }
138 
139 class ScopedMmap {
140  public:
ScopedMmap(void * addr,size_t length,int prot,int flags,int fd,off_t offset)141   ScopedMmap(void* addr,
142              size_t length,
143              int prot,
144              int flags,
145              int fd,
146              off_t offset)
147       : length_(length), ptr_(mmap(addr, length, prot, flags, fd, offset)) {}
~ScopedMmap()148   ~ScopedMmap() {
149     if (ptr_ != MAP_FAILED)
150       munmap(ptr_, length_);
151   }
152 
operator *()153   void* operator*() { return ptr_; }
154 
155  private:
156   size_t length_;
157   void* ptr_;
158 };
159 
ParseLine(std::string line,std::string * file_name,uint32_t * line_no)160 bool ParseLine(std::string line, std::string* file_name, uint32_t* line_no) {
161   base::StringSplitter sp(std::move(line), ':');
162   if (!sp.Next())
163     return false;
164   *file_name = sp.cur_token();
165   if (!sp.Next())
166     return false;
167   char* endptr;
168   auto parsed_line_no = strtoll(sp.cur_token(), &endptr, 10);
169   if (parsed_line_no >= 0)
170     *line_no = static_cast<uint32_t>(parsed_line_no);
171   return *endptr == '\0' && parsed_line_no >= 0;
172 }
173 
SplitBuildID(const std::string & hex_build_id)174 std::string SplitBuildID(const std::string& hex_build_id) {
175   if (hex_build_id.size() < 3) {
176     PERFETTO_DFATAL_OR_ELOG("Invalid build-id (< 3 char) %s",
177                             hex_build_id.c_str());
178     return {};
179   }
180 
181   return hex_build_id.substr(0, 2) + "/" + hex_build_id.substr(2);
182 }
183 
184 }  // namespace
185 
FindBinary(const std::string & abspath,const std::string & build_id)186 base::Optional<std::string> LocalBinaryFinder::FindBinary(
187     const std::string& abspath,
188     const std::string& build_id) {
189   auto p = cache_.emplace(abspath, base::nullopt);
190   if (!p.second)
191     return p.first->second;
192 
193   base::Optional<std::string>& cache_entry = p.first->second;
194 
195   for (const std::string& root_str : roots_) {
196     cache_entry = FindBinaryInRoot(root_str, abspath, build_id);
197     if (cache_entry)
198       return cache_entry;
199   }
200   PERFETTO_ELOG("Could not find %s (Build ID: %s).", abspath.c_str(),
201                 base::ToHex(build_id).c_str());
202   return cache_entry;
203 }
204 
IsCorrectFile(const std::string & symbol_file,const std::string & build_id)205 bool LocalBinaryFinder::IsCorrectFile(const std::string& symbol_file,
206                                       const std::string& build_id) {
207   base::ScopedFile fd(base::OpenFile(symbol_file, O_RDONLY));
208   if (!fd)
209     return false;
210 
211   struct stat statbuf;
212   if (fstat(*fd, &statbuf) == -1)
213     return false;
214 
215   size_t size = static_cast<size_t>(statbuf.st_size);
216 
217   if (size <= EI_CLASS)
218     return false;
219 
220   ScopedMmap map(nullptr, size, PROT_READ, MAP_PRIVATE, *fd, 0);
221   if (*map == MAP_FAILED) {
222     PERFETTO_PLOG("mmap");
223     return false;
224   }
225   char* mem = static_cast<char*>(*map);
226 
227   if (mem[EI_MAG0] != ELFMAG0 || mem[EI_MAG1] != ELFMAG1 ||
228       mem[EI_MAG2] != ELFMAG2 || mem[EI_MAG3] != ELFMAG3) {
229     return false;
230   }
231 
232   switch (mem[EI_CLASS]) {
233     case ELFCLASS32:
234       return build_id == GetBuildId<Elf32>(mem, size);
235     case ELFCLASS64:
236       return build_id == GetBuildId<Elf64>(mem, size);
237     default:
238       return false;
239   }
240 }
241 
FindBinaryInRoot(const std::string & root_str,const std::string & abspath,const std::string & build_id)242 base::Optional<std::string> LocalBinaryFinder::FindBinaryInRoot(
243     const std::string& root_str,
244     const std::string& abspath,
245     const std::string& build_id) {
246   constexpr char kApkPrefix[] = "base.apk!";
247 
248   std::string filename;
249   std::string dirname;
250 
251   for (base::StringSplitter sp(abspath, '/'); sp.Next();) {
252     if (!dirname.empty())
253       dirname += "/";
254     dirname += filename;
255     filename = sp.cur_token();
256   }
257 
258   // Return the first match for the following options:
259   // * absolute path of library file relative to root.
260   // * absolute path of library file relative to root, but with base.apk!
261   //   removed from filename.
262   // * only filename of library file relative to root.
263   // * only filename of library file relative to root, but with base.apk!
264   //   removed from filename.
265   // * in the subdirectory .build-id: the first two hex digits of the build-id
266   //   as subdirectory, then the rest of the hex digits, with ".debug"appended.
267   //   See
268   //   https://fedoraproject.org/wiki/RolandMcGrath/BuildID#Find_files_by_build_ID
269   //
270   // For example, "/system/lib/base.apk!foo.so" with build id abcd1234,
271   // is looked for at
272   // * $ROOT/system/lib/base.apk!foo.so
273   // * $ROOT/system/lib/foo.so
274   // * $ROOT/base.apk!foo.so
275   // * $ROOT/foo.so
276   // * $ROOT/.build-id/ab/cd1234.debug
277 
278   std::string symbol_file = root_str + "/" + dirname + "/" + filename;
279   if (access(symbol_file.c_str(), F_OK) == 0 &&
280       IsCorrectFile(symbol_file, build_id))
281     return {symbol_file};
282 
283   if (filename.find(kApkPrefix) == 0) {
284     symbol_file =
285         root_str + "/" + dirname + "/" + filename.substr(sizeof(kApkPrefix));
286     if (access(symbol_file.c_str(), F_OK) == 0 &&
287         IsCorrectFile(symbol_file, build_id))
288       return {symbol_file};
289   }
290 
291   symbol_file = root_str + "/" + filename;
292   if (access(symbol_file.c_str(), F_OK) == 0 &&
293       IsCorrectFile(symbol_file, build_id))
294     return {symbol_file};
295 
296   if (filename.find(kApkPrefix) == 0) {
297     symbol_file = root_str + "/" + filename.substr(sizeof(kApkPrefix));
298     if (access(symbol_file.c_str(), F_OK) == 0 &&
299         IsCorrectFile(symbol_file, build_id))
300       return {symbol_file};
301   }
302 
303   std::string hex_build_id = base::ToHex(build_id.c_str(), build_id.size());
304   std::string split_hex_build_id = SplitBuildID(hex_build_id);
305   if (!split_hex_build_id.empty()) {
306     symbol_file =
307         root_str + "/" + ".build-id" + "/" + split_hex_build_id + ".debug";
308     if (access(symbol_file.c_str(), F_OK) == 0 &&
309         IsCorrectFile(symbol_file, build_id))
310       return {symbol_file};
311   }
312 
313   return base::nullopt;
314 }
315 
Subprocess(const std::string & file,std::vector<std::string> args)316 Subprocess::Subprocess(const std::string& file, std::vector<std::string> args)
317     : input_pipe_(base::Pipe::Create(base::Pipe::kBothBlock)),
318       output_pipe_(base::Pipe::Create(base::Pipe::kBothBlock)) {
319   std::vector<char*> c_str_args(args.size() + 1, nullptr);
320   for (std::string& arg : args)
321     c_str_args.push_back(&(arg[0]));
322 
323   if ((pid_ = fork()) == 0) {
324     // Child
325     PERFETTO_CHECK(dup2(*input_pipe_.rd, STDIN_FILENO) != -1);
326     PERFETTO_CHECK(dup2(*output_pipe_.wr, STDOUT_FILENO) != -1);
327     input_pipe_.wr.reset();
328     output_pipe_.rd.reset();
329     if (execvp(file.c_str(), &(c_str_args[0])) == -1)
330       PERFETTO_FATAL("Failed to exec %s", file.c_str());
331   }
332   PERFETTO_CHECK(pid_ != -1);
333   input_pipe_.rd.reset();
334   output_pipe_.wr.reset();
335 }
336 
~Subprocess()337 Subprocess::~Subprocess() {
338   if (pid_ != -1) {
339     kill(pid_, SIGKILL);
340     int wstatus;
341     PERFETTO_EINTR(waitpid(pid_, &wstatus, 0));
342   }
343 }
344 
LLVMSymbolizerProcess()345 LLVMSymbolizerProcess::LLVMSymbolizerProcess()
346     : subprocess_("llvm-symbolizer", {"llvm-symbolizer"}),
347       read_file_(fdopen(subprocess_.read_fd(), "r")) {}
348 
Symbolize(const std::string & binary,uint64_t address)349 std::vector<SymbolizedFrame> LLVMSymbolizerProcess::Symbolize(
350     const std::string& binary,
351     uint64_t address) {
352   std::vector<SymbolizedFrame> result;
353 
354   if (PERFETTO_EINTR(dprintf(subprocess_.write_fd(), "%s 0x%" PRIx64 "\n",
355                              binary.c_str(), address)) < 0) {
356     PERFETTO_ELOG("Failed to write to llvm-symbolizer.");
357     return result;
358   }
359   auto lines = GetLines(read_file_);
360   // llvm-symbolizer writes out records in the form of
361   // Foo(Bar*)
362   // foo.cc:123
363   // This is why we should always get a multiple of two number of lines.
364   PERFETTO_DCHECK(lines.size() % 2 == 0);
365   result.resize(lines.size() / 2);
366   for (size_t i = 0; i < lines.size(); ++i) {
367     SymbolizedFrame& cur = result[i / 2];
368     if (i % 2 == 0) {
369       cur.function_name = lines[i];
370     } else {
371       if (!ParseLine(lines[i], &cur.file_name, &cur.line)) {
372         PERFETTO_ELOG("Failed to parse llvm-symbolizer line: %s",
373                       lines[i].c_str());
374         cur.file_name = "";
375         cur.line = 0;
376       }
377     }
378   }
379 
380   for (auto it = result.begin(); it != result.end();) {
381     if (it->function_name == "??")
382       it = result.erase(it);
383     else
384       ++it;
385   }
386   return result;
387 }
Symbolize(const std::string & mapping_name,const std::string & build_id,const std::vector<uint64_t> & addresses)388 std::vector<std::vector<SymbolizedFrame>> LocalSymbolizer::Symbolize(
389     const std::string& mapping_name,
390     const std::string& build_id,
391     const std::vector<uint64_t>& addresses) {
392   base::Optional<std::string> binary =
393       finder_.FindBinary(mapping_name, build_id);
394   if (!binary)
395     return {};
396   std::vector<std::vector<SymbolizedFrame>> result;
397   result.reserve(addresses.size());
398   for (uint64_t address : addresses)
399     result.emplace_back(llvm_symbolizer_.Symbolize(*binary, address));
400   return result;
401 }
402 
403 LocalSymbolizer::~LocalSymbolizer() = default;
404 
405 }  // namespace profiling
406 }  // namespace perfetto
407 
408 #endif  // PERFETTO_BUILDFLAG(PERFETTO_LOCAL_SYMBOLIZER)
409