|
| 1 | +# GDB Debugging with Firecracker |
| 2 | + |
| 3 | +Firecracker supports debugging the guest kernel via GDB remote serial protocol. |
| 4 | +This allows us to connect GDB to the firecracker process and step through debug |
| 5 | +the guest kernel. Currently only debugging on x86 is supported. |
| 6 | + |
| 7 | +The GDB feature requires Firecracker to be booted with a config file. |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +Firstly, to enable GDB debugging we need to compile Firecracker with the `debug` |
| 12 | +feature enabled, this will enable the necessary components for the debugging |
| 13 | +process. |
| 14 | + |
| 15 | +To build firecracker with the `gdb` feature enabled we run: |
| 16 | + |
| 17 | +```bash |
| 18 | +cargo build --features "gdb" |
| 19 | +``` |
| 20 | + |
| 21 | +Secondly, we need to compile a kernel with specific features enabled for |
| 22 | +debugging to work. The key config options to enable are: |
| 23 | + |
| 24 | +``` |
| 25 | +CONFIG_FRAME_POINTER=y |
| 26 | +CONFIG_KGDB=y |
| 27 | +CONFIG_KGDB_SERIAL_CONSOLE=y |
| 28 | +CONFIG_DEBUG_INFO=y |
| 29 | +``` |
| 30 | + |
| 31 | +For GDB debugging the `gdb-socket` option should be set in your config file. In |
| 32 | +this example we set it to `/tmp/gdb.socket` |
| 33 | + |
| 34 | +``` |
| 35 | +{ |
| 36 | + ... |
| 37 | + "gdb-socket": "/tmp/gdb.socket" |
| 38 | + ... |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +## Starting Firecracker with GDB |
| 43 | + |
| 44 | +With all the prerequisites in place you can now start firecracker ready to |
| 45 | +connect to GDB. When you start the firecracker binary now you'll notice it'll be |
| 46 | +blocked waiting for the GDB connection. This is done to allow us to set |
| 47 | +breakpoints before the boot process begins. |
| 48 | + |
| 49 | +With Firecracker running and waiting for GDB we are now able to start GDB and |
| 50 | +connect to Firecracker. You may need to set the permissions of your GDB socket |
| 51 | +E.g. `/tmp/gdb.socket` to `0666` before connecting. |
| 52 | + |
| 53 | +An example of the steps taken to start GDB, load the symbols and connect to |
| 54 | +Firecracker: |
| 55 | + |
| 56 | +1. Start the GDB process, you can attach the symbols by appending the kernel |
| 57 | + blob, for example here `vmlinux` |
| 58 | + |
| 59 | + ```bash |
| 60 | + gdb vmlinux |
| 61 | + ``` |
| 62 | + |
| 63 | +1. When GDB has started set the target remote to `/tmp/gdb.socket` to connect to |
| 64 | + Firecracker |
| 65 | + |
| 66 | + ```bash |
| 67 | + (gdb) target remote /tmp/gdb.socket |
| 68 | + ``` |
| 69 | + |
| 70 | +With these steps completed you'll now see GDB has stopped at the entry point |
| 71 | +ready for us to start inserting breakpoints and debugging. |
| 72 | + |
| 73 | +## Notes |
| 74 | + |
| 75 | +### Software Breakpoints not working on start |
| 76 | + |
| 77 | +When at the initial paused state you'll notice software breakpoints won't work |
| 78 | +and only hardware breakpoints will until memory virtualisation is enabled. To |
| 79 | +circumvent this one solution is to set a hardware breakpoint at `start_kernel` |
| 80 | +and continue. Once you've hit the start_kernel set the regular breakpoints as |
| 81 | +you would do normally. E.g. |
| 82 | + |
| 83 | +```bash |
| 84 | +> hbreak start_kernel |
| 85 | +> c |
| 86 | +``` |
| 87 | + |
| 88 | +### Pausing Firecracker while it's running |
| 89 | + |
| 90 | +While Firecracker is running you can pause vcpu 1 by pressing `Ctrl+C` which |
| 91 | +will stop the vcpu and allow you to set breakpoints or inspect the current |
| 92 | +location. |
| 93 | + |
| 94 | +### Halting execution of GDB and Firecracker |
| 95 | + |
| 96 | +To end the debugging session and shut down Firecracker you can run the `exit` |
| 97 | +command in the GDB session which will terminate both. |
| 98 | + |
| 99 | +## Known limitations |
| 100 | + |
| 101 | +- The multi-core scheduler can in some cases cause issues with GDB, this can be |
| 102 | + mitigated by setting these kernel config values: |
| 103 | + |
| 104 | + ``` |
| 105 | + CONFIG_SCHED_MC=y |
| 106 | + CONFIG_SCHED_MC_PRIO=y |
| 107 | + ``` |
| 108 | + |
| 109 | +- Currently we support a limited subset of cpu registers for get and set |
| 110 | + operations, if more are required feel free to contribute. |
0 commit comments