forked from hyperlight-dev/hyperlight
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Hyperlight KVM guest debugging using gdb (hyperlight-…
…dev#111) * add gdb cargo feature Signed-off-by: Doru Blânzeanu <dblnz@pm.me> * disable timeouts for messaging between host and hypervisor handler when gdb feature is on Signed-off-by: Doru Blânzeanu <dblnz@pm.me> * add guest debug port to sandbox configuration Signed-off-by: Doru Blânzeanu <dblnz@pm.me> * add gdb debug thread creation method Signed-off-by: Doru Blânzeanu <dblnz@pm.me> * add debug communication channel type - this type will be used by the gdb and the hypervisor handler to send requests and receive responses Signed-off-by: Doru Blânzeanu <dblnz@pm.me> * add hyperlight sandbox target to handle gdb commands support - the target implements the traits to provide callbacks for the gdb commands Signed-off-by: Doru Blânzeanu <dblnz@pm.me> * handle debug vcpu exit on kvm side - adds a specific handler for the vcpu exit debug that waits for debug messages and processes them Signed-off-by: Doru Blânzeanu <dblnz@pm.me> * add kvm debug configuration that handles vCPU interaction Signed-off-by: Doru Blânzeanu <dblnz@pm.me> * add read/write registers support Signed-off-by: Doru Blânzeanu <dblnz@pm.me> * hw breakpoint and resume support added Signed-off-by: Doru Blânzeanu <dblnz@pm.me> * add stepping support - also adds handling of gdb client disconnect by resuming execution Signed-off-by: Doru Blânzeanu <dblnz@pm.me> * add a way to read/write host shared mem when debugging Signed-off-by: Doru Blânzeanu <dblnz@pm.me> * add read/write address and text section offset support Signed-off-by: Doru Blânzeanu <dblnz@pm.me> * add sw breakpoint support Signed-off-by: Doru Blânzeanu <dblnz@pm.me> * add ci checks for the gdb feature Signed-off-by: Doru Blânzeanu <dblnz@pm.me> * add documentation about the gdb debugging feature Signed-off-by: Doru Blânzeanu <dblnz@pm.me> * add a way to report a non-fatal error to gdb - sometimes gdb tries to read from an address where the vcpu translate method fails, so we need to handle that in such a way that the hypervisor handler doesn't crash Signed-off-by: Doru Blânzeanu <dblnz@pm.me> * add CI gdb test Signed-off-by: Doru Blânzeanu <dblnz@pm.me> * add support for interrupts Signed-off-by: Doru Blânzeanu <dblnz@pm.me> * move gdb thread creation in hypervisor handler - this helps with separation and testing Signed-off-by: Doru Blânzeanu <dblnz@pm.me> * improve error handling and avoid panicking - replace `expect` statements with logic to propagate error up the call chain - improve log messages and comments Signed-off-by: Doru Blânzeanu <dblnz@pm.me> --------- Signed-off-by: Doru Blânzeanu <dblnz@pm.me>
- Loading branch information
Showing
25 changed files
with
2,208 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
# How to debug a Hyperlight **KVM** guest using gdb | ||
|
||
Hyperlight supports gdb debugging of a **KVM** guest running inside a Hyperlight sandbox. | ||
When Hyperlight is compiled with the `gdb` feature enabled, a Hyperlight KVM sandbox can be configured | ||
to start listening for a gdb connection. | ||
|
||
## Supported features | ||
|
||
The Hyperlight `gdb` feature enables **KVM** guest debugging: | ||
- an entry point breakpoint is automatically set for the guest to stop | ||
- add and remove HW breakpoints (maximum 4 set breakpoints at a time) | ||
- add and remove SW breakpoints | ||
- read and write registers | ||
- read and write addresses | ||
- step/continue | ||
- get code offset from target | ||
|
||
## Expected behavior | ||
|
||
Below is a list describing some cases of expected behavior from a gdb debug | ||
session of a guest binary running inside a KVM Hyperlight sandbox. | ||
|
||
- when the `gdb` feature is enabled and a SandboxConfiguration is provided a | ||
debug port, the created sandbox will wait for a gdb client to connect on the | ||
configured port | ||
- when the gdb client attaches, the guest vCPU is expected to be stopped at the | ||
entry point | ||
- if a gdb client disconnects unexpectedly, the debug session will be closed and | ||
the guest will continue executing disregarding any prior breakpoints | ||
- if multiple sandbox instances are created, each instance will have its own | ||
gdb thread listening on the configured port | ||
- if two sandbox instances are created with the same debug port, the second | ||
instance logs an error and the gdb thread will not be created, but the sandbox | ||
will continue to run without gdb debugging | ||
|
||
## Example | ||
|
||
### Sandbox configuration | ||
|
||
The `guest-debugging` example in Hyperlight demonstrates how to configure a Hyperlight | ||
sandbox to listen for a gdb client on a specific port. | ||
|
||
### CLI Gdb configuration | ||
|
||
One can use a gdb config file to provide the symbols and desired configuration. | ||
|
||
The below contents of the `.gdbinit` file can be used to provide a basic configuration | ||
to gdb startup. | ||
|
||
```gdb | ||
# Path to symbols | ||
file path/to/symbols.elf | ||
# The port on which Hyperlight listens for a connection | ||
target remote :8080 | ||
set disassembly-flavor intel | ||
set disassemble-next-line on | ||
enable pretty-printer | ||
layout src | ||
``` | ||
One can find more information about the `.gdbinit` file at [gdbinit(5)](https://www.man7.org/linux/man-pages/man5/gdbinit.5.html). | ||
|
||
### End to end example | ||
|
||
Using the example mentioned at [Sandbox configuration](#sandbox-configuration) | ||
one can run the below commands to debug the guest binary: | ||
|
||
```bash | ||
# Terminal 1 | ||
$ cargo run --example guest-debugging --features gdb | ||
``` | ||
|
||
```bash | ||
# Terminal 2 | ||
$ cat .gdbinit | ||
file src/tests/rust_guests/bin/debug/simpleguest | ||
target remote :8080 | ||
set disassembly-flavor intel | ||
set disassemble-next-line on | ||
enable pretty-printer | ||
layout src | ||
|
||
$ gdb | ||
``` | ||
|
||
### Using VSCode to debug a Hyperlight guest | ||
|
||
To replicate the above behavior using VSCode follow the below steps: | ||
- install the `gdb` package on the host machine | ||
- install the `C/C++` extension in VSCode to add debugging capabilities | ||
- create a `.vscode/launch.json` file in the project directory with the below content: | ||
```json | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "GDB", | ||
"type": "cppdbg", | ||
"request": "launch", | ||
"program": "${workspaceFolder}/src/tests/rust_guests/bin/debug/simpleguest", | ||
"args": [], | ||
"stopAtEntry": true, | ||
"hardwareBreakpoints": {"require": false, "limit": 4}, | ||
"cwd": "${workspaceFolder}", | ||
"environment": [], | ||
"externalConsole": false, | ||
"MIMode": "gdb", | ||
"miDebuggerPath": "/usr/bin/gdb", | ||
"miDebuggerServerAddress": "localhost:8080", | ||
"setupCommands": [ | ||
{ | ||
"description": "Enable pretty-printing for gdb", | ||
"text": "-enable-pretty-printing", | ||
"ignoreFailures": true | ||
}, | ||
{ | ||
"description": "Set Disassembly Flavor to Intel", | ||
"text": "-gdb-set disassembly-flavor intel", | ||
"ignoreFailures": true | ||
} | ||
] | ||
} | ||
] | ||
} | ||
``` | ||
- in `Run and Debug` tab, select the `GDB` configuration and click on the `Run` | ||
button to start the debugging session. | ||
The gdb client will connect to the Hyperlight sandbox and the guest vCPU will | ||
stop at the entry point. | ||
|
||
|
||
## How it works | ||
|
||
The gdb feature is designed to work like a Request - Response protocol between | ||
a thread that accepts commands from a gdb client and the hypervisor handler over | ||
a communication channel. | ||
|
||
All the functionality is implemented on the hypervisor side so it has access to | ||
the shared memory and the vCPU. | ||
|
||
The gdb thread uses the `gdbstub` crate to handle the communication with the gdb client. | ||
When the gdb client requests one of the supported features mentioned above, a request | ||
is sent over the communication channel to the hypervisor handler for the sandbox | ||
to resolve. | ||
|
||
Below is a sequence diagram that shows the interaction between the entities | ||
involved in the gdb debugging of a Hyperlight guest running inside a KVM sandbox. | ||
|
||
``` | ||
┌───────────────────────────────────────────────────────────────────────────────────────────────┐ | ||
│ Hyperlight Sandbox │ | ||
USER │ │ | ||
┌────────────┐ │ ┌──────────────┐ ┌───────────────────────────┐ ┌────────┐ │ | ||
│ gdb client │ │ │ gdb thread │ │ hypervisor handler thread │ │ vCPU │ │ | ||
└────────────┘ │ └──────────────┘ └───────────────────────────┘ └────────┘ │ | ||
| │ | create_gdb_thread | | │ | ||
| │ |◄─────────────────────────────────────────┌─┐ vcpu stopped ┌─┐ │ | ||
| attach │ ┌─┐ │ │◄──────────────────────────────┴─┘ │ | ||
┌─┐───────────────────────┼────────►│ │ │ │ entrypoint breakpoint | │ | ||
│ │ attach response │ │ │ │ │ | │ | ||
│ │◄──────────────────────┼─────────│ │ │ │ | │ | ||
│ │ │ │ │ │ │ | │ | ||
│ │ add_breakpoint │ │ │ │ │ | │ | ||
│ │───────────────────────┼────────►│ │ add_breakpoint │ │ | │ | ||
│ │ │ │ │────────────────────────────────────────►│ │ add_breakpoint | │ | ||
│ │ │ │ │ │ │────┐ | │ | ||
│ │ │ │ │ │ │ │ | │ | ||
│ │ │ │ │ │ │◄───┘ | │ | ||
│ │ │ │ │ add_breakpoint response │ │ | │ | ||
│ │ add_breakpoint response │ │◄────────────────────────────────────────│ │ | │ | ||
│ │◄──────────────────────┬─────────│ │ │ │ | │ | ||
│ │ continue │ │ │ │ │ | │ | ||
│ │───────────────────────┼────────►│ │ continue │ │ | │ | ||
│ │ │ │ │────────────────────────────────────────►│ │ resume vcpu | │ | ||
│ │ │ │ │ │ │──────────────────────────────►┌─┐ │ | ||
│ │ │ │ │ │ │ │ │ │ | ||
│ │ │ │ │ │ │ │ │ │ | ||
│ │ │ │ │ │ │ │ │ │ | ||
│ │ │ │ │ │ │ │ │ │ | ||
│ │ │ │ │ │ │ vcpu stopped │ │ │ | ||
│ │ │ │ │ notify vcpu stop reason │ │◄──────────────────────────────┴─┘ │ | ||
│ │ notify vcpu stop reason │ │◄────────────────────────────────────────│ │ | │ | ||
│ │◄──────────────────────┬─────────│ │ │ │ | │ | ||
│ │ continue until end │ │ │ │ │ | │ | ||
│ │───────────────────────┼────────►│ │ continue │ │ resume vcpu | │ | ||
│ │ │ │ │────────────────────────────────────────►│ │──────────────────────────────►┌─┐ │ | ||
│ │ │ │ │ │ │ │ │ │ | ||
│ │ │ │ │ comm channel disconnected │ │ vcpu halted │ │ │ | ||
│ │ target finished exec│ │ │◄────────────────────────────────────────┤ │◄──────────────────────────────┴─┘ │ | ||
│ │◄──────────────────────┼─────────┴─┘ target finished exec └─┘ | │ | ||
│ │ │ | | | │ | ||
└─┘ │ | | | │ | ||
| └───────────────────────────────────────────────────────────────────────────────────────────────┘ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.