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