xref: /aosp_15_r20/external/perfetto/src/kernel_utils/syscall_table.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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 
17 #ifndef SRC_KERNEL_UTILS_SYSCALL_TABLE_H_
18 #define SRC_KERNEL_UTILS_SYSCALL_TABLE_H_
19 
20 #include <optional>
21 #include <string>
22 
23 #include "perfetto/ext/base/string_view.h"
24 #include "perfetto/ext/base/utils.h"
25 
26 namespace perfetto {
27 
28 static constexpr size_t kMaxSyscalls = 550;
29 
30 enum class Architecture {
31   kUnknown = 0,
32   kArm64,
33   kArm32,
34   kX86_64,
35   kX86,
36 };
37 
38 class SyscallTable {
39  public:
40   using OffT = uint16_t;
41   // Exposed for testing.
42   template <typename Table>
Load()43   static SyscallTable Load() {
44     static_assert(base::ArraySize(Table::offsets) <= kMaxSyscalls,
45                   "kMaxSyscalls too small");
46     return SyscallTable(Table::names, Table::offsets,
47                         base::ArraySize(Table::offsets));
48   }
49 
50   explicit SyscallTable(Architecture arch);
51 
52   // Return the architecture enum for the given uname machine string.
53   static Architecture ArchFromString(base::StringView machine);
54 
55   // Returns the syscall table based on the current machine's architecture. Only
56   // works on Linux based systems.
57   static SyscallTable FromCurrentArch();
58 
59   // Returns the syscall id for the syscall with the given name. If the syscall
60   // is not found, returns std::nullopt.
61   std::optional<size_t> GetByName(const std::string& name) const;
62 
63   // Returns the syscall name for the syscall with the given id. If the syscall
64   // is not found, returns nullptr.
65   const char* GetById(size_t id) const;
66 
67  private:
SyscallTable(const char * names,const OffT * off,size_t count)68   SyscallTable(const char* names, const OffT* off, size_t count)
69       : syscall_names_(names), syscall_offsets_(off), syscall_count_(count) {}
70 
71   const char* syscall_names_ = "";
72   const OffT* syscall_offsets_ = {};
73   size_t syscall_count_ = 0;
74 };
75 }  // namespace perfetto
76 
77 #endif  // SRC_KERNEL_UTILS_SYSCALL_TABLE_H_
78