|
| 1 | +//===--- EnvironmentVariables.h - Debug variables. --------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// Debug behavior conditionally enabled using environment variables. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +#include "swift/Runtime/Debug.h" |
| 18 | +#include "swift/Runtime/EnvironmentVariables.h" |
| 19 | + |
| 20 | +#include <string.h> |
| 21 | + |
| 22 | +using namespace swift; |
| 23 | + |
| 24 | +namespace { |
| 25 | + |
| 26 | +// Require all environment variable names to start with SWIFT_ |
| 27 | +static constexpr bool hasSwiftPrefix(const char *str) { |
| 28 | + const char prefix[] = "SWIFT_"; |
| 29 | + for (unsigned i = 0; i < sizeof(prefix) - 1; i++) |
| 30 | + if (str[i] != prefix[i]) |
| 31 | + return false; |
| 32 | + return true; |
| 33 | +} |
| 34 | +#define VARIABLE(name, type, defaultValue, help) \ |
| 35 | + static_assert(hasSwiftPrefix(#name), "Names must start with SWIFT"); |
| 36 | +#include "EnvironmentVariables.def" |
| 37 | + |
| 38 | +// Value parsers. Add new functions named parse_<type> to accommodate more |
| 39 | +// debug variable types. |
| 40 | +static bool parse_bool(const char *name, const char *value, bool defaultValue) { |
| 41 | + if (!value) |
| 42 | + return defaultValue; |
| 43 | + switch (value[0]) { |
| 44 | + case 'Y': |
| 45 | + case 'y': |
| 46 | + case 'T': |
| 47 | + case 't': |
| 48 | + case '1': |
| 49 | + return true; |
| 50 | + case 'N': |
| 51 | + case 'n': |
| 52 | + case 'F': |
| 53 | + case 'f': |
| 54 | + case '0': |
| 55 | + return false; |
| 56 | + default: |
| 57 | + swift::warning(RuntimeErrorFlagNone, |
| 58 | + "Warning: cannot parse value %s=%s, defaulting to %s.\n", |
| 59 | + name, value, defaultValue ? "true" : "false"); |
| 60 | + return defaultValue; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +static uint8_t parse_uint8_t(const char *name, |
| 65 | + const char *value, |
| 66 | + uint8_t defaultValue) { |
| 67 | + if (!value) |
| 68 | + return defaultValue; |
| 69 | + char *end; |
| 70 | + long n = strtol(value, &end, 0); |
| 71 | + if (*end != '\0') { |
| 72 | + swift::warning(RuntimeErrorFlagNone, |
| 73 | + "Warning: cannot parse value %s=%s, defaulting to %u.\n", |
| 74 | + name, value, defaultValue); |
| 75 | + return defaultValue; |
| 76 | + } |
| 77 | + |
| 78 | + if (n < 0) { |
| 79 | + swift::warning(RuntimeErrorFlagNone, |
| 80 | + "Warning: %s=%s out of bounds, clamping to 0.\n", |
| 81 | + name, value); |
| 82 | + return 0; |
| 83 | + } |
| 84 | + if (n > UINT8_MAX) { |
| 85 | + swift::warning(RuntimeErrorFlagNone, |
| 86 | + "Warning: %s=%s out of bounds, clamping to %d.\n", |
| 87 | + name, value, UINT8_MAX); |
| 88 | + return UINT8_MAX; |
| 89 | + } |
| 90 | + |
| 91 | + return n; |
| 92 | +} |
| 93 | + |
| 94 | +// Print a list of all the environment variables. Lazy initialization makes |
| 95 | +// this a bit odd, but the use of these variables in the metadata system means |
| 96 | +// it's almost certain to run early. |
| 97 | +// |
| 98 | +// The "extra" parameter is printed after the header and before the list of |
| 99 | +// variables. |
| 100 | +void printHelp(const char *extra) { |
| 101 | + swift::warning(RuntimeErrorFlagNone, "Swift runtime debugging:\n"); |
| 102 | + if (extra) |
| 103 | + swift::warning(RuntimeErrorFlagNone, "%s\n", extra); |
| 104 | +#define VARIABLE(name, type, defaultValue, help) \ |
| 105 | + swift::warning(RuntimeErrorFlagNone, "%7s %s [default: %s] - %s\n", \ |
| 106 | + #type, #name, #defaultValue, help); |
| 107 | +#include "EnvironmentVariables.def" |
| 108 | + swift::warning(RuntimeErrorFlagNone, "SWIFT_DEBUG_HELP=YES - Print this help."); |
| 109 | +} |
| 110 | + |
| 111 | +} // end anonymous namespace |
| 112 | + |
| 113 | +// Define backing variables. |
| 114 | +#define VARIABLE(name, type, defaultValue, help) \ |
| 115 | + type swift::runtime::environment::name ## _variable = defaultValue; |
| 116 | +#include "EnvironmentVariables.def" |
| 117 | + |
| 118 | +// Initialization code. |
| 119 | +OnceToken_t swift::runtime::environment::initializeToken; |
| 120 | + |
| 121 | +#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__linux__) |
| 122 | +extern "C" char **environ; |
| 123 | +#define ENVIRON environ |
| 124 | +#elif defined(_WIN32) |
| 125 | +extern "C" char **_environ; |
| 126 | +#define ENVIRON _environ |
| 127 | +#endif |
| 128 | + |
| 129 | +#ifdef ENVIRON |
| 130 | +void swift::runtime::environment::initialize(void *context) { |
| 131 | + // On platforms where we have an environment variable array available, scan it |
| 132 | + // directly. This optimizes for the common case where no variables are set, |
| 133 | + // since we only need to perform one scan to set all variables. It also allows |
| 134 | + // us to detect some spelling mistakes by warning on unknown SWIFT_ variables. |
| 135 | + |
| 136 | + bool SWIFT_DEBUG_HELP_variable = false; |
| 137 | + for (char **var = ENVIRON; *var; var++) { |
| 138 | + // Immediately skip anything without a SWIFT_ prefix. |
| 139 | + if (strncmp(*var, "SWIFT_", 6) != 0) |
| 140 | + continue; |
| 141 | + |
| 142 | + bool foundVariable = false; |
| 143 | + // Check each defined variable in turn, plus SWIFT_DEBUG_HELP. Variables are |
| 144 | + // parsed by functions named parse_<type> above. An unknown type will |
| 145 | + // produce an error that parse_<unknown-type> doesn't exist. Add new parsers |
| 146 | + // above. |
| 147 | +#define VARIABLE(name, type, defaultValue, help) \ |
| 148 | + if (strncmp(*var, #name "=", strlen(#name "=")) == 0) { \ |
| 149 | + name ## _variable = \ |
| 150 | + parse_ ## type(#name, *var + strlen(#name "="), defaultValue); \ |
| 151 | + foundVariable = true; \ |
| 152 | + } |
| 153 | + // SWIFT_DEBUG_HELP is not in the variables list. Parse it like the other |
| 154 | + // variables. |
| 155 | + VARIABLE(SWIFT_DEBUG_HELP, bool, false, ) |
| 156 | +#include "EnvironmentVariables.def" |
| 157 | + |
| 158 | + // Flag unknown SWIFT_DEBUG_ variables to catch misspellings. We don't flag |
| 159 | + // all unknown SWIFT_ variables, because there are a bunch of other SWIFT_ |
| 160 | + // variables used for other purposes, such as SWIFT_SOURCE_ROOT and |
| 161 | + // SWIFT_INSTALL_DIR, and we don't want to warn for all of those. |
| 162 | + const char *swiftDebugPrefix = "SWIFT_DEBUG_"; |
| 163 | + if (!foundVariable && |
| 164 | + strncmp(*var, swiftDebugPrefix, strlen(swiftDebugPrefix)) == 0) { |
| 165 | + const char *equals = strchr(*var, '='); |
| 166 | + if (!equals) |
| 167 | + equals = *var + strlen(*var); |
| 168 | + swift::warning(RuntimeErrorFlagNone, |
| 169 | + "Warning: unknown environment variable %.*s\n", |
| 170 | + (int)(equals - *var), *var); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + if (SWIFT_DEBUG_HELP_variable) |
| 175 | + printHelp(nullptr); |
| 176 | +} |
| 177 | +#else |
| 178 | +void swift::runtime::environment::initialize(void *context) { |
| 179 | + // Emit a getenv call for each variable. This is less efficient but works |
| 180 | + // everywhere. |
| 181 | +#define VARIABLE(name, type, defaultValue, help) \ |
| 182 | + name ## _variable = parse_ ## type(#name, getenv(#name), defaultValue); |
| 183 | +#include "EnvironmentVariables.def" |
| 184 | + |
| 185 | + // Print help if requested. |
| 186 | + if (parse_bool("SWIFT_DEBUG_HELP", getenv("SWIFT_DEBUG_HELP"), false)) |
| 187 | + printHelp("Using getenv to read variables. Unknown SWIFT_DEBUG_ variables " |
| 188 | + "will not be flagged."); |
| 189 | +} |
| 190 | +#endif |
0 commit comments