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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
//===--- EnvironmentVariables.h - Debug variables. --------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Debug behavior conditionally enabled using environment variables.
//
//===----------------------------------------------------------------------===//
#include "swift/Runtime/Debug.h"
#include "swift/Runtime/Paths.h"
#include "swift/Runtime/EnvironmentVariables.h"
#include <string.h>
#include <inttypes.h>
using namespace swift;
namespace {
// This is required to make the macro machinery work correctly; we can't
// declare a VARIABLE(..., const char *, ...) because then the token-pasted
// names won't work properly. It *does* mean that if you want to use std::string
// somewhere in this file, you'll have to fully qualify the name.
typedef const char *string;
// Require all environment variable names to start with SWIFT_
static constexpr bool hasSwiftPrefix(const char *str) {
const char prefix[] = "SWIFT_";
for (unsigned i = 0; i < sizeof(prefix) - 1; i++)
if (str[i] != prefix[i])
return false;
return true;
}
#define VARIABLE(name, type, defaultValue, help) \
static_assert(hasSwiftPrefix(#name), "Names must start with SWIFT");
#include "EnvironmentVariables.def"
#if SWIFT_STDLIB_HAS_ENVIRON
// Value parsers. Add new functions named parse_<type> to accommodate more
// debug variable types.
static bool parse_bool(const char *name, const char *value, bool defaultValue) {
if (!value)
return defaultValue;
switch (value[0]) {
case 'Y':
case 'y':
case 'T':
case 't':
case '1':
return true;
case 'N':
case 'n':
case 'F':
case 'f':
case '0':
return false;
default:
swift::warning(RuntimeErrorFlagNone,
"Warning: cannot parse value %s=%s, defaulting to %s.\n",
name, value, defaultValue ? "true" : "false");
return defaultValue;
}
}
static uint8_t parse_uint8_t(const char *name,
const char *value,
uint8_t defaultValue) {
if (!value)
return defaultValue;
char *end;
long n = strtol(value, &end, 0);
if (*end != '\0') {
swift::warning(RuntimeErrorFlagNone,
"Warning: cannot parse value %s=%s, defaulting to %u.\n",
name, value, defaultValue);
return defaultValue;
}
if (n < 0) {
swift::warning(RuntimeErrorFlagNone,
"Warning: %s=%s out of bounds, clamping to 0.\n",
name, value);
return 0;
}
if (n > UINT8_MAX) {
swift::warning(RuntimeErrorFlagNone,
"Warning: %s=%s out of bounds, clamping to %d.\n",
name, value, UINT8_MAX);
return UINT8_MAX;
}
return n;
}
static uint32_t parse_uint32_t(const char *name,
const char *value,
uint32_t defaultValue) {
if (!value)
return defaultValue;
char *end;
long long n = strtoll(value, &end, 0);
if (*end != '\0') {
swift::warning(RuntimeErrorFlagNone,
"Warning: cannot parse value %s=%s, defaulting to %u.\n",
name, value, defaultValue);
return defaultValue;
}
if (n < 0) {
swift::warning(RuntimeErrorFlagNone,
"Warning: %s=%s out of bounds, clamping to 0.\n",
name, value);
return 0;
}
if (n > UINT32_MAX) {
swift::warning(RuntimeErrorFlagNone,
"Warning: %s=%s out of bounds, clamping to %" PRIu32 ".\n",
name, value, UINT32_MAX);
return UINT32_MAX;
}
return n;
}
static string parse_string(const char *name,
const char *value,
string defaultValue) {
if (!value || value[0] == 0)
return strdup(defaultValue);
return strdup(value);
}
// Print a list of all the environment variables. Lazy initialization makes
// this a bit odd, but the use of these variables in the metadata system means
// it's almost certain to run early.
//
// The "extra" parameter is printed after the header and before the list of
// variables.
void printHelp(const char *extra) {
swift::warning(RuntimeErrorFlagNone, "Swift runtime debugging:\n");
if (extra)
swift::warning(RuntimeErrorFlagNone, "%s\n", extra);
#define VARIABLE(name, type, defaultValue, help) \
swift::warning(RuntimeErrorFlagNone, "%7s %s [default: %s] - %s\n", \
#type, #name, #defaultValue, help);
#include "EnvironmentVariables.def"
swift::warning(RuntimeErrorFlagNone, "SWIFT_DEBUG_HELP=YES - Print this help.");
}
#endif // SWIFT_STDLIB_HAS_ENVIRON
} // end anonymous namespace
// Define backing variables.
#define VARIABLE(name, type, defaultValue, help) \
type swift::runtime::environment::name##_variable = defaultValue; \
bool swift::runtime::environment::name##_isSet_variable = false;
#include "EnvironmentVariables.def"
// Initialization code.
swift::once_t swift::runtime::environment::initializeToken;
#if SWIFT_STDLIB_HAS_ENVIRON && (defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__linux__))
extern "C" char **environ;
#define ENVIRON environ
#elif defined(_WIN32)
// `_environ` is DLL-imported unless we are linking against the static C runtime
// (via `/MT` or `/MTd`).
#if defined(_DLL)
extern "C" __declspec(dllimport) char **_environ;
#else
extern "C" char **_environ;
#endif
// `_environ` is unavailable in the Windows Runtime environment.
// https://docs.microsoft.com/en-us/cpp/c-runtime-library/environ-wenviron?view=msvc-160
#if !defined(_WINRT_DLL)
#define ENVIRON _environ
#endif
#endif
#ifdef ENVIRON
void swift::runtime::environment::initialize(void *context) {
// On platforms where we have an environment variable array available, scan it
// directly. This optimizes for the common case where no variables are set,
// since we only need to perform one scan to set all variables. It also allows
// us to detect some spelling mistakes by warning on unknown SWIFT_ variables.
bool SWIFT_DEBUG_HELP_variable = false;
bool SWIFT_DEBUG_HELP_isSet_variable = false;
for (char **var = ENVIRON; *var; var++) {
// Immediately skip anything without a SWIFT_ prefix.
if (strncmp(*var, "SWIFT_", 6) != 0)
continue;
bool foundVariable = false;
// Check each defined variable in turn, plus SWIFT_DEBUG_HELP. Variables are
// parsed by functions named parse_<type> above. An unknown type will
// produce an error that parse_<unknown-type> doesn't exist. Add new parsers
// above.
#define VARIABLE(name, type, defaultValue, help) \
if (strncmp(*var, #name "=", strlen(#name "=")) == 0) { \
name##_variable = \
parse_##type(#name, *var + strlen(#name "="), defaultValue); \
name##_isSet_variable = true; \
foundVariable = true; \
}
// SWIFT_DEBUG_HELP is not in the variables list. Parse it like the other
// variables.
VARIABLE(SWIFT_DEBUG_HELP, bool, false, )
#include "EnvironmentVariables.def"
// Flag unknown SWIFT_DEBUG_ variables to catch misspellings. We don't flag
// all unknown SWIFT_ variables, because there are a bunch of other SWIFT_
// variables used for other purposes, such as SWIFT_SOURCE_ROOT and
// SWIFT_INSTALL_DIR, and we don't want to warn for all of those.
const char *swiftDebugPrefix = "SWIFT_DEBUG_";
if (!foundVariable &&
strncmp(*var, swiftDebugPrefix, strlen(swiftDebugPrefix)) == 0) {
const char *equals = strchr(*var, '=');
if (!equals)
equals = *var + strlen(*var);
swift::warning(RuntimeErrorFlagNone,
"Warning: unknown environment variable %.*s\n",
(int)(equals - *var), *var);
}
}
if (SWIFT_DEBUG_HELP_variable)
printHelp(nullptr);
}
#elif SWIFT_STDLIB_HAS_ENVIRON
void swift::runtime::environment::initialize(void *context) {
// Emit a getenv call for each variable. This is less efficient but works
// everywhere.
#define VARIABLE(name, type, defaultValue, help) \
do { \
const char name##_string = getenv(#name); \
if (name##_string) \
name##_isSet_variable = true; \
name##_variable = parse_##type(#name, name##_string, defaultValue); \
} while (0)
#include "EnvironmentVariables.def"
// Print help if requested.
if (parse_bool("SWIFT_DEBUG_HELP", getenv("SWIFT_DEBUG_HELP"), false))
printHelp("Using getenv to read variables. Unknown SWIFT_DEBUG_ variables "
"will not be flagged.");
}
#else
void swift::runtime::environment::initialize(void *context) {
(void)context;
}
#endif
SWIFT_RUNTIME_EXPORT
bool swift_COWChecksEnabled() {
return runtime::environment::SWIFT_DEBUG_ENABLE_COW_CHECKS();
}
SWIFT_RUNTIME_STDLIB_SPI bool concurrencyEnableCooperativeQueues() {
return runtime::environment::
SWIFT_DEBUG_CONCURRENCY_ENABLE_COOPERATIVE_QUEUES();
}
SWIFT_RUNTIME_STDLIB_SPI bool concurrencyEnableJobDispatchIntegration() {
return runtime::environment::
SWIFT_ENABLE_ASYNC_JOB_DISPATCH_INTEGRATION();
}
SWIFT_RUNTIME_STDLIB_SPI bool concurrencyValidateUncheckedContinuations() {
return runtime::environment::SWIFT_DEBUG_VALIDATE_UNCHECKED_CONTINUATIONS();
}
SWIFT_RUNTIME_STDLIB_SPI const char *concurrencyIsCurrentExecutorLegacyModeOverride() {
return runtime::environment::SWIFT_IS_CURRENT_EXECUTOR_LEGACY_MODE_OVERRIDE();
}
|