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 = { |
| 72 | {"guard", |
| 73 | {FRONT_GUARD | REAR_GUARD, &Config::SetGuard}, |
| 74 | }, |
| 75 | {"front_guard", |
| 76 | {FRONT_GUARD, &Config::SetFrontGuard}, |
| 77 | }, |
| 78 | {"rear_guard", |
| 79 | {REAR_GUARD, &Config::SetRearGuard}, |
| 80 | }, |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 81 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 82 | {"backtrace", |
| 83 | {BACKTRACE | TRACK_ALLOCS, &Config::SetBacktrace}, |
| 84 | }, |
| 85 | {"backtrace_enable_on_signal", |
| 86 | {BACKTRACE | TRACK_ALLOCS, &Config::SetBacktraceEnableOnSignal}, |
| 87 | }, |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 88 | |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 89 | {"backtrace_dump_on_exit", |
| 90 | {0, &Config::SetBacktraceDumpOnExit}, |
| 91 | }, |
| 92 | {"backtrace_dump_prefix", |
| 93 | {0, &Config::SetBacktraceDumpPrefix}, |
| 94 | }, |
| 95 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 96 | {"fill", |
| 97 | {FILL_ON_ALLOC | FILL_ON_FREE, &Config::SetFill}, |
| 98 | }, |
| 99 | {"fill_on_alloc", |
| 100 | {FILL_ON_ALLOC, &Config::SetFillOnAlloc}, |
| 101 | }, |
| 102 | {"fill_on_free", |
| 103 | {FILL_ON_FREE, &Config::SetFillOnFree}, |
| 104 | }, |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 105 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 106 | {"expand_alloc", |
| 107 | {EXPAND_ALLOC, &Config::SetExpandAlloc}, |
| 108 | }, |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 109 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 110 | {"free_track", |
| 111 | {FREE_TRACK | FILL_ON_FREE, &Config::SetFreeTrack}, |
| 112 | }, |
| 113 | {"free_track_backtrace_num_frames", |
| 114 | {0, &Config::SetFreeTrackBacktraceNumFrames}, |
| 115 | }, |
| 116 | |
| 117 | {"leak_track", |
| 118 | {LEAK_TRACK | TRACK_ALLOCS, &Config::VerifyValueEmpty}, |
| 119 | }, |
| 120 | |
| 121 | {"record_allocs", |
| 122 | {RECORD_ALLOCS, &Config::SetRecordAllocs}, |
| 123 | }, |
| 124 | {"record_allocs_file", |
| 125 | {0, &Config::SetRecordAllocsFile}, |
| 126 | }, |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 127 | }; |
| 128 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 129 | bool Config::ParseValue(const std::string& option, const std::string& value, |
| 130 | size_t min_value, size_t max_value, size_t* parsed_value) const { |
| 131 | assert(!value.empty()); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 132 | |
| 133 | // Parse the value into a size_t value. |
| 134 | errno = 0; |
| 135 | char* end; |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 136 | long long_value = strtol(value.c_str(), &end, 10); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 137 | if (errno != 0) { |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 138 | error_log("%s: bad value for option '%s': %s", getprogname(), option.c_str(), |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 139 | strerror(errno)); |
| 140 | return false; |
| 141 | } |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 142 | if (end == value.c_str()) { |
| 143 | error_log("%s: bad value for option '%s'", getprogname(), option.c_str()); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 144 | return false; |
| 145 | } |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 146 | if (static_cast<size_t>(end - value.c_str()) != value.size()) { |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 147 | error_log("%s: bad value for option '%s', non space found after option: %s", |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 148 | getprogname(), option.c_str(), end); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 149 | return false; |
| 150 | } |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 151 | if (long_value < 0) { |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 152 | error_log("%s: bad value for option '%s', value cannot be negative: %ld", |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 153 | getprogname(), option.c_str(), long_value); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 154 | return false; |
| 155 | } |
| 156 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 157 | if (static_cast<size_t>(long_value) < min_value) { |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 158 | error_log("%s: bad value for option '%s', value must be >= %zu: %ld", |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 159 | getprogname(), option.c_str(), min_value, long_value); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 160 | return false; |
| 161 | } |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 162 | if (static_cast<size_t>(long_value) > max_value) { |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 163 | error_log("%s: bad value for option '%s', value must be <= %zu: %ld", |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 164 | getprogname(), option.c_str(), max_value, long_value); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 165 | return false; |
| 166 | } |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 167 | *parsed_value = static_cast<size_t>(long_value); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 168 | return true; |
| 169 | } |
| 170 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 171 | bool Config::ParseValue(const std::string& option, const std::string& value, size_t default_value, |
| 172 | size_t min_value, size_t max_value, size_t* new_value) const { |
| 173 | if (value.empty()) { |
| 174 | *new_value = default_value; |
| 175 | return true; |
| 176 | } |
| 177 | return ParseValue(option, value, min_value, max_value, new_value); |
| 178 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 179 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 180 | bool Config::SetGuard(const std::string& option, const std::string& value) { |
| 181 | if (value.empty()) { |
| 182 | // Set the defaults. |
| 183 | front_guard_bytes_ = DEFAULT_GUARD_BYTES; |
| 184 | rear_guard_bytes_ = DEFAULT_GUARD_BYTES; |
| 185 | return true; |
| 186 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 187 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 188 | if (!ParseValue(option, value, 1, MAX_GUARD_BYTES, &rear_guard_bytes_)) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 189 | return false; |
| 190 | } |
| 191 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 192 | // It's necessary to align the front guard to MINIMUM_ALIGNMENT_BYTES to |
| 193 | // make sure that the header is aligned properly. |
Dan Albert | a613d0d | 2017-10-05 16:39:33 -0700 | [diff] [blame^] | 194 | front_guard_bytes_ = __BIONIC_ALIGN(rear_guard_bytes_, MINIMUM_ALIGNMENT_BYTES); |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 195 | return true; |
| 196 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 197 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 198 | bool Config::SetFrontGuard(const std::string& option, const std::string& value) { |
| 199 | if (!ParseValue(option, value, DEFAULT_GUARD_BYTES, 1, MAX_GUARD_BYTES, &front_guard_bytes_)) { |
| 200 | return false; |
| 201 | } |
| 202 | // It's necessary to align the front guard to MINIMUM_ALIGNMENT_BYTES to |
| 203 | // make sure that the header is aligned properly. |
Dan Albert | a613d0d | 2017-10-05 16:39:33 -0700 | [diff] [blame^] | 204 | front_guard_bytes_ = __BIONIC_ALIGN(front_guard_bytes_, MINIMUM_ALIGNMENT_BYTES); |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 205 | return true; |
| 206 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 207 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 208 | bool Config::SetRearGuard(const std::string& option, const std::string& value) { |
| 209 | return ParseValue(option, value, DEFAULT_GUARD_BYTES, 1, MAX_GUARD_BYTES, &rear_guard_bytes_); |
| 210 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 211 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 212 | bool Config::SetFill(const std::string& option, const std::string& value) { |
| 213 | if (value.empty()) { |
| 214 | // Set the defaults. |
| 215 | fill_on_alloc_bytes_ = SIZE_MAX; |
| 216 | fill_on_free_bytes_ = SIZE_MAX; |
| 217 | return true; |
| 218 | } |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 219 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 220 | if (!ParseValue(option, value, 1, SIZE_MAX, &fill_on_alloc_bytes_)) { |
| 221 | return false; |
| 222 | } |
| 223 | fill_on_free_bytes_ = fill_on_alloc_bytes_; |
| 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 | bool Config::SetFillOnAlloc(const std::string& option, const std::string& value) { |
| 228 | return ParseValue(option, value, SIZE_MAX, 1, SIZE_MAX, &fill_on_alloc_bytes_); |
| 229 | } |
| 230 | |
| 231 | bool Config::SetFillOnFree(const std::string& option, const std::string& value) { |
| 232 | return ParseValue(option, value, SIZE_MAX, 1, SIZE_MAX, &fill_on_free_bytes_); |
| 233 | } |
| 234 | |
| 235 | bool Config::SetBacktrace(const std::string& option, const std::string& value) { |
| 236 | backtrace_enabled_ = true; |
| 237 | return ParseValue(option, value, DEFAULT_BACKTRACE_FRAMES, 1, MAX_BACKTRACE_FRAMES, |
| 238 | &backtrace_frames_); |
| 239 | } |
| 240 | |
| 241 | bool Config::SetBacktraceEnableOnSignal(const std::string& option, const std::string& value) { |
| 242 | backtrace_enable_on_signal_ = true; |
| 243 | return ParseValue(option, value, DEFAULT_BACKTRACE_FRAMES, 1, MAX_BACKTRACE_FRAMES, |
| 244 | &backtrace_frames_); |
| 245 | } |
| 246 | |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 247 | bool Config::SetBacktraceDumpOnExit(const std::string& option, const std::string& value) { |
| 248 | if (Config::VerifyValueEmpty(option, value)) { |
| 249 | backtrace_dump_on_exit_ = true; |
| 250 | return true; |
| 251 | } |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | bool Config::SetBacktraceDumpPrefix(const std::string&, const std::string& value) { |
| 256 | if (value.empty()) { |
| 257 | backtrace_dump_prefix_ = DEFAULT_BACKTRACE_DUMP_PREFIX; |
| 258 | } else { |
| 259 | backtrace_dump_prefix_ = value; |
| 260 | } |
| 261 | return true; |
| 262 | } |
| 263 | |
| 264 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 265 | bool Config::SetExpandAlloc(const std::string& option, const std::string& value) { |
| 266 | return ParseValue(option, value, DEFAULT_EXPAND_BYTES, 1, MAX_EXPAND_BYTES, &expand_alloc_bytes_); |
| 267 | } |
| 268 | |
| 269 | bool Config::SetFreeTrack(const std::string& option, const std::string& value) { |
| 270 | // This option enables fill on free, so set the bytes to the default value. |
| 271 | if (fill_on_free_bytes_ == 0) { |
| 272 | fill_on_free_bytes_ = SIZE_MAX; |
| 273 | } |
| 274 | if (free_track_backtrace_num_frames_ == 0) { |
| 275 | free_track_backtrace_num_frames_ = DEFAULT_BACKTRACE_FRAMES; |
| 276 | } |
| 277 | |
| 278 | return ParseValue(option, value, DEFAULT_FREE_TRACK_ALLOCATIONS, 1, MAX_FREE_TRACK_ALLOCATIONS, |
| 279 | &free_track_allocations_); |
| 280 | } |
| 281 | |
| 282 | bool Config::SetFreeTrackBacktraceNumFrames(const std::string& option, const std::string& value) { |
| 283 | return ParseValue(option, value, DEFAULT_BACKTRACE_FRAMES, 0, MAX_BACKTRACE_FRAMES, |
| 284 | &free_track_backtrace_num_frames_); |
| 285 | } |
| 286 | |
| 287 | bool Config::SetRecordAllocs(const std::string& option, const std::string& value) { |
| 288 | if (record_allocs_file_.empty()) { |
| 289 | record_allocs_file_ = DEFAULT_RECORD_ALLOCS_FILE; |
| 290 | } |
| 291 | return ParseValue(option, value, DEFAULT_RECORD_ALLOCS, 1, MAX_RECORD_ALLOCS, |
| 292 | &record_allocs_num_entries_); |
| 293 | } |
| 294 | |
| 295 | bool Config::SetRecordAllocsFile(const std::string&, const std::string& value) { |
| 296 | if (value.empty()) { |
| 297 | // Set the default. |
| 298 | record_allocs_file_ = DEFAULT_RECORD_ALLOCS_FILE; |
| 299 | return true; |
| 300 | } |
| 301 | record_allocs_file_ = value; |
| 302 | return true; |
| 303 | } |
| 304 | |
| 305 | bool Config::VerifyValueEmpty(const std::string& option, const std::string& value) { |
| 306 | if (!value.empty()) { |
| 307 | // This is not valid. |
| 308 | error_log("%s: value set for option '%s' which does not take a value", |
| 309 | getprogname(), option.c_str()); |
| 310 | return false; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 311 | } |
| 312 | return true; |
| 313 | } |
| 314 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 315 | |
| 316 | void Config::LogUsage() const { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 317 | error_log("malloc debug options usage:"); |
| 318 | error_log(""); |
| 319 | error_log(" front_guard[=XX]"); |
| 320 | error_log(" Enables a front guard on all allocations. If XX is set"); |
| 321 | error_log(" it sets the number of bytes in the guard. The default is"); |
Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 322 | error_log(" %zu bytes, the max bytes is %zu.", DEFAULT_GUARD_BYTES, MAX_GUARD_BYTES); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 323 | error_log(""); |
| 324 | error_log(" rear_guard[=XX]"); |
| 325 | error_log(" Enables a rear guard on all allocations. If XX is set"); |
| 326 | error_log(" it sets the number of bytes in the guard. The default is"); |
Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 327 | error_log(" %zu bytes, the max bytes is %zu.", DEFAULT_GUARD_BYTES, MAX_GUARD_BYTES); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 328 | error_log(""); |
| 329 | error_log(" guard[=XX]"); |
| 330 | error_log(" Enables both a front guard and a rear guard on all allocations."); |
| 331 | error_log(" If XX is set it sets the number of bytes in both guards."); |
Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 332 | error_log(" The default is %zu bytes, the max bytes is %zu.", |
| 333 | DEFAULT_GUARD_BYTES, MAX_GUARD_BYTES); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 334 | error_log(""); |
| 335 | error_log(" backtrace[=XX]"); |
| 336 | error_log(" Enable capturing the backtrace at the point of allocation."); |
| 337 | error_log(" If XX is set it sets the number of backtrace frames."); |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 338 | error_log(" This option also enables dumping the backtrace heap data"); |
| 339 | error_log(" when a signal is received. The data is dumped to the file"); |
| 340 | error_log(" backtrace_dump_prefix.<PID>.txt."); |
Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 341 | error_log(" The default is %zu frames, the max number of frames is %zu.", |
| 342 | DEFAULT_BACKTRACE_FRAMES, MAX_BACKTRACE_FRAMES); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 343 | error_log(""); |
| 344 | error_log(" backtrace_enable_on_signal[=XX]"); |
| 345 | error_log(" Enable capturing the backtrace at the point of allocation."); |
| 346 | error_log(" The backtrace capture is not enabled until the process"); |
| 347 | error_log(" receives a signal. If XX is set it sets the number of backtrace"); |
Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 348 | error_log(" frames. The default is %zu frames, the max number of frames is %zu.", |
| 349 | DEFAULT_BACKTRACE_FRAMES, MAX_BACKTRACE_FRAMES); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 350 | error_log(""); |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 351 | error_log(" backtrace_dump_prefix[=FILE]"); |
| 352 | error_log(" This option only has meaning if the backtrace option has been specified."); |
| 353 | error_log(" This is the prefix of the name of the file to which backtrace heap"); |
| 354 | error_log(" data will be dumped. The file will be named backtrace_dump_prefix.<PID>.txt."); |
| 355 | error_log(" The default is %s.", DEFAULT_BACKTRACE_DUMP_PREFIX); |
| 356 | |
| 357 | error_log(""); |
| 358 | error_log(" backtrace_dump_on_exit"); |
| 359 | error_log(" This option only has meaning if the backtrace option has been specified."); |
| 360 | error_log(" This will cause all live allocations to be dumped to the file"); |
| 361 | error_log(" backtrace_dump_prefix.<PID>.final.txt."); |
| 362 | error_log(" The default is false."); |
| 363 | error_log(""); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 364 | error_log(" fill_on_alloc[=XX]"); |
| 365 | error_log(" On first allocation, fill with the value 0x%02x.", DEFAULT_FILL_ALLOC_VALUE); |
| 366 | error_log(" If XX is set it will only fill up to XX bytes of the"); |
| 367 | error_log(" allocation. The default is to fill the entire allocation."); |
| 368 | error_log(""); |
| 369 | error_log(" fill_on_free[=XX]"); |
| 370 | error_log(" On free, fill with the value 0x%02x. If XX is set it will", |
| 371 | DEFAULT_FILL_FREE_VALUE); |
| 372 | error_log(" only fill up to XX bytes of the allocation. The default is to"); |
| 373 | error_log(" fill the entire allocation."); |
| 374 | error_log(""); |
| 375 | error_log(" fill[=XX]"); |
| 376 | error_log(" On both first allocation free, fill with the value 0x%02x on", |
| 377 | DEFAULT_FILL_ALLOC_VALUE); |
| 378 | error_log(" first allocation and the value 0x%02x. If XX is set, only fill", |
| 379 | DEFAULT_FILL_FREE_VALUE); |
| 380 | error_log(" up to XX bytes. The default is to fill the entire allocation."); |
| 381 | error_log(""); |
| 382 | error_log(" expand_alloc[=XX]"); |
| 383 | error_log(" Allocate an extra number of bytes for every allocation call."); |
| 384 | error_log(" If XX is set, that is the number of bytes to expand the"); |
Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 385 | error_log(" allocation by. The default is %zu bytes, the max bytes is %zu.", |
| 386 | DEFAULT_EXPAND_BYTES, MAX_EXPAND_BYTES); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 387 | error_log(""); |
| 388 | error_log(" free_track[=XX]"); |
| 389 | error_log(" When a pointer is freed, do not free the memory right away."); |
| 390 | error_log(" Instead, keep XX of these allocations around and then verify"); |
| 391 | error_log(" that they have not been modified when the total number of freed"); |
| 392 | error_log(" allocations exceeds the XX amount. When the program terminates,"); |
Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 393 | error_log(" the rest of these allocations are verified. When this option is"); |
| 394 | error_log(" enabled, it automatically records the backtrace at the time of the free."); |
Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 395 | error_log(" The default is to record %zu allocations, the max allocations", |
| 396 | DEFAULT_FREE_TRACK_ALLOCATIONS); |
| 397 | error_log(" to record is %zu.", MAX_FREE_TRACK_ALLOCATIONS); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 398 | error_log(""); |
Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 399 | error_log(" free_track_backtrace_num_frames[=XX]"); |
| 400 | error_log(" This option only has meaning if free_track is set. This indicates"); |
| 401 | error_log(" how many backtrace frames to capture when an allocation is freed."); |
| 402 | error_log(" If XX is set, that is the number of frames to capture. If XX"); |
| 403 | error_log(" is set to zero, then no backtrace will be captured."); |
Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 404 | error_log(" The default is to record %zu frames, the max number of frames is %zu.", |
| 405 | DEFAULT_BACKTRACE_FRAMES, MAX_BACKTRACE_FRAMES); |
Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 406 | error_log(""); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 407 | error_log(" leak_track"); |
| 408 | error_log(" Enable the leak tracking of memory allocations."); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 409 | error_log(""); |
| 410 | error_log(" record_allocs[=XX]"); |
| 411 | error_log(" Record every single allocation/free call. When a specific signal"); |
| 412 | error_log(" is sent to the process, the contents of recording are written to"); |
| 413 | error_log(" a file (%s) and the recording is cleared.", DEFAULT_RECORD_ALLOCS_FILE); |
| 414 | error_log(" If XX is set, that is the total number of allocations/frees that can"); |
| 415 | error_log(" recorded. of frames to capture. The default value is %zu.", DEFAULT_RECORD_ALLOCS); |
| 416 | error_log(" If the allocation list fills up, all further allocations are not recorded."); |
| 417 | error_log(""); |
Tamas Berghammer | 545808a | 2016-08-26 12:34:16 +0100 | [diff] [blame] | 418 | error_log(" record_allocs_file[=FILE]"); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 419 | error_log(" This option only has meaning if the record_allocs options has been specified."); |
| 420 | error_log(" This is the name of the file to which recording information will be dumped."); |
| 421 | error_log(" The default is %s.", DEFAULT_RECORD_ALLOCS_FILE); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 422 | } |
| 423 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 424 | bool Config::GetOption(const char** options_str, std::string* option, std::string* value) { |
| 425 | const char* cur = *options_str; |
| 426 | // Process each property name we can find. |
| 427 | while (isspace(*cur)) |
| 428 | ++cur; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 429 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 430 | if (*cur == '\0') { |
| 431 | *options_str = cur; |
| 432 | return false; |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 435 | const char* start = cur; |
| 436 | while (!isspace(*cur) && *cur != '=' && *cur != '\0') |
| 437 | ++cur; |
| 438 | |
| 439 | *option = std::string(start, cur - start); |
| 440 | |
| 441 | // Skip any spaces after the name. |
| 442 | while (isspace(*cur)) |
| 443 | ++cur; |
| 444 | |
| 445 | value->clear(); |
| 446 | if (*cur == '=') { |
| 447 | ++cur; |
| 448 | // Skip the space after the equal. |
| 449 | while (isspace(*cur)) |
| 450 | ++cur; |
| 451 | |
| 452 | start = cur; |
| 453 | while (!isspace(*cur) && *cur != '\0') |
| 454 | ++cur; |
| 455 | |
| 456 | if (cur != start) { |
| 457 | *value = std::string(start, cur - start); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 458 | } |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 459 | } |
| 460 | *options_str = cur; |
| 461 | return true; |
| 462 | } |
| 463 | |
| 464 | bool Config::Init(const char* options_str) { |
| 465 | // Initialize a few default values. |
| 466 | fill_alloc_value_ = DEFAULT_FILL_ALLOC_VALUE; |
| 467 | fill_free_value_ = DEFAULT_FILL_FREE_VALUE; |
| 468 | front_guard_value_ = DEFAULT_FRONT_GUARD_VALUE; |
| 469 | rear_guard_value_ = DEFAULT_REAR_GUARD_VALUE; |
| 470 | backtrace_signal_ = SIGRTMAX - 19; |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 471 | backtrace_dump_signal_ = SIGRTMAX - 17; |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 472 | record_allocs_signal_ = SIGRTMAX - 18; |
| 473 | free_track_backtrace_num_frames_ = 0; |
| 474 | record_allocs_file_.clear(); |
| 475 | fill_on_free_bytes_ = 0; |
| 476 | backtrace_enable_on_signal_ = false; |
| 477 | backtrace_enabled_ = false; |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 478 | backtrace_dump_on_exit_ = false; |
| 479 | backtrace_dump_prefix_ = DEFAULT_BACKTRACE_DUMP_PREFIX; |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 480 | |
| 481 | // Process each option name we can find. |
| 482 | std::string option; |
| 483 | std::string value; |
| 484 | bool valid = true; |
| 485 | while (GetOption(&options_str, &option, &value)) { |
| 486 | auto entry = kOptions.find(option); |
| 487 | if (entry == kOptions.end()) { |
| 488 | error_log("%s: unknown option %s", getprogname(), option.c_str()); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 489 | valid = false; |
| 490 | break; |
| 491 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 492 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 493 | const OptionInfo* info = &entry->second; |
| 494 | auto process_func = info->process_func; |
| 495 | if (process_func != nullptr && !(this->*process_func)(option, value)) { |
| 496 | valid = false; |
| 497 | break; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 498 | } |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 499 | options_ |= info->option; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 500 | } |
| 501 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 502 | if (!valid || *options_str != '\0') { |
| 503 | LogUsage(); |
| 504 | return false; |
| 505 | } |
| 506 | |
| 507 | return true; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 508 | } |