1 //===-- lldb-vscode.cpp -----------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "VSCode.h"
10
11 #include <cassert>
12 #include <climits>
13 #include <cstdarg>
14 #include <cstdio>
15 #include <cstdlib>
16 #include <cstring>
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #if defined(_WIN32)
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
24 // against these.
25 #define NOMINMAX
26 #include <windows.h>
27 #undef GetObject
28 #include <io.h>
29 #else
30 #include <netinet/in.h>
31 #include <sys/socket.h>
32 #include <unistd.h>
33 #endif
34
35 #include <algorithm>
36 #include <chrono>
37 #include <fstream>
38 #include <map>
39 #include <memory>
40 #include <mutex>
41 #include <set>
42 #include <sstream>
43 #include <thread>
44 #include <vector>
45
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"
58
59 #include "JSONUtils.h"
60 #include "LLDBUtils.h"
61 #include "OutputRedirector.h"
62
63 #if defined(_WIN32)
64 #ifndef PATH_MAX
65 #define PATH_MAX MAX_PATH
66 #endif
67 typedef int socklen_t;
68 #endif
69
70 using namespace lldb_vscode;
71
72 namespace {
73 enum ID {
74 OPT_INVALID = 0, // This is not an option ID.
75 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
76 HELPTEXT, METAVAR, VALUES) \
77 OPT_##ID,
78 #include "Options.inc"
79 #undef OPTION
80 };
81
82 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
83 #include "Options.inc"
84 #undef PREFIX
85
86 static const llvm::opt::OptTable::Info InfoTable[] = {
87 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
88 HELPTEXT, METAVAR, VALUES) \
89 {PREFIX, NAME, HELPTEXT, \
90 METAVAR, OPT_##ID, llvm::opt::Option::KIND##Class, \
91 PARAM, FLAGS, OPT_##GROUP, \
92 OPT_##ALIAS, ALIASARGS, VALUES},
93 #include "Options.inc"
94 #undef OPTION
95 };
96 class LLDBVSCodeOptTable : public llvm::opt::OptTable {
97 public:
LLDBVSCodeOptTable()98 LLDBVSCodeOptTable() : OptTable(InfoTable, true) {}
99 };
100
101 typedef void (*RequestCallback)(const llvm::json::Object &command);
102
103 enum LaunchMethod { Launch, Attach, AttachForSuspendedLaunch };
104
GetTopLevelScope(int64_t variablesReference)105 lldb::SBValueList *GetTopLevelScope(int64_t variablesReference) {
106 switch (variablesReference) {
107 case VARREF_LOCALS:
108 return &g_vsc.variables.locals;
109 case VARREF_GLOBALS:
110 return &g_vsc.variables.globals;
111 case VARREF_REGS:
112 return &g_vsc.variables.registers;
113 default:
114 return nullptr;
115 }
116 }
117
AcceptConnection(int portno)118 SOCKET AcceptConnection(int portno) {
119 // Accept a socket connection from any host on "portno".
120 SOCKET newsockfd = -1;
121 struct sockaddr_in serv_addr, cli_addr;
122 SOCKET sockfd = socket(AF_INET, SOCK_STREAM, 0);
123 if (sockfd < 0) {
124 if (g_vsc.log)
125 *g_vsc.log << "error: opening socket (" << strerror(errno) << ")"
126 << std::endl;
127 } else {
128 memset((char *)&serv_addr, 0, sizeof(serv_addr));
129 serv_addr.sin_family = AF_INET;
130 // serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
131 serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
132 serv_addr.sin_port = htons(portno);
133 if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
134 if (g_vsc.log)
135 *g_vsc.log << "error: binding socket (" << strerror(errno) << ")"
136 << std::endl;
137 } else {
138 listen(sockfd, 5);
139 socklen_t clilen = sizeof(cli_addr);
140 newsockfd =
141 llvm::sys::RetryAfterSignal(static_cast<SOCKET>(-1), accept, sockfd,
142 (struct sockaddr *)&cli_addr, &clilen);
143 if (newsockfd < 0)
144 if (g_vsc.log)
145 *g_vsc.log << "error: accept (" << strerror(errno) << ")"
146 << std::endl;
147 }
148 #if defined(_WIN32)
149 closesocket(sockfd);
150 #else
151 close(sockfd);
152 #endif
153 }
154 return newsockfd;
155 }
156
MakeArgv(const llvm::ArrayRef<std::string> & strs)157 std::vector<const char *> MakeArgv(const llvm::ArrayRef<std::string> &strs) {
158 // Create and return an array of "const char *", one for each C string in
159 // "strs" and terminate the list with a NULL. This can be used for argument
160 // vectors (argv) or environment vectors (envp) like those passed to the
161 // "main" function in C programs.
162 std::vector<const char *> argv;
163 for (const auto &s : strs)
164 argv.push_back(s.c_str());
165 argv.push_back(nullptr);
166 return argv;
167 }
168
169 // Send a "exited" event to indicate the process has exited.
SendProcessExitedEvent(lldb::SBProcess & process)170 void SendProcessExitedEvent(lldb::SBProcess &process) {
171 llvm::json::Object event(CreateEventObject("exited"));
172 llvm::json::Object body;
173 body.try_emplace("exitCode", (int64_t)process.GetExitStatus());
174 event.try_emplace("body", std::move(body));
175 g_vsc.SendJSON(llvm::json::Value(std::move(event)));
176 }
177
SendThreadExitedEvent(lldb::tid_t tid)178 void SendThreadExitedEvent(lldb::tid_t tid) {
179 llvm::json::Object event(CreateEventObject("thread"));
180 llvm::json::Object body;
181 body.try_emplace("reason", "exited");
182 body.try_emplace("threadId", (int64_t)tid);
183 event.try_emplace("body", std::move(body));
184 g_vsc.SendJSON(llvm::json::Value(std::move(event)));
185 }
186
187 // Send a "terminated" event to indicate the process is done being
188 // debugged.
SendTerminatedEvent()189 void SendTerminatedEvent() {
190 // If an inferior exits prior to the processing of a disconnect request, then
191 // the threads executing EventThreadFunction and request_discontinue
192 // respectively may call SendTerminatedEvent simultaneously. Without any
193 // synchronization, the thread executing EventThreadFunction may set
194 // g_vsc.sent_terminated_event before the thread executing
195 // request_discontinue has had a chance to test it, in which case the latter
196 // would move ahead to issue a response to the disconnect request. Said
197 // response may get dispatched ahead of the terminated event compelling the
198 // client to terminate the debug session without consuming any console output
199 // that might've been generated by the execution of terminateCommands. So,
200 // synchronize simultaneous calls to SendTerminatedEvent.
201 static std::mutex mutex;
202 std::lock_guard<std::mutex> locker(mutex);
203 if (!g_vsc.sent_terminated_event) {
204 g_vsc.sent_terminated_event = true;
205 g_vsc.RunTerminateCommands();
206 // Send a "terminated" event
207 llvm::json::Object event(CreateEventObject("terminated"));
208 g_vsc.SendJSON(llvm::json::Value(std::move(event)));
209 }
210 }
211
212 // Send a thread stopped event for all threads as long as the process
213 // is stopped.
SendThreadStoppedEvent()214 void SendThreadStoppedEvent() {
215 lldb::SBProcess process = g_vsc.target.GetProcess();
216 if (process.IsValid()) {
217 auto state = process.GetState();
218 if (state == lldb::eStateStopped) {
219 llvm::DenseSet<lldb::tid_t> old_thread_ids;
220 old_thread_ids.swap(g_vsc.thread_ids);
221 uint32_t stop_id = process.GetStopID();
222 const uint32_t num_threads = process.GetNumThreads();
223
224 // First make a pass through the threads to see if the focused thread
225 // has a stop reason. In case the focus thread doesn't have a stop
226 // reason, remember the first thread that has a stop reason so we can
227 // set it as the focus thread if below if needed.
228 lldb::tid_t first_tid_with_reason = LLDB_INVALID_THREAD_ID;
229 uint32_t num_threads_with_reason = 0;
230 bool focus_thread_exists = false;
231 for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) {
232 lldb::SBThread thread = process.GetThreadAtIndex(thread_idx);
233 const lldb::tid_t tid = thread.GetThreadID();
234 const bool has_reason = ThreadHasStopReason(thread);
235 // If the focus thread doesn't have a stop reason, clear the thread ID
236 if (tid == g_vsc.focus_tid) {
237 focus_thread_exists = true;
238 if (!has_reason)
239 g_vsc.focus_tid = LLDB_INVALID_THREAD_ID;
240 }
241 if (has_reason) {
242 ++num_threads_with_reason;
243 if (first_tid_with_reason == LLDB_INVALID_THREAD_ID)
244 first_tid_with_reason = tid;
245 }
246 }
247
248 // We will have cleared g_vsc.focus_tid if he focus thread doesn't have
249 // a stop reason, so if it was cleared, or wasn't set, or doesn't exist,
250 // then set the focus thread to the first thread with a stop reason.
251 if (!focus_thread_exists || g_vsc.focus_tid == LLDB_INVALID_THREAD_ID)
252 g_vsc.focus_tid = first_tid_with_reason;
253
254 // If no threads stopped with a reason, then report the first one so
255 // we at least let the UI know we stopped.
256 if (num_threads_with_reason == 0) {
257 lldb::SBThread thread = process.GetThreadAtIndex(0);
258 g_vsc.SendJSON(CreateThreadStopped(thread, stop_id));
259 } else {
260 for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) {
261 lldb::SBThread thread = process.GetThreadAtIndex(thread_idx);
262 g_vsc.thread_ids.insert(thread.GetThreadID());
263 if (ThreadHasStopReason(thread)) {
264 g_vsc.SendJSON(CreateThreadStopped(thread, stop_id));
265 }
266 }
267 }
268
269 for (auto tid : old_thread_ids) {
270 auto end = g_vsc.thread_ids.end();
271 auto pos = g_vsc.thread_ids.find(tid);
272 if (pos == end)
273 SendThreadExitedEvent(tid);
274 }
275 } else {
276 if (g_vsc.log)
277 *g_vsc.log << "error: SendThreadStoppedEvent() when process"
278 " isn't stopped ("
279 << lldb::SBDebugger::StateAsCString(state) << ')'
280 << std::endl;
281 }
282 } else {
283 if (g_vsc.log)
284 *g_vsc.log << "error: SendThreadStoppedEvent() invalid process"
285 << std::endl;
286 }
287 g_vsc.RunStopCommands();
288 }
289
290 // "ProcessEvent": {
291 // "allOf": [
292 // { "$ref": "#/definitions/Event" },
293 // {
294 // "type": "object",
295 // "description": "Event message for 'process' event type. The event
296 // indicates that the debugger has begun debugging a
297 // new process. Either one that it has launched, or one
298 // that it has attached to.",
299 // "properties": {
300 // "event": {
301 // "type": "string",
302 // "enum": [ "process" ]
303 // },
304 // "body": {
305 // "type": "object",
306 // "properties": {
307 // "name": {
308 // "type": "string",
309 // "description": "The logical name of the process. This is
310 // usually the full path to process's executable
311 // file. Example: /home/myproj/program.js."
312 // },
313 // "systemProcessId": {
314 // "type": "integer",
315 // "description": "The system process id of the debugged process.
316 // This property will be missing for non-system
317 // processes."
318 // },
319 // "isLocalProcess": {
320 // "type": "boolean",
321 // "description": "If true, the process is running on the same
322 // computer as the debug adapter."
323 // },
324 // "startMethod": {
325 // "type": "string",
326 // "enum": [ "launch", "attach", "attachForSuspendedLaunch" ],
327 // "description": "Describes how the debug engine started
328 // debugging this process.",
329 // "enumDescriptions": [
330 // "Process was launched under the debugger.",
331 // "Debugger attached to an existing process.",
332 // "A project launcher component has launched a new process in
333 // a suspended state and then asked the debugger to attach."
334 // ]
335 // }
336 // },
337 // "required": [ "name" ]
338 // }
339 // },
340 // "required": [ "event", "body" ]
341 // }
342 // ]
343 // }
SendProcessEvent(LaunchMethod launch_method)344 void SendProcessEvent(LaunchMethod launch_method) {
345 lldb::SBFileSpec exe_fspec = g_vsc.target.GetExecutable();
346 char exe_path[PATH_MAX];
347 exe_fspec.GetPath(exe_path, sizeof(exe_path));
348 llvm::json::Object event(CreateEventObject("process"));
349 llvm::json::Object body;
350 EmplaceSafeString(body, "name", std::string(exe_path));
351 const auto pid = g_vsc.target.GetProcess().GetProcessID();
352 body.try_emplace("systemProcessId", (int64_t)pid);
353 body.try_emplace("isLocalProcess", true);
354 const char *startMethod = nullptr;
355 switch (launch_method) {
356 case Launch:
357 startMethod = "launch";
358 break;
359 case Attach:
360 startMethod = "attach";
361 break;
362 case AttachForSuspendedLaunch:
363 startMethod = "attachForSuspendedLaunch";
364 break;
365 }
366 body.try_emplace("startMethod", startMethod);
367 event.try_emplace("body", std::move(body));
368 g_vsc.SendJSON(llvm::json::Value(std::move(event)));
369 }
370
371 // Grab any STDOUT and STDERR from the process and send it up to VS Code
372 // via an "output" event to the "stdout" and "stderr" categories.
SendStdOutStdErr(lldb::SBProcess & process)373 void SendStdOutStdErr(lldb::SBProcess &process) {
374 char buffer[1024];
375 size_t count;
376 while ((count = process.GetSTDOUT(buffer, sizeof(buffer))) > 0)
377 g_vsc.SendOutput(OutputType::Stdout, llvm::StringRef(buffer, count));
378 while ((count = process.GetSTDERR(buffer, sizeof(buffer))) > 0)
379 g_vsc.SendOutput(OutputType::Stderr, llvm::StringRef(buffer, count));
380 }
381
ProgressEventThreadFunction()382 void ProgressEventThreadFunction() {
383 lldb::SBListener listener("lldb-vscode.progress.listener");
384 g_vsc.debugger.GetBroadcaster().AddListener(
385 listener, lldb::SBDebugger::eBroadcastBitProgress);
386 g_vsc.broadcaster.AddListener(listener, eBroadcastBitStopProgressThread);
387 lldb::SBEvent event;
388 bool done = false;
389 while (!done) {
390 if (listener.WaitForEvent(1, event)) {
391 const auto event_mask = event.GetType();
392 if (event.BroadcasterMatchesRef(g_vsc.broadcaster)) {
393 if (event_mask & eBroadcastBitStopProgressThread) {
394 done = true;
395 }
396 } else {
397 uint64_t progress_id = 0;
398 uint64_t completed = 0;
399 uint64_t total = 0;
400 bool is_debugger_specific = false;
401 const char *message = lldb::SBDebugger::GetProgressFromEvent(
402 event, progress_id, completed, total, is_debugger_specific);
403 if (message)
404 g_vsc.SendProgressEvent(progress_id, message, completed, total);
405 }
406 }
407 }
408 }
409
410 // All events from the debugger, target, process, thread and frames are
411 // received in this function that runs in its own thread. We are using a
412 // "FILE *" to output packets back to VS Code and they have mutexes in them
413 // them prevent multiple threads from writing simultaneously so no locking
414 // is required.
EventThreadFunction()415 void EventThreadFunction() {
416 lldb::SBEvent event;
417 lldb::SBListener listener = g_vsc.debugger.GetListener();
418 bool done = false;
419 while (!done) {
420 if (listener.WaitForEvent(1, event)) {
421 const auto event_mask = event.GetType();
422 if (lldb::SBProcess::EventIsProcessEvent(event)) {
423 lldb::SBProcess process = lldb::SBProcess::GetProcessFromEvent(event);
424 if (event_mask & lldb::SBProcess::eBroadcastBitStateChanged) {
425 auto state = lldb::SBProcess::GetStateFromEvent(event);
426 switch (state) {
427 case lldb::eStateInvalid:
428 // Not a state event
429 break;
430 case lldb::eStateUnloaded:
431 break;
432 case lldb::eStateConnected:
433 break;
434 case lldb::eStateAttaching:
435 break;
436 case lldb::eStateLaunching:
437 break;
438 case lldb::eStateStepping:
439 break;
440 case lldb::eStateCrashed:
441 break;
442 case lldb::eStateDetached:
443 break;
444 case lldb::eStateSuspended:
445 break;
446 case lldb::eStateStopped:
447 // We launch and attach in synchronous mode then the first stop
448 // event will not be delivered. If we use "launchCommands" during a
449 // launch or "attachCommands" during an attach we might some process
450 // stop events which we do not want to send an event for. We will
451 // manually send a stopped event in request_configurationDone(...)
452 // so don't send any before then.
453 if (g_vsc.configuration_done_sent) {
454 // Only report a stopped event if the process was not restarted.
455 if (!lldb::SBProcess::GetRestartedFromEvent(event)) {
456 SendStdOutStdErr(process);
457 SendThreadStoppedEvent();
458 }
459 }
460 break;
461 case lldb::eStateRunning:
462 g_vsc.WillContinue();
463 break;
464 case lldb::eStateExited: {
465 // Run any exit LLDB commands the user specified in the
466 // launch.json
467 g_vsc.RunExitCommands();
468 SendProcessExitedEvent(process);
469 SendTerminatedEvent();
470 done = true;
471 } break;
472 }
473 } else if ((event_mask & lldb::SBProcess::eBroadcastBitSTDOUT) ||
474 (event_mask & lldb::SBProcess::eBroadcastBitSTDERR)) {
475 SendStdOutStdErr(process);
476 }
477 } else if (lldb::SBBreakpoint::EventIsBreakpointEvent(event)) {
478 if (event_mask & lldb::SBTarget::eBroadcastBitBreakpointChanged) {
479 auto event_type =
480 lldb::SBBreakpoint::GetBreakpointEventTypeFromEvent(event);
481 auto bp = lldb::SBBreakpoint::GetBreakpointFromEvent(event);
482 // If the breakpoint was originated from the IDE, it will have the
483 // BreakpointBase::GetBreakpointLabel() label attached. Regardless
484 // of wether the locations were added or removed, the breakpoint
485 // ins't going away, so we the reason is always "changed".
486 if ((event_type & lldb::eBreakpointEventTypeLocationsAdded ||
487 event_type & lldb::eBreakpointEventTypeLocationsRemoved) &&
488 bp.MatchesName(BreakpointBase::GetBreakpointLabel())) {
489 auto bp_event = CreateEventObject("breakpoint");
490 llvm::json::Object body;
491 // As VSCode already knows the path of this breakpoint, we don't
492 // need to send it back as part of a "changed" event. This
493 // prevent us from sending to VSCode paths that should be source
494 // mapped. Note that CreateBreakpoint doesn't apply source mapping.
495 // Besides, the current implementation of VSCode ignores the
496 // "source" element of breakpoint events.
497 llvm::json::Value source_bp = CreateBreakpoint(bp);
498 source_bp.getAsObject()->erase("source");
499
500 body.try_emplace("breakpoint", source_bp);
501 body.try_emplace("reason", "changed");
502 bp_event.try_emplace("body", std::move(body));
503 g_vsc.SendJSON(llvm::json::Value(std::move(bp_event)));
504 }
505 }
506 } else if (event.BroadcasterMatchesRef(g_vsc.broadcaster)) {
507 if (event_mask & eBroadcastBitStopEventThread) {
508 done = true;
509 }
510 }
511 }
512 }
513 }
514
515 // Both attach and launch take a either a sourcePath or sourceMap
516 // argument (or neither), from which we need to set the target.source-map.
SetSourceMapFromArguments(const llvm::json::Object & arguments)517 void SetSourceMapFromArguments(const llvm::json::Object &arguments) {
518 const char *sourceMapHelp =
519 "source must be be an array of two-element arrays, "
520 "each containing a source and replacement path string.\n";
521
522 std::string sourceMapCommand;
523 llvm::raw_string_ostream strm(sourceMapCommand);
524 strm << "settings set target.source-map ";
525 auto sourcePath = GetString(arguments, "sourcePath");
526
527 // sourceMap is the new, more general form of sourcePath and overrides it.
528 auto sourceMap = arguments.getArray("sourceMap");
529 if (sourceMap) {
530 for (const auto &value : *sourceMap) {
531 auto mapping = value.getAsArray();
532 if (mapping == nullptr || mapping->size() != 2 ||
533 (*mapping)[0].kind() != llvm::json::Value::String ||
534 (*mapping)[1].kind() != llvm::json::Value::String) {
535 g_vsc.SendOutput(OutputType::Console, llvm::StringRef(sourceMapHelp));
536 return;
537 }
538 auto mapFrom = GetAsString((*mapping)[0]);
539 auto mapTo = GetAsString((*mapping)[1]);
540 strm << "\"" << mapFrom << "\" \"" << mapTo << "\" ";
541 }
542 } else {
543 if (ObjectContainsKey(arguments, "sourceMap")) {
544 g_vsc.SendOutput(OutputType::Console, llvm::StringRef(sourceMapHelp));
545 return;
546 }
547 if (sourcePath.empty())
548 return;
549 // Do any source remapping needed before we create our targets
550 strm << "\".\" \"" << sourcePath << "\"";
551 }
552 strm.flush();
553 if (!sourceMapCommand.empty()) {
554 g_vsc.RunLLDBCommands("Setting source map:", {sourceMapCommand});
555 }
556 }
557
558 // "AttachRequest": {
559 // "allOf": [ { "$ref": "#/definitions/Request" }, {
560 // "type": "object",
561 // "description": "Attach request; value of command field is 'attach'.",
562 // "properties": {
563 // "command": {
564 // "type": "string",
565 // "enum": [ "attach" ]
566 // },
567 // "arguments": {
568 // "$ref": "#/definitions/AttachRequestArguments"
569 // }
570 // },
571 // "required": [ "command", "arguments" ]
572 // }]
573 // },
574 // "AttachRequestArguments": {
575 // "type": "object",
576 // "description": "Arguments for 'attach' request.\nThe attach request has no
577 // standardized attributes."
578 // },
579 // "AttachResponse": {
580 // "allOf": [ { "$ref": "#/definitions/Response" }, {
581 // "type": "object",
582 // "description": "Response to 'attach' request. This is just an
583 // acknowledgement, so no body field is required."
584 // }]
585 // }
request_attach(const llvm::json::Object & request)586 void request_attach(const llvm::json::Object &request) {
587 g_vsc.is_attach = true;
588 llvm::json::Object response;
589 lldb::SBError error;
590 FillResponse(request, response);
591 lldb::SBAttachInfo attach_info;
592 auto arguments = request.getObject("arguments");
593 const lldb::pid_t pid =
594 GetUnsigned(arguments, "pid", LLDB_INVALID_PROCESS_ID);
595 if (pid != LLDB_INVALID_PROCESS_ID)
596 attach_info.SetProcessID(pid);
597 const auto wait_for = GetBoolean(arguments, "waitFor", false);
598 attach_info.SetWaitForLaunch(wait_for, false /*async*/);
599 g_vsc.init_commands = GetStrings(arguments, "initCommands");
600 g_vsc.pre_run_commands = GetStrings(arguments, "preRunCommands");
601 g_vsc.stop_commands = GetStrings(arguments, "stopCommands");
602 g_vsc.exit_commands = GetStrings(arguments, "exitCommands");
603 g_vsc.terminate_commands = GetStrings(arguments, "terminateCommands");
604 auto attachCommands = GetStrings(arguments, "attachCommands");
605 llvm::StringRef core_file = GetString(arguments, "coreFile");
606 const uint64_t timeout_seconds = GetUnsigned(arguments, "timeout", 30);
607 g_vsc.stop_at_entry =
608 core_file.empty() ? GetBoolean(arguments, "stopOnEntry", false) : true;
609 std::vector<std::string> postRunCommands =
610 GetStrings(arguments, "postRunCommands");
611 const llvm::StringRef debuggerRoot = GetString(arguments, "debuggerRoot");
612
613 // This is a hack for loading DWARF in .o files on Mac where the .o files
614 // in the debug map of the main executable have relative paths which require
615 // the lldb-vscode binary to have its working directory set to that relative
616 // root for the .o files in order to be able to load debug info.
617 if (!debuggerRoot.empty())
618 llvm::sys::fs::set_current_path(debuggerRoot);
619
620 // Run any initialize LLDB commands the user specified in the launch.json
621 g_vsc.RunInitCommands();
622
623 SetSourceMapFromArguments(*arguments);
624
625 lldb::SBError status;
626 g_vsc.SetTarget(g_vsc.CreateTargetFromArguments(*arguments, status));
627 if (status.Fail()) {
628 response["success"] = llvm::json::Value(false);
629 EmplaceSafeString(response, "message", status.GetCString());
630 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
631 return;
632 }
633
634 // Run any pre run LLDB commands the user specified in the launch.json
635 g_vsc.RunPreRunCommands();
636
637 if (pid == LLDB_INVALID_PROCESS_ID && wait_for) {
638 char attach_msg[256];
639 auto attach_msg_len = snprintf(attach_msg, sizeof(attach_msg),
640 "Waiting to attach to \"%s\"...",
641 g_vsc.target.GetExecutable().GetFilename());
642 g_vsc.SendOutput(OutputType::Console,
643 llvm::StringRef(attach_msg, attach_msg_len));
644 }
645 if (attachCommands.empty()) {
646 // No "attachCommands", just attach normally.
647 // Disable async events so the attach will be successful when we return from
648 // the launch call and the launch will happen synchronously
649 g_vsc.debugger.SetAsync(false);
650 if (core_file.empty())
651 g_vsc.target.Attach(attach_info, error);
652 else
653 g_vsc.target.LoadCore(core_file.data(), error);
654 // Reenable async events
655 g_vsc.debugger.SetAsync(true);
656 } else {
657 // We have "attachCommands" that are a set of commands that are expected
658 // to execute the commands after which a process should be created. If there
659 // is no valid process after running these commands, we have failed.
660 g_vsc.RunLLDBCommands("Running attachCommands:", attachCommands);
661 // The custom commands might have created a new target so we should use the
662 // selected target after these commands are run.
663 g_vsc.target = g_vsc.debugger.GetSelectedTarget();
664
665 // Make sure the process is attached and stopped before proceeding as the
666 // the launch commands are not run using the synchronous mode.
667 error = g_vsc.WaitForProcessToStop(timeout_seconds);
668 }
669
670 if (error.Success() && core_file.empty()) {
671 auto attached_pid = g_vsc.target.GetProcess().GetProcessID();
672 if (attached_pid == LLDB_INVALID_PROCESS_ID) {
673 if (attachCommands.empty())
674 error.SetErrorString("failed to attach to a process");
675 else
676 error.SetErrorString("attachCommands failed to attach to a process");
677 }
678 }
679
680 if (error.Fail()) {
681 response["success"] = llvm::json::Value(false);
682 EmplaceSafeString(response, "message", std::string(error.GetCString()));
683 } else {
684 g_vsc.RunLLDBCommands("Running postRunCommands:", postRunCommands);
685 }
686
687 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
688 if (error.Success()) {
689 SendProcessEvent(Attach);
690 g_vsc.SendJSON(CreateEventObject("initialized"));
691 }
692 }
693
694 // "ContinueRequest": {
695 // "allOf": [ { "$ref": "#/definitions/Request" }, {
696 // "type": "object",
697 // "description": "Continue request; value of command field is 'continue'.
698 // The request starts the debuggee to run again.",
699 // "properties": {
700 // "command": {
701 // "type": "string",
702 // "enum": [ "continue" ]
703 // },
704 // "arguments": {
705 // "$ref": "#/definitions/ContinueArguments"
706 // }
707 // },
708 // "required": [ "command", "arguments" ]
709 // }]
710 // },
711 // "ContinueArguments": {
712 // "type": "object",
713 // "description": "Arguments for 'continue' request.",
714 // "properties": {
715 // "threadId": {
716 // "type": "integer",
717 // "description": "Continue execution for the specified thread (if
718 // possible). If the backend cannot continue on a single
719 // thread but will continue on all threads, it should
720 // set the allThreadsContinued attribute in the response
721 // to true."
722 // }
723 // },
724 // "required": [ "threadId" ]
725 // },
726 // "ContinueResponse": {
727 // "allOf": [ { "$ref": "#/definitions/Response" }, {
728 // "type": "object",
729 // "description": "Response to 'continue' request.",
730 // "properties": {
731 // "body": {
732 // "type": "object",
733 // "properties": {
734 // "allThreadsContinued": {
735 // "type": "boolean",
736 // "description": "If true, the continue request has ignored the
737 // specified thread and continued all threads
738 // instead. If this attribute is missing a value
739 // of 'true' is assumed for backward
740 // compatibility."
741 // }
742 // }
743 // }
744 // },
745 // "required": [ "body" ]
746 // }]
747 // }
request_continue(const llvm::json::Object & request)748 void request_continue(const llvm::json::Object &request) {
749 llvm::json::Object response;
750 FillResponse(request, response);
751 lldb::SBProcess process = g_vsc.target.GetProcess();
752 auto arguments = request.getObject("arguments");
753 // Remember the thread ID that caused the resume so we can set the
754 // "threadCausedFocus" boolean value in the "stopped" events.
755 g_vsc.focus_tid = GetUnsigned(arguments, "threadId", LLDB_INVALID_THREAD_ID);
756 lldb::SBError error = process.Continue();
757 llvm::json::Object body;
758 body.try_emplace("allThreadsContinued", true);
759 response.try_emplace("body", std::move(body));
760 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
761 }
762
763 // "ConfigurationDoneRequest": {
764 // "allOf": [ { "$ref": "#/definitions/Request" }, {
765 // "type": "object",
766 // "description": "ConfigurationDone request; value of command field
767 // is 'configurationDone'.\nThe client of the debug protocol must
768 // send this request at the end of the sequence of configuration
769 // requests (which was started by the InitializedEvent).",
770 // "properties": {
771 // "command": {
772 // "type": "string",
773 // "enum": [ "configurationDone" ]
774 // },
775 // "arguments": {
776 // "$ref": "#/definitions/ConfigurationDoneArguments"
777 // }
778 // },
779 // "required": [ "command" ]
780 // }]
781 // },
782 // "ConfigurationDoneArguments": {
783 // "type": "object",
784 // "description": "Arguments for 'configurationDone' request.\nThe
785 // configurationDone request has no standardized attributes."
786 // },
787 // "ConfigurationDoneResponse": {
788 // "allOf": [ { "$ref": "#/definitions/Response" }, {
789 // "type": "object",
790 // "description": "Response to 'configurationDone' request. This is
791 // just an acknowledgement, so no body field is required."
792 // }]
793 // },
request_configurationDone(const llvm::json::Object & request)794 void request_configurationDone(const llvm::json::Object &request) {
795 llvm::json::Object response;
796 FillResponse(request, response);
797 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
798 g_vsc.configuration_done_sent = true;
799 if (g_vsc.stop_at_entry)
800 SendThreadStoppedEvent();
801 else
802 g_vsc.target.GetProcess().Continue();
803 }
804
805 // "DisconnectRequest": {
806 // "allOf": [ { "$ref": "#/definitions/Request" }, {
807 // "type": "object",
808 // "description": "Disconnect request; value of command field is
809 // 'disconnect'.",
810 // "properties": {
811 // "command": {
812 // "type": "string",
813 // "enum": [ "disconnect" ]
814 // },
815 // "arguments": {
816 // "$ref": "#/definitions/DisconnectArguments"
817 // }
818 // },
819 // "required": [ "command" ]
820 // }]
821 // },
822 // "DisconnectArguments": {
823 // "type": "object",
824 // "description": "Arguments for 'disconnect' request.",
825 // "properties": {
826 // "terminateDebuggee": {
827 // "type": "boolean",
828 // "description": "Indicates whether the debuggee should be terminated
829 // when the debugger is disconnected. If unspecified,
830 // the debug adapter is free to do whatever it thinks
831 // is best. A client can only rely on this attribute
832 // being properly honored if a debug adapter returns
833 // true for the 'supportTerminateDebuggee' capability."
834 // },
835 // "restart": {
836 // "type": "boolean",
837 // "description": "Indicates whether the debuggee should be restart
838 // the process."
839 // }
840 // }
841 // },
842 // "DisconnectResponse": {
843 // "allOf": [ { "$ref": "#/definitions/Response" }, {
844 // "type": "object",
845 // "description": "Response to 'disconnect' request. This is just an
846 // acknowledgement, so no body field is required."
847 // }]
848 // }
request_disconnect(const llvm::json::Object & request)849 void request_disconnect(const llvm::json::Object &request) {
850 llvm::json::Object response;
851 FillResponse(request, response);
852 auto arguments = request.getObject("arguments");
853
854 bool defaultTerminateDebuggee = g_vsc.is_attach ? false : true;
855 bool terminateDebuggee =
856 GetBoolean(arguments, "terminateDebuggee", defaultTerminateDebuggee);
857 lldb::SBProcess process = g_vsc.target.GetProcess();
858 auto state = process.GetState();
859 switch (state) {
860 case lldb::eStateInvalid:
861 case lldb::eStateUnloaded:
862 case lldb::eStateDetached:
863 case lldb::eStateExited:
864 break;
865 case lldb::eStateConnected:
866 case lldb::eStateAttaching:
867 case lldb::eStateLaunching:
868 case lldb::eStateStepping:
869 case lldb::eStateCrashed:
870 case lldb::eStateSuspended:
871 case lldb::eStateStopped:
872 case lldb::eStateRunning:
873 g_vsc.debugger.SetAsync(false);
874 lldb::SBError error = terminateDebuggee ? process.Kill() : process.Detach();
875 if (!error.Success())
876 response.try_emplace("error", error.GetCString());
877 g_vsc.debugger.SetAsync(true);
878 break;
879 }
880 SendTerminatedEvent();
881 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
882 if (g_vsc.event_thread.joinable()) {
883 g_vsc.broadcaster.BroadcastEventByType(eBroadcastBitStopEventThread);
884 g_vsc.event_thread.join();
885 }
886 if (g_vsc.progress_event_thread.joinable()) {
887 g_vsc.broadcaster.BroadcastEventByType(eBroadcastBitStopProgressThread);
888 g_vsc.progress_event_thread.join();
889 }
890 }
891
request_exceptionInfo(const llvm::json::Object & request)892 void request_exceptionInfo(const llvm::json::Object &request) {
893 llvm::json::Object response;
894 FillResponse(request, response);
895 auto arguments = request.getObject("arguments");
896 llvm::json::Object body;
897 lldb::SBThread thread = g_vsc.GetLLDBThread(*arguments);
898 if (thread.IsValid()) {
899 auto stopReason = thread.GetStopReason();
900 if (stopReason == lldb::eStopReasonSignal)
901 body.try_emplace("exceptionId", "signal");
902 else if (stopReason == lldb::eStopReasonBreakpoint) {
903 ExceptionBreakpoint *exc_bp = g_vsc.GetExceptionBPFromStopReason(thread);
904 if (exc_bp) {
905 EmplaceSafeString(body, "exceptionId", exc_bp->filter);
906 EmplaceSafeString(body, "description", exc_bp->label);
907 } else {
908 body.try_emplace("exceptionId", "exception");
909 }
910 } else {
911 body.try_emplace("exceptionId", "exception");
912 }
913 if (!ObjectContainsKey(body, "description")) {
914 char description[1024];
915 if (thread.GetStopDescription(description, sizeof(description))) {
916 EmplaceSafeString(body, "description", std::string(description));
917 }
918 }
919 body.try_emplace("breakMode", "always");
920 // auto excInfoCount = thread.GetStopReasonDataCount();
921 // for (auto i=0; i<excInfoCount; ++i) {
922 // uint64_t exc_data = thread.GetStopReasonDataAtIndex(i);
923 // }
924 } else {
925 response["success"] = llvm::json::Value(false);
926 }
927 response.try_emplace("body", std::move(body));
928 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
929 }
930
931 // "CompletionsRequest": {
932 // "allOf": [ { "$ref": "#/definitions/Request" }, {
933 // "type": "object",
934 // "description": "Returns a list of possible completions for a given caret
935 // position and text.\nThe CompletionsRequest may only be called if the
936 // 'supportsCompletionsRequest' capability exists and is true.",
937 // "properties": {
938 // "command": {
939 // "type": "string",
940 // "enum": [ "completions" ]
941 // },
942 // "arguments": {
943 // "$ref": "#/definitions/CompletionsArguments"
944 // }
945 // },
946 // "required": [ "command", "arguments" ]
947 // }]
948 // },
949 // "CompletionsArguments": {
950 // "type": "object",
951 // "description": "Arguments for 'completions' request.",
952 // "properties": {
953 // "frameId": {
954 // "type": "integer",
955 // "description": "Returns completions in the scope of this stack frame.
956 // If not specified, the completions are returned for the global scope."
957 // },
958 // "text": {
959 // "type": "string",
960 // "description": "One or more source lines. Typically this is the text a
961 // user has typed into the debug console before he asked for completion."
962 // },
963 // "column": {
964 // "type": "integer",
965 // "description": "The character position for which to determine the
966 // completion proposals."
967 // },
968 // "line": {
969 // "type": "integer",
970 // "description": "An optional line for which to determine the completion
971 // proposals. If missing the first line of the text is assumed."
972 // }
973 // },
974 // "required": [ "text", "column" ]
975 // },
976 // "CompletionsResponse": {
977 // "allOf": [ { "$ref": "#/definitions/Response" }, {
978 // "type": "object",
979 // "description": "Response to 'completions' request.",
980 // "properties": {
981 // "body": {
982 // "type": "object",
983 // "properties": {
984 // "targets": {
985 // "type": "array",
986 // "items": {
987 // "$ref": "#/definitions/CompletionItem"
988 // },
989 // "description": "The possible completions for ."
990 // }
991 // },
992 // "required": [ "targets" ]
993 // }
994 // },
995 // "required": [ "body" ]
996 // }]
997 // },
998 // "CompletionItem": {
999 // "type": "object",
1000 // "description": "CompletionItems are the suggestions returned from the
1001 // CompletionsRequest.", "properties": {
1002 // "label": {
1003 // "type": "string",
1004 // "description": "The label of this completion item. By default this is
1005 // also the text that is inserted when selecting this completion."
1006 // },
1007 // "text": {
1008 // "type": "string",
1009 // "description": "If text is not falsy then it is inserted instead of the
1010 // label."
1011 // },
1012 // "sortText": {
1013 // "type": "string",
1014 // "description": "A string that should be used when comparing this item
1015 // with other items. When `falsy` the label is used."
1016 // },
1017 // "type": {
1018 // "$ref": "#/definitions/CompletionItemType",
1019 // "description": "The item's type. Typically the client uses this
1020 // information to render the item in the UI with an icon."
1021 // },
1022 // "start": {
1023 // "type": "integer",
1024 // "description": "This value determines the location (in the
1025 // CompletionsRequest's 'text' attribute) where the completion text is
1026 // added.\nIf missing the text is added at the location specified by the
1027 // CompletionsRequest's 'column' attribute."
1028 // },
1029 // "length": {
1030 // "type": "integer",
1031 // "description": "This value determines how many characters are
1032 // overwritten by the completion text.\nIf missing the value 0 is assumed
1033 // which results in the completion text being inserted."
1034 // }
1035 // },
1036 // "required": [ "label" ]
1037 // },
1038 // "CompletionItemType": {
1039 // "type": "string",
1040 // "description": "Some predefined types for the CompletionItem. Please note
1041 // that not all clients have specific icons for all of them.", "enum": [
1042 // "method", "function", "constructor", "field", "variable", "class",
1043 // "interface", "module", "property", "unit", "value", "enum", "keyword",
1044 // "snippet", "text", "color", "file", "reference", "customcolor" ]
1045 // }
request_completions(const llvm::json::Object & request)1046 void request_completions(const llvm::json::Object &request) {
1047 llvm::json::Object response;
1048 FillResponse(request, response);
1049 llvm::json::Object body;
1050 auto arguments = request.getObject("arguments");
1051 std::string text = std::string(GetString(arguments, "text"));
1052 auto original_column = GetSigned(arguments, "column", text.size());
1053 auto actual_column = original_column - 1;
1054 llvm::json::Array targets;
1055 // NOTE: the 'line' argument is not needed, as multiline expressions
1056 // work well already
1057 // TODO: support frameID. Currently
1058 // g_vsc.debugger.GetCommandInterpreter().HandleCompletionWithDescriptions
1059 // is frame-unaware.
1060
1061 if (!text.empty() && text[0] == '`') {
1062 text = text.substr(1);
1063 actual_column--;
1064 } else {
1065 text = "p " + text;
1066 actual_column += 2;
1067 }
1068 lldb::SBStringList matches;
1069 lldb::SBStringList descriptions;
1070 g_vsc.debugger.GetCommandInterpreter().HandleCompletionWithDescriptions(
1071 text.c_str(), actual_column, 0, -1, matches, descriptions);
1072 size_t count = std::min((uint32_t)100, matches.GetSize());
1073 targets.reserve(count);
1074 for (size_t i = 0; i < count; i++) {
1075 std::string match = matches.GetStringAtIndex(i);
1076 std::string description = descriptions.GetStringAtIndex(i);
1077
1078 llvm::json::Object item;
1079
1080 llvm::StringRef match_ref = match;
1081 for (llvm::StringRef commit_point : {".", "->"}) {
1082 if (match_ref.contains(commit_point)) {
1083 match_ref = match_ref.rsplit(commit_point).second;
1084 }
1085 }
1086 EmplaceSafeString(item, "text", match_ref);
1087
1088 if (description.empty())
1089 EmplaceSafeString(item, "label", match);
1090 else
1091 EmplaceSafeString(item, "label", match + " -- " + description);
1092
1093 targets.emplace_back(std::move(item));
1094 }
1095
1096 body.try_emplace("targets", std::move(targets));
1097 response.try_emplace("body", std::move(body));
1098 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
1099 }
1100
1101 // "EvaluateRequest": {
1102 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1103 // "type": "object",
1104 // "description": "Evaluate request; value of command field is 'evaluate'.
1105 // Evaluates the given expression in the context of the
1106 // top most stack frame. The expression has access to any
1107 // variables and arguments that are in scope.",
1108 // "properties": {
1109 // "command": {
1110 // "type": "string",
1111 // "enum": [ "evaluate" ]
1112 // },
1113 // "arguments": {
1114 // "$ref": "#/definitions/EvaluateArguments"
1115 // }
1116 // },
1117 // "required": [ "command", "arguments" ]
1118 // }]
1119 // },
1120 // "EvaluateArguments": {
1121 // "type": "object",
1122 // "description": "Arguments for 'evaluate' request.",
1123 // "properties": {
1124 // "expression": {
1125 // "type": "string",
1126 // "description": "The expression to evaluate."
1127 // },
1128 // "frameId": {
1129 // "type": "integer",
1130 // "description": "Evaluate the expression in the scope of this stack
1131 // frame. If not specified, the expression is evaluated
1132 // in the global scope."
1133 // },
1134 // "context": {
1135 // "type": "string",
1136 // "_enum": [ "watch", "repl", "hover" ],
1137 // "enumDescriptions": [
1138 // "evaluate is run in a watch.",
1139 // "evaluate is run from REPL console.",
1140 // "evaluate is run from a data hover."
1141 // ],
1142 // "description": "The context in which the evaluate request is run."
1143 // },
1144 // "format": {
1145 // "$ref": "#/definitions/ValueFormat",
1146 // "description": "Specifies details on how to format the Evaluate
1147 // result."
1148 // }
1149 // },
1150 // "required": [ "expression" ]
1151 // },
1152 // "EvaluateResponse": {
1153 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1154 // "type": "object",
1155 // "description": "Response to 'evaluate' request.",
1156 // "properties": {
1157 // "body": {
1158 // "type": "object",
1159 // "properties": {
1160 // "result": {
1161 // "type": "string",
1162 // "description": "The result of the evaluate request."
1163 // },
1164 // "type": {
1165 // "type": "string",
1166 // "description": "The optional type of the evaluate result."
1167 // },
1168 // "presentationHint": {
1169 // "$ref": "#/definitions/VariablePresentationHint",
1170 // "description": "Properties of a evaluate result that can be
1171 // used to determine how to render the result in
1172 // the UI."
1173 // },
1174 // "variablesReference": {
1175 // "type": "number",
1176 // "description": "If variablesReference is > 0, the evaluate
1177 // result is structured and its children can be
1178 // retrieved by passing variablesReference to the
1179 // VariablesRequest."
1180 // },
1181 // "namedVariables": {
1182 // "type": "number",
1183 // "description": "The number of named child variables. The
1184 // client can use this optional information to
1185 // present the variables in a paged UI and fetch
1186 // them in chunks."
1187 // },
1188 // "indexedVariables": {
1189 // "type": "number",
1190 // "description": "The number of indexed child variables. The
1191 // client can use this optional information to
1192 // present the variables in a paged UI and fetch
1193 // them in chunks."
1194 // }
1195 // },
1196 // "required": [ "result", "variablesReference" ]
1197 // }
1198 // },
1199 // "required": [ "body" ]
1200 // }]
1201 // }
request_evaluate(const llvm::json::Object & request)1202 void request_evaluate(const llvm::json::Object &request) {
1203 llvm::json::Object response;
1204 FillResponse(request, response);
1205 llvm::json::Object body;
1206 auto arguments = request.getObject("arguments");
1207 lldb::SBFrame frame = g_vsc.GetLLDBFrame(*arguments);
1208 const auto expression = GetString(arguments, "expression");
1209 llvm::StringRef context = GetString(arguments, "context");
1210
1211 if (!expression.empty() && expression[0] == '`') {
1212 auto result =
1213 RunLLDBCommands(llvm::StringRef(), {std::string(expression.substr(1))});
1214 EmplaceSafeString(body, "result", result);
1215 body.try_emplace("variablesReference", (int64_t)0);
1216 } else {
1217 // Always try to get the answer from the local variables if possible. If
1218 // this fails, then if the context is not "hover", actually evaluate an
1219 // expression using the expression parser.
1220 //
1221 // "frame variable" is more reliable than the expression parser in
1222 // many cases and it is faster.
1223 lldb::SBValue value = frame.GetValueForVariablePath(
1224 expression.data(), lldb::eDynamicDontRunTarget);
1225
1226 // Freeze dry the value in case users expand it later in the debug console
1227 if (value.GetError().Success() && context == "repl")
1228 value = value.Persist();
1229
1230 if (value.GetError().Fail() && context != "hover")
1231 value = frame.EvaluateExpression(expression.data());
1232
1233 if (value.GetError().Fail()) {
1234 response["success"] = llvm::json::Value(false);
1235 // This error object must live until we're done with the pointer returned
1236 // by GetCString().
1237 lldb::SBError error = value.GetError();
1238 const char *error_cstr = error.GetCString();
1239 if (error_cstr && error_cstr[0])
1240 EmplaceSafeString(response, "message", std::string(error_cstr));
1241 else
1242 EmplaceSafeString(response, "message", "evaluate failed");
1243 } else {
1244 SetValueForKey(value, body, "result");
1245 auto value_typename = value.GetType().GetDisplayTypeName();
1246 EmplaceSafeString(body, "type",
1247 value_typename ? value_typename : NO_TYPENAME);
1248 if (value.MightHaveChildren()) {
1249 auto variableReference = g_vsc.variables.InsertExpandableVariable(
1250 value, /*is_permanent=*/context == "repl");
1251 body.try_emplace("variablesReference", variableReference);
1252 } else {
1253 body.try_emplace("variablesReference", (int64_t)0);
1254 }
1255 }
1256 }
1257 response.try_emplace("body", std::move(body));
1258 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
1259 }
1260
1261 // "compileUnitsRequest": {
1262 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1263 // "type": "object",
1264 // "description": "Compile Unit request; value of command field is
1265 // 'compileUnits'.",
1266 // "properties": {
1267 // "command": {
1268 // "type": "string",
1269 // "enum": [ "compileUnits" ]
1270 // },
1271 // "arguments": {
1272 // "$ref": "#/definitions/compileUnitRequestArguments"
1273 // }
1274 // },
1275 // "required": [ "command", "arguments" ]
1276 // }]
1277 // },
1278 // "compileUnitsRequestArguments": {
1279 // "type": "object",
1280 // "description": "Arguments for 'compileUnits' request.",
1281 // "properties": {
1282 // "moduleId": {
1283 // "type": "string",
1284 // "description": "The ID of the module."
1285 // }
1286 // },
1287 // "required": [ "moduleId" ]
1288 // },
1289 // "compileUnitsResponse": {
1290 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1291 // "type": "object",
1292 // "description": "Response to 'compileUnits' request.",
1293 // "properties": {
1294 // "body": {
1295 // "description": "Response to 'compileUnits' request. Array of
1296 // paths of compile units."
1297 // }
1298 // }
1299 // }]
1300 // }
request_compileUnits(const llvm::json::Object & request)1301 void request_compileUnits(const llvm::json::Object &request) {
1302 llvm::json::Object response;
1303 FillResponse(request, response);
1304 llvm::json::Object body;
1305 llvm::json::Array units;
1306 auto arguments = request.getObject("arguments");
1307 std::string module_id = std::string(GetString(arguments, "moduleId"));
1308 int num_modules = g_vsc.target.GetNumModules();
1309 for (int i = 0; i < num_modules; i++) {
1310 auto curr_module = g_vsc.target.GetModuleAtIndex(i);
1311 if (module_id == curr_module.GetUUIDString()) {
1312 int num_units = curr_module.GetNumCompileUnits();
1313 for (int j = 0; j < num_units; j++) {
1314 auto curr_unit = curr_module.GetCompileUnitAtIndex(j);
1315 units.emplace_back(CreateCompileUnit(curr_unit));
1316 }
1317 body.try_emplace("compileUnits", std::move(units));
1318 break;
1319 }
1320 }
1321 response.try_emplace("body", std::move(body));
1322 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
1323 }
1324
1325 // "modulesRequest": {
1326 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1327 // "type": "object",
1328 // "description": "Modules request; value of command field is
1329 // 'modules'.",
1330 // "properties": {
1331 // "command": {
1332 // "type": "string",
1333 // "enum": [ "modules" ]
1334 // },
1335 // },
1336 // "required": [ "command" ]
1337 // }]
1338 // },
1339 // "modulesResponse": {
1340 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1341 // "type": "object",
1342 // "description": "Response to 'modules' request.",
1343 // "properties": {
1344 // "body": {
1345 // "description": "Response to 'modules' request. Array of
1346 // module objects."
1347 // }
1348 // }
1349 // }]
1350 // }
request_modules(const llvm::json::Object & request)1351 void request_modules(const llvm::json::Object &request) {
1352 llvm::json::Object response;
1353 FillResponse(request, response);
1354
1355 llvm::json::Array modules;
1356 for (size_t i = 0; i < g_vsc.target.GetNumModules(); i++) {
1357 lldb::SBModule module = g_vsc.target.GetModuleAtIndex(i);
1358 modules.emplace_back(CreateModule(module));
1359 }
1360
1361 llvm::json::Object body;
1362 body.try_emplace("modules", std::move(modules));
1363 response.try_emplace("body", std::move(body));
1364 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
1365 }
1366
1367 // "InitializeRequest": {
1368 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1369 // "type": "object",
1370 // "description": "Initialize request; value of command field is
1371 // 'initialize'.",
1372 // "properties": {
1373 // "command": {
1374 // "type": "string",
1375 // "enum": [ "initialize" ]
1376 // },
1377 // "arguments": {
1378 // "$ref": "#/definitions/InitializeRequestArguments"
1379 // }
1380 // },
1381 // "required": [ "command", "arguments" ]
1382 // }]
1383 // },
1384 // "InitializeRequestArguments": {
1385 // "type": "object",
1386 // "description": "Arguments for 'initialize' request.",
1387 // "properties": {
1388 // "clientID": {
1389 // "type": "string",
1390 // "description": "The ID of the (frontend) client using this adapter."
1391 // },
1392 // "adapterID": {
1393 // "type": "string",
1394 // "description": "The ID of the debug adapter."
1395 // },
1396 // "locale": {
1397 // "type": "string",
1398 // "description": "The ISO-639 locale of the (frontend) client using
1399 // this adapter, e.g. en-US or de-CH."
1400 // },
1401 // "linesStartAt1": {
1402 // "type": "boolean",
1403 // "description": "If true all line numbers are 1-based (default)."
1404 // },
1405 // "columnsStartAt1": {
1406 // "type": "boolean",
1407 // "description": "If true all column numbers are 1-based (default)."
1408 // },
1409 // "pathFormat": {
1410 // "type": "string",
1411 // "_enum": [ "path", "uri" ],
1412 // "description": "Determines in what format paths are specified. The
1413 // default is 'path', which is the native format."
1414 // },
1415 // "supportsVariableType": {
1416 // "type": "boolean",
1417 // "description": "Client supports the optional type attribute for
1418 // variables."
1419 // },
1420 // "supportsVariablePaging": {
1421 // "type": "boolean",
1422 // "description": "Client supports the paging of variables."
1423 // },
1424 // "supportsRunInTerminalRequest": {
1425 // "type": "boolean",
1426 // "description": "Client supports the runInTerminal request."
1427 // }
1428 // },
1429 // "required": [ "adapterID" ]
1430 // },
1431 // "InitializeResponse": {
1432 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1433 // "type": "object",
1434 // "description": "Response to 'initialize' request.",
1435 // "properties": {
1436 // "body": {
1437 // "$ref": "#/definitions/Capabilities",
1438 // "description": "The capabilities of this debug adapter."
1439 // }
1440 // }
1441 // }]
1442 // }
request_initialize(const llvm::json::Object & request)1443 void request_initialize(const llvm::json::Object &request) {
1444 auto log_cb = [](const char *buf, void *baton) -> void {
1445 g_vsc.SendOutput(OutputType::Console, llvm::StringRef{buf});
1446 };
1447 g_vsc.debugger =
1448 lldb::SBDebugger::Create(true /*source_init_files*/, log_cb, nullptr);
1449 g_vsc.progress_event_thread = std::thread(ProgressEventThreadFunction);
1450
1451 // Start our event thread so we can receive events from the debugger, target,
1452 // process and more.
1453 g_vsc.event_thread = std::thread(EventThreadFunction);
1454
1455 llvm::json::Object response;
1456 FillResponse(request, response);
1457 llvm::json::Object body;
1458 // The debug adapter supports the configurationDoneRequest.
1459 body.try_emplace("supportsConfigurationDoneRequest", true);
1460 // The debug adapter supports function breakpoints.
1461 body.try_emplace("supportsFunctionBreakpoints", true);
1462 // The debug adapter supports conditional breakpoints.
1463 body.try_emplace("supportsConditionalBreakpoints", true);
1464 // The debug adapter supports breakpoints that break execution after a
1465 // specified number of hits.
1466 body.try_emplace("supportsHitConditionalBreakpoints", true);
1467 // The debug adapter supports a (side effect free) evaluate request for
1468 // data hovers.
1469 body.try_emplace("supportsEvaluateForHovers", true);
1470 // Available filters or options for the setExceptionBreakpoints request.
1471 llvm::json::Array filters;
1472 for (const auto &exc_bp : g_vsc.exception_breakpoints) {
1473 filters.emplace_back(CreateExceptionBreakpointFilter(exc_bp));
1474 }
1475 body.try_emplace("exceptionBreakpointFilters", std::move(filters));
1476 // The debug adapter supports launching a debugee in intergrated VSCode
1477 // terminal.
1478 body.try_emplace("supportsRunInTerminalRequest", true);
1479 // The debug adapter supports stepping back via the stepBack and
1480 // reverseContinue requests.
1481 body.try_emplace("supportsStepBack", false);
1482 // The debug adapter supports setting a variable to a value.
1483 body.try_emplace("supportsSetVariable", true);
1484 // The debug adapter supports restarting a frame.
1485 body.try_emplace("supportsRestartFrame", false);
1486 // The debug adapter supports the gotoTargetsRequest.
1487 body.try_emplace("supportsGotoTargetsRequest", false);
1488 // The debug adapter supports the stepInTargetsRequest.
1489 body.try_emplace("supportsStepInTargetsRequest", false);
1490 // We need to improve the current implementation of completions in order to
1491 // enable it again. For some context, this is how VSCode works:
1492 // - VSCode sends a completion request whenever chars are added, the user
1493 // triggers completion manually via CTRL-space or similar mechanisms, but
1494 // not when there's a deletion. Besides, VSCode doesn't let us know which
1495 // of these events we are handling. What is more, the use can paste or cut
1496 // sections of the text arbitrarily.
1497 // https://github.com/microsoft/vscode/issues/89531 tracks part of the
1498 // issue just mentioned.
1499 // This behavior causes many problems with the current way completion is
1500 // implemented in lldb-vscode, as these requests could be really expensive,
1501 // blocking the debugger, and there could be many concurrent requests unless
1502 // the user types very slowly... We need to address this specific issue, or
1503 // at least trigger completion only when the user explicitly wants it, which
1504 // is the behavior of LLDB CLI, that expects a TAB.
1505 body.try_emplace("supportsCompletionsRequest", false);
1506 // The debug adapter supports the modules request.
1507 body.try_emplace("supportsModulesRequest", true);
1508 // The set of additional module information exposed by the debug adapter.
1509 // body.try_emplace("additionalModuleColumns"] = ColumnDescriptor
1510 // Checksum algorithms supported by the debug adapter.
1511 // body.try_emplace("supportedChecksumAlgorithms"] = ChecksumAlgorithm
1512 // The debug adapter supports the RestartRequest. In this case a client
1513 // should not implement 'restart' by terminating and relaunching the adapter
1514 // but by calling the RestartRequest.
1515 body.try_emplace("supportsRestartRequest", false);
1516 // The debug adapter supports 'exceptionOptions' on the
1517 // setExceptionBreakpoints request.
1518 body.try_emplace("supportsExceptionOptions", true);
1519 // The debug adapter supports a 'format' attribute on the stackTraceRequest,
1520 // variablesRequest, and evaluateRequest.
1521 body.try_emplace("supportsValueFormattingOptions", true);
1522 // The debug adapter supports the exceptionInfo request.
1523 body.try_emplace("supportsExceptionInfoRequest", true);
1524 // The debug adapter supports the 'terminateDebuggee' attribute on the
1525 // 'disconnect' request.
1526 body.try_emplace("supportTerminateDebuggee", true);
1527 // The debug adapter supports the delayed loading of parts of the stack,
1528 // which requires that both the 'startFrame' and 'levels' arguments and the
1529 // 'totalFrames' result of the 'StackTrace' request are supported.
1530 body.try_emplace("supportsDelayedStackTraceLoading", true);
1531 // The debug adapter supports the 'loadedSources' request.
1532 body.try_emplace("supportsLoadedSourcesRequest", false);
1533 // The debug adapter supports sending progress reporting events.
1534 body.try_emplace("supportsProgressReporting", true);
1535 // The debug adapter supports 'logMessage' in breakpoint.
1536 body.try_emplace("supportsLogPoints", true);
1537
1538 response.try_emplace("body", std::move(body));
1539 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
1540 }
1541
request_runInTerminal(const llvm::json::Object & launch_request)1542 llvm::Error request_runInTerminal(const llvm::json::Object &launch_request) {
1543 g_vsc.is_attach = true;
1544 lldb::SBAttachInfo attach_info;
1545
1546 llvm::Expected<std::shared_ptr<FifoFile>> comm_file_or_err =
1547 CreateRunInTerminalCommFile();
1548 if (!comm_file_or_err)
1549 return comm_file_or_err.takeError();
1550 FifoFile &comm_file = *comm_file_or_err.get();
1551
1552 RunInTerminalDebugAdapterCommChannel comm_channel(comm_file.m_path);
1553
1554 llvm::json::Object reverse_request = CreateRunInTerminalReverseRequest(
1555 launch_request, g_vsc.debug_adaptor_path, comm_file.m_path);
1556 llvm::json::Object reverse_response;
1557 lldb_vscode::PacketStatus status =
1558 g_vsc.SendReverseRequest(reverse_request, reverse_response);
1559 if (status != lldb_vscode::PacketStatus::Success)
1560 return llvm::createStringError(llvm::inconvertibleErrorCode(),
1561 "Process cannot be launched by the IDE. %s",
1562 comm_channel.GetLauncherError().c_str());
1563
1564 if (llvm::Expected<lldb::pid_t> pid = comm_channel.GetLauncherPid())
1565 attach_info.SetProcessID(*pid);
1566 else
1567 return pid.takeError();
1568
1569 g_vsc.debugger.SetAsync(false);
1570 lldb::SBError error;
1571 g_vsc.target.Attach(attach_info, error);
1572
1573 if (error.Fail())
1574 return llvm::createStringError(llvm::inconvertibleErrorCode(),
1575 "Failed to attach to the target process. %s",
1576 comm_channel.GetLauncherError().c_str());
1577 // This will notify the runInTerminal launcher that we attached.
1578 // We have to make this async, as the function won't return until the launcher
1579 // resumes and reads the data.
1580 std::future<lldb::SBError> did_attach_message_success =
1581 comm_channel.NotifyDidAttach();
1582
1583 // We just attached to the runInTerminal launcher, which was waiting to be
1584 // attached. We now resume it, so it can receive the didAttach notification
1585 // and then perform the exec. Upon continuing, the debugger will stop the
1586 // process right in the middle of the exec. To the user, what we are doing is
1587 // transparent, as they will only be able to see the process since the exec,
1588 // completely unaware of the preparatory work.
1589 g_vsc.target.GetProcess().Continue();
1590
1591 // Now that the actual target is just starting (i.e. exec was just invoked),
1592 // we return the debugger to its async state.
1593 g_vsc.debugger.SetAsync(true);
1594
1595 // If sending the notification failed, the launcher should be dead by now and
1596 // the async didAttach notification should have an error message, so we
1597 // return it. Otherwise, everything was a success.
1598 did_attach_message_success.wait();
1599 error = did_attach_message_success.get();
1600 if (error.Success())
1601 return llvm::Error::success();
1602 return llvm::createStringError(llvm::inconvertibleErrorCode(),
1603 error.GetCString());
1604 }
1605
1606 // "LaunchRequest": {
1607 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1608 // "type": "object",
1609 // "description": "Launch request; value of command field is 'launch'.",
1610 // "properties": {
1611 // "command": {
1612 // "type": "string",
1613 // "enum": [ "launch" ]
1614 // },
1615 // "arguments": {
1616 // "$ref": "#/definitions/LaunchRequestArguments"
1617 // }
1618 // },
1619 // "required": [ "command", "arguments" ]
1620 // }]
1621 // },
1622 // "LaunchRequestArguments": {
1623 // "type": "object",
1624 // "description": "Arguments for 'launch' request.",
1625 // "properties": {
1626 // "noDebug": {
1627 // "type": "boolean",
1628 // "description": "If noDebug is true the launch request should launch
1629 // the program without enabling debugging."
1630 // }
1631 // }
1632 // },
1633 // "LaunchResponse": {
1634 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1635 // "type": "object",
1636 // "description": "Response to 'launch' request. This is just an
1637 // acknowledgement, so no body field is required."
1638 // }]
1639 // }
request_launch(const llvm::json::Object & request)1640 void request_launch(const llvm::json::Object &request) {
1641 g_vsc.is_attach = false;
1642 llvm::json::Object response;
1643 lldb::SBError error;
1644 FillResponse(request, response);
1645 auto arguments = request.getObject("arguments");
1646 g_vsc.init_commands = GetStrings(arguments, "initCommands");
1647 g_vsc.pre_run_commands = GetStrings(arguments, "preRunCommands");
1648 g_vsc.stop_commands = GetStrings(arguments, "stopCommands");
1649 g_vsc.exit_commands = GetStrings(arguments, "exitCommands");
1650 g_vsc.terminate_commands = GetStrings(arguments, "terminateCommands");
1651 auto launchCommands = GetStrings(arguments, "launchCommands");
1652 std::vector<std::string> postRunCommands =
1653 GetStrings(arguments, "postRunCommands");
1654 g_vsc.stop_at_entry = GetBoolean(arguments, "stopOnEntry", false);
1655 const llvm::StringRef debuggerRoot = GetString(arguments, "debuggerRoot");
1656 const uint64_t timeout_seconds = GetUnsigned(arguments, "timeout", 30);
1657
1658 // This is a hack for loading DWARF in .o files on Mac where the .o files
1659 // in the debug map of the main executable have relative paths which require
1660 // the lldb-vscode binary to have its working directory set to that relative
1661 // root for the .o files in order to be able to load debug info.
1662 if (!debuggerRoot.empty())
1663 llvm::sys::fs::set_current_path(debuggerRoot);
1664
1665 // Run any initialize LLDB commands the user specified in the launch.json.
1666 // This is run before target is created, so commands can't do anything with
1667 // the targets - preRunCommands are run with the target.
1668 g_vsc.RunInitCommands();
1669
1670 SetSourceMapFromArguments(*arguments);
1671
1672 lldb::SBError status;
1673 g_vsc.SetTarget(g_vsc.CreateTargetFromArguments(*arguments, status));
1674 if (status.Fail()) {
1675 response["success"] = llvm::json::Value(false);
1676 EmplaceSafeString(response, "message", status.GetCString());
1677 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
1678 return;
1679 }
1680
1681 // Instantiate a launch info instance for the target.
1682 auto launch_info = g_vsc.target.GetLaunchInfo();
1683
1684 // Grab the current working directory if there is one and set it in the
1685 // launch info.
1686 const auto cwd = GetString(arguments, "cwd");
1687 if (!cwd.empty())
1688 launch_info.SetWorkingDirectory(cwd.data());
1689
1690 // Extract any extra arguments and append them to our program arguments for
1691 // when we launch
1692 auto args = GetStrings(arguments, "args");
1693 if (!args.empty())
1694 launch_info.SetArguments(MakeArgv(args).data(), true);
1695
1696 // Pass any environment variables along that the user specified.
1697 auto envs = GetStrings(arguments, "env");
1698 if (!envs.empty())
1699 launch_info.SetEnvironmentEntries(MakeArgv(envs).data(), true);
1700
1701 auto flags = launch_info.GetLaunchFlags();
1702
1703 if (GetBoolean(arguments, "disableASLR", true))
1704 flags |= lldb::eLaunchFlagDisableASLR;
1705 if (GetBoolean(arguments, "disableSTDIO", false))
1706 flags |= lldb::eLaunchFlagDisableSTDIO;
1707 if (GetBoolean(arguments, "shellExpandArguments", false))
1708 flags |= lldb::eLaunchFlagShellExpandArguments;
1709 const bool detatchOnError = GetBoolean(arguments, "detachOnError", false);
1710 launch_info.SetDetachOnError(detatchOnError);
1711 launch_info.SetLaunchFlags(flags | lldb::eLaunchFlagDebug |
1712 lldb::eLaunchFlagStopAtEntry);
1713
1714 // Run any pre run LLDB commands the user specified in the launch.json
1715 g_vsc.RunPreRunCommands();
1716
1717 if (GetBoolean(arguments, "runInTerminal", false)) {
1718 if (llvm::Error err = request_runInTerminal(request))
1719 error.SetErrorString(llvm::toString(std::move(err)).c_str());
1720 } else if (launchCommands.empty()) {
1721 // Disable async events so the launch will be successful when we return from
1722 // the launch call and the launch will happen synchronously
1723 g_vsc.debugger.SetAsync(false);
1724 g_vsc.target.Launch(launch_info, error);
1725 g_vsc.debugger.SetAsync(true);
1726 } else {
1727 g_vsc.RunLLDBCommands("Running launchCommands:", launchCommands);
1728 // The custom commands might have created a new target so we should use the
1729 // selected target after these commands are run.
1730 g_vsc.target = g_vsc.debugger.GetSelectedTarget();
1731 // Make sure the process is launched and stopped at the entry point before
1732 // proceeding as the the launch commands are not run using the synchronous
1733 // mode.
1734 error = g_vsc.WaitForProcessToStop(timeout_seconds);
1735 }
1736
1737 if (error.Fail()) {
1738 response["success"] = llvm::json::Value(false);
1739 EmplaceSafeString(response, "message", std::string(error.GetCString()));
1740 } else {
1741 g_vsc.RunLLDBCommands("Running postRunCommands:", postRunCommands);
1742 }
1743
1744 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
1745
1746 if (g_vsc.is_attach)
1747 SendProcessEvent(Attach); // this happens when doing runInTerminal
1748 else
1749 SendProcessEvent(Launch);
1750 g_vsc.SendJSON(llvm::json::Value(CreateEventObject("initialized")));
1751 }
1752
1753 // "NextRequest": {
1754 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1755 // "type": "object",
1756 // "description": "Next request; value of command field is 'next'. The
1757 // request starts the debuggee to run again for one step.
1758 // The debug adapter first sends the NextResponse and then
1759 // a StoppedEvent (event type 'step') after the step has
1760 // completed.",
1761 // "properties": {
1762 // "command": {
1763 // "type": "string",
1764 // "enum": [ "next" ]
1765 // },
1766 // "arguments": {
1767 // "$ref": "#/definitions/NextArguments"
1768 // }
1769 // },
1770 // "required": [ "command", "arguments" ]
1771 // }]
1772 // },
1773 // "NextArguments": {
1774 // "type": "object",
1775 // "description": "Arguments for 'next' request.",
1776 // "properties": {
1777 // "threadId": {
1778 // "type": "integer",
1779 // "description": "Execute 'next' for this thread."
1780 // }
1781 // },
1782 // "required": [ "threadId" ]
1783 // },
1784 // "NextResponse": {
1785 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1786 // "type": "object",
1787 // "description": "Response to 'next' request. This is just an
1788 // acknowledgement, so no body field is required."
1789 // }]
1790 // }
request_next(const llvm::json::Object & request)1791 void request_next(const llvm::json::Object &request) {
1792 llvm::json::Object response;
1793 FillResponse(request, response);
1794 auto arguments = request.getObject("arguments");
1795 lldb::SBThread thread = g_vsc.GetLLDBThread(*arguments);
1796 if (thread.IsValid()) {
1797 // Remember the thread ID that caused the resume so we can set the
1798 // "threadCausedFocus" boolean value in the "stopped" events.
1799 g_vsc.focus_tid = thread.GetThreadID();
1800 thread.StepOver();
1801 } else {
1802 response["success"] = llvm::json::Value(false);
1803 }
1804 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
1805 }
1806
1807 // "PauseRequest": {
1808 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1809 // "type": "object",
1810 // "description": "Pause request; value of command field is 'pause'. The
1811 // request suspenses the debuggee. The debug adapter first sends the
1812 // PauseResponse and then a StoppedEvent (event type 'pause') after the
1813 // thread has been paused successfully.", "properties": {
1814 // "command": {
1815 // "type": "string",
1816 // "enum": [ "pause" ]
1817 // },
1818 // "arguments": {
1819 // "$ref": "#/definitions/PauseArguments"
1820 // }
1821 // },
1822 // "required": [ "command", "arguments" ]
1823 // }]
1824 // },
1825 // "PauseArguments": {
1826 // "type": "object",
1827 // "description": "Arguments for 'pause' request.",
1828 // "properties": {
1829 // "threadId": {
1830 // "type": "integer",
1831 // "description": "Pause execution for this thread."
1832 // }
1833 // },
1834 // "required": [ "threadId" ]
1835 // },
1836 // "PauseResponse": {
1837 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1838 // "type": "object",
1839 // "description": "Response to 'pause' request. This is just an
1840 // acknowledgement, so no body field is required."
1841 // }]
1842 // }
request_pause(const llvm::json::Object & request)1843 void request_pause(const llvm::json::Object &request) {
1844 llvm::json::Object response;
1845 FillResponse(request, response);
1846 lldb::SBProcess process = g_vsc.target.GetProcess();
1847 lldb::SBError error = process.Stop();
1848 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
1849 }
1850
1851 // "ScopesRequest": {
1852 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1853 // "type": "object",
1854 // "description": "Scopes request; value of command field is 'scopes'. The
1855 // request returns the variable scopes for a given stackframe ID.",
1856 // "properties": {
1857 // "command": {
1858 // "type": "string",
1859 // "enum": [ "scopes" ]
1860 // },
1861 // "arguments": {
1862 // "$ref": "#/definitions/ScopesArguments"
1863 // }
1864 // },
1865 // "required": [ "command", "arguments" ]
1866 // }]
1867 // },
1868 // "ScopesArguments": {
1869 // "type": "object",
1870 // "description": "Arguments for 'scopes' request.",
1871 // "properties": {
1872 // "frameId": {
1873 // "type": "integer",
1874 // "description": "Retrieve the scopes for this stackframe."
1875 // }
1876 // },
1877 // "required": [ "frameId" ]
1878 // },
1879 // "ScopesResponse": {
1880 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1881 // "type": "object",
1882 // "description": "Response to 'scopes' request.",
1883 // "properties": {
1884 // "body": {
1885 // "type": "object",
1886 // "properties": {
1887 // "scopes": {
1888 // "type": "array",
1889 // "items": {
1890 // "$ref": "#/definitions/Scope"
1891 // },
1892 // "description": "The scopes of the stackframe. If the array has
1893 // length zero, there are no scopes available."
1894 // }
1895 // },
1896 // "required": [ "scopes" ]
1897 // }
1898 // },
1899 // "required": [ "body" ]
1900 // }]
1901 // }
request_scopes(const llvm::json::Object & request)1902 void request_scopes(const llvm::json::Object &request) {
1903 llvm::json::Object response;
1904 FillResponse(request, response);
1905 llvm::json::Object body;
1906 auto arguments = request.getObject("arguments");
1907 lldb::SBFrame frame = g_vsc.GetLLDBFrame(*arguments);
1908 // As the user selects different stack frames in the GUI, a "scopes" request
1909 // will be sent to the DAP. This is the only way we know that the user has
1910 // selected a frame in a thread. There are no other notifications that are
1911 // sent and VS code doesn't allow multiple frames to show variables
1912 // concurrently. If we select the thread and frame as the "scopes" requests
1913 // are sent, this allows users to type commands in the debugger console
1914 // with a backtick character to run lldb commands and these lldb commands
1915 // will now have the right context selected as they are run. If the user
1916 // types "`bt" into the debugger console and we had another thread selected
1917 // in the LLDB library, we would show the wrong thing to the user. If the
1918 // users switches threads with a lldb command like "`thread select 14", the
1919 // GUI will not update as there are no "event" notification packets that
1920 // allow us to change the currently selected thread or frame in the GUI that
1921 // I am aware of.
1922 if (frame.IsValid()) {
1923 frame.GetThread().GetProcess().SetSelectedThread(frame.GetThread());
1924 frame.GetThread().SetSelectedFrame(frame.GetFrameID());
1925 }
1926 g_vsc.variables.locals = frame.GetVariables(/*arguments=*/true,
1927 /*locals=*/true,
1928 /*statics=*/false,
1929 /*in_scope_only=*/true);
1930 g_vsc.variables.globals = frame.GetVariables(/*arguments=*/false,
1931 /*locals=*/false,
1932 /*statics=*/true,
1933 /*in_scope_only=*/true);
1934 g_vsc.variables.registers = frame.GetRegisters();
1935 body.try_emplace("scopes", g_vsc.CreateTopLevelScopes());
1936 response.try_emplace("body", std::move(body));
1937 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
1938 }
1939
1940 // "SetBreakpointsRequest": {
1941 // "allOf": [ { "$ref": "#/definitions/Request" }, {
1942 // "type": "object",
1943 // "description": "SetBreakpoints request; value of command field is
1944 // 'setBreakpoints'. Sets multiple breakpoints for a single source and
1945 // clears all previous breakpoints in that source. To clear all breakpoint
1946 // for a source, specify an empty array. When a breakpoint is hit, a
1947 // StoppedEvent (event type 'breakpoint') is generated.", "properties": {
1948 // "command": {
1949 // "type": "string",
1950 // "enum": [ "setBreakpoints" ]
1951 // },
1952 // "arguments": {
1953 // "$ref": "#/definitions/SetBreakpointsArguments"
1954 // }
1955 // },
1956 // "required": [ "command", "arguments" ]
1957 // }]
1958 // },
1959 // "SetBreakpointsArguments": {
1960 // "type": "object",
1961 // "description": "Arguments for 'setBreakpoints' request.",
1962 // "properties": {
1963 // "source": {
1964 // "$ref": "#/definitions/Source",
1965 // "description": "The source location of the breakpoints; either
1966 // source.path or source.reference must be specified."
1967 // },
1968 // "breakpoints": {
1969 // "type": "array",
1970 // "items": {
1971 // "$ref": "#/definitions/SourceBreakpoint"
1972 // },
1973 // "description": "The code locations of the breakpoints."
1974 // },
1975 // "lines": {
1976 // "type": "array",
1977 // "items": {
1978 // "type": "integer"
1979 // },
1980 // "description": "Deprecated: The code locations of the breakpoints."
1981 // },
1982 // "sourceModified": {
1983 // "type": "boolean",
1984 // "description": "A value of true indicates that the underlying source
1985 // has been modified which results in new breakpoint locations."
1986 // }
1987 // },
1988 // "required": [ "source" ]
1989 // },
1990 // "SetBreakpointsResponse": {
1991 // "allOf": [ { "$ref": "#/definitions/Response" }, {
1992 // "type": "object",
1993 // "description": "Response to 'setBreakpoints' request. Returned is
1994 // information about each breakpoint created by this request. This includes
1995 // the actual code location and whether the breakpoint could be verified.
1996 // The breakpoints returned are in the same order as the elements of the
1997 // 'breakpoints' (or the deprecated 'lines') in the
1998 // SetBreakpointsArguments.", "properties": {
1999 // "body": {
2000 // "type": "object",
2001 // "properties": {
2002 // "breakpoints": {
2003 // "type": "array",
2004 // "items": {
2005 // "$ref": "#/definitions/Breakpoint"
2006 // },
2007 // "description": "Information about the breakpoints. The array
2008 // elements are in the same order as the elements of the
2009 // 'breakpoints' (or the deprecated 'lines') in the
2010 // SetBreakpointsArguments."
2011 // }
2012 // },
2013 // "required": [ "breakpoints" ]
2014 // }
2015 // },
2016 // "required": [ "body" ]
2017 // }]
2018 // },
2019 // "SourceBreakpoint": {
2020 // "type": "object",
2021 // "description": "Properties of a breakpoint or logpoint passed to the
2022 // setBreakpoints request.", "properties": {
2023 // "line": {
2024 // "type": "integer",
2025 // "description": "The source line of the breakpoint or logpoint."
2026 // },
2027 // "column": {
2028 // "type": "integer",
2029 // "description": "An optional source column of the breakpoint."
2030 // },
2031 // "condition": {
2032 // "type": "string",
2033 // "description": "An optional expression for conditional breakpoints."
2034 // },
2035 // "hitCondition": {
2036 // "type": "string",
2037 // "description": "An optional expression that controls how many hits of
2038 // the breakpoint are ignored. The backend is expected to interpret the
2039 // expression as needed."
2040 // },
2041 // "logMessage": {
2042 // "type": "string",
2043 // "description": "If this attribute exists and is non-empty, the backend
2044 // must not 'break' (stop) but log the message instead. Expressions within
2045 // {} are interpolated."
2046 // }
2047 // },
2048 // "required": [ "line" ]
2049 // }
request_setBreakpoints(const llvm::json::Object & request)2050 void request_setBreakpoints(const llvm::json::Object &request) {
2051 llvm::json::Object response;
2052 lldb::SBError error;
2053 FillResponse(request, response);
2054 auto arguments = request.getObject("arguments");
2055 auto source = arguments->getObject("source");
2056 const auto path = GetString(source, "path");
2057 auto breakpoints = arguments->getArray("breakpoints");
2058 llvm::json::Array response_breakpoints;
2059
2060 // Decode the source breakpoint infos for this "setBreakpoints" request
2061 SourceBreakpointMap request_bps;
2062 // "breakpoints" may be unset, in which case we treat it the same as being set
2063 // to an empty array.
2064 if (breakpoints) {
2065 for (const auto &bp : *breakpoints) {
2066 auto bp_obj = bp.getAsObject();
2067 if (bp_obj) {
2068 SourceBreakpoint src_bp(*bp_obj);
2069 request_bps[src_bp.line] = src_bp;
2070
2071 // We check if this breakpoint already exists to update it
2072 auto existing_source_bps = g_vsc.source_breakpoints.find(path);
2073 if (existing_source_bps != g_vsc.source_breakpoints.end()) {
2074 const auto &existing_bp =
2075 existing_source_bps->second.find(src_bp.line);
2076 if (existing_bp != existing_source_bps->second.end()) {
2077 existing_bp->second.UpdateBreakpoint(src_bp);
2078 AppendBreakpoint(existing_bp->second.bp, response_breakpoints, path,
2079 src_bp.line);
2080 continue;
2081 }
2082 }
2083 // At this point the breakpoint is new
2084 g_vsc.source_breakpoints[path][src_bp.line] = src_bp;
2085 SourceBreakpoint &new_bp = g_vsc.source_breakpoints[path][src_bp.line];
2086 new_bp.SetBreakpoint(path.data());
2087 AppendBreakpoint(new_bp.bp, response_breakpoints, path, new_bp.line);
2088 }
2089 }
2090 }
2091
2092 // Delete any breakpoints in this source file that aren't in the
2093 // request_bps set. There is no call to remove breakpoints other than
2094 // calling this function with a smaller or empty "breakpoints" list.
2095 auto old_src_bp_pos = g_vsc.source_breakpoints.find(path);
2096 if (old_src_bp_pos != g_vsc.source_breakpoints.end()) {
2097 for (auto &old_bp : old_src_bp_pos->second) {
2098 auto request_pos = request_bps.find(old_bp.first);
2099 if (request_pos == request_bps.end()) {
2100 // This breakpoint no longer exists in this source file, delete it
2101 g_vsc.target.BreakpointDelete(old_bp.second.bp.GetID());
2102 old_src_bp_pos->second.erase(old_bp.first);
2103 }
2104 }
2105 }
2106
2107 llvm::json::Object body;
2108 body.try_emplace("breakpoints", std::move(response_breakpoints));
2109 response.try_emplace("body", std::move(body));
2110 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
2111 }
2112
2113 // "SetExceptionBreakpointsRequest": {
2114 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2115 // "type": "object",
2116 // "description": "SetExceptionBreakpoints request; value of command field
2117 // is 'setExceptionBreakpoints'. The request configures the debuggers
2118 // response to thrown exceptions. If an exception is configured to break, a
2119 // StoppedEvent is fired (event type 'exception').", "properties": {
2120 // "command": {
2121 // "type": "string",
2122 // "enum": [ "setExceptionBreakpoints" ]
2123 // },
2124 // "arguments": {
2125 // "$ref": "#/definitions/SetExceptionBreakpointsArguments"
2126 // }
2127 // },
2128 // "required": [ "command", "arguments" ]
2129 // }]
2130 // },
2131 // "SetExceptionBreakpointsArguments": {
2132 // "type": "object",
2133 // "description": "Arguments for 'setExceptionBreakpoints' request.",
2134 // "properties": {
2135 // "filters": {
2136 // "type": "array",
2137 // "items": {
2138 // "type": "string"
2139 // },
2140 // "description": "IDs of checked exception options. The set of IDs is
2141 // returned via the 'exceptionBreakpointFilters' capability."
2142 // },
2143 // "exceptionOptions": {
2144 // "type": "array",
2145 // "items": {
2146 // "$ref": "#/definitions/ExceptionOptions"
2147 // },
2148 // "description": "Configuration options for selected exceptions."
2149 // }
2150 // },
2151 // "required": [ "filters" ]
2152 // },
2153 // "SetExceptionBreakpointsResponse": {
2154 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2155 // "type": "object",
2156 // "description": "Response to 'setExceptionBreakpoints' request. This is
2157 // just an acknowledgement, so no body field is required."
2158 // }]
2159 // }
request_setExceptionBreakpoints(const llvm::json::Object & request)2160 void request_setExceptionBreakpoints(const llvm::json::Object &request) {
2161 llvm::json::Object response;
2162 lldb::SBError error;
2163 FillResponse(request, response);
2164 auto arguments = request.getObject("arguments");
2165 auto filters = arguments->getArray("filters");
2166 // Keep a list of any exception breakpoint filter names that weren't set
2167 // so we can clear any exception breakpoints if needed.
2168 std::set<std::string> unset_filters;
2169 for (const auto &bp : g_vsc.exception_breakpoints)
2170 unset_filters.insert(bp.filter);
2171
2172 for (const auto &value : *filters) {
2173 const auto filter = GetAsString(value);
2174 auto exc_bp = g_vsc.GetExceptionBreakpoint(std::string(filter));
2175 if (exc_bp) {
2176 exc_bp->SetBreakpoint();
2177 unset_filters.erase(std::string(filter));
2178 }
2179 }
2180 for (const auto &filter : unset_filters) {
2181 auto exc_bp = g_vsc.GetExceptionBreakpoint(filter);
2182 if (exc_bp)
2183 exc_bp->ClearBreakpoint();
2184 }
2185 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
2186 }
2187
2188 // "SetFunctionBreakpointsRequest": {
2189 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2190 // "type": "object",
2191 // "description": "SetFunctionBreakpoints request; value of command field is
2192 // 'setFunctionBreakpoints'. Sets multiple function breakpoints and clears
2193 // all previous function breakpoints. To clear all function breakpoint,
2194 // specify an empty array. When a function breakpoint is hit, a StoppedEvent
2195 // (event type 'function breakpoint') is generated.", "properties": {
2196 // "command": {
2197 // "type": "string",
2198 // "enum": [ "setFunctionBreakpoints" ]
2199 // },
2200 // "arguments": {
2201 // "$ref": "#/definitions/SetFunctionBreakpointsArguments"
2202 // }
2203 // },
2204 // "required": [ "command", "arguments" ]
2205 // }]
2206 // },
2207 // "SetFunctionBreakpointsArguments": {
2208 // "type": "object",
2209 // "description": "Arguments for 'setFunctionBreakpoints' request.",
2210 // "properties": {
2211 // "breakpoints": {
2212 // "type": "array",
2213 // "items": {
2214 // "$ref": "#/definitions/FunctionBreakpoint"
2215 // },
2216 // "description": "The function names of the breakpoints."
2217 // }
2218 // },
2219 // "required": [ "breakpoints" ]
2220 // },
2221 // "FunctionBreakpoint": {
2222 // "type": "object",
2223 // "description": "Properties of a breakpoint passed to the
2224 // setFunctionBreakpoints request.", "properties": {
2225 // "name": {
2226 // "type": "string",
2227 // "description": "The name of the function."
2228 // },
2229 // "condition": {
2230 // "type": "string",
2231 // "description": "An optional expression for conditional breakpoints."
2232 // },
2233 // "hitCondition": {
2234 // "type": "string",
2235 // "description": "An optional expression that controls how many hits of
2236 // the breakpoint are ignored. The backend is expected to interpret the
2237 // expression as needed."
2238 // }
2239 // },
2240 // "required": [ "name" ]
2241 // },
2242 // "SetFunctionBreakpointsResponse": {
2243 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2244 // "type": "object",
2245 // "description": "Response to 'setFunctionBreakpoints' request. Returned is
2246 // information about each breakpoint created by this request.",
2247 // "properties": {
2248 // "body": {
2249 // "type": "object",
2250 // "properties": {
2251 // "breakpoints": {
2252 // "type": "array",
2253 // "items": {
2254 // "$ref": "#/definitions/Breakpoint"
2255 // },
2256 // "description": "Information about the breakpoints. The array
2257 // elements correspond to the elements of the 'breakpoints' array."
2258 // }
2259 // },
2260 // "required": [ "breakpoints" ]
2261 // }
2262 // },
2263 // "required": [ "body" ]
2264 // }]
2265 // }
request_setFunctionBreakpoints(const llvm::json::Object & request)2266 void request_setFunctionBreakpoints(const llvm::json::Object &request) {
2267 llvm::json::Object response;
2268 lldb::SBError error;
2269 FillResponse(request, response);
2270 auto arguments = request.getObject("arguments");
2271 auto breakpoints = arguments->getArray("breakpoints");
2272 FunctionBreakpointMap request_bps;
2273 llvm::json::Array response_breakpoints;
2274 for (const auto &value : *breakpoints) {
2275 auto bp_obj = value.getAsObject();
2276 if (bp_obj == nullptr)
2277 continue;
2278 FunctionBreakpoint func_bp(*bp_obj);
2279 request_bps[func_bp.functionName] = std::move(func_bp);
2280 }
2281
2282 std::vector<llvm::StringRef> remove_names;
2283 // Disable any function breakpoints that aren't in the request_bps.
2284 // There is no call to remove function breakpoints other than calling this
2285 // function with a smaller or empty "breakpoints" list.
2286 for (auto &pair : g_vsc.function_breakpoints) {
2287 auto request_pos = request_bps.find(pair.first());
2288 if (request_pos == request_bps.end()) {
2289 // This function breakpoint no longer exists delete it from LLDB
2290 g_vsc.target.BreakpointDelete(pair.second.bp.GetID());
2291 remove_names.push_back(pair.first());
2292 } else {
2293 // Update the existing breakpoint as any setting withing the function
2294 // breakpoint might have changed.
2295 pair.second.UpdateBreakpoint(request_pos->second);
2296 // Remove this breakpoint from the request breakpoints since we have
2297 // handled it here and we don't need to set a new breakpoint below.
2298 request_bps.erase(request_pos);
2299 // Add this breakpoint info to the response
2300 AppendBreakpoint(pair.second.bp, response_breakpoints);
2301 }
2302 }
2303 // Remove any breakpoints that are no longer in our list
2304 for (const auto &name : remove_names)
2305 g_vsc.function_breakpoints.erase(name);
2306
2307 // Any breakpoints that are left in "request_bps" are breakpoints that
2308 // need to be set.
2309 for (auto &pair : request_bps) {
2310 // Add this breakpoint info to the response
2311 g_vsc.function_breakpoints[pair.first()] = std::move(pair.second);
2312 FunctionBreakpoint &new_bp = g_vsc.function_breakpoints[pair.first()];
2313 new_bp.SetBreakpoint();
2314 AppendBreakpoint(new_bp.bp, response_breakpoints);
2315 }
2316
2317 llvm::json::Object body;
2318 body.try_emplace("breakpoints", std::move(response_breakpoints));
2319 response.try_emplace("body", std::move(body));
2320 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
2321 }
2322
2323 // "SourceRequest": {
2324 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2325 // "type": "object",
2326 // "description": "Source request; value of command field is 'source'. The
2327 // request retrieves the source code for a given source reference.",
2328 // "properties": {
2329 // "command": {
2330 // "type": "string",
2331 // "enum": [ "source" ]
2332 // },
2333 // "arguments": {
2334 // "$ref": "#/definitions/SourceArguments"
2335 // }
2336 // },
2337 // "required": [ "command", "arguments" ]
2338 // }]
2339 // },
2340 // "SourceArguments": {
2341 // "type": "object",
2342 // "description": "Arguments for 'source' request.",
2343 // "properties": {
2344 // "source": {
2345 // "$ref": "#/definitions/Source",
2346 // "description": "Specifies the source content to load. Either
2347 // source.path or source.sourceReference must be specified."
2348 // },
2349 // "sourceReference": {
2350 // "type": "integer",
2351 // "description": "The reference to the source. This is the same as
2352 // source.sourceReference. This is provided for backward compatibility
2353 // since old backends do not understand the 'source' attribute."
2354 // }
2355 // },
2356 // "required": [ "sourceReference" ]
2357 // },
2358 // "SourceResponse": {
2359 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2360 // "type": "object",
2361 // "description": "Response to 'source' request.",
2362 // "properties": {
2363 // "body": {
2364 // "type": "object",
2365 // "properties": {
2366 // "content": {
2367 // "type": "string",
2368 // "description": "Content of the source reference."
2369 // },
2370 // "mimeType": {
2371 // "type": "string",
2372 // "description": "Optional content type (mime type) of the source."
2373 // }
2374 // },
2375 // "required": [ "content" ]
2376 // }
2377 // },
2378 // "required": [ "body" ]
2379 // }]
2380 // }
request_source(const llvm::json::Object & request)2381 void request_source(const llvm::json::Object &request) {
2382 llvm::json::Object response;
2383 FillResponse(request, response);
2384 llvm::json::Object body;
2385
2386 auto arguments = request.getObject("arguments");
2387 auto source = arguments->getObject("source");
2388 auto sourceReference = GetSigned(source, "sourceReference", -1);
2389 auto pos = g_vsc.source_map.find((lldb::addr_t)sourceReference);
2390 if (pos != g_vsc.source_map.end()) {
2391 EmplaceSafeString(body, "content", pos->second.content);
2392 } else {
2393 response["success"] = llvm::json::Value(false);
2394 }
2395 EmplaceSafeString(body, "mimeType", "text/x-lldb.disassembly");
2396 response.try_emplace("body", std::move(body));
2397 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
2398 }
2399
2400 // "StackTraceRequest": {
2401 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2402 // "type": "object",
2403 // "description": "StackTrace request; value of command field is
2404 // 'stackTrace'. The request returns a stacktrace from the current execution
2405 // state.", "properties": {
2406 // "command": {
2407 // "type": "string",
2408 // "enum": [ "stackTrace" ]
2409 // },
2410 // "arguments": {
2411 // "$ref": "#/definitions/StackTraceArguments"
2412 // }
2413 // },
2414 // "required": [ "command", "arguments" ]
2415 // }]
2416 // },
2417 // "StackTraceArguments": {
2418 // "type": "object",
2419 // "description": "Arguments for 'stackTrace' request.",
2420 // "properties": {
2421 // "threadId": {
2422 // "type": "integer",
2423 // "description": "Retrieve the stacktrace for this thread."
2424 // },
2425 // "startFrame": {
2426 // "type": "integer",
2427 // "description": "The index of the first frame to return; if omitted
2428 // frames start at 0."
2429 // },
2430 // "levels": {
2431 // "type": "integer",
2432 // "description": "The maximum number of frames to return. If levels is
2433 // not specified or 0, all frames are returned."
2434 // },
2435 // "format": {
2436 // "$ref": "#/definitions/StackFrameFormat",
2437 // "description": "Specifies details on how to format the stack frames."
2438 // }
2439 // },
2440 // "required": [ "threadId" ]
2441 // },
2442 // "StackTraceResponse": {
2443 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2444 // "type": "object",
2445 // "description": "Response to 'stackTrace' request.",
2446 // "properties": {
2447 // "body": {
2448 // "type": "object",
2449 // "properties": {
2450 // "stackFrames": {
2451 // "type": "array",
2452 // "items": {
2453 // "$ref": "#/definitions/StackFrame"
2454 // },
2455 // "description": "The frames of the stackframe. If the array has
2456 // length zero, there are no stackframes available. This means that
2457 // there is no location information available."
2458 // },
2459 // "totalFrames": {
2460 // "type": "integer",
2461 // "description": "The total number of frames available."
2462 // }
2463 // },
2464 // "required": [ "stackFrames" ]
2465 // }
2466 // },
2467 // "required": [ "body" ]
2468 // }]
2469 // }
request_stackTrace(const llvm::json::Object & request)2470 void request_stackTrace(const llvm::json::Object &request) {
2471 llvm::json::Object response;
2472 FillResponse(request, response);
2473 lldb::SBError error;
2474 auto arguments = request.getObject("arguments");
2475 lldb::SBThread thread = g_vsc.GetLLDBThread(*arguments);
2476 llvm::json::Array stackFrames;
2477 llvm::json::Object body;
2478
2479 if (thread.IsValid()) {
2480 const auto startFrame = GetUnsigned(arguments, "startFrame", 0);
2481 const auto levels = GetUnsigned(arguments, "levels", 0);
2482 const auto endFrame = (levels == 0) ? INT64_MAX : (startFrame + levels);
2483 for (uint32_t i = startFrame; i < endFrame; ++i) {
2484 auto frame = thread.GetFrameAtIndex(i);
2485 if (!frame.IsValid())
2486 break;
2487 stackFrames.emplace_back(CreateStackFrame(frame));
2488 }
2489 const auto totalFrames = thread.GetNumFrames();
2490 body.try_emplace("totalFrames", totalFrames);
2491 }
2492 body.try_emplace("stackFrames", std::move(stackFrames));
2493 response.try_emplace("body", std::move(body));
2494 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
2495 }
2496
2497 // "StepInRequest": {
2498 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2499 // "type": "object",
2500 // "description": "StepIn request; value of command field is 'stepIn'. The
2501 // request starts the debuggee to step into a function/method if possible.
2502 // If it cannot step into a target, 'stepIn' behaves like 'next'. The debug
2503 // adapter first sends the StepInResponse and then a StoppedEvent (event
2504 // type 'step') after the step has completed. If there are multiple
2505 // function/method calls (or other targets) on the source line, the optional
2506 // argument 'targetId' can be used to control into which target the 'stepIn'
2507 // should occur. The list of possible targets for a given source line can be
2508 // retrieved via the 'stepInTargets' request.", "properties": {
2509 // "command": {
2510 // "type": "string",
2511 // "enum": [ "stepIn" ]
2512 // },
2513 // "arguments": {
2514 // "$ref": "#/definitions/StepInArguments"
2515 // }
2516 // },
2517 // "required": [ "command", "arguments" ]
2518 // }]
2519 // },
2520 // "StepInArguments": {
2521 // "type": "object",
2522 // "description": "Arguments for 'stepIn' request.",
2523 // "properties": {
2524 // "threadId": {
2525 // "type": "integer",
2526 // "description": "Execute 'stepIn' for this thread."
2527 // },
2528 // "targetId": {
2529 // "type": "integer",
2530 // "description": "Optional id of the target to step into."
2531 // }
2532 // },
2533 // "required": [ "threadId" ]
2534 // },
2535 // "StepInResponse": {
2536 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2537 // "type": "object",
2538 // "description": "Response to 'stepIn' request. This is just an
2539 // acknowledgement, so no body field is required."
2540 // }]
2541 // }
request_stepIn(const llvm::json::Object & request)2542 void request_stepIn(const llvm::json::Object &request) {
2543 llvm::json::Object response;
2544 FillResponse(request, response);
2545 auto arguments = request.getObject("arguments");
2546 lldb::SBThread thread = g_vsc.GetLLDBThread(*arguments);
2547 if (thread.IsValid()) {
2548 // Remember the thread ID that caused the resume so we can set the
2549 // "threadCausedFocus" boolean value in the "stopped" events.
2550 g_vsc.focus_tid = thread.GetThreadID();
2551 thread.StepInto();
2552 } else {
2553 response["success"] = llvm::json::Value(false);
2554 }
2555 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
2556 }
2557
2558 // "StepOutRequest": {
2559 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2560 // "type": "object",
2561 // "description": "StepOut request; value of command field is 'stepOut'. The
2562 // request starts the debuggee to run again for one step. The debug adapter
2563 // first sends the StepOutResponse and then a StoppedEvent (event type
2564 // 'step') after the step has completed.", "properties": {
2565 // "command": {
2566 // "type": "string",
2567 // "enum": [ "stepOut" ]
2568 // },
2569 // "arguments": {
2570 // "$ref": "#/definitions/StepOutArguments"
2571 // }
2572 // },
2573 // "required": [ "command", "arguments" ]
2574 // }]
2575 // },
2576 // "StepOutArguments": {
2577 // "type": "object",
2578 // "description": "Arguments for 'stepOut' request.",
2579 // "properties": {
2580 // "threadId": {
2581 // "type": "integer",
2582 // "description": "Execute 'stepOut' for this thread."
2583 // }
2584 // },
2585 // "required": [ "threadId" ]
2586 // },
2587 // "StepOutResponse": {
2588 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2589 // "type": "object",
2590 // "description": "Response to 'stepOut' request. This is just an
2591 // acknowledgement, so no body field is required."
2592 // }]
2593 // }
request_stepOut(const llvm::json::Object & request)2594 void request_stepOut(const llvm::json::Object &request) {
2595 llvm::json::Object response;
2596 FillResponse(request, response);
2597 auto arguments = request.getObject("arguments");
2598 lldb::SBThread thread = g_vsc.GetLLDBThread(*arguments);
2599 if (thread.IsValid()) {
2600 // Remember the thread ID that caused the resume so we can set the
2601 // "threadCausedFocus" boolean value in the "stopped" events.
2602 g_vsc.focus_tid = thread.GetThreadID();
2603 thread.StepOut();
2604 } else {
2605 response["success"] = llvm::json::Value(false);
2606 }
2607 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
2608 }
2609
2610 // "ThreadsRequest": {
2611 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2612 // "type": "object",
2613 // "description": "Thread request; value of command field is 'threads'. The
2614 // request retrieves a list of all threads.", "properties": {
2615 // "command": {
2616 // "type": "string",
2617 // "enum": [ "threads" ]
2618 // }
2619 // },
2620 // "required": [ "command" ]
2621 // }]
2622 // },
2623 // "ThreadsResponse": {
2624 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2625 // "type": "object",
2626 // "description": "Response to 'threads' request.",
2627 // "properties": {
2628 // "body": {
2629 // "type": "object",
2630 // "properties": {
2631 // "threads": {
2632 // "type": "array",
2633 // "items": {
2634 // "$ref": "#/definitions/Thread"
2635 // },
2636 // "description": "All threads."
2637 // }
2638 // },
2639 // "required": [ "threads" ]
2640 // }
2641 // },
2642 // "required": [ "body" ]
2643 // }]
2644 // }
request_threads(const llvm::json::Object & request)2645 void request_threads(const llvm::json::Object &request) {
2646
2647 lldb::SBProcess process = g_vsc.target.GetProcess();
2648 llvm::json::Object response;
2649 FillResponse(request, response);
2650
2651 const uint32_t num_threads = process.GetNumThreads();
2652 llvm::json::Array threads;
2653 for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) {
2654 lldb::SBThread thread = process.GetThreadAtIndex(thread_idx);
2655 threads.emplace_back(CreateThread(thread));
2656 }
2657 if (threads.size() == 0) {
2658 response["success"] = llvm::json::Value(false);
2659 }
2660 llvm::json::Object body;
2661 body.try_emplace("threads", std::move(threads));
2662 response.try_emplace("body", std::move(body));
2663 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
2664 }
2665
2666 // "SetVariableRequest": {
2667 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2668 // "type": "object",
2669 // "description": "setVariable request; value of command field is
2670 // 'setVariable'. Set the variable with the given name in the variable
2671 // container to a new value.", "properties": {
2672 // "command": {
2673 // "type": "string",
2674 // "enum": [ "setVariable" ]
2675 // },
2676 // "arguments": {
2677 // "$ref": "#/definitions/SetVariableArguments"
2678 // }
2679 // },
2680 // "required": [ "command", "arguments" ]
2681 // }]
2682 // },
2683 // "SetVariableArguments": {
2684 // "type": "object",
2685 // "description": "Arguments for 'setVariable' request.",
2686 // "properties": {
2687 // "variablesReference": {
2688 // "type": "integer",
2689 // "description": "The reference of the variable container."
2690 // },
2691 // "name": {
2692 // "type": "string",
2693 // "description": "The name of the variable."
2694 // },
2695 // "value": {
2696 // "type": "string",
2697 // "description": "The value of the variable."
2698 // },
2699 // "format": {
2700 // "$ref": "#/definitions/ValueFormat",
2701 // "description": "Specifies details on how to format the response value."
2702 // }
2703 // },
2704 // "required": [ "variablesReference", "name", "value" ]
2705 // },
2706 // "SetVariableResponse": {
2707 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2708 // "type": "object",
2709 // "description": "Response to 'setVariable' request.",
2710 // "properties": {
2711 // "body": {
2712 // "type": "object",
2713 // "properties": {
2714 // "value": {
2715 // "type": "string",
2716 // "description": "The new value of the variable."
2717 // },
2718 // "type": {
2719 // "type": "string",
2720 // "description": "The type of the new value. Typically shown in the
2721 // UI when hovering over the value."
2722 // },
2723 // "variablesReference": {
2724 // "type": "number",
2725 // "description": "If variablesReference is > 0, the new value is
2726 // structured and its children can be retrieved by passing
2727 // variablesReference to the VariablesRequest."
2728 // },
2729 // "namedVariables": {
2730 // "type": "number",
2731 // "description": "The number of named child variables. The client
2732 // can use this optional information to present the variables in a
2733 // paged UI and fetch them in chunks."
2734 // },
2735 // "indexedVariables": {
2736 // "type": "number",
2737 // "description": "The number of indexed child variables. The client
2738 // can use this optional information to present the variables in a
2739 // paged UI and fetch them in chunks."
2740 // }
2741 // },
2742 // "required": [ "value" ]
2743 // }
2744 // },
2745 // "required": [ "body" ]
2746 // }]
2747 // }
request_setVariable(const llvm::json::Object & request)2748 void request_setVariable(const llvm::json::Object &request) {
2749 llvm::json::Object response;
2750 FillResponse(request, response);
2751 llvm::json::Array variables;
2752 llvm::json::Object body;
2753 auto arguments = request.getObject("arguments");
2754 // This is a reference to the containing variable/scope
2755 const auto variablesReference =
2756 GetUnsigned(arguments, "variablesReference", 0);
2757 llvm::StringRef name = GetString(arguments, "name");
2758 bool is_duplicated_variable_name = name.contains(" @");
2759
2760 const auto value = GetString(arguments, "value");
2761 // Set success to false just in case we don't find the variable by name
2762 response.try_emplace("success", false);
2763
2764 lldb::SBValue variable;
2765 int64_t newVariablesReference = 0;
2766
2767 // The "id" is the unique integer ID that is unique within the enclosing
2768 // variablesReference. It is optionally added to any "interface Variable"
2769 // objects to uniquely identify a variable within an enclosing
2770 // variablesReference. It helps to disambiguate between two variables that
2771 // have the same name within the same scope since the "setVariables" request
2772 // only specifies the variable reference of the enclosing scope/variable, and
2773 // the name of the variable. We could have two shadowed variables with the
2774 // same name in "Locals" or "Globals". In our case the "id" absolute index
2775 // of the variable within the g_vsc.variables list.
2776 const auto id_value = GetUnsigned(arguments, "id", UINT64_MAX);
2777 if (id_value != UINT64_MAX) {
2778 variable = g_vsc.variables.GetVariable(id_value);
2779 } else if (lldb::SBValueList *top_scope =
2780 GetTopLevelScope(variablesReference)) {
2781 // variablesReference is one of our scopes, not an actual variable it is
2782 // asking for a variable in locals or globals or registers
2783 int64_t end_idx = top_scope->GetSize();
2784 // Searching backward so that we choose the variable in closest scope
2785 // among variables of the same name.
2786 for (int64_t i = end_idx - 1; i >= 0; --i) {
2787 lldb::SBValue curr_variable = top_scope->GetValueAtIndex(i);
2788 std::string variable_name = CreateUniqueVariableNameForDisplay(
2789 curr_variable, is_duplicated_variable_name);
2790 if (variable_name == name) {
2791 variable = curr_variable;
2792 break;
2793 }
2794 }
2795 } else {
2796 // This is not under the globals or locals scope, so there are no duplicated
2797 // names.
2798
2799 // We have a named item within an actual variable so we need to find it
2800 // withing the container variable by name.
2801 lldb::SBValue container = g_vsc.variables.GetVariable(variablesReference);
2802 variable = container.GetChildMemberWithName(name.data());
2803 if (!variable.IsValid()) {
2804 if (name.startswith("[")) {
2805 llvm::StringRef index_str(name.drop_front(1));
2806 uint64_t index = 0;
2807 if (!index_str.consumeInteger(0, index)) {
2808 if (index_str == "]")
2809 variable = container.GetChildAtIndex(index);
2810 }
2811 }
2812 }
2813 }
2814
2815 if (variable.IsValid()) {
2816 lldb::SBError error;
2817 bool success = variable.SetValueFromCString(value.data(), error);
2818 if (success) {
2819 SetValueForKey(variable, body, "value");
2820 EmplaceSafeString(body, "type", variable.GetType().GetDisplayTypeName());
2821
2822 // We don't know the index of the variable in our g_vsc.variables
2823 // so always insert a new one to get its variablesReference.
2824 // is_permanent is false because debug console does not support
2825 // setVariable request.
2826 if (variable.MightHaveChildren())
2827 newVariablesReference = g_vsc.variables.InsertExpandableVariable(
2828 variable, /*is_permanent=*/false);
2829
2830 body.try_emplace("variablesReference", newVariablesReference);
2831 } else {
2832 EmplaceSafeString(body, "message", std::string(error.GetCString()));
2833 }
2834 response["success"] = llvm::json::Value(success);
2835 } else {
2836 response["success"] = llvm::json::Value(false);
2837 }
2838
2839 response.try_emplace("body", std::move(body));
2840 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
2841 }
2842
2843 // "VariablesRequest": {
2844 // "allOf": [ { "$ref": "#/definitions/Request" }, {
2845 // "type": "object",
2846 // "description": "Variables request; value of command field is 'variables'.
2847 // Retrieves all child variables for the given variable reference. An
2848 // optional filter can be used to limit the fetched children to either named
2849 // or indexed children.", "properties": {
2850 // "command": {
2851 // "type": "string",
2852 // "enum": [ "variables" ]
2853 // },
2854 // "arguments": {
2855 // "$ref": "#/definitions/VariablesArguments"
2856 // }
2857 // },
2858 // "required": [ "command", "arguments" ]
2859 // }]
2860 // },
2861 // "VariablesArguments": {
2862 // "type": "object",
2863 // "description": "Arguments for 'variables' request.",
2864 // "properties": {
2865 // "variablesReference": {
2866 // "type": "integer",
2867 // "description": "The Variable reference."
2868 // },
2869 // "filter": {
2870 // "type": "string",
2871 // "enum": [ "indexed", "named" ],
2872 // "description": "Optional filter to limit the child variables to either
2873 // named or indexed. If ommited, both types are fetched."
2874 // },
2875 // "start": {
2876 // "type": "integer",
2877 // "description": "The index of the first variable to return; if omitted
2878 // children start at 0."
2879 // },
2880 // "count": {
2881 // "type": "integer",
2882 // "description": "The number of variables to return. If count is missing
2883 // or 0, all variables are returned."
2884 // },
2885 // "format": {
2886 // "$ref": "#/definitions/ValueFormat",
2887 // "description": "Specifies details on how to format the Variable
2888 // values."
2889 // }
2890 // },
2891 // "required": [ "variablesReference" ]
2892 // },
2893 // "VariablesResponse": {
2894 // "allOf": [ { "$ref": "#/definitions/Response" }, {
2895 // "type": "object",
2896 // "description": "Response to 'variables' request.",
2897 // "properties": {
2898 // "body": {
2899 // "type": "object",
2900 // "properties": {
2901 // "variables": {
2902 // "type": "array",
2903 // "items": {
2904 // "$ref": "#/definitions/Variable"
2905 // },
2906 // "description": "All (or a range) of variables for the given
2907 // variable reference."
2908 // }
2909 // },
2910 // "required": [ "variables" ]
2911 // }
2912 // },
2913 // "required": [ "body" ]
2914 // }]
2915 // }
request_variables(const llvm::json::Object & request)2916 void request_variables(const llvm::json::Object &request) {
2917 llvm::json::Object response;
2918 FillResponse(request, response);
2919 llvm::json::Array variables;
2920 auto arguments = request.getObject("arguments");
2921 const auto variablesReference =
2922 GetUnsigned(arguments, "variablesReference", 0);
2923 const int64_t start = GetSigned(arguments, "start", 0);
2924 const int64_t count = GetSigned(arguments, "count", 0);
2925 bool hex = false;
2926 auto format = arguments->getObject("format");
2927 if (format)
2928 hex = GetBoolean(format, "hex", false);
2929
2930 if (lldb::SBValueList *top_scope = GetTopLevelScope(variablesReference)) {
2931 // variablesReference is one of our scopes, not an actual variable it is
2932 // asking for the list of args, locals or globals.
2933 int64_t start_idx = 0;
2934 int64_t num_children = 0;
2935
2936 num_children = top_scope->GetSize();
2937 const int64_t end_idx = start_idx + ((count == 0) ? num_children : count);
2938
2939 // We first find out which variable names are duplicated
2940 std::map<std::string, int> variable_name_counts;
2941 for (auto i = start_idx; i < end_idx; ++i) {
2942 lldb::SBValue variable = top_scope->GetValueAtIndex(i);
2943 if (!variable.IsValid())
2944 break;
2945 variable_name_counts[GetNonNullVariableName(variable)]++;
2946 }
2947
2948 // Now we construct the result with unique display variable names
2949 for (auto i = start_idx; i < end_idx; ++i) {
2950 lldb::SBValue variable = top_scope->GetValueAtIndex(i);
2951
2952 if (!variable.IsValid())
2953 break;
2954
2955 int64_t var_ref = 0;
2956 if (variable.MightHaveChildren()) {
2957 var_ref = g_vsc.variables.InsertExpandableVariable(
2958 variable, /*is_permanent=*/false);
2959 }
2960 variables.emplace_back(CreateVariable(
2961 variable, var_ref, var_ref != 0 ? var_ref : UINT64_MAX, hex,
2962 variable_name_counts[GetNonNullVariableName(variable)] > 1));
2963 }
2964 } else {
2965 // We are expanding a variable that has children, so we will return its
2966 // children.
2967 lldb::SBValue variable = g_vsc.variables.GetVariable(variablesReference);
2968 if (variable.IsValid()) {
2969 const auto num_children = variable.GetNumChildren();
2970 const int64_t end_idx = start + ((count == 0) ? num_children : count);
2971 for (auto i = start; i < end_idx; ++i) {
2972 lldb::SBValue child = variable.GetChildAtIndex(i);
2973 if (!child.IsValid())
2974 break;
2975 if (child.MightHaveChildren()) {
2976 auto is_permanent =
2977 g_vsc.variables.IsPermanentVariableReference(variablesReference);
2978 auto childVariablesReferences =
2979 g_vsc.variables.InsertExpandableVariable(child, is_permanent);
2980 variables.emplace_back(CreateVariable(child, childVariablesReferences,
2981 childVariablesReferences, hex));
2982 } else {
2983 variables.emplace_back(CreateVariable(child, 0, INT64_MAX, hex));
2984 }
2985 }
2986 }
2987 }
2988 llvm::json::Object body;
2989 body.try_emplace("variables", std::move(variables));
2990 response.try_emplace("body", std::move(body));
2991 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
2992 }
2993
2994 // A request used in testing to get the details on all breakpoints that are
2995 // currently set in the target. This helps us to test "setBreakpoints" and
2996 // "setFunctionBreakpoints" requests to verify we have the correct set of
2997 // breakpoints currently set in LLDB.
request__testGetTargetBreakpoints(const llvm::json::Object & request)2998 void request__testGetTargetBreakpoints(const llvm::json::Object &request) {
2999 llvm::json::Object response;
3000 FillResponse(request, response);
3001 llvm::json::Array response_breakpoints;
3002 for (uint32_t i = 0; g_vsc.target.GetBreakpointAtIndex(i).IsValid(); ++i) {
3003 auto bp = g_vsc.target.GetBreakpointAtIndex(i);
3004 AppendBreakpoint(bp, response_breakpoints);
3005 }
3006 llvm::json::Object body;
3007 body.try_emplace("breakpoints", std::move(response_breakpoints));
3008 response.try_emplace("body", std::move(body));
3009 g_vsc.SendJSON(llvm::json::Value(std::move(response)));
3010 }
3011
RegisterRequestCallbacks()3012 void RegisterRequestCallbacks() {
3013 g_vsc.RegisterRequestCallback("attach", request_attach);
3014 g_vsc.RegisterRequestCallback("completions", request_completions);
3015 g_vsc.RegisterRequestCallback("continue", request_continue);
3016 g_vsc.RegisterRequestCallback("configurationDone", request_configurationDone);
3017 g_vsc.RegisterRequestCallback("disconnect", request_disconnect);
3018 g_vsc.RegisterRequestCallback("evaluate", request_evaluate);
3019 g_vsc.RegisterRequestCallback("exceptionInfo", request_exceptionInfo);
3020 g_vsc.RegisterRequestCallback("initialize", request_initialize);
3021 g_vsc.RegisterRequestCallback("launch", request_launch);
3022 g_vsc.RegisterRequestCallback("next", request_next);
3023 g_vsc.RegisterRequestCallback("pause", request_pause);
3024 g_vsc.RegisterRequestCallback("scopes", request_scopes);
3025 g_vsc.RegisterRequestCallback("setBreakpoints", request_setBreakpoints);
3026 g_vsc.RegisterRequestCallback("setExceptionBreakpoints",
3027 request_setExceptionBreakpoints);
3028 g_vsc.RegisterRequestCallback("setFunctionBreakpoints",
3029 request_setFunctionBreakpoints);
3030 g_vsc.RegisterRequestCallback("setVariable", request_setVariable);
3031 g_vsc.RegisterRequestCallback("source", request_source);
3032 g_vsc.RegisterRequestCallback("stackTrace", request_stackTrace);
3033 g_vsc.RegisterRequestCallback("stepIn", request_stepIn);
3034 g_vsc.RegisterRequestCallback("stepOut", request_stepOut);
3035 g_vsc.RegisterRequestCallback("threads", request_threads);
3036 g_vsc.RegisterRequestCallback("variables", request_variables);
3037 // Custom requests
3038 g_vsc.RegisterRequestCallback("compileUnits", request_compileUnits);
3039 g_vsc.RegisterRequestCallback("modules", request_modules);
3040 // Testing requests
3041 g_vsc.RegisterRequestCallback("_testGetTargetBreakpoints",
3042 request__testGetTargetBreakpoints);
3043 }
3044
3045 } // anonymous namespace
3046
printHelp(LLDBVSCodeOptTable & table,llvm::StringRef tool_name)3047 static void printHelp(LLDBVSCodeOptTable &table, llvm::StringRef tool_name) {
3048 std::string usage_str = tool_name.str() + " options";
3049 table.printHelp(llvm::outs(), usage_str.c_str(), "LLDB VSCode", false);
3050
3051 std::string examples = R"___(
3052 EXAMPLES:
3053 The debug adapter can be started in two modes.
3054
3055 Running lldb-vscode without any arguments will start communicating with the
3056 parent over stdio. Passing a port number causes lldb-vscode to start listening
3057 for connections on that port.
3058
3059 lldb-vscode -p <port>
3060
3061 Passing --wait-for-debugger will pause the process at startup and wait for a
3062 debugger to attach to the process.
3063
3064 lldb-vscode -g
3065 )___";
3066 llvm::outs() << examples;
3067 }
3068
3069 // If --launch-target is provided, this instance of lldb-vscode becomes a
3070 // runInTerminal launcher. It will ultimately launch the program specified in
3071 // the --launch-target argument, which is the original program the user wanted
3072 // to debug. This is done in such a way that the actual debug adaptor can
3073 // place breakpoints at the beginning of the program.
3074 //
3075 // The launcher will communicate with the debug adaptor using a fifo file in the
3076 // directory specified in the --comm-file argument.
3077 //
3078 // Regarding the actual flow, this launcher will first notify the debug adaptor
3079 // of its pid. Then, the launcher will be in a pending state waiting to be
3080 // attached by the adaptor.
3081 //
3082 // Once attached and resumed, the launcher will exec and become the program
3083 // specified by --launch-target, which is the original target the
3084 // user wanted to run.
3085 //
3086 // In case of errors launching the target, a suitable error message will be
3087 // emitted to the debug adaptor.
LaunchRunInTerminalTarget(llvm::opt::Arg & target_arg,llvm::StringRef comm_file,char * argv[])3088 void LaunchRunInTerminalTarget(llvm::opt::Arg &target_arg,
3089 llvm::StringRef comm_file, char *argv[]) {
3090 #if defined(_WIN32)
3091 llvm::errs() << "runInTerminal is only supported on POSIX systems\n";
3092 exit(EXIT_FAILURE);
3093 #else
3094 RunInTerminalLauncherCommChannel comm_channel(comm_file);
3095 if (llvm::Error err = comm_channel.NotifyPid()) {
3096 llvm::errs() << llvm::toString(std::move(err)) << "\n";
3097 exit(EXIT_FAILURE);
3098 }
3099
3100 // We will wait to be attached with a timeout. We don't wait indefinitely
3101 // using a signal to prevent being paused forever.
3102
3103 // This env var should be used only for tests.
3104 const char *timeout_env_var = getenv("LLDB_VSCODE_RIT_TIMEOUT_IN_MS");
3105 int timeout_in_ms =
3106 timeout_env_var != nullptr ? atoi(timeout_env_var) : 20000;
3107 if (llvm::Error err = comm_channel.WaitUntilDebugAdaptorAttaches(
3108 std::chrono::milliseconds(timeout_in_ms))) {
3109 llvm::errs() << llvm::toString(std::move(err)) << "\n";
3110 exit(EXIT_FAILURE);
3111 }
3112
3113 const char *target = target_arg.getValue();
3114 execvp(target, argv);
3115
3116 std::string error = std::strerror(errno);
3117 comm_channel.NotifyError(error);
3118 llvm::errs() << error << "\n";
3119 exit(EXIT_FAILURE);
3120 #endif
3121 }
3122
3123 /// used only by TestVSCode_redirection_to_console.py
redirection_test()3124 void redirection_test() {
3125 printf("stdout message\n");
3126 fprintf(stderr, "stderr message\n");
3127 fflush(stdout);
3128 fflush(stderr);
3129 }
3130
3131 /// Redirect stdout and stderr fo the IDE's console output.
3132 ///
3133 /// Errors in this operation will be printed to the log file and the IDE's
3134 /// console output as well.
3135 ///
3136 /// \return
3137 /// A fd pointing to the original stdout.
SetupStdoutStderrRedirection()3138 int SetupStdoutStderrRedirection() {
3139 int stdoutfd = fileno(stdout);
3140 int new_stdout_fd = dup(stdoutfd);
3141 auto output_callback_stderr = [](llvm::StringRef data) {
3142 g_vsc.SendOutput(OutputType::Stderr, data);
3143 };
3144 auto output_callback_stdout = [](llvm::StringRef data) {
3145 g_vsc.SendOutput(OutputType::Stdout, data);
3146 };
3147 if (llvm::Error err = RedirectFd(stdoutfd, output_callback_stdout)) {
3148 std::string error_message = llvm::toString(std::move(err));
3149 if (g_vsc.log)
3150 *g_vsc.log << error_message << std::endl;
3151 output_callback_stderr(error_message);
3152 }
3153 if (llvm::Error err = RedirectFd(fileno(stderr), output_callback_stderr)) {
3154 std::string error_message = llvm::toString(std::move(err));
3155 if (g_vsc.log)
3156 *g_vsc.log << error_message << std::endl;
3157 output_callback_stderr(error_message);
3158 }
3159
3160 /// used only by TestVSCode_redirection_to_console.py
3161 if (getenv("LLDB_VSCODE_TEST_STDOUT_STDERR_REDIRECTION") != nullptr)
3162 redirection_test();
3163 return new_stdout_fd;
3164 }
3165
main(int argc,char * argv[])3166 int main(int argc, char *argv[]) {
3167 llvm::InitLLVM IL(argc, argv, /*InstallPipeSignalExitHandler=*/false);
3168 llvm::PrettyStackTraceProgram X(argc, argv);
3169
3170 llvm::SmallString<256> program_path(argv[0]);
3171 llvm::sys::fs::make_absolute(program_path);
3172 g_vsc.debug_adaptor_path = program_path.str().str();
3173
3174 LLDBVSCodeOptTable T;
3175 unsigned MAI, MAC;
3176 llvm::ArrayRef<const char *> ArgsArr = llvm::makeArrayRef(argv + 1, argc);
3177 llvm::opt::InputArgList input_args = T.ParseArgs(ArgsArr, MAI, MAC);
3178
3179 if (input_args.hasArg(OPT_help)) {
3180 printHelp(T, llvm::sys::path::filename(argv[0]));
3181 return EXIT_SUCCESS;
3182 }
3183
3184 if (llvm::opt::Arg *target_arg = input_args.getLastArg(OPT_launch_target)) {
3185 if (llvm::opt::Arg *comm_file = input_args.getLastArg(OPT_comm_file)) {
3186 int target_args_pos = argc;
3187 for (int i = 0; i < argc; i++)
3188 if (strcmp(argv[i], "--launch-target") == 0) {
3189 target_args_pos = i + 1;
3190 break;
3191 }
3192 LaunchRunInTerminalTarget(*target_arg, comm_file->getValue(),
3193 argv + target_args_pos);
3194 } else {
3195 llvm::errs() << "\"--launch-target\" requires \"--comm-file\" to be "
3196 "specified\n";
3197 return EXIT_FAILURE;
3198 }
3199 }
3200
3201 // stdout/stderr redirection to the IDE's console
3202 int new_stdout_fd = SetupStdoutStderrRedirection();
3203
3204 // Initialize LLDB first before we do anything.
3205 lldb::SBDebugger::Initialize();
3206
3207 // Terminate the debugger before the C++ destructor chain kicks in.
3208 auto terminate_debugger =
3209 llvm::make_scope_exit([] { lldb::SBDebugger::Terminate(); });
3210
3211 RegisterRequestCallbacks();
3212
3213 int portno = -1;
3214
3215 if (auto *arg = input_args.getLastArg(OPT_port)) {
3216 auto optarg = arg->getValue();
3217 char *remainder;
3218 portno = strtol(optarg, &remainder, 0);
3219 if (remainder == optarg || *remainder != '\0') {
3220 fprintf(stderr, "'%s' is not a valid port number.\n", optarg);
3221 return EXIT_FAILURE;
3222 }
3223 }
3224
3225 #if !defined(_WIN32)
3226 if (input_args.hasArg(OPT_wait_for_debugger)) {
3227 printf("Paused waiting for debugger to attach (pid = %i)...\n", getpid());
3228 pause();
3229 }
3230 #endif
3231 if (portno != -1) {
3232 printf("Listening on port %i...\n", portno);
3233 SOCKET socket_fd = AcceptConnection(portno);
3234 if (socket_fd >= 0) {
3235 g_vsc.input.descriptor = StreamDescriptor::from_socket(socket_fd, true);
3236 g_vsc.output.descriptor = StreamDescriptor::from_socket(socket_fd, false);
3237 } else {
3238 return EXIT_FAILURE;
3239 }
3240 } else {
3241 g_vsc.input.descriptor = StreamDescriptor::from_file(fileno(stdin), false);
3242 g_vsc.output.descriptor = StreamDescriptor::from_file(new_stdout_fd, false);
3243 }
3244
3245 while (!g_vsc.sent_terminated_event) {
3246 llvm::json::Object object;
3247 lldb_vscode::PacketStatus status = g_vsc.GetNextObject(object);
3248 if (status == lldb_vscode::PacketStatus::EndOfFile)
3249 break;
3250 if (status != lldb_vscode::PacketStatus::Success)
3251 return 1; // Fatal error
3252
3253 if (!g_vsc.HandleObject(object))
3254 return 1;
3255 }
3256
3257 return EXIT_SUCCESS;
3258 }
3259