Linux Kernel Startup Process Tracking
1. Use your Linux environment to build MenuOS
# Download the kernel source code to compile the kernel
Cd ~ /LinuxKernel/
Wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.18.6.tar.xz
Xz-d linux-3.18.6.tar.xz
Tar-xvf linux-3.18.6.tar
Cd linux-3.18.6
Make i386_defconfig
Make # It usually takes a long time to compile. If it is less than 20 minutes, it will be several hours.
# Create a root file system
Cd ~ /LinuxKernel/
Mkdir rootfs
Git clone https://github.com/mengning/menu.git # You can use the attachment menu.zip
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
# Start the MenuOS System
Cd ~ /LinuxKernel/
Qemu-kernel linux-3.18.6/arch/x86/boot/bzImage-initrd rootfs. img
//-Initrd file Use file as initial ram disk.
Ii. reconfigure and compile Linux to bring debugging information
Based on the original configuration, make menuconfig selects the following options to reconfigure Linux to carry debugging information
Kernel hacking-> [*] compile the kernel with debug info
Make Re-compilation (long time)
Iii. Use gdb to track and debug the kernel
1. qemu-kernel linux-3.18.6/arch/x86/boot/bzImage-initrd rootfs. img-s-S # description of-s and-S options:
// #-S freeze CPU at startup (use 'C' to start execution)
// #-S shorthand for-gdb tcp: 1234 if you do not want to use port 1234, you can use-gdb tcp: xxxx to replace the-s option
2. Open another shell window
Gdb
(Gdb) file linux-3.18.6/vmlinux # load the symbol table before targe remote in the gdb Interface
(Gdb) target remote: 1234 # establish a connection between gdb and gdbserver, and press c to run Linux on qemu.
(Gdb) break start_kernel # breakpoint settings can be performed before or after target remote
4. Process diagram:
1. You can see that QEMU is running and "Frozen ". (There are several files that do not affect subsequent operations)
2. open another window, enter the linuxkernel directory, and enter gdb and press Enter.
3, input :( gdb) file linux-3.18.6/vmlinux
(Gdb) target remote: 1234
(Gdb) break start_kernel
4. Enter c and press Enter. You can see that the kernel continues to start and stops at start_kernel:
More gdb commands:
Show and find program source code
(1) list: 10 lines of code are displayed, but why is it not displayed successfully?
(2) list 5, 10: displays the code from line 5 to line 10 of the source file.
(3) list t4.c: 5, 10: the code from the fifth row to the Tenth row in the source file is used when the jump contains the order of multiple source files;
(4) list get_sum: display the code around the get_sum function. // What is the surrounding code?
(5) list t4.c: get_sum: displays the code from row 5 to row 10 in the source file t4.c. It is used when the hop contains the order of multiple source files;
(6) If you run linux commands during debugging, you can enter the shell command. (gdb) shell ls at the gdb prompt.
(7) search forward is used to search for the first matched string from the current row;
Search get_sum forward get_sum
(8) reverse_search is used to find the first matched string from the current row: Example: reverse_search main
Set and manage breakpoints:
(1) set a breakpoint with a row number: (gdb) break 7
(2) set the breakpoint using the function name: (gdb) break get_sum
(3) set a breakpoint using a conditional expression: Method 1: break row number or function name if condition. Example: (gdb) break 7 if I = 99
Method 2: watch conditional expression. The following is an example:
Method 3: awatch is used to set a breakpoint for the expression. When the expression value changes or the value cup of the expression is read, the program is temporarily stopped;
(4) view the currently set breakpoint: info breakpoints
(5) The "disable breakpoint number" command may indicate that a breakpoint is invalid. When the program runs to this breakpoint, it does not stop but continues to run.
(6) use the "enable breakpoint number" command to restore a breakpoint.
You can use the clear or delete command to permanently delete a breakpoint.
(1) clear: Delete All Breakpoints in the program;
(2) clear row number: Delete the breakpoint in this row
(3) clear function name: Delete the breakpoint of the Function
(4) delete breakpoint number: delete the breakpoint of the specified number. If you want to delete multiple breakpoints at a time, each breakpoint number is separated by a space.
Execution of the control program:
(1) continue command: Let the program continue to run until the next breakpoint or running is complete. Format: continue
(2) kill command: Used to end debugging of the current program
(3) next and step commands
Difference: If a function is encountered, next executes the function call as a statement, and then enters next to execute the statement after the function call;
The step will trace the code in the function and execute the code in the function one by one until the code in the function is executed and the statement after the function is called;
(4) nexti and stepi commands: these commands are used to execute a machine command in one step. Note that the command is not executed in one step. Execute one statement in one step using the next and step commands. A statement consists of multiple machine commands.
Note: Some commands of gdb can be abbreviated. For example, the list can be replaced by li, and the continue command can be replaced by cont.
5. Continue to enter the list start_kernel to view the code of the top 10 lines in the center of the start_kernel function.
Then enter list to view the subsequent code (list is entered every time and 10 rows are displayed backward)
We will continue to input the list to trace the code after start_kernel and see the subsequent execution process:
As you can see, after start_kernel, there are a lot of initialization operations, such as initializing peripherals, clocks, registers, and so on. There are still many operations in the future, which are not shown much here,
6. Enter the rest_init function later:
Iv. Summary:
A series of initialization functions are called in start_kernel, and the kernel settings have been completed: set the system-related environment, process scheduler initialization, console initialization, system IRQ initialization, and memory initialization. The rest_init () function is called at the end of the Start_kernel function, the init thread is established in rest_init, And the cpu_idle () function is called at the end.
It can be understood as follows: start_kernel finally clones a new process, that is, the init process. Then the original process executes the cpu_idle () function and becomes the idle process, after a process is scheduled, the init process is scheduled to run.
The core process init () mainly initializes some peripherals, including the initialization of SMP (Symmetric Multi-Processing), and CALLS do_basic_setup () to load and initialize peripherals and their drivers, when the do_basic_setup () function returns init (), init () opens the dev/console device again, redirects the output file to the console, and finally loads and executes the init program through kernel -- execve.
After tracking init, you can modify the file system. For example, you can change the displayed MENUOS to MYOS, modify the quit command, and exit the file system:
Qemu-kernel linux-3.18.6/arch/x86/boot/bzImage-initrd rootfs. img
//-Initrd file Use file as initial ram disk.
Here we will go to the parameter rootfs after-initrd. img is loaded to the memory for running, so the rootfs here. img can change the name at will, because the kernel code does not directly use this name but is passed through initrd. In addition, rootfs. img is packaged by the tootfs folder, while the rootfs folder contains only one init executable file, and the init executable file comes from the menu folder, run the make command in the menu folder and a test executable file is generated. After running test and init in the menu folder, the results are exactly the same, so it is clear that init is copied and renamed by test through (cp test init), so the final modification to the file system can be attributed to modifying main in the menu folder. c. test. c file.
In the init/initramfs. c file: