blob: 79872b4db4ce8b45fa9e1d582c69847e129fc265 [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"
24#include "lldb/Core/ThreadSafeValue.h"
25#include "lldb/Target/Process.h"
26#include "lldb/Target/Thread.h"
27
28#include "GDBRemoteCommunication.h"
Greg Clayton54e7afa2010-07-09 20:39:50 +000029#include "Utility/StringExtractor.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030#include "GDBRemoteRegisterContext.h"
Greg Clayton54e7afa2010-07-09 20:39:50 +000031#include "libunwind/include/libunwind.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
70 //------------------------------------------------------------------
71 // Creating a new process, or attaching to an existing one
72 //------------------------------------------------------------------
73 virtual lldb_private::Error
74 WillLaunch (lldb_private::Module* module);
75
76 virtual lldb_private::Error
77 DoLaunch (lldb_private::Module* module,
78 char const *argv[], // Can be NULL
79 char const *envp[], // Can be NULL
80 const char *stdin_path, // Can be NULL
81 const char *stdout_path, // Can be NULL
82 const char *stderr_path); // Can be NULL
83
84 virtual void
85 DidLaunch ();
86
87 virtual lldb_private::Error
88 WillAttach (lldb::pid_t pid);
89
90 virtual lldb_private::Error
91 WillAttach (const char *process_name, bool wait_for_launch);
92
93 lldb_private::Error
94 WillLaunchOrAttach ();
95
96 virtual lldb_private::Error
Greg Clayton54e7afa2010-07-09 20:39:50 +000097 DoAttachToProcessWithID (lldb::pid_t pid);
Chris Lattner24943d22010-06-08 16:52:24 +000098
99 virtual lldb_private::Error
Greg Clayton54e7afa2010-07-09 20:39:50 +0000100 DoAttachToProcessWithName (const char *process_name, bool wait_for_launch);
Chris Lattner24943d22010-06-08 16:52:24 +0000101
102 virtual void
103 DidAttach ();
104
105 //------------------------------------------------------------------
106 // PluginInterface protocol
107 //------------------------------------------------------------------
108 virtual const char *
109 GetPluginName();
110
111 virtual const char *
112 GetShortPluginName();
113
114 virtual uint32_t
115 GetPluginVersion();
116
117 virtual void
118 GetPluginCommandHelp (const char *command, lldb_private::Stream *strm);
119
120 virtual lldb_private::Error
121 ExecutePluginCommand (lldb_private::Args &command, lldb_private::Stream *strm);
122
123 virtual lldb_private::Log *
124 EnablePluginLogging (lldb_private::Stream *strm, lldb_private::Args &command);
125
126 //------------------------------------------------------------------
127 // Process Control
128 //------------------------------------------------------------------
129 virtual lldb_private::Error
130 WillResume ();
131
132 virtual lldb_private::Error
133 DoResume ();
134
135 virtual lldb_private::Error
136 DoHalt ();
137
138 virtual lldb_private::Error
139 WillDetach ();
140
141 virtual lldb_private::Error
142 DoDetach ();
143
144 virtual lldb_private::Error
145 DoSignal (int signal);
146
147 virtual lldb_private::Error
148 DoDestroy ();
149
150 virtual void
151 RefreshStateAfterStop();
152
153 //------------------------------------------------------------------
154 // Process Queries
155 //------------------------------------------------------------------
156 virtual bool
157 IsAlive ();
158
159 virtual lldb::addr_t
160 GetImageInfoAddress();
161
162 //------------------------------------------------------------------
163 // Process Memory
164 //------------------------------------------------------------------
165 virtual size_t
166 DoReadMemory (lldb::addr_t addr, void *buf, size_t size, lldb_private::Error &error);
167
168 virtual size_t
169 DoWriteMemory (lldb::addr_t addr, const void *buf, size_t size, lldb_private::Error &error);
170
171 virtual lldb::addr_t
172 DoAllocateMemory (size_t size, uint32_t permissions, lldb_private::Error &error);
173
174 virtual lldb_private::Error
175 DoDeallocateMemory (lldb::addr_t ptr);
176
177 //------------------------------------------------------------------
178 // Process STDIO
179 //------------------------------------------------------------------
180 virtual size_t
181 GetSTDOUT (char *buf, size_t buf_size, lldb_private::Error &error);
182
183 virtual size_t
184 GetSTDERR (char *buf, size_t buf_size, lldb_private::Error &error);
185
186 virtual size_t
187 PutSTDIN (const char *buf, size_t buf_size, lldb_private::Error &error);
188
189 //----------------------------------------------------------------------
190 // Process Breakpoints
191 //----------------------------------------------------------------------
192 virtual size_t
193 GetSoftwareBreakpointTrapOpcode (lldb_private::BreakpointSite *bp_site);
194
195 //----------------------------------------------------------------------
196 // Process Breakpoints
197 //----------------------------------------------------------------------
198 virtual lldb_private::Error
199 EnableBreakpoint (lldb_private::BreakpointSite *bp_site);
200
201 virtual lldb_private::Error
202 DisableBreakpoint (lldb_private::BreakpointSite *bp_site);
203
204 //----------------------------------------------------------------------
205 // Process Watchpoints
206 //----------------------------------------------------------------------
207 virtual lldb_private::Error
208 EnableWatchpoint (lldb_private::WatchpointLocation *wp_loc);
209
210 virtual lldb_private::Error
211 DisableWatchpoint (lldb_private::WatchpointLocation *wp_loc);
212
213 virtual lldb::ByteOrder
214 GetByteOrder () const;
215
216 virtual lldb_private::DynamicLoader *
217 GetDynamicLoader ();
218
219protected:
220 friend class ThreadGDBRemote;
221 friend class GDBRemoteCommunication;
222 friend class GDBRemoteRegisterContext;
223
224 bool
225 SetCurrentGDBRemoteThread (int tid);
226
227 bool
228 SetCurrentGDBRemoteThreadForRun (int tid);
229
230 //----------------------------------------------------------------------
231 // Accessors
232 //----------------------------------------------------------------------
233 bool
234 IsRunning ( lldb::StateType state )
235 {
236 return state == lldb::eStateRunning || IsStepping(state);
237 }
238
239 bool
240 IsStepping ( lldb::StateType state)
241 {
242 return state == lldb::eStateStepping;
243 }
244 bool
245 CanResume ( lldb::StateType state)
246 {
247 return state == lldb::eStateStopped;
248 }
249
250 bool
251 HasExited (lldb::StateType state)
252 {
253 return state == lldb::eStateExited;
254 }
255
256 bool
257 ProcessIDIsValid ( ) const;
258
259 static void
260 STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len);
261
262 void
263 AppendSTDOUT (const char* s, size_t len);
264
265 lldb_private::ArchSpec&
266 GetArchSpec()
267 {
268 return m_arch_spec;
269 }
270 const lldb_private::ArchSpec&
271 GetArchSpec() const
272 {
273 return m_arch_spec;
274 }
275
276 void
277 Clear ( );
278
279 lldb_private::Flags &
280 GetFlags ()
281 {
282 return m_flags;
283 }
284
285 const lldb_private::Flags &
286 GetFlags () const
287 {
288 return m_flags;
289 }
290
291 uint32_t
292 UpdateThreadListIfNeeded ();
293
294 lldb_private::Error
295 StartDebugserverProcess (const char *debugserver_url, // The connection string to use in the spawned debugserver ("localhost:1234" or "/dev/tty...")
296 char const *inferior_argv[],
297 char const *inferior_envp[],
298 const char *stdin_path,
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
302 lldb_private::ArchSpec& arch_spec);
303
304 void
305 KillDebugserverProcess ();
306
307 void
308 BuildDynamicRegisterInfo ();
309
310 GDBRemoteCommunication &
311 GetGDBRemote()
312 {
313 return m_gdb_comm;
314 }
315
316 //------------------------------------------------------------------
317 /// Broadcaster event bits definitions.
318 //------------------------------------------------------------------
319 enum
320 {
321 eBroadcastBitAsyncContinue = (1 << 0),
322 eBroadcastBitAsyncThreadShouldExit = (1 << 1)
323 };
324
325
326 std::auto_ptr<lldb_private::DynamicLoader> m_dynamic_loader_ap;
327 lldb_private::Flags m_flags; // Process specific flags (see eFlags enums)
328 lldb_private::Communication m_stdio_communication;
329 lldb_private::Mutex m_stdio_mutex; // Multithreaded protection for stdio
330 std::string m_stdout_data;
331 lldb_private::ArchSpec m_arch_spec;
332 lldb::ByteOrder m_byte_order;
333 GDBRemoteCommunication m_gdb_comm;
334 lldb::pid_t m_debugserver_pid;
335 uint32_t m_debugserver_monitor;
336 StringExtractor m_last_stop_packet;
337 GDBRemoteDynamicRegisterInfo m_register_info;
338 lldb_private::Broadcaster m_async_broadcaster;
339 lldb::thread_t m_async_thread;
340 // Current GDB remote state. Any members added here need to be reset to
341 // proper default values in ResetGDBRemoteState ().
342 lldb::tid_t m_curr_tid; // Current gdb remote protocol thread index for all other operations
343 lldb::tid_t m_curr_tid_run; // Current gdb remote protocol thread index for continue, step, etc
344 uint32_t m_z0_supported:1; // Set to non-zero if Z0 and z0 packets are supported
345 lldb_private::StreamString m_continue_packet;
346 lldb::addr_t m_dispatch_queue_offsets_addr;
347 uint32_t m_packet_timeout;
348 size_t m_max_memory_size; // The maximum number of bytes to read/write when reading and writing memory
349 lldb_private::unw_targettype_t m_libunwind_target_type;
350 lldb_private::unw_addr_space_t m_libunwind_addr_space; // libunwind address space object for this process.
351 bool m_waiting_for_attach;
352
353 void
354 ResetGDBRemoteState ();
355
356 bool
357 StartAsyncThread ();
358
359 void
360 StopAsyncThread ();
361
362 static void *
363 AsyncThread (void *arg);
364
365 static bool
366 MonitorDebugserverProcess (void *callback_baton,
367 lldb::pid_t pid,
368 int signo, // Zero for no signal
369 int exit_status); // Exit value of process if signal is zero
370
371 lldb::StateType
372 SetThreadStopInfo (StringExtractor& stop_packet);
373
374 void
375 DidLaunchOrAttach ();
376
377 lldb_private::Error
378 ConnectToDebugserver (const char *host_port);
379
380 const char *
381 GetDispatchQueueNameForThread (lldb::addr_t thread_dispatch_qaddr,
382 std::string &dispatch_queue_name);
383
384 static size_t
385 AttachInputReaderCallback (void *baton,
386 lldb_private::InputReader *reader,
387 lldb::InputReaderAction notification,
388 const char *bytes,
389 size_t bytes_len);
390
391private:
392 //------------------------------------------------------------------
393 // For ProcessGDBRemote only
394 //------------------------------------------------------------------
395 DISALLOW_COPY_AND_ASSIGN (ProcessGDBRemote);
396
397 lldb_private::unw_addr_space_t
398 GetLibUnwindAddressSpace ();
399
400 void
401 DestoryLibUnwindAddressSpace ();
402};
403
404#endif // liblldb_ProcessGDBRemote_h_