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 uint32_t
75 //    ListProcessesMatchingName (const char *name, lldb_private::StringList &matches, std::vector<lldb::pid_t> &pids);
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 (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     //------------------------------------------------------------------
117     // PluginInterface protocol
118     //------------------------------------------------------------------
119     virtual const char *
120     GetPluginName();
121 
122     virtual const char *
123     GetShortPluginName();
124 
125     virtual uint32_t
126     GetPluginVersion();
127 
128     //------------------------------------------------------------------
129     // Process Control
130     //------------------------------------------------------------------
131     virtual lldb_private::Error
132     WillResume ();
133 
134     virtual lldb_private::Error
135     DoResume ();
136 
137     virtual lldb_private::Error
138     DoHalt (bool &caused_stop);
139 
140     virtual lldb_private::Error
141     WillDetach ();
142 
143     virtual lldb_private::Error
144     DoDetach ();
145 
146     virtual lldb_private::Error
147     DoSignal (int signal);
148 
149     virtual lldb_private::Error
150     DoDestroy ();
151 
152     virtual void
153     RefreshStateAfterStop();
154 
155     //------------------------------------------------------------------
156     // Process Queries
157     //------------------------------------------------------------------
158     virtual bool
159     IsAlive ();
160 
161     virtual lldb::addr_t
162     GetImageInfoAddress();
163 
164     //------------------------------------------------------------------
165     // Process Memory
166     //------------------------------------------------------------------
167     virtual size_t
168     DoReadMemory (lldb::addr_t addr, void *buf, size_t size, lldb_private::Error &error);
169 
170     virtual size_t
171     DoWriteMemory (lldb::addr_t addr, const void *buf, size_t size, lldb_private::Error &error);
172 
173     virtual lldb::addr_t
174     DoAllocateMemory (size_t size, uint32_t permissions, lldb_private::Error &error);
175 
176     virtual lldb_private::Error
177     GetMemoryRegionInfo (lldb::addr_t load_addr,
178                          lldb_private::MemoryRegionInfo &region_info);
179 
180     virtual lldb_private::Error
181     DoDeallocateMemory (lldb::addr_t ptr);
182 
183     //------------------------------------------------------------------
184     // Process STDIO
185     //------------------------------------------------------------------
186     virtual size_t
187     PutSTDIN (const char *buf, size_t buf_size, lldb_private::Error &error);
188 
189     //----------------------------------------------------------------------
190     // Process Breakpoints
191     //----------------------------------------------------------------------
192     virtual lldb_private::Error
193     EnableBreakpoint (lldb_private::BreakpointSite *bp_site);
194 
195     virtual lldb_private::Error
196     DisableBreakpoint (lldb_private::BreakpointSite *bp_site);
197 
198     //----------------------------------------------------------------------
199     // Process Watchpoints
200     //----------------------------------------------------------------------
201     virtual lldb_private::Error
202     EnableWatchpoint (lldb_private::Watchpoint *wp);
203 
204     virtual lldb_private::Error
205     DisableWatchpoint (lldb_private::Watchpoint *wp);
206 
207     virtual lldb_private::Error
208     GetWatchpointSupportInfo (uint32_t &num);
209 
210     virtual lldb_private::Error
211     GetWatchpointSupportInfo (uint32_t &num, bool& after);
212 
213     virtual bool
214     StartNoticingNewThreads();
215 
216     virtual bool
217     StopNoticingNewThreads();
218 
219     GDBRemoteCommunicationClient &
220     GetGDBRemote()
221     {
222         return m_gdb_comm;
223     }
224 
225 protected:
226     friend class ThreadGDBRemote;
227     friend class GDBRemoteCommunicationClient;
228     friend class GDBRemoteRegisterContext;
229 
230     //----------------------------------------------------------------------
231     // Accessors
232     //----------------------------------------------------------------------
233     bool
234     IsRunning ( lldb::StateType state )
235     {
236         return    state == lldb::eStateRunning || IsStepping(state);
237     }
238 
239     bool
240     IsStepping ( lldb::StateType state)
241     {
242         return    state == lldb::eStateStepping;
243     }
244     bool
245     CanResume ( lldb::StateType state)
246     {
247         return state == lldb::eStateStopped;
248     }
249 
250     bool
251     HasExited (lldb::StateType state)
252     {
253         return state == lldb::eStateExited;
254     }
255 
256     bool
257     ProcessIDIsValid ( ) const;
258 
259     void
260     Clear ( );
261 
262     lldb_private::Flags &
263     GetFlags ()
264     {
265         return m_flags;
266     }
267 
268     const lldb_private::Flags &
269     GetFlags () const
270     {
271         return m_flags;
272     }
273 
274     virtual bool
275     UpdateThreadList (lldb_private::ThreadList &old_thread_list,
276                       lldb_private::ThreadList &new_thread_list);
277 
278     lldb_private::Error
279     StartDebugserverProcess (const char *debugserver_url);
280 
281     lldb_private::Error
282     StartDebugserverProcess (const char *debugserver_url, const lldb_private::ProcessInfo &process_info);
283 
284     void
285     KillDebugserverProcess ();
286 
287     void
288     BuildDynamicRegisterInfo (bool force);
289 
290     void
291     SetLastStopPacket (const StringExtractorGDBRemote &response)
292     {
293         lldb_private::Mutex::Locker locker (m_last_stop_packet_mutex);
294         m_last_stop_packet = response;
295     }
296     //------------------------------------------------------------------
297     /// Broadcaster event bits definitions.
298     //------------------------------------------------------------------
299     enum
300     {
301         eBroadcastBitAsyncContinue                  = (1 << 0),
302         eBroadcastBitAsyncThreadShouldExit          = (1 << 1),
303         eBroadcastBitAsyncThreadDidExit             = (1 << 2)
304     };
305 
306     lldb_private::Flags m_flags;            // Process specific flags (see eFlags enums)
307     GDBRemoteCommunicationClient m_gdb_comm;
308     lldb::pid_t m_debugserver_pid;
309     StringExtractorGDBRemote m_last_stop_packet;
310     lldb_private::Mutex m_last_stop_packet_mutex;
311     GDBRemoteDynamicRegisterInfo m_register_info;
312     lldb_private::Broadcaster m_async_broadcaster;
313     lldb::thread_t m_async_thread;
314     typedef std::vector<lldb::tid_t> tid_collection;
315     typedef std::vector< std::pair<lldb::tid_t,int> > tid_sig_collection;
316     typedef std::map<lldb::addr_t, lldb::addr_t> MMapMap;
317     tid_collection m_thread_ids; // Thread IDs for all threads. This list gets updated after stopping
318     tid_collection m_continue_c_tids;                  // 'c' for continue
319     tid_sig_collection m_continue_C_tids; // 'C' for continue with signal
320     tid_collection m_continue_s_tids;                  // 's' for step
321     tid_sig_collection m_continue_S_tids; // 'S' for step with signal
322     lldb::addr_t m_dispatch_queue_offsets_addr;
323     size_t m_max_memory_size;       // The maximum number of bytes to read/write when reading and writing memory
324     MMapMap m_addr_to_mmap_size;
325     lldb::BreakpointSP m_thread_create_bp_sp;
326     bool m_waiting_for_attach;
327     bool m_destroy_tried_resuming;
328 
329     bool
330     StartAsyncThread ();
331 
332     void
333     StopAsyncThread ();
334 
335     static void *
336     AsyncThread (void *arg);
337 
338     static bool
339     MonitorDebugserverProcess (void *callback_baton,
340                                lldb::pid_t pid,
341                                bool exited,
342                                int signo,
343                                int exit_status);
344 
345     lldb::StateType
346     SetThreadStopInfo (StringExtractor& stop_packet);
347 
348     void
349     ClearThreadIDList ();
350 
351     bool
352     UpdateThreadIDList ();
353 
354     void
355     DidLaunchOrAttach ();
356 
357     lldb_private::Error
358     ConnectToDebugserver (const char *host_port);
359 
360     const char *
361     GetDispatchQueueNameForThread (lldb::addr_t thread_dispatch_qaddr,
362                                    std::string &dispatch_queue_name);
363 
364     static size_t
365     AttachInputReaderCallback (void *baton,
366                                lldb_private::InputReader *reader,
367                                lldb::InputReaderAction notification,
368                                const char *bytes,
369                                size_t bytes_len);
370 
371     lldb_private::Error
372     InterruptIfRunning (bool discard_thread_plans,
373                         bool catch_stop_event,
374                         lldb::EventSP &stop_event_sp);
375 
376 private:
377     //------------------------------------------------------------------
378     // For ProcessGDBRemote only
379     //------------------------------------------------------------------
380     static bool
381     NewThreadNotifyBreakpointHit (void *baton,
382                          lldb_private::StoppointCallbackContext *context,
383                          lldb::user_id_t break_id,
384                          lldb::user_id_t break_loc_id);
385 
386     DISALLOW_COPY_AND_ASSIGN (ProcessGDBRemote);
387 
388 };
389 
390 #endif  // liblldb_ProcessGDBRemote_h_
391