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