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 #pragma once 18 19 #include <functional> 20 #include <memory> 21 #include <string> 22 23 #include <android-base/expected.h> 24 25 #include "record.h" 26 #include "thread_tree.h" 27 28 namespace simpleperf { 29 30 struct ETMDumpOption { 31 bool dump_raw_data = false; 32 bool dump_packets = false; 33 bool dump_elements = false; 34 }; 35 36 bool ParseEtmDumpOption(const std::string& s, ETMDumpOption* option); 37 38 struct ETMInstrRange { 39 // the binary containing the instruction range 40 Dso* dso = nullptr; 41 // the address of the first instruction in the binary 42 uint64_t start_addr = 0; 43 // the address of the last instruction in the binary 44 uint64_t end_addr = 0; 45 // If the last instruction is a branch instruction, and it branches 46 // to a fixed location in the same binary, then branch_to_addr points 47 // to the branched to instruction. 48 uint64_t branch_to_addr = 0; 49 // times the branch is taken 50 uint64_t branch_taken_count = 0; 51 // times the branch isn't taken 52 uint64_t branch_not_taken_count = 0; 53 }; 54 55 struct ETMBranchList { 56 // the binary containing the branch list 57 Dso* dso = nullptr; 58 // the instruction address before the first branch. Bit 0 is set for thumb instructions. 59 uint64_t addr = 0; 60 // the branch list (one bit for each branch, true for branch taken, false for not taken) 61 std::vector<bool> branch; 62 }; 63 64 // ThreadTree interface used by ETMDecoder 65 class ETMThreadTree { 66 public: ~ETMThreadTree()67 virtual ~ETMThreadTree() {} 68 virtual void DisableThreadExitRecords() = 0; 69 virtual const ThreadEntry* FindThread(int tid) = 0; 70 virtual const MapSet& GetKernelMaps() = 0; 71 }; 72 73 class ETMDecoder { 74 public: 75 static std::unique_ptr<ETMDecoder> Create(const AuxTraceInfoRecord& auxtrace_info, 76 ETMThreadTree& thread_tree); ~ETMDecoder()77 virtual ~ETMDecoder() {} 78 virtual void EnableDump(const ETMDumpOption& option) = 0; 79 80 using InstrRangeCallbackFn = std::function<void(const ETMInstrRange&)>; 81 virtual void RegisterCallback(const InstrRangeCallbackFn& callback) = 0; 82 83 using BranchListCallbackFn = std::function<void(const ETMBranchList&)>; 84 virtual void RegisterCallback(const BranchListCallbackFn& callback) = 0; 85 86 virtual bool ProcessData(const uint8_t* data, size_t size, bool formatted, uint32_t cpu) = 0; 87 virtual bool FinishData() = 0; 88 }; 89 90 // Map from addrs to a map of (branch_list, count). 91 // Use maps instead of unordered_maps. Because it helps locality by decoding instructions for sorted 92 // addresses. 93 using ETMBranchMap = std::map<uint64_t, std::map<std::vector<bool>, uint64_t>>; 94 95 android::base::expected<void, std::string> ConvertETMBranchMapToInstrRanges( 96 Dso* dso, const ETMBranchMap& branch_map, const ETMDecoder::InstrRangeCallbackFn& callback); 97 98 } // namespace simpleperf