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