blob: 97c3f9fa798ae9092c1cb0f4d600ffc8aff9273c [file] [log] [blame]
Greg Clayton576d8832011-03-22 04:00:09 +00001//===-- GDBRemoteCommunicationClient.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_GDBRemoteCommunicationClient_h_
11#define liblldb_GDBRemoteCommunicationClient_h_
12
13// C Includes
14// C++ Includes
Greg Claytonadc00cb2011-05-20 23:38:13 +000015#include <vector>
16
Greg Clayton576d8832011-03-22 04:00:09 +000017// Other libraries and framework includes
18// Project includes
19#include "lldb/Core/ArchSpec.h"
Greg Clayton46fb5582011-11-18 07:03:08 +000020#include "lldb/Target/Process.h"
Greg Clayton576d8832011-03-22 04:00:09 +000021
22#include "GDBRemoteCommunication.h"
23
Greg Clayton8b82f082011-04-12 05:54:46 +000024typedef enum
25{
26 eBreakpointSoftware = 0,
27 eBreakpointHardware,
28 eWatchpointWrite,
29 eWatchpointRead,
30 eWatchpointReadWrite
31} GDBStoppointType;
32
Greg Clayton576d8832011-03-22 04:00:09 +000033class GDBRemoteCommunicationClient : public GDBRemoteCommunication
34{
35public:
36 //------------------------------------------------------------------
37 // Constructors and Destructors
38 //------------------------------------------------------------------
Greg Clayton8b82f082011-04-12 05:54:46 +000039 GDBRemoteCommunicationClient(bool is_platform);
Greg Clayton576d8832011-03-22 04:00:09 +000040
41 virtual
42 ~GDBRemoteCommunicationClient();
43
Greg Clayton1cb64962011-03-24 04:28:38 +000044 //------------------------------------------------------------------
45 // After connecting, send the handshake to the server to make sure
46 // we are communicating with it.
47 //------------------------------------------------------------------
48 bool
49 HandshakeWithServer (lldb_private::Error *error_ptr);
50
Greg Clayton576d8832011-03-22 04:00:09 +000051 size_t
52 SendPacketAndWaitForResponse (const char *send_payload,
53 StringExtractorGDBRemote &response,
54 bool send_async);
55
56 size_t
57 SendPacketAndWaitForResponse (const char *send_payload,
58 size_t send_length,
59 StringExtractorGDBRemote &response,
60 bool send_async);
61
62 lldb::StateType
63 SendContinuePacketAndWaitForResponse (ProcessGDBRemote *process,
64 const char *packet_payload,
65 size_t packet_length,
66 StringExtractorGDBRemote &response);
67
68 virtual bool
69 GetThreadSuffixSupported ();
70
Greg Clayton1cb64962011-03-24 04:28:38 +000071 void
72 QueryNoAckModeSupported ();
Greg Clayton576d8832011-03-22 04:00:09 +000073
Greg Clayton44633992012-04-10 03:22:03 +000074 void
75 GetListThreadsInStopReplySupported ();
76
Greg Clayton576d8832011-03-22 04:00:09 +000077 bool
78 SendAsyncSignal (int signo);
79
80 bool
81 SendInterrupt (lldb_private::Mutex::Locker &locker,
82 uint32_t seconds_to_wait_for_stop,
Greg Clayton576d8832011-03-22 04:00:09 +000083 bool &timed_out);
84
85 lldb::pid_t
86 GetCurrentProcessID ();
87
88 bool
89 GetLaunchSuccess (std::string &error_str);
90
Greg Clayton8b82f082011-04-12 05:54:46 +000091 uint16_t
Daniel Maleae0f8f572013-08-26 23:57:52 +000092 LaunchGDBserverAndGetPort (lldb::pid_t &pid);
93
94 bool
95 KillSpawnedProcess (lldb::pid_t pid);
Greg Clayton8b82f082011-04-12 05:54:46 +000096
Greg Clayton576d8832011-03-22 04:00:09 +000097 //------------------------------------------------------------------
98 /// Sends a GDB remote protocol 'A' packet that delivers program
99 /// arguments to the remote server.
100 ///
101 /// @param[in] argv
102 /// A NULL terminated array of const C strings to use as the
103 /// arguments.
104 ///
105 /// @return
106 /// Zero if the response was "OK", a positive value if the
107 /// the response was "Exx" where xx are two hex digits, or
108 /// -1 if the call is unsupported or any other unexpected
109 /// response was received.
110 //------------------------------------------------------------------
111 int
112 SendArgumentsPacket (char const *argv[]);
113
114 //------------------------------------------------------------------
115 /// Sends a "QEnvironment:NAME=VALUE" packet that will build up the
116 /// environment that will get used when launching an application
117 /// in conjunction with the 'A' packet. This function can be called
118 /// multiple times in a row in order to pass on the desired
119 /// environment that the inferior should be launched with.
120 ///
121 /// @param[in] name_equal_value
122 /// A NULL terminated C string that contains a single environment
123 /// in the format "NAME=VALUE".
124 ///
125 /// @return
126 /// Zero if the response was "OK", a positive value if the
127 /// the response was "Exx" where xx are two hex digits, or
128 /// -1 if the call is unsupported or any other unexpected
129 /// response was received.
130 //------------------------------------------------------------------
131 int
132 SendEnvironmentPacket (char const *name_equal_value);
133
Greg Claytonc4103b32011-05-08 04:53:50 +0000134 int
135 SendLaunchArchPacket (const char *arch);
Greg Clayton576d8832011-03-22 04:00:09 +0000136 //------------------------------------------------------------------
137 /// Sends a "vAttach:PID" where PID is in hex.
138 ///
139 /// @param[in] pid
140 /// A process ID for the remote gdb server to attach to.
141 ///
142 /// @param[out] response
143 /// The response received from the gdb server. If the return
144 /// value is zero, \a response will contain a stop reply
145 /// packet.
146 ///
147 /// @return
148 /// Zero if the attach was successful, or an error indicating
149 /// an error code.
150 //------------------------------------------------------------------
151 int
152 SendAttach (lldb::pid_t pid,
153 StringExtractorGDBRemote& response);
154
155
156 //------------------------------------------------------------------
157 /// Sets the path to use for stdin/out/err for a process
158 /// that will be launched with the 'A' packet.
159 ///
160 /// @param[in] path
161 /// The path to use for stdin/out/err
162 ///
163 /// @return
164 /// Zero if the for success, or an error code for failure.
165 //------------------------------------------------------------------
166 int
167 SetSTDIN (char const *path);
168 int
169 SetSTDOUT (char const *path);
170 int
171 SetSTDERR (char const *path);
172
173 //------------------------------------------------------------------
174 /// Sets the disable ASLR flag to \a enable for a process that will
175 /// be launched with the 'A' packet.
176 ///
177 /// @param[in] enable
178 /// A boolean value indicating wether to disable ASLR or not.
179 ///
180 /// @return
181 /// Zero if the for success, or an error code for failure.
182 //------------------------------------------------------------------
183 int
184 SetDisableASLR (bool enable);
185
186 //------------------------------------------------------------------
187 /// Sets the working directory to \a path for a process that will
188 /// be launched with the 'A' packet.
189 ///
190 /// @param[in] path
191 /// The path to a directory to use when launching our processs
192 ///
193 /// @return
194 /// Zero if the for success, or an error code for failure.
195 //------------------------------------------------------------------
196 int
197 SetWorkingDir (char const *path);
198
199 lldb::addr_t
200 AllocateMemory (size_t size, uint32_t permissions);
201
202 bool
203 DeallocateMemory (lldb::addr_t addr);
204
Jim Inghamacff8952013-05-02 00:27:30 +0000205 lldb_private::Error
206 Detach (bool keep_stopped);
Greg Clayton37a0a242012-04-11 00:24:49 +0000207
Greg Clayton46fb5582011-11-18 07:03:08 +0000208 lldb_private::Error
209 GetMemoryRegionInfo (lldb::addr_t addr,
210 lldb_private::MemoryRegionInfo &range_info);
211
Johnny Chen64637202012-05-23 21:09:52 +0000212 lldb_private::Error
213 GetWatchpointSupportInfo (uint32_t &num);
214
Enrico Granataf04a2192012-07-13 23:18:48 +0000215 lldb_private::Error
216 GetWatchpointSupportInfo (uint32_t &num, bool& after);
217
218 lldb_private::Error
219 GetWatchpointsTriggerAfterInstruction (bool &after);
220
Greg Clayton576d8832011-03-22 04:00:09 +0000221 const lldb_private::ArchSpec &
222 GetHostArchitecture ();
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000223
224 const lldb_private::ArchSpec &
225 GetProcessArchitecture ();
226
Greg Clayton576d8832011-03-22 04:00:09 +0000227 bool
228 GetVContSupported (char flavor);
229
Jim Inghamcd16df92012-07-20 21:37:13 +0000230 bool
Sean Callananb1de1142013-09-04 23:24:15 +0000231 GetpPacketSupported (lldb::tid_t tid);
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000232
233 bool
Jim Inghamcd16df92012-07-20 21:37:13 +0000234 GetVAttachOrWaitSupported ();
235
Jim Ingham279ceec2012-07-25 21:12:43 +0000236 bool
237 GetSyncThreadStateSupported();
238
Greg Clayton576d8832011-03-22 04:00:09 +0000239 void
240 ResetDiscoverableSettings();
241
242 bool
Greg Clayton9b1e1cd2011-04-04 18:18:57 +0000243 GetHostInfo (bool force = false);
Greg Clayton576d8832011-03-22 04:00:09 +0000244
245 bool
Greg Clayton1cb64962011-03-24 04:28:38 +0000246 GetOSVersion (uint32_t &major,
247 uint32_t &minor,
248 uint32_t &update);
249
250 bool
251 GetOSBuildString (std::string &s);
252
253 bool
254 GetOSKernelDescription (std::string &s);
255
256 lldb_private::ArchSpec
257 GetSystemArchitecture ();
258
259 bool
260 GetHostname (std::string &s);
261
Greg Clayton37a0a242012-04-11 00:24:49 +0000262 lldb::addr_t
263 GetShlibInfoAddr();
264
Greg Clayton1cb64962011-03-24 04:28:38 +0000265 bool
Greg Clayton576d8832011-03-22 04:00:09 +0000266 GetSupportsThreadSuffix ();
267
268 bool
Greg Clayton32e0a752011-03-30 18:16:51 +0000269 GetProcessInfo (lldb::pid_t pid,
Greg Clayton8b82f082011-04-12 05:54:46 +0000270 lldb_private::ProcessInstanceInfo &process_info);
Greg Clayton32e0a752011-03-30 18:16:51 +0000271
272 uint32_t
Greg Clayton8b82f082011-04-12 05:54:46 +0000273 FindProcesses (const lldb_private::ProcessInstanceInfoMatch &process_match_info,
274 lldb_private::ProcessInstanceInfoList &process_infos);
Greg Clayton32e0a752011-03-30 18:16:51 +0000275
276 bool
277 GetUserName (uint32_t uid, std::string &name);
278
279 bool
280 GetGroupName (uint32_t gid, std::string &name);
281
282 bool
Greg Clayton576d8832011-03-22 04:00:09 +0000283 HasFullVContSupport ()
284 {
285 return GetVContSupported ('A');
286 }
287
288 bool
289 HasAnyVContSupport ()
290 {
291 return GetVContSupported ('a');
292 }
293
Greg Clayton8b82f082011-04-12 05:54:46 +0000294 bool
295 GetStopReply (StringExtractorGDBRemote &response);
296
297 bool
Greg Claytonf402f782012-10-13 02:11:55 +0000298 GetThreadStopInfo (lldb::tid_t tid,
Greg Clayton8b82f082011-04-12 05:54:46 +0000299 StringExtractorGDBRemote &response);
300
301 bool
302 SupportsGDBStoppointPacket (GDBStoppointType type)
303 {
304 switch (type)
305 {
306 case eBreakpointSoftware: return m_supports_z0;
307 case eBreakpointHardware: return m_supports_z1;
308 case eWatchpointWrite: return m_supports_z2;
309 case eWatchpointRead: return m_supports_z3;
310 case eWatchpointReadWrite: return m_supports_z4;
Greg Clayton8b82f082011-04-12 05:54:46 +0000311 }
312 return false;
313 }
314 uint8_t
315 SendGDBStoppointTypePacket (GDBStoppointType type, // Type of breakpoint or watchpoint
316 bool insert, // Insert or remove?
317 lldb::addr_t addr, // Address of breakpoint or watchpoint
318 uint32_t length); // Byte Size of breakpoint or watchpoint
319
Greg Clayton9b1e1cd2011-04-04 18:18:57 +0000320 void
321 TestPacketSpeed (const uint32_t num_packets);
322
323 // This packet is for testing the speed of the interface only. Both
324 // the client and server need to support it, but this allows us to
325 // measure the packet speed without any other work being done on the
326 // other end and avoids any of that work affecting the packet send
327 // and response times.
328 bool
329 SendSpeedTestPacket (uint32_t send_size,
330 uint32_t recv_size);
Greg Clayton8b82f082011-04-12 05:54:46 +0000331
332 bool
Jason Molendae9ca4af2013-02-23 02:04:45 +0000333 SetCurrentThread (uint64_t tid);
Greg Clayton8b82f082011-04-12 05:54:46 +0000334
335 bool
Jason Molendae9ca4af2013-02-23 02:04:45 +0000336 SetCurrentThreadForRun (uint64_t tid);
Greg Clayton8b82f082011-04-12 05:54:46 +0000337
Greg Clayton2a48f522011-05-14 01:50:35 +0000338 lldb_private::LazyBool
Jim Ingham372787f2012-04-07 00:00:41 +0000339 SupportsAllocDeallocMemory () // const
Greg Clayton2a48f522011-05-14 01:50:35 +0000340 {
Jim Ingham372787f2012-04-07 00:00:41 +0000341 // Uncomment this to have lldb pretend the debug server doesn't respond to alloc/dealloc memory packets.
342 // m_supports_alloc_dealloc_memory = lldb_private::eLazyBoolNo;
Greg Clayton70b57652011-05-15 01:25:55 +0000343 return m_supports_alloc_dealloc_memory;
Greg Clayton2a48f522011-05-14 01:50:35 +0000344 }
345
Greg Claytonadc00cb2011-05-20 23:38:13 +0000346 size_t
347 GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids,
348 bool &sequence_mutex_unavailable);
349
Greg Clayton2687cd12012-03-29 01:55:41 +0000350 bool
351 GetInterruptWasSent () const
352 {
353 return m_interrupt_sent;
354 }
Han Ming Ong4b6459f2013-01-18 23:11:53 +0000355
Daniel Maleae0f8f572013-08-26 23:57:52 +0000356 virtual lldb::user_id_t
357 OpenFile (const lldb_private::FileSpec& file_spec,
358 uint32_t flags,
359 mode_t mode,
360 lldb_private::Error &error);
361
362 virtual bool
363 CloseFile (lldb::user_id_t fd,
364 lldb_private::Error &error);
365
366 virtual lldb::user_id_t
367 GetFileSize (const lldb_private::FileSpec& file_spec);
368
369 virtual uint32_t
370 GetFilePermissions(const lldb_private::FileSpec& file_spec,
371 lldb_private::Error &error);
372
373 virtual uint64_t
374 ReadFile (lldb::user_id_t fd,
375 uint64_t offset,
376 void *dst,
377 uint64_t dst_len,
378 lldb_private::Error &error);
379
380 virtual uint64_t
381 WriteFile (lldb::user_id_t fd,
382 uint64_t offset,
383 const void* src,
384 uint64_t src_len,
385 lldb_private::Error &error);
386
387 virtual uint32_t
388 MakeDirectory (const std::string &path,
389 mode_t mode);
390
391 virtual bool
392 GetFileExists (const lldb_private::FileSpec& file_spec);
393
394 virtual lldb_private::Error
395 RunShellCommand (const char *command, // Shouldn't be NULL
396 const char *working_dir, // Pass NULL to use the current working directory
397 int *status_ptr, // Pass NULL if you don't want the process exit status
398 int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit
399 std::string *command_output, // Pass NULL if you don't want the command output
400 uint32_t timeout_sec); // Timeout in seconds to wait for shell program to finish
401
402 virtual bool
403 CalculateMD5 (const lldb_private::FileSpec& file_spec,
404 uint64_t &high,
405 uint64_t &low);
406
Han Ming Ong4b6459f2013-01-18 23:11:53 +0000407 std::string
408 HarmonizeThreadIdsForProfileData (ProcessGDBRemote *process,
409 StringExtractorGDBRemote &inputStringExtractor);
Daniel Maleae0f8f572013-08-26 23:57:52 +0000410
Greg Clayton576d8832011-03-22 04:00:09 +0000411protected:
412
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000413 bool
414 GetCurrentProcessInfo ();
415
Greg Clayton576d8832011-03-22 04:00:09 +0000416 //------------------------------------------------------------------
417 // Classes that inherit from GDBRemoteCommunicationClient can see and modify these
418 //------------------------------------------------------------------
Greg Claytone0d378b2011-03-24 21:19:54 +0000419 lldb_private::LazyBool m_supports_not_sending_acks;
420 lldb_private::LazyBool m_supports_thread_suffix;
Greg Clayton44633992012-04-10 03:22:03 +0000421 lldb_private::LazyBool m_supports_threads_in_stop_reply;
Greg Claytone0d378b2011-03-24 21:19:54 +0000422 lldb_private::LazyBool m_supports_vCont_all;
423 lldb_private::LazyBool m_supports_vCont_any;
424 lldb_private::LazyBool m_supports_vCont_c;
425 lldb_private::LazyBool m_supports_vCont_C;
426 lldb_private::LazyBool m_supports_vCont_s;
427 lldb_private::LazyBool m_supports_vCont_S;
Greg Clayton32e0a752011-03-30 18:16:51 +0000428 lldb_private::LazyBool m_qHostInfo_is_valid;
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000429 lldb_private::LazyBool m_qProcessInfo_is_valid;
Greg Clayton70b57652011-05-15 01:25:55 +0000430 lldb_private::LazyBool m_supports_alloc_dealloc_memory;
Greg Clayton46fb5582011-11-18 07:03:08 +0000431 lldb_private::LazyBool m_supports_memory_region_info;
Johnny Chen64637202012-05-23 21:09:52 +0000432 lldb_private::LazyBool m_supports_watchpoint_support_info;
Jim Inghamacff8952013-05-02 00:27:30 +0000433 lldb_private::LazyBool m_supports_detach_stay_stopped;
Enrico Granataf04a2192012-07-13 23:18:48 +0000434 lldb_private::LazyBool m_watchpoints_trigger_after_instruction;
Jim Inghamcd16df92012-07-20 21:37:13 +0000435 lldb_private::LazyBool m_attach_or_wait_reply;
Jim Ingham279ceec2012-07-25 21:12:43 +0000436 lldb_private::LazyBool m_prepare_for_reg_writing_reply;
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000437 lldb_private::LazyBool m_supports_p;
Jim Inghamcd16df92012-07-20 21:37:13 +0000438
Greg Clayton8b82f082011-04-12 05:54:46 +0000439 bool
440 m_supports_qProcessInfoPID:1,
441 m_supports_qfProcessInfo:1,
442 m_supports_qUserName:1,
443 m_supports_qGroupName:1,
444 m_supports_qThreadStopInfo:1,
445 m_supports_z0:1,
446 m_supports_z1:1,
447 m_supports_z2:1,
448 m_supports_z3:1,
Greg Clayton89600582013-10-10 17:53:50 +0000449 m_supports_z4:1,
450 m_supports_QEnvironment:1,
451 m_supports_QEnvironmentHexEncoded:1;
Greg Clayton2a48f522011-05-14 01:50:35 +0000452
Greg Clayton8b82f082011-04-12 05:54:46 +0000453
454 lldb::tid_t m_curr_tid; // Current gdb remote protocol thread index for all other operations
455 lldb::tid_t m_curr_tid_run; // Current gdb remote protocol thread index for continue, step, etc
456
Greg Clayton576d8832011-03-22 04:00:09 +0000457
Johnny Chen64637202012-05-23 21:09:52 +0000458 uint32_t m_num_supported_hardware_watchpoints;
459
Greg Clayton576d8832011-03-22 04:00:09 +0000460 // If we need to send a packet while the target is running, the m_async_XXX
461 // member variables take care of making this happen.
462 lldb_private::Mutex m_async_mutex;
463 lldb_private::Predicate<bool> m_async_packet_predicate;
464 std::string m_async_packet;
465 StringExtractorGDBRemote m_async_response;
466 int m_async_signal; // We were asked to deliver a signal to the inferior process.
Greg Clayton2687cd12012-03-29 01:55:41 +0000467 bool m_interrupt_sent;
Han Ming Ong4b6459f2013-01-18 23:11:53 +0000468 std::string m_partial_profile_data;
469 std::map<uint64_t, uint32_t> m_thread_id_to_used_usec_map;
Greg Clayton576d8832011-03-22 04:00:09 +0000470
Greg Claytond314e812011-03-23 00:09:55 +0000471 lldb_private::ArchSpec m_host_arch;
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000472 lldb_private::ArchSpec m_process_arch;
Greg Clayton1cb64962011-03-24 04:28:38 +0000473 uint32_t m_os_version_major;
474 uint32_t m_os_version_minor;
475 uint32_t m_os_version_update;
476 std::string m_os_build;
477 std::string m_os_kernel;
478 std::string m_hostname;
Greg Clayton576d8832011-03-22 04:00:09 +0000479
Greg Clayton32e0a752011-03-30 18:16:51 +0000480 bool
481 DecodeProcessInfoResponse (StringExtractorGDBRemote &response,
Greg Clayton8b82f082011-04-12 05:54:46 +0000482 lldb_private::ProcessInstanceInfo &process_info);
Greg Clayton576d8832011-03-22 04:00:09 +0000483private:
484 //------------------------------------------------------------------
485 // For GDBRemoteCommunicationClient only
486 //------------------------------------------------------------------
487 DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunicationClient);
488};
489
490#endif // liblldb_GDBRemoteCommunicationClient_h_