Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 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 | |
Elliott Hughes | cbc80ba | 2018-02-13 14:26:29 -0800 | [diff] [blame] | 29 | #pragma once |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 30 | |
| 31 | #include <stdint.h> |
| 32 | |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 33 | #include <string> |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 34 | #include <unordered_map> |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 35 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 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; |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 45 | constexpr uint64_t RECORD_ALLOCS = 0x200; |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 46 | constexpr uint64_t BACKTRACE_FULL = 0x400; |
Iris Chang | 7f209a9 | 2019-01-16 11:17:15 +0800 | [diff] [blame] | 47 | constexpr uint64_t ABORT_ON_ERROR = 0x800; |
Christopher Ferris | c328e44 | 2019-04-01 19:31:26 -0700 | [diff] [blame] | 48 | constexpr uint64_t VERBOSE = 0x1000; |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 49 | constexpr uint64_t CHECK_UNREACHABLE_ON_SIGNAL = 0x2000; |
Christopher Ferris | a383648 | 2022-05-13 12:09:39 -0700 | [diff] [blame] | 50 | constexpr uint64_t BACKTRACE_SPECIFIC_SIZES = 0x4000; |
Christopher Ferris | 5610d5a | 2023-11-14 15:04:50 -0800 | [diff] [blame] | 51 | constexpr uint64_t LOG_ALLOCATOR_STATS_ON_SIGNAL = 0x8000; |
Christopher Ferris | c1341e1 | 2024-07-11 19:48:08 -0700 | [diff] [blame] | 52 | constexpr uint64_t LOG_ALLOCATOR_STATS_ON_EXIT = 0x10000; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 53 | |
Christopher Ferris | 72df670 | 2016-02-11 15:51:31 -0800 | [diff] [blame] | 54 | // In order to guarantee posix compliance, set the minimum alignment |
| 55 | // to 8 bytes for 32 bit systems and 16 bytes for 64 bit systems. |
| 56 | #if defined(__LP64__) |
| 57 | constexpr size_t MINIMUM_ALIGNMENT_BYTES = 16; |
| 58 | #else |
| 59 | constexpr size_t MINIMUM_ALIGNMENT_BYTES = 8; |
| 60 | #endif |
| 61 | |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 62 | // If one or more of these options is set, then a special header is needed. |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 63 | constexpr uint64_t HEADER_OPTIONS = FRONT_GUARD | REAR_GUARD; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 64 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 65 | class Config { |
| 66 | public: |
| 67 | bool Init(const char* options_str); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 68 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 69 | void LogUsage() const; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 70 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 71 | uint64_t options() const { return options_; } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 72 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 73 | int backtrace_signal() const { return backtrace_signal_; } |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 74 | int backtrace_dump_signal() const { return backtrace_dump_signal_; } |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 75 | size_t backtrace_frames() const { return backtrace_frames_; } |
| 76 | size_t backtrace_enabled() const { return backtrace_enabled_; } |
| 77 | size_t backtrace_enable_on_signal() const { return backtrace_enable_on_signal_; } |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 78 | bool backtrace_dump_on_exit() const { return backtrace_dump_on_exit_; } |
| 79 | const std::string& backtrace_dump_prefix() const { return backtrace_dump_prefix_; } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 80 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 81 | size_t front_guard_bytes() const { return front_guard_bytes_; } |
| 82 | size_t rear_guard_bytes() const { return rear_guard_bytes_; } |
| 83 | uint8_t front_guard_value() const { return front_guard_value_; } |
| 84 | uint8_t rear_guard_value() const { return rear_guard_value_; } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 85 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 86 | size_t expand_alloc_bytes() const { return expand_alloc_bytes_; } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 87 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 88 | size_t free_track_allocations() const { return free_track_allocations_; } |
| 89 | size_t free_track_backtrace_num_frames() const { return free_track_backtrace_num_frames_; } |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 90 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 91 | size_t fill_on_alloc_bytes() const { return fill_on_alloc_bytes_; } |
| 92 | size_t fill_on_free_bytes() const { return fill_on_free_bytes_; } |
| 93 | uint8_t fill_alloc_value() const { return fill_alloc_value_; } |
| 94 | uint8_t fill_free_value() const { return fill_free_value_; } |
| 95 | |
Christopher Ferris | a383648 | 2022-05-13 12:09:39 -0700 | [diff] [blame] | 96 | size_t backtrace_min_size_bytes() const { return backtrace_min_size_bytes_; } |
| 97 | size_t backtrace_max_size_bytes() const { return backtrace_max_size_bytes_; } |
| 98 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 99 | int record_allocs_signal() const { return record_allocs_signal_; } |
| 100 | size_t record_allocs_num_entries() const { return record_allocs_num_entries_; } |
| 101 | const std::string& record_allocs_file() const { return record_allocs_file_; } |
Christopher Ferris | 9b88d0a | 2024-06-13 13:22:02 -0700 | [diff] [blame] | 102 | bool record_allocs_on_exit() const { return record_allocs_on_exit_; } |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 103 | |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 104 | int check_unreachable_signal() const { return check_unreachable_signal_; } |
| 105 | |
Christopher Ferris | 5610d5a | 2023-11-14 15:04:50 -0800 | [diff] [blame] | 106 | int log_allocator_stats_signal() const { return log_allocator_stats_signal_; } |
| 107 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 108 | private: |
| 109 | struct OptionInfo { |
| 110 | uint64_t option; |
| 111 | bool (Config::*process_func)(const std::string&, const std::string&); |
| 112 | }; |
| 113 | |
| 114 | bool ParseValue(const std::string& option, const std::string& value, size_t default_value, |
| 115 | size_t min_value, size_t max_value, size_t* new_value) const; |
| 116 | |
| 117 | bool ParseValue(const std::string& option, const std::string& value, size_t min_value, |
| 118 | size_t max_value, size_t* parsed_value) const; |
| 119 | |
| 120 | bool SetGuard(const std::string& option, const std::string& value); |
| 121 | bool SetFrontGuard(const std::string& option, const std::string& value); |
| 122 | bool SetRearGuard(const std::string& option, const std::string& value); |
| 123 | |
| 124 | bool SetFill(const std::string& option, const std::string& value); |
| 125 | bool SetFillOnAlloc(const std::string& option, const std::string& value); |
| 126 | bool SetFillOnFree(const std::string& option, const std::string& value); |
| 127 | |
| 128 | bool SetBacktrace(const std::string& option, const std::string& value); |
| 129 | bool SetBacktraceEnableOnSignal(const std::string& option, const std::string& value); |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 130 | bool SetBacktraceDumpOnExit(const std::string& option, const std::string& value); |
| 131 | bool SetBacktraceDumpPrefix(const std::string& option, const std::string& value); |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 132 | |
Christopher Ferris | a383648 | 2022-05-13 12:09:39 -0700 | [diff] [blame] | 133 | bool SetBacktraceSize(const std::string& option, const std::string& value); |
| 134 | bool SetBacktraceMinSize(const std::string& option, const std::string& value); |
| 135 | bool SetBacktraceMaxSize(const std::string& option, const std::string& value); |
| 136 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 137 | bool SetExpandAlloc(const std::string& option, const std::string& value); |
| 138 | |
| 139 | bool SetFreeTrack(const std::string& option, const std::string& value); |
| 140 | bool SetFreeTrackBacktraceNumFrames(const std::string& option, const std::string& value); |
| 141 | |
| 142 | bool SetRecordAllocs(const std::string& option, const std::string& value); |
| 143 | bool SetRecordAllocsFile(const std::string& option, const std::string& value); |
Christopher Ferris | 9b88d0a | 2024-06-13 13:22:02 -0700 | [diff] [blame] | 144 | bool SetRecordAllocsOnExit(const std::string& option, const std::string& value); |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 145 | |
| 146 | bool VerifyValueEmpty(const std::string& option, const std::string& value); |
| 147 | |
| 148 | static bool GetOption(const char** option_str, std::string* option, std::string* value); |
| 149 | |
| 150 | const static std::unordered_map<std::string, OptionInfo> kOptions; |
| 151 | |
| 152 | size_t front_guard_bytes_ = 0; |
| 153 | size_t rear_guard_bytes_ = 0; |
| 154 | |
| 155 | bool backtrace_enable_on_signal_ = false; |
| 156 | int backtrace_signal_ = 0; |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 157 | int backtrace_dump_signal_ = 0; |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 158 | bool backtrace_enabled_ = false; |
| 159 | size_t backtrace_frames_ = 0; |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 160 | bool backtrace_dump_on_exit_ = false; |
| 161 | std::string backtrace_dump_prefix_; |
Christopher Ferris | a383648 | 2022-05-13 12:09:39 -0700 | [diff] [blame] | 162 | size_t backtrace_min_size_bytes_ = 0; |
| 163 | size_t backtrace_max_size_bytes_ = 0; |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 164 | |
| 165 | size_t fill_on_alloc_bytes_ = 0; |
| 166 | size_t fill_on_free_bytes_ = 0; |
| 167 | |
| 168 | size_t expand_alloc_bytes_ = 0; |
| 169 | |
| 170 | size_t free_track_allocations_ = 0; |
| 171 | size_t free_track_backtrace_num_frames_ = 0; |
| 172 | |
| 173 | int record_allocs_signal_ = 0; |
| 174 | size_t record_allocs_num_entries_ = 0; |
| 175 | std::string record_allocs_file_; |
Christopher Ferris | 9b88d0a | 2024-06-13 13:22:02 -0700 | [diff] [blame] | 176 | bool record_allocs_on_exit_ = false; |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 177 | |
| 178 | uint64_t options_ = 0; |
| 179 | uint8_t fill_alloc_value_; |
| 180 | uint8_t fill_free_value_; |
| 181 | uint8_t front_guard_value_; |
| 182 | uint8_t rear_guard_value_; |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 183 | |
| 184 | int check_unreachable_signal_ = 0; |
Christopher Ferris | 5610d5a | 2023-11-14 15:04:50 -0800 | [diff] [blame] | 185 | int log_allocator_stats_signal_ = 0; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 186 | }; |