1 //===-- lldb-vscode.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 //===----------------------------------------------------------------------===//
18 #include <sys/types.h>
20 // We need to #define NOMINMAX in order to skip `min()` and `max()` macro
21 // definitions that conflict with other system headers.
22 // We also need to #undef GetObject (which is defined to GetObjectW) because
23 // the JSON code we use also has methods named `GetObject()` and we conflict
30 #include <netinet/in.h>
31 #include <sys/socket.h>
46 #include "llvm/ADT/ArrayRef.h"
47 #include "llvm/ADT/DenseMap.h"
48 #include "llvm/ADT/ScopeExit.h"
49 #include "llvm/Option/Arg.h"
50 #include "llvm/Option/ArgList.h"
51 #include "llvm/Option/Option.h"
52 #include "llvm/Support/Errno.h"
53 #include "llvm/Support/FileSystem.h"
54 #include "llvm/Support/InitLLVM.h"
55 #include "llvm/Support/Path.h"
56 #include "llvm/Support/PrettyStackTrace.h"
57 #include "llvm/Support/raw_ostream.h"
59 #include "JSONUtils.h"
60 #include "LLDBUtils.h"
61 #include "OutputRedirector.h"
65 #define PATH_MAX MAX_PATH
67 typedef int socklen_t
;
68 constexpr const char *dev_null_path
= "nul";
71 constexpr const char *dev_null_path
= "/dev/null";
75 using namespace lldb_vscode
;
79 OPT_INVALID
= 0, // This is not an option ID.
80 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
81 HELPTEXT, METAVAR, VALUES) \
83 #include "Options.inc"
87 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
88 #include "Options.inc"
91 static const llvm::opt::OptTable::Info InfoTable
[] = {
92 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
93 HELPTEXT, METAVAR, VALUES) \
94 {PREFIX, NAME, HELPTEXT, \
95 METAVAR, OPT_##ID, llvm::opt::Option::KIND##Class, \
96 PARAM, FLAGS, OPT_##GROUP, \
97 OPT_##ALIAS, ALIASARGS, VALUES},
98 #include "Options.inc"
101 class LLDBVSCodeOptTable
: public llvm::opt::OptTable
{
103 LLDBVSCodeOptTable() : OptTable(InfoTable
, true) {}
106 typedef void (*RequestCallback
)(const llvm::json::Object
&command
);
108 enum LaunchMethod
{ Launch
, Attach
, AttachForSuspendedLaunch
};
110 lldb::SBValueList
*GetTopLevelScope(int64_t variablesReference
) {
111 switch (variablesReference
) {
113 return &g_vsc
.variables
.locals
;
115 return &g_vsc
.variables
.globals
;
117 return &g_vsc
.variables
.registers
;
123 SOCKET
AcceptConnection(int portno
) {
124 // Accept a socket connection from any host on "portno".
125 SOCKET newsockfd
= -1;
126 struct sockaddr_in serv_addr
, cli_addr
;
127 SOCKET sockfd
= socket(AF_INET
, SOCK_STREAM
, 0);
130 *g_vsc
.log
<< "error: opening socket (" << strerror(errno
) << ")"
133 memset((char *)&serv_addr
, 0, sizeof(serv_addr
));
134 serv_addr
.sin_family
= AF_INET
;
135 // serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
136 serv_addr
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
137 serv_addr
.sin_port
= htons(portno
);
138 if (bind(sockfd
, (struct sockaddr
*)&serv_addr
, sizeof(serv_addr
)) < 0) {
140 *g_vsc
.log
<< "error: binding socket (" << strerror(errno
) << ")"
144 socklen_t clilen
= sizeof(cli_addr
);
146 llvm::sys::RetryAfterSignal(static_cast<SOCKET
>(-1), accept
, sockfd
,
147 (struct sockaddr
*)&cli_addr
, &clilen
);
150 *g_vsc
.log
<< "error: accept (" << strerror(errno
) << ")"
162 std::vector
<const char *> MakeArgv(const llvm::ArrayRef
<std::string
> &strs
) {
163 // Create and return an array of "const char *", one for each C string in
164 // "strs" and terminate the list with a NULL. This can be used for argument
165 // vectors (argv) or environment vectors (envp) like those passed to the
166 // "main" function in C programs.
167 std::vector
<const char *> argv
;
168 for (const auto &s
: strs
)
169 argv
.push_back(s
.c_str());
170 argv
.push_back(nullptr);
174 // Send a "exited" event to indicate the process has exited.
175 void SendProcessExitedEvent(lldb::SBProcess
&process
) {
176 llvm::json::Object
event(CreateEventObject("exited"));
177 llvm::json::Object body
;
178 body
.try_emplace("exitCode", (int64_t)process
.GetExitStatus());
179 event
.try_emplace("body", std::move(body
));
180 g_vsc
.SendJSON(llvm::json::Value(std::move(event
)));
183 void SendThreadExitedEvent(lldb::tid_t tid
) {
184 llvm::json::Object
event(CreateEventObject("thread"));
185 llvm::json::Object body
;
186 body
.try_emplace("reason", "exited");
187 body
.try_emplace("threadId", (int64_t)tid
);
188 event
.try_emplace("body", std::move(body
));
189 g_vsc
.SendJSON(llvm::json::Value(std::move(event
)));
192 // Send a "terminated" event to indicate the process is done being
194 void SendTerminatedEvent() {
195 // If an inferior exits prior to the processing of a disconnect request, then
196 // the threads executing EventThreadFunction and request_discontinue
197 // respectively may call SendTerminatedEvent simultaneously. Without any
198 // synchronization, the thread executing EventThreadFunction may set
199 // g_vsc.sent_terminated_event before the thread executing
200 // request_discontinue has had a chance to test it, in which case the latter
201 // would move ahead to issue a response to the disconnect request. Said
202 // response may get dispatched ahead of the terminated event compelling the
203 // client to terminate the debug session without consuming any console output
204 // that might've been generated by the execution of terminateCommands. So,
205 // synchronize simultaneous calls to SendTerminatedEvent.
206 static std::mutex mutex
;
207 std::lock_guard
<std::mutex
> locker(mutex
);
208 if (!g_vsc
.sent_terminated_event
) {
209 g_vsc
.sent_terminated_event
= true;
210 g_vsc
.RunTerminateCommands();
211 // Send a "terminated" event
212 llvm::json::Object
event(CreateEventObject("terminated"));
213 g_vsc
.SendJSON(llvm::json::Value(std::move(event
)));
217 // Send a thread stopped event for all threads as long as the process
219 void SendThreadStoppedEvent() {
220 lldb::SBProcess process
= g_vsc
.target
.GetProcess();
221 if (process
.IsValid()) {
222 auto state
= process
.GetState();
223 if (state
== lldb::eStateStopped
) {
224 llvm::DenseSet
<lldb::tid_t
> old_thread_ids
;
225 old_thread_ids
.swap(g_vsc
.thread_ids
);
226 uint32_t stop_id
= process
.GetStopID();
227 const uint32_t num_threads
= process
.GetNumThreads();
229 // First make a pass through the threads to see if the focused thread
230 // has a stop reason. In case the focus thread doesn't have a stop
231 // reason, remember the first thread that has a stop reason so we can
232 // set it as the focus thread if below if needed.
233 lldb::tid_t first_tid_with_reason
= LLDB_INVALID_THREAD_ID
;
234 uint32_t num_threads_with_reason
= 0;
235 bool focus_thread_exists
= false;
236 for (uint32_t thread_idx
= 0; thread_idx
< num_threads
; ++thread_idx
) {
237 lldb::SBThread thread
= process
.GetThreadAtIndex(thread_idx
);
238 const lldb::tid_t tid
= thread
.GetThreadID();
239 const bool has_reason
= ThreadHasStopReason(thread
);
240 // If the focus thread doesn't have a stop reason, clear the thread ID
241 if (tid
== g_vsc
.focus_tid
) {
242 focus_thread_exists
= true;
244 g_vsc
.focus_tid
= LLDB_INVALID_THREAD_ID
;
247 ++num_threads_with_reason
;
248 if (first_tid_with_reason
== LLDB_INVALID_THREAD_ID
)
249 first_tid_with_reason
= tid
;
253 // We will have cleared g_vsc.focus_tid if he focus thread doesn't have
254 // a stop reason, so if it was cleared, or wasn't set, or doesn't exist,
255 // then set the focus thread to the first thread with a stop reason.
256 if (!focus_thread_exists
|| g_vsc
.focus_tid
== LLDB_INVALID_THREAD_ID
)
257 g_vsc
.focus_tid
= first_tid_with_reason
;
259 // If no threads stopped with a reason, then report the first one so
260 // we at least let the UI know we stopped.
261 if (num_threads_with_reason
== 0) {
262 lldb::SBThread thread
= process
.GetThreadAtIndex(0);
263 g_vsc
.SendJSON(CreateThreadStopped(thread
, stop_id
));
265 for (uint32_t thread_idx
= 0; thread_idx
< num_threads
; ++thread_idx
) {
266 lldb::SBThread thread
= process
.GetThreadAtIndex(thread_idx
);
267 g_vsc
.thread_ids
.insert(thread
.GetThreadID());
268 if (ThreadHasStopReason(thread
)) {
269 g_vsc
.SendJSON(CreateThreadStopped(thread
, stop_id
));
274 for (auto tid
: old_thread_ids
) {
275 auto end
= g_vsc
.thread_ids
.end();
276 auto pos
= g_vsc
.thread_ids
.find(tid
);
278 SendThreadExitedEvent(tid
);
282 *g_vsc
.log
<< "error: SendThreadStoppedEvent() when process"
284 << lldb::SBDebugger::StateAsCString(state
) << ')'
289 *g_vsc
.log
<< "error: SendThreadStoppedEvent() invalid process"
292 g_vsc
.RunStopCommands();
297 // { "$ref": "#/definitions/Event" },
300 // "description": "Event message for 'process' event type. The event
301 // indicates that the debugger has begun debugging a
302 // new process. Either one that it has launched, or one
303 // that it has attached to.",
307 // "enum": [ "process" ]
314 // "description": "The logical name of the process. This is
315 // usually the full path to process's executable
316 // file. Example: /home/myproj/program.js."
318 // "systemProcessId": {
319 // "type": "integer",
320 // "description": "The system process id of the debugged process.
321 // This property will be missing for non-system
324 // "isLocalProcess": {
325 // "type": "boolean",
326 // "description": "If true, the process is running on the same
327 // computer as the debug adapter."
331 // "enum": [ "launch", "attach", "attachForSuspendedLaunch" ],
332 // "description": "Describes how the debug engine started
333 // debugging this process.",
334 // "enumDescriptions": [
335 // "Process was launched under the debugger.",
336 // "Debugger attached to an existing process.",
337 // "A project launcher component has launched a new process in
338 // a suspended state and then asked the debugger to attach."
342 // "required": [ "name" ]
345 // "required": [ "event", "body" ]
349 void SendProcessEvent(LaunchMethod launch_method
) {
350 lldb::SBFileSpec exe_fspec
= g_vsc
.target
.GetExecutable();
351 char exe_path
[PATH_MAX
];
352 exe_fspec
.GetPath(exe_path
, sizeof(exe_path
));
353 llvm::json::Object
event(CreateEventObject("process"));
354 llvm::json::Object body
;
355 EmplaceSafeString(body
, "name", std::string(exe_path
));
356 const auto pid
= g_vsc
.target
.GetProcess().GetProcessID();
357 body
.try_emplace("systemProcessId", (int64_t)pid
);
358 body
.try_emplace("isLocalProcess", true);
359 const char *startMethod
= nullptr;
360 switch (launch_method
) {
362 startMethod
= "launch";
365 startMethod
= "attach";
367 case AttachForSuspendedLaunch
:
368 startMethod
= "attachForSuspendedLaunch";
371 body
.try_emplace("startMethod", startMethod
);
372 event
.try_emplace("body", std::move(body
));
373 g_vsc
.SendJSON(llvm::json::Value(std::move(event
)));
376 // Grab any STDOUT and STDERR from the process and send it up to VS Code
377 // via an "output" event to the "stdout" and "stderr" categories.
378 void SendStdOutStdErr(lldb::SBProcess
&process
) {
381 while ((count
= process
.GetSTDOUT(buffer
, sizeof(buffer
))) > 0)
382 g_vsc
.SendOutput(OutputType::Stdout
, llvm::StringRef(buffer
, count
));
383 while ((count
= process
.GetSTDERR(buffer
, sizeof(buffer
))) > 0)
384 g_vsc
.SendOutput(OutputType::Stderr
, llvm::StringRef(buffer
, count
));
387 void ProgressEventThreadFunction() {
388 lldb::SBListener
listener("lldb-vscode.progress.listener");
389 g_vsc
.debugger
.GetBroadcaster().AddListener(
390 listener
, lldb::SBDebugger::eBroadcastBitProgress
);
391 g_vsc
.broadcaster
.AddListener(listener
, eBroadcastBitStopProgressThread
);
395 if (listener
.WaitForEvent(1, event
)) {
396 const auto event_mask
= event
.GetType();
397 if (event
.BroadcasterMatchesRef(g_vsc
.broadcaster
)) {
398 if (event_mask
& eBroadcastBitStopProgressThread
) {
402 uint64_t progress_id
= 0;
403 uint64_t completed
= 0;
405 bool is_debugger_specific
= false;
406 const char *message
= lldb::SBDebugger::GetProgressFromEvent(
407 event
, progress_id
, completed
, total
, is_debugger_specific
);
409 g_vsc
.SendProgressEvent(progress_id
, message
, completed
, total
);
415 // All events from the debugger, target, process, thread and frames are
416 // received in this function that runs in its own thread. We are using a
417 // "FILE *" to output packets back to VS Code and they have mutexes in them
418 // them prevent multiple threads from writing simultaneously so no locking
420 void EventThreadFunction() {
422 lldb::SBListener listener
= g_vsc
.debugger
.GetListener();
425 if (listener
.WaitForEvent(1, event
)) {
426 const auto event_mask
= event
.GetType();
427 if (lldb::SBProcess::EventIsProcessEvent(event
)) {
428 lldb::SBProcess process
= lldb::SBProcess::GetProcessFromEvent(event
);
429 if (event_mask
& lldb::SBProcess::eBroadcastBitStateChanged
) {
430 auto state
= lldb::SBProcess::GetStateFromEvent(event
);
432 case lldb::eStateInvalid
:
435 case lldb::eStateUnloaded
:
437 case lldb::eStateConnected
:
439 case lldb::eStateAttaching
:
441 case lldb::eStateLaunching
:
443 case lldb::eStateStepping
:
445 case lldb::eStateCrashed
:
447 case lldb::eStateDetached
:
449 case lldb::eStateSuspended
:
451 case lldb::eStateStopped
:
452 // Only report a stopped event if the process was not restarted.
453 if (!lldb::SBProcess::GetRestartedFromEvent(event
)) {
454 SendStdOutStdErr(process
);
455 SendThreadStoppedEvent();
458 case lldb::eStateRunning
:
459 g_vsc
.WillContinue();
461 case lldb::eStateExited
: {
462 // Run any exit LLDB commands the user specified in the
464 g_vsc
.RunExitCommands();
465 SendProcessExitedEvent(process
);
466 SendTerminatedEvent();
470 } else if ((event_mask
& lldb::SBProcess::eBroadcastBitSTDOUT
) ||
471 (event_mask
& lldb::SBProcess::eBroadcastBitSTDERR
)) {
472 SendStdOutStdErr(process
);
474 } else if (lldb::SBBreakpoint::EventIsBreakpointEvent(event
)) {
475 if (event_mask
& lldb::SBTarget::eBroadcastBitBreakpointChanged
) {
477 lldb::SBBreakpoint::GetBreakpointEventTypeFromEvent(event
);
478 auto bp
= lldb::SBBreakpoint::GetBreakpointFromEvent(event
);
479 // If the breakpoint was originated from the IDE, it will have the
480 // BreakpointBase::GetBreakpointLabel() label attached. Regardless
481 // of wether the locations were added or removed, the breakpoint
482 // ins't going away, so we the reason is always "changed".
483 if ((event_type
& lldb::eBreakpointEventTypeLocationsAdded
||
484 event_type
& lldb::eBreakpointEventTypeLocationsRemoved
) &&
485 bp
.MatchesName(BreakpointBase::GetBreakpointLabel())) {
486 auto bp_event
= CreateEventObject("breakpoint");
487 llvm::json::Object body
;
488 // As VSCode already knows the path of this breakpoint, we don't
489 // need to send it back as part of a "changed" event. This
490 // prevent us from sending to VSCode paths that should be source
491 // mapped. Note that CreateBreakpoint doesn't apply source mapping.
492 // Besides, the current implementation of VSCode ignores the
493 // "source" element of breakpoint events.
494 llvm::json::Value source_bp
= CreateBreakpoint(bp
);
495 source_bp
.getAsObject()->erase("source");
497 body
.try_emplace("breakpoint", source_bp
);
498 body
.try_emplace("reason", "changed");
499 bp_event
.try_emplace("body", std::move(body
));
500 g_vsc
.SendJSON(llvm::json::Value(std::move(bp_event
)));
503 } else if (event
.BroadcasterMatchesRef(g_vsc
.broadcaster
)) {
504 if (event_mask
& eBroadcastBitStopEventThread
) {
512 // Both attach and launch take a either a sourcePath or sourceMap
513 // argument (or neither), from which we need to set the target.source-map.
514 void SetSourceMapFromArguments(const llvm::json::Object
&arguments
) {
515 const char *sourceMapHelp
=
516 "source must be be an array of two-element arrays, "
517 "each containing a source and replacement path string.\n";
519 std::string sourceMapCommand
;
520 llvm::raw_string_ostream
strm(sourceMapCommand
);
521 strm
<< "settings set target.source-map ";
522 auto sourcePath
= GetString(arguments
, "sourcePath");
524 // sourceMap is the new, more general form of sourcePath and overrides it.
525 auto sourceMap
= arguments
.getArray("sourceMap");
527 for (const auto &value
: *sourceMap
) {
528 auto mapping
= value
.getAsArray();
529 if (mapping
== nullptr || mapping
->size() != 2 ||
530 (*mapping
)[0].kind() != llvm::json::Value::String
||
531 (*mapping
)[1].kind() != llvm::json::Value::String
) {
532 g_vsc
.SendOutput(OutputType::Console
, llvm::StringRef(sourceMapHelp
));
535 auto mapFrom
= GetAsString((*mapping
)[0]);
536 auto mapTo
= GetAsString((*mapping
)[1]);
537 strm
<< "\"" << mapFrom
<< "\" \"" << mapTo
<< "\" ";
540 if (ObjectContainsKey(arguments
, "sourceMap")) {
541 g_vsc
.SendOutput(OutputType::Console
, llvm::StringRef(sourceMapHelp
));
544 if (sourcePath
.empty())
546 // Do any source remapping needed before we create our targets
547 strm
<< "\".\" \"" << sourcePath
<< "\"";
550 if (!sourceMapCommand
.empty()) {
551 g_vsc
.RunLLDBCommands("Setting source map:", {sourceMapCommand
});
555 // "AttachRequest": {
556 // "allOf": [ { "$ref": "#/definitions/Request" }, {
558 // "description": "Attach request; value of command field is 'attach'.",
562 // "enum": [ "attach" ]
565 // "$ref": "#/definitions/AttachRequestArguments"
568 // "required": [ "command", "arguments" ]
571 // "AttachRequestArguments": {
573 // "description": "Arguments for 'attach' request.\nThe attach request has no
574 // standardized attributes."
576 // "AttachResponse": {
577 // "allOf": [ { "$ref": "#/definitions/Response" }, {
579 // "description": "Response to 'attach' request. This is just an
580 // acknowledgement, so no body field is required."
583 void request_attach(const llvm::json::Object
&request
) {
584 g_vsc
.is_attach
= true;
585 llvm::json::Object response
;
587 FillResponse(request
, response
);
588 lldb::SBAttachInfo attach_info
;
589 auto arguments
= request
.getObject("arguments");
590 const lldb::pid_t pid
=
591 GetUnsigned(arguments
, "pid", LLDB_INVALID_PROCESS_ID
);
592 if (pid
!= LLDB_INVALID_PROCESS_ID
)
593 attach_info
.SetProcessID(pid
);
594 const auto wait_for
= GetBoolean(arguments
, "waitFor", false);
595 attach_info
.SetWaitForLaunch(wait_for
, false /*async*/);
596 g_vsc
.init_commands
= GetStrings(arguments
, "initCommands");
597 g_vsc
.pre_run_commands
= GetStrings(arguments
, "preRunCommands");
598 g_vsc
.stop_commands
= GetStrings(arguments
, "stopCommands");
599 g_vsc
.exit_commands
= GetStrings(arguments
, "exitCommands");
600 g_vsc
.terminate_commands
= GetStrings(arguments
, "terminateCommands");
601 auto attachCommands
= GetStrings(arguments
, "attachCommands");
602 llvm::StringRef core_file
= GetString(arguments
, "coreFile");
603 g_vsc
.stop_at_entry
=
604 core_file
.empty() ? GetBoolean(arguments
, "stopOnEntry", false) : true;
605 std::vector
<std::string
> postRunCommands
=
606 GetStrings(arguments
, "postRunCommands");
607 const llvm::StringRef debuggerRoot
= GetString(arguments
, "debuggerRoot");
609 // This is a hack for loading DWARF in .o files on Mac where the .o files
610 // in the debug map of the main executable have relative paths which require
611 // the lldb-vscode binary to have its working directory set to that relative
612 // root for the .o files in order to be able to load debug info.
613 if (!debuggerRoot
.empty())
614 llvm::sys::fs::set_current_path(debuggerRoot
);
616 // Run any initialize LLDB commands the user specified in the launch.json
617 g_vsc
.RunInitCommands();
619 SetSourceMapFromArguments(*arguments
);
621 lldb::SBError status
;
622 g_vsc
.SetTarget(g_vsc
.CreateTargetFromArguments(*arguments
, status
));
624 response
["success"] = llvm::json::Value(false);
625 EmplaceSafeString(response
, "message", status
.GetCString());
626 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
630 // Run any pre run LLDB commands the user specified in the launch.json
631 g_vsc
.RunPreRunCommands();
633 if (pid
== LLDB_INVALID_PROCESS_ID
&& wait_for
) {
634 char attach_msg
[256];
635 auto attach_msg_len
= snprintf(attach_msg
, sizeof(attach_msg
),
636 "Waiting to attach to \"%s\"...",
637 g_vsc
.target
.GetExecutable().GetFilename());
638 g_vsc
.SendOutput(OutputType::Console
,
639 llvm::StringRef(attach_msg
, attach_msg_len
));
641 if (attachCommands
.empty()) {
642 // No "attachCommands", just attach normally.
643 // Disable async events so the attach will be successful when we return from
644 // the launch call and the launch will happen synchronously
645 g_vsc
.debugger
.SetAsync(false);
646 if (core_file
.empty())
647 g_vsc
.target
.Attach(attach_info
, error
);
649 g_vsc
.target
.LoadCore(core_file
.data(), error
);
650 // Reenable async events
651 g_vsc
.debugger
.SetAsync(true);
653 // We have "attachCommands" that are a set of commands that are expected
654 // to execute the commands after which a process should be created. If there
655 // is no valid process after running these commands, we have failed.
656 g_vsc
.RunLLDBCommands("Running attachCommands:", attachCommands
);
657 // The custom commands might have created a new target so we should use the
658 // selected target after these commands are run.
659 g_vsc
.target
= g_vsc
.debugger
.GetSelectedTarget();
662 if (error
.Success() && core_file
.empty()) {
663 auto attached_pid
= g_vsc
.target
.GetProcess().GetProcessID();
664 if (attached_pid
== LLDB_INVALID_PROCESS_ID
) {
665 if (attachCommands
.empty())
666 error
.SetErrorString("failed to attach to a process");
668 error
.SetErrorString("attachCommands failed to attach to a process");
673 response
["success"] = llvm::json::Value(false);
674 EmplaceSafeString(response
, "message", std::string(error
.GetCString()));
676 g_vsc
.RunLLDBCommands("Running postRunCommands:", postRunCommands
);
679 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
680 if (error
.Success()) {
681 SendProcessEvent(Attach
);
682 g_vsc
.SendJSON(CreateEventObject("initialized"));
686 // "ContinueRequest": {
687 // "allOf": [ { "$ref": "#/definitions/Request" }, {
689 // "description": "Continue request; value of command field is 'continue'.
690 // The request starts the debuggee to run again.",
694 // "enum": [ "continue" ]
697 // "$ref": "#/definitions/ContinueArguments"
700 // "required": [ "command", "arguments" ]
703 // "ContinueArguments": {
705 // "description": "Arguments for 'continue' request.",
708 // "type": "integer",
709 // "description": "Continue execution for the specified thread (if
710 // possible). If the backend cannot continue on a single
711 // thread but will continue on all threads, it should
712 // set the allThreadsContinued attribute in the response
716 // "required": [ "threadId" ]
718 // "ContinueResponse": {
719 // "allOf": [ { "$ref": "#/definitions/Response" }, {
721 // "description": "Response to 'continue' request.",
726 // "allThreadsContinued": {
727 // "type": "boolean",
728 // "description": "If true, the continue request has ignored the
729 // specified thread and continued all threads
730 // instead. If this attribute is missing a value
731 // of 'true' is assumed for backward
737 // "required": [ "body" ]
740 void request_continue(const llvm::json::Object
&request
) {
741 llvm::json::Object response
;
742 FillResponse(request
, response
);
743 lldb::SBProcess process
= g_vsc
.target
.GetProcess();
744 auto arguments
= request
.getObject("arguments");
745 // Remember the thread ID that caused the resume so we can set the
746 // "threadCausedFocus" boolean value in the "stopped" events.
747 g_vsc
.focus_tid
= GetUnsigned(arguments
, "threadId", LLDB_INVALID_THREAD_ID
);
748 lldb::SBError error
= process
.Continue();
749 llvm::json::Object body
;
750 body
.try_emplace("allThreadsContinued", true);
751 response
.try_emplace("body", std::move(body
));
752 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
755 // "ConfigurationDoneRequest": {
756 // "allOf": [ { "$ref": "#/definitions/Request" }, {
758 // "description": "ConfigurationDone request; value of command field
759 // is 'configurationDone'.\nThe client of the debug protocol must
760 // send this request at the end of the sequence of configuration
761 // requests (which was started by the InitializedEvent).",
765 // "enum": [ "configurationDone" ]
768 // "$ref": "#/definitions/ConfigurationDoneArguments"
771 // "required": [ "command" ]
774 // "ConfigurationDoneArguments": {
776 // "description": "Arguments for 'configurationDone' request.\nThe
777 // configurationDone request has no standardized attributes."
779 // "ConfigurationDoneResponse": {
780 // "allOf": [ { "$ref": "#/definitions/Response" }, {
782 // "description": "Response to 'configurationDone' request. This is
783 // just an acknowledgement, so no body field is required."
786 void request_configurationDone(const llvm::json::Object
&request
) {
787 llvm::json::Object response
;
788 FillResponse(request
, response
);
789 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
790 if (g_vsc
.stop_at_entry
)
791 SendThreadStoppedEvent();
793 g_vsc
.target
.GetProcess().Continue();
796 // "DisconnectRequest": {
797 // "allOf": [ { "$ref": "#/definitions/Request" }, {
799 // "description": "Disconnect request; value of command field is
804 // "enum": [ "disconnect" ]
807 // "$ref": "#/definitions/DisconnectArguments"
810 // "required": [ "command" ]
813 // "DisconnectArguments": {
815 // "description": "Arguments for 'disconnect' request.",
817 // "terminateDebuggee": {
818 // "type": "boolean",
819 // "description": "Indicates whether the debuggee should be terminated
820 // when the debugger is disconnected. If unspecified,
821 // the debug adapter is free to do whatever it thinks
822 // is best. A client can only rely on this attribute
823 // being properly honored if a debug adapter returns
824 // true for the 'supportTerminateDebuggee' capability."
827 // "type": "boolean",
828 // "description": "Indicates whether the debuggee should be restart
833 // "DisconnectResponse": {
834 // "allOf": [ { "$ref": "#/definitions/Response" }, {
836 // "description": "Response to 'disconnect' request. This is just an
837 // acknowledgement, so no body field is required."
840 void request_disconnect(const llvm::json::Object
&request
) {
841 llvm::json::Object response
;
842 FillResponse(request
, response
);
843 auto arguments
= request
.getObject("arguments");
845 bool defaultTerminateDebuggee
= g_vsc
.is_attach
? false : true;
846 bool terminateDebuggee
=
847 GetBoolean(arguments
, "terminateDebuggee", defaultTerminateDebuggee
);
848 lldb::SBProcess process
= g_vsc
.target
.GetProcess();
849 auto state
= process
.GetState();
851 case lldb::eStateInvalid
:
852 case lldb::eStateUnloaded
:
853 case lldb::eStateDetached
:
854 case lldb::eStateExited
:
856 case lldb::eStateConnected
:
857 case lldb::eStateAttaching
:
858 case lldb::eStateLaunching
:
859 case lldb::eStateStepping
:
860 case lldb::eStateCrashed
:
861 case lldb::eStateSuspended
:
862 case lldb::eStateStopped
:
863 case lldb::eStateRunning
:
864 g_vsc
.debugger
.SetAsync(false);
865 lldb::SBError error
= terminateDebuggee
? process
.Kill() : process
.Detach();
866 if (!error
.Success())
867 response
.try_emplace("error", error
.GetCString());
868 g_vsc
.debugger
.SetAsync(true);
871 SendTerminatedEvent();
872 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
873 if (g_vsc
.event_thread
.joinable()) {
874 g_vsc
.broadcaster
.BroadcastEventByType(eBroadcastBitStopEventThread
);
875 g_vsc
.event_thread
.join();
877 if (g_vsc
.progress_event_thread
.joinable()) {
878 g_vsc
.broadcaster
.BroadcastEventByType(eBroadcastBitStopProgressThread
);
879 g_vsc
.progress_event_thread
.join();
883 void request_exceptionInfo(const llvm::json::Object
&request
) {
884 llvm::json::Object response
;
885 FillResponse(request
, response
);
886 auto arguments
= request
.getObject("arguments");
887 llvm::json::Object body
;
888 lldb::SBThread thread
= g_vsc
.GetLLDBThread(*arguments
);
889 if (thread
.IsValid()) {
890 auto stopReason
= thread
.GetStopReason();
891 if (stopReason
== lldb::eStopReasonSignal
)
892 body
.try_emplace("exceptionId", "signal");
893 else if (stopReason
== lldb::eStopReasonBreakpoint
) {
894 ExceptionBreakpoint
*exc_bp
= g_vsc
.GetExceptionBPFromStopReason(thread
);
896 EmplaceSafeString(body
, "exceptionId", exc_bp
->filter
);
897 EmplaceSafeString(body
, "description", exc_bp
->label
);
899 body
.try_emplace("exceptionId", "exception");
902 body
.try_emplace("exceptionId", "exception");
904 if (!ObjectContainsKey(body
, "description")) {
905 char description
[1024];
906 if (thread
.GetStopDescription(description
, sizeof(description
))) {
907 EmplaceSafeString(body
, "description", std::string(description
));
910 body
.try_emplace("breakMode", "always");
911 // auto excInfoCount = thread.GetStopReasonDataCount();
912 // for (auto i=0; i<excInfoCount; ++i) {
913 // uint64_t exc_data = thread.GetStopReasonDataAtIndex(i);
916 response
["success"] = llvm::json::Value(false);
918 response
.try_emplace("body", std::move(body
));
919 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
922 // "CompletionsRequest": {
923 // "allOf": [ { "$ref": "#/definitions/Request" }, {
925 // "description": "Returns a list of possible completions for a given caret
926 // position and text.\nThe CompletionsRequest may only be called if the
927 // 'supportsCompletionsRequest' capability exists and is true.",
931 // "enum": [ "completions" ]
934 // "$ref": "#/definitions/CompletionsArguments"
937 // "required": [ "command", "arguments" ]
940 // "CompletionsArguments": {
942 // "description": "Arguments for 'completions' request.",
945 // "type": "integer",
946 // "description": "Returns completions in the scope of this stack frame.
947 // If not specified, the completions are returned for the global scope."
951 // "description": "One or more source lines. Typically this is the text a
952 // user has typed into the debug console before he asked for completion."
955 // "type": "integer",
956 // "description": "The character position for which to determine the
957 // completion proposals."
960 // "type": "integer",
961 // "description": "An optional line for which to determine the completion
962 // proposals. If missing the first line of the text is assumed."
965 // "required": [ "text", "column" ]
967 // "CompletionsResponse": {
968 // "allOf": [ { "$ref": "#/definitions/Response" }, {
970 // "description": "Response to 'completions' request.",
978 // "$ref": "#/definitions/CompletionItem"
980 // "description": "The possible completions for ."
983 // "required": [ "targets" ]
986 // "required": [ "body" ]
989 // "CompletionItem": {
991 // "description": "CompletionItems are the suggestions returned from the
992 // CompletionsRequest.", "properties": {
995 // "description": "The label of this completion item. By default this is
996 // also the text that is inserted when selecting this completion."
1000 // "description": "If text is not falsy then it is inserted instead of the
1004 // "type": "string",
1005 // "description": "A string that should be used when comparing this item
1006 // with other items. When `falsy` the label is used."
1009 // "$ref": "#/definitions/CompletionItemType",
1010 // "description": "The item's type. Typically the client uses this
1011 // information to render the item in the UI with an icon."
1014 // "type": "integer",
1015 // "description": "This value determines the location (in the
1016 // CompletionsRequest's 'text' attribute) where the completion text is
1017 // added.\nIf missing the text is added at the location specified by the
1018 // CompletionsRequest's 'column' attribute."
1021 // "type": "integer",
1022 // "description": "This value determines how many characters are
1023 // overwritten by the completion text.\nIf missing the value 0 is assumed
1024 // which results in the completion text being inserted."
1027 // "required": [ "label" ]
1029 // "CompletionItemType": {
1030 // "type": "string",
1031 // "description": "Some predefined types for the CompletionItem. Please note
1032 // that not all clients have specific icons for all of them.", "enum": [
1033 // "method", "function", "constructor", "field", "variable", "class",
1034 // "interface", "module", "property", "unit", "value", "enum", "keyword",
1035 // "snippet", "text", "color", "file", "reference", "customcolor" ]
1037 void request_completions(const llvm::json::Object
&request
) {
1038 llvm::json::Object response
;
1039 FillResponse(request
, response
);
1040 llvm::json::Object body
;
1041 auto arguments
= request
.getObject("arguments");
1042 std::string text
= std::string(GetString(arguments
, "text"));
1043 auto original_column
= GetSigned(arguments
, "column", text
.size());
1044 auto actual_column
= original_column
- 1;
1045 llvm::json::Array targets
;
1046 // NOTE: the 'line' argument is not needed, as multiline expressions
1047 // work well already
1048 // TODO: support frameID. Currently
1049 // g_vsc.debugger.GetCommandInterpreter().HandleCompletionWithDescriptions
1050 // is frame-unaware.
1052 if (!text
.empty() && text
[0] == '`') {
1053 text
= text
.substr(1);
1059 lldb::SBStringList matches
;
1060 lldb::SBStringList descriptions
;
1061 g_vsc
.debugger
.GetCommandInterpreter().HandleCompletionWithDescriptions(
1062 text
.c_str(), actual_column
, 0, -1, matches
, descriptions
);
1063 size_t count
= std::min((uint32_t)100, matches
.GetSize());
1064 targets
.reserve(count
);
1065 for (size_t i
= 0; i
< count
; i
++) {
1066 std::string match
= matches
.GetStringAtIndex(i
);
1067 std::string description
= descriptions
.GetStringAtIndex(i
);
1069 llvm::json::Object item
;
1071 llvm::StringRef match_ref
= match
;
1072 for (llvm::StringRef commit_point
: {".", "->"}) {
1073 if (match_ref
.contains(commit_point
)) {
1074 match_ref
= match_ref
.rsplit(commit_point
).second
;
1077 EmplaceSafeString(item
, "text", match_ref
);
1079 if (description
.empty())
1080 EmplaceSafeString(item
, "label", match
);
1082 EmplaceSafeString(item
, "label", match
+ " -- " + description
);
1084 targets
.emplace_back(std::move(item
));
1087 body
.try_emplace("targets", std::move(targets
));
1088 response
.try_emplace("body", std::move(body
));
1089 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
1092 // "EvaluateRequest": {
1093 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1094 // "type": "object",
1095 // "description": "Evaluate request; value of command field is 'evaluate'.
1096 // Evaluates the given expression in the context of the
1097 // top most stack frame. The expression has access to any
1098 // variables and arguments that are in scope.",
1101 // "type": "string",
1102 // "enum": [ "evaluate" ]
1105 // "$ref": "#/definitions/EvaluateArguments"
1108 // "required": [ "command", "arguments" ]
1111 // "EvaluateArguments": {
1112 // "type": "object",
1113 // "description": "Arguments for 'evaluate' request.",
1116 // "type": "string",
1117 // "description": "The expression to evaluate."
1120 // "type": "integer",
1121 // "description": "Evaluate the expression in the scope of this stack
1122 // frame. If not specified, the expression is evaluated
1123 // in the global scope."
1126 // "type": "string",
1127 // "_enum": [ "watch", "repl", "hover" ],
1128 // "enumDescriptions": [
1129 // "evaluate is run in a watch.",
1130 // "evaluate is run from REPL console.",
1131 // "evaluate is run from a data hover."
1133 // "description": "The context in which the evaluate request is run."
1136 // "$ref": "#/definitions/ValueFormat",
1137 // "description": "Specifies details on how to format the Evaluate
1141 // "required": [ "expression" ]
1143 // "EvaluateResponse": {
1144 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1145 // "type": "object",
1146 // "description": "Response to 'evaluate' request.",
1149 // "type": "object",
1152 // "type": "string",
1153 // "description": "The result of the evaluate request."
1156 // "type": "string",
1157 // "description": "The optional type of the evaluate result."
1159 // "presentationHint": {
1160 // "$ref": "#/definitions/VariablePresentationHint",
1161 // "description": "Properties of a evaluate result that can be
1162 // used to determine how to render the result in
1165 // "variablesReference": {
1166 // "type": "number",
1167 // "description": "If variablesReference is > 0, the evaluate
1168 // result is structured and its children can be
1169 // retrieved by passing variablesReference to the
1170 // VariablesRequest."
1172 // "namedVariables": {
1173 // "type": "number",
1174 // "description": "The number of named child variables. The
1175 // client can use this optional information to
1176 // present the variables in a paged UI and fetch
1179 // "indexedVariables": {
1180 // "type": "number",
1181 // "description": "The number of indexed child variables. The
1182 // client can use this optional information to
1183 // present the variables in a paged UI and fetch
1187 // "required": [ "result", "variablesReference" ]
1190 // "required": [ "body" ]
1193 void request_evaluate(const llvm::json::Object
&request
) {
1194 llvm::json::Object response
;
1195 FillResponse(request
, response
);
1196 llvm::json::Object body
;
1197 auto arguments
= request
.getObject("arguments");
1198 lldb::SBFrame frame
= g_vsc
.GetLLDBFrame(*arguments
);
1199 const auto expression
= GetString(arguments
, "expression");
1200 llvm::StringRef context
= GetString(arguments
, "context");
1202 if (!expression
.empty() && expression
[0] == '`') {
1204 RunLLDBCommands(llvm::StringRef(), {std::string(expression
.substr(1))});
1205 EmplaceSafeString(body
, "result", result
);
1206 body
.try_emplace("variablesReference", (int64_t)0);
1208 // Always try to get the answer from the local variables if possible. If
1209 // this fails, then if the context is not "hover", actually evaluate an
1210 // expression using the expression parser.
1212 // "frame variable" is more reliable than the expression parser in
1213 // many cases and it is faster.
1214 lldb::SBValue value
= frame
.GetValueForVariablePath(
1215 expression
.data(), lldb::eDynamicDontRunTarget
);
1217 // Freeze dry the value in case users expand it later in the debug console
1218 if (value
.GetError().Success() && context
== "repl")
1219 value
= value
.Persist();
1221 if (value
.GetError().Fail() && context
!= "hover")
1222 value
= frame
.EvaluateExpression(expression
.data());
1224 if (value
.GetError().Fail()) {
1225 response
["success"] = llvm::json::Value(false);
1226 // This error object must live until we're done with the pointer returned
1228 lldb::SBError error
= value
.GetError();
1229 const char *error_cstr
= error
.GetCString();
1230 if (error_cstr
&& error_cstr
[0])
1231 EmplaceSafeString(response
, "message", std::string(error_cstr
));
1233 EmplaceSafeString(response
, "message", "evaluate failed");
1235 SetValueForKey(value
, body
, "result");
1236 auto value_typename
= value
.GetType().GetDisplayTypeName();
1237 EmplaceSafeString(body
, "type",
1238 value_typename
? value_typename
: NO_TYPENAME
);
1239 if (value
.MightHaveChildren()) {
1240 auto variableReference
= g_vsc
.variables
.InsertExpandableVariable(
1241 value
, /*is_permanent=*/context
== "repl");
1242 body
.try_emplace("variablesReference", variableReference
);
1244 body
.try_emplace("variablesReference", (int64_t)0);
1248 response
.try_emplace("body", std::move(body
));
1249 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
1252 // "compileUnitsRequest": {
1253 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1254 // "type": "object",
1255 // "description": "Compile Unit request; value of command field is
1256 // 'compileUnits'.",
1259 // "type": "string",
1260 // "enum": [ "compileUnits" ]
1263 // "$ref": "#/definitions/compileUnitRequestArguments"
1266 // "required": [ "command", "arguments" ]
1269 // "compileUnitsRequestArguments": {
1270 // "type": "object",
1271 // "description": "Arguments for 'compileUnits' request.",
1274 // "type": "string",
1275 // "description": "The ID of the module."
1278 // "required": [ "moduleId" ]
1280 // "compileUnitsResponse": {
1281 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1282 // "type": "object",
1283 // "description": "Response to 'compileUnits' request.",
1286 // "description": "Response to 'compileUnits' request. Array of
1287 // paths of compile units."
1292 void request_compileUnits(const llvm::json::Object
&request
) {
1293 llvm::json::Object response
;
1294 FillResponse(request
, response
);
1295 llvm::json::Object body
;
1296 llvm::json::Array units
;
1297 auto arguments
= request
.getObject("arguments");
1298 std::string module_id
= std::string(GetString(arguments
, "moduleId"));
1299 int num_modules
= g_vsc
.target
.GetNumModules();
1300 for (int i
= 0; i
< num_modules
; i
++) {
1301 auto curr_module
= g_vsc
.target
.GetModuleAtIndex(i
);
1302 if (module_id
== curr_module
.GetUUIDString()) {
1303 int num_units
= curr_module
.GetNumCompileUnits();
1304 for (int j
= 0; j
< num_units
; j
++) {
1305 auto curr_unit
= curr_module
.GetCompileUnitAtIndex(j
);
1306 units
.emplace_back(CreateCompileUnit(curr_unit
));
1308 body
.try_emplace("compileUnits", std::move(units
));
1312 response
.try_emplace("body", std::move(body
));
1313 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
1316 // "modulesRequest": {
1317 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1318 // "type": "object",
1319 // "description": "Modules request; value of command field is
1323 // "type": "string",
1324 // "enum": [ "modules" ]
1327 // "required": [ "command" ]
1330 // "modulesResponse": {
1331 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1332 // "type": "object",
1333 // "description": "Response to 'modules' request.",
1336 // "description": "Response to 'modules' request. Array of
1342 void request_modules(const llvm::json::Object
&request
) {
1343 llvm::json::Object response
;
1344 FillResponse(request
, response
);
1346 llvm::json::Array modules
;
1347 for (size_t i
= 0; i
< g_vsc
.target
.GetNumModules(); i
++) {
1348 lldb::SBModule module
= g_vsc
.target
.GetModuleAtIndex(i
);
1349 modules
.emplace_back(CreateModule(module
));
1352 llvm::json::Object body
;
1353 body
.try_emplace("modules", std::move(modules
));
1354 response
.try_emplace("body", std::move(body
));
1355 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
1358 // "InitializeRequest": {
1359 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1360 // "type": "object",
1361 // "description": "Initialize request; value of command field is
1365 // "type": "string",
1366 // "enum": [ "initialize" ]
1369 // "$ref": "#/definitions/InitializeRequestArguments"
1372 // "required": [ "command", "arguments" ]
1375 // "InitializeRequestArguments": {
1376 // "type": "object",
1377 // "description": "Arguments for 'initialize' request.",
1380 // "type": "string",
1381 // "description": "The ID of the (frontend) client using this adapter."
1384 // "type": "string",
1385 // "description": "The ID of the debug adapter."
1388 // "type": "string",
1389 // "description": "The ISO-639 locale of the (frontend) client using
1390 // this adapter, e.g. en-US or de-CH."
1392 // "linesStartAt1": {
1393 // "type": "boolean",
1394 // "description": "If true all line numbers are 1-based (default)."
1396 // "columnsStartAt1": {
1397 // "type": "boolean",
1398 // "description": "If true all column numbers are 1-based (default)."
1401 // "type": "string",
1402 // "_enum": [ "path", "uri" ],
1403 // "description": "Determines in what format paths are specified. The
1404 // default is 'path', which is the native format."
1406 // "supportsVariableType": {
1407 // "type": "boolean",
1408 // "description": "Client supports the optional type attribute for
1411 // "supportsVariablePaging": {
1412 // "type": "boolean",
1413 // "description": "Client supports the paging of variables."
1415 // "supportsRunInTerminalRequest": {
1416 // "type": "boolean",
1417 // "description": "Client supports the runInTerminal request."
1420 // "required": [ "adapterID" ]
1422 // "InitializeResponse": {
1423 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1424 // "type": "object",
1425 // "description": "Response to 'initialize' request.",
1428 // "$ref": "#/definitions/Capabilities",
1429 // "description": "The capabilities of this debug adapter."
1434 void request_initialize(const llvm::json::Object
&request
) {
1435 g_vsc
.debugger
= lldb::SBDebugger::Create(true /*source_init_files*/);
1436 g_vsc
.progress_event_thread
= std::thread(ProgressEventThreadFunction
);
1438 // Create an empty target right away since we might get breakpoint requests
1439 // before we are given an executable to launch in a "launch" request, or a
1440 // executable when attaching to a process by process ID in a "attach"
1442 FILE *out
= llvm::sys::RetryAfterSignal(nullptr, fopen
, dev_null_path
, "w");
1444 // Set the output and error file handles to redirect into nothing otherwise
1445 // if any code in LLDB prints to the debugger file handles, the output and
1446 // error file handles are initialized to STDOUT and STDERR and any output
1447 // will kill our debug session.
1448 g_vsc
.debugger
.SetOutputFileHandle(out
, true);
1449 g_vsc
.debugger
.SetErrorFileHandle(out
, false);
1452 // Start our event thread so we can receive events from the debugger, target,
1453 // process and more.
1454 g_vsc
.event_thread
= std::thread(EventThreadFunction
);
1456 llvm::json::Object response
;
1457 FillResponse(request
, response
);
1458 llvm::json::Object body
;
1459 // The debug adapter supports the configurationDoneRequest.
1460 body
.try_emplace("supportsConfigurationDoneRequest", true);
1461 // The debug adapter supports function breakpoints.
1462 body
.try_emplace("supportsFunctionBreakpoints", true);
1463 // The debug adapter supports conditional breakpoints.
1464 body
.try_emplace("supportsConditionalBreakpoints", true);
1465 // The debug adapter supports breakpoints that break execution after a
1466 // specified number of hits.
1467 body
.try_emplace("supportsHitConditionalBreakpoints", true);
1468 // The debug adapter supports a (side effect free) evaluate request for
1470 body
.try_emplace("supportsEvaluateForHovers", true);
1471 // Available filters or options for the setExceptionBreakpoints request.
1472 llvm::json::Array filters
;
1473 for (const auto &exc_bp
: g_vsc
.exception_breakpoints
) {
1474 filters
.emplace_back(CreateExceptionBreakpointFilter(exc_bp
));
1476 body
.try_emplace("exceptionBreakpointFilters", std::move(filters
));
1477 // The debug adapter supports launching a debugee in intergrated VSCode
1479 body
.try_emplace("supportsRunInTerminalRequest", true);
1480 // The debug adapter supports stepping back via the stepBack and
1481 // reverseContinue requests.
1482 body
.try_emplace("supportsStepBack", false);
1483 // The debug adapter supports setting a variable to a value.
1484 body
.try_emplace("supportsSetVariable", true);
1485 // The debug adapter supports restarting a frame.
1486 body
.try_emplace("supportsRestartFrame", false);
1487 // The debug adapter supports the gotoTargetsRequest.
1488 body
.try_emplace("supportsGotoTargetsRequest", false);
1489 // The debug adapter supports the stepInTargetsRequest.
1490 body
.try_emplace("supportsStepInTargetsRequest", false);
1491 // We need to improve the current implementation of completions in order to
1492 // enable it again. For some context, this is how VSCode works:
1493 // - VSCode sends a completion request whenever chars are added, the user
1494 // triggers completion manually via CTRL-space or similar mechanisms, but
1495 // not when there's a deletion. Besides, VSCode doesn't let us know which
1496 // of these events we are handling. What is more, the use can paste or cut
1497 // sections of the text arbitrarily.
1498 // https://github.com/microsoft/vscode/issues/89531 tracks part of the
1499 // issue just mentioned.
1500 // This behavior causes many problems with the current way completion is
1501 // implemented in lldb-vscode, as these requests could be really expensive,
1502 // blocking the debugger, and there could be many concurrent requests unless
1503 // the user types very slowly... We need to address this specific issue, or
1504 // at least trigger completion only when the user explicitly wants it, which
1505 // is the behavior of LLDB CLI, that expects a TAB.
1506 body
.try_emplace("supportsCompletionsRequest", false);
1507 // The debug adapter supports the modules request.
1508 body
.try_emplace("supportsModulesRequest", true);
1509 // The set of additional module information exposed by the debug adapter.
1510 // body.try_emplace("additionalModuleColumns"] = ColumnDescriptor
1511 // Checksum algorithms supported by the debug adapter.
1512 // body.try_emplace("supportedChecksumAlgorithms"] = ChecksumAlgorithm
1513 // The debug adapter supports the RestartRequest. In this case a client
1514 // should not implement 'restart' by terminating and relaunching the adapter
1515 // but by calling the RestartRequest.
1516 body
.try_emplace("supportsRestartRequest", false);
1517 // The debug adapter supports 'exceptionOptions' on the
1518 // setExceptionBreakpoints request.
1519 body
.try_emplace("supportsExceptionOptions", true);
1520 // The debug adapter supports a 'format' attribute on the stackTraceRequest,
1521 // variablesRequest, and evaluateRequest.
1522 body
.try_emplace("supportsValueFormattingOptions", true);
1523 // The debug adapter supports the exceptionInfo request.
1524 body
.try_emplace("supportsExceptionInfoRequest", true);
1525 // The debug adapter supports the 'terminateDebuggee' attribute on the
1526 // 'disconnect' request.
1527 body
.try_emplace("supportTerminateDebuggee", true);
1528 // The debug adapter supports the delayed loading of parts of the stack,
1529 // which requires that both the 'startFrame' and 'levels' arguments and the
1530 // 'totalFrames' result of the 'StackTrace' request are supported.
1531 body
.try_emplace("supportsDelayedStackTraceLoading", true);
1532 // The debug adapter supports the 'loadedSources' request.
1533 body
.try_emplace("supportsLoadedSourcesRequest", false);
1534 // The debug adapter supports sending progress reporting events.
1535 body
.try_emplace("supportsProgressReporting", true);
1537 response
.try_emplace("body", std::move(body
));
1538 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
1541 llvm::Error
request_runInTerminal(const llvm::json::Object
&launch_request
) {
1542 g_vsc
.is_attach
= true;
1543 lldb::SBAttachInfo attach_info
;
1545 llvm::Expected
<std::shared_ptr
<FifoFile
>> comm_file_or_err
=
1546 CreateRunInTerminalCommFile();
1547 if (!comm_file_or_err
)
1548 return comm_file_or_err
.takeError();
1549 FifoFile
&comm_file
= *comm_file_or_err
.get();
1551 RunInTerminalDebugAdapterCommChannel
comm_channel(comm_file
.m_path
);
1553 llvm::json::Object reverse_request
= CreateRunInTerminalReverseRequest(
1554 launch_request
, g_vsc
.debug_adaptor_path
, comm_file
.m_path
);
1555 llvm::json::Object reverse_response
;
1556 lldb_vscode::PacketStatus status
=
1557 g_vsc
.SendReverseRequest(reverse_request
, reverse_response
);
1558 if (status
!= lldb_vscode::PacketStatus::Success
)
1559 return llvm::createStringError(llvm::inconvertibleErrorCode(),
1560 "Process cannot be launched by the IDE. %s",
1561 comm_channel
.GetLauncherError().c_str());
1563 if (llvm::Expected
<lldb::pid_t
> pid
= comm_channel
.GetLauncherPid())
1564 attach_info
.SetProcessID(*pid
);
1566 return pid
.takeError();
1568 g_vsc
.debugger
.SetAsync(false);
1569 lldb::SBError error
;
1570 g_vsc
.target
.Attach(attach_info
, error
);
1573 return llvm::createStringError(llvm::inconvertibleErrorCode(),
1574 "Failed to attach to the target process. %s",
1575 comm_channel
.GetLauncherError().c_str());
1576 // This will notify the runInTerminal launcher that we attached.
1577 // We have to make this async, as the function won't return until the launcher
1578 // resumes and reads the data.
1579 std::future
<lldb::SBError
> did_attach_message_success
=
1580 comm_channel
.NotifyDidAttach();
1582 // We just attached to the runInTerminal launcher, which was waiting to be
1583 // attached. We now resume it, so it can receive the didAttach notification
1584 // and then perform the exec. Upon continuing, the debugger will stop the
1585 // process right in the middle of the exec. To the user, what we are doing is
1586 // transparent, as they will only be able to see the process since the exec,
1587 // completely unaware of the preparatory work.
1588 g_vsc
.target
.GetProcess().Continue();
1590 // Now that the actual target is just starting (i.e. exec was just invoked),
1591 // we return the debugger to its async state.
1592 g_vsc
.debugger
.SetAsync(true);
1594 // If sending the notification failed, the launcher should be dead by now and
1595 // the async didAttach notification should have an error message, so we
1596 // return it. Otherwise, everything was a success.
1597 did_attach_message_success
.wait();
1598 error
= did_attach_message_success
.get();
1599 if (error
.Success())
1600 return llvm::Error::success();
1601 return llvm::createStringError(llvm::inconvertibleErrorCode(),
1602 error
.GetCString());
1605 // "LaunchRequest": {
1606 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1607 // "type": "object",
1608 // "description": "Launch request; value of command field is 'launch'.",
1611 // "type": "string",
1612 // "enum": [ "launch" ]
1615 // "$ref": "#/definitions/LaunchRequestArguments"
1618 // "required": [ "command", "arguments" ]
1621 // "LaunchRequestArguments": {
1622 // "type": "object",
1623 // "description": "Arguments for 'launch' request.",
1626 // "type": "boolean",
1627 // "description": "If noDebug is true the launch request should launch
1628 // the program without enabling debugging."
1632 // "LaunchResponse": {
1633 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1634 // "type": "object",
1635 // "description": "Response to 'launch' request. This is just an
1636 // acknowledgement, so no body field is required."
1639 void request_launch(const llvm::json::Object
&request
) {
1640 g_vsc
.is_attach
= false;
1641 llvm::json::Object response
;
1642 lldb::SBError error
;
1643 FillResponse(request
, response
);
1644 auto arguments
= request
.getObject("arguments");
1645 g_vsc
.init_commands
= GetStrings(arguments
, "initCommands");
1646 g_vsc
.pre_run_commands
= GetStrings(arguments
, "preRunCommands");
1647 g_vsc
.stop_commands
= GetStrings(arguments
, "stopCommands");
1648 g_vsc
.exit_commands
= GetStrings(arguments
, "exitCommands");
1649 g_vsc
.terminate_commands
= GetStrings(arguments
, "terminateCommands");
1650 auto launchCommands
= GetStrings(arguments
, "launchCommands");
1651 std::vector
<std::string
> postRunCommands
=
1652 GetStrings(arguments
, "postRunCommands");
1653 g_vsc
.stop_at_entry
= GetBoolean(arguments
, "stopOnEntry", false);
1654 const llvm::StringRef debuggerRoot
= GetString(arguments
, "debuggerRoot");
1656 // This is a hack for loading DWARF in .o files on Mac where the .o files
1657 // in the debug map of the main executable have relative paths which require
1658 // the lldb-vscode binary to have its working directory set to that relative
1659 // root for the .o files in order to be able to load debug info.
1660 if (!debuggerRoot
.empty())
1661 llvm::sys::fs::set_current_path(debuggerRoot
);
1663 // Run any initialize LLDB commands the user specified in the launch.json.
1664 // This is run before target is created, so commands can't do anything with
1665 // the targets - preRunCommands are run with the target.
1666 g_vsc
.RunInitCommands();
1668 SetSourceMapFromArguments(*arguments
);
1670 lldb::SBError status
;
1671 g_vsc
.SetTarget(g_vsc
.CreateTargetFromArguments(*arguments
, status
));
1672 if (status
.Fail()) {
1673 response
["success"] = llvm::json::Value(false);
1674 EmplaceSafeString(response
, "message", status
.GetCString());
1675 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
1679 // Instantiate a launch info instance for the target.
1680 auto launch_info
= g_vsc
.target
.GetLaunchInfo();
1682 // Grab the current working directory if there is one and set it in the
1684 const auto cwd
= GetString(arguments
, "cwd");
1686 launch_info
.SetWorkingDirectory(cwd
.data());
1688 // Extract any extra arguments and append them to our program arguments for
1690 auto args
= GetStrings(arguments
, "args");
1692 launch_info
.SetArguments(MakeArgv(args
).data(), true);
1694 // Pass any environment variables along that the user specified.
1695 auto envs
= GetStrings(arguments
, "env");
1697 launch_info
.SetEnvironmentEntries(MakeArgv(envs
).data(), true);
1699 auto flags
= launch_info
.GetLaunchFlags();
1701 if (GetBoolean(arguments
, "disableASLR", true))
1702 flags
|= lldb::eLaunchFlagDisableASLR
;
1703 if (GetBoolean(arguments
, "disableSTDIO", false))
1704 flags
|= lldb::eLaunchFlagDisableSTDIO
;
1705 if (GetBoolean(arguments
, "shellExpandArguments", false))
1706 flags
|= lldb::eLaunchFlagShellExpandArguments
;
1707 const bool detatchOnError
= GetBoolean(arguments
, "detachOnError", false);
1708 launch_info
.SetDetachOnError(detatchOnError
);
1709 launch_info
.SetLaunchFlags(flags
| lldb::eLaunchFlagDebug
|
1710 lldb::eLaunchFlagStopAtEntry
);
1712 // Run any pre run LLDB commands the user specified in the launch.json
1713 g_vsc
.RunPreRunCommands();
1715 if (GetBoolean(arguments
, "runInTerminal", false)) {
1716 if (llvm::Error err
= request_runInTerminal(request
))
1717 error
.SetErrorString(llvm::toString(std::move(err
)).c_str());
1718 } else if (launchCommands
.empty()) {
1719 // Disable async events so the launch will be successful when we return from
1720 // the launch call and the launch will happen synchronously
1721 g_vsc
.debugger
.SetAsync(false);
1722 g_vsc
.target
.Launch(launch_info
, error
);
1723 g_vsc
.debugger
.SetAsync(true);
1725 g_vsc
.RunLLDBCommands("Running launchCommands:", launchCommands
);
1726 // The custom commands might have created a new target so we should use the
1727 // selected target after these commands are run.
1728 g_vsc
.target
= g_vsc
.debugger
.GetSelectedTarget();
1732 response
["success"] = llvm::json::Value(false);
1733 EmplaceSafeString(response
, "message", std::string(error
.GetCString()));
1735 g_vsc
.RunLLDBCommands("Running postRunCommands:", postRunCommands
);
1738 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
1740 if (g_vsc
.is_attach
)
1741 SendProcessEvent(Attach
); // this happens when doing runInTerminal
1743 SendProcessEvent(Launch
);
1744 g_vsc
.SendJSON(llvm::json::Value(CreateEventObject("initialized")));
1748 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1749 // "type": "object",
1750 // "description": "Next request; value of command field is 'next'. The
1751 // request starts the debuggee to run again for one step.
1752 // The debug adapter first sends the NextResponse and then
1753 // a StoppedEvent (event type 'step') after the step has
1757 // "type": "string",
1758 // "enum": [ "next" ]
1761 // "$ref": "#/definitions/NextArguments"
1764 // "required": [ "command", "arguments" ]
1767 // "NextArguments": {
1768 // "type": "object",
1769 // "description": "Arguments for 'next' request.",
1772 // "type": "integer",
1773 // "description": "Execute 'next' for this thread."
1776 // "required": [ "threadId" ]
1778 // "NextResponse": {
1779 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1780 // "type": "object",
1781 // "description": "Response to 'next' request. This is just an
1782 // acknowledgement, so no body field is required."
1785 void request_next(const llvm::json::Object
&request
) {
1786 llvm::json::Object response
;
1787 FillResponse(request
, response
);
1788 auto arguments
= request
.getObject("arguments");
1789 lldb::SBThread thread
= g_vsc
.GetLLDBThread(*arguments
);
1790 if (thread
.IsValid()) {
1791 // Remember the thread ID that caused the resume so we can set the
1792 // "threadCausedFocus" boolean value in the "stopped" events.
1793 g_vsc
.focus_tid
= thread
.GetThreadID();
1796 response
["success"] = llvm::json::Value(false);
1798 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
1801 // "PauseRequest": {
1802 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1803 // "type": "object",
1804 // "description": "Pause request; value of command field is 'pause'. The
1805 // request suspenses the debuggee. The debug adapter first sends the
1806 // PauseResponse and then a StoppedEvent (event type 'pause') after the
1807 // thread has been paused successfully.", "properties": {
1809 // "type": "string",
1810 // "enum": [ "pause" ]
1813 // "$ref": "#/definitions/PauseArguments"
1816 // "required": [ "command", "arguments" ]
1819 // "PauseArguments": {
1820 // "type": "object",
1821 // "description": "Arguments for 'pause' request.",
1824 // "type": "integer",
1825 // "description": "Pause execution for this thread."
1828 // "required": [ "threadId" ]
1830 // "PauseResponse": {
1831 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1832 // "type": "object",
1833 // "description": "Response to 'pause' request. This is just an
1834 // acknowledgement, so no body field is required."
1837 void request_pause(const llvm::json::Object
&request
) {
1838 llvm::json::Object response
;
1839 FillResponse(request
, response
);
1840 lldb::SBProcess process
= g_vsc
.target
.GetProcess();
1841 lldb::SBError error
= process
.Stop();
1842 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
1845 // "ScopesRequest": {
1846 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1847 // "type": "object",
1848 // "description": "Scopes request; value of command field is 'scopes'. The
1849 // request returns the variable scopes for a given stackframe ID.",
1852 // "type": "string",
1853 // "enum": [ "scopes" ]
1856 // "$ref": "#/definitions/ScopesArguments"
1859 // "required": [ "command", "arguments" ]
1862 // "ScopesArguments": {
1863 // "type": "object",
1864 // "description": "Arguments for 'scopes' request.",
1867 // "type": "integer",
1868 // "description": "Retrieve the scopes for this stackframe."
1871 // "required": [ "frameId" ]
1873 // "ScopesResponse": {
1874 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1875 // "type": "object",
1876 // "description": "Response to 'scopes' request.",
1879 // "type": "object",
1884 // "$ref": "#/definitions/Scope"
1886 // "description": "The scopes of the stackframe. If the array has
1887 // length zero, there are no scopes available."
1890 // "required": [ "scopes" ]
1893 // "required": [ "body" ]
1896 void request_scopes(const llvm::json::Object
&request
) {
1897 llvm::json::Object response
;
1898 FillResponse(request
, response
);
1899 llvm::json::Object body
;
1900 auto arguments
= request
.getObject("arguments");
1901 lldb::SBFrame frame
= g_vsc
.GetLLDBFrame(*arguments
);
1902 // As the user selects different stack frames in the GUI, a "scopes" request
1903 // will be sent to the DAP. This is the only way we know that the user has
1904 // selected a frame in a thread. There are no other notifications that are
1905 // sent and VS code doesn't allow multiple frames to show variables
1906 // concurrently. If we select the thread and frame as the "scopes" requests
1907 // are sent, this allows users to type commands in the debugger console
1908 // with a backtick character to run lldb commands and these lldb commands
1909 // will now have the right context selected as they are run. If the user
1910 // types "`bt" into the debugger console and we had another thread selected
1911 // in the LLDB library, we would show the wrong thing to the user. If the
1912 // users switches threads with a lldb command like "`thread select 14", the
1913 // GUI will not update as there are no "event" notification packets that
1914 // allow us to change the currently selected thread or frame in the GUI that
1916 if (frame
.IsValid()) {
1917 frame
.GetThread().GetProcess().SetSelectedThread(frame
.GetThread());
1918 frame
.GetThread().SetSelectedFrame(frame
.GetFrameID());
1920 g_vsc
.variables
.locals
= frame
.GetVariables(/*arguments=*/true,
1923 /*in_scope_only=*/true);
1924 g_vsc
.variables
.globals
= frame
.GetVariables(/*arguments=*/false,
1927 /*in_scope_only=*/true);
1928 g_vsc
.variables
.registers
= frame
.GetRegisters();
1929 body
.try_emplace("scopes", g_vsc
.CreateTopLevelScopes());
1930 response
.try_emplace("body", std::move(body
));
1931 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
1934 // "SetBreakpointsRequest": {
1935 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1936 // "type": "object",
1937 // "description": "SetBreakpoints request; value of command field is
1938 // 'setBreakpoints'. Sets multiple breakpoints for a single source and
1939 // clears all previous breakpoints in that source. To clear all breakpoint
1940 // for a source, specify an empty array. When a breakpoint is hit, a
1941 // StoppedEvent (event type 'breakpoint') is generated.", "properties": {
1943 // "type": "string",
1944 // "enum": [ "setBreakpoints" ]
1947 // "$ref": "#/definitions/SetBreakpointsArguments"
1950 // "required": [ "command", "arguments" ]
1953 // "SetBreakpointsArguments": {
1954 // "type": "object",
1955 // "description": "Arguments for 'setBreakpoints' request.",
1958 // "$ref": "#/definitions/Source",
1959 // "description": "The source location of the breakpoints; either
1960 // source.path or source.reference must be specified."
1965 // "$ref": "#/definitions/SourceBreakpoint"
1967 // "description": "The code locations of the breakpoints."
1972 // "type": "integer"
1974 // "description": "Deprecated: The code locations of the breakpoints."
1976 // "sourceModified": {
1977 // "type": "boolean",
1978 // "description": "A value of true indicates that the underlying source
1979 // has been modified which results in new breakpoint locations."
1982 // "required": [ "source" ]
1984 // "SetBreakpointsResponse": {
1985 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1986 // "type": "object",
1987 // "description": "Response to 'setBreakpoints' request. Returned is
1988 // information about each breakpoint created by this request. This includes
1989 // the actual code location and whether the breakpoint could be verified.
1990 // The breakpoints returned are in the same order as the elements of the
1991 // 'breakpoints' (or the deprecated 'lines') in the
1992 // SetBreakpointsArguments.", "properties": {
1994 // "type": "object",
1999 // "$ref": "#/definitions/Breakpoint"
2001 // "description": "Information about the breakpoints. The array
2002 // elements are in the same order as the elements of the
2003 // 'breakpoints' (or the deprecated 'lines') in the
2004 // SetBreakpointsArguments."
2007 // "required": [ "breakpoints" ]
2010 // "required": [ "body" ]
2013 // "SourceBreakpoint": {
2014 // "type": "object",
2015 // "description": "Properties of a breakpoint or logpoint passed to the
2016 // setBreakpoints request.", "properties": {
2018 // "type": "integer",
2019 // "description": "The source line of the breakpoint or logpoint."
2022 // "type": "integer",
2023 // "description": "An optional source column of the breakpoint."
2026 // "type": "string",
2027 // "description": "An optional expression for conditional breakpoints."
2029 // "hitCondition": {
2030 // "type": "string",
2031 // "description": "An optional expression that controls how many hits of
2032 // the breakpoint are ignored. The backend is expected to interpret the
2033 // expression as needed."
2036 // "type": "string",
2037 // "description": "If this attribute exists and is non-empty, the backend
2038 // must not 'break' (stop) but log the message instead. Expressions within
2039 // {} are interpolated."
2042 // "required": [ "line" ]
2044 void request_setBreakpoints(const llvm::json::Object
&request
) {
2045 llvm::json::Object response
;
2046 lldb::SBError error
;
2047 FillResponse(request
, response
);
2048 auto arguments
= request
.getObject("arguments");
2049 auto source
= arguments
->getObject("source");
2050 const auto path
= GetString(source
, "path");
2051 auto breakpoints
= arguments
->getArray("breakpoints");
2052 llvm::json::Array response_breakpoints
;
2054 // Decode the source breakpoint infos for this "setBreakpoints" request
2055 SourceBreakpointMap request_bps
;
2056 // "breakpoints" may be unset, in which case we treat it the same as being set
2057 // to an empty array.
2059 for (const auto &bp
: *breakpoints
) {
2060 auto bp_obj
= bp
.getAsObject();
2062 SourceBreakpoint
src_bp(*bp_obj
);
2063 request_bps
[src_bp
.line
] = src_bp
;
2065 // We check if this breakpoint already exists to update it
2066 auto existing_source_bps
= g_vsc
.source_breakpoints
.find(path
);
2067 if (existing_source_bps
!= g_vsc
.source_breakpoints
.end()) {
2068 const auto &existing_bp
=
2069 existing_source_bps
->second
.find(src_bp
.line
);
2070 if (existing_bp
!= existing_source_bps
->second
.end()) {
2071 existing_bp
->second
.UpdateBreakpoint(src_bp
);
2072 AppendBreakpoint(existing_bp
->second
.bp
, response_breakpoints
, path
,
2077 // At this point the breakpoint is new
2078 src_bp
.SetBreakpoint(path
.data());
2079 AppendBreakpoint(src_bp
.bp
, response_breakpoints
, path
, src_bp
.line
);
2080 g_vsc
.source_breakpoints
[path
][src_bp
.line
] = std::move(src_bp
);
2085 // Delete any breakpoints in this source file that aren't in the
2086 // request_bps set. There is no call to remove breakpoints other than
2087 // calling this function with a smaller or empty "breakpoints" list.
2088 auto old_src_bp_pos
= g_vsc
.source_breakpoints
.find(path
);
2089 if (old_src_bp_pos
!= g_vsc
.source_breakpoints
.end()) {
2090 for (auto &old_bp
: old_src_bp_pos
->second
) {
2091 auto request_pos
= request_bps
.find(old_bp
.first
);
2092 if (request_pos
== request_bps
.end()) {
2093 // This breakpoint no longer exists in this source file, delete it
2094 g_vsc
.target
.BreakpointDelete(old_bp
.second
.bp
.GetID());
2095 old_src_bp_pos
->second
.erase(old_bp
.first
);
2100 llvm::json::Object body
;
2101 body
.try_emplace("breakpoints", std::move(response_breakpoints
));
2102 response
.try_emplace("body", std::move(body
));
2103 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
2106 // "SetExceptionBreakpointsRequest": {
2107 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2108 // "type": "object",
2109 // "description": "SetExceptionBreakpoints request; value of command field
2110 // is 'setExceptionBreakpoints'. The request configures the debuggers
2111 // response to thrown exceptions. If an exception is configured to break, a
2112 // StoppedEvent is fired (event type 'exception').", "properties": {
2114 // "type": "string",
2115 // "enum": [ "setExceptionBreakpoints" ]
2118 // "$ref": "#/definitions/SetExceptionBreakpointsArguments"
2121 // "required": [ "command", "arguments" ]
2124 // "SetExceptionBreakpointsArguments": {
2125 // "type": "object",
2126 // "description": "Arguments for 'setExceptionBreakpoints' request.",
2133 // "description": "IDs of checked exception options. The set of IDs is
2134 // returned via the 'exceptionBreakpointFilters' capability."
2136 // "exceptionOptions": {
2139 // "$ref": "#/definitions/ExceptionOptions"
2141 // "description": "Configuration options for selected exceptions."
2144 // "required": [ "filters" ]
2146 // "SetExceptionBreakpointsResponse": {
2147 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2148 // "type": "object",
2149 // "description": "Response to 'setExceptionBreakpoints' request. This is
2150 // just an acknowledgement, so no body field is required."
2153 void request_setExceptionBreakpoints(const llvm::json::Object
&request
) {
2154 llvm::json::Object response
;
2155 lldb::SBError error
;
2156 FillResponse(request
, response
);
2157 auto arguments
= request
.getObject("arguments");
2158 auto filters
= arguments
->getArray("filters");
2159 // Keep a list of any exception breakpoint filter names that weren't set
2160 // so we can clear any exception breakpoints if needed.
2161 std::set
<std::string
> unset_filters
;
2162 for (const auto &bp
: g_vsc
.exception_breakpoints
)
2163 unset_filters
.insert(bp
.filter
);
2165 for (const auto &value
: *filters
) {
2166 const auto filter
= GetAsString(value
);
2167 auto exc_bp
= g_vsc
.GetExceptionBreakpoint(std::string(filter
));
2169 exc_bp
->SetBreakpoint();
2170 unset_filters
.erase(std::string(filter
));
2173 for (const auto &filter
: unset_filters
) {
2174 auto exc_bp
= g_vsc
.GetExceptionBreakpoint(filter
);
2176 exc_bp
->ClearBreakpoint();
2178 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
2181 // "SetFunctionBreakpointsRequest": {
2182 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2183 // "type": "object",
2184 // "description": "SetFunctionBreakpoints request; value of command field is
2185 // 'setFunctionBreakpoints'. Sets multiple function breakpoints and clears
2186 // all previous function breakpoints. To clear all function breakpoint,
2187 // specify an empty array. When a function breakpoint is hit, a StoppedEvent
2188 // (event type 'function breakpoint') is generated.", "properties": {
2190 // "type": "string",
2191 // "enum": [ "setFunctionBreakpoints" ]
2194 // "$ref": "#/definitions/SetFunctionBreakpointsArguments"
2197 // "required": [ "command", "arguments" ]
2200 // "SetFunctionBreakpointsArguments": {
2201 // "type": "object",
2202 // "description": "Arguments for 'setFunctionBreakpoints' request.",
2207 // "$ref": "#/definitions/FunctionBreakpoint"
2209 // "description": "The function names of the breakpoints."
2212 // "required": [ "breakpoints" ]
2214 // "FunctionBreakpoint": {
2215 // "type": "object",
2216 // "description": "Properties of a breakpoint passed to the
2217 // setFunctionBreakpoints request.", "properties": {
2219 // "type": "string",
2220 // "description": "The name of the function."
2223 // "type": "string",
2224 // "description": "An optional expression for conditional breakpoints."
2226 // "hitCondition": {
2227 // "type": "string",
2228 // "description": "An optional expression that controls how many hits of
2229 // the breakpoint are ignored. The backend is expected to interpret the
2230 // expression as needed."
2233 // "required": [ "name" ]
2235 // "SetFunctionBreakpointsResponse": {
2236 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2237 // "type": "object",
2238 // "description": "Response to 'setFunctionBreakpoints' request. Returned is
2239 // information about each breakpoint created by this request.",
2242 // "type": "object",
2247 // "$ref": "#/definitions/Breakpoint"
2249 // "description": "Information about the breakpoints. The array
2250 // elements correspond to the elements of the 'breakpoints' array."
2253 // "required": [ "breakpoints" ]
2256 // "required": [ "body" ]
2259 void request_setFunctionBreakpoints(const llvm::json::Object
&request
) {
2260 llvm::json::Object response
;
2261 lldb::SBError error
;
2262 FillResponse(request
, response
);
2263 auto arguments
= request
.getObject("arguments");
2264 auto breakpoints
= arguments
->getArray("breakpoints");
2265 FunctionBreakpointMap request_bps
;
2266 llvm::json::Array response_breakpoints
;
2267 for (const auto &value
: *breakpoints
) {
2268 auto bp_obj
= value
.getAsObject();
2269 if (bp_obj
== nullptr)
2271 FunctionBreakpoint
func_bp(*bp_obj
);
2272 request_bps
[func_bp
.functionName
] = std::move(func_bp
);
2275 std::vector
<llvm::StringRef
> remove_names
;
2276 // Disable any function breakpoints that aren't in the request_bps.
2277 // There is no call to remove function breakpoints other than calling this
2278 // function with a smaller or empty "breakpoints" list.
2279 for (auto &pair
: g_vsc
.function_breakpoints
) {
2280 auto request_pos
= request_bps
.find(pair
.first());
2281 if (request_pos
== request_bps
.end()) {
2282 // This function breakpoint no longer exists delete it from LLDB
2283 g_vsc
.target
.BreakpointDelete(pair
.second
.bp
.GetID());
2284 remove_names
.push_back(pair
.first());
2286 // Update the existing breakpoint as any setting withing the function
2287 // breakpoint might have changed.
2288 pair
.second
.UpdateBreakpoint(request_pos
->second
);
2289 // Remove this breakpoint from the request breakpoints since we have
2290 // handled it here and we don't need to set a new breakpoint below.
2291 request_bps
.erase(request_pos
);
2292 // Add this breakpoint info to the response
2293 AppendBreakpoint(pair
.second
.bp
, response_breakpoints
);
2296 // Remove any breakpoints that are no longer in our list
2297 for (const auto &name
: remove_names
)
2298 g_vsc
.function_breakpoints
.erase(name
);
2300 // Any breakpoints that are left in "request_bps" are breakpoints that
2302 for (auto &pair
: request_bps
) {
2303 pair
.second
.SetBreakpoint();
2304 // Add this breakpoint info to the response
2305 AppendBreakpoint(pair
.second
.bp
, response_breakpoints
);
2306 g_vsc
.function_breakpoints
[pair
.first()] = std::move(pair
.second
);
2309 llvm::json::Object body
;
2310 body
.try_emplace("breakpoints", std::move(response_breakpoints
));
2311 response
.try_emplace("body", std::move(body
));
2312 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
2315 // "SourceRequest": {
2316 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2317 // "type": "object",
2318 // "description": "Source request; value of command field is 'source'. The
2319 // request retrieves the source code for a given source reference.",
2322 // "type": "string",
2323 // "enum": [ "source" ]
2326 // "$ref": "#/definitions/SourceArguments"
2329 // "required": [ "command", "arguments" ]
2332 // "SourceArguments": {
2333 // "type": "object",
2334 // "description": "Arguments for 'source' request.",
2337 // "$ref": "#/definitions/Source",
2338 // "description": "Specifies the source content to load. Either
2339 // source.path or source.sourceReference must be specified."
2341 // "sourceReference": {
2342 // "type": "integer",
2343 // "description": "The reference to the source. This is the same as
2344 // source.sourceReference. This is provided for backward compatibility
2345 // since old backends do not understand the 'source' attribute."
2348 // "required": [ "sourceReference" ]
2350 // "SourceResponse": {
2351 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2352 // "type": "object",
2353 // "description": "Response to 'source' request.",
2356 // "type": "object",
2359 // "type": "string",
2360 // "description": "Content of the source reference."
2363 // "type": "string",
2364 // "description": "Optional content type (mime type) of the source."
2367 // "required": [ "content" ]
2370 // "required": [ "body" ]
2373 void request_source(const llvm::json::Object
&request
) {
2374 llvm::json::Object response
;
2375 FillResponse(request
, response
);
2376 llvm::json::Object body
;
2378 auto arguments
= request
.getObject("arguments");
2379 auto source
= arguments
->getObject("source");
2380 auto sourceReference
= GetSigned(source
, "sourceReference", -1);
2381 auto pos
= g_vsc
.source_map
.find((lldb::addr_t
)sourceReference
);
2382 if (pos
!= g_vsc
.source_map
.end()) {
2383 EmplaceSafeString(body
, "content", pos
->second
.content
);
2385 response
["success"] = llvm::json::Value(false);
2387 EmplaceSafeString(body
, "mimeType", "text/x-lldb.disassembly");
2388 response
.try_emplace("body", std::move(body
));
2389 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
2392 // "StackTraceRequest": {
2393 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2394 // "type": "object",
2395 // "description": "StackTrace request; value of command field is
2396 // 'stackTrace'. The request returns a stacktrace from the current execution
2397 // state.", "properties": {
2399 // "type": "string",
2400 // "enum": [ "stackTrace" ]
2403 // "$ref": "#/definitions/StackTraceArguments"
2406 // "required": [ "command", "arguments" ]
2409 // "StackTraceArguments": {
2410 // "type": "object",
2411 // "description": "Arguments for 'stackTrace' request.",
2414 // "type": "integer",
2415 // "description": "Retrieve the stacktrace for this thread."
2418 // "type": "integer",
2419 // "description": "The index of the first frame to return; if omitted
2420 // frames start at 0."
2423 // "type": "integer",
2424 // "description": "The maximum number of frames to return. If levels is
2425 // not specified or 0, all frames are returned."
2428 // "$ref": "#/definitions/StackFrameFormat",
2429 // "description": "Specifies details on how to format the stack frames."
2432 // "required": [ "threadId" ]
2434 // "StackTraceResponse": {
2435 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2436 // "type": "object",
2437 // "description": "Response to 'stackTrace' request.",
2440 // "type": "object",
2445 // "$ref": "#/definitions/StackFrame"
2447 // "description": "The frames of the stackframe. If the array has
2448 // length zero, there are no stackframes available. This means that
2449 // there is no location information available."
2452 // "type": "integer",
2453 // "description": "The total number of frames available."
2456 // "required": [ "stackFrames" ]
2459 // "required": [ "body" ]
2462 void request_stackTrace(const llvm::json::Object
&request
) {
2463 llvm::json::Object response
;
2464 FillResponse(request
, response
);
2465 lldb::SBError error
;
2466 auto arguments
= request
.getObject("arguments");
2467 lldb::SBThread thread
= g_vsc
.GetLLDBThread(*arguments
);
2468 llvm::json::Array stackFrames
;
2469 llvm::json::Object body
;
2471 if (thread
.IsValid()) {
2472 const auto startFrame
= GetUnsigned(arguments
, "startFrame", 0);
2473 const auto levels
= GetUnsigned(arguments
, "levels", 0);
2474 const auto endFrame
= (levels
== 0) ? INT64_MAX
: (startFrame
+ levels
);
2475 for (uint32_t i
= startFrame
; i
< endFrame
; ++i
) {
2476 auto frame
= thread
.GetFrameAtIndex(i
);
2477 if (!frame
.IsValid())
2479 stackFrames
.emplace_back(CreateStackFrame(frame
));
2481 const auto totalFrames
= thread
.GetNumFrames();
2482 body
.try_emplace("totalFrames", totalFrames
);
2484 body
.try_emplace("stackFrames", std::move(stackFrames
));
2485 response
.try_emplace("body", std::move(body
));
2486 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
2489 // "StepInRequest": {
2490 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2491 // "type": "object",
2492 // "description": "StepIn request; value of command field is 'stepIn'. The
2493 // request starts the debuggee to step into a function/method if possible.
2494 // If it cannot step into a target, 'stepIn' behaves like 'next'. The debug
2495 // adapter first sends the StepInResponse and then a StoppedEvent (event
2496 // type 'step') after the step has completed. If there are multiple
2497 // function/method calls (or other targets) on the source line, the optional
2498 // argument 'targetId' can be used to control into which target the 'stepIn'
2499 // should occur. The list of possible targets for a given source line can be
2500 // retrieved via the 'stepInTargets' request.", "properties": {
2502 // "type": "string",
2503 // "enum": [ "stepIn" ]
2506 // "$ref": "#/definitions/StepInArguments"
2509 // "required": [ "command", "arguments" ]
2512 // "StepInArguments": {
2513 // "type": "object",
2514 // "description": "Arguments for 'stepIn' request.",
2517 // "type": "integer",
2518 // "description": "Execute 'stepIn' for this thread."
2521 // "type": "integer",
2522 // "description": "Optional id of the target to step into."
2525 // "required": [ "threadId" ]
2527 // "StepInResponse": {
2528 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2529 // "type": "object",
2530 // "description": "Response to 'stepIn' request. This is just an
2531 // acknowledgement, so no body field is required."
2534 void request_stepIn(const llvm::json::Object
&request
) {
2535 llvm::json::Object response
;
2536 FillResponse(request
, response
);
2537 auto arguments
= request
.getObject("arguments");
2538 lldb::SBThread thread
= g_vsc
.GetLLDBThread(*arguments
);
2539 if (thread
.IsValid()) {
2540 // Remember the thread ID that caused the resume so we can set the
2541 // "threadCausedFocus" boolean value in the "stopped" events.
2542 g_vsc
.focus_tid
= thread
.GetThreadID();
2545 response
["success"] = llvm::json::Value(false);
2547 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
2550 // "StepOutRequest": {
2551 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2552 // "type": "object",
2553 // "description": "StepOut request; value of command field is 'stepOut'. The
2554 // request starts the debuggee to run again for one step. The debug adapter
2555 // first sends the StepOutResponse and then a StoppedEvent (event type
2556 // 'step') after the step has completed.", "properties": {
2558 // "type": "string",
2559 // "enum": [ "stepOut" ]
2562 // "$ref": "#/definitions/StepOutArguments"
2565 // "required": [ "command", "arguments" ]
2568 // "StepOutArguments": {
2569 // "type": "object",
2570 // "description": "Arguments for 'stepOut' request.",
2573 // "type": "integer",
2574 // "description": "Execute 'stepOut' for this thread."
2577 // "required": [ "threadId" ]
2579 // "StepOutResponse": {
2580 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2581 // "type": "object",
2582 // "description": "Response to 'stepOut' request. This is just an
2583 // acknowledgement, so no body field is required."
2586 void request_stepOut(const llvm::json::Object
&request
) {
2587 llvm::json::Object response
;
2588 FillResponse(request
, response
);
2589 auto arguments
= request
.getObject("arguments");
2590 lldb::SBThread thread
= g_vsc
.GetLLDBThread(*arguments
);
2591 if (thread
.IsValid()) {
2592 // Remember the thread ID that caused the resume so we can set the
2593 // "threadCausedFocus" boolean value in the "stopped" events.
2594 g_vsc
.focus_tid
= thread
.GetThreadID();
2597 response
["success"] = llvm::json::Value(false);
2599 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
2602 // "ThreadsRequest": {
2603 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2604 // "type": "object",
2605 // "description": "Thread request; value of command field is 'threads'. The
2606 // request retrieves a list of all threads.", "properties": {
2608 // "type": "string",
2609 // "enum": [ "threads" ]
2612 // "required": [ "command" ]
2615 // "ThreadsResponse": {
2616 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2617 // "type": "object",
2618 // "description": "Response to 'threads' request.",
2621 // "type": "object",
2626 // "$ref": "#/definitions/Thread"
2628 // "description": "All threads."
2631 // "required": [ "threads" ]
2634 // "required": [ "body" ]
2637 void request_threads(const llvm::json::Object
&request
) {
2639 lldb::SBProcess process
= g_vsc
.target
.GetProcess();
2640 llvm::json::Object response
;
2641 FillResponse(request
, response
);
2643 const uint32_t num_threads
= process
.GetNumThreads();
2644 llvm::json::Array threads
;
2645 for (uint32_t thread_idx
= 0; thread_idx
< num_threads
; ++thread_idx
) {
2646 lldb::SBThread thread
= process
.GetThreadAtIndex(thread_idx
);
2647 threads
.emplace_back(CreateThread(thread
));
2649 if (threads
.size() == 0) {
2650 response
["success"] = llvm::json::Value(false);
2652 llvm::json::Object body
;
2653 body
.try_emplace("threads", std::move(threads
));
2654 response
.try_emplace("body", std::move(body
));
2655 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
2658 // "SetVariableRequest": {
2659 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2660 // "type": "object",
2661 // "description": "setVariable request; value of command field is
2662 // 'setVariable'. Set the variable with the given name in the variable
2663 // container to a new value.", "properties": {
2665 // "type": "string",
2666 // "enum": [ "setVariable" ]
2669 // "$ref": "#/definitions/SetVariableArguments"
2672 // "required": [ "command", "arguments" ]
2675 // "SetVariableArguments": {
2676 // "type": "object",
2677 // "description": "Arguments for 'setVariable' request.",
2679 // "variablesReference": {
2680 // "type": "integer",
2681 // "description": "The reference of the variable container."
2684 // "type": "string",
2685 // "description": "The name of the variable."
2688 // "type": "string",
2689 // "description": "The value of the variable."
2692 // "$ref": "#/definitions/ValueFormat",
2693 // "description": "Specifies details on how to format the response value."
2696 // "required": [ "variablesReference", "name", "value" ]
2698 // "SetVariableResponse": {
2699 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2700 // "type": "object",
2701 // "description": "Response to 'setVariable' request.",
2704 // "type": "object",
2707 // "type": "string",
2708 // "description": "The new value of the variable."
2711 // "type": "string",
2712 // "description": "The type of the new value. Typically shown in the
2713 // UI when hovering over the value."
2715 // "variablesReference": {
2716 // "type": "number",
2717 // "description": "If variablesReference is > 0, the new value is
2718 // structured and its children can be retrieved by passing
2719 // variablesReference to the VariablesRequest."
2721 // "namedVariables": {
2722 // "type": "number",
2723 // "description": "The number of named child variables. The client
2724 // can use this optional information to present the variables in a
2725 // paged UI and fetch them in chunks."
2727 // "indexedVariables": {
2728 // "type": "number",
2729 // "description": "The number of indexed child variables. The client
2730 // can use this optional information to present the variables in a
2731 // paged UI and fetch them in chunks."
2734 // "required": [ "value" ]
2737 // "required": [ "body" ]
2740 void request_setVariable(const llvm::json::Object
&request
) {
2741 llvm::json::Object response
;
2742 FillResponse(request
, response
);
2743 llvm::json::Array variables
;
2744 llvm::json::Object body
;
2745 auto arguments
= request
.getObject("arguments");
2746 // This is a reference to the containing variable/scope
2747 const auto variablesReference
=
2748 GetUnsigned(arguments
, "variablesReference", 0);
2749 llvm::StringRef name
= GetString(arguments
, "name");
2750 bool is_duplicated_variable_name
= name
.contains(" @");
2752 const auto value
= GetString(arguments
, "value");
2753 // Set success to false just in case we don't find the variable by name
2754 response
.try_emplace("success", false);
2756 lldb::SBValue variable
;
2757 int64_t newVariablesReference
= 0;
2759 // The "id" is the unique integer ID that is unique within the enclosing
2760 // variablesReference. It is optionally added to any "interface Variable"
2761 // objects to uniquely identify a variable within an enclosing
2762 // variablesReference. It helps to disambiguate between two variables that
2763 // have the same name within the same scope since the "setVariables" request
2764 // only specifies the variable reference of the enclosing scope/variable, and
2765 // the name of the variable. We could have two shadowed variables with the
2766 // same name in "Locals" or "Globals". In our case the "id" absolute index
2767 // of the variable within the g_vsc.variables list.
2768 const auto id_value
= GetUnsigned(arguments
, "id", UINT64_MAX
);
2769 if (id_value
!= UINT64_MAX
) {
2770 variable
= g_vsc
.variables
.GetVariable(id_value
);
2771 } else if (lldb::SBValueList
*top_scope
=
2772 GetTopLevelScope(variablesReference
)) {
2773 // variablesReference is one of our scopes, not an actual variable it is
2774 // asking for a variable in locals or globals or registers
2775 int64_t end_idx
= top_scope
->GetSize();
2776 // Searching backward so that we choose the variable in closest scope
2777 // among variables of the same name.
2778 for (int64_t i
= end_idx
- 1; i
>= 0; --i
) {
2779 lldb::SBValue curr_variable
= top_scope
->GetValueAtIndex(i
);
2780 std::string variable_name
= CreateUniqueVariableNameForDisplay(
2781 curr_variable
, is_duplicated_variable_name
);
2782 if (variable_name
== name
) {
2783 variable
= curr_variable
;
2788 // This is not under the globals or locals scope, so there are no duplicated
2791 // We have a named item within an actual variable so we need to find it
2792 // withing the container variable by name.
2793 lldb::SBValue container
= g_vsc
.variables
.GetVariable(variablesReference
);
2794 variable
= container
.GetChildMemberWithName(name
.data());
2795 if (!variable
.IsValid()) {
2796 if (name
.startswith("[")) {
2797 llvm::StringRef
index_str(name
.drop_front(1));
2799 if (!index_str
.consumeInteger(0, index
)) {
2800 if (index_str
== "]")
2801 variable
= container
.GetChildAtIndex(index
);
2807 if (variable
.IsValid()) {
2808 lldb::SBError error
;
2809 bool success
= variable
.SetValueFromCString(value
.data(), error
);
2811 SetValueForKey(variable
, body
, "value");
2812 EmplaceSafeString(body
, "type", variable
.GetType().GetDisplayTypeName());
2814 // We don't know the index of the variable in our g_vsc.variables
2815 // so always insert a new one to get its variablesReference.
2816 // is_permanent is false because debug console does not support
2817 // setVariable request.
2818 if (variable
.MightHaveChildren())
2819 newVariablesReference
= g_vsc
.variables
.InsertExpandableVariable(
2820 variable
, /*is_permanent=*/false);
2822 body
.try_emplace("variablesReference", newVariablesReference
);
2824 EmplaceSafeString(body
, "message", std::string(error
.GetCString()));
2826 response
["success"] = llvm::json::Value(success
);
2828 response
["success"] = llvm::json::Value(false);
2831 response
.try_emplace("body", std::move(body
));
2832 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
2835 // "VariablesRequest": {
2836 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2837 // "type": "object",
2838 // "description": "Variables request; value of command field is 'variables'.
2839 // Retrieves all child variables for the given variable reference. An
2840 // optional filter can be used to limit the fetched children to either named
2841 // or indexed children.", "properties": {
2843 // "type": "string",
2844 // "enum": [ "variables" ]
2847 // "$ref": "#/definitions/VariablesArguments"
2850 // "required": [ "command", "arguments" ]
2853 // "VariablesArguments": {
2854 // "type": "object",
2855 // "description": "Arguments for 'variables' request.",
2857 // "variablesReference": {
2858 // "type": "integer",
2859 // "description": "The Variable reference."
2862 // "type": "string",
2863 // "enum": [ "indexed", "named" ],
2864 // "description": "Optional filter to limit the child variables to either
2865 // named or indexed. If ommited, both types are fetched."
2868 // "type": "integer",
2869 // "description": "The index of the first variable to return; if omitted
2870 // children start at 0."
2873 // "type": "integer",
2874 // "description": "The number of variables to return. If count is missing
2875 // or 0, all variables are returned."
2878 // "$ref": "#/definitions/ValueFormat",
2879 // "description": "Specifies details on how to format the Variable
2883 // "required": [ "variablesReference" ]
2885 // "VariablesResponse": {
2886 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2887 // "type": "object",
2888 // "description": "Response to 'variables' request.",
2891 // "type": "object",
2896 // "$ref": "#/definitions/Variable"
2898 // "description": "All (or a range) of variables for the given
2899 // variable reference."
2902 // "required": [ "variables" ]
2905 // "required": [ "body" ]
2908 void request_variables(const llvm::json::Object
&request
) {
2909 llvm::json::Object response
;
2910 FillResponse(request
, response
);
2911 llvm::json::Array variables
;
2912 auto arguments
= request
.getObject("arguments");
2913 const auto variablesReference
=
2914 GetUnsigned(arguments
, "variablesReference", 0);
2915 const int64_t start
= GetSigned(arguments
, "start", 0);
2916 const int64_t count
= GetSigned(arguments
, "count", 0);
2918 auto format
= arguments
->getObject("format");
2920 hex
= GetBoolean(format
, "hex", false);
2922 if (lldb::SBValueList
*top_scope
= GetTopLevelScope(variablesReference
)) {
2923 // variablesReference is one of our scopes, not an actual variable it is
2924 // asking for the list of args, locals or globals.
2925 int64_t start_idx
= 0;
2926 int64_t num_children
= 0;
2928 num_children
= top_scope
->GetSize();
2929 const int64_t end_idx
= start_idx
+ ((count
== 0) ? num_children
: count
);
2931 // We first find out which variable names are duplicated
2932 std::map
<std::string
, int> variable_name_counts
;
2933 for (auto i
= start_idx
; i
< end_idx
; ++i
) {
2934 lldb::SBValue variable
= top_scope
->GetValueAtIndex(i
);
2935 if (!variable
.IsValid())
2937 variable_name_counts
[GetNonNullVariableName(variable
)]++;
2940 // Now we construct the result with unique display variable names
2941 for (auto i
= start_idx
; i
< end_idx
; ++i
) {
2942 lldb::SBValue variable
= top_scope
->GetValueAtIndex(i
);
2944 if (!variable
.IsValid())
2947 int64_t var_ref
= 0;
2948 if (variable
.MightHaveChildren()) {
2949 var_ref
= g_vsc
.variables
.InsertExpandableVariable(
2950 variable
, /*is_permanent=*/false);
2952 variables
.emplace_back(CreateVariable(
2953 variable
, var_ref
, var_ref
!= 0 ? var_ref
: UINT64_MAX
, hex
,
2954 variable_name_counts
[GetNonNullVariableName(variable
)] > 1));
2957 // We are expanding a variable that has children, so we will return its
2959 lldb::SBValue variable
= g_vsc
.variables
.GetVariable(variablesReference
);
2960 if (variable
.IsValid()) {
2961 const auto num_children
= variable
.GetNumChildren();
2962 const int64_t end_idx
= start
+ ((count
== 0) ? num_children
: count
);
2963 for (auto i
= start
; i
< end_idx
; ++i
) {
2964 lldb::SBValue child
= variable
.GetChildAtIndex(i
);
2965 if (!child
.IsValid())
2967 if (child
.MightHaveChildren()) {
2969 g_vsc
.variables
.IsPermanentVariableReference(variablesReference
);
2970 auto childVariablesReferences
=
2971 g_vsc
.variables
.InsertExpandableVariable(child
, is_permanent
);
2972 variables
.emplace_back(CreateVariable(child
, childVariablesReferences
,
2973 childVariablesReferences
, hex
));
2975 variables
.emplace_back(CreateVariable(child
, 0, INT64_MAX
, hex
));
2980 llvm::json::Object body
;
2981 body
.try_emplace("variables", std::move(variables
));
2982 response
.try_emplace("body", std::move(body
));
2983 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
2986 // A request used in testing to get the details on all breakpoints that are
2987 // currently set in the target. This helps us to test "setBreakpoints" and
2988 // "setFunctionBreakpoints" requests to verify we have the correct set of
2989 // breakpoints currently set in LLDB.
2990 void request__testGetTargetBreakpoints(const llvm::json::Object
&request
) {
2991 llvm::json::Object response
;
2992 FillResponse(request
, response
);
2993 llvm::json::Array response_breakpoints
;
2994 for (uint32_t i
= 0; g_vsc
.target
.GetBreakpointAtIndex(i
).IsValid(); ++i
) {
2995 auto bp
= g_vsc
.target
.GetBreakpointAtIndex(i
);
2996 AppendBreakpoint(bp
, response_breakpoints
);
2998 llvm::json::Object body
;
2999 body
.try_emplace("breakpoints", std::move(response_breakpoints
));
3000 response
.try_emplace("body", std::move(body
));
3001 g_vsc
.SendJSON(llvm::json::Value(std::move(response
)));
3004 void RegisterRequestCallbacks() {
3005 g_vsc
.RegisterRequestCallback("attach", request_attach
);
3006 g_vsc
.RegisterRequestCallback("completions", request_completions
);
3007 g_vsc
.RegisterRequestCallback("continue", request_continue
);
3008 g_vsc
.RegisterRequestCallback("configurationDone", request_configurationDone
);
3009 g_vsc
.RegisterRequestCallback("disconnect", request_disconnect
);
3010 g_vsc
.RegisterRequestCallback("evaluate", request_evaluate
);
3011 g_vsc
.RegisterRequestCallback("exceptionInfo", request_exceptionInfo
);
3012 g_vsc
.RegisterRequestCallback("initialize", request_initialize
);
3013 g_vsc
.RegisterRequestCallback("launch", request_launch
);
3014 g_vsc
.RegisterRequestCallback("next", request_next
);
3015 g_vsc
.RegisterRequestCallback("pause", request_pause
);
3016 g_vsc
.RegisterRequestCallback("scopes", request_scopes
);
3017 g_vsc
.RegisterRequestCallback("setBreakpoints", request_setBreakpoints
);
3018 g_vsc
.RegisterRequestCallback("setExceptionBreakpoints",
3019 request_setExceptionBreakpoints
);
3020 g_vsc
.RegisterRequestCallback("setFunctionBreakpoints",
3021 request_setFunctionBreakpoints
);
3022 g_vsc
.RegisterRequestCallback("setVariable", request_setVariable
);
3023 g_vsc
.RegisterRequestCallback("source", request_source
);
3024 g_vsc
.RegisterRequestCallback("stackTrace", request_stackTrace
);
3025 g_vsc
.RegisterRequestCallback("stepIn", request_stepIn
);
3026 g_vsc
.RegisterRequestCallback("stepOut", request_stepOut
);
3027 g_vsc
.RegisterRequestCallback("threads", request_threads
);
3028 g_vsc
.RegisterRequestCallback("variables", request_variables
);
3030 g_vsc
.RegisterRequestCallback("compileUnits", request_compileUnits
);
3031 g_vsc
.RegisterRequestCallback("modules", request_modules
);
3033 g_vsc
.RegisterRequestCallback("_testGetTargetBreakpoints",
3034 request__testGetTargetBreakpoints
);
3037 } // anonymous namespace
3039 static void printHelp(LLDBVSCodeOptTable
&table
, llvm::StringRef tool_name
) {
3040 std::string usage_str
= tool_name
.str() + " options";
3041 table
.printHelp(llvm::outs(), usage_str
.c_str(), "LLDB VSCode", false);
3043 std::string examples
= R
"___(
3045 The debug adapter can be started in two modes.
3047 Running lldb-vscode without any arguments will start communicating with the
3048 parent over stdio. Passing a port number causes lldb-vscode to start listening
3049 for connections on that port.
3051 lldb-vscode -p <port>
3053 Passing --wait-for-debugger will pause the process at startup and wait for a
3054 debugger to attach to the process.
3058 llvm::outs() << examples
;
3061 // If --launch-target is provided, this instance of lldb-vscode becomes a
3062 // runInTerminal launcher. It will ultimately launch the program specified in
3063 // the --launch-target argument, which is the original program the user wanted
3064 // to debug. This is done in such a way that the actual debug adaptor can
3065 // place breakpoints at the beginning of the program.
3067 // The launcher will communicate with the debug adaptor using a fifo file in the
3068 // directory specified in the --comm-file argument.
3070 // Regarding the actual flow, this launcher will first notify the debug adaptor
3071 // of its pid. Then, the launcher will be in a pending state waiting to be
3072 // attached by the adaptor.
3074 // Once attached and resumed, the launcher will exec and become the program
3075 // specified by --launch-target, which is the original target the
3076 // user wanted to run.
3078 // In case of errors launching the target, a suitable error message will be
3079 // emitted to the debug adaptor.
3080 void LaunchRunInTerminalTarget(llvm::opt::Arg
&target_arg
,
3081 llvm::StringRef comm_file
, char *argv
[]) {
3083 llvm::errs() << "runInTerminal is only supported on POSIX systems\n";
3086 RunInTerminalLauncherCommChannel
comm_channel(comm_file
);
3087 if (llvm::Error err
= comm_channel
.NotifyPid()) {
3088 llvm::errs() << llvm::toString(std::move(err
)) << "\n";
3092 // We will wait to be attached with a timeout. We don't wait indefinitely
3093 // using a signal to prevent being paused forever.
3095 // This env var should be used only for tests.
3096 const char *timeout_env_var
= getenv("LLDB_VSCODE_RIT_TIMEOUT_IN_MS");
3098 timeout_env_var
!= nullptr ? atoi(timeout_env_var
) : 20000;
3099 if (llvm::Error err
= comm_channel
.WaitUntilDebugAdaptorAttaches(
3100 std::chrono::milliseconds(timeout_in_ms
))) {
3101 llvm::errs() << llvm::toString(std::move(err
)) << "\n";
3105 const char *target
= target_arg
.getValue();
3106 execvp(target
, argv
);
3108 std::string error
= std::strerror(errno
);
3109 comm_channel
.NotifyError(error
);
3110 llvm::errs() << error
<< "\n";
3115 /// used only by TestVSCode_redirection_to_console.py
3116 void redirection_test() {
3117 printf("stdout message\n");
3118 fprintf(stderr
, "stderr message\n");
3123 /// Redirect stdout and stderr fo the IDE's console output.
3125 /// Errors in this operation will be printed to the log file and the IDE's
3126 /// console output as well.
3129 /// A fd pointing to the original stdout.
3130 int SetupStdoutStderrRedirection() {
3131 int new_stdout_fd
= dup(fileno(stdout
));
3132 auto stdout_err_redirector_callback
= [&](llvm::StringRef data
) {
3133 g_vsc
.SendOutput(OutputType::Console
, data
);
3136 for (int fd
: {fileno(stdout
), fileno(stderr
)}) {
3137 if (llvm::Error err
= RedirectFd(fd
, stdout_err_redirector_callback
)) {
3138 std::string error_message
= llvm::toString(std::move(err
));
3140 *g_vsc
.log
<< error_message
<< std::endl
;
3141 stdout_err_redirector_callback(error_message
);
3145 /// used only by TestVSCode_redirection_to_console.py
3146 if (getenv("LLDB_VSCODE_TEST_STDOUT_STDERR_REDIRECTION") != nullptr)
3148 return new_stdout_fd
;
3151 int main(int argc
, char *argv
[]) {
3152 llvm::InitLLVM
IL(argc
, argv
, /*InstallPipeSignalExitHandler=*/false);
3153 llvm::PrettyStackTraceProgram
X(argc
, argv
);
3155 llvm::SmallString
<256> program_path(argv
[0]);
3156 llvm::sys::fs::make_absolute(program_path
);
3157 g_vsc
.debug_adaptor_path
= program_path
.str().str();
3159 LLDBVSCodeOptTable T
;
3161 llvm::ArrayRef
<const char *> ArgsArr
= llvm::makeArrayRef(argv
+ 1, argc
);
3162 llvm::opt::InputArgList input_args
= T
.ParseArgs(ArgsArr
, MAI
, MAC
);
3164 if (input_args
.hasArg(OPT_help
)) {
3165 printHelp(T
, llvm::sys::path::filename(argv
[0]));
3166 return EXIT_SUCCESS
;
3169 if (llvm::opt::Arg
*target_arg
= input_args
.getLastArg(OPT_launch_target
)) {
3170 if (llvm::opt::Arg
*comm_file
= input_args
.getLastArg(OPT_comm_file
)) {
3171 int target_args_pos
= argc
;
3172 for (int i
= 0; i
< argc
; i
++)
3173 if (strcmp(argv
[i
], "--launch-target") == 0) {
3174 target_args_pos
= i
+ 1;
3177 LaunchRunInTerminalTarget(*target_arg
, comm_file
->getValue(),
3178 argv
+ target_args_pos
);
3180 llvm::errs() << "\"--launch-target\" requires \"--comm-file\" to be "
3182 return EXIT_FAILURE
;
3186 // stdout/stderr redirection to the IDE's console
3187 int new_stdout_fd
= SetupStdoutStderrRedirection();
3189 // Initialize LLDB first before we do anything.
3190 lldb::SBDebugger::Initialize();
3192 // Terminate the debugger before the C++ destructor chain kicks in.
3193 auto terminate_debugger
=
3194 llvm::make_scope_exit([] { lldb::SBDebugger::Terminate(); });
3196 RegisterRequestCallbacks();
3200 if (auto *arg
= input_args
.getLastArg(OPT_port
)) {
3201 auto optarg
= arg
->getValue();
3203 portno
= strtol(optarg
, &remainder
, 0);
3204 if (remainder
== optarg
|| *remainder
!= '\0') {
3205 fprintf(stderr
, "'%s' is not a valid port number.\n", optarg
);
3206 return EXIT_FAILURE
;
3210 #if !defined(_WIN32)
3211 if (input_args
.hasArg(OPT_wait_for_debugger
)) {
3212 printf("Paused waiting for debugger to attach (pid = %i)...\n", getpid());
3217 printf("Listening on port %i...\n", portno
);
3218 SOCKET socket_fd
= AcceptConnection(portno
);
3219 if (socket_fd
>= 0) {
3220 g_vsc
.input
.descriptor
= StreamDescriptor::from_socket(socket_fd
, true);
3221 g_vsc
.output
.descriptor
= StreamDescriptor::from_socket(socket_fd
, false);
3223 return EXIT_FAILURE
;
3226 g_vsc
.input
.descriptor
= StreamDescriptor::from_file(fileno(stdin
), false);
3227 g_vsc
.output
.descriptor
= StreamDescriptor::from_file(new_stdout_fd
, false);
3230 uint32_t packet_idx
= 0;
3231 while (!g_vsc
.sent_terminated_event
) {
3232 llvm::json::Object object
;
3233 lldb_vscode::PacketStatus status
= g_vsc
.GetNextObject(object
);
3234 if (status
== lldb_vscode::PacketStatus::EndOfFile
)
3236 if (status
!= lldb_vscode::PacketStatus::Success
)
3237 return 1; // Fatal error
3239 if (!g_vsc
.HandleObject(object
))
3244 return EXIT_SUCCESS
;