blob: 96fc27f73fc33719e4c62c2cc5e2b631859723b3 [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
Christopher Ferris63860cb2015-11-16 17:30:32 -080041#include <private/bionic_macros.h>
42
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 {
73 "guard", {FRONT_GUARD | REAR_GUARD | TRACK_ALLOCS, &Config::SetGuard},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070074 },
Christopher Ferris4da25032018-03-07 13:38:48 -080075 {
76 "front_guard", {FRONT_GUARD | TRACK_ALLOCS, &Config::SetFrontGuard},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070077 },
Christopher Ferris4da25032018-03-07 13:38:48 -080078 {
79 "rear_guard", {REAR_GUARD | TRACK_ALLOCS, &Config::SetRearGuard},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070080 },
Christopher Ferris7bd01782016-04-20 12:30:58 -070081
Christopher Ferris4da25032018-03-07 13:38:48 -080082 {
83 "backtrace", {BACKTRACE | TRACK_ALLOCS, &Config::SetBacktrace},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070084 },
Christopher Ferris4da25032018-03-07 13:38:48 -080085 {
86 "backtrace_enable_on_signal",
87 {BACKTRACE | TRACK_ALLOCS, &Config::SetBacktraceEnableOnSignal},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070088 },
Christopher Ferris7bd01782016-04-20 12:30:58 -070089
Christopher Ferris4da25032018-03-07 13:38:48 -080090 {
91 "backtrace_dump_on_exit", {0, &Config::SetBacktraceDumpOnExit},
Christopher Ferris602b88c2017-08-04 13:04:04 -070092 },
Christopher Ferris4da25032018-03-07 13:38:48 -080093 {
94 "backtrace_dump_prefix", {0, &Config::SetBacktraceDumpPrefix},
Christopher Ferris602b88c2017-08-04 13:04:04 -070095 },
Christopher Ferris93bdd6a2018-04-05 11:12:38 -070096 {
97 "backtrace_full", {BACKTRACE_FULL, &Config::VerifyValueEmpty},
98 },
Christopher Ferris602b88c2017-08-04 13:04:04 -070099
Christopher Ferris4da25032018-03-07 13:38:48 -0800100 {
101 "fill", {FILL_ON_ALLOC | FILL_ON_FREE, &Config::SetFill},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700102 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800103 {
104 "fill_on_alloc", {FILL_ON_ALLOC, &Config::SetFillOnAlloc},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700105 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800106 {
107 "fill_on_free", {FILL_ON_FREE, &Config::SetFillOnFree},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700108 },
Christopher Ferris7bd01782016-04-20 12:30:58 -0700109
Christopher Ferris4da25032018-03-07 13:38:48 -0800110 {
111 "expand_alloc", {EXPAND_ALLOC, &Config::SetExpandAlloc},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700112 },
Christopher Ferris7bd01782016-04-20 12:30:58 -0700113
Christopher Ferris4da25032018-03-07 13:38:48 -0800114 {
115 "free_track", {FREE_TRACK | FILL_ON_FREE | TRACK_ALLOCS, &Config::SetFreeTrack},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700116 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800117 {
118 "free_track_backtrace_num_frames", {0, &Config::SetFreeTrackBacktraceNumFrames},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700119 },
120
Christopher Ferris4da25032018-03-07 13:38:48 -0800121 {
122 "leak_track", {LEAK_TRACK | TRACK_ALLOCS, &Config::VerifyValueEmpty},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700123 },
124
Christopher Ferris4da25032018-03-07 13:38:48 -0800125 {
126 "record_allocs", {RECORD_ALLOCS, &Config::SetRecordAllocs},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700127 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800128 {
129 "record_allocs_file", {0, &Config::SetRecordAllocsFile},
130 },
131
132 {
133 "verify_pointers", {TRACK_ALLOCS, &Config::VerifyValueEmpty},
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700134 },
Christopher Ferris63860cb2015-11-16 17:30:32 -0800135};
136
Christopher Ferris4da25032018-03-07 13:38:48 -0800137bool 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 Ferris2b2b25b2017-04-05 19:13:03 -0700139 assert(!value.empty());
Christopher Ferris7bd01782016-04-20 12:30:58 -0700140
141 // Parse the value into a size_t value.
142 errno = 0;
143 char* end;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700144 long long_value = strtol(value.c_str(), &end, 10);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700145 if (errno != 0) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800146 error_log("%s: bad value for option '%s': %s", getprogname(), option.c_str(), strerror(errno));
Christopher Ferris7bd01782016-04-20 12:30:58 -0700147 return false;
148 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700149 if (end == value.c_str()) {
150 error_log("%s: bad value for option '%s'", getprogname(), option.c_str());
Christopher Ferris7bd01782016-04-20 12:30:58 -0700151 return false;
152 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700153 if (static_cast<size_t>(end - value.c_str()) != value.size()) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800154 error_log("%s: bad value for option '%s', non space found after option: %s", getprogname(),
155 option.c_str(), end);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700156 return false;
157 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700158 if (long_value < 0) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800159 error_log("%s: bad value for option '%s', value cannot be negative: %ld", getprogname(),
160 option.c_str(), long_value);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700161 return false;
162 }
163
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700164 if (static_cast<size_t>(long_value) < min_value) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800165 error_log("%s: bad value for option '%s', value must be >= %zu: %ld", getprogname(),
166 option.c_str(), min_value, long_value);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700167 return false;
168 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700169 if (static_cast<size_t>(long_value) > max_value) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800170 error_log("%s: bad value for option '%s', value must be <= %zu: %ld", getprogname(),
171 option.c_str(), max_value, long_value);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700172 return false;
173 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700174 *parsed_value = static_cast<size_t>(long_value);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700175 return true;
176}
177
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700178bool 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 Ferris63860cb2015-11-16 17:30:32 -0800186
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700187bool 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 Ferris63860cb2015-11-16 17:30:32 -0800194
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700195 if (!ParseValue(option, value, 1, MAX_GUARD_BYTES, &rear_guard_bytes_)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800196 return false;
197 }
198
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700199 // It's necessary to align the front guard to MINIMUM_ALIGNMENT_BYTES to
200 // make sure that the header is aligned properly.
Dan Alberta613d0d2017-10-05 16:39:33 -0700201 front_guard_bytes_ = __BIONIC_ALIGN(rear_guard_bytes_, MINIMUM_ALIGNMENT_BYTES);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700202 return true;
203}
Christopher Ferris63860cb2015-11-16 17:30:32 -0800204
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700205bool 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 Alberta613d0d2017-10-05 16:39:33 -0700211 front_guard_bytes_ = __BIONIC_ALIGN(front_guard_bytes_, MINIMUM_ALIGNMENT_BYTES);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700212 return true;
213}
Christopher Ferris63860cb2015-11-16 17:30:32 -0800214
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700215bool 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 Ferris63860cb2015-11-16 17:30:32 -0800218
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700219bool 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 Ferris7bd01782016-04-20 12:30:58 -0700226
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700227 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 Ferris7bd01782016-04-20 12:30:58 -0700233
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700234bool 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
238bool 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
242bool 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
248bool 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 Ferris602b88c2017-08-04 13:04:04 -0700254bool 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
262bool 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 Ferris2b2b25b2017-04-05 19:13:03 -0700271bool 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
275bool 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
288bool 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
293bool 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
301bool 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
311bool Config::VerifyValueEmpty(const std::string& option, const std::string& value) {
312 if (!value.empty()) {
313 // This is not valid.
Christopher Ferris4da25032018-03-07 13:38:48 -0800314 error_log("%s: value set for option '%s' which does not take a value", getprogname(),
315 option.c_str());
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700316 return false;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800317 }
318 return true;
319}
320
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700321void Config::LogUsage() const {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800322 error_log("malloc debug options usage:");
323 error_log("");
324 error_log(" front_guard[=XX]");
325 error_log(" Enables a front guard on all allocations. If XX is set");
326 error_log(" it sets the number of bytes in the guard. The default is");
Christopher Ferris549e5222016-02-22 19:23:26 -0800327 error_log(" %zu bytes, the max bytes is %zu.", DEFAULT_GUARD_BYTES, MAX_GUARD_BYTES);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800328 error_log("");
329 error_log(" rear_guard[=XX]");
330 error_log(" Enables a rear guard on all allocations. If XX is set");
331 error_log(" it sets the number of bytes in the guard. The default is");
Christopher Ferris549e5222016-02-22 19:23:26 -0800332 error_log(" %zu bytes, the max bytes is %zu.", DEFAULT_GUARD_BYTES, MAX_GUARD_BYTES);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800333 error_log("");
334 error_log(" guard[=XX]");
335 error_log(" Enables both a front guard and a rear guard on all allocations.");
336 error_log(" If XX is set it sets the number of bytes in both guards.");
Christopher Ferris4da25032018-03-07 13:38:48 -0800337 error_log(" The default is %zu bytes, the max bytes is %zu.", DEFAULT_GUARD_BYTES,
338 MAX_GUARD_BYTES);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800339 error_log("");
340 error_log(" backtrace[=XX]");
341 error_log(" Enable capturing the backtrace at the point of allocation.");
342 error_log(" If XX is set it sets the number of backtrace frames.");
Christopher Ferris602b88c2017-08-04 13:04:04 -0700343 error_log(" This option also enables dumping the backtrace heap data");
344 error_log(" when a signal is received. The data is dumped to the file");
345 error_log(" backtrace_dump_prefix.<PID>.txt.");
Christopher Ferris549e5222016-02-22 19:23:26 -0800346 error_log(" The default is %zu frames, the max number of frames is %zu.",
347 DEFAULT_BACKTRACE_FRAMES, MAX_BACKTRACE_FRAMES);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800348 error_log("");
349 error_log(" backtrace_enable_on_signal[=XX]");
350 error_log(" Enable capturing the backtrace at the point of allocation.");
351 error_log(" The backtrace capture is not enabled until the process");
352 error_log(" receives a signal. If XX is set it sets the number of backtrace");
Christopher Ferris549e5222016-02-22 19:23:26 -0800353 error_log(" frames. The default is %zu frames, the max number of frames is %zu.",
354 DEFAULT_BACKTRACE_FRAMES, MAX_BACKTRACE_FRAMES);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800355 error_log("");
Christopher Ferris602b88c2017-08-04 13:04:04 -0700356 error_log(" backtrace_dump_prefix[=FILE]");
357 error_log(" This option only has meaning if the backtrace option has been specified.");
358 error_log(" This is the prefix of the name of the file to which backtrace heap");
359 error_log(" data will be dumped. The file will be named backtrace_dump_prefix.<PID>.txt.");
360 error_log(" The default is %s.", DEFAULT_BACKTRACE_DUMP_PREFIX);
361
362 error_log("");
363 error_log(" backtrace_dump_on_exit");
364 error_log(" This option only has meaning if the backtrace option has been specified.");
365 error_log(" This will cause all live allocations to be dumped to the file");
366 error_log(" backtrace_dump_prefix.<PID>.final.txt.");
367 error_log(" The default is false.");
368 error_log("");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700369 error_log(" backtrace_full");
370 error_log(" Any time a backtrace is acquired, use an unwinder that can");
371 error_log(" display Java stack frames. This unwinder can run slower than");
372 error_log(" normal unwinder.");
373 error_log("");
Christopher Ferris63860cb2015-11-16 17:30:32 -0800374 error_log(" fill_on_alloc[=XX]");
375 error_log(" On first allocation, fill with the value 0x%02x.", DEFAULT_FILL_ALLOC_VALUE);
376 error_log(" If XX is set it will only fill up to XX bytes of the");
377 error_log(" allocation. The default is to fill the entire allocation.");
378 error_log("");
379 error_log(" fill_on_free[=XX]");
380 error_log(" On free, fill with the value 0x%02x. If XX is set it will",
381 DEFAULT_FILL_FREE_VALUE);
382 error_log(" only fill up to XX bytes of the allocation. The default is to");
383 error_log(" fill the entire allocation.");
384 error_log("");
385 error_log(" fill[=XX]");
386 error_log(" On both first allocation free, fill with the value 0x%02x on",
387 DEFAULT_FILL_ALLOC_VALUE);
388 error_log(" first allocation and the value 0x%02x. If XX is set, only fill",
389 DEFAULT_FILL_FREE_VALUE);
390 error_log(" up to XX bytes. The default is to fill the entire allocation.");
391 error_log("");
392 error_log(" expand_alloc[=XX]");
393 error_log(" Allocate an extra number of bytes for every allocation call.");
394 error_log(" If XX is set, that is the number of bytes to expand the");
Christopher Ferris549e5222016-02-22 19:23:26 -0800395 error_log(" allocation by. The default is %zu bytes, the max bytes is %zu.",
396 DEFAULT_EXPAND_BYTES, MAX_EXPAND_BYTES);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800397 error_log("");
398 error_log(" free_track[=XX]");
399 error_log(" When a pointer is freed, do not free the memory right away.");
400 error_log(" Instead, keep XX of these allocations around and then verify");
401 error_log(" that they have not been modified when the total number of freed");
402 error_log(" allocations exceeds the XX amount. When the program terminates,");
Christopher Ferris7993b802016-01-28 18:35:05 -0800403 error_log(" the rest of these allocations are verified. When this option is");
404 error_log(" enabled, it automatically records the backtrace at the time of the free.");
Christopher Ferris549e5222016-02-22 19:23:26 -0800405 error_log(" The default is to record %zu allocations, the max allocations",
406 DEFAULT_FREE_TRACK_ALLOCATIONS);
407 error_log(" to record is %zu.", MAX_FREE_TRACK_ALLOCATIONS);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800408 error_log("");
Christopher Ferris7993b802016-01-28 18:35:05 -0800409 error_log(" free_track_backtrace_num_frames[=XX]");
410 error_log(" This option only has meaning if free_track is set. This indicates");
411 error_log(" how many backtrace frames to capture when an allocation is freed.");
412 error_log(" If XX is set, that is the number of frames to capture. If XX");
413 error_log(" is set to zero, then no backtrace will be captured.");
Christopher Ferris549e5222016-02-22 19:23:26 -0800414 error_log(" The default is to record %zu frames, the max number of frames is %zu.",
415 DEFAULT_BACKTRACE_FRAMES, MAX_BACKTRACE_FRAMES);
Christopher Ferris7993b802016-01-28 18:35:05 -0800416 error_log("");
Christopher Ferris63860cb2015-11-16 17:30:32 -0800417 error_log(" leak_track");
418 error_log(" Enable the leak tracking of memory allocations.");
Christopher Ferris7bd01782016-04-20 12:30:58 -0700419 error_log("");
420 error_log(" record_allocs[=XX]");
421 error_log(" Record every single allocation/free call. When a specific signal");
422 error_log(" is sent to the process, the contents of recording are written to");
423 error_log(" a file (%s) and the recording is cleared.", DEFAULT_RECORD_ALLOCS_FILE);
424 error_log(" If XX is set, that is the total number of allocations/frees that can");
425 error_log(" recorded. of frames to capture. The default value is %zu.", DEFAULT_RECORD_ALLOCS);
426 error_log(" If the allocation list fills up, all further allocations are not recorded.");
427 error_log("");
Tamas Berghammer545808a2016-08-26 12:34:16 +0100428 error_log(" record_allocs_file[=FILE]");
Christopher Ferris7bd01782016-04-20 12:30:58 -0700429 error_log(" This option only has meaning if the record_allocs options has been specified.");
430 error_log(" This is the name of the file to which recording information will be dumped.");
431 error_log(" The default is %s.", DEFAULT_RECORD_ALLOCS_FILE);
Christopher Ferris4da25032018-03-07 13:38:48 -0800432 error_log("");
433 error_log(" verify_pointers");
434 error_log(" A lightweight way to verify that free/malloc_usable_size/realloc");
435 error_log(" are passed valid pointers.");
Christopher Ferris63860cb2015-11-16 17:30:32 -0800436}
437
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700438bool Config::GetOption(const char** options_str, std::string* option, std::string* value) {
439 const char* cur = *options_str;
440 // Process each property name we can find.
Christopher Ferris4da25032018-03-07 13:38:48 -0800441 while (isspace(*cur)) ++cur;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800442
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700443 if (*cur == '\0') {
444 *options_str = cur;
445 return false;
Christopher Ferris7bd01782016-04-20 12:30:58 -0700446 }
447
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700448 const char* start = cur;
Christopher Ferris4da25032018-03-07 13:38:48 -0800449 while (!isspace(*cur) && *cur != '=' && *cur != '\0') ++cur;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700450
451 *option = std::string(start, cur - start);
452
453 // Skip any spaces after the name.
Christopher Ferris4da25032018-03-07 13:38:48 -0800454 while (isspace(*cur)) ++cur;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700455
456 value->clear();
457 if (*cur == '=') {
458 ++cur;
459 // Skip the space after the equal.
Christopher Ferris4da25032018-03-07 13:38:48 -0800460 while (isspace(*cur)) ++cur;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700461
462 start = cur;
Christopher Ferris4da25032018-03-07 13:38:48 -0800463 while (!isspace(*cur) && *cur != '\0') ++cur;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700464
465 if (cur != start) {
466 *value = std::string(start, cur - start);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800467 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700468 }
469 *options_str = cur;
470 return true;
471}
472
473bool Config::Init(const char* options_str) {
474 // Initialize a few default values.
475 fill_alloc_value_ = DEFAULT_FILL_ALLOC_VALUE;
476 fill_free_value_ = DEFAULT_FILL_FREE_VALUE;
477 front_guard_value_ = DEFAULT_FRONT_GUARD_VALUE;
478 rear_guard_value_ = DEFAULT_REAR_GUARD_VALUE;
479 backtrace_signal_ = SIGRTMAX - 19;
Christopher Ferris602b88c2017-08-04 13:04:04 -0700480 backtrace_dump_signal_ = SIGRTMAX - 17;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700481 record_allocs_signal_ = SIGRTMAX - 18;
482 free_track_backtrace_num_frames_ = 0;
483 record_allocs_file_.clear();
484 fill_on_free_bytes_ = 0;
485 backtrace_enable_on_signal_ = false;
486 backtrace_enabled_ = false;
Christopher Ferris602b88c2017-08-04 13:04:04 -0700487 backtrace_dump_on_exit_ = false;
488 backtrace_dump_prefix_ = DEFAULT_BACKTRACE_DUMP_PREFIX;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700489
490 // Process each option name we can find.
491 std::string option;
492 std::string value;
493 bool valid = true;
494 while (GetOption(&options_str, &option, &value)) {
495 auto entry = kOptions.find(option);
496 if (entry == kOptions.end()) {
497 error_log("%s: unknown option %s", getprogname(), option.c_str());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800498 valid = false;
499 break;
500 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800501
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700502 const OptionInfo* info = &entry->second;
503 auto process_func = info->process_func;
504 if (process_func != nullptr && !(this->*process_func)(option, value)) {
505 valid = false;
506 break;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800507 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700508 options_ |= info->option;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800509 }
510
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700511 if (!valid || *options_str != '\0') {
512 LogUsage();
513 return false;
514 }
515
516 return true;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800517}