11#include "llvm/Support/ErrorExtras.h"
18struct DebuggerResource {
19 uint64_t debugger_id = 0;
21 uint64_t num_targets = 0;
24llvm::json::Value
toJSON(
const DebuggerResource &DR) {
25 llvm::json::Object Result{{
"debugger_id", DR.debugger_id},
26 {
"num_targets", DR.num_targets}};
28 Result.insert({
"name", DR.name});
32struct TargetResource {
33 size_t debugger_id = 0;
34 size_t target_idx = 0;
35 bool selected =
false;
42llvm::json::Value
toJSON(
const TargetResource &TR) {
43 llvm::json::Object Result{{
"debugger_id", TR.debugger_id},
44 {
"target_idx", TR.target_idx},
45 {
"selected", TR.selected},
48 Result.insert({
"arch", TR.arch});
50 Result.insert({
"path", TR.path});
51 if (!TR.platform.empty())
52 Result.insert({
"platform", TR.platform});
60 return llvm::make_error<UnsupportedURI>(uri.str());
68 resource.
uri = llvm::formatv(
"lldb://debugger/{0}", debugger_id);
71 llvm::formatv(
"Information about debugger instance {0}: {1}", debugger_id,
81 std::string target_name = llvm::formatv(
"target {0}", target_idx);
84 target_name = exe_module->GetFileSpec().GetFilename().GetString();
88 llvm::formatv(
"lldb://debugger/{0}/target/{1}", debugger_id, target_idx);
89 resource.
name = target_name;
91 llvm::formatv(
"Information about target {0} in debugger instance {1}",
92 target_idx, debugger_id);
97std::vector<lldb_protocol::mcp::Resource>
99 std::vector<lldb_protocol::mcp::Resource> resources;
102 for (
size_t i = 0; i < num_debuggers; ++i) {
108 TargetList &target_list = debugger_sp->GetTargetList();
110 for (
size_t j = 0; j < num_targets; ++j) {
121llvm::Expected<lldb_protocol::mcp::ReadResourceResult>
124 auto [protocol, path] = uri.split(
"://");
126 if (protocol !=
"lldb")
129 llvm::SmallVector<llvm::StringRef, 4> components;
130 path.split(components,
'/');
132 if (components.size() < 2)
135 if (components[0] !=
"debugger")
139 if (components[1].getAsInteger(0, debugger_idx))
140 return llvm::createStringErrorV(
"invalid debugger id '{0}': {1}",
141 components[1], path);
143 if (components.size() > 3) {
144 if (components[2] !=
"target")
148 if (components[3].getAsInteger(0, target_idx))
149 return llvm::createStringErrorV(
"invalid target id '{0}': {1}",
150 components[3], path);
158llvm::Expected<lldb_protocol::mcp::ReadResourceResult>
163 return llvm::createStringErrorV(
"invalid debugger id: {0}", debugger_id);
165 DebuggerResource debugger_resource;
166 debugger_resource.debugger_id = debugger_id;
167 debugger_resource.name = debugger_sp->GetInstanceName();
168 debugger_resource.num_targets = debugger_sp->GetTargetList().GetNumTargets();
173 contents.
text = llvm::formatv(
"{0}",
toJSON(debugger_resource));
176 result.
contents.push_back(contents);
180llvm::Expected<lldb_protocol::mcp::ReadResourceResult>
187 return llvm::createStringErrorV(
"invalid debugger id: {0}", debugger_id);
189 TargetList &target_list = debugger_sp->GetTargetList();
192 return llvm::createStringErrorV(
"invalid target idx: {0}", target_idx);
194 TargetResource target_resource;
195 target_resource.debugger_id = debugger_id;
196 target_resource.target_idx = target_idx;
197 target_resource.arch = target_sp->GetArchitecture().GetTriple().str();
198 target_resource.dummy = target_sp->IsDummyTarget();
199 target_resource.selected = target_sp == debugger_sp->GetSelectedTarget();
201 if (
Module *exe_module = target_sp->GetExecutableModulePointer())
202 target_resource.path = exe_module->GetFileSpec().GetPath();
204 target_resource.platform = platform_sp->GetName();
209 contents.
text = llvm::formatv(
"{0}",
toJSON(target_resource));
212 result.
contents.push_back(contents);
static constexpr llvm::StringLiteral kMimeTypeJSON
static llvm::Error createUnsupportedURIError(llvm::StringRef uri)
A class to manage flag bits.
static lldb::DebuggerSP GetDebuggerAtIndex(size_t index)
const std::string & GetInstanceName() const
static lldb::DebuggerSP FindDebuggerWithID(lldb::user_id_t id)
static size_t GetNumDebuggers()
A class that describes an executable image and its associated object and symbol files.
lldb::TargetSP GetTargetAtIndex(uint32_t index) const
size_t GetNumTargets() const
Module * GetExecutableModulePointer()
Debugger & GetDebugger() const
llvm::Expected< lldb_protocol::mcp::ReadResourceResult > ReadResource(llvm::StringRef uri) const override
static llvm::Expected< lldb_protocol::mcp::ReadResourceResult > ReadTargetResource(llvm::StringRef uri, lldb::user_id_t debugger_id, size_t target_idx)
static lldb_protocol::mcp::Resource GetDebuggerResource(Debugger &debugger)
static lldb_protocol::mcp::Resource GetTargetResource(size_t target_idx, Target &target)
static llvm::Expected< lldb_protocol::mcp::ReadResourceResult > ReadDebuggerResource(llvm::StringRef uri, lldb::user_id_t debugger_id)
std::vector< lldb_protocol::mcp::Resource > GetResources() const override
A class that represents a running process on the host machine.
llvm::json::Value toJSON(const TraceSupportedResponse &packet)
llvm::json::Value toJSON(const Request &)
std::shared_ptr< lldb_private::Platform > PlatformSP
std::shared_ptr< lldb_private::Debugger > DebuggerSP
std::shared_ptr< lldb_private::Target > TargetSP
lldb::user_id_t GetID() const
Get accessor for the user ID.
The server's response to a resources/read request from the client.
std::vector< TextResourceContents > contents
A known resource that the server is capable of reading.
std::string description
A description of what this resource represents.
std::string uri
The URI of this resource.
std::string mimeType
The MIME type of this resource, if known.
std::string name
A human-readable name for this resource.
The contents of a specific resource or sub-resource.
std::string text
The text of the item.
std::string uri
The URI of this resource.
std::string mimeType
The MIME type of this resource, if known.