LLDB调试Apple ARM64汇编时无法切换被包含文件源码显示
Apple ARM64汇编调试:.include引入文件无法正确显示的解决方法
问题现象
调试Apple ARM64架构的汇编程序时,将代码拆分至多个文件并通过.include指令引入分支逻辑,LLDB单步进入分支时能定位到正确的行号,但始终显示主文件example.s的内容,而非分支所在的exit.s文件。
最小复现示例
example.s
.align 4 .global _start _start: mov X0, #0 // exit code b exit .include "exit.s"
exit.s
.align 4 exit: mov X16, #1 // exit system call svc #0 // call system call
原编译调试流程及异常表现
as example.s -g -o example.o ld example.o -o example -l System -syslibroot `xcrun -sdk macosx --show-sdk-path` -e _start -arch arm64 lldb example (lldb) target create "example" Current executable set to '/example' (arm64). (lldb) b start Breakpoint 1: where = example`start + 4, address = 0x0000000100003f94 (lldb) run Process 61477 launched: '/example' (arm64) Process 61477 stopped * thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1 frame #0: 0x0000000100003f94 example`start at example.s:6 3 4 _start: 5 mov X0, #0 // exit code -> 6 b exit 7 8 .include "exit.s" 9 Target 0: (example) stopped. warning: This version of LLDB has no plugin for the language "assembler". Inspection of frame variables will be limited. (lldb) stepi Process 61477 stopped * thread #1, queue = 'com.apple.main-thread', stop reason = instruction step into frame #0: 0x0000000100003fa0 example`exit at example.s:4 1 .align 4 2 .global _start 3 -> 4 _start: 5 mov X0, #0 // exit code 6 b exit 7 Target 0: (example) stopped.
解决方法
.include是汇编预处理器的文本级插入操作,汇编器会将exit.s的内容直接合并到example.s中生成目标文件,导致所有调试信息都关联到example.s,LLDB无法区分代码来源。正确的做法是单独汇编每个源文件,再通过链接器合并:
- 修改
example.s,移除.include并声明exit为外部符号:
.align 4 .global _start .extern exit // 声明exit是外部定义的符号 _start: mov X0, #0 // exit code b exit
- 单独汇编每个源文件,保留调试信息:
as -g example.s -o example.o as -g exit.s -o exit.o
- 链接两个目标文件生成可执行程序:
ld example.o exit.o -o example -l System -syslibroot `xcrun -sdk macosx --show-sdk-path` -e _start -arch arm64
此时再用LLDB调试,执行stepi进入exit分支时,就会正确显示exit.s的内容和对应行号。
原理说明
单独汇编每个文件时,每个目标文件(.o)都会携带自身的调试信息,包含源文件路径、行号与机器指令的映射关系。链接器合并目标文件时会保留这些调试信息,LLDB就能根据指令地址正确关联到对应的源文件,而非全部映射到主文件。
内容的提问来源于stack exchange,提问作者maxcnunes
相关产品推荐
相关产品推荐

