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