• 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 #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 class ETMDecoder {
65  public:
66   static std::unique_ptr<ETMDecoder> Create(const AuxTraceInfoRecord& auxtrace_info,
67                                             ThreadTree& thread_tree);
~ETMDecoder()68   virtual ~ETMDecoder() {}
69   virtual void EnableDump(const ETMDumpOption& option) = 0;
70 
71   using InstrRangeCallbackFn = std::function<void(const ETMInstrRange&)>;
72   virtual void RegisterCallback(const InstrRangeCallbackFn& callback) = 0;
73 
74   using BranchListCallbackFn = std::function<void(const ETMBranchList&)>;
75   virtual void RegisterCallback(const BranchListCallbackFn& callback) = 0;
76 
77   virtual bool ProcessData(const uint8_t* data, size_t size) = 0;
78   virtual bool FinishData() = 0;
79 };
80 
81 // Map from addrs to a map of (branch_list, count).
82 // Use maps instead of unordered_maps. Because it helps locality by decoding instructions for sorted
83 // addresses.
84 using BranchMap = std::map<uint64_t, std::map<std::vector<bool>, uint64_t>>;
85 
86 android::base::expected<void, std::string> ConvertBranchMapToInstrRanges(
87     Dso* dso, const BranchMap& branch_map, const ETMDecoder::InstrRangeCallbackFn& callback);
88 
89 }  // namespace simpleperf