Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 1 | //===-- GDBRemoteCommunicationServerPlatform.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 "GDBRemoteCommunicationServerPlatform.h" |
| 11 | |
| 12 | #include <errno.h> |
| 13 | |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 14 | #include <chrono> |
Pavel Labath | 6b3c8bb | 2018-04-05 16:23:54 +0000 | [diff] [blame] | 15 | #include <csignal> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 16 | #include <cstring> |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 17 | #include <mutex> |
| 18 | #include <sstream> |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 19 | |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 20 | #include "llvm/Support/FileSystem.h" |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Threading.h" |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 22 | |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 23 | #include "lldb/Host/Config.h" |
| 24 | #include "lldb/Host/ConnectionFileDescriptor.h" |
| 25 | #include "lldb/Host/Host.h" |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 26 | #include "lldb/Host/HostInfo.h" |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 27 | #include "lldb/Target/FileAction.h" |
| 28 | #include "lldb/Target/Platform.h" |
| 29 | #include "lldb/Target/Process.h" |
Chaoren Lin | 98d0a4b | 2015-07-14 01:09:28 +0000 | [diff] [blame] | 30 | #include "lldb/Target/UnixSignals.h" |
Tamas Berghammer | ccd6cff | 2015-12-08 14:08:19 +0000 | [diff] [blame] | 31 | #include "lldb/Utility/JSON.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 32 | #include "lldb/Utility/Log.h" |
Zachary Turner | fb1a0a0 | 2017-03-06 18:34:25 +0000 | [diff] [blame] | 33 | #include "lldb/Utility/StreamGDBRemote.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 34 | #include "lldb/Utility/StreamString.h" |
Pavel Labath | f2a8bcc | 2017-06-27 10:45:31 +0000 | [diff] [blame] | 35 | #include "lldb/Utility/StructuredData.h" |
Pavel Labath | 5f7e583 | 2017-02-10 12:21:22 +0000 | [diff] [blame] | 36 | #include "lldb/Utility/UriParser.h" |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 37 | |
Pavel Labath | 9af71b3 | 2018-03-20 16:14:00 +0000 | [diff] [blame] | 38 | #include "lldb/Utility/StringExtractorGDBRemote.h" |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 39 | |
| 40 | using namespace lldb; |
| 41 | using namespace lldb_private; |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 42 | using namespace lldb_private::process_gdb_remote; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 43 | |
| 44 | //---------------------------------------------------------------------- |
| 45 | // GDBRemoteCommunicationServerPlatform constructor |
| 46 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 47 | GDBRemoteCommunicationServerPlatform::GDBRemoteCommunicationServerPlatform( |
| 48 | const Socket::SocketProtocol socket_protocol, const char *socket_scheme) |
| 49 | : GDBRemoteCommunicationServerCommon("gdb-remote.server", |
| 50 | "gdb-remote.server.rx_packet"), |
| 51 | m_socket_protocol(socket_protocol), m_socket_scheme(socket_scheme), |
| 52 | m_spawned_pids_mutex(), m_port_map(), m_port_offset(0) { |
| 53 | m_pending_gdb_server.pid = LLDB_INVALID_PROCESS_ID; |
| 54 | m_pending_gdb_server.port = 0; |
Tamas Berghammer | 372810f | 2015-12-08 14:27:40 +0000 | [diff] [blame] | 55 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | RegisterMemberFunctionHandler( |
| 57 | StringExtractorGDBRemote::eServerPacketType_qC, |
| 58 | &GDBRemoteCommunicationServerPlatform::Handle_qC); |
| 59 | RegisterMemberFunctionHandler( |
| 60 | StringExtractorGDBRemote::eServerPacketType_qGetWorkingDir, |
| 61 | &GDBRemoteCommunicationServerPlatform::Handle_qGetWorkingDir); |
| 62 | RegisterMemberFunctionHandler( |
| 63 | StringExtractorGDBRemote::eServerPacketType_qLaunchGDBServer, |
| 64 | &GDBRemoteCommunicationServerPlatform::Handle_qLaunchGDBServer); |
| 65 | RegisterMemberFunctionHandler( |
| 66 | StringExtractorGDBRemote::eServerPacketType_qQueryGDBServer, |
| 67 | &GDBRemoteCommunicationServerPlatform::Handle_qQueryGDBServer); |
| 68 | RegisterMemberFunctionHandler( |
| 69 | StringExtractorGDBRemote::eServerPacketType_qKillSpawnedProcess, |
| 70 | &GDBRemoteCommunicationServerPlatform::Handle_qKillSpawnedProcess); |
| 71 | RegisterMemberFunctionHandler( |
| 72 | StringExtractorGDBRemote::eServerPacketType_qProcessInfo, |
| 73 | &GDBRemoteCommunicationServerPlatform::Handle_qProcessInfo); |
| 74 | RegisterMemberFunctionHandler( |
| 75 | StringExtractorGDBRemote::eServerPacketType_QSetWorkingDir, |
| 76 | &GDBRemoteCommunicationServerPlatform::Handle_QSetWorkingDir); |
| 77 | RegisterMemberFunctionHandler( |
| 78 | StringExtractorGDBRemote::eServerPacketType_jSignalsInfo, |
| 79 | &GDBRemoteCommunicationServerPlatform::Handle_jSignalsInfo); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 80 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 81 | RegisterPacketHandler(StringExtractorGDBRemote::eServerPacketType_interrupt, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 82 | [](StringExtractorGDBRemote packet, Status &error, |
Zachary Turner | 3bc714b | 2017-03-02 00:05:25 +0000 | [diff] [blame] | 83 | bool &interrupt, bool &quit) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | error.SetErrorString("interrupt received"); |
| 85 | interrupt = true; |
| 86 | return PacketResult::Success; |
| 87 | }); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | //---------------------------------------------------------------------- |
| 91 | // Destructor |
| 92 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 93 | GDBRemoteCommunicationServerPlatform::~GDBRemoteCommunicationServerPlatform() {} |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 94 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 95 | Status GDBRemoteCommunicationServerPlatform::LaunchGDBServer( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | const lldb_private::Args &args, std::string hostname, lldb::pid_t &pid, |
| 97 | uint16_t &port, std::string &socket_name) { |
| 98 | if (port == UINT16_MAX) |
| 99 | port = GetNextAvailablePort(); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 100 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 101 | // Spawn a new thread to accept the port that gets bound after binding to |
| 102 | // port 0 (zero). |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 103 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 104 | // ignore the hostname send from the remote end, just use the ip address that |
| 105 | // we're currently communicating with as the hostname |
Tamas Berghammer | ccd6cff | 2015-12-08 14:08:19 +0000 | [diff] [blame] | 106 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 107 | // Spawn a debugserver and try to get the port it listens to. |
| 108 | ProcessLaunchInfo debugserver_launch_info; |
| 109 | if (hostname.empty()) |
| 110 | hostname = "127.0.0.1"; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 111 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 112 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM)); |
| 113 | if (log) |
| 114 | log->Printf("Launching debugserver with: %s:%u...", hostname.c_str(), port); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 115 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 116 | // Do not run in a new session so that it can not linger after the platform |
| 117 | // closes. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 118 | debugserver_launch_info.SetLaunchInSeparateProcessGroup(false); |
| 119 | debugserver_launch_info.SetMonitorProcessCallback( |
| 120 | std::bind(&GDBRemoteCommunicationServerPlatform::DebugserverProcessReaped, |
| 121 | this, std::placeholders::_1), |
| 122 | false); |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 123 | |
Zachary Turner | 245f7fd | 2016-11-17 01:38:02 +0000 | [diff] [blame] | 124 | llvm::StringRef platform_scheme; |
| 125 | llvm::StringRef platform_ip; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 126 | int platform_port; |
Zachary Turner | 245f7fd | 2016-11-17 01:38:02 +0000 | [diff] [blame] | 127 | llvm::StringRef platform_path; |
Pavel Labath | ae7dd12 | 2017-10-27 04:53:24 +0000 | [diff] [blame] | 128 | std::string platform_uri = GetConnection()->GetURI(); |
| 129 | bool ok = UriParser::Parse(platform_uri, platform_scheme, platform_ip, |
| 130 | platform_port, platform_path); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 131 | UNUSED_IF_ASSERT_DISABLED(ok); |
| 132 | assert(ok); |
| 133 | |
| 134 | std::ostringstream url; |
| 135 | // debugserver does not accept the URL scheme prefix. |
Todd Fiala | 873a2ab | 2016-05-27 04:04:52 +0000 | [diff] [blame] | 136 | #if !defined(__APPLE__) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 137 | url << m_socket_scheme << "://"; |
Todd Fiala | 873a2ab | 2016-05-27 04:04:52 +0000 | [diff] [blame] | 138 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 139 | uint16_t *port_ptr = &port; |
| 140 | if (m_socket_protocol == Socket::ProtocolTcp) |
Zachary Turner | 245f7fd | 2016-11-17 01:38:02 +0000 | [diff] [blame] | 141 | url << platform_ip.str() << ":" << port; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 142 | else { |
| 143 | socket_name = GetDomainSocketPath("gdbserver").GetPath(); |
| 144 | url << socket_name; |
| 145 | port_ptr = nullptr; |
| 146 | } |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 147 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 148 | Status error = StartDebugserverProcess( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 149 | url.str().c_str(), nullptr, debugserver_launch_info, port_ptr, &args, -1); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 150 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 151 | pid = debugserver_launch_info.GetProcessID(); |
| 152 | if (pid != LLDB_INVALID_PROCESS_ID) { |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 153 | std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 154 | m_spawned_pids.insert(pid); |
| 155 | if (port > 0) |
| 156 | AssociatePortWithProcess(port, pid); |
| 157 | } else { |
| 158 | if (port > 0) |
| 159 | FreePort(port); |
| 160 | } |
| 161 | return error; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 164 | GDBRemoteCommunication::PacketResult |
| 165 | GDBRemoteCommunicationServerPlatform::Handle_qLaunchGDBServer( |
| 166 | StringExtractorGDBRemote &packet) { |
| 167 | #ifdef _WIN32 |
| 168 | return SendErrorResponse(9); |
| 169 | #else |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 170 | // Spawn a local debugserver as a platform so we can then attach or launch a |
| 171 | // process... |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 172 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 173 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM)); |
| 174 | if (log) |
| 175 | log->Printf("GDBRemoteCommunicationServerPlatform::%s() called", |
| 176 | __FUNCTION__); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 177 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 178 | ConnectionFileDescriptor file_conn; |
| 179 | std::string hostname; |
| 180 | packet.SetFilePos(::strlen("qLaunchGDBServer;")); |
| 181 | llvm::StringRef name; |
| 182 | llvm::StringRef value; |
| 183 | uint16_t port = UINT16_MAX; |
| 184 | while (packet.GetNameColonValue(name, value)) { |
| 185 | if (name.equals("host")) |
| 186 | hostname = value; |
| 187 | else if (name.equals("port")) |
| 188 | value.getAsInteger(0, port); |
| 189 | } |
| 190 | |
| 191 | lldb::pid_t debugserver_pid = LLDB_INVALID_PROCESS_ID; |
| 192 | std::string socket_name; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 193 | Status error = |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 194 | LaunchGDBServer(Args(), hostname, debugserver_pid, port, socket_name); |
| 195 | if (error.Fail()) { |
| 196 | if (log) |
| 197 | log->Printf("GDBRemoteCommunicationServerPlatform::%s() debugserver " |
| 198 | "launch failed: %s", |
| 199 | __FUNCTION__, error.AsCString()); |
| 200 | return SendErrorResponse(9); |
| 201 | } |
| 202 | |
| 203 | if (log) |
| 204 | log->Printf("GDBRemoteCommunicationServerPlatform::%s() debugserver " |
| 205 | "launched successfully as pid %" PRIu64, |
| 206 | __FUNCTION__, debugserver_pid); |
| 207 | |
| 208 | StreamGDBRemote response; |
| 209 | response.Printf("pid:%" PRIu64 ";port:%u;", debugserver_pid, |
| 210 | port + m_port_offset); |
| 211 | if (!socket_name.empty()) { |
| 212 | response.PutCString("socket_name:"); |
| 213 | response.PutCStringAsRawHex8(socket_name.c_str()); |
| 214 | response.PutChar(';'); |
| 215 | } |
| 216 | |
| 217 | PacketResult packet_result = SendPacketNoLock(response.GetString()); |
| 218 | if (packet_result != PacketResult::Success) { |
| 219 | if (debugserver_pid != LLDB_INVALID_PROCESS_ID) |
| 220 | ::kill(debugserver_pid, SIGINT); |
| 221 | } |
| 222 | return packet_result; |
| 223 | #endif |
| 224 | } |
| 225 | |
| 226 | GDBRemoteCommunication::PacketResult |
| 227 | GDBRemoteCommunicationServerPlatform::Handle_qQueryGDBServer( |
| 228 | StringExtractorGDBRemote &packet) { |
| 229 | if (m_pending_gdb_server.pid == LLDB_INVALID_PROCESS_ID) |
| 230 | return SendErrorResponse(4); |
| 231 | |
| 232 | JSONObject::SP server_sp = std::make_shared<JSONObject>(); |
| 233 | server_sp->SetObject("port", |
| 234 | std::make_shared<JSONNumber>(m_pending_gdb_server.port)); |
| 235 | if (!m_pending_gdb_server.socket_name.empty()) |
| 236 | server_sp->SetObject( |
| 237 | "socket_name", |
| 238 | std::make_shared<JSONString>(m_pending_gdb_server.socket_name.c_str())); |
| 239 | |
| 240 | JSONArray server_list; |
| 241 | server_list.AppendObject(server_sp); |
| 242 | |
| 243 | StreamGDBRemote response; |
| 244 | server_list.Write(response); |
| 245 | |
| 246 | StreamGDBRemote escaped_response; |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 247 | escaped_response.PutEscapedBytes(response.GetString().data(), |
| 248 | response.GetSize()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 249 | return SendPacketNoLock(escaped_response.GetString()); |
| 250 | } |
| 251 | |
| 252 | GDBRemoteCommunication::PacketResult |
| 253 | GDBRemoteCommunicationServerPlatform::Handle_qKillSpawnedProcess( |
| 254 | StringExtractorGDBRemote &packet) { |
| 255 | packet.SetFilePos(::strlen("qKillSpawnedProcess:")); |
| 256 | |
| 257 | lldb::pid_t pid = packet.GetU64(LLDB_INVALID_PROCESS_ID); |
| 258 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 259 | // verify that we know anything about this pid. Scope for locker |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 260 | { |
| 261 | std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex); |
| 262 | if (m_spawned_pids.find(pid) == m_spawned_pids.end()) { |
| 263 | // not a pid we know about |
| 264 | return SendErrorResponse(10); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 265 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 266 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 267 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 268 | // go ahead and attempt to kill the spawned process |
| 269 | if (KillSpawnedProcess(pid)) |
| 270 | return SendOKResponse(); |
| 271 | else |
| 272 | return SendErrorResponse(11); |
| 273 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 274 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 275 | bool GDBRemoteCommunicationServerPlatform::KillSpawnedProcess(lldb::pid_t pid) { |
| 276 | // make sure we know about this process |
| 277 | { |
| 278 | std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex); |
| 279 | if (m_spawned_pids.find(pid) == m_spawned_pids.end()) |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | // first try a SIGTERM (standard kill) |
| 284 | Host::Kill(pid, SIGTERM); |
| 285 | |
| 286 | // check if that worked |
| 287 | for (size_t i = 0; i < 10; ++i) { |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 288 | { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 289 | std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex); |
| 290 | if (m_spawned_pids.find(pid) == m_spawned_pids.end()) { |
| 291 | // it is now killed |
| 292 | return true; |
| 293 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 294 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 295 | usleep(10000); |
| 296 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 297 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 298 | // check one more time after the final usleep |
| 299 | { |
| 300 | std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex); |
| 301 | if (m_spawned_pids.find(pid) == m_spawned_pids.end()) |
| 302 | return true; |
| 303 | } |
| 304 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 305 | // the launched process still lives. Now try killing it again, this time |
| 306 | // with an unblockable signal. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 307 | Host::Kill(pid, SIGKILL); |
| 308 | |
| 309 | for (size_t i = 0; i < 10; ++i) { |
| 310 | { |
| 311 | std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex); |
| 312 | if (m_spawned_pids.find(pid) == m_spawned_pids.end()) { |
| 313 | // it is now killed |
| 314 | return true; |
| 315 | } |
| 316 | } |
| 317 | usleep(10000); |
| 318 | } |
| 319 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 320 | // check one more time after the final usleep Scope for locker |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 321 | { |
| 322 | std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex); |
| 323 | if (m_spawned_pids.find(pid) == m_spawned_pids.end()) |
| 324 | return true; |
| 325 | } |
| 326 | |
| 327 | // no luck - the process still lives |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | GDBRemoteCommunication::PacketResult |
| 332 | GDBRemoteCommunicationServerPlatform::Handle_qProcessInfo( |
| 333 | StringExtractorGDBRemote &packet) { |
| 334 | lldb::pid_t pid = m_process_launch_info.GetProcessID(); |
| 335 | m_process_launch_info.Clear(); |
| 336 | |
| 337 | if (pid == LLDB_INVALID_PROCESS_ID) |
| 338 | return SendErrorResponse(1); |
| 339 | |
| 340 | ProcessInstanceInfo proc_info; |
| 341 | if (!Host::GetProcessInfo(pid, proc_info)) |
| 342 | return SendErrorResponse(1); |
| 343 | |
| 344 | StreamString response; |
| 345 | CreateProcessInfoResponse_DebugServerStyle(proc_info, response); |
| 346 | return SendPacketNoLock(response.GetString()); |
| 347 | } |
| 348 | |
| 349 | GDBRemoteCommunication::PacketResult |
| 350 | GDBRemoteCommunicationServerPlatform::Handle_qGetWorkingDir( |
| 351 | StringExtractorGDBRemote &packet) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 352 | |
Pavel Labath | 1d5855b | 2017-01-23 15:56:45 +0000 | [diff] [blame] | 353 | llvm::SmallString<64> cwd; |
| 354 | if (std::error_code ec = llvm::sys::fs::current_path(cwd)) |
| 355 | return SendErrorResponse(ec.value()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 356 | |
| 357 | StreamString response; |
Pavel Labath | 1d5855b | 2017-01-23 15:56:45 +0000 | [diff] [blame] | 358 | response.PutBytesAsRawHex8(cwd.data(), cwd.size()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 359 | return SendPacketNoLock(response.GetString()); |
| 360 | } |
| 361 | |
| 362 | GDBRemoteCommunication::PacketResult |
| 363 | GDBRemoteCommunicationServerPlatform::Handle_QSetWorkingDir( |
| 364 | StringExtractorGDBRemote &packet) { |
| 365 | packet.SetFilePos(::strlen("QSetWorkingDir:")); |
| 366 | std::string path; |
| 367 | packet.GetHexByteString(path); |
| 368 | |
Pavel Labath | 2d0c5b0 | 2017-01-25 11:10:52 +0000 | [diff] [blame] | 369 | if (std::error_code ec = llvm::sys::fs::set_current_path(path)) |
| 370 | return SendErrorResponse(ec.value()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 371 | return SendOKResponse(); |
| 372 | } |
| 373 | |
| 374 | GDBRemoteCommunication::PacketResult |
| 375 | GDBRemoteCommunicationServerPlatform::Handle_qC( |
| 376 | StringExtractorGDBRemote &packet) { |
| 377 | // NOTE: lldb should now be using qProcessInfo for process IDs. This path |
| 378 | // here |
| 379 | // should not be used. It is reporting process id instead of thread id. The |
| 380 | // correct answer doesn't seem to make much sense for lldb-platform. |
| 381 | // CONSIDER: flip to "unsupported". |
| 382 | lldb::pid_t pid = m_process_launch_info.GetProcessID(); |
| 383 | |
| 384 | StreamString response; |
| 385 | response.Printf("QC%" PRIx64, pid); |
| 386 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 387 | // If we launch a process and this GDB server is acting as a platform, then |
| 388 | // we need to clear the process launch state so we can start launching |
| 389 | // another process. In order to launch a process a bunch or packets need to |
| 390 | // be sent: environment packets, working directory, disable ASLR, and many |
| 391 | // more settings. When we launch a process we then need to know when to clear |
| 392 | // this information. Currently we are selecting the 'qC' packet as that |
| 393 | // packet which seems to make the most sense. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 394 | if (pid != LLDB_INVALID_PROCESS_ID) { |
| 395 | m_process_launch_info.Clear(); |
| 396 | } |
| 397 | |
| 398 | return SendPacketNoLock(response.GetString()); |
| 399 | } |
| 400 | |
| 401 | GDBRemoteCommunication::PacketResult |
| 402 | GDBRemoteCommunicationServerPlatform::Handle_jSignalsInfo( |
| 403 | StringExtractorGDBRemote &packet) { |
| 404 | StructuredData::Array signal_array; |
| 405 | |
| 406 | const auto &signals = Host::GetUnixSignals(); |
| 407 | for (auto signo = signals->GetFirstSignalNumber(); |
| 408 | signo != LLDB_INVALID_SIGNAL_NUMBER; |
| 409 | signo = signals->GetNextSignalNumber(signo)) { |
| 410 | auto dictionary = std::make_shared<StructuredData::Dictionary>(); |
| 411 | |
| 412 | dictionary->AddIntegerItem("signo", signo); |
| 413 | dictionary->AddStringItem("name", signals->GetSignalAsCString(signo)); |
| 414 | |
| 415 | bool suppress, stop, notify; |
| 416 | signals->GetSignalInfo(signo, suppress, stop, notify); |
| 417 | dictionary->AddBooleanItem("suppress", suppress); |
| 418 | dictionary->AddBooleanItem("stop", stop); |
| 419 | dictionary->AddBooleanItem("notify", notify); |
| 420 | |
| 421 | signal_array.Push(dictionary); |
| 422 | } |
| 423 | |
| 424 | StreamString response; |
| 425 | signal_array.Dump(response); |
| 426 | return SendPacketNoLock(response.GetString()); |
| 427 | } |
| 428 | |
| 429 | bool GDBRemoteCommunicationServerPlatform::DebugserverProcessReaped( |
| 430 | lldb::pid_t pid) { |
| 431 | std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex); |
| 432 | FreePortForProcess(pid); |
| 433 | m_spawned_pids.erase(pid); |
| 434 | return true; |
| 435 | } |
| 436 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 437 | Status GDBRemoteCommunicationServerPlatform::LaunchProcess() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 438 | if (!m_process_launch_info.GetArguments().GetArgumentCount()) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 439 | return Status("%s: no process command line specified to launch", |
| 440 | __FUNCTION__); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 441 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 442 | // specify the process monitor if not already set. This should generally be |
| 443 | // what happens since we need to reap started processes. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 444 | if (!m_process_launch_info.GetMonitorProcessCallback()) |
| 445 | m_process_launch_info.SetMonitorProcessCallback( |
| 446 | std::bind( |
| 447 | &GDBRemoteCommunicationServerPlatform::DebugserverProcessReaped, |
| 448 | this, std::placeholders::_1), |
| 449 | false); |
| 450 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 451 | Status error = Host::LaunchProcess(m_process_launch_info); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 452 | if (!error.Success()) { |
| 453 | fprintf(stderr, "%s: failed to launch executable %s", __FUNCTION__, |
| 454 | m_process_launch_info.GetArguments().GetArgumentAtIndex(0)); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 455 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | printf("Launched '%s' as process %" PRIu64 "...\n", |
| 459 | m_process_launch_info.GetArguments().GetArgumentAtIndex(0), |
| 460 | m_process_launch_info.GetProcessID()); |
| 461 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 462 | // add to list of spawned processes. On an lldb-gdbserver, we would expect |
| 463 | // there to be only one. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 464 | const auto pid = m_process_launch_info.GetProcessID(); |
| 465 | if (pid != LLDB_INVALID_PROCESS_ID) { |
| 466 | // add to spawned pids |
| 467 | std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex); |
| 468 | m_spawned_pids.insert(pid); |
| 469 | } |
| 470 | |
| 471 | return error; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 474 | void GDBRemoteCommunicationServerPlatform::SetPortMap(PortMap &&port_map) { |
| 475 | m_port_map = port_map; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 478 | uint16_t GDBRemoteCommunicationServerPlatform::GetNextAvailablePort() { |
| 479 | if (m_port_map.empty()) |
| 480 | return 0; // Bind to port zero and get a port, we didn't have any |
| 481 | // limitations |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 482 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 483 | for (auto &pair : m_port_map) { |
| 484 | if (pair.second == LLDB_INVALID_PROCESS_ID) { |
| 485 | pair.second = ~(lldb::pid_t)LLDB_INVALID_PROCESS_ID; |
| 486 | return pair.first; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 487 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 488 | } |
| 489 | return UINT16_MAX; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 492 | bool GDBRemoteCommunicationServerPlatform::AssociatePortWithProcess( |
| 493 | uint16_t port, lldb::pid_t pid) { |
| 494 | PortMap::iterator pos = m_port_map.find(port); |
| 495 | if (pos != m_port_map.end()) { |
| 496 | pos->second = pid; |
| 497 | return true; |
| 498 | } |
| 499 | return false; |
| 500 | } |
| 501 | |
| 502 | bool GDBRemoteCommunicationServerPlatform::FreePort(uint16_t port) { |
| 503 | PortMap::iterator pos = m_port_map.find(port); |
| 504 | if (pos != m_port_map.end()) { |
| 505 | pos->second = LLDB_INVALID_PROCESS_ID; |
| 506 | return true; |
| 507 | } |
| 508 | return false; |
| 509 | } |
| 510 | |
| 511 | bool GDBRemoteCommunicationServerPlatform::FreePortForProcess(lldb::pid_t pid) { |
| 512 | if (!m_port_map.empty()) { |
| 513 | for (auto &pair : m_port_map) { |
| 514 | if (pair.second == pid) { |
| 515 | pair.second = LLDB_INVALID_PROCESS_ID; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 516 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 517 | } |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 518 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 519 | } |
| 520 | return false; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 523 | const FileSpec &GDBRemoteCommunicationServerPlatform::GetDomainSocketDir() { |
| 524 | static FileSpec g_domainsocket_dir; |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 525 | static llvm::once_flag g_once_flag; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 526 | |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 527 | llvm::call_once(g_once_flag, []() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 528 | const char *domainsocket_dir_env = |
| 529 | ::getenv("LLDB_DEBUGSERVER_DOMAINSOCKET_DIR"); |
| 530 | if (domainsocket_dir_env != nullptr) |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 531 | g_domainsocket_dir = FileSpec(domainsocket_dir_env); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 532 | else |
Pavel Labath | 60f028f | 2018-06-19 15:09:07 +0000 | [diff] [blame] | 533 | g_domainsocket_dir = HostInfo::GetProcessTempDir(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 534 | }); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 535 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 536 | return g_domainsocket_dir; |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | FileSpec |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 540 | GDBRemoteCommunicationServerPlatform::GetDomainSocketPath(const char *prefix) { |
Stella Stamenova | b3f44ad | 2018-12-10 17:23:28 +0000 | [diff] [blame] | 541 | llvm::SmallString<128> socket_path; |
| 542 | llvm::SmallString<128> socket_name( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 543 | (llvm::StringRef(prefix) + ".%%%%%%").str()); |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 544 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 545 | FileSpec socket_path_spec(GetDomainSocketDir()); |
| 546 | socket_path_spec.AppendPathComponent(socket_name.c_str()); |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 547 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 548 | llvm::sys::fs::createUniqueFile(socket_path_spec.GetCString(), socket_path); |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 549 | return FileSpec(socket_path.c_str()); |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 552 | void GDBRemoteCommunicationServerPlatform::SetPortOffset(uint16_t port_offset) { |
| 553 | m_port_offset = port_offset; |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 554 | } |
Tamas Berghammer | ccd6cff | 2015-12-08 14:08:19 +0000 | [diff] [blame] | 555 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 556 | void GDBRemoteCommunicationServerPlatform::SetPendingGdbServer( |
| 557 | lldb::pid_t pid, uint16_t port, const std::string &socket_name) { |
| 558 | m_pending_gdb_server.pid = pid; |
| 559 | m_pending_gdb_server.port = port; |
| 560 | m_pending_gdb_server.socket_name = socket_name; |
Tamas Berghammer | ccd6cff | 2015-12-08 14:08:19 +0000 | [diff] [blame] | 561 | } |