-
Couldn't load subscription status.
- Fork 5.2k
Start to use STL and stdio in superpmi #116422
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
base: main
Are you sure you want to change the base?
Changes from 29 commits
688e2b0
6df1501
0ba0e96
ff5058e
01f4be7
0a95bc9
84b95f3
9d8e705
137e9e3
65cdfa5
67d2f11
ce5d486
93224cf
08d99e4
b22e3a9
407af70
af049c7
9f1fcc8
70152a8
4db4066
58183b3
b3ce44d
7e81613
c169695
f0f1857
b1d2987
3d10f49
899f12d
16f6ee1
dd225a6
8db0353
d61d34b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -9,6 +9,8 @@ | |||||
| #include "logging.h" | ||||||
| #include "spmiutil.h" | ||||||
|
|
||||||
| #include <sstream> | ||||||
| #include <iomanip> | ||||||
| #include <minipal/debugger.h> | ||||||
| #include <minipal/random.h> | ||||||
|
|
||||||
|
|
@@ -99,86 +101,103 @@ WCHAR* GetEnvironmentVariableWithDefaultW(const WCHAR* envVarName, const WCHAR* | |||||
| return retString; | ||||||
| } | ||||||
|
|
||||||
| #ifdef TARGET_UNIX | ||||||
| // For some reason, the PAL doesn't have GetCommandLineA(). So write it. | ||||||
| LPSTR GetCommandLineA() | ||||||
| const char* GetEnvWithDefault(const char* envVarName, const char* defaultValue) | ||||||
| { | ||||||
| LPSTR pCmdLine = nullptr; | ||||||
| LPWSTR pwCmdLine = GetCommandLineW(); | ||||||
| // getenv isn't thread safe, but it's simple and sufficient since we are development-only tool | ||||||
| char* env = getenv(envVarName); | ||||||
| return env ? env : defaultValue; | ||||||
| } | ||||||
|
|
||||||
| if (pwCmdLine != nullptr) | ||||||
| std::string GetProcessCommandLine() | ||||||
| { | ||||||
| #ifdef TARGET_WINDOWS | ||||||
| return ::GetCommandLineA(); | ||||||
| #else | ||||||
| FILE* fp = fopen("/proc/self/cmdline", "r"); | ||||||
| if (fp != NULL) | ||||||
| { | ||||||
| // Convert to ASCII | ||||||
| std::string result; | ||||||
| char* cmdLine = nullptr; | ||||||
| size_t size = 0; | ||||||
|
|
||||||
| int n = WideCharToMultiByte(CP_ACP, 0, pwCmdLine, -1, nullptr, 0, nullptr, nullptr); | ||||||
| if (n == 0) | ||||||
| while (getdelim(&cmdLine, &size, '\0', fp) != -1) | ||||||
| { | ||||||
| LogError("MultiByteToWideChar failed %d", GetLastError()); | ||||||
| return nullptr; | ||||||
| } | ||||||
| // /proc/self/cmdline uses \0 as delimeter, convert it to space | ||||||
| if (!result.empty()) | ||||||
| result += ' '; | ||||||
|
|
||||||
| pCmdLine = new char[n]; | ||||||
| result += cmdLine; | ||||||
jkotas marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| int n2 = WideCharToMultiByte(CP_ACP, 0, pwCmdLine, -1, pCmdLine, n, nullptr, nullptr); | ||||||
| if ((n2 == 0) || (n2 != n)) | ||||||
| { | ||||||
| LogError("MultiByteToWideChar failed %d", GetLastError()); | ||||||
| return nullptr; | ||||||
| free(cmdLine); | ||||||
| cmdLine = nullptr; | ||||||
| size = 0; | ||||||
| } | ||||||
|
|
||||||
| fclose(fp); | ||||||
| return result; | ||||||
| } | ||||||
|
|
||||||
| return pCmdLine; | ||||||
| return ""; | ||||||
| #endif | ||||||
| } | ||||||
| #endif // TARGET_UNIX | ||||||
|
|
||||||
| bool LoadRealJitLib(HMODULE& jitLib, WCHAR* jitLibPath) | ||||||
| bool LoadRealJitLib(HMODULE& jitLib, const std::string& jitLibPath) | ||||||
| { | ||||||
| // Load Library | ||||||
| if (jitLib == NULL) | ||||||
| { | ||||||
| if (jitLibPath == nullptr) | ||||||
| if (jitLibPath.empty()) | ||||||
| { | ||||||
| LogError("LoadRealJitLib - No real jit path"); | ||||||
| return false; | ||||||
| } | ||||||
| jitLib = ::LoadLibraryExW(jitLibPath, NULL, 0); | ||||||
| #ifdef TARGET_WINDOWS | ||||||
| jitLib = ::LoadLibraryExA(jitLibPath.c_str(), NULL, 0); | ||||||
| if (jitLib == NULL) | ||||||
| { | ||||||
| LogError("LoadRealJitLib - LoadLibrary failed to load '%ws' (0x%08x)", jitLibPath, ::GetLastError()); | ||||||
| LogError("LoadRealJitLib - LoadLibrary failed to load '%s' (%d)", jitLibPath.c_str(), ::GetLastError()); | ||||||
|
||||||
| LogError("LoadRealJitLib - LoadLibrary failed to load '%s' (%d)", jitLibPath.c_str(), ::GetLastError()); | |
| LogError("LoadRealJitLib - LoadLibrary failed to load '%s' (0x%08x)", jitLibPath.c_str(), ::GetLastError()); |
Win32 error codes are best expressed as hex.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Win32 error codes are best expressed as hex.
Nit: Win32 error code != HRESULTs. I believe Win32 error codes are typically displayed in decimal. For example, our own Win32Exception prints them as decimal - Console.WriteLine(new Win32Exception(3 /* ERROR_PATH_NOT_FOUND */)); gives System.ComponentModel.Win32Exception (3): The system cannot find the path specified..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This particular case was using hex format, while there are other places using decimal. I tend to keep it as-is for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Win32 error code != HRESULTs.
Agree. That is why I stated Win32 error code :)
I believe Win32 error codes are typically displayed in decimal.
Sadly, yes. That has been the most common experience with user facing scenarios and I continue to believe it isn't a helpful display format. The most common Win32 error codes (for example, 3, 5 ,etc) are sub 10, which display the same. The higher ones are less common and often also used with HRESULT and LSTATUS. Consistently displaying them as hex avoids the common signed issue (that is, using %d instead of %u) and allows for easier pattern recognition in logs.
In a better world, our error system would have something as simple as dlerror() as opposed to the painfully baroque FormatMessage().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it isn't a helpful display format.
I believe that it is the common established display format for Windows error code. For example, compare these search results - the first returns good answer for me, the second one returns confusing garbage.
https://www.bing.com/search?q=windows+error+code+3
https://www.bing.com/search?q=windows+error+code+0x00000003
The higher ones are less common and often also used with HRESULT and LSTATUS.
Right, these are different enums that are not interchangeable. I do not have a problem with using hex for HRESULT.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are ~100 places that print GetLastError in superpmi. ~85% print it as decimal.
Uh oh!
There was an error while loading. Please reload this page.