在gdb中,当我在内核代码中,bt只有内核代码,那么我该怎么知道用户指令执行到了哪里,才陷入内核的 pwndbg> c Continuing. Breakpoint 3,…
- User
在gdb中,当我在内核代码中,bt只有内核代码,那么我该怎么知道用户指令执行到了哪里,才陷入内核的
pwndbg> c Continuing.
Breakpoint 3, handle_page_fault (address=140737352260032, error_code=20, regs=0xffffc90000177f58) at arch/x86/mm/fault.c:1478 1478 if (unlikely(fault_in_kernel_space(address))) { Permission error when attempting to parse page tables with gdb-pt-dump. Either change the kernel-vmmap setting, re-run GDB as root, or disable
ptrace_scope(echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope) Exception ignored in: <function QemuGdbMachine.del at 0x7f3eb8239a80> Traceback (most recent call last): File "/mnt/home/tangr/pwndbg/.venv/lib/python3.12/site-packages/pt_gdb/pt_gdb.py", line 19, in del if self.file: ^^^^^^^^^ AttributeError: 'QemuGdbMachine' object has no attribute 'file' Permission error when attempting to parse page tables with gdb-pt-dump. Either change the kernel-vmmap setting, re-run GDB as root, or disableptrace_scope(echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope) Exception ignored in: <function QemuGdbMachine.del at 0x7f3eb8239a80> Traceback (most recent call last): File "/mnt/home/tangr/pwndbg/.venv/lib/python3.12/site-packages/pt_gdb/pt_gdb.py", line 19, in del if self.file: ^^^^^^^^^ AttributeError: 'QemuGdbMachine' object has no attribute 'file' Permission error when attempting to parse page tables with gdb-pt-dump. Either change the kernel-vmmap setting, re-run GDB as root, or disableptrace_scope(echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope) Exception ignored in: <function QemuGdbMachine.del at 0x7f3eb8239a80> Traceback (most recent call last): File "/mnt/home/tangr/pwndbg/.venv/lib/python3.12/site-packages/pt_gdb/pt_gdb.py", line 19, in del if self.file: ^^^^^^^^^ AttributeError: 'QemuGdbMachine' object has no attribute 'file' Permission error when attempting to parse page tables with gdb-pt-dump. Either change the kernel-vmmap setting, re-run GDB as root, or disableptrace_scope(echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope) Exception ignored in: <function QemuGdbMachine.del at 0x7f3eb8239a80> Traceback (most recent call last): File "/mnt/home/tangr/pwndbg/.venv/lib/python3.12/site-packages/pt_gdb/pt_gdb.py", line 19, in del if self.file: ^^^^^^^^^ AttributeError: 'QemuGdbMachine' object has no attribute 'file' LEGEND: STACK | HEAP | CODE | DATA | RWX | RODATA ─────────────────────────────────────────────[ REGISTERS / show-flags off / show-compact-regs off ]────────────────────────────────────────────── *RAX 0 *RBX 0xffffc90000177f58 —▸ 0x403e00 (__do_global_dtors_aux_fini_array_entry) —▸ 0x401160 (__do_global_dtors_aux) ◂— 0x2ec53d80fa1e0ff3 *RCX 0 *RDX 0 *RDI 0xffffc90000177f58 —▸ 0x403e00 (__do_global_dtors_aux_fini_array_entry) —▸ 0x401160 (__do_global_dtors_aux) ◂— 0x2ec53d80fa1e0ff3 *RSI 0x14 R8 0 *R9 0 *R10 0 *R11 0 *R12 0x14 *R13 0x7ffff7e359c0 *R14 0 *R15 0 '>@' */ *RSP 0xffffc90000177f28 ◂— 0 *RIP 0xffffffff817d9054 (exc_page_fault+68) ◂— 0xfff0002548e8894c ──────────────────────────────────────────────────────[ DISASM / x86-64 / set emulate on ]─────────────────────────────────────────────────────── ► 0xffffffff817d9054 <exc_page_fault+68> mov rax, r13 RAX => 0x7ffff7e359c0 0xffffffff817d9057 <exc_page_fault+71> and rax, 0xfffffffffffff000 0xffffffff817d905d <exc_page_fault+77> cmp rax, -0xa00000 0xffffffff817d9063 <exc_page_fault+83> je exc_page_fault+100 <exc_page_fault+100> - Assistant
简短说法:看 pt_regs 里的用户态寄存器就行。异常进内核时,CPU把用户态的 RIP/RSP 等全塞进了 struct pt_regs,内核把它当成 regs 传给了 handle_page_fault。用它就能还原“用户态执行到哪儿才陷进来的”。
怎么做(在你这个现场):
- 在 handle_page_fault 这一帧里直接看用户态 RIP:
- p/x regs->ip
- x/10i (void*)regs->ip # 反汇编看看是哪条用户态指令
- 看用户态栈:
- p/x regs->sp
- x/24gx (void*)regs->sp
- 看 fault 的线性地址(也就是 CR2;page fault 到底访问了哪个地址):
- p/x $cr2
- 或者就用函数参数 address(它通常就是 CR2,可能按页对齐)
- 判断是不是用户态触发的:
- p/x regs->cs # 低两位为 3 就是从用户态来的
- 你这儿 error_code=0x14(二进制 10100)= 用户态 + 指令取指 fault(IFetch),而且 P=0 表示页不在
顺便用你贴出来的寄存器对一下:
- R13 是内核里临时放的 fault 地址(CR2),现在是 0x7ffff7e359c0,落在 ld.so 附近。
- 真正“当时的用户态指令地址”是 regs->ip,直接 p/x regs->ip 就能拿到,然后 x/i 看具体指令。
- 在 handle_page_fault 这一帧里直接看用户态 RIP:
- User
我用ftrace跟踪,并没有调用到handle_page_fault,不过用gdb跟踪,会调用到handle_page_fault,为什么ftrace不显式
- Assistant
因为你用的是 ftrace 的“函数跟踪”,而页故障这条路径里很多关键函数被标了 notrace/noinstr/NOKPROBE,ftrace 根本不会给它们打钩子,所以你在 trace 里看不到它们;但 gdb 是靠符号和调试信息单步/断点的,和是否可被 ftrace 插桩没关系,所以 gdb 能看到 handle_page_fault 正常被调用。
更具体点:
- 异常入口 asm_exc_page_fault/exc_page_fault 属于异常/硬中断入口路径,这段代码大量使用 noinstr/notrace,避免任何插桩(防递归、不可抢占、RCU/锁dep/KASAN 等约束)。
- 在不少内核版本里,handle_page_fault 以及紧邻的 do_kern_addr_fault/do_user_addr_fault 也可能被标成 notrace 或 NOKPROBE(防止 kprobe/ftrace 落在异常处理最热路径上)。一旦是这样,它就不会出现在 /sys/kernel/debug/tracing/available_filter_functions 里,自然也不会出现在 function/function_graph 的 trace。
你可以快速验证:
- 如果 grep -w handle_page_fault /sys/kernel/debug/tracing/available_filter_functions 没输出,那它就是不可被 ftrace 函数跟踪的(被 notrace/编译期剔除了)。
- 在 /proc/kallsyms 里能看到符号,但这不代表能被 ftrace 插桩。
想追这条路径,有几种替代方式:
- 开 exceptions 里的 tracepoint 事件(推荐,最省心):
- echo 1 > /sys/kernel/debug/tracing/events/exceptions/page_fault_user/enable
- echo 1 > /sys/kernel/debug/tracing/events/exceptions/page_fault_kernel/enable 这些事件会直接给你 ip、address(CR2)、error_code 等关键信息,比函数跟踪更贴合“页故障发生了什么”。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.