Linux kernel source code reading and related tools

Source: Internet
Author: User
Article title: Linux kernel source code reading and related tools. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
With the gradual popularization of linux, many people are familiar with the installation and setting of Linux. It is compatible with the vigorous development of Linux, and more users want to know more about Linux. To gain a better understanding of Linux, you need to read and analyze the source code of the Linux kernel.
  
Linux kernel source code can be obtained in many ways. Generally, in the installed linux system, the/usr/src/linux directory contains the kernel source code. In addition, you can also download files from the internet. after decompression, files are generally stored in the linux directory. The kernel source code has many versions. The latest stable version is 2.2.14.
  
Many people have a fear of reading the Linux kernel. Of course, there are indeed a lot of difficulties to read such large and complex system code as the Linux kernel, but it is not as difficult as you think. Difficulties can be overcome with perseverance. You don't have to worry about the insufficient level. In fact, there are many things that we will never attend. do we learn while doing?
  
Methods and tools are required for everything. The correct method can guide the work, and good tools can get twice the result with half the effort. The same is true for Linux kernel source code. Next, I will introduce my experience in reading the kernel source code. Finally, I will introduce a reading tool on the Windows platform.
  
If you want to read the source code smoothly, it is best to have a certain understanding of the source code knowledge in advance. For the Linux kernel source code, I think the basic requirements are: 1. basic knowledge of the operating system; 2. familiar with the C language, it is best to have the assembly language knowledge and gnu c's knowledge of standard C extensions. In addition, you should know the overall distribution of the Linux kernel source code before reading. We know that modern operating systems generally consist of process management, memory management, file systems, drivers, and networks. You can see from the Linux kernel source code that each directory roughly corresponds to these aspects. The composition of the linux kernel source code is as follows (assuming it is relative to the linux directory ):
  
The arch subdirectory contains the core code related to the hardware architecture supported by the core source code. For example, the X86 platform is i386.
  
The include directory contains most of the core include files. In addition, each supported architecture has a subdirectory.
  
Init this directory contains the core startup code.
  
Mm this directory contains all the memory management code. The memory management code related to the specific hardware architecture is located in the arch/*/mm Directory. for example, arch/i386/mm/fault. c corresponds to X86.
  
All device drivers in the drivers system are located in this directory. It is further divided into several types of device drivers, each of which also has corresponding sub-directories, such as the driver of the sound card corresponds to drivers/sound.
  
Ipc this directory contains the core inter-process communication code.
  
Modules this directory contains the modules that can be dynamically loaded.
  
File system code supported by fs Linux. Different file systems have different subdirectories. for example, ext2 file systems correspond to ext2 sub-directories.
  
The main core code of kernel. At the same time, the code related to the processor structure is stored in the arch/*/kernel directory.
  
Net Core network code. Each subdirectory corresponds to one aspect of the network.
  
Lib this directory contains the core library code. The library code related to the processor structure is stored in the arch/*/lib/directory.
  
Scripts this directory contains the script file used to configure the core.
  
Documentation this directory is a reference for some documents.
  
After understanding the structure of the source code, you can start to read it. There are vertical and horizontal points for reading methods or sequences. The vertical direction is to follow the execution sequence of the program; the horizontal direction is to implement the module. In fact, they are not absolute, but often combined. For Linux source code, the startup code can follow the linux startup sequence step by step. the general process is as follows (taking the X86 platform as an example ):
  
. /Larch/i386/boot/bootSect. S -->. /larch/i386/boot/setup. S -->. /larch/i386/kernel/head. S -->. /init/main. start_kernel () in c (). For parts such as memory management, you can read and analyze them separately. My experience is: it is best to read the startup code in sequence and then read the topics, such as process and memory management. Each function should be implemented step by step. In fact, this is a repetitive process, and it is impossible to read it once.
  
As the saying goes: "To do good deeds, you must first sharpen your tools ". Reading complex programs like Linux core code is daunting. It is like a snowball that gets bigger and bigger. reading a part of the core often uses several other related documents, and you will soon forget what you did. Therefore, it is impossible to have a good tool. Most of the fans are familiar with the Window platform and are still commonly used in windows, so here I will introduce a tool software for Windows: Source Insight. This is a free 30-day software that can be downloaded from www.sourcedyn.com. Installation is very simple. just like other installation methods, double-click the installation file name and follow the prompts. After the installation is complete, you can start the program. This software is very simple to use and is a good tool for reading the source code. A brief introduction to its usage is as follows: first select new under the Project menu, create a Project, enter the Project name, and then ask you to add the source code to be read (you can add the entire directory, the software analyzes the source code you add. After the analysis, you can read it. If you want to see the definition of a variable in the open reading file, first calibrate the light in the variable, and then click the corresponding option on the toolbar. The definition of the variable is displayed. You can also define and implement functions. Other functions are not mentioned here. if you are interested, you can install a Source Insight, which greatly improves the efficiency of reading the Source code. How about it? try it!
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.