- 
                Notifications
    
You must be signed in to change notification settings  - Fork 2.1k
 
feat: Enable gdb debugging on x86 #4797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            JackThomson2
  merged 4 commits into
  firecracker-microvm:main
from
JackThomson2:feat/gdb_server_x86
  
      
      
   
  Oct 10, 2024 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
      
      Oops, something went wrong.
      
    
  
  
    
      This file contains hidden or 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,110 @@ | ||
| # GDB Debugging with Firecracker | ||
| 
     | 
||
| Firecracker supports debugging the guest kernel via GDB remote serial protocol. | ||
| This allows us to connect GDB to the firecracker process and step through debug | ||
| the guest kernel. Currently only debugging on x86 is supported. | ||
| 
     | 
||
| The GDB feature requires Firecracker to be booted with a config file. | ||
| 
     | 
||
| ## Prerequisites | ||
| 
     | 
||
| Firstly, to enable GDB debugging we need to compile Firecracker with the `debug` | ||
| feature enabled, this will enable the necessary components for the debugging | ||
| process. | ||
| 
     | 
||
| To build firecracker with the `gdb` feature enabled we run: | ||
| 
     | 
||
| ```bash | ||
| cargo build --features "gdb" | ||
| ``` | ||
| 
     | 
||
| Secondly, we need to compile a kernel with specific features enabled for | ||
| debugging to work. The key config options to enable are: | ||
| 
     | 
||
| ``` | ||
| CONFIG_FRAME_POINTER=y | ||
| CONFIG_KGDB=y | ||
| CONFIG_KGDB_SERIAL_CONSOLE=y | ||
| CONFIG_DEBUG_INFO=y | ||
| ``` | ||
| 
     | 
||
| For GDB debugging the `gdb-socket` option should be set in your config file. In | ||
| this example we set it to `/tmp/gdb.socket` | ||
| 
     | 
||
| ``` | ||
| { | ||
| ... | ||
| "gdb-socket": "/tmp/gdb.socket" | ||
| ... | ||
| } | ||
| ``` | ||
| 
     | 
||
| ## Starting Firecracker with GDB | ||
| 
     | 
||
| With all the prerequisites in place you can now start firecracker ready to | ||
| connect to GDB. When you start the firecracker binary now you'll notice it'll be | ||
| blocked waiting for the GDB connection. This is done to allow us to set | ||
| breakpoints before the boot process begins. | ||
| 
     | 
||
| With Firecracker running and waiting for GDB we are now able to start GDB and | ||
| connect to Firecracker. You may need to set the permissions of your GDB socket | ||
| E.g. `/tmp/gdb.socket` to `0666` before connecting. | ||
| 
     | 
||
| An example of the steps taken to start GDB, load the symbols and connect to | ||
| Firecracker: | ||
| 
     | 
||
| 1. Start the GDB process, you can attach the symbols by appending the kernel | ||
| blob, for example here `vmlinux` | ||
| 
     | 
||
| ```bash | ||
| gdb vmlinux | ||
| ``` | ||
                
      
                  ShadowCurse marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| 1. When GDB has started set the target remote to `/tmp/gdb.socket` to connect to | ||
| Firecracker | ||
| 
     | 
||
| ```bash | ||
| (gdb) target remote /tmp/gdb.socket | ||
| ``` | ||
| 
     | 
||
| With these steps completed you'll now see GDB has stopped at the entry point | ||
| ready for us to start inserting breakpoints and debugging. | ||
| 
     | 
||
| ## Notes | ||
| 
     | 
||
| ### Software Breakpoints not working on start | ||
| 
     | 
||
| When at the initial paused state you'll notice software breakpoints won't work | ||
| and only hardware breakpoints will until memory virtualisation is enabled. To | ||
| circumvent this one solution is to set a hardware breakpoint at `start_kernel` | ||
| and continue. Once you've hit the `start_kernel` set the regular breakpoints as | ||
| you would do normally. E.g. | ||
| 
     | 
||
| ```bash | ||
| > hbreak start_kernel | ||
| > c | ||
| ``` | ||
| 
     | 
