Sparse is a tool for static code analysis that helps kernel developers to detect coding errors. Kernel code that is prone to mistakes is annotated by kernel developers using the 'attribute' specifiers. Sparse tool uses these specifiers to pinpoint coding mistakes.
How to install sparse
The best way to install sparse is using the package manager of your linux distribution. That way, in case you want later to uninstall it, you won't need to cleanup the executable files manually.
For instance, if you have Ubuntu, to install sparse do:
$ sudo apt-get install sparse
And, to remove it, do:
$ sudo apt-get remove sparse
If your distro does not provide a precompiled package for sparse, either you can create one or proceed with a manual installation.
For example, if you use Arch linux, you can download the sparse source code from AUR.
In the above link, you will find a link from where you can download the most recent version of sparse source code.
http://www.kernel.org/pub/software/devel/sparse/dist/sparse-latest.tar.gz or any other sparse-version tarball.
To decompress the sparse-0.4.4.tar.gz file, do:
$ tar -xzvf sparse-0.4.4.tar.gz
Then, enter sparse-0.4.4 directory:
$ cd sparse-0.4.4/
Now, you need to create a file, called PKGBUILD, and copy the contents of the PKGBUILD file found in AUR.
After creating PKGBUILD, you can build the package doing:
$ makepkg -s
To install it, do:
$ sudo pacman -U <package_name>.pkg.tar.gz
If you want later to uninstall it, do:
$ sudo pacman -R sparse
Manual installation from sparse git repository
You can download sparse from git://git.kernel.org/pub/scm/devel/sparse/sparse.git doing:
$ git clone git://git.kernel.org/pub/scm/devel/sparse/sparse.git
This will create in your current directory a subdirectory, called 'sparse', which contains the sparse source code.
Enter sparse directory:
$ cd sparse
To see the available releases, do:
$ git tag
Choose the last stable release (v0.4.4) and update the files of the current sparse git tree to match this release:
$ git checkout -b stable v0.4.4
The above command creates a new branch, named 'stable', which refers to the tagged commit 'v0.4.4' and then updates the tree to refer to this branch.
To build sparse, do:
To install it, set in the Makefile the desirable destination directory for the installation by changing the line PREFIX=$(HOME). By default, it will be installed in your 'bin' directory. For instance, you can change it into PREFIX=$(HOME)/sparse or PREFIX=/usr and then, to install it do:
$ make install
If you have chosen to install sparse in a directory out of your executable search path, you need to setup the environmental variable PATH to include the path to the sparse executable.
For instance, if you had set PREFIX=$(HOME)/sparse, do:
$ export PATH=$PATH:$HOME/sparse/bin
To setup permanently the path, do:
$ echo 'export PATH=$PATH:$HOME/sparse/bin' >> ~/.bashrc
To see if sparse can be successfully located, do:
$ which sparse
How to use sparse
Choose a subdirectory in the kernel tree that you want to check for sparse warnings and errors, for instance that could be drivers/staging/wlan-ng, and do:
$ make C=2 drivers/staging/wlan-ng/
You can use the variable CF to pass more checkflags to sparse. For example, you can enable endian checks doing:
$ make C=2 CF="-D__CHECK_ENDIAN__" drivers/staging/wlan-ng/
The warnings produced indicate sites in code where types relevant to byteorder are mixed, possibly leading to buggy behavior.
You can observe what are the default checkflags set in the Makefile. You will see something close to the following:
CHECKFLAGS := -D__linux__ -Dlinux -D__STDC__ -Dunix -D__unix__ \ -Wbitwise -Wno-return-void $(CF)
More documentation on sparse can be found in your kernel source under Documentation/dev-tools/sparse.rst, in the sparse source tree Documentation/ sub-directory, and the links below: