/* * Copyright (C) 2018-2023 Intel Corporation * * SPDX-License-Identifier: MIT * */ #include "shared/source/os_interface/windows/debug_registry_reader.h" #include "shared/source/debug_settings/debug_settings_manager.h" #include "shared/source/helpers/api_specific_config.h" #include "shared/source/os_interface/windows/sys_calls.h" #include "shared/source/os_interface/windows/windows_wrapper.h" #include "shared/source/utilities/debug_settings_reader.h" #include namespace NEO { SettingsReader *SettingsReader::createOsReader(bool userScope, const std::string ®Key) { return new RegistryReader(userScope, regKey); } RegistryReader::RegistryReader(bool userScope, const std::string ®Key) : registryReadRootKey(regKey) { hkeyType = userScope ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE; setUpProcessName(); } 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(); } bool RegistryReader::getSetting(const char *settingName, bool defaultValue, DebugVarPrefix &type) { return getSetting(settingName, static_cast(defaultValue), type) ? true : false; } bool RegistryReader::getSetting(const char *settingName, bool defaultValue) { return getSetting(settingName, static_cast(defaultValue)) ? true : false; } int32_t RegistryReader::getSetting(const char *settingName, int32_t defaultValue, DebugVarPrefix &type) { return static_cast(getSetting(settingName, static_cast(defaultValue), type)); } int32_t RegistryReader::getSetting(const char *settingName, int32_t defaultValue) { return static_cast(getSetting(settingName, static_cast(defaultValue))); } bool RegistryReader::getSettingIntCommon(const char *settingName, int64_t &value) { HKEY Key{}; DWORD success = ERROR_SUCCESS; bool retVal = false; success = SysCalls::regOpenKeyExA(hkeyType, registryReadRootKey.c_str(), 0, KEY_READ, &Key); if (ERROR_SUCCESS == success) { DWORD size = sizeof(int64_t); int64_t regData; success = SysCalls::regQueryValueExA(Key, settingName, NULL, NULL, reinterpret_cast(®Data), &size); if (ERROR_SUCCESS == success) { value = regData; retVal = true; } RegCloseKey(Key); } return retVal; } int64_t RegistryReader::getSetting(const char *settingName, int64_t defaultValue, DebugVarPrefix &type) { int64_t value = defaultValue; if (!(getSettingIntCommon(settingName, value))) { char *envValue; auto prefixString = ApiSpecificConfig::getPrefixStrings(); auto prefixType = ApiSpecificConfig::getPrefixTypes(); uint32_t i = 0; for (const auto &prefix : prefixString) { std::string neoKey = prefix; neoKey += settingName; envValue = IoFunctions::getenvPtr(neoKey.c_str()); if (envValue) { value = atoll(envValue); type = prefixType[i]; return value; } i++; } } type = DebugVarPrefix::None; return value; } int64_t RegistryReader::getSetting(const char *settingName, int64_t defaultValue) { int64_t value = defaultValue; if (!(getSettingIntCommon(settingName, value))) { const char *envValue = IoFunctions::getenvPtr(settingName); if (envValue) { value = atoll(envValue); } } return value; } bool RegistryReader::getSettingStringCommon(const char *settingName, std::string &keyValue) { HKEY Key{}; DWORD success = ERROR_SUCCESS; bool retVal = false; success = SysCalls::regOpenKeyExA(hkeyType, registryReadRootKey.c_str(), 0, KEY_READ, &Key); if (ERROR_SUCCESS == success) { DWORD regType = REG_NONE; DWORD regSize = 0; success = SysCalls::regQueryValueExA(Key, settingName, NULL, ®Type, NULL, ®Size); if (ERROR_SUCCESS == success) { if (regType == REG_SZ || regType == REG_MULTI_SZ) { auto regData = std::make_unique(regSize); success = SysCalls::regQueryValueExA(Key, settingName, NULL, ®Type, reinterpret_cast(regData.get()), ®Size); if (success == ERROR_SUCCESS) { keyValue.assign(regData.get()); retVal = true; } } else if (regType == REG_BINARY) { size_t charCount = regSize / sizeof(wchar_t); auto regData = std::make_unique(charCount); success = SysCalls::regQueryValueExA(Key, settingName, NULL, ®Type, reinterpret_cast(regData.get()), ®Size); if (ERROR_SUCCESS == success) { std::unique_ptr convertedData(new char[charCount + 1]); std::wcstombs(convertedData.get(), regData.get(), charCount); convertedData.get()[charCount] = 0; keyValue.assign(convertedData.get()); retVal = true; } } } RegCloseKey(Key); } return retVal; } std::string RegistryReader::getSetting(const char *settingName, const std::string &value, DebugVarPrefix &type) { std::string keyValue = value; if (!(getSettingStringCommon(settingName, keyValue))) { char *envValue; auto prefixString = ApiSpecificConfig::getPrefixStrings(); auto prefixType = ApiSpecificConfig::getPrefixTypes(); uint32_t i = 0; for (const auto &prefix : prefixString) { std::string neoKey = prefix; neoKey += settingName; envValue = strcmp(processName.c_str(), neoKey.c_str()) ? IoFunctions::getenvPtr(neoKey.c_str()) : IoFunctions::getenvPtr("cl_cache_dir"); if (envValue) { keyValue.assign(envValue); type = prefixType[i]; return keyValue; } i++; } } type = DebugVarPrefix::None; return keyValue; } std::string RegistryReader::getSetting(const char *settingName, const std::string &value) { std::string keyValue = value; if (!(getSettingStringCommon(settingName, keyValue))) { const char *envValue = strcmp(processName.c_str(), settingName) ? IoFunctions::getenvPtr(settingName) : IoFunctions::getenvPtr("cl_cache_dir"); if (envValue) { keyValue.assign(envValue); } } return keyValue; } }; // namespace NEO