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