• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   // Supported record types for the Breakpad symbol file format.
52   // https://chromium.googlesource.com/breakpad/breakpad/+/HEAD/docs/symbol_files.md
53   enum class RecordType {
54     FUNC,   // FUNC [m] address size parameter_size name
55     PUBLIC  // PUBLIC [m] address parameter_size name
56   };
57 
58   explicit BreakpadParser(const std::string& file_path);
59 
60   BreakpadParser(const BreakpadParser& other) = delete;
61   BreakpadParser& operator=(const BreakpadParser& other) = delete;
62 
63   // Fills in |symbols_| by parsing the breakpad file given.
64   // Returns true if it is able to parse the entire file specified by
65   // |file_path_|. Returns false if the parsing fails.
66   bool ParseFile();
67 
68   // Parses from  a string instead of a file.
69   bool ParseFromString(const std::string& file_contents);
70 
71   // Returns the function name corresponding to |address| as a string. The
72   // search is log(N) on the number of functions in the binary. |address| is the
73   // relative offset from the start of the binary.
74   std::optional<std::string> GetSymbol(uint64_t address) const;
75 
76   // As same as GetSymbol, but retrieve from the PUBLIC records.
77   std::optional<std::string> GetPublicSymbol(uint64_t address) const;
78 
symbols_for_testing()79   const std::vector<Symbol>& symbols_for_testing() const { return symbols_; }
public_symbols_for_testing()80   const std::vector<Symbol>& public_symbols_for_testing() const {
81     return public_symbols_;
82   }
83 
84  private:
85   // Parses the given string and creates a new Symbol object if it is a
86   // {RecordType} record. Returns an ok status if it was successfully able to
87   // add to the |symbols_ or public_symbols_| or if the string is not a FUNC
88   // record. Return a fail status on parsing errors on FUNC record.
89   base::Status ParseIfRecord(base::StringView current_line, RecordType type);
90 
91   std::string GetRecordLabel(RecordType type) const;
92   void StoreSymbol(Symbol& symbol, RecordType type);
93 
94   std::vector<Symbol> symbols_;
95   std::vector<Symbol> public_symbols_;
96   const std::string file_path_;
97 };
98 
99 }  // namespace profiling
100 }  // namespace perfetto
101 
102 #endif  // SRC_PROFILING_SYMBOLIZER_BREAKPAD_PARSER_H_
103