Re: [LONG] how to learn kernel programming

G. Sumner Hayes (
Fri, 23 Jan 1998 12:12:48 -0500

[Medium-long post]
On Fri, Jan 23, 1998 at 06:19:54PM +1100, Adam Wiggins wrote:
> How exactly do you go about tracing code from to init?
> there is a lot of src there and finding functions when your new to the
> structure could be hard.

Use tags. They're by far the most useful feature of real editors, much
better than autoindenting or syntax highlighting. They let you step
through the flow of a program, entering functions as you need to and
then returning from them when you're done. Here's a quick primer on tag
usage in general, followed by the actual commands and keystrokes for vi
and an Emacs keyboard macro I've found useful. With other editors, the
generic idea should apply (presuming they support tags; ex and jed do,
presumably nedit does though I'm not certain).

Generic overview:

1. cd /usr/src/linux
2. Generate a "tags" file (or a "TAGS" file if you use Emacs/XEmacs)
using "ctags" ("etags" for Emacs).
3. edit init/main.c, leaving /usr/src/linux as your current working
4. Find the "init" function (around line 937 in 2.0.33)
5. Start looking at code. If you see a function you want to enter,
use the follow-tag function to enter it; when you're done and want
to return to where you were, use the pop-tag-stack function.

with vim (a vi clone) and the version of ctags that comes with
it, that means:

1. cd /usr/src/linux
2. find . -name *.[ch] | ctags -a -L -
3. vim init/main.c
4. ":tag init"
5. Use Control-] to enter a function and Control-T to return from a
function you've entered; this nests properly, so you can follow the
control as deeply as you want to.

^] and ^T will also visit and return from struct definitions, variable
declarations, class definitions, typedefs, enums, etc.
Other versions of vi will work the same way, but the ctags options
will vary dramatically -- you want to generate all kinds of useful
tags, and the version of ctags that comes with vim does this best.

Probably something like this with the ctags from elvis:

find . -name *.[ch] | xargs ctags -stv

With emacs, it's something like:

find . -name *.[ch] | xargs etags

but read the man pages for both of the above or for other versions of
ctags (including the version of ctags that comes with Emacs, which is
probably what you're stuck with if you haven't installed vim or elvis on
your own; the that comes with Red Hat has the vim
version of ctags in it and installing it over the ctags from emacs won't
hurt your emacs setup at all) to make sure you're getting all the tags
you want.

I'm not too familiar with the tag navigation in Emacs; Meta-. is sort
of like the Control-] from vi, but the following kbd-macro is closer:

(defalias 'find-this-bloody-tag
(read-kbd-macro "M-. RET"))

The online help in Emacs should be consulted, as well as your local

Emacs probably has a super-snazzy way to do this already, I am not
the aforementioned guru (except maybe for vi).

comp.emacs.* and comp.editors are good places to ask these kinds of
questions; don't crosspost, though. Use the former for emacs questions
and the latter for vi and other editors. I think there's also a
gnu.emacs hierarchy, but it's more for developers and elisp authors

Hope this helps,


c'est ne pas un .sig