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 "record.h"
24 #include "thread_tree.h"
25 
26 namespace simpleperf {
27 
28 struct ETMDumpOption {
29   bool dump_raw_data = false;
30   bool dump_packets = false;
31   bool dump_elements = false;
32 };
33 
34 bool ParseEtmDumpOption(const std::string& s, ETMDumpOption* option);
35 
36 struct ETMInstrRange {
37   // the binary containing the instruction range
38   Dso* dso = nullptr;
39   // the address of the first instruction in the binary
40   uint64_t start_addr = 0;
41   // the address of the last instruction in the binary
42   uint64_t end_addr = 0;
43   // If the last instruction is a branch instruction, and it branches
44   // to a fixed location in the same binary, then branch_to_addr points
45   // to the branched to instruction.
46   uint64_t branch_to_addr = 0;
47   // times the branch is taken
48   uint64_t branch_taken_count = 0;
49   // times the branch isn't taken
50   uint64_t branch_not_taken_count = 0;
51 };
52 
53 class ETMDecoder {
54  public:
55   static std::unique_ptr<ETMDecoder> Create(const AuxTraceInfoRecord& auxtrace_info,
56                                             ThreadTree& thread_tree);
~ETMDecoder()57   virtual ~ETMDecoder() {}
58   virtual void EnableDump(const ETMDumpOption& option) = 0;
59 
60   using CallbackFn = std::function<void(const ETMInstrRange&)>;
61   virtual void RegisterCallback(const CallbackFn& callback) = 0;
62 
63   virtual bool ProcessData(const uint8_t* data, size_t size) = 0;
64 };
65 
66 }  // namespace simpleperf