Raphael Isemann | 8081428 | 2020-01-24 08:23:27 +0100 | [diff] [blame] | 1 | //===-- GDBRemoteCommunicationServerCommon.cpp ----------------------------===// |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 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 |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "GDBRemoteCommunicationServerCommon.h" |
| 10 | |
| 11 | #include <errno.h> |
| 12 | |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 13 | #ifdef __APPLE__ |
| 14 | #include <TargetConditionals.h> |
| 15 | #endif |
| 16 | |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 17 | #include <chrono> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 18 | #include <cstring> |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 19 | |
Oleksiy Vyalov | 6801be3 | 2015-02-25 22:15:44 +0000 | [diff] [blame] | 20 | #include "lldb/Core/ModuleSpec.h" |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 21 | #include "lldb/Host/Config.h" |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 22 | #include "lldb/Host/File.h" |
Pavel Labath | eef758e | 2019-02-04 14:28:08 +0000 | [diff] [blame] | 23 | #include "lldb/Host/FileAction.h" |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 24 | #include "lldb/Host/FileSystem.h" |
| 25 | #include "lldb/Host/Host.h" |
| 26 | #include "lldb/Host/HostInfo.h" |
Pavel Labath | 7704473 | 2018-09-12 12:26:05 +0000 | [diff] [blame] | 27 | #include "lldb/Host/SafeMachO.h" |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 28 | #include "lldb/Interpreter/OptionArgParser.h" |
Oleksiy Vyalov | 6801be3 | 2015-02-25 22:15:44 +0000 | [diff] [blame] | 29 | #include "lldb/Symbol/ObjectFile.h" |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 30 | #include "lldb/Target/Platform.h" |
Zachary Turner | 01c3243 | 2017-02-14 19:06:07 +0000 | [diff] [blame] | 31 | #include "lldb/Utility/Endian.h" |
Jonas Devlieghere | ff5225b | 2019-09-13 23:14:10 +0000 | [diff] [blame] | 32 | #include "lldb/Utility/GDBRemote.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 33 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 34 | #include "lldb/Utility/StreamString.h" |
Saleem Abdulrasool | bd689b9 | 2019-05-02 18:15:03 +0000 | [diff] [blame] | 35 | #include "lldb/Utility/StructuredData.h" |
Jonas Devlieghere | 4f8151e | 2019-10-02 18:02:32 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/StringSwitch.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/Triple.h" |
Jonas Devlieghere | 4f8151e | 2019-10-02 18:02:32 +0000 | [diff] [blame] | 38 | #include "llvm/Support/JSON.h" |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 39 | |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 40 | #include "ProcessGDBRemoteLog.h" |
Pavel Labath | 9af71b3 | 2018-03-20 16:14:00 +0000 | [diff] [blame] | 41 | #include "lldb/Utility/StringExtractorGDBRemote.h" |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 42 | |
Tamas Berghammer | dad4db7 | 2015-03-13 11:16:08 +0000 | [diff] [blame] | 43 | #ifdef __ANDROID__ |
| 44 | #include "lldb/Host/android/HostInfoAndroid.h" |
| 45 | #endif |
| 46 | |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 47 | |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 48 | using namespace lldb; |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 49 | using namespace lldb_private::process_gdb_remote; |
Jonas Devlieghere | 4f8151e | 2019-10-02 18:02:32 +0000 | [diff] [blame] | 50 | using namespace lldb_private; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 51 | |
Tamas Berghammer | 2d52afd | 2015-02-26 11:37:21 +0000 | [diff] [blame] | 52 | #ifdef __ANDROID__ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | const static uint32_t g_default_packet_timeout_sec = 20; // seconds |
Tamas Berghammer | 2d52afd | 2015-02-26 11:37:21 +0000 | [diff] [blame] | 54 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 55 | const static uint32_t g_default_packet_timeout_sec = 0; // not specified |
Tamas Berghammer | 2d52afd | 2015-02-26 11:37:21 +0000 | [diff] [blame] | 56 | #endif |
| 57 | |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 58 | // GDBRemoteCommunicationServerCommon constructor |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 59 | GDBRemoteCommunicationServerCommon::GDBRemoteCommunicationServerCommon( |
| 60 | const char *comm_name, const char *listener_name) |
| 61 | : GDBRemoteCommunicationServer(comm_name, listener_name), |
| 62 | m_process_launch_info(), m_process_launch_error(), m_proc_infos(), |
| 63 | m_proc_infos_index(0), m_thread_suffix_supported(false), |
| 64 | m_list_threads_in_stop_reply(false) { |
| 65 | RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_A, |
| 66 | &GDBRemoteCommunicationServerCommon::Handle_A); |
| 67 | RegisterMemberFunctionHandler( |
| 68 | StringExtractorGDBRemote::eServerPacketType_QEnvironment, |
| 69 | &GDBRemoteCommunicationServerCommon::Handle_QEnvironment); |
| 70 | RegisterMemberFunctionHandler( |
| 71 | StringExtractorGDBRemote::eServerPacketType_QEnvironmentHexEncoded, |
| 72 | &GDBRemoteCommunicationServerCommon::Handle_QEnvironmentHexEncoded); |
| 73 | RegisterMemberFunctionHandler( |
| 74 | StringExtractorGDBRemote::eServerPacketType_qfProcessInfo, |
| 75 | &GDBRemoteCommunicationServerCommon::Handle_qfProcessInfo); |
| 76 | RegisterMemberFunctionHandler( |
| 77 | StringExtractorGDBRemote::eServerPacketType_qGroupName, |
| 78 | &GDBRemoteCommunicationServerCommon::Handle_qGroupName); |
| 79 | RegisterMemberFunctionHandler( |
| 80 | StringExtractorGDBRemote::eServerPacketType_qHostInfo, |
| 81 | &GDBRemoteCommunicationServerCommon::Handle_qHostInfo); |
| 82 | RegisterMemberFunctionHandler( |
| 83 | StringExtractorGDBRemote::eServerPacketType_QLaunchArch, |
| 84 | &GDBRemoteCommunicationServerCommon::Handle_QLaunchArch); |
| 85 | RegisterMemberFunctionHandler( |
| 86 | StringExtractorGDBRemote::eServerPacketType_qLaunchSuccess, |
| 87 | &GDBRemoteCommunicationServerCommon::Handle_qLaunchSuccess); |
| 88 | RegisterMemberFunctionHandler( |
| 89 | StringExtractorGDBRemote::eServerPacketType_QListThreadsInStopReply, |
| 90 | &GDBRemoteCommunicationServerCommon::Handle_QListThreadsInStopReply); |
| 91 | RegisterMemberFunctionHandler( |
| 92 | StringExtractorGDBRemote::eServerPacketType_qEcho, |
| 93 | &GDBRemoteCommunicationServerCommon::Handle_qEcho); |
| 94 | RegisterMemberFunctionHandler( |
| 95 | StringExtractorGDBRemote::eServerPacketType_qModuleInfo, |
| 96 | &GDBRemoteCommunicationServerCommon::Handle_qModuleInfo); |
| 97 | RegisterMemberFunctionHandler( |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 98 | StringExtractorGDBRemote::eServerPacketType_jModulesInfo, |
| 99 | &GDBRemoteCommunicationServerCommon::Handle_jModulesInfo); |
| 100 | RegisterMemberFunctionHandler( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | StringExtractorGDBRemote::eServerPacketType_qPlatform_chmod, |
| 102 | &GDBRemoteCommunicationServerCommon::Handle_qPlatform_chmod); |
| 103 | RegisterMemberFunctionHandler( |
| 104 | StringExtractorGDBRemote::eServerPacketType_qPlatform_mkdir, |
| 105 | &GDBRemoteCommunicationServerCommon::Handle_qPlatform_mkdir); |
| 106 | RegisterMemberFunctionHandler( |
| 107 | StringExtractorGDBRemote::eServerPacketType_qPlatform_shell, |
| 108 | &GDBRemoteCommunicationServerCommon::Handle_qPlatform_shell); |
| 109 | RegisterMemberFunctionHandler( |
| 110 | StringExtractorGDBRemote::eServerPacketType_qProcessInfoPID, |
| 111 | &GDBRemoteCommunicationServerCommon::Handle_qProcessInfoPID); |
| 112 | RegisterMemberFunctionHandler( |
| 113 | StringExtractorGDBRemote::eServerPacketType_QSetDetachOnError, |
| 114 | &GDBRemoteCommunicationServerCommon::Handle_QSetDetachOnError); |
| 115 | RegisterMemberFunctionHandler( |
| 116 | StringExtractorGDBRemote::eServerPacketType_QSetSTDERR, |
| 117 | &GDBRemoteCommunicationServerCommon::Handle_QSetSTDERR); |
| 118 | RegisterMemberFunctionHandler( |
| 119 | StringExtractorGDBRemote::eServerPacketType_QSetSTDIN, |
| 120 | &GDBRemoteCommunicationServerCommon::Handle_QSetSTDIN); |
| 121 | RegisterMemberFunctionHandler( |
| 122 | StringExtractorGDBRemote::eServerPacketType_QSetSTDOUT, |
| 123 | &GDBRemoteCommunicationServerCommon::Handle_QSetSTDOUT); |
| 124 | RegisterMemberFunctionHandler( |
| 125 | StringExtractorGDBRemote::eServerPacketType_qSpeedTest, |
| 126 | &GDBRemoteCommunicationServerCommon::Handle_qSpeedTest); |
| 127 | RegisterMemberFunctionHandler( |
| 128 | StringExtractorGDBRemote::eServerPacketType_qsProcessInfo, |
| 129 | &GDBRemoteCommunicationServerCommon::Handle_qsProcessInfo); |
| 130 | RegisterMemberFunctionHandler( |
| 131 | StringExtractorGDBRemote::eServerPacketType_QStartNoAckMode, |
| 132 | &GDBRemoteCommunicationServerCommon::Handle_QStartNoAckMode); |
| 133 | RegisterMemberFunctionHandler( |
| 134 | StringExtractorGDBRemote::eServerPacketType_qSupported, |
| 135 | &GDBRemoteCommunicationServerCommon::Handle_qSupported); |
| 136 | RegisterMemberFunctionHandler( |
| 137 | StringExtractorGDBRemote::eServerPacketType_QThreadSuffixSupported, |
| 138 | &GDBRemoteCommunicationServerCommon::Handle_QThreadSuffixSupported); |
| 139 | RegisterMemberFunctionHandler( |
| 140 | StringExtractorGDBRemote::eServerPacketType_qUserName, |
| 141 | &GDBRemoteCommunicationServerCommon::Handle_qUserName); |
| 142 | RegisterMemberFunctionHandler( |
| 143 | StringExtractorGDBRemote::eServerPacketType_vFile_close, |
| 144 | &GDBRemoteCommunicationServerCommon::Handle_vFile_Close); |
| 145 | RegisterMemberFunctionHandler( |
| 146 | StringExtractorGDBRemote::eServerPacketType_vFile_exists, |
| 147 | &GDBRemoteCommunicationServerCommon::Handle_vFile_Exists); |
| 148 | RegisterMemberFunctionHandler( |
| 149 | StringExtractorGDBRemote::eServerPacketType_vFile_md5, |
| 150 | &GDBRemoteCommunicationServerCommon::Handle_vFile_MD5); |
| 151 | RegisterMemberFunctionHandler( |
| 152 | StringExtractorGDBRemote::eServerPacketType_vFile_mode, |
| 153 | &GDBRemoteCommunicationServerCommon::Handle_vFile_Mode); |
| 154 | RegisterMemberFunctionHandler( |
| 155 | StringExtractorGDBRemote::eServerPacketType_vFile_open, |
| 156 | &GDBRemoteCommunicationServerCommon::Handle_vFile_Open); |
| 157 | RegisterMemberFunctionHandler( |
| 158 | StringExtractorGDBRemote::eServerPacketType_vFile_pread, |
| 159 | &GDBRemoteCommunicationServerCommon::Handle_vFile_pRead); |
| 160 | RegisterMemberFunctionHandler( |
| 161 | StringExtractorGDBRemote::eServerPacketType_vFile_pwrite, |
| 162 | &GDBRemoteCommunicationServerCommon::Handle_vFile_pWrite); |
| 163 | RegisterMemberFunctionHandler( |
| 164 | StringExtractorGDBRemote::eServerPacketType_vFile_size, |
| 165 | &GDBRemoteCommunicationServerCommon::Handle_vFile_Size); |
| 166 | RegisterMemberFunctionHandler( |
| 167 | StringExtractorGDBRemote::eServerPacketType_vFile_stat, |
| 168 | &GDBRemoteCommunicationServerCommon::Handle_vFile_Stat); |
| 169 | RegisterMemberFunctionHandler( |
| 170 | StringExtractorGDBRemote::eServerPacketType_vFile_symlink, |
| 171 | &GDBRemoteCommunicationServerCommon::Handle_vFile_symlink); |
| 172 | RegisterMemberFunctionHandler( |
| 173 | StringExtractorGDBRemote::eServerPacketType_vFile_unlink, |
| 174 | &GDBRemoteCommunicationServerCommon::Handle_vFile_unlink); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 177 | // Destructor |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 178 | GDBRemoteCommunicationServerCommon::~GDBRemoteCommunicationServerCommon() {} |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 179 | |
| 180 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 181 | GDBRemoteCommunicationServerCommon::Handle_qHostInfo( |
| 182 | StringExtractorGDBRemote &packet) { |
| 183 | StreamString response; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 184 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 185 | // $cputype:16777223;cpusubtype:3;ostype:Darwin;vendor:apple;endian:little;ptrsize:8;#00 |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 186 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 187 | ArchSpec host_arch(HostInfo::GetArchitecture()); |
| 188 | const llvm::Triple &host_triple = host_arch.GetTriple(); |
| 189 | response.PutCString("triple:"); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 190 | response.PutStringAsRawHex8(host_triple.getTriple()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 191 | response.Printf(";ptrsize:%u;", host_arch.GetAddressByteSize()); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 192 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 193 | const char *distribution_id = host_arch.GetDistributionId().AsCString(); |
| 194 | if (distribution_id) { |
| 195 | response.PutCString("distribution_id:"); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 196 | response.PutStringAsRawHex8(distribution_id); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 197 | response.PutCString(";"); |
| 198 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 199 | |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 200 | #if defined(__APPLE__) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 201 | // For parity with debugserver, we'll include the vendor key. |
| 202 | response.PutCString("vendor:apple;"); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 203 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 204 | // Send out MachO info. |
| 205 | uint32_t cpu = host_arch.GetMachOCPUType(); |
| 206 | uint32_t sub = host_arch.GetMachOCPUSubType(); |
| 207 | if (cpu != LLDB_INVALID_CPUTYPE) |
| 208 | response.Printf("cputype:%u;", cpu); |
| 209 | if (sub != LLDB_INVALID_CPUTYPE) |
| 210 | response.Printf("cpusubtype:%u;", sub); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 211 | |
Aaron Smith | 3a14249 | 2019-02-07 18:46:25 +0000 | [diff] [blame] | 212 | if (cpu == llvm::MachO::CPU_TYPE_ARM || cpu == llvm::MachO::CPU_TYPE_ARM64) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 213 | // Indicate the OS type. |
| 214 | #if defined(TARGET_OS_TV) && TARGET_OS_TV == 1 |
| 215 | response.PutCString("ostype:tvos;"); |
| 216 | #elif defined(TARGET_OS_WATCH) && TARGET_OS_WATCH == 1 |
| 217 | response.PutCString("ostype:watchos;"); |
Jason Molenda | 8c0d106 | 2018-02-05 23:10:51 +0000 | [diff] [blame] | 218 | #elif defined(TARGET_OS_BRIDGE) && TARGET_OS_BRIDGE == 1 |
| 219 | response.PutCString("ostype:bridgeos;"); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 220 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 221 | response.PutCString("ostype:ios;"); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 222 | #endif |
| 223 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 224 | // On arm, we use "synchronous" watchpoints which means the exception is |
| 225 | // delivered before the instruction executes. |
| 226 | response.PutCString("watchpoint_exceptions_received:before;"); |
| 227 | } else { |
| 228 | response.PutCString("ostype:macosx;"); |
| 229 | response.Printf("watchpoint_exceptions_received:after;"); |
| 230 | } |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 231 | |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 232 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 233 | if (host_arch.GetMachine() == llvm::Triple::aarch64 || |
Jason Molenda | 7dd7a36 | 2019-10-16 19:14:49 +0000 | [diff] [blame] | 234 | host_arch.GetMachine() == llvm::Triple::aarch64_32 || |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 235 | host_arch.GetMachine() == llvm::Triple::aarch64_be || |
| 236 | host_arch.GetMachine() == llvm::Triple::arm || |
Fangrui Song | ddb93b6 | 2019-05-16 08:37:32 +0000 | [diff] [blame] | 237 | host_arch.GetMachine() == llvm::Triple::armeb || host_arch.IsMIPS()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 238 | response.Printf("watchpoint_exceptions_received:before;"); |
| 239 | else |
| 240 | response.Printf("watchpoint_exceptions_received:after;"); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 241 | #endif |
| 242 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 243 | switch (endian::InlHostByteOrder()) { |
| 244 | case eByteOrderBig: |
| 245 | response.PutCString("endian:big;"); |
| 246 | break; |
| 247 | case eByteOrderLittle: |
| 248 | response.PutCString("endian:little;"); |
| 249 | break; |
| 250 | case eByteOrderPDP: |
| 251 | response.PutCString("endian:pdp;"); |
| 252 | break; |
| 253 | default: |
| 254 | response.PutCString("endian:unknown;"); |
| 255 | break; |
| 256 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 257 | |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 258 | llvm::VersionTuple version = HostInfo::GetOSVersion(); |
| 259 | if (!version.empty()) { |
| 260 | response.Format("os_version:{0}", version.getAsString()); |
| 261 | response.PutChar(';'); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 262 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 263 | |
Adrian Prantl | 2461061 | 2019-09-04 17:23:15 +0000 | [diff] [blame] | 264 | #if defined(__APPLE__) |
| 265 | llvm::VersionTuple maccatalyst_version = HostInfo::GetMacCatalystVersion(); |
| 266 | if (!maccatalyst_version.empty()) { |
| 267 | response.Format("maccatalyst_version:{0}", |
| 268 | maccatalyst_version.getAsString()); |
| 269 | response.PutChar(';'); |
| 270 | } |
| 271 | #endif |
| 272 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 273 | std::string s; |
| 274 | if (HostInfo::GetOSBuildString(s)) { |
| 275 | response.PutCString("os_build:"); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 276 | response.PutStringAsRawHex8(s); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 277 | response.PutChar(';'); |
| 278 | } |
| 279 | if (HostInfo::GetOSKernelDescription(s)) { |
| 280 | response.PutCString("os_kernel:"); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 281 | response.PutStringAsRawHex8(s); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 282 | response.PutChar(';'); |
| 283 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 284 | |
| 285 | #if defined(__APPLE__) |
| 286 | |
| 287 | #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 288 | // For iOS devices, we are connected through a USB Mux so we never pretend to |
| 289 | // actually have a hostname as far as the remote lldb that is connecting to |
| 290 | // this lldb-platform is concerned |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 291 | response.PutCString("hostname:"); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 292 | response.PutStringAsRawHex8("127.0.0.1"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 293 | response.PutChar(';'); |
| 294 | #else // #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) |
| 295 | if (HostInfo::GetHostname(s)) { |
| 296 | response.PutCString("hostname:"); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 297 | response.PutStringAsRawHex8(s); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 298 | response.PutChar(';'); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 299 | } |
| 300 | #endif // #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 301 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 302 | #else // #if defined(__APPLE__) |
| 303 | if (HostInfo::GetHostname(s)) { |
| 304 | response.PutCString("hostname:"); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 305 | response.PutStringAsRawHex8(s); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 306 | response.PutChar(';'); |
| 307 | } |
| 308 | #endif // #if defined(__APPLE__) |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 309 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 310 | if (g_default_packet_timeout_sec > 0) |
| 311 | response.Printf("default_packet_timeout:%u;", g_default_packet_timeout_sec); |
Tamas Berghammer | 2d52afd | 2015-02-26 11:37:21 +0000 | [diff] [blame] | 312 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 313 | return SendPacketNoLock(response.GetString()); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 317 | GDBRemoteCommunicationServerCommon::Handle_qProcessInfoPID( |
| 318 | StringExtractorGDBRemote &packet) { |
| 319 | // Packet format: "qProcessInfoPID:%i" where %i is the pid |
| 320 | packet.SetFilePos(::strlen("qProcessInfoPID:")); |
| 321 | lldb::pid_t pid = packet.GetU32(LLDB_INVALID_PROCESS_ID); |
| 322 | if (pid != LLDB_INVALID_PROCESS_ID) { |
| 323 | ProcessInstanceInfo proc_info; |
| 324 | if (Host::GetProcessInfo(pid, proc_info)) { |
| 325 | StreamString response; |
| 326 | CreateProcessInfoResponse(proc_info, response); |
| 327 | return SendPacketNoLock(response.GetString()); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 328 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 329 | } |
| 330 | return SendErrorResponse(1); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 334 | GDBRemoteCommunicationServerCommon::Handle_qfProcessInfo( |
| 335 | StringExtractorGDBRemote &packet) { |
| 336 | m_proc_infos_index = 0; |
| 337 | m_proc_infos.Clear(); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 338 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 339 | ProcessInstanceInfoMatch match_info; |
| 340 | packet.SetFilePos(::strlen("qfProcessInfo")); |
| 341 | if (packet.GetChar() == ':') { |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 342 | llvm::StringRef key; |
| 343 | llvm::StringRef value; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 344 | while (packet.GetNameColonValue(key, value)) { |
| 345 | bool success = true; |
| 346 | if (key.equals("name")) { |
| 347 | StringExtractor extractor(value); |
| 348 | std::string file; |
| 349 | extractor.GetHexByteString(file); |
Jonas Devlieghere | 937348c | 2018-06-13 22:08:14 +0000 | [diff] [blame] | 350 | match_info.GetProcessInfo().GetExecutableFile().SetFile( |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 351 | file, FileSpec::Style::native); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 352 | } else if (key.equals("name_match")) { |
Pavel Labath | c4a3395 | 2017-02-20 11:35:33 +0000 | [diff] [blame] | 353 | NameMatch name_match = llvm::StringSwitch<NameMatch>(value) |
| 354 | .Case("equals", NameMatch::Equals) |
| 355 | .Case("starts_with", NameMatch::StartsWith) |
| 356 | .Case("ends_with", NameMatch::EndsWith) |
| 357 | .Case("contains", NameMatch::Contains) |
| 358 | .Case("regex", NameMatch::RegularExpression) |
| 359 | .Default(NameMatch::Ignore); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 360 | match_info.SetNameMatchType(name_match); |
Pavel Labath | c4a3395 | 2017-02-20 11:35:33 +0000 | [diff] [blame] | 361 | if (name_match == NameMatch::Ignore) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 362 | return SendErrorResponse(2); |
| 363 | } else if (key.equals("pid")) { |
| 364 | lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; |
| 365 | if (value.getAsInteger(0, pid)) |
| 366 | return SendErrorResponse(2); |
| 367 | match_info.GetProcessInfo().SetProcessID(pid); |
| 368 | } else if (key.equals("parent_pid")) { |
| 369 | lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; |
| 370 | if (value.getAsInteger(0, pid)) |
| 371 | return SendErrorResponse(2); |
| 372 | match_info.GetProcessInfo().SetParentProcessID(pid); |
| 373 | } else if (key.equals("uid")) { |
| 374 | uint32_t uid = UINT32_MAX; |
| 375 | if (value.getAsInteger(0, uid)) |
| 376 | return SendErrorResponse(2); |
| 377 | match_info.GetProcessInfo().SetUserID(uid); |
| 378 | } else if (key.equals("gid")) { |
| 379 | uint32_t gid = UINT32_MAX; |
| 380 | if (value.getAsInteger(0, gid)) |
| 381 | return SendErrorResponse(2); |
| 382 | match_info.GetProcessInfo().SetGroupID(gid); |
| 383 | } else if (key.equals("euid")) { |
| 384 | uint32_t uid = UINT32_MAX; |
| 385 | if (value.getAsInteger(0, uid)) |
| 386 | return SendErrorResponse(2); |
| 387 | match_info.GetProcessInfo().SetEffectiveUserID(uid); |
| 388 | } else if (key.equals("egid")) { |
| 389 | uint32_t gid = UINT32_MAX; |
| 390 | if (value.getAsInteger(0, gid)) |
| 391 | return SendErrorResponse(2); |
| 392 | match_info.GetProcessInfo().SetEffectiveGroupID(gid); |
| 393 | } else if (key.equals("all_users")) { |
| 394 | match_info.SetMatchAllUsers( |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 395 | OptionArgParser::ToBoolean(value, false, &success)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 396 | } else if (key.equals("triple")) { |
Pavel Labath | 7263f1b | 2017-10-31 10:56:03 +0000 | [diff] [blame] | 397 | match_info.GetProcessInfo().GetArchitecture() = |
| 398 | HostInfo::GetAugmentedArchSpec(value); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 399 | } else { |
| 400 | success = false; |
| 401 | } |
| 402 | |
| 403 | if (!success) |
| 404 | return SendErrorResponse(2); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 405 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | if (Host::FindProcesses(match_info, m_proc_infos)) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 409 | // We found something, return the first item by calling the get subsequent |
| 410 | // process info packet handler... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 411 | return Handle_qsProcessInfo(packet); |
| 412 | } |
| 413 | return SendErrorResponse(3); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 417 | GDBRemoteCommunicationServerCommon::Handle_qsProcessInfo( |
| 418 | StringExtractorGDBRemote &packet) { |
| 419 | if (m_proc_infos_index < m_proc_infos.GetSize()) { |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 420 | StreamString response; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 421 | CreateProcessInfoResponse( |
| 422 | m_proc_infos.GetProcessInfoAtIndex(m_proc_infos_index), response); |
| 423 | ++m_proc_infos_index; |
Zachary Turner | 26709df | 2016-08-27 15:52:29 +0000 | [diff] [blame] | 424 | return SendPacketNoLock(response.GetString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 425 | } |
| 426 | return SendErrorResponse(4); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 430 | GDBRemoteCommunicationServerCommon::Handle_qUserName( |
| 431 | StringExtractorGDBRemote &packet) { |
Jonas Devlieghere | 3011d55 | 2019-12-12 10:00:45 -0800 | [diff] [blame] | 432 | #if LLDB_ENABLE_POSIX |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 433 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 434 | LLDB_LOGF(log, "GDBRemoteCommunicationServerCommon::%s begin", __FUNCTION__); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 435 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 436 | // Packet format: "qUserName:%i" where %i is the uid |
| 437 | packet.SetFilePos(::strlen("qUserName:")); |
| 438 | uint32_t uid = packet.GetU32(UINT32_MAX); |
| 439 | if (uid != UINT32_MAX) { |
Pavel Labath | aa51e6a | 2019-03-04 18:48:00 +0000 | [diff] [blame] | 440 | if (llvm::Optional<llvm::StringRef> name = |
| 441 | HostInfo::GetUserIDResolver().GetUserName(uid)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 442 | StreamString response; |
Pavel Labath | aa51e6a | 2019-03-04 18:48:00 +0000 | [diff] [blame] | 443 | response.PutStringAsRawHex8(*name); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 444 | return SendPacketNoLock(response.GetString()); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 445 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 446 | } |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 447 | LLDB_LOGF(log, "GDBRemoteCommunicationServerCommon::%s end", __FUNCTION__); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 448 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 449 | return SendErrorResponse(5); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 453 | GDBRemoteCommunicationServerCommon::Handle_qGroupName( |
| 454 | StringExtractorGDBRemote &packet) { |
Jonas Devlieghere | 3011d55 | 2019-12-12 10:00:45 -0800 | [diff] [blame] | 455 | #if LLDB_ENABLE_POSIX |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 456 | // Packet format: "qGroupName:%i" where %i is the gid |
| 457 | packet.SetFilePos(::strlen("qGroupName:")); |
| 458 | uint32_t gid = packet.GetU32(UINT32_MAX); |
| 459 | if (gid != UINT32_MAX) { |
Pavel Labath | aa51e6a | 2019-03-04 18:48:00 +0000 | [diff] [blame] | 460 | if (llvm::Optional<llvm::StringRef> name = |
| 461 | HostInfo::GetUserIDResolver().GetGroupName(gid)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 462 | StreamString response; |
Pavel Labath | aa51e6a | 2019-03-04 18:48:00 +0000 | [diff] [blame] | 463 | response.PutStringAsRawHex8(*name); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 464 | return SendPacketNoLock(response.GetString()); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 465 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 466 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 467 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 468 | return SendErrorResponse(6); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 472 | GDBRemoteCommunicationServerCommon::Handle_qSpeedTest( |
| 473 | StringExtractorGDBRemote &packet) { |
| 474 | packet.SetFilePos(::strlen("qSpeedTest:")); |
| 475 | |
| 476 | llvm::StringRef key; |
| 477 | llvm::StringRef value; |
| 478 | bool success = packet.GetNameColonValue(key, value); |
| 479 | if (success && key.equals("response_size")) { |
| 480 | uint32_t response_size = 0; |
| 481 | if (!value.getAsInteger(0, response_size)) { |
| 482 | if (response_size == 0) |
| 483 | return SendOKResponse(); |
| 484 | StreamString response; |
| 485 | uint32_t bytes_left = response_size; |
| 486 | response.PutCString("data:"); |
| 487 | while (bytes_left > 0) { |
| 488 | if (bytes_left >= 26) { |
| 489 | response.PutCString("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); |
| 490 | bytes_left -= 26; |
| 491 | } else { |
| 492 | response.Printf("%*.*s;", bytes_left, bytes_left, |
| 493 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); |
| 494 | bytes_left = 0; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 495 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 496 | } |
| 497 | return SendPacketNoLock(response.GetString()); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 498 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 499 | } |
| 500 | return SendErrorResponse(7); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 504 | GDBRemoteCommunicationServerCommon::Handle_vFile_Open( |
| 505 | StringExtractorGDBRemote &packet) { |
| 506 | packet.SetFilePos(::strlen("vFile:open:")); |
| 507 | std::string path; |
| 508 | packet.GetHexByteStringTerminatedBy(path, ','); |
| 509 | if (!path.empty()) { |
| 510 | if (packet.GetChar() == ',') { |
Lawrence D'Anna | 62c9fe4 | 2019-10-14 20:15:34 +0000 | [diff] [blame] | 511 | // FIXME |
| 512 | // The flag values for OpenOptions do not match the values used by GDB |
| 513 | // * https://sourceware.org/gdb/onlinedocs/gdb/Open-Flags.html#Open-Flags |
| 514 | // * rdar://problem/46788934 |
| 515 | auto flags = File::OpenOptions(packet.GetHexMaxU32(false, 0)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 516 | if (packet.GetChar() == ',') { |
| 517 | mode_t mode = packet.GetHexMaxU32(false, 0600); |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 518 | FileSpec path_spec(path); |
| 519 | FileSystem::Instance().Resolve(path_spec); |
Aaron Smith | 3a14249 | 2019-02-07 18:46:25 +0000 | [diff] [blame] | 520 | // Do not close fd. |
Lawrence D'Anna | 2fce113 | 2019-09-26 17:54:59 +0000 | [diff] [blame] | 521 | auto file = FileSystem::Instance().Open(path_spec, flags, mode, false); |
| 522 | |
| 523 | int save_errno = 0; |
| 524 | int descriptor = File::kInvalidDescriptor; |
| 525 | if (file) { |
| 526 | descriptor = file.get()->GetDescriptor(); |
| 527 | } else { |
| 528 | std::error_code code = errorToErrorCode(file.takeError()); |
| 529 | if (code.category() == std::system_category()) { |
| 530 | save_errno = code.value(); |
| 531 | } |
| 532 | } |
| 533 | |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 534 | StreamString response; |
| 535 | response.PutChar('F'); |
Lawrence D'Anna | 2fce113 | 2019-09-26 17:54:59 +0000 | [diff] [blame] | 536 | response.Printf("%i", descriptor); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 537 | if (save_errno) |
| 538 | response.Printf(",%i", save_errno); |
Zachary Turner | 26709df | 2016-08-27 15:52:29 +0000 | [diff] [blame] | 539 | return SendPacketNoLock(response.GetString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 540 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 541 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 542 | } |
| 543 | return SendErrorResponse(18); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 547 | GDBRemoteCommunicationServerCommon::Handle_vFile_Close( |
| 548 | StringExtractorGDBRemote &packet) { |
| 549 | packet.SetFilePos(::strlen("vFile:close:")); |
| 550 | int fd = packet.GetS32(-1); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 551 | int err = -1; |
| 552 | int save_errno = 0; |
| 553 | if (fd >= 0) { |
Lawrence D'Anna | 62c9fe4 | 2019-10-14 20:15:34 +0000 | [diff] [blame] | 554 | NativeFile file(fd, File::OpenOptions(0), true); |
Aaron Smith | 3a14249 | 2019-02-07 18:46:25 +0000 | [diff] [blame] | 555 | Status error = file.Close(); |
| 556 | err = 0; |
| 557 | save_errno = error.GetError(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 558 | } else { |
| 559 | save_errno = EINVAL; |
| 560 | } |
| 561 | StreamString response; |
| 562 | response.PutChar('F'); |
| 563 | response.Printf("%i", err); |
| 564 | if (save_errno) |
| 565 | response.Printf(",%i", save_errno); |
| 566 | return SendPacketNoLock(response.GetString()); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 570 | GDBRemoteCommunicationServerCommon::Handle_vFile_pRead( |
| 571 | StringExtractorGDBRemote &packet) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 572 | StreamGDBRemote response; |
| 573 | packet.SetFilePos(::strlen("vFile:pread:")); |
| 574 | int fd = packet.GetS32(-1); |
| 575 | if (packet.GetChar() == ',') { |
Pavel Labath | 341df3f | 2019-08-26 13:56:33 +0000 | [diff] [blame] | 576 | size_t count = packet.GetU64(SIZE_MAX); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 577 | if (packet.GetChar() == ',') { |
Aaron Smith | 3a14249 | 2019-02-07 18:46:25 +0000 | [diff] [blame] | 578 | off_t offset = packet.GetU64(UINT32_MAX); |
Pavel Labath | 341df3f | 2019-08-26 13:56:33 +0000 | [diff] [blame] | 579 | if (count == SIZE_MAX) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 580 | response.Printf("F-1:%i", EINVAL); |
Zachary Turner | 26709df | 2016-08-27 15:52:29 +0000 | [diff] [blame] | 581 | return SendPacketNoLock(response.GetString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | std::string buffer(count, 0); |
Lawrence D'Anna | f913fd6 | 2019-10-03 04:31:46 +0000 | [diff] [blame] | 585 | NativeFile file(fd, File::eOpenOptionRead, false); |
Aaron Smith | 3a14249 | 2019-02-07 18:46:25 +0000 | [diff] [blame] | 586 | Status error = file.Read(static_cast<void *>(&buffer[0]), count, offset); |
| 587 | const ssize_t bytes_read = error.Success() ? count : -1; |
| 588 | const int save_errno = error.GetError(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 589 | response.PutChar('F'); |
| 590 | response.Printf("%zi", bytes_read); |
| 591 | if (save_errno) |
| 592 | response.Printf(",%i", save_errno); |
| 593 | else { |
| 594 | response.PutChar(';'); |
| 595 | response.PutEscapedBytes(&buffer[0], bytes_read); |
| 596 | } |
| 597 | return SendPacketNoLock(response.GetString()); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 598 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 599 | } |
| 600 | return SendErrorResponse(21); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 601 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 602 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 603 | GDBRemoteCommunication::PacketResult |
| 604 | GDBRemoteCommunicationServerCommon::Handle_vFile_pWrite( |
| 605 | StringExtractorGDBRemote &packet) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 606 | packet.SetFilePos(::strlen("vFile:pwrite:")); |
| 607 | |
| 608 | StreamGDBRemote response; |
| 609 | response.PutChar('F'); |
| 610 | |
| 611 | int fd = packet.GetU32(UINT32_MAX); |
| 612 | if (packet.GetChar() == ',') { |
| 613 | off_t offset = packet.GetU64(UINT32_MAX); |
| 614 | if (packet.GetChar() == ',') { |
| 615 | std::string buffer; |
| 616 | if (packet.GetEscapedBinaryData(buffer)) { |
Lawrence D'Anna | f913fd6 | 2019-10-03 04:31:46 +0000 | [diff] [blame] | 617 | NativeFile file(fd, File::eOpenOptionWrite, false); |
Aaron Smith | 3a14249 | 2019-02-07 18:46:25 +0000 | [diff] [blame] | 618 | size_t count = buffer.size(); |
| 619 | Status error = |
| 620 | file.Write(static_cast<const void *>(&buffer[0]), count, offset); |
| 621 | const ssize_t bytes_written = error.Success() ? count : -1; |
| 622 | const int save_errno = error.GetError(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 623 | response.Printf("%zi", bytes_written); |
| 624 | if (save_errno) |
| 625 | response.Printf(",%i", save_errno); |
| 626 | } else { |
| 627 | response.Printf("-1,%i", EINVAL); |
| 628 | } |
| 629 | return SendPacketNoLock(response.GetString()); |
| 630 | } |
| 631 | } |
| 632 | return SendErrorResponse(27); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | GDBRemoteCommunication::PacketResult |
| 636 | GDBRemoteCommunicationServerCommon::Handle_vFile_Size( |
| 637 | StringExtractorGDBRemote &packet) { |
| 638 | packet.SetFilePos(::strlen("vFile:size:")); |
| 639 | std::string path; |
| 640 | packet.GetHexByteString(path); |
| 641 | if (!path.empty()) { |
Zachary Turner | 07db3f7 | 2017-03-21 05:47:57 +0000 | [diff] [blame] | 642 | uint64_t Size; |
| 643 | if (llvm::sys::fs::file_size(path, Size)) |
| 644 | return SendErrorResponse(5); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 645 | StreamString response; |
| 646 | response.PutChar('F'); |
Zachary Turner | 07db3f7 | 2017-03-21 05:47:57 +0000 | [diff] [blame] | 647 | response.PutHex64(Size); |
| 648 | if (Size == UINT64_MAX) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 649 | response.PutChar(','); |
Zachary Turner | 07db3f7 | 2017-03-21 05:47:57 +0000 | [diff] [blame] | 650 | response.PutHex64(Size); // TODO: replace with Host::GetSyswideErrorCode() |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 651 | } |
Zachary Turner | 26709df | 2016-08-27 15:52:29 +0000 | [diff] [blame] | 652 | return SendPacketNoLock(response.GetString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 653 | } |
| 654 | return SendErrorResponse(22); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 658 | GDBRemoteCommunicationServerCommon::Handle_vFile_Mode( |
| 659 | StringExtractorGDBRemote &packet) { |
| 660 | packet.SetFilePos(::strlen("vFile:mode:")); |
| 661 | std::string path; |
| 662 | packet.GetHexByteString(path); |
| 663 | if (!path.empty()) { |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 664 | FileSpec file_spec(path); |
| 665 | FileSystem::Instance().Resolve(file_spec); |
Jonas Devlieghere | 73ed607 | 2018-11-01 22:46:49 +0000 | [diff] [blame] | 666 | std::error_code ec; |
| 667 | const uint32_t mode = FileSystem::Instance().GetPermissions(file_spec, ec); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 668 | StreamString response; |
| 669 | response.Printf("F%u", mode); |
Jonas Devlieghere | 73ed607 | 2018-11-01 22:46:49 +0000 | [diff] [blame] | 670 | if (mode == 0 || ec) |
| 671 | response.Printf(",%i", (int)Status(ec).GetError()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 672 | return SendPacketNoLock(response.GetString()); |
| 673 | } |
| 674 | return SendErrorResponse(23); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 678 | GDBRemoteCommunicationServerCommon::Handle_vFile_Exists( |
| 679 | StringExtractorGDBRemote &packet) { |
| 680 | packet.SetFilePos(::strlen("vFile:exists:")); |
| 681 | std::string path; |
| 682 | packet.GetHexByteString(path); |
| 683 | if (!path.empty()) { |
Zachary Turner | 07db3f7 | 2017-03-21 05:47:57 +0000 | [diff] [blame] | 684 | bool retcode = llvm::sys::fs::exists(path); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 685 | StreamString response; |
| 686 | response.PutChar('F'); |
| 687 | response.PutChar(','); |
| 688 | if (retcode) |
| 689 | response.PutChar('1'); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 690 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 691 | response.PutChar('0'); |
| 692 | return SendPacketNoLock(response.GetString()); |
| 693 | } |
| 694 | return SendErrorResponse(24); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 698 | GDBRemoteCommunicationServerCommon::Handle_vFile_symlink( |
| 699 | StringExtractorGDBRemote &packet) { |
| 700 | packet.SetFilePos(::strlen("vFile:symlink:")); |
| 701 | std::string dst, src; |
| 702 | packet.GetHexByteStringTerminatedBy(dst, ','); |
| 703 | packet.GetChar(); // Skip ',' char |
| 704 | packet.GetHexByteString(src); |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 705 | |
| 706 | FileSpec src_spec(src); |
| 707 | FileSystem::Instance().Resolve(src_spec); |
| 708 | Status error = FileSystem::Instance().Symlink(src_spec, FileSpec(dst)); |
| 709 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 710 | StreamString response; |
| 711 | response.Printf("F%u,%u", error.GetError(), error.GetError()); |
| 712 | return SendPacketNoLock(response.GetString()); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 716 | GDBRemoteCommunicationServerCommon::Handle_vFile_unlink( |
| 717 | StringExtractorGDBRemote &packet) { |
| 718 | packet.SetFilePos(::strlen("vFile:unlink:")); |
| 719 | std::string path; |
| 720 | packet.GetHexByteString(path); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 721 | Status error(llvm::sys::fs::remove(path)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 722 | StreamString response; |
| 723 | response.Printf("F%u,%u", error.GetError(), error.GetError()); |
| 724 | return SendPacketNoLock(response.GetString()); |
| 725 | } |
| 726 | |
| 727 | GDBRemoteCommunication::PacketResult |
| 728 | GDBRemoteCommunicationServerCommon::Handle_qPlatform_shell( |
| 729 | StringExtractorGDBRemote &packet) { |
| 730 | packet.SetFilePos(::strlen("qPlatform_shell:")); |
| 731 | std::string path; |
| 732 | std::string working_dir; |
| 733 | packet.GetHexByteStringTerminatedBy(path, ','); |
| 734 | if (!path.empty()) { |
| 735 | if (packet.GetChar() == ',') { |
| 736 | // FIXME: add timeout to qPlatform_shell packet |
| 737 | // uint32_t timeout = packet.GetHexMaxU32(false, 32); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 738 | if (packet.GetChar() == ',') |
| 739 | packet.GetHexByteString(working_dir); |
| 740 | int status, signo; |
| 741 | std::string output; |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 742 | FileSpec working_spec(working_dir); |
| 743 | FileSystem::Instance().Resolve(working_spec); |
| 744 | Status err = |
| 745 | Host::RunShellCommand(path.c_str(), working_spec, &status, &signo, |
| 746 | &output, std::chrono::seconds(10)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 747 | StreamGDBRemote response; |
| 748 | if (err.Fail()) { |
| 749 | response.PutCString("F,"); |
| 750 | response.PutHex32(UINT32_MAX); |
| 751 | } else { |
| 752 | response.PutCString("F,"); |
| 753 | response.PutHex32(status); |
| 754 | response.PutChar(','); |
| 755 | response.PutHex32(signo); |
| 756 | response.PutChar(','); |
| 757 | response.PutEscapedBytes(output.c_str(), output.size()); |
| 758 | } |
| 759 | return SendPacketNoLock(response.GetString()); |
| 760 | } |
| 761 | } |
| 762 | return SendErrorResponse(24); |
| 763 | } |
| 764 | |
| 765 | GDBRemoteCommunication::PacketResult |
| 766 | GDBRemoteCommunicationServerCommon::Handle_vFile_Stat( |
| 767 | StringExtractorGDBRemote &packet) { |
| 768 | return SendUnimplementedResponse( |
| 769 | "GDBRemoteCommunicationServerCommon::Handle_vFile_Stat() unimplemented"); |
| 770 | } |
| 771 | |
| 772 | GDBRemoteCommunication::PacketResult |
| 773 | GDBRemoteCommunicationServerCommon::Handle_vFile_MD5( |
| 774 | StringExtractorGDBRemote &packet) { |
| 775 | packet.SetFilePos(::strlen("vFile:MD5:")); |
| 776 | std::string path; |
| 777 | packet.GetHexByteString(path); |
| 778 | if (!path.empty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 779 | StreamGDBRemote response; |
Zachary Turner | 076a259 | 2017-03-20 23:54:54 +0000 | [diff] [blame] | 780 | auto Result = llvm::sys::fs::md5_contents(path); |
| 781 | if (!Result) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 782 | response.PutCString("F,"); |
| 783 | response.PutCString("x"); |
| 784 | } else { |
| 785 | response.PutCString("F,"); |
Zachary Turner | 076a259 | 2017-03-20 23:54:54 +0000 | [diff] [blame] | 786 | response.PutHex64(Result->low()); |
| 787 | response.PutHex64(Result->high()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 788 | } |
| 789 | return SendPacketNoLock(response.GetString()); |
| 790 | } |
| 791 | return SendErrorResponse(25); |
| 792 | } |
| 793 | |
| 794 | GDBRemoteCommunication::PacketResult |
| 795 | GDBRemoteCommunicationServerCommon::Handle_qPlatform_mkdir( |
| 796 | StringExtractorGDBRemote &packet) { |
| 797 | packet.SetFilePos(::strlen("qPlatform_mkdir:")); |
| 798 | mode_t mode = packet.GetHexMaxU32(false, UINT32_MAX); |
| 799 | if (packet.GetChar() == ',') { |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 800 | std::string path; |
| 801 | packet.GetHexByteString(path); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 802 | Status error(llvm::sys::fs::create_directory(path, mode)); |
Oleksiy Vyalov | 6801be3 | 2015-02-25 22:15:44 +0000 | [diff] [blame] | 803 | |
| 804 | StreamGDBRemote response; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 805 | response.Printf("F%u", error.GetError()); |
Oleksiy Vyalov | 6801be3 | 2015-02-25 22:15:44 +0000 | [diff] [blame] | 806 | |
Zachary Turner | 26709df | 2016-08-27 15:52:29 +0000 | [diff] [blame] | 807 | return SendPacketNoLock(response.GetString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 808 | } |
| 809 | return SendErrorResponse(20); |
Oleksiy Vyalov | 6801be3 | 2015-02-25 22:15:44 +0000 | [diff] [blame] | 810 | } |
| 811 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 812 | GDBRemoteCommunication::PacketResult |
| 813 | GDBRemoteCommunicationServerCommon::Handle_qPlatform_chmod( |
| 814 | StringExtractorGDBRemote &packet) { |
| 815 | packet.SetFilePos(::strlen("qPlatform_chmod:")); |
| 816 | |
Zachary Turner | 6934e0a | 2017-03-19 05:49:43 +0000 | [diff] [blame] | 817 | auto perms = |
| 818 | static_cast<llvm::sys::fs::perms>(packet.GetHexMaxU32(false, UINT32_MAX)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 819 | if (packet.GetChar() == ',') { |
| 820 | std::string path; |
| 821 | packet.GetHexByteString(path); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 822 | Status error(llvm::sys::fs::setPermissions(path, perms)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 823 | |
| 824 | StreamGDBRemote response; |
| 825 | response.Printf("F%u", error.GetError()); |
| 826 | |
| 827 | return SendPacketNoLock(response.GetString()); |
| 828 | } |
| 829 | return SendErrorResponse(19); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 832 | GDBRemoteCommunication::PacketResult |
| 833 | GDBRemoteCommunicationServerCommon::Handle_qSupported( |
| 834 | StringExtractorGDBRemote &packet) { |
| 835 | StreamGDBRemote response; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 836 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 837 | // Features common to lldb-platform and llgs. |
| 838 | uint32_t max_packet_size = 128 * 1024; // 128KBytes is a reasonable max packet |
| 839 | // size--debugger can always use less |
| 840 | response.Printf("PacketSize=%x", max_packet_size); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 841 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 842 | response.PutCString(";QStartNoAckMode+"); |
| 843 | response.PutCString(";QThreadSuffixSupported+"); |
| 844 | response.PutCString(";QListThreadsInStopReply+"); |
| 845 | response.PutCString(";qEcho+"); |
Kamil Rytarowski | c93408a | 2017-03-21 17:27:59 +0000 | [diff] [blame] | 846 | #if defined(__linux__) || defined(__NetBSD__) |
Pavel Labath | 4a705e7 | 2017-02-24 09:29:14 +0000 | [diff] [blame] | 847 | response.PutCString(";QPassSignals+"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 848 | response.PutCString(";qXfer:auxv:read+"); |
Antonio Afonso | 05e32ba | 2019-07-23 20:40:30 +0000 | [diff] [blame] | 849 | response.PutCString(";qXfer:libraries-svr4:read+"); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 850 | #endif |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 851 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 852 | return SendPacketNoLock(response.GetString()); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 853 | } |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 854 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 855 | GDBRemoteCommunication::PacketResult |
| 856 | GDBRemoteCommunicationServerCommon::Handle_QThreadSuffixSupported( |
| 857 | StringExtractorGDBRemote &packet) { |
| 858 | m_thread_suffix_supported = true; |
| 859 | return SendOKResponse(); |
| 860 | } |
| 861 | |
| 862 | GDBRemoteCommunication::PacketResult |
| 863 | GDBRemoteCommunicationServerCommon::Handle_QListThreadsInStopReply( |
| 864 | StringExtractorGDBRemote &packet) { |
| 865 | m_list_threads_in_stop_reply = true; |
| 866 | return SendOKResponse(); |
| 867 | } |
| 868 | |
| 869 | GDBRemoteCommunication::PacketResult |
| 870 | GDBRemoteCommunicationServerCommon::Handle_QSetDetachOnError( |
| 871 | StringExtractorGDBRemote &packet) { |
| 872 | packet.SetFilePos(::strlen("QSetDetachOnError:")); |
| 873 | if (packet.GetU32(0)) |
| 874 | m_process_launch_info.GetFlags().Set(eLaunchFlagDetachOnError); |
| 875 | else |
| 876 | m_process_launch_info.GetFlags().Clear(eLaunchFlagDetachOnError); |
| 877 | return SendOKResponse(); |
| 878 | } |
| 879 | |
| 880 | GDBRemoteCommunication::PacketResult |
| 881 | GDBRemoteCommunicationServerCommon::Handle_QStartNoAckMode( |
| 882 | StringExtractorGDBRemote &packet) { |
| 883 | // Send response first before changing m_send_acks to we ack this packet |
| 884 | PacketResult packet_result = SendOKResponse(); |
| 885 | m_send_acks = false; |
| 886 | return packet_result; |
| 887 | } |
| 888 | |
| 889 | GDBRemoteCommunication::PacketResult |
| 890 | GDBRemoteCommunicationServerCommon::Handle_QSetSTDIN( |
| 891 | StringExtractorGDBRemote &packet) { |
| 892 | packet.SetFilePos(::strlen("QSetSTDIN:")); |
| 893 | FileAction file_action; |
| 894 | std::string path; |
| 895 | packet.GetHexByteString(path); |
| 896 | const bool read = true; |
| 897 | const bool write = false; |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 898 | if (file_action.Open(STDIN_FILENO, FileSpec(path), read, write)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 899 | m_process_launch_info.AppendFileAction(file_action); |
| 900 | return SendOKResponse(); |
| 901 | } |
| 902 | return SendErrorResponse(15); |
| 903 | } |
| 904 | |
| 905 | GDBRemoteCommunication::PacketResult |
| 906 | GDBRemoteCommunicationServerCommon::Handle_QSetSTDOUT( |
| 907 | StringExtractorGDBRemote &packet) { |
| 908 | packet.SetFilePos(::strlen("QSetSTDOUT:")); |
| 909 | FileAction file_action; |
| 910 | std::string path; |
| 911 | packet.GetHexByteString(path); |
| 912 | const bool read = false; |
| 913 | const bool write = true; |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 914 | if (file_action.Open(STDOUT_FILENO, FileSpec(path), read, write)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 915 | m_process_launch_info.AppendFileAction(file_action); |
| 916 | return SendOKResponse(); |
| 917 | } |
| 918 | return SendErrorResponse(16); |
| 919 | } |
| 920 | |
| 921 | GDBRemoteCommunication::PacketResult |
| 922 | GDBRemoteCommunicationServerCommon::Handle_QSetSTDERR( |
| 923 | StringExtractorGDBRemote &packet) { |
| 924 | packet.SetFilePos(::strlen("QSetSTDERR:")); |
| 925 | FileAction file_action; |
| 926 | std::string path; |
| 927 | packet.GetHexByteString(path); |
| 928 | const bool read = false; |
| 929 | const bool write = true; |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 930 | if (file_action.Open(STDERR_FILENO, FileSpec(path), read, write)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 931 | m_process_launch_info.AppendFileAction(file_action); |
| 932 | return SendOKResponse(); |
| 933 | } |
| 934 | return SendErrorResponse(17); |
| 935 | } |
| 936 | |
| 937 | GDBRemoteCommunication::PacketResult |
| 938 | GDBRemoteCommunicationServerCommon::Handle_qLaunchSuccess( |
| 939 | StringExtractorGDBRemote &packet) { |
| 940 | if (m_process_launch_error.Success()) |
| 941 | return SendOKResponse(); |
| 942 | StreamString response; |
| 943 | response.PutChar('E'); |
| 944 | response.PutCString(m_process_launch_error.AsCString("<unknown error>")); |
| 945 | return SendPacketNoLock(response.GetString()); |
| 946 | } |
| 947 | |
| 948 | GDBRemoteCommunication::PacketResult |
| 949 | GDBRemoteCommunicationServerCommon::Handle_QEnvironment( |
| 950 | StringExtractorGDBRemote &packet) { |
| 951 | packet.SetFilePos(::strlen("QEnvironment:")); |
| 952 | const uint32_t bytes_left = packet.GetBytesLeft(); |
| 953 | if (bytes_left > 0) { |
Pavel Labath | 62930e5 | 2018-01-10 11:57:31 +0000 | [diff] [blame] | 954 | m_process_launch_info.GetEnvironment().insert(packet.Peek()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 955 | return SendOKResponse(); |
| 956 | } |
| 957 | return SendErrorResponse(12); |
| 958 | } |
| 959 | |
| 960 | GDBRemoteCommunication::PacketResult |
| 961 | GDBRemoteCommunicationServerCommon::Handle_QEnvironmentHexEncoded( |
| 962 | StringExtractorGDBRemote &packet) { |
| 963 | packet.SetFilePos(::strlen("QEnvironmentHexEncoded:")); |
| 964 | const uint32_t bytes_left = packet.GetBytesLeft(); |
| 965 | if (bytes_left > 0) { |
| 966 | std::string str; |
| 967 | packet.GetHexByteString(str); |
Pavel Labath | 62930e5 | 2018-01-10 11:57:31 +0000 | [diff] [blame] | 968 | m_process_launch_info.GetEnvironment().insert(str); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 969 | return SendOKResponse(); |
| 970 | } |
| 971 | return SendErrorResponse(12); |
| 972 | } |
| 973 | |
| 974 | GDBRemoteCommunication::PacketResult |
| 975 | GDBRemoteCommunicationServerCommon::Handle_QLaunchArch( |
| 976 | StringExtractorGDBRemote &packet) { |
| 977 | packet.SetFilePos(::strlen("QLaunchArch:")); |
| 978 | const uint32_t bytes_left = packet.GetBytesLeft(); |
| 979 | if (bytes_left > 0) { |
| 980 | const char *arch_triple = packet.Peek(); |
Aaron Smith | 3a14249 | 2019-02-07 18:46:25 +0000 | [diff] [blame] | 981 | m_process_launch_info.SetArchitecture( |
| 982 | HostInfo::GetAugmentedArchSpec(arch_triple)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 983 | return SendOKResponse(); |
| 984 | } |
| 985 | return SendErrorResponse(13); |
| 986 | } |
| 987 | |
| 988 | GDBRemoteCommunication::PacketResult |
| 989 | GDBRemoteCommunicationServerCommon::Handle_A(StringExtractorGDBRemote &packet) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 990 | // The 'A' packet is the most over designed packet ever here with redundant |
| 991 | // argument indexes, redundant argument lengths and needed hex encoded |
| 992 | // argument string values. Really all that is needed is a comma separated hex |
| 993 | // encoded argument value list, but we will stay true to the documented |
| 994 | // version of the 'A' packet here... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 995 | |
| 996 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 997 | int actual_arg_index = 0; |
| 998 | |
| 999 | packet.SetFilePos(1); // Skip the 'A' |
| 1000 | bool success = true; |
| 1001 | while (success && packet.GetBytesLeft() > 0) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1002 | // Decode the decimal argument string length. This length is the number of |
| 1003 | // hex nibbles in the argument string value. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1004 | const uint32_t arg_len = packet.GetU32(UINT32_MAX); |
| 1005 | if (arg_len == UINT32_MAX) |
| 1006 | success = false; |
| 1007 | else { |
| 1008 | // Make sure the argument hex string length is followed by a comma |
| 1009 | if (packet.GetChar() != ',') |
| 1010 | success = false; |
| 1011 | else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1012 | // Decode the argument index. We ignore this really because who would |
| 1013 | // really send down the arguments in a random order??? |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1014 | const uint32_t arg_idx = packet.GetU32(UINT32_MAX); |
| 1015 | if (arg_idx == UINT32_MAX) |
| 1016 | success = false; |
| 1017 | else { |
| 1018 | // Make sure the argument index is followed by a comma |
| 1019 | if (packet.GetChar() != ',') |
| 1020 | success = false; |
| 1021 | else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1022 | // Decode the argument string value from hex bytes back into a UTF8 |
| 1023 | // string and make sure the length matches the one supplied in the |
| 1024 | // packet |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1025 | std::string arg; |
| 1026 | if (packet.GetHexByteStringFixedLength(arg, arg_len) != |
| 1027 | (arg_len / 2)) |
| 1028 | success = false; |
| 1029 | else { |
| 1030 | // If there are any bytes left |
| 1031 | if (packet.GetBytesLeft()) { |
| 1032 | if (packet.GetChar() != ',') |
| 1033 | success = false; |
| 1034 | } |
| 1035 | |
| 1036 | if (success) { |
| 1037 | if (arg_idx == 0) |
Jonas Devlieghere | 937348c | 2018-06-13 22:08:14 +0000 | [diff] [blame] | 1038 | m_process_launch_info.GetExecutableFile().SetFile( |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 1039 | arg, FileSpec::Style::native); |
Zachary Turner | ecbb0bb | 2016-09-19 17:54:06 +0000 | [diff] [blame] | 1040 | m_process_launch_info.GetArguments().AppendArgument(arg); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1041 | LLDB_LOGF(log, "LLGSPacketHandler::%s added arg %d: \"%s\"", |
| 1042 | __FUNCTION__, actual_arg_index, arg.c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1043 | ++actual_arg_index; |
| 1044 | } |
| 1045 | } |
| 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | if (success) { |
| 1053 | m_process_launch_error = LaunchProcess(); |
Pavel Labath | 96e600f | 2017-07-07 11:02:19 +0000 | [diff] [blame] | 1054 | if (m_process_launch_error.Success()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1055 | return SendOKResponse(); |
Pavel Labath | 96e600f | 2017-07-07 11:02:19 +0000 | [diff] [blame] | 1056 | LLDB_LOG(log, "failed to launch exe: {0}", m_process_launch_error); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1057 | } |
| 1058 | return SendErrorResponse(8); |
| 1059 | } |
| 1060 | |
| 1061 | GDBRemoteCommunication::PacketResult |
| 1062 | GDBRemoteCommunicationServerCommon::Handle_qEcho( |
| 1063 | StringExtractorGDBRemote &packet) { |
| 1064 | // Just echo back the exact same packet for qEcho... |
| 1065 | return SendPacketNoLock(packet.GetStringRef()); |
| 1066 | } |
| 1067 | |
| 1068 | GDBRemoteCommunication::PacketResult |
| 1069 | GDBRemoteCommunicationServerCommon::Handle_qModuleInfo( |
| 1070 | StringExtractorGDBRemote &packet) { |
| 1071 | packet.SetFilePos(::strlen("qModuleInfo:")); |
| 1072 | |
| 1073 | std::string module_path; |
| 1074 | packet.GetHexByteStringTerminatedBy(module_path, ';'); |
| 1075 | if (module_path.empty()) |
| 1076 | return SendErrorResponse(1); |
| 1077 | |
| 1078 | if (packet.GetChar() != ';') |
| 1079 | return SendErrorResponse(2); |
| 1080 | |
| 1081 | std::string triple; |
| 1082 | packet.GetHexByteString(triple); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1083 | |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 1084 | ModuleSpec matched_module_spec = GetModuleInfo(module_path, triple); |
| 1085 | if (!matched_module_spec.GetFileSpec()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1086 | return SendErrorResponse(3); |
| 1087 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1088 | const auto file_offset = matched_module_spec.GetObjectOffset(); |
| 1089 | const auto file_size = matched_module_spec.GetObjectSize(); |
| 1090 | const auto uuid_str = matched_module_spec.GetUUID().GetAsString(""); |
| 1091 | |
| 1092 | StreamGDBRemote response; |
| 1093 | |
| 1094 | if (uuid_str.empty()) { |
Aaron Smith | 3a14249 | 2019-02-07 18:46:25 +0000 | [diff] [blame] | 1095 | auto Result = llvm::sys::fs::md5_contents( |
| 1096 | matched_module_spec.GetFileSpec().GetPath()); |
Zachary Turner | 076a259 | 2017-03-20 23:54:54 +0000 | [diff] [blame] | 1097 | if (!Result) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1098 | return SendErrorResponse(5); |
| 1099 | response.PutCString("md5:"); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 1100 | response.PutStringAsRawHex8(Result->digest()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1101 | } else { |
| 1102 | response.PutCString("uuid:"); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 1103 | response.PutStringAsRawHex8(uuid_str); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1104 | } |
| 1105 | response.PutChar(';'); |
| 1106 | |
| 1107 | const auto &module_arch = matched_module_spec.GetArchitecture(); |
| 1108 | response.PutCString("triple:"); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 1109 | response.PutStringAsRawHex8(module_arch.GetTriple().getTriple()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1110 | response.PutChar(';'); |
| 1111 | |
| 1112 | response.PutCString("file_path:"); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 1113 | response.PutStringAsRawHex8(matched_module_spec.GetFileSpec().GetCString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1114 | response.PutChar(';'); |
| 1115 | response.PutCString("file_offset:"); |
| 1116 | response.PutHex64(file_offset); |
| 1117 | response.PutChar(';'); |
| 1118 | response.PutCString("file_size:"); |
| 1119 | response.PutHex64(file_size); |
| 1120 | response.PutChar(';'); |
| 1121 | |
| 1122 | return SendPacketNoLock(response.GetString()); |
| 1123 | } |
| 1124 | |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 1125 | GDBRemoteCommunication::PacketResult |
| 1126 | GDBRemoteCommunicationServerCommon::Handle_jModulesInfo( |
| 1127 | StringExtractorGDBRemote &packet) { |
Jonas Devlieghere | 4f8151e | 2019-10-02 18:02:32 +0000 | [diff] [blame] | 1128 | namespace json = llvm::json; |
| 1129 | |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 1130 | packet.SetFilePos(::strlen("jModulesInfo:")); |
| 1131 | |
| 1132 | StructuredData::ObjectSP object_sp = StructuredData::ParseJSON(packet.Peek()); |
| 1133 | if (!object_sp) |
| 1134 | return SendErrorResponse(1); |
| 1135 | |
| 1136 | StructuredData::Array *packet_array = object_sp->GetAsArray(); |
| 1137 | if (!packet_array) |
| 1138 | return SendErrorResponse(2); |
| 1139 | |
Jonas Devlieghere | 4f8151e | 2019-10-02 18:02:32 +0000 | [diff] [blame] | 1140 | json::Array response_array; |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 1141 | for (size_t i = 0; i < packet_array->GetSize(); ++i) { |
| 1142 | StructuredData::Dictionary *query = |
| 1143 | packet_array->GetItemAtIndex(i)->GetAsDictionary(); |
| 1144 | if (!query) |
| 1145 | continue; |
Zachary Turner | 2833321 | 2017-05-12 05:49:54 +0000 | [diff] [blame] | 1146 | llvm::StringRef file, triple; |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 1147 | if (!query->GetValueForKeyAsString("file", file) || |
| 1148 | !query->GetValueForKeyAsString("triple", triple)) |
| 1149 | continue; |
| 1150 | |
| 1151 | ModuleSpec matched_module_spec = GetModuleInfo(file, triple); |
| 1152 | if (!matched_module_spec.GetFileSpec()) |
| 1153 | continue; |
| 1154 | |
| 1155 | const auto file_offset = matched_module_spec.GetObjectOffset(); |
| 1156 | const auto file_size = matched_module_spec.GetObjectSize(); |
| 1157 | const auto uuid_str = matched_module_spec.GetUUID().GetAsString(""); |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 1158 | if (uuid_str.empty()) |
| 1159 | continue; |
Jonas Devlieghere | 4f8151e | 2019-10-02 18:02:32 +0000 | [diff] [blame] | 1160 | const auto triple_str = |
| 1161 | matched_module_spec.GetArchitecture().GetTriple().getTriple(); |
| 1162 | const auto file_path = matched_module_spec.GetFileSpec().GetPath(); |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 1163 | |
Jonas Devlieghere | 4f8151e | 2019-10-02 18:02:32 +0000 | [diff] [blame] | 1164 | json::Object response{{"uuid", uuid_str}, |
| 1165 | {"triple", triple_str}, |
| 1166 | {"file_path", file_path}, |
| 1167 | {"file_offset", static_cast<int64_t>(file_offset)}, |
| 1168 | {"file_size", static_cast<int64_t>(file_size)}}; |
| 1169 | response_array.push_back(std::move(response)); |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | StreamString response; |
Jonas Devlieghere | 4f8151e | 2019-10-02 18:02:32 +0000 | [diff] [blame] | 1173 | response.AsRawOstream() << std::move(response_array); |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 1174 | StreamGDBRemote escaped_response; |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 1175 | escaped_response.PutEscapedBytes(response.GetString().data(), |
| 1176 | response.GetSize()); |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 1177 | return SendPacketNoLock(escaped_response.GetString()); |
| 1178 | } |
| 1179 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1180 | void GDBRemoteCommunicationServerCommon::CreateProcessInfoResponse( |
| 1181 | const ProcessInstanceInfo &proc_info, StreamString &response) { |
| 1182 | response.Printf( |
| 1183 | "pid:%" PRIu64 ";ppid:%" PRIu64 ";uid:%i;gid:%i;euid:%i;egid:%i;", |
| 1184 | proc_info.GetProcessID(), proc_info.GetParentProcessID(), |
| 1185 | proc_info.GetUserID(), proc_info.GetGroupID(), |
| 1186 | proc_info.GetEffectiveUserID(), proc_info.GetEffectiveGroupID()); |
| 1187 | response.PutCString("name:"); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 1188 | response.PutStringAsRawHex8(proc_info.GetExecutableFile().GetCString()); |
Walter Erquinigo | 48a50ee | 2019-10-16 18:47:05 +0000 | [diff] [blame] | 1189 | |
| 1190 | response.PutChar(';'); |
| 1191 | response.PutCString("args:"); |
| 1192 | response.PutStringAsRawHex8(proc_info.GetArg0()); |
| 1193 | for (auto &arg : proc_info.GetArguments()) { |
| 1194 | response.PutChar('-'); |
| 1195 | response.PutStringAsRawHex8(arg.ref()); |
| 1196 | } |
| 1197 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1198 | response.PutChar(';'); |
| 1199 | const ArchSpec &proc_arch = proc_info.GetArchitecture(); |
| 1200 | if (proc_arch.IsValid()) { |
| 1201 | const llvm::Triple &proc_triple = proc_arch.GetTriple(); |
| 1202 | response.PutCString("triple:"); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 1203 | response.PutStringAsRawHex8(proc_triple.getTriple()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1204 | response.PutChar(';'); |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | void GDBRemoteCommunicationServerCommon:: |
| 1209 | CreateProcessInfoResponse_DebugServerStyle( |
| 1210 | const ProcessInstanceInfo &proc_info, StreamString &response) { |
| 1211 | response.Printf("pid:%" PRIx64 ";parent-pid:%" PRIx64 |
| 1212 | ";real-uid:%x;real-gid:%x;effective-uid:%x;effective-gid:%x;", |
| 1213 | proc_info.GetProcessID(), proc_info.GetParentProcessID(), |
| 1214 | proc_info.GetUserID(), proc_info.GetGroupID(), |
| 1215 | proc_info.GetEffectiveUserID(), |
| 1216 | proc_info.GetEffectiveGroupID()); |
| 1217 | |
| 1218 | const ArchSpec &proc_arch = proc_info.GetArchitecture(); |
| 1219 | if (proc_arch.IsValid()) { |
| 1220 | const llvm::Triple &proc_triple = proc_arch.GetTriple(); |
| 1221 | #if defined(__APPLE__) |
| 1222 | // We'll send cputype/cpusubtype. |
| 1223 | const uint32_t cpu_type = proc_arch.GetMachOCPUType(); |
| 1224 | if (cpu_type != 0) |
| 1225 | response.Printf("cputype:%" PRIx32 ";", cpu_type); |
| 1226 | |
| 1227 | const uint32_t cpu_subtype = proc_arch.GetMachOCPUSubType(); |
| 1228 | if (cpu_subtype != 0) |
| 1229 | response.Printf("cpusubtype:%" PRIx32 ";", cpu_subtype); |
| 1230 | |
| 1231 | const std::string vendor = proc_triple.getVendorName(); |
| 1232 | if (!vendor.empty()) |
| 1233 | response.Printf("vendor:%s;", vendor.c_str()); |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 1234 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1235 | // We'll send the triple. |
| 1236 | response.PutCString("triple:"); |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame] | 1237 | response.PutStringAsRawHex8(proc_triple.getTriple()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1238 | response.PutChar(';'); |
| 1239 | #endif |
| 1240 | std::string ostype = proc_triple.getOSName(); |
| 1241 | // Adjust so ostype reports ios for Apple/ARM and Apple/ARM64. |
| 1242 | if (proc_triple.getVendor() == llvm::Triple::Apple) { |
| 1243 | switch (proc_triple.getArch()) { |
| 1244 | case llvm::Triple::arm: |
| 1245 | case llvm::Triple::thumb: |
| 1246 | case llvm::Triple::aarch64: |
Jason Molenda | 7dd7a36 | 2019-10-16 19:14:49 +0000 | [diff] [blame] | 1247 | case llvm::Triple::aarch64_32: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1248 | ostype = "ios"; |
| 1249 | break; |
| 1250 | default: |
| 1251 | // No change. |
| 1252 | break; |
| 1253 | } |
| 1254 | } |
| 1255 | response.Printf("ostype:%s;", ostype.c_str()); |
| 1256 | |
| 1257 | switch (proc_arch.GetByteOrder()) { |
| 1258 | case lldb::eByteOrderLittle: |
| 1259 | response.PutCString("endian:little;"); |
| 1260 | break; |
| 1261 | case lldb::eByteOrderBig: |
| 1262 | response.PutCString("endian:big;"); |
| 1263 | break; |
| 1264 | case lldb::eByteOrderPDP: |
| 1265 | response.PutCString("endian:pdp;"); |
| 1266 | break; |
| 1267 | default: |
| 1268 | // Nothing. |
| 1269 | break; |
| 1270 | } |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1271 | // In case of MIPS64, pointer size is depend on ELF ABI For N32 the pointer |
| 1272 | // size is 4 and for N64 it is 8 |
Nitesh Jain | 8999edf | 2016-10-12 10:21:09 +0000 | [diff] [blame] | 1273 | std::string abi = proc_arch.GetTargetABI(); |
| 1274 | if (!abi.empty()) |
| 1275 | response.Printf("elf_abi:%s;", abi.c_str()); |
| 1276 | response.Printf("ptrsize:%d;", proc_arch.GetAddressByteSize()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | FileSpec GDBRemoteCommunicationServerCommon::FindModuleFile( |
| 1281 | const std::string &module_path, const ArchSpec &arch) { |
| 1282 | #ifdef __ANDROID__ |
| 1283 | return HostInfoAndroid::ResolveLibraryPath(module_path, arch); |
| 1284 | #else |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 1285 | FileSpec file_spec(module_path); |
| 1286 | FileSystem::Instance().Resolve(file_spec); |
| 1287 | return file_spec; |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 1288 | #endif |
| 1289 | } |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 1290 | |
Zachary Turner | 2833321 | 2017-05-12 05:49:54 +0000 | [diff] [blame] | 1291 | ModuleSpec |
| 1292 | GDBRemoteCommunicationServerCommon::GetModuleInfo(llvm::StringRef module_path, |
| 1293 | llvm::StringRef triple) { |
| 1294 | ArchSpec arch(triple); |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 1295 | |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 1296 | FileSpec req_module_path_spec(module_path); |
| 1297 | FileSystem::Instance().Resolve(req_module_path_spec); |
| 1298 | |
Pavel Labath | 2f1fbae | 2016-09-08 10:07:04 +0000 | [diff] [blame] | 1299 | const FileSpec module_path_spec = |
| 1300 | FindModuleFile(req_module_path_spec.GetPath(), arch); |
| 1301 | const ModuleSpec module_spec(module_path_spec, arch); |
| 1302 | |
| 1303 | ModuleSpecList module_specs; |
| 1304 | if (!ObjectFile::GetModuleSpecifications(module_path_spec, 0, 0, |
| 1305 | module_specs)) |
| 1306 | return ModuleSpec(); |
| 1307 | |
| 1308 | ModuleSpec matched_module_spec; |
| 1309 | if (!module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec)) |
| 1310 | return ModuleSpec(); |
| 1311 | |
| 1312 | return matched_module_spec; |
| 1313 | } |