blob: 3e61ac6b840aea9fd12ad377ca1e602df4a2ea73 [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
30#include "GDBRemoteCommunication.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
69 CanDebug (lldb_private::Target &target);
70
Jim Ingham7508e732010-08-09 23:31:02 +000071 virtual uint32_t
72 ListProcessesMatchingName (const char *name, lldb_private::StringList &matches, std::vector<lldb::pid_t> &pids);
73
Chris Lattner24943d22010-06-08 16:52:24 +000074 //------------------------------------------------------------------
75 // Creating a new process, or attaching to an existing one
76 //------------------------------------------------------------------
77 virtual lldb_private::Error
78 WillLaunch (lldb_private::Module* module);
79
80 virtual lldb_private::Error
81 DoLaunch (lldb_private::Module* module,
82 char const *argv[], // Can be NULL
83 char const *envp[], // Can be NULL
Greg Clayton452bf612010-08-31 18:35:14 +000084 uint32_t flags,
Chris Lattner24943d22010-06-08 16:52:24 +000085 const char *stdin_path, // Can be NULL
Greg Claytonde915be2011-01-23 05:56:20 +000086 const char *stdout_path, // Can be NULL
87 const char *stderr_path, // Can be NULL
88 const char *working_dir); // Can be NULL
Chris Lattner24943d22010-06-08 16:52:24 +000089
90 virtual void
91 DidLaunch ();
92
93 virtual lldb_private::Error
Greg Clayton20d338f2010-11-18 05:57:03 +000094 WillAttachToProcessWithID (lldb::pid_t pid);
Chris Lattner24943d22010-06-08 16:52:24 +000095
96 virtual lldb_private::Error
Greg Clayton20d338f2010-11-18 05:57:03 +000097 WillAttachToProcessWithName (const char *process_name, bool wait_for_launch);
Chris Lattner24943d22010-06-08 16:52:24 +000098
Greg Claytone71e2582011-02-04 01:58:07 +000099 virtual lldb_private::Error
100 DoConnectRemote (const char *remote_url);
101
Chris Lattner24943d22010-06-08 16:52:24 +0000102 lldb_private::Error
103 WillLaunchOrAttach ();
104
105 virtual lldb_private::Error
Greg Clayton54e7afa2010-07-09 20:39:50 +0000106 DoAttachToProcessWithID (lldb::pid_t pid);
Chris Lattner24943d22010-06-08 16:52:24 +0000107
108 virtual lldb_private::Error
Greg Clayton54e7afa2010-07-09 20:39:50 +0000109 DoAttachToProcessWithName (const char *process_name, bool wait_for_launch);
Chris Lattner24943d22010-06-08 16:52:24 +0000110
111 virtual void
112 DidAttach ();
113
114 //------------------------------------------------------------------
115 // PluginInterface protocol
116 //------------------------------------------------------------------
117 virtual const char *
118 GetPluginName();
119
120 virtual const char *
121 GetShortPluginName();
122
123 virtual uint32_t
124 GetPluginVersion();
125
126 virtual void
127 GetPluginCommandHelp (const char *command, lldb_private::Stream *strm);
128
129 virtual lldb_private::Error
130 ExecutePluginCommand (lldb_private::Args &command, lldb_private::Stream *strm);
131
132 virtual lldb_private::Log *
133 EnablePluginLogging (lldb_private::Stream *strm, lldb_private::Args &command);
134
135 //------------------------------------------------------------------
136 // Process Control
137 //------------------------------------------------------------------
138 virtual lldb_private::Error
139 WillResume ();
140
141 virtual lldb_private::Error
142 DoResume ();
143
144 virtual lldb_private::Error
Jim Ingham3ae449a2010-11-17 02:32:00 +0000145 DoHalt (bool &caused_stop);
Chris Lattner24943d22010-06-08 16:52:24 +0000146
147 virtual lldb_private::Error
148 WillDetach ();
149
150 virtual lldb_private::Error
151 DoDetach ();
152
153 virtual lldb_private::Error
154 DoSignal (int signal);
155
156 virtual lldb_private::Error
157 DoDestroy ();
158
159 virtual void
160 RefreshStateAfterStop();
161
162 //------------------------------------------------------------------
163 // Process Queries
164 //------------------------------------------------------------------
165 virtual bool
166 IsAlive ();
167
168 virtual lldb::addr_t
169 GetImageInfoAddress();
170
171 //------------------------------------------------------------------
172 // Process Memory
173 //------------------------------------------------------------------
174 virtual size_t
175 DoReadMemory (lldb::addr_t addr, void *buf, size_t size, lldb_private::Error &error);
176
177 virtual size_t
178 DoWriteMemory (lldb::addr_t addr, const void *buf, size_t size, lldb_private::Error &error);
179
180 virtual lldb::addr_t
181 DoAllocateMemory (size_t size, uint32_t permissions, lldb_private::Error &error);
182
183 virtual lldb_private::Error
184 DoDeallocateMemory (lldb::addr_t ptr);
185
186 //------------------------------------------------------------------
187 // Process STDIO
188 //------------------------------------------------------------------
189 virtual size_t
190 GetSTDOUT (char *buf, size_t buf_size, lldb_private::Error &error);
191
192 virtual size_t
193 GetSTDERR (char *buf, size_t buf_size, lldb_private::Error &error);
194
195 virtual size_t
196 PutSTDIN (const char *buf, size_t buf_size, lldb_private::Error &error);
197
198 //----------------------------------------------------------------------
199 // Process Breakpoints
200 //----------------------------------------------------------------------
201 virtual size_t
202 GetSoftwareBreakpointTrapOpcode (lldb_private::BreakpointSite *bp_site);
203
204 //----------------------------------------------------------------------
205 // Process Breakpoints
206 //----------------------------------------------------------------------
207 virtual lldb_private::Error
208 EnableBreakpoint (lldb_private::BreakpointSite *bp_site);
209
210 virtual lldb_private::Error
211 DisableBreakpoint (lldb_private::BreakpointSite *bp_site);
212
213 //----------------------------------------------------------------------
214 // Process Watchpoints
215 //----------------------------------------------------------------------
216 virtual lldb_private::Error
217 EnableWatchpoint (lldb_private::WatchpointLocation *wp_loc);
218
219 virtual lldb_private::Error
220 DisableWatchpoint (lldb_private::WatchpointLocation *wp_loc);
221
Chris Lattner24943d22010-06-08 16:52:24 +0000222 virtual lldb_private::DynamicLoader *
223 GetDynamicLoader ();
Jim Ingham55e01d82011-01-22 01:33:44 +0000224
225 virtual bool
226 StartNoticingNewThreads();
227
228 virtual bool
229 StopNoticingNewThreads();
Chris Lattner24943d22010-06-08 16:52:24 +0000230
231protected:
232 friend class ThreadGDBRemote;
233 friend class GDBRemoteCommunication;
234 friend class GDBRemoteRegisterContext;
235
236 bool
237 SetCurrentGDBRemoteThread (int tid);
238
239 bool
240 SetCurrentGDBRemoteThreadForRun (int tid);
241
242 //----------------------------------------------------------------------
243 // Accessors
244 //----------------------------------------------------------------------
245 bool
246 IsRunning ( lldb::StateType state )
247 {
248 return state == lldb::eStateRunning || IsStepping(state);
249 }
250
251 bool
252 IsStepping ( lldb::StateType state)
253 {
254 return state == lldb::eStateStepping;
255 }
256 bool
257 CanResume ( lldb::StateType state)
258 {
259 return state == lldb::eStateStopped;
260 }
261
262 bool
263 HasExited (lldb::StateType state)
264 {
265 return state == lldb::eStateExited;
266 }
267
268 bool
269 ProcessIDIsValid ( ) const;
270
Caroline Tice861efb32010-11-16 05:07:41 +0000271// static void
272// STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len);
Chris Lattner24943d22010-06-08 16:52:24 +0000273
Caroline Tice861efb32010-11-16 05:07:41 +0000274// void
275// AppendSTDOUT (const char* s, size_t len);
Chris Lattner24943d22010-06-08 16:52:24 +0000276
Chris Lattner24943d22010-06-08 16:52:24 +0000277 void
278 Clear ( );
279
280 lldb_private::Flags &
281 GetFlags ()
282 {
283 return m_flags;
284 }
285
286 const lldb_private::Flags &
287 GetFlags () const
288 {
289 return m_flags;
290 }
291
292 uint32_t
293 UpdateThreadListIfNeeded ();
294
295 lldb_private::Error
Greg Clayton23cf0c72010-11-08 04:29:11 +0000296 StartDebugserverProcess (const char *debugserver_url, // The connection string to use in the spawned debugserver ("localhost:1234" or "/dev/tty...")
Chris Lattner24943d22010-06-08 16:52:24 +0000297 char const *inferior_argv[],
298 char const *inferior_envp[],
299 const char *stdin_path,
Greg Claytonde915be2011-01-23 05:56:20 +0000300 const char *stdout_path,
301 const char *stderr_path,
302 const char *working_dir,
Greg Clayton23cf0c72010-11-08 04:29:11 +0000303 bool launch_process, // Set to true if we are going to be launching a the process
304 lldb::pid_t attach_pid, // If inferior inferior_argv == NULL, then attach to this pid
305 const char *attach_pid_name, // Wait for the next process to launch whose basename matches "attach_wait_name"
306 bool wait_for_launch, // Wait for the process named "attach_wait_name" to launch
Caroline Ticebd666012010-12-03 18:46:09 +0000307 uint32_t launch_flags,
Chris Lattner24943d22010-06-08 16:52:24 +0000308 lldb_private::ArchSpec& arch_spec);
309
310 void
311 KillDebugserverProcess ();
312
313 void
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000314 BuildDynamicRegisterInfo (bool force);
Chris Lattner24943d22010-06-08 16:52:24 +0000315
316 GDBRemoteCommunication &
317 GetGDBRemote()
318 {
319 return m_gdb_comm;
320 }
321
322 //------------------------------------------------------------------
323 /// Broadcaster event bits definitions.
324 //------------------------------------------------------------------
325 enum
326 {
327 eBroadcastBitAsyncContinue = (1 << 0),
328 eBroadcastBitAsyncThreadShouldExit = (1 << 1)
329 };
330
331
332 std::auto_ptr<lldb_private::DynamicLoader> m_dynamic_loader_ap;
333 lldb_private::Flags m_flags; // Process specific flags (see eFlags enums)
Chris Lattner24943d22010-06-08 16:52:24 +0000334 lldb_private::Mutex m_stdio_mutex; // Multithreaded protection for stdio
Chris Lattner24943d22010-06-08 16:52:24 +0000335 GDBRemoteCommunication m_gdb_comm;
336 lldb::pid_t m_debugserver_pid;
Greg Clayton75ccf502010-08-21 02:22:51 +0000337 lldb::thread_t m_debugserver_thread;
Chris Lattner24943d22010-06-08 16:52:24 +0000338 StringExtractor m_last_stop_packet;
339 GDBRemoteDynamicRegisterInfo m_register_info;
340 lldb_private::Broadcaster m_async_broadcaster;
341 lldb::thread_t m_async_thread;
342 // Current GDB remote state. Any members added here need to be reset to
343 // proper default values in ResetGDBRemoteState ().
344 lldb::tid_t m_curr_tid; // Current gdb remote protocol thread index for all other operations
345 lldb::tid_t m_curr_tid_run; // Current gdb remote protocol thread index for continue, step, etc
346 uint32_t m_z0_supported:1; // Set to non-zero if Z0 and z0 packets are supported
Greg Claytonc1f45872011-02-12 06:28:37 +0000347 typedef std::vector<lldb::tid_t> tid_collection;
348 typedef std::vector< std::pair<lldb::tid_t,int> > tid_sig_collection;
349 tid_collection m_continue_c_tids; // 'c' for continue
350 tid_sig_collection m_continue_C_tids; // 'C' for continue with signal
351 tid_collection m_continue_s_tids; // 's' for step
352 tid_sig_collection m_continue_S_tids; // 'S' for step with signal
Chris Lattner24943d22010-06-08 16:52:24 +0000353 lldb::addr_t m_dispatch_queue_offsets_addr;
354 uint32_t m_packet_timeout;
355 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 +0000356 bool m_waiting_for_attach;
Jim Ingham7508e732010-08-09 23:31:02 +0000357 bool m_local_debugserver; // Is the debugserver process we are talking to local or on another machine.
Jim Ingham55e01d82011-01-22 01:33:44 +0000358 std::vector<lldb::user_id_t> m_thread_observation_bps;
Chris Lattner24943d22010-06-08 16:52:24 +0000359
360 void
361 ResetGDBRemoteState ();
362
363 bool
364 StartAsyncThread ();
365
366 void
367 StopAsyncThread ();
368
369 static void *
370 AsyncThread (void *arg);
371
372 static bool
373 MonitorDebugserverProcess (void *callback_baton,
374 lldb::pid_t pid,
375 int signo, // Zero for no signal
376 int exit_status); // Exit value of process if signal is zero
377
378 lldb::StateType
379 SetThreadStopInfo (StringExtractor& stop_packet);
380
381 void
382 DidLaunchOrAttach ();
383
384 lldb_private::Error
385 ConnectToDebugserver (const char *host_port);
386
387 const char *
388 GetDispatchQueueNameForThread (lldb::addr_t thread_dispatch_qaddr,
389 std::string &dispatch_queue_name);
390
391 static size_t
392 AttachInputReaderCallback (void *baton,
393 lldb_private::InputReader *reader,
394 lldb::InputReaderAction notification,
395 const char *bytes,
396 size_t bytes_len);
397
Greg Clayton72e1c782011-01-22 23:43:18 +0000398 lldb_private::Error
399 InterruptIfRunning (bool discard_thread_plans,
400 bool catch_stop_event,
Greg Clayton72e1c782011-01-22 23:43:18 +0000401 lldb::EventSP &stop_event_sp);
402
Chris Lattner24943d22010-06-08 16:52:24 +0000403private:
404 //------------------------------------------------------------------
405 // For ProcessGDBRemote only
406 //------------------------------------------------------------------
Jim Ingham55e01d82011-01-22 01:33:44 +0000407 static bool
408 NewThreadNotifyBreakpointHit (void *baton,
409 lldb_private::StoppointCallbackContext *context,
410 lldb::user_id_t break_id,
411 lldb::user_id_t break_loc_id);
412
Chris Lattner24943d22010-06-08 16:52:24 +0000413 DISALLOW_COPY_AND_ASSIGN (ProcessGDBRemote);
414
Chris Lattner24943d22010-06-08 16:52:24 +0000415};
416
417#endif // liblldb_ProcessGDBRemote_h_