diff options
Diffstat (limited to 'lldb/tools/lldb-vscode/VSCode.cpp')
-rw-r--r-- | lldb/tools/lldb-vscode/VSCode.cpp | 104 |
1 files changed, 37 insertions, 67 deletions
diff --git a/lldb/tools/lldb-vscode/VSCode.cpp b/lldb/tools/lldb-vscode/VSCode.cpp index d689dc5f474..1f5515fd8a9 100644 --- a/lldb/tools/lldb-vscode/VSCode.cpp +++ b/lldb/tools/lldb-vscode/VSCode.cpp @@ -10,12 +10,15 @@ #include <fstream> #include <mutex> -#include "VSCode.h" #include "LLDBUtils.h" +#include "VSCode.h" +#include "llvm/Support/FormatVariadic.h" #if defined(_WIN32) -#include <io.h> +#define NOMINMAX +#include <Windows.h> #include <fcntl.h> +#include <io.h> #endif using namespace lldb_vscode; @@ -31,15 +34,15 @@ namespace lldb_vscode { VSCode g_vsc; VSCode::VSCode() - : in(stdin), out(stdout), launch_info(nullptr), variables(), - broadcaster("lldb-vscode"), num_regs(0), num_locals(0), num_globals(0), - log(), exception_breakpoints( - {{"cpp_catch", "C++ Catch", lldb::eLanguageTypeC_plus_plus}, - {"cpp_throw", "C++ Throw", lldb::eLanguageTypeC_plus_plus}, - {"objc_catch", "Objective C Catch", lldb::eLanguageTypeObjC}, - {"objc_throw", "Objective C Throw", lldb::eLanguageTypeObjC}, - {"swift_catch", "Swift Catch", lldb::eLanguageTypeSwift}, - {"swift_throw", "Swift Throw", lldb::eLanguageTypeSwift}}), + : launch_info(nullptr), variables(), broadcaster("lldb-vscode"), + num_regs(0), num_locals(0), num_globals(0), log(), + exception_breakpoints( + {{"cpp_catch", "C++ Catch", lldb::eLanguageTypeC_plus_plus}, + {"cpp_throw", "C++ Throw", lldb::eLanguageTypeC_plus_plus}, + {"objc_catch", "Objective C Catch", lldb::eLanguageTypeObjC}, + {"objc_throw", "Objective C Throw", lldb::eLanguageTypeObjC}, + {"swift_catch", "Swift Catch", lldb::eLanguageTypeSwift}, + {"swift_throw", "Swift Throw", lldb::eLanguageTypeSwift}}), focus_tid(LLDB_INVALID_THREAD_ID), sent_terminated_event(false), stop_at_entry(false) { const char *log_file_path = getenv("LLDBVSCODE_LOG"); @@ -55,22 +58,6 @@ VSCode::VSCode() } VSCode::~VSCode() { - CloseInputStream(); - CloseOutputStream(); -} - -void VSCode::CloseInputStream() { - if (in != stdin) { - fclose(in); - in = nullptr; - } -} - -void VSCode::CloseOutputStream() { - if (out != stdout) { - fclose(out); - out = nullptr; - } } int64_t VSCode::GetLineForPC(int64_t sourceReference, lldb::addr_t pc) const { @@ -103,9 +90,11 @@ VSCode::GetExceptionBreakpoint(const lldb::break_id_t bp_id) { // JSON bytes. //---------------------------------------------------------------------- void VSCode::SendJSON(const std::string &json_str) { - fprintf(out, "Content-Length: %u\r\n\r\n%s", (uint32_t)json_str.size(), - json_str.c_str()); - fflush(out); + output.write_full("Content-Length: "); + output.write_full(llvm::utostr(json_str.size())); + output.write_full("\r\n\r\n"); + output.write_full(json_str); + if (log) { *log << "<-- " << std::endl << "Content-Length: " << json_str.size() << "\r\n\r\n" @@ -130,44 +119,25 @@ void VSCode::SendJSON(const llvm::json::Value &json) { // Read a JSON packet from the "in" stream. //---------------------------------------------------------------------- std::string VSCode::ReadJSON() { - static std::string header("Content-Length: "); - - uint32_t packet_len = 0; + std::string length_str; std::string json_str; - char line[1024]; - - while (fgets(line, sizeof(line), in)) { - if (strncmp(line, header.data(), header.size()) == 0) { - packet_len = atoi(line + header.size()); - if (fgets(line, sizeof(line), in)) { - if (!IsEmptyLine(line)) - if (log) - *log << "warning: expected empty line but got: \"" << line << "\"" - << std::endl; - break; - } - } else { - if (log) - *log << "warning: expected \"" << header << "\" but got: \"" << line - << "\"" << std::endl; - } - } - // This is followed by two windows newline sequences ("\r\n\r\n") so eat - // two the newline sequences - if (packet_len > 0) { - json_str.resize(packet_len); - auto bytes_read = fread(&json_str[0], 1, packet_len, in); - if (bytes_read < packet_len) { - if (log) - *log << "error: read fewer bytes (" << bytes_read - << ") than requested (" << packet_len << ")" << std::endl; - json_str.erase(bytes_read); - } - if (log) { - *log << "--> " << std::endl; - *log << header << packet_len << "\r\n\r\n" << json_str << std::endl; - } - } + int length; + + if (!input.read_expected(log.get(), "Content-Length: ")) + return json_str; + + if (!input.read_line(log.get(), length_str)) + return json_str; + + if (!llvm::to_integer(length_str, length)) + return json_str; + + if (!input.read_expected(log.get(), "\r\n")) + return json_str; + + if (!input.read_full(log.get(), length, json_str)) + return json_str; + return json_str; } |