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     GetMemoryRegionInfo (lldb::addr_t load_addr,
171                          lldb_private::MemoryRegionInfo &region_info);
172 
173     virtual lldb_private::Error
174     DoDeallocateMemory (lldb::addr_t ptr);
175 
176     //------------------------------------------------------------------
177     // Process STDIO
178     //------------------------------------------------------------------
179     virtual size_t
180     PutSTDIN (const char *buf, size_t buf_size, lldb_private::Error &error);
181 
182     //----------------------------------------------------------------------
183     // Process Breakpoints
184     //----------------------------------------------------------------------
185     virtual lldb_private::Error
186     EnableBreakpoint (lldb_private::BreakpointSite *bp_site);
187 
188     virtual lldb_private::Error
189     DisableBreakpoint (lldb_private::BreakpointSite *bp_site);
190 
191     //----------------------------------------------------------------------
192     // Process Watchpoints
193     //----------------------------------------------------------------------
194     virtual lldb_private::Error
195     EnableWatchpoint (lldb_private::Watchpoint *wp);
196 
197     virtual lldb_private::Error
198     DisableWatchpoint (lldb_private::Watchpoint *wp);
199 
200     virtual bool
201     StartNoticingNewThreads();
202 
203     virtual bool
204     StopNoticingNewThreads();
205 
206 protected:
207     friend class ThreadGDBRemote;
208     friend class GDBRemoteCommunicationClient;
209     friend class GDBRemoteRegisterContext;
210 
211     //----------------------------------------------------------------------
212     // Accessors
213     //----------------------------------------------------------------------
214     bool
215     IsRunning ( lldb::StateType state )
216     {
217         return    state == lldb::eStateRunning || IsStepping(state);
218     }
219 
220     bool
221     IsStepping ( lldb::StateType state)
222     {
223         return    state == lldb::eStateStepping;
224     }
225     bool
226     CanResume ( lldb::StateType state)
227     {
228         return state == lldb::eStateStopped;
229     }
230 
231     bool
232     HasExited (lldb::StateType state)
233     {
234         return state == lldb::eStateExited;
235     }
236 
237     bool
238     ProcessIDIsValid ( ) const;
239 
240     void
241     Clear ( );
242 
243     lldb_private::Flags &
244     GetFlags ()
245     {
246         return m_flags;
247     }
248 
249     const lldb_private::Flags &
250     GetFlags () const
251     {
252         return m_flags;
253     }
254 
255     uint32_t
256     UpdateThreadList (lldb_private::ThreadList &old_thread_list,
257                       lldb_private::ThreadList &new_thread_list);
258 
259     lldb_private::Error
260     StartDebugserverProcess (const char *debugserver_url);
261 
262     void
263     KillDebugserverProcess ();
264 
265     void
266     BuildDynamicRegisterInfo (bool force);
267 
268     GDBRemoteCommunicationClient &
269     GetGDBRemote()
270     {
271         return m_gdb_comm;
272     }
273 
274     void
275     SetLastStopPacket (const StringExtractorGDBRemote &response)
276     {
277         lldb_private::Mutex::Locker locker (m_last_stop_packet_mutex);
278         m_last_stop_packet = response;
279     }
280     //------------------------------------------------------------------
281     /// Broadcaster event bits definitions.
282     //------------------------------------------------------------------
283     enum
284     {
285         eBroadcastBitAsyncContinue                  = (1 << 0),
286         eBroadcastBitAsyncThreadShouldExit          = (1 << 1)
287     };
288 
289     lldb_private::Flags m_flags;            // Process specific flags (see eFlags enums)
290     GDBRemoteCommunicationClient m_gdb_comm;
291     lldb::pid_t m_debugserver_pid;
292     StringExtractorGDBRemote m_last_stop_packet;
293     lldb_private::Mutex m_last_stop_packet_mutex;
294     GDBRemoteDynamicRegisterInfo m_register_info;
295     lldb_private::Broadcaster m_async_broadcaster;
296     lldb::thread_t m_async_thread;
297     typedef std::vector<lldb::tid_t> tid_collection;
298     typedef std::vector< std::pair<lldb::tid_t,int> > tid_sig_collection;
299     typedef std::map<lldb::addr_t, lldb::addr_t> MMapMap;
300     tid_collection m_continue_c_tids;                  // 'c' for continue
301     tid_sig_collection m_continue_C_tids; // 'C' for continue with signal
302     tid_collection m_continue_s_tids;                  // 's' for step
303     tid_sig_collection m_continue_S_tids; // 'S' for step with signal
304     lldb::addr_t m_dispatch_queue_offsets_addr;
305     size_t m_max_memory_size;       // The maximum number of bytes to read/write when reading and writing memory
306     bool m_waiting_for_attach;
307     std::vector<lldb::user_id_t>  m_thread_observation_bps;
308     MMapMap m_addr_to_mmap_size;
309     bool
310     StartAsyncThread ();
311 
312     void
313     StopAsyncThread ();
314 
315     static void *
316     AsyncThread (void *arg);
317 
318     static bool
319     MonitorDebugserverProcess (void *callback_baton,
320                                lldb::pid_t pid,
321                                bool exited,
322                                int signo,
323                                int exit_status);
324 
325     lldb::StateType
326     SetThreadStopInfo (StringExtractor& stop_packet);
327 
328     void
329     DidLaunchOrAttach ();
330 
331     lldb_private::Error
332     ConnectToDebugserver (const char *host_port);
333 
334     const char *
335     GetDispatchQueueNameForThread (lldb::addr_t thread_dispatch_qaddr,
336                                    std::string &dispatch_queue_name);
337 
338     static size_t
339     AttachInputReaderCallback (void *baton,
340                                lldb_private::InputReader *reader,
341                                lldb::InputReaderAction notification,
342                                const char *bytes,
343                                size_t bytes_len);
344 
345     lldb_private::Error
346     InterruptIfRunning (bool discard_thread_plans,
347                         bool catch_stop_event,
348                         lldb::EventSP &stop_event_sp);
349 
350 private:
351     //------------------------------------------------------------------
352     // For ProcessGDBRemote only
353     //------------------------------------------------------------------
354     static bool
355     NewThreadNotifyBreakpointHit (void *baton,
356                          lldb_private::StoppointCallbackContext *context,
357                          lldb::user_id_t break_id,
358                          lldb::user_id_t break_loc_id);
359 
360     DISALLOW_COPY_AND_ASSIGN (ProcessGDBRemote);
361 
362 };
363 
364 #endif  // liblldb_ProcessGDBRemote_h_
365