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