Session 1 - Getting started with the Linux Kernel


Source Code

  1. Clone the repo with the source code
     git clone git:// 
    • Find and analyze the following files or directories: README, main.c, development-process/, tcp.c, ipv6/.
    • Analyze the output of the following commands:
      • git log
      • git log –pretty=format:“%h - %an, %ar : %s”
      • git log –pretty=format:“%h %s” –graph
      • gitk


The exercises below assume that you are using an already configured cscope and vim editor. For more information about the installation and configuration of cscope, you can check this tutorial.

  1. Go to the directory that has the Linux kernel sources in order to build the cscope database.
    • cd /usr/src/linux/
    • Type cscope -R (also check the -b parameter)
    • Try some searches in the interface. Search for the definitions of:
      • struct semaphore and current symbols.
      • main.c file.
      • the functions called by start_kernel().
      • the function that calls init_IRQ().
    • Hints:
      • Use arrow keys to move around between search types.
      • Use 'tab' to switch between the search types and your results.
      • Type the number at the left of a search result to open Vim right to that location.
      • Use CTRL-D to exit cscope.
  2. Start vim - you can also start it with a C symbol (ex: 'vim -t start_kernel') and it will jump at the symbol's definition in the code.
    • Put the cursor over a C symbol. Type “CTRL-\ s” (Control-backslash, then just 's'), and you should see a menu with all the uses of the symbol in the program.
      • You can hit “CTRL-t” to jump back to your original location before the search.
    • Try the same search, but this time via “CTRL-spacebar s”.
      • CTRL-W w or arrow key, or CTRL-W h/j/k/l for left/up/down/right to move between windows.
      • Use “CTRL-spacebar CTRL-spacebar s” for vertical split.
    • If you want to search for a specific symbol, not the one under the cursor, type “:cscope find symbol foo”
      • Or, more tersely, “:cs f s foo”.
      • To do the horizontal split version, use “:scscope” (or just “:scs”).
  3. The “CTRL-\ s” (“:cs f s X”) search finds all uses of a symbol X. But if you need more specific searches you only have to change the 's' letter with: 'g' for the global definition(s) of a symbol, 'c' for all calls to a function, 'f' to open the filename under the cursor.
    • For example you can search for start_kernel function using 's' vs 'c' letter. Compare the results.
  4. Lets say you hate double pressing “CTRL-spacebar” for a vertical split. Change the mapping so you would only have to press it once for a vertical split.
    • Hint: see /root/.vim/plugin/cscope_maps.vim configuration file.
  5. For more search options use “:help cscope” (in Vim) and/or “man cscope” (from your shell).

Features for larger projects:

  1. You can set $CSCOPE_DB environment variable to point to a Cscope database you create, so you won't always need to launch Vim in the same directory as the database.
  2. When working with larger projects, you may want to exclude some files that increase the amount of code to be parsed and may also contain duplicate definitions. For example, in the Linux sources, you may want to exclude all the code in the docs and scripts directories, plus all of the architecture and assembly code for all chips except for the ones you're interested in (e.g. Intel x86).
    • Type “vim -t main” and observe the number of search results.
    • Use the commands below to generate a cscope.files file that contains only the name of the files that need to be parsed.
          cd / 	
          find  $LNX                                                                \
      	-path "$LNX/arch/*" ! -path "$LNX/arch/i386*" -prune -o               \
      	-path "$LNX/include/asm-*" ! -path "$LNX/include/asm-i386*" -prune -o \
      	-path "$LNX/tmp*" -prune -o                                           \
      	-path "$LNX/Documentation*" -prune -o                                 \
      	-path "$LNX/scripts*" -prune -o                                       \
      	-path "$LNX/drivers*" -prune -o                                       \
              -name "*.[chxsS]" -print >/usr/src/linux-3.2.5-so-so2/cscope.files
    • Generate the cscope database.
      cscope -b -q
    • Type “vim -t main” again. This time cscope is using the newly customized database.

Startup Journey

  • Follow the links from this page while we discover how the kernel is initialized.


sesiuni/kernel/day-1.txt · Last modified: 2013/07/01 00:51 by dbaluta