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