首页
社区
课程
招聘
[原创]在ubuntu24上编译和调试qemu 0.10.6
发表于: 2025-9-10 12:34 410

[原创]在ubuntu24上编译和调试qemu 0.10.6

2025-9-10 12:34
410

起源

最近对qemu很感兴趣,由于现代版本的qemu已经很复杂了,所以从低版本开始看起,已经分析过0.4.4版本了,本文来介绍如何在ubuntu24上编译qemu0.10.6,这个版本不大不小,很适合研究。

修改configure文件

1
2
3
4
5
6
7
8
if test -z "$target_list" ; then
# these targets are portable
    if [ "$softmmu" = "yes" ] ; then
        target_list="\
i386-softmmu \
x86_64-softmmu \
arm-softmmu \
"

我只保留生成这3个qemu可执行文件

在源码目录执行

1
./configure --disable-vnc-tls --disable-linux-user --disable-kvm --disable-kqemu

修改configure后生成的config-host.mak文件

1
CFLAGS=  -g -fno-strict-aliasing -Wall -Wundef -Wendif-labels -Wwrite-strings -Wmissing-prototypes -Wstrict-prototypes -Wredundant-decls

这一步是为了适合调试去掉了编译器优化-O2选项。

修改qemu项目文件

0.10.6这个版本编译输出的qemu文件在x64平台上有bug,生成的目标码有一部分在内存高端,有一部分代码在内存低端,因此在低端地址执行jmp指令到高端地址时就会产生内存段错误,导致无法正常运行。好消息是我通过修改源码解决了这个问题。
修改exec.c文件头部中的如下代码

1
2
3
// static uint8_t *code_gen_buffer;
 
static uint8_t code_gen_buffer[800 * 1024 * 1024];

修改exec.c的code_gen_alloc函数

1
2
3
4
5
6
7
8
9
10
// code_gen_buffer = mmap(start, code_gen_buffer_size,
//                        PROT_WRITE | PROT_READ | PROT_EXEC,
//                        flags, -1, 0);
// if (code_gen_buffer == MAP_FAILED) {
//     fprintf(stderr, "Could not allocate dynamic translator buffer\n");
//     exit(1);
// }
 
size_t pagesize = sysconf(_SC_PAGESIZE);
mprotect(((size_t)code_gen_buffer & ~(pagesize - 1)), 800 * 1024 * 1024, PROT_WRITE | PROT_READ | PROT_EXEC);

修改的方法都是注释掉源代码,然后加上未注释的代码。修改的目的是让生成的目标码都在内存高端,这样jmp跳转就会正确。

make

make install

接下来就可以在Win上用VS Code远程调试了。首先在VS Code中远程ssh登录上ubuntu,然后打开qemu文件夹,在文件夹根目录下新建.vscode文件夹,新建两个文件tasks.json文件和launch.json。

tasks.json文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "make",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": ["$gcc"]
        }
    ]
}

该文件会在每次调试时先执行make,生成最新的qemu。

launch.json文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program":"/root/qemu-0.10.6/x86_64-softmmu/qemu-system-x86_64",
            "args": ["-hda", "/root/linux-0.11-devel-060625.qcow2"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build", // 引用tasks.json中的build任务
        }
    ]
}

该文件会先调用build生成最新的qemu,然后调试生成的

1
/root/qemu-0.10.6/x86_64-softmmu/qemu-system-x86_64

调试的命令行参数在args中,可自行设置。

载入linux系统

linux文件地址如下:

1
https://github.com/oldlinux-web/oldlinux-files/blob/master/qemu-images/Linux%200.11%20on%20qemu-12.5.i386.zip

下载后解压,执行下述指令执行linux

1
/root/qemu-0.10.6/x86_64-softmmu/qemu-system-x86_64 -hda /root/linux-0.11-devel-060625.qcow2

注意路径修改成实际的路径。


传播安全知识、拓宽行业人脉——看雪讲师团队等你加入!

最后于 2025-9-10 12:36 被zhzhz编辑 ,原因:
收藏
免费 0
支持
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回