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