1 //===-- NativeProcessNetBSD.h --------------------------------- -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef liblldb_NativeProcessNetBSD_H_
11 #define liblldb_NativeProcessNetBSD_H_
12 
13 // C++ Includes
14 
15 // Other libraries and framework includes
16 
17 #include "lldb/Core/ArchSpec.h"
18 #include "lldb/Target/MemoryRegionInfo.h"
19 #include "lldb/Utility/FileSpec.h"
20 
21 #include "NativeThreadNetBSD.h"
22 #include "lldb/Host/common/NativeProcessProtocol.h"
23 
24 namespace lldb_private {
25 namespace process_netbsd {
26 /// @class NativeProcessNetBSD
27 /// @brief Manages communication with the inferior (debugee) process.
28 ///
29 /// Upon construction, this class prepares and launches an inferior process for
30 /// debugging.
31 ///
32 /// Changes in the inferior process state are broadcasted.
33 class NativeProcessNetBSD : public NativeProcessProtocol {
34 public:
35   class Factory : public NativeProcessProtocol::Factory {
36   public:
37     llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
38     Launch(ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate,
39            MainLoop &mainloop) const override;
40 
41     llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
42     Attach(lldb::pid_t pid, NativeDelegate &native_delegate,
43            MainLoop &mainloop) const override;
44   };
45 
46   // ---------------------------------------------------------------------
47   // NativeProcessProtocol Interface
48   // ---------------------------------------------------------------------
49   Status Resume(const ResumeActionList &resume_actions) override;
50 
51   Status Halt() override;
52 
53   Status Detach() override;
54 
55   Status Signal(int signo) override;
56 
57   Status Kill() override;
58 
59   Status GetMemoryRegionInfo(lldb::addr_t load_addr,
60                              MemoryRegionInfo &range_info) override;
61 
62   Status ReadMemory(lldb::addr_t addr, void *buf, size_t size,
63                     size_t &bytes_read) override;
64 
65   Status ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size,
66                                size_t &bytes_read) override;
67 
68   Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size,
69                      size_t &bytes_written) override;
70 
71   Status AllocateMemory(size_t size, uint32_t permissions,
72                         lldb::addr_t &addr) override;
73 
74   Status DeallocateMemory(lldb::addr_t addr) override;
75 
76   lldb::addr_t GetSharedLibraryInfoAddress() override;
77 
78   size_t UpdateThreads() override;
79 
80   bool GetArchitecture(ArchSpec &arch) const override;
81 
82   Status SetBreakpoint(lldb::addr_t addr, uint32_t size,
83                        bool hardware) override;
84 
85   Status GetLoadedModuleFileSpec(const char *module_path,
86                                  FileSpec &file_spec) override;
87 
88   Status GetFileLoadAddress(const llvm::StringRef &file_name,
89                             lldb::addr_t &load_addr) override;
90 
91   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
92   GetAuxvData() const override;
93 
94   // ---------------------------------------------------------------------
95   // Interface used by NativeRegisterContext-derived classes.
96   // ---------------------------------------------------------------------
97   static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr = nullptr,
98                               int data = 0, int *result = nullptr);
99 
100 protected:
101   // ---------------------------------------------------------------------
102   // NativeProcessProtocol protected interface
103   // ---------------------------------------------------------------------
104 
105   Status
106   GetSoftwareBreakpointTrapOpcode(size_t trap_opcode_size_hint,
107                                   size_t &actual_opcode_size,
108                                   const uint8_t *&trap_opcode_bytes) override;
109 
110 private:
111   MainLoop::SignalHandleUP m_sigchld_handle;
112   ArchSpec m_arch;
113   LazyBool m_supports_mem_region = eLazyBoolCalculate;
114   std::vector<std::pair<MemoryRegionInfo, FileSpec>> m_mem_region_cache;
115 
116   // ---------------------------------------------------------------------
117   // Private Instance Methods
118   // ---------------------------------------------------------------------
119   NativeProcessNetBSD(::pid_t pid, int terminal_fd, NativeDelegate &delegate,
120                       const ArchSpec &arch, MainLoop &mainloop);
121 
122   bool HasThreadNoLock(lldb::tid_t thread_id);
123 
124   NativeThreadNetBSDSP AddThread(lldb::tid_t thread_id);
125 
126   void MonitorCallback(lldb::pid_t pid, int signal);
127   void MonitorExited(lldb::pid_t pid, WaitStatus status);
128   void MonitorSIGSTOP(lldb::pid_t pid);
129   void MonitorSIGTRAP(lldb::pid_t pid);
130   void MonitorSignal(lldb::pid_t pid, int signal);
131 
132   Status GetSoftwareBreakpointPCOffset(uint32_t &actual_opcode_size);
133   Status FixupBreakpointPCAsNeeded(NativeThreadNetBSD &thread);
134   Status PopulateMemoryRegionCache();
135   void SigchldHandler();
136 
137   Status Attach();
138   Status ReinitializeThreads();
139 };
140 
141 } // namespace process_netbsd
142 } // namespace lldb_private
143 
144 #endif // #ifndef liblldb_NativeProcessNetBSD_H_
145