• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
32 namespace perfetto {
33 namespace profiling {
34 
35 namespace {
36 using trace_processor::Iterator;
37 
38 constexpr const char* kQueryUnsymbolized =
39     "select spm.name, spm.build_id, spf.rel_pc, spm.load_bias "
40     "from stack_profile_frame spf "
41     "join stack_profile_mapping spm "
42     "on spf.mapping = spm.id "
43     "where spm.build_id != '' and spf.symbol_set_id IS NULL";
44 
45 using NameAndBuildIdPair = std::pair<std::string, std::string>;
46 
47 struct UnsymbolizedMapping {
48   std::string name;
49   std::string build_id;
50   uint64_t load_bias;
operator <perfetto::profiling::__anon86aadca50111::UnsymbolizedMapping51   bool operator<(const UnsymbolizedMapping& o) const {
52     return std::tie(name, build_id, load_bias) <
53            std::tie(o.name, o.build_id, o.load_bias);
54   }
55 };
56 
FromHex(const char * str,size_t size)57 std::string FromHex(const char* str, size_t size) {
58   if (size % 2) {
59     PERFETTO_DFATAL_OR_ELOG("Failed to parse hex %s", str);
60     return "";
61   }
62   std::string result(size / 2, '\0');
63   for (size_t i = 0; i < size; i += 2) {
64     char hex_byte[3];
65     hex_byte[0] = str[i];
66     hex_byte[1] = str[i + 1];
67     hex_byte[2] = '\0';
68     char* end;
69     long int byte = strtol(hex_byte, &end, 16);
70     if (*end != '\0') {
71       PERFETTO_DFATAL_OR_ELOG("Failed to parse hex %s", str);
72       return "";
73     }
74     result[i / 2] = static_cast<char>(byte);
75   }
76   return result;
77 }
78 
FromHex(const std::string & str)79 std::string FromHex(const std::string& str) {
80   return FromHex(str.c_str(), str.size());
81 }
82 
GetUnsymbolizedFrames(trace_processor::TraceProcessor * tp)83 std::map<UnsymbolizedMapping, std::vector<uint64_t>> GetUnsymbolizedFrames(
84     trace_processor::TraceProcessor* tp) {
85   std::map<UnsymbolizedMapping, std::vector<uint64_t>> res;
86   Iterator it = tp->ExecuteQuery(kQueryUnsymbolized);
87   while (it.Next()) {
88     int64_t load_bias = it.Get(3).AsLong();
89     PERFETTO_CHECK(load_bias >= 0);
90     UnsymbolizedMapping unsymbolized_mapping{it.Get(0).AsString(),
91                                              FromHex(it.Get(1).AsString()),
92                                              static_cast<uint64_t>(load_bias)};
93     int64_t rel_pc = it.Get(2).AsLong();
94     res[unsymbolized_mapping].emplace_back(rel_pc);
95   }
96   if (!it.Status().ok()) {
97     PERFETTO_DFATAL_OR_ELOG("Invalid iterator: %s",
98                             it.Status().message().c_str());
99     return {};
100   }
101   return res;
102 }
103 }  // namespace
104 
SymbolizeDatabase(trace_processor::TraceProcessor * tp,Symbolizer * symbolizer,std::function<void (const std::string &)> callback)105 void SymbolizeDatabase(trace_processor::TraceProcessor* tp,
106                        Symbolizer* symbolizer,
107                        std::function<void(const std::string&)> callback) {
108   PERFETTO_CHECK(symbolizer);
109   auto unsymbolized = GetUnsymbolizedFrames(tp);
110   for (auto it = unsymbolized.cbegin(); it != unsymbolized.cend(); ++it) {
111     const auto& unsymbolized_mapping = it->first;
112     const std::vector<uint64_t>& rel_pcs = it->second;
113     auto res = symbolizer->Symbolize(unsymbolized_mapping.name,
114                                      unsymbolized_mapping.build_id,
115                                      unsymbolized_mapping.load_bias, rel_pcs);
116     if (res.empty())
117       continue;
118 
119     protozero::HeapBuffered<perfetto::protos::pbzero::Trace> trace;
120     auto* packet = trace->add_packet();
121     auto* module_symbols = packet->set_module_symbols();
122     module_symbols->set_path(unsymbolized_mapping.name);
123     module_symbols->set_build_id(unsymbolized_mapping.build_id);
124     PERFETTO_DCHECK(res.size() == rel_pcs.size());
125     for (size_t i = 0; i < res.size(); ++i) {
126       auto* address_symbols = module_symbols->add_address_symbols();
127       address_symbols->set_address(rel_pcs[i]);
128       for (const SymbolizedFrame& frame : res[i]) {
129         auto* line = address_symbols->add_lines();
130         line->set_function_name(frame.function_name);
131         line->set_source_file_name(frame.file_name);
132         line->set_line_number(frame.line);
133       }
134     }
135     callback(trace.SerializeAsString());
136   }
137 }
138 
GetPerfettoBinaryPath()139 std::vector<std::string> GetPerfettoBinaryPath() {
140   const char* root = getenv("PERFETTO_BINARY_PATH");
141   if (root != nullptr)
142     return base::SplitString(root, ":");
143   return {};
144 }
145 
146 }  // namespace profiling
147 }  // namespace perfetto
148