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