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