Coverage Report

Created: 2026-02-09 06:05

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