2017-12-21 00:45:38 +01:00
|
|
|
/*
|
2021-05-16 20:51:16 +02:00
|
|
|
* Copyright (C) 2018-2021 Intel Corporation
|
2017-12-21 00:45:38 +01:00
|
|
|
*
|
2018-09-18 09:11:08 +02:00
|
|
|
* SPDX-License-Identifier: MIT
|
2017-12-21 00:45:38 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-02-23 22:44:01 +01:00
|
|
|
#include "shared/source/os_interface/windows/debug_registry_reader.h"
|
2019-08-29 12:15:33 +02:00
|
|
|
|
2020-02-23 22:44:01 +01:00
|
|
|
#include "shared/source/debug_settings/debug_settings_manager.h"
|
2020-08-24 01:55:47 +02:00
|
|
|
#include "shared/source/os_interface/windows/sys_calls.h"
|
2020-02-23 22:44:01 +01:00
|
|
|
#include "shared/source/os_interface/windows/windows_wrapper.h"
|
|
|
|
#include "shared/source/utilities/debug_settings_reader.h"
|
2019-02-27 11:39:32 +01:00
|
|
|
|
2017-12-21 00:45:38 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2019-03-26 11:59:46 +01:00
|
|
|
namespace NEO {
|
2018-07-01 18:44:29 +02:00
|
|
|
|
2019-09-02 12:04:22 +02:00
|
|
|
SettingsReader *SettingsReader::createOsReader(bool userScope, const std::string ®Key) {
|
|
|
|
return new RegistryReader(userScope, regKey);
|
2018-11-30 11:51:49 +01:00
|
|
|
}
|
|
|
|
|
2020-08-24 01:55:47 +02:00
|
|
|
char *SettingsReader::getenv(const char *settingName) {
|
|
|
|
return SysCalls::getenv(settingName);
|
|
|
|
}
|
|
|
|
|
2019-09-02 12:04:22 +02:00
|
|
|
RegistryReader::RegistryReader(bool userScope, const std::string ®Key) : registryReadRootKey(regKey) {
|
2020-03-19 14:42:49 +01:00
|
|
|
hkeyType = userScope ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE;
|
2018-11-30 11:51:49 +01:00
|
|
|
setUpProcessName();
|
|
|
|
}
|
2019-09-02 12:04:22 +02:00
|
|
|
|
2018-10-10 16:02:19 +02: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 00:45:38 +01:00
|
|
|
|
|
|
|
bool RegistryReader::getSetting(const char *settingName, bool defaultValue) {
|
2020-04-09 10:48:17 +02:00
|
|
|
return getSetting(settingName, static_cast<int32_t>(defaultValue)) ? true : false;
|
2017-12-21 00:45:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t RegistryReader::getSetting(const char *settingName, int32_t defaultValue) {
|
2020-03-27 07:47:16 +01:00
|
|
|
return static_cast<int32_t>(getSetting(settingName, static_cast<int64_t>(defaultValue)));
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t RegistryReader::getSetting(const char *settingName, int64_t defaultValue) {
|
2020-03-19 20:51:30 +01:00
|
|
|
HKEY Key{};
|
2020-03-27 07:47:16 +01:00
|
|
|
int64_t value = defaultValue;
|
2017-12-21 00:45:38 +01:00
|
|
|
DWORD success = ERROR_SUCCESS;
|
2020-03-19 20:51:30 +01:00
|
|
|
bool readSettingFromEnv = true;
|
2017-12-21 00:45:38 +01:00
|
|
|
|
2021-10-05 11:21:46 +00:00
|
|
|
success = SysCalls::regOpenKeyExA(hkeyType,
|
|
|
|
registryReadRootKey.c_str(),
|
|
|
|
0,
|
|
|
|
KEY_READ,
|
|
|
|
&Key);
|
2017-12-21 00:45:38 +01:00
|
|
|
|
|
|
|
if (ERROR_SUCCESS == success) {
|
2020-03-27 07:47:16 +01:00
|
|
|
DWORD size = sizeof(int64_t);
|
|
|
|
int64_t regData;
|
2017-12-21 00:45:38 +01:00
|
|
|
|
2021-10-05 11:21:46 +00:00
|
|
|
success = SysCalls::regQueryValueExA(Key,
|
|
|
|
settingName,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
reinterpret_cast<LPBYTE>(®Data),
|
|
|
|
&size);
|
2020-03-19 20:51:30 +01:00
|
|
|
if (ERROR_SUCCESS == success) {
|
|
|
|
value = regData;
|
|
|
|
readSettingFromEnv = false;
|
|
|
|
}
|
2017-12-21 00:45:38 +01:00
|
|
|
RegCloseKey(Key);
|
2020-03-19 20:51:30 +01:00
|
|
|
}
|
|
|
|
if (readSettingFromEnv) {
|
2019-10-10 11:14:58 +02:00
|
|
|
const char *envValue = getenv(settingName);
|
2019-10-09 11:40:50 +02:00
|
|
|
if (envValue) {
|
|
|
|
value = atoi(envValue);
|
|
|
|
}
|
2017-12-21 00:45:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RegistryReader::getSetting(const char *settingName, const std::string &value) {
|
2020-03-19 20:51:30 +01:00
|
|
|
HKEY Key{};
|
2017-12-21 00:45:38 +01:00
|
|
|
DWORD success = ERROR_SUCCESS;
|
|
|
|
std::string keyValue = value;
|
2020-03-19 20:51:30 +01:00
|
|
|
bool readSettingFromEnv = true;
|
2017-12-21 00:45:38 +01:00
|
|
|
|
2021-10-05 11:21:46 +00:00
|
|
|
success = SysCalls::regOpenKeyExA(hkeyType,
|
|
|
|
registryReadRootKey.c_str(),
|
|
|
|
0,
|
|
|
|
KEY_READ,
|
|
|
|
&Key);
|
2017-12-21 00:45:38 +01:00
|
|
|
if (ERROR_SUCCESS == success) {
|
|
|
|
DWORD regType = REG_NONE;
|
|
|
|
DWORD regSize = 0;
|
|
|
|
|
2021-10-05 11:21:46 +00:00
|
|
|
success = SysCalls::regQueryValueExA(Key,
|
|
|
|
settingName,
|
|
|
|
NULL,
|
|
|
|
®Type,
|
|
|
|
NULL,
|
|
|
|
®Size);
|
2020-03-19 20:51:30 +01:00
|
|
|
if (ERROR_SUCCESS == success) {
|
2020-03-18 15:19:03 +01:00
|
|
|
if (regType == REG_SZ || regType == REG_MULTI_SZ) {
|
2020-03-19 20:51:30 +01:00
|
|
|
auto regData = std::make_unique<char[]>(regSize);
|
2021-10-05 11:21:46 +00:00
|
|
|
success = SysCalls::regQueryValueExA(Key,
|
|
|
|
settingName,
|
|
|
|
NULL,
|
|
|
|
®Type,
|
|
|
|
reinterpret_cast<LPBYTE>(regData.get()),
|
|
|
|
®Size);
|
2020-03-19 20:51:30 +01:00
|
|
|
if (success == ERROR_SUCCESS) {
|
|
|
|
keyValue.assign(regData.get());
|
|
|
|
readSettingFromEnv = false;
|
|
|
|
}
|
|
|
|
} else if (regType == REG_BINARY) {
|
2020-03-23 17:54:39 +01:00
|
|
|
size_t charCount = regSize / sizeof(wchar_t);
|
|
|
|
auto regData = std::make_unique<wchar_t[]>(charCount);
|
2021-10-05 11:21:46 +00:00
|
|
|
success = SysCalls::regQueryValueExA(Key,
|
|
|
|
settingName,
|
|
|
|
NULL,
|
|
|
|
®Type,
|
|
|
|
reinterpret_cast<LPBYTE>(regData.get()),
|
|
|
|
®Size);
|
2020-03-19 20:51:30 +01:00
|
|
|
|
|
|
|
if (ERROR_SUCCESS == success) {
|
|
|
|
|
2020-03-23 17:54:39 +01:00
|
|
|
std::unique_ptr<char[]> convertedData(new char[charCount + 1]);
|
|
|
|
std::wcstombs(convertedData.get(), regData.get(), charCount);
|
|
|
|
convertedData.get()[charCount] = 0;
|
2020-03-19 20:51:30 +01:00
|
|
|
|
|
|
|
keyValue.assign(convertedData.get());
|
|
|
|
readSettingFromEnv = false;
|
|
|
|
}
|
|
|
|
}
|
2017-12-21 00:45:38 +01:00
|
|
|
}
|
|
|
|
RegCloseKey(Key);
|
2020-03-19 20:51:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (readSettingFromEnv) {
|
2020-07-30 15:27:03 +02:00
|
|
|
const char *envValue = strcmp(processName.c_str(), settingName) ? getenv(settingName) : getenv("cl_cache_dir");
|
2019-10-09 11:40:50 +02:00
|
|
|
if (envValue) {
|
|
|
|
keyValue.assign(envValue);
|
|
|
|
}
|
2017-12-21 00:45:38 +01:00
|
|
|
}
|
|
|
|
return keyValue;
|
|
|
|
}
|
2018-10-10 16:02:19 +02:00
|
|
|
|
2019-03-26 11:59:46 +01:00
|
|
|
}; // namespace NEO
|