Create compilation database (compile_commands.json) for NetBSD-kernel source
Tags: netbsd, debug, c, kernel
Published: 2018-12-23 09:29:18

---
title: Create compilation database (compile_commands.json) for NetBSD-kernel source
published: true
description: explains how to create compile_commands.json for netbsd-src/sys/ directory
tags: netbsd, debug, c, kernel
---

<!--
Accurate code jumping across NetBSD-kernel source with compilation database
-->

![Peek 2018-12-23 16-49.gif](https://qiita-image-store.s3.amazonaws.com/0/167409/744421a3-26f2-50ca-a59f-46fcc9fec6a4.gif)

[にほんご](https://qiita.com/wataash/items/6844815d28477109cfc0)

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](https://clang.llvm.org/docs/JSONCompilationDatabase.html) 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](https://github.com/rizsotto/Bear/issues/212). And it's may be avoided by using [scan-build](https://github.com/rizsotto/scan-build))

```sh
# 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
```

Then open the kernel sources with your preferred editor that supports compilation database, such like vim with [YouCompleteMe](https://github.com/Valloric/YouCompleteMe#c-family-semantic-completion) or [CLion](https://www.jetbrains.com/help/clion/compilation-database.html) (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
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/167409/aba7814e-7157-603e-94f9-4507b3a03d35.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:

```sh
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
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/167409/c9d37160-1359-8671-4784-b3935f452ec1.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.