Skip to content

build: Fix source paths for debugging in CMake - #34081

Closed
arejula27 wants to merge 1 commit into
bitcoin:masterfrom
arejula27:debug-path-cmake-adjust
Closed

arejula27 wants to merge 1 commit into
bitcoin:masterfrom
arejula27:debug-path-cmake-adjust

Conversation

@arejula27

@arejula27 arejula27 commented Dec 16, 2025 •

Copy link
Copy Markdown
Contributor

With the build workflow change from Autotools to CMake, the debug information became incorrect due to outdated file path remapping (#31204). This issue comes from a workaround that was needed in the previous build system (#21885 problem, which was addressed in #20353 and later again on #22409. ).

This PR attempts to resolve the problem by correcting the file path remapping for the new out-of-source build.

I have tested the fix both in the terminal using GDB and in VS Code using LLDB

After my changes, I tested by running gdb:

cmake -DCMAKE_BUILD_TYPE=Debug -B build 
cmake --build build -j "$(($(nproc)/2+1))"

gdb  build/bin/bitcoind
break init.cpp:1848
run  -regtest 
      Thread 1 "bitcoind" hit Breakpoint 1, AppInitMain (node=..., tip_info=0x0) at ./init.cpp:1848
       1848        if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {

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

{
"version": "0.2.0",
"configurations": [
	{
	"type": "lldb",
	"request": "launch",
	"name": "Debug",
	"program": "${workspaceFolder}/build/bin/bitcoind",
	"args": [
		"-regtest"
	],
	"cwd": "${workspaceFolder}",
	}
]
}

Alternatives

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

@DrahtBot

DrahtBot commented Dec 16, 2025 •

Copy link
Copy Markdown
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.

Type Reviewers
Concept ACK pinheadmz, ismaelsadeeq

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #34116 (cmake: Fix debugging info by hebasto)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@maflcko

maflcko commented Dec 16, 2025

Copy link
Copy Markdown
Member

See #31204 (comment)

@arejula27

arejula27 commented Dec 16, 2025 •

Copy link
Copy Markdown
Contributor Author

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)

@maflcko

maflcko commented Dec 16, 2025

Copy link
Copy Markdown
Member

How is your patch different from just removing the prefix-map option?

@arejula27

arejula27 commented Dec 16, 2025 •

Copy link
Copy Markdown
Contributor Author

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

@maflcko

maflcko commented Dec 16, 2025

Copy link
Copy Markdown
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)

@arejula27

arejula27 commented Dec 16, 2025 •

Copy link
Copy Markdown
Contributor Author

