LCOV - code coverage report
Current view: top level - src - bitcoin.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 0.0 % 101 0
Test Date: 2025-10-04 05:03:45 Functions: 0.0 % 5 0
Branches: 0.0 % 294 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2025 The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <bitcoin-build-config.h> // IWYU pragma: keep
       6                 :             : 
       7                 :             : #include <clientversion.h>
       8                 :             : #include <common/args.h>
       9                 :             : #include <util/fs.h>
      10                 :             : #include <util/exec.h>
      11                 :             : #include <util/strencodings.h>
      12                 :             : #include <util/translation.h>
      13                 :             : 
      14                 :             : #include <iostream>
      15                 :             : #include <string>
      16                 :             : #include <tinyformat.h>
      17                 :             : #include <vector>
      18                 :             : 
      19                 :             : const TranslateFn G_TRANSLATION_FUN{nullptr};
      20                 :             : 
      21                 :             : static constexpr auto HELP_USAGE = R"(Usage: %s [OPTIONS] COMMAND...
      22                 :             : 
      23                 :             : Options:
      24                 :             :   -m, --multiprocess     Run multiprocess binaries bitcoin-node, bitcoin-gui.
      25                 :             :   -M, --monolithic       Run monolithic binaries bitcoind, bitcoin-qt. (Default behavior)
      26                 :             :   -v, --version          Show version information
      27                 :             :   -h, --help             Show full help message
      28                 :             : 
      29                 :             : Commands:
      30                 :             :   gui [ARGS]     Start GUI, equivalent to running 'bitcoin-qt [ARGS]' or 'bitcoin-gui [ARGS]'.
      31                 :             :   node [ARGS]    Start node, equivalent to running 'bitcoind [ARGS]' or 'bitcoin-node [ARGS]'.
      32                 :             :   rpc [ARGS]     Call RPC method, equivalent to running 'bitcoin-cli -named [ARGS]'.
      33                 :             :   wallet [ARGS]  Call wallet command, equivalent to running 'bitcoin-wallet [ARGS]'.
      34                 :             :   tx [ARGS]      Manipulate hex-encoded transactions, equivalent to running 'bitcoin-tx [ARGS]'.
      35                 :             :   help           Show full help message.
      36                 :             : )";
      37                 :             : 
      38                 :             : static constexpr auto HELP_FULL = R"(
      39                 :             : Additional less commonly used commands:
      40                 :             :   bench [ARGS]      Run bench command, equivalent to running 'bench_bitcoin [ARGS]'.
      41                 :             :   chainstate [ARGS] Run bitcoin kernel chainstate util, equivalent to running 'bitcoin-chainstate [ARGS]'.
      42                 :             :   test [ARGS]       Run unit tests, equivalent to running 'test_bitcoin [ARGS]'.
      43                 :             :   test-gui [ARGS]   Run GUI unit tests, equivalent to running 'test_bitcoin-qt [ARGS]'.
      44                 :             : )";
      45                 :             : 
      46                 :             : static constexpr auto HELP_SHORT = R"(
      47                 :             : Run '%s help' to see additional commands (e.g. for testing and debugging).
      48                 :             : )";
      49                 :             : 
      50                 :           0 : struct CommandLine {
      51                 :             :     std::optional<bool> use_multiprocess;
      52                 :             :     bool show_version{false};
      53                 :             :     bool show_help{false};
      54                 :             :     std::string_view command;
      55                 :             :     std::vector<const char*> args;
      56                 :             : };
      57                 :             : 
      58                 :             : CommandLine ParseCommandLine(int argc, char* argv[]);
      59                 :             : bool UseMultiprocess(const CommandLine& cmd);
      60                 :             : static void ExecCommand(const std::vector<const char*>& args, std::string_view argv0);
      61                 :             : 
      62                 :           0 : int main(int argc, char* argv[])
      63                 :             : {
      64                 :           0 :     try {
      65         [ #  # ]:           0 :         CommandLine cmd{ParseCommandLine(argc, argv)};
      66         [ #  # ]:           0 :         if (cmd.show_version) {
      67   [ #  #  #  #  :           0 :             tfm::format(std::cout, "%s version %s\n%s", CLIENT_NAME, FormatFullVersion(), FormatParagraph(LicenseInfo()));
             #  #  #  # ]
      68                 :           0 :             return EXIT_SUCCESS;
      69                 :             :         }
      70                 :             : 
      71   [ #  #  #  #  :           0 :         std::string exe_name{fs::PathToString(fs::PathFromString(argv[0]).filename())};
                   #  # ]
      72                 :           0 :         std::vector<const char*> args;
      73   [ #  #  #  # ]:           0 :         if (cmd.show_help || cmd.command.empty()) {
      74         [ #  # ]:           0 :             tfm::format(std::cout, HELP_USAGE, exe_name);
      75         [ #  # ]:           0 :             if (cmd.show_help) {
      76         [ #  # ]:           0 :                 tfm::format(std::cout, HELP_FULL);
      77                 :             :                 return EXIT_SUCCESS;
      78                 :             :             } else {
      79         [ #  # ]:           0 :                 tfm::format(std::cout, HELP_SHORT, exe_name);
      80                 :             :                 return EXIT_FAILURE;
      81                 :             :             }
      82         [ #  # ]:           0 :         } else if (cmd.command == "gui") {
      83   [ #  #  #  #  :           0 :             args.emplace_back(UseMultiprocess(cmd) ? "bitcoin-gui" : "bitcoin-qt");
                   #  # ]
      84         [ #  # ]:           0 :         } else if (cmd.command == "node") {
      85   [ #  #  #  #  :           0 :             args.emplace_back(UseMultiprocess(cmd) ? "bitcoin-node" : "bitcoind");
                   #  # ]
      86         [ #  # ]:           0 :         } else if (cmd.command == "rpc") {
      87         [ #  # ]:           0 :             args.emplace_back("bitcoin-cli");
      88                 :             :             // Since "bitcoin rpc" is a new interface that doesn't need to be
      89                 :             :             // backward compatible, enable -named by default so it is convenient
      90                 :             :             // for callers to use a mix of named and unnamed parameters. Callers
      91                 :             :             // can override this by specifying -nonamed, but should not need to
      92                 :             :             // unless they are passing string values containing '=' characters
      93                 :             :             // as unnamed parameters.
      94         [ #  # ]:           0 :             args.emplace_back("-named");
      95         [ #  # ]:           0 :         } else if (cmd.command == "wallet") {
      96         [ #  # ]:           0 :             args.emplace_back("bitcoin-wallet");
      97         [ #  # ]:           0 :         } else if (cmd.command == "tx") {
      98         [ #  # ]:           0 :             args.emplace_back("bitcoin-tx");
      99         [ #  # ]:           0 :         } else if (cmd.command == "bench") {
     100         [ #  # ]:           0 :             args.emplace_back("bench_bitcoin");
     101         [ #  # ]:           0 :         } else if (cmd.command == "chainstate") {
     102         [ #  # ]:           0 :             args.emplace_back("bitcoin-chainstate");
     103         [ #  # ]:           0 :         } else if (cmd.command == "test") {
     104         [ #  # ]:           0 :             args.emplace_back("test_bitcoin");
     105         [ #  # ]:           0 :         } else if (cmd.command == "test-gui") {
     106         [ #  # ]:           0 :             args.emplace_back("test_bitcoin-qt");
     107         [ #  # ]:           0 :         } else if (cmd.command == "util") {
     108         [ #  # ]:           0 :             args.emplace_back("bitcoin-util");
     109                 :             :         } else {
     110   [ #  #  #  # ]:           0 :             throw std::runtime_error(strprintf("Unrecognized command: '%s'", cmd.command));
     111                 :             :         }
     112         [ #  # ]:           0 :         if (!args.empty()) {
     113         [ #  # ]:           0 :             args.insert(args.end(), cmd.args.begin(), cmd.args.end());
     114         [ #  # ]:           0 :             ExecCommand(args, argv[0]);
     115                 :             :         }
     116         [ #  # ]:           0 :     } catch (const std::exception& e) {
     117         [ -  - ]:           0 :         tfm::format(std::cerr, "Error: %s\nTry '%s --help' for more information.\n", e.what(), argv[0]);
     118                 :           0 :         return EXIT_FAILURE;
     119                 :           0 :     }
     120                 :           0 :     return EXIT_SUCCESS;
     121                 :             : }
     122                 :             : 
     123                 :           0 : CommandLine ParseCommandLine(int argc, char* argv[])
     124                 :             : {
     125                 :           0 :     CommandLine cmd;
     126         [ #  # ]:           0 :     cmd.args.reserve(argc);
     127         [ #  # ]:           0 :     for (int i = 1; i < argc; ++i) {
     128         [ #  # ]:           0 :         std::string_view arg = argv[i];
     129         [ #  # ]:           0 :         if (!cmd.command.empty()) {
     130         [ #  # ]:           0 :             cmd.args.emplace_back(argv[i]);
     131   [ #  #  #  # ]:           0 :         } else if (arg == "-m" || arg == "--multiprocess") {
     132                 :           0 :             cmd.use_multiprocess = true;
     133   [ #  #  #  # ]:           0 :         } else if (arg == "-M" || arg == "--monolithic") {
     134                 :           0 :             cmd.use_multiprocess = false;
     135   [ #  #  #  # ]:           0 :         } else if (arg == "-v" || arg == "--version") {
     136                 :           0 :             cmd.show_version = true;
     137   [ #  #  #  #  :           0 :         } else if (arg == "-h" || arg == "--help" || arg == "help") {
                   #  # ]
     138                 :           0 :             cmd.show_help = true;
     139         [ #  # ]:           0 :         } else if (arg.starts_with("-")) {
     140   [ #  #  #  # ]:           0 :             throw std::runtime_error(strprintf("Unknown option: %s", arg));
     141         [ #  # ]:           0 :         } else if (!arg.empty()) {
     142                 :           0 :             cmd.command = arg;
     143                 :             :         }
     144                 :             :     }
     145                 :           0 :     return cmd;
     146                 :           0 : }
     147                 :             : 
     148                 :           0 : bool UseMultiprocess(const CommandLine& cmd)
     149                 :             : {
     150                 :             :     // If -m or -M options were explicitly specified, there is no need to
     151                 :             :     // further parse arguments to determine which to use.
     152         [ #  # ]:           0 :     if (cmd.use_multiprocess) return *cmd.use_multiprocess;
     153                 :             : 
     154                 :           0 :     ArgsManager args;
     155         [ #  # ]:           0 :     args.SetDefaultFlags(ArgsManager::ALLOW_ANY);
     156         [ #  # ]:           0 :     std::string error_message;
     157         [ #  # ]:           0 :     auto argv{cmd.args};
     158         [ #  # ]:           0 :     argv.insert(argv.begin(), nullptr);
     159   [ #  #  #  #  :           0 :     if (!args.ParseParameters(argv.size(), argv.data(), error_message)) {
                   #  # ]
     160         [ #  # ]:           0 :         tfm::format(std::cerr, "Warning: failed to parse subcommand command line options: %s\n", error_message);
     161                 :             :     }
     162   [ #  #  #  # ]:           0 :     if (!args.ReadConfigFiles(error_message, true)) {
     163         [ #  # ]:           0 :         tfm::format(std::cerr, "Warning: failed to parse subcommand config: %s\n", error_message);
     164                 :             :     }
     165   [ #  #  #  # ]:           0 :     args.SelectConfigNetwork(args.GetChainTypeString());
     166                 :             : 
     167                 :             :     // If any -ipc* options are set these need to be processed by a
     168                 :             :     // multiprocess-capable binary.
     169   [ #  #  #  #  :           0 :     return args.IsArgSet("-ipcbind") || args.IsArgSet("-ipcconnect") || args.IsArgSet("-ipcfd");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     170                 :           0 : }
     171                 :             : 
     172                 :             : //! Execute the specified bitcoind, bitcoin-qt or other command line in `args`
     173                 :             : //! using src, bin and libexec directory paths relative to this executable, where
     174                 :             : //! the path to this executable is specified in `wrapper_argv0`.
     175                 :             : //!
     176                 :             : //! @param args Command line arguments to execute, where first argument should
     177                 :             : //!             be a relative path to a bitcoind, bitcoin-qt or other executable
     178                 :             : //!             that will be located on the PATH or relative to wrapper_argv0.
     179                 :             : //!
     180                 :             : //! @param wrapper_argv0 String containing first command line argument passed to
     181                 :             : //!                      main() to run the current executable. This is used to
     182                 :             : //!                      help determine the path to the current executable and
     183                 :             : //!                      how to look for new executables.
     184                 :             : //
     185                 :             : //! @note This function doesn't currently print anything but can be debugged
     186                 :             : //! from the command line using strace or dtrace like:
     187                 :             : //!
     188                 :             : //!     strace -e trace=execve -s 10000 build/bin/bitcoin ...
     189                 :             : //!     dtrace -n 'proc:::exec-success  /pid == $target/ { trace(curpsinfo->pr_psargs); }' -c ...
     190                 :           0 : static void ExecCommand(const std::vector<const char*>& args, std::string_view wrapper_argv0)
     191                 :             : {
     192                 :             :     // Construct argument string for execvp
     193                 :           0 :     std::vector<const char*> exec_args{args};
     194         [ #  # ]:           0 :     exec_args.emplace_back(nullptr);
     195                 :             : 
     196                 :             :     // Try to call ExecVp with given exe path.
     197                 :           0 :     auto try_exec = [&](fs::path exe_path, bool allow_notfound = true) {
     198         [ #  # ]:           0 :         std::string exe_path_str{fs::PathToString(exe_path)};
     199         [ #  # ]:           0 :         exec_args[0] = exe_path_str.c_str();
     200   [ #  #  #  # ]:           0 :         if (util::ExecVp(exec_args[0], (char*const*)exec_args.data()) == -1) {
     201   [ #  #  #  # ]:           0 :             if (allow_notfound && errno == ENOENT) return false;
     202   [ #  #  #  # ]:           0 :             throw std::system_error(errno, std::system_category(), strprintf("execvp failed to execute '%s'", exec_args[0]));
     203                 :             :         }
     204         [ #  # ]:           0 :         throw std::runtime_error("execvp returned unexpectedly");
     205                 :           0 :     };
     206                 :             : 
     207                 :             :     // Get the wrapper executable path.
     208         [ #  # ]:           0 :     const fs::path wrapper_path{util::GetExePath(wrapper_argv0)};
     209                 :             : 
     210                 :             :     // Try to resolve any symlinks and figure out the directory containing the wrapper executable.
     211         [ #  # ]:           0 :     std::error_code ec;
     212         [ #  # ]:           0 :     fs::path wrapper_dir{fs::weakly_canonical(wrapper_path, ec)};
     213   [ #  #  #  # ]:           0 :     if (wrapper_dir.empty()) wrapper_dir = wrapper_path; // Restore previous path if weakly_canonical failed.
     214         [ #  # ]:           0 :     wrapper_dir = wrapper_dir.parent_path();
     215                 :             : 
     216                 :             :     // Get path of the executable to be invoked.
     217   [ #  #  #  # ]:           0 :     const fs::path arg0{fs::PathFromString(args[0])};
     218                 :             : 
     219                 :             :     // Decide whether to fall back to the operating system to search for the
     220                 :             :     // specified executable. Avoid doing this if it looks like the wrapper
     221                 :             :     // executable was invoked by path, rather than by search, to avoid
     222                 :             :     // unintentionally launching system executables in a local build.
     223                 :             :     // (https://github.com/bitcoin/bitcoin/pull/31375#discussion_r1861814807)
     224   [ #  #  #  # ]:           0 :     const bool fallback_os_search{!fs::PathFromString(std::string{wrapper_argv0}).has_parent_path()};
     225                 :             : 
     226                 :             :     // If wrapper is installed in a bin/ directory, look for target executable
     227                 :             :     // in libexec/
     228   [ #  #  #  #  :           0 :     (wrapper_dir.filename() == "bin" && try_exec(fs::path{wrapper_dir.parent_path()} / "libexec" / arg0.filename())) ||
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     229                 :             : #ifdef WIN32
     230                 :             :     // Otherwise check the "daemon" subdirectory in a windows install.
     231                 :             :     (!wrapper_dir.empty() && try_exec(wrapper_dir / "daemon" / arg0.filename())) ||
     232                 :             : #endif
     233                 :             :     // Otherwise look for target executable next to current wrapper
     234   [ #  #  #  #  :           0 :     (!wrapper_dir.empty() && try_exec(wrapper_dir / arg0.filename(), fallback_os_search)) ||
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     235                 :             :     // Otherwise just look on the system path.
     236   [ #  #  #  # ]:           0 :     (fallback_os_search && try_exec(arg0.filename(), false));
     237                 :           0 : }
        

Generated by: LCOV version 2.0-1