flags.cc 18.1 KB
Newer Older
1
// Copyright 2006-2008 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#include "src/flags/flags.h"
6

7
#include <cctype>
8
#include <cerrno>
9
#include <cinttypes>
10 11
#include <cstdlib>
#include <sstream>
12

13
#include "src/base/functional.h"
14
#include "src/base/platform/platform.h"
15
#include "src/codegen/cpu-features.h"
16
#include "src/logging/counters.h"
17 18 19 20
#include "src/utils/allocation.h"
#include "src/utils/memcopy.h"
#include "src/utils/ostreams.h"
#include "src/utils/utils.h"
21
#include "src/wasm/wasm-limits.h"
22

23 24
namespace v8 {
namespace internal {
25

26 27
// Define all of our flags.
#define FLAG_MODE_DEFINE
28
#include "src/flags/flag-definitions.h"  // NOLINT(build/include)
29

30 31
// Define all of our flags default values.
#define FLAG_MODE_DEFINE_DEFAULTS
32
#include "src/flags/flag-definitions.h"  // NOLINT(build/include)
33 34 35 36 37 38 39

namespace {

// This structure represents a single entry in the flag system, with a pointer
// to the actual flag, default value, comment, etc.  This is designed to be POD
// initialized as to avoid requiring static constructors.
struct Flag {
40 41 42 43 44
  enum FlagType {
    TYPE_BOOL,
    TYPE_MAYBE_BOOL,
    TYPE_INT,
    TYPE_UINT,
Yang Guo's avatar
Yang Guo committed
45
    TYPE_UINT64,
46
    TYPE_FLOAT,
47
    TYPE_SIZE_T,
48 49
    TYPE_STRING,
  };
50

51 52 53 54 55 56
  FlagType type_;       // What type of flag, bool, int, or string.
  const char* name_;    // Name of the flag, ex "my_flag".
  void* valptr_;        // Pointer to the global flag variable.
  const void* defptr_;  // Pointer to the default value.
  const char* cmt_;     // A comment about the flags purpose.
  bool owns_ptr_;       // Does the flag own its string value?
57 58

  FlagType type() const { return type_; }
59

60
  const char* name() const { return name_; }
61

62 63 64
  const char* comment() const { return cmt_; }

