1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
/*
* Copyright (C) 2018-2022 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/os_interface/windows/sys_calls.h"
#include "shared/source/os_interface/windows/windows_wrapper.h"
#include "shared/source/utilities/debug_settings_reader.h"
#include <stdint.h>
namespace NEO {
SettingsReader *SettingsReader::createOsReader(bool userScope, const std::string ®Key) {
return new RegistryReader(userScope, regKey);
}
char *SettingsReader::getenv(const char *settingName) {
return SysCalls::getenv(settingName);
}
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) {
return getSetting(settingName, static_cast<int32_t>(defaultValue)) ? true : false;
}
int32_t RegistryReader::getSetting(const char *settingName, int32_t defaultValue) {
return static_cast<int32_t>(getSetting(settingName, static_cast<int64_t>(defaultValue)));
}
int64_t RegistryReader::getSetting(const char *settingName, int64_t defaultValue) {
HKEY Key{};
int64_t value = defaultValue;
DWORD success = ERROR_SUCCESS;
bool readSettingFromEnv = true;
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<LPBYTE>(®Data),
&size);
if (ERROR_SUCCESS == success) {
value = regData;
readSettingFromEnv = false;
}
RegCloseKey(Key);
}
if (readSettingFromEnv) {
const char *envValue = getenv(settingName);
if (envValue) {
value = atoll(envValue);
}
}
return value;
}
std::string RegistryReader::getSetting(const char *settingName, const std::string &value) {
HKEY Key{};
DWORD success = ERROR_SUCCESS;
std::string keyValue = value;
bool readSettingFromEnv = true;
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<char[]>(regSize);
success = SysCalls::regQueryValueExA(Key,
settingName,
NULL,
®Type,
reinterpret_cast<LPBYTE>(regData.get()),
®Size);
if (success == ERROR_SUCCESS) {
keyValue.assign(regData.get());
readSettingFromEnv = false;
}
} else if (regType == REG_BINARY) {
size_t charCount = regSize / sizeof(wchar_t);
auto regData = std::make_unique<wchar_t[]>(charCount);
success = SysCalls::regQueryValueExA(Key,
settingName,
NULL,
®Type,
reinterpret_cast<LPBYTE>(regData.get()),
®Size);
if (ERROR_SUCCESS == success) {
std::unique_ptr<char[]> convertedData(new char[charCount + 1]);
std::wcstombs(convertedData.get(), regData.get(), charCount);
convertedData.get()[charCount] = 0;
keyValue.assign(convertedData.get());
readSettingFromEnv = false;
}
}
}
RegCloseKey(Key);
}
if (readSettingFromEnv) {
const char *envValue = strcmp(processName.c_str(), settingName) ? getenv(settingName) : getenv("cl_cache_dir");
if (envValue) {
keyValue.assign(envValue);
}
}
return keyValue;
}
}; // namespace NEO
|