Skip to content
Merged
14 changes: 12 additions & 2 deletions lldb/include/lldb/Host/HostInfoBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,19 @@ class HostInfoBase {
/// member of the FileSpec is filled in.
static FileSpec GetSystemPluginDir();

/// Returns the directory containing the users home (e.g. `~/`). Only the
/// directory member of the FileSpec is filled in.
static FileSpec GetUserHomeDir();

/// Returns the directory containing the users lldb home (e.g. `~/.lldb/`).
/// Only the directory member of the FileSpec is filled in.
static FileSpec GetUserLLDBDir();

/// Returns the directory containing the user plugins. Only the directory
/// member of the FileSpec is filled in.
static FileSpec GetUserPluginDir();

/// Returns the proces temporary directory. This directory will be cleaned up
/// Returns the process temporary directory. This directory will be cleaned up
/// when this process exits. Only the directory member of the FileSpec is
/// filled in.
static FileSpec GetProcessTempDir();
Expand Down Expand Up @@ -167,11 +175,13 @@ class HostInfoBase {
static bool ComputeTempFileBaseDirectory(FileSpec &file_spec);
static bool ComputeHeaderDirectory(FileSpec &file_spec);
static bool ComputeSystemPluginsDirectory(FileSpec &file_spec);
static bool ComputeUserHomeDirectory(FileSpec &file_spec);
static bool ComputeUserLLDBHomeDirectory(FileSpec &file_spec);
static bool ComputeUserPluginsDirectory(FileSpec &file_spec);

static void ComputeHostArchitectureSupport(ArchSpec &arch_32,
ArchSpec &arch_64);
};
}
} // namespace lldb_private

#endif
3 changes: 2 additions & 1 deletion lldb/include/lldb/Host/macosx/HostInfoMacOSX.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class HostInfoMacOSX : public HostInfoPosix {
static std::string FindComponentInPath(llvm::StringRef path,
llvm::StringRef component);
};
}

} // namespace lldb_private

#endif
9 changes: 9 additions & 0 deletions lldb/include/lldb/Protocol/MCP/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ class MCPTransport
LogCallback m_log_callback;
};

/// Information about this instance of lldb's MCP server for lldb-mcp to use to
/// coordinate connecting an lldb-mcp client.
struct ServerInfo {
std::string connection_uri;
lldb::pid_t pid;
};
llvm::json::Value toJSON(const ServerInfo &);
bool fromJSON(const llvm::json::Value &, ServerInfo &, llvm::json::Path);

