帖子: 双物理机kgdb串口调试内核和内核模块
设备条件
- 两台物理机,最好系统版本相同或接近。target机运行内核,host机打开gdb。我这里用的ubuntu20.04 + wsl,gdb版本9.1
- 串口线,预先知道串口号,例如 /dev/ttyS0 /dev/ttyS1
编译内核
当前内核配置在 /boot/$(uname -r)-config,ubuntu和debian编译默认开启了kgdb和debug info。重新编译内核是为了获得vmlinux及符号信息。
在ubuntu下有两种方式编译内核,推荐第一种:
一、
mkdir ~/debug-kernel;
cd debug-kernel
apt source linux
cd linux-5.4.0
#建议check一下 make menucofigure <frame-point debug_info KGDB>
sudo apt build-dep linux
dpkg-buildpackage -b
等待很久后,在~/debug-kernel中有linux-header、linux-image-deb、linux-image-*-dbg-deb、linux-libc四个包
然后安装:
sudo dpkg -i linux-image*-deb linux-image-*-dbg-deb linux-header*
二、
sudo apt search source | grep linux-source- # 查看源
sudo apt install linux-source-* # 选择一个跟原来版本最接近的就行
cd /usr/src/linux-source-* ;
su
make menuconfigure #修改配置参数 可以直接 拷贝/boot的配置文件到源码目录,重命名为.config
make -j12
make modules_install
make install
update-grub
最后,直接把source-code目录(例如方法一:~/debug-kernel)拷贝到host主机上,一定保证路径一致!!!
taget编译显卡kernel module
cmake或者make时添加debug参数,build完成后一定得拷贝到host主机且保持目录一致(可以直接放在linux目录下编)
调试内核
1.更改grub启动参数
最好通过default/grub改,直接改/boot路径可能会被覆盖
vim /etc/default/grub
替换一行:
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash nokaslr console=tty0 console=ttyS0,115200n kgdbcon kgdboc=ttyS0,115200"
如果需要断在load kernel阶段则(kgdbwait让kgdb阻塞并等待host连接):
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash nokaslr console=tty0 console=ttyS0,115200n kgdbwait kgdbcon kgdboc=ttyS0,115200 "
2.target主机开机,等待gdb连接
3.host主机启动gdb:
cd /your/path/debug-kernel/linux-5.4.0
gdb -ex "file vmlinux" -ex "target remote /dev/ttyUSB0" -ex "lx-symbols /your/driver/path/arise_kernel" -ex "b wanna_debug_founction" -ex "continue" #修改为合适参数
然后调试driver(假设driver module没有加载):
sudo modprobe drm
sudo modprobe drm_kms_helper
sudo insmod /your/driver/path/arise_pro.ko
sudo systemctl start gdm3
想要随时停住target,在终端输入:
echo g > /proc/sysrq-trigger
此时host主机gdb可以输入gdb断点、目录、符号等命令
tips: 如果报错没有lx-symbols,修复链接ln -sf scripts/gdb/vmlinux-gdb.py cd /your/path/debug-kernel/linux-5.4.0
(可选)host机vscode
cd /your/path/debug-kernel/linux-5.4.0
mkdir .vscode
cd .vscode
vim launch.json
```Json
然后粘贴(注意替换路径):
```Json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) linux",
"type": "cppdbg",
"request": "launch",
"program": "/your/path/debug-kernel/linux-5.4.0/vmlinux",
"miDebuggerServerAddress": "/dev/ttyUSB0",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerArgs": "-n",
"targetArchitecture": "x64",
"sourceFileMap":{"/your/path/debug-kernel/linux-5.4.0": "${workspaceFolder}"},
"setupCommands": [
{"text": "set arch i386:x86-64:intel", "ignoreFailures": false},
{"text": "dir .","ignoreFailures": false},
{"text": "add-auto-load-safe-path ./","ignoreFailures": false},
{"text": "-enable-pretty-printing","ignoreFailures": true},
{"text": "set breakpoint pending on"}
]
}
]
}
(可选)wsl环境配置
win工具包:usbip usb-win kernel(如果wsl内核缺少pl2303模块就编一个)
串口包:pl2303 agent-proxy
window共享usb串口到wsl usbipd attach --wsl Ubuntu-20.04 --busid 1-5
wsl分离串口 ~/Download/agent-proxy-1.97$ ./agent-proxy 5550^5551 0 /dev/ttyUSB0,115200
tips:报错deal with Ignoring packet error, continuing…
sudo stty -F /dev/ttyUSB0 115200
快速拷贝ext4硬盘数据到wsl
GET-CimInstance -query "SELECT * from Win32_DiskDrive"
wsl --mount <DiskPath> --bare
lsblk #Get PartitionNumber
wsl --mount <DiskPath> --partition <PartitionNumber> --type <Filesystem>