DIVISION OF ENGINEERING AND APPLIED SCIENCES
HARVARD UNIVERSITY

CS 161. Operating Systems

Matt Welsh
Spring 2006

Using ctags(1) for Code Navigation

By Philip Zeyliger
Updated 2007 by Stephen Dawson-Haggerty

[CS161 Home Page]

The idea of tags is to navigate around the kernel source by following symbols. This way you don't have to remember which file a given symbol is defined in.

vim Instructions

For example, I might be in uio.c, looking at uiomove(). I could go over to the symbol uio (part of struct uio), type Ctrl-] and be moved over to uio.h, where struct uio is defined. I press Ctrl-t to go back.

Directions:
  1. Go to $HOME/cs161/src/kern and type "ctags -R ." (You want to use exuberant ctags, so you have to do this on ice; the nice machines have a different ctags version.) This should generate a file called 'tags' in the current directory.
  2. Add the following to ~/.vimrc: set tagstack set tags=./tags,tags,$HOME/cs161/src/kern/tags
  3. Use Ctrl-] to follow tags, and Ctrl-t to go back. Try ":help ctags" in vim for more info.
A few more caveats:

emacs Instructions

The useage is similar, except if the cursor is on a tag, you jump to the definition with 'M-.'. The first time you try this it will ask you where the tag file is; if you have followed the instructions below there will be a file named TAGS in ~/cs161/src/kern so you can just select that directory and emacs will use the correct tags table.

In order to jump forward in the tag stack (go back from whence you came), use 'M-*'. If you have typed the first few characters of a symbol, M-tab will complete it from the tabs table, if there is a unique completion. This is only occasionally useful to me.

Directions:
  1. Go to $HOME/cs161/src/kern and type "ctags -eR ." (You want to use exuberant ctags, so you have to do this on ice; the nice machines have a different ctags version.) This should generate a file called 'TAGS' in the current directory.
  2. Use M-. to follow tags, and M-* to go back.

ctags on nice

You may notice that ctags is installed on nice and want to use it. However, it is not so exuberent so you must use it along with find(1). The command below worked for me to generate an emacs tag file. Just use 'ctags' for vim tags, and run it in ~/cs161/src/kern.

find \( -name '*.h' -or -name '*.c' \) -print | xargs etags