Skip to content
chyyuu edited this page Apr 26, 2019 · 18 revisions

Way #1:

cd kernel
make run arch=x86_64 mode=debug

qemu-system-x86_64 -smp cores=4 -drive format=raw,file=target/x86_64/debug/bootimage.bin -serial mon:stdio -m 4G -device isa-debug-exit -drive format=qcow2,file=../user/build/x86_64.qcow2,media=disk,cache=writeback,id=sfsimg,if=none -device ahci,id=ahci0 -device ide-drive,drive=sfsimg,bus=ahci0.0 -nographic  -S -s

OR ## for x86_64.qcow2

qemu-system-x86_64 -smp cores=1 -drive format=raw,file=target/x86_64/release/bootimage.bin -serial mon:stdio -m 4G -device isa-debug-exit -drive format=qcow2,file=../user/build/x86_64.qcow2,media=disk,cache=writeback,id=sfsimg,if=none -device ahci,id=ahci0 -device ide-drive,drive=sfsimg,bus=ahci0.0 -nographic  -S -s

# in another terminal
# That is, if GDB is stopped at a breakpoint in a function in crate ‘A’, module ‘B’, then break B::f will attempt to set a breakpoint in a function named ‘f’ in a crate named ‘B’.
cd kernel
gdb target/x86_64/debug/rcore 
(gdb) target remote :1234
(gdb) continue
(gdb) b _start
(gdb) continue
(gdb) b rcore::trap::error::h608153f262c04ec9
## meet some crash
(gdb) where

Way #2:

cd kernel
make debug arch=x86_64 mode=debug

Way #3:

cd kernel
make run arch=x86_64 mode=debug
# in QEMU
# Press C-a c
/ # QEMU 3.1.0 monitor - type 'help' for more information
(qemu) gdbserver
Waiting for gdb connection on device 'tcp::1234'
(qemu)
# in another terminal
gdb target/x86_64/debug/rcore -x ../tools/gdbinit

test gcc The quickest way to install GCC on Alpine Linux is by issuing the following command:

#for alpine linux
apk update
apk add build-base

test dynamic_lib-based gcc

dynamic_lib-based gcc tools 位于 /usr/bin目录下 目前dynamic_lib-based gcc 执行会出错

### in rcore/kernel
cd kernel 
make run arch=x86_64 smp=1 mode=release LOG=debug
## 就可以进入到rcore的执行中

cd /test
#step0: 用gcc 这个driver 执行fork+execve来测试下面的cc1/as/ld的具体执行过程
gcc env1.c
#或者 分步骤测试
gcc -E env1.c -o env1.i
gcc -S env1.i

#或者 单独测试各个cc1/as/ld
#step1: produce dev1.i
gcc -E env1.c -o env1.i
#OR
/usr/libexec/gcc/x86_64-alpine-linux-musl/8.3.0/cc1 -E -quiet  env1.c -o env1.i
#step2: produce dev1.s
/usr/libexec/gcc/x86_64-alpine-linux-musl/8.3.0/cc1 -quiet env1.i -o env1.s

#step3: produce dev1.o
 /usr/x86_64-alpine-linux-musl/bin/as env1.s -o env1.o

#step4: produce dev1
/usr/x86_64-alpine-linux-musl/bin/ld -dynamic-linker /lib/ld-musl-x86_64.so.1 /usr/lib/crt1.o /usr/lib/crtn.o /usr/lib/gcc/x86_64-alpine-linux-musl/8.3.0/crtbeginS.o /usr/lib/gcc/x86_64-alpine-linux-musl/8.3.0/crtendS.o env1.o -o env1 -lc -static

way3

#in ash  need setup PATH env var
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/x86_64-alpine-linux-musl/bin
cd test
gcc -o env1 env1.c

usr/kern mem layout

in kernel/src/arch/x86_64/consts.rs

in step2

cc1 出错情况
 WARN] sys_sigaction is unimplemented
