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