blob: 1547cb67af5f1800fc2ff8f54fa98ea5495a04f1 [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"
Chris Lattner30fdc8d2010-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 Clayton576d8832011-03-22 04:00:09 +000030#include "GDBRemoteCommunicationClient.h"
Greg Claytonc982c762010-07-09 20:39:50 +000031#include "Utility/StringExtractor.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "GDBRemoteRegisterContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033
34class ThreadGDBRemote;
35
36class ProcessGDBRemote : public lldb_private::Process
37{
38public:
39 //------------------------------------------------------------------
40 // Constructors and Destructors
41 //------------------------------------------------------------------
Greg Claytonc3776bf2012-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 Lattner30fdc8d2010-06-08 16:52:24 +000046
47 static void
48 Initialize();
49
50 static void
Greg Clayton7f982402013-07-15 22:54:20 +000051 DebuggerInitialize (lldb_private::Debugger &debugger);
52
53 static void
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054 Terminate();
55
Greg Clayton57abc5d2013-05-10 21:47:16 +000056 static lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057 GetPluginNameStatic();
58
59 static const char *
60 GetPluginDescriptionStatic();
61
62 //------------------------------------------------------------------
63 // Constructors and Destructors
64 //------------------------------------------------------------------
65 ProcessGDBRemote(lldb_private::Target& target, lldb_private::Listener &listener);
66
67 virtual
68 ~ProcessGDBRemote();
69
70 //------------------------------------------------------------------
71 // Check if a given Process
72 //------------------------------------------------------------------
73 virtual bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +000074 CanDebug (lldb_private::Target &target,
75 bool plugin_specified_by_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000076
Greg Clayton998255b2012-10-13 02:07:45 +000077 virtual lldb_private::CommandObject *
78 GetPluginCommandObject();
Jim Ingham5aee1622010-08-09 23:31:02 +000079
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080 //------------------------------------------------------------------
81 // Creating a new process, or attaching to an existing one
82 //------------------------------------------------------------------
83 virtual lldb_private::Error
84 WillLaunch (lldb_private::Module* module);
85
86 virtual lldb_private::Error
Greg Clayton982c9762011-11-03 21:22:33 +000087 DoLaunch (lldb_private::Module *exe_module,
Jean-Daniel Dupas7782de92013-12-09 22:52:50 +000088 lldb_private::ProcessLaunchInfo &launch_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089
90 virtual void
91 DidLaunch ();
92
93 virtual lldb_private::Error
Greg Clayton3af9ea52010-11-18 05:57:03 +000094 WillAttachToProcessWithID (lldb::pid_t pid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095
96 virtual lldb_private::Error
Greg Clayton3af9ea52010-11-18 05:57:03 +000097 WillAttachToProcessWithName (const char *process_name, bool wait_for_launch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098
Greg Claytonb766a732011-02-04 01:58:07 +000099 virtual lldb_private::Error
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000100 DoConnectRemote (lldb_private::Stream *strm, const char *remote_url);
Greg Claytonb766a732011-02-04 01:58:07 +0000101
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102 lldb_private::Error
103 WillLaunchOrAttach ();
104
105 virtual lldb_private::Error
Greg Claytonc982c762010-07-09 20:39:50 +0000106 DoAttachToProcessWithID (lldb::pid_t pid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107
108 virtual lldb_private::Error
Han Ming Ong84647042012-02-25 01:07:38 +0000109 DoAttachToProcessWithID (lldb::pid_t pid, const lldb_private::ProcessAttachInfo &attach_info);
110
111 virtual lldb_private::Error
112 DoAttachToProcessWithName (const char *process_name,
Han Ming Ong84647042012-02-25 01:07:38 +0000113 const lldb_private::ProcessAttachInfo &attach_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114
115 virtual void
116 DidAttach ();
117
118 //------------------------------------------------------------------
119 // PluginInterface protocol
120 //------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000121 virtual lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122 GetPluginName();
123
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000124 virtual uint32_t
125 GetPluginVersion();
126
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000127 //------------------------------------------------------------------
128 // Process Control
129 //------------------------------------------------------------------
130 virtual lldb_private::Error
131 WillResume ();
132
133 virtual lldb_private::Error
134 DoResume ();
135
136 virtual lldb_private::Error
Jim Ingham0d8bcc72010-11-17 02:32:00 +0000137 DoHalt (bool &caused_stop);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138
139 virtual lldb_private::Error
Jim Inghamacff8952013-05-02 00:27:30 +0000140 DoDetach (bool keep_stopped);
Jim Ingham8af3b9c2013-03-29 01:18:12 +0000141
142 virtual bool
143 DetachRequiresHalt() { return true; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144
145 virtual lldb_private::Error
146 DoSignal (int signal);
147
148 virtual lldb_private::Error
149 DoDestroy ();
150
151 virtual void
152 RefreshStateAfterStop();
153
154 //------------------------------------------------------------------
155 // Process Queries
156 //------------------------------------------------------------------
157 virtual bool
158 IsAlive ();
159
160 virtual lldb::addr_t
161 GetImageInfoAddress();
162
163 //------------------------------------------------------------------
164 // Process Memory
165 //------------------------------------------------------------------
166 virtual size_t
167 DoReadMemory (lldb::addr_t addr, void *buf, size_t size, lldb_private::Error &error);
168
169 virtual size_t
170 DoWriteMemory (lldb::addr_t addr, const void *buf, size_t size, lldb_private::Error &error);
171
172 virtual lldb::addr_t
173 DoAllocateMemory (size_t size, uint32_t permissions, lldb_private::Error &error);
174
175 virtual lldb_private::Error
Greg Clayton46fb5582011-11-18 07:03:08 +0000176 GetMemoryRegionInfo (lldb::addr_t load_addr,
177 lldb_private::MemoryRegionInfo &region_info);
178
179 virtual lldb_private::Error
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000180 DoDeallocateMemory (lldb::addr_t ptr);
181
182 //------------------------------------------------------------------
183 // Process STDIO
184 //------------------------------------------------------------------
185 virtual size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186 PutSTDIN (const char *buf, size_t buf_size, lldb_private::Error &error);
187
188 //----------------------------------------------------------------------
189 // Process Breakpoints
190 //----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000191 virtual lldb_private::Error
Jim Ingham299c0c12013-02-15 02:06:30 +0000192 EnableBreakpointSite (lldb_private::BreakpointSite *bp_site);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193
194 virtual lldb_private::Error
Jim Ingham299c0c12013-02-15 02:06:30 +0000195 DisableBreakpointSite (lldb_private::BreakpointSite *bp_site);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000196
197 //----------------------------------------------------------------------
198 // Process Watchpoints
199 //----------------------------------------------------------------------
200 virtual lldb_private::Error
Jim Ingham1b5792e2012-12-18 02:03:49 +0000201 EnableWatchpoint (lldb_private::Watchpoint *wp, bool notify = true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202
203 virtual lldb_private::Error
Jim Ingham1b5792e2012-12-18 02:03:49 +0000204 DisableWatchpoint (lldb_private::Watchpoint *wp, bool notify = true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000205
Johnny Chen64637202012-05-23 21:09:52 +0000206 virtual lldb_private::Error
207 GetWatchpointSupportInfo (uint32_t &num);
208
Enrico Granataf04a2192012-07-13 23:18:48 +0000209 virtual lldb_private::Error
210 GetWatchpointSupportInfo (uint32_t &num, bool& after);
211
Jim Ingham1c823b42011-01-22 01:33:44 +0000212 virtual bool
213 StartNoticingNewThreads();
214
215 virtual bool
216 StopNoticingNewThreads();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217
Greg Claytonc1422c12012-04-09 22:46:21 +0000218 GDBRemoteCommunicationClient &
219 GetGDBRemote()
220 {
221 return m_gdb_comm;
222 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000223
Jason Molendaa3329782014-03-29 18:54:20 +0000224 virtual lldb_private::Error
225 SendEventData(const char *data);
226
Greg Claytonfbb76342013-11-20 21:07:01 +0000227 //----------------------------------------------------------------------
228 // Override SetExitStatus so we can disconnect from the remote GDB server
229 //----------------------------------------------------------------------
230 virtual bool
231 SetExitStatus (int exit_status, const char *cstr);
232
Jason Molenda6076bf42014-05-06 04:34:52 +0000233 void
234 SetUserSpecifiedMaxMemoryTransferSize (uint64_t user_specified_max);
235
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236protected:
237 friend class ThreadGDBRemote;
Greg Clayton576d8832011-03-22 04:00:09 +0000238 friend class GDBRemoteCommunicationClient;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000239 friend class GDBRemoteRegisterContext;
240
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000241 //----------------------------------------------------------------------
242 // Accessors
243 //----------------------------------------------------------------------
244 bool
245 IsRunning ( lldb::StateType state )
246 {
247 return state == lldb::eStateRunning || IsStepping(state);
248 }
249
250 bool
251 IsStepping ( lldb::StateType state)
252 {
253 return state == lldb::eStateStepping;
254 }
255 bool
256 CanResume ( lldb::StateType state)
257 {
258 return state == lldb::eStateStopped;
259 }
260
261 bool
262 HasExited (lldb::StateType state)
263 {
264 return state == lldb::eStateExited;
265 }
266
267 bool
268 ProcessIDIsValid ( ) const;
269
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000270 void
271 Clear ( );
272
273 lldb_private::Flags &
274 GetFlags ()
275 {
276 return m_flags;
277 }
278
279 const lldb_private::Flags &
280 GetFlags () const
281 {
282 return m_flags;
283 }
284
Greg Clayton9fc13552012-04-10 00:18:59 +0000285 virtual bool
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000286 UpdateThreadList (lldb_private::ThreadList &old_thread_list,
287 lldb_private::ThreadList &new_thread_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288
289 lldb_private::Error
Greg Clayton91a9b2472013-12-04 19:19:12 +0000290 LaunchAndConnectToDebugserver (const lldb_private::ProcessInfo &process_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000291
292 void
293 KillDebugserverProcess ();
294
295 void
Greg Clayton513c26c2011-01-29 07:10:55 +0000296 BuildDynamicRegisterInfo (bool force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000297
Greg Clayton09c3e3d2011-12-06 04:51:14 +0000298 void
Greg Clayton8cda7f02013-05-21 21:55:59 +0000299 SetLastStopPacket (const StringExtractorGDBRemote &response);
Jason Molendad1fae142012-09-29 08:03:33 +0000300
Greg Claytonef8180a2013-10-15 00:14:28 +0000301 bool
302 ParsePythonTargetDefinition(const lldb_private::FileSpec &target_definition_fspec);
303
304 bool
305 ParseRegisters(lldb_private::ScriptInterpreterObject *registers_array);
306
Steve Pucci03904ac2014-03-04 23:18:46 +0000307 virtual const lldb::DataBufferSP
308 GetAuxvData();
309
Jason Molenda6076bf42014-05-06 04:34:52 +0000310 void
311 GetMaxMemorySize();
312
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000313 //------------------------------------------------------------------
314 /// Broadcaster event bits definitions.
315 //------------------------------------------------------------------
316 enum
317 {
318 eBroadcastBitAsyncContinue = (1 << 0),
Jim Inghamb1e2e842012-04-12 18:49:31 +0000319 eBroadcastBitAsyncThreadShouldExit = (1 << 1),
320 eBroadcastBitAsyncThreadDidExit = (1 << 2)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321 };
322
Jim Ingham455fa5c2012-11-01 01:15:33 +0000323 typedef enum AsyncThreadState
324 {
325 eAsyncThreadNotStarted,
326 eAsyncThreadRunning,
327 eAsyncThreadDone
328 } AsyncThreadState;
329
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000330 lldb_private::Flags m_flags; // Process specific flags (see eFlags enums)
Greg Clayton576d8832011-03-22 04:00:09 +0000331 GDBRemoteCommunicationClient m_gdb_comm;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332 lldb::pid_t m_debugserver_pid;
Greg Claytondd0e5a52011-06-02 22:22:38 +0000333 StringExtractorGDBRemote m_last_stop_packet;
Greg Clayton09c3e3d2011-12-06 04:51:14 +0000334 lldb_private::Mutex m_last_stop_packet_mutex;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335 GDBRemoteDynamicRegisterInfo m_register_info;
336 lldb_private::Broadcaster m_async_broadcaster;
337 lldb::thread_t m_async_thread;
Jim Ingham455fa5c2012-11-01 01:15:33 +0000338 AsyncThreadState m_async_thread_state;
339 lldb_private::Mutex m_async_thread_state_mutex;
Greg Clayton71fc2a32011-02-12 06:28:37 +0000340 typedef std::vector<lldb::tid_t> tid_collection;
341 typedef std::vector< std::pair<lldb::tid_t,int> > tid_sig_collection;
Greg Clayton70b57652011-05-15 01:25:55 +0000342 typedef std::map<lldb::addr_t, lldb::addr_t> MMapMap;
Greg Clayton9e920902012-04-10 02:25:43 +0000343 tid_collection m_thread_ids; // Thread IDs for all threads. This list gets updated after stopping
Greg Clayton71fc2a32011-02-12 06:28:37 +0000344 tid_collection m_continue_c_tids; // 'c' for continue
345 tid_sig_collection m_continue_C_tids; // 'C' for continue with signal
346 tid_collection m_continue_s_tids; // 's' for step
347 tid_sig_collection m_continue_S_tids; // 'S' for step with signal
Jason Molenda6076bf42014-05-06 04:34:52 +0000348 uint64_t m_max_memory_size; // The maximum number of bytes to read/write when reading and writing memory
349 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 +0000350 MMapMap m_addr_to_mmap_size;
Greg Clayton4116e932012-05-15 02:33:01 +0000351 lldb::BreakpointSP m_thread_create_bp_sp;
352 bool m_waiting_for_attach;
Jim Ingham43c555d2012-07-04 00:35:43 +0000353 bool m_destroy_tried_resuming;
Greg Clayton998255b2012-10-13 02:07:45 +0000354 lldb::CommandObjectSP m_command_sp;
Hafiz Abid Qadeer85a4daf2013-10-18 10:04:33 +0000355 int64_t m_breakpoint_pc_offset;
Greg Clayton4116e932012-05-15 02:33:01 +0000356
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000357 bool
358 StartAsyncThread ();
359
360 void
361 StopAsyncThread ();
362
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000363 static lldb::thread_result_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000364 AsyncThread (void *arg);
365
366 static bool
367 MonitorDebugserverProcess (void *callback_baton,
368 lldb::pid_t pid,
Greg Claytone4e45922011-11-16 05:37:56 +0000369 bool exited,
370 int signo,
371 int exit_status);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372
373 lldb::StateType
374 SetThreadStopInfo (StringExtractor& stop_packet);
375
376 void
Greg Clayton9e920902012-04-10 02:25:43 +0000377 ClearThreadIDList ();
378
379 bool
380 UpdateThreadIDList ();
381
382 void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383 DidLaunchOrAttach ();
384
385 lldb_private::Error
386 ConnectToDebugserver (const char *host_port);
387
388 const char *
389 GetDispatchQueueNameForThread (lldb::addr_t thread_dispatch_qaddr,
390 std::string &dispatch_queue_name);
391
Jason Molenda5e8534e2012-10-03 01:29:34 +0000392 lldb_private::DynamicLoader *
393 GetDynamicLoader ();
394
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395private:
396 //------------------------------------------------------------------
397 // For ProcessGDBRemote only
398 //------------------------------------------------------------------
Jim Ingham1c823b42011-01-22 01:33:44 +0000399 static bool
400 NewThreadNotifyBreakpointHit (void *baton,
401 lldb_private::StoppointCallbackContext *context,
402 lldb::user_id_t break_id,
403 lldb::user_id_t break_loc_id);
404
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405 DISALLOW_COPY_AND_ASSIGN (ProcessGDBRemote);
406
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407};
408
409#endif // liblldb_ProcessGDBRemote_h_