NVIDIA Tegra
NVIDIA Tegra Linux Driver Package

Development Guide
32.2 Release


 
Debugging the Kernel on Tegra Devices
 
Reconfiguring and Rebuilding the Kernel
Modifiying the Configuration Files
Using KGDB
At Boot: Kernel Command Line
This topic describes how to use the Linux kernel source-level debugger (KGDB) for debugging the Linux Kernel on Tegra devices.
Detailed information about KGDB is available at:
https://www.kernel.org/doc/htmldocs/kgdb/index.html
Using KGDB requires a host system with a serial connection to the target device to be debugged. This allows live source-level debugging of the kernel operation to set breakpoints and trace errors back through the source calls.
KGDB cannot be used to trace system calls and other types of processes. A tracer must be used for that type of debugging.
Reconfiguring and Rebuilding the Kernel
To use KGDB, you must reconfigure and rebuild the kernel for KGDB. This requires connecting it to GDB, and using a serial port terminal for debugging.
Note:
To debug devices outfitted with the 64-bit version of NVIDIA® Tegra®  processor, you need GDB 7.0 to connect to KGDB, due to AARCH64 requirements.The L4T BSP supports GDB 7.7 by default. GDB 7.8 also works; however it is not fully tested.
Before rebuilding your kernel, you must determine if the default kernel configuration file must be modified to enable KGDB configuration.
To determine if the Kernel configuration must be modified:
1. Execute the command:
#zcat /proc/config.gz | egrep 'CONFIG_KGDB|IKCONFIG|CONFIG_WATCHDOG'
If your current kernel has all the requisite KGDB features enabled, the following exact output is returned:
CONFIG_IKCONFIG=y
CONFIG_IKCONFIG_PROC=y
# CONFIG_WATCHDOG is not set
CONFIG_KGDB=y
CONFIG_KGDB_SERIAL_CONSOLE=y
# CONFIG_KGDB_TESTS is not set
# CONFIG_KGDB_KDB is not set
2. If the output identifies that /proc/config.gz does not exist, your current kernel was not compiled with CONFIG_IKCONFIG_PROC set. If so, recompile the kernel with CONFIG_IKCONFIG_PROC set.
The general instructions for reconfiguring and rebuilding the Linux kernel for KGDB are available at:
https://www.kernel.org/doc/htmldocs/kgdb/CompilingAKernel.html
Modifiying the Configuration Files
For the L4T Linux kernel running on Tegra devices, some specific configuration files must modified, depending on the target device.
1. Select the configuration file based on your target device.
For example, for T186 boards running L4T, the configuration file is:
tegra18_defconfig
2. Setup the L4T build environment and execute these commands within the kernel directory:
cd $TEGRA_TOP/kernel/kernel-4.4
export ARCH=arm64
make DEFCONFIG_PATH=arch/arm64/configs tegra18_defconfig
make menuconfig
<<Update config options>>
make savedefconfig
cp -v defconfig arch/arm64/configs/tegra18_defconfig
make mrproper
3. Rebuild the kernel using the new configuration file by executing the command:
tmp kernel
4. Flash the new kernel on the target Tegra device to be debugged.
Using KGDB
To use KGDB, you must attach it to GDB. KGDB must be set up to be attached using one of two methods, depending on the state of the system:
At boot, using the kernel command line
If the system is already running, using a system request trigger to KGDB to wait for a GDB connection
At Boot: Kernel Command Line
To create a custom kernel boot command line for KGDB, the command line must include:
The terminal device used for debugging
The baud rate of the serial connection between the host and the target device
The kgdbwait command
To find the existing command line
To see the command line, execute the command:
cat /proc/cmdline
To modify the command line for KGDB
1. Add the following arguments to the kernel command line:
console=ttyS0,115200n8 kgdboc=ttyS0,115200 kgdbwait
Where:
<terminal> is the terminal device for debugging.
<baud_rate> is the baud rate of the serial connection between the host and the target device to be debugged.
Note:
Verify that the baud rate is the same for both the host and target device. Any type of connection that can work like an RS-232 serial connection can be used, including USB connections that are bridged or can tunnel serial data.
The kgdboc parameter sets the target device to use ttyS0. On the host you need to find which serial port or USB port you are receiving boot messages on. This will be the same port that you will use for your GDB host.
With above arguments, target boot waits for connection from remote gdb after below prints.
[ 2.368148] console [ttyS0] enabled
[ 2.368148] console [ttyS0] enabled
[ 2.375339] bootconsole [uart0] disabled
[ 2.375339] bootconsole [uart0] disabled
[ 2.383940] KGDB: Registered I/O driver kgdboc
[ 2.402324] KGDB: Waiting for connection from remote gdb...
2. If you are using same serial line for both console and debug, at this point, you can disconnect the terminal application. For example, minicom.
3. Connect from GDB in the usual way by executing these commands on host:
$TEGRA_TOP/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin/aarch64-linux-android-gdb $TEGRA_TOP/out/l4t-t186ref-debug-aarch64/nvidia/kernel/vmlinux
or
sudo `which <gdb_name>` $TEGRA_TOP/out/l4t-t186ref-debug-aarch64/nvidia/kernel/vmlinux
set remotebaud 115200
target remote /dev/<tty_device>
Where:
<gdb_name> is the name of the correct GDB executable on your machine.
<tty_device> is the host output device configured for GDB.
target remote /dev/<tty_device>
For example:
(gdb) target remote /dev/ttyUSB2
Note:
For GDB 7.8, use the following instead of the last line shown in the preceding example:
set serial baud 115200
In the above example, the terminal is connected through a USB port at:
/dev/ttyUSB2
To connect through a standard serial port, replace the USB device number with a serial port number such as:
/dev/ttyS0
Generally, it will be the same port as the one you receive boot messages through.
To trigger KGDB to Wait for GDB Connection on a system that is running
1. Open a terminal window.
2. Enable the KGDB console on the configured terminal device at the configured baud rate.
To enable the KGDB console
On the target, execute the command:
sudo su
# echo ttyS0,115200 > /sys/module/kgdboc/parameters/kgdboc
Note:
This sets the target device to use ttyS0. On the host, you must find which serial port or USB port you are receiving boot messages on. This will be the same port that you will use for your GDB host.
If you were already connected to the serial console with minicom or screen then you will see:
[ 60.206779] kgdb: Registered I/O driver kgdboc.
To signal KGDB to break
Execute the command:
# echo -n g > /proc/sysrq-trigger
This will give below print on console.
[ 68.398684] sysrq: SysRq : DEBUG
[ 68.402103] KGDB: Entering KGDB
To attach GDB to KGDB
On the host, execute the command:
sudo `which <gdb_name>` $OUT/obj/KERNEL/vmlinux
set remotebaud 115200
target remote /dev/<tty_device$TEGRA_TOP/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin/aarch64-linux-android-gdb $TEGRA_TOP/out/l4t-t186ref-debug-aarch64/nvidia/kernel/vmlinux
Or
sudo `which <gdb_name>` $TEGRA_TOP/out/l4t-t186ref-debug-aarch64/nvidia/kernel/vmlinux
set remotebaud 115200
target remote /dev/<tty_device>
e.g: (gdb) target remote /dev/ttyUSB2
Where:
<gdb_name> is the name of the correct GDB executable on your machine.
<tty_device> is the host output device configured for GDB.
Note:
Note: For GDB 7.8, use the following instead of the last line shown in the preceding example:
 
set serial baud 115200
 
In the above example, the terminal is connected through a USB port such as:
/dev/ttyUSB2
To connect through a standard serial port, replace the USB device number with a serial port number such as:
/dev/ttyS0
Generally, it will be the same port as the one you receive boot messages through.
To save typing each time you connect GDB to KGDB, define a dbgattach alias for them in a .gdbinit file:
cat >~/.gdbinit << "EOF"
define dbgattach
echo set remotebaud 115200 \n
set remotebaud 115200
echo target remote /dev/<tty_device> \n
target remote /dev/<tty_device>
end
EOF