1 //===-- NativeRegisterContextLinux.h ----------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef lldb_NativeRegisterContextLinux_h
10 #define lldb_NativeRegisterContextLinux_h
11 
12 #include "Plugins/Process/Utility/NativeRegisterContextRegisterInfo.h"
13 #include "lldb/Host/common/NativeThreadProtocol.h"
14 
15 namespace lldb_private {
16 namespace process_linux {
17 
18 class NativeThreadLinux;
19 
20 class NativeRegisterContextLinux
21     : public virtual NativeRegisterContextRegisterInfo {
22 public:
23   // This function is implemented in the NativeRegisterContextLinux_* subclasses
24   // to create a new instance of the host specific NativeRegisterContextLinux.
25   // The implementations can't collide as only one NativeRegisterContextLinux_*
26   // variant should be compiled into the final executable.
27   static std::unique_ptr<NativeRegisterContextLinux>
28   CreateHostNativeRegisterContextLinux(const ArchSpec &target_arch,
29                                        NativeThreadLinux &native_thread);
30 
31   // Invalidates cached values in register context data structures
32   virtual void InvalidateAllRegisters(){}
33 
34   struct SyscallData {
35     /// The syscall instruction. If the architecture uses software
36     /// single-stepping, the instruction should also be followed by a trap to
37     /// ensure the process is stopped after the syscall.
38     llvm::ArrayRef<uint8_t> Insn;
39 
40     /// Registers used for syscall arguments. The first register is used to
41     /// store the syscall number.
42     llvm::ArrayRef<uint32_t> Args;
43 
44     uint32_t Result; ///< Register containing the syscall result.
45   };
46   /// Return architecture-specific data needed to make inferior syscalls, if
47   /// they are supported.
48   virtual llvm::Optional<SyscallData> GetSyscallData() { return llvm::None; }
49 
50   struct MmapData {
51     // Syscall numbers can be found (e.g.) in /usr/include/asm/unistd.h for the
52     // relevant architecture.
53     unsigned SysMmap;   ///< mmap syscall number.
54     unsigned SysMunmap; ///< munmap syscall number
55   };
56   /// Return the architecture-specific data needed to make mmap syscalls, if
57   /// they are supported.
58   virtual llvm::Optional<MmapData> GetMmapData() { return llvm::None; }
59 
60 protected:
61   // NB: This constructor is here only because gcc<=6.5 requires a virtual base
62   // class initializer on abstract class (even though it is never used). It can
63   // be deleted once we move to gcc>=7.0.
64   NativeRegisterContextLinux(NativeThreadProtocol &thread)
65       : NativeRegisterContextRegisterInfo(thread, nullptr) {}
66 
67   lldb::ByteOrder GetByteOrder() const;
68 
69   virtual Status ReadRegisterRaw(uint32_t reg_index, RegisterValue &reg_value);
70 
71   virtual Status WriteRegisterRaw(uint32_t reg_index,
72                                   const RegisterValue &reg_value);
73 
74   virtual Status ReadRegisterSet(void *buf, size_t buf_size,
75                                  unsigned int regset);
76 
77   virtual Status WriteRegisterSet(void *buf, size_t buf_size,
78                                   unsigned int regset);
79 
80   virtual Status ReadGPR();
81 
82   virtual Status WriteGPR();
83 
84   virtual Status ReadFPR();
85 
86   virtual Status WriteFPR();
87 
88   virtual void *GetGPRBuffer() = 0;
89 
90   virtual size_t GetGPRSize() const {
91     return GetRegisterInfoInterface().GetGPRSize();
92   }
93 
94   virtual void *GetFPRBuffer() = 0;
95 
96   virtual size_t GetFPRSize() = 0;
97 
98   virtual uint32_t GetPtraceOffset(uint32_t reg_index) {
99     return GetRegisterInfoAtIndex(reg_index)->byte_offset;
100   }
101 
102   // The Do*** functions are executed on the privileged thread and can perform
103   // ptrace
104   // operations directly.
105   virtual Status DoReadRegisterValue(uint32_t offset, const char *reg_name,
106                                      uint32_t size, RegisterValue &value);
107 
108   virtual Status DoWriteRegisterValue(uint32_t offset, const char *reg_name,
109                                       const RegisterValue &value);
110 };
111 
112 } // namespace process_linux
113 } // namespace lldb_private
114 
115 #endif // #ifndef lldb_NativeRegisterContextLinux_h
116