• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef SRC_PROFILING_SYMBOLIZER_LOCAL_SYMBOLIZER_H_
18 #define SRC_PROFILING_SYMBOLIZER_LOCAL_SYMBOLIZER_H_
19 
20 #include <map>
21 #include <string>
22 #include <vector>
23 
24 #include "perfetto/ext/base/optional.h"
25 #include "perfetto/ext/base/pipe.h"
26 #include "src/profiling/symbolizer/symbolizer.h"
27 
28 namespace perfetto {
29 namespace profiling {
30 
31 class LocalBinaryFinder {
32  public:
LocalBinaryFinder(std::vector<std::string> roots)33   LocalBinaryFinder(std::vector<std::string> roots)
34       : roots_(std::move(roots)) {}
35 
36   base::Optional<std::string> FindBinary(const std::string& abspath,
37                                          const std::string& build_id);
38 
39  private:
40   bool IsCorrectFile(const std::string& symbol_file,
41                      const std::string& build_id);
42 
43   base::Optional<std::string> FindBinaryInRoot(const std::string& root_str,
44                                                const std::string& abspath,
45                                                const std::string& build_id);
46 
47  private:
48   std::vector<std::string> roots_;
49   std::map<std::string, base::Optional<std::string>> cache_;
50 };
51 
52 class Subprocess {
53  public:
54   Subprocess(const std::string& file, std::vector<std::string> args);
55 
56   ~Subprocess();
57 
read_fd()58   int read_fd() { return output_pipe_.rd.get(); }
write_fd()59   int write_fd() { return input_pipe_.wr.get(); }
60 
61  private:
62   base::Pipe input_pipe_;
63   base::Pipe output_pipe_;
64 
65   pid_t pid_ = -1;
66 };
67 
68 class LLVMSymbolizerProcess {
69  public:
70   LLVMSymbolizerProcess();
71 
72   std::vector<SymbolizedFrame> Symbolize(const std::string& binary,
73                                          uint64_t address);
74 
75  private:
76   Subprocess subprocess_;
77   FILE* read_file_;
78 };
79 
80 class LocalSymbolizer : public Symbolizer {
81  public:
LocalSymbolizer(std::vector<std::string> roots)82   LocalSymbolizer(std::vector<std::string> roots) : finder_(std::move(roots)) {}
83 
84   std::vector<std::vector<SymbolizedFrame>> Symbolize(
85       const std::string& mapping_name,
86       const std::string& build_id,
87       const std::vector<uint64_t>& address) override;
88 
89   ~LocalSymbolizer() override;
90 
91  private:
92   LLVMSymbolizerProcess llvm_symbolizer_;
93   LocalBinaryFinder finder_;
94 };
95 
96 }  // namespace profiling
97 }  // namespace perfetto
98 
99 #endif  // SRC_PROFILING_SYMBOLIZER_LOCAL_SYMBOLIZER_H_
100