Skip to content
TheChatArchiveThe Chat Archive
gpt-5-high-new-system-prompt· OpenAIHugging Face Arena Expert 5K

在gdb中,当我在内核代码中,bt只有内核代码,那么我该怎么知道用户指令执行到了哪里,才陷入内核的 pwndbg> c Continuing. Breakpoint 3,…

  1. 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 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 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 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' 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 RBP 0xffffc90000177f48 —▸ 0xffffc90000177f59 ◂— 0x403e / '>@' */ *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>

    0xffffffff817d9065 <exc_page_fault+85> movabs rax, 0x7ffffffff000 RAX => 0x7ffffffff000 0xffffffff817d906f <exc_page_fault+95> cmp r13, rax 0xffffffff817d9072 <exc_page_fault+98> jae exc_page_fault+153 <exc_page_fault+153>

    0xffffffff817d9074 <exc_page_fault+100> mov rdx, r13 0xffffffff817d9077 <exc_page_fault+103> mov rsi, r12 0xffffffff817d907a <exc_page_fault+106> mov rdi, rbx 0xffffffff817d907d <exc_page_fault+109> call do_user_addr_fault <do_user_addr_fault> ────────────────────────────────────────────────────────────────[ SOURCE (CODE) ]──────────────────────────────────────────────────────────────── In file: /mnt/home/tangr/buildroot/output/build/linux-6.12.9/arch/x86/mm/fault.c:1478 1473 1474 if (unlikely(kmmio_fault(regs, address))) 1475 return; 1476 1477 /* Was the fault on kernel-controlled part of the address space? / ► 1478 if (unlikely(fault_in_kernel_space(address))) { 1479 do_kern_addr_fault(regs, error_code, address); 1480 } else { 1481 do_user_addr_fault(regs, error_code, address); 1482 / 1483 * User address page fault handling might have reenabled ────────────────────────────────────────────────────────────────────[ STACK ]──────────────────────────────────────────────────────────────────── 00:0000│ rsp 0xffffc90000177f28 ◂— 0 ... ↓ 3 skipped 04:0020│ rbp 0xffffc90000177f48 —▸ 0xffffc90000177f59 ◂— 0x403e /* '>@' */ 05:0028│+008 0xffffc90000177f50 —▸ 0xffffffff8180134b (asm_exc_page_fault+43) ◂— 0x1e0ff300000850e9 06:0030│ rbx rdi 0xffffc90000177f58 —▸ 0x403e00 (__do_global_dtors_aux_fini_array_entry) —▸ 0x401160 (__do_global_dtors_aux) ◂— 0x2ec53d80fa1e0ff3 07:0038│+018 0xffffc90000177f60 —▸ 0x7ffff7ffd000 (_rtld_local) —▸ 0x7ffff7ffe310 ◂— 0 ──────────────────────────────────────────────────────────────────[ BACKTRACE ]────────────────────────────────────────────────────────────────── ► 0 0xffffffff817d9054 exc_page_fault+68 1 0xffffffff817d9054 exc_page_fault+68 2 0xffffffff8180134b asm_exc_page_fault+43 3 0x403e00 __do_global_dtors_aux_fini_array_entry 4 0x7ffff7ffd000 _rtld_local 5 0x7fffffffed68 6 0x0 fixed_percpu_data ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── pwndbg> bt #0 handle_page_fault (address=140737352260032, error_code=20, regs=0xffffc90000177f58) at arch/x86/mm/fault.c:1478 #1 exc_page_fault (regs=0xffffc90000177f58, error_code=20) at arch/x86/mm/fault.c:1539 #2 0xffffffff8180134b in asm_exc_page_fault () at ./arch/x86/include/asm/idtentry.h:623 #3 0x0000000000403e00 in __frame_dummy_init_array_entry () #4 0x00007ffff7ffd000 in ?? () #5 0x00007fffffffed68 in ?? () #6 0x0000000000000000 in ?? ()

  2. 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 看具体指令。

    如果还想把它对应回用户程序/符号名:

    • 没加载用户态符号时只能看到裸地址。要看到函数名:
      • 关闭 ASLR(echo 0 | sudo tee /proc/sys/kernel/randomize_va_space 或内核 cmdline randomize_va_space=0),或者
      • 查出该进程中对应 ELF 的加载基址(/proc//maps),然后在 gdb 里 add-symbol-file /path/to/bin 0x基址,再 disas/whereis。
      • 动态库(glibc/ld.so)同理,给每个需要的 so 做一次 add-symbol-file。
    • 在 QEMU+vmlinux 的远程 gdb 场景下,一般可以直接 x/i (void*)regs->ip 读到用户页(CR3 指向当前进程),除非 KPTI/权限等阻止。

    关于 pwndbg 的那串 “Permission error when attempting to parse page tables with gdb-pt-dump”:

    • 这是 pwndbg 的辅助插件想在宿主机上解析页表失败,不影响你从 pt_regs 拿用户态 RIP/SP。要安静点可以:
      • 用 root 跑 gdb,或
      • echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope,或
      • 在 pwndbg 里禁用相关插件/功能(如 vmmap 的 pt 解析)。

    小结:

    • 用户态“卡在哪条指令”= regs->ip
    • 访问了哪个地址= CR2($cr2 或 handle_page_fault 的 address)
    • 是否取指/读/写、用户/内核= error_code 位 + regs->cs
    • 需要符号名就 add-symbol-file 对齐基址后再 x/i regs->ip

    需要的话你把 p/x regs->ip 的结果贴一下,我帮你一起把它映射回具体的用户代码位置。

  3. User

    我用ftrace跟踪,并没有调用到handle_page_fault,不过用gdb跟踪,会调用到handle_page_fault,为什么ftrace不显式

  4. 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 等关键信息,比函数跟踪更贴合“页故障发生了什么”。
    • 如果 handle_page_fault 被标了 NOKPROBE,试试更深/更外层、没被屏蔽的函数,比如 mm/ 里的 handle_mm_fault、do_user_addr_fault(是否可探针依版本/配置而定)。用:
      • kprobe:echo 'p:fault do_user_addr_fault' > /sys/kernel/debug/tracing/kprobe_events || echo 失败说明被 NOKPROBE
      • 或 bpftrace/perf probe(同样受 NOKPROBE 限制)
    • 你也可以只用 gdb 从 pt_regs 拿用户态 ip/栈来还原现场,这一点不依赖 ftrace。

    小结:

    • gdb 能看到,是因为调试层面不受“不可插桩”限制;
    • ftrace 看不到,是因为该函数或整条异常路径被 notrace/noinstr/NOKPROBE 屏蔽;
    • 用 exceptions:page_fault_* 这些 tracepoint,或者换到没被屏蔽的下游函数,是更稳的做法。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.