Log all COR_, CORECLR_, DOTNET_, and OTEL_environmental variables in native code (#1378)

This commit is contained in:
Piotr Kiełkowicz 2022-10-10 09:31:48 +02:00 committed by GitHub
parent 78e649bf92
commit 4d0d2b0ac2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 29 deletions

View File

@ -123,13 +123,14 @@ HRESULT STDMETHODCALLTYPE CorProfiler::Initialize(IUnknown* cor_profiler_info_un
} }
info10 = nullptr; info10 = nullptr;
Logger::Info("Environment variables:"); if (IsDebugEnabled())
for (auto&& env_var : env_vars_to_display)
{ {
WSTRING env_var_value = GetEnvironmentValue(env_var); const auto env_variables = GetEnvironmentVariables(env_vars_prefixes_to_display);
if (IsDebugEnabled() || !env_var_value.empty()) Logger::Info("Environment variables:");
for (const auto& env_variable : env_variables)
{ {
Logger::Info(" ", env_var, "=", env_var_value); Logger::Info(" ", env_variable);
} }
} }

View File

@ -94,6 +94,11 @@ const WSTRING dotnet_shared_store = WStr("DOTNET_SHARED_STORE");
// for more information about this environment variable. // for more information about this environment variable.
const WSTRING dotnet_startup_hooks = WStr("DOTNET_STARTUP_HOOKS"); const WSTRING dotnet_startup_hooks = WStr("DOTNET_STARTUP_HOOKS");
const WSTRING prefix_cor = WStr("COR_");
const WSTRING prefix_coreclr = WStr("CORECLR_");
const WSTRING prefix_dotnet = WStr("DOTNET_");
const WSTRING prefix_otel = WStr("OTEL_");
} // namespace environment } // namespace environment
} // namespace trace } // namespace trace

View File

@ -4,30 +4,14 @@
#include <string> #include <string>
#include "environment_variables.h" #include "environment_variables.h"
#include "logger.h"
namespace trace namespace trace
{ {
const std::vector env_vars_prefixes_to_display{environment::prefix_cor,
const WSTRING env_vars_to_display[]{environment::clr_profiler_enabled, environment::prefix_coreclr,
environment::debug_enabled, environment::prefix_dotnet,
environment::profiler_home_path, environment::prefix_otel,
environment::integrations_path, environment::azure_app_services_app_pool_id};
environment::include_process_names,
environment::exclude_process_names,
environment::enabled_integrations,
environment::disabled_integrations,
environment::log_directory,
environment::clr_disable_optimizations,
environment::clr_enable_inlining,
environment::clr_enable_ngen,
environment::dump_il_rewrite_enabled,
environment::azure_app_services,
environment::azure_app_services_app_pool_id,
environment::azure_app_services_cli_telemetry_profile_value,
environment::dotnet_additional_deps,
environment::dotnet_shared_store,
environment::dotnet_startup_hooks};
const WSTRING skip_assembly_prefixes[]{ const WSTRING skip_assembly_prefixes[]{
WStr("Microsoft.AI"), WStr("Microsoft.AI"),

View File

@ -1,13 +1,15 @@
#include "util.h" #include "util.h"
#include "miniutf.hpp"
#include "pal.h" #include "pal.h"
#include <cwctype> #include <cwctype>
#include <iterator> #include <iterator>
#include <sstream> #include <string>
#include <string> //NOLINT
#include <vector> #include <vector>
#ifdef MACOS
extern char** environ;
#endif
namespace trace namespace trace
{ {
@ -97,6 +99,54 @@ std::vector<WSTRING> GetEnvironmentValues(const WSTRING& name)
return GetEnvironmentValues(name, L','); return GetEnvironmentValues(name, L',');
} }
std::vector<WSTRING> GetEnvironmentVariables(const std::vector<WSTRING> &prefixes)
{
std::vector<WSTRING> env_strings;
#ifdef _WIN32
// Documentation for GetEnvironmentStrings: https://learn.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-getenvironmentstrings#remarks
const auto env_variables = GetEnvironmentStrings();
int prev = 0;
for (int i = 0;; i++) {
if (env_variables[i] != '\0') {
continue;
}
auto env_variable = WSTRING(env_variables + prev, env_variables + i);
for (const auto& prefix : prefixes)
{
if (env_variable.find(prefix) == 0)
{
env_strings.push_back(env_variable);
break;
}
}
prev = i + 1;
if (env_variables[i + 1] == '\0')
{
break;
}
}
FreeEnvironmentStrings(env_variables);
#else
for (char** current = environ; *current; current++)
{
auto env_variable = ToWSTRING(ToString(*current));
for (const auto& prefix : prefixes)
{
if (env_variable.find(prefix) == 0)
{
env_strings.push_back(env_variable);
break;
}
}
}
#endif
return env_strings;
}
constexpr char HexMap[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; constexpr char HexMap[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
WSTRING HexStr(const void* dataPtr, int len) WSTRING HexStr(const void* dataPtr, int len)

View File

@ -32,6 +32,9 @@ std::vector<WSTRING> GetEnvironmentValues(const WSTRING& name, const wchar_t del
// GetEnvironmentValues calls GetEnvironmentValues with a semicolon delimiter. // GetEnvironmentValues calls GetEnvironmentValues with a semicolon delimiter.
std::vector<WSTRING> GetEnvironmentValues(const WSTRING& name); std::vector<WSTRING> GetEnvironmentValues(const WSTRING& name);
// GetEnvironmentVariables returns list of all environment variable
std::vector<WSTRING> GetEnvironmentVariables(const std::vector<WSTRING> &prefixes);
// Convert Hex to string // Convert Hex to string
WSTRING HexStr(const void* data, int len); WSTRING HexStr(const void* data, int len);