• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 #ifndef SIMPLE_PERF_PERF_REGS_H_
18 #define SIMPLE_PERF_PERF_REGS_H_
19 
20 #if defined(USE_BIONIC_UAPI_HEADERS)
21 #include <uapi/asm-x86/asm/perf_regs.h>
22 #include <uapi/asm-arm/asm/perf_regs.h>
23 #define perf_event_arm_regs perf_event_arm64_regs
24 #include <uapi/asm-arm64/asm/perf_regs.h>
25 #else
26 #include <asm-x86/asm/perf_regs.h>
27 #include <asm-arm/asm/perf_regs.h>
28 #define perf_event_arm_regs perf_event_arm64_regs
29 #include <asm-arm64/asm/perf_regs.h>
30 #endif
31 
32 #include <stdint.h>
33 #include <string>
34 #include <vector>
35 
36 #include "perf_event.h"
37 
38 enum ArchType {
39   ARCH_X86_32,
40   ARCH_X86_64,
41   ARCH_ARM,
42   ARCH_ARM64,
43   ARCH_UNSUPPORTED,
44 };
45 
GetBuildArch()46 constexpr ArchType GetBuildArch() {
47 #if defined(__i386__)
48   return ARCH_X86_32;
49 #elif defined(__x86_64__)
50   return ARCH_X86_64;
51 #elif defined(__aarch64__)
52   return ARCH_ARM64;
53 #elif defined(__arm__)
54   return ARCH_ARM;
55 #else
56   return ARCH_UNSUPPORTED;
57 #endif
58 }
59 
60 ArchType GetArchType(const std::string& arch);
61 ArchType GetArchForAbi(ArchType machine_arch, int abi);
62 std::string GetArchString(ArchType arch);
63 uint64_t GetSupportedRegMask(ArchType arch);
64 std::string GetRegName(size_t regno, ArchType arch);
65 
66 class ScopedCurrentArch {
67  public:
ScopedCurrentArch(ArchType arch)68   explicit ScopedCurrentArch(ArchType arch) : saved_arch(current_arch) {
69     current_arch = arch;
70     current_arch32 = GetArchForAbi(arch, PERF_SAMPLE_REGS_ABI_32);
71   }
~ScopedCurrentArch()72   ~ScopedCurrentArch() {
73     current_arch = saved_arch;
74     current_arch32 = GetArchForAbi(saved_arch, PERF_SAMPLE_REGS_ABI_32);
75   }
GetCurrentArch()76   static ArchType GetCurrentArch() {
77     return current_arch;
78   }
GetCurrentArch32()79   static ArchType GetCurrentArch32() {
80     return current_arch32;
81   }
82 
83  private:
84   ArchType saved_arch;
85   static ArchType current_arch;
86   static ArchType current_arch32;
87 };
88 
89 struct RegSet {
90   ArchType arch;
91   // For each setting bit in valid_mask, there is a valid reg value in data[].
92   uint64_t valid_mask;
93   // Stores reg values. Values for invalid regs are 0.
94   uint64_t data[64];
95 
96   RegSet(int abi, uint64_t valid_mask, const uint64_t* valid_regs);
97 
98   bool GetRegValue(size_t regno, uint64_t* value) const;
99   bool GetSpRegValue(uint64_t* value) const;
100   bool GetIpRegValue(uint64_t* value) const;
101 };
102 
103 #endif  // SIMPLE_PERF_PERF_REGS_H_
104