memprof_flags.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //===-- memprof_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. //
  9. // This file is a part of MemProfiler, a memory profiler.
  10. //
  11. // MemProf flag parsing logic.
  12. //===----------------------------------------------------------------------===//
  13. #include "memprof_flags.h"
  14. #include "memprof_interface_internal.h"
  15. #include "memprof_stack.h"
  16. #include "sanitizer_common/sanitizer_common.h"
  17. #include "sanitizer_common/sanitizer_flag_parser.h"
  18. #include "sanitizer_common/sanitizer_flags.h"
  19. namespace __memprof {
  20. Flags memprof_flags_dont_use_directly; // use via flags().
  21. static const char *MaybeUseMemprofDefaultOptionsCompileDefinition() {
  22. #ifdef MEMPROF_DEFAULT_OPTIONS
  23. return SANITIZER_STRINGIFY(MEMPROF_DEFAULT_OPTIONS);
  24. #else
  25. return "";
  26. #endif
  27. }
  28. void Flags::SetDefaults() {
  29. #define MEMPROF_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
  30. #include "memprof_flags.inc"
  31. #undef MEMPROF_FLAG
  32. }
  33. static void RegisterMemprofFlags(FlagParser *parser, Flags *f) {
  34. #define MEMPROF_FLAG(Type, Name, DefaultValue, Description) \
  35. RegisterFlag(parser, #Name, Description, &f->Name);
  36. #include "memprof_flags.inc"
  37. #undef MEMPROF_FLAG
  38. }
  39. void InitializeFlags() {
  40. // Set the default values and prepare for parsing MemProf and common flags.
  41. SetCommonFlagsDefaults();
  42. {
  43. CommonFlags cf;
  44. cf.CopyFrom(*common_flags());
  45. cf.external_symbolizer_path = GetEnv("MEMPROF_SYMBOLIZER_PATH");
  46. cf.malloc_context_size = kDefaultMallocContextSize;
  47. cf.intercept_tls_get_addr = true;
  48. cf.exitcode = 1;
  49. OverrideCommonFlags(cf);
  50. }
  51. Flags *f = flags();
  52. f->SetDefaults();
  53. FlagParser memprof_parser;
  54. RegisterMemprofFlags(&memprof_parser, f);
  55. RegisterCommonFlags(&memprof_parser);
  56. // Override from MemProf compile definition.
  57. const char *memprof_compile_def =
  58. MaybeUseMemprofDefaultOptionsCompileDefinition();
  59. memprof_parser.ParseString(memprof_compile_def);
  60. // Override from user-specified string.
  61. const char *memprof_default_options = __memprof_default_options();
  62. memprof_parser.ParseString(memprof_default_options);
  63. // Override from command line.
  64. memprof_parser.ParseStringFromEnv("MEMPROF_OPTIONS");
  65. InitializeCommonFlags();
  66. if (Verbosity())
  67. ReportUnrecognizedFlags();
  68. if (common_flags()->help) {
  69. memprof_parser.PrintFlagDescriptions();
  70. }
  71. CHECK_LE((uptr)common_flags()->malloc_context_size, kStackTraceMax);
  72. }
  73. } // namespace __memprof
  74. SANITIZER_INTERFACE_WEAK_DEF(const char *, __memprof_default_options, void) {
  75. return "";
  76. }