blob: 4ecd3a0642443c9b713f16a719295b9ec992942a [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
97 DoConnectRemote (const char *remote_url);
98
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
Jim Ingham55e01d82011-01-22 01:33:44 +0000207 virtual bool
208 StartNoticingNewThreads();
209
210 virtual bool
211 StopNoticingNewThreads();
Chris Lattner24943d22010-06-08 16:52:24 +0000212
213protected:
214 friend class ThreadGDBRemote;
Greg Clayton61d043b2011-03-22 04:00:09 +0000215 friend class GDBRemoteCommunicationClient;
Chris Lattner24943d22010-06-08 16:52:24 +0000216 friend class GDBRemoteRegisterContext;
217
Chris Lattner24943d22010-06-08 16:52:24 +0000218 //----------------------------------------------------------------------
219 // Accessors
220 //----------------------------------------------------------------------
221 bool
222 IsRunning ( lldb::StateType state )
223 {
224 return state == lldb::eStateRunning || IsStepping(state);
225 }
226
227 bool
228 IsStepping ( lldb::StateType state)
229 {
230 return state == lldb::eStateStepping;
231 }
232 bool
233 CanResume ( lldb::StateType state)
234 {
235 return state == lldb::eStateStopped;
236 }
237
238 bool
239 HasExited (lldb::StateType state)
240 {
241 return state == lldb::eStateExited;
242 }
243
244 bool
245 ProcessIDIsValid ( ) const;
246
Chris Lattner24943d22010-06-08 16:52:24 +0000247 void
248 Clear ( );
249
250 lldb_private::Flags &
251 GetFlags ()
252 {
253 return m_flags;
254 }
255
256 const lldb_private::Flags &
257 GetFlags () const
258 {
259 return m_flags;
260 }
261
262 uint32_t
Greg Clayton37f962e2011-08-22 02:49:39 +0000263 UpdateThreadList (lldb_private::ThreadList &old_thread_list,
264 lldb_private::ThreadList &new_thread_list);
Chris Lattner24943d22010-06-08 16:52:24 +0000265
266 lldb_private::Error
Greg Claytonb72d0f02011-04-12 05:54:46 +0000267 StartDebugserverProcess (const char *debugserver_url);
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000268
269 lldb_private::Error
270 StartDebugserverProcess (const char *debugserver_url, const lldb_private::ProcessInfo &process_info);
Chris Lattner24943d22010-06-08 16:52:24 +0000271
272 void
273 KillDebugserverProcess ();
274
275 void
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000276 BuildDynamicRegisterInfo (bool force);
Chris Lattner24943d22010-06-08 16:52:24 +0000277
Greg Clayton61d043b2011-03-22 04:00:09 +0000278 GDBRemoteCommunicationClient &
Chris Lattner24943d22010-06-08 16:52:24 +0000279 GetGDBRemote()
280 {
281 return m_gdb_comm;
282 }
283
Greg Clayton06709002011-12-06 04:51:14 +0000284 void
285 SetLastStopPacket (const StringExtractorGDBRemote &response)
286 {
287 lldb_private::Mutex::Locker locker (m_last_stop_packet_mutex);
288 m_last_stop_packet = response;
289 }
Chris Lattner24943d22010-06-08 16:52:24 +0000290 //------------------------------------------------------------------
291 /// Broadcaster event bits definitions.
292 //------------------------------------------------------------------
293 enum
294 {
295 eBroadcastBitAsyncContinue = (1 << 0),
296 eBroadcastBitAsyncThreadShouldExit = (1 << 1)
297 };
298
Chris Lattner24943d22010-06-08 16:52:24 +0000299 lldb_private::Flags m_flags; // Process specific flags (see eFlags enums)
Greg Clayton61d043b2011-03-22 04:00:09 +0000300 GDBRemoteCommunicationClient m_gdb_comm;
Chris Lattner24943d22010-06-08 16:52:24 +0000301 lldb::pid_t m_debugserver_pid;
Greg Clayton261a18b2011-06-02 22:22:38 +0000302 StringExtractorGDBRemote m_last_stop_packet;
Greg Clayton06709002011-12-06 04:51:14 +0000303 lldb_private::Mutex m_last_stop_packet_mutex;
Chris Lattner24943d22010-06-08 16:52:24 +0000304 GDBRemoteDynamicRegisterInfo m_register_info;
305 lldb_private::Broadcaster m_async_broadcaster;
306 lldb::thread_t m_async_thread;
Greg Claytonc1f45872011-02-12 06:28:37 +0000307 typedef std::vector<lldb::tid_t> tid_collection;
308 typedef std::vector< std::pair<lldb::tid_t,int> > tid_sig_collection;
Greg Clayton2f085c62011-05-15 01:25:55 +0000309 typedef std::map<lldb::addr_t, lldb::addr_t> MMapMap;
Greg Claytonc1f45872011-02-12 06:28:37 +0000310 tid_collection m_continue_c_tids; // 'c' for continue
311 tid_sig_collection m_continue_C_tids; // 'C' for continue with signal
312 tid_collection m_continue_s_tids; // 's' for step
313 tid_sig_collection m_continue_S_tids; // 'S' for step with signal
Chris Lattner24943d22010-06-08 16:52:24 +0000314 lldb::addr_t m_dispatch_queue_offsets_addr;
Chris Lattner24943d22010-06-08 16:52:24 +0000315 size_t m_max_memory_size; // The maximum number of bytes to read/write when reading and writing memory
Chris Lattner24943d22010-06-08 16:52:24 +0000316 bool m_waiting_for_attach;
Jim Ingham55e01d82011-01-22 01:33:44 +0000317 std::vector<lldb::user_id_t> m_thread_observation_bps;
Greg Clayton2f085c62011-05-15 01:25:55 +0000318 MMapMap m_addr_to_mmap_size;
Chris Lattner24943d22010-06-08 16:52:24 +0000319 bool
320 StartAsyncThread ();
321
322 void
323 StopAsyncThread ();
324
325 static void *
326 AsyncThread (void *arg);
327
328 static bool
329 MonitorDebugserverProcess (void *callback_baton,
330 lldb::pid_t pid,
Greg Clayton1c4642c2011-11-16 05:37:56 +0000331 bool exited,
332 int signo,
333 int exit_status);
Chris Lattner24943d22010-06-08 16:52:24 +0000334
335 lldb::StateType
336 SetThreadStopInfo (StringExtractor& stop_packet);
337
338 void
339 DidLaunchOrAttach ();
340
341 lldb_private::Error
342 ConnectToDebugserver (const char *host_port);
343
344 const char *
345 GetDispatchQueueNameForThread (lldb::addr_t thread_dispatch_qaddr,
346 std::string &dispatch_queue_name);
347
348 static size_t
349 AttachInputReaderCallback (void *baton,
350 lldb_private::InputReader *reader,
351 lldb::InputReaderAction notification,
352 const char *bytes,
353 size_t bytes_len);
354
Greg Clayton72e1c782011-01-22 23:43:18 +0000355 lldb_private::Error
356 InterruptIfRunning (bool discard_thread_plans,
357 bool catch_stop_event,
Greg Clayton72e1c782011-01-22 23:43:18 +0000358 lldb::EventSP &stop_event_sp);
359
Chris Lattner24943d22010-06-08 16:52:24 +0000360private:
361 //------------------------------------------------------------------
362 // For ProcessGDBRemote only
363 //------------------------------------------------------------------
Jim Ingham55e01d82011-01-22 01:33:44 +0000364 static bool
365 NewThreadNotifyBreakpointHit (void *baton,
366 lldb_private::StoppointCallbackContext *context,
367 lldb::user_id_t break_id,
368 lldb::user_id_t break_loc_id);
369
Chris Lattner24943d22010-06-08 16:52:24 +0000370 DISALLOW_COPY_AND_ASSIGN (ProcessGDBRemote);
371
Chris Lattner24943d22010-06-08 16:52:24 +0000372};
373
374#endif // liblldb_ProcessGDBRemote_h_