Just tested what you mean, removed:

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

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 (#30799.) I will try to look into it #31957 (comment)

@maflcko

maflcko commented Dec 16, 2025

Copy link
Copy Markdown
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.

@arejula27

arejula27 commented Dec 17, 2025 •

Copy link
Copy Markdown
Contributor Author

I have analysed how the different debug information is stored in the master branch, using my changes and removing the remaps (your proposal ). The observable difference is in how DWARF debug information records source file paths.

Here are the three scenarios I tested with objdump to extract the info:

objdump -g ./build/bin/bitcoind | grep -A5 -B5 "src/" 

First case (master branch – remap relative path for old project layout)

<3674904>   DW_AT_name        : (indirect line string, offset: 0x5f8a): ./wallet/interfaces.cpp
<3674908>   DW_AT_comp_dir    : (indirect line string, offset: 0x5f4f): /home/arejula27/workspaces/bitcoin/build-master/src/wallet
<367490c>   DW_AT_ranges      : 0xedf1f
  • DW_AT_comp_dir points to a build directory (build-master/src/wallet)
  • This indicates an out-of-tree build (build and src folders are not the same)
  • The debug info records paths relative to the build tree

Second case (with my changes, remap relative path correctly)

<3674904>   DW_AT_name        : (indirect line string, offset: 0x5f70): ./wallet/interfaces.cpp
<3674908>   DW_AT_comp_dir    : (indirect line string, offset: 0x5f42): /home/arejula27/workspaces/bitcoin/src/wallet
  • DW_AT_comp_dir points to the source directory (src/wallet)
  • This indicates an in-tree-like behaviour for paths (build and src folders are the same)
  • The debug info records paths relative to the source tree

Third case (removing the flags entirely, use absolute path)

<18>   DW_AT_name        : (indirect string, offset: 0x32): /home/arejula27/workspaces/bitcoin/src/bitcoind.cpp
<1c>   DW_AT_comp_dir    : (indirect string, offset: 0): /home/arejula27/workspaces/bitcoin/build-clean/src
  • DW_AT_name contains an absolute path to the source file (src/bitcoind.cpp)
  • DW_AT_comp_dir points to a build directory (build-clean/src)
  • This indicates an out-of-tree build
  • The debug info records the full source path directly, not relative to the build or source tree

Conclusion

So replying to:

What would be the observable difference? It just seems odd to have code that doesn't achieve anything.

The difference is in storing absolute paths or relative ones. The current branch is just wrong, it points incorrectly. The decision is whether you prefer a relative or an absolute path. I read on some old related issues/pr that relative paths are better for tools like ccache, but I am not an expert on that.

@maflcko

maflcko commented Dec 19, 2025

Copy link
Copy Markdown
Member

Second case (with my changes, remap relative path correctly)

It is interesting that this is using relative paths. I had the impression that "-fdebug-prefix-map=${PROJECT_BINARY_DIR}/src=${PROJECT_SOURCE_DIR}/src" has absolute paths on both sides of the mapping.

The difference is in storing absolute paths or relative ones. The current branch is just wrong, it points incorrectly. The decision is whether you prefer a relative or an absolute path. I read on some old related issues/pr that relative paths are better for tools like ccache, but I am not an expert on that.

I think the ccache behavior is currently broken anyway (#30861 was closed), so there is little that can be broken by removing the code.

Of course, everything will still need to be tested to confirm the fixes and no new breakages.

You'll have to include in the pull request descriptions, which bugs are fixed. Also, mentioning autogen and configure doesn't really help, when the project here is using cmake.

@pinheadmz

Copy link
Copy Markdown
Member

concept ACK, this works for source code in src/ when debugging bitcoind but more changes are needed, for example, to debug test_bitcoin.

This is a much better fix than just updating docs like #32205

Example that works: bitcoind, set breakpoint at BindListenPort

master:

Process 92900 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x00000001002c9720 bitcoind`CConnman::BindListenPort(this=0x0000000121009a00, addrBind=0x000000016fdfa980, strError=0x000000016fdfa950, permissions=None) at net.cpp:3139:9
Target 0: (bitcoind) stopped.

PR:

Process 97697 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x00000001002c56f0 bitcoind`CConnman::BindListenPort(this=0x000000012b80e000, addrBind=0x000000016fdfab60, strError=0x000000016fdfab30, permissions=None) at net.cpp:3139:9
   3136 
   3137 bool CConnman::BindListenPort(const CService& addrBind, bilingual_str& strError, NetPermissionFlags permissions)
   3138 {
-> 3139     int nOne = 1;
   3140 
   3141     // Create socket for listening for incoming connections
   3142     struct sockaddr_storage sockaddr;
Target 0: (bitcoind) stopped.

Example that doesn't work: test_bitcoin, set breakpoint at GetCheckRatio() in addrman_tests

both master and PR:

Process 97809 stopped
* thread #1, name = 'b-test', queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x0000000100169820 test_bitcoin`GetCheckRatio(node_ctx=0x000000016fdfa9b8) at addrman_tests.cpp:32:32
Target 0: (test_bitcoin) stopped.

@arejula27

Copy link
Copy Markdown
Contributor Author

re: @maflcko

You'll have to include in the pull request descriptions, which bugs are fixed. Also, mentioning autogen and configure doesn't really help, when the project here is using cmake.

Thank you for your feedback. This is my first PR, so any advice is welcome. I’ve edited the description to better summarize the change and to point directly to the specific bug being addressed.

@arejula27

Copy link
Copy Markdown
Contributor Author

re: @pinheadmz

this works [...] when debugging bitcoind but more changes are needed, for example, to debug test_bitcoin

Good point, I’ll try to address this as well. Is there any other binary you’d like me to verify? I don’t think the functional tests are related to this PR, but I might be others I am missing.

@ismaelsadeeq

Copy link
Copy Markdown
Member

Concept ACK

This works for me out of the box

Process 98441 stopped
* thread #36, name = 'b-msghand', stop reason = breakpoint 1.1
    frame #0: 0x00000001002d8a3c bitcoind`(anonymous namespace)::PeerManagerImpl::ProcessMessage(this=0x00000007e703d400, pfrom=0x0000000103b6b430, msg_type="version", vRecv=0x000000017111eb30, time_received=1766574637095060 µs, interruptMsgProc=0x00000007e703d240) at net_processing.cpp:3431:5
   3428	                                     const std::chrono::microseconds time_received,
   3429	                                     const std::atomic<bool>& interruptMsgProc)
   3430	{
-> 3431	    AssertLockHeld(g_msgproc_mutex);
   3432	
   3433	    LogDebug(BCLog::NET, "received: %s (%u bytes) peer=%d\n", SanitizeString(msg_type), vRecv.size(), pfrom.GetId());
   3434	
Target 0: (bitcoind) stopped.
(lldb) 

I also saw the same behaviour as @pinheadmz for test_bitcoin

You can see the list of binaries using

 ls build/bin
bitcoin*        bitcoin-cli*    bitcoin-node*   bitcoin-tx*     bitcoin-util*   bitcoin-wallet* bitcoind*       test_bitcoin*

@arejula27

Copy link
Copy Markdown
Contributor Author

This PR conflicts with #34116, which solves the same issue. After reviewing it i believe #34116 is a better approach. I would keep this one open to receive other opinions, but this PR should be closed if #34116 is preferred by the community

@fanquake

fanquake commented Jan 2, 2026

Copy link
Copy Markdown
Member

After reviewing it i believe #34116 is a better approach.

Thanks, we'll close this in favour of #34116 for now.

@fanquake fanquake closed this Jan 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants