• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #pragma once
30 
31 #include <stdint.h>
32 
33 #include <string>
34 #include <unordered_map>
35 
36 constexpr uint64_t FRONT_GUARD = 0x1;
37 constexpr uint64_t REAR_GUARD = 0x2;
38 constexpr uint64_t BACKTRACE = 0x4;
39 constexpr uint64_t FILL_ON_ALLOC = 0x8;
40 constexpr uint64_t FILL_ON_FREE = 0x10;
41 constexpr uint64_t EXPAND_ALLOC = 0x20;
42 constexpr uint64_t FREE_TRACK = 0x40;
43 constexpr uint64_t TRACK_ALLOCS = 0x80;
44 constexpr uint64_t LEAK_TRACK = 0x100;
45 constexpr uint64_t RECORD_ALLOCS = 0x200;
46 constexpr uint64_t BACKTRACE_FULL = 0x400;
47 constexpr uint64_t ABORT_ON_ERROR = 0x800;
48 constexpr uint64_t VERBOSE = 0x1000;
49 constexpr uint64_t CHECK_UNREACHABLE_ON_SIGNAL = 0x2000;
50 constexpr uint64_t BACKTRACE_SPECIFIC_SIZES = 0x4000;
51 constexpr uint64_t LOG_ALLOCATOR_STATS_ON_SIGNAL = 0x8000;
52 
53 // In order to guarantee posix compliance, set the minimum alignment
54 // to 8 bytes for 32 bit systems and 16 bytes for 64 bit systems.
55 #if defined(__LP64__)
56 constexpr size_t MINIMUM_ALIGNMENT_BYTES = 16;
57 #else
58 constexpr size_t MINIMUM_ALIGNMENT_BYTES = 8;
59 #endif
60 
61 // If one or more of these options is set, then a special header is needed.
62 constexpr uint64_t HEADER_OPTIONS = FRONT_GUARD | REAR_GUARD;
63 
64 class Config {
65  public:
66   bool Init(const char* options_str);
67 
68   void LogUsage() const;
69 
options()70   uint64_t options() const { return options_; }
71 
backtrace_signal()72   int backtrace_signal() const { return backtrace_signal_; }
backtrace_dump_signal()73   int backtrace_dump_signal() const { return backtrace_dump_signal_; }
backtrace_frames()74   size_t backtrace_frames() const { return backtrace_frames_; }
backtrace_enabled()75   size_t backtrace_enabled() const { return backtrace_enabled_; }
backtrace_enable_on_signal()76   size_t backtrace_enable_on_signal() const { return backtrace_enable_on_signal_; }
backtrace_dump_on_exit()77   bool backtrace_dump_on_exit() const { return backtrace_dump_on_exit_; }
backtrace_dump_prefix()78   const std::string& backtrace_dump_prefix() const { return backtrace_dump_prefix_; }
79 
front_guard_bytes()80   size_t front_guard_bytes() const { return front_guard_bytes_; }
rear_guard_bytes()81   size_t rear_guard_bytes() const { return rear_guard_bytes_; }
front_guard_value()82   uint8_t front_guard_value() const { return front_guard_value_; }
rear_guard_value()83   uint8_t rear_guard_value() const { return rear_guard_value_; }
84 
expand_alloc_bytes()85   size_t expand_alloc_bytes() const { return expand_alloc_bytes_; }
86 
free_track_allocations()87   size_t free_track_allocations() const { return free_track_allocations_; }
free_track_backtrace_num_frames()88   size_t free_track_backtrace_num_frames() const { return free_track_backtrace_num_frames_; }
89 
fill_on_alloc_bytes()90   size_t fill_on_alloc_bytes() const { return fill_on_alloc_bytes_; }
fill_on_free_bytes()91   size_t fill_on_free_bytes() const { return fill_on_free_bytes_; }
fill_alloc_value()92   uint8_t fill_alloc_value() const { return fill_alloc_value_; }
fill_free_value()93   uint8_t fill_free_value() const { return fill_free_value_; }
94 
backtrace_min_size_bytes()95   size_t backtrace_min_size_bytes() const { return backtrace_min_size_bytes_; }
backtrace_max_size_bytes()96   size_t backtrace_max_size_bytes() const { return backtrace_max_size_bytes_; }
97 
record_allocs_signal()98   int record_allocs_signal() const { return record_allocs_signal_; }
record_allocs_num_entries()99   size_t record_allocs_num_entries() const { return record_allocs_num_entries_; }
record_allocs_file()100   const std::string& record_allocs_file() const { return record_allocs_file_; }
101 
check_unreachable_signal()102   int check_unreachable_signal() const { return check_unreachable_signal_; }
103 
log_allocator_stats_signal()104   int log_allocator_stats_signal() const { return log_allocator_stats_signal_; }
105 
106  private:
107   struct OptionInfo {
108     uint64_t option;
109     bool (Config::*process_func)(const std::string&, const std::string&);
110   };
111 
112   bool ParseValue(const std::string& option, const std::string& value, size_t default_value,
113                   size_t min_value, size_t max_value, size_t* new_value) const;
114 
115   bool ParseValue(const std::string& option, const std::string& value, size_t min_value,
116                   size_t max_value, size_t* parsed_value) const;
117 
118   bool SetGuard(const std::string& option, const std::string& value);
119   bool SetFrontGuard(const std::string& option, const std::string& value);
120   bool SetRearGuard(const std::string& option, const std::string& value);
121 
122   bool SetFill(const std::string& option, const std::string& value);
123   bool SetFillOnAlloc(const std::string& option, const std::string& value);
124   bool SetFillOnFree(const std::string& option, const std::string& value);
125 
126   bool SetBacktrace(const std::string& option, const std::string& value);
127   bool SetBacktraceEnableOnSignal(const std::string& option, const std::string& value);
128   bool SetBacktraceDumpOnExit(const std::string& option, const std::string& value);
129   bool SetBacktraceDumpPrefix(const std::string& option, const std::string& value);
130 
131   bool SetBacktraceSize(const std::string& option, const std::string& value);
132   bool SetBacktraceMinSize(const std::string& option, const std::string& value);
133   bool SetBacktraceMaxSize(const std::string& option, const std::string& value);
134 
135   bool SetExpandAlloc(const std::string& option, const std::string& value);
136 
137   bool SetFreeTrack(const std::string& option, const std::string& value);
138   bool SetFreeTrackBacktraceNumFrames(const std::string& option, const std::string& value);
139 
140   bool SetRecordAllocs(const std::string& option, const std::string& value);
141   bool SetRecordAllocsFile(const std::string& option, const std::string& value);
142 
143   bool VerifyValueEmpty(const std::string& option, const std::string& value);
144 
145   static bool GetOption(const char** option_str, std::string* option, std::string* value);
146 
147   const static std::unordered_map<std::string, OptionInfo> kOptions;
148 
149   size_t front_guard_bytes_ = 0;
150   size_t rear_guard_bytes_ = 0;
151 
152   bool backtrace_enable_on_signal_ = false;
153   int backtrace_signal_ = 0;
154   int backtrace_dump_signal_ = 0;
155   bool backtrace_enabled_ = false;
156   size_t backtrace_frames_ = 0;
157   bool backtrace_dump_on_exit_ = false;
158   std::string backtrace_dump_prefix_;
159   size_t backtrace_min_size_bytes_ = 0;
160   size_t backtrace_max_size_bytes_ = 0;
161 
162   size_t fill_on_alloc_bytes_ = 0;
163   size_t fill_on_free_bytes_ = 0;
164 
165   size_t expand_alloc_bytes_ = 0;
166 
167   size_t free_track_allocations_ = 0;
168   size_t free_track_backtrace_num_frames_ = 0;
169 
170   int record_allocs_signal_ = 0;
171   size_t record_allocs_num_entries_ = 0;
172   std::string record_allocs_file_;
173 
174   uint64_t options_ = 0;
175   uint8_t fill_alloc_value_;
176   uint8_t fill_free_value_;
177   uint8_t front_guard_value_;
178   uint8_t rear_guard_value_;
179 
180   int check_unreachable_signal_ = 0;
181   int log_allocator_stats_signal_ = 0;
182 };
183