计算机 · 2021年8月13日 0

How to Read Linux Source Code

阅读Linux内核源码的正确姿势:

浏览内核代码用的工具

4种浏览内核代码用的工具:

  • cscope
  • kscope
  • LXR Cross-Reference
  • SourceWeb

第三种工具LXR有一个衍生出来的工具elixirelixir的一个在线浏览内核代码的网站,用来查找函数和变量的定义和使用还不错。
cscope是以前贝尔实验室发明的一款用于浏览c代码(c++,java也可以)的工具,可以实现以下搜索功能:

  • 某个符号的所有引用
  • 全局定义
  • 字符串
  • 函数调用
  • 正则表达式
  • 文件
  • 包含一个文件的所有文件

那么如何用cscope浏览内核代码呢?
首先在Linux代码目录下,执行make ARCH=X86 cscope或者make ARCH=x86 COMPILED_SOURCE=1 cscope可以生成cscope所需的数据库,这两个命令的区别在于后者只包含了以前的编译过程用到了的符号。然后还需要配置vim,使得可以在vim中使用cscope命令,在.vimrc文件中加入以下内容

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" CSCOPE settings for vim           
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" This file contains some boilerplate settings for vim's cscope interface,
" plus some keyboard mappings that I've found useful.
"
" USAGE: 
" -- vim 6:     Stick this file in your ~/.vim/plugin directory (or in a
"               'plugin' directory in some other directory that is in your
"               'runtimepath'.
"
" -- vim 5:     Stick this file somewhere and 'source cscope.vim' it from
"               your ~/.vimrc file (or cut and paste it into your .vimrc).
"
" NOTE: 
" These key maps use multiple keystrokes (2 or 3 keys).  If you find that vim
" keeps timing you out before you can complete them, try changing your timeout
" settings, as explained below.
"
" Happy cscoping,
"
" Jason Duell       jduell@alumni.princeton.edu     2002/3/7
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""


