[Lldb-commits] [lldb] [windows][lldb] force the console to use a UTF-8 codepage (PR #149493)

Charles Zablit via lldb-commits lldb-commits at lists.llvm.org
Fri Jul 18 03:39:24 PDT 2025


https://github.com/charles-zablit updated https://github.com/llvm/llvm-project/pull/149493

>From 77bf2b7acb82ea930702c8b0587c019fd48dc0f2 Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Fri, 18 Jul 2025 12:29:31 +0200
Subject: [PATCH 1/2] [windows][lldb] force the console to use a UTF-8 codepage

---
 .../Platform/Windows/PlatformWindows.cpp      | 20 +++++++++++++++++++
 .../Platform/Windows/PlatformWindows.h        |  8 ++++++++
 2 files changed, 28 insertions(+)

diff --git a/lldb/source/Plugins/Platform/Windows/PlatformWindows.cpp b/lldb/source/Plugins/Platform/Windows/PlatformWindows.cpp
index c0c26cc5f1954..d3e981de81313 100644
--- a/lldb/source/Plugins/Platform/Windows/PlatformWindows.cpp
+++ b/lldb/source/Plugins/Platform/Windows/PlatformWindows.cpp
@@ -41,6 +41,10 @@ LLDB_PLUGIN_DEFINE(PlatformWindows)
 
 static uint32_t g_initialize_count = 0;
 
+#if defined(_WIN32)
+std::optional<UINT> g_prev_console_cp = std::nullopt;
+#endif
+
 PlatformSP PlatformWindows::CreateInstance(bool force,
                                            const lldb_private::ArchSpec *arch) {
   // The only time we create an instance is when we are creating a remote
@@ -98,6 +102,7 @@ void PlatformWindows::Initialize() {
     default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
     Platform::SetHostPlatform(default_platform_sp);
 #endif
+    SetConsoleCodePage();
     PluginManager::RegisterPlugin(
         PlatformWindows::GetPluginNameStatic(false),
         PlatformWindows::GetPluginDescriptionStatic(false),
@@ -108,6 +113,7 @@ void PlatformWindows::Initialize() {
 void PlatformWindows::Terminate() {
   if (g_initialize_count > 0) {
     if (--g_initialize_count == 0) {
+      ResetConsoleCodePage();
       PluginManager::UnregisterPlugin(PlatformWindows::CreateInstance);
     }
   }
@@ -808,3 +814,17 @@ extern "C" {
 
   return Status();
 }
+
+void PlatformWindows::SetConsoleCodePage() {
+  #if defined(_WIN32)
+    g_prev_console_cp = GetConsoleOutputCP();
+    SetConsoleOutputCP(CP_UTF8);
+  #endif
+}
+
+void PlatformWindows::ResetConsoleCodePage() {
+  #if defined(_WIN32)
+  if (g_prev_console_cp)
+    SetConsoleOutputCP(*g_prev_console_cp);
+  #endif
+}
diff --git a/lldb/source/Plugins/Platform/Windows/PlatformWindows.h b/lldb/source/Plugins/Platform/Windows/PlatformWindows.h
index 771133f341e90..d14aa52e5e1c8 100644
--- a/lldb/source/Plugins/Platform/Windows/PlatformWindows.h
+++ b/lldb/source/Plugins/Platform/Windows/PlatformWindows.h
@@ -80,6 +80,14 @@ class PlatformWindows : public RemoteAwarePlatform {
   size_t GetSoftwareBreakpointTrapOpcode(Target &target,
                                          BreakpointSite *bp_site) override;
 
+  /// Set the current console's code page to UTF-8 and store the previous
+  /// codepage in \a g_prev_console_cp.
+  static void SetConsoleCodePage();
+
+  /// Reset the current console's code page to the value stored
+  /// in \a g_prev_console_cp if any.
+  static void ResetConsoleCodePage();
+
   std::vector<ArchSpec> m_supported_architectures;
 
 private:

>From fcec07f707c9e854e0cded19d0052bec63e0d26f Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Fri, 18 Jul 2025 12:39:11 +0200
Subject: [PATCH 2/2] fixup! [windows][lldb] force the console to use a UTF-8
 codepage

---
 .../Plugins/Platform/Windows/PlatformWindows.cpp     | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/lldb/source/Plugins/Platform/Windows/PlatformWindows.cpp b/lldb/source/Plugins/Platform/Windows/PlatformWindows.cpp
index d3e981de81313..dffbbc1c2806a 100644
--- a/lldb/source/Plugins/Platform/Windows/PlatformWindows.cpp
+++ b/lldb/source/Plugins/Platform/Windows/PlatformWindows.cpp
@@ -816,15 +816,15 @@ extern "C" {
 }
 
 void PlatformWindows::SetConsoleCodePage() {
-  #if defined(_WIN32)
-    g_prev_console_cp = GetConsoleOutputCP();
-    SetConsoleOutputCP(CP_UTF8);
-  #endif
+#if defined(_WIN32)
+  g_prev_console_cp = GetConsoleOutputCP();
+  SetConsoleOutputCP(CP_UTF8);
+#endif
 }
 
 void PlatformWindows::ResetConsoleCodePage() {
-  #if defined(_WIN32)
+#if defined(_WIN32)
   if (g_prev_console_cp)
     SetConsoleOutputCP(*g_prev_console_cp);
-  #endif
+#endif
 }



More information about the lldb-commits mailing list