[Lldb-commits] [lldb] [lldb] Expose debuggers and target as resources through MCP (PR #148075)
Jonas Devlieghere via lldb-commits
lldb-commits at lists.llvm.org
Fri Jul 11 10:10:49 PDT 2025
================
@@ -0,0 +1,166 @@
+// 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 "Resource.h"
+#include "MCPError.h"
+#include "lldb/Core/Debugger.h"
+#include "lldb/Core/Module.h"
+
+using namespace lldb_private::mcp;
+
+template <typename... Args>
+static llvm::Error createStringError(const char *format, Args &&...args) {
+ return llvm::createStringError(
+ llvm::formatv(format, std::forward<Args>(args)...).str());
+}
+
+static llvm::Error createUnsupportedURIError(llvm::StringRef uri) {
+ return llvm::make_error<UnsupportedURI>(uri.str());
+}
+
+protocol::Resource
+DebuggerResourceProvider::GetDebuggerResource(lldb::user_id_t debugger_id) {
+ protocol::Resource resource;
+ resource.uri = llvm::formatv("lldb://debugger/{0}", debugger_id);
+ resource.name = llvm::formatv("debugger {0}", debugger_id);
+ resource.description =
+ llvm::formatv("Information about debugger instance {0}", debugger_id);
+ resource.mimeType = "application/json";
+ return resource;
+}
+
+protocol::Resource
+DebuggerResourceProvider::GetTargetResource(lldb::user_id_t debugger_id,
+ lldb::user_id_t target_id) {
+ protocol::Resource resource;
+ resource.uri =
+ llvm::formatv("lldb://debugger/{0}/target/{1}", debugger_id, target_id);
+ resource.name = llvm::formatv("target {0}", target_id);
+ resource.description =
+ llvm::formatv("Information about target {0} in debugger instance {1}",
+ target_id, debugger_id);
+ resource.mimeType = "application/json";
+ return resource;
+}
+
+std::vector<protocol::Resource> DebuggerResourceProvider::GetResources() const {
+ std::vector<protocol::Resource> resources;
+
+ const size_t num_debuggers = Debugger::GetNumDebuggers();
+ for (size_t i = 0; i < num_debuggers; ++i) {
+ lldb::DebuggerSP debugger_sp = Debugger::GetDebuggerAtIndex(i);
+ if (!debugger_sp)
+ continue;
+ resources.emplace_back(GetDebuggerResource(i));
+
+ TargetList &target_list = debugger_sp->GetTargetList();
+ const size_t num_targets = target_list.GetNumTargets();
+ for (size_t j = 0; j < num_targets; ++j) {
+ lldb::TargetSP target_sp = target_list.GetTargetAtIndex(j);
+ if (!target_sp)
+ continue;
+ resources.emplace_back(GetTargetResource(i, j));
+ }
+ }
+
+ return resources;
+}
+
+llvm::Expected<protocol::ResourceResult>
+DebuggerResourceProvider::ReadResource(llvm::StringRef uri) const {
+ auto [protocol, path] = uri.split("://");
----------------
JDevlieghere wrote:
Yeah, it treats the first component as the hostname, which is why I didn't use it. Not sure if there's a good way to distinguish the two.
https://github.com/llvm/llvm-project/pull/148075
More information about the lldb-commits
mailing list