1af245d11STodd Fiala //===-- NativeProcessLinux.h ---------------------------------- -*- C++ -*-===//
2af245d11STodd Fiala //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6af245d11STodd Fiala //
7af245d11STodd Fiala //===----------------------------------------------------------------------===//
8af245d11STodd Fiala 
9af245d11STodd Fiala #ifndef liblldb_NativeProcessLinux_H_
10af245d11STodd Fiala #define liblldb_NativeProcessLinux_H_
11af245d11STodd Fiala 
12b6dbe9a9SPavel Labath #include <csignal>
13af245d11STodd Fiala #include <unordered_set>
14af245d11STodd Fiala 
15af245d11STodd Fiala #include "lldb/Host/Debug.h"
1639de3110SZachary Turner #include "lldb/Host/HostThread.h"
17b7f0f45fSPavel Labath #include "lldb/Host/linux/Support.h"
18af245d11STodd Fiala #include "lldb/Target/MemoryRegionInfo.h"
195f19b907SPavel Labath #include "lldb/Utility/ArchSpec.h"
205713a05bSZachary Turner #include "lldb/Utility/FileSpec.h"
21b9c1b51eSKate Stone #include "lldb/lldb-types.h"
22af245d11STodd Fiala 
238c8ff7afSPavel Labath #include "NativeThreadLinux.h"
24*f4335b8eSAntonio Afonso #include "Plugins/Process/POSIX/NativeProcessELF.h"
2599e37695SRavitheja Addepally #include "ProcessorTrace.h"
26af245d11STodd Fiala 
27db264a6dSTamas Berghammer namespace lldb_private {
2897206d57SZachary Turner class Status;
29af245d11STodd Fiala class Scalar;
30af245d11STodd Fiala 
31db264a6dSTamas Berghammer namespace process_linux {
32f05b42e9SAdrian Prantl /// \class NativeProcessLinux
33d8f460e8SAdrian Prantl /// Manages communication with the inferior (debugee) process.
34af245d11STodd Fiala ///
35d8f460e8SAdrian Prantl /// Upon construction, this class prepares and launches an inferior process
36d8f460e8SAdrian Prantl /// for debugging.
37af245d11STodd Fiala ///
38af245d11STodd Fiala /// Changes in the inferior process state are broadcasted.
39*f4335b8eSAntonio Afonso class NativeProcessLinux : public NativeProcessELF {
40c307c270SSean Callanan public:
4196e600fcSPavel Labath   class Factory : public NativeProcessProtocol::Factory {
4296e600fcSPavel Labath   public:
4382abefa4SPavel Labath     llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
4496e600fcSPavel Labath     Launch(ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate,
4596e600fcSPavel Labath            MainLoop &mainloop) const override;
4696e600fcSPavel Labath 
4782abefa4SPavel Labath     llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
4896e600fcSPavel Labath     Attach(lldb::pid_t pid, NativeDelegate &native_delegate,
4996e600fcSPavel Labath            MainLoop &mainloop) const override;
5096e600fcSPavel Labath   };
5196e600fcSPavel Labath 
52af245d11STodd Fiala   // NativeProcessProtocol Interface
5397206d57SZachary Turner   Status Resume(const ResumeActionList &resume_actions) override;
54af245d11STodd Fiala 
5597206d57SZachary Turner   Status Halt() override;
56af245d11STodd Fiala 
5797206d57SZachary Turner   Status Detach() override;
58af245d11STodd Fiala 
5997206d57SZachary Turner   Status Signal(int signo) override;
60af245d11STodd Fiala 
6197206d57SZachary Turner   Status Interrupt() override;
62e9547b80SChaoren Lin 
6397206d57SZachary Turner   Status Kill() override;
64af245d11STodd Fiala 
6597206d57SZachary Turner   Status GetMemoryRegionInfo(lldb::addr_t load_addr,
66b9c1b51eSKate Stone                              MemoryRegionInfo &range_info) override;
67af245d11STodd Fiala 
6897206d57SZachary Turner   Status ReadMemory(lldb::addr_t addr, void *buf, size_t size,
69b9c1b51eSKate Stone                     size_t &bytes_read) override;
70af245d11STodd Fiala 
7197206d57SZachary Turner   Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size,
72b9c1b51eSKate Stone                      size_t &bytes_written) override;
733eb4b458SChaoren Lin 
7497206d57SZachary Turner   Status AllocateMemory(size_t size, uint32_t permissions,
75b9c1b51eSKate Stone                         lldb::addr_t &addr) override;
76af245d11STodd Fiala 
7797206d57SZachary Turner   Status DeallocateMemory(lldb::addr_t addr) override;
78af245d11STodd Fiala 
79b9c1b51eSKate Stone   size_t UpdateThreads() override;
80af245d11STodd Fiala 
81578a4258SPavel Labath   const ArchSpec &GetArchitecture() const override { return m_arch; }
82af245d11STodd Fiala 
8397206d57SZachary Turner   Status SetBreakpoint(lldb::addr_t addr, uint32_t size,
8497206d57SZachary Turner                        bool hardware) override;
85af245d11STodd Fiala 
8697206d57SZachary Turner   Status RemoveBreakpoint(lldb::addr_t addr, bool hardware = false) override;
87d5ffbad2SOmair Javaid 
88b9c1b51eSKate Stone   void DoStopIDBumped(uint32_t newBumpId) override;
89af245d11STodd Fiala 
9097206d57SZachary Turner   Status GetLoadedModuleFileSpec(const char *module_path,
91b9c1b51eSKate Stone                                  FileSpec &file_spec) override;
92068f8a7eSTamas Berghammer 
9397206d57SZachary Turner   Status GetFileLoadAddress(const llvm::StringRef &file_name,
94b9c1b51eSKate Stone                             lldb::addr_t &load_addr) override;
95783bfc8cSTamas Berghammer 
96a5be48b3SPavel Labath   NativeThreadLinux *GetThreadByID(lldb::tid_t id);
97f9077782SPavel Labath 
98b7f0f45fSPavel Labath   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
99b7f0f45fSPavel Labath   GetAuxvData() const override {
100b7f0f45fSPavel Labath     return getProcFile(GetID(), "auxv");
101b7f0f45fSPavel Labath   }
102b7f0f45fSPavel Labath 
10399e37695SRavitheja Addepally   lldb::user_id_t StartTrace(const TraceOptions &config,
10499e37695SRavitheja Addepally                              Status &error) override;
10599e37695SRavitheja Addepally 
10699e37695SRavitheja Addepally   Status StopTrace(lldb::user_id_t traceid,
10799e37695SRavitheja Addepally                    lldb::tid_t thread) override;
10899e37695SRavitheja Addepally 
10999e37695SRavitheja Addepally   Status GetData(lldb::user_id_t traceid, lldb::tid_t thread,
11099e37695SRavitheja Addepally                  llvm::MutableArrayRef<uint8_t> &buffer,
11199e37695SRavitheja Addepally                  size_t offset = 0) override;
11299e37695SRavitheja Addepally 
11399e37695SRavitheja Addepally   Status GetMetaData(lldb::user_id_t traceid, lldb::tid_t thread,
11499e37695SRavitheja Addepally                      llvm::MutableArrayRef<uint8_t> &buffer,
11599e37695SRavitheja Addepally                      size_t offset = 0) override;
11699e37695SRavitheja Addepally 
11799e37695SRavitheja Addepally   Status GetTraceConfig(lldb::user_id_t traceid, TraceOptions &config) override;
11899e37695SRavitheja Addepally 
119af245d11STodd Fiala   // Interface used by NativeRegisterContext-derived classes.
12097206d57SZachary Turner   static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr = nullptr,
121b9c1b51eSKate Stone                               void *data = nullptr, size_t data_size = 0,
1224a9babb2SPavel Labath                               long *result = nullptr);
1237cb18bf5STamas Berghammer 
124b9c1b51eSKate Stone   bool SupportHardwareSingleStepping() const;
125605b51b8SPavel Labath 
126af245d11STodd Fiala protected:
127f8b825f6SPavel Labath   llvm::Expected<llvm::ArrayRef<uint8_t>>
128f8b825f6SPavel Labath   GetSoftwareBreakpointTrapOpcode(size_t size_hint) override;
129af245d11STodd Fiala 
130af245d11STodd Fiala private:
13119cbe96aSPavel Labath   MainLoop::SignalHandleUP m_sigchld_handle;
132db264a6dSTamas Berghammer   ArchSpec m_arch;
133af245d11STodd Fiala 
13496e600fcSPavel Labath   LazyBool m_supports_mem_region = eLazyBoolCalculate;
135a6f5795aSTamas Berghammer   std::vector<std::pair<MemoryRegionInfo, FileSpec>> m_mem_region_cache;
136af245d11STodd Fiala 
13796e600fcSPavel Labath   lldb::tid_t m_pending_notification_tid = LLDB_INVALID_THREAD_ID;
1380e1d729bSPavel Labath 
139d8c338d4STamas Berghammer   // List of thread ids stepping with a breakpoint with the address of
140d8c338d4STamas Berghammer   // the relevan breakpoint
141d8c338d4STamas Berghammer   std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint;
142d8c338d4STamas Berghammer 
143af245d11STodd Fiala   // Private Instance Methods
14496e600fcSPavel Labath   NativeProcessLinux(::pid_t pid, int terminal_fd, NativeDelegate &delegate,
14582abefa4SPavel Labath                      const ArchSpec &arch, MainLoop &mainloop,
14682abefa4SPavel Labath                      llvm::ArrayRef<::pid_t> tids);
147af245d11STodd Fiala 
14896e600fcSPavel Labath   // Returns a list of process threads that we have attached to.
14996e600fcSPavel Labath   static llvm::Expected<std::vector<::pid_t>> Attach(::pid_t pid);
150af245d11STodd Fiala 
15197206d57SZachary Turner   static Status SetDefaultPtraceOpts(const lldb::pid_t);
152af245d11STodd Fiala 
1533508fc8cSPavel Labath   void MonitorCallback(lldb::pid_t pid, bool exited, WaitStatus status);
154af245d11STodd Fiala 
155b9c1b51eSKate Stone   void WaitForNewThread(::pid_t tid);
156426bdf88SPavel Labath 
157b9c1b51eSKate Stone   void MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread);
158af245d11STodd Fiala 
159b9c1b51eSKate Stone   void MonitorTrace(NativeThreadLinux &thread);
160c16f5dcaSChaoren Lin 
161b9c1b51eSKate Stone   void MonitorBreakpoint(NativeThreadLinux &thread);
162c16f5dcaSChaoren Lin 
163b9c1b51eSKate Stone   void MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index);
164c16f5dcaSChaoren Lin 
165b9c1b51eSKate Stone   void MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread,
166b9c1b51eSKate Stone                      bool exited);
167af245d11STodd Fiala 
16897206d57SZachary Turner   Status SetupSoftwareSingleStepping(NativeThreadLinux &thread);
169e7708688STamas Berghammer 
170b9c1b51eSKate Stone   bool HasThreadNoLock(lldb::tid_t thread_id);
171af245d11STodd Fiala 
172b9c1b51eSKate Stone   bool StopTrackingThread(lldb::tid_t thread_id);
173af245d11STodd Fiala 
174a5be48b3SPavel Labath   NativeThreadLinux &AddThread(lldb::tid_t thread_id);
175af245d11STodd Fiala 
176af245d11STodd Fiala   /// Writes a siginfo_t structure corresponding to the given thread ID to the
177f05b42e9SAdrian Prantl   /// memory region pointed to by \p siginfo.
17897206d57SZachary Turner   Status GetSignalInfo(lldb::tid_t tid, void *siginfo);
179af245d11STodd Fiala 
180af245d11STodd Fiala   /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
181af245d11STodd Fiala   /// corresponding to the given thread ID to the memory pointed to by @p
182af245d11STodd Fiala   /// message.
18397206d57SZachary Turner   Status GetEventMessage(lldb::tid_t tid, unsigned long *message);
184af245d11STodd Fiala 
185b9c1b51eSKate Stone   void NotifyThreadDeath(lldb::tid_t tid);
186fa03ad2eSChaoren Lin 
18797206d57SZachary Turner   Status Detach(lldb::tid_t tid);
18886fd8e45SChaoren Lin 
189b9c1b51eSKate Stone   // This method is requests a stop on all threads which are still running. It
190b9c1b51eSKate Stone   // sets up a
191b9c1b51eSKate Stone   // deferred delegate notification, which will fire once threads report as
192b9c1b51eSKate Stone   // stopped. The
1931dbc6c9cSPavel Labath   // triggerring_tid will be set as the current thread (main stop reason).
194b9c1b51eSKate Stone   void StopRunningThreads(lldb::tid_t triggering_tid);
195c076559aSPavel Labath 
1969eb1ecb9SPavel Labath   // Notify the delegate if all threads have stopped.
1979eb1ecb9SPavel Labath   void SignalIfAllThreadsStopped();
198c076559aSPavel Labath 
199b9c1b51eSKate Stone   // Resume the given thread, optionally passing it the given signal. The type
200b9c1b51eSKate Stone   // of resume
2010e1d729bSPavel Labath   // operation (continue, single-step) depends on the state parameter.
20297206d57SZachary Turner   Status ResumeThread(NativeThreadLinux &thread, lldb::StateType state,
203b9c1b51eSKate Stone                       int signo);
204c076559aSPavel Labath 
205b9c1b51eSKate Stone   void ThreadWasCreated(NativeThreadLinux &thread);
206c076559aSPavel Labath 
207b9c1b51eSKate Stone   void SigchldHandler();
208a6f5795aSTamas Berghammer 
20997206d57SZachary Turner   Status PopulateMemoryRegionCache();
21099e37695SRavitheja Addepally 
21199e37695SRavitheja Addepally   lldb::user_id_t StartTraceGroup(const TraceOptions &config,
21299e37695SRavitheja Addepally                                          Status &error);
21399e37695SRavitheja Addepally 
21499e37695SRavitheja Addepally   // This function is intended to be used to stop tracing
21599e37695SRavitheja Addepally   // on a thread that exited.
21699e37695SRavitheja Addepally   Status StopTracingForThread(lldb::tid_t thread);
21799e37695SRavitheja Addepally 
21899e37695SRavitheja Addepally   // The below function as the name suggests, looks up a ProcessorTrace
21999e37695SRavitheja Addepally   // instance from the m_processor_trace_monitor map. In the case of
22099e37695SRavitheja Addepally   // process tracing where the traceid passed would map to the complete
22199e37695SRavitheja Addepally   // process, it is mandatory to provide a threadid to obtain a trace
22299e37695SRavitheja Addepally   // instance (since ProcessorTrace is tied to a thread). In the other
22399e37695SRavitheja Addepally   // scenario that an individual thread is being traced, just the traceid
22499e37695SRavitheja Addepally   // is sufficient to obtain the actual ProcessorTrace instance.
22599e37695SRavitheja Addepally   llvm::Expected<ProcessorTraceMonitor &>
22699e37695SRavitheja Addepally   LookupProcessorTraceInstance(lldb::user_id_t traceid, lldb::tid_t thread);
22799e37695SRavitheja Addepally 
22899e37695SRavitheja Addepally   // Stops tracing on individual threads being traced. Not intended
22999e37695SRavitheja Addepally   // to be used to stop tracing on complete process.
23099e37695SRavitheja Addepally   Status StopProcessorTracingOnThread(lldb::user_id_t traceid,
23199e37695SRavitheja Addepally                                       lldb::tid_t thread);
23299e37695SRavitheja Addepally 
23399e37695SRavitheja Addepally   // Intended to stop tracing on complete process.
23499e37695SRavitheja Addepally   // Should not be used for stopping trace on
23599e37695SRavitheja Addepally   // individual threads.
23699e37695SRavitheja Addepally   void StopProcessorTracingOnProcess();
23799e37695SRavitheja Addepally 
23899e37695SRavitheja Addepally   llvm::DenseMap<lldb::tid_t, ProcessorTraceMonitorUP>
23999e37695SRavitheja Addepally       m_processor_trace_monitor;
24099e37695SRavitheja Addepally 
24199e37695SRavitheja Addepally   // Set for tracking threads being traced under
24299e37695SRavitheja Addepally   // same process user id.
24399e37695SRavitheja Addepally   llvm::DenseSet<lldb::tid_t> m_pt_traced_thread_group;
24499e37695SRavitheja Addepally 
24596e600fcSPavel Labath   lldb::user_id_t m_pt_proces_trace_id = LLDB_INVALID_UID;
24699e37695SRavitheja Addepally   TraceOptions m_pt_process_trace_config;
247af245d11STodd Fiala };
248db264a6dSTamas Berghammer 
249db264a6dSTamas Berghammer } // namespace process_linux
250db264a6dSTamas Berghammer } // namespace lldb_private
251af245d11STodd Fiala 
252af245d11STodd Fiala #endif // #ifndef liblldb_NativeProcessLinux_H_
253