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 <inttypes.h>
20 
21 #include <map>
22 #include <memory>
23 
24 #include "event_type.h"
25 #include "perf_event.h"
26 #include "record.h"
27 
28 namespace simpleperf {
29 
30 struct ETMPerCpu {
31   uint32_t trcidr0;
32   uint32_t trcidr1;
33   uint32_t trcidr2;
34   uint32_t trcidr4;
35   uint32_t trcidr8;
36   uint32_t trcauthstatus;
37 
38   int GetMajorVersion() const;
39   bool IsContextIDSupported() const;
40   bool IsTimestampSupported() const;
41   bool IsEnabled() const;
42 };
43 
44 // Help recording Coresight ETM data on ARM devices.
45 // 1. Get etm event type on device.
46 // 2. Get sink config, which selects the ETR device moving etm data to memory.
47 // 3. Get etm info on each cpu.
48 // The etm event type and sink config are used to build perf_event_attr for etm data tracing.
49 // The etm info is kept in perf.data to help etm decoding.
50 class ETMRecorder {
51  public:
52   static ETMRecorder& GetInstance();
53 
54   // If not found, return -1.
55   int GetEtmEventType();
56   std::unique_ptr<EventType> BuildEventType();
57   bool CheckEtmSupport();
58   void SetEtmPerfEventAttr(perf_event_attr* attr);
59   AuxTraceInfoRecord CreateAuxTraceInfoRecord();
60   size_t GetAddrFilterPairs();
61 
62  private:
63   bool ReadEtmInfo();
64   bool FindSinkConfig();
65   void BuildEtmConfig();
66 
67   int event_type_ = 0;
68   bool etm_supported_ = false;
69   // select ETR device, setting in perf_event_attr->config2
70   uint32_t sink_config_ = 0;
71   // select etm options (timestamp, context_id, ...), setting in perf_event_attr->config
72   uint64_t etm_event_config_ = 0;
73   // record etm options in AuxTraceInfoRecord
74   uint32_t etm_config_reg_ = 0;
75   std::map<int, ETMPerCpu> etm_info_;
76 };
77 
78 }  // namespace simpleperf