Using CScope to Browse Linux Kernel Source Code
CScope is a very powerful code browsing tool. In this blog, I will show you how to use CScope to browse the kernel source code.
Download the Kernel Source Code:
To start, let us first download the kernel source code from www.kernel.org. At the time of writing this blog, the latest kernel version is 4.1.5.
Assuming that you have downloaded the 4.1.5 kernel in .xz compression, use the below command to untar the same:
tar -xvJf linux-4.1.5.tar.xz
Check whether you have cscope installed in your system. You can do that by typing ‘cscope’ in your bash prompt.
If cscope is not install, install the same using the following command:
sudo apt-get install cscope
Note: The above command is for Ubuntu.
Building the CScope Database:
Assuming that you have extracted the Linux source code under /usr/src directory, execute the following commands:
cd /usr/src/<linux source>
find . -name “*.[ch]” > cscope.files
Integrating CScope to GVim:
Download the CScope key map file and save it as .vimrc file in your home directory (you can execute “cd ~” to go to your home directory).
Browsing the code:
Now let us browse the code:
- Go to the kernel source directory and open any file, for example, include/linux/fs.h.
- Now search for register_chrdev_region function
- Assuming that you want to look at the body (implementation) of the above function, place the cursor on the function name and press ‘CTRL + \ + g’. This will take you to the definition of the register_chrdev_region function.
On similar you can browse the code for other information. The below paragraph explains the different short key combinations that can be used to browse the source code.
You can execute “CTRL + \ + key”, where key is as shown below:
” ‘s’ symbol: find all references to the token under cursor
” ‘g’ global: find global definition(s) of the token under cursor
” ‘c’ calls: find all calls to the function name under cursor
” ‘t’ text: find all instances of the text under cursor
” ‘e’ egrep: egrep search for the word under cursor
” ‘f’ file: open the filename under cursor
” ‘i’ includes: find files that include the filename under cursor
” ‘d’ called: find functions that function under cursor calls