1 //===-- NativeThreadLinux.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_NativeThreadLinux_H_
11 #define liblldb_NativeThreadLinux_H_
12 
13 #include "Plugins/Process/Linux/NativeRegisterContextLinux.h"
14 #include "Plugins/Process/Linux/SingleStepCheck.h"
15 #include "lldb/Host/common/NativeThreadProtocol.h"
16 #include "lldb/lldb-private-forward.h"
17 
18 #include <csignal>
19 #include <map>
20 #include <memory>
21 #include <string>
22 
23 namespace lldb_private {
24 namespace process_linux {
25 
26 class NativeProcessLinux;
27 
28 class NativeThreadLinux : public NativeThreadProtocol {
29   friend class NativeProcessLinux;
30 
31 public:
32   NativeThreadLinux(NativeProcessLinux &process, lldb::tid_t tid);
33 
34   // ---------------------------------------------------------------------
35   // NativeThreadProtocol Interface
36   // ---------------------------------------------------------------------
37   std::string GetName() override;
38 
39   lldb::StateType GetState() override;
40 
41   bool GetStopReason(ThreadStopInfo &stop_info,
42                      std::string &description) override;
43 
44   NativeRegisterContextLinux &GetRegisterContext() override {
45     return *m_reg_context_up;
46   }
47 
48   Status SetWatchpoint(lldb::addr_t addr, size_t size, uint32_t watch_flags,
49                        bool hardware) override;
50 
51   Status RemoveWatchpoint(lldb::addr_t addr) override;
52 
53   Status SetHardwareBreakpoint(lldb::addr_t addr, size_t size) override;
54 
55   Status RemoveHardwareBreakpoint(lldb::addr_t addr) override;
56 
57 private:
58   // ---------------------------------------------------------------------
59   // Interface for friend classes
60   // ---------------------------------------------------------------------
61 
62   /// Resumes the thread.  If @p signo is anything but
63   /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
64   Status Resume(uint32_t signo);
65 
66   /// Single steps the thread.  If @p signo is anything but
67   /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
68   Status SingleStep(uint32_t signo);
69 
70   void SetStoppedBySignal(uint32_t signo, const siginfo_t *info = nullptr);
71 
72   /// Return true if the thread is stopped.
73   /// If stopped by a signal, indicate the signo in the signo argument.
74   /// Otherwise, return LLDB_INVALID_SIGNAL_NUMBER.
75   bool IsStopped(int *signo);
76 
77   void SetStoppedByExec();
78 
79   void SetStoppedByBreakpoint();
80 
81   void SetStoppedByWatchpoint(uint32_t wp_index);
82 
83   bool IsStoppedAtBreakpoint();
84 
85   bool IsStoppedAtWatchpoint();
86 
87   void SetStoppedByTrace();
88 
89   void SetStoppedWithNoReason();
90 
91   void SetExited();
92 
93   Status RequestStop();
94 
95   // ---------------------------------------------------------------------
96   // Private interface
97   // ---------------------------------------------------------------------
98   void MaybeLogStateChange(lldb::StateType new_state);
99 
100   NativeProcessLinux &GetProcess();
101 
102   void SetStopped();
103 
104   // ---------------------------------------------------------------------
105   // Member Variables
106   // ---------------------------------------------------------------------
107   lldb::StateType m_state;
108   ThreadStopInfo m_stop_info;
109   std::unique_ptr<NativeRegisterContextLinux> m_reg_context_up;
110   std::string m_stop_description;
111   using WatchpointIndexMap = std::map<lldb::addr_t, uint32_t>;
112   WatchpointIndexMap m_watchpoint_index_map;
113   WatchpointIndexMap m_hw_break_index_map;
114   std::unique_ptr<SingleStepWorkaround> m_step_workaround;
115 };
116 } // namespace process_linux
117 } // namespace lldb_private
118 
119 #endif // #ifndef liblldb_NativeThreadLinux_H_
120