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