如何使用 gdb 追蹤 xv6 的 system call 過程

本文目標:照著以下步驟就可以看到整個 system call 的過程 整個過程大致上都是照著這個影片做的,但其中有幾個步驟稍稍的不同。 2. 用 gdb-multiarch debug xv6 的方式 這裡會需要開啟 2 個終端機, 先在其中一個終端機輸入 make qemu-gdb # 這是被 debug 的對象 在另一個終端機輸入 gdb-multiarch # 開啟 debuger 會開始針對上面的那個終端機中的程式進行除錯 不知道也沒關係的點:gdb-multiarch 是透過 .gdbinit 這個檔案找到 debug 的對象的 # .gdbinit set confirm off set architecture riscv:rv64 target remote 127.0.0.1:26000 # 透過這個來找到 qemu symbol-file kernel/kernel set disassemble-next-line auto set riscv use-compressed-breakpoints yes 動機:$ 的出現 make qemu 後 xv6 開機時,會顯示的畫面 xv6 kernel is booting hart 1 starting hart 2 starting init: starting sh $ xv6 第一個執行的程式是 sh,它最一開始會 print 出字元 $ print 的過程需要使用 system call write。 ...

September 27, 2025

[xv6 學習紀錄 01] Lab: Xv6 and Unix utilities

Lab 連結: Lab: Xv6 and Unix utilities Boot xv6(Easy) 題目敘述: 這部份的詳細內容都寫在 lab util 中,會需要一個 linux 系統(windows使用者可以用虛擬機),Xv6 會跑在 linux 所架設的虛擬機上。 下載原始碼 git clone git://g.csail.mit.edu/xv6-labs-2022 cd xv6-labs-2022 安裝架設虛擬機的套件 我自己是用 Debian,如果你用的是 ubuntu 的話下載步驟應該也是一樣的,至於是其他系統的使用者,可以看這裡 sudo apt-get install git build-essential gdb-multiarch qemu-system-misc gcc-riscv64-linux-gnu binutils-riscv64-linux-gnu compile程式碼並且讓他跑在虛擬機上 $ make qemu ... (一大串訊息) ... xv6 kernel is booting hart 2 starting hart 1 starting init: starting sh $ 到這裡,Xv6已經成功開機了! 嘗試打個指令 $ ls . 1 1 1024 .. 1 1 1024 README 2 2 2059 xargstest.sh 2 3 93 cat 2 4 24120 echo 2 5 22944 forktest 2 6 13184 grep 2 7 27424 init 2 8 23680 kill 2 9 22904 ln 2 10 22744 ls 2 11 26312 mkdir 2 12 23040 rm 2 13 23032 sh 2 14 41856 stressfs 2 15 23904 usertests 2 16 148312 grind 2 17 38008 wc 2 18 25232 zombie 2 19 22280 console 3 20 0 沒意外的話,會出現以上的畫面 ...

September 24, 2025

[xv6 學習紀錄 02] Lab: Xv6 and Unix utilities

Lab 連結: Lab: system calls 大綱 xv6 有哪些 system call,以及他們的作用為何 ? 以程式碼的觀點來理解 xv6 的 system call 流程 Using gdb System call tracing 1. xv6 有哪些 system call,以及他們的作用為何 ? 0. kernel/syscall.h 定義 syste mcall 的編號 // System call numbers #define SYS_fork 1 #define SYS_exit 2 #define SYS_wait 3 #define SYS_pipe 4 #define SYS_read 5 #define SYS_kill 6 #define SYS_exec 7 #define SYS_fstat 8 #define SYS_chdir 9 #define SYS_dup 10 #define SYS_getpid 11 #define SYS_sbrk 12 #define SYS_sleep 13 #define SYS_uptime 14 #define SYS_open 15 #define SYS_write 16 #define SYS_mknod 17 #define SYS_unlink 18 #define SYS_link 19 #define SYS_mkdir 20 #define SYS_close 21 2. 以程式碼的觀點來理解 xv6 的 system call 流程 以下使用 user/cat.c 為例,來探討 xv6 的 system call 流程,流程中有 3 大步驟 ...

September 24, 2025