gdb调试内核

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 禁止内核地址随机化

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秃秃秃秃哇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值