Files
llvm/lldb/source/Core/Communication.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

167 lines
5.3 KiB
C++
Raw Normal View History

//===-- Communication.cpp -------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/Core/Communication.h"
#include "lldb/Utility/Connection.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/Status.h"
#include "llvm/Support/Compiler.h"
#include <algorithm>
#include <cstring>
#include <memory>
#include <cerrno>
#include <cinttypes>
#include <cstdio>
using namespace lldb;
using namespace lldb_private;
Communication::Communication()
: m_connection_sp(), m_write_mutex(), m_close_on_eof(true) {
}
Communication::~Communication() {
Clear();
}
void Communication::Clear() {
Disconnect(nullptr);
}
ConnectionStatus Communication::Connect(const char *url, Status *error_ptr) {
Clear();
LLDB_LOG(GetLog(LLDBLog::Communication),
"{0} Communication::Connect (url = {1})", this, url);
lldb::ConnectionSP connection_sp(m_connection_sp);
if (connection_sp)
return connection_sp->Connect(url, error_ptr);
if (error_ptr)
error_ptr->SetErrorString("Invalid connection.");
return eConnectionStatusNoConnection;
}
ConnectionStatus Communication::Disconnect(Status *error_ptr) {
LLDB_LOG(GetLog(LLDBLog::Communication), "{0} Communication::Disconnect ()",
this);
lldb::ConnectionSP connection_sp(m_connection_sp);
if (connection_sp) {
ConnectionStatus status = connection_sp->Disconnect(error_ptr);
// We currently don't protect connection_sp with any mutex for multi-
// threaded environments. So lets not nuke our connection class without
// putting some multi-threaded protections in. We also probably don't want
// to pay for the overhead it might cause if every time we access the
// connection we have to take a lock.
//
// This unique pointer will cleanup after itself when this object goes
// away, so there is no need to currently have it destroy itself
// immediately upon disconnect.
// connection_sp.reset();
return status;
}
return eConnectionStatusNoConnection;
}
bool Communication::IsConnected() const {
lldb::ConnectionSP connection_sp(m_connection_sp);
return (connection_sp ? connection_sp->IsConnected() : false);
}
bool Communication::HasConnection() const {
return m_connection_sp.get() != nullptr;
}
size_t Communication::Read(void *dst, size_t dst_len,
const Timeout<std::micro> &timeout,
ConnectionStatus &status, Status *error_ptr) {
Log *log = GetLog(LLDBLog::Communication);
LLDB_LOG(
log,
"this = {0}, dst = {1}, dst_len = {2}, timeout = {3}, connection = {4}",
this, dst, dst_len, timeout, m_connection_sp.get());
return ReadFromConnection(dst, dst_len, timeout, status, error_ptr);
}
size_t Communication::Write(const void *src, size_t src_len,
ConnectionStatus &status, Status *error_ptr) {
lldb::ConnectionSP connection_sp(m_connection_sp);
Modified LLDB expressions to not have to JIT and run code just to see variable values or persistent expression variables. Now if an expression consists of a value that is a child of a variable, or of a persistent variable only, we will create a value object for it and make a ValueObjectConstResult from it to freeze the value (for program variables only, not persistent variables) and avoid running JITed code. For everything else we still parse up and JIT code and run it in the inferior. There was also a lot of clean up in the expression code. I made the ClangExpressionVariables be stored in collections of shared pointers instead of in collections of objects. This will help stop a lot of copy constructors on these large objects and also cleans up the code considerably. The persistent clang expression variables were moved over to the Target to ensure they persist across process executions. Added the ability for lldb_private::Target objects to evaluate expressions. We want to evaluate expressions at the target level in case we aren't running yet, or we have just completed running. We still want to be able to access the persistent expression variables between runs, and also evaluate constant expressions. Added extra logging to the dynamic loader plug-in for MacOSX. ModuleList objects can now dump their contents with the UUID, arch and full paths being logged with appropriate prefix values. Thread hardened the Communication class a bit by making the connection auto_ptr member into a shared pointer member and then making a local copy of the shared pointer in each method that uses it to make sure another thread can't nuke the connection object while it is being used by another thread. Added a new file to the lldb/test/load_unload test that causes the test a.out file to link to the libd.dylib file all the time. This will allow us to test using the DYLD_LIBRARY_PATH environment variable after moving libd.dylib somewhere else. llvm-svn: 121745
2010-12-14 02:59:59 +00:00
std::lock_guard<std::mutex> guard(m_write_mutex);
LLDB_LOG(GetLog(LLDBLog::Communication),
"{0} Communication::Write (src = {1}, src_len = {2}"
") connection = {3}",
this, src, (uint64_t)src_len, connection_sp.get());
if (connection_sp)
return connection_sp->Write(src, src_len, status, error_ptr);
if (error_ptr)
error_ptr->SetErrorString("Invalid connection.");
status = eConnectionStatusNoConnection;
return 0;
}
size_t Communication::WriteAll(const void *src, size_t src_len,
ConnectionStatus &status, Status *error_ptr) {
size_t total_written = 0;
do
total_written += Write(static_cast<const char *>(src) + total_written,
src_len - total_written, status, error_ptr);
while (status == eConnectionStatusSuccess && total_written < src_len);
return total_written;
}
size_t Communication::ReadFromConnection(void *dst, size_t dst_len,
const Timeout<std::micro> &timeout,
ConnectionStatus &status,
Status *error_ptr) {
lldb::ConnectionSP connection_sp(m_connection_sp);
if (connection_sp)
return connection_sp->Read(dst, dst_len, timeout, status, error_ptr);
if (error_ptr)
error_ptr->SetErrorString("Invalid connection.");
status = eConnectionStatusNoConnection;
return 0;
}
void Communication::SetConnection(std::unique_ptr<Connection> connection) {
Disconnect(nullptr);
m_connection_sp = std::move(connection);
}
std::string
Communication::ConnectionStatusAsString(lldb::ConnectionStatus status) {
switch (status) {
case eConnectionStatusSuccess:
return "success";
case eConnectionStatusError:
return "error";
case eConnectionStatusTimedOut:
return "timed out";
case eConnectionStatusNoConnection:
return "no connection";
case eConnectionStatusLostConnection:
return "lost connection";
case eConnectionStatusEndOfFile:
return "end of file";
case eConnectionStatusInterrupted:
return "interrupted";
}
return "@" + std::to_string(status);
}