blob: cb5c8dc86c966fac9703a85b271e3d5aa2e1aa2d [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#pragma once
18
Yabin Cuic573eaa2019-08-21 16:05:07 -070019#include <functional>
Yabin Cuifc9da9b2019-08-08 18:15:14 -070020#include <memory>
21#include <string>
22
Yabin Cui16f41ff2021-03-23 14:58:25 -070023#include <android-base/expected.h>
24
Yabin Cuifc9da9b2019-08-08 18:15:14 -070025#include "record.h"
26#include "thread_tree.h"
27
28namespace simpleperf {
29
30struct ETMDumpOption {
31 bool dump_raw_data = false;
32 bool dump_packets = false;
33 bool dump_elements = false;
34};
35
36bool ParseEtmDumpOption(const std::string& s, ETMDumpOption* option);
37
Yabin Cuic573eaa2019-08-21 16:05:07 -070038struct 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 Cui193f2382020-04-01 14:30:03 -070055struct 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 Cui2b1cfec2023-05-01 09:53:34 -070064// ThreadTree interface used by ETMDecoder
65class 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 Cuifc9da9b2019-08-08 18:15:14 -070073class ETMDecoder {
74 public:
75 static std::unique_ptr<ETMDecoder> Create(const AuxTraceInfoRecord& auxtrace_info,
Yabin Cui2b1cfec2023-05-01 09:53:34 -070076 ETMThreadTree& thread_tree);
Yabin Cuifc9da9b2019-08-08 18:15:14 -070077 virtual ~ETMDecoder() {}
78 virtual void EnableDump(const ETMDumpOption& option) = 0;
Yabin Cuic573eaa2019-08-21 16:05:07 -070079
Yabin Cui193f2382020-04-01 14:30:03 -070080 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 Cuic573eaa2019-08-21 16:05:07 -070085
Tamas Zsoldosdd9ff552021-04-23 17:25:00 +020086 virtual bool ProcessData(const uint8_t* data, size_t size, bool formatted, uint32_t cpu) = 0;
Yabin Cui2c294912020-03-31 11:59:47 -070087 virtual bool FinishData() = 0;
Yabin Cuifc9da9b2019-08-08 18:15:14 -070088};
89
Yabin Cui16f41ff2021-03-23 14:58:25 -070090// Map from addrs to a map of (branch_list, count).
Yabin Cui4ad10fb2020-04-01 15:45:48 -070091// Use maps instead of unordered_maps. Because it helps locality by decoding instructions for sorted
92// addresses.
Yabin Cui82d48052023-11-22 15:51:32 -080093using ETMBranchMap = std::map<uint64_t, std::map<std::vector<bool>, uint64_t>>;
Yabin Cui16f41ff2021-03-23 14:58:25 -070094
Yabin Cui82d48052023-11-22 15:51:32 -080095android::base::expected<void, std::string> ConvertETMBranchMapToInstrRanges(
96 Dso* dso, const ETMBranchMap& branch_map, const ETMDecoder::InstrRangeCallbackFn& callback);
Yabin Cui4ad10fb2020-04-01 15:45:48 -070097
Yabin Cuic573eaa2019-08-21 16:05:07 -070098} // namespace simpleperf