1af245d11STodd Fiala //===-- NativeProcessLinux.h ---------------------------------- -*- C++ -*-===//
2af245d11STodd Fiala //
3af245d11STodd Fiala //                     The LLVM Compiler Infrastructure
4af245d11STodd Fiala //
5af245d11STodd Fiala // This file is distributed under the University of Illinois Open Source
6af245d11STodd Fiala // License. See LICENSE.TXT for details.
7af245d11STodd Fiala //
8af245d11STodd Fiala //===----------------------------------------------------------------------===//
9af245d11STodd Fiala 
10af245d11STodd Fiala #ifndef liblldb_NativeProcessLinux_H_
11af245d11STodd Fiala #define liblldb_NativeProcessLinux_H_
12af245d11STodd Fiala 
13af245d11STodd Fiala // C++ Includes
14af245d11STodd Fiala #include <unordered_set>
15af245d11STodd Fiala 
16af245d11STodd Fiala // Other libraries and framework includes
17af245d11STodd Fiala #include "lldb/Core/ArchSpec.h"
18af245d11STodd Fiala #include "lldb/lldb-types.h"
19af245d11STodd Fiala #include "lldb/Host/Debug.h"
20d3173f34SChaoren Lin #include "lldb/Host/FileSpec.h"
2139de3110SZachary Turner #include "lldb/Host/HostThread.h"
22af245d11STodd Fiala #include "lldb/Host/Mutex.h"
23af245d11STodd Fiala #include "lldb/Target/MemoryRegionInfo.h"
24af245d11STodd Fiala 
252fe1d0abSChaoren Lin #include "lldb/Host/common/NativeProcessProtocol.h"
268c8ff7afSPavel Labath #include "NativeThreadLinux.h"
27af245d11STodd Fiala 
28db264a6dSTamas Berghammer namespace lldb_private {
29af245d11STodd Fiala     class Error;
30af245d11STodd Fiala     class Module;
31af245d11STodd Fiala     class Scalar;
32af245d11STodd Fiala 
33db264a6dSTamas Berghammer namespace process_linux {
34af245d11STodd Fiala     /// @class NativeProcessLinux
35af245d11STodd Fiala     /// @brief Manages communication with the inferior (debugee) process.
36af245d11STodd Fiala     ///
37af245d11STodd Fiala     /// Upon construction, this class prepares and launches an inferior process for
38af245d11STodd Fiala     /// debugging.
39af245d11STodd Fiala     ///
40af245d11STodd Fiala     /// Changes in the inferior process state are broadcasted.
41af245d11STodd Fiala     class NativeProcessLinux: public NativeProcessProtocol
42af245d11STodd Fiala     {
43*d5b310f2SPavel Labath         friend Error
44*d5b310f2SPavel Labath         NativeProcessProtocol::Launch (ProcessLaunchInfo &launch_info,
45*d5b310f2SPavel Labath                 NativeDelegate &native_delegate,
46*d5b310f2SPavel Labath                 NativeProcessProtocolSP &process_sp);
47*d5b310f2SPavel Labath 
48*d5b310f2SPavel Labath         friend Error
49*d5b310f2SPavel Labath         NativeProcessProtocol::Attach (lldb::pid_t pid,
50*d5b310f2SPavel Labath             NativeProcessProtocol::NativeDelegate &native_delegate,
51*d5b310f2SPavel Labath             NativeProcessProtocolSP &native_process_sp);
52*d5b310f2SPavel Labath 
53c307c270SSean Callanan     public:
54068f8a7eSTamas Berghammer         //------------------------------------------------------------------------------
55068f8a7eSTamas Berghammer         /// @class Operation
56068f8a7eSTamas Berghammer         /// @brief Represents a NativeProcessLinux operation.
57068f8a7eSTamas Berghammer         ///
58068f8a7eSTamas Berghammer         /// Under Linux, it is not possible to ptrace() from any other thread but the
59068f8a7eSTamas Berghammer         /// one that spawned or attached to the process from the start.  Therefore, when
60068f8a7eSTamas Berghammer         /// a NativeProcessLinux is asked to deliver or change the state of an inferior
61068f8a7eSTamas Berghammer         /// process the operation must be "funneled" to a specific thread to perform the
62c7512fdcSPavel Labath         /// task.
63c7512fdcSPavel Labath         typedef std::function<Error()> Operation;
64068f8a7eSTamas Berghammer 
65af245d11STodd Fiala         // ---------------------------------------------------------------------
66af245d11STodd Fiala         // NativeProcessProtocol Interface
67af245d11STodd Fiala         // ---------------------------------------------------------------------
68af245d11STodd Fiala         Error
69af245d11STodd Fiala         Resume (const ResumeActionList &resume_actions) override;
70af245d11STodd Fiala 
71af245d11STodd Fiala         Error
72af245d11STodd Fiala         Halt () override;
73af245d11STodd Fiala 
74af245d11STodd Fiala         Error
75af245d11STodd Fiala         Detach () override;
76af245d11STodd Fiala 
77af245d11STodd Fiala         Error
78af245d11STodd Fiala         Signal (int signo) override;
79af245d11STodd Fiala 
80af245d11STodd Fiala         Error
81e9547b80SChaoren Lin         Interrupt () override;
82e9547b80SChaoren Lin 
83e9547b80SChaoren Lin         Error
84af245d11STodd Fiala         Kill () override;
85af245d11STodd Fiala 
86af245d11STodd Fiala         Error
87af245d11STodd Fiala         GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) override;
88af245d11STodd Fiala 
89af245d11STodd Fiala         Error
903eb4b458SChaoren Lin         ReadMemory(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override;
91af245d11STodd Fiala 
92af245d11STodd Fiala         Error
933eb4b458SChaoren Lin         ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override;
94af245d11STodd Fiala 
95af245d11STodd Fiala         Error
963eb4b458SChaoren Lin         WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) override;
973eb4b458SChaoren Lin 
983eb4b458SChaoren Lin         Error
993eb4b458SChaoren Lin         AllocateMemory(size_t size, uint32_t permissions, lldb::addr_t &addr) override;
100af245d11STodd Fiala 
101af245d11STodd Fiala         Error
102af245d11STodd Fiala         DeallocateMemory (lldb::addr_t addr) override;
103af245d11STodd Fiala 
104af245d11STodd Fiala         lldb::addr_t
105af245d11STodd Fiala         GetSharedLibraryInfoAddress () override;
106af245d11STodd Fiala 
107af245d11STodd Fiala         size_t
108af245d11STodd Fiala         UpdateThreads () override;
109af245d11STodd Fiala 
110af245d11STodd Fiala         bool
111af245d11STodd Fiala         GetArchitecture (ArchSpec &arch) const override;
112af245d11STodd Fiala 
113af245d11STodd Fiala         Error
114af245d11STodd Fiala         SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) override;
115af245d11STodd Fiala 
11645f5cb31SPavel Labath         Error
11745f5cb31SPavel Labath         SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) override;
11845f5cb31SPavel Labath 
11945f5cb31SPavel Labath         Error
12045f5cb31SPavel Labath         RemoveWatchpoint (lldb::addr_t addr) override;
12145f5cb31SPavel Labath 
122af245d11STodd Fiala         void
123af245d11STodd Fiala         DoStopIDBumped (uint32_t newBumpId) override;
124af245d11STodd Fiala 
1258bc34f4dSOleksiy Vyalov         void
1268bc34f4dSOleksiy Vyalov         Terminate () override;
1278bc34f4dSOleksiy Vyalov 
128068f8a7eSTamas Berghammer         Error
129068f8a7eSTamas Berghammer         GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) override;
130068f8a7eSTamas Berghammer 
131783bfc8cSTamas Berghammer         Error
132783bfc8cSTamas Berghammer         GetFileLoadAddress(const llvm::StringRef& file_name, lldb::addr_t& load_addr) override;
133783bfc8cSTamas Berghammer 
134af245d11STodd Fiala         // ---------------------------------------------------------------------
135af245d11STodd Fiala         // Interface used by NativeRegisterContext-derived classes.
136af245d11STodd Fiala         // ---------------------------------------------------------------------
13797ccc294SChaoren Lin         Error
138c7512fdcSPavel Labath         DoOperation(const Operation &op);
139068f8a7eSTamas Berghammer 
1404a9babb2SPavel Labath         static Error
1414a9babb2SPavel Labath         PtraceWrapper(int req,
1424a9babb2SPavel Labath                       lldb::pid_t pid,
1434a9babb2SPavel Labath                       void *addr = nullptr,
1444a9babb2SPavel Labath                       void *data = nullptr,
1454a9babb2SPavel Labath                       size_t data_size = 0,
1464a9babb2SPavel Labath                       long *result = nullptr);
1477cb18bf5STamas Berghammer 
148af245d11STodd Fiala     protected:
149af245d11STodd Fiala         // ---------------------------------------------------------------------
150af245d11STodd Fiala         // NativeProcessProtocol protected interface
151af245d11STodd Fiala         // ---------------------------------------------------------------------
152af245d11STodd Fiala         Error
153af245d11STodd Fiala         GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) override;
154af245d11STodd Fiala 
155af245d11STodd Fiala     private:
156af245d11STodd Fiala 
1571107b5a5SPavel Labath         class Monitor;
1581107b5a5SPavel Labath 
159db264a6dSTamas Berghammer         ArchSpec m_arch;
160af245d11STodd Fiala 
1611107b5a5SPavel Labath         std::unique_ptr<Monitor> m_monitor_up;
162af245d11STodd Fiala 
163db264a6dSTamas Berghammer         LazyBool m_supports_mem_region;
164af245d11STodd Fiala         std::vector<MemoryRegionInfo> m_mem_region_cache;
165db264a6dSTamas Berghammer         Mutex m_mem_region_cache_mutex;
166af245d11STodd Fiala 
167d8c338d4STamas Berghammer         // List of thread ids stepping with a breakpoint with the address of
168d8c338d4STamas Berghammer         // the relevan breakpoint
169d8c338d4STamas Berghammer         std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint;
170d8c338d4STamas Berghammer 
171af245d11STodd Fiala         /// @class LauchArgs
172af245d11STodd Fiala         ///
173af245d11STodd Fiala         /// @brief Simple structure to pass data to the thread responsible for
174af245d11STodd Fiala         /// launching a child process.
175bd7cbc5aSPavel Labath         struct LaunchArgs
176af245d11STodd Fiala         {
177bd7cbc5aSPavel Labath             LaunchArgs(Module *module,
178af245d11STodd Fiala                     char const **argv,
179af245d11STodd Fiala                     char const **envp,
180d3173f34SChaoren Lin                     const FileSpec &stdin_file_spec,
181d3173f34SChaoren Lin                     const FileSpec &stdout_file_spec,
182d3173f34SChaoren Lin                     const FileSpec &stderr_file_spec,
183d3173f34SChaoren Lin                     const FileSpec &working_dir,
184db264a6dSTamas Berghammer                     const ProcessLaunchInfo &launch_info);
185af245d11STodd Fiala 
186af245d11STodd Fiala             ~LaunchArgs();
187af245d11STodd Fiala 
188db264a6dSTamas Berghammer             Module *m_module;                  // The executable image to launch.
189af245d11STodd Fiala             char const **m_argv;               // Process arguments.
190af245d11STodd Fiala             char const **m_envp;               // Process environment.
191d3173f34SChaoren Lin             const FileSpec m_stdin_file_spec;  // Redirect stdin if not empty.
192d3173f34SChaoren Lin             const FileSpec m_stdout_file_spec; // Redirect stdout if not empty.
193d3173f34SChaoren Lin             const FileSpec m_stderr_file_spec; // Redirect stderr if not empty.
194d3173f34SChaoren Lin             const FileSpec m_working_dir;      // Working directory or empty.
195db264a6dSTamas Berghammer             const ProcessLaunchInfo &m_launch_info;
196af245d11STodd Fiala         };
197af245d11STodd Fiala 
198bd7cbc5aSPavel Labath         typedef std::function<::pid_t(Error &)> InitialOperation;
199af245d11STodd Fiala 
200af245d11STodd Fiala         // ---------------------------------------------------------------------
201af245d11STodd Fiala         // Private Instance Methods
202af245d11STodd Fiala         // ---------------------------------------------------------------------
203af245d11STodd Fiala         NativeProcessLinux ();
204af245d11STodd Fiala 
205af245d11STodd Fiala         /// Launches an inferior process ready for debugging.  Forms the
206af245d11STodd Fiala         /// implementation of Process::DoLaunch.
207af245d11STodd Fiala         void
208af245d11STodd Fiala         LaunchInferior (
209af245d11STodd Fiala             Module *module,
210af245d11STodd Fiala             char const *argv[],
211af245d11STodd Fiala             char const *envp[],
212d3173f34SChaoren Lin             const FileSpec &stdin_file_spec,
213d3173f34SChaoren Lin             const FileSpec &stdout_file_spec,
214d3173f34SChaoren Lin             const FileSpec &stderr_file_spec,
215d3173f34SChaoren Lin             const FileSpec &working_dir,
216db264a6dSTamas Berghammer             const ProcessLaunchInfo &launch_info,
217af245d11STodd Fiala             Error &error);
218af245d11STodd Fiala 
219af245d11STodd Fiala         /// Attaches to an existing process.  Forms the
2200cbf0b13STamas Berghammer         /// implementation of Process::DoAttach
221af245d11STodd Fiala         void
222af245d11STodd Fiala         AttachToInferior (lldb::pid_t pid, Error &error);
223af245d11STodd Fiala 
224af245d11STodd Fiala         void
225bd7cbc5aSPavel Labath         StartMonitorThread(const InitialOperation &operation, Error &error);
2261107b5a5SPavel Labath 
227bd7cbc5aSPavel Labath         ::pid_t
228bd7cbc5aSPavel Labath         Launch(LaunchArgs *args, Error &error);
229af245d11STodd Fiala 
230bd7cbc5aSPavel Labath         ::pid_t
231bd7cbc5aSPavel Labath         Attach(lldb::pid_t pid, Error &error);
232af245d11STodd Fiala 
23397ccc294SChaoren Lin         static Error
234af245d11STodd Fiala         SetDefaultPtraceOpts(const lldb::pid_t);
235af245d11STodd Fiala 
236af245d11STodd Fiala         static bool
237d3173f34SChaoren Lin         DupDescriptor(const FileSpec &file_spec, int fd, int flags);
238af245d11STodd Fiala 
2391107b5a5SPavel Labath         static void *
2401107b5a5SPavel Labath         MonitorThread(void *baton);
2411107b5a5SPavel Labath 
2421107b5a5SPavel Labath         void
2431107b5a5SPavel Labath         MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status);
244af245d11STodd Fiala 
245af245d11STodd Fiala         void
246426bdf88SPavel Labath         WaitForNewThread(::pid_t tid);
247426bdf88SPavel Labath 
248426bdf88SPavel Labath         void
249af245d11STodd Fiala         MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid);
250af245d11STodd Fiala 
251af245d11STodd Fiala         void
252c16f5dcaSChaoren Lin         MonitorTrace(lldb::pid_t pid, NativeThreadProtocolSP thread_sp);
253c16f5dcaSChaoren Lin 
254c16f5dcaSChaoren Lin         void
255c16f5dcaSChaoren Lin         MonitorBreakpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp);
256c16f5dcaSChaoren Lin 
257c16f5dcaSChaoren Lin         void
258c16f5dcaSChaoren Lin         MonitorWatchpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp, uint32_t wp_index);
259c16f5dcaSChaoren Lin 
260c16f5dcaSChaoren Lin         void
261af245d11STodd Fiala         MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited);
262af245d11STodd Fiala 
263e7708688STamas Berghammer         bool
264e7708688STamas Berghammer         SupportHardwareSingleStepping() const;
265e7708688STamas Berghammer 
266e7708688STamas Berghammer         Error
267e7708688STamas Berghammer         SetupSoftwareSingleStepping(NativeThreadProtocolSP thread_sp);
268e7708688STamas Berghammer 
269af245d11STodd Fiala #if 0
270af245d11STodd Fiala         static ::ProcessMessage::CrashReason
271af245d11STodd Fiala         GetCrashReasonForSIGSEGV(const siginfo_t *info);
272af245d11STodd Fiala 
273af245d11STodd Fiala         static ::ProcessMessage::CrashReason
274af245d11STodd Fiala         GetCrashReasonForSIGILL(const siginfo_t *info);
275af245d11STodd Fiala 
276af245d11STodd Fiala         static ::ProcessMessage::CrashReason
277af245d11STodd Fiala         GetCrashReasonForSIGFPE(const siginfo_t *info);
278af245d11STodd Fiala 
279af245d11STodd Fiala         static ::ProcessMessage::CrashReason
280af245d11STodd Fiala         GetCrashReasonForSIGBUS(const siginfo_t *info);
281af245d11STodd Fiala #endif
282af245d11STodd Fiala 
283af245d11STodd Fiala         bool
284af245d11STodd Fiala         HasThreadNoLock (lldb::tid_t thread_id);
285af245d11STodd Fiala 
286af245d11STodd Fiala         NativeThreadProtocolSP
287af245d11STodd Fiala         MaybeGetThreadNoLock (lldb::tid_t thread_id);
288af245d11STodd Fiala 
289af245d11STodd Fiala         bool
290af245d11STodd Fiala         StopTrackingThread (lldb::tid_t thread_id);
291af245d11STodd Fiala 
292af245d11STodd Fiala         NativeThreadProtocolSP
293af245d11STodd Fiala         AddThread (lldb::tid_t thread_id);
294af245d11STodd Fiala 
295af245d11STodd Fiala         Error
29663c8be95STamas Berghammer         GetSoftwareBreakpointPCOffset (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size);
297af245d11STodd Fiala 
298af245d11STodd Fiala         Error
299af245d11STodd Fiala         FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp);
300af245d11STodd Fiala 
301af245d11STodd Fiala         /// Writes a siginfo_t structure corresponding to the given thread ID to the
302af245d11STodd Fiala         /// memory region pointed to by @p siginfo.
30397ccc294SChaoren Lin         Error
30497ccc294SChaoren Lin         GetSignalInfo(lldb::tid_t tid, void *siginfo);
305af245d11STodd Fiala 
306af245d11STodd Fiala         /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
307af245d11STodd Fiala         /// corresponding to the given thread ID to the memory pointed to by @p
308af245d11STodd Fiala         /// message.
30997ccc294SChaoren Lin         Error
310af245d11STodd Fiala         GetEventMessage(lldb::tid_t tid, unsigned long *message);
311af245d11STodd Fiala 
312af245d11STodd Fiala         /// Resumes the given thread.  If @p signo is anything but
313af245d11STodd Fiala         /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
31497ccc294SChaoren Lin         Error
315af245d11STodd Fiala         Resume(lldb::tid_t tid, uint32_t signo);
316af245d11STodd Fiala 
317af245d11STodd Fiala         /// Single steps the given thread.  If @p signo is anything but
318af245d11STodd Fiala         /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
31997ccc294SChaoren Lin         Error
320af245d11STodd Fiala         SingleStep(lldb::tid_t tid, uint32_t signo);
321af245d11STodd Fiala 
322511e5cdcSTodd Fiala         void
323fa03ad2eSChaoren Lin         NotifyThreadDeath (lldb::tid_t tid);
324fa03ad2eSChaoren Lin 
325db264a6dSTamas Berghammer         Error
326af245d11STodd Fiala         Detach(lldb::tid_t tid);
32786fd8e45SChaoren Lin 
328c076559aSPavel Labath 
329c076559aSPavel Labath         // Typedefs.
330c076559aSPavel Labath         typedef std::unordered_set<lldb::tid_t> ThreadIDSet;
331c076559aSPavel Labath 
3321dbc6c9cSPavel Labath         // This method is requests a stop on all threads which are still running. It sets up a
3331dbc6c9cSPavel Labath         // deferred delegate notification, which will fire once threads report as stopped. The
3341dbc6c9cSPavel Labath         // triggerring_tid will be set as the current thread (main stop reason).
335c076559aSPavel Labath         void
336337f3eb9SPavel Labath         StopRunningThreads(lldb::tid_t triggering_tid);
337c076559aSPavel Labath 
338c076559aSPavel Labath         struct PendingNotification
339c076559aSPavel Labath         {
340337f3eb9SPavel Labath             PendingNotification (lldb::tid_t triggering_tid):
341337f3eb9SPavel Labath                 triggering_tid (triggering_tid),
342108c325dSPavel Labath                 wait_for_stop_tids ()
343337f3eb9SPavel Labath             {
344337f3eb9SPavel Labath             }
345337f3eb9SPavel Labath 
346c076559aSPavel Labath             const lldb::tid_t  triggering_tid;
347c076559aSPavel Labath             ThreadIDSet        wait_for_stop_tids;
348c076559aSPavel Labath         };
349c076559aSPavel Labath         typedef std::unique_ptr<PendingNotification> PendingNotificationUP;
350c076559aSPavel Labath 
3519eb1ecb9SPavel Labath         // Notify the delegate if all threads have stopped.
3529eb1ecb9SPavel Labath         void SignalIfAllThreadsStopped();
353c076559aSPavel Labath 
354c076559aSPavel Labath         void
355c076559aSPavel Labath         RequestStopOnAllRunningThreads();
356c076559aSPavel Labath 
3575eb721edSPavel Labath         Error
3585eb721edSPavel Labath         ThreadDidStop(lldb::tid_t tid, bool initiated_by_llgs);
359c076559aSPavel Labath 
3601dbc6c9cSPavel Labath         // Resume the thread with the given thread id using the request_thread_resume_function
3611dbc6c9cSPavel Labath         // called. If error_when_already_running is then then an error is raised if we think this
3621dbc6c9cSPavel Labath         // thread is already running.
3635eb721edSPavel Labath         Error
3641dbc6c9cSPavel Labath         ResumeThread(lldb::tid_t tid, NativeThreadLinux::ResumeThreadFunction request_thread_resume_function,
3655eb721edSPavel Labath                 bool error_when_already_running);
366c076559aSPavel Labath 
367c076559aSPavel Labath         void
368ed89c7feSPavel Labath         DoStopThreads(PendingNotificationUP &&notification_up);
369c076559aSPavel Labath 
370c076559aSPavel Labath         void
3718c8ff7afSPavel Labath         ThreadWasCreated (lldb::tid_t tid);
372c076559aSPavel Labath 
373c076559aSPavel Labath         // Member variables.
374c076559aSPavel Labath         PendingNotificationUP m_pending_notification_up;
375af245d11STodd Fiala     };
376db264a6dSTamas Berghammer 
377db264a6dSTamas Berghammer } // namespace process_linux
378db264a6dSTamas Berghammer } // namespace lldb_private
379af245d11STodd Fiala 
380af245d11STodd Fiala #endif // #ifndef liblldb_NativeProcessLinux_H_
381