Article title: Linux kernel debugger. 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.
It is useful to track kernel execution and view its memory and data structure when debugging kernel problems. The built-in inner-core debugger KDB in Linux provides this function. In this article, you will learn how to use the features provided by KDB and how to install and set KDB on Linux machines. You will also be familiar with the commands that can be used in KDB and the settings and display options.
The Linux kernel debugger (KDB) allows you to debug the Linux kernel. This tool, like its name, is essentially a patch of kernel code, which allows experts to access the kernel memory and data structure. One of the main advantages of KDB is that it does not need to be debugged on another machine: you can debug the running kernel.
It takes some work to set a machine for KDB, because the kernel needs to be patched and re-compiled. KDB users should be familiar with the compilation of the Linux kernel (to a certain extent, they must be familiar with the internal mechanism of the kernel). However, if you need to compile the kernel, see references at the end of this article.
In this article, we will start with information on downloading KDB patches, patching, (re-compiling) the kernel, and starting KDB. Then we will understand the KDB commands and study some common commands. Finally, we will look at some details about settings and display options.
Getting started
The KDB project is maintained by Silicon Graphics (see references for links). you need to download patches related to the kernel version from its FTP site. (When writing this article) the latest available KDB version is 4.2. You will need to download and apply two patches. One is a "public" patch that includes changes to the general kernel code, and the other is a system-specific patch. The patch can be obtained as an bz2 file. For example, on an x86 machine running the 2.4.20 kernel, you need kdb-v4.2-2.4.20-common-1.bz2 and kdb-v4.2-2.4.20-i386-1.bz2.
All examples provided here are for the i386 architecture and the 2.4.20 kernel. You will need to make appropriate changes based on your machine and kernel version. You also need the root permission to perform these operations.
Copy the file to the/usr/src/linux directory and extract the patch file from the file compressed with bzip2:
# Bzip2-d kdb-v4.2-2.4.20-common-1.bz2
# Bzip2-d kdb-v4.2-2.4.20-i386-1.bz2
You will get kdb-v4.2-2.4.20-common-1 and kdb-v4.2-2.4-i386-1 files.
Apply these patches:
# Patch-p1
# Patch-p1
These patches should be applied cleanly. Search for any file ending with. rej. This extension indicates that these are failed patches. If there is no problem with the kernel tree, the patch application will not have any problems.
Next, you need to build a kernel to support KDB. The first step is to set the CONFIG_KDB option. Use your favorite configuration mechanisms (such as xconfig and menuconfig) to complete this step. Go to the "Kernel hacking" section at the end and select the "Built-in Kernel Debugger support" option.
You can also select the other two options based on your preferences. Select the "Compile the kernel with frame pointers" option (if any), set the CONFIG_FRAME_POINTER flag. This produces better stack backtracking because frame pointer registers are used as frame pointers rather than general registers. You can also select the "KDB off by default" option. This sets the CONFIG_KDB_OFF flag, and KDB is disabled by default. We will introduce this in detail in later sections.
Save the configuration and exit. Recompile the kernel. We recommend that you run "make clean" before building the kernel ". Install and boot the kernel in common ways.
Initialize and set environment variables
You can define the KDB command that will be executed during KDB initialization. You need to define these commands in the plain text file kdb_cmds, which is located in the KDB Directory of the Linux source code tree (after patches are installed, of course. This file can also be used to define environment variables for setting display and print options. Annotations at the beginning of the file provide help in editing the file. The disadvantage of using this file is that you need to re-build and re-install the kernel after you change the file.
Activate KDB
If CONFIG_KDB_OFF is not selected during compilation, KDB is active by default. Otherwise, you need to activate it explicitly-pass the kdb = on flag to the kernel during boot or execute the job after/proc is mounted:
# Echo "1">/proc/sys/kernel/kdb
If you perform the preceding steps, KDB is deactivated. That is to say, if KDB is enabled by default, the kdb = off flag is passed to the kernel or the following operation will cancel KDB activation:
# Echo "0">/proc/sys/kernel/kdb
Another flag can be passed to the kernel during boot. The kdb = early flag will cause control to be passed to KDB at the initial stage of the boot process. This is helpful if you need to debug in the initial phase of the boot process.
? There are many ways to call KDB. If KDB is on, it is automatically called as long as there is an emergency in the kernel. Press the PAUSE key on the keyboard to manually call KDB. Another way to call KDB is through the serial console. To do this, you need to set up the serial console (see references for help) and a program to read data from the serial console. Press Ctrl-A to call KDB from the serial console.
KDB command
KDB is a powerful tool that allows several operations, such as memory and register modification, application breakpoint, and stack tracing. Based on these, KDB commands can be divided into several categories. The following describes the most common commands in each type.
Memory Display and modification
The most common commands in this category are md, MCM, mm, and mmW.
The md command takes an address/symbol and a row count as the parameter to display the line-count row memory starting from this address. If line-count is not specified, the default value specified by the environment variable is used. If no address is specified, the md continues from the last printed address. The address is printed at the beginning, and the character conversion is printed at the end.
The MCM Command carries the address/symbol and the byte count to display the initial memory content of the byte-count bytes starting from the specified address. It is essentially the same as md, but it does not show the starting address and does not show character conversion at the end. The command is rarely used.
Mm command to modify the memory content. It uses the address/symbol and new content as parameters and replaces the content at the address with new-contents.
The mmW command changes the W bytes starting from the address. Note that mm must change the machine word.
Example
Display the 15 rows of memory starting from 0xc000000:
[0] kdb> md 0xc000000 15
Change the content at the memory location 0xc000000 to 0x10:
[0] kdb> mm 0xc000000 0x10
Display and modify registers
Commands in this category include rd, rm, and ef.
The rd command (without any parameters) displays the content of the processor register. It can have three parameters. If the c parameter is passed, rd displays the control register of the processor; if it carries the d parameter, it displays the debugging register; if it carries the u parameter, displays the Register Group of the last task that entered the kernel.
Rm command to modify the Register content. It uses the register name and new-contents as parameters and uses new-contents to modify the register. The register name is related to a specific architecture. Currently, the control register cannot be modified.
The ef command uses an address as a parameter to display abnormal frames at a specified address.
Example
Display general register group:
[0] kdb> rd
Set the content of the register ebx to 0x25:
[0] kdb> rm % ebx 0x25
Breakpoint
Common breakpoint commands include bp, bc, bd, be, and bl.
The bp command uses an address/symbol as a parameter. it applies a breakpoint at the address. When this breakpoint occurs, the execution is stopped and the control is handed over to KDB. This command has several useful variants. The bpa command applies breakpoints to all processors in the SMP system. The ALB command forces it to be used on a system that supports hardware registers. The bpha command is similar to the bpa command, except that it forces the use of hardware registers.
The bd command disables special breakpoints. It receives the breakpoint number as a parameter. This command does not remove a breakpoint from the breakpoint table, but simply disables it. The breakpoint number starts from 0 and is allocated to the breakpoint according to the availability order.
Be command to enable breakpoint. The parameter of this command is also the breakpoint number.
The bl command is used to list the current breakpoint set. It contains enabled and disabled breakpoints.
The bc command removes breakpoints from the breakpoint table. It uses a specific breakpoint number or * as a parameter. in the latter case, it removes all breakpoints.
Example
Set a breakpoint for the function sys_write:
[0] kdb> bp sys_write
List all breakpoints in the breakpoint table:
[0] kdb> bl
Clear breakpoint 1:
[0] kdb> bc 1
Stack tracing
Major stack trace commands include bt, btp, btc, and bta.
The bt command tries to provide information about the stack of the current thread. It can selectively use the stack frame address as a parameter. If the address is not provided, it uses the current register to trace the stack. Otherwise, it assumes that the provided address is a valid starting address of the stack frame and tries to trace back. If the CONFIG_FRAME_POINTER option is set during kernel compilation, the frame pointer register is used to maintain the stack, so that stack tracing can be correctly executed. If CONFIG_FRAME_POINTER is not set, the bt command may produce incorrect results.
The btp command uses the process ID as a parameter and traces back the stack of the specific process.
The btc command executes stack backtracking for processes running on each active CPU. It starts to execute bt from the first active CPU, switches to the next active CPU, and so on.
The bta command traces back all processes in a specific state. If no parameter is included, it will trace back all processes. You can selectively pass parameters to the command. Processes in a specific state are processed based on parameters. Options and the corresponding status are as follows:
D: non-disruptive status
R: Running
S: resumable sleep
T: Tracked or stopped
Z: dead
U: not running
Each of these commands prints a large amount of information. For more information about these fields, see the following references.
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