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 #include "src/profiling/symbolizer/breakpad_symbolizer.h"
18
19 #include <optional>
20
21 #include "perfetto/base/build_config.h"
22 #include "perfetto/ext/base/file_utils.h"
23 #include "perfetto/ext/base/string_view.h"
24 #include "perfetto/ext/base/string_writer.h"
25 #include "src/profiling/symbolizer/breakpad_parser.h"
26
27 namespace perfetto {
28 namespace profiling {
29
30 namespace {
31
32 // Returns the file path for a breakpad symbol file with the given |build_id|.
MakeFilePath(const std::string & build_id,const std::string & symbol_dir_path)33 std::string MakeFilePath(const std::string& build_id,
34 const std::string& symbol_dir_path) {
35 // The directory of the symbol file is stored in an environment variable.
36 constexpr char kBreakpadSuffix[] = ".breakpad";
37 std::string file_path;
38 // Append file name to symbol directory path using |build_id| and
39 // |kBreakpadSuffix|.
40 file_path.append(symbol_dir_path);
41 // TODO: Add a path utility for perfetto to use here.
42 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
43 file_path.append("\\");
44 #else
45 file_path.append("/");
46 #endif
47 file_path.append(build_id);
48 file_path.append(kBreakpadSuffix);
49
50 return file_path;
51 }
52
53 } // namespace
54
BreakpadSymbolizer(const std::string & symbol_dir_path)55 BreakpadSymbolizer::BreakpadSymbolizer(const std::string& symbol_dir_path)
56 : symbol_dir_path_(symbol_dir_path) {}
57
Symbolize(const std::string &,const std::string & build_id,uint64_t,const std::vector<uint64_t> & address)58 std::vector<std::vector<SymbolizedFrame>> BreakpadSymbolizer::Symbolize(
59 const std::string&,
60 const std::string& build_id,
61 uint64_t,
62 const std::vector<uint64_t>& address) {
63 std::vector<std::vector<SymbolizedFrame>> result;
64 size_t num_symbolized_frames = 0;
65 result.reserve(address.size());
66 std::string file_path;
67 std::string raw_build_id = base::ToHex(build_id.c_str(), build_id.length());
68
69 // Check to see if the |file_path_for_testing_| member is populated. If it is,
70 // this file must be used.
71 if (file_path_for_testing_.empty()) {
72 file_path = MakeFilePath(raw_build_id, symbol_dir_path_).c_str();
73 } else {
74 file_path = file_path_for_testing_;
75 }
76
77 BreakpadParser parser(file_path);
78 if (!parser.ParseFile()) {
79 PERFETTO_ELOG("Failed to parse file %s.", file_path.c_str());
80 PERFETTO_PLOG("Symbolized %zu of %zu frames.", num_symbolized_frames,
81 address.size());
82 return result;
83 }
84
85 // Add each address's function name to the |result| vector in the same order.
86 for (uint64_t addr : address) {
87 SymbolizedFrame frame;
88 std::optional<std::string> opt_func_name = parser.GetSymbol(addr);
89 if (opt_func_name) {
90 frame.function_name = *opt_func_name;
91 num_symbolized_frames++;
92 }
93 result.push_back({std::move(frame)});
94 }
95 PERFETTO_PLOG("Symbolized %zu of %zu frames.", num_symbolized_frames,
96 address.size());
97 return result;
98 }
99
100 } // namespace profiling
101 } // namespace perfetto
102