blob: ef1d2a932d1bcdc3e4de82b7434664af20645b1f [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
Elliott Hughescbc80ba2018-02-13 14:26:29 -080029#pragma once
Christopher Ferris63860cb2015-11-16 17:30:32 -080030
31#include <stdint.h>
32
Christopher Ferris7bd01782016-04-20 12:30:58 -070033#include <string>
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070034#include <unordered_map>
Christopher Ferris7bd01782016-04-20 12:30:58 -070035
Christopher Ferris63860cb2015-11-16 17:30:32 -080036constexpr uint64_t FRONT_GUARD = 0x1;
37constexpr uint64_t REAR_GUARD = 0x2;
38constexpr uint64_t BACKTRACE = 0x4;
39constexpr uint64_t FILL_ON_ALLOC = 0x8;
40constexpr uint64_t FILL_ON_FREE = 0x10;
41constexpr uint64_t EXPAND_ALLOC = 0x20;
42constexpr uint64_t FREE_TRACK = 0x40;
43constexpr uint64_t TRACK_ALLOCS = 0x80;
44constexpr uint64_t LEAK_TRACK = 0x100;
Christopher Ferris7bd01782016-04-20 12:30:58 -070045constexpr uint64_t RECORD_ALLOCS = 0x200;
Christopher Ferris93bdd6a2018-04-05 11:12:38 -070046constexpr uint64_t BACKTRACE_FULL = 0x400;
Iris Chang7f209a92019-01-16 11:17:15 +080047constexpr uint64_t ABORT_ON_ERROR = 0x800;
Christopher Ferrisc328e442019-04-01 19:31:26 -070048constexpr uint64_t VERBOSE = 0x1000;
Christopher Ferrisb42e8b42022-05-09 14:00:47 -070049constexpr uint64_t CHECK_UNREACHABLE_ON_SIGNAL = 0x2000;
Christopher Ferrisa3836482022-05-13 12:09:39 -070050constexpr uint64_t BACKTRACE_SPECIFIC_SIZES = 0x4000;
Christopher Ferris63860cb2015-11-16 17:30:32 -080051
Christopher Ferris72df6702016-02-11 15:51:31 -080052// In order to guarantee posix compliance, set the minimum alignment
53// to 8 bytes for 32 bit systems and 16 bytes for 64 bit systems.
54#if defined(__LP64__)
55constexpr size_t MINIMUM_ALIGNMENT_BYTES = 16;
56#else
57constexpr size_t MINIMUM_ALIGNMENT_BYTES = 8;
58#endif
59
Christopher Ferris55a89a42016-04-07 17:14:53 -070060// If one or more of these options is set, then a special header is needed.
Christopher Ferris4da25032018-03-07 13:38:48 -080061constexpr uint64_t HEADER_OPTIONS = FRONT_GUARD | REAR_GUARD;
Christopher Ferris63860cb2015-11-16 17:30:32 -080062
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070063class Config {
64 public:
65 bool Init(const char* options_str);
Christopher Ferris63860cb2015-11-16 17:30:32 -080066
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070067 void LogUsage() const;
Christopher Ferris63860cb2015-11-16 17:30:32 -080068
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070069 uint64_t options() const { return options_; }
Christopher Ferris63860cb2015-11-16 17:30:32 -080070
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070071 int backtrace_signal() const { return backtrace_signal_; }
Christopher Ferris602b88c2017-08-04 13:04:04 -070072 int backtrace_dump_signal() const { return backtrace_dump_signal_; }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070073 size_t backtrace_frames() const { return backtrace_frames_; }
74 size_t backtrace_enabled() const { return backtrace_enabled_; }
75 size_t backtrace_enable_on_signal() const { return backtrace_enable_on_signal_; }
Christopher Ferris602b88c2017-08-04 13:04:04 -070076 bool backtrace_dump_on_exit() const { return backtrace_dump_on_exit_; }
77 const std::string& backtrace_dump_prefix() const { return backtrace_dump_prefix_; }
Christopher Ferris63860cb2015-11-16 17:30:32 -080078
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070079 size_t front_guard_bytes() const { return front_guard_bytes_; }
80 size_t rear_guard_bytes() const { return rear_guard_bytes_; }
81 uint8_t front_guard_value() const { return front_guard_value_; }
82 uint8_t rear_guard_value() const { return rear_guard_value_; }
Christopher Ferris63860cb2015-11-16 17:30:32 -080083
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070084 size_t expand_alloc_bytes() const { return expand_alloc_bytes_; }
Christopher Ferris63860cb2015-11-16 17:30:32 -080085
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070086 size_t free_track_allocations() const { return free_track_allocations_; }
87 size_t free_track_backtrace_num_frames() const { return free_track_backtrace_num_frames_; }
Christopher Ferris7bd01782016-04-20 12:30:58 -070088
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070089 size_t fill_on_alloc_bytes() const { return fill_on_alloc_bytes_; }
90 size_t fill_on_free_bytes() const { return fill_on_free_bytes_; }
91 uint8_t fill_alloc_value() const { return fill_alloc_value_; }
92 uint8_t fill_free_value() const { return fill_free_value_; }
93
Christopher Ferrisa3836482022-05-13 12:09:39 -070094 size_t backtrace_min_size_bytes() const { return backtrace_min_size_bytes_; }
95 size_t backtrace_max_size_bytes() const { return backtrace_max_size_bytes_; }
96
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070097 int record_allocs_signal() const { return record_allocs_signal_; }
98 size_t record_allocs_num_entries() const { return record_allocs_num_entries_; }
99 const std::string& record_allocs_file() const { return record_allocs_file_; }
100
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700101 int check_unreachable_signal() const { return check_unreachable_signal_; }
102
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700103 private:
104 struct OptionInfo {
105 uint64_t option;
106 bool (Config::*process_func)(const std::string&, const std::string&);
107 };
108
109 bool ParseValue(const std::string& option, const std::string& value, size_t default_value,
110 size_t min_value, size_t max_value, size_t* new_value) const;
111
112 bool ParseValue(const std::string& option, const std::string& value, size_t min_value,
113 size_t max_value, size_t* parsed_value) const;
114
115 bool SetGuard(const std::string& option, const std::string& value);
116 bool SetFrontGuard(const std::string& option, const std::string& value);
117 bool SetRearGuard(const std::string& option, const std::string& value);
118
119 bool SetFill(const std::string& option, const std::string& value);
120 bool SetFillOnAlloc(const std::string& option, const std::string& value);
121 bool SetFillOnFree(const std::string& option, const std::string& value);
122
123 bool SetBacktrace(const std::string& option, const std::string& value);
124 bool SetBacktraceEnableOnSignal(const std::string& option, const std::string& value);
Christopher Ferris602b88c2017-08-04 13:04:04 -0700125 bool SetBacktraceDumpOnExit(const std::string& option, const std::string& value);
126 bool SetBacktraceDumpPrefix(const std::string& option, const std::string& value);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700127
Christopher Ferrisa3836482022-05-13 12:09:39 -0700128 bool SetBacktraceSize(const std::string& option, const std::string& value);
129 bool SetBacktraceMinSize(const std::string& option, const std::string& value);
130 bool SetBacktraceMaxSize(const std::string& option, const std::string& value);
131
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700132 bool SetExpandAlloc(const std::string& option, const std::string& value);
133
134 bool SetFreeTrack(const std::string& option, const std::string& value);
135 bool SetFreeTrackBacktraceNumFrames(const std::string& option, const std::string& value);
136
137 bool SetRecordAllocs(const std::string& option, const std::string& value);
138 bool SetRecordAllocsFile(const std::string& option, const std::string& value);
139
140 bool VerifyValueEmpty(const std::string& option, const std::string& value);
141
142 static bool GetOption(const char** option_str, std::string* option, std::string* value);
143
144 const static std::unordered_map<std::string, OptionInfo> kOptions;
145
146 size_t front_guard_bytes_ = 0;
147 size_t rear_guard_bytes_ = 0;
148
149 bool backtrace_enable_on_signal_ = false;
150 int backtrace_signal_ = 0;
Christopher Ferris602b88c2017-08-04 13:04:04 -0700151 int backtrace_dump_signal_ = 0;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700152 bool backtrace_enabled_ = false;
153 size_t backtrace_frames_ = 0;
Christopher Ferris602b88c2017-08-04 13:04:04 -0700154 bool backtrace_dump_on_exit_ = false;
155 std::string backtrace_dump_prefix_;
Christopher Ferrisa3836482022-05-13 12:09:39 -0700156 size_t backtrace_min_size_bytes_ = 0;
157 size_t backtrace_max_size_bytes_ = 0;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700158
159 size_t fill_on_alloc_bytes_ = 0;
160 size_t fill_on_free_bytes_ = 0;
161
162 size_t expand_alloc_bytes_ = 0;
163
164 size_t free_track_allocations_ = 0;
165 size_t free_track_backtrace_num_frames_ = 0;
166
167 int record_allocs_signal_ = 0;
168 size_t record_allocs_num_entries_ = 0;
169 std::string record_allocs_file_;
170
171 uint64_t options_ = 0;
172 uint8_t fill_alloc_value_;
173 uint8_t fill_free_value_;
174 uint8_t front_guard_value_;
175 uint8_t rear_guard_value_;
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700176
177 int check_unreachable_signal_ = 0;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800178};