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