||
| ### Pausing Firecracker while it's running | ||
| 
     | 
||
| While Firecracker is running you can pause vcpu 1 by pressing `Ctrl+C` which | ||
                
      
                  JackThomson2 marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| will stop the vcpu and allow you to set breakpoints or inspect the current | ||
| location. | ||
| 
     | 
||
| ### Halting execution of GDB and Firecracker | ||
| 
     | 
||
| To end the debugging session and shut down Firecracker you can run the `exit` | ||
| command in the GDB session which will terminate both. | ||
| 
     | 
||
| ## Known limitations | ||
| 
     | 
||
| - The multi-core scheduler can in some cases cause issues with GDB, this can be | ||
| mitigated by setting these kernel config values: | ||
| 
     | 
||
| ``` | ||
| CONFIG_SCHED_MC=y | ||
| CONFIG_SCHED_MC_PRIO=y | ||
| ``` | ||
| 
     | 
||
| - Currently we support a limited subset of cpu registers for get and set | ||
| operations, if more are required feel free to contribute. | ||
  
    
      This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,62 @@ | ||
| // Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| 
     | 
||
| use gdbstub_arch::aarch64::reg::AArch64CoreRegs as CoreRegs; | ||
| use kvm_ioctls::VcpuFd; | ||
| use vm_memory::GuestAddress; | ||
| 
     | 
||
| use crate::gdb::target::GdbTargetError; | ||
| 
     | 
||
| /// Configures the number of bytes required for a software breakpoint | ||
| pub const SW_BP_SIZE: usize = 1; | ||
| 
     | 
||
| /// The bytes stored for a software breakpoint | ||
| pub const SW_BP: [u8; SW_BP_SIZE] = [0]; | ||
| 
     | 
||
| /// Gets the RIP value for a Vcpu | ||
| pub fn get_instruction_pointer(_vcpu_fd: &VcpuFd) -> Result<u64, GdbTargetError> { | ||
| unimplemented!() | ||
| } | ||
| 
     | 
||
| /// Translates a virtual address according to the vCPU's current address translation mode. | ||
| pub fn translate_gva(_vcpu_fd: &VcpuFd, _gva: u64) -> Result<u64, GdbTargetError> { | ||
| unimplemented!() | ||
| } | ||
| 
     | 
||
| /// Configures the kvm guest debug regs to register the hardware breakpoints | ||
| fn set_kvm_debug( | ||
| _control: u32, | ||
| _vcpu_fd: &VcpuFd, | ||
| _addrs: &[GuestAddress], | ||
| ) -> Result<(), GdbTargetError> { | ||
| unimplemented!() | ||
| } | ||
| 
     | 
||
| /// Configures the Vcpu for debugging and sets the hardware breakpoints on the Vcpu | ||
| pub fn vcpu_set_debug( | ||
| _vcpu_fd: &VcpuFd, | ||
| _addrs: &[GuestAddress], | ||
| _step: bool, | ||
| ) -> Result<(), GdbTargetError> { | ||
| unimplemented!() | ||
| } | ||
| 
     | 
||
| /// Injects a BP back into the guest kernel for it to handle, this is particularly useful for the | ||
| /// kernels selftesting which can happen during boot. | ||
| pub fn vcpu_inject_bp( | ||
| _vcpu_fd: &VcpuFd, | ||
| _addrs: &[GuestAddress], | ||
| _step: bool, | ||
| ) -> Result<(), GdbTargetError> { | ||
| unimplemented!() | ||
| } | ||
| 
     | 
||
| /// Reads the registers for the Vcpu | ||
| pub fn read_registers(_vcpu_fd: &VcpuFd, _regs: &mut CoreRegs) -> Result<(), GdbTargetError> { | ||
| unimplemented!() | ||
| } | ||
| 
     | 
||
| /// Writes to the registers for the Vcpu | ||
| pub fn write_registers(_vcpu_fd: &VcpuFd, _regs: &CoreRegs) -> Result<(), GdbTargetError> { | ||
| unimplemented!() | ||
| } | ||
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.