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