Linux kernel 源码导读

Author Avatar
W-quan 8月 22, 2017
  • 在其它设备中阅读本文章

Linux kernel 源码庞大,幸亏其提供了方法生成 ctags 或者 cscope 来帮助我们阅读源码。

Makefile

在源码根目录下执行 make help 后可以看到:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Other generic targets:
all - Build all targets marked with [*]
* vmlinux - Build the bare kernel
* modules - Build all modules
modules_install - Install all modules to INSTALL_MOD_PATH (default: /)
firmware_install- Install all firmware to INSTALL_FW_PATH
(default: $(INSTALL_MOD_PATH)/lib/firmware)
dir/ - Build all files in dir and below
dir/file.[ois] - Build specified target only
dir/file.ll - Build the LLVM assembly file
(requires compiler support for LLVM assembly generation)
dir/file.lst - Build specified mixed source/assembly target only
(requires a recent binutils and recent build (System.map))
dir/file.ko - Build module including final link
modules_prepare - Set up for building external modules
tags/TAGS - Generate tags file for editors
cscope - Generate cscope index
gtags - Generate GNU GLOBAL index

其中cscope 或者 tags就是我们要的。

ctags

生成tags文件

执行 make tags

Vim 与 ctags 的简单使用

Key func
Ctrl + ] 调转到定义位置
gCtrl + o 调转到之前的位置(如果有多个匹配项)
Ctrl + o 返回

cscope

生成cscope文件

执行 make cscope

cscope 简单使用

查找

在源码根目录下执行 bash cscope 或者在 Vim 里执行 :cs find

1
2
3
4
5
6
7
8
9
10
find : Query for a pattern            (Usage: find a|c|d|e|f|g|i|s|t name)
a: Find assignments to this symbol
c: Find functions calling this function
d: Find functions called by this function
e: Find this egrep pattern
f: Find this file
g: Find this definition
i: Find files #including this file
s: Find this C symbol
t: Find this text string