Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1 | //===-- GDBRemoteCommunicationClient.cpp ------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 9 | #include "GDBRemoteCommunicationClient.h" |
| 10 | |
Greg Clayton | e034a04 | 2015-05-21 20:52:06 +0000 | [diff] [blame] | 11 | #include <math.h> |
Daniel Malea | b89d049 | 2013-08-28 16:06:16 +0000 | [diff] [blame] | 12 | #include <sys/stat.h> |
| 13 | |
Greg Clayton | e034a04 | 2015-05-21 20:52:06 +0000 | [diff] [blame] | 14 | #include <numeric> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 15 | #include <sstream> |
Han Ming Ong | 4b6459f | 2013-01-18 23:11:53 +0000 | [diff] [blame] | 16 | |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 17 | #include "lldb/Core/ModuleSpec.h" |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 18 | #include "lldb/Host/HostInfo.h" |
Pavel Labath | 16064d3 | 2018-03-20 11:56:24 +0000 | [diff] [blame] | 19 | #include "lldb/Host/XML.h" |
Greg Clayton | 0b90be1 | 2015-06-23 21:27:50 +0000 | [diff] [blame] | 20 | #include "lldb/Symbol/Symbol.h" |
Pavel Labath | 4cb6992 | 2016-07-29 15:41:52 +0000 | [diff] [blame] | 21 | #include "lldb/Target/MemoryRegionInfo.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 22 | #include "lldb/Target/Target.h" |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 23 | #include "lldb/Target/UnixSignals.h" |
Pavel Labath | 145d95c | 2018-04-17 18:53:35 +0000 | [diff] [blame] | 24 | #include "lldb/Utility/Args.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 25 | #include "lldb/Utility/DataBufferHeap.h" |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 26 | #include "lldb/Utility/LLDBAssert.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 27 | #include "lldb/Utility/Log.h" |
Pavel Labath | d821c99 | 2018-08-07 11:07:21 +0000 | [diff] [blame] | 28 | #include "lldb/Utility/State.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 29 | #include "lldb/Utility/StreamString.h" |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 30 | |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 31 | #include "ProcessGDBRemote.h" |
| 32 | #include "ProcessGDBRemoteLog.h" |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 33 | #include "lldb/Host/Config.h" |
Pavel Labath | 9af71b3 | 2018-03-20 16:14:00 +0000 | [diff] [blame] | 34 | #include "lldb/Utility/StringExtractorGDBRemote.h" |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 35 | |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/StringSwitch.h" |
Jonas Devlieghere | 2a5a906 | 2019-10-02 18:02:26 +0000 | [diff] [blame] | 37 | #include "llvm/Support/JSON.h" |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 38 | |
Haibo Huang | 7debc93 | 2019-08-08 21:42:33 +0000 | [diff] [blame] | 39 | #if defined(HAVE_LIBCOMPRESSION) |
Jason Molenda | 91ffe0a | 2015-06-18 21:46:06 +0000 | [diff] [blame] | 40 | #include <compression.h> |
| 41 | #endif |
| 42 | |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 43 | using namespace lldb; |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 44 | using namespace lldb_private::process_gdb_remote; |
Jonas Devlieghere | 2a5a906 | 2019-10-02 18:02:26 +0000 | [diff] [blame] | 45 | using namespace lldb_private; |
Pavel Labath | 1eff73c | 2016-11-24 10:54:49 +0000 | [diff] [blame] | 46 | using namespace std::chrono; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 47 | |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 48 | // GDBRemoteCommunicationClient constructor |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 49 | GDBRemoteCommunicationClient::GDBRemoteCommunicationClient() |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 50 | : GDBRemoteClientBase("gdb-remote.client", "gdb-remote.client.rx_packet"), |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 51 | m_supports_not_sending_acks(eLazyBoolCalculate), |
| 52 | m_supports_thread_suffix(eLazyBoolCalculate), |
| 53 | m_supports_threads_in_stop_reply(eLazyBoolCalculate), |
| 54 | m_supports_vCont_all(eLazyBoolCalculate), |
| 55 | m_supports_vCont_any(eLazyBoolCalculate), |
| 56 | m_supports_vCont_c(eLazyBoolCalculate), |
| 57 | m_supports_vCont_C(eLazyBoolCalculate), |
| 58 | m_supports_vCont_s(eLazyBoolCalculate), |
| 59 | m_supports_vCont_S(eLazyBoolCalculate), |
| 60 | m_qHostInfo_is_valid(eLazyBoolCalculate), |
| 61 | m_curr_pid_is_valid(eLazyBoolCalculate), |
| 62 | m_qProcessInfo_is_valid(eLazyBoolCalculate), |
| 63 | m_qGDBServerVersion_is_valid(eLazyBoolCalculate), |
| 64 | m_supports_alloc_dealloc_memory(eLazyBoolCalculate), |
| 65 | m_supports_memory_region_info(eLazyBoolCalculate), |
| 66 | m_supports_watchpoint_support_info(eLazyBoolCalculate), |
| 67 | m_supports_detach_stay_stopped(eLazyBoolCalculate), |
| 68 | m_watchpoints_trigger_after_instruction(eLazyBoolCalculate), |
| 69 | m_attach_or_wait_reply(eLazyBoolCalculate), |
| 70 | m_prepare_for_reg_writing_reply(eLazyBoolCalculate), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 71 | m_supports_p(eLazyBoolCalculate), m_supports_x(eLazyBoolCalculate), |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 72 | m_avoid_g_packets(eLazyBoolCalculate), |
| 73 | m_supports_QSaveRegisterState(eLazyBoolCalculate), |
| 74 | m_supports_qXfer_auxv_read(eLazyBoolCalculate), |
| 75 | m_supports_qXfer_libraries_read(eLazyBoolCalculate), |
| 76 | m_supports_qXfer_libraries_svr4_read(eLazyBoolCalculate), |
| 77 | m_supports_qXfer_features_read(eLazyBoolCalculate), |
Pavel Labath | 16064d3 | 2018-03-20 11:56:24 +0000 | [diff] [blame] | 78 | m_supports_qXfer_memory_map_read(eLazyBoolCalculate), |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 79 | m_supports_augmented_libraries_svr4_read(eLazyBoolCalculate), |
| 80 | m_supports_jThreadExtendedInfo(eLazyBoolCalculate), |
| 81 | m_supports_jLoadedDynamicLibrariesInfos(eLazyBoolCalculate), |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 82 | m_supports_jGetSharedCacheInfo(eLazyBoolCalculate), |
Eugene Zemtsov | 7993cc5 | 2017-03-07 21:34:40 +0000 | [diff] [blame] | 83 | m_supports_QPassSignals(eLazyBoolCalculate), |
Ravitheja Addepally | dab1d5f | 2017-07-12 11:15:34 +0000 | [diff] [blame] | 84 | m_supports_error_string_reply(eLazyBoolCalculate), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 85 | m_supports_qProcessInfoPID(true), m_supports_qfProcessInfo(true), |
| 86 | m_supports_qUserName(true), m_supports_qGroupName(true), |
| 87 | m_supports_qThreadStopInfo(true), m_supports_z0(true), |
| 88 | m_supports_z1(true), m_supports_z2(true), m_supports_z3(true), |
| 89 | m_supports_z4(true), m_supports_QEnvironment(true), |
| 90 | m_supports_QEnvironmentHexEncoded(true), m_supports_qSymbol(true), |
| 91 | m_qSymbol_requests_done(false), m_supports_qModuleInfo(true), |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 92 | m_supports_jThreadsInfo(true), m_supports_jModulesInfo(true), |
| 93 | m_curr_pid(LLDB_INVALID_PROCESS_ID), m_curr_tid(LLDB_INVALID_THREAD_ID), |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 94 | m_curr_tid_run(LLDB_INVALID_THREAD_ID), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 95 | m_num_supported_hardware_watchpoints(0), m_host_arch(), m_process_arch(), |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 96 | m_os_build(), m_os_kernel(), m_hostname(), m_gdb_server_name(), |
| 97 | m_gdb_server_version(UINT32_MAX), m_default_packet_timeout(0), |
| 98 | m_max_packet_size(0), m_qSupported_response(), |
| 99 | m_supported_async_json_packets_is_valid(false), |
Pavel Labath | 16064d3 | 2018-03-20 11:56:24 +0000 | [diff] [blame] | 100 | m_supported_async_json_packets_sp(), m_qXfer_memory_map(), |
| 101 | m_qXfer_memory_map_loaded(false) {} |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 102 | |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 103 | // Destructor |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 104 | GDBRemoteCommunicationClient::~GDBRemoteCommunicationClient() { |
| 105 | if (IsConnected()) |
| 106 | Disconnect(); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 109 | bool GDBRemoteCommunicationClient::HandshakeWithServer(Status *error_ptr) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 110 | ResetDiscoverableSettings(false); |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 111 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 112 | // Start the read thread after we send the handshake ack since if we fail to |
| 113 | // send the handshake ack, there is no reason to continue... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | if (SendAck()) { |
Jonas Devlieghere | 135cf98 | 2019-06-30 19:00:09 +0000 | [diff] [blame] | 115 | // Wait for any responses that might have been queued up in the remote |
| 116 | // GDB server and flush them all |
| 117 | StringExtractorGDBRemote response; |
| 118 | PacketResult packet_result = PacketResult::Success; |
| 119 | while (packet_result == PacketResult::Success) |
| 120 | packet_result = ReadPacket(response, milliseconds(10), false); |
| 121 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 122 | // The return value from QueryNoAckModeSupported() is true if the packet |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 123 | // was sent and _any_ response (including UNIMPLEMENTED) was received), or |
| 124 | // false if no response was received. This quickly tells us if we have a |
| 125 | // live connection to a remote GDB server... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 126 | if (QueryNoAckModeSupported()) { |
| 127 | return true; |
| 128 | } else { |
| 129 | if (error_ptr) |
| 130 | error_ptr->SetErrorString("failed to get reply to handshake packet"); |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 131 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 132 | } else { |
| 133 | if (error_ptr) |
| 134 | error_ptr->SetErrorString("failed to send the handshake ack"); |
| 135 | } |
| 136 | return false; |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 139 | bool GDBRemoteCommunicationClient::GetEchoSupported() { |
| 140 | if (m_supports_qEcho == eLazyBoolCalculate) { |
| 141 | GetRemoteQSupported(); |
| 142 | } |
| 143 | return m_supports_qEcho == eLazyBoolYes; |
Greg Clayton | b30c50c | 2015-05-29 00:01:55 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Eugene Zemtsov | 7993cc5 | 2017-03-07 21:34:40 +0000 | [diff] [blame] | 146 | bool GDBRemoteCommunicationClient::GetQPassSignalsSupported() { |
| 147 | if (m_supports_QPassSignals == eLazyBoolCalculate) { |
| 148 | GetRemoteQSupported(); |
| 149 | } |
| 150 | return m_supports_QPassSignals == eLazyBoolYes; |
| 151 | } |
| 152 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 153 | bool GDBRemoteCommunicationClient::GetAugmentedLibrariesSVR4ReadSupported() { |
| 154 | if (m_supports_augmented_libraries_svr4_read == eLazyBoolCalculate) { |
| 155 | GetRemoteQSupported(); |
| 156 | } |
| 157 | return m_supports_augmented_libraries_svr4_read == eLazyBoolYes; |
Steve Pucci | 5ae54ae | 2014-01-25 05:46:51 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 160 | bool GDBRemoteCommunicationClient::GetQXferLibrariesSVR4ReadSupported() { |
| 161 | if (m_supports_qXfer_libraries_svr4_read == eLazyBoolCalculate) { |
| 162 | GetRemoteQSupported(); |
| 163 | } |
| 164 | return m_supports_qXfer_libraries_svr4_read == eLazyBoolYes; |
Steve Pucci | 5ae54ae | 2014-01-25 05:46:51 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 167 | bool GDBRemoteCommunicationClient::GetQXferLibrariesReadSupported() { |
| 168 | if (m_supports_qXfer_libraries_read == eLazyBoolCalculate) { |
| 169 | GetRemoteQSupported(); |
| 170 | } |
| 171 | return m_supports_qXfer_libraries_read == eLazyBoolYes; |
Steve Pucci | 5ae54ae | 2014-01-25 05:46:51 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 174 | bool GDBRemoteCommunicationClient::GetQXferAuxvReadSupported() { |
| 175 | if (m_supports_qXfer_auxv_read == eLazyBoolCalculate) { |
| 176 | GetRemoteQSupported(); |
| 177 | } |
| 178 | return m_supports_qXfer_auxv_read == eLazyBoolYes; |
Steve Pucci | 03904ac | 2014-03-04 23:18:46 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 181 | bool GDBRemoteCommunicationClient::GetQXferFeaturesReadSupported() { |
| 182 | if (m_supports_qXfer_features_read == eLazyBoolCalculate) { |
| 183 | GetRemoteQSupported(); |
| 184 | } |
| 185 | return m_supports_qXfer_features_read == eLazyBoolYes; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Pavel Labath | 16064d3 | 2018-03-20 11:56:24 +0000 | [diff] [blame] | 188 | bool GDBRemoteCommunicationClient::GetQXferMemoryMapReadSupported() { |
| 189 | if (m_supports_qXfer_memory_map_read == eLazyBoolCalculate) { |
| 190 | GetRemoteQSupported(); |
| 191 | } |
| 192 | return m_supports_qXfer_memory_map_read == eLazyBoolYes; |
| 193 | } |
| 194 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 195 | uint64_t GDBRemoteCommunicationClient::GetRemoteMaxPacketSize() { |
| 196 | if (m_max_packet_size == 0) { |
| 197 | GetRemoteQSupported(); |
| 198 | } |
| 199 | return m_max_packet_size; |
Steve Pucci | 5ae54ae | 2014-01-25 05:46:51 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 202 | bool GDBRemoteCommunicationClient::QueryNoAckModeSupported() { |
| 203 | if (m_supports_not_sending_acks == eLazyBoolCalculate) { |
| 204 | m_send_acks = true; |
| 205 | m_supports_not_sending_acks = eLazyBoolNo; |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 206 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 207 | // This is the first real packet that we'll send in a debug session and it |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 208 | // may take a little longer than normal to receive a reply. Wait at least |
| 209 | // 6 seconds for a reply to this packet. |
Jason Molenda | 36a216e | 2014-07-24 01:36:24 +0000 | [diff] [blame] | 210 | |
Pavel Labath | 1eff73c | 2016-11-24 10:54:49 +0000 | [diff] [blame] | 211 | ScopedTimeout timeout(*this, std::max(GetPacketTimeout(), seconds(6))); |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 212 | |
Steve Pucci | 5ae54ae | 2014-01-25 05:46:51 +0000 | [diff] [blame] | 213 | StringExtractorGDBRemote response; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 214 | if (SendPacketAndWaitForResponse("QStartNoAckMode", response, false) == |
| 215 | PacketResult::Success) { |
| 216 | if (response.IsOKResponse()) { |
| 217 | m_send_acks = false; |
| 218 | m_supports_not_sending_acks = eLazyBoolYes; |
| 219 | } |
| 220 | return true; |
Steve Pucci | 5ae54ae | 2014-01-25 05:46:51 +0000 | [diff] [blame] | 221 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 222 | } |
| 223 | return false; |
Steve Pucci | 5ae54ae | 2014-01-25 05:46:51 +0000 | [diff] [blame] | 224 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 225 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 226 | void GDBRemoteCommunicationClient::GetListThreadsInStopReplySupported() { |
| 227 | if (m_supports_threads_in_stop_reply == eLazyBoolCalculate) { |
| 228 | m_supports_threads_in_stop_reply = eLazyBoolNo; |
| 229 | |
| 230 | StringExtractorGDBRemote response; |
| 231 | if (SendPacketAndWaitForResponse("QListThreadsInStopReply", response, |
| 232 | false) == PacketResult::Success) { |
| 233 | if (response.IsOKResponse()) |
| 234 | m_supports_threads_in_stop_reply = eLazyBoolYes; |
Pavel Labath | 5c95ee4 | 2016-08-30 13:56:11 +0000 | [diff] [blame] | 235 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 236 | } |
Pavel Labath | 0faf373 | 2016-08-25 08:34:57 +0000 | [diff] [blame] | 237 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 238 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 239 | bool GDBRemoteCommunicationClient::GetVAttachOrWaitSupported() { |
| 240 | if (m_attach_or_wait_reply == eLazyBoolCalculate) { |
| 241 | m_attach_or_wait_reply = eLazyBoolNo; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 242 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 243 | StringExtractorGDBRemote response; |
| 244 | if (SendPacketAndWaitForResponse("qVAttachOrWaitSupported", response, |
| 245 | false) == PacketResult::Success) { |
| 246 | if (response.IsOKResponse()) |
| 247 | m_attach_or_wait_reply = eLazyBoolYes; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 248 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 249 | } |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 250 | return m_attach_or_wait_reply == eLazyBoolYes; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 253 | bool GDBRemoteCommunicationClient::GetSyncThreadStateSupported() { |
| 254 | if (m_prepare_for_reg_writing_reply == eLazyBoolCalculate) { |
| 255 | m_prepare_for_reg_writing_reply = eLazyBoolNo; |
| 256 | |
| 257 | StringExtractorGDBRemote response; |
| 258 | if (SendPacketAndWaitForResponse("qSyncThreadStateSupported", response, |
| 259 | false) == PacketResult::Success) { |
| 260 | if (response.IsOKResponse()) |
| 261 | m_prepare_for_reg_writing_reply = eLazyBoolYes; |
| 262 | } |
| 263 | } |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 264 | return m_prepare_for_reg_writing_reply == eLazyBoolYes; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void GDBRemoteCommunicationClient::ResetDiscoverableSettings(bool did_exec) { |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 268 | if (!did_exec) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 269 | // Hard reset everything, this is when we first connect to a GDB server |
| 270 | m_supports_not_sending_acks = eLazyBoolCalculate; |
| 271 | m_supports_thread_suffix = eLazyBoolCalculate; |
| 272 | m_supports_threads_in_stop_reply = eLazyBoolCalculate; |
| 273 | m_supports_vCont_c = eLazyBoolCalculate; |
| 274 | m_supports_vCont_C = eLazyBoolCalculate; |
| 275 | m_supports_vCont_s = eLazyBoolCalculate; |
| 276 | m_supports_vCont_S = eLazyBoolCalculate; |
| 277 | m_supports_p = eLazyBoolCalculate; |
| 278 | m_supports_x = eLazyBoolCalculate; |
| 279 | m_supports_QSaveRegisterState = eLazyBoolCalculate; |
| 280 | m_qHostInfo_is_valid = eLazyBoolCalculate; |
| 281 | m_curr_pid_is_valid = eLazyBoolCalculate; |
| 282 | m_qGDBServerVersion_is_valid = eLazyBoolCalculate; |
| 283 | m_supports_alloc_dealloc_memory = eLazyBoolCalculate; |
| 284 | m_supports_memory_region_info = eLazyBoolCalculate; |
| 285 | m_prepare_for_reg_writing_reply = eLazyBoolCalculate; |
| 286 | m_attach_or_wait_reply = eLazyBoolCalculate; |
| 287 | m_avoid_g_packets = eLazyBoolCalculate; |
| 288 | m_supports_qXfer_auxv_read = eLazyBoolCalculate; |
| 289 | m_supports_qXfer_libraries_read = eLazyBoolCalculate; |
| 290 | m_supports_qXfer_libraries_svr4_read = eLazyBoolCalculate; |
| 291 | m_supports_qXfer_features_read = eLazyBoolCalculate; |
Pavel Labath | 16064d3 | 2018-03-20 11:56:24 +0000 | [diff] [blame] | 292 | m_supports_qXfer_memory_map_read = eLazyBoolCalculate; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 293 | m_supports_augmented_libraries_svr4_read = eLazyBoolCalculate; |
| 294 | m_supports_qProcessInfoPID = true; |
| 295 | m_supports_qfProcessInfo = true; |
| 296 | m_supports_qUserName = true; |
| 297 | m_supports_qGroupName = true; |
| 298 | m_supports_qThreadStopInfo = true; |
| 299 | m_supports_z0 = true; |
| 300 | m_supports_z1 = true; |
| 301 | m_supports_z2 = true; |
| 302 | m_supports_z3 = true; |
| 303 | m_supports_z4 = true; |
| 304 | m_supports_QEnvironment = true; |
| 305 | m_supports_QEnvironmentHexEncoded = true; |
| 306 | m_supports_qSymbol = true; |
| 307 | m_qSymbol_requests_done = false; |
| 308 | m_supports_qModuleInfo = true; |
| 309 | m_host_arch.Clear(); |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 310 | m_os_version = llvm::VersionTuple(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 311 | m_os_build.clear(); |
| 312 | m_os_kernel.clear(); |
| 313 | m_hostname.clear(); |
| 314 | m_gdb_server_name.clear(); |
| 315 | m_gdb_server_version = UINT32_MAX; |
Pavel Labath | 1eff73c | 2016-11-24 10:54:49 +0000 | [diff] [blame] | 316 | m_default_packet_timeout = seconds(0); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 317 | m_max_packet_size = 0; |
| 318 | m_qSupported_response.clear(); |
| 319 | m_supported_async_json_packets_is_valid = false; |
| 320 | m_supported_async_json_packets_sp.reset(); |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 321 | m_supports_jModulesInfo = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 324 | // These flags should be reset when we first connect to a GDB server and when |
| 325 | // our inferior process execs |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 326 | m_qProcessInfo_is_valid = eLazyBoolCalculate; |
| 327 | m_process_arch.Clear(); |
| 328 | } |
| 329 | |
| 330 | void GDBRemoteCommunicationClient::GetRemoteQSupported() { |
| 331 | // Clear out any capabilities we expect to see in the qSupported response |
| 332 | m_supports_qXfer_auxv_read = eLazyBoolNo; |
| 333 | m_supports_qXfer_libraries_read = eLazyBoolNo; |
| 334 | m_supports_qXfer_libraries_svr4_read = eLazyBoolNo; |
| 335 | m_supports_augmented_libraries_svr4_read = eLazyBoolNo; |
| 336 | m_supports_qXfer_features_read = eLazyBoolNo; |
Pavel Labath | 16064d3 | 2018-03-20 11:56:24 +0000 | [diff] [blame] | 337 | m_supports_qXfer_memory_map_read = eLazyBoolNo; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 338 | m_max_packet_size = UINT64_MAX; // It's supposed to always be there, but if |
| 339 | // not, we assume no limit |
| 340 | |
| 341 | // build the qSupported packet |
Tatyana Krasnukha | faf6b25 | 2019-10-17 15:16:21 +0000 | [diff] [blame] | 342 | std::vector<std::string> features = {"xmlRegisters=i386,arm,mips,arc"}; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 343 | StreamString packet; |
| 344 | packet.PutCString("qSupported"); |
| 345 | for (uint32_t i = 0; i < features.size(); ++i) { |
| 346 | packet.PutCString(i == 0 ? ":" : ";"); |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 347 | packet.PutCString(features[i]); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | StringExtractorGDBRemote response; |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 351 | if (SendPacketAndWaitForResponse(packet.GetString(), response, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 352 | /*send_async=*/false) == |
| 353 | PacketResult::Success) { |
Jonas Devlieghere | d35b42f | 2019-08-21 04:55:56 +0000 | [diff] [blame] | 354 | const char *response_cstr = response.GetStringRef().data(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 355 | |
| 356 | // Hang on to the qSupported packet, so that platforms can do custom |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 357 | // configuration of the transport before attaching/launching the process. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 358 | m_qSupported_response = response_cstr; |
| 359 | |
| 360 | if (::strstr(response_cstr, "qXfer:auxv:read+")) |
| 361 | m_supports_qXfer_auxv_read = eLazyBoolYes; |
| 362 | if (::strstr(response_cstr, "qXfer:libraries-svr4:read+")) |
| 363 | m_supports_qXfer_libraries_svr4_read = eLazyBoolYes; |
| 364 | if (::strstr(response_cstr, "augmented-libraries-svr4-read")) { |
| 365 | m_supports_qXfer_libraries_svr4_read = eLazyBoolYes; // implied |
| 366 | m_supports_augmented_libraries_svr4_read = eLazyBoolYes; |
| 367 | } |
| 368 | if (::strstr(response_cstr, "qXfer:libraries:read+")) |
| 369 | m_supports_qXfer_libraries_read = eLazyBoolYes; |
| 370 | if (::strstr(response_cstr, "qXfer:features:read+")) |
| 371 | m_supports_qXfer_features_read = eLazyBoolYes; |
Pavel Labath | 16064d3 | 2018-03-20 11:56:24 +0000 | [diff] [blame] | 372 | if (::strstr(response_cstr, "qXfer:memory-map:read+")) |
| 373 | m_supports_qXfer_memory_map_read = eLazyBoolYes; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 374 | |
| 375 | // Look for a list of compressions in the features list e.g. |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 376 | // qXfer:features:read+;PacketSize=20000;qEcho+;SupportedCompressions=zlib- |
| 377 | // deflate,lzma |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 378 | const char *features_list = ::strstr(response_cstr, "qXfer:features:"); |
| 379 | if (features_list) { |
| 380 | const char *compressions = |
| 381 | ::strstr(features_list, "SupportedCompressions="); |
| 382 | if (compressions) { |
| 383 | std::vector<std::string> supported_compressions; |
| 384 | compressions += sizeof("SupportedCompressions=") - 1; |
| 385 | const char *end_of_compressions = strchr(compressions, ';'); |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 386 | if (end_of_compressions == nullptr) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 387 | end_of_compressions = strchr(compressions, '\0'); |
| 388 | } |
| 389 | const char *current_compression = compressions; |
| 390 | while (current_compression < end_of_compressions) { |
| 391 | const char *next_compression_name = strchr(current_compression, ','); |
| 392 | const char *end_of_this_word = next_compression_name; |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 393 | if (next_compression_name == nullptr || |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 394 | end_of_compressions < next_compression_name) { |
| 395 | end_of_this_word = end_of_compressions; |
| 396 | } |
| 397 | |
| 398 | if (end_of_this_word) { |
| 399 | if (end_of_this_word == current_compression) { |
| 400 | current_compression++; |
| 401 | } else { |
| 402 | std::string this_compression( |
| 403 | current_compression, end_of_this_word - current_compression); |
| 404 | supported_compressions.push_back(this_compression); |
| 405 | current_compression = end_of_this_word + 1; |
| 406 | } |
| 407 | } else { |
| 408 | supported_compressions.push_back(current_compression); |
| 409 | current_compression = end_of_compressions; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | if (supported_compressions.size() > 0) { |
| 414 | MaybeEnableCompression(supported_compressions); |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | if (::strstr(response_cstr, "qEcho")) |
| 420 | m_supports_qEcho = eLazyBoolYes; |
| 421 | else |
| 422 | m_supports_qEcho = eLazyBoolNo; |
| 423 | |
Eugene Zemtsov | 7993cc5 | 2017-03-07 21:34:40 +0000 | [diff] [blame] | 424 | if (::strstr(response_cstr, "QPassSignals+")) |
| 425 | m_supports_QPassSignals = eLazyBoolYes; |
| 426 | else |
| 427 | m_supports_QPassSignals = eLazyBoolNo; |
| 428 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 429 | const char *packet_size_str = ::strstr(response_cstr, "PacketSize="); |
| 430 | if (packet_size_str) { |
| 431 | StringExtractorGDBRemote packet_response(packet_size_str + |
| 432 | strlen("PacketSize=")); |
| 433 | m_max_packet_size = |
| 434 | packet_response.GetHexMaxU64(/*little_endian=*/false, UINT64_MAX); |
| 435 | if (m_max_packet_size == 0) { |
| 436 | m_max_packet_size = UINT64_MAX; // Must have been a garbled response |
| 437 | Log *log( |
| 438 | ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 439 | LLDB_LOGF(log, "Garbled PacketSize spec in qSupported response"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | bool GDBRemoteCommunicationClient::GetThreadSuffixSupported() { |
| 446 | if (m_supports_thread_suffix == eLazyBoolCalculate) { |
| 447 | StringExtractorGDBRemote response; |
| 448 | m_supports_thread_suffix = eLazyBoolNo; |
| 449 | if (SendPacketAndWaitForResponse("QThreadSuffixSupported", response, |
| 450 | false) == PacketResult::Success) { |
| 451 | if (response.IsOKResponse()) |
| 452 | m_supports_thread_suffix = eLazyBoolYes; |
| 453 | } |
| 454 | } |
| 455 | return m_supports_thread_suffix; |
| 456 | } |
| 457 | bool GDBRemoteCommunicationClient::GetVContSupported(char flavor) { |
| 458 | if (m_supports_vCont_c == eLazyBoolCalculate) { |
| 459 | StringExtractorGDBRemote response; |
| 460 | m_supports_vCont_any = eLazyBoolNo; |
| 461 | m_supports_vCont_all = eLazyBoolNo; |
| 462 | m_supports_vCont_c = eLazyBoolNo; |
| 463 | m_supports_vCont_C = eLazyBoolNo; |
| 464 | m_supports_vCont_s = eLazyBoolNo; |
| 465 | m_supports_vCont_S = eLazyBoolNo; |
| 466 | if (SendPacketAndWaitForResponse("vCont?", response, false) == |
| 467 | PacketResult::Success) { |
Jonas Devlieghere | d35b42f | 2019-08-21 04:55:56 +0000 | [diff] [blame] | 468 | const char *response_cstr = response.GetStringRef().data(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 469 | if (::strstr(response_cstr, ";c")) |
| 470 | m_supports_vCont_c = eLazyBoolYes; |
| 471 | |
| 472 | if (::strstr(response_cstr, ";C")) |
| 473 | m_supports_vCont_C = eLazyBoolYes; |
| 474 | |
| 475 | if (::strstr(response_cstr, ";s")) |
| 476 | m_supports_vCont_s = eLazyBoolYes; |
| 477 | |
| 478 | if (::strstr(response_cstr, ";S")) |
| 479 | m_supports_vCont_S = eLazyBoolYes; |
| 480 | |
| 481 | if (m_supports_vCont_c == eLazyBoolYes && |
| 482 | m_supports_vCont_C == eLazyBoolYes && |
| 483 | m_supports_vCont_s == eLazyBoolYes && |
| 484 | m_supports_vCont_S == eLazyBoolYes) { |
| 485 | m_supports_vCont_all = eLazyBoolYes; |
| 486 | } |
| 487 | |
| 488 | if (m_supports_vCont_c == eLazyBoolYes || |
| 489 | m_supports_vCont_C == eLazyBoolYes || |
| 490 | m_supports_vCont_s == eLazyBoolYes || |
| 491 | m_supports_vCont_S == eLazyBoolYes) { |
| 492 | m_supports_vCont_any = eLazyBoolYes; |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | switch (flavor) { |
| 498 | case 'a': |
| 499 | return m_supports_vCont_any; |
| 500 | case 'A': |
| 501 | return m_supports_vCont_all; |
| 502 | case 'c': |
| 503 | return m_supports_vCont_c; |
| 504 | case 'C': |
| 505 | return m_supports_vCont_C; |
| 506 | case 's': |
| 507 | return m_supports_vCont_s; |
| 508 | case 'S': |
| 509 | return m_supports_vCont_S; |
| 510 | default: |
| 511 | break; |
| 512 | } |
| 513 | return false; |
| 514 | } |
| 515 | |
Pavel Labath | 4b6f959 | 2016-08-18 08:30:03 +0000 | [diff] [blame] | 516 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 517 | GDBRemoteCommunicationClient::SendThreadSpecificPacketAndWaitForResponse( |
| 518 | lldb::tid_t tid, StreamString &&payload, StringExtractorGDBRemote &response, |
| 519 | bool send_async) { |
| 520 | Lock lock(*this, send_async); |
| 521 | if (!lock) { |
| 522 | if (Log *log = ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet( |
| 523 | GDBR_LOG_PROCESS | GDBR_LOG_PACKETS)) |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 524 | LLDB_LOGF(log, |
| 525 | "GDBRemoteCommunicationClient::%s: Didn't get sequence mutex " |
| 526 | "for %s packet.", |
| 527 | __FUNCTION__, payload.GetData()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 528 | return PacketResult::ErrorNoSequenceLock; |
| 529 | } |
Pavel Labath | 5c95ee4 | 2016-08-30 13:56:11 +0000 | [diff] [blame] | 530 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 531 | if (GetThreadSuffixSupported()) |
| 532 | payload.Printf(";thread:%4.4" PRIx64 ";", tid); |
| 533 | else { |
| 534 | if (!SetCurrentThread(tid)) |
| 535 | return PacketResult::ErrorSendFailed; |
| 536 | } |
Pavel Labath | 4b6f959 | 2016-08-18 08:30:03 +0000 | [diff] [blame] | 537 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 538 | return SendPacketAndWaitForResponseNoLock(payload.GetString(), response); |
Pavel Labath | 4b6f959 | 2016-08-18 08:30:03 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 541 | // Check if the target supports 'p' packet. It sends out a 'p' packet and |
| 542 | // checks the response. A normal packet will tell us that support is available. |
Sean Callanan | b1de114 | 2013-09-04 23:24:15 +0000 | [diff] [blame] | 543 | // |
| 544 | // Takes a valid thread ID because p needs to apply to a thread. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 545 | bool GDBRemoteCommunicationClient::GetpPacketSupported(lldb::tid_t tid) { |
| 546 | if (m_supports_p == eLazyBoolCalculate) { |
| 547 | m_supports_p = eLazyBoolNo; |
| 548 | StreamString payload; |
| 549 | payload.PutCString("p0"); |
| 550 | StringExtractorGDBRemote response; |
| 551 | if (SendThreadSpecificPacketAndWaitForResponse(tid, std::move(payload), |
| 552 | response, false) == |
| 553 | PacketResult::Success && |
| 554 | response.IsNormalResponse()) { |
| 555 | m_supports_p = eLazyBoolYes; |
Hafiz Abid Qadeer | 9a78cdf | 2013-08-29 09:09:45 +0000 | [diff] [blame] | 556 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 557 | } |
| 558 | return m_supports_p; |
Hafiz Abid Qadeer | 9a78cdf | 2013-08-29 09:09:45 +0000 | [diff] [blame] | 559 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 560 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 561 | StructuredData::ObjectSP GDBRemoteCommunicationClient::GetThreadsInfo() { |
| 562 | // Get information on all threads at one using the "jThreadsInfo" packet |
| 563 | StructuredData::ObjectSP object_sp; |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 564 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 565 | if (m_supports_jThreadsInfo) { |
| 566 | StringExtractorGDBRemote response; |
| 567 | response.SetResponseValidatorToJSON(); |
| 568 | if (SendPacketAndWaitForResponse("jThreadsInfo", response, false) == |
| 569 | PacketResult::Success) { |
| 570 | if (response.IsUnsupportedResponse()) { |
| 571 | m_supports_jThreadsInfo = false; |
| 572 | } else if (!response.Empty()) { |
| 573 | object_sp = StructuredData::ParseJSON(response.GetStringRef()); |
| 574 | } |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 575 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 576 | } |
| 577 | return object_sp; |
Greg Clayton | 358cf1e | 2015-06-25 21:46:34 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 580 | bool GDBRemoteCommunicationClient::GetThreadExtendedInfoSupported() { |
| 581 | if (m_supports_jThreadExtendedInfo == eLazyBoolCalculate) { |
| 582 | StringExtractorGDBRemote response; |
| 583 | m_supports_jThreadExtendedInfo = eLazyBoolNo; |
| 584 | if (SendPacketAndWaitForResponse("jThreadExtendedInfo:", response, false) == |
| 585 | PacketResult::Success) { |
| 586 | if (response.IsOKResponse()) { |
| 587 | m_supports_jThreadExtendedInfo = eLazyBoolYes; |
| 588 | } |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 589 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 590 | } |
| 591 | return m_supports_jThreadExtendedInfo; |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Ravitheja Addepally | dab1d5f | 2017-07-12 11:15:34 +0000 | [diff] [blame] | 594 | void GDBRemoteCommunicationClient::EnableErrorStringInPacket() { |
| 595 | if (m_supports_error_string_reply == eLazyBoolCalculate) { |
| 596 | StringExtractorGDBRemote response; |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 597 | // We try to enable error strings in remote packets but if we fail, we just |
| 598 | // work in the older way. |
Ravitheja Addepally | dab1d5f | 2017-07-12 11:15:34 +0000 | [diff] [blame] | 599 | m_supports_error_string_reply = eLazyBoolNo; |
| 600 | if (SendPacketAndWaitForResponse("QEnableErrorStrings", response, false) == |
| 601 | PacketResult::Success) { |
| 602 | if (response.IsOKResponse()) { |
| 603 | m_supports_error_string_reply = eLazyBoolYes; |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 609 | bool GDBRemoteCommunicationClient::GetLoadedDynamicLibrariesInfosSupported() { |
| 610 | if (m_supports_jLoadedDynamicLibrariesInfos == eLazyBoolCalculate) { |
| 611 | StringExtractorGDBRemote response; |
| 612 | m_supports_jLoadedDynamicLibrariesInfos = eLazyBoolNo; |
| 613 | if (SendPacketAndWaitForResponse("jGetLoadedDynamicLibrariesInfos:", |
| 614 | response, |
| 615 | false) == PacketResult::Success) { |
| 616 | if (response.IsOKResponse()) { |
| 617 | m_supports_jLoadedDynamicLibrariesInfos = eLazyBoolYes; |
| 618 | } |
Jason Molenda | 20ee21b | 2015-07-10 23:15:22 +0000 | [diff] [blame] | 619 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 620 | } |
| 621 | return m_supports_jLoadedDynamicLibrariesInfos; |
Jason Molenda | 20ee21b | 2015-07-10 23:15:22 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 624 | bool GDBRemoteCommunicationClient::GetSharedCacheInfoSupported() { |
| 625 | if (m_supports_jGetSharedCacheInfo == eLazyBoolCalculate) { |
| 626 | StringExtractorGDBRemote response; |
| 627 | m_supports_jGetSharedCacheInfo = eLazyBoolNo; |
| 628 | if (SendPacketAndWaitForResponse("jGetSharedCacheInfo:", response, false) == |
| 629 | PacketResult::Success) { |
| 630 | if (response.IsOKResponse()) { |
| 631 | m_supports_jGetSharedCacheInfo = eLazyBoolYes; |
| 632 | } |
Jason Molenda | 3739735 | 2016-07-22 00:17:55 +0000 | [diff] [blame] | 633 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 634 | } |
| 635 | return m_supports_jGetSharedCacheInfo; |
Jason Molenda | 3739735 | 2016-07-22 00:17:55 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 638 | bool GDBRemoteCommunicationClient::GetxPacketSupported() { |
| 639 | if (m_supports_x == eLazyBoolCalculate) { |
| 640 | StringExtractorGDBRemote response; |
| 641 | m_supports_x = eLazyBoolNo; |
| 642 | char packet[256]; |
| 643 | snprintf(packet, sizeof(packet), "x0,0"); |
| 644 | if (SendPacketAndWaitForResponse(packet, response, false) == |
| 645 | PacketResult::Success) { |
| 646 | if (response.IsOKResponse()) |
| 647 | m_supports_x = eLazyBoolYes; |
Jason Molenda | bdc4f12 | 2014-05-06 02:59:39 +0000 | [diff] [blame] | 648 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 649 | } |
| 650 | return m_supports_x; |
Jason Molenda | bdc4f12 | 2014-05-06 02:59:39 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 653 | GDBRemoteCommunicationClient::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 654 | GDBRemoteCommunicationClient::SendPacketsAndConcatenateResponses( |
| 655 | const char *payload_prefix, std::string &response_string) { |
| 656 | Lock lock(*this, false); |
| 657 | if (!lock) { |
| 658 | Log *log(ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet(GDBR_LOG_PROCESS | |
| 659 | GDBR_LOG_PACKETS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 660 | LLDB_LOGF(log, |
| 661 | "error: failed to get packet sequence mutex, not sending " |
| 662 | "packets with prefix '%s'", |
| 663 | payload_prefix); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 664 | return PacketResult::ErrorNoSequenceLock; |
| 665 | } |
| 666 | |
| 667 | response_string = ""; |
| 668 | std::string payload_prefix_str(payload_prefix); |
| 669 | unsigned int response_size = 0x1000; |
| 670 | if (response_size > GetRemoteMaxPacketSize()) { // May send qSupported packet |
| 671 | response_size = GetRemoteMaxPacketSize(); |
| 672 | } |
| 673 | |
| 674 | for (unsigned int offset = 0; true; offset += response_size) { |
| 675 | StringExtractorGDBRemote this_response; |
| 676 | // Construct payload |
| 677 | char sizeDescriptor[128]; |
| 678 | snprintf(sizeDescriptor, sizeof(sizeDescriptor), "%x,%x", offset, |
| 679 | response_size); |
| 680 | PacketResult result = SendPacketAndWaitForResponseNoLock( |
| 681 | payload_prefix_str + sizeDescriptor, this_response); |
| 682 | if (result != PacketResult::Success) |
| 683 | return result; |
| 684 | |
| 685 | const std::string &this_string = this_response.GetStringRef(); |
| 686 | |
| 687 | // Check for m or l as first character; l seems to mean this is the last |
| 688 | // chunk |
| 689 | char first_char = *this_string.c_str(); |
| 690 | if (first_char != 'm' && first_char != 'l') { |
| 691 | return PacketResult::ErrorReplyInvalid; |
Steve Pucci | 5ae54ae | 2014-01-25 05:46:51 +0000 | [diff] [blame] | 692 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 693 | // Concatenate the result so far (skipping 'm' or 'l') |
| 694 | response_string.append(this_string, 1, std::string::npos); |
| 695 | if (first_char == 'l') |
| 696 | // We're done |
| 697 | return PacketResult::Success; |
| 698 | } |
Steve Pucci | 5ae54ae | 2014-01-25 05:46:51 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 701 | lldb::pid_t GDBRemoteCommunicationClient::GetCurrentProcessID(bool allow_lazy) { |
| 702 | if (allow_lazy && m_curr_pid_is_valid == eLazyBoolYes) |
| 703 | return m_curr_pid; |
Jaydeep Patil | 1142f83 | 2015-08-13 03:46:36 +0000 | [diff] [blame] | 704 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 705 | // First try to retrieve the pid via the qProcessInfo request. |
| 706 | GetCurrentProcessInfo(allow_lazy); |
| 707 | if (m_curr_pid_is_valid == eLazyBoolYes) { |
| 708 | // We really got it. |
| 709 | return m_curr_pid; |
| 710 | } else { |
| 711 | // If we don't get a response for qProcessInfo, check if $qC gives us a |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 712 | // result. $qC only returns a real process id on older debugserver and |
| 713 | // lldb-platform stubs. The gdb remote protocol documents $qC as returning |
| 714 | // the thread id, which newer debugserver and lldb-gdbserver stubs return |
| 715 | // correctly. |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 716 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 717 | if (SendPacketAndWaitForResponse("qC", response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 718 | PacketResult::Success) { |
| 719 | if (response.GetChar() == 'Q') { |
| 720 | if (response.GetChar() == 'C') { |
| 721 | m_curr_pid = response.GetHexMaxU32(false, LLDB_INVALID_PROCESS_ID); |
| 722 | if (m_curr_pid != LLDB_INVALID_PROCESS_ID) { |
| 723 | m_curr_pid_is_valid = eLazyBoolYes; |
| 724 | return m_curr_pid; |
| 725 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 726 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 727 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 728 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 729 | |
| 730 | // If we don't get a response for $qC, check if $qfThreadID gives us a |
| 731 | // result. |
| 732 | if (m_curr_pid == LLDB_INVALID_PROCESS_ID) { |
| 733 | std::vector<lldb::tid_t> thread_ids; |
| 734 | bool sequence_mutex_unavailable; |
| 735 | size_t size; |
| 736 | size = GetCurrentThreadIDs(thread_ids, sequence_mutex_unavailable); |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 737 | if (size && !sequence_mutex_unavailable) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 738 | m_curr_pid = thread_ids.front(); |
| 739 | m_curr_pid_is_valid = eLazyBoolYes; |
| 740 | return m_curr_pid; |
| 741 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 742 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | return LLDB_INVALID_PROCESS_ID; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 748 | bool GDBRemoteCommunicationClient::GetLaunchSuccess(std::string &error_str) { |
| 749 | error_str.clear(); |
| 750 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 751 | if (SendPacketAndWaitForResponse("qLaunchSuccess", response, false) == |
| 752 | PacketResult::Success) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 753 | if (response.IsOKResponse()) |
| 754 | return true; |
| 755 | if (response.GetChar() == 'E') { |
| 756 | // A string the describes what failed when launching... |
| 757 | error_str = response.GetStringRef().substr(1); |
| 758 | } else { |
| 759 | error_str.assign("unknown error occurred launching process"); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 760 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 761 | } else { |
| 762 | error_str.assign("timed out waiting for app to launch"); |
| 763 | } |
| 764 | return false; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 767 | int GDBRemoteCommunicationClient::SendArgumentsPacket( |
| 768 | const ProcessLaunchInfo &launch_info) { |
| 769 | // Since we don't get the send argv0 separate from the executable path, we |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 770 | // need to make sure to use the actual executable path found in the |
| 771 | // launch_info... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 772 | std::vector<const char *> argv; |
| 773 | FileSpec exe_file = launch_info.GetExecutableFile(); |
| 774 | std::string exe_path; |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 775 | const char *arg = nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 776 | const Args &launch_args = launch_info.GetArguments(); |
| 777 | if (exe_file) |
| 778 | exe_path = exe_file.GetPath(false); |
| 779 | else { |
| 780 | arg = launch_args.GetArgumentAtIndex(0); |
| 781 | if (arg) |
| 782 | exe_path = arg; |
| 783 | } |
| 784 | if (!exe_path.empty()) { |
| 785 | argv.push_back(exe_path.c_str()); |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 786 | for (uint32_t i = 1; (arg = launch_args.GetArgumentAtIndex(i)) != nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 787 | ++i) { |
| 788 | if (arg) |
| 789 | argv.push_back(arg); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 790 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 791 | } |
| 792 | if (!argv.empty()) { |
Vince Harron | e0be425 | 2015-02-06 18:32:57 +0000 | [diff] [blame] | 793 | StreamString packet; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 794 | packet.PutChar('A'); |
| 795 | for (size_t i = 0, n = argv.size(); i < n; ++i) { |
| 796 | arg = argv[i]; |
| 797 | const int arg_len = strlen(arg); |
| 798 | if (i > 0) |
| 799 | packet.PutChar(','); |
| 800 | packet.Printf("%i,%i,", arg_len * 2, (int)i); |
| 801 | packet.PutBytesAsRawHex8(arg, arg_len); |
| 802 | } |
| 803 | |
Vince Harron | e0be425 | 2015-02-06 18:32:57 +0000 | [diff] [blame] | 804 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 805 | if (SendPacketAndWaitForResponse(packet.GetString(), response, false) == |
| 806 | PacketResult::Success) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 807 | if (response.IsOKResponse()) |
Vince Harron | e0be425 | 2015-02-06 18:32:57 +0000 | [diff] [blame] | 808 | return 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 809 | uint8_t error = response.GetError(); |
| 810 | if (error) |
Johnny Chen | 6463720 | 2012-05-23 21:09:52 +0000 | [diff] [blame] | 811 | return error; |
| 812 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 813 | } |
| 814 | return -1; |
| 815 | } |
Johnny Chen | 6463720 | 2012-05-23 21:09:52 +0000 | [diff] [blame] | 816 | |
Pavel Labath | 62930e5 | 2018-01-10 11:57:31 +0000 | [diff] [blame] | 817 | int GDBRemoteCommunicationClient::SendEnvironment(const Environment &env) { |
| 818 | for (const auto &KV : env) { |
| 819 | int r = SendEnvironmentPacket(Environment::compose(KV).c_str()); |
| 820 | if (r != 0) |
| 821 | return r; |
| 822 | } |
| 823 | return 0; |
| 824 | } |
| 825 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 826 | int GDBRemoteCommunicationClient::SendEnvironmentPacket( |
| 827 | char const *name_equal_value) { |
| 828 | if (name_equal_value && name_equal_value[0]) { |
| 829 | StreamString packet; |
| 830 | bool send_hex_encoding = false; |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 831 | for (const char *p = name_equal_value; *p != '\0' && !send_hex_encoding; |
| 832 | ++p) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 833 | if (isprint(*p)) { |
| 834 | switch (*p) { |
| 835 | case '$': |
| 836 | case '#': |
| 837 | case '*': |
| 838 | case '}': |
| 839 | send_hex_encoding = true; |
| 840 | break; |
| 841 | default: |
| 842 | break; |
Johnny Chen | 6463720 | 2012-05-23 21:09:52 +0000 | [diff] [blame] | 843 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 844 | } else { |
| 845 | // We have non printable characters, lets hex encode this... |
| 846 | send_hex_encoding = true; |
| 847 | } |
Johnny Chen | 6463720 | 2012-05-23 21:09:52 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 850 | StringExtractorGDBRemote response; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 851 | if (send_hex_encoding) { |
| 852 | if (m_supports_QEnvironmentHexEncoded) { |
| 853 | packet.PutCString("QEnvironmentHexEncoded:"); |
| 854 | packet.PutBytesAsRawHex8(name_equal_value, strlen(name_equal_value)); |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 855 | if (SendPacketAndWaitForResponse(packet.GetString(), response, false) == |
| 856 | PacketResult::Success) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 857 | if (response.IsOKResponse()) |
| 858 | return 0; |
| 859 | uint8_t error = response.GetError(); |
| 860 | if (error) |
| 861 | return error; |
| 862 | if (response.IsUnsupportedResponse()) |
| 863 | m_supports_QEnvironmentHexEncoded = false; |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | } else if (m_supports_QEnvironment) { |
| 868 | packet.Printf("QEnvironment:%s", name_equal_value); |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 869 | if (SendPacketAndWaitForResponse(packet.GetString(), response, false) == |
| 870 | PacketResult::Success) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 871 | if (response.IsOKResponse()) |
| 872 | return 0; |
| 873 | uint8_t error = response.GetError(); |
| 874 | if (error) |
| 875 | return error; |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 876 | if (response.IsUnsupportedResponse()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 877 | m_supports_QEnvironment = false; |
| 878 | } |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 879 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 880 | } |
| 881 | return -1; |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 884 | int GDBRemoteCommunicationClient::SendLaunchArchPacket(char const *arch) { |
| 885 | if (arch && arch[0]) { |
| 886 | StreamString packet; |
| 887 | packet.Printf("QLaunchArch:%s", arch); |
| 888 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 889 | if (SendPacketAndWaitForResponse(packet.GetString(), response, false) == |
| 890 | PacketResult::Success) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 891 | if (response.IsOKResponse()) |
| 892 | return 0; |
| 893 | uint8_t error = response.GetError(); |
| 894 | if (error) |
| 895 | return error; |
| 896 | } |
| 897 | } |
| 898 | return -1; |
| 899 | } |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 900 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 901 | int GDBRemoteCommunicationClient::SendLaunchEventDataPacket( |
| 902 | char const *data, bool *was_supported) { |
| 903 | if (data && *data != '\0') { |
| 904 | StreamString packet; |
| 905 | packet.Printf("QSetProcessEvent:%s", data); |
| 906 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 907 | if (SendPacketAndWaitForResponse(packet.GetString(), response, false) == |
| 908 | PacketResult::Success) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 909 | if (response.IsOKResponse()) { |
| 910 | if (was_supported) |
| 911 | *was_supported = true; |
| 912 | return 0; |
| 913 | } else if (response.IsUnsupportedResponse()) { |
| 914 | if (was_supported) |
| 915 | *was_supported = false; |
| 916 | return -1; |
| 917 | } else { |
| 918 | uint8_t error = response.GetError(); |
| 919 | if (was_supported) |
| 920 | *was_supported = true; |
| 921 | if (error) |
| 922 | return error; |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | return -1; |
| 927 | } |
| 928 | |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 929 | llvm::VersionTuple GDBRemoteCommunicationClient::GetOSVersion() { |
| 930 | GetHostInfo(); |
| 931 | return m_os_version; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 932 | } |
| 933 | |
Adrian Prantl | 2461061 | 2019-09-04 17:23:15 +0000 | [diff] [blame] | 934 | llvm::VersionTuple GDBRemoteCommunicationClient::GetMacCatalystVersion() { |
| 935 | GetHostInfo(); |
| 936 | return m_maccatalyst_version; |
| 937 | } |
| 938 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 939 | bool GDBRemoteCommunicationClient::GetOSBuildString(std::string &s) { |
| 940 | if (GetHostInfo()) { |
| 941 | if (!m_os_build.empty()) { |
| 942 | s = m_os_build; |
| 943 | return true; |
| 944 | } |
| 945 | } |
| 946 | s.clear(); |
| 947 | return false; |
| 948 | } |
| 949 | |
| 950 | bool GDBRemoteCommunicationClient::GetOSKernelDescription(std::string &s) { |
| 951 | if (GetHostInfo()) { |
| 952 | if (!m_os_kernel.empty()) { |
| 953 | s = m_os_kernel; |
| 954 | return true; |
| 955 | } |
| 956 | } |
| 957 | s.clear(); |
| 958 | return false; |
| 959 | } |
| 960 | |
| 961 | bool GDBRemoteCommunicationClient::GetHostname(std::string &s) { |
| 962 | if (GetHostInfo()) { |
| 963 | if (!m_hostname.empty()) { |
| 964 | s = m_hostname; |
| 965 | return true; |
| 966 | } |
| 967 | } |
| 968 | s.clear(); |
| 969 | return false; |
| 970 | } |
| 971 | |
| 972 | ArchSpec GDBRemoteCommunicationClient::GetSystemArchitecture() { |
| 973 | if (GetHostInfo()) |
| 974 | return m_host_arch; |
| 975 | return ArchSpec(); |
| 976 | } |
| 977 | |
| 978 | const lldb_private::ArchSpec & |
| 979 | GDBRemoteCommunicationClient::GetProcessArchitecture() { |
| 980 | if (m_qProcessInfo_is_valid == eLazyBoolCalculate) |
| 981 | GetCurrentProcessInfo(); |
| 982 | return m_process_arch; |
| 983 | } |
| 984 | |
| 985 | bool GDBRemoteCommunicationClient::GetGDBServerVersion() { |
| 986 | if (m_qGDBServerVersion_is_valid == eLazyBoolCalculate) { |
| 987 | m_gdb_server_name.clear(); |
| 988 | m_gdb_server_version = 0; |
| 989 | m_qGDBServerVersion_is_valid = eLazyBoolNo; |
| 990 | |
| 991 | StringExtractorGDBRemote response; |
| 992 | if (SendPacketAndWaitForResponse("qGDBServerVersion", response, false) == |
| 993 | PacketResult::Success) { |
| 994 | if (response.IsNormalResponse()) { |
| 995 | llvm::StringRef name, value; |
| 996 | bool success = false; |
| 997 | while (response.GetNameColonValue(name, value)) { |
| 998 | if (name.equals("name")) { |
| 999 | success = true; |
| 1000 | m_gdb_server_name = value; |
| 1001 | } else if (name.equals("version")) { |
| 1002 | llvm::StringRef major, minor; |
| 1003 | std::tie(major, minor) = value.split('.'); |
| 1004 | if (!major.getAsInteger(0, m_gdb_server_version)) |
| 1005 | success = true; |
| 1006 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1007 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1008 | if (success) |
| 1009 | m_qGDBServerVersion_is_valid = eLazyBoolYes; |
| 1010 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1011 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1012 | } |
| 1013 | return m_qGDBServerVersion_is_valid == eLazyBoolYes; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1016 | void GDBRemoteCommunicationClient::MaybeEnableCompression( |
| 1017 | std::vector<std::string> supported_compressions) { |
| 1018 | CompressionType avail_type = CompressionType::None; |
| 1019 | std::string avail_name; |
| 1020 | |
| 1021 | #if defined(HAVE_LIBCOMPRESSION) |
Vedant Kumar | 606908a | 2017-12-06 19:21:10 +0000 | [diff] [blame] | 1022 | if (avail_type == CompressionType::None) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1023 | for (auto compression : supported_compressions) { |
| 1024 | if (compression == "lzfse") { |
| 1025 | avail_type = CompressionType::LZFSE; |
| 1026 | avail_name = compression; |
| 1027 | break; |
| 1028 | } |
| 1029 | } |
| 1030 | } |
| 1031 | #endif |
| 1032 | |
| 1033 | #if defined(HAVE_LIBCOMPRESSION) |
Vedant Kumar | 606908a | 2017-12-06 19:21:10 +0000 | [diff] [blame] | 1034 | if (avail_type == CompressionType::None) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1035 | for (auto compression : supported_compressions) { |
| 1036 | if (compression == "zlib-deflate") { |
| 1037 | avail_type = CompressionType::ZlibDeflate; |
| 1038 | avail_name = compression; |
| 1039 | break; |
| 1040 | } |
| 1041 | } |
| 1042 | } |
| 1043 | #endif |
| 1044 | |
| 1045 | #if defined(HAVE_LIBZ) |
| 1046 | if (avail_type == CompressionType::None) { |
| 1047 | for (auto compression : supported_compressions) { |
| 1048 | if (compression == "zlib-deflate") { |
| 1049 | avail_type = CompressionType::ZlibDeflate; |
| 1050 | avail_name = compression; |
| 1051 | break; |
| 1052 | } |
| 1053 | } |
| 1054 | } |
| 1055 | #endif |
| 1056 | |
| 1057 | #if defined(HAVE_LIBCOMPRESSION) |
Vedant Kumar | 606908a | 2017-12-06 19:21:10 +0000 | [diff] [blame] | 1058 | if (avail_type == CompressionType::None) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1059 | for (auto compression : supported_compressions) { |
| 1060 | if (compression == "lz4") { |
| 1061 | avail_type = CompressionType::LZ4; |
| 1062 | avail_name = compression; |
| 1063 | break; |
| 1064 | } |
| 1065 | } |
| 1066 | } |
| 1067 | #endif |
| 1068 | |
| 1069 | #if defined(HAVE_LIBCOMPRESSION) |
Vedant Kumar | 606908a | 2017-12-06 19:21:10 +0000 | [diff] [blame] | 1070 | if (avail_type == CompressionType::None) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1071 | for (auto compression : supported_compressions) { |
| 1072 | if (compression == "lzma") { |
| 1073 | avail_type = CompressionType::LZMA; |
| 1074 | avail_name = compression; |
| 1075 | break; |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | #endif |
| 1080 | |
| 1081 | if (avail_type != CompressionType::None) { |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1082 | StringExtractorGDBRemote response; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1083 | std::string packet = "QEnableCompression:type:" + avail_name + ";"; |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 1084 | if (SendPacketAndWaitForResponse(packet, response, false) != |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1085 | PacketResult::Success) |
| 1086 | return; |
| 1087 | |
| 1088 | if (response.IsOKResponse()) { |
| 1089 | m_compression_type = avail_type; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1090 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1091 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1092 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1093 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1094 | const char *GDBRemoteCommunicationClient::GetGDBServerProgramName() { |
| 1095 | if (GetGDBServerVersion()) { |
| 1096 | if (!m_gdb_server_name.empty()) |
| 1097 | return m_gdb_server_name.c_str(); |
| 1098 | } |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 1099 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | uint32_t GDBRemoteCommunicationClient::GetGDBServerProgramVersion() { |
| 1103 | if (GetGDBServerVersion()) |
| 1104 | return m_gdb_server_version; |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
| 1108 | bool GDBRemoteCommunicationClient::GetDefaultThreadId(lldb::tid_t &tid) { |
| 1109 | StringExtractorGDBRemote response; |
| 1110 | if (SendPacketAndWaitForResponse("qC", response, false) != |
| 1111 | PacketResult::Success) |
| 1112 | return false; |
| 1113 | |
| 1114 | if (!response.IsNormalResponse()) |
| 1115 | return false; |
| 1116 | |
| 1117 | if (response.GetChar() == 'Q' && response.GetChar() == 'C') |
| 1118 | tid = response.GetHexMaxU32(true, -1); |
| 1119 | |
| 1120 | return true; |
| 1121 | } |
| 1122 | |
| 1123 | bool GDBRemoteCommunicationClient::GetHostInfo(bool force) { |
| 1124 | Log *log(ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet(GDBR_LOG_PROCESS)); |
| 1125 | |
| 1126 | if (force || m_qHostInfo_is_valid == eLazyBoolCalculate) { |
Pavel Labath | e7ec083 | 2018-08-31 05:34:03 +0000 | [diff] [blame] | 1127 | // host info computation can require DNS traffic and shelling out to external processes. |
| 1128 | // Increase the timeout to account for that. |
| 1129 | ScopedTimeout timeout(*this, seconds(10)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1130 | m_qHostInfo_is_valid = eLazyBoolNo; |
Jim Ingham | 106d028 | 2014-06-25 02:32:56 +0000 | [diff] [blame] | 1131 | StringExtractorGDBRemote response; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1132 | if (SendPacketAndWaitForResponse("qHostInfo", response, false) == |
| 1133 | PacketResult::Success) { |
| 1134 | if (response.IsNormalResponse()) { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 1135 | llvm::StringRef name; |
| 1136 | llvm::StringRef value; |
Jason Molenda | 89c3749 | 2014-01-27 22:23:20 +0000 | [diff] [blame] | 1137 | uint32_t cpu = LLDB_INVALID_CPUTYPE; |
| 1138 | uint32_t sub = 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1139 | std::string arch_name; |
| 1140 | std::string os_name; |
Adrian Prantl | 2461061 | 2019-09-04 17:23:15 +0000 | [diff] [blame] | 1141 | std::string environment; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1142 | std::string vendor_name; |
| 1143 | std::string triple; |
| 1144 | std::string distribution_id; |
| 1145 | uint32_t pointer_byte_size = 0; |
| 1146 | ByteOrder byte_order = eByteOrderInvalid; |
| 1147 | uint32_t num_keys_decoded = 0; |
| 1148 | while (response.GetNameColonValue(name, value)) { |
| 1149 | if (name.equals("cputype")) { |
| 1150 | // exception type in big endian hex |
| 1151 | if (!value.getAsInteger(0, cpu)) |
| 1152 | ++num_keys_decoded; |
| 1153 | } else if (name.equals("cpusubtype")) { |
| 1154 | // exception count in big endian hex |
| 1155 | if (!value.getAsInteger(0, sub)) |
| 1156 | ++num_keys_decoded; |
| 1157 | } else if (name.equals("arch")) { |
| 1158 | arch_name = value; |
| 1159 | ++num_keys_decoded; |
| 1160 | } else if (name.equals("triple")) { |
| 1161 | StringExtractor extractor(value); |
| 1162 | extractor.GetHexByteString(triple); |
| 1163 | ++num_keys_decoded; |
| 1164 | } else if (name.equals("distribution_id")) { |
| 1165 | StringExtractor extractor(value); |
| 1166 | extractor.GetHexByteString(distribution_id); |
| 1167 | ++num_keys_decoded; |
| 1168 | } else if (name.equals("os_build")) { |
| 1169 | StringExtractor extractor(value); |
| 1170 | extractor.GetHexByteString(m_os_build); |
| 1171 | ++num_keys_decoded; |
| 1172 | } else if (name.equals("hostname")) { |
| 1173 | StringExtractor extractor(value); |
| 1174 | extractor.GetHexByteString(m_hostname); |
| 1175 | ++num_keys_decoded; |
| 1176 | } else if (name.equals("os_kernel")) { |
| 1177 | StringExtractor extractor(value); |
| 1178 | extractor.GetHexByteString(m_os_kernel); |
| 1179 | ++num_keys_decoded; |
| 1180 | } else if (name.equals("ostype")) { |
Adrian Prantl | 2461061 | 2019-09-04 17:23:15 +0000 | [diff] [blame] | 1181 | if (value.equals("maccatalyst")) { |
| 1182 | os_name = "ios"; |
| 1183 | environment = "macabi"; |
| 1184 | } else |
| 1185 | os_name = value; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1186 | ++num_keys_decoded; |
| 1187 | } else if (name.equals("vendor")) { |
| 1188 | vendor_name = value; |
| 1189 | ++num_keys_decoded; |
| 1190 | } else if (name.equals("endian")) { |
| 1191 | byte_order = llvm::StringSwitch<lldb::ByteOrder>(value) |
| 1192 | .Case("little", eByteOrderLittle) |
| 1193 | .Case("big", eByteOrderBig) |
| 1194 | .Case("pdp", eByteOrderPDP) |
| 1195 | .Default(eByteOrderInvalid); |
| 1196 | if (byte_order != eByteOrderInvalid) |
| 1197 | ++num_keys_decoded; |
| 1198 | } else if (name.equals("ptrsize")) { |
| 1199 | if (!value.getAsInteger(0, pointer_byte_size)) |
| 1200 | ++num_keys_decoded; |
| 1201 | } else if (name.equals("os_version") || |
| 1202 | name.equals( |
| 1203 | "version")) // Older debugserver binaries used the |
| 1204 | // "version" key instead of |
| 1205 | // "os_version"... |
| 1206 | { |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 1207 | if (!m_os_version.tryParse(value)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1208 | ++num_keys_decoded; |
Adrian Prantl | 2461061 | 2019-09-04 17:23:15 +0000 | [diff] [blame] | 1209 | } else if (name.equals("maccatalyst_version")) { |
| 1210 | if (!m_maccatalyst_version.tryParse(value)) |
| 1211 | ++num_keys_decoded; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1212 | } else if (name.equals("watchpoint_exceptions_received")) { |
| 1213 | m_watchpoints_trigger_after_instruction = |
| 1214 | llvm::StringSwitch<LazyBool>(value) |
| 1215 | .Case("before", eLazyBoolNo) |
| 1216 | .Case("after", eLazyBoolYes) |
| 1217 | .Default(eLazyBoolCalculate); |
| 1218 | if (m_watchpoints_trigger_after_instruction != eLazyBoolCalculate) |
| 1219 | ++num_keys_decoded; |
| 1220 | } else if (name.equals("default_packet_timeout")) { |
Pavel Labath | 3aa0491 | 2016-10-31 17:19:42 +0000 | [diff] [blame] | 1221 | uint32_t timeout_seconds; |
| 1222 | if (!value.getAsInteger(0, timeout_seconds)) { |
Pavel Labath | 1eff73c | 2016-11-24 10:54:49 +0000 | [diff] [blame] | 1223 | m_default_packet_timeout = seconds(timeout_seconds); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1224 | SetPacketTimeout(m_default_packet_timeout); |
| 1225 | ++num_keys_decoded; |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1226 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1227 | } |
Jason Molenda | 89c3749 | 2014-01-27 22:23:20 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1230 | if (num_keys_decoded > 0) |
| 1231 | m_qHostInfo_is_valid = eLazyBoolYes; |
| 1232 | |
| 1233 | if (triple.empty()) { |
| 1234 | if (arch_name.empty()) { |
| 1235 | if (cpu != LLDB_INVALID_CPUTYPE) { |
| 1236 | m_host_arch.SetArchitecture(eArchTypeMachO, cpu, sub); |
| 1237 | if (pointer_byte_size) { |
| 1238 | assert(pointer_byte_size == m_host_arch.GetAddressByteSize()); |
| 1239 | } |
| 1240 | if (byte_order != eByteOrderInvalid) { |
| 1241 | assert(byte_order == m_host_arch.GetByteOrder()); |
| 1242 | } |
| 1243 | |
| 1244 | if (!vendor_name.empty()) |
| 1245 | m_host_arch.GetTriple().setVendorName( |
| 1246 | llvm::StringRef(vendor_name)); |
| 1247 | if (!os_name.empty()) |
| 1248 | m_host_arch.GetTriple().setOSName(llvm::StringRef(os_name)); |
Adrian Prantl | 2461061 | 2019-09-04 17:23:15 +0000 | [diff] [blame] | 1249 | if (!environment.empty()) |
| 1250 | m_host_arch.GetTriple().setEnvironmentName(environment); |
Jason Molenda | 89c3749 | 2014-01-27 22:23:20 +0000 | [diff] [blame] | 1251 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1252 | } else { |
Jason Molenda | f17b5ac | 2012-12-19 02:54:03 +0000 | [diff] [blame] | 1253 | std::string triple; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1254 | triple += arch_name; |
| 1255 | if (!vendor_name.empty() || !os_name.empty()) { |
| 1256 | triple += '-'; |
| 1257 | if (vendor_name.empty()) |
| 1258 | triple += "unknown"; |
| 1259 | else |
| 1260 | triple += vendor_name; |
| 1261 | triple += '-'; |
| 1262 | if (os_name.empty()) |
| 1263 | triple += "unknown"; |
| 1264 | else |
| 1265 | triple += os_name; |
Jason Molenda | f17b5ac | 2012-12-19 02:54:03 +0000 | [diff] [blame] | 1266 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1267 | m_host_arch.SetTriple(triple.c_str()); |
Todd Fiala | c540dd0 | 2014-08-26 18:21:02 +0000 | [diff] [blame] | 1268 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1269 | llvm::Triple &host_triple = m_host_arch.GetTriple(); |
| 1270 | if (host_triple.getVendor() == llvm::Triple::Apple && |
| 1271 | host_triple.getOS() == llvm::Triple::Darwin) { |
| 1272 | switch (m_host_arch.GetMachine()) { |
| 1273 | case llvm::Triple::aarch64: |
Jason Molenda | 7dd7a36 | 2019-10-16 19:14:49 +0000 | [diff] [blame] | 1274 | case llvm::Triple::aarch64_32: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1275 | case llvm::Triple::arm: |
| 1276 | case llvm::Triple::thumb: |
| 1277 | host_triple.setOS(llvm::Triple::IOS); |
Greg Clayton | adc00cb | 2011-05-20 23:38:13 +0000 | [diff] [blame] | 1278 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1279 | default: |
| 1280 | host_triple.setOS(llvm::Triple::MacOSX); |
| 1281 | break; |
| 1282 | } |
Greg Clayton | adc00cb | 2011-05-20 23:38:13 +0000 | [diff] [blame] | 1283 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1284 | if (pointer_byte_size) { |
| 1285 | assert(pointer_byte_size == m_host_arch.GetAddressByteSize()); |
| 1286 | } |
| 1287 | if (byte_order != eByteOrderInvalid) { |
| 1288 | assert(byte_order == m_host_arch.GetByteOrder()); |
| 1289 | } |
| 1290 | } |
| 1291 | } else { |
| 1292 | m_host_arch.SetTriple(triple.c_str()); |
| 1293 | if (pointer_byte_size) { |
| 1294 | assert(pointer_byte_size == m_host_arch.GetAddressByteSize()); |
| 1295 | } |
| 1296 | if (byte_order != eByteOrderInvalid) { |
| 1297 | assert(byte_order == m_host_arch.GetByteOrder()); |
| 1298 | } |
Jaydeep Patil | 630dd7f | 2015-09-18 05:32:54 +0000 | [diff] [blame] | 1299 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1300 | LLDB_LOGF(log, |
| 1301 | "GDBRemoteCommunicationClient::%s parsed host " |
| 1302 | "architecture as %s, triple as %s from triple text %s", |
| 1303 | __FUNCTION__, |
| 1304 | m_host_arch.GetArchitectureName() |
| 1305 | ? m_host_arch.GetArchitectureName() |
| 1306 | : "<null-arch-name>", |
| 1307 | m_host_arch.GetTriple().getTriple().c_str(), |
| 1308 | triple.c_str()); |
Jaydeep Patil | 630dd7f | 2015-09-18 05:32:54 +0000 | [diff] [blame] | 1309 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1310 | if (!distribution_id.empty()) |
| 1311 | m_host_arch.SetDistributionId(distribution_id.c_str()); |
| 1312 | } |
Greg Clayton | adc00cb | 2011-05-20 23:38:13 +0000 | [diff] [blame] | 1313 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1314 | } |
| 1315 | return m_qHostInfo_is_valid == eLazyBoolYes; |
Greg Clayton | adc00cb | 2011-05-20 23:38:13 +0000 | [diff] [blame] | 1316 | } |
Greg Clayton | 37a0a24 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 1317 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1318 | int GDBRemoteCommunicationClient::SendAttach( |
| 1319 | lldb::pid_t pid, StringExtractorGDBRemote &response) { |
| 1320 | if (pid != LLDB_INVALID_PROCESS_ID) { |
| 1321 | char packet[64]; |
| 1322 | const int packet_len = |
| 1323 | ::snprintf(packet, sizeof(packet), "vAttach;%" PRIx64, pid); |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 1324 | UNUSED_IF_ASSERT_DISABLED(packet_len); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1325 | assert(packet_len < (int)sizeof(packet)); |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 1326 | if (SendPacketAndWaitForResponse(packet, response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1327 | PacketResult::Success) { |
| 1328 | if (response.IsErrorResponse()) |
| 1329 | return response.GetError(); |
| 1330 | return 0; |
| 1331 | } |
| 1332 | } |
| 1333 | return -1; |
| 1334 | } |
| 1335 | |
| 1336 | int GDBRemoteCommunicationClient::SendStdinNotification(const char *data, |
| 1337 | size_t data_len) { |
| 1338 | StreamString packet; |
| 1339 | packet.PutCString("I"); |
| 1340 | packet.PutBytesAsRawHex8(data, data_len); |
| 1341 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 1342 | if (SendPacketAndWaitForResponse(packet.GetString(), response, false) == |
| 1343 | PacketResult::Success) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1344 | return 0; |
| 1345 | } |
| 1346 | return response.GetError(); |
| 1347 | } |
| 1348 | |
| 1349 | const lldb_private::ArchSpec & |
| 1350 | GDBRemoteCommunicationClient::GetHostArchitecture() { |
| 1351 | if (m_qHostInfo_is_valid == eLazyBoolCalculate) |
| 1352 | GetHostInfo(); |
| 1353 | return m_host_arch; |
| 1354 | } |
| 1355 | |
Pavel Labath | 1eff73c | 2016-11-24 10:54:49 +0000 | [diff] [blame] | 1356 | seconds GDBRemoteCommunicationClient::GetHostDefaultPacketTimeout() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1357 | if (m_qHostInfo_is_valid == eLazyBoolCalculate) |
| 1358 | GetHostInfo(); |
| 1359 | return m_default_packet_timeout; |
| 1360 | } |
| 1361 | |
| 1362 | addr_t GDBRemoteCommunicationClient::AllocateMemory(size_t size, |
| 1363 | uint32_t permissions) { |
| 1364 | if (m_supports_alloc_dealloc_memory != eLazyBoolNo) { |
| 1365 | m_supports_alloc_dealloc_memory = eLazyBoolYes; |
| 1366 | char packet[64]; |
| 1367 | const int packet_len = ::snprintf( |
| 1368 | packet, sizeof(packet), "_M%" PRIx64 ",%s%s%s", (uint64_t)size, |
| 1369 | permissions & lldb::ePermissionsReadable ? "r" : "", |
| 1370 | permissions & lldb::ePermissionsWritable ? "w" : "", |
| 1371 | permissions & lldb::ePermissionsExecutable ? "x" : ""); |
| 1372 | assert(packet_len < (int)sizeof(packet)); |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 1373 | UNUSED_IF_ASSERT_DISABLED(packet_len); |
Pavel Labath | 83082a0 | 2016-08-18 14:33:55 +0000 | [diff] [blame] | 1374 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 1375 | if (SendPacketAndWaitForResponse(packet, response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1376 | PacketResult::Success) { |
| 1377 | if (response.IsUnsupportedResponse()) |
| 1378 | m_supports_alloc_dealloc_memory = eLazyBoolNo; |
| 1379 | else if (!response.IsErrorResponse()) |
| 1380 | return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS); |
| 1381 | } else { |
| 1382 | m_supports_alloc_dealloc_memory = eLazyBoolNo; |
| 1383 | } |
| 1384 | } |
| 1385 | return LLDB_INVALID_ADDRESS; |
| 1386 | } |
| 1387 | |
| 1388 | bool GDBRemoteCommunicationClient::DeallocateMemory(addr_t addr) { |
| 1389 | if (m_supports_alloc_dealloc_memory != eLazyBoolNo) { |
| 1390 | m_supports_alloc_dealloc_memory = eLazyBoolYes; |
| 1391 | char packet[64]; |
| 1392 | const int packet_len = |
| 1393 | ::snprintf(packet, sizeof(packet), "_m%" PRIx64, (uint64_t)addr); |
| 1394 | assert(packet_len < (int)sizeof(packet)); |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 1395 | UNUSED_IF_ASSERT_DISABLED(packet_len); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1396 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 1397 | if (SendPacketAndWaitForResponse(packet, response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1398 | PacketResult::Success) { |
| 1399 | if (response.IsUnsupportedResponse()) |
| 1400 | m_supports_alloc_dealloc_memory = eLazyBoolNo; |
| 1401 | else if (response.IsOKResponse()) |
| 1402 | return true; |
| 1403 | } else { |
| 1404 | m_supports_alloc_dealloc_memory = eLazyBoolNo; |
| 1405 | } |
| 1406 | } |
| 1407 | return false; |
| 1408 | } |
| 1409 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1410 | Status GDBRemoteCommunicationClient::Detach(bool keep_stopped) { |
| 1411 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1412 | |
| 1413 | if (keep_stopped) { |
| 1414 | if (m_supports_detach_stay_stopped == eLazyBoolCalculate) { |
| 1415 | char packet[64]; |
| 1416 | const int packet_len = |
| 1417 | ::snprintf(packet, sizeof(packet), "qSupportsDetachAndStayStopped:"); |
| 1418 | assert(packet_len < (int)sizeof(packet)); |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 1419 | UNUSED_IF_ASSERT_DISABLED(packet_len); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1420 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 1421 | if (SendPacketAndWaitForResponse(packet, response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1422 | PacketResult::Success && |
| 1423 | response.IsOKResponse()) { |
| 1424 | m_supports_detach_stay_stopped = eLazyBoolYes; |
| 1425 | } else { |
| 1426 | m_supports_detach_stay_stopped = eLazyBoolNo; |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | if (m_supports_detach_stay_stopped == eLazyBoolNo) { |
| 1431 | error.SetErrorString("Stays stopped not supported by this target."); |
| 1432 | return error; |
| 1433 | } else { |
| 1434 | StringExtractorGDBRemote response; |
| 1435 | PacketResult packet_result = |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 1436 | SendPacketAndWaitForResponse("D1", response, false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1437 | if (packet_result != PacketResult::Success) |
| 1438 | error.SetErrorString("Sending extended disconnect packet failed."); |
| 1439 | } |
| 1440 | } else { |
| 1441 | StringExtractorGDBRemote response; |
| 1442 | PacketResult packet_result = |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 1443 | SendPacketAndWaitForResponse("D", response, false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1444 | if (packet_result != PacketResult::Success) |
| 1445 | error.SetErrorString("Sending disconnect packet failed."); |
| 1446 | } |
| 1447 | return error; |
| 1448 | } |
| 1449 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1450 | Status GDBRemoteCommunicationClient::GetMemoryRegionInfo( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1451 | lldb::addr_t addr, lldb_private::MemoryRegionInfo ®ion_info) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1452 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1453 | region_info.Clear(); |
| 1454 | |
| 1455 | if (m_supports_memory_region_info != eLazyBoolNo) { |
| 1456 | m_supports_memory_region_info = eLazyBoolYes; |
| 1457 | char packet[64]; |
| 1458 | const int packet_len = ::snprintf( |
| 1459 | packet, sizeof(packet), "qMemoryRegionInfo:%" PRIx64, (uint64_t)addr); |
| 1460 | assert(packet_len < (int)sizeof(packet)); |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 1461 | UNUSED_IF_ASSERT_DISABLED(packet_len); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1462 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 1463 | if (SendPacketAndWaitForResponse(packet, response, false) == |
Pavel Labath | 16064d3 | 2018-03-20 11:56:24 +0000 | [diff] [blame] | 1464 | PacketResult::Success && |
| 1465 | response.GetResponseType() == StringExtractorGDBRemote::eResponse) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1466 | llvm::StringRef name; |
| 1467 | llvm::StringRef value; |
| 1468 | addr_t addr_value = LLDB_INVALID_ADDRESS; |
| 1469 | bool success = true; |
| 1470 | bool saw_permissions = false; |
| 1471 | while (success && response.GetNameColonValue(name, value)) { |
| 1472 | if (name.equals("start")) { |
| 1473 | if (!value.getAsInteger(16, addr_value)) |
| 1474 | region_info.GetRange().SetRangeBase(addr_value); |
| 1475 | } else if (name.equals("size")) { |
| 1476 | if (!value.getAsInteger(16, addr_value)) |
| 1477 | region_info.GetRange().SetByteSize(addr_value); |
| 1478 | } else if (name.equals("permissions") && |
| 1479 | region_info.GetRange().IsValid()) { |
| 1480 | saw_permissions = true; |
| 1481 | if (region_info.GetRange().Contains(addr)) { |
| 1482 | if (value.find('r') != llvm::StringRef::npos) |
| 1483 | region_info.SetReadable(MemoryRegionInfo::eYes); |
| 1484 | else |
| 1485 | region_info.SetReadable(MemoryRegionInfo::eNo); |
| 1486 | |
| 1487 | if (value.find('w') != llvm::StringRef::npos) |
| 1488 | region_info.SetWritable(MemoryRegionInfo::eYes); |
| 1489 | else |
| 1490 | region_info.SetWritable(MemoryRegionInfo::eNo); |
| 1491 | |
| 1492 | if (value.find('x') != llvm::StringRef::npos) |
| 1493 | region_info.SetExecutable(MemoryRegionInfo::eYes); |
| 1494 | else |
| 1495 | region_info.SetExecutable(MemoryRegionInfo::eNo); |
| 1496 | |
| 1497 | region_info.SetMapped(MemoryRegionInfo::eYes); |
| 1498 | } else { |
| 1499 | // The reported region does not contain this address -- we're |
| 1500 | // looking at an unmapped page |
| 1501 | region_info.SetReadable(MemoryRegionInfo::eNo); |
| 1502 | region_info.SetWritable(MemoryRegionInfo::eNo); |
| 1503 | region_info.SetExecutable(MemoryRegionInfo::eNo); |
| 1504 | region_info.SetMapped(MemoryRegionInfo::eNo); |
| 1505 | } |
| 1506 | } else if (name.equals("name")) { |
| 1507 | StringExtractorGDBRemote name_extractor(value); |
| 1508 | std::string name; |
| 1509 | name_extractor.GetHexByteString(name); |
| 1510 | region_info.SetName(name.c_str()); |
| 1511 | } else if (name.equals("error")) { |
| 1512 | StringExtractorGDBRemote error_extractor(value); |
| 1513 | std::string error_string; |
| 1514 | // Now convert the HEX bytes into a string value |
| 1515 | error_extractor.GetHexByteString(error_string); |
| 1516 | error.SetErrorString(error_string.c_str()); |
| 1517 | } |
| 1518 | } |
| 1519 | |
Stephane Sezer | 48d1427 | 2017-03-31 18:00:48 +0000 | [diff] [blame] | 1520 | if (region_info.GetRange().IsValid()) { |
| 1521 | // We got a valid address range back but no permissions -- which means |
| 1522 | // this is an unmapped page |
| 1523 | if (!saw_permissions) { |
| 1524 | region_info.SetReadable(MemoryRegionInfo::eNo); |
| 1525 | region_info.SetWritable(MemoryRegionInfo::eNo); |
| 1526 | region_info.SetExecutable(MemoryRegionInfo::eNo); |
| 1527 | region_info.SetMapped(MemoryRegionInfo::eNo); |
| 1528 | } |
| 1529 | } else { |
| 1530 | // We got an invalid address range back |
| 1531 | error.SetErrorString("Server returned invalid range"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1532 | } |
| 1533 | } else { |
| 1534 | m_supports_memory_region_info = eLazyBoolNo; |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | if (m_supports_memory_region_info == eLazyBoolNo) { |
| 1539 | error.SetErrorString("qMemoryRegionInfo is not supported"); |
| 1540 | } |
Pavel Labath | 16064d3 | 2018-03-20 11:56:24 +0000 | [diff] [blame] | 1541 | |
| 1542 | // Try qXfer:memory-map:read to get region information not included in |
| 1543 | // qMemoryRegionInfo |
| 1544 | MemoryRegionInfo qXfer_region_info; |
| 1545 | Status qXfer_error = GetQXferMemoryMapRegionInfo(addr, qXfer_region_info); |
| 1546 | |
| 1547 | if (error.Fail()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1548 | // If qMemoryRegionInfo failed, but qXfer:memory-map:read succeeded, use |
| 1549 | // the qXfer result as a fallback |
Pavel Labath | 16064d3 | 2018-03-20 11:56:24 +0000 | [diff] [blame] | 1550 | if (qXfer_error.Success()) { |
| 1551 | region_info = qXfer_region_info; |
| 1552 | error.Clear(); |
| 1553 | } else { |
| 1554 | region_info.Clear(); |
| 1555 | } |
| 1556 | } else if (qXfer_error.Success()) { |
| 1557 | // If both qMemoryRegionInfo and qXfer:memory-map:read succeeded, and if |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1558 | // both regions are the same range, update the result to include the flash- |
| 1559 | // memory information that is specific to the qXfer result. |
Pavel Labath | 16064d3 | 2018-03-20 11:56:24 +0000 | [diff] [blame] | 1560 | if (region_info.GetRange() == qXfer_region_info.GetRange()) { |
| 1561 | region_info.SetFlash(qXfer_region_info.GetFlash()); |
| 1562 | region_info.SetBlocksize(qXfer_region_info.GetBlocksize()); |
| 1563 | } |
| 1564 | } |
| 1565 | return error; |
| 1566 | } |
| 1567 | |
| 1568 | Status GDBRemoteCommunicationClient::GetQXferMemoryMapRegionInfo( |
| 1569 | lldb::addr_t addr, MemoryRegionInfo ®ion) { |
| 1570 | Status error = LoadQXferMemoryMap(); |
| 1571 | if (!error.Success()) |
| 1572 | return error; |
| 1573 | for (const auto &map_region : m_qXfer_memory_map) { |
| 1574 | if (map_region.GetRange().Contains(addr)) { |
| 1575 | region = map_region; |
| 1576 | return error; |
| 1577 | } |
| 1578 | } |
| 1579 | error.SetErrorString("Region not found"); |
| 1580 | return error; |
| 1581 | } |
| 1582 | |
| 1583 | Status GDBRemoteCommunicationClient::LoadQXferMemoryMap() { |
| 1584 | |
| 1585 | Status error; |
| 1586 | |
| 1587 | if (m_qXfer_memory_map_loaded) |
| 1588 | // Already loaded, return success |
| 1589 | return error; |
| 1590 | |
| 1591 | if (!XMLDocument::XMLEnabled()) { |
| 1592 | error.SetErrorString("XML is not supported"); |
| 1593 | return error; |
| 1594 | } |
| 1595 | |
| 1596 | if (!GetQXferMemoryMapReadSupported()) { |
| 1597 | error.SetErrorString("Memory map is not supported"); |
| 1598 | return error; |
| 1599 | } |
| 1600 | |
| 1601 | std::string xml; |
| 1602 | lldb_private::Status lldberr; |
| 1603 | if (!ReadExtFeature(ConstString("memory-map"), ConstString(""), xml, |
| 1604 | lldberr)) { |
| 1605 | error.SetErrorString("Failed to read memory map"); |
| 1606 | return error; |
| 1607 | } |
| 1608 | |
| 1609 | XMLDocument xml_document; |
| 1610 | |
| 1611 | if (!xml_document.ParseMemory(xml.c_str(), xml.size())) { |
| 1612 | error.SetErrorString("Failed to parse memory map xml"); |
| 1613 | return error; |
| 1614 | } |
| 1615 | |
| 1616 | XMLNode map_node = xml_document.GetRootElement("memory-map"); |
| 1617 | if (!map_node) { |
| 1618 | error.SetErrorString("Invalid root node in memory map xml"); |
| 1619 | return error; |
| 1620 | } |
| 1621 | |
| 1622 | m_qXfer_memory_map.clear(); |
| 1623 | |
| 1624 | map_node.ForEachChildElement([this](const XMLNode &memory_node) -> bool { |
| 1625 | if (!memory_node.IsElement()) |
| 1626 | return true; |
| 1627 | if (memory_node.GetName() != "memory") |
| 1628 | return true; |
| 1629 | auto type = memory_node.GetAttributeValue("type", ""); |
| 1630 | uint64_t start; |
| 1631 | uint64_t length; |
| 1632 | if (!memory_node.GetAttributeValueAsUnsigned("start", start)) |
| 1633 | return true; |
| 1634 | if (!memory_node.GetAttributeValueAsUnsigned("length", length)) |
| 1635 | return true; |
| 1636 | MemoryRegionInfo region; |
| 1637 | region.GetRange().SetRangeBase(start); |
| 1638 | region.GetRange().SetByteSize(length); |
| 1639 | if (type == "rom") { |
| 1640 | region.SetReadable(MemoryRegionInfo::eYes); |
| 1641 | this->m_qXfer_memory_map.push_back(region); |
| 1642 | } else if (type == "ram") { |
| 1643 | region.SetReadable(MemoryRegionInfo::eYes); |
| 1644 | region.SetWritable(MemoryRegionInfo::eYes); |
| 1645 | this->m_qXfer_memory_map.push_back(region); |
| 1646 | } else if (type == "flash") { |
| 1647 | region.SetFlash(MemoryRegionInfo::eYes); |
| 1648 | memory_node.ForEachChildElement( |
| 1649 | [®ion](const XMLNode &prop_node) -> bool { |
| 1650 | if (!prop_node.IsElement()) |
| 1651 | return true; |
| 1652 | if (prop_node.GetName() != "property") |
| 1653 | return true; |
| 1654 | auto propname = prop_node.GetAttributeValue("name", ""); |
| 1655 | if (propname == "blocksize") { |
| 1656 | uint64_t blocksize; |
| 1657 | if (prop_node.GetElementTextAsUnsigned(blocksize)) |
| 1658 | region.SetBlocksize(blocksize); |
| 1659 | } |
| 1660 | return true; |
| 1661 | }); |
| 1662 | this->m_qXfer_memory_map.push_back(region); |
| 1663 | } |
| 1664 | return true; |
| 1665 | }); |
| 1666 | |
| 1667 | m_qXfer_memory_map_loaded = true; |
| 1668 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1669 | return error; |
| 1670 | } |
| 1671 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1672 | Status GDBRemoteCommunicationClient::GetWatchpointSupportInfo(uint32_t &num) { |
| 1673 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1674 | |
| 1675 | if (m_supports_watchpoint_support_info == eLazyBoolYes) { |
| 1676 | num = m_num_supported_hardware_watchpoints; |
| 1677 | return error; |
| 1678 | } |
| 1679 | |
| 1680 | // Set num to 0 first. |
| 1681 | num = 0; |
| 1682 | if (m_supports_watchpoint_support_info != eLazyBoolNo) { |
| 1683 | char packet[64]; |
| 1684 | const int packet_len = |
| 1685 | ::snprintf(packet, sizeof(packet), "qWatchpointSupportInfo:"); |
| 1686 | assert(packet_len < (int)sizeof(packet)); |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 1687 | UNUSED_IF_ASSERT_DISABLED(packet_len); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1688 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 1689 | if (SendPacketAndWaitForResponse(packet, response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1690 | PacketResult::Success) { |
| 1691 | m_supports_watchpoint_support_info = eLazyBoolYes; |
| 1692 | llvm::StringRef name; |
| 1693 | llvm::StringRef value; |
Jason Molenda | c0e793d | 2018-11-09 22:33:26 +0000 | [diff] [blame] | 1694 | bool found_num_field = false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1695 | while (response.GetNameColonValue(name, value)) { |
| 1696 | if (name.equals("num")) { |
| 1697 | value.getAsInteger(0, m_num_supported_hardware_watchpoints); |
| 1698 | num = m_num_supported_hardware_watchpoints; |
Jason Molenda | c0e793d | 2018-11-09 22:33:26 +0000 | [diff] [blame] | 1699 | found_num_field = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1700 | } |
| 1701 | } |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 1702 | if (!found_num_field) { |
Jason Molenda | c0e793d | 2018-11-09 22:33:26 +0000 | [diff] [blame] | 1703 | m_supports_watchpoint_support_info = eLazyBoolNo; |
| 1704 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1705 | } else { |
| 1706 | m_supports_watchpoint_support_info = eLazyBoolNo; |
| 1707 | } |
| 1708 | } |
| 1709 | |
| 1710 | if (m_supports_watchpoint_support_info == eLazyBoolNo) { |
| 1711 | error.SetErrorString("qWatchpointSupportInfo is not supported"); |
| 1712 | } |
| 1713 | return error; |
| 1714 | } |
| 1715 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1716 | lldb_private::Status GDBRemoteCommunicationClient::GetWatchpointSupportInfo( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1717 | uint32_t &num, bool &after, const ArchSpec &arch) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1718 | Status error(GetWatchpointSupportInfo(num)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1719 | if (error.Success()) |
| 1720 | error = GetWatchpointsTriggerAfterInstruction(after, arch); |
| 1721 | return error; |
Greg Clayton | 37a0a24 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1724 | lldb_private::Status |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1725 | GDBRemoteCommunicationClient::GetWatchpointsTriggerAfterInstruction( |
| 1726 | bool &after, const ArchSpec &arch) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1727 | Status error; |
Fangrui Song | 2f677ab | 2019-05-16 09:07:33 +0000 | [diff] [blame] | 1728 | llvm::Triple triple = arch.GetTriple(); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1729 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1730 | // we assume watchpoints will happen after running the relevant opcode and we |
| 1731 | // only want to override this behavior if we have explicitly received a |
| 1732 | // qHostInfo telling us otherwise |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1733 | if (m_qHostInfo_is_valid != eLazyBoolYes) { |
Fangrui Song | 2f677ab | 2019-05-16 09:07:33 +0000 | [diff] [blame] | 1734 | // On targets like MIPS and ppc64, watchpoint exceptions are always |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1735 | // generated before the instruction is executed. The connected target may |
| 1736 | // not support qHostInfo or qWatchpointSupportInfo packets. |
Fangrui Song | 2f677ab | 2019-05-16 09:07:33 +0000 | [diff] [blame] | 1737 | after = !(triple.isMIPS() || triple.isPPC64()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1738 | } else { |
Fangrui Song | 2f677ab | 2019-05-16 09:07:33 +0000 | [diff] [blame] | 1739 | // For MIPS and ppc64, set m_watchpoints_trigger_after_instruction to |
Pavel Labath | c51ad48 | 2017-10-27 17:02:32 +0000 | [diff] [blame] | 1740 | // eLazyBoolNo if it is not calculated before. |
Fangrui Song | ddb93b6 | 2019-05-16 08:37:32 +0000 | [diff] [blame] | 1741 | if (m_watchpoints_trigger_after_instruction == eLazyBoolCalculate && |
Fangrui Song | 2f677ab | 2019-05-16 09:07:33 +0000 | [diff] [blame] | 1742 | (triple.isMIPS() || triple.isPPC64())) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1743 | m_watchpoints_trigger_after_instruction = eLazyBoolNo; |
| 1744 | |
| 1745 | after = (m_watchpoints_trigger_after_instruction != eLazyBoolNo); |
| 1746 | } |
| 1747 | return error; |
| 1748 | } |
| 1749 | |
| 1750 | int GDBRemoteCommunicationClient::SetSTDIN(const FileSpec &file_spec) { |
| 1751 | if (file_spec) { |
| 1752 | std::string path{file_spec.GetPath(false)}; |
| 1753 | StreamString packet; |
| 1754 | packet.PutCString("QSetSTDIN:"); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 1755 | packet.PutStringAsRawHex8(path); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1756 | |
| 1757 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 1758 | if (SendPacketAndWaitForResponse(packet.GetString(), response, false) == |
| 1759 | PacketResult::Success) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1760 | if (response.IsOKResponse()) |
| 1761 | return 0; |
| 1762 | uint8_t error = response.GetError(); |
| 1763 | if (error) |
| 1764 | return error; |
| 1765 | } |
| 1766 | } |
| 1767 | return -1; |
| 1768 | } |
| 1769 | |
| 1770 | int GDBRemoteCommunicationClient::SetSTDOUT(const FileSpec &file_spec) { |
| 1771 | if (file_spec) { |
| 1772 | std::string path{file_spec.GetPath(false)}; |
| 1773 | StreamString packet; |
| 1774 | packet.PutCString("QSetSTDOUT:"); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 1775 | packet.PutStringAsRawHex8(path); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1776 | |
| 1777 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 1778 | if (SendPacketAndWaitForResponse(packet.GetString(), response, false) == |
| 1779 | PacketResult::Success) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1780 | if (response.IsOKResponse()) |
| 1781 | return 0; |
| 1782 | uint8_t error = response.GetError(); |
| 1783 | if (error) |
| 1784 | return error; |
| 1785 | } |
| 1786 | } |
| 1787 | return -1; |
| 1788 | } |
| 1789 | |
| 1790 | int GDBRemoteCommunicationClient::SetSTDERR(const FileSpec &file_spec) { |
| 1791 | if (file_spec) { |
| 1792 | std::string path{file_spec.GetPath(false)}; |
| 1793 | StreamString packet; |
| 1794 | packet.PutCString("QSetSTDERR:"); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 1795 | packet.PutStringAsRawHex8(path); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1796 | |
| 1797 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 1798 | if (SendPacketAndWaitForResponse(packet.GetString(), response, false) == |
| 1799 | PacketResult::Success) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1800 | if (response.IsOKResponse()) |
| 1801 | return 0; |
| 1802 | uint8_t error = response.GetError(); |
| 1803 | if (error) |
| 1804 | return error; |
| 1805 | } |
| 1806 | } |
| 1807 | return -1; |
| 1808 | } |
| 1809 | |
| 1810 | bool GDBRemoteCommunicationClient::GetWorkingDir(FileSpec &working_dir) { |
| 1811 | StringExtractorGDBRemote response; |
| 1812 | if (SendPacketAndWaitForResponse("qGetWorkingDir", response, false) == |
| 1813 | PacketResult::Success) { |
| 1814 | if (response.IsUnsupportedResponse()) |
| 1815 | return false; |
| 1816 | if (response.IsErrorResponse()) |
| 1817 | return false; |
| 1818 | std::string cwd; |
| 1819 | response.GetHexByteString(cwd); |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 1820 | working_dir.SetFile(cwd, GetHostArchitecture().GetTriple()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1821 | return !cwd.empty(); |
| 1822 | } |
| 1823 | return false; |
| 1824 | } |
| 1825 | |
| 1826 | int GDBRemoteCommunicationClient::SetWorkingDir(const FileSpec &working_dir) { |
| 1827 | if (working_dir) { |
| 1828 | std::string path{working_dir.GetPath(false)}; |
| 1829 | StreamString packet; |
| 1830 | packet.PutCString("QSetWorkingDir:"); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 1831 | packet.PutStringAsRawHex8(path); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1832 | |
| 1833 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 1834 | if (SendPacketAndWaitForResponse(packet.GetString(), response, false) == |
| 1835 | PacketResult::Success) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1836 | if (response.IsOKResponse()) |
| 1837 | return 0; |
| 1838 | uint8_t error = response.GetError(); |
| 1839 | if (error) |
| 1840 | return error; |
| 1841 | } |
| 1842 | } |
| 1843 | return -1; |
| 1844 | } |
| 1845 | |
| 1846 | int GDBRemoteCommunicationClient::SetDisableASLR(bool enable) { |
| 1847 | char packet[32]; |
| 1848 | const int packet_len = |
| 1849 | ::snprintf(packet, sizeof(packet), "QSetDisableASLR:%i", enable ? 1 : 0); |
| 1850 | assert(packet_len < (int)sizeof(packet)); |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 1851 | UNUSED_IF_ASSERT_DISABLED(packet_len); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1852 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 1853 | if (SendPacketAndWaitForResponse(packet, response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1854 | PacketResult::Success) { |
| 1855 | if (response.IsOKResponse()) |
| 1856 | return 0; |
| 1857 | uint8_t error = response.GetError(); |
| 1858 | if (error) |
| 1859 | return error; |
| 1860 | } |
| 1861 | return -1; |
| 1862 | } |
| 1863 | |
| 1864 | int GDBRemoteCommunicationClient::SetDetachOnError(bool enable) { |
| 1865 | char packet[32]; |
| 1866 | const int packet_len = ::snprintf(packet, sizeof(packet), |
| 1867 | "QSetDetachOnError:%i", enable ? 1 : 0); |
| 1868 | assert(packet_len < (int)sizeof(packet)); |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 1869 | UNUSED_IF_ASSERT_DISABLED(packet_len); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1870 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 1871 | if (SendPacketAndWaitForResponse(packet, response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1872 | PacketResult::Success) { |
| 1873 | if (response.IsOKResponse()) |
| 1874 | return 0; |
| 1875 | uint8_t error = response.GetError(); |
| 1876 | if (error) |
| 1877 | return error; |
| 1878 | } |
| 1879 | return -1; |
| 1880 | } |
| 1881 | |
| 1882 | bool GDBRemoteCommunicationClient::DecodeProcessInfoResponse( |
| 1883 | StringExtractorGDBRemote &response, ProcessInstanceInfo &process_info) { |
| 1884 | if (response.IsNormalResponse()) { |
| 1885 | llvm::StringRef name; |
| 1886 | llvm::StringRef value; |
| 1887 | StringExtractor extractor; |
| 1888 | |
| 1889 | uint32_t cpu = LLDB_INVALID_CPUTYPE; |
| 1890 | uint32_t sub = 0; |
| 1891 | std::string vendor; |
| 1892 | std::string os_type; |
| 1893 | |
| 1894 | while (response.GetNameColonValue(name, value)) { |
| 1895 | if (name.equals("pid")) { |
| 1896 | lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; |
| 1897 | value.getAsInteger(0, pid); |
| 1898 | process_info.SetProcessID(pid); |
| 1899 | } else if (name.equals("ppid")) { |
| 1900 | lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; |
| 1901 | value.getAsInteger(0, pid); |
| 1902 | process_info.SetParentProcessID(pid); |
| 1903 | } else if (name.equals("uid")) { |
| 1904 | uint32_t uid = UINT32_MAX; |
| 1905 | value.getAsInteger(0, uid); |
| 1906 | process_info.SetUserID(uid); |
| 1907 | } else if (name.equals("euid")) { |
| 1908 | uint32_t uid = UINT32_MAX; |
| 1909 | value.getAsInteger(0, uid); |
Walter Erquinigo | 8b6dcc1 | 2019-10-07 20:26:49 +0000 | [diff] [blame] | 1910 | process_info.SetEffectiveUserID(uid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1911 | } else if (name.equals("gid")) { |
| 1912 | uint32_t gid = UINT32_MAX; |
| 1913 | value.getAsInteger(0, gid); |
| 1914 | process_info.SetGroupID(gid); |
| 1915 | } else if (name.equals("egid")) { |
| 1916 | uint32_t gid = UINT32_MAX; |
| 1917 | value.getAsInteger(0, gid); |
| 1918 | process_info.SetEffectiveGroupID(gid); |
| 1919 | } else if (name.equals("triple")) { |
| 1920 | StringExtractor extractor(value); |
| 1921 | std::string triple; |
| 1922 | extractor.GetHexByteString(triple); |
| 1923 | process_info.GetArchitecture().SetTriple(triple.c_str()); |
| 1924 | } else if (name.equals("name")) { |
| 1925 | StringExtractor extractor(value); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1926 | // The process name from ASCII hex bytes since we can't control the |
| 1927 | // characters in a process name |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1928 | std::string name; |
| 1929 | extractor.GetHexByteString(name); |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 1930 | process_info.GetExecutableFile().SetFile(name, FileSpec::Style::native); |
Walter Erquinigo | 48a50ee | 2019-10-16 18:47:05 +0000 | [diff] [blame] | 1931 | } else if (name.equals("args")) { |
| 1932 | llvm::StringRef encoded_args(value), hex_arg; |
| 1933 | |
| 1934 | bool is_arg0 = true; |
| 1935 | while (!encoded_args.empty()) { |
| 1936 | std::tie(hex_arg, encoded_args) = encoded_args.split('-'); |
| 1937 | std::string arg; |
| 1938 | StringExtractor extractor(hex_arg); |
| 1939 | if (extractor.GetHexByteString(arg) * 2 != hex_arg.size()) { |
| 1940 | // In case of wrong encoding, we discard all the arguments |
| 1941 | process_info.GetArguments().Clear(); |
| 1942 | process_info.SetArg0(""); |
| 1943 | break; |
| 1944 | } |
| 1945 | if (is_arg0) |
| 1946 | process_info.SetArg0(arg); |
| 1947 | else |
| 1948 | process_info.GetArguments().AppendArgument(arg); |
| 1949 | is_arg0 = false; |
| 1950 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1951 | } else if (name.equals("cputype")) { |
| 1952 | value.getAsInteger(0, cpu); |
| 1953 | } else if (name.equals("cpusubtype")) { |
| 1954 | value.getAsInteger(0, sub); |
| 1955 | } else if (name.equals("vendor")) { |
| 1956 | vendor = value; |
| 1957 | } else if (name.equals("ostype")) { |
| 1958 | os_type = value; |
| 1959 | } |
| 1960 | } |
| 1961 | |
| 1962 | if (cpu != LLDB_INVALID_CPUTYPE && !vendor.empty() && !os_type.empty()) { |
| 1963 | if (vendor == "apple") { |
| 1964 | process_info.GetArchitecture().SetArchitecture(eArchTypeMachO, cpu, |
| 1965 | sub); |
| 1966 | process_info.GetArchitecture().GetTriple().setVendorName( |
| 1967 | llvm::StringRef(vendor)); |
| 1968 | process_info.GetArchitecture().GetTriple().setOSName( |
| 1969 | llvm::StringRef(os_type)); |
| 1970 | } |
| 1971 | } |
| 1972 | |
| 1973 | if (process_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) |
| 1974 | return true; |
| 1975 | } |
| 1976 | return false; |
| 1977 | } |
| 1978 | |
| 1979 | bool GDBRemoteCommunicationClient::GetProcessInfo( |
| 1980 | lldb::pid_t pid, ProcessInstanceInfo &process_info) { |
| 1981 | process_info.Clear(); |
| 1982 | |
| 1983 | if (m_supports_qProcessInfoPID) { |
| 1984 | char packet[32]; |
| 1985 | const int packet_len = |
| 1986 | ::snprintf(packet, sizeof(packet), "qProcessInfoPID:%" PRIu64, pid); |
| 1987 | assert(packet_len < (int)sizeof(packet)); |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 1988 | UNUSED_IF_ASSERT_DISABLED(packet_len); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1989 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 1990 | if (SendPacketAndWaitForResponse(packet, response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1991 | PacketResult::Success) { |
| 1992 | return DecodeProcessInfoResponse(response, process_info); |
| 1993 | } else { |
| 1994 | m_supports_qProcessInfoPID = false; |
| 1995 | return false; |
| 1996 | } |
| 1997 | } |
| 1998 | return false; |
| 1999 | } |
| 2000 | |
| 2001 | bool GDBRemoteCommunicationClient::GetCurrentProcessInfo(bool allow_lazy) { |
| 2002 | Log *log(ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet(GDBR_LOG_PROCESS | |
| 2003 | GDBR_LOG_PACKETS)); |
| 2004 | |
| 2005 | if (allow_lazy) { |
| 2006 | if (m_qProcessInfo_is_valid == eLazyBoolYes) |
| 2007 | return true; |
| 2008 | if (m_qProcessInfo_is_valid == eLazyBoolNo) |
| 2009 | return false; |
| 2010 | } |
| 2011 | |
| 2012 | GetHostInfo(); |
| 2013 | |
| 2014 | StringExtractorGDBRemote response; |
| 2015 | if (SendPacketAndWaitForResponse("qProcessInfo", response, false) == |
| 2016 | PacketResult::Success) { |
| 2017 | if (response.IsNormalResponse()) { |
| 2018 | llvm::StringRef name; |
| 2019 | llvm::StringRef value; |
| 2020 | uint32_t cpu = LLDB_INVALID_CPUTYPE; |
| 2021 | uint32_t sub = 0; |
| 2022 | std::string arch_name; |
| 2023 | std::string os_name; |
Adrian Prantl | 2461061 | 2019-09-04 17:23:15 +0000 | [diff] [blame] | 2024 | std::string environment; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2025 | std::string vendor_name; |
| 2026 | std::string triple; |
Nitesh Jain | 8999edf | 2016-10-12 10:21:09 +0000 | [diff] [blame] | 2027 | std::string elf_abi; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2028 | uint32_t pointer_byte_size = 0; |
| 2029 | StringExtractor extractor; |
| 2030 | ByteOrder byte_order = eByteOrderInvalid; |
| 2031 | uint32_t num_keys_decoded = 0; |
| 2032 | lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; |
| 2033 | while (response.GetNameColonValue(name, value)) { |
| 2034 | if (name.equals("cputype")) { |
| 2035 | if (!value.getAsInteger(16, cpu)) |
| 2036 | ++num_keys_decoded; |
| 2037 | } else if (name.equals("cpusubtype")) { |
| 2038 | if (!value.getAsInteger(16, sub)) |
| 2039 | ++num_keys_decoded; |
| 2040 | } else if (name.equals("triple")) { |
| 2041 | StringExtractor extractor(value); |
| 2042 | extractor.GetHexByteString(triple); |
| 2043 | ++num_keys_decoded; |
| 2044 | } else if (name.equals("ostype")) { |
Adrian Prantl | 2461061 | 2019-09-04 17:23:15 +0000 | [diff] [blame] | 2045 | if (value.equals("maccatalyst")) { |
| 2046 | os_name = "ios"; |
| 2047 | environment = "macabi"; |
| 2048 | } else |
| 2049 | os_name = value; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2050 | ++num_keys_decoded; |
| 2051 | } else if (name.equals("vendor")) { |
| 2052 | vendor_name = value; |
| 2053 | ++num_keys_decoded; |
| 2054 | } else if (name.equals("endian")) { |
| 2055 | byte_order = llvm::StringSwitch<lldb::ByteOrder>(value) |
| 2056 | .Case("little", eByteOrderLittle) |
| 2057 | .Case("big", eByteOrderBig) |
| 2058 | .Case("pdp", eByteOrderPDP) |
| 2059 | .Default(eByteOrderInvalid); |
| 2060 | if (byte_order != eByteOrderInvalid) |
| 2061 | ++num_keys_decoded; |
| 2062 | } else if (name.equals("ptrsize")) { |
| 2063 | if (!value.getAsInteger(16, pointer_byte_size)) |
| 2064 | ++num_keys_decoded; |
| 2065 | } else if (name.equals("pid")) { |
| 2066 | if (!value.getAsInteger(16, pid)) |
| 2067 | ++num_keys_decoded; |
Nitesh Jain | 8999edf | 2016-10-12 10:21:09 +0000 | [diff] [blame] | 2068 | } else if (name.equals("elf_abi")) { |
| 2069 | elf_abi = value; |
| 2070 | ++num_keys_decoded; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2071 | } |
| 2072 | } |
| 2073 | if (num_keys_decoded > 0) |
| 2074 | m_qProcessInfo_is_valid = eLazyBoolYes; |
| 2075 | if (pid != LLDB_INVALID_PROCESS_ID) { |
| 2076 | m_curr_pid_is_valid = eLazyBoolYes; |
| 2077 | m_curr_pid = pid; |
| 2078 | } |
| 2079 | |
| 2080 | // Set the ArchSpec from the triple if we have it. |
| 2081 | if (!triple.empty()) { |
| 2082 | m_process_arch.SetTriple(triple.c_str()); |
Nitesh Jain | 8999edf | 2016-10-12 10:21:09 +0000 | [diff] [blame] | 2083 | m_process_arch.SetFlags(elf_abi); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2084 | if (pointer_byte_size) { |
| 2085 | assert(pointer_byte_size == m_process_arch.GetAddressByteSize()); |
| 2086 | } |
| 2087 | } else if (cpu != LLDB_INVALID_CPUTYPE && !os_name.empty() && |
| 2088 | !vendor_name.empty()) { |
| 2089 | llvm::Triple triple(llvm::Twine("-") + vendor_name + "-" + os_name); |
Adrian Prantl | 2461061 | 2019-09-04 17:23:15 +0000 | [diff] [blame] | 2090 | if (!environment.empty()) |
| 2091 | triple.setEnvironmentName(environment); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2092 | |
| 2093 | assert(triple.getObjectFormat() != llvm::Triple::UnknownObjectFormat); |
Pavel Labath | 4f19fce2 | 2017-02-17 13:39:50 +0000 | [diff] [blame] | 2094 | assert(triple.getObjectFormat() != llvm::Triple::Wasm); |
Jason Liu | a03ae73 | 2019-03-12 22:01:10 +0000 | [diff] [blame] | 2095 | assert(triple.getObjectFormat() != llvm::Triple::XCOFF); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2096 | switch (triple.getObjectFormat()) { |
| 2097 | case llvm::Triple::MachO: |
| 2098 | m_process_arch.SetArchitecture(eArchTypeMachO, cpu, sub); |
| 2099 | break; |
| 2100 | case llvm::Triple::ELF: |
| 2101 | m_process_arch.SetArchitecture(eArchTypeELF, cpu, sub); |
| 2102 | break; |
| 2103 | case llvm::Triple::COFF: |
| 2104 | m_process_arch.SetArchitecture(eArchTypeCOFF, cpu, sub); |
| 2105 | break; |
Pavel Labath | 4f19fce2 | 2017-02-17 13:39:50 +0000 | [diff] [blame] | 2106 | case llvm::Triple::Wasm: |
Jason Liu | a03ae73 | 2019-03-12 22:01:10 +0000 | [diff] [blame] | 2107 | case llvm::Triple::XCOFF: |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2108 | LLDB_LOGF(log, "error: not supported target architecture"); |
Pavel Labath | 4f19fce2 | 2017-02-17 13:39:50 +0000 | [diff] [blame] | 2109 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2110 | case llvm::Triple::UnknownObjectFormat: |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2111 | LLDB_LOGF(log, "error: failed to determine target architecture"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2112 | return false; |
| 2113 | } |
| 2114 | |
| 2115 | if (pointer_byte_size) { |
| 2116 | assert(pointer_byte_size == m_process_arch.GetAddressByteSize()); |
| 2117 | } |
| 2118 | if (byte_order != eByteOrderInvalid) { |
| 2119 | assert(byte_order == m_process_arch.GetByteOrder()); |
| 2120 | } |
| 2121 | m_process_arch.GetTriple().setVendorName(llvm::StringRef(vendor_name)); |
| 2122 | m_process_arch.GetTriple().setOSName(llvm::StringRef(os_name)); |
Adrian Prantl | 2461061 | 2019-09-04 17:23:15 +0000 | [diff] [blame] | 2123 | m_process_arch.GetTriple().setEnvironmentName(llvm::StringRef(environment)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2124 | m_host_arch.GetTriple().setVendorName(llvm::StringRef(vendor_name)); |
| 2125 | m_host_arch.GetTriple().setOSName(llvm::StringRef(os_name)); |
Adrian Prantl | 2461061 | 2019-09-04 17:23:15 +0000 | [diff] [blame] | 2126 | m_host_arch.GetTriple().setEnvironmentName(llvm::StringRef(environment)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2127 | } |
| 2128 | return true; |
| 2129 | } |
| 2130 | } else { |
| 2131 | m_qProcessInfo_is_valid = eLazyBoolNo; |
| 2132 | } |
| 2133 | |
| 2134 | return false; |
| 2135 | } |
| 2136 | |
| 2137 | uint32_t GDBRemoteCommunicationClient::FindProcesses( |
| 2138 | const ProcessInstanceInfoMatch &match_info, |
| 2139 | ProcessInstanceInfoList &process_infos) { |
| 2140 | process_infos.Clear(); |
| 2141 | |
| 2142 | if (m_supports_qfProcessInfo) { |
| 2143 | StreamString packet; |
| 2144 | packet.PutCString("qfProcessInfo"); |
| 2145 | if (!match_info.MatchAllProcesses()) { |
| 2146 | packet.PutChar(':'); |
| 2147 | const char *name = match_info.GetProcessInfo().GetName(); |
| 2148 | bool has_name_match = false; |
| 2149 | if (name && name[0]) { |
| 2150 | has_name_match = true; |
Pavel Labath | c4a3395 | 2017-02-20 11:35:33 +0000 | [diff] [blame] | 2151 | NameMatch name_match_type = match_info.GetNameMatchType(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2152 | switch (name_match_type) { |
Pavel Labath | c4a3395 | 2017-02-20 11:35:33 +0000 | [diff] [blame] | 2153 | case NameMatch::Ignore: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2154 | has_name_match = false; |
| 2155 | break; |
| 2156 | |
Pavel Labath | c4a3395 | 2017-02-20 11:35:33 +0000 | [diff] [blame] | 2157 | case NameMatch::Equals: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2158 | packet.PutCString("name_match:equals;"); |
| 2159 | break; |
| 2160 | |
Pavel Labath | c4a3395 | 2017-02-20 11:35:33 +0000 | [diff] [blame] | 2161 | case NameMatch::Contains: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2162 | packet.PutCString("name_match:contains;"); |
| 2163 | break; |
| 2164 | |
Pavel Labath | c4a3395 | 2017-02-20 11:35:33 +0000 | [diff] [blame] | 2165 | case NameMatch::StartsWith: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2166 | packet.PutCString("name_match:starts_with;"); |
| 2167 | break; |
| 2168 | |
Pavel Labath | c4a3395 | 2017-02-20 11:35:33 +0000 | [diff] [blame] | 2169 | case NameMatch::EndsWith: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2170 | packet.PutCString("name_match:ends_with;"); |
| 2171 | break; |
| 2172 | |
Pavel Labath | c4a3395 | 2017-02-20 11:35:33 +0000 | [diff] [blame] | 2173 | case NameMatch::RegularExpression: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2174 | packet.PutCString("name_match:regex;"); |
| 2175 | break; |
| 2176 | } |
| 2177 | if (has_name_match) { |
| 2178 | packet.PutCString("name:"); |
| 2179 | packet.PutBytesAsRawHex8(name, ::strlen(name)); |
| 2180 | packet.PutChar(';'); |
| 2181 | } |
| 2182 | } |
| 2183 | |
| 2184 | if (match_info.GetProcessInfo().ProcessIDIsValid()) |
| 2185 | packet.Printf("pid:%" PRIu64 ";", |
| 2186 | match_info.GetProcessInfo().GetProcessID()); |
| 2187 | if (match_info.GetProcessInfo().ParentProcessIDIsValid()) |
| 2188 | packet.Printf("parent_pid:%" PRIu64 ";", |
| 2189 | match_info.GetProcessInfo().GetParentProcessID()); |
| 2190 | if (match_info.GetProcessInfo().UserIDIsValid()) |
| 2191 | packet.Printf("uid:%u;", match_info.GetProcessInfo().GetUserID()); |
| 2192 | if (match_info.GetProcessInfo().GroupIDIsValid()) |
| 2193 | packet.Printf("gid:%u;", match_info.GetProcessInfo().GetGroupID()); |
| 2194 | if (match_info.GetProcessInfo().EffectiveUserIDIsValid()) |
| 2195 | packet.Printf("euid:%u;", |
| 2196 | match_info.GetProcessInfo().GetEffectiveUserID()); |
| 2197 | if (match_info.GetProcessInfo().EffectiveGroupIDIsValid()) |
| 2198 | packet.Printf("egid:%u;", |
| 2199 | match_info.GetProcessInfo().GetEffectiveGroupID()); |
Walter Erquinigo | af1d27e | 2019-10-12 02:36:16 +0000 | [diff] [blame] | 2200 | packet.Printf("all_users:%u;", match_info.GetMatchAllUsers() ? 1 : 0); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2201 | if (match_info.GetProcessInfo().GetArchitecture().IsValid()) { |
| 2202 | const ArchSpec &match_arch = |
| 2203 | match_info.GetProcessInfo().GetArchitecture(); |
| 2204 | const llvm::Triple &triple = match_arch.GetTriple(); |
| 2205 | packet.PutCString("triple:"); |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 2206 | packet.PutCString(triple.getTriple()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2207 | packet.PutChar(';'); |
| 2208 | } |
| 2209 | } |
| 2210 | StringExtractorGDBRemote response; |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2211 | // Increase timeout as the first qfProcessInfo packet takes a long time on |
| 2212 | // Android. The value of 1min was arrived at empirically. |
Pavel Labath | 1eff73c | 2016-11-24 10:54:49 +0000 | [diff] [blame] | 2213 | ScopedTimeout timeout(*this, minutes(1)); |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2214 | if (SendPacketAndWaitForResponse(packet.GetString(), response, false) == |
| 2215 | PacketResult::Success) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2216 | do { |
| 2217 | ProcessInstanceInfo process_info; |
| 2218 | if (!DecodeProcessInfoResponse(response, process_info)) |
| 2219 | break; |
| 2220 | process_infos.Append(process_info); |
Jonas Devlieghere | d35b42f | 2019-08-21 04:55:56 +0000 | [diff] [blame] | 2221 | response = StringExtractorGDBRemote(); |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2222 | } while (SendPacketAndWaitForResponse("qsProcessInfo", response, false) == |
| 2223 | PacketResult::Success); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2224 | } else { |
| 2225 | m_supports_qfProcessInfo = false; |
| 2226 | return 0; |
| 2227 | } |
| 2228 | } |
| 2229 | return process_infos.GetSize(); |
| 2230 | } |
| 2231 | |
| 2232 | bool GDBRemoteCommunicationClient::GetUserName(uint32_t uid, |
| 2233 | std::string &name) { |
| 2234 | if (m_supports_qUserName) { |
| 2235 | char packet[32]; |
| 2236 | const int packet_len = |
| 2237 | ::snprintf(packet, sizeof(packet), "qUserName:%i", uid); |
| 2238 | assert(packet_len < (int)sizeof(packet)); |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2239 | UNUSED_IF_ASSERT_DISABLED(packet_len); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2240 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2241 | if (SendPacketAndWaitForResponse(packet, response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2242 | PacketResult::Success) { |
| 2243 | if (response.IsNormalResponse()) { |
| 2244 | // Make sure we parsed the right number of characters. The response is |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2245 | // the hex encoded user name and should make up the entire packet. If |
| 2246 | // there are any non-hex ASCII bytes, the length won't match below.. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2247 | if (response.GetHexByteString(name) * 2 == |
| 2248 | response.GetStringRef().size()) |
| 2249 | return true; |
| 2250 | } |
| 2251 | } else { |
| 2252 | m_supports_qUserName = false; |
| 2253 | return false; |
| 2254 | } |
| 2255 | } |
| 2256 | return false; |
| 2257 | } |
| 2258 | |
| 2259 | bool GDBRemoteCommunicationClient::GetGroupName(uint32_t gid, |
| 2260 | std::string &name) { |
| 2261 | if (m_supports_qGroupName) { |
| 2262 | char packet[32]; |
| 2263 | const int packet_len = |
| 2264 | ::snprintf(packet, sizeof(packet), "qGroupName:%i", gid); |
| 2265 | assert(packet_len < (int)sizeof(packet)); |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2266 | UNUSED_IF_ASSERT_DISABLED(packet_len); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2267 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2268 | if (SendPacketAndWaitForResponse(packet, response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2269 | PacketResult::Success) { |
| 2270 | if (response.IsNormalResponse()) { |
| 2271 | // Make sure we parsed the right number of characters. The response is |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2272 | // the hex encoded group name and should make up the entire packet. If |
| 2273 | // there are any non-hex ASCII bytes, the length won't match below.. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2274 | if (response.GetHexByteString(name) * 2 == |
| 2275 | response.GetStringRef().size()) |
| 2276 | return true; |
| 2277 | } |
| 2278 | } else { |
| 2279 | m_supports_qGroupName = false; |
| 2280 | return false; |
| 2281 | } |
| 2282 | } |
| 2283 | return false; |
| 2284 | } |
| 2285 | |
| 2286 | bool GDBRemoteCommunicationClient::SetNonStopMode(const bool enable) { |
| 2287 | // Form non-stop packet request |
| 2288 | char packet[32]; |
| 2289 | const int packet_len = |
| 2290 | ::snprintf(packet, sizeof(packet), "QNonStop:%1d", (int)enable); |
| 2291 | assert(packet_len < (int)sizeof(packet)); |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2292 | UNUSED_IF_ASSERT_DISABLED(packet_len); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2293 | |
| 2294 | StringExtractorGDBRemote response; |
| 2295 | // Send to target |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2296 | if (SendPacketAndWaitForResponse(packet, response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2297 | PacketResult::Success) |
| 2298 | if (response.IsOKResponse()) |
| 2299 | return true; |
| 2300 | |
| 2301 | // Failed or not supported |
| 2302 | return false; |
| 2303 | } |
| 2304 | |
| 2305 | static void MakeSpeedTestPacket(StreamString &packet, uint32_t send_size, |
| 2306 | uint32_t recv_size) { |
| 2307 | packet.Clear(); |
| 2308 | packet.Printf("qSpeedTest:response_size:%i;data:", recv_size); |
| 2309 | uint32_t bytes_left = send_size; |
| 2310 | while (bytes_left > 0) { |
| 2311 | if (bytes_left >= 26) { |
| 2312 | packet.PutCString("abcdefghijklmnopqrstuvwxyz"); |
| 2313 | bytes_left -= 26; |
| 2314 | } else { |
| 2315 | packet.Printf("%*.*s;", bytes_left, bytes_left, |
| 2316 | "abcdefghijklmnopqrstuvwxyz"); |
| 2317 | bytes_left = 0; |
| 2318 | } |
| 2319 | } |
| 2320 | } |
| 2321 | |
Pavel Labath | 1eff73c | 2016-11-24 10:54:49 +0000 | [diff] [blame] | 2322 | duration<float> |
| 2323 | calculate_standard_deviation(const std::vector<duration<float>> &v) { |
| 2324 | using Dur = duration<float>; |
Pavel Labath | 3aa0491 | 2016-10-31 17:19:42 +0000 | [diff] [blame] | 2325 | Dur sum = std::accumulate(std::begin(v), std::end(v), Dur()); |
| 2326 | Dur mean = sum / v.size(); |
| 2327 | float accum = 0; |
| 2328 | for (auto d : v) { |
| 2329 | float delta = (d - mean).count(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2330 | accum += delta * delta; |
Pavel Labath | 3aa0491 | 2016-10-31 17:19:42 +0000 | [diff] [blame] | 2331 | }; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2332 | |
Pavel Labath | 3aa0491 | 2016-10-31 17:19:42 +0000 | [diff] [blame] | 2333 | return Dur(sqrtf(accum / (v.size() - 1))); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | void GDBRemoteCommunicationClient::TestPacketSpeed(const uint32_t num_packets, |
| 2337 | uint32_t max_send, |
Pavel Labath | 2fd9a1e | 2016-11-04 11:49:06 +0000 | [diff] [blame] | 2338 | uint32_t max_recv, |
| 2339 | uint64_t recv_amount, |
| 2340 | bool json, Stream &strm) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2341 | uint32_t i; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2342 | if (SendSpeedTestPacket(0, 0)) { |
| 2343 | StreamString packet; |
| 2344 | if (json) |
| 2345 | strm.Printf("{ \"packet_speeds\" : {\n \"num_packets\" : %u,\n " |
| 2346 | "\"results\" : [", |
| 2347 | num_packets); |
| 2348 | else |
| 2349 | strm.Printf("Testing sending %u packets of various sizes:\n", |
| 2350 | num_packets); |
| 2351 | strm.Flush(); |
| 2352 | |
| 2353 | uint32_t result_idx = 0; |
| 2354 | uint32_t send_size; |
Pavel Labath | 3aa0491 | 2016-10-31 17:19:42 +0000 | [diff] [blame] | 2355 | std::vector<duration<float>> packet_times; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2356 | |
| 2357 | for (send_size = 0; send_size <= max_send; |
| 2358 | send_size ? send_size *= 2 : send_size = 4) { |
| 2359 | for (uint32_t recv_size = 0; recv_size <= max_recv; |
| 2360 | recv_size ? recv_size *= 2 : recv_size = 4) { |
| 2361 | MakeSpeedTestPacket(packet, send_size, recv_size); |
| 2362 | |
| 2363 | packet_times.clear(); |
| 2364 | // Test how long it takes to send 'num_packets' packets |
Pavel Labath | 3aa0491 | 2016-10-31 17:19:42 +0000 | [diff] [blame] | 2365 | const auto start_time = steady_clock::now(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2366 | for (i = 0; i < num_packets; ++i) { |
Pavel Labath | 3aa0491 | 2016-10-31 17:19:42 +0000 | [diff] [blame] | 2367 | const auto packet_start_time = steady_clock::now(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2368 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2369 | SendPacketAndWaitForResponse(packet.GetString(), response, false); |
Pavel Labath | 3aa0491 | 2016-10-31 17:19:42 +0000 | [diff] [blame] | 2370 | const auto packet_end_time = steady_clock::now(); |
| 2371 | packet_times.push_back(packet_end_time - packet_start_time); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2372 | } |
Pavel Labath | 3aa0491 | 2016-10-31 17:19:42 +0000 | [diff] [blame] | 2373 | const auto end_time = steady_clock::now(); |
| 2374 | const auto total_time = end_time - start_time; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2375 | |
| 2376 | float packets_per_second = |
Pavel Labath | 3aa0491 | 2016-10-31 17:19:42 +0000 | [diff] [blame] | 2377 | ((float)num_packets) / duration<float>(total_time).count(); |
| 2378 | auto average_per_packet = total_time / num_packets; |
| 2379 | const duration<float> standard_deviation = |
| 2380 | calculate_standard_deviation(packet_times); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2381 | if (json) { |
Pavel Labath | 2f7cfaf | 2017-02-10 11:49:40 +0000 | [diff] [blame] | 2382 | strm.Format("{0}\n {{\"send_size\" : {1,6}, \"recv_size\" : " |
| 2383 | "{2,6}, \"total_time_nsec\" : {3,12:ns-}, " |
| 2384 | "\"standard_deviation_nsec\" : {4,9:ns-f0}}", |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2385 | result_idx > 0 ? "," : "", send_size, recv_size, |
Pavel Labath | 2f7cfaf | 2017-02-10 11:49:40 +0000 | [diff] [blame] | 2386 | total_time, standard_deviation); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2387 | ++result_idx; |
| 2388 | } else { |
Pavel Labath | 2f7cfaf | 2017-02-10 11:49:40 +0000 | [diff] [blame] | 2389 | strm.Format("qSpeedTest(send={0,7}, recv={1,7}) in {2:s+f9} for " |
| 2390 | "{3,9:f2} packets/s ({4,10:ms+f6} per packet) with " |
| 2391 | "standard deviation of {5,10:ms+f6}\n", |
| 2392 | send_size, recv_size, duration<float>(total_time), |
| 2393 | packets_per_second, duration<float>(average_per_packet), |
| 2394 | standard_deviation); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2395 | } |
| 2396 | strm.Flush(); |
| 2397 | } |
| 2398 | } |
| 2399 | |
Pavel Labath | 2fd9a1e | 2016-11-04 11:49:06 +0000 | [diff] [blame] | 2400 | const float k_recv_amount_mb = (float)recv_amount / (1024.0f * 1024.0f); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2401 | if (json) |
| 2402 | strm.Printf("\n ]\n },\n \"download_speed\" : {\n \"byte_size\" " |
| 2403 | ": %" PRIu64 ",\n \"results\" : [", |
Pavel Labath | 2fd9a1e | 2016-11-04 11:49:06 +0000 | [diff] [blame] | 2404 | recv_amount); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2405 | else |
| 2406 | strm.Printf("Testing receiving %2.1fMB of data using varying receive " |
| 2407 | "packet sizes:\n", |
| 2408 | k_recv_amount_mb); |
| 2409 | strm.Flush(); |
| 2410 | send_size = 0; |
| 2411 | result_idx = 0; |
| 2412 | for (uint32_t recv_size = 32; recv_size <= max_recv; recv_size *= 2) { |
| 2413 | MakeSpeedTestPacket(packet, send_size, recv_size); |
| 2414 | |
| 2415 | // If we have a receive size, test how long it takes to receive 4MB of |
| 2416 | // data |
| 2417 | if (recv_size > 0) { |
Pavel Labath | 3aa0491 | 2016-10-31 17:19:42 +0000 | [diff] [blame] | 2418 | const auto start_time = steady_clock::now(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2419 | uint32_t bytes_read = 0; |
| 2420 | uint32_t packet_count = 0; |
Pavel Labath | 2fd9a1e | 2016-11-04 11:49:06 +0000 | [diff] [blame] | 2421 | while (bytes_read < recv_amount) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2422 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2423 | SendPacketAndWaitForResponse(packet.GetString(), response, false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2424 | bytes_read += recv_size; |
| 2425 | ++packet_count; |
| 2426 | } |
Pavel Labath | 3aa0491 | 2016-10-31 17:19:42 +0000 | [diff] [blame] | 2427 | const auto end_time = steady_clock::now(); |
| 2428 | const auto total_time = end_time - start_time; |
Pavel Labath | 2fd9a1e | 2016-11-04 11:49:06 +0000 | [diff] [blame] | 2429 | float mb_second = ((float)recv_amount) / |
Pavel Labath | 3aa0491 | 2016-10-31 17:19:42 +0000 | [diff] [blame] | 2430 | duration<float>(total_time).count() / |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2431 | (1024.0 * 1024.0); |
| 2432 | float packets_per_second = |
Pavel Labath | 3aa0491 | 2016-10-31 17:19:42 +0000 | [diff] [blame] | 2433 | ((float)packet_count) / duration<float>(total_time).count(); |
| 2434 | const auto average_per_packet = total_time / packet_count; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2435 | |
| 2436 | if (json) { |
Pavel Labath | 2f7cfaf | 2017-02-10 11:49:40 +0000 | [diff] [blame] | 2437 | strm.Format("{0}\n {{\"send_size\" : {1,6}, \"recv_size\" : " |
| 2438 | "{2,6}, \"total_time_nsec\" : {3,12:ns-}}", |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2439 | result_idx > 0 ? "," : "", send_size, recv_size, |
Pavel Labath | 2f7cfaf | 2017-02-10 11:49:40 +0000 | [diff] [blame] | 2440 | total_time); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2441 | ++result_idx; |
| 2442 | } else { |
Pavel Labath | 2f7cfaf | 2017-02-10 11:49:40 +0000 | [diff] [blame] | 2443 | strm.Format("qSpeedTest(send={0,7}, recv={1,7}) {2,6} packets needed " |
| 2444 | "to receive {3:f1}MB in {4:s+f9} for {5} MB/sec for " |
| 2445 | "{6,9:f2} packets/sec ({7,10:ms+f6} per packet)\n", |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2446 | send_size, recv_size, packet_count, k_recv_amount_mb, |
Pavel Labath | 2f7cfaf | 2017-02-10 11:49:40 +0000 | [diff] [blame] | 2447 | duration<float>(total_time), mb_second, |
| 2448 | packets_per_second, duration<float>(average_per_packet)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2449 | } |
| 2450 | strm.Flush(); |
| 2451 | } |
| 2452 | } |
| 2453 | if (json) |
| 2454 | strm.Printf("\n ]\n }\n}\n"); |
| 2455 | else |
| 2456 | strm.EOL(); |
| 2457 | } |
| 2458 | } |
| 2459 | |
| 2460 | bool GDBRemoteCommunicationClient::SendSpeedTestPacket(uint32_t send_size, |
| 2461 | uint32_t recv_size) { |
| 2462 | StreamString packet; |
| 2463 | packet.Printf("qSpeedTest:response_size:%i;data:", recv_size); |
| 2464 | uint32_t bytes_left = send_size; |
| 2465 | while (bytes_left > 0) { |
| 2466 | if (bytes_left >= 26) { |
| 2467 | packet.PutCString("abcdefghijklmnopqrstuvwxyz"); |
| 2468 | bytes_left -= 26; |
| 2469 | } else { |
| 2470 | packet.Printf("%*.*s;", bytes_left, bytes_left, |
| 2471 | "abcdefghijklmnopqrstuvwxyz"); |
| 2472 | bytes_left = 0; |
| 2473 | } |
| 2474 | } |
| 2475 | |
| 2476 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2477 | return SendPacketAndWaitForResponse(packet.GetString(), response, false) == |
| 2478 | PacketResult::Success; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
| 2481 | bool GDBRemoteCommunicationClient::LaunchGDBServer( |
| 2482 | const char *remote_accept_hostname, lldb::pid_t &pid, uint16_t &port, |
| 2483 | std::string &socket_name) { |
| 2484 | pid = LLDB_INVALID_PROCESS_ID; |
| 2485 | port = 0; |
| 2486 | socket_name.clear(); |
| 2487 | |
| 2488 | StringExtractorGDBRemote response; |
| 2489 | StreamString stream; |
| 2490 | stream.PutCString("qLaunchGDBServer;"); |
| 2491 | std::string hostname; |
| 2492 | if (remote_accept_hostname && remote_accept_hostname[0]) |
| 2493 | hostname = remote_accept_hostname; |
| 2494 | else { |
| 2495 | if (HostInfo::GetHostname(hostname)) { |
| 2496 | // Make the GDB server we launch only accept connections from this host |
| 2497 | stream.Printf("host:%s;", hostname.c_str()); |
| 2498 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2499 | // Make the GDB server we launch accept connections from any host since |
| 2500 | // we can't figure out the hostname |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2501 | stream.Printf("host:*;"); |
| 2502 | } |
| 2503 | } |
| 2504 | // give the process a few seconds to startup |
Pavel Labath | 1eff73c | 2016-11-24 10:54:49 +0000 | [diff] [blame] | 2505 | ScopedTimeout timeout(*this, seconds(10)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2506 | |
| 2507 | if (SendPacketAndWaitForResponse(stream.GetString(), response, false) == |
| 2508 | PacketResult::Success) { |
| 2509 | llvm::StringRef name; |
| 2510 | llvm::StringRef value; |
| 2511 | while (response.GetNameColonValue(name, value)) { |
| 2512 | if (name.equals("port")) |
| 2513 | value.getAsInteger(0, port); |
| 2514 | else if (name.equals("pid")) |
| 2515 | value.getAsInteger(0, pid); |
| 2516 | else if (name.compare("socket_name") == 0) { |
| 2517 | StringExtractor extractor(value); |
| 2518 | extractor.GetHexByteString(socket_name); |
| 2519 | } |
| 2520 | } |
| 2521 | return true; |
| 2522 | } |
| 2523 | return false; |
| 2524 | } |
| 2525 | |
| 2526 | size_t GDBRemoteCommunicationClient::QueryGDBServer( |
| 2527 | std::vector<std::pair<uint16_t, std::string>> &connection_urls) { |
| 2528 | connection_urls.clear(); |
| 2529 | |
| 2530 | StringExtractorGDBRemote response; |
| 2531 | if (SendPacketAndWaitForResponse("qQueryGDBServer", response, false) != |
| 2532 | PacketResult::Success) |
| 2533 | return 0; |
| 2534 | |
| 2535 | StructuredData::ObjectSP data = |
| 2536 | StructuredData::ParseJSON(response.GetStringRef()); |
| 2537 | if (!data) |
| 2538 | return 0; |
| 2539 | |
| 2540 | StructuredData::Array *array = data->GetAsArray(); |
| 2541 | if (!array) |
| 2542 | return 0; |
| 2543 | |
| 2544 | for (size_t i = 0, count = array->GetSize(); i < count; ++i) { |
| 2545 | StructuredData::Dictionary *element = nullptr; |
| 2546 | if (!array->GetItemAtIndexAsDictionary(i, element)) |
| 2547 | continue; |
| 2548 | |
| 2549 | uint16_t port = 0; |
| 2550 | if (StructuredData::ObjectSP port_osp = |
| 2551 | element->GetValueForKey(llvm::StringRef("port"))) |
| 2552 | port = port_osp->GetIntegerValue(0); |
| 2553 | |
| 2554 | std::string socket_name; |
| 2555 | if (StructuredData::ObjectSP socket_name_osp = |
| 2556 | element->GetValueForKey(llvm::StringRef("socket_name"))) |
| 2557 | socket_name = socket_name_osp->GetStringValue(); |
| 2558 | |
| 2559 | if (port != 0 || !socket_name.empty()) |
| 2560 | connection_urls.emplace_back(port, socket_name); |
| 2561 | } |
| 2562 | return connection_urls.size(); |
| 2563 | } |
| 2564 | |
| 2565 | bool GDBRemoteCommunicationClient::KillSpawnedProcess(lldb::pid_t pid) { |
| 2566 | StreamString stream; |
| 2567 | stream.Printf("qKillSpawnedProcess:%" PRId64, pid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2568 | |
| 2569 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2570 | if (SendPacketAndWaitForResponse(stream.GetString(), response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2571 | PacketResult::Success) { |
| 2572 | if (response.IsOKResponse()) |
| 2573 | return true; |
| 2574 | } |
| 2575 | return false; |
| 2576 | } |
| 2577 | |
| 2578 | bool GDBRemoteCommunicationClient::SetCurrentThread(uint64_t tid) { |
| 2579 | if (m_curr_tid == tid) |
| 2580 | return true; |
| 2581 | |
| 2582 | char packet[32]; |
| 2583 | int packet_len; |
| 2584 | if (tid == UINT64_MAX) |
| 2585 | packet_len = ::snprintf(packet, sizeof(packet), "Hg-1"); |
| 2586 | else |
| 2587 | packet_len = ::snprintf(packet, sizeof(packet), "Hg%" PRIx64, tid); |
| 2588 | assert(packet_len + 1 < (int)sizeof(packet)); |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2589 | UNUSED_IF_ASSERT_DISABLED(packet_len); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2590 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2591 | if (SendPacketAndWaitForResponse(packet, response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2592 | PacketResult::Success) { |
| 2593 | if (response.IsOKResponse()) { |
| 2594 | m_curr_tid = tid; |
| 2595 | return true; |
| 2596 | } |
| 2597 | |
| 2598 | /* |
| 2599 | * Connected bare-iron target (like YAMON gdb-stub) may not have support for |
| 2600 | * Hg packet. |
| 2601 | * The reply from '?' packet could be as simple as 'S05'. There is no packet |
| 2602 | * which can |
| 2603 | * give us pid and/or tid. Assume pid=tid=1 in such cases. |
| 2604 | */ |
| 2605 | if (response.IsUnsupportedResponse() && IsConnected()) { |
| 2606 | m_curr_tid = 1; |
| 2607 | return true; |
| 2608 | } |
| 2609 | } |
| 2610 | return false; |
| 2611 | } |
| 2612 | |
| 2613 | bool GDBRemoteCommunicationClient::SetCurrentThreadForRun(uint64_t tid) { |
| 2614 | if (m_curr_tid_run == tid) |
| 2615 | return true; |
| 2616 | |
| 2617 | char packet[32]; |
| 2618 | int packet_len; |
| 2619 | if (tid == UINT64_MAX) |
| 2620 | packet_len = ::snprintf(packet, sizeof(packet), "Hc-1"); |
| 2621 | else |
| 2622 | packet_len = ::snprintf(packet, sizeof(packet), "Hc%" PRIx64, tid); |
| 2623 | |
| 2624 | assert(packet_len + 1 < (int)sizeof(packet)); |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2625 | UNUSED_IF_ASSERT_DISABLED(packet_len); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2626 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2627 | if (SendPacketAndWaitForResponse(packet, response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2628 | PacketResult::Success) { |
| 2629 | if (response.IsOKResponse()) { |
| 2630 | m_curr_tid_run = tid; |
| 2631 | return true; |
| 2632 | } |
| 2633 | |
| 2634 | /* |
| 2635 | * Connected bare-iron target (like YAMON gdb-stub) may not have support for |
| 2636 | * Hc packet. |
| 2637 | * The reply from '?' packet could be as simple as 'S05'. There is no packet |
| 2638 | * which can |
| 2639 | * give us pid and/or tid. Assume pid=tid=1 in such cases. |
| 2640 | */ |
| 2641 | if (response.IsUnsupportedResponse() && IsConnected()) { |
| 2642 | m_curr_tid_run = 1; |
| 2643 | return true; |
| 2644 | } |
| 2645 | } |
| 2646 | return false; |
| 2647 | } |
| 2648 | |
| 2649 | bool GDBRemoteCommunicationClient::GetStopReply( |
| 2650 | StringExtractorGDBRemote &response) { |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2651 | if (SendPacketAndWaitForResponse("?", response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2652 | PacketResult::Success) |
| 2653 | return response.IsNormalResponse(); |
| 2654 | return false; |
| 2655 | } |
| 2656 | |
| 2657 | bool GDBRemoteCommunicationClient::GetThreadStopInfo( |
| 2658 | lldb::tid_t tid, StringExtractorGDBRemote &response) { |
| 2659 | if (m_supports_qThreadStopInfo) { |
| 2660 | char packet[256]; |
| 2661 | int packet_len = |
| 2662 | ::snprintf(packet, sizeof(packet), "qThreadStopInfo%" PRIx64, tid); |
| 2663 | assert(packet_len < (int)sizeof(packet)); |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2664 | UNUSED_IF_ASSERT_DISABLED(packet_len); |
| 2665 | if (SendPacketAndWaitForResponse(packet, response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2666 | PacketResult::Success) { |
| 2667 | if (response.IsUnsupportedResponse()) |
| 2668 | m_supports_qThreadStopInfo = false; |
| 2669 | else if (response.IsNormalResponse()) |
| 2670 | return true; |
| 2671 | else |
| 2672 | return false; |
| 2673 | } else { |
| 2674 | m_supports_qThreadStopInfo = false; |
| 2675 | } |
| 2676 | } |
| 2677 | return false; |
| 2678 | } |
| 2679 | |
| 2680 | uint8_t GDBRemoteCommunicationClient::SendGDBStoppointTypePacket( |
| 2681 | GDBStoppointType type, bool insert, addr_t addr, uint32_t length) { |
| 2682 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2683 | LLDB_LOGF(log, "GDBRemoteCommunicationClient::%s() %s at addr = 0x%" PRIx64, |
| 2684 | __FUNCTION__, insert ? "add" : "remove", addr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2685 | |
| 2686 | // Check if the stub is known not to support this breakpoint type |
| 2687 | if (!SupportsGDBStoppointPacket(type)) |
| 2688 | return UINT8_MAX; |
| 2689 | // Construct the breakpoint packet |
| 2690 | char packet[64]; |
| 2691 | const int packet_len = |
| 2692 | ::snprintf(packet, sizeof(packet), "%c%i,%" PRIx64 ",%x", |
| 2693 | insert ? 'Z' : 'z', type, addr, length); |
| 2694 | // Check we haven't overwritten the end of the packet buffer |
| 2695 | assert(packet_len + 1 < (int)sizeof(packet)); |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2696 | UNUSED_IF_ASSERT_DISABLED(packet_len); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2697 | StringExtractorGDBRemote response; |
| 2698 | // Make sure the response is either "OK", "EXX" where XX are two hex digits, |
| 2699 | // or "" (unsupported) |
| 2700 | response.SetResponseValidatorToOKErrorNotSupported(); |
| 2701 | // Try to send the breakpoint packet, and check that it was correctly sent |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2702 | if (SendPacketAndWaitForResponse(packet, response, true) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2703 | PacketResult::Success) { |
| 2704 | // Receive and OK packet when the breakpoint successfully placed |
| 2705 | if (response.IsOKResponse()) |
| 2706 | return 0; |
| 2707 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2708 | // Status while setting breakpoint, send back specific error |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2709 | if (response.IsErrorResponse()) |
| 2710 | return response.GetError(); |
| 2711 | |
| 2712 | // Empty packet informs us that breakpoint is not supported |
| 2713 | if (response.IsUnsupportedResponse()) { |
| 2714 | // Disable this breakpoint type since it is unsupported |
| 2715 | switch (type) { |
| 2716 | case eBreakpointSoftware: |
| 2717 | m_supports_z0 = false; |
| 2718 | break; |
| 2719 | case eBreakpointHardware: |
| 2720 | m_supports_z1 = false; |
| 2721 | break; |
| 2722 | case eWatchpointWrite: |
| 2723 | m_supports_z2 = false; |
| 2724 | break; |
| 2725 | case eWatchpointRead: |
| 2726 | m_supports_z3 = false; |
| 2727 | break; |
| 2728 | case eWatchpointReadWrite: |
| 2729 | m_supports_z4 = false; |
| 2730 | break; |
| 2731 | case eStoppointInvalid: |
| 2732 | return UINT8_MAX; |
| 2733 | } |
| 2734 | } |
| 2735 | } |
| 2736 | // Signal generic failure |
| 2737 | return UINT8_MAX; |
| 2738 | } |
| 2739 | |
| 2740 | size_t GDBRemoteCommunicationClient::GetCurrentThreadIDs( |
| 2741 | std::vector<lldb::tid_t> &thread_ids, bool &sequence_mutex_unavailable) { |
| 2742 | thread_ids.clear(); |
| 2743 | |
| 2744 | Lock lock(*this, false); |
| 2745 | if (lock) { |
| 2746 | sequence_mutex_unavailable = false; |
| 2747 | StringExtractorGDBRemote response; |
| 2748 | |
| 2749 | PacketResult packet_result; |
| 2750 | for (packet_result = |
| 2751 | SendPacketAndWaitForResponseNoLock("qfThreadInfo", response); |
| 2752 | packet_result == PacketResult::Success && response.IsNormalResponse(); |
| 2753 | packet_result = |
| 2754 | SendPacketAndWaitForResponseNoLock("qsThreadInfo", response)) { |
| 2755 | char ch = response.GetChar(); |
| 2756 | if (ch == 'l') |
| 2757 | break; |
| 2758 | if (ch == 'm') { |
| 2759 | do { |
| 2760 | tid_t tid = response.GetHexMaxU64(false, LLDB_INVALID_THREAD_ID); |
| 2761 | |
| 2762 | if (tid != LLDB_INVALID_THREAD_ID) { |
| 2763 | thread_ids.push_back(tid); |
| 2764 | } |
| 2765 | ch = response.GetChar(); // Skip the command separator |
| 2766 | } while (ch == ','); // Make sure we got a comma separator |
| 2767 | } |
| 2768 | } |
| 2769 | |
| 2770 | /* |
| 2771 | * Connected bare-iron target (like YAMON gdb-stub) may not have support for |
| 2772 | * qProcessInfo, qC and qfThreadInfo packets. The reply from '?' packet |
| 2773 | * could |
| 2774 | * be as simple as 'S05'. There is no packet which can give us pid and/or |
| 2775 | * tid. |
| 2776 | * Assume pid=tid=1 in such cases. |
| 2777 | */ |
Tamas Berghammer | 1492cb8 | 2017-09-18 10:24:48 +0000 | [diff] [blame] | 2778 | if ((response.IsUnsupportedResponse() || response.IsNormalResponse()) && |
| 2779 | thread_ids.size() == 0 && IsConnected()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2780 | thread_ids.push_back(1); |
| 2781 | } |
| 2782 | } else { |
David Blaikie | a322f36 | 2017-01-06 00:38:06 +0000 | [diff] [blame] | 2783 | #if !defined(LLDB_CONFIGURATION_DEBUG) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2784 | Log *log(ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet(GDBR_LOG_PROCESS | |
| 2785 | GDBR_LOG_PACKETS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2786 | LLDB_LOGF(log, "error: failed to get packet sequence mutex, not sending " |
| 2787 | "packet 'qfThreadInfo'"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2788 | #endif |
| 2789 | sequence_mutex_unavailable = true; |
| 2790 | } |
| 2791 | return thread_ids.size(); |
| 2792 | } |
| 2793 | |
| 2794 | lldb::addr_t GDBRemoteCommunicationClient::GetShlibInfoAddr() { |
| 2795 | StringExtractorGDBRemote response; |
| 2796 | if (SendPacketAndWaitForResponse("qShlibInfoAddr", response, false) != |
| 2797 | PacketResult::Success || |
| 2798 | !response.IsNormalResponse()) |
| 2799 | return LLDB_INVALID_ADDRESS; |
| 2800 | return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS); |
| 2801 | } |
| 2802 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2803 | lldb_private::Status GDBRemoteCommunicationClient::RunShellCommand( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2804 | const char *command, // Shouldn't be NULL |
| 2805 | const FileSpec & |
| 2806 | working_dir, // Pass empty FileSpec to use the current working directory |
| 2807 | int *status_ptr, // Pass NULL if you don't want the process exit status |
| 2808 | int *signo_ptr, // Pass NULL if you don't want the signal that caused the |
| 2809 | // process to exit |
| 2810 | std::string |
| 2811 | *command_output, // Pass NULL if you don't want the command output |
Pavel Labath | 19dd1a0 | 2018-05-10 10:46:03 +0000 | [diff] [blame] | 2812 | const Timeout<std::micro> &timeout) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2813 | lldb_private::StreamString stream; |
| 2814 | stream.PutCString("qPlatform_shell:"); |
| 2815 | stream.PutBytesAsRawHex8(command, strlen(command)); |
| 2816 | stream.PutChar(','); |
Pavel Labath | 19dd1a0 | 2018-05-10 10:46:03 +0000 | [diff] [blame] | 2817 | uint32_t timeout_sec = UINT32_MAX; |
| 2818 | if (timeout) { |
| 2819 | // TODO: Use chrono version of std::ceil once c++17 is available. |
| 2820 | timeout_sec = std::ceil(std::chrono::duration<double>(*timeout).count()); |
| 2821 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2822 | stream.PutHex32(timeout_sec); |
| 2823 | if (working_dir) { |
| 2824 | std::string path{working_dir.GetPath(false)}; |
| 2825 | stream.PutChar(','); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 2826 | stream.PutStringAsRawHex8(path); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2827 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2828 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2829 | if (SendPacketAndWaitForResponse(stream.GetString(), response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2830 | PacketResult::Success) { |
| 2831 | if (response.GetChar() != 'F') |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2832 | return Status("malformed reply"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2833 | if (response.GetChar() != ',') |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2834 | return Status("malformed reply"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2835 | uint32_t exitcode = response.GetHexMaxU32(false, UINT32_MAX); |
| 2836 | if (exitcode == UINT32_MAX) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2837 | return Status("unable to run remote process"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2838 | else if (status_ptr) |
| 2839 | *status_ptr = exitcode; |
| 2840 | if (response.GetChar() != ',') |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2841 | return Status("malformed reply"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2842 | uint32_t signo = response.GetHexMaxU32(false, UINT32_MAX); |
| 2843 | if (signo_ptr) |
| 2844 | *signo_ptr = signo; |
| 2845 | if (response.GetChar() != ',') |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2846 | return Status("malformed reply"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2847 | std::string output; |
| 2848 | response.GetEscapedBinaryData(output); |
| 2849 | if (command_output) |
| 2850 | command_output->assign(output); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2851 | return Status(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2852 | } |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2853 | return Status("unable to send packet"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2854 | } |
| 2855 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2856 | Status GDBRemoteCommunicationClient::MakeDirectory(const FileSpec &file_spec, |
| 2857 | uint32_t file_permissions) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2858 | std::string path{file_spec.GetPath(false)}; |
| 2859 | lldb_private::StreamString stream; |
| 2860 | stream.PutCString("qPlatform_mkdir:"); |
| 2861 | stream.PutHex32(file_permissions); |
| 2862 | stream.PutChar(','); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 2863 | stream.PutStringAsRawHex8(path); |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 2864 | llvm::StringRef packet = stream.GetString(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2865 | StringExtractorGDBRemote response; |
| 2866 | |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2867 | if (SendPacketAndWaitForResponse(packet, response, false) != |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2868 | PacketResult::Success) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2869 | return Status("failed to send '%s' packet", packet.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2870 | |
| 2871 | if (response.GetChar() != 'F') |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2872 | return Status("invalid response to '%s' packet", packet.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2873 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2874 | return Status(response.GetU32(UINT32_MAX), eErrorTypePOSIX); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2875 | } |
| 2876 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2877 | Status |
| 2878 | GDBRemoteCommunicationClient::SetFilePermissions(const FileSpec &file_spec, |
| 2879 | uint32_t file_permissions) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2880 | std::string path{file_spec.GetPath(false)}; |
| 2881 | lldb_private::StreamString stream; |
| 2882 | stream.PutCString("qPlatform_chmod:"); |
| 2883 | stream.PutHex32(file_permissions); |
| 2884 | stream.PutChar(','); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 2885 | stream.PutStringAsRawHex8(path); |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 2886 | llvm::StringRef packet = stream.GetString(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2887 | StringExtractorGDBRemote response; |
| 2888 | |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2889 | if (SendPacketAndWaitForResponse(packet, response, false) != |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2890 | PacketResult::Success) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2891 | return Status("failed to send '%s' packet", stream.GetData()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2892 | |
| 2893 | if (response.GetChar() != 'F') |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2894 | return Status("invalid response to '%s' packet", stream.GetData()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2895 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2896 | return Status(response.GetU32(UINT32_MAX), eErrorTypePOSIX); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2897 | } |
| 2898 | |
| 2899 | static uint64_t ParseHostIOPacketResponse(StringExtractorGDBRemote &response, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2900 | uint64_t fail_result, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2901 | response.SetFilePos(0); |
| 2902 | if (response.GetChar() != 'F') |
| 2903 | return fail_result; |
| 2904 | int32_t result = response.GetS32(-2); |
| 2905 | if (result == -2) |
| 2906 | return fail_result; |
| 2907 | if (response.GetChar() == ',') { |
| 2908 | int result_errno = response.GetS32(-2); |
| 2909 | if (result_errno != -2) |
| 2910 | error.SetError(result_errno, eErrorTypePOSIX); |
| 2911 | else |
| 2912 | error.SetError(-1, eErrorTypeGeneric); |
| 2913 | } else |
| 2914 | error.Clear(); |
| 2915 | return result; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2916 | } |
| 2917 | lldb::user_id_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2918 | GDBRemoteCommunicationClient::OpenFile(const lldb_private::FileSpec &file_spec, |
Lawrence D'Anna | 62c9fe4 | 2019-10-14 20:15:34 +0000 | [diff] [blame] | 2919 | File::OpenOptions flags, mode_t mode, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2920 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2921 | std::string path(file_spec.GetPath(false)); |
| 2922 | lldb_private::StreamString stream; |
| 2923 | stream.PutCString("vFile:open:"); |
| 2924 | if (path.empty()) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2925 | return UINT64_MAX; |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 2926 | stream.PutStringAsRawHex8(path); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2927 | stream.PutChar(','); |
| 2928 | stream.PutHex32(flags); |
| 2929 | stream.PutChar(','); |
| 2930 | stream.PutHex32(mode); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2931 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2932 | if (SendPacketAndWaitForResponse(stream.GetString(), response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2933 | PacketResult::Success) { |
| 2934 | return ParseHostIOPacketResponse(response, UINT64_MAX, error); |
| 2935 | } |
| 2936 | return UINT64_MAX; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2939 | bool GDBRemoteCommunicationClient::CloseFile(lldb::user_id_t fd, |
| 2940 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2941 | lldb_private::StreamString stream; |
| 2942 | stream.Printf("vFile:close:%i", (int)fd); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2943 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2944 | if (SendPacketAndWaitForResponse(stream.GetString(), response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2945 | PacketResult::Success) { |
| 2946 | return ParseHostIOPacketResponse(response, -1, error) == 0; |
| 2947 | } |
| 2948 | return false; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2949 | } |
| 2950 | |
| 2951 | // Extension of host I/O packets to get the file size. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2952 | lldb::user_id_t GDBRemoteCommunicationClient::GetFileSize( |
| 2953 | const lldb_private::FileSpec &file_spec) { |
| 2954 | std::string path(file_spec.GetPath(false)); |
| 2955 | lldb_private::StreamString stream; |
| 2956 | stream.PutCString("vFile:size:"); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 2957 | stream.PutStringAsRawHex8(path); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2958 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2959 | if (SendPacketAndWaitForResponse(stream.GetString(), response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2960 | PacketResult::Success) { |
| 2961 | if (response.GetChar() != 'F') |
| 2962 | return UINT64_MAX; |
| 2963 | uint32_t retcode = response.GetHexMaxU64(false, UINT64_MAX); |
| 2964 | return retcode; |
| 2965 | } |
| 2966 | return UINT64_MAX; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2967 | } |
| 2968 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2969 | Status |
| 2970 | GDBRemoteCommunicationClient::GetFilePermissions(const FileSpec &file_spec, |
| 2971 | uint32_t &file_permissions) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2972 | std::string path{file_spec.GetPath(false)}; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2973 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2974 | lldb_private::StreamString stream; |
| 2975 | stream.PutCString("vFile:mode:"); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 2976 | stream.PutStringAsRawHex8(path); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2977 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2978 | if (SendPacketAndWaitForResponse(stream.GetString(), response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2979 | PacketResult::Success) { |
| 2980 | if (response.GetChar() != 'F') { |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2981 | error.SetErrorStringWithFormat("invalid response to '%s' packet", |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 2982 | stream.GetData()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2983 | } else { |
| 2984 | const uint32_t mode = response.GetS32(-1); |
| 2985 | if (static_cast<int32_t>(mode) == -1) { |
| 2986 | if (response.GetChar() == ',') { |
| 2987 | int response_errno = response.GetS32(-1); |
| 2988 | if (response_errno > 0) |
| 2989 | error.SetError(response_errno, lldb::eErrorTypePOSIX); |
| 2990 | else |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2991 | error.SetErrorToGenericError(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2992 | } else |
| 2993 | error.SetErrorToGenericError(); |
| 2994 | } else { |
| 2995 | file_permissions = mode & (S_IRWXU | S_IRWXG | S_IRWXO); |
| 2996 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2997 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2998 | } else { |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 2999 | error.SetErrorStringWithFormat("failed to send '%s' packet", |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 3000 | stream.GetData()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3001 | } |
| 3002 | return error; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 3003 | } |
| 3004 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3005 | uint64_t GDBRemoteCommunicationClient::ReadFile(lldb::user_id_t fd, |
| 3006 | uint64_t offset, void *dst, |
| 3007 | uint64_t dst_len, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3008 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3009 | lldb_private::StreamString stream; |
| 3010 | stream.Printf("vFile:pread:%i,%" PRId64 ",%" PRId64, (int)fd, dst_len, |
| 3011 | offset); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3012 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 3013 | if (SendPacketAndWaitForResponse(stream.GetString(), response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3014 | PacketResult::Success) { |
| 3015 | if (response.GetChar() != 'F') |
| 3016 | return 0; |
| 3017 | uint32_t retcode = response.GetHexMaxU32(false, UINT32_MAX); |
| 3018 | if (retcode == UINT32_MAX) |
| 3019 | return retcode; |
| 3020 | const char next = (response.Peek() ? *response.Peek() : 0); |
| 3021 | if (next == ',') |
| 3022 | return 0; |
| 3023 | if (next == ';') { |
| 3024 | response.GetChar(); // skip the semicolon |
| 3025 | std::string buffer; |
| 3026 | if (response.GetEscapedBinaryData(buffer)) { |
| 3027 | const uint64_t data_to_write = |
| 3028 | std::min<uint64_t>(dst_len, buffer.size()); |
| 3029 | if (data_to_write > 0) |
| 3030 | memcpy(dst, &buffer[0], data_to_write); |
| 3031 | return data_to_write; |
| 3032 | } |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 3033 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3034 | } |
| 3035 | return 0; |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 3036 | } |
| 3037 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3038 | uint64_t GDBRemoteCommunicationClient::WriteFile(lldb::user_id_t fd, |
| 3039 | uint64_t offset, |
| 3040 | const void *src, |
| 3041 | uint64_t src_len, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3042 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3043 | lldb_private::StreamGDBRemote stream; |
| 3044 | stream.Printf("vFile:pwrite:%i,%" PRId64 ",", (int)fd, offset); |
| 3045 | stream.PutEscapedBytes(src, src_len); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3046 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 3047 | if (SendPacketAndWaitForResponse(stream.GetString(), response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3048 | PacketResult::Success) { |
| 3049 | if (response.GetChar() != 'F') { |
| 3050 | error.SetErrorStringWithFormat("write file failed"); |
| 3051 | return 0; |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 3052 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3053 | uint64_t bytes_written = response.GetU64(UINT64_MAX); |
| 3054 | if (bytes_written == UINT64_MAX) { |
| 3055 | error.SetErrorToGenericError(); |
| 3056 | if (response.GetChar() == ',') { |
| 3057 | int response_errno = response.GetS32(-1); |
| 3058 | if (response_errno > 0) |
| 3059 | error.SetError(response_errno, lldb::eErrorTypePOSIX); |
| 3060 | } |
| 3061 | return 0; |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 3062 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3063 | return bytes_written; |
| 3064 | } else { |
| 3065 | error.SetErrorString("failed to send vFile:pwrite packet"); |
| 3066 | } |
| 3067 | return 0; |
| 3068 | } |
| 3069 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3070 | Status GDBRemoteCommunicationClient::CreateSymlink(const FileSpec &src, |
| 3071 | const FileSpec &dst) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3072 | std::string src_path{src.GetPath(false)}, dst_path{dst.GetPath(false)}; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3073 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3074 | lldb_private::StreamGDBRemote stream; |
| 3075 | stream.PutCString("vFile:symlink:"); |
| 3076 | // the unix symlink() command reverses its parameters where the dst if first, |
| 3077 | // so we follow suit here |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 3078 | stream.PutStringAsRawHex8(dst_path); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3079 | stream.PutChar(','); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 3080 | stream.PutStringAsRawHex8(src_path); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3081 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 3082 | if (SendPacketAndWaitForResponse(stream.GetString(), response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3083 | PacketResult::Success) { |
| 3084 | if (response.GetChar() == 'F') { |
| 3085 | uint32_t result = response.GetU32(UINT32_MAX); |
| 3086 | if (result != 0) { |
| 3087 | error.SetErrorToGenericError(); |
| 3088 | if (response.GetChar() == ',') { |
| 3089 | int response_errno = response.GetS32(-1); |
| 3090 | if (response_errno > 0) |
| 3091 | error.SetError(response_errno, lldb::eErrorTypePOSIX); |
| 3092 | } |
| 3093 | } |
| 3094 | } else { |
| 3095 | // Should have returned with 'F<result>[,<errno>]' |
| 3096 | error.SetErrorStringWithFormat("symlink failed"); |
| 3097 | } |
| 3098 | } else { |
| 3099 | error.SetErrorString("failed to send vFile:symlink packet"); |
| 3100 | } |
| 3101 | return error; |
| 3102 | } |
| 3103 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3104 | Status GDBRemoteCommunicationClient::Unlink(const FileSpec &file_spec) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3105 | std::string path{file_spec.GetPath(false)}; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3106 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3107 | lldb_private::StreamGDBRemote stream; |
| 3108 | stream.PutCString("vFile:unlink:"); |
| 3109 | // the unix symlink() command reverses its parameters where the dst if first, |
| 3110 | // so we follow suit here |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 3111 | stream.PutStringAsRawHex8(path); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3112 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 3113 | if (SendPacketAndWaitForResponse(stream.GetString(), response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3114 | PacketResult::Success) { |
| 3115 | if (response.GetChar() == 'F') { |
| 3116 | uint32_t result = response.GetU32(UINT32_MAX); |
| 3117 | if (result != 0) { |
| 3118 | error.SetErrorToGenericError(); |
| 3119 | if (response.GetChar() == ',') { |
| 3120 | int response_errno = response.GetS32(-1); |
| 3121 | if (response_errno > 0) |
| 3122 | error.SetError(response_errno, lldb::eErrorTypePOSIX); |
| 3123 | } |
| 3124 | } |
| 3125 | } else { |
| 3126 | // Should have returned with 'F<result>[,<errno>]' |
| 3127 | error.SetErrorStringWithFormat("unlink failed"); |
| 3128 | } |
| 3129 | } else { |
| 3130 | error.SetErrorString("failed to send vFile:unlink packet"); |
| 3131 | } |
| 3132 | return error; |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 3133 | } |
| 3134 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 3135 | // Extension of host I/O packets to get whether a file exists. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3136 | bool GDBRemoteCommunicationClient::GetFileExists( |
| 3137 | const lldb_private::FileSpec &file_spec) { |
| 3138 | std::string path(file_spec.GetPath(false)); |
| 3139 | lldb_private::StreamString stream; |
| 3140 | stream.PutCString("vFile:exists:"); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 3141 | stream.PutStringAsRawHex8(path); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3142 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 3143 | if (SendPacketAndWaitForResponse(stream.GetString(), response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3144 | PacketResult::Success) { |
| 3145 | if (response.GetChar() != 'F') |
| 3146 | return false; |
| 3147 | if (response.GetChar() != ',') |
| 3148 | return false; |
| 3149 | bool retcode = (response.GetChar() != '0'); |
| 3150 | return retcode; |
| 3151 | } |
| 3152 | return false; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 3153 | } |
| 3154 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3155 | bool GDBRemoteCommunicationClient::CalculateMD5( |
| 3156 | const lldb_private::FileSpec &file_spec, uint64_t &high, uint64_t &low) { |
| 3157 | std::string path(file_spec.GetPath(false)); |
| 3158 | lldb_private::StreamString stream; |
| 3159 | stream.PutCString("vFile:MD5:"); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 3160 | stream.PutStringAsRawHex8(path); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3161 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 3162 | if (SendPacketAndWaitForResponse(stream.GetString(), response, false) == |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3163 | PacketResult::Success) { |
| 3164 | if (response.GetChar() != 'F') |
| 3165 | return false; |
| 3166 | if (response.GetChar() != ',') |
| 3167 | return false; |
| 3168 | if (response.Peek() && *response.Peek() == 'x') |
| 3169 | return false; |
| 3170 | low = response.GetHexMaxU64(false, UINT64_MAX); |
| 3171 | high = response.GetHexMaxU64(false, UINT64_MAX); |
Pavel Labath | 4b6f959 | 2016-08-18 08:30:03 +0000 | [diff] [blame] | 3172 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3173 | } |
| 3174 | return false; |
Greg Clayton | f74cf86 | 2013-11-13 23:28:31 +0000 | [diff] [blame] | 3175 | } |
| 3176 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3177 | bool GDBRemoteCommunicationClient::AvoidGPackets(ProcessGDBRemote *process) { |
| 3178 | // Some targets have issues with g/G packets and we need to avoid using them |
| 3179 | if (m_avoid_g_packets == eLazyBoolCalculate) { |
| 3180 | if (process) { |
| 3181 | m_avoid_g_packets = eLazyBoolNo; |
| 3182 | const ArchSpec &arch = process->GetTarget().GetArchitecture(); |
| 3183 | if (arch.IsValid() && |
| 3184 | arch.GetTriple().getVendor() == llvm::Triple::Apple && |
| 3185 | arch.GetTriple().getOS() == llvm::Triple::IOS && |
Jason Molenda | 7dd7a36 | 2019-10-16 19:14:49 +0000 | [diff] [blame] | 3186 | (arch.GetTriple().getArch() == llvm::Triple::aarch64 || |
| 3187 | arch.GetTriple().getArch() == llvm::Triple::aarch64_32)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3188 | m_avoid_g_packets = eLazyBoolYes; |
| 3189 | uint32_t gdb_server_version = GetGDBServerProgramVersion(); |
| 3190 | if (gdb_server_version != 0) { |
| 3191 | const char *gdb_server_name = GetGDBServerProgramName(); |
| 3192 | if (gdb_server_name && strcmp(gdb_server_name, "debugserver") == 0) { |
| 3193 | if (gdb_server_version >= 310) |
| 3194 | m_avoid_g_packets = eLazyBoolNo; |
| 3195 | } |
| 3196 | } |
| 3197 | } |
| 3198 | } |
| 3199 | } |
| 3200 | return m_avoid_g_packets == eLazyBoolYes; |
| 3201 | } |
Saleem Abdulrasool | 2d6a9ec | 2016-07-28 17:32:20 +0000 | [diff] [blame] | 3202 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3203 | DataBufferSP GDBRemoteCommunicationClient::ReadRegister(lldb::tid_t tid, |
| 3204 | uint32_t reg) { |
| 3205 | StreamString payload; |
| 3206 | payload.Printf("p%x", reg); |
| 3207 | StringExtractorGDBRemote response; |
| 3208 | if (SendThreadSpecificPacketAndWaitForResponse( |
| 3209 | tid, std::move(payload), response, false) != PacketResult::Success || |
| 3210 | !response.IsNormalResponse()) |
| 3211 | return nullptr; |
Pavel Labath | 4b6f959 | 2016-08-18 08:30:03 +0000 | [diff] [blame] | 3212 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3213 | DataBufferSP buffer_sp( |
| 3214 | new DataBufferHeap(response.GetStringRef().size() / 2, 0)); |
| 3215 | response.GetHexBytes(buffer_sp->GetData(), '\xcc'); |
| 3216 | return buffer_sp; |
| 3217 | } |
Pavel Labath | 4b6f959 | 2016-08-18 08:30:03 +0000 | [diff] [blame] | 3218 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3219 | DataBufferSP GDBRemoteCommunicationClient::ReadAllRegisters(lldb::tid_t tid) { |
| 3220 | StreamString payload; |
| 3221 | payload.PutChar('g'); |
| 3222 | StringExtractorGDBRemote response; |
| 3223 | if (SendThreadSpecificPacketAndWaitForResponse( |
| 3224 | tid, std::move(payload), response, false) != PacketResult::Success || |
| 3225 | !response.IsNormalResponse()) |
| 3226 | return nullptr; |
| 3227 | |
| 3228 | DataBufferSP buffer_sp( |
| 3229 | new DataBufferHeap(response.GetStringRef().size() / 2, 0)); |
| 3230 | response.GetHexBytes(buffer_sp->GetData(), '\xcc'); |
| 3231 | return buffer_sp; |
| 3232 | } |
| 3233 | |
| 3234 | bool GDBRemoteCommunicationClient::WriteRegister(lldb::tid_t tid, |
| 3235 | uint32_t reg_num, |
| 3236 | llvm::ArrayRef<uint8_t> data) { |
| 3237 | StreamString payload; |
| 3238 | payload.Printf("P%x=", reg_num); |
| 3239 | payload.PutBytesAsRawHex8(data.data(), data.size(), |
| 3240 | endian::InlHostByteOrder(), |
| 3241 | endian::InlHostByteOrder()); |
| 3242 | StringExtractorGDBRemote response; |
| 3243 | return SendThreadSpecificPacketAndWaitForResponse(tid, std::move(payload), |
| 3244 | response, false) == |
| 3245 | PacketResult::Success && |
| 3246 | response.IsOKResponse(); |
| 3247 | } |
| 3248 | |
| 3249 | bool GDBRemoteCommunicationClient::WriteAllRegisters( |
| 3250 | lldb::tid_t tid, llvm::ArrayRef<uint8_t> data) { |
| 3251 | StreamString payload; |
| 3252 | payload.PutChar('G'); |
| 3253 | payload.PutBytesAsRawHex8(data.data(), data.size(), |
| 3254 | endian::InlHostByteOrder(), |
| 3255 | endian::InlHostByteOrder()); |
| 3256 | StringExtractorGDBRemote response; |
| 3257 | return SendThreadSpecificPacketAndWaitForResponse(tid, std::move(payload), |
| 3258 | response, false) == |
| 3259 | PacketResult::Success && |
| 3260 | response.IsOKResponse(); |
| 3261 | } |
| 3262 | |
| 3263 | bool GDBRemoteCommunicationClient::SaveRegisterState(lldb::tid_t tid, |
| 3264 | uint32_t &save_id) { |
| 3265 | save_id = 0; // Set to invalid save ID |
| 3266 | if (m_supports_QSaveRegisterState == eLazyBoolNo) |
Greg Clayton | f74cf86 | 2013-11-13 23:28:31 +0000 | [diff] [blame] | 3267 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3268 | |
| 3269 | m_supports_QSaveRegisterState = eLazyBoolYes; |
| 3270 | StreamString payload; |
| 3271 | payload.PutCString("QSaveRegisterState"); |
| 3272 | StringExtractorGDBRemote response; |
| 3273 | if (SendThreadSpecificPacketAndWaitForResponse( |
| 3274 | tid, std::move(payload), response, false) != PacketResult::Success) |
| 3275 | return false; |
| 3276 | |
| 3277 | if (response.IsUnsupportedResponse()) |
| 3278 | m_supports_QSaveRegisterState = eLazyBoolNo; |
| 3279 | |
| 3280 | const uint32_t response_save_id = response.GetU32(0); |
| 3281 | if (response_save_id == 0) |
| 3282 | return false; |
| 3283 | |
| 3284 | save_id = response_save_id; |
| 3285 | return true; |
Greg Clayton | f74cf86 | 2013-11-13 23:28:31 +0000 | [diff] [blame] | 3286 | } |
Oleksiy Vyalov | 6801be3 | 2015-02-25 22:15:44 +0000 | [diff] [blame] | 3287 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3288 | bool GDBRemoteCommunicationClient::RestoreRegisterState(lldb::tid_t tid, |
| 3289 | uint32_t save_id) { |
| 3290 | // We use the "m_supports_QSaveRegisterState" variable here because the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3291 | // QSaveRegisterState and QRestoreRegisterState packets must both be |
| 3292 | // supported in order to be useful |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3293 | if (m_supports_QSaveRegisterState == eLazyBoolNo) |
| 3294 | return false; |
Pavel Labath | 27402d2 | 2016-08-18 12:32:41 +0000 | [diff] [blame] | 3295 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3296 | StreamString payload; |
| 3297 | payload.Printf("QRestoreRegisterState:%u", save_id); |
| 3298 | StringExtractorGDBRemote response; |
| 3299 | if (SendThreadSpecificPacketAndWaitForResponse( |
| 3300 | tid, std::move(payload), response, false) != PacketResult::Success) |
| 3301 | return false; |
Pavel Labath | 27402d2 | 2016-08-18 12:32:41 +0000 | [diff] [blame] | 3302 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3303 | if (response.IsOKResponse()) |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 3304 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3305 | |
| 3306 | if (response.IsUnsupportedResponse()) |
| 3307 | m_supports_QSaveRegisterState = eLazyBoolNo; |
| 3308 | return false; |
| 3309 | } |
| 3310 | |
| 3311 | bool GDBRemoteCommunicationClient::SyncThreadState(lldb::tid_t tid) { |
| 3312 | if (!GetSyncThreadStateSupported()) |
| 3313 | return false; |
| 3314 | |
| 3315 | StreamString packet; |
| 3316 | StringExtractorGDBRemote response; |
| 3317 | packet.Printf("QSyncThreadState:%4.4" PRIx64 ";", tid); |
| 3318 | return SendPacketAndWaitForResponse(packet.GetString(), response, false) == |
| 3319 | GDBRemoteCommunication::PacketResult::Success && |
| 3320 | response.IsOKResponse(); |
| 3321 | } |
| 3322 | |
Ravitheja Addepally | e714c4f | 2017-05-26 11:46:27 +0000 | [diff] [blame] | 3323 | lldb::user_id_t |
| 3324 | GDBRemoteCommunicationClient::SendStartTracePacket(const TraceOptions &options, |
| 3325 | Status &error) { |
| 3326 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 3327 | lldb::user_id_t ret_uid = LLDB_INVALID_UID; |
| 3328 | |
| 3329 | StreamGDBRemote escaped_packet; |
| 3330 | escaped_packet.PutCString("jTraceStart:"); |
| 3331 | |
| 3332 | StructuredData::Dictionary json_packet; |
| 3333 | json_packet.AddIntegerItem("type", options.getType()); |
| 3334 | json_packet.AddIntegerItem("buffersize", options.getTraceBufferSize()); |
| 3335 | json_packet.AddIntegerItem("metabuffersize", options.getMetaDataBufferSize()); |
| 3336 | |
| 3337 | if (options.getThreadID() != LLDB_INVALID_THREAD_ID) |
| 3338 | json_packet.AddIntegerItem("threadid", options.getThreadID()); |
| 3339 | |
| 3340 | StructuredData::DictionarySP custom_params = options.getTraceParams(); |
| 3341 | if (custom_params) |
| 3342 | json_packet.AddItem("params", custom_params); |
| 3343 | |
| 3344 | StreamString json_string; |
| 3345 | json_packet.Dump(json_string, false); |
| 3346 | escaped_packet.PutEscapedBytes(json_string.GetData(), json_string.GetSize()); |
| 3347 | |
| 3348 | StringExtractorGDBRemote response; |
| 3349 | if (SendPacketAndWaitForResponse(escaped_packet.GetString(), response, |
| 3350 | true) == |
| 3351 | GDBRemoteCommunication::PacketResult::Success) { |
| 3352 | if (!response.IsNormalResponse()) { |
Ravitheja Addepally | dab1d5f | 2017-07-12 11:15:34 +0000 | [diff] [blame] | 3353 | error = response.GetStatus(); |
| 3354 | LLDB_LOG(log, "Target does not support Tracing , error {0}", error); |
Ravitheja Addepally | e714c4f | 2017-05-26 11:46:27 +0000 | [diff] [blame] | 3355 | } else { |
| 3356 | ret_uid = response.GetHexMaxU64(false, LLDB_INVALID_UID); |
| 3357 | } |
| 3358 | } else { |
| 3359 | LLDB_LOG(log, "failed to send packet"); |
| 3360 | error.SetErrorStringWithFormat("failed to send packet: '%s'", |
| 3361 | escaped_packet.GetData()); |
| 3362 | } |
| 3363 | return ret_uid; |
| 3364 | } |
| 3365 | |
| 3366 | Status |
| 3367 | GDBRemoteCommunicationClient::SendStopTracePacket(lldb::user_id_t uid, |
| 3368 | lldb::tid_t thread_id) { |
| 3369 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 3370 | StringExtractorGDBRemote response; |
| 3371 | Status error; |
| 3372 | |
| 3373 | StructuredData::Dictionary json_packet; |
| 3374 | StreamGDBRemote escaped_packet; |
| 3375 | StreamString json_string; |
| 3376 | escaped_packet.PutCString("jTraceStop:"); |
| 3377 | |
| 3378 | json_packet.AddIntegerItem("traceid", uid); |
| 3379 | |
| 3380 | if (thread_id != LLDB_INVALID_THREAD_ID) |
| 3381 | json_packet.AddIntegerItem("threadid", thread_id); |
| 3382 | |
| 3383 | json_packet.Dump(json_string, false); |
| 3384 | |
| 3385 | escaped_packet.PutEscapedBytes(json_string.GetData(), json_string.GetSize()); |
| 3386 | |
| 3387 | if (SendPacketAndWaitForResponse(escaped_packet.GetString(), response, |
| 3388 | true) == |
| 3389 | GDBRemoteCommunication::PacketResult::Success) { |
| 3390 | if (!response.IsOKResponse()) { |
Ravitheja Addepally | dab1d5f | 2017-07-12 11:15:34 +0000 | [diff] [blame] | 3391 | error = response.GetStatus(); |
Ravitheja Addepally | e714c4f | 2017-05-26 11:46:27 +0000 | [diff] [blame] | 3392 | LLDB_LOG(log, "stop tracing failed"); |
| 3393 | } |
| 3394 | } else { |
| 3395 | LLDB_LOG(log, "failed to send packet"); |
| 3396 | error.SetErrorStringWithFormat( |
| 3397 | "failed to send packet: '%s' with error '%d'", escaped_packet.GetData(), |
| 3398 | response.GetError()); |
| 3399 | } |
| 3400 | return error; |
| 3401 | } |
| 3402 | |
| 3403 | Status GDBRemoteCommunicationClient::SendGetDataPacket( |
| 3404 | lldb::user_id_t uid, lldb::tid_t thread_id, |
| 3405 | llvm::MutableArrayRef<uint8_t> &buffer, size_t offset) { |
Ravitheja Addepally | dab1d5f | 2017-07-12 11:15:34 +0000 | [diff] [blame] | 3406 | |
Ravitheja Addepally | e714c4f | 2017-05-26 11:46:27 +0000 | [diff] [blame] | 3407 | StreamGDBRemote escaped_packet; |
| 3408 | escaped_packet.PutCString("jTraceBufferRead:"); |
| 3409 | return SendGetTraceDataPacket(escaped_packet, uid, thread_id, buffer, offset); |
| 3410 | } |
| 3411 | |
| 3412 | Status GDBRemoteCommunicationClient::SendGetMetaDataPacket( |
| 3413 | lldb::user_id_t uid, lldb::tid_t thread_id, |
| 3414 | llvm::MutableArrayRef<uint8_t> &buffer, size_t offset) { |
Ravitheja Addepally | dab1d5f | 2017-07-12 11:15:34 +0000 | [diff] [blame] | 3415 | |
Ravitheja Addepally | e714c4f | 2017-05-26 11:46:27 +0000 | [diff] [blame] | 3416 | StreamGDBRemote escaped_packet; |
| 3417 | escaped_packet.PutCString("jTraceMetaRead:"); |
| 3418 | return SendGetTraceDataPacket(escaped_packet, uid, thread_id, buffer, offset); |
| 3419 | } |
| 3420 | |
| 3421 | Status |
| 3422 | GDBRemoteCommunicationClient::SendGetTraceConfigPacket(lldb::user_id_t uid, |
| 3423 | TraceOptions &options) { |
| 3424 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 3425 | StringExtractorGDBRemote response; |
| 3426 | Status error; |
| 3427 | |
| 3428 | StreamString json_string; |
| 3429 | StreamGDBRemote escaped_packet; |
| 3430 | escaped_packet.PutCString("jTraceConfigRead:"); |
| 3431 | |
| 3432 | StructuredData::Dictionary json_packet; |
| 3433 | json_packet.AddIntegerItem("traceid", uid); |
| 3434 | |
| 3435 | if (options.getThreadID() != LLDB_INVALID_THREAD_ID) |
| 3436 | json_packet.AddIntegerItem("threadid", options.getThreadID()); |
| 3437 | |
| 3438 | json_packet.Dump(json_string, false); |
| 3439 | escaped_packet.PutEscapedBytes(json_string.GetData(), json_string.GetSize()); |
| 3440 | |
| 3441 | if (SendPacketAndWaitForResponse(escaped_packet.GetString(), response, |
| 3442 | true) == |
| 3443 | GDBRemoteCommunication::PacketResult::Success) { |
| 3444 | if (response.IsNormalResponse()) { |
| 3445 | uint64_t type = std::numeric_limits<uint64_t>::max(); |
| 3446 | uint64_t buffersize = std::numeric_limits<uint64_t>::max(); |
| 3447 | uint64_t metabuffersize = std::numeric_limits<uint64_t>::max(); |
| 3448 | |
| 3449 | auto json_object = StructuredData::ParseJSON(response.Peek()); |
| 3450 | |
| 3451 | if (!json_object || |
Stephan Bergmann | cf4321a | 2017-05-29 08:51:58 +0000 | [diff] [blame] | 3452 | json_object->GetType() != lldb::eStructuredDataTypeDictionary) { |
Ravitheja Addepally | e714c4f | 2017-05-26 11:46:27 +0000 | [diff] [blame] | 3453 | error.SetErrorString("Invalid Configuration obtained"); |
| 3454 | return error; |
| 3455 | } |
| 3456 | |
| 3457 | auto json_dict = json_object->GetAsDictionary(); |
| 3458 | |
| 3459 | json_dict->GetValueForKeyAsInteger<uint64_t>("metabuffersize", |
| 3460 | metabuffersize); |
| 3461 | options.setMetaDataBufferSize(metabuffersize); |
| 3462 | |
| 3463 | json_dict->GetValueForKeyAsInteger<uint64_t>("buffersize", buffersize); |
| 3464 | options.setTraceBufferSize(buffersize); |
| 3465 | |
| 3466 | json_dict->GetValueForKeyAsInteger<uint64_t>("type", type); |
| 3467 | options.setType(static_cast<lldb::TraceType>(type)); |
| 3468 | |
| 3469 | StructuredData::ObjectSP custom_params_sp = |
| 3470 | json_dict->GetValueForKey("params"); |
| 3471 | if (custom_params_sp) { |
| 3472 | if (custom_params_sp->GetType() != |
Stephan Bergmann | cf4321a | 2017-05-29 08:51:58 +0000 | [diff] [blame] | 3473 | lldb::eStructuredDataTypeDictionary) { |
Ravitheja Addepally | e714c4f | 2017-05-26 11:46:27 +0000 | [diff] [blame] | 3474 | error.SetErrorString("Invalid Configuration obtained"); |
| 3475 | return error; |
| 3476 | } else |
| 3477 | options.setTraceParams( |
| 3478 | static_pointer_cast<StructuredData::Dictionary>( |
| 3479 | custom_params_sp)); |
| 3480 | } |
| 3481 | } else { |
Ravitheja Addepally | dab1d5f | 2017-07-12 11:15:34 +0000 | [diff] [blame] | 3482 | error = response.GetStatus(); |
Ravitheja Addepally | e714c4f | 2017-05-26 11:46:27 +0000 | [diff] [blame] | 3483 | } |
| 3484 | } else { |
| 3485 | LLDB_LOG(log, "failed to send packet"); |
| 3486 | error.SetErrorStringWithFormat("failed to send packet: '%s'", |
| 3487 | escaped_packet.GetData()); |
| 3488 | } |
| 3489 | return error; |
| 3490 | } |
| 3491 | |
| 3492 | Status GDBRemoteCommunicationClient::SendGetTraceDataPacket( |
| 3493 | StreamGDBRemote &packet, lldb::user_id_t uid, lldb::tid_t thread_id, |
| 3494 | llvm::MutableArrayRef<uint8_t> &buffer, size_t offset) { |
| 3495 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 3496 | Status error; |
| 3497 | |
| 3498 | StructuredData::Dictionary json_packet; |
| 3499 | |
| 3500 | json_packet.AddIntegerItem("traceid", uid); |
| 3501 | json_packet.AddIntegerItem("offset", offset); |
| 3502 | json_packet.AddIntegerItem("buffersize", buffer.size()); |
| 3503 | |
| 3504 | if (thread_id != LLDB_INVALID_THREAD_ID) |
| 3505 | json_packet.AddIntegerItem("threadid", thread_id); |
| 3506 | |
| 3507 | StreamString json_string; |
| 3508 | json_packet.Dump(json_string, false); |
| 3509 | |
| 3510 | packet.PutEscapedBytes(json_string.GetData(), json_string.GetSize()); |
| 3511 | StringExtractorGDBRemote response; |
| 3512 | if (SendPacketAndWaitForResponse(packet.GetString(), response, true) == |
| 3513 | GDBRemoteCommunication::PacketResult::Success) { |
| 3514 | if (response.IsNormalResponse()) { |
| 3515 | size_t filled_size = response.GetHexBytesAvail(buffer); |
| 3516 | buffer = llvm::MutableArrayRef<uint8_t>(buffer.data(), filled_size); |
| 3517 | } else { |
Ravitheja Addepally | dab1d5f | 2017-07-12 11:15:34 +0000 | [diff] [blame] | 3518 | error = response.GetStatus(); |
Ravitheja Addepally | e714c4f | 2017-05-26 11:46:27 +0000 | [diff] [blame] | 3519 | buffer = buffer.slice(buffer.size()); |
| 3520 | } |
| 3521 | } else { |
| 3522 | LLDB_LOG(log, "failed to send packet"); |
| 3523 | error.SetErrorStringWithFormat("failed to send packet: '%s'", |
| 3524 | packet.GetData()); |
| 3525 | buffer = buffer.slice(buffer.size()); |
| 3526 | } |
| 3527 | return error; |
| 3528 | } |
| 3529 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3530 | bool GDBRemoteCommunicationClient::GetModuleInfo( |
| 3531 | const FileSpec &module_file_spec, const lldb_private::ArchSpec &arch_spec, |
| 3532 | ModuleSpec &module_spec) { |
| 3533 | if (!m_supports_qModuleInfo) |
| 3534 | return false; |
| 3535 | |
| 3536 | std::string module_path = module_file_spec.GetPath(false); |
| 3537 | if (module_path.empty()) |
| 3538 | return false; |
| 3539 | |
| 3540 | StreamString packet; |
| 3541 | packet.PutCString("qModuleInfo:"); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 3542 | packet.PutStringAsRawHex8(module_path); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3543 | packet.PutCString(";"); |
| 3544 | const auto &triple = arch_spec.GetTriple().getTriple(); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 3545 | packet.PutStringAsRawHex8(triple); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3546 | |
| 3547 | StringExtractorGDBRemote response; |
Pavel Labath | 0f8f0d3 | 2016-09-23 09:11:49 +0000 | [diff] [blame] | 3548 | if (SendPacketAndWaitForResponse(packet.GetString(), response, false) != |
| 3549 | PacketResult::Success) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3550 | return false; |
| 3551 | |
| 3552 | if (response.IsErrorResponse()) |
| 3553 | return false; |
| 3554 | |
| 3555 | if (response.IsUnsupportedResponse()) { |
| 3556 | m_supports_qModuleInfo = false; |
| 3557 | return false; |
| 3558 | } |
| 3559 | |
| 3560 | llvm::StringRef name; |
| 3561 | llvm::StringRef value; |
| 3562 | |
| 3563 | module_spec.Clear(); |
| 3564 | module_spec.GetFileSpec() = module_file_spec; |
| 3565 | |
| 3566 | while (response.GetNameColonValue(name, value)) { |
| 3567 | if (name == "uuid" || name == "md5") { |
| 3568 | StringExtractor extractor(value); |
| 3569 | std::string uuid; |
| 3570 | extractor.GetHexByteString(uuid); |
Pavel Labath | a174bcb | 2018-06-21 15:24:39 +0000 | [diff] [blame] | 3571 | module_spec.GetUUID().SetFromStringRef(uuid, uuid.size() / 2); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3572 | } else if (name == "triple") { |
| 3573 | StringExtractor extractor(value); |
| 3574 | std::string triple; |
| 3575 | extractor.GetHexByteString(triple); |
| 3576 | module_spec.GetArchitecture().SetTriple(triple.c_str()); |
| 3577 | } else if (name == "file_offset") { |
| 3578 | uint64_t ival = 0; |
| 3579 | if (!value.getAsInteger(16, ival)) |
| 3580 | module_spec.SetObjectOffset(ival); |
| 3581 | } else if (name == "file_size") { |
| 3582 | uint64_t ival = 0; |
| 3583 | if (!value.getAsInteger(16, ival)) |
| 3584 | module_spec.SetObjectSize(ival); |
| 3585 | } else if (name == "file_path") { |
| 3586 | StringExtractor extractor(value); |
| 3587 | std::string path; |
| 3588 | extractor.GetHexByteString(path); |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 3589 | module_spec.GetFileSpec() = FileSpec(path, arch_spec.GetTriple()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3590 | } |
| 3591 | } |
| 3592 | |
| 3593 | return true; |
Oleksiy Vyalov | 6801be3 | 2015-02-25 22:15:44 +0000 | [diff] [blame] | 3594 | } |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3595 | |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 3596 | static llvm::Optional<ModuleSpec> |
| 3597 | ParseModuleSpec(StructuredData::Dictionary *dict) { |
| 3598 | ModuleSpec result; |
| 3599 | if (!dict) |
| 3600 | return llvm::None; |
| 3601 | |
Zachary Turner | 2833321 | 2017-05-12 05:49:54 +0000 | [diff] [blame] | 3602 | llvm::StringRef string; |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 3603 | uint64_t integer; |
| 3604 | |
| 3605 | if (!dict->GetValueForKeyAsString("uuid", string)) |
| 3606 | return llvm::None; |
Pavel Labath | 8c92c89 | 2017-12-18 14:31:44 +0000 | [diff] [blame] | 3607 | if (result.GetUUID().SetFromStringRef(string, string.size() / 2) != |
| 3608 | string.size()) |
| 3609 | return llvm::None; |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 3610 | |
| 3611 | if (!dict->GetValueForKeyAsInteger("file_offset", integer)) |
| 3612 | return llvm::None; |
| 3613 | result.SetObjectOffset(integer); |
| 3614 | |
| 3615 | if (!dict->GetValueForKeyAsInteger("file_size", integer)) |
| 3616 | return llvm::None; |
| 3617 | result.SetObjectSize(integer); |
| 3618 | |
| 3619 | if (!dict->GetValueForKeyAsString("triple", string)) |
| 3620 | return llvm::None; |
Zachary Turner | 2833321 | 2017-05-12 05:49:54 +0000 | [diff] [blame] | 3621 | result.GetArchitecture().SetTriple(string); |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 3622 | |
| 3623 | if (!dict->GetValueForKeyAsString("file_path", string)) |
| 3624 | return llvm::None; |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 3625 | result.GetFileSpec() = FileSpec(string, result.GetArchitecture().GetTriple()); |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 3626 | |
| 3627 | return result; |
| 3628 | } |
| 3629 | |
| 3630 | llvm::Optional<std::vector<ModuleSpec>> |
| 3631 | GDBRemoteCommunicationClient::GetModulesInfo( |
| 3632 | llvm::ArrayRef<FileSpec> module_file_specs, const llvm::Triple &triple) { |
Jonas Devlieghere | 2a5a906 | 2019-10-02 18:02:26 +0000 | [diff] [blame] | 3633 | namespace json = llvm::json; |
| 3634 | |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 3635 | if (!m_supports_jModulesInfo) |
| 3636 | return llvm::None; |
| 3637 | |
Jonas Devlieghere | 2a5a906 | 2019-10-02 18:02:26 +0000 | [diff] [blame] | 3638 | json::Array module_array; |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 3639 | for (const FileSpec &module_file_spec : module_file_specs) { |
Jonas Devlieghere | 2a5a906 | 2019-10-02 18:02:26 +0000 | [diff] [blame] | 3640 | module_array.push_back( |
| 3641 | json::Object{{"file", module_file_spec.GetPath(false)}, |
| 3642 | {"triple", triple.getTriple()}}); |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 3643 | } |
| 3644 | StreamString unescaped_payload; |
| 3645 | unescaped_payload.PutCString("jModulesInfo:"); |
Jonas Devlieghere | 2a5a906 | 2019-10-02 18:02:26 +0000 | [diff] [blame] | 3646 | unescaped_payload.AsRawOstream() << std::move(module_array); |
| 3647 | |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 3648 | StreamGDBRemote payload; |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 3649 | payload.PutEscapedBytes(unescaped_payload.GetString().data(), |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 3650 | unescaped_payload.GetSize()); |
| 3651 | |
Greg Clayton | 70a9f51 | 2017-04-14 17:10:04 +0000 | [diff] [blame] | 3652 | // Increase the timeout for jModulesInfo since this packet can take longer. |
| 3653 | ScopedTimeout timeout(*this, std::chrono::seconds(10)); |
| 3654 | |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 3655 | StringExtractorGDBRemote response; |
| 3656 | if (SendPacketAndWaitForResponse(payload.GetString(), response, false) != |
| 3657 | PacketResult::Success || |
| 3658 | response.IsErrorResponse()) |
| 3659 | return llvm::None; |
| 3660 | |
| 3661 | if (response.IsUnsupportedResponse()) { |
| 3662 | m_supports_jModulesInfo = false; |
| 3663 | return llvm::None; |
| 3664 | } |
| 3665 | |
| 3666 | StructuredData::ObjectSP response_object_sp = |
| 3667 | StructuredData::ParseJSON(response.GetStringRef()); |
| 3668 | if (!response_object_sp) |
| 3669 | return llvm::None; |
| 3670 | |
| 3671 | StructuredData::Array *response_array = response_object_sp->GetAsArray(); |
| 3672 | if (!response_array) |
| 3673 | return llvm::None; |
| 3674 | |
| 3675 | std::vector<ModuleSpec> result; |
| 3676 | for (size_t i = 0; i < response_array->GetSize(); ++i) { |
| 3677 | if (llvm::Optional<ModuleSpec> module_spec = ParseModuleSpec( |
| 3678 | response_array->GetItemAtIndex(i)->GetAsDictionary())) |
| 3679 | result.push_back(*module_spec); |
| 3680 | } |
| 3681 | |
| 3682 | return result; |
| 3683 | } |
| 3684 | |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3685 | // query the target remote for extended information using the qXfer packet |
| 3686 | // |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3687 | // example: object='features', annex='target.xml', out=<xml output> return: |
| 3688 | // 'true' on success |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3689 | // 'false' on failure (err set) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3690 | bool GDBRemoteCommunicationClient::ReadExtFeature( |
| 3691 | const lldb_private::ConstString object, |
| 3692 | const lldb_private::ConstString annex, std::string &out, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3693 | lldb_private::Status &err) { |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3694 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3695 | std::stringstream output; |
| 3696 | StringExtractorGDBRemote chunk; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3697 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3698 | uint64_t size = GetRemoteMaxPacketSize(); |
| 3699 | if (size == 0) |
| 3700 | size = 0x1000; |
| 3701 | size = size - 1; // Leave space for the 'm' or 'l' character in the response |
| 3702 | int offset = 0; |
| 3703 | bool active = true; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3704 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3705 | // loop until all data has been read |
| 3706 | while (active) { |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3707 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3708 | // send query extended feature packet |
| 3709 | std::stringstream packet; |
| 3710 | packet << "qXfer:" << object.AsCString("") |
| 3711 | << ":read:" << annex.AsCString("") << ":" << std::hex << offset |
| 3712 | << "," << std::hex << size; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3713 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3714 | GDBRemoteCommunication::PacketResult res = |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 3715 | SendPacketAndWaitForResponse(packet.str(), chunk, false); |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3716 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3717 | if (res != GDBRemoteCommunication::PacketResult::Success) { |
| 3718 | err.SetErrorString("Error sending $qXfer packet"); |
| 3719 | return false; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3720 | } |
| 3721 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3722 | const std::string &str = chunk.GetStringRef(); |
| 3723 | if (str.length() == 0) { |
| 3724 | // should have some data in chunk |
| 3725 | err.SetErrorString("Empty response from $qXfer packet"); |
| 3726 | return false; |
| 3727 | } |
| 3728 | |
| 3729 | // check packet code |
| 3730 | switch (str[0]) { |
| 3731 | // last chunk |
| 3732 | case ('l'): |
| 3733 | active = false; |
| 3734 | LLVM_FALLTHROUGH; |
| 3735 | |
| 3736 | // more chunks |
| 3737 | case ('m'): |
| 3738 | if (str.length() > 1) |
| 3739 | output << &str[1]; |
| 3740 | offset += size; |
| 3741 | break; |
| 3742 | |
| 3743 | // unknown chunk |
| 3744 | default: |
| 3745 | err.SetErrorString("Invalid continuation code from $qXfer packet"); |
| 3746 | return false; |
| 3747 | } |
| 3748 | } |
| 3749 | |
| 3750 | out = output.str(); |
| 3751 | err.Success(); |
| 3752 | return true; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3753 | } |
Greg Clayton | 0b90be1 | 2015-06-23 21:27:50 +0000 | [diff] [blame] | 3754 | |
| 3755 | // Notify the target that gdb is prepared to serve symbol lookup requests. |
| 3756 | // packet: "qSymbol::" |
| 3757 | // reply: |
| 3758 | // OK The target does not need to look up any (more) symbols. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3759 | // qSymbol:<sym_name> The target requests the value of symbol sym_name (hex |
| 3760 | // encoded). |
| 3761 | // LLDB may provide the value by sending another qSymbol |
| 3762 | // packet |
Greg Clayton | 0b90be1 | 2015-06-23 21:27:50 +0000 | [diff] [blame] | 3763 | // in the form of"qSymbol:<sym_value>:<sym_name>". |
Jason Molenda | 50018d3 | 2016-01-13 04:08:10 +0000 | [diff] [blame] | 3764 | // |
| 3765 | // Three examples: |
| 3766 | // |
| 3767 | // lldb sends: qSymbol:: |
| 3768 | // lldb receives: OK |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3769 | // Remote gdb stub does not need to know the addresses of any symbols, lldb |
| 3770 | // does not |
Jason Molenda | 50018d3 | 2016-01-13 04:08:10 +0000 | [diff] [blame] | 3771 | // need to ask again in this session. |
| 3772 | // |
| 3773 | // lldb sends: qSymbol:: |
| 3774 | // lldb receives: qSymbol:64697370617463685f71756575655f6f666673657473 |
| 3775 | // lldb sends: qSymbol::64697370617463685f71756575655f6f666673657473 |
| 3776 | // lldb receives: OK |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3777 | // Remote gdb stub asks for address of 'dispatch_queue_offsets'. lldb does |
| 3778 | // not know |
| 3779 | // the address at this time. lldb needs to send qSymbol:: again when it has |
| 3780 | // more |
Jason Molenda | 50018d3 | 2016-01-13 04:08:10 +0000 | [diff] [blame] | 3781 | // solibs loaded. |
| 3782 | // |
| 3783 | // lldb sends: qSymbol:: |
| 3784 | // lldb receives: qSymbol:64697370617463685f71756575655f6f666673657473 |
| 3785 | // lldb sends: qSymbol:2bc97554:64697370617463685f71756575655f6f666673657473 |
| 3786 | // lldb receives: OK |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3787 | // Remote gdb stub asks for address of 'dispatch_queue_offsets'. lldb says |
| 3788 | // that it |
| 3789 | // is at address 0x2bc97554. Remote gdb stub sends 'OK' indicating that it |
| 3790 | // does not |
Jason Molenda | 50018d3 | 2016-01-13 04:08:10 +0000 | [diff] [blame] | 3791 | // need any more symbols. lldb does not need to ask again in this session. |
Greg Clayton | 0b90be1 | 2015-06-23 21:27:50 +0000 | [diff] [blame] | 3792 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3793 | void GDBRemoteCommunicationClient::ServeSymbolLookups( |
| 3794 | lldb_private::Process *process) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3795 | // Set to true once we've resolved a symbol to an address for the remote |
| 3796 | // stub. If we get an 'OK' response after this, the remote stub doesn't need |
| 3797 | // any more symbols and we can stop asking. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3798 | bool symbol_response_provided = false; |
Jason Molenda | 50018d3 | 2016-01-13 04:08:10 +0000 | [diff] [blame] | 3799 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3800 | // Is this the initial qSymbol:: packet? |
| 3801 | bool first_qsymbol_query = true; |
Jason Molenda | 50018d3 | 2016-01-13 04:08:10 +0000 | [diff] [blame] | 3802 | |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 3803 | if (m_supports_qSymbol && !m_qSymbol_requests_done) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3804 | Lock lock(*this, false); |
| 3805 | if (lock) { |
| 3806 | StreamString packet; |
| 3807 | packet.PutCString("qSymbol::"); |
| 3808 | StringExtractorGDBRemote response; |
| 3809 | while (SendPacketAndWaitForResponseNoLock(packet.GetString(), response) == |
| 3810 | PacketResult::Success) { |
| 3811 | if (response.IsOKResponse()) { |
| 3812 | if (symbol_response_provided || first_qsymbol_query) { |
| 3813 | m_qSymbol_requests_done = true; |
| 3814 | } |
| 3815 | |
| 3816 | // We are done serving symbols requests |
| 3817 | return; |
| 3818 | } |
| 3819 | first_qsymbol_query = false; |
| 3820 | |
| 3821 | if (response.IsUnsupportedResponse()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3822 | // qSymbol is not supported by the current GDB server we are |
| 3823 | // connected to |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3824 | m_supports_qSymbol = false; |
| 3825 | return; |
| 3826 | } else { |
| 3827 | llvm::StringRef response_str(response.GetStringRef()); |
| 3828 | if (response_str.startswith("qSymbol:")) { |
| 3829 | response.SetFilePos(strlen("qSymbol:")); |
| 3830 | std::string symbol_name; |
| 3831 | if (response.GetHexByteString(symbol_name)) { |
| 3832 | if (symbol_name.empty()) |
| 3833 | return; |
| 3834 | |
| 3835 | addr_t symbol_load_addr = LLDB_INVALID_ADDRESS; |
| 3836 | lldb_private::SymbolContextList sc_list; |
| 3837 | if (process->GetTarget().GetImages().FindSymbolsWithNameAndType( |
| 3838 | ConstString(symbol_name), eSymbolTypeAny, sc_list)) { |
| 3839 | const size_t num_scs = sc_list.GetSize(); |
| 3840 | for (size_t sc_idx = 0; |
| 3841 | sc_idx < num_scs && |
| 3842 | symbol_load_addr == LLDB_INVALID_ADDRESS; |
| 3843 | ++sc_idx) { |
| 3844 | SymbolContext sc; |
| 3845 | if (sc_list.GetContextAtIndex(sc_idx, sc)) { |
| 3846 | if (sc.symbol) { |
| 3847 | switch (sc.symbol->GetType()) { |
| 3848 | case eSymbolTypeInvalid: |
| 3849 | case eSymbolTypeAbsolute: |
| 3850 | case eSymbolTypeUndefined: |
| 3851 | case eSymbolTypeSourceFile: |
| 3852 | case eSymbolTypeHeaderFile: |
| 3853 | case eSymbolTypeObjectFile: |
| 3854 | case eSymbolTypeCommonBlock: |
| 3855 | case eSymbolTypeBlock: |
| 3856 | case eSymbolTypeLocal: |
| 3857 | case eSymbolTypeParam: |
| 3858 | case eSymbolTypeVariable: |
| 3859 | case eSymbolTypeVariableType: |
| 3860 | case eSymbolTypeLineEntry: |
| 3861 | case eSymbolTypeLineHeader: |
| 3862 | case eSymbolTypeScopeBegin: |
| 3863 | case eSymbolTypeScopeEnd: |
| 3864 | case eSymbolTypeAdditional: |
| 3865 | case eSymbolTypeCompiler: |
| 3866 | case eSymbolTypeInstrumentation: |
| 3867 | case eSymbolTypeTrampoline: |
| 3868 | break; |
| 3869 | |
| 3870 | case eSymbolTypeCode: |
| 3871 | case eSymbolTypeResolver: |
| 3872 | case eSymbolTypeData: |
| 3873 | case eSymbolTypeRuntime: |
| 3874 | case eSymbolTypeException: |
| 3875 | case eSymbolTypeObjCClass: |
| 3876 | case eSymbolTypeObjCMetaClass: |
| 3877 | case eSymbolTypeObjCIVar: |
| 3878 | case eSymbolTypeReExported: |
| 3879 | symbol_load_addr = |
| 3880 | sc.symbol->GetLoadAddress(&process->GetTarget()); |
| 3881 | break; |
| 3882 | } |
Jason Molenda | 50018d3 | 2016-01-13 04:08:10 +0000 | [diff] [blame] | 3883 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3884 | } |
Greg Clayton | 42b0148 | 2015-08-11 22:07:46 +0000 | [diff] [blame] | 3885 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3886 | } |
| 3887 | // This is the normal path where our symbol lookup was successful |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3888 | // and we want to send a packet with the new symbol value and see |
| 3889 | // if another lookup needs to be done. |
Greg Clayton | 0b90be1 | 2015-06-23 21:27:50 +0000 | [diff] [blame] | 3890 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3891 | // Change "packet" to contain the requested symbol value and name |
| 3892 | packet.Clear(); |
| 3893 | packet.PutCString("qSymbol:"); |
| 3894 | if (symbol_load_addr != LLDB_INVALID_ADDRESS) { |
| 3895 | packet.Printf("%" PRIx64, symbol_load_addr); |
| 3896 | symbol_response_provided = true; |
| 3897 | } else { |
| 3898 | symbol_response_provided = false; |
| 3899 | } |
| 3900 | packet.PutCString(":"); |
| 3901 | packet.PutBytesAsRawHex8(symbol_name.data(), symbol_name.size()); |
| 3902 | continue; // go back to the while loop and send "packet" and wait |
| 3903 | // for another response |
Greg Clayton | 0b90be1 | 2015-06-23 21:27:50 +0000 | [diff] [blame] | 3904 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3905 | } |
| 3906 | } |
| 3907 | } |
| 3908 | // If we make it here, the symbol request packet response wasn't valid or |
| 3909 | // our symbol lookup failed so we must abort |
| 3910 | return; |
Greg Clayton | 0b90be1 | 2015-06-23 21:27:50 +0000 | [diff] [blame] | 3911 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3912 | } else if (Log *log = ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet( |
| 3913 | GDBR_LOG_PROCESS | GDBR_LOG_PACKETS)) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3914 | LLDB_LOGF(log, |
| 3915 | "GDBRemoteCommunicationClient::%s: Didn't get sequence mutex.", |
| 3916 | __FUNCTION__); |
Greg Clayton | 0b90be1 | 2015-06-23 21:27:50 +0000 | [diff] [blame] | 3917 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3918 | } |
Greg Clayton | 0b90be1 | 2015-06-23 21:27:50 +0000 | [diff] [blame] | 3919 | } |
| 3920 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3921 | StructuredData::Array * |
| 3922 | GDBRemoteCommunicationClient::GetSupportedStructuredDataPlugins() { |
| 3923 | if (!m_supported_async_json_packets_is_valid) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3924 | // Query the server for the array of supported asynchronous JSON packets. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3925 | m_supported_async_json_packets_is_valid = true; |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 3926 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3927 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 3928 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3929 | // Poll it now. |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 3930 | StringExtractorGDBRemote response; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3931 | const bool send_async = false; |
| 3932 | if (SendPacketAndWaitForResponse("qStructuredDataPlugins", response, |
| 3933 | send_async) == PacketResult::Success) { |
| 3934 | m_supported_async_json_packets_sp = |
| 3935 | StructuredData::ParseJSON(response.GetStringRef()); |
| 3936 | if (m_supported_async_json_packets_sp && |
| 3937 | !m_supported_async_json_packets_sp->GetAsArray()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3938 | // We were returned something other than a JSON array. This is |
| 3939 | // invalid. Clear it out. |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3940 | LLDB_LOGF(log, |
| 3941 | "GDBRemoteCommunicationClient::%s(): " |
| 3942 | "QSupportedAsyncJSONPackets returned invalid " |
| 3943 | "result: %s", |
Jonas Devlieghere | d35b42f | 2019-08-21 04:55:56 +0000 | [diff] [blame] | 3944 | __FUNCTION__, response.GetStringRef().data()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3945 | m_supported_async_json_packets_sp.reset(); |
| 3946 | } |
| 3947 | } else { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3948 | LLDB_LOGF(log, |
| 3949 | "GDBRemoteCommunicationClient::%s(): " |
| 3950 | "QSupportedAsyncJSONPackets unsupported", |
| 3951 | __FUNCTION__); |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 3952 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3953 | |
| 3954 | if (log && m_supported_async_json_packets_sp) { |
| 3955 | StreamString stream; |
| 3956 | m_supported_async_json_packets_sp->Dump(stream); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3957 | LLDB_LOGF(log, |
| 3958 | "GDBRemoteCommunicationClient::%s(): supported async " |
| 3959 | "JSON packets: %s", |
| 3960 | __FUNCTION__, stream.GetData()); |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 3961 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3962 | } |
| 3963 | |
| 3964 | return m_supported_async_json_packets_sp |
| 3965 | ? m_supported_async_json_packets_sp->GetAsArray() |
| 3966 | : nullptr; |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 3967 | } |
| 3968 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3969 | Status GDBRemoteCommunicationClient::SendSignalsToIgnore( |
Eugene Zemtsov | 7993cc5 | 2017-03-07 21:34:40 +0000 | [diff] [blame] | 3970 | llvm::ArrayRef<int32_t> signals) { |
| 3971 | // Format packet: |
| 3972 | // QPassSignals:<hex_sig1>;<hex_sig2>...;<hex_sigN> |
| 3973 | auto range = llvm::make_range(signals.begin(), signals.end()); |
| 3974 | std::string packet = formatv("QPassSignals:{0:$[;]@(x-2)}", range).str(); |
| 3975 | |
| 3976 | StringExtractorGDBRemote response; |
| 3977 | auto send_status = SendPacketAndWaitForResponse(packet, response, false); |
| 3978 | |
| 3979 | if (send_status != GDBRemoteCommunication::PacketResult::Success) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3980 | return Status("Sending QPassSignals packet failed"); |
Eugene Zemtsov | 7993cc5 | 2017-03-07 21:34:40 +0000 | [diff] [blame] | 3981 | |
| 3982 | if (response.IsOKResponse()) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3983 | return Status(); |
Eugene Zemtsov | 7993cc5 | 2017-03-07 21:34:40 +0000 | [diff] [blame] | 3984 | } else { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3985 | return Status("Unknown error happened during sending QPassSignals packet."); |
Eugene Zemtsov | 7993cc5 | 2017-03-07 21:34:40 +0000 | [diff] [blame] | 3986 | } |
| 3987 | } |
| 3988 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3989 | Status GDBRemoteCommunicationClient::ConfigureRemoteStructuredData( |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 3990 | ConstString type_name, const StructuredData::ObjectSP &config_sp) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3991 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3992 | |
| 3993 | if (type_name.GetLength() == 0) { |
| 3994 | error.SetErrorString("invalid type_name argument"); |
| 3995 | return error; |
| 3996 | } |
| 3997 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3998 | // Build command: Configure{type_name}: serialized config data. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3999 | StreamGDBRemote stream; |
| 4000 | stream.PutCString("QConfigure"); |
| 4001 | stream.PutCString(type_name.AsCString()); |
| 4002 | stream.PutChar(':'); |
| 4003 | if (config_sp) { |
| 4004 | // Gather the plain-text version of the configuration data. |
| 4005 | StreamString unescaped_stream; |
| 4006 | config_sp->Dump(unescaped_stream); |
| 4007 | unescaped_stream.Flush(); |
| 4008 | |
| 4009 | // Add it to the stream in escaped fashion. |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 4010 | stream.PutEscapedBytes(unescaped_stream.GetString().data(), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4011 | unescaped_stream.GetSize()); |
| 4012 | } |
| 4013 | stream.Flush(); |
| 4014 | |
| 4015 | // Send the packet. |
| 4016 | const bool send_async = false; |
| 4017 | StringExtractorGDBRemote response; |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 4018 | auto result = |
| 4019 | SendPacketAndWaitForResponse(stream.GetString(), response, send_async); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4020 | if (result == PacketResult::Success) { |
| 4021 | // We failed if the config result comes back other than OK. |
Jonas Devlieghere | d35b42f | 2019-08-21 04:55:56 +0000 | [diff] [blame] | 4022 | if (strcmp(response.GetStringRef().data(), "OK") == 0) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4023 | // Okay! |
| 4024 | error.Clear(); |
| 4025 | } else { |
| 4026 | error.SetErrorStringWithFormat("configuring StructuredData feature " |
| 4027 | "%s failed with error %s", |
| 4028 | type_name.AsCString(), |
Jonas Devlieghere | d35b42f | 2019-08-21 04:55:56 +0000 | [diff] [blame] | 4029 | response.GetStringRef().data()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4030 | } |
| 4031 | } else { |
| 4032 | // Can we get more data here on the failure? |
| 4033 | error.SetErrorStringWithFormat("configuring StructuredData feature %s " |
| 4034 | "failed when sending packet: " |
| 4035 | "PacketResult=%d", |
Ilia K | 4f730dc | 2016-09-12 05:25:33 +0000 | [diff] [blame] | 4036 | type_name.AsCString(), (int)result); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4037 | } |
| 4038 | return error; |
| 4039 | } |
| 4040 | |
| 4041 | void GDBRemoteCommunicationClient::OnRunPacketSent(bool first) { |
| 4042 | GDBRemoteClientBase::OnRunPacketSent(first); |
| 4043 | m_curr_tid = LLDB_INVALID_THREAD_ID; |
Pavel Labath | 8c1b6bd | 2016-08-09 12:04:46 +0000 | [diff] [blame] | 4044 | } |