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