Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- GDBRemoteCommunication.h --------------------------------*- 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 | #ifndef liblldb_GDBRemoteCommunication_h_ |
| 11 | #define liblldb_GDBRemoteCommunication_h_ |
| 12 | |
| 13 | // C Includes |
| 14 | // C++ Includes |
| 15 | #include <list> |
| 16 | #include <string> |
| 17 | |
| 18 | // Other libraries and framework includes |
| 19 | // Project includes |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 20 | #include "lldb/lldb-public.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include "lldb/Core/Communication.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | #include "lldb/Core/Listener.h" |
| 23 | #include "lldb/Host/Mutex.h" |
| 24 | #include "lldb/Host/Predicate.h" |
Peter Collingbourne | ba23ca0 | 2011-06-18 23:52:14 +0000 | [diff] [blame] | 25 | #include "lldb/Host/TimeValue.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 26 | |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 27 | #include "Utility/StringExtractorGDBRemote.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | |
| 29 | class ProcessGDBRemote; |
| 30 | |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 31 | class GDBRemoteCommunication : public lldb_private::Communication |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | { |
| 33 | public: |
Greg Clayton | e521966 | 2010-12-03 06:02:24 +0000 | [diff] [blame] | 34 | enum |
| 35 | { |
| 36 | eBroadcastBitRunPacketSent = kLoUserBroadcastBit |
| 37 | }; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 38 | |
| 39 | enum class PacketResult |
| 40 | { |
| 41 | Success = 0, // Success |
| 42 | ErrorSendFailed, // Error sending the packet |
| 43 | ErrorSendAck, // Didn't get an ack back after sending a packet |
| 44 | ErrorReplyFailed, // Error getting the reply |
| 45 | ErrorReplyTimeout, // Timed out waiting for reply |
| 46 | ErrorReplyInvalid, // Got a reply but it wasn't valid for the packet that was sent |
| 47 | ErrorReplyAck, // Sending reply ack failed |
Steve Pucci | 5ae54ae | 2014-01-25 05:46:51 +0000 | [diff] [blame] | 48 | ErrorDisconnected, // We were disconnected |
| 49 | ErrorNoSequenceLock // We couldn't get the sequence lock for a multi-packet request |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 50 | }; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | //------------------------------------------------------------------ |
| 52 | // Constructors and Destructors |
| 53 | //------------------------------------------------------------------ |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 54 | GDBRemoteCommunication(const char *comm_name, |
| 55 | const char *listener_name, |
| 56 | bool is_platform); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 57 | |
| 58 | virtual |
| 59 | ~GDBRemoteCommunication(); |
| 60 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 61 | PacketResult |
Greg Clayton | c574ede | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 62 | GetAck (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 63 | |
| 64 | size_t |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 65 | SendAck (); |
| 66 | |
| 67 | size_t |
| 68 | SendNack (); |
| 69 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | char |
| 71 | CalculcateChecksum (const char *payload, |
| 72 | size_t payload_length); |
| 73 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 74 | bool |
Jim Ingham | 4ceb928 | 2012-06-08 22:50:40 +0000 | [diff] [blame] | 75 | GetSequenceMutex (lldb_private::Mutex::Locker& locker, const char *failure_message = NULL); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 76 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 77 | bool |
| 78 | CheckForPacket (const uint8_t *src, |
| 79 | size_t src_len, |
| 80 | StringExtractorGDBRemote &packet); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 81 | bool |
| 82 | IsRunning() const |
| 83 | { |
Greg Clayton | 4dc7228 | 2011-01-20 07:53:45 +0000 | [diff] [blame] | 84 | return m_public_is_running.GetValue(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 85 | } |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 86 | |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 87 | bool |
| 88 | GetSendAcks () |
| 89 | { |
| 90 | return m_send_acks; |
| 91 | } |
| 92 | |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 93 | //------------------------------------------------------------------ |
| 94 | // Client and server must implement these pure virtual functions |
| 95 | //------------------------------------------------------------------ |
| 96 | virtual bool |
| 97 | GetThreadSuffixSupported () = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 98 | |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 99 | //------------------------------------------------------------------ |
| 100 | // Set the global packet timeout. |
| 101 | // |
| 102 | // For clients, this is the timeout that gets used when sending |
| 103 | // packets and waiting for responses. For servers, this might not |
| 104 | // get used, and if it doesn't this should be moved to the |
| 105 | // GDBRemoteCommunicationClient. |
| 106 | //------------------------------------------------------------------ |
Greg Clayton | c574ede | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 107 | uint32_t |
| 108 | SetPacketTimeout (uint32_t packet_timeout) |
| 109 | { |
| 110 | const uint32_t old_packet_timeout = m_packet_timeout; |
| 111 | m_packet_timeout = packet_timeout; |
| 112 | return old_packet_timeout; |
| 113 | } |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 114 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 115 | uint32_t |
| 116 | GetPacketTimeoutInMicroSeconds () const |
| 117 | { |
Peter Collingbourne | ba23ca0 | 2011-06-18 23:52:14 +0000 | [diff] [blame] | 118 | return m_packet_timeout * lldb_private::TimeValue::MicroSecPerSec; |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 119 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 120 | //------------------------------------------------------------------ |
| 121 | // Start a debugserver instance on the current host using the |
| 122 | // supplied connection URL. |
| 123 | //------------------------------------------------------------------ |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 124 | lldb_private::Error |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 125 | StartDebugserverProcess (const char *hostname, |
| 126 | uint16_t in_port, // If set to zero, then out_port will contain the bound port on exit |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 127 | lldb_private::ProcessLaunchInfo &launch_info, |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 128 | uint16_t &out_port); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 129 | |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 130 | void |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 131 | DumpHistory(lldb_private::Stream &strm); |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 132 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 133 | protected: |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 134 | |
| 135 | class History |
| 136 | { |
| 137 | public: |
| 138 | enum PacketType |
| 139 | { |
| 140 | ePacketTypeInvalid = 0, |
| 141 | ePacketTypeSend, |
| 142 | ePacketTypeRecv |
| 143 | }; |
| 144 | |
| 145 | struct Entry |
| 146 | { |
| 147 | Entry() : |
| 148 | packet(), |
| 149 | type (ePacketTypeInvalid), |
| 150 | bytes_transmitted (0), |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 151 | packet_idx (0), |
| 152 | tid (LLDB_INVALID_THREAD_ID) |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 153 | { |
| 154 | } |
| 155 | |
| 156 | void |
| 157 | Clear () |
| 158 | { |
| 159 | packet.clear(); |
| 160 | type = ePacketTypeInvalid; |
| 161 | bytes_transmitted = 0; |
| 162 | packet_idx = 0; |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 163 | tid = LLDB_INVALID_THREAD_ID; |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 164 | } |
| 165 | std::string packet; |
| 166 | PacketType type; |
| 167 | uint32_t bytes_transmitted; |
| 168 | uint32_t packet_idx; |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 169 | lldb::tid_t tid; |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | History (uint32_t size); |
| 173 | |
| 174 | ~History (); |
| 175 | |
| 176 | // For single char packets for ack, nack and /x03 |
| 177 | void |
| 178 | AddPacket (char packet_char, |
| 179 | PacketType type, |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 180 | uint32_t bytes_transmitted); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 181 | void |
| 182 | AddPacket (const std::string &src, |
| 183 | uint32_t src_len, |
| 184 | PacketType type, |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 185 | uint32_t bytes_transmitted); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 186 | |
| 187 | void |
| 188 | Dump (lldb_private::Stream &strm) const; |
| 189 | |
| 190 | void |
| 191 | Dump (lldb_private::Log *log) const; |
| 192 | |
| 193 | bool |
| 194 | DidDumpToLog () const |
| 195 | { |
| 196 | return m_dumped_to_log; |
| 197 | } |
| 198 | |
| 199 | protected: |
| 200 | uint32_t |
| 201 | GetFirstSavedPacketIndex () const |
| 202 | { |
| 203 | if (m_total_packet_count < m_packets.size()) |
| 204 | return 0; |
| 205 | else |
| 206 | return m_curr_idx + 1; |
| 207 | } |
| 208 | |
| 209 | uint32_t |
| 210 | GetNumPacketsInHistory () const |
| 211 | { |
| 212 | if (m_total_packet_count < m_packets.size()) |
| 213 | return m_total_packet_count; |
| 214 | else |
| 215 | return (uint32_t)m_packets.size(); |
| 216 | } |
| 217 | |
| 218 | uint32_t |
| 219 | GetNextIndex() |
| 220 | { |
| 221 | ++m_total_packet_count; |
| 222 | const uint32_t idx = m_curr_idx; |
| 223 | m_curr_idx = NormalizeIndex(idx + 1); |
| 224 | return idx; |
| 225 | } |
| 226 | |
| 227 | uint32_t |
| 228 | NormalizeIndex (uint32_t i) const |
| 229 | { |
| 230 | return i % m_packets.size(); |
| 231 | } |
| 232 | |
| 233 | |
| 234 | std::vector<Entry> m_packets; |
| 235 | uint32_t m_curr_idx; |
| 236 | uint32_t m_total_packet_count; |
| 237 | mutable bool m_dumped_to_log; |
| 238 | }; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 239 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 240 | PacketResult |
Greg Clayton | 37a0a24 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 241 | SendPacket (const char *payload, |
| 242 | size_t payload_length); |
| 243 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 244 | PacketResult |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 245 | SendPacketNoLock (const char *payload, |
| 246 | size_t payload_length); |
| 247 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 248 | PacketResult |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 249 | WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &response, |
| 250 | uint32_t timeout_usec); |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 251 | |
| 252 | bool |
| 253 | WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 254 | |
| 255 | //------------------------------------------------------------------ |
| 256 | // Classes that inherit from GDBRemoteCommunication can see and modify these |
| 257 | //------------------------------------------------------------------ |
Greg Clayton | c574ede | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 258 | uint32_t m_packet_timeout; |
Greg Clayton | b09c538 | 2013-12-13 17:20:18 +0000 | [diff] [blame] | 259 | #ifdef ENABLE_MUTEX_ERROR_CHECKING |
Jim Ingham | 4ceb928 | 2012-06-08 22:50:40 +0000 | [diff] [blame] | 260 | lldb_private::TrackingMutex m_sequence_mutex; |
| 261 | #else |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 262 | lldb_private::Mutex m_sequence_mutex; // Restrict access to sending/receiving packets to a single thread at a time |
Jim Ingham | 4ceb928 | 2012-06-08 22:50:40 +0000 | [diff] [blame] | 263 | #endif |
Greg Clayton | 4dc7228 | 2011-01-20 07:53:45 +0000 | [diff] [blame] | 264 | lldb_private::Predicate<bool> m_public_is_running; |
| 265 | lldb_private::Predicate<bool> m_private_is_running; |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 266 | History m_history; |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 267 | bool m_send_acks; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 268 | bool m_is_platform; // Set to true if this class represents a platform, |
| 269 | // false if this class represents a debug session for |
| 270 | // a single process |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 271 | |
| 272 | |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 273 | lldb_private::Error |
Greg Clayton | 1681092 | 2014-02-27 19:38:18 +0000 | [diff] [blame] | 274 | StartListenThread (const char *hostname = "127.0.0.1", |
Greg Clayton | d629980 | 2013-12-06 17:46:35 +0000 | [diff] [blame] | 275 | uint16_t port = 0); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 276 | |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 277 | bool |
| 278 | JoinListenThread (); |
| 279 | |
| 280 | static lldb::thread_result_t |
| 281 | ListenThread (lldb::thread_arg_t arg); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 282 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 283 | private: |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 284 | |
| 285 | lldb::thread_t m_listen_thread; |
| 286 | std::string m_listen_url; |
| 287 | |
| 288 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 289 | //------------------------------------------------------------------ |
| 290 | // For GDBRemoteCommunication only |
| 291 | //------------------------------------------------------------------ |
| 292 | DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunication); |
| 293 | }; |
| 294 | |
| 295 | #endif // liblldb_GDBRemoteCommunication_h_ |