Skip to content

Commit d061528

Browse files
committed
Merge remote-tracking branch 'opcm-github/master' into main
Change-Id: I20bb283e23d98ed29ab66009ac0c1a72e33eef32
2 parents 3ed1b35 + 0d031d8 commit d061528

File tree

13 files changed

+89
-63
lines changed

13 files changed

+89
-63
lines changed

src/cpucounters.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ bool PCM::initWinRing0Lib()
104104

105105
BYTE major, minor, revision, release;
106106
GetDriverVersion(&major, &minor, &revision, &release);
107-
wchar_t buffer[128];
108-
swprintf_s(buffer, 128, _T("\\\\.\\WinRing0_%d_%d_%d"),(int)major,(int)minor, (int)revision);
107+
TCHAR buffer[128];
108+
_stprintf_s(buffer, 128, TEXT("\\\\.\\WinRing0_%d_%d_%d"),(int)major,(int)minor, (int)revision);
109109
restrictDriverAccess(buffer);
110110

111111
return true;
@@ -1089,11 +1089,11 @@ bool PCM::discoverSystemTopology()
10891089
}
10901090
else
10911091
{
1092-
std::wcerr << "Error in Windows function 'GetLogicalProcessorInformationEx': " <<
1092+
tcerr << "Error in Windows function 'GetLogicalProcessorInformationEx': " <<
10931093
GetLastError() << " ";
10941094
const TCHAR * strError = _com_error(GetLastError()).ErrorMessage();
1095-
if (strError) std::wcerr << strError;
1096-
std::wcerr << "\n";
1095+
if (strError) tcerr << strError;
1096+
tcerr << "\n";
10971097
return false;
10981098
}
10991099
}
@@ -2158,8 +2158,8 @@ PCM::PCM() :
21582158
// drv.stop(); // restart driver (usually not needed)
21592159
if (!drv.start())
21602160
{
2161-
std::wcerr << "Cannot access CPU counters\n";
2162-
std::wcerr << "You must have a signed driver at " << drv.driverPath() << " and have administrator rights to run this program\n";
2161+
tcerr << "Cannot access CPU counters\n";
2162+
tcerr << "You must have a signed driver at " << drv.driverPath() << " and have administrator rights to run this program\n";
21632163
return;
21642164
}
21652165
#endif

