编辑
2025-01-20
工作知识
0
请注意,本文编写于 137 天前,最后修改于 137 天前,其中某些信息可能已经过时。

目录

一、kgdb功能
二、gdb支持高波特率
三、windows支持usbipd
3.1 查看wsl状态
3.2 下载usbipd.msi
3.3 查看windows的usb列表
3.4 绑定和连接
四、打开kgdb
五、sshfs
六、禁IRQ
七、gdb连接kgdb

根据上次描述的kgdb在arm64的使用,我们可以直接在arm64的机器上运行kgdb了,但是考虑到大家并不是每个人都有arm64的机器,故,这里基于windows的wsl2来实现kgdb连接,方便大家调试。

一、kgdb功能

关于kgdb功能打开,如下:

CONFIG_KGDB CONFIG_KGDB_SERIAL_CONSOLE

二、gdb支持高波特率

默认的gdb不支持1500000波特率,需要合并补丁如下补丁:

https://gitlab.com/gnutools/binutils-gdb/-/commit/78d16865df671f80da8d0a97b18596ef8a3feae3

主要步骤如下:

wget https://launchpad.net/ubuntu/+archive/primary/+sourcefiles/gdb/8.1-0ubuntu3/gdb_8.1-0ubuntu3.dsc https://launchpad.net/ubuntu/+archive/primary/+sourcefiles/gdb/8.1-0ubuntu3/gdb_8.1-0ubuntu3.debian.tar.xz https://launchpad.net/ubuntu/+archive/primary/+sourcefiles/gdb/8.1-0ubuntu3/gdb_8.1.orig.tar.xz dpkg-source -x *.dsc apt-get build-dep . patch -p1 < 0001-78d16865df671f80da8d0a97b18596ef8a3feae3.patch DEB_BUILD_OPTIONS=nocheck dpkg-buildpackage -uc -us

然后安装即可

dpkg -i ../*.deb

三、windows支持usbipd

关于在wsl2中支持usb serial的方式,可以参考如下:

https://learn.microsoft.com/en-us/windows/wsl/connect-usb

主要步骤如下:

3.1 查看wsl状态

我们要保证wsl的版本是2,内核大于5.10.60.1,如下

wsl --status 默认分发:Ubuntu 默认版本:2 适用于 Linux 的 Windows 子系统最后更新于 2023/11/13 适用于 Linux 的 Windows 子系统内核可以使用“wsl --update”手动更新,但由于你的系统设置,无法进行自动更新。 若要接收自动内核更新,请启用 Windows 更新设置:“在更新 Windows 时接收其他 Microsoft 产品的更新”。 有关详细信息,请访问https://aka.ms/wsl2kernel。 内核版本: 5.10.102.1

3.2 下载usbipd.msi

直接到如下网址下载msi文件安装

https://github.com/dorssel/usbipd-win/releases

当前版本是usbipd-win_4.3.0.msi

3.3 查看windows的usb列表

此时我们需要administrator权限的powershell,运行如下:

PS C:\> usbipd list Connected: BUSID VID:PID DEVICE STATE 1-10 1ea7:0064 USB 输入设备 Not shared 1-11 0603:0351 USB 输入设备 Not shared 1-12 1840:13fd USB Attached SCSI (UAS) 大容量存储设备 Not shared 1-21 0bc2:ac31 USB Attached SCSI (UAS) 大容量存储设备 Not shared 1-23 1d5c:2000 FrescoLogic FL2000 USB Display Adapter Not shared 1-24 2207:0006 Android ADB Interface Not shared 3-2 0403:6001 USB Serial Converter Shared Persisted: GUID DEVICE

我们看到了我的串口是在3-2的设备上

3.4 绑定和连接

如下命令直接绑定即可:

PS C:\> usbipd bind --busid 3-2 PS C:\> usbipd attach --wsl --busid 3-2 usbipd: info: Using WSL distribution 'Ubuntu' to attach; the device will be available in all WSL 2 distributions. usbipd: info: Using IP address 172.20.64.1 to reach the host.

此时我们在wsl可以看到日志如下:

[85304.990343] usb 1-1: new full-speed USB device number 3 using vhci_hcd [85305.180488] usb 1-1: SetAddress Request (3) to port 0 [85305.243206] usb 1-1: New USB device found, idVendor=0403, idProduct=6001, bcdDevice= 6.00 [85305.243249] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3 [85305.243293] usb 1-1: Product: FT232R USB UART [85305.259943] usb 1-1: Manufacturer: FTDI [85305.259961] usb 1-1: SerialNumber: A5XK3RJT [85305.300748] ftdi_sio 1-1:1.0: FTDI USB Serial Device converter detected [85305.300921] usb 1-1: Detected FT232RL [85305.302620] usb 1-1: FTDI USB Serial Device converter now attached to ttyUSB0

如下:

# ls /dev/ttyUSB0 /dev/ttyUSB0

然后直接minicom连接串口看是否正常即可:

minicom /dev/ttyUSB0 -s

正常就能操作串口了

Welcome to minicom 2.7.1 OPTIONS: I18n Compiled on Aug 13 2017, 15:25:34. Port /dev/ttyUSB0, 10:33:22 Press CTRL-A Z for help on special keys

如不用了记得主动断开。

usbipd detach --busid <busid>

四、打开kgdb

这时候我们在机器内,命令如下:

echo ttyFIQ0 > /sys/module/kgdboc/parameters/kgdboc echo g > /proc/sysrq-trigger

这时候机器在Debug状态了,所以退出串口,拿gdb去连接它

五、sshfs

在连接之前,我们需要一份内核vmlinux和源码,方便调试,而通常我们的代码在服务器,所以需要sshfs

sshfs root@172.25.130.130:/root/public-workspace/tf/01-3588-x11/squashfs-root/root/kernel/ ~/sshfs

六、禁IRQ

为了gdb能够ss调试,我们需要合一下不规范的补丁

https://patchwork.kernel.org/project/linux-arm-kernel/patch/20170523043058.5463-3-takahiro.akashi@linaro.org/

七、gdb连接kgdb

然后直接使用gdb,如下:

root@9:~/sshfs/# gdb-multiarch vmlinux GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git Copyright (C) 2018 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>. For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from vmlinux...done. (gdb) set serial baud 1500000 (gdb) set architecture aarch64 The target architecture is assumed to be aarch64 (gdb) target remote /dev/ttyUSB0 Remote debugging using /dev/ttyUSB0 arch_kgdb_breakpoint () at ./arch/arm64/include/asm/kgdb.h:21 21 asm ("brk %0" : : "I" (KGDB_COMPILED_DBG_BRK_IMM)); (gdb)

搞定