Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-present The Bitcoin Core developers
3 : : // Distributed under the MIT software license, see the accompanying
4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 : :
6 : : #include <common/args.h>
7 : :
8 : : #include <chainparamsbase.h>
9 : : #include <common/settings.h>
10 : : #include <sync.h>
11 : : #include <tinyformat.h>
12 : : #include <univalue.h>
13 : : #include <util/chaintype.h>
14 : : #include <util/check.h>
15 : : #include <util/fs.h>
16 : : #include <util/fs_helpers.h>
17 : : #include <util/log.h>
18 : : #include <util/strencodings.h>
19 : : #include <util/string.h>
20 : :
21 : : #ifdef WIN32
22 : : #include <shlobj.h>
23 : : #endif
24 : :
25 : : #include <algorithm>
26 : : #include <cstdlib>
27 : : #include <cstring>
28 : : #include <list>
29 : : #include <map>
30 : : #include <optional>
31 : : #include <stdexcept>
32 : : #include <string>
33 : : #include <utility>
34 : : #include <variant>
35 : :
36 : : const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf";
37 : : const char * const BITCOIN_SETTINGS_FILENAME = "settings.json";
38 : :
39 : : ArgsManager gArgs;
40 : :
41 : : /**
42 : : * Interpret a string argument as a boolean.
43 : : *
44 : : * The definition of LocaleIndependentAtoi<int>() requires that non-numeric string values
45 : : * like "foo", return 0. This means that if a user unintentionally supplies a
46 : : * non-integer argument here, the return value is always false. This means that
47 : : * -foo=false does what the user probably expects, but -foo=true is well defined
48 : : * but does not do what they probably expected.
49 : : *
50 : : * The return value of LocaleIndependentAtoi<int>(...) is zero when given input not
51 : : * representable as an int.
52 : : *
53 : : * For a more extensive discussion of this topic (and a wide range of opinions
54 : : * on the Right Way to change this code), see PR12713.
55 : : */
56 : 398862 : static bool InterpretBool(const std::string& strValue)
57 : : {
58 [ + + ]: 398862 : if (strValue.empty())
59 : : return true;
60 [ - + ]: 379382 : return (LocaleIndependentAtoi<int>(strValue) != 0);
61 : : }
62 : :
63 : 1300018 : static std::string SettingName(const std::string& arg)
64 : : {
65 [ - + + - : 1301366 : return arg.size() > 0 && arg[0] == '-' ? arg.substr(1) : arg;
+ + ]
66 : : }
67 : :
68 : : /**
69 : : * Parse "name", "section.name", "noname", "section.noname" settings keys.
70 : : *
71 : : * @note Where an option was negated can be later checked using the
72 : : * IsArgNegated() method. One use case for this is to have a way to disable
73 : : * options that are not normally boolean (e.g. using -nodebuglogfile to request
74 : : * that debug log output is not sent to any file at all).
75 : : */
76 : 394691 : KeyInfo InterpretKey(std::string key)
77 : : {
78 [ + + ]: 394691 : KeyInfo result;
79 : : // Split section name from key name for keys like "testnet.foo" or "regtest.bar"
80 : 394691 : size_t option_index = key.find('.');
81 [ + + ]: 394691 : if (option_index != std::string::npos) {
82 [ + - ]: 143410 : result.section = key.substr(0, option_index);
83 [ + - ]: 143410 : key.erase(0, option_index + 1);
84 : : }
85 [ - + + + ]: 394691 : if (key.starts_with("no")) {
86 [ + - ]: 105724 : key.erase(0, 2);
87 : 105724 : result.negated = true;
88 : : }
89 [ + - ]: 394691 : result.name = key;
90 : 394691 : return result;
91 : 0 : }
92 : :
93 : : /**
94 : : * Interpret settings value based on registered flags.
95 : : *
96 : : * @param[in] key key information to know if key was negated
97 : : * @param[in] value string value of setting to be parsed
98 : : * @param[in] flags ArgsManager registered argument flags
99 : : * @param[out] error Error description if settings value is not valid
100 : : *
101 : : * @return parsed settings value if it is valid, otherwise nullopt accompanied
102 : : * by a descriptive error string
103 : : */
104 : 372779 : std::optional<common::SettingsValue> InterpretValue(const KeyInfo& key, const std::string* value,
105 : : unsigned int flags, std::string& error)
106 : : {
107 : : // Return negated settings as false values.
108 [ + + ]: 372779 : if (key.negated) {
109 [ - + ]: 105723 : if (flags & ArgsManager::DISALLOW_NEGATION) {
110 : 0 : error = strprintf("Negating of -%s is meaningless and therefore forbidden", key.name);
111 : 0 : return std::nullopt;
112 : : }
113 : : // Double negatives like -nofoo=0 are supported (but discouraged)
114 [ + + + + ]: 105723 : if (value && !InterpretBool(*value)) {
115 : 12 : LogWarning("Parsed potentially confusing double-negative -%s=%s", key.name, *value);
116 : 12 : return true;
117 : : }
118 : 105711 : return false;
119 : : }
120 [ + + + + ]: 267056 : if (!value && (flags & ArgsManager::DISALLOW_ELISION)) {
121 : 1 : error = strprintf("Can not set -%s with no value. Please specify value with -%s=value.", key.name, key.name);
122 : 1 : return std::nullopt;
123 : : }
124 [ + - ]: 267055 : return value ? *value : "";
125 : : }
126 : :
127 : : // Define default constructor and destructor that are not inline, so code instantiating this class doesn't need to
128 : : // #include class definitions for all members.
129 : : // For example, m_settings has an internal dependency on univalue.
130 : 52429 : ArgsManager::ArgsManager() = default;
131 [ + + ]: 262140 : ArgsManager::~ArgsManager() = default;
132 : :
133 : 49574 : std::set<std::string> ArgsManager::GetUnsuitableSectionOnlyArgs() const
134 : : {
135 [ + - ]: 49574 : std::set<std::string> unsuitables;
136 : :
137 [ + - ]: 49574 : LOCK(cs_args);
138 : :
139 : : // if there's no section selected, don't worry
140 [ - + ]: 49574 : if (m_network.empty()) return std::set<std::string> {};
141 : :
142 : : // if it's okay to use the default section for this network, don't worry
143 [ + - + + ]: 49574 : if (m_network == ChainTypeToString(ChainType::MAIN)) return std::set<std::string> {};
144 : :
145 [ + + ]: 66114 : for (const auto& arg : m_network_only_args) {
146 [ + - + - : 29088 : if (OnlyHasDefaultSectionSetting(m_settings, m_network, SettingName(arg))) {
+ + ]
147 [ + - ]: 1753 : unsuitables.insert(arg);
148 : : }
149 : : }
150 : 37026 : return unsuitables;
151 : 49574 : }
152 : :
153 : 2085 : std::list<SectionInfo> ArgsManager::GetUnrecognizedSections() const
154 : : {
155 : : // Section names to be recognized in the config file.
156 : 2085 : static const std::set<std::string> available_sections{
157 : : ChainTypeToString(ChainType::REGTEST),
158 : : ChainTypeToString(ChainType::SIGNET),
159 : : ChainTypeToString(ChainType::TESTNET),
160 : : ChainTypeToString(ChainType::TESTNET4),
161 : : ChainTypeToString(ChainType::MAIN),
162 [ + + + - : 10593 : };
- + + + -
- ]
163 : :
164 : 2085 : LOCK(cs_args);
165 [ + - ]: 2085 : std::list<SectionInfo> unrecognized = m_config_sections;
166 : 3375 : std::erase_if(unrecognized, [](const SectionInfo& appeared) { return available_sections.contains(appeared.m_name); });
167 [ + - ]: 2085 : return unrecognized;
168 [ + - + - : 3503 : }
+ - + - +
- - - ]
169 : :
170 : 51263 : void ArgsManager::SelectConfigNetwork(const std::string& network)
171 : : {
172 : 51263 : LOCK(cs_args);
173 [ + - + - ]: 102526 : m_network = network;
174 : 51263 : }
175 : :
176 : 52347 : bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::string& error)
177 : : {
178 : 52347 : LOCK(cs_args);
179 : 52347 : m_settings.command_line_options.clear();
180 : :
181 [ + + ]: 207106 : for (int i = 1; i < argc; i++) {
182 [ + - ]: 155676 : std::string key(argv[i]);
183 : :
184 : : #ifdef __APPLE__
185 : : // At the first time when a user gets the "App downloaded from the
186 : : // internet" warning, and clicks the Open button, macOS passes
187 : : // a unique process serial number (PSN) as -psn_... command-line
188 : : // argument, which we filter out.
189 : : if (key.starts_with("-psn_")) continue;
190 : : #endif
191 : :
192 [ + + ]: 155676 : if (key == "-") break; //bitcoin-tx using stdin
193 : 155664 : std::optional<std::string> val;
194 : 155664 : size_t is_index = key.find('=');
195 [ + + ]: 155664 : if (is_index != std::string::npos) {
196 [ + - ]: 141727 : val = key.substr(is_index + 1);
197 [ + - ]: 141727 : key.erase(is_index);
198 : : }
199 : : #ifdef WIN32
200 : : key = ToLower(key);
201 : : if (key[0] == '/')
202 : : key[0] = '-';
203 : : #endif
204 : :
205 [ + + ]: 155664 : if (key[0] != '-') {
206 [ + + + - ]: 893 : if (!m_accept_any_command && m_command.empty()) {
207 : : // The first non-dash arg is a registered command
208 [ + - ]: 66 : std::optional<unsigned int> flags = GetArgFlags_(key);
209 [ + + - + ]: 66 : if (!flags || !(*flags & ArgsManager::COMMAND)) {
210 [ + - ]: 5 : error = strprintf("Invalid command '%s'", argv[i]);
211 : 5 : return false;
212 : : }
213 : : }
214 [ + - ]: 888 : m_command.push_back(key);
215 [ + + ]: 1947 : while (++i < argc) {
216 : : // The remaining args are command args
217 [ + - ]: 1059 : m_command.emplace_back(argv[i]);
218 : : }
219 : 888 : break;
220 : : }
221 : :
222 : : // Transform --foo to -foo
223 [ - + + - : 154771 : if (key.length() > 1 && key[1] == '-')
+ + ]
224 [ + - ]: 6 : key.erase(0, 1);
225 : :
226 : : // Transform -foo to foo
227 [ + - ]: 154771 : key.erase(0, 1);
228 [ - + + - ]: 309542 : KeyInfo keyinfo = InterpretKey(key);
229 [ + - + - ]: 154771 : std::optional<unsigned int> flags = GetArgFlags_('-' + keyinfo.name);
230 : :
231 : : // Unknown command line options and command line options with dot
232 : : // characters (which are returned from InterpretKey with nonempty
233 : : // section strings) are not valid.
234 [ + + + + ]: 154771 : if (!flags || !keyinfo.section.empty()) {
235 [ + - ]: 11 : error = strprintf("Invalid parameter %s", argv[i]);
236 : 11 : return false;
237 : : }
238 : :
239 [ + + + - ]: 167884 : std::optional<common::SettingsValue> value = InterpretValue(keyinfo, val ? &*val : nullptr, *flags, error);
240 [ + + ]: 154760 : if (!value) return false;
241 : :
242 [ + - + - ]: 154759 : m_settings.command_line_options[keyinfo.name].push_back(*value);
243 : 311336 : }
244 : :
245 : : // we do not allow -includeconf from command line, only -noincludeconf
246 [ + - + + ]: 52330 : if (auto* includes = common::FindKey(m_settings.command_line_options, "includeconf")) {
247 : 5 : const common::SettingsSpan values{*includes};
248 : : // Range may be empty if -noincludeconf was passed
249 [ + - + + ]: 5 : if (!values.empty()) {
250 [ + - + - : 4 : error = "-includeconf cannot be used from commandline; -includeconf=" + values.begin()->write();
+ - ]
251 : 4 : return false; // pick first value as example
252 : : }
253 : : }
254 : : return true;
255 : 52347 : }
256 : :
257 : 440237 : std::optional<unsigned int> ArgsManager::GetArgFlags_(const std::string& name) const
258 : : {
259 : 440237 : AssertLockHeld(cs_args);
260 [ + + ]: 939829 : for (const auto& arg_map : m_available_args) {
261 : 918060 : const auto search = arg_map.second.find(name);
262 [ + + ]: 918060 : if (search != arg_map.second.end()) {
263 : 418468 : return search->second.m_flags;
264 : : }
265 : : }
266 : 21769 : return m_default_flags;
267 : : }
268 : :
269 : 0 : std::optional<unsigned int> ArgsManager::GetArgFlags(const std::string& name) const
270 : : {
271 : 0 : LOCK(cs_args);
272 [ # # ]: 0 : return GetArgFlags_(name);
273 : 0 : }
274 : :
275 : 0 : void ArgsManager::SetDefaultFlags(std::optional<unsigned int> flags)
276 : : {
277 : 0 : LOCK(cs_args);
278 [ # # ]: 0 : m_default_flags = flags;
279 : 0 : }
280 : :
281 : 33054 : fs::path ArgsManager::GetPathArg_(std::string arg, const fs::path& default_value) const
282 : : {
283 : 33054 : AssertLockHeld(cs_args);
284 : 33054 : const auto value = GetSetting_(arg);
285 [ + + ]: 33054 : if (value.isFalse()) return {};
286 [ + - + - ]: 33040 : std::string path_str = SettingToString(value).value_or("");
287 [ + + + - ]: 47247 : if (path_str.empty()) return default_value;
288 [ + - + - ]: 42621 : fs::path result = fs::PathFromString(path_str).lexically_normal();
289 : : // Remove trailing slash, if present.
290 [ + + + - : 42621 : return result.has_filename() ? result : result.parent_path();
+ - ]
291 : 47261 : }
292 : :
293 : 22617 : fs::path ArgsManager::GetPathArg(std::string arg, const fs::path& default_value) const
294 : : {
295 : 22617 : LOCK(cs_args);
296 [ + - + - ]: 45234 : return GetPathArg_(std::move(arg), default_value);
297 : 22617 : }
298 : :
299 : 9696 : fs::path ArgsManager::GetBlocksDirPath() const
300 : : {
301 : 9696 : LOCK(cs_args);
302 : 9696 : fs::path& path = m_cached_blocks_path;
303 : :
304 : : // Cache the path to avoid calling fs::create_directories on every call of
305 : : // this function
306 [ + + + - ]: 9696 : if (!path.empty()) return path;
307 : :
308 [ + - + - : 2284 : if (!GetSetting_("-blocksdir").isNull()) {
+ + ]
309 [ + - + - : 12 : path = fs::absolute(GetPathArg_("-blocksdir"));
+ - ]
310 [ + - + + ]: 3 : if (!fs::is_directory(path)) {
311 [ + - ]: 1 : path = "";
312 [ + - ]: 1 : return path;
313 : : }
314 : : } else {
315 [ + - ]: 6843 : path = GetDataDir(/*net_specific=*/false);
316 : : }
317 : :
318 [ + - + - ]: 4566 : path /= fs::PathFromString(BaseParams().DataDir());
319 [ + - ]: 2283 : path /= "blocks";
320 [ + - ]: 2283 : fs::create_directories(path);
321 [ + - + - ]: 9696 : return path;
322 : 9696 : }
323 : :
324 : 4209 : fs::path ArgsManager::GetDataDirBase() const {
325 : 4209 : LOCK(cs_args);
326 [ + - ]: 4209 : return GetDataDir(/*net_specific=*/false);
327 : 4209 : }
328 : :
329 : 42319 : fs::path ArgsManager::GetDataDirNet() const {
330 : 42319 : LOCK(cs_args);
331 [ + - ]: 42319 : return GetDataDir(/*net_specific=*/true);
332 : 42319 : }
333 : :
334 : 51163 : fs::path ArgsManager::GetDataDir(bool net_specific) const
335 : : {
336 : 51163 : AssertLockHeld(cs_args);
337 [ + + ]: 51163 : fs::path& path = net_specific ? m_cached_network_datadir_path : m_cached_datadir_path;
338 : :
339 : : // Used cached path if available
340 [ + + ]: 51163 : if (!path.empty()) return path;
341 : :
342 [ + - + - ]: 16140 : const fs::path datadir{GetPathArg_("-datadir")};
343 [ + + ]: 8070 : if (!datadir.empty()) {
344 [ + - ]: 16130 : path = fs::absolute(datadir);
345 [ + - - + ]: 8065 : if (!fs::is_directory(path)) {
346 [ # # ]: 0 : path = "";
347 [ # # ]: 0 : return path;
348 : : }
349 : : } else {
350 [ + - ]: 15 : path = GetDefaultDataDir();
351 : : }
352 : :
353 [ + + + - : 8070 : if (net_specific && !BaseParams().DataDir().empty()) {
+ + ]
354 [ + - + - ]: 7776 : path /= fs::PathFromString(BaseParams().DataDir());
355 : : }
356 : :
357 [ + - ]: 8070 : return path;
358 : 8070 : }
359 : :
360 : 3147 : void ArgsManager::ClearPathCache()
361 : : {
362 : 3147 : LOCK(cs_args);
363 : :
364 : 3147 : m_cached_datadir_path = fs::path();
365 : 3147 : m_cached_network_datadir_path = fs::path();
366 [ + - ]: 6294 : m_cached_blocks_path = fs::path();
367 : 3147 : }
368 : :
369 : 63 : std::optional<const ArgsManager::Command> ArgsManager::GetCommand() const
370 : : {
371 [ + - ]: 63 : Command ret;
372 [ + - ]: 63 : LOCK(cs_args);
373 [ + + ]: 63 : auto it = m_command.begin();
374 [ + + ]: 63 : if (it == m_command.end()) {
375 : : // No command was passed
376 : 2 : return std::nullopt;
377 : : }
378 [ + - ]: 61 : if (!m_accept_any_command) {
379 : : // The registered command
380 [ + - ]: 61 : ret.command = *(it++);
381 : : }
382 [ + + ]: 73 : while (it != m_command.end()) {
383 : : // The unregistered command and args (if any)
384 [ + - ]: 12 : ret.args.push_back(*(it++));
385 : : }
386 : 61 : return ret;
387 : 126 : }
388 : :
389 : 45 : bool ArgsManager::CheckCommandOptions(const std::string& command, std::vector<std::string>* errors) const
390 : : {
391 : 45 : LOCK(cs_args);
392 : :
393 : 45 : auto command_options = m_available_args.find(OptionsCategory::COMMAND_OPTIONS);
394 [ + - ]: 45 : if (command_options == m_available_args.end()) {
395 : : // There are no command-specific options at all, so everything is fine
396 : : return true;
397 : : }
398 : :
399 : 45 : const auto command_args = m_command_args.find(command);
400 : 77 : auto is_valid_opt = [&](const auto& opt) EXCLUSIVE_LOCKS_REQUIRED(cs_args) -> bool {
401 [ + + ]: 32 : if (command_args == m_command_args.end()) {
402 : : // Caller may not have checked that command actually exists
403 : : // before calling this function. In that case, treat it as
404 : : // having no valid command-specific options.
405 : : return false;
406 : : } else {
407 : 27 : return command_args->second.contains(opt);
408 : : }
409 : 45 : };
410 : :
411 : 45 : bool ok = true;
412 [ + - + + ]: 103 : for (const auto& [arg, _] : command_options->second) {
413 [ + - + + : 83 : if (!GetSetting_(arg).isNull() && !is_valid_opt(arg)) {
+ + + + ]
414 : 7 : ok = false;
415 [ + - ]: 7 : if (errors != nullptr) {
416 [ + - + - ]: 14 : errors->emplace_back(strprintf("The %s option cannot be used with the '%s' command.", arg, command));
417 : : }
418 : : }
419 : : }
420 : : return ok;
421 : 45 : }
422 : :
423 : 249268 : std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg) const
424 : : {
425 : 249268 : std::vector<std::string> result;
426 [ + - + + ]: 346921 : for (const common::SettingsValue& value : GetSettingsList(strArg)) {
427 [ - + - - : 292955 : result.push_back(value.isFalse() ? "0" : value.isTrue() ? "1" : value.get_str());
+ + + - +
- - + +
- ]
428 : : }
429 : 249268 : return result;
430 : 0 : }
431 : :
432 : 80264 : bool ArgsManager::IsArgSet(const std::string& strArg) const
433 : : {
434 : 80264 : return !GetSetting(strArg).isNull();
435 : : }
436 : :
437 : 5829 : bool ArgsManager::GetSettingsPath(fs::path* filepath, bool temp, bool backup) const
438 : : {
439 [ + - + - ]: 11658 : fs::path settings = GetPathArg("-settings", BITCOIN_SETTINGS_FILENAME);
440 [ + + ]: 5829 : if (settings.empty()) {
441 : : return false;
442 : : }
443 [ - + ]: 5822 : if (backup) {
444 [ # # ]: 0 : settings += ".bak";
445 : : }
446 [ + + ]: 5822 : if (filepath) {
447 [ + + + - : 28902 : *filepath = fsbridge::AbsPathJoin(GetDataDirNet(), temp ? settings + ".tmp" : settings);
+ - + - +
+ - - ]
448 : : }
449 : : return true;
450 : 5829 : }
451 : :
452 : 4 : static void SaveErrors(const std::vector<std::string> errors, std::vector<std::string>* error_out)
453 : : {
454 [ + + ]: 8 : for (const auto& error : errors) {
455 [ + + ]: 4 : if (error_out) {
456 : 3 : error_out->emplace_back(error);
457 : : } else {
458 : 1 : LogWarning("%s", error);
459 : : }
460 : : }
461 : 4 : }
462 : :
463 : 1303 : bool ArgsManager::ReadSettingsFile(std::vector<std::string>* errors)
464 : : {
465 : 1303 : fs::path path;
466 [ + - + - ]: 1303 : if (!GetSettingsPath(&path, /* temp= */ false)) {
467 : : return true; // Do nothing if settings file disabled.
468 : : }
469 : :
470 [ + - ]: 1303 : LOCK(cs_args);
471 : 1303 : m_settings.rw_settings.clear();
472 : 1303 : std::vector<std::string> read_errors;
473 [ + - + + ]: 1303 : if (!common::ReadSettings(path, m_settings.rw_settings, read_errors)) {
474 [ + - + - ]: 3 : SaveErrors(read_errors, errors);
475 : 3 : return false;
476 : : }
477 [ + + ]: 1452 : for (const auto& setting : m_settings.rw_settings) {
478 [ - + + - ]: 304 : KeyInfo key = InterpretKey(setting.first); // Split setting key into section and argname
479 [ + - + - : 152 : if (!GetArgFlags_('-' + key.name)) {
+ + ]
480 [ + - ]: 152 : LogWarning("Ignoring unknown rw_settings value %s", setting.first);
481 : : }
482 : 152 : }
483 : : return true;
484 [ + - ]: 3909 : }
485 : :
486 : 1506 : bool ArgsManager::WriteSettingsFile(std::vector<std::string>* errors, bool backup) const
487 : : {
488 : 1506 : fs::path path, path_tmp;
489 [ + - + - : 1506 : if (!GetSettingsPath(&path, /*temp=*/false, backup) || !GetSettingsPath(&path_tmp, /*temp=*/true, backup)) {
+ - - + ]
490 [ # # ]: 0 : throw std::logic_error("Attempt to write settings file when dynamic settings are disabled.");
491 : : }
492 : :
493 [ + - ]: 1506 : LOCK(cs_args);
494 : 1506 : std::vector<std::string> write_errors;
495 [ + - - + ]: 1506 : if (!common::WriteSettings(path_tmp, m_settings.rw_settings, write_errors)) {
496 [ # # # # ]: 0 : SaveErrors(write_errors, errors);
497 : 0 : return false;
498 : : }
499 [ + - + - : 4518 : if (!RenameOver(path_tmp, path)) {
+ - + + ]
500 [ - + + - : 4 : SaveErrors({strprintf("Failed renaming settings file %s to %s\n", fs::PathToString(path_tmp), fs::PathToString(path))}, errors);
+ - + + -
- ]
501 : 1 : return false;
502 : : }
503 : : return true;
504 [ - + + - : 6026 : }
+ - ]
505 : :
506 : 0 : common::SettingsValue ArgsManager::GetPersistentSetting(const std::string& name) const
507 : : {
508 : 0 : LOCK(cs_args);
509 [ # # # # : 0 : return common::GetSetting(m_settings, m_network, name, !UseDefaultSection("-" + name),
# # ]
510 [ # # ]: 0 : /*ignore_nonpersistent=*/true, /*get_chain_type=*/false);
511 : 0 : }
512 : :
513 : 54253 : bool ArgsManager::IsArgNegated(const std::string& strArg) const
514 : : {
515 : 54253 : return GetSetting(strArg).isFalse();
516 : : }
517 : :
518 : 66815 : std::string ArgsManager::GetArg(const std::string& strArg, const std::string& strDefault) const
519 : : {
520 [ + - ]: 133628 : return GetArg(strArg).value_or(strDefault);
521 : : }
522 : :
523 : 114883 : std::optional<std::string> ArgsManager::GetArg(const std::string& strArg) const
524 : : {
525 : 114883 : const common::SettingsValue value = GetSetting(strArg);
526 [ + + ]: 229764 : return SettingToString(value);
527 : 114883 : }
528 : :
529 : 147923 : std::optional<std::string> SettingToString(const common::SettingsValue& value)
530 : : {
531 [ + + ]: 147923 : if (value.isNull()) return std::nullopt;
532 [ + + ]: 67992 : if (value.isFalse()) return "0";
533 [ + + ]: 56277 : if (value.isTrue()) return "1";
534 [ + + - + ]: 56272 : if (value.isNum()) return value.getValStr();
535 [ - + ]: 112530 : return value.get_str();
536 : : }
537 : :
538 : : template <std::integral Int>
539 : 106172 : Int ArgsManager::GetArg(const std::string& strArg, Int nDefault) const
540 : : {
541 [ + + ]: 106172 : return GetArg<Int>(strArg).value_or(nDefault);
542 : : }
543 : :
544 : : template <std::integral Int>
545 : 151699 : std::optional<Int> ArgsManager::GetArg(const std::string& strArg) const
546 : : {
547 : 151699 : const common::SettingsValue value = GetSetting(strArg);
548 [ + + ]: 303395 : return SettingTo<Int>(value);
549 : 151699 : }
550 : :
551 : : template <std::integral Int>
552 [ + + ]: 151699 : std::optional<Int> SettingTo(const common::SettingsValue& value)
553 : : {
554 [ + + ]: 151699 : if (value.isNull()) return std::nullopt;
555 [ + + ]: 16900 : if (value.isFalse()) return 0;
556 [ + + ]: 16895 : if (value.isTrue()) return 1;
557 [ + + ]: 16892 : if (value.isNum()) return value.getInt<Int>();
558 [ - + ]: 16889 : return LocaleIndependentAtoi<Int>(value.get_str());
559 : : }
560 : :
561 : 402632 : bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault) const
562 : : {
563 [ + + ]: 402632 : return GetBoolArg(strArg).value_or(fDefault);
564 : : }
565 : :
566 : 430894 : std::optional<bool> ArgsManager::GetBoolArg(const std::string& strArg) const
567 : : {
568 : 430894 : const common::SettingsValue value = GetSetting(strArg);
569 [ + + ]: 861778 : return SettingToBool(value);
570 : 430894 : }
571 : :
572 : 430894 : std::optional<bool> SettingToBool(const common::SettingsValue& value)
573 : : {
574 [ + + ]: 430894 : if (value.isNull()) return std::nullopt;
575 [ + + ]: 286892 : if (value.isBool()) return value.get_bool();
576 : 285175 : return InterpretBool(value.get_str());
577 : : }
578 : :
579 : : #define INSTANTIATE_INT_TYPE(Type) \
580 : : template Type ArgsManager::GetArg<Type>(const std::string&, Type) const; \
581 : : template std::optional<Type> ArgsManager::GetArg<Type>(const std::string&) const; \
582 : : template std::optional<Type> SettingTo<Type>(const common::SettingsValue&)
583 : :
584 : : INSTANTIATE_INT_TYPE(int8_t);
585 : : INSTANTIATE_INT_TYPE(uint8_t);
586 : : INSTANTIATE_INT_TYPE(int16_t);
587 : : INSTANTIATE_INT_TYPE(uint16_t);
588 : : INSTANTIATE_INT_TYPE(int32_t);
589 : : INSTANTIATE_INT_TYPE(uint32_t);
590 : : INSTANTIATE_INT_TYPE(int64_t);
591 : : INSTANTIATE_INT_TYPE(uint64_t);
592 : :
593 : : #undef INSTANTIATE_INT_TYPE
594 : :
595 : 52856 : bool ArgsManager::SoftSetArg(const std::string& strArg, const std::string& strValue)
596 : : {
597 : 52856 : LOCK(cs_args);
598 [ + - + + ]: 52856 : if (!GetSetting_(strArg).isNull()) return false;
599 [ + - + - : 2094 : m_settings.forced_settings[SettingName(strArg)] = strValue;
+ - ]
600 : 2094 : return true;
601 : 52856 : }
602 : :
603 : 5360 : bool ArgsManager::SoftSetBoolArg(const std::string& strArg, bool fValue)
604 : : {
605 [ + + ]: 5360 : if (fValue)
606 [ + - ]: 2570 : return SoftSetArg(strArg, std::string("1"));
607 : : else
608 [ + - ]: 2790 : return SoftSetArg(strArg, std::string("0"));
609 : : }
610 : :
611 : 51497 : void ArgsManager::ForceSetArg(const std::string& strArg, const std::string& strValue)
612 : : {
613 : 51497 : LOCK(cs_args);
614 [ + - + - : 51497 : m_settings.forced_settings[SettingName(strArg)] = strValue;
+ - + - ]
615 : 51497 : }
616 : :
617 : 242 : void ArgsManager::AddCommand(const std::string& cmd, const std::string& help, std::set<std::string> options)
618 : : {
619 : 242 : Assert(cmd.find('=') == std::string::npos);
620 : 242 : Assert(cmd.at(0) != '-');
621 : :
622 : 242 : LOCK(cs_args);
623 : 242 : m_accept_any_command = false; // latch to false
624 [ + - ]: 242 : std::map<std::string, Arg>& arg_map = m_available_args[OptionsCategory::COMMANDS];
625 [ + - - + : 484 : auto ret = arg_map.emplace(cmd, Arg{"", help, ArgsManager::COMMAND});
+ - ]
626 [ + + ]: 242 : if (!options.empty()) {
627 [ + - ]: 112 : auto& cmdopts = m_available_args[OptionsCategory::COMMAND_OPTIONS];
628 : 112 : bool command_has_all_options_defined = true;
629 [ + + ]: 240 : for (const auto& opt : options) {
630 [ - + ]: 128 : if (!cmdopts.contains(opt)) {
631 : 0 : command_has_all_options_defined = false;
632 : : }
633 : : }
634 [ + - ]: 112 : Assert(command_has_all_options_defined);
635 : :
636 [ + - ]: 112 : m_command_args.try_emplace(cmd, std::move(options));
637 : : }
638 [ + - ]: 242 : Assert(ret.second); // Fail on duplicate commands
639 : 242 : }
640 : :
641 : 510677 : void ArgsManager::AddArg(const std::string& name, const std::string& help, unsigned int flags, const OptionsCategory& cat)
642 : : {
643 : 510677 : Assert((flags & ArgsManager::COMMAND) == 0); // use AddCommand
644 : :
645 : : // Split arg name from its help param
646 : 510677 : size_t eq_index = name.find('=');
647 [ + + ]: 510677 : if (eq_index == std::string::npos) {
648 [ - + ]: 257882 : eq_index = name.size();
649 : : }
650 : 510677 : std::string arg_name = name.substr(0, eq_index);
651 : :
652 [ + - ]: 510677 : LOCK(cs_args);
653 : :
654 : : // Allow duplicates involving HIDDEN — it is used as a placeholder for args
655 : : // unavailable in this binary but tolerated for shared config files (see #13441).
656 [ + + ]: 3148313 : for (const auto& arg_map : m_available_args) {
657 [ + + + + ]: 2637636 : if (arg_map.first == OptionsCategory::HIDDEN || cat == OptionsCategory::HIDDEN) continue;
658 [ + - ]: 1968696 : Assert(!arg_map.second.contains(arg_name));
659 : : }
660 : :
661 [ + - ]: 510677 : std::map<std::string, Arg>& arg_map = m_available_args[cat];
662 [ - + + - : 1021354 : auto ret = arg_map.emplace(arg_name, Arg{name.substr(eq_index, name.size() - eq_index), help, flags});
- + + - ]
663 [ - + ]: 510677 : assert(ret.second); // Make sure an insertion actually happened
664 : :
665 [ + + ]: 510677 : if (flags & ArgsManager::NETWORK_ONLY) {
666 [ + - ]: 41845 : m_network_only_args.emplace(arg_name);
667 : : }
668 : 510677 : }
669 : :
670 : 5452 : void ArgsManager::AddHiddenArgs(const std::vector<std::string>& names)
671 : : {
672 [ + + ]: 29124 : for (const std::string& name : names) {
673 [ + - ]: 47344 : AddArg(name, "", ArgsManager::ALLOW_ANY, OptionsCategory::HIDDEN);
674 : : }
675 : 5452 : }
676 : :
677 : 792 : void ArgsManager::ClearArgs()
678 : : {
679 : 792 : LOCK(cs_args);
680 : 792 : m_settings = {};
681 : 792 : m_available_args.clear();
682 : 792 : m_command_args.clear();
683 : 792 : m_network_only_args.clear();
684 [ + - ]: 792 : m_config_sections.clear();
685 : 792 : }
686 : :
687 : 1035 : void ArgsManager::CheckMultipleCLIArgs() const
688 : : {
689 : 1035 : LOCK(cs_args);
690 : 1035 : std::vector<std::string> found{};
691 : 1035 : auto cmds = m_available_args.find(OptionsCategory::CLI_COMMANDS);
692 [ + - ]: 1035 : if (cmds != m_available_args.end()) {
693 [ + - + + ]: 5175 : for (const auto& [cmd, argspec] : cmds->second) {
694 [ + - - + ]: 4140 : if (!GetSetting_(cmd).isNull()) {
695 [ # # ]: 0 : found.push_back(cmd);
696 : : }
697 : : }
698 [ - + - + ]: 1035 : if (found.size() > 1) {
699 [ # # # # : 0 : throw std::runtime_error(strprintf("Only one of %s may be specified.", util::Join(found, ", ")));
# # ]
700 : : }
701 : : }
702 [ + - ]: 2070 : }
703 : :
704 : 2 : std::string ArgsManager::GetHelpMessage() const
705 : : {
706 [ + - ]: 2 : const bool show_debug = GetBoolArg("-help-debug", false);
707 : :
708 [ + - ]: 2 : std::string usage;
709 [ + - ]: 2 : LOCK(cs_args);
710 : :
711 : 2 : const auto command_options = m_available_args.find(OptionsCategory::COMMAND_OPTIONS);
712 [ + - ]: 1 : const auto for_matching_cmd_opts = [&](const std::set<std::string>& select, auto&& fn) EXCLUSIVE_LOCKS_REQUIRED(cs_args) {
713 [ + - ]: 1 : if (select.empty()) return;
714 [ + - ]: 1 : if (command_options == m_available_args.end()) return;
715 [ + - + + ]: 2 : for (const auto& [name, info] : command_options->second) {
716 [ + - - + ]: 1 : if (!show_debug && (info.m_flags & ArgsManager::DEBUG_ONLY)) continue;
717 [ - + ]: 1 : if (!select.contains(name)) continue;
718 : 1 : fn(name, info);
719 : : }
720 : 2 : };
721 : :
722 [ + + + + : 14 : for (const auto& [category, category_args] : m_available_args) {
+ + + - +
+ + - + -
- + + + ]
723 [ + + + + : 13 : switch(category) {
+ + + - +
+ + - + -
- + ]
724 : 1 : case OptionsCategory::OPTIONS:
725 [ + - + - ]: 2 : usage += HelpMessageGroup("Options:");
726 : 1 : break;
727 : 1 : case OptionsCategory::CONNECTION:
728 [ + - + - ]: 2 : usage += HelpMessageGroup("Connection options:");
729 : 1 : break;
730 : 1 : case OptionsCategory::ZMQ:
731 [ + - + - ]: 2 : usage += HelpMessageGroup("ZeroMQ notification options:");
732 : 1 : break;
733 : 1 : case OptionsCategory::DEBUG_TEST:
734 [ + - + - ]: 2 : usage += HelpMessageGroup("Debugging/Testing options:");
735 : 1 : break;
736 : 1 : case OptionsCategory::NODE_RELAY:
737 [ + - + - ]: 2 : usage += HelpMessageGroup("Node relay options:");
738 : 1 : break;
739 : 1 : case OptionsCategory::BLOCK_CREATION:
740 [ + - + - ]: 2 : usage += HelpMessageGroup("Block creation options:");
741 : 1 : break;
742 : 1 : case OptionsCategory::RPC:
743 [ + - + - ]: 2 : usage += HelpMessageGroup("RPC server options:");
744 : 1 : break;
745 : 0 : case OptionsCategory::IPC:
746 [ # # # # ]: 0 : usage += HelpMessageGroup("IPC interprocess connection options:");
747 : 0 : break;
748 : 1 : case OptionsCategory::WALLET:
749 [ + - + - ]: 2 : usage += HelpMessageGroup("Wallet options:");
750 : 1 : break;
751 : 1 : case OptionsCategory::WALLET_DEBUG_TEST:
752 [ - + - - : 1 : if (show_debug) usage += HelpMessageGroup("Wallet debugging/testing options:");
- - ]
753 : : break;
754 : 1 : case OptionsCategory::CHAINPARAMS:
755 [ + - + - ]: 2 : usage += HelpMessageGroup("Chain selection options:");
756 : 1 : break;
757 : 0 : case OptionsCategory::GUI:
758 [ # # # # ]: 0 : usage += HelpMessageGroup("UI Options:");
759 : 0 : break;
760 : 1 : case OptionsCategory::COMMANDS:
761 [ + - + - ]: 2 : usage += HelpMessageGroup("Commands:");
762 : 1 : break;
763 : 0 : case OptionsCategory::REGISTER_COMMANDS:
764 [ # # # # ]: 0 : usage += HelpMessageGroup("Register Commands:");
765 : 0 : break;
766 : 0 : case OptionsCategory::CLI_COMMANDS:
767 [ # # # # ]: 0 : usage += HelpMessageGroup("CLI Commands:");
768 : 0 : break;
769 : : case OptionsCategory::COMMAND_OPTIONS:
770 : : case OptionsCategory::HIDDEN:
771 : : break;
772 : : } // no default case, so the compiler can warn about missing cases
773 : :
774 [ + + ]: 13 : if (category == OptionsCategory::COMMAND_OPTIONS) continue;
775 : :
776 : : // When we get to the hidden options, stop
777 [ + + ]: 12 : if (category == OptionsCategory::HIDDEN) break;
778 : :
779 [ + - + + ]: 196 : for (const auto& [arg_name, arg_info] : category_args) {
780 [ + - + + ]: 185 : if (show_debug || !(arg_info.m_flags & ArgsManager::DEBUG_ONLY)) {
781 [ - + - + : 288 : usage += HelpMessageOpt(arg_name, arg_info.m_help_param, arg_info.m_help_text);
- + + - ]
782 : :
783 [ + + ]: 144 : if (category == OptionsCategory::COMMANDS) {
784 : 1 : const auto cmd_args = m_command_args.find(arg_name);
785 [ - + ]: 1 : if (cmd_args == m_command_args.end()) continue;
786 [ + - ]: 1 : for_matching_cmd_opts(cmd_args->second, [&](const auto& cmdopt_name, const auto& cmdopt_info) {
787 [ - + - + : 2 : usage += HelpMessageOpt(cmdopt_name, cmdopt_info.m_help_param, cmdopt_info.m_help_text, /*subopt=*/true);
- + - + ]
788 : 1 : });
789 : : }
790 : : }
791 : : }
792 : : }
793 [ + - ]: 2 : return usage;
794 : 2 : }
795 : :
796 : 2491 : bool HelpRequested(const ArgsManager& args)
797 : : {
798 [ + - + - : 12453 : return args.IsArgSet("-?") || args.IsArgSet("-h") || args.IsArgSet("-help") || args.IsArgSet("-help-debug");
+ - + - +
- + + + -
+ - + - +
- + - + -
+ + + - -
- - - -
- ]
799 : : }
800 : :
801 : 3324 : void SetupHelpOptions(ArgsManager& args)
802 : : {
803 [ + - + - ]: 6648 : args.AddArg("-help", "Print this help message and exit (also -h or -?)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
804 [ + - ]: 3324 : args.AddHiddenArgs({"-h", "-?"});
805 : 3324 : }
806 : :
807 : 10 : std::string HelpMessageGroup(const std::string &message) {
808 [ + - ]: 30 : return std::string(message) + std::string("\n\n");
809 : : }
810 : :
811 : 145 : std::string HelpMessageOpt(std::string_view option, std::string_view help_param, std::string_view message, bool subopt)
812 : : {
813 : 145 : constexpr int screen_width = 79;
814 : 145 : int opt_indent = 2;
815 : 145 : int msg_indent = 7;
816 : :
817 [ + + ]: 145 : if (subopt) {
818 : 1 : int bump = msg_indent - opt_indent;
819 : 1 : opt_indent += bump; // opt_indent now at the old msg_indent level
820 : 1 : msg_indent += bump; // indent by the same amount
821 : : }
822 : 145 : int msg_width = screen_width - msg_indent;
823 : :
824 : 145 : return strprintf("%*s%s%s\n%*s%s\n\n",
825 : : opt_indent, "", option, help_param,
826 [ + - ]: 290 : msg_indent, "", FormatParagraph(message, msg_width, msg_indent));
827 : : }
828 : :
829 : : const std::vector<std::string> TEST_OPTIONS_DOC{
830 : : "addrman (use deterministic addrman)",
831 : : "reindex_after_failure_noninteractive_yes (When asked for a reindex after failure interactively, simulate as-if answered with 'yes')",
832 : : "bip94 (enforce BIP94 consensus rules)",
833 : : };
834 : :
835 : 4912 : bool HasTestOption(const ArgsManager& args, const std::string& test_option)
836 : : {
837 [ + - ]: 4912 : const auto options = args.GetArgs("-test");
838 [ - + - + : 58944 : return std::any_of(options.begin(), options.end(), [test_option](const auto& option) {
- + + - -
+ + - - +
+ - - + +
- ]
839 [ + + ]: 33 : return option == test_option;
840 : 4912 : });
841 : 4912 : }
842 : :
843 : 1267 : fs::path GetDefaultDataDir()
844 : : {
845 : : // Windows:
846 : : // old: C:\Users\Username\AppData\Roaming\Bitcoin
847 : : // new: C:\Users\Username\AppData\Local\Bitcoin
848 : : // macOS: ~/Library/Application Support/Bitcoin
849 : : // Unix-like: ~/.bitcoin
850 : : #ifdef WIN32
851 : : // Windows
852 : : // Check for existence of datadir in old location and keep it there
853 : : fs::path legacy_path = GetSpecialFolderPath(CSIDL_APPDATA) / "Bitcoin";
854 : : if (fs::exists(legacy_path)) return legacy_path;
855 : :
856 : : // Otherwise, fresh installs can start in the new, "proper" location
857 : : return GetSpecialFolderPath(CSIDL_LOCAL_APPDATA) / "Bitcoin";
858 : : #else
859 : 1267 : fs::path pathRet;
860 : 1267 : char* pszHome = getenv("HOME");
861 [ + - - + ]: 1267 : if (pszHome == nullptr || strlen(pszHome) == 0)
862 [ # # ]: 0 : pathRet = fs::path("/");
863 : : else
864 [ + - ]: 2534 : pathRet = fs::path(pszHome);
865 : : #ifdef __APPLE__
866 : : // macOS
867 : : return pathRet / "Library/Application Support/Bitcoin";
868 : : #else
869 : : // Unix-like
870 [ + - + - ]: 3801 : return pathRet / ".bitcoin";
871 : : #endif
872 : : #endif
873 : 1267 : }
874 : :
875 : 4750 : bool CheckDataDirOption(const ArgsManager& args)
876 : : {
877 [ + - + - ]: 9500 : const fs::path datadir{args.GetPathArg("-datadir")};
878 [ + + + - : 9495 : return datadir.empty() || fs::is_directory(fs::absolute(datadir));
+ - + + ]
879 : 4750 : }
880 : :
881 : 3631 : fs::path ArgsManager::GetConfigFilePath() const
882 : : {
883 : 3631 : LOCK(cs_args);
884 [ + - + - : 3631 : return *Assert(m_config_path);
+ - ]
885 : 3631 : }
886 : :
887 : 1 : void ArgsManager::SetConfigFilePath(fs::path path)
888 : : {
889 : 1 : LOCK(cs_args);
890 [ - + ]: 1 : assert(!m_config_path);
891 [ + - ]: 1 : m_config_path = path;
892 : 1 : }
893 : :
894 : 4595 : ChainType ArgsManager::GetChainType() const
895 : : {
896 : 4595 : std::variant<ChainType, std::string> arg = GetChainArg();
897 [ + - ]: 4595 : if (auto* parsed = std::get_if<ChainType>(&arg)) return *parsed;
898 [ # # # # : 0 : throw std::runtime_error(strprintf("Unknown chain %s.", std::get<std::string>(arg)));
# # ]
899 : 4595 : }
900 : :
901 : 6109 : std::string ArgsManager::GetChainTypeString() const
902 : : {
903 : 6109 : auto arg = GetChainArg();
904 [ + - + - ]: 5922 : if (auto* parsed = std::get_if<ChainType>(&arg)) return ChainTypeToString(*parsed);
905 [ - - - - ]: 5922 : return std::get<std::string>(arg);
906 : 5922 : }
907 : :
908 : 10704 : std::variant<ChainType, std::string> ArgsManager::GetChainArg() const
909 : : {
910 : 53520 : auto get_net = [&](const std::string& arg) {
911 : 42816 : LOCK(cs_args);
912 [ + - + - ]: 85632 : common::SettingsValue value = common::GetSetting(m_settings, /* section= */ "", SettingName(arg),
913 : : /* ignore_default_section_config= */ false,
914 : : /*ignore_nonpersistent=*/false,
915 [ + - ]: 42816 : /* get_chain_type= */ true);
916 [ + + - + : 42816 : return value.isNull() ? false : value.isBool() ? value.get_bool() : InterpretBool(value.get_str());
- - + - +
- ]
917 [ + - ]: 85632 : };
918 : :
919 [ + - ]: 10704 : const bool fRegTest = get_net("-regtest");
920 [ + - ]: 10704 : const bool fSigNet = get_net("-signet");
921 [ + - ]: 10704 : const bool fTestNet = get_net("-testnet");
922 [ + - ]: 10704 : const bool fTestNet4 = get_net("-testnet4");
923 [ + - ]: 10704 : const auto chain_arg = GetArg("-chain");
924 : :
925 [ + + ]: 10704 : if ((int)chain_arg.has_value() + (int)fRegTest + (int)fSigNet + (int)fTestNet + (int)fTestNet4 > 1) {
926 [ + - ]: 187 : throw std::runtime_error("Invalid combination of -regtest, -signet, -testnet, -testnet4 and -chain. Can use at most one.");
927 : : }
928 [ + + ]: 10517 : if (chain_arg) {
929 [ - + + - : 33 : if (auto parsed = ChainTypeFromString(*chain_arg)) return *parsed;
+ - ]
930 : : // Not a known string, so return original string
931 [ # # ]: 0 : return *chain_arg;
932 : : }
933 [ + + ]: 10484 : if (fRegTest) return ChainType::REGTEST;
934 [ + + ]: 1966 : if (fSigNet) return ChainType::SIGNET;
935 [ + + ]: 1821 : if (fTestNet) return ChainType::TESTNET;
936 [ + + ]: 1812 : if (fTestNet4) return ChainType::TESTNET4;
937 : 1473 : return ChainType::MAIN;
938 : 10517 : }
939 : :
940 : 1174523 : bool ArgsManager::UseDefaultSection(const std::string& arg) const
941 : : {
942 : 1174523 : AssertLockHeld(cs_args);
943 [ + + + + ]: 1174523 : return m_network == ChainTypeToString(ChainType::MAIN) || !m_network_only_args.contains(arg);
944 : : }
945 : :
946 : 924411 : common::SettingsValue ArgsManager::GetSetting_(const std::string& arg) const
947 : : {
948 : 924411 : AssertLockHeld(cs_args);
949 : 924411 : return common::GetSetting(
950 : 924411 : m_settings, m_network, SettingName(arg), !UseDefaultSection(arg),
951 [ + - ]: 1848822 : /*ignore_nonpersistent=*/false, /*get_chain_type=*/false);
952 : : }
953 : :
954 : 832019 : common::SettingsValue ArgsManager::GetSetting(const std::string& arg) const
955 : : {
956 : 832019 : LOCK(cs_args);
957 [ + - ]: 832019 : return GetSetting_(arg);
958 : 832019 : }
959 : :
960 : 250112 : std::vector<common::SettingsValue> ArgsManager::GetSettingsList(const std::string& arg) const
961 : : {
962 : 250112 : LOCK(cs_args);
963 [ + - + - : 500224 : return common::GetSettingsList(m_settings, m_network, SettingName(arg), !UseDefaultSection(arg));
+ - + - ]
964 : 250112 : }
965 : :
966 : 3779 : void ArgsManager::logArgsPrefix(
967 : : const std::string& prefix,
968 : : const std::string& section,
969 : : const std::map<std::string, std::vector<common::SettingsValue>>& args) const
970 : : {
971 : 3779 : AssertLockHeld(cs_args);
972 [ + + + - ]: 5036 : std::string section_str = section.empty() ? "" : "[" + section + "] ";
973 [ + + ]: 47803 : for (const auto& arg : args) {
974 [ + + ]: 89504 : for (const auto& value : arg.second) {
975 [ + - + - ]: 45480 : std::optional<unsigned int> flags = GetArgFlags_('-' + arg.first);
976 [ + - ]: 45480 : if (flags) {
977 [ + + + - : 45480 : std::string value_str = (*flags & SENSITIVE) ? "****" : value.write();
+ - ]
978 [ + - ]: 45480 : LogInfo("%s %s%s=%s\n", prefix, section_str, arg.first, value_str);
979 : 45480 : }
980 : : }
981 : : }
982 : 3779 : }
983 : :
984 : 1263 : void ArgsManager::LogArgs() const
985 : : {
986 : 1263 : LOCK(cs_args);
987 [ + + ]: 3779 : for (const auto& section : m_settings.ro_config) {
988 [ + - + - ]: 5032 : logArgsPrefix("Config file arg:", section.first, section.second);
989 : : }
990 [ + + ]: 1410 : for (const auto& setting : m_settings.rw_settings) {
991 [ + - + - ]: 147 : LogInfo("Setting file arg: %s = %s\n", setting.first, setting.second.write());
992 : : }
993 [ + - + - : 2526 : logArgsPrefix("Command-line arg:", "", m_settings.command_line_options);
+ - + - ]
994 : 1263 : }
|