cmDebuggerAdapter.cxx 14 KB

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