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