ccls is a great tool to inspect the kernel source code

Preview

vscode_ccls

0x00. Enviroment

Mac OS 10.14.x

Xcode 10.x

Homebrew

0x01. Setup

Install ccls

You should have homebrew installed at first. If you don't, go to brew.sh and follow the instructions

$ brew install ccls

Install Visual Studio Code

You should have Visual Studio Code installed to work with ccls.

$ brew cask install visual-studio-code

0x02. VS Code Setup

  1. Using CMD + Shift + P and type ext install

  2. install extension cccls

0x03. Compile XNU Kernel

Mac OS Catalina

If you are building 10.14.x kernel using Mac OS 10.15 and Xcode 11 , install command lines tools on your 10.14 machine and copy old sdk to your 10.15 Machine with followling command.

$ scp -r user@IP:/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/

xnu-build

$ git clone https://github.com/PureDarwin/xnubuild.git
$ cd xnubuild

0x04. Patch With JSONDB

add JSONCOMPILATIONDB

Save the following as a ccls.patch

diff --git a/xnubuild.sh b/xnubuild.sh
index 413c626..5d47f6d 100755
--- a/xnubuild.sh
+++ b/xnubuild.sh
@@ -60,7 +60,7 @@ DTRACE_VERSION=dtrace-284.200.15
 AVAILABILITYVERSIONS_VERSION=AvailabilityVersions-33.200.4
 LIBPLATFORM_VERSION=libplatform-177.200.16

-SDK_ROOT=`xcodebuild -version -sdk macosx Path`
+SDK_ROOT=`xcodebuild -version -sdk macosx10.14 Path`

 # Wait for user input
 function wait_enter {
@@ -165,8 +165,8 @@ print "Installing XNU & LibSyscall headers"
                patch -s -p1 < $PATCH_DIRECTORY/xnu/fix_system_framework.patch && \
                patch -s -p1 < $PATCH_DIRECTORY/xnu/xnu_dependencies_dir.patch && \
                patch -s -p1 < $PATCH_DIRECTORY/xnu/bsd-xcconfig.patch && \
-               DEPENDENCIES_DIR=$BUILD_DIR/dependencies make installhdrs SDKROOT=macosx ARCH_CONFIGS=X86_64 SRCROOT=$PWD OBJROOT=$BUILD_DIR/$XNU_VERSION.hdrs.obj SYMROOT=$BUILD_DIR/$XNU_VERSION.hdrs.sym DSTROOT=$BUILD_DIR/$XNU_VERSION.hdrs.dst && \
-               xcodebuild installhdrs -project libsyscall/Libsyscall.xcodeproj -sdk macosx SRCROOT=$PWD/libsyscall OBJROOT=$BUILD_DIR/$XNU_VERSION.hdrs.obj SYMROOT=$BUILD_DIR/$XNU_VERSION.hdrs.sym DSTROOT=$BUILD_DIR/$XNU_VERSION.hdrs.dst DEPENDENCIES_DIR=$BUILD_DIR/dependencies && \
+               DEPENDENCIES_DIR=$BUILD_DIR/dependencies make installhdrs SDKROOT=macosx10.14 ARCH_CONFIGS=X86_64 SRCROOT=$PWD OBJROOT=$BUILD_DIR/$XNU_VERSION.hdrs.obj SYMROOT=$BUILD_DIR/$XNU_VERSION.hdrs.sym DSTROOT=$BUILD_DIR/$XNU_VERSION.hdrs.dst JSONCOMPILATIONDB=1 && \
+               xcodebuild installhdrs -project libsyscall/Libsyscall.xcodeproj -sdk macosx SRCROOT=$PWD/libsyscall OBJROOT=$BUILD_DIR/$XNU_VERSION.hdrs.obj SYMROOT=$BUILD_DIR/$XNU_VERSION.hdrs.sym DSTROOT=$BUILD_DIR/$XNU_VERSION.hdrs.dst DEPENDENCIES_DIR=$BUILD_DIR/dependencies JSONCOMPILATIONDB=1 && \
                ditto $BUILD_DIR/$XNU_VERSION.hdrs.dst $BUILD_DIR/dependencies
 } || {
        error "Failed to build XNU & LibSyscall headers"
@@ -211,7 +211,7 @@ print "Building XNU, sudo password may be required"
        cd $SCRIPT_DIRECTORY/$XNU_VERSION && \
                patch -s -p1 < $PATCH_DIRECTORY/xnu/kext_copyright_check.patch && \
                patch -s -p1 < $PATCH_DIRECTORY/xnu/xnu_firehose_dir.patch && \
-               sudo env DEPENDENCIES_DIR=$BUILD_DIR/dependencies make install SDKROOT=macosx ARCH_CONFIGS=X86_64 KERNEL_CONFIGS=RELEASE OBJROOT=$BUILD_DIR/$XNU_VERSION.obj SYMROOT=$BUILD_DIR/$XNU_VERSION.sym DSTROOT=$BUILD_DIR/$XNU_VERSION.dst DEPENDENCIES_DIR=$BUILD_DIR/dependencies BUILD_WERROR=0 BUILD_LTO=0
+               sudo env DEPENDENCIES_DIR=$BUILD_DIR/dependencies make install SDKROOT=macosx10.14 ARCH_CONFIGS=X86_64 KERNEL_CONFIGS=RELEASE OBJROOT=$BUILD_DIR/$XNU_VERSION.obj SYMROOT=$BUILD_DIR/$XNU_VERSION.sym DSTROOT=$BUILD_DIR/$XNU_VERSION.dst DEPENDENCIES_DIR=$BUILD_DIR/dependencies BUILD_WERROR=0 BUILD_LTO=0 JSONCOMPILATIONDB=1
 } || {
        error "Failed to build XNU"
        exit 1
@@ -223,7 +223,7 @@ print "Building Libsyscall, sudo password may be required"
        mkdir -p $BUILD_DIR/Libsyscall.{obj,sym}
        cd $SCRIPT_DIRECTORY/$XNU_VERSION && \
                patch -s -p1 < $PATCH_DIRECTORY/xnu/libsyscall-build.patch && \
-               sudo env DEPENDENCIES_DIR=$BUILD_DIR/dependencies RC_ProjectName=Libsyscall make install SDKROOT=macosx OBJROOT=$BUILD_DIR/Libsyscall.obj SYMROOT=$BUILD_DIR/Libsyscall.sym DSTROOT=$BUILD_DIR/$XNU_VERSION.dst
+               sudo env DEPENDENCIES_DIR=$BUILD_DIR/dependencies RC_ProjectName=Libsyscall make install SDKROOT=macosx10.14 OBJROOT=$BUILD_DIR/Libsyscall.obj SYMROOT=$BUILD_DIR/Libsyscall.sym DSTROOT=$BUILD_DIR/$XNU_VERSION.dst JSONCOMPILATIONDB=1
 } || {
        error "Failed to build Libsyscall"
        exit 1

And apply patch

$ patch -s -p1 < ccls.patch

0x05. Using JsonDB with project

For example, current xnu-build is working on 10.14.3

$ ./xnubuild.sh -travis
# copy output to source code tree
$ cp ./build/xnu-4903.241.1.obj/RELEASE_X86_64/compile_commands.json ./xnu-4903.241.1
$ cd ./xnu-4903.241.1

# setup your project and open with vscode
$ git init && git add . && git commit -m 'init'
$ code .