blob: ab8fef3f295dbd59c5a2240ae5ffc06a15f1d1c5 [file] [log] [blame]
Chris Lattner30fdc8d2010-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 Clayton71fc2a32011-02-12 06:28:37 +000017#include <vector>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018
19// Other libraries and framework includes
20#include "lldb/Core/ArchSpec.h"
21#include "lldb/Core/Broadcaster.h"
Jason Molenda2e56a252013-05-11 03:09:05 +000022#include "lldb/Core/ConstString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/Error.h"
24#include "lldb/Core/InputReader.h"
25#include "lldb/Core/StreamString.h"
Jim Ingham5aee1622010-08-09 23:31:02 +000026#include "lldb/Core/StringList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Core/ThreadSafeValue.h"
28#include "lldb/Target/Process.h"
29#include "lldb/Target/Thread.h"
30
Greg Clayton576d8832011-03-22 04:00:09 +000031#include "GDBRemoteCommunicationClient.h"
Greg Claytonc982c762010-07-09 20:39:50 +000032#include "Utility/StringExtractor.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "GDBRemoteRegisterContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034
35class ThreadGDBRemote;
36
37class ProcessGDBRemote : public lldb_private::Process
38{
39public:
40 //------------------------------------------------------------------
41 // Constructors and Destructors
42 //------------------------------------------------------------------
Greg Claytonc3776bf2012-02-09 06:16:32 +000043 static lldb::ProcessSP
44 CreateInstance (lldb_private::Target& target,
45 lldb_private::Listener &listener,
46 const lldb_private::FileSpec *crash_file_path);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047
48 static void
49 Initialize();
50
51 static void
52 Terminate();
53
Greg Clayton57abc5d2013-05-10 21:47:16 +000054 static lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055 GetPluginNameStatic();
56
57 static const char *
58 GetPluginDescriptionStatic();
59
60 //------------------------------------------------------------------
61 // Constructors and Destructors
62 //------------------------------------------------------------------
63 ProcessGDBRemote(lldb_private::Target& target, lldb_private::Listener &listener);
64
65 virtual
66 ~ProcessGDBRemote();
67
68 //------------------------------------------------------------------
69 // Check if a given Process
70 //------------------------------------------------------------------
71 virtual bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +000072 CanDebug (lldb_private::Target &target,
73 bool plugin_specified_by_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074
Greg Clayton998255b2012-10-13 02:07:45 +000075 virtual lldb_private::CommandObject *
76 GetPluginCommandObject();
Jim Ingham5aee1622010-08-09 23:31:02 +000077
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078 //------------------------------------------------------------------
79 // Creating a new process, or attaching to an existing one
80 //------------------------------------------------------------------
81 virtual lldb_private::Error
82 WillLaunch (lldb_private::Module* module);
83
84 virtual lldb_private::Error
Greg Clayton982c9762011-11-03 21:22:33 +000085 DoLaunch (lldb_private::Module *exe_module,
86 const lldb_private::ProcessLaunchInfo &launch_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087
88 virtual void
89 DidLaunch ();
90
91 virtual lldb_private::Error
Greg Clayton3af9ea52010-11-18 05:57:03 +000092 WillAttachToProcessWithID (lldb::pid_t pid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000093
94 virtual lldb_private::Error
Greg Clayton3af9ea52010-11-18 05:57:03 +000095 WillAttachToProcessWithName (const char *process_name, bool wait_for_launch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000096
Greg Claytonb766a732011-02-04 01:58:07 +000097 virtual lldb_private::Error
Jason Molenda4bd4e7e2012-09-29 04:02:01 +000098 DoConnectRemote (lldb_private::Stream *strm, const char *remote_url);
Greg Claytonb766a732011-02-04 01:58:07 +000099
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100 lldb_private::Error
101 WillLaunchOrAttach ();
102
103 virtual lldb_private::Error
Greg Claytonc982c762010-07-09 20:39:50 +0000104 DoAttachToProcessWithID (lldb::pid_t pid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105
106 virtual lldb_private::Error
Han Ming Ong84647042012-02-25 01:07:38 +0000107 DoAttachToProcessWithID (lldb::pid_t pid, const lldb_private::ProcessAttachInfo &attach_info);
108
109 virtual lldb_private::Error
110 DoAttachToProcessWithName (const char *process_name,
111 bool wait_for_launch,
112 const lldb_private::ProcessAttachInfo &attach_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113
114 virtual void
115 DidAttach ();
116
117 //------------------------------------------------------------------
118 // PluginInterface protocol
119 //------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000120 virtual lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000121 GetPluginName();
122
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000123 virtual uint32_t
124 GetPluginVersion();
125
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000126 //------------------------------------------------------------------
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
Jim Ingham0d8bcc72010-11-17 02:32:00 +0000136 DoHalt (bool &caused_stop);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137
138 virtual lldb_private::Error
Jim Inghamacff8952013-05-02 00:27:30 +0000139 DoDetach (bool keep_stopped);
Jim Ingham8af3b9c2013-03-29 01:18:12 +0000140
141 virtual bool
142 DetachRequiresHalt() { return true; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143
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
Greg Clayton46fb5582011-11-18 07:03:08 +0000175 GetMemoryRegionInfo (lldb::addr_t load_addr,
176 lldb_private::MemoryRegionInfo &region_info);
177
178 virtual lldb_private::Error
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179 DoDeallocateMemory (lldb::addr_t ptr);
180
181 //------------------------------------------------------------------
182 // Process STDIO
183 //------------------------------------------------------------------
184 virtual size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185 PutSTDIN (const char *buf, size_t buf_size, lldb_private::Error &error);
186
187 //----------------------------------------------------------------------
188 // Process Breakpoints
189 //----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000190 virtual lldb_private::Error
Jim Ingham299c0c12013-02-15 02:06:30 +0000191 EnableBreakpointSite (lldb_private::BreakpointSite *bp_site);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192
193 virtual lldb_private::Error
Jim Ingham299c0c12013-02-15 02:06:30 +0000194 DisableBreakpointSite (lldb_private::BreakpointSite *bp_site);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195
196 //----------------------------------------------------------------------
197 // Process Watchpoints
198 //----------------------------------------------------------------------
199 virtual lldb_private::Error
Jim Ingham1b5792e2012-12-18 02:03:49 +0000200 EnableWatchpoint (lldb_private::Watchpoint *wp, bool notify = true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201
202 virtual lldb_private::Error
Jim Ingham1b5792e2012-12-18 02:03:49 +0000203 DisableWatchpoint (lldb_private::Watchpoint *wp, bool notify = true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204
Johnny Chen64637202012-05-23 21:09:52 +0000205 virtual lldb_private::Error
206 GetWatchpointSupportInfo (uint32_t &num);
207
Enrico Granataf04a2192012-07-13 23:18:48 +0000208 virtual lldb_private::Error
209 GetWatchpointSupportInfo (uint32_t &num, bool& after);
210
Jim Ingham1c823b42011-01-22 01:33:44 +0000211 virtual bool
212 StartNoticingNewThreads();
213
214 virtual bool
215 StopNoticingNewThreads();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000216
Greg Claytonc1422c12012-04-09 22:46:21 +0000217 GDBRemoteCommunicationClient &
218 GetGDBRemote()
219 {
220 return m_gdb_comm;
221 }
222
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223protected:
224 friend class ThreadGDBRemote;
Greg Clayton576d8832011-03-22 04:00:09 +0000225 friend class GDBRemoteCommunicationClient;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000226 friend class GDBRemoteRegisterContext;
227
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000228 //----------------------------------------------------------------------
229 // Accessors
230 //----------------------------------------------------------------------
231 bool
232 IsRunning ( lldb::StateType state )
233 {
234 return state == lldb::eStateRunning || IsStepping(state);
235 }
236
237 bool
238 IsStepping ( lldb::StateType state)
239 {
240 return state == lldb::eStateStepping;
241 }
242 bool
243 CanResume ( lldb::StateType state)
244 {
245 return state == lldb::eStateStopped;
246 }
247
248 bool
249 HasExited (lldb::StateType state)
250 {
251 return state == lldb::eStateExited;
252 }
253
254 bool
255 ProcessIDIsValid ( ) const;
256
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257 void
258 Clear ( );
259
260 lldb_private::Flags &
261 GetFlags ()
262 {
263 return m_flags;
264 }
265
266 const lldb_private::Flags &
267 GetFlags () const
268 {
269 return m_flags;
270 }
271
Greg Clayton9fc13552012-04-10 00:18:59 +0000272 virtual bool
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000273 UpdateThreadList (lldb_private::ThreadList &old_thread_list,
274 lldb_private::ThreadList &new_thread_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275
276 lldb_private::Error
Greg Clayton8b82f082011-04-12 05:54:46 +0000277 StartDebugserverProcess (const char *debugserver_url);
Han Ming Ong84647042012-02-25 01:07:38 +0000278
279 lldb_private::Error
280 StartDebugserverProcess (const char *debugserver_url, const lldb_private::ProcessInfo &process_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281
282 void
283 KillDebugserverProcess ();
284
285 void
Greg Clayton513c26c2011-01-29 07:10:55 +0000286 BuildDynamicRegisterInfo (bool force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000287
Greg Clayton09c3e3d2011-12-06 04:51:14 +0000288 void
Greg Clayton8cda7f02013-05-21 21:55:59 +0000289 SetLastStopPacket (const StringExtractorGDBRemote &response);
Jason Molendad1fae142012-09-29 08:03:33 +0000290
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000291 //------------------------------------------------------------------
292 /// Broadcaster event bits definitions.
293 //------------------------------------------------------------------
294 enum
295 {
296 eBroadcastBitAsyncContinue = (1 << 0),
Jim Inghamb1e2e842012-04-12 18:49:31 +0000297 eBroadcastBitAsyncThreadShouldExit = (1 << 1),
298 eBroadcastBitAsyncThreadDidExit = (1 << 2)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000299 };
300
Jim Ingham455fa5c2012-11-01 01:15:33 +0000301 typedef enum AsyncThreadState
302 {
303 eAsyncThreadNotStarted,
304 eAsyncThreadRunning,
305 eAsyncThreadDone
306 } AsyncThreadState;
307
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308 lldb_private::Flags m_flags; // Process specific flags (see eFlags enums)
Greg Clayton576d8832011-03-22 04:00:09 +0000309 GDBRemoteCommunicationClient m_gdb_comm;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000310 lldb::pid_t m_debugserver_pid;
Greg Claytondd0e5a52011-06-02 22:22:38 +0000311 StringExtractorGDBRemote m_last_stop_packet;
Greg Clayton09c3e3d2011-12-06 04:51:14 +0000312 lldb_private::Mutex m_last_stop_packet_mutex;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000313 GDBRemoteDynamicRegisterInfo m_register_info;
314 lldb_private::Broadcaster m_async_broadcaster;
315 lldb::thread_t m_async_thread;
Jim Ingham455fa5c2012-11-01 01:15:33 +0000316 AsyncThreadState m_async_thread_state;
317 lldb_private::Mutex m_async_thread_state_mutex;
Greg Clayton71fc2a32011-02-12 06:28:37 +0000318 typedef std::vector<lldb::tid_t> tid_collection;
319 typedef std::vector< std::pair<lldb::tid_t,int> > tid_sig_collection;
Greg Clayton70b57652011-05-15 01:25:55 +0000320 typedef std::map<lldb::addr_t, lldb::addr_t> MMapMap;
Greg Clayton9e920902012-04-10 02:25:43 +0000321 tid_collection m_thread_ids; // Thread IDs for all threads. This list gets updated after stopping
Greg Clayton71fc2a32011-02-12 06:28:37 +0000322 tid_collection m_continue_c_tids; // 'c' for continue
323 tid_sig_collection m_continue_C_tids; // 'C' for continue with signal
324 tid_collection m_continue_s_tids; // 's' for step
325 tid_sig_collection m_continue_S_tids; // 'S' for step with signal
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000326 lldb::addr_t m_dispatch_queue_offsets_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000327 size_t m_max_memory_size; // The maximum number of bytes to read/write when reading and writing memory
Greg Clayton70b57652011-05-15 01:25:55 +0000328 MMapMap m_addr_to_mmap_size;
Greg Clayton4116e932012-05-15 02:33:01 +0000329 lldb::BreakpointSP m_thread_create_bp_sp;
330 bool m_waiting_for_attach;
Jim Ingham43c555d2012-07-04 00:35:43 +0000331 bool m_destroy_tried_resuming;
Greg Clayton998255b2012-10-13 02:07:45 +0000332 lldb::CommandObjectSP m_command_sp;
Greg Clayton4116e932012-05-15 02:33:01 +0000333
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334 bool
335 StartAsyncThread ();
336
337 void
338 StopAsyncThread ();
339
340 static void *
341 AsyncThread (void *arg);
342
343 static bool
344 MonitorDebugserverProcess (void *callback_baton,
345 lldb::pid_t pid,
Greg Claytone4e45922011-11-16 05:37:56 +0000346 bool exited,
347 int signo,
348 int exit_status);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349
350 lldb::StateType
351 SetThreadStopInfo (StringExtractor& stop_packet);
352
353 void
Greg Clayton9e920902012-04-10 02:25:43 +0000354 ClearThreadIDList ();
355
356 bool
357 UpdateThreadIDList ();
358
359 void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000360 DidLaunchOrAttach ();
361
362 lldb_private::Error
363 ConnectToDebugserver (const char *host_port);
364
365 const char *
366 GetDispatchQueueNameForThread (lldb::addr_t thread_dispatch_qaddr,
367 std::string &dispatch_queue_name);
368
369 static size_t
370 AttachInputReaderCallback (void *baton,
371 lldb_private::InputReader *reader,
372 lldb::InputReaderAction notification,
373 const char *bytes,
374 size_t bytes_len);
375
Jason Molenda5e8534e2012-10-03 01:29:34 +0000376 lldb_private::DynamicLoader *
377 GetDynamicLoader ();
378
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379private:
380 //------------------------------------------------------------------
381 // For ProcessGDBRemote only
382 //------------------------------------------------------------------
Jim Ingham1c823b42011-01-22 01:33:44 +0000383 static bool
384 NewThreadNotifyBreakpointHit (void *baton,
385 lldb_private::StoppointCallbackContext *context,
386 lldb::user_id_t break_id,
387 lldb::user_id_t break_loc_id);
388
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389 DISALLOW_COPY_AND_ASSIGN (ProcessGDBRemote);
390
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000391};
392
393#endif // liblldb_ProcessGDBRemote_h_