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