Feature/ksym backtrace - #11768
Conversation
Add optional symbol lookup for native SCons GCC/ELF builds. The generated table includes local, global, and weak functions in a compact relative-address format with a shared string table and per-symbol sizes. Use a lock-free binary search, preserve raw addresses, and canonicalize Thumb addresses only for 32-bit ARM. Verify generated addresses after the multi-pass link. Keep artifacts under build/ksym and pass the toolchain environment to the generators. The option remains disabled by default because the embedded function names and entries consume read-only memory.
Use the kernel symbol resolver from the common backtrace printers when RT_USING_KSYMS is enabled. Keep the raw program counter in every line and fall back to the address-only format when no symbol is found. Retain the existing addr2line guidance when the option is disabled.
Route Cortex-A unwind frames through the common formatted printer when kernel symbols are enabled. Disable the legacy Cortex-A function-name path in that mode to avoid duplicate output while retaining the existing unwind behavior. Add static-function lookup and backtrace coverage for ARM32 and AArch64.
|
👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread! 为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。 🛠 操作步骤 | Steps
完成后,提交将自动更新至 如有问题欢迎联系我们,再次感谢您的贡献!💐 |
📌 Code Review Assignment🏷️ Tag: kernelReviewers: @GorrayLi @ReviewSun @hamburger-os @lianux-mm @wdfk-prog @xu18838022837 Changed Files (Click to expand)
📊 Current Review Status (Last Updated: 2026-08-29 02:52 CST)
📝 Review Instructions
|
|
拉取/合并请求描述:(PR description)
为什么提交这份PR (why to submit this PR)
当前 RT-Thread 通用 backtrace 主要输出原始 PC 地址,并提示用户使用
addr2line进行解析。在 exception、assert、panic 等现场,通常没有完整 ELF/DWARF 环境,定位函数名称不够方便。
本 PR 增加一个可选的内核函数符号表,使 backtrace 可以直接输出函数名、
函数内偏移和函数大小,例如:
该功能默认关闭,不影响未启用配置的 ROM 和原有 backtrace 行为。
本 PR 包含三个 commit:
kernel: add optional ksym table
kernel: symbolize backtraces
libcpu: add Cortex-A ksym backtrace
你的解决方案是什么 (what is your solution)
新增 RT_USING_KSYMS 配置项,默认值为 n。
在 native SCons GCC/ELF 构建中,构建流程会从预链接 ELF 中提取函数符号,
生成紧凑的只读 kernel symbol table,然后重新链接最终 ELF:
objects
-> rtthread.pre1.elf
-> generate ksym table
-> rtthread.pre2.elf
-> generate final ksym table
-> rtthread.elf
-> verify final symbol addresses
符号表包含:
运行时使用无锁二分查找,不使用 heap、mutex 或动态内存。符号表使用相对地址、
name offset 和共享字符串表,以减少存储开销。
地址解析规则:
该版本第一阶段仅支持 GCC/ELF/native SCons 构建。其他工具链和
--target=makefile 等生成项目在启用 RT_USING_KSYMS 时会直接报错。
请提供验证的bsp和config (provide the config and bsp)
BSP:
.config:
CONFIG_RT_USING_KSYMS=y
CONFIG_RT_USING_UTEST=y
CONFIG_RT_UTEST_KSYMS=y
其中:
CONFIG_RT_USING_KSYMS=n
为默认配置,不启用时保持原有行为。
action:
https://github.com/liulangrenaaa/rt-thread/actions/workflows/manual_dist.yml
PR branch:
feature/ksym-backtrace
测试结果
Python generator tests
python3 -m unittest tools.testcases.test_gen_ksym
......
Ran 6 tests in 0.001s
OK
另外通过:
python3 -m py_compile
tools/gen_ksym.py
tools/ksym.py
tools/testcases/test_gen_ksym.py
ARM32 qemu-vexpress-a9
实际输出:
0x60043e90 <ksym_test_level3+0xc/0x14>
0x60043e7c <ksym_test_level2+0xc/0x14>
0x60043e68 <ksym_test_level1+0xc/0x14>
[ PASSED ] testcase (core.ksym)
[ PASSED ] 1 tests.
AArch64 qemu-virt64-aarch64
实际输出:
0x40099a44 <ksym_test_level3+0x8/0x18>
[ PASSED ] testcase (core.ksym)
RISC-V64 qemu-virt64-riscv
0x802430e6 <_query+0x9a/0x178>
后续失败发生在现有 RISC-V frame-pointer unwind 路径中。
unwind 访问非法 frame 地址 0x10,随后触发嵌套异常。
该问题发生在 symbol lookup 之后,与 rt_ksym_lookup() 和 KSYMS
表生成无关。
--exec-path 验证
使用以下方式完成 AArch64 构建:
scons --pyconfig-silent
--exec-path=/opt/rtthread-aarch64-none-elf/bin
scons -j2
--exec-path=/opt/rtthread-aarch64-none-elf/bin
构建过程确认:
LINK build/ksym/rtthread.pre1.elf
CC build/ksym/rtthread.pre1.o
LINK build/ksym/rtthread.pre2.elf
CC build/ksym/rtthread.final.o
LINK rtthread.elf
verify_final(["rtthread.elf"], ...)
代码检查
action: xxx当前拉取/合并请求的状态 Intent for your PR
必须选择一项 Choose one (Mandatory):
代码质量 Code Quality:
我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:
#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up