使用GDB调试Linux内核

使用GDB调试Linux内核

1.自行静态编译init文件,该文件是内核执行的第一个进程

#include <stdio.h>
int main(const int argc, const char *argv[])
{
    printf("hello, world\n");
    return 0;
}
zhanghao@LAPTOP-7UL4ROG7:~/documents/demo/xiaomi$ gcc -static ./helloworld.c -o init
zhanghao@LAPTOP-7UL4ROG7:~/documents/demo/xiaomi$ file init 
init: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, BuildID[sha1]=3ee48c7c46163b6131ff18c7765baf7468d8f65f, for GNU/Linux 3.2.0, not stripped
echo init | cpio -H newc -o > init.cpio

2.编译内核

 # 使用x86_64默认配置
 make x86_64_defconfig
 # 配置内核调试选项, [Kernel hacking] -> [Compile-time checks and compiler options] -> [Compile the kernel with debug info]&[Provide GDB scripts for kernel debugging]
 make menuconfig
 # 编译内核
 make bzImage -j16
 make scripts_gdb

在这里插入图片描述

在这里插入图片描述

3.使用qemu运行内核

# 窗口1
qemu-system-x86_64 -kernel <path-to-linux>/arch/x86_64/boot/bzImage -initrd <path-to-init>/init.cpio -m 2048 -append "rdinit=/init console=ttyS0 no5lvl nokaslr" \
-nographic -s -S

# 窗口2
# vmlinux中包含了符号信息
gdb <path-to-linux>/vmlinux
# 连入gdb调试端口
target remote :1234
# 再 ksys_write函数处打入断点
b ksys_write
# 继续运行
c

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值