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/StructuredData.h"
27 #include "lldb/Core/ThreadSafeValue.h"
28 #include "lldb/lldb-private-forward.h"
29 #include "lldb/Target/Process.h"
30 #include "lldb/Target/Thread.h"
31 
32 #include "GDBRemoteCommunicationClient.h"
33 #include "Utility/StringExtractor.h"
34 #include "GDBRemoteRegisterContext.h"
35 
36 class ThreadGDBRemote;
37 
38 class ProcessGDBRemote : public lldb_private::Process
39 {
40 public:
41     //------------------------------------------------------------------
42     // Constructors and Destructors
43     //------------------------------------------------------------------
44     static lldb::ProcessSP
45     CreateInstance (lldb_private::Target& target,
46                     lldb_private::Listener &listener,
47                     const lldb_private::FileSpec *crash_file_path);
48 
49     static void
50     Initialize();
51 
52     static void
53     DebuggerInitialize (lldb_private::Debugger &debugger);
54 
55     static void
56     Terminate();
57 
58     static lldb_private::ConstString
59     GetPluginNameStatic();
60 
61     static const char *
62     GetPluginDescriptionStatic();
63 
64     //------------------------------------------------------------------
65     // Constructors and Destructors
66     //------------------------------------------------------------------
67     ProcessGDBRemote(lldb_private::Target& target, lldb_private::Listener &listener);
68 
69     virtual
70     ~ProcessGDBRemote();
71 
72     //------------------------------------------------------------------
73     // Check if a given Process
74     //------------------------------------------------------------------
75     virtual bool
76     CanDebug (lldb_private::Target &target,
77               bool plugin_specified_by_name);
78 
79     virtual lldb_private::CommandObject *
80     GetPluginCommandObject();
81 
82     //------------------------------------------------------------------
83     // Creating a new process, or attaching to an existing one
84     //------------------------------------------------------------------
85     virtual lldb_private::Error
86     WillLaunch (lldb_private::Module* module);
87 
88     virtual lldb_private::Error
89     DoLaunch (lldb_private::Module *exe_module,
90               lldb_private::ProcessLaunchInfo &launch_info);
91 
92     virtual void
93     DidLaunch ();
94 
95     virtual lldb_private::Error
96     WillAttachToProcessWithID (lldb::pid_t pid);
97 
98     virtual lldb_private::Error
99     WillAttachToProcessWithName (const char *process_name, bool wait_for_launch);
100 
101     virtual lldb_private::Error
102     DoConnectRemote (lldb_private::Stream *strm, const char *remote_url);
103 
104     lldb_private::Error
105     WillLaunchOrAttach ();
106 
107     virtual lldb_private::Error
108     DoAttachToProcessWithID (lldb::pid_t pid);
109 
110     virtual lldb_private::Error
111     DoAttachToProcessWithID (lldb::pid_t pid, const lldb_private::ProcessAttachInfo &attach_info);
112 
113     virtual lldb_private::Error
114     DoAttachToProcessWithName (const char *process_name,
115                                const lldb_private::ProcessAttachInfo &attach_info);
116 
117     virtual void
118     DidAttach (lldb_private::ArchSpec &process_arch);
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
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 &
221     GetGDBRemote()
222     {
223         return m_gdb_comm;
224     }
225 
226     virtual lldb_private::Error
227     SendEventData(const char *data);
228 
229     //----------------------------------------------------------------------
230     // Override SetExitStatus so we can disconnect from the remote GDB server
231     //----------------------------------------------------------------------
232     virtual bool
233     SetExitStatus (int exit_status, const char *cstr);
234 
235     void
236     SetUserSpecifiedMaxMemoryTransferSize (uint64_t user_specified_max);
237 
238 protected:
239     friend class ThreadGDBRemote;
240     friend class GDBRemoteCommunicationClient;
241     friend class GDBRemoteRegisterContext;
242 
243     //----------------------------------------------------------------------
244     // Accessors
245     //----------------------------------------------------------------------
246     bool
247     IsRunning ( lldb::StateType state )
248     {
249         return    state == lldb::eStateRunning || IsStepping(state);
250     }
251 
252     bool
253     IsStepping ( lldb::StateType state)
254     {
255         return    state == lldb::eStateStepping;
256     }
257     bool
258     CanResume ( lldb::StateType state)
259     {
260         return state == lldb::eStateStopped;
261     }
262 
263     bool
264     HasExited (lldb::StateType state)
265     {
266         return state == lldb::eStateExited;
267     }
268 
269     bool
270     ProcessIDIsValid ( ) const;
271 
272     void
273     Clear ( );
274 
275     lldb_private::Flags &
276     GetFlags ()
277     {
278         return m_flags;
279     }
280 
281     const lldb_private::Flags &
282     GetFlags () const
283     {
284         return m_flags;
285     }
286 
287     virtual bool
288     UpdateThreadList (lldb_private::ThreadList &old_thread_list,
289                       lldb_private::ThreadList &new_thread_list);
290 
291     lldb_private::Error
292     LaunchAndConnectToDebugserver (const lldb_private::ProcessInfo &process_info);
293 
294     void
295     KillDebugserverProcess ();
296 
297     void
298     BuildDynamicRegisterInfo (bool force);
299 
300     void
301     SetLastStopPacket (const StringExtractorGDBRemote &response);
302 
303     bool
304     ParsePythonTargetDefinition(const lldb_private::FileSpec &target_definition_fspec);
305 
306     bool
307     ParseRegisters(lldb_private::ScriptInterpreterObject *registers_array);
308 
309     const lldb::DataBufferSP
310     GetAuxvData() override;
311 
312     lldb_private::StructuredData::ObjectSP
313     GetExtendedInfoForThread (lldb::tid_t tid);
314 
315     void
316     GetMaxMemorySize();
317 
318     //------------------------------------------------------------------
319     /// Broadcaster event bits definitions.
320     //------------------------------------------------------------------
321     enum
322     {
323         eBroadcastBitAsyncContinue                  = (1 << 0),
324         eBroadcastBitAsyncThreadShouldExit          = (1 << 1),
325         eBroadcastBitAsyncThreadDidExit             = (1 << 2)
326     };
327 
328     typedef enum AsyncThreadState
329     {
330         eAsyncThreadNotStarted,
331         eAsyncThreadRunning,
332         eAsyncThreadDone
333     } AsyncThreadState;
334 
335     lldb_private::Flags m_flags;            // Process specific flags (see eFlags enums)
336     GDBRemoteCommunicationClient m_gdb_comm;
337     lldb::pid_t m_debugserver_pid;
338     StringExtractorGDBRemote m_last_stop_packet;
339     lldb_private::Mutex m_last_stop_packet_mutex;
340     GDBRemoteDynamicRegisterInfo m_register_info;
341     lldb_private::Broadcaster m_async_broadcaster;
342     lldb::thread_t m_async_thread;
343     AsyncThreadState m_async_thread_state;
344     lldb_private::Mutex m_async_thread_state_mutex;
345     typedef std::vector<lldb::tid_t> tid_collection;
346     typedef std::vector< std::pair<lldb::tid_t,int> > tid_sig_collection;
347     typedef std::map<lldb::addr_t, lldb::addr_t> MMapMap;
348     tid_collection m_thread_ids; // Thread IDs for all threads. This list gets updated after stopping
349     tid_collection m_continue_c_tids;                  // 'c' for continue
350     tid_sig_collection m_continue_C_tids; // 'C' for continue with signal
351     tid_collection m_continue_s_tids;                  // 's' for step
352     tid_sig_collection m_continue_S_tids; // 'S' for step with signal
353     uint64_t m_max_memory_size;       // The maximum number of bytes to read/write when reading and writing memory
354     uint64_t m_remote_stub_max_memory_size;    // The maximum memory size the remote gdb stub can handle
355     MMapMap m_addr_to_mmap_size;
356     lldb::BreakpointSP m_thread_create_bp_sp;
357     bool m_waiting_for_attach;
358     bool m_destroy_tried_resuming;
359     lldb::CommandObjectSP m_command_sp;
360     int64_t m_breakpoint_pc_offset;
361 
362     bool
363     StartAsyncThread ();
364 
365     void
366     StopAsyncThread ();
367 
368     static lldb::thread_result_t
369     AsyncThread (void *arg);
370 
371     static bool
372     MonitorDebugserverProcess (void *callback_baton,
373                                lldb::pid_t pid,
374                                bool exited,
375                                int signo,
376                                int exit_status);
377 
378     lldb::StateType
379     SetThreadStopInfo (StringExtractor& stop_packet);
380 
381     void
382     ClearThreadIDList ();
383 
384     bool
385     UpdateThreadIDList ();
386 
387     void
388     DidLaunchOrAttach (lldb_private::ArchSpec& process_arch);
389 
390     lldb_private::Error
391     ConnectToDebugserver (const char *host_port);
392 
393     const char *
394     GetDispatchQueueNameForThread (lldb::addr_t thread_dispatch_qaddr,
395                                    std::string &dispatch_queue_name);
396 
397     lldb_private::DynamicLoader *
398     GetDynamicLoader ();
399 
400 private:
401     //------------------------------------------------------------------
402     // For ProcessGDBRemote only
403     //------------------------------------------------------------------
404     static bool
405     NewThreadNotifyBreakpointHit (void *baton,
406                          lldb_private::StoppointCallbackContext *context,
407                          lldb::user_id_t break_id,
408                          lldb::user_id_t break_loc_id);
409 
410     DISALLOW_COPY_AND_ASSIGN (ProcessGDBRemote);
411 
412 };
413 
414 #endif  // liblldb_ProcessGDBRemote_h_
415