xref: /aosp_15_r20/external/perfetto/src/profiling/symbolizer/symbolize_database.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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 #include "src/profiling/symbolizer/symbolize_database.h"
18 
19 #include <map>
20 #include <utility>
21 #include <vector>
22 
23 #include "perfetto/base/logging.h"
24 #include "perfetto/ext/base/string_utils.h"
25 #include "perfetto/protozero/scattered_heap_buffer.h"
26 #include "perfetto/trace_processor/trace_processor.h"
27 
28 #include "protos/perfetto/trace/profiling/profile_common.pbzero.h"
29 #include "protos/perfetto/trace/trace.pbzero.h"
30 #include "protos/perfetto/trace/trace_packet.pbzero.h"
31 #include "src/trace_processor/util/build_id.h"
32 
33 namespace perfetto {
34 namespace profiling {
35 
36 namespace {
37 using trace_processor::Iterator;
38 
39 constexpr const char* kQueryUnsymbolized =
40     "select spm.name, spm.build_id, spf.rel_pc, spm.load_bias "
41     "from stack_profile_frame spf "
42     "join stack_profile_mapping spm "
43     "on spf.mapping = spm.id "
44     "where spm.build_id != '' and spf.symbol_set_id IS NULL";
45 
46 using NameAndBuildIdPair = std::pair<std::string, std::string>;
47 
48 struct UnsymbolizedMapping {
49   std::string name;
50   std::string build_id;
51   uint64_t load_bias;
operator <perfetto::profiling::__anond5867af40111::UnsymbolizedMapping52   bool operator<(const UnsymbolizedMapping& o) const {
53     return std::tie(name, build_id, load_bias) <
54            std::tie(o.name, o.build_id, o.load_bias);
55   }
56 };
57 
GetUnsymbolizedFrames(trace_processor::TraceProcessor * tp)58 std::map<UnsymbolizedMapping, std::vector<uint64_t>> GetUnsymbolizedFrames(
59     trace_processor::TraceProcessor* tp) {
60   std::map<UnsymbolizedMapping, std::vector<uint64_t>> res;
61   Iterator it = tp->ExecuteQuery(kQueryUnsymbolized);
62   while (it.Next()) {
63     int64_t load_bias = it.Get(3).AsLong();
64     PERFETTO_CHECK(load_bias >= 0);
65     trace_processor::BuildId build_id =
66         trace_processor::BuildId::FromHex(it.Get(1).AsString());
67     UnsymbolizedMapping unsymbolized_mapping{
68         it.Get(0).AsString(), build_id.raw(), static_cast<uint64_t>(load_bias)};
69     int64_t rel_pc = it.Get(2).AsLong();
70     res[unsymbolized_mapping].emplace_back(rel_pc);
71   }
72   if (!it.Status().ok()) {
73     PERFETTO_DFATAL_OR_ELOG("Invalid iterator: %s",
74                             it.Status().message().c_str());
75     return {};
76   }
77   return res;
78 }
79 }  // namespace
80 
SymbolizeDatabase(trace_processor::TraceProcessor * tp,Symbolizer * symbolizer,std::function<void (const std::string &)> callback)81 void SymbolizeDatabase(trace_processor::TraceProcessor* tp,
82                        Symbolizer* symbolizer,
83                        std::function<void(const std::string&)> callback) {
84   PERFETTO_CHECK(symbolizer);
85   auto unsymbolized = GetUnsymbolizedFrames(tp);
86   for (auto it = unsymbolized.cbegin(); it != unsymbolized.cend(); ++it) {
87     const auto& unsymbolized_mapping = it->first;
88     const std::vector<uint64_t>& rel_pcs = it->second;
89     auto res = symbolizer->Symbolize(unsymbolized_mapping.name,
90                                      unsymbolized_mapping.build_id,
91                                      unsymbolized_mapping.load_bias, rel_pcs);
92     if (res.empty())
93       continue;
94 
95     protozero::HeapBuffered<perfetto::protos::pbzero::Trace> trace;
96     auto* packet = trace->add_packet();
97     auto* module_symbols = packet->set_module_symbols();
98     module_symbols->set_path(unsymbolized_mapping.name);
99     module_symbols->set_build_id(unsymbolized_mapping.build_id);
100     PERFETTO_DCHECK(res.size() == rel_pcs.size());
101     for (size_t i = 0; i < res.size(); ++i) {
102       auto* address_symbols = module_symbols->add_address_symbols();
103       address_symbols->set_address(rel_pcs[i]);
104       for (const SymbolizedFrame& frame : res[i]) {
105         auto* line = address_symbols->add_lines();
106         line->set_function_name(frame.function_name);
107         line->set_source_file_name(frame.file_name);
108         line->set_line_number(frame.line);
109       }
110     }
111     callback(trace.SerializeAsString());
112   }
113 }
114 
GetPerfettoBinaryPath()115 std::vector<std::string> GetPerfettoBinaryPath() {
116   const char* root = getenv("PERFETTO_BINARY_PATH");
117   if (root != nullptr) {
118 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
119     const char* delimiter = ";";
120 #else
121     const char* delimiter = ":";
122 #endif
123     return base::SplitString(root, delimiter);
124   }
125   return {};
126 }
127 
128 }  // namespace profiling
129 }  // namespace perfetto
130