  bool* bool_variable() const {
65
    DCHECK(type_ == TYPE_BOOL);
66
    return reinterpret_cast<bool*>(valptr_);
67 68
  }

69
  MaybeBoolFlag* maybe_bool_variable() const {
70
    DCHECK(type_ == TYPE_MAYBE_BOOL);
71 72 73
    return reinterpret_cast<MaybeBoolFlag*>(valptr_);
  }

74
  int* int_variable() const {
75
    DCHECK(type_ == TYPE_INT);
76 77
    return reinterpret_cast<int*>(valptr_);
  }
78

79 80 81 82 83
  unsigned int* uint_variable() const {
    DCHECK(type_ == TYPE_UINT);
    return reinterpret_cast<unsigned int*>(valptr_);
  }

Yang Guo's avatar
Yang Guo committed
84 85 86 87 88
  uint64_t* uint64_variable() const {
    DCHECK(type_ == TYPE_UINT64);
    return reinterpret_cast<uint64_t*>(valptr_);
  }

89
  double* float_variable() const {
90
    DCHECK(type_ == TYPE_FLOAT);
91
    return reinterpret_cast<double*>(valptr_);
92 93
  }

94 95 96 97 98
  size_t* size_t_variable() const {
    DCHECK(type_ == TYPE_SIZE_T);
    return reinterpret_cast<size_t*>(valptr_);
  }

99
  const char* string_value() const {
100
    DCHECK(type_ == TYPE_STRING);
101 102 103
    return *reinterpret_cast<const char**>(valptr_);
  }

104
  void set_string_value(const char* value, bool owns_ptr) {
105
    DCHECK(type_ == TYPE_STRING);
106
    const char** ptr = reinterpret_cast<const char**>(valptr_);
107
    if (owns_ptr_ && *ptr != nullptr) DeleteArray(*ptr);
108 109
    *ptr = value;
    owns_ptr_ = owns_ptr;
110
  }
111

112
  bool bool_default() const {
113
    DCHECK(type_ == TYPE_BOOL);
114 115
    return *reinterpret_cast<const bool*>(defptr_);
  }
116

117
  int int_default() const {
118
    DCHECK(type_ == TYPE_INT);
119 120
    return *reinterpret_cast<const int*>(defptr_);
  }
121

122 123 124 125 126
  unsigned int uint_default() const {
    DCHECK(type_ == TYPE_UINT);
    return *reinterpret_cast<const unsigned int*>(defptr_);
  }

Yang Guo's avatar
Yang Guo committed
127 128 129 130 131
  uint64_t uint64_default() const {
    DCHECK(type_ == TYPE_UINT64);
    return *reinterpret_cast<const uint64_t*>(defptr_);
  }

132
  double float_default() const {
133
    DCHECK(type_ == TYPE_FLOAT);
134 135
    return *reinterpret_cast<const double*>(defptr_);
  }
136

137 138 139 140 141
  size_t size_t_default() const {
    DCHECK(type_ == TYPE_SIZE_T);
    return *reinterpret_cast<const size_t*>(defptr_);
  }

142
  const char* string_default() const {
143
    DCHECK(type_ == TYPE_STRING);
144
    return *reinterpret_cast<const char* const*>(defptr_);
145 146
  }

147 148 149
  // Compare this flag's current value against the default.
  bool IsDefault() const {
    switch (type_) {
150 151
      case TYPE_BOOL:
        return *bool_variable() == bool_default();
152 153
      case TYPE_MAYBE_BOOL:
        return maybe_bool_variable()->has_value == false;
154 155
      case TYPE_INT:
        return *int_variable() == int_default();
156 157
      case TYPE_UINT:
        return *uint_variable() == uint_default();
Yang Guo's avatar
Yang Guo committed
158 159
      case TYPE_UINT64:
        return *uint64_variable() == uint64_default();
160 161
      case TYPE_FLOAT:
        return *float_variable() == float_default();
162 163
      case TYPE_SIZE_T:
        return *size_t_variable() == size_t_default();
164
      case TYPE_STRING: {
165
        const char* str1 = string_value();
166
        const char* str2 = string_default();
167 168
        if (str2 == nullptr) return str1 == nullptr;
        if (str1 == nullptr) return str2 == nullptr;
169
        return strcmp(str1, str2) == 0;
170
      }
171
    }
172
    UNREACHABLE();
173
  }
174

175 176 177
  // Set a flag back to it's default value.
  void Reset() {
    switch (type_) {
178 179 180
      case TYPE_BOOL:
        *bool_variable() = bool_default();
        break;
181 182 183
      case TYPE_MAYBE_BOOL:
        *maybe_bool_variable() = MaybeBoolFlag::Create(false, false);
        break;
184 185 186
      case TYPE_INT:
        *int_variable() = int_default();
        break;
187 188 189
      case TYPE_UINT:
        *uint_variable() = uint_default();
        break;
Yang Guo's avatar
Yang Guo committed
190 191 192
      case TYPE_UINT64:
        *uint64_variable() = uint64_default();
        break;
193 194 195
      case TYPE_FLOAT:
        *float_variable() = float_default();
        break;
196 197 198
      case TYPE_SIZE_T:
        *size_t_variable() = size_t_default();
        break;
199
      case TYPE_STRING:
200
        set_string_value(string_default(), false);
201
        break;
202
    }
203
  }
204 205 206 207
};

Flag flags[] = {
#define FLAG_MODE_META
208
#include "src/flags/flag-definitions.h"  // NOLINT(build/include)
209 210 211 212 213
};

const size_t num_flags = sizeof(flags) / sizeof(*flags);

}  // namespace
214

215
static const char* Type2String(Flag::FlagType type) {
216
  switch (type) {
217 218 219 220 221 222
    case Flag::TYPE_BOOL:
      return "bool";
    case Flag::TYPE_MAYBE_BOOL:
      return "maybe_bool";
    case Flag::TYPE_INT:
      return "int";
223 224
    case Flag::TYPE_UINT:
      return "uint";
Yang Guo's avatar
Yang Guo committed
225 226
    case Flag::TYPE_UINT64:
      return "uint64";
227 228
    case Flag::TYPE_FLOAT:
      return "float";
229 230
    case Flag::TYPE_SIZE_T:
      return "size_t";
231 232
    case Flag::TYPE_STRING:
      return "string";
233 234 235 236
  }
  UNREACHABLE();
}

237
std::ostream& operator<<(std::ostream& os, const Flag& flag) {  // NOLINT
238
  switch (flag.type()) {
239
    case Flag::TYPE_BOOL:
240
      os << (*flag.bool_variable() ? "true" : "false");
241
      break;
242
    case Flag::TYPE_MAYBE_BOOL:
243 244 245
      os << (flag.maybe_bool_variable()->has_value
                 ? (flag.maybe_bool_variable()->value ? "true" : "false")
                 : "unset");
246
      break;
247
    case Flag::TYPE_INT:
248
      os << *flag.int_variable();
249
      break;
250 251 252
    case Flag::TYPE_UINT:
      os << *flag.uint_variable();
      break;
Yang Guo's avatar
Yang Guo committed
253 254 255
    case Flag::TYPE_UINT64:
      os << *flag.uint64_variable();
      break;
256
    case Flag::TYPE_FLOAT:
257
      os << *flag.float_variable();
258
      break;
259 260 261
    case Flag::TYPE_SIZE_T:
      os << *flag.size_t_variable();
      break;
262
    case Flag::TYPE_STRING: {
263
      const char* str = flag.string_value();
264
      os << (str ? str : "nullptr");
265 266
      break;
    }
267
  }
268
  return os;
269 270
}

271
// static
272 273
std::vector<const char*>* FlagList::argv() {
  std::vector<const char*>* args = new std::vector<const char*>(8);
274 275
  for (size_t i = 0; i < num_flags; ++i) {
    Flag* f = &flags[i];
276
    if (!f->IsDefault()) {
277 278
      {
        bool disabled = f->type() == Flag::TYPE_BOOL && !*f->bool_variable();
279
        std::ostringstream os;
280
        os << (disabled ? "--no" : "--") << f->name();
281
        args->push_back(StrDup(os.str().c_str()));
282
      }
283
      if (f->type() != Flag::TYPE_BOOL) {
284
        std::ostringstream os;
285
        os << *f;
286
        args->push_back(StrDup(os.str().c_str()));
287 288 289
      }
    }
  }
290
  return args;
291 292
}

293
inline char NormalizeChar(char ch) { return ch == '_' ? '-' : ch; }
294

295
// Helper function to parse flags: Takes an argument arg and splits it into
296
// a flag name and flag value (or nullptr if they are missing). negated is set
297 298
// if the arg started with "-no" or "--no". The buffer may be used to NUL-
// terminate the name, it must be large enough to hold any possible name.
299 300 301
static void SplitArgument(const char* arg, char* buffer, int buffer_size,
                          const char** name, const char** value,
                          bool* negated) {
302 303
  *name = nullptr;
  *value = nullptr;
304
  *negated = false;
305

306
  if (arg != nullptr && *arg == '-') {
307 308
    // find the begin of the flag name
    arg++;  // remove 1st '-'
309
    if (*arg == '-') {
310
      arg++;                    // remove 2nd '-'
311
      DCHECK_NE('\0', arg[0]);  // '--' arguments are handled in the caller.
312
    }
313
    if (arg[0] == 'n' && arg[1] == 'o') {
314
      arg += 2;                                 // remove "no"
315
      if (NormalizeChar(arg[0]) == '-') arg++;  // remove dash after "no".
316
      *negated = true;
317 318 319 320
    }
    *name = arg;

    // find the end of the flag name
321
    while (*arg != '\0' && *arg != '=') arg++;
322 323 324 325

    // get the value if any
    if (*arg == '=') {
      // make a copy so we can NUL-terminate flag name
326 327
      size_t n = arg - *name;
      CHECK(n < static_cast<size_t>(buffer_size));  // buffer is too small
328
      MemCopy(buffer, *name, n);
329 330 331 332 333 334 335 336
      buffer[n] = '\0';
      *name = buffer;
      // get the value
      *value = arg + 1;
    }
  }
}

337 338 339 340 341 342 343 344 345 346 347
static bool EqualNames(const char* a, const char* b) {
  for (int i = 0; NormalizeChar(a[i]) == NormalizeChar(b[i]); i++) {
    if (a[i] == '\0') {
      return true;
    }
  }
  return false;
}

static Flag* FindFlag(const char* name) {
  for (size_t i = 0; i < num_flags; ++i) {
348
    if (EqualNames(name, flags[i].name())) return &flags[i];
349
  }
350
  return nullptr;
351 352
}

353 354 355 356 357 358 359 360 361 362 363 364
template <typename T>
bool TryParseUnsigned(Flag* flag, const char* arg, const char* value,
                      char** endp, T* out_val) {
  // We do not use strtoul because it accepts negative numbers.
  // Rejects values >= 2**63 when T is 64 bits wide but that
  // seems like an acceptable trade-off.
  uint64_t max = static_cast<uint64_t>(std::numeric_limits<T>::max());
  errno = 0;
  int64_t val = static_cast<int64_t>(strtoll(value, endp, 10));
  if (val < 0 || static_cast<uint64_t>(val) > max || errno != 0) {
    PrintF(stderr,
           "Error: Value for flag %s of type %s is out of bounds "
365
           "[0-%" PRIu64 "]\n",
366 367 368 369 370 371
           arg, Type2String(flag->type()), max);
    return false;
  }
  *out_val = static_cast<T>(val);
  return true;
}
372 373

// static
374
int FlagList::SetFlagsFromCommandLine(int* argc, char** argv,
375
                                      bool remove_flags) {
376
  int return_code = 0;
377 378 379 380 381 382
  // parse arguments
  for (int i = 1; i < *argc;) {
    int j = i;  // j > 0
    const char* arg = argv[i++];

    // split arg into flag components
383
    char buffer[1 * KB];
384 385
    const char* name;
    const char* value;
386 387
    bool negated;
    SplitArgument(arg, buffer, sizeof buffer, &name, &value, &negated);
388

389
    if (name != nullptr) {
390
      // lookup the flag
391
      Flag* flag = FindFlag(name);
392
      if (flag == nullptr) {
393 394 395 396 397 398 399
        if (remove_flags) {
          // We don't recognize this flag but since we're removing
          // the flags we recognize we assume that the remaining flags
          // will be processed somewhere else so this flag might make
          // sense there.
          continue;
        } else {
400
          PrintF(stderr, "Error: unrecognized flag %s\n", arg);
401 402
          return_code = j;
          break;
403
        }
404 405 406
      }

      // if we still need a flag value, use the next argument if available
407
      if (flag->type() != Flag::TYPE_BOOL &&
408
          flag->type() != Flag::TYPE_MAYBE_BOOL && value == nullptr) {
409 410
        if (i < *argc) {
          value = argv[i++];
411 412
        }
        if (!value) {
413 414
          PrintF(stderr, "Error: missing value for flag %s of type %s\n", arg,
                 Type2String(flag->type()));
415 416
          return_code = j;
          break;
417 418 419 420 421 422
        }
      }

      // set the flag
      char* endp = const_cast<char*>("");  // *endp is only read
      switch (flag->type()) {
423
        case Flag::TYPE_BOOL:
424
          *flag->bool_variable() = !negated;
425
          break;
426
        case Flag::TYPE_MAYBE_BOOL:
427
          *flag->maybe_bool_variable() = MaybeBoolFlag::Create(true, !negated);
428
          break;
429
        case Flag::TYPE_INT:
430
          *flag->int_variable() = static_cast<int>(strtol(value, &endp, 10));
431
          break;
432 433 434
        case Flag::TYPE_UINT:
          if (!TryParseUnsigned(flag, arg, value, &endp,
                                flag->uint_variable())) {
435 436 437
            return_code = j;
          }
          break;
Yang Guo's avatar
Yang Guo committed
438 439 440 441 442 443
        case Flag::TYPE_UINT64:
          if (!TryParseUnsigned(flag, arg, value, &endp,
                                flag->uint64_variable())) {
            return_code = j;
          }
          break;
444
        case Flag::TYPE_FLOAT:
445 446
          *flag->float_variable() = strtod(value, &endp);
          break;
447 448 449 450 451 452
        case Flag::TYPE_SIZE_T:
          if (!TryParseUnsigned(flag, arg, value, &endp,
                                flag->size_t_variable())) {
            return_code = j;
          }
          break;
453
        case Flag::TYPE_STRING:
454
          flag->set_string_value(value ? StrDup(value) : nullptr, true);
455 456 457 458
          break;
      }

      // handle errors
459
      bool is_bool_type = flag->type() == Flag::TYPE_BOOL ||
460
                          flag->type() == Flag::TYPE_MAYBE_BOOL;
461
      if ((is_bool_type && value != nullptr) || (!is_bool_type && negated) ||
462
          *endp != '\0') {
463 464 465 466
        // TODO(neis): TryParseUnsigned may return with {*endp == '\0'} even in
        // an error case.
        PrintF(stderr, "Error: illegal value for flag %s of type %s\n", arg,
               Type2String(flag->type()));
467 468 469 470
        if (is_bool_type) {
          PrintF(stderr,
                 "To set or unset a boolean flag, use --flag or --no-flag.\n");
        }
471 472
        return_code = j;
        break;
473 474 475
      }

      // remove the flag & value from the command
476 477
      if (remove_flags) {
        while (j < i) {
478
          argv[j++] = nullptr;
479 480
        }
      }
481 482 483
    }
  }

484 485 486 487 488
  if (FLAG_help) {
    PrintHelp();
    exit(0);
  }

489
  if (remove_flags) {
490
    // shrink the argument list
491 492
    int j = 1;
    for (int i = 1; i < *argc; i++) {
493
      if (argv[i] != nullptr) argv[j++] = argv[i];
494 495
    }
    *argc = j;
496 497 498 499 500 501 502 503
  } else if (return_code != 0) {
    if (return_code + 1 < *argc) {
      PrintF(stderr, "The remaining arguments were ignored:");
      for (int i = return_code + 1; i < *argc; ++i) {
        PrintF(stderr, " %s", argv[i]);
      }
      PrintF(stderr, "\n");
    }
504
  }
505
  if (return_code != 0) PrintF(stderr, "Try --help for options\n");
506

507
  return return_code;
508 509 510 511 512 513 514 515 516 517 518 519
}

static char* SkipWhiteSpace(char* p) {
  while (*p != '\0' && isspace(*p) != 0) p++;
  return p;
}

static char* SkipBlackSpace(char* p) {
  while (*p != '\0' && isspace(*p) == 0) p++;
  return p;
}

520
// static
521
int FlagList::SetFlagsFromString(const char* str, size_t len) {
522
  // make a 0-terminated copy of str
523 524
  std::unique_ptr<char[]> copy0{NewArray<char>(len + 1)};
  MemCopy(copy0.get(), str, len);
525 526 527
  copy0[len] = '\0';

  // strip leading white space
528
  char* copy = SkipWhiteSpace(copy0.get());
529 530 531 532 533 534 535 536 537

  // count the number of 'arguments'
  int argc = 1;  // be compatible with SetFlagsFromCommandLine()
  for (char* p = copy; *p != '\0'; argc++) {
    p = SkipBlackSpace(p);
    p = SkipWhiteSpace(p);
  }

  // allocate argument array
538
  ScopedVector<char*> argv(argc);
539 540 541 542 543 544 545 546 547 548

  // split the flags string into arguments
  argc = 1;  // be compatible with SetFlagsFromCommandLine()
  for (char* p = copy; *p != '\0'; argc++) {
    argv[argc] = p;
    p = SkipBlackSpace(p);
    if (*p != '\0') *p++ = '\0';  // 0-terminate argument
    p = SkipWhiteSpace(p);
  }

549
  return SetFlagsFromCommandLine(&argc, argv.begin(), false);
550 551
}

552 553 554 555 556 557 558 559 560
// static
void FlagList::ResetAllFlags() {
  for (size_t i = 0; i < num_flags; ++i) {
    flags[i].Reset();
  }
}

// static
void FlagList::PrintHelp() {
561
  CpuFeatures::Probe(false);
562 563 564
  CpuFeatures::PrintTarget();
  CpuFeatures::PrintFeatures();

565
  StdoutStream os;
566 567 568 569 570 571
  os << "Synopsis:\n"
        "  shell [options] [--shell] [<file>...]\n"
        "  d8 [options] [-e <string>] [--shell] [[--module] <file>...]\n\n"
        "  -e        execute a string in V8\n"
        "  --shell   run an interactive JavaScript shell\n"
        "  --module  execute a file as a JavaScript module\n\n"
572
        "Note: the --module option is implicitly enabled for *.mjs files.\n\n"
573 574 575
        "The following syntax for options is accepted (both '-' and '--' are "
        "ok):\n"
        "  --flag        (bool flags only)\n"
576
        "  --no-flag     (bool flags only)\n"
577 578
        "  --flag=value  (non-bool flags only, no spaces around '=')\n"
        "  --flag value  (non-bool flags only)\n"
579
        "  --            (captures all remaining args in JavaScript)\n\n"
580 581
        "Options:\n";

582 583 584 585 586 587 588
  for (const Flag& f : flags) {
    os << "  --";
    for (const char* c = f.name(); *c != '\0'; ++c) {
      os << NormalizeChar(*c);
    }
    os << " (" << f.comment() << ")\n"
       << "        type: " << Type2String(f.type()) << "  default: " << f
589
       << "\n";
590
  }
591 592
}

593
static uint32_t flag_hash = 0;
594

595
void ComputeFlagListHash() {
596
  std::ostringstream modified_args_as_string;
597 598 599 600 601 602
  if (COMPRESS_POINTERS_BOOL) {
    modified_args_as_string << "ptr-compr";
  }
  if (DEBUG_BOOL) {
    modified_args_as_string << "debug";
  }
603 604
  for (size_t i = 0; i < num_flags; ++i) {
    Flag* current = &flags[i];
605 606 607 608 609 610
    if (current->type() == Flag::TYPE_BOOL &&
        current->bool_variable() == &FLAG_profile_deserialization) {
      // We want to be able to flip --profile-deserialization without
      // causing the code cache to get invalidated by this hash.
      continue;
    }
611
    if (!current->IsDefault()) {
612
      modified_args_as_string << i;
613 614 615 616
      modified_args_as_string << *current;
    }
  }
  std::string args(modified_args_as_string.str());
617
  flag_hash = static_cast<uint32_t>(
618 619
      base::hash_range(args.c_str(), args.c_str() + args.length()));
}
620 621 622 623

// static
void FlagList::EnforceFlagImplications() {
#define FLAG_MODE_DEFINE_IMPLICATIONS
624
#include "src/flags/flag-definitions.h"  // NOLINT(build/include)
625 626 627 628 629
#undef FLAG_MODE_DEFINE_IMPLICATIONS
  ComputeFlagListHash();
}

uint32_t FlagList::Hash() { return flag_hash; }
630 631 632 633 634

#undef FLAG_MODE_DEFINE
#undef FLAG_MODE_DEFINE_DEFAULTS
#undef FLAG_MODE_META

635 636
}  // namespace internal
}  // namespace v8