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