Greg Clayton | 61d043b | 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 |
| 14 | // C++ Includes |
| 15 | // Other libraries and framework includes |
| 16 | #include "llvm/ADT/Triple.h" |
| 17 | #include "lldb/Interpreter/Args.h" |
| 18 | #include "lldb/Core/ConnectionFileDescriptor.h" |
| 19 | #include "lldb/Core/Log.h" |
| 20 | #include "lldb/Core/State.h" |
| 21 | #include "lldb/Core/StreamString.h" |
| 22 | #include "lldb/Host/Endian.h" |
| 23 | #include "lldb/Host/Host.h" |
| 24 | #include "lldb/Host/TimeValue.h" |
| 25 | |
| 26 | // Project includes |
| 27 | #include "Utility/StringExtractorGDBRemote.h" |
| 28 | #include "ProcessGDBRemote.h" |
| 29 | #include "ProcessGDBRemoteLog.h" |
| 30 | |
| 31 | using namespace lldb; |
| 32 | using namespace lldb_private; |
| 33 | |
| 34 | //---------------------------------------------------------------------- |
| 35 | // GDBRemoteCommunicationClient constructor |
| 36 | //---------------------------------------------------------------------- |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 37 | GDBRemoteCommunicationClient::GDBRemoteCommunicationClient(bool is_platform) : |
| 38 | GDBRemoteCommunication("gdb-remote.client", "gdb-remote.client.rx_packet", is_platform), |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 39 | m_supports_not_sending_acks (eLazyBoolCalculate), |
| 40 | m_supports_thread_suffix (eLazyBoolCalculate), |
Greg Clayton | a1f645e | 2012-04-10 03:22:03 +0000 | [diff] [blame] | 41 | m_supports_threads_in_stop_reply (eLazyBoolCalculate), |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 42 | m_supports_vCont_all (eLazyBoolCalculate), |
| 43 | m_supports_vCont_any (eLazyBoolCalculate), |
| 44 | m_supports_vCont_c (eLazyBoolCalculate), |
| 45 | m_supports_vCont_C (eLazyBoolCalculate), |
| 46 | m_supports_vCont_s (eLazyBoolCalculate), |
| 47 | m_supports_vCont_S (eLazyBoolCalculate), |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 48 | m_qHostInfo_is_valid (eLazyBoolCalculate), |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 49 | m_supports_alloc_dealloc_memory (eLazyBoolCalculate), |
Greg Clayton | a938553 | 2011-11-18 07:03:08 +0000 | [diff] [blame] | 50 | m_supports_memory_region_info (eLazyBoolCalculate), |
Johnny Chen | 7cbdcfb | 2012-05-23 21:09:52 +0000 | [diff] [blame^] | 51 | m_supports_watchpoint_support_info (eLazyBoolCalculate), |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 52 | m_supports_qProcessInfoPID (true), |
| 53 | m_supports_qfProcessInfo (true), |
| 54 | m_supports_qUserName (true), |
| 55 | m_supports_qGroupName (true), |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 56 | m_supports_qThreadStopInfo (true), |
| 57 | m_supports_z0 (true), |
| 58 | m_supports_z1 (true), |
| 59 | m_supports_z2 (true), |
| 60 | m_supports_z3 (true), |
| 61 | m_supports_z4 (true), |
| 62 | m_curr_tid (LLDB_INVALID_THREAD_ID), |
| 63 | m_curr_tid_run (LLDB_INVALID_THREAD_ID), |
Johnny Chen | 7cbdcfb | 2012-05-23 21:09:52 +0000 | [diff] [blame^] | 64 | m_num_supported_hardware_watchpoints (0), |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 65 | m_async_mutex (Mutex::eMutexTypeRecursive), |
| 66 | m_async_packet_predicate (false), |
| 67 | m_async_packet (), |
| 68 | m_async_response (), |
| 69 | m_async_signal (-1), |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 70 | m_host_arch(), |
| 71 | m_os_version_major (UINT32_MAX), |
| 72 | m_os_version_minor (UINT32_MAX), |
| 73 | m_os_version_update (UINT32_MAX) |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 74 | { |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | //---------------------------------------------------------------------- |
| 78 | // Destructor |
| 79 | //---------------------------------------------------------------------- |
| 80 | GDBRemoteCommunicationClient::~GDBRemoteCommunicationClient() |
| 81 | { |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 82 | if (IsConnected()) |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 83 | Disconnect(); |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | bool |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 87 | GDBRemoteCommunicationClient::HandshakeWithServer (Error *error_ptr) |
| 88 | { |
| 89 | // Start the read thread after we send the handshake ack since if we |
| 90 | // fail to send the handshake ack, there is no reason to continue... |
| 91 | if (SendAck()) |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 92 | return true; |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 93 | |
| 94 | if (error_ptr) |
| 95 | error_ptr->SetErrorString("failed to send the handshake ack"); |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | GDBRemoteCommunicationClient::QueryNoAckModeSupported () |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 101 | { |
| 102 | if (m_supports_not_sending_acks == eLazyBoolCalculate) |
| 103 | { |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 104 | m_send_acks = true; |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 105 | m_supports_not_sending_acks = eLazyBoolNo; |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 106 | |
| 107 | StringExtractorGDBRemote response; |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 108 | if (SendPacketAndWaitForResponse("QStartNoAckMode", response, false)) |
| 109 | { |
| 110 | if (response.IsOKResponse()) |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 111 | { |
| 112 | m_send_acks = false; |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 113 | m_supports_not_sending_acks = eLazyBoolYes; |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 114 | } |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void |
Greg Clayton | a1f645e | 2012-04-10 03:22:03 +0000 | [diff] [blame] | 120 | GDBRemoteCommunicationClient::GetListThreadsInStopReplySupported () |
| 121 | { |
| 122 | if (m_supports_threads_in_stop_reply == eLazyBoolCalculate) |
| 123 | { |
| 124 | m_supports_threads_in_stop_reply = eLazyBoolNo; |
| 125 | |
| 126 | StringExtractorGDBRemote response; |
| 127 | if (SendPacketAndWaitForResponse("QListThreadsInStopReply", response, false)) |
| 128 | { |
| 129 | if (response.IsOKResponse()) |
| 130 | m_supports_threads_in_stop_reply = eLazyBoolYes; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | |
| 136 | void |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 137 | GDBRemoteCommunicationClient::ResetDiscoverableSettings() |
| 138 | { |
| 139 | m_supports_not_sending_acks = eLazyBoolCalculate; |
| 140 | m_supports_thread_suffix = eLazyBoolCalculate; |
Greg Clayton | a1f645e | 2012-04-10 03:22:03 +0000 | [diff] [blame] | 141 | m_supports_threads_in_stop_reply = eLazyBoolCalculate; |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 142 | m_supports_vCont_c = eLazyBoolCalculate; |
| 143 | m_supports_vCont_C = eLazyBoolCalculate; |
| 144 | m_supports_vCont_s = eLazyBoolCalculate; |
| 145 | m_supports_vCont_S = eLazyBoolCalculate; |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 146 | m_qHostInfo_is_valid = eLazyBoolCalculate; |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 147 | m_supports_alloc_dealloc_memory = eLazyBoolCalculate; |
Greg Clayton | a938553 | 2011-11-18 07:03:08 +0000 | [diff] [blame] | 148 | m_supports_memory_region_info = eLazyBoolCalculate; |
Greg Clayton | 989816b | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 149 | |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 150 | m_supports_qProcessInfoPID = true; |
| 151 | m_supports_qfProcessInfo = true; |
| 152 | m_supports_qUserName = true; |
| 153 | m_supports_qGroupName = true; |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 154 | m_supports_qThreadStopInfo = true; |
| 155 | m_supports_z0 = true; |
| 156 | m_supports_z1 = true; |
| 157 | m_supports_z2 = true; |
| 158 | m_supports_z3 = true; |
| 159 | m_supports_z4 = true; |
Greg Clayton | cb8977d | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 160 | m_host_arch.Clear(); |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | |
| 164 | bool |
| 165 | GDBRemoteCommunicationClient::GetThreadSuffixSupported () |
| 166 | { |
| 167 | if (m_supports_thread_suffix == eLazyBoolCalculate) |
| 168 | { |
| 169 | StringExtractorGDBRemote response; |
| 170 | m_supports_thread_suffix = eLazyBoolNo; |
| 171 | if (SendPacketAndWaitForResponse("QThreadSuffixSupported", response, false)) |
| 172 | { |
| 173 | if (response.IsOKResponse()) |
| 174 | m_supports_thread_suffix = eLazyBoolYes; |
| 175 | } |
| 176 | } |
| 177 | return m_supports_thread_suffix; |
| 178 | } |
| 179 | bool |
| 180 | GDBRemoteCommunicationClient::GetVContSupported (char flavor) |
| 181 | { |
| 182 | if (m_supports_vCont_c == eLazyBoolCalculate) |
| 183 | { |
| 184 | StringExtractorGDBRemote response; |
| 185 | m_supports_vCont_any = eLazyBoolNo; |
| 186 | m_supports_vCont_all = eLazyBoolNo; |
| 187 | m_supports_vCont_c = eLazyBoolNo; |
| 188 | m_supports_vCont_C = eLazyBoolNo; |
| 189 | m_supports_vCont_s = eLazyBoolNo; |
| 190 | m_supports_vCont_S = eLazyBoolNo; |
| 191 | if (SendPacketAndWaitForResponse("vCont?", response, false)) |
| 192 | { |
| 193 | const char *response_cstr = response.GetStringRef().c_str(); |
| 194 | if (::strstr (response_cstr, ";c")) |
| 195 | m_supports_vCont_c = eLazyBoolYes; |
| 196 | |
| 197 | if (::strstr (response_cstr, ";C")) |
| 198 | m_supports_vCont_C = eLazyBoolYes; |
| 199 | |
| 200 | if (::strstr (response_cstr, ";s")) |
| 201 | m_supports_vCont_s = eLazyBoolYes; |
| 202 | |
| 203 | if (::strstr (response_cstr, ";S")) |
| 204 | m_supports_vCont_S = eLazyBoolYes; |
| 205 | |
| 206 | if (m_supports_vCont_c == eLazyBoolYes && |
| 207 | m_supports_vCont_C == eLazyBoolYes && |
| 208 | m_supports_vCont_s == eLazyBoolYes && |
| 209 | m_supports_vCont_S == eLazyBoolYes) |
| 210 | { |
| 211 | m_supports_vCont_all = eLazyBoolYes; |
| 212 | } |
| 213 | |
| 214 | if (m_supports_vCont_c == eLazyBoolYes || |
| 215 | m_supports_vCont_C == eLazyBoolYes || |
| 216 | m_supports_vCont_s == eLazyBoolYes || |
| 217 | m_supports_vCont_S == eLazyBoolYes) |
| 218 | { |
| 219 | m_supports_vCont_any = eLazyBoolYes; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | switch (flavor) |
| 225 | { |
| 226 | case 'a': return m_supports_vCont_any; |
| 227 | case 'A': return m_supports_vCont_all; |
| 228 | case 'c': return m_supports_vCont_c; |
| 229 | case 'C': return m_supports_vCont_C; |
| 230 | case 's': return m_supports_vCont_s; |
| 231 | case 'S': return m_supports_vCont_S; |
| 232 | default: break; |
| 233 | } |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | |
| 238 | size_t |
| 239 | GDBRemoteCommunicationClient::SendPacketAndWaitForResponse |
| 240 | ( |
| 241 | const char *payload, |
| 242 | StringExtractorGDBRemote &response, |
| 243 | bool send_async |
| 244 | ) |
| 245 | { |
| 246 | return SendPacketAndWaitForResponse (payload, |
| 247 | ::strlen (payload), |
| 248 | response, |
| 249 | send_async); |
| 250 | } |
| 251 | |
| 252 | size_t |
| 253 | GDBRemoteCommunicationClient::SendPacketAndWaitForResponse |
| 254 | ( |
| 255 | const char *payload, |
| 256 | size_t payload_length, |
| 257 | StringExtractorGDBRemote &response, |
| 258 | bool send_async |
| 259 | ) |
| 260 | { |
| 261 | Mutex::Locker locker; |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 262 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Greg Clayton | 801417e | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 263 | size_t response_len = 0; |
Greg Clayton | c8dd570 | 2012-04-12 19:04:34 +0000 | [diff] [blame] | 264 | if (GetSequenceMutex (locker)) |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 265 | { |
Greg Clayton | 139da72 | 2011-05-20 03:15:54 +0000 | [diff] [blame] | 266 | if (SendPacketNoLock (payload, payload_length)) |
Greg Clayton | 801417e | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 267 | response_len = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ()); |
| 268 | else |
| 269 | { |
| 270 | if (log) |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 271 | log->Printf("error: failed to send '%*s'", (int) payload_length, payload); |
Greg Clayton | 801417e | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 272 | } |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 273 | } |
| 274 | else |
| 275 | { |
| 276 | if (send_async) |
| 277 | { |
| 278 | Mutex::Locker async_locker (m_async_mutex); |
| 279 | m_async_packet.assign(payload, payload_length); |
| 280 | m_async_packet_predicate.SetValue (true, eBroadcastNever); |
| 281 | |
| 282 | if (log) |
| 283 | log->Printf ("async: async packet = %s", m_async_packet.c_str()); |
| 284 | |
| 285 | bool timed_out = false; |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 286 | if (SendInterrupt(locker, 2, timed_out)) |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 287 | { |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 288 | if (m_interrupt_sent) |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 289 | { |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 290 | TimeValue timeout_time; |
| 291 | timeout_time = TimeValue::Now(); |
| 292 | timeout_time.OffsetWithSeconds (m_packet_timeout); |
| 293 | |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 294 | if (log) |
| 295 | log->Printf ("async: sent interrupt"); |
Greg Clayton | 801417e | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 296 | |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 297 | if (m_async_packet_predicate.WaitForValueEqualTo (false, &timeout_time, &timed_out)) |
| 298 | { |
| 299 | if (log) |
| 300 | log->Printf ("async: got response"); |
Greg Clayton | 801417e | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 301 | |
| 302 | // Swap the response buffer to avoid malloc and string copy |
| 303 | response.GetStringRef().swap (m_async_response.GetStringRef()); |
| 304 | response_len = response.GetStringRef().size(); |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 305 | } |
| 306 | else |
| 307 | { |
| 308 | if (log) |
| 309 | log->Printf ("async: timed out waiting for response"); |
| 310 | } |
| 311 | |
| 312 | // Make sure we wait until the continue packet has been sent again... |
| 313 | if (m_private_is_running.WaitForValueEqualTo (true, &timeout_time, &timed_out)) |
| 314 | { |
Greg Clayton | b7669b3 | 2011-10-27 22:04:16 +0000 | [diff] [blame] | 315 | if (log) |
| 316 | { |
| 317 | if (timed_out) |
| 318 | log->Printf ("async: timed out waiting for process to resume, but process was resumed"); |
| 319 | else |
| 320 | log->Printf ("async: async packet sent"); |
| 321 | } |
| 322 | } |
| 323 | else |
| 324 | { |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 325 | if (log) |
| 326 | log->Printf ("async: timed out waiting for process to resume"); |
| 327 | } |
| 328 | } |
| 329 | else |
| 330 | { |
| 331 | // We had a racy condition where we went to send the interrupt |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 332 | // yet we were able to get the lock, so the process must have |
| 333 | // just stopped? |
Greg Clayton | 801417e | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 334 | if (log) |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 335 | log->Printf ("async: got lock without sending interrupt"); |
| 336 | // Send the packet normally since we got the lock |
| 337 | if (SendPacketNoLock (payload, payload_length)) |
| 338 | response_len = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ()); |
| 339 | else |
| 340 | { |
| 341 | if (log) |
| 342 | log->Printf("error: failed to send '%*s'", (int) payload_length, payload); |
| 343 | } |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | else |
| 347 | { |
| 348 | if (log) |
| 349 | log->Printf ("async: failed to interrupt"); |
| 350 | } |
| 351 | } |
| 352 | else |
| 353 | { |
| 354 | if (log) |
Greg Clayton | c8dd570 | 2012-04-12 19:04:34 +0000 | [diff] [blame] | 355 | log->Printf("error: failed to get packet sequence mutex, not sending packet '%*s'", (int) payload_length, payload); |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 356 | } |
| 357 | } |
Greg Clayton | 801417e | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 358 | if (response_len == 0) |
| 359 | { |
| 360 | if (log) |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 361 | log->Printf("error: failed to get response for '%*s'", (int) payload_length, payload); |
Greg Clayton | 801417e | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 362 | } |
| 363 | return response_len; |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 366 | StateType |
| 367 | GDBRemoteCommunicationClient::SendContinuePacketAndWaitForResponse |
| 368 | ( |
| 369 | ProcessGDBRemote *process, |
| 370 | const char *payload, |
| 371 | size_t packet_length, |
| 372 | StringExtractorGDBRemote &response |
| 373 | ) |
| 374 | { |
| 375 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
| 376 | if (log) |
| 377 | log->Printf ("GDBRemoteCommunicationClient::%s ()", __FUNCTION__); |
| 378 | |
| 379 | Mutex::Locker locker(m_sequence_mutex); |
| 380 | StateType state = eStateRunning; |
| 381 | |
| 382 | BroadcastEvent(eBroadcastBitRunPacketSent, NULL); |
| 383 | m_public_is_running.SetValue (true, eBroadcastNever); |
| 384 | // Set the starting continue packet into "continue_packet". This packet |
| 385 | // make change if we are interrupted and we continue after an async packet... |
| 386 | std::string continue_packet(payload, packet_length); |
| 387 | |
Greg Clayton | 628cead | 2011-05-19 03:54:16 +0000 | [diff] [blame] | 388 | bool got_stdout = false; |
| 389 | |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 390 | while (state == eStateRunning) |
| 391 | { |
Greg Clayton | 628cead | 2011-05-19 03:54:16 +0000 | [diff] [blame] | 392 | if (!got_stdout) |
| 393 | { |
| 394 | if (log) |
| 395 | log->Printf ("GDBRemoteCommunicationClient::%s () sending continue packet: %s", __FUNCTION__, continue_packet.c_str()); |
Greg Clayton | 516f084 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 396 | if (SendPacketNoLock(continue_packet.c_str(), continue_packet.size()) == 0) |
Greg Clayton | 628cead | 2011-05-19 03:54:16 +0000 | [diff] [blame] | 397 | state = eStateInvalid; |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 398 | |
Greg Clayton | b7669b3 | 2011-10-27 22:04:16 +0000 | [diff] [blame] | 399 | m_private_is_running.SetValue (true, eBroadcastAlways); |
Greg Clayton | 628cead | 2011-05-19 03:54:16 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | got_stdout = false; |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 403 | |
| 404 | if (log) |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 405 | log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(%s)", __FUNCTION__, continue_packet.c_str()); |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 406 | |
Greg Clayton | 516f084 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 407 | if (WaitForPacketWithTimeoutMicroSecondsNoLock(response, UINT32_MAX)) |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 408 | { |
| 409 | if (response.Empty()) |
| 410 | state = eStateInvalid; |
| 411 | else |
| 412 | { |
| 413 | const char stop_type = response.GetChar(); |
| 414 | if (log) |
| 415 | log->Printf ("GDBRemoteCommunicationClient::%s () got packet: %s", __FUNCTION__, response.GetStringRef().c_str()); |
| 416 | switch (stop_type) |
| 417 | { |
| 418 | case 'T': |
| 419 | case 'S': |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 420 | { |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 421 | if (process->GetStopID() == 0) |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 422 | { |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 423 | if (process->GetID() == LLDB_INVALID_PROCESS_ID) |
| 424 | { |
| 425 | lldb::pid_t pid = GetCurrentProcessID (); |
| 426 | if (pid != LLDB_INVALID_PROCESS_ID) |
| 427 | process->SetID (pid); |
| 428 | } |
| 429 | process->BuildDynamicRegisterInfo (true); |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 430 | } |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 431 | |
| 432 | // Privately notify any internal threads that we have stopped |
| 433 | // in case we wanted to interrupt our process, yet we might |
| 434 | // send a packet and continue without returning control to the |
| 435 | // user. |
| 436 | m_private_is_running.SetValue (false, eBroadcastAlways); |
| 437 | |
| 438 | const uint8_t signo = response.GetHexU8 (UINT8_MAX); |
| 439 | |
Greg Clayton | c2c822c | 2012-05-15 02:50:49 +0000 | [diff] [blame] | 440 | bool continue_after_async = false; |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 441 | if (m_async_signal != -1 || m_async_packet_predicate.GetValue()) |
| 442 | { |
Greg Clayton | c2c822c | 2012-05-15 02:50:49 +0000 | [diff] [blame] | 443 | continue_after_async = true; |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 444 | // We sent an interrupt packet to stop the inferior process |
| 445 | // for an async signal or to send an async packet while running |
| 446 | // but we might have been single stepping and received the |
| 447 | // stop packet for the step instead of for the interrupt packet. |
| 448 | // Typically when an interrupt is sent a SIGINT or SIGSTOP |
| 449 | // is used, so if we get anything else, we need to try and |
| 450 | // get another stop reply packet that may have been sent |
| 451 | // due to sending the interrupt when the target is stopped |
| 452 | // which will just re-send a copy of the last stop reply |
| 453 | // packet. If we don't do this, then the reply for our |
| 454 | // async packet will be the repeat stop reply packet and cause |
| 455 | // a lot of trouble for us! |
| 456 | if (signo != SIGINT && signo != SIGSTOP) |
| 457 | { |
Greg Clayton | c2c822c | 2012-05-15 02:50:49 +0000 | [diff] [blame] | 458 | continue_after_async = false; |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 459 | |
| 460 | // We didn't get a a SIGINT or SIGSTOP, so try for a |
| 461 | // very brief time (1 ms) to get another stop reply |
| 462 | // packet to make sure it doesn't get in the way |
| 463 | StringExtractorGDBRemote extra_stop_reply_packet; |
| 464 | uint32_t timeout_usec = 1000; |
| 465 | if (WaitForPacketWithTimeoutMicroSecondsNoLock (extra_stop_reply_packet, timeout_usec)) |
| 466 | { |
| 467 | switch (extra_stop_reply_packet.GetChar()) |
| 468 | { |
| 469 | case 'T': |
| 470 | case 'S': |
| 471 | // We did get an extra stop reply, which means |
| 472 | // our interrupt didn't stop the target so we |
| 473 | // shouldn't continue after the async signal |
| 474 | // or packet is sent... |
Greg Clayton | c2c822c | 2012-05-15 02:50:49 +0000 | [diff] [blame] | 475 | continue_after_async = false; |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 476 | break; |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | if (m_async_signal != -1) |
| 483 | { |
| 484 | if (log) |
| 485 | log->Printf ("async: send signo = %s", Host::GetSignalAsCString (m_async_signal)); |
| 486 | |
| 487 | // Save off the async signal we are supposed to send |
| 488 | const int async_signal = m_async_signal; |
| 489 | // Clear the async signal member so we don't end up |
| 490 | // sending the signal multiple times... |
| 491 | m_async_signal = -1; |
| 492 | // Check which signal we stopped with |
| 493 | if (signo == async_signal) |
| 494 | { |
| 495 | if (log) |
| 496 | log->Printf ("async: stopped with signal %s, we are done running", Host::GetSignalAsCString (signo)); |
| 497 | |
| 498 | // We already stopped with a signal that we wanted |
| 499 | // to stop with, so we are done |
| 500 | } |
| 501 | else |
| 502 | { |
| 503 | // We stopped with a different signal that the one |
| 504 | // we wanted to stop with, so now we must resume |
| 505 | // with the signal we want |
| 506 | char signal_packet[32]; |
| 507 | int signal_packet_len = 0; |
| 508 | signal_packet_len = ::snprintf (signal_packet, |
| 509 | sizeof (signal_packet), |
| 510 | "C%2.2x", |
| 511 | async_signal); |
| 512 | |
| 513 | if (log) |
| 514 | log->Printf ("async: stopped with signal %s, resume with %s", |
| 515 | Host::GetSignalAsCString (signo), |
| 516 | Host::GetSignalAsCString (async_signal)); |
| 517 | |
| 518 | // Set the continue packet to resume even if the |
Greg Clayton | c2c822c | 2012-05-15 02:50:49 +0000 | [diff] [blame] | 519 | // interrupt didn't cause our stop (ignore continue_after_async) |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 520 | continue_packet.assign(signal_packet, signal_packet_len); |
| 521 | continue; |
| 522 | } |
| 523 | } |
| 524 | else if (m_async_packet_predicate.GetValue()) |
| 525 | { |
| 526 | LogSP packet_log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); |
| 527 | |
| 528 | // We are supposed to send an asynchronous packet while |
| 529 | // we are running. |
| 530 | m_async_response.Clear(); |
| 531 | if (m_async_packet.empty()) |
| 532 | { |
| 533 | if (packet_log) |
| 534 | packet_log->Printf ("async: error: empty async packet"); |
| 535 | |
| 536 | } |
| 537 | else |
| 538 | { |
| 539 | if (packet_log) |
| 540 | packet_log->Printf ("async: sending packet"); |
| 541 | |
| 542 | SendPacketAndWaitForResponse (&m_async_packet[0], |
| 543 | m_async_packet.size(), |
| 544 | m_async_response, |
| 545 | false); |
| 546 | } |
| 547 | // Let the other thread that was trying to send the async |
| 548 | // packet know that the packet has been sent and response is |
| 549 | // ready... |
| 550 | m_async_packet_predicate.SetValue(false, eBroadcastAlways); |
| 551 | |
| 552 | if (packet_log) |
Greg Clayton | c2c822c | 2012-05-15 02:50:49 +0000 | [diff] [blame] | 553 | packet_log->Printf ("async: sent packet, continue_after_async = %i", continue_after_async); |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 554 | |
| 555 | // Set the continue packet to resume if our interrupt |
| 556 | // for the async packet did cause the stop |
Greg Clayton | c2c822c | 2012-05-15 02:50:49 +0000 | [diff] [blame] | 557 | if (continue_after_async) |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 558 | { |
Greg Clayton | c2c822c | 2012-05-15 02:50:49 +0000 | [diff] [blame] | 559 | //continue_packet.assign (1, 'c'); |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 560 | continue; |
| 561 | } |
| 562 | } |
| 563 | // Stop with signal and thread info |
| 564 | state = eStateStopped; |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 565 | } |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 566 | break; |
| 567 | |
| 568 | case 'W': |
| 569 | case 'X': |
| 570 | // process exited |
| 571 | state = eStateExited; |
| 572 | break; |
| 573 | |
| 574 | case 'O': |
| 575 | // STDOUT |
| 576 | { |
Greg Clayton | 628cead | 2011-05-19 03:54:16 +0000 | [diff] [blame] | 577 | got_stdout = true; |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 578 | std::string inferior_stdout; |
| 579 | inferior_stdout.reserve(response.GetBytesLeft () / 2); |
| 580 | char ch; |
| 581 | while ((ch = response.GetHexU8()) != '\0') |
| 582 | inferior_stdout.append(1, ch); |
| 583 | process->AppendSTDOUT (inferior_stdout.c_str(), inferior_stdout.size()); |
| 584 | } |
| 585 | break; |
| 586 | |
| 587 | case 'E': |
| 588 | // ERROR |
| 589 | state = eStateInvalid; |
| 590 | break; |
| 591 | |
| 592 | default: |
| 593 | if (log) |
| 594 | log->Printf ("GDBRemoteCommunicationClient::%s () unrecognized async packet", __FUNCTION__); |
| 595 | state = eStateInvalid; |
| 596 | break; |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | else |
| 601 | { |
| 602 | if (log) |
| 603 | log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(...) => false", __FUNCTION__); |
| 604 | state = eStateInvalid; |
| 605 | } |
| 606 | } |
| 607 | if (log) |
| 608 | log->Printf ("GDBRemoteCommunicationClient::%s () => %s", __FUNCTION__, StateAsCString(state)); |
| 609 | response.SetFilePos(0); |
| 610 | m_private_is_running.SetValue (false, eBroadcastAlways); |
| 611 | m_public_is_running.SetValue (false, eBroadcastAlways); |
| 612 | return state; |
| 613 | } |
| 614 | |
| 615 | bool |
| 616 | GDBRemoteCommunicationClient::SendAsyncSignal (int signo) |
| 617 | { |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 618 | Mutex::Locker async_locker (m_async_mutex); |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 619 | m_async_signal = signo; |
| 620 | bool timed_out = false; |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 621 | Mutex::Locker locker; |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 622 | if (SendInterrupt (locker, 1, timed_out)) |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 623 | return true; |
| 624 | m_async_signal = -1; |
| 625 | return false; |
| 626 | } |
| 627 | |
Greg Clayton | 516f084 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 628 | // This function takes a mutex locker as a parameter in case the GetSequenceMutex |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 629 | // actually succeeds. If it doesn't succeed in acquiring the sequence mutex |
| 630 | // (the expected result), then it will send the halt packet. If it does succeed |
| 631 | // then the caller that requested the interrupt will want to keep the sequence |
| 632 | // locked down so that no one else can send packets while the caller has control. |
| 633 | // This function usually gets called when we are running and need to stop the |
| 634 | // target. It can also be used when we are running and and we need to do something |
| 635 | // else (like read/write memory), so we need to interrupt the running process |
| 636 | // (gdb remote protocol requires this), and do what we need to do, then resume. |
| 637 | |
| 638 | bool |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 639 | GDBRemoteCommunicationClient::SendInterrupt |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 640 | ( |
| 641 | Mutex::Locker& locker, |
| 642 | uint32_t seconds_to_wait_for_stop, |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 643 | bool &timed_out |
| 644 | ) |
| 645 | { |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 646 | m_interrupt_sent = false; |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 647 | timed_out = false; |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 648 | LogSP log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS)); |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 649 | |
| 650 | if (IsRunning()) |
| 651 | { |
| 652 | // Only send an interrupt if our debugserver is running... |
Greg Clayton | c8dd570 | 2012-04-12 19:04:34 +0000 | [diff] [blame] | 653 | if (GetSequenceMutex (locker)) |
Greg Clayton | 516f084 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 654 | { |
| 655 | if (log) |
| 656 | log->Printf ("SendInterrupt () - got sequence mutex without having to interrupt"); |
| 657 | } |
| 658 | else |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 659 | { |
| 660 | // Someone has the mutex locked waiting for a response or for the |
| 661 | // inferior to stop, so send the interrupt on the down low... |
| 662 | char ctrl_c = '\x03'; |
| 663 | ConnectionStatus status = eConnectionStatusSuccess; |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 664 | size_t bytes_written = Write (&ctrl_c, 1, status, NULL); |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 665 | if (log) |
| 666 | log->PutCString("send packet: \\x03"); |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 667 | if (bytes_written > 0) |
| 668 | { |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 669 | m_interrupt_sent = true; |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 670 | if (seconds_to_wait_for_stop) |
| 671 | { |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 672 | TimeValue timeout; |
| 673 | if (seconds_to_wait_for_stop) |
| 674 | { |
| 675 | timeout = TimeValue::Now(); |
| 676 | timeout.OffsetWithSeconds (seconds_to_wait_for_stop); |
| 677 | } |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 678 | if (m_private_is_running.WaitForValueEqualTo (false, &timeout, &timed_out)) |
| 679 | { |
| 680 | if (log) |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 681 | log->PutCString ("SendInterrupt () - sent interrupt, private state stopped"); |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 682 | return true; |
| 683 | } |
| 684 | else |
| 685 | { |
| 686 | if (log) |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 687 | log->Printf ("SendInterrupt () - sent interrupt, timed out wating for async thread resume"); |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | else |
| 691 | { |
| 692 | if (log) |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 693 | log->Printf ("SendInterrupt () - sent interrupt, not waiting for stop..."); |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 694 | return true; |
| 695 | } |
| 696 | } |
| 697 | else |
| 698 | { |
| 699 | if (log) |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 700 | log->Printf ("SendInterrupt () - failed to write interrupt"); |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 701 | } |
| 702 | return false; |
| 703 | } |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 704 | } |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 705 | else |
| 706 | { |
| 707 | if (log) |
| 708 | log->Printf ("SendInterrupt () - not running"); |
| 709 | } |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 710 | return true; |
| 711 | } |
| 712 | |
| 713 | lldb::pid_t |
| 714 | GDBRemoteCommunicationClient::GetCurrentProcessID () |
| 715 | { |
| 716 | StringExtractorGDBRemote response; |
| 717 | if (SendPacketAndWaitForResponse("qC", strlen("qC"), response, false)) |
| 718 | { |
| 719 | if (response.GetChar() == 'Q') |
| 720 | if (response.GetChar() == 'C') |
| 721 | return response.GetHexMaxU32 (false, LLDB_INVALID_PROCESS_ID); |
| 722 | } |
| 723 | return LLDB_INVALID_PROCESS_ID; |
| 724 | } |
| 725 | |
| 726 | bool |
| 727 | GDBRemoteCommunicationClient::GetLaunchSuccess (std::string &error_str) |
| 728 | { |
| 729 | error_str.clear(); |
| 730 | StringExtractorGDBRemote response; |
| 731 | if (SendPacketAndWaitForResponse("qLaunchSuccess", strlen("qLaunchSuccess"), response, false)) |
| 732 | { |
| 733 | if (response.IsOKResponse()) |
| 734 | return true; |
| 735 | if (response.GetChar() == 'E') |
| 736 | { |
| 737 | // A string the describes what failed when launching... |
| 738 | error_str = response.GetStringRef().substr(1); |
| 739 | } |
| 740 | else |
| 741 | { |
| 742 | error_str.assign ("unknown error occurred launching process"); |
| 743 | } |
| 744 | } |
| 745 | else |
| 746 | { |
| 747 | error_str.assign ("failed to send the qLaunchSuccess packet"); |
| 748 | } |
| 749 | return false; |
| 750 | } |
| 751 | |
| 752 | int |
| 753 | GDBRemoteCommunicationClient::SendArgumentsPacket (char const *argv[]) |
| 754 | { |
| 755 | if (argv && argv[0]) |
| 756 | { |
| 757 | StreamString packet; |
| 758 | packet.PutChar('A'); |
| 759 | const char *arg; |
| 760 | for (uint32_t i = 0; (arg = argv[i]) != NULL; ++i) |
| 761 | { |
| 762 | const int arg_len = strlen(arg); |
| 763 | if (i > 0) |
| 764 | packet.PutChar(','); |
| 765 | packet.Printf("%i,%i,", arg_len * 2, i); |
| 766 | packet.PutBytesAsRawHex8 (arg, arg_len); |
| 767 | } |
| 768 | |
| 769 | StringExtractorGDBRemote response; |
| 770 | if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) |
| 771 | { |
| 772 | if (response.IsOKResponse()) |
| 773 | return 0; |
| 774 | uint8_t error = response.GetError(); |
| 775 | if (error) |
| 776 | return error; |
| 777 | } |
| 778 | } |
| 779 | return -1; |
| 780 | } |
| 781 | |
| 782 | int |
| 783 | GDBRemoteCommunicationClient::SendEnvironmentPacket (char const *name_equal_value) |
| 784 | { |
| 785 | if (name_equal_value && name_equal_value[0]) |
| 786 | { |
| 787 | StreamString packet; |
| 788 | packet.Printf("QEnvironment:%s", name_equal_value); |
| 789 | StringExtractorGDBRemote response; |
| 790 | if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) |
| 791 | { |
| 792 | if (response.IsOKResponse()) |
| 793 | return 0; |
| 794 | uint8_t error = response.GetError(); |
| 795 | if (error) |
| 796 | return error; |
| 797 | } |
| 798 | } |
| 799 | return -1; |
| 800 | } |
| 801 | |
Greg Clayton | a458240 | 2011-05-08 04:53:50 +0000 | [diff] [blame] | 802 | int |
| 803 | GDBRemoteCommunicationClient::SendLaunchArchPacket (char const *arch) |
| 804 | { |
| 805 | if (arch && arch[0]) |
| 806 | { |
| 807 | StreamString packet; |
| 808 | packet.Printf("QLaunchArch:%s", arch); |
| 809 | StringExtractorGDBRemote response; |
| 810 | if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) |
| 811 | { |
| 812 | if (response.IsOKResponse()) |
| 813 | return 0; |
| 814 | uint8_t error = response.GetError(); |
| 815 | if (error) |
| 816 | return error; |
| 817 | } |
| 818 | } |
| 819 | return -1; |
| 820 | } |
| 821 | |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 822 | bool |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 823 | GDBRemoteCommunicationClient::GetOSVersion (uint32_t &major, |
| 824 | uint32_t &minor, |
| 825 | uint32_t &update) |
| 826 | { |
| 827 | if (GetHostInfo ()) |
| 828 | { |
| 829 | if (m_os_version_major != UINT32_MAX) |
| 830 | { |
| 831 | major = m_os_version_major; |
| 832 | minor = m_os_version_minor; |
| 833 | update = m_os_version_update; |
| 834 | return true; |
| 835 | } |
| 836 | } |
| 837 | return false; |
| 838 | } |
| 839 | |
| 840 | bool |
| 841 | GDBRemoteCommunicationClient::GetOSBuildString (std::string &s) |
| 842 | { |
| 843 | if (GetHostInfo ()) |
| 844 | { |
| 845 | if (!m_os_build.empty()) |
| 846 | { |
| 847 | s = m_os_build; |
| 848 | return true; |
| 849 | } |
| 850 | } |
| 851 | s.clear(); |
| 852 | return false; |
| 853 | } |
| 854 | |
| 855 | |
| 856 | bool |
| 857 | GDBRemoteCommunicationClient::GetOSKernelDescription (std::string &s) |
| 858 | { |
| 859 | if (GetHostInfo ()) |
| 860 | { |
| 861 | if (!m_os_kernel.empty()) |
| 862 | { |
| 863 | s = m_os_kernel; |
| 864 | return true; |
| 865 | } |
| 866 | } |
| 867 | s.clear(); |
| 868 | return false; |
| 869 | } |
| 870 | |
| 871 | bool |
| 872 | GDBRemoteCommunicationClient::GetHostname (std::string &s) |
| 873 | { |
| 874 | if (GetHostInfo ()) |
| 875 | { |
| 876 | if (!m_hostname.empty()) |
| 877 | { |
| 878 | s = m_hostname; |
| 879 | return true; |
| 880 | } |
| 881 | } |
| 882 | s.clear(); |
| 883 | return false; |
| 884 | } |
| 885 | |
| 886 | ArchSpec |
| 887 | GDBRemoteCommunicationClient::GetSystemArchitecture () |
| 888 | { |
| 889 | if (GetHostInfo ()) |
| 890 | return m_host_arch; |
| 891 | return ArchSpec(); |
| 892 | } |
| 893 | |
| 894 | |
| 895 | bool |
Greg Clayton | 06d7cc8 | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 896 | GDBRemoteCommunicationClient::GetHostInfo (bool force) |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 897 | { |
Greg Clayton | 06d7cc8 | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 898 | if (force || m_qHostInfo_is_valid == eLazyBoolCalculate) |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 899 | { |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 900 | m_qHostInfo_is_valid = eLazyBoolNo; |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 901 | StringExtractorGDBRemote response; |
| 902 | if (SendPacketAndWaitForResponse ("qHostInfo", response, false)) |
| 903 | { |
Greg Clayton | 5b53e8e | 2011-05-15 23:46:54 +0000 | [diff] [blame] | 904 | if (response.IsNormalResponse()) |
Greg Clayton | cb8977d | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 905 | { |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 906 | std::string name; |
| 907 | std::string value; |
| 908 | uint32_t cpu = LLDB_INVALID_CPUTYPE; |
| 909 | uint32_t sub = 0; |
| 910 | std::string arch_name; |
| 911 | std::string os_name; |
| 912 | std::string vendor_name; |
| 913 | std::string triple; |
| 914 | uint32_t pointer_byte_size = 0; |
| 915 | StringExtractor extractor; |
| 916 | ByteOrder byte_order = eByteOrderInvalid; |
| 917 | uint32_t num_keys_decoded = 0; |
| 918 | while (response.GetNameColonValue(name, value)) |
Greg Clayton | cb8977d | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 919 | { |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 920 | if (name.compare("cputype") == 0) |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 921 | { |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 922 | // exception type in big endian hex |
| 923 | cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 0); |
| 924 | if (cpu != LLDB_INVALID_CPUTYPE) |
| 925 | ++num_keys_decoded; |
| 926 | } |
| 927 | else if (name.compare("cpusubtype") == 0) |
| 928 | { |
| 929 | // exception count in big endian hex |
| 930 | sub = Args::StringToUInt32 (value.c_str(), 0, 0); |
| 931 | if (sub != 0) |
| 932 | ++num_keys_decoded; |
| 933 | } |
| 934 | else if (name.compare("arch") == 0) |
| 935 | { |
| 936 | arch_name.swap (value); |
| 937 | ++num_keys_decoded; |
| 938 | } |
| 939 | else if (name.compare("triple") == 0) |
| 940 | { |
| 941 | // The triple comes as ASCII hex bytes since it contains '-' chars |
| 942 | extractor.GetStringRef().swap(value); |
| 943 | extractor.SetFilePos(0); |
| 944 | extractor.GetHexByteString (triple); |
| 945 | ++num_keys_decoded; |
| 946 | } |
| 947 | else if (name.compare("os_build") == 0) |
| 948 | { |
| 949 | extractor.GetStringRef().swap(value); |
| 950 | extractor.SetFilePos(0); |
| 951 | extractor.GetHexByteString (m_os_build); |
| 952 | ++num_keys_decoded; |
| 953 | } |
| 954 | else if (name.compare("hostname") == 0) |
| 955 | { |
| 956 | extractor.GetStringRef().swap(value); |
| 957 | extractor.SetFilePos(0); |
| 958 | extractor.GetHexByteString (m_hostname); |
| 959 | ++num_keys_decoded; |
| 960 | } |
| 961 | else if (name.compare("os_kernel") == 0) |
| 962 | { |
| 963 | extractor.GetStringRef().swap(value); |
| 964 | extractor.SetFilePos(0); |
| 965 | extractor.GetHexByteString (m_os_kernel); |
| 966 | ++num_keys_decoded; |
| 967 | } |
| 968 | else if (name.compare("ostype") == 0) |
| 969 | { |
| 970 | os_name.swap (value); |
| 971 | ++num_keys_decoded; |
| 972 | } |
| 973 | else if (name.compare("vendor") == 0) |
| 974 | { |
| 975 | vendor_name.swap(value); |
| 976 | ++num_keys_decoded; |
| 977 | } |
| 978 | else if (name.compare("endian") == 0) |
| 979 | { |
| 980 | ++num_keys_decoded; |
| 981 | if (value.compare("little") == 0) |
| 982 | byte_order = eByteOrderLittle; |
| 983 | else if (value.compare("big") == 0) |
| 984 | byte_order = eByteOrderBig; |
| 985 | else if (value.compare("pdp") == 0) |
| 986 | byte_order = eByteOrderPDP; |
| 987 | else |
| 988 | --num_keys_decoded; |
| 989 | } |
| 990 | else if (name.compare("ptrsize") == 0) |
| 991 | { |
| 992 | pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 0); |
| 993 | if (pointer_byte_size != 0) |
| 994 | ++num_keys_decoded; |
| 995 | } |
| 996 | else if (name.compare("os_version") == 0) |
| 997 | { |
| 998 | Args::StringToVersion (value.c_str(), |
| 999 | m_os_version_major, |
| 1000 | m_os_version_minor, |
| 1001 | m_os_version_update); |
| 1002 | if (m_os_version_major != UINT32_MAX) |
| 1003 | ++num_keys_decoded; |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | if (num_keys_decoded > 0) |
| 1008 | m_qHostInfo_is_valid = eLazyBoolYes; |
| 1009 | |
| 1010 | if (triple.empty()) |
| 1011 | { |
| 1012 | if (arch_name.empty()) |
| 1013 | { |
| 1014 | if (cpu != LLDB_INVALID_CPUTYPE) |
| 1015 | { |
| 1016 | m_host_arch.SetArchitecture (eArchTypeMachO, cpu, sub); |
| 1017 | if (pointer_byte_size) |
| 1018 | { |
| 1019 | assert (pointer_byte_size == m_host_arch.GetAddressByteSize()); |
| 1020 | } |
| 1021 | if (byte_order != eByteOrderInvalid) |
| 1022 | { |
| 1023 | assert (byte_order == m_host_arch.GetByteOrder()); |
| 1024 | } |
Greg Clayton | b170aee | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 1025 | |
| 1026 | if (!os_name.empty() && vendor_name.compare("apple") == 0 && os_name.find("darwin") == 0) |
| 1027 | { |
| 1028 | switch (m_host_arch.GetMachine()) |
| 1029 | { |
| 1030 | case llvm::Triple::arm: |
| 1031 | case llvm::Triple::thumb: |
| 1032 | os_name = "ios"; |
| 1033 | break; |
| 1034 | default: |
| 1035 | os_name = "macosx"; |
| 1036 | break; |
| 1037 | } |
| 1038 | } |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1039 | if (!vendor_name.empty()) |
| 1040 | m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name)); |
| 1041 | if (!os_name.empty()) |
Greg Clayton | 89798cc | 2011-09-15 00:21:03 +0000 | [diff] [blame] | 1042 | m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name)); |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1043 | |
| 1044 | } |
| 1045 | } |
| 1046 | else |
| 1047 | { |
| 1048 | std::string triple; |
| 1049 | triple += arch_name; |
Greg Clayton | b170aee | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 1050 | if (!vendor_name.empty() || !os_name.empty()) |
| 1051 | { |
| 1052 | triple += '-'; |
| 1053 | if (vendor_name.empty()) |
| 1054 | triple += "unknown"; |
| 1055 | else |
| 1056 | triple += vendor_name; |
| 1057 | triple += '-'; |
| 1058 | if (os_name.empty()) |
| 1059 | triple += "unknown"; |
| 1060 | else |
| 1061 | triple += os_name; |
| 1062 | } |
| 1063 | m_host_arch.SetTriple (triple.c_str()); |
| 1064 | |
| 1065 | llvm::Triple &host_triple = m_host_arch.GetTriple(); |
| 1066 | if (host_triple.getVendor() == llvm::Triple::Apple && host_triple.getOS() == llvm::Triple::Darwin) |
| 1067 | { |
| 1068 | switch (m_host_arch.GetMachine()) |
| 1069 | { |
| 1070 | case llvm::Triple::arm: |
| 1071 | case llvm::Triple::thumb: |
| 1072 | host_triple.setOS(llvm::Triple::IOS); |
| 1073 | break; |
| 1074 | default: |
| 1075 | host_triple.setOS(llvm::Triple::MacOSX); |
| 1076 | break; |
| 1077 | } |
| 1078 | } |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1079 | if (pointer_byte_size) |
| 1080 | { |
| 1081 | assert (pointer_byte_size == m_host_arch.GetAddressByteSize()); |
| 1082 | } |
| 1083 | if (byte_order != eByteOrderInvalid) |
| 1084 | { |
| 1085 | assert (byte_order == m_host_arch.GetByteOrder()); |
| 1086 | } |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1087 | |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1088 | } |
| 1089 | } |
| 1090 | else |
| 1091 | { |
Greg Clayton | b170aee | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 1092 | m_host_arch.SetTriple (triple.c_str()); |
Greg Clayton | cb8977d | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1093 | if (pointer_byte_size) |
| 1094 | { |
| 1095 | assert (pointer_byte_size == m_host_arch.GetAddressByteSize()); |
| 1096 | } |
| 1097 | if (byte_order != eByteOrderInvalid) |
| 1098 | { |
| 1099 | assert (byte_order == m_host_arch.GetByteOrder()); |
| 1100 | } |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1101 | } |
Greg Clayton | cb8977d | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1102 | } |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1103 | } |
| 1104 | } |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1105 | return m_qHostInfo_is_valid == eLazyBoolYes; |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | int |
| 1109 | GDBRemoteCommunicationClient::SendAttach |
| 1110 | ( |
| 1111 | lldb::pid_t pid, |
| 1112 | StringExtractorGDBRemote& response |
| 1113 | ) |
| 1114 | { |
| 1115 | if (pid != LLDB_INVALID_PROCESS_ID) |
| 1116 | { |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1117 | char packet[64]; |
Greg Clayton | d9919d3 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 1118 | const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%llx", pid); |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1119 | assert (packet_len < sizeof(packet)); |
| 1120 | if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1121 | { |
| 1122 | if (response.IsErrorResponse()) |
| 1123 | return response.GetError(); |
| 1124 | return 0; |
| 1125 | } |
| 1126 | } |
| 1127 | return -1; |
| 1128 | } |
| 1129 | |
| 1130 | const lldb_private::ArchSpec & |
| 1131 | GDBRemoteCommunicationClient::GetHostArchitecture () |
| 1132 | { |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1133 | if (m_qHostInfo_is_valid == eLazyBoolCalculate) |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1134 | GetHostInfo (); |
Greg Clayton | cb8977d | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1135 | return m_host_arch; |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | addr_t |
| 1139 | GDBRemoteCommunicationClient::AllocateMemory (size_t size, uint32_t permissions) |
| 1140 | { |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 1141 | if (m_supports_alloc_dealloc_memory != eLazyBoolNo) |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1142 | { |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 1143 | m_supports_alloc_dealloc_memory = eLazyBoolYes; |
Greg Clayton | 989816b | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 1144 | char packet[64]; |
| 1145 | const int packet_len = ::snprintf (packet, sizeof(packet), "_M%zx,%s%s%s", size, |
| 1146 | permissions & lldb::ePermissionsReadable ? "r" : "", |
| 1147 | permissions & lldb::ePermissionsWritable ? "w" : "", |
| 1148 | permissions & lldb::ePermissionsExecutable ? "x" : ""); |
| 1149 | assert (packet_len < sizeof(packet)); |
| 1150 | StringExtractorGDBRemote response; |
| 1151 | if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) |
| 1152 | { |
Greg Clayton | 5b53e8e | 2011-05-15 23:46:54 +0000 | [diff] [blame] | 1153 | if (!response.IsErrorResponse()) |
Greg Clayton | 989816b | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 1154 | return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS); |
| 1155 | } |
Greg Clayton | 5b53e8e | 2011-05-15 23:46:54 +0000 | [diff] [blame] | 1156 | else |
| 1157 | { |
| 1158 | m_supports_alloc_dealloc_memory = eLazyBoolNo; |
| 1159 | } |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1160 | } |
| 1161 | return LLDB_INVALID_ADDRESS; |
| 1162 | } |
| 1163 | |
| 1164 | bool |
| 1165 | GDBRemoteCommunicationClient::DeallocateMemory (addr_t addr) |
| 1166 | { |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 1167 | if (m_supports_alloc_dealloc_memory != eLazyBoolNo) |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1168 | { |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 1169 | m_supports_alloc_dealloc_memory = eLazyBoolYes; |
Greg Clayton | 989816b | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 1170 | char packet[64]; |
| 1171 | const int packet_len = ::snprintf(packet, sizeof(packet), "_m%llx", (uint64_t)addr); |
| 1172 | assert (packet_len < sizeof(packet)); |
| 1173 | StringExtractorGDBRemote response; |
| 1174 | if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) |
| 1175 | { |
| 1176 | if (response.IsOKResponse()) |
| 1177 | return true; |
Greg Clayton | 5b53e8e | 2011-05-15 23:46:54 +0000 | [diff] [blame] | 1178 | } |
| 1179 | else |
| 1180 | { |
| 1181 | m_supports_alloc_dealloc_memory = eLazyBoolNo; |
Greg Clayton | 989816b | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 1182 | } |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1183 | } |
| 1184 | return false; |
| 1185 | } |
| 1186 | |
Greg Clayton | 516f084 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 1187 | bool |
| 1188 | GDBRemoteCommunicationClient::Detach () |
| 1189 | { |
| 1190 | return SendPacket ("D", 1) > 0; |
| 1191 | } |
| 1192 | |
Greg Clayton | a938553 | 2011-11-18 07:03:08 +0000 | [diff] [blame] | 1193 | Error |
| 1194 | GDBRemoteCommunicationClient::GetMemoryRegionInfo (lldb::addr_t addr, |
| 1195 | lldb_private::MemoryRegionInfo ®ion_info) |
| 1196 | { |
| 1197 | Error error; |
| 1198 | region_info.Clear(); |
| 1199 | |
| 1200 | if (m_supports_memory_region_info != eLazyBoolNo) |
| 1201 | { |
| 1202 | m_supports_memory_region_info = eLazyBoolYes; |
| 1203 | char packet[64]; |
| 1204 | const int packet_len = ::snprintf(packet, sizeof(packet), "qMemoryRegionInfo:%llx", (uint64_t)addr); |
| 1205 | assert (packet_len < sizeof(packet)); |
| 1206 | StringExtractorGDBRemote response; |
| 1207 | if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) |
| 1208 | { |
| 1209 | std::string name; |
| 1210 | std::string value; |
| 1211 | addr_t addr_value; |
| 1212 | bool success = true; |
Jason Molenda | 1f9c39c | 2011-12-13 05:39:38 +0000 | [diff] [blame] | 1213 | bool saw_permissions = false; |
Greg Clayton | a938553 | 2011-11-18 07:03:08 +0000 | [diff] [blame] | 1214 | while (success && response.GetNameColonValue(name, value)) |
| 1215 | { |
| 1216 | if (name.compare ("start") == 0) |
| 1217 | { |
| 1218 | addr_value = Args::StringToUInt64(value.c_str(), LLDB_INVALID_ADDRESS, 16, &success); |
| 1219 | if (success) |
| 1220 | region_info.GetRange().SetRangeBase(addr_value); |
| 1221 | } |
| 1222 | else if (name.compare ("size") == 0) |
| 1223 | { |
| 1224 | addr_value = Args::StringToUInt64(value.c_str(), 0, 16, &success); |
| 1225 | if (success) |
| 1226 | region_info.GetRange().SetByteSize (addr_value); |
| 1227 | } |
Jason Molenda | 1f9c39c | 2011-12-13 05:39:38 +0000 | [diff] [blame] | 1228 | else if (name.compare ("permissions") == 0 && region_info.GetRange().IsValid()) |
Greg Clayton | a938553 | 2011-11-18 07:03:08 +0000 | [diff] [blame] | 1229 | { |
Jason Molenda | 1f9c39c | 2011-12-13 05:39:38 +0000 | [diff] [blame] | 1230 | saw_permissions = true; |
| 1231 | if (region_info.GetRange().Contains (addr)) |
| 1232 | { |
| 1233 | if (value.find('r') != std::string::npos) |
| 1234 | region_info.SetReadable (MemoryRegionInfo::eYes); |
| 1235 | else |
| 1236 | region_info.SetReadable (MemoryRegionInfo::eNo); |
| 1237 | |
| 1238 | if (value.find('w') != std::string::npos) |
| 1239 | region_info.SetWritable (MemoryRegionInfo::eYes); |
| 1240 | else |
| 1241 | region_info.SetWritable (MemoryRegionInfo::eNo); |
| 1242 | |
| 1243 | if (value.find('x') != std::string::npos) |
| 1244 | region_info.SetExecutable (MemoryRegionInfo::eYes); |
| 1245 | else |
| 1246 | region_info.SetExecutable (MemoryRegionInfo::eNo); |
| 1247 | } |
| 1248 | else |
| 1249 | { |
| 1250 | // The reported region does not contain this address -- we're looking at an unmapped page |
| 1251 | region_info.SetReadable (MemoryRegionInfo::eNo); |
| 1252 | region_info.SetWritable (MemoryRegionInfo::eNo); |
| 1253 | region_info.SetExecutable (MemoryRegionInfo::eNo); |
| 1254 | } |
Greg Clayton | a938553 | 2011-11-18 07:03:08 +0000 | [diff] [blame] | 1255 | } |
| 1256 | else if (name.compare ("error") == 0) |
| 1257 | { |
| 1258 | StringExtractorGDBRemote name_extractor; |
| 1259 | // Swap "value" over into "name_extractor" |
| 1260 | name_extractor.GetStringRef().swap(value); |
| 1261 | // Now convert the HEX bytes into a string value |
| 1262 | name_extractor.GetHexByteString (value); |
| 1263 | error.SetErrorString(value.c_str()); |
| 1264 | } |
| 1265 | } |
Jason Molenda | 1f9c39c | 2011-12-13 05:39:38 +0000 | [diff] [blame] | 1266 | |
| 1267 | // We got a valid address range back but no permissions -- which means this is an unmapped page |
| 1268 | if (region_info.GetRange().IsValid() && saw_permissions == false) |
| 1269 | { |
| 1270 | region_info.SetReadable (MemoryRegionInfo::eNo); |
| 1271 | region_info.SetWritable (MemoryRegionInfo::eNo); |
| 1272 | region_info.SetExecutable (MemoryRegionInfo::eNo); |
| 1273 | } |
Greg Clayton | a938553 | 2011-11-18 07:03:08 +0000 | [diff] [blame] | 1274 | } |
| 1275 | else |
| 1276 | { |
| 1277 | m_supports_memory_region_info = eLazyBoolNo; |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | if (m_supports_memory_region_info == eLazyBoolNo) |
| 1282 | { |
| 1283 | error.SetErrorString("qMemoryRegionInfo is not supported"); |
| 1284 | } |
| 1285 | if (error.Fail()) |
| 1286 | region_info.Clear(); |
| 1287 | return error; |
| 1288 | |
| 1289 | } |
| 1290 | |
Johnny Chen | 7cbdcfb | 2012-05-23 21:09:52 +0000 | [diff] [blame^] | 1291 | Error |
| 1292 | GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num) |
| 1293 | { |
| 1294 | Error error; |
| 1295 | |
| 1296 | if (m_supports_watchpoint_support_info == eLazyBoolYes) |
| 1297 | { |
| 1298 | num = m_num_supported_hardware_watchpoints; |
| 1299 | return error; |
| 1300 | } |
| 1301 | |
| 1302 | // Set num to 0 first. |
| 1303 | num = 0; |
| 1304 | if (m_supports_watchpoint_support_info != eLazyBoolNo) |
| 1305 | { |
| 1306 | char packet[64]; |
| 1307 | const int packet_len = ::snprintf(packet, sizeof(packet), "qWatchpointSupportInfo:"); |
| 1308 | assert (packet_len < sizeof(packet)); |
| 1309 | StringExtractorGDBRemote response; |
| 1310 | if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) |
| 1311 | { |
| 1312 | m_supports_watchpoint_support_info = eLazyBoolYes; |
| 1313 | std::string name; |
| 1314 | std::string value; |
| 1315 | while (response.GetNameColonValue(name, value)) |
| 1316 | { |
| 1317 | if (name.compare ("num") == 0) |
| 1318 | { |
| 1319 | num = Args::StringToUInt32(value.c_str(), 0, 0); |
| 1320 | m_num_supported_hardware_watchpoints = num; |
| 1321 | } |
| 1322 | } |
| 1323 | } |
| 1324 | else |
| 1325 | { |
| 1326 | m_supports_watchpoint_support_info = eLazyBoolNo; |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | if (m_supports_watchpoint_support_info == eLazyBoolNo) |
| 1331 | { |
| 1332 | error.SetErrorString("qWatchpointSupportInfo is not supported"); |
| 1333 | } |
| 1334 | return error; |
| 1335 | |
| 1336 | } |
Greg Clayton | a938553 | 2011-11-18 07:03:08 +0000 | [diff] [blame] | 1337 | |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1338 | int |
| 1339 | GDBRemoteCommunicationClient::SetSTDIN (char const *path) |
| 1340 | { |
| 1341 | if (path && path[0]) |
| 1342 | { |
| 1343 | StreamString packet; |
| 1344 | packet.PutCString("QSetSTDIN:"); |
| 1345 | packet.PutBytesAsRawHex8(path, strlen(path)); |
| 1346 | |
| 1347 | StringExtractorGDBRemote response; |
| 1348 | if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) |
| 1349 | { |
| 1350 | if (response.IsOKResponse()) |
| 1351 | return 0; |
| 1352 | uint8_t error = response.GetError(); |
| 1353 | if (error) |
| 1354 | return error; |
| 1355 | } |
| 1356 | } |
| 1357 | return -1; |
| 1358 | } |
| 1359 | |
| 1360 | int |
| 1361 | GDBRemoteCommunicationClient::SetSTDOUT (char const *path) |
| 1362 | { |
| 1363 | if (path && path[0]) |
| 1364 | { |
| 1365 | StreamString packet; |
| 1366 | packet.PutCString("QSetSTDOUT:"); |
| 1367 | packet.PutBytesAsRawHex8(path, strlen(path)); |
| 1368 | |
| 1369 | StringExtractorGDBRemote response; |
| 1370 | if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) |
| 1371 | { |
| 1372 | if (response.IsOKResponse()) |
| 1373 | return 0; |
| 1374 | uint8_t error = response.GetError(); |
| 1375 | if (error) |
| 1376 | return error; |
| 1377 | } |
| 1378 | } |
| 1379 | return -1; |
| 1380 | } |
| 1381 | |
| 1382 | int |
| 1383 | GDBRemoteCommunicationClient::SetSTDERR (char const *path) |
| 1384 | { |
| 1385 | if (path && path[0]) |
| 1386 | { |
| 1387 | StreamString packet; |
| 1388 | packet.PutCString("QSetSTDERR:"); |
| 1389 | packet.PutBytesAsRawHex8(path, strlen(path)); |
| 1390 | |
| 1391 | StringExtractorGDBRemote response; |
| 1392 | if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) |
| 1393 | { |
| 1394 | if (response.IsOKResponse()) |
| 1395 | return 0; |
| 1396 | uint8_t error = response.GetError(); |
| 1397 | if (error) |
| 1398 | return error; |
| 1399 | } |
| 1400 | } |
| 1401 | return -1; |
| 1402 | } |
| 1403 | |
| 1404 | int |
| 1405 | GDBRemoteCommunicationClient::SetWorkingDir (char const *path) |
| 1406 | { |
| 1407 | if (path && path[0]) |
| 1408 | { |
| 1409 | StreamString packet; |
| 1410 | packet.PutCString("QSetWorkingDir:"); |
| 1411 | packet.PutBytesAsRawHex8(path, strlen(path)); |
| 1412 | |
| 1413 | StringExtractorGDBRemote response; |
| 1414 | if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) |
| 1415 | { |
| 1416 | if (response.IsOKResponse()) |
| 1417 | return 0; |
| 1418 | uint8_t error = response.GetError(); |
| 1419 | if (error) |
| 1420 | return error; |
| 1421 | } |
| 1422 | } |
| 1423 | return -1; |
| 1424 | } |
| 1425 | |
| 1426 | int |
| 1427 | GDBRemoteCommunicationClient::SetDisableASLR (bool enable) |
| 1428 | { |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1429 | char packet[32]; |
| 1430 | const int packet_len = ::snprintf (packet, sizeof (packet), "QSetDisableASLR:%i", enable ? 1 : 0); |
| 1431 | assert (packet_len < sizeof(packet)); |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1432 | StringExtractorGDBRemote response; |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1433 | if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1434 | { |
| 1435 | if (response.IsOKResponse()) |
| 1436 | return 0; |
| 1437 | uint8_t error = response.GetError(); |
| 1438 | if (error) |
| 1439 | return error; |
| 1440 | } |
| 1441 | return -1; |
| 1442 | } |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1443 | |
| 1444 | bool |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1445 | GDBRemoteCommunicationClient::DecodeProcessInfoResponse (StringExtractorGDBRemote &response, ProcessInstanceInfo &process_info) |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1446 | { |
| 1447 | if (response.IsNormalResponse()) |
| 1448 | { |
| 1449 | std::string name; |
| 1450 | std::string value; |
| 1451 | StringExtractor extractor; |
| 1452 | |
| 1453 | while (response.GetNameColonValue(name, value)) |
| 1454 | { |
| 1455 | if (name.compare("pid") == 0) |
| 1456 | { |
| 1457 | process_info.SetProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0)); |
| 1458 | } |
| 1459 | else if (name.compare("ppid") == 0) |
| 1460 | { |
| 1461 | process_info.SetParentProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0)); |
| 1462 | } |
| 1463 | else if (name.compare("uid") == 0) |
| 1464 | { |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1465 | process_info.SetUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0)); |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1466 | } |
| 1467 | else if (name.compare("euid") == 0) |
| 1468 | { |
| 1469 | process_info.SetEffectiveUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0)); |
| 1470 | } |
| 1471 | else if (name.compare("gid") == 0) |
| 1472 | { |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1473 | process_info.SetGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0)); |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1474 | } |
| 1475 | else if (name.compare("egid") == 0) |
| 1476 | { |
| 1477 | process_info.SetEffectiveGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0)); |
| 1478 | } |
| 1479 | else if (name.compare("triple") == 0) |
| 1480 | { |
| 1481 | // The triple comes as ASCII hex bytes since it contains '-' chars |
| 1482 | extractor.GetStringRef().swap(value); |
| 1483 | extractor.SetFilePos(0); |
| 1484 | extractor.GetHexByteString (value); |
Greg Clayton | b170aee | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 1485 | process_info.GetArchitecture ().SetTriple (value.c_str()); |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1486 | } |
| 1487 | else if (name.compare("name") == 0) |
| 1488 | { |
| 1489 | StringExtractor extractor; |
Filipe Cabecinhas | e61ec7f | 2012-05-07 09:30:51 +0000 | [diff] [blame] | 1490 | // The process name from ASCII hex bytes since we can't |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1491 | // control the characters in a process name |
| 1492 | extractor.GetStringRef().swap(value); |
| 1493 | extractor.SetFilePos(0); |
| 1494 | extractor.GetHexByteString (value); |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 1495 | process_info.GetExecutableFile().SetFile (value.c_str(), false); |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | if (process_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) |
| 1500 | return true; |
| 1501 | } |
| 1502 | return false; |
| 1503 | } |
| 1504 | |
| 1505 | bool |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1506 | GDBRemoteCommunicationClient::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1507 | { |
| 1508 | process_info.Clear(); |
| 1509 | |
| 1510 | if (m_supports_qProcessInfoPID) |
| 1511 | { |
| 1512 | char packet[32]; |
Greg Clayton | d9919d3 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 1513 | const int packet_len = ::snprintf (packet, sizeof (packet), "qProcessInfoPID:%llu", pid); |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1514 | assert (packet_len < sizeof(packet)); |
| 1515 | StringExtractorGDBRemote response; |
| 1516 | if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) |
| 1517 | { |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1518 | return DecodeProcessInfoResponse (response, process_info); |
| 1519 | } |
Greg Clayton | 5b53e8e | 2011-05-15 23:46:54 +0000 | [diff] [blame] | 1520 | else |
| 1521 | { |
| 1522 | m_supports_qProcessInfoPID = false; |
| 1523 | return false; |
| 1524 | } |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1525 | } |
| 1526 | return false; |
| 1527 | } |
| 1528 | |
| 1529 | uint32_t |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1530 | GDBRemoteCommunicationClient::FindProcesses (const ProcessInstanceInfoMatch &match_info, |
| 1531 | ProcessInstanceInfoList &process_infos) |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1532 | { |
| 1533 | process_infos.Clear(); |
| 1534 | |
| 1535 | if (m_supports_qfProcessInfo) |
| 1536 | { |
| 1537 | StreamString packet; |
| 1538 | packet.PutCString ("qfProcessInfo"); |
| 1539 | if (!match_info.MatchAllProcesses()) |
| 1540 | { |
| 1541 | packet.PutChar (':'); |
| 1542 | const char *name = match_info.GetProcessInfo().GetName(); |
| 1543 | bool has_name_match = false; |
| 1544 | if (name && name[0]) |
| 1545 | { |
| 1546 | has_name_match = true; |
| 1547 | NameMatchType name_match_type = match_info.GetNameMatchType(); |
| 1548 | switch (name_match_type) |
| 1549 | { |
| 1550 | case eNameMatchIgnore: |
| 1551 | has_name_match = false; |
| 1552 | break; |
| 1553 | |
| 1554 | case eNameMatchEquals: |
| 1555 | packet.PutCString ("name_match:equals;"); |
| 1556 | break; |
| 1557 | |
| 1558 | case eNameMatchContains: |
| 1559 | packet.PutCString ("name_match:contains;"); |
| 1560 | break; |
| 1561 | |
| 1562 | case eNameMatchStartsWith: |
| 1563 | packet.PutCString ("name_match:starts_with;"); |
| 1564 | break; |
| 1565 | |
| 1566 | case eNameMatchEndsWith: |
| 1567 | packet.PutCString ("name_match:ends_with;"); |
| 1568 | break; |
| 1569 | |
| 1570 | case eNameMatchRegularExpression: |
| 1571 | packet.PutCString ("name_match:regex;"); |
| 1572 | break; |
| 1573 | } |
| 1574 | if (has_name_match) |
| 1575 | { |
| 1576 | packet.PutCString ("name:"); |
| 1577 | packet.PutBytesAsRawHex8(name, ::strlen(name)); |
| 1578 | packet.PutChar (';'); |
| 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | if (match_info.GetProcessInfo().ProcessIDIsValid()) |
Greg Clayton | d9919d3 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 1583 | packet.Printf("pid:%llu;",match_info.GetProcessInfo().GetProcessID()); |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1584 | if (match_info.GetProcessInfo().ParentProcessIDIsValid()) |
Greg Clayton | d9919d3 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 1585 | packet.Printf("parent_pid:%llu;",match_info.GetProcessInfo().GetParentProcessID()); |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1586 | if (match_info.GetProcessInfo().UserIDIsValid()) |
| 1587 | packet.Printf("uid:%u;",match_info.GetProcessInfo().GetUserID()); |
| 1588 | if (match_info.GetProcessInfo().GroupIDIsValid()) |
| 1589 | packet.Printf("gid:%u;",match_info.GetProcessInfo().GetGroupID()); |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1590 | if (match_info.GetProcessInfo().EffectiveUserIDIsValid()) |
| 1591 | packet.Printf("euid:%u;",match_info.GetProcessInfo().GetEffectiveUserID()); |
| 1592 | if (match_info.GetProcessInfo().EffectiveGroupIDIsValid()) |
| 1593 | packet.Printf("egid:%u;",match_info.GetProcessInfo().GetEffectiveGroupID()); |
| 1594 | if (match_info.GetProcessInfo().EffectiveGroupIDIsValid()) |
| 1595 | packet.Printf("all_users:%u;",match_info.GetMatchAllUsers() ? 1 : 0); |
| 1596 | if (match_info.GetProcessInfo().GetArchitecture().IsValid()) |
| 1597 | { |
| 1598 | const ArchSpec &match_arch = match_info.GetProcessInfo().GetArchitecture(); |
| 1599 | const llvm::Triple &triple = match_arch.GetTriple(); |
| 1600 | packet.PutCString("triple:"); |
| 1601 | packet.PutCStringAsRawHex8(triple.getTriple().c_str()); |
| 1602 | packet.PutChar (';'); |
| 1603 | } |
| 1604 | } |
| 1605 | StringExtractorGDBRemote response; |
| 1606 | if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) |
| 1607 | { |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1608 | do |
| 1609 | { |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1610 | ProcessInstanceInfo process_info; |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1611 | if (!DecodeProcessInfoResponse (response, process_info)) |
| 1612 | break; |
| 1613 | process_infos.Append(process_info); |
| 1614 | response.GetStringRef().clear(); |
| 1615 | response.SetFilePos(0); |
| 1616 | } while (SendPacketAndWaitForResponse ("qsProcessInfo", strlen ("qsProcessInfo"), response, false)); |
| 1617 | } |
Greg Clayton | 5b53e8e | 2011-05-15 23:46:54 +0000 | [diff] [blame] | 1618 | else |
| 1619 | { |
| 1620 | m_supports_qfProcessInfo = false; |
| 1621 | return 0; |
| 1622 | } |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1623 | } |
| 1624 | return process_infos.GetSize(); |
| 1625 | |
| 1626 | } |
| 1627 | |
| 1628 | bool |
| 1629 | GDBRemoteCommunicationClient::GetUserName (uint32_t uid, std::string &name) |
| 1630 | { |
| 1631 | if (m_supports_qUserName) |
| 1632 | { |
| 1633 | char packet[32]; |
| 1634 | const int packet_len = ::snprintf (packet, sizeof (packet), "qUserName:%i", uid); |
| 1635 | assert (packet_len < sizeof(packet)); |
| 1636 | StringExtractorGDBRemote response; |
| 1637 | if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) |
| 1638 | { |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1639 | if (response.IsNormalResponse()) |
| 1640 | { |
| 1641 | // Make sure we parsed the right number of characters. The response is |
| 1642 | // the hex encoded user name and should make up the entire packet. |
| 1643 | // If there are any non-hex ASCII bytes, the length won't match below.. |
| 1644 | if (response.GetHexByteString (name) * 2 == response.GetStringRef().size()) |
| 1645 | return true; |
| 1646 | } |
| 1647 | } |
Greg Clayton | 5b53e8e | 2011-05-15 23:46:54 +0000 | [diff] [blame] | 1648 | else |
| 1649 | { |
| 1650 | m_supports_qUserName = false; |
| 1651 | return false; |
| 1652 | } |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1653 | } |
| 1654 | return false; |
| 1655 | |
| 1656 | } |
| 1657 | |
| 1658 | bool |
| 1659 | GDBRemoteCommunicationClient::GetGroupName (uint32_t gid, std::string &name) |
| 1660 | { |
| 1661 | if (m_supports_qGroupName) |
| 1662 | { |
| 1663 | char packet[32]; |
| 1664 | const int packet_len = ::snprintf (packet, sizeof (packet), "qGroupName:%i", gid); |
| 1665 | assert (packet_len < sizeof(packet)); |
| 1666 | StringExtractorGDBRemote response; |
| 1667 | if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) |
| 1668 | { |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1669 | if (response.IsNormalResponse()) |
| 1670 | { |
| 1671 | // Make sure we parsed the right number of characters. The response is |
| 1672 | // the hex encoded group name and should make up the entire packet. |
| 1673 | // If there are any non-hex ASCII bytes, the length won't match below.. |
| 1674 | if (response.GetHexByteString (name) * 2 == response.GetStringRef().size()) |
| 1675 | return true; |
| 1676 | } |
| 1677 | } |
Greg Clayton | 5b53e8e | 2011-05-15 23:46:54 +0000 | [diff] [blame] | 1678 | else |
| 1679 | { |
| 1680 | m_supports_qGroupName = false; |
| 1681 | return false; |
| 1682 | } |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1683 | } |
| 1684 | return false; |
Greg Clayton | 06d7cc8 | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 1685 | } |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1686 | |
Greg Clayton | 06d7cc8 | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 1687 | void |
| 1688 | GDBRemoteCommunicationClient::TestPacketSpeed (const uint32_t num_packets) |
| 1689 | { |
| 1690 | uint32_t i; |
| 1691 | TimeValue start_time, end_time; |
| 1692 | uint64_t total_time_nsec; |
| 1693 | float packets_per_second; |
| 1694 | if (SendSpeedTestPacket (0, 0)) |
| 1695 | { |
| 1696 | for (uint32_t send_size = 0; send_size <= 1024; send_size *= 2) |
| 1697 | { |
| 1698 | for (uint32_t recv_size = 0; recv_size <= 1024; recv_size *= 2) |
| 1699 | { |
| 1700 | start_time = TimeValue::Now(); |
| 1701 | for (i=0; i<num_packets; ++i) |
| 1702 | { |
| 1703 | SendSpeedTestPacket (send_size, recv_size); |
| 1704 | } |
| 1705 | end_time = TimeValue::Now(); |
| 1706 | total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970(); |
Peter Collingbourne | 20fe30c | 2011-06-18 23:52:14 +0000 | [diff] [blame] | 1707 | packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec; |
Greg Clayton | 153ccd7 | 2011-08-10 02:10:13 +0000 | [diff] [blame] | 1708 | printf ("%u qSpeedTest(send=%-5u, recv=%-5u) in %llu.%9.9llu sec for %f packets/sec.\n", |
Greg Clayton | 06d7cc8 | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 1709 | num_packets, |
| 1710 | send_size, |
| 1711 | recv_size, |
Peter Collingbourne | 20fe30c | 2011-06-18 23:52:14 +0000 | [diff] [blame] | 1712 | total_time_nsec / TimeValue::NanoSecPerSec, |
| 1713 | total_time_nsec % TimeValue::NanoSecPerSec, |
Greg Clayton | 06d7cc8 | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 1714 | packets_per_second); |
| 1715 | if (recv_size == 0) |
| 1716 | recv_size = 32; |
| 1717 | } |
| 1718 | if (send_size == 0) |
| 1719 | send_size = 32; |
| 1720 | } |
| 1721 | } |
| 1722 | else |
| 1723 | { |
| 1724 | start_time = TimeValue::Now(); |
| 1725 | for (i=0; i<num_packets; ++i) |
| 1726 | { |
| 1727 | GetCurrentProcessID (); |
| 1728 | } |
| 1729 | end_time = TimeValue::Now(); |
| 1730 | total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970(); |
Peter Collingbourne | 20fe30c | 2011-06-18 23:52:14 +0000 | [diff] [blame] | 1731 | packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec; |
Greg Clayton | 153ccd7 | 2011-08-10 02:10:13 +0000 | [diff] [blame] | 1732 | printf ("%u 'qC' packets packets in 0x%llu%9.9llu sec for %f packets/sec.\n", |
Greg Clayton | 06d7cc8 | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 1733 | num_packets, |
Peter Collingbourne | 20fe30c | 2011-06-18 23:52:14 +0000 | [diff] [blame] | 1734 | total_time_nsec / TimeValue::NanoSecPerSec, |
| 1735 | total_time_nsec % TimeValue::NanoSecPerSec, |
Greg Clayton | 06d7cc8 | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 1736 | packets_per_second); |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | bool |
| 1741 | GDBRemoteCommunicationClient::SendSpeedTestPacket (uint32_t send_size, uint32_t recv_size) |
| 1742 | { |
| 1743 | StreamString packet; |
| 1744 | packet.Printf ("qSpeedTest:response_size:%i;data:", recv_size); |
| 1745 | uint32_t bytes_left = send_size; |
| 1746 | while (bytes_left > 0) |
| 1747 | { |
| 1748 | if (bytes_left >= 26) |
| 1749 | { |
| 1750 | packet.PutCString("abcdefghijklmnopqrstuvwxyz"); |
| 1751 | bytes_left -= 26; |
| 1752 | } |
| 1753 | else |
| 1754 | { |
| 1755 | packet.Printf ("%*.*s;", bytes_left, bytes_left, "abcdefghijklmnopqrstuvwxyz"); |
| 1756 | bytes_left = 0; |
| 1757 | } |
| 1758 | } |
| 1759 | |
| 1760 | StringExtractorGDBRemote response; |
Greg Clayton | 5b53e8e | 2011-05-15 23:46:54 +0000 | [diff] [blame] | 1761 | return SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) > 0; |
Greg Clayton | 06d7cc8 | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 1762 | return false; |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1763 | } |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1764 | |
| 1765 | uint16_t |
| 1766 | GDBRemoteCommunicationClient::LaunchGDBserverAndGetPort () |
| 1767 | { |
| 1768 | StringExtractorGDBRemote response; |
| 1769 | if (SendPacketAndWaitForResponse("qLaunchGDBServer", strlen("qLaunchGDBServer"), response, false)) |
| 1770 | { |
| 1771 | std::string name; |
| 1772 | std::string value; |
| 1773 | uint16_t port = 0; |
| 1774 | lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; |
| 1775 | while (response.GetNameColonValue(name, value)) |
| 1776 | { |
| 1777 | if (name.size() == 4 && name.compare("port") == 0) |
| 1778 | port = Args::StringToUInt32(value.c_str(), 0, 0); |
| 1779 | if (name.size() == 3 && name.compare("pid") == 0) |
| 1780 | pid = Args::StringToUInt32(value.c_str(), LLDB_INVALID_PROCESS_ID, 0); |
| 1781 | } |
| 1782 | return port; |
| 1783 | } |
| 1784 | return 0; |
| 1785 | } |
| 1786 | |
| 1787 | bool |
| 1788 | GDBRemoteCommunicationClient::SetCurrentThread (int tid) |
| 1789 | { |
| 1790 | if (m_curr_tid == tid) |
| 1791 | return true; |
| 1792 | |
| 1793 | char packet[32]; |
| 1794 | int packet_len; |
| 1795 | if (tid <= 0) |
| 1796 | packet_len = ::snprintf (packet, sizeof(packet), "Hg%i", tid); |
| 1797 | else |
| 1798 | packet_len = ::snprintf (packet, sizeof(packet), "Hg%x", tid); |
| 1799 | assert (packet_len + 1 < sizeof(packet)); |
| 1800 | StringExtractorGDBRemote response; |
| 1801 | if (SendPacketAndWaitForResponse(packet, packet_len, response, false)) |
| 1802 | { |
| 1803 | if (response.IsOKResponse()) |
| 1804 | { |
| 1805 | m_curr_tid = tid; |
| 1806 | return true; |
| 1807 | } |
| 1808 | } |
| 1809 | return false; |
| 1810 | } |
| 1811 | |
| 1812 | bool |
| 1813 | GDBRemoteCommunicationClient::SetCurrentThreadForRun (int tid) |
| 1814 | { |
| 1815 | if (m_curr_tid_run == tid) |
| 1816 | return true; |
| 1817 | |
| 1818 | char packet[32]; |
| 1819 | int packet_len; |
| 1820 | if (tid <= 0) |
| 1821 | packet_len = ::snprintf (packet, sizeof(packet), "Hc%i", tid); |
| 1822 | else |
| 1823 | packet_len = ::snprintf (packet, sizeof(packet), "Hc%x", tid); |
| 1824 | |
| 1825 | assert (packet_len + 1 < sizeof(packet)); |
| 1826 | StringExtractorGDBRemote response; |
| 1827 | if (SendPacketAndWaitForResponse(packet, packet_len, response, false)) |
| 1828 | { |
| 1829 | if (response.IsOKResponse()) |
| 1830 | { |
| 1831 | m_curr_tid_run = tid; |
| 1832 | return true; |
| 1833 | } |
| 1834 | } |
| 1835 | return false; |
| 1836 | } |
| 1837 | |
| 1838 | bool |
| 1839 | GDBRemoteCommunicationClient::GetStopReply (StringExtractorGDBRemote &response) |
| 1840 | { |
| 1841 | if (SendPacketAndWaitForResponse("?", 1, response, false)) |
| 1842 | return response.IsNormalResponse(); |
| 1843 | return false; |
| 1844 | } |
| 1845 | |
| 1846 | bool |
| 1847 | GDBRemoteCommunicationClient::GetThreadStopInfo (uint32_t tid, StringExtractorGDBRemote &response) |
| 1848 | { |
| 1849 | if (m_supports_qThreadStopInfo) |
| 1850 | { |
| 1851 | char packet[256]; |
| 1852 | int packet_len = ::snprintf(packet, sizeof(packet), "qThreadStopInfo%x", tid); |
| 1853 | assert (packet_len < sizeof(packet)); |
| 1854 | if (SendPacketAndWaitForResponse(packet, packet_len, response, false)) |
| 1855 | { |
Greg Clayton | 5b53e8e | 2011-05-15 23:46:54 +0000 | [diff] [blame] | 1856 | if (response.IsNormalResponse()) |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1857 | return true; |
| 1858 | else |
| 1859 | return false; |
| 1860 | } |
Greg Clayton | 5b53e8e | 2011-05-15 23:46:54 +0000 | [diff] [blame] | 1861 | else |
| 1862 | { |
| 1863 | m_supports_qThreadStopInfo = false; |
| 1864 | } |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1865 | } |
Greg Clayton | 139da72 | 2011-05-20 03:15:54 +0000 | [diff] [blame] | 1866 | // if (SetCurrentThread (tid)) |
| 1867 | // return GetStopReply (response); |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1868 | return false; |
| 1869 | } |
| 1870 | |
| 1871 | |
| 1872 | uint8_t |
| 1873 | GDBRemoteCommunicationClient::SendGDBStoppointTypePacket (GDBStoppointType type, bool insert, addr_t addr, uint32_t length) |
| 1874 | { |
| 1875 | switch (type) |
| 1876 | { |
| 1877 | case eBreakpointSoftware: if (!m_supports_z0) return UINT8_MAX; break; |
| 1878 | case eBreakpointHardware: if (!m_supports_z1) return UINT8_MAX; break; |
| 1879 | case eWatchpointWrite: if (!m_supports_z2) return UINT8_MAX; break; |
| 1880 | case eWatchpointRead: if (!m_supports_z3) return UINT8_MAX; break; |
| 1881 | case eWatchpointReadWrite: if (!m_supports_z4) return UINT8_MAX; break; |
| 1882 | default: return UINT8_MAX; |
| 1883 | } |
| 1884 | |
| 1885 | char packet[64]; |
| 1886 | const int packet_len = ::snprintf (packet, |
| 1887 | sizeof(packet), |
| 1888 | "%c%i,%llx,%x", |
| 1889 | insert ? 'Z' : 'z', |
| 1890 | type, |
| 1891 | addr, |
| 1892 | length); |
| 1893 | |
| 1894 | assert (packet_len + 1 < sizeof(packet)); |
| 1895 | StringExtractorGDBRemote response; |
| 1896 | if (SendPacketAndWaitForResponse(packet, packet_len, response, true)) |
| 1897 | { |
| 1898 | if (response.IsOKResponse()) |
| 1899 | return 0; |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1900 | else if (response.IsErrorResponse()) |
| 1901 | return response.GetError(); |
| 1902 | } |
Greg Clayton | 5b53e8e | 2011-05-15 23:46:54 +0000 | [diff] [blame] | 1903 | else |
| 1904 | { |
| 1905 | switch (type) |
| 1906 | { |
| 1907 | case eBreakpointSoftware: m_supports_z0 = false; break; |
| 1908 | case eBreakpointHardware: m_supports_z1 = false; break; |
| 1909 | case eWatchpointWrite: m_supports_z2 = false; break; |
| 1910 | case eWatchpointRead: m_supports_z3 = false; break; |
| 1911 | case eWatchpointReadWrite: m_supports_z4 = false; break; |
| 1912 | default: break; |
| 1913 | } |
| 1914 | } |
| 1915 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1916 | return UINT8_MAX; |
| 1917 | } |
Greg Clayton | 4a60f9e | 2011-05-20 23:38:13 +0000 | [diff] [blame] | 1918 | |
| 1919 | size_t |
| 1920 | GDBRemoteCommunicationClient::GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids, |
| 1921 | bool &sequence_mutex_unavailable) |
| 1922 | { |
| 1923 | Mutex::Locker locker; |
| 1924 | thread_ids.clear(); |
| 1925 | |
Greg Clayton | c8dd570 | 2012-04-12 19:04:34 +0000 | [diff] [blame] | 1926 | if (GetSequenceMutex (locker)) |
Greg Clayton | 4a60f9e | 2011-05-20 23:38:13 +0000 | [diff] [blame] | 1927 | { |
| 1928 | sequence_mutex_unavailable = false; |
| 1929 | StringExtractorGDBRemote response; |
| 1930 | |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 1931 | for (SendPacketNoLock ("qfThreadInfo", strlen("qfThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ()); |
Greg Clayton | 4a60f9e | 2011-05-20 23:38:13 +0000 | [diff] [blame] | 1932 | response.IsNormalResponse(); |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 1933 | SendPacketNoLock ("qsThreadInfo", strlen("qsThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ())) |
Greg Clayton | 4a60f9e | 2011-05-20 23:38:13 +0000 | [diff] [blame] | 1934 | { |
| 1935 | char ch = response.GetChar(); |
| 1936 | if (ch == 'l') |
| 1937 | break; |
| 1938 | if (ch == 'm') |
| 1939 | { |
| 1940 | do |
| 1941 | { |
| 1942 | tid_t tid = response.GetHexMaxU32(false, LLDB_INVALID_THREAD_ID); |
| 1943 | |
| 1944 | if (tid != LLDB_INVALID_THREAD_ID) |
| 1945 | { |
| 1946 | thread_ids.push_back (tid); |
| 1947 | } |
| 1948 | ch = response.GetChar(); // Skip the command separator |
| 1949 | } while (ch == ','); // Make sure we got a comma separator |
| 1950 | } |
| 1951 | } |
| 1952 | } |
| 1953 | else |
| 1954 | { |
Greg Clayton | c8dd570 | 2012-04-12 19:04:34 +0000 | [diff] [blame] | 1955 | LogSP log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS)); |
| 1956 | if (log) |
| 1957 | log->Printf("error: failed to get packet sequence mutex, not sending packet 'qfThreadInfo'"); |
Greg Clayton | 4a60f9e | 2011-05-20 23:38:13 +0000 | [diff] [blame] | 1958 | sequence_mutex_unavailable = true; |
| 1959 | } |
| 1960 | return thread_ids.size(); |
| 1961 | } |
Greg Clayton | 516f084 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 1962 | |
| 1963 | lldb::addr_t |
| 1964 | GDBRemoteCommunicationClient::GetShlibInfoAddr() |
| 1965 | { |
| 1966 | if (!IsRunning()) |
| 1967 | { |
| 1968 | StringExtractorGDBRemote response; |
| 1969 | if (SendPacketAndWaitForResponse("qShlibInfoAddr", ::strlen ("qShlibInfoAddr"), response, false)) |
| 1970 | { |
| 1971 | if (response.IsNormalResponse()) |
| 1972 | return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS); |
| 1973 | } |
| 1974 | } |
| 1975 | return LLDB_INVALID_ADDRESS; |
| 1976 | } |
| 1977 | |