blob: 5cbb13f49146b5cc8ad35f319af0db8840b6167f [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 Clayton13193d52012-10-13 02:07:45 +000074 virtual lldb_private::CommandObject *
75 GetPluginCommandObject();
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
Greg Clayton0bce9a22012-12-05 00:16:59 +0000116 virtual void
117 DoDidExec ();
118
Chris Lattner24943d22010-06-08 16:52:24 +0000119 //------------------------------------------------------------------
120 // PluginInterface protocol
121 //------------------------------------------------------------------
122 virtual const char *
123 GetPluginName();
124
125 virtual const char *
126 GetShortPluginName();
127
128 virtual uint32_t
129 GetPluginVersion();
130
Chris Lattner24943d22010-06-08 16:52:24 +0000131 //------------------------------------------------------------------
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
Jim Ingham761afb82013-05-02 00:27:30 +0000144 DoDetach (bool keep_stopped);
Jim Inghame33bb5b2013-03-29 01:18:12 +0000145
146 virtual bool
147 DetachRequiresHalt() { return true; }
Chris Lattner24943d22010-06-08 16:52:24 +0000148
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
Greg Claytona9385532011-11-18 07:03:08 +0000180 GetMemoryRegionInfo (lldb::addr_t load_addr,
181 lldb_private::MemoryRegionInfo &region_info);
182
183 virtual lldb_private::Error
Chris Lattner24943d22010-06-08 16:52:24 +0000184 DoDeallocateMemory (lldb::addr_t ptr);
185
186 //------------------------------------------------------------------
187 // Process STDIO
188 //------------------------------------------------------------------
189 virtual size_t
Chris Lattner24943d22010-06-08 16:52:24 +0000190 PutSTDIN (const char *buf, size_t buf_size, lldb_private::Error &error);
191
192 //----------------------------------------------------------------------
193 // Process Breakpoints
194 //----------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +0000195 virtual lldb_private::Error
Jim Inghamefb4aeb2013-02-15 02:06:30 +0000196 EnableBreakpointSite (lldb_private::BreakpointSite *bp_site);
Chris Lattner24943d22010-06-08 16:52:24 +0000197
198 virtual lldb_private::Error
Jim Inghamefb4aeb2013-02-15 02:06:30 +0000199 DisableBreakpointSite (lldb_private::BreakpointSite *bp_site);
Chris Lattner24943d22010-06-08 16:52:24 +0000200
201 //----------------------------------------------------------------------
202 // Process Watchpoints
203 //----------------------------------------------------------------------
204 virtual lldb_private::Error
Jim Ingham9c970a32012-12-18 02:03:49 +0000205 EnableWatchpoint (lldb_private::Watchpoint *wp, bool notify = true);
Chris Lattner24943d22010-06-08 16:52:24 +0000206
207 virtual lldb_private::Error
Jim Ingham9c970a32012-12-18 02:03:49 +0000208 DisableWatchpoint (lldb_private::Watchpoint *wp, bool notify = true);
Chris Lattner24943d22010-06-08 16:52:24 +0000209
Johnny Chen7cbdcfb2012-05-23 21:09:52 +0000210 virtual lldb_private::Error
211 GetWatchpointSupportInfo (uint32_t &num);
212
Enrico Granata7de2a3b2012-07-13 23:18:48 +0000213 virtual lldb_private::Error
214 GetWatchpointSupportInfo (uint32_t &num, bool& after);
215
Jim Ingham55e01d82011-01-22 01:33:44 +0000216 virtual bool
217 StartNoticingNewThreads();
218
219 virtual bool
220 StopNoticingNewThreads();
Chris Lattner24943d22010-06-08 16:52:24 +0000221
Greg Clayton451fa822012-04-09 22:46:21 +0000222 GDBRemoteCommunicationClient &
223 GetGDBRemote()
224 {
225 return m_gdb_comm;
226 }
227
Chris Lattner24943d22010-06-08 16:52:24 +0000228protected:
229 friend class ThreadGDBRemote;
Greg Clayton61d043b2011-03-22 04:00:09 +0000230 friend class GDBRemoteCommunicationClient;
Chris Lattner24943d22010-06-08 16:52:24 +0000231 friend class GDBRemoteRegisterContext;
232
Chris Lattner24943d22010-06-08 16:52:24 +0000233 //----------------------------------------------------------------------
234 // Accessors
235 //----------------------------------------------------------------------
236 bool
237 IsRunning ( lldb::StateType state )
238 {
239 return state == lldb::eStateRunning || IsStepping(state);
240 }
241
242 bool
243 IsStepping ( lldb::StateType state)
244 {
245 return state == lldb::eStateStepping;
246 }
247 bool
248 CanResume ( lldb::StateType state)
249 {
250 return state == lldb::eStateStopped;
251 }
252
253 bool
254 HasExited (lldb::StateType state)
255 {
256 return state == lldb::eStateExited;
257 }
258
259 bool
260 ProcessIDIsValid ( ) const;
261
Chris Lattner24943d22010-06-08 16:52:24 +0000262 void
263 Clear ( );
264
265 lldb_private::Flags &
266 GetFlags ()
267 {
268 return m_flags;
269 }
270
271 const lldb_private::Flags &
272 GetFlags () const
273 {
274 return m_flags;
275 }
276
Greg Claytonae932352012-04-10 00:18:59 +0000277 virtual bool
Greg Clayton37f962e2011-08-22 02:49:39 +0000278 UpdateThreadList (lldb_private::ThreadList &old_thread_list,
279 lldb_private::ThreadList &new_thread_list);
Chris Lattner24943d22010-06-08 16:52:24 +0000280
281 lldb_private::Error
Greg Claytonb72d0f02011-04-12 05:54:46 +0000282 StartDebugserverProcess (const char *debugserver_url);
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000283
284 lldb_private::Error
285 StartDebugserverProcess (const char *debugserver_url, const lldb_private::ProcessInfo &process_info);
Chris Lattner24943d22010-06-08 16:52:24 +0000286
287 void
288 KillDebugserverProcess ();
289
290 void
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000291 BuildDynamicRegisterInfo (bool force);
Chris Lattner24943d22010-06-08 16:52:24 +0000292
Greg Clayton06709002011-12-06 04:51:14 +0000293 void
294 SetLastStopPacket (const StringExtractorGDBRemote &response)
295 {
296 lldb_private::Mutex::Locker locker (m_last_stop_packet_mutex);
297 m_last_stop_packet = response;
298 }
Jason Molendab0e3c7c2012-09-29 08:03:33 +0000299
Chris Lattner24943d22010-06-08 16:52:24 +0000300 //------------------------------------------------------------------
301 /// Broadcaster event bits definitions.
302 //------------------------------------------------------------------
303 enum
304 {
305 eBroadcastBitAsyncContinue = (1 << 0),
Jim Ingham7fa7b2f2012-04-12 18:49:31 +0000306 eBroadcastBitAsyncThreadShouldExit = (1 << 1),
307 eBroadcastBitAsyncThreadDidExit = (1 << 2)
Chris Lattner24943d22010-06-08 16:52:24 +0000308 };
309
Jim Inghama9488302012-11-01 01:15:33 +0000310 typedef enum AsyncThreadState
311 {
312 eAsyncThreadNotStarted,
313 eAsyncThreadRunning,
314 eAsyncThreadDone
315 } AsyncThreadState;
316
Chris Lattner24943d22010-06-08 16:52:24 +0000317 lldb_private::Flags m_flags; // Process specific flags (see eFlags enums)
Greg Clayton61d043b2011-03-22 04:00:09 +0000318 GDBRemoteCommunicationClient m_gdb_comm;
Chris Lattner24943d22010-06-08 16:52:24 +0000319 lldb::pid_t m_debugserver_pid;
Greg Clayton261a18b2011-06-02 22:22:38 +0000320 StringExtractorGDBRemote m_last_stop_packet;
Greg Clayton06709002011-12-06 04:51:14 +0000321 lldb_private::Mutex m_last_stop_packet_mutex;
Chris Lattner24943d22010-06-08 16:52:24 +0000322 GDBRemoteDynamicRegisterInfo m_register_info;
323 lldb_private::Broadcaster m_async_broadcaster;
324 lldb::thread_t m_async_thread;
Jim Inghama9488302012-11-01 01:15:33 +0000325 AsyncThreadState m_async_thread_state;
326 lldb_private::Mutex m_async_thread_state_mutex;
Greg Claytonc1f45872011-02-12 06:28:37 +0000327 typedef std::vector<lldb::tid_t> tid_collection;
328 typedef std::vector< std::pair<lldb::tid_t,int> > tid_sig_collection;
Greg Clayton2f085c62011-05-15 01:25:55 +0000329 typedef std::map<lldb::addr_t, lldb::addr_t> MMapMap;
Greg Clayton5a9f85c2012-04-10 02:25:43 +0000330 tid_collection m_thread_ids; // Thread IDs for all threads. This list gets updated after stopping
Greg Claytonc1f45872011-02-12 06:28:37 +0000331 tid_collection m_continue_c_tids; // 'c' for continue
332 tid_sig_collection m_continue_C_tids; // 'C' for continue with signal
333 tid_collection m_continue_s_tids; // 's' for step
334 tid_sig_collection m_continue_S_tids; // 'S' for step with signal
Chris Lattner24943d22010-06-08 16:52:24 +0000335 lldb::addr_t m_dispatch_queue_offsets_addr;
Chris Lattner24943d22010-06-08 16:52:24 +0000336 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 +0000337 MMapMap m_addr_to_mmap_size;
Greg Claytonbd5c23d2012-05-15 02:33:01 +0000338 lldb::BreakpointSP m_thread_create_bp_sp;
339 bool m_waiting_for_attach;
Jim Ingham06b84492012-07-04 00:35:43 +0000340 bool m_destroy_tried_resuming;
Jason Molendab46937c2012-10-03 01:29:34 +0000341 std::string m_dyld_plugin_name;
Greg Clayton13193d52012-10-13 02:07:45 +0000342 lldb::CommandObjectSP m_command_sp;
Greg Claytonbd5c23d2012-05-15 02:33:01 +0000343
Chris Lattner24943d22010-06-08 16:52:24 +0000344 bool
345 StartAsyncThread ();
346
347 void
348 StopAsyncThread ();
349
350 static void *
351 AsyncThread (void *arg);
352
353 static bool
354 MonitorDebugserverProcess (void *callback_baton,
355 lldb::pid_t pid,
Greg Clayton1c4642c2011-11-16 05:37:56 +0000356 bool exited,
357 int signo,
358 int exit_status);
Chris Lattner24943d22010-06-08 16:52:24 +0000359
360 lldb::StateType
361 SetThreadStopInfo (StringExtractor& stop_packet);
362
363 void
Greg Clayton5a9f85c2012-04-10 02:25:43 +0000364 ClearThreadIDList ();
365
366 bool
367 UpdateThreadIDList ();
368
369 void
Chris Lattner24943d22010-06-08 16:52:24 +0000370 DidLaunchOrAttach ();
371
372 lldb_private::Error
373 ConnectToDebugserver (const char *host_port);
374
375 const char *
376 GetDispatchQueueNameForThread (lldb::addr_t thread_dispatch_qaddr,
377 std::string &dispatch_queue_name);
378
379 static size_t
380 AttachInputReaderCallback (void *baton,
381 lldb_private::InputReader *reader,
382 lldb::InputReaderAction notification,
383 const char *bytes,
384 size_t bytes_len);
385
Jason Molendab46937c2012-10-03 01:29:34 +0000386 lldb_private::DynamicLoader *
387 GetDynamicLoader ();
388
Chris Lattner24943d22010-06-08 16:52:24 +0000389private:
390 //------------------------------------------------------------------
391 // For ProcessGDBRemote only
392 //------------------------------------------------------------------
Jim Ingham55e01d82011-01-22 01:33:44 +0000393 static bool
394 NewThreadNotifyBreakpointHit (void *baton,
395 lldb_private::StoppointCallbackContext *context,
396 lldb::user_id_t break_id,
397 lldb::user_id_t break_loc_id);
398
Chris Lattner24943d22010-06-08 16:52:24 +0000399 DISALLOW_COPY_AND_ASSIGN (ProcessGDBRemote);
400
Chris Lattner24943d22010-06-08 16:52:24 +0000401};
402
403#endif // liblldb_ProcessGDBRemote_h_