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 ParseLlvmSymbolizerLine(const std::string& line, 35 std::string* file_name, 36 uint32_t* line_no); 37 std::vector<std::string> GetLines( 38 std::function<int64_t(char*, size_t)> fn_read); 39 40 struct FoundBinary { 41 std::string file_name; 42 uint64_t load_bias; 43 }; 44 45 class BinaryFinder { 46 public: 47 virtual ~BinaryFinder(); 48 virtual std::optional<FoundBinary> FindBinary( 49 const std::string& abspath, 50 const std::string& build_id) = 0; 51 }; 52 53 class LocalBinaryIndexer : public BinaryFinder { 54 public: 55 explicit LocalBinaryIndexer(std::vector<std::string> roots); 56 57 std::optional<FoundBinary> FindBinary(const std::string& abspath, 58 const std::string& build_id) override; 59 ~LocalBinaryIndexer() override; 60 61 private: 62 std::map<std::string, FoundBinary> buildid_to_file_; 63 }; 64 65 class LocalBinaryFinder : public BinaryFinder { 66 public: 67 explicit LocalBinaryFinder(std::vector<std::string> roots); 68 69 std::optional<FoundBinary> FindBinary(const std::string& abspath, 70 const std::string& build_id) override; 71 72 ~LocalBinaryFinder() override; 73 74 private: 75 std::optional<FoundBinary> IsCorrectFile(const std::string& symbol_file, 76 const std::string& build_id); 77 78 std::optional<FoundBinary> FindBinaryInRoot(const std::string& root_str, 79 const std::string& abspath, 80 const std::string& build_id); 81 82 private: 83 std::vector<std::string> roots_; 84 std::map<std::string, std::optional<FoundBinary>> cache_; 85 }; 86 87 class LLVMSymbolizerProcess { 88 public: 89 explicit LLVMSymbolizerProcess(const std::string& symbolizer_path); 90 91 std::vector<SymbolizedFrame> Symbolize(const std::string& binary, 92 uint64_t address); 93 94 private: 95 Subprocess subprocess_; 96 }; 97 98 class LocalSymbolizer : public Symbolizer { 99 public: 100 LocalSymbolizer(const std::string& symbolizer_path, 101 std::unique_ptr<BinaryFinder> finder); 102 103 explicit LocalSymbolizer(std::unique_ptr<BinaryFinder> finder); 104 105 std::vector<std::vector<SymbolizedFrame>> Symbolize( 106 const std::string& mapping_name, 107 const std::string& build_id, 108 uint64_t load_bias, 109 const std::vector<uint64_t>& address) override; 110 BuildIdNeedsHexConversion()111 bool BuildIdNeedsHexConversion() override { return true; } 112 113 ~LocalSymbolizer() override; 114 115 private: 116 LLVMSymbolizerProcess llvm_symbolizer_; 117 std::unique_ptr<BinaryFinder> finder_; 118 }; 119 120 std::unique_ptr<Symbolizer> LocalSymbolizerOrDie( 121 std::vector<std::string> binary_path, 122 const char* mode); 123 124 } // namespace profiling 125 } // namespace perfetto 126 127 #endif // SRC_PROFILING_SYMBOLIZER_LOCAL_SYMBOLIZER_H_ 128