blob: d8a706925405760f171c61383d2bdbc4e68fe39f [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
29#ifndef MALLOC_DEBUG_CONFIG_H
30#define MALLOC_DEBUG_CONFIG_H
31
32#include <stdint.h>
33
Christopher Ferris7bd01782016-04-20 12:30:58 -070034#include <string>
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070035#include <unordered_map>
Christopher Ferris7bd01782016-04-20 12:30:58 -070036
Christopher Ferris63860cb2015-11-16 17:30:32 -080037constexpr uint64_t FRONT_GUARD = 0x1;
38constexpr uint64_t REAR_GUARD = 0x2;
39constexpr uint64_t BACKTRACE = 0x4;
40constexpr uint64_t FILL_ON_ALLOC = 0x8;
41constexpr uint64_t FILL_ON_FREE = 0x10;
42constexpr uint64_t EXPAND_ALLOC = 0x20;
43constexpr uint64_t FREE_TRACK = 0x40;
44constexpr uint64_t TRACK_ALLOCS = 0x80;
45constexpr uint64_t LEAK_TRACK = 0x100;
Christopher Ferris7bd01782016-04-20 12:30:58 -070046constexpr uint64_t RECORD_ALLOCS = 0x200;
Christopher Ferris63860cb2015-11-16 17:30:32 -080047
Christopher Ferris72df6702016-02-11 15:51:31 -080048// In order to guarantee posix compliance, set the minimum alignment
49// to 8 bytes for 32 bit systems and 16 bytes for 64 bit systems.
50#if defined(__LP64__)
51constexpr size_t MINIMUM_ALIGNMENT_BYTES = 16;
52#else
53constexpr size_t MINIMUM_ALIGNMENT_BYTES = 8;
54#endif
55
Christopher Ferris55a89a42016-04-07 17:14:53 -070056// If one or more of these options is set, then a special header is needed.
57constexpr uint64_t HEADER_OPTIONS = FRONT_GUARD | REAR_GUARD | BACKTRACE | FREE_TRACK | LEAK_TRACK;
Christopher Ferris63860cb2015-11-16 17:30:32 -080058
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070059class Config {
60 public:
61 bool Init(const char* options_str);
Christopher Ferris63860cb2015-11-16 17:30:32 -080062
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070063 void LogUsage() const;
Christopher Ferris63860cb2015-11-16 17:30:32 -080064
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070065 uint64_t options() const { return options_; }
Christopher Ferris63860cb2015-11-16 17:30:32 -080066
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070067 int backtrace_signal() const { return backtrace_signal_; }
68 size_t backtrace_frames() const { return backtrace_frames_; }
69 size_t backtrace_enabled() const { return backtrace_enabled_; }
70 size_t backtrace_enable_on_signal() const { return backtrace_enable_on_signal_; }
Christopher Ferris63860cb2015-11-16 17:30:32 -080071
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070072 size_t front_guard_bytes() const { return front_guard_bytes_; }
73 size_t rear_guard_bytes() const { return rear_guard_bytes_; }
74 uint8_t front_guard_value() const { return front_guard_value_; }
75 uint8_t rear_guard_value() const { return rear_guard_value_; }
Christopher Ferris63860cb2015-11-16 17:30:32 -080076
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070077 size_t expand_alloc_bytes() const { return expand_alloc_bytes_; }
Christopher Ferris63860cb2015-11-16 17:30:32 -080078
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070079 size_t free_track_allocations() const { return free_track_allocations_; }
80 size_t free_track_backtrace_num_frames() const { return free_track_backtrace_num_frames_; }
Christopher Ferris7bd01782016-04-20 12:30:58 -070081
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070082 size_t fill_on_alloc_bytes() const { return fill_on_alloc_bytes_; }
83 size_t fill_on_free_bytes() const { return fill_on_free_bytes_; }
84 uint8_t fill_alloc_value() const { return fill_alloc_value_; }
85 uint8_t fill_free_value() const { return fill_free_value_; }
86
87 int record_allocs_signal() const { return record_allocs_signal_; }
88 size_t record_allocs_num_entries() const { return record_allocs_num_entries_; }
89 const std::string& record_allocs_file() const { return record_allocs_file_; }
90
91 private:
92 struct OptionInfo {
93 uint64_t option;
94 bool (Config::*process_func)(const std::string&, const std::string&);
95 };
96
97 bool ParseValue(const std::string& option, const std::string& value, size_t default_value,
98 size_t min_value, size_t max_value, size_t* new_value) const;
99
100 bool ParseValue(const std::string& option, const std::string& value, size_t min_value,
101 size_t max_value, size_t* parsed_value) const;
102
103 bool SetGuard(const std::string& option, const std::string& value);
104 bool SetFrontGuard(const std::string& option, const std::string& value);
105 bool SetRearGuard(const std::string& option, const std::string& value);
106
107 bool SetFill(const std::string& option, const std::string& value);
108 bool SetFillOnAlloc(const std::string& option, const std::string& value);
109 bool SetFillOnFree(const std::string& option, const std::string& value);
110
111 bool SetBacktrace(const std::string& option, const std::string& value);
112 bool SetBacktraceEnableOnSignal(const std::string& option, const std::string& value);
113
114 bool SetExpandAlloc(const std::string& option, const std::string& value);
115
116 bool SetFreeTrack(const std::string& option, const std::string& value);
117 bool SetFreeTrackBacktraceNumFrames(const std::string& option, const std::string& value);
118
119 bool SetRecordAllocs(const std::string& option, const std::string& value);
120 bool SetRecordAllocsFile(const std::string& option, const std::string& value);
121
122 bool VerifyValueEmpty(const std::string& option, const std::string& value);
123
124 static bool GetOption(const char** option_str, std::string* option, std::string* value);
125
126 const static std::unordered_map<std::string, OptionInfo> kOptions;
127
128 size_t front_guard_bytes_ = 0;
129 size_t rear_guard_bytes_ = 0;
130
131 bool backtrace_enable_on_signal_ = false;
132 int backtrace_signal_ = 0;
133 bool backtrace_enabled_ = false;
134 size_t backtrace_frames_ = 0;
135
136 size_t fill_on_alloc_bytes_ = 0;
137 size_t fill_on_free_bytes_ = 0;
138
139 size_t expand_alloc_bytes_ = 0;
140
141 size_t free_track_allocations_ = 0;
142 size_t free_track_backtrace_num_frames_ = 0;
143
144 int record_allocs_signal_ = 0;
145 size_t record_allocs_num_entries_ = 0;
146 std::string record_allocs_file_;
147
148 uint64_t options_ = 0;
149 uint8_t fill_alloc_value_;
150 uint8_t fill_free_value_;
151 uint8_t front_guard_value_;
152 uint8_t rear_guard_value_;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800153};
154
155#endif // MALLOC_DEBUG_CONFIG_H