Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- SBCommunication.cpp -------------------------------------*- 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 | #include "lldb/API/SBCommunication.h" |
| 11 | #include "lldb/API/SBBroadcaster.h" |
| 12 | #include "lldb/Core/Communication.h" |
| 13 | #include "lldb/Core/ConnectionFileDescriptor.h" |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Log.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace lldb; |
| 17 | using namespace lldb_private; |
| 18 | |
| 19 | |
| 20 | |
| 21 | SBCommunication::SBCommunication() : |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 22 | m_opaque (NULL), |
| 23 | m_opaque_owned (false) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 24 | { |
| 25 | } |
| 26 | |
| 27 | SBCommunication::SBCommunication(const char * broadcaster_name) : |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 28 | m_opaque (new Communication (broadcaster_name)), |
| 29 | m_opaque_owned (true) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | { |
Caroline Tice | 61ba7ec | 2010-10-26 23:49:36 +0000 | [diff] [blame] | 31 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API); |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 32 | |
| 33 | if (log) |
Greg Clayton | 49ce682 | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 34 | log->Printf ("SBCommunication::SBCommunication (broadcaster_name=\"%s\") => " |
| 35 | "SBCommunication(%p)", broadcaster_name, m_opaque); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | SBCommunication::~SBCommunication() |
| 39 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 40 | if (m_opaque && m_opaque_owned) |
| 41 | delete m_opaque; |
| 42 | m_opaque = NULL; |
| 43 | m_opaque_owned = false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | ConnectionStatus |
| 47 | SBCommunication::CheckIfBytesAvailable () |
| 48 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 49 | if (m_opaque) |
| 50 | return m_opaque->BytesAvailable (0, NULL); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | return eConnectionStatusNoConnection; |
| 52 | } |
| 53 | |
| 54 | ConnectionStatus |
| 55 | SBCommunication::WaitForBytesAvailableInfinite () |
| 56 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 57 | if (m_opaque) |
| 58 | return m_opaque->BytesAvailable (UINT32_MAX, NULL); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 59 | return eConnectionStatusNoConnection; |
| 60 | } |
| 61 | |
| 62 | ConnectionStatus |
| 63 | SBCommunication::WaitForBytesAvailableWithTimeout (uint32_t timeout_usec) |
| 64 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 65 | if (m_opaque) |
| 66 | return m_opaque->BytesAvailable (timeout_usec, NULL); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 67 | return eConnectionStatusNoConnection; |
| 68 | } |
| 69 | |
| 70 | ConnectionStatus |
| 71 | SBCommunication::Connect (const char *url) |
| 72 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 73 | if (m_opaque) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 74 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 75 | if (!m_opaque->HasConnection ()) |
| 76 | m_opaque->SetConnection (new ConnectionFileDescriptor()); |
| 77 | return m_opaque->Connect (url, NULL); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 78 | } |
| 79 | return eConnectionStatusNoConnection; |
| 80 | } |
| 81 | |
| 82 | ConnectionStatus |
| 83 | SBCommunication::AdoptFileDesriptor (int fd, bool owns_fd) |
| 84 | { |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 85 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API); |
| 86 | |
Greg Clayton | a66ba46 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 87 | ConnectionStatus status = eConnectionStatusNoConnection; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 88 | if (m_opaque) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 89 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 90 | if (m_opaque->HasConnection ()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 91 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 92 | if (m_opaque->IsConnected()) |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 93 | m_opaque->Disconnect(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 94 | } |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 95 | m_opaque->SetConnection (new ConnectionFileDescriptor (fd, owns_fd)); |
| 96 | if (m_opaque->IsConnected()) |
Greg Clayton | a66ba46 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 97 | status = eConnectionStatusSuccess; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 98 | else |
Greg Clayton | a66ba46 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 99 | status = eConnectionStatusLostConnection; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 100 | } |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 101 | |
| 102 | if (log) |
Greg Clayton | a66ba46 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 103 | log->Printf ("SBCommunication(%p)::AdoptFileDescriptor (fd=%d, ownd_fd=%i) => %s", |
| 104 | m_opaque, fd, owns_fd, Communication::ConnectionStatusAsCString (status)); |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 105 | |
Greg Clayton | a66ba46 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 106 | return status; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | |
| 110 | ConnectionStatus |
| 111 | SBCommunication::Disconnect () |
| 112 | { |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 113 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API); |
| 114 | |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 115 | ConnectionStatus status= eConnectionStatusNoConnection; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 116 | if (m_opaque) |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 117 | status = m_opaque->Disconnect (); |
| 118 | |
| 119 | if (log) |
Greg Clayton | a66ba46 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 120 | log->Printf ("SBCommunication(%p)::Disconnect () => %s", m_opaque, |
Caroline Tice | 61ba7ec | 2010-10-26 23:49:36 +0000 | [diff] [blame] | 121 | Communication::ConnectionStatusAsCString (status)); |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 122 | |
| 123 | return status; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | bool |
| 127 | SBCommunication::IsConnected () const |
| 128 | { |
Greg Clayton | a66ba46 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 129 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API); |
| 130 | bool result = false; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 131 | if (m_opaque) |
Greg Clayton | a66ba46 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 132 | result = m_opaque->IsConnected (); |
| 133 | |
| 134 | if (log) |
| 135 | log->Printf ("SBCommunication(%p)::IsConnected () => %i", m_opaque, result); |
| 136 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 137 | return false; |
| 138 | } |
| 139 | |
| 140 | size_t |
| 141 | SBCommunication::Read (void *dst, size_t dst_len, uint32_t timeout_usec, ConnectionStatus &status) |
| 142 | { |
Greg Clayton | a66ba46 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 143 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API); |
| 144 | if (log) |
| 145 | log->Printf ("SBCommunication(%p)::Read (dst=%p, dst_len=%zu, timeout_usec=%u, &status)...", |
| 146 | m_opaque, dst, dst_len, timeout_usec); |
| 147 | size_t bytes_read = 0; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 148 | if (m_opaque) |
Greg Clayton | a66ba46 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 149 | bytes_read = m_opaque->Read (dst, dst_len, timeout_usec, status, NULL); |
| 150 | else |
| 151 | status = eConnectionStatusNoConnection; |
| 152 | |
| 153 | if (log) |
| 154 | log->Printf ("SBCommunication(%p)::Read (dst=%p, dst_len=%zu, timeout_usec=%u, &status=%s) => %zu", |
| 155 | m_opaque, dst, dst_len, timeout_usec, Communication::ConnectionStatusAsCString (status), |
| 156 | bytes_read); |
| 157 | return bytes_read; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | |
| 161 | size_t |
| 162 | SBCommunication::Write (const void *src, size_t src_len, ConnectionStatus &status) |
| 163 | { |
Greg Clayton | a66ba46 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 164 | size_t bytes_written = 0; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 165 | if (m_opaque) |
Greg Clayton | a66ba46 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 166 | bytes_written = m_opaque->Write (src, src_len, status, NULL); |
| 167 | else |
| 168 | status = eConnectionStatusNoConnection; |
| 169 | |
| 170 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API); |
| 171 | if (log) |
| 172 | log->Printf ("SBCommunication(%p)::Write (src=%p, src_len=%zu, &status=%s) => %zu", |
| 173 | m_opaque, src, src_len, Communication::ConnectionStatusAsCString (status), bytes_written); |
| 174 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | bool |
| 179 | SBCommunication::ReadThreadStart () |
| 180 | { |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 181 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API); |
| 182 | |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 183 | bool success = false; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 184 | if (m_opaque) |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 185 | success = m_opaque->StartReadThread (); |
| 186 | |
Caroline Tice | 926060e | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 187 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API); |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 188 | if (log) |
Greg Clayton | a66ba46 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 189 | log->Printf ("SBCommunication(%p)::ReadThreadStart () => %i", m_opaque, success); |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 190 | |
| 191 | return success; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | |
| 195 | bool |
| 196 | SBCommunication::ReadThreadStop () |
| 197 | { |
Greg Clayton | a66ba46 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 198 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API); |
| 199 | if (log) |
| 200 | log->Printf ("SBCommunication(%p)::ReadThreadStop ()...", m_opaque); |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 201 | |
| 202 | bool success = false; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 203 | if (m_opaque) |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 204 | success = m_opaque->StopReadThread (); |
| 205 | |
| 206 | if (log) |
Greg Clayton | a66ba46 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 207 | log->Printf ("SBCommunication(%p)::ReadThreadStop () => %i", m_opaque, success); |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 208 | |
| 209 | return success; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | bool |
| 213 | SBCommunication::ReadThreadIsRunning () |
| 214 | { |
Greg Clayton | a66ba46 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 215 | bool result = false; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 216 | if (m_opaque) |
Greg Clayton | a66ba46 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 217 | result = m_opaque->ReadThreadIsRunning (); |
| 218 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API); |
| 219 | if (log) |
| 220 | log->Printf ("SBCommunication(%p)::ReadThreadIsRunning () => %i", m_opaque, result); |
| 221 | return result; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | bool |
| 225 | SBCommunication::SetReadThreadBytesReceivedCallback |
| 226 | ( |
| 227 | ReadThreadBytesReceived callback, |
| 228 | void *callback_baton |
| 229 | ) |
| 230 | { |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 231 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API); |
| 232 | |
Greg Clayton | a66ba46 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 233 | bool result = false; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 234 | if (m_opaque) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 235 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 236 | m_opaque->SetReadThreadBytesReceivedCallback (callback, callback_baton); |
Greg Clayton | a66ba46 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 237 | result = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 238 | } |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 239 | |
| 240 | if (log) |
Greg Clayton | a66ba46 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 241 | log->Printf ("SBCommunication(%p)::SetReadThreadBytesReceivedCallback (callback=%p, baton=%p) => %i", |
| 242 | m_opaque, callback, callback_baton, result); |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 243 | |
Greg Clayton | a66ba46 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 244 | return result; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | SBBroadcaster |
| 248 | SBCommunication::GetBroadcaster () |
| 249 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 250 | SBBroadcaster broadcaster (m_opaque, false); |
Greg Clayton | a66ba46 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 251 | |
| 252 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API); |
| 253 | |
| 254 | if (log) |
| 255 | log->Printf ("SBCommunication(%p)::GetBroadcaster () => SBBroadcaster (%p)", |
| 256 | m_opaque, broadcaster.get()); |
| 257 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 258 | return broadcaster; |
| 259 | } |
| 260 | |
| 261 | |
| 262 | // |
| 263 | //void |
| 264 | //SBCommunication::CreateIfNeeded () |
| 265 | //{ |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 266 | // if (m_opaque == NULL) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 267 | // { |
| 268 | // static uint32_t g_broadcaster_num; |
| 269 | // char broadcaster_name[256]; |
| 270 | // ::snprintf (name, broadcaster_name, "%p SBCommunication", this); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 271 | // m_opaque = new Communication (broadcaster_name); |
| 272 | // m_opaque_owned = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 273 | // } |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 274 | // assert (m_opaque); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 275 | //} |
| 276 | // |
| 277 | // |