blob: baee872bb733e0df086447e9e2df250941688fbc [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"
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
Greg Clayton7f982402013-07-15 22:54:20 +000052 DebuggerInitialize (lldb_private::Debugger &debugger);
53
54 static void
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055 Terminate();
56
Greg Clayton57abc5d2013-05-10 21:47:16 +000057 static lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058 GetPluginNameStatic();
59
60 static const char *
61 GetPluginDescriptionStatic();
62
63 //------------------------------------------------------------------
64 // Constructors and Destructors
65 //------------------------------------------------------------------
66 ProcessGDBRemote(lldb_private::Target& target, lldb_private::Listener &listener);
67
68 virtual
69 ~ProcessGDBRemote();
70
71 //------------------------------------------------------------------
72 // Check if a given Process
73 //------------------------------------------------------------------
74 virtual bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +000075 CanDebug (lldb_private::Target &target,
76 bool plugin_specified_by_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077
Greg Clayton998255b2012-10-13 02:07:45 +000078 virtual lldb_private::CommandObject *
79 GetPluginCommandObject();
Jim Ingham5aee1622010-08-09 23:31:02 +000080
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081 //------------------------------------------------------------------
82 // Creating a new process, or attaching to an existing one
83 //------------------------------------------------------------------
84 virtual lldb_private::Error
85 WillLaunch (lldb_private::Module* module);
86
87 virtual lldb_private::Error
Greg Clayton982c9762011-11-03 21:22:33 +000088 DoLaunch (lldb_private::Module *exe_module,
Jean-Daniel Dupas7782de92013-12-09 22:52:50 +000089 lldb_private::ProcessLaunchInfo &launch_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000090
91 virtual void
92 DidLaunch ();
93
Todd Fialaaf245d12014-06-30 21:05:18 +000094 lldb_private::UnixSignals&
95 GetUnixSignals () override;
96
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097 virtual lldb_private::Error
Greg Clayton3af9ea52010-11-18 05:57:03 +000098 WillAttachToProcessWithID (lldb::pid_t pid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099
100 virtual lldb_private::Error
Greg Clayton3af9ea52010-11-18 05:57:03 +0000101 WillAttachToProcessWithName (const char *process_name, bool wait_for_launch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102
Greg Claytonb766a732011-02-04 01:58:07 +0000103 virtual lldb_private::Error
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000104 DoConnectRemote (lldb_private::Stream *strm, const char *remote_url);
Greg Claytonb766a732011-02-04 01:58:07 +0000105
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106 lldb_private::Error
107 WillLaunchOrAttach ();
108
109 virtual lldb_private::Error
Greg Claytonc982c762010-07-09 20:39:50 +0000110 DoAttachToProcessWithID (lldb::pid_t pid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111
112 virtual lldb_private::Error
Han Ming Ong84647042012-02-25 01:07:38 +0000113 DoAttachToProcessWithID (lldb::pid_t pid, const lldb_private::ProcessAttachInfo &attach_info);
114
115 virtual lldb_private::Error
116 DoAttachToProcessWithName (const char *process_name,
Han Ming Ong84647042012-02-25 01:07:38 +0000117 const lldb_private::ProcessAttachInfo &attach_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000118
119 virtual void
120 DidAttach ();
121
122 //------------------------------------------------------------------
123 // PluginInterface protocol
124 //------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000125 virtual lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000126 GetPluginName();
127
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000128 virtual uint32_t
129 GetPluginVersion();
130
Chris Lattner30fdc8d2010-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 Ingham0d8bcc72010-11-17 02:32:00 +0000141 DoHalt (bool &caused_stop);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142
143 virtual lldb_private::Error
Jim Inghamacff8952013-05-02 00:27:30 +0000144 DoDetach (bool keep_stopped);
Jim Ingham8af3b9c2013-03-29 01:18:12 +0000145
146 virtual bool
147 DetachRequiresHalt() { return true; }
Chris Lattner30fdc8d2010-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 Clayton46fb5582011-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 Lattner30fdc8d2010-06-08 16:52:24 +0000184 DoDeallocateMemory (lldb::addr_t ptr);
185
186 //------------------------------------------------------------------
187 // Process STDIO
188 //------------------------------------------------------------------
189 virtual size_t
Chris Lattner30fdc8d2010-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 Lattner30fdc8d2010-06-08 16:52:24 +0000195 virtual lldb_private::Error
Jim Ingham299c0c12013-02-15 02:06:30 +0000196 EnableBreakpointSite (lldb_private::BreakpointSite *bp_site);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000197
198 virtual lldb_private::Error
Jim Ingham299c0c12013-02-15 02:06:30 +0000199 DisableBreakpointSite (lldb_private::BreakpointSite *bp_site);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000200
201 //----------------------------------------------------------------------
202 // Process Watchpoints
203 //----------------------------------------------------------------------
204 virtual lldb_private::Error
Jim Ingham1b5792e2012-12-18 02:03:49 +0000205 EnableWatchpoint (lldb_private::Watchpoint *wp, bool notify = true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000206
207 virtual lldb_private::Error
Jim Ingham1b5792e2012-12-18 02:03:49 +0000208 DisableWatchpoint (lldb_private::Watchpoint *wp, bool notify = true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000209
Johnny Chen64637202012-05-23 21:09:52 +0000210 virtual lldb_private::Error
211 GetWatchpointSupportInfo (uint32_t &num);
212
Enrico Granataf04a2192012-07-13 23:18:48 +0000213 virtual lldb_private::Error
214 GetWatchpointSupportInfo (uint32_t &num, bool& after);
215
Jim Ingham1c823b42011-01-22 01:33:44 +0000216 virtual bool
217 StartNoticingNewThreads();
218
219 virtual bool
220 StopNoticingNewThreads();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000221
Greg Claytonc1422c12012-04-09 22:46:21 +0000222 GDBRemoteCommunicationClient &
223 GetGDBRemote()
224 {
225 return m_gdb_comm;
226 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000227
Jason Molendaa3329782014-03-29 18:54:20 +0000228 virtual lldb_private::Error
229 SendEventData(const char *data);
230
Greg Claytonfbb76342013-11-20 21:07:01 +0000231 //----------------------------------------------------------------------
232 // Override SetExitStatus so we can disconnect from the remote GDB server
233 //----------------------------------------------------------------------
234 virtual bool
235 SetExitStatus (int exit_status, const char *cstr);
236
Jason Molenda6076bf42014-05-06 04:34:52 +0000237 void
238 SetUserSpecifiedMaxMemoryTransferSize (uint64_t user_specified_max);
239
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240protected:
241 friend class ThreadGDBRemote;
Greg Clayton576d8832011-03-22 04:00:09 +0000242 friend class GDBRemoteCommunicationClient;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000243 friend class GDBRemoteRegisterContext;
244
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245 //----------------------------------------------------------------------
246 // Accessors
247 //----------------------------------------------------------------------
248 bool
249 IsRunning ( lldb::StateType state )
250 {
251 return state == lldb::eStateRunning || IsStepping(state);
252 }
253
254 bool
255 IsStepping ( lldb::StateType state)
256 {
257 return state == lldb::eStateStepping;
258 }
259 bool
260 CanResume ( lldb::StateType state)
261 {
262 return state == lldb::eStateStopped;
263 }
264
265 bool
266 HasExited (lldb::StateType state)
267 {
268 return state == lldb::eStateExited;
269 }
270
271 bool
272 ProcessIDIsValid ( ) const;
273
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000274 void
275 Clear ( );
276
277 lldb_private::Flags &
278 GetFlags ()
279 {
280 return m_flags;
281 }
282
283 const lldb_private::Flags &
284 GetFlags () const
285 {
286 return m_flags;
287 }
288
Greg Clayton9fc13552012-04-10 00:18:59 +0000289 virtual bool
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000290 UpdateThreadList (lldb_private::ThreadList &old_thread_list,
291 lldb_private::ThreadList &new_thread_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000292
293 lldb_private::Error
Greg Clayton91a9b2472013-12-04 19:19:12 +0000294 LaunchAndConnectToDebugserver (const lldb_private::ProcessInfo &process_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295
296 void
297 KillDebugserverProcess ();
298
299 void
Greg Clayton513c26c2011-01-29 07:10:55 +0000300 BuildDynamicRegisterInfo (bool force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000301
Greg Clayton09c3e3d2011-12-06 04:51:14 +0000302 void
Greg Clayton8cda7f02013-05-21 21:55:59 +0000303 SetLastStopPacket (const StringExtractorGDBRemote &response);
Jason Molendad1fae142012-09-29 08:03:33 +0000304
Greg Claytonef8180a2013-10-15 00:14:28 +0000305 bool
306 ParsePythonTargetDefinition(const lldb_private::FileSpec &target_definition_fspec);
307
308 bool
309 ParseRegisters(lldb_private::ScriptInterpreterObject *registers_array);
310
Todd Fialaaf245d12014-06-30 21:05:18 +0000311 const lldb::DataBufferSP
312 GetAuxvData() override;
Steve Pucci03904ac2014-03-04 23:18:46 +0000313
Jason Molenda705b1802014-06-13 02:37:02 +0000314 lldb_private::StructuredData::ObjectSP
315 GetExtendedInfoForThread (lldb::tid_t tid);
316
Jason Molenda6076bf42014-05-06 04:34:52 +0000317 void
318 GetMaxMemorySize();
319
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000320 //------------------------------------------------------------------
321 /// Broadcaster event bits definitions.
322 //------------------------------------------------------------------
323 enum
324 {
325 eBroadcastBitAsyncContinue = (1 << 0),
Jim Inghamb1e2e842012-04-12 18:49:31 +0000326 eBroadcastBitAsyncThreadShouldExit = (1 << 1),
327 eBroadcastBitAsyncThreadDidExit = (1 << 2)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000328 };
329
Jim Ingham455fa5c2012-11-01 01:15:33 +0000330 typedef enum AsyncThreadState
331 {
332 eAsyncThreadNotStarted,
333 eAsyncThreadRunning,
334 eAsyncThreadDone
335 } AsyncThreadState;
336
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337 lldb_private::Flags m_flags; // Process specific flags (see eFlags enums)
Greg Clayton576d8832011-03-22 04:00:09 +0000338 GDBRemoteCommunicationClient m_gdb_comm;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000339 lldb::pid_t m_debugserver_pid;
Greg Claytondd0e5a52011-06-02 22:22:38 +0000340 StringExtractorGDBRemote m_last_stop_packet;
Greg Clayton09c3e3d2011-12-06 04:51:14 +0000341 lldb_private::Mutex m_last_stop_packet_mutex;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000342 GDBRemoteDynamicRegisterInfo m_register_info;
343 lldb_private::Broadcaster m_async_broadcaster;
344 lldb::thread_t m_async_thread;
Jim Ingham455fa5c2012-11-01 01:15:33 +0000345 AsyncThreadState m_async_thread_state;
346 lldb_private::Mutex m_async_thread_state_mutex;
Greg Clayton71fc2a32011-02-12 06:28:37 +0000347 typedef std::vector<lldb::tid_t> tid_collection;
348 typedef std::vector< std::pair<lldb::tid_t,int> > tid_sig_collection;
Greg Clayton70b57652011-05-15 01:25:55 +0000349 typedef std::map<lldb::addr_t, lldb::addr_t> MMapMap;
Greg Clayton9e920902012-04-10 02:25:43 +0000350 tid_collection m_thread_ids; // Thread IDs for all threads. This list gets updated after stopping
Greg Clayton71fc2a32011-02-12 06:28:37 +0000351 tid_collection m_continue_c_tids; // 'c' for continue
352 tid_sig_collection m_continue_C_tids; // 'C' for continue with signal
353 tid_collection m_continue_s_tids; // 's' for step
354 tid_sig_collection m_continue_S_tids; // 'S' for step with signal
Jason Molenda6076bf42014-05-06 04:34:52 +0000355 uint64_t m_max_memory_size; // The maximum number of bytes to read/write when reading and writing memory
356 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 +0000357 MMapMap m_addr_to_mmap_size;
Greg Clayton4116e932012-05-15 02:33:01 +0000358 lldb::BreakpointSP m_thread_create_bp_sp;
359 bool m_waiting_for_attach;
Jim Ingham43c555d2012-07-04 00:35:43 +0000360 bool m_destroy_tried_resuming;
Greg Clayton998255b2012-10-13 02:07:45 +0000361 lldb::CommandObjectSP m_command_sp;
Hafiz Abid Qadeer85a4daf2013-10-18 10:04:33 +0000362 int64_t m_breakpoint_pc_offset;
Todd Fialaaf245d12014-06-30 21:05:18 +0000363 std::shared_ptr<lldb_private::UnixSignals> m_unix_signals_sp;
364
365
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000366 bool
367 StartAsyncThread ();
368
369 void
370 StopAsyncThread ();
371
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000372 static lldb::thread_result_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373 AsyncThread (void *arg);
374
375 static bool
376 MonitorDebugserverProcess (void *callback_baton,
377 lldb::pid_t pid,
Greg Claytone4e45922011-11-16 05:37:56 +0000378 bool exited,
379 int signo,
380 int exit_status);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000381
382 lldb::StateType
383 SetThreadStopInfo (StringExtractor& stop_packet);
384
385 void
Greg Clayton9e920902012-04-10 02:25:43 +0000386 ClearThreadIDList ();
387
388 bool
389 UpdateThreadIDList ();
390
391 void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000392 DidLaunchOrAttach ();
393
394 lldb_private::Error
395 ConnectToDebugserver (const char *host_port);
396
397 const char *
398 GetDispatchQueueNameForThread (lldb::addr_t thread_dispatch_qaddr,
399 std::string &dispatch_queue_name);
400
Jason Molenda5e8534e2012-10-03 01:29:34 +0000401 lldb_private::DynamicLoader *
402 GetDynamicLoader ();
403
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000404private:
405 //------------------------------------------------------------------
406 // For ProcessGDBRemote only
407 //------------------------------------------------------------------
Jim Ingham1c823b42011-01-22 01:33:44 +0000408 static bool
409 NewThreadNotifyBreakpointHit (void *baton,
410 lldb_private::StoppointCallbackContext *context,
411 lldb::user_id_t break_id,
412 lldb::user_id_t break_loc_id);
413
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414 DISALLOW_COPY_AND_ASSIGN (ProcessGDBRemote);
415
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000416};
417
418#endif // liblldb_ProcessGDBRemote_h_