cmDebuggerAdapter.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt 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. inline void Notify()
  62. {
  63. std::unique_lock<std::mutex> lock(Mutex);
  64. Count++;
  65. // notify the waiting thread
  66. Cv.notify_one();
  67. }
  68. inline 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](const char* 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](const dap::CMakeInitializeRequest& req) {
  127. SupportsVariableType = req.supportsVariableType.value(false);
  128. dap::CMakeInitializeResponse response;
  129. response.supportsConfigurationDoneRequest = true;
  130. response.cmakeVersion.major = CMake_VERSION_MAJOR;
  131. response.cmakeVersion.minor = CMake_VERSION_MINOR;
  132. response.cmakeVersion.patch = CMake_VERSION_PATCH;
  133. response.cmakeVersion.full = CMake_VERSION;
  134. ExceptionManager->HandleInitializeRequest(response);
  135. return response;
  136. });
  137. // https://microsoft.github.io/debug-adapter-protocol/specification#Events_Initialized
  138. Session->registerSentHandler(
  139. [&](const dap::ResponseOrError<dap::CMakeInitializeResponse>&) {
  140. Session->send(dap::InitializedEvent());
  141. });
  142. // https://microsoft.github.io/debug-adapter-protocol/specification#Requests_Threads
  143. Session->registerHandler([this](const dap::ThreadsRequest& req) {
  144. (void)req;
  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](const dap::StackTraceRequest& request)
  159. -> dap::ResponseOrError<dap::StackTraceResponse> {
  160. std::unique_lock<std::mutex> lock(Mutex);
  161. cm::optional<dap::StackTraceResponse> response =
  162. ThreadManager->GetThreadStackTraceResponse(request.threadId);
  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](const 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](const dap::VariablesRequest& 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](const dap::PauseRequest& req) {
  182. (void)req;
  183. PauseRequest.store(true);
  184. return dap::PauseResponse();
  185. });
  186. // https://microsoft.github.io/debug-adapter-protocol/specification#Requests_Continue
  187. Session->registerHandler([this](const dap::ContinueRequest& req) {
  188. (void)req;
  189. ContinueSem->Notify();
  190. return dap::ContinueResponse();
  191. });
  192. // https://microsoft.github.io/debug-adapter-protocol/specification#Requests_Next
  193. Session->registerHandler([this](const dap::NextRequest& req) {
  194. (void)req;
  195. NextStepFrom.store(DefaultThread->GetStackFrameSize());
  196. ContinueSem->Notify();
  197. return dap::NextResponse();
  198. });
  199. // https://microsoft.github.io/debug-adapter-protocol/specification#Requests_StepIn
  200. Session->registerHandler([this](const dap::StepInRequest& req) {
  201. (void)req;
  202. // This would stop after stepped in, single line stepped or stepped out.
  203. StepInRequest.store(true);
  204. ContinueSem->Notify();
  205. return dap::StepInResponse();
  206. });
  207. // https://microsoft.github.io/debug-adapter-protocol/specification#Requests_StepOut
  208. Session->registerHandler([this](const dap::StepOutRequest& req) {
  209. (void)req;
  210. StepOutDepth.store(DefaultThread->GetStackFrameSize() - 1);
  211. ContinueSem->Notify();
  212. return dap::StepOutResponse();
  213. });
  214. // https://microsoft.github.io/debug-adapter-protocol/specification#Requests_Launch
  215. Session->registerHandler([](const dap::LaunchRequest& req) {
  216. (void)req;
  217. return dap::LaunchResponse();
  218. });
  219. // Handler for disconnect requests
  220. Session->registerHandler([this](const dap::DisconnectRequest& request) {
  221. (void)request;
  222. BreakpointManager->ClearAll();
  223. ExceptionManager->ClearAll();
  224. ClearStepRequests();
  225. ContinueSem->Notify();
  226. DisconnectEvent->Fire();
  227. SessionActive.store(false);
  228. return dap::DisconnectResponse();
  229. });
  230. Session->registerHandler([this](const dap::EvaluateRequest& request) {
  231. dap::EvaluateResponse response;
  232. if (request.frameId.has_value()) {
  233. std::shared_ptr<cmDebuggerStackFrame> frame =
  234. DefaultThread->GetStackFrame(request.frameId.value());
  235. auto var = frame->GetMakefile()->GetDefinition(request.expression);
  236. if (var) {
  237. response.type = "string";
  238. response.result = var;
  239. return response;
  240. }
  241. }
  242. return response;
  243. });
  244. // The ConfigurationDone request is made by the client once all configuration
  245. // requests have been made.
  246. // https://microsoft.github.io/debug-adapter-protocol/specification#Requests_ConfigurationDone
  247. Session->registerHandler([this](const dap::ConfigurationDoneRequest& req) {
  248. (void)req;
  249. ConfigurationDoneEvent->Fire();
  250. return dap::ConfigurationDoneResponse();
  251. });
  252. std::string errorMessage;
  253. if (!Connection->StartListening(errorMessage)) {
  254. throw std::runtime_error(errorMessage);
  255. }
  256. // Connect to the client. Write a well-known message to stdout so that
  257. // clients know it is safe to attempt to connect.
  258. std::cout << "Waiting for debugger client to connect..." << std::endl;
  259. Connection->WaitForConnection();
  260. std::cout << "Debugger client connected." << std::endl;
  261. if (SessionLog) {
  262. Session->connect(spy(Connection->GetReader(), SessionLog),
  263. spy(Connection->GetWriter(), SessionLog));
  264. } else {
  265. Session->connect(Connection->GetReader(), Connection->GetWriter());
  266. }
  267. // Start the processing thread.
  268. SessionThread = std::thread([this] {
  269. while (SessionActive.load()) {
  270. if (auto payload = Session->getPayload()) {
  271. payload();
  272. }
  273. }
  274. });
  275. ConfigurationDoneEvent->Wait();
  276. DefaultThread = ThreadManager->StartThread("CMake script");
  277. dap::ThreadEvent threadEvent;
  278. threadEvent.reason = "started";
  279. threadEvent.threadId = DefaultThread->GetId();
  280. Session->send(threadEvent);
  281. }
  282. cmDebuggerAdapter::~cmDebuggerAdapter()
  283. {
  284. if (SessionThread.joinable()) {
  285. SessionThread.join();
  286. }
  287. Session.reset(nullptr);
  288. if (SessionLog) {
  289. SessionLog->close();
  290. }
  291. }
  292. void cmDebuggerAdapter::ReportExitCode(int exitCode)
  293. {
  294. ThreadManager->EndThread(DefaultThread);
  295. dap::ThreadEvent threadEvent;
  296. threadEvent.reason = "exited";
  297. threadEvent.threadId = DefaultThread->GetId();
  298. DefaultThread.reset();
  299. dap::ExitedEvent exitEvent;
  300. exitEvent.exitCode = exitCode;
  301. dap::TerminatedEvent terminatedEvent;
  302. if (SessionActive.load()) {
  303. Session->send(threadEvent);
  304. Session->send(exitEvent);
  305. Session->send(terminatedEvent);
  306. }
  307. // Wait until disconnected or error.
  308. DisconnectEvent->Wait();
  309. }
  310. void cmDebuggerAdapter::OnFileParsedSuccessfully(
  311. std::string const& sourcePath,
  312. std::vector<cmListFileFunction> const& functions)
  313. {
  314. BreakpointManager->SourceFileLoaded(sourcePath, functions);
  315. }
  316. void cmDebuggerAdapter::OnBeginFunctionCall(cmMakefile* mf,
  317. std::string const& sourcePath,
  318. cmListFileFunction const& lff)
  319. {
  320. std::unique_lock<std::mutex> lock(Mutex);
  321. DefaultThread->PushStackFrame(mf, sourcePath, lff);
  322. if (lff.Line() == 0) {
  323. // File just loaded, continue to first valid function call.
  324. return;
  325. }
  326. auto hits = BreakpointManager->GetBreakpoints(sourcePath, lff.Line());
  327. lock.unlock();
  328. bool waitSem = false;
  329. dap::StoppedEvent stoppedEvent;
  330. stoppedEvent.allThreadsStopped = true;
  331. stoppedEvent.threadId = DefaultThread->GetId();
  332. if (!hits.empty()) {
  333. ClearStepRequests();
  334. waitSem = true;
  335. dap::array<dap::integer> hitBreakpoints;
  336. hitBreakpoints.resize(hits.size());
  337. std::transform(hits.begin(), hits.end(), hitBreakpoints.begin(),
  338. [&](const int64_t& id) { return dap::integer(id); });
  339. stoppedEvent.reason = "breakpoint";
  340. stoppedEvent.hitBreakpointIds = hitBreakpoints;
  341. }
  342. if (long(DefaultThread->GetStackFrameSize()) <= NextStepFrom.load() ||
  343. StepInRequest.load() ||
  344. long(DefaultThread->GetStackFrameSize()) <= StepOutDepth.load()) {
  345. ClearStepRequests();
  346. waitSem = true;
  347. stoppedEvent.reason = "step";
  348. }
  349. if (PauseRequest.load()) {
  350. ClearStepRequests();
  351. waitSem = true;
  352. stoppedEvent.reason = "pause";
  353. }
  354. if (waitSem) {
  355. Session->send(stoppedEvent);
  356. ContinueSem->Wait();
  357. }
  358. }
  359. void cmDebuggerAdapter::OnEndFunctionCall()
  360. {
  361. DefaultThread->PopStackFrame();
  362. }
  363. static std::shared_ptr<cmListFileFunction> listFileFunction;
  364. void cmDebuggerAdapter::OnBeginFileParse(cmMakefile* mf,
  365. std::string const& sourcePath)
  366. {
  367. std::unique_lock<std::mutex> lock(Mutex);
  368. listFileFunction = std::make_shared<cmListFileFunction>(
  369. sourcePath, 0, 0, std::vector<cmListFileArgument>());
  370. DefaultThread->PushStackFrame(mf, sourcePath, *listFileFunction);
  371. }
  372. void cmDebuggerAdapter::OnEndFileParse()
  373. {
  374. DefaultThread->PopStackFrame();
  375. listFileFunction = nullptr;
  376. }
  377. void cmDebuggerAdapter::OnMessageOutput(MessageType t, std::string const& text)
  378. {
  379. cm::optional<dap::StoppedEvent> stoppedEvent =
  380. ExceptionManager->RaiseExceptionIfAny(t, text);
  381. if (stoppedEvent.has_value()) {
  382. stoppedEvent->threadId = DefaultThread->GetId();
  383. Session->send(*stoppedEvent);
  384. ContinueSem->Wait();
  385. }
  386. }
  387. void cmDebuggerAdapter::ClearStepRequests()
  388. {
  389. NextStepFrom.store(INT_MIN);
  390. StepInRequest.store(false);
  391. StepOutDepth.store(INT_MIN);
  392. PauseRequest.store(false);
  393. }
  394. } // namespace cmDebugger