Article Title: Linux kernel debugging. 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.
Debugging is an essential part in the software development process. In the process of Linux kernel development, it will inevitably face the issue of how to debug the kernel. However, Linux developers are reluctant to add a debugger to the Linux kernel source code tree to ensure kernel code correctness. They think that the debugger in the kernel will mislead developers and introduce bad corrections [1]. therefore, debugging the Linux kernel has always been a tough issue for Kernel programmers. The difficulty of debugging is a significant feature that distinguishes kernel-level development from user-level development.
Despite the lack of an effective built-in method to debug the kernel, the Linux system has gradually formed some techniques for monitoring kernel code and Error Tracking during kernel development. At the same time, many patches came into being, which attached kernel debugging support to the standard kernel. Although some of these patches are not recognized by the Linux official organization, they are indeed functional and powerful. When debugging kernel problems, it is very useful to use these tools and methods to track kernel execution and view its memory and data structure.
This article will first introduce some kernel code monitoring and Error Tracking Technologies on the Linux kernel. These debugging and tracing methods vary according to the required environment and usage methods, next, we will introduce the source code-level debugging methods for the three Linux kernels.
1. debugging technology for Linux kernel-level software
Printk () is the most common technology used to debug kernel code. Add printk () to a specific position in the kernel code for debugging and calling. You can directly print the information you are interested in to the screen, this allows you to observe the program execution path, variables, pointers, and other information. The Linux kernel debugger (kdb) is a patch for the Linux kernel. It provides a way to check the kernel memory and data structure when the system can run. Oops and KDB have a detailed introduction to the Linux debugging technology in the article. For details, refer. Kprobes provides an interface for forcibly entering any kernel routine and collecting information from the interrupt processor without interference. Kprobes allows you to easily collect debugging information such as processor registers and global data structures without frequently compiling and starting the Linux kernel. For more information about how to use Kprobes to debug the kernel, see Kprobes.
The above describes common technologies and methods for Linux kernel debugging and tracking. Of course, the kernel debugging and tracking methods are not just mentioned above. One common feature of these debugging technologies is that they cannot provide effective kernel debugging methods at the source code level, and some can only be called Error Tracking Technologies, therefore, these methods can only provide limited debugging capabilities. The following describes three practical source code-level kernel debugging methods.
2. Use KGDB to build the Linux kernel debugging environment
Kgdb provides a mechanism to debug the Linux kernel using gdb. KGDB allows you to set breakpoints, check variable values, and run a single-step trace program in the kernel, just like debugging common applications. When using KGDB for debugging, two machines are required. One is used as the Development Machine and the other as the Target Machine. The two machines are connected through the serial port or Ethernet port. The serial connection line is a RS-232 interface cable, the internal two ends of the 2nd feet (TXD) and 3rd feet (RXD) cross connection, 7th feet (ground feet) directly connected. During the debugging process, the tested kernel runs on the target machine, and the gdb debugger runs on the development machine.
Currently, kgdb has released debuggers that support several architectures, including i386, x86_64, 32-bit PPC, and iSCSI. For details about kgdb patch, refer to [4].
2.1 kgdb debugging principles
To install the kgdb debugging environment, you must apply the kgdb patch to the Linux kernel. The functions required for the patch-based gdb remote debugging include command processing, trap processing, and serial communication. The main function of kgdb patch is to add a debugging Stub in the Linux kernel. The debugging Stub is a short piece of code in the Linux kernel. It provides a medium between the development machine running gdb and the tested kernel. Gdb communicates with stub through the gdb serial protocol. The gdb serial protocol is a message-based ASCII code protocol that contains various Debugging commands. When a breakpoint is set, kgdb adds a trap command before the breakpoint instruction. When the breakpoint is executed, the control is transferred to the debug stub. In this case, the task of debugging stub is to use the remote serial communication protocol to send the current environment to gdb, and then receive the command from gdb. The gdb Command tells stub what to do next. When stub receives the command to continue executing, it restores the running environment of the program and returns control of the CPU to the kernel.
2.2 install and set Kgdb
Next, we will take the Linux 2.6.7 kernel as an example to describe how to establish the kgdb debugging environment.
2.2.1 software and hardware preparation
The following software and hardware configurations are taken from the system configurations tested by the author:
The kgdb patch version follows the following naming mode: Linux-A-kgdb-B, where A indicates the Linux kernel version and B indicates the kgdb version. Taking the kgdb patch used in the experiment as an example, the Linux kernel version is the linux-2.6.7 and the patch version is the kgdb-2.2.
After the serial port is physically connected, run the following command to test the serial port connection between the two machines. The stty command can set the serial port parameters:
Run the following command on the development machine:
stty ispeed 115200 ospeed 115200 -F /dev/ttyS0 |
Run the following command on the target machine:
stty ispeed 115200 ospeed 115200 -F /dev/ttyS0 |
Run the following command on the developement machine:
Run the following command on the target machine:
[1] [2] [3] [4] [5] [6] Next page