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