Building a FreeBSD kernel for debugging

You may want to actually debug the kernel or a kernel module, however, debugging is not enabled by default.


Before getting started it is assumed you have a FreeBSD Install and you have downloaded/installed FreeBSD Source.

The steps are identical to the steps contained in the How to build and install a custom kernel on FreeBSD? article, with the exception of the kernel configuration file.

Step 1 – Create a new kernel config

  1. Determine your architecture by running this command:
    # uname -a
    FreeBSD 9.0-RELEASE FreeBSD 9.0-RELEASE #0: Tue Jan 3 07:46:30 UTC 2012 amd64
  2. Look at the last text item in the output string. I have amd64 so that is my architecture: amd64
  3. Change to the directory of the kernel configuration files for your architecture. Remember if you are on a different architecture to replace amd64 in the following command lien with your architecture.
    # cd /usr/src/sys/amd64/conf
  4. Copy GENERIC to a new file.
  5. Edit KRNLDBG with your favorite text editor.
  6. First, change the ident value near the top from GENERIC to KRNLDBG.
    ident           KRNLDBG
  7. Add debugging settings to your KRNLDBG configuraiton file.
    options           KDB
    options           KDB_TRACE
    options           DDB
    options           GDB
  8. Save and close your new KRNLDBG configuration file.

For the remaining steps, follow the How to build and install a custom kernel on FreeBSD? article, only replace any references to KRNL1 kernel config file with the KRNLDBG config file created in the previous step.

Once you are done building and installing the kernel, you should have debugging enabled.

I happen to be working with a kernel module that is crashing and when it crashes, it automatically places me in a debugging session with the following prompt.



  1. Faisal Shahzad says:

    Hi Rhyous,
    I visited your site through Google and read many articles, which gave me too much guideline. I am using FreeBSD since version 5.6. But i couldn't pay attension seriously.
    Your site give me a new passion.
    Thanks for creating a good site.

  2. […] KGDB with VirtualBox: Debug a Live Kernel (Linux) FreeBSD 10; new, KMS, PKGNG, poudriere… Building a FreeBSD kernel for debugging How to build and install a custom kernel on […]

Leave a Reply

How to post code in comments?