class Server : public MCPTransport::MessageHandler {
public:
Server(std::string name, std::string version,
Expand Down
10 changes: 1 addition & 9 deletions lldb/source/API/SBHostOS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,7 @@ SBFileSpec SBHostOS::GetLLDBPath(lldb::PathType path_type) {

SBFileSpec SBHostOS::GetUserHomeDirectory() {
LLDB_INSTRUMENT();

FileSpec homedir;
FileSystem::Instance().GetHomeDirectory(homedir);
FileSystem::Instance().Resolve(homedir);

SBFileSpec sb_fspec;
sb_fspec.SetFileSpec(homedir);

return sb_fspec;
return HostInfo::GetUserHomeDir();
}

lldb::thread_t SBHostOS::ThreadCreate(const char *name,
Expand Down
15 changes: 7 additions & 8 deletions lldb/source/Commands/CommandObjectProtocolServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "lldb/Utility/UriParser.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/FormatAdapters.h"
#include <string>

using namespace llvm;
using namespace lldb;
Expand All @@ -28,7 +29,7 @@ class CommandObjectProtocolServerStart : public CommandObjectParsed {
CommandObjectProtocolServerStart(CommandInterpreter &interpreter)
: CommandObjectParsed(interpreter, "protocol-server start",
"start protocol server",
"protocol-server start <protocol> <connection>") {
"protocol-server start <protocol> [<connection>]") {
AddSimpleArgumentList(lldb::eArgTypeProtocol, eArgRepeatPlain);
AddSimpleArgumentList(lldb::eArgTypeConnectURL, eArgRepeatPlain);
}
Expand All @@ -51,15 +52,13 @@ class CommandObjectProtocolServerStart : public CommandObjectParsed {
return;
}

if (args.GetArgumentCount() < 2) {
result.AppendError("no connection specified");
return;
}
llvm::StringRef connection_uri = args.GetArgumentAtIndex(1);
std::string connection_uri = "listen://[localhost]:0";
if (args.GetArgumentCount() >= 2)
connection_uri = args.GetArgumentAtIndex(1);

const char *connection_error =
"unsupported connection specifier, expected 'accept:///path' or "
"'listen://[host]:port', got '{0}'.";
"unsupported connection specifier, expected 'accept:///path' "
"or 'listen://[host]:port', got '{0}'.";
auto uri = lldb_private::URI::Parse(connection_uri);
if (!uri) {
result.AppendErrorWithFormatv(connection_error, connection_uri);
Expand Down
15 changes: 6 additions & 9 deletions lldb/source/Host/common/Editline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
#include <iomanip>
#include <optional>

#include "lldb/Host/ConnectionFileDescriptor.h"
#include "lldb/Host/Editline.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/Host.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/Host/StreamFile.h"
#include "lldb/Utility/AnsiTerminal.h"
#include "lldb/Utility/CompletionRequest.h"
Expand Down Expand Up @@ -219,20 +217,19 @@ class EditlineHistory {
const char *GetHistoryFilePath() {
// Compute the history path lazily.
if (m_path.empty() && m_history && !m_prefix.empty()) {
llvm::SmallString<128> lldb_history_file;
FileSystem::Instance().GetHomeDirectory(lldb_history_file);
llvm::sys::path::append(lldb_history_file, ".lldb");
FileSpec lldb_dir = HostInfo::GetUserLLDBDir();

// LLDB stores its history in ~/.lldb/. If for some reason this directory
// isn't writable or cannot be created, history won't be available.
if (!llvm::sys::fs::create_directory(lldb_history_file)) {
if (!llvm::sys::fs::create_directory(lldb_dir.GetPath())) {
#if LLDB_EDITLINE_USE_WCHAR
std::string filename = m_prefix + "-widehistory";
#else
std::string filename = m_prefix + "-history";
#endif
llvm::sys::path::append(lldb_history_file, filename);
m_path = std::string(lldb_history_file.str());
FileSpec lldb_history_file =
lldb_dir.CopyByAppendingPathComponent(filename);
m_path = lldb_history_file.GetPath();
}
}

Expand Down
38 changes: 38 additions & 0 deletions lldb/source/Host/common/HostInfoBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ struct HostInfoBaseFields {
FileSpec m_lldb_clang_resource_dir;
llvm::once_flag m_lldb_system_plugin_dir_once;
FileSpec m_lldb_system_plugin_dir;
llvm::once_flag m_lldb_user_home_dir_once;
FileSpec m_lldb_user_home_dir;
llvm::once_flag m_lldb_user_lldb_dir_once;
FileSpec m_lldb_user_lldb_dir;
llvm::once_flag m_lldb_user_plugin_dir_once;
FileSpec m_lldb_user_plugin_dir;
llvm::once_flag m_lldb_process_tmp_dir_once;
Expand Down Expand Up @@ -161,6 +165,26 @@ FileSpec HostInfoBase::GetSystemPluginDir() {
return g_fields->m_lldb_system_plugin_dir;
}

FileSpec HostInfoBase::GetUserHomeDir() {
llvm::call_once(g_fields->m_lldb_user_home_dir_once, []() {
if (!HostInfo::ComputeUserHomeDirectory(g_fields->m_lldb_user_home_dir))
g_fields->m_lldb_user_home_dir = FileSpec();
LLDB_LOG(GetLog(LLDBLog::Host), "user home dir -> `{0}`",
g_fields->m_lldb_user_home_dir);
});
return g_fields->m_lldb_user_home_dir;
}

FileSpec HostInfoBase::GetUserLLDBDir() {
llvm::call_once(g_fields->m_lldb_user_lldb_dir_once, []() {
if (!HostInfo::ComputeUserLLDBHomeDirectory(g_fields->m_lldb_user_lldb_dir))
g_fields->m_lldb_user_lldb_dir = FileSpec();
LLDB_LOG(GetLog(LLDBLog::Host), "user lldb home dir -> `{0}`",
g_fields->m_lldb_user_lldb_dir);
});
return g_fields->m_lldb_user_lldb_dir;
}

FileSpec HostInfoBase::GetUserPluginDir() {
llvm::call_once(g_fields->m_lldb_user_plugin_dir_once, []() {
if (!HostInfo::ComputeUserPluginsDirectory(
Expand Down Expand Up @@ -316,6 +340,20 @@ bool HostInfoBase::ComputeSystemPluginsDirectory(FileSpec &file_spec) {
return false;
}

bool HostInfoBase::ComputeUserHomeDirectory(FileSpec &file_spec) {
FileSpec temp_file("~");
FileSystem::Instance().Resolve(temp_file);
file_spec.SetDirectory(temp_file.GetPathAsConstString());
return true;
}

bool HostInfoBase::ComputeUserLLDBHomeDirectory(FileSpec &file_spec) {
FileSpec home_dir_spec = GetUserHomeDir();
home_dir_spec.AppendPathComponent(".lldb");
file_spec.SetDirectory(home_dir_spec.GetPathAsConstString());
return true;
}

bool HostInfoBase::ComputeUserPluginsDirectory(FileSpec &file_spec) {
// TODO(zturner): Figure out how to compute the user plugins directory for
// all platforms.
Expand Down
12 changes: 6 additions & 6 deletions lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#include <Foundation/Foundation.h>
#include <mach-o/dyld.h>
#if defined(MAC_OS_X_VERSION_MIN_REQUIRED) && \
MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_VERSION_12_0
MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_VERSION_12_0
#if __has_include(<mach-o/dyld_introspection.h>)
#include <mach-o/dyld_introspection.h>
#define SDK_HAS_NEW_DYLD_INTROSPECTION_SPIS
Expand Down Expand Up @@ -78,8 +78,8 @@
static void ParseOSVersion(llvm::VersionTuple &version, NSString *Key) {
@autoreleasepool {
NSDictionary *version_info =
[NSDictionary dictionaryWithContentsOfFile:
@"/System/Library/CoreServices/SystemVersion.plist"];
[NSDictionary dictionaryWithContentsOfFile:
@"/System/Library/CoreServices/SystemVersion.plist"];
NSString *version_value = [version_info objectForKey: Key];
const char *version_str = [version_value UTF8String];
version.tryParse(version_str);
Expand Down Expand Up @@ -225,9 +225,9 @@ static bool ResolveAndVerifyCandidateSupportDir(FileSpec &path) {
}

bool HostInfoMacOSX::ComputeUserPluginsDirectory(FileSpec &file_spec) {
FileSpec temp_file("~/Library/Application Support/LLDB/PlugIns");
FileSystem::Instance().Resolve(temp_file);
file_spec.SetDirectory(temp_file.GetPathAsConstString());
FileSpec home_dir_spec = GetUserHomeDir();
home_dir_spec.AppendPathComponent("Library/Application Support/LLDB/PlugIns");
file_spec.SetDirectory(home_dir_spec.GetPathAsConstString());
return true;
}

Expand Down
31 changes: 14 additions & 17 deletions lldb/source/Interpreter/CommandInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include "lldb/Core/Telemetry.h"
#include "lldb/Host/StreamFile.h"
#include "lldb/Utility/ErrorMessages.h"
#include "lldb/Utility/FileSpec.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/State.h"
Expand Down Expand Up @@ -2502,22 +2503,18 @@ int CommandInterpreter::GetOptionArgumentPosition(const char *in_string) {
return position;
}

static void GetHomeInitFile(llvm::SmallVectorImpl<char> &init_file,
llvm::StringRef suffix = {}) {
static void GetHomeInitFile(FileSpec &init_file, llvm::StringRef suffix = {}) {
std::string init_file_name = ".lldbinit";
if (!suffix.empty()) {
init_file_name.append("-");
init_file_name.append(suffix.str());
}

FileSystem::Instance().GetHomeDirectory(init_file);
llvm::sys::path::append(init_file, init_file_name);

FileSystem::Instance().Resolve(init_file);
init_file =
HostInfo::GetUserHomeDir().CopyByAppendingPathComponent(init_file_name);
}

static void GetHomeREPLInitFile(llvm::SmallVectorImpl<char> &init_file,
LanguageType language) {
static void GetHomeREPLInitFile(FileSpec &init_file, LanguageType language) {
if (language == eLanguageTypeUnknown) {
LanguageSet repl_languages = Language::GetLanguagesSupportingREPLs();
if (auto main_repl_language = repl_languages.GetSingularLanguage())
Expand All @@ -2531,9 +2528,9 @@ static void GetHomeREPLInitFile(llvm::SmallVectorImpl<char> &init_file,
llvm::Twine(Language::GetNameForLanguageType(language)) +
llvm::Twine("-repl"))
.str();
FileSystem::Instance().GetHomeDirectory(init_file);
llvm::sys::path::append(init_file, init_file_name);
FileSystem::Instance().Resolve(init_file);

init_file =
HostInfo::GetUserHomeDir().CopyByAppendingPathComponent(init_file_name);
}

static void GetCwdInitFile(llvm::SmallVectorImpl<char> &init_file) {
Expand Down Expand Up @@ -2588,10 +2585,10 @@ void CommandInterpreter::SourceInitFileCwd(CommandReturnObject &result) {
SourceInitFile(FileSpec(init_file.str()), result);
break;
case eLoadCWDlldbinitWarn: {
llvm::SmallString<128> home_init_file;
FileSpec home_init_file;
GetHomeInitFile(home_init_file);
if (llvm::sys::path::parent_path(init_file) ==
llvm::sys::path::parent_path(home_init_file)) {
llvm::sys::path::parent_path(home_init_file.GetPath())) {
result.SetStatus(eReturnStatusSuccessFinishNoResult);
} else {
result.AppendError(InitFileWarning);
Expand All @@ -2611,24 +2608,24 @@ void CommandInterpreter::SourceInitFileHome(CommandReturnObject &result,
return;
}

llvm::SmallString<128> init_file;
FileSpec init_file;

if (is_repl)
GetHomeREPLInitFile(init_file, GetDebugger().GetREPLLanguage());

if (init_file.empty())
if (init_file.GetPath().empty())
GetHomeInitFile(init_file);

if (!m_skip_app_init_files) {
llvm::StringRef program_name =
HostInfo::GetProgramFileSpec().GetFilename().GetStringRef();
llvm::SmallString<128> program_init_file;
FileSpec program_init_file;
GetHomeInitFile(program_init_file, program_name);
if (FileSystem::Instance().Exists(program_init_file))
init_file = program_init_file;
}

SourceInitFile(FileSpec(init_file.str()), result);
SourceInitFile(init_file, result);
}

void CommandInterpreter::SourceInitFileGlobal(CommandReturnObject &result) {
Expand Down
Loading
Loading