qemu四种远程调试方法
字数 885 2025-08-22 12:23:00
QEMU四种远程调试方法详解
调试环境准备
- 操作系统: Ubuntu 16.04
- 工具:
- IDA 6及以上版本
- gdb-multiarch (支持不同架构)
- gdbserver (需匹配目标架构)
- QEMU
方法一:QEMU用户模式 + IDA
1. 挂起程序
sudo chroot ./ ./qemu-arm-static -g 1234 ./bin/httpd
-g参数指定调试端口,程序将被挂起等待调试
2. IDA配置
- 加载目标程序(httpd)
- 配置调试模式:
- 设置IP和端口(本地调试可设置为127.0.0.1)
- 选择远程调试选项
- 按F9运行程序,开始调试
方法二:QEMU用户模式 + GDB (Remote方式)
1. 挂起程序
sudo chroot ./ ./qemu-arm-static -g 1234 ./bin/httpd
2. GDB连接调试
gdb-multiarch -q ./bin/httpd
在GDB中执行:
set architecture arm
target remote 127.0.0.1:1234
c
自动化脚本
创建调试脚本dbgscript:
set architecture arm
target remote 127.0.0.1:1234
c
使用脚本启动:
gdb-multiarch -q ./bin/httpd -x dbgscript
方法三:QEMU用户模式 + GDB (Attach方式)
1. 启动程序
sudo chroot ./ ./qemu-arm-static ./bin/httpd
2. 查找进程PID
方法一:
netstat -pantu | grep 80
方法二:
sudo ss -tunlp
3. GDB附加调试
gdb-multiarch -q ./bin/httpd
在GDB中执行:
attach <PID>
注意事项
- 两种GDB方法加载的libc可能不同
- 查看libc的方法:
- 通过进程映射:
sudo cat /proc/<PID>/maps- 使用GDB的vmmap功能
方法四:QEMU系统模式 + GDB
1. 准备系统镜像
下载必要文件:
wget https://people.debian.org/~aurel32/qemu/armhf/debian_wheezy_armhf_standard.qcow2
wget https://people.debian.org/~aurel32/qemu/armhf/initrd.img-3.2.0-4-vexpress
wget https://people.debian.org/~aurel32/qemu/armhf/vmlinuz-3.2.0-4-vexpress
2. 启动QEMU虚拟机
sudo qemu-system-arm -M vexpress-a9 -kernel vmlinuz-3.2.0-4-vexpress \
-initrd initrd.img-3.2.0-4-vexpress \
-drive if=sd,file=debian_wheezy_armhf_standard.qcow2 \
-append "root=/dev/mmcblk0p2 console=ttyAMA0" \
-net nic -net tap,ifname=tap0,script=no,downscript=no -nographic
3. 上传文件到虚拟机
scp -r [file] root@<虚拟机IP>:/root/
4. 使用gdbserver挂起程序
./gdbserver-7.7.1-armhf-eabi5-v1-sysv 0.0.0.0:1234 ./bin/httpd
5. GDB连接
gdb-multiarch -q ./bin/httpd
在GDB中执行:
set architecture arm
target remote <虚拟机IP>:1234
方法五:系统模式 + IDA (不推荐)
1. 使用gdbserver挂起程序
chroot ./ sh
./gdbserver-7.7.1-armhf-eabi5-v1-sysv 0.0.0.0:1234 ./bin/httpd
2. IDA配置
与用户模式类似,但存在以下问题:
- 终端调试退不出来
- 不能再次进行调试
- 错误提示难以解决
总结对比
| 方法 | 优点 | 缺点 |
|---|---|---|
| QEMU用户模式+IDA | 全局视图清晰 | 功能相对有限 |
| QEMU用户模式+GDB | 指令功能强大 | 需要熟悉GDB命令 |
| QEMU系统模式+GDB | 完整系统环境 | 配置复杂 |
| 系统模式+IDA | 不推荐使用 | 问题较多 |
推荐选择:
- 需要全局分析时使用IDA
- 需要深入调试时使用GDB
- 根据场景需求选择用户模式或系统模式