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