本文从调试的角度出发,分享QEMU调试过程中的常见调试方法。
1.如何查看makefile构建过程执行的命令?
为了深入理解ucore操作系统实验的编译链接细节,需要知道makefile在执行的过程中一步一步的指令执行情况。然而大部分的工程中,makefile实际执行情况是没有输出日志的,因此如何产生makefile指令执行的内容十分必要。
make命令的--just-print选项可以让makefile文件组织的工程只输出构建过程中要执行的命令,而不实际执行这些命令。
当上述的命令执行完成后,十分详细的输出的结果如下:
touch -c kern/process/proc.c
make --quiet --no-print-directory "DEFS+=-DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size"
echo + cc kern/init/entry.S
gcc -Ikern/init/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/init/entry.S -o obj/kern/init/entry.o
echo + cc kern/init/init.c
gcc -Ikern/init/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/init/init.c -o obj/kern/init/init.o
echo + cc kern/libs/stdio.c
gcc -Ikern/libs/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/libs/stdio.c -o obj/kern/libs/stdio.o
echo + cc kern/libs/readline.c
gcc -Ikern/libs/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/libs/readline.c -o obj/kern/libs/readline.o
echo + cc kern/debug/panic.c
gcc -Ikern/debug/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/debug/panic.c -o obj/kern/debug/panic.o
echo + cc kern/debug/kdebug.c
gcc -Ikern/debug/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/debug/kdebug.c -o obj/kern/debug/kdebug.o
echo + cc kern/debug/kmonitor.c
gcc -Ikern/debug/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/debug/kmonitor.c -o obj/kern/debug/kmonitor.o
echo + cc kern/driver/ide.c
gcc -Ikern/driver/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/driver/ide.c -o obj/kern/driver/ide.o
echo + cc kern/driver/clock.c
gcc -Ikern/driver/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/driver/clock.c -o obj/kern/driver/clock.o
echo + cc kern/driver/console.c
gcc -Ikern/driver/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/driver/console.c -o obj/kern/driver/console.o
echo + cc kern/driver/picirq.c
gcc -Ikern/driver/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/driver/picirq.c -o obj/kern/driver/picirq.o
echo + cc kern/driver/intr.c
gcc -Ikern/driver/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/driver/intr.c -o obj/kern/driver/intr.o
echo + cc kern/trap/trap.c
gcc -Ikern/trap/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/trap/trap.c -o obj/kern/trap/trap.o
echo + cc kern/trap/vectors.S
gcc -Ikern/trap/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/trap/vectors.S -o obj/kern/trap/vectors.o
echo + cc kern/trap/trapentry.S
gcc -Ikern/trap/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/trap/trapentry.S -o obj/kern/trap/trapentry.o
echo + cc kern/mm/pmm.c
gcc -Ikern/mm/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/mm/pmm.c -o obj/kern/mm/pmm.o
echo + cc kern/mm/swap_fifo.c
gcc -Ikern/mm/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/mm/swap_fifo.c -o obj/kern/mm/swap_fifo.o
echo + cc kern/mm/vmm.c
gcc -Ikern/mm/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/mm/vmm.c -o obj/kern/mm/vmm.o
echo + cc kern/mm/kmalloc.c
gcc -Ikern/mm/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/mm/kmalloc.c -o obj/kern/mm/kmalloc.o
echo + cc kern/mm/swap.c
gcc -Ikern/mm/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/mm/swap.c -o obj/kern/mm/swap.o
echo + cc kern/mm/default_pmm.c
gcc -Ikern/mm/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/mm/default_pmm.c -o obj/kern/mm/default_pmm.o
echo + cc kern/fs/swapfs.c
gcc -Ikern/fs/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/fs/swapfs.c -o obj/kern/fs/swapfs.o
echo + cc kern/process/switch.S
gcc -Ikern/process/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/process/switch.S -o obj/kern/process/switch.o
echo + cc kern/process/entry.S
gcc -Ikern/process/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/process/entry.S -o obj/kern/process/entry.o
echo + cc kern/process/proc.c
gcc -Ikern/process/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/process/proc.c -o obj/kern/process/proc.o
echo + cc kern/schedule/sched.c
gcc -Ikern/schedule/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/schedule/sched.c -o obj/kern/schedule/sched.o
echo + cc kern/syscall/syscall.c
gcc -Ikern/syscall/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Ikern/debug/ -Ikern/driver/ -Ikern/trap/ -Ikern/mm/ -Ikern/libs/ -Ikern/sync/ -Ikern/fs/ -Ikern/process -Ikern/schedule -Ikern/syscall -c kern/syscall/syscall.c -o obj/kern/syscall/syscall.o
echo + cc libs/string.c
gcc -Ilibs/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -c libs/string.c -o obj/libs/string.o
echo + cc libs/printfmt.c
gcc -Ilibs/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -c libs/printfmt.c -o obj/libs/printfmt.o
echo + cc libs/hash.c
gcc -Ilibs/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -c libs/hash.c -o obj/libs/hash.o
echo + cc libs/rand.c
gcc -Ilibs/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -c libs/rand.c -o obj/libs/rand.o
echo + cc user/hello.c
gcc -Iuser/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Iuser/include/ -Iuser/libs/ -c user/hello.c -o obj/user/hello.o
echo + cc user/libs/panic.c
gcc -Iuser/libs/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Iuser/include/ -Iuser/libs/ -c user/libs/panic.c -o obj/user/libs/panic.o
echo + cc user/libs/syscall.c
gcc -Iuser/libs/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Iuser/include/ -Iuser/libs/ -c user/libs/syscall.c -o obj/user/libs/syscall.o
echo + cc user/libs/ulib.c
gcc -Iuser/libs/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Iuser/include/ -Iuser/libs/ -c user/libs/ulib.c -o obj/user/libs/ulib.o
echo + cc user/libs/initcode.S
gcc -Iuser/libs/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Iuser/include/ -Iuser/libs/ -c user/libs/initcode.S -o obj/user/libs/initcode.o
echo + cc user/libs/stdio.c
gcc -Iuser/libs/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Iuser/include/ -Iuser/libs/ -c user/libs/stdio.c -o obj/user/libs/stdio.o
echo + cc user/libs/umain.c
gcc -Iuser/libs/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Iuser/include/ -Iuser/libs/ -c user/libs/umain.c -o obj/user/libs/umain.o
ld -m elf_i386 -nostdlib -T tools/user.ld -o obj/__user_hello.out obj/user/libs/panic.o obj/user/libs/syscall.o obj/user/libs/ulib.o obj/user/libs/initcode.o obj/user/libs/stdio.o obj/user/libs/umain.o obj/libs/string.o obj/libs/printfmt.o obj/libs/hash.o obj/libs/rand.o obj/user/hello.o
objdump -S obj/__user_hello.out > obj/user/hello.asm
objdump -t obj/__user_hello.out | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$/d' > obj/user/hello.sym
echo + cc user/badarg.c
gcc -Iuser/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Iuser/include/ -Iuser/libs/ -c user/badarg.c -o obj/user/badarg.o
ld -m elf_i386 -nostdlib -T tools/user.ld -o obj/__user_badarg.out obj/user/libs/panic.o obj/user/libs/syscall.o obj/user/libs/ulib.o obj/user/libs/initcode.o obj/user/libs/stdio.o obj/user/libs/umain.o obj/libs/string.o obj/libs/printfmt.o obj/libs/hash.o obj/libs/rand.o obj/user/badarg.o
objdump -S obj/__user_badarg.out > obj/user/badarg.asm
objdump -t obj/__user_badarg.out | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$/d' > obj/user/badarg.sym
echo + cc user/forktree.c
gcc -Iuser/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Iuser/include/ -Iuser/libs/ -c user/forktree.c -o obj/user/forktree.o
ld -m elf_i386 -nostdlib -T tools/user.ld -o obj/__user_forktree.out obj/user/libs/panic.o obj/user/libs/syscall.o obj/user/libs/ulib.o obj/user/libs/initcode.o obj/user/libs/stdio.o obj/user/libs/umain.o obj/libs/string.o obj/libs/printfmt.o obj/libs/hash.o obj/libs/rand.o obj/user/forktree.o
objdump -S obj/__user_forktree.out > obj/user/forktree.asm
objdump -t obj/__user_forktree.out | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$/d' > obj/user/forktree.sym
echo + cc user/badsegment.c
gcc -Iuser/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Iuser/include/ -Iuser/libs/ -c user/badsegment.c -o obj/user/badsegment.o
ld -m elf_i386 -nostdlib -T tools/user.ld -o obj/__user_badsegment.out obj/user/libs/panic.o obj/user/libs/syscall.o obj/user/libs/ulib.o obj/user/libs/initcode.o obj/user/libs/stdio.o obj/user/libs/umain.o obj/libs/string.o obj/libs/printfmt.o obj/libs/hash.o obj/libs/rand.o obj/user/badsegment.o
objdump -S obj/__user_badsegment.out > obj/user/badsegment.asm
objdump -t obj/__user_badsegment.out | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$/d' > obj/user/badsegment.sym
echo + cc user/faultread.c
gcc -Iuser/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Iuser/include/ -Iuser/libs/ -c user/faultread.c -o obj/user/faultread.o
ld -m elf_i386 -nostdlib -T tools/user.ld -o obj/__user_faultread.out obj/user/libs/panic.o obj/user/libs/syscall.o obj/user/libs/ulib.o obj/user/libs/initcode.o obj/user/libs/stdio.o obj/user/libs/umain.o obj/libs/string.o obj/libs/printfmt.o obj/libs/hash.o obj/libs/rand.o obj/user/faultread.o
objdump -S obj/__user_faultread.out > obj/user/faultread.asm
objdump -t obj/__user_faultread.out | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$/d' > obj/user/faultread.sym
echo + cc user/pgdir.c
gcc -Iuser/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Iuser/include/ -Iuser/libs/ -c user/pgdir.c -o obj/user/pgdir.o
ld -m elf_i386 -nostdlib -T tools/user.ld -o obj/__user_pgdir.out obj/user/libs/panic.o obj/user/libs/syscall.o obj/user/libs/ulib.o obj/user/libs/initcode.o obj/user/libs/stdio.o obj/user/libs/umain.o obj/libs/string.o obj/libs/printfmt.o obj/libs/hash.o obj/libs/rand.o obj/user/pgdir.o
objdump -S obj/__user_pgdir.out > obj/user/pgdir.asm
objdump -t obj/__user_pgdir.out | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$/d' > obj/user/pgdir.sym
echo + cc user/exit.c
gcc -Iuser/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Iuser/include/ -Iuser/libs/ -c user/exit.c -o obj/user/exit.o
ld -m elf_i386 -nostdlib -T tools/user.ld -o obj/__user_exit.out obj/user/libs/panic.o obj/user/libs/syscall.o obj/user/libs/ulib.o obj/user/libs/initcode.o obj/user/libs/stdio.o obj/user/libs/umain.o obj/libs/string.o obj/libs/printfmt.o obj/libs/hash.o obj/libs/rand.o obj/user/exit.o
objdump -S obj/__user_exit.out > obj/user/exit.asm
objdump -t obj/__user_exit.out | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$/d' > obj/user/exit.sym
echo + cc user/softint.c
gcc -Iuser/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Iuser/include/ -Iuser/libs/ -c user/softint.c -o obj/user/softint.o
ld -m elf_i386 -nostdlib -T tools/user.ld -o obj/__user_softint.out obj/user/libs/panic.o obj/user/libs/syscall.o obj/user/libs/ulib.o obj/user/libs/initcode.o obj/user/libs/stdio.o obj/user/libs/umain.o obj/libs/string.o obj/libs/printfmt.o obj/libs/hash.o obj/libs/rand.o obj/user/softint.o
objdump -S obj/__user_softint.out > obj/user/softint.asm
objdump -t obj/__user_softint.out | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$/d' > obj/user/softint.sym
echo + cc user/waitkill.c
gcc -Iuser/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Iuser/include/ -Iuser/libs/ -c user/waitkill.c -o obj/user/waitkill.o
ld -m elf_i386 -nostdlib -T tools/user.ld -o obj/__user_waitkill.out obj/user/libs/panic.o obj/user/libs/syscall.o obj/user/libs/ulib.o obj/user/libs/initcode.o obj/user/libs/stdio.o obj/user/libs/umain.o obj/libs/string.o obj/libs/printfmt.o obj/libs/hash.o obj/libs/rand.o obj/user/waitkill.o
objdump -S obj/__user_waitkill.out > obj/user/waitkill.asm
objdump -t obj/__user_waitkill.out | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$/d' > obj/user/waitkill.sym
echo + cc user/spin.c
gcc -Iuser/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Iuser/include/ -Iuser/libs/ -c user/spin.c -o obj/user/spin.o
ld -m elf_i386 -nostdlib -T tools/user.ld -o obj/__user_spin.out obj/user/libs/panic.o obj/user/libs/syscall.o obj/user/libs/ulib.o obj/user/libs/initcode.o obj/user/libs/stdio.o obj/user/libs/umain.o obj/libs/string.o obj/libs/printfmt.o obj/libs/hash.o obj/libs/rand.o obj/user/spin.o
objdump -S obj/__user_spin.out > obj/user/spin.asm
objdump -t obj/__user_spin.out | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$/d' > obj/user/spin.sym
echo + cc user/yield.c
gcc -Iuser/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Iuser/include/ -Iuser/libs/ -c user/yield.c -o obj/user/yield.o
ld -m elf_i386 -nostdlib -T tools/user.ld -o obj/__user_yield.out obj/user/libs/panic.o obj/user/libs/syscall.o obj/user/libs/ulib.o obj/user/libs/initcode.o obj/user/libs/stdio.o obj/user/libs/umain.o obj/libs/string.o obj/libs/printfmt.o obj/libs/hash.o obj/libs/rand.o obj/user/yield.o
objdump -S obj/__user_yield.out > obj/user/yield.asm
objdump -t obj/__user_yield.out | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$/d' > obj/user/yield.sym
echo + cc user/divzero.c
gcc -Iuser/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Iuser/include/ -Iuser/libs/ -c user/divzero.c -o obj/user/divzero.o
ld -m elf_i386 -nostdlib -T tools/user.ld -o obj/__user_divzero.out obj/user/libs/panic.o obj/user/libs/syscall.o obj/user/libs/ulib.o obj/user/libs/initcode.o obj/user/libs/stdio.o obj/user/libs/umain.o obj/libs/string.o obj/libs/printfmt.o obj/libs/hash.o obj/libs/rand.o obj/user/divzero.o
objdump -S obj/__user_divzero.out > obj/user/divzero.asm
objdump -t obj/__user_divzero.out | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$/d' > obj/user/divzero.sym
echo + cc user/testbss.c
gcc -Iuser/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Iuser/include/ -Iuser/libs/ -c user/testbss.c -o obj/user/testbss.o
ld -m elf_i386 -nostdlib -T tools/user.ld -o obj/__user_testbss.out obj/user/libs/panic.o obj/user/libs/syscall.o obj/user/libs/ulib.o obj/user/libs/initcode.o obj/user/libs/stdio.o obj/user/libs/umain.o obj/libs/string.o obj/libs/printfmt.o obj/libs/hash.o obj/libs/rand.o obj/user/testbss.o
objdump -S obj/__user_testbss.out > obj/user/testbss.asm
objdump -t obj/__user_testbss.out | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$/d' > obj/user/testbss.sym
echo + cc user/faultreadkernel.c
gcc -Iuser/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Iuser/include/ -Iuser/libs/ -c user/faultreadkernel.c -o obj/user/faultreadkernel.o
ld -m elf_i386 -nostdlib -T tools/user.ld -o obj/__user_faultreadkernel.out obj/user/libs/panic.o obj/user/libs/syscall.o obj/user/libs/ulib.o obj/user/libs/initcode.o obj/user/libs/stdio.o obj/user/libs/umain.o obj/libs/string.o obj/libs/printfmt.o obj/libs/hash.o obj/libs/rand.o obj/user/faultreadkernel.o
objdump -S obj/__user_faultreadkernel.out > obj/user/faultreadkernel.asm
objdump -t obj/__user_faultreadkernel.out | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$/d' > obj/user/faultreadkernel.sym
echo + cc user/forktest.c
gcc -Iuser/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Iuser/include/ -Iuser/libs/ -c user/forktest.c -o obj/user/forktest.o
ld -m elf_i386 -nostdlib -T tools/user.ld -o obj/__user_forktest.out obj/user/libs/panic.o obj/user/libs/syscall.o obj/user/libs/ulib.o obj/user/libs/initcode.o obj/user/libs/stdio.o obj/user/libs/umain.o obj/libs/string.o obj/libs/printfmt.o obj/libs/hash.o obj/libs/rand.o obj/user/forktest.o
objdump -S obj/__user_forktest.out > obj/user/forktest.asm
objdump -t obj/__user_forktest.out | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$/d' > obj/user/forktest.sym
mkdir -p bin/
echo + ld bin/kernel
ld -m elf_i386 -nostdlib -T tools/kernel.ld -o bin/kernel obj/kern/init/entry.o obj/kern/init/init.o obj/kern/libs/stdio.o obj/kern/libs/readline.o obj/kern/debug/panic.o obj/kern/debug/kdebug.o obj/kern/debug/kmonitor.o obj/kern/driver/ide.o obj/kern/driver/clock.o obj/kern/driver/console.o obj/kern/driver/picirq.o obj/kern/driver/intr.o obj/kern/trap/trap.o obj/kern/trap/vectors.o obj/kern/trap/trapentry.o obj/kern/mm/pmm.o obj/kern/mm/swap_fifo.o obj/kern/mm/vmm.o obj/kern/mm/kmalloc.o obj/kern/mm/swap.o obj/kern/mm/default_pmm.o obj/kern/fs/swapfs.o obj/kern/process/switch.o obj/kern/process/entry.o obj/kern/process/proc.o obj/kern/schedule/sched.o obj/kern/syscall/syscall.o obj/libs/string.o obj/libs/printfmt.o obj/libs/hash.o obj/libs/rand.o -b binary obj/__user_hello.out obj/__user_badarg.out obj/__user_forktree.out obj/__user_badsegment.out obj/__user_faultread.out obj/__user_pgdir.out obj/__user_exit.out obj/__user_softint.out obj/__user_waitkill.out obj/__user_spin.out obj/__user_yield.out obj/__user_divzero.out obj/__user_testbss.out obj/__user_faultreadkernel.out obj/__user_forktest.out
objdump -S bin/kernel > obj/kernel.asm
objdump -t bin/kernel | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$/d' > obj/kernel.sym
echo + cc boot/bootasm.S
gcc -Iboot/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Os -nostdinc -c boot/bootasm.S -o obj/boot/bootasm.o
echo + cc boot/bootmain.c
gcc -Iboot/ -march=i686 -fno-builtin -fno-PIC -Wall -ggdb -m32 -gstabs -nostdinc -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size -fno-stack-protector -Ilibs/ -Os -nostdinc -c boot/bootmain.c -o obj/boot/bootmain.o
echo + cc tools/sign.c
gcc -Itools/ -g -Wall -O2 -c tools/sign.c -o obj/sign/tools/sign.o
gcc -g -Wall -O2 obj/sign/tools/sign.o -o bin/sign
echo + ld bin/bootblock
ld -m elf_i386 -nostdlib -N -T tools/boot.ld obj/boot/bootasm.o obj/boot/bootmain.o -o obj/bootblock.o
objdump -S obj/bootblock.o > obj/bootblock.asm
objcopy -S -O binary obj/bootblock.o obj/bootblock.out
bin/sign obj/bootblock.out bin/bootblock
dd if=/dev/zero of=bin/ucore.img count=10000
dd if=bin/bootblock of=bin/ucore.img conv=notrunc
dd if=bin/kernel of=bin/ucore.img seek=1 conv=notrunc
dd if=/dev/zero of=bin/swap.img bs=1024k count=128
2.如何使用GCC生成C程序的宏展开文件?
在ucore调试的过程中,会遇到多层嵌套的宏定义,此时理解起来十分的困难,那么知道宏展开的结果十分重要(例如以下test.c的宏展开)。
//test.c文件
// kernel_execve - do SYS_exec syscall to exec a user program called by user_main kernel_thread
static int
kernel_execve(const char *name, unsigned char *binary, size_t size) {int ret, len = strlen(name);asm volatile ("int %1;": "=a" (ret): "i" (T_SYSCALL), "0" (SYS_exec), "d" (name), "c" (len), "b" (binary), "D" (size): "memory");return ret;
}#define __KERNEL_EXECVE(name, binary, size) ({ \cprintf("kernel_execve: pid = %d, name = \"%s\".\n", \current->pid, name); \kernel_execve(name, binary, (size_t)(size)); \})#define KERNEL_EXECVE(x) ({ \extern unsigned char _binary_obj___user_##x##_out_start[], \_binary_obj___user_##x##_out_size[]; \__KERNEL_EXECVE(#x, _binary_obj___user_##x##_out_start, \_binary_obj___user_##x##_out_size); \})#define __KERNEL_EXECVE2(x, xstart, xsize) ({ \extern unsigned char xstart[], xsize[]; \__KERNEL_EXECVE(#x, xstart, (size_t)xsize); \})#define KERNEL_EXECVE2(x, xstart, xsize) __KERNEL_EXECVE2(x, xstart, xsize)// user_main - kernel thread used to exec a user program
static int
user_main(void *arg) {
#ifdef TESTKERNEL_EXECVE2(TEST, TESTSTART, TESTSIZE);
#elseKERNEL_EXECVE(exit);
#endifpanic("user_main execve failed.\n");
}
在上面的代码中,使用的宏定义如下:
TEST=hello
TESTSTART=_binary_obj___user_hello_out_start
TESTSIZE=_binary_obj___user_hello_out_size
此时需要gcc的命令输出C代码的预处理结果,这个命令如下:
gcc -E -DTEST=hello -DTESTSTART=_binary_obj___user_hello_out_start -DTESTSIZE=_binary_obj___user_hello_out_size test.c -o test.i
最终宏展开的详细输出的结果如下:
# 1 "test.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "test.c"static int
kernel_execve(const char *name, unsigned char *binary, size_t size) {int ret, len = strlen(name);asm volatile ("int %1;": "=a" (ret): "i" (T_SYSCALL), "0" (SYS_exec), "d" (name), "c" (len), "b" (binary), "D" (size): "memory");return ret;
}
# 35 "test.c"
static int
user_main(void *arg) {({ extern unsigned char _binary_obj___user_hello_out_start[], _binary_obj___user_hello_out_size[]; ({ cprintf("kernel_execve: pid = %d, name = \"%s\".\n", current->pid, "hello"); kernel_execve("hello", _binary_obj___user_hello_out_start, (size_t)((size_t)_binary_obj___user_hello_out_size)); });
});panic("user_main execve failed.\n");
}
在上面的代码中,变量_binary_obj___user_hello_out_start是外部变量,只有声明(说明变量的性质,但并不分配存储单元),没有定义(表示创建变量或分配存储单元),那在哪里定义的呢?
这是由于gcc指令中-D的宏(-Dmacro[=defn]),因此可以理解_binary_obj___user_hello_out_start在gcc命令表示成定义,而不是声明。于是上述的代码是可以生成可执行文件的。
-D macro=string,等价于在头文件中定义:#define macro string。
为了验证相当于#define macro string,下面的Qt代码能打印输出。
#include <stdio.h>#include <stdint.h>#define TEST hello
#define TESTSTART _binary_obj___user_hello_out_start
#define TESTSIZE _binary_obj___user_hello_out_size#define __KERNEL_EXECVE(name, binary, size) ({ \printf("kernel_execve: pid = %d, name = \"%s\".\n", \1, name); \})#define KERNEL_EXECVE(x) ({ \extern unsigned char _binary_obj___user_##x##_out_start[], \_binary_obj___user_##x##_out_size[]; \__KERNEL_EXECVE(#x, _binary_obj___user_##x##_out_start, \_binary_obj___user_##x##_out_size); \})#define __KERNEL_EXECVE2(TEST, TESTSTART, TESTSIZE) ({ \extern unsigned char xstart[], xsize[]; \__KERNEL_EXECVE("hello", _binary_obj___user_hello_out_start, (size_t)_binary_obj___user_hello_out_size); \})#define KERNEL_EXECVE2(TEST, TESTSTART, TESTSIZE) __KERNEL_EXECVE2(TEST, TESTSTART, TESTSIZE)// user_main - kernel thread used to exec a user program
static int
user_main(void *arg) {
#ifdef TESTKERNEL_EXECVE2(TEST, TESTSTART, TESTSIZE);
#elseKERNEL_EXECVE(exit);
#endifprintf("user_main execve failed.\n");
}int main()
{user_main(NULL);return 0;
}
3.如何在qemu中执行特定的可执行程序?
在Lab8中,用户态编译了多个进程,如hello、badarg,如何在文件系统中执行某个单独的应用程序,是一个关键的问题。
首先,执行make clean,这一步将清空Lab8工程所有的编译输出结果。
然后,执行make ,这一步将生成ucore镜像以及应用程序。
然后,执行make qemu,这一步将启动qemu虚拟机。
然后,在qemu输入输出界面,按下回车键进入shell界面
最后,在shell的终端界面中执行sfs_filetest1