使用gdb+qemu调试OpenWrt内核

1、下载源代码
git clone https://github.com/openwrt/openwrt.git
2、选择Target
make menuconfig
    Target System (QEMU ARM Virtual Machine)  --->
        Subtarget (QEMU ARM Virtual Machine (cortex-a15))  --->
3、编译
make -j4

4、安装工具qemu
apt-get install qemu qemu-system-arm

5、用initramfs启动openwrt
qemu-system-arm -nographic -M virt -m 64 \
-kernel bin/targets/armvirt/32/openwrt-armvirt-32-zImage-initramfs \
-device virtio-net-pci,netdev=lan -device virtio-net-pci,netdev=wan \
-netdev user,id=lan -netdev user,id=wan,hostfwd=tcp::5555-:22

有关网络的配置请参考:https://wiki.qemu.org/Documentation/Networking

6、在openwrt中修改防火墙配置
config zone
        option name             wan
        list   network          'wan'
        list   network          'wan6'
        option input            ACCEPT
        option output           ACCEPT
        option forward          REJECT
        option masq             1
        option mtu_fix          1


使配置生效

/etc/init.d/firewall reload

6、使用ssh连接openwrt
ssh root@localhost -p 5555

7、传文件到openwrt

scp -P 5555 test_file root@localhost:/tmp/

8、使用gdb远程调试内核

qemu-system-arm -nographic -M virt -m 64 \
-kernel bin/targets/armvirt/32/openwrt-armvirt-32-zImage-initramfs \
-device virtio-net-pci,netdev=lan -device virtio-net-pci,netdev=wan \
-netdev user,id=lan -netdev user,id=wan,hostfwd=tcp::5555-:22 \
-S -s

-S表示qemu虚拟机会冻结CPU,等待远程的gdb进行连接

-s表示在1234端口接受gdb连接

在另一个窗口执行gdb

./staging_dir/toolchain-arm_cortex-a15+neon-vfpv4_gcc-5.5.0_musl_eabi/bin/arm-openwrt-linux-gdb build_dir/target-arm_cortex-a15+neon-vfpv4_musl_eabi/linux-armvirt_32/vmlinux.debug

(gdb) target remote localhost:1234

(gdb) b ip_rcv

Breakpoint 1 at 0xc0408b50: file net/ipv4/ip_input.c, line 421.

(gdb) c
Continuing.

Breakpoint 1, ip_rcv (skb=0xc3a23000, dev=0xc0a05654, pt=0xc0a05654, orig_dev=0xc39a1000) at net/ipv4/ip_input.c:421

421             if (skb->pkt_type == PACKET_OTHERHOST)


target remote localhost:1234    远程连接到qemu

b ip_rcv  在ip_rcv处设置断点

c  继续执行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

奋斗-永无止境

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

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

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

打赏作者

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

抵扣说明:

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

余额充值