Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1 | //===-- 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 Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 13 | // C++ Includes |
| 14 | #include <unordered_set> |
| 15 | |
| 16 | // Other libraries and framework includes |
| 17 | #include "lldb/Core/ArchSpec.h" |
| 18 | #include "lldb/lldb-types.h" |
| 19 | #include "lldb/Host/Debug.h" |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 20 | #include "lldb/Host/FileSpec.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 21 | #include "lldb/Host/HostThread.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 22 | #include "lldb/Target/MemoryRegionInfo.h" |
| 23 | |
Chaoren Lin | 2fe1d0a | 2015-02-03 01:51:38 +0000 | [diff] [blame] | 24 | #include "lldb/Host/common/NativeProcessProtocol.h" |
Pavel Labath | 8c8ff7a | 2015-05-11 10:03:10 +0000 | [diff] [blame] | 25 | #include "NativeThreadLinux.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 26 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 27 | namespace lldb_private { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 28 | class Error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 29 | class Scalar; |
| 30 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 31 | namespace process_linux { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 32 | /// @class NativeProcessLinux |
| 33 | /// @brief Manages communication with the inferior (debugee) process. |
| 34 | /// |
| 35 | /// Upon construction, this class prepares and launches an inferior process for |
| 36 | /// debugging. |
| 37 | /// |
| 38 | /// Changes in the inferior process state are broadcasted. |
| 39 | class NativeProcessLinux: public NativeProcessProtocol |
| 40 | { |
Pavel Labath | d5b310f | 2015-07-09 11:51:11 +0000 | [diff] [blame] | 41 | friend Error |
| 42 | NativeProcessProtocol::Launch (ProcessLaunchInfo &launch_info, |
| 43 | NativeDelegate &native_delegate, |
Pavel Labath | 19cbe96 | 2015-07-21 13:20:32 +0000 | [diff] [blame] | 44 | MainLoop &mainloop, |
Pavel Labath | d5b310f | 2015-07-09 11:51:11 +0000 | [diff] [blame] | 45 | NativeProcessProtocolSP &process_sp); |
| 46 | |
| 47 | friend Error |
| 48 | NativeProcessProtocol::Attach (lldb::pid_t pid, |
Pavel Labath | 19cbe96 | 2015-07-21 13:20:32 +0000 | [diff] [blame] | 49 | NativeProcessProtocol::NativeDelegate &native_delegate, |
| 50 | MainLoop &mainloop, |
| 51 | NativeProcessProtocolSP &process_sp); |
Pavel Labath | d5b310f | 2015-07-09 11:51:11 +0000 | [diff] [blame] | 52 | |
Sean Callanan | c307c27 | 2015-07-08 16:33:46 +0000 | [diff] [blame] | 53 | public: |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 54 | // --------------------------------------------------------------------- |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 55 | // NativeProcessProtocol Interface |
| 56 | // --------------------------------------------------------------------- |
| 57 | Error |
| 58 | Resume (const ResumeActionList &resume_actions) override; |
| 59 | |
| 60 | Error |
| 61 | Halt () override; |
| 62 | |
| 63 | Error |
| 64 | Detach () override; |
| 65 | |
| 66 | Error |
| 67 | Signal (int signo) override; |
| 68 | |
| 69 | Error |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 70 | Interrupt () override; |
| 71 | |
| 72 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 73 | Kill () override; |
| 74 | |
| 75 | Error |
| 76 | GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) override; |
| 77 | |
| 78 | Error |
Chaoren Lin | 3eb4b45 | 2015-04-29 17:24:48 +0000 | [diff] [blame] | 79 | ReadMemory(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 80 | |
| 81 | Error |
Chaoren Lin | 3eb4b45 | 2015-04-29 17:24:48 +0000 | [diff] [blame] | 82 | ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 83 | |
| 84 | Error |
Chaoren Lin | 3eb4b45 | 2015-04-29 17:24:48 +0000 | [diff] [blame] | 85 | WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) override; |
| 86 | |
| 87 | Error |
| 88 | AllocateMemory(size_t size, uint32_t permissions, lldb::addr_t &addr) override; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 89 | |
| 90 | Error |
| 91 | DeallocateMemory (lldb::addr_t addr) override; |
| 92 | |
| 93 | lldb::addr_t |
| 94 | GetSharedLibraryInfoAddress () override; |
| 95 | |
| 96 | size_t |
| 97 | UpdateThreads () override; |
| 98 | |
| 99 | bool |
| 100 | GetArchitecture (ArchSpec &arch) const override; |
| 101 | |
| 102 | Error |
| 103 | SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) override; |
| 104 | |
| 105 | void |
| 106 | DoStopIDBumped (uint32_t newBumpId) override; |
| 107 | |
Tamas Berghammer | 068f8a7 | 2015-05-26 11:58:52 +0000 | [diff] [blame] | 108 | Error |
| 109 | GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) override; |
| 110 | |
Tamas Berghammer | 783bfc8 | 2015-06-18 20:43:56 +0000 | [diff] [blame] | 111 | Error |
| 112 | GetFileLoadAddress(const llvm::StringRef& file_name, lldb::addr_t& load_addr) override; |
| 113 | |
Pavel Labath | f907778 | 2015-08-21 09:13:53 +0000 | [diff] [blame] | 114 | NativeThreadLinuxSP |
| 115 | GetThreadByID(lldb::tid_t id); |
| 116 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 117 | // --------------------------------------------------------------------- |
| 118 | // Interface used by NativeRegisterContext-derived classes. |
| 119 | // --------------------------------------------------------------------- |
Pavel Labath | 4a9babb | 2015-06-30 17:04:49 +0000 | [diff] [blame] | 120 | static Error |
| 121 | PtraceWrapper(int req, |
| 122 | lldb::pid_t pid, |
| 123 | void *addr = nullptr, |
| 124 | void *data = nullptr, |
| 125 | size_t data_size = 0, |
| 126 | long *result = nullptr); |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 127 | |
Pavel Labath | 605b51b | 2016-02-23 13:56:30 +0000 | [diff] [blame] | 128 | bool |
| 129 | SupportHardwareSingleStepping() const; |
| 130 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 131 | protected: |
| 132 | // --------------------------------------------------------------------- |
| 133 | // NativeProcessProtocol protected interface |
| 134 | // --------------------------------------------------------------------- |
| 135 | Error |
| 136 | GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) override; |
| 137 | |
| 138 | private: |
| 139 | |
Pavel Labath | 19cbe96 | 2015-07-21 13:20:32 +0000 | [diff] [blame] | 140 | MainLoop::SignalHandleUP m_sigchld_handle; |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 141 | ArchSpec m_arch; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 142 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 143 | LazyBool m_supports_mem_region; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 144 | std::vector<MemoryRegionInfo> m_mem_region_cache; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 145 | |
Pavel Labath | 0e1d729 | 2015-08-20 09:06:12 +0000 | [diff] [blame] | 146 | lldb::tid_t m_pending_notification_tid; |
| 147 | |
Tamas Berghammer | d8c338d | 2015-04-15 09:47:02 +0000 | [diff] [blame] | 148 | // List of thread ids stepping with a breakpoint with the address of |
| 149 | // the relevan breakpoint |
| 150 | std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint; |
| 151 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 152 | |
| 153 | // --------------------------------------------------------------------- |
| 154 | // Private Instance Methods |
| 155 | // --------------------------------------------------------------------- |
| 156 | NativeProcessLinux (); |
| 157 | |
Pavel Labath | 4abe5d6 | 2016-07-15 10:18:15 +0000 | [diff] [blame^] | 158 | Error |
| 159 | LaunchInferior(MainLoop &mainloop, ProcessLaunchInfo &launch_info); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 160 | |
| 161 | /// Attaches to an existing process. Forms the |
Tamas Berghammer | 0cbf0b1 | 2015-03-13 11:16:03 +0000 | [diff] [blame] | 162 | /// implementation of Process::DoAttach |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 163 | void |
Pavel Labath | 19cbe96 | 2015-07-21 13:20:32 +0000 | [diff] [blame] | 164 | AttachToInferior (MainLoop &mainloop, lldb::pid_t pid, Error &error); |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 165 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 166 | ::pid_t |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 167 | Attach(lldb::pid_t pid, Error &error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 168 | |
Pavel Labath | 0c4f01d | 2016-07-06 13:18:50 +0000 | [diff] [blame] | 169 | static void |
Pavel Labath | 4abe5d6 | 2016-07-15 10:18:15 +0000 | [diff] [blame^] | 170 | ChildFunc(const ProcessLaunchInfo &launch_info) LLVM_ATTRIBUTE_NORETURN; |
Pavel Labath | 0c4f01d | 2016-07-06 13:18:50 +0000 | [diff] [blame] | 171 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 172 | static Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 173 | SetDefaultPtraceOpts(const lldb::pid_t); |
| 174 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 175 | static bool |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 176 | DupDescriptor(const FileSpec &file_spec, int fd, int flags); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 177 | |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 178 | static void * |
| 179 | MonitorThread(void *baton); |
| 180 | |
| 181 | void |
| 182 | MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 183 | |
| 184 | void |
Pavel Labath | 426bdf8 | 2015-04-28 07:51:52 +0000 | [diff] [blame] | 185 | WaitForNewThread(::pid_t tid); |
| 186 | |
| 187 | void |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 188 | MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 189 | |
| 190 | void |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 191 | MonitorTrace(NativeThreadLinux &thread); |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 192 | |
| 193 | void |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 194 | MonitorBreakpoint(NativeThreadLinux &thread); |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 195 | |
| 196 | void |
Pavel Labath | f907778 | 2015-08-21 09:13:53 +0000 | [diff] [blame] | 197 | MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index); |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 198 | |
| 199 | void |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 200 | MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread, bool exited); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 201 | |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 202 | Error |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 203 | SetupSoftwareSingleStepping(NativeThreadLinux &thread); |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 204 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 205 | #if 0 |
| 206 | static ::ProcessMessage::CrashReason |
| 207 | GetCrashReasonForSIGSEGV(const siginfo_t *info); |
| 208 | |
| 209 | static ::ProcessMessage::CrashReason |
| 210 | GetCrashReasonForSIGILL(const siginfo_t *info); |
| 211 | |
| 212 | static ::ProcessMessage::CrashReason |
| 213 | GetCrashReasonForSIGFPE(const siginfo_t *info); |
| 214 | |
| 215 | static ::ProcessMessage::CrashReason |
| 216 | GetCrashReasonForSIGBUS(const siginfo_t *info); |
| 217 | #endif |
| 218 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 219 | bool |
| 220 | HasThreadNoLock (lldb::tid_t thread_id); |
| 221 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 222 | bool |
| 223 | StopTrackingThread (lldb::tid_t thread_id); |
| 224 | |
Pavel Labath | f907778 | 2015-08-21 09:13:53 +0000 | [diff] [blame] | 225 | NativeThreadLinuxSP |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 226 | AddThread (lldb::tid_t thread_id); |
| 227 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 228 | Error |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 229 | GetSoftwareBreakpointPCOffset(uint32_t &actual_opcode_size); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 230 | |
| 231 | Error |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 232 | FixupBreakpointPCAsNeeded(NativeThreadLinux &thread); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 233 | |
| 234 | /// Writes a siginfo_t structure corresponding to the given thread ID to the |
| 235 | /// memory region pointed to by @p siginfo. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 236 | Error |
| 237 | GetSignalInfo(lldb::tid_t tid, void *siginfo); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 238 | |
| 239 | /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) |
| 240 | /// corresponding to the given thread ID to the memory pointed to by @p |
| 241 | /// message. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 242 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 243 | GetEventMessage(lldb::tid_t tid, unsigned long *message); |
| 244 | |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 245 | void |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 246 | NotifyThreadDeath (lldb::tid_t tid); |
| 247 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 248 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 249 | Detach(lldb::tid_t tid); |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 250 | |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 251 | |
Pavel Labath | 1dbc6c9 | 2015-05-12 08:35:33 +0000 | [diff] [blame] | 252 | // This method is requests a stop on all threads which are still running. It sets up a |
| 253 | // deferred delegate notification, which will fire once threads report as stopped. The |
| 254 | // triggerring_tid will be set as the current thread (main stop reason). |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 255 | void |
Pavel Labath | 337f3eb | 2015-05-08 08:57:45 +0000 | [diff] [blame] | 256 | StopRunningThreads(lldb::tid_t triggering_tid); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 257 | |
Pavel Labath | 9eb1ecb | 2015-05-15 13:49:01 +0000 | [diff] [blame] | 258 | // Notify the delegate if all threads have stopped. |
| 259 | void SignalIfAllThreadsStopped(); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 260 | |
Pavel Labath | 0e1d729 | 2015-08-20 09:06:12 +0000 | [diff] [blame] | 261 | // Resume the given thread, optionally passing it the given signal. The type of resume |
| 262 | // operation (continue, single-step) depends on the state parameter. |
Pavel Labath | 5eb721e | 2015-05-07 08:30:31 +0000 | [diff] [blame] | 263 | Error |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 264 | ResumeThread(NativeThreadLinux &thread, lldb::StateType state, int signo); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 265 | |
| 266 | void |
Pavel Labath | f907778 | 2015-08-21 09:13:53 +0000 | [diff] [blame] | 267 | ThreadWasCreated(NativeThreadLinux &thread); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 268 | |
Pavel Labath | 19cbe96 | 2015-07-21 13:20:32 +0000 | [diff] [blame] | 269 | void |
| 270 | SigchldHandler(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 271 | }; |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 272 | |
| 273 | } // namespace process_linux |
| 274 | } // namespace lldb_private |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 275 | |
| 276 | #endif // #ifndef liblldb_NativeProcessLinux_H_ |