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
DetachRequiresHalt()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 &
GetGDBRemote()219     GetGDBRemote()
220     {
221         return m_gdb_comm;
222     }
223 
224     //----------------------------------------------------------------------
225     // Override SetExitStatus so we can disconnect from the remote GDB server
226     //----------------------------------------------------------------------
227     virtual bool
228     SetExitStatus (int exit_status, const char *cstr);
229 
230 
231 protected:
232     friend class ThreadGDBRemote;
233     friend class GDBRemoteCommunicationClient;
234     friend class GDBRemoteRegisterContext;
235 
236     //----------------------------------------------------------------------
237     // Accessors
238     //----------------------------------------------------------------------
239     bool
IsRunning(lldb::StateType state)240     IsRunning ( lldb::StateType state )
241     {
242         return    state == lldb::eStateRunning || IsStepping(state);
243     }
244 
245     bool
IsStepping(lldb::StateType state)246     IsStepping ( lldb::StateType state)
247     {
248         return    state == lldb::eStateStepping;
249     }
250     bool
CanResume(lldb::StateType state)251     CanResume ( lldb::StateType state)
252     {
253         return state == lldb::eStateStopped;
254     }
255 
256     bool
HasExited(lldb::StateType state)257     HasExited (lldb::StateType state)
258     {
259         return state == lldb::eStateExited;
260     }
261 
262     bool
263     ProcessIDIsValid ( ) const;
264 
265     void
266     Clear ( );
267 
268     lldb_private::Flags &
GetFlags()269     GetFlags ()
270     {
271         return m_flags;
272     }
273 
274     const lldb_private::Flags &
GetFlags()275     GetFlags () const
276     {
277         return m_flags;
278     }
279 
280     virtual bool
281     UpdateThreadList (lldb_private::ThreadList &old_thread_list,
282                       lldb_private::ThreadList &new_thread_list);
283 
284     lldb_private::Error
285     LaunchAndConnectToDebugserver (const lldb_private::ProcessInfo &process_info);
286 
287     void
288     KillDebugserverProcess ();
289 
290     void
291     BuildDynamicRegisterInfo (bool force);
292 
293     void
294     SetLastStopPacket (const StringExtractorGDBRemote &response);
295 
296     bool
297     ParsePythonTargetDefinition(const lldb_private::FileSpec &target_definition_fspec);
298 
299     bool
300     ParseRegisters(lldb_private::ScriptInterpreterObject *registers_array);
301 
302     //------------------------------------------------------------------
303     /// Broadcaster event bits definitions.
304     //------------------------------------------------------------------
305     enum
306     {
307         eBroadcastBitAsyncContinue                  = (1 << 0),
308         eBroadcastBitAsyncThreadShouldExit          = (1 << 1),
309         eBroadcastBitAsyncThreadDidExit             = (1 << 2)
310     };
311 
312     typedef enum AsyncThreadState
313     {
314         eAsyncThreadNotStarted,
315         eAsyncThreadRunning,
316         eAsyncThreadDone
317     } AsyncThreadState;
318 
319     lldb_private::Flags m_flags;            // Process specific flags (see eFlags enums)
320     GDBRemoteCommunicationClient m_gdb_comm;
321     lldb::pid_t m_debugserver_pid;
322     StringExtractorGDBRemote m_last_stop_packet;
323     lldb_private::Mutex m_last_stop_packet_mutex;
324     GDBRemoteDynamicRegisterInfo m_register_info;
325     lldb_private::Broadcaster m_async_broadcaster;
326     lldb::thread_t m_async_thread;
327     AsyncThreadState m_async_thread_state;
328     lldb_private::Mutex m_async_thread_state_mutex;
329     typedef std::vector<lldb::tid_t> tid_collection;
330     typedef std::vector< std::pair<lldb::tid_t,int> > tid_sig_collection;
331     typedef std::map<lldb::addr_t, lldb::addr_t> MMapMap;
332     tid_collection m_thread_ids; // Thread IDs for all threads. This list gets updated after stopping
333     tid_collection m_continue_c_tids;                  // 'c' for continue
334     tid_sig_collection m_continue_C_tids; // 'C' for continue with signal
335     tid_collection m_continue_s_tids;                  // 's' for step
336     tid_sig_collection m_continue_S_tids; // 'S' for step with signal
337     size_t m_max_memory_size;       // The maximum number of bytes to read/write when reading and writing memory
338     MMapMap m_addr_to_mmap_size;
339     lldb::BreakpointSP m_thread_create_bp_sp;
340     bool m_waiting_for_attach;
341     bool m_destroy_tried_resuming;
342     lldb::CommandObjectSP m_command_sp;
343     int64_t m_breakpoint_pc_offset;
344 
345     bool
346     StartAsyncThread ();
347 
348     void
349     StopAsyncThread ();
350 
351     static lldb::thread_result_t
352     AsyncThread (void *arg);
353 
354     static bool
355     MonitorDebugserverProcess (void *callback_baton,
356                                lldb::pid_t pid,
357                                bool exited,
358                                int signo,
359                                int exit_status);
360 
361     lldb::StateType
362     SetThreadStopInfo (StringExtractor& stop_packet);
363 
364     void
365     ClearThreadIDList ();
366 
367     bool
368     UpdateThreadIDList ();
369 
370     void
371     DidLaunchOrAttach ();
372 
373     lldb_private::Error
374     ConnectToDebugserver (const char *host_port);
375 
376     const char *
377     GetDispatchQueueNameForThread (lldb::addr_t thread_dispatch_qaddr,
378                                    std::string &dispatch_queue_name);
379 
380     lldb_private::DynamicLoader *
381     GetDynamicLoader ();
382 
383 private:
384     //------------------------------------------------------------------
385     // For ProcessGDBRemote only
386     //------------------------------------------------------------------
387     static bool
388     NewThreadNotifyBreakpointHit (void *baton,
389                          lldb_private::StoppointCallbackContext *context,
390                          lldb::user_id_t break_id,
391                          lldb::user_id_t break_loc_id);
392 
393     DISALLOW_COPY_AND_ASSIGN (ProcessGDBRemote);
394 
395 };
396 
397 #endif  // liblldb_ProcessGDBRemote_h_
398