fuzz coverage

Coverage Report

Created: 2025-09-17 22:41

/Users/eugenesiegel/btc/bitcoin/src/common/args.cpp
Line
Count
Source (jump to first uncovered line)
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 <logging.h>
11
#include <sync.h>
12
#include <tinyformat.h>
13
#include <univalue.h>
14
#include <util/chaintype.h>
15
#include <util/check.h>
16
#include <util/fs.h>
17
#include <util/fs_helpers.h>
18
#include <util/strencodings.h>
19
#include <util/string.h>
20
21
#ifdef WIN32
22
#include <codecvt>
23
#include <shellapi.h>
24
#include <shlobj.h>
25
#endif
26
27
#include <algorithm>
28
#include <cassert>
29
#include <cstdint>
30
#include <cstdlib>
31
#include <cstring>
32
#include <map>
33
#include <optional>
34
#include <stdexcept>
35
#include <string>
36
#include <utility>
37
#include <variant>
38
39
const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf";
40
const char * const BITCOIN_SETTINGS_FILENAME = "settings.json";
41
42
ArgsManager gArgs;
43
44
/**
45
 * Interpret a string argument as a boolean.
46
 *
47
 * The definition of LocaleIndependentAtoi<int>() requires that non-numeric string values
48
 * like "foo", return 0. This means that if a user unintentionally supplies a
49
 * non-integer argument here, the return value is always false. This means that
50
 * -foo=false does what the user probably expects, but -foo=true is well defined
51
 * but does not do what they probably expected.
52
 *
53
 * The return value of LocaleIndependentAtoi<int>(...) is zero when given input not
54
 * representable as an int.
55
 *
56
 * For a more extensive discussion of this topic (and a wide range of opinions
57
 * on the Right Way to change this code), see PR12713.
58
 */
59
static bool InterpretBool(const std::string& strValue)
60
155k
{
61
155k
    if (strValue.empty())
62
116k
        return true;
63
38.8k
    return (LocaleIndependentAtoi<int>(strValue) != 0);
64
155k
}
65
66
static std::string SettingName(const std::string& arg)
67
6.52M
{
68
6.52M
    return arg.size() > 0 && arg[0] == '-' ? arg.substr(1) : 
arg0
;
69
6.52M
}
70
71
/**
72
 * Parse "name", "section.name", "noname", "section.noname" settings keys.
73
 *
74
 * @note Where an option was negated can be later checked using the
75
 * IsArgNegated() method. One use case for this is to have a way to disable
76
 * options that are not normally boolean (e.g. using -nodebuglogfile to request
77
 * that debug log output is not sent to any file at all).
78
 */
79
KeyInfo InterpretKey(std::string key)
80
466k
{
81
466k
    KeyInfo result;
82
    // Split section name from key name for keys like "testnet.foo" or "regtest.bar"
83
466k
    size_t option_index = key.find('.');
84
466k
    if (option_index != std::string::npos) {
85
0
        result.section = key.substr(0, option_index);
86
0
        key.erase(0, option_index + 1);
87
0
    }
88
466k
    if (key.starts_with("no")) {
89
77.7k
        key.erase(0, 2);
90
77.7k
        result.negated = true;
91
77.7k
    }
92
466k
    result.name = key;
93
466k
    return result;
94
466k
}
95
96
/**
97
 * Interpret settings value based on registered flags.
98
 *
99
 * @param[in]   key      key information to know if key was negated
100
 * @param[in]   value    string value of setting to be parsed
101
 * @param[in]   flags    ArgsManager registered argument flags
102
 * @param[out]  error    Error description if settings value is not valid
103
 *
104
 * @return parsed settings value if it is valid, otherwise nullopt accompanied
105
 * by a descriptive error string
106
 */
107
std::optional<common::SettingsValue> InterpretValue(const KeyInfo& key, const std::string* value,
108
                                                  unsigned int flags, std::string& error)
