1 /* 2 * Copyright (C) 2021 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_BREAKPAD_PARSER_H_ 18 #define SRC_PROFILING_SYMBOLIZER_BREAKPAD_PARSER_H_ 19 20 #include <optional> 21 #include <string> 22 #include <vector> 23 24 #include "perfetto/base/status.h" 25 #include "perfetto/ext/base/string_view.h" 26 27 namespace perfetto { 28 namespace profiling { 29 30 // The BreakpadParser class is used to parse a breakpad file and store data on 31 // symbols so that a given address can be used to query a symbol. The class is 32 // instantiated with the |file_path| of the file to be parsed. Breakpad file 33 // format: 34 // https://chromium.googlesource.com/breakpad/breakpad/+/master/docs/symbol_files.md 35 // Usage: 36 // 37 // BreakpadParser parser("file.breakpad"); 38 // parser.ParseFile(); 39 // std::string symbol = parser.GetSymbol(addr); 40 class BreakpadParser { 41 public: 42 struct Symbol { 43 // The address where a function starts. 44 uint64_t start_address = 0; 45 // The length in bytes of the function's instructions. 46 size_t function_size = 0; 47 // The human-readable name for the function signature. 48 std::string symbol_name; 49 }; 50 51 explicit BreakpadParser(const std::string& file_path); 52 53 BreakpadParser(const BreakpadParser& other) = delete; 54 BreakpadParser& operator=(const BreakpadParser& other) = delete; 55 56 // Fills in |symbols_| by parsing the breakpad file given. 57 // Returns true if it is able to parse the entire file specified by 58 // |file_path_|. Returns false if the parsing fails. 59 bool ParseFile(); 60 61 // Parses from a string instead of a file. 62 bool ParseFromString(const std::string& file_contents); 63 64 // Returns the function name corresponding to |address| as a string. The 65 // search is log(N) on the number of functions in the binary. |address| is the 66 // relative offset from the start of the binary. 67 std::optional<std::string> GetSymbol(uint64_t address) const; 68 symbols_for_testing()69 const std::vector<Symbol>& symbols_for_testing() const { return symbols_; } 70 71 private: 72 // Parses the given string and creates a new Symbol object if it is a FUNC 73 // record. Returns an ok status if it was successfully able to add to the 74 // |symbols_| or if the string is not a FUNC record. Return a fail status on 75 // parsing errors on FUNC record. 76 base::Status ParseIfFuncRecord(base::StringView current_line); 77 78 std::vector<Symbol> symbols_; 79 const std::string file_path_; 80 }; 81 82 } // namespace profiling 83 } // namespace perfetto 84 85 #endif // SRC_PROFILING_SYMBOLIZER_BREAKPAD_PARSER_H_ 86