1 //===-- lldb-dap.cpp -----------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 #include "Watchpoint.h"
11 #include "lldb/API/SBMemoryRegionInfo.h"
21 #include <sys/types.h>
23 // We need to #define NOMINMAX in order to skip `min()` and `max()` macro
24 // definitions that conflict with other system headers.
25 // We also need to #undef GetObject (which is defined to GetObjectW) because
26 // the JSON code we use also has methods named `GetObject()` and we conflict
33 #include <netinet/in.h>
34 #include <sys/socket.h>
38 #if defined(__linux__)
39 #include <sys/prctl.h>
54 #include "lldb/API/SBStream.h"
55 #include "lldb/Host/Config.h"
56 #include "llvm/ADT/ArrayRef.h"
57 #include "llvm/ADT/DenseMap.h"
58 #include "llvm/ADT/ScopeExit.h"
59 #include "llvm/ADT/StringExtras.h"
60 #include "llvm/Option/Arg.h"
61 #include "llvm/Option/ArgList.h"
62 #include "llvm/Option/OptTable.h"
63 #include "llvm/Option/Option.h"
64 #include "llvm/Support/Errno.h"
65 #include "llvm/Support/FileSystem.h"
66 #include "llvm/Support/InitLLVM.h"
67 #include "llvm/Support/Path.h"
68 #include "llvm/Support/PrettyStackTrace.h"
69 #include "llvm/Support/raw_ostream.h"
71 #include "JSONUtils.h"
72 #include "LLDBUtils.h"
73 #include "OutputRedirector.h"
77 #define PATH_MAX MAX_PATH
79 typedef int socklen_t
;
82 using namespace lldb_dap
;
85 using namespace llvm::opt
;
88 OPT_INVALID
= 0, // This is not an option ID.
89 #define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
90 #include "Options.inc"
94 #define PREFIX(NAME, VALUE) \
95 static constexpr llvm::StringLiteral NAME##_init[] = VALUE; \
96 static constexpr llvm::ArrayRef<llvm::StringLiteral> NAME( \
97 NAME##_init, std::size(NAME##_init) - 1);
98 #include "Options.inc"
101 static constexpr llvm::opt::OptTable::Info InfoTable
[] = {
102 #define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
103 #include "Options.inc"
106 class LLDBDAPOptTable
: public llvm::opt::GenericOptTable
{
108 LLDBDAPOptTable() : llvm::opt::GenericOptTable(InfoTable
, true) {}
111 typedef void (*RequestCallback
)(const llvm::json::Object
&command
);
113 enum LaunchMethod
{ Launch
, Attach
, AttachForSuspendedLaunch
};
115 /// Prints a welcome message on the editor if the preprocessor variable
116 /// LLDB_DAP_WELCOME_MESSAGE is defined.
117 static void PrintWelcomeMessage() {
118 #ifdef LLDB_DAP_WELCOME_MESSAGE
119 g_dap
.SendOutput(OutputType::Console
, LLDB_DAP_WELCOME_MESSAGE
);
123 lldb::SBValueList
*GetTopLevelScope(int64_t variablesReference
) {
124 switch (variablesReference
) {
126 return &g_dap
.variables
.locals
;
128 return &g_dap
.variables
.globals
;
130 return &g_dap
.variables
.registers
;
136 SOCKET
AcceptConnection(int portno
) {
137 // Accept a socket connection from any host on "portno".
138 SOCKET newsockfd
= -1;
139 struct sockaddr_in serv_addr
, cli_addr
;
140 SOCKET sockfd
= socket(AF_INET
, SOCK_STREAM
, 0);
143 *g_dap
.log
<< "error: opening socket (" << strerror(errno
) << ")"
146 memset((char *)&serv_addr
, 0, sizeof(serv_addr
));
147 serv_addr
.sin_family
= AF_INET
;
148 // serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
149 serv_addr
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
150 serv_addr
.sin_port
= htons(portno
);
151 if (bind(sockfd
, (struct sockaddr
*)&serv_addr
, sizeof(serv_addr
)) < 0) {
153 *g_dap
.log
<< "error: binding socket (" << strerror(errno
) << ")"
157 socklen_t clilen
= sizeof(cli_addr
);
159 llvm::sys::RetryAfterSignal(static_cast<SOCKET
>(-1), accept
, sockfd
,
160 (struct sockaddr
*)&cli_addr
, &clilen
);
163 *g_dap
.log
<< "error: accept (" << strerror(errno
) << ")"
175 std::vector
<const char *> MakeArgv(const llvm::ArrayRef
<std::string
> &strs
) {
176 // Create and return an array of "const char *", one for each C string in
177 // "strs" and terminate the list with a NULL. This can be used for argument
178 // vectors (argv) or environment vectors (envp) like those passed to the
179 // "main" function in C programs.
180 std::vector
<const char *> argv
;
181 for (const auto &s
: strs
)
182 argv
.push_back(s
.c_str());
183 argv
.push_back(nullptr);
187 // Send a "exited" event to indicate the process has exited.
188 void SendProcessExitedEvent(lldb::SBProcess
&process
) {
189 llvm::json::Object
event(CreateEventObject("exited"));
190 llvm::json::Object body
;
191 body
.try_emplace("exitCode", (int64_t)process
.GetExitStatus());
192 event
.try_emplace("body", std::move(body
));
193 g_dap
.SendJSON(llvm::json::Value(std::move(event
)));
196 void SendThreadExitedEvent(lldb::tid_t tid
) {
197 llvm::json::Object
event(CreateEventObject("thread"));
198 llvm::json::Object body
;
199 body
.try_emplace("reason", "exited");
200 body
.try_emplace("threadId", (int64_t)tid
);
201 event
.try_emplace("body", std::move(body
));
202 g_dap
.SendJSON(llvm::json::Value(std::move(event
)));
205 // Send a "continued" event to indicate the process is in the running state.
206 void SendContinuedEvent() {
207 lldb::SBProcess process
= g_dap
.target
.GetProcess();
208 if (!process
.IsValid()) {
212 // If the focus thread is not set then we haven't reported any thread status
213 // to the client, so nothing to report.
214 if (!g_dap
.configuration_done_sent
||
215 g_dap
.focus_tid
== LLDB_INVALID_THREAD_ID
) {
219 llvm::json::Object
event(CreateEventObject("continued"));
220 llvm::json::Object body
;
221 body
.try_emplace("threadId", (int64_t)g_dap
.focus_tid
);
222 body
.try_emplace("allThreadsContinued", true);
223 event
.try_emplace("body", std::move(body
));
224 g_dap
.SendJSON(llvm::json::Value(std::move(event
)));
227 // Send a "terminated" event to indicate the process is done being
229 void SendTerminatedEvent() {
230 // Prevent races if the process exits while we're being asked to disconnect.
231 llvm::call_once(g_dap
.terminated_event_flag
, [&] {
232 g_dap
.RunTerminateCommands();
233 // Send a "terminated" event
234 llvm::json::Object
event(CreateTerminatedEventObject());
235 g_dap
.SendJSON(llvm::json::Value(std::move(event
)));
239 // Send a thread stopped event for all threads as long as the process
241 void SendThreadStoppedEvent() {
242 lldb::SBProcess process
= g_dap
.target
.GetProcess();
243 if (process
.IsValid()) {
244 auto state
= process
.GetState();
245 if (state
== lldb::eStateStopped
) {
246 llvm::DenseSet
<lldb::tid_t
> old_thread_ids
;
247 old_thread_ids
.swap(g_dap
.thread_ids
);
248 uint32_t stop_id
= process
.GetStopID();
249 const uint32_t num_threads
= process
.GetNumThreads();
251 // First make a pass through the threads to see if the focused thread
252 // has a stop reason. In case the focus thread doesn't have a stop
253 // reason, remember the first thread that has a stop reason so we can
254 // set it as the focus thread if below if needed.
255 lldb::tid_t first_tid_with_reason
= LLDB_INVALID_THREAD_ID
;
256 uint32_t num_threads_with_reason
= 0;
257 bool focus_thread_exists
= false;
258 for (uint32_t thread_idx
= 0; thread_idx
< num_threads
; ++thread_idx
) {
259 lldb::SBThread thread
= process
.GetThreadAtIndex(thread_idx
);
260 const lldb::tid_t tid
= thread
.GetThreadID();
261 const bool has_reason
= ThreadHasStopReason(thread
);
262 // If the focus thread doesn't have a stop reason, clear the thread ID
263 if (tid
== g_dap
.focus_tid
) {
264 focus_thread_exists
= true;
266 g_dap
.focus_tid
= LLDB_INVALID_THREAD_ID
;
269 ++num_threads_with_reason
;
270 if (first_tid_with_reason
== LLDB_INVALID_THREAD_ID
)
271 first_tid_with_reason
= tid
;
275 // We will have cleared g_dap.focus_tid if the focus thread doesn't have
276 // a stop reason, so if it was cleared, or wasn't set, or doesn't exist,
277 // then set the focus thread to the first thread with a stop reason.
278 if (!focus_thread_exists
|| g_dap
.focus_tid
== LLDB_INVALID_THREAD_ID
)
279 g_dap
.focus_tid
= first_tid_with_reason
;
281 // If no threads stopped with a reason, then report the first one so
282 // we at least let the UI know we stopped.
283 if (num_threads_with_reason
== 0) {
284 lldb::SBThread thread
= process
.GetThreadAtIndex(0);
285 g_dap
.focus_tid
= thread
.GetThreadID();
286 g_dap
.SendJSON(CreateThreadStopped(thread
, stop_id
));
288 for (uint32_t thread_idx
= 0; thread_idx
< num_threads
; ++thread_idx
) {
289 lldb::SBThread thread
= process
.GetThreadAtIndex(thread_idx
);
290 g_dap
.thread_ids
.insert(thread
.GetThreadID());
291 if (ThreadHasStopReason(thread
)) {
292 g_dap
.SendJSON(CreateThreadStopped(thread
, stop_id
));
297 for (auto tid
: old_thread_ids
) {
298 auto end
= g_dap
.thread_ids
.end();
299 auto pos
= g_dap
.thread_ids
.find(tid
);
301 SendThreadExitedEvent(tid
);
305 *g_dap
.log
<< "error: SendThreadStoppedEvent() when process"
307 << lldb::SBDebugger::StateAsCString(state
) << ')'
312 *g_dap
.log
<< "error: SendThreadStoppedEvent() invalid process"
315 g_dap
.RunStopCommands();
320 // { "$ref": "#/definitions/Event" },
323 // "description": "Event message for 'process' event type. The event
324 // indicates that the debugger has begun debugging a
325 // new process. Either one that it has launched, or one
326 // that it has attached to.",
330 // "enum": [ "process" ]
337 // "description": "The logical name of the process. This is
338 // usually the full path to process's executable
339 // file. Example: /home/myproj/program.js."
341 // "systemProcessId": {
342 // "type": "integer",
343 // "description": "The system process id of the debugged process.
344 // This property will be missing for non-system
347 // "isLocalProcess": {
348 // "type": "boolean",
349 // "description": "If true, the process is running on the same
350 // computer as the debug adapter."
354 // "enum": [ "launch", "attach", "attachForSuspendedLaunch" ],
355 // "description": "Describes how the debug engine started
356 // debugging this process.",
357 // "enumDescriptions": [
358 // "Process was launched under the debugger.",
359 // "Debugger attached to an existing process.",
360 // "A project launcher component has launched a new process in
361 // a suspended state and then asked the debugger to attach."
365 // "required": [ "name" ]
368 // "required": [ "event", "body" ]
372 void SendProcessEvent(LaunchMethod launch_method
) {
373 lldb::SBFileSpec exe_fspec
= g_dap
.target
.GetExecutable();
374 char exe_path
[PATH_MAX
];
375 exe_fspec
.GetPath(exe_path
, sizeof(exe_path
));
376 llvm::json::Object
event(CreateEventObject("process"));
377 llvm::json::Object body
;
378 EmplaceSafeString(body
, "name", std::string(exe_path
));
379 const auto pid
= g_dap
.target
.GetProcess().GetProcessID();
380 body
.try_emplace("systemProcessId", (int64_t)pid
);
381 body
.try_emplace("isLocalProcess", true);
382 const char *startMethod
= nullptr;
383 switch (launch_method
) {
385 startMethod
= "launch";
388 startMethod
= "attach";
390 case AttachForSuspendedLaunch
:
391 startMethod
= "attachForSuspendedLaunch";
394 body
.try_emplace("startMethod", startMethod
);
395 event
.try_emplace("body", std::move(body
));
396 g_dap
.SendJSON(llvm::json::Value(std::move(event
)));
399 // Grab any STDOUT and STDERR from the process and send it up to VS Code
400 // via an "output" event to the "stdout" and "stderr" categories.
401 void SendStdOutStdErr(lldb::SBProcess
&process
) {
404 while ((count
= process
.GetSTDOUT(buffer
, sizeof(buffer
))) > 0)
405 g_dap
.SendOutput(OutputType::Stdout
, llvm::StringRef(buffer
, count
));
406 while ((count
= process
.GetSTDERR(buffer
, sizeof(buffer
))) > 0)
407 g_dap
.SendOutput(OutputType::Stderr
, llvm::StringRef(buffer
, count
));
410 void ProgressEventThreadFunction() {
411 lldb::SBListener
listener("lldb-dap.progress.listener");
412 g_dap
.debugger
.GetBroadcaster().AddListener(
413 listener
, lldb::SBDebugger::eBroadcastBitProgress
);
414 g_dap
.broadcaster
.AddListener(listener
, eBroadcastBitStopProgressThread
);
418 if (listener
.WaitForEvent(1, event
)) {
419 const auto event_mask
= event
.GetType();
420 if (event
.BroadcasterMatchesRef(g_dap
.broadcaster
)) {
421 if (event_mask
& eBroadcastBitStopProgressThread
) {
425 uint64_t progress_id
= 0;
426 uint64_t completed
= 0;
428 bool is_debugger_specific
= false;
429 const char *message
= lldb::SBDebugger::GetProgressFromEvent(
430 event
, progress_id
, completed
, total
, is_debugger_specific
);
432 g_dap
.SendProgressEvent(progress_id
, message
, completed
, total
);
438 // All events from the debugger, target, process, thread and frames are
439 // received in this function that runs in its own thread. We are using a
440 // "FILE *" to output packets back to VS Code and they have mutexes in them
441 // them prevent multiple threads from writing simultaneously so no locking
443 void EventThreadFunction() {
445 lldb::SBListener listener
= g_dap
.debugger
.GetListener();
448 if (listener
.WaitForEvent(1, event
)) {
449 const auto event_mask
= event
.GetType();
450 if (lldb::SBProcess::EventIsProcessEvent(event
)) {
451 lldb::SBProcess process
= lldb::SBProcess::GetProcessFromEvent(event
);
452 if (event_mask
& lldb::SBProcess::eBroadcastBitStateChanged
) {
453 auto state
= lldb::SBProcess::GetStateFromEvent(event
);
455 case lldb::eStateInvalid
:
458 case lldb::eStateUnloaded
:
460 case lldb::eStateConnected
:
462 case lldb::eStateAttaching
:
464 case lldb::eStateLaunching
:
466 case lldb::eStateStepping
:
468 case lldb::eStateCrashed
:
470 case lldb::eStateDetached
:
472 case lldb::eStateSuspended
:
474 case lldb::eStateStopped
:
475 // We launch and attach in synchronous mode then the first stop
476 // event will not be delivered. If we use "launchCommands" during a
477 // launch or "attachCommands" during an attach we might some process
478 // stop events which we do not want to send an event for. We will
479 // manually send a stopped event in request_configurationDone(...)
480 // so don't send any before then.
481 if (g_dap
.configuration_done_sent
) {
482 // Only report a stopped event if the process was not
483 // automatically restarted.
484 if (!lldb::SBProcess::GetRestartedFromEvent(event
)) {
485 SendStdOutStdErr(process
);
486 SendThreadStoppedEvent();
490 case lldb::eStateRunning
:
491 g_dap
.WillContinue();
492 SendContinuedEvent();
494 case lldb::eStateExited
:
495 lldb::SBStream stream
;
496 process
.GetStatus(stream
);
497 g_dap
.SendOutput(OutputType::Console
, stream
.GetData());
499 // When restarting, we can get an "exited" event for the process we
500 // just killed with the old PID, or even with no PID. In that case
501 // we don't have to terminate the session.
502 if (process
.GetProcessID() == LLDB_INVALID_PROCESS_ID
||
503 process
.GetProcessID() == g_dap
.restarting_process_id
) {
504 g_dap
.restarting_process_id
= LLDB_INVALID_PROCESS_ID
;
506 // Run any exit LLDB commands the user specified in the
508 g_dap
.RunExitCommands();
509 SendProcessExitedEvent(process
);
510 SendTerminatedEvent();
515 } else if ((event_mask
& lldb::SBProcess::eBroadcastBitSTDOUT
) ||
516 (event_mask
& lldb::SBProcess::eBroadcastBitSTDERR
)) {
517 SendStdOutStdErr(process
);
519 } else if (lldb::SBBreakpoint::EventIsBreakpointEvent(event
)) {
520 if (event_mask
& lldb::SBTarget::eBroadcastBitBreakpointChanged
) {
522 lldb::SBBreakpoint::GetBreakpointEventTypeFromEvent(event
);
524 Breakpoint(lldb::SBBreakpoint::GetBreakpointFromEvent(event
));
525 // If the breakpoint was originated from the IDE, it will have the
526 // BreakpointBase::GetBreakpointLabel() label attached. Regardless
527 // of wether the locations were added or removed, the breakpoint
528 // ins't going away, so we the reason is always "changed".
529 if ((event_type
& lldb::eBreakpointEventTypeLocationsAdded
||
530 event_type
& lldb::eBreakpointEventTypeLocationsRemoved
) &&
531 bp
.MatchesName(BreakpointBase::GetBreakpointLabel())) {
532 auto bp_event
= CreateEventObject("breakpoint");
533 llvm::json::Object body
;
534 // As VSCode already knows the path of this breakpoint, we don't
535 // need to send it back as part of a "changed" event. This
536 // prevent us from sending to VSCode paths that should be source
537 // mapped. Note that CreateBreakpoint doesn't apply source mapping.
538 // Besides, the current implementation of VSCode ignores the
539 // "source" element of breakpoint events.
540 llvm::json::Value source_bp
= CreateBreakpoint(&bp
);
541 source_bp
.getAsObject()->erase("source");
543 body
.try_emplace("breakpoint", source_bp
);
544 body
.try_emplace("reason", "changed");
545 bp_event
.try_emplace("body", std::move(body
));
546 g_dap
.SendJSON(llvm::json::Value(std::move(bp_event
)));
549 } else if (event
.BroadcasterMatchesRef(g_dap
.broadcaster
)) {
550 if (event_mask
& eBroadcastBitStopEventThread
) {
558 lldb::SBValue
FindVariable(uint64_t variablesReference
, llvm::StringRef name
) {
559 lldb::SBValue variable
;
560 if (lldb::SBValueList
*top_scope
= GetTopLevelScope(variablesReference
)) {
561 bool is_duplicated_variable_name
= name
.contains(" @");
562 // variablesReference is one of our scopes, not an actual variable it is
563 // asking for a variable in locals or globals or registers
564 int64_t end_idx
= top_scope
->GetSize();
565 // Searching backward so that we choose the variable in closest scope
566 // among variables of the same name.
567 for (int64_t i
= end_idx
- 1; i
>= 0; --i
) {
568 lldb::SBValue curr_variable
= top_scope
->GetValueAtIndex(i
);
569 std::string variable_name
= CreateUniqueVariableNameForDisplay(
570 curr_variable
, is_duplicated_variable_name
);
571 if (variable_name
== name
) {
572 variable
= curr_variable
;
577 // This is not under the globals or locals scope, so there are no duplicated
580 // We have a named item within an actual variable so we need to find it
581 // withing the container variable by name.
582 lldb::SBValue container
= g_dap
.variables
.GetVariable(variablesReference
);
583 variable
= container
.GetChildMemberWithName(name
.data());
584 if (!variable
.IsValid()) {
585 if (name
.starts_with("[")) {
586 llvm::StringRef
index_str(name
.drop_front(1));
588 if (!index_str
.consumeInteger(0, index
)) {
589 if (index_str
== "]")
590 variable
= container
.GetChildAtIndex(index
);
598 // Both attach and launch take a either a sourcePath or sourceMap
599 // argument (or neither), from which we need to set the target.source-map.
600 void SetSourceMapFromArguments(const llvm::json::Object
&arguments
) {
601 const char *sourceMapHelp
=
602 "source must be be an array of two-element arrays, "
603 "each containing a source and replacement path string.\n";
605 std::string sourceMapCommand
;
606 llvm::raw_string_ostream
strm(sourceMapCommand
);
607 strm
<< "settings set target.source-map ";
608 auto sourcePath
= GetString(arguments
, "sourcePath");
610 // sourceMap is the new, more general form of sourcePath and overrides it.
611 auto sourceMap
= arguments
.getArray("sourceMap");
613 for (const auto &value
: *sourceMap
) {
614 auto mapping
= value
.getAsArray();
615 if (mapping
== nullptr || mapping
->size() != 2 ||
616 (*mapping
)[0].kind() != llvm::json::Value::String
||
617 (*mapping
)[1].kind() != llvm::json::Value::String
) {
618 g_dap
.SendOutput(OutputType::Console
, llvm::StringRef(sourceMapHelp
));
621 auto mapFrom
= GetAsString((*mapping
)[0]);
622 auto mapTo
= GetAsString((*mapping
)[1]);
623 strm
<< "\"" << mapFrom
<< "\" \"" << mapTo
<< "\" ";
626 if (ObjectContainsKey(arguments
, "sourceMap")) {
627 g_dap
.SendOutput(OutputType::Console
, llvm::StringRef(sourceMapHelp
));
630 if (sourcePath
.empty())
632 // Do any source remapping needed before we create our targets
633 strm
<< "\".\" \"" << sourcePath
<< "\"";
636 if (!sourceMapCommand
.empty()) {
637 g_dap
.RunLLDBCommands("Setting source map:", {sourceMapCommand
});
641 // "AttachRequest": {
642 // "allOf": [ { "$ref": "#/definitions/Request" }, {
644 // "description": "Attach request; value of command field is 'attach'.",
648 // "enum": [ "attach" ]
651 // "$ref": "#/definitions/AttachRequestArguments"
654 // "required": [ "command", "arguments" ]
657 // "AttachRequestArguments": {
659 // "description": "Arguments for 'attach' request.\nThe attach request has no
660 // standardized attributes."
662 // "AttachResponse": {
663 // "allOf": [ { "$ref": "#/definitions/Response" }, {
665 // "description": "Response to 'attach' request. This is just an
666 // acknowledgement, so no body field is required."
669 void request_attach(const llvm::json::Object
&request
) {
670 g_dap
.is_attach
= true;
671 g_dap
.last_launch_or_attach_request
= request
;
672 llvm::json::Object response
;
674 FillResponse(request
, response
);
675 lldb::SBAttachInfo attach_info
;
676 const int invalid_port
= 0;
677 auto arguments
= request
.getObject("arguments");
678 const lldb::pid_t pid
=
679 GetUnsigned(arguments
, "pid", LLDB_INVALID_PROCESS_ID
);
680 const auto gdb_remote_port
=
681 GetUnsigned(arguments
, "gdb-remote-port", invalid_port
);
682 const auto gdb_remote_hostname
=
683 GetString(arguments
, "gdb-remote-hostname", "localhost");
684 if (pid
!= LLDB_INVALID_PROCESS_ID
)
685 attach_info
.SetProcessID(pid
);
686 const auto wait_for
= GetBoolean(arguments
, "waitFor", false);
687 attach_info
.SetWaitForLaunch(wait_for
, false /*async*/);
688 g_dap
.init_commands
= GetStrings(arguments
, "initCommands");
689 g_dap
.pre_run_commands
= GetStrings(arguments
, "preRunCommands");
690 g_dap
.stop_commands
= GetStrings(arguments
, "stopCommands");
691 g_dap
.exit_commands
= GetStrings(arguments
, "exitCommands");
692 g_dap
.terminate_commands
= GetStrings(arguments
, "terminateCommands");
693 auto attachCommands
= GetStrings(arguments
, "attachCommands");
694 llvm::StringRef core_file
= GetString(arguments
, "coreFile");
695 const uint64_t timeout_seconds
= GetUnsigned(arguments
, "timeout", 30);
696 g_dap
.stop_at_entry
=
697 core_file
.empty() ? GetBoolean(arguments
, "stopOnEntry", false) : true;
698 g_dap
.post_run_commands
= GetStrings(arguments
, "postRunCommands");
699 const llvm::StringRef debuggerRoot
= GetString(arguments
, "debuggerRoot");
700 g_dap
.enable_auto_variable_summaries
=
701 GetBoolean(arguments
, "enableAutoVariableSummaries", false);
702 g_dap
.enable_synthetic_child_debugging
=
703 GetBoolean(arguments
, "enableSyntheticChildDebugging", false);
704 g_dap
.command_escape_prefix
=
705 GetString(arguments
, "commandEscapePrefix", "`");
706 g_dap
.SetFrameFormat(GetString(arguments
, "customFrameFormat"));
707 g_dap
.SetThreadFormat(GetString(arguments
, "customThreadFormat"));
709 PrintWelcomeMessage();
711 // This is a hack for loading DWARF in .o files on Mac where the .o files
712 // in the debug map of the main executable have relative paths which require
713 // the lldb-dap binary to have its working directory set to that relative
714 // root for the .o files in order to be able to load debug info.
715 if (!debuggerRoot
.empty())
716 llvm::sys::fs::set_current_path(debuggerRoot
);
718 // Run any initialize LLDB commands the user specified in the launch.json
719 if (llvm::Error err
= g_dap
.RunInitCommands()) {
720 response
["success"] = false;
721 EmplaceSafeString(response
, "message", llvm::toString(std::move(err
)));
722 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
726 SetSourceMapFromArguments(*arguments
);
728 lldb::SBError status
;
729 g_dap
.SetTarget(g_dap
.CreateTargetFromArguments(*arguments
, status
));
731 response
["success"] = llvm::json::Value(false);
732 EmplaceSafeString(response
, "message", status
.GetCString());
733 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
737 // Run any pre run LLDB commands the user specified in the launch.json
738 if (llvm::Error err
= g_dap
.RunPreRunCommands()) {
739 response
["success"] = false;
740 EmplaceSafeString(response
, "message", llvm::toString(std::move(err
)));
741 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
745 if ((pid
== LLDB_INVALID_PROCESS_ID
|| gdb_remote_port
== invalid_port
) &&
747 char attach_msg
[256];
748 auto attach_msg_len
= snprintf(attach_msg
, sizeof(attach_msg
),
749 "Waiting to attach to \"%s\"...",
750 g_dap
.target
.GetExecutable().GetFilename());
751 g_dap
.SendOutput(OutputType::Console
,
752 llvm::StringRef(attach_msg
, attach_msg_len
));
754 if (attachCommands
.empty()) {
755 // No "attachCommands", just attach normally.
756 // Disable async events so the attach will be successful when we return from
757 // the launch call and the launch will happen synchronously
758 g_dap
.debugger
.SetAsync(false);
759 if (core_file
.empty()) {
760 if ((pid
!= LLDB_INVALID_PROCESS_ID
) &&
761 (gdb_remote_port
!= invalid_port
)) {
762 // If both pid and port numbers are specified.
763 error
.SetErrorString("The user can't specify both pid and port");
764 } else if (gdb_remote_port
!= invalid_port
) {
765 // If port is specified and pid is not.
766 lldb::SBListener listener
= g_dap
.debugger
.GetListener();
768 // If the user hasn't provided the hostname property, default localhost
770 std::string connect_url
=
771 llvm::formatv("connect://{0}:", gdb_remote_hostname
);
772 connect_url
+= std::to_string(gdb_remote_port
);
773 g_dap
.target
.ConnectRemote(listener
, connect_url
.c_str(), "gdb-remote",
776 // Attach by process name or id.
777 g_dap
.target
.Attach(attach_info
, error
);
780 g_dap
.target
.LoadCore(core_file
.data(), error
);
781 // Reenable async events
782 g_dap
.debugger
.SetAsync(true);
784 // We have "attachCommands" that are a set of commands that are expected
785 // to execute the commands after which a process should be created. If there
786 // is no valid process after running these commands, we have failed.
787 if (llvm::Error err
= g_dap
.RunAttachCommands(attachCommands
)) {
788 response
["success"] = false;
789 EmplaceSafeString(response
, "message", llvm::toString(std::move(err
)));
790 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
793 // The custom commands might have created a new target so we should use the
794 // selected target after these commands are run.
795 g_dap
.target
= g_dap
.debugger
.GetSelectedTarget();
797 // Make sure the process is attached and stopped before proceeding as the
798 // the launch commands are not run using the synchronous mode.
799 error
= g_dap
.WaitForProcessToStop(timeout_seconds
);
802 if (error
.Success() && core_file
.empty()) {
803 auto attached_pid
= g_dap
.target
.GetProcess().GetProcessID();
804 if (attached_pid
== LLDB_INVALID_PROCESS_ID
) {
805 if (attachCommands
.empty())
806 error
.SetErrorString("failed to attach to a process");
808 error
.SetErrorString("attachCommands failed to attach to a process");
813 response
["success"] = llvm::json::Value(false);
814 EmplaceSafeString(response
, "message", std::string(error
.GetCString()));
816 g_dap
.RunPostRunCommands();
819 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
820 if (error
.Success()) {
821 SendProcessEvent(Attach
);
822 g_dap
.SendJSON(CreateEventObject("initialized"));
826 // "ContinueRequest": {
827 // "allOf": [ { "$ref": "#/definitions/Request" }, {
829 // "description": "Continue request; value of command field is 'continue'.
830 // The request starts the debuggee to run again.",
834 // "enum": [ "continue" ]
837 // "$ref": "#/definitions/ContinueArguments"
840 // "required": [ "command", "arguments" ]
843 // "ContinueArguments": {
845 // "description": "Arguments for 'continue' request.",
848 // "type": "integer",
849 // "description": "Continue execution for the specified thread (if
850 // possible). If the backend cannot continue on a single
851 // thread but will continue on all threads, it should
852 // set the allThreadsContinued attribute in the response
856 // "required": [ "threadId" ]
858 // "ContinueResponse": {
859 // "allOf": [ { "$ref": "#/definitions/Response" }, {
861 // "description": "Response to 'continue' request.",
866 // "allThreadsContinued": {
867 // "type": "boolean",
868 // "description": "If true, the continue request has ignored the
869 // specified thread and continued all threads
870 // instead. If this attribute is missing a value
871 // of 'true' is assumed for backward
877 // "required": [ "body" ]
880 void request_continue(const llvm::json::Object
&request
) {
881 llvm::json::Object response
;
882 FillResponse(request
, response
);
883 lldb::SBProcess process
= g_dap
.target
.GetProcess();
884 lldb::SBError error
= process
.Continue();
885 llvm::json::Object body
;
886 body
.try_emplace("allThreadsContinued", true);
887 response
.try_emplace("body", std::move(body
));
888 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
891 // "ConfigurationDoneRequest": {
892 // "allOf": [ { "$ref": "#/definitions/Request" }, {
894 // "description": "ConfigurationDone request; value of command field
895 // is 'configurationDone'.\nThe client of the debug protocol must
896 // send this request at the end of the sequence of configuration
897 // requests (which was started by the InitializedEvent).",
901 // "enum": [ "configurationDone" ]
904 // "$ref": "#/definitions/ConfigurationDoneArguments"
907 // "required": [ "command" ]
910 // "ConfigurationDoneArguments": {
912 // "description": "Arguments for 'configurationDone' request.\nThe
913 // configurationDone request has no standardized attributes."
915 // "ConfigurationDoneResponse": {
916 // "allOf": [ { "$ref": "#/definitions/Response" }, {
918 // "description": "Response to 'configurationDone' request. This is
919 // just an acknowledgement, so no body field is required."
922 void request_configurationDone(const llvm::json::Object
&request
) {
923 llvm::json::Object response
;
924 FillResponse(request
, response
);
925 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
926 g_dap
.configuration_done_sent
= true;
927 if (g_dap
.stop_at_entry
)
928 SendThreadStoppedEvent();
930 g_dap
.target
.GetProcess().Continue();
933 // "DisconnectRequest": {
934 // "allOf": [ { "$ref": "#/definitions/Request" }, {
936 // "description": "Disconnect request; value of command field is
941 // "enum": [ "disconnect" ]
944 // "$ref": "#/definitions/DisconnectArguments"
947 // "required": [ "command" ]
950 // "DisconnectArguments": {
952 // "description": "Arguments for 'disconnect' request.",
954 // "terminateDebuggee": {
955 // "type": "boolean",
956 // "description": "Indicates whether the debuggee should be terminated
957 // when the debugger is disconnected. If unspecified,
958 // the debug adapter is free to do whatever it thinks
959 // is best. A client can only rely on this attribute
960 // being properly honored if a debug adapter returns
961 // true for the 'supportTerminateDebuggee' capability."
964 // "type": "boolean",
965 // "description": "Indicates whether the debuggee should be restart
970 // "DisconnectResponse": {
971 // "allOf": [ { "$ref": "#/definitions/Response" }, {
973 // "description": "Response to 'disconnect' request. This is just an
974 // acknowledgement, so no body field is required."
977 void request_disconnect(const llvm::json::Object
&request
) {
978 llvm::json::Object response
;
979 FillResponse(request
, response
);
980 auto arguments
= request
.getObject("arguments");
982 bool defaultTerminateDebuggee
= g_dap
.is_attach
? false : true;
983 bool terminateDebuggee
=
984 GetBoolean(arguments
, "terminateDebuggee", defaultTerminateDebuggee
);
985 lldb::SBProcess process
= g_dap
.target
.GetProcess();
986 auto state
= process
.GetState();
988 case lldb::eStateInvalid
:
989 case lldb::eStateUnloaded
:
990 case lldb::eStateDetached
:
991 case lldb::eStateExited
:
993 case lldb::eStateConnected
:
994 case lldb::eStateAttaching
:
995 case lldb::eStateLaunching
:
996 case lldb::eStateStepping
:
997 case lldb::eStateCrashed
:
998 case lldb::eStateSuspended
:
999 case lldb::eStateStopped
:
1000 case lldb::eStateRunning
:
1001 g_dap
.debugger
.SetAsync(false);
1002 lldb::SBError error
= terminateDebuggee
? process
.Kill() : process
.Detach();
1003 if (!error
.Success())
1004 EmplaceSafeString(response
, "error", error
.GetCString());
1005 g_dap
.debugger
.SetAsync(true);
1008 SendTerminatedEvent();
1009 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
1010 if (g_dap
.event_thread
.joinable()) {
1011 g_dap
.broadcaster
.BroadcastEventByType(eBroadcastBitStopEventThread
);
1012 g_dap
.event_thread
.join();
1014 if (g_dap
.progress_event_thread
.joinable()) {
1015 g_dap
.broadcaster
.BroadcastEventByType(eBroadcastBitStopProgressThread
);
1016 g_dap
.progress_event_thread
.join();
1018 g_dap
.disconnecting
= true;
1021 void request_exceptionInfo(const llvm::json::Object
&request
) {
1022 llvm::json::Object response
;
1023 FillResponse(request
, response
);
1024 auto arguments
= request
.getObject("arguments");
1025 llvm::json::Object body
;
1026 lldb::SBThread thread
= g_dap
.GetLLDBThread(*arguments
);
1027 if (thread
.IsValid()) {
1028 auto stopReason
= thread
.GetStopReason();
1029 if (stopReason
== lldb::eStopReasonSignal
)
1030 body
.try_emplace("exceptionId", "signal");
1031 else if (stopReason
== lldb::eStopReasonBreakpoint
) {
1032 ExceptionBreakpoint
*exc_bp
= g_dap
.GetExceptionBPFromStopReason(thread
);
1034 EmplaceSafeString(body
, "exceptionId", exc_bp
->filter
);
1035 EmplaceSafeString(body
, "description", exc_bp
->label
);
1037 body
.try_emplace("exceptionId", "exception");
1040 body
.try_emplace("exceptionId", "exception");
1042 if (!ObjectContainsKey(body
, "description")) {
1043 char description
[1024];
1044 if (thread
.GetStopDescription(description
, sizeof(description
))) {
1045 EmplaceSafeString(body
, "description", std::string(description
));
1048 body
.try_emplace("breakMode", "always");
1049 // auto excInfoCount = thread.GetStopReasonDataCount();
1050 // for (auto i=0; i<excInfoCount; ++i) {
1051 // uint64_t exc_data = thread.GetStopReasonDataAtIndex(i);
1054 response
["success"] = llvm::json::Value(false);
1056 response
.try_emplace("body", std::move(body
));
1057 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
1060 // "CompletionsRequest": {
1061 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1062 // "type": "object",
1063 // "description": "Returns a list of possible completions for a given caret
1064 // position and text.\nThe CompletionsRequest may only be called if the
1065 // 'supportsCompletionsRequest' capability exists and is true.",
1068 // "type": "string",
1069 // "enum": [ "completions" ]
1072 // "$ref": "#/definitions/CompletionsArguments"
1075 // "required": [ "command", "arguments" ]
1078 // "CompletionsArguments": {
1079 // "type": "object",
1080 // "description": "Arguments for 'completions' request.",
1083 // "type": "integer",
1084 // "description": "Returns completions in the scope of this stack frame.
1085 // If not specified, the completions are returned for the global scope."
1088 // "type": "string",
1089 // "description": "One or more source lines. Typically this is the text a
1090 // user has typed into the debug console before he asked for completion."
1093 // "type": "integer",
1094 // "description": "The character position for which to determine the
1095 // completion proposals."
1098 // "type": "integer",
1099 // "description": "An optional line for which to determine the completion
1100 // proposals. If missing the first line of the text is assumed."
1103 // "required": [ "text", "column" ]
1105 // "CompletionsResponse": {
1106 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1107 // "type": "object",
1108 // "description": "Response to 'completions' request.",
1111 // "type": "object",
1116 // "$ref": "#/definitions/CompletionItem"
1118 // "description": "The possible completions for ."
1121 // "required": [ "targets" ]
1124 // "required": [ "body" ]
1127 // "CompletionItem": {
1128 // "type": "object",
1129 // "description": "CompletionItems are the suggestions returned from the
1130 // CompletionsRequest.", "properties": {
1132 // "type": "string",
1133 // "description": "The label of this completion item. By default this is
1134 // also the text that is inserted when selecting this completion."
1137 // "type": "string",
1138 // "description": "If text is not falsy then it is inserted instead of the
1142 // "type": "string",
1143 // "description": "A string that should be used when comparing this item
1144 // with other items. When `falsy` the label is used."
1147 // "$ref": "#/definitions/CompletionItemType",
1148 // "description": "The item's type. Typically the client uses this
1149 // information to render the item in the UI with an icon."
1152 // "type": "integer",
1153 // "description": "This value determines the location (in the
1154 // CompletionsRequest's 'text' attribute) where the completion text is
1155 // added.\nIf missing the text is added at the location specified by the
1156 // CompletionsRequest's 'column' attribute."
1159 // "type": "integer",
1160 // "description": "This value determines how many characters are
1161 // overwritten by the completion text.\nIf missing the value 0 is assumed
1162 // which results in the completion text being inserted."
1165 // "required": [ "label" ]
1167 // "CompletionItemType": {
1168 // "type": "string",
1169 // "description": "Some predefined types for the CompletionItem. Please note
1170 // that not all clients have specific icons for all of them.", "enum": [
1171 // "method", "function", "constructor", "field", "variable", "class",
1172 // "interface", "module", "property", "unit", "value", "enum", "keyword",
1173 // "snippet", "text", "color", "file", "reference", "customcolor" ]
1175 void request_completions(const llvm::json::Object
&request
) {
1176 llvm::json::Object response
;
1177 FillResponse(request
, response
);
1178 llvm::json::Object body
;
1179 auto arguments
= request
.getObject("arguments");
1181 // If we have a frame, try to set the context for variable completions.
1182 lldb::SBFrame frame
= g_dap
.GetLLDBFrame(*arguments
);
1183 if (frame
.IsValid()) {
1184 frame
.GetThread().GetProcess().SetSelectedThread(frame
.GetThread());
1185 frame
.GetThread().SetSelectedFrame(frame
.GetFrameID());
1188 std::string text
= GetString(arguments
, "text").str();
1189 auto original_column
= GetSigned(arguments
, "column", text
.size());
1190 auto original_line
= GetSigned(arguments
, "line", 1);
1191 auto offset
= original_column
- 1;
1192 if (original_line
> 1) {
1193 llvm::SmallVector
<::llvm::StringRef
, 2> lines
;
1194 llvm::StringRef(text
).split(lines
, '\n');
1195 for (int i
= 0; i
< original_line
- 1; i
++) {
1196 offset
+= lines
[i
].size();
1199 llvm::json::Array targets
;
1201 if (!text
.empty() &&
1202 llvm::StringRef(text
).starts_with(g_dap
.command_escape_prefix
)) {
1203 text
= text
.substr(g_dap
.command_escape_prefix
.size());
1206 // While the user is typing then we likely have an incomplete input and cannot
1207 // reliably determine the precise intent (command vs variable), try completing
1208 // the text as both a command and variable expression, if applicable.
1209 const std::string expr_prefix
= "expression -- ";
1210 std::array
<std::tuple
<ReplMode
, std::string
, uint64_t>, 2> exprs
= {
1211 {std::make_tuple(ReplMode::Command
, text
, offset
),
1212 std::make_tuple(ReplMode::Variable
, expr_prefix
+ text
,
1213 offset
+ expr_prefix
.size())}};
1214 for (const auto &[mode
, line
, cursor
] : exprs
) {
1215 if (g_dap
.repl_mode
!= ReplMode::Auto
&& g_dap
.repl_mode
!= mode
)
1218 lldb::SBStringList matches
;
1219 lldb::SBStringList descriptions
;
1220 if (!g_dap
.debugger
.GetCommandInterpreter()
1221 .HandleCompletionWithDescriptions(line
.c_str(), cursor
, 0, 100,
1222 matches
, descriptions
))
1225 // The first element is the common substring after the cursor position for
1226 // all the matches. The rest of the elements are the matches so ignore the
1228 for (size_t i
= 1; i
< matches
.GetSize(); i
++) {
1229 std::string match
= matches
.GetStringAtIndex(i
);
1230 std::string description
= descriptions
.GetStringAtIndex(i
);
1232 llvm::json::Object item
;
1233 llvm::StringRef match_ref
= match
;
1234 for (llvm::StringRef commit_point
: {".", "->"}) {
1235 if (match_ref
.contains(commit_point
)) {
1236 match_ref
= match_ref
.rsplit(commit_point
).second
;
1239 EmplaceSafeString(item
, "text", match_ref
);
1241 if (description
.empty())
1242 EmplaceSafeString(item
, "label", match
);
1244 EmplaceSafeString(item
, "label", match
+ " -- " + description
);
1246 targets
.emplace_back(std::move(item
));
1250 body
.try_emplace("targets", std::move(targets
));
1251 response
.try_emplace("body", std::move(body
));
1252 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
1255 // "EvaluateRequest": {
1256 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1257 // "type": "object",
1258 // "description": "Evaluate request; value of command field is 'evaluate'.
1259 // Evaluates the given expression in the context of the
1260 // top most stack frame. The expression has access to any
1261 // variables and arguments that are in scope.",
1264 // "type": "string",
1265 // "enum": [ "evaluate" ]
1268 // "$ref": "#/definitions/EvaluateArguments"
1271 // "required": [ "command", "arguments" ]
1274 // "EvaluateArguments": {
1275 // "type": "object",
1276 // "description": "Arguments for 'evaluate' request.",
1279 // "type": "string",
1280 // "description": "The expression to evaluate."
1283 // "type": "integer",
1284 // "description": "Evaluate the expression in the scope of this stack
1285 // frame. If not specified, the expression is evaluated
1286 // in the global scope."
1289 // "type": "string",
1290 // "_enum": [ "watch", "repl", "hover" ],
1291 // "enumDescriptions": [
1292 // "evaluate is run in a watch.",
1293 // "evaluate is run from REPL console.",
1294 // "evaluate is run from a data hover."
1296 // "description": "The context in which the evaluate request is run."
1299 // "$ref": "#/definitions/ValueFormat",
1300 // "description": "Specifies details on how to format the Evaluate
1304 // "required": [ "expression" ]
1306 // "EvaluateResponse": {
1307 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1308 // "type": "object",
1309 // "description": "Response to 'evaluate' request.",
1312 // "type": "object",
1315 // "type": "string",
1316 // "description": "The result of the evaluate request."
1319 // "type": "string",
1320 // "description": "The optional type of the evaluate result."
1322 // "presentationHint": {
1323 // "$ref": "#/definitions/VariablePresentationHint",
1324 // "description": "Properties of a evaluate result that can be
1325 // used to determine how to render the result in
1328 // "variablesReference": {
1329 // "type": "number",
1330 // "description": "If variablesReference is > 0, the evaluate
1331 // result is structured and its children can be
1332 // retrieved by passing variablesReference to the
1333 // VariablesRequest."
1335 // "namedVariables": {
1336 // "type": "number",
1337 // "description": "The number of named child variables. The
1338 // client can use this optional information to
1339 // present the variables in a paged UI and fetch
1342 // "indexedVariables": {
1343 // "type": "number",
1344 // "description": "The number of indexed child variables. The
1345 // client can use this optional information to
1346 // present the variables in a paged UI and fetch
1350 // "required": [ "result", "variablesReference" ]
1353 // "required": [ "body" ]
1356 void request_evaluate(const llvm::json::Object
&request
) {
1357 llvm::json::Object response
;
1358 FillResponse(request
, response
);
1359 llvm::json::Object body
;
1360 auto arguments
= request
.getObject("arguments");
1361 lldb::SBFrame frame
= g_dap
.GetLLDBFrame(*arguments
);
1362 std::string expression
= GetString(arguments
, "expression").str();
1363 llvm::StringRef context
= GetString(arguments
, "context");
1365 if (context
== "repl" && g_dap
.DetectExpressionContext(frame
, expression
) ==
1366 ExpressionContext::Command
) {
1367 // If we're evaluating a command relative to the current frame, set the
1368 // focus_tid to the current frame for any thread related events.
1369 if (frame
.IsValid()) {
1370 g_dap
.focus_tid
= frame
.GetThread().GetThreadID();
1373 RunLLDBCommandsVerbatim(llvm::StringRef(), {std::string(expression
)});
1374 EmplaceSafeString(body
, "result", result
);
1375 body
.try_emplace("variablesReference", (int64_t)0);
1377 // Always try to get the answer from the local variables if possible. If
1378 // this fails, then if the context is not "hover", actually evaluate an
1379 // expression using the expression parser.
1381 // "frame variable" is more reliable than the expression parser in
1382 // many cases and it is faster.
1383 lldb::SBValue value
= frame
.GetValueForVariablePath(
1384 expression
.data(), lldb::eDynamicDontRunTarget
);
1386 // Freeze dry the value in case users expand it later in the debug console
1387 if (value
.GetError().Success() && context
== "repl")
1388 value
= value
.Persist();
1390 if (value
.GetError().Fail() && context
!= "hover")
1391 value
= frame
.EvaluateExpression(expression
.data());
1393 if (value
.GetError().Fail()) {
1394 response
["success"] = llvm::json::Value(false);
1395 // This error object must live until we're done with the pointer returned
1397 lldb::SBError error
= value
.GetError();
1398 const char *error_cstr
= error
.GetCString();
1399 if (error_cstr
&& error_cstr
[0])
1400 EmplaceSafeString(response
, "message", std::string(error_cstr
));
1402 EmplaceSafeString(response
, "message", "evaluate failed");
1404 VariableDescription
desc(value
);
1405 EmplaceSafeString(body
, "result", desc
.GetResult(context
));
1406 EmplaceSafeString(body
, "type", desc
.display_type_name
);
1407 if (value
.MightHaveChildren()) {
1408 auto variableReference
= g_dap
.variables
.InsertExpandableVariable(
1409 value
, /*is_permanent=*/context
== "repl");
1410 body
.try_emplace("variablesReference", variableReference
);
1412 body
.try_emplace("variablesReference", (int64_t)0);
1416 response
.try_emplace("body", std::move(body
));
1417 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
1420 // "compileUnitsRequest": {
1421 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1422 // "type": "object",
1423 // "description": "Compile Unit request; value of command field is
1424 // 'compileUnits'.",
1427 // "type": "string",
1428 // "enum": [ "compileUnits" ]
1431 // "$ref": "#/definitions/compileUnitRequestArguments"
1434 // "required": [ "command", "arguments" ]
1437 // "compileUnitsRequestArguments": {
1438 // "type": "object",
1439 // "description": "Arguments for 'compileUnits' request.",
1442 // "type": "string",
1443 // "description": "The ID of the module."
1446 // "required": [ "moduleId" ]
1448 // "compileUnitsResponse": {
1449 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1450 // "type": "object",
1451 // "description": "Response to 'compileUnits' request.",
1454 // "description": "Response to 'compileUnits' request. Array of
1455 // paths of compile units."
1460 void request_compileUnits(const llvm::json::Object
&request
) {
1461 llvm::json::Object response
;
1462 FillResponse(request
, response
);
1463 llvm::json::Object body
;
1464 llvm::json::Array units
;
1465 auto arguments
= request
.getObject("arguments");
1466 std::string module_id
= std::string(GetString(arguments
, "moduleId"));
1467 int num_modules
= g_dap
.target
.GetNumModules();
1468 for (int i
= 0; i
< num_modules
; i
++) {
1469 auto curr_module
= g_dap
.target
.GetModuleAtIndex(i
);
1470 if (module_id
== curr_module
.GetUUIDString()) {
1471 int num_units
= curr_module
.GetNumCompileUnits();
1472 for (int j
= 0; j
< num_units
; j
++) {
1473 auto curr_unit
= curr_module
.GetCompileUnitAtIndex(j
);
1474 units
.emplace_back(CreateCompileUnit(curr_unit
));
1476 body
.try_emplace("compileUnits", std::move(units
));
1480 response
.try_emplace("body", std::move(body
));
1481 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
1484 // "modulesRequest": {
1485 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1486 // "type": "object",
1487 // "description": "Modules request; value of command field is
1491 // "type": "string",
1492 // "enum": [ "modules" ]
1495 // "required": [ "command" ]
1498 // "modulesResponse": {
1499 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1500 // "type": "object",
1501 // "description": "Response to 'modules' request.",
1504 // "description": "Response to 'modules' request. Array of
1510 void request_modules(const llvm::json::Object
&request
) {
1511 llvm::json::Object response
;
1512 FillResponse(request
, response
);
1514 llvm::json::Array modules
;
1515 for (size_t i
= 0; i
< g_dap
.target
.GetNumModules(); i
++) {
1516 lldb::SBModule module
= g_dap
.target
.GetModuleAtIndex(i
);
1517 modules
.emplace_back(CreateModule(module
));
1520 llvm::json::Object body
;
1521 body
.try_emplace("modules", std::move(modules
));
1522 response
.try_emplace("body", std::move(body
));
1523 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
1526 // "InitializeRequest": {
1527 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1528 // "type": "object",
1529 // "description": "Initialize request; value of command field is
1533 // "type": "string",
1534 // "enum": [ "initialize" ]
1537 // "$ref": "#/definitions/InitializeRequestArguments"
1540 // "required": [ "command", "arguments" ]
1543 // "InitializeRequestArguments": {
1544 // "type": "object",
1545 // "description": "Arguments for 'initialize' request.",
1548 // "type": "string",
1549 // "description": "The ID of the (frontend) client using this adapter."
1552 // "type": "string",
1553 // "description": "The ID of the debug adapter."
1556 // "type": "string",
1557 // "description": "The ISO-639 locale of the (frontend) client using
1558 // this adapter, e.g. en-US or de-CH."
1560 // "linesStartAt1": {
1561 // "type": "boolean",
1562 // "description": "If true all line numbers are 1-based (default)."
1564 // "columnsStartAt1": {
1565 // "type": "boolean",
1566 // "description": "If true all column numbers are 1-based (default)."
1569 // "type": "string",
1570 // "_enum": [ "path", "uri" ],
1571 // "description": "Determines in what format paths are specified. The
1572 // default is 'path', which is the native format."
1574 // "supportsVariableType": {
1575 // "type": "boolean",
1576 // "description": "Client supports the optional type attribute for
1579 // "supportsVariablePaging": {
1580 // "type": "boolean",
1581 // "description": "Client supports the paging of variables."
1583 // "supportsRunInTerminalRequest": {
1584 // "type": "boolean",
1585 // "description": "Client supports the runInTerminal request."
1588 // "required": [ "adapterID" ]
1590 // "InitializeResponse": {
1591 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1592 // "type": "object",
1593 // "description": "Response to 'initialize' request.",
1596 // "$ref": "#/definitions/Capabilities",
1597 // "description": "The capabilities of this debug adapter."
1602 void request_initialize(const llvm::json::Object
&request
) {
1603 llvm::json::Object response
;
1604 FillResponse(request
, response
);
1605 llvm::json::Object body
;
1607 auto log_cb
= [](const char *buf
, void *baton
) -> void {
1608 g_dap
.SendOutput(OutputType::Console
, llvm::StringRef
{buf
});
1611 auto arguments
= request
.getObject("arguments");
1612 // sourceInitFile option is not from formal DAP specification. It is only
1613 // used by unit tests to prevent sourcing .lldbinit files from environment
1614 // which may affect the outcome of tests.
1615 bool source_init_file
= GetBoolean(arguments
, "sourceInitFile", true);
1617 g_dap
.debugger
= lldb::SBDebugger::Create(source_init_file
, log_cb
, nullptr);
1618 if (llvm::Error err
= g_dap
.RunPreInitCommands()) {
1619 response
["success"] = false;
1620 EmplaceSafeString(response
, "message", llvm::toString(std::move(err
)));
1621 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
1625 g_dap
.PopulateExceptionBreakpoints();
1626 auto cmd
= g_dap
.debugger
.GetCommandInterpreter().AddMultiwordCommand(
1627 "lldb-dap", "Commands for managing lldb-dap.");
1628 if (GetBoolean(arguments
, "supportsStartDebuggingRequest", false)) {
1630 "startDebugging", &g_dap
.start_debugging_request_handler
,
1631 "Sends a startDebugging request from the debug adapter to the client "
1632 "to start a child debug session of the same type as the caller.");
1635 "repl-mode", &g_dap
.repl_mode_request_handler
,
1636 "Get or set the repl behavior of lldb-dap evaluation requests.");
1638 g_dap
.progress_event_thread
= std::thread(ProgressEventThreadFunction
);
1640 // Start our event thread so we can receive events from the debugger, target,
1641 // process and more.
1642 g_dap
.event_thread
= std::thread(EventThreadFunction
);
1644 // The debug adapter supports the configurationDoneRequest.
1645 body
.try_emplace("supportsConfigurationDoneRequest", true);
1646 // The debug adapter supports function breakpoints.
1647 body
.try_emplace("supportsFunctionBreakpoints", true);
1648 // The debug adapter supports conditional breakpoints.
1649 body
.try_emplace("supportsConditionalBreakpoints", true);
1650 // The debug adapter supports breakpoints that break execution after a
1651 // specified number of hits.
1652 body
.try_emplace("supportsHitConditionalBreakpoints", true);
1653 // The debug adapter supports a (side effect free) evaluate request for
1655 body
.try_emplace("supportsEvaluateForHovers", true);
1656 // Available filters or options for the setExceptionBreakpoints request.
1657 llvm::json::Array filters
;
1658 for (const auto &exc_bp
: *g_dap
.exception_breakpoints
) {
1659 filters
.emplace_back(CreateExceptionBreakpointFilter(exc_bp
));
1661 body
.try_emplace("exceptionBreakpointFilters", std::move(filters
));
1662 // The debug adapter supports launching a debugee in intergrated VSCode
1664 body
.try_emplace("supportsRunInTerminalRequest", true);
1665 // The debug adapter supports stepping back via the stepBack and
1666 // reverseContinue requests.
1667 body
.try_emplace("supportsStepBack", false);
1668 // The debug adapter supports setting a variable to a value.
1669 body
.try_emplace("supportsSetVariable", true);
1670 // The debug adapter supports restarting a frame.
1671 body
.try_emplace("supportsRestartFrame", false);
1672 // The debug adapter supports the gotoTargetsRequest.
1673 body
.try_emplace("supportsGotoTargetsRequest", false);
1674 // The debug adapter supports the stepInTargetsRequest.
1675 body
.try_emplace("supportsStepInTargetsRequest", true);
1676 // The debug adapter supports the completions request.
1677 body
.try_emplace("supportsCompletionsRequest", true);
1678 // The debug adapter supports the disassembly request.
1679 body
.try_emplace("supportsDisassembleRequest", true);
1681 llvm::json::Array completion_characters
;
1682 completion_characters
.emplace_back(".");
1683 completion_characters
.emplace_back(" ");
1684 completion_characters
.emplace_back("\t");
1685 body
.try_emplace("completionTriggerCharacters",
1686 std::move(completion_characters
));
1688 // The debug adapter supports the modules request.
1689 body
.try_emplace("supportsModulesRequest", true);
1690 // The set of additional module information exposed by the debug adapter.
1691 // body.try_emplace("additionalModuleColumns"] = ColumnDescriptor
1692 // Checksum algorithms supported by the debug adapter.
1693 // body.try_emplace("supportedChecksumAlgorithms"] = ChecksumAlgorithm
1694 // The debug adapter supports the RestartRequest. In this case a client
1695 // should not implement 'restart' by terminating and relaunching the adapter
1696 // but by calling the RestartRequest.
1697 body
.try_emplace("supportsRestartRequest", true);
1698 // The debug adapter supports 'exceptionOptions' on the
1699 // setExceptionBreakpoints request.
1700 body
.try_emplace("supportsExceptionOptions", true);
1701 // The debug adapter supports a 'format' attribute on the stackTraceRequest,
1702 // variablesRequest, and evaluateRequest.
1703 body
.try_emplace("supportsValueFormattingOptions", true);
1704 // The debug adapter supports the exceptionInfo request.
1705 body
.try_emplace("supportsExceptionInfoRequest", true);
1706 // The debug adapter supports the 'terminateDebuggee' attribute on the
1707 // 'disconnect' request.
1708 body
.try_emplace("supportTerminateDebuggee", true);
1709 // The debug adapter supports the delayed loading of parts of the stack,
1710 // which requires that both the 'startFrame' and 'levels' arguments and the
1711 // 'totalFrames' result of the 'StackTrace' request are supported.
1712 body
.try_emplace("supportsDelayedStackTraceLoading", true);
1713 // The debug adapter supports the 'loadedSources' request.
1714 body
.try_emplace("supportsLoadedSourcesRequest", false);
1715 // The debug adapter supports sending progress reporting events.
1716 body
.try_emplace("supportsProgressReporting", true);
1717 // The debug adapter supports 'logMessage' in breakpoint.
1718 body
.try_emplace("supportsLogPoints", true);
1719 // The debug adapter supports data watchpoints.
1720 body
.try_emplace("supportsDataBreakpoints", true);
1722 // Put in non-DAP specification lldb specific information.
1723 llvm::json::Object lldb_json
;
1724 lldb_json
.try_emplace("version", g_dap
.debugger
.GetVersionString());
1725 body
.try_emplace("__lldb", std::move(lldb_json
));
1727 response
.try_emplace("body", std::move(body
));
1728 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
1731 llvm::Error
request_runInTerminal(const llvm::json::Object
&launch_request
,
1732 const uint64_t timeout_seconds
) {
1733 g_dap
.is_attach
= true;
1734 lldb::SBAttachInfo attach_info
;
1736 llvm::Expected
<std::shared_ptr
<FifoFile
>> comm_file_or_err
=
1737 CreateRunInTerminalCommFile();
1738 if (!comm_file_or_err
)
1739 return comm_file_or_err
.takeError();
1740 FifoFile
&comm_file
= *comm_file_or_err
.get();
1742 RunInTerminalDebugAdapterCommChannel
comm_channel(comm_file
.m_path
);
1744 lldb::pid_t debugger_pid
= LLDB_INVALID_PROCESS_ID
;
1745 #if !defined(_WIN32)
1746 debugger_pid
= getpid();
1748 llvm::json::Object reverse_request
= CreateRunInTerminalReverseRequest(
1749 launch_request
, g_dap
.debug_adaptor_path
, comm_file
.m_path
, debugger_pid
);
1750 g_dap
.SendReverseRequest("runInTerminal", std::move(reverse_request
),
1751 [](llvm::Expected
<llvm::json::Value
> value
) {
1753 llvm::Error err
= value
.takeError();
1755 << "runInTerminal request failed: "
1756 << llvm::toString(std::move(err
)) << "\n";
1760 if (llvm::Expected
<lldb::pid_t
> pid
= comm_channel
.GetLauncherPid())
1761 attach_info
.SetProcessID(*pid
);
1763 return pid
.takeError();
1765 g_dap
.debugger
.SetAsync(false);
1766 lldb::SBError error
;
1767 g_dap
.target
.Attach(attach_info
, error
);
1770 return llvm::createStringError(llvm::inconvertibleErrorCode(),
1771 "Failed to attach to the target process. %s",
1772 comm_channel
.GetLauncherError().c_str());
1773 // This will notify the runInTerminal launcher that we attached.
1774 // We have to make this async, as the function won't return until the launcher
1775 // resumes and reads the data.
1776 std::future
<lldb::SBError
> did_attach_message_success
=
1777 comm_channel
.NotifyDidAttach();
1779 // We just attached to the runInTerminal launcher, which was waiting to be
1780 // attached. We now resume it, so it can receive the didAttach notification
1781 // and then perform the exec. Upon continuing, the debugger will stop the
1782 // process right in the middle of the exec. To the user, what we are doing is
1783 // transparent, as they will only be able to see the process since the exec,
1784 // completely unaware of the preparatory work.
1785 g_dap
.target
.GetProcess().Continue();
1787 // Now that the actual target is just starting (i.e. exec was just invoked),
1788 // we return the debugger to its async state.
1789 g_dap
.debugger
.SetAsync(true);
1791 // If sending the notification failed, the launcher should be dead by now and
1792 // the async didAttach notification should have an error message, so we
1793 // return it. Otherwise, everything was a success.
1794 did_attach_message_success
.wait();
1795 error
= did_attach_message_success
.get();
1796 if (error
.Success())
1797 return llvm::Error::success();
1798 return llvm::createStringError(llvm::inconvertibleErrorCode(),
1799 error
.GetCString());
1802 // Takes a LaunchRequest object and launches the process, also handling
1803 // runInTerminal if applicable. It doesn't do any of the additional
1804 // initialization and bookkeeping stuff that is needed for `request_launch`.
1805 // This way we can reuse the process launching logic for RestartRequest too.
1806 lldb::SBError
LaunchProcess(const llvm::json::Object
&request
) {
1807 lldb::SBError error
;
1808 auto arguments
= request
.getObject("arguments");
1809 auto launchCommands
= GetStrings(arguments
, "launchCommands");
1811 // Instantiate a launch info instance for the target.
1812 auto launch_info
= g_dap
.target
.GetLaunchInfo();
1814 // Grab the current working directory if there is one and set it in the
1816 const auto cwd
= GetString(arguments
, "cwd");
1818 launch_info
.SetWorkingDirectory(cwd
.data());
1820 // Extract any extra arguments and append them to our program arguments for
1822 auto args
= GetStrings(arguments
, "args");
1824 launch_info
.SetArguments(MakeArgv(args
).data(), true);
1826 // Pass any environment variables along that the user specified.
1827 auto envs
= GetStrings(arguments
, "env");
1829 launch_info
.SetEnvironmentEntries(MakeArgv(envs
).data(), true);
1831 auto flags
= launch_info
.GetLaunchFlags();
1833 if (GetBoolean(arguments
, "disableASLR", true))
1834 flags
|= lldb::eLaunchFlagDisableASLR
;
1835 if (GetBoolean(arguments
, "disableSTDIO", false))
1836 flags
|= lldb::eLaunchFlagDisableSTDIO
;
1837 if (GetBoolean(arguments
, "shellExpandArguments", false))
1838 flags
|= lldb::eLaunchFlagShellExpandArguments
;
1839 const bool detachOnError
= GetBoolean(arguments
, "detachOnError", false);
1840 launch_info
.SetDetachOnError(detachOnError
);
1841 launch_info
.SetLaunchFlags(flags
| lldb::eLaunchFlagDebug
|
1842 lldb::eLaunchFlagStopAtEntry
);
1843 const uint64_t timeout_seconds
= GetUnsigned(arguments
, "timeout", 30);
1845 if (GetBoolean(arguments
, "runInTerminal", false)) {
1846 if (llvm::Error err
= request_runInTerminal(request
, timeout_seconds
))
1847 error
.SetErrorString(llvm::toString(std::move(err
)).c_str());
1848 } else if (launchCommands
.empty()) {
1849 // Disable async events so the launch will be successful when we return from
1850 // the launch call and the launch will happen synchronously
1851 g_dap
.debugger
.SetAsync(false);
1852 g_dap
.target
.Launch(launch_info
, error
);
1853 g_dap
.debugger
.SetAsync(true);
1855 // Set the launch info so that run commands can access the configured
1857 g_dap
.target
.SetLaunchInfo(launch_info
);
1858 if (llvm::Error err
= g_dap
.RunLaunchCommands(launchCommands
)) {
1859 error
.SetErrorString(llvm::toString(std::move(err
)).c_str());
1862 // The custom commands might have created a new target so we should use the
1863 // selected target after these commands are run.
1864 g_dap
.target
= g_dap
.debugger
.GetSelectedTarget();
1865 // Make sure the process is launched and stopped at the entry point before
1866 // proceeding as the launch commands are not run using the synchronous
1868 error
= g_dap
.WaitForProcessToStop(timeout_seconds
);
1873 // "LaunchRequest": {
1874 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1875 // "type": "object",
1876 // "description": "Launch request; value of command field is 'launch'.",
1879 // "type": "string",
1880 // "enum": [ "launch" ]
1883 // "$ref": "#/definitions/LaunchRequestArguments"
1886 // "required": [ "command", "arguments" ]
1889 // "LaunchRequestArguments": {
1890 // "type": "object",
1891 // "description": "Arguments for 'launch' request.",
1894 // "type": "boolean",
1895 // "description": "If noDebug is true the launch request should launch
1896 // the program without enabling debugging."
1900 // "LaunchResponse": {
1901 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1902 // "type": "object",
1903 // "description": "Response to 'launch' request. This is just an
1904 // acknowledgement, so no body field is required."
1907 void request_launch(const llvm::json::Object
&request
) {
1908 g_dap
.is_attach
= false;
1909 g_dap
.last_launch_or_attach_request
= request
;
1910 llvm::json::Object response
;
1911 FillResponse(request
, response
);
1912 auto arguments
= request
.getObject("arguments");
1913 g_dap
.init_commands
= GetStrings(arguments
, "initCommands");
1914 g_dap
.pre_run_commands
= GetStrings(arguments
, "preRunCommands");
1915 g_dap
.stop_commands
= GetStrings(arguments
, "stopCommands");
1916 g_dap
.exit_commands
= GetStrings(arguments
, "exitCommands");
1917 g_dap
.terminate_commands
= GetStrings(arguments
, "terminateCommands");
1918 g_dap
.post_run_commands
= GetStrings(arguments
, "postRunCommands");
1919 g_dap
.stop_at_entry
= GetBoolean(arguments
, "stopOnEntry", false);
1920 const llvm::StringRef debuggerRoot
= GetString(arguments
, "debuggerRoot");
1921 g_dap
.enable_auto_variable_summaries
=
1922 GetBoolean(arguments
, "enableAutoVariableSummaries", false);
1923 g_dap
.enable_synthetic_child_debugging
=
1924 GetBoolean(arguments
, "enableSyntheticChildDebugging", false);
1925 g_dap
.command_escape_prefix
=
1926 GetString(arguments
, "commandEscapePrefix", "`");
1927 g_dap
.SetFrameFormat(GetString(arguments
, "customFrameFormat"));
1928 g_dap
.SetThreadFormat(GetString(arguments
, "customThreadFormat"));
1930 PrintWelcomeMessage();
1932 // This is a hack for loading DWARF in .o files on Mac where the .o files
1933 // in the debug map of the main executable have relative paths which
1934 // require the lldb-dap binary to have its working directory set to that
1935 // relative root for the .o files in order to be able to load debug info.
1936 if (!debuggerRoot
.empty())
1937 llvm::sys::fs::set_current_path(debuggerRoot
);
1939 // Run any initialize LLDB commands the user specified in the launch.json.
1940 // This is run before target is created, so commands can't do anything with
1941 // the targets - preRunCommands are run with the target.
1942 if (llvm::Error err
= g_dap
.RunInitCommands()) {
1943 response
["success"] = false;
1944 EmplaceSafeString(response
, "message", llvm::toString(std::move(err
)));
1945 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
1949 SetSourceMapFromArguments(*arguments
);
1951 lldb::SBError status
;
1952 g_dap
.SetTarget(g_dap
.CreateTargetFromArguments(*arguments
, status
));
1953 if (status
.Fail()) {
1954 response
["success"] = llvm::json::Value(false);
1955 EmplaceSafeString(response
, "message", status
.GetCString());
1956 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
1960 // Run any pre run LLDB commands the user specified in the launch.json
1961 if (llvm::Error err
= g_dap
.RunPreRunCommands()) {
1962 response
["success"] = false;
1963 EmplaceSafeString(response
, "message", llvm::toString(std::move(err
)));
1964 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
1968 status
= LaunchProcess(request
);
1970 if (status
.Fail()) {
1971 response
["success"] = llvm::json::Value(false);
1972 EmplaceSafeString(response
, "message", std::string(status
.GetCString()));
1974 g_dap
.RunPostRunCommands();
1977 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
1979 if (!status
.Fail()) {
1980 if (g_dap
.is_attach
)
1981 SendProcessEvent(Attach
); // this happens when doing runInTerminal
1983 SendProcessEvent(Launch
);
1985 g_dap
.SendJSON(CreateEventObject("initialized"));
1989 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1990 // "type": "object",
1991 // "description": "Next request; value of command field is 'next'. The
1992 // request starts the debuggee to run again for one step.
1993 // The debug adapter first sends the NextResponse and then
1994 // a StoppedEvent (event type 'step') after the step has
1998 // "type": "string",
1999 // "enum": [ "next" ]
2002 // "$ref": "#/definitions/NextArguments"
2005 // "required": [ "command", "arguments" ]
2008 // "NextArguments": {
2009 // "type": "object",
2010 // "description": "Arguments for 'next' request.",
2013 // "type": "integer",
2014 // "description": "Execute 'next' for this thread."
2017 // "required": [ "threadId" ]
2019 // "NextResponse": {
2020 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2021 // "type": "object",
2022 // "description": "Response to 'next' request. This is just an
2023 // acknowledgement, so no body field is required."
2026 void request_next(const llvm::json::Object
&request
) {
2027 llvm::json::Object response
;
2028 FillResponse(request
, response
);
2029 auto arguments
= request
.getObject("arguments");
2030 lldb::SBThread thread
= g_dap
.GetLLDBThread(*arguments
);
2031 if (thread
.IsValid()) {
2032 // Remember the thread ID that caused the resume so we can set the
2033 // "threadCausedFocus" boolean value in the "stopped" events.
2034 g_dap
.focus_tid
= thread
.GetThreadID();
2037 response
["success"] = llvm::json::Value(false);
2039 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
2042 // "PauseRequest": {
2043 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2044 // "type": "object",
2045 // "description": "Pause request; value of command field is 'pause'. The
2046 // request suspenses the debuggee. The debug adapter first sends the
2047 // PauseResponse and then a StoppedEvent (event type 'pause') after the
2048 // thread has been paused successfully.", "properties": {
2050 // "type": "string",
2051 // "enum": [ "pause" ]
2054 // "$ref": "#/definitions/PauseArguments"
2057 // "required": [ "command", "arguments" ]
2060 // "PauseArguments": {
2061 // "type": "object",
2062 // "description": "Arguments for 'pause' request.",
2065 // "type": "integer",
2066 // "description": "Pause execution for this thread."
2069 // "required": [ "threadId" ]
2071 // "PauseResponse": {
2072 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2073 // "type": "object",
2074 // "description": "Response to 'pause' request. This is just an
2075 // acknowledgement, so no body field is required."
2078 void request_pause(const llvm::json::Object
&request
) {
2079 llvm::json::Object response
;
2080 FillResponse(request
, response
);
2081 lldb::SBProcess process
= g_dap
.target
.GetProcess();
2082 lldb::SBError error
= process
.Stop();
2083 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
2086 // "RestartRequest": {
2087 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2088 // "type": "object",
2089 // "description": "Restarts a debug session. Clients should only call this
2090 // request if the corresponding capability `supportsRestartRequest` is
2091 // true.\nIf the capability is missing or has the value false, a typical
2092 // client emulates `restart` by terminating the debug adapter first and then
2093 // launching it anew.",
2096 // "type": "string",
2097 // "enum": [ "restart" ]
2100 // "$ref": "#/definitions/RestartArguments"
2103 // "required": [ "command" ]
2106 // "RestartArguments": {
2107 // "type": "object",
2108 // "description": "Arguments for `restart` request.",
2112 // { "$ref": "#/definitions/LaunchRequestArguments" },
2113 // { "$ref": "#/definitions/AttachRequestArguments" }
2115 // "description": "The latest version of the `launch` or `attach`
2120 // "RestartResponse": {
2121 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2122 // "type": "object",
2123 // "description": "Response to `restart` request. This is just an
2124 // acknowledgement, so no body field is required."
2127 void request_restart(const llvm::json::Object
&request
) {
2128 llvm::json::Object response
;
2129 FillResponse(request
, response
);
2130 if (!g_dap
.last_launch_or_attach_request
) {
2131 response
["success"] = llvm::json::Value(false);
2132 EmplaceSafeString(response
, "message",
2133 "Restart request received but no process was launched.");
2134 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
2137 // Check if we were in a "launch" session or an "attach" session.
2139 // Restarting is not well defined when we started the session by attaching to
2140 // an existing process, because we don't know how the process was started, so
2141 // we don't support it.
2143 // Note that when using runInTerminal we're technically attached, but it's an
2144 // implementation detail. The adapter *did* launch the process in response to
2145 // a "launch" command, so we can still stop it and re-run it. This is why we
2146 // don't just check `g_dap.is_attach`.
2147 if (GetString(*g_dap
.last_launch_or_attach_request
, "command") == "attach") {
2148 response
["success"] = llvm::json::Value(false);
2149 EmplaceSafeString(response
, "message",
2150 "Restarting an \"attach\" session is not supported.");
2151 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
2155 // The optional `arguments` field in RestartRequest can contain an updated
2156 // version of the launch arguments. If there's one, use it.
2157 auto restart_arguments
= request
.getObject("arguments");
2158 if (restart_arguments
) {
2159 auto launch_request_arguments
= restart_arguments
->getObject("arguments");
2160 if (launch_request_arguments
) {
2161 (*g_dap
.last_launch_or_attach_request
)["arguments"] =
2162 llvm::json::Value(llvm::json::Object(*launch_request_arguments
));
2166 // Keep track of the old PID so when we get a "process exited" event from the
2167 // killed process we can detect it and not shut down the whole session.
2168 lldb::SBProcess process
= g_dap
.target
.GetProcess();
2169 g_dap
.restarting_process_id
= process
.GetProcessID();
2171 // Stop the current process if necessary. The logic here is similar to
2172 // CommandObjectProcessLaunchOrAttach::StopProcessIfNecessary, except that
2173 // we don't ask the user for confirmation.
2174 g_dap
.debugger
.SetAsync(false);
2175 if (process
.IsValid()) {
2176 lldb::StateType state
= process
.GetState();
2177 if (state
!= lldb::eStateConnected
) {
2180 // Clear the list of thread ids to avoid sending "thread exited" events
2181 // for threads of the process we are terminating.
2182 g_dap
.thread_ids
.clear();
2184 g_dap
.debugger
.SetAsync(true);
2185 LaunchProcess(*g_dap
.last_launch_or_attach_request
);
2187 // This is normally done after receiving a "configuration done" request.
2188 // Because we're restarting, configuration has already happened so we can
2189 // continue the process right away.
2190 if (g_dap
.stop_at_entry
) {
2191 SendThreadStoppedEvent();
2193 g_dap
.target
.GetProcess().Continue();
2196 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
2199 // "ScopesRequest": {
2200 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2201 // "type": "object",
2202 // "description": "Scopes request; value of command field is 'scopes'. The
2203 // request returns the variable scopes for a given stackframe ID.",
2206 // "type": "string",
2207 // "enum": [ "scopes" ]
2210 // "$ref": "#/definitions/ScopesArguments"
2213 // "required": [ "command", "arguments" ]
2216 // "ScopesArguments": {
2217 // "type": "object",
2218 // "description": "Arguments for 'scopes' request.",
2221 // "type": "integer",
2222 // "description": "Retrieve the scopes for this stackframe."
2225 // "required": [ "frameId" ]
2227 // "ScopesResponse": {
2228 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2229 // "type": "object",
2230 // "description": "Response to 'scopes' request.",
2233 // "type": "object",
2238 // "$ref": "#/definitions/Scope"
2240 // "description": "The scopes of the stackframe. If the array has
2241 // length zero, there are no scopes available."
2244 // "required": [ "scopes" ]
2247 // "required": [ "body" ]
2250 void request_scopes(const llvm::json::Object
&request
) {
2251 llvm::json::Object response
;
2252 FillResponse(request
, response
);
2253 llvm::json::Object body
;
2254 auto arguments
= request
.getObject("arguments");
2255 lldb::SBFrame frame
= g_dap
.GetLLDBFrame(*arguments
);
2256 // As the user selects different stack frames in the GUI, a "scopes" request
2257 // will be sent to the DAP. This is the only way we know that the user has
2258 // selected a frame in a thread. There are no other notifications that are
2259 // sent and VS code doesn't allow multiple frames to show variables
2260 // concurrently. If we select the thread and frame as the "scopes" requests
2261 // are sent, this allows users to type commands in the debugger console
2262 // with a backtick character to run lldb commands and these lldb commands
2263 // will now have the right context selected as they are run. If the user
2264 // types "`bt" into the debugger console and we had another thread selected
2265 // in the LLDB library, we would show the wrong thing to the user. If the
2266 // users switches threads with a lldb command like "`thread select 14", the
2267 // GUI will not update as there are no "event" notification packets that
2268 // allow us to change the currently selected thread or frame in the GUI that
2270 if (frame
.IsValid()) {
2271 frame
.GetThread().GetProcess().SetSelectedThread(frame
.GetThread());
2272 frame
.GetThread().SetSelectedFrame(frame
.GetFrameID());
2275 g_dap
.variables
.locals
= frame
.GetVariables(/*arguments=*/true,
2278 /*in_scope_only=*/true);
2279 g_dap
.variables
.globals
= frame
.GetVariables(/*arguments=*/false,
2282 /*in_scope_only=*/true);
2283 g_dap
.variables
.registers
= frame
.GetRegisters();
2284 body
.try_emplace("scopes", g_dap
.CreateTopLevelScopes());
2285 response
.try_emplace("body", std::move(body
));
2286 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
2289 // "SetBreakpointsRequest": {
2290 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2291 // "type": "object",
2292 // "description": "SetBreakpoints request; value of command field is
2293 // 'setBreakpoints'. Sets multiple breakpoints for a single source and
2294 // clears all previous breakpoints in that source. To clear all breakpoint
2295 // for a source, specify an empty array. When a breakpoint is hit, a
2296 // StoppedEvent (event type 'breakpoint') is generated.", "properties": {
2298 // "type": "string",
2299 // "enum": [ "setBreakpoints" ]
2302 // "$ref": "#/definitions/SetBreakpointsArguments"
2305 // "required": [ "command", "arguments" ]
2308 // "SetBreakpointsArguments": {
2309 // "type": "object",
2310 // "description": "Arguments for 'setBreakpoints' request.",
2313 // "$ref": "#/definitions/Source",
2314 // "description": "The source location of the breakpoints; either
2315 // source.path or source.reference must be specified."
2320 // "$ref": "#/definitions/SourceBreakpoint"
2322 // "description": "The code locations of the breakpoints."
2327 // "type": "integer"
2329 // "description": "Deprecated: The code locations of the breakpoints."
2331 // "sourceModified": {
2332 // "type": "boolean",
2333 // "description": "A value of true indicates that the underlying source
2334 // has been modified which results in new breakpoint locations."
2337 // "required": [ "source" ]
2339 // "SetBreakpointsResponse": {
2340 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2341 // "type": "object",
2342 // "description": "Response to 'setBreakpoints' request. Returned is
2343 // information about each breakpoint created by this request. This includes
2344 // the actual code location and whether the breakpoint could be verified.
2345 // The breakpoints returned are in the same order as the elements of the
2346 // 'breakpoints' (or the deprecated 'lines') in the
2347 // SetBreakpointsArguments.", "properties": {
2349 // "type": "object",
2354 // "$ref": "#/definitions/Breakpoint"
2356 // "description": "Information about the breakpoints. The array
2357 // elements are in the same order as the elements of the
2358 // 'breakpoints' (or the deprecated 'lines') in the
2359 // SetBreakpointsArguments."
2362 // "required": [ "breakpoints" ]
2365 // "required": [ "body" ]
2368 // "SourceBreakpoint": {
2369 // "type": "object",
2370 // "description": "Properties of a breakpoint or logpoint passed to the
2371 // setBreakpoints request.", "properties": {
2373 // "type": "integer",
2374 // "description": "The source line of the breakpoint or logpoint."
2377 // "type": "integer",
2378 // "description": "An optional source column of the breakpoint."
2381 // "type": "string",
2382 // "description": "An optional expression for conditional breakpoints."
2384 // "hitCondition": {
2385 // "type": "string",
2386 // "description": "An optional expression that controls how many hits of
2387 // the breakpoint are ignored. The backend is expected to interpret the
2388 // expression as needed."
2391 // "type": "string",
2392 // "description": "If this attribute exists and is non-empty, the backend
2393 // must not 'break' (stop) but log the message instead. Expressions within
2394 // {} are interpolated."
2397 // "required": [ "line" ]
2399 void request_setBreakpoints(const llvm::json::Object
&request
) {
2400 llvm::json::Object response
;
2401 lldb::SBError error
;
2402 FillResponse(request
, response
);
2403 auto arguments
= request
.getObject("arguments");
2404 auto source
= arguments
->getObject("source");
2405 const auto path
= GetString(source
, "path");
2406 auto breakpoints
= arguments
->getArray("breakpoints");
2407 llvm::json::Array response_breakpoints
;
2409 // Decode the source breakpoint infos for this "setBreakpoints" request
2410 SourceBreakpointMap request_bps
;
2411 // "breakpoints" may be unset, in which case we treat it the same as being set
2412 // to an empty array.
2414 for (const auto &bp
: *breakpoints
) {
2415 auto bp_obj
= bp
.getAsObject();
2417 SourceBreakpoint
src_bp(*bp_obj
);
2418 request_bps
[src_bp
.line
] = src_bp
;
2420 // We check if this breakpoint already exists to update it
2421 auto existing_source_bps
= g_dap
.source_breakpoints
.find(path
);
2422 if (existing_source_bps
!= g_dap
.source_breakpoints
.end()) {
2423 const auto &existing_bp
=
2424 existing_source_bps
->second
.find(src_bp
.line
);
2425 if (existing_bp
!= existing_source_bps
->second
.end()) {
2426 existing_bp
->second
.UpdateBreakpoint(src_bp
);
2427 AppendBreakpoint(&existing_bp
->second
, response_breakpoints
, path
,
2432 // At this point the breakpoint is new
2433 g_dap
.source_breakpoints
[path
][src_bp
.line
] = src_bp
;
2434 SourceBreakpoint
&new_bp
= g_dap
.source_breakpoints
[path
][src_bp
.line
];
2435 new_bp
.SetBreakpoint(path
.data());
2436 AppendBreakpoint(&new_bp
, response_breakpoints
, path
, new_bp
.line
);
2441 // Delete any breakpoints in this source file that aren't in the
2442 // request_bps set. There is no call to remove breakpoints other than
2443 // calling this function with a smaller or empty "breakpoints" list.
2444 auto old_src_bp_pos
= g_dap
.source_breakpoints
.find(path
);
2445 if (old_src_bp_pos
!= g_dap
.source_breakpoints
.end()) {
2446 for (auto &old_bp
: old_src_bp_pos
->second
) {
2447 auto request_pos
= request_bps
.find(old_bp
.first
);
2448 if (request_pos
== request_bps
.end()) {
2449 // This breakpoint no longer exists in this source file, delete it
2450 g_dap
.target
.BreakpointDelete(old_bp
.second
.bp
.GetID());
2451 old_src_bp_pos
->second
.erase(old_bp
.first
);
2456 llvm::json::Object body
;
2457 body
.try_emplace("breakpoints", std::move(response_breakpoints
));
2458 response
.try_emplace("body", std::move(body
));
2459 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
2462 // "SetExceptionBreakpointsRequest": {
2463 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2464 // "type": "object",
2465 // "description": "SetExceptionBreakpoints request; value of command field
2466 // is 'setExceptionBreakpoints'. The request configures the debuggers
2467 // response to thrown exceptions. If an exception is configured to break, a
2468 // StoppedEvent is fired (event type 'exception').", "properties": {
2470 // "type": "string",
2471 // "enum": [ "setExceptionBreakpoints" ]
2474 // "$ref": "#/definitions/SetExceptionBreakpointsArguments"
2477 // "required": [ "command", "arguments" ]
2480 // "SetExceptionBreakpointsArguments": {
2481 // "type": "object",
2482 // "description": "Arguments for 'setExceptionBreakpoints' request.",
2489 // "description": "IDs of checked exception options. The set of IDs is
2490 // returned via the 'exceptionBreakpointFilters' capability."
2492 // "exceptionOptions": {
2495 // "$ref": "#/definitions/ExceptionOptions"
2497 // "description": "Configuration options for selected exceptions."
2500 // "required": [ "filters" ]
2502 // "SetExceptionBreakpointsResponse": {
2503 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2504 // "type": "object",
2505 // "description": "Response to 'setExceptionBreakpoints' request. This is
2506 // just an acknowledgement, so no body field is required."
2509 void request_setExceptionBreakpoints(const llvm::json::Object
&request
) {
2510 llvm::json::Object response
;
2511 lldb::SBError error
;
2512 FillResponse(request
, response
);
2513 auto arguments
= request
.getObject("arguments");
2514 auto filters
= arguments
->getArray("filters");
2515 // Keep a list of any exception breakpoint filter names that weren't set
2516 // so we can clear any exception breakpoints if needed.
2517 std::set
<std::string
> unset_filters
;
2518 for (const auto &bp
: *g_dap
.exception_breakpoints
)
2519 unset_filters
.insert(bp
.filter
);
2521 for (const auto &value
: *filters
) {
2522 const auto filter
= GetAsString(value
);
2523 auto exc_bp
= g_dap
.GetExceptionBreakpoint(std::string(filter
));
2525 exc_bp
->SetBreakpoint();
2526 unset_filters
.erase(std::string(filter
));
2529 for (const auto &filter
: unset_filters
) {
2530 auto exc_bp
= g_dap
.GetExceptionBreakpoint(filter
);
2532 exc_bp
->ClearBreakpoint();
2534 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
2537 // "SetFunctionBreakpointsRequest": {
2538 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2539 // "type": "object",
2540 // "description": "SetFunctionBreakpoints request; value of command field is
2541 // 'setFunctionBreakpoints'. Sets multiple function breakpoints and clears
2542 // all previous function breakpoints. To clear all function breakpoint,
2543 // specify an empty array. When a function breakpoint is hit, a StoppedEvent
2544 // (event type 'function breakpoint') is generated.", "properties": {
2546 // "type": "string",
2547 // "enum": [ "setFunctionBreakpoints" ]
2550 // "$ref": "#/definitions/SetFunctionBreakpointsArguments"
2553 // "required": [ "command", "arguments" ]
2556 // "SetFunctionBreakpointsArguments": {
2557 // "type": "object",
2558 // "description": "Arguments for 'setFunctionBreakpoints' request.",
2563 // "$ref": "#/definitions/FunctionBreakpoint"
2565 // "description": "The function names of the breakpoints."
2568 // "required": [ "breakpoints" ]
2570 // "FunctionBreakpoint": {
2571 // "type": "object",
2572 // "description": "Properties of a breakpoint passed to the
2573 // setFunctionBreakpoints request.", "properties": {
2575 // "type": "string",
2576 // "description": "The name of the function."
2579 // "type": "string",
2580 // "description": "An optional expression for conditional breakpoints."
2582 // "hitCondition": {
2583 // "type": "string",
2584 // "description": "An optional expression that controls how many hits of
2585 // the breakpoint are ignored. The backend is expected to interpret the
2586 // expression as needed."
2589 // "required": [ "name" ]
2591 // "SetFunctionBreakpointsResponse": {
2592 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2593 // "type": "object",
2594 // "description": "Response to 'setFunctionBreakpoints' request. Returned is
2595 // information about each breakpoint created by this request.",
2598 // "type": "object",
2603 // "$ref": "#/definitions/Breakpoint"
2605 // "description": "Information about the breakpoints. The array
2606 // elements correspond to the elements of the 'breakpoints' array."
2609 // "required": [ "breakpoints" ]
2612 // "required": [ "body" ]
2615 void request_setFunctionBreakpoints(const llvm::json::Object
&request
) {
2616 llvm::json::Object response
;
2617 lldb::SBError error
;
2618 FillResponse(request
, response
);
2619 auto arguments
= request
.getObject("arguments");
2620 auto breakpoints
= arguments
->getArray("breakpoints");
2621 FunctionBreakpointMap request_bps
;
2622 llvm::json::Array response_breakpoints
;
2623 for (const auto &value
: *breakpoints
) {
2624 auto bp_obj
= value
.getAsObject();
2625 if (bp_obj
== nullptr)
2627 FunctionBreakpoint
func_bp(*bp_obj
);
2628 request_bps
[func_bp
.functionName
] = std::move(func_bp
);
2631 std::vector
<llvm::StringRef
> remove_names
;
2632 // Disable any function breakpoints that aren't in the request_bps.
2633 // There is no call to remove function breakpoints other than calling this
2634 // function with a smaller or empty "breakpoints" list.
2635 for (auto &pair
: g_dap
.function_breakpoints
) {
2636 auto request_pos
= request_bps
.find(pair
.first());
2637 if (request_pos
== request_bps
.end()) {
2638 // This function breakpoint no longer exists delete it from LLDB
2639 g_dap
.target
.BreakpointDelete(pair
.second
.bp
.GetID());
2640 remove_names
.push_back(pair
.first());
2642 // Update the existing breakpoint as any setting withing the function
2643 // breakpoint might have changed.
2644 pair
.second
.UpdateBreakpoint(request_pos
->second
);
2645 // Remove this breakpoint from the request breakpoints since we have
2646 // handled it here and we don't need to set a new breakpoint below.
2647 request_bps
.erase(request_pos
);
2648 // Add this breakpoint info to the response
2649 AppendBreakpoint(&pair
.second
, response_breakpoints
);
2652 // Remove any breakpoints that are no longer in our list
2653 for (const auto &name
: remove_names
)
2654 g_dap
.function_breakpoints
.erase(name
);
2656 // Any breakpoints that are left in "request_bps" are breakpoints that
2658 for (auto &pair
: request_bps
) {
2659 // Add this breakpoint info to the response
2660 g_dap
.function_breakpoints
[pair
.first()] = std::move(pair
.second
);
2661 FunctionBreakpoint
&new_bp
= g_dap
.function_breakpoints
[pair
.first()];
2662 new_bp
.SetBreakpoint();
2663 AppendBreakpoint(&new_bp
, response_breakpoints
);
2666 llvm::json::Object body
;
2667 body
.try_emplace("breakpoints", std::move(response_breakpoints
));
2668 response
.try_emplace("body", std::move(body
));
2669 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
2672 // "DataBreakpointInfoRequest": {
2673 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2674 // "type": "object",
2675 // "description": "Obtains information on a possible data breakpoint that
2676 // could be set on an expression or variable.\nClients should only call this
2677 // request if the corresponding capability `supportsDataBreakpoints` is
2678 // true.", "properties": {
2680 // "type": "string",
2681 // "enum": [ "dataBreakpointInfo" ]
2684 // "$ref": "#/definitions/DataBreakpointInfoArguments"
2687 // "required": [ "command", "arguments" ]
2690 // "DataBreakpointInfoArguments": {
2691 // "type": "object",
2692 // "description": "Arguments for `dataBreakpointInfo` request.",
2694 // "variablesReference": {
2695 // "type": "integer",
2696 // "description": "Reference to the variable container if the data
2697 // breakpoint is requested for a child of the container. The
2698 // `variablesReference` must have been obtained in the current suspended
2699 // state. See 'Lifetime of Object References' in the Overview section for
2703 // "type": "string",
2704 // "description": "The name of the variable's child to obtain data
2705 // breakpoint information for.\nIf `variablesReference` isn't specified,
2706 // this can be an expression."
2709 // "type": "integer",
2710 // "description": "When `name` is an expression, evaluate it in the scope
2711 // of this stack frame. If not specified, the expression is evaluated in
2712 // the global scope. When `variablesReference` is specified, this property
2716 // "required": [ "name" ]
2718 // "DataBreakpointInfoResponse": {
2719 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2720 // "type": "object",
2721 // "description": "Response to `dataBreakpointInfo` request.",
2724 // "type": "object",
2727 // "type": [ "string", "null" ],
2728 // "description": "An identifier for the data on which a data
2729 // breakpoint can be registered with the `setDataBreakpoints`
2730 // request or null if no data breakpoint is available. If a
2731 // `variablesReference` or `frameId` is passed, the `dataId` is
2732 // valid in the current suspended state, otherwise it's valid
2733 // indefinitely. See 'Lifetime of Object References' in the Overview
2734 // section for details. Breakpoints set using the `dataId` in the
2735 // `setDataBreakpoints` request may outlive the lifetime of the
2736 // associated `dataId`."
2739 // "type": "string",
2740 // "description": "UI string that describes on what data the
2741 // breakpoint is set on or why a data breakpoint is not available."
2746 // "$ref": "#/definitions/DataBreakpointAccessType"
2748 // "description": "Attribute lists the available access types for a
2749 // potential data breakpoint. A UI client could surface this
2753 // "type": "boolean",
2754 // "description": "Attribute indicates that a potential data
2755 // breakpoint could be persisted across sessions."
2758 // "required": [ "dataId", "description" ]
2761 // "required": [ "body" ]
2764 void request_dataBreakpointInfo(const llvm::json::Object
&request
) {
2765 llvm::json::Object response
;
2766 FillResponse(request
, response
);
2767 llvm::json::Object body
;
2768 lldb::SBError error
;
2769 llvm::json::Array accessTypes
{"read", "write", "readWrite"};
2770 const auto *arguments
= request
.getObject("arguments");
2771 const auto variablesReference
=
2772 GetUnsigned(arguments
, "variablesReference", 0);
2773 llvm::StringRef name
= GetString(arguments
, "name");
2774 lldb::SBFrame frame
= g_dap
.GetLLDBFrame(*arguments
);
2775 lldb::SBValue variable
= FindVariable(variablesReference
, name
);
2776 std::string addr
, size
;
2778 if (variable
.IsValid()) {
2779 lldb::addr_t load_addr
= variable
.GetLoadAddress();
2780 size_t byte_size
= variable
.GetByteSize();
2781 if (load_addr
== LLDB_INVALID_ADDRESS
) {
2782 body
.try_emplace("dataId", nullptr);
2783 body
.try_emplace("description",
2784 "does not exist in memory, its location is " +
2785 std::string(variable
.GetLocation()));
2786 } else if (byte_size
== 0) {
2787 body
.try_emplace("dataId", nullptr);
2788 body
.try_emplace("description", "variable size is 0");
2790 addr
= llvm::utohexstr(load_addr
);
2791 size
= llvm::utostr(byte_size
);
2793 } else if (variablesReference
== 0 && frame
.IsValid()) {
2794 lldb::SBValue value
= frame
.EvaluateExpression(name
.data());
2795 if (value
.GetError().Fail()) {
2796 lldb::SBError error
= value
.GetError();
2797 const char *error_cstr
= error
.GetCString();
2798 body
.try_emplace("dataId", nullptr);
2799 body
.try_emplace("description", error_cstr
&& error_cstr
[0]
2800 ? std::string(error_cstr
)
2801 : "evaluation failed");
2803 uint64_t load_addr
= value
.GetValueAsUnsigned();
2804 lldb::SBData data
= value
.GetPointeeData();
2805 if (data
.IsValid()) {
2806 size
= llvm::utostr(data
.GetByteSize());
2807 addr
= llvm::utohexstr(load_addr
);
2808 lldb::SBMemoryRegionInfo region
;
2810 g_dap
.target
.GetProcess().GetMemoryRegionInfo(load_addr
, region
);
2811 // Only lldb-server supports "qMemoryRegionInfo". So, don't fail this
2812 // request if SBProcess::GetMemoryRegionInfo returns error.
2813 if (err
.Success()) {
2814 if (!(region
.IsReadable() || region
.IsWritable())) {
2815 body
.try_emplace("dataId", nullptr);
2816 body
.try_emplace("description",
2817 "memory region for address " + addr
+
2818 " has no read or write permissions");
2822 body
.try_emplace("dataId", nullptr);
2823 body
.try_emplace("description",
2824 "unable to get byte size for expression: " +
2829 body
.try_emplace("dataId", nullptr);
2830 body
.try_emplace("description", "variable not found: " + name
.str());
2833 if (!body
.getObject("dataId")) {
2834 body
.try_emplace("dataId", addr
+ "/" + size
);
2835 body
.try_emplace("accessTypes", std::move(accessTypes
));
2836 body
.try_emplace("description",
2837 size
+ " bytes at " + addr
+ " " + name
.str());
2839 response
.try_emplace("body", std::move(body
));
2840 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
2843 // "SetDataBreakpointsRequest": {
2844 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2845 // "type": "object",
2846 // "description": "Replaces all existing data breakpoints with new data
2847 // breakpoints.\nTo clear all data breakpoints, specify an empty
2848 // array.\nWhen a data breakpoint is hit, a `stopped` event (with reason
2849 // `data breakpoint`) is generated.\nClients should only call this request
2850 // if the corresponding capability `supportsDataBreakpoints` is true.",
2853 // "type": "string",
2854 // "enum": [ "setDataBreakpoints" ]
2857 // "$ref": "#/definitions/SetDataBreakpointsArguments"
2860 // "required": [ "command", "arguments" ]
2863 // "SetDataBreakpointsArguments": {
2864 // "type": "object",
2865 // "description": "Arguments for `setDataBreakpoints` request.",
2870 // "$ref": "#/definitions/DataBreakpoint"
2872 // "description": "The contents of this array replaces all existing data
2873 // breakpoints. An empty array clears all data breakpoints."
2876 // "required": [ "breakpoints" ]
2878 // "SetDataBreakpointsResponse": {
2879 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2880 // "type": "object",
2881 // "description": "Response to `setDataBreakpoints` request.\nReturned is
2882 // information about each breakpoint created by this request.",
2885 // "type": "object",
2890 // "$ref": "#/definitions/Breakpoint"
2892 // "description": "Information about the data breakpoints. The array
2893 // elements correspond to the elements of the input argument
2894 // `breakpoints` array."
2897 // "required": [ "breakpoints" ]
2900 // "required": [ "body" ]
2903 void request_setDataBreakpoints(const llvm::json::Object
&request
) {
2904 llvm::json::Object response
;
2905 lldb::SBError error
;
2906 FillResponse(request
, response
);
2907 const auto *arguments
= request
.getObject("arguments");
2908 const auto *breakpoints
= arguments
->getArray("breakpoints");
2909 llvm::json::Array response_breakpoints
;
2910 g_dap
.target
.DeleteAllWatchpoints();
2911 std::vector
<Watchpoint
> watchpoints
;
2913 for (const auto &bp
: *breakpoints
) {
2914 const auto *bp_obj
= bp
.getAsObject();
2916 Watchpoint
wp(*bp_obj
);
2917 watchpoints
.push_back(wp
);
2921 // If two watchpoints start at the same address, the latter overwrite the
2922 // former. So, we only enable those at first-seen addresses when iterating
2924 std::set
<lldb::addr_t
> addresses
;
2925 for (auto iter
= watchpoints
.rbegin(); iter
!= watchpoints
.rend(); ++iter
) {
2926 if (addresses
.count(iter
->addr
) == 0) {
2927 iter
->SetWatchpoint();
2928 addresses
.insert(iter
->addr
);
2931 for (auto wp
: watchpoints
)
2932 AppendBreakpoint(&wp
, response_breakpoints
);
2934 llvm::json::Object body
;
2935 body
.try_emplace("breakpoints", std::move(response_breakpoints
));
2936 response
.try_emplace("body", std::move(body
));
2937 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
2940 // "SourceRequest": {
2941 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2942 // "type": "object",
2943 // "description": "Source request; value of command field is 'source'. The
2944 // request retrieves the source code for a given source reference.",
2947 // "type": "string",
2948 // "enum": [ "source" ]
2951 // "$ref": "#/definitions/SourceArguments"
2954 // "required": [ "command", "arguments" ]
2957 // "SourceArguments": {
2958 // "type": "object",
2959 // "description": "Arguments for 'source' request.",
2962 // "$ref": "#/definitions/Source",
2963 // "description": "Specifies the source content to load. Either
2964 // source.path or source.sourceReference must be specified."
2966 // "sourceReference": {
2967 // "type": "integer",
2968 // "description": "The reference to the source. This is the same as
2969 // source.sourceReference. This is provided for backward compatibility
2970 // since old backends do not understand the 'source' attribute."
2973 // "required": [ "sourceReference" ]
2975 // "SourceResponse": {
2976 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2977 // "type": "object",
2978 // "description": "Response to 'source' request.",
2981 // "type": "object",
2984 // "type": "string",
2985 // "description": "Content of the source reference."
2988 // "type": "string",
2989 // "description": "Optional content type (mime type) of the source."
2992 // "required": [ "content" ]
2995 // "required": [ "body" ]
2998 void request_source(const llvm::json::Object
&request
) {
2999 llvm::json::Object response
;
3000 FillResponse(request
, response
);
3001 llvm::json::Object body
{{"content", ""}};
3002 response
.try_emplace("body", std::move(body
));
3003 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
3006 // "StackTraceRequest": {
3007 // "allOf": [ { "$ref": "#/definitions/Request" }, {
3008 // "type": "object",
3009 // "description": "StackTrace request; value of command field is
3010 // 'stackTrace'. The request returns a stacktrace from the current execution
3011 // state.", "properties": {
3013 // "type": "string",
3014 // "enum": [ "stackTrace" ]
3017 // "$ref": "#/definitions/StackTraceArguments"
3020 // "required": [ "command", "arguments" ]
3023 // "StackTraceArguments": {
3024 // "type": "object",
3025 // "description": "Arguments for 'stackTrace' request.",
3028 // "type": "integer",
3029 // "description": "Retrieve the stacktrace for this thread."
3032 // "type": "integer",
3033 // "description": "The index of the first frame to return; if omitted
3034 // frames start at 0."
3037 // "type": "integer",
3038 // "description": "The maximum number of frames to return. If levels is
3039 // not specified or 0, all frames are returned."
3042 // "$ref": "#/definitions/StackFrameFormat",
3043 // "description": "Specifies details on how to format the stack frames."
3046 // "required": [ "threadId" ]
3048 // "StackTraceResponse": {
3049 // "allOf": [ { "$ref": "#/definitions/Response" }, {
3050 // "type": "object",
3051 // "description": "Response to 'stackTrace' request.",
3054 // "type": "object",
3059 // "$ref": "#/definitions/StackFrame"
3061 // "description": "The frames of the stackframe. If the array has
3062 // length zero, there are no stackframes available. This means that
3063 // there is no location information available."
3066 // "type": "integer",
3067 // "description": "The total number of frames available."
3070 // "required": [ "stackFrames" ]
3073 // "required": [ "body" ]
3076 void request_stackTrace(const llvm::json::Object
&request
) {
3077 llvm::json::Object response
;
3078 FillResponse(request
, response
);
3079 lldb::SBError error
;
3080 auto arguments
= request
.getObject("arguments");
3081 lldb::SBThread thread
= g_dap
.GetLLDBThread(*arguments
);
3082 llvm::json::Array stackFrames
;
3083 llvm::json::Object body
;
3085 if (thread
.IsValid()) {
3086 const auto startFrame
= GetUnsigned(arguments
, "startFrame", 0);
3087 const auto levels
= GetUnsigned(arguments
, "levels", 0);
3088 const auto endFrame
= (levels
== 0) ? INT64_MAX
: (startFrame
+ levels
);
3089 auto totalFrames
= thread
.GetNumFrames();
3091 // This will always return an invalid thread when
3092 // libBacktraceRecording.dylib is not loaded or if there is no extended
3094 lldb::SBThread queue_backtrace_thread
=
3095 thread
.GetExtendedBacktraceThread("libdispatch");
3096 if (queue_backtrace_thread
.IsValid()) {
3097 // One extra frame as a label to mark the enqueued thread.
3098 totalFrames
+= queue_backtrace_thread
.GetNumFrames() + 1;
3101 // This will always return an invalid thread when there is no exception in
3102 // the current thread.
3103 lldb::SBThread exception_backtrace_thread
=
3104 thread
.GetCurrentExceptionBacktrace();
3105 if (exception_backtrace_thread
.IsValid()) {
3106 // One extra frame as a label to mark the exception thread.
3107 totalFrames
+= exception_backtrace_thread
.GetNumFrames() + 1;
3110 for (uint32_t i
= startFrame
; i
< endFrame
; ++i
) {
3111 lldb::SBFrame frame
;
3113 if (i
< thread
.GetNumFrames()) {
3114 frame
= thread
.GetFrameAtIndex(i
);
3115 } else if (queue_backtrace_thread
.IsValid() &&
3116 i
< (thread
.GetNumFrames() +
3117 queue_backtrace_thread
.GetNumFrames() + 1)) {
3118 if (i
== thread
.GetNumFrames()) {
3119 const uint32_t thread_idx
=
3120 queue_backtrace_thread
.GetExtendedBacktraceOriginatingIndexID();
3121 const char *queue_name
= queue_backtrace_thread
.GetQueueName();
3122 auto name
= llvm::formatv("Enqueued from {0} (Thread {1})",
3123 queue_name
, thread_idx
);
3124 stackFrames
.emplace_back(
3125 llvm::json::Object
{{"id", thread
.GetThreadID() + 1},
3127 {"presentationHint", "label"}});
3130 frame
= queue_backtrace_thread
.GetFrameAtIndex(
3131 i
- thread
.GetNumFrames() - 1);
3132 } else if (exception_backtrace_thread
.IsValid()) {
3133 if (i
== thread
.GetNumFrames() +
3134 (queue_backtrace_thread
.IsValid()
3135 ? queue_backtrace_thread
.GetNumFrames() + 1
3137 stackFrames
.emplace_back(
3138 llvm::json::Object
{{"id", thread
.GetThreadID() + 2},
3139 {"name", "Original Exception Backtrace"},
3140 {"presentationHint", "label"}});
3144 frame
= exception_backtrace_thread
.GetFrameAtIndex(
3145 i
- thread
.GetNumFrames() -
3146 (queue_backtrace_thread
.IsValid()
3147 ? queue_backtrace_thread
.GetNumFrames() + 1
3150 if (!frame
.IsValid())
3152 stackFrames
.emplace_back(CreateStackFrame(frame
));
3155 body
.try_emplace("totalFrames", totalFrames
);
3157 body
.try_emplace("stackFrames", std::move(stackFrames
));
3158 response
.try_emplace("body", std::move(body
));
3159 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
3162 // "StepInRequest": {
3163 // "allOf": [ { "$ref": "#/definitions/Request" }, {
3164 // "type": "object",
3165 // "description": "StepIn request; value of command field is 'stepIn'. The
3166 // request starts the debuggee to step into a function/method if possible.
3167 // If it cannot step into a target, 'stepIn' behaves like 'next'. The debug
3168 // adapter first sends the StepInResponse and then a StoppedEvent (event
3169 // type 'step') after the step has completed. If there are multiple
3170 // function/method calls (or other targets) on the source line, the optional
3171 // argument 'targetId' can be used to control into which target the 'stepIn'
3172 // should occur. The list of possible targets for a given source line can be
3173 // retrieved via the 'stepInTargets' request.", "properties": {
3175 // "type": "string",
3176 // "enum": [ "stepIn" ]
3179 // "$ref": "#/definitions/StepInArguments"
3182 // "required": [ "command", "arguments" ]
3185 // "StepInArguments": {
3186 // "type": "object",
3187 // "description": "Arguments for 'stepIn' request.",
3190 // "type": "integer",
3191 // "description": "Execute 'stepIn' for this thread."
3194 // "type": "integer",
3195 // "description": "Optional id of the target to step into."
3198 // "required": [ "threadId" ]
3200 // "StepInResponse": {
3201 // "allOf": [ { "$ref": "#/definitions/Response" }, {
3202 // "type": "object",
3203 // "description": "Response to 'stepIn' request. This is just an
3204 // acknowledgement, so no body field is required."
3207 void request_stepIn(const llvm::json::Object
&request
) {
3208 llvm::json::Object response
;
3209 FillResponse(request
, response
);
3210 auto arguments
= request
.getObject("arguments");
3212 std::string step_in_target
;
3213 uint64_t target_id
= GetUnsigned(arguments
, "targetId", 0);
3214 auto it
= g_dap
.step_in_targets
.find(target_id
);
3215 if (it
!= g_dap
.step_in_targets
.end())
3216 step_in_target
= it
->second
;
3218 const bool single_thread
= GetBoolean(arguments
, "singleThread", false);
3219 lldb::RunMode run_mode
=
3220 single_thread
? lldb::eOnlyThisThread
: lldb::eOnlyDuringStepping
;
3221 lldb::SBThread thread
= g_dap
.GetLLDBThread(*arguments
);
3222 if (thread
.IsValid()) {
3223 // Remember the thread ID that caused the resume so we can set the
3224 // "threadCausedFocus" boolean value in the "stopped" events.
3225 g_dap
.focus_tid
= thread
.GetThreadID();
3226 thread
.StepInto(step_in_target
.c_str(), run_mode
);
3228 response
["success"] = llvm::json::Value(false);
3230 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
3233 // "StepInTargetsRequest": {
3234 // "allOf": [ { "$ref": "#/definitions/Request" }, {
3235 // "type": "object",
3236 // "description": "This request retrieves the possible step-in targets for
3237 // the specified stack frame.\nThese targets can be used in the `stepIn`
3238 // request.\nClients should only call this request if the corresponding
3239 // capability `supportsStepInTargetsRequest` is true.", "properties": {
3241 // "type": "string",
3242 // "enum": [ "stepInTargets" ]
3245 // "$ref": "#/definitions/StepInTargetsArguments"
3248 // "required": [ "command", "arguments" ]
3251 // "StepInTargetsArguments": {
3252 // "type": "object",
3253 // "description": "Arguments for `stepInTargets` request.",
3256 // "type": "integer",
3257 // "description": "The stack frame for which to retrieve the possible
3258 // step-in targets."
3261 // "required": [ "frameId" ]
3263 // "StepInTargetsResponse": {
3264 // "allOf": [ { "$ref": "#/definitions/Response" }, {
3265 // "type": "object",
3266 // "description": "Response to `stepInTargets` request.",
3269 // "type": "object",
3274 // "$ref": "#/definitions/StepInTarget"
3276 // "description": "The possible step-in targets of the specified
3277 // source location."
3280 // "required": [ "targets" ]
3283 // "required": [ "body" ]
3286 void request_stepInTargets(const llvm::json::Object
&request
) {
3287 llvm::json::Object response
;
3288 FillResponse(request
, response
);
3289 auto arguments
= request
.getObject("arguments");
3291 g_dap
.step_in_targets
.clear();
3292 lldb::SBFrame frame
= g_dap
.GetLLDBFrame(*arguments
);
3293 if (frame
.IsValid()) {
3294 lldb::SBAddress pc_addr
= frame
.GetPCAddress();
3295 lldb::SBAddress line_end_addr
=
3296 pc_addr
.GetLineEntry().GetSameLineContiguousAddressRangeEnd(true);
3297 lldb::SBInstructionList insts
= g_dap
.target
.ReadInstructions(
3298 pc_addr
, line_end_addr
, /*flavor_string=*/nullptr);
3300 if (!insts
.IsValid()) {
3301 response
["success"] = false;
3302 response
["message"] = "Failed to get instructions for frame.";
3303 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
3307 llvm::json::Array step_in_targets
;
3308 const auto num_insts
= insts
.GetSize();
3309 for (size_t i
= 0; i
< num_insts
; ++i
) {
3310 lldb::SBInstruction inst
= insts
.GetInstructionAtIndex(i
);
3311 if (!inst
.IsValid())
3314 lldb::addr_t inst_addr
= inst
.GetAddress().GetLoadAddress(g_dap
.target
);
3316 // Note: currently only x86/x64 supports flow kind.
3317 lldb::InstructionControlFlowKind flow_kind
=
3318 inst
.GetControlFlowKind(g_dap
.target
);
3319 if (flow_kind
== lldb::eInstructionControlFlowKindCall
) {
3320 // Use call site instruction address as id which is easy to debug.
3321 llvm::json::Object step_in_target
;
3322 step_in_target
["id"] = inst_addr
;
3324 llvm::StringRef call_operand_name
= inst
.GetOperands(g_dap
.target
);
3325 lldb::addr_t call_target_addr
;
3326 if (call_operand_name
.getAsInteger(0, call_target_addr
))
3329 lldb::SBAddress call_target_load_addr
=
3330 g_dap
.target
.ResolveLoadAddress(call_target_addr
);
3331 if (!call_target_load_addr
.IsValid())
3334 // The existing ThreadPlanStepInRange only accept step in target
3335 // function with debug info.
3336 lldb::SBSymbolContext sc
= g_dap
.target
.ResolveSymbolContextForAddress(
3337 call_target_load_addr
, lldb::eSymbolContextFunction
);
3339 // The existing ThreadPlanStepInRange only accept step in target
3340 // function with debug info.
3341 std::string step_in_target_name
;
3342 if (sc
.IsValid() && sc
.GetFunction().IsValid())
3343 step_in_target_name
= sc
.GetFunction().GetDisplayName();
3345 // Skip call sites if we fail to resolve its symbol name.
3346 if (step_in_target_name
.empty())
3349 g_dap
.step_in_targets
.try_emplace(inst_addr
, step_in_target_name
);
3350 step_in_target
.try_emplace("label", step_in_target_name
);
3351 step_in_targets
.emplace_back(std::move(step_in_target
));
3354 llvm::json::Object body
;
3355 body
.try_emplace("targets", std::move(step_in_targets
));
3356 response
.try_emplace("body", std::move(body
));
3358 response
["success"] = llvm::json::Value(false);
3359 response
["message"] = "Failed to get frame for input frameId.";
3361 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
3364 // "StepOutRequest": {
3365 // "allOf": [ { "$ref": "#/definitions/Request" }, {
3366 // "type": "object",
3367 // "description": "StepOut request; value of command field is 'stepOut'. The
3368 // request starts the debuggee to run again for one step. The debug adapter
3369 // first sends the StepOutResponse and then a StoppedEvent (event type
3370 // 'step') after the step has completed.", "properties": {
3372 // "type": "string",
3373 // "enum": [ "stepOut" ]
3376 // "$ref": "#/definitions/StepOutArguments"
3379 // "required": [ "command", "arguments" ]
3382 // "StepOutArguments": {
3383 // "type": "object",
3384 // "description": "Arguments for 'stepOut' request.",
3387 // "type": "integer",
3388 // "description": "Execute 'stepOut' for this thread."
3391 // "required": [ "threadId" ]
3393 // "StepOutResponse": {
3394 // "allOf": [ { "$ref": "#/definitions/Response" }, {
3395 // "type": "object",
3396 // "description": "Response to 'stepOut' request. This is just an
3397 // acknowledgement, so no body field is required."
3400 void request_stepOut(const llvm::json::Object
&request
) {
3401 llvm::json::Object response
;
3402 FillResponse(request
, response
);
3403 auto arguments
= request
.getObject("arguments");
3404 lldb::SBThread thread
= g_dap
.GetLLDBThread(*arguments
);
3405 if (thread
.IsValid()) {
3406 // Remember the thread ID that caused the resume so we can set the
3407 // "threadCausedFocus" boolean value in the "stopped" events.
3408 g_dap
.focus_tid
= thread
.GetThreadID();
3411 response
["success"] = llvm::json::Value(false);
3413 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
3416 // "ThreadsRequest": {
3417 // "allOf": [ { "$ref": "#/definitions/Request" }, {
3418 // "type": "object",
3419 // "description": "Thread request; value of command field is 'threads'. The
3420 // request retrieves a list of all threads.", "properties": {
3422 // "type": "string",
3423 // "enum": [ "threads" ]
3426 // "required": [ "command" ]
3429 // "ThreadsResponse": {
3430 // "allOf": [ { "$ref": "#/definitions/Response" }, {
3431 // "type": "object",
3432 // "description": "Response to 'threads' request.",
3435 // "type": "object",
3440 // "$ref": "#/definitions/Thread"
3442 // "description": "All threads."
3445 // "required": [ "threads" ]
3448 // "required": [ "body" ]
3451 void request_threads(const llvm::json::Object
&request
) {
3453 lldb::SBProcess process
= g_dap
.target
.GetProcess();
3454 llvm::json::Object response
;
3455 FillResponse(request
, response
);
3457 const uint32_t num_threads
= process
.GetNumThreads();
3458 llvm::json::Array threads
;
3459 for (uint32_t thread_idx
= 0; thread_idx
< num_threads
; ++thread_idx
) {
3460 lldb::SBThread thread
= process
.GetThreadAtIndex(thread_idx
);
3461 threads
.emplace_back(CreateThread(thread
));
3463 if (threads
.size() == 0) {
3464 response
["success"] = llvm::json::Value(false);
3466 llvm::json::Object body
;
3467 body
.try_emplace("threads", std::move(threads
));
3468 response
.try_emplace("body", std::move(body
));
3469 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
3472 // "SetVariableRequest": {
3473 // "allOf": [ { "$ref": "#/definitions/Request" }, {
3474 // "type": "object",
3475 // "description": "setVariable request; value of command field is
3476 // 'setVariable'. Set the variable with the given name in the variable
3477 // container to a new value.", "properties": {
3479 // "type": "string",
3480 // "enum": [ "setVariable" ]
3483 // "$ref": "#/definitions/SetVariableArguments"
3486 // "required": [ "command", "arguments" ]
3489 // "SetVariableArguments": {
3490 // "type": "object",
3491 // "description": "Arguments for 'setVariable' request.",
3493 // "variablesReference": {
3494 // "type": "integer",
3495 // "description": "The reference of the variable container."
3498 // "type": "string",
3499 // "description": "The name of the variable."
3502 // "type": "string",
3503 // "description": "The value of the variable."
3506 // "$ref": "#/definitions/ValueFormat",
3507 // "description": "Specifies details on how to format the response value."
3510 // "required": [ "variablesReference", "name", "value" ]
3512 // "SetVariableResponse": {
3513 // "allOf": [ { "$ref": "#/definitions/Response" }, {
3514 // "type": "object",
3515 // "description": "Response to 'setVariable' request.",
3518 // "type": "object",
3521 // "type": "string",
3522 // "description": "The new value of the variable."
3525 // "type": "string",
3526 // "description": "The type of the new value. Typically shown in the
3527 // UI when hovering over the value."
3529 // "variablesReference": {
3530 // "type": "number",
3531 // "description": "If variablesReference is > 0, the new value is
3532 // structured and its children can be retrieved by passing
3533 // variablesReference to the VariablesRequest."
3535 // "namedVariables": {
3536 // "type": "number",
3537 // "description": "The number of named child variables. The client
3538 // can use this optional information to present the variables in a
3539 // paged UI and fetch them in chunks."
3541 // "indexedVariables": {
3542 // "type": "number",
3543 // "description": "The number of indexed child variables. The client
3544 // can use this optional information to present the variables in a
3545 // paged UI and fetch them in chunks."
3548 // "required": [ "value" ]
3551 // "required": [ "body" ]
3554 void request_setVariable(const llvm::json::Object
&request
) {
3555 llvm::json::Object response
;
3556 FillResponse(request
, response
);
3557 llvm::json::Array variables
;
3558 llvm::json::Object body
;
3559 auto arguments
= request
.getObject("arguments");
3560 // This is a reference to the containing variable/scope
3561 const auto variablesReference
=
3562 GetUnsigned(arguments
, "variablesReference", 0);
3563 llvm::StringRef name
= GetString(arguments
, "name");
3565 const auto value
= GetString(arguments
, "value");
3566 // Set success to false just in case we don't find the variable by name
3567 response
.try_emplace("success", false);
3569 lldb::SBValue variable
;
3570 int64_t newVariablesReference
= 0;
3572 // The "id" is the unique integer ID that is unique within the enclosing
3573 // variablesReference. It is optionally added to any "interface Variable"
3574 // objects to uniquely identify a variable within an enclosing
3575 // variablesReference. It helps to disambiguate between two variables that
3576 // have the same name within the same scope since the "setVariables" request
3577 // only specifies the variable reference of the enclosing scope/variable, and
3578 // the name of the variable. We could have two shadowed variables with the
3579 // same name in "Locals" or "Globals". In our case the "id" absolute index
3580 // of the variable within the g_dap.variables list.
3581 const auto id_value
= GetUnsigned(arguments
, "id", UINT64_MAX
);
3582 if (id_value
!= UINT64_MAX
) {
3583 variable
= g_dap
.variables
.GetVariable(id_value
);
3585 variable
= FindVariable(variablesReference
, name
);
3588 if (variable
.IsValid()) {
3589 lldb::SBError error
;
3590 bool success
= variable
.SetValueFromCString(value
.data(), error
);
3592 VariableDescription
desc(variable
);
3593 EmplaceSafeString(body
, "result", desc
.display_value
);
3594 EmplaceSafeString(body
, "type", desc
.display_type_name
);
3596 // We don't know the index of the variable in our g_dap.variables
3597 // so always insert a new one to get its variablesReference.
3598 // is_permanent is false because debug console does not support
3599 // setVariable request.
3600 if (variable
.MightHaveChildren())
3601 newVariablesReference
= g_dap
.variables
.InsertExpandableVariable(
3602 variable
, /*is_permanent=*/false);
3604 body
.try_emplace("variablesReference", newVariablesReference
);
3606 EmplaceSafeString(body
, "message", std::string(error
.GetCString()));
3608 response
["success"] = llvm::json::Value(success
);
3610 response
["success"] = llvm::json::Value(false);
3613 response
.try_emplace("body", std::move(body
));
3614 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
3617 // "VariablesRequest": {
3618 // "allOf": [ { "$ref": "#/definitions/Request" }, {
3619 // "type": "object",
3620 // "description": "Variables request; value of command field is 'variables'.
3621 // Retrieves all child variables for the given variable reference. An
3622 // optional filter can be used to limit the fetched children to either named
3623 // or indexed children.", "properties": {
3625 // "type": "string",
3626 // "enum": [ "variables" ]
3629 // "$ref": "#/definitions/VariablesArguments"
3632 // "required": [ "command", "arguments" ]
3635 // "VariablesArguments": {
3636 // "type": "object",
3637 // "description": "Arguments for 'variables' request.",
3639 // "variablesReference": {
3640 // "type": "integer",
3641 // "description": "The Variable reference."
3644 // "type": "string",
3645 // "enum": [ "indexed", "named" ],
3646 // "description": "Optional filter to limit the child variables to either
3647 // named or indexed. If ommited, both types are fetched."
3650 // "type": "integer",
3651 // "description": "The index of the first variable to return; if omitted
3652 // children start at 0."
3655 // "type": "integer",
3656 // "description": "The number of variables to return. If count is missing
3657 // or 0, all variables are returned."
3660 // "$ref": "#/definitions/ValueFormat",
3661 // "description": "Specifies details on how to format the Variable
3665 // "required": [ "variablesReference" ]
3667 // "VariablesResponse": {
3668 // "allOf": [ { "$ref": "#/definitions/Response" }, {
3669 // "type": "object",
3670 // "description": "Response to 'variables' request.",
3673 // "type": "object",
3678 // "$ref": "#/definitions/Variable"
3680 // "description": "All (or a range) of variables for the given
3681 // variable reference."
3684 // "required": [ "variables" ]
3687 // "required": [ "body" ]
3690 void request_variables(const llvm::json::Object
&request
) {
3691 llvm::json::Object response
;
3692 FillResponse(request
, response
);
3693 llvm::json::Array variables
;
3694 auto arguments
= request
.getObject("arguments");
3695 const auto variablesReference
=
3696 GetUnsigned(arguments
, "variablesReference", 0);
3697 const int64_t start
= GetSigned(arguments
, "start", 0);
3698 const int64_t count
= GetSigned(arguments
, "count", 0);
3700 auto format
= arguments
->getObject("format");
3702 hex
= GetBoolean(format
, "hex", false);
3704 if (lldb::SBValueList
*top_scope
= GetTopLevelScope(variablesReference
)) {
3705 // variablesReference is one of our scopes, not an actual variable it is
3706 // asking for the list of args, locals or globals.
3707 int64_t start_idx
= 0;
3708 int64_t num_children
= 0;
3710 if (variablesReference
== VARREF_REGS
) {
3711 // Change the default format of any pointer sized registers in the first
3712 // register set to be the lldb::eFormatAddressInfo so we show the pointer
3713 // and resolve what the pointer resolves to. Only change the format if the
3714 // format was set to the default format or if it was hex as some registers
3715 // have formats set for them.
3716 const uint32_t addr_size
= g_dap
.target
.GetProcess().GetAddressByteSize();
3717 lldb::SBValue reg_set
= g_dap
.variables
.registers
.GetValueAtIndex(0);
3718 const uint32_t num_regs
= reg_set
.GetNumChildren();
3719 for (uint32_t reg_idx
= 0; reg_idx
< num_regs
; ++reg_idx
) {
3720 lldb::SBValue reg
= reg_set
.GetChildAtIndex(reg_idx
);
3721 const lldb::Format format
= reg
.GetFormat();
3722 if (format
== lldb::eFormatDefault
|| format
== lldb::eFormatHex
) {
3723 if (reg
.GetByteSize() == addr_size
)
3724 reg
.SetFormat(lldb::eFormatAddressInfo
);
3729 num_children
= top_scope
->GetSize();
3730 if (num_children
== 0 && variablesReference
== VARREF_LOCALS
) {
3731 // Check for an error in the SBValueList that might explain why we don't
3732 // have locals. If we have an error display it as the sole value in the
3735 // "error" owns the error string so we must keep it alive as long as we
3736 // want to use the returns "const char *"
3737 lldb::SBError error
= top_scope
->GetError();
3738 const char *var_err
= error
.GetCString();
3740 // Create a fake variable named "error" to explain why variables were
3741 // not available. This new error will help let users know when there was
3742 // a problem that kept variables from being available for display and
3743 // allow users to fix this issue instead of seeing no variables. The
3744 // errors are only set when there is a problem that the user could
3745 // fix, so no error will show up when you have no debug info, only when
3746 // we do have debug info and something that is fixable can be done.
3747 llvm::json::Object object
;
3748 EmplaceSafeString(object
, "name", "<error>");
3749 EmplaceSafeString(object
, "type", "const char *");
3750 EmplaceSafeString(object
, "value", var_err
);
3751 object
.try_emplace("variablesReference", (int64_t)0);
3752 variables
.emplace_back(std::move(object
));
3755 const int64_t end_idx
= start_idx
+ ((count
== 0) ? num_children
: count
);
3757 // We first find out which variable names are duplicated
3758 std::map
<std::string
, int> variable_name_counts
;
3759 for (auto i
= start_idx
; i
< end_idx
; ++i
) {
3760 lldb::SBValue variable
= top_scope
->GetValueAtIndex(i
);
3761 if (!variable
.IsValid())
3763 variable_name_counts
[GetNonNullVariableName(variable
)]++;
3766 // Now we construct the result with unique display variable names
3767 for (auto i
= start_idx
; i
< end_idx
; ++i
) {
3768 lldb::SBValue variable
= top_scope
->GetValueAtIndex(i
);
3770 if (!variable
.IsValid())
3773 int64_t var_ref
= 0;
3774 if (variable
.MightHaveChildren() || variable
.IsSynthetic()) {
3775 var_ref
= g_dap
.variables
.InsertExpandableVariable(
3776 variable
, /*is_permanent=*/false);
3778 variables
.emplace_back(CreateVariable(
3779 variable
, var_ref
, var_ref
!= 0 ? var_ref
: UINT64_MAX
, hex
,
3780 variable_name_counts
[GetNonNullVariableName(variable
)] > 1));
3783 // We are expanding a variable that has children, so we will return its
3785 lldb::SBValue variable
= g_dap
.variables
.GetVariable(variablesReference
);
3786 if (variable
.IsValid()) {
3787 auto addChild
= [&](lldb::SBValue child
,
3788 std::optional
<std::string
> custom_name
= {}) {
3789 if (!child
.IsValid())
3791 if (child
.MightHaveChildren()) {
3793 g_dap
.variables
.IsPermanentVariableReference(variablesReference
);
3794 auto childVariablesReferences
=
3795 g_dap
.variables
.InsertExpandableVariable(child
, is_permanent
);
3796 variables
.emplace_back(CreateVariable(
3797 child
, childVariablesReferences
, childVariablesReferences
, hex
,
3798 /*is_name_duplicated=*/false, custom_name
));
3800 variables
.emplace_back(CreateVariable(child
, 0, INT64_MAX
, hex
,
3801 /*is_name_duplicated=*/false,
3805 const int64_t num_children
= variable
.GetNumChildren();
3806 int64_t end_idx
= start
+ ((count
== 0) ? num_children
: count
);
3808 for (; i
< end_idx
&& i
< num_children
; ++i
)
3809 addChild(variable
.GetChildAtIndex(i
));
3811 // If we haven't filled the count quota from the request, we insert a new
3812 // "[raw]" child that can be used to inspect the raw version of a
3813 // synthetic member. That eliminates the need for the user to go to the
3814 // debug console and type `frame var <variable> to get these values.
3815 if (g_dap
.enable_synthetic_child_debugging
&& variable
.IsSynthetic() &&
3817 addChild(variable
.GetNonSyntheticValue(), "[raw]");
3820 llvm::json::Object body
;
3821 body
.try_emplace("variables", std::move(variables
));
3822 response
.try_emplace("body", std::move(body
));
3823 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
3826 // "DisassembleRequest": {
3827 // "allOf": [ { "$ref": "#/definitions/Request" }, {
3828 // "type": "object",
3829 // "description": "Disassembles code stored at the provided
3830 // location.\nClients should only call this request if the corresponding
3831 // capability `supportsDisassembleRequest` is true.", "properties": {
3833 // "type": "string",
3834 // "enum": [ "disassemble" ]
3837 // "$ref": "#/definitions/DisassembleArguments"
3840 // "required": [ "command", "arguments" ]
3843 // "DisassembleArguments": {
3844 // "type": "object",
3845 // "description": "Arguments for `disassemble` request.",
3847 // "memoryReference": {
3848 // "type": "string",
3849 // "description": "Memory reference to the base location containing the
3850 // instructions to disassemble."
3853 // "type": "integer",
3854 // "description": "Offset (in bytes) to be applied to the reference
3855 // location before disassembling. Can be negative."
3857 // "instructionOffset": {
3858 // "type": "integer",
3859 // "description": "Offset (in instructions) to be applied after the byte
3860 // offset (if any) before disassembling. Can be negative."
3862 // "instructionCount": {
3863 // "type": "integer",
3864 // "description": "Number of instructions to disassemble starting at the
3865 // specified location and offset.\nAn adapter must return exactly this
3866 // number of instructions - any unavailable instructions should be
3867 // replaced with an implementation-defined 'invalid instruction' value."
3869 // "resolveSymbols": {
3870 // "type": "boolean",
3871 // "description": "If true, the adapter should attempt to resolve memory
3872 // addresses and other values to symbolic names."
3875 // "required": [ "memoryReference", "instructionCount" ]
3877 // "DisassembleResponse": {
3878 // "allOf": [ { "$ref": "#/definitions/Response" }, {
3879 // "type": "object",
3880 // "description": "Response to `disassemble` request.",
3883 // "type": "object",
3885 // "instructions": {
3888 // "$ref": "#/definitions/DisassembledInstruction"
3890 // "description": "The list of disassembled instructions."
3893 // "required": [ "instructions" ]
3898 void request_disassemble(const llvm::json::Object
&request
) {
3899 llvm::json::Object response
;
3900 FillResponse(request
, response
);
3901 auto arguments
= request
.getObject("arguments");
3903 auto memoryReference
= GetString(arguments
, "memoryReference");
3904 lldb::addr_t addr_ptr
;
3905 if (memoryReference
.consumeInteger(0, addr_ptr
)) {
3906 response
["success"] = false;
3907 response
["message"] =
3908 "Malformed memory reference: " + memoryReference
.str();
3909 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
3913 addr_ptr
+= GetSigned(arguments
, "instructionOffset", 0);
3914 lldb::SBAddress
addr(addr_ptr
, g_dap
.target
);
3915 if (!addr
.IsValid()) {
3916 response
["success"] = false;
3917 response
["message"] = "Memory reference not found in the current binary.";
3918 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
3922 const auto inst_count
= GetUnsigned(arguments
, "instructionCount", 0);
3923 lldb::SBInstructionList insts
=
3924 g_dap
.target
.ReadInstructions(addr
, inst_count
);
3926 if (!insts
.IsValid()) {
3927 response
["success"] = false;
3928 response
["message"] = "Failed to find instructions for memory address.";
3929 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
3933 const bool resolveSymbols
= GetBoolean(arguments
, "resolveSymbols", false);
3934 llvm::json::Array instructions
;
3935 const auto num_insts
= insts
.GetSize();
3936 for (size_t i
= 0; i
< num_insts
; ++i
) {
3937 lldb::SBInstruction inst
= insts
.GetInstructionAtIndex(i
);
3938 auto addr
= inst
.GetAddress();
3939 const auto inst_addr
= addr
.GetLoadAddress(g_dap
.target
);
3940 const char *m
= inst
.GetMnemonic(g_dap
.target
);
3941 const char *o
= inst
.GetOperands(g_dap
.target
);
3942 const char *c
= inst
.GetComment(g_dap
.target
);
3943 auto d
= inst
.GetData(g_dap
.target
);
3946 llvm::raw_string_ostream
sb(bytes
);
3947 for (unsigned i
= 0; i
< inst
.GetByteSize(); i
++) {
3948 lldb::SBError error
;
3949 uint8_t b
= d
.GetUnsignedInt8(error
, i
);
3950 if (error
.Success()) {
3951 sb
<< llvm::format("%2.2x ", b
);
3956 llvm::json::Object disassembled_inst
{
3957 {"address", "0x" + llvm::utohexstr(inst_addr
)},
3958 {"instructionBytes",
3959 bytes
.size() > 0 ? bytes
.substr(0, bytes
.size() - 1) : ""},
3962 std::string instruction
;
3963 llvm::raw_string_ostream
si(instruction
);
3965 lldb::SBSymbol symbol
= addr
.GetSymbol();
3966 // Only add the symbol on the first line of the function.
3967 if (symbol
.IsValid() && symbol
.GetStartAddress() == addr
) {
3968 // If we have a valid symbol, append it as a label prefix for the first
3969 // instruction. This is so you can see the start of a function/callsite
3970 // in the assembly, at the moment VS Code (1.80) does not visualize the
3971 // symbol associated with the assembly instruction.
3972 si
<< (symbol
.GetMangledName() != nullptr ? symbol
.GetMangledName()
3976 if (resolveSymbols
) {
3977 disassembled_inst
.try_emplace("symbol", symbol
.GetDisplayName());
3981 si
<< llvm::formatv("{0,7} {1,12}", m
, o
);
3987 disassembled_inst
.try_emplace("instruction", instruction
);
3989 auto line_entry
= addr
.GetLineEntry();
3990 // If the line number is 0 then the entry represents a compiler generated
3992 if (line_entry
.GetStartAddress() == addr
&& line_entry
.IsValid() &&
3993 line_entry
.GetFileSpec().IsValid() && line_entry
.GetLine() != 0) {
3994 auto source
= CreateSource(line_entry
);
3995 disassembled_inst
.try_emplace("location", source
);
3997 const auto line
= line_entry
.GetLine();
3998 if (line
&& line
!= LLDB_INVALID_LINE_NUMBER
) {
3999 disassembled_inst
.try_emplace("line", line
);
4001 const auto column
= line_entry
.GetColumn();
4002 if (column
&& column
!= LLDB_INVALID_COLUMN_NUMBER
) {
4003 disassembled_inst
.try_emplace("column", column
);
4006 auto end_line_entry
= line_entry
.GetEndAddress().GetLineEntry();
4007 if (end_line_entry
.IsValid() &&
4008 end_line_entry
.GetFileSpec() == line_entry
.GetFileSpec()) {
4009 const auto end_line
= end_line_entry
.GetLine();
4010 if (end_line
&& end_line
!= LLDB_INVALID_LINE_NUMBER
&&
4012 disassembled_inst
.try_emplace("endLine", end_line
);
4014 const auto end_column
= end_line_entry
.GetColumn();
4015 if (end_column
&& end_column
!= LLDB_INVALID_COLUMN_NUMBER
&&
4016 end_column
!= column
) {
4017 disassembled_inst
.try_emplace("endColumn", end_column
- 1);
4023 instructions
.emplace_back(std::move(disassembled_inst
));
4026 llvm::json::Object body
;
4027 body
.try_emplace("instructions", std::move(instructions
));
4028 response
.try_emplace("body", std::move(body
));
4029 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
4031 // A request used in testing to get the details on all breakpoints that are
4032 // currently set in the target. This helps us to test "setBreakpoints" and
4033 // "setFunctionBreakpoints" requests to verify we have the correct set of
4034 // breakpoints currently set in LLDB.
4035 void request__testGetTargetBreakpoints(const llvm::json::Object
&request
) {
4036 llvm::json::Object response
;
4037 FillResponse(request
, response
);
4038 llvm::json::Array response_breakpoints
;
4039 for (uint32_t i
= 0; g_dap
.target
.GetBreakpointAtIndex(i
).IsValid(); ++i
) {
4040 auto bp
= Breakpoint(g_dap
.target
.GetBreakpointAtIndex(i
));
4041 AppendBreakpoint(&bp
, response_breakpoints
);
4043 llvm::json::Object body
;
4044 body
.try_emplace("breakpoints", std::move(response_breakpoints
));
4045 response
.try_emplace("body", std::move(body
));
4046 g_dap
.SendJSON(llvm::json::Value(std::move(response
)));
4049 void RegisterRequestCallbacks() {
4050 g_dap
.RegisterRequestCallback("attach", request_attach
);
4051 g_dap
.RegisterRequestCallback("completions", request_completions
);
4052 g_dap
.RegisterRequestCallback("continue", request_continue
);
4053 g_dap
.RegisterRequestCallback("configurationDone", request_configurationDone
);
4054 g_dap
.RegisterRequestCallback("disconnect", request_disconnect
);
4055 g_dap
.RegisterRequestCallback("evaluate", request_evaluate
);
4056 g_dap
.RegisterRequestCallback("exceptionInfo", request_exceptionInfo
);
4057 g_dap
.RegisterRequestCallback("initialize", request_initialize
);
4058 g_dap
.RegisterRequestCallback("launch", request_launch
);
4059 g_dap
.RegisterRequestCallback("next", request_next
);
4060 g_dap
.RegisterRequestCallback("pause", request_pause
);
4061 g_dap
.RegisterRequestCallback("restart", request_restart
);
4062 g_dap
.RegisterRequestCallback("scopes", request_scopes
);
4063 g_dap
.RegisterRequestCallback("setBreakpoints", request_setBreakpoints
);
4064 g_dap
.RegisterRequestCallback("setExceptionBreakpoints",
4065 request_setExceptionBreakpoints
);
4066 g_dap
.RegisterRequestCallback("setFunctionBreakpoints",
4067 request_setFunctionBreakpoints
);
4068 g_dap
.RegisterRequestCallback("dataBreakpointInfo",
4069 request_dataBreakpointInfo
);
4070 g_dap
.RegisterRequestCallback("setDataBreakpoints",
4071 request_setDataBreakpoints
);
4072 g_dap
.RegisterRequestCallback("setVariable", request_setVariable
);
4073 g_dap
.RegisterRequestCallback("source", request_source
);
4074 g_dap
.RegisterRequestCallback("stackTrace", request_stackTrace
);
4075 g_dap
.RegisterRequestCallback("stepIn", request_stepIn
);
4076 g_dap
.RegisterRequestCallback("stepInTargets", request_stepInTargets
);
4077 g_dap
.RegisterRequestCallback("stepOut", request_stepOut
);
4078 g_dap
.RegisterRequestCallback("threads", request_threads
);
4079 g_dap
.RegisterRequestCallback("variables", request_variables
);
4080 g_dap
.RegisterRequestCallback("disassemble", request_disassemble
);
4082 g_dap
.RegisterRequestCallback("compileUnits", request_compileUnits
);
4083 g_dap
.RegisterRequestCallback("modules", request_modules
);
4085 g_dap
.RegisterRequestCallback("_testGetTargetBreakpoints",
4086 request__testGetTargetBreakpoints
);
4089 } // anonymous namespace
4091 static void printHelp(LLDBDAPOptTable
&table
, llvm::StringRef tool_name
) {
4092 std::string usage_str
= tool_name
.str() + " options";
4093 table
.printHelp(llvm::outs(), usage_str
.c_str(), "LLDB DAP", false);
4095 std::string examples
= R
"___(
4097 The debug adapter can be started in two modes.
4099 Running lldb-dap without any arguments will start communicating with the
4100 parent over stdio. Passing a port number causes lldb-dap to start listening
4101 for connections on that port.
4105 Passing --wait-for-debugger will pause the process at startup and wait for a
4106 debugger to attach to the process.
4110 llvm::outs() << examples
;
4113 // If --launch-target is provided, this instance of lldb-dap becomes a
4114 // runInTerminal launcher. It will ultimately launch the program specified in
4115 // the --launch-target argument, which is the original program the user wanted
4116 // to debug. This is done in such a way that the actual debug adaptor can
4117 // place breakpoints at the beginning of the program.
4119 // The launcher will communicate with the debug adaptor using a fifo file in the
4120 // directory specified in the --comm-file argument.
4122 // Regarding the actual flow, this launcher will first notify the debug adaptor
4123 // of its pid. Then, the launcher will be in a pending state waiting to be
4124 // attached by the adaptor.
4126 // Once attached and resumed, the launcher will exec and become the program
4127 // specified by --launch-target, which is the original target the
4128 // user wanted to run.
4130 // In case of errors launching the target, a suitable error message will be
4131 // emitted to the debug adaptor.
4132 void LaunchRunInTerminalTarget(llvm::opt::Arg
&target_arg
,
4133 llvm::StringRef comm_file
,
4134 lldb::pid_t debugger_pid
, char *argv
[]) {
4136 llvm::errs() << "runInTerminal is only supported on POSIX systems\n";
4140 // On Linux with the Yama security module enabled, a process can only attach
4141 // to its descendants by default. In the runInTerminal case the target
4142 // process is launched by the client so we need to allow tracing explicitly.
4143 #if defined(__linux__)
4144 if (debugger_pid
!= LLDB_INVALID_PROCESS_ID
)
4145 (void)prctl(PR_SET_PTRACER
, debugger_pid
, 0, 0, 0);
4148 RunInTerminalLauncherCommChannel
comm_channel(comm_file
);
4149 if (llvm::Error err
= comm_channel
.NotifyPid()) {
4150 llvm::errs() << llvm::toString(std::move(err
)) << "\n";
4154 // We will wait to be attached with a timeout. We don't wait indefinitely
4155 // using a signal to prevent being paused forever.
4157 // This env var should be used only for tests.
4158 const char *timeout_env_var
= getenv("LLDB_DAP_RIT_TIMEOUT_IN_MS");
4160 timeout_env_var
!= nullptr ? atoi(timeout_env_var
) : 20000;
4161 if (llvm::Error err
= comm_channel
.WaitUntilDebugAdaptorAttaches(
4162 std::chrono::milliseconds(timeout_in_ms
))) {
4163 llvm::errs() << llvm::toString(std::move(err
)) << "\n";
4167 const char *target
= target_arg
.getValue();
4168 execvp(target
, argv
);
4170 std::string error
= std::strerror(errno
);
4171 comm_channel
.NotifyError(error
);
4172 llvm::errs() << error
<< "\n";
4177 /// used only by TestVSCode_redirection_to_console.py
4178 void redirection_test() {
4179 printf("stdout message\n");
4180 fprintf(stderr
, "stderr message\n");
4185 /// Redirect stdout and stderr fo the IDE's console output.
4187 /// Errors in this operation will be printed to the log file and the IDE's
4188 /// console output as well.
4191 /// A fd pointing to the original stdout.
4192 int SetupStdoutStderrRedirection() {
4193 int stdoutfd
= fileno(stdout
);
4194 int new_stdout_fd
= dup(stdoutfd
);
4195 auto output_callback_stderr
= [](llvm::StringRef data
) {
4196 g_dap
.SendOutput(OutputType::Stderr
, data
);
4198 auto output_callback_stdout
= [](llvm::StringRef data
) {
4199 g_dap
.SendOutput(OutputType::Stdout
, data
);
4201 if (llvm::Error err
= RedirectFd(stdoutfd
, output_callback_stdout
)) {
4202 std::string error_message
= llvm::toString(std::move(err
));
4204 *g_dap
.log
<< error_message
<< std::endl
;
4205 output_callback_stderr(error_message
);
4207 if (llvm::Error err
= RedirectFd(fileno(stderr
), output_callback_stderr
)) {
4208 std::string error_message
= llvm::toString(std::move(err
));
4210 *g_dap
.log
<< error_message
<< std::endl
;
4211 output_callback_stderr(error_message
);
4214 /// used only by TestVSCode_redirection_to_console.py
4215 if (getenv("LLDB_DAP_TEST_STDOUT_STDERR_REDIRECTION") != nullptr)
4217 return new_stdout_fd
;
4220 int main(int argc
, char *argv
[]) {
4221 llvm::InitLLVM
IL(argc
, argv
, /*InstallPipeSignalExitHandler=*/false);
4222 #if !defined(__APPLE__)
4223 llvm::setBugReportMsg("PLEASE submit a bug report to " LLDB_BUG_REPORT_URL
4224 " and include the crash backtrace.\n");
4226 llvm::setBugReportMsg("PLEASE submit a bug report to " LLDB_BUG_REPORT_URL
4227 " and include the crash report from "
4228 "~/Library/Logs/DiagnosticReports/.\n");
4231 llvm::SmallString
<256> program_path(argv
[0]);
4232 llvm::sys::fs::make_absolute(program_path
);
4233 g_dap
.debug_adaptor_path
= program_path
.str().str();
4237 llvm::ArrayRef
<const char *> ArgsArr
= llvm::ArrayRef(argv
+ 1, argc
);
4238 llvm::opt::InputArgList input_args
= T
.ParseArgs(ArgsArr
, MAI
, MAC
);
4240 if (input_args
.hasArg(OPT_help
)) {
4241 printHelp(T
, llvm::sys::path::filename(argv
[0]));
4242 return EXIT_SUCCESS
;
4245 if (input_args
.hasArg(OPT_repl_mode
)) {
4246 llvm::opt::Arg
*repl_mode
= input_args
.getLastArg(OPT_repl_mode
);
4247 llvm::StringRef repl_mode_value
= repl_mode
->getValue();
4248 if (repl_mode_value
== "auto") {
4249 g_dap
.repl_mode
= ReplMode::Auto
;
4250 } else if (repl_mode_value
== "variable") {
4251 g_dap
.repl_mode
= ReplMode::Variable
;
4252 } else if (repl_mode_value
== "command") {
4253 g_dap
.repl_mode
= ReplMode::Command
;
4256 << "'" << repl_mode_value
4257 << "' is not a valid option, use 'variable', 'command' or 'auto'.\n";
4258 return EXIT_FAILURE
;
4262 if (llvm::opt::Arg
*target_arg
= input_args
.getLastArg(OPT_launch_target
)) {
4263 if (llvm::opt::Arg
*comm_file
= input_args
.getLastArg(OPT_comm_file
)) {
4264 lldb::pid_t pid
= LLDB_INVALID_PROCESS_ID
;
4265 llvm::opt::Arg
*debugger_pid
= input_args
.getLastArg(OPT_debugger_pid
);
4267 llvm::StringRef debugger_pid_value
= debugger_pid
->getValue();
4268 if (debugger_pid_value
.getAsInteger(10, pid
)) {
4269 llvm::errs() << "'" << debugger_pid_value
4270 << "' is not a valid "
4272 return EXIT_FAILURE
;
4275 int target_args_pos
= argc
;
4276 for (int i
= 0; i
< argc
; i
++)
4277 if (strcmp(argv
[i
], "--launch-target") == 0) {
4278 target_args_pos
= i
+ 1;
4281 LaunchRunInTerminalTarget(*target_arg
, comm_file
->getValue(), pid
,
4282 argv
+ target_args_pos
);
4284 llvm::errs() << "\"--launch-target\" requires \"--comm-file\" to be "
4286 return EXIT_FAILURE
;
4290 // stdout/stderr redirection to the IDE's console
4291 int new_stdout_fd
= SetupStdoutStderrRedirection();
4293 // Initialize LLDB first before we do anything.
4294 lldb::SBDebugger::Initialize();
4296 // Terminate the debugger before the C++ destructor chain kicks in.
4297 auto terminate_debugger
=
4298 llvm::make_scope_exit([] { lldb::SBDebugger::Terminate(); });
4300 RegisterRequestCallbacks();
4304 if (auto *arg
= input_args
.getLastArg(OPT_port
)) {
4305 auto optarg
= arg
->getValue();
4307 portno
= strtol(optarg
, &remainder
, 0);
4308 if (remainder
== optarg
|| *remainder
!= '\0') {
4309 fprintf(stderr
, "'%s' is not a valid port number.\n", optarg
);
4310 return EXIT_FAILURE
;
4314 #if !defined(_WIN32)
4315 if (input_args
.hasArg(OPT_wait_for_debugger
)) {
4316 printf("Paused waiting for debugger to attach (pid = %i)...\n", getpid());
4321 printf("Listening on port %i...\n", portno
);
4322 SOCKET socket_fd
= AcceptConnection(portno
);
4323 if (socket_fd
>= 0) {
4324 g_dap
.input
.descriptor
= StreamDescriptor::from_socket(socket_fd
, true);
4325 g_dap
.output
.descriptor
= StreamDescriptor::from_socket(socket_fd
, false);
4327 return EXIT_FAILURE
;
4330 g_dap
.input
.descriptor
= StreamDescriptor::from_file(fileno(stdin
), false);
4331 g_dap
.output
.descriptor
= StreamDescriptor::from_file(new_stdout_fd
, false);
4334 for (const std::string
&arg
:
4335 input_args
.getAllArgValues(OPT_pre_init_command
)) {
4336 g_dap
.pre_init_commands
.push_back(arg
);
4339 bool CleanExit
= true;
4340 if (auto Err
= g_dap
.Loop()) {
4342 *g_dap
.log
<< "Transport Error: " << llvm::toString(std::move(Err
))
4347 return CleanExit
? EXIT_SUCCESS
: EXIT_FAILURE
;