Method and device for debugging kernel function of operating system
By using qemu virtual machine and gdb debugging tools in operating system kernel development, the problem of low debugging efficiency of operating system kernel is solved, efficient kernel debugging and testing processes are realized, and development efficiency is improved.
Patent Information
- Application Number
- CN202411640419.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-18
- Publication Date
- 2024-12-17
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
The operating system kernel debugging is difficult and the lack of suitable development and debugging tools has affected the development progress and the existing technology is inefficient, requiring frequent recompilation and installation, affecting the testing efficiency.
Using qemu virtual machine and gdb debugging tools based on AMD64 architecture, it realizes efficient debugging of the operating system kernel by creating virtual disks, compiling kernel code, installing necessary software and mounting file systems. The specific steps include creating a virtual disk, formatting and mounting, building a file system, installing software, compiling a kernel, installing memory modules, and using gdb to connect to a virtual machine for debugging during the debugging stage.
It greatly improves the speed and efficiency of operating system kernel debugging, provides flexibility in viewing code, setting breakpoints and viewing variable values in gdb, greatly improving the efficiency and flexibility of testing.
Smart Images

Figure CN119149436A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of operating system debugging, and specifically provides a method and a device for realizing the debugging of operating system kernel function. Background Art
[0002] Software development is carried out through debugging. Without good debugging tools, it is difficult to develop good software.
[0003] For application development, the general development language is relatively easy, and there are relatively good IDE or debugging tools, which can be easily debugged. Even if it is very difficult to develop in C language and C++, there are corresponding debugging tools under Windows and Linux.
[0004] For operating system development, in addition to using very difficult C language and assembly language programming, debugging of operating system functions is also a very troublesome problem. You must have a development and debugging tool that suits you, otherwise it will greatly affect the development progress. Especially when you encounter problems, there is no suitable debugging method, you can only recompile the kernel and install and test it over and over again, and you need to restart the system after each installation, which is very inefficient. Summary of the invention
[0005] The present invention aims at the above-mentioned deficiencies of the prior art and provides a method for realizing the debugging of the kernel function of an operating system with strong practicability.
[0006] A further technical task of the present invention is to provide a device that is reasonably designed, safe and applicable for implementing the function of debugging the kernel of an operating system.
[0007] The technical solution adopted by the present invention to solve its technical problem is: A method for realizing debugging of operating system kernel function includes a preparation stage and a debugging stage. The preparation stage is to make a boot file and compile kernel code; the debugging stage is to start a qemu virtual machine and use gbd for debugging.
[0008] Furthermore, based on the AMD64 architecture qemu implementation, the preparation stage has the following steps: (1) Create a virtual disk rootfs.img; (2) Format rootfs.img; (3) Mount rootfs.img to the rootfs directory; (4) Build the root file system; (5) Set the system login password; (6) Install necessary software; (7) Unmount the rootfs.img mounted in the rootfs directory; (8) Compile the operating system kernel; (9) Mount rootfs.img to the directory; (10) Install the memory module to rootfs.img; (11) umount unmounts the rootfs directory.
[0009] Furthermore, in step (1), the command qemu-img is used to create a virtual disk file rootfs.img; In step (2), use the command mkfs.ext4 to format the virtual disk file; In step (3), use the mount command to mount the virtual disk file to the rootfs directory.
[0010] Further, in step (4), the debootstrap command is used to construct the root file system in the directory rootfs, but the use of the debootstrap command is not limited to that; In step (5), the system login password is set, and the user logs in to the virtual machine started using this virtual file system; In step (6), install the necessary software systemd-resolved bpftrace bpfcc-tools gbd for kernel debugging.
[0011] Furthermore, in step (7), the umount command is used to unmount the virtual disk mounted in the rootfs directory; In step (8), use the make command to compile the operating system kernel source code; In step (9), use the mount command to mount the virtual disk file to the rootfs directory.
[0012] Furthermore, in step (10), the makeinstall command installs the kernel module to the path rootfs; In step (11), use the umount command to unmount the virtual disk mounted in the rootfs directory.
[0013] Furthermore, during the debugging phase, the following steps are performed: (a) Boot the virtual machine using rootfs.img and the compiled kernel; (b) Virtual machines started using gbd connections; (c) Use gdb command to debug the kernel.
[0014] Further, in step (a), use rootfs.img as the boot disk and the compiled kernel file bzImage as the kernel to start the qemu virtual machine; In step (b), use gbd and vmlinux to debug the kernel; In step (c), enter gdb, use the target command to connect to the qemu virtual machine, and use the gdb command to debug the operating system kernel.
[0015] A device for implementing a debugging operating system kernel function, comprising: at least one memory and at least one processor; The at least one memory is used to store a machine-readable program; The at least one processor is used to call the machine-readable program to execute a method for implementing a debugging operating system kernel function.
[0016] Compared with the prior art, the method and device for implementing the debugging of the operating system kernel function of the present invention have the following outstanding beneficial effects: The present invention can efficiently debug the kernel function of the operating system, greatly improve the speed of operating system kernel debugging, view the code in gdb as needed, set breakpoints, view the value of scalars, and set the value of variables, which greatly improves the efficiency and flexibility of the test. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0018] Attached Figure 1 It is a flowchart of the preparation phase in a method for realizing the debugging of operating system kernel function; Attached Figure 2 The present invention is a flowchart of the debugging phase in a method for realizing the debugging of operating system kernel functions. DETAILED DESCRIPTION
[0019] In order to enable those skilled in the art to better understand the solution of the present invention, the present invention is further described in detail below in conjunction with specific implementation methods. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0020] A best embodiment is given below: A method for implementing the debugging of the operating system kernel function in this embodiment includes a preparation stage and a debugging stage. The preparation stage is to prepare a boot file and compile the kernel code; the debugging stage is to start the qemu virtual machine and use gbd for debugging.
[0021] like Figure 1 As shown, this embodiment is implemented based on AMD64 architecture qemu, and in the preparation stage, there are the following steps: (1) Create a virtual disk rootfs.img and use the command qemu-img to create a virtual disk file rootfs.img; (2) Format rootfs.img and use the command mkfs.ext4 to format the virtual disk file; (3) Mount rootfs.img to the rootfs directory, and use the mount command to mount the virtual disk file to the rootfs directory; (4) Build the root file system. Use the debootstrap command to build the root file system in the rootfs directory. This is not limited to using the debootstrap command. (5) Set the system login password. Set the system login password, and the user logs in to the virtual machine started using this virtual file system; (6) Install necessary software, including systemd-resolved bpftracebpfcc-tools gbd, which are required for kernel debugging. (7) Unmount the rootfs.img mounted in the rootfs directory, and use the umount command to unmount the virtual disk mounted in the rootfs directory; (8) Compile the operating system kernel and use the make command to compile the operating system kernel source code; (9) Mount rootfs.img to the directory and use the mount command to mount the virtual disk file to the rootfs directory; (10) Install the memory module to rootfs.img, and use the makeinstall command to install the kernel module to the rootfs path; (11) umount unmounts the rootfs directory. Use the umount command to unmount the virtual disk mounted by the rootfs directory.
[0022] The code is as follows: #Create a virtual disk qemu-img create ${basedir} / rootfs.img 8192m # Format the virtual disk / sbin / mkfs.ext4 ${basedir} / rootfs.img # Mount rootfs.img to the rootfs directory sudo mount -o loop ${basedir} / rootfs.img ${basedir} / rootfs #Build the root file system sudo / sbin / debootstrap unstable ${basedir} / rootfs https: / / mirror.nju.edu.cn / debian / # Set the system login password sudo sh -c 'echo '\''wintermute'\''>${basedir} / rootfs / etc / hostname' sudo sh -c 'chroot ${basedir} / rootfs passwd root' # Install necessary software sudo sh -c 'chroot ${basedir} / rootfs apt update' sudo chroot ${basedir} / rootfs / bin / sh -c 'export TMPDIR= / tmp; aptinstall -y systemd-resolved bpftrace bpfcc-tools gdb' sudo cp ${basedir} / config / dhcp.network ${basedir} / rootfs / etc / systemd / network / sudo sh -c 'chroot ${basedir} / rootfs systemctl enable systemd-networkd.service' # umount unmount the rootfs directory sudo umount ${basedir} / rootfs # Compile kernel source code cd src git clone git: / / git.kernel.org / pub / scm / linux / kernel / git / torvalds / linux.git cd linux make defconfig make -j$(nproc) #mount rootfs.img to the directory sudo mount -o loop ${basedir} / rootfs.img rootfs # Install the memory module to rootfs.img sudo cp ${basedir} / src / kernel / vmlinux rootfs cd ${basedir} / src / kernel sudo make INSTALL_MOD_PATH=rootfs modules_install # umount unmount the rootfs directory sudo umount ${basedir} / rootfs like Figure 2 As shown, during the debugging phase, there are the following steps: (a) Use rootfs.img and the compiled kernel to start the virtual machine. Use rootfs.img as the boot disk and the compiled kernel file bzImage as the kernel to start the qemu virtual machine. (b) Use gbd to connect to the started virtual machine, and use gbd and vmlinux to debug the kernel; (c) Use the gdb command to debug the kernel, enter gdb, use the target command to connect to the qemu virtual machine, and use the gdb command to debug the operating system kernel.
[0023] The code is as follows: # Start the virtual machine qemu-system-x86_64 -m 2018m -kernel ${basedir} / src / kernel / arch / x86_64 / boot / bzImage -drive file=${basedir} / rootfs.img,if=virtio,cache=writeback,discard=ignore,format=raw -nographic -append 'root= / dev / vda init= / lib / systemd / systemd rw console=ttyS0 earlyprintk=vga nokaslr selinux=0 audit=0debug' -enable-kvm -netdev user,id=network0 -device e1000,netdev=network0 -gdb tcp::8889 -device virtio-net,netdev=user.0 -netdev user,id=user.0 #Use gdb to debug the kernel gdb . / vmlinux (gdb) target remote :1234 #Connect to the qemu virtual machine created by '. / boot.sh [kernel]'; 1234 is the port number, which needs to be the same as the port used to create the qemu virtual machine.
[0024] Among them, the operating system is a built-in program that is used to cooperate with various hardware of the computer to interact with the user.
[0025] qemu: A pure software virtualization emulator that can simulate almost any hardware device. The one we are most familiar with is the ability to simulate a virtual machine that can run an operating system independently.
[0026] qemu-system-x86_64: The executable program of qemu on the X86 server.
[0027] kernel: The Linux kernel is the main component of the Linux operating system (OS) and the core interface between the computer hardware and its processes. It is responsible for the communication between the two and managing resources as efficiently as possible. It is called the kernel because it is like a seed in the hard shell of a fruit, controlling all the major functions of the hardware.
[0028] debootstrap: debootstrap is a tool used to build a basic root file system. The generated directory conforms to the Linux File System Standard (FHS), that is, it includes directories such as / boot, / etc, / bin, / usr, etc. However, it is much smaller than the released version of Linux, and of course its functions are not as powerful. Therefore, it can only be said to be a "basic system".
[0029] gbd: GBD is a debugging tool provided by the GNU software system community. Together with GCC, it forms a complete development environment that can debug languages such as C, C++, Go, Java, objective-c, and PHP.
[0030] mount: One of the commonly used Linux commands, used to mount files outside the Linux system.
[0031] umount: One of the commonly used Linux commands, which unmounts the file system currently mounted in the Linux directory; AMD64: x86-64 (also known as x64, which is the English word 64-bit extended, abbreviation of 64-bit extension) is a 64-bit extension of the x86 architecture, backward compatible with the 16-bit and 32-bit x86 architecture. x64 was designed by AMD in 1999. AMD first publicly released a 64-bit set to extend x86, called "AMD64". It was later adopted by Intel, and Intel now calls it "Intel 64".
[0032] C86: Domestic X86 CPU architecture; ARM64: Also known as AARCH64, it is a 64-bit ARM architecture instruction set.
[0033] LoongArch: Loongson announced the launch of the LoongArch instruction set, which has more than 2,500 independent instructions and can also translate MIPS, ARM and x86 instructions.
[0034] Based on the above method, a device for implementing a debugging operating system kernel function in this embodiment includes: at least one memory and at least one processor; The at least one memory is used to store a machine-readable program; The at least one processor is used to call the machine-readable program to execute a method for implementing a debugging operating system kernel function.
[0035] The above-mentioned specific implementations are only specific cases of the present invention. The patent protection scope of the present invention includes but is not limited to the above-mentioned specific implementations. Any technical solutions that conform to the above-mentioned specific implementations of the present invention and any appropriate changes or substitutions made by ordinary technicians in the relevant technical field shall fall within the patent protection scope of the present invention.
[0036] Although embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions and variations may be made to the embodiments without departing from the principles and spirit of the present invention, and that the scope of the present invention is defined by the appended claims and their equivalents.
Claims
1. A method for debugging an operating system kernel function, characterized in that: It includes a preparation phase and a debugging phase. The preparation phase is to make a boot file and compile kernel code; the debugging phase is to start the qemu virtual machine and use gbd for debugging.
2. A method for implementing debugging of operating system kernel function according to claim 1, characterized in that: Based on the AMD64 architecture qemu implementation, the preparation stage has the following steps: (1) Create a virtual disk rootfs.img; (2) Format rootfs.img; (3) Mount rootfs.img to the rootfs directory; (4) Build the root file system; (5) Set the system login password; (6) Install necessary software; (7) Unmount the rootfs.img mounted in the rootfs directory; (8) Compile the operating system kernel; (9) Mount rootfs.img to the directory; (10) Install the memory module to rootfs.img; (11) umount unmounts the rootfs directory.
3. A method for implementing debugging of operating system kernel function according to claim 2, characterized in that: In step (1), use the command qemu-img to create the virtual disk file rootfs.img; In step (2), use the command mkfs.ext4 to format the virtual disk file; In step (3), use the mount command to mount the virtual disk file to the rootfs directory.
4. A method for implementing debugging of operating system kernel function according to claim 3, characterized in that: In step (4), use the debootstrap command to build the root file system in the rootfs directory, but do not limit yourself to using the debootstrap command. In step (5), the system login password is set, and the user logs in to the virtual machine started using this virtual file system; In step (6), install the necessary software systemd-resolved bpftrace bpfcc-toolsgbd for debugging the kernel.
5. A method for implementing debugging of operating system kernel function according to claim 4, characterized in that: In step (7), use the umount command to unmount the virtual disk mounted in the rootfs directory; In step (8), use the make command to compile the operating system kernel source code; In step (9), use the mount command to mount the virtual disk file to the rootfs directory.
6. A method for implementing debugging of operating system kernel function according to claim 5, characterized in that: In step (10), the makeinstall command installs the kernel module to the path rootfs; In step (11), use the umount command to unmount the virtual disk mounted in the rootfs directory.
7. A method for implementing debugging of operating system kernel function according to claim 6, characterized in that: During the debugging phase, the following steps are involved: (a) Boot the virtual machine using rootfs.img and the compiled kernel; (b) Virtual machines started using gbd connections; (c) Use gdb command to debug the kernel.
8. A method for implementing debugging of operating system kernel function according to claim 7, characterized in that: In step (a), use rootfs.img as the boot disk and the compiled kernel file bzImage as the kernel to start the qemu virtual machine; In step (b), use gbd and vmlinux to debug the kernel; In step (c), enter gdb, use the target command to connect to the qemu virtual machine, and use the gdb command to debug the operating system kernel.
9. A device for implementing a debugging operating system kernel function, characterized in that: include: at least one memory and at least one processor; The at least one memory is used to store a machine-readable program; The at least one processor is configured to call the machine-readable program to execute the method according to any one of claims 1 to 8.
Citation Information
Patent Citations
Method for quickly adapting Kylin operating system to mobile terminal equipment
CN116974590A
Method for debugging jailhouse by using gdb based on qemu
CN117648245A
