Skip to content

Commit 883f042

Browse files
committed
Add instrutions how to combine valgrind with gdb debugging
Signed-off-by: DL6ER <[email protected]>
1 parent 8e155e3 commit 883f042

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

docs/ftldns/valgrind.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ We suggest the following one-liner to run `pihole-FTL` in `memcheck`:
4545

4646
```
4747
sudo service pihole-FTL stop && sudo setcap -r /usr/bin/pihole-FTL
48-
sudo valgrind --trace-children=yes --leak-check=full --track-origins=yes --log-file=valgrind.log -s /usr/bin/pihole-FTL
48+
sudo valgrind --trace-children=yes --leak-check=full --track-origins=yes --vgdb=full --log-file=valgrind.log -s /usr/bin/pihole-FTL
4949
```
5050

5151
If you compile FTL from source, use
5252

5353
```
54-
sudo service pihole-FTL stop && sudo setcap -r /usr/bin/pihole-FTL
55-
./build.sh && sudo valgrind --trace-children=yes --leak-check=full --track-origins=yes --log-file=valgrind.log -s ./pihole-FTL
54+
sudo service pihole-FTL stop
55+
./build.sh && sudo valgrind --trace-children=yes --leak-check=full --track-origins=yes --vgdb=full --log-file=valgrind.log -s ./pihole-FTL
5656
```
5757

5858
The most useful information (about which memory is *possibly* and which is *definitely* lost) is written to `valgrind.log` at the end of the analysis. Terminate FTL by running:
@@ -73,6 +73,29 @@ The used options are:
7373
2. `leak-check=full` - When enabled, search for memory leaks when the client program finishes. Each individual leak will be shown in detail and/or counted as an error.
7474
3. `track-origins=yes` - Memcheck tracks the origin of uninitialised values. By default, it does not, which means that although it can tell you that an uninitialised value is being used in a dangerous way, it cannot tell you where the uninitialised value came from. This often makes it difficult to track down the root problem.
7575
When set to `yes`, Memcheck keeps track of the origins of all uninitialised values. Then, when an uninitialised value error is reported, Memcheck will try to show the origin of the value.
76+
4. `vgdb=full` - The Valgrind core provides a built-in gdbserver implementation. This is useful when you want to investigate a crash that is not easily reproducible and memory errors are suspected to be the cause. This gdbserver allows the process running on Valgrind's synthetic CPU to be debugged remotely. GDB sends protocol query packets (such as "get register contents") to the Valgrind embedded gdbserver. The gdbserver executes the queries (for example, it will get the register values of the synthetic CPU) and gives the results back to GDB.
77+
78+
### Combining `valgrind` with `gdb`
79+
80+
You can also combine `valgrind` with `gdb` to get both the memory error detection and the ability to step through the code after a crash (or other unexpected behaviour).
81+
82+
1. Prepare `gdb` as described in [the `gdb` guide](debugging.md).
83+
2. Start `pihole-FTL` in `valgrind` as described above. The `--vgdb=full` option tells `valgrind` to start a GDB server.
84+
3. Once FTL has started, you can attach `gdb` to the running process using
85+
86+
``` bash
87+
sudo gdb /usr/bin/pihole-FTL
88+
```
89+
90+
and then at the `(gdb)` prompt,
91+
92+
``` plain
93+
target remote | vgdb
94+
```
95+
96+
to connect `gdb` to the "remote" `valgrind` process
97+
4. Don't forget to enter `continue` to continue the execution of `pihole-FTL` in `valgrind`
98+
5. At the time of a crash, you can step through the code, and investigate the state of the program as usual with `gdb`. Note that variables and functions may have different names than in the source code, as `valgrind` modifies the program with additional instrumentation code.
7699
77100
### False-positive memory issues
78101

0 commit comments

Comments
 (0)