blob: 4a716157943f7cc324773ed46a39dfd55487eb46 [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>
Greg Claytonc1f45872011-02-12 06:28:37 +000017#include <vector>
Chris Lattner24943d22010-06-08 16:52:24 +000018
19// Other libraries and framework includes
20#include "lldb/Core/ArchSpec.h"
21#include "lldb/Core/Broadcaster.h"
22#include "lldb/Core/Error.h"
23#include "lldb/Core/InputReader.h"
24#include "lldb/Core/StreamString.h"
Jim Ingham7508e732010-08-09 23:31:02 +000025#include "lldb/Core/StringList.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026#include "lldb/Core/ThreadSafeValue.h"
27#include "lldb/Target/Process.h"
28#include "lldb/Target/Thread.h"
29
Greg Clayton61d043b2011-03-22 04:00:09 +000030#include "GDBRemoteCommunicationClient.h"
Greg Clayton54e7afa2010-07-09 20:39:50 +000031#include "Utility/StringExtractor.h"
Chris Lattner24943d22010-06-08 16:52:24 +000032#include "GDBRemoteRegisterContext.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 //------------------------------------------------------------------
Greg Clayton46c9a352012-02-09 06:16:32 +000042 static lldb::ProcessSP
43 CreateInstance (lldb_private::Target& target,
44 lldb_private::Listener &listener,
45 const lldb_private::FileSpec *crash_file_path);
Chris Lattner24943d22010-06-08 16:52:24 +000046
47 static void
48 Initialize();
49
50 static void
51 Terminate();
52
53 static const char *
54 GetPluginNameStatic();
55
56 static const char *
57 GetPluginDescriptionStatic();
58
59 //------------------------------------------------------------------
60 // Constructors and Destructors
61 //------------------------------------------------------------------
62 ProcessGDBRemote(lldb_private::Target& target, lldb_private::Listener &listener);
63
64 virtual
65 ~ProcessGDBRemote();
66
67 //------------------------------------------------------------------
68 // Check if a given Process
69 //------------------------------------------------------------------
70 virtual bool
Greg Clayton8d2ea282011-07-17 20:36:25 +000071 CanDebug (lldb_private::Target &target,
72 bool plugin_specified_by_name);
Chris Lattner24943d22010-06-08 16:52:24 +000073
Greg Claytone4b9c1f2011-03-08 22:40:15 +000074// virtual uint32_t
75// ListProcessesMatchingName (const char *name, lldb_private::StringList &matches, std::vector<lldb::pid_t> &pids);
Jim Ingham7508e732010-08-09 23:31:02 +000076
Chris Lattner24943d22010-06-08 16:52:24 +000077 //------------------------------------------------------------------
78 // Creating a new process, or attaching to an existing one
79 //------------------------------------------------------------------
80 virtual lldb_private::Error
81 WillLaunch (lldb_private::Module* module);
82
83 virtual lldb_private::Error
Greg Clayton36bc5ea2011-11-03 21:22:33 +000084 DoLaunch (lldb_private::Module *exe_module,
85 const lldb_private::ProcessLaunchInfo &launch_info);
Chris Lattner24943d22010-06-08 16:52:24 +000086
87 virtual void
88 DidLaunch ();
89
90 virtual lldb_private::Error
Greg Clayton20d338f2010-11-18 05:57:03 +000091 WillAttachToProcessWithID (lldb::pid_t pid);
Chris Lattner24943d22010-06-08 16:52:24 +000092
93 virtual lldb_private::Error
Greg Clayton20d338f2010-11-18 05:57:03 +000094 WillAttachToProcessWithName (const char *process_name, bool wait_for_launch);
Chris Lattner24943d22010-06-08 16:52:24 +000095
Greg Claytone71e2582011-02-04 01:58:07 +000096 virtual lldb_private::Error
Jason Molendafac2e622012-09-29 04:02:01 +000097 DoConnectRemote (lldb_private::Stream *strm, const char *remote_url);
Greg Claytone71e2582011-02-04 01:58:07 +000098
Chris Lattner24943d22010-06-08 16:52:24 +000099 lldb_private::Error
100 WillLaunchOrAttach ();
101
102 virtual lldb_private::Error
Greg Clayton54e7afa2010-07-09 20:39:50 +0000103 DoAttachToProcessWithID (lldb::pid_t pid);
Chris Lattner24943d22010-06-08 16:52:24 +0000104
105 virtual lldb_private::Error
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000106 DoAttachToProcessWithID (lldb::pid_t pid, const lldb_private::ProcessAttachInfo &attach_info);
107
108 virtual lldb_private::Error
109 DoAttachToProcessWithName (const char *process_name,
110 bool wait_for_launch,
111 const lldb_private::ProcessAttachInfo &attach_info);
Chris Lattner24943d22010-06-08 16:52:24 +0000112
113 virtual void
114 DidAttach ();
115
116 //------------------------------------------------------------------
117 // PluginInterface protocol
118 //------------------------------------------------------------------
119 virtual const char *
120 GetPluginName();
121
122 virtual const char *
123 GetShortPluginName();
124
125 virtual uint32_t
126 GetPluginVersion();
127
Chris Lattner24943d22010-06-08 16:52:24 +0000128 //------------------------------------------------------------------
129 // Process Control
130 //------------------------------------------------------------------
131 virtual lldb_private::Error
132 WillResume ();
133
134 virtual lldb_private::Error
135 DoResume ();
136
137 virtual lldb_private::Error
Jim Ingham3ae449a2010-11-17 02:32:00 +0000138 DoHalt (bool &caused_stop);
Chris Lattner24943d22010-06-08 16:52:24 +0000139
140 virtual lldb_private::Error
141 WillDetach ();
142
143 virtual lldb_private::Error
144 DoDetach ();
145
146 virtual lldb_private::Error
147 DoSignal (int signal);
148
149 virtual lldb_private::Error
150 DoDestroy ();
151
152 virtual void
153 RefreshStateAfterStop();
154
155 //------------------------------------------------------------------
156 // Process Queries
157 //------------------------------------------------------------------
158 virtual bool
159 IsAlive ();
160
161 virtual lldb::addr_t
162 GetImageInfoAddress();
163
164 //------------------------------------------------------------------
165 // Process Memory
166 //------------------------------------------------------------------
167 virtual size_t
168 DoReadMemory (lldb::addr_t addr, void *buf, size_t size, lldb_private::Error &error);
169
170 virtual size_t
171 DoWriteMemory (lldb::addr_t addr, const void *buf, size_t size, lldb_private::Error &error);
172
173 virtual lldb::addr_t
174 DoAllocateMemory (size_t size, uint32_t permissions, lldb_private::Error &error);
175
176 virtual lldb_private::Error
Greg Claytona9385532011-11-18 07:03:08 +0000177 GetMemoryRegionInfo (lldb::addr_t load_addr,
178 lldb_private::MemoryRegionInfo &region_info);
179
180 virtual lldb_private::Error
Chris Lattner24943d22010-06-08 16:52:24 +0000181 DoDeallocateMemory (lldb::addr_t ptr);
182
183 //------------------------------------------------------------------
184 // Process STDIO
185 //------------------------------------------------------------------
186 virtual size_t
Chris Lattner24943d22010-06-08 16:52:24 +0000187 PutSTDIN (const char *buf, size_t buf_size, lldb_private::Error &error);
188
189 //----------------------------------------------------------------------
190 // Process Breakpoints
191 //----------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +0000192 virtual lldb_private::Error
193 EnableBreakpoint (lldb_private::BreakpointSite *bp_site);
194
195 virtual lldb_private::Error
196 DisableBreakpoint (lldb_private::BreakpointSite *bp_site);
197
198 //----------------------------------------------------------------------
199 // Process Watchpoints
200 //----------------------------------------------------------------------
201 virtual lldb_private::Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000202 EnableWatchpoint (lldb_private::Watchpoint *wp);
Chris Lattner24943d22010-06-08 16:52:24 +0000203
204 virtual lldb_private::Error
Johnny Chenecd4feb2011-10-14 00:42:25 +0000205 DisableWatchpoint (lldb_private::Watchpoint *wp);
Chris Lattner24943d22010-06-08 16:52:24 +0000206
Johnny Chen7cbdcfb2012-05-23 21:09:52 +0000207 virtual lldb_private::Error
208 GetWatchpointSupportInfo (uint32_t &num);
209
Enrico Granata7de2a3b2012-07-13 23:18:48 +0000210 virtual lldb_private::Error
211 GetWatchpointSupportInfo (uint32_t &num, bool& after);
212
Jim Ingham55e01d82011-01-22 01:33:44 +0000213 virtual bool
214 StartNoticingNewThreads();
215
216 virtual bool
217 StopNoticingNewThreads();
Chris Lattner24943d22010-06-08 16:52:24 +0000218
Greg Clayton451fa822012-04-09 22:46:21 +0000219 GDBRemoteCommunicationClient &
220 GetGDBRemote()
221 {
222 return m_gdb_comm;
223 }
224
Chris Lattner24943d22010-06-08 16:52:24 +0000225protected:
226 friend class ThreadGDBRemote;
Greg Clayton61d043b2011-03-22 04:00:09 +0000227 friend class GDBRemoteCommunicationClient;
Chris Lattner24943d22010-06-08 16:52:24 +0000228 friend class GDBRemoteRegisterContext;
229
Chris Lattner24943d22010-06-08 16:52:24 +0000230 //----------------------------------------------------------------------
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
Chris Lattner24943d22010-06-08 16:52:24 +0000259 void
260 Clear ( );
261
262 lldb_private::Flags &
263 GetFlags ()
264 {
265 return m_flags;
266 }
267
268 const lldb_private::Flags &
269 GetFlags () const
270 {
271 return m_flags;
272 }
273
Greg Claytonae932352012-04-10 00:18:59 +0000274 virtual bool
Greg Clayton37f962e2011-08-22 02:49:39 +0000275 UpdateThreadList (lldb_private::ThreadList &old_thread_list,
276 lldb_private::ThreadList &new_thread_list);
Chris Lattner24943d22010-06-08 16:52:24 +0000277
278 lldb_private::Error
Greg Claytonb72d0f02011-04-12 05:54:46 +0000279 StartDebugserverProcess (const char *debugserver_url);
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000280
281 lldb_private::Error
282 StartDebugserverProcess (const char *debugserver_url, const lldb_private::ProcessInfo &process_info);
Chris Lattner24943d22010-06-08 16:52:24 +0000283
284 void
285 KillDebugserverProcess ();
286
287 void
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000288 BuildDynamicRegisterInfo (bool force);
Chris Lattner24943d22010-06-08 16:52:24 +0000289
Greg Clayton06709002011-12-06 04:51:14 +0000290 void
291 SetLastStopPacket (const StringExtractorGDBRemote &response)
292 {
293 lldb_private::Mutex::Locker locker (m_last_stop_packet_mutex);
294 m_last_stop_packet = response;
295 }
Chris Lattner24943d22010-06-08 16:52:24 +0000296 //------------------------------------------------------------------
297 /// Broadcaster event bits definitions.
298 //------------------------------------------------------------------
299 enum
300 {
301 eBroadcastBitAsyncContinue = (1 << 0),
Jim Ingham7fa7b2f2012-04-12 18:49:31 +0000302 eBroadcastBitAsyncThreadShouldExit = (1 << 1),
303 eBroadcastBitAsyncThreadDidExit = (1 << 2)
Chris Lattner24943d22010-06-08 16:52:24 +0000304 };
305
Chris Lattner24943d22010-06-08 16:52:24 +0000306 lldb_private::Flags m_flags; // Process specific flags (see eFlags enums)
Greg Clayton61d043b2011-03-22 04:00:09 +0000307 GDBRemoteCommunicationClient m_gdb_comm;
Chris Lattner24943d22010-06-08 16:52:24 +0000308 lldb::pid_t m_debugserver_pid;
Greg Clayton261a18b2011-06-02 22:22:38 +0000309 StringExtractorGDBRemote m_last_stop_packet;
Greg Clayton06709002011-12-06 04:51:14 +0000310 lldb_private::Mutex m_last_stop_packet_mutex;
Chris Lattner24943d22010-06-08 16:52:24 +0000311 GDBRemoteDynamicRegisterInfo m_register_info;
312 lldb_private::Broadcaster m_async_broadcaster;
313 lldb::thread_t m_async_thread;
Greg Claytonc1f45872011-02-12 06:28:37 +0000314 typedef std::vector<lldb::tid_t> tid_collection;
315 typedef std::vector< std::pair<lldb::tid_t,int> > tid_sig_collection;
Greg Clayton2f085c62011-05-15 01:25:55 +0000316 typedef std::map<lldb::addr_t, lldb::addr_t> MMapMap;
Greg Clayton5a9f85c2012-04-10 02:25:43 +0000317 tid_collection m_thread_ids; // Thread IDs for all threads. This list gets updated after stopping
Greg Claytonc1f45872011-02-12 06:28:37 +0000318 tid_collection m_continue_c_tids; // 'c' for continue
319 tid_sig_collection m_continue_C_tids; // 'C' for continue with signal
320 tid_collection m_continue_s_tids; // 's' for step
321 tid_sig_collection m_continue_S_tids; // 'S' for step with signal
Chris Lattner24943d22010-06-08 16:52:24 +0000322 lldb::addr_t m_dispatch_queue_offsets_addr;
Chris Lattner24943d22010-06-08 16:52:24 +0000323 size_t m_max_memory_size; // The maximum number of bytes to read/write when reading and writing memory
Greg Clayton2f085c62011-05-15 01:25:55 +0000324 MMapMap m_addr_to_mmap_size;
Greg Claytonbd5c23d2012-05-15 02:33:01 +0000325 lldb::BreakpointSP m_thread_create_bp_sp;
326 bool m_waiting_for_attach;
Jim Ingham06b84492012-07-04 00:35:43 +0000327 bool m_destroy_tried_resuming;
Greg Claytonbd5c23d2012-05-15 02:33:01 +0000328
Chris Lattner24943d22010-06-08 16:52:24 +0000329 bool
330 StartAsyncThread ();
331
332 void
333 StopAsyncThread ();
334
335 static void *
336 AsyncThread (void *arg);
337
338 static bool
339 MonitorDebugserverProcess (void *callback_baton,
340 lldb::pid_t pid,
Greg Clayton1c4642c2011-11-16 05:37:56 +0000341 bool exited,
342 int signo,
343 int exit_status);
Chris Lattner24943d22010-06-08 16:52:24 +0000344
345 lldb::StateType
346 SetThreadStopInfo (StringExtractor& stop_packet);
347
348 void
Greg Clayton5a9f85c2012-04-10 02:25:43 +0000349 ClearThreadIDList ();
350
351 bool
352 UpdateThreadIDList ();
353
354 void
Chris Lattner24943d22010-06-08 16:52:24 +0000355 DidLaunchOrAttach ();
356
357 lldb_private::Error
358 ConnectToDebugserver (const char *host_port);
359
360 const char *
361 GetDispatchQueueNameForThread (lldb::addr_t thread_dispatch_qaddr,
362 std::string &dispatch_queue_name);
363
364 static size_t
365 AttachInputReaderCallback (void *baton,
366 lldb_private::InputReader *reader,
367 lldb::InputReaderAction notification,
368 const char *bytes,
369 size_t bytes_len);
370
Greg Clayton72e1c782011-01-22 23:43:18 +0000371 lldb_private::Error
372 InterruptIfRunning (bool discard_thread_plans,
373 bool catch_stop_event,
Greg Clayton72e1c782011-01-22 23:43:18 +0000374 lldb::EventSP &stop_event_sp);
375
Chris Lattner24943d22010-06-08 16:52:24 +0000376private:
377 //------------------------------------------------------------------
378 // For ProcessGDBRemote only
379 //------------------------------------------------------------------
Jim Ingham55e01d82011-01-22 01:33:44 +0000380 static bool
381 NewThreadNotifyBreakpointHit (void *baton,
382 lldb_private::StoppointCallbackContext *context,
383 lldb::user_id_t break_id,
384 lldb::user_id_t break_loc_id);
385
Chris Lattner24943d22010-06-08 16:52:24 +0000386 DISALLOW_COPY_AND_ASSIGN (ProcessGDBRemote);
387
Chris Lattner24943d22010-06-08 16:52:24 +0000388};
389
390#endif // liblldb_ProcessGDBRemote_h_