Monday, July 4, 2011

Emacs configuration for Linux kernel development

Linux kernel programming is a hard task. Mostly due the size of the Linux kernel as a project. Fortunately there are some tools that can help us and make the task easier. One of those tools is the excellent Emacs editor.

In this blog entry I will show you how to make the best of Emacs for Linux kernel development. We will see how to use from Emacs the cscope and tags symbol databases and to set the code indentation according to the Linux kernel coding style.

The first problem I had when starting doing kernel development (and in any medium size project that I was involved BTW) was browsing the code. The first attempt and a method I still use for find complicate expressions is to use the commands grep, frep and the grep git option (git grep). Also you can use the Linux Cross Reference (LXR).

Another approach is to use tools that were created to make it easy to browse source code. Two very used utilities are cscope and ctags. They both create a symbol database known as the index. This index have all global symbols, that in the case of C programs, this will be global and member functions, structures, enums, typedefs and #defines.

etags is a custom flavor of ctags that generates a database that can be used by Emacs.

The tools can be used both to create the index and also to navigate the symbols in the index, for example to find all the places in the code that a symbol is declared or used.

To manually create the index for cscope, first you have to list all the files that have symbols that you want to add to the index, you can do this using the find or whatever command line utility is best for you:

$ find . -name "*.c" -o -name "*.h" > cscope.files

then to build the database:

$ cscope -qkRv

and finally to browse the database:

$ cscope -d

A similar procedure is needed to build the database for etags:

$ etags `find . -name "*.c" -o -name "*.h"`

Create cscope and etags symbol databases for the Linux kernel

Creating the databases for both cscope and etags in the kernel source directory is straightforward since the kernel main Makefile has custom targets for doing this.

So, just go to the directory where the kernel source code is:

$ cd /path/to/linux/source/code

and to create the cscope database execute:

$ make cscope

and to create the etags database

$ make TAGS

Use cscope and etags from Emacs

Once the databases are created, to use it from Emacs just open a source file that is inside the kernel source directory (whose symbols are already in the databases).

To use cscope, you have to add this line to your ~/.emacs config file

(require 'xcscope)

Note: Remember has to special keys that can be used to create key sequences. These keys are the Control and Meta key. When specifying key sequences these key are usually refer as Ctrl and Meta or C and M. These can be confusing for a new Emacs user.

So the key sequences:

Ctrl+c s s
C-c s s

are the same, and so are:

Meta+x
M-x

The Control and Meta keys are usually mapped to the Control and Alt keys, but this is not necessary true and in fact most people remap the Control key to the Caps Lock keyboard key.

Then to navigate trough the symbols just put your cursor over a symbol and use one of these commands:

C-c s s Find symbol.
C-c s d Find global definition.
C-c s g Find global definition (alternate binding).
C-c s G Find global definition without prompting.
C-c s c Find functions calling a function.
C-c s C Find called functions (list functions called from a function).
C-c s t Find text string.
C-c s e Find egrep pattern.
C-c s f Find a file.
C-c s i Find files #including a file.

For a complete list of commands refer to the file: xcscope.el.

To use the etags database, first you have to tell emacs where the symbol database is, for example:

M-x visit-tag-table /path/to/linux/source/code/TAGS

to navigate, go to a function an do:

M-.

to go back

M-*

Configure Emacs indentation according to kernel coding style

The Linux kernel coding style is very strict. This ensures that anyone can modify the source code and it will always look consistent. To configure Emacs so its standard indentation uses the kernel coding style, add this line to your ~/.emacs config file:

(setq c-default-style "linux")

Also I have a few tweaks that maybe you find helpful, I list here a portion of my .emacs configuration relevant to development (since I have other uses for Emacs such as LaTex editing):

; Get rid of the startup message
(setq inhibit-startup-message t)
; Show file full path in title bar
(setq-default frame-title-format
   (list '((buffer-file-name " %f"
             (dired-directory
              dired-directory
              (revert-buffer-function " %b"
              ("%b - Dir:  " default-directory)))))))
; Shows parenthesis 
(show-paren-mode 1)
; Shows column number
(column-number-mode 1)
; Change default colors
(set-background-color "grey14")
(set-foreground-color "white")
(set-cursor-color "white")
; No toolbar
(progn
  (if (fboundp 'tool-bar-mode) (tool-bar-mode -1))
;  (menu-bar-mode -1)
  (scroll-bar-mode -1)
)