• 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 #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 
68   // Check to see if the |file_path_for_testing_| member is populated. If it is,
69   // this file must be used.
70   if (file_path_for_testing_.empty()) {
71     file_path = MakeFilePath(build_id, symbol_dir_path_).c_str();
72   } else {
73     file_path = file_path_for_testing_;
74   }
75 
76   BreakpadParser parser(file_path);
77   if (!parser.ParseFile()) {
78     PERFETTO_ELOG("Failed to parse file %s.", file_path.c_str());
79     PERFETTO_PLOG("Symbolized %zu of %zu frames.", num_symbolized_frames,
80                   address.size());
81     return result;
82   }
83 
84   // Add each address's function name to the |result| vector in the same order.
85   for (uint64_t addr : address) {
86     SymbolizedFrame frame;
87     std::optional<std::string> opt_func_name = parser.GetSymbol(addr);
88     if (opt_func_name) {
89       frame.function_name = *opt_func_name;
90       num_symbolized_frames++;
91     }
92     result.push_back({std::move(frame)});
93   }
94   PERFETTO_PLOG("Symbolized %zu of %zu frames.", num_symbolized_frames,
95                 address.size());
96   return result;
97 }
98 
99 }  // namespace profiling
100 }  // namespace perfetto
101