blob: 822cc6cd2fda608e07dfcbd5312c10a4314b4b7d [file] [log] [blame]
Yabin Cuifc9da9b2019-08-08 18:15:14 -07001/*
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 "ETMDecoder.h"
18
Yabin Cui16f41ff2021-03-23 14:58:25 -070019#include <sstream>
20
21#include <android-base/expected.h>
Yabin Cuifc9da9b2019-08-08 18:15:14 -070022#include <android-base/logging.h>
23#include <android-base/strings.h>
24#include <llvm/Support/MemoryBuffer.h>
25#include <opencsd.h>
26
Tamas Zsoldos68fa61e2021-05-05 14:42:09 +020027#include "ETMConstants.h"
28
Yabin Cuifaa7b922021-01-11 17:35:57 -080029namespace simpleperf {
Yabin Cuifc9da9b2019-08-08 18:15:14 -070030namespace {
31
32class DecoderLogStr : public ocsdMsgLogStrOutI {
33 public:
Yabin Cuif058ffa2022-02-01 10:24:48 -080034 void printOutStr(const std::string& out_str) override { LOG(DEBUG) << out_str; }
Yabin Cuifc9da9b2019-08-08 18:15:14 -070035};
36
Yabin Cui7d2a6cc2019-10-18 14:01:15 -070037class DecodeErrorLogger : public ocsdDefaultErrorLogger {
38 public:
39 DecodeErrorLogger(const std::function<void(const ocsdError&)>& error_callback)
40 : error_callback_(error_callback) {
41 initErrorLogger(OCSD_ERR_SEV_INFO, false);
42 msg_logger_.setLogOpts(ocsdMsgLogger::OUT_STR_CB);
43 msg_logger_.setStrOutFn(&log_str_);
44 setOutputLogger(&msg_logger_);
Yabin Cuifc9da9b2019-08-08 18:15:14 -070045 }
Yabin Cui7d2a6cc2019-10-18 14:01:15 -070046
47 void LogError(const ocsd_hndl_err_log_t handle, const ocsdError* error) override {
48 ocsdDefaultErrorLogger::LogError(handle, error);
49 if (error != nullptr) {
50 error_callback_(*error);
51 }
52 }
53
54 private:
55 std::function<void(const ocsdError&)> error_callback_;
56 DecoderLogStr log_str_;
57 ocsdMsgLogger msg_logger_;
58};
Yabin Cuifc9da9b2019-08-08 18:15:14 -070059
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +020060static bool IsRespError(ocsd_datapath_resp_t resp) {
61 return resp >= OCSD_RESP_ERR_CONT;
62}
Yabin Cuifc9da9b2019-08-08 18:15:14 -070063
64// Used instead of DecodeTree in OpenCSD to avoid linking decoders not for ETMV4 instruction tracing
65// in OpenCSD.
66class ETMV4IDecodeTree {
67 public:
Yabin Cui7d2a6cc2019-10-18 14:01:15 -070068 ETMV4IDecodeTree()
69 : error_logger_(std::bind(&ETMV4IDecodeTree::ProcessError, this, std::placeholders::_1)) {
Yabin Cuia21b6632022-11-04 10:00:05 -070070 ocsd_err_t err = frame_decoder_.Init();
71 CHECK_EQ(err, OCSD_OK);
72 err = frame_decoder_.Configure(OCSD_DFRMTR_FRAME_MEM_ALIGN);
73 CHECK_EQ(err, OCSD_OK);
Yabin Cui7d2a6cc2019-10-18 14:01:15 -070074 frame_decoder_.getErrLogAttachPt()->attach(&error_logger_);
Yabin Cuifc9da9b2019-08-08 18:15:14 -070075 }
76
Branislav Rankov587fd042021-09-28 21:45:45 +010077 bool CreateDecoder(const EtmV4Config* config) {
78 uint8_t trace_id = config->getTraceID();
Yabin Cuifc9da9b2019-08-08 18:15:14 -070079 auto packet_decoder = std::make_unique<TrcPktProcEtmV4I>(trace_id);
Branislav Rankov587fd042021-09-28 21:45:45 +010080 packet_decoder->setProtocolConfig(config);
Yabin Cui7d2a6cc2019-10-18 14:01:15 -070081 packet_decoder->getErrorLogAttachPt()->replace_first(&error_logger_);
Yabin Cuifc9da9b2019-08-08 18:15:14 -070082 frame_decoder_.getIDStreamAttachPt(trace_id)->attach(packet_decoder.get());
83 auto result = packet_decoders_.emplace(trace_id, packet_decoder.release());
84 if (!result.second) {
85 LOG(ERROR) << "trace id " << trace_id << " has been used";
86 }
87 return result.second;
88 }
89
90 void AttachPacketSink(uint8_t trace_id, IPktDataIn<EtmV4ITrcPacket>& packet_sink) {
91 auto& packet_decoder = packet_decoders_[trace_id];
92 CHECK(packet_decoder);
93 packet_decoder->getPacketOutAttachPt()->replace_first(&packet_sink);
94 }
95
96 void AttachPacketMonitor(uint8_t trace_id, IPktRawDataMon<EtmV4ITrcPacket>& packet_monitor) {
97 auto& packet_decoder = packet_decoders_[trace_id];
98 CHECK(packet_decoder);
99 packet_decoder->getRawPacketMonAttachPt()->replace_first(&packet_monitor);
100 }
101
102 void AttachRawFramePrinter(RawFramePrinter& frame_printer) {
103 frame_decoder_.Configure(frame_decoder_.getConfigFlags() | OCSD_DFRMTR_PACKED_RAW_OUT);
104 frame_decoder_.getTrcRawFrameAttachPt()->replace_first(&frame_printer);
105 }
106
Tamas Zsoldosdd9ff552021-04-23 17:25:00 +0200107 ITrcDataIn& GetFormattedDataIn() { return frame_decoder_; }
108
109 ITrcDataIn& GetUnformattedDataIn(uint8_t trace_id) {
110 auto& decoder = packet_decoders_[trace_id];
111 CHECK(decoder);
112 return *decoder;
113 }
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700114
Yabin Cui7d2a6cc2019-10-18 14:01:15 -0700115 void ProcessError(const ocsdError& error) {
116 if (error.getErrorCode() == OCSD_ERR_INVALID_PCKT_HDR) {
117 // Found an invalid packet header, following packets for this trace id may also be invalid.
118 // So reset the decoder to find I_ASYNC packet in the data stream.
119 if (auto it = packet_decoders_.find(error.getErrorChanID()); it != packet_decoders_.end()) {
120 auto& packet_decoder = it->second;
121 CHECK(packet_decoder);
122 packet_decoder->TraceDataIn(OCSD_OP_RESET, error.getErrorIndex(), 0, nullptr, nullptr);
123 }
124 }
125 }
126
127 DecodeErrorLogger& ErrorLogger() { return error_logger_; }
128
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700129 private:
Yabin Cui7d2a6cc2019-10-18 14:01:15 -0700130 DecodeErrorLogger error_logger_;
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700131 TraceFormatterFrameDecoder frame_decoder_;
132 std::unordered_map<uint8_t, std::unique_ptr<TrcPktProcEtmV4I>> packet_decoders_;
133};
134
135// Similar to IPktDataIn<EtmV4ITrcPacket>, but add trace id.
136struct PacketCallback {
Yabin Cui418ba0d2020-03-24 11:53:39 -0700137 // packet callbacks are called in priority order.
138 enum Priority {
139 MAP_LOCATOR,
Yabin Cui193f2382020-04-01 14:30:03 -0700140 BRANCH_LIST_PARSER,
Yabin Cui418ba0d2020-03-24 11:53:39 -0700141 PACKET_TO_ELEMENT,
142 };
143
144 PacketCallback(Priority prio) : priority(prio) {}
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700145 virtual ~PacketCallback() {}
146 virtual ocsd_datapath_resp_t ProcessPacket(uint8_t trace_id, ocsd_datapath_op_t op,
147 ocsd_trc_index_t index_sop,
148 const EtmV4ITrcPacket* pkt) = 0;
Yabin Cui418ba0d2020-03-24 11:53:39 -0700149 const Priority priority;
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700150};
151
152// Receives packets from a packet decoder in OpenCSD library.
153class PacketSink : public IPktDataIn<EtmV4ITrcPacket> {
154 public:
155 PacketSink(uint8_t trace_id) : trace_id_(trace_id) {}
156
Yabin Cui418ba0d2020-03-24 11:53:39 -0700157 void AddCallback(PacketCallback* callback) {
158 auto it = std::lower_bound(callbacks_.begin(), callbacks_.end(), callback,
159 [](const PacketCallback* c1, const PacketCallback* c2) {
160 return c1->priority < c2->priority;
161 });
162 callbacks_.insert(it, callback);
163 }
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700164
165 ocsd_datapath_resp_t PacketDataIn(ocsd_datapath_op_t op, ocsd_trc_index_t index_sop,
166 const EtmV4ITrcPacket* pkt) override {
167 for (auto& callback : callbacks_) {
168 auto resp = callback->ProcessPacket(trace_id_, op, index_sop, pkt);
169 if (IsRespError(resp)) {
170 return resp;
171 }
172 }
173 return OCSD_RESP_CONT;
174 }
175
176 private:
177 uint8_t trace_id_;
178 std::vector<PacketCallback*> callbacks_;
179};
180
Yabin Cui418ba0d2020-03-24 11:53:39 -0700181// For each trace_id, when given an addr, find the thread and map it belongs to.
182class MapLocator : public PacketCallback {
183 public:
Yabin Cui2b1cfec2023-05-01 09:53:34 -0700184 MapLocator(ETMThreadTree& thread_tree)
Yabin Cui418ba0d2020-03-24 11:53:39 -0700185 : PacketCallback(PacketCallback::MAP_LOCATOR), thread_tree_(thread_tree) {}
186
Yabin Cui193f2382020-04-01 14:30:03 -0700187 // Return current thread id of a trace_id. If not available, return -1.
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +0200188 pid_t GetTid(uint8_t trace_id) const { return trace_data_[trace_id].tid; }
Yabin Cui193f2382020-04-01 14:30:03 -0700189
Yabin Cui418ba0d2020-03-24 11:53:39 -0700190 ocsd_datapath_resp_t ProcessPacket(uint8_t trace_id, ocsd_datapath_op_t op,
191 ocsd_trc_index_t index_sop,
192 const EtmV4ITrcPacket* pkt) override {
193 TraceData& data = trace_data_[trace_id];
194 if (op == OCSD_OP_DATA) {
Tamas Zsoldos68fa61e2021-05-05 14:42:09 +0200195 if (pkt != nullptr && ((!data.use_vmid && pkt->getContext().updated_c) ||
196 (data.use_vmid && pkt->getContext().updated_v))) {
197 int32_t new_tid =
198 static_cast<int32_t>(data.use_vmid ? pkt->getContext().VMID : pkt->getContext().ctxtID);
Yabin Cui418ba0d2020-03-24 11:53:39 -0700199 if (data.tid != new_tid) {
200 data.tid = new_tid;
201 data.thread = nullptr;
202 data.userspace_map = nullptr;
203 }
204 }
205 } else if (op == OCSD_OP_RESET) {
206 data.tid = -1;
207 data.thread = nullptr;
208 data.userspace_map = nullptr;
209 }
210 return OCSD_RESP_CONT;
211 }
212
213 const MapEntry* FindMap(uint8_t trace_id, uint64_t addr) {
214 TraceData& data = trace_data_[trace_id];
215 if (data.userspace_map != nullptr && data.userspace_map->Contains(addr)) {
216 return data.userspace_map;
217 }
218 if (data.tid == -1) {
219 return nullptr;
220 }
221 if (data.thread == nullptr) {
222 data.thread = thread_tree_.FindThread(data.tid);
223 if (data.thread == nullptr) {
224 return nullptr;
225 }
226 }
227 data.userspace_map = data.thread->maps->FindMapByAddr(addr);
228 if (data.userspace_map != nullptr) {
229 return data.userspace_map;
230 }
231 // We don't cache kernel map. Because kernel map can start from 0 and overlap all userspace
232 // maps.
233 return thread_tree_.GetKernelMaps().FindMapByAddr(addr);
234 }
235
Tamas Zsoldos68fa61e2021-05-05 14:42:09 +0200236 void SetUseVmid(uint8_t trace_id, bool value) { trace_data_[trace_id].use_vmid = value; }
237
Yabin Cui418ba0d2020-03-24 11:53:39 -0700238 private:
239 struct TraceData {
240 int32_t tid = -1; // thread id, -1 if invalid
241 const ThreadEntry* thread = nullptr;
242 const MapEntry* userspace_map = nullptr;
Tamas Zsoldos68fa61e2021-05-05 14:42:09 +0200243 bool use_vmid = false; // use vmid for PID
Yabin Cui418ba0d2020-03-24 11:53:39 -0700244 };
245
Yabin Cui2b1cfec2023-05-01 09:53:34 -0700246 ETMThreadTree& thread_tree_;
Yabin Cui418ba0d2020-03-24 11:53:39 -0700247 TraceData trace_data_[256];
248};
249
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700250// Map (trace_id, ip address) to (binary_path, binary_offset), and read binary files.
251class MemAccess : public ITargetMemAccess {
252 public:
Yabin Cui418ba0d2020-03-24 11:53:39 -0700253 MemAccess(MapLocator& map_locator) : map_locator_(map_locator) {}
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700254
Yabin Cui418ba0d2020-03-24 11:53:39 -0700255 ocsd_err_t ReadTargetMemory(const ocsd_vaddr_t address, uint8_t trace_id, ocsd_mem_space_acc_t,
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700256 uint32_t* num_bytes, uint8_t* p_buffer) override {
Yabin Cui418ba0d2020-03-24 11:53:39 -0700257 TraceData& data = trace_data_[trace_id];
258 const MapEntry* map = map_locator_.FindMap(trace_id, address);
259 // fast path
260 if (map != nullptr && map == data.buffer_map && address >= data.buffer_start &&
261 address + *num_bytes <= data.buffer_end) {
262 if (data.buffer == nullptr) {
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700263 *num_bytes = 0;
264 } else {
Yabin Cui418ba0d2020-03-24 11:53:39 -0700265 memcpy(p_buffer, data.buffer + (address - data.buffer_start), *num_bytes);
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700266 }
267 return OCSD_OK;
268 }
269
Yabin Cui418ba0d2020-03-24 11:53:39 -0700270 // slow path
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700271 size_t copy_size = 0;
Yabin Cui418ba0d2020-03-24 11:53:39 -0700272 if (map != nullptr) {
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700273 llvm::MemoryBuffer* memory = GetMemoryBuffer(map->dso);
274 if (memory != nullptr) {
Yabin Cui7078c672020-11-10 16:24:12 -0800275 if (auto opt_offset = map->dso->IpToFileOffset(address, map->start_addr, map->pgoff);
276 opt_offset) {
277 uint64_t offset = opt_offset.value();
278 size_t file_size = memory->getBufferSize();
279 copy_size = file_size > offset ? std::min<size_t>(file_size - offset, *num_bytes) : 0;
280 if (copy_size > 0) {
281 memcpy(p_buffer, memory->getBufferStart() + offset, copy_size);
282 }
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700283 }
284 }
285 // Update the last buffer cache.
Yabin Cui7078c672020-11-10 16:24:12 -0800286 // Don't cache for the kernel map. Because simpleperf doesn't record an accurate kernel end
287 // addr.
288 if (!map->in_kernel) {
289 data.buffer_map = map;
Yabin Cui7078c672020-11-10 16:24:12 -0800290 data.buffer_start = map->start_addr;
291 data.buffer_end = map->get_end_addr();
Yabin Cuibc5b7892023-01-13 16:12:25 -0800292 if (memory != nullptr && memory->getBufferSize() > map->pgoff &&
293 (memory->getBufferSize() - map->pgoff >= map->len)) {
294 data.buffer = memory->getBufferStart() + map->pgoff;
295 } else {
296 data.buffer = nullptr;
297 }
Yabin Cui7078c672020-11-10 16:24:12 -0800298 }
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700299 }
300 *num_bytes = copy_size;
301 return OCSD_OK;
302 }
303
Yi Kong613f82b2022-10-26 14:40:47 +0900304 void InvalidateMemAccCache(const uint8_t cs_trace_id) override {}
305
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700306 private:
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700307 llvm::MemoryBuffer* GetMemoryBuffer(Dso* dso) {
Yabin Cui02e20332020-03-16 19:38:23 -0700308 auto it = elf_map_.find(dso);
309 if (it == elf_map_.end()) {
310 ElfStatus status;
311 auto res = elf_map_.emplace(dso, ElfFile::Open(dso->GetDebugFilePath(), &status));
312 it = res.first;
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700313 }
Yabin Cui02e20332020-03-16 19:38:23 -0700314 return it->second ? it->second->GetMemoryBuffer() : nullptr;
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700315 }
316
Yabin Cui418ba0d2020-03-24 11:53:39 -0700317 struct TraceData {
318 const MapEntry* buffer_map = nullptr;
319 const char* buffer = nullptr;
320 uint64_t buffer_start = 0;
321 uint64_t buffer_end = 0;
322 };
323
324 MapLocator& map_locator_;
Yabin Cui02e20332020-03-16 19:38:23 -0700325 std::unordered_map<Dso*, std::unique_ptr<ElfFile>> elf_map_;
Yabin Cui418ba0d2020-03-24 11:53:39 -0700326 TraceData trace_data_[256];
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700327};
328
329class InstructionDecoder : public TrcIDecode {
330 public:
331 ocsd_err_t DecodeInstruction(ocsd_instr_info* instr_info) {
332 this->instr_info = instr_info;
333 return TrcIDecode::DecodeInstruction(instr_info);
334 }
335
336 ocsd_instr_info* instr_info;
337};
338
339// Similar to ITrcGenElemIn, but add next instruction info, which is needed to get branch to addr
340// for an InstructionRange element.
341struct ElementCallback {
342 public:
343 virtual ~ElementCallback(){};
344 virtual ocsd_datapath_resp_t ProcessElement(ocsd_trc_index_t index_sop, uint8_t trace_id,
345 const OcsdTraceElement& elem,
346 const ocsd_instr_info* next_instr) = 0;
347};
348
349// Decode packets into elements.
350class PacketToElement : public PacketCallback, public ITrcGenElemIn {
351 public:
Branislav Rankov587fd042021-09-28 21:45:45 +0100352 PacketToElement(MapLocator& map_locator,
353 const std::unordered_map<uint8_t, std::unique_ptr<EtmV4Config>>& configs,
Yabin Cui7d2a6cc2019-10-18 14:01:15 -0700354 DecodeErrorLogger& error_logger)
Yabin Cui418ba0d2020-03-24 11:53:39 -0700355 : PacketCallback(PacketCallback::PACKET_TO_ELEMENT), mem_access_(map_locator) {
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700356 for (auto& p : configs) {
357 uint8_t trace_id = p.first;
Branislav Rankov587fd042021-09-28 21:45:45 +0100358 const EtmV4Config* config = p.second.get();
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700359 element_decoders_.emplace(trace_id, trace_id);
360 auto& decoder = element_decoders_[trace_id];
Branislav Rankov587fd042021-09-28 21:45:45 +0100361 decoder.setProtocolConfig(config);
Yabin Cui7d2a6cc2019-10-18 14:01:15 -0700362 decoder.getErrorLogAttachPt()->replace_first(&error_logger);
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700363 decoder.getInstrDecodeAttachPt()->replace_first(&instruction_decoder_);
364 decoder.getMemoryAccessAttachPt()->replace_first(&mem_access_);
365 decoder.getTraceElemOutAttachPt()->replace_first(this);
366 }
367 }
368
369 void AddCallback(ElementCallback* callback) { callbacks_.push_back(callback); }
370
371 ocsd_datapath_resp_t ProcessPacket(uint8_t trace_id, ocsd_datapath_op_t op,
372 ocsd_trc_index_t index_sop,
373 const EtmV4ITrcPacket* pkt) override {
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700374 return element_decoders_[trace_id].PacketDataIn(op, index_sop, pkt);
375 }
376
377 ocsd_datapath_resp_t TraceElemIn(const ocsd_trc_index_t index_sop, uint8_t trc_chan_id,
378 const OcsdTraceElement& elem) override {
379 for (auto& callback : callbacks_) {
380 auto resp =
381 callback->ProcessElement(index_sop, trc_chan_id, elem, instruction_decoder_.instr_info);
382 if (IsRespError(resp)) {
383 return resp;
384 }
385 }
386 return OCSD_RESP_CONT;
387 }
388
389 private:
390 // map from trace id of an etm device to its element decoder
391 std::unordered_map<uint8_t, TrcPktDecodeEtmV4I> element_decoders_;
392 MemAccess mem_access_;
393 InstructionDecoder instruction_decoder_;
394 std::vector<ElementCallback*> callbacks_;
395};
396
397// Dump etm data generated at different stages.
398class DataDumper : public ElementCallback {
399 public:
400 DataDumper(ETMV4IDecodeTree& decode_tree) : decode_tree_(decode_tree) {}
401
402 void DumpRawData() {
403 decode_tree_.AttachRawFramePrinter(frame_printer_);
404 frame_printer_.setMessageLogger(&stdout_logger_);
405 }
406
Branislav Rankov587fd042021-09-28 21:45:45 +0100407 void DumpPackets(const std::unordered_map<uint8_t, std::unique_ptr<EtmV4Config>>& configs) {
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700408 for (auto& p : configs) {
409 uint8_t trace_id = p.first;
410 auto result = packet_printers_.emplace(trace_id, trace_id);
411 CHECK(result.second);
412 auto& packet_printer = result.first->second;
413 decode_tree_.AttachPacketMonitor(trace_id, packet_printer);
414 packet_printer.setMessageLogger(&stdout_logger_);
415 }
416 }
417
418 void DumpElements() { element_printer_.setMessageLogger(&stdout_logger_); }
419
420 ocsd_datapath_resp_t ProcessElement(ocsd_trc_index_t index_sop, uint8_t trc_chan_id,
421 const OcsdTraceElement& elem, const ocsd_instr_info*) {
422 return element_printer_.TraceElemIn(index_sop, trc_chan_id, elem);
423 }
424
425 private:
426 ETMV4IDecodeTree& decode_tree_;
427 RawFramePrinter frame_printer_;
428 std::unordered_map<uint8_t, PacketPrinter<EtmV4ITrcPacket>> packet_printers_;
429 TrcGenericElementPrinter element_printer_;
430 ocsdMsgLogger stdout_logger_;
431};
432
Yabin Cuic573eaa2019-08-21 16:05:07 -0700433// It decodes each ETMV4IPacket into TraceElements, and generates ETMInstrRanges from TraceElements.
434// Decoding each packet is slow, but ensures correctness.
Yabin Cui418ba0d2020-03-24 11:53:39 -0700435class InstrRangeParser : public ElementCallback {
Yabin Cui2c294912020-03-31 11:59:47 -0700436 private:
437 struct TraceData {
438 ETMInstrRange instr_range;
439 bool wait_for_branch_to_addr_fix = false;
440 };
441
Yabin Cuic573eaa2019-08-21 16:05:07 -0700442 public:
Yabin Cui193f2382020-04-01 14:30:03 -0700443 InstrRangeParser(MapLocator& map_locator, const ETMDecoder::InstrRangeCallbackFn& callback)
Yabin Cui418ba0d2020-03-24 11:53:39 -0700444 : map_locator_(map_locator), callback_(callback) {}
Yabin Cuic573eaa2019-08-21 16:05:07 -0700445
446 ocsd_datapath_resp_t ProcessElement(const ocsd_trc_index_t, uint8_t trace_id,
447 const OcsdTraceElement& elem,
448 const ocsd_instr_info* next_instr) override {
Yabin Cui418ba0d2020-03-24 11:53:39 -0700449 if (elem.getType() == OCSD_GEN_TRC_ELEM_INSTR_RANGE) {
Yabin Cui2c294912020-03-31 11:59:47 -0700450 TraceData& data = trace_data_[trace_id];
Yabin Cui418ba0d2020-03-24 11:53:39 -0700451 const MapEntry* map = map_locator_.FindMap(trace_id, elem.st_addr);
452 if (map == nullptr) {
Yabin Cui2c294912020-03-31 11:59:47 -0700453 FlushData(data);
Yabin Cuic573eaa2019-08-21 16:05:07 -0700454 return OCSD_RESP_CONT;
455 }
Yabin Cui2c294912020-03-31 11:59:47 -0700456 uint64_t start_addr = map->GetVaddrInFile(elem.st_addr);
457 auto& instr_range = data.instr_range;
458
459 if (data.wait_for_branch_to_addr_fix) {
460 // OpenCSD may cache a list of InstrRange elements, making it inaccurate to get branch to
461 // address from next_instr->branch_addr. So fix it by using the start address of the next
462 // InstrRange element.
463 instr_range.branch_to_addr = start_addr;
464 }
465 FlushData(data);
466 instr_range.dso = map->dso;
467 instr_range.start_addr = start_addr;
468 instr_range.end_addr = map->GetVaddrInFile(elem.en_addr - elem.last_instr_sz);
Yabin Cuic573eaa2019-08-21 16:05:07 -0700469 bool end_with_branch =
470 elem.last_i_type == OCSD_INSTR_BR || elem.last_i_type == OCSD_INSTR_BR_INDIRECT;
471 bool branch_taken = end_with_branch && elem.last_instr_exec;
472 if (elem.last_i_type == OCSD_INSTR_BR && branch_taken) {
Yabin Cui418ba0d2020-03-24 11:53:39 -0700473 // It is based on the assumption that we only do immediate branch inside a binary,
474 // which may not be true for all cases. TODO: http://b/151665001.
Yabin Cui2c294912020-03-31 11:59:47 -0700475 instr_range.branch_to_addr = map->GetVaddrInFile(next_instr->branch_addr);
476 data.wait_for_branch_to_addr_fix = true;
Yabin Cuic573eaa2019-08-21 16:05:07 -0700477 } else {
Yabin Cui2c294912020-03-31 11:59:47 -0700478 instr_range.branch_to_addr = 0;
Yabin Cuic573eaa2019-08-21 16:05:07 -0700479 }
Yabin Cui2c294912020-03-31 11:59:47 -0700480 instr_range.branch_taken_count = branch_taken ? 1 : 0;
481 instr_range.branch_not_taken_count = branch_taken ? 0 : 1;
482
483 } else if (elem.getType() == OCSD_GEN_TRC_ELEM_TRACE_ON) {
484 // According to the ETM Specification, the Trace On element indicates a discontinuity in the
485 // instruction trace stream. So it cuts the connection between instr ranges.
486 FlushData(trace_data_[trace_id]);
Yabin Cuic573eaa2019-08-21 16:05:07 -0700487 }
488 return OCSD_RESP_CONT;
489 }
490
Yabin Cui2c294912020-03-31 11:59:47 -0700491 void FinishData() {
492 for (auto& pair : trace_data_) {
493 FlushData(pair.second);
494 }
495 }
496
Yabin Cuic573eaa2019-08-21 16:05:07 -0700497 private:
Yabin Cui2c294912020-03-31 11:59:47 -0700498 void FlushData(TraceData& data) {
499 if (data.instr_range.dso != nullptr) {
500 callback_(data.instr_range);
501 data.instr_range.dso = nullptr;
502 }
503 data.wait_for_branch_to_addr_fix = false;
504 }
505
Yabin Cui418ba0d2020-03-24 11:53:39 -0700506 MapLocator& map_locator_;
Yabin Cui2c294912020-03-31 11:59:47 -0700507 std::unordered_map<uint8_t, TraceData> trace_data_;
Yabin Cui193f2382020-04-01 14:30:03 -0700508 ETMDecoder::InstrRangeCallbackFn callback_;
509};
510
511// It parses ETMBranchLists from ETMV4IPackets.
512// It doesn't do element decoding and instruction decoding, thus is about 5 timers faster than
513// InstrRangeParser. But some data will be lost when converting ETMBranchLists to InstrRanges:
514// 1. InstrRanges described by Except packets (the last instructions executed before exeception,
515// about 2%?).
516// 2. Branch to addresses of direct branch instructions across binaries.
517class BranchListParser : public PacketCallback {
518 private:
519 struct TraceData {
520 uint64_t addr = 0;
521 uint8_t addr_valid_bits = 0;
522 uint8_t isa = 0;
523 bool invalid_branch = false;
524 ETMBranchList branch;
525 };
526
527 public:
528 BranchListParser(MapLocator& map_locator, const ETMDecoder::BranchListCallbackFn& callback)
529 : PacketCallback(BRANCH_LIST_PARSER), map_locator_(map_locator), callback_(callback) {}
530
Branislav Rankov587fd042021-09-28 21:45:45 +0100531 void CheckConfigs(std::unordered_map<uint8_t, std::unique_ptr<EtmV4Config>>& configs) {
Yabin Cui193f2382020-04-01 14:30:03 -0700532 // TODO: Current implementation doesn't support non-zero speculation length and return stack.
533 for (auto& p : configs) {
Branislav Rankov587fd042021-09-28 21:45:45 +0100534 if (p.second->MaxSpecDepth() > 0) {
Yabin Cui193f2382020-04-01 14:30:03 -0700535 LOG(WARNING) << "branch list collection isn't accurate with non-zero speculation length";
536 break;
537 }
538 }
539 for (auto& p : configs) {
Branislav Rankov587fd042021-09-28 21:45:45 +0100540 if (p.second->enabledRetStack()) {
Yabin Cui193f2382020-04-01 14:30:03 -0700541 LOG(WARNING) << "branch list collection will lose some data with return stack enabled";
542 break;
543 }
544 }
545 }
546
547 bool IsAddrPacket(const EtmV4ITrcPacket* pkt) {
548 return pkt->getType() >= ETM4_PKT_I_ADDR_CTXT_L_32IS0 &&
549 pkt->getType() <= ETM4_PKT_I_ADDR_L_64IS1;
550 }
551
552 bool IsAtomPacket(const EtmV4ITrcPacket* pkt) { return pkt->getAtom().num > 0; }
553
554 ocsd_datapath_resp_t ProcessPacket(uint8_t trace_id, ocsd_datapath_op_t op,
555 ocsd_trc_index_t /*index_sop */,
556 const EtmV4ITrcPacket* pkt) override {
557 TraceData& data = trace_data_[trace_id];
558 if (op == OCSD_OP_DATA) {
559 if (IsAddrPacket(pkt)) {
560 // Flush branch when seeing an Addr packet. Because it isn't correct to concatenate
561 // branches before and after an Addr packet.
562 FlushBranch(data);
563 data.addr = pkt->getAddrVal();
564 data.addr_valid_bits = pkt->v_addr.valid_bits;
565 data.isa = pkt->getAddrIS();
566 }
567
568 if (IsAtomPacket(pkt)) {
569 // An atom packet contains a branch list. We may receive one or more atom packets in a row,
570 // and need to concatenate them.
571 ProcessAtomPacket(trace_id, data, pkt);
572 }
573
574 } else {
575 // Flush branch when seeing a flush or reset operation.
576 FlushBranch(data);
577 if (op == OCSD_OP_RESET) {
578 data.addr = 0;
579 data.addr_valid_bits = 0;
580 data.isa = 0;
581 data.invalid_branch = false;
582 }
583 }
584 return OCSD_RESP_CONT;
585 }
586
587 void FinishData() {
588 for (auto& pair : trace_data_) {
589 FlushBranch(pair.second);
590 }
591 }
592
593 private:
594 void ProcessAtomPacket(uint8_t trace_id, TraceData& data, const EtmV4ITrcPacket* pkt) {
595 if (data.invalid_branch) {
596 return; // Skip atom packets when we think a branch list is invalid.
597 }
598 if (data.branch.branch.empty()) {
599 // This is the first atom packet in a branch list. Check if we have tid and addr info to
600 // parse it and the following atom packets. If not, mark the branch list as invalid.
601 if (map_locator_.GetTid(trace_id) == -1 || data.addr_valid_bits == 0) {
602 data.invalid_branch = true;
603 return;
604 }
605 const MapEntry* map = map_locator_.FindMap(trace_id, data.addr);
606 if (map == nullptr) {
607 data.invalid_branch = true;
608 return;
609 }
610 data.branch.dso = map->dso;
611 data.branch.addr = map->GetVaddrInFile(data.addr);
612 if (data.isa == 1) { // thumb instruction, mark it in bit 0.
613 data.branch.addr |= 1;
614 }
615 }
616 uint32_t bits = pkt->atom.En_bits;
617 for (size_t i = 0; i < pkt->atom.num; i++) {
618 data.branch.branch.push_back((bits & 1) == 1);
619 bits >>= 1;
620 }
621 }
622
623 void FlushBranch(TraceData& data) {
624 if (!data.branch.branch.empty()) {
625 callback_(data.branch);
626 data.branch.branch.clear();
627 }
628 data.invalid_branch = false;
629 }
630
631 MapLocator& map_locator_;
632 ETMDecoder::BranchListCallbackFn callback_;
633 std::unordered_map<uint8_t, TraceData> trace_data_;
Yabin Cuic573eaa2019-08-21 16:05:07 -0700634};
635
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700636// Etm data decoding in OpenCSD library has two steps:
637// 1. From byte stream to etm packets. Each packet shows an event happened. For example,
638// an Address packet shows the cpu is running the instruction at that address, an Atom
639// packet shows whether the cpu decides to branch or not.
640// 2. From etm packets to trace elements. To generates elements, the decoder needs both etm
641// packets and executed binaries. For example, an InstructionRange element needs the decoder
642// to find the next branch instruction starting from an address.
643//
644// ETMDecoderImpl uses OpenCSD library to decode etm data. It has the following properties:
645// 1. Supports flexible decoding strategy. It allows installing packet callbacks and element
646// callbacks, and decodes to either packets or elements based on requirements.
647// 2. Supports dumping data at different stages.
648class ETMDecoderImpl : public ETMDecoder {
649 public:
Yabin Cui2b1cfec2023-05-01 09:53:34 -0700650 ETMDecoderImpl(ETMThreadTree& thread_tree) : thread_tree_(thread_tree) {
651 // If the aux record for a thread is processed after it's thread exit record, we can't find
652 // the thread's maps when processing ETM data. To handle this, disable thread exit records.
653 thread_tree.DisableThreadExitRecords();
654 }
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700655
656 void CreateDecodeTree(const AuxTraceInfoRecord& auxtrace_info) {
Branislav Rankov587fd042021-09-28 21:45:45 +0100657 uint8_t trace_id = 0;
658 uint64_t* info = auxtrace_info.data->info;
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700659 for (int i = 0; i < auxtrace_info.data->nr_cpu; i++) {
Branislav Rankov587fd042021-09-28 21:45:45 +0100660 if (info[0] == AuxTraceInfoRecord::MAGIC_ETM4) {
661 auto& etm4 = *reinterpret_cast<AuxTraceInfoRecord::ETM4Info*>(info);
662 ocsd_etmv4_cfg cfg;
663 memset(&cfg, 0, sizeof(cfg));
664 cfg.reg_idr0 = etm4.trcidr0;
665 cfg.reg_idr1 = etm4.trcidr1;
666 cfg.reg_idr2 = etm4.trcidr2;
667 cfg.reg_idr8 = etm4.trcidr8;
668 cfg.reg_configr = etm4.trcconfigr;
669 cfg.reg_traceidr = etm4.trctraceidr;
670 cfg.arch_ver = ARCH_V8;
671 cfg.core_prof = profile_CortexA;
672 trace_id = cfg.reg_traceidr & 0x7f;
673 trace_ids_.emplace(etm4.cpu, trace_id);
674 configs_.emplace(trace_id, new EtmV4Config(&cfg));
675 info = reinterpret_cast<uint64_t*>(&etm4 + 1);
676 } else {
677 CHECK_EQ(info[0], AuxTraceInfoRecord::MAGIC_ETE);
678 auto& ete = *reinterpret_cast<AuxTraceInfoRecord::ETEInfo*>(info);
679 ocsd_ete_cfg cfg;
680 memset(&cfg, 0, sizeof(cfg));
681 cfg.reg_idr0 = ete.trcidr0;
682 cfg.reg_idr1 = ete.trcidr1;
683 cfg.reg_idr2 = ete.trcidr2;
684 cfg.reg_idr8 = ete.trcidr8;
685 cfg.reg_devarch = ete.trcdevarch;
686 cfg.reg_configr = ete.trcconfigr;
687 cfg.reg_traceidr = ete.trctraceidr;
688 cfg.arch_ver = ARCH_AA64;
689 cfg.core_prof = profile_CortexA;
690 trace_id = cfg.reg_traceidr & 0x7f;
691 trace_ids_.emplace(ete.cpu, trace_id);
692 configs_.emplace(trace_id, new ETEConfig(&cfg));
693 info = reinterpret_cast<uint64_t*>(&ete + 1);
694 }
695 decode_tree_.CreateDecoder(configs_[trace_id].get());
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700696 auto result = packet_sinks_.emplace(trace_id, trace_id);
697 CHECK(result.second);
698 decode_tree_.AttachPacketSink(trace_id, result.first->second);
699 }
700 }
701
702 void EnableDump(const ETMDumpOption& option) override {
703 dumper_.reset(new DataDumper(decode_tree_));
704 if (option.dump_raw_data) {
705 dumper_->DumpRawData();
706 }
707 if (option.dump_packets) {
708 dumper_->DumpPackets(configs_);
709 }
710 if (option.dump_elements) {
711 dumper_->DumpElements();
712 InstallElementCallback(dumper_.get());
713 }
714 }
715
Yabin Cui193f2382020-04-01 14:30:03 -0700716 void RegisterCallback(const InstrRangeCallbackFn& callback) {
Yabin Cui418ba0d2020-03-24 11:53:39 -0700717 InstallMapLocator();
718 instr_range_parser_.reset(new InstrRangeParser(*map_locator_, callback));
719 InstallElementCallback(instr_range_parser_.get());
Yabin Cuic573eaa2019-08-21 16:05:07 -0700720 }
721
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +0200722 void RegisterCallback(const BranchListCallbackFn& callback) {
Yabin Cui193f2382020-04-01 14:30:03 -0700723 InstallMapLocator();
724 branch_list_parser_.reset(new BranchListParser(*map_locator_, callback));
725 branch_list_parser_->CheckConfigs(configs_);
726 InstallPacketCallback(branch_list_parser_.get());
727 }
728
Tamas Zsoldosdd9ff552021-04-23 17:25:00 +0200729 bool ProcessData(const uint8_t* data, size_t size, bool formatted, uint32_t cpu) override {
Yabin Cui7d2a6cc2019-10-18 14:01:15 -0700730 // Reset decoders before processing each data block. Because:
731 // 1. Data blocks are not continuous. So decoders shouldn't keep previous states when
732 // processing a new block.
733 // 2. The beginning part of a data block may be truncated if kernel buffer is temporarily full.
734 // So we may see garbage data, which can cause decoding errors if we don't reset decoders.
Tamas Zsoldosdd9ff552021-04-23 17:25:00 +0200735 LOG(DEBUG) << "Processing " << (!formatted ? "un" : "") << "formatted data with size " << size;
736 auto& decoder = formatted ? decode_tree_.GetFormattedDataIn()
737 : decode_tree_.GetUnformattedDataIn(trace_ids_[cpu]);
738
739 auto resp = decoder.TraceDataIn(OCSD_OP_RESET, data_index_, 0, nullptr, nullptr);
Yabin Cui7d2a6cc2019-10-18 14:01:15 -0700740 if (IsRespError(resp)) {
741 LOG(ERROR) << "failed to reset decoder, resp " << resp;
742 return false;
743 }
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700744 size_t left_size = size;
Yabin Cui930b6a82022-03-07 14:29:16 -0800745 const size_t MAX_RESET_RETRY_COUNT = 3;
746 size_t reset_retry_count = 0;
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700747 while (left_size > 0) {
748 uint32_t processed;
Tamas Zsoldosdd9ff552021-04-23 17:25:00 +0200749 auto resp = decoder.TraceDataIn(OCSD_OP_DATA, data_index_, left_size, data, &processed);
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700750 if (IsRespError(resp)) {
Yabin Cui7d2a6cc2019-10-18 14:01:15 -0700751 // A decoding error shouldn't ruin all data. Reset decoders to recover from it.
Yabin Cui930b6a82022-03-07 14:29:16 -0800752 // But some errors may not be recoverable by resetting decoders. So use a max retry limit.
753 if (++reset_retry_count > MAX_RESET_RETRY_COUNT) {
754 break;
755 }
756 LOG(DEBUG) << "reset etm decoders for seeing a decode failure, resp " << resp
757 << ", reset_retry_count is " << reset_retry_count;
Tamas Zsoldosdd9ff552021-04-23 17:25:00 +0200758 decoder.TraceDataIn(OCSD_OP_RESET, data_index_ + processed, 0, nullptr, nullptr);
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700759 }
760 data += processed;
761 left_size -= processed;
762 data_index_ += processed;
763 }
764 return true;
765 }
766
Yabin Cui2c294912020-03-31 11:59:47 -0700767 bool FinishData() override {
768 if (instr_range_parser_) {
769 instr_range_parser_->FinishData();
770 }
Yabin Cui193f2382020-04-01 14:30:03 -0700771 if (branch_list_parser_) {
772 branch_list_parser_->FinishData();
773 }
Yabin Cui2c294912020-03-31 11:59:47 -0700774 return true;
775 }
776
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700777 private:
Yabin Cui418ba0d2020-03-24 11:53:39 -0700778 void InstallMapLocator() {
779 if (!map_locator_) {
780 map_locator_.reset(new MapLocator(thread_tree_));
Tamas Zsoldos68fa61e2021-05-05 14:42:09 +0200781 for (auto& cfg : configs_) {
Branislav Rankov587fd042021-09-28 21:45:45 +0100782 int64_t configr = (*(const ocsd_etmv4_cfg*)*cfg.second).reg_configr;
Yabin Cuif058ffa2022-02-01 10:24:48 -0800783 map_locator_->SetUseVmid(cfg.first,
784 configr & (1U << ETM4_CFG_BIT_VMID | 1U << ETM4_CFG_BIT_VMID_OPT));
Tamas Zsoldos68fa61e2021-05-05 14:42:09 +0200785 }
786
Yabin Cui418ba0d2020-03-24 11:53:39 -0700787 InstallPacketCallback(map_locator_.get());
788 }
789 }
790
791 void InstallPacketCallback(PacketCallback* callback) {
792 for (auto& p : packet_sinks_) {
793 p.second.AddCallback(callback);
794 }
795 }
796
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700797 void InstallElementCallback(ElementCallback* callback) {
798 if (!packet_to_element_) {
Yabin Cui418ba0d2020-03-24 11:53:39 -0700799 InstallMapLocator();
Yabin Cui7d2a6cc2019-10-18 14:01:15 -0700800 packet_to_element_.reset(
Yabin Cui418ba0d2020-03-24 11:53:39 -0700801 new PacketToElement(*map_locator_, configs_, decode_tree_.ErrorLogger()));
802 InstallPacketCallback(packet_to_element_.get());
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700803 }
804 packet_to_element_->AddCallback(callback);
805 }
806
807 // map ip address to binary path and binary offset
Yabin Cui2b1cfec2023-05-01 09:53:34 -0700808 ETMThreadTree& thread_tree_;
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700809 // handle to build OpenCSD decoder
810 ETMV4IDecodeTree decode_tree_;
Tamas Zsoldosdd9ff552021-04-23 17:25:00 +0200811 // map from cpu to trace id
812 std::unordered_map<uint64_t, uint8_t> trace_ids_;
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700813 // map from the trace id of an etm device to its config
Branislav Rankov587fd042021-09-28 21:45:45 +0100814 std::unordered_map<uint8_t, std::unique_ptr<EtmV4Config>> configs_;
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700815 // map from the trace id of an etm device to its PacketSink
816 std::unordered_map<uint8_t, PacketSink> packet_sinks_;
817 std::unique_ptr<PacketToElement> packet_to_element_;
818 std::unique_ptr<DataDumper> dumper_;
819 // an index keeping processed etm data size
820 size_t data_index_ = 0;
Yabin Cuic573eaa2019-08-21 16:05:07 -0700821 std::unique_ptr<InstrRangeParser> instr_range_parser_;
Yabin Cui418ba0d2020-03-24 11:53:39 -0700822 std::unique_ptr<MapLocator> map_locator_;
Yabin Cui193f2382020-04-01 14:30:03 -0700823 std::unique_ptr<BranchListParser> branch_list_parser_;
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700824};
825
826} // namespace
827
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700828bool ParseEtmDumpOption(const std::string& s, ETMDumpOption* option) {
829 for (auto& value : android::base::Split(s, ",")) {
830 if (value == "raw") {
831 option->dump_raw_data = true;
832 } else if (value == "packet") {
833 option->dump_packets = true;
834 } else if (value == "element") {
835 option->dump_elements = true;
836 } else {
837 LOG(ERROR) << "unknown etm dump option: " << value;
838 return false;
839 }
840 }
841 return true;
842}
843
844std::unique_ptr<ETMDecoder> ETMDecoder::Create(const AuxTraceInfoRecord& auxtrace_info,
Yabin Cui2b1cfec2023-05-01 09:53:34 -0700845 ETMThreadTree& thread_tree) {
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700846 auto decoder = std::make_unique<ETMDecoderImpl>(thread_tree);
847 decoder->CreateDecodeTree(auxtrace_info);
848 return std::unique_ptr<ETMDecoder>(decoder.release());
849}
850
Yabin Cui4ad10fb2020-04-01 15:45:48 -0700851// Use OpenCSD instruction decoder to convert branches to instruction addresses.
852class BranchDecoder {
853 public:
Yabin Cui16f41ff2021-03-23 14:58:25 -0700854 android::base::expected<void, std::string> Init(Dso* dso) {
Yabin Cui4ad10fb2020-04-01 15:45:48 -0700855 ElfStatus status;
856 elf_ = ElfFile::Open(dso->GetDebugFilePath(), &status);
857 if (!elf_) {
Yabin Cui16f41ff2021-03-23 14:58:25 -0700858 std::stringstream ss;
859 ss << status;
860 return android::base::unexpected(ss.str());
Yabin Cui4ad10fb2020-04-01 15:45:48 -0700861 }
Yabin Cui16f41ff2021-03-23 14:58:25 -0700862 if (dso->type() == DSO_KERNEL_MODULE) {
863 // Kernel module doesn't have program header. So create a fake one mapping to .text section.
864 for (const auto& section : elf_->GetSectionHeader()) {
865 if (section.name == ".text") {
866 segments_.resize(1);
867 segments_[0].is_executable = true;
868 segments_[0].is_load = true;
869 segments_[0].file_offset = section.file_offset;
870 segments_[0].file_size = section.size;
871 segments_[0].vaddr = section.vaddr;
872 break;
873 }
874 }
875 } else {
876 segments_ = elf_->GetProgramHeader();
877 auto it = std::remove_if(segments_.begin(), segments_.end(),
878 [](const ElfSegment& s) { return !s.is_executable; });
879 segments_.resize(it - segments_.begin());
880 }
Yabin Cui4ad10fb2020-04-01 15:45:48 -0700881 if (segments_.empty()) {
Yabin Cui16f41ff2021-03-23 14:58:25 -0700882 return android::base::unexpected("no segments");
Yabin Cui4ad10fb2020-04-01 15:45:48 -0700883 }
884 buffer_ = elf_->GetMemoryBuffer();
Yabin Cui16f41ff2021-03-23 14:58:25 -0700885 return {};
Yabin Cui4ad10fb2020-04-01 15:45:48 -0700886 }
887
888 void SetAddr(uint64_t addr, bool is_thumb) {
889 memset(&instr_info_, 0, sizeof(instr_info_));
890 instr_info_.pe_type.arch = ARCH_V8;
891 instr_info_.pe_type.profile = profile_CortexA;
892 instr_info_.isa =
893 elf_->Is64Bit() ? ocsd_isa_aarch64 : (is_thumb ? ocsd_isa_thumb2 : ocsd_isa_arm);
894 instr_info_.instr_addr = addr;
895 }
896
897 bool FindNextBranch() {
898 // Loop until we find a branch instruction.
899 while (ReadMem(instr_info_.instr_addr, 4, &instr_info_.opcode)) {
900 ocsd_err_t err = instruction_decoder_.DecodeInstruction(&instr_info_);
901 if (err != OCSD_OK) {
902 break;
903 }
904 if (instr_info_.type != OCSD_INSTR_OTHER) {
905 return true;
906 }
907 instr_info_.instr_addr += instr_info_.instr_size;
908 }
909 return false;
910 };
911
912 ocsd_instr_info& InstrInfo() { return instr_info_; }
913
914 private:
915 bool ReadMem(uint64_t vaddr, size_t size, void* data) {
916 for (auto& segment : segments_) {
917 if (vaddr >= segment.vaddr && vaddr + size <= segment.vaddr + segment.file_size) {
918 uint64_t offset = vaddr - segment.vaddr + segment.file_offset;
919 memcpy(data, buffer_->getBufferStart() + offset, size);
920 return true;
921 }
922 }
923 return false;
924 }
925
926 std::unique_ptr<ElfFile> elf_;
927 std::vector<ElfSegment> segments_;
928 llvm::MemoryBuffer* buffer_ = nullptr;
929 ocsd_instr_info instr_info_;
930 InstructionDecoder instruction_decoder_;
931};
932
Yabin Cui82d48052023-11-22 15:51:32 -0800933android::base::expected<void, std::string> ConvertETMBranchMapToInstrRanges(
934 Dso* dso, const ETMBranchMap& branch_map, const ETMDecoder::InstrRangeCallbackFn& callback) {
Yabin Cui4ad10fb2020-04-01 15:45:48 -0700935 ETMInstrRange instr_range;
936 instr_range.dso = dso;
937
938 BranchDecoder decoder;
Yabin Cui16f41ff2021-03-23 14:58:25 -0700939 if (auto result = decoder.Init(dso); !result.ok()) {
940 return result;
Yabin Cui4ad10fb2020-04-01 15:45:48 -0700941 }
942
943 for (const auto& addr_p : branch_map) {
944 uint64_t start_addr = addr_p.first & ~1ULL;
945 bool is_thumb = addr_p.first & 1;
946 for (const auto& branch_p : addr_p.second) {
947 const std::vector<bool>& branch = branch_p.first;
948 uint64_t count = branch_p.second;
949 decoder.SetAddr(start_addr, is_thumb);
950
951 for (bool b : branch) {
952 ocsd_instr_info& instr = decoder.InstrInfo();
953 uint64_t from_addr = instr.instr_addr;
954 if (!decoder.FindNextBranch()) {
955 break;
956 }
957 bool end_with_branch = instr.type == OCSD_INSTR_BR || instr.type == OCSD_INSTR_BR_INDIRECT;
958 bool branch_taken = end_with_branch && b;
959 instr_range.start_addr = from_addr;
960 instr_range.end_addr = instr.instr_addr;
961 if (instr.type == OCSD_INSTR_BR) {
962 instr_range.branch_to_addr = instr.branch_addr;
963 } else {
964 instr_range.branch_to_addr = 0;
965 }
966 instr_range.branch_taken_count = branch_taken ? count : 0;
967 instr_range.branch_not_taken_count = branch_taken ? 0 : count;
968
969 callback(instr_range);
970
971 if (b) {
972 instr.instr_addr = instr.branch_addr;
973 } else {
974 instr.instr_addr += instr.instr_size;
975 }
976 }
977 }
978 }
Yabin Cui16f41ff2021-03-23 14:58:25 -0700979 return {};
Yabin Cui4ad10fb2020-04-01 15:45:48 -0700980}
981
Yi Kong613f82b2022-10-26 14:40:47 +0900982} // namespace simpleperf