Linux Kernel learning three-track analysis kernel startup process

Source: Internet
Author: User
Tags git clone gdb debugger

Preparatory work for the first stage

1 Building the environment

1.1 Download the kernel source code and compile the kernel

Create a directory and enter the directory:

Download Source:

Unzip, and enter the directory: xz-d linux-3.18.6.tar.xz

TAR-XVF linux-3.18.6

CD linux-3.18.6

Compile related files for the selected x86 schema:

Compile:

1.2 Making the root file system

Create a new folder under the working directory:

mkdir Rootfs

Download the information provided by the teacher: Git clone https://github.com/mengning/menu.git

CD Menu

Gcc-o Init linktable.c menu.c test.c-m32-static-lpthread

Cd.. /rootfs

Cp.. /menu/init./

Find. Cpio-o HNEWC |gzip-9 >. /rootfs.img

Return to working directory to start Menuos

Cd..

Qemu-kernel LINUX-3.18.6/ARCH/X86/BOOT/BZIMAGE-INITRD rootfs.img

Operating effect:

2 Debugging Tools GDB Introduction

The GDB debugger is a program debugging tool under the GNU Development Organization and released by Unix/linux. While there is no graphical interface, its powerful debugging capabilities are comparable to, and even simpler than, Windows Professional tools such as Visual Studio. (Note: GDB is debugging an executable file instead of the source code such as "**.C".) )

Introduction to command commands used

GDB//Enter the directory where the files need to be debugged and enter the GDB debugging environment

L//view files (all loaded files)

b//Set breakpoints, such as B 6, to set breakpoints on line 6th

Info b//view breakpoint Information

R//Run code

P "Variable name"//view the value of the variable, such as: P N, view the value of the variable n

N//Single step operation

S//single-step operation, with the above difference is that when a function call occurs, "s" will enter the function, but "n" does not enter the function

Two Debug traces

in order to visually display kernel dynamics during debugging, the QEMU emulator is used initially to stop the state:

-S means frozen in the start state

-S sets port 1234 for the GDB debugging tool, and can also be used for other

Open a GDB debug window, load the symbol table, set up a connection, set a breakpoint

Where the first breakpoint is at Start_kernel, and the second breakpoint is at Rest_init, continue execution and stop at the first breakpoint:

Enter "C", continue execution, stop at the second breakpoint:

Then execute:

Three people understanding of the Linux boot process

The role of the Start_kernel () function is to complete the initialization of the Linux kernel, almost all modules are initialized by this function, by reading the code and using the GDB tool, the following actions are completed:

Call the Sched_init () function to initialize the scheduler, call Page_alloc_init () to initialize the partner system dispatcher, call Trap_init () and INIT_IRQ () to complete the exception and interrupt initialization; call Softirq_init () The function initializes the soft interrupt, calls the Time_init () function to initialize the system and the time, etc...

Start-up process analysis: After the computer power on, load the BIOS hardware information and self-test, read and execute the first boot device within the master boot record bootloader, according to bootloader settings load kernel, kernel will start to detect hardware and load drivers. Each of these parts requires a large textual description, and here is just a look at the process: the Init_task process (process No. 0) is created statically and is created by the kernel developer, not by the other processes through do_fork. It is initialized from Start_kernel () until the last function in Start_kernel () Rest_init (), starting with Rest_init, Linux begins to produce a process, and in Rest_init functions the kernel passes KERNEL_ Thread () produces the first real process (pid=1), and at this point the task of Init_task is basically completely over, it will be reduced to an idle process, in fact, in the earlier Sched_init () function, through Init_idle (current, The call to the smp_processor_id ()) function has initialized the init_task to an idle process.

By: Fanglongwei

Original works reproduced please indicate the source

"Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

Linux Kernel learning three-track analysis kernel startup process

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.