build: Fix source paths for debugging in CMake #34081

pull arejula27 wants to merge 1 commits into bitcoin:master from arejula27:debug-path-cmake-adjust changing 1 files +10 −0
  1. arejula27 commented at 6:20 pm on December 16, 2025: none

    I was following the debugging guide written by fjahr when I realised a small bug. It was expected to create breakpoints from the root of the project; however, the following commands do not work properly, as the file is not found:

    0cmake -DCMAKE_BUILD_TYPE=Debug -B build 
    1cmake --build build -j "$(($(nproc)/2+1))"
    2gdb  build/bin/bitcoind
    3break init.cpp:1848
    4run  -regtest 
    5Thread 1 "bitcoind" hit Breakpoint 1, AppInitMain (node=..., tip_info=0x0) at ./init.cpp:1848
    6warning: 1848   ./init.cpp: No such file or directory
    

    For example, the gdb command layout src does not show the code. Additionally, other debuggers, such as LLDB, crash if you set breakpoints in the editor and then launch the debugger.

    As the documentation points out, the code should be available to run from the root of the project and not from the src/ folder, as the documentation says:

    This might break source file detection in case you move binaries after compilation, debug from the directory other than the project root or use an IDE that only supports absolute paths for debugging (e.g. it won’t stop at breakpoints).

    To find the cause of the problem, I explored previous similar issues. A user reported on issue #21885 a similar problem, which was addressed in #20353 and later again on #22409.

    The solution proposed on these PRs was remapping the path of the debug files from src/ to . . However, this change was made when the project had another structure, and the compilation happened on the source folder, after fixing the gdb debugger could be initiated with:

    0./autogen.sh 
    1./configure CXXFLAGS="-O0 -g" CFLAGS="-O0 -g" 
    2make   
    3gdb  src/bitcoind 
    4b init.cpp:1193
    5run  -regtest
    6Thread 1 "bitcoind" hit Breakpoint 1, AppInitMain (node=..., tip_info=0x0) at init.cpp:1195
    71195        for (const auto& client : node.chain_clients) {
    

    After the project restructuring, the same remap has been applied in the CMake pipeline; however, the paths are different, now the source and the build folders must be different. To solve this temporarily, I ran:

    0set substitute-path /home/arejula27/workspaces/bitcoin/build/src /home/arejula27/workspaces/bitcoin/src
    

    However, this pull request addresses the problem and tries to fix it, updating the remaps to the new folder layout directly on CMake.

    After my changes, I tested by running gdb:

    0cmake -DCMAKE_BUILD_TYPE=Debug -B build 
    1cmake --build build -j "$(($(nproc)/2+1))"
    2
    3gdb  build/bin/bitcoind
    4break init.cpp:1848
    5run  -regtest 
    6      Thread 1 "bitcoind" hit Breakpoint 1, AppInitMain (node=..., tip_info=0x0) at ./init.cpp:1848
    7       1848        if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
    

    And also with lldb and vscode using the following .vscode/launch.json:

     0{
     1"version": "0.2.0",
     2"configurations": [
     3	{
     4	"type": "lldb",
     5	"request": "launch",
     6	"name": "Debug",
     7	"program": "${workspaceFolder}/build/bin/bitcoind",
     8	"args": [
     9		"-regtest"
    10	],
    11	"cwd": "${workspaceFolder}",
    12	}
    13]
    14}
    

    Alternatives

    If this PR is not considered, I have two alternative proposals:

    • Update the doc to remove the text indicating the user must debug from the root of the project
    • PR #https://github.com/bitcoin/bitcoin/pull/32205 Update the doc indicating they must use set substitute-path path/to/build/src /path/to/project/root/src
  2. build: Fix source paths for debugging in CMake e3195783ed
  3. DrahtBot added the label Build system on Dec 16, 2025
  4. DrahtBot commented at 6:20 pm on December 16, 2025: contributor

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/34081.

    Reviews

    See the guideline for information on the review process. A summary of reviews will appear here.

  5. maflcko commented at 7:41 pm on December 16, 2025: member
  6. arejula27 commented at 7:52 pm on December 16, 2025: none

    re: @maflcko

    See #31204 (comment)

    I didn’t see this, thank you. The issue points exactly to the problem I’m trying to fix. I see that the proposed solution in #32205 addresses the problem by updating the documentation. While that might work, it doesn’t actually fix the underlying issue and requires updating the paths for each build (some users may run cmake -B debug-build1 and cmake -B debug-build2 . My proposal avoids additional user configuration, and it is more dynamic as it uses variables within CMake (PROJECT_BINARY_DIR)

  7. maflcko commented at 7:56 pm on December 16, 2025: member
    How is your patch different from just removing the prefix-map option?
  8. arejula27 commented at 8:01 pm on December 16, 2025: none

    As I understand the problem, the solution is not removing the prefix-map option but changing where it points. This can be done either in .lldbinit, .gdbinit, or in CMake. My proposal simply removes friction for the developer, as it will not require them to put a path in their configuration.

    i propose modifying the cmake: "-fdebug-prefix-map=${PROJECT_BINARY_DIR}/src=${PROJECT_SOURCE_DIR}/src"

    The #32205 proposes suggesting users to modify the debugger config: settings set target.source-map /path/to/bitcoin/build/src /path/to/bitcoin/src

    It will have the same outcome, but mine will be automatically done. Also it is more interoperable as I mentioned i it worked with gdb and lldb, while in the other case I would require to configure both debuggers

  9. maflcko commented at 8:11 pm on December 16, 2025: member

    As I understand the problem, the solution is not removing the prefix-map option but changing where it points.

    Yes, but I am asking why? What would be the observable difference? It just seems odd to have code that doesn’t achieve anything.

    At least last time I checked, the two seemed to achieve the same: #31957 (comment)

  10. arejula27 commented at 8:24 pm on December 16, 2025: none

    Just tested what you mean, removed:

    0-try_append_cxx_flags("-fdebug-prefix-map=A=B" TARGET core_interface SKIP_LINK
    1-  IF_CHECK_PASSED "-fdebug-prefix-map=${PROJECT_SOURCE_DIR}/src=."
    2-)
    

    It had the same effect, yes, make more sense removing code that makes an incorrect behaviour than adding more above it. I will try to research if it has any advantages, but in other cas,e the correct approach should be to remove the incorrect mapping

    Edit: @hebasto pointed out that removing it might resurface an issue (https://github.com/bitcoin/bitcoin/issues/30799.) I will try to look into it #31957 (comment)

  11. maflcko commented at 8:42 pm on December 16, 2025: member

    Edit: @hebasto pointed out that removing it might resurface an issue (#30799.) I will try to look into it #31957 (comment)

    This may be a macro, and not the debug info, so may be fine, but I haven’t checked this.


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2025-12-17 03:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me