15#include "llvm/ADT/StringRef.h"
16#include "llvm/Support/Error.h"
28static constexpr StringLiteral kSchemeAndHost =
"lldb-mcp://debugger/";
30struct CommandToolArguments {
36bool fromJSON(
const json::Value &V, CommandToolArguments &A, json::Path P) {
37 json::ObjectMapper O(V, P);
38 return O && O.mapOptional(
"debugger", A.debugger) &&
39 O.mapOptional(
"command", A.command);
44createTextResult(std::string output,
bool is_error =
false) {
46 text_result.
content.emplace_back(
53 return (kSchemeAndHost + std::to_string(debugger->GetID())).str();
58Expected<lldb_protocol::mcp::CallToolResult>
60 if (!std::holds_alternative<json::Value>(args))
63 json::Path::Root root;
65 CommandToolArguments arguments;
66 if (!
fromJSON(std::get<json::Value>(args), arguments, root))
67 return root.getError();
71 if (!arguments.debugger.empty()) {
72 llvm::StringRef debugger_specifier = arguments.debugger;
73 debugger_specifier.consume_front(kSchemeAndHost);
74 uint32_t debugger_id = 0;
75 if (debugger_specifier.consumeInteger(10, debugger_id))
77 formatv(
"malformed debugger specifier {0}", arguments.debugger));
93 debugger_sp->GetCommandInterpreter().HandleCommand(arguments.command.c_str(),
98 if (!output_str.empty())
99 output += output_str.str();
102 if (!err_str.empty()) {
108 return createTextResult(output, !result.
Succeeded());
115 Object{{
"type",
"string"},
117 "The debugger ID or URI to a specific debug session. If not "
118 "specified, the first debugger will be used."}}},
120 Object{{
"type",
"string"}, {
"description",
"An lldb command to run."}}}};
121 Object schema{{
"type",
"object"}, {
"properties", std::move(properties)}};
125Expected<lldb_protocol::mcp::CallToolResult>
127 llvm::json::Path::Root root;
137 llvm::raw_string_ostream os(output);
140 for (
size_t i = 0; i < num_debuggers; ++i) {
145 os <<
"- " << to_uri(debugger_sp) <<
'\n';
148 return createTextResult(output);
static llvm::Error createStringError(const char *format, Args &&...args)
std::string GetErrorString(bool with_diagnostics=true) const
Return the errors as a string.
llvm::StringRef GetOutputString() const
static lldb::DebuggerSP GetDebuggerAtIndex(size_t index)
static lldb::DebuggerSP FindDebuggerWithID(lldb::user_id_t id)
static size_t GetNumDebuggers()
A class that represents a running process on the host machine.
bool fromJSON(const llvm::json::Value &value, TraceSupportedResponse &info, llvm::json::Path path)
std::variant< std::monostate, llvm::json::Value > ToolArguments
std::shared_ptr< lldb_private::Debugger > DebuggerSP
Text provided to or from an LLM.