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 | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame^] | 86 | "backtrace", |
| 87 | {BACKTRACE | TRACK_ALLOCS, &Config::SetBacktrace}, |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 88 | }, |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 89 | { |
| 90 | "backtrace_enable_on_signal", |
| 91 | {BACKTRACE | TRACK_ALLOCS, &Config::SetBacktraceEnableOnSignal}, |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 92 | }, |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 93 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 94 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame^] | 95 | "backtrace_dump_on_exit", |
| 96 | {0, &Config::SetBacktraceDumpOnExit}, |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 97 | }, |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 98 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame^] | 99 | "backtrace_dump_prefix", |
| 100 | {0, &Config::SetBacktraceDumpPrefix}, |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 101 | }, |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 102 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame^] | 103 | "backtrace_full", |
| 104 | {BACKTRACE_FULL, &Config::VerifyValueEmpty}, |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 105 | }, |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 106 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 107 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame^] | 108 | "fill", |
| 109 | {FILL_ON_ALLOC | FILL_ON_FREE, &Config::SetFill}, |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 110 | }, |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 111 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame^] | 112 | "fill_on_alloc", |
| 113 | {FILL_ON_ALLOC, &Config::SetFillOnAlloc}, |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 114 | }, |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 115 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame^] | 116 | "fill_on_free", |
| 117 | {FILL_ON_FREE, &Config::SetFillOnFree}, |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 118 | }, |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 119 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 120 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame^] | 121 | "expand_alloc", |
| 122 | {EXPAND_ALLOC, &Config::SetExpandAlloc}, |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 123 | }, |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 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 | "free_track", |
| 127 | {FREE_TRACK | FILL_ON_FREE | TRACK_ALLOCS, &Config::SetFreeTrack}, |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 128 | }, |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 129 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame^] | 130 | "free_track_backtrace_num_frames", |
| 131 | {0, &Config::SetFreeTrackBacktraceNumFrames}, |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 132 | }, |
| 133 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 134 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame^] | 135 | "leak_track", |
| 136 | {LEAK_TRACK | TRACK_ALLOCS, &Config::VerifyValueEmpty}, |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 137 | }, |
| 138 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 139 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame^] | 140 | "record_allocs", |
| 141 | {RECORD_ALLOCS, &Config::SetRecordAllocs}, |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 142 | }, |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 143 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame^] | 144 | "record_allocs_file", |
| 145 | {0, &Config::SetRecordAllocsFile}, |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 146 | }, |
| 147 | |
| 148 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame^] | 149 | "verify_pointers", |
| 150 | {TRACK_ALLOCS, &Config::VerifyValueEmpty}, |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 151 | }, |
Iris Chang | 7f209a9 | 2019-01-16 11:17:15 +0800 | [diff] [blame] | 152 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame^] | 153 | "abort_on_error", |
| 154 | {ABORT_ON_ERROR, &Config::VerifyValueEmpty}, |
Iris Chang | 7f209a9 | 2019-01-16 11:17:15 +0800 | [diff] [blame] | 155 | }, |
Christopher Ferris | c328e44 | 2019-04-01 19:31:26 -0700 | [diff] [blame] | 156 | { |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame^] | 157 | "verbose", |
| 158 | {VERBOSE, &Config::VerifyValueEmpty}, |
| 159 | }, |
| 160 | { |
| 161 | "check_unreachable_on_signal", |
| 162 | {CHECK_UNREACHABLE_ON_SIGNAL, &Config::VerifyValueEmpty}, |
Christopher Ferris | c328e44 | 2019-04-01 19:31:26 -0700 | [diff] [blame] | 163 | }, |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 164 | }; |
| 165 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 166 | bool Config::ParseValue(const std::string& option, const std::string& value, size_t min_value, |
| 167 | size_t max_value, size_t* parsed_value) const { |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 168 | assert(!value.empty()); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 169 | |
| 170 | // Parse the value into a size_t value. |
| 171 | errno = 0; |
| 172 | char* end; |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 173 | long long_value = strtol(value.c_str(), &end, 10); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 174 | if (errno != 0) { |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 175 | 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] | 176 | return false; |
| 177 | } |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 178 | if (end == value.c_str()) { |
| 179 | error_log("%s: bad value for option '%s'", getprogname(), option.c_str()); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 180 | return false; |
| 181 | } |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 182 | if (static_cast<size_t>(end - value.c_str()) != value.size()) { |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 183 | error_log("%s: bad value for option '%s', non space found after option: %s", getprogname(), |
| 184 | option.c_str(), end); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 185 | return false; |
| 186 | } |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 187 | if (long_value < 0) { |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 188 | error_log("%s: bad value for option '%s', value cannot be negative: %ld", getprogname(), |
| 189 | option.c_str(), long_value); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 190 | return false; |
| 191 | } |
| 192 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 193 | if (static_cast<size_t>(long_value) < min_value) { |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 194 | error_log("%s: bad value for option '%s', value must be >= %zu: %ld", getprogname(), |
| 195 | option.c_str(), min_value, long_value); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 196 | return false; |
| 197 | } |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 198 | if (static_cast<size_t>(long_value) > max_value) { |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 199 | error_log("%s: bad value for option '%s', value must be <= %zu: %ld", getprogname(), |
| 200 | option.c_str(), max_value, long_value); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 201 | return false; |
| 202 | } |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 203 | *parsed_value = static_cast<size_t>(long_value); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 204 | return true; |
| 205 | } |
| 206 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 207 | bool Config::ParseValue(const std::string& option, const std::string& value, size_t default_value, |
| 208 | size_t min_value, size_t max_value, size_t* new_value) const { |
| 209 | if (value.empty()) { |
| 210 | *new_value = default_value; |
| 211 | return true; |
| 212 | } |
| 213 | return ParseValue(option, value, min_value, max_value, new_value); |
| 214 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 215 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 216 | bool Config::SetGuard(const std::string& option, const std::string& value) { |
| 217 | if (value.empty()) { |
| 218 | // Set the defaults. |
| 219 | front_guard_bytes_ = DEFAULT_GUARD_BYTES; |
| 220 | rear_guard_bytes_ = DEFAULT_GUARD_BYTES; |
| 221 | return true; |
| 222 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 223 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 224 | if (!ParseValue(option, value, 1, MAX_GUARD_BYTES, &rear_guard_bytes_)) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 225 | return false; |
| 226 | } |
| 227 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 228 | // It's necessary to align the front guard to MINIMUM_ALIGNMENT_BYTES to |
| 229 | // make sure that the header is aligned properly. |
Dan Albert | a613d0d | 2017-10-05 16:39:33 -0700 | [diff] [blame] | 230 | front_guard_bytes_ = __BIONIC_ALIGN(rear_guard_bytes_, MINIMUM_ALIGNMENT_BYTES); |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 231 | return true; |
| 232 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 233 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 234 | bool Config::SetFrontGuard(const std::string& option, const std::string& value) { |
| 235 | if (!ParseValue(option, value, DEFAULT_GUARD_BYTES, 1, MAX_GUARD_BYTES, &front_guard_bytes_)) { |
| 236 | return false; |
| 237 | } |
| 238 | // It's necessary to align the front guard to MINIMUM_ALIGNMENT_BYTES to |
| 239 | // make sure that the header is aligned properly. |
Dan Albert | a613d0d | 2017-10-05 16:39:33 -0700 | [diff] [blame] | 240 | front_guard_bytes_ = __BIONIC_ALIGN(front_guard_bytes_, MINIMUM_ALIGNMENT_BYTES); |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 241 | return true; |
| 242 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 243 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 244 | bool Config::SetRearGuard(const std::string& option, const std::string& value) { |
| 245 | return ParseValue(option, value, DEFAULT_GUARD_BYTES, 1, MAX_GUARD_BYTES, &rear_guard_bytes_); |
| 246 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 247 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 248 | bool Config::SetFill(const std::string& option, const std::string& value) { |
| 249 | if (value.empty()) { |
| 250 | // Set the defaults. |
| 251 | fill_on_alloc_bytes_ = SIZE_MAX; |
| 252 | fill_on_free_bytes_ = SIZE_MAX; |
| 253 | return true; |
| 254 | } |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 255 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 256 | if (!ParseValue(option, value, 1, SIZE_MAX, &fill_on_alloc_bytes_)) { |
| 257 | return false; |
| 258 | } |
| 259 | fill_on_free_bytes_ = fill_on_alloc_bytes_; |
| 260 | return true; |
| 261 | } |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 262 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 263 | bool Config::SetFillOnAlloc(const std::string& option, const std::string& value) { |
| 264 | return ParseValue(option, value, SIZE_MAX, 1, SIZE_MAX, &fill_on_alloc_bytes_); |
| 265 | } |
| 266 | |
| 267 | bool Config::SetFillOnFree(const std::string& option, const std::string& value) { |
| 268 | return ParseValue(option, value, SIZE_MAX, 1, SIZE_MAX, &fill_on_free_bytes_); |
| 269 | } |
| 270 | |
| 271 | bool Config::SetBacktrace(const std::string& option, const std::string& value) { |
| 272 | backtrace_enabled_ = true; |
| 273 | return ParseValue(option, value, DEFAULT_BACKTRACE_FRAMES, 1, MAX_BACKTRACE_FRAMES, |
| 274 | &backtrace_frames_); |
| 275 | } |
| 276 | |
| 277 | bool Config::SetBacktraceEnableOnSignal(const std::string& option, const std::string& value) { |
| 278 | backtrace_enable_on_signal_ = true; |
| 279 | return ParseValue(option, value, DEFAULT_BACKTRACE_FRAMES, 1, MAX_BACKTRACE_FRAMES, |
| 280 | &backtrace_frames_); |
| 281 | } |
| 282 | |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 283 | bool Config::SetBacktraceDumpOnExit(const std::string& option, const std::string& value) { |
| 284 | if (Config::VerifyValueEmpty(option, value)) { |
| 285 | backtrace_dump_on_exit_ = true; |
| 286 | return true; |
| 287 | } |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | bool Config::SetBacktraceDumpPrefix(const std::string&, const std::string& value) { |
| 292 | if (value.empty()) { |
| 293 | backtrace_dump_prefix_ = DEFAULT_BACKTRACE_DUMP_PREFIX; |
| 294 | } else { |
| 295 | backtrace_dump_prefix_ = value; |
| 296 | } |
| 297 | return true; |
| 298 | } |
| 299 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 300 | bool Config::SetExpandAlloc(const std::string& option, const std::string& value) { |
| 301 | return ParseValue(option, value, DEFAULT_EXPAND_BYTES, 1, MAX_EXPAND_BYTES, &expand_alloc_bytes_); |
| 302 | } |
| 303 | |
| 304 | bool Config::SetFreeTrack(const std::string& option, const std::string& value) { |
| 305 | // This option enables fill on free, so set the bytes to the default value. |
| 306 | if (fill_on_free_bytes_ == 0) { |
| 307 | fill_on_free_bytes_ = SIZE_MAX; |
| 308 | } |
| 309 | if (free_track_backtrace_num_frames_ == 0) { |
| 310 | free_track_backtrace_num_frames_ = DEFAULT_BACKTRACE_FRAMES; |
| 311 | } |
| 312 | |
| 313 | return ParseValue(option, value, DEFAULT_FREE_TRACK_ALLOCATIONS, 1, MAX_FREE_TRACK_ALLOCATIONS, |
| 314 | &free_track_allocations_); |
| 315 | } |
| 316 | |
| 317 | bool Config::SetFreeTrackBacktraceNumFrames(const std::string& option, const std::string& value) { |
| 318 | return ParseValue(option, value, DEFAULT_BACKTRACE_FRAMES, 0, MAX_BACKTRACE_FRAMES, |
| 319 | &free_track_backtrace_num_frames_); |
| 320 | } |
| 321 | |
| 322 | bool Config::SetRecordAllocs(const std::string& option, const std::string& value) { |
| 323 | if (record_allocs_file_.empty()) { |
| 324 | record_allocs_file_ = DEFAULT_RECORD_ALLOCS_FILE; |
| 325 | } |
| 326 | return ParseValue(option, value, DEFAULT_RECORD_ALLOCS, 1, MAX_RECORD_ALLOCS, |
| 327 | &record_allocs_num_entries_); |
| 328 | } |
| 329 | |
| 330 | bool Config::SetRecordAllocsFile(const std::string&, const std::string& value) { |
| 331 | if (value.empty()) { |
| 332 | // Set the default. |
| 333 | record_allocs_file_ = DEFAULT_RECORD_ALLOCS_FILE; |
| 334 | return true; |
| 335 | } |
| 336 | record_allocs_file_ = value; |
| 337 | return true; |
| 338 | } |
| 339 | |
| 340 | bool Config::VerifyValueEmpty(const std::string& option, const std::string& value) { |
| 341 | if (!value.empty()) { |
| 342 | // This is not valid. |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 343 | error_log("%s: value set for option '%s' which does not take a value", getprogname(), |
| 344 | option.c_str()); |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 345 | return false; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 346 | } |
| 347 | return true; |
| 348 | } |
| 349 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 350 | void Config::LogUsage() const { |
Christopher Ferris | 770cbb3 | 2018-05-25 12:46:55 -0700 | [diff] [blame] | 351 | error_log("For malloc debug option descriptions go to:"); |
| 352 | error_log(" https://android.googlesource.com/platform/bionic/+/master/libc/malloc_debug/README.md"); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 355 | bool Config::GetOption(const char** options_str, std::string* option, std::string* value) { |
| 356 | const char* cur = *options_str; |
| 357 | // Process each property name we can find. |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 358 | while (isspace(*cur)) ++cur; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 359 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 360 | if (*cur == '\0') { |
| 361 | *options_str = cur; |
| 362 | return false; |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 365 | const char* start = cur; |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 366 | while (!isspace(*cur) && *cur != '=' && *cur != '\0') ++cur; |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 367 | |
| 368 | *option = std::string(start, cur - start); |
| 369 | |
| 370 | // Skip any spaces after the name. |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 371 | while (isspace(*cur)) ++cur; |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 372 | |
| 373 | value->clear(); |
| 374 | if (*cur == '=') { |
| 375 | ++cur; |
| 376 | // Skip the space after the equal. |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 377 | while (isspace(*cur)) ++cur; |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 378 | |
| 379 | start = cur; |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 380 | while (!isspace(*cur) && *cur != '\0') ++cur; |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 381 | |
| 382 | if (cur != start) { |
| 383 | *value = std::string(start, cur - start); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 384 | } |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 385 | } |
| 386 | *options_str = cur; |
| 387 | return true; |
| 388 | } |
| 389 | |
| 390 | bool Config::Init(const char* options_str) { |
| 391 | // Initialize a few default values. |
| 392 | fill_alloc_value_ = DEFAULT_FILL_ALLOC_VALUE; |
| 393 | fill_free_value_ = DEFAULT_FILL_FREE_VALUE; |
| 394 | front_guard_value_ = DEFAULT_FRONT_GUARD_VALUE; |
| 395 | rear_guard_value_ = DEFAULT_REAR_GUARD_VALUE; |
| 396 | backtrace_signal_ = SIGRTMAX - 19; |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 397 | backtrace_dump_signal_ = SIGRTMAX - 17; |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 398 | record_allocs_signal_ = SIGRTMAX - 18; |
| 399 | free_track_backtrace_num_frames_ = 0; |
| 400 | record_allocs_file_.clear(); |
| 401 | fill_on_free_bytes_ = 0; |
| 402 | backtrace_enable_on_signal_ = false; |
| 403 | backtrace_enabled_ = false; |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 404 | backtrace_dump_on_exit_ = false; |
| 405 | backtrace_dump_prefix_ = DEFAULT_BACKTRACE_DUMP_PREFIX; |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame^] | 406 | check_unreachable_signal_ = SIGRTMAX - 16; |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 407 | |
| 408 | // Process each option name we can find. |
| 409 | std::string option; |
| 410 | std::string value; |
| 411 | bool valid = true; |
| 412 | while (GetOption(&options_str, &option, &value)) { |
| 413 | auto entry = kOptions.find(option); |
| 414 | if (entry == kOptions.end()) { |
| 415 | error_log("%s: unknown option %s", getprogname(), option.c_str()); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 416 | valid = false; |
| 417 | break; |
| 418 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 419 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 420 | const OptionInfo* info = &entry->second; |
| 421 | auto process_func = info->process_func; |
| 422 | if (process_func != nullptr && !(this->*process_func)(option, value)) { |
| 423 | valid = false; |
| 424 | break; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 425 | } |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 426 | options_ |= info->option; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 427 | } |
| 428 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 429 | if (!valid || *options_str != '\0') { |
| 430 | LogUsage(); |
| 431 | return false; |
| 432 | } |
| 433 | |
| 434 | return true; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 435 | } |