diff --git a/lldb/include/lldb/Utility/AnsiTerminal.h b/lldb/include/lldb/Utility/AnsiTerminal.h index 41acac74364bb..aaaf94c6c5f7f 100644 --- a/lldb/include/lldb/Utility/AnsiTerminal.h +++ b/lldb/include/lldb/Utility/AnsiTerminal.h @@ -72,6 +72,17 @@ #define ANSI_ESC_START_LEN 2 +// Cursor Position, set cursor to position [l, c] (default = [1, 1]). +#define ANSI_CSI_CUP(...) ANSI_ESC_START #__VA_ARGS__ "H" +// Reset cursor to position. +#define ANSI_CSI_RESET_CURSOR ANSI_CSI_CUP() +// Erase In Display. +#define ANSI_CSI_ED(opt) ANSI_ESC_START #opt "J" +// Erase complete viewport. +#define ANSI_CSI_ERASE_VIEWPORT ANSI_CSI_ED(2) +// Erase scrollback. +#define ANSI_CSI_ERASE_SCROLLBACK ANSI_CSI_ED(3) + // OSC (Operating System Commands) // https://invisible-island.net/xterm/ctlseqs/ctlseqs.html #define OSC_ESCAPE_START "\033" diff --git a/lldb/tools/lldb-dap/tool/lldb-dap.cpp b/lldb/tools/lldb-dap/tool/lldb-dap.cpp index e59cef9793366..45caa1a81059b 100644 --- a/lldb/tools/lldb-dap/tool/lldb-dap.cpp +++ b/lldb/tools/lldb-dap/tool/lldb-dap.cpp @@ -21,6 +21,7 @@ #include "lldb/Host/MainLoopBase.h" #include "lldb/Host/MemoryMonitor.h" #include "lldb/Host/Socket.h" +#include "lldb/Utility/AnsiTerminal.h" #include "lldb/Utility/Status.h" #include "lldb/Utility/UriParser.h" #include "lldb/lldb-forward.h" @@ -74,6 +75,7 @@ typedef int socklen_t; #include #include #include +#include #include #endif @@ -261,6 +263,15 @@ static llvm::Error LaunchRunInTerminalTarget(llvm::opt::Arg &target_arg, files.push_back(files.back()); if (llvm::Error err = SetupIORedirection(files)) return err; + } else if ((isatty(STDIN_FILENO) != 0) && + llvm::StringRef(getenv("TERM")).starts_with_insensitive("xterm")) { + // Clear the screen. + llvm::outs() << ANSI_CSI_RESET_CURSOR ANSI_CSI_ERASE_VIEWPORT + ANSI_CSI_ERASE_SCROLLBACK; + // VS Code will reuse the same terminal for the same debug configuration + // between runs. Clear the input buffer prior to starting the new process so + // prior input is not carried forward to the new debug session. + tcflush(STDIN_FILENO, TCIFLUSH); } RunInTerminalLauncherCommChannel comm_channel(comm_file);