Skip to content

Conversation

ashgti
Copy link
Contributor

@ashgti ashgti commented Oct 16, 2025

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.

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 termainl 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.
@llvmbot
Copy link
Member

llvmbot commented Oct 16, 2025

@llvm/pr-subscribers-lldb

Author: John Harrison (ashgti)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/163830.diff

1 Files Affected:

  • (modified) lldb/tools/lldb-dap/tool/lldb-dap.cpp (+18)
diff --git a/lldb/tools/lldb-dap/tool/lldb-dap.cpp b/lldb/tools/lldb-dap/tool/lldb-dap.cpp index e59cef9793366..7a077d220e47f 100644 --- a/lldb/tools/lldb-dap/tool/lldb-dap.cpp +++ b/lldb/tools/lldb-dap/tool/lldb-dap.cpp @@ -74,6 +74,7 @@ typedef int socklen_t; #include <netinet/in.h> #include <sys/socket.h> #include <sys/un.h> +#include <termios.h> #include <unistd.h> #endif @@ -121,6 +122,15 @@ class LLDBDAPOptTable : public llvm::opt::GenericOptTable { }; } // anonymous namespace +#define ESCAPE "\x1b" +#define CSI ESCAPE "[" +// Move the cursor to 0,0 +#define ANSI_CURSOR_HOME CSI "H" +// Clear the screen buffer +#define ANSI_ERASE_SCREEN CSI "2J" +// Clear the scroll back buffer +#define ANSI_ERASE_SCROLLBACK CSI "3J" + static void PrintHelp(LLDBDAPOptTable &table, llvm::StringRef tool_name) { std::string usage_str = tool_name.str() + " options"; table.printHelp(llvm::outs(), usage_str.c_str(), "LLDB DAP", false); @@ -261,6 +271,14 @@ 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_CURSOR_HOME ANSI_ERASE_SCREEN ANSI_ERASE_SCROLLBACK; + // VSCode 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); 
@ashgti ashgti requested a review from JDevlieghere October 16, 2025 20:04
Comment on lines +75 to +84
// 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I'll adopt ANSI_CSI_ERASE_VIEWPORT in the statusline which does this too.

Copy link
Member

@JDevlieghere JDevlieghere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Co-authored-by: Jonas Devlieghere <jonas@devlieghere.com>
@ashgti ashgti merged commit db530bf into llvm:main Oct 16, 2025
10 checks passed
@ashgti ashgti deleted the lldb-dap-console branch October 16, 2025 20:48
Copy link
Member

@walter-erquinigo walter-erquinigo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fancy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

4 participants