linux内核中__acquires用来检查什么、Sparse

来源:互联网 发布:编程循环 编辑:程序博客网 时间:2024/04/26 02:58

linux内核中__acquires用来检查什么

*******************************************************************************************************************************************
#ifdef __CHECKER__        ……        # define __acquire(x)__context__(x,1)        # define __release(x)__context__(x,-1) #else        ……        # define __acquires(x)        # define __releases(x)#endif这是一对用于sparse对代码检测的相互关联的函数定义,第一句表示要增加变量x的计数,增加量为1,第二句则正好相反,这个是用来函数编译的过程中。如果在代码中出现了不平衡的状况,那么在Sparse的检测中就会报警。如果要使用Sparse检测功能就需要安装sparse工具(参考相关安装方法),然后编译内核#make zImage C=1 (C=1,只检测新编译的文件,C=2是查所有文件)Sparse会定义__CHECKER__,如果你没有使用sparse工具,__acquire(lock)则定义为空
*******************************************************************************************************************************************
---baidu zhidao

Sparse

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-0.4.4.tar.gz

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:

$ make

To install it, set in the Makefile the desirable destination directoty for the installation by changing the line PREFIX=$(HOME). 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)

Documentation

More documentation on sparse can be found in your kernel source under Documentation/sparse.txt and the links below:

https://sparse.wiki.kernel.org/index.php/Main_Page

http://en.wikipedia.org/wiki/Sparse

0 0
原创粉丝点击