blob: e3798ab9fcd082e905c1e1e7cbf06fb8b12974a6 [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 = {
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 Ferris7bd01782016-04-20 12:30:58 -070081
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070082 {"backtrace",
83 {BACKTRACE | TRACK_ALLOCS, &Config::SetBacktrace},
84 },
85 {"backtrace_enable_on_signal",
86 {BACKTRACE | TRACK_ALLOCS, &Config::SetBacktraceEnableOnSignal},
87 },
Christopher Ferris7bd01782016-04-20 12:30:58 -070088
Christopher Ferris602b88c2017-08-04 13:04:04 -070089 {"backtrace_dump_on_exit",
90 {0, &Config::SetBacktraceDumpOnExit},
91 },
92 {"backtrace_dump_prefix",
93 {0, &Config::SetBacktraceDumpPrefix},
94 },
95
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070096 {"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 Ferris7bd01782016-04-20 12:30:58 -0700105
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700106 {"expand_alloc",
107 {EXPAND_ALLOC, &Config::SetExpandAlloc},
108 },
Christopher Ferris7bd01782016-04-20 12:30:58 -0700109
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700110 {"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 Ferris63860cb2015-11-16 17:30:32 -0800127};
128
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700129bool 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 Ferris7bd01782016-04-20 12:30:58 -0700132
133 // Parse the value into a size_t value.
134 errno = 0;
135 char* end;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700136 long long_value = strtol(value.c_str(), &end, 10);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700137 if (errno != 0) {
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700138 error_log("%s: bad value for option '%s': %s", getprogname(), option.c_str(),
Christopher Ferris7bd01782016-04-20 12:30:58 -0700139 strerror(errno));
140 return false;
141 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700142 if (end == value.c_str()) {
143 error_log("%s: bad value for option '%s'", getprogname(), option.c_str());
Christopher Ferris7bd01782016-04-20 12:30:58 -0700144 return false;
145 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700146 if (static_cast<size_t>(end - value.c_str()) != value.size()) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700147 error_log("%s: bad value for option '%s', non space found after option: %s",
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700148 getprogname(), option.c_str(), end);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700149 return false;
150 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700151 if (long_value < 0) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700152 error_log("%s: bad value for option '%s', value cannot be negative: %ld",
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700153 getprogname(), option.c_str(), long_value);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700154 return false;
155 }
156
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700157 if (static_cast<size_t>(long_value) < min_value) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700158 error_log("%s: bad value for option '%s', value must be >= %zu: %ld",
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700159 getprogname(), option.c_str(), min_value, long_value);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700160 return false;
161 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700162 if (static_cast<size_t>(long_value) > max_value) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700163 error_log("%s: bad value for option '%s', value must be <= %zu: %ld",
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700164 getprogname(), option.c_str(), max_value, long_value);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700165 return false;
166 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700167 *parsed_value = static_cast<size_t>(long_value);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700168 return true;
169}
170
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700171bool 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 Ferris63860cb2015-11-16 17:30:32 -0800179
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700180bool 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 Ferris63860cb2015-11-16 17:30:32 -0800187
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700188 if (!ParseValue(option, value, 1, MAX_GUARD_BYTES, &rear_guard_bytes_)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800189 return false;
190 }
191
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700192 // It's necessary to align the front guard to MINIMUM_ALIGNMENT_BYTES to
193 // make sure that the header is aligned properly.
194 front_guard_bytes_ = BIONIC_ALIGN(rear_guard_bytes_, MINIMUM_ALIGNMENT_BYTES);
195 return true;
196}
Christopher Ferris63860cb2015-11-16 17:30:32 -0800197
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700198bool 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.
204 front_guard_bytes_ = BIONIC_ALIGN(front_guard_bytes_, MINIMUM_ALIGNMENT_BYTES);
205 return true;
206}
Christopher Ferris63860cb2015-11-16 17:30:32 -0800207
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700208bool 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 Ferris63860cb2015-11-16 17:30:32 -0800211
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700212bool 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 Ferris7bd01782016-04-20 12:30:58 -0700219
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700220 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 Ferris7bd01782016-04-20 12:30:58 -0700226
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700227bool 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
231bool 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
235bool 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
241bool 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 Ferris602b88c2017-08-04 13:04:04 -0700247bool 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
255bool 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 Ferris2b2b25b2017-04-05 19:13:03 -0700265bool 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
269bool 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
282bool 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
287bool 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
295bool 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
305bool 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 Ferris63860cb2015-11-16 17:30:32 -0800311 }
312 return true;
313}
314
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700315
316void Config::LogUsage() const {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800317 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 Ferris549e5222016-02-22 19:23:26 -0800322 error_log(" %zu bytes, the max bytes is %zu.", DEFAULT_GUARD_BYTES, MAX_GUARD_BYTES);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800323 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 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(" 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 Ferris549e5222016-02-22 19:23:26 -0800332 error_log(" The default is %zu bytes, the max bytes is %zu.",
333 DEFAULT_GUARD_BYTES, MAX_GUARD_BYTES);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800334 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 Ferris602b88c2017-08-04 13:04:04 -0700338 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 Ferris549e5222016-02-22 19:23:26 -0800341 error_log(" The default is %zu frames, the max number of frames is %zu.",
342 DEFAULT_BACKTRACE_FRAMES, MAX_BACKTRACE_FRAMES);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800343 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 Ferris549e5222016-02-22 19:23:26 -0800348 error_log(" frames. The default is %zu frames, the max number of frames is %zu.",
349 DEFAULT_BACKTRACE_FRAMES, MAX_BACKTRACE_FRAMES);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800350 error_log("");
Christopher Ferris602b88c2017-08-04 13:04:04 -0700351 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 Ferris63860cb2015-11-16 17:30:32 -0800364 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 Ferris549e5222016-02-22 19:23:26 -0800385 error_log(" allocation by. The default is %zu bytes, the max bytes is %zu.",
386 DEFAULT_EXPAND_BYTES, MAX_EXPAND_BYTES);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800387 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 Ferris7993b802016-01-28 18:35:05 -0800393 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 Ferris549e5222016-02-22 19:23:26 -0800395 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 Ferris63860cb2015-11-16 17:30:32 -0800398 error_log("");
Christopher Ferris7993b802016-01-28 18:35:05 -0800399 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 Ferris549e5222016-02-22 19:23:26 -0800404 error_log(" The default is to record %zu frames, the max number of frames is %zu.",
405 DEFAULT_BACKTRACE_FRAMES, MAX_BACKTRACE_FRAMES);
Christopher Ferris7993b802016-01-28 18:35:05 -0800406 error_log("");
Christopher Ferris63860cb2015-11-16 17:30:32 -0800407 error_log(" leak_track");
408 error_log(" Enable the leak tracking of memory allocations.");
Christopher Ferris7bd01782016-04-20 12:30:58 -0700409 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 Berghammer545808a2016-08-26 12:34:16 +0100418 error_log(" record_allocs_file[=FILE]");
Christopher Ferris7bd01782016-04-20 12:30:58 -0700419 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 Ferris63860cb2015-11-16 17:30:32 -0800422}
423
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700424bool 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 Ferris63860cb2015-11-16 17:30:32 -0800429
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700430 if (*cur == '\0') {
431 *options_str = cur;
432 return false;
Christopher Ferris7bd01782016-04-20 12:30:58 -0700433 }
434
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700435 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 Ferris63860cb2015-11-16 17:30:32 -0800458 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700459 }
460 *options_str = cur;
461 return true;
462}
463
464bool 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 Ferris602b88c2017-08-04 13:04:04 -0700471 backtrace_dump_signal_ = SIGRTMAX - 17;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700472 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 Ferris602b88c2017-08-04 13:04:04 -0700478 backtrace_dump_on_exit_ = false;
479 backtrace_dump_prefix_ = DEFAULT_BACKTRACE_DUMP_PREFIX;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700480
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 Ferris63860cb2015-11-16 17:30:32 -0800489 valid = false;
490 break;
491 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800492
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700493 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 Ferris63860cb2015-11-16 17:30:32 -0800498 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700499 options_ |= info->option;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800500 }
501
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700502 if (!valid || *options_str != '\0') {
503 LogUsage();
504 return false;
505 }
506
507 return true;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800508}