Skip to content

[lldb][windows] force the console to use a UTF-8 codepage #149493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lldb/source/Plugins/Platform/Windows/PlatformWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions lldb/source/Plugins/Platform/Windows/PlatformWindows.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading