blob: d1fb7bf915591bcbaad4ee26aac69c9ee1d51c06 [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
84 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
92 WillAttach (lldb::pid_t pid);
93
94 virtual lldb_private::Error
95 WillAttach (const char *process_name, bool wait_for_launch);
96
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
140 DoHalt ();
141
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
152 DoDestroy ();
153
154 virtual void
155 RefreshStateAfterStop();
156
157 //------------------------------------------------------------------
158 // Process Queries
159 //------------------------------------------------------------------
160 virtual bool
161 IsAlive ();
162
163 virtual lldb::addr_t
164 GetImageInfoAddress();
165
166 //------------------------------------------------------------------
167 // Process Memory
168 //------------------------------------------------------------------
169 virtual size_t
170 DoReadMemory (lldb::addr_t addr, void *buf, size_t size, lldb_private::Error &error);
171
172 virtual size_t
173 DoWriteMemory (lldb::addr_t addr, const void *buf, size_t size, lldb_private::Error &error);
174
175 virtual lldb::addr_t
176 DoAllocateMemory (size_t size, uint32_t permissions, lldb_private::Error &error);
177
178 virtual lldb_private::Error
179 DoDeallocateMemory (lldb::addr_t ptr);
180
181 //------------------------------------------------------------------
182 // Process STDIO
183 //------------------------------------------------------------------
184 virtual size_t
185 GetSTDOUT (char *buf, size_t buf_size, lldb_private::Error &error);
186
187 virtual size_t
188 GetSTDERR (char *buf, size_t buf_size, lldb_private::Error &error);
189
190 virtual size_t
191 PutSTDIN (const char *buf, size_t buf_size, lldb_private::Error &error);
192
193 //----------------------------------------------------------------------
194 // Process Breakpoints
195 //----------------------------------------------------------------------
196 virtual size_t
197 GetSoftwareBreakpointTrapOpcode (lldb_private::BreakpointSite *bp_site);
198
199 //----------------------------------------------------------------------
200 // Process Breakpoints
201 //----------------------------------------------------------------------
202 virtual lldb_private::Error
203 EnableBreakpoint (lldb_private::BreakpointSite *bp_site);
204
205 virtual lldb_private::Error
206 DisableBreakpoint (lldb_private::BreakpointSite *bp_site);
207
208 //----------------------------------------------------------------------
209 // Process Watchpoints
210 //----------------------------------------------------------------------
211 virtual lldb_private::Error
212 EnableWatchpoint (lldb_private::WatchpointLocation *wp_loc);
213
214 virtual lldb_private::Error
215 DisableWatchpoint (lldb_private::WatchpointLocation *wp_loc);
216
217 virtual lldb::ByteOrder
218 GetByteOrder () const;
219
220 virtual lldb_private::DynamicLoader *
221 GetDynamicLoader ();
222
223protected:
224 friend class ThreadGDBRemote;
225 friend class GDBRemoteCommunication;
226 friend class GDBRemoteRegisterContext;
227
228 bool
229 SetCurrentGDBRemoteThread (int tid);
230
231 bool
232 SetCurrentGDBRemoteThreadForRun (int tid);
233
234 //----------------------------------------------------------------------
235 // Accessors
236 //----------------------------------------------------------------------
237 bool
238 IsRunning ( lldb::StateType state )
239 {
240 return state == lldb::eStateRunning || IsStepping(state);
241 }
242
243 bool
244 IsStepping ( lldb::StateType state)
245 {
246 return state == lldb::eStateStepping;
247 }
248 bool
249 CanResume ( lldb::StateType state)
250 {
251 return state == lldb::eStateStopped;
252 }
253
254 bool
255 HasExited (lldb::StateType state)
256 {
257 return state == lldb::eStateExited;
258 }
259
260 bool
261 ProcessIDIsValid ( ) const;
262
263 static void
264 STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len);
265
266 void
267 AppendSTDOUT (const char* s, size_t len);
268
Chris Lattner24943d22010-06-08 16:52:24 +0000269 void
270 Clear ( );
271
272 lldb_private::Flags &
273 GetFlags ()
274 {
275 return m_flags;
276 }
277
278 const lldb_private::Flags &
279 GetFlags () const
280 {
281 return m_flags;
282 }
283
284 uint32_t
285 UpdateThreadListIfNeeded ();
286
287 lldb_private::Error
288 StartDebugserverProcess (const char *debugserver_url, // The connection string to use in the spawned debugserver ("localhost:1234" or "/dev/tty...")
289 char const *inferior_argv[],
290 char const *inferior_envp[],
291 const char *stdin_path,
292 lldb::pid_t attach_pid, // If inferior inferior_argv == NULL, then attach to this pid
293 const char *attach_pid_name, // Wait for the next process to launch whose basename matches "attach_wait_name"
294 bool wait_for_launch, // Wait for the process named "attach_wait_name" to launch
295 lldb_private::ArchSpec& arch_spec);
296
297 void
298 KillDebugserverProcess ();
299
300 void
301 BuildDynamicRegisterInfo ();
302
303 GDBRemoteCommunication &
304 GetGDBRemote()
305 {
306 return m_gdb_comm;
307 }
308
309 //------------------------------------------------------------------
310 /// Broadcaster event bits definitions.
311 //------------------------------------------------------------------
312 enum
313 {
314 eBroadcastBitAsyncContinue = (1 << 0),
315 eBroadcastBitAsyncThreadShouldExit = (1 << 1)
316 };
317
318
319 std::auto_ptr<lldb_private::DynamicLoader> m_dynamic_loader_ap;
320 lldb_private::Flags m_flags; // Process specific flags (see eFlags enums)
321 lldb_private::Communication m_stdio_communication;
322 lldb_private::Mutex m_stdio_mutex; // Multithreaded protection for stdio
323 std::string m_stdout_data;
Chris Lattner24943d22010-06-08 16:52:24 +0000324 lldb::ByteOrder m_byte_order;
325 GDBRemoteCommunication m_gdb_comm;
326 lldb::pid_t m_debugserver_pid;
327 uint32_t m_debugserver_monitor;
328 StringExtractor m_last_stop_packet;
329 GDBRemoteDynamicRegisterInfo m_register_info;
330 lldb_private::Broadcaster m_async_broadcaster;
331 lldb::thread_t m_async_thread;
332 // Current GDB remote state. Any members added here need to be reset to
333 // proper default values in ResetGDBRemoteState ().
334 lldb::tid_t m_curr_tid; // Current gdb remote protocol thread index for all other operations
335 lldb::tid_t m_curr_tid_run; // Current gdb remote protocol thread index for continue, step, etc
336 uint32_t m_z0_supported:1; // Set to non-zero if Z0 and z0 packets are supported
337 lldb_private::StreamString m_continue_packet;
338 lldb::addr_t m_dispatch_queue_offsets_addr;
339 uint32_t m_packet_timeout;
340 size_t m_max_memory_size; // The maximum number of bytes to read/write when reading and writing memory
341 lldb_private::unw_targettype_t m_libunwind_target_type;
342 lldb_private::unw_addr_space_t m_libunwind_addr_space; // libunwind address space object for this process.
343 bool m_waiting_for_attach;
Jim Ingham7508e732010-08-09 23:31:02 +0000344 bool m_local_debugserver; // Is the debugserver process we are talking to local or on another machine.
Chris Lattner24943d22010-06-08 16:52:24 +0000345
346 void
347 ResetGDBRemoteState ();
348
349 bool
350 StartAsyncThread ();
351
352 void
353 StopAsyncThread ();
354
355 static void *
356 AsyncThread (void *arg);
357
358 static bool
359 MonitorDebugserverProcess (void *callback_baton,
360 lldb::pid_t pid,
361 int signo, // Zero for no signal
362 int exit_status); // Exit value of process if signal is zero
363
364 lldb::StateType
365 SetThreadStopInfo (StringExtractor& stop_packet);
366
367 void
368 DidLaunchOrAttach ();
369
370 lldb_private::Error
371 ConnectToDebugserver (const char *host_port);
372
373 const char *
374 GetDispatchQueueNameForThread (lldb::addr_t thread_dispatch_qaddr,
375 std::string &dispatch_queue_name);
376
377 static size_t
378 AttachInputReaderCallback (void *baton,
379 lldb_private::InputReader *reader,
380 lldb::InputReaderAction notification,
381 const char *bytes,
382 size_t bytes_len);
383
384private:
385 //------------------------------------------------------------------
386 // For ProcessGDBRemote only
387 //------------------------------------------------------------------
388 DISALLOW_COPY_AND_ASSIGN (ProcessGDBRemote);
389
390 lldb_private::unw_addr_space_t
391 GetLibUnwindAddressSpace ();
392
393 void
394 DestoryLibUnwindAddressSpace ();
395};
396
397#endif // liblldb_ProcessGDBRemote_h_