https://github.com/ashgti updated 
https://github.com/llvm/llvm-project/pull/163830

>From a2248f10e60868634f620b62b3765703f623c6f5 Mon Sep 17 00:00:00 2001
From: John Harrison <[email protected]>
Date: Thu, 16 Oct 2025 10:27:31 -0700
Subject: [PATCH 1/2] [lldb-dap] Improve the runInTerminal ux.

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.
---
 lldb/tools/lldb-dap/tool/lldb-dap.cpp | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

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);

>From 28467ce86a4fed88e943add1f05671ba29087fd5 Mon Sep 17 00:00:00 2001
From: John Harrison <[email protected]>
Date: Thu, 16 Oct 2025 13:02:09 -0700
Subject: [PATCH 2/2] Moving new ansi CSI helpers into AnsiTerminal.h

---
 lldb/include/lldb/Utility/AnsiTerminal.h | 11 +++++++++++
 lldb/tools/lldb-dap/tool/lldb-dap.cpp    | 13 +++----------
 2 files changed, 14 insertions(+), 10 deletions(-)

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 7a077d220e47f..66b352700de33 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"
@@ -122,15 +123,6 @@ 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);
@@ -274,7 +266,8 @@ static llvm::Error LaunchRunInTerminalTarget(llvm::opt::Arg 
&target_arg,
   } 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;
+    llvm::outs() << ANSI_CSI_RESET_CURSOR ANSI_CSI_ERASE_VIEWPORT
+            ANSI_CSI_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.

_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to