109
466k
{
110
    // Return negated settings as false values.
111
466k
    if (key.negated) {
112
77.7k
        if (flags & ArgsManager::DISALLOW_NEGATION) {
113
0
            error = strprintf("Negating of -%s is meaningless and therefore forbidden", key.name);
Line
Count
Source
1172
0
#define strprintf tfm::format
114
0
            return std::nullopt;
115
0
        }
116
        // Double negatives like -nofoo=0 are supported (but discouraged)
117
77.7k
        if (value && 
!InterpretBool(*value)0
) {
118
0
            LogPrintf("Warning: parsed potentially confusing double-negative -%s=%s\n", key.name, *value);
Line
Count
Source
361
0
#define LogPrintf(...) LogInfo(__VA_ARGS__)
Line
Count
Source
356
0
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
350
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(std::source_location::current(), category, level, should_ratelimit, __VA_ARGS__)
119
0
            return true;
120
0
        }
121
77.7k
        return false;
122
77.7k
    }
123
388k
    if (!value && 
(flags & ArgsManager::DISALLOW_ELISION)155k
) {
124
0
        error = strprintf("Can not set -%s with no value. Please specify value with -%s=value.", key.name, key.name);
Line
Count
Source
1172
0
#define strprintf tfm::format
125
0
        return std::nullopt;
126
0
    }
127
388k
    return value ? 
*value233k
:
""155k
;
128
388k
}
129
130
// Define default constructor and destructor that are not inline, so code instantiating this class doesn't need to
131
// #include class definitions for all members.
132
// For example, m_settings has an internal dependency on univalue.
133
38.8k
ArgsManager::ArgsManager() = default;
134
38.8k
ArgsManager::~ArgsManager() = default;
135
136
std::set<std::string> ArgsManager::GetUnsuitableSectionOnlyArgs() const
137
38.8k
{
138
38.8k
    std::set<std::string> unsuitables;
139
140
38.8k
    LOCK(cs_args);
Line
Count
Source
259
38.8k
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
38.8k
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
38.8k
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
38.8k
#define PASTE(x, y) x ## y
141
142
    // if there's no section selected, don't worry
143
38.8k
    if (m_network.empty()) 
return std::set<std::string> {}0
;
144
145
    // if it's okay to use the default section for this network, don't worry
146
38.8k
    if (m_network == ChainTypeToString(ChainType::MAIN)) 
return std::set<std::string> {}0
;
147
148
311k
    
for (const auto& arg : m_network_only_args)38.8k
{
149
311k
        if (OnlyHasDefaultSectionSetting(m_settings, m_network, SettingName(arg))) {
150
0
            unsuitables.insert(arg);
151
0
        }
152
311k
    }
153
38.8k
    return unsuitables;
154
38.8k
}
155
156
std::list<SectionInfo> ArgsManager::GetUnrecognizedSections() const
157
38.8k
{
158
    // Section names to be recognized in the config file.
159
38.8k
    static const std::set<std::string> available_sections{
160
38.8k
        ChainTypeToString(ChainType::REGTEST),
161
38.8k
        ChainTypeToString(ChainType::SIGNET),
162
38.8k
        ChainTypeToString(ChainType::TESTNET),
163
38.8k
        ChainTypeToString(ChainType::TESTNET4),
164
38.8k
        ChainTypeToString(ChainType::MAIN),
165
38.8k
    };
166
167
38.8k
    LOCK(cs_args);
Line
Count
Source
259
38.8k
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
38.8k
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
38.8k
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
38.8k
#define PASTE(x, y) x ## y
168
38.8k
    std::list<SectionInfo> unrecognized = m_config_sections;
169
38.8k
    unrecognized.remove_if([](const SectionInfo& appeared)
{ return available_sections.find(appeared.m_name) != available_sections.end(); }0
);
170
38.8k
    return unrecognized;
171
38.8k
}
172
173
void ArgsManager::SelectConfigNetwork(const std::string& network)
174
38.8k
{
175
38.8k
    LOCK(cs_args);
Line
Count
Source
259
38.8k
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
38.8k
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
38.8k
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
38.8k
#define PASTE(x, y) x ## y
176
38.8k
    m_network = network;
177
38.8k
}
178
179
bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::string& error)
180
38.8k
{
181
38.8k
    LOCK(cs_args);
Line
Count
Source
259
38.8k
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
38.8k
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
38.8k
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
38.8k
#define PASTE(x, y) x ## y
182
38.8k
    m_settings.command_line_options.clear();
183
184
505k
    for (int i = 1; i < argc; 
i++466k
) {
185
466k
        std::string key(argv[i]);
186
187
466k
#ifdef __APPLE__
188
        // At the first time when a user gets the "App downloaded from the
189
        // internet" warning, and clicks the Open button, macOS passes
190
        // a unique process serial number (PSN) as -psn_... command-line
191
        // argument, which we filter out.
192
466k
        if (key.starts_with("-psn_")) 
continue0
;
193
466k
#endif
194
195
466k
        if (key == "-") 
break0
; //bitcoin-tx using stdin
196
466k
        std::optional<std::string> val;
197
466k
        size_t is_index = key.find('=');
198
466k
        if (is_index != std::string::npos) {
199
233k
            val = key.substr(is_index + 1);
200
233k
            key.erase(is_index);
201
233k
        }
202
#ifdef WIN32
203
        key = ToLower(key);
204
        if (key[0] == '/')
205
            key[0] = '-';
206
#endif
207
208
466k
        if (key[0] != '-') {
209
0
            if (!m_accept_any_command && m_command.empty()) {
210
                // The first non-dash arg is a registered command
211
0
                std::optional<unsigned int> flags = GetArgFlags(key);
212
0
                if (!flags || !(*flags & ArgsManager::COMMAND)) {
213
0
                    error = strprintf("Invalid command '%s'", argv[i]);
Line
Count
Source
1172
0
#define strprintf tfm::format
214
0
                    return false;
215
0
                }
216
0
            }
217
0
            m_command.push_back(key);
218
0
            while (++i < argc) {
219
                // The remaining args are command args
220
0
                m_command.emplace_back(argv[i]);
221
0
            }
222
0
            break;
223
0
        }
224
225
        // Transform --foo to -foo
226
466k
        if (key.length() > 1 && key[1] == '-')
227
0
            key.erase(0, 1);
228
229
        // Transform -foo to foo
230
466k
        key.erase(0, 1);
231
466k
        KeyInfo keyinfo = InterpretKey(key);
232
466k
        std::optional<unsigned int> flags = GetArgFlags('-' + keyinfo.name);
233
234
        // Unknown command line options and command line options with dot
235
        // characters (which are returned from InterpretKey with nonempty
236
        // section strings) are not valid.
237
466k
        if (!flags || !keyinfo.section.empty()) {
238
0
            error = strprintf("Invalid parameter %s", argv[i]);
Line
Count
Source
1172
0
#define strprintf tfm::format
239
0
            return false;
240
0
        }
241
242
466k
        std::optional<common::SettingsValue> value = InterpretValue(keyinfo, val ? 
&*val233k
:
nullptr233k
, *flags, error);
243
466k
        if (!value) 
return false0
;
244
245
466k
        m_settings.command_line_options[keyinfo.name].push_back(*value);
246
466k
    }
247
248
    // we do not allow -includeconf from command line, only -noincludeconf
249
38.8k
    if (auto* includes = common::FindKey(m_settings.command_line_options, "includeconf")) {
250
0
        const common::SettingsSpan values{*includes};
251
        // Range may be empty if -noincludeconf was passed
252
0
        if (!values.empty()) {
253
0
            error = "-includeconf cannot be used from commandline; -includeconf=" + values.begin()->write();
254
0
            return false; // pick first value as example
255
0
        }
256
0
    }
257
38.8k
    return true;
258
38.8k
}
259
260
std::optional<unsigned int> ArgsManager::GetArgFlags(const std::string& name) const
261
466k
{
262
466k
    LOCK(cs_args);
Line
Count
Source
259
466k
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
466k
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
466k
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
466k
#define PASTE(x, y) x ## y
263
2.17M
    for (const auto& arg_map : m_available_args) {
264
2.17M
        const auto search = arg_map.second.find(name);
265
2.17M
        if (search != arg_map.second.end()) {
266
466k
            return search->second.m_flags;
267
466k
        }
268
2.17M
    }
269
0
    return std::nullopt;
270
466k
}
271
272
fs::path ArgsManager::GetPathArg(std::string arg, const fs::path& default_value) const
273
233k
{
274
233k
    if (IsArgNegated(arg)) 
return fs::path{}38.8k
;
275
194k
    std::string path_str = GetArg(arg, "");
276
194k
    if (path_str.empty()) 
return default_value0
;
277
194k
    fs::path result = fs::PathFromString(path_str).lexically_normal();
278
    // Remove trailing slash, if present.
279
194k
    return result.has_filename() ? result : 
result.parent_path()0
;
280
194k
}
281
282
fs::path ArgsManager::GetBlocksDirPath() const
283
116k
{
284
116k
    LOCK(cs_args);
Line
Count
Source
259
116k
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
116k
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
116k
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
116k
#define PASTE(x, y) x ## y
285
116k
    fs::path& path = m_cached_blocks_path;
286
287
    // Cache the path to avoid calling fs::create_directories on every call of
288
    // this function
289
116k
    if (!path.empty()) 
return path38.8k
;
290
291
77.7k
    if (IsArgSet("-blocksdir")) {
292
0
        path = fs::absolute(GetPathArg("-blocksdir"));
293
0
        if (!fs::is_directory(path)) {
294
0
            path = "";
295
0
            return path;
296
0
        }
297
77.7k
    } else {
298
77.7k
        path = GetDataDirBase();
299
77.7k
    }
300
301
77.7k
    path /= fs::PathFromString(BaseParams().DataDir());
302
77.7k
    path /= "blocks";
303
77.7k
    fs::create_directories(path);
304
77.7k
    return path;
305
77.7k
}
306
307
fs::path ArgsManager::GetDataDir(bool net_specific) const
308
272k
{
309
272k
    LOCK(cs_args);
Line
Count
Source
259
272k
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
272k
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
272k
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
272k
#define PASTE(x, y) x ## y
310
272k
    fs::path& path = net_specific ? 
m_cached_network_datadir_path155k
:
m_cached_datadir_path116k
;
311
312
    // Used cached path if available
313
272k
    if (!path.empty()) 
return path116k
;
314
315
155k
    const fs::path datadir{GetPathArg("-datadir")};
316
155k
    if (!datadir.empty()) {
317
155k
        path = fs::absolute(datadir);
318
155k
        if (!fs::is_directory(path)) {
319
0
            path = "";
320
0
            return path;
321
0
        }
322
155k
    } else {
323
0
        path = GetDefaultDataDir();
324
0
    }
325
326
155k
    if (net_specific && 
!BaseParams().DataDir().empty()77.7k
) {
327
77.7k
        path /= fs::PathFromString(BaseParams().DataDir());
328
77.7k
    }
329
330
155k
    return path;
331
155k
}
332
333
void ArgsManager::ClearPathCache()
334
38.8k
{
335
38.8k
    LOCK(cs_args);
Line
Count
Source
259
38.8k
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
38.8k
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
38.8k
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
38.8k
#define PASTE(x, y) x ## y
336
337
38.8k
    m_cached_datadir_path = fs::path();
338
38.8k
    m_cached_network_datadir_path = fs::path();
339
38.8k
    m_cached_blocks_path = fs::path();
340
38.8k
}
341
342
std::optional<const ArgsManager::Command> ArgsManager::GetCommand() const
343
0
{
344
0
    Command ret;
345
0
    LOCK(cs_args);
Line
Count
Source
259
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
346
0
    auto it = m_command.begin();
347
0
    if (it == m_command.end()) {
348
        // No command was passed
349
0
        return std::nullopt;
350
0
    }
351
0
    if (!m_accept_any_command) {
352
        // The registered command
353
0
        ret.command = *(it++);
354
0
    }
355
0
    while (it != m_command.end()) {
356
        // The unregistered command and args (if any)
357
0
        ret.args.push_back(*(it++));
358
0
    }
359
0
    return ret;
360
0
}
361
362
std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg) const
363
652k
{
364
652k
    std::vector<std::string> result;
365
652k
    for (const common::SettingsValue& value : GetSettingsList(strArg)) {
366
116k
        result.push_back(value.isFalse() ? 
"0"0
: value.isTrue() ?
"1"0
: value.get_str());
367
116k
    }
368
652k
    return result;
369
652k
}
370
371
bool ArgsManager::IsArgSet(const std::string& strArg) const
372
388k
{
373
388k
    return !GetSetting(strArg).isNull();
374
388k
}
375
376
bool ArgsManager::GetSettingsPath(fs::path* filepath, bool temp, bool backup) const
377
0
{
378
0
    fs::path settings = GetPathArg("-settings", BITCOIN_SETTINGS_FILENAME);
379
0
    if (settings.empty()) {
380
0
        return false;
381
0
    }
382
0
    if (backup) {
383
0
        settings += ".bak";
384
0
    }
385
0
    if (filepath) {
386
0
        *filepath = fsbridge::AbsPathJoin(GetDataDirNet(), temp ? settings + ".tmp" : settings);
387
0
    }
388
0
    return true;
389
0
}
390
391
static void SaveErrors(const std::vector<std::string> errors, std::vector<std::string>* error_out)
392
0
{
393
0
    for (const auto& error : errors) {
394
0
        if (error_out) {
395
0
            error_out->emplace_back(error);
396
0
        } else {
397
0
            LogPrintf("%s\n", error);
Line
Count
Source
361
0
#define LogPrintf(...) LogInfo(__VA_ARGS__)
Line
Count
Source
356
0
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
350
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(std::source_location::current(), category, level, should_ratelimit, __VA_ARGS__)
398
0
        }
399
0
    }
400
0
}
401
402
bool ArgsManager::ReadSettingsFile(std::vector<std::string>* errors)
403
0
{
404
0
    fs::path path;
405
0
    if (!GetSettingsPath(&path, /* temp= */ false)) {
406
0
        return true; // Do nothing if settings file disabled.
407
0
    }
408
409
0
    LOCK(cs_args);
Line
Count
Source
259
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
410
0
    m_settings.rw_settings.clear();
411
0
    std::vector<std::string> read_errors;
412
0
    if (!common::ReadSettings(path, m_settings.rw_settings, read_errors)) {
413
0
        SaveErrors(read_errors, errors);
414
0
        return false;
415
0
    }
416
0
    for (const auto& setting : m_settings.rw_settings) {
417
0
        KeyInfo key = InterpretKey(setting.first); // Split setting key into section and argname
418
0
        if (!GetArgFlags('-' + key.name)) {
419
0
            LogPrintf("Ignoring unknown rw_settings value %s\n", setting.first);
Line
Count
Source
361
0
#define LogPrintf(...) LogInfo(__VA_ARGS__)
Line
Count
Source
356
0
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
350
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(std::source_location::current(), category, level, should_ratelimit, __VA_ARGS__)
420
0
        }
421
0
    }
422
0
    return true;
423
0
}
424
425
bool ArgsManager::WriteSettingsFile(std::vector<std::string>* errors, bool backup) const
426
0
{
427
0
    fs::path path, path_tmp;
428
0
    if (!GetSettingsPath(&path, /*temp=*/false, backup) || !GetSettingsPath(&path_tmp, /*temp=*/true, backup)) {
429
0
        throw std::logic_error("Attempt to write settings file when dynamic settings are disabled.");
430
0
    }
431
432
0
    LOCK(cs_args);
Line
Count
Source
259
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
433
0
    std::vector<std::string> write_errors;
434
0
    if (!common::WriteSettings(path_tmp, m_settings.rw_settings, write_errors)) {
435
0
        SaveErrors(write_errors, errors);
436
0
        return false;
437
0
    }
438
0
    if (!RenameOver(path_tmp, path)) {
439
0
        SaveErrors({strprintf("Failed renaming settings file %s to %s\n", fs::PathToString(path_tmp), fs::PathToString(path))}, errors);
Line
Count
Source
1172
0
#define strprintf tfm::format
440
0
        return false;
441
0
    }
442
0
    return true;
443
0
}
444
445
common::SettingsValue ArgsManager::GetPersistentSetting(const std::string& name) const
446
0
{
447
0
    LOCK(cs_args);
Line
Count
Source
259
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
448
0
    return common::GetSetting(m_settings, m_network, name, !UseDefaultSection("-" + name),
449
0
        /*ignore_nonpersistent=*/true, /*get_chain_type=*/false);
450
0
}
451
452
bool ArgsManager::IsArgNegated(const std::string& strArg) const
453
272k
{
454
272k
    return GetSetting(strArg).isFalse();
455
272k
}
456
457
std::string ArgsManager::GetArg(const std::string& strArg, const std::string& strDefault) const
458
233k
{
459
233k
    return GetArg(strArg).value_or(strDefault);
460
233k
}
461
462
std::optional<std::string> ArgsManager::GetArg(const std::string& strArg) const
463
622k
{
464
622k
    const common::SettingsValue value = GetSetting(strArg);
465
622k
    return SettingToString(value);
466
622k
}
467
468
std::optional<std::string> SettingToString(const common::SettingsValue& value)
469
622k
{
470
622k
    if (value.isNull()) 
return std::nullopt427k
;
471
194k
    if (value.isFalse()) 
return "0"0
;
472
194k
    if (value.isTrue()) 
return "1"0
;
473
194k
    if (value.isNum()) 
return value.getValStr()0
;
474
194k
    return value.get_str();
475
194k
}
476
477
std::string SettingToString(const common::SettingsValue& value, const std::string& strDefault)
478
0
{
479
0
    return SettingToString(value).value_or(strDefault);
480
0
}
481
482
int64_t ArgsManager::GetIntArg(const std::string& strArg, int64_t nDefault) const
483
831k
{
484
831k
    return GetIntArg(strArg).value_or(nDefault);
485
831k
}
486
487
std::optional<int64_t> ArgsManager::GetIntArg(const std::string& strArg) const
488
1.45M
{
489
1.45M
    const common::SettingsValue value = GetSetting(strArg);
490
1.45M
    return SettingToInt(value);
491
1.45M
}
492
493
std::optional<int64_t> SettingToInt(const common::SettingsValue& value)
494
1.45M
{
495
1.45M
    if (value.isNull()) 
return std::nullopt1.41M
;
496
38.8k
    if (value.isFalse()) 
return 00
;
497
38.8k
    if (value.isTrue()) 
return 10
;
498
38.8k
    if (value.isNum()) 
return value.getInt<int64_t>()0
;
499
38.8k
    return LocaleIndependentAtoi<int64_t>(value.get_str());
500
38.8k
}
501
502
int64_t SettingToInt(const common::SettingsValue& value, int64_t nDefault)
503
0
{
504
0
    return SettingToInt(value).value_or(nDefault);
505
0
}
506
507
bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault) const
508
2.19M
{
509
2.19M
    return GetBoolArg(strArg).value_or(fDefault);
510
2.19M
}
511
512
std::optional<bool> ArgsManager::GetBoolArg(const std::string& strArg) const
513
2.54M
{
514
2.54M
    const common::SettingsValue value = GetSetting(strArg);
515
2.54M
    return SettingToBool(value);
516
2.54M
}
517
518
std::optional<bool> SettingToBool(const common::SettingsValue& value)
519
2.54M
{
520
2.54M
    if (value.isNull()) 
return std::nullopt2.39M
;
521
155k
    if (value.isBool()) 
return value.get_bool()0
;
522
155k
    return InterpretBool(value.get_str());
523
155k
}
524
525
bool SettingToBool(const common::SettingsValue& value, bool fDefault)
526
0
{
527
0
    return SettingToBool(value).value_or(fDefault);
528
0
}
529
530
bool ArgsManager::SoftSetArg(const std::string& strArg, const std::string& strValue)
531
0
{
532
0
    LOCK(cs_args);
Line
Count
Source
259
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
533
0
    if (IsArgSet(strArg)) return false;
534
0
    ForceSetArg(strArg, strValue);
535
0
    return true;
536
0
}
537
538
bool ArgsManager::SoftSetBoolArg(const std::string& strArg, bool fValue)
539
0
{
540
0
    if (fValue)
541
0
        return SoftSetArg(strArg, std::string("1"));
542
0
    else
543
0
        return SoftSetArg(strArg, std::string("0"));
544
0
}
545
546
void ArgsManager::ForceSetArg(const std::string& strArg, const std::string& strValue)
547
116k
{
548
116k
    LOCK(cs_args);
Line
Count
Source
259
116k
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
116k
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
116k
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
116k
#define PASTE(x, y) x ## y
549
116k
    m_settings.forced_settings[SettingName(strArg)] = strValue;
550
116k
}
551
552
void ArgsManager::AddCommand(const std::string& cmd, const std::string& help)
553
0
{
554
0
    Assert(cmd.find('=') == std::string::npos);
Line
Count
Source
106
0
#define Assert(val) inline_assertion_check<true>(val, __FILE__, __LINE__, __func__, #val)
555
0
    Assert(cmd.at(0) != '-');
Line
Count
Source
106
0
#define Assert(val) inline_assertion_check<true>(val, __FILE__, __LINE__, __func__, #val)
556
557
0
    LOCK(cs_args);
Line
Count
Source
259
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
558
0
    m_accept_any_command = false; // latch to false
559
0
    std::map<std::string, Arg>& arg_map = m_available_args[OptionsCategory::COMMANDS];
560
0
    auto ret = arg_map.emplace(cmd, Arg{"", help, ArgsManager::COMMAND});
561
0
    Assert(ret.second); // Fail on duplicate commands
Line
Count
Source
106
0
#define Assert(val) inline_assertion_check<true>(val, __FILE__, __LINE__, __func__, #val)
562
0
}
563
564
void ArgsManager::AddArg(const std::string& name, const std::string& help, unsigned int flags, const OptionsCategory& cat)
565
7.46M
{
566
7.46M
    Assert((flags & ArgsManager::COMMAND) == 0); // use AddCommand
Line
Count
Source
106
7.46M
#define Assert(val) inline_assertion_check<true>(val, __FILE__, __LINE__, __func__, #val)
567
568
    // Split arg name from its help param
569
7.46M
    size_t eq_index = name.find('=');
570
7.46M
    if (eq_index == std::string::npos) {
571
3.34M
        eq_index = name.size();
572
3.34M
    }
573
7.46M
    std::string arg_name = name.substr(0, eq_index);
574
575
7.46M
    LOCK(cs_args);
Line
Count
Source
259
7.46M
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
7.46M
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
7.46M
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
7.46M
#define PASTE(x, y) x ## y
576
7.46M
    std::map<std::string, Arg>& arg_map = m_available_args[cat];
577
7.46M
    auto ret = arg_map.emplace(arg_name, Arg{name.substr(eq_index, name.size() - eq_index), help, flags});
578
7.46M
    assert(ret.second); // Make sure an insertion actually happened
579
580
7.46M
    if (flags & ArgsManager::NETWORK_ONLY) {
581
311k
        m_network_only_args.emplace(arg_name);
582
311k
    }
583
7.46M
}
584
585
void ArgsManager::AddHiddenArgs(const std::vector<std::string>& names)
586
77.7k
{
587
777k
    for (const std::string& name : names) {
588
777k
        AddArg(name, "", ArgsManager::ALLOW_ANY, OptionsCategory::HIDDEN);
589
777k
    }
590
77.7k
}
591
592
void ArgsManager::ClearArgs()
593
38.8k
{
594
38.8k
    LOCK(cs_args);
Line
Count
Source
259
38.8k
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
38.8k
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
38.8k
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
38.8k
#define PASTE(x, y) x ## y
595
38.8k
    m_settings = {};
596
38.8k
    m_available_args.clear();
597
38.8k
    m_network_only_args.clear();
598
38.8k
}
599
600
void ArgsManager::CheckMultipleCLIArgs() const
601
0
{
602
0
    LOCK(cs_args);
Line
Count
Source
259
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
603
0
    std::vector<std::string> found{};
604
0
    auto cmds = m_available_args.find(OptionsCategory::CLI_COMMANDS);
605
0
    if (cmds != m_available_args.end()) {
606
0
        for (const auto& [cmd, argspec] : cmds->second) {
607
0
            if (IsArgSet(cmd)) {
608
0
                found.push_back(cmd);
609
0
            }
610
0
        }
611
0
        if (found.size() > 1) {
612
0
            throw std::runtime_error(strprintf("Only one of %s may be specified.", util::Join(found, ", ")));
Line
Count
Source
1172
0
#define strprintf tfm::format
613
0
        }
614
0
    }
615
0
}
616
617
std::string ArgsManager::GetHelpMessage() const
618
0
{
619
0
    const bool show_debug = GetBoolArg("-help-debug", false);
620
621
0
    std::string usage;
622
0
    LOCK(cs_args);
Line
Count
Source
259
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
623
0
    for (const auto& arg_map : m_available_args) {
624
0
        switch(arg_map.first) {
625
0
            case OptionsCategory::OPTIONS:
626
0
                usage += HelpMessageGroup("Options:");
627
0
                break;
628
0
            case OptionsCategory::CONNECTION:
629
0
                usage += HelpMessageGroup("Connection options:");
630
0
                break;
631
0
            case OptionsCategory::ZMQ:
632
0
                usage += HelpMessageGroup("ZeroMQ notification options:");
633
0
                break;
634
0
            case OptionsCategory::DEBUG_TEST:
635
0
                usage += HelpMessageGroup("Debugging/Testing options:");
636
0
                break;
637
0
            case OptionsCategory::NODE_RELAY:
638
0
                usage += HelpMessageGroup("Node relay options:");
639
0
                break;
640
0
            case OptionsCategory::BLOCK_CREATION:
641
0
                usage += HelpMessageGroup("Block creation options:");
642
0
                break;
643
0
            case OptionsCategory::RPC:
644
0
                usage += HelpMessageGroup("RPC server options:");
645
0
                break;
646
0
            case OptionsCategory::IPC:
647
0
                usage += HelpMessageGroup("IPC interprocess connection options:");
648
0
                break;
649
0
            case OptionsCategory::WALLET:
650
0
                usage += HelpMessageGroup("Wallet options:");
651
0
                break;
652
0
            case OptionsCategory::WALLET_DEBUG_TEST:
653
0
                if (show_debug) usage += HelpMessageGroup("Wallet debugging/testing options:");
654
0
                break;
655
0
            case OptionsCategory::CHAINPARAMS:
656
0
                usage += HelpMessageGroup("Chain selection options:");
657
0
                break;
658
0
            case OptionsCategory::GUI:
659
0
                usage += HelpMessageGroup("UI Options:");
660
0
                break;
661
0
            case OptionsCategory::COMMANDS:
662
0
                usage += HelpMessageGroup("Commands:");
663
0
                break;
664
0
            case OptionsCategory::REGISTER_COMMANDS:
665
0
                usage += HelpMessageGroup("Register Commands:");
666
0
                break;
667
0
            case OptionsCategory::CLI_COMMANDS:
668
0
                usage += HelpMessageGroup("CLI Commands:");
669
0
                break;
670
0
            default:
671
0
                break;
672
0
        }
673
674
        // When we get to the hidden options, stop
675
0
        if (arg_map.first == OptionsCategory::HIDDEN) break;
676
677
0
        for (const auto& arg : arg_map.second) {
678
0
            if (show_debug || !(arg.second.m_flags & ArgsManager::DEBUG_ONLY)) {
679
0
                std::string name;
680
0
                if (arg.second.m_help_param.empty()) {
681
0
                    name = arg.first;
682
0
                } else {
683
0
                    name = arg.first + arg.second.m_help_param;
684
0
                }
685
0
                usage += HelpMessageOpt(name, arg.second.m_help_text);
686
0
            }
687
0
        }
688
0
    }
689
0
    return usage;
690
0
}
691
692
bool HelpRequested(const ArgsManager& args)
693
0
{
694
0
    return args.IsArgSet("-?") || args.IsArgSet("-h") || args.IsArgSet("-help") || args.IsArgSet("-help-debug");
695
0
}
696
697
void SetupHelpOptions(ArgsManager& args)
698
38.8k
{
699
38.8k
    args.AddArg("-help", "Print this help message and exit (also -h or -?)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
700
38.8k
    args.AddHiddenArgs({"-h", "-?"});
701
38.8k
}
702
703
static const int screenWidth = 79;
704
static const int optIndent = 2;
705
static const int msgIndent = 7;
706
707
0
std::string HelpMessageGroup(const std::string &message) {
708
0
    return std::string(message) + std::string("\n\n");
709
0
}
710
711
0
std::string HelpMessageOpt(const std::string &option, const std::string &message) {
712
0
    return std::string(optIndent,' ') + std::string(option) +
713
0
           std::string("\n") + std::string(msgIndent,' ') +
714
0
           FormatParagraph(message, screenWidth - msgIndent, msgIndent) +
715
0
           std::string("\n\n");
716
0
}
717
718
const std::vector<std::string> TEST_OPTIONS_DOC{
719
    "addrman (use deterministic addrman)",
720
    "reindex_after_failure_noninteractive_yes (When asked for a reindex after failure interactively, simulate as-if answered with 'yes')",
721
    "bip94 (enforce BIP94 consensus rules)",
722
};
723
724
bool HasTestOption(const ArgsManager& args, const std::string& test_option)
725
77.7k
{
726
77.7k
    const auto options = args.GetArgs("-test");
727
77.7k
    return std::any_of(options.begin(), options.end(), [test_option](const auto& option) {
728
0
        return option == test_option;
729
0
    });
730
77.7k
}
731
732
fs::path GetDefaultDataDir()
733
0
{
734
    // Windows:
735
    //   old: C:\Users\Username\AppData\Roaming\Bitcoin
736
    //   new: C:\Users\Username\AppData\Local\Bitcoin
737
    // macOS: ~/Library/Application Support/Bitcoin
738
    // Unix-like: ~/.bitcoin
739
#ifdef WIN32
740
    // Windows
741
    // Check for existence of datadir in old location and keep it there
742
    fs::path legacy_path = GetSpecialFolderPath(CSIDL_APPDATA) / "Bitcoin";
743
    if (fs::exists(legacy_path)) return legacy_path;
744
745
    // Otherwise, fresh installs can start in the new, "proper" location
746
    return GetSpecialFolderPath(CSIDL_LOCAL_APPDATA) / "Bitcoin";
747
#else
748
0
    fs::path pathRet;
749
0
    char* pszHome = getenv("HOME");
750
0
    if (pszHome == nullptr || strlen(pszHome) == 0)
751
0
        pathRet = fs::path("/");
752
0
    else
753
0
        pathRet = fs::path(pszHome);
754
0
#ifdef __APPLE__
755
    // macOS
756
0
    return pathRet / "Library/Application Support/Bitcoin";
757
#else
758
    // Unix-like
759
    return pathRet / ".bitcoin";
760
#endif
761
0
#endif
762
0
}
763
764
bool CheckDataDirOption(const ArgsManager& args)
765
0
{
766
0
    const fs::path datadir{args.GetPathArg("-datadir")};
767
0
    return datadir.empty() || fs::is_directory(fs::absolute(datadir));
768
0
}
769
770
fs::path ArgsManager::GetConfigFilePath() const
771
0
{
772
0
    LOCK(cs_args);
Line
Count
Source
259
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
773
0
    return *Assert(m_config_path);
Line
Count
Source
106
0
#define Assert(val) inline_assertion_check<true>(val, __FILE__, __LINE__, __func__, #val)
774
0
}
775
776
void ArgsManager::SetConfigFilePath(fs::path path)
777
0
{
778
0
    LOCK(cs_args);
Line
Count
Source
259
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
779
0
    assert(!m_config_path);
780
0
    m_config_path = path;
781
0
}
782
783
ChainType ArgsManager::GetChainType() const
784
38.8k
{
785
38.8k
    std::variant<ChainType, std::string> arg = GetChainArg();
786
38.8k
    if (auto* parsed = std::get_if<ChainType>(&arg)) return *parsed;
787
0
    throw std::runtime_error(strprintf("Unknown chain %s.", std::get<std::string>(arg)));
Line
Count
Source
1172
0
#define strprintf tfm::format
788
38.8k
}
789
790
std::string ArgsManager::GetChainTypeString() const
791
0
{
792
0
    auto arg = GetChainArg();
793
0
    if (auto* parsed = std::get_if<ChainType>(&arg)) return ChainTypeToString(*parsed);
794
0
    return std::get<std::string>(arg);
795
0
}
796
797
std::variant<ChainType, std::string> ArgsManager::GetChainArg() const
798
38.8k
{
799
155k
    auto get_net = [&](const std::string& arg) {
800
155k
        LOCK(cs_args);
Line
Count
Source
259
155k
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
155k
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
155k
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
155k
#define PASTE(x, y) x ## y
801
155k
        common::SettingsValue value = common::GetSetting(m_settings, /* section= */ "", SettingName(arg),
802
155k
            /* ignore_default_section_config= */ false,
803
155k
            /*ignore_nonpersistent=*/false,
804
155k
            /* get_chain_type= */ true);
805
155k
        return value.isNull() ? false : 
value.isBool()0
?
value.get_bool()0
:
InterpretBool(value.get_str())0
;
806
155k
    };
807
808
38.8k
    const bool fRegTest = get_net("-regtest");
809
38.8k
    const bool fSigNet  = get_net("-signet");
810
38.8k
    const bool fTestNet = get_net("-testnet");
811
38.8k
    const bool fTestNet4 = get_net("-testnet4");
812
38.8k
    const auto chain_arg = GetArg("-chain");
813
814
38.8k
    if ((int)chain_arg.has_value() + (int)fRegTest + (int)fSigNet + (int)fTestNet + (int)fTestNet4 > 1) {
815
0
        throw std::runtime_error("Invalid combination of -regtest, -signet, -testnet, -testnet4 and -chain. Can use at most one.");
816
0
    }
817
38.8k
    if (chain_arg) {
818
0
        if (auto parsed = ChainTypeFromString(*chain_arg)) return *parsed;
819
        // Not a known string, so return original string
820
0
        return *chain_arg;
821
0
    }
822
38.8k
    if (fRegTest) 
return ChainType::REGTEST0
;
823
38.8k
    if (fSigNet) 
return ChainType::SIGNET0
;
824
38.8k
    if (fTestNet) 
return ChainType::TESTNET0
;
825
38.8k
    if (fTestNet4) 
return ChainType::TESTNET40
;
826
38.8k
    return ChainType::MAIN;
827
38.8k
}
828
829
bool ArgsManager::UseDefaultSection(const std::string& arg) const
830
5.93M
{
831
5.93M
    return m_network == ChainTypeToString(ChainType::MAIN) || m_network_only_args.count(arg) == 0;
832
5.93M
}
833
834
common::SettingsValue ArgsManager::GetSetting(const std::string& arg) const
835
5.28M
{
836
5.28M
    LOCK(cs_args);
Line
Count
Source
259
5.28M
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
5.28M
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
5.28M
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
5.28M
#define PASTE(x, y) x ## y
837
5.28M
    return common::GetSetting(
838
5.28M
        m_settings, m_network, SettingName(arg), !UseDefaultSection(arg),
839
5.28M
        /*ignore_nonpersistent=*/false, /*get_chain_type=*/false);
840
5.28M
}
841
842
std::vector<common::SettingsValue> ArgsManager::GetSettingsList(const std::string& arg) const
843
652k
{
844
652k
    LOCK(cs_args);
Line
Count
Source
259
652k
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
652k
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
652k
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
652k
#define PASTE(x, y) x ## y
845
652k
    return common::GetSettingsList(m_settings, m_network, SettingName(arg), !UseDefaultSection(arg));
846
652k
}
847
848
void ArgsManager::logArgsPrefix(
849
    const std::string& prefix,
850
    const std::string& section,
851
    const std::map<std::string, std::vector<common::SettingsValue>>& args) const
852
0
{
853
0
    std::string section_str = section.empty() ? "" : "[" + section + "] ";
854
0
    for (const auto& arg : args) {
855
0
        for (const auto& value : arg.second) {
856
0
            std::optional<unsigned int> flags = GetArgFlags('-' + arg.first);
857
0
            if (flags) {
858
0
                std::string value_str = (*flags & SENSITIVE) ? "****" : value.write();
859
0
                LogPrintf("%s %s%s=%s\n", prefix, section_str, arg.first, value_str);
Line
Count
Source
361
0
#define LogPrintf(...) LogInfo(__VA_ARGS__)
Line
Count
Source
356
0
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
350
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(std::source_location::current(), category, level, should_ratelimit, __VA_ARGS__)
860
0
            }
861
0
        }
862
0
    }
863
0
}
864
865
void ArgsManager::LogArgs() const
866
0
{
867
0
    LOCK(cs_args);
Line
Count
Source
259
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
868
0
    for (const auto& section : m_settings.ro_config) {
869
0
        logArgsPrefix("Config file arg:", section.first, section.second);
870
0
    }
871
0
    for (const auto& setting : m_settings.rw_settings) {
872
0
        LogPrintf("Setting file arg: %s = %s\n", setting.first, setting.second.write());
Line
Count
Source
361
0
#define LogPrintf(...) LogInfo(__VA_ARGS__)
Line
Count
Source
356
0
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
350
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(std::source_location::current(), category, level, should_ratelimit, __VA_ARGS__)
873
0
    }
874
0
    logArgsPrefix("Command-line arg:", "", m_settings.command_line_options);
875
0
}
876
877
namespace common {
878
#ifdef WIN32
879
WinCmdLineArgs::WinCmdLineArgs()
880
{
881
    wchar_t** wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
882
    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> utf8_cvt;
883
    argv = new char*[argc];
884
    args.resize(argc);
885
    for (int i = 0; i < argc; i++) {
886
        args[i] = utf8_cvt.to_bytes(wargv[i]);
887
        argv[i] = &*args[i].begin();
888
    }
889
    LocalFree(wargv);
890
}
891
892
WinCmdLineArgs::~WinCmdLineArgs()
893
{
894
    delete[] argv;
895
}
896
897
std::pair<int, char**> WinCmdLineArgs::get()
898
{
899
    return std::make_pair(argc, argv);
900
}
901
#endif
902
} // namespace common