blob: 886d077aac7ef6ad55c599f4d03a5412f039461e [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
13// C Includes
14#include <semaphore.h>
15#include <signal.h>
16
17// C++ Includes
Pavel Labathc0765592015-05-06 10:46:34 +000018#include <mutex>
19#include <unordered_map>
Todd Fialaaf245d12014-06-30 21:05:18 +000020#include <unordered_set>
21
22// Other libraries and framework includes
23#include "lldb/Core/ArchSpec.h"
24#include "lldb/lldb-types.h"
25#include "lldb/Host/Debug.h"
Zachary Turner39de3112014-09-09 20:54:56 +000026#include "lldb/Host/HostThread.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000027#include "lldb/Host/Mutex.h"
28#include "lldb/Target/MemoryRegionInfo.h"
29
Chaoren Lin2fe1d0a2015-02-03 01:51:38 +000030#include "lldb/Host/common/NativeProcessProtocol.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000031
Tamas Berghammerdb264a62015-03-31 09:52:22 +000032namespace lldb_private {
Todd Fialaaf245d12014-06-30 21:05:18 +000033 class Error;
34 class Module;
35 class Scalar;
36
Tamas Berghammerdb264a62015-03-31 09:52:22 +000037namespace process_linux {
Todd Fialaaf245d12014-06-30 21:05:18 +000038 /// @class NativeProcessLinux
39 /// @brief Manages communication with the inferior (debugee) process.
40 ///
41 /// Upon construction, this class prepares and launches an inferior process for
42 /// debugging.
43 ///
44 /// Changes in the inferior process state are broadcasted.
45 class NativeProcessLinux: public NativeProcessProtocol
46 {
47 public:
48
Tamas Berghammerdb264a62015-03-31 09:52:22 +000049 static Error
Todd Fialaaf245d12014-06-30 21:05:18 +000050 LaunchProcess (
51 Module *exe_module,
52 ProcessLaunchInfo &launch_info,
Tamas Berghammerdb264a62015-03-31 09:52:22 +000053 NativeProcessProtocol::NativeDelegate &native_delegate,
Todd Fialaaf245d12014-06-30 21:05:18 +000054 NativeProcessProtocolSP &native_process_sp);
55
Tamas Berghammerdb264a62015-03-31 09:52:22 +000056 static Error
Todd Fialaaf245d12014-06-30 21:05:18 +000057 AttachToProcess (
58 lldb::pid_t pid,
Tamas Berghammerdb264a62015-03-31 09:52:22 +000059 NativeProcessProtocol::NativeDelegate &native_delegate,
Todd Fialaaf245d12014-06-30 21:05:18 +000060 NativeProcessProtocolSP &native_process_sp);
61
62 // ---------------------------------------------------------------------
Todd Fialaaf245d12014-06-30 21:05:18 +000063 // NativeProcessProtocol Interface
64 // ---------------------------------------------------------------------
65 Error
66 Resume (const ResumeActionList &resume_actions) override;
67
68 Error
69 Halt () override;
70
71 Error
72 Detach () override;
73
74 Error
75 Signal (int signo) override;
76
77 Error
Chaoren Line9547b82015-02-03 01:51:00 +000078 Interrupt () override;
79
80 Error
Todd Fialaaf245d12014-06-30 21:05:18 +000081 Kill () override;
82
83 Error
84 GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) override;
85
86 Error
Chaoren Lin3eb4b452015-04-29 17:24:48 +000087 ReadMemory(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000088
89 Error
Chaoren Lin3eb4b452015-04-29 17:24:48 +000090 ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000091
92 Error
Chaoren Lin3eb4b452015-04-29 17:24:48 +000093 WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) override;
94
95 Error
96 AllocateMemory(size_t size, uint32_t permissions, lldb::addr_t &addr) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000097
98 Error
99 DeallocateMemory (lldb::addr_t addr) override;
100
101 lldb::addr_t
102 GetSharedLibraryInfoAddress () override;
103
104 size_t
105 UpdateThreads () override;
106
107 bool
108 GetArchitecture (ArchSpec &arch) const override;
109
110 Error
111 SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) override;
112
Pavel Labath45f5cb32015-05-05 15:05:50 +0000113 Error
114 SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) override;
115
116 Error
117 RemoveWatchpoint (lldb::addr_t addr) override;
118
Todd Fialaaf245d12014-06-30 21:05:18 +0000119 void
120 DoStopIDBumped (uint32_t newBumpId) override;
121
Oleksiy Vyalov8bc34f42015-02-19 17:58:04 +0000122 void
123 Terminate () override;
124
Todd Fialaaf245d12014-06-30 21:05:18 +0000125 // ---------------------------------------------------------------------
126 // Interface used by NativeRegisterContext-derived classes.
127 // ---------------------------------------------------------------------
128
129 /// Reads the contents from the register identified by the given (architecture
130 /// dependent) offset.
131 ///
132 /// This method is provided for use by RegisterContextLinux derivatives.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000133 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000134 ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000135 unsigned size, RegisterValue &value);
Todd Fialaaf245d12014-06-30 21:05:18 +0000136
137 /// Writes the given value to the register identified by the given
138 /// (architecture dependent) offset.
139 ///
140 /// This method is provided for use by RegisterContextLinux derivatives.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000141 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000142 WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000143 const RegisterValue &value);
Todd Fialaaf245d12014-06-30 21:05:18 +0000144
145 /// Reads all general purpose registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000146 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000147 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
148
149 /// Reads generic floating point registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000150 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000151 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
152
153 /// Reads the specified register set into the specified buffer.
154 /// For instance, the extended floating-point register set.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000155 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000156 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
157
158 /// Writes all general purpose registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000159 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000160 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
161
162 /// Writes generic floating point registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000163 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000164 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
165
166 /// Writes the specified register set into the specified buffer.
167 /// For instance, the extended floating-point register set.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000168 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000169 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +0000170
171 Error
172 GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) override;
173
Todd Fialaaf245d12014-06-30 21:05:18 +0000174 protected:
175 // ---------------------------------------------------------------------
176 // NativeProcessProtocol protected interface
177 // ---------------------------------------------------------------------
178 Error
179 GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) override;
180
181 private:
182
Pavel Labath1107b5a2015-04-17 14:07:49 +0000183 class Monitor;
184
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000185 ArchSpec m_arch;
Todd Fialaaf245d12014-06-30 21:05:18 +0000186
Pavel Labath1107b5a2015-04-17 14:07:49 +0000187 std::unique_ptr<Monitor> m_monitor_up;
Todd Fialaaf245d12014-06-30 21:05:18 +0000188
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000189 LazyBool m_supports_mem_region;
Todd Fialaaf245d12014-06-30 21:05:18 +0000190 std::vector<MemoryRegionInfo> m_mem_region_cache;
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000191 Mutex m_mem_region_cache_mutex;
Todd Fialaaf245d12014-06-30 21:05:18 +0000192
Tamas Berghammerd8c338d2015-04-15 09:47:02 +0000193 // List of thread ids stepping with a breakpoint with the address of
194 // the relevan breakpoint
195 std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint;
196
Todd Fialaaf245d12014-06-30 21:05:18 +0000197 /// @class LauchArgs
198 ///
199 /// @brief Simple structure to pass data to the thread responsible for
200 /// launching a child process.
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000201 struct LaunchArgs
Todd Fialaaf245d12014-06-30 21:05:18 +0000202 {
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000203 LaunchArgs(Module *module,
Todd Fialaaf245d12014-06-30 21:05:18 +0000204 char const **argv,
205 char const **envp,
Todd Fiala75f47c32014-10-11 21:42:09 +0000206 const std::string &stdin_path,
207 const std::string &stdout_path,
208 const std::string &stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000209 const char *working_dir,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000210 const ProcessLaunchInfo &launch_info);
Todd Fialaaf245d12014-06-30 21:05:18 +0000211
212 ~LaunchArgs();
213
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000214 Module *m_module; // The executable image to launch.
215 char const **m_argv; // Process arguments.
216 char const **m_envp; // Process environment.
Todd Fiala75f47c32014-10-11 21:42:09 +0000217 const std::string &m_stdin_path; // Redirect stdin if not empty.
218 const std::string &m_stdout_path; // Redirect stdout if not empty.
219 const std::string &m_stderr_path; // Redirect stderr if not empty.
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000220 const char *m_working_dir; // Working directory or NULL.
221 const ProcessLaunchInfo &m_launch_info;
Todd Fialaaf245d12014-06-30 21:05:18 +0000222 };
223
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000224 typedef std::function<::pid_t(Error &)> InitialOperation;
Todd Fialaaf245d12014-06-30 21:05:18 +0000225
226 // ---------------------------------------------------------------------
227 // Private Instance Methods
228 // ---------------------------------------------------------------------
229 NativeProcessLinux ();
230
231 /// Launches an inferior process ready for debugging. Forms the
232 /// implementation of Process::DoLaunch.
233 void
234 LaunchInferior (
235 Module *module,
236 char const *argv[],
237 char const *envp[],
Todd Fiala75f47c32014-10-11 21:42:09 +0000238 const std::string &stdin_path,
239 const std::string &stdout_path,
240 const std::string &stderr_path,
Todd Fialaaf245d12014-06-30 21:05:18 +0000241 const char *working_dir,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000242 const ProcessLaunchInfo &launch_info,
Todd Fialaaf245d12014-06-30 21:05:18 +0000243 Error &error);
244
245 /// Attaches to an existing process. Forms the
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +0000246 /// implementation of Process::DoAttach
Todd Fialaaf245d12014-06-30 21:05:18 +0000247 void
248 AttachToInferior (lldb::pid_t pid, Error &error);
249
250 void
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000251 StartMonitorThread(const InitialOperation &operation, Error &error);
Pavel Labath1107b5a2015-04-17 14:07:49 +0000252
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000253 ::pid_t
254 Launch(LaunchArgs *args, Error &error);
Todd Fialaaf245d12014-06-30 21:05:18 +0000255
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000256 ::pid_t
257 Attach(lldb::pid_t pid, Error &error);
Todd Fialaaf245d12014-06-30 21:05:18 +0000258
Chaoren Lin97ccc292015-02-03 01:51:12 +0000259 static Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000260 SetDefaultPtraceOpts(const lldb::pid_t);
261
Todd Fialaaf245d12014-06-30 21:05:18 +0000262 static bool
263 DupDescriptor(const char *path, int fd, int flags);
264
Pavel Labath1107b5a2015-04-17 14:07:49 +0000265 static void *
266 MonitorThread(void *baton);
267
268 void
269 MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status);
Todd Fialaaf245d12014-06-30 21:05:18 +0000270
271 void
Pavel Labath426bdf82015-04-28 07:51:52 +0000272 WaitForNewThread(::pid_t tid);
273
274 void
Todd Fialaaf245d12014-06-30 21:05:18 +0000275 MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid);
276
277 void
Chaoren Linc16f5dc2015-03-19 23:28:10 +0000278 MonitorTrace(lldb::pid_t pid, NativeThreadProtocolSP thread_sp);
279
280 void
281 MonitorBreakpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp);
282
283 void
284 MonitorWatchpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp, uint32_t wp_index);
285
286 void
Todd Fialaaf245d12014-06-30 21:05:18 +0000287 MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited);
288
Tamas Berghammere7708682015-04-22 10:00:23 +0000289 bool
290 SupportHardwareSingleStepping() const;
291
292 Error
293 SetupSoftwareSingleStepping(NativeThreadProtocolSP thread_sp);
294
Todd Fialaaf245d12014-06-30 21:05:18 +0000295#if 0
296 static ::ProcessMessage::CrashReason
297 GetCrashReasonForSIGSEGV(const siginfo_t *info);
298
299 static ::ProcessMessage::CrashReason
300 GetCrashReasonForSIGILL(const siginfo_t *info);
301
302 static ::ProcessMessage::CrashReason
303 GetCrashReasonForSIGFPE(const siginfo_t *info);
304
305 static ::ProcessMessage::CrashReason
306 GetCrashReasonForSIGBUS(const siginfo_t *info);
307#endif
308
Todd Fialaaf245d12014-06-30 21:05:18 +0000309 bool
310 HasThreadNoLock (lldb::tid_t thread_id);
311
312 NativeThreadProtocolSP
313 MaybeGetThreadNoLock (lldb::tid_t thread_id);
314
315 bool
316 StopTrackingThread (lldb::tid_t thread_id);
317
318 NativeThreadProtocolSP
319 AddThread (lldb::tid_t thread_id);
320
Todd Fialaaf245d12014-06-30 21:05:18 +0000321 Error
Tamas Berghammer63c8be92015-04-15 09:38:48 +0000322 GetSoftwareBreakpointPCOffset (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size);
Todd Fialaaf245d12014-06-30 21:05:18 +0000323
324 Error
325 FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp);
326
327 /// Writes a siginfo_t structure corresponding to the given thread ID to the
328 /// memory region pointed to by @p siginfo.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000329 Error
330 GetSignalInfo(lldb::tid_t tid, void *siginfo);
Todd Fialaaf245d12014-06-30 21:05:18 +0000331
332 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
333 /// corresponding to the given thread ID to the memory pointed to by @p
334 /// message.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000335 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000336 GetEventMessage(lldb::tid_t tid, unsigned long *message);
337
338 /// Resumes the given thread. If @p signo is anything but
339 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000340 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000341 Resume(lldb::tid_t tid, uint32_t signo);
342
343 /// Single steps the given thread. If @p signo is anything but
344 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000345 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000346 SingleStep(lldb::tid_t tid, uint32_t signo);
347
Chaoren Linfa03ad22015-02-03 01:50:42 +0000348 // ThreadStateCoordinator helper methods.
Todd Fiala511e5cd2014-09-11 23:29:14 +0000349 void
Chaoren Linfa03ad22015-02-03 01:50:42 +0000350 NotifyThreadCreateStopped (lldb::tid_t tid);
Todd Fiala511e5cd2014-09-11 23:29:14 +0000351
352 void
Chaoren Linfa03ad22015-02-03 01:50:42 +0000353 NotifyThreadCreateRunning (lldb::tid_t tid);
354
355 void
356 NotifyThreadDeath (lldb::tid_t tid);
357
358 void
359 NotifyThreadStop (lldb::tid_t tid);
360
361 void
362 CallAfterRunningThreadsStop (lldb::tid_t tid,
363 const std::function<void (lldb::tid_t tid)> &call_after_function);
Todd Fiala511e5cd2014-09-11 23:29:14 +0000364
Chaoren Lin03f12d62015-02-03 01:50:49 +0000365 void
366 CallAfterRunningThreadsStopWithSkipTID (lldb::tid_t deferred_signal_tid,
367 lldb::tid_t skip_stop_request_tid,
368 const std::function<void (lldb::tid_t tid)> &call_after_function);
369
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000370 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000371 Detach(lldb::tid_t tid);
Chaoren Lin86fd8e42015-02-03 01:51:15 +0000372
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000373 Error
Chaoren Lin86fd8e42015-02-03 01:51:15 +0000374 RequestThreadStop (const lldb::pid_t pid, const lldb::tid_t tid);
Pavel Labathc0765592015-05-06 10:46:34 +0000375
376
377 public:
378 // Typedefs.
379 typedef std::unordered_set<lldb::tid_t> ThreadIDSet;
380
381 // Callback/block definitions.
382 typedef std::function<void (lldb::tid_t tid)> ThreadIDFunction;
383 typedef std::function<void (const char *format, va_list args)> LogFunction;
384 typedef std::function<void (const std::string &error_message)> ErrorFunction;
385 typedef std::function<Error (lldb::tid_t tid)> StopThreadFunction;
386 typedef std::function<Error (lldb::tid_t tid, bool supress_signal)> ResumeThreadFunction;
387
388 private:
389 // Notify the coordinator when a thread is created and/or starting to be
390 // tracked. is_stopped should be true if the thread is currently stopped;
391 // otherwise, it should be set false if it is already running. Will
392 // call the error function if the thread id is already tracked.
393 void
394 NotifyThreadCreate (lldb::tid_t tid,
395 bool is_stopped,
396 const ErrorFunction &error_function);
397
398 // Notify the coordinator when a previously-existing thread should no
399 // longer be tracked. The error_function will trigger if the thread
400 // is not being tracked.
401 void
402 NotifyThreadDeath (lldb::tid_t tid,
403 const ErrorFunction &error_function);
404
405
406 // This method is the main purpose of the class: triggering a deferred
407 // action after a given set of threads stop. The triggering_tid is the
408 // thread id passed to the call_after_function. The error_function will
409 // be fired if either the triggering tid or any of the wait_for_stop_tids
410 // are unknown at the time the method is processed.
411 void
412 CallAfterThreadsStop (lldb::tid_t triggering_tid,
413 const ThreadIDSet &wait_for_stop_tids,
414 const StopThreadFunction &request_thread_stop_function,
415 const ThreadIDFunction &call_after_function,
416 const ErrorFunction &error_function);
417
418 // This method is the main purpose of the class: triggering a deferred
419 // action after all non-stopped threads stop. The triggering_tid is the
420 // thread id passed to the call_after_function. The error_function will
421 // be fired if the triggering tid is unknown at the time of execution.
422 void
423 CallAfterRunningThreadsStop (lldb::tid_t triggering_tid,
424 const StopThreadFunction &request_thread_stop_function,
425 const ThreadIDFunction &call_after_function,
426 const ErrorFunction &error_function);
427
428 // This method is the main purpose of the class: triggering a deferred
429 // action after all non-stopped threads stop. The triggering_tid is the
430 // thread id passed to the call_after_function. The error_function will
431 // be fired if the triggering tid is unknown at the time of execution.
432 // This variant will send stop requests to all non-stopped threads except
433 // for any contained in skip_stop_request_tids.
434 void
435 CallAfterRunningThreadsStopWithSkipTIDs (lldb::tid_t triggering_tid,
436 const ThreadIDSet &skip_stop_request_tids,
437 const StopThreadFunction &request_thread_stop_function,
438 const ThreadIDFunction &call_after_function,
439 const ErrorFunction &error_function);
440
441 // Notify the thread stopped. Will trigger error at time of execution if we
442 // already think it is stopped.
443 void
444 NotifyThreadStop (lldb::tid_t tid,
445 bool initiated_by_llgs,
446 const ErrorFunction &error_function);
447
448 // Request that the given thread id should have the request_thread_resume_function
449 // called. Will trigger the error_function if the thread is thought to be running
450 // already at that point. This call signals an error if the thread resume is for
451 // a thread that is already in a running state.
452 void
453 RequestThreadResume (lldb::tid_t tid,
454 const ResumeThreadFunction &request_thread_resume_function,
455 const ErrorFunction &error_function);
456
457 // Request that the given thread id should have the request_thread_resume_function
458 // called. Will trigger the error_function if the thread is thought to be running
459 // already at that point. This call ignores threads that are already running and
460 // does not trigger an error in that case.
461 void
462 RequestThreadResumeAsNeeded (lldb::tid_t tid,
463 const ResumeThreadFunction &request_thread_resume_function,
464 const ErrorFunction &error_function);
465
466 // Indicate the calling process did an exec and that the thread state
467 // should be 100% cleared.
468 void
469 ResetForExec ();
470
471 // Enable/disable verbose logging of event processing.
472 void
473 LogEnableEventProcessing (bool enabled);
474
475 private:
476
477 enum class ThreadState
478 {
479 Running,
480 Stopped
481 };
482
483 struct ThreadContext
484 {
485 ThreadState m_state;
486 bool m_stop_requested = false;
487 ResumeThreadFunction m_request_resume_function;
488 };
489 typedef std::unordered_map<lldb::tid_t, ThreadContext> TIDContextMap;
490
491 struct PendingNotification
492 {
493 PendingNotification (lldb::tid_t triggering_tid,
494 const ThreadIDSet &wait_for_stop_tids,
495 const StopThreadFunction &request_thread_stop_function,
496 const ThreadIDFunction &call_after_function,
497 const ErrorFunction &error_function):
498 triggering_tid (triggering_tid),
499 wait_for_stop_tids (wait_for_stop_tids),
500 original_wait_for_stop_tids (wait_for_stop_tids),
501 request_thread_stop_function (request_thread_stop_function),
502 call_after_function (call_after_function),
503 error_function (error_function),
504 request_stop_on_all_unstopped_threads (false),
505 skip_stop_request_tids ()
506 {
507 }
508
509 PendingNotification (lldb::tid_t triggering_tid,
510 const StopThreadFunction &request_thread_stop_function,
511 const ThreadIDFunction &call_after_function,
512 const ErrorFunction &error_function) :
513 triggering_tid (triggering_tid),
514 wait_for_stop_tids (),
515 original_wait_for_stop_tids (),
516 request_thread_stop_function (request_thread_stop_function),
517 call_after_function (call_after_function),
518 error_function (error_function),
519 request_stop_on_all_unstopped_threads (true),
520 skip_stop_request_tids ()
521 {
522 }
523
524 PendingNotification (lldb::tid_t triggering_tid,
525 const StopThreadFunction &request_thread_stop_function,
526 const ThreadIDFunction &call_after_function,
527 const ThreadIDSet &skip_stop_request_tids,
528 const ErrorFunction &error_function) :
529 triggering_tid (triggering_tid),
530 wait_for_stop_tids (),
531 original_wait_for_stop_tids (),
532 request_thread_stop_function (request_thread_stop_function),
533 call_after_function (call_after_function),
534 error_function (error_function),
535 request_stop_on_all_unstopped_threads (true),
536 skip_stop_request_tids (skip_stop_request_tids)
537 {
538 }
539
540 const lldb::tid_t triggering_tid;
541 ThreadIDSet wait_for_stop_tids;
542 const ThreadIDSet original_wait_for_stop_tids;
543 StopThreadFunction request_thread_stop_function;
544 ThreadIDFunction call_after_function;
545 ErrorFunction error_function;
546 const bool request_stop_on_all_unstopped_threads;
547 ThreadIDSet skip_stop_request_tids;
548 };
549 typedef std::unique_ptr<PendingNotification> PendingNotificationUP;
550
551 // Fire pending notification if no pending thread stops remain.
552 void SignalIfRequirementsSatisfied();
553
554 bool
555 RequestStopOnAllSpecifiedThreads();
556
557 void
558 RequestStopOnAllRunningThreads();
559
560 void
561 RequestThreadStop (lldb::tid_t tid, ThreadContext& context);
562
563 std::mutex m_event_mutex; // Serializes execution of ProcessEvent. XXX
564
565 void
566 ThreadDidStop (lldb::tid_t tid, bool initiated_by_llgs, const ErrorFunction &error_function);
567
568 void
569 DoResume(lldb::tid_t tid, ResumeThreadFunction request_thread_resume_function,
570 ErrorFunction error_function, bool error_when_already_running);
571
572 void
573 DoCallAfterThreadsStop(PendingNotificationUP &&notification_up);
574
575 void
576 ThreadWasCreated (lldb::tid_t tid, bool is_stopped, const ErrorFunction &error_function);
577
578 void
579 ThreadDidDie (lldb::tid_t tid, const ErrorFunction &error_function);
580
581 bool
582 IsKnownThread(lldb::tid_t tid) const;
583
584 void
585 TSCLog (const char *format, ...);
586
587 // Member variables.
588 LogFunction m_log_function;
589 PendingNotificationUP m_pending_notification_up;
590
591 // Maps known TIDs to ThreadContext.
592 TIDContextMap m_tid_map;
593
594 bool m_log_event_processing;
Todd Fialaaf245d12014-06-30 21:05:18 +0000595 };
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000596
597} // namespace process_linux
598} // namespace lldb_private
Todd Fialaaf245d12014-06-30 21:05:18 +0000599
600#endif // #ifndef liblldb_NativeProcessLinux_H_