blob: d8a2cebbd33e75ba779e2989bf51a0e139f73100 [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>
17
18// Other libraries and framework includes
19#include "lldb/Core/ArchSpec.h"
20#include "lldb/Core/Broadcaster.h"
21#include "lldb/Core/Error.h"
22#include "lldb/Core/InputReader.h"
23#include "lldb/Core/StreamString.h"
Jim Ingham7508e732010-08-09 23:31:02 +000024#include "lldb/Core/StringList.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Core/ThreadSafeValue.h"
26#include "lldb/Target/Process.h"
27#include "lldb/Target/Thread.h"
28
29#include "GDBRemoteCommunication.h"
Greg Clayton54e7afa2010-07-09 20:39:50 +000030#include "Utility/StringExtractor.h"
Chris Lattner24943d22010-06-08 16:52:24 +000031#include "GDBRemoteRegisterContext.h"
Chris Lattner24943d22010-06-08 16:52:24 +000032
33class ThreadGDBRemote;
34
35class ProcessGDBRemote : public lldb_private::Process
36{
37public:
38 //------------------------------------------------------------------
39 // Constructors and Destructors
40 //------------------------------------------------------------------
41 static Process*
42 CreateInstance (lldb_private::Target& target, lldb_private::Listener &listener);
43
44 static void
45 Initialize();
46
47 static void
48 Terminate();
49
50 static const char *
51 GetPluginNameStatic();
52
53 static const char *
54 GetPluginDescriptionStatic();
55
56 //------------------------------------------------------------------
57 // Constructors and Destructors
58 //------------------------------------------------------------------
59 ProcessGDBRemote(lldb_private::Target& target, lldb_private::Listener &listener);
60
61 virtual
62 ~ProcessGDBRemote();
63
64 //------------------------------------------------------------------
65 // Check if a given Process
66 //------------------------------------------------------------------
67 virtual bool
68 CanDebug (lldb_private::Target &target);
69
Jim Ingham7508e732010-08-09 23:31:02 +000070 virtual uint32_t
71 ListProcessesMatchingName (const char *name, lldb_private::StringList &matches, std::vector<lldb::pid_t> &pids);
72
Chris Lattner24943d22010-06-08 16:52:24 +000073 //------------------------------------------------------------------
74 // Creating a new process, or attaching to an existing one
75 //------------------------------------------------------------------
76 virtual lldb_private::Error
77 WillLaunch (lldb_private::Module* module);
78
79 virtual lldb_private::Error
80 DoLaunch (lldb_private::Module* module,
81 char const *argv[], // Can be NULL
82 char const *envp[], // Can be NULL
Greg Clayton452bf612010-08-31 18:35:14 +000083 uint32_t flags,
Chris Lattner24943d22010-06-08 16:52:24 +000084 const char *stdin_path, // Can be NULL
85 const char *stdout_path, // Can be NULL
86 const char *stderr_path); // Can be NULL
87
88 virtual void
89 DidLaunch ();
90
91 virtual lldb_private::Error
Greg Clayton20d338f2010-11-18 05:57:03 +000092 WillAttachToProcessWithID (lldb::pid_t pid);
Chris Lattner24943d22010-06-08 16:52:24 +000093
94 virtual lldb_private::Error
Greg Clayton20d338f2010-11-18 05:57:03 +000095 WillAttachToProcessWithName (const char *process_name, bool wait_for_launch);
Chris Lattner24943d22010-06-08 16:52:24 +000096
97 lldb_private::Error
98 WillLaunchOrAttach ();
99
100 virtual lldb_private::Error
Greg Clayton54e7afa2010-07-09 20:39:50 +0000101 DoAttachToProcessWithID (lldb::pid_t pid);
Chris Lattner24943d22010-06-08 16:52:24 +0000102
103 virtual lldb_private::Error
Greg Clayton54e7afa2010-07-09 20:39:50 +0000104 DoAttachToProcessWithName (const char *process_name, bool wait_for_launch);
Chris Lattner24943d22010-06-08 16:52:24 +0000105
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 virtual void
122 GetPluginCommandHelp (const char *command, lldb_private::Stream *strm);
123
124 virtual lldb_private::Error
125 ExecutePluginCommand (lldb_private::Args &command, lldb_private::Stream *strm);
126
127 virtual lldb_private::Log *
128 EnablePluginLogging (lldb_private::Stream *strm, lldb_private::Args &command);
129
130 //------------------------------------------------------------------
131 // Process Control
132 //------------------------------------------------------------------
133 virtual lldb_private::Error
134 WillResume ();
135
136 virtual lldb_private::Error
137 DoResume ();
138
139 virtual lldb_private::Error
Jim Ingham3ae449a2010-11-17 02:32:00 +0000140 DoHalt (bool &caused_stop);
Chris Lattner24943d22010-06-08 16:52:24 +0000141
142 virtual lldb_private::Error
143 WillDetach ();
144
145 virtual lldb_private::Error
146 DoDetach ();
147
148 virtual lldb_private::Error
149 DoSignal (int signal);
150
151 virtual lldb_private::Error
Greg Clayton72e1c782011-01-22 23:43:18 +0000152 WillDestroy ();
153
154 virtual lldb_private::Error
Chris Lattner24943d22010-06-08 16:52:24 +0000155 DoDestroy ();
156
157 virtual void
158 RefreshStateAfterStop();
159
160 //------------------------------------------------------------------
161 // Process Queries
162 //------------------------------------------------------------------
163 virtual bool
164 IsAlive ();
165
166 virtual lldb::addr_t
167 GetImageInfoAddress();
168
169 //------------------------------------------------------------------
170 // Process Memory
171 //------------------------------------------------------------------
172 virtual size_t
173 DoReadMemory (lldb::addr_t addr, void *buf, size_t size, lldb_private::Error &error);
174
175 virtual size_t
176 DoWriteMemory (lldb::addr_t addr, const void *buf, size_t size, lldb_private::Error &error);
177
178 virtual lldb::addr_t
179 DoAllocateMemory (size_t size, uint32_t permissions, lldb_private::Error &error);
180
181 virtual lldb_private::Error
182 DoDeallocateMemory (lldb::addr_t ptr);
183
184 //------------------------------------------------------------------
185 // Process STDIO
186 //------------------------------------------------------------------
187 virtual size_t
188 GetSTDOUT (char *buf, size_t buf_size, lldb_private::Error &error);
189
190 virtual size_t
191 GetSTDERR (char *buf, size_t buf_size, lldb_private::Error &error);
192
193 virtual size_t
194 PutSTDIN (const char *buf, size_t buf_size, lldb_private::Error &error);
195
196 //----------------------------------------------------------------------
197 // Process Breakpoints
198 //----------------------------------------------------------------------
199 virtual size_t
200 GetSoftwareBreakpointTrapOpcode (lldb_private::BreakpointSite *bp_site);
201
202 //----------------------------------------------------------------------
203 // Process Breakpoints
204 //----------------------------------------------------------------------
205 virtual lldb_private::Error
206 EnableBreakpoint (lldb_private::BreakpointSite *bp_site);
207
208 virtual lldb_private::Error
209 DisableBreakpoint (lldb_private::BreakpointSite *bp_site);
210
211 //----------------------------------------------------------------------
212 // Process Watchpoints
213 //----------------------------------------------------------------------
214 virtual lldb_private::Error
215 EnableWatchpoint (lldb_private::WatchpointLocation *wp_loc);
216
217 virtual lldb_private::Error
218 DisableWatchpoint (lldb_private::WatchpointLocation *wp_loc);
219
Chris Lattner24943d22010-06-08 16:52:24 +0000220 virtual lldb_private::DynamicLoader *
221 GetDynamicLoader ();
Jim Ingham55e01d82011-01-22 01:33:44 +0000222
223 virtual bool
224 StartNoticingNewThreads();
225
226 virtual bool
227 StopNoticingNewThreads();
Chris Lattner24943d22010-06-08 16:52:24 +0000228
229protected:
230 friend class ThreadGDBRemote;
231 friend class GDBRemoteCommunication;
232 friend class GDBRemoteRegisterContext;
233
234 bool
235 SetCurrentGDBRemoteThread (int tid);
236
237 bool
238 SetCurrentGDBRemoteThreadForRun (int tid);
239
240 //----------------------------------------------------------------------
241 // Accessors
242 //----------------------------------------------------------------------
243 bool
244 IsRunning ( lldb::StateType state )
245 {
246 return state == lldb::eStateRunning || IsStepping(state);
247 }
248
249 bool
250 IsStepping ( lldb::StateType state)
251 {
252 return state == lldb::eStateStepping;
253 }
254 bool
255 CanResume ( lldb::StateType state)
256 {
257 return state == lldb::eStateStopped;
258 }
259
260 bool
261 HasExited (lldb::StateType state)
262 {
263 return state == lldb::eStateExited;
264 }
265
266 bool
267 ProcessIDIsValid ( ) const;
268
Caroline Tice861efb32010-11-16 05:07:41 +0000269// static void
270// STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len);
Chris Lattner24943d22010-06-08 16:52:24 +0000271
Caroline Tice861efb32010-11-16 05:07:41 +0000272// void
273// AppendSTDOUT (const char* s, size_t len);
Chris Lattner24943d22010-06-08 16:52:24 +0000274
Chris Lattner24943d22010-06-08 16:52:24 +0000275 void
276 Clear ( );
277
278 lldb_private::Flags &
279 GetFlags ()
280 {
281 return m_flags;
282 }
283
284 const lldb_private::Flags &
285 GetFlags () const
286 {
287 return m_flags;
288 }
289
290 uint32_t
291 UpdateThreadListIfNeeded ();
292
293 lldb_private::Error
Greg Clayton23cf0c72010-11-08 04:29:11 +0000294 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 +0000295 char const *inferior_argv[],
296 char const *inferior_envp[],
297 const char *stdin_path,
Greg Clayton23cf0c72010-11-08 04:29:11 +0000298 bool launch_process, // Set to true if we are going to be launching a the process
299 lldb::pid_t attach_pid, // If inferior inferior_argv == NULL, then attach to this pid
300 const char *attach_pid_name, // Wait for the next process to launch whose basename matches "attach_wait_name"
301 bool wait_for_launch, // Wait for the process named "attach_wait_name" to launch
Caroline Ticebd666012010-12-03 18:46:09 +0000302 uint32_t launch_flags,
Chris Lattner24943d22010-06-08 16:52:24 +0000303 lldb_private::ArchSpec& arch_spec);
304
305 void
306 KillDebugserverProcess ();
307
308 void
309 BuildDynamicRegisterInfo ();
310
311 GDBRemoteCommunication &
312 GetGDBRemote()
313 {
314 return m_gdb_comm;
315 }
316
317 //------------------------------------------------------------------
318 /// Broadcaster event bits definitions.
319 //------------------------------------------------------------------
320 enum
321 {
322 eBroadcastBitAsyncContinue = (1 << 0),
323 eBroadcastBitAsyncThreadShouldExit = (1 << 1)
324 };
325
326
327 std::auto_ptr<lldb_private::DynamicLoader> m_dynamic_loader_ap;
328 lldb_private::Flags m_flags; // Process specific flags (see eFlags enums)
Chris Lattner24943d22010-06-08 16:52:24 +0000329 lldb_private::Mutex m_stdio_mutex; // Multithreaded protection for stdio
Chris Lattner24943d22010-06-08 16:52:24 +0000330 GDBRemoteCommunication m_gdb_comm;
331 lldb::pid_t m_debugserver_pid;
Greg Clayton75ccf502010-08-21 02:22:51 +0000332 lldb::thread_t m_debugserver_thread;
Chris Lattner24943d22010-06-08 16:52:24 +0000333 StringExtractor m_last_stop_packet;
334 GDBRemoteDynamicRegisterInfo m_register_info;
335 lldb_private::Broadcaster m_async_broadcaster;
336 lldb::thread_t m_async_thread;
337 // Current GDB remote state. Any members added here need to be reset to
338 // proper default values in ResetGDBRemoteState ().
339 lldb::tid_t m_curr_tid; // Current gdb remote protocol thread index for all other operations
340 lldb::tid_t m_curr_tid_run; // Current gdb remote protocol thread index for continue, step, etc
341 uint32_t m_z0_supported:1; // Set to non-zero if Z0 and z0 packets are supported
342 lldb_private::StreamString m_continue_packet;
343 lldb::addr_t m_dispatch_queue_offsets_addr;
344 uint32_t m_packet_timeout;
345 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 +0000346 bool m_waiting_for_attach;
Jim Ingham7508e732010-08-09 23:31:02 +0000347 bool m_local_debugserver; // Is the debugserver process we are talking to local or on another machine.
Jim Ingham55e01d82011-01-22 01:33:44 +0000348 std::vector<lldb::user_id_t> m_thread_observation_bps;
Chris Lattner24943d22010-06-08 16:52:24 +0000349
350 void
351 ResetGDBRemoteState ();
352
353 bool
354 StartAsyncThread ();
355
356 void
357 StopAsyncThread ();
358
359 static void *
360 AsyncThread (void *arg);
361
362 static bool
363 MonitorDebugserverProcess (void *callback_baton,
364 lldb::pid_t pid,
365 int signo, // Zero for no signal
366 int exit_status); // Exit value of process if signal is zero
367
368 lldb::StateType
369 SetThreadStopInfo (StringExtractor& stop_packet);
370
371 void
372 DidLaunchOrAttach ();
373
374 lldb_private::Error
375 ConnectToDebugserver (const char *host_port);
376
377 const char *
378 GetDispatchQueueNameForThread (lldb::addr_t thread_dispatch_qaddr,
379 std::string &dispatch_queue_name);
380
381 static size_t
382 AttachInputReaderCallback (void *baton,
383 lldb_private::InputReader *reader,
384 lldb::InputReaderAction notification,
385 const char *bytes,
386 size_t bytes_len);
387
Greg Clayton72e1c782011-01-22 23:43:18 +0000388 lldb_private::Error
389 InterruptIfRunning (bool discard_thread_plans,
390 bool catch_stop_event,
391 bool resume_private_state_thread,
392 lldb::EventSP &stop_event_sp);
393
Chris Lattner24943d22010-06-08 16:52:24 +0000394private:
395 //------------------------------------------------------------------
396 // For ProcessGDBRemote only
397 //------------------------------------------------------------------
Jim Ingham55e01d82011-01-22 01:33:44 +0000398 static bool
399 NewThreadNotifyBreakpointHit (void *baton,
400 lldb_private::StoppointCallbackContext *context,
401 lldb::user_id_t break_id,
402 lldb::user_id_t break_loc_id);
403
Chris Lattner24943d22010-06-08 16:52:24 +0000404 DISALLOW_COPY_AND_ASSIGN (ProcessGDBRemote);
405
Chris Lattner24943d22010-06-08 16:52:24 +0000406};
407
408#endif // liblldb_ProcessGDBRemote_h_