blob: 0d442b4c37ff17421d4cbce94b900fe3411bf244 [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 {
Junjie Hu8655d172023-01-10 18:30:45 +080090 "bt_sz",
91 {BACKTRACE_SPECIFIC_SIZES, &Config::SetBacktraceSize},
92 },
93 {
Christopher Ferrisa3836482022-05-13 12:09:39 -070094 "backtrace_min_size",
95 {BACKTRACE_SPECIFIC_SIZES, &Config::SetBacktraceMinSize},
96 },
97 {
Junjie Hu8655d172023-01-10 18:30:45 +080098 "bt_min_sz",
99 {BACKTRACE_SPECIFIC_SIZES, &Config::SetBacktraceMinSize},
100 },
101 {
Christopher Ferrisa3836482022-05-13 12:09:39 -0700102 "backtrace_max_size",
103 {BACKTRACE_SPECIFIC_SIZES, &Config::SetBacktraceMaxSize},
104 },
Junjie Hu8655d172023-01-10 18:30:45 +0800105 {
106 "bt_max_sz",
107 {BACKTRACE_SPECIFIC_SIZES, &Config::SetBacktraceMaxSize},
108 },
Christopher Ferrisa3836482022-05-13 12:09:39 -0700109 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700110 "backtrace",
111 {BACKTRACE | TRACK_ALLOCS, &Config::SetBacktrace},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700112 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800113 {
Junjie Hu8655d172023-01-10 18:30:45 +0800114 "bt",
115 {BACKTRACE | TRACK_ALLOCS, &Config::SetBacktrace},
116 },
117 {
Christopher Ferris4da25032018-03-07 13:38:48 -0800118 "backtrace_enable_on_signal",
119 {BACKTRACE | TRACK_ALLOCS, &Config::SetBacktraceEnableOnSignal},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700120 },
Junjie Hu8655d172023-01-10 18:30:45 +0800121 {
122 "bt_en_on_sig",
123 {BACKTRACE | TRACK_ALLOCS, &Config::SetBacktraceEnableOnSignal},
124 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800125 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700126 "backtrace_dump_on_exit",
127 {0, &Config::SetBacktraceDumpOnExit},
Christopher Ferris602b88c2017-08-04 13:04:04 -0700128 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800129 {
Junjie Hu8655d172023-01-10 18:30:45 +0800130 "bt_dmp_on_ex",
131 {0, &Config::SetBacktraceDumpOnExit},
132 },
133 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700134 "backtrace_dump_prefix",
135 {0, &Config::SetBacktraceDumpPrefix},
Christopher Ferris602b88c2017-08-04 13:04:04 -0700136 },
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700137 {
Junjie Hu8655d172023-01-10 18:30:45 +0800138 "bt_dmp_pre",
139 {0, &Config::SetBacktraceDumpPrefix},
140 },
141 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700142 "backtrace_full",
143 {BACKTRACE_FULL, &Config::VerifyValueEmpty},
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700144 },
Junjie Hu8655d172023-01-10 18:30:45 +0800145 {
146 "bt_full",
147 {BACKTRACE_FULL, &Config::VerifyValueEmpty},
148 },
Christopher Ferris602b88c2017-08-04 13:04:04 -0700149
Christopher Ferris4da25032018-03-07 13:38:48 -0800150 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700151 "fill",
152 {FILL_ON_ALLOC | FILL_ON_FREE, &Config::SetFill},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700153 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800154 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700155 "fill_on_alloc",
156 {FILL_ON_ALLOC, &Config::SetFillOnAlloc},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700157 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800158 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700159 "fill_on_free",
160 {FILL_ON_FREE, &Config::SetFillOnFree},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700161 },
Christopher Ferris7bd01782016-04-20 12:30:58 -0700162
Christopher Ferris4da25032018-03-07 13:38:48 -0800163 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700164 "expand_alloc",
165 {EXPAND_ALLOC, &Config::SetExpandAlloc},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700166 },
Christopher Ferris7bd01782016-04-20 12:30:58 -0700167
Christopher Ferris4da25032018-03-07 13:38:48 -0800168 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700169 "free_track",
170 {FREE_TRACK | FILL_ON_FREE | TRACK_ALLOCS, &Config::SetFreeTrack},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700171 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800172 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700173 "free_track_backtrace_num_frames",
174 {0, &Config::SetFreeTrackBacktraceNumFrames},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700175 },
176
Christopher Ferris4da25032018-03-07 13:38:48 -0800177 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700178 "leak_track",
179 {LEAK_TRACK | TRACK_ALLOCS, &Config::VerifyValueEmpty},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700180 },
181
Christopher Ferris4da25032018-03-07 13:38:48 -0800182 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700183 "record_allocs",
184 {RECORD_ALLOCS, &Config::SetRecordAllocs},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700185 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800186 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700187 "record_allocs_file",
188 {0, &Config::SetRecordAllocsFile},
Christopher Ferris4da25032018-03-07 13:38:48 -0800189 },
Christopher Ferris9b88d0a2024-06-13 13:22:02 -0700190 {
191 "record_allocs_on_exit",
192 {0, &Config::SetRecordAllocsOnExit},
193 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800194
195 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700196 "verify_pointers",
197 {TRACK_ALLOCS, &Config::VerifyValueEmpty},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700198 },
Iris Chang7f209a92019-01-16 11:17:15 +0800199 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700200 "abort_on_error",
201 {ABORT_ON_ERROR, &Config::VerifyValueEmpty},
Iris Chang7f209a92019-01-16 11:17:15 +0800202 },
Christopher Ferrisc328e442019-04-01 19:31:26 -0700203 {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700204 "verbose",
205 {VERBOSE, &Config::VerifyValueEmpty},
206 },
207 {
208 "check_unreachable_on_signal",
209 {CHECK_UNREACHABLE_ON_SIGNAL, &Config::VerifyValueEmpty},
Christopher Ferrisc328e442019-04-01 19:31:26 -0700210 },
Christopher Ferris5610d5a2023-11-14 15:04:50 -0800211 {
212 "log_allocator_stats_on_signal",
213 {LOG_ALLOCATOR_STATS_ON_SIGNAL, &Config::VerifyValueEmpty},
214 },
Christopher Ferris63860cb2015-11-16 17:30:32 -0800215};
216
Christopher Ferris4da25032018-03-07 13:38:48 -0800217bool Config::ParseValue(const std::string& option, const std::string& value, size_t min_value,
218 size_t max_value, size_t* parsed_value) const {
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700219 assert(!value.empty());
Christopher Ferris7bd01782016-04-20 12:30:58 -0700220
221 // Parse the value into a size_t value.
222 errno = 0;
223 char* end;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700224 long long_value = strtol(value.c_str(), &end, 10);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700225 if (errno != 0) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800226 error_log("%s: bad value for option '%s': %s", getprogname(), option.c_str(), strerror(errno));
Christopher Ferris7bd01782016-04-20 12:30:58 -0700227 return false;
228 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700229 if (end == value.c_str()) {
230 error_log("%s: bad value for option '%s'", getprogname(), option.c_str());
Christopher Ferris7bd01782016-04-20 12:30:58 -0700231 return false;
232 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700233 if (static_cast<size_t>(end - value.c_str()) != value.size()) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800234 error_log("%s: bad value for option '%s', non space found after option: %s", getprogname(),
235 option.c_str(), end);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700236 return false;
237 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700238 if (long_value < 0) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800239 error_log("%s: bad value for option '%s', value cannot be negative: %ld", getprogname(),
240 option.c_str(), long_value);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700241 return false;
242 }
243
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700244 if (static_cast<size_t>(long_value) < min_value) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800245 error_log("%s: bad value for option '%s', value must be >= %zu: %ld", getprogname(),
246 option.c_str(), min_value, long_value);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700247 return false;
248 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700249 if (static_cast<size_t>(long_value) > max_value) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800250 error_log("%s: bad value for option '%s', value must be <= %zu: %ld", getprogname(),
251 option.c_str(), max_value, long_value);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700252 return false;
253 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700254 *parsed_value = static_cast<size_t>(long_value);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700255 return true;
256}
257
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700258bool Config::ParseValue(const std::string& option, const std::string& value, size_t default_value,
259 size_t min_value, size_t max_value, size_t* new_value) const {
260 if (value.empty()) {
261 *new_value = default_value;
262 return true;
263 }
264 return ParseValue(option, value, min_value, max_value, new_value);
265}
Christopher Ferris63860cb2015-11-16 17:30:32 -0800266
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700267bool Config::SetGuard(const std::string& option, const std::string& value) {
268 if (value.empty()) {
269 // Set the defaults.
270 front_guard_bytes_ = DEFAULT_GUARD_BYTES;
271 rear_guard_bytes_ = DEFAULT_GUARD_BYTES;
272 return true;
273 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800274
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700275 if (!ParseValue(option, value, 1, MAX_GUARD_BYTES, &rear_guard_bytes_)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800276 return false;
277 }
278
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700279 // It's necessary to align the front guard to MINIMUM_ALIGNMENT_BYTES to
280 // make sure that the header is aligned properly.
Dan Alberta613d0d2017-10-05 16:39:33 -0700281 front_guard_bytes_ = __BIONIC_ALIGN(rear_guard_bytes_, MINIMUM_ALIGNMENT_BYTES);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700282 return true;
283}
Christopher Ferris63860cb2015-11-16 17:30:32 -0800284
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700285bool Config::SetFrontGuard(const std::string& option, const std::string& value) {
286 if (!ParseValue(option, value, DEFAULT_GUARD_BYTES, 1, MAX_GUARD_BYTES, &front_guard_bytes_)) {
287 return false;
288 }
289 // It's necessary to align the front guard to MINIMUM_ALIGNMENT_BYTES to
290 // make sure that the header is aligned properly.
Dan Alberta613d0d2017-10-05 16:39:33 -0700291 front_guard_bytes_ = __BIONIC_ALIGN(front_guard_bytes_, MINIMUM_ALIGNMENT_BYTES);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700292 return true;
293}
Christopher Ferris63860cb2015-11-16 17:30:32 -0800294
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700295bool Config::SetRearGuard(const std::string& option, const std::string& value) {
296 return ParseValue(option, value, DEFAULT_GUARD_BYTES, 1, MAX_GUARD_BYTES, &rear_guard_bytes_);
297}
Christopher Ferris63860cb2015-11-16 17:30:32 -0800298
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700299bool Config::SetFill(const std::string& option, const std::string& value) {
300 if (value.empty()) {
301 // Set the defaults.
302 fill_on_alloc_bytes_ = SIZE_MAX;
303 fill_on_free_bytes_ = SIZE_MAX;
304 return true;
305 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700306
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700307 if (!ParseValue(option, value, 1, SIZE_MAX, &fill_on_alloc_bytes_)) {
308 return false;
309 }
310 fill_on_free_bytes_ = fill_on_alloc_bytes_;
311 return true;
312}
Christopher Ferris7bd01782016-04-20 12:30:58 -0700313
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700314bool Config::SetFillOnAlloc(const std::string& option, const std::string& value) {
315 return ParseValue(option, value, SIZE_MAX, 1, SIZE_MAX, &fill_on_alloc_bytes_);
316}
317
318bool Config::SetFillOnFree(const std::string& option, const std::string& value) {
319 return ParseValue(option, value, SIZE_MAX, 1, SIZE_MAX, &fill_on_free_bytes_);
320}
321
322bool Config::SetBacktrace(const std::string& option, const std::string& value) {
323 backtrace_enabled_ = true;
324 return ParseValue(option, value, DEFAULT_BACKTRACE_FRAMES, 1, MAX_BACKTRACE_FRAMES,
325 &backtrace_frames_);
326}
327
328bool Config::SetBacktraceEnableOnSignal(const std::string& option, const std::string& value) {
329 backtrace_enable_on_signal_ = true;
330 return ParseValue(option, value, DEFAULT_BACKTRACE_FRAMES, 1, MAX_BACKTRACE_FRAMES,
331 &backtrace_frames_);
332}
333
Christopher Ferris602b88c2017-08-04 13:04:04 -0700334bool Config::SetBacktraceDumpOnExit(const std::string& option, const std::string& value) {
335 if (Config::VerifyValueEmpty(option, value)) {
336 backtrace_dump_on_exit_ = true;
337 return true;
338 }
339 return false;
340}
341
342bool Config::SetBacktraceDumpPrefix(const std::string&, const std::string& value) {
343 if (value.empty()) {
344 backtrace_dump_prefix_ = DEFAULT_BACKTRACE_DUMP_PREFIX;
345 } else {
346 backtrace_dump_prefix_ = value;
347 }
348 return true;
349}
350
Christopher Ferrisa3836482022-05-13 12:09:39 -0700351bool Config::SetBacktraceSize(const std::string& option, const std::string& value) {
352 if (!ParseValue(option, value, 1, SIZE_MAX, &backtrace_min_size_bytes_)) {
353 return false;
354 }
355 backtrace_max_size_bytes_ = backtrace_min_size_bytes_;
356
357 return true;
358}
359
360bool Config::SetBacktraceMinSize(const std::string& option, const std::string& value) {
361 return ParseValue(option, value, 1, SIZE_MAX, &backtrace_min_size_bytes_);
362}
363
364bool Config::SetBacktraceMaxSize(const std::string& option, const std::string& value) {
365 return ParseValue(option, value, 1, SIZE_MAX, &backtrace_max_size_bytes_);
366}
367
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700368bool Config::SetExpandAlloc(const std::string& option, const std::string& value) {
369 return ParseValue(option, value, DEFAULT_EXPAND_BYTES, 1, MAX_EXPAND_BYTES, &expand_alloc_bytes_);
370}
371
372bool Config::SetFreeTrack(const std::string& option, const std::string& value) {
373 // This option enables fill on free, so set the bytes to the default value.
374 if (fill_on_free_bytes_ == 0) {
375 fill_on_free_bytes_ = SIZE_MAX;
376 }
377 if (free_track_backtrace_num_frames_ == 0) {
378 free_track_backtrace_num_frames_ = DEFAULT_BACKTRACE_FRAMES;
379 }
380
381 return ParseValue(option, value, DEFAULT_FREE_TRACK_ALLOCATIONS, 1, MAX_FREE_TRACK_ALLOCATIONS,
382 &free_track_allocations_);
383}
384
385bool Config::SetFreeTrackBacktraceNumFrames(const std::string& option, const std::string& value) {
386 return ParseValue(option, value, DEFAULT_BACKTRACE_FRAMES, 0, MAX_BACKTRACE_FRAMES,
387 &free_track_backtrace_num_frames_);
388}
389
390bool Config::SetRecordAllocs(const std::string& option, const std::string& value) {
391 if (record_allocs_file_.empty()) {
392 record_allocs_file_ = DEFAULT_RECORD_ALLOCS_FILE;
393 }
394 return ParseValue(option, value, DEFAULT_RECORD_ALLOCS, 1, MAX_RECORD_ALLOCS,
395 &record_allocs_num_entries_);
396}
397
398bool Config::SetRecordAllocsFile(const std::string&, const std::string& value) {
399 if (value.empty()) {
400 // Set the default.
401 record_allocs_file_ = DEFAULT_RECORD_ALLOCS_FILE;
402 return true;
403 }
404 record_allocs_file_ = value;
405 return true;
406}
407
Christopher Ferris9b88d0a2024-06-13 13:22:02 -0700408bool Config::SetRecordAllocsOnExit(const std::string& option, const std::string& value) {
409 if (Config::VerifyValueEmpty(option, value)) {
410 record_allocs_on_exit_ = true;
411 return true;
412 }
413 return false;
414}
415
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700416bool Config::VerifyValueEmpty(const std::string& option, const std::string& value) {
417 if (!value.empty()) {
418 // This is not valid.
Christopher Ferris4da25032018-03-07 13:38:48 -0800419 error_log("%s: value set for option '%s' which does not take a value", getprogname(),
420 option.c_str());
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700421 return false;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800422 }
423 return true;
424}
425
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700426void Config::LogUsage() const {
Christopher Ferris770cbb32018-05-25 12:46:55 -0700427 error_log("For malloc debug option descriptions go to:");
Elliott Hughes9c06d162023-10-04 23:36:14 +0000428 error_log(
429 " https://android.googlesource.com/platform/bionic/+/main/libc/malloc_debug/README.md");
Christopher Ferris63860cb2015-11-16 17:30:32 -0800430}
431
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700432bool Config::GetOption(const char** options_str, std::string* option, std::string* value) {
433 const char* cur = *options_str;
434 // Process each property name we can find.
Christopher Ferris4da25032018-03-07 13:38:48 -0800435 while (isspace(*cur)) ++cur;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800436
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700437 if (*cur == '\0') {
438 *options_str = cur;
439 return false;
Christopher Ferris7bd01782016-04-20 12:30:58 -0700440 }
441
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700442 const char* start = cur;
Christopher Ferris4da25032018-03-07 13:38:48 -0800443 while (!isspace(*cur) && *cur != '=' && *cur != '\0') ++cur;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700444
445 *option = std::string(start, cur - start);
446
447 // Skip any spaces after the name.
Christopher Ferris4da25032018-03-07 13:38:48 -0800448 while (isspace(*cur)) ++cur;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700449
450 value->clear();
451 if (*cur == '=') {
452 ++cur;
453 // Skip the space after the equal.
Christopher Ferris4da25032018-03-07 13:38:48 -0800454 while (isspace(*cur)) ++cur;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700455
456 start = cur;
Christopher Ferris4da25032018-03-07 13:38:48 -0800457 while (!isspace(*cur) && *cur != '\0') ++cur;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700458
459 if (cur != start) {
460 *value = std::string(start, cur - start);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800461 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700462 }
463 *options_str = cur;
464 return true;
465}
466
467bool Config::Init(const char* options_str) {
468 // Initialize a few default values.
469 fill_alloc_value_ = DEFAULT_FILL_ALLOC_VALUE;
470 fill_free_value_ = DEFAULT_FILL_FREE_VALUE;
471 front_guard_value_ = DEFAULT_FRONT_GUARD_VALUE;
472 rear_guard_value_ = DEFAULT_REAR_GUARD_VALUE;
473 backtrace_signal_ = SIGRTMAX - 19;
Christopher Ferris602b88c2017-08-04 13:04:04 -0700474 backtrace_dump_signal_ = SIGRTMAX - 17;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700475 record_allocs_signal_ = SIGRTMAX - 18;
476 free_track_backtrace_num_frames_ = 0;
477 record_allocs_file_.clear();
478 fill_on_free_bytes_ = 0;
479 backtrace_enable_on_signal_ = false;
480 backtrace_enabled_ = false;
Christopher Ferris602b88c2017-08-04 13:04:04 -0700481 backtrace_dump_on_exit_ = false;
482 backtrace_dump_prefix_ = DEFAULT_BACKTRACE_DUMP_PREFIX;
Christopher Ferrisa3836482022-05-13 12:09:39 -0700483 backtrace_min_size_bytes_ = 0;
484 backtrace_max_size_bytes_ = SIZE_MAX;
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700485 check_unreachable_signal_ = SIGRTMAX - 16;
Christopher Ferris5610d5a2023-11-14 15:04:50 -0800486 log_allocator_stats_signal_ = SIGRTMAX - 15;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700487
488 // Process each option name we can find.
489 std::string option;
490 std::string value;
491 bool valid = true;
492 while (GetOption(&options_str, &option, &value)) {
493 auto entry = kOptions.find(option);
494 if (entry == kOptions.end()) {
495 error_log("%s: unknown option %s", getprogname(), option.c_str());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800496 valid = false;
497 break;
498 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800499
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700500 const OptionInfo* info = &entry->second;
501 auto process_func = info->process_func;
502 if (process_func != nullptr && !(this->*process_func)(option, value)) {
503 valid = false;
504 break;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800505 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700506 options_ |= info->option;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800507 }
508
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700509 if (!valid || *options_str != '\0') {
510 LogUsage();
511 return false;
512 }
513
514 return true;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800515}