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