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 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 29 | #include <assert.h> |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 30 | #include <ctype.h> |
| 31 | #include <errno.h> |
| 32 | #include <limits.h> |
| 33 | #include <signal.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | #include <sys/cdefs.h> |
| 37 | |
| 38 | #include <string> |
| 39 | #include <vector> |
| 40 | |
Josh Gao | 4956c37 | 2019-12-19 16:35:51 -0800 | [diff] [blame] | 41 | #include <platform/bionic/macros.h> |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 42 | |
| 43 | #include "Config.h" |
| 44 | #include "debug_log.h" |
| 45 | |
Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 46 | // Config constants |
| 47 | static constexpr uint8_t DEFAULT_FILL_ALLOC_VALUE = 0xeb; |
| 48 | static constexpr uint8_t DEFAULT_FILL_FREE_VALUE = 0xef; |
| 49 | |
| 50 | static constexpr uint8_t DEFAULT_FRONT_GUARD_VALUE = 0xaa; |
| 51 | static constexpr uint8_t DEFAULT_REAR_GUARD_VALUE = 0xbb; |
| 52 | |
| 53 | // Used as the default for all guard values. |
| 54 | static constexpr size_t DEFAULT_GUARD_BYTES = 32; |
| 55 | static constexpr size_t MAX_GUARD_BYTES = 16384; |
| 56 | |
| 57 | static constexpr size_t DEFAULT_BACKTRACE_FRAMES = 16; |
| 58 | static constexpr size_t MAX_BACKTRACE_FRAMES = 256; |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 59 | static constexpr const char DEFAULT_BACKTRACE_DUMP_PREFIX[] = "/data/local/tmp/backtrace_heap"; |
Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 60 | |
| 61 | static constexpr size_t DEFAULT_EXPAND_BYTES = 16; |
| 62 | static constexpr size_t MAX_EXPAND_BYTES = 16384; |
| 63 | |
| 64 | static constexpr size_t DEFAULT_FREE_TRACK_ALLOCATIONS = 100; |
| 65 | static constexpr size_t MAX_FREE_TRACK_ALLOCATIONS = 16384; |
| 66 | |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 67 | static constexpr size_t DEFAULT_RECORD_ALLOCS = 8000000; |
| 68 | static constexpr size_t MAX_RECORD_ALLOCS = 50000000; |
| 69 | static constexpr const char DEFAULT_RECORD_ALLOCS_FILE[] = "/data/local/tmp/record_allocs.txt"; |
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 | const std::unordered_map<std::string, Config::OptionInfo> Config::kOptions = { |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 72 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 73 | "guard", |
| 74 | {FRONT_GUARD | REAR_GUARD | TRACK_ALLOCS, &Config::SetGuard}, |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 75 | }, |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 76 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 77 | "front_guard", |
| 78 | {FRONT_GUARD | TRACK_ALLOCS, &Config::SetFrontGuard}, |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 79 | }, |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 80 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 81 | "rear_guard", |
| 82 | {REAR_GUARD | TRACK_ALLOCS, &Config::SetRearGuard}, |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 83 | }, |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 84 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 85 | { |
Christopher Ferris | a383648 | 2022-05-13 12:09:39 -0700 | [diff] [blame] | 86 | "backtrace_size", |
| 87 | {BACKTRACE_SPECIFIC_SIZES, &Config::SetBacktraceSize}, |
| 88 | }, |
| 89 | { |
Junjie Hu | 8655d17 | 2023-01-10 18:30:45 +0800 | [diff] [blame] | 90 | "bt_sz", |
| 91 | {BACKTRACE_SPECIFIC_SIZES, &Config::SetBacktraceSize}, |
| 92 | }, |
| 93 | { |
Christopher Ferris | a383648 | 2022-05-13 12:09:39 -0700 | [diff] [blame] | 94 | "backtrace_min_size", |
| 95 | {BACKTRACE_SPECIFIC_SIZES, &Config::SetBacktraceMinSize}, |
| 96 | }, |
| 97 | { |
Junjie Hu | 8655d17 | 2023-01-10 18:30:45 +0800 | [diff] [blame] | 98 | "bt_min_sz", |
| 99 | {BACKTRACE_SPECIFIC_SIZES, &Config::SetBacktraceMinSize}, |
| 100 | }, |
| 101 | { |
Christopher Ferris | a383648 | 2022-05-13 12:09:39 -0700 | [diff] [blame] | 102 | "backtrace_max_size", |
| 103 | {BACKTRACE_SPECIFIC_SIZES, &Config::SetBacktraceMaxSize}, |
| 104 | }, |
Junjie Hu | 8655d17 | 2023-01-10 18:30:45 +0800 | [diff] [blame] | 105 | { |
| 106 | "bt_max_sz", |
| 107 | {BACKTRACE_SPECIFIC_SIZES, &Config::SetBacktraceMaxSize}, |
| 108 | }, |
Christopher Ferris | a383648 | 2022-05-13 12:09:39 -0700 | [diff] [blame] | 109 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 110 | "backtrace", |
| 111 | {BACKTRACE | TRACK_ALLOCS, &Config::SetBacktrace}, |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 112 | }, |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 113 | { |
Junjie Hu | 8655d17 | 2023-01-10 18:30:45 +0800 | [diff] [blame] | 114 | "bt", |
| 115 | {BACKTRACE | TRACK_ALLOCS, &Config::SetBacktrace}, |
| 116 | }, |
| 117 | { |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 118 | "backtrace_enable_on_signal", |
| 119 | {BACKTRACE | TRACK_ALLOCS, &Config::SetBacktraceEnableOnSignal}, |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 120 | }, |
Junjie Hu | 8655d17 | 2023-01-10 18:30:45 +0800 | [diff] [blame] | 121 | { |
| 122 | "bt_en_on_sig", |
| 123 | {BACKTRACE | TRACK_ALLOCS, &Config::SetBacktraceEnableOnSignal}, |
| 124 | }, |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 125 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 126 | "backtrace_dump_on_exit", |
| 127 | {0, &Config::SetBacktraceDumpOnExit}, |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 128 | }, |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 129 | { |
Junjie Hu | 8655d17 | 2023-01-10 18:30:45 +0800 | [diff] [blame] | 130 | "bt_dmp_on_ex", |
| 131 | {0, &Config::SetBacktraceDumpOnExit}, |
| 132 | }, |
| 133 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 134 | "backtrace_dump_prefix", |
| 135 | {0, &Config::SetBacktraceDumpPrefix}, |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 136 | }, |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 137 | { |
Junjie Hu | 8655d17 | 2023-01-10 18:30:45 +0800 | [diff] [blame] | 138 | "bt_dmp_pre", |
| 139 | {0, &Config::SetBacktraceDumpPrefix}, |
| 140 | }, |
| 141 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 142 | "backtrace_full", |
| 143 | {BACKTRACE_FULL, &Config::VerifyValueEmpty}, |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 144 | }, |
Junjie Hu | 8655d17 | 2023-01-10 18:30:45 +0800 | [diff] [blame] | 145 | { |
| 146 | "bt_full", |
| 147 | {BACKTRACE_FULL, &Config::VerifyValueEmpty}, |
| 148 | }, |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 149 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 150 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 151 | "fill", |
| 152 | {FILL_ON_ALLOC | FILL_ON_FREE, &Config::SetFill}, |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 153 | }, |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 154 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 155 | "fill_on_alloc", |
| 156 | {FILL_ON_ALLOC, &Config::SetFillOnAlloc}, |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 157 | }, |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 158 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 159 | "fill_on_free", |
| 160 | {FILL_ON_FREE, &Config::SetFillOnFree}, |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 161 | }, |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 162 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 163 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 164 | "expand_alloc", |
| 165 | {EXPAND_ALLOC, &Config::SetExpandAlloc}, |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 166 | }, |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 167 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 168 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 169 | "free_track", |
| 170 | {FREE_TRACK | FILL_ON_FREE | TRACK_ALLOCS, &Config::SetFreeTrack}, |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 171 | }, |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 172 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 173 | "free_track_backtrace_num_frames", |
| 174 | {0, &Config::SetFreeTrackBacktraceNumFrames}, |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 175 | }, |
| 176 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 177 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 178 | "leak_track", |
| 179 | {LEAK_TRACK | TRACK_ALLOCS, &Config::VerifyValueEmpty}, |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 180 | }, |
| 181 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 182 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 183 | "record_allocs", |
| 184 | {RECORD_ALLOCS, &Config::SetRecordAllocs}, |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 185 | }, |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 186 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 187 | "record_allocs_file", |
| 188 | {0, &Config::SetRecordAllocsFile}, |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 189 | }, |
Christopher Ferris | 9b88d0a | 2024-06-13 13:22:02 -0700 | [diff] [blame] | 190 | { |
| 191 | "record_allocs_on_exit", |
| 192 | {0, &Config::SetRecordAllocsOnExit}, |
| 193 | }, |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 194 | |
| 195 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 196 | "verify_pointers", |
| 197 | {TRACK_ALLOCS, &Config::VerifyValueEmpty}, |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 198 | }, |
Iris Chang | 7f209a9 | 2019-01-16 11:17:15 +0800 | [diff] [blame] | 199 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 200 | "abort_on_error", |
| 201 | {ABORT_ON_ERROR, &Config::VerifyValueEmpty}, |
Iris Chang | 7f209a9 | 2019-01-16 11:17:15 +0800 | [diff] [blame] | 202 | }, |
Christopher Ferris | c328e44 | 2019-04-01 19:31:26 -0700 | [diff] [blame] | 203 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 204 | "verbose", |
| 205 | {VERBOSE, &Config::VerifyValueEmpty}, |
| 206 | }, |
| 207 | { |
| 208 | "check_unreachable_on_signal", |
| 209 | {CHECK_UNREACHABLE_ON_SIGNAL, &Config::VerifyValueEmpty}, |
Christopher Ferris | c328e44 | 2019-04-01 19:31:26 -0700 | [diff] [blame] | 210 | }, |
Christopher Ferris | 5610d5a | 2023-11-14 15:04:50 -0800 | [diff] [blame] | 211 | { |
| 212 | "log_allocator_stats_on_signal", |
| 213 | {LOG_ALLOCATOR_STATS_ON_SIGNAL, &Config::VerifyValueEmpty}, |
| 214 | }, |
Christopher Ferris | c1341e1 | 2024-07-11 19:48:08 -0700 | [diff] [blame^] | 215 | { |
| 216 | "log_allocator_stats_on_exit", |
| 217 | {LOG_ALLOCATOR_STATS_ON_EXIT, &Config::VerifyValueEmpty}, |
| 218 | }, |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 219 | }; |
| 220 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 221 | bool Config::ParseValue(const std::string& option, const std::string& value, size_t min_value, |
| 222 | size_t max_value, size_t* parsed_value) const { |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 223 | assert(!value.empty()); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 224 | |
| 225 | // Parse the value into a size_t value. |
| 226 | errno = 0; |
| 227 | char* end; |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 228 | long long_value = strtol(value.c_str(), &end, 10); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 229 | if (errno != 0) { |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 230 | error_log("%s: bad value for option '%s': %s", getprogname(), option.c_str(), strerror(errno)); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 231 | return false; |
| 232 | } |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 233 | if (end == value.c_str()) { |
| 234 | error_log("%s: bad value for option '%s'", getprogname(), option.c_str()); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 235 | return false; |
| 236 | } |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 237 | if (static_cast<size_t>(end - value.c_str()) != value.size()) { |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 238 | error_log("%s: bad value for option '%s', non space found after option: %s", getprogname(), |
| 239 | option.c_str(), end); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 240 | return false; |
| 241 | } |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 242 | if (long_value < 0) { |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 243 | error_log("%s: bad value for option '%s', value cannot be negative: %ld", getprogname(), |
| 244 | option.c_str(), long_value); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 245 | return false; |
| 246 | } |
| 247 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 248 | if (static_cast<size_t>(long_value) < min_value) { |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 249 | error_log("%s: bad value for option '%s', value must be >= %zu: %ld", getprogname(), |
| 250 | option.c_str(), min_value, long_value); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 251 | return false; |
| 252 | } |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 253 | if (static_cast<size_t>(long_value) > max_value) { |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 254 | error_log("%s: bad value for option '%s', value must be <= %zu: %ld", getprogname(), |
| 255 | option.c_str(), max_value, long_value); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 256 | return false; |
| 257 | } |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 258 | *parsed_value = static_cast<size_t>(long_value); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 259 | return true; |
| 260 | } |
| 261 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 262 | bool Config::ParseValue(const std::string& option, const std::string& value, size_t default_value, |
| 263 | size_t min_value, size_t max_value, size_t* new_value) const { |
| 264 | if (value.empty()) { |
| 265 | *new_value = default_value; |
| 266 | return true; |
| 267 | } |
| 268 | return ParseValue(option, value, min_value, max_value, new_value); |
| 269 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 270 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 271 | bool Config::SetGuard(const std::string& option, const std::string& value) { |
| 272 | if (value.empty()) { |
| 273 | // Set the defaults. |
| 274 | front_guard_bytes_ = DEFAULT_GUARD_BYTES; |
| 275 | rear_guard_bytes_ = DEFAULT_GUARD_BYTES; |
| 276 | return true; |
| 277 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 278 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 279 | if (!ParseValue(option, value, 1, MAX_GUARD_BYTES, &rear_guard_bytes_)) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 280 | return false; |
| 281 | } |
| 282 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 283 | // It's necessary to align the front guard to MINIMUM_ALIGNMENT_BYTES to |
| 284 | // make sure that the header is aligned properly. |
Dan Albert | a613d0d | 2017-10-05 16:39:33 -0700 | [diff] [blame] | 285 | front_guard_bytes_ = __BIONIC_ALIGN(rear_guard_bytes_, MINIMUM_ALIGNMENT_BYTES); |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 286 | return true; |
| 287 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 288 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 289 | bool Config::SetFrontGuard(const std::string& option, const std::string& value) { |
| 290 | if (!ParseValue(option, value, DEFAULT_GUARD_BYTES, 1, MAX_GUARD_BYTES, &front_guard_bytes_)) { |
| 291 | return false; |
| 292 | } |
| 293 | // It's necessary to align the front guard to MINIMUM_ALIGNMENT_BYTES to |
| 294 | // make sure that the header is aligned properly. |
Dan Albert | a613d0d | 2017-10-05 16:39:33 -0700 | [diff] [blame] | 295 | front_guard_bytes_ = __BIONIC_ALIGN(front_guard_bytes_, MINIMUM_ALIGNMENT_BYTES); |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 296 | return true; |
| 297 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 298 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 299 | bool Config::SetRearGuard(const std::string& option, const std::string& value) { |
| 300 | return ParseValue(option, value, DEFAULT_GUARD_BYTES, 1, MAX_GUARD_BYTES, &rear_guard_bytes_); |
| 301 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 302 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 303 | bool Config::SetFill(const std::string& option, const std::string& value) { |
| 304 | if (value.empty()) { |
| 305 | // Set the defaults. |
| 306 | fill_on_alloc_bytes_ = SIZE_MAX; |
| 307 | fill_on_free_bytes_ = SIZE_MAX; |
| 308 | return true; |
| 309 | } |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 310 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 311 | if (!ParseValue(option, value, 1, SIZE_MAX, &fill_on_alloc_bytes_)) { |
| 312 | return false; |
| 313 | } |
| 314 | fill_on_free_bytes_ = fill_on_alloc_bytes_; |
| 315 | return true; |
| 316 | } |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 317 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 318 | bool Config::SetFillOnAlloc(const std::string& option, const std::string& value) { |
| 319 | return ParseValue(option, value, SIZE_MAX, 1, SIZE_MAX, &fill_on_alloc_bytes_); |
| 320 | } |
| 321 | |
| 322 | bool Config::SetFillOnFree(const std::string& option, const std::string& value) { |
| 323 | return ParseValue(option, value, SIZE_MAX, 1, SIZE_MAX, &fill_on_free_bytes_); |
| 324 | } |
| 325 | |
| 326 | bool Config::SetBacktrace(const std::string& option, const std::string& value) { |
| 327 | backtrace_enabled_ = true; |
| 328 | return ParseValue(option, value, DEFAULT_BACKTRACE_FRAMES, 1, MAX_BACKTRACE_FRAMES, |
| 329 | &backtrace_frames_); |
| 330 | } |
| 331 | |
| 332 | bool Config::SetBacktraceEnableOnSignal(const std::string& option, const std::string& value) { |
| 333 | backtrace_enable_on_signal_ = true; |
| 334 | return ParseValue(option, value, DEFAULT_BACKTRACE_FRAMES, 1, MAX_BACKTRACE_FRAMES, |
| 335 | &backtrace_frames_); |
| 336 | } |
| 337 | |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 338 | bool Config::SetBacktraceDumpOnExit(const std::string& option, const std::string& value) { |
| 339 | if (Config::VerifyValueEmpty(option, value)) { |
| 340 | backtrace_dump_on_exit_ = true; |
| 341 | return true; |
| 342 | } |
| 343 | return false; |
| 344 | } |
| 345 | |
| 346 | bool Config::SetBacktraceDumpPrefix(const std::string&, const std::string& value) { |
| 347 | if (value.empty()) { |
| 348 | backtrace_dump_prefix_ = DEFAULT_BACKTRACE_DUMP_PREFIX; |
| 349 | } else { |
| 350 | backtrace_dump_prefix_ = value; |
| 351 | } |
| 352 | return true; |
| 353 | } |
| 354 | |
Christopher Ferris | a383648 | 2022-05-13 12:09:39 -0700 | [diff] [blame] | 355 | bool Config::SetBacktraceSize(const std::string& option, const std::string& value) { |
| 356 | if (!ParseValue(option, value, 1, SIZE_MAX, &backtrace_min_size_bytes_)) { |
| 357 | return false; |
| 358 | } |
| 359 | backtrace_max_size_bytes_ = backtrace_min_size_bytes_; |
| 360 | |
| 361 | return true; |
| 362 | } |
| 363 | |
| 364 | bool Config::SetBacktraceMinSize(const std::string& option, const std::string& value) { |
| 365 | return ParseValue(option, value, 1, SIZE_MAX, &backtrace_min_size_bytes_); |
| 366 | } |
| 367 | |
| 368 | bool Config::SetBacktraceMaxSize(const std::string& option, const std::string& value) { |
| 369 | return ParseValue(option, value, 1, SIZE_MAX, &backtrace_max_size_bytes_); |
| 370 | } |
| 371 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 372 | bool Config::SetExpandAlloc(const std::string& option, const std::string& value) { |
| 373 | return ParseValue(option, value, DEFAULT_EXPAND_BYTES, 1, MAX_EXPAND_BYTES, &expand_alloc_bytes_); |
| 374 | } |
| 375 | |
| 376 | bool Config::SetFreeTrack(const std::string& option, const std::string& value) { |
| 377 | // This option enables fill on free, so set the bytes to the default value. |
| 378 | if (fill_on_free_bytes_ == 0) { |
| 379 | fill_on_free_bytes_ = SIZE_MAX; |
| 380 | } |
| 381 | if (free_track_backtrace_num_frames_ == 0) { |
| 382 | free_track_backtrace_num_frames_ = DEFAULT_BACKTRACE_FRAMES; |
| 383 | } |
| 384 | |
| 385 | return ParseValue(option, value, DEFAULT_FREE_TRACK_ALLOCATIONS, 1, MAX_FREE_TRACK_ALLOCATIONS, |
| 386 | &free_track_allocations_); |
| 387 | } |
| 388 | |
| 389 | bool Config::SetFreeTrackBacktraceNumFrames(const std::string& option, const std::string& value) { |
| 390 | return ParseValue(option, value, DEFAULT_BACKTRACE_FRAMES, 0, MAX_BACKTRACE_FRAMES, |
| 391 | &free_track_backtrace_num_frames_); |
| 392 | } |
| 393 | |
| 394 | bool Config::SetRecordAllocs(const std::string& option, const std::string& value) { |
| 395 | if (record_allocs_file_.empty()) { |
| 396 | record_allocs_file_ = DEFAULT_RECORD_ALLOCS_FILE; |
| 397 | } |
| 398 | return ParseValue(option, value, DEFAULT_RECORD_ALLOCS, 1, MAX_RECORD_ALLOCS, |
| 399 | &record_allocs_num_entries_); |
| 400 | } |
| 401 | |
| 402 | bool Config::SetRecordAllocsFile(const std::string&, const std::string& value) { |
| 403 | if (value.empty()) { |
| 404 | // Set the default. |
| 405 | record_allocs_file_ = DEFAULT_RECORD_ALLOCS_FILE; |
| 406 | return true; |
| 407 | } |
| 408 | record_allocs_file_ = value; |
| 409 | return true; |
| 410 | } |
| 411 | |
Christopher Ferris | 9b88d0a | 2024-06-13 13:22:02 -0700 | [diff] [blame] | 412 | bool Config::SetRecordAllocsOnExit(const std::string& option, const std::string& value) { |
| 413 | if (Config::VerifyValueEmpty(option, value)) { |
| 414 | record_allocs_on_exit_ = true; |
| 415 | return true; |
| 416 | } |
| 417 | return false; |
| 418 | } |
| 419 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 420 | bool Config::VerifyValueEmpty(const std::string& option, const std::string& value) { |
| 421 | if (!value.empty()) { |
| 422 | // This is not valid. |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 423 | error_log("%s: value set for option '%s' which does not take a value", getprogname(), |
| 424 | option.c_str()); |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 425 | return false; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 426 | } |
| 427 | return true; |
| 428 | } |
| 429 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 430 | void Config::LogUsage() const { |
Christopher Ferris | 770cbb3 | 2018-05-25 12:46:55 -0700 | [diff] [blame] | 431 | error_log("For malloc debug option descriptions go to:"); |
Elliott Hughes | 9c06d16 | 2023-10-04 23:36:14 +0000 | [diff] [blame] | 432 | error_log( |
| 433 | " https://android.googlesource.com/platform/bionic/+/main/libc/malloc_debug/README.md"); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 434 | } |
| 435 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 436 | bool Config::GetOption(const char** options_str, std::string* option, std::string* value) { |
| 437 | const char* cur = *options_str; |
| 438 | // Process each property name we can find. |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 439 | while (isspace(*cur)) ++cur; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 440 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 441 | if (*cur == '\0') { |
| 442 | *options_str = cur; |
| 443 | return false; |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 446 | const char* start = cur; |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 447 | while (!isspace(*cur) && *cur != '=' && *cur != '\0') ++cur; |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 448 | |
| 449 | *option = std::string(start, cur - start); |
| 450 | |
| 451 | // Skip any spaces after the name. |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 452 | while (isspace(*cur)) ++cur; |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 453 | |
| 454 | value->clear(); |
| 455 | if (*cur == '=') { |
| 456 | ++cur; |
| 457 | // Skip the space after the equal. |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 458 | while (isspace(*cur)) ++cur; |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 459 | |
| 460 | start = cur; |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 461 | while (!isspace(*cur) && *cur != '\0') ++cur; |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 462 | |
| 463 | if (cur != start) { |
| 464 | *value = std::string(start, cur - start); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 465 | } |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 466 | } |
| 467 | *options_str = cur; |
| 468 | return true; |
| 469 | } |
| 470 | |
| 471 | bool Config::Init(const char* options_str) { |
| 472 | // Initialize a few default values. |
| 473 | fill_alloc_value_ = DEFAULT_FILL_ALLOC_VALUE; |
| 474 | fill_free_value_ = DEFAULT_FILL_FREE_VALUE; |
| 475 | front_guard_value_ = DEFAULT_FRONT_GUARD_VALUE; |
| 476 | rear_guard_value_ = DEFAULT_REAR_GUARD_VALUE; |
| 477 | backtrace_signal_ = SIGRTMAX - 19; |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 478 | backtrace_dump_signal_ = SIGRTMAX - 17; |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 479 | record_allocs_signal_ = SIGRTMAX - 18; |
| 480 | free_track_backtrace_num_frames_ = 0; |
| 481 | record_allocs_file_.clear(); |
| 482 | fill_on_free_bytes_ = 0; |
| 483 | backtrace_enable_on_signal_ = false; |
| 484 | backtrace_enabled_ = false; |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 485 | backtrace_dump_on_exit_ = false; |
| 486 | backtrace_dump_prefix_ = DEFAULT_BACKTRACE_DUMP_PREFIX; |
Christopher Ferris | a383648 | 2022-05-13 12:09:39 -0700 | [diff] [blame] | 487 | backtrace_min_size_bytes_ = 0; |
| 488 | backtrace_max_size_bytes_ = SIZE_MAX; |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 489 | check_unreachable_signal_ = SIGRTMAX - 16; |
Christopher Ferris | 5610d5a | 2023-11-14 15:04:50 -0800 | [diff] [blame] | 490 | log_allocator_stats_signal_ = SIGRTMAX - 15; |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 491 | |
| 492 | // Process each option name we can find. |
| 493 | std::string option; |
| 494 | std::string value; |
| 495 | bool valid = true; |
| 496 | while (GetOption(&options_str, &option, &value)) { |
| 497 | auto entry = kOptions.find(option); |
| 498 | if (entry == kOptions.end()) { |
| 499 | error_log("%s: unknown option %s", getprogname(), option.c_str()); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 500 | valid = false; |
| 501 | break; |
| 502 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 503 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 504 | const OptionInfo* info = &entry->second; |
| 505 | auto process_func = info->process_func; |
| 506 | if (process_func != nullptr && !(this->*process_func)(option, value)) { |
| 507 | valid = false; |
| 508 | break; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 509 | } |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 510 | options_ |= info->option; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 511 | } |
| 512 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 513 | if (!valid || *options_str != '\0') { |
| 514 | LogUsage(); |
| 515 | return false; |
| 516 | } |
| 517 | |
| 518 | return true; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 519 | } |