• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <memory>
21 #include <optional>
22 #include <string>
23 
24 #include "perfetto/ext/base/string_view.h"
25 
26 namespace perfetto {
27 
28 static constexpr size_t kMaxSyscalls = 550;
29 
30 enum Architecture {
31   kUnknown = 0,
32   kArmEabi,  // 32-bit kernel running a 32-bit process (most old devices).
33   kAarch32,  // 64-bit kernel running a 32-bit process (should be rare).
34   kAarch64,  // 64-bit kernel running a 64-bit process (most new devices).
35   kX86_64,
36   kX86,
37 };
38 
39 class SyscallTable {
40  public:
41   explicit SyscallTable(Architecture arch);
42 
43   // Use for testing.
SyscallTable(const char * const * table,size_t count)44   SyscallTable(const char* const* table, size_t count)
45       : syscall_count_(count), syscall_table_(table) {}
46 
47   // Return the architecture enum for the given uname machine string.
48   static Architecture ArchFromString(base::StringView machine);
49 
50   // Returns the syscall table based on the current machine's architecture. Only
51   // works on Linux based systems.
52   static SyscallTable FromCurrentArch();
53 
54   // Returns the syscall id for the syscall with the given name. If the syscall
55   // is not found, returns std::nullopt.
56   std::optional<size_t> GetByName(const std::string& name) const;
57 
58   // Returns the syscall name for the syscall with the given id. If the syscall
59   // is not found, returns nullptr.
60   const char* GetById(size_t id) const;
61 
62  private:
63   size_t syscall_count_;
64   const char* const* syscall_table_;
65 };
66 }  // namespace perfetto
67 
68 #endif  // SRC_KERNEL_UTILS_SYSCALL_TABLE_H_
69