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