blob: 1e85e9918a7ffa1cd8bbdff7990069ede88dda84 [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"
Greg Clayton54e7afa2010-07-09 20:39:50 +000032#include "libunwind/include/libunwind.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
86 const char *stdout_path, // Can be NULL
87 const char *stderr_path); // Can be NULL
88
89 virtual void
90 DidLaunch ();
91
92 virtual lldb_private::Error
93 WillAttach (lldb::pid_t pid);
94
95 virtual lldb_private::Error
96 WillAttach (const char *process_name, bool wait_for_launch);
97
98 lldb_private::Error
99 WillLaunchOrAttach ();
100
101 virtual lldb_private::Error
Greg Clayton54e7afa2010-07-09 20:39:50 +0000102 DoAttachToProcessWithID (lldb::pid_t pid);
Chris Lattner24943d22010-06-08 16:52:24 +0000103
104 virtual lldb_private::Error
Greg Clayton54e7afa2010-07-09 20:39:50 +0000105 DoAttachToProcessWithName (const char *process_name, bool wait_for_launch);
Chris Lattner24943d22010-06-08 16:52:24 +0000106
107 virtual void
108 DidAttach ();
109
110 //------------------------------------------------------------------
111 // PluginInterface protocol
112 //------------------------------------------------------------------
113 virtual const char *
114 GetPluginName();
115
116 virtual const char *
117 GetShortPluginName();
118
119 virtual uint32_t
120 GetPluginVersion();
121
122 virtual void
123 GetPluginCommandHelp (const char *command, lldb_private::Stream *strm);
124
125 virtual lldb_private::Error
126 ExecutePluginCommand (lldb_private::Args &command, lldb_private::Stream *strm);
127
128 virtual lldb_private::Log *
129 EnablePluginLogging (lldb_private::Stream *strm, lldb_private::Args &command);
130
131 //------------------------------------------------------------------
132 // Process Control
133 //------------------------------------------------------------------
134 virtual lldb_private::Error
135 WillResume ();
136
137 virtual lldb_private::Error
138 DoResume ();
139
140 virtual lldb_private::Error
Jim Ingham3ae449a2010-11-17 02:32:00 +0000141 DoHalt (bool &caused_stop);
Chris Lattner24943d22010-06-08 16:52:24 +0000142
143 virtual lldb_private::Error
144 WillDetach ();
145
146 virtual lldb_private::Error
147 DoDetach ();
148
149 virtual lldb_private::Error
150 DoSignal (int signal);
151
152 virtual lldb_private::Error
153 DoDestroy ();
154
155 virtual void
156 RefreshStateAfterStop();
157
158 //------------------------------------------------------------------
159 // Process Queries
160 //------------------------------------------------------------------
161 virtual bool
162 IsAlive ();
163
164 virtual lldb::addr_t
165 GetImageInfoAddress();
166
167 //------------------------------------------------------------------
168 // Process Memory
169 //------------------------------------------------------------------
170 virtual size_t
171 DoReadMemory (lldb::addr_t addr, void *buf, size_t size, lldb_private::Error &error);
172
173 virtual size_t
174 DoWriteMemory (lldb::addr_t addr, const void *buf, size_t size, lldb_private::Error &error);
175
176 virtual lldb::addr_t
177 DoAllocateMemory (size_t size, uint32_t permissions, lldb_private::Error &error);
178
179 virtual lldb_private::Error
180 DoDeallocateMemory (lldb::addr_t ptr);
181
182 //------------------------------------------------------------------
183 // Process STDIO
184 //------------------------------------------------------------------
185 virtual size_t
186 GetSTDOUT (char *buf, size_t buf_size, lldb_private::Error &error);
187
188 virtual size_t
189 GetSTDERR (char *buf, size_t buf_size, lldb_private::Error &error);
190
191 virtual size_t
192 PutSTDIN (const char *buf, size_t buf_size, lldb_private::Error &error);
193
194 //----------------------------------------------------------------------
195 // Process Breakpoints
196 //----------------------------------------------------------------------
197 virtual size_t
198 GetSoftwareBreakpointTrapOpcode (lldb_private::BreakpointSite *bp_site);
199
200 //----------------------------------------------------------------------
201 // Process Breakpoints
202 //----------------------------------------------------------------------
203 virtual lldb_private::Error
204 EnableBreakpoint (lldb_private::BreakpointSite *bp_site);
205
206 virtual lldb_private::Error
207 DisableBreakpoint (lldb_private::BreakpointSite *bp_site);
208
209 //----------------------------------------------------------------------
210 // Process Watchpoints
211 //----------------------------------------------------------------------
212 virtual lldb_private::Error
213 EnableWatchpoint (lldb_private::WatchpointLocation *wp_loc);
214
215 virtual lldb_private::Error
216 DisableWatchpoint (lldb_private::WatchpointLocation *wp_loc);
217
218 virtual lldb::ByteOrder
219 GetByteOrder () const;
220
221 virtual lldb_private::DynamicLoader *
222 GetDynamicLoader ();
223
224protected:
225 friend class ThreadGDBRemote;
226 friend class GDBRemoteCommunication;
227 friend class GDBRemoteRegisterContext;
228
229 bool
230 SetCurrentGDBRemoteThread (int tid);
231
232 bool
233 SetCurrentGDBRemoteThreadForRun (int tid);
234
235 //----------------------------------------------------------------------
236 // Accessors
237 //----------------------------------------------------------------------
238 bool
239 IsRunning ( lldb::StateType state )
240 {
241 return state == lldb::eStateRunning || IsStepping(state);
242 }
243
244 bool
245 IsStepping ( lldb::StateType state)
246 {
247 return state == lldb::eStateStepping;
248 }
249 bool
250 CanResume ( lldb::StateType state)
251 {
252 return state == lldb::eStateStopped;
253 }
254
255 bool
256 HasExited (lldb::StateType state)
257 {
258 return state == lldb::eStateExited;
259 }
260
261 bool
262 ProcessIDIsValid ( ) const;
263
Caroline Tice861efb32010-11-16 05:07:41 +0000264// static void
265// STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len);
Chris Lattner24943d22010-06-08 16:52:24 +0000266
Caroline Tice861efb32010-11-16 05:07:41 +0000267// void
268// AppendSTDOUT (const char* s, size_t len);
Chris Lattner24943d22010-06-08 16:52:24 +0000269
Chris Lattner24943d22010-06-08 16:52:24 +0000270 void
271 Clear ( );
272
273 lldb_private::Flags &
274 GetFlags ()
275 {
276 return m_flags;
277 }
278
279 const lldb_private::Flags &
280 GetFlags () const
281 {
282 return m_flags;
283 }
284
285 uint32_t
286 UpdateThreadListIfNeeded ();
287
288 lldb_private::Error
Greg Clayton23cf0c72010-11-08 04:29:11 +0000289 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 +0000290 char const *inferior_argv[],
291 char const *inferior_envp[],
292 const char *stdin_path,
Greg Clayton23cf0c72010-11-08 04:29:11 +0000293 bool launch_process, // Set to true if we are going to be launching a the process
294 lldb::pid_t attach_pid, // If inferior inferior_argv == NULL, then attach to this pid
295 const char *attach_pid_name, // Wait for the next process to launch whose basename matches "attach_wait_name"
296 bool wait_for_launch, // Wait for the process named "attach_wait_name" to launch
297 bool disable_aslr, // Disable ASLR
Chris Lattner24943d22010-06-08 16:52:24 +0000298 lldb_private::ArchSpec& arch_spec);
299
300 void
301 KillDebugserverProcess ();
302
303 void
304 BuildDynamicRegisterInfo ();
305
306 GDBRemoteCommunication &
307 GetGDBRemote()
308 {
309 return m_gdb_comm;
310 }
311
312 //------------------------------------------------------------------
313 /// Broadcaster event bits definitions.
314 //------------------------------------------------------------------
315 enum
316 {
317 eBroadcastBitAsyncContinue = (1 << 0),
318 eBroadcastBitAsyncThreadShouldExit = (1 << 1)
319 };
320
321
322 std::auto_ptr<lldb_private::DynamicLoader> m_dynamic_loader_ap;
323 lldb_private::Flags m_flags; // Process specific flags (see eFlags enums)
Chris Lattner24943d22010-06-08 16:52:24 +0000324 lldb_private::Mutex m_stdio_mutex; // Multithreaded protection for stdio
Chris Lattner24943d22010-06-08 16:52:24 +0000325 lldb::ByteOrder m_byte_order;
326 GDBRemoteCommunication m_gdb_comm;
327 lldb::pid_t m_debugserver_pid;
Greg Clayton75ccf502010-08-21 02:22:51 +0000328 lldb::thread_t m_debugserver_thread;
Chris Lattner24943d22010-06-08 16:52:24 +0000329 StringExtractor m_last_stop_packet;
330 GDBRemoteDynamicRegisterInfo m_register_info;
331 lldb_private::Broadcaster m_async_broadcaster;
332 lldb::thread_t m_async_thread;
333 // Current GDB remote state. Any members added here need to be reset to
334 // proper default values in ResetGDBRemoteState ().
335 lldb::tid_t m_curr_tid; // Current gdb remote protocol thread index for all other operations
336 lldb::tid_t m_curr_tid_run; // Current gdb remote protocol thread index for continue, step, etc
337 uint32_t m_z0_supported:1; // Set to non-zero if Z0 and z0 packets are supported
338 lldb_private::StreamString m_continue_packet;
339 lldb::addr_t m_dispatch_queue_offsets_addr;
340 uint32_t m_packet_timeout;
341 size_t m_max_memory_size; // The maximum number of bytes to read/write when reading and writing memory
342 lldb_private::unw_targettype_t m_libunwind_target_type;
343 lldb_private::unw_addr_space_t m_libunwind_addr_space; // libunwind address space object for this process.
344 bool m_waiting_for_attach;
Jim Ingham7508e732010-08-09 23:31:02 +0000345 bool m_local_debugserver; // Is the debugserver process we are talking to local or on another machine.
Chris Lattner24943d22010-06-08 16:52:24 +0000346
347 void
348 ResetGDBRemoteState ();
349
350 bool
351 StartAsyncThread ();
352
353 void
354 StopAsyncThread ();
355
356 static void *
357 AsyncThread (void *arg);
358
359 static bool
360 MonitorDebugserverProcess (void *callback_baton,
361 lldb::pid_t pid,
362 int signo, // Zero for no signal
363 int exit_status); // Exit value of process if signal is zero
364
365 lldb::StateType
366 SetThreadStopInfo (StringExtractor& stop_packet);
367
368 void
369 DidLaunchOrAttach ();
370
371 lldb_private::Error
372 ConnectToDebugserver (const char *host_port);
373
374 const char *
375 GetDispatchQueueNameForThread (lldb::addr_t thread_dispatch_qaddr,
376 std::string &dispatch_queue_name);
377
378 static size_t
379 AttachInputReaderCallback (void *baton,
380 lldb_private::InputReader *reader,
381 lldb::InputReaderAction notification,
382 const char *bytes,
383 size_t bytes_len);
384
385private:
386 //------------------------------------------------------------------
387 // For ProcessGDBRemote only
388 //------------------------------------------------------------------
389 DISALLOW_COPY_AND_ASSIGN (ProcessGDBRemote);
390
391 lldb_private::unw_addr_space_t
392 GetLibUnwindAddressSpace ();
393
394 void
395 DestoryLibUnwindAddressSpace ();
396};
397
398#endif // liblldb_ProcessGDBRemote_h_