Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1 | //===-- GDBRemoteCommunicationClient.cpp ------------------------*- 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 | |
| 11 | #include "GDBRemoteCommunicationClient.h" |
| 12 | |
| 13 | // C Includes |
Daniel Malea | b89d049 | 2013-08-28 16:06:16 +0000 | [diff] [blame] | 14 | #include <sys/stat.h> |
| 15 | |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 16 | // C++ Includes |
Han Ming Ong | 4b6459f | 2013-01-18 23:11:53 +0000 | [diff] [blame] | 17 | #include <sstream> |
| 18 | |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 19 | // Other libraries and framework includes |
| 20 | #include "llvm/ADT/Triple.h" |
| 21 | #include "lldb/Interpreter/Args.h" |
| 22 | #include "lldb/Core/ConnectionFileDescriptor.h" |
| 23 | #include "lldb/Core/Log.h" |
| 24 | #include "lldb/Core/State.h" |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 25 | #include "lldb/Core/StreamGDBRemote.h" |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 26 | #include "lldb/Core/StreamString.h" |
| 27 | #include "lldb/Host/Endian.h" |
| 28 | #include "lldb/Host/Host.h" |
| 29 | #include "lldb/Host/TimeValue.h" |
| 30 | |
| 31 | // Project includes |
| 32 | #include "Utility/StringExtractorGDBRemote.h" |
| 33 | #include "ProcessGDBRemote.h" |
| 34 | #include "ProcessGDBRemoteLog.h" |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 35 | #include "lldb/Host/Config.h" |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 36 | |
| 37 | using namespace lldb; |
| 38 | using namespace lldb_private; |
| 39 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 40 | #ifdef LLDB_DISABLE_POSIX |
| 41 | #define SIGSTOP 17 |
| 42 | #endif |
| 43 | |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 44 | //---------------------------------------------------------------------- |
| 45 | // GDBRemoteCommunicationClient constructor |
| 46 | //---------------------------------------------------------------------- |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 47 | GDBRemoteCommunicationClient::GDBRemoteCommunicationClient(bool is_platform) : |
| 48 | GDBRemoteCommunication("gdb-remote.client", "gdb-remote.client.rx_packet", is_platform), |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 49 | m_supports_not_sending_acks (eLazyBoolCalculate), |
| 50 | m_supports_thread_suffix (eLazyBoolCalculate), |
Greg Clayton | 4463399 | 2012-04-10 03:22:03 +0000 | [diff] [blame] | 51 | m_supports_threads_in_stop_reply (eLazyBoolCalculate), |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 52 | m_supports_vCont_all (eLazyBoolCalculate), |
| 53 | m_supports_vCont_any (eLazyBoolCalculate), |
| 54 | m_supports_vCont_c (eLazyBoolCalculate), |
| 55 | m_supports_vCont_C (eLazyBoolCalculate), |
| 56 | m_supports_vCont_s (eLazyBoolCalculate), |
| 57 | m_supports_vCont_S (eLazyBoolCalculate), |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 58 | m_qHostInfo_is_valid (eLazyBoolCalculate), |
Jason Molenda | f17b5ac | 2012-12-19 02:54:03 +0000 | [diff] [blame] | 59 | m_qProcessInfo_is_valid (eLazyBoolCalculate), |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 60 | m_supports_alloc_dealloc_memory (eLazyBoolCalculate), |
Greg Clayton | 46fb558 | 2011-11-18 07:03:08 +0000 | [diff] [blame] | 61 | m_supports_memory_region_info (eLazyBoolCalculate), |
Johnny Chen | 6463720 | 2012-05-23 21:09:52 +0000 | [diff] [blame] | 62 | m_supports_watchpoint_support_info (eLazyBoolCalculate), |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 63 | m_supports_detach_stay_stopped (eLazyBoolCalculate), |
Enrico Granata | f04a219 | 2012-07-13 23:18:48 +0000 | [diff] [blame] | 64 | m_watchpoints_trigger_after_instruction(eLazyBoolCalculate), |
Jim Ingham | cd16df9 | 2012-07-20 21:37:13 +0000 | [diff] [blame] | 65 | m_attach_or_wait_reply(eLazyBoolCalculate), |
Jim Ingham | 279ceec | 2012-07-25 21:12:43 +0000 | [diff] [blame] | 66 | m_prepare_for_reg_writing_reply (eLazyBoolCalculate), |
Eric Christopher | 2490f5c | 2013-08-30 17:50:57 +0000 | [diff] [blame] | 67 | m_supports_p (eLazyBoolCalculate), |
Greg Clayton | f74cf86 | 2013-11-13 23:28:31 +0000 | [diff] [blame] | 68 | m_supports_QSaveRegisterState (eLazyBoolCalculate), |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 69 | m_supports_qProcessInfoPID (true), |
| 70 | m_supports_qfProcessInfo (true), |
| 71 | m_supports_qUserName (true), |
| 72 | m_supports_qGroupName (true), |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 73 | m_supports_qThreadStopInfo (true), |
| 74 | m_supports_z0 (true), |
| 75 | m_supports_z1 (true), |
| 76 | m_supports_z2 (true), |
| 77 | m_supports_z3 (true), |
| 78 | m_supports_z4 (true), |
Greg Clayton | 8960058 | 2013-10-10 17:53:50 +0000 | [diff] [blame] | 79 | m_supports_QEnvironment (true), |
| 80 | m_supports_QEnvironmentHexEncoded (true), |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 81 | m_curr_tid (LLDB_INVALID_THREAD_ID), |
| 82 | m_curr_tid_run (LLDB_INVALID_THREAD_ID), |
Johnny Chen | 6463720 | 2012-05-23 21:09:52 +0000 | [diff] [blame] | 83 | m_num_supported_hardware_watchpoints (0), |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 84 | m_async_mutex (Mutex::eMutexTypeRecursive), |
| 85 | m_async_packet_predicate (false), |
| 86 | m_async_packet (), |
Jim Ingham | a6195b7 | 2013-12-18 01:24:33 +0000 | [diff] [blame^] | 87 | m_async_result (PacketResult::Success), |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 88 | m_async_response (), |
| 89 | m_async_signal (-1), |
Han Ming Ong | 4b6459f | 2013-01-18 23:11:53 +0000 | [diff] [blame] | 90 | m_thread_id_to_used_usec_map (), |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 91 | m_host_arch(), |
Jason Molenda | f17b5ac | 2012-12-19 02:54:03 +0000 | [diff] [blame] | 92 | m_process_arch(), |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 93 | m_os_version_major (UINT32_MAX), |
| 94 | m_os_version_minor (UINT32_MAX), |
Greg Clayton | 9ac6d2d | 2013-10-25 18:13:17 +0000 | [diff] [blame] | 95 | m_os_version_update (UINT32_MAX), |
| 96 | m_os_build (), |
| 97 | m_os_kernel (), |
| 98 | m_hostname (), |
| 99 | m_default_packet_timeout (0) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 100 | { |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | //---------------------------------------------------------------------- |
| 104 | // Destructor |
| 105 | //---------------------------------------------------------------------- |
| 106 | GDBRemoteCommunicationClient::~GDBRemoteCommunicationClient() |
| 107 | { |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 108 | if (IsConnected()) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 109 | Disconnect(); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | bool |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 113 | GDBRemoteCommunicationClient::HandshakeWithServer (Error *error_ptr) |
| 114 | { |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 115 | ResetDiscoverableSettings(); |
| 116 | |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 117 | // Start the read thread after we send the handshake ack since if we |
| 118 | // fail to send the handshake ack, there is no reason to continue... |
| 119 | if (SendAck()) |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 120 | { |
| 121 | // The return value from QueryNoAckModeSupported() is true if the packet |
| 122 | // was sent and _any_ response (including UNIMPLEMENTED) was received), |
| 123 | // or false if no response was received. This quickly tells us if we have |
| 124 | // a live connection to a remote GDB server... |
| 125 | if (QueryNoAckModeSupported()) |
| 126 | { |
| 127 | return true; |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | if (error_ptr) |
| 132 | error_ptr->SetErrorString("failed to get reply to handshake packet"); |
| 133 | } |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | if (error_ptr) |
| 138 | error_ptr->SetErrorString("failed to send the handshake ack"); |
| 139 | } |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 140 | return false; |
| 141 | } |
| 142 | |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 143 | bool |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 144 | GDBRemoteCommunicationClient::QueryNoAckModeSupported () |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 145 | { |
| 146 | if (m_supports_not_sending_acks == eLazyBoolCalculate) |
| 147 | { |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 148 | m_send_acks = true; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 149 | m_supports_not_sending_acks = eLazyBoolNo; |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 150 | |
| 151 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 152 | if (SendPacketAndWaitForResponse("QStartNoAckMode", response, false) == PacketResult::Success) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 153 | { |
| 154 | if (response.IsOKResponse()) |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 155 | { |
| 156 | m_send_acks = false; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 157 | m_supports_not_sending_acks = eLazyBoolYes; |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 158 | } |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 159 | return true; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 162 | return false; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void |
Greg Clayton | 4463399 | 2012-04-10 03:22:03 +0000 | [diff] [blame] | 166 | GDBRemoteCommunicationClient::GetListThreadsInStopReplySupported () |
| 167 | { |
| 168 | if (m_supports_threads_in_stop_reply == eLazyBoolCalculate) |
| 169 | { |
| 170 | m_supports_threads_in_stop_reply = eLazyBoolNo; |
| 171 | |
| 172 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 173 | if (SendPacketAndWaitForResponse("QListThreadsInStopReply", response, false) == PacketResult::Success) |
Greg Clayton | 4463399 | 2012-04-10 03:22:03 +0000 | [diff] [blame] | 174 | { |
| 175 | if (response.IsOKResponse()) |
| 176 | m_supports_threads_in_stop_reply = eLazyBoolYes; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
Jim Ingham | cd16df9 | 2012-07-20 21:37:13 +0000 | [diff] [blame] | 181 | bool |
| 182 | GDBRemoteCommunicationClient::GetVAttachOrWaitSupported () |
| 183 | { |
| 184 | if (m_attach_or_wait_reply == eLazyBoolCalculate) |
| 185 | { |
| 186 | m_attach_or_wait_reply = eLazyBoolNo; |
| 187 | |
| 188 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 189 | if (SendPacketAndWaitForResponse("qVAttachOrWaitSupported", response, false) == PacketResult::Success) |
Jim Ingham | cd16df9 | 2012-07-20 21:37:13 +0000 | [diff] [blame] | 190 | { |
| 191 | if (response.IsOKResponse()) |
| 192 | m_attach_or_wait_reply = eLazyBoolYes; |
| 193 | } |
| 194 | } |
| 195 | if (m_attach_or_wait_reply == eLazyBoolYes) |
| 196 | return true; |
| 197 | else |
| 198 | return false; |
| 199 | } |
| 200 | |
Jim Ingham | 279ceec | 2012-07-25 21:12:43 +0000 | [diff] [blame] | 201 | bool |
| 202 | GDBRemoteCommunicationClient::GetSyncThreadStateSupported () |
| 203 | { |
| 204 | if (m_prepare_for_reg_writing_reply == eLazyBoolCalculate) |
| 205 | { |
| 206 | m_prepare_for_reg_writing_reply = eLazyBoolNo; |
| 207 | |
| 208 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 209 | if (SendPacketAndWaitForResponse("qSyncThreadStateSupported", response, false) == PacketResult::Success) |
Jim Ingham | 279ceec | 2012-07-25 21:12:43 +0000 | [diff] [blame] | 210 | { |
| 211 | if (response.IsOKResponse()) |
| 212 | m_prepare_for_reg_writing_reply = eLazyBoolYes; |
| 213 | } |
| 214 | } |
| 215 | if (m_prepare_for_reg_writing_reply == eLazyBoolYes) |
| 216 | return true; |
| 217 | else |
| 218 | return false; |
| 219 | } |
| 220 | |
Greg Clayton | 4463399 | 2012-04-10 03:22:03 +0000 | [diff] [blame] | 221 | |
| 222 | void |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 223 | GDBRemoteCommunicationClient::ResetDiscoverableSettings() |
| 224 | { |
| 225 | m_supports_not_sending_acks = eLazyBoolCalculate; |
| 226 | m_supports_thread_suffix = eLazyBoolCalculate; |
Greg Clayton | 4463399 | 2012-04-10 03:22:03 +0000 | [diff] [blame] | 227 | m_supports_threads_in_stop_reply = eLazyBoolCalculate; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 228 | m_supports_vCont_c = eLazyBoolCalculate; |
| 229 | m_supports_vCont_C = eLazyBoolCalculate; |
| 230 | m_supports_vCont_s = eLazyBoolCalculate; |
| 231 | m_supports_vCont_S = eLazyBoolCalculate; |
Hafiz Abid Qadeer | 9a78cdf | 2013-08-29 09:09:45 +0000 | [diff] [blame] | 232 | m_supports_p = eLazyBoolCalculate; |
Greg Clayton | f74cf86 | 2013-11-13 23:28:31 +0000 | [diff] [blame] | 233 | m_supports_QSaveRegisterState = eLazyBoolCalculate; |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 234 | m_qHostInfo_is_valid = eLazyBoolCalculate; |
Jason Molenda | f17b5ac | 2012-12-19 02:54:03 +0000 | [diff] [blame] | 235 | m_qProcessInfo_is_valid = eLazyBoolCalculate; |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 236 | m_supports_alloc_dealloc_memory = eLazyBoolCalculate; |
Greg Clayton | 46fb558 | 2011-11-18 07:03:08 +0000 | [diff] [blame] | 237 | m_supports_memory_region_info = eLazyBoolCalculate; |
Jim Ingham | 279ceec | 2012-07-25 21:12:43 +0000 | [diff] [blame] | 238 | m_prepare_for_reg_writing_reply = eLazyBoolCalculate; |
| 239 | m_attach_or_wait_reply = eLazyBoolCalculate; |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 240 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 241 | m_supports_qProcessInfoPID = true; |
| 242 | m_supports_qfProcessInfo = true; |
| 243 | m_supports_qUserName = true; |
| 244 | m_supports_qGroupName = true; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 245 | m_supports_qThreadStopInfo = true; |
| 246 | m_supports_z0 = true; |
| 247 | m_supports_z1 = true; |
| 248 | m_supports_z2 = true; |
| 249 | m_supports_z3 = true; |
| 250 | m_supports_z4 = true; |
Greg Clayton | 8960058 | 2013-10-10 17:53:50 +0000 | [diff] [blame] | 251 | m_supports_QEnvironment = true; |
| 252 | m_supports_QEnvironmentHexEncoded = true; |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 253 | m_host_arch.Clear(); |
Jason Molenda | f17b5ac | 2012-12-19 02:54:03 +0000 | [diff] [blame] | 254 | m_process_arch.Clear(); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | |
| 258 | bool |
| 259 | GDBRemoteCommunicationClient::GetThreadSuffixSupported () |
| 260 | { |
| 261 | if (m_supports_thread_suffix == eLazyBoolCalculate) |
| 262 | { |
| 263 | StringExtractorGDBRemote response; |
| 264 | m_supports_thread_suffix = eLazyBoolNo; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 265 | if (SendPacketAndWaitForResponse("QThreadSuffixSupported", response, false) == PacketResult::Success) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 266 | { |
| 267 | if (response.IsOKResponse()) |
| 268 | m_supports_thread_suffix = eLazyBoolYes; |
| 269 | } |
| 270 | } |
| 271 | return m_supports_thread_suffix; |
| 272 | } |
| 273 | bool |
| 274 | GDBRemoteCommunicationClient::GetVContSupported (char flavor) |
| 275 | { |
| 276 | if (m_supports_vCont_c == eLazyBoolCalculate) |
| 277 | { |
| 278 | StringExtractorGDBRemote response; |
| 279 | m_supports_vCont_any = eLazyBoolNo; |
| 280 | m_supports_vCont_all = eLazyBoolNo; |
| 281 | m_supports_vCont_c = eLazyBoolNo; |
| 282 | m_supports_vCont_C = eLazyBoolNo; |
| 283 | m_supports_vCont_s = eLazyBoolNo; |
| 284 | m_supports_vCont_S = eLazyBoolNo; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 285 | if (SendPacketAndWaitForResponse("vCont?", response, false) == PacketResult::Success) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 286 | { |
| 287 | const char *response_cstr = response.GetStringRef().c_str(); |
| 288 | if (::strstr (response_cstr, ";c")) |
| 289 | m_supports_vCont_c = eLazyBoolYes; |
| 290 | |
| 291 | if (::strstr (response_cstr, ";C")) |
| 292 | m_supports_vCont_C = eLazyBoolYes; |
| 293 | |
| 294 | if (::strstr (response_cstr, ";s")) |
| 295 | m_supports_vCont_s = eLazyBoolYes; |
| 296 | |
| 297 | if (::strstr (response_cstr, ";S")) |
| 298 | m_supports_vCont_S = eLazyBoolYes; |
| 299 | |
| 300 | if (m_supports_vCont_c == eLazyBoolYes && |
| 301 | m_supports_vCont_C == eLazyBoolYes && |
| 302 | m_supports_vCont_s == eLazyBoolYes && |
| 303 | m_supports_vCont_S == eLazyBoolYes) |
| 304 | { |
| 305 | m_supports_vCont_all = eLazyBoolYes; |
| 306 | } |
| 307 | |
| 308 | if (m_supports_vCont_c == eLazyBoolYes || |
| 309 | m_supports_vCont_C == eLazyBoolYes || |
| 310 | m_supports_vCont_s == eLazyBoolYes || |
| 311 | m_supports_vCont_S == eLazyBoolYes) |
| 312 | { |
| 313 | m_supports_vCont_any = eLazyBoolYes; |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | switch (flavor) |
| 319 | { |
| 320 | case 'a': return m_supports_vCont_any; |
| 321 | case 'A': return m_supports_vCont_all; |
| 322 | case 'c': return m_supports_vCont_c; |
| 323 | case 'C': return m_supports_vCont_C; |
| 324 | case 's': return m_supports_vCont_s; |
| 325 | case 'S': return m_supports_vCont_S; |
| 326 | default: break; |
| 327 | } |
| 328 | return false; |
| 329 | } |
| 330 | |
Hafiz Abid Qadeer | 9a78cdf | 2013-08-29 09:09:45 +0000 | [diff] [blame] | 331 | // Check if the target supports 'p' packet. It sends out a 'p' |
| 332 | // packet and checks the response. A normal packet will tell us |
| 333 | // that support is available. |
Sean Callanan | b1de114 | 2013-09-04 23:24:15 +0000 | [diff] [blame] | 334 | // |
| 335 | // Takes a valid thread ID because p needs to apply to a thread. |
Hafiz Abid Qadeer | 9a78cdf | 2013-08-29 09:09:45 +0000 | [diff] [blame] | 336 | bool |
Sean Callanan | b1de114 | 2013-09-04 23:24:15 +0000 | [diff] [blame] | 337 | GDBRemoteCommunicationClient::GetpPacketSupported (lldb::tid_t tid) |
Hafiz Abid Qadeer | 9a78cdf | 2013-08-29 09:09:45 +0000 | [diff] [blame] | 338 | { |
| 339 | if (m_supports_p == eLazyBoolCalculate) |
| 340 | { |
| 341 | StringExtractorGDBRemote response; |
| 342 | m_supports_p = eLazyBoolNo; |
Sean Callanan | b1de114 | 2013-09-04 23:24:15 +0000 | [diff] [blame] | 343 | char packet[256]; |
| 344 | if (GetThreadSuffixSupported()) |
| 345 | snprintf(packet, sizeof(packet), "p0;thread:%" PRIx64 ";", tid); |
| 346 | else |
| 347 | snprintf(packet, sizeof(packet), "p0"); |
| 348 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 349 | if (SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success) |
Hafiz Abid Qadeer | 9a78cdf | 2013-08-29 09:09:45 +0000 | [diff] [blame] | 350 | { |
| 351 | if (response.IsNormalResponse()) |
| 352 | m_supports_p = eLazyBoolYes; |
| 353 | } |
| 354 | } |
| 355 | return m_supports_p; |
| 356 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 357 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 358 | GDBRemoteCommunicationClient::PacketResult |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 359 | GDBRemoteCommunicationClient::SendPacketAndWaitForResponse |
| 360 | ( |
| 361 | const char *payload, |
| 362 | StringExtractorGDBRemote &response, |
| 363 | bool send_async |
| 364 | ) |
| 365 | { |
| 366 | return SendPacketAndWaitForResponse (payload, |
| 367 | ::strlen (payload), |
| 368 | response, |
| 369 | send_async); |
| 370 | } |
| 371 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 372 | GDBRemoteCommunicationClient::PacketResult |
| 373 | GDBRemoteCommunicationClient::SendPacketAndWaitForResponseNoLock (const char *payload, |
| 374 | size_t payload_length, |
| 375 | StringExtractorGDBRemote &response) |
| 376 | { |
| 377 | PacketResult packet_result = SendPacketNoLock (payload, payload_length); |
| 378 | if (packet_result == PacketResult::Success) |
| 379 | packet_result = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ()); |
| 380 | return packet_result; |
| 381 | } |
| 382 | |
| 383 | GDBRemoteCommunicationClient::PacketResult |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 384 | GDBRemoteCommunicationClient::SendPacketAndWaitForResponse |
| 385 | ( |
| 386 | const char *payload, |
| 387 | size_t payload_length, |
| 388 | StringExtractorGDBRemote &response, |
| 389 | bool send_async |
| 390 | ) |
| 391 | { |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 392 | PacketResult packet_result = PacketResult::ErrorSendFailed; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 393 | Mutex::Locker locker; |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 394 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Greg Clayton | 644247c | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 395 | size_t response_len = 0; |
Greg Clayton | c3c0b0e | 2012-04-12 19:04:34 +0000 | [diff] [blame] | 396 | if (GetSequenceMutex (locker)) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 397 | { |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 398 | packet_result = SendPacketAndWaitForResponseNoLock (payload, payload_length, response); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 399 | } |
| 400 | else |
| 401 | { |
| 402 | if (send_async) |
| 403 | { |
Greg Clayton | d354405 | 2012-05-31 21:24:20 +0000 | [diff] [blame] | 404 | if (IsRunning()) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 405 | { |
Greg Clayton | d354405 | 2012-05-31 21:24:20 +0000 | [diff] [blame] | 406 | Mutex::Locker async_locker (m_async_mutex); |
| 407 | m_async_packet.assign(payload, payload_length); |
| 408 | m_async_packet_predicate.SetValue (true, eBroadcastNever); |
| 409 | |
| 410 | if (log) |
| 411 | log->Printf ("async: async packet = %s", m_async_packet.c_str()); |
| 412 | |
| 413 | bool timed_out = false; |
| 414 | if (SendInterrupt(locker, 2, timed_out)) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 415 | { |
Greg Clayton | d354405 | 2012-05-31 21:24:20 +0000 | [diff] [blame] | 416 | if (m_interrupt_sent) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 417 | { |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 418 | m_interrupt_sent = false; |
Greg Clayton | d354405 | 2012-05-31 21:24:20 +0000 | [diff] [blame] | 419 | TimeValue timeout_time; |
| 420 | timeout_time = TimeValue::Now(); |
| 421 | timeout_time.OffsetWithSeconds (m_packet_timeout); |
| 422 | |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 423 | if (log) |
Greg Clayton | d354405 | 2012-05-31 21:24:20 +0000 | [diff] [blame] | 424 | log->Printf ("async: sent interrupt"); |
Greg Clayton | 644247c | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 425 | |
Greg Clayton | d354405 | 2012-05-31 21:24:20 +0000 | [diff] [blame] | 426 | if (m_async_packet_predicate.WaitForValueEqualTo (false, &timeout_time, &timed_out)) |
Greg Clayton | e889ad6 | 2011-10-27 22:04:16 +0000 | [diff] [blame] | 427 | { |
Greg Clayton | d354405 | 2012-05-31 21:24:20 +0000 | [diff] [blame] | 428 | if (log) |
| 429 | log->Printf ("async: got response"); |
| 430 | |
| 431 | // Swap the response buffer to avoid malloc and string copy |
| 432 | response.GetStringRef().swap (m_async_response.GetStringRef()); |
| 433 | response_len = response.GetStringRef().size(); |
Jim Ingham | a6195b7 | 2013-12-18 01:24:33 +0000 | [diff] [blame^] | 434 | packet_result = m_async_result; |
Greg Clayton | d354405 | 2012-05-31 21:24:20 +0000 | [diff] [blame] | 435 | } |
| 436 | else |
| 437 | { |
| 438 | if (log) |
| 439 | log->Printf ("async: timed out waiting for response"); |
| 440 | } |
| 441 | |
| 442 | // Make sure we wait until the continue packet has been sent again... |
| 443 | if (m_private_is_running.WaitForValueEqualTo (true, &timeout_time, &timed_out)) |
| 444 | { |
| 445 | if (log) |
| 446 | { |
| 447 | if (timed_out) |
| 448 | log->Printf ("async: timed out waiting for process to resume, but process was resumed"); |
| 449 | else |
| 450 | log->Printf ("async: async packet sent"); |
| 451 | } |
| 452 | } |
| 453 | else |
| 454 | { |
| 455 | if (log) |
| 456 | log->Printf ("async: timed out waiting for process to resume"); |
Greg Clayton | e889ad6 | 2011-10-27 22:04:16 +0000 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | else |
| 460 | { |
Greg Clayton | d354405 | 2012-05-31 21:24:20 +0000 | [diff] [blame] | 461 | // We had a racy condition where we went to send the interrupt |
| 462 | // yet we were able to get the lock, so the process must have |
| 463 | // just stopped? |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 464 | if (log) |
Greg Clayton | d354405 | 2012-05-31 21:24:20 +0000 | [diff] [blame] | 465 | log->Printf ("async: got lock without sending interrupt"); |
| 466 | // Send the packet normally since we got the lock |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 467 | packet_result = SendPacketAndWaitForResponseNoLock (payload, payload_length, response); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | else |
| 471 | { |
Greg Clayton | 644247c | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 472 | if (log) |
Greg Clayton | d354405 | 2012-05-31 21:24:20 +0000 | [diff] [blame] | 473 | log->Printf ("async: failed to interrupt"); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | else |
| 477 | { |
| 478 | if (log) |
Greg Clayton | d354405 | 2012-05-31 21:24:20 +0000 | [diff] [blame] | 479 | log->Printf ("async: not running, async is ignored"); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | else |
| 483 | { |
| 484 | if (log) |
Greg Clayton | c3c0b0e | 2012-04-12 19:04:34 +0000 | [diff] [blame] | 485 | log->Printf("error: failed to get packet sequence mutex, not sending packet '%*s'", (int) payload_length, payload); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 486 | } |
| 487 | } |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 488 | return packet_result; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Han Ming Ong | 4b6459f | 2013-01-18 23:11:53 +0000 | [diff] [blame] | 491 | static const char *end_delimiter = "--end--;"; |
| 492 | static const int end_delimiter_len = 8; |
| 493 | |
| 494 | std::string |
| 495 | GDBRemoteCommunicationClient::HarmonizeThreadIdsForProfileData |
| 496 | ( ProcessGDBRemote *process, |
| 497 | StringExtractorGDBRemote& profileDataExtractor |
| 498 | ) |
| 499 | { |
| 500 | std::map<uint64_t, uint32_t> new_thread_id_to_used_usec_map; |
| 501 | std::stringstream final_output; |
| 502 | std::string name, value; |
| 503 | |
| 504 | // Going to assuming thread_used_usec comes first, else bail out. |
| 505 | while (profileDataExtractor.GetNameColonValue(name, value)) |
| 506 | { |
| 507 | if (name.compare("thread_used_id") == 0) |
| 508 | { |
| 509 | StringExtractor threadIDHexExtractor(value.c_str()); |
| 510 | uint64_t thread_id = threadIDHexExtractor.GetHexMaxU64(false, 0); |
| 511 | |
| 512 | bool has_used_usec = false; |
| 513 | uint32_t curr_used_usec = 0; |
| 514 | std::string usec_name, usec_value; |
| 515 | uint32_t input_file_pos = profileDataExtractor.GetFilePos(); |
| 516 | if (profileDataExtractor.GetNameColonValue(usec_name, usec_value)) |
| 517 | { |
| 518 | if (usec_name.compare("thread_used_usec") == 0) |
| 519 | { |
| 520 | has_used_usec = true; |
| 521 | curr_used_usec = strtoull(usec_value.c_str(), NULL, 0); |
| 522 | } |
| 523 | else |
| 524 | { |
| 525 | // We didn't find what we want, it is probably |
| 526 | // an older version. Bail out. |
| 527 | profileDataExtractor.SetFilePos(input_file_pos); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | if (has_used_usec) |
| 532 | { |
| 533 | uint32_t prev_used_usec = 0; |
| 534 | std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_used_usec_map.find(thread_id); |
| 535 | if (iterator != m_thread_id_to_used_usec_map.end()) |
| 536 | { |
| 537 | prev_used_usec = m_thread_id_to_used_usec_map[thread_id]; |
| 538 | } |
| 539 | |
| 540 | uint32_t real_used_usec = curr_used_usec - prev_used_usec; |
| 541 | // A good first time record is one that runs for at least 0.25 sec |
| 542 | bool good_first_time = (prev_used_usec == 0) && (real_used_usec > 250000); |
| 543 | bool good_subsequent_time = (prev_used_usec > 0) && |
| 544 | ((real_used_usec > 0) || (process->HasAssignedIndexIDToThread(thread_id))); |
| 545 | |
| 546 | if (good_first_time || good_subsequent_time) |
| 547 | { |
| 548 | // We try to avoid doing too many index id reservation, |
| 549 | // resulting in fast increase of index ids. |
| 550 | |
| 551 | final_output << name << ":"; |
| 552 | int32_t index_id = process->AssignIndexIDToThread(thread_id); |
| 553 | final_output << index_id << ";"; |
| 554 | |
| 555 | final_output << usec_name << ":" << usec_value << ";"; |
| 556 | } |
| 557 | else |
| 558 | { |
| 559 | // Skip past 'thread_used_name'. |
| 560 | std::string local_name, local_value; |
| 561 | profileDataExtractor.GetNameColonValue(local_name, local_value); |
| 562 | } |
| 563 | |
| 564 | // Store current time as previous time so that they can be compared later. |
| 565 | new_thread_id_to_used_usec_map[thread_id] = curr_used_usec; |
| 566 | } |
| 567 | else |
| 568 | { |
| 569 | // Bail out and use old string. |
| 570 | final_output << name << ":" << value << ";"; |
| 571 | } |
| 572 | } |
| 573 | else |
| 574 | { |
| 575 | final_output << name << ":" << value << ";"; |
| 576 | } |
| 577 | } |
| 578 | final_output << end_delimiter; |
| 579 | m_thread_id_to_used_usec_map = new_thread_id_to_used_usec_map; |
| 580 | |
| 581 | return final_output.str(); |
| 582 | } |
| 583 | |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 584 | StateType |
| 585 | GDBRemoteCommunicationClient::SendContinuePacketAndWaitForResponse |
| 586 | ( |
| 587 | ProcessGDBRemote *process, |
| 588 | const char *payload, |
| 589 | size_t packet_length, |
| 590 | StringExtractorGDBRemote &response |
| 591 | ) |
| 592 | { |
Greg Clayton | 1f5181a | 2012-07-02 22:05:25 +0000 | [diff] [blame] | 593 | m_curr_tid = LLDB_INVALID_THREAD_ID; |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 594 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 595 | if (log) |
| 596 | log->Printf ("GDBRemoteCommunicationClient::%s ()", __FUNCTION__); |
| 597 | |
| 598 | Mutex::Locker locker(m_sequence_mutex); |
| 599 | StateType state = eStateRunning; |
| 600 | |
| 601 | BroadcastEvent(eBroadcastBitRunPacketSent, NULL); |
| 602 | m_public_is_running.SetValue (true, eBroadcastNever); |
| 603 | // Set the starting continue packet into "continue_packet". This packet |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 604 | // may change if we are interrupted and we continue after an async packet... |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 605 | std::string continue_packet(payload, packet_length); |
| 606 | |
Greg Clayton | 3f875c5 | 2013-02-22 22:23:55 +0000 | [diff] [blame] | 607 | bool got_async_packet = false; |
Greg Clayton | af247d7 | 2011-05-19 03:54:16 +0000 | [diff] [blame] | 608 | |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 609 | while (state == eStateRunning) |
| 610 | { |
Greg Clayton | 3f875c5 | 2013-02-22 22:23:55 +0000 | [diff] [blame] | 611 | if (!got_async_packet) |
Greg Clayton | af247d7 | 2011-05-19 03:54:16 +0000 | [diff] [blame] | 612 | { |
| 613 | if (log) |
| 614 | log->Printf ("GDBRemoteCommunicationClient::%s () sending continue packet: %s", __FUNCTION__, continue_packet.c_str()); |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 615 | if (SendPacketNoLock(continue_packet.c_str(), continue_packet.size()) != PacketResult::Success) |
Greg Clayton | af247d7 | 2011-05-19 03:54:16 +0000 | [diff] [blame] | 616 | state = eStateInvalid; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 617 | |
Greg Clayton | e889ad6 | 2011-10-27 22:04:16 +0000 | [diff] [blame] | 618 | m_private_is_running.SetValue (true, eBroadcastAlways); |
Greg Clayton | af247d7 | 2011-05-19 03:54:16 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Greg Clayton | 3f875c5 | 2013-02-22 22:23:55 +0000 | [diff] [blame] | 621 | got_async_packet = false; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 622 | |
| 623 | if (log) |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 624 | log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(%s)", __FUNCTION__, continue_packet.c_str()); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 625 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 626 | if (WaitForPacketWithTimeoutMicroSecondsNoLock(response, UINT32_MAX) == PacketResult::Success) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 627 | { |
| 628 | if (response.Empty()) |
| 629 | state = eStateInvalid; |
| 630 | else |
| 631 | { |
| 632 | const char stop_type = response.GetChar(); |
| 633 | if (log) |
| 634 | log->Printf ("GDBRemoteCommunicationClient::%s () got packet: %s", __FUNCTION__, response.GetStringRef().c_str()); |
| 635 | switch (stop_type) |
| 636 | { |
| 637 | case 'T': |
| 638 | case 'S': |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 639 | { |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 640 | if (process->GetStopID() == 0) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 641 | { |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 642 | if (process->GetID() == LLDB_INVALID_PROCESS_ID) |
| 643 | { |
| 644 | lldb::pid_t pid = GetCurrentProcessID (); |
| 645 | if (pid != LLDB_INVALID_PROCESS_ID) |
| 646 | process->SetID (pid); |
| 647 | } |
| 648 | process->BuildDynamicRegisterInfo (true); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 649 | } |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 650 | |
| 651 | // Privately notify any internal threads that we have stopped |
| 652 | // in case we wanted to interrupt our process, yet we might |
| 653 | // send a packet and continue without returning control to the |
| 654 | // user. |
| 655 | m_private_is_running.SetValue (false, eBroadcastAlways); |
| 656 | |
| 657 | const uint8_t signo = response.GetHexU8 (UINT8_MAX); |
| 658 | |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 659 | bool continue_after_async = m_async_signal != -1 || m_async_packet_predicate.GetValue(); |
| 660 | if (continue_after_async || m_interrupt_sent) |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 661 | { |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 662 | // We sent an interrupt packet to stop the inferior process |
| 663 | // for an async signal or to send an async packet while running |
| 664 | // but we might have been single stepping and received the |
| 665 | // stop packet for the step instead of for the interrupt packet. |
| 666 | // Typically when an interrupt is sent a SIGINT or SIGSTOP |
| 667 | // is used, so if we get anything else, we need to try and |
| 668 | // get another stop reply packet that may have been sent |
| 669 | // due to sending the interrupt when the target is stopped |
| 670 | // which will just re-send a copy of the last stop reply |
| 671 | // packet. If we don't do this, then the reply for our |
| 672 | // async packet will be the repeat stop reply packet and cause |
| 673 | // a lot of trouble for us! |
| 674 | if (signo != SIGINT && signo != SIGSTOP) |
| 675 | { |
Greg Clayton | fb72fde | 2012-05-15 02:50:49 +0000 | [diff] [blame] | 676 | continue_after_async = false; |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 677 | |
| 678 | // We didn't get a a SIGINT or SIGSTOP, so try for a |
| 679 | // very brief time (1 ms) to get another stop reply |
| 680 | // packet to make sure it doesn't get in the way |
| 681 | StringExtractorGDBRemote extra_stop_reply_packet; |
| 682 | uint32_t timeout_usec = 1000; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 683 | if (WaitForPacketWithTimeoutMicroSecondsNoLock (extra_stop_reply_packet, timeout_usec) == PacketResult::Success) |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 684 | { |
| 685 | switch (extra_stop_reply_packet.GetChar()) |
| 686 | { |
| 687 | case 'T': |
| 688 | case 'S': |
| 689 | // We did get an extra stop reply, which means |
| 690 | // our interrupt didn't stop the target so we |
| 691 | // shouldn't continue after the async signal |
| 692 | // or packet is sent... |
Greg Clayton | fb72fde | 2012-05-15 02:50:49 +0000 | [diff] [blame] | 693 | continue_after_async = false; |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 694 | break; |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | if (m_async_signal != -1) |
| 701 | { |
| 702 | if (log) |
| 703 | log->Printf ("async: send signo = %s", Host::GetSignalAsCString (m_async_signal)); |
| 704 | |
| 705 | // Save off the async signal we are supposed to send |
| 706 | const int async_signal = m_async_signal; |
| 707 | // Clear the async signal member so we don't end up |
| 708 | // sending the signal multiple times... |
| 709 | m_async_signal = -1; |
| 710 | // Check which signal we stopped with |
| 711 | if (signo == async_signal) |
| 712 | { |
| 713 | if (log) |
| 714 | log->Printf ("async: stopped with signal %s, we are done running", Host::GetSignalAsCString (signo)); |
| 715 | |
| 716 | // We already stopped with a signal that we wanted |
| 717 | // to stop with, so we are done |
| 718 | } |
| 719 | else |
| 720 | { |
| 721 | // We stopped with a different signal that the one |
| 722 | // we wanted to stop with, so now we must resume |
| 723 | // with the signal we want |
| 724 | char signal_packet[32]; |
| 725 | int signal_packet_len = 0; |
| 726 | signal_packet_len = ::snprintf (signal_packet, |
| 727 | sizeof (signal_packet), |
| 728 | "C%2.2x", |
| 729 | async_signal); |
| 730 | |
| 731 | if (log) |
| 732 | log->Printf ("async: stopped with signal %s, resume with %s", |
| 733 | Host::GetSignalAsCString (signo), |
| 734 | Host::GetSignalAsCString (async_signal)); |
| 735 | |
| 736 | // Set the continue packet to resume even if the |
Greg Clayton | fb72fde | 2012-05-15 02:50:49 +0000 | [diff] [blame] | 737 | // interrupt didn't cause our stop (ignore continue_after_async) |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 738 | continue_packet.assign(signal_packet, signal_packet_len); |
| 739 | continue; |
| 740 | } |
| 741 | } |
| 742 | else if (m_async_packet_predicate.GetValue()) |
| 743 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 744 | Log * packet_log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 745 | |
| 746 | // We are supposed to send an asynchronous packet while |
Jim Ingham | a6195b7 | 2013-12-18 01:24:33 +0000 | [diff] [blame^] | 747 | // we are running. |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 748 | m_async_response.Clear(); |
| 749 | if (m_async_packet.empty()) |
| 750 | { |
Jim Ingham | a6195b7 | 2013-12-18 01:24:33 +0000 | [diff] [blame^] | 751 | m_async_result = PacketResult::ErrorSendFailed; |
| 752 | if (packet_log) |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 753 | packet_log->Printf ("async: error: empty async packet"); |
| 754 | |
| 755 | } |
| 756 | else |
| 757 | { |
| 758 | if (packet_log) |
| 759 | packet_log->Printf ("async: sending packet"); |
| 760 | |
Jim Ingham | a6195b7 | 2013-12-18 01:24:33 +0000 | [diff] [blame^] | 761 | m_async_result = SendPacketAndWaitForResponse (&m_async_packet[0], |
| 762 | m_async_packet.size(), |
| 763 | m_async_response, |
| 764 | false); |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 765 | } |
| 766 | // Let the other thread that was trying to send the async |
| 767 | // packet know that the packet has been sent and response is |
| 768 | // ready... |
| 769 | m_async_packet_predicate.SetValue(false, eBroadcastAlways); |
| 770 | |
| 771 | if (packet_log) |
Greg Clayton | fb72fde | 2012-05-15 02:50:49 +0000 | [diff] [blame] | 772 | packet_log->Printf ("async: sent packet, continue_after_async = %i", continue_after_async); |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 773 | |
| 774 | // Set the continue packet to resume if our interrupt |
| 775 | // for the async packet did cause the stop |
Greg Clayton | fb72fde | 2012-05-15 02:50:49 +0000 | [diff] [blame] | 776 | if (continue_after_async) |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 777 | { |
Greg Clayton | f1186de | 2012-05-24 23:42:14 +0000 | [diff] [blame] | 778 | // Reverting this for now as it is causing deadlocks |
| 779 | // in programs (<rdar://problem/11529853>). In the future |
| 780 | // we should check our thread list and "do the right thing" |
| 781 | // for new threads that show up while we stop and run async |
| 782 | // packets. Setting the packet to 'c' to continue all threads |
| 783 | // is the right thing to do 99.99% of the time because if a |
| 784 | // thread was single stepping, and we sent an interrupt, we |
| 785 | // will notice above that we didn't stop due to an interrupt |
| 786 | // but stopped due to stepping and we would _not_ continue. |
| 787 | continue_packet.assign (1, 'c'); |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 788 | continue; |
| 789 | } |
| 790 | } |
| 791 | // Stop with signal and thread info |
| 792 | state = eStateStopped; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 793 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 794 | break; |
| 795 | |
| 796 | case 'W': |
| 797 | case 'X': |
| 798 | // process exited |
| 799 | state = eStateExited; |
| 800 | break; |
| 801 | |
| 802 | case 'O': |
| 803 | // STDOUT |
| 804 | { |
Greg Clayton | 3f875c5 | 2013-02-22 22:23:55 +0000 | [diff] [blame] | 805 | got_async_packet = true; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 806 | std::string inferior_stdout; |
| 807 | inferior_stdout.reserve(response.GetBytesLeft () / 2); |
| 808 | char ch; |
| 809 | while ((ch = response.GetHexU8()) != '\0') |
| 810 | inferior_stdout.append(1, ch); |
| 811 | process->AppendSTDOUT (inferior_stdout.c_str(), inferior_stdout.size()); |
| 812 | } |
| 813 | break; |
| 814 | |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 815 | case 'A': |
| 816 | // Async miscellaneous reply. Right now, only profile data is coming through this channel. |
| 817 | { |
Greg Clayton | 3f875c5 | 2013-02-22 22:23:55 +0000 | [diff] [blame] | 818 | got_async_packet = true; |
Han Ming Ong | 4b6459f | 2013-01-18 23:11:53 +0000 | [diff] [blame] | 819 | std::string input = response.GetStringRef().substr(1); // '1' to move beyond 'A' |
| 820 | if (m_partial_profile_data.length() > 0) |
| 821 | { |
| 822 | m_partial_profile_data.append(input); |
| 823 | input = m_partial_profile_data; |
| 824 | m_partial_profile_data.clear(); |
| 825 | } |
| 826 | |
| 827 | size_t found, pos = 0, len = input.length(); |
| 828 | while ((found = input.find(end_delimiter, pos)) != std::string::npos) |
| 829 | { |
| 830 | StringExtractorGDBRemote profileDataExtractor(input.substr(pos, found).c_str()); |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 831 | std::string profile_data = HarmonizeThreadIdsForProfileData(process, profileDataExtractor); |
| 832 | process->BroadcastAsyncProfileData (profile_data); |
Han Ming Ong | 4b6459f | 2013-01-18 23:11:53 +0000 | [diff] [blame] | 833 | |
| 834 | pos = found + end_delimiter_len; |
| 835 | } |
| 836 | |
| 837 | if (pos < len) |
| 838 | { |
| 839 | // Last incomplete chunk. |
| 840 | m_partial_profile_data = input.substr(pos); |
| 841 | } |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 842 | } |
| 843 | break; |
| 844 | |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 845 | case 'E': |
| 846 | // ERROR |
| 847 | state = eStateInvalid; |
| 848 | break; |
| 849 | |
| 850 | default: |
| 851 | if (log) |
| 852 | log->Printf ("GDBRemoteCommunicationClient::%s () unrecognized async packet", __FUNCTION__); |
| 853 | state = eStateInvalid; |
| 854 | break; |
| 855 | } |
| 856 | } |
| 857 | } |
| 858 | else |
| 859 | { |
| 860 | if (log) |
| 861 | log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(...) => false", __FUNCTION__); |
| 862 | state = eStateInvalid; |
| 863 | } |
| 864 | } |
| 865 | if (log) |
| 866 | log->Printf ("GDBRemoteCommunicationClient::%s () => %s", __FUNCTION__, StateAsCString(state)); |
| 867 | response.SetFilePos(0); |
| 868 | m_private_is_running.SetValue (false, eBroadcastAlways); |
| 869 | m_public_is_running.SetValue (false, eBroadcastAlways); |
| 870 | return state; |
| 871 | } |
| 872 | |
| 873 | bool |
| 874 | GDBRemoteCommunicationClient::SendAsyncSignal (int signo) |
| 875 | { |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 876 | Mutex::Locker async_locker (m_async_mutex); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 877 | m_async_signal = signo; |
| 878 | bool timed_out = false; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 879 | Mutex::Locker locker; |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 880 | if (SendInterrupt (locker, 1, timed_out)) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 881 | return true; |
| 882 | m_async_signal = -1; |
| 883 | return false; |
| 884 | } |
| 885 | |
Greg Clayton | 37a0a24 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 886 | // This function takes a mutex locker as a parameter in case the GetSequenceMutex |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 887 | // actually succeeds. If it doesn't succeed in acquiring the sequence mutex |
| 888 | // (the expected result), then it will send the halt packet. If it does succeed |
| 889 | // then the caller that requested the interrupt will want to keep the sequence |
| 890 | // locked down so that no one else can send packets while the caller has control. |
| 891 | // This function usually gets called when we are running and need to stop the |
| 892 | // target. It can also be used when we are running and and we need to do something |
| 893 | // else (like read/write memory), so we need to interrupt the running process |
| 894 | // (gdb remote protocol requires this), and do what we need to do, then resume. |
| 895 | |
| 896 | bool |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 897 | GDBRemoteCommunicationClient::SendInterrupt |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 898 | ( |
| 899 | Mutex::Locker& locker, |
| 900 | uint32_t seconds_to_wait_for_stop, |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 901 | bool &timed_out |
| 902 | ) |
| 903 | { |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 904 | timed_out = false; |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 905 | Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS)); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 906 | |
| 907 | if (IsRunning()) |
| 908 | { |
| 909 | // Only send an interrupt if our debugserver is running... |
Greg Clayton | c3c0b0e | 2012-04-12 19:04:34 +0000 | [diff] [blame] | 910 | if (GetSequenceMutex (locker)) |
Greg Clayton | 37a0a24 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 911 | { |
| 912 | if (log) |
| 913 | log->Printf ("SendInterrupt () - got sequence mutex without having to interrupt"); |
| 914 | } |
| 915 | else |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 916 | { |
| 917 | // Someone has the mutex locked waiting for a response or for the |
| 918 | // inferior to stop, so send the interrupt on the down low... |
| 919 | char ctrl_c = '\x03'; |
| 920 | ConnectionStatus status = eConnectionStatusSuccess; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 921 | size_t bytes_written = Write (&ctrl_c, 1, status, NULL); |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 922 | if (log) |
| 923 | log->PutCString("send packet: \\x03"); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 924 | if (bytes_written > 0) |
| 925 | { |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 926 | m_interrupt_sent = true; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 927 | if (seconds_to_wait_for_stop) |
| 928 | { |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 929 | TimeValue timeout; |
| 930 | if (seconds_to_wait_for_stop) |
| 931 | { |
| 932 | timeout = TimeValue::Now(); |
| 933 | timeout.OffsetWithSeconds (seconds_to_wait_for_stop); |
| 934 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 935 | if (m_private_is_running.WaitForValueEqualTo (false, &timeout, &timed_out)) |
| 936 | { |
| 937 | if (log) |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 938 | log->PutCString ("SendInterrupt () - sent interrupt, private state stopped"); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 939 | return true; |
| 940 | } |
| 941 | else |
| 942 | { |
| 943 | if (log) |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 944 | log->Printf ("SendInterrupt () - sent interrupt, timed out wating for async thread resume"); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | else |
| 948 | { |
| 949 | if (log) |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 950 | log->Printf ("SendInterrupt () - sent interrupt, not waiting for stop..."); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 951 | return true; |
| 952 | } |
| 953 | } |
| 954 | else |
| 955 | { |
| 956 | if (log) |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 957 | log->Printf ("SendInterrupt () - failed to write interrupt"); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 958 | } |
| 959 | return false; |
| 960 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 961 | } |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 962 | else |
| 963 | { |
| 964 | if (log) |
| 965 | log->Printf ("SendInterrupt () - not running"); |
| 966 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 967 | return true; |
| 968 | } |
| 969 | |
| 970 | lldb::pid_t |
| 971 | GDBRemoteCommunicationClient::GetCurrentProcessID () |
| 972 | { |
| 973 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 974 | if (SendPacketAndWaitForResponse("qC", strlen("qC"), response, false) == PacketResult::Success) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 975 | { |
| 976 | if (response.GetChar() == 'Q') |
| 977 | if (response.GetChar() == 'C') |
| 978 | return response.GetHexMaxU32 (false, LLDB_INVALID_PROCESS_ID); |
| 979 | } |
| 980 | return LLDB_INVALID_PROCESS_ID; |
| 981 | } |
| 982 | |
| 983 | bool |
| 984 | GDBRemoteCommunicationClient::GetLaunchSuccess (std::string &error_str) |
| 985 | { |
| 986 | error_str.clear(); |
| 987 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 988 | if (SendPacketAndWaitForResponse("qLaunchSuccess", strlen("qLaunchSuccess"), response, false) == PacketResult::Success) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 989 | { |
| 990 | if (response.IsOKResponse()) |
| 991 | return true; |
| 992 | if (response.GetChar() == 'E') |
| 993 | { |
| 994 | // A string the describes what failed when launching... |
| 995 | error_str = response.GetStringRef().substr(1); |
| 996 | } |
| 997 | else |
| 998 | { |
| 999 | error_str.assign ("unknown error occurred launching process"); |
| 1000 | } |
| 1001 | } |
| 1002 | else |
| 1003 | { |
Jim Ingham | 98d6da5 | 2012-06-28 20:30:23 +0000 | [diff] [blame] | 1004 | error_str.assign ("timed out waiting for app to launch"); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1005 | } |
| 1006 | return false; |
| 1007 | } |
| 1008 | |
| 1009 | int |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1010 | GDBRemoteCommunicationClient::SendArgumentsPacket (const ProcessLaunchInfo &launch_info) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1011 | { |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1012 | // Since we don't get the send argv0 separate from the executable path, we need to |
| 1013 | // make sure to use the actual exectuable path found in the launch_info... |
| 1014 | std::vector<const char *> argv; |
| 1015 | FileSpec exe_file = launch_info.GetExecutableFile(); |
| 1016 | std::string exe_path; |
| 1017 | const char *arg = NULL; |
| 1018 | const Args &launch_args = launch_info.GetArguments(); |
| 1019 | if (exe_file) |
| 1020 | exe_path = exe_file.GetPath(); |
| 1021 | else |
| 1022 | { |
| 1023 | arg = launch_args.GetArgumentAtIndex(0); |
| 1024 | if (arg) |
| 1025 | exe_path = arg; |
| 1026 | } |
| 1027 | if (!exe_path.empty()) |
| 1028 | { |
| 1029 | argv.push_back(exe_path.c_str()); |
| 1030 | for (uint32_t i=1; (arg = launch_args.GetArgumentAtIndex(i)) != NULL; ++i) |
| 1031 | { |
| 1032 | if (arg) |
| 1033 | argv.push_back(arg); |
| 1034 | } |
| 1035 | } |
| 1036 | if (!argv.empty()) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1037 | { |
| 1038 | StreamString packet; |
| 1039 | packet.PutChar('A'); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1040 | for (size_t i = 0, n = argv.size(); i < n; ++i) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1041 | { |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1042 | arg = argv[i]; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1043 | const int arg_len = strlen(arg); |
| 1044 | if (i > 0) |
| 1045 | packet.PutChar(','); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1046 | packet.Printf("%i,%i,", arg_len * 2, (int)i); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1047 | packet.PutBytesAsRawHex8 (arg, arg_len); |
| 1048 | } |
| 1049 | |
| 1050 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1051 | if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1052 | { |
| 1053 | if (response.IsOKResponse()) |
| 1054 | return 0; |
| 1055 | uint8_t error = response.GetError(); |
| 1056 | if (error) |
| 1057 | return error; |
| 1058 | } |
| 1059 | } |
| 1060 | return -1; |
| 1061 | } |
| 1062 | |
| 1063 | int |
| 1064 | GDBRemoteCommunicationClient::SendEnvironmentPacket (char const *name_equal_value) |
| 1065 | { |
| 1066 | if (name_equal_value && name_equal_value[0]) |
| 1067 | { |
| 1068 | StreamString packet; |
Greg Clayton | 8960058 | 2013-10-10 17:53:50 +0000 | [diff] [blame] | 1069 | bool send_hex_encoding = false; |
| 1070 | for (const char *p = name_equal_value; *p != '\0' && send_hex_encoding == false; ++p) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1071 | { |
Greg Clayton | 8960058 | 2013-10-10 17:53:50 +0000 | [diff] [blame] | 1072 | if (isprint(*p)) |
| 1073 | { |
| 1074 | switch (*p) |
| 1075 | { |
| 1076 | case '$': |
| 1077 | case '#': |
| 1078 | send_hex_encoding = true; |
| 1079 | break; |
| 1080 | default: |
| 1081 | break; |
| 1082 | } |
| 1083 | } |
| 1084 | else |
| 1085 | { |
| 1086 | // We have non printable characters, lets hex encode this... |
| 1087 | send_hex_encoding = true; |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | StringExtractorGDBRemote response; |
| 1092 | if (send_hex_encoding) |
| 1093 | { |
| 1094 | if (m_supports_QEnvironmentHexEncoded) |
| 1095 | { |
| 1096 | packet.PutCString("QEnvironmentHexEncoded:"); |
| 1097 | packet.PutBytesAsRawHex8 (name_equal_value, strlen(name_equal_value)); |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1098 | if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success) |
Greg Clayton | 8960058 | 2013-10-10 17:53:50 +0000 | [diff] [blame] | 1099 | { |
| 1100 | if (response.IsOKResponse()) |
| 1101 | return 0; |
| 1102 | uint8_t error = response.GetError(); |
| 1103 | if (error) |
| 1104 | return error; |
| 1105 | if (response.IsUnsupportedResponse()) |
| 1106 | m_supports_QEnvironmentHexEncoded = false; |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | } |
| 1111 | else if (m_supports_QEnvironment) |
| 1112 | { |
| 1113 | packet.Printf("QEnvironment:%s", name_equal_value); |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1114 | if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success) |
Greg Clayton | 8960058 | 2013-10-10 17:53:50 +0000 | [diff] [blame] | 1115 | { |
| 1116 | if (response.IsOKResponse()) |
| 1117 | return 0; |
| 1118 | uint8_t error = response.GetError(); |
| 1119 | if (error) |
| 1120 | return error; |
| 1121 | if (response.IsUnsupportedResponse()) |
| 1122 | m_supports_QEnvironment = false; |
| 1123 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1124 | } |
| 1125 | } |
| 1126 | return -1; |
| 1127 | } |
| 1128 | |
Greg Clayton | c4103b3 | 2011-05-08 04:53:50 +0000 | [diff] [blame] | 1129 | int |
| 1130 | GDBRemoteCommunicationClient::SendLaunchArchPacket (char const *arch) |
| 1131 | { |
| 1132 | if (arch && arch[0]) |
| 1133 | { |
| 1134 | StreamString packet; |
| 1135 | packet.Printf("QLaunchArch:%s", arch); |
| 1136 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1137 | if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success) |
Greg Clayton | c4103b3 | 2011-05-08 04:53:50 +0000 | [diff] [blame] | 1138 | { |
| 1139 | if (response.IsOKResponse()) |
| 1140 | return 0; |
| 1141 | uint8_t error = response.GetError(); |
| 1142 | if (error) |
| 1143 | return error; |
| 1144 | } |
| 1145 | } |
| 1146 | return -1; |
| 1147 | } |
| 1148 | |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1149 | bool |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1150 | GDBRemoteCommunicationClient::GetOSVersion (uint32_t &major, |
| 1151 | uint32_t &minor, |
| 1152 | uint32_t &update) |
| 1153 | { |
| 1154 | if (GetHostInfo ()) |
| 1155 | { |
| 1156 | if (m_os_version_major != UINT32_MAX) |
| 1157 | { |
| 1158 | major = m_os_version_major; |
| 1159 | minor = m_os_version_minor; |
| 1160 | update = m_os_version_update; |
| 1161 | return true; |
| 1162 | } |
| 1163 | } |
| 1164 | return false; |
| 1165 | } |
| 1166 | |
| 1167 | bool |
| 1168 | GDBRemoteCommunicationClient::GetOSBuildString (std::string &s) |
| 1169 | { |
| 1170 | if (GetHostInfo ()) |
| 1171 | { |
| 1172 | if (!m_os_build.empty()) |
| 1173 | { |
| 1174 | s = m_os_build; |
| 1175 | return true; |
| 1176 | } |
| 1177 | } |
| 1178 | s.clear(); |
| 1179 | return false; |
| 1180 | } |
| 1181 | |
| 1182 | |
| 1183 | bool |
| 1184 | GDBRemoteCommunicationClient::GetOSKernelDescription (std::string &s) |
| 1185 | { |
| 1186 | if (GetHostInfo ()) |
| 1187 | { |
| 1188 | if (!m_os_kernel.empty()) |
| 1189 | { |
| 1190 | s = m_os_kernel; |
| 1191 | return true; |
| 1192 | } |
| 1193 | } |
| 1194 | s.clear(); |
| 1195 | return false; |
| 1196 | } |
| 1197 | |
| 1198 | bool |
| 1199 | GDBRemoteCommunicationClient::GetHostname (std::string &s) |
| 1200 | { |
| 1201 | if (GetHostInfo ()) |
| 1202 | { |
| 1203 | if (!m_hostname.empty()) |
| 1204 | { |
| 1205 | s = m_hostname; |
| 1206 | return true; |
| 1207 | } |
| 1208 | } |
| 1209 | s.clear(); |
| 1210 | return false; |
| 1211 | } |
| 1212 | |
| 1213 | ArchSpec |
| 1214 | GDBRemoteCommunicationClient::GetSystemArchitecture () |
| 1215 | { |
| 1216 | if (GetHostInfo ()) |
| 1217 | return m_host_arch; |
| 1218 | return ArchSpec(); |
| 1219 | } |
| 1220 | |
Jason Molenda | f17b5ac | 2012-12-19 02:54:03 +0000 | [diff] [blame] | 1221 | const lldb_private::ArchSpec & |
| 1222 | GDBRemoteCommunicationClient::GetProcessArchitecture () |
| 1223 | { |
| 1224 | if (m_qProcessInfo_is_valid == eLazyBoolCalculate) |
| 1225 | GetCurrentProcessInfo (); |
| 1226 | return m_process_arch; |
| 1227 | } |
| 1228 | |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1229 | |
| 1230 | bool |
Greg Clayton | 9b1e1cd | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 1231 | GDBRemoteCommunicationClient::GetHostInfo (bool force) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1232 | { |
Greg Clayton | 9b1e1cd | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 1233 | if (force || m_qHostInfo_is_valid == eLazyBoolCalculate) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1234 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1235 | m_qHostInfo_is_valid = eLazyBoolNo; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1236 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1237 | if (SendPacketAndWaitForResponse ("qHostInfo", response, false) == PacketResult::Success) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1238 | { |
Greg Clayton | 17a0cb6 | 2011-05-15 23:46:54 +0000 | [diff] [blame] | 1239 | if (response.IsNormalResponse()) |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1240 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1241 | std::string name; |
| 1242 | std::string value; |
| 1243 | uint32_t cpu = LLDB_INVALID_CPUTYPE; |
| 1244 | uint32_t sub = 0; |
| 1245 | std::string arch_name; |
| 1246 | std::string os_name; |
| 1247 | std::string vendor_name; |
| 1248 | std::string triple; |
| 1249 | uint32_t pointer_byte_size = 0; |
| 1250 | StringExtractor extractor; |
| 1251 | ByteOrder byte_order = eByteOrderInvalid; |
| 1252 | uint32_t num_keys_decoded = 0; |
| 1253 | while (response.GetNameColonValue(name, value)) |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1254 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1255 | if (name.compare("cputype") == 0) |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1256 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1257 | // exception type in big endian hex |
| 1258 | cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 0); |
| 1259 | if (cpu != LLDB_INVALID_CPUTYPE) |
| 1260 | ++num_keys_decoded; |
| 1261 | } |
| 1262 | else if (name.compare("cpusubtype") == 0) |
| 1263 | { |
| 1264 | // exception count in big endian hex |
| 1265 | sub = Args::StringToUInt32 (value.c_str(), 0, 0); |
| 1266 | if (sub != 0) |
| 1267 | ++num_keys_decoded; |
| 1268 | } |
| 1269 | else if (name.compare("arch") == 0) |
| 1270 | { |
| 1271 | arch_name.swap (value); |
| 1272 | ++num_keys_decoded; |
| 1273 | } |
| 1274 | else if (name.compare("triple") == 0) |
| 1275 | { |
| 1276 | // The triple comes as ASCII hex bytes since it contains '-' chars |
| 1277 | extractor.GetStringRef().swap(value); |
| 1278 | extractor.SetFilePos(0); |
| 1279 | extractor.GetHexByteString (triple); |
| 1280 | ++num_keys_decoded; |
| 1281 | } |
| 1282 | else if (name.compare("os_build") == 0) |
| 1283 | { |
| 1284 | extractor.GetStringRef().swap(value); |
| 1285 | extractor.SetFilePos(0); |
| 1286 | extractor.GetHexByteString (m_os_build); |
| 1287 | ++num_keys_decoded; |
| 1288 | } |
| 1289 | else if (name.compare("hostname") == 0) |
| 1290 | { |
| 1291 | extractor.GetStringRef().swap(value); |
| 1292 | extractor.SetFilePos(0); |
| 1293 | extractor.GetHexByteString (m_hostname); |
| 1294 | ++num_keys_decoded; |
| 1295 | } |
| 1296 | else if (name.compare("os_kernel") == 0) |
| 1297 | { |
| 1298 | extractor.GetStringRef().swap(value); |
| 1299 | extractor.SetFilePos(0); |
| 1300 | extractor.GetHexByteString (m_os_kernel); |
| 1301 | ++num_keys_decoded; |
| 1302 | } |
| 1303 | else if (name.compare("ostype") == 0) |
| 1304 | { |
| 1305 | os_name.swap (value); |
| 1306 | ++num_keys_decoded; |
| 1307 | } |
| 1308 | else if (name.compare("vendor") == 0) |
| 1309 | { |
| 1310 | vendor_name.swap(value); |
| 1311 | ++num_keys_decoded; |
| 1312 | } |
| 1313 | else if (name.compare("endian") == 0) |
| 1314 | { |
| 1315 | ++num_keys_decoded; |
| 1316 | if (value.compare("little") == 0) |
| 1317 | byte_order = eByteOrderLittle; |
| 1318 | else if (value.compare("big") == 0) |
| 1319 | byte_order = eByteOrderBig; |
| 1320 | else if (value.compare("pdp") == 0) |
| 1321 | byte_order = eByteOrderPDP; |
| 1322 | else |
| 1323 | --num_keys_decoded; |
| 1324 | } |
| 1325 | else if (name.compare("ptrsize") == 0) |
| 1326 | { |
| 1327 | pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 0); |
| 1328 | if (pointer_byte_size != 0) |
| 1329 | ++num_keys_decoded; |
| 1330 | } |
| 1331 | else if (name.compare("os_version") == 0) |
| 1332 | { |
| 1333 | Args::StringToVersion (value.c_str(), |
| 1334 | m_os_version_major, |
| 1335 | m_os_version_minor, |
| 1336 | m_os_version_update); |
| 1337 | if (m_os_version_major != UINT32_MAX) |
| 1338 | ++num_keys_decoded; |
| 1339 | } |
Enrico Granata | f04a219 | 2012-07-13 23:18:48 +0000 | [diff] [blame] | 1340 | else if (name.compare("watchpoint_exceptions_received") == 0) |
| 1341 | { |
| 1342 | ++num_keys_decoded; |
| 1343 | if (strcmp(value.c_str(),"before") == 0) |
| 1344 | m_watchpoints_trigger_after_instruction = eLazyBoolNo; |
| 1345 | else if (strcmp(value.c_str(),"after") == 0) |
| 1346 | m_watchpoints_trigger_after_instruction = eLazyBoolYes; |
| 1347 | else |
| 1348 | --num_keys_decoded; |
| 1349 | } |
Greg Clayton | 9ac6d2d | 2013-10-25 18:13:17 +0000 | [diff] [blame] | 1350 | else if (name.compare("default_packet_timeout") == 0) |
| 1351 | { |
| 1352 | m_default_packet_timeout = Args::StringToUInt32(value.c_str(), 0); |
| 1353 | if (m_default_packet_timeout > 0) |
| 1354 | { |
| 1355 | SetPacketTimeout(m_default_packet_timeout); |
| 1356 | ++num_keys_decoded; |
| 1357 | } |
| 1358 | } |
Enrico Granata | f04a219 | 2012-07-13 23:18:48 +0000 | [diff] [blame] | 1359 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | if (num_keys_decoded > 0) |
| 1363 | m_qHostInfo_is_valid = eLazyBoolYes; |
| 1364 | |
| 1365 | if (triple.empty()) |
| 1366 | { |
| 1367 | if (arch_name.empty()) |
| 1368 | { |
| 1369 | if (cpu != LLDB_INVALID_CPUTYPE) |
| 1370 | { |
| 1371 | m_host_arch.SetArchitecture (eArchTypeMachO, cpu, sub); |
| 1372 | if (pointer_byte_size) |
| 1373 | { |
| 1374 | assert (pointer_byte_size == m_host_arch.GetAddressByteSize()); |
| 1375 | } |
| 1376 | if (byte_order != eByteOrderInvalid) |
| 1377 | { |
| 1378 | assert (byte_order == m_host_arch.GetByteOrder()); |
| 1379 | } |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 1380 | |
| 1381 | if (!os_name.empty() && vendor_name.compare("apple") == 0 && os_name.find("darwin") == 0) |
| 1382 | { |
| 1383 | switch (m_host_arch.GetMachine()) |
| 1384 | { |
| 1385 | case llvm::Triple::arm: |
| 1386 | case llvm::Triple::thumb: |
| 1387 | os_name = "ios"; |
| 1388 | break; |
| 1389 | default: |
| 1390 | os_name = "macosx"; |
| 1391 | break; |
| 1392 | } |
| 1393 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1394 | if (!vendor_name.empty()) |
| 1395 | m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name)); |
| 1396 | if (!os_name.empty()) |
Greg Clayton | e1dadb8 | 2011-09-15 00:21:03 +0000 | [diff] [blame] | 1397 | m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name)); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1398 | |
| 1399 | } |
| 1400 | } |
| 1401 | else |
| 1402 | { |
| 1403 | std::string triple; |
| 1404 | triple += arch_name; |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 1405 | if (!vendor_name.empty() || !os_name.empty()) |
| 1406 | { |
| 1407 | triple += '-'; |
| 1408 | if (vendor_name.empty()) |
| 1409 | triple += "unknown"; |
| 1410 | else |
| 1411 | triple += vendor_name; |
| 1412 | triple += '-'; |
| 1413 | if (os_name.empty()) |
| 1414 | triple += "unknown"; |
| 1415 | else |
| 1416 | triple += os_name; |
| 1417 | } |
| 1418 | m_host_arch.SetTriple (triple.c_str()); |
| 1419 | |
| 1420 | llvm::Triple &host_triple = m_host_arch.GetTriple(); |
| 1421 | if (host_triple.getVendor() == llvm::Triple::Apple && host_triple.getOS() == llvm::Triple::Darwin) |
| 1422 | { |
| 1423 | switch (m_host_arch.GetMachine()) |
| 1424 | { |
| 1425 | case llvm::Triple::arm: |
| 1426 | case llvm::Triple::thumb: |
| 1427 | host_triple.setOS(llvm::Triple::IOS); |
| 1428 | break; |
| 1429 | default: |
| 1430 | host_triple.setOS(llvm::Triple::MacOSX); |
| 1431 | break; |
| 1432 | } |
| 1433 | } |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1434 | if (pointer_byte_size) |
| 1435 | { |
| 1436 | assert (pointer_byte_size == m_host_arch.GetAddressByteSize()); |
| 1437 | } |
| 1438 | if (byte_order != eByteOrderInvalid) |
| 1439 | { |
| 1440 | assert (byte_order == m_host_arch.GetByteOrder()); |
| 1441 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1442 | |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1443 | } |
| 1444 | } |
| 1445 | else |
| 1446 | { |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 1447 | m_host_arch.SetTriple (triple.c_str()); |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1448 | if (pointer_byte_size) |
| 1449 | { |
| 1450 | assert (pointer_byte_size == m_host_arch.GetAddressByteSize()); |
| 1451 | } |
| 1452 | if (byte_order != eByteOrderInvalid) |
| 1453 | { |
| 1454 | assert (byte_order == m_host_arch.GetByteOrder()); |
| 1455 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1456 | } |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1457 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1458 | } |
| 1459 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1460 | return m_qHostInfo_is_valid == eLazyBoolYes; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | int |
| 1464 | GDBRemoteCommunicationClient::SendAttach |
| 1465 | ( |
| 1466 | lldb::pid_t pid, |
| 1467 | StringExtractorGDBRemote& response |
| 1468 | ) |
| 1469 | { |
| 1470 | if (pid != LLDB_INVALID_PROCESS_ID) |
| 1471 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1472 | char packet[64]; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1473 | const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%" PRIx64, pid); |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 1474 | assert (packet_len < (int)sizeof(packet)); |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1475 | if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1476 | { |
| 1477 | if (response.IsErrorResponse()) |
| 1478 | return response.GetError(); |
| 1479 | return 0; |
| 1480 | } |
| 1481 | } |
| 1482 | return -1; |
| 1483 | } |
| 1484 | |
| 1485 | const lldb_private::ArchSpec & |
| 1486 | GDBRemoteCommunicationClient::GetHostArchitecture () |
| 1487 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1488 | if (m_qHostInfo_is_valid == eLazyBoolCalculate) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1489 | GetHostInfo (); |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1490 | return m_host_arch; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
Greg Clayton | 9ac6d2d | 2013-10-25 18:13:17 +0000 | [diff] [blame] | 1493 | uint32_t |
| 1494 | GDBRemoteCommunicationClient::GetHostDefaultPacketTimeout () |
| 1495 | { |
| 1496 | if (m_qHostInfo_is_valid == eLazyBoolCalculate) |
| 1497 | GetHostInfo (); |
| 1498 | return m_default_packet_timeout; |
| 1499 | } |
| 1500 | |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1501 | addr_t |
| 1502 | GDBRemoteCommunicationClient::AllocateMemory (size_t size, uint32_t permissions) |
| 1503 | { |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 1504 | if (m_supports_alloc_dealloc_memory != eLazyBoolNo) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1505 | { |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 1506 | m_supports_alloc_dealloc_memory = eLazyBoolYes; |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 1507 | char packet[64]; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1508 | const int packet_len = ::snprintf (packet, sizeof(packet), "_M%" PRIx64 ",%s%s%s", |
Greg Clayton | 43e0af0 | 2012-09-18 18:04:04 +0000 | [diff] [blame] | 1509 | (uint64_t)size, |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 1510 | permissions & lldb::ePermissionsReadable ? "r" : "", |
| 1511 | permissions & lldb::ePermissionsWritable ? "w" : "", |
| 1512 | permissions & lldb::ePermissionsExecutable ? "x" : ""); |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 1513 | assert (packet_len < (int)sizeof(packet)); |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 1514 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1515 | if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success) |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 1516 | { |
Greg Clayton | 17a0cb6 | 2011-05-15 23:46:54 +0000 | [diff] [blame] | 1517 | if (!response.IsErrorResponse()) |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 1518 | return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS); |
| 1519 | } |
Greg Clayton | 17a0cb6 | 2011-05-15 23:46:54 +0000 | [diff] [blame] | 1520 | else |
| 1521 | { |
| 1522 | m_supports_alloc_dealloc_memory = eLazyBoolNo; |
| 1523 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1524 | } |
| 1525 | return LLDB_INVALID_ADDRESS; |
| 1526 | } |
| 1527 | |
| 1528 | bool |
| 1529 | GDBRemoteCommunicationClient::DeallocateMemory (addr_t addr) |
| 1530 | { |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 1531 | if (m_supports_alloc_dealloc_memory != eLazyBoolNo) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1532 | { |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 1533 | m_supports_alloc_dealloc_memory = eLazyBoolYes; |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 1534 | char packet[64]; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1535 | const int packet_len = ::snprintf(packet, sizeof(packet), "_m%" PRIx64, (uint64_t)addr); |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 1536 | assert (packet_len < (int)sizeof(packet)); |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 1537 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1538 | if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success) |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 1539 | { |
| 1540 | if (response.IsOKResponse()) |
| 1541 | return true; |
Greg Clayton | 17a0cb6 | 2011-05-15 23:46:54 +0000 | [diff] [blame] | 1542 | } |
| 1543 | else |
| 1544 | { |
| 1545 | m_supports_alloc_dealloc_memory = eLazyBoolNo; |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 1546 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1547 | } |
| 1548 | return false; |
| 1549 | } |
| 1550 | |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 1551 | Error |
| 1552 | GDBRemoteCommunicationClient::Detach (bool keep_stopped) |
Greg Clayton | 37a0a24 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 1553 | { |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 1554 | Error error; |
| 1555 | |
| 1556 | if (keep_stopped) |
| 1557 | { |
| 1558 | if (m_supports_detach_stay_stopped == eLazyBoolCalculate) |
| 1559 | { |
| 1560 | char packet[64]; |
| 1561 | const int packet_len = ::snprintf(packet, sizeof(packet), "qSupportsDetachAndStayStopped:"); |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 1562 | assert (packet_len < (int)sizeof(packet)); |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 1563 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1564 | if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success) |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 1565 | { |
| 1566 | m_supports_detach_stay_stopped = eLazyBoolYes; |
| 1567 | } |
| 1568 | else |
| 1569 | { |
| 1570 | m_supports_detach_stay_stopped = eLazyBoolNo; |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | if (m_supports_detach_stay_stopped == eLazyBoolNo) |
| 1575 | { |
| 1576 | error.SetErrorString("Stays stopped not supported by this target."); |
| 1577 | return error; |
| 1578 | } |
| 1579 | else |
| 1580 | { |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1581 | PacketResult packet_result = SendPacket ("D1", 2); |
| 1582 | if (packet_result != PacketResult::Success) |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 1583 | error.SetErrorString ("Sending extended disconnect packet failed."); |
| 1584 | } |
| 1585 | } |
| 1586 | else |
| 1587 | { |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1588 | PacketResult packet_result = SendPacket ("D", 1); |
| 1589 | if (packet_result != PacketResult::Success) |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 1590 | error.SetErrorString ("Sending disconnect packet failed."); |
| 1591 | } |
| 1592 | return error; |
Greg Clayton | 37a0a24 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
Greg Clayton | 46fb558 | 2011-11-18 07:03:08 +0000 | [diff] [blame] | 1595 | Error |
| 1596 | GDBRemoteCommunicationClient::GetMemoryRegionInfo (lldb::addr_t addr, |
| 1597 | lldb_private::MemoryRegionInfo ®ion_info) |
| 1598 | { |
| 1599 | Error error; |
| 1600 | region_info.Clear(); |
| 1601 | |
| 1602 | if (m_supports_memory_region_info != eLazyBoolNo) |
| 1603 | { |
| 1604 | m_supports_memory_region_info = eLazyBoolYes; |
| 1605 | char packet[64]; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1606 | const int packet_len = ::snprintf(packet, sizeof(packet), "qMemoryRegionInfo:%" PRIx64, (uint64_t)addr); |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 1607 | assert (packet_len < (int)sizeof(packet)); |
Greg Clayton | 46fb558 | 2011-11-18 07:03:08 +0000 | [diff] [blame] | 1608 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1609 | if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success) |
Greg Clayton | 46fb558 | 2011-11-18 07:03:08 +0000 | [diff] [blame] | 1610 | { |
| 1611 | std::string name; |
| 1612 | std::string value; |
| 1613 | addr_t addr_value; |
| 1614 | bool success = true; |
Jason Molenda | cb349ee | 2011-12-13 05:39:38 +0000 | [diff] [blame] | 1615 | bool saw_permissions = false; |
Greg Clayton | 46fb558 | 2011-11-18 07:03:08 +0000 | [diff] [blame] | 1616 | while (success && response.GetNameColonValue(name, value)) |
| 1617 | { |
| 1618 | if (name.compare ("start") == 0) |
| 1619 | { |
| 1620 | addr_value = Args::StringToUInt64(value.c_str(), LLDB_INVALID_ADDRESS, 16, &success); |
| 1621 | if (success) |
| 1622 | region_info.GetRange().SetRangeBase(addr_value); |
| 1623 | } |
| 1624 | else if (name.compare ("size") == 0) |
| 1625 | { |
| 1626 | addr_value = Args::StringToUInt64(value.c_str(), 0, 16, &success); |
| 1627 | if (success) |
| 1628 | region_info.GetRange().SetByteSize (addr_value); |
| 1629 | } |
Jason Molenda | cb349ee | 2011-12-13 05:39:38 +0000 | [diff] [blame] | 1630 | else if (name.compare ("permissions") == 0 && region_info.GetRange().IsValid()) |
Greg Clayton | 46fb558 | 2011-11-18 07:03:08 +0000 | [diff] [blame] | 1631 | { |
Jason Molenda | cb349ee | 2011-12-13 05:39:38 +0000 | [diff] [blame] | 1632 | saw_permissions = true; |
| 1633 | if (region_info.GetRange().Contains (addr)) |
| 1634 | { |
| 1635 | if (value.find('r') != std::string::npos) |
| 1636 | region_info.SetReadable (MemoryRegionInfo::eYes); |
| 1637 | else |
| 1638 | region_info.SetReadable (MemoryRegionInfo::eNo); |
| 1639 | |
| 1640 | if (value.find('w') != std::string::npos) |
| 1641 | region_info.SetWritable (MemoryRegionInfo::eYes); |
| 1642 | else |
| 1643 | region_info.SetWritable (MemoryRegionInfo::eNo); |
| 1644 | |
| 1645 | if (value.find('x') != std::string::npos) |
| 1646 | region_info.SetExecutable (MemoryRegionInfo::eYes); |
| 1647 | else |
| 1648 | region_info.SetExecutable (MemoryRegionInfo::eNo); |
| 1649 | } |
| 1650 | else |
| 1651 | { |
| 1652 | // The reported region does not contain this address -- we're looking at an unmapped page |
| 1653 | region_info.SetReadable (MemoryRegionInfo::eNo); |
| 1654 | region_info.SetWritable (MemoryRegionInfo::eNo); |
| 1655 | region_info.SetExecutable (MemoryRegionInfo::eNo); |
| 1656 | } |
Greg Clayton | 46fb558 | 2011-11-18 07:03:08 +0000 | [diff] [blame] | 1657 | } |
| 1658 | else if (name.compare ("error") == 0) |
| 1659 | { |
| 1660 | StringExtractorGDBRemote name_extractor; |
| 1661 | // Swap "value" over into "name_extractor" |
| 1662 | name_extractor.GetStringRef().swap(value); |
| 1663 | // Now convert the HEX bytes into a string value |
| 1664 | name_extractor.GetHexByteString (value); |
| 1665 | error.SetErrorString(value.c_str()); |
| 1666 | } |
| 1667 | } |
Jason Molenda | cb349ee | 2011-12-13 05:39:38 +0000 | [diff] [blame] | 1668 | |
| 1669 | // We got a valid address range back but no permissions -- which means this is an unmapped page |
| 1670 | if (region_info.GetRange().IsValid() && saw_permissions == false) |
| 1671 | { |
| 1672 | region_info.SetReadable (MemoryRegionInfo::eNo); |
| 1673 | region_info.SetWritable (MemoryRegionInfo::eNo); |
| 1674 | region_info.SetExecutable (MemoryRegionInfo::eNo); |
| 1675 | } |
Greg Clayton | 46fb558 | 2011-11-18 07:03:08 +0000 | [diff] [blame] | 1676 | } |
| 1677 | else |
| 1678 | { |
| 1679 | m_supports_memory_region_info = eLazyBoolNo; |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | if (m_supports_memory_region_info == eLazyBoolNo) |
| 1684 | { |
| 1685 | error.SetErrorString("qMemoryRegionInfo is not supported"); |
| 1686 | } |
| 1687 | if (error.Fail()) |
| 1688 | region_info.Clear(); |
| 1689 | return error; |
| 1690 | |
| 1691 | } |
| 1692 | |
Johnny Chen | 6463720 | 2012-05-23 21:09:52 +0000 | [diff] [blame] | 1693 | Error |
| 1694 | GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num) |
| 1695 | { |
| 1696 | Error error; |
| 1697 | |
| 1698 | if (m_supports_watchpoint_support_info == eLazyBoolYes) |
| 1699 | { |
| 1700 | num = m_num_supported_hardware_watchpoints; |
| 1701 | return error; |
| 1702 | } |
| 1703 | |
| 1704 | // Set num to 0 first. |
| 1705 | num = 0; |
| 1706 | if (m_supports_watchpoint_support_info != eLazyBoolNo) |
| 1707 | { |
| 1708 | char packet[64]; |
| 1709 | const int packet_len = ::snprintf(packet, sizeof(packet), "qWatchpointSupportInfo:"); |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 1710 | assert (packet_len < (int)sizeof(packet)); |
Johnny Chen | 6463720 | 2012-05-23 21:09:52 +0000 | [diff] [blame] | 1711 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1712 | if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success) |
Johnny Chen | 6463720 | 2012-05-23 21:09:52 +0000 | [diff] [blame] | 1713 | { |
| 1714 | m_supports_watchpoint_support_info = eLazyBoolYes; |
| 1715 | std::string name; |
| 1716 | std::string value; |
| 1717 | while (response.GetNameColonValue(name, value)) |
| 1718 | { |
| 1719 | if (name.compare ("num") == 0) |
| 1720 | { |
| 1721 | num = Args::StringToUInt32(value.c_str(), 0, 0); |
| 1722 | m_num_supported_hardware_watchpoints = num; |
| 1723 | } |
| 1724 | } |
| 1725 | } |
| 1726 | else |
| 1727 | { |
| 1728 | m_supports_watchpoint_support_info = eLazyBoolNo; |
| 1729 | } |
| 1730 | } |
| 1731 | |
| 1732 | if (m_supports_watchpoint_support_info == eLazyBoolNo) |
| 1733 | { |
| 1734 | error.SetErrorString("qWatchpointSupportInfo is not supported"); |
| 1735 | } |
| 1736 | return error; |
| 1737 | |
| 1738 | } |
Greg Clayton | 46fb558 | 2011-11-18 07:03:08 +0000 | [diff] [blame] | 1739 | |
Enrico Granata | f04a219 | 2012-07-13 23:18:48 +0000 | [diff] [blame] | 1740 | lldb_private::Error |
| 1741 | GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num, bool& after) |
| 1742 | { |
| 1743 | Error error(GetWatchpointSupportInfo(num)); |
| 1744 | if (error.Success()) |
| 1745 | error = GetWatchpointsTriggerAfterInstruction(after); |
| 1746 | return error; |
| 1747 | } |
| 1748 | |
| 1749 | lldb_private::Error |
| 1750 | GDBRemoteCommunicationClient::GetWatchpointsTriggerAfterInstruction (bool &after) |
| 1751 | { |
| 1752 | Error error; |
| 1753 | |
| 1754 | // we assume watchpoints will happen after running the relevant opcode |
| 1755 | // and we only want to override this behavior if we have explicitly |
| 1756 | // received a qHostInfo telling us otherwise |
| 1757 | if (m_qHostInfo_is_valid != eLazyBoolYes) |
| 1758 | after = true; |
| 1759 | else |
| 1760 | after = (m_watchpoints_trigger_after_instruction != eLazyBoolNo); |
| 1761 | return error; |
| 1762 | } |
| 1763 | |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1764 | int |
| 1765 | GDBRemoteCommunicationClient::SetSTDIN (char const *path) |
| 1766 | { |
| 1767 | if (path && path[0]) |
| 1768 | { |
| 1769 | StreamString packet; |
| 1770 | packet.PutCString("QSetSTDIN:"); |
| 1771 | packet.PutBytesAsRawHex8(path, strlen(path)); |
| 1772 | |
| 1773 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1774 | if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1775 | { |
| 1776 | if (response.IsOKResponse()) |
| 1777 | return 0; |
| 1778 | uint8_t error = response.GetError(); |
| 1779 | if (error) |
| 1780 | return error; |
| 1781 | } |
| 1782 | } |
| 1783 | return -1; |
| 1784 | } |
| 1785 | |
| 1786 | int |
| 1787 | GDBRemoteCommunicationClient::SetSTDOUT (char const *path) |
| 1788 | { |
| 1789 | if (path && path[0]) |
| 1790 | { |
| 1791 | StreamString packet; |
| 1792 | packet.PutCString("QSetSTDOUT:"); |
| 1793 | packet.PutBytesAsRawHex8(path, strlen(path)); |
| 1794 | |
| 1795 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1796 | if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1797 | { |
| 1798 | if (response.IsOKResponse()) |
| 1799 | return 0; |
| 1800 | uint8_t error = response.GetError(); |
| 1801 | if (error) |
| 1802 | return error; |
| 1803 | } |
| 1804 | } |
| 1805 | return -1; |
| 1806 | } |
| 1807 | |
| 1808 | int |
| 1809 | GDBRemoteCommunicationClient::SetSTDERR (char const *path) |
| 1810 | { |
| 1811 | if (path && path[0]) |
| 1812 | { |
| 1813 | StreamString packet; |
| 1814 | packet.PutCString("QSetSTDERR:"); |
| 1815 | packet.PutBytesAsRawHex8(path, strlen(path)); |
| 1816 | |
| 1817 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1818 | if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1819 | { |
| 1820 | if (response.IsOKResponse()) |
| 1821 | return 0; |
| 1822 | uint8_t error = response.GetError(); |
| 1823 | if (error) |
| 1824 | return error; |
| 1825 | } |
| 1826 | } |
| 1827 | return -1; |
| 1828 | } |
| 1829 | |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1830 | bool |
| 1831 | GDBRemoteCommunicationClient::GetWorkingDir (std::string &cwd) |
| 1832 | { |
| 1833 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1834 | if (SendPacketAndWaitForResponse ("qGetWorkingDir", response, false) == PacketResult::Success) |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1835 | { |
| 1836 | if (response.IsUnsupportedResponse()) |
| 1837 | return false; |
| 1838 | if (response.IsErrorResponse()) |
| 1839 | return false; |
| 1840 | response.GetHexByteString (cwd); |
| 1841 | return !cwd.empty(); |
| 1842 | } |
| 1843 | return false; |
| 1844 | } |
| 1845 | |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1846 | int |
| 1847 | GDBRemoteCommunicationClient::SetWorkingDir (char const *path) |
| 1848 | { |
| 1849 | if (path && path[0]) |
| 1850 | { |
| 1851 | StreamString packet; |
| 1852 | packet.PutCString("QSetWorkingDir:"); |
| 1853 | packet.PutBytesAsRawHex8(path, strlen(path)); |
| 1854 | |
| 1855 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1856 | if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1857 | { |
| 1858 | if (response.IsOKResponse()) |
| 1859 | return 0; |
| 1860 | uint8_t error = response.GetError(); |
| 1861 | if (error) |
| 1862 | return error; |
| 1863 | } |
| 1864 | } |
| 1865 | return -1; |
| 1866 | } |
| 1867 | |
| 1868 | int |
| 1869 | GDBRemoteCommunicationClient::SetDisableASLR (bool enable) |
| 1870 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1871 | char packet[32]; |
| 1872 | const int packet_len = ::snprintf (packet, sizeof (packet), "QSetDisableASLR:%i", enable ? 1 : 0); |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 1873 | assert (packet_len < (int)sizeof(packet)); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1874 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1875 | if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1876 | { |
| 1877 | if (response.IsOKResponse()) |
| 1878 | return 0; |
| 1879 | uint8_t error = response.GetError(); |
| 1880 | if (error) |
| 1881 | return error; |
| 1882 | } |
| 1883 | return -1; |
| 1884 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1885 | |
| 1886 | bool |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1887 | GDBRemoteCommunicationClient::DecodeProcessInfoResponse (StringExtractorGDBRemote &response, ProcessInstanceInfo &process_info) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1888 | { |
| 1889 | if (response.IsNormalResponse()) |
| 1890 | { |
| 1891 | std::string name; |
| 1892 | std::string value; |
| 1893 | StringExtractor extractor; |
| 1894 | |
| 1895 | while (response.GetNameColonValue(name, value)) |
| 1896 | { |
| 1897 | if (name.compare("pid") == 0) |
| 1898 | { |
| 1899 | process_info.SetProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0)); |
| 1900 | } |
| 1901 | else if (name.compare("ppid") == 0) |
| 1902 | { |
| 1903 | process_info.SetParentProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0)); |
| 1904 | } |
| 1905 | else if (name.compare("uid") == 0) |
| 1906 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1907 | process_info.SetUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0)); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1908 | } |
| 1909 | else if (name.compare("euid") == 0) |
| 1910 | { |
| 1911 | process_info.SetEffectiveUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0)); |
| 1912 | } |
| 1913 | else if (name.compare("gid") == 0) |
| 1914 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1915 | process_info.SetGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0)); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1916 | } |
| 1917 | else if (name.compare("egid") == 0) |
| 1918 | { |
| 1919 | process_info.SetEffectiveGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0)); |
| 1920 | } |
| 1921 | else if (name.compare("triple") == 0) |
| 1922 | { |
| 1923 | // The triple comes as ASCII hex bytes since it contains '-' chars |
| 1924 | extractor.GetStringRef().swap(value); |
| 1925 | extractor.SetFilePos(0); |
| 1926 | extractor.GetHexByteString (value); |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 1927 | process_info.GetArchitecture ().SetTriple (value.c_str()); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1928 | } |
| 1929 | else if (name.compare("name") == 0) |
| 1930 | { |
| 1931 | StringExtractor extractor; |
Filipe Cabecinhas | f86cf78 | 2012-05-07 09:30:51 +0000 | [diff] [blame] | 1932 | // The process name from ASCII hex bytes since we can't |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1933 | // control the characters in a process name |
| 1934 | extractor.GetStringRef().swap(value); |
| 1935 | extractor.SetFilePos(0); |
| 1936 | extractor.GetHexByteString (value); |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 1937 | process_info.GetExecutableFile().SetFile (value.c_str(), false); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1938 | } |
| 1939 | } |
| 1940 | |
| 1941 | if (process_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) |
| 1942 | return true; |
| 1943 | } |
| 1944 | return false; |
| 1945 | } |
| 1946 | |
| 1947 | bool |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1948 | GDBRemoteCommunicationClient::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1949 | { |
| 1950 | process_info.Clear(); |
| 1951 | |
| 1952 | if (m_supports_qProcessInfoPID) |
| 1953 | { |
| 1954 | char packet[32]; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1955 | const int packet_len = ::snprintf (packet, sizeof (packet), "qProcessInfoPID:%" PRIu64, pid); |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 1956 | assert (packet_len < (int)sizeof(packet)); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1957 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1958 | if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1959 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1960 | return DecodeProcessInfoResponse (response, process_info); |
| 1961 | } |
Greg Clayton | 17a0cb6 | 2011-05-15 23:46:54 +0000 | [diff] [blame] | 1962 | else |
| 1963 | { |
| 1964 | m_supports_qProcessInfoPID = false; |
| 1965 | return false; |
| 1966 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1967 | } |
| 1968 | return false; |
| 1969 | } |
| 1970 | |
Jason Molenda | f17b5ac | 2012-12-19 02:54:03 +0000 | [diff] [blame] | 1971 | bool |
| 1972 | GDBRemoteCommunicationClient::GetCurrentProcessInfo () |
| 1973 | { |
| 1974 | if (m_qProcessInfo_is_valid == eLazyBoolYes) |
| 1975 | return true; |
| 1976 | if (m_qProcessInfo_is_valid == eLazyBoolNo) |
| 1977 | return false; |
| 1978 | |
| 1979 | GetHostInfo (); |
| 1980 | |
| 1981 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1982 | if (SendPacketAndWaitForResponse ("qProcessInfo", response, false) == PacketResult::Success) |
Jason Molenda | f17b5ac | 2012-12-19 02:54:03 +0000 | [diff] [blame] | 1983 | { |
| 1984 | if (response.IsNormalResponse()) |
| 1985 | { |
| 1986 | std::string name; |
| 1987 | std::string value; |
| 1988 | uint32_t cpu = LLDB_INVALID_CPUTYPE; |
| 1989 | uint32_t sub = 0; |
| 1990 | std::string arch_name; |
| 1991 | std::string os_name; |
| 1992 | std::string vendor_name; |
| 1993 | std::string triple; |
| 1994 | uint32_t pointer_byte_size = 0; |
| 1995 | StringExtractor extractor; |
| 1996 | ByteOrder byte_order = eByteOrderInvalid; |
| 1997 | uint32_t num_keys_decoded = 0; |
| 1998 | while (response.GetNameColonValue(name, value)) |
| 1999 | { |
| 2000 | if (name.compare("cputype") == 0) |
| 2001 | { |
| 2002 | cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 16); |
| 2003 | if (cpu != LLDB_INVALID_CPUTYPE) |
| 2004 | ++num_keys_decoded; |
| 2005 | } |
| 2006 | else if (name.compare("cpusubtype") == 0) |
| 2007 | { |
| 2008 | sub = Args::StringToUInt32 (value.c_str(), 0, 16); |
| 2009 | if (sub != 0) |
| 2010 | ++num_keys_decoded; |
| 2011 | } |
| 2012 | else if (name.compare("ostype") == 0) |
| 2013 | { |
| 2014 | os_name.swap (value); |
| 2015 | ++num_keys_decoded; |
| 2016 | } |
| 2017 | else if (name.compare("vendor") == 0) |
| 2018 | { |
| 2019 | vendor_name.swap(value); |
| 2020 | ++num_keys_decoded; |
| 2021 | } |
| 2022 | else if (name.compare("endian") == 0) |
| 2023 | { |
| 2024 | ++num_keys_decoded; |
| 2025 | if (value.compare("little") == 0) |
| 2026 | byte_order = eByteOrderLittle; |
| 2027 | else if (value.compare("big") == 0) |
| 2028 | byte_order = eByteOrderBig; |
| 2029 | else if (value.compare("pdp") == 0) |
| 2030 | byte_order = eByteOrderPDP; |
| 2031 | else |
| 2032 | --num_keys_decoded; |
| 2033 | } |
| 2034 | else if (name.compare("ptrsize") == 0) |
| 2035 | { |
| 2036 | pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 16); |
| 2037 | if (pointer_byte_size != 0) |
| 2038 | ++num_keys_decoded; |
| 2039 | } |
| 2040 | } |
| 2041 | if (num_keys_decoded > 0) |
| 2042 | m_qProcessInfo_is_valid = eLazyBoolYes; |
| 2043 | if (cpu != LLDB_INVALID_CPUTYPE && !os_name.empty() && !vendor_name.empty()) |
| 2044 | { |
| 2045 | m_process_arch.SetArchitecture (eArchTypeMachO, cpu, sub); |
| 2046 | if (pointer_byte_size) |
| 2047 | { |
| 2048 | assert (pointer_byte_size == m_process_arch.GetAddressByteSize()); |
| 2049 | } |
| 2050 | m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name)); |
| 2051 | m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name)); |
| 2052 | return true; |
| 2053 | } |
| 2054 | } |
| 2055 | } |
| 2056 | else |
| 2057 | { |
| 2058 | m_qProcessInfo_is_valid = eLazyBoolNo; |
| 2059 | } |
| 2060 | |
| 2061 | return false; |
| 2062 | } |
| 2063 | |
| 2064 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 2065 | uint32_t |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2066 | GDBRemoteCommunicationClient::FindProcesses (const ProcessInstanceInfoMatch &match_info, |
| 2067 | ProcessInstanceInfoList &process_infos) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 2068 | { |
| 2069 | process_infos.Clear(); |
| 2070 | |
| 2071 | if (m_supports_qfProcessInfo) |
| 2072 | { |
| 2073 | StreamString packet; |
| 2074 | packet.PutCString ("qfProcessInfo"); |
| 2075 | if (!match_info.MatchAllProcesses()) |
| 2076 | { |
| 2077 | packet.PutChar (':'); |
| 2078 | const char *name = match_info.GetProcessInfo().GetName(); |
| 2079 | bool has_name_match = false; |
| 2080 | if (name && name[0]) |
| 2081 | { |
| 2082 | has_name_match = true; |
| 2083 | NameMatchType name_match_type = match_info.GetNameMatchType(); |
| 2084 | switch (name_match_type) |
| 2085 | { |
| 2086 | case eNameMatchIgnore: |
| 2087 | has_name_match = false; |
| 2088 | break; |
| 2089 | |
| 2090 | case eNameMatchEquals: |
| 2091 | packet.PutCString ("name_match:equals;"); |
| 2092 | break; |
| 2093 | |
| 2094 | case eNameMatchContains: |
| 2095 | packet.PutCString ("name_match:contains;"); |
| 2096 | break; |
| 2097 | |
| 2098 | case eNameMatchStartsWith: |
| 2099 | packet.PutCString ("name_match:starts_with;"); |
| 2100 | break; |
| 2101 | |
| 2102 | case eNameMatchEndsWith: |
| 2103 | packet.PutCString ("name_match:ends_with;"); |
| 2104 | break; |
| 2105 | |
| 2106 | case eNameMatchRegularExpression: |
| 2107 | packet.PutCString ("name_match:regex;"); |
| 2108 | break; |
| 2109 | } |
| 2110 | if (has_name_match) |
| 2111 | { |
| 2112 | packet.PutCString ("name:"); |
| 2113 | packet.PutBytesAsRawHex8(name, ::strlen(name)); |
| 2114 | packet.PutChar (';'); |
| 2115 | } |
| 2116 | } |
| 2117 | |
| 2118 | if (match_info.GetProcessInfo().ProcessIDIsValid()) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2119 | packet.Printf("pid:%" PRIu64 ";",match_info.GetProcessInfo().GetProcessID()); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 2120 | if (match_info.GetProcessInfo().ParentProcessIDIsValid()) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2121 | packet.Printf("parent_pid:%" PRIu64 ";",match_info.GetProcessInfo().GetParentProcessID()); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2122 | if (match_info.GetProcessInfo().UserIDIsValid()) |
| 2123 | packet.Printf("uid:%u;",match_info.GetProcessInfo().GetUserID()); |
| 2124 | if (match_info.GetProcessInfo().GroupIDIsValid()) |
| 2125 | packet.Printf("gid:%u;",match_info.GetProcessInfo().GetGroupID()); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 2126 | if (match_info.GetProcessInfo().EffectiveUserIDIsValid()) |
| 2127 | packet.Printf("euid:%u;",match_info.GetProcessInfo().GetEffectiveUserID()); |
| 2128 | if (match_info.GetProcessInfo().EffectiveGroupIDIsValid()) |
| 2129 | packet.Printf("egid:%u;",match_info.GetProcessInfo().GetEffectiveGroupID()); |
| 2130 | if (match_info.GetProcessInfo().EffectiveGroupIDIsValid()) |
| 2131 | packet.Printf("all_users:%u;",match_info.GetMatchAllUsers() ? 1 : 0); |
| 2132 | if (match_info.GetProcessInfo().GetArchitecture().IsValid()) |
| 2133 | { |
| 2134 | const ArchSpec &match_arch = match_info.GetProcessInfo().GetArchitecture(); |
| 2135 | const llvm::Triple &triple = match_arch.GetTriple(); |
| 2136 | packet.PutCString("triple:"); |
| 2137 | packet.PutCStringAsRawHex8(triple.getTriple().c_str()); |
| 2138 | packet.PutChar (';'); |
| 2139 | } |
| 2140 | } |
| 2141 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2142 | if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 2143 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 2144 | do |
| 2145 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2146 | ProcessInstanceInfo process_info; |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 2147 | if (!DecodeProcessInfoResponse (response, process_info)) |
| 2148 | break; |
| 2149 | process_infos.Append(process_info); |
| 2150 | response.GetStringRef().clear(); |
| 2151 | response.SetFilePos(0); |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2152 | } while (SendPacketAndWaitForResponse ("qsProcessInfo", strlen ("qsProcessInfo"), response, false) == PacketResult::Success); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 2153 | } |
Greg Clayton | 17a0cb6 | 2011-05-15 23:46:54 +0000 | [diff] [blame] | 2154 | else |
| 2155 | { |
| 2156 | m_supports_qfProcessInfo = false; |
| 2157 | return 0; |
| 2158 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 2159 | } |
| 2160 | return process_infos.GetSize(); |
| 2161 | |
| 2162 | } |
| 2163 | |
| 2164 | bool |
| 2165 | GDBRemoteCommunicationClient::GetUserName (uint32_t uid, std::string &name) |
| 2166 | { |
| 2167 | if (m_supports_qUserName) |
| 2168 | { |
| 2169 | char packet[32]; |
| 2170 | const int packet_len = ::snprintf (packet, sizeof (packet), "qUserName:%i", uid); |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 2171 | assert (packet_len < (int)sizeof(packet)); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 2172 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2173 | if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 2174 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 2175 | if (response.IsNormalResponse()) |
| 2176 | { |
| 2177 | // Make sure we parsed the right number of characters. The response is |
| 2178 | // the hex encoded user name and should make up the entire packet. |
| 2179 | // If there are any non-hex ASCII bytes, the length won't match below.. |
| 2180 | if (response.GetHexByteString (name) * 2 == response.GetStringRef().size()) |
| 2181 | return true; |
| 2182 | } |
| 2183 | } |
Greg Clayton | 17a0cb6 | 2011-05-15 23:46:54 +0000 | [diff] [blame] | 2184 | else |
| 2185 | { |
| 2186 | m_supports_qUserName = false; |
| 2187 | return false; |
| 2188 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 2189 | } |
| 2190 | return false; |
| 2191 | |
| 2192 | } |
| 2193 | |
| 2194 | bool |
| 2195 | GDBRemoteCommunicationClient::GetGroupName (uint32_t gid, std::string &name) |
| 2196 | { |
| 2197 | if (m_supports_qGroupName) |
| 2198 | { |
| 2199 | char packet[32]; |
| 2200 | const int packet_len = ::snprintf (packet, sizeof (packet), "qGroupName:%i", gid); |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 2201 | assert (packet_len < (int)sizeof(packet)); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 2202 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2203 | if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 2204 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 2205 | if (response.IsNormalResponse()) |
| 2206 | { |
| 2207 | // Make sure we parsed the right number of characters. The response is |
| 2208 | // the hex encoded group name and should make up the entire packet. |
| 2209 | // If there are any non-hex ASCII bytes, the length won't match below.. |
| 2210 | if (response.GetHexByteString (name) * 2 == response.GetStringRef().size()) |
| 2211 | return true; |
| 2212 | } |
| 2213 | } |
Greg Clayton | 17a0cb6 | 2011-05-15 23:46:54 +0000 | [diff] [blame] | 2214 | else |
| 2215 | { |
| 2216 | m_supports_qGroupName = false; |
| 2217 | return false; |
| 2218 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 2219 | } |
| 2220 | return false; |
Greg Clayton | 9b1e1cd | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 2221 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 2222 | |
Greg Clayton | 9b1e1cd | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 2223 | void |
| 2224 | GDBRemoteCommunicationClient::TestPacketSpeed (const uint32_t num_packets) |
| 2225 | { |
| 2226 | uint32_t i; |
| 2227 | TimeValue start_time, end_time; |
| 2228 | uint64_t total_time_nsec; |
| 2229 | float packets_per_second; |
| 2230 | if (SendSpeedTestPacket (0, 0)) |
| 2231 | { |
| 2232 | for (uint32_t send_size = 0; send_size <= 1024; send_size *= 2) |
| 2233 | { |
| 2234 | for (uint32_t recv_size = 0; recv_size <= 1024; recv_size *= 2) |
| 2235 | { |
| 2236 | start_time = TimeValue::Now(); |
| 2237 | for (i=0; i<num_packets; ++i) |
| 2238 | { |
| 2239 | SendSpeedTestPacket (send_size, recv_size); |
| 2240 | } |
| 2241 | end_time = TimeValue::Now(); |
| 2242 | total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970(); |
Peter Collingbourne | ba23ca0 | 2011-06-18 23:52:14 +0000 | [diff] [blame] | 2243 | packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2244 | printf ("%u qSpeedTest(send=%-5u, recv=%-5u) in %" PRIu64 ".%9.9" PRIu64 " sec for %f packets/sec.\n", |
Greg Clayton | 9b1e1cd | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 2245 | num_packets, |
| 2246 | send_size, |
| 2247 | recv_size, |
Peter Collingbourne | ba23ca0 | 2011-06-18 23:52:14 +0000 | [diff] [blame] | 2248 | total_time_nsec / TimeValue::NanoSecPerSec, |
| 2249 | total_time_nsec % TimeValue::NanoSecPerSec, |
Greg Clayton | 9b1e1cd | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 2250 | packets_per_second); |
| 2251 | if (recv_size == 0) |
| 2252 | recv_size = 32; |
| 2253 | } |
| 2254 | if (send_size == 0) |
| 2255 | send_size = 32; |
| 2256 | } |
| 2257 | } |
| 2258 | else |
| 2259 | { |
| 2260 | start_time = TimeValue::Now(); |
| 2261 | for (i=0; i<num_packets; ++i) |
| 2262 | { |
| 2263 | GetCurrentProcessID (); |
| 2264 | } |
| 2265 | end_time = TimeValue::Now(); |
| 2266 | total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970(); |
Peter Collingbourne | ba23ca0 | 2011-06-18 23:52:14 +0000 | [diff] [blame] | 2267 | packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2268 | printf ("%u 'qC' packets packets in 0x%" PRIu64 "%9.9" PRIu64 " sec for %f packets/sec.\n", |
Greg Clayton | 9b1e1cd | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 2269 | num_packets, |
Peter Collingbourne | ba23ca0 | 2011-06-18 23:52:14 +0000 | [diff] [blame] | 2270 | total_time_nsec / TimeValue::NanoSecPerSec, |
| 2271 | total_time_nsec % TimeValue::NanoSecPerSec, |
Greg Clayton | 9b1e1cd | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 2272 | packets_per_second); |
| 2273 | } |
| 2274 | } |
| 2275 | |
| 2276 | bool |
| 2277 | GDBRemoteCommunicationClient::SendSpeedTestPacket (uint32_t send_size, uint32_t recv_size) |
| 2278 | { |
| 2279 | StreamString packet; |
| 2280 | packet.Printf ("qSpeedTest:response_size:%i;data:", recv_size); |
| 2281 | uint32_t bytes_left = send_size; |
| 2282 | while (bytes_left > 0) |
| 2283 | { |
| 2284 | if (bytes_left >= 26) |
| 2285 | { |
| 2286 | packet.PutCString("abcdefghijklmnopqrstuvwxyz"); |
| 2287 | bytes_left -= 26; |
| 2288 | } |
| 2289 | else |
| 2290 | { |
| 2291 | packet.Printf ("%*.*s;", bytes_left, bytes_left, "abcdefghijklmnopqrstuvwxyz"); |
| 2292 | bytes_left = 0; |
| 2293 | } |
| 2294 | } |
| 2295 | |
| 2296 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2297 | return SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success; |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 2298 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2299 | |
| 2300 | uint16_t |
Greg Clayton | dbf0457 | 2013-12-04 19:40:33 +0000 | [diff] [blame] | 2301 | GDBRemoteCommunicationClient::LaunchGDBserverAndGetPort (lldb::pid_t &pid, const char *remote_accept_hostname) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2302 | { |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2303 | pid = LLDB_INVALID_PROCESS_ID; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2304 | StringExtractorGDBRemote response; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2305 | StreamString stream; |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 2306 | stream.PutCString("qLaunchGDBServer;"); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2307 | std::string hostname; |
Greg Clayton | dbf0457 | 2013-12-04 19:40:33 +0000 | [diff] [blame] | 2308 | if (remote_accept_hostname && remote_accept_hostname[0]) |
| 2309 | hostname = remote_accept_hostname; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2310 | else |
| 2311 | { |
Greg Clayton | dbf0457 | 2013-12-04 19:40:33 +0000 | [diff] [blame] | 2312 | if (Host::GetHostname (hostname)) |
| 2313 | { |
| 2314 | // Make the GDB server we launch only accept connections from this host |
| 2315 | stream.Printf("host:%s;", hostname.c_str()); |
| 2316 | } |
| 2317 | else |
| 2318 | { |
| 2319 | // Make the GDB server we launch accept connections from any host since we can't figure out the hostname |
| 2320 | stream.Printf("host:*;"); |
| 2321 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2322 | } |
| 2323 | const char *packet = stream.GetData(); |
| 2324 | int packet_len = stream.GetSize(); |
| 2325 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2326 | if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2327 | { |
| 2328 | std::string name; |
| 2329 | std::string value; |
| 2330 | uint16_t port = 0; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2331 | while (response.GetNameColonValue(name, value)) |
| 2332 | { |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2333 | if (name.compare("port") == 0) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2334 | port = Args::StringToUInt32(value.c_str(), 0, 0); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2335 | else if (name.compare("pid") == 0) |
| 2336 | pid = Args::StringToUInt64(value.c_str(), LLDB_INVALID_PROCESS_ID, 0); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2337 | } |
| 2338 | return port; |
| 2339 | } |
| 2340 | return 0; |
| 2341 | } |
| 2342 | |
| 2343 | bool |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2344 | GDBRemoteCommunicationClient::KillSpawnedProcess (lldb::pid_t pid) |
| 2345 | { |
| 2346 | StreamString stream; |
| 2347 | stream.Printf ("qKillSpawnedProcess:%" PRId64 , pid); |
| 2348 | const char *packet = stream.GetData(); |
| 2349 | int packet_len = stream.GetSize(); |
Sylvestre Ledru | fd654c4 | 2013-10-06 09:51:02 +0000 | [diff] [blame] | 2350 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2351 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2352 | if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2353 | { |
| 2354 | if (response.IsOKResponse()) |
| 2355 | return true; |
| 2356 | } |
| 2357 | return false; |
| 2358 | } |
| 2359 | |
| 2360 | bool |
Jason Molenda | e9ca4af | 2013-02-23 02:04:45 +0000 | [diff] [blame] | 2361 | GDBRemoteCommunicationClient::SetCurrentThread (uint64_t tid) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2362 | { |
| 2363 | if (m_curr_tid == tid) |
| 2364 | return true; |
Jason Molenda | e9ca4af | 2013-02-23 02:04:45 +0000 | [diff] [blame] | 2365 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2366 | char packet[32]; |
| 2367 | int packet_len; |
Jason Molenda | e9ca4af | 2013-02-23 02:04:45 +0000 | [diff] [blame] | 2368 | if (tid == UINT64_MAX) |
| 2369 | packet_len = ::snprintf (packet, sizeof(packet), "Hg-1"); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2370 | else |
Jason Molenda | e9ca4af | 2013-02-23 02:04:45 +0000 | [diff] [blame] | 2371 | packet_len = ::snprintf (packet, sizeof(packet), "Hg%" PRIx64, tid); |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 2372 | assert (packet_len + 1 < (int)sizeof(packet)); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2373 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2374 | if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2375 | { |
| 2376 | if (response.IsOKResponse()) |
| 2377 | { |
| 2378 | m_curr_tid = tid; |
| 2379 | return true; |
| 2380 | } |
| 2381 | } |
| 2382 | return false; |
| 2383 | } |
| 2384 | |
| 2385 | bool |
Jason Molenda | e9ca4af | 2013-02-23 02:04:45 +0000 | [diff] [blame] | 2386 | GDBRemoteCommunicationClient::SetCurrentThreadForRun (uint64_t tid) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2387 | { |
| 2388 | if (m_curr_tid_run == tid) |
| 2389 | return true; |
Jason Molenda | e9ca4af | 2013-02-23 02:04:45 +0000 | [diff] [blame] | 2390 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2391 | char packet[32]; |
| 2392 | int packet_len; |
Jason Molenda | e9ca4af | 2013-02-23 02:04:45 +0000 | [diff] [blame] | 2393 | if (tid == UINT64_MAX) |
| 2394 | packet_len = ::snprintf (packet, sizeof(packet), "Hc-1"); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2395 | else |
Jason Molenda | e9ca4af | 2013-02-23 02:04:45 +0000 | [diff] [blame] | 2396 | packet_len = ::snprintf (packet, sizeof(packet), "Hc%" PRIx64, tid); |
| 2397 | |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 2398 | assert (packet_len + 1 < (int)sizeof(packet)); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2399 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2400 | if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2401 | { |
| 2402 | if (response.IsOKResponse()) |
| 2403 | { |
| 2404 | m_curr_tid_run = tid; |
| 2405 | return true; |
| 2406 | } |
| 2407 | } |
| 2408 | return false; |
| 2409 | } |
| 2410 | |
| 2411 | bool |
| 2412 | GDBRemoteCommunicationClient::GetStopReply (StringExtractorGDBRemote &response) |
| 2413 | { |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2414 | if (SendPacketAndWaitForResponse("?", 1, response, false) == PacketResult::Success) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2415 | return response.IsNormalResponse(); |
| 2416 | return false; |
| 2417 | } |
| 2418 | |
| 2419 | bool |
Greg Clayton | f402f78 | 2012-10-13 02:11:55 +0000 | [diff] [blame] | 2420 | GDBRemoteCommunicationClient::GetThreadStopInfo (lldb::tid_t tid, StringExtractorGDBRemote &response) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2421 | { |
| 2422 | if (m_supports_qThreadStopInfo) |
| 2423 | { |
| 2424 | char packet[256]; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2425 | int packet_len = ::snprintf(packet, sizeof(packet), "qThreadStopInfo%" PRIx64, tid); |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 2426 | assert (packet_len < (int)sizeof(packet)); |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2427 | if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2428 | { |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 2429 | if (response.IsUnsupportedResponse()) |
| 2430 | m_supports_qThreadStopInfo = false; |
| 2431 | else if (response.IsNormalResponse()) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2432 | return true; |
| 2433 | else |
| 2434 | return false; |
| 2435 | } |
Greg Clayton | 17a0cb6 | 2011-05-15 23:46:54 +0000 | [diff] [blame] | 2436 | else |
| 2437 | { |
| 2438 | m_supports_qThreadStopInfo = false; |
| 2439 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2440 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2441 | return false; |
| 2442 | } |
| 2443 | |
| 2444 | |
| 2445 | uint8_t |
| 2446 | GDBRemoteCommunicationClient::SendGDBStoppointTypePacket (GDBStoppointType type, bool insert, addr_t addr, uint32_t length) |
| 2447 | { |
| 2448 | switch (type) |
| 2449 | { |
| 2450 | case eBreakpointSoftware: if (!m_supports_z0) return UINT8_MAX; break; |
| 2451 | case eBreakpointHardware: if (!m_supports_z1) return UINT8_MAX; break; |
| 2452 | case eWatchpointWrite: if (!m_supports_z2) return UINT8_MAX; break; |
| 2453 | case eWatchpointRead: if (!m_supports_z3) return UINT8_MAX; break; |
| 2454 | case eWatchpointReadWrite: if (!m_supports_z4) return UINT8_MAX; break; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2455 | } |
| 2456 | |
| 2457 | char packet[64]; |
| 2458 | const int packet_len = ::snprintf (packet, |
| 2459 | sizeof(packet), |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2460 | "%c%i,%" PRIx64 ",%x", |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2461 | insert ? 'Z' : 'z', |
| 2462 | type, |
| 2463 | addr, |
| 2464 | length); |
| 2465 | |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 2466 | assert (packet_len + 1 < (int)sizeof(packet)); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2467 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2468 | if (SendPacketAndWaitForResponse(packet, packet_len, response, true) == PacketResult::Success) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2469 | { |
| 2470 | if (response.IsOKResponse()) |
| 2471 | return 0; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2472 | else if (response.IsErrorResponse()) |
| 2473 | return response.GetError(); |
| 2474 | } |
Greg Clayton | 17a0cb6 | 2011-05-15 23:46:54 +0000 | [diff] [blame] | 2475 | else |
| 2476 | { |
| 2477 | switch (type) |
| 2478 | { |
| 2479 | case eBreakpointSoftware: m_supports_z0 = false; break; |
| 2480 | case eBreakpointHardware: m_supports_z1 = false; break; |
| 2481 | case eWatchpointWrite: m_supports_z2 = false; break; |
| 2482 | case eWatchpointRead: m_supports_z3 = false; break; |
| 2483 | case eWatchpointReadWrite: m_supports_z4 = false; break; |
Greg Clayton | 17a0cb6 | 2011-05-15 23:46:54 +0000 | [diff] [blame] | 2484 | } |
| 2485 | } |
| 2486 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2487 | return UINT8_MAX; |
| 2488 | } |
Greg Clayton | adc00cb | 2011-05-20 23:38:13 +0000 | [diff] [blame] | 2489 | |
| 2490 | size_t |
| 2491 | GDBRemoteCommunicationClient::GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids, |
| 2492 | bool &sequence_mutex_unavailable) |
| 2493 | { |
| 2494 | Mutex::Locker locker; |
| 2495 | thread_ids.clear(); |
| 2496 | |
Jim Ingham | 4ceb928 | 2012-06-08 22:50:40 +0000 | [diff] [blame] | 2497 | if (GetSequenceMutex (locker, "ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex")) |
Greg Clayton | adc00cb | 2011-05-20 23:38:13 +0000 | [diff] [blame] | 2498 | { |
| 2499 | sequence_mutex_unavailable = false; |
| 2500 | StringExtractorGDBRemote response; |
| 2501 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2502 | PacketResult packet_result; |
| 2503 | for (packet_result = SendPacketAndWaitForResponseNoLock ("qfThreadInfo", strlen("qfThreadInfo"), response); |
| 2504 | packet_result == PacketResult::Success && response.IsNormalResponse(); |
| 2505 | packet_result = SendPacketAndWaitForResponseNoLock ("qsThreadInfo", strlen("qsThreadInfo"), response)) |
Greg Clayton | adc00cb | 2011-05-20 23:38:13 +0000 | [diff] [blame] | 2506 | { |
| 2507 | char ch = response.GetChar(); |
| 2508 | if (ch == 'l') |
| 2509 | break; |
| 2510 | if (ch == 'm') |
| 2511 | { |
| 2512 | do |
| 2513 | { |
Jason Molenda | e9ca4af | 2013-02-23 02:04:45 +0000 | [diff] [blame] | 2514 | tid_t tid = response.GetHexMaxU64(false, LLDB_INVALID_THREAD_ID); |
Greg Clayton | adc00cb | 2011-05-20 23:38:13 +0000 | [diff] [blame] | 2515 | |
| 2516 | if (tid != LLDB_INVALID_THREAD_ID) |
| 2517 | { |
| 2518 | thread_ids.push_back (tid); |
| 2519 | } |
| 2520 | ch = response.GetChar(); // Skip the command separator |
| 2521 | } while (ch == ','); // Make sure we got a comma separator |
| 2522 | } |
| 2523 | } |
| 2524 | } |
| 2525 | else |
| 2526 | { |
Jim Ingham | 4ceb928 | 2012-06-08 22:50:40 +0000 | [diff] [blame] | 2527 | #if defined (LLDB_CONFIGURATION_DEBUG) |
| 2528 | // assert(!"ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex"); |
| 2529 | #else |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2530 | Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS)); |
Greg Clayton | c3c0b0e | 2012-04-12 19:04:34 +0000 | [diff] [blame] | 2531 | if (log) |
| 2532 | log->Printf("error: failed to get packet sequence mutex, not sending packet 'qfThreadInfo'"); |
Jim Ingham | 4ceb928 | 2012-06-08 22:50:40 +0000 | [diff] [blame] | 2533 | #endif |
Greg Clayton | adc00cb | 2011-05-20 23:38:13 +0000 | [diff] [blame] | 2534 | sequence_mutex_unavailable = true; |
| 2535 | } |
| 2536 | return thread_ids.size(); |
| 2537 | } |
Greg Clayton | 37a0a24 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 2538 | |
| 2539 | lldb::addr_t |
| 2540 | GDBRemoteCommunicationClient::GetShlibInfoAddr() |
| 2541 | { |
| 2542 | if (!IsRunning()) |
| 2543 | { |
| 2544 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2545 | if (SendPacketAndWaitForResponse("qShlibInfoAddr", ::strlen ("qShlibInfoAddr"), response, false) == PacketResult::Success) |
Greg Clayton | 37a0a24 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 2546 | { |
| 2547 | if (response.IsNormalResponse()) |
| 2548 | return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS); |
| 2549 | } |
| 2550 | } |
| 2551 | return LLDB_INVALID_ADDRESS; |
| 2552 | } |
| 2553 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2554 | lldb_private::Error |
| 2555 | GDBRemoteCommunicationClient::RunShellCommand (const char *command, // Shouldn't be NULL |
| 2556 | const char *working_dir, // Pass NULL to use the current working directory |
| 2557 | int *status_ptr, // Pass NULL if you don't want the process exit status |
| 2558 | int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit |
| 2559 | std::string *command_output, // Pass NULL if you don't want the command output |
| 2560 | uint32_t timeout_sec) // Timeout in seconds to wait for shell program to finish |
| 2561 | { |
| 2562 | lldb_private::StreamString stream; |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2563 | stream.PutCString("qPlatform_shell:"); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2564 | stream.PutBytesAsRawHex8(command, strlen(command)); |
| 2565 | stream.PutChar(','); |
| 2566 | stream.PutHex32(timeout_sec); |
| 2567 | if (working_dir && *working_dir) |
| 2568 | { |
| 2569 | stream.PutChar(','); |
| 2570 | stream.PutBytesAsRawHex8(working_dir, strlen(working_dir)); |
| 2571 | } |
| 2572 | const char *packet = stream.GetData(); |
| 2573 | int packet_len = stream.GetSize(); |
| 2574 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2575 | if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2576 | { |
| 2577 | if (response.GetChar() != 'F') |
| 2578 | return Error("malformed reply"); |
| 2579 | if (response.GetChar() != ',') |
| 2580 | return Error("malformed reply"); |
| 2581 | uint32_t exitcode = response.GetHexMaxU32(false, UINT32_MAX); |
| 2582 | if (exitcode == UINT32_MAX) |
| 2583 | return Error("unable to run remote process"); |
| 2584 | else if (status_ptr) |
| 2585 | *status_ptr = exitcode; |
| 2586 | if (response.GetChar() != ',') |
| 2587 | return Error("malformed reply"); |
| 2588 | uint32_t signo = response.GetHexMaxU32(false, UINT32_MAX); |
| 2589 | if (signo_ptr) |
| 2590 | *signo_ptr = signo; |
| 2591 | if (response.GetChar() != ',') |
| 2592 | return Error("malformed reply"); |
| 2593 | std::string output; |
| 2594 | response.GetEscapedBinaryData(output); |
| 2595 | if (command_output) |
| 2596 | command_output->assign(output); |
| 2597 | return Error(); |
| 2598 | } |
| 2599 | return Error("unable to send packet"); |
| 2600 | } |
| 2601 | |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2602 | Error |
| 2603 | GDBRemoteCommunicationClient::MakeDirectory (const char *path, |
| 2604 | uint32_t file_permissions) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2605 | { |
| 2606 | lldb_private::StreamString stream; |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2607 | stream.PutCString("qPlatform_mkdir:"); |
| 2608 | stream.PutHex32(file_permissions); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2609 | stream.PutChar(','); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2610 | stream.PutBytesAsRawHex8(path, strlen(path)); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2611 | const char *packet = stream.GetData(); |
| 2612 | int packet_len = stream.GetSize(); |
| 2613 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2614 | if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2615 | { |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2616 | return Error(response.GetHexMaxU32(false, UINT32_MAX), eErrorTypePOSIX); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2617 | } |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2618 | return Error(); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2619 | |
| 2620 | } |
| 2621 | |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2622 | Error |
| 2623 | GDBRemoteCommunicationClient::SetFilePermissions (const char *path, |
| 2624 | uint32_t file_permissions) |
| 2625 | { |
| 2626 | lldb_private::StreamString stream; |
| 2627 | stream.PutCString("qPlatform_chmod:"); |
| 2628 | stream.PutHex32(file_permissions); |
| 2629 | stream.PutChar(','); |
| 2630 | stream.PutBytesAsRawHex8(path, strlen(path)); |
| 2631 | const char *packet = stream.GetData(); |
| 2632 | int packet_len = stream.GetSize(); |
| 2633 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2634 | if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success) |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2635 | { |
| 2636 | return Error(response.GetHexMaxU32(false, UINT32_MAX), eErrorTypePOSIX); |
| 2637 | } |
| 2638 | return Error(); |
| 2639 | |
| 2640 | } |
| 2641 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2642 | static uint64_t |
| 2643 | ParseHostIOPacketResponse (StringExtractorGDBRemote &response, |
| 2644 | uint64_t fail_result, |
| 2645 | Error &error) |
| 2646 | { |
| 2647 | response.SetFilePos(0); |
| 2648 | if (response.GetChar() != 'F') |
| 2649 | return fail_result; |
| 2650 | int32_t result = response.GetS32 (-2); |
| 2651 | if (result == -2) |
| 2652 | return fail_result; |
| 2653 | if (response.GetChar() == ',') |
| 2654 | { |
| 2655 | int result_errno = response.GetS32 (-2); |
| 2656 | if (result_errno != -2) |
| 2657 | error.SetError(result_errno, eErrorTypePOSIX); |
| 2658 | else |
| 2659 | error.SetError(-1, eErrorTypeGeneric); |
| 2660 | } |
| 2661 | else |
| 2662 | error.Clear(); |
| 2663 | return result; |
| 2664 | } |
| 2665 | lldb::user_id_t |
| 2666 | GDBRemoteCommunicationClient::OpenFile (const lldb_private::FileSpec& file_spec, |
| 2667 | uint32_t flags, |
| 2668 | mode_t mode, |
| 2669 | Error &error) |
| 2670 | { |
| 2671 | lldb_private::StreamString stream; |
| 2672 | stream.PutCString("vFile:open:"); |
| 2673 | std::string path (file_spec.GetPath()); |
| 2674 | if (path.empty()) |
| 2675 | return UINT64_MAX; |
| 2676 | stream.PutCStringAsRawHex8(path.c_str()); |
| 2677 | stream.PutChar(','); |
| 2678 | const uint32_t posix_open_flags = File::ConvertOpenOptionsForPOSIXOpen(flags); |
| 2679 | stream.PutHex32(posix_open_flags); |
| 2680 | stream.PutChar(','); |
| 2681 | stream.PutHex32(mode); |
| 2682 | const char* packet = stream.GetData(); |
| 2683 | int packet_len = stream.GetSize(); |
| 2684 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2685 | if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2686 | { |
| 2687 | return ParseHostIOPacketResponse (response, UINT64_MAX, error); |
| 2688 | } |
| 2689 | return UINT64_MAX; |
| 2690 | } |
| 2691 | |
| 2692 | bool |
| 2693 | GDBRemoteCommunicationClient::CloseFile (lldb::user_id_t fd, |
| 2694 | Error &error) |
| 2695 | { |
| 2696 | lldb_private::StreamString stream; |
| 2697 | stream.Printf("vFile:close:%i", (int)fd); |
| 2698 | const char* packet = stream.GetData(); |
| 2699 | int packet_len = stream.GetSize(); |
| 2700 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2701 | if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2702 | { |
| 2703 | return ParseHostIOPacketResponse (response, -1, error) == 0; |
| 2704 | } |
Deepak Panickal | d66b50c | 2013-10-22 12:27:43 +0000 | [diff] [blame] | 2705 | return false; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2706 | } |
| 2707 | |
| 2708 | // Extension of host I/O packets to get the file size. |
| 2709 | lldb::user_id_t |
| 2710 | GDBRemoteCommunicationClient::GetFileSize (const lldb_private::FileSpec& file_spec) |
| 2711 | { |
| 2712 | lldb_private::StreamString stream; |
| 2713 | stream.PutCString("vFile:size:"); |
| 2714 | std::string path (file_spec.GetPath()); |
| 2715 | stream.PutCStringAsRawHex8(path.c_str()); |
| 2716 | const char* packet = stream.GetData(); |
| 2717 | int packet_len = stream.GetSize(); |
| 2718 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2719 | if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2720 | { |
| 2721 | if (response.GetChar() != 'F') |
| 2722 | return UINT64_MAX; |
| 2723 | uint32_t retcode = response.GetHexMaxU64(false, UINT64_MAX); |
| 2724 | return retcode; |
| 2725 | } |
| 2726 | return UINT64_MAX; |
| 2727 | } |
| 2728 | |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2729 | Error |
| 2730 | GDBRemoteCommunicationClient::GetFilePermissions(const char *path, uint32_t &file_permissions) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2731 | { |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2732 | Error error; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2733 | lldb_private::StreamString stream; |
| 2734 | stream.PutCString("vFile:mode:"); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2735 | stream.PutCStringAsRawHex8(path); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2736 | const char* packet = stream.GetData(); |
| 2737 | int packet_len = stream.GetSize(); |
| 2738 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2739 | if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2740 | { |
| 2741 | if (response.GetChar() != 'F') |
| 2742 | { |
| 2743 | error.SetErrorStringWithFormat ("invalid response to '%s' packet", packet); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2744 | } |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2745 | else |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2746 | { |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2747 | const uint32_t mode = response.GetS32(-1); |
| 2748 | if (mode == -1) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2749 | { |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2750 | if (response.GetChar() == ',') |
| 2751 | { |
| 2752 | int response_errno = response.GetS32(-1); |
| 2753 | if (response_errno > 0) |
| 2754 | error.SetError(response_errno, lldb::eErrorTypePOSIX); |
| 2755 | else |
| 2756 | error.SetErrorToGenericError(); |
| 2757 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2758 | else |
| 2759 | error.SetErrorToGenericError(); |
| 2760 | } |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2761 | else |
| 2762 | { |
| 2763 | file_permissions = mode & (S_IRWXU|S_IRWXG|S_IRWXO); |
| 2764 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2765 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2766 | } |
| 2767 | else |
| 2768 | { |
| 2769 | error.SetErrorStringWithFormat ("failed to send '%s' packet", packet); |
| 2770 | } |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2771 | return error; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
| 2774 | uint64_t |
| 2775 | GDBRemoteCommunicationClient::ReadFile (lldb::user_id_t fd, |
| 2776 | uint64_t offset, |
| 2777 | void *dst, |
| 2778 | uint64_t dst_len, |
| 2779 | Error &error) |
| 2780 | { |
| 2781 | lldb_private::StreamString stream; |
| 2782 | stream.Printf("vFile:pread:%i,%" PRId64 ",%" PRId64, (int)fd, dst_len, offset); |
| 2783 | const char* packet = stream.GetData(); |
| 2784 | int packet_len = stream.GetSize(); |
| 2785 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2786 | if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2787 | { |
| 2788 | if (response.GetChar() != 'F') |
| 2789 | return 0; |
| 2790 | uint32_t retcode = response.GetHexMaxU32(false, UINT32_MAX); |
| 2791 | if (retcode == UINT32_MAX) |
| 2792 | return retcode; |
| 2793 | const char next = (response.Peek() ? *response.Peek() : 0); |
| 2794 | if (next == ',') |
| 2795 | return 0; |
| 2796 | if (next == ';') |
| 2797 | { |
| 2798 | response.GetChar(); // skip the semicolon |
| 2799 | std::string buffer; |
| 2800 | if (response.GetEscapedBinaryData(buffer)) |
| 2801 | { |
| 2802 | const uint64_t data_to_write = std::min<uint64_t>(dst_len, buffer.size()); |
| 2803 | if (data_to_write > 0) |
| 2804 | memcpy(dst, &buffer[0], data_to_write); |
| 2805 | return data_to_write; |
| 2806 | } |
| 2807 | } |
| 2808 | } |
| 2809 | return 0; |
| 2810 | } |
| 2811 | |
| 2812 | uint64_t |
| 2813 | GDBRemoteCommunicationClient::WriteFile (lldb::user_id_t fd, |
| 2814 | uint64_t offset, |
| 2815 | const void* src, |
| 2816 | uint64_t src_len, |
| 2817 | Error &error) |
| 2818 | { |
| 2819 | lldb_private::StreamGDBRemote stream; |
| 2820 | stream.Printf("vFile:pwrite:%i,%" PRId64 ",", (int)fd, offset); |
| 2821 | stream.PutEscapedBytes(src, src_len); |
| 2822 | const char* packet = stream.GetData(); |
| 2823 | int packet_len = stream.GetSize(); |
| 2824 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2825 | if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2826 | { |
| 2827 | if (response.GetChar() != 'F') |
| 2828 | { |
| 2829 | error.SetErrorStringWithFormat("write file failed"); |
| 2830 | return 0; |
| 2831 | } |
| 2832 | uint64_t bytes_written = response.GetU64(UINT64_MAX); |
| 2833 | if (bytes_written == UINT64_MAX) |
| 2834 | { |
| 2835 | error.SetErrorToGenericError(); |
| 2836 | if (response.GetChar() == ',') |
| 2837 | { |
| 2838 | int response_errno = response.GetS32(-1); |
| 2839 | if (response_errno > 0) |
| 2840 | error.SetError(response_errno, lldb::eErrorTypePOSIX); |
| 2841 | } |
| 2842 | return 0; |
| 2843 | } |
| 2844 | return bytes_written; |
| 2845 | } |
| 2846 | else |
| 2847 | { |
| 2848 | error.SetErrorString ("failed to send vFile:pwrite packet"); |
| 2849 | } |
| 2850 | return 0; |
| 2851 | } |
| 2852 | |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2853 | Error |
| 2854 | GDBRemoteCommunicationClient::CreateSymlink (const char *src, const char *dst) |
| 2855 | { |
| 2856 | Error error; |
| 2857 | lldb_private::StreamGDBRemote stream; |
| 2858 | stream.PutCString("vFile:symlink:"); |
| 2859 | // the unix symlink() command reverses its parameters where the dst if first, |
| 2860 | // so we follow suit here |
| 2861 | stream.PutCStringAsRawHex8(dst); |
| 2862 | stream.PutChar(','); |
| 2863 | stream.PutCStringAsRawHex8(src); |
| 2864 | const char* packet = stream.GetData(); |
| 2865 | int packet_len = stream.GetSize(); |
| 2866 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2867 | if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success) |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2868 | { |
| 2869 | if (response.GetChar() == 'F') |
| 2870 | { |
| 2871 | uint32_t result = response.GetU32(UINT32_MAX); |
| 2872 | if (result != 0) |
| 2873 | { |
| 2874 | error.SetErrorToGenericError(); |
| 2875 | if (response.GetChar() == ',') |
| 2876 | { |
| 2877 | int response_errno = response.GetS32(-1); |
| 2878 | if (response_errno > 0) |
| 2879 | error.SetError(response_errno, lldb::eErrorTypePOSIX); |
| 2880 | } |
| 2881 | } |
| 2882 | } |
| 2883 | else |
| 2884 | { |
| 2885 | // Should have returned with 'F<result>[,<errno>]' |
| 2886 | error.SetErrorStringWithFormat("symlink failed"); |
| 2887 | } |
| 2888 | } |
| 2889 | else |
| 2890 | { |
| 2891 | error.SetErrorString ("failed to send vFile:symlink packet"); |
| 2892 | } |
| 2893 | return error; |
| 2894 | } |
| 2895 | |
| 2896 | Error |
| 2897 | GDBRemoteCommunicationClient::Unlink (const char *path) |
| 2898 | { |
| 2899 | Error error; |
| 2900 | lldb_private::StreamGDBRemote stream; |
| 2901 | stream.PutCString("vFile:unlink:"); |
| 2902 | // the unix symlink() command reverses its parameters where the dst if first, |
| 2903 | // so we follow suit here |
| 2904 | stream.PutCStringAsRawHex8(path); |
| 2905 | const char* packet = stream.GetData(); |
| 2906 | int packet_len = stream.GetSize(); |
| 2907 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2908 | if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success) |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2909 | { |
| 2910 | if (response.GetChar() == 'F') |
| 2911 | { |
| 2912 | uint32_t result = response.GetU32(UINT32_MAX); |
| 2913 | if (result != 0) |
| 2914 | { |
| 2915 | error.SetErrorToGenericError(); |
| 2916 | if (response.GetChar() == ',') |
| 2917 | { |
| 2918 | int response_errno = response.GetS32(-1); |
| 2919 | if (response_errno > 0) |
| 2920 | error.SetError(response_errno, lldb::eErrorTypePOSIX); |
| 2921 | } |
| 2922 | } |
| 2923 | } |
| 2924 | else |
| 2925 | { |
| 2926 | // Should have returned with 'F<result>[,<errno>]' |
| 2927 | error.SetErrorStringWithFormat("unlink failed"); |
| 2928 | } |
| 2929 | } |
| 2930 | else |
| 2931 | { |
| 2932 | error.SetErrorString ("failed to send vFile:unlink packet"); |
| 2933 | } |
| 2934 | return error; |
| 2935 | } |
| 2936 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2937 | // Extension of host I/O packets to get whether a file exists. |
| 2938 | bool |
| 2939 | GDBRemoteCommunicationClient::GetFileExists (const lldb_private::FileSpec& file_spec) |
| 2940 | { |
| 2941 | lldb_private::StreamString stream; |
| 2942 | stream.PutCString("vFile:exists:"); |
| 2943 | std::string path (file_spec.GetPath()); |
| 2944 | stream.PutCStringAsRawHex8(path.c_str()); |
| 2945 | const char* packet = stream.GetData(); |
| 2946 | int packet_len = stream.GetSize(); |
| 2947 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2948 | if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2949 | { |
| 2950 | if (response.GetChar() != 'F') |
| 2951 | return false; |
| 2952 | if (response.GetChar() != ',') |
| 2953 | return false; |
| 2954 | bool retcode = (response.GetChar() != '0'); |
| 2955 | return retcode; |
| 2956 | } |
| 2957 | return false; |
| 2958 | } |
| 2959 | |
| 2960 | bool |
| 2961 | GDBRemoteCommunicationClient::CalculateMD5 (const lldb_private::FileSpec& file_spec, |
| 2962 | uint64_t &high, |
| 2963 | uint64_t &low) |
| 2964 | { |
| 2965 | lldb_private::StreamString stream; |
| 2966 | stream.PutCString("vFile:MD5:"); |
| 2967 | std::string path (file_spec.GetPath()); |
| 2968 | stream.PutCStringAsRawHex8(path.c_str()); |
| 2969 | const char* packet = stream.GetData(); |
| 2970 | int packet_len = stream.GetSize(); |
| 2971 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2972 | if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2973 | { |
| 2974 | if (response.GetChar() != 'F') |
| 2975 | return false; |
| 2976 | if (response.GetChar() != ',') |
| 2977 | return false; |
| 2978 | if (response.Peek() && *response.Peek() == 'x') |
| 2979 | return false; |
| 2980 | low = response.GetHexMaxU64(false, UINT64_MAX); |
| 2981 | high = response.GetHexMaxU64(false, UINT64_MAX); |
| 2982 | return true; |
| 2983 | } |
| 2984 | return false; |
| 2985 | } |
Greg Clayton | f74cf86 | 2013-11-13 23:28:31 +0000 | [diff] [blame] | 2986 | |
| 2987 | bool |
| 2988 | GDBRemoteCommunicationClient::ReadRegister(lldb::tid_t tid, uint32_t reg, StringExtractorGDBRemote &response) |
| 2989 | { |
| 2990 | Mutex::Locker locker; |
| 2991 | if (GetSequenceMutex (locker, "Didn't get sequence mutex for p packet.")) |
| 2992 | { |
| 2993 | const bool thread_suffix_supported = GetThreadSuffixSupported(); |
| 2994 | |
| 2995 | if (thread_suffix_supported || SetCurrentThread(tid)) |
| 2996 | { |
| 2997 | char packet[64]; |
| 2998 | int packet_len = 0; |
| 2999 | if (thread_suffix_supported) |
| 3000 | packet_len = ::snprintf (packet, sizeof(packet), "p%x;thread:%4.4" PRIx64 ";", reg, tid); |
| 3001 | else |
| 3002 | packet_len = ::snprintf (packet, sizeof(packet), "p%x", reg); |
| 3003 | assert (packet_len < ((int)sizeof(packet) - 1)); |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 3004 | return SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success; |
Greg Clayton | f74cf86 | 2013-11-13 23:28:31 +0000 | [diff] [blame] | 3005 | } |
| 3006 | } |
| 3007 | return false; |
| 3008 | |
| 3009 | } |
| 3010 | |
| 3011 | |
| 3012 | bool |
| 3013 | GDBRemoteCommunicationClient::ReadAllRegisters (lldb::tid_t tid, StringExtractorGDBRemote &response) |
| 3014 | { |
| 3015 | Mutex::Locker locker; |
| 3016 | if (GetSequenceMutex (locker, "Didn't get sequence mutex for g packet.")) |
| 3017 | { |
| 3018 | const bool thread_suffix_supported = GetThreadSuffixSupported(); |
| 3019 | |
| 3020 | if (thread_suffix_supported || SetCurrentThread(tid)) |
| 3021 | { |
| 3022 | char packet[64]; |
| 3023 | int packet_len = 0; |
| 3024 | // Get all registers in one packet |
| 3025 | if (thread_suffix_supported) |
| 3026 | packet_len = ::snprintf (packet, sizeof(packet), "g;thread:%4.4" PRIx64 ";", tid); |
| 3027 | else |
| 3028 | packet_len = ::snprintf (packet, sizeof(packet), "g"); |
| 3029 | assert (packet_len < ((int)sizeof(packet) - 1)); |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 3030 | return SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success; |
Greg Clayton | f74cf86 | 2013-11-13 23:28:31 +0000 | [diff] [blame] | 3031 | } |
| 3032 | } |
| 3033 | return false; |
| 3034 | } |
| 3035 | bool |
| 3036 | GDBRemoteCommunicationClient::SaveRegisterState (lldb::tid_t tid, uint32_t &save_id) |
| 3037 | { |
| 3038 | save_id = 0; // Set to invalid save ID |
| 3039 | if (m_supports_QSaveRegisterState == eLazyBoolNo) |
| 3040 | return false; |
| 3041 | |
| 3042 | m_supports_QSaveRegisterState = eLazyBoolYes; |
| 3043 | Mutex::Locker locker; |
| 3044 | if (GetSequenceMutex (locker, "Didn't get sequence mutex for QSaveRegisterState.")) |
| 3045 | { |
| 3046 | const bool thread_suffix_supported = GetThreadSuffixSupported(); |
| 3047 | if (thread_suffix_supported || SetCurrentThread(tid)) |
| 3048 | { |
| 3049 | char packet[256]; |
| 3050 | if (thread_suffix_supported) |
| 3051 | ::snprintf (packet, sizeof(packet), "QSaveRegisterState;thread:%4.4" PRIx64 ";", tid); |
| 3052 | else |
| 3053 | ::strncpy (packet, "QSaveRegisterState", sizeof(packet)); |
| 3054 | |
| 3055 | StringExtractorGDBRemote response; |
| 3056 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 3057 | if (SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success) |
Greg Clayton | f74cf86 | 2013-11-13 23:28:31 +0000 | [diff] [blame] | 3058 | { |
| 3059 | if (response.IsUnsupportedResponse()) |
| 3060 | { |
| 3061 | // This packet isn't supported, don't try calling it again |
| 3062 | m_supports_QSaveRegisterState = eLazyBoolNo; |
| 3063 | } |
| 3064 | |
| 3065 | const uint32_t response_save_id = response.GetU32(0); |
| 3066 | if (response_save_id != 0) |
| 3067 | { |
| 3068 | save_id = response_save_id; |
| 3069 | return true; |
| 3070 | } |
| 3071 | } |
| 3072 | } |
| 3073 | } |
| 3074 | return false; |
| 3075 | } |
| 3076 | |
| 3077 | bool |
| 3078 | GDBRemoteCommunicationClient::RestoreRegisterState (lldb::tid_t tid, uint32_t save_id) |
| 3079 | { |
| 3080 | // We use the "m_supports_QSaveRegisterState" variable here becuase the |
| 3081 | // QSaveRegisterState and QRestoreRegisterState packets must both be supported in |
| 3082 | // order to be useful |
| 3083 | if (m_supports_QSaveRegisterState == eLazyBoolNo) |
| 3084 | return false; |
| 3085 | |
| 3086 | Mutex::Locker locker; |
| 3087 | if (GetSequenceMutex (locker, "Didn't get sequence mutex for QRestoreRegisterState.")) |
| 3088 | { |
| 3089 | const bool thread_suffix_supported = GetThreadSuffixSupported(); |
| 3090 | if (thread_suffix_supported || SetCurrentThread(tid)) |
| 3091 | { |
| 3092 | char packet[256]; |
| 3093 | if (thread_suffix_supported) |
| 3094 | ::snprintf (packet, sizeof(packet), "QRestoreRegisterState:%u;thread:%4.4" PRIx64 ";", save_id, tid); |
| 3095 | else |
| 3096 | ::snprintf (packet, sizeof(packet), "QRestoreRegisterState:%u" PRIx64 ";", save_id); |
| 3097 | |
| 3098 | StringExtractorGDBRemote response; |
| 3099 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 3100 | if (SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success) |
Greg Clayton | f74cf86 | 2013-11-13 23:28:31 +0000 | [diff] [blame] | 3101 | { |
| 3102 | if (response.IsOKResponse()) |
| 3103 | { |
| 3104 | return true; |
| 3105 | } |
| 3106 | else if (response.IsUnsupportedResponse()) |
| 3107 | { |
| 3108 | // This packet isn't supported, don't try calling this packet or |
| 3109 | // QSaveRegisterState again... |
| 3110 | m_supports_QSaveRegisterState = eLazyBoolNo; |
| 3111 | } |
| 3112 | } |
| 3113 | } |
| 3114 | } |
| 3115 | return false; |
| 3116 | } |