|
| 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 | +## Prerequiesites |
| 8 | + |
| 9 | +Firstly to enable GDB debugging we need to compile Firecracker with the `debug` |
| 10 | +feature enabled, this will enable the necessary components for the debugging |
| 11 | +process. |
| 12 | + |
| 13 | +To build firecracker with the `debug` feature enabled we run: |
| 14 | + |
| 15 | +```bash |
| 16 | +cargo build --features "debug" |
| 17 | +``` |
| 18 | + |
| 19 | +Secondly we need to compile a kernel with specific features enabled for |
| 20 | +debugging to work, the key features to enable on the kernel are: |
| 21 | + |
| 22 | +``` |
| 23 | +CONFIG_FRAME_POINTER=y |
| 24 | +CONFIG_KGDB=y |
| 25 | +CONFIG_KGDB_SERIAL_CONSOLE=y |
| 26 | +CONFIG_DEBUG_INFO |
| 27 | +``` |
| 28 | + |
| 29 | +It can also be worth disabling the multi core scheduler as this can cause issues |
| 30 | +with GDB these are set under these configs and can be disabled if you encounter |
| 31 | +issues |
| 32 | + |
| 33 | +``` |
| 34 | +CONFIG_SCHED_MC=y |
| 35 | +CONFIG_SCHED_MC_PRIO=y |
| 36 | +``` |
| 37 | + |
| 38 | +## Starting Firecracker with GDB |
| 39 | + |
| 40 | +With all the prerequiesites in place you can now start firecracker ready to |
| 41 | +connect to GDB. When you start the firecracker binary now you'll notice it'll be |
| 42 | +blocked waiting for the GDB connection this is done to allow us to set |
| 43 | +breakpoints before the boot process begins. |
| 44 | + |
| 45 | +With Firecracker running and waiting for GDB we are now able to start GDB and |
| 46 | +connect to Firecracker. You may need to set the permissions of `/tmp/gdb.socket` |
| 47 | +to `777` before connecting. |
| 48 | + |
| 49 | +An example of the steps taken to start GDB, load the symbols and connect to |
| 50 | +Firecracker. |
| 51 | + |
| 52 | +1. Start the GDB process, you can attach the symbols by appending the kernel |
| 53 | + blob for example here `vmlinux` |
| 54 | + |
| 55 | + ```bash |
| 56 | + gdb vmlinux |
| 57 | + ``` |
| 58 | + |
| 59 | +1. When GDB has started set the target remote to `/tmp/gdb.socket` to connect to |
| 60 | + Firecracker |
| 61 | + |
| 62 | + ```bash |
| 63 | + (gdb) target remote /tmp/gdb.socket |
| 64 | + ``` |
| 65 | + |
| 66 | +With these steps completed you'll now see GDB has stopped at the entry point |
| 67 | +ready for us to start inserting breakpoints and debugging. |
| 68 | + |
| 69 | +## Notes |
| 70 | + |
| 71 | +### Software Breakpoints not working on start |
| 72 | + |
| 73 | +When at the initial paused state you'll notice software breakpoints won't work |
| 74 | +and only hardware breakpoints will until pages are setup. To circumvent this one |
| 75 | +solution is to set a hardware breakpoint at start_kernel and continue. Once |
| 76 | +you've hit the start_kernel set the regular breakpoints as you would do |
| 77 | +normally. E.G. |
| 78 | + |
| 79 | +```bash |
| 80 | +> hbreak start_kernel |
| 81 | +> c |
| 82 | +``` |
| 83 | + |
| 84 | +### Pausing Firecracker while it's running |
| 85 | + |
| 86 | +While Firecracker is running you can pause vcpu 1 by pressing `Ctrl+C` which |
| 87 | +will stop the VCPU and allow you to set breakpoints or inspect the current |
| 88 | +location |
| 89 | + |
| 90 | +### Halting execution of GDB and Firecracker |
| 91 | + |
| 92 | +To end the debugging session and shutdown Firecracker you can run the `exit` |
| 93 | +command in the GDB session which will terminate both |
0 commit comments