Problem Statement
For several days, I have been trying to generate an Abstract Syntax Tree (AST) for part of the Linux kernel filesystem, and cannot get it to work. I am able to make the kernel, but when I try to generate the AST I am plagued by hundreds errors and warnings complaining about redefined macros, missing typedef's, double underscores, missing semicolons, etc. All syntax stuff.
What I've Tried
- I've tried cloning the linux repo at https://github.com/torvalds/linux as well as downloading the source code directly from https://www.kernel.org/.
- I've tried using
clang,gcc,astgen,pyparser,joern-frontend,sparse- the most promising one wasclang. - I've tried running
clang -Xclang -ast-dump -fsyntax-only linux/fs/fuse/xattr.c(for example) on the raw code as well as after configuring and runningmake, same errors. - I've tried this on my Mac as well as a Linux VM running Ubuntu 16.0.4.
- I've tried this on the
rpiversion of Linux (https://github.com/raspberrypi). - I've tried including headers to the local
linux/includedirectory, thelinux-headers-$(uname -r)/includedir and thelinux-headers-$(uname -r)-genericdir, separately and together.
Here are some reproducible steps to showcase what I mean:
- Open up my Linux VM running Ubuntu with >100GB in storage
- Run
suto log in asroot - Run
sudo apt-get install vim lld clang - Run
sudo apt-get install vim libncurses-dev flex bison clang-12 lld-12 lldb-12 libssl-dev libelf-dev clang-format clang-tidy clang-tools clangd libc++-dev libc++1 libc++abi-dev libc++abi1 libclang-dev clibclang1 liblldb-dev libllvm-ocaml-dev libomp-dev libomp5 lld lldb llvm-dev llvm-runtime llvm python3-clang - Run
apt remove clang lld, thenln -s /usr/bin/clang-12 /usr/bin/clangandln -s /usr/bin/lld-12 /usr/bin/ld.lld(I got some errors about clang-10 being out of date, so I linked-12) - Download the latest stable kernel source from
https://www.kernel.organd unzip it (for me, it's 6.8.3) - Run
make allnoconfig && make -j16 LLVM=1inside the linux directory (I triedmake menuconfig, but the install took over a day, so I stopped it).
This make's without errors. I did not run make install, as I don't actually want to start the kernel.
If I then try to run clang -I <my-path>/linux/include -I <my-path>/linux/include/uapi -I /usr/src/linux-headers-$(uname -r)/include -I /usr/src/linux-headers-$(uname -r)-generic/include ... < a lot more includes w/ arch/x86, generated/uapi, etc > ... -Xclang -ast-dump fs/fuse/xattr.c, I get hundreds of typing and syntax errors.
I've read tons of SO posts and clang documentation, none of it worked. I saw Clang and Linux kernel, where the OP seems to resolve the issue, but I could not replicate that on my machine (it's a bit dated). I also tried including kconfig.h at the top of the file, no dice. I'm feeling pretty stuck.
Can anyone offer me any pointers (pun intended) on what I'm missing? Don't know what else to try. Also, this is outside my area of expertise - if I missed a step or did something stupid, please let me know.