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