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