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