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/Error.h"
23 #include "lldb/Core/InputReader.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 Process*
43     CreateInstance (lldb_private::Target& target, lldb_private::Listener &listener);
44 
45     static void
46     Initialize();
47 
48     static void
49     Terminate();
50 
51     static const char *
52     GetPluginNameStatic();
53 
54     static const char *
55     GetPluginDescriptionStatic();
56 
57     //------------------------------------------------------------------
58     // Constructors and Destructors
59     //------------------------------------------------------------------
60     ProcessGDBRemote(lldb_private::Target& target, lldb_private::Listener &listener);
61 
62     virtual
63     ~ProcessGDBRemote();
64 
65     //------------------------------------------------------------------
66     // Check if a given Process
67     //------------------------------------------------------------------
68     virtual bool
69     CanDebug (lldb_private::Target &target,
70               bool plugin_specified_by_name);
71 
72 //    virtual uint32_t
73 //    ListProcessesMatchingName (const char *name, lldb_private::StringList &matches, std::vector<lldb::pid_t> &pids);
74 
75     //------------------------------------------------------------------
76     // Creating a new process, or attaching to an existing one
77     //------------------------------------------------------------------
78     virtual lldb_private::Error
79     WillLaunch (lldb_private::Module* module);
80 
81     virtual lldb_private::Error
82     DoLaunch (lldb_private::Module *exe_module,
83               const lldb_private::ProcessLaunchInfo &launch_info);
84 
85     virtual void
86     DidLaunch ();
87 
88     virtual lldb_private::Error
89     WillAttachToProcessWithID (lldb::pid_t pid);
90 
91     virtual lldb_private::Error
92     WillAttachToProcessWithName (const char *process_name, bool wait_for_launch);
93 
94     virtual lldb_private::Error
95     DoConnectRemote (const char *remote_url);
96 
97     lldb_private::Error
98     WillLaunchOrAttach ();
99 
100     virtual lldb_private::Error
101     DoAttachToProcessWithID (lldb::pid_t pid);
102 
103     virtual lldb_private::Error
104     DoAttachToProcessWithName (const char *process_name, bool wait_for_launch);
105 
106     virtual void
107     DidAttach ();
108 
109     //------------------------------------------------------------------
110     // PluginInterface protocol
111     //------------------------------------------------------------------
112     virtual const char *
113     GetPluginName();
114 
115     virtual const char *
116     GetShortPluginName();
117 
118     virtual uint32_t
119     GetPluginVersion();
120 
121     //------------------------------------------------------------------
122     // Process Control
123     //------------------------------------------------------------------
124     virtual lldb_private::Error
125     WillResume ();
126 
127     virtual lldb_private::Error
128     DoResume ();
129 
130     virtual lldb_private::Error
131     DoHalt (bool &caused_stop);
132 
133     virtual lldb_private::Error
134     WillDetach ();
135 
136     virtual lldb_private::Error
137     DoDetach ();
138 
139     virtual lldb_private::Error
140     DoSignal (int signal);
141 
142     virtual lldb_private::Error
143     DoDestroy ();
144 
145     virtual void
146     RefreshStateAfterStop();
147 
148     //------------------------------------------------------------------
149     // Process Queries
150     //------------------------------------------------------------------
151     virtual bool
152     IsAlive ();
153 
154     virtual lldb::addr_t
155     GetImageInfoAddress();
156 
157     //------------------------------------------------------------------
158     // Process Memory
159     //------------------------------------------------------------------
160     virtual size_t
161     DoReadMemory (lldb::addr_t addr, void *buf, size_t size, lldb_private::Error &error);
162 
163     virtual size_t
164     DoWriteMemory (lldb::addr_t addr, const void *buf, size_t size, lldb_private::Error &error);
165 
166     virtual lldb::addr_t
167     DoAllocateMemory (size_t size, uint32_t permissions, lldb_private::Error &error);
168 
169     virtual lldb_private::Error
170     DoDeallocateMemory (lldb::addr_t ptr);
171 
172     //------------------------------------------------------------------
173     // Process STDIO
174     //------------------------------------------------------------------
175     virtual size_t
176     PutSTDIN (const char *buf, size_t buf_size, lldb_private::Error &error);
177 
178     //----------------------------------------------------------------------
179     // Process Breakpoints
180     //----------------------------------------------------------------------
181     virtual lldb_private::Error
182     EnableBreakpoint (lldb_private::BreakpointSite *bp_site);
183 
184     virtual lldb_private::Error
185     DisableBreakpoint (lldb_private::BreakpointSite *bp_site);
186 
187     //----------------------------------------------------------------------
188     // Process Watchpoints
189     //----------------------------------------------------------------------
190     virtual lldb_private::Error
191     EnableWatchpoint (lldb_private::Watchpoint *wp);
192 
193     virtual lldb_private::Error
194     DisableWatchpoint (lldb_private::Watchpoint *wp);
195 
196     virtual bool
197     StartNoticingNewThreads();
198 
199     virtual bool
200     StopNoticingNewThreads();
201 
202 protected:
203     friend class ThreadGDBRemote;
204     friend class GDBRemoteCommunicationClient;
205     friend class GDBRemoteRegisterContext;
206 
207     //----------------------------------------------------------------------
208     // Accessors
209     //----------------------------------------------------------------------
210     bool
211     IsRunning ( lldb::StateType state )
212     {
213         return    state == lldb::eStateRunning || IsStepping(state);
214     }
215 
216     bool
217     IsStepping ( lldb::StateType state)
218     {
219         return    state == lldb::eStateStepping;
220     }
221     bool
222     CanResume ( lldb::StateType state)
223     {
224         return state == lldb::eStateStopped;
225     }
226 
227     bool
228     HasExited (lldb::StateType state)
229     {
230         return state == lldb::eStateExited;
231     }
232 
233     bool
234     ProcessIDIsValid ( ) const;
235 
236     void
237     Clear ( );
238 
239     lldb_private::Flags &
240     GetFlags ()
241     {
242         return m_flags;
243     }
244 
245     const lldb_private::Flags &
246     GetFlags () const
247     {
248         return m_flags;
249     }
250 
251     uint32_t
252     UpdateThreadList (lldb_private::ThreadList &old_thread_list,
253                       lldb_private::ThreadList &new_thread_list);
254 
255     lldb_private::Error
256     StartDebugserverProcess (const char *debugserver_url);
257 
258     void
259     KillDebugserverProcess ();
260 
261     void
262     BuildDynamicRegisterInfo (bool force);
263 
264     GDBRemoteCommunicationClient &
265     GetGDBRemote()
266     {
267         return m_gdb_comm;
268     }
269 
270     //------------------------------------------------------------------
271     /// Broadcaster event bits definitions.
272     //------------------------------------------------------------------
273     enum
274     {
275         eBroadcastBitAsyncContinue                  = (1 << 0),
276         eBroadcastBitAsyncThreadShouldExit          = (1 << 1)
277     };
278 
279     lldb_private::Flags m_flags;            // Process specific flags (see eFlags enums)
280     GDBRemoteCommunicationClient m_gdb_comm;
281     lldb::pid_t m_debugserver_pid;
282     StringExtractorGDBRemote m_last_stop_packet;
283     GDBRemoteDynamicRegisterInfo m_register_info;
284     lldb_private::Broadcaster m_async_broadcaster;
285     lldb::thread_t m_async_thread;
286     typedef std::vector<lldb::tid_t> tid_collection;
287     typedef std::vector< std::pair<lldb::tid_t,int> > tid_sig_collection;
288     typedef std::map<lldb::addr_t, lldb::addr_t> MMapMap;
289     tid_collection m_continue_c_tids;                  // 'c' for continue
290     tid_sig_collection m_continue_C_tids; // 'C' for continue with signal
291     tid_collection m_continue_s_tids;                  // 's' for step
292     tid_sig_collection m_continue_S_tids; // 'S' for step with signal
293     lldb::addr_t m_dispatch_queue_offsets_addr;
294     size_t m_max_memory_size;       // The maximum number of bytes to read/write when reading and writing memory
295     bool m_waiting_for_attach;
296     std::vector<lldb::user_id_t>  m_thread_observation_bps;
297     MMapMap m_addr_to_mmap_size;
298     bool
299     StartAsyncThread ();
300 
301     void
302     StopAsyncThread ();
303 
304     static void *
305     AsyncThread (void *arg);
306 
307     static bool
308     MonitorDebugserverProcess (void *callback_baton,
309                                lldb::pid_t pid,
310                                bool exited,
311                                int signo,
312                                int exit_status);
313 
314     lldb::StateType
315     SetThreadStopInfo (StringExtractor& stop_packet);
316 
317     void
318     DidLaunchOrAttach ();
319 
320     lldb_private::Error
321     ConnectToDebugserver (const char *host_port);
322 
323     const char *
324     GetDispatchQueueNameForThread (lldb::addr_t thread_dispatch_qaddr,
325                                    std::string &dispatch_queue_name);
326 
327     static size_t
328     AttachInputReaderCallback (void *baton,
329                                lldb_private::InputReader *reader,
330                                lldb::InputReaderAction notification,
331                                const char *bytes,
332                                size_t bytes_len);
333 
334     lldb_private::Error
335     InterruptIfRunning (bool discard_thread_plans,
336                         bool catch_stop_event,
337                         lldb::EventSP &stop_event_sp);
338 
339 private:
340     //------------------------------------------------------------------
341     // For ProcessGDBRemote only
342     //------------------------------------------------------------------
343     static bool
344     NewThreadNotifyBreakpointHit (void *baton,
345                          lldb_private::StoppointCallbackContext *context,
346                          lldb::user_id_t break_id,
347                          lldb::user_id_t break_loc_id);
348 
349     DISALLOW_COPY_AND_ASSIGN (ProcessGDBRemote);
350 
351 };
352 
353 #endif  // liblldb_ProcessGDBRemote_h_
354