美文网首页程序员我爱编程
Virturbox debugger & kgdb 双剑合璧

Virturbox debugger & kgdb 双剑合璧

作者: 独孤求真007 | 来源:发表于2018-04-12 12:23 被阅读0次

    通过上一篇的学习,我们已经可以用KGDB调试内核了,但是GDB并不能查看所有寄存器,比如IDTR. 本篇将介绍使用Virtualbox自带的调试器+kgdb双剑合璧,来学习内核。

    VirtualBox 内置的调试器默认是disable的,首先通过下面的命令开启VBoxDBG.

    # 获取Guest OS的Name和UUID
    root@ubuntu:~/vagrant# VBoxManage list vms
    "vagrant_default_1523049472371_88710" {bb455030-5669-4012-8dcb-65c6d0549c61}
    # Enable VBoxDBG
    root@ubuntu:~/vagrant/kernel# VBoxManage setextradata \
        "vagrant_default_1523049472371_88710" VBoxInternal/DBGC/Enabled 1
    

    现在利用Vagrant启动Guest Ubuntu:

    root@ubuntu:~/vagrant# vagrant up
    root@ubuntu:~/vagrant# vagrant ssh
    

    现在进入Guest Ubuntu使Guest kernel进入被调试状态:

    vagrant@ubuntu-xenial:~$ sudo sh -c 'echo g > /proc/sysrq-trigger'
    

    开另外一个窗口,用gdb连接到Guest内核:

    root@ubuntu:~/vagrant/obj/x86_64# gdb vmlinux
    GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
    Copyright (C) 2018 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    ...
    Type "apropos word" to search for commands related to "word"...
    Reading symbols from vmlinux...done.
    (gdb) target remote :1234
    Remote debugging using :1234
    kgdb_breakpoint () at /root/vagrant/linux/kernel/debug/debug_core.c:1073
    1073            wmb(); /* Sync point after breakpoint */
    (gdb)
    

    再开另外一个窗口,连接到VBoxDBG,VBoxDBG默认使用telnet协议监听在5000端口,

    root@ubuntu:~/vagrant/# telnet  localhost 5000
    Trying ::1...
    Trying 127.0.0.1...
    Connected to localhost.
    Escape character is '^]'.
    Welcome to the VirtualBox Debugger!
    Current VM is 79be5000, CPU #0
    

    现在我们要用DI来打印出中断向量表,不带参数会打印出256个中断向量,太多了,这里我们只查看14号中断向量,14号中断是缺页中断:

    VBoxDbg> help di
    di          [int [..]]                     Dump the interrupt descriptor table (IDT).
                 <0+ args>
        int          The interrupt vector or interrupt vector range. <optional+>
    
    VBoxDbg> di 0xe
    000e Int64  Sel:Off=0010:ffffffff818012c0     DPL=0 P  IST=0
    

    可以看到14号中断处理函数地址是: 0xffffffff818012c0,现在回到gdb:

    (gdb) info symbol 0xffffffff818012c0
    page_fault in section .text
    
    (gdb) info line page_fault
    Line 1158 of "/root/vagrant/linux/arch/x86/entry/entry_64.S" starts at address 0xffffffff81801273 <general_protection+3>
       and ends at 0xffffffff818012c3 <page_fault+3>.
    

    第一个命令显示地址0xffffffff818012c0位于代码段,符号名称为page_fault。
    第二个命令显示,page_fault位于arch/x86/entry/entry_64.S,第1158行。
    现在看看源代码吧:

    1158 idtentry general_protection     do_general_protection   has_error_code=1
    

    idtentry 是一个宏,定义在同一个文件中,它根据处理器spec进行中断进入和退出的预处理。

    901 .macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1
    902 ENTRY(\sym)
    903         UNWIND_HINT_IRET_REGS offset=\has_error_code*8
    ...
    950         call    \do_sym
    

    这个宏太啰嗦,不如看汇编代码来的方便:

    (gdb) disassemble page_fault
    Dump of assembler code for function page_fault:
       0xffffffff818012c0 <+0>:     nopl   (%rax)
       0xffffffff818012c3 <+3>:     testb  $0x3,0x10(%rsp)
       0xffffffff818012c8 <+8>:     jne    0xffffffff818012ea <page_fault+42>
       0xffffffff818012ca <+10>:    callq  0xffffffff818014a0 <error_entry>
       0xffffffff818012cf <+15>:    mov    %rsp,%rdi
       0xffffffff818012d2 <+18>:    mov    0x78(%rsp),%rsi
       0xffffffff818012d7 <+23>:    movq   $0xffffffffffffffff,0x78(%rsp)
       0xffffffff818012e0 <+32>:    callq  0xffffffff810566b0 <do_page_fault>
       0xffffffff818012e5 <+37>:    jmpq   0xffffffff81801590 <error_exit>
       0xffffffff818012ea <+42>:    callq  0xffffffff818014a0 <error_entry>
       0xffffffff818012ef <+47>:    mov    %rsp,%rdi
       0xffffffff818012f2 <+50>:    mov    0x78(%rsp),%rsi
       0xffffffff818012f7 <+55>:    movq   $0xffffffffffffffff,0x78(%rsp)
       0xffffffff81801300 <+64>:    callq  0xffffffff810566b0 <do_page_fault>
       0xffffffff81801305 <+69>:    jmpq   0xffffffff81801590 <error_exit>
    End of assembler dump.
    

    简单的说,就是进入中断处理函数之前调用error_entry,做一些准备工作,然后调用实际的中断处理函数do_page_fault, 然后再退出。

    (gdb) info line do_page_fault
    Line 1466 of "/root/vagrant/linux/arch/x86/mm/fault.c"
    

    看看实际 page fault处理函数吧。

    dotraplinkage void notrace
    do_page_fault(struct pt_regs *regs, unsigned long error_code)
    {
            unsigned long address = read_cr2(); /* Get the faulting address */
            enum ctx_state prev_state;
    
            prev_state = exception_enter();
            if (trace_pagefault_enabled())
                    trace_page_fault_entries(address, regs, error_code);
    
            __do_page_fault(regs, error_code, address);
            exception_exit(prev_state);
    }
    

    下一篇将介绍一个更加灵活的工具,GDB Python extension.
    参考:
    https://www.virtualbox.org/manual/

    相关文章

      网友评论

        本文标题:Virturbox debugger & kgdb 双剑合璧

        本文链接:https://www.haomeiwen.com/subject/csnghftx.html