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