Systemtap 是一種在運行時收集 Linux 系統信息的自由軟體(GPL)框架。
SystemTap
安裝 systemtapAUR 或 systemtap-gitAUR。和上游版本的對比:[1].
要從原始碼編譯,請訪問這裡。最新代碼會包含新內核版本和發行版的支持。
標準內核
至少需要安裝軟體包 linux-headers包。
Arch 會從發布的二進制文件,包括內核中刪除調試數據。很多 systmetap 功能就不能使用了。stapprobes 手冊記錄了 NON-DWARF 和 AUTO-DWARF 類型下什麼功能可用:
- kernel tracepoints: kernel.trace("*")
- user-space probes: process("...").function("...") (for programs you build yourself with -g)
- user-space markers: process("...").mark("...") (if they were configured with the <sys/sdt.h> markers)
- perfctr-based probes: perf.*
- non-dwarf kernel probes: kprobe.function("...") and nd_syscall.* tapset (if a /boot/System.map* file is available, see below).
內核重新編譯
可用自定義 linux-custom 軟體包之後再運行 SystemTap。重新編譯 linux包 軟體包也非常方便。參考 Kernels/Traditional compilation。
準備
首先,運行 cd ~/ && mkdir build && cd build/ && asp checkout linux && cd linux/trunk
來獲取原始內核構建文件。然後使用 makepkg --verifysource
獲取附加文件。通過執行驗證,您可以安全地跳過「更新校驗和」步驟。
修改config文件
編輯 config (32位內核) 或 config.x86_64 (64位內核), 確保打開這些選項:
- CONFIG_KPROBES=y
- CONFIG_KPROBES_SANITY_TEST=n
- CONFIG_KPROBE_EVENT=y
- CONFIG_NET_DCCPPROBE=m
- CONFIG_NET_SCTPPROBE=m
- CONFIG_NET_TCPPROBE=y
- CONFIG_DEBUG_INFO=y
- CONFIG_DEBUG_INFO_REDUCED=n
- CONFIG_X86_DECODER_SELFTEST=n
- CONFIG_DEBUG_INFO_VTA=y
默認只有CONFIG_DEBUG_INFO 和CONFIG_DEBUG_INFO_REDUCED沒被打開,修改這兩個即可.
更新校驗值
執行 md5sum config[.x86_64]
獲得新的文件校驗值.
編輯 PKGBUILD 文件, 這一部分 md5sums=('sum-of-first' ... 'sum-of-last')
和這一部分
source=('first-source' ... 'last-source')
是個數相同,順序相同的, 把新獲得的校驗值在合適的位置替換.
makepkg --skipchecksums
使用命令可以跳過校驗,但這樣做對其它文件(比如下載的內核源碼包)來說不安全,因此建議按這裡給出的方法操作。
編譯並安裝
可選步驟: 可以在 /etc/makepkg.conf
文件中設置 MAKEFLAGS="-j16"
加速編譯.
執行 makepkg
開始編譯, 然後 sudo pacman -U *.pkg.tar.gz
安裝編譯好的包.
pacman 會提示你這是重新安裝 (reinstall), 這就對了!
linux包 和 linux-headers包 需要安, linux-docs包 則隨意.
通過這個方法, 外部內核模塊 (例如 nvidia包 和 virtualbox包) 就不需要被重新編譯了.
Systemtap
從 AUR 中安裝systemtap即可: systemtapAUR, 完成.
編譯自定義內核
參考這個官方README
問題處理
版本匹配問題
如果出現如下錯誤:
/usr/share/systemtap/runtime/stat.c:214:2: error: 'cpu_possible_map' undeclared (first use in this function)
請安裝 systemtap-git。
System.map丟失
你可以再啟用 DEBUG_INFO 的情況下構建Linux內核時恢復它:
cp src/linux-3.6/System.map /boot/System.map-3.6.7-1-ARCH
或者執行以下命令,
sudo cp /proc/kallsyms /boot/System.map-`uname -r`
Process return probes not available
如果你確定內核配置正確,但再啟動 stap
時收到以下兩條信息:
WARNING: Kernel function symbol table missing [man warning::symbols]
semantic error: process return probes not available [man error::inode-uprobes]
那麼SystemTap可能未能驗證此功能的支持,你可以在 System.map丟失這個步驟中解決此問題。