[lldb-dap] Improve the runInTerminal ux. (#163830)

This updates lldb-dap to clear the screen when using `"console":
"integratedTerminal"` or `"console": "externalTerminal"`.

VSCode will reuse the same terminal for a given debug configuration.
After the process exits it will return to the shell but if the debug
session is launched again it will be invoked in the same terminal.
VSCode is sending the terminal the launch args as terminal input which
means the terminal would now have a string like `lldb-dap --comm-file
... --launch-target ...` and the scrollback buffer from any previous
output or shell commands used in the terminal.

To address this, I've updated LaunchRunInTerminalTarget to reset the
cursor, clear the screen and clear the scrollback buffer to soft 'reset'
the terminal prior to launching the process.

---------

Co-authored-by: Jonas Devlieghere <jonas@devlieghere.com>
This commit is contained in:
John Harrison
2025-10-16 13:28:09 -07:00
committed by GitHub
parent 05e86001c1
commit db530bf7aa
2 changed files with 22 additions and 0 deletions

View File

@@ -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"

View File

@@ -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 <netinet/in.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <termios.h>
#include <unistd.h>
#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);