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