flags.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //===-- flags.cpp -----------------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "flags.h"
  9. #include "common.h"
  10. #include "flags_parser.h"
  11. #include "scudo/interface.h"
  12. namespace scudo {
  13. Flags *getFlags() {
  14. static Flags F;
  15. return &F;
  16. }
  17. void Flags::setDefaults() {
  18. #define SCUDO_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
  19. #include "flags.inc"
  20. #undef SCUDO_FLAG
  21. #ifdef GWP_ASAN_HOOKS
  22. #define GWP_ASAN_OPTION(Type, Name, DefaultValue, Description) \
  23. GWP_ASAN_##Name = DefaultValue;
  24. #include "gwp_asan/options.inc"
  25. #undef GWP_ASAN_OPTION
  26. #endif // GWP_ASAN_HOOKS
  27. }
  28. void registerFlags(FlagParser *Parser, Flags *F) {
  29. #define SCUDO_FLAG(Type, Name, DefaultValue, Description) \
  30. Parser->registerFlag(#Name, Description, FlagType::FT_##Type, \
  31. reinterpret_cast<void *>(&F->Name));
  32. #include "flags.inc"
  33. #undef SCUDO_FLAG
  34. #ifdef GWP_ASAN_HOOKS
  35. #define GWP_ASAN_OPTION(Type, Name, DefaultValue, Description) \
  36. Parser->registerFlag("GWP_ASAN_" #Name, Description, FlagType::FT_##Type, \
  37. reinterpret_cast<void *>(&F->GWP_ASAN_##Name));
  38. #include "gwp_asan/options.inc"
  39. #undef GWP_ASAN_OPTION
  40. #endif // GWP_ASAN_HOOKS
  41. }
  42. static const char *getCompileDefinitionScudoDefaultOptions() {
  43. #ifdef SCUDO_DEFAULT_OPTIONS
  44. return STRINGIFY(SCUDO_DEFAULT_OPTIONS);
  45. #else
  46. return "";
  47. #endif
  48. }
  49. static const char *getScudoDefaultOptions() {
  50. return (&__scudo_default_options) ? __scudo_default_options() : "";
  51. }
  52. void initFlags() {
  53. Flags *F = getFlags();
  54. F->setDefaults();
  55. FlagParser Parser;
  56. registerFlags(&Parser, F);
  57. Parser.parseString(getCompileDefinitionScudoDefaultOptions());
  58. Parser.parseString(getScudoDefaultOptions());
  59. Parser.parseString(getEnv("SCUDO_OPTIONS"));
  60. }
  61. } // namespace scudo