blob: 4f4f7fb87f24cc284bc544c6e0815983fef1d799 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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>
Greg Claytonc1f45872011-02-12 06:28:37 +000017#include <vector>
Chris Lattner24943d22010-06-08 16:52:24 +000018
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"
Jim Ingham7508e732010-08-09 23:31:02 +000025#include "lldb/Core/StringList.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026#include "lldb/Core/ThreadSafeValue.h"
27#include "lldb/Target/Process.h"
28#include "lldb/Target/Thread.h"
29
Greg Clayton61d043b2011-03-22 04:00:09 +000030#include "GDBRemoteCommunicationClient.h"
Greg Clayton54e7afa2010-07-09 20:39:50 +000031#include "Utility/StringExtractor.h"
Chris Lattner24943d22010-06-08 16:52:24 +000032#include "GDBRemoteRegisterContext.h"
Chris Lattner24943d22010-06-08 16:52:24 +000033
34class ThreadGDBRemote;
35
36class ProcessGDBRemote : public lldb_private::Process
37{
38public:
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
Greg Clayton8d2ea282011-07-17 20:36:25 +000069 CanDebug (lldb_private::Target &target,
70 bool plugin_specified_by_name);
Chris Lattner24943d22010-06-08 16:52:24 +000071
Greg Claytone4b9c1f2011-03-08 22:40:15 +000072// virtual uint32_t
73// ListProcessesMatchingName (const char *name, lldb_private::StringList &matches, std::vector<lldb::pid_t> &pids);
Jim Ingham7508e732010-08-09 23:31:02 +000074
Chris Lattner24943d22010-06-08 16:52:24 +000075 //------------------------------------------------------------------
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
Greg Clayton452bf612010-08-31 18:35:14 +000085 uint32_t flags,
Chris Lattner24943d22010-06-08 16:52:24 +000086 const char *stdin_path, // Can be NULL
Greg Claytonde915be2011-01-23 05:56:20 +000087 const char *stdout_path, // Can be NULL
88 const char *stderr_path, // Can be NULL
89 const char *working_dir); // Can be NULL
Chris Lattner24943d22010-06-08 16:52:24 +000090
91 virtual void
92 DidLaunch ();
93
94 virtual lldb_private::Error
Greg Clayton20d338f2010-11-18 05:57:03 +000095 WillAttachToProcessWithID (lldb::pid_t pid);
Chris Lattner24943d22010-06-08 16:52:24 +000096
97 virtual lldb_private::Error
Greg Clayton20d338f2010-11-18 05:57:03 +000098 WillAttachToProcessWithName (const char *process_name, bool wait_for_launch);
Chris Lattner24943d22010-06-08 16:52:24 +000099
Greg Claytone71e2582011-02-04 01:58:07 +0000100 virtual lldb_private::Error
101 DoConnectRemote (const char *remote_url);
102
Chris Lattner24943d22010-06-08 16:52:24 +0000103 lldb_private::Error
104 WillLaunchOrAttach ();
105
106 virtual lldb_private::Error
Greg Clayton54e7afa2010-07-09 20:39:50 +0000107 DoAttachToProcessWithID (lldb::pid_t pid);
Chris Lattner24943d22010-06-08 16:52:24 +0000108
109 virtual lldb_private::Error
Greg Clayton54e7afa2010-07-09 20:39:50 +0000110 DoAttachToProcessWithName (const char *process_name, bool wait_for_launch);
Chris Lattner24943d22010-06-08 16:52:24 +0000111
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
Chris Lattner24943d22010-06-08 16:52:24 +0000127 //------------------------------------------------------------------
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
Jim Ingham3ae449a2010-11-17 02:32:00 +0000137 DoHalt (bool &caused_stop);
Chris Lattner24943d22010-06-08 16:52:24 +0000138
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 //----------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +0000193 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
Jim Ingham55e01d82011-01-22 01:33:44 +0000208 virtual bool
209 StartNoticingNewThreads();
210
211 virtual bool
212 StopNoticingNewThreads();
Chris Lattner24943d22010-06-08 16:52:24 +0000213
214protected:
215 friend class ThreadGDBRemote;
Greg Clayton61d043b2011-03-22 04:00:09 +0000216 friend class GDBRemoteCommunicationClient;
Chris Lattner24943d22010-06-08 16:52:24 +0000217 friend class GDBRemoteRegisterContext;
218
Chris Lattner24943d22010-06-08 16:52:24 +0000219 //----------------------------------------------------------------------
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
Caroline Tice861efb32010-11-16 05:07:41 +0000248// static void
249// STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len);
Chris Lattner24943d22010-06-08 16:52:24 +0000250
Caroline Tice861efb32010-11-16 05:07:41 +0000251// void
252// AppendSTDOUT (const char* s, size_t len);
Chris Lattner24943d22010-06-08 16:52:24 +0000253
Chris Lattner24943d22010-06-08 16:52:24 +0000254 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 UpdateThreadListIfNeeded ();
271
272 lldb_private::Error
Greg Claytonb72d0f02011-04-12 05:54:46 +0000273 StartDebugserverProcess (const char *debugserver_url);
Chris Lattner24943d22010-06-08 16:52:24 +0000274
275 void
276 KillDebugserverProcess ();
277
278 void
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000279 BuildDynamicRegisterInfo (bool force);
Chris Lattner24943d22010-06-08 16:52:24 +0000280
Greg Clayton61d043b2011-03-22 04:00:09 +0000281 GDBRemoteCommunicationClient &
Chris Lattner24943d22010-06-08 16:52:24 +0000282 GetGDBRemote()
283 {
284 return m_gdb_comm;
285 }
286
287 //------------------------------------------------------------------
288 /// Broadcaster event bits definitions.
289 //------------------------------------------------------------------
290 enum
291 {
292 eBroadcastBitAsyncContinue = (1 << 0),
293 eBroadcastBitAsyncThreadShouldExit = (1 << 1)
294 };
295
Chris Lattner24943d22010-06-08 16:52:24 +0000296 lldb_private::Flags m_flags; // Process specific flags (see eFlags enums)
Chris Lattner24943d22010-06-08 16:52:24 +0000297 lldb_private::Mutex m_stdio_mutex; // Multithreaded protection for stdio
Greg Clayton61d043b2011-03-22 04:00:09 +0000298 GDBRemoteCommunicationClient m_gdb_comm;
Chris Lattner24943d22010-06-08 16:52:24 +0000299 lldb::pid_t m_debugserver_pid;
Greg Clayton75ccf502010-08-21 02:22:51 +0000300 lldb::thread_t m_debugserver_thread;
Greg Clayton261a18b2011-06-02 22:22:38 +0000301 StringExtractorGDBRemote m_last_stop_packet;
Chris Lattner24943d22010-06-08 16:52:24 +0000302 GDBRemoteDynamicRegisterInfo m_register_info;
303 lldb_private::Broadcaster m_async_broadcaster;
304 lldb::thread_t m_async_thread;
Greg Claytonc1f45872011-02-12 06:28:37 +0000305 typedef std::vector<lldb::tid_t> tid_collection;
306 typedef std::vector< std::pair<lldb::tid_t,int> > tid_sig_collection;
Greg Clayton2f085c62011-05-15 01:25:55 +0000307 typedef std::map<lldb::addr_t, lldb::addr_t> MMapMap;
Greg Claytonc1f45872011-02-12 06:28:37 +0000308 tid_collection m_continue_c_tids; // 'c' for continue
309 tid_sig_collection m_continue_C_tids; // 'C' for continue with signal
310 tid_collection m_continue_s_tids; // 's' for step
311 tid_sig_collection m_continue_S_tids; // 'S' for step with signal
Chris Lattner24943d22010-06-08 16:52:24 +0000312 lldb::addr_t m_dispatch_queue_offsets_addr;
Chris Lattner24943d22010-06-08 16:52:24 +0000313 size_t m_max_memory_size; // The maximum number of bytes to read/write when reading and writing memory
Chris Lattner24943d22010-06-08 16:52:24 +0000314 bool m_waiting_for_attach;
Jim Ingham55e01d82011-01-22 01:33:44 +0000315 std::vector<lldb::user_id_t> m_thread_observation_bps;
Greg Clayton2f085c62011-05-15 01:25:55 +0000316 MMapMap m_addr_to_mmap_size;
Chris Lattner24943d22010-06-08 16:52:24 +0000317 bool
318 StartAsyncThread ();
319
320 void
321 StopAsyncThread ();
322
323 static void *
324 AsyncThread (void *arg);
325
326 static bool
327 MonitorDebugserverProcess (void *callback_baton,
328 lldb::pid_t pid,
329 int signo, // Zero for no signal
330 int exit_status); // Exit value of process if signal is zero
331
332 lldb::StateType
333 SetThreadStopInfo (StringExtractor& stop_packet);
334
335 void
336 DidLaunchOrAttach ();
337
338 lldb_private::Error
339 ConnectToDebugserver (const char *host_port);
340
341 const char *
342 GetDispatchQueueNameForThread (lldb::addr_t thread_dispatch_qaddr,
343 std::string &dispatch_queue_name);
344
345 static size_t
346 AttachInputReaderCallback (void *baton,
347 lldb_private::InputReader *reader,
348 lldb::InputReaderAction notification,
349 const char *bytes,
350 size_t bytes_len);
351
Greg Clayton72e1c782011-01-22 23:43:18 +0000352 lldb_private::Error
353 InterruptIfRunning (bool discard_thread_plans,
354 bool catch_stop_event,
Greg Clayton72e1c782011-01-22 23:43:18 +0000355 lldb::EventSP &stop_event_sp);
356
Chris Lattner24943d22010-06-08 16:52:24 +0000357private:
358 //------------------------------------------------------------------
359 // For ProcessGDBRemote only
360 //------------------------------------------------------------------
Jim Ingham55e01d82011-01-22 01:33:44 +0000361 static bool
362 NewThreadNotifyBreakpointHit (void *baton,
363 lldb_private::StoppointCallbackContext *context,
364 lldb::user_id_t break_id,
365 lldb::user_id_t break_loc_id);
366
Chris Lattner24943d22010-06-08 16:52:24 +0000367 DISALLOW_COPY_AND_ASSIGN (ProcessGDBRemote);
368
Chris Lattner24943d22010-06-08 16:52:24 +0000369};
370
371#endif // liblldb_ProcessGDBRemote_h_