Yabin Cui | fc9da9b | 2019-08-08 18:15:14 -0700 | [diff] [blame] | 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 | |
Yabin Cui | c573eaa | 2019-08-21 16:05:07 -0700 | [diff] [blame] | 19 | #include <functional> |
Yabin Cui | fc9da9b | 2019-08-08 18:15:14 -0700 | [diff] [blame] | 20 | #include <memory> |
| 21 | #include <string> |
| 22 | |
Yabin Cui | 16f41ff | 2021-03-23 14:58:25 -0700 | [diff] [blame] | 23 | #include <android-base/expected.h> |
| 24 | |
Yabin Cui | fc9da9b | 2019-08-08 18:15:14 -0700 | [diff] [blame] | 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 | |
Yabin Cui | c573eaa | 2019-08-21 16:05:07 -0700 | [diff] [blame] | 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 | |
Yabin Cui | 193f238 | 2020-04-01 14:30:03 -0700 | [diff] [blame] | 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 | |
Yabin Cui | 2b1cfec | 2023-05-01 09:53:34 -0700 | [diff] [blame] | 64 | // ThreadTree interface used by ETMDecoder |
| 65 | class ETMThreadTree { |
| 66 | public: |
| 67 | virtual ~ETMThreadTree() {} |
| 68 | virtual void DisableThreadExitRecords() = 0; |
| 69 | virtual const ThreadEntry* FindThread(int tid) = 0; |
| 70 | virtual const MapSet& GetKernelMaps() = 0; |
| 71 | }; |
| 72 | |
Yabin Cui | fc9da9b | 2019-08-08 18:15:14 -0700 | [diff] [blame] | 73 | class ETMDecoder { |
| 74 | public: |
| 75 | static std::unique_ptr<ETMDecoder> Create(const AuxTraceInfoRecord& auxtrace_info, |
Yabin Cui | 2b1cfec | 2023-05-01 09:53:34 -0700 | [diff] [blame] | 76 | ETMThreadTree& thread_tree); |
Yabin Cui | fc9da9b | 2019-08-08 18:15:14 -0700 | [diff] [blame] | 77 | virtual ~ETMDecoder() {} |
| 78 | virtual void EnableDump(const ETMDumpOption& option) = 0; |
Yabin Cui | c573eaa | 2019-08-21 16:05:07 -0700 | [diff] [blame] | 79 | |
Yabin Cui | 193f238 | 2020-04-01 14:30:03 -0700 | [diff] [blame] | 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; |
Yabin Cui | c573eaa | 2019-08-21 16:05:07 -0700 | [diff] [blame] | 85 | |
Tamas Zsoldos | dd9ff55 | 2021-04-23 17:25:00 +0200 | [diff] [blame] | 86 | virtual bool ProcessData(const uint8_t* data, size_t size, bool formatted, uint32_t cpu) = 0; |
Yabin Cui | 2c29491 | 2020-03-31 11:59:47 -0700 | [diff] [blame] | 87 | virtual bool FinishData() = 0; |
Yabin Cui | fc9da9b | 2019-08-08 18:15:14 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
Yabin Cui | 16f41ff | 2021-03-23 14:58:25 -0700 | [diff] [blame] | 90 | // Map from addrs to a map of (branch_list, count). |
Yabin Cui | 4ad10fb | 2020-04-01 15:45:48 -0700 | [diff] [blame] | 91 | // Use maps instead of unordered_maps. Because it helps locality by decoding instructions for sorted |
| 92 | // addresses. |
Yabin Cui | 82d4805 | 2023-11-22 15:51:32 -0800 | [diff] [blame] | 93 | using ETMBranchMap = std::map<uint64_t, std::map<std::vector<bool>, uint64_t>>; |
Yabin Cui | 16f41ff | 2021-03-23 14:58:25 -0700 | [diff] [blame] | 94 | |
Yabin Cui | 82d4805 | 2023-11-22 15:51:32 -0800 | [diff] [blame] | 95 | android::base::expected<void, std::string> ConvertETMBranchMapToInstrRanges( |
| 96 | Dso* dso, const ETMBranchMap& branch_map, const ETMDecoder::InstrRangeCallbackFn& callback); |
Yabin Cui | 4ad10fb | 2020-04-01 15:45:48 -0700 | [diff] [blame] | 97 | |
Yabin Cui | c573eaa | 2019-08-21 16:05:07 -0700 | [diff] [blame] | 98 | } // namespace simpleperf |