2017-12-21 07:45:38 +08:00
|
|
|
/*
|
2025-02-07 21:56:19 +08:00
|
|
|
* Copyright (C) 2018-2025 Intel Corporation
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
2018-09-18 15:11:08 +08:00
|
|
|
* SPDX-License-Identifier: MIT
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/os_interface/windows/debug_registry_reader.h"
|
2019-08-29 18:15:33 +08:00
|
|
|
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/debug_settings/debug_settings_manager.h"
|
2023-08-20 12:31:48 +08:00
|
|
|
#include "shared/source/helpers/api_specific_config.h"
|
2020-08-24 07:55:47 +08:00
|
|
|
#include "shared/source/os_interface/windows/sys_calls.h"
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/os_interface/windows/windows_wrapper.h"
|
|
|
|
#include "shared/source/utilities/debug_settings_reader.h"
|
2019-02-27 18:39:32 +08:00
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
namespace NEO {
|
2018-07-02 00:44:29 +08:00
|
|
|
|
2019-09-02 18:04:22 +08:00
|
|
|
SettingsReader *SettingsReader::createOsReader(bool userScope, const std::string ®Key) {
|
|
|
|
return new RegistryReader(userScope, regKey);
|
2018-11-30 18:51:49 +08:00
|
|
|
}
|
|
|
|
|
2019-09-02 18:04:22 +08:00
|
|
|
RegistryReader::RegistryReader(bool userScope, const std::string ®Key) : registryReadRootKey(regKey) {
|
2020-03-19 21:42:49 +08:00
|
|
|
hkeyType = userScope ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE;
|
2018-11-30 18:51:49 +08:00
|
|
|
setUpProcessName();
|
|
|
|
}
|
2019-09-02 18:04:22 +08:00
|
|
|
|
2018-10-10 22:02:19 +08:00
|
|
|
void RegistryReader::setUpProcessName() {
|
|
|
|
char buff[MAX_PATH];
|
|
|
|
GetModuleFileNameA(nullptr, buff, MAX_PATH);
|
|
|
|
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
|
|
|
|
processName = "";
|
|
|
|
}
|
|
|
|
processName.assign(buff);
|
|
|
|
}
|
|
|
|
const char *RegistryReader::appSpecificLocation(const std::string &name) {
|
|
|
|
if (processName.length() > 0)
|
|
|
|
return processName.c_str();
|
|
|
|
return name.c_str();
|
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2023-08-20 12:31:48 +08:00
|
|
|
bool RegistryReader::getSetting(const char *settingName, bool defaultValue, DebugVarPrefix &type) {
|
|
|
|
return getSetting(settingName, static_cast<int32_t>(defaultValue), type) ? true : false;
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
bool RegistryReader::getSetting(const char *settingName, bool defaultValue) {
|
2020-04-09 16:48:17 +08:00
|
|
|
return getSetting(settingName, static_cast<int32_t>(defaultValue)) ? true : false;
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
2023-08-20 12:31:48 +08:00
|
|
|
int32_t RegistryReader::getSetting(const char *settingName, int32_t defaultValue, DebugVarPrefix &type) {
|
|
|
|
return static_cast<int32_t>(getSetting(settingName, static_cast<int64_t>(defaultValue), type));
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
int32_t RegistryReader::getSetting(const char *settingName, int32_t defaultValue) {
|
2020-03-27 14:47:16 +08:00
|
|
|
return static_cast<int32_t>(getSetting(settingName, static_cast<int64_t>(defaultValue)));
|
|
|
|
}
|
|
|
|
|
2023-08-20 12:31:48 +08:00
|
|
|
bool RegistryReader::getSettingIntCommon(const char *settingName, int64_t &value) {
|
2023-10-30 19:52:17 +08:00
|
|
|
HKEY key{};
|
2017-12-21 07:45:38 +08:00
|
|
|
DWORD success = ERROR_SUCCESS;
|
2023-08-20 12:31:48 +08:00
|
|
|
bool retVal = false;
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2021-10-05 19:21:46 +08:00
|
|
|
success = SysCalls::regOpenKeyExA(hkeyType,
|
|
|
|
registryReadRootKey.c_str(),
|
|
|
|
0,
|
|
|
|
KEY_READ,
|
2023-10-30 19:52:17 +08:00
|
|
|
&key);
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
if (ERROR_SUCCESS == success) {
|
2020-03-27 14:47:16 +08:00
|
|
|
DWORD size = sizeof(int64_t);
|
|
|
|
int64_t regData;
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2023-10-30 19:52:17 +08:00
|
|
|
success = SysCalls::regQueryValueExA(key,
|
2021-10-05 19:21:46 +08:00
|
|
|
settingName,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
reinterpret_cast<LPBYTE>(®Data),
|
|
|
|
&size);
|
2020-03-20 03:51:30 +08:00
|
|
|
if (ERROR_SUCCESS == success) {
|
|
|
|
value = regData;
|
2023-08-20 12:31:48 +08:00
|
|
|
retVal = true;
|
2020-03-20 03:51:30 +08:00
|
|
|
}
|
2023-10-30 19:52:17 +08:00
|
|
|
RegCloseKey(key);
|
2020-03-20 03:51:30 +08:00
|
|
|
}
|
2023-08-20 12:31:48 +08:00
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t RegistryReader::getSetting(const char *settingName, int64_t defaultValue, DebugVarPrefix &type) {
|
|
|
|
int64_t value = defaultValue;
|
|
|
|
|
|
|
|
if (!(getSettingIntCommon(settingName, value))) {
|
|
|
|
char *envValue;
|
|
|
|
|
2023-09-21 22:02:00 +08:00
|
|
|
auto prefixString = ApiSpecificConfig::getPrefixStrings();
|
|
|
|
auto prefixType = ApiSpecificConfig::getPrefixTypes();
|
2023-08-20 12:31:48 +08:00
|
|
|
|
|
|
|
uint32_t i = 0;
|
|
|
|
for (const auto &prefix : prefixString) {
|
2023-09-07 20:44:36 +08:00
|
|
|
std::string neoKey = prefix;
|
|
|
|
neoKey += settingName;
|
|
|
|
envValue = IoFunctions::getenvPtr(neoKey.c_str());
|
2023-08-20 12:31:48 +08:00
|
|
|
if (envValue) {
|
|
|
|
value = atoll(envValue);
|
|
|
|
type = prefixType[i];
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2023-12-05 20:06:54 +08:00
|
|
|
type = DebugVarPrefix::none;
|
2023-08-20 12:31:48 +08:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t RegistryReader::getSetting(const char *settingName, int64_t defaultValue) {
|
|
|
|
int64_t value = defaultValue;
|
|
|
|
|
|
|
|
if (!(getSettingIntCommon(settingName, value))) {
|
2023-09-07 20:44:36 +08:00
|
|
|
const char *envValue = IoFunctions::getenvPtr(settingName);
|
2019-10-09 17:40:50 +08:00
|
|
|
if (envValue) {
|
2022-09-09 21:19:21 +08:00
|
|
|
value = atoll(envValue);
|
2019-10-09 17:40:50 +08:00
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2023-08-20 12:31:48 +08:00
|
|
|
bool RegistryReader::getSettingStringCommon(const char *settingName, std::string &keyValue) {
|
2023-10-30 19:52:17 +08:00
|
|
|
HKEY key{};
|
2017-12-21 07:45:38 +08:00
|
|
|
DWORD success = ERROR_SUCCESS;
|
2023-08-20 12:31:48 +08:00
|
|
|
bool retVal = false;
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2021-10-05 19:21:46 +08:00
|
|
|
success = SysCalls::regOpenKeyExA(hkeyType,
|
|
|
|
registryReadRootKey.c_str(),
|
|
|
|
0,
|
|
|
|
KEY_READ,
|
2023-10-30 19:52:17 +08:00
|
|
|
&key);
|
2017-12-21 07:45:38 +08:00
|
|
|
if (ERROR_SUCCESS == success) {
|
|
|
|
DWORD regType = REG_NONE;
|
|
|
|
DWORD regSize = 0;
|
|
|
|
|
2023-10-30 19:52:17 +08:00
|
|
|
success = SysCalls::regQueryValueExA(key,
|
2021-10-05 19:21:46 +08:00
|
|
|
settingName,
|
|
|
|
NULL,
|
|
|
|
®Type,
|
|
|
|
NULL,
|
|
|
|
®Size);
|
2020-03-20 03:51:30 +08:00
|
|
|
if (ERROR_SUCCESS == success) {
|
2020-03-18 22:19:03 +08:00
|
|
|
if (regType == REG_SZ || regType == REG_MULTI_SZ) {
|
2025-03-11 20:16:50 +08:00
|
|
|
auto regData = std::make_unique_for_overwrite<char[]>(regSize);
|
2023-10-30 19:52:17 +08:00
|
|
|
success = SysCalls::regQueryValueExA(key,
|
2021-10-05 19:21:46 +08:00
|
|
|
settingName,
|
|
|
|
NULL,
|
|
|
|
®Type,
|
|
|
|
reinterpret_cast<LPBYTE>(regData.get()),
|
|
|
|
®Size);
|
2020-03-20 03:51:30 +08:00
|
|
|
if (success == ERROR_SUCCESS) {
|
|
|
|
keyValue.assign(regData.get());
|
2023-08-20 12:31:48 +08:00
|
|
|
retVal = true;
|
2020-03-20 03:51:30 +08:00
|
|
|
}
|
|
|
|
} else if (regType == REG_BINARY) {
|
2020-03-24 00:54:39 +08:00
|
|
|
size_t charCount = regSize / sizeof(wchar_t);
|
2025-03-11 20:16:50 +08:00
|
|
|
auto regData = std::make_unique_for_overwrite<wchar_t[]>(charCount);
|
2023-10-30 19:52:17 +08:00
|
|
|
success = SysCalls::regQueryValueExA(key,
|
2021-10-05 19:21:46 +08:00
|
|
|
settingName,
|
|
|
|
NULL,
|
|
|
|
®Type,
|
|
|
|
reinterpret_cast<LPBYTE>(regData.get()),
|
|
|
|
®Size);
|
2020-03-20 03:51:30 +08:00
|
|
|
|
|
|
|
if (ERROR_SUCCESS == success) {
|
|
|
|
|
2020-03-24 00:54:39 +08:00
|
|
|
std::unique_ptr<char[]> convertedData(new char[charCount + 1]);
|
|
|
|
std::wcstombs(convertedData.get(), regData.get(), charCount);
|
|
|
|
convertedData.get()[charCount] = 0;
|
2020-03-20 03:51:30 +08:00
|
|
|
|
|
|
|
keyValue.assign(convertedData.get());
|
2023-08-20 12:31:48 +08:00
|
|
|
retVal = true;
|
2020-03-20 03:51:30 +08:00
|
|
|
}
|
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
2023-10-30 19:52:17 +08:00
|
|
|
RegCloseKey(key);
|
2020-03-20 03:51:30 +08:00
|
|
|
}
|
2023-08-20 12:31:48 +08:00
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RegistryReader::getSetting(const char *settingName, const std::string &value, DebugVarPrefix &type) {
|
|
|
|
std::string keyValue = value;
|
|
|
|
|
|
|
|
if (!(getSettingStringCommon(settingName, keyValue))) {
|
|
|
|
|
2023-09-21 22:02:00 +08:00
|
|
|
auto prefixString = ApiSpecificConfig::getPrefixStrings();
|
|
|
|
auto prefixType = ApiSpecificConfig::getPrefixTypes();
|
2023-08-20 12:31:48 +08:00
|
|
|
|
|
|
|
uint32_t i = 0;
|
|
|
|
for (const auto &prefix : prefixString) {
|
2023-09-07 20:44:36 +08:00
|
|
|
std::string neoKey = prefix;
|
|
|
|
neoKey += settingName;
|
2025-02-07 21:56:19 +08:00
|
|
|
auto envValue = IoFunctions::getEnvironmentVariable(neoKey.c_str());
|
|
|
|
|
2023-08-20 12:31:48 +08:00
|
|
|
if (envValue) {
|
|
|
|
keyValue.assign(envValue);
|
|
|
|
type = prefixType[i];
|
|
|
|
return keyValue;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2023-12-05 20:06:54 +08:00
|
|
|
type = DebugVarPrefix::none;
|
2023-08-20 12:31:48 +08:00
|
|
|
return keyValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RegistryReader::getSetting(const char *settingName, const std::string &value) {
|
|
|
|
std::string keyValue = value;
|
2020-03-20 03:51:30 +08:00
|
|
|
|
2023-08-20 12:31:48 +08:00
|
|
|
if (!(getSettingStringCommon(settingName, keyValue))) {
|
2025-02-07 21:56:19 +08:00
|
|
|
const char *envValue = IoFunctions::getEnvironmentVariable(settingName);
|
|
|
|
|
2019-10-09 17:40:50 +08:00
|
|
|
if (envValue) {
|
|
|
|
keyValue.assign(envValue);
|
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
return keyValue;
|
|
|
|
}
|
2018-10-10 22:02:19 +08:00
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
}; // namespace NEO
|