Author: John Harrison Date: 2025-08-29T16:17:45-07:00 New Revision: a9ea7cf60c1ec0a85bc5d970e5205b612a70ae1c
URL: https://github.com/llvm/llvm-project/commit/a9ea7cf60c1ec0a85bc5d970e5205b612a70ae1c DIFF: https://github.com/llvm/llvm-project/commit/a9ea7cf60c1ec0a85bc5d970e5205b612a70ae1c.diff LOG: [lldb] Adjust ProtocolServer connection defaults. (#155714) This adjusts the ProtocolServer command to default to create a new connection listening on `localhost:0` and adds a new `ServerMetadata` details to `~/.lldb/mcp/lldb-<pid>.json` to record information about the current MCP server. This can be consumed by the lldb-mcp binary to establish a connection from an LLM client. --------- Co-authored-by: Jonas Devlieghere <jo...@devlieghere.com> Added: Modified: lldb/include/lldb/Host/HostInfoBase.h lldb/include/lldb/Host/macosx/HostInfoMacOSX.h lldb/include/lldb/Protocol/MCP/Server.h lldb/source/API/SBHostOS.cpp lldb/source/Commands/CommandObjectProtocolServer.cpp lldb/source/Host/common/Editline.cpp lldb/source/Host/common/HostInfoBase.cpp lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm lldb/source/Interpreter/CommandInterpreter.cpp lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.cpp lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.h lldb/source/Protocol/MCP/Server.cpp lldb/unittests/Editline/EditlineTest.cpp Removed: ################################################################################ diff --git a/lldb/include/lldb/Host/HostInfoBase.h b/lldb/include/lldb/Host/HostInfoBase.h index b6a95fffb2db2..a6aaacd9d6feb 100644 --- a/lldb/include/lldb/Host/HostInfoBase.h +++ b/lldb/include/lldb/Host/HostInfoBase.h @@ -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(); @@ -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 diff --git a/lldb/include/lldb/Host/macosx/HostInfoMacOSX.h b/lldb/include/lldb/Host/macosx/HostInfoMacOSX.h index d048418856604..734a394c18679 100644 --- a/lldb/include/lldb/Host/macosx/HostInfoMacOSX.h +++ b/lldb/include/lldb/Host/macosx/HostInfoMacOSX.h @@ -56,6 +56,7 @@ class HostInfoMacOSX : public HostInfoPosix { static std::string FindComponentInPath(llvm::StringRef path, llvm::StringRef component); }; -} + +} // namespace lldb_private #endif diff --git a/lldb/include/lldb/Protocol/MCP/Server.h b/lldb/include/lldb/Protocol/MCP/Server.h index aa5714e45755e..c6e78a9ea0cff 100644 --- a/lldb/include/lldb/Protocol/MCP/Server.h +++ b/lldb/include/lldb/Protocol/MCP/Server.h @@ -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, diff --git a/lldb/source/API/SBHostOS.cpp b/lldb/source/API/SBHostOS.cpp index a77a703bba37b..cd9b8571df3e8 100644 --- a/lldb/source/API/SBHostOS.cpp +++ b/lldb/source/API/SBHostOS.cpp @@ -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, diff --git a/lldb/source/Commands/CommandObjectProtocolServer.cpp b/lldb/source/Commands/CommandObjectProtocolServer.cpp index f11e27f01c8a8..c5ab9e9f05bec 100644 --- a/lldb/source/Commands/CommandObjectProtocolServer.cpp +++ b/lldb/source/Commands/CommandObjectProtocolServer.cpp @@ -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; @@ -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); } @@ -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); diff --git a/lldb/source/Host/common/Editline.cpp b/lldb/source/Host/common/Editline.cpp index 5ed30fbb231d8..1fc86c8a3e1be 100644 --- a/lldb/source/Host/common/Editline.cpp +++ b/lldb/source/Host/common/Editline.cpp @@ -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" @@ -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(); } } diff --git a/lldb/source/Host/common/HostInfoBase.cpp b/lldb/source/Host/common/HostInfoBase.cpp index 89dfe4a9e9baa..a02ac77df66a3 100644 --- a/lldb/source/Host/common/HostInfoBase.cpp +++ b/lldb/source/Host/common/HostInfoBase.cpp @@ -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; @@ -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( @@ -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. diff --git a/lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm b/lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm index 61f94190c956c..79e1322a870eb 100644 --- a/lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm +++ b/lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm @@ -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 @@ -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); @@ -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; } diff --git a/lldb/source/Interpreter/CommandInterpreter.cpp b/lldb/source/Interpreter/CommandInterpreter.cpp index 650b754fd8ace..d06e8c344c237 100644 --- a/lldb/source/Interpreter/CommandInterpreter.cpp +++ b/lldb/source/Interpreter/CommandInterpreter.cpp @@ -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" @@ -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()) @@ -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) { @@ -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); @@ -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) { diff --git a/lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.cpp b/lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.cpp index e6c5d9bc9c278..12cb25788534e 100644 --- a/lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.cpp +++ b/lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.cpp @@ -10,14 +10,15 @@ #include "Resource.h" #include "Tool.h" #include "lldb/Core/PluginManager.h" -#include "lldb/Protocol/MCP/MCPError.h" -#include "lldb/Protocol/MCP/Tool.h" +#include "lldb/Host/FileSystem.h" +#include "lldb/Host/HostInfo.h" +#include "lldb/Protocol/MCP/Server.h" #include "lldb/Utility/LLDBLog.h" #include "lldb/Utility/Log.h" #include "llvm/ADT/StringExtras.h" +#include "llvm/Support/Error.h" #include "llvm/Support/Threading.h" #include <thread> -#include <variant> using namespace lldb_private; using namespace lldb_private::mcp; @@ -106,6 +107,39 @@ llvm::Error ProtocolServerMCP::Start(ProtocolServer::Connection connection) { if (llvm::Error error = handles.takeError()) return error; + auto listening_uris = m_listener->GetListeningConnectionURI(); + if (listening_uris.empty()) + return createStringError("failed to get listening connections"); + std::string address = + llvm::join(m_listener->GetListeningConnectionURI(), ", "); + + FileSpec user_lldb_dir = HostInfo::GetUserLLDBDir(); + + Status error(llvm::sys::fs::create_directory(user_lldb_dir.GetPath())); + if (error.Fail()) + return error.takeError(); + + m_mcp_registry_entry_path = user_lldb_dir.CopyByAppendingPathComponent( + formatv("lldb-mcp-{0}.json", getpid()).str()); + + ServerInfo info; + info.connection_uri = listening_uris[0]; + info.pid = getpid(); + + std::string buf = formatv("{0}", toJSON(info)).str(); + size_t num_bytes = buf.size(); + + const File::OpenOptions flags = File::eOpenOptionWriteOnly | + File::eOpenOptionCanCreate | + File::eOpenOptionTruncate; + llvm::Expected<lldb::FileUP> file = + FileSystem::Instance().Open(m_mcp_registry_entry_path, flags, + lldb::eFilePermissionsFileDefault, false); + if (!file) + return file.takeError(); + if (llvm::Error error = (*file)->Write(buf.data(), num_bytes).takeError()) + return error; + m_running = true; m_listen_handlers = std::move(*handles); m_loop_thread = std::thread([=] { @@ -124,6 +158,10 @@ llvm::Error ProtocolServerMCP::Stop() { m_running = false; } + if (!m_mcp_registry_entry_path.GetPath().empty()) + FileSystem::Instance().RemoveFile(m_mcp_registry_entry_path); + m_mcp_registry_entry_path.Clear(); + // Stop the main loop. m_loop.AddPendingCallback( [](lldb_private::MainLoopBase &loop) { loop.RequestTermination(); }); diff --git a/lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.h b/lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.h index fc650ffe0dfa7..004fa3c2d05a8 100644 --- a/lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.h +++ b/lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.h @@ -48,6 +48,7 @@ class ProtocolServerMCP : public ProtocolServer { bool m_running = false; + FileSpec m_mcp_registry_entry_path; lldb_private::MainLoop m_loop; std::thread m_loop_thread; std::mutex m_mutex; diff --git a/lldb/source/Protocol/MCP/Server.cpp b/lldb/source/Protocol/MCP/Server.cpp index 63c2d01d17922..0381b7f745e98 100644 --- a/lldb/source/Protocol/MCP/Server.cpp +++ b/lldb/source/Protocol/MCP/Server.cpp @@ -14,6 +14,18 @@ using namespace lldb_protocol::mcp; using namespace llvm; +llvm::json::Value lldb_protocol::mcp::toJSON(const ServerInfo &SM) { + return llvm::json::Object{{"connection_uri", SM.connection_uri}, + {"pid", SM.pid}}; +} + +bool lldb_protocol::mcp::fromJSON(const llvm::json::Value &V, ServerInfo &SM, + llvm::json::Path P) { + llvm::json::ObjectMapper O(V, P); + return O && O.map("connection_uri", SM.connection_uri) && + O.map("pid", SM.pid); +} + Server::Server(std::string name, std::string version, std::unique_ptr<MCPTransport> transport_up, lldb_private::MainLoop &loop) diff --git a/lldb/unittests/Editline/EditlineTest.cpp b/lldb/unittests/Editline/EditlineTest.cpp index 6c5a0c907a33e..2875f4ee7e6b4 100644 --- a/lldb/unittests/Editline/EditlineTest.cpp +++ b/lldb/unittests/Editline/EditlineTest.cpp @@ -8,6 +8,7 @@ #include "lldb/Host/Config.h" #include "lldb/Host/File.h" +#include "lldb/Host/HostInfo.h" #if LLDB_ENABLE_LIBEDIT @@ -244,7 +245,7 @@ void EditlineAdapter::ConsumeAllOutput() { } class EditlineTestFixture : public ::testing::Test { - SubsystemRAII<FileSystem> subsystems; + SubsystemRAII<FileSystem, HostInfo> subsystems; EditlineAdapter _el_adapter; std::shared_ptr<std::thread> _sp_output_thread; _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits