1 //===-- NativeProcessFreeBSD.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 liblldb_NativeProcessFreeBSD_H_
10 #define liblldb_NativeProcessFreeBSD_H_
11 
12 #include "Plugins/Process/POSIX/NativeProcessELF.h"
13 #include "Plugins/Process/Utility/NativeProcessSoftwareSingleStep.h"
14 
15 #include "lldb/Target/MemoryRegionInfo.h"
16 #include "lldb/Utility/ArchSpec.h"
17 #include "lldb/Utility/FileSpec.h"
18 
19 #include "NativeThreadFreeBSD.h"
20 
21 namespace lldb_private {
22 namespace process_freebsd {
23 /// \class NativeProcessFreeBSD
24 /// Manages communication with the inferior (debugee) process.
25 ///
26 /// Upon construction, this class prepares and launches an inferior process
27 /// for debugging.
28 ///
29 /// Changes in the inferior process state are broadcasted.
30 class NativeProcessFreeBSD : public NativeProcessELF,
31                              private NativeProcessSoftwareSingleStep {
32 public:
33   class Factory : public NativeProcessProtocol::Factory {
34   public:
35     llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
36     Launch(ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate,
37            MainLoop &mainloop) const override;
38 
39     llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
40     Attach(lldb::pid_t pid, NativeDelegate &native_delegate,
41            MainLoop &mainloop) const override;
42 
43     Extension GetSupportedExtensions() const override;
44   };
45 
46   // NativeProcessProtocol Interface
47   Status Resume(const ResumeActionList &resume_actions) override;
48 
49   Status Halt() override;
50 
51   Status Detach() override;
52 
53   Status Signal(int signo) override;
54 
55   Status Interrupt() 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 WriteMemory(lldb::addr_t addr, const void *buf, size_t size,
66                      size_t &bytes_written) override;
67 
68   size_t UpdateThreads() override;
69 
70   const ArchSpec &GetArchitecture() const override { return m_arch; }
71 
72   Status SetBreakpoint(lldb::addr_t addr, uint32_t size,
73                        bool hardware) override;
74 
75   // The two following methods are probably not necessary and probably
76   // will never be called.  Nevertheless, we implement them right now
77   // to reduce the differences between different platforms and reduce
78   // the risk of the lack of implementation actually breaking something,
79   // at least for the time being.
80   Status GetLoadedModuleFileSpec(const char *module_path,
81                                  FileSpec &file_spec) override;
82   Status GetFileLoadAddress(const llvm::StringRef &file_name,
83                             lldb::addr_t &load_addr) override;
84 
85   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
86   GetAuxvData() const override;
87 
88   // Interface used by NativeRegisterContext-derived classes.
89   static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr = nullptr,
90                               int data = 0, int *result = nullptr);
91 
92   bool SupportHardwareSingleStepping() const;
93 
94 protected:
95   llvm::Expected<llvm::ArrayRef<uint8_t>>
96   GetSoftwareBreakpointTrapOpcode(size_t size_hint) override;
97 
98 private:
99   MainLoop::SignalHandleUP m_sigchld_handle;
100   ArchSpec m_arch;
101   MainLoop& m_main_loop;
102   LazyBool m_supports_mem_region = eLazyBoolCalculate;
103   std::vector<std::pair<MemoryRegionInfo, FileSpec>> m_mem_region_cache;
104 
105   // Private Instance Methods
106   NativeProcessFreeBSD(::pid_t pid, int terminal_fd, NativeDelegate &delegate,
107                        const ArchSpec &arch, MainLoop &mainloop);
108 
109   bool HasThreadNoLock(lldb::tid_t thread_id);
110 
111   NativeThreadFreeBSD &AddThread(lldb::tid_t thread_id);
112   void RemoveThread(lldb::tid_t thread_id);
113 
114   void MonitorCallback(lldb::pid_t pid, int signal);
115   void MonitorExited(lldb::pid_t pid, WaitStatus status);
116   void MonitorSIGSTOP(lldb::pid_t pid);
117   void MonitorSIGTRAP(lldb::pid_t pid);
118   void MonitorSignal(lldb::pid_t pid, int signal);
119   void MonitorClone(::pid_t child_pid, bool is_vfork,
120                     NativeThreadFreeBSD &parent_thread);
121 
122   Status PopulateMemoryRegionCache();
123   void SigchldHandler();
124 
125   Status Attach();
126   Status SetupTrace();
127   Status ReinitializeThreads();
128 };
129 
130 } // namespace process_freebsd
131 } // namespace lldb_private
132 
133 #endif // #ifndef liblldb_NativeProcessFreeBSD_H_
134