使用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  继续执行

猜你喜欢

转载自blog.csdn.net/zjhsucceed_329/article/details/79827960