1 //===-- ProcessGDBRemote.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_ProcessGDBRemote_h_
11 #define liblldb_ProcessGDBRemote_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 "GDBRemoteCommunicationClient.h"
31 #include "Utility/StringExtractor.h"
32 #include "GDBRemoteRegisterContext.h"
33 
34 class ThreadGDBRemote;
35 
36 class ProcessGDBRemote : public lldb_private::Process
37 {
38 public:
39     //------------------------------------------------------------------
40     // Constructors and Destructors
41     //------------------------------------------------------------------
42     static lldb::ProcessSP
43     CreateInstance (lldb_private::Target& target,
44                     lldb_private::Listener &listener,
45                     const lldb_private::FileSpec *crash_file_path);
46 
47     static void
48     Initialize();
49 
50     static void
51     Terminate();
52 
53     static const char *
54     GetPluginNameStatic();
55 
56     static const char *
57     GetPluginDescriptionStatic();
58 
59     //------------------------------------------------------------------
60     // Constructors and Destructors
61     //------------------------------------------------------------------
62     ProcessGDBRemote(lldb_private::Target& target, lldb_private::Listener &listener);
63 
64     virtual
65     ~ProcessGDBRemote();
66 
67     //------------------------------------------------------------------
68     // Check if a given Process
69     //------------------------------------------------------------------
70     virtual bool
71     CanDebug (lldb_private::Target &target,
72               bool plugin_specified_by_name);
73 
74     virtual lldb_private::CommandObject *
75     GetPluginCommandObject();
76 
77     //------------------------------------------------------------------
78     // Creating a new process, or attaching to an existing one
79     //------------------------------------------------------------------
80     virtual lldb_private::Error
81     WillLaunch (lldb_private::Module* module);
82 
83     virtual lldb_private::Error
84     DoLaunch (lldb_private::Module *exe_module,
85               const lldb_private::ProcessLaunchInfo &launch_info);
86 
87     virtual void
88     DidLaunch ();
89 
90     virtual lldb_private::Error
91     WillAttachToProcessWithID (lldb::pid_t pid);
92 
93     virtual lldb_private::Error
94     WillAttachToProcessWithName (const char *process_name, bool wait_for_launch);
95 
96     virtual lldb_private::Error
97     DoConnectRemote (lldb_private::Stream *strm, const char *remote_url);
98 
99     lldb_private::Error
100     WillLaunchOrAttach ();
101 
102     virtual lldb_private::Error
103     DoAttachToProcessWithID (lldb::pid_t pid);
104 
105     virtual lldb_private::Error
106     DoAttachToProcessWithID (lldb::pid_t pid, const lldb_private::ProcessAttachInfo &attach_info);
107 
108     virtual lldb_private::Error
109     DoAttachToProcessWithName (const char *process_name,
110                                bool wait_for_launch,
111                                const lldb_private::ProcessAttachInfo &attach_info);
112 
113     virtual void
114     DidAttach ();
115 
116     virtual void
117     DoDidExec ();
118 
119     //------------------------------------------------------------------
120     // PluginInterface protocol
121     //------------------------------------------------------------------
122     virtual const char *
123     GetPluginName();
124 
125     virtual const char *
126     GetShortPluginName();
127 
128     virtual uint32_t
129     GetPluginVersion();
130 
131     //------------------------------------------------------------------
132     // Process Control
133     //------------------------------------------------------------------
134     virtual lldb_private::Error
135     WillResume ();
136 
137     virtual lldb_private::Error
138     DoResume ();
139 
140     virtual lldb_private::Error
141     DoHalt (bool &caused_stop);
142 
143     virtual lldb_private::Error
144     WillDetach ();
145 
146     virtual lldb_private::Error
147     DoDetach ();
148 
149     virtual lldb_private::Error
150     DoSignal (int signal);
151 
152     virtual lldb_private::Error
153     DoDestroy ();
154 
155     virtual void
156     RefreshStateAfterStop();
157 
158     //------------------------------------------------------------------
159     // Process Queries
160     //------------------------------------------------------------------
161     virtual bool
162     IsAlive ();
163 
164     virtual lldb::addr_t
165     GetImageInfoAddress();
166 
167     //------------------------------------------------------------------
168     // Process Memory
169     //------------------------------------------------------------------
170     virtual size_t
171     DoReadMemory (lldb::addr_t addr, void *buf, size_t size, lldb_private::Error &error);
172 
173     virtual size_t
174     DoWriteMemory (lldb::addr_t addr, const void *buf, size_t size, lldb_private::Error &error);
175 
176     virtual lldb::addr_t
177     DoAllocateMemory (size_t size, uint32_t permissions, lldb_private::Error &error);
178 
179     virtual lldb_private::Error
180     GetMemoryRegionInfo (lldb::addr_t load_addr,
181                          lldb_private::MemoryRegionInfo &region_info);
182 
183     virtual lldb_private::Error
184     DoDeallocateMemory (lldb::addr_t ptr);
185 
186     //------------------------------------------------------------------
187     // Process STDIO
188     //------------------------------------------------------------------
189     virtual size_t
190     PutSTDIN (const char *buf, size_t buf_size, lldb_private::Error &error);
191 
192     //----------------------------------------------------------------------
193     // Process Breakpoints
194     //----------------------------------------------------------------------
195     virtual lldb_private::Error
196     EnableBreakpointSite (lldb_private::BreakpointSite *bp_site);
197 
198     virtual lldb_private::Error
199     DisableBreakpointSite (lldb_private::BreakpointSite *bp_site);
200 
201     //----------------------------------------------------------------------
202     // Process Watchpoints
203     //----------------------------------------------------------------------
204     virtual lldb_private::Error
205     EnableWatchpoint (lldb_private::Watchpoint *wp, bool notify = true);
206 
207     virtual lldb_private::Error
208     DisableWatchpoint (lldb_private::Watchpoint *wp, bool notify = true);
209 
210     virtual lldb_private::Error
211     GetWatchpointSupportInfo (uint32_t &num);
212 
213     virtual lldb_private::Error
214     GetWatchpointSupportInfo (uint32_t &num, bool& after);
215 
216     virtual bool
217     StartNoticingNewThreads();
218 
219     virtual bool
220     StopNoticingNewThreads();
221 
222     GDBRemoteCommunicationClient &
223     GetGDBRemote()
224     {
225         return m_gdb_comm;
226     }
227 
228 protected:
229     friend class ThreadGDBRemote;
230     friend class GDBRemoteCommunicationClient;
231     friend class GDBRemoteRegisterContext;
232 
233     //----------------------------------------------------------------------
234     // Accessors
235     //----------------------------------------------------------------------
236     bool
237     IsRunning ( lldb::StateType state )
238     {
239         return    state == lldb::eStateRunning || IsStepping(state);
240     }
241 
242     bool
243     IsStepping ( lldb::StateType state)
244     {
245         return    state == lldb::eStateStepping;
246     }
247     bool
248     CanResume ( lldb::StateType state)
249     {
250         return state == lldb::eStateStopped;
251     }
252 
253     bool
254     HasExited (lldb::StateType state)
255     {
256         return state == lldb::eStateExited;
257     }
258 
259     bool
260     ProcessIDIsValid ( ) const;
261 
262     void
263     Clear ( );
264 
265     lldb_private::Flags &
266     GetFlags ()
267     {
268         return m_flags;
269     }
270 
271     const lldb_private::Flags &
272     GetFlags () const
273     {
274         return m_flags;
275     }
276 
277     virtual bool
278     UpdateThreadList (lldb_private::ThreadList &old_thread_list,
279                       lldb_private::ThreadList &new_thread_list);
280 
281     lldb_private::Error
282     StartDebugserverProcess (const char *debugserver_url);
283 
284     lldb_private::Error
285     StartDebugserverProcess (const char *debugserver_url, const lldb_private::ProcessInfo &process_info);
286 
287     void
288     KillDebugserverProcess ();
289 
290     void
291     BuildDynamicRegisterInfo (bool force);
292 
293     void
294     SetLastStopPacket (const StringExtractorGDBRemote &response)
295     {
296         lldb_private::Mutex::Locker locker (m_last_stop_packet_mutex);
297         m_last_stop_packet = response;
298     }
299 
300     //------------------------------------------------------------------
301     /// Broadcaster event bits definitions.
302     //------------------------------------------------------------------
303     enum
304     {
305         eBroadcastBitAsyncContinue                  = (1 << 0),
306         eBroadcastBitAsyncThreadShouldExit          = (1 << 1),
307         eBroadcastBitAsyncThreadDidExit             = (1 << 2)
308     };
309 
310     typedef enum AsyncThreadState
311     {
312         eAsyncThreadNotStarted,
313         eAsyncThreadRunning,
314         eAsyncThreadDone
315     } AsyncThreadState;
316 
317     lldb_private::Flags m_flags;            // Process specific flags (see eFlags enums)
318     GDBRemoteCommunicationClient m_gdb_comm;
319     lldb::pid_t m_debugserver_pid;
320     StringExtractorGDBRemote m_last_stop_packet;
321     lldb_private::Mutex m_last_stop_packet_mutex;
322     GDBRemoteDynamicRegisterInfo m_register_info;
323     lldb_private::Broadcaster m_async_broadcaster;
324     lldb::thread_t m_async_thread;
325     AsyncThreadState m_async_thread_state;
326     lldb_private::Mutex m_async_thread_state_mutex;
327     typedef std::vector<lldb::tid_t> tid_collection;
328     typedef std::vector< std::pair<lldb::tid_t,int> > tid_sig_collection;
329     typedef std::map<lldb::addr_t, lldb::addr_t> MMapMap;
330     tid_collection m_thread_ids; // Thread IDs for all threads. This list gets updated after stopping
331     tid_collection m_continue_c_tids;                  // 'c' for continue
332     tid_sig_collection m_continue_C_tids; // 'C' for continue with signal
333     tid_collection m_continue_s_tids;                  // 's' for step
334     tid_sig_collection m_continue_S_tids; // 'S' for step with signal
335     lldb::addr_t m_dispatch_queue_offsets_addr;
336     size_t m_max_memory_size;       // The maximum number of bytes to read/write when reading and writing memory
337     MMapMap m_addr_to_mmap_size;
338     lldb::BreakpointSP m_thread_create_bp_sp;
339     bool m_waiting_for_attach;
340     bool m_destroy_tried_resuming;
341     std::string m_dyld_plugin_name;
342     lldb::CommandObjectSP m_command_sp;
343 
344     bool
345     StartAsyncThread ();
346 
347     void
348     StopAsyncThread ();
349 
350     static void *
351     AsyncThread (void *arg);
352 
353     static bool
354     MonitorDebugserverProcess (void *callback_baton,
355                                lldb::pid_t pid,
356                                bool exited,
357                                int signo,
358                                int exit_status);
359 
360     lldb::StateType
361     SetThreadStopInfo (StringExtractor& stop_packet);
362 
363     void
364     ClearThreadIDList ();
365 
366     bool
367     UpdateThreadIDList ();
368 
369     void
370     DidLaunchOrAttach ();
371 
372     lldb_private::Error
373     ConnectToDebugserver (const char *host_port);
374 
375     const char *
376     GetDispatchQueueNameForThread (lldb::addr_t thread_dispatch_qaddr,
377                                    std::string &dispatch_queue_name);
378 
379     static size_t
380     AttachInputReaderCallback (void *baton,
381                                lldb_private::InputReader *reader,
382                                lldb::InputReaderAction notification,
383                                const char *bytes,
384                                size_t bytes_len);
385 
386     lldb_private::Error
387     InterruptIfRunning (bool discard_thread_plans,
388                         bool catch_stop_event,
389                         lldb::EventSP &stop_event_sp);
390 
391     lldb_private::DynamicLoader *
392     GetDynamicLoader ();
393 
394 private:
395     //------------------------------------------------------------------
396     // For ProcessGDBRemote only
397     //------------------------------------------------------------------
398     static bool
399     NewThreadNotifyBreakpointHit (void *baton,
400                          lldb_private::StoppointCallbackContext *context,
401                          lldb::user_id_t break_id,
402                          lldb::user_id_t break_loc_id);
403 
404     DISALLOW_COPY_AND_ASSIGN (ProcessGDBRemote);
405 
406 };
407 
408 #endif  // liblldb_ProcessGDBRemote_h_
409