1 /* 2 * Copyright (C) 2022 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 #include "src/kernel_utils/syscall_table.h" 17 18 #include "perfetto/base/build_config.h" 19 20 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \ 21 PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) 22 #include <sys/utsname.h> 23 #endif 24 25 #include "src/kernel_utils/syscalls_aarch32.h" 26 #include "src/kernel_utils/syscalls_aarch64.h" 27 #include "src/kernel_utils/syscalls_armeabi.h" 28 #include "src/kernel_utils/syscalls_x86.h" 29 #include "src/kernel_utils/syscalls_x86_64.h" 30 31 namespace perfetto { 32 33 template <typename T> GetSyscalls(const T &)34constexpr size_t GetSyscalls(const T&) { 35 static_assert(std::extent<T>::value <= kMaxSyscalls, 36 "kMaxSyscalls too small"); 37 return std::extent<T>::value; 38 } 39 SyscallTable(Architecture arch)40SyscallTable::SyscallTable(Architecture arch) { 41 static const char* kSyscalls_Unknown[] = {nullptr}; 42 43 switch (arch) { 44 case kArmEabi: 45 syscall_count_ = GetSyscalls(kSyscalls_ArmEabi); 46 syscall_table_ = &kSyscalls_ArmEabi[0]; 47 break; 48 case kAarch32: 49 syscall_count_ = GetSyscalls(kSyscalls_Aarch32); 50 syscall_table_ = &kSyscalls_Aarch32[0]; 51 break; 52 case kAarch64: 53 syscall_count_ = GetSyscalls(kSyscalls_Aarch64); 54 syscall_table_ = &kSyscalls_Aarch64[0]; 55 break; 56 case kX86_64: 57 syscall_count_ = GetSyscalls(kSyscalls_x86_64); 58 syscall_table_ = &kSyscalls_x86_64[0]; 59 break; 60 case kX86: 61 syscall_count_ = GetSyscalls(kSyscalls_x86); 62 syscall_table_ = &kSyscalls_x86[0]; 63 break; 64 case kUnknown: 65 syscall_count_ = 0; 66 syscall_table_ = &kSyscalls_Unknown[0]; 67 break; 68 } 69 } 70 ArchFromString(base::StringView machine)71Architecture SyscallTable::ArchFromString(base::StringView machine) { 72 if (machine == "aarch64") { 73 return kAarch64; 74 } else if (machine == "armv8l") { 75 return kArmEabi; 76 } else if (machine == "armv7l") { 77 return kAarch32; 78 } else if (machine == "x86_64") { 79 return kX86_64; 80 } else if (machine == "i686") { 81 return kX86; 82 } else { 83 return kUnknown; 84 } 85 } 86 FromCurrentArch()87SyscallTable SyscallTable::FromCurrentArch() { 88 Architecture arch = kUnknown; 89 90 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \ 91 PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) 92 struct utsname uname_info; 93 if (uname(&uname_info) == 0) { 94 arch = ArchFromString(uname_info.machine); 95 } 96 #endif 97 98 return SyscallTable(arch); 99 } 100 GetByName(const std::string & name) const101std::optional<size_t> SyscallTable::GetByName(const std::string& name) const { 102 for (size_t i = 0; i < syscall_count_; i++) { 103 if (name == syscall_table_[i]) { 104 return i; 105 } 106 } 107 return std::nullopt; 108 } 109 GetById(size_t id) const110const char* SyscallTable::GetById(size_t id) const { 111 if (id < syscall_count_) { 112 return syscall_table_[id]; 113 } 114 return nullptr; 115 } 116 117 } // namespace perfetto 118