Skip to content

SOS: Wide char output #52

Closed
Closed

Description

@mikem8361

@bendono commented on Wed Oct 12 2016

While recently debugging an issue, I noticed that SOS was unable to output Unicode text properly. This has resulted in various obscure workarounds such replacing non-ASCII characters with dots as seen here. Note that printf here is really ExtOut, which uses IDebugControl2 to output text. The problem is that IDebugControl2 only supports multibyte strings. A better option is to use IDebugControl4 which also supports Unicode strings.

I would suggest the following changes which fallback to IDebugControl2 if the IDebugControl4 interface is for whatever reason unavailable:

void ExtOut(PCWSTR Format, ...)
{
    if (Output::IsOutputSuppressed())
        return;

    // Get instance of IDebugControl4 to output wide strings.
    IDebugControl4* debugControl4 = nullptr;
    if (SUCCEEDED(g_ExtClient->QueryInterface(__uuidof(IDebugControl4), (void**)&debugControl4)))
    {
        ExtOutIndent();

        va_list Args;
        va_start(Args, Format);
        debugControl4->OutputVaListWide(DEBUG_OUTPUT_NORMAL, Format, Args);
        va_end(Args);

        EXT_RELEASE(debugControl4);
    }
    else
    {
        // Unable to obtain IDebugControl4. Use IDebugControl as an alternative.
        // Convert wide string to multibyte string.
        IDebugControl* debugControl = nullptr;
        if (SUCCEEDED(g_ExtClient->QueryInterface(__uuidof(IDebugControl), (void**)&debugControl)))
        {
            // Convert wide string to multibyte.
            // Determine required size of multibyte buffer.
            int bufferSize = WideCharToMultiByte(CP_ACP, 0, Format, -1, NULL, 0, NULL, NULL);

            if (bufferSize != 0)
            {
                LPSTR multibyteFormat = new CHAR[bufferSize];

                if (WideCharToMultiByte(CP_ACP, 0, Format, -1, multibyteFormat, bufferSize, NULL, NULL) != 0)
                {
                    ExtOutIndent();

                    va_list Args;
                    va_start(Args, Format);
                    debugControl->OutputVaList(DEBUG_OUTPUT_NORMAL, multibyteFormat, Args);
                    va_end(Args);
                }

                delete multibyteFormat;
            }

            EXT_RELEASE(debugControl);
        }
    }
}

Questions:

  1. Would a pull request be accepted changing ExtOut(PCSTR format, ...) to ExtOut(PCWSTR format, ...)? Similar changes would be needed for ExtWarn, ExtErr, and DMLOut.
  2. This change would require prefixing MANY string literals as Unicode. Is there a preference for L, W, T(), TEXT() or another such prefix? This change alone is the largest, so I'd prefer to get this right the first time.
  3. Are modern techniques such as variadic templates permitted in the codebase? This could enable code share between the ExtOut, ExtWarn, and ExtErr which only differ in the DEBUG_OUTPUT_* level.
  4. Another option is to leave ExtOut, ExtWarn and ExtErr alone but add new ExtOutEx, ExtWarnEx, and ExtErrEx functions that take a wide string. Or just add overloads that accept wide strings. Then select portions of the code can be updated to use the newer functions as necessary. This would result in a lot less code churn.

Please let me know your opinions.


@jkotas commented on Wed Oct 12 2016

cc @mikem8361


@mikem8361 commented on Tue Oct 18 2016

I wouldn't recommend actually changing the existing output functions, but either add the Unicode overrides or new "Ex" functions like you suggest.

But the biggest problem is that for xplat support (Linux/OS X) the IDebugControl2 is emulated on top of LLDB apis. It will take some more work to write the Unicode output functions on top of the LLDB apis. The LLDB output functions only takes ANSI strings so conversion needs to be done. And the LLDB/C++ runtime defines Unicode strings (wchar_t *) as UTF32 instead of UTF16 which is what SOS uses.

All this adds up to a chunck of work that isn't that high of priority compared to my other workitems. I might eventually get to it but I have no time frame right now.

Activity

  1. self-assigned this
    on Jul 30, 2018
  2. added this to the Future milestone on Jul 30, 2018
  3. ghost locked as resolved and limited conversation to collaborators on Jun 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions