blob: 942b31c84dde1d309f8511754ff4afce1e2b3359 [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"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Core/StreamString.h"
Jim Ingham5aee1622010-08-09 23:31:02 +000025#include "lldb/Core/StringList.h"
Jason Molenda705b1802014-06-13 02:37:02 +000026#include "lldb/Core/StructuredData.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Core/ThreadSafeValue.h"
Todd Fiala4ceced32014-08-29 17:35:57 +000028#include "lldb/lldb-private-forward.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Target/Process.h"
30#include "lldb/Target/Thread.h"
31
Greg Clayton576d8832011-03-22 04:00:09 +000032#include "GDBRemoteCommunicationClient.h"
Greg Claytonc982c762010-07-09 20:39:50 +000033#include "Utility/StringExtractor.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034#include "GDBRemoteRegisterContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035
36class ThreadGDBRemote;
37
38class ProcessGDBRemote : public lldb_private::Process
39{
40public:
41 //------------------------------------------------------------------
42 // Constructors and Destructors
43 //------------------------------------------------------------------
Greg Claytonc3776bf2012-02-09 06:16:32 +000044 static lldb::ProcessSP
45 CreateInstance (lldb_private::Target& target,
46 lldb_private::Listener &listener,
47 const lldb_private::FileSpec *crash_file_path);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048
49 static void
50 Initialize();
51
52 static void
Greg Clayton7f982402013-07-15 22:54:20 +000053 DebuggerInitialize (lldb_private::Debugger &debugger);
54
55 static void
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056 Terminate();
57
Greg Clayton57abc5d2013-05-10 21:47:16 +000058 static lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059 GetPluginNameStatic();
60
61 static const char *
62 GetPluginDescriptionStatic();
63
64 //------------------------------------------------------------------
65 // Constructors and Destructors
66 //------------------------------------------------------------------
67 ProcessGDBRemote(lldb_private::Target& target, lldb_private::Listener &listener);
68
69 virtual
70 ~ProcessGDBRemote();
71
72 //------------------------------------------------------------------
73 // Check if a given Process
74 //------------------------------------------------------------------
75 virtual bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +000076 CanDebug (lldb_private::Target &target,
77 bool plugin_specified_by_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078
Greg Clayton998255b2012-10-13 02:07:45 +000079 virtual lldb_private::CommandObject *
80 GetPluginCommandObject();
Jim Ingham5aee1622010-08-09 23:31:02 +000081
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082 //------------------------------------------------------------------
83 // Creating a new process, or attaching to an existing one
84 //------------------------------------------------------------------
85 virtual lldb_private::Error
86 WillLaunch (lldb_private::Module* module);
87
88 virtual lldb_private::Error
Greg Clayton982c9762011-11-03 21:22:33 +000089 DoLaunch (lldb_private::Module *exe_module,
Jean-Daniel Dupas7782de92013-12-09 22:52:50 +000090 lldb_private::ProcessLaunchInfo &launch_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091
92 virtual void
93 DidLaunch ();
94
95 virtual lldb_private::Error
Greg Clayton3af9ea52010-11-18 05:57:03 +000096 WillAttachToProcessWithID (lldb::pid_t pid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097
98 virtual lldb_private::Error
Greg Clayton3af9ea52010-11-18 05:57:03 +000099 WillAttachToProcessWithName (const char *process_name, bool wait_for_launch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100
Greg Claytonb766a732011-02-04 01:58:07 +0000101 virtual lldb_private::Error
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000102 DoConnectRemote (lldb_private::Stream *strm, const char *remote_url);
Greg Claytonb766a732011-02-04 01:58:07 +0000103
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104 lldb_private::Error
105 WillLaunchOrAttach ();
106
107 virtual lldb_private::Error
Greg Claytonc982c762010-07-09 20:39:50 +0000108 DoAttachToProcessWithID (lldb::pid_t pid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109
110 virtual lldb_private::Error
Han Ming Ong84647042012-02-25 01:07:38 +0000111 DoAttachToProcessWithID (lldb::pid_t pid, const lldb_private::ProcessAttachInfo &attach_info);
112
113 virtual lldb_private::Error
114 DoAttachToProcessWithName (const char *process_name,
Han Ming Ong84647042012-02-25 01:07:38 +0000115 const lldb_private::ProcessAttachInfo &attach_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116
117 virtual void
Jim Inghambb006ce2014-08-02 00:33:35 +0000118 DidAttach (lldb_private::ArchSpec &process_arch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119
120 //------------------------------------------------------------------
121 // PluginInterface protocol
122 //------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000123 virtual lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000124 GetPluginName();
125
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000126 virtual uint32_t
127 GetPluginVersion();
128
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129 //------------------------------------------------------------------
130 // Process Control
131 //------------------------------------------------------------------
132 virtual lldb_private::Error
133 WillResume ();
134
135 virtual lldb_private::Error
136 DoResume ();
137
138 virtual lldb_private::Error
Jim Ingham0d8bcc72010-11-17 02:32:00 +0000139 DoHalt (bool &caused_stop);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140
141 virtual lldb_private::Error
Jim Inghamacff8952013-05-02 00:27:30 +0000142 DoDetach (bool keep_stopped);
Jim Ingham8af3b9c2013-03-29 01:18:12 +0000143
144 virtual bool
145 DetachRequiresHalt() { return true; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000146
147 virtual lldb_private::Error
148 DoSignal (int signal);
149
150 virtual lldb_private::Error
151 DoDestroy ();
152
153 virtual void
154 RefreshStateAfterStop();
155
156 //------------------------------------------------------------------
157 // Process Queries
158 //------------------------------------------------------------------
159 virtual bool
160 IsAlive ();
161
162 virtual lldb::addr_t
163 GetImageInfoAddress();
164
165 //------------------------------------------------------------------
166 // Process Memory
167 //------------------------------------------------------------------
168 virtual size_t
169 DoReadMemory (lldb::addr_t addr, void *buf, size_t size, lldb_private::Error &error);
170
171 virtual size_t
172 DoWriteMemory (lldb::addr_t addr, const void *buf, size_t size, lldb_private::Error &error);
173
174 virtual lldb::addr_t
175 DoAllocateMemory (size_t size, uint32_t permissions, lldb_private::Error &error);
176
177 virtual lldb_private::Error
Greg Clayton46fb5582011-11-18 07:03:08 +0000178 GetMemoryRegionInfo (lldb::addr_t load_addr,
179 lldb_private::MemoryRegionInfo &region_info);
180
181 virtual lldb_private::Error
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182 DoDeallocateMemory (lldb::addr_t ptr);
183
184 //------------------------------------------------------------------
185 // Process STDIO
186 //------------------------------------------------------------------
187 virtual size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000188 PutSTDIN (const char *buf, size_t buf_size, lldb_private::Error &error);
189
190 //----------------------------------------------------------------------
191 // Process Breakpoints
192 //----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193 virtual lldb_private::Error
Jim Ingham299c0c12013-02-15 02:06:30 +0000194 EnableBreakpointSite (lldb_private::BreakpointSite *bp_site);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195
196 virtual lldb_private::Error
Jim Ingham299c0c12013-02-15 02:06:30 +0000197 DisableBreakpointSite (lldb_private::BreakpointSite *bp_site);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000198
199 //----------------------------------------------------------------------
200 // Process Watchpoints
201 //----------------------------------------------------------------------
202 virtual lldb_private::Error
Jim Ingham1b5792e2012-12-18 02:03:49 +0000203 EnableWatchpoint (lldb_private::Watchpoint *wp, bool notify = true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204
205 virtual lldb_private::Error
Jim Ingham1b5792e2012-12-18 02:03:49 +0000206 DisableWatchpoint (lldb_private::Watchpoint *wp, bool notify = true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207
Johnny Chen64637202012-05-23 21:09:52 +0000208 virtual lldb_private::Error
209 GetWatchpointSupportInfo (uint32_t &num);
210
Enrico Granataf04a2192012-07-13 23:18:48 +0000211 virtual lldb_private::Error
212 GetWatchpointSupportInfo (uint32_t &num, bool& after);
213
Jim Ingham1c823b42011-01-22 01:33:44 +0000214 virtual bool
215 StartNoticingNewThreads();
216
217 virtual bool
218 StopNoticingNewThreads();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000219
Greg Claytonc1422c12012-04-09 22:46:21 +0000220 GDBRemoteCommunicationClient &
221 GetGDBRemote()
222 {
223 return m_gdb_comm;
224 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000225
Jason Molendaa3329782014-03-29 18:54:20 +0000226 virtual lldb_private::Error
227 SendEventData(const char *data);
228
Greg Claytonfbb76342013-11-20 21:07:01 +0000229 //----------------------------------------------------------------------
230 // Override SetExitStatus so we can disconnect from the remote GDB server
231 //----------------------------------------------------------------------
232 virtual bool
233 SetExitStatus (int exit_status, const char *cstr);
234
Jason Molenda6076bf42014-05-06 04:34:52 +0000235 void
236 SetUserSpecifiedMaxMemoryTransferSize (uint64_t user_specified_max);
237
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238protected:
239 friend class ThreadGDBRemote;
Greg Clayton576d8832011-03-22 04:00:09 +0000240 friend class GDBRemoteCommunicationClient;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000241 friend class GDBRemoteRegisterContext;
242
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000243 //----------------------------------------------------------------------
244 // Accessors
245 //----------------------------------------------------------------------
246 bool
247 IsRunning ( lldb::StateType state )
248 {
249 return state == lldb::eStateRunning || IsStepping(state);
250 }
251
252 bool
253 IsStepping ( lldb::StateType state)
254 {
255 return state == lldb::eStateStepping;
256 }
257 bool
258 CanResume ( lldb::StateType state)
259 {
260 return state == lldb::eStateStopped;
261 }
262
263 bool
264 HasExited (lldb::StateType state)
265 {
266 return state == lldb::eStateExited;
267 }
268
269 bool
270 ProcessIDIsValid ( ) const;
271
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000272 void
273 Clear ( );
274
275 lldb_private::Flags &
276 GetFlags ()
277 {
278 return m_flags;
279 }
280
281 const lldb_private::Flags &
282 GetFlags () const
283 {
284 return m_flags;
285 }
286
Greg Clayton9fc13552012-04-10 00:18:59 +0000287 virtual bool
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000288 UpdateThreadList (lldb_private::ThreadList &old_thread_list,
289 lldb_private::ThreadList &new_thread_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290
291 lldb_private::Error
Greg Clayton91a9b2472013-12-04 19:19:12 +0000292 LaunchAndConnectToDebugserver (const lldb_private::ProcessInfo &process_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000293
294 void
295 KillDebugserverProcess ();
296
297 void
Greg Clayton513c26c2011-01-29 07:10:55 +0000298 BuildDynamicRegisterInfo (bool force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000299
Greg Clayton09c3e3d2011-12-06 04:51:14 +0000300 void
Greg Clayton8cda7f02013-05-21 21:55:59 +0000301 SetLastStopPacket (const StringExtractorGDBRemote &response);
Jason Molendad1fae142012-09-29 08:03:33 +0000302
Greg Claytonef8180a2013-10-15 00:14:28 +0000303 bool
304 ParsePythonTargetDefinition(const lldb_private::FileSpec &target_definition_fspec);
305
306 bool
307 ParseRegisters(lldb_private::ScriptInterpreterObject *registers_array);
308
Todd Fialaaf245d12014-06-30 21:05:18 +0000309 const lldb::DataBufferSP
310 GetAuxvData() override;
Steve Pucci03904ac2014-03-04 23:18:46 +0000311
Jason Molenda705b1802014-06-13 02:37:02 +0000312 lldb_private::StructuredData::ObjectSP
313 GetExtendedInfoForThread (lldb::tid_t tid);
314
Jason Molenda6076bf42014-05-06 04:34:52 +0000315 void
316 GetMaxMemorySize();
317
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000318 //------------------------------------------------------------------
319 /// Broadcaster event bits definitions.
320 //------------------------------------------------------------------
321 enum
322 {
323 eBroadcastBitAsyncContinue = (1 << 0),
Jim Inghamb1e2e842012-04-12 18:49:31 +0000324 eBroadcastBitAsyncThreadShouldExit = (1 << 1),
325 eBroadcastBitAsyncThreadDidExit = (1 << 2)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000326 };
327
Jim Ingham455fa5c2012-11-01 01:15:33 +0000328 typedef enum AsyncThreadState
329 {
330 eAsyncThreadNotStarted,
331 eAsyncThreadRunning,
332 eAsyncThreadDone
333 } AsyncThreadState;
334
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335 lldb_private::Flags m_flags; // Process specific flags (see eFlags enums)
Greg Clayton576d8832011-03-22 04:00:09 +0000336 GDBRemoteCommunicationClient m_gdb_comm;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337 lldb::pid_t m_debugserver_pid;
Greg Claytondd0e5a52011-06-02 22:22:38 +0000338 StringExtractorGDBRemote m_last_stop_packet;
Greg Clayton09c3e3d2011-12-06 04:51:14 +0000339 lldb_private::Mutex m_last_stop_packet_mutex;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340 GDBRemoteDynamicRegisterInfo m_register_info;
341 lldb_private::Broadcaster m_async_broadcaster;
342 lldb::thread_t m_async_thread;
Jim Ingham455fa5c2012-11-01 01:15:33 +0000343 AsyncThreadState m_async_thread_state;
344 lldb_private::Mutex m_async_thread_state_mutex;
Greg Clayton71fc2a32011-02-12 06:28:37 +0000345 typedef std::vector<lldb::tid_t> tid_collection;
346 typedef std::vector< std::pair<lldb::tid_t,int> > tid_sig_collection;
Greg Clayton70b57652011-05-15 01:25:55 +0000347 typedef std::map<lldb::addr_t, lldb::addr_t> MMapMap;
Greg Clayton9e920902012-04-10 02:25:43 +0000348 tid_collection m_thread_ids; // Thread IDs for all threads. This list gets updated after stopping
Greg Clayton71fc2a32011-02-12 06:28:37 +0000349 tid_collection m_continue_c_tids; // 'c' for continue
350 tid_sig_collection m_continue_C_tids; // 'C' for continue with signal
351 tid_collection m_continue_s_tids; // 's' for step
352 tid_sig_collection m_continue_S_tids; // 'S' for step with signal
Jason Molenda6076bf42014-05-06 04:34:52 +0000353 uint64_t m_max_memory_size; // The maximum number of bytes to read/write when reading and writing memory
354 uint64_t m_remote_stub_max_memory_size; // The maximum memory size the remote gdb stub can handle
Greg Clayton70b57652011-05-15 01:25:55 +0000355 MMapMap m_addr_to_mmap_size;
Greg Clayton4116e932012-05-15 02:33:01 +0000356 lldb::BreakpointSP m_thread_create_bp_sp;
357 bool m_waiting_for_attach;
Jim Ingham43c555d2012-07-04 00:35:43 +0000358 bool m_destroy_tried_resuming;
Greg Clayton998255b2012-10-13 02:07:45 +0000359 lldb::CommandObjectSP m_command_sp;
Hafiz Abid Qadeer85a4daf2013-10-18 10:04:33 +0000360 int64_t m_breakpoint_pc_offset;
Todd Fialaaf245d12014-06-30 21:05:18 +0000361
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000362 bool
363 StartAsyncThread ();
364
365 void
366 StopAsyncThread ();
367
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000368 static lldb::thread_result_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369 AsyncThread (void *arg);
370
371 static bool
372 MonitorDebugserverProcess (void *callback_baton,
373 lldb::pid_t pid,
Greg Claytone4e45922011-11-16 05:37:56 +0000374 bool exited,
375 int signo,
376 int exit_status);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377
378 lldb::StateType
379 SetThreadStopInfo (StringExtractor& stop_packet);
380
381 void
Greg Clayton9e920902012-04-10 02:25:43 +0000382 ClearThreadIDList ();
383
384 bool
385 UpdateThreadIDList ();
386
387 void
Jim Inghambb006ce2014-08-02 00:33:35 +0000388 DidLaunchOrAttach (lldb_private::ArchSpec& process_arch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389
390 lldb_private::Error
391 ConnectToDebugserver (const char *host_port);
392
393 const char *
394 GetDispatchQueueNameForThread (lldb::addr_t thread_dispatch_qaddr,
395 std::string &dispatch_queue_name);
396
Jason Molenda5e8534e2012-10-03 01:29:34 +0000397 lldb_private::DynamicLoader *
398 GetDynamicLoader ();
399
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400private:
401 //------------------------------------------------------------------
402 // For ProcessGDBRemote only
403 //------------------------------------------------------------------
Jim Ingham1c823b42011-01-22 01:33:44 +0000404 static bool
405 NewThreadNotifyBreakpointHit (void *baton,
406 lldb_private::StoppointCallbackContext *context,
407 lldb::user_id_t break_id,
408 lldb::user_id_t break_loc_id);
409
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410 DISALLOW_COPY_AND_ASSIGN (ProcessGDBRemote);
411
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412};
413
414#endif // liblldb_ProcessGDBRemote_h_