基于debian9编译调试qemu

基于debian9编译调试qemu

os环境

1
wget https://get.debian.org/images/archive/9.13.0/amd64/iso-dvd/debian-9.13.0-amd64-DVD-1.iso
1
2
3
uname -a

Linux debian 4.9.0-13-amd64 #1 SMP Debian 4.9.228-1 (2020-07-05) x86_64 GNU/Linux
1
2
3
4
5
6
7
8
9
10
root@debian:/home/wujing/code# cat /etc/os-release 
PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
NAME="Debian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
VERSION_CODENAME=stretch
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

内核环境

将内核版本替换成linux主线v4.4.161:

1
2
3
4
5
6
7
8
git clone https://mirrors.tuna.tsinghua.edu.cn/git/linux-stable.git
git checkout -b v4.4.161 v4.4.161
apt build-dep linux
cp ~/Downloads/deb/ubuntu/16.04/linux-image-4.4.160-0404160-generic/boot/config-4.4.160-0404160-generic .config
make menuconfig
# load .config
make bindeb-pkg -j10 2> make_error.log
sudo apt install ../*.deb
1
2
3
4
5
6
7
8
for r in $(git remote); do
matches=$(git ls-remote --tags "$r" 2>/dev/null | grep v4.4.161)
if [ -n "$matches" ]; then
echo "Remote: $r"
echo "$matches"
echo
fi
done

qemu源码下载

1
apt source qemu

可以看到当前目录多了三个文件:

1
2
3
qemu_2.8+dfsg-6+deb9u17.debian.tar.xz
qemu_2.8+dfsg-6+deb9u17.dsc
qemu_2.8+dfsg.orig.tar.xz

进入qemu-2.8+dfsg目录:

1
cd qemu-2.8+dfsg

编译调试

安装编译依赖:

1
apt build-dep qemu

开启32线程并行编译,保留调试符号,避免剥离:

1
time DEB_BUILD_OPTIONS="parallel=32 nostrip" debian/rules binary-arch 2> make_error.log

bugfix

1
2
3
root@ffef21905078:~/code/qemu-2.8+dfsg# git log --oneline
e709e55 include/qemu/bitops.h: fix the preprocessor not recognizing the `sizeof` operator
46d8e51 first commit
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
git show e709e55

commit e709e5589dcef5a731abcb232b7f27388bf9ded2
Author: wujing <realwujing@qq.com>
Date: Sun Jun 8 19:43:06 2025 +0800

include/qemu/bitops.h: fix the preprocessor not recognizing the `sizeof` operator

cat make_error.log
In file included from /home/wujing/code/qemu-2.8+dfsg/include/qemu/hbitmap.h:15:0,
from /home/wujing/code/qemu-2.8+dfsg/include/block/dirty-bitmap.h:5,
from /home/wujing/code/qemu-2.8+dfsg/include/block/block.h:9,
from /home/wujing/code/qemu-2.8+dfsg/include/block/block_int.h:28,
from /home/wujing/code/qemu-2.8+dfsg/block/raw-posix.c:30:
/usr/include/linux/swab.h: In function ‘__swab’:
/home/wujing/code/qemu-2.8+dfsg/include/qemu/bitops.h:20:34: warning: "sizeof" is not defined [-Wundef]
#define BITS_PER_LONG (sizeof (unsigned long) * BITS_PER_BYTE)
^
/home/wujing/code/qemu-2.8+dfsg/include/qemu/bitops.h:20:41: error: missing binary operator before token "("
#define BITS_PER_LONG (sizeof (unsigned long) * BITS_PER_BYTE)
^
make: *** [block/raw-posix.o] Error 1
make: *** 正在等待未完成的任务....

apt policy gcc
gcc:
已安装:4:6.3.0-4
候选: 4:6.3.0-4
版本列表:
*** 4:6.3.0-4 500
500 http://archive.debian.org/debian stretch/main amd64 Packages
100 /var/lib/dpkg/status

- [qemu 安装 recipe for target 'block/file-posix.o' failed error:
missing binary operator before token
"(](https://blog.csdn.net/FJDJFKDJFKDJFKD/article/details/105982709)

Signed-off-by: wujing <realwujing@qq.com>
Signed-off-by: QiLiang Yuan <yuanql9@chinatelecom.cn>

diff --git a/include/qemu/bitops.h b/include/qemu/bitops.h
index 1881284..cb49cbf 100644
--- a/include/qemu/bitops.h
+++ b/include/qemu/bitops.h
@@ -17,7 +17,7 @@
#include "atomic.h"

#define BITS_PER_BYTE CHAR_BIT
-#define BITS_PER_LONG (sizeof (unsigned long) * BITS_PER_BYTE)
+#define BITS_PER_LONG (__SIZEOF_LONG__ * BITS_PER_BYTE)

#define BIT(nr) (1UL << (nr))
#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))

合入此补丁后,重新编译qemu即可编译通过。


基于debian9编译调试qemu
https://realwujing.github.io/linux/kernel/kvm/qemu/基于debian9编译调试qemu/
作者
Wu Jing
发布于
2025年6月9日
许可协议