|
| 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. |
| 6 | + |
| 7 | +The GDB feature requires Firecracker to be booted with a config file. |
| 8 | + |
| 9 | +## Prerequiesites |
| 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 `debug` 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 on 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 | +It can also be worth disabling the multi core scheduler as this can cause issues |
| 32 | +with GDB these are set under these configs and can be disabled if you encounter |
| 33 | +issues |
| 34 | + |
| 35 | +``` |
| 36 | +CONFIG_SCHED_MC=y |
| 37 | +CONFIG_SCHED_MC_PRIO=y |
| 38 | +``` |
| 39 | + |
| 40 | +For GDB debugging the `gdb-socket` option should be set in your config file in |
| 41 | +this example we set it to `/tmp/gdb.socket` |
| 42 | + |
| 43 | +``` |
| 44 | +{ |
| 45 | + ... |
| 46 | + "gdb-socket": "/tmp/gdb.socket" |
| 47 | + ... |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +## Starting Firecracker with GDB |
| 52 | + |
| 53 | +With all the prerequisites in place you can now start firecracker ready to |
| 54 | +connect to GDB. When you start the firecracker binary now you'll notice it'll be |
| 55 | +blocked waiting for the GDB connection this is done to allow us to set |
| 56 | +breakpoints before the boot process begins. |
| 57 | + |
| 58 | +With Firecracker running and waiting for GDB we are now able to start GDB and |
| 59 | +connect to Firecracker. You may need to set the permissions of your gdb socket |
| 60 | +e.g. `/tmp/gdb.socket` to `777` before connecting. |
| 61 | + |
| 62 | +An example of the steps taken to start GDB, load the symbols and connect to |
| 63 | +Firecracker: |
| 64 | + |
| 65 | +1. Start the GDB process, you can attach the symbols by appending the kernel |
| 66 | + blob for example here `vmlinux` |
| 67 | + |
| 68 | + ```bash |
| 69 | + gdb vmlinux |
| 70 | + ``` |
| 71 | + |
| 72 | +1. When GDB has started set the target remote to `/tmp/gdb.socket` to connect to |
| 73 | + Firecracker |
| 74 | + |
| 75 | + ```bash |
| 76 | + (gdb) target remote /tmp/gdb.socket |
| 77 | + ``` |
| 78 | + |
| 79 | +With these steps completed you'll now see GDB has stopped at the entry point |
| 80 | +ready for us to start inserting breakpoints and debugging. |
| 81 | + |
| 82 | +## Notes |
| 83 | + |
| 84 | +### Software Breakpoints not working on start |
| 85 | + |
| 86 | +When at the initial paused state you'll notice software breakpoints won't work |
| 87 | +and only hardware breakpoints will until pages are setup. To circumvent this one |
| 88 | +solution is to set a hardware breakpoint at start_kernel and continue. Once |
| 89 | +you've hit the start_kernel set the regular breakpoints as you would do |
| 90 | +normally. E.G. |
| 91 | + |
| 92 | +```bash |
| 93 | +> hbreak start_kernel |
| 94 | +> c |
| 95 | +``` |
| 96 | + |
| 97 | +### Pausing Firecracker while it's running |
| 98 | + |
| 99 | +While Firecracker is running you can pause vcpu 1 by pressing `Ctrl+C` which |
| 100 | +will stop the VCPU and allow you to set breakpoints or inspect the current |
| 101 | +location |
| 102 | + |
| 103 | +### Halting execution of GDB and Firecracker |
| 104 | + |
| 105 | +To end the debugging session and shutdown Firecracker you can run the `exit` |
| 106 | +command in the GDB session which will terminate both |
| 107 | + |
| 108 | +### Limited CPU registers |
| 109 | + |
| 110 | +Currently we support a limited subset of cpu registers for get and set |
| 111 | +operations, if more are required feel free to contribute. |
0 commit comments