1、设置内核配置
CONFIG_KGDB=y, CONFIG_DEBUG_INFO = y, CONFIG_FRAME_POINTER=y,CONFIG_STRICT_KERNEL_RWX is not set 屏蔽掉该选型
然后重新给板子烧录内核镜像
2、进入uboot,设置bootargs,添加kgdboc参数
setenv bootargs 'mem=512M console=ttyAMA0,115200n8 noinitrd ubi.mtd=4 root=ubi0:ubifs kgdboc=ttyAMA0,115200n8 kgdbwait rootfstype=ubifs rw init=/linuxrc mtdparts=spi0.0:4m(boot),128k(env),2m(dtb),20m(kernel),-(rootfs)'
3、kgdb相关操作
kgdb页面的相关命令:
输入kgdb,进入kgdb模式
然后断开开发板的串口连接,把串口连接到虚拟机
执行vmlinux
sudo ../../../../../../vs-linux/x86-arm/gcc-linaro-7.5.0-aarch64-linux-gnu/bin/aarch64-linux-gnu-gdb ./vmlinux
set serial baud 115200
target remote /dev/ttyUSB2 // 这里可能是USB0或者1或者2,都试一遍就行了
set detach-on-fork on //要加上这个,不然后面continue时会卡住
出现问题
1、gdb的时候,第二次step就会卡主
解决方法:打补丁
参考文章:https://www.byteisland.com/arm64-%e7%9a%84-linux-%e5%86%85%e6%a0%b8-kgdb-kdb-%e8%b0%83%e8%af%95/
Patch 如下:
Subject: [PATCH] KERNEL: arm64, debug: disable interrupts while a software
step is enabled
This patch enforce interrupts to be masked while single stepping. Without
this patch, we will alway fall into arm64/kernel/entry.S while issue step
or next operate.
---
arch/arm64/kernel/kgdb.c | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/arch/arm64/kernel/kgdb.c b/arch/arm64/kernel/kgdb.c
index bcac81e..e83b960 100644
--- a/arch/arm64/kernel/kgdb.c
+++ b/arch/arm64/kernel/kgdb.c
@@ -23,6 +23,10 @@
#include <linux/kdebug.h>
#include <linux/kgdb.h>
#include <asm/traps.h>
+#include <asm/ptrace.h>
+
+
+static DEFINE_PER_CPU(unsigned int, kgdb_pstate);
struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = {
{ "x0", 8, offsetof(struct pt_regs, regs[0])},
@@ -188,6 +192,9 @@ int kgdb_arch_handle_exception(int exception_vector, int signo,
err = 0;
break;
case 's':
+
+ __this_cpu_write(kgdb_pstate, linux_regs->pstate);
+ linux_regs->pstate |= PSR_I_BIT;
/*
* Update step address value with address passed
* with step packet.
@@ -229,6 +236,17 @@ static int kgdb_compiled_brk_fn(struct pt_regs *regs, unsigned int esr)
static int kgdb_step_brk_fn(struct pt_regs *regs, unsigned int esr)
{
+ unsigned int pstate;
+
+ if (!kgdb_single_step)
+ return DBG_HOOK_ERROR;
+ kernel_disable_single_step();
+
+ pstate = __this_cpu_read(kgdb_pstate);
+ if (pstate & PSR_I_BIT)
+ regs->pstate |= PSR_I_BIT;
+ else
+ regs->pstate &= ~PSR_I_BIT;
kgdb_handle_exception(1, SIGTRAP, 0, regs);
return 0;
}
--
2.7.4
问题的原因在于 kgdb 在断点处没有关闭处理器的中断能力,在单步执行的时候处理器会接收中断信号,比如时钟中断,从而转向中断处理,正如上面第一个表格呈现的那样。所以修复方法就是单步执行的时候关闭 cpu 的中断处理机制,continue 的时候恢复中断处理。
2、执行continue时,会卡主
进入uboot界面,设置bootargs的值,添加earlycon=uart8250,mmio32,0xfeb50000,nokaslr
nokaslr 禁止内核地址随机化