# 设备目标平台 set architecture aarch64 # 设置串口波特率 set serial baud 115200 # 主机通过串口连接到开发板上的kgdb target remote /dev/ttyUSB0
设置成功后会有如下提示:
1 2 3 4 5 6 7
(gdb) set architecture aarch64 The target architecture is assumed to be aarch64 (gdb) set serial baud 115200 (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));
在内核中启用了地址随机化(KASLR,Kernel Address Space Layout Randomization)时,内核模块的加载地址会被随机化,这使得直接在源代码中设置断点变得困难。获取模块的 .text 段信息有助于了解实际的加载地址,从而在调试时提供正确的断点信息。
也可以在启动参数添加nokaslr kgdboc=ttyUSB0,115200 kgdbwait
接着输入:
1
echo g > /proc/sysrq-trigger
这个指令是触发kgdb运行的,输入该指令后,内核就会停下,等待远端gdb连接。
然后在主机打开新的终端,进入驱动源码所在目录,输入如下指令:
1 2 3 4 5 6 7 8
sudo gdb-multiarch (gdb) set architecture aarch64 The target architecture is assumed to be aarch64 (gdb) set serial baud 115200 #将这个地址信息告知 GDB,以便它可以正确地设置断点 (gdb) add-symbol-file /path/to/usbhid.ko 0xffffffffc014d000 # 主机通过串口连接到测试机上的kgdb (gdb) target remote localhost:5551
(gdb) apropos lx function lx_clk_core_lookup -- Find struct clk_core by name function lx_current -- Return current task function lx_device_find_by_bus_name -- Find struct device by bus and name (both strings) function lx_device_find_by_class_name -- Find struct device by class and name (both strings) function lx_module -- Find module by name and return the module variable function lx_per_cpu -- Return per-cpu variable function lx_rb_first -- Lookup and return a node from an RBTree function lx_rb_last -- Lookup and return a node from an RBTree function lx_rb_next -- Lookup and return a node from an RBTree function lx_rb_prev -- Lookup and return a node from an RBTree function lx_task_by_pid -- Find Linux task by PID and return the task_struct variable function lx_thread_info -- Calculate Linux thread_info from task variable function lx_thread_info_by_pid -- Calculate Linux thread_info from task variable found by pid lx-clk-summary -- Print clk tree summary lx-cmdline -- Report the Linux Commandline used in the current kernel lx-configdump -- Output kernel config to the filename specified as the command lx-cpus -- List CPU status arrays lx-device-list-bus -- Print devices on a bus (or all buses if not specified) lx-device-list-class -- Print devices in a class (or all classes if not specified) lx-device-list-tree -- Print a device and its children recursively lx-dmesg -- Print Linux kernel log buffer lx-fdtdump -- Output Flattened Device Tree header and dump FDT blob to the filename lx-genpd-summary -- Print genpd summary lx-iomem -- Identify the IO memory resource locations defined by the kernel lx-ioports -- Identify the IO port resource locations defined by the kernel lx-list-check -- Verify a list consistency lx-lsmod -- List currently loaded modules lx-mounts -- Report the VFS mounts of the current process namespace lx-ps -- Dump Linux tasks lx-symbols -- (Re-)load symbols of Linux kernel and currently loaded modules lx-timerlist -- Print /proc/timer_list lx-version -- Report the Linux Version of the current kernel
对于(gdb) lx-symbols时出现:
1
Python Exception <class 'gdb.MemoryError'> Cannot access memory at address