[NFC][Coroutines] Use structured binding with llvm::enumerate in CoroSplit (#116879)
[llvm-project.git] / lldb / tools / lldb-dap / lldb-dap.cpp
blob3bfc578806021ee4f001496bf1931b4206c2b06e
1 //===-- lldb-dap.cpp -----------------------------------------*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
9 #include "DAP.h"
10 #include "FifoFiles.h"
11 #include "JSONUtils.h"
12 #include "LLDBUtils.h"
13 #include "OutputRedirector.h"
14 #include "RunInTerminal.h"
15 #include "Watchpoint.h"
16 #include "lldb/API/SBDeclaration.h"
17 #include "lldb/API/SBInstruction.h"
18 #include "lldb/API/SBListener.h"
19 #include "lldb/API/SBMemoryRegionInfo.h"
20 #include "lldb/API/SBStream.h"
21 #include "lldb/API/SBStringList.h"
22 #include "lldb/Host/Config.h"
23 #include "llvm/ADT/ArrayRef.h"
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/DenseSet.h"
26 #include "llvm/ADT/ScopeExit.h"
27 #include "llvm/ADT/StringExtras.h"
28 #include "llvm/Option/Arg.h"
29 #include "llvm/Option/ArgList.h"
30 #include "llvm/Option/OptTable.h"
31 #include "llvm/Option/Option.h"
32 #include "llvm/Support/Base64.h"
33 #include "llvm/Support/Errno.h"
34 #include "llvm/Support/FileSystem.h"
35 #include "llvm/Support/InitLLVM.h"
36 #include "llvm/Support/Path.h"
37 #include "llvm/Support/PrettyStackTrace.h"
38 #include "llvm/Support/raw_ostream.h"
39 #include <algorithm>
40 #include <array>
41 #include <cassert>
42 #include <climits>
43 #include <cstdarg>
44 #include <cstdio>
45 #include <cstdlib>
46 #include <cstring>
47 #include <map>
48 #include <memory>
49 #include <optional>
50 #include <set>
51 #include <sys/stat.h>
52 #include <sys/types.h>
53 #include <thread>
54 #include <vector>
56 #if defined(_WIN32)
57 // We need to #define NOMINMAX in order to skip `min()` and `max()` macro
58 // definitions that conflict with other system headers.
59 // We also need to #undef GetObject (which is defined to GetObjectW) because
60 // the JSON code we use also has methods named `GetObject()` and we conflict
61 // against these.
62 #define NOMINMAX
63 #include <windows.h>
64 #undef GetObject
65 #include <io.h>
66 #else
67 #include <netinet/in.h>
68 #include <sys/socket.h>
69 #include <unistd.h>
70 #endif
72 #if defined(__linux__)
73 #include <sys/prctl.h>
74 #endif
76 #if defined(_WIN32)
77 #ifndef PATH_MAX
78 #define PATH_MAX MAX_PATH
79 #endif
80 typedef int socklen_t;
81 #endif
83 using namespace lldb_dap;
85 namespace {
86 using namespace llvm::opt;
88 enum ID {
89 OPT_INVALID = 0, // This is not an option ID.
90 #define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
91 #include "Options.inc"
92 #undef OPTION
95 #define PREFIX(NAME, VALUE) \
96 static constexpr llvm::StringLiteral NAME##_init[] = VALUE; \
97 static constexpr llvm::ArrayRef<llvm::StringLiteral> NAME( \
98 NAME##_init, std::size(NAME##_init) - 1);
99 #include "Options.inc"
100 #undef PREFIX
102 static constexpr llvm::opt::OptTable::Info InfoTable[] = {
103 #define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
104 #include "Options.inc"
105 #undef OPTION
107 class LLDBDAPOptTable : public llvm::opt::GenericOptTable {
108 public:
109 LLDBDAPOptTable() : llvm::opt::GenericOptTable(InfoTable, true) {}
112 typedef void (*RequestCallback)(const llvm::json::Object &command);
114 enum LaunchMethod { Launch, Attach, AttachForSuspendedLaunch };
116 /// Page size used for reporting addtional frames in the 'stackTrace' request.
117 constexpr int StackPageSize = 20;
119 /// Prints a welcome message on the editor if the preprocessor variable
120 /// LLDB_DAP_WELCOME_MESSAGE is defined.
121 static void PrintWelcomeMessage(DAP &dap) {
122 #ifdef LLDB_DAP_WELCOME_MESSAGE
123 dap.SendOutput(OutputType::Console, LLDB_DAP_WELCOME_MESSAGE);
124 #endif
127 lldb::SBValueList *GetTopLevelScope(DAP &dap, int64_t variablesReference) {
128 switch (variablesReference) {
129 case VARREF_LOCALS:
130 return &dap.variables.locals;
131 case VARREF_GLOBALS:
132 return &dap.variables.globals;
133 case VARREF_REGS:
134 return &dap.variables.registers;
135 default:
136 return nullptr;
140 SOCKET AcceptConnection(DAP &dap, int portno) {
141 // Accept a socket connection from any host on "portno".
142 SOCKET newsockfd = -1;
143 struct sockaddr_in serv_addr, cli_addr;
144 SOCKET sockfd = socket(AF_INET, SOCK_STREAM, 0);
145 if (sockfd < 0) {
146 if (dap.log)
147 *dap.log << "error: opening socket (" << strerror(errno) << ")"
148 << std::endl;
149 } else {
150 memset((char *)&serv_addr, 0, sizeof(serv_addr));
151 serv_addr.sin_family = AF_INET;
152 // serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
153 serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
154 serv_addr.sin_port = htons(portno);
155 if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
156 if (dap.log)
157 *dap.log << "error: binding socket (" << strerror(errno) << ")"
158 << std::endl;
159 } else {
160 listen(sockfd, 5);
161 socklen_t clilen = sizeof(cli_addr);
162 newsockfd =
163 llvm::sys::RetryAfterSignal(static_cast<SOCKET>(-1), accept, sockfd,
164 (struct sockaddr *)&cli_addr, &clilen);
165 if (newsockfd < 0)
166 if (dap.log)
167 *dap.log << "error: accept (" << strerror(errno) << ")" << std::endl;
169 #if defined(_WIN32)
170 closesocket(sockfd);
171 #else
172 close(sockfd);
173 #endif
175 return newsockfd;
178 std::vector<const char *> MakeArgv(const llvm::ArrayRef<std::string> &strs) {
179 // Create and return an array of "const char *", one for each C string in
180 // "strs" and terminate the list with a NULL. This can be used for argument
181 // vectors (argv) or environment vectors (envp) like those passed to the
182 // "main" function in C programs.
183 std::vector<const char *> argv;
184 for (const auto &s : strs)
185 argv.push_back(s.c_str());
186 argv.push_back(nullptr);
187 return argv;
190 // Send a "exited" event to indicate the process has exited.
191 void SendProcessExitedEvent(DAP &dap, lldb::SBProcess &process) {
192 llvm::json::Object event(CreateEventObject("exited"));
193 llvm::json::Object body;
194 body.try_emplace("exitCode", (int64_t)process.GetExitStatus());
195 event.try_emplace("body", std::move(body));
196 dap.SendJSON(llvm::json::Value(std::move(event)));
199 void SendThreadExitedEvent(DAP &dap, lldb::tid_t tid) {
200 llvm::json::Object event(CreateEventObject("thread"));
201 llvm::json::Object body;
202 body.try_emplace("reason", "exited");
203 body.try_emplace("threadId", (int64_t)tid);
204 event.try_emplace("body", std::move(body));
205 dap.SendJSON(llvm::json::Value(std::move(event)));
208 // Send a "continued" event to indicate the process is in the running state.
209 void SendContinuedEvent(DAP &dap) {
210 lldb::SBProcess process = dap.target.GetProcess();
211 if (!process.IsValid()) {
212 return;
215 // If the focus thread is not set then we haven't reported any thread status
216 // to the client, so nothing to report.
217 if (!dap.configuration_done_sent || dap.focus_tid == LLDB_INVALID_THREAD_ID) {
218 return;
221 llvm::json::Object event(CreateEventObject("continued"));
222 llvm::json::Object body;
223 body.try_emplace("threadId", (int64_t)dap.focus_tid);
224 body.try_emplace("allThreadsContinued", true);
225 event.try_emplace("body", std::move(body));
226 dap.SendJSON(llvm::json::Value(std::move(event)));
229 // Send a "terminated" event to indicate the process is done being
230 // debugged.
231 void SendTerminatedEvent(DAP &dap) {
232 // Prevent races if the process exits while we're being asked to disconnect.
233 llvm::call_once(dap.terminated_event_flag, [&] {
234 dap.RunTerminateCommands();
235 // Send a "terminated" event
236 llvm::json::Object event(CreateTerminatedEventObject(dap.target));
237 dap.SendJSON(llvm::json::Value(std::move(event)));
241 // Send a thread stopped event for all threads as long as the process
242 // is stopped.
243 void SendThreadStoppedEvent(DAP &dap) {
244 lldb::SBProcess process = dap.target.GetProcess();
245 if (process.IsValid()) {
246 auto state = process.GetState();
247 if (state == lldb::eStateStopped) {
248 llvm::DenseSet<lldb::tid_t> old_thread_ids;
249 old_thread_ids.swap(dap.thread_ids);
250 uint32_t stop_id = process.GetStopID();
251 const uint32_t num_threads = process.GetNumThreads();
253 // First make a pass through the threads to see if the focused thread
254 // has a stop reason. In case the focus thread doesn't have a stop
255 // reason, remember the first thread that has a stop reason so we can
256 // set it as the focus thread if below if needed.
257 lldb::tid_t first_tid_with_reason = LLDB_INVALID_THREAD_ID;
258 uint32_t num_threads_with_reason = 0;
259 bool focus_thread_exists = false;
260 for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) {
261 lldb::SBThread thread = process.GetThreadAtIndex(thread_idx);
262 const lldb::tid_t tid = thread.GetThreadID();
263 const bool has_reason = ThreadHasStopReason(thread);
264 // If the focus thread doesn't have a stop reason, clear the thread ID
265 if (tid == dap.focus_tid) {
266 focus_thread_exists = true;
267 if (!has_reason)
268 dap.focus_tid = LLDB_INVALID_THREAD_ID;
270 if (has_reason) {
271 ++num_threads_with_reason;
272 if (first_tid_with_reason == LLDB_INVALID_THREAD_ID)
273 first_tid_with_reason = tid;
277 // We will have cleared dap.focus_tid if the focus thread doesn't have
278 // a stop reason, so if it was cleared, or wasn't set, or doesn't exist,
279 // then set the focus thread to the first thread with a stop reason.
280 if (!focus_thread_exists || dap.focus_tid == LLDB_INVALID_THREAD_ID)
281 dap.focus_tid = first_tid_with_reason;
283 // If no threads stopped with a reason, then report the first one so
284 // we at least let the UI know we stopped.
285 if (num_threads_with_reason == 0) {
286 lldb::SBThread thread = process.GetThreadAtIndex(0);
287 dap.focus_tid = thread.GetThreadID();
288 dap.SendJSON(CreateThreadStopped(dap, thread, stop_id));
289 } else {
290 for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) {
291 lldb::SBThread thread = process.GetThreadAtIndex(thread_idx);
292 dap.thread_ids.insert(thread.GetThreadID());
293 if (ThreadHasStopReason(thread)) {
294 dap.SendJSON(CreateThreadStopped(dap, thread, stop_id));
299 for (auto tid : old_thread_ids) {
300 auto end = dap.thread_ids.end();
301 auto pos = dap.thread_ids.find(tid);
302 if (pos == end)
303 SendThreadExitedEvent(dap, tid);
305 } else {
306 if (dap.log)
307 *dap.log << "error: SendThreadStoppedEvent() when process"
308 " isn't stopped ("
309 << lldb::SBDebugger::StateAsCString(state) << ')' << std::endl;
311 } else {
312 if (dap.log)
313 *dap.log << "error: SendThreadStoppedEvent() invalid process"
314 << std::endl;
316 dap.RunStopCommands();
319 // "ProcessEvent": {
320 // "allOf": [
321 // { "$ref": "#/definitions/Event" },
322 // {
323 // "type": "object",
324 // "description": "Event message for 'process' event type. The event
325 // indicates that the debugger has begun debugging a
326 // new process. Either one that it has launched, or one
327 // that it has attached to.",
328 // "properties": {
329 // "event": {
330 // "type": "string",
331 // "enum": [ "process" ]
332 // },
333 // "body": {
334 // "type": "object",
335 // "properties": {
336 // "name": {
337 // "type": "string",
338 // "description": "The logical name of the process. This is
339 // usually the full path to process's executable
340 // file. Example: /home/myproj/program.js."
341 // },
342 // "systemProcessId": {
343 // "type": "integer",
344 // "description": "The system process id of the debugged process.
345 // This property will be missing for non-system
346 // processes."
347 // },
348 // "isLocalProcess": {
349 // "type": "boolean",
350 // "description": "If true, the process is running on the same
351 // computer as the debug adapter."
352 // },
353 // "startMethod": {
354 // "type": "string",
355 // "enum": [ "launch", "attach", "attachForSuspendedLaunch" ],
356 // "description": "Describes how the debug engine started
357 // debugging this process.",
358 // "enumDescriptions": [
359 // "Process was launched under the debugger.",
360 // "Debugger attached to an existing process.",
361 // "A project launcher component has launched a new process in
362 // a suspended state and then asked the debugger to attach."
363 // ]
364 // }
365 // },
366 // "required": [ "name" ]
367 // }
368 // },
369 // "required": [ "event", "body" ]
370 // }
371 // ]
372 // }
373 void SendProcessEvent(DAP &dap, LaunchMethod launch_method) {
374 lldb::SBFileSpec exe_fspec = dap.target.GetExecutable();
375 char exe_path[PATH_MAX];
376 exe_fspec.GetPath(exe_path, sizeof(exe_path));
377 llvm::json::Object event(CreateEventObject("process"));
378 llvm::json::Object body;
379 EmplaceSafeString(body, "name", std::string(exe_path));
380 const auto pid = dap.target.GetProcess().GetProcessID();
381 body.try_emplace("systemProcessId", (int64_t)pid);
382 body.try_emplace("isLocalProcess", true);
383 const char *startMethod = nullptr;
384 switch (launch_method) {
385 case Launch:
386 startMethod = "launch";
387 break;
388 case Attach:
389 startMethod = "attach";
390 break;
391 case AttachForSuspendedLaunch:
392 startMethod = "attachForSuspendedLaunch";
393 break;
395 body.try_emplace("startMethod", startMethod);
396 event.try_emplace("body", std::move(body));
397 dap.SendJSON(llvm::json::Value(std::move(event)));
400 // Grab any STDOUT and STDERR from the process and send it up to VS Code
401 // via an "output" event to the "stdout" and "stderr" categories.
402 void SendStdOutStdErr(DAP &dap, lldb::SBProcess &process) {
403 char buffer[OutputBufferSize];
404 size_t count;
405 while ((count = process.GetSTDOUT(buffer, sizeof(buffer))) > 0)
406 dap.SendOutput(OutputType::Stdout, llvm::StringRef(buffer, count));
407 while ((count = process.GetSTDERR(buffer, sizeof(buffer))) > 0)
408 dap.SendOutput(OutputType::Stderr, llvm::StringRef(buffer, count));
411 void ProgressEventThreadFunction(DAP &dap) {
412 lldb::SBListener listener("lldb-dap.progress.listener");
413 dap.debugger.GetBroadcaster().AddListener(
414 listener, lldb::SBDebugger::eBroadcastBitProgress);
415 dap.broadcaster.AddListener(listener, eBroadcastBitStopProgressThread);
416 lldb::SBEvent event;
417 bool done = false;
418 while (!done) {
419 if (listener.WaitForEvent(1, event)) {
420 const auto event_mask = event.GetType();
421 if (event.BroadcasterMatchesRef(dap.broadcaster)) {
422 if (event_mask & eBroadcastBitStopProgressThread) {
423 done = true;
425 } else {
426 uint64_t progress_id = 0;
427 uint64_t completed = 0;
428 uint64_t total = 0;
429 bool is_debugger_specific = false;
430 const char *message = lldb::SBDebugger::GetProgressFromEvent(
431 event, progress_id, completed, total, is_debugger_specific);
432 if (message)
433 dap.SendProgressEvent(progress_id, message, completed, total);
439 // All events from the debugger, target, process, thread and frames are
440 // received in this function that runs in its own thread. We are using a
441 // "FILE *" to output packets back to VS Code and they have mutexes in them
442 // them prevent multiple threads from writing simultaneously so no locking
443 // is required.
444 void EventThreadFunction(DAP &dap) {
445 lldb::SBEvent event;
446 lldb::SBListener listener = dap.debugger.GetListener();
447 bool done = false;
448 while (!done) {
449 if (listener.WaitForEvent(1, event)) {
450 const auto event_mask = event.GetType();
451 if (lldb::SBProcess::EventIsProcessEvent(event)) {
452 lldb::SBProcess process = lldb::SBProcess::GetProcessFromEvent(event);
453 if (event_mask & lldb::SBProcess::eBroadcastBitStateChanged) {
454 auto state = lldb::SBProcess::GetStateFromEvent(event);
455 switch (state) {
456 case lldb::eStateInvalid:
457 // Not a state event
458 break;
459 case lldb::eStateUnloaded:
460 break;
461 case lldb::eStateConnected:
462 break;
463 case lldb::eStateAttaching:
464 break;
465 case lldb::eStateLaunching:
466 break;
467 case lldb::eStateStepping:
468 break;
469 case lldb::eStateCrashed:
470 break;
471 case lldb::eStateDetached:
472 break;
473 case lldb::eStateSuspended:
474 break;
475 case lldb::eStateStopped:
476 // We launch and attach in synchronous mode then the first stop
477 // event will not be delivered. If we use "launchCommands" during a
478 // launch or "attachCommands" during an attach we might some process
479 // stop events which we do not want to send an event for. We will
480 // manually send a stopped event in request_configurationDone(...)
481 // so don't send any before then.
482 if (dap.configuration_done_sent) {
483 // Only report a stopped event if the process was not
484 // automatically restarted.
485 if (!lldb::SBProcess::GetRestartedFromEvent(event)) {
486 SendStdOutStdErr(dap, process);
487 SendThreadStoppedEvent(dap);
490 break;
491 case lldb::eStateRunning:
492 dap.WillContinue();
493 SendContinuedEvent(dap);
494 break;
495 case lldb::eStateExited:
496 lldb::SBStream stream;
497 process.GetStatus(stream);
498 dap.SendOutput(OutputType::Console, stream.GetData());
500 // When restarting, we can get an "exited" event for the process we
501 // just killed with the old PID, or even with no PID. In that case
502 // we don't have to terminate the session.
503 if (process.GetProcessID() == LLDB_INVALID_PROCESS_ID ||
504 process.GetProcessID() == dap.restarting_process_id) {
505 dap.restarting_process_id = LLDB_INVALID_PROCESS_ID;
506 } else {
507 // Run any exit LLDB commands the user specified in the
508 // launch.json
509 dap.RunExitCommands();
510 SendProcessExitedEvent(dap, process);
511 SendTerminatedEvent(dap);
512 done = true;
514 break;
516 } else if ((event_mask & lldb::SBProcess::eBroadcastBitSTDOUT) ||
517 (event_mask & lldb::SBProcess::eBroadcastBitSTDERR)) {
518 SendStdOutStdErr(dap, process);
520 } else if (lldb::SBBreakpoint::EventIsBreakpointEvent(event)) {
521 if (event_mask & lldb::SBTarget::eBroadcastBitBreakpointChanged) {
522 auto event_type =
523 lldb::SBBreakpoint::GetBreakpointEventTypeFromEvent(event);
524 auto bp = Breakpoint(
525 dap, lldb::SBBreakpoint::GetBreakpointFromEvent(event));
526 // If the breakpoint was originated from the IDE, it will have the
527 // BreakpointBase::GetBreakpointLabel() label attached. Regardless
528 // of wether the locations were added or removed, the breakpoint
529 // ins't going away, so we the reason is always "changed".
530 if ((event_type & lldb::eBreakpointEventTypeLocationsAdded ||
531 event_type & lldb::eBreakpointEventTypeLocationsRemoved) &&
532 bp.MatchesName(BreakpointBase::GetBreakpointLabel())) {
533 auto bp_event = CreateEventObject("breakpoint");
534 llvm::json::Object body;
535 // As VSCode already knows the path of this breakpoint, we don't
536 // need to send it back as part of a "changed" event. This
537 // prevent us from sending to VSCode paths that should be source
538 // mapped. Note that CreateBreakpoint doesn't apply source mapping.
539 // Besides, the current implementation of VSCode ignores the
540 // "source" element of breakpoint events.
541 llvm::json::Value source_bp = CreateBreakpoint(&bp);
542 source_bp.getAsObject()->erase("source");
544 body.try_emplace("breakpoint", source_bp);
545 body.try_emplace("reason", "changed");
546 bp_event.try_emplace("body", std::move(body));
547 dap.SendJSON(llvm::json::Value(std::move(bp_event)));
550 } else if (event.BroadcasterMatchesRef(dap.broadcaster)) {
551 if (event_mask & eBroadcastBitStopEventThread) {
552 done = true;
559 lldb::SBValue FindVariable(DAP &dap, uint64_t variablesReference,
560 llvm::StringRef name) {
561 lldb::SBValue variable;
562 if (lldb::SBValueList *top_scope =
563 GetTopLevelScope(dap, variablesReference)) {
564 bool is_duplicated_variable_name = name.contains(" @");
565 // variablesReference is one of our scopes, not an actual variable it is
566 // asking for a variable in locals or globals or registers
567 int64_t end_idx = top_scope->GetSize();
568 // Searching backward so that we choose the variable in closest scope
569 // among variables of the same name.
570 for (int64_t i = end_idx - 1; i >= 0; --i) {
571 lldb::SBValue curr_variable = top_scope->GetValueAtIndex(i);
572 std::string variable_name = CreateUniqueVariableNameForDisplay(
573 curr_variable, is_duplicated_variable_name);
574 if (variable_name == name) {
575 variable = curr_variable;
576 break;
579 } else {
580 // This is not under the globals or locals scope, so there are no duplicated
581 // names.
583 // We have a named item within an actual variable so we need to find it
584 // withing the container variable by name.
585 lldb::SBValue container = dap.variables.GetVariable(variablesReference);
586 variable = container.GetChildMemberWithName(name.data());
587 if (!variable.IsValid()) {
588 if (name.starts_with("[")) {
589 llvm::StringRef index_str(name.drop_front(1));
590 uint64_t index = 0;
591 if (!index_str.consumeInteger(0, index)) {
592 if (index_str == "]")
593 variable = container.GetChildAtIndex(index);
598 return variable;
601 // Both attach and launch take a either a sourcePath or sourceMap
602 // argument (or neither), from which we need to set the target.source-map.
603 void SetSourceMapFromArguments(DAP &dap, const llvm::json::Object &arguments) {
604 const char *sourceMapHelp =
605 "source must be be an array of two-element arrays, "
606 "each containing a source and replacement path string.\n";
608 std::string sourceMapCommand;
609 llvm::raw_string_ostream strm(sourceMapCommand);
610 strm << "settings set target.source-map ";
611 const auto sourcePath = GetString(arguments, "sourcePath");
613 // sourceMap is the new, more general form of sourcePath and overrides it.
614 constexpr llvm::StringRef sourceMapKey = "sourceMap";
616 if (const auto *sourceMapArray = arguments.getArray(sourceMapKey)) {
617 for (const auto &value : *sourceMapArray) {
618 const auto *mapping = value.getAsArray();
619 if (mapping == nullptr || mapping->size() != 2 ||
620 (*mapping)[0].kind() != llvm::json::Value::String ||
621 (*mapping)[1].kind() != llvm::json::Value::String) {
622 dap.SendOutput(OutputType::Console, llvm::StringRef(sourceMapHelp));
623 return;
625 const auto mapFrom = GetAsString((*mapping)[0]);
626 const auto mapTo = GetAsString((*mapping)[1]);
627 strm << "\"" << mapFrom << "\" \"" << mapTo << "\" ";
629 } else if (const auto *sourceMapObj = arguments.getObject(sourceMapKey)) {
630 for (const auto &[key, value] : *sourceMapObj) {
631 if (value.kind() == llvm::json::Value::String) {
632 strm << "\"" << key.str() << "\" \"" << GetAsString(value) << "\" ";
635 } else {
636 if (ObjectContainsKey(arguments, sourceMapKey)) {
637 dap.SendOutput(OutputType::Console, llvm::StringRef(sourceMapHelp));
638 return;
640 if (sourcePath.empty())
641 return;
642 // Do any source remapping needed before we create our targets
643 strm << "\".\" \"" << sourcePath << "\"";
645 if (!sourceMapCommand.empty()) {
646 dap.RunLLDBCommands("Setting source map:", {sourceMapCommand});
650 // Fill in the stack frames of the thread.
652 // Threads stacks may contain runtime specific extended backtraces, when
653 // constructing a stack trace first report the full thread stack trace then
654 // perform a breadth first traversal of any extended backtrace frames.
656 // For example:
658 // Thread (id=th0) stack=[s0, s1, s2, s3]
659 // \ Extended backtrace "libdispatch" Thread (id=th1) stack=[s0, s1]
660 // \ Extended backtrace "libdispatch" Thread (id=th2) stack=[s0, s1]
661 // \ Extended backtrace "Application Specific Backtrace" Thread (id=th3)
662 // stack=[s0, s1, s2]
664 // Which will flatten into:
666 // 0. th0->s0
667 // 1. th0->s1
668 // 2. th0->s2
669 // 3. th0->s3
670 // 4. label - Enqueued from th1, sf=-1, i=-4
671 // 5. th1->s0
672 // 6. th1->s1
673 // 7. label - Enqueued from th2
674 // 8. th2->s0
675 // 9. th2->s1
676 // 10. label - Application Specific Backtrace
677 // 11. th3->s0
678 // 12. th3->s1
679 // 13. th3->s2
681 // s=3,l=3 = [th0->s3, label1, th1->s0]
682 bool FillStackFrames(DAP &dap, lldb::SBThread &thread,
683 llvm::json::Array &stack_frames, int64_t &offset,
684 const int64_t start_frame, const int64_t levels) {
685 bool reached_end_of_stack = false;
686 for (int64_t i = start_frame;
687 static_cast<int64_t>(stack_frames.size()) < levels; i++) {
688 if (i == -1) {
689 stack_frames.emplace_back(
690 CreateExtendedStackFrameLabel(thread, dap.frame_format));
691 continue;
694 lldb::SBFrame frame = thread.GetFrameAtIndex(i);
695 if (!frame.IsValid()) {
696 offset += thread.GetNumFrames() + 1 /* label between threads */;
697 reached_end_of_stack = true;
698 break;
701 stack_frames.emplace_back(CreateStackFrame(frame, dap.frame_format));
704 if (dap.display_extended_backtrace && reached_end_of_stack) {
705 // Check for any extended backtraces.
706 for (uint32_t bt = 0;
707 bt < thread.GetProcess().GetNumExtendedBacktraceTypes(); bt++) {
708 lldb::SBThread backtrace = thread.GetExtendedBacktraceThread(
709 thread.GetProcess().GetExtendedBacktraceTypeAtIndex(bt));
710 if (!backtrace.IsValid())
711 continue;
713 reached_end_of_stack = FillStackFrames(
714 dap, backtrace, stack_frames, offset,
715 (start_frame - offset) > 0 ? start_frame - offset : -1, levels);
716 if (static_cast<int64_t>(stack_frames.size()) >= levels)
717 break;
721 return reached_end_of_stack;
724 // "AttachRequest": {
725 // "allOf": [ { "$ref": "#/definitions/Request" }, {
726 // "type": "object",
727 // "description": "Attach request; value of command field is 'attach'.",
728 // "properties": {
729 // "command": {
730 // "type": "string",
731 // "enum": [ "attach" ]
732 // },
733 // "arguments": {
734 // "$ref": "#/definitions/AttachRequestArguments"
735 // }
736 // },
737 // "required": [ "command", "arguments" ]
738 // }]
739 // },
740 // "AttachRequestArguments": {
741 // "type": "object",
742 // "description": "Arguments for 'attach' request.\nThe attach request has no
743 // standardized attributes."
744 // },
745 // "AttachResponse": {
746 // "allOf": [ { "$ref": "#/definitions/Response" }, {
747 // "type": "object",
748 // "description": "Response to 'attach' request. This is just an
749 // acknowledgement, so no body field is required."
750 // }]
751 // }
752 void request_attach(DAP &dap, const llvm::json::Object &request) {
753 dap.is_attach = true;
754 dap.last_launch_or_attach_request = request;
755 llvm::json::Object response;
756 lldb::SBError error;
757 FillResponse(request, response);
758 lldb::SBAttachInfo attach_info;
759 const int invalid_port = 0;
760 const auto *arguments = request.getObject("arguments");
761 const lldb::pid_t pid =
762 GetUnsigned(arguments, "pid", LLDB_INVALID_PROCESS_ID);
763 const auto gdb_remote_port =
764 GetUnsigned(arguments, "gdb-remote-port", invalid_port);
765 const auto gdb_remote_hostname =
766 GetString(arguments, "gdb-remote-hostname", "localhost");
767 if (pid != LLDB_INVALID_PROCESS_ID)
768 attach_info.SetProcessID(pid);
769 const auto wait_for = GetBoolean(arguments, "waitFor", false);
770 attach_info.SetWaitForLaunch(wait_for, false /*async*/);
771 dap.init_commands = GetStrings(arguments, "initCommands");
772 dap.pre_run_commands = GetStrings(arguments, "preRunCommands");
773 dap.stop_commands = GetStrings(arguments, "stopCommands");
774 dap.exit_commands = GetStrings(arguments, "exitCommands");
775 dap.terminate_commands = GetStrings(arguments, "terminateCommands");
776 auto attachCommands = GetStrings(arguments, "attachCommands");
777 llvm::StringRef core_file = GetString(arguments, "coreFile");
778 const uint64_t timeout_seconds = GetUnsigned(arguments, "timeout", 30);
779 dap.stop_at_entry =
780 core_file.empty() ? GetBoolean(arguments, "stopOnEntry", false) : true;
781 dap.post_run_commands = GetStrings(arguments, "postRunCommands");
782 const llvm::StringRef debuggerRoot = GetString(arguments, "debuggerRoot");
783 dap.enable_auto_variable_summaries =
784 GetBoolean(arguments, "enableAutoVariableSummaries", false);
785 dap.enable_synthetic_child_debugging =
786 GetBoolean(arguments, "enableSyntheticChildDebugging", false);
787 dap.display_extended_backtrace =
788 GetBoolean(arguments, "displayExtendedBacktrace", false);
789 dap.command_escape_prefix = GetString(arguments, "commandEscapePrefix", "`");
790 dap.SetFrameFormat(GetString(arguments, "customFrameFormat"));
791 dap.SetThreadFormat(GetString(arguments, "customThreadFormat"));
793 PrintWelcomeMessage(dap);
795 // This is a hack for loading DWARF in .o files on Mac where the .o files
796 // in the debug map of the main executable have relative paths which require
797 // the lldb-dap binary to have its working directory set to that relative
798 // root for the .o files in order to be able to load debug info.
799 if (!debuggerRoot.empty())
800 llvm::sys::fs::set_current_path(debuggerRoot);
802 // Run any initialize LLDB commands the user specified in the launch.json
803 if (llvm::Error err = dap.RunInitCommands()) {
804 response["success"] = false;
805 EmplaceSafeString(response, "message", llvm::toString(std::move(err)));
806 dap.SendJSON(llvm::json::Value(std::move(response)));
807 return;
810 SetSourceMapFromArguments(dap, *arguments);
812 lldb::SBError status;
813 dap.SetTarget(dap.CreateTargetFromArguments(*arguments, status));
814 if (status.Fail()) {
815 response["success"] = llvm::json::Value(false);
816 EmplaceSafeString(response, "message", status.GetCString());
817 dap.SendJSON(llvm::json::Value(std::move(response)));
818 return;
821 // Run any pre run LLDB commands the user specified in the launch.json
822 if (llvm::Error err = dap.RunPreRunCommands()) {
823 response["success"] = false;
824 EmplaceSafeString(response, "message", llvm::toString(std::move(err)));
825 dap.SendJSON(llvm::json::Value(std::move(response)));
826 return;
829 if ((pid == LLDB_INVALID_PROCESS_ID || gdb_remote_port == invalid_port) &&
830 wait_for) {
831 char attach_msg[256];
832 auto attach_msg_len = snprintf(attach_msg, sizeof(attach_msg),
833 "Waiting to attach to \"%s\"...",
834 dap.target.GetExecutable().GetFilename());
835 dap.SendOutput(OutputType::Console,
836 llvm::StringRef(attach_msg, attach_msg_len));
838 if (attachCommands.empty()) {
839 // No "attachCommands", just attach normally.
840 // Disable async events so the attach will be successful when we return from
841 // the launch call and the launch will happen synchronously
842 dap.debugger.SetAsync(false);
843 if (core_file.empty()) {
844 if ((pid != LLDB_INVALID_PROCESS_ID) &&
845 (gdb_remote_port != invalid_port)) {
846 // If both pid and port numbers are specified.
847 error.SetErrorString("The user can't specify both pid and port");
848 } else if (gdb_remote_port != invalid_port) {
849 // If port is specified and pid is not.
850 lldb::SBListener listener = dap.debugger.GetListener();
852 // If the user hasn't provided the hostname property, default localhost
853 // being used.
854 std::string connect_url =
855 llvm::formatv("connect://{0}:", gdb_remote_hostname);
856 connect_url += std::to_string(gdb_remote_port);
857 dap.target.ConnectRemote(listener, connect_url.c_str(), "gdb-remote",
858 error);
859 } else {
860 // Attach by process name or id.
861 dap.target.Attach(attach_info, error);
863 } else
864 dap.target.LoadCore(core_file.data(), error);
865 // Reenable async events
866 dap.debugger.SetAsync(true);
867 } else {
868 // We have "attachCommands" that are a set of commands that are expected
869 // to execute the commands after which a process should be created. If there
870 // is no valid process after running these commands, we have failed.
871 if (llvm::Error err = dap.RunAttachCommands(attachCommands)) {
872 response["success"] = false;
873 EmplaceSafeString(response, "message", llvm::toString(std::move(err)));
874 dap.SendJSON(llvm::json::Value(std::move(response)));
875 return;
877 // The custom commands might have created a new target so we should use the
878 // selected target after these commands are run.
879 dap.target = dap.debugger.GetSelectedTarget();
881 // Make sure the process is attached and stopped before proceeding as the
882 // the launch commands are not run using the synchronous mode.
883 error = dap.WaitForProcessToStop(timeout_seconds);
886 if (error.Success() && core_file.empty()) {
887 auto attached_pid = dap.target.GetProcess().GetProcessID();
888 if (attached_pid == LLDB_INVALID_PROCESS_ID) {
889 if (attachCommands.empty())
890 error.SetErrorString("failed to attach to a process");
891 else
892 error.SetErrorString("attachCommands failed to attach to a process");
896 if (error.Fail()) {
897 response["success"] = llvm::json::Value(false);
898 EmplaceSafeString(response, "message", std::string(error.GetCString()));
899 } else {
900 dap.RunPostRunCommands();
903 dap.SendJSON(llvm::json::Value(std::move(response)));
904 if (error.Success()) {
905 SendProcessEvent(dap, Attach);
906 dap.SendJSON(CreateEventObject("initialized"));
910 // "ContinueRequest": {
911 // "allOf": [ { "$ref": "#/definitions/Request" }, {
912 // "type": "object",
913 // "description": "Continue request; value of command field is 'continue'.
914 // The request starts the debuggee to run again.",
915 // "properties": {
916 // "command": {
917 // "type": "string",
918 // "enum": [ "continue" ]
919 // },
920 // "arguments": {
921 // "$ref": "#/definitions/ContinueArguments"
922 // }
923 // },
924 // "required": [ "command", "arguments" ]
925 // }]
926 // },
927 // "ContinueArguments": {
928 // "type": "object",
929 // "description": "Arguments for 'continue' request.",
930 // "properties": {
931 // "threadId": {
932 // "type": "integer",
933 // "description": "Continue execution for the specified thread (if
934 // possible). If the backend cannot continue on a single
935 // thread but will continue on all threads, it should
936 // set the allThreadsContinued attribute in the response
937 // to true."
938 // }
939 // },
940 // "required": [ "threadId" ]
941 // },
942 // "ContinueResponse": {
943 // "allOf": [ { "$ref": "#/definitions/Response" }, {
944 // "type": "object",
945 // "description": "Response to 'continue' request.",
946 // "properties": {
947 // "body": {
948 // "type": "object",
949 // "properties": {
950 // "allThreadsContinued": {
951 // "type": "boolean",
952 // "description": "If true, the continue request has ignored the
953 // specified thread and continued all threads
954 // instead. If this attribute is missing a value
955 // of 'true' is assumed for backward
956 // compatibility."
957 // }
958 // }
959 // }
960 // },
961 // "required": [ "body" ]
962 // }]
963 // }
964 void request_continue(DAP &dap, const llvm::json::Object &request) {
965 llvm::json::Object response;
966 FillResponse(request, response);
967 lldb::SBProcess process = dap.target.GetProcess();
968 lldb::SBError error = process.Continue();
969 llvm::json::Object body;
970 body.try_emplace("allThreadsContinued", true);
971 response.try_emplace("body", std::move(body));
972 dap.SendJSON(llvm::json::Value(std::move(response)));
975 // "ConfigurationDoneRequest": {
976 // "allOf": [ { "$ref": "#/definitions/Request" }, {
977 // "type": "object",
978 // "description": "ConfigurationDone request; value of command field
979 // is 'configurationDone'.\nThe client of the debug protocol must
980 // send this request at the end of the sequence of configuration
981 // requests (which was started by the InitializedEvent).",
982 // "properties": {
983 // "command": {
984 // "type": "string",
985 // "enum": [ "configurationDone" ]
986 // },
987 // "arguments": {
988 // "$ref": "#/definitions/ConfigurationDoneArguments"
989 // }
990 // },
991 // "required": [ "command" ]
992 // }]
993 // },
994 // "ConfigurationDoneArguments": {
995 // "type": "object",
996 // "description": "Arguments for 'configurationDone' request.\nThe
997 // configurationDone request has no standardized attributes."
998 // },
999 // "ConfigurationDoneResponse": {
1000 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1001 // "type": "object",
1002 // "description": "Response to 'configurationDone' request. This is
1003 // just an acknowledgement, so no body field is required."
1004 // }]
1005 // },
1006 void request_configurationDone(DAP &dap, const llvm::json::Object &request) {
1007 llvm::json::Object response;
1008 FillResponse(request, response);
1009 dap.SendJSON(llvm::json::Value(std::move(response)));
1010 dap.configuration_done_sent = true;
1011 if (dap.stop_at_entry)
1012 SendThreadStoppedEvent(dap);
1013 else
1014 dap.target.GetProcess().Continue();
1017 // "DisconnectRequest": {
1018 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1019 // "type": "object",
1020 // "description": "Disconnect request; value of command field is
1021 // 'disconnect'.",
1022 // "properties": {
1023 // "command": {
1024 // "type": "string",
1025 // "enum": [ "disconnect" ]
1026 // },
1027 // "arguments": {
1028 // "$ref": "#/definitions/DisconnectArguments"
1029 // }
1030 // },
1031 // "required": [ "command" ]
1032 // }]
1033 // },
1034 // "DisconnectArguments": {
1035 // "type": "object",
1036 // "description": "Arguments for 'disconnect' request.",
1037 // "properties": {
1038 // "terminateDebuggee": {
1039 // "type": "boolean",
1040 // "description": "Indicates whether the debuggee should be terminated
1041 // when the debugger is disconnected. If unspecified,
1042 // the debug adapter is free to do whatever it thinks
1043 // is best. A client can only rely on this attribute
1044 // being properly honored if a debug adapter returns
1045 // true for the 'supportTerminateDebuggee' capability."
1046 // },
1047 // "restart": {
1048 // "type": "boolean",
1049 // "description": "Indicates whether the debuggee should be restart
1050 // the process."
1051 // }
1052 // }
1053 // },
1054 // "DisconnectResponse": {
1055 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1056 // "type": "object",
1057 // "description": "Response to 'disconnect' request. This is just an
1058 // acknowledgement, so no body field is required."
1059 // }]
1060 // }
1061 void request_disconnect(DAP &dap, const llvm::json::Object &request) {
1062 llvm::json::Object response;
1063 FillResponse(request, response);
1064 const auto *arguments = request.getObject("arguments");
1066 bool defaultTerminateDebuggee = dap.is_attach ? false : true;
1067 bool terminateDebuggee =
1068 GetBoolean(arguments, "terminateDebuggee", defaultTerminateDebuggee);
1069 lldb::SBProcess process = dap.target.GetProcess();
1070 auto state = process.GetState();
1071 switch (state) {
1072 case lldb::eStateInvalid:
1073 case lldb::eStateUnloaded:
1074 case lldb::eStateDetached:
1075 case lldb::eStateExited:
1076 break;
1077 case lldb::eStateConnected:
1078 case lldb::eStateAttaching:
1079 case lldb::eStateLaunching:
1080 case lldb::eStateStepping:
1081 case lldb::eStateCrashed:
1082 case lldb::eStateSuspended:
1083 case lldb::eStateStopped:
1084 case lldb::eStateRunning:
1085 dap.debugger.SetAsync(false);
1086 lldb::SBError error = terminateDebuggee ? process.Kill() : process.Detach();
1087 if (!error.Success())
1088 EmplaceSafeString(response, "error", error.GetCString());
1089 dap.debugger.SetAsync(true);
1090 break;
1092 SendTerminatedEvent(dap);
1093 dap.SendJSON(llvm::json::Value(std::move(response)));
1094 if (dap.event_thread.joinable()) {
1095 dap.broadcaster.BroadcastEventByType(eBroadcastBitStopEventThread);
1096 dap.event_thread.join();
1098 if (dap.progress_event_thread.joinable()) {
1099 dap.broadcaster.BroadcastEventByType(eBroadcastBitStopProgressThread);
1100 dap.progress_event_thread.join();
1102 dap.disconnecting = true;
1105 // "ExceptionInfoRequest": {
1106 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1107 // "type": "object",
1108 // "description": "Retrieves the details of the exception that
1109 // caused this event to be raised. Clients should only call this request if
1110 // the corresponding capability `supportsExceptionInfoRequest` is true.",
1111 // "properties": {
1112 // "command": {
1113 // "type": "string",
1114 // "enum": [ "exceptionInfo" ]
1115 // },
1116 // "arguments": {
1117 // "$ref": "#/definitions/ExceptionInfoArguments"
1118 // }
1119 // },
1120 // "required": [ "command", "arguments" ]
1121 // }]
1122 // },
1123 // "ExceptionInfoArguments": {
1124 // "type": "object",
1125 // "description": "Arguments for `exceptionInfo` request.",
1126 // "properties": {
1127 // "threadId": {
1128 // "type": "integer",
1129 // "description": "Thread for which exception information should be
1130 // retrieved."
1131 // }
1132 // },
1133 // "required": [ "threadId" ]
1134 // },
1135 // "ExceptionInfoResponse": {
1136 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1137 // "type": "object",
1138 // "description": "Response to `exceptionInfo` request.",
1139 // "properties": {
1140 // "body": {
1141 // "type": "object",
1142 // "properties": {
1143 // "exceptionId": {
1144 // "type": "string",
1145 // "description": "ID of the exception that was thrown."
1146 // },
1147 // "description": {
1148 // "type": "string",
1149 // "description": "Descriptive text for the exception."
1150 // },
1151 // "breakMode": {
1152 // "$ref": "#/definitions/ExceptionBreakMode",
1153 // "description": "Mode that caused the exception notification to
1154 // be raised."
1155 // },
1156 // "details": {
1157 // "$ref": "#/definitions/ExceptionDetails",
1158 // "description": "Detailed information about the exception."
1159 // }
1160 // },
1161 // "required": [ "exceptionId", "breakMode" ]
1162 // }
1163 // },
1164 // "required": [ "body" ]
1165 // }]
1166 // }
1167 // "ExceptionDetails": {
1168 // "type": "object",
1169 // "description": "Detailed information about an exception that has
1170 // occurred.", "properties": {
1171 // "message": {
1172 // "type": "string",
1173 // "description": "Message contained in the exception."
1174 // },
1175 // "typeName": {
1176 // "type": "string",
1177 // "description": "Short type name of the exception object."
1178 // },
1179 // "fullTypeName": {
1180 // "type": "string",
1181 // "description": "Fully-qualified type name of the exception object."
1182 // },
1183 // "evaluateName": {
1184 // "type": "string",
1185 // "description": "An expression that can be evaluated in the current
1186 // scope to obtain the exception object."
1187 // },
1188 // "stackTrace": {
1189 // "type": "string",
1190 // "description": "Stack trace at the time the exception was thrown."
1191 // },
1192 // "innerException": {
1193 // "type": "array",
1194 // "items": {
1195 // "$ref": "#/definitions/ExceptionDetails"
1196 // },
1197 // "description": "Details of the exception contained by this exception,
1198 // if any."
1199 // }
1200 // }
1201 // },
1202 void request_exceptionInfo(DAP &dap, const llvm::json::Object &request) {
1203 llvm::json::Object response;
1204 FillResponse(request, response);
1205 const auto *arguments = request.getObject("arguments");
1206 llvm::json::Object body;
1207 lldb::SBThread thread = dap.GetLLDBThread(*arguments);
1208 if (thread.IsValid()) {
1209 auto stopReason = thread.GetStopReason();
1210 if (stopReason == lldb::eStopReasonSignal)
1211 body.try_emplace("exceptionId", "signal");
1212 else if (stopReason == lldb::eStopReasonBreakpoint) {
1213 ExceptionBreakpoint *exc_bp = dap.GetExceptionBPFromStopReason(thread);
1214 if (exc_bp) {
1215 EmplaceSafeString(body, "exceptionId", exc_bp->filter);
1216 EmplaceSafeString(body, "description", exc_bp->label);
1217 } else {
1218 body.try_emplace("exceptionId", "exception");
1220 } else {
1221 body.try_emplace("exceptionId", "exception");
1223 if (!ObjectContainsKey(body, "description")) {
1224 char description[1024];
1225 if (thread.GetStopDescription(description, sizeof(description))) {
1226 EmplaceSafeString(body, "description", std::string(description));
1229 body.try_emplace("breakMode", "always");
1230 auto exception = thread.GetCurrentException();
1231 if (exception.IsValid()) {
1232 llvm::json::Object details;
1233 lldb::SBStream stream;
1234 if (exception.GetDescription(stream)) {
1235 EmplaceSafeString(details, "message", stream.GetData());
1238 auto exceptionBacktrace = thread.GetCurrentExceptionBacktrace();
1239 if (exceptionBacktrace.IsValid()) {
1240 lldb::SBStream stream;
1241 exceptionBacktrace.GetDescription(stream);
1242 for (uint32_t i = 0; i < exceptionBacktrace.GetNumFrames(); i++) {
1243 lldb::SBFrame frame = exceptionBacktrace.GetFrameAtIndex(i);
1244 frame.GetDescription(stream);
1246 EmplaceSafeString(details, "stackTrace", stream.GetData());
1249 body.try_emplace("details", std::move(details));
1251 // auto excInfoCount = thread.GetStopReasonDataCount();
1252 // for (auto i=0; i<excInfoCount; ++i) {
1253 // uint64_t exc_data = thread.GetStopReasonDataAtIndex(i);
1254 // }
1255 } else {
1256 response["success"] = llvm::json::Value(false);
1258 response.try_emplace("body", std::move(body));
1259 dap.SendJSON(llvm::json::Value(std::move(response)));
1262 // "CompletionsRequest": {
1263 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1264 // "type": "object",
1265 // "description": "Returns a list of possible completions for a given caret
1266 // position and text.\nThe CompletionsRequest may only be called if the
1267 // 'supportsCompletionsRequest' capability exists and is true.",
1268 // "properties": {
1269 // "command": {
1270 // "type": "string",
1271 // "enum": [ "completions" ]
1272 // },
1273 // "arguments": {
1274 // "$ref": "#/definitions/CompletionsArguments"
1275 // }
1276 // },
1277 // "required": [ "command", "arguments" ]
1278 // }]
1279 // },
1280 // "CompletionsArguments": {
1281 // "type": "object",
1282 // "description": "Arguments for 'completions' request.",
1283 // "properties": {
1284 // "frameId": {
1285 // "type": "integer",
1286 // "description": "Returns completions in the scope of this stack frame.
1287 // If not specified, the completions are returned for the global scope."
1288 // },
1289 // "text": {
1290 // "type": "string",
1291 // "description": "One or more source lines. Typically this is the text a
1292 // user has typed into the debug console before he asked for completion."
1293 // },
1294 // "column": {
1295 // "type": "integer",
1296 // "description": "The character position for which to determine the
1297 // completion proposals."
1298 // },
1299 // "line": {
1300 // "type": "integer",
1301 // "description": "An optional line for which to determine the completion
1302 // proposals. If missing the first line of the text is assumed."
1303 // }
1304 // },
1305 // "required": [ "text", "column" ]
1306 // },
1307 // "CompletionsResponse": {
1308 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1309 // "type": "object",
1310 // "description": "Response to 'completions' request.",
1311 // "properties": {
1312 // "body": {
1313 // "type": "object",
1314 // "properties": {
1315 // "targets": {
1316 // "type": "array",
1317 // "items": {
1318 // "$ref": "#/definitions/CompletionItem"
1319 // },
1320 // "description": "The possible completions for ."
1321 // }
1322 // },
1323 // "required": [ "targets" ]
1324 // }
1325 // },
1326 // "required": [ "body" ]
1327 // }]
1328 // },
1329 // "CompletionItem": {
1330 // "type": "object",
1331 // "description": "CompletionItems are the suggestions returned from the
1332 // CompletionsRequest.", "properties": {
1333 // "label": {
1334 // "type": "string",
1335 // "description": "The label of this completion item. By default this is
1336 // also the text that is inserted when selecting this completion."
1337 // },
1338 // "text": {
1339 // "type": "string",
1340 // "description": "If text is not falsy then it is inserted instead of the
1341 // label."
1342 // },
1343 // "sortText": {
1344 // "type": "string",
1345 // "description": "A string that should be used when comparing this item
1346 // with other items. When `falsy` the label is used."
1347 // },
1348 // "type": {
1349 // "$ref": "#/definitions/CompletionItemType",
1350 // "description": "The item's type. Typically the client uses this
1351 // information to render the item in the UI with an icon."
1352 // },
1353 // "start": {
1354 // "type": "integer",
1355 // "description": "This value determines the location (in the
1356 // CompletionsRequest's 'text' attribute) where the completion text is
1357 // added.\nIf missing the text is added at the location specified by the
1358 // CompletionsRequest's 'column' attribute."
1359 // },
1360 // "length": {
1361 // "type": "integer",
1362 // "description": "This value determines how many characters are
1363 // overwritten by the completion text.\nIf missing the value 0 is assumed
1364 // which results in the completion text being inserted."
1365 // }
1366 // },
1367 // "required": [ "label" ]
1368 // },
1369 // "CompletionItemType": {
1370 // "type": "string",
1371 // "description": "Some predefined types for the CompletionItem. Please note
1372 // that not all clients have specific icons for all of them.", "enum": [
1373 // "method", "function", "constructor", "field", "variable", "class",
1374 // "interface", "module", "property", "unit", "value", "enum", "keyword",
1375 // "snippet", "text", "color", "file", "reference", "customcolor" ]
1376 // }
1377 void request_completions(DAP &dap, const llvm::json::Object &request) {
1378 llvm::json::Object response;
1379 FillResponse(request, response);
1380 llvm::json::Object body;
1381 const auto *arguments = request.getObject("arguments");
1383 // If we have a frame, try to set the context for variable completions.
1384 lldb::SBFrame frame = dap.GetLLDBFrame(*arguments);
1385 if (frame.IsValid()) {
1386 frame.GetThread().GetProcess().SetSelectedThread(frame.GetThread());
1387 frame.GetThread().SetSelectedFrame(frame.GetFrameID());
1390 std::string text = GetString(arguments, "text").str();
1391 auto original_column = GetSigned(arguments, "column", text.size());
1392 auto original_line = GetSigned(arguments, "line", 1);
1393 auto offset = original_column - 1;
1394 if (original_line > 1) {
1395 llvm::SmallVector<::llvm::StringRef, 2> lines;
1396 llvm::StringRef(text).split(lines, '\n');
1397 for (int i = 0; i < original_line - 1; i++) {
1398 offset += lines[i].size();
1401 llvm::json::Array targets;
1403 bool had_escape_prefix =
1404 llvm::StringRef(text).starts_with(dap.command_escape_prefix);
1405 ReplMode completion_mode = dap.DetectReplMode(frame, text, true);
1407 // Handle the offset change introduced by stripping out the
1408 // `command_escape_prefix`.
1409 if (had_escape_prefix) {
1410 if (offset < static_cast<int64_t>(dap.command_escape_prefix.size())) {
1411 body.try_emplace("targets", std::move(targets));
1412 response.try_emplace("body", std::move(body));
1413 dap.SendJSON(llvm::json::Value(std::move(response)));
1414 return;
1416 offset -= dap.command_escape_prefix.size();
1419 // While the user is typing then we likely have an incomplete input and cannot
1420 // reliably determine the precise intent (command vs variable), try completing
1421 // the text as both a command and variable expression, if applicable.
1422 const std::string expr_prefix = "expression -- ";
1423 std::array<std::tuple<ReplMode, std::string, uint64_t>, 2> exprs = {
1424 {std::make_tuple(ReplMode::Command, text, offset),
1425 std::make_tuple(ReplMode::Variable, expr_prefix + text,
1426 offset + expr_prefix.size())}};
1427 for (const auto &[mode, line, cursor] : exprs) {
1428 if (completion_mode != ReplMode::Auto && completion_mode != mode)
1429 continue;
1431 lldb::SBStringList matches;
1432 lldb::SBStringList descriptions;
1433 if (!dap.debugger.GetCommandInterpreter().HandleCompletionWithDescriptions(
1434 line.c_str(), cursor, 0, 100, matches, descriptions))
1435 continue;
1437 // The first element is the common substring after the cursor position for
1438 // all the matches. The rest of the elements are the matches so ignore the
1439 // first result.
1440 for (size_t i = 1; i < matches.GetSize(); i++) {
1441 std::string match = matches.GetStringAtIndex(i);
1442 std::string description = descriptions.GetStringAtIndex(i);
1444 llvm::json::Object item;
1445 llvm::StringRef match_ref = match;
1446 for (llvm::StringRef commit_point : {".", "->"}) {
1447 if (match_ref.contains(commit_point)) {
1448 match_ref = match_ref.rsplit(commit_point).second;
1451 EmplaceSafeString(item, "text", match_ref);
1453 if (description.empty())
1454 EmplaceSafeString(item, "label", match);
1455 else
1456 EmplaceSafeString(item, "label", match + " -- " + description);
1458 targets.emplace_back(std::move(item));
1462 body.try_emplace("targets", std::move(targets));
1463 response.try_emplace("body", std::move(body));
1464 dap.SendJSON(llvm::json::Value(std::move(response)));
1467 // "EvaluateRequest": {
1468 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1469 // "type": "object",
1470 // "description": "Evaluate request; value of command field is 'evaluate'.
1471 // Evaluates the given expression in the context of the
1472 // top most stack frame. The expression has access to any
1473 // variables and arguments that are in scope.",
1474 // "properties": {
1475 // "command": {
1476 // "type": "string",
1477 // "enum": [ "evaluate" ]
1478 // },
1479 // "arguments": {
1480 // "$ref": "#/definitions/EvaluateArguments"
1481 // }
1482 // },
1483 // "required": [ "command", "arguments" ]
1484 // }]
1485 // },
1486 // "EvaluateArguments": {
1487 // "type": "object",
1488 // "description": "Arguments for 'evaluate' request.",
1489 // "properties": {
1490 // "expression": {
1491 // "type": "string",
1492 // "description": "The expression to evaluate."
1493 // },
1494 // "frameId": {
1495 // "type": "integer",
1496 // "description": "Evaluate the expression in the scope of this stack
1497 // frame. If not specified, the expression is evaluated
1498 // in the global scope."
1499 // },
1500 // "context": {
1501 // "type": "string",
1502 // "_enum": [ "watch", "repl", "hover" ],
1503 // "enumDescriptions": [
1504 // "evaluate is run in a watch.",
1505 // "evaluate is run from REPL console.",
1506 // "evaluate is run from a data hover."
1507 // ],
1508 // "description": "The context in which the evaluate request is run."
1509 // },
1510 // "format": {
1511 // "$ref": "#/definitions/ValueFormat",
1512 // "description": "Specifies details on how to format the Evaluate
1513 // result."
1514 // }
1515 // },
1516 // "required": [ "expression" ]
1517 // },
1518 // "EvaluateResponse": {
1519 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1520 // "type": "object",
1521 // "description": "Response to 'evaluate' request.",
1522 // "properties": {
1523 // "body": {
1524 // "type": "object",
1525 // "properties": {
1526 // "result": {
1527 // "type": "string",
1528 // "description": "The result of the evaluate request."
1529 // },
1530 // "type": {
1531 // "type": "string",
1532 // "description": "The optional type of the evaluate result."
1533 // },
1534 // "presentationHint": {
1535 // "$ref": "#/definitions/VariablePresentationHint",
1536 // "description": "Properties of a evaluate result that can be
1537 // used to determine how to render the result in
1538 // the UI."
1539 // },
1540 // "variablesReference": {
1541 // "type": "number",
1542 // "description": "If variablesReference is > 0, the evaluate
1543 // result is structured and its children can be
1544 // retrieved by passing variablesReference to the
1545 // VariablesRequest."
1546 // },
1547 // "namedVariables": {
1548 // "type": "number",
1549 // "description": "The number of named child variables. The
1550 // client can use this optional information to
1551 // present the variables in a paged UI and fetch
1552 // them in chunks."
1553 // },
1554 // "indexedVariables": {
1555 // "type": "number",
1556 // "description": "The number of indexed child variables. The
1557 // client can use this optional information to
1558 // present the variables in a paged UI and fetch
1559 // them in chunks."
1560 // },
1561 // "valueLocationReference": {
1562 // "type": "integer",
1563 // "description": "A reference that allows the client to request
1564 // the location where the returned value is
1565 // declared. For example, if a function pointer is
1566 // returned, the adapter may be able to look up the
1567 // function's location. This should be present only
1568 // if the adapter is likely to be able to resolve
1569 // the location.\n\nThis reference shares the same
1570 // lifetime as the `variablesReference`. See
1571 // 'Lifetime of Object References' in the
1572 // Overview section for details."
1573 // }
1574 // "memoryReference": {
1575 // "type": "string",
1576 // "description": "A memory reference to a location appropriate
1577 // for this result. For pointer type eval
1578 // results, this is generally a reference to the
1579 // memory address contained in the pointer. This
1580 // attribute may be returned by a debug adapter
1581 // if corresponding capability
1582 // `supportsMemoryReferences` is true."
1583 // },
1584 // },
1585 // "required": [ "result", "variablesReference" ]
1586 // }
1587 // },
1588 // "required": [ "body" ]
1589 // }]
1590 // }
1591 void request_evaluate(DAP &dap, const llvm::json::Object &request) {
1592 llvm::json::Object response;
1593 FillResponse(request, response);
1594 llvm::json::Object body;
1595 const auto *arguments = request.getObject("arguments");
1596 lldb::SBFrame frame = dap.GetLLDBFrame(*arguments);
1597 std::string expression = GetString(arguments, "expression").str();
1598 llvm::StringRef context = GetString(arguments, "context");
1599 bool repeat_last_command =
1600 expression.empty() && dap.last_nonempty_var_expression.empty();
1602 if (context == "repl" &&
1603 (repeat_last_command ||
1604 (!expression.empty() &&
1605 dap.DetectReplMode(frame, expression, false) == ReplMode::Command))) {
1606 // Since the current expression is not for a variable, clear the
1607 // last_nonempty_var_expression field.
1608 dap.last_nonempty_var_expression.clear();
1609 // If we're evaluating a command relative to the current frame, set the
1610 // focus_tid to the current frame for any thread related events.
1611 if (frame.IsValid()) {
1612 dap.focus_tid = frame.GetThread().GetThreadID();
1614 auto result = RunLLDBCommandsVerbatim(dap.debugger, llvm::StringRef(),
1615 {std::string(expression)});
1616 EmplaceSafeString(body, "result", result);
1617 body.try_emplace("variablesReference", (int64_t)0);
1618 } else {
1619 if (context == "repl") {
1620 // If the expression is empty and the last expression was for a
1621 // variable, set the expression to the previous expression (repeat the
1622 // evaluation); otherwise save the current non-empty expression for the
1623 // next (possibly empty) variable expression.
1624 if (expression.empty())
1625 expression = dap.last_nonempty_var_expression;
1626 else
1627 dap.last_nonempty_var_expression = expression;
1629 // Always try to get the answer from the local variables if possible. If
1630 // this fails, then if the context is not "hover", actually evaluate an
1631 // expression using the expression parser.
1633 // "frame variable" is more reliable than the expression parser in
1634 // many cases and it is faster.
1635 lldb::SBValue value = frame.GetValueForVariablePath(
1636 expression.data(), lldb::eDynamicDontRunTarget);
1638 // Freeze dry the value in case users expand it later in the debug console
1639 if (value.GetError().Success() && context == "repl")
1640 value = value.Persist();
1642 if (value.GetError().Fail() && context != "hover")
1643 value = frame.EvaluateExpression(expression.data());
1645 if (value.GetError().Fail()) {
1646 response["success"] = llvm::json::Value(false);
1647 // This error object must live until we're done with the pointer returned
1648 // by GetCString().
1649 lldb::SBError error = value.GetError();
1650 const char *error_cstr = error.GetCString();
1651 if (error_cstr && error_cstr[0])
1652 EmplaceSafeString(response, "message", std::string(error_cstr));
1653 else
1654 EmplaceSafeString(response, "message", "evaluate failed");
1655 } else {
1656 VariableDescription desc(value, dap.enable_auto_variable_summaries);
1657 EmplaceSafeString(body, "result", desc.GetResult(context));
1658 EmplaceSafeString(body, "type", desc.display_type_name);
1659 int64_t var_ref = 0;
1660 if (value.MightHaveChildren() || ValuePointsToCode(value))
1661 var_ref = dap.variables.InsertVariable(
1662 value, /*is_permanent=*/context == "repl");
1663 if (value.MightHaveChildren())
1664 body.try_emplace("variablesReference", var_ref);
1665 else
1666 body.try_emplace("variablesReference", (int64_t)0);
1667 if (lldb::addr_t addr = value.GetLoadAddress();
1668 addr != LLDB_INVALID_ADDRESS)
1669 body.try_emplace("memoryReference", EncodeMemoryReference(addr));
1670 if (ValuePointsToCode(value))
1671 body.try_emplace("valueLocationReference", var_ref);
1674 response.try_emplace("body", std::move(body));
1675 dap.SendJSON(llvm::json::Value(std::move(response)));
1678 // "compileUnitsRequest": {
1679 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1680 // "type": "object",
1681 // "description": "Compile Unit request; value of command field is
1682 // 'compileUnits'.",
1683 // "properties": {
1684 // "command": {
1685 // "type": "string",
1686 // "enum": [ "compileUnits" ]
1687 // },
1688 // "arguments": {
1689 // "$ref": "#/definitions/compileUnitRequestArguments"
1690 // }
1691 // },
1692 // "required": [ "command", "arguments" ]
1693 // }]
1694 // },
1695 // "compileUnitsRequestArguments": {
1696 // "type": "object",
1697 // "description": "Arguments for 'compileUnits' request.",
1698 // "properties": {
1699 // "moduleId": {
1700 // "type": "string",
1701 // "description": "The ID of the module."
1702 // }
1703 // },
1704 // "required": [ "moduleId" ]
1705 // },
1706 // "compileUnitsResponse": {
1707 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1708 // "type": "object",
1709 // "description": "Response to 'compileUnits' request.",
1710 // "properties": {
1711 // "body": {
1712 // "description": "Response to 'compileUnits' request. Array of
1713 // paths of compile units."
1714 // }
1715 // }
1716 // }]
1717 // }
1718 void request_compileUnits(DAP &dap, const llvm::json::Object &request) {
1719 llvm::json::Object response;
1720 FillResponse(request, response);
1721 llvm::json::Object body;
1722 llvm::json::Array units;
1723 const auto *arguments = request.getObject("arguments");
1724 std::string module_id = std::string(GetString(arguments, "moduleId"));
1725 int num_modules = dap.target.GetNumModules();
1726 for (int i = 0; i < num_modules; i++) {
1727 auto curr_module = dap.target.GetModuleAtIndex(i);
1728 if (module_id == curr_module.GetUUIDString()) {
1729 int num_units = curr_module.GetNumCompileUnits();
1730 for (int j = 0; j < num_units; j++) {
1731 auto curr_unit = curr_module.GetCompileUnitAtIndex(j);
1732 units.emplace_back(CreateCompileUnit(curr_unit));
1734 body.try_emplace("compileUnits", std::move(units));
1735 break;
1738 response.try_emplace("body", std::move(body));
1739 dap.SendJSON(llvm::json::Value(std::move(response)));
1742 // "modulesRequest": {
1743 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1744 // "type": "object",
1745 // "description": "Modules request; value of command field is
1746 // 'modules'.",
1747 // "properties": {
1748 // "command": {
1749 // "type": "string",
1750 // "enum": [ "modules" ]
1751 // },
1752 // },
1753 // "required": [ "command" ]
1754 // }]
1755 // },
1756 // "modulesResponse": {
1757 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1758 // "type": "object",
1759 // "description": "Response to 'modules' request.",
1760 // "properties": {
1761 // "body": {
1762 // "description": "Response to 'modules' request. Array of
1763 // module objects."
1764 // }
1765 // }
1766 // }]
1767 // }
1768 void request_modules(DAP &dap, const llvm::json::Object &request) {
1769 llvm::json::Object response;
1770 FillResponse(request, response);
1772 llvm::json::Array modules;
1773 for (size_t i = 0; i < dap.target.GetNumModules(); i++) {
1774 lldb::SBModule module = dap.target.GetModuleAtIndex(i);
1775 modules.emplace_back(CreateModule(dap.target, module));
1778 llvm::json::Object body;
1779 body.try_emplace("modules", std::move(modules));
1780 response.try_emplace("body", std::move(body));
1781 dap.SendJSON(llvm::json::Value(std::move(response)));
1784 // "InitializeRequest": {
1785 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1786 // "type": "object",
1787 // "description": "Initialize request; value of command field is
1788 // 'initialize'.",
1789 // "properties": {
1790 // "command": {
1791 // "type": "string",
1792 // "enum": [ "initialize" ]
1793 // },
1794 // "arguments": {
1795 // "$ref": "#/definitions/InitializeRequestArguments"
1796 // }
1797 // },
1798 // "required": [ "command", "arguments" ]
1799 // }]
1800 // },
1801 // "InitializeRequestArguments": {
1802 // "type": "object",
1803 // "description": "Arguments for 'initialize' request.",
1804 // "properties": {
1805 // "clientID": {
1806 // "type": "string",
1807 // "description": "The ID of the (frontend) client using this adapter."
1808 // },
1809 // "adapterID": {
1810 // "type": "string",
1811 // "description": "The ID of the debug adapter."
1812 // },
1813 // "locale": {
1814 // "type": "string",
1815 // "description": "The ISO-639 locale of the (frontend) client using
1816 // this adapter, e.g. en-US or de-CH."
1817 // },
1818 // "linesStartAt1": {
1819 // "type": "boolean",
1820 // "description": "If true all line numbers are 1-based (default)."
1821 // },
1822 // "columnsStartAt1": {
1823 // "type": "boolean",
1824 // "description": "If true all column numbers are 1-based (default)."
1825 // },
1826 // "pathFormat": {
1827 // "type": "string",
1828 // "_enum": [ "path", "uri" ],
1829 // "description": "Determines in what format paths are specified. The
1830 // default is 'path', which is the native format."
1831 // },
1832 // "supportsVariableType": {
1833 // "type": "boolean",
1834 // "description": "Client supports the optional type attribute for
1835 // variables."
1836 // },
1837 // "supportsVariablePaging": {
1838 // "type": "boolean",
1839 // "description": "Client supports the paging of variables."
1840 // },
1841 // "supportsRunInTerminalRequest": {
1842 // "type": "boolean",
1843 // "description": "Client supports the runInTerminal request."
1844 // }
1845 // },
1846 // "required": [ "adapterID" ]
1847 // },
1848 // "InitializeResponse": {
1849 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1850 // "type": "object",
1851 // "description": "Response to 'initialize' request.",
1852 // "properties": {
1853 // "body": {
1854 // "$ref": "#/definitions/Capabilities",
1855 // "description": "The capabilities of this debug adapter."
1856 // }
1857 // }
1858 // }]
1859 // }
1860 void request_initialize(DAP &dap, const llvm::json::Object &request) {
1861 llvm::json::Object response;
1862 FillResponse(request, response);
1863 llvm::json::Object body;
1865 const auto *arguments = request.getObject("arguments");
1866 // sourceInitFile option is not from formal DAP specification. It is only
1867 // used by unit tests to prevent sourcing .lldbinit files from environment
1868 // which may affect the outcome of tests.
1869 bool source_init_file = GetBoolean(arguments, "sourceInitFile", true);
1871 dap.debugger = lldb::SBDebugger::Create(source_init_file);
1872 if (llvm::Error err = dap.RunPreInitCommands()) {
1873 response["success"] = false;
1874 EmplaceSafeString(response, "message", llvm::toString(std::move(err)));
1875 dap.SendJSON(llvm::json::Value(std::move(response)));
1876 return;
1879 dap.PopulateExceptionBreakpoints();
1880 auto cmd = dap.debugger.GetCommandInterpreter().AddMultiwordCommand(
1881 "lldb-dap", "Commands for managing lldb-dap.");
1882 if (GetBoolean(arguments, "supportsStartDebuggingRequest", false)) {
1883 cmd.AddCommand(
1884 "start-debugging", new StartDebuggingRequestHandler(dap),
1885 "Sends a startDebugging request from the debug adapter to the client "
1886 "to start a child debug session of the same type as the caller.");
1888 cmd.AddCommand(
1889 "repl-mode", new ReplModeRequestHandler(dap),
1890 "Get or set the repl behavior of lldb-dap evaluation requests.");
1891 cmd.AddCommand("send-event", new SendEventRequestHandler(dap),
1892 "Sends an DAP event to the client.");
1894 dap.progress_event_thread =
1895 std::thread(ProgressEventThreadFunction, std::ref(dap));
1897 // Start our event thread so we can receive events from the debugger, target,
1898 // process and more.
1899 dap.event_thread = std::thread(EventThreadFunction, std::ref(dap));
1901 // The debug adapter supports the configurationDoneRequest.
1902 body.try_emplace("supportsConfigurationDoneRequest", true);
1903 // The debug adapter supports function breakpoints.
1904 body.try_emplace("supportsFunctionBreakpoints", true);
1905 // The debug adapter supports conditional breakpoints.
1906 body.try_emplace("supportsConditionalBreakpoints", true);
1907 // The debug adapter supports breakpoints that break execution after a
1908 // specified number of hits.
1909 body.try_emplace("supportsHitConditionalBreakpoints", true);
1910 // The debug adapter supports a (side effect free) evaluate request for
1911 // data hovers.
1912 body.try_emplace("supportsEvaluateForHovers", true);
1913 // Available filters or options for the setExceptionBreakpoints request.
1914 llvm::json::Array filters;
1915 for (const auto &exc_bp : *dap.exception_breakpoints) {
1916 filters.emplace_back(CreateExceptionBreakpointFilter(exc_bp));
1918 body.try_emplace("exceptionBreakpointFilters", std::move(filters));
1919 // The debug adapter supports launching a debugee in intergrated VSCode
1920 // terminal.
1921 body.try_emplace("supportsRunInTerminalRequest", true);
1922 // The debug adapter supports stepping back via the stepBack and
1923 // reverseContinue requests.
1924 body.try_emplace("supportsStepBack", false);
1925 // The debug adapter supports setting a variable to a value.
1926 body.try_emplace("supportsSetVariable", true);
1927 // The debug adapter supports restarting a frame.
1928 body.try_emplace("supportsRestartFrame", false);
1929 // The debug adapter supports the gotoTargetsRequest.
1930 body.try_emplace("supportsGotoTargetsRequest", false);
1931 // The debug adapter supports the stepInTargetsRequest.
1932 body.try_emplace("supportsStepInTargetsRequest", true);
1933 // The debug adapter supports the completions request.
1934 body.try_emplace("supportsCompletionsRequest", true);
1935 // The debug adapter supports the disassembly request.
1936 body.try_emplace("supportsDisassembleRequest", true);
1937 // The debug adapter supports stepping granularities (argument `granularity`)
1938 // for the stepping requests.
1939 body.try_emplace("supportsSteppingGranularity", true);
1940 // The debug adapter support for instruction breakpoint.
1941 body.try_emplace("supportsInstructionBreakpoints", true);
1943 llvm::json::Array completion_characters;
1944 completion_characters.emplace_back(".");
1945 completion_characters.emplace_back(" ");
1946 completion_characters.emplace_back("\t");
1947 body.try_emplace("completionTriggerCharacters",
1948 std::move(completion_characters));
1950 // The debug adapter supports the modules request.
1951 body.try_emplace("supportsModulesRequest", true);
1952 // The set of additional module information exposed by the debug adapter.
1953 // body.try_emplace("additionalModuleColumns"] = ColumnDescriptor
1954 // Checksum algorithms supported by the debug adapter.
1955 // body.try_emplace("supportedChecksumAlgorithms"] = ChecksumAlgorithm
1956 // The debug adapter supports the RestartRequest. In this case a client
1957 // should not implement 'restart' by terminating and relaunching the adapter
1958 // but by calling the RestartRequest.
1959 body.try_emplace("supportsRestartRequest", true);
1960 // The debug adapter supports 'exceptionOptions' on the
1961 // setExceptionBreakpoints request.
1962 body.try_emplace("supportsExceptionOptions", true);
1963 // The debug adapter supports a 'format' attribute on the stackTraceRequest,
1964 // variablesRequest, and evaluateRequest.
1965 body.try_emplace("supportsValueFormattingOptions", true);
1966 // The debug adapter supports the exceptionInfo request.
1967 body.try_emplace("supportsExceptionInfoRequest", true);
1968 // The debug adapter supports the 'terminateDebuggee' attribute on the
1969 // 'disconnect' request.
1970 body.try_emplace("supportTerminateDebuggee", true);
1971 // The debug adapter supports the delayed loading of parts of the stack,
1972 // which requires that both the 'startFrame' and 'levels' arguments and the
1973 // 'totalFrames' result of the 'StackTrace' request are supported.
1974 body.try_emplace("supportsDelayedStackTraceLoading", true);
1975 // The debug adapter supports the 'loadedSources' request.
1976 body.try_emplace("supportsLoadedSourcesRequest", false);
1977 // The debug adapter supports sending progress reporting events.
1978 body.try_emplace("supportsProgressReporting", true);
1979 // The debug adapter supports 'logMessage' in breakpoint.
1980 body.try_emplace("supportsLogPoints", true);
1981 // The debug adapter supports data watchpoints.
1982 body.try_emplace("supportsDataBreakpoints", true);
1983 // The debug adapter supports the `readMemory` request.
1984 body.try_emplace("supportsReadMemoryRequest", true);
1986 // Put in non-DAP specification lldb specific information.
1987 llvm::json::Object lldb_json;
1988 lldb_json.try_emplace("version", dap.debugger.GetVersionString());
1989 body.try_emplace("__lldb", std::move(lldb_json));
1991 response.try_emplace("body", std::move(body));
1992 dap.SendJSON(llvm::json::Value(std::move(response)));
1995 llvm::Error request_runInTerminal(DAP &dap,
1996 const llvm::json::Object &launch_request,
1997 const uint64_t timeout_seconds) {
1998 dap.is_attach = true;
1999 lldb::SBAttachInfo attach_info;
2001 llvm::Expected<std::shared_ptr<FifoFile>> comm_file_or_err =
2002 CreateRunInTerminalCommFile();
2003 if (!comm_file_or_err)
2004 return comm_file_or_err.takeError();
2005 FifoFile &comm_file = *comm_file_or_err.get();
2007 RunInTerminalDebugAdapterCommChannel comm_channel(comm_file.m_path);
2009 lldb::pid_t debugger_pid = LLDB_INVALID_PROCESS_ID;
2010 #if !defined(_WIN32)
2011 debugger_pid = getpid();
2012 #endif
2013 llvm::json::Object reverse_request = CreateRunInTerminalReverseRequest(
2014 launch_request, dap.debug_adaptor_path, comm_file.m_path, debugger_pid);
2015 dap.SendReverseRequest("runInTerminal", std::move(reverse_request),
2016 [](llvm::Expected<llvm::json::Value> value) {
2017 if (!value) {
2018 llvm::Error err = value.takeError();
2019 llvm::errs()
2020 << "runInTerminal request failed: "
2021 << llvm::toString(std::move(err)) << "\n";
2025 if (llvm::Expected<lldb::pid_t> pid = comm_channel.GetLauncherPid())
2026 attach_info.SetProcessID(*pid);
2027 else
2028 return pid.takeError();
2030 dap.debugger.SetAsync(false);
2031 lldb::SBError error;
2032 dap.target.Attach(attach_info, error);
2034 if (error.Fail())
2035 return llvm::createStringError(llvm::inconvertibleErrorCode(),
2036 "Failed to attach to the target process. %s",
2037 comm_channel.GetLauncherError().c_str());
2038 // This will notify the runInTerminal launcher that we attached.
2039 // We have to make this async, as the function won't return until the launcher
2040 // resumes and reads the data.
2041 std::future<lldb::SBError> did_attach_message_success =
2042 comm_channel.NotifyDidAttach();
2044 // We just attached to the runInTerminal launcher, which was waiting to be
2045 // attached. We now resume it, so it can receive the didAttach notification
2046 // and then perform the exec. Upon continuing, the debugger will stop the
2047 // process right in the middle of the exec. To the user, what we are doing is
2048 // transparent, as they will only be able to see the process since the exec,
2049 // completely unaware of the preparatory work.
2050 dap.target.GetProcess().Continue();
2052 // Now that the actual target is just starting (i.e. exec was just invoked),
2053 // we return the debugger to its async state.
2054 dap.debugger.SetAsync(true);
2056 // If sending the notification failed, the launcher should be dead by now and
2057 // the async didAttach notification should have an error message, so we
2058 // return it. Otherwise, everything was a success.
2059 did_attach_message_success.wait();
2060 error = did_attach_message_success.get();
2061 if (error.Success())
2062 return llvm::Error::success();
2063 return llvm::createStringError(llvm::inconvertibleErrorCode(),
2064 error.GetCString());
2067 // Takes a LaunchRequest object and launches the process, also handling
2068 // runInTerminal if applicable. It doesn't do any of the additional
2069 // initialization and bookkeeping stuff that is needed for `request_launch`.
2070 // This way we can reuse the process launching logic for RestartRequest too.
2071 lldb::SBError LaunchProcess(DAP &dap, const llvm::json::Object &request) {
2072 lldb::SBError error;
2073 const auto *arguments = request.getObject("arguments");
2074 auto launchCommands = GetStrings(arguments, "launchCommands");
2076 // Instantiate a launch info instance for the target.
2077 auto launch_info = dap.target.GetLaunchInfo();
2079 // Grab the current working directory if there is one and set it in the
2080 // launch info.
2081 const auto cwd = GetString(arguments, "cwd");
2082 if (!cwd.empty())
2083 launch_info.SetWorkingDirectory(cwd.data());
2085 // Extract any extra arguments and append them to our program arguments for
2086 // when we launch
2087 auto args = GetStrings(arguments, "args");
2088 if (!args.empty())
2089 launch_info.SetArguments(MakeArgv(args).data(), true);
2091 // Pass any environment variables along that the user specified.
2092 const auto envs = GetEnvironmentFromArguments(*arguments);
2093 launch_info.SetEnvironment(envs, true);
2095 auto flags = launch_info.GetLaunchFlags();
2097 if (GetBoolean(arguments, "disableASLR", true))
2098 flags |= lldb::eLaunchFlagDisableASLR;
2099 if (GetBoolean(arguments, "disableSTDIO", false))
2100 flags |= lldb::eLaunchFlagDisableSTDIO;
2101 if (GetBoolean(arguments, "shellExpandArguments", false))
2102 flags |= lldb::eLaunchFlagShellExpandArguments;
2103 const bool detachOnError = GetBoolean(arguments, "detachOnError", false);
2104 launch_info.SetDetachOnError(detachOnError);
2105 launch_info.SetLaunchFlags(flags | lldb::eLaunchFlagDebug |
2106 lldb::eLaunchFlagStopAtEntry);
2107 const uint64_t timeout_seconds = GetUnsigned(arguments, "timeout", 30);
2109 if (GetBoolean(arguments, "runInTerminal", false)) {
2110 if (llvm::Error err = request_runInTerminal(dap, request, timeout_seconds))
2111 error.SetErrorString(llvm::toString(std::move(err)).c_str());
2112 } else if (launchCommands.empty()) {
2113 // Disable async events so the launch will be successful when we return from
2114 // the launch call and the launch will happen synchronously
2115 dap.debugger.SetAsync(false);
2116 dap.target.Launch(launch_info, error);
2117 dap.debugger.SetAsync(true);
2118 } else {
2119 // Set the launch info so that run commands can access the configured
2120 // launch details.
2121 dap.target.SetLaunchInfo(launch_info);
2122 if (llvm::Error err = dap.RunLaunchCommands(launchCommands)) {
2123 error.SetErrorString(llvm::toString(std::move(err)).c_str());
2124 return error;
2126 // The custom commands might have created a new target so we should use the
2127 // selected target after these commands are run.
2128 dap.target = dap.debugger.GetSelectedTarget();
2129 // Make sure the process is launched and stopped at the entry point before
2130 // proceeding as the launch commands are not run using the synchronous
2131 // mode.
2132 error = dap.WaitForProcessToStop(timeout_seconds);
2134 return error;
2137 // "LaunchRequest": {
2138 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2139 // "type": "object",
2140 // "description": "Launch request; value of command field is 'launch'.",
2141 // "properties": {
2142 // "command": {
2143 // "type": "string",
2144 // "enum": [ "launch" ]
2145 // },
2146 // "arguments": {
2147 // "$ref": "#/definitions/LaunchRequestArguments"
2148 // }
2149 // },
2150 // "required": [ "command", "arguments" ]
2151 // }]
2152 // },
2153 // "LaunchRequestArguments": {
2154 // "type": "object",
2155 // "description": "Arguments for 'launch' request.",
2156 // "properties": {
2157 // "noDebug": {
2158 // "type": "boolean",
2159 // "description": "If noDebug is true the launch request should launch
2160 // the program without enabling debugging."
2161 // }
2162 // }
2163 // },
2164 // "LaunchResponse": {
2165 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2166 // "type": "object",
2167 // "description": "Response to 'launch' request. This is just an
2168 // acknowledgement, so no body field is required."
2169 // }]
2170 // }
2171 void request_launch(DAP &dap, const llvm::json::Object &request) {
2172 dap.is_attach = false;
2173 dap.last_launch_or_attach_request = request;
2174 llvm::json::Object response;
2175 FillResponse(request, response);
2176 const auto *arguments = request.getObject("arguments");
2177 dap.init_commands = GetStrings(arguments, "initCommands");
2178 dap.pre_run_commands = GetStrings(arguments, "preRunCommands");
2179 dap.stop_commands = GetStrings(arguments, "stopCommands");
2180 dap.exit_commands = GetStrings(arguments, "exitCommands");
2181 dap.terminate_commands = GetStrings(arguments, "terminateCommands");
2182 dap.post_run_commands = GetStrings(arguments, "postRunCommands");
2183 dap.stop_at_entry = GetBoolean(arguments, "stopOnEntry", false);
2184 const llvm::StringRef debuggerRoot = GetString(arguments, "debuggerRoot");
2185 dap.enable_auto_variable_summaries =
2186 GetBoolean(arguments, "enableAutoVariableSummaries", false);
2187 dap.enable_synthetic_child_debugging =
2188 GetBoolean(arguments, "enableSyntheticChildDebugging", false);
2189 dap.display_extended_backtrace =
2190 GetBoolean(arguments, "displayExtendedBacktrace", false);
2191 dap.command_escape_prefix = GetString(arguments, "commandEscapePrefix", "`");
2192 dap.SetFrameFormat(GetString(arguments, "customFrameFormat"));
2193 dap.SetThreadFormat(GetString(arguments, "customThreadFormat"));
2195 PrintWelcomeMessage(dap);
2197 // This is a hack for loading DWARF in .o files on Mac where the .o files
2198 // in the debug map of the main executable have relative paths which
2199 // require the lldb-dap binary to have its working directory set to that
2200 // relative root for the .o files in order to be able to load debug info.
2201 if (!debuggerRoot.empty())
2202 llvm::sys::fs::set_current_path(debuggerRoot);
2204 // Run any initialize LLDB commands the user specified in the launch.json.
2205 // This is run before target is created, so commands can't do anything with
2206 // the targets - preRunCommands are run with the target.
2207 if (llvm::Error err = dap.RunInitCommands()) {
2208 response["success"] = false;
2209 EmplaceSafeString(response, "message", llvm::toString(std::move(err)));
2210 dap.SendJSON(llvm::json::Value(std::move(response)));
2211 return;
2214 SetSourceMapFromArguments(dap, *arguments);
2216 lldb::SBError status;
2217 dap.SetTarget(dap.CreateTargetFromArguments(*arguments, status));
2218 if (status.Fail()) {
2219 response["success"] = llvm::json::Value(false);
2220 EmplaceSafeString(response, "message", status.GetCString());
2221 dap.SendJSON(llvm::json::Value(std::move(response)));
2222 return;
2225 // Run any pre run LLDB commands the user specified in the launch.json
2226 if (llvm::Error err = dap.RunPreRunCommands()) {
2227 response["success"] = false;
2228 EmplaceSafeString(response, "message", llvm::toString(std::move(err)));
2229 dap.SendJSON(llvm::json::Value(std::move(response)));
2230 return;
2233 status = LaunchProcess(dap, request);
2235 if (status.Fail()) {
2236 response["success"] = llvm::json::Value(false);
2237 EmplaceSafeString(response, "message", std::string(status.GetCString()));
2238 } else {
2239 dap.RunPostRunCommands();
2242 dap.SendJSON(llvm::json::Value(std::move(response)));
2244 if (!status.Fail()) {
2245 if (dap.is_attach)
2246 SendProcessEvent(dap, Attach); // this happens when doing runInTerminal
2247 else
2248 SendProcessEvent(dap, Launch);
2250 dap.SendJSON(CreateEventObject("initialized"));
2253 // Check if the step-granularity is `instruction`
2254 static bool hasInstructionGranularity(const llvm::json::Object &requestArgs) {
2255 if (std::optional<llvm::StringRef> value =
2256 requestArgs.getString("granularity"))
2257 return value == "instruction";
2258 return false;
2261 // "NextRequest": {
2262 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2263 // "type": "object",
2264 // "description": "Next request; value of command field is 'next'. The
2265 // request starts the debuggee to run again for one step.
2266 // The debug adapter first sends the NextResponse and then
2267 // a StoppedEvent (event type 'step') after the step has
2268 // completed.",
2269 // "properties": {
2270 // "command": {
2271 // "type": "string",
2272 // "enum": [ "next" ]
2273 // },
2274 // "arguments": {
2275 // "$ref": "#/definitions/NextArguments"
2276 // }
2277 // },
2278 // "required": [ "command", "arguments" ]
2279 // }]
2280 // },
2281 // "NextArguments": {
2282 // "type": "object",
2283 // "description": "Arguments for 'next' request.",
2284 // "properties": {
2285 // "threadId": {
2286 // "type": "integer",
2287 // "description": "Execute 'next' for this thread."
2288 // },
2289 // "granularity": {
2290 // "$ref": "#/definitions/SteppingGranularity",
2291 // "description": "Stepping granularity. If no granularity is specified, a
2292 // granularity of `statement` is assumed."
2293 // }
2294 // },
2295 // "required": [ "threadId" ]
2296 // },
2297 // "NextResponse": {
2298 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2299 // "type": "object",
2300 // "description": "Response to 'next' request. This is just an
2301 // acknowledgement, so no body field is required."
2302 // }]
2303 // }
2304 void request_next(DAP &dap, const llvm::json::Object &request) {
2305 llvm::json::Object response;
2306 FillResponse(request, response);
2307 const auto *arguments = request.getObject("arguments");
2308 lldb::SBThread thread = dap.GetLLDBThread(*arguments);
2309 if (thread.IsValid()) {
2310 // Remember the thread ID that caused the resume so we can set the
2311 // "threadCausedFocus" boolean value in the "stopped" events.
2312 dap.focus_tid = thread.GetThreadID();
2313 if (hasInstructionGranularity(*arguments)) {
2314 thread.StepInstruction(/*step_over=*/true);
2315 } else {
2316 thread.StepOver();
2318 } else {
2319 response["success"] = llvm::json::Value(false);
2321 dap.SendJSON(llvm::json::Value(std::move(response)));
2324 // "PauseRequest": {
2325 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2326 // "type": "object",
2327 // "description": "Pause request; value of command field is 'pause'. The
2328 // request suspenses the debuggee. The debug adapter first sends the
2329 // PauseResponse and then a StoppedEvent (event type 'pause') after the
2330 // thread has been paused successfully.", "properties": {
2331 // "command": {
2332 // "type": "string",
2333 // "enum": [ "pause" ]
2334 // },
2335 // "arguments": {
2336 // "$ref": "#/definitions/PauseArguments"
2337 // }
2338 // },
2339 // "required": [ "command", "arguments" ]
2340 // }]
2341 // },
2342 // "PauseArguments": {
2343 // "type": "object",
2344 // "description": "Arguments for 'pause' request.",
2345 // "properties": {
2346 // "threadId": {
2347 // "type": "integer",
2348 // "description": "Pause execution for this thread."
2349 // }
2350 // },
2351 // "required": [ "threadId" ]
2352 // },
2353 // "PauseResponse": {
2354 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2355 // "type": "object",
2356 // "description": "Response to 'pause' request. This is just an
2357 // acknowledgement, so no body field is required."
2358 // }]
2359 // }
2360 void request_pause(DAP &dap, const llvm::json::Object &request) {
2361 llvm::json::Object response;
2362 FillResponse(request, response);
2363 lldb::SBProcess process = dap.target.GetProcess();
2364 lldb::SBError error = process.Stop();
2365 dap.SendJSON(llvm::json::Value(std::move(response)));
2368 // "RestartRequest": {
2369 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2370 // "type": "object",
2371 // "description": "Restarts a debug session. Clients should only call this
2372 // request if the corresponding capability `supportsRestartRequest` is
2373 // true.\nIf the capability is missing or has the value false, a typical
2374 // client emulates `restart` by terminating the debug adapter first and then
2375 // launching it anew.",
2376 // "properties": {
2377 // "command": {
2378 // "type": "string",
2379 // "enum": [ "restart" ]
2380 // },
2381 // "arguments": {
2382 // "$ref": "#/definitions/RestartArguments"
2383 // }
2384 // },
2385 // "required": [ "command" ]
2386 // }]
2387 // },
2388 // "RestartArguments": {
2389 // "type": "object",
2390 // "description": "Arguments for `restart` request.",
2391 // "properties": {
2392 // "arguments": {
2393 // "oneOf": [
2394 // { "$ref": "#/definitions/LaunchRequestArguments" },
2395 // { "$ref": "#/definitions/AttachRequestArguments" }
2396 // ],
2397 // "description": "The latest version of the `launch` or `attach`
2398 // configuration."
2399 // }
2400 // }
2401 // },
2402 // "RestartResponse": {
2403 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2404 // "type": "object",
2405 // "description": "Response to `restart` request. This is just an
2406 // acknowledgement, so no body field is required."
2407 // }]
2408 // },
2409 void request_restart(DAP &dap, const llvm::json::Object &request) {
2410 llvm::json::Object response;
2411 FillResponse(request, response);
2412 if (!dap.last_launch_or_attach_request) {
2413 response["success"] = llvm::json::Value(false);
2414 EmplaceSafeString(response, "message",
2415 "Restart request received but no process was launched.");
2416 dap.SendJSON(llvm::json::Value(std::move(response)));
2417 return;
2419 // Check if we were in a "launch" session or an "attach" session.
2421 // Restarting is not well defined when we started the session by attaching to
2422 // an existing process, because we don't know how the process was started, so
2423 // we don't support it.
2425 // Note that when using runInTerminal we're technically attached, but it's an
2426 // implementation detail. The adapter *did* launch the process in response to
2427 // a "launch" command, so we can still stop it and re-run it. This is why we
2428 // don't just check `dap.is_attach`.
2429 if (GetString(*dap.last_launch_or_attach_request, "command") == "attach") {
2430 response["success"] = llvm::json::Value(false);
2431 EmplaceSafeString(response, "message",
2432 "Restarting an \"attach\" session is not supported.");
2433 dap.SendJSON(llvm::json::Value(std::move(response)));
2434 return;
2437 // The optional `arguments` field in RestartRequest can contain an updated
2438 // version of the launch arguments. If there's one, use it.
2439 const auto *restart_arguments = request.getObject("arguments");
2440 if (restart_arguments) {
2441 const auto *launch_request_arguments =
2442 restart_arguments->getObject("arguments");
2443 if (launch_request_arguments) {
2444 (*dap.last_launch_or_attach_request)["arguments"] =
2445 llvm::json::Value(llvm::json::Object(*launch_request_arguments));
2449 // Keep track of the old PID so when we get a "process exited" event from the
2450 // killed process we can detect it and not shut down the whole session.
2451 lldb::SBProcess process = dap.target.GetProcess();
2452 dap.restarting_process_id = process.GetProcessID();
2454 // Stop the current process if necessary. The logic here is similar to
2455 // CommandObjectProcessLaunchOrAttach::StopProcessIfNecessary, except that
2456 // we don't ask the user for confirmation.
2457 dap.debugger.SetAsync(false);
2458 if (process.IsValid()) {
2459 lldb::StateType state = process.GetState();
2460 if (state != lldb::eStateConnected) {
2461 process.Kill();
2463 // Clear the list of thread ids to avoid sending "thread exited" events
2464 // for threads of the process we are terminating.
2465 dap.thread_ids.clear();
2467 dap.debugger.SetAsync(true);
2468 LaunchProcess(dap, *dap.last_launch_or_attach_request);
2470 // This is normally done after receiving a "configuration done" request.
2471 // Because we're restarting, configuration has already happened so we can
2472 // continue the process right away.
2473 if (dap.stop_at_entry) {
2474 SendThreadStoppedEvent(dap);
2475 } else {
2476 dap.target.GetProcess().Continue();
2479 dap.SendJSON(llvm::json::Value(std::move(response)));
2482 // "ScopesRequest": {
2483 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2484 // "type": "object",
2485 // "description": "Scopes request; value of command field is 'scopes'. The
2486 // request returns the variable scopes for a given stackframe ID.",
2487 // "properties": {
2488 // "command": {
2489 // "type": "string",
2490 // "enum": [ "scopes" ]
2491 // },
2492 // "arguments": {
2493 // "$ref": "#/definitions/ScopesArguments"
2494 // }
2495 // },
2496 // "required": [ "command", "arguments" ]
2497 // }]
2498 // },
2499 // "ScopesArguments": {
2500 // "type": "object",
2501 // "description": "Arguments for 'scopes' request.",
2502 // "properties": {
2503 // "frameId": {
2504 // "type": "integer",
2505 // "description": "Retrieve the scopes for this stackframe."
2506 // }
2507 // },
2508 // "required": [ "frameId" ]
2509 // },
2510 // "ScopesResponse": {
2511 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2512 // "type": "object",
2513 // "description": "Response to 'scopes' request.",
2514 // "properties": {
2515 // "body": {
2516 // "type": "object",
2517 // "properties": {
2518 // "scopes": {
2519 // "type": "array",
2520 // "items": {
2521 // "$ref": "#/definitions/Scope"
2522 // },
2523 // "description": "The scopes of the stackframe. If the array has
2524 // length zero, there are no scopes available."
2525 // }
2526 // },
2527 // "required": [ "scopes" ]
2528 // }
2529 // },
2530 // "required": [ "body" ]
2531 // }]
2532 // }
2533 void request_scopes(DAP &dap, const llvm::json::Object &request) {
2534 llvm::json::Object response;
2535 FillResponse(request, response);
2536 llvm::json::Object body;
2537 const auto *arguments = request.getObject("arguments");
2538 lldb::SBFrame frame = dap.GetLLDBFrame(*arguments);
2539 // As the user selects different stack frames in the GUI, a "scopes" request
2540 // will be sent to the DAP. This is the only way we know that the user has
2541 // selected a frame in a thread. There are no other notifications that are
2542 // sent and VS code doesn't allow multiple frames to show variables
2543 // concurrently. If we select the thread and frame as the "scopes" requests
2544 // are sent, this allows users to type commands in the debugger console
2545 // with a backtick character to run lldb commands and these lldb commands
2546 // will now have the right context selected as they are run. If the user
2547 // types "`bt" into the debugger console and we had another thread selected
2548 // in the LLDB library, we would show the wrong thing to the user. If the
2549 // users switches threads with a lldb command like "`thread select 14", the
2550 // GUI will not update as there are no "event" notification packets that
2551 // allow us to change the currently selected thread or frame in the GUI that
2552 // I am aware of.
2553 if (frame.IsValid()) {
2554 frame.GetThread().GetProcess().SetSelectedThread(frame.GetThread());
2555 frame.GetThread().SetSelectedFrame(frame.GetFrameID());
2558 dap.variables.locals = frame.GetVariables(/*arguments=*/true,
2559 /*locals=*/true,
2560 /*statics=*/false,
2561 /*in_scope_only=*/true);
2562 dap.variables.globals = frame.GetVariables(/*arguments=*/false,
2563 /*locals=*/false,
2564 /*statics=*/true,
2565 /*in_scope_only=*/true);
2566 dap.variables.registers = frame.GetRegisters();
2567 body.try_emplace("scopes", dap.CreateTopLevelScopes());
2568 response.try_emplace("body", std::move(body));
2569 dap.SendJSON(llvm::json::Value(std::move(response)));
2572 // "SetBreakpointsRequest": {
2573 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2574 // "type": "object",
2575 // "description": "SetBreakpoints request; value of command field is
2576 // 'setBreakpoints'. Sets multiple breakpoints for a single source and
2577 // clears all previous breakpoints in that source. To clear all breakpoint
2578 // for a source, specify an empty array. When a breakpoint is hit, a
2579 // StoppedEvent (event type 'breakpoint') is generated.", "properties": {
2580 // "command": {
2581 // "type": "string",
2582 // "enum": [ "setBreakpoints" ]
2583 // },
2584 // "arguments": {
2585 // "$ref": "#/definitions/SetBreakpointsArguments"
2586 // }
2587 // },
2588 // "required": [ "command", "arguments" ]
2589 // }]
2590 // },
2591 // "SetBreakpointsArguments": {
2592 // "type": "object",
2593 // "description": "Arguments for 'setBreakpoints' request.",
2594 // "properties": {
2595 // "source": {
2596 // "$ref": "#/definitions/Source",
2597 // "description": "The source location of the breakpoints; either
2598 // source.path or source.reference must be specified."
2599 // },
2600 // "breakpoints": {
2601 // "type": "array",
2602 // "items": {
2603 // "$ref": "#/definitions/SourceBreakpoint"
2604 // },
2605 // "description": "The code locations of the breakpoints."
2606 // },
2607 // "lines": {
2608 // "type": "array",
2609 // "items": {
2610 // "type": "integer"
2611 // },
2612 // "description": "Deprecated: The code locations of the breakpoints."
2613 // },
2614 // "sourceModified": {
2615 // "type": "boolean",
2616 // "description": "A value of true indicates that the underlying source
2617 // has been modified which results in new breakpoint locations."
2618 // }
2619 // },
2620 // "required": [ "source" ]
2621 // },
2622 // "SetBreakpointsResponse": {
2623 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2624 // "type": "object",
2625 // "description": "Response to 'setBreakpoints' request. Returned is
2626 // information about each breakpoint created by this request. This includes
2627 // the actual code location and whether the breakpoint could be verified.
2628 // The breakpoints returned are in the same order as the elements of the
2629 // 'breakpoints' (or the deprecated 'lines') in the
2630 // SetBreakpointsArguments.", "properties": {
2631 // "body": {
2632 // "type": "object",
2633 // "properties": {
2634 // "breakpoints": {
2635 // "type": "array",
2636 // "items": {
2637 // "$ref": "#/definitions/Breakpoint"
2638 // },
2639 // "description": "Information about the breakpoints. The array
2640 // elements are in the same order as the elements of the
2641 // 'breakpoints' (or the deprecated 'lines') in the
2642 // SetBreakpointsArguments."
2643 // }
2644 // },
2645 // "required": [ "breakpoints" ]
2646 // }
2647 // },
2648 // "required": [ "body" ]
2649 // }]
2650 // },
2651 // "SourceBreakpoint": {
2652 // "type": "object",
2653 // "description": "Properties of a breakpoint or logpoint passed to the
2654 // setBreakpoints request.", "properties": {
2655 // "line": {
2656 // "type": "integer",
2657 // "description": "The source line of the breakpoint or logpoint."
2658 // },
2659 // "column": {
2660 // "type": "integer",
2661 // "description": "An optional source column of the breakpoint."
2662 // },
2663 // "condition": {
2664 // "type": "string",
2665 // "description": "An optional expression for conditional breakpoints."
2666 // },
2667 // "hitCondition": {
2668 // "type": "string",
2669 // "description": "An optional expression that controls how many hits of
2670 // the breakpoint are ignored. The backend is expected to interpret the
2671 // expression as needed."
2672 // },
2673 // "logMessage": {
2674 // "type": "string",
2675 // "description": "If this attribute exists and is non-empty, the backend
2676 // must not 'break' (stop) but log the message instead. Expressions within
2677 // {} are interpolated."
2678 // }
2679 // },
2680 // "required": [ "line" ]
2681 // }
2682 void request_setBreakpoints(DAP &dap, const llvm::json::Object &request) {
2683 llvm::json::Object response;
2684 lldb::SBError error;
2685 FillResponse(request, response);
2686 const auto *arguments = request.getObject("arguments");
2687 const auto *source = arguments->getObject("source");
2688 const auto path = GetString(source, "path");
2689 const auto *breakpoints = arguments->getArray("breakpoints");
2690 llvm::json::Array response_breakpoints;
2692 // Decode the source breakpoint infos for this "setBreakpoints" request
2693 SourceBreakpointMap request_bps;
2694 // "breakpoints" may be unset, in which case we treat it the same as being set
2695 // to an empty array.
2696 if (breakpoints) {
2697 for (const auto &bp : *breakpoints) {
2698 const auto *bp_obj = bp.getAsObject();
2699 if (bp_obj) {
2700 SourceBreakpoint src_bp(dap, *bp_obj);
2701 request_bps.try_emplace(src_bp.line, src_bp);
2702 const auto [iv, inserted] =
2703 dap.source_breakpoints[path].try_emplace(src_bp.line, src_bp);
2704 // We check if this breakpoint already exists to update it
2705 if (inserted)
2706 iv->getSecond().SetBreakpoint(path.data());
2707 else
2708 iv->getSecond().UpdateBreakpoint(src_bp);
2709 AppendBreakpoint(&iv->getSecond(), response_breakpoints, path,
2710 src_bp.line);
2715 // Delete any breakpoints in this source file that aren't in the
2716 // request_bps set. There is no call to remove breakpoints other than
2717 // calling this function with a smaller or empty "breakpoints" list.
2718 auto old_src_bp_pos = dap.source_breakpoints.find(path);
2719 if (old_src_bp_pos != dap.source_breakpoints.end()) {
2720 for (auto &old_bp : old_src_bp_pos->second) {
2721 auto request_pos = request_bps.find(old_bp.first);
2722 if (request_pos == request_bps.end()) {
2723 // This breakpoint no longer exists in this source file, delete it
2724 dap.target.BreakpointDelete(old_bp.second.bp.GetID());
2725 old_src_bp_pos->second.erase(old_bp.first);
2730 llvm::json::Object body;
2731 body.try_emplace("breakpoints", std::move(response_breakpoints));
2732 response.try_emplace("body", std::move(body));
2733 dap.SendJSON(llvm::json::Value(std::move(response)));
2736 // "SetExceptionBreakpointsRequest": {
2737 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2738 // "type": "object",
2739 // "description": "SetExceptionBreakpoints request; value of command field
2740 // is 'setExceptionBreakpoints'. The request configures the debuggers
2741 // response to thrown exceptions. If an exception is configured to break, a
2742 // StoppedEvent is fired (event type 'exception').", "properties": {
2743 // "command": {
2744 // "type": "string",
2745 // "enum": [ "setExceptionBreakpoints" ]
2746 // },
2747 // "arguments": {
2748 // "$ref": "#/definitions/SetExceptionBreakpointsArguments"
2749 // }
2750 // },
2751 // "required": [ "command", "arguments" ]
2752 // }]
2753 // },
2754 // "SetExceptionBreakpointsArguments": {
2755 // "type": "object",
2756 // "description": "Arguments for 'setExceptionBreakpoints' request.",
2757 // "properties": {
2758 // "filters": {
2759 // "type": "array",
2760 // "items": {
2761 // "type": "string"
2762 // },
2763 // "description": "IDs of checked exception options. The set of IDs is
2764 // returned via the 'exceptionBreakpointFilters' capability."
2765 // },
2766 // "exceptionOptions": {
2767 // "type": "array",
2768 // "items": {
2769 // "$ref": "#/definitions/ExceptionOptions"
2770 // },
2771 // "description": "Configuration options for selected exceptions."
2772 // }
2773 // },
2774 // "required": [ "filters" ]
2775 // },
2776 // "SetExceptionBreakpointsResponse": {
2777 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2778 // "type": "object",
2779 // "description": "Response to 'setExceptionBreakpoints' request. This is
2780 // just an acknowledgement, so no body field is required."
2781 // }]
2782 // }
2783 void request_setExceptionBreakpoints(DAP &dap,
2784 const llvm::json::Object &request) {
2785 llvm::json::Object response;
2786 lldb::SBError error;
2787 FillResponse(request, response);
2788 const auto *arguments = request.getObject("arguments");
2789 const auto *filters = arguments->getArray("filters");
2790 // Keep a list of any exception breakpoint filter names that weren't set
2791 // so we can clear any exception breakpoints if needed.
2792 std::set<std::string> unset_filters;
2793 for (const auto &bp : *dap.exception_breakpoints)
2794 unset_filters.insert(bp.filter);
2796 for (const auto &value : *filters) {
2797 const auto filter = GetAsString(value);
2798 auto *exc_bp = dap.GetExceptionBreakpoint(std::string(filter));
2799 if (exc_bp) {
2800 exc_bp->SetBreakpoint();
2801 unset_filters.erase(std::string(filter));
2804 for (const auto &filter : unset_filters) {
2805 auto *exc_bp = dap.GetExceptionBreakpoint(filter);
2806 if (exc_bp)
2807 exc_bp->ClearBreakpoint();
2809 dap.SendJSON(llvm::json::Value(std::move(response)));
2812 // "SetFunctionBreakpointsRequest": {
2813 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2814 // "type": "object",
2815 // "description": "SetFunctionBreakpoints request; value of command field is
2816 // 'setFunctionBreakpoints'. Sets multiple function breakpoints and clears
2817 // all previous function breakpoints. To clear all function breakpoint,
2818 // specify an empty array. When a function breakpoint is hit, a StoppedEvent
2819 // (event type 'function breakpoint') is generated.", "properties": {
2820 // "command": {
2821 // "type": "string",
2822 // "enum": [ "setFunctionBreakpoints" ]
2823 // },
2824 // "arguments": {
2825 // "$ref": "#/definitions/SetFunctionBreakpointsArguments"
2826 // }
2827 // },
2828 // "required": [ "command", "arguments" ]
2829 // }]
2830 // },
2831 // "SetFunctionBreakpointsArguments": {
2832 // "type": "object",
2833 // "description": "Arguments for 'setFunctionBreakpoints' request.",
2834 // "properties": {
2835 // "breakpoints": {
2836 // "type": "array",
2837 // "items": {
2838 // "$ref": "#/definitions/FunctionBreakpoint"
2839 // },
2840 // "description": "The function names of the breakpoints."
2841 // }
2842 // },
2843 // "required": [ "breakpoints" ]
2844 // },
2845 // "FunctionBreakpoint": {
2846 // "type": "object",
2847 // "description": "Properties of a breakpoint passed to the
2848 // setFunctionBreakpoints request.", "properties": {
2849 // "name": {
2850 // "type": "string",
2851 // "description": "The name of the function."
2852 // },
2853 // "condition": {
2854 // "type": "string",
2855 // "description": "An optional expression for conditional breakpoints."
2856 // },
2857 // "hitCondition": {
2858 // "type": "string",
2859 // "description": "An optional expression that controls how many hits of
2860 // the breakpoint are ignored. The backend is expected to interpret the
2861 // expression as needed."
2862 // }
2863 // },
2864 // "required": [ "name" ]
2865 // },
2866 // "SetFunctionBreakpointsResponse": {
2867 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2868 // "type": "object",
2869 // "description": "Response to 'setFunctionBreakpoints' request. Returned is
2870 // information about each breakpoint created by this request.",
2871 // "properties": {
2872 // "body": {
2873 // "type": "object",
2874 // "properties": {
2875 // "breakpoints": {
2876 // "type": "array",
2877 // "items": {
2878 // "$ref": "#/definitions/Breakpoint"
2879 // },
2880 // "description": "Information about the breakpoints. The array
2881 // elements correspond to the elements of the 'breakpoints' array."
2882 // }
2883 // },
2884 // "required": [ "breakpoints" ]
2885 // }
2886 // },
2887 // "required": [ "body" ]
2888 // }]
2889 // }
2890 void request_setFunctionBreakpoints(DAP &dap,
2891 const llvm::json::Object &request) {
2892 llvm::json::Object response;
2893 lldb::SBError error;
2894 FillResponse(request, response);
2895 const auto *arguments = request.getObject("arguments");
2896 const auto *breakpoints = arguments->getArray("breakpoints");
2897 llvm::json::Array response_breakpoints;
2899 // Disable any function breakpoints that aren't in this request.
2900 // There is no call to remove function breakpoints other than calling this
2901 // function with a smaller or empty "breakpoints" list.
2902 const auto name_iter = dap.function_breakpoints.keys();
2903 llvm::DenseSet<llvm::StringRef> seen(name_iter.begin(), name_iter.end());
2904 for (const auto &value : *breakpoints) {
2905 const auto *bp_obj = value.getAsObject();
2906 if (!bp_obj)
2907 continue;
2908 FunctionBreakpoint fn_bp(dap, *bp_obj);
2909 const auto [it, inserted] =
2910 dap.function_breakpoints.try_emplace(fn_bp.functionName, dap, *bp_obj);
2911 if (inserted)
2912 it->second.SetBreakpoint();
2913 else
2914 it->second.UpdateBreakpoint(fn_bp);
2916 AppendBreakpoint(&it->second, response_breakpoints);
2917 seen.erase(fn_bp.functionName);
2920 // Remove any breakpoints that are no longer in our list
2921 for (const auto &name : seen) {
2922 auto fn_bp = dap.function_breakpoints.find(name);
2923 if (fn_bp == dap.function_breakpoints.end())
2924 continue;
2925 dap.target.BreakpointDelete(fn_bp->second.bp.GetID());
2926 dap.function_breakpoints.erase(name);
2929 llvm::json::Object body;
2930 body.try_emplace("breakpoints", std::move(response_breakpoints));
2931 response.try_emplace("body", std::move(body));
2932 dap.SendJSON(llvm::json::Value(std::move(response)));
2935 // "DataBreakpointInfoRequest": {
2936 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2937 // "type": "object",
2938 // "description": "Obtains information on a possible data breakpoint that
2939 // could be set on an expression or variable.\nClients should only call this
2940 // request if the corresponding capability `supportsDataBreakpoints` is
2941 // true.", "properties": {
2942 // "command": {
2943 // "type": "string",
2944 // "enum": [ "dataBreakpointInfo" ]
2945 // },
2946 // "arguments": {
2947 // "$ref": "#/definitions/DataBreakpointInfoArguments"
2948 // }
2949 // },
2950 // "required": [ "command", "arguments" ]
2951 // }]
2952 // },
2953 // "DataBreakpointInfoArguments": {
2954 // "type": "object",
2955 // "description": "Arguments for `dataBreakpointInfo` request.",
2956 // "properties": {
2957 // "variablesReference": {
2958 // "type": "integer",
2959 // "description": "Reference to the variable container if the data
2960 // breakpoint is requested for a child of the container. The
2961 // `variablesReference` must have been obtained in the current suspended
2962 // state. See 'Lifetime of Object References' in the Overview section for
2963 // details."
2964 // },
2965 // "name": {
2966 // "type": "string",
2967 // "description": "The name of the variable's child to obtain data
2968 // breakpoint information for.\nIf `variablesReference` isn't specified,
2969 // this can be an expression."
2970 // },
2971 // "frameId": {
2972 // "type": "integer",
2973 // "description": "When `name` is an expression, evaluate it in the scope
2974 // of this stack frame. If not specified, the expression is evaluated in
2975 // the global scope. When `variablesReference` is specified, this property
2976 // has no effect."
2977 // }
2978 // },
2979 // "required": [ "name" ]
2980 // },
2981 // "DataBreakpointInfoResponse": {
2982 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2983 // "type": "object",
2984 // "description": "Response to `dataBreakpointInfo` request.",
2985 // "properties": {
2986 // "body": {
2987 // "type": "object",
2988 // "properties": {
2989 // "dataId": {
2990 // "type": [ "string", "null" ],
2991 // "description": "An identifier for the data on which a data
2992 // breakpoint can be registered with the `setDataBreakpoints`
2993 // request or null if no data breakpoint is available. If a
2994 // `variablesReference` or `frameId` is passed, the `dataId` is
2995 // valid in the current suspended state, otherwise it's valid
2996 // indefinitely. See 'Lifetime of Object References' in the Overview
2997 // section for details. Breakpoints set using the `dataId` in the
2998 // `setDataBreakpoints` request may outlive the lifetime of the
2999 // associated `dataId`."
3000 // },
3001 // "description": {
3002 // "type": "string",
3003 // "description": "UI string that describes on what data the
3004 // breakpoint is set on or why a data breakpoint is not available."
3005 // },
3006 // "accessTypes": {
3007 // "type": "array",
3008 // "items": {
3009 // "$ref": "#/definitions/DataBreakpointAccessType"
3010 // },
3011 // "description": "Attribute lists the available access types for a
3012 // potential data breakpoint. A UI client could surface this
3013 // information."
3014 // },
3015 // "canPersist": {
3016 // "type": "boolean",
3017 // "description": "Attribute indicates that a potential data
3018 // breakpoint could be persisted across sessions."
3019 // }
3020 // },
3021 // "required": [ "dataId", "description" ]
3022 // }
3023 // },
3024 // "required": [ "body" ]
3025 // }]
3026 // }
3027 void request_dataBreakpointInfo(DAP &dap, const llvm::json::Object &request) {
3028 llvm::json::Object response;
3029 FillResponse(request, response);
3030 llvm::json::Object body;
3031 lldb::SBError error;
3032 llvm::json::Array accessTypes{"read", "write", "readWrite"};
3033 const auto *arguments = request.getObject("arguments");
3034 const auto variablesReference =
3035 GetUnsigned(arguments, "variablesReference", 0);
3036 llvm::StringRef name = GetString(arguments, "name");
3037 lldb::SBFrame frame = dap.GetLLDBFrame(*arguments);
3038 lldb::SBValue variable = FindVariable(dap, variablesReference, name);
3039 std::string addr, size;
3041 if (variable.IsValid()) {
3042 lldb::addr_t load_addr = variable.GetLoadAddress();
3043 size_t byte_size = variable.GetByteSize();
3044 if (load_addr == LLDB_INVALID_ADDRESS) {
3045 body.try_emplace("dataId", nullptr);
3046 body.try_emplace("description",
3047 "does not exist in memory, its location is " +
3048 std::string(variable.GetLocation()));
3049 } else if (byte_size == 0) {
3050 body.try_emplace("dataId", nullptr);
3051 body.try_emplace("description", "variable size is 0");
3052 } else {
3053 addr = llvm::utohexstr(load_addr);
3054 size = llvm::utostr(byte_size);
3056 } else if (variablesReference == 0 && frame.IsValid()) {
3057 lldb::SBValue value = frame.EvaluateExpression(name.data());
3058 if (value.GetError().Fail()) {
3059 lldb::SBError error = value.GetError();
3060 const char *error_cstr = error.GetCString();
3061 body.try_emplace("dataId", nullptr);
3062 body.try_emplace("description", error_cstr && error_cstr[0]
3063 ? std::string(error_cstr)
3064 : "evaluation failed");
3065 } else {
3066 uint64_t load_addr = value.GetValueAsUnsigned();
3067 lldb::SBData data = value.GetPointeeData();
3068 if (data.IsValid()) {
3069 size = llvm::utostr(data.GetByteSize());
3070 addr = llvm::utohexstr(load_addr);
3071 lldb::SBMemoryRegionInfo region;
3072 lldb::SBError err =
3073 dap.target.GetProcess().GetMemoryRegionInfo(load_addr, region);
3074 // Only lldb-server supports "qMemoryRegionInfo". So, don't fail this
3075 // request if SBProcess::GetMemoryRegionInfo returns error.
3076 if (err.Success()) {
3077 if (!(region.IsReadable() || region.IsWritable())) {
3078 body.try_emplace("dataId", nullptr);
3079 body.try_emplace("description",
3080 "memory region for address " + addr +
3081 " has no read or write permissions");
3084 } else {
3085 body.try_emplace("dataId", nullptr);
3086 body.try_emplace("description",
3087 "unable to get byte size for expression: " +
3088 name.str());
3091 } else {
3092 body.try_emplace("dataId", nullptr);
3093 body.try_emplace("description", "variable not found: " + name.str());
3096 if (!body.getObject("dataId")) {
3097 body.try_emplace("dataId", addr + "/" + size);
3098 body.try_emplace("accessTypes", std::move(accessTypes));
3099 body.try_emplace("description",
3100 size + " bytes at " + addr + " " + name.str());
3102 response.try_emplace("body", std::move(body));
3103 dap.SendJSON(llvm::json::Value(std::move(response)));
3106 // "SetDataBreakpointsRequest": {
3107 // "allOf": [ { "$ref": "#/definitions/Request" }, {
3108 // "type": "object",
3109 // "description": "Replaces all existing data breakpoints with new data
3110 // breakpoints.\nTo clear all data breakpoints, specify an empty
3111 // array.\nWhen a data breakpoint is hit, a `stopped` event (with reason
3112 // `data breakpoint`) is generated.\nClients should only call this request
3113 // if the corresponding capability `supportsDataBreakpoints` is true.",
3114 // "properties": {
3115 // "command": {
3116 // "type": "string",
3117 // "enum": [ "setDataBreakpoints" ]
3118 // },
3119 // "arguments": {
3120 // "$ref": "#/definitions/SetDataBreakpointsArguments"
3121 // }
3122 // },
3123 // "required": [ "command", "arguments" ]
3124 // }]
3125 // },
3126 // "SetDataBreakpointsArguments": {
3127 // "type": "object",
3128 // "description": "Arguments for `setDataBreakpoints` request.",
3129 // "properties": {
3130 // "breakpoints": {
3131 // "type": "array",
3132 // "items": {
3133 // "$ref": "#/definitions/DataBreakpoint"
3134 // },
3135 // "description": "The contents of this array replaces all existing data
3136 // breakpoints. An empty array clears all data breakpoints."
3137 // }
3138 // },
3139 // "required": [ "breakpoints" ]
3140 // },
3141 // "SetDataBreakpointsResponse": {
3142 // "allOf": [ { "$ref": "#/definitions/Response" }, {
3143 // "type": "object",
3144 // "description": "Response to `setDataBreakpoints` request.\nReturned is
3145 // information about each breakpoint created by this request.",
3146 // "properties": {
3147 // "body": {
3148 // "type": "object",
3149 // "properties": {
3150 // "breakpoints": {
3151 // "type": "array",
3152 // "items": {
3153 // "$ref": "#/definitions/Breakpoint"
3154 // },
3155 // "description": "Information about the data breakpoints. The array
3156 // elements correspond to the elements of the input argument
3157 // `breakpoints` array."
3158 // }
3159 // },
3160 // "required": [ "breakpoints" ]
3161 // }
3162 // },
3163 // "required": [ "body" ]
3164 // }]
3165 // }
3166 void request_setDataBreakpoints(DAP &dap, const llvm::json::Object &request) {
3167 llvm::json::Object response;
3168 lldb::SBError error;
3169 FillResponse(request, response);
3170 const auto *arguments = request.getObject("arguments");
3171 const auto *breakpoints = arguments->getArray("breakpoints");
3172 llvm::json::Array response_breakpoints;
3173 dap.target.DeleteAllWatchpoints();
3174 std::vector<Watchpoint> watchpoints;
3175 if (breakpoints) {
3176 for (const auto &bp : *breakpoints) {
3177 const auto *bp_obj = bp.getAsObject();
3178 if (bp_obj)
3179 watchpoints.emplace_back(dap, *bp_obj);
3182 // If two watchpoints start at the same address, the latter overwrite the
3183 // former. So, we only enable those at first-seen addresses when iterating
3184 // backward.
3185 std::set<lldb::addr_t> addresses;
3186 for (auto iter = watchpoints.rbegin(); iter != watchpoints.rend(); ++iter) {
3187 if (addresses.count(iter->addr) == 0) {
3188 iter->SetWatchpoint();
3189 addresses.insert(iter->addr);
3192 for (auto wp : watchpoints)
3193 AppendBreakpoint(&wp, response_breakpoints);
3195 llvm::json::Object body;
3196 body.try_emplace("breakpoints", std::move(response_breakpoints));
3197 response.try_emplace("body", std::move(body));
3198 dap.SendJSON(llvm::json::Value(std::move(response)));
3201 // "SourceRequest": {
3202 // "allOf": [ { "$ref": "#/definitions/Request" }, {
3203 // "type": "object",
3204 // "description": "Source request; value of command field is 'source'. The
3205 // request retrieves the source code for a given source reference.",
3206 // "properties": {
3207 // "command": {
3208 // "type": "string",
3209 // "enum": [ "source" ]
3210 // },
3211 // "arguments": {
3212 // "$ref": "#/definitions/SourceArguments"
3213 // }
3214 // },
3215 // "required": [ "command", "arguments" ]
3216 // }]
3217 // },
3218 // "SourceArguments": {
3219 // "type": "object",
3220 // "description": "Arguments for 'source' request.",
3221 // "properties": {
3222 // "source": {
3223 // "$ref": "#/definitions/Source",
3224 // "description": "Specifies the source content to load. Either
3225 // source.path or source.sourceReference must be specified."
3226 // },
3227 // "sourceReference": {
3228 // "type": "integer",
3229 // "description": "The reference to the source. This is the same as
3230 // source.sourceReference. This is provided for backward compatibility
3231 // since old backends do not understand the 'source' attribute."
3232 // }
3233 // },
3234 // "required": [ "sourceReference" ]
3235 // },
3236 // "SourceResponse": {
3237 // "allOf": [ { "$ref": "#/definitions/Response" }, {
3238 // "type": "object",
3239 // "description": "Response to 'source' request.",
3240 // "properties": {
3241 // "body": {
3242 // "type": "object",
3243 // "properties": {
3244 // "content": {
3245 // "type": "string",
3246 // "description": "Content of the source reference."
3247 // },
3248 // "mimeType": {
3249 // "type": "string",
3250 // "description": "Optional content type (mime type) of the source."
3251 // }
3252 // },
3253 // "required": [ "content" ]
3254 // }
3255 // },
3256 // "required": [ "body" ]
3257 // }]
3258 // }
3259 void request_source(DAP &dap, const llvm::json::Object &request) {
3260 llvm::json::Object response;
3261 FillResponse(request, response);
3262 llvm::json::Object body{{"content", ""}};
3263 response.try_emplace("body", std::move(body));
3264 dap.SendJSON(llvm::json::Value(std::move(response)));
3267 // "StackTraceRequest": {
3268 // "allOf": [ { "$ref": "#/definitions/Request" }, {
3269 // "type": "object",
3270 // "description": "StackTrace request; value of command field is
3271 // 'stackTrace'. The request returns a stacktrace from the current execution
3272 // state.", "properties": {
3273 // "command": {
3274 // "type": "string",
3275 // "enum": [ "stackTrace" ]
3276 // },
3277 // "arguments": {
3278 // "$ref": "#/definitions/StackTraceArguments"
3279 // }
3280 // },
3281 // "required": [ "command", "arguments" ]
3282 // }]
3283 // },
3284 // "StackTraceArguments": {
3285 // "type": "object",
3286 // "description": "Arguments for 'stackTrace' request.",
3287 // "properties": {
3288 // "threadId": {
3289 // "type": "integer",
3290 // "description": "Retrieve the stacktrace for this thread."
3291 // },
3292 // "startFrame": {
3293 // "type": "integer",
3294 // "description": "The index of the first frame to return; if omitted
3295 // frames start at 0."
3296 // },
3297 // "levels": {
3298 // "type": "integer",
3299 // "description": "The maximum number of frames to return. If levels is
3300 // not specified or 0, all frames are returned."
3301 // },
3302 // "format": {
3303 // "$ref": "#/definitions/StackFrameFormat",
3304 // "description": "Specifies details on how to format the stack frames.
3305 // The attribute is only honored by a debug adapter if the corresponding
3306 // capability `supportsValueFormattingOptions` is true."
3307 // }
3308 // },
3309 // "required": [ "threadId" ]
3310 // },
3311 // "StackTraceResponse": {
3312 // "allOf": [ { "$ref": "#/definitions/Response" }, {
3313 // "type": "object",
3314 // "description": "Response to `stackTrace` request.",
3315 // "properties": {
3316 // "body": {
3317 // "type": "object",
3318 // "properties": {
3319 // "stackFrames": {
3320 // "type": "array",
3321 // "items": {
3322 // "$ref": "#/definitions/StackFrame"
3323 // },
3324 // "description": "The frames of the stackframe. If the array has
3325 // length zero, there are no stackframes available. This means that
3326 // there is no location information available."
3327 // },
3328 // "totalFrames": {
3329 // "type": "integer",
3330 // "description": "The total number of frames available in the
3331 // stack. If omitted or if `totalFrames` is larger than the
3332 // available frames, a client is expected to request frames until
3333 // a request returns less frames than requested (which indicates
3334 // the end of the stack). Returning monotonically increasing
3335 // `totalFrames` values for subsequent requests can be used to
3336 // enforce paging in the client."
3337 // }
3338 // },
3339 // "required": [ "stackFrames" ]
3340 // }
3341 // },
3342 // "required": [ "body" ]
3343 // }]
3344 // }
3345 void request_stackTrace(DAP &dap, const llvm::json::Object &request) {
3346 llvm::json::Object response;
3347 FillResponse(request, response);
3348 lldb::SBError error;
3349 const auto *arguments = request.getObject("arguments");
3350 lldb::SBThread thread = dap.GetLLDBThread(*arguments);
3351 llvm::json::Array stack_frames;
3352 llvm::json::Object body;
3354 if (thread.IsValid()) {
3355 const auto start_frame = GetUnsigned(arguments, "startFrame", 0);
3356 const auto levels = GetUnsigned(arguments, "levels", 0);
3357 int64_t offset = 0;
3358 bool reached_end_of_stack =
3359 FillStackFrames(dap, thread, stack_frames, offset, start_frame,
3360 levels == 0 ? INT64_MAX : levels);
3361 body.try_emplace("totalFrames",
3362 start_frame + stack_frames.size() +
3363 (reached_end_of_stack ? 0 : StackPageSize));
3366 body.try_emplace("stackFrames", std::move(stack_frames));
3367 response.try_emplace("body", std::move(body));
3368 dap.SendJSON(llvm::json::Value(std::move(response)));
3371 // "StepInRequest": {
3372 // "allOf": [ { "$ref": "#/definitions/Request" }, {
3373 // "type": "object",
3374 // "description": "StepIn request; value of command field is 'stepIn'. The
3375 // request starts the debuggee to step into a function/method if possible.
3376 // If it cannot step into a target, 'stepIn' behaves like 'next'. The debug
3377 // adapter first sends the StepInResponse and then a StoppedEvent (event
3378 // type 'step') after the step has completed. If there are multiple
3379 // function/method calls (or other targets) on the source line, the optional
3380 // argument 'targetId' can be used to control into which target the 'stepIn'
3381 // should occur. The list of possible targets for a given source line can be
3382 // retrieved via the 'stepInTargets' request.", "properties": {
3383 // "command": {
3384 // "type": "string",
3385 // "enum": [ "stepIn" ]
3386 // },
3387 // "arguments": {
3388 // "$ref": "#/definitions/StepInArguments"
3389 // }
3390 // },
3391 // "required": [ "command", "arguments" ]
3392 // }]
3393 // },
3394 // "StepInArguments": {
3395 // "type": "object",
3396 // "description": "Arguments for 'stepIn' request.",
3397 // "properties": {
3398 // "threadId": {
3399 // "type": "integer",
3400 // "description": "Execute 'stepIn' for this thread."
3401 // },
3402 // "targetId": {
3403 // "type": "integer",
3404 // "description": "Optional id of the target to step into."
3405 // },
3406 // "granularity": {
3407 // "$ref": "#/definitions/SteppingGranularity",
3408 // "description": "Stepping granularity. If no granularity is specified, a
3409 // granularity of `statement` is assumed."
3410 // }
3411 // },
3412 // "required": [ "threadId" ]
3413 // },
3414 // "StepInResponse": {
3415 // "allOf": [ { "$ref": "#/definitions/Response" }, {
3416 // "type": "object",
3417 // "description": "Response to 'stepIn' request. This is just an
3418 // acknowledgement, so no body field is required."
3419 // }]
3420 // }
3421 void request_stepIn(DAP &dap, const llvm::json::Object &request) {
3422 llvm::json::Object response;
3423 FillResponse(request, response);
3424 const auto *arguments = request.getObject("arguments");
3426 std::string step_in_target;
3427 uint64_t target_id = GetUnsigned(arguments, "targetId", 0);
3428 auto it = dap.step_in_targets.find(target_id);
3429 if (it != dap.step_in_targets.end())
3430 step_in_target = it->second;
3432 const bool single_thread = GetBoolean(arguments, "singleThread", false);
3433 lldb::RunMode run_mode =
3434 single_thread ? lldb::eOnlyThisThread : lldb::eOnlyDuringStepping;
3435 lldb::SBThread thread = dap.GetLLDBThread(*arguments);
3436 if (thread.IsValid()) {
3437 // Remember the thread ID that caused the resume so we can set the
3438 // "threadCausedFocus" boolean value in the "stopped" events.
3439 dap.focus_tid = thread.GetThreadID();
3440 if (hasInstructionGranularity(*arguments)) {
3441 thread.StepInstruction(/*step_over=*/false);
3442 } else {
3443 thread.StepInto(step_in_target.c_str(), run_mode);
3445 } else {
3446 response["success"] = llvm::json::Value(false);
3448 dap.SendJSON(llvm::json::Value(std::move(response)));
3451 // "StepInTargetsRequest": {
3452 // "allOf": [ { "$ref": "#/definitions/Request" }, {
3453 // "type": "object",
3454 // "description": "This request retrieves the possible step-in targets for
3455 // the specified stack frame.\nThese targets can be used in the `stepIn`
3456 // request.\nClients should only call this request if the corresponding
3457 // capability `supportsStepInTargetsRequest` is true.", "properties": {
3458 // "command": {
3459 // "type": "string",
3460 // "enum": [ "stepInTargets" ]
3461 // },
3462 // "arguments": {
3463 // "$ref": "#/definitions/StepInTargetsArguments"
3464 // }
3465 // },
3466 // "required": [ "command", "arguments" ]
3467 // }]
3468 // },
3469 // "StepInTargetsArguments": {
3470 // "type": "object",
3471 // "description": "Arguments for `stepInTargets` request.",
3472 // "properties": {
3473 // "frameId": {
3474 // "type": "integer",
3475 // "description": "The stack frame for which to retrieve the possible
3476 // step-in targets."
3477 // }
3478 // },
3479 // "required": [ "frameId" ]
3480 // },
3481 // "StepInTargetsResponse": {
3482 // "allOf": [ { "$ref": "#/definitions/Response" }, {
3483 // "type": "object",
3484 // "description": "Response to `stepInTargets` request.",
3485 // "properties": {
3486 // "body": {
3487 // "type": "object",
3488 // "properties": {
3489 // "targets": {
3490 // "type": "array",
3491 // "items": {
3492 // "$ref": "#/definitions/StepInTarget"
3493 // },
3494 // "description": "The possible step-in targets of the specified
3495 // source location."
3496 // }
3497 // },
3498 // "required": [ "targets" ]
3499 // }
3500 // },
3501 // "required": [ "body" ]
3502 // }]
3503 // }
3504 void request_stepInTargets(DAP &dap, const llvm::json::Object &request) {
3505 llvm::json::Object response;
3506 FillResponse(request, response);
3507 const auto *arguments = request.getObject("arguments");
3509 dap.step_in_targets.clear();
3510 lldb::SBFrame frame = dap.GetLLDBFrame(*arguments);
3511 if (frame.IsValid()) {
3512 lldb::SBAddress pc_addr = frame.GetPCAddress();
3513 lldb::SBAddress line_end_addr =
3514 pc_addr.GetLineEntry().GetSameLineContiguousAddressRangeEnd(true);
3515 lldb::SBInstructionList insts = dap.target.ReadInstructions(
3516 pc_addr, line_end_addr, /*flavor_string=*/nullptr);
3518 if (!insts.IsValid()) {
3519 response["success"] = false;
3520 response["message"] = "Failed to get instructions for frame.";
3521 dap.SendJSON(llvm::json::Value(std::move(response)));
3522 return;
3525 llvm::json::Array step_in_targets;
3526 const auto num_insts = insts.GetSize();
3527 for (size_t i = 0; i < num_insts; ++i) {
3528 lldb::SBInstruction inst = insts.GetInstructionAtIndex(i);
3529 if (!inst.IsValid())
3530 break;
3532 lldb::addr_t inst_addr = inst.GetAddress().GetLoadAddress(dap.target);
3534 // Note: currently only x86/x64 supports flow kind.
3535 lldb::InstructionControlFlowKind flow_kind =
3536 inst.GetControlFlowKind(dap.target);
3537 if (flow_kind == lldb::eInstructionControlFlowKindCall) {
3538 // Use call site instruction address as id which is easy to debug.
3539 llvm::json::Object step_in_target;
3540 step_in_target["id"] = inst_addr;
3542 llvm::StringRef call_operand_name = inst.GetOperands(dap.target);
3543 lldb::addr_t call_target_addr;
3544 if (call_operand_name.getAsInteger(0, call_target_addr))
3545 continue;
3547 lldb::SBAddress call_target_load_addr =
3548 dap.target.ResolveLoadAddress(call_target_addr);
3549 if (!call_target_load_addr.IsValid())
3550 continue;
3552 // The existing ThreadPlanStepInRange only accept step in target
3553 // function with debug info.
3554 lldb::SBSymbolContext sc = dap.target.ResolveSymbolContextForAddress(
3555 call_target_load_addr, lldb::eSymbolContextFunction);
3557 // The existing ThreadPlanStepInRange only accept step in target
3558 // function with debug info.
3559 std::string step_in_target_name;
3560 if (sc.IsValid() && sc.GetFunction().IsValid())
3561 step_in_target_name = sc.GetFunction().GetDisplayName();
3563 // Skip call sites if we fail to resolve its symbol name.
3564 if (step_in_target_name.empty())
3565 continue;
3567 dap.step_in_targets.try_emplace(inst_addr, step_in_target_name);
3568 step_in_target.try_emplace("label", step_in_target_name);
3569 step_in_targets.emplace_back(std::move(step_in_target));
3572 llvm::json::Object body;
3573 body.try_emplace("targets", std::move(step_in_targets));
3574 response.try_emplace("body", std::move(body));
3575 } else {
3576 response["success"] = llvm::json::Value(false);
3577 response["message"] = "Failed to get frame for input frameId.";
3579 dap.SendJSON(llvm::json::Value(std::move(response)));
3582 // "StepOutRequest": {
3583 // "allOf": [ { "$ref": "#/definitions/Request" }, {
3584 // "type": "object",
3585 // "description": "StepOut request; value of command field is 'stepOut'. The
3586 // request starts the debuggee to run again for one step. The debug adapter
3587 // first sends the StepOutResponse and then a StoppedEvent (event type
3588 // 'step') after the step has completed.", "properties": {
3589 // "command": {
3590 // "type": "string",
3591 // "enum": [ "stepOut" ]
3592 // },
3593 // "arguments": {
3594 // "$ref": "#/definitions/StepOutArguments"
3595 // }
3596 // },
3597 // "required": [ "command", "arguments" ]
3598 // }]
3599 // },
3600 // "StepOutArguments": {
3601 // "type": "object",
3602 // "description": "Arguments for 'stepOut' request.",
3603 // "properties": {
3604 // "threadId": {
3605 // "type": "integer",
3606 // "description": "Execute 'stepOut' for this thread."
3607 // }
3608 // },
3609 // "required": [ "threadId" ]
3610 // },
3611 // "StepOutResponse": {
3612 // "allOf": [ { "$ref": "#/definitions/Response" }, {
3613 // "type": "object",
3614 // "description": "Response to 'stepOut' request. This is just an
3615 // acknowledgement, so no body field is required."
3616 // }]
3617 // }
3618 void request_stepOut(DAP &dap, const llvm::json::Object &request) {
3619 llvm::json::Object response;
3620 FillResponse(request, response);
3621 const auto *arguments = request.getObject("arguments");
3622 lldb::SBThread thread = dap.GetLLDBThread(*arguments);
3623 if (thread.IsValid()) {
3624 // Remember the thread ID that caused the resume so we can set the
3625 // "threadCausedFocus" boolean value in the "stopped" events.
3626 dap.focus_tid = thread.GetThreadID();
3627 thread.StepOut();
3628 } else {
3629 response["success"] = llvm::json::Value(false);
3631 dap.SendJSON(llvm::json::Value(std::move(response)));
3634 // "ThreadsRequest": {
3635 // "allOf": [ { "$ref": "#/definitions/Request" }, {
3636 // "type": "object",
3637 // "description": "Thread request; value of command field is 'threads'. The
3638 // request retrieves a list of all threads.", "properties": {
3639 // "command": {
3640 // "type": "string",
3641 // "enum": [ "threads" ]
3642 // }
3643 // },
3644 // "required": [ "command" ]
3645 // }]
3646 // },
3647 // "ThreadsResponse": {
3648 // "allOf": [ { "$ref": "#/definitions/Response" }, {
3649 // "type": "object",
3650 // "description": "Response to 'threads' request.",
3651 // "properties": {
3652 // "body": {
3653 // "type": "object",
3654 // "properties": {
3655 // "threads": {
3656 // "type": "array",
3657 // "items": {
3658 // "$ref": "#/definitions/Thread"
3659 // },
3660 // "description": "All threads."
3661 // }
3662 // },
3663 // "required": [ "threads" ]
3664 // }
3665 // },
3666 // "required": [ "body" ]
3667 // }]
3668 // }
3669 void request_threads(DAP &dap, const llvm::json::Object &request) {
3670 lldb::SBProcess process = dap.target.GetProcess();
3671 llvm::json::Object response;
3672 FillResponse(request, response);
3674 const uint32_t num_threads = process.GetNumThreads();
3675 llvm::json::Array threads;
3676 for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) {
3677 lldb::SBThread thread = process.GetThreadAtIndex(thread_idx);
3678 threads.emplace_back(CreateThread(thread, dap.thread_format));
3680 if (threads.size() == 0) {
3681 response["success"] = llvm::json::Value(false);
3683 llvm::json::Object body;
3684 body.try_emplace("threads", std::move(threads));
3685 response.try_emplace("body", std::move(body));
3686 dap.SendJSON(llvm::json::Value(std::move(response)));
3689 // "SetVariableRequest": {
3690 // "allOf": [ { "$ref": "#/definitions/Request" }, {
3691 // "type": "object",
3692 // "description": "setVariable request; value of command field is
3693 // 'setVariable'. Set the variable with the given name in the variable
3694 // container to a new value.", "properties": {
3695 // "command": {
3696 // "type": "string",
3697 // "enum": [ "setVariable" ]
3698 // },
3699 // "arguments": {
3700 // "$ref": "#/definitions/SetVariableArguments"
3701 // }
3702 // },
3703 // "required": [ "command", "arguments" ]
3704 // }]
3705 // },
3706 // "SetVariableArguments": {
3707 // "type": "object",
3708 // "description": "Arguments for 'setVariable' request.",
3709 // "properties": {
3710 // "variablesReference": {
3711 // "type": "integer",
3712 // "description": "The reference of the variable container."
3713 // },
3714 // "name": {
3715 // "type": "string",
3716 // "description": "The name of the variable."
3717 // },
3718 // "value": {
3719 // "type": "string",
3720 // "description": "The value of the variable."
3721 // },
3722 // "format": {
3723 // "$ref": "#/definitions/ValueFormat",
3724 // "description": "Specifies details on how to format the response value."
3725 // }
3726 // },
3727 // "required": [ "variablesReference", "name", "value" ]
3728 // },
3729 // "SetVariableResponse": {
3730 // "allOf": [ { "$ref": "#/definitions/Response" }, {
3731 // "type": "object",
3732 // "description": "Response to 'setVariable' request.",
3733 // "properties": {
3734 // "body": {
3735 // "type": "object",
3736 // "properties": {
3737 // "value": {
3738 // "type": "string",
3739 // "description": "The new value of the variable."
3740 // },
3741 // "type": {
3742 // "type": "string",
3743 // "description": "The type of the new value. Typically shown in the
3744 // UI when hovering over the value."
3745 // },
3746 // "variablesReference": {
3747 // "type": "number",
3748 // "description": "If variablesReference is > 0, the new value is
3749 // structured and its children can be retrieved by passing
3750 // variablesReference to the VariablesRequest."
3751 // },
3752 // "namedVariables": {
3753 // "type": "number",
3754 // "description": "The number of named child variables. The client
3755 // can use this optional information to present the variables in a
3756 // paged UI and fetch them in chunks."
3757 // },
3758 // "indexedVariables": {
3759 // "type": "number",
3760 // "description": "The number of indexed child variables. The client
3761 // can use this optional information to present the variables in a
3762 // paged UI and fetch them in chunks."
3763 // },
3764 // "valueLocationReference": {
3765 // "type": "integer",
3766 // "description": "A reference that allows the client to request the
3767 // location where the new value is declared. For example, if the new
3768 // value is function pointer, the adapter may be able to look up the
3769 // function's location. This should be present only if the adapter
3770 // is likely to be able to resolve the location.\n\nThis reference
3771 // shares the same lifetime as the `variablesReference`. See
3772 // 'Lifetime of Object References' in the Overview section for
3773 // details."
3774 // }
3775 // },
3776 // "required": [ "value" ]
3777 // }
3778 // },
3779 // "required": [ "body" ]
3780 // }]
3781 // }
3782 void request_setVariable(DAP &dap, const llvm::json::Object &request) {
3783 llvm::json::Object response;
3784 FillResponse(request, response);
3785 llvm::json::Array variables;
3786 llvm::json::Object body;
3787 const auto *arguments = request.getObject("arguments");
3788 // This is a reference to the containing variable/scope
3789 const auto variablesReference =
3790 GetUnsigned(arguments, "variablesReference", 0);
3791 llvm::StringRef name = GetString(arguments, "name");
3793 const auto value = GetString(arguments, "value");
3794 // Set success to false just in case we don't find the variable by name
3795 response.try_emplace("success", false);
3797 lldb::SBValue variable;
3799 // The "id" is the unique integer ID that is unique within the enclosing
3800 // variablesReference. It is optionally added to any "interface Variable"
3801 // objects to uniquely identify a variable within an enclosing
3802 // variablesReference. It helps to disambiguate between two variables that
3803 // have the same name within the same scope since the "setVariables" request
3804 // only specifies the variable reference of the enclosing scope/variable, and
3805 // the name of the variable. We could have two shadowed variables with the
3806 // same name in "Locals" or "Globals". In our case the "id" absolute index
3807 // of the variable within the dap.variables list.
3808 const auto id_value = GetUnsigned(arguments, "id", UINT64_MAX);
3809 if (id_value != UINT64_MAX) {
3810 variable = dap.variables.GetVariable(id_value);
3811 } else {
3812 variable = FindVariable(dap, variablesReference, name);
3815 if (variable.IsValid()) {
3816 lldb::SBError error;
3817 bool success = variable.SetValueFromCString(value.data(), error);
3818 if (success) {
3819 VariableDescription desc(variable, dap.enable_auto_variable_summaries);
3820 EmplaceSafeString(body, "result", desc.display_value);
3821 EmplaceSafeString(body, "type", desc.display_type_name);
3823 // We don't know the index of the variable in our dap.variables
3824 // so always insert a new one to get its variablesReference.
3825 // is_permanent is false because debug console does not support
3826 // setVariable request.
3827 int64_t new_var_ref =
3828 dap.variables.InsertVariable(variable, /*is_permanent=*/false);
3829 if (variable.MightHaveChildren())
3830 body.try_emplace("variablesReference", new_var_ref);
3831 else
3832 body.try_emplace("variablesReference", 0);
3833 if (lldb::addr_t addr = variable.GetLoadAddress();
3834 addr != LLDB_INVALID_ADDRESS)
3835 body.try_emplace("memoryReference", EncodeMemoryReference(addr));
3836 if (ValuePointsToCode(variable))
3837 body.try_emplace("valueLocationReference", new_var_ref);
3838 } else {
3839 EmplaceSafeString(body, "message", std::string(error.GetCString()));
3841 response["success"] = llvm::json::Value(success);
3842 } else {
3843 response["success"] = llvm::json::Value(false);
3846 response.try_emplace("body", std::move(body));
3847 dap.SendJSON(llvm::json::Value(std::move(response)));
3850 // "VariablesRequest": {
3851 // "allOf": [ { "$ref": "#/definitions/Request" }, {
3852 // "type": "object",
3853 // "description": "Variables request; value of command field is 'variables'.
3854 // Retrieves all child variables for the given variable reference. An
3855 // optional filter can be used to limit the fetched children to either named
3856 // or indexed children.", "properties": {
3857 // "command": {
3858 // "type": "string",
3859 // "enum": [ "variables" ]
3860 // },
3861 // "arguments": {
3862 // "$ref": "#/definitions/VariablesArguments"
3863 // }
3864 // },
3865 // "required": [ "command", "arguments" ]
3866 // }]
3867 // },
3868 // "VariablesArguments": {
3869 // "type": "object",
3870 // "description": "Arguments for 'variables' request.",
3871 // "properties": {
3872 // "variablesReference": {
3873 // "type": "integer",
3874 // "description": "The Variable reference."
3875 // },
3876 // "filter": {
3877 // "type": "string",
3878 // "enum": [ "indexed", "named" ],
3879 // "description": "Optional filter to limit the child variables to either
3880 // named or indexed. If ommited, both types are fetched."
3881 // },
3882 // "start": {
3883 // "type": "integer",
3884 // "description": "The index of the first variable to return; if omitted
3885 // children start at 0."
3886 // },
3887 // "count": {
3888 // "type": "integer",
3889 // "description": "The number of variables to return. If count is missing
3890 // or 0, all variables are returned."
3891 // },
3892 // "format": {
3893 // "$ref": "#/definitions/ValueFormat",
3894 // "description": "Specifies details on how to format the Variable
3895 // values."
3896 // }
3897 // },
3898 // "required": [ "variablesReference" ]
3899 // },
3900 // "VariablesResponse": {
3901 // "allOf": [ { "$ref": "#/definitions/Response" }, {
3902 // "type": "object",
3903 // "description": "Response to 'variables' request.",
3904 // "properties": {
3905 // "body": {
3906 // "type": "object",
3907 // "properties": {
3908 // "variables": {
3909 // "type": "array",
3910 // "items": {
3911 // "$ref": "#/definitions/Variable"
3912 // },
3913 // "description": "All (or a range) of variables for the given
3914 // variable reference."
3915 // }
3916 // },
3917 // "required": [ "variables" ]
3918 // }
3919 // },
3920 // "required": [ "body" ]
3921 // }]
3922 // }
3923 void request_variables(DAP &dap, const llvm::json::Object &request) {
3924 llvm::json::Object response;
3925 FillResponse(request, response);
3926 llvm::json::Array variables;
3927 const auto *arguments = request.getObject("arguments");
3928 const auto variablesReference =
3929 GetUnsigned(arguments, "variablesReference", 0);
3930 const int64_t start = GetSigned(arguments, "start", 0);
3931 const int64_t count = GetSigned(arguments, "count", 0);
3932 bool hex = false;
3933 const auto *format = arguments->getObject("format");
3934 if (format)
3935 hex = GetBoolean(format, "hex", false);
3937 if (lldb::SBValueList *top_scope =
3938 GetTopLevelScope(dap, variablesReference)) {
3939 // variablesReference is one of our scopes, not an actual variable it is
3940 // asking for the list of args, locals or globals.
3941 int64_t start_idx = 0;
3942 int64_t num_children = 0;
3944 if (variablesReference == VARREF_REGS) {
3945 // Change the default format of any pointer sized registers in the first
3946 // register set to be the lldb::eFormatAddressInfo so we show the pointer
3947 // and resolve what the pointer resolves to. Only change the format if the
3948 // format was set to the default format or if it was hex as some registers
3949 // have formats set for them.
3950 const uint32_t addr_size = dap.target.GetProcess().GetAddressByteSize();
3951 lldb::SBValue reg_set = dap.variables.registers.GetValueAtIndex(0);
3952 const uint32_t num_regs = reg_set.GetNumChildren();
3953 for (uint32_t reg_idx = 0; reg_idx < num_regs; ++reg_idx) {
3954 lldb::SBValue reg = reg_set.GetChildAtIndex(reg_idx);
3955 const lldb::Format format = reg.GetFormat();
3956 if (format == lldb::eFormatDefault || format == lldb::eFormatHex) {
3957 if (reg.GetByteSize() == addr_size)
3958 reg.SetFormat(lldb::eFormatAddressInfo);
3963 num_children = top_scope->GetSize();
3964 if (num_children == 0 && variablesReference == VARREF_LOCALS) {
3965 // Check for an error in the SBValueList that might explain why we don't
3966 // have locals. If we have an error display it as the sole value in the
3967 // the locals.
3969 // "error" owns the error string so we must keep it alive as long as we
3970 // want to use the returns "const char *"
3971 lldb::SBError error = top_scope->GetError();
3972 const char *var_err = error.GetCString();
3973 if (var_err) {
3974 // Create a fake variable named "error" to explain why variables were
3975 // not available. This new error will help let users know when there was
3976 // a problem that kept variables from being available for display and
3977 // allow users to fix this issue instead of seeing no variables. The
3978 // errors are only set when there is a problem that the user could
3979 // fix, so no error will show up when you have no debug info, only when
3980 // we do have debug info and something that is fixable can be done.
3981 llvm::json::Object object;
3982 EmplaceSafeString(object, "name", "<error>");
3983 EmplaceSafeString(object, "type", "const char *");
3984 EmplaceSafeString(object, "value", var_err);
3985 object.try_emplace("variablesReference", (int64_t)0);
3986 variables.emplace_back(std::move(object));
3989 const int64_t end_idx = start_idx + ((count == 0) ? num_children : count);
3991 // We first find out which variable names are duplicated
3992 std::map<std::string, int> variable_name_counts;
3993 for (auto i = start_idx; i < end_idx; ++i) {
3994 lldb::SBValue variable = top_scope->GetValueAtIndex(i);
3995 if (!variable.IsValid())
3996 break;
3997 variable_name_counts[GetNonNullVariableName(variable)]++;
4000 // Now we construct the result with unique display variable names
4001 for (auto i = start_idx; i < end_idx; ++i) {
4002 lldb::SBValue variable = top_scope->GetValueAtIndex(i);
4004 if (!variable.IsValid())
4005 break;
4007 int64_t var_ref =
4008 dap.variables.InsertVariable(variable, /*is_permanent=*/false);
4009 variables.emplace_back(CreateVariable(
4010 variable, var_ref, hex, dap.enable_auto_variable_summaries,
4011 dap.enable_synthetic_child_debugging,
4012 variable_name_counts[GetNonNullVariableName(variable)] > 1));
4014 } else {
4015 // We are expanding a variable that has children, so we will return its
4016 // children.
4017 lldb::SBValue variable = dap.variables.GetVariable(variablesReference);
4018 if (variable.IsValid()) {
4019 auto addChild = [&](lldb::SBValue child,
4020 std::optional<std::string> custom_name = {}) {
4021 if (!child.IsValid())
4022 return;
4023 bool is_permanent =
4024 dap.variables.IsPermanentVariableReference(variablesReference);
4025 int64_t var_ref = dap.variables.InsertVariable(child, is_permanent);
4026 variables.emplace_back(CreateVariable(
4027 child, var_ref, hex, dap.enable_auto_variable_summaries,
4028 dap.enable_synthetic_child_debugging,
4029 /*is_name_duplicated=*/false, custom_name));
4031 const int64_t num_children = variable.GetNumChildren();
4032 int64_t end_idx = start + ((count == 0) ? num_children : count);
4033 int64_t i = start;
4034 for (; i < end_idx && i < num_children; ++i)
4035 addChild(variable.GetChildAtIndex(i));
4037 // If we haven't filled the count quota from the request, we insert a new
4038 // "[raw]" child that can be used to inspect the raw version of a
4039 // synthetic member. That eliminates the need for the user to go to the
4040 // debug console and type `frame var <variable> to get these values.
4041 if (dap.enable_synthetic_child_debugging && variable.IsSynthetic() &&
4042 i == num_children)
4043 addChild(variable.GetNonSyntheticValue(), "[raw]");
4046 llvm::json::Object body;
4047 body.try_emplace("variables", std::move(variables));
4048 response.try_emplace("body", std::move(body));
4049 dap.SendJSON(llvm::json::Value(std::move(response)));
4052 // "LocationsRequest": {
4053 // "allOf": [ { "$ref": "#/definitions/Request" }, {
4054 // "type": "object",
4055 // "description": "Looks up information about a location reference
4056 // previously returned by the debug adapter.",
4057 // "properties": {
4058 // "command": {
4059 // "type": "string",
4060 // "enum": [ "locations" ]
4061 // },
4062 // "arguments": {
4063 // "$ref": "#/definitions/LocationsArguments"
4064 // }
4065 // },
4066 // "required": [ "command", "arguments" ]
4067 // }]
4068 // },
4069 // "LocationsArguments": {
4070 // "type": "object",
4071 // "description": "Arguments for `locations` request.",
4072 // "properties": {
4073 // "locationReference": {
4074 // "type": "integer",
4075 // "description": "Location reference to resolve."
4076 // }
4077 // },
4078 // "required": [ "locationReference" ]
4079 // },
4080 // "LocationsResponse": {
4081 // "allOf": [ { "$ref": "#/definitions/Response" }, {
4082 // "type": "object",
4083 // "description": "Response to `locations` request.",
4084 // "properties": {
4085 // "body": {
4086 // "type": "object",
4087 // "properties": {
4088 // "source": {
4089 // "$ref": "#/definitions/Source",
4090 // "description": "The source containing the location; either
4091 // `source.path` or `source.sourceReference` must be
4092 // specified."
4093 // },
4094 // "line": {
4095 // "type": "integer",
4096 // "description": "The line number of the location. The client
4097 // capability `linesStartAt1` determines whether it
4098 // is 0- or 1-based."
4099 // },
4100 // "column": {
4101 // "type": "integer",
4102 // "description": "Position of the location within the `line`. It is
4103 // measured in UTF-16 code units and the client
4104 // capability `columnsStartAt1` determines whether
4105 // it is 0- or 1-based. If no column is given, the
4106 // first position in the start line is assumed."
4107 // },
4108 // "endLine": {
4109 // "type": "integer",
4110 // "description": "End line of the location, present if the location
4111 // refers to a range. The client capability
4112 // `linesStartAt1` determines whether it is 0- or
4113 // 1-based."
4114 // },
4115 // "endColumn": {
4116 // "type": "integer",
4117 // "description": "End position of the location within `endLine`,
4118 // present if the location refers to a range. It is
4119 // measured in UTF-16 code units and the client
4120 // capability `columnsStartAt1` determines whether
4121 // it is 0- or 1-based."
4122 // }
4123 // },
4124 // "required": [ "source", "line" ]
4125 // }
4126 // }
4127 // }]
4128 // },
4129 void request_locations(DAP &dap, const llvm::json::Object &request) {
4130 llvm::json::Object response;
4131 FillResponse(request, response);
4132 auto *arguments = request.getObject("arguments");
4134 uint64_t location_id = GetUnsigned(arguments, "locationReference", 0);
4135 // We use the lowest bit to distinguish between value location and declaration
4136 // location
4137 auto [var_ref, is_value_location] = UnpackLocation(location_id);
4138 lldb::SBValue variable = dap.variables.GetVariable(var_ref);
4139 if (!variable.IsValid()) {
4140 response["success"] = false;
4141 response["message"] = "Invalid variable reference";
4142 dap.SendJSON(llvm::json::Value(std::move(response)));
4143 return;
4146 llvm::json::Object body;
4147 if (is_value_location) {
4148 // Get the value location
4149 if (!variable.GetType().IsPointerType() &&
4150 !variable.GetType().IsReferenceType()) {
4151 response["success"] = false;
4152 response["message"] =
4153 "Value locations are only available for pointers and references";
4154 dap.SendJSON(llvm::json::Value(std::move(response)));
4155 return;
4158 lldb::addr_t addr = variable.GetValueAsAddress();
4159 lldb::SBLineEntry line_entry =
4160 dap.target.ResolveLoadAddress(addr).GetLineEntry();
4162 if (!line_entry.IsValid()) {
4163 response["success"] = false;
4164 response["message"] = "Failed to resolve line entry for location";
4165 dap.SendJSON(llvm::json::Value(std::move(response)));
4166 return;
4169 body.try_emplace("source", CreateSource(line_entry.GetFileSpec()));
4170 if (int line = line_entry.GetLine())
4171 body.try_emplace("line", line);
4172 if (int column = line_entry.GetColumn())
4173 body.try_emplace("column", column);
4174 } else {
4175 // Get the declaration location
4176 lldb::SBDeclaration decl = variable.GetDeclaration();
4177 if (!decl.IsValid()) {
4178 response["success"] = false;
4179 response["message"] = "No declaration location available";
4180 dap.SendJSON(llvm::json::Value(std::move(response)));
4181 return;
4184 body.try_emplace("source", CreateSource(decl.GetFileSpec()));
4185 if (int line = decl.GetLine())
4186 body.try_emplace("line", line);
4187 if (int column = decl.GetColumn())
4188 body.try_emplace("column", column);
4191 response.try_emplace("body", std::move(body));
4192 dap.SendJSON(llvm::json::Value(std::move(response)));
4195 // "DisassembleRequest": {
4196 // "allOf": [ { "$ref": "#/definitions/Request" }, {
4197 // "type": "object",
4198 // "description": "Disassembles code stored at the provided
4199 // location.\nClients should only call this request if the corresponding
4200 // capability `supportsDisassembleRequest` is true.", "properties": {
4201 // "command": {
4202 // "type": "string",
4203 // "enum": [ "disassemble" ]
4204 // },
4205 // "arguments": {
4206 // "$ref": "#/definitions/DisassembleArguments"
4207 // }
4208 // },
4209 // "required": [ "command", "arguments" ]
4210 // }]
4211 // },
4212 // "DisassembleArguments": {
4213 // "type": "object",
4214 // "description": "Arguments for `disassemble` request.",
4215 // "properties": {
4216 // "memoryReference": {
4217 // "type": "string",
4218 // "description": "Memory reference to the base location containing the
4219 // instructions to disassemble."
4220 // },
4221 // "offset": {
4222 // "type": "integer",
4223 // "description": "Offset (in bytes) to be applied to the reference
4224 // location before disassembling. Can be negative."
4225 // },
4226 // "instructionOffset": {
4227 // "type": "integer",
4228 // "description": "Offset (in instructions) to be applied after the byte
4229 // offset (if any) before disassembling. Can be negative."
4230 // },
4231 // "instructionCount": {
4232 // "type": "integer",
4233 // "description": "Number of instructions to disassemble starting at the
4234 // specified location and offset.\nAn adapter must return exactly this
4235 // number of instructions - any unavailable instructions should be
4236 // replaced with an implementation-defined 'invalid instruction' value."
4237 // },
4238 // "resolveSymbols": {
4239 // "type": "boolean",
4240 // "description": "If true, the adapter should attempt to resolve memory
4241 // addresses and other values to symbolic names."
4242 // }
4243 // },
4244 // "required": [ "memoryReference", "instructionCount" ]
4245 // },
4246 // "DisassembleResponse": {
4247 // "allOf": [ { "$ref": "#/definitions/Response" }, {
4248 // "type": "object",
4249 // "description": "Response to `disassemble` request.",
4250 // "properties": {
4251 // "body": {
4252 // "type": "object",
4253 // "properties": {
4254 // "instructions": {
4255 // "type": "array",
4256 // "items": {
4257 // "$ref": "#/definitions/DisassembledInstruction"
4258 // },
4259 // "description": "The list of disassembled instructions."
4260 // }
4261 // },
4262 // "required": [ "instructions" ]
4263 // }
4264 // }
4265 // }]
4266 // }
4267 void request_disassemble(DAP &dap, const llvm::json::Object &request) {
4268 llvm::json::Object response;
4269 FillResponse(request, response);
4270 auto *arguments = request.getObject("arguments");
4272 llvm::StringRef memoryReference = GetString(arguments, "memoryReference");
4273 auto addr_opt = DecodeMemoryReference(memoryReference);
4274 if (!addr_opt.has_value()) {
4275 response["success"] = false;
4276 response["message"] =
4277 "Malformed memory reference: " + memoryReference.str();
4278 dap.SendJSON(llvm::json::Value(std::move(response)));
4279 return;
4281 lldb::addr_t addr_ptr = *addr_opt;
4283 addr_ptr += GetSigned(arguments, "instructionOffset", 0);
4284 lldb::SBAddress addr(addr_ptr, dap.target);
4285 if (!addr.IsValid()) {
4286 response["success"] = false;
4287 response["message"] = "Memory reference not found in the current binary.";
4288 dap.SendJSON(llvm::json::Value(std::move(response)));
4289 return;
4292 const auto inst_count = GetUnsigned(arguments, "instructionCount", 0);
4293 lldb::SBInstructionList insts = dap.target.ReadInstructions(addr, inst_count);
4295 if (!insts.IsValid()) {
4296 response["success"] = false;
4297 response["message"] = "Failed to find instructions for memory address.";
4298 dap.SendJSON(llvm::json::Value(std::move(response)));
4299 return;
4302 const bool resolveSymbols = GetBoolean(arguments, "resolveSymbols", false);
4303 llvm::json::Array instructions;
4304 const auto num_insts = insts.GetSize();
4305 for (size_t i = 0; i < num_insts; ++i) {
4306 lldb::SBInstruction inst = insts.GetInstructionAtIndex(i);
4307 auto addr = inst.GetAddress();
4308 const auto inst_addr = addr.GetLoadAddress(dap.target);
4309 const char *m = inst.GetMnemonic(dap.target);
4310 const char *o = inst.GetOperands(dap.target);
4311 const char *c = inst.GetComment(dap.target);
4312 auto d = inst.GetData(dap.target);
4314 std::string bytes;
4315 llvm::raw_string_ostream sb(bytes);
4316 for (unsigned i = 0; i < inst.GetByteSize(); i++) {
4317 lldb::SBError error;
4318 uint8_t b = d.GetUnsignedInt8(error, i);
4319 if (error.Success()) {
4320 sb << llvm::format("%2.2x ", b);
4324 llvm::json::Object disassembled_inst{
4325 {"address", "0x" + llvm::utohexstr(inst_addr)},
4326 {"instructionBytes",
4327 bytes.size() > 0 ? bytes.substr(0, bytes.size() - 1) : ""},
4330 std::string instruction;
4331 llvm::raw_string_ostream si(instruction);
4333 lldb::SBSymbol symbol = addr.GetSymbol();
4334 // Only add the symbol on the first line of the function.
4335 if (symbol.IsValid() && symbol.GetStartAddress() == addr) {
4336 // If we have a valid symbol, append it as a label prefix for the first
4337 // instruction. This is so you can see the start of a function/callsite
4338 // in the assembly, at the moment VS Code (1.80) does not visualize the
4339 // symbol associated with the assembly instruction.
4340 si << (symbol.GetMangledName() != nullptr ? symbol.GetMangledName()
4341 : symbol.GetName())
4342 << ": ";
4344 if (resolveSymbols) {
4345 disassembled_inst.try_emplace("symbol", symbol.GetDisplayName());
4349 si << llvm::formatv("{0,7} {1,12}", m, o);
4350 if (c && c[0]) {
4351 si << " ; " << c;
4354 disassembled_inst.try_emplace("instruction", instruction);
4356 auto line_entry = addr.GetLineEntry();
4357 // If the line number is 0 then the entry represents a compiler generated
4358 // location.
4359 if (line_entry.GetStartAddress() == addr && line_entry.IsValid() &&
4360 line_entry.GetFileSpec().IsValid() && line_entry.GetLine() != 0) {
4361 auto source = CreateSource(line_entry);
4362 disassembled_inst.try_emplace("location", source);
4364 const auto line = line_entry.GetLine();
4365 if (line && line != LLDB_INVALID_LINE_NUMBER) {
4366 disassembled_inst.try_emplace("line", line);
4368 const auto column = line_entry.GetColumn();
4369 if (column && column != LLDB_INVALID_COLUMN_NUMBER) {
4370 disassembled_inst.try_emplace("column", column);
4373 auto end_line_entry = line_entry.GetEndAddress().GetLineEntry();
4374 if (end_line_entry.IsValid() &&
4375 end_line_entry.GetFileSpec() == line_entry.GetFileSpec()) {
4376 const auto end_line = end_line_entry.GetLine();
4377 if (end_line && end_line != LLDB_INVALID_LINE_NUMBER &&
4378 end_line != line) {
4379 disassembled_inst.try_emplace("endLine", end_line);
4381 const auto end_column = end_line_entry.GetColumn();
4382 if (end_column && end_column != LLDB_INVALID_COLUMN_NUMBER &&
4383 end_column != column) {
4384 disassembled_inst.try_emplace("endColumn", end_column - 1);
4390 instructions.emplace_back(std::move(disassembled_inst));
4393 llvm::json::Object body;
4394 body.try_emplace("instructions", std::move(instructions));
4395 response.try_emplace("body", std::move(body));
4396 dap.SendJSON(llvm::json::Value(std::move(response)));
4399 // "ReadMemoryRequest": {
4400 // "allOf": [ { "$ref": "#/definitions/Request" }, {
4401 // "type": "object",
4402 // "description": "Reads bytes from memory at the provided location. Clients
4403 // should only call this request if the corresponding
4404 // capability `supportsReadMemoryRequest` is true.",
4405 // "properties": {
4406 // "command": {
4407 // "type": "string",
4408 // "enum": [ "readMemory" ]
4409 // },
4410 // "arguments": {
4411 // "$ref": "#/definitions/ReadMemoryArguments"
4412 // }
4413 // },
4414 // "required": [ "command", "arguments" ]
4415 // }]
4416 // },
4417 // "ReadMemoryArguments": {
4418 // "type": "object",
4419 // "description": "Arguments for `readMemory` request.",
4420 // "properties": {
4421 // "memoryReference": {
4422 // "type": "string",
4423 // "description": "Memory reference to the base location from which data
4424 // should be read."
4425 // },
4426 // "offset": {
4427 // "type": "integer",
4428 // "description": "Offset (in bytes) to be applied to the reference
4429 // location before reading data. Can be negative."
4430 // },
4431 // "count": {
4432 // "type": "integer",
4433 // "description": "Number of bytes to read at the specified location and
4434 // offset."
4435 // }
4436 // },
4437 // "required": [ "memoryReference", "count" ]
4438 // },
4439 // "ReadMemoryResponse": {
4440 // "allOf": [ { "$ref": "#/definitions/Response" }, {
4441 // "type": "object",
4442 // "description": "Response to `readMemory` request.",
4443 // "properties": {
4444 // "body": {
4445 // "type": "object",
4446 // "properties": {
4447 // "address": {
4448 // "type": "string",
4449 // "description": "The address of the first byte of data returned.
4450 // Treated as a hex value if prefixed with `0x`, or
4451 // as a decimal value otherwise."
4452 // },
4453 // "unreadableBytes": {
4454 // "type": "integer",
4455 // "description": "The number of unreadable bytes encountered after
4456 // the last successfully read byte.\nThis can be
4457 // used to determine the number of bytes that should
4458 // be skipped before a subsequent
4459 // `readMemory` request succeeds."
4460 // },
4461 // "data": {
4462 // "type": "string",
4463 // "description": "The bytes read from memory, encoded using base64.
4464 // If the decoded length of `data` is less than the
4465 // requested `count` in the original `readMemory`
4466 // request, and `unreadableBytes` is zero or
4467 // omitted, then the client should assume it's
4468 // reached the end of readable memory."
4469 // }
4470 // },
4471 // "required": [ "address" ]
4472 // }
4473 // }
4474 // }]
4475 // },
4476 void request_readMemory(DAP &dap, const llvm::json::Object &request) {
4477 llvm::json::Object response;
4478 FillResponse(request, response);
4479 auto *arguments = request.getObject("arguments");
4481 llvm::StringRef memoryReference = GetString(arguments, "memoryReference");
4482 auto addr_opt = DecodeMemoryReference(memoryReference);
4483 if (!addr_opt.has_value()) {
4484 response["success"] = false;
4485 response["message"] =
4486 "Malformed memory reference: " + memoryReference.str();
4487 dap.SendJSON(llvm::json::Value(std::move(response)));
4488 return;
4490 lldb::addr_t addr_int = *addr_opt;
4491 addr_int += GetSigned(arguments, "offset", 0);
4492 const uint64_t count_requested = GetUnsigned(arguments, "count", 0);
4494 // We also need support reading 0 bytes
4495 // VS Code sends those requests to check if a `memoryReference`
4496 // can be dereferenced.
4497 const uint64_t count_read = std::max<uint64_t>(count_requested, 1);
4498 std::vector<uint8_t> buf;
4499 buf.resize(count_read);
4500 lldb::SBError error;
4501 lldb::SBAddress addr{addr_int, dap.target};
4502 size_t count_result =
4503 dap.target.ReadMemory(addr, buf.data(), count_read, error);
4504 if (count_result == 0) {
4505 response["success"] = false;
4506 EmplaceSafeString(response, "message", error.GetCString());
4507 dap.SendJSON(llvm::json::Value(std::move(response)));
4508 return;
4510 buf.resize(std::min<size_t>(count_result, count_requested));
4512 llvm::json::Object body;
4513 std::string formatted_addr = "0x" + llvm::utohexstr(addr_int);
4514 body.try_emplace("address", formatted_addr);
4515 body.try_emplace("data", llvm::encodeBase64(buf));
4516 response.try_emplace("body", std::move(body));
4517 dap.SendJSON(llvm::json::Value(std::move(response)));
4520 // A request used in testing to get the details on all breakpoints that are
4521 // currently set in the target. This helps us to test "setBreakpoints" and
4522 // "setFunctionBreakpoints" requests to verify we have the correct set of
4523 // breakpoints currently set in LLDB.
4524 void request__testGetTargetBreakpoints(DAP &dap,
4525 const llvm::json::Object &request) {
4526 llvm::json::Object response;
4527 FillResponse(request, response);
4528 llvm::json::Array response_breakpoints;
4529 for (uint32_t i = 0; dap.target.GetBreakpointAtIndex(i).IsValid(); ++i) {
4530 auto bp = Breakpoint(dap, dap.target.GetBreakpointAtIndex(i));
4531 AppendBreakpoint(&bp, response_breakpoints);
4533 llvm::json::Object body;
4534 body.try_emplace("breakpoints", std::move(response_breakpoints));
4535 response.try_emplace("body", std::move(body));
4536 dap.SendJSON(llvm::json::Value(std::move(response)));
4539 // "SetInstructionBreakpointsRequest": {
4540 // "allOf": [
4541 // {"$ref": "#/definitions/Request"},
4542 // {
4543 // "type": "object",
4544 // "description" :
4545 // "Replaces all existing instruction breakpoints. Typically, "
4546 // "instruction breakpoints would be set from a disassembly window. "
4547 // "\nTo clear all instruction breakpoints, specify an empty "
4548 // "array.\nWhen an instruction breakpoint is hit, a `stopped` event "
4549 // "(with reason `instruction breakpoint`) is generated.\nClients "
4550 // "should only call this request if the corresponding capability "
4551 // "`supportsInstructionBreakpoints` is true.",
4552 // "properties": {
4553 // "command": { "type": "string", "enum": ["setInstructionBreakpoints"]
4554 // }, "arguments": {"$ref":
4555 // "#/definitions/SetInstructionBreakpointsArguments"}
4556 // },
4557 // "required": [ "command", "arguments" ]
4558 // }
4559 // ]
4560 // },
4561 // "SetInstructionBreakpointsArguments": {
4562 // "type": "object",
4563 // "description": "Arguments for `setInstructionBreakpoints` request",
4564 // "properties": {
4565 // "breakpoints": {
4566 // "type": "array",
4567 // "items": {"$ref": "#/definitions/InstructionBreakpoint"},
4568 // "description": "The instruction references of the breakpoints"
4569 // }
4570 // },
4571 // "required": ["breakpoints"]
4572 // },
4573 // "SetInstructionBreakpointsResponse": {
4574 // "allOf": [
4575 // {"$ref": "#/definitions/Response"},
4576 // {
4577 // "type": "object",
4578 // "description": "Response to `setInstructionBreakpoints` request",
4579 // "properties": {
4580 // "body": {
4581 // "type": "object",
4582 // "properties": {
4583 // "breakpoints": {
4584 // "type": "array",
4585 // "items": {"$ref": "#/definitions/Breakpoint"},
4586 // "description":
4587 // "Information about the breakpoints. The array elements
4588 // " "correspond to the elements of the `breakpoints`
4589 // array."
4590 // }
4591 // },
4592 // "required": ["breakpoints"]
4593 // }
4594 // },
4595 // "required": ["body"]
4596 // }
4597 // ]
4598 // },
4599 // "InstructionBreakpoint": {
4600 // "type": "object",
4601 // "description": "Properties of a breakpoint passed to the "
4602 // "`setInstructionBreakpoints` request",
4603 // "properties": {
4604 // "instructionReference": {
4605 // "type": "string",
4606 // "description" :
4607 // "The instruction reference of the breakpoint.\nThis should be a "
4608 // "memory or instruction pointer reference from an
4609 // `EvaluateResponse`, "
4610 // "`Variable`, `StackFrame`, `GotoTarget`, or `Breakpoint`."
4611 // },
4612 // "offset": {
4613 // "type": "integer",
4614 // "description": "The offset from the instruction reference in "
4615 // "bytes.\nThis can be negative."
4616 // },
4617 // "condition": {
4618 // "type": "string",
4619 // "description": "An expression for conditional breakpoints.\nIt is only
4620 // "
4621 // "honored by a debug adapter if the corresponding "
4622 // "capability `supportsConditionalBreakpoints` is true."
4623 // },
4624 // "hitCondition": {
4625 // "type": "string",
4626 // "description": "An expression that controls how many hits of the "
4627 // "breakpoint are ignored.\nThe debug adapter is expected
4628 // " "to interpret the expression as needed.\nThe
4629 // attribute " "is only honored by a debug adapter if the
4630 // corresponding " "capability
4631 // `supportsHitConditionalBreakpoints` is true."
4632 // },
4633 // "mode": {
4634 // "type": "string",
4635 // "description": "The mode of this breakpoint. If defined, this must be
4636 // "
4637 // "one of the `breakpointModes` the debug adapter "
4638 // "advertised in its `Capabilities`."
4639 // }
4640 // },
4641 // "required": ["instructionReference"]
4642 // },
4643 // "Breakpoint": {
4644 // "type": "object",
4645 // "description" :
4646 // "Information about a breakpoint created in `setBreakpoints`, "
4647 // "`setFunctionBreakpoints`, `setInstructionBreakpoints`, or "
4648 // "`setDataBreakpoints` requests.",
4649 // "properties": {
4650 // "id": {
4651 // "type": "integer",
4652 // "description" :
4653 // "The identifier for the breakpoint. It is needed if breakpoint
4654 // " "events are used to update or remove breakpoints."
4655 // },
4656 // "verified": {
4657 // "type": "boolean",
4658 // "description": "If true, the breakpoint could be set (but not "
4659 // "necessarily at the desired location)."
4660 // },
4661 // "message": {
4662 // "type": "string",
4663 // "description": "A message about the state of the breakpoint.\nThis
4664 // "
4665 // "is shown to the user and can be used to explain
4666 // why " "a breakpoint could not be verified."
4667 // },
4668 // "source": {
4669 // "$ref": "#/definitions/Source",
4670 // "description": "The source where the breakpoint is located."
4671 // },
4672 // "line": {
4673 // "type": "integer",
4674 // "description" :
4675 // "The start line of the actual range covered by the breakpoint."
4676 // },
4677 // "column": {
4678 // "type": "integer",
4679 // "description" :
4680 // "Start position of the source range covered by the breakpoint.
4681 // " "It is measured in UTF-16 code units and the client
4682 // capability "
4683 // "`columnsStartAt1` determines whether it is 0- or 1-based."
4684 // },
4685 // "endLine": {
4686 // "type": "integer",
4687 // "description" :
4688 // "The end line of the actual range covered by the breakpoint."
4689 // },
4690 // "endColumn": {
4691 // "type": "integer",
4692 // "description" :
4693 // "End position of the source range covered by the breakpoint. It
4694 // " "is measured in UTF-16 code units and the client capability "
4695 // "`columnsStartAt1` determines whether it is 0- or 1-based.\nIf
4696 // " "no end line is given, then the end column is assumed to be
4697 // in " "the start line."
4698 // },
4699 // "instructionReference": {
4700 // "type": "string",
4701 // "description": "A memory reference to where the breakpoint is
4702 // set."
4703 // },
4704 // "offset": {
4705 // "type": "integer",
4706 // "description": "The offset from the instruction reference.\nThis "
4707 // "can be negative."
4708 // },
4709 // "reason": {
4710 // "type": "string",
4711 // "description" :
4712 // "A machine-readable explanation of why a breakpoint may not be
4713 // " "verified. If a breakpoint is verified or a specific reason
4714 // is " "not known, the adapter should omit this property.
4715 // Possible " "values include:\n\n- `pending`: Indicates a
4716 // breakpoint might be " "verified in the future, but the adapter
4717 // cannot verify it in the " "current state.\n - `failed`:
4718 // Indicates a breakpoint was not " "able to be verified, and the
4719 // adapter does not believe it can be " "verified without
4720 // intervention.",
4721 // "enum": [ "pending", "failed" ]
4722 // }
4723 // },
4724 // "required": ["verified"]
4725 // },
4726 void request_setInstructionBreakpoints(DAP &dap,
4727 const llvm::json::Object &request) {
4728 llvm::json::Object response;
4729 llvm::json::Array response_breakpoints;
4730 llvm::json::Object body;
4731 FillResponse(request, response);
4733 const auto *arguments = request.getObject("arguments");
4734 const auto *breakpoints = arguments->getArray("breakpoints");
4736 // Disable any instruction breakpoints that aren't in this request.
4737 // There is no call to remove instruction breakpoints other than calling this
4738 // function with a smaller or empty "breakpoints" list.
4739 llvm::DenseSet<lldb::addr_t> seen;
4740 for (const auto &addr : dap.instruction_breakpoints)
4741 seen.insert(addr.first);
4743 for (const auto &bp : *breakpoints) {
4744 const auto *bp_obj = bp.getAsObject();
4745 if (!bp_obj)
4746 continue;
4747 // Read instruction breakpoint request.
4748 InstructionBreakpoint inst_bp(dap, *bp_obj);
4749 const auto [iv, inserted] = dap.instruction_breakpoints.try_emplace(
4750 inst_bp.instructionAddressReference, dap, *bp_obj);
4751 if (inserted)
4752 iv->second.SetBreakpoint();
4753 else
4754 iv->second.UpdateBreakpoint(inst_bp);
4755 AppendBreakpoint(&iv->second, response_breakpoints);
4756 seen.erase(inst_bp.instructionAddressReference);
4759 for (const auto &addr : seen) {
4760 auto inst_bp = dap.instruction_breakpoints.find(addr);
4761 if (inst_bp == dap.instruction_breakpoints.end())
4762 continue;
4763 dap.target.BreakpointDelete(inst_bp->second.bp.GetID());
4764 dap.instruction_breakpoints.erase(addr);
4767 body.try_emplace("breakpoints", std::move(response_breakpoints));
4768 response.try_emplace("body", std::move(body));
4769 dap.SendJSON(llvm::json::Value(std::move(response)));
4772 void RegisterRequestCallbacks(DAP &dap) {
4773 dap.RegisterRequestCallback("attach", request_attach);
4774 dap.RegisterRequestCallback("completions", request_completions);
4775 dap.RegisterRequestCallback("continue", request_continue);
4776 dap.RegisterRequestCallback("configurationDone", request_configurationDone);
4777 dap.RegisterRequestCallback("disconnect", request_disconnect);
4778 dap.RegisterRequestCallback("evaluate", request_evaluate);
4779 dap.RegisterRequestCallback("exceptionInfo", request_exceptionInfo);
4780 dap.RegisterRequestCallback("initialize", request_initialize);
4781 dap.RegisterRequestCallback("launch", request_launch);
4782 dap.RegisterRequestCallback("next", request_next);
4783 dap.RegisterRequestCallback("pause", request_pause);
4784 dap.RegisterRequestCallback("restart", request_restart);
4785 dap.RegisterRequestCallback("scopes", request_scopes);
4786 dap.RegisterRequestCallback("setBreakpoints", request_setBreakpoints);
4787 dap.RegisterRequestCallback("setExceptionBreakpoints",
4788 request_setExceptionBreakpoints);
4789 dap.RegisterRequestCallback("setFunctionBreakpoints",
4790 request_setFunctionBreakpoints);
4791 dap.RegisterRequestCallback("dataBreakpointInfo", request_dataBreakpointInfo);
4792 dap.RegisterRequestCallback("setDataBreakpoints", request_setDataBreakpoints);
4793 dap.RegisterRequestCallback("setVariable", request_setVariable);
4794 dap.RegisterRequestCallback("source", request_source);
4795 dap.RegisterRequestCallback("stackTrace", request_stackTrace);
4796 dap.RegisterRequestCallback("stepIn", request_stepIn);
4797 dap.RegisterRequestCallback("stepInTargets", request_stepInTargets);
4798 dap.RegisterRequestCallback("stepOut", request_stepOut);
4799 dap.RegisterRequestCallback("threads", request_threads);
4800 dap.RegisterRequestCallback("variables", request_variables);
4801 dap.RegisterRequestCallback("locations", request_locations);
4802 dap.RegisterRequestCallback("disassemble", request_disassemble);
4803 dap.RegisterRequestCallback("readMemory", request_readMemory);
4804 dap.RegisterRequestCallback("setInstructionBreakpoints",
4805 request_setInstructionBreakpoints);
4806 // Custom requests
4807 dap.RegisterRequestCallback("compileUnits", request_compileUnits);
4808 dap.RegisterRequestCallback("modules", request_modules);
4809 // Testing requests
4810 dap.RegisterRequestCallback("_testGetTargetBreakpoints",
4811 request__testGetTargetBreakpoints);
4814 } // anonymous namespace
4816 static void printHelp(LLDBDAPOptTable &table, llvm::StringRef tool_name) {
4817 std::string usage_str = tool_name.str() + " options";
4818 table.printHelp(llvm::outs(), usage_str.c_str(), "LLDB DAP", false);
4820 std::string examples = R"___(
4821 EXAMPLES:
4822 The debug adapter can be started in two modes.
4824 Running lldb-dap without any arguments will start communicating with the
4825 parent over stdio. Passing a port number causes lldb-dap to start listening
4826 for connections on that port.
4828 lldb-dap -p <port>
4830 Passing --wait-for-debugger will pause the process at startup and wait for a
4831 debugger to attach to the process.
4833 lldb-dap -g
4834 )___";
4835 llvm::outs() << examples;
4838 // If --launch-target is provided, this instance of lldb-dap becomes a
4839 // runInTerminal launcher. It will ultimately launch the program specified in
4840 // the --launch-target argument, which is the original program the user wanted
4841 // to debug. This is done in such a way that the actual debug adaptor can
4842 // place breakpoints at the beginning of the program.
4844 // The launcher will communicate with the debug adaptor using a fifo file in the
4845 // directory specified in the --comm-file argument.
4847 // Regarding the actual flow, this launcher will first notify the debug adaptor
4848 // of its pid. Then, the launcher will be in a pending state waiting to be
4849 // attached by the adaptor.
4851 // Once attached and resumed, the launcher will exec and become the program
4852 // specified by --launch-target, which is the original target the
4853 // user wanted to run.
4855 // In case of errors launching the target, a suitable error message will be
4856 // emitted to the debug adaptor.
4857 static void LaunchRunInTerminalTarget(llvm::opt::Arg &target_arg,
4858 llvm::StringRef comm_file,
4859 lldb::pid_t debugger_pid, char *argv[]) {
4860 #if defined(_WIN32)
4861 llvm::errs() << "runInTerminal is only supported on POSIX systems\n";
4862 exit(EXIT_FAILURE);
4863 #else
4865 // On Linux with the Yama security module enabled, a process can only attach
4866 // to its descendants by default. In the runInTerminal case the target
4867 // process is launched by the client so we need to allow tracing explicitly.
4868 #if defined(__linux__)
4869 if (debugger_pid != LLDB_INVALID_PROCESS_ID)
4870 (void)prctl(PR_SET_PTRACER, debugger_pid, 0, 0, 0);
4871 #endif
4873 RunInTerminalLauncherCommChannel comm_channel(comm_file);
4874 if (llvm::Error err = comm_channel.NotifyPid()) {
4875 llvm::errs() << llvm::toString(std::move(err)) << "\n";
4876 exit(EXIT_FAILURE);
4879 // We will wait to be attached with a timeout. We don't wait indefinitely
4880 // using a signal to prevent being paused forever.
4882 // This env var should be used only for tests.
4883 const char *timeout_env_var = getenv("LLDB_DAP_RIT_TIMEOUT_IN_MS");
4884 int timeout_in_ms =
4885 timeout_env_var != nullptr ? atoi(timeout_env_var) : 20000;
4886 if (llvm::Error err = comm_channel.WaitUntilDebugAdaptorAttaches(
4887 std::chrono::milliseconds(timeout_in_ms))) {
4888 llvm::errs() << llvm::toString(std::move(err)) << "\n";
4889 exit(EXIT_FAILURE);
4892 const char *target = target_arg.getValue();
4893 execvp(target, argv);
4895 std::string error = std::strerror(errno);
4896 comm_channel.NotifyError(error);
4897 llvm::errs() << error << "\n";
4898 exit(EXIT_FAILURE);
4899 #endif
4902 /// used only by TestVSCode_redirection_to_console.py
4903 static void redirection_test() {
4904 printf("stdout message\n");
4905 fprintf(stderr, "stderr message\n");
4906 fflush(stdout);
4907 fflush(stderr);
4910 /// Redirect stdout and stderr fo the IDE's console output.
4912 /// Errors in this operation will be printed to the log file and the IDE's
4913 /// console output as well.
4915 /// \return
4916 /// A fd pointing to the original stdout.
4917 static int SetupStdoutStderrRedirection(DAP &dap) {
4918 int stdoutfd = fileno(stdout);
4919 int new_stdout_fd = dup(stdoutfd);
4920 auto output_callback_stderr = [&dap](llvm::StringRef data) {
4921 dap.SendOutput(OutputType::Stderr, data);
4923 auto output_callback_stdout = [&dap](llvm::StringRef data) {
4924 dap.SendOutput(OutputType::Stdout, data);
4926 if (llvm::Error err = RedirectFd(stdoutfd, output_callback_stdout)) {
4927 std::string error_message = llvm::toString(std::move(err));
4928 if (dap.log)
4929 *dap.log << error_message << std::endl;
4930 output_callback_stderr(error_message);
4932 if (llvm::Error err = RedirectFd(fileno(stderr), output_callback_stderr)) {
4933 std::string error_message = llvm::toString(std::move(err));
4934 if (dap.log)
4935 *dap.log << error_message << std::endl;
4936 output_callback_stderr(error_message);
4939 return new_stdout_fd;
4942 int main(int argc, char *argv[]) {
4943 llvm::InitLLVM IL(argc, argv, /*InstallPipeSignalExitHandler=*/false);
4944 #if !defined(__APPLE__)
4945 llvm::setBugReportMsg("PLEASE submit a bug report to " LLDB_BUG_REPORT_URL
4946 " and include the crash backtrace.\n");
4947 #else
4948 llvm::setBugReportMsg("PLEASE submit a bug report to " LLDB_BUG_REPORT_URL
4949 " and include the crash report from "
4950 "~/Library/Logs/DiagnosticReports/.\n");
4951 #endif
4953 llvm::SmallString<256> program_path(argv[0]);
4954 llvm::sys::fs::make_absolute(program_path);
4956 LLDBDAPOptTable T;
4957 unsigned MAI, MAC;
4958 llvm::ArrayRef<const char *> ArgsArr = llvm::ArrayRef(argv + 1, argc);
4959 llvm::opt::InputArgList input_args = T.ParseArgs(ArgsArr, MAI, MAC);
4961 if (input_args.hasArg(OPT_help)) {
4962 printHelp(T, llvm::sys::path::filename(argv[0]));
4963 return EXIT_SUCCESS;
4966 ReplMode default_repl_mode = ReplMode::Auto;
4967 if (input_args.hasArg(OPT_repl_mode)) {
4968 llvm::opt::Arg *repl_mode = input_args.getLastArg(OPT_repl_mode);
4969 llvm::StringRef repl_mode_value = repl_mode->getValue();
4970 if (repl_mode_value == "auto") {
4971 default_repl_mode = ReplMode::Auto;
4972 } else if (repl_mode_value == "variable") {
4973 default_repl_mode = ReplMode::Variable;
4974 } else if (repl_mode_value == "command") {
4975 default_repl_mode = ReplMode::Command;
4976 } else {
4977 llvm::errs()
4978 << "'" << repl_mode_value
4979 << "' is not a valid option, use 'variable', 'command' or 'auto'.\n";
4980 return EXIT_FAILURE;
4984 if (llvm::opt::Arg *target_arg = input_args.getLastArg(OPT_launch_target)) {
4985 if (llvm::opt::Arg *comm_file = input_args.getLastArg(OPT_comm_file)) {
4986 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
4987 llvm::opt::Arg *debugger_pid = input_args.getLastArg(OPT_debugger_pid);
4988 if (debugger_pid) {
4989 llvm::StringRef debugger_pid_value = debugger_pid->getValue();
4990 if (debugger_pid_value.getAsInteger(10, pid)) {
4991 llvm::errs() << "'" << debugger_pid_value
4992 << "' is not a valid "
4993 "PID\n";
4994 return EXIT_FAILURE;
4997 int target_args_pos = argc;
4998 for (int i = 0; i < argc; i++)
4999 if (strcmp(argv[i], "--launch-target") == 0) {
5000 target_args_pos = i + 1;
5001 break;
5003 LaunchRunInTerminalTarget(*target_arg, comm_file->getValue(), pid,
5004 argv + target_args_pos);
5005 } else {
5006 llvm::errs() << "\"--launch-target\" requires \"--comm-file\" to be "
5007 "specified\n";
5008 return EXIT_FAILURE;
5012 int portno = -1;
5013 if (auto *arg = input_args.getLastArg(OPT_port)) {
5014 const auto *optarg = arg->getValue();
5015 char *remainder;
5016 portno = strtol(optarg, &remainder, 0);
5017 if (remainder == optarg || *remainder != '\0') {
5018 fprintf(stderr, "'%s' is not a valid port number.\n", optarg);
5019 return EXIT_FAILURE;
5023 #if !defined(_WIN32)
5024 if (input_args.hasArg(OPT_wait_for_debugger)) {
5025 printf("Paused waiting for debugger to attach (pid = %i)...\n", getpid());
5026 pause();
5028 #endif
5030 // Initialize LLDB first before we do anything.
5031 lldb::SBDebugger::Initialize();
5033 // Terminate the debugger before the C++ destructor chain kicks in.
5034 auto terminate_debugger =
5035 llvm::make_scope_exit([] { lldb::SBDebugger::Terminate(); });
5037 DAP dap = DAP(program_path.str(), default_repl_mode);
5039 RegisterRequestCallbacks(dap);
5041 // stdout/stderr redirection to the IDE's console
5042 int new_stdout_fd = SetupStdoutStderrRedirection(dap);
5044 if (portno != -1) {
5045 printf("Listening on port %i...\n", portno);
5046 SOCKET socket_fd = AcceptConnection(dap, portno);
5047 if (socket_fd >= 0) {
5048 dap.input.descriptor = StreamDescriptor::from_socket(socket_fd, true);
5049 dap.output.descriptor = StreamDescriptor::from_socket(socket_fd, false);
5050 } else {
5051 return EXIT_FAILURE;
5053 } else {
5054 dap.input.descriptor = StreamDescriptor::from_file(fileno(stdin), false);
5055 dap.output.descriptor = StreamDescriptor::from_file(new_stdout_fd, false);
5057 /// used only by TestVSCode_redirection_to_console.py
5058 if (getenv("LLDB_DAP_TEST_STDOUT_STDERR_REDIRECTION") != nullptr)
5059 redirection_test();
5062 for (const std::string &arg :
5063 input_args.getAllArgValues(OPT_pre_init_command)) {
5064 dap.pre_init_commands.push_back(arg);
5067 bool CleanExit = true;
5068 if (auto Err = dap.Loop()) {
5069 if (dap.log)
5070 *dap.log << "Transport Error: " << llvm::toString(std::move(Err)) << "\n";
5071 CleanExit = false;
5074 return CleanExit ? EXIT_SUCCESS : EXIT_FAILURE;