2010-06-09 00:52:24 +08:00
|
|
|
//===-- SBCommunication.cpp -------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lldb/API/SBCommunication.h"
|
|
|
|
#include "lldb/API/SBBroadcaster.h"
|
|
|
|
#include "lldb/Core/Communication.h"
|
|
|
|
#include "lldb/Core/ConnectionFileDescriptor.h"
|
2010-10-26 11:11:13 +08:00
|
|
|
#include "lldb/Core/Log.h"
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SBCommunication::SBCommunication() :
|
2010-06-23 09:19:29 +08:00
|
|
|
m_opaque (NULL),
|
|
|
|
m_opaque_owned (false)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-12-04 10:39:47 +08:00
|
|
|
SBCommunication::SBCommunication(const char * broadcaster_name) :
|
|
|
|
m_opaque (new Communication (broadcaster_name)),
|
2010-06-23 09:19:29 +08:00
|
|
|
m_opaque_owned (true)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2013-03-28 07:08:40 +08:00
|
|
|
Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
|
2010-10-26 11:11:13 +08:00
|
|
|
|
|
|
|
if (log)
|
2010-10-31 11:01:06 +08:00
|
|
|
log->Printf ("SBCommunication::SBCommunication (broadcaster_name=\"%s\") => "
|
|
|
|
"SBCommunication(%p)", broadcaster_name, m_opaque);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SBCommunication::~SBCommunication()
|
|
|
|
{
|
2010-06-23 09:19:29 +08:00
|
|
|
if (m_opaque && m_opaque_owned)
|
|
|
|
delete m_opaque;
|
|
|
|
m_opaque = NULL;
|
|
|
|
m_opaque_owned = false;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2011-06-21 06:30:48 +08:00
|
|
|
bool
|
|
|
|
SBCommunication::IsValid () const
|
|
|
|
{
|
|
|
|
return m_opaque != NULL;
|
|
|
|
}
|
|
|
|
|
2010-12-04 10:39:47 +08:00
|
|
|
bool
|
|
|
|
SBCommunication::GetCloseOnEOF ()
|
|
|
|
{
|
|
|
|
if (m_opaque)
|
|
|
|
return m_opaque->GetCloseOnEOF ();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SBCommunication::SetCloseOnEOF (bool b)
|
|
|
|
{
|
|
|
|
if (m_opaque)
|
|
|
|
m_opaque->SetCloseOnEOF (b);
|
|
|
|
}
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
ConnectionStatus
|
|
|
|
SBCommunication::Connect (const char *url)
|
|
|
|
{
|
2010-06-23 09:19:29 +08:00
|
|
|
if (m_opaque)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2010-06-23 09:19:29 +08:00
|
|
|
if (!m_opaque->HasConnection ())
|
|
|
|
m_opaque->SetConnection (new ConnectionFileDescriptor());
|
|
|
|
return m_opaque->Connect (url, NULL);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
return eConnectionStatusNoConnection;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionStatus
|
|
|
|
SBCommunication::AdoptFileDesriptor (int fd, bool owns_fd)
|
|
|
|
{
|
2013-03-28 07:08:40 +08:00
|
|
|
Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
|
2010-10-26 11:11:13 +08:00
|
|
|
|
2010-10-30 12:51:46 +08:00
|
|
|
ConnectionStatus status = eConnectionStatusNoConnection;
|
2010-06-23 09:19:29 +08:00
|
|
|
if (m_opaque)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2010-06-23 09:19:29 +08:00
|
|
|
if (m_opaque->HasConnection ())
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2010-06-23 09:19:29 +08:00
|
|
|
if (m_opaque->IsConnected())
|
2010-10-26 11:11:13 +08:00
|
|
|
m_opaque->Disconnect();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
2010-06-23 09:19:29 +08:00
|
|
|
m_opaque->SetConnection (new ConnectionFileDescriptor (fd, owns_fd));
|
|
|
|
if (m_opaque->IsConnected())
|
2010-10-30 12:51:46 +08:00
|
|
|
status = eConnectionStatusSuccess;
|
2010-06-09 00:52:24 +08:00
|
|
|
else
|
2010-10-30 12:51:46 +08:00
|
|
|
status = eConnectionStatusLostConnection;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
2010-10-26 11:11:13 +08:00
|
|
|
|
|
|
|
if (log)
|
2010-10-30 12:51:46 +08:00
|
|
|
log->Printf ("SBCommunication(%p)::AdoptFileDescriptor (fd=%d, ownd_fd=%i) => %s",
|
|
|
|
m_opaque, fd, owns_fd, Communication::ConnectionStatusAsCString (status));
|
2010-10-26 11:11:13 +08:00
|
|
|
|
2010-10-30 12:51:46 +08:00
|
|
|
return status;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ConnectionStatus
|
|
|
|
SBCommunication::Disconnect ()
|
|
|
|
{
|
2013-03-28 07:08:40 +08:00
|
|
|
Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
|
2010-10-26 11:11:13 +08:00
|
|
|
|
|
|
|
ConnectionStatus status= eConnectionStatusNoConnection;
|
2010-06-23 09:19:29 +08:00
|
|
|
if (m_opaque)
|
2010-10-26 11:11:13 +08:00
|
|
|
status = m_opaque->Disconnect ();
|
|
|
|
|
|
|
|
if (log)
|
2010-10-30 12:51:46 +08:00
|
|
|
log->Printf ("SBCommunication(%p)::Disconnect () => %s", m_opaque,
|
2010-10-27 07:49:36 +08:00
|
|
|
Communication::ConnectionStatusAsCString (status));
|
2010-10-26 11:11:13 +08:00
|
|
|
|
|
|
|
return status;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SBCommunication::IsConnected () const
|
|
|
|
{
|
2013-03-28 07:08:40 +08:00
|
|
|
Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
|
2010-10-30 12:51:46 +08:00
|
|
|
bool result = false;
|
2010-06-23 09:19:29 +08:00
|
|
|
if (m_opaque)
|
2010-10-30 12:51:46 +08:00
|
|
|
result = m_opaque->IsConnected ();
|
|
|
|
|
|
|
|
if (log)
|
|
|
|
log->Printf ("SBCommunication(%p)::IsConnected () => %i", m_opaque, result);
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
SBCommunication::Read (void *dst, size_t dst_len, uint32_t timeout_usec, ConnectionStatus &status)
|
|
|
|
{
|
2013-03-28 07:08:40 +08:00
|
|
|
Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
|
2010-10-30 12:51:46 +08:00
|
|
|
if (log)
|
2012-11-30 05:49:15 +08:00
|
|
|
log->Printf ("SBCommunication(%p)::Read (dst=%p, dst_len=%" PRIu64 ", timeout_usec=%u, &status)...",
|
2012-09-19 02:04:04 +08:00
|
|
|
m_opaque,
|
|
|
|
dst,
|
|
|
|
(uint64_t)dst_len,
|
|
|
|
timeout_usec);
|
2010-10-30 12:51:46 +08:00
|
|
|
size_t bytes_read = 0;
|
2010-06-23 09:19:29 +08:00
|
|
|
if (m_opaque)
|
2010-10-30 12:51:46 +08:00
|
|
|
bytes_read = m_opaque->Read (dst, dst_len, timeout_usec, status, NULL);
|
|
|
|
else
|
|
|
|
status = eConnectionStatusNoConnection;
|
|
|
|
|
|
|
|
if (log)
|
2012-11-30 05:49:15 +08:00
|
|
|
log->Printf ("SBCommunication(%p)::Read (dst=%p, dst_len=%" PRIu64 ", timeout_usec=%u, &status=%s) => %" PRIu64,
|
2012-09-19 02:04:04 +08:00
|
|
|
m_opaque,
|
|
|
|
dst,
|
|
|
|
(uint64_t)dst_len,
|
|
|
|
timeout_usec,
|
|
|
|
Communication::ConnectionStatusAsCString (status),
|
|
|
|
(uint64_t)bytes_read);
|
2010-10-30 12:51:46 +08:00
|
|
|
return bytes_read;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t
|
|
|
|
SBCommunication::Write (const void *src, size_t src_len, ConnectionStatus &status)
|
|
|
|
{
|
2010-10-30 12:51:46 +08:00
|
|
|
size_t bytes_written = 0;
|
2010-06-23 09:19:29 +08:00
|
|
|
if (m_opaque)
|
2010-10-30 12:51:46 +08:00
|
|
|
bytes_written = m_opaque->Write (src, src_len, status, NULL);
|
|
|
|
else
|
|
|
|
status = eConnectionStatusNoConnection;
|
|
|
|
|
2013-03-28 07:08:40 +08:00
|
|
|
Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
|
2010-10-30 12:51:46 +08:00
|
|
|
if (log)
|
2012-11-30 05:49:15 +08:00
|
|
|
log->Printf ("SBCommunication(%p)::Write (src=%p, src_len=%" PRIu64 ", &status=%s) => %" PRIu64,
|
2012-09-19 02:04:04 +08:00
|
|
|
m_opaque, src, (uint64_t)src_len, Communication::ConnectionStatusAsCString (status), (uint64_t)bytes_written);
|
2010-10-30 12:51:46 +08:00
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SBCommunication::ReadThreadStart ()
|
|
|
|
{
|
2013-03-28 07:08:40 +08:00
|
|
|
Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
|
2010-10-26 11:11:13 +08:00
|
|
|
|
|
|
|
bool success = false;
|
2010-06-23 09:19:29 +08:00
|
|
|
if (m_opaque)
|
2010-10-26 11:11:13 +08:00
|
|
|
success = m_opaque->StartReadThread ();
|
|
|
|
|
2010-10-30 05:48:37 +08:00
|
|
|
log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
|
2010-10-26 11:11:13 +08:00
|
|
|
if (log)
|
2010-10-30 12:51:46 +08:00
|
|
|
log->Printf ("SBCommunication(%p)::ReadThreadStart () => %i", m_opaque, success);
|
2010-10-26 11:11:13 +08:00
|
|
|
|
|
|
|
return success;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
SBCommunication::ReadThreadStop ()
|
|
|
|
{
|
2013-03-28 07:08:40 +08:00
|
|
|
Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
|
2010-10-30 12:51:46 +08:00
|
|
|
if (log)
|
|
|
|
log->Printf ("SBCommunication(%p)::ReadThreadStop ()...", m_opaque);
|
2010-10-26 11:11:13 +08:00
|
|
|
|
|
|
|
bool success = false;
|
2010-06-23 09:19:29 +08:00
|
|
|
if (m_opaque)
|
2010-10-26 11:11:13 +08:00
|
|
|
success = m_opaque->StopReadThread ();
|
|
|
|
|
|
|
|
if (log)
|
2010-10-30 12:51:46 +08:00
|
|
|
log->Printf ("SBCommunication(%p)::ReadThreadStop () => %i", m_opaque, success);
|
2010-10-26 11:11:13 +08:00
|
|
|
|
|
|
|
return success;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SBCommunication::ReadThreadIsRunning ()
|
|
|
|
{
|
2010-10-30 12:51:46 +08:00
|
|
|
bool result = false;
|
2010-06-23 09:19:29 +08:00
|
|
|
if (m_opaque)
|
2010-10-30 12:51:46 +08:00
|
|
|
result = m_opaque->ReadThreadIsRunning ();
|
2013-03-28 07:08:40 +08:00
|
|
|
Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
|
2010-10-30 12:51:46 +08:00
|
|
|
if (log)
|
|
|
|
log->Printf ("SBCommunication(%p)::ReadThreadIsRunning () => %i", m_opaque, result);
|
|
|
|
return result;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SBCommunication::SetReadThreadBytesReceivedCallback
|
|
|
|
(
|
|
|
|
ReadThreadBytesReceived callback,
|
|
|
|
void *callback_baton
|
|
|
|
)
|
|
|
|
{
|
2013-03-28 07:08:40 +08:00
|
|
|
Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
|
2010-10-26 11:11:13 +08:00
|
|
|
|
2010-10-30 12:51:46 +08:00
|
|
|
bool result = false;
|
2010-06-23 09:19:29 +08:00
|
|
|
if (m_opaque)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2010-06-23 09:19:29 +08:00
|
|
|
m_opaque->SetReadThreadBytesReceivedCallback (callback, callback_baton);
|
2010-10-30 12:51:46 +08:00
|
|
|
result = true;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
2010-10-26 11:11:13 +08:00
|
|
|
|
|
|
|
if (log)
|
2010-10-30 12:51:46 +08:00
|
|
|
log->Printf ("SBCommunication(%p)::SetReadThreadBytesReceivedCallback (callback=%p, baton=%p) => %i",
|
|
|
|
m_opaque, callback, callback_baton, result);
|
2010-10-26 11:11:13 +08:00
|
|
|
|
2010-10-30 12:51:46 +08:00
|
|
|
return result;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SBBroadcaster
|
|
|
|
SBCommunication::GetBroadcaster ()
|
|
|
|
{
|
2010-06-23 09:19:29 +08:00
|
|
|
SBBroadcaster broadcaster (m_opaque, false);
|
2010-10-30 12:51:46 +08:00
|
|
|
|
2013-03-28 07:08:40 +08:00
|
|
|
Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
|
2010-10-30 12:51:46 +08:00
|
|
|
|
|
|
|
if (log)
|
|
|
|
log->Printf ("SBCommunication(%p)::GetBroadcaster () => SBBroadcaster (%p)",
|
|
|
|
m_opaque, broadcaster.get());
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
return broadcaster;
|
|
|
|
}
|
|
|
|
|
2012-02-16 14:50:00 +08:00
|
|
|
const char *
|
|
|
|
SBCommunication::GetBroadcasterClass ()
|
|
|
|
{
|
|
|
|
return Communication::GetStaticBroadcasterClass().AsCString();
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
//void
|
|
|
|
//SBCommunication::CreateIfNeeded ()
|
|
|
|
//{
|
2010-06-23 09:19:29 +08:00
|
|
|
// if (m_opaque == NULL)
|
2010-06-09 00:52:24 +08:00
|
|
|
// {
|
|
|
|
// static uint32_t g_broadcaster_num;
|
|
|
|
// char broadcaster_name[256];
|
|
|
|
// ::snprintf (name, broadcaster_name, "%p SBCommunication", this);
|
2010-06-23 09:19:29 +08:00
|
|
|
// m_opaque = new Communication (broadcaster_name);
|
|
|
|
// m_opaque_owned = true;
|
2010-06-09 00:52:24 +08:00
|
|
|
// }
|
2010-06-23 09:19:29 +08:00
|
|
|
// assert (m_opaque);
|
2010-06-09 00:52:24 +08:00
|
|
|
//}
|
|
|
|
//
|
|
|
|
//
|