|  | 
|  | 1 | +//===----------------------------------------------------------------------===// | 
|  | 2 | +// | 
|  | 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | 
|  | 4 | +// See https://llvm.org/LICENSE.txt for license information. | 
|  | 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | 
|  | 6 | +// | 
|  | 7 | +//===----------------------------------------------------------------------===// | 
|  | 8 | + | 
|  | 9 | +#include "lldb/Host/Config.h" | 
|  | 10 | +#include "lldb/Host/File.h" | 
|  | 11 | +#include "lldb/Host/MainLoop.h" | 
|  | 12 | +#include "lldb/Host/MainLoopBase.h" | 
|  | 13 | +#include "lldb/Protocol/MCP/Protocol.h" | 
|  | 14 | +#include "lldb/Protocol/MCP/Server.h" | 
|  | 15 | +#include "llvm/ADT/StringRef.h" | 
|  | 16 | +#include "llvm/Support/InitLLVM.h" | 
|  | 17 | +#include "llvm/Support/Signals.h" | 
|  | 18 | +#include "llvm/Support/WithColor.h" | 
|  | 19 | + | 
|  | 20 | +using namespace lldb_protocol::mcp; | 
|  | 21 | + | 
|  | 22 | +using lldb_private::File; | 
|  | 23 | +using lldb_private::MainLoop; | 
|  | 24 | +using lldb_private::MainLoopBase; | 
|  | 25 | +using lldb_private::NativeFile; | 
|  | 26 | + | 
|  | 27 | +static constexpr llvm::StringLiteral kName = "lldb-mcp"; | 
|  | 28 | +static constexpr llvm::StringLiteral kVersion = "0.1.0"; | 
|  | 29 | + | 
|  | 30 | +int main(int argc, char *argv[]) { | 
|  | 31 | +  llvm::InitLLVM IL(argc, argv, /*InstallPipeSignalExitHandler=*/false); | 
|  | 32 | +#if !defined(__APPLE__) | 
|  | 33 | +  llvm::setBugReportMsg("PLEASE submit a bug report to " LLDB_BUG_REPORT_URL | 
|  | 34 | +                        " and include the crash backtrace.\n"); | 
|  | 35 | +#else | 
|  | 36 | +  llvm::setBugReportMsg("PLEASE submit a bug report to " LLDB_BUG_REPORT_URL | 
|  | 37 | +                        " and include the crash report from " | 
|  | 38 | +                        "~/Library/Logs/DiagnosticReports/.\n"); | 
|  | 39 | +#endif | 
|  | 40 | + | 
|  | 41 | +#if defined(_WIN32) | 
|  | 42 | +  // Windows opens stdout and stdin in text mode which converts \n to 13,10 | 
|  | 43 | +  // while the value is just 10 on Darwin/Linux. Setting the file mode to | 
|  | 44 | +  // binary fixes this. | 
|  | 45 | +  int result = _setmode(fileno(stdout), _O_BINARY); | 
|  | 46 | +  assert(result); | 
|  | 47 | +  result = _setmode(fileno(stdin), _O_BINARY); | 
|  | 48 | +  UNUSED_IF_ASSERT_DISABLED(result); | 
|  | 49 | +  assert(result); | 
|  | 50 | +#endif | 
|  | 51 | + | 
|  | 52 | +  lldb::IOObjectSP input = std::make_shared<NativeFile>( | 
|  | 53 | +      fileno(stdin), File::eOpenOptionReadOnly, NativeFile::Unowned); | 
|  | 54 | + | 
|  | 55 | +  lldb::IOObjectSP output = std::make_shared<NativeFile>( | 
|  | 56 | +      fileno(stdout), File::eOpenOptionWriteOnly, NativeFile::Unowned); | 
|  | 57 | + | 
|  | 58 | +  constexpr llvm::StringLiteral client_name = "stdio"; | 
|  | 59 | +  static MainLoop loop; | 
|  | 60 | + | 
|  | 61 | +  llvm::sys::SetInterruptFunction([]() { | 
|  | 62 | +    loop.AddPendingCallback( | 
|  | 63 | +        [](MainLoopBase &loop) { loop.RequestTermination(); }); | 
|  | 64 | +  }); | 
|  | 65 | + | 
|  | 66 | +  auto transport_up = std::make_unique<lldb_protocol::mcp::MCPTransport>( | 
|  | 67 | +      input, output, std::string(client_name), | 
|  | 68 | +      [&](llvm::StringRef message) { llvm::errs() << message << '\n'; }); | 
|  | 69 | + | 
|  | 70 | +  auto instance_up = std::make_unique<lldb_protocol::mcp::Server>( | 
|  | 71 | +      std::string(kName), std::string(kVersion), std::move(transport_up), loop); | 
|  | 72 | + | 
|  | 73 | +  if (llvm::Error error = instance_up->Run()) { | 
|  | 74 | +    llvm::logAllUnhandledErrors(std::move(error), llvm::WithColor::error(), | 
|  | 75 | +                                "MCP error: "); | 
|  | 76 | +    return EXIT_FAILURE; | 
|  | 77 | +  } | 
|  | 78 | + | 
|  | 79 | +  return EXIT_SUCCESS; | 
|  | 80 | +} | 
0 commit comments