cmDebuggerAdapter.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmConfigure.h" // IWYU pragma: keep
  4. #include "cmDebuggerAdapter.h"
  5. #include <algorithm>
  6. #include <climits>
  7. #include <condition_variable>
  8. #include <cstdint>
  9. #include <functional>
  10. #include <iostream>
  11. #include <stdexcept>
  12. #include <utility>
  13. #include <cm/memory>
  14. #include <cm/optional>
  15. #include <cm3p/cppdap/io.h> // IWYU pragma: keep
  16. #include <cm3p/cppdap/protocol.h>
  17. #include <cm3p/cppdap/session.h>
  18. #include "cmDebuggerBreakpointManager.h"
  19. #include "cmDebuggerExceptionManager.h"
  20. #include "cmDebuggerProtocol.h"
  21. #include "cmDebuggerSourceBreakpoint.h" // IWYU pragma: keep
  22. #include "cmDebuggerStackFrame.h"
  23. #include "cmDebuggerThread.h"
  24. #include "cmDebuggerThreadManager.h"
  25. #include "cmListFileCache.h"
  26. #include "cmMakefile.h"
  27. #include "cmValue.h"
  28. #include "cmVersionConfig.h"
  29. #include <cmcppdap/include/dap/optional.h>
  30. #include <cmcppdap/include/dap/types.h>
  31. namespace cmDebugger {
  32. // Event provides a basic wait and signal synchronization primitive.
  33. class SyncEvent
  34. {
  35. public:
  36. // Wait() blocks until the event is fired.
  37. void Wait()
  38. {
  39. std::unique_lock<std::mutex> lock(Mutex);
  40. Cv.wait(lock, [&] { return Fired; });
  41. }
  42. // Fire() sets signals the event, and unblocks any calls to Wait().
  43. void Fire()
  44. {
  45. std::unique_lock<std::mutex> lock(Mutex);
  46. Fired = true;
  47. Cv.notify_all();
  48. }
  49. private:
  50. std::mutex Mutex;
  51. std::condition_variable Cv;
  52. bool Fired = false;
  53. };
  54. class Semaphore
  55. {
  56. public:
  57. Semaphore(int count_ = 0)
  58. : Count(count_)
  59. {
  60. }
  61. void Notify()
  62. {
  63. std::unique_lock<std::mutex> lock(Mutex);
  64. Count++;
  65. // notify the waiting thread
  66. Cv.notify_one();
  67. }
  68. void Wait()
  69. {
  70. std::unique_lock<std::mutex> lock(Mutex);
  71. while (Count == 0) {
  72. // wait on the mutex until notify is called
  73. Cv.wait(lock);
  74. }
  75. Count--;
  76. }
  77. private:
  78. std::mutex Mutex;
  79. std::condition_variable Cv;
  80. int Count;
  81. };
  82. cmDebuggerAdapter::cmDebuggerAdapter(
  83. std::shared_ptr<cmDebuggerConnection> connection,
  84. std::string const& dapLogPath)
  85. : cmDebuggerAdapter(std::move(connection),
  86. dapLogPath.empty()
  87. ? cm::nullopt
  88. : cm::optional<std::shared_ptr<dap::Writer>>(
  89. dap::file(dapLogPath.c_str())))
  90. {
  91. }
  92. cmDebuggerAdapter::cmDebuggerAdapter(
  93. std::shared_ptr<cmDebuggerConnection> connection,
  94. cm::optional<std::shared_ptr<dap::Writer>> logger)
  95. : Connection(std::move(connection))
  96. , SessionActive(true)
  97. , DisconnectEvent(cm::make_unique<SyncEvent>())
  98. , ConfigurationDoneEvent(cm::make_unique<SyncEvent>())
  99. , ContinueSem(cm::make_unique<Semaphore>())
  100. , ThreadManager(cm::make_unique<cmDebuggerThreadManager>())
  101. {
  102. if (logger.has_value()) {
  103. SessionLog = std::move(logger.value());
  104. }
  105. ClearStepRequests();
  106. Session = dap::Session::create();
  107. BreakpointManager =
  108. cm::make_unique<cmDebuggerBreakpointManager>(Session.get());
  109. ExceptionManager =
  110. cm::make_unique<cmDebuggerExceptionManager>(Session.get());
  111. // Handle errors reported by the Session. These errors include protocol
  112. // parsing errors and receiving messages with no handler.
  113. Session->onError([this](char const* msg) {
  114. if (SessionLog) {
  115. dap::writef(SessionLog, "dap::Session error: %s\n", msg);
  116. }
  117. std::cout << "[CMake Debugger] DAP session error: " << msg << std::endl;
  118. BreakpointManager->ClearAll();
  119. ExceptionManager->ClearAll();
  120. ClearStepRequests();
  121. ContinueSem->Notify();
  122. DisconnectEvent->Fire();
  123. SessionActive.store(false);
  124. });
  125. // https://microsoft.github.io/debug-adapter-protocol/specification#Requests_Initialize
  126. Session->registerHandler([this](dap::CMakeInitializeRequest const& req) {
  127. SupportsVariableType = req.supportsVariableType.value(false);
  128. dap::CMakeInitializeResponse response;
  129. response.supportsConfigurationDoneRequest = true;
  130. response.supportsValueFormattingOptions = true;
  131. response.cmakeVersion.major = CMake_VERSION_MAJOR;
  132. response.cmakeVersion.minor = CMake_VERSION_MINOR;
  133. response.cmakeVersion.patch = CMake_VERSION_PATCH;
  134. response.cmakeVersion.full = CMake_VERSION;
  135. ExceptionManager->HandleInitializeRequest(response);
  136. return response;
  137. });
  138. // https://microsoft.github.io/debug-adapter-protocol/specification#Events_Initialized
  139. Session->registerSentHandler(
  140. [&](dap::ResponseOrError<dap::CMakeInitializeResponse> const&) {
  141. Session->send(dap::InitializedEvent());
  142. });
  143. // https://microsoft.github.io/debug-adapter-protocol/specification#Requests_Threads
  144. Session->registerHandler([this](dap::ThreadsRequest /*unused*/) {
  145. std::unique_lock<std::mutex> lock(Mutex);
  146. dap::ThreadsResponse response;
  147. // If a client requests threads during shutdown (like after receiving the
  148. // thread exited event), DefaultThread won't be set.
  149. if (DefaultThread) {
  150. dap::Thread thread;
  151. thread.id = DefaultThread->GetId();
  152. thread.name = DefaultThread->GetName();
  153. response.threads.push_back(thread);
  154. }
  155. return response;
  156. });
  157. // https://microsoft.github.io/debug-adapter-protocol/specification#Requests_StackTrace
  158. Session->registerHandler([this](dap::StackTraceRequest const& request)
  159. -> dap::ResponseOrError<dap::StackTraceResponse> {
  160. std::unique_lock<std::mutex> lock(Mutex);
  161. cm::optional<dap::StackTraceResponse> response =
  162. ThreadManager->GetThreadStackTraceResponse(request);
  163. if (response.has_value()) {
  164. return response.value();
  165. }
  166. return dap::Error("Unknown threadId '%d'", int(request.threadId));
  167. });
  168. // https://microsoft.github.io/debug-adapter-protocol/specification#Requests_Scopes
  169. Session->registerHandler([this](dap::ScopesRequest request)
  170. -> dap::ResponseOrError<dap::ScopesResponse> {
  171. std::unique_lock<std::mutex> lock(Mutex);
  172. return DefaultThread->GetScopesResponse(request.frameId,
  173. SupportsVariableType);
  174. });
  175. // https://microsoft.github.io/debug-adapter-protocol/specification#Requests_Variables
  176. Session->registerHandler([this](dap::VariablesRequest const& request)
  177. -> dap::ResponseOrError<dap::VariablesResponse> {
  178. return DefaultThread->GetVariablesResponse(request);
  179. });
  180. // https://microsoft.github.io/debug-adapter-protocol/specification#Requests_Pause
  181. Session->registerHandler([this](dap::PauseRequest /*unused*/) {
  182. PauseRequest.store(true);
  183. return dap::PauseResponse();
  184. });
  185. // https://microsoft.github.io/debug-adapter-protocol/specification#Requests_Continue
  186. Session->registerHandler([this](dap::ContinueRequest const& req) {
  187. (void)req;
  188. ContinueSem->Notify();
  189. return dap::ContinueResponse();
  190. });
  191. // https://microsoft.github.io/debug-adapter-protocol/specification#Requests_Next
  192. Session->registerHandler([this](dap::NextRequest const& req) {
  193. (void)req;
  194. NextStepFrom.store(DefaultThread->GetStackFrameSize());
  195. ContinueSem->Notify();
  196. return dap::NextResponse();
  197. });
  198. // https://microsoft.github.io/debug-adapter-protocol/specification#Requests_StepIn
  199. Session->registerHandler([this](dap::StepInRequest const& req) {
  200. (void)req;
  201. // This would stop after stepped in, single line stepped or stepped out.
  202. StepInRequest.store(true);
  203. ContinueSem->Notify();
  204. return dap::StepInResponse();
  205. });
  206. // https://microsoft.github.io/debug-adapter-protocol/specification#Requests_StepOut
  207. Session->registerHandler([this](dap::StepOutRequest const& req) {
  208. (void)req;
  209. StepOutDepth.store(DefaultThread->GetStackFrameSize() - 1);
  210. ContinueSem->Notify();
  211. return dap::StepOutResponse();
  212. });
  213. // https://microsoft.github.io/debug-adapter-protocol/specification#Requests_Launch
  214. Session->registerHandler([](dap::LaunchRequest const& req) {
  215. (void)req;
  216. return dap::LaunchResponse();
  217. });
  218. // Handler for disconnect requests
  219. Session->registerHandler([this](dap::DisconnectRequest const& request) {
  220. (void)request;
  221. BreakpointManager->ClearAll();
  222. ExceptionManager->ClearAll();
  223. ClearStepRequests();
  224. ContinueSem->Notify();
  225. DisconnectEvent->Fire();
  226. SessionActive.store(false);
  227. return dap::DisconnectResponse();
  228. });
  229. Session->registerHandler([this](dap::EvaluateRequest const& request) {
  230. dap::EvaluateResponse response;
  231. if (request.frameId.has_value()) {
  232. std::shared_ptr<cmDebuggerStackFrame> frame =
  233. DefaultThread->GetStackFrame(request.frameId.value());
  234. auto var = frame->GetMakefile()->GetDefinition(request.expression);
  235. if (var) {
  236. response.type = "string";
  237. response.result = var;
  238. return response;
  239. }
  240. }
  241. return response;
  242. });
  243. // The ConfigurationDone request is made by the client once all configuration
  244. // requests have been made.
  245. // https://microsoft.github.io/debug-adapter-protocol/specification#Requests_ConfigurationDone
  246. Session->registerHandler([this](dap::ConfigurationDoneRequest /*unused*/) {
  247. ConfigurationDoneEvent->Fire();
  248. return dap::ConfigurationDoneResponse();
  249. });
  250. std::string errorMessage;
  251. if (!Connection->StartListening(errorMessage)) {
  252. throw std::runtime_error(errorMessage);
  253. }
  254. // Connect to the client. Write a well-known message to stdout so that
  255. // clients know it is safe to attempt to connect.
  256. std::cout << "Waiting for debugger client to connect..." << std::endl;
  257. Connection->WaitForConnection();
  258. std::cout << "Debugger client connected." << std::endl;
  259. if (SessionLog) {
  260. Session->connect(spy(Connection->GetReader(), SessionLog),
  261. spy(Connection->GetWriter(), SessionLog));
  262. } else {
  263. Session->connect(Connection->GetReader(), Connection->GetWriter());
  264. }
  265. // Start the processing thread.
  266. SessionThread = std::thread([this] {
  267. while (SessionActive.load()) {
  268. if (auto payload = Session->getPayload()) {
  269. payload();
  270. }
  271. }
  272. });
  273. ConfigurationDoneEvent->Wait();
  274. DefaultThread = ThreadManager->StartThread("CMake script");
  275. dap::ThreadEvent threadEvent;
  276. threadEvent.reason = "started";
  277. threadEvent.threadId = DefaultThread->GetId();
  278. Session->send(threadEvent);
  279. }
  280. cmDebuggerAdapter::~cmDebuggerAdapter()
  281. {
  282. if (SessionThread.joinable()) {
  283. SessionThread.join();
  284. }
  285. Session.reset(nullptr);
  286. if (SessionLog) {
  287. SessionLog->close();
  288. }
  289. }
  290. void cmDebuggerAdapter::ReportExitCode(int exitCode)
  291. {
  292. ThreadManager->EndThread(DefaultThread);
  293. dap::ThreadEvent threadEvent;
  294. threadEvent.reason = "exited";
  295. threadEvent.threadId = DefaultThread->GetId();
  296. DefaultThread.reset();
  297. dap::ExitedEvent exitEvent;
  298. exitEvent.exitCode = exitCode;
  299. dap::TerminatedEvent terminatedEvent;
  300. if (SessionActive.load()) {
  301. Session->send(threadEvent);
  302. Session->send(exitEvent);
  303. Session->send(terminatedEvent);
  304. }
  305. // Wait until disconnected or error.
  306. DisconnectEvent->Wait();
  307. }
  308. void cmDebuggerAdapter::OnFileParsedSuccessfully(
  309. std::string const& sourcePath,
  310. std::vector<cmListFileFunction> const& functions)
  311. {
  312. BreakpointManager->SourceFileLoaded(sourcePath, functions);
  313. }
  314. void cmDebuggerAdapter::OnBeginFunctionCall(cmMakefile* mf,
  315. std::string const& sourcePath,
  316. cmListFileFunction const& lff)
  317. {
  318. std::unique_lock<std::mutex> lock(Mutex);
  319. DefaultThread->PushStackFrame(mf, sourcePath, lff);
  320. if (lff.Line() == 0) {
  321. // File just loaded, continue to first valid function call.
  322. return;
  323. }
  324. auto hits = BreakpointManager->GetBreakpoints(sourcePath, lff.Line());
  325. lock.unlock();
  326. bool waitSem = false;
  327. dap::StoppedEvent stoppedEvent;
  328. stoppedEvent.allThreadsStopped = true;
  329. stoppedEvent.threadId = DefaultThread->GetId();
  330. if (!hits.empty()) {
  331. ClearStepRequests();
  332. waitSem = true;
  333. dap::array<dap::integer> hitBreakpoints;
  334. hitBreakpoints.resize(hits.size());
  335. std::transform(hits.begin(), hits.end(), hitBreakpoints.begin(),
  336. [&](int64_t id) { return dap::integer(id); });
  337. stoppedEvent.reason = "breakpoint";
  338. stoppedEvent.hitBreakpointIds = hitBreakpoints;
  339. }
  340. if (long(DefaultThread->GetStackFrameSize()) <= NextStepFrom.load() ||
  341. StepInRequest.load() ||
  342. long(DefaultThread->GetStackFrameSize()) <= StepOutDepth.load()) {
  343. ClearStepRequests();
  344. waitSem = true;
  345. stoppedEvent.reason = "step";
  346. }
  347. if (PauseRequest.load()) {
  348. ClearStepRequests();
  349. waitSem = true;
  350. stoppedEvent.reason = "pause";
  351. }
  352. if (waitSem) {
  353. Session->send(stoppedEvent);
  354. ContinueSem->Wait();
  355. }
  356. }
  357. void cmDebuggerAdapter::OnEndFunctionCall()
  358. {
  359. DefaultThread->PopStackFrame();
  360. }
  361. static std::shared_ptr<cmListFileFunction> listFileFunction;
  362. void cmDebuggerAdapter::OnBeginFileParse(cmMakefile* mf,
  363. std::string const& sourcePath)
  364. {
  365. std::unique_lock<std::mutex> lock(Mutex);
  366. listFileFunction = std::make_shared<cmListFileFunction>(
  367. sourcePath, 0, 0, std::vector<cmListFileArgument>());
  368. DefaultThread->PushStackFrame(mf, sourcePath, *listFileFunction);
  369. }
  370. void cmDebuggerAdapter::OnEndFileParse()
  371. {
  372. DefaultThread->PopStackFrame();
  373. listFileFunction = nullptr;
  374. }
  375. void cmDebuggerAdapter::OnMessageOutput(MessageType t, std::string const& text)
  376. {
  377. cm::optional<dap::StoppedEvent> stoppedEvent =
  378. ExceptionManager->RaiseExceptionIfAny(t, text);
  379. if (stoppedEvent.has_value()) {
  380. stoppedEvent->threadId = DefaultThread->GetId();
  381. Session->send(*stoppedEvent);
  382. ContinueSem->Wait();
  383. }
  384. }
  385. void cmDebuggerAdapter::ClearStepRequests()
  386. {
  387. NextStepFrom.store(INT_MIN);
  388. StepInRequest.store(false);
  389. StepOutDepth.store(INT_MIN);
  390. PauseRequest.store(false);
  391. }
  392. } // namespace cmDebugger