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