" This tests to see if vim was configured with the '--enable-cscope' option
" when it was compiled.  If it wasn't, time to recompile vim... 
if has("cscope")

    """"""""""""" Standard cscope/vim boilerplate

    " use both cscope and ctag for 'ctrl-]', ':ta', and 'vim -t'
    set cscopetag

    " check cscope for definition of a symbol before checking ctags: set to 1
    " if you want the reverse search order.
    set csto=0

    " add any cscope database in current directory
    if filereadable("cscope.out")
        cs add cscope.out  
    " else add the database pointed to by environment variable 
    elseif $CSCOPE_DB != ""
        cs add $CSCOPE_DB
    endif

    " show msg when any other cscope db added
    set cscopeverbose  


    """"""""""""" My cscope/vim key mappings
    "
    " The following maps all invoke one of the following cscope search types:
    "
    "   '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
    "
    " Below are three sets of the maps: one set that just jumps to your
    " search result, one that splits the existing vim window horizontally and
    " diplays your search result in the new window, and one that does the same
    " thing, but does a vertical split instead (vim 6 only).
    "
    " I've used CTRL-\ and CTRL-@ as the starting keys for these maps, as it's
    " unlikely that you need their default mappings (CTRL-\'s default use is
    " as part of CTRL-\ CTRL-N typemap, which basically just does the same
    " thing as hitting 'escape': CTRL-@ doesn't seem to have any default use).
    " If you don't like using 'CTRL-@' or CTRL-\, , you can change some or all
    " of these maps to use other keys.  One likely candidate is 'CTRL-_'
    " (which also maps to CTRL-/, which is easier to type).  By default it is
    " used to switch between Hebrew and English keyboard mode.
    "
    " All of the maps involving the <cfile> macro use '^<cfile>$': this is so
    " that searches over '#include <time.h>" return only references to
    " 'time.h', and not 'sys/time.h', etc. (by default cscope will return all
    " files that contain 'time.h' as part of their name).


    " To do the first type of search, hit 'CTRL-\', followed by one of the
    " cscope search types above (s,g,c,t,e,f,i,d).  The result of your cscope
    " search will be displayed in the current window.  You can use CTRL-T to
    " go back to where you were before the search.  
    "

    nmap <C-\>s :cs find s <C-R>=expand("<cword>")<CR><CR>    
    nmap <C-\>g :cs find g <C-R>=expand("<cword>")<CR><CR>    
    nmap <C-\>c :cs find c <C-R>=expand("<cword>")<CR><CR>    
    nmap <C-\>t :cs find t <C-R>=expand("<cword>")<CR><CR>    
    nmap <C-\>e :cs find e <C-R>=expand("<cword>")<CR><CR>    
    nmap <C-\>f :cs find f <C-R>=expand("<cfile>")<CR><CR>    
    nmap <C-\>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
    nmap <C-\>d :cs find d <C-R>=expand("<cword>")<CR><CR>    


    " Using 'CTRL-spacebar' (intepreted as CTRL-@ by vim) then a search type
    " makes the vim window split horizontally, with search result displayed in
    " the new window.
    "
    " (Note: earlier versions of vim may not have the :scs command, but it
    " can be simulated roughly via:
    "    nmap <C-@>s <C-W><C-S> :cs find s <C-R>=expand("<cword>")<CR><CR>    

    nmap <C-@>s :scs find s <C-R>=expand("<cword>")<CR><CR>    
    nmap <C-@>g :scs find g <C-R>=expand("<cword>")<CR><CR>    
    nmap <C-@>c :scs find c <C-R>=expand("<cword>")<CR><CR>    
    nmap <C-@>t :scs find t <C-R>=expand("<cword>")<CR><CR>    
    nmap <C-@>e :scs find e <C-R>=expand("<cword>")<CR><CR>    
    nmap <C-@>f :scs find f <C-R>=expand("<cfile>")<CR><CR>    
    nmap <C-@>i :scs find i ^<C-R>=expand("<cfile>")<CR>$<CR>    
    nmap <C-@>d :scs find d <C-R>=expand("<cword>")<CR><CR>    


    " Hitting CTRL-space *twice* before the search type does a vertical 
    " split instead of a horizontal one (vim 6 and up only)
    "
    " (Note: you may wish to put a 'set splitright' in your .vimrc
    " if you prefer the new window on the right instead of the left

    nmap <C-@><C-@>s :vert scs find s <C-R>=expand("<cword>")<CR><CR>
    nmap <C-@><C-@>g :vert scs find g <C-R>=expand("<cword>")<CR><CR>
    nmap <C-@><C-@>c :vert scs find c <C-R>=expand("<cword>")<CR><CR>
    nmap <C-@><C-@>t :vert scs find t <C-R>=expand("<cword>")<CR><CR>
    nmap <C-@><C-@>e :vert scs find e <C-R>=expand("<cword>")<CR><CR>
    nmap <C-@><C-@>f :vert scs find f <C-R>=expand("<cfile>")<CR><CR>    
    nmap <C-@><C-@>i :vert scs find i ^<C-R>=expand("<cfile>")<CR>$<CR>    
    nmap <C-@><C-@>d :vert scs find d <C-R>=expand("<cword>")<CR><CR>


    """"""""""""" key map timeouts
    "
    " By default Vim will only wait 1 second for each keystroke in a mapping.
    " You may find that too short with the above typemaps.  If so, you should
    " either turn off mapping timeouts via 'notimeout'.
    "
    "set notimeout 
    "
    " Or, you can keep timeouts, by uncommenting the timeoutlen line below,
    " with your own personal favorite value (in milliseconds):
    "
    "set timeoutlen=4000
    "
    " Either way, since mapping timeout settings by default also set the
    " timeouts for multicharacter 'keys codes' (like <F1>), you should also
    " set ttimeout and ttimeoutlen: otherwise, you will experience strange
    " delays as vim waits for a keystroke after you hit ESC (it will be
    " waiting to see if the ESC is actually part of a key code like <F1>).
    "
    "set ttimeout 
    "
    " personally, I find a tenth of a second to work well for key code
    " timeouts. If you experience problems and have a slow terminal or network
    " connection, set it higher.  If you don't set ttimeoutlen, the value for
    " timeoutlent (default: 1000 = 1 second, which is sluggish) is used.
    "
    "set ttimeoutlen=100

endif

最后,在vim中可以使用的cscope相关命令有:

  • Ctrl+\ s
    查找光标下符号的所有引用,也可以用:cs find s name代替,后面的同理
  • Ctrl+\ g
    查找管表下符号的定义
  • Ctrl+\ c
    查找所有光标下函数的调用
  • Ctrl+\ t
    查找所有光标下文字出现的地方
  • Ctrl+\ e
    用egrep搜索光标下的单词
  • Ctrl+\ f
    打开光标下的文件
  • Ctrl+\ i
    找到包含了光标下文件的所有文件
  • Ctrl+\ d
    找到光标下函数的所有调用
  • Ctrl+]
    查找光标下符号定义
  • vim -t tagname
    从bash启动时直接查找tagname的引用
  • :cs help或者:h cscope或者:h copen
    获取cscope相关的帮助

使用cscope阅读其它C/C++代码

也可以用csope来阅读普通C/C++项目的源码,方法就是:

  1. 先在项目目录里用find . -name *.cpp之类的命令把需要引用的文件列表写到一个名为cscope.files里去;
  2. 在目录里运行cscope -b -q,生成相应的数据库,然后按照上面的一样配置一下vim就可以了;
  3. vim配合cscope使用的时候,应该在cscope数据库所在的文件目录里启动vim,不然vim会提示找不到cscope数据库;

看下cscope的官网和manual可知,cscope有一个专门的-k选项来支持内核源码的索引。

使用vim阅读源码的其他插件

目前个人感觉最好用的就是tagbar了,在屏幕右侧将所有的函数、变量都以列表的方式展现出来,看起来特别直观和方便。