blob: 98fc88baab6e3cb9ae3f05144795085488840fac [file] [log] [blame]
Todd Fialaaf245d12014-06-30 21:05:18 +00001//===-- NativeProcessLinux.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_NativeProcessLinux_H_
11#define liblldb_NativeProcessLinux_H_
12
Todd Fialaaf245d12014-06-30 21:05:18 +000013// C++ Includes
14#include <unordered_set>
15
16// Other libraries and framework includes
17#include "lldb/Core/ArchSpec.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000018#include "lldb/Host/Debug.h"
Zachary Turner39de3112014-09-09 20:54:56 +000019#include "lldb/Host/HostThread.h"
Pavel Labathb7f0f452017-03-17 11:08:40 +000020#include "lldb/Host/linux/Support.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000021#include "lldb/Target/MemoryRegionInfo.h"
Zachary Turner5713a052017-03-22 18:40:07 +000022#include "lldb/Utility/FileSpec.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000023#include "lldb/lldb-types.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000024
Pavel Labath8c8ff7a2015-05-11 10:03:10 +000025#include "NativeThreadLinux.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000026#include "lldb/Host/common/NativeProcessProtocol.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000027
Tamas Berghammerdb264a62015-03-31 09:52:22 +000028namespace lldb_private {
Zachary Turner97206d52017-05-12 04:51:55 +000029class Status;
Kate Stoneb9c1b512016-09-06 20:57:50 +000030class Scalar;
Todd Fialaaf245d12014-06-30 21:05:18 +000031
Tamas Berghammerdb264a62015-03-31 09:52:22 +000032namespace process_linux {
Kate Stoneb9c1b512016-09-06 20:57:50 +000033/// @class NativeProcessLinux
34/// @brief Manages communication with the inferior (debugee) process.
35///
36/// Upon construction, this class prepares and launches an inferior process for
37/// debugging.
38///
39/// Changes in the inferior process state are broadcasted.
40class NativeProcessLinux : public NativeProcessProtocol {
Zachary Turner97206d52017-05-12 04:51:55 +000041 friend Status NativeProcessProtocol::Launch(
Kate Stoneb9c1b512016-09-06 20:57:50 +000042 ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate,
43 MainLoop &mainloop, NativeProcessProtocolSP &process_sp);
Pavel Labathd5b310f2015-07-09 11:51:11 +000044
Zachary Turner97206d52017-05-12 04:51:55 +000045 friend Status NativeProcessProtocol::Attach(
Kate Stoneb9c1b512016-09-06 20:57:50 +000046 lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate,
47 MainLoop &mainloop, NativeProcessProtocolSP &process_sp);
Pavel Labathd5b310f2015-07-09 11:51:11 +000048
Kate Stoneb9c1b512016-09-06 20:57:50 +000049public:
50 // ---------------------------------------------------------------------
51 // NativeProcessProtocol Interface
52 // ---------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +000053 Status Resume(const ResumeActionList &resume_actions) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000054
Zachary Turner97206d52017-05-12 04:51:55 +000055 Status Halt() override;
Todd Fialaaf245d12014-06-30 21:05:18 +000056
Zachary Turner97206d52017-05-12 04:51:55 +000057 Status Detach() override;
Todd Fialaaf245d12014-06-30 21:05:18 +000058
Zachary Turner97206d52017-05-12 04:51:55 +000059 Status Signal(int signo) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000060
Zachary Turner97206d52017-05-12 04:51:55 +000061 Status Interrupt() override;
Chaoren Line9547b82015-02-03 01:51:00 +000062
Zachary Turner97206d52017-05-12 04:51:55 +000063 Status Kill() override;
Todd Fialaaf245d12014-06-30 21:05:18 +000064
Zachary Turner97206d52017-05-12 04:51:55 +000065 Status GetMemoryRegionInfo(lldb::addr_t load_addr,
66 MemoryRegionInfo &range_info) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000067
Zachary Turner97206d52017-05-12 04:51:55 +000068 Status ReadMemory(lldb::addr_t addr, void *buf, size_t size,
69 size_t &bytes_read) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000070
Zachary Turner97206d52017-05-12 04:51:55 +000071 Status ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size,
72 size_t &bytes_read) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000073
Zachary Turner97206d52017-05-12 04:51:55 +000074 Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size,
75 size_t &bytes_written) override;
Chaoren Lin3eb4b452015-04-29 17:24:48 +000076
Zachary Turner97206d52017-05-12 04:51:55 +000077 Status AllocateMemory(size_t size, uint32_t permissions,
78 lldb::addr_t &addr) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000079
Zachary Turner97206d52017-05-12 04:51:55 +000080 Status DeallocateMemory(lldb::addr_t addr) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000081
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 lldb::addr_t GetSharedLibraryInfoAddress() override;
Todd Fialaaf245d12014-06-30 21:05:18 +000083
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 size_t UpdateThreads() override;
Todd Fialaaf245d12014-06-30 21:05:18 +000085
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 bool GetArchitecture(ArchSpec &arch) const override;
Todd Fialaaf245d12014-06-30 21:05:18 +000087
Zachary Turner97206d52017-05-12 04:51:55 +000088 Status SetBreakpoint(lldb::addr_t addr, uint32_t size,
89 bool hardware) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000090
Zachary Turner97206d52017-05-12 04:51:55 +000091 Status RemoveBreakpoint(lldb::addr_t addr, bool hardware = false) override;
Omair Javaidd5ffbad2017-02-24 13:27:31 +000092
Kate Stoneb9c1b512016-09-06 20:57:50 +000093 void DoStopIDBumped(uint32_t newBumpId) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000094
Zachary Turner97206d52017-05-12 04:51:55 +000095 Status GetLoadedModuleFileSpec(const char *module_path,
96 FileSpec &file_spec) override;
Tamas Berghammer068f8a72015-05-26 11:58:52 +000097
Zachary Turner97206d52017-05-12 04:51:55 +000098 Status GetFileLoadAddress(const llvm::StringRef &file_name,
99 lldb::addr_t &load_addr) override;
Tamas Berghammer783bfc82015-06-18 20:43:56 +0000100
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101 NativeThreadLinuxSP GetThreadByID(lldb::tid_t id);
Pavel Labathf9077782015-08-21 09:13:53 +0000102
Pavel Labathb7f0f452017-03-17 11:08:40 +0000103 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
104 GetAuxvData() const override {
105 return getProcFile(GetID(), "auxv");
106 }
107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 // ---------------------------------------------------------------------
109 // Interface used by NativeRegisterContext-derived classes.
110 // ---------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000111 static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr = nullptr,
112 void *data = nullptr, size_t data_size = 0,
113 long *result = nullptr);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +0000114
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 bool SupportHardwareSingleStepping() const;
Pavel Labath605b51b2016-02-23 13:56:30 +0000116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117protected:
118 // ---------------------------------------------------------------------
119 // NativeProcessProtocol protected interface
120 // ---------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000121 Status
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122 GetSoftwareBreakpointTrapOpcode(size_t trap_opcode_size_hint,
123 size_t &actual_opcode_size,
124 const uint8_t *&trap_opcode_bytes) override;
Todd Fialaaf245d12014-06-30 21:05:18 +0000125
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126private:
127 MainLoop::SignalHandleUP m_sigchld_handle;
128 ArchSpec m_arch;
Todd Fialaaf245d12014-06-30 21:05:18 +0000129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 LazyBool m_supports_mem_region;
Tamas Berghammera6f57952017-01-03 16:29:43 +0000131 std::vector<std::pair<MemoryRegionInfo, FileSpec>> m_mem_region_cache;
Todd Fialaaf245d12014-06-30 21:05:18 +0000132
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 lldb::tid_t m_pending_notification_tid;
Todd Fialaaf245d12014-06-30 21:05:18 +0000134
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135 // List of thread ids stepping with a breakpoint with the address of
136 // the relevan breakpoint
137 std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint;
Pavel Labath0e1d7292015-08-20 09:06:12 +0000138
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 // ---------------------------------------------------------------------
140 // Private Instance Methods
141 // ---------------------------------------------------------------------
142 NativeProcessLinux();
Tamas Berghammerd8c338d2015-04-15 09:47:02 +0000143
Zachary Turner97206d52017-05-12 04:51:55 +0000144 Status LaunchInferior(MainLoop &mainloop, ProcessLaunchInfo &launch_info);
Todd Fialaaf245d12014-06-30 21:05:18 +0000145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146 /// Attaches to an existing process. Forms the
147 /// implementation of Process::DoAttach
Zachary Turner97206d52017-05-12 04:51:55 +0000148 void AttachToInferior(MainLoop &mainloop, lldb::pid_t pid, Status &error);
Todd Fialaaf245d12014-06-30 21:05:18 +0000149
Zachary Turner97206d52017-05-12 04:51:55 +0000150 ::pid_t Attach(lldb::pid_t pid, Status &error);
Todd Fialaaf245d12014-06-30 21:05:18 +0000151
Zachary Turner97206d52017-05-12 04:51:55 +0000152 static Status SetDefaultPtraceOpts(const lldb::pid_t);
Pavel Labath1107b5a2015-04-17 14:07:49 +0000153
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154 static void *MonitorThread(void *baton);
Todd Fialaaf245d12014-06-30 21:05:18 +0000155
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156 void MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status);
Todd Fialaaf245d12014-06-30 21:05:18 +0000157
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158 void WaitForNewThread(::pid_t tid);
Pavel Labath1107b5a2015-04-17 14:07:49 +0000159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 void MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread);
Todd Fialaaf245d12014-06-30 21:05:18 +0000161
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 void MonitorTrace(NativeThreadLinux &thread);
Pavel Labath426bdf82015-04-28 07:51:52 +0000163
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164 void MonitorBreakpoint(NativeThreadLinux &thread);
Todd Fialaaf245d12014-06-30 21:05:18 +0000165
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 void MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index);
Chaoren Linc16f5dc2015-03-19 23:28:10 +0000167
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 void MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread,
169 bool exited);
Chaoren Linc16f5dc2015-03-19 23:28:10 +0000170
Zachary Turner97206d52017-05-12 04:51:55 +0000171 Status SetupSoftwareSingleStepping(NativeThreadLinux &thread);
Tamas Berghammere7708682015-04-22 10:00:23 +0000172
Todd Fialaaf245d12014-06-30 21:05:18 +0000173#if 0
174 static ::ProcessMessage::CrashReason
175 GetCrashReasonForSIGSEGV(const siginfo_t *info);
176
177 static ::ProcessMessage::CrashReason
178 GetCrashReasonForSIGILL(const siginfo_t *info);
179
180 static ::ProcessMessage::CrashReason
181 GetCrashReasonForSIGFPE(const siginfo_t *info);
182
183 static ::ProcessMessage::CrashReason
184 GetCrashReasonForSIGBUS(const siginfo_t *info);
185#endif
186
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187 bool HasThreadNoLock(lldb::tid_t thread_id);
Todd Fialaaf245d12014-06-30 21:05:18 +0000188
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189 bool StopTrackingThread(lldb::tid_t thread_id);
Todd Fialaaf245d12014-06-30 21:05:18 +0000190
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191 NativeThreadLinuxSP AddThread(lldb::tid_t thread_id);
Todd Fialaaf245d12014-06-30 21:05:18 +0000192
Zachary Turner97206d52017-05-12 04:51:55 +0000193 Status GetSoftwareBreakpointPCOffset(uint32_t &actual_opcode_size);
Todd Fialaaf245d12014-06-30 21:05:18 +0000194
Zachary Turner97206d52017-05-12 04:51:55 +0000195 Status FixupBreakpointPCAsNeeded(NativeThreadLinux &thread);
Todd Fialaaf245d12014-06-30 21:05:18 +0000196
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197 /// Writes a siginfo_t structure corresponding to the given thread ID to the
198 /// memory region pointed to by @p siginfo.
Zachary Turner97206d52017-05-12 04:51:55 +0000199 Status GetSignalInfo(lldb::tid_t tid, void *siginfo);
Todd Fialaaf245d12014-06-30 21:05:18 +0000200
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
202 /// corresponding to the given thread ID to the memory pointed to by @p
203 /// message.
Zachary Turner97206d52017-05-12 04:51:55 +0000204 Status GetEventMessage(lldb::tid_t tid, unsigned long *message);
Todd Fialaaf245d12014-06-30 21:05:18 +0000205
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206 void NotifyThreadDeath(lldb::tid_t tid);
Chaoren Linfa03ad22015-02-03 01:50:42 +0000207
Zachary Turner97206d52017-05-12 04:51:55 +0000208 Status Detach(lldb::tid_t tid);
Chaoren Lin86fd8e42015-02-03 01:51:15 +0000209
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210 // This method is requests a stop on all threads which are still running. It
211 // sets up a
212 // deferred delegate notification, which will fire once threads report as
213 // stopped. The
214 // triggerring_tid will be set as the current thread (main stop reason).
215 void StopRunningThreads(lldb::tid_t triggering_tid);
Pavel Labathc0765592015-05-06 10:46:34 +0000216
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217 // Notify the delegate if all threads have stopped.
218 void SignalIfAllThreadsStopped();
Pavel Labathc0765592015-05-06 10:46:34 +0000219
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220 // Resume the given thread, optionally passing it the given signal. The type
221 // of resume
222 // operation (continue, single-step) depends on the state parameter.
Zachary Turner97206d52017-05-12 04:51:55 +0000223 Status ResumeThread(NativeThreadLinux &thread, lldb::StateType state,
224 int signo);
Pavel Labathc0765592015-05-06 10:46:34 +0000225
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 void ThreadWasCreated(NativeThreadLinux &thread);
Pavel Labathc0765592015-05-06 10:46:34 +0000227
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228 void SigchldHandler();
Tamas Berghammera6f57952017-01-03 16:29:43 +0000229
Zachary Turner97206d52017-05-12 04:51:55 +0000230 Status PopulateMemoryRegionCache();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231};
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000232
233} // namespace process_linux
234} // namespace lldb_private
Todd Fialaaf245d12014-06-30 21:05:18 +0000235
236#endif // #ifndef liblldb_NativeProcessLinux_H_