src/cpucounters.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
namespace pcm {
6767

6868
#ifdef _MSC_VER
69-
void PCM_API restrictDriverAccess(LPCWSTR path);
69+
void PCM_API restrictDriverAccess(LPCTSTR path);
7070
#endif
7171

7272
class SystemCounterState;

src/mmio.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ class PCMPmem : public WinPmem {
4343
switch (sys_info.wProcessorArchitecture)
4444
{
4545
case PROCESSOR_ARCHITECTURE_AMD64:
46-
wcscat_s(driver_filename, MAX_PATH, L"\\winpmem_x64.sys");
46+
_tcscat_s(driver_filename, MAX_PATH, TEXT("\\winpmem_x64.sys"));
4747
if (GetFileAttributes(driver_filename) == INVALID_FILE_ATTRIBUTES)
4848
{
4949
std::cerr << "ERROR: winpmem_x64.sys not found in current directory. Download it from https://github.com/Velocidex/WinPmem/blob/master/kernel/binaries/winpmem_x64.sys .\n";
5050
std::cerr << "ERROR: Memory bandwidth statistics will not be available.\n";
5151
}
5252
break;
5353
case PROCESSOR_ARCHITECTURE_INTEL:
54-
wcscat_s(driver_filename, MAX_PATH, L"\\winpmem_x86.sys");
54+
_tcscat_s(driver_filename, MAX_PATH, TEXT("\\winpmem_x86.sys"));
5555
if (GetFileAttributes(driver_filename) == INVALID_FILE_ATTRIBUTES)
5656
{
5757
std::cerr << "ERROR: winpmem_x86.sys not found in current directory. Download it from https://github.com/Velocidex/WinPmem/blob/master/kernel/binaries/winpmem_x86.sys .\n";

src/pcm-memory.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,8 +1130,8 @@ int main(int argc, char * argv[])
11301130
Driver tmpDrvObject = Driver(Driver::msrLocalPath());
11311131
if (!tmpDrvObject.start())
11321132
{
1133-
wcerr << "Can not access CPU counters\n";
1134-
wcerr << "You must have a signed driver at " << tmpDrvObject.driverPath() << " and have administrator rights to run this program\n";
1133+
tcerr << "Can not access CPU counters\n";
1134+
tcerr << "You must have a signed driver at " << tmpDrvObject.driverPath() << " and have administrator rights to run this program\n";
11351135
exit(EXIT_FAILURE);
11361136
}
11371137
exit(EXIT_SUCCESS);

src/pcm-msr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ int main(int argc, char * argv[])
8383
// drv.stop(); // restart driver (usually not needed)
8484
if (!drv.start())
8585
{
86-
std::wcerr << "Can not load MSR driver.\n";
87-
std::wcerr << "You must have a signed driver at " << drv.driverPath() << " and have administrator rights to run this program\n";
86+
tcerr << "Can not load MSR driver.\n";
87+
tcerr << "You must have a signed driver at " << drv.driverPath() << " and have administrator rights to run this program\n";
8888
return -1;
8989
}
9090
#endif

src/pcm-pcicfg.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ int main(int argc, char * argv[])
8383
// drv.stop(); // restart driver (usually not needed)
8484
if (!drv.start())
8585
{
86-
std::wcerr << "Can not load MSR driver.\n";
87-
std::wcerr << "You must have a signed driver at " << drv.driverPath() << " and have administrator rights to run this program\n";
86+
tcerr << "Can not load MSR driver.\n";
87+
tcerr << "You must have a signed driver at " << drv.driverPath() << " and have administrator rights to run this program\n";
8888
return -1;
8989
}
9090
#endif

src/pcm.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,8 +1306,8 @@ int main(int argc, char * argv[])
13061306
Driver tmpDrvObject = Driver(Driver::msrLocalPath());
13071307
if (!tmpDrvObject.start())
13081308
{
1309-
wcerr << "Can not access CPU counters\n";
1310-
wcerr << "You must have a signed driver at " << tmpDrvObject.driverPath() << " and have administrator rights to run this program\n";
1309+
tcerr << "Can not access CPU counters\n";
1310+
tcerr << "You must have a signed driver at " << tmpDrvObject.driverPath() << " and have administrator rights to run this program\n";
13111311
exit(EXIT_FAILURE);
13121312
}
13131313
exit(EXIT_SUCCESS);

src/utils.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,18 +264,18 @@ void set_signal_handlers(void)
264264
// to fix Cygwin/BASH setting Ctrl+C handler need first to restore the default one
265265
handlerStatus = SetConsoleCtrlHandler(NULL, FALSE); // restores normal processing of CTRL+C input
266266
if (handlerStatus == 0) {
267-
std::wcerr << "Failed to set Ctrl+C handler. Error code: " << GetLastError() << " ";
267+
tcerr << "Failed to set Ctrl+C handler. Error code: " << GetLastError() << " ";
268268
const TCHAR * errorStr = _com_error(GetLastError()).ErrorMessage();
269-
if (errorStr) std::wcerr << errorStr;
270-
std::wcerr << "\n";
269+
if (errorStr) tcerr << errorStr;
270+
tcerr << "\n";
271271
_exit(EXIT_FAILURE);
272272
}
273273
handlerStatus = SetConsoleCtrlHandler((PHANDLER_ROUTINE)sigINT_handler, TRUE);
274274
if (handlerStatus == 0) {
275-
std::wcerr << "Failed to set Ctrl+C handler. Error code: " << GetLastError() << " ";
275+
tcerr << "Failed to set Ctrl+C handler. Error code: " << GetLastError() << " ";
276276
const TCHAR * errorStr = _com_error(GetLastError()).ErrorMessage();
277-
if (errorStr) std::wcerr << errorStr;
278-
std::wcerr << "\n";
277+
if (errorStr) tcerr << errorStr;
278+
tcerr << "\n";
279279
_exit(EXIT_FAILURE);
280280
}
281281
SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)&unhandled_exception_handler);

src/utils.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@
2929

3030
namespace pcm {
3131

32+
#ifdef _MSC_VER
33+
using tstring = std::basic_string<TCHAR>;
34+
#ifdef UNICODE
35+
static auto& tcerr = std::wcerr;
36+
#else
37+
static auto& tcerr = std::cerr;
38+
#endif
39+
#endif // _MSC_VER
40+
3241
void exit_cleanup(void);
3342
void set_signal_handlers(void);
3443
void set_real_time_priority(const bool & silent);
@@ -80,7 +89,7 @@ void MySystem(char * sysCmd, char ** argc);
8089
#endif
8190
struct null_stream : public std::streambuf
8291
{
83-
void overflow(char) { }
92+
int_type overflow(int_type) override { return {}; }
8493
};
8594
#ifdef __GCC__
8695
#pragma GCC diagnostic pop
@@ -496,7 +505,7 @@ std::string safe_getenv(const char* env);
496505
#ifdef _MSC_VER
497506
inline HANDLE openMSRDriver()
498507
{
499-
return CreateFile(L"\\\\.\\RDMSR", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
508+
return CreateFile(TEXT("\\\\.\\RDMSR"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
500509
}
501510
#endif
502511

src/windows/PCMService.h

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
#include "pcm-lib.h"
1111
#include "windriver.h"
1212
#include <stdexcept>
13+
14+
#ifndef UNICODE
15+
#include <locale>
16+
#include <codecvt>
17+
#endif
1318
#pragma managed
1419

1520
using namespace pcm;
@@ -747,29 +752,29 @@ namespace PCMServiceNS {
747752

748753
// Read configuration values from registry
749754
HKEY hkey;
750-
if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\pcm\\service", NULL, KEY_READ, &hkey))
755+
if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\pcm\\service"), NULL, KEY_READ, &hkey))
751756
{
752757
DWORD regDWORD = static_cast<DWORD>(REG_DWORD);
753758
DWORD lenDWORD = 32;
754759

755760
DWORD sampleRateRead(0);
756-
if (ERROR_SUCCESS == RegQueryValueEx(hkey, L"SampleRate", NULL, NULL, reinterpret_cast<LPBYTE>(&sampleRateRead), &lenDWORD))
761+
if (ERROR_SUCCESS == RegQueryValueEx(hkey, TEXT("SampleRate"), NULL, NULL, reinterpret_cast<LPBYTE>(&sampleRateRead), &lenDWORD))
757762
{
758763
sampleRate = (int)sampleRateRead;
759764
}
760765

761766
DWORD collectCoreRead(0);
762-
if (ERROR_SUCCESS == RegQueryValueEx(hkey, L"CollectCore", NULL, NULL, reinterpret_cast<LPBYTE>(&collectCoreRead), &lenDWORD)) {
767+
if (ERROR_SUCCESS == RegQueryValueEx(hkey, TEXT("CollectCore"), NULL, NULL, reinterpret_cast<LPBYTE>(&collectCoreRead), &lenDWORD)) {
763768
collectionInformation->core = (int)collectCoreRead > 0;
764769
}
765770

766771
DWORD collectSocketRead(0);
767-
if (ERROR_SUCCESS == RegQueryValueEx(hkey, L"CollectSocket", NULL, NULL, reinterpret_cast<LPBYTE>(&collectSocketRead), &lenDWORD)) {
772+
if (ERROR_SUCCESS == RegQueryValueEx(hkey, TEXT("CollectSocket"), NULL, NULL, reinterpret_cast<LPBYTE>(&collectSocketRead), &lenDWORD)) {
768773
collectionInformation->socket = (int)collectSocketRead > 0;
769774
}
770775

771776
DWORD collectQpiRead(0);
772-
if (ERROR_SUCCESS == RegQueryValueEx(hkey, L"CollectQpi", NULL, NULL, reinterpret_cast<LPBYTE>(&collectQpiRead), &lenDWORD)) {
777+
if (ERROR_SUCCESS == RegQueryValueEx(hkey, TEXT("CollectQpi"), NULL, NULL, reinterpret_cast<LPBYTE>(&collectQpiRead), &lenDWORD)) {
773778
collectionInformation->qpi = (int)collectQpiRead > 0;
774779
}
775780

@@ -782,7 +787,13 @@ namespace PCMServiceNS {
782787
drv_ = new Driver;
783788
if (!drv_->start())
784789
{
785-
String^ s = gcnew String((L"Cannot open the driver.\nYou must have a signed driver at " + drv_->driverPath() + L" and have administrator rights to run this program.\n\n").c_str());
790+
#ifdef UNICODE
791+
const auto& driverPath = drv_->driverPath();
792+
#else
793+
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> char_to_wide;
794+
std::wstring driverPath = char_to_wide.from_bytes(drv_->driverPath().c_str());
795+
#endif
796+
String^ s = gcnew String((L"Cannot open the driver.\nYou must have a signed driver at " + driverPath + L" and have administrator rights to run this program.\n\n").c_str());
786797
EventLog->WriteEntry(Globals::ServiceName, s, EventLogEntryType::Error);
787798
SetServiceFail(ERROR_FILE_NOT_FOUND);
788799
throw gcnew Exception(s);

0 commit comments

Comments
 (0)