File: debug_registry_reader.cpp

package info (click to toggle)
intel-compute-runtime 25.44.36015.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 79,632 kB
  • sloc: cpp: 931,547; lisp: 2,074; sh: 719; makefile: 162; python: 21
file content (227 lines) | stat: -rw-r--r-- 8,026 bytes parent folder | download
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 * Copyright (C) 2018-2025 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 <stdint.h>

namespace NEO {

SettingsReader *SettingsReader::createOsReader(bool userScope, const std::string &regKey) {
    return new RegistryReader(userScope, regKey);
}

RegistryReader::RegistryReader(bool userScope, const std::string &regKey) : 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<int32_t>(defaultValue), type) ? true : false;
}

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, DebugVarPrefix &type) {
    return static_cast<int32_t>(getSetting(settingName, static_cast<int64_t>(defaultValue), type));
}

int32_t RegistryReader::getSetting(const char *settingName, int32_t defaultValue) {
    return static_cast<int32_t>(getSetting(settingName, static_cast<int64_t>(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<LPBYTE>(&regData),
                                             &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,
                                             &regType,
                                             NULL,
                                             &regSize);
        if (ERROR_SUCCESS == success) {
            if (regType == REG_SZ || regType == REG_MULTI_SZ) {
                auto regData = std::make_unique_for_overwrite<char[]>(regSize);
                success = SysCalls::regQueryValueExA(key,
                                                     settingName,
                                                     NULL,
                                                     &regType,
                                                     reinterpret_cast<LPBYTE>(regData.get()),
                                                     &regSize);
                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_for_overwrite<wchar_t[]>(charCount);
                success = SysCalls::regQueryValueExA(key,
                                                     settingName,
                                                     NULL,
                                                     &regType,
                                                     reinterpret_cast<LPBYTE>(regData.get()),
                                                     &regSize);

                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());
                    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))) {

        auto prefixString = ApiSpecificConfig::getPrefixStrings();
        auto prefixType = ApiSpecificConfig::getPrefixTypes();

        uint32_t i = 0;
        for (const auto &prefix : prefixString) {
            std::string neoKey = prefix;
            neoKey += settingName;
            auto envValue = IoFunctions::getEnvironmentVariable(neoKey.c_str());

            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 = IoFunctions::getEnvironmentVariable(settingName);

        if (envValue) {
            keyValue.assign(envValue);
        }
    }
    return keyValue;
}

}; // namespace NEO