一次chroot程序调试过程记录

chroot debug

Posted by cslqm on July 10, 2019

qemu的qcow2镜像挂载后,进入挂载目录,chroot成”/”。结果出现了Bus Error(core dump).

第一回合

尝试调试coredump文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
gdb coredump
...
(gdb) bt
#0  0x00007f3494eafaf4 in ?? ()
#1  0x00000000000010a4 in ?? ()
#2  0x0000000000000001 in ?? ()
#3  0x00007fffb0ad8d90 in ?? ()
#4  0x0000000000000163 in ?? ()
#5  0x00000000000010a4 in ?? ()
#6  0x00007fffb0ad8ca8 in ?? ()
#7  0x0000000000000002 in ?? ()
#8  0x00007f3494eb01f6 in ?? ()
#9  0x74752e53555f6e65 in ?? ()
#10 0x00007fffb0003866 in ?? ()
#11 0x00007fffb0ad8f10 in ?? ()
#12 0x00007f3494eb02e3 in ?? ()
#13 0x00007f348e95a038 in ?? ()
#14 0x0000000c9567d5c4 in ?? ()
...

可以看到无有效信息。

第二回合

安装coreutils-debuginfo软件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
yum install coreutils-debuginfo

rpm -ql coreutils-debuginfo | grep chroot.deb
/usr/lib/debug/usr/sbin/chroot.debug

#已经在挂载目录下
gdb /usr/lib/debug/usr/sbin/chroot.debug
...
(gdb) set args "."
(gdb) r
...
(gdb) bt
#0  0x00007f3494eafaf4 in ?? ()
#1  0x00000000000010a4 in ?? ()
...

无可用信息。

换其他方法

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
strace /usr/lib/debug/usr/sbin/chroot.debug .

execve("/usr/lib/debug/usr/sbin/chroot.debug", ["/usr/lib/debug/usr/sbin/chroot.d"..., "."], [/* 26 vars */]) = -1 ENOENT (No such file or directory)
dup(2)                                  = 3
fcntl(3, F_GETFL)                       = 0x8002 (flags O_RDWR|O_LARGEFILE)
fstat(3, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f09f5eb4000
lseek(3, 0, SEEK_CUR)                   = -1 ESPIPE (Illegal seek)
write(3, "strace: exec: No such file or di"..., 40strace: exec: No such file or directory
) = 40
close(3)                                = 0
munmap(0x7f09f5eb4000, 4096)            = 0
exit_group(1)                           = ?


strace /usr/sbin/chroot .
...
openat(AT_FDCWD, "/dev", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 3
getdents(3, /* 2 entries */, 32768)     = 48
stat("/dev/..", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
stat("/dev/.", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
getdents(3, /* 0 entries */, 32768)     = 0
close(3)                                = 0
open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=106070960, ...}) = 0
mmap(NULL, 106070960, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fa6341a9000
close(3)                                = 0
--- SIGBUS (Bus error) @ 0 (0) ---
+++ killed by SIGBUS (core dumped) +++
Bus error (core dumped)

第三回合

自己编译coreutils包。

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
wget https://ftp.gnu.org/gnu/coreutils/coreutils-8.4.tar.gz
gunzip coreutils-8.4.tar.gz
tar -xf coreutils-8.4.tar
cd coreutils-8.4
./configure
make -j 20

ls src/chroot
src/chroot

gdb -tui  ../package/coreutils-8.4/src/chroot 
(gdb) set args "."
(gdb) b main
...
...

process 31005 is executing new program: ./usr/bin/bash
Missing separate debuginfo for ./usr/bin/bash
Try: yum --enablerepo='*-debug*' install /usr/lib/debug/.build-id/ab/347e897f002d8e3836479e2430d75305fe6a94.debug

Breakpoint 1, 0x000000000041bca0 in main ()
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.80.el6.x86_64
Single stepping until exit from function main,
which has no line number information.

Program received signal SIGBUS, Bus error.
0x00007ffff7616af4 in ?? ()

可以看到是执行镜像中bash出错了。

无力解决。