Chris Lattner | 24943d2 | 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 |
| 20 | #include "lldb/lldb-private.h" |
| 21 | #include "lldb/Core/ArchSpec.h" |
| 22 | #include "lldb/Core/Communication.h" |
| 23 | #include "lldb/Core/ConstString.h" |
| 24 | #include "lldb/Core/Error.h" |
| 25 | #include "lldb/Core/Listener.h" |
| 26 | #include "lldb/Host/Mutex.h" |
| 27 | #include "lldb/Host/Predicate.h" |
| 28 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 29 | #include "Utility/StringExtractorGDBRemote.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | |
| 31 | class ProcessGDBRemote; |
| 32 | |
| 33 | class GDBRemoteCommunication : |
| 34 | public lldb_private::Communication |
| 35 | { |
| 36 | public: |
Greg Clayton | b749a26 | 2010-12-03 06:02:24 +0000 | [diff] [blame] | 37 | enum |
| 38 | { |
| 39 | eBroadcastBitRunPacketSent = kLoUserBroadcastBit |
| 40 | }; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | //------------------------------------------------------------------ |
| 42 | // Constructors and Destructors |
| 43 | //------------------------------------------------------------------ |
| 44 | GDBRemoteCommunication(); |
| 45 | |
| 46 | virtual |
| 47 | ~GDBRemoteCommunication(); |
| 48 | |
| 49 | size_t |
| 50 | SendPacket (const char *payload); |
| 51 | |
| 52 | size_t |
| 53 | SendPacket (const char *payload, |
| 54 | size_t payload_length); |
| 55 | |
| 56 | size_t |
| 57 | SendPacketAndWaitForResponse (const char *send_payload, |
| 58 | StringExtractorGDBRemote &response, |
| 59 | uint32_t timeout_seconds, |
| 60 | bool send_async); |
| 61 | |
| 62 | size_t |
| 63 | SendPacketAndWaitForResponse (const char *send_payload, |
| 64 | size_t send_length, |
| 65 | StringExtractorGDBRemote &response, |
| 66 | uint32_t timeout_seconds, |
| 67 | bool send_async); |
| 68 | |
| 69 | lldb::StateType |
| 70 | SendContinuePacketAndWaitForResponse (ProcessGDBRemote *process, |
| 71 | const char *packet_payload, |
| 72 | size_t packet_length, |
| 73 | StringExtractorGDBRemote &response); |
| 74 | |
| 75 | // Wait for a packet within 'nsec' seconds |
| 76 | size_t |
| 77 | WaitForPacket (StringExtractorGDBRemote &response, |
| 78 | uint32_t nsec); |
| 79 | |
| 80 | // Wait for a packet with an absolute timeout time. If 'timeout' is NULL |
| 81 | // wait indefinitely. |
| 82 | size_t |
| 83 | WaitForPacket (StringExtractorGDBRemote &response, |
| 84 | lldb_private::TimeValue* timeout); |
| 85 | |
| 86 | char |
| 87 | GetAck (uint32_t timeout_seconds); |
| 88 | |
| 89 | size_t |
| 90 | SendAck (char ack_char); |
| 91 | |
| 92 | char |
| 93 | CalculcateChecksum (const char *payload, |
| 94 | size_t payload_length); |
| 95 | |
| 96 | void |
| 97 | SetAckMode (bool enabled) |
| 98 | { |
| 99 | m_send_acks = enabled; |
| 100 | } |
| 101 | |
| 102 | bool |
Greg Clayton | c71899e | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 103 | GetThreadSuffixSupported () const |
| 104 | { |
| 105 | return m_thread_suffix_supported; |
| 106 | } |
| 107 | |
| 108 | void |
| 109 | SetThreadSuffixSupported (bool enabled) |
| 110 | { |
| 111 | m_thread_suffix_supported = enabled; |
| 112 | } |
| 113 | |
| 114 | bool |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 115 | SendAsyncSignal (int signo); |
| 116 | |
| 117 | bool |
Greg Clayton | 1a67946 | 2010-09-03 19:15:43 +0000 | [diff] [blame] | 118 | SendInterrupt (lldb_private::Mutex::Locker &locker, |
| 119 | uint32_t seconds_to_wait_for_stop, |
| 120 | bool *timed_out = NULL); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 121 | |
| 122 | bool |
| 123 | GetSequenceMutex(lldb_private::Mutex::Locker& locker); |
| 124 | |
| 125 | //------------------------------------------------------------------ |
| 126 | // Communication overrides |
| 127 | //------------------------------------------------------------------ |
| 128 | virtual void |
Caroline Tice | c4f55fe | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 129 | AppendBytesToCache (const uint8_t *src, size_t src_len, bool broadcast, lldb::ConnectionStatus status); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 130 | |
| 131 | |
| 132 | lldb::pid_t |
| 133 | GetCurrentProcessID (uint32_t timeout_seconds); |
| 134 | |
| 135 | bool |
| 136 | GetLaunchSuccess (uint32_t timeout_seconds, std::string &error_str); |
| 137 | |
| 138 | //------------------------------------------------------------------ |
| 139 | /// Sends a GDB remote protocol 'A' packet that delivers program |
| 140 | /// arguments to the remote server. |
| 141 | /// |
| 142 | /// @param[in] argv |
| 143 | /// A NULL terminated array of const C strings to use as the |
| 144 | /// arguments. |
| 145 | /// |
| 146 | /// @param[in] timeout_seconds |
| 147 | /// The number of seconds to wait for a response from the remote |
| 148 | /// server. |
| 149 | /// |
| 150 | /// @return |
| 151 | /// Zero if the response was "OK", a positive value if the |
| 152 | /// the response was "Exx" where xx are two hex digits, or |
| 153 | /// -1 if the call is unsupported or any other unexpected |
| 154 | /// response was received. |
| 155 | //------------------------------------------------------------------ |
| 156 | int |
| 157 | SendArgumentsPacket (char const *argv[], uint32_t timeout_seconds); |
| 158 | |
| 159 | //------------------------------------------------------------------ |
| 160 | /// Sends a "QEnvironment:NAME=VALUE" packet that will build up the |
| 161 | /// environment that will get used when launching an application |
| 162 | /// in conjunction with the 'A' packet. This function can be called |
| 163 | /// multiple times in a row in order to pass on the desired |
| 164 | /// environment that the inferior should be launched with. |
| 165 | /// |
| 166 | /// @param[in] name_equal_value |
Greg Clayton | 5d187e5 | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 167 | /// A NULL terminated C string that contains a single environment |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 168 | /// in the format "NAME=VALUE". |
| 169 | /// |
| 170 | /// @param[in] timeout_seconds |
| 171 | /// The number of seconds to wait for a response from the remote |
| 172 | /// server. |
| 173 | /// |
| 174 | /// @return |
| 175 | /// Zero if the response was "OK", a positive value if the |
| 176 | /// the response was "Exx" where xx are two hex digits, or |
| 177 | /// -1 if the call is unsupported or any other unexpected |
| 178 | /// response was received. |
| 179 | //------------------------------------------------------------------ |
| 180 | int |
| 181 | SendEnvironmentPacket (char const *name_equal_value, |
| 182 | uint32_t timeout_seconds); |
| 183 | |
| 184 | //------------------------------------------------------------------ |
| 185 | /// Sends a "vAttach:PID" where PID is in hex. |
| 186 | /// |
| 187 | /// @param[in] pid |
| 188 | /// A process ID for the remote gdb server to attach to. |
| 189 | /// |
| 190 | /// @param[in] timeout_seconds |
| 191 | /// The number of seconds to wait for a response from the remote |
| 192 | /// server. |
| 193 | /// |
| 194 | /// @param[out] response |
| 195 | /// The response received from the gdb server. If the return |
| 196 | /// value is zero, \a response will contain a stop reply |
| 197 | /// packet. |
| 198 | /// |
| 199 | /// @return |
| 200 | /// Zero if the attach was successful, or an error indicating |
| 201 | /// an error code. |
| 202 | //------------------------------------------------------------------ |
| 203 | int |
| 204 | SendAttach (lldb::pid_t pid, |
| 205 | uint32_t timeout_seconds, |
| 206 | StringExtractorGDBRemote& response); |
| 207 | |
| 208 | |
| 209 | lldb::addr_t |
| 210 | AllocateMemory (size_t size, uint32_t permissions, uint32_t timeout_seconds); |
| 211 | |
| 212 | bool |
| 213 | DeallocateMemory (lldb::addr_t addr, uint32_t timeout_seconds); |
| 214 | |
| 215 | bool |
| 216 | IsRunning() const |
| 217 | { |
Greg Clayton | cecf348 | 2011-01-20 07:53:45 +0000 | [diff] [blame^] | 218 | return m_public_is_running.GetValue(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | bool |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 222 | GetHostInfo (uint32_t timeout_seconds); |
| 223 | |
| 224 | bool |
| 225 | HostInfoIsValid () const |
| 226 | { |
| 227 | return m_pointer_byte_size != 0; |
| 228 | } |
| 229 | |
| 230 | const lldb_private::ArchSpec & |
| 231 | GetHostArchitecture (); |
| 232 | |
| 233 | const lldb_private::ConstString & |
| 234 | GetOSString (); |
| 235 | |
| 236 | const lldb_private::ConstString & |
| 237 | GetVendorString(); |
| 238 | |
| 239 | lldb::ByteOrder |
| 240 | GetByteOrder (); |
| 241 | |
| 242 | uint32_t |
| 243 | GetAddressByteSize (); |
| 244 | |
| 245 | protected: |
| 246 | typedef std::list<std::string> packet_collection; |
| 247 | |
| 248 | size_t |
| 249 | SendPacketNoLock (const char *payload, |
| 250 | size_t payload_length); |
| 251 | |
| 252 | size_t |
| 253 | WaitForPacketNoLock (StringExtractorGDBRemote &response, |
| 254 | lldb_private::TimeValue* timeout_time_ptr); |
| 255 | |
| 256 | //------------------------------------------------------------------ |
| 257 | // Classes that inherit from GDBRemoteCommunication can see and modify these |
| 258 | //------------------------------------------------------------------ |
Greg Clayton | c71899e | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 259 | bool m_send_acks:1, |
| 260 | m_thread_suffix_supported:1; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 261 | lldb_private::Listener m_rx_packet_listener; |
| 262 | lldb_private::Mutex m_sequence_mutex; // Restrict access to sending/receiving packets to a single thread at a time |
Greg Clayton | cecf348 | 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; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 265 | |
| 266 | // If we need to send a packet while the target is running, the m_async_XXX |
| 267 | // member variables take care of making this happen. |
| 268 | lldb_private::Mutex m_async_mutex; |
| 269 | lldb_private::Predicate<bool> m_async_packet_predicate; |
| 270 | std::string m_async_packet; |
| 271 | StringExtractorGDBRemote m_async_response; |
| 272 | uint32_t m_async_timeout; |
| 273 | int m_async_signal; // We were asked to deliver a signal to the inferior process. |
| 274 | |
| 275 | lldb_private::ArchSpec m_arch; // Results from the qHostInfo call |
| 276 | uint32_t m_cpusubtype; // Results from the qHostInfo call |
| 277 | lldb_private::ConstString m_os; // Results from the qHostInfo call |
| 278 | lldb_private::ConstString m_vendor; // Results from the qHostInfo call |
| 279 | lldb::ByteOrder m_byte_order; // Results from the qHostInfo call |
| 280 | uint32_t m_pointer_byte_size; // Results from the qHostInfo call |
| 281 | |
| 282 | |
| 283 | private: |
| 284 | //------------------------------------------------------------------ |
| 285 | // For GDBRemoteCommunication only |
| 286 | //------------------------------------------------------------------ |
| 287 | DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunication); |
| 288 | }; |
| 289 | |
| 290 | #endif // liblldb_GDBRemoteCommunication_h_ |