Line data Source code
1 : // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/flags.h"
6 :
7 : #include <cctype>
8 : #include <cstdlib>
9 : #include <sstream>
10 :
11 : #include "src/allocation.h"
12 : #include "src/assembler.h"
13 : #include "src/base/functional.h"
14 : #include "src/base/platform/platform.h"
15 : #include "src/ostreams.h"
16 : #include "src/utils.h"
17 : #include "src/wasm/wasm-limits.h"
18 :
19 : namespace v8 {
20 : namespace internal {
21 :
22 : // Define all of our flags.
23 : #define FLAG_MODE_DEFINE
24 : #include "src/flag-definitions.h" // NOLINT(build/include)
25 :
26 : // Define all of our flags default values.
27 : #define FLAG_MODE_DEFINE_DEFAULTS
28 : #include "src/flag-definitions.h" // NOLINT(build/include)
29 :
30 : namespace {
31 :
32 : // This structure represents a single entry in the flag system, with a pointer
33 : // to the actual flag, default value, comment, etc. This is designed to be POD
34 : // initialized as to avoid requiring static constructors.
35 : struct Flag {
36 : enum FlagType {
37 : TYPE_BOOL,
38 : TYPE_MAYBE_BOOL,
39 : TYPE_INT,
40 : TYPE_UINT,
41 : TYPE_FLOAT,
42 : TYPE_STRING,
43 : TYPE_ARGS
44 : };
45 :
46 : FlagType type_; // What type of flag, bool, int, or string.
47 : const char* name_; // Name of the flag, ex "my_flag".
48 : void* valptr_; // Pointer to the global flag variable.
49 : const void* defptr_; // Pointer to the default value.
50 : const char* cmt_; // A comment about the flags purpose.
51 : bool owns_ptr_; // Does the flag own its string value?
52 :
53 : FlagType type() const { return type_; }
54 :
55 : const char* name() const { return name_; }
56 :
57 : const char* comment() const { return cmt_; }
58 :
59 : bool* bool_variable() const {
60 : DCHECK(type_ == TYPE_BOOL);
61 : return reinterpret_cast<bool*>(valptr_);
62 : }
63 :
64 : MaybeBoolFlag* maybe_bool_variable() const {
65 : DCHECK(type_ == TYPE_MAYBE_BOOL);
66 : return reinterpret_cast<MaybeBoolFlag*>(valptr_);
67 : }
68 :
69 : int* int_variable() const {
70 : DCHECK(type_ == TYPE_INT);
71 : return reinterpret_cast<int*>(valptr_);
72 : }
73 :
74 : unsigned int* uint_variable() const {
75 : DCHECK(type_ == TYPE_UINT);
76 : return reinterpret_cast<unsigned int*>(valptr_);
77 : }
78 :
79 : double* float_variable() const {
80 : DCHECK(type_ == TYPE_FLOAT);
81 : return reinterpret_cast<double*>(valptr_);
82 : }
83 :
84 : const char* string_value() const {
85 : DCHECK(type_ == TYPE_STRING);
86 3156073 : return *reinterpret_cast<const char**>(valptr_);
87 : }
88 :
89 : void set_string_value(const char* value, bool owns_ptr) {
90 : DCHECK(type_ == TYPE_STRING);
91 : const char** ptr = reinterpret_cast<const char**>(valptr_);
92 646465 : if (owns_ptr_ && *ptr != nullptr) DeleteArray(*ptr);
93 646465 : *ptr = value;
94 646465 : owns_ptr_ = owns_ptr;
95 : }
96 :
97 : JSArguments* args_variable() const {
98 : DCHECK(type_ == TYPE_ARGS);
99 : return reinterpret_cast<JSArguments*>(valptr_);
100 : }
101 :
102 : bool bool_default() const {
103 : DCHECK(type_ == TYPE_BOOL);
104 55216736 : return *reinterpret_cast<const bool*>(defptr_);
105 : }
106 :
107 : int int_default() const {
108 : DCHECK(type_ == TYPE_INT);
109 7810262 : return *reinterpret_cast<const int*>(defptr_);
110 : }
111 :
112 : unsigned int uint_default() const {
113 : DCHECK(type_ == TYPE_UINT);
114 544902 : return *reinterpret_cast<const unsigned int*>(defptr_);
115 : }
116 :
117 : double float_default() const {
118 : DCHECK(type_ == TYPE_FLOAT);
119 544902 : return *reinterpret_cast<const double*>(defptr_);
120 : }
121 :
122 : const char* string_default() const {
123 : DCHECK(type_ == TYPE_STRING);
124 3632680 : return *reinterpret_cast<const char* const *>(defptr_);
125 : }
126 :
127 : JSArguments args_default() const {
128 : DCHECK(type_ == TYPE_ARGS);
129 29636 : return *reinterpret_cast<const JSArguments*>(defptr_);
130 : }
131 :
132 : // Compare this flag's current value against the default.
133 172517730 : bool IsDefault() const {
134 57911238 : switch (type_) {
135 : case TYPE_BOOL:
136 92414784 : return *bool_variable() == bool_default();
137 : case TYPE_MAYBE_BOOL:
138 1063986 : return maybe_bool_variable()->has_value == false;
139 : case TYPE_INT:
140 13071828 : return *int_variable() == int_default();
141 : case TYPE_UINT:
142 911988 : return *uint_variable() == uint_default();
143 : case TYPE_FLOAT:
144 911988 : return *float_variable() == float_default();
145 : case TYPE_STRING: {
146 : const char* str1 = string_value();
147 : const char* str2 = string_default();
148 3039960 : if (str2 == nullptr) return str1 == nullptr;
149 1519980 : if (str1 == nullptr) return str2 == nullptr;
150 1519980 : return strcmp(str1, str2) == 0;
151 : }
152 : case TYPE_ARGS:
153 151998 : return args_variable()->argc == 0;
154 : }
155 0 : UNREACHABLE();
156 : }
157 :
158 : // Set a flag back to it's default value.
159 23175352 : void Reset() {
160 11291316 : switch (type_) {
161 : case TYPE_BOOL:
162 9009344 : *bool_variable() = bool_default();
163 9009344 : break;
164 : case TYPE_MAYBE_BOOL:
165 207452 : *maybe_bool_variable() = MaybeBoolFlag::Create(false, false);
166 207452 : break;
167 : case TYPE_INT:
168 1274348 : *int_variable() = int_default();
169 1274348 : break;
170 : case TYPE_UINT:
171 88908 : *uint_variable() = uint_default();
172 88908 : break;
173 : case TYPE_FLOAT:
174 88908 : *float_variable() = float_default();
175 88908 : break;
176 : case TYPE_STRING:
177 : set_string_value(string_default(), false);
178 : break;
179 : case TYPE_ARGS:
180 29636 : *args_variable() = args_default();
181 29636 : break;
182 : }
183 11291316 : }
184 : };
185 :
186 : Flag flags[] = {
187 : #define FLAG_MODE_META
188 : #include "src/flag-definitions.h" // NOLINT(build/include)
189 : };
190 :
191 : const size_t num_flags = sizeof(flags) / sizeof(*flags);
192 :
193 : } // namespace
194 :
195 :
196 2316 : static const char* Type2String(Flag::FlagType type) {
197 2316 : switch (type) {
198 : case Flag::TYPE_BOOL: return "bool";
199 42 : case Flag::TYPE_MAYBE_BOOL: return "maybe_bool";
200 270 : case Flag::TYPE_INT: return "int";
201 : case Flag::TYPE_UINT:
202 18 : return "uint";
203 30 : case Flag::TYPE_FLOAT: return "float";
204 126 : case Flag::TYPE_STRING: return "string";
205 6 : case Flag::TYPE_ARGS: return "arguments";
206 : }
207 0 : UNREACHABLE();
208 : }
209 :
210 :
211 2197878 : std::ostream& operator<<(std::ostream& os, const Flag& flag) { // NOLINT
212 1098939 : switch (flag.type()) {
213 : case Flag::TYPE_BOOL:
214 791973 : os << (*flag.bool_variable() ? "true" : "false");
215 791973 : break;
216 : case Flag::TYPE_MAYBE_BOOL:
217 : os << (flag.maybe_bool_variable()->has_value
218 : ? (flag.maybe_bool_variable()->value ? "true" : "false")
219 66 : : "unset");
220 66 : break;
221 : case Flag::TYPE_INT:
222 190529 : os << *flag.int_variable();
223 190529 : break;
224 : case Flag::TYPE_UINT:
225 46 : os << *flag.uint_variable();
226 : break;
227 : case Flag::TYPE_FLOAT:
228 206 : os << *flag.float_variable();
229 : break;
230 : case Flag::TYPE_STRING: {
231 : const char* str = flag.string_value();
232 116113 : os << (str ? str : "nullptr");
233 116113 : break;
234 : }
235 : case Flag::TYPE_ARGS: {
236 6 : JSArguments args = *flag.args_variable();
237 6 : if (args.argc > 0) {
238 0 : os << args[0];
239 0 : for (int i = 1; i < args.argc; i++) {
240 0 : os << args[i];
241 : }
242 : }
243 : break;
244 : }
245 : }
246 1098939 : return os;
247 : }
248 :
249 :
250 : // static
251 40 : std::vector<const char*>* FlagList::argv() {
252 40 : std::vector<const char*>* args = new std::vector<const char*>(8);
253 0 : Flag* args_flag = nullptr;
254 15280 : for (size_t i = 0; i < num_flags; ++i) {
255 15840 : Flag* f = &flags[i];
256 15240 : if (!f->IsDefault()) {
257 160 : if (f->type() == Flag::TYPE_ARGS) {
258 : DCHECK_NULL(args_flag);
259 : args_flag = f; // Must be last in arguments.
260 : continue;
261 : }
262 : {
263 280 : bool disabled = f->type() == Flag::TYPE_BOOL && !*f->bool_variable();
264 160 : std::ostringstream os;
265 160 : os << (disabled ? "--no" : "--") << f->name();
266 480 : args->push_back(StrDup(os.str().c_str()));
267 : }
268 160 : if (f->type() != Flag::TYPE_BOOL) {
269 40 : std::ostringstream os;
270 40 : os << *f;
271 120 : args->push_back(StrDup(os.str().c_str()));
272 : }
273 : }
274 : }
275 40 : if (args_flag != nullptr) {
276 0 : std::ostringstream os;
277 0 : os << "--" << args_flag->name();
278 0 : args->push_back(StrDup(os.str().c_str()));
279 0 : JSArguments jsargs = *args_flag->args_variable();
280 0 : for (int j = 0; j < jsargs.argc; j++) {
281 0 : args->push_back(StrDup(jsargs[j]));
282 0 : }
283 : }
284 40 : return args;
285 : }
286 :
287 :
288 : inline char NormalizeChar(char ch) {
289 203421337 : return ch == '_' ? '-' : ch;
290 : }
291 :
292 : // Helper function to parse flags: Takes an argument arg and splits it into
293 : // a flag name and flag value (or nullptr if they are missing). is_bool is set
294 : // if the arg started with "-no" or "--no". The buffer may be used to NUL-
295 : // terminate the name, it must be large enough to hold any possible name.
296 449453 : static void SplitArgument(const char* arg,
297 : char* buffer,
298 : int buffer_size,
299 : const char** name,
300 : const char** value,
301 : bool* is_bool) {
302 449453 : *name = nullptr;
303 449453 : *value = nullptr;
304 449453 : *is_bool = false;
305 :
306 449453 : if (arg != nullptr && *arg == '-') {
307 : // find the begin of the flag name
308 329975 : arg++; // remove 1st '-'
309 329975 : if (*arg == '-') {
310 328435 : arg++; // remove 2nd '-'
311 328435 : if (arg[0] == '\0') {
312 : const char* kJSArgumentsFlagName = "js_arguments";
313 18 : *name = kJSArgumentsFlagName;
314 449471 : return;
315 : }
316 : }
317 329957 : if (arg[0] == 'n' && arg[1] == 'o') {
318 102761 : arg += 2; // remove "no"
319 205522 : if (NormalizeChar(arg[0]) == '-') arg++; // remove dash after "no".
320 102761 : *is_bool = true;
321 : }
322 329957 : *name = arg;
323 :
324 : // find the end of the flag name
325 5971897 : while (*arg != '\0' && *arg != '=')
326 5311983 : arg++;
327 :
328 : // get the value if any
329 329957 : if (*arg == '=') {
330 : // make a copy so we can NUL-terminate flag name
331 134713 : size_t n = arg - *name;
332 134713 : CHECK(n < static_cast<size_t>(buffer_size)); // buffer is too small
333 : MemCopy(buffer, *name, n);
334 134713 : buffer[n] = '\0';
335 134713 : *name = buffer;
336 : // get the value
337 134713 : *value = arg + 1;
338 : }
339 : }
340 : }
341 :
342 :
343 : static bool EqualNames(const char* a, const char* b) {
344 220789095 : for (int i = 0; NormalizeChar(a[i]) == NormalizeChar(b[i]); i++) {
345 17791235 : if (a[i] == '\0') {
346 : return true;
347 : }
348 : }
349 : return false;
350 : }
351 :
352 :
353 329975 : static Flag* FindFlag(const char* name) {
354 84198028 : for (size_t i = 0; i < num_flags; ++i) {
355 168377538 : if (EqualNames(name, flags[i].name()))
356 : return &flags[i];
357 : }
358 : return nullptr;
359 : }
360 :
361 :
362 : // static
363 152133 : int FlagList::SetFlagsFromCommandLine(int* argc,
364 : char** argv,
365 : bool remove_flags) {
366 : int return_code = 0;
367 : // parse arguments
368 753683 : for (int i = 1; i < *argc;) {
369 : int j = i; // j > 0
370 449453 : const char* arg = argv[i++];
371 :
372 : // split arg into flag components
373 : char buffer[1*KB];
374 : const char* name;
375 : const char* value;
376 : bool is_bool;
377 449453 : SplitArgument(arg, buffer, sizeof buffer, &name, &value, &is_bool);
378 :
379 449453 : if (name != nullptr) {
380 : // lookup the flag
381 1612803 : Flag* flag = FindFlag(name);
382 329975 : if (flag == nullptr) {
383 9259 : if (remove_flags) {
384 : // We don't recognize this flag but since we're removing
385 : // the flags we recognize we assume that the remaining flags
386 : // will be processed somewhere else so this flag might make
387 : // sense there.
388 9253 : continue;
389 : } else {
390 : PrintF(stderr, "Error: unrecognized flag %s\n"
391 6 : "Try --help for options\n", arg);
392 : return_code = j;
393 6 : break;
394 : }
395 : }
396 :
397 : // if we still need a flag value, use the next argument if available
398 455620 : if (flag->type() != Flag::TYPE_BOOL &&
399 134875 : flag->type() != Flag::TYPE_MAYBE_BOOL &&
400 455561 : flag->type() != Flag::TYPE_ARGS && value == nullptr) {
401 156 : if (i < *argc) {
402 138 : value = argv[i++];
403 : }
404 156 : if (!value) {
405 : PrintF(stderr, "Error: missing value for flag %s of type %s\n"
406 : "Try --help for options\n",
407 18 : arg, Type2String(flag->type()));
408 : return_code = j;
409 18 : break;
410 : }
411 : }
412 :
413 : // set the flag
414 320698 : char* endp = const_cast<char*>(""); // *endp is only read
415 320698 : switch (flag->type()) {
416 : case Flag::TYPE_BOOL:
417 185812 : *flag->bool_variable() = !is_bool;
418 185812 : break;
419 : case Flag::TYPE_MAYBE_BOOL:
420 58 : *flag->maybe_bool_variable() = MaybeBoolFlag::Create(true, !is_bool);
421 29 : break;
422 : case Flag::TYPE_INT:
423 81052 : *flag->int_variable() = static_cast<int>(strtol(value, &endp, 10));
424 81052 : break;
425 : case Flag::TYPE_UINT: {
426 : // We do not use strtoul because it accepts negative numbers.
427 6 : int64_t val = static_cast<int64_t>(strtoll(value, &endp, 10));
428 6 : if (val < 0 || val > std::numeric_limits<unsigned int>::max()) {
429 : PrintF(stderr,
430 : "Error: Value for flag %s of type %s is out of bounds "
431 : "[0-%" PRIu64
432 : "]\n"
433 : "Try --help for options\n",
434 : arg, Type2String(flag->type()),
435 : static_cast<uint64_t>(
436 0 : std::numeric_limits<unsigned int>::max()));
437 : return_code = j;
438 0 : break;
439 : }
440 6 : *flag->uint_variable() = static_cast<unsigned int>(val);
441 6 : break;
442 : }
443 : case Flag::TYPE_FLOAT:
444 24 : *flag->float_variable() = strtod(value, &endp);
445 24 : break;
446 : case Flag::TYPE_STRING:
447 53745 : flag->set_string_value(value ? StrDup(value) : nullptr, true);
448 : break;
449 : case Flag::TYPE_ARGS: {
450 30 : int start_pos = (value == nullptr) ? i : i - 1;
451 30 : int js_argc = *argc - start_pos;
452 30 : const char** js_argv = NewArray<const char*>(js_argc);
453 30 : if (value != nullptr) {
454 6 : js_argv[0] = StrDup(value);
455 : }
456 72 : for (int k = i; k < *argc; k++) {
457 42 : js_argv[k - start_pos] = StrDup(argv[k]);
458 : }
459 30 : *flag->args_variable() = JSArguments::Create(js_argc, js_argv);
460 30 : i = *argc; // Consume all arguments
461 30 : break;
462 : }
463 : }
464 :
465 : // handle errors
466 320698 : bool is_bool_type = flag->type() == Flag::TYPE_BOOL ||
467 : flag->type() == Flag::TYPE_MAYBE_BOOL;
468 641396 : if ((is_bool_type && value != nullptr) || (!is_bool_type && is_bool) ||
469 320698 : *endp != '\0') {
470 : PrintF(stderr, "Error: illegal value for flag %s of type %s\n"
471 : "Try --help for options\n",
472 12 : arg, Type2String(flag->type()));
473 12 : if (is_bool_type) {
474 : PrintF(stderr,
475 0 : "To set or unset a boolean flag, use --flag or --no-flag.\n");
476 : }
477 : return_code = j;
478 : break;
479 : }
480 :
481 : // remove the flag & value from the command
482 320686 : if (remove_flags) {
483 365522 : while (j < i) {
484 182789 : argv[j++] = nullptr;
485 : }
486 : }
487 : }
488 : }
489 :
490 : // shrink the argument list
491 152133 : if (remove_flags) {
492 : int j = 1;
493 311240 : for (int i = 1; i < *argc; i++) {
494 311240 : if (argv[i] != nullptr) argv[j++] = argv[i];
495 : }
496 54019 : *argc = j;
497 : }
498 :
499 152133 : if (FLAG_help) {
500 0 : PrintHelp();
501 0 : exit(0);
502 : }
503 : // parsed all flags successfully
504 152133 : return return_code;
505 : }
506 :
507 :
508 373810 : static char* SkipWhiteSpace(char* p) {
509 373810 : while (*p != '\0' && isspace(*p) != 0) p++;
510 373810 : return p;
511 : }
512 :
513 :
514 275742 : static char* SkipBlackSpace(char* p) {
515 275742 : while (*p != '\0' && isspace(*p) == 0) p++;
516 275742 : return p;
517 : }
518 :
519 :
520 : // static
521 98068 : int FlagList::SetFlagsFromString(const char* str, int len) {
522 : // make a 0-terminated copy of str
523 98068 : ScopedVector<char> copy0(len + 1);
524 98068 : MemCopy(copy0.start(), str, len);
525 98068 : copy0[len] = '\0';
526 :
527 : // strip leading white space
528 98068 : char* copy = SkipWhiteSpace(copy0.start());
529 :
530 : // count the number of 'arguments'
531 98068 : int argc = 1; // be compatible with SetFlagsFromCommandLine()
532 235939 : for (char* p = copy; *p != '\0'; argc++) {
533 137871 : p = SkipBlackSpace(p);
534 137871 : p = SkipWhiteSpace(p);
535 : }
536 :
537 : // allocate argument array
538 98068 : ScopedVector<char*> argv(argc);
539 :
540 : // split the flags string into arguments
541 98068 : argc = 1; // be compatible with SetFlagsFromCommandLine()
542 235939 : for (char* p = copy; *p != '\0'; argc++) {
543 275742 : argv[argc] = p;
544 137871 : p = SkipBlackSpace(p);
545 137871 : if (*p != '\0') *p++ = '\0'; // 0-terminate argument
546 137871 : p = SkipWhiteSpace(p);
547 : }
548 :
549 : // set the flags
550 98068 : int result = SetFlagsFromCommandLine(&argc, argv.start(), false);
551 :
552 98068 : return result;
553 : }
554 :
555 :
556 : // static
557 29636 : void FlagList::ResetAllFlags() {
558 11320952 : for (size_t i = 0; i < num_flags; ++i) {
559 11291316 : flags[i].Reset();
560 : }
561 29636 : }
562 :
563 :
564 : // static
565 6 : void FlagList::PrintHelp() {
566 : CpuFeatures::Probe(false);
567 6 : CpuFeatures::PrintTarget();
568 6 : CpuFeatures::PrintFeatures();
569 :
570 6 : OFStream os(stdout);
571 : os << "Usage:\n"
572 : " shell [options] -e string\n"
573 : " execute string in V8\n"
574 : " shell [options] file1 file2 ... filek\n"
575 : " run JavaScript scripts in file1, file2, ..., filek\n"
576 : " shell [options]\n"
577 : " shell [options] --shell [file1 file2 ... filek]\n"
578 : " run an interactive JavaScript shell\n"
579 : " d8 [options] file1 file2 ... filek\n"
580 : " d8 [options]\n"
581 : " d8 [options] --shell [file1 file2 ... filek]\n"
582 : " run the new debugging shell\n\n"
583 6 : "Options:\n";
584 :
585 2292 : for (size_t i = 0; i < num_flags; ++i) {
586 4572 : Flag* f = &flags[i];
587 2286 : os << " --" << f->name() << " (" << f->comment() << ")\n"
588 4572 : << " type: " << Type2String(f->type()) << " default: " << *f
589 2286 : << "\n";
590 6 : }
591 6 : }
592 :
593 :
594 : static uint32_t flag_hash = 0;
595 :
596 :
597 151958 : void ComputeFlagListHash() {
598 151958 : std::ostringstream modified_args_as_string;
599 : #ifdef DEBUG
600 : modified_args_as_string << "debug";
601 : #endif // DEBUG
602 58047956 : for (size_t i = 0; i < num_flags; ++i) {
603 57895998 : Flag* current = &flags[i];
604 57895998 : if (!current->IsDefault()) {
605 : modified_args_as_string << i;
606 1096613 : modified_args_as_string << *current;
607 : }
608 : }
609 : std::string args(modified_args_as_string.str());
610 : flag_hash = static_cast<uint32_t>(
611 455874 : base::hash_range(args.c_str(), args.c_str() + args.length()));
612 151958 : }
613 :
614 :
615 : // static
616 151958 : void FlagList::EnforceFlagImplications() {
617 : #define FLAG_MODE_DEFINE_IMPLICATIONS
618 : #include "src/flag-definitions.h" // NOLINT(build/include)
619 : #undef FLAG_MODE_DEFINE_IMPLICATIONS
620 151958 : ComputeFlagListHash();
621 151958 : }
622 :
623 :
624 734 : uint32_t FlagList::Hash() { return flag_hash; }
625 : } // namespace internal
626 : } // namespace v8
|