DEV Community

Wataru Ashihara
Wataru Ashihara

Posted on • Edited on

Create compilation database (compile_commands.json) for NetBSD-kernel source

Peek 2018-12-23 16-49.gif

にほんご

Source code structure of NetBSD kernel (netbsd-src/sys/) is so complicated that it is almost impossible for a static-source-analysis tool to provide an accurate location of a symbol. For an example, cscope can't understand where <machine/endian.h> is:

  • sys/arch/hpc/stand/include/machine/endian.h?
  • or sys/arch/hpcmips/stand/include/machine/endian.h?

Actually it's ${OBJDIR}/sys/arch/amd64/compile/GENERIC/machine/endian.h! cscope doesn't know that the compilation is done in ${OBJDIR}/sys/arch/amd64/compile/GENERIC with the -I. flag.

To realize an accurate code analysis, compiling information is needed -- so-called compilation database or compile_commands.json.

Here's how to create compile_commands.json for NetBSD kernel on Linux (Ubuntu 18.04):
(I'll write additional information when it's done on NetBSD and macOS soon... NetBSD may be faceed with an issue in bear. And it's may be avoided by using scan-build)



# https://github.com/rizsotto/Bear/
sudo apt install bear

cd netbsd-src/

# First, create ${OBJDIR}/sys/arch/amd64/compile/GENERIC/Makefile
# and confirm that the kernel can be built
./build.sh kernel=GENERIC

cd ${OBJDIR}/sys/arch/amd64/compile/GENERIC/
${TOOLDIR}/bin/nbmake-amd64 clean
bear ${TOOLDIR}/bin/nbmake-amd64             # creates compile_commands.json
mv compile_commands.json ${NETBSD-SRC}/sys/  # optional


Enter fullscreen mode Exit fullscreen mode

Then open the kernel sources with your preferred editor that supports compilation database, such like vim with YouCompleteMe or CLion (used in this article). You may get errors from cc:



Compiler exited with error code 1: /usr/bin/cc -xc -c -mcmodel=kernel (snip) -v -dD -E
    Using built-in specs.
    (snip)
    End of search list.
    cc1: error: code model kernel does not support PIC mode


Enter fullscreen mode Exit fullscreen mode

image.png

The editor executes /usr/bin/cc for the code analysis, whereas it's should be ${TOOLDIR}/bin/x86_64--netbsd-gcc[--<target>] This problem can be resolved by simply replacing the path to the compiler:



cp compile_commands.json compile_commands.json.bak
sed -i s!'"cc",'!'"/home/wsh/src/netbsd/tools/bin/x86_64--netbsd-gcc",'!g compile_commands.json


Enter fullscreen mode Exit fullscreen mode

image.png

Enjoy code jumping!

For maintainers of official documentations

I'd be glad to migrate all or part of this and other articles to official documentations. Please make a comment here or contact me to do so.

Top comments (0)