Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- GDBRemoteCommunication.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 | |
| 11 | #include "GDBRemoteCommunication.h" |
| 12 | |
| 13 | // C Includes |
Johnny Chen | a566355 | 2011-05-13 20:07:25 +0000 | [diff] [blame] | 14 | #include <limits.h> |
Stephen Wilson | a78867b | 2011-03-25 18:16:28 +0000 | [diff] [blame] | 15 | #include <string.h> |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 16 | #include <sys/stat.h> |
Stephen Wilson | a78867b | 2011-03-25 18:16:28 +0000 | [diff] [blame] | 17 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | // C++ Includes |
| 19 | // Other libraries and framework includes |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 20 | #include "lldb/Core/ConnectionFileDescriptor.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include "lldb/Core/Log.h" |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 22 | #include "lldb/Core/StreamFile.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | #include "lldb/Core/StreamString.h" |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 24 | #include "lldb/Host/FileSpec.h" |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 25 | #include "lldb/Host/FileSystem.h" |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 26 | #include "lldb/Host/Host.h" |
Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame^] | 27 | #include "lldb/Host/HostInfo.h" |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 28 | #include "lldb/Host/Socket.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | #include "lldb/Host/TimeValue.h" |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 30 | #include "lldb/Target/Process.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | |
| 32 | // Project includes |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | #include "ProcessGDBRemoteLog.h" |
| 34 | |
Todd Fiala | 015d818 | 2014-07-22 23:41:36 +0000 | [diff] [blame] | 35 | #if defined(__APPLE__) |
| 36 | # define DEBUGSERVER_BASENAME "debugserver" |
| 37 | #else |
| 38 | # define DEBUGSERVER_BASENAME "lldb-gdbserver" |
| 39 | #endif |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | using namespace lldb; |
| 42 | using namespace lldb_private; |
| 43 | |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 44 | GDBRemoteCommunication::History::History (uint32_t size) : |
| 45 | m_packets(), |
| 46 | m_curr_idx (0), |
| 47 | m_total_packet_count (0), |
| 48 | m_dumped_to_log (false) |
| 49 | { |
| 50 | m_packets.resize(size); |
| 51 | } |
| 52 | |
| 53 | GDBRemoteCommunication::History::~History () |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | void |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 58 | GDBRemoteCommunication::History::AddPacket (char packet_char, |
| 59 | PacketType type, |
| 60 | uint32_t bytes_transmitted) |
| 61 | { |
| 62 | const size_t size = m_packets.size(); |
| 63 | if (size > 0) |
| 64 | { |
| 65 | const uint32_t idx = GetNextIndex(); |
| 66 | m_packets[idx].packet.assign (1, packet_char); |
| 67 | m_packets[idx].type = type; |
| 68 | m_packets[idx].bytes_transmitted = bytes_transmitted; |
| 69 | m_packets[idx].packet_idx = m_total_packet_count; |
| 70 | m_packets[idx].tid = Host::GetCurrentThreadID(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | GDBRemoteCommunication::History::AddPacket (const std::string &src, |
| 76 | uint32_t src_len, |
| 77 | PacketType type, |
| 78 | uint32_t bytes_transmitted) |
| 79 | { |
| 80 | const size_t size = m_packets.size(); |
| 81 | if (size > 0) |
| 82 | { |
| 83 | const uint32_t idx = GetNextIndex(); |
| 84 | m_packets[idx].packet.assign (src, 0, src_len); |
| 85 | m_packets[idx].type = type; |
| 86 | m_packets[idx].bytes_transmitted = bytes_transmitted; |
| 87 | m_packets[idx].packet_idx = m_total_packet_count; |
| 88 | m_packets[idx].tid = Host::GetCurrentThreadID(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 93 | GDBRemoteCommunication::History::Dump (lldb_private::Stream &strm) const |
| 94 | { |
| 95 | const uint32_t size = GetNumPacketsInHistory (); |
| 96 | const uint32_t first_idx = GetFirstSavedPacketIndex (); |
| 97 | const uint32_t stop_idx = m_curr_idx + size; |
| 98 | for (uint32_t i = first_idx; i < stop_idx; ++i) |
| 99 | { |
| 100 | const uint32_t idx = NormalizeIndex (i); |
| 101 | const Entry &entry = m_packets[idx]; |
| 102 | if (entry.type == ePacketTypeInvalid || entry.packet.empty()) |
| 103 | break; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 104 | strm.Printf ("history[%u] tid=0x%4.4" PRIx64 " <%4u> %s packet: %s\n", |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 105 | entry.packet_idx, |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 106 | entry.tid, |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 107 | entry.bytes_transmitted, |
| 108 | (entry.type == ePacketTypeSend) ? "send" : "read", |
| 109 | entry.packet.c_str()); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void |
| 114 | GDBRemoteCommunication::History::Dump (lldb_private::Log *log) const |
| 115 | { |
| 116 | if (log && !m_dumped_to_log) |
| 117 | { |
| 118 | m_dumped_to_log = true; |
| 119 | const uint32_t size = GetNumPacketsInHistory (); |
| 120 | const uint32_t first_idx = GetFirstSavedPacketIndex (); |
| 121 | const uint32_t stop_idx = m_curr_idx + size; |
| 122 | for (uint32_t i = first_idx; i < stop_idx; ++i) |
| 123 | { |
| 124 | const uint32_t idx = NormalizeIndex (i); |
| 125 | const Entry &entry = m_packets[idx]; |
| 126 | if (entry.type == ePacketTypeInvalid || entry.packet.empty()) |
| 127 | break; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 128 | log->Printf ("history[%u] tid=0x%4.4" PRIx64 " <%4u> %s packet: %s", |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 129 | entry.packet_idx, |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 130 | entry.tid, |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 131 | entry.bytes_transmitted, |
| 132 | (entry.type == ePacketTypeSend) ? "send" : "read", |
| 133 | entry.packet.c_str()); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 138 | //---------------------------------------------------------------------- |
| 139 | // GDBRemoteCommunication constructor |
| 140 | //---------------------------------------------------------------------- |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 141 | GDBRemoteCommunication::GDBRemoteCommunication(const char *comm_name, |
| 142 | const char *listener_name, |
| 143 | bool is_platform) : |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 144 | Communication(comm_name), |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 145 | #ifdef LLDB_CONFIGURATION_DEBUG |
| 146 | m_packet_timeout (1000), |
| 147 | #else |
Greg Clayton | f3dd93c | 2011-06-17 03:31:01 +0000 | [diff] [blame] | 148 | m_packet_timeout (1), |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 149 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 150 | m_sequence_mutex (Mutex::eMutexTypeRecursive), |
Greg Clayton | 4dc7228 | 2011-01-20 07:53:45 +0000 | [diff] [blame] | 151 | m_public_is_running (false), |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 152 | m_private_is_running (false), |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 153 | m_history (512), |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 154 | m_send_acks (true), |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 155 | m_is_platform (is_platform), |
| 156 | m_listen_thread (LLDB_INVALID_HOST_THREAD), |
| 157 | m_listen_url () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 158 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | //---------------------------------------------------------------------- |
| 162 | // Destructor |
| 163 | //---------------------------------------------------------------------- |
| 164 | GDBRemoteCommunication::~GDBRemoteCommunication() |
| 165 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 166 | if (IsConnected()) |
| 167 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 168 | Disconnect(); |
| 169 | } |
| 170 | } |
| 171 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 172 | char |
| 173 | GDBRemoteCommunication::CalculcateChecksum (const char *payload, size_t payload_length) |
| 174 | { |
| 175 | int checksum = 0; |
| 176 | |
Ed Maste | a6b4c77 | 2013-08-20 14:12:58 +0000 | [diff] [blame] | 177 | for (size_t i = 0; i < payload_length; ++i) |
| 178 | checksum += payload[i]; |
| 179 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 180 | return checksum & 255; |
| 181 | } |
| 182 | |
| 183 | size_t |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 184 | GDBRemoteCommunication::SendAck () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 185 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 186 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 187 | ConnectionStatus status = eConnectionStatusSuccess; |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 188 | char ch = '+'; |
| 189 | const size_t bytes_written = Write (&ch, 1, status, NULL); |
| 190 | if (log) |
Greg Clayton | 4598907 | 2013-10-23 18:24:30 +0000 | [diff] [blame] | 191 | log->Printf ("<%4" PRIu64 "> send packet: %c", (uint64_t)bytes_written, ch); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 192 | m_history.AddPacket (ch, History::ePacketTypeSend, bytes_written); |
| 193 | return bytes_written; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | size_t |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 197 | GDBRemoteCommunication::SendNack () |
| 198 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 199 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 200 | ConnectionStatus status = eConnectionStatusSuccess; |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 201 | char ch = '-'; |
| 202 | const size_t bytes_written = Write (&ch, 1, status, NULL); |
| 203 | if (log) |
Greg Clayton | 4598907 | 2013-10-23 18:24:30 +0000 | [diff] [blame] | 204 | log->Printf("<%4" PRIu64 "> send packet: %c", (uint64_t)bytes_written, ch); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 205 | m_history.AddPacket (ch, History::ePacketTypeSend, bytes_written); |
| 206 | return bytes_written; |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 209 | GDBRemoteCommunication::PacketResult |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 210 | GDBRemoteCommunication::SendPacket (const char *payload, size_t payload_length) |
| 211 | { |
| 212 | Mutex::Locker locker(m_sequence_mutex); |
| 213 | return SendPacketNoLock (payload, payload_length); |
| 214 | } |
| 215 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 216 | GDBRemoteCommunication::PacketResult |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 217 | GDBRemoteCommunication::SendPacketNoLock (const char *payload, size_t payload_length) |
| 218 | { |
| 219 | if (IsConnected()) |
| 220 | { |
| 221 | StreamString packet(0, 4, eByteOrderBig); |
| 222 | |
| 223 | packet.PutChar('$'); |
| 224 | packet.Write (payload, payload_length); |
| 225 | packet.PutChar('#'); |
| 226 | packet.PutHex8(CalculcateChecksum (payload, payload_length)); |
| 227 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 228 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 229 | ConnectionStatus status = eConnectionStatusSuccess; |
| 230 | size_t bytes_written = Write (packet.GetData(), packet.GetSize(), status, NULL); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 231 | if (log) |
| 232 | { |
| 233 | // If logging was just enabled and we have history, then dump out what |
| 234 | // we have to the log so we get the historical context. The Dump() call that |
| 235 | // logs all of the packet will set a boolean so that we don't dump this more |
| 236 | // than once |
| 237 | if (!m_history.DidDumpToLog ()) |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 238 | m_history.Dump (log); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 239 | |
Greg Clayton | 4598907 | 2013-10-23 18:24:30 +0000 | [diff] [blame] | 240 | log->Printf("<%4" PRIu64 "> send packet: %.*s", (uint64_t)bytes_written, (int)packet.GetSize(), packet.GetData()); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | m_history.AddPacket (packet.GetString(), packet.GetSize(), History::ePacketTypeSend, bytes_written); |
| 244 | |
| 245 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 246 | if (bytes_written == packet.GetSize()) |
| 247 | { |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 248 | if (GetSendAcks ()) |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 249 | return GetAck (); |
| 250 | else |
| 251 | return PacketResult::Success; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 252 | } |
Johnny Chen | d0c40dd | 2010-09-14 22:10:43 +0000 | [diff] [blame] | 253 | else |
| 254 | { |
Greg Clayton | 6d09345 | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 255 | if (log) |
Greg Clayton | 5fe15d2 | 2011-05-20 03:15:54 +0000 | [diff] [blame] | 256 | log->Printf ("error: failed to send packet: %.*s", (int)packet.GetSize(), packet.GetData()); |
Johnny Chen | d0c40dd | 2010-09-14 22:10:43 +0000 | [diff] [blame] | 257 | } |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 258 | } |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 259 | return PacketResult::ErrorSendFailed; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 262 | GDBRemoteCommunication::PacketResult |
Greg Clayton | c574ede | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 263 | GDBRemoteCommunication::GetAck () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 264 | { |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 265 | StringExtractorGDBRemote packet; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 266 | PacketResult result = WaitForPacketWithTimeoutMicroSecondsNoLock (packet, GetPacketTimeoutInMicroSeconds ()); |
| 267 | if (result == PacketResult::Success) |
| 268 | { |
| 269 | if (packet.GetResponseType() == StringExtractorGDBRemote::ResponseType::eAck) |
| 270 | return PacketResult::Success; |
| 271 | else |
| 272 | return PacketResult::ErrorSendAck; |
| 273 | } |
| 274 | return result; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | bool |
Jim Ingham | 4ceb928 | 2012-06-08 22:50:40 +0000 | [diff] [blame] | 278 | GDBRemoteCommunication::GetSequenceMutex (Mutex::Locker& locker, const char *failure_message) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 279 | { |
Greg Clayton | d989673 | 2012-05-31 16:54:51 +0000 | [diff] [blame] | 280 | if (IsRunning()) |
Jim Ingham | 4ceb928 | 2012-06-08 22:50:40 +0000 | [diff] [blame] | 281 | return locker.TryLock (m_sequence_mutex, failure_message); |
Greg Clayton | d989673 | 2012-05-31 16:54:51 +0000 | [diff] [blame] | 282 | |
| 283 | locker.Lock (m_sequence_mutex); |
| 284 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 287 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 288 | bool |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 289 | GDBRemoteCommunication::WaitForNotRunningPrivate (const TimeValue *timeout_ptr) |
| 290 | { |
| 291 | return m_private_is_running.WaitForValueEqualTo (false, timeout_ptr, NULL); |
| 292 | } |
| 293 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 294 | GDBRemoteCommunication::PacketResult |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 295 | GDBRemoteCommunication::WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &packet, uint32_t timeout_usec) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 296 | { |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 297 | uint8_t buffer[8192]; |
| 298 | Error error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 299 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 300 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS | GDBR_LOG_VERBOSE)); |
Greg Clayton | 644247c | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 301 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 302 | // Check for a packet from our cache first without trying any reading... |
| 303 | if (CheckForPacket (NULL, 0, packet)) |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 304 | return PacketResult::Success; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 305 | |
Greg Clayton | 0c51ac3 | 2011-07-02 23:21:06 +0000 | [diff] [blame] | 306 | bool timed_out = false; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 307 | bool disconnected = false; |
Greg Clayton | 0c51ac3 | 2011-07-02 23:21:06 +0000 | [diff] [blame] | 308 | while (IsConnected() && !timed_out) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 309 | { |
Johnny Chen | 74549c8 | 2011-07-19 01:13:00 +0000 | [diff] [blame] | 310 | lldb::ConnectionStatus status = eConnectionStatusNoConnection; |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 311 | size_t bytes_read = Read (buffer, sizeof(buffer), timeout_usec, status, &error); |
Greg Clayton | 644247c | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 312 | |
| 313 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 314 | log->Printf ("%s: Read (buffer, (sizeof(buffer), timeout_usec = 0x%x, status = %s, error = %s) => bytes_read = %" PRIu64, |
Greg Clayton | 644247c | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 315 | __PRETTY_FUNCTION__, |
| 316 | timeout_usec, |
| 317 | Communication::ConnectionStatusAsCString (status), |
| 318 | error.AsCString(), |
Greg Clayton | 43e0af0 | 2012-09-18 18:04:04 +0000 | [diff] [blame] | 319 | (uint64_t)bytes_read); |
Greg Clayton | 644247c | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 320 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 321 | if (bytes_read > 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 322 | { |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 323 | if (CheckForPacket (buffer, bytes_read, packet)) |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 324 | return PacketResult::Success; |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 325 | } |
| 326 | else |
| 327 | { |
| 328 | switch (status) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 329 | { |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 330 | case eConnectionStatusTimedOut: |
Greg Clayton | f0066ad | 2014-05-02 00:45:31 +0000 | [diff] [blame] | 331 | case eConnectionStatusInterrupted: |
Greg Clayton | 0c51ac3 | 2011-07-02 23:21:06 +0000 | [diff] [blame] | 332 | timed_out = true; |
| 333 | break; |
| 334 | case eConnectionStatusSuccess: |
| 335 | //printf ("status = success but error = %s\n", error.AsCString("<invalid>")); |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 336 | break; |
| 337 | |
| 338 | case eConnectionStatusEndOfFile: |
| 339 | case eConnectionStatusNoConnection: |
| 340 | case eConnectionStatusLostConnection: |
| 341 | case eConnectionStatusError: |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 342 | disconnected = true; |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 343 | Disconnect(); |
| 344 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 345 | } |
| 346 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 347 | } |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 348 | packet.Clear (); |
| 349 | if (disconnected) |
| 350 | return PacketResult::ErrorDisconnected; |
| 351 | if (timed_out) |
| 352 | return PacketResult::ErrorReplyTimeout; |
| 353 | else |
| 354 | return PacketResult::ErrorReplyFailed; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 357 | bool |
| 358 | GDBRemoteCommunication::CheckForPacket (const uint8_t *src, size_t src_len, StringExtractorGDBRemote &packet) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 359 | { |
| 360 | // Put the packet data into the buffer in a thread safe fashion |
| 361 | Mutex::Locker locker(m_bytes_mutex); |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 362 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 363 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 364 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 365 | if (src && src_len > 0) |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 366 | { |
Greg Clayton | 0c51ac3 | 2011-07-02 23:21:06 +0000 | [diff] [blame] | 367 | if (log && log->GetVerbose()) |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 368 | { |
| 369 | StreamString s; |
Greg Clayton | 0c51ac3 | 2011-07-02 23:21:06 +0000 | [diff] [blame] | 370 | log->Printf ("GDBRemoteCommunication::%s adding %u bytes: %.*s", |
| 371 | __FUNCTION__, |
| 372 | (uint32_t)src_len, |
| 373 | (uint32_t)src_len, |
| 374 | src); |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 375 | } |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 376 | m_bytes.append ((const char *)src, src_len); |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 377 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 378 | |
| 379 | // Parse up the packets into gdb remote packets |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 380 | if (!m_bytes.empty()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 381 | { |
| 382 | // end_idx must be one past the last valid packet byte. Start |
| 383 | // it off with an invalid value that is the same as the current |
| 384 | // index. |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 385 | size_t content_start = 0; |
| 386 | size_t content_length = 0; |
| 387 | size_t total_length = 0; |
| 388 | size_t checksum_idx = std::string::npos; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 389 | |
| 390 | switch (m_bytes[0]) |
| 391 | { |
| 392 | case '+': // Look for ack |
| 393 | case '-': // Look for cancel |
| 394 | case '\x03': // ^C to halt target |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 395 | content_length = total_length = 1; // The command is one byte long... |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 396 | break; |
| 397 | |
| 398 | case '$': |
| 399 | // Look for a standard gdb packet? |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 400 | { |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 401 | size_t hash_pos = m_bytes.find('#'); |
| 402 | if (hash_pos != std::string::npos) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 403 | { |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 404 | if (hash_pos + 2 < m_bytes.size()) |
| 405 | { |
| 406 | checksum_idx = hash_pos + 1; |
| 407 | // Skip the dollar sign |
| 408 | content_start = 1; |
| 409 | // Don't include the # in the content or the $ in the content length |
| 410 | content_length = hash_pos - 1; |
| 411 | |
| 412 | total_length = hash_pos + 3; // Skip the # and the two hex checksum bytes |
| 413 | } |
| 414 | else |
| 415 | { |
| 416 | // Checksum bytes aren't all here yet |
| 417 | content_length = std::string::npos; |
| 418 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | break; |
| 422 | |
| 423 | default: |
Greg Clayton | f3dd93c | 2011-06-17 03:31:01 +0000 | [diff] [blame] | 424 | { |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 425 | // We have an unexpected byte and we need to flush all bad |
| 426 | // data that is in m_bytes, so we need to find the first |
| 427 | // byte that is a '+' (ACK), '-' (NACK), \x03 (CTRL+C interrupt), |
| 428 | // or '$' character (start of packet header) or of course, |
| 429 | // the end of the data in m_bytes... |
| 430 | const size_t bytes_len = m_bytes.size(); |
| 431 | bool done = false; |
| 432 | uint32_t idx; |
| 433 | for (idx = 1; !done && idx < bytes_len; ++idx) |
| 434 | { |
| 435 | switch (m_bytes[idx]) |
| 436 | { |
| 437 | case '+': |
| 438 | case '-': |
| 439 | case '\x03': |
| 440 | case '$': |
| 441 | done = true; |
| 442 | break; |
| 443 | |
| 444 | default: |
| 445 | break; |
| 446 | } |
| 447 | } |
Greg Clayton | f3dd93c | 2011-06-17 03:31:01 +0000 | [diff] [blame] | 448 | if (log) |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 449 | log->Printf ("GDBRemoteCommunication::%s tossing %u junk bytes: '%.*s'", |
| 450 | __FUNCTION__, idx, idx, m_bytes.c_str()); |
| 451 | m_bytes.erase(0, idx); |
Greg Clayton | f3dd93c | 2011-06-17 03:31:01 +0000 | [diff] [blame] | 452 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 453 | break; |
| 454 | } |
| 455 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 456 | if (content_length == std::string::npos) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 457 | { |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 458 | packet.Clear(); |
| 459 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 460 | } |
Greg Clayton | f3dd93c | 2011-06-17 03:31:01 +0000 | [diff] [blame] | 461 | else if (total_length > 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 462 | { |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 463 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 464 | // We have a valid packet... |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 465 | assert (content_length <= m_bytes.size()); |
| 466 | assert (total_length <= m_bytes.size()); |
| 467 | assert (content_length <= total_length); |
Greg Clayton | 06f09b5 | 2014-06-20 20:41:07 +0000 | [diff] [blame] | 468 | const size_t content_end = content_start + content_length; |
| 469 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 470 | bool success = true; |
| 471 | std::string &packet_str = packet.GetStringRef(); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 472 | |
| 473 | |
| 474 | if (log) |
| 475 | { |
| 476 | // If logging was just enabled and we have history, then dump out what |
| 477 | // we have to the log so we get the historical context. The Dump() call that |
| 478 | // logs all of the packet will set a boolean so that we don't dump this more |
| 479 | // than once |
| 480 | if (!m_history.DidDumpToLog ()) |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 481 | m_history.Dump (log); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 482 | |
Greg Clayton | 06f09b5 | 2014-06-20 20:41:07 +0000 | [diff] [blame] | 483 | bool binary = false; |
| 484 | // Only detect binary for packets that start with a '$' and have a '#CC' checksum |
| 485 | if (m_bytes[0] == '$' && total_length > 4) |
| 486 | { |
| 487 | for (size_t i=0; !binary && i<total_length; ++i) |
| 488 | { |
| 489 | if (isprint(m_bytes[i]) == 0) |
| 490 | binary = true; |
| 491 | } |
| 492 | } |
| 493 | if (binary) |
| 494 | { |
| 495 | StreamString strm; |
| 496 | // Packet header... |
| 497 | strm.Printf("<%4" PRIu64 "> read packet: %c", (uint64_t)total_length, m_bytes[0]); |
| 498 | for (size_t i=content_start; i<content_end; ++i) |
| 499 | { |
| 500 | // Remove binary escaped bytes when displaying the packet... |
| 501 | const char ch = m_bytes[i]; |
| 502 | if (ch == 0x7d) |
| 503 | { |
| 504 | // 0x7d is the escape character. The next character is to |
| 505 | // be XOR'd with 0x20. |
| 506 | const char escapee = m_bytes[++i] ^ 0x20; |
| 507 | strm.Printf("%2.2x", escapee); |
| 508 | } |
| 509 | else |
| 510 | { |
| 511 | strm.Printf("%2.2x", (uint8_t)ch); |
| 512 | } |
| 513 | } |
| 514 | // Packet footer... |
| 515 | strm.Printf("%c%c%c", m_bytes[total_length-3], m_bytes[total_length-2], m_bytes[total_length-1]); |
| 516 | log->PutCString(strm.GetString().c_str()); |
| 517 | } |
| 518 | else |
| 519 | { |
| 520 | log->Printf("<%4" PRIu64 "> read packet: %.*s", (uint64_t)total_length, (int)(total_length), m_bytes.c_str()); |
| 521 | } |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | m_history.AddPacket (m_bytes.c_str(), total_length, History::ePacketTypeRecv, total_length); |
| 525 | |
Hafiz Abid Qadeer | e5fd5e1 | 2013-08-28 15:10:37 +0000 | [diff] [blame] | 526 | // Clear packet_str in case there is some existing data in it. |
| 527 | packet_str.clear(); |
Hafiz Abid Qadeer | da96ef2 | 2013-08-28 10:31:52 +0000 | [diff] [blame] | 528 | // Copy the packet from m_bytes to packet_str expanding the |
| 529 | // run-length encoding in the process. |
| 530 | // Reserve enough byte for the most common case (no RLE used) |
| 531 | packet_str.reserve(m_bytes.length()); |
Greg Clayton | 06f09b5 | 2014-06-20 20:41:07 +0000 | [diff] [blame] | 532 | for (std::string::const_iterator c = m_bytes.begin() + content_start; c != m_bytes.begin() + content_end; ++c) |
Hafiz Abid Qadeer | da96ef2 | 2013-08-28 10:31:52 +0000 | [diff] [blame] | 533 | { |
| 534 | if (*c == '*') |
| 535 | { |
| 536 | // '*' indicates RLE. Next character will give us the |
| 537 | // repeat count and previous character is what is to be |
| 538 | // repeated. |
| 539 | char char_to_repeat = packet_str.back(); |
| 540 | // Number of time the previous character is repeated |
| 541 | int repeat_count = *++c + 3 - ' '; |
| 542 | // We have the char_to_repeat and repeat_count. Now push |
| 543 | // it in the packet. |
| 544 | for (int i = 0; i < repeat_count; ++i) |
| 545 | packet_str.push_back(char_to_repeat); |
| 546 | } |
Steve Pucci | 3c5d333 | 2014-02-24 19:07:29 +0000 | [diff] [blame] | 547 | else if (*c == 0x7d) |
| 548 | { |
| 549 | // 0x7d is the escape character. The next character is to |
| 550 | // be XOR'd with 0x20. |
| 551 | char escapee = *++c ^ 0x20; |
| 552 | packet_str.push_back(escapee); |
| 553 | } |
Hafiz Abid Qadeer | da96ef2 | 2013-08-28 10:31:52 +0000 | [diff] [blame] | 554 | else |
| 555 | { |
| 556 | packet_str.push_back(*c); |
| 557 | } |
| 558 | } |
| 559 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 560 | if (m_bytes[0] == '$') |
| 561 | { |
| 562 | assert (checksum_idx < m_bytes.size()); |
| 563 | if (::isxdigit (m_bytes[checksum_idx+0]) || |
| 564 | ::isxdigit (m_bytes[checksum_idx+1])) |
| 565 | { |
| 566 | if (GetSendAcks ()) |
| 567 | { |
| 568 | const char *packet_checksum_cstr = &m_bytes[checksum_idx]; |
| 569 | char packet_checksum = strtol (packet_checksum_cstr, NULL, 16); |
| 570 | char actual_checksum = CalculcateChecksum (packet_str.c_str(), packet_str.size()); |
| 571 | success = packet_checksum == actual_checksum; |
| 572 | if (!success) |
| 573 | { |
| 574 | if (log) |
| 575 | log->Printf ("error: checksum mismatch: %.*s expected 0x%2.2x, got 0x%2.2x", |
| 576 | (int)(total_length), |
| 577 | m_bytes.c_str(), |
| 578 | (uint8_t)packet_checksum, |
| 579 | (uint8_t)actual_checksum); |
| 580 | } |
| 581 | // Send the ack or nack if needed |
| 582 | if (!success) |
| 583 | SendNack(); |
| 584 | else |
| 585 | SendAck(); |
| 586 | } |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 587 | } |
| 588 | else |
| 589 | { |
| 590 | success = false; |
| 591 | if (log) |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 592 | log->Printf ("error: invalid checksum in packet: '%s'\n", m_bytes.c_str()); |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 593 | } |
| 594 | } |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 595 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 596 | m_bytes.erase(0, total_length); |
| 597 | packet.SetFilePos(0); |
| 598 | return success; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 599 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 600 | } |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 601 | packet.Clear(); |
| 602 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 605 | Error |
Greg Clayton | d629980 | 2013-12-06 17:46:35 +0000 | [diff] [blame] | 606 | GDBRemoteCommunication::StartListenThread (const char *hostname, uint16_t port) |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 607 | { |
| 608 | Error error; |
| 609 | if (IS_VALID_LLDB_HOST_THREAD(m_listen_thread)) |
| 610 | { |
| 611 | error.SetErrorString("listen thread already running"); |
| 612 | } |
| 613 | else |
| 614 | { |
| 615 | char listen_url[512]; |
| 616 | if (hostname && hostname[0]) |
Jean-Daniel Dupas | 3c6774a | 2014-02-08 20:29:40 +0000 | [diff] [blame] | 617 | snprintf(listen_url, sizeof(listen_url), "listen://%s:%i", hostname, port); |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 618 | else |
| 619 | snprintf(listen_url, sizeof(listen_url), "listen://%i", port); |
| 620 | m_listen_url = listen_url; |
| 621 | SetConnection(new ConnectionFileDescriptor()); |
| 622 | m_listen_thread = Host::ThreadCreate (listen_url, GDBRemoteCommunication::ListenThread, this, &error); |
| 623 | } |
| 624 | return error; |
| 625 | } |
| 626 | |
| 627 | bool |
| 628 | GDBRemoteCommunication::JoinListenThread () |
| 629 | { |
| 630 | if (IS_VALID_LLDB_HOST_THREAD(m_listen_thread)) |
| 631 | { |
| 632 | Host::ThreadJoin(m_listen_thread, NULL, NULL); |
| 633 | m_listen_thread = LLDB_INVALID_HOST_THREAD; |
| 634 | } |
| 635 | return true; |
| 636 | } |
| 637 | |
| 638 | lldb::thread_result_t |
| 639 | GDBRemoteCommunication::ListenThread (lldb::thread_arg_t arg) |
| 640 | { |
| 641 | GDBRemoteCommunication *comm = (GDBRemoteCommunication *)arg; |
| 642 | Error error; |
| 643 | ConnectionFileDescriptor *connection = (ConnectionFileDescriptor *)comm->GetConnection (); |
| 644 | |
| 645 | if (connection) |
| 646 | { |
| 647 | // Do the listen on another thread so we can continue on... |
| 648 | if (connection->Connect(comm->m_listen_url.c_str(), &error) != eConnectionStatusSuccess) |
| 649 | comm->SetConnection(NULL); |
| 650 | } |
| 651 | return NULL; |
| 652 | } |
| 653 | |
| 654 | Error |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 655 | GDBRemoteCommunication::StartDebugserverProcess (const char *hostname, |
| 656 | uint16_t in_port, |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 657 | lldb_private::ProcessLaunchInfo &launch_info, |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 658 | uint16_t &out_port) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 659 | { |
Todd Fiala | 015d818 | 2014-07-22 23:41:36 +0000 | [diff] [blame] | 660 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
| 661 | if (log) |
| 662 | log->Printf ("GDBRemoteCommunication::%s(hostname=%s, in_port=%" PRIu16 ", out_port=%" PRIu16, __FUNCTION__, hostname ? hostname : "<empty>", in_port, out_port); |
| 663 | |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 664 | out_port = in_port; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 665 | Error error; |
| 666 | // If we locate debugserver, keep that located version around |
| 667 | static FileSpec g_debugserver_file_spec; |
| 668 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 669 | char debugserver_path[PATH_MAX]; |
| 670 | FileSpec &debugserver_file_spec = launch_info.GetExecutableFile(); |
| 671 | |
| 672 | // Always check to see if we have an environment override for the path |
| 673 | // to the debugserver to use and use it if we do. |
| 674 | const char *env_debugserver_path = getenv("LLDB_DEBUGSERVER_PATH"); |
| 675 | if (env_debugserver_path) |
Todd Fiala | 015d818 | 2014-07-22 23:41:36 +0000 | [diff] [blame] | 676 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 677 | debugserver_file_spec.SetFile (env_debugserver_path, false); |
Todd Fiala | 015d818 | 2014-07-22 23:41:36 +0000 | [diff] [blame] | 678 | if (log) |
| 679 | log->Printf ("GDBRemoteCommunication::%s() gdb-remote stub exe path set from environment variable: %s", __FUNCTION__, env_debugserver_path); |
| 680 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 681 | else |
| 682 | debugserver_file_spec = g_debugserver_file_spec; |
| 683 | bool debugserver_exists = debugserver_file_spec.Exists(); |
| 684 | if (!debugserver_exists) |
| 685 | { |
| 686 | // The debugserver binary is in the LLDB.framework/Resources |
Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame^] | 687 | // directory. |
| 688 | if (HostInfo::GetLLDBPath(ePathTypeSupportExecutableDir, debugserver_file_spec)) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 689 | { |
| 690 | debugserver_file_spec.GetFilename().SetCString(DEBUGSERVER_BASENAME); |
| 691 | debugserver_exists = debugserver_file_spec.Exists(); |
| 692 | if (debugserver_exists) |
| 693 | { |
Todd Fiala | 015d818 | 2014-07-22 23:41:36 +0000 | [diff] [blame] | 694 | if (log) |
| 695 | log->Printf ("GDBRemoteCommunication::%s() found gdb-remote stub exe '%s'", __FUNCTION__, debugserver_file_spec.GetPath ().c_str ()); |
| 696 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 697 | g_debugserver_file_spec = debugserver_file_spec; |
| 698 | } |
| 699 | else |
| 700 | { |
Todd Fiala | 015d818 | 2014-07-22 23:41:36 +0000 | [diff] [blame] | 701 | if (log) |
| 702 | log->Printf ("GDBRemoteCommunication::%s() could not find gdb-remote stub exe '%s'", __FUNCTION__, debugserver_file_spec.GetPath ().c_str ()); |
| 703 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 704 | g_debugserver_file_spec.Clear(); |
| 705 | debugserver_file_spec.Clear(); |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | if (debugserver_exists) |
| 711 | { |
| 712 | debugserver_file_spec.GetPath (debugserver_path, sizeof(debugserver_path)); |
| 713 | |
| 714 | Args &debugserver_args = launch_info.GetArguments(); |
| 715 | debugserver_args.Clear(); |
| 716 | char arg_cstr[PATH_MAX]; |
| 717 | |
| 718 | // Start args with "debugserver /file/path -r --" |
| 719 | debugserver_args.AppendArgument(debugserver_path); |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 720 | |
| 721 | // If a host and port is supplied then use it |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 722 | char host_and_port[128]; |
| 723 | if (hostname) |
| 724 | { |
| 725 | snprintf (host_and_port, sizeof(host_and_port), "%s:%u", hostname, in_port); |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 726 | debugserver_args.AppendArgument(host_and_port); |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 727 | } |
| 728 | else |
| 729 | { |
| 730 | host_and_port[0] = '\0'; |
| 731 | } |
| 732 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 733 | // use native registers, not the GDB registers |
| 734 | debugserver_args.AppendArgument("--native-regs"); |
| 735 | // make debugserver run in its own session so signals generated by |
| 736 | // special terminal key sequences (^C) don't affect debugserver |
| 737 | debugserver_args.AppendArgument("--setsid"); |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 738 | |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 739 | char named_pipe_path[PATH_MAX]; |
Jason Molenda | 6e20554 | 2014-01-25 03:57:13 +0000 | [diff] [blame] | 740 | named_pipe_path[0] = '\0'; |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 741 | |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 742 | bool listen = false; |
| 743 | if (host_and_port[0]) |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 744 | { |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 745 | // Create a temporary file to get the stdout/stderr and redirect the |
| 746 | // output of the command into this file. We will later read this file |
| 747 | // if all goes well and fill the data into "command_output_ptr" |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 748 | |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 749 | if (in_port == 0) |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 750 | { |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 751 | // Binding to port zero, we need to figure out what port it ends up |
| 752 | // using using a named pipe... |
| 753 | FileSpec tmpdir_file_spec; |
Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame^] | 754 | if (HostInfo::GetLLDBPath(ePathTypeLLDBTempSystemDir, tmpdir_file_spec)) |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 755 | { |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 756 | tmpdir_file_spec.GetFilename().SetCString("debugserver-named-pipe.XXXXXX"); |
| 757 | strncpy(named_pipe_path, tmpdir_file_spec.GetPath().c_str(), sizeof(named_pipe_path)); |
| 758 | } |
| 759 | else |
| 760 | { |
| 761 | strncpy(named_pipe_path, "/tmp/debugserver-named-pipe.XXXXXX", sizeof(named_pipe_path)); |
| 762 | } |
| 763 | |
| 764 | if (::mktemp (named_pipe_path)) |
| 765 | { |
Hafiz Abid Qadeer | 6eff101 | 2014-03-12 10:45:23 +0000 | [diff] [blame] | 766 | #if defined(_WIN32) |
Deepak Panickal | b36da43 | 2014-01-13 14:55:15 +0000 | [diff] [blame] | 767 | if ( false ) |
| 768 | #else |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 769 | if (::mkfifo(named_pipe_path, 0600) == 0) |
Deepak Panickal | b36da43 | 2014-01-13 14:55:15 +0000 | [diff] [blame] | 770 | #endif |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 771 | { |
| 772 | debugserver_args.AppendArgument("--named-pipe"); |
| 773 | debugserver_args.AppendArgument(named_pipe_path); |
| 774 | } |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 775 | } |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 776 | } |
| 777 | else |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 778 | { |
| 779 | listen = true; |
| 780 | } |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 781 | } |
| 782 | else |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 783 | { |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 784 | // No host and port given, so lets listen on our end and make the debugserver |
| 785 | // connect to us.. |
Greg Clayton | 1681092 | 2014-02-27 19:38:18 +0000 | [diff] [blame] | 786 | error = StartListenThread ("127.0.0.1", 0); |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 787 | if (error.Fail()) |
| 788 | return error; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 789 | |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 790 | ConnectionFileDescriptor *connection = (ConnectionFileDescriptor *)GetConnection (); |
Greg Clayton | 1681092 | 2014-02-27 19:38:18 +0000 | [diff] [blame] | 791 | // Wait for 10 seconds to resolve the bound port |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 792 | out_port = connection->GetListeningPort(10); |
Greg Clayton | 1681092 | 2014-02-27 19:38:18 +0000 | [diff] [blame] | 793 | if (out_port > 0) |
| 794 | { |
| 795 | char port_cstr[32]; |
| 796 | snprintf(port_cstr, sizeof(port_cstr), "127.0.0.1:%i", out_port); |
| 797 | // Send the host and port down that debugserver and specify an option |
| 798 | // so that it connects back to the port we are listening to in this process |
| 799 | debugserver_args.AppendArgument("--reverse-connect"); |
| 800 | debugserver_args.AppendArgument(port_cstr); |
| 801 | } |
| 802 | else |
| 803 | { |
| 804 | error.SetErrorString ("failed to bind to port 0 on 127.0.0.1"); |
| 805 | return error; |
| 806 | } |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 807 | } |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 808 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 809 | const char *env_debugserver_log_file = getenv("LLDB_DEBUGSERVER_LOG_FILE"); |
| 810 | if (env_debugserver_log_file) |
| 811 | { |
| 812 | ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-file=%s", env_debugserver_log_file); |
| 813 | debugserver_args.AppendArgument(arg_cstr); |
| 814 | } |
| 815 | |
| 816 | const char *env_debugserver_log_flags = getenv("LLDB_DEBUGSERVER_LOG_FLAGS"); |
| 817 | if (env_debugserver_log_flags) |
| 818 | { |
| 819 | ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-flags=%s", env_debugserver_log_flags); |
| 820 | debugserver_args.AppendArgument(arg_cstr); |
| 821 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 822 | |
| 823 | // Close STDIN, STDOUT and STDERR. We might need to redirect them |
| 824 | // to "/dev/null" if we run into any problems. |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 825 | launch_info.AppendCloseFileAction (STDIN_FILENO); |
| 826 | launch_info.AppendCloseFileAction (STDOUT_FILENO); |
| 827 | launch_info.AppendCloseFileAction (STDERR_FILENO); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 828 | |
| 829 | error = Host::LaunchProcess(launch_info); |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 830 | |
Greg Clayton | 3121fde | 2014-02-28 20:47:08 +0000 | [diff] [blame] | 831 | if (error.Success() && launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 832 | { |
Greg Clayton | 3121fde | 2014-02-28 20:47:08 +0000 | [diff] [blame] | 833 | if (named_pipe_path[0]) |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 834 | { |
Greg Clayton | 3121fde | 2014-02-28 20:47:08 +0000 | [diff] [blame] | 835 | File name_pipe_file; |
| 836 | error = name_pipe_file.Open(named_pipe_path, File::eOpenOptionRead); |
| 837 | if (error.Success()) |
| 838 | { |
| 839 | char port_cstr[256]; |
| 840 | port_cstr[0] = '\0'; |
| 841 | size_t num_bytes = sizeof(port_cstr); |
| 842 | error = name_pipe_file.Read(port_cstr, num_bytes); |
| 843 | assert (error.Success()); |
| 844 | assert (num_bytes > 0 && port_cstr[num_bytes-1] == '\0'); |
| 845 | out_port = Args::StringToUInt32(port_cstr, 0); |
| 846 | name_pipe_file.Close(); |
| 847 | } |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 848 | FileSystem::Unlink(named_pipe_path); |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 849 | } |
Greg Clayton | 3121fde | 2014-02-28 20:47:08 +0000 | [diff] [blame] | 850 | else if (listen) |
| 851 | { |
| 852 | |
| 853 | } |
| 854 | else |
| 855 | { |
| 856 | // Make sure we actually connect with the debugserver... |
| 857 | JoinListenThread(); |
| 858 | } |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 859 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 860 | } |
| 861 | else |
| 862 | { |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 863 | error.SetErrorStringWithFormat ("unable to locate " DEBUGSERVER_BASENAME ); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 864 | } |
| 865 | return error; |
| 866 | } |
| 867 | |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 868 | void |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 869 | GDBRemoteCommunication::DumpHistory(Stream &strm) |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 870 | { |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 871 | m_history.Dump (strm); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 872 | } |