Debugging the Linux kernel with KGDB

Source: Internet
Author: User
Tags error handling

Brief introduction

This document records the entire process of debugging the Linux kernel with KGDB, which is a summary of the predecessors ' work. The following actions are based on a specific board, but most of them can be applied to other platforms.

To debug the kernel using KGDB, you first need to modify the config configuration file, open the appropriate configuration, configure the kernel boot parameters, and even modify the serial port driver to add poll support before you can remotely debug the kernel via the serial port.

Configuring the Kernel Basic configuration

In the kernel configuration file:. config, you need to open the following options

Config_kgdb Join KGDB Support
Config_kgdb_serial_console Enable KGDB to communicate with host via serial port (open this option, Config_console_poll and CONFIG_MAGIC_SYSRQ are turned on by default)
Config_kgdb_kdb Join KDB Support
Config_debug_kernel Contains driver debug information
Config_debug_info To have the kernel contain basic debugging information
Config_debug_rodata=n Close this to set breakpoints in the read-only region
Optional options
Config_panic_timeout=5config_bootparam_softlockup_panic_value=1config_bootparam_hung_task_panic_value=1config_ S3c2410_watchdog_atboot=0config_frame_pointer--Enables KDB to print more stack information config_kallsyms--adding symbolic information config_kdb_keyboard-- If you are communicating with KDB via the target version of the keyboard, you need to turn this on and the keyboard cannot be a USB interface config_kgdb_tests
Startup parameters

After opening the appropriate options, you need to configure the kernel startup parameters so that the kgdb and kernel can find the correct communication interface. If you are using a serial port, you need to configure the following options:

console=ttysac3,115200 kgdboc=ttysac3,115200

If you need to debug the boot process of the kernel, you need to add kgdbwait after Kgdboc.

On other boards, if you use an Ethernet port to communicate with the KGDB, you need to change Kgdboc to Kgdboe (kgdb
over Ethernet)).

Once the configuration is complete, you can compile it properly and download the kernel to the target board.

Serial Port Driver Modification

If the following error message appears during kernel boot:

kgdb:unregistered I/O driver, debugger disabled.

You need to modify the serial port driver according to this part, if can enter kgdb normally, ignore this section, go directly to the next section to use Kgdb.

The CONFIGURE_KGDBOC function in DRIVERS/TTY/SERIAL/KGDBOC.C is passed through the Tty_find_polling_driver (Cptr,
&tty_line) to find the serial drive specified in the kernel boot parameters. Then through the Kgdboc_get_char () and Kgdboc_put_char () to the host port and normal communication.

You can see that the config_console_poll in the config configuration file is the interface to enable the serial port and Kgdboc. If the tty_find_polling_driver does not find a corresponding serial communication interface, the Kgdb_unregister_io_module in kernel/debug/debug_core.c is called for error handling.

Some of the board's serial drive does not include support for KGDBOC communications, such as Samsung's serial driver needs to be added manually in DRIVERS/TTY/SERIAL/SAMSUNG.C.  
to add an interface to KGDBOC communication, simply add a send function and a receive function, and then add the corresponding function in the drive operation structure body. The specific patches are as follows:

drivers/tty/serial/samsung.c | ++++++++++++++++++++++1 files changed, insertions (+), 0 deletions (-) diff--git A/drivers/tty/serial/samsung.c b/ Drivers/tty/serial/samsung.cindex Ff6a4f8. 5ceb7d7 100755---a/drivers/tty/serial/samsung.c+++ b/drivers/tty/serial/samsung.c@@ -893,7 +893,29 @@ static struct Console s3c24xx_serial_console; #define S3c24xx_serial_console null#endif+ #ifdef config_console_poll+static void S3c24xx_serial_poll_put_char (struct uart_port *port, unsigned char c) +{+ while (!) ( RD_REGL (port, S3c2410_utrstat) & S3c2410_utrstat_txe)) + + + WR_REGL (port, S3c2410_utxh, c); +}++static int S3 C24xx_serial_poll_get_char (struct Uart_port *port) +{+ while (!) ( RD_REGL (port, S3c2410_utrstat) & S3C2410_UTRSTAT_RXDR) +; + + return Rd_regl (port, s3c2410_urxh); +}+ #endif +sta    tic struct Uart_ops s3c24xx_serial_ops = {+ #ifdef config_console_poll+. Poll_get_char = s3c24xx_serial_poll_get_char,+ . Poll_put_char = s3c24xx_serial_poll_put_char,+ #endif. PM =S3c24xx_serial_pm,.tx_empty = S3c24xx_serial_tx_empty,.get_mctrl = S3c24xx_serial_get_mctrl,--1.7.5.4 

Add this patch, recompile the kernel, and then get into kgdb

GDB Remote Debugging

If Kgdbwait is added to the kernel boot parameters, the kernel will stay in the debug trap of kgdb and wait for the host's GDB remote connection after basic initialization is complete.

Because most of the board has only one debug serial port, so you need to minicom the serial communication with the previous exit, and then in the kernel source directory, execute the following command:

$ ARM-LINUX-GNUEABI-GCC vmlinux (GDB) target remote/dev/ttyusb0 (GDB) Set detach-on-fork on (GDB) b Panic () (GDB) C

Of course, you can also agent-proxy to use a serial port, through the virtual out of two TCP ports. At this point, GDB needs to use target
The remote command connects to the KGDB, for example:

(GDB) Target remote localhost:5551

Agent-proxy can be obtained here: Git://git.kernel.org/pub/scm/utils/kernel/kgdb/agent-proxy.git, for specific use, please see the repo under the readme.

When debugging the kernel with GDB, many child threads are created as the kernel initializes. The default gdb will take over all the threads, and if you switch from one thread to another, GDB will immediately pause the original thread. But it's easy to cause kernel to die, so you need to set up gdb.
GDB is generally used for multi-threaded debugging, you need to pay attention to two parameters: Follow-fork-mode and Detach-on-fork.

    • The Detach-on-fork parameter, which indicates whether GDB disconnects (detach) the debug of a process after fork, or is controlled by GDB:

      Set detach-on-fork [On|off]

      • On: Disconnects the specified process from debug Follow-fork-mode.
      • Off:gdb will control the parent and child processes.
    • Follow-fork-mode The specified process is debugged and the other process is placed in a paused (suspended) state. The usage of Follow-fork-mode is:

      Set Follow-fork-mode [Parent|child]

      • Parent:fork continues to debug the parent process after the child process is not affected.
      • Child:fork after the child process is debugged, the parent process is unaffected.
REFERENCE
      • GDB User
        mannual:http://sourceware.org/gdb/current/onlinedocs/gdb/
      • Gdb
        Internal:http://www.sourceware.org/gdb/onlinedocs/gdbint.html
      • KGDB/KDB official
        website:https://kgdb.wiki.kernel.org/
      • Kernel Debug
        Usage:http://www.kernel.org/doc/htmldocs/kgdb.html
      • KDB in ELINUX.ORG:HTTP://ELINUX.ORG/KDB
      • Multi-threads Debug in
        gdb:http://www.ibm.com/developerworks/cn/linux/l-cn-gdbmp/
      • kgdb.info:http://www.kgdb.info/

Debugging the Linux kernel with KGDB

Related Article

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.