1 //===-- 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 <stdarg.h> 10 #include <fstream> 11 #include <mutex> 12 13 #include "LLDBUtils.h" 14 #include "VSCode.h" 15 #include "llvm/Support/FormatVariadic.h" 16 17 #if defined(_WIN32) 18 #define NOMINMAX 19 #include <windows.h> 20 #include <fcntl.h> 21 #include <io.h> 22 #endif 23 24 using namespace lldb_vscode; 25 26 namespace lldb_vscode { 27 28 VSCode g_vsc; 29 30 VSCode::VSCode() 31 : variables(), broadcaster("lldb-vscode"), num_regs(0), num_locals(0), 32 num_globals(0), log(), 33 exception_breakpoints( 34 {{"cpp_catch", "C++ Catch", lldb::eLanguageTypeC_plus_plus}, 35 {"cpp_throw", "C++ Throw", lldb::eLanguageTypeC_plus_plus}, 36 {"objc_catch", "Objective C Catch", lldb::eLanguageTypeObjC}, 37 {"objc_throw", "Objective C Throw", lldb::eLanguageTypeObjC}, 38 {"swift_catch", "Swift Catch", lldb::eLanguageTypeSwift}, 39 {"swift_throw", "Swift Throw", lldb::eLanguageTypeSwift}}), 40 focus_tid(LLDB_INVALID_THREAD_ID), sent_terminated_event(false), 41 stop_at_entry(false), is_attach(false) { 42 const char *log_file_path = getenv("LLDBVSCODE_LOG"); 43 #if defined(_WIN32) 44 // Windows opens stdout and stdin in text mode which converts \n to 13,10 45 // while the value is just 10 on Darwin/Linux. Setting the file mode to binary 46 // fixes this. 47 int result = _setmode(fileno(stdout), _O_BINARY); 48 assert(result); 49 result = _setmode(fileno(stdin), _O_BINARY); 50 (void)result; 51 assert(result); 52 #endif 53 if (log_file_path) 54 log.reset(new std::ofstream(log_file_path)); 55 } 56 57 VSCode::~VSCode() { 58 } 59 60 int64_t VSCode::GetLineForPC(int64_t sourceReference, lldb::addr_t pc) const { 61 auto pos = source_map.find(sourceReference); 62 if (pos != source_map.end()) 63 return pos->second.GetLineForPC(pc); 64 return 0; 65 } 66 67 ExceptionBreakpoint *VSCode::GetExceptionBreakpoint(const std::string &filter) { 68 for (auto &bp : exception_breakpoints) { 69 if (bp.filter == filter) 70 return &bp; 71 } 72 return nullptr; 73 } 74 75 ExceptionBreakpoint * 76 VSCode::GetExceptionBreakpoint(const lldb::break_id_t bp_id) { 77 for (auto &bp : exception_breakpoints) { 78 if (bp.bp.GetID() == bp_id) 79 return &bp; 80 } 81 return nullptr; 82 } 83 84 // Send the JSON in "json_str" to the "out" stream. Correctly send the 85 // "Content-Length:" field followed by the length, followed by the raw 86 // JSON bytes. 87 void VSCode::SendJSON(const std::string &json_str) { 88 output.write_full("Content-Length: "); 89 output.write_full(llvm::utostr(json_str.size())); 90 output.write_full("\r\n\r\n"); 91 output.write_full(json_str); 92 93 if (log) { 94 *log << "<-- " << std::endl 95 << "Content-Length: " << json_str.size() << "\r\n\r\n" 96 << json_str << std::endl; 97 } 98 } 99 100 // Serialize the JSON value into a string and send the JSON packet to 101 // the "out" stream. 102 void VSCode::SendJSON(const llvm::json::Value &json) { 103 std::string s; 104 llvm::raw_string_ostream strm(s); 105 strm << json; 106 static std::mutex mutex; 107 std::lock_guard<std::mutex> locker(mutex); 108 SendJSON(strm.str()); 109 } 110 111 // Read a JSON packet from the "in" stream. 112 std::string VSCode::ReadJSON() { 113 std::string length_str; 114 std::string json_str; 115 int length; 116 117 if (!input.read_expected(log.get(), "Content-Length: ")) 118 return json_str; 119 120 if (!input.read_line(log.get(), length_str)) 121 return json_str; 122 123 if (!llvm::to_integer(length_str, length)) 124 return json_str; 125 126 if (!input.read_expected(log.get(), "\r\n")) 127 return json_str; 128 129 if (!input.read_full(log.get(), length, json_str)) 130 return json_str; 131 132 if (log) { 133 *log << "--> " << std::endl 134 << "Content-Length: " << length << "\r\n\r\n" 135 << json_str << std::endl; 136 } 137 138 return json_str; 139 } 140 141 // "OutputEvent": { 142 // "allOf": [ { "$ref": "#/definitions/Event" }, { 143 // "type": "object", 144 // "description": "Event message for 'output' event type. The event 145 // indicates that the target has produced some output.", 146 // "properties": { 147 // "event": { 148 // "type": "string", 149 // "enum": [ "output" ] 150 // }, 151 // "body": { 152 // "type": "object", 153 // "properties": { 154 // "category": { 155 // "type": "string", 156 // "description": "The output category. If not specified, 157 // 'console' is assumed.", 158 // "_enum": [ "console", "stdout", "stderr", "telemetry" ] 159 // }, 160 // "output": { 161 // "type": "string", 162 // "description": "The output to report." 163 // }, 164 // "variablesReference": { 165 // "type": "number", 166 // "description": "If an attribute 'variablesReference' exists 167 // and its value is > 0, the output contains 168 // objects which can be retrieved by passing 169 // variablesReference to the VariablesRequest." 170 // }, 171 // "source": { 172 // "$ref": "#/definitions/Source", 173 // "description": "An optional source location where the output 174 // was produced." 175 // }, 176 // "line": { 177 // "type": "integer", 178 // "description": "An optional source location line where the 179 // output was produced." 180 // }, 181 // "column": { 182 // "type": "integer", 183 // "description": "An optional source location column where the 184 // output was produced." 185 // }, 186 // "data": { 187 // "type":["array","boolean","integer","null","number","object", 188 // "string"], 189 // "description": "Optional data to report. For the 'telemetry' 190 // category the data will be sent to telemetry, for 191 // the other categories the data is shown in JSON 192 // format." 193 // } 194 // }, 195 // "required": ["output"] 196 // } 197 // }, 198 // "required": [ "event", "body" ] 199 // }] 200 // } 201 void VSCode::SendOutput(OutputType o, const llvm::StringRef output) { 202 if (output.empty()) 203 return; 204 205 llvm::json::Object event(CreateEventObject("output")); 206 llvm::json::Object body; 207 const char *category = nullptr; 208 switch (o) { 209 case OutputType::Console: 210 category = "console"; 211 break; 212 case OutputType::Stdout: 213 category = "stdout"; 214 break; 215 case OutputType::Stderr: 216 category = "stderr"; 217 break; 218 case OutputType::Telemetry: 219 category = "telemetry"; 220 break; 221 } 222 body.try_emplace("category", category); 223 EmplaceSafeString(body, "output", output.str()); 224 event.try_emplace("body", std::move(body)); 225 SendJSON(llvm::json::Value(std::move(event))); 226 } 227 228 void __attribute__((format(printf, 3, 4))) 229 VSCode::SendFormattedOutput(OutputType o, const char *format, ...) { 230 char buffer[1024]; 231 va_list args; 232 va_start(args, format); 233 int actual_length = vsnprintf(buffer, sizeof(buffer), format, args); 234 va_end(args); 235 SendOutput(o, llvm::StringRef(buffer, 236 std::min<int>(actual_length, sizeof(buffer)))); 237 } 238 239 int64_t VSCode::GetNextSourceReference() { 240 static int64_t ref = 0; 241 return ++ref; 242 } 243 244 ExceptionBreakpoint * 245 VSCode::GetExceptionBPFromStopReason(lldb::SBThread &thread) { 246 const auto num = thread.GetStopReasonDataCount(); 247 // Check to see if have hit an exception breakpoint and change the 248 // reason to "exception", but only do so if all breakpoints that were 249 // hit are exception breakpoints. 250 ExceptionBreakpoint *exc_bp = nullptr; 251 for (size_t i = 0; i < num; i += 2) { 252 // thread.GetStopReasonDataAtIndex(i) will return the bp ID and 253 // thread.GetStopReasonDataAtIndex(i+1) will return the location 254 // within that breakpoint. We only care about the bp ID so we can 255 // see if this is an exception breakpoint that is getting hit. 256 lldb::break_id_t bp_id = thread.GetStopReasonDataAtIndex(i); 257 exc_bp = GetExceptionBreakpoint(bp_id); 258 // If any breakpoint is not an exception breakpoint, then stop and 259 // report this as a normal breakpoint 260 if (exc_bp == nullptr) 261 return nullptr; 262 } 263 return exc_bp; 264 } 265 266 lldb::SBThread VSCode::GetLLDBThread(const llvm::json::Object &arguments) { 267 auto tid = GetSigned(arguments, "threadId", LLDB_INVALID_THREAD_ID); 268 return target.GetProcess().GetThreadByID(tid); 269 } 270 271 lldb::SBFrame VSCode::GetLLDBFrame(const llvm::json::Object &arguments) { 272 const uint64_t frame_id = GetUnsigned(arguments, "frameId", UINT64_MAX); 273 lldb::SBProcess process = target.GetProcess(); 274 // Upper 32 bits is the thread index ID 275 lldb::SBThread thread = 276 process.GetThreadByIndexID(GetLLDBThreadIndexID(frame_id)); 277 // Lower 32 bits is the frame index 278 return thread.GetFrameAtIndex(GetLLDBFrameID(frame_id)); 279 } 280 281 llvm::json::Value VSCode::CreateTopLevelScopes() { 282 llvm::json::Array scopes; 283 scopes.emplace_back(CreateScope("Locals", VARREF_LOCALS, num_locals, false)); 284 scopes.emplace_back( 285 CreateScope("Globals", VARREF_GLOBALS, num_globals, false)); 286 scopes.emplace_back(CreateScope("Registers", VARREF_REGS, num_regs, false)); 287 return llvm::json::Value(std::move(scopes)); 288 } 289 290 void VSCode::RunLLDBCommands(llvm::StringRef prefix, 291 const std::vector<std::string> &commands) { 292 SendOutput(OutputType::Console, 293 llvm::StringRef(::RunLLDBCommands(prefix, commands))); 294 } 295 296 void VSCode::RunInitCommands() { 297 RunLLDBCommands("Running initCommands:", init_commands); 298 } 299 300 void VSCode::RunPreRunCommands() { 301 RunLLDBCommands("Running preRunCommands:", pre_run_commands); 302 } 303 304 void VSCode::RunStopCommands() { 305 RunLLDBCommands("Running stopCommands:", stop_commands); 306 } 307 308 void VSCode::RunExitCommands() { 309 RunLLDBCommands("Running exitCommands:", exit_commands); 310 } 311 312 void VSCode::RunTerminateCommands() { 313 RunLLDBCommands("Running terminateCommands:", terminate_commands); 314 } 315 316 lldb::SBTarget VSCode::CreateTargetFromArguments( 317 const llvm::json::Object &arguments, 318 lldb::SBError &error) { 319 // Grab the name of the program we need to debug and create a target using 320 // the given program as an argument. Executable file can be a source of target 321 // architecture and platform, if they differ from the host. Setting exe path 322 // in launch info is useless because Target.Launch() will not change 323 // architecture and platform, therefore they should be known at the target 324 // creation. We also use target triple and platform from the launch 325 // configuration, if given, since in some cases ELF file doesn't contain 326 // enough information to determine correct arch and platform (or ELF can be 327 // omitted at all), so it is good to leave the user an apportunity to specify 328 // those. Any of those three can be left empty. 329 llvm::StringRef target_triple = GetString(arguments, "targetTriple"); 330 llvm::StringRef platform_name = GetString(arguments, "platformName"); 331 llvm::StringRef program = GetString(arguments, "program"); 332 auto target = this->debugger.CreateTarget( 333 program.data(), 334 target_triple.data(), 335 platform_name.data(), 336 true, // Add dependent modules. 337 error 338 ); 339 340 if (error.Fail()) { 341 // Update message if there was an error. 342 error.SetErrorStringWithFormat( 343 "Could not create a target for a program '%s': %s.", 344 program.data(), error.GetCString()); 345 } 346 347 return target; 348 } 349 350 void VSCode::SetTarget(const lldb::SBTarget target) { 351 this->target = target; 352 353 if (target.IsValid()) { 354 // Configure breakpoint event listeners for the target. 355 lldb::SBListener listener = this->debugger.GetListener(); 356 listener.StartListeningForEvents( 357 this->target.GetBroadcaster(), 358 lldb::SBTarget::eBroadcastBitBreakpointChanged); 359 listener.StartListeningForEvents(this->broadcaster, 360 eBroadcastBitStopEventThread); 361 listener.StartListeningForEvents( 362 this->target.GetBroadcaster(), 363 lldb::SBTarget::eBroadcastBitModulesLoaded | 364 lldb::SBTarget::eBroadcastBitModulesUnloaded | 365 lldb::SBTarget::eBroadcastBitSymbolsLoaded); 366 } 367 } 368 369 } // namespace lldb_vscode 370