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