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 <functional> 21 #include <map> 22 #include <memory> 23 #include <optional> 24 #include <string> 25 #include <vector> 26 27 #include "perfetto/ext/base/scoped_file.h" 28 #include "src/profiling/symbolizer/subprocess.h" 29 #include "src/profiling/symbolizer/symbolizer.h" 30 31 namespace perfetto { 32 namespace profiling { 33 34 bool ParseLlvmSymbolizerJsonLine(const std::string& line, 35 std::vector<SymbolizedFrame>* result); 36 enum BinaryType { 37 kElf, 38 kMachO, 39 kMachODsym, 40 }; 41 42 struct FoundBinary { 43 std::string file_name; 44 uint64_t load_bias; 45 BinaryType type; 46 }; 47 48 class BinaryFinder { 49 public: 50 virtual ~BinaryFinder(); 51 virtual std::optional<FoundBinary> FindBinary( 52 const std::string& abspath, 53 const std::string& build_id) = 0; 54 }; 55 56 class LocalBinaryIndexer : public BinaryFinder { 57 public: 58 explicit LocalBinaryIndexer(std::vector<std::string> roots); 59 60 std::optional<FoundBinary> FindBinary(const std::string& abspath, 61 const std::string& build_id) override; 62 ~LocalBinaryIndexer() override; 63 64 private: 65 std::map<std::string, FoundBinary> buildid_to_file_; 66 }; 67 68 class LocalBinaryFinder : public BinaryFinder { 69 public: 70 explicit LocalBinaryFinder(std::vector<std::string> roots); 71 72 std::optional<FoundBinary> FindBinary(const std::string& abspath, 73 const std::string& build_id) override; 74 75 ~LocalBinaryFinder() override; 76 77 private: 78 std::optional<FoundBinary> IsCorrectFile(const std::string& symbol_file, 79 const std::string& build_id); 80 81 std::optional<FoundBinary> FindBinaryInRoot(const std::string& root_str, 82 const std::string& abspath, 83 const std::string& build_id); 84 85 private: 86 std::vector<std::string> roots_; 87 std::map<std::string, std::optional<FoundBinary>> cache_; 88 }; 89 90 class LLVMSymbolizerProcess { 91 public: 92 explicit LLVMSymbolizerProcess(const std::string& symbolizer_path); 93 94 std::vector<SymbolizedFrame> Symbolize(const std::string& binary, 95 uint64_t address); 96 97 private: 98 Subprocess subprocess_; 99 }; 100 101 class LocalSymbolizer : public Symbolizer { 102 public: 103 LocalSymbolizer(const std::string& symbolizer_path, 104 std::unique_ptr<BinaryFinder> finder); 105 106 explicit LocalSymbolizer(std::unique_ptr<BinaryFinder> finder); 107 108 std::vector<std::vector<SymbolizedFrame>> Symbolize( 109 const std::string& mapping_name, 110 const std::string& build_id, 111 uint64_t load_bias, 112 const std::vector<uint64_t>& address) override; 113 114 ~LocalSymbolizer() override; 115 116 private: 117 LLVMSymbolizerProcess llvm_symbolizer_; 118 std::unique_ptr<BinaryFinder> finder_; 119 }; 120 121 std::unique_ptr<Symbolizer> LocalSymbolizerOrDie( 122 std::vector<std::string> binary_path, 123 const char* mode); 124 125 } // namespace profiling 126 } // namespace perfetto 127 128 #endif // SRC_PROFILING_SYMBOLIZER_LOCAL_SYMBOLIZER_H_ 129