blob: 6a81277af6f5412b32a6142083415dfe7e654389 [file] [log] [blame]
Christopher Ferris63860cb2015-11-16 17:30:32 -08001/*
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 Ferris2b2b25b2017-04-05 19:13:03 -070029#include <assert.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -080030#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 Gao4956c372019-12-19 16:35:51 -080041#include <platform/bionic/macros.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -080042
43#include "Config.h"
44#include "debug_log.h"
45
Christopher Ferris549e5222016-02-22 19:23:26 -080046// Config constants
47static constexpr uint8_t DEFAULT_FILL_ALLOC_VALUE = 0xeb;
48static constexpr uint8_t DEFAULT_FILL_FREE_VALUE = 0xef;
49
50static constexpr uint8_t DEFAULT_FRONT_GUARD_VALUE = 0xaa;
51static constexpr uint8_t DEFAULT_REAR_GUARD_VALUE = 0xbb;
52
53// Used as the default for all guard values.
54static constexpr size_t DEFAULT_GUARD_BYTES = 32;
55static constexpr size_t MAX_GUARD_BYTES = 16384;
56
57static constexpr size_t DEFAULT_BACKTRACE_FRAMES = 16;
58static constexpr size_t MAX_BACKTRACE_FRAMES = 256;
Christopher Ferris602b88c2017-08-04 13:04:04 -070059static constexpr const char DEFAULT_BACKTRACE_DUMP_PREFIX[] = "/data/local/tmp/backtrace_heap";
Christopher Ferris549e5222016-02-22 19:23:26 -080060
61static constexpr size_t DEFAULT_EXPAND_BYTES = 16;
62static constexpr size_t MAX_EXPAND_BYTES = 16384;
63
64static constexpr size_t DEFAULT_FREE_TRACK_ALLOCATIONS = 100;
65static constexpr size_t MAX_FREE_TRACK_ALLOCATIONS = 16384;
66
Christopher Ferris7bd01782016-04-20 12:30:58 -070067static constexpr size_t DEFAULT_RECORD_ALLOCS = 8000000;
68static constexpr size_t MAX_RECORD_ALLOCS = 50000000;
69static constexpr const char DEFAULT_RECORD_ALLOCS_FILE[] = "/data/local/tmp/record_allocs.txt";
Christopher Ferris63860cb2015-11-16 17:30:32 -080070
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070071const std::unordered_map<std::string, Config::OptionInfo> Config::kOptions = {
Christopher Ferris4da25032018-03-07 13:38:48 -080072 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -070073 "guard",
74 {FRONT_GUARD | REAR_GUARD | TRACK_ALLOCS, &Config::SetGuard},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070075 },
Christopher Ferris4da25032018-03-07 13:38:48 -080076 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -070077 "front_guard",
78 {FRONT_GUARD | TRACK_ALLOCS, &Config::SetFrontGuard},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070079 },
Christopher Ferris4da25032018-03-07 13:38:48 -080080 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -070081 "rear_guard",
82 {REAR_GUARD | TRACK_ALLOCS, &Config::SetRearGuard},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070083 },
Christopher Ferris7bd01782016-04-20 12:30:58 -070084
Christopher Ferris4da25032018-03-07 13:38:48 -080085 {
Christopher Ferrisa3836482022-05-13 12:09:39 -070086 "backtrace_size",
87 {BACKTRACE_SPECIFIC_SIZES, &Config::SetBacktraceSize},
88 },
89 {
90 "backtrace_min_size",
91 {BACKTRACE_SPECIFIC_SIZES, &Config::SetBacktraceMinSize},
92 },
93 {
94 "backtrace_max_size",
95 {BACKTRACE_SPECIFIC_SIZES, &Config::SetBacktraceMaxSize},
96 },
97
98 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -070099 "backtrace",
100 {BACKTRACE | TRACK_ALLOCS, &Config::SetBacktrace},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700101 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800102 {
103 "backtrace_enable_on_signal",
104 {BACKTRACE | TRACK_ALLOCS, &Config::SetBacktraceEnableOnSignal},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700105 },
Christopher Ferris7bd01782016-04-20 12:30:58 -0700106
Christopher Ferris4da25032018-03-07 13:38:48 -0800107 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700108 "backtrace_dump_on_exit",
109 {0, &Config::SetBacktraceDumpOnExit},
Christopher Ferris602b88c2017-08-04 13:04:04 -0700110 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800111 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700112 "backtrace_dump_prefix",
113 {0, &Config::SetBacktraceDumpPrefix},
Christopher Ferris602b88c2017-08-04 13:04:04 -0700114 },
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700115 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700116 "backtrace_full",
117 {BACKTRACE_FULL, &Config::VerifyValueEmpty},
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700118 },
Christopher Ferris602b88c2017-08-04 13:04:04 -0700119
Christopher Ferris4da25032018-03-07 13:38:48 -0800120 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700121 "fill",
122 {FILL_ON_ALLOC | FILL_ON_FREE, &Config::SetFill},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700123 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800124 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700125 "fill_on_alloc",
126 {FILL_ON_ALLOC, &Config::SetFillOnAlloc},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700127 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800128 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700129 "fill_on_free",
130 {FILL_ON_FREE, &Config::SetFillOnFree},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700131 },
Christopher Ferris7bd01782016-04-20 12:30:58 -0700132
Christopher Ferris4da25032018-03-07 13:38:48 -0800133 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700134 "expand_alloc",
135 {EXPAND_ALLOC, &Config::SetExpandAlloc},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700136 },
Christopher Ferris7bd01782016-04-20 12:30:58 -0700137
Christopher Ferris4da25032018-03-07 13:38:48 -0800138 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700139 "free_track",
140 {FREE_TRACK | FILL_ON_FREE | TRACK_ALLOCS, &Config::SetFreeTrack},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700141 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800142 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700143 "free_track_backtrace_num_frames",
144 {0, &Config::SetFreeTrackBacktraceNumFrames},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700145 },
146
Christopher Ferris4da25032018-03-07 13:38:48 -0800147 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700148 "leak_track",
149 {LEAK_TRACK | TRACK_ALLOCS, &Config::VerifyValueEmpty},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700150 },
151
Christopher Ferris4da25032018-03-07 13:38:48 -0800152 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700153 "record_allocs",
154 {RECORD_ALLOCS, &Config::SetRecordAllocs},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700155 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800156 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700157 "record_allocs_file",
158 {0, &Config::SetRecordAllocsFile},
Christopher Ferris4da25032018-03-07 13:38:48 -0800159 },
160
161 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700162 "verify_pointers",
163 {TRACK_ALLOCS, &Config::VerifyValueEmpty},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700164 },
Iris Chang7f209a92019-01-16 11:17:15 +0800165 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700166 "abort_on_error",
167 {ABORT_ON_ERROR, &Config::VerifyValueEmpty},
Iris Chang7f209a92019-01-16 11:17:15 +0800168 },
Christopher Ferrisc328e442019-04-01 19:31:26 -0700169 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700170 "verbose",
171 {VERBOSE, &Config::VerifyValueEmpty},
172 },
173 {
174 "check_unreachable_on_signal",
175 {CHECK_UNREACHABLE_ON_SIGNAL, &Config::VerifyValueEmpty},
Christopher Ferrisc328e442019-04-01 19:31:26 -0700176 },
Christopher Ferris63860cb2015-11-16 17:30:32 -0800177};
178
Christopher Ferris4da25032018-03-07 13:38:48 -0800179bool Config::ParseValue(const std::string& option, const std::string& value, size_t min_value,
180 size_t max_value, size_t* parsed_value) const {
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700181 assert(!value.empty());
Christopher Ferris7bd01782016-04-20 12:30:58 -0700182
183 // Parse the value into a size_t value.
184 errno = 0;
185 char* end;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700186 long long_value = strtol(value.c_str(), &end, 10);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700187 if (errno != 0) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800188 error_log("%s: bad value for option '%s': %s", getprogname(), option.c_str(), strerror(errno));
Christopher Ferris7bd01782016-04-20 12:30:58 -0700189 return false;
190 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700191 if (end == value.c_str()) {
192 error_log("%s: bad value for option '%s'", getprogname(), option.c_str());
Christopher Ferris7bd01782016-04-20 12:30:58 -0700193 return false;
194 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700195 if (static_cast<size_t>(end - value.c_str()) != value.size()) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800196 error_log("%s: bad value for option '%s', non space found after option: %s", getprogname(),
197 option.c_str(), end);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700198 return false;
199 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700200 if (long_value < 0) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800201 error_log("%s: bad value for option '%s', value cannot be negative: %ld", getprogname(),
202 option.c_str(), long_value);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700203 return false;
204 }
205
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700206 if (static_cast<size_t>(long_value) < min_value) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800207 error_log("%s: bad value for option '%s', value must be >= %zu: %ld", getprogname(),
208 option.c_str(), min_value, long_value);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700209 return false;
210 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700211 if (static_cast<size_t>(long_value) > max_value) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800212 error_log("%s: bad value for option '%s', value must be <= %zu: %ld", getprogname(),
213 option.c_str(), max_value, long_value);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700214 return false;
215 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700216 *parsed_value = static_cast<size_t>(long_value);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700217 return true;
218}
219
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700220bool Config::ParseValue(const std::string& option, const std::string& value, size_t default_value,
221 size_t min_value, size_t max_value, size_t* new_value) const {
222 if (value.empty()) {
223 *new_value = default_value;
224 return true;
225 }
226 return ParseValue(option, value, min_value, max_value, new_value);
227}
Christopher Ferris63860cb2015-11-16 17:30:32 -0800228
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700229bool Config::SetGuard(const std::string& option, const std::string& value) {
230 if (value.empty()) {
231 // Set the defaults.
232 front_guard_bytes_ = DEFAULT_GUARD_BYTES;
233 rear_guard_bytes_ = DEFAULT_GUARD_BYTES;
234 return true;
235 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800236
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700237 if (!ParseValue(option, value, 1, MAX_GUARD_BYTES, &rear_guard_bytes_)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800238 return false;
239 }
240
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700241 // It's necessary to align the front guard to MINIMUM_ALIGNMENT_BYTES to
242 // make sure that the header is aligned properly.
Dan Alberta613d0d2017-10-05 16:39:33 -0700243 front_guard_bytes_ = __BIONIC_ALIGN(rear_guard_bytes_, MINIMUM_ALIGNMENT_BYTES);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700244 return true;
245}
Christopher Ferris63860cb2015-11-16 17:30:32 -0800246
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700247bool Config::SetFrontGuard(const std::string& option, const std::string& value) {
248 if (!ParseValue(option, value, DEFAULT_GUARD_BYTES, 1, MAX_GUARD_BYTES, &front_guard_bytes_)) {
249 return false;
250 }
251 // It's necessary to align the front guard to MINIMUM_ALIGNMENT_BYTES to
252 // make sure that the header is aligned properly.
Dan Alberta613d0d2017-10-05 16:39:33 -0700253 front_guard_bytes_ = __BIONIC_ALIGN(front_guard_bytes_, MINIMUM_ALIGNMENT_BYTES);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700254 return true;
255}
Christopher Ferris63860cb2015-11-16 17:30:32 -0800256
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700257bool Config::SetRearGuard(const std::string& option, const std::string& value) {
258 return ParseValue(option, value, DEFAULT_GUARD_BYTES, 1, MAX_GUARD_BYTES, &rear_guard_bytes_);
259}
Christopher Ferris63860cb2015-11-16 17:30:32 -0800260
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700261bool Config::SetFill(const std::string& option, const std::string& value) {
262 if (value.empty()) {
263 // Set the defaults.
264 fill_on_alloc_bytes_ = SIZE_MAX;
265 fill_on_free_bytes_ = SIZE_MAX;
266 return true;
267 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700268
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700269 if (!ParseValue(option, value, 1, SIZE_MAX, &fill_on_alloc_bytes_)) {
270 return false;
271 }
272 fill_on_free_bytes_ = fill_on_alloc_bytes_;
273 return true;
274}
Christopher Ferris7bd01782016-04-20 12:30:58 -0700275
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700276bool Config::SetFillOnAlloc(const std::string& option, const std::string& value) {
277 return ParseValue(option, value, SIZE_MAX, 1, SIZE_MAX, &fill_on_alloc_bytes_);
278}
279
280bool Config::SetFillOnFree(const std::string& option, const std::string& value) {
281 return ParseValue(option, value, SIZE_MAX, 1, SIZE_MAX, &fill_on_free_bytes_);
282}
283
284bool Config::SetBacktrace(const std::string& option, const std::string& value) {
285 backtrace_enabled_ = true;
286 return ParseValue(option, value, DEFAULT_BACKTRACE_FRAMES, 1, MAX_BACKTRACE_FRAMES,
287 &backtrace_frames_);
288}
289
290bool Config::SetBacktraceEnableOnSignal(const std::string& option, const std::string& value) {
291 backtrace_enable_on_signal_ = true;
292 return ParseValue(option, value, DEFAULT_BACKTRACE_FRAMES, 1, MAX_BACKTRACE_FRAMES,
293 &backtrace_frames_);
294}
295
Christopher Ferris602b88c2017-08-04 13:04:04 -0700296bool Config::SetBacktraceDumpOnExit(const std::string& option, const std::string& value) {
297 if (Config::VerifyValueEmpty(option, value)) {
298 backtrace_dump_on_exit_ = true;
299 return true;
300 }
301 return false;
302}
303
304bool Config::SetBacktraceDumpPrefix(const std::string&, const std::string& value) {
305 if (value.empty()) {
306 backtrace_dump_prefix_ = DEFAULT_BACKTRACE_DUMP_PREFIX;
307 } else {
308 backtrace_dump_prefix_ = value;
309 }
310 return true;
311}
312
Christopher Ferrisa3836482022-05-13 12:09:39 -0700313bool Config::SetBacktraceSize(const std::string& option, const std::string& value) {
314 if (!ParseValue(option, value, 1, SIZE_MAX, &backtrace_min_size_bytes_)) {
315 return false;
316 }
317 backtrace_max_size_bytes_ = backtrace_min_size_bytes_;
318
319 return true;
320}
321
322bool Config::SetBacktraceMinSize(const std::string& option, const std::string& value) {
323 return ParseValue(option, value, 1, SIZE_MAX, &backtrace_min_size_bytes_);
324}
325
326bool Config::SetBacktraceMaxSize(const std::string& option, const std::string& value) {
327 return ParseValue(option, value, 1, SIZE_MAX, &backtrace_max_size_bytes_);
328}
329
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700330bool Config::SetExpandAlloc(const std::string& option, const std::string& value) {
331 return ParseValue(option, value, DEFAULT_EXPAND_BYTES, 1, MAX_EXPAND_BYTES, &expand_alloc_bytes_);
332}
333
334bool Config::SetFreeTrack(const std::string& option, const std::string& value) {
335 // This option enables fill on free, so set the bytes to the default value.
336 if (fill_on_free_bytes_ == 0) {
337 fill_on_free_bytes_ = SIZE_MAX;
338 }
339 if (free_track_backtrace_num_frames_ == 0) {
340 free_track_backtrace_num_frames_ = DEFAULT_BACKTRACE_FRAMES;
341 }
342
343 return ParseValue(option, value, DEFAULT_FREE_TRACK_ALLOCATIONS, 1, MAX_FREE_TRACK_ALLOCATIONS,
344 &free_track_allocations_);
345}
346
347bool Config::SetFreeTrackBacktraceNumFrames(const std::string& option, const std::string& value) {
348 return ParseValue(option, value, DEFAULT_BACKTRACE_FRAMES, 0, MAX_BACKTRACE_FRAMES,
349 &free_track_backtrace_num_frames_);
350}
351
352bool Config::SetRecordAllocs(const std::string& option, const std::string& value) {
353 if (record_allocs_file_.empty()) {
354 record_allocs_file_ = DEFAULT_RECORD_ALLOCS_FILE;
355 }
356 return ParseValue(option, value, DEFAULT_RECORD_ALLOCS, 1, MAX_RECORD_ALLOCS,
357 &record_allocs_num_entries_);
358}
359
360bool Config::SetRecordAllocsFile(const std::string&, const std::string& value) {
361 if (value.empty()) {
362 // Set the default.
363 record_allocs_file_ = DEFAULT_RECORD_ALLOCS_FILE;
364 return true;
365 }
366 record_allocs_file_ = value;
367 return true;
368}
369
370bool Config::VerifyValueEmpty(const std::string& option, const std::string& value) {
371 if (!value.empty()) {
372 // This is not valid.
Christopher Ferris4da25032018-03-07 13:38:48 -0800373 error_log("%s: value set for option '%s' which does not take a value", getprogname(),
374 option.c_str());
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700375 return false;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800376 }
377 return true;
378}
379
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700380void Config::LogUsage() const {
Christopher Ferris770cbb32018-05-25 12:46:55 -0700381 error_log("For malloc debug option descriptions go to:");
382 error_log(" https://android.googlesource.com/platform/bionic/+/master/libc/malloc_debug/README.md");
Christopher Ferris63860cb2015-11-16 17:30:32 -0800383}
384
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700385bool Config::GetOption(const char** options_str, std::string* option, std::string* value) {
386 const char* cur = *options_str;
387 // Process each property name we can find.
Christopher Ferris4da25032018-03-07 13:38:48 -0800388 while (isspace(*cur)) ++cur;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800389
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700390 if (*cur == '\0') {
391 *options_str = cur;
392 return false;
Christopher Ferris7bd01782016-04-20 12:30:58 -0700393 }
394
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700395 const char* start = cur;
Christopher Ferris4da25032018-03-07 13:38:48 -0800396 while (!isspace(*cur) && *cur != '=' && *cur != '\0') ++cur;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700397
398 *option = std::string(start, cur - start);
399
400 // Skip any spaces after the name.
Christopher Ferris4da25032018-03-07 13:38:48 -0800401 while (isspace(*cur)) ++cur;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700402
403 value->clear();
404 if (*cur == '=') {
405 ++cur;
406 // Skip the space after the equal.
Christopher Ferris4da25032018-03-07 13:38:48 -0800407 while (isspace(*cur)) ++cur;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700408
409 start = cur;
Christopher Ferris4da25032018-03-07 13:38:48 -0800410 while (!isspace(*cur) && *cur != '\0') ++cur;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700411
412 if (cur != start) {
413 *value = std::string(start, cur - start);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800414 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700415 }
416 *options_str = cur;
417 return true;
418}
419
420bool Config::Init(const char* options_str) {
421 // Initialize a few default values.
422 fill_alloc_value_ = DEFAULT_FILL_ALLOC_VALUE;
423 fill_free_value_ = DEFAULT_FILL_FREE_VALUE;
424 front_guard_value_ = DEFAULT_FRONT_GUARD_VALUE;
425 rear_guard_value_ = DEFAULT_REAR_GUARD_VALUE;
426 backtrace_signal_ = SIGRTMAX - 19;
Christopher Ferris602b88c2017-08-04 13:04:04 -0700427 backtrace_dump_signal_ = SIGRTMAX - 17;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700428 record_allocs_signal_ = SIGRTMAX - 18;
429 free_track_backtrace_num_frames_ = 0;
430 record_allocs_file_.clear();
431 fill_on_free_bytes_ = 0;
432 backtrace_enable_on_signal_ = false;
433 backtrace_enabled_ = false;
Christopher Ferris602b88c2017-08-04 13:04:04 -0700434 backtrace_dump_on_exit_ = false;
435 backtrace_dump_prefix_ = DEFAULT_BACKTRACE_DUMP_PREFIX;
Christopher Ferrisa3836482022-05-13 12:09:39 -0700436 backtrace_min_size_bytes_ = 0;
437 backtrace_max_size_bytes_ = SIZE_MAX;
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700438 check_unreachable_signal_ = SIGRTMAX - 16;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700439
440 // Process each option name we can find.
441 std::string option;
442 std::string value;
443 bool valid = true;
444 while (GetOption(&options_str, &option, &value)) {
445 auto entry = kOptions.find(option);
446 if (entry == kOptions.end()) {
447 error_log("%s: unknown option %s", getprogname(), option.c_str());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800448 valid = false;
449 break;
450 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800451
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700452 const OptionInfo* info = &entry->second;
453 auto process_func = info->process_func;
454 if (process_func != nullptr && !(this->*process_func)(option, value)) {
455 valid = false;
456 break;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800457 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700458 options_ |= info->option;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800459 }
460
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700461 if (!valid || *options_str != '\0') {
462 LogUsage();
463 return false;
464 }
465
466 return true;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800467}