Use `std::wstring_convert()` instead of PAL-based conversion
authorIgor Kulaychuk <i.kulaychuk@samsung.com>
Tue, 6 Feb 2018 12:52:43 +0000 (15:52 +0300)
committerIgor Kulaychuk <i.kulaychuk@samsung.com>
Tue, 6 Feb 2018 12:54:03 +0000 (15:54 +0300)
src/debug/netcoredbg/cputil.h
src/debug/netcoredbg/expr.cpp
src/debug/netcoredbg/manageddebugger.cpp
src/debug/netcoredbg/modules.cpp
src/debug/netcoredbg/symbolreader.cpp
src/debug/netcoredbg/symbolreader.h
src/debug/netcoredbg/valuewalk.cpp
src/debug/netcoredbg/vscodeprotocol.cpp

index d9c6c13..b2563ef 100644 (file)
@@ -2,14 +2,17 @@
 // Distributed under the MIT License.
 // See the LICENSE file in the project root for more information.
 
+#include <string>
+#include <locale>
+#include <codecvt>
+
+static std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> convert;
+
 static inline std::string to_utf8(const WCHAR *wstr, int len = -1)
 {
     if (len == -1)
-        len = _wcslen(wstr);
+        return convert.to_bytes(wstr);
     if (len == 0)
         return std::string();
-    int size_needed = WideCharToMultiByte(CP_UTF8, 0, wstr, len, NULL, 0, NULL, NULL);
-    std::string strTo(size_needed, 0);
-    WideCharToMultiByte(CP_UTF8, 0, wstr, len, &strTo[0], size_needed, NULL, NULL);
-    return strTo;
+    return convert.to_bytes(wstr, wstr + len);
 }
index 8e079be..52b404b 100644 (file)
@@ -14,6 +14,8 @@
 #include <list>
 #include <iomanip>
 
+#include "cputil.h"
+
 #include "manageddebugger.h"
 #include "typeprinter.h"
 #include "valueprint.h"
@@ -136,14 +138,8 @@ HRESULT Evaluator::GetFieldOrPropertyWithName(ICorDebugThread *pThread,
 
 static mdTypeDef GetTypeTokenForName(IMetaDataImport *pMD, mdTypeDef tkEnclosingClass, const std::string &name)
 {
-    HRESULT Status;
-
-    std::unique_ptr<WCHAR[]> wname(new WCHAR[name.size() + 1]);
-
-    MultiByteToWideChar(CP_UTF8, 0, name.c_str(), name.size() + 1, &wname[0], name.size() + 1);
-
     mdTypeDef typeToken = mdTypeDefNil;
-    pMD->FindTypeDefByName(&wname[0], tkEnclosingClass, &typeToken);
+    pMD->FindTypeDefByName(convert.from_bytes(name).c_str(), tkEnclosingClass, &typeToken);
     return typeToken;
 }
 
index 06b6a98..6e66a59 100644 (file)
@@ -983,9 +983,6 @@ HRESULT ManagedDebugger::RunProcess(std::string fileExec, std::vector<std::strin
         ss << " \"" << EscapeShellArg(arg) << "\"";
     }
     std::string cmdString = ss.str();
-    std::unique_ptr<WCHAR[]> cmd(new WCHAR[cmdString.size() + 1]);
-
-    MultiByteToWideChar(CP_UTF8, 0, cmdString.c_str(), cmdString.size() + 1, &cmd[0], cmdString.size() + 1);
 
     m_startupReady = false;
     m_clrPath.clear();
@@ -994,7 +991,7 @@ HRESULT ManagedDebugger::RunProcess(std::string fileExec, std::vector<std::strin
     LPVOID lpEnvironment = nullptr; // as current
     LPCWSTR lpCurrentDirectory = nullptr; // as current
     HANDLE resumeHandle;
-    IfFailRet(CreateProcessForLaunch(&cmd[0], bSuspendProcess, lpEnvironment, lpCurrentDirectory, &m_processId, &resumeHandle));
+    IfFailRet(CreateProcessForLaunch(const_cast<LPWSTR>(convert.from_bytes(cmdString).c_str()), bSuspendProcess, lpEnvironment, lpCurrentDirectory, &m_processId, &resumeHandle));
 
     IfFailRet(RegisterForRuntimeStartup(m_processId, ManagedDebugger::StartupCallback, this, &m_unregisterToken));
 
@@ -1101,14 +1098,11 @@ HRESULT ManagedDebugger::AttachToProcess(int pid)
     if (m_clrPath.empty())
         return E_INVALIDARG; // Unable to find libcoreclr.so
 
-    WCHAR szModuleName[MAX_LONGPATH];
-    MultiByteToWideChar(CP_UTF8, 0, m_clrPath.c_str(), m_clrPath.size() + 1, szModuleName, MAX_LONGPATH);
-
     WCHAR pBuffer[100];
     DWORD dwLength;
     IfFailRet(CreateVersionStringFromModule(
         pid,
-        szModuleName,
+        convert.from_bytes(m_clrPath).c_str(),
         pBuffer,
         _countof(pBuffer),
         &dwLength));
index 6afe6df..a89f99f 100644 (file)
@@ -44,10 +44,6 @@ HRESULT Modules::GetLocationInAny(
 {
     HRESULT Status;
 
-    WCHAR nameBuffer[MAX_LONGPATH];
-
-    Status = MultiByteToWideChar(CP_UTF8, 0, filename.c_str(), filename.size() + 1, nameBuffer, MAX_LONGPATH);
-
     std::lock_guard<std::mutex> lock(m_modulesInfoMutex);
 
     for (auto &info_pair : m_modulesInfo)
@@ -56,7 +52,7 @@ HRESULT Modules::GetLocationInAny(
 
         CORDB_ADDRESS modAddress;
         IfFailRet(mdInfo.module->GetBaseAddress(&modAddress));
-        if (FAILED(mdInfo.symbols->ResolveSequencePoint(nameBuffer, linenum, modAddress, &methodToken, &ilOffset)))
+        if (FAILED(mdInfo.symbols->ResolveSequencePoint(filename.c_str(), linenum, modAddress, &methodToken, &ilOffset)))
             continue;
 
         WCHAR wFilename[MAX_LONGPATH];
@@ -83,10 +79,6 @@ HRESULT Modules::GetLocationInModule(
 {
     HRESULT Status;
 
-    WCHAR nameBuffer[MAX_LONGPATH];
-
-    Status = MultiByteToWideChar(CP_UTF8, 0, filename.c_str(), filename.size() + 1, nameBuffer, MAX_LONGPATH);
-
     CORDB_ADDRESS modAddress;
     IfFailRet(pModule->GetBaseAddress(&modAddress));
 
@@ -97,7 +89,7 @@ HRESULT Modules::GetLocationInModule(
         return E_FAIL;
     }
 
-    IfFailRet(info_pair->second.symbols->ResolveSequencePoint(nameBuffer, linenum, modAddress, &methodToken, &ilOffset));
+    IfFailRet(info_pair->second.symbols->ResolveSequencePoint(filename.c_str(), linenum, modAddress, &methodToken, &ilOffset));
 
     WCHAR wFilename[MAX_LONGPATH];
     ULONG resolvedLinenum;
index 66df6e9..e598080 100644 (file)
@@ -9,6 +9,7 @@
 #include <string>
 #include <vector>
 
+#include "cputil.h"
 #include "symbolreader.h"
 
 #include "platform.h"
@@ -81,14 +82,12 @@ HRESULT SymbolReader::LoadSymbolsForPortablePDB(
     }
 
     // The module name needs to be null for in-memory PE's.
-    ArrayHolder<char> szModuleName = nullptr;
+    const char *szModuleName = nullptr;
+    std::string moduleName;
     if (!isInMemory && pModuleName != nullptr)
     {
-        szModuleName = new char[MAX_LONGPATH];
-        if (WideCharToMultiByte(CP_ACP, 0, pModuleName, (int)(_wcslen(pModuleName) + 1), szModuleName, MAX_LONGPATH, NULL, NULL) == 0)
-        {
-            return E_FAIL;
-        }
+        moduleName = convert.to_bytes(pModuleName);
+        szModuleName = moduleName.c_str();
     }
 
     m_symbolReaderHandle = loadSymbolsForModuleDelegate(szModuleName, isFileLayout, peAddress,
@@ -113,14 +112,11 @@ HRESULT SymbolReader::PrepareSymbolReader()
 
     attemptedSymbolReaderPreparation = true;
 
-    WCHAR wszCoreClrPath[MAX_LONGPATH];
-    MultiByteToWideChar(CP_UTF8, 0, coreClrPath.c_str(), coreClrPath.size() + 1, wszCoreClrPath, MAX_LONGPATH);
-
     std::string clrDir = coreClrPath.substr(0, coreClrPath.rfind(DIRECTORY_SEPARATOR_STR_A));
 
     HRESULT Status;
 
-    HMODULE coreclrLib = LoadLibraryW(wszCoreClrPath);
+    HMODULE coreclrLib = LoadLibraryW(convert.from_bytes(coreClrPath).c_str());
     if (coreclrLib == nullptr)
     {
         fprintf(stderr, "Error: Failed to load coreclr\n");
@@ -198,7 +194,7 @@ HRESULT SymbolReader::PrepareSymbolReader()
     return Status;
 }
 
-HRESULT SymbolReader::ResolveSequencePoint(WCHAR* pFilename, ULONG32 lineNumber, TADDR mod, mdMethodDef* pToken, ULONG32* pIlOffset)
+HRESULT SymbolReader::ResolveSequencePoint(const char *filename, ULONG32 lineNumber, TADDR mod, mdMethodDef* pToken, ULONG32* pIlOffset)
 {
     HRESULT Status = S_OK;
 
@@ -206,12 +202,7 @@ HRESULT SymbolReader::ResolveSequencePoint(WCHAR* pFilename, ULONG32 lineNumber,
     {
         _ASSERTE(resolveSequencePointDelegate != nullptr);
 
-        char szName[mdNameLen];
-        if (WideCharToMultiByte(CP_ACP, 0, pFilename, (int)(_wcslen(pFilename) + 1), szName, mdNameLen, NULL, NULL) == 0)
-        {
-            return E_FAIL;
-        }
-        if (resolveSequencePointDelegate(m_symbolReaderHandle, szName, lineNumber, pToken, pIlOffset) == FALSE)
+        if (resolveSequencePointDelegate(m_symbolReaderHandle, filename, lineNumber, pToken, pIlOffset) == FALSE)
         {
             return E_FAIL;
         }
index fa7deeb..f27b649 100644 (file)
@@ -75,7 +75,7 @@ public:
     HRESULT LoadSymbols(IMetaDataImport* pMD, ICorDebugModule* pModule);
     HRESULT GetLineByILOffset(mdMethodDef MethodToken, ULONG64 IlOffset, ULONG *pLinenum, WCHAR* pwszFileName, ULONG cchFileName);
     HRESULT GetNamedLocalVariableAndScope(ICorDebugILFrame * pILFrame, mdMethodDef methodToken, ULONG localIndex, WCHAR* paramName, ULONG paramNameLen, ICorDebugValue **ppValue, ULONG32* pIlStart, ULONG32* pIlEnd);
-    HRESULT ResolveSequencePoint(WCHAR* pFilename, ULONG32 lineNumber, TADDR mod, mdMethodDef* pToken, ULONG32* pIlOffset);
+    HRESULT ResolveSequencePoint(const char *filename, ULONG32 lineNumber, TADDR mod, mdMethodDef* pToken, ULONG32* pIlOffset);
     HRESULT GetStepRangesFromIP(ULONG64 ip, mdMethodDef MethodToken, ULONG32 *ilStartOffset, ULONG32 *ilEndOffset);
     HRESULT GetSequencePoints(mdMethodDef methodToken, std::vector<SequencePoint> &points);
 };
index 5b98527..ed4107d 100644 (file)
@@ -841,19 +841,27 @@ HRESULT Evaluator::WalkStackVars(ICorDebugFrame *pFrame, WalkStackVarsCallback c
         {
             ULONG paramNameLen = 0;
             mdParamDef paramDef;
-            WCHAR wParamName[mdNameLen] = W("\0");
+            std::string paramName;
 
             bool thisParam = i == 0 && (methodAttr & mdStatic) == 0;
             if (thisParam)
-                swprintf_s(wParamName, mdNameLen, W("this\0"));
+                paramName = "this";
             else
             {
                 int idx = ((methodAttr & mdStatic) == 0)? i : (i + 1);
                 if(SUCCEEDED(pMD->GetParamForMethodIndex(methodDef, idx, &paramDef)))
+                {
+                    WCHAR wParamName[mdNameLen] = W("\0");
                     pMD->GetParamProps(paramDef, NULL, NULL, wParamName, mdNameLen, &paramNameLen, NULL, NULL, NULL, NULL);
+                    paramName = convert.to_bytes(wParamName);
+                }
+            }
+            if(paramName.empty())
+            {
+                std::stringstream ss;
+                ss << "param_" << i;
+                paramName = ss.str();
             }
-            if(_wcslen(wParamName) == 0)
-                swprintf_s(wParamName, mdNameLen, W("param_%d\0"), i);
 
             ToRelease<ICorDebugValue> pValue;
             ULONG cArgsFetched;
@@ -865,8 +873,6 @@ HRESULT Evaluator::WalkStackVars(ICorDebugFrame *pFrame, WalkStackVarsCallback c
             if (Status == S_FALSE)
                 break;
 
-            std::string paramName = to_utf8(wParamName/*, paramNameLen*/);
-
             if (thisParam)
             {
                 IfFailRet(HandleSpecialThisParam(pValue, pILFrame, locals, cb));
index 29d5854..ac55490 100644 (file)
@@ -504,11 +504,9 @@ void VSCodeProtocol::CommandLoop()
             }
             else
             {
-                WCHAR msgBuffer[1024] = {0};
-                HRESULT msgStatus = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, Status, 0, msgBuffer, 1024, nullptr);
                 std::stringstream ss;
                 ss << "Failed command '" << command << "' : "
-                   << "0x" << std::setw(8) << std::setfill('0') << std::hex << Status << " " << to_utf8(msgBuffer);
+                   << "0x" << std::setw(8) << std::setfill('0') << std::hex << Status;
                 response["success"] = false;
                 response["message"] = ss.str();
             }