系列简介:TinyLinux 是由 LabHub 发起的"大道至简"Linux 学习项目。我们通过对内核进行"剔骨"级的物理精简,旨在构建一个 100% 透明、可重现的极简实验室。
公众号:LabHub | 项目仓库:https://gitee.com/lynyujiang/tiny-linux.git
scripts/gen_callgraph.shscripts/trace_galaxy.sh | |
CONFIG_FTRACE=yCONFIG_FRAME_POINTER=y | |
-s -S 参数启动后,QEMU 在 1234 端口监听 GDB 连接,暂停等待调试器。用户空间程序调试相对简单:
# 用户空间调试
gdb ./my_program
(gdb) break main
(gdb) run
但内核调试面临挑战:
// 在内核代码中添加
printk(KERN_INFO "DEBUG: value = %d\n", value);
// 查看输出
dmesg | tail -20
# 或通过串口输出到主机
优点:简单直接,无需额外工具缺点:需要重新编译,信息有限
# QEMU 端
qemu-system-x86_64 ... -s -S
# GDB 端
gdb vmlinux
(gdb) target remote :1234
(gdb) break start_kernel
(gdb) continue
优点:可单步执行、查看变量缺点:系统暂停,调试时间长
# 启用 ftrace
mount -t debugfs none /sys/kernel/debug
echo function_graph > /sys/kernel/debug/tracing/current_tracer
echo start_kernel > /sys/kernel/debug/tracing/set_ftrace_filter
echo 1 > /sys/kernel/debug/tracing/tracing_on
# 查看追踪结果
cat /sys/kernel/debug/tracing/trace
优点:无需重新编译,信息详细缺点:需要内核支持,输出量大
由于 TinyLinux 极致精简,调试反而更简单:
#!/bin/bash
# debug_kernel.sh - GDB 内核调试脚本
# 1. 启动 QEMU(带 GDB 支持)
qemu-system-x86_64 \
-m 512 \
-kernel output/target/bzImage \
-initrd output/target/rootfs.cpio.xz \
-nographic \
-s -S # -s: 在 1234 端口监听 GDB
# -S: 启动时暂停,等待 GDB 连接
# 2. 在另一个终端连接 GDB
gdb vmlinux
(gdb) target remote :1234
(gdb) break start_kernel
(gdb) continue
# 进入内核源码目录
cd linux-6.12.51
# 生成 Cscope 数据库
make cscope
# 常用查询命令
cscope -d -L -1 start_kernel # 查找函数定义
cscope -d -L -2 start_kernel # 查找调用者
cscope -d -L -3 start_kernel # 查找引用位置
cscope -d -L -0 start_kernel # 查找出现位置
#!/bin/bash
###############################################################################
# TinyLinux Call Graph Generator
# 生成可视化的内核函数调用图
###############################################################################
MODE="forward"# forward: 向下追踪 / reverse: 向上追踪
ENTRY_POINT="start_kernel"
MAX_DEPTH=2
# 1. 确保 Cscope 数据库存在
[ -f "cscope.out" ] || make cscope
# 2. 递归追踪函数调用
trace_logic() {
local func=$1
local depth=$2
if [ $depth -ge $MAX_DEPTH ]; thenreturn; fi
# 查询调用者或被调用者
local results
if [ "${MODE}" == "forward" ]; then
results=$(cscope -d -L -2 "${func}" | awk "{print \$2}" | sort -u)
else
results=$(cscope -d -L -3 "${func}" | awk "{print \$2}" | sort -u)
fi
# 生成连接关系
for item in$results; do
echo"{ \"from\": \"${func}\", \"to\": \"${item}\" }"
trace_logic "${item}" $((depth + 1))
done
}
# 3. 生成 HTML 可视化文件
# 使用 vis-network 库渲染交互式图表
#!/bin/bash
###############################################################################
# TinyLinux Trace Galaxy - Ftrace 解析和可视化
###############################################################################
TRACE_LOG=$1
OUTPUT_DIR="output/docs"
# 1. 解析 ftrace 输出(function_graph 格式)
# 示例输入:
# 1) | start_kernel() {
# 1) | set_task_stack_end_magic();
awk '
/\(\)\\{/ {
# 提取函数名和深度
match($0, /[a-zA-Z0-9_]+\(\)/);
name = substr($0, RSTART, RLENGTH-2);
depth = index($0, "|");
# 记录父子关系
printf "{ \"from\": \"%s\", \"to\": \"%s\" }\n", parent, name;
parent = name;
}
'"${TRACE_LOG}" > "${OUTPUT_DIR}/trace_links.json"
# 2. 生成动态追踪可视化 HTML
# 亮绿色表示实际执行过的代码路径
# GDB 常用命令
(gdb) target remote :1234 # 连接 QEMU
(gdb) break start_kernel # 设置断点
(gdb) info breakpoints # 查看断点
(gdb) continue# 继续执行
(gdb) next # 单步执行(不进入函数)
(gdb) step # 单步执行(进入函数)
(gdb) print variable # 查看变量值
(gdb) backtrace # 查看调用栈
(gdb) info registers # 查看寄存器
# ftrace 常用命令
echo function_graph > current_tracer # 设置追踪器
echo start_kernel > set_ftrace_filter # 设置过滤
echo 1 > tracing_on # 开始追踪
echo 0 > tracing_on # 停止追踪
cat trace # 查看结果
CONFIG_DEBUG_INFO | |||
CONFIG_GDB_SCRIPTS | |||
CONFIG_FRAME_POINTER | |||
CONFIG_FUNCTION_TRACER | |||
CONFIG_FTRACE | |||
CONFIG_DYNAMIC_FTRACE |
⚠️ 注意:当前 TinyLinux 内核未启用
CONFIG_DEBUG_INFO,GDB 调试仅能查看汇编和寄存器,无法进行源码级调试。如需源码调试,需重新编译内核。
cd /home/devhub/xlabs/tiny-linux/linux-6.12.51
# 打开内核配置
make menuconfig
# 导航路径:
# Kernel hacking --->
# Compile-time checks and compiler options --->
# [*] Compile the kernel with debug info
# 或者手动编辑 .config
echo"CONFIG_DEBUG_INFO=y" >> .config
echo"CONFIG_GDB_SCRIPTS=y" >> .config
# 重新编译内核
cd ..
./scripts/build_kernel.sh
-s | -s | |
-S | -S | |
-d | -d int,cpu_reset | |
-D | -D qemu.log | |
-no-reboot | -no-reboot |
# 1. 安装 GDB(已安装)
which gdb
# 输出:/usr/bin/gdb
# 2. 生成 Cscope 数据库(必需)
cd linux-6.12.51
make cscope
# 输出:生成 cscope.out tags 文件
# 3. 验证 ftrace 支持(已启用)
grep "CONFIG_FUNCTION_TRACER" linux-6.12.51/.config
# 输出:CONFIG_FUNCTION_TRACER=y
# 4. 启动 QEMU(带 GDB 支持)
# 注意:当前内核无调试符号,GDB 仅能查看汇编
qemu-system-x86_64 -m 512 \
-kernel output/target/bzImage \
-initrd output/target/rootfs.cpio.xz \
-nographic -s -S
# 5. 连接 GDB
gdb linux-6.12.51/vmlinux
(gdb) target remote :1234
⚠️ 重要说明:
ftrace:✅ 已启用,可在 TinyLinux 内部使用 Cscope:⚠️ 需手动生成(运行 make cscope)GDB 源码调试:⚠️ 需重新编译内核(启用 CONFIG_DEBUG_INFO)GDB 汇编调试:✅ 当前即可使用(查看寄存器、内存)
# 终端 1: 启动 QEMU
cd /home/devhub/xlabs/tiny-linux
source env.sh
qemu-system-x86_64 \
-m 512 \
-kernel output/target/bzImage \
-initrd output/target/rootfs.cpio.xz \
-nographic -s -S
# 终端 2: 连接 GDB
gdb linux-6.12.51/vmlinux
(gdb) target remote :1234
(gdb) break start_kernel
Breakpoint 1 at 0xffffffff81000000: file init/main.c, line 1234.
(gdb) continue
Continuing.
# QEMU 端会停在 start_kernel 入口处
(gdb) next # 单步执行
(gdb) print jiffies # 查看变量
$1 = 4294943616
(gdb) backtrace # 查看调用栈
#0 start_kernel () at init/main.c:1234
#1 0x0000000000000000 in ?? ()
cd /home/devhub/xlabs/tiny-linux
# 正向追踪(从 start_kernel 向下)
./scripts/gen_callgraph.sh start_kernel 2
# 反向追踪(查找谁调用了某个函数)
./scripts/gen_callgraph.sh --reverse do_initcalls 2
# 生成的 HTML 文件位于
ls output/docs/*_galaxy.html
# 使用浏览器打开(需要 VS Code 支持 vscode:// 协议)
xdg-open output/docs/start_kernel_galaxy.html
# 在 TinyLinux 内部执行(需要内核支持)
mount -t debugfs none /sys/kernel/debug
# 设置追踪器
echo function_graph > /sys/kernel/debug/tracing/current_tracer
# 设置要追踪的函数
echo start_kernel > /sys/kernel/debug/tracing/set_ftrace_filter
# 开始追踪
echo 1 > /sys/kernel/debug/tracing/tracing_on
# 重启系统后查看追踪结果
cat /sys/kernel/debug/tracing/trace > trace.log
# 在主机上分析
./scripts/trace_galaxy.sh trace.log
cd linux-6.12.51
# 查找函数定义
cscope -d -L -1 start_kernel
# 输出:init/main.c start_kernel 1234
# 查找调用者
cscope -d -L -2 start_kernel
# 输出:列出所有调用 start_kernel 的位置
# 查找引用
cscope -d -L -3 jiffies
# 输出:列出所有使用 jiffies 的位置
# 使用 tags 文件(更快)
grep "^start_kernel" tags
# 输出:start_kernel init/main.c /^void __init start_kernel(void)$/;" f
# 问题 1: GDB 连接失败
# 解决:确认 QEMU 已启动且带有 -s -S 参数
netstat -tlnp | grep 1234
# 问题 2: 断点无法命中
# 解决:确认内核带调试信息编译
readelf -S vmlinux | grep debug
# 问题 3: Cscope 找不到符号
# 解决:重新生成数据库
make cscope
# 问题 4: ftrace 不可用
# 解决:检查内核配置
grep CONFIG_FUNCTION_TRACER .config
GDB 调试时关注:
✅ 断点命中时的寄存器状态
✅ 关键变量的值(如 jiffies、task_struct)
✅ 调用栈的完整性
✅ 内存布局(通过 info proc mappings)
函数调用图关注:
✅ 中心节点(被最多函数调用的)
✅ 叶子节点(不调用其他函数的)
✅ 跨子系统调用(不同颜色的连接)
✅ 循环调用(A→B→A)
ftrace 输出关注:
✅ 函数执行时间(带时间戳)
✅ 调用深度(缩进级别)
✅ 异常返回(带错误码)
✅ 中断上下文(带中断号)
# 启动 QEMU + GDB
qemu-system-x86_64 -m 512 -kernel output/target/bzImage \
-initrd output/target/rootfs.cpio.xz -nographic -s -S &
# 连接 GDB
gdb linux-6.12.51/vmlinux << 'EOF'
target remote :1234
break start_kernel
continue
print"TinyLinux GDB Test Success!"
quit
EOF
# 生成调用图
./scripts/gen_callgraph.sh start_kernel 2
# 检查输出文件
ls -lh output/docs/start_kernel_galaxy.html
# 预期:文件存在且大小 > 10KB
# 验证 HTML 内容
grep "vis-network" output/docs/start_kernel_galaxy.html
# 预期:包含 vis-network 库引用
cd linux-6.12.51
# 确保数据库存在
ls -lh cscope.out tags
# 预期:两个文件都存在
# 测试查询
cscope -d -L -1 start_kernel | head -3
# 预期:显示函数定义位置
9e35606c4 | |
git checkout 9e35606c4 | |
source env.sh && ./scripts/gen_callgraph.sh start_kernel 2 | |
output/docs/start_kernel_galaxy.html |
⚠️ 注意:Git 提交信息请执行
git log --oneline -5获取最新提交。
通过本章,我们掌握了内核调试的核心技能:
在 [TinyLinux-011] 中,我们将探索性能优化技巧,包括内核编译优化、启动时间优化和内存占用优化。敬请期待!
如果你觉得这个项目对理解 Linux 底层有帮助,请不要吝啬你的支持:
本文由 LabHub 团队原创,转载请注明出处。
项目地址:https://gitee.com/lynyujiang/tiny-linux.git