1 //===-- memprof_flags.cpp --------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of MemProfiler, a memory profiler.
11 // MemProf flag parsing logic.
12 //===----------------------------------------------------------------------===//
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"
23 Flags memprof_flags_dont_use_directly
; // use via flags().
25 static const char *MaybeUseMemprofDefaultOptionsCompileDefinition() {
26 #ifdef MEMPROF_DEFAULT_OPTIONS
27 return SANITIZER_STRINGIFY(MEMPROF_DEFAULT_OPTIONS
);
33 void Flags::SetDefaults() {
34 #define MEMPROF_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
35 #include "memprof_flags.inc"
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"
46 void InitializeFlags() {
47 // Set the default values and prepare for parsing MemProf and common flags.
48 SetCommonFlagsDefaults();
51 cf
.CopyFrom(*common_flags());
52 cf
.external_symbolizer_path
= GetEnv("MEMPROF_SYMBOLIZER_PATH");
53 cf
.malloc_context_size
= kDefaultMallocContextSize
;
54 cf
.intercept_tls_get_addr
= true;
56 OverrideCommonFlags(cf
);
61 FlagParser memprof_parser
;
62 RegisterMemprofFlags(&memprof_parser
, f
);
63 RegisterCommonFlags(&memprof_parser
);
65 // Override from MemProf compile definition.
66 const char *memprof_compile_def
=
67 MaybeUseMemprofDefaultOptionsCompileDefinition();
68 memprof_parser
.ParseString(memprof_compile_def
);
70 // Override from user-specified string.
71 const char *memprof_default_options
= __memprof_default_options();
72 memprof_parser
.ParseString(memprof_default_options
);
74 // Override from command line.
75 memprof_parser
.ParseStringFromEnv("MEMPROF_OPTIONS");
77 InitializeCommonFlags();
80 ReportUnrecognizedFlags();
82 if (common_flags()->help
) {
83 memprof_parser
.PrintFlagDescriptions();
86 CHECK_LE((uptr
)common_flags()->malloc_context_size
, kStackTraceMax
);
89 } // namespace __memprof
91 SANITIZER_INTERFACE_WEAK_DEF(const char *, __memprof_default_options
, void) {