1 //===-- ProcessKDP.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_ProcessKDP_h_
11 #define liblldb_ProcessKDP_h_
12 
13 // C Includes
14 
15 // C++ Includes
16 #include <list>
17 #include <vector>
18 
19 // Other libraries and framework includes
20 #include "lldb/Core/ArchSpec.h"
21 #include "lldb/Core/Broadcaster.h"
22 #include "lldb/Core/Error.h"
23 #include "lldb/Core/InputReader.h"
24 #include "lldb/Core/StreamString.h"
25 #include "lldb/Core/StringList.h"
26 #include "lldb/Core/ThreadSafeValue.h"
27 #include "lldb/Target/Process.h"
28 #include "lldb/Target/Thread.h"
29 
30 #include "CommunicationKDP.h"
31 #include "Utility/StringExtractor.h"
32 
33 class ThreadKDP;
34 
35 class ProcessKDP : public lldb_private::Process
36 {
37 public:
38     //------------------------------------------------------------------
39     // Constructors and Destructors
40     //------------------------------------------------------------------
41     static lldb::ProcessSP
42     CreateInstance (lldb_private::Target& target,
43                     lldb_private::Listener &listener,
44                     const lldb_private::FileSpec *crash_file_path);
45 
46     static void
47     Initialize();
48 
49     static void
50     Terminate();
51 
52     static const char *
53     GetPluginNameStatic();
54 
55     static const char *
56     GetPluginDescriptionStatic();
57 
58     //------------------------------------------------------------------
59     // Constructors and Destructors
60     //------------------------------------------------------------------
61     ProcessKDP(lldb_private::Target& target, lldb_private::Listener &listener);
62 
63     virtual
64     ~ProcessKDP();
65 
66     //------------------------------------------------------------------
67     // Check if a given Process
68     //------------------------------------------------------------------
69     virtual bool
70     CanDebug (lldb_private::Target &target,
71               bool plugin_specified_by_name);
72 
73     //    virtual uint32_t
74     //    ListProcessesMatchingName (const char *name, lldb_private::StringList &matches, std::vector<lldb::pid_t> &pids);
75 
76     //------------------------------------------------------------------
77     // Creating a new process, or attaching to an existing one
78     //------------------------------------------------------------------
79     virtual lldb_private::Error
80     WillLaunch (lldb_private::Module* module);
81 
82     virtual lldb_private::Error
83     DoLaunch (lldb_private::Module *exe_module,
84               const lldb_private::ProcessLaunchInfo &launch_info);
85 
86     virtual lldb_private::Error
87     WillAttachToProcessWithID (lldb::pid_t pid);
88 
89     virtual lldb_private::Error
90     WillAttachToProcessWithName (const char *process_name, bool wait_for_launch);
91 
92     virtual lldb_private::Error
93     DoConnectRemote (const char *remote_url);
94 
95     virtual lldb_private::Error
96     DoAttachToProcessWithID (lldb::pid_t pid);
97 
98     virtual lldb_private::Error
99     DoAttachToProcessWithName (const char *process_name, bool wait_for_launch);
100 
101     virtual void
102     DidAttach ();
103 
104     //------------------------------------------------------------------
105     // PluginInterface protocol
106     //------------------------------------------------------------------
107     virtual const char *
108     GetPluginName();
109 
110     virtual const char *
111     GetShortPluginName();
112 
113     virtual uint32_t
114     GetPluginVersion();
115 
116     //------------------------------------------------------------------
117     // Process Control
118     //------------------------------------------------------------------
119     virtual lldb_private::Error
120     WillResume ();
121 
122     virtual lldb_private::Error
123     DoResume ();
124 
125     virtual lldb_private::Error
126     DoHalt (bool &caused_stop);
127 
128     virtual lldb_private::Error
129     WillDetach ();
130 
131     virtual lldb_private::Error
132     DoDetach ();
133 
134     virtual lldb_private::Error
135     DoSignal (int signal);
136 
137     virtual lldb_private::Error
138     DoDestroy ();
139 
140     virtual void
141     RefreshStateAfterStop();
142 
143     //------------------------------------------------------------------
144     // Process Queries
145     //------------------------------------------------------------------
146     virtual bool
147     IsAlive ();
148 
149     //------------------------------------------------------------------
150     // Process Memory
151     //------------------------------------------------------------------
152     virtual size_t
153     DoReadMemory (lldb::addr_t addr, void *buf, size_t size, lldb_private::Error &error);
154 
155     virtual size_t
156     DoWriteMemory (lldb::addr_t addr, const void *buf, size_t size, lldb_private::Error &error);
157 
158     virtual lldb::addr_t
159     DoAllocateMemory (size_t size, uint32_t permissions, lldb_private::Error &error);
160 
161     virtual lldb_private::Error
162     DoDeallocateMemory (lldb::addr_t ptr);
163 
164     //----------------------------------------------------------------------
165     // Process Breakpoints
166     //----------------------------------------------------------------------
167     virtual lldb_private::Error
168     EnableBreakpoint (lldb_private::BreakpointSite *bp_site);
169 
170     virtual lldb_private::Error
171     DisableBreakpoint (lldb_private::BreakpointSite *bp_site);
172 
173     //----------------------------------------------------------------------
174     // Process Watchpoints
175     //----------------------------------------------------------------------
176     virtual lldb_private::Error
177     EnableWatchpoint (lldb_private::Watchpoint *wp);
178 
179     virtual lldb_private::Error
180     DisableWatchpoint (lldb_private::Watchpoint *wp);
181 
182     CommunicationKDP &
183     GetCommunication()
184     {
185         return m_comm;
186     }
187 
188 protected:
189     friend class ThreadKDP;
190     friend class CommunicationKDP;
191 
192     //----------------------------------------------------------------------
193     // Accessors
194     //----------------------------------------------------------------------
195     bool
196     IsRunning ( lldb::StateType state )
197     {
198         return    state == lldb::eStateRunning || IsStepping(state);
199     }
200 
201     bool
202     IsStepping ( lldb::StateType state)
203     {
204         return    state == lldb::eStateStepping;
205     }
206 
207     bool
208     CanResume ( lldb::StateType state)
209     {
210         return state == lldb::eStateStopped;
211     }
212 
213     bool
214     HasExited (lldb::StateType state)
215     {
216         return state == lldb::eStateExited;
217     }
218 
219     bool
220     ProcessIDIsValid ( ) const;
221 
222     //    static void
223     //    STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len);
224 
225     //    void
226     //    AppendSTDOUT (const char* s, size_t len);
227 
228     void
229     Clear ( );
230 
231     uint32_t
232     UpdateThreadList (lldb_private::ThreadList &old_thread_list,
233                       lldb_private::ThreadList &new_thread_list);
234 
235     enum
236     {
237         eBroadcastBitAsyncContinue                  = (1 << 0),
238         eBroadcastBitAsyncThreadShouldExit          = (1 << 1)
239     };
240 
241     lldb_private::Error
242     InterruptIfRunning (bool discard_thread_plans,
243                         bool catch_stop_event,
244                         lldb::EventSP &stop_event_sp);
245 
246     //------------------------------------------------------------------
247     /// Broadcaster event bits definitions.
248     //------------------------------------------------------------------
249     CommunicationKDP m_comm;
250     lldb_private::Broadcaster m_async_broadcaster;
251     lldb::thread_t m_async_thread;
252 
253     bool
254     StartAsyncThread ();
255 
256     void
257     StopAsyncThread ();
258 
259     static void *
260     AsyncThread (void *arg);
261 
262     lldb::StateType
263     SetThreadStopInfo (StringExtractor& stop_packet);
264 
265 private:
266     //------------------------------------------------------------------
267     // For ProcessKDP only
268     //------------------------------------------------------------------
269 
270     DISALLOW_COPY_AND_ASSIGN (ProcessKDP);
271 
272 };
273 
274 #endif  // liblldb_ProcessKDP_h_
275