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, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 59 | bool send_async); |
| 60 | |
| 61 | size_t |
| 62 | SendPacketAndWaitForResponse (const char *send_payload, |
| 63 | size_t send_length, |
| 64 | StringExtractorGDBRemote &response, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 65 | bool send_async); |
| 66 | |
| 67 | lldb::StateType |
| 68 | SendContinuePacketAndWaitForResponse (ProcessGDBRemote *process, |
| 69 | const char *packet_payload, |
| 70 | size_t packet_length, |
| 71 | StringExtractorGDBRemote &response); |
| 72 | |
| 73 | // Wait for a packet within 'nsec' seconds |
| 74 | size_t |
| 75 | WaitForPacket (StringExtractorGDBRemote &response, |
Greg Clayton | c97bfdb | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 76 | uint32_t sec); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 77 | |
| 78 | // Wait for a packet with an absolute timeout time. If 'timeout' is NULL |
| 79 | // wait indefinitely. |
| 80 | size_t |
| 81 | WaitForPacket (StringExtractorGDBRemote &response, |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 82 | const lldb_private::TimeValue* timeout); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 83 | |
| 84 | char |
Greg Clayton | c97bfdb | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 85 | GetAck (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 86 | |
| 87 | size_t |
Greg Clayton | a4881d0 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 88 | SendAck (); |
| 89 | |
| 90 | size_t |
| 91 | SendNack (); |
| 92 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 93 | |
| 94 | char |
| 95 | CalculcateChecksum (const char *payload, |
| 96 | size_t payload_length); |
| 97 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 98 | bool |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 99 | GetThreadSuffixSupported (); |
Greg Clayton | c71899e | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 100 | |
| 101 | bool |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 102 | SendAsyncSignal (int signo); |
| 103 | |
| 104 | bool |
Greg Clayton | 1a67946 | 2010-09-03 19:15:43 +0000 | [diff] [blame] | 105 | SendInterrupt (lldb_private::Mutex::Locker &locker, |
| 106 | uint32_t seconds_to_wait_for_stop, |
Greg Clayton | a4881d0 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 107 | bool &sent_interrupt, |
| 108 | bool &timed_out); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 109 | |
| 110 | bool |
| 111 | GetSequenceMutex(lldb_private::Mutex::Locker& locker); |
| 112 | |
| 113 | //------------------------------------------------------------------ |
| 114 | // Communication overrides |
| 115 | //------------------------------------------------------------------ |
| 116 | virtual void |
Caroline Tice | c4f55fe | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 117 | 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] | 118 | |
| 119 | |
| 120 | lldb::pid_t |
Greg Clayton | c97bfdb | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 121 | GetCurrentProcessID (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 122 | |
| 123 | bool |
Greg Clayton | c97bfdb | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 124 | GetLaunchSuccess (std::string &error_str); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 125 | |
| 126 | //------------------------------------------------------------------ |
| 127 | /// Sends a GDB remote protocol 'A' packet that delivers program |
| 128 | /// arguments to the remote server. |
| 129 | /// |
| 130 | /// @param[in] argv |
| 131 | /// A NULL terminated array of const C strings to use as the |
| 132 | /// arguments. |
| 133 | /// |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 134 | /// @return |
| 135 | /// Zero if the response was "OK", a positive value if the |
| 136 | /// the response was "Exx" where xx are two hex digits, or |
| 137 | /// -1 if the call is unsupported or any other unexpected |
| 138 | /// response was received. |
| 139 | //------------------------------------------------------------------ |
| 140 | int |
Greg Clayton | c97bfdb | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 141 | SendArgumentsPacket (char const *argv[]); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 142 | |
| 143 | //------------------------------------------------------------------ |
| 144 | /// Sends a "QEnvironment:NAME=VALUE" packet that will build up the |
| 145 | /// environment that will get used when launching an application |
| 146 | /// in conjunction with the 'A' packet. This function can be called |
| 147 | /// multiple times in a row in order to pass on the desired |
| 148 | /// environment that the inferior should be launched with. |
| 149 | /// |
| 150 | /// @param[in] name_equal_value |
Greg Clayton | 5d187e5 | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 151 | /// A NULL terminated C string that contains a single environment |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 152 | /// in the format "NAME=VALUE". |
| 153 | /// |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 154 | /// @return |
| 155 | /// Zero if the response was "OK", a positive value if the |
| 156 | /// the response was "Exx" where xx are two hex digits, or |
| 157 | /// -1 if the call is unsupported or any other unexpected |
| 158 | /// response was received. |
| 159 | //------------------------------------------------------------------ |
| 160 | int |
Greg Clayton | c97bfdb | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 161 | SendEnvironmentPacket (char const *name_equal_value); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 162 | |
| 163 | //------------------------------------------------------------------ |
| 164 | /// Sends a "vAttach:PID" where PID is in hex. |
| 165 | /// |
| 166 | /// @param[in] pid |
| 167 | /// A process ID for the remote gdb server to attach to. |
| 168 | /// |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 169 | /// @param[out] response |
| 170 | /// The response received from the gdb server. If the return |
| 171 | /// value is zero, \a response will contain a stop reply |
| 172 | /// packet. |
| 173 | /// |
| 174 | /// @return |
| 175 | /// Zero if the attach was successful, or an error indicating |
| 176 | /// an error code. |
| 177 | //------------------------------------------------------------------ |
| 178 | int |
| 179 | SendAttach (lldb::pid_t pid, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 180 | StringExtractorGDBRemote& response); |
| 181 | |
| 182 | |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 183 | //------------------------------------------------------------------ |
| 184 | /// Sets the path to use for stdin/out/err for a process |
| 185 | /// that will be launched with the 'A' packet. |
| 186 | /// |
| 187 | /// @param[in] path |
| 188 | /// The path to use for stdin/out/err |
| 189 | /// |
| 190 | /// @return |
| 191 | /// Zero if the for success, or an error code for failure. |
| 192 | //------------------------------------------------------------------ |
| 193 | int |
| 194 | SetSTDIN (char const *path); |
| 195 | int |
| 196 | SetSTDOUT (char const *path); |
| 197 | int |
| 198 | SetSTDERR (char const *path); |
| 199 | |
| 200 | //------------------------------------------------------------------ |
| 201 | /// Sets the disable ASLR flag to \a enable for a process that will |
| 202 | /// be launched with the 'A' packet. |
| 203 | /// |
| 204 | /// @param[in] enable |
| 205 | /// A boolean value indicating wether to disable ASLR or not. |
| 206 | /// |
| 207 | /// @return |
| 208 | /// Zero if the for success, or an error code for failure. |
| 209 | //------------------------------------------------------------------ |
| 210 | int |
| 211 | SetDisableASLR (bool enable); |
| 212 | |
| 213 | //------------------------------------------------------------------ |
| 214 | /// Sets the working directory to \a path for a process that will |
| 215 | /// be launched with the 'A' packet. |
| 216 | /// |
| 217 | /// @param[in] path |
| 218 | /// The path to a directory to use when launching our processs |
| 219 | /// |
| 220 | /// @return |
| 221 | /// Zero if the for success, or an error code for failure. |
| 222 | //------------------------------------------------------------------ |
| 223 | int |
| 224 | SetWorkingDir (char const *path); |
| 225 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 226 | lldb::addr_t |
Greg Clayton | c97bfdb | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 227 | AllocateMemory (size_t size, uint32_t permissions); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 228 | |
| 229 | bool |
Greg Clayton | c97bfdb | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 230 | DeallocateMemory (lldb::addr_t addr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 231 | |
| 232 | bool |
| 233 | IsRunning() const |
| 234 | { |
Greg Clayton | cecf348 | 2011-01-20 07:53:45 +0000 | [diff] [blame] | 235 | return m_public_is_running.GetValue(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 236 | } |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 237 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 238 | const lldb_private::ArchSpec & |
| 239 | GetHostArchitecture (); |
| 240 | |
| 241 | const lldb_private::ConstString & |
| 242 | GetOSString (); |
| 243 | |
| 244 | const lldb_private::ConstString & |
| 245 | GetVendorString(); |
| 246 | |
| 247 | lldb::ByteOrder |
| 248 | GetByteOrder (); |
| 249 | |
| 250 | uint32_t |
| 251 | GetAddressByteSize (); |
| 252 | |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 253 | bool |
| 254 | GetVContSupported (char flavor); |
| 255 | |
| 256 | void |
| 257 | ResetDiscoverableSettings(); |
| 258 | |
| 259 | bool |
| 260 | GetHostInfo (); |
| 261 | |
| 262 | bool |
| 263 | GetSendAcks (); |
| 264 | |
| 265 | bool |
| 266 | GetSupportsThreadSuffix (); |
| 267 | |
| 268 | bool |
| 269 | HasFullVContSupport () |
| 270 | { |
| 271 | return GetVContSupported ('A'); |
| 272 | } |
| 273 | |
| 274 | bool |
| 275 | HasAnyVContSupport () |
| 276 | { |
| 277 | return GetVContSupported ('a'); |
| 278 | } |
Greg Clayton | c97bfdb | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 279 | |
| 280 | uint32_t |
| 281 | SetPacketTimeout (uint32_t packet_timeout) |
| 282 | { |
| 283 | const uint32_t old_packet_timeout = m_packet_timeout; |
| 284 | m_packet_timeout = packet_timeout; |
| 285 | return old_packet_timeout; |
| 286 | } |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 287 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 288 | protected: |
| 289 | typedef std::list<std::string> packet_collection; |
| 290 | |
| 291 | size_t |
| 292 | SendPacketNoLock (const char *payload, |
| 293 | size_t payload_length); |
| 294 | |
| 295 | size_t |
| 296 | WaitForPacketNoLock (StringExtractorGDBRemote &response, |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 297 | const lldb_private::TimeValue* timeout_ptr); |
| 298 | |
| 299 | bool |
| 300 | WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 301 | |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 302 | bool |
| 303 | HostInfoIsValid () const |
| 304 | { |
| 305 | return m_supports_qHostInfo != lldb::eLazyBoolCalculate; |
| 306 | } |
| 307 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 308 | //------------------------------------------------------------------ |
| 309 | // Classes that inherit from GDBRemoteCommunication can see and modify these |
| 310 | //------------------------------------------------------------------ |
Greg Clayton | c97bfdb | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 311 | uint32_t m_packet_timeout; |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 312 | lldb::LazyBool m_supports_not_sending_acks; |
| 313 | lldb::LazyBool m_supports_thread_suffix; |
| 314 | lldb::LazyBool m_supports_qHostInfo; |
| 315 | lldb::LazyBool m_supports_vCont_all; |
| 316 | lldb::LazyBool m_supports_vCont_any; |
| 317 | lldb::LazyBool m_supports_vCont_c; |
| 318 | lldb::LazyBool m_supports_vCont_C; |
| 319 | lldb::LazyBool m_supports_vCont_s; |
| 320 | lldb::LazyBool m_supports_vCont_S; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 321 | lldb_private::Listener m_rx_packet_listener; |
| 322 | 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] | 323 | lldb_private::Predicate<bool> m_public_is_running; |
| 324 | lldb_private::Predicate<bool> m_private_is_running; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 325 | |
| 326 | // If we need to send a packet while the target is running, the m_async_XXX |
| 327 | // member variables take care of making this happen. |
| 328 | lldb_private::Mutex m_async_mutex; |
| 329 | lldb_private::Predicate<bool> m_async_packet_predicate; |
| 330 | std::string m_async_packet; |
| 331 | StringExtractorGDBRemote m_async_response; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 332 | int m_async_signal; // We were asked to deliver a signal to the inferior process. |
| 333 | |
| 334 | lldb_private::ArchSpec m_arch; // Results from the qHostInfo call |
| 335 | uint32_t m_cpusubtype; // Results from the qHostInfo call |
| 336 | lldb_private::ConstString m_os; // Results from the qHostInfo call |
| 337 | lldb_private::ConstString m_vendor; // Results from the qHostInfo call |
| 338 | lldb::ByteOrder m_byte_order; // Results from the qHostInfo call |
| 339 | uint32_t m_pointer_byte_size; // Results from the qHostInfo call |
| 340 | |
| 341 | |
| 342 | private: |
| 343 | //------------------------------------------------------------------ |
| 344 | // For GDBRemoteCommunication only |
| 345 | //------------------------------------------------------------------ |
| 346 | DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunication); |
| 347 | }; |
| 348 | |
| 349 | #endif // liblldb_GDBRemoteCommunication_h_ |