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