Skip to content

Commit a657669

Browse files
Debugging tip: reverse debugging with rr
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
1 parent af74960 commit a657669

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

kb/development/debugging_tips.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Tips for debugging Mbed TLS
2+
3+
This is a collection of tips for debugging TF-PSA-Crypto or Mbed TLS.
4+
It may also be useful for debugging applications using these projects, but that is not this document's main purpose.
5+
6+
This document assumes some familiarity with the project, e.g. that you already know how to build and test it.
7+
8+
This document is written primarily with Linux in mind. Similar platforms such as macOS will require few adaptations. Windows (except WSL) is out of scope.
9+
10+
## Reverse debugging
11+
12+
### What is reverse debugging?
13+
14+
Also known as back-in-time debugging or time travel debugging.
15+
16+
Reverse debugging allows you to go backward in time when stepping through a program. For example, a reverse single step after returning from a function goes back to the function's `return` statement.
17+
18+
### Tools for reverse debugging
19+
20+
* Gdb supports reverse debugging, but not out of the box, it requires some complex setup.
21+
* LLDB does not support reverse debugging as of 2025.
22+
* Visual Studio (under Windows) supports reverse debugging since 2017.
23+
24+
Reverse debugging works by taking snapshots of a program and recording its inputs and outputs. It may or may not work when the program interacts with its environment in complex ways, since the environment does not roll back when the program does.
25+
26+
### Replay debuggers
27+
28+
A replay debugger records one execution of the program. It then replays this same execution, simulating all inputs and outputs.
29+
30+
#### Replay debugging on Linux with rr
31+
32+
Install the Mozilla Record and Replay framework (rr) from https://rr-project.org/ or e.g. `apt install rr`.​
33+
34+
If needed, give yourself debugging permission:
35+
36+
```
37+
# The Ubuntu default is 4 which is too paranoid.
38+
sudo sysctl kernel.perf_event_paranoid=1.​
39+
# Make this persistent across reboots.
40+
echo 'kernel.perf_event_paranoid = 1' >>/etc/sysctl.d/zz-local.conf​
41+
```
42+
43+
To debug a program​, build it with debugging symbols as usual (`-O0 –g3` or `–Og -g3`).​ Then run it once to save a full trace of the execution:
44+
45+
```
46+
rr record tests/test_suite_ssl
47+
```
48+
49+
Then `rr replay` gives you a gdb interface where reverse execution actually works.​ You can use [`reverse-xxx` commands​](https://sourceware.org/gdb/current/onlinedocs/gdb.html/Reverse-Execution.html) such as:
50+
51+
* `rs` (`reverse-step`) steps into functions​.
52+
* `rn` (`reverse-next`) steps over function calls​.
53+
* `reverse-finish` goes back to where the current function was called​.
54+
* `set exec-direction reverse` changes `step`, `next`, etc. to go backwards. Switch this off with `set exec-direction forward`.
55+
56+
If you use a frontend, configure it to run `rr replay` instead of `gdb myprogram`.​ If the frontend uses gdb's machine interface, use `rr replay -i=mi …` instead of `gdb -i=mi …`.
57+
58+
#### Replay debugging on macOS with warpspeed
59+
60+
Try [warpspeed](https://github.com/kallsyms/warpspeed).

0 commit comments

Comments
 (0)