[DEBUG] 0:2:2 syscall id 13 ret with Ok(0)
[DEBUG] 0:2:2 syscall id 13 begin
[ WARN] sys_sigaction is unimplemented
[DEBUG] 0:2:2 syscall id 13 ret with Ok(0)
[DEBUG] 0:2:2 syscall id 14 begin
[ WARN] sys_sigprocmask is unimplemented
[DEBUG] 0:2:2 syscall id 14 ret with Ok(0)
[DEBUG] 0:2:2 syscall id 13 begin
[ WARN] sys_sigaction is unimplemented
[DEBUG] 0:2:2 syscall id 13 ret with Ok(0)
[DEBUG] 0:2:2 syscall id 14 begin
[ WARN] sys_sigprocmask is unimplemented
[DEBUG] 0:2:2 syscall id 14 ret with Ok(0)
[DEBUG] 0:2:2 syscall id 13 begin
[ WARN] sys_sigaction is unimplemented
[DEBUG] 0:2:2 syscall id 13 ret with Ok(0)
[DEBUG] 0:2:2 syscall id 9 begin
[ INFO] mmap: addr=0x0, size=0x1000, prot=READ | WRITE, flags=PRIVATE | ANONYMOUS, fd=18446744073709551615, offset=0x0
[DEBUG] 0:2:2 syscall id 9 ret with Ok(26a000)
[ERROR] TrapFrame {
    fpstate_offset: 0x8,
    fpstate: fpstate,
    fsbase: 0x202790,
    r15: 0x0,
    r14: 0x180009fff7f,
    r13: 0x593d1a,
    r12: 0x180009ffef0,
    rbp: 0x0,
    rbx: 0xe3,
    r11: 0x1,
    r10: 0x1,
    r9: 0x1000,
    r8: 0x41,
    rsi: 0x8dbc00000000600,
    rdi: 0xe3,
    rdx: 0x1793e00,
    rcx: 0x0,
    rax: 0x0,
    trap_num: 0xd,
    error_code: 0x0,
    rip: 0xf5f607,
    cs: 0x2b,
    rflags: 0x206,
    rsp: 0x180009ffd60,
    ss: 0x23
}
[ERROR] On CPU0 Thread 2
[ INFO] PageTable dropping: InactivePageTable0 { p4_frame: PhysFrame[4KiB](0x13a974000) }
qemu-system-x86_64: terminating on signal 15 from pid 11419 ()


cc1 反汇编 objdump -S cc1
0000000000f5f5ef <_obstack_free>:                  
  f5f5ef:       41 54                   push   %r12                                         
  f5f5f1:       55                      push   %rbp 
  f5f5f2:       48 89 f5                mov    %rsi,%rbp          
  f5f5f5:       53                      push   %rbx                                            
  f5f5f6:       48 8b 77 08             mov    0x8(%rdi),%rsi
  f5f5fa:       48 89 fb                mov    %rdi,%rbx                                                       
  f5f5fd:       48 85 f6                test   %rsi,%rsi
  f5f600:       74 34                   je     f5f636 <_obstack_free+0x47>
  f5f602:       48 39 ee                cmp    %rbp,%rsi                                              
  f5f605:       72 15                   jb     f5f61c <_obstack_free+0x2d>
  f5f607:       4c 8b 66 08             mov    0x8(%rsi),%r12                          
  f5f60b:       48 89 df                mov    %rbx,%rdi
  f5f60e:       e8 71 fe ff ff          callq  f5f484 <make_relative_prefix_ignore_links+0xc5>
  f5f613:       80 4b 50 02             orb    $0x2,0x50(%rbx)                              
  f5f617:       4c 89 e6                mov    %r12,%rsi
  f5f61a:       eb e1                   jmp    f5f5fd <_obstack_free+0xe>              
  f5f61c:       48 8b 06                mov    (%rsi),%rax
  f5f61f:       48 39 e8                cmp    %rbp,%rax          
  f5f622:       72 e3                   jb     f5f607 <_obstack_free+0x18>                                 
  f5f624:       48 89 6b 18             mov    %rbp,0x18(%rbx)
  f5f628:       48 89 6b 10             mov    %rbp,0x10(%rbx)                                
  f5f62c:       48 89 43 20             mov    %rax,0x20(%rbx)
  f5f630:       48 89 73 08             mov    %rsi,0x8(%rbx)     
  f5f634:       eb 0a                   jmp    f5f640 <_obstack_free+0x51>                          
  f5f636:       48 85 ed                test   %rbp,%rbp
  f5f639:       74 05                   je     f5f640 <_obstack_free+0x51>                
  f5f63b:       e8 80 fc 62 ff          callq  58f2c0 <abort@plt>
  f5f640:       5b                      pop    %rbx               
  f5f641:       5d                      pop    %rbp                                             
  f5f642:       41 5c                   pop    %r12
  f5f644:       c3                      retq           

the source code of obstack_free is in gcc-8.3.0/libiberty/obstrack.c

reference