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