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