zoukankan      html  css  js  c++  java
  • 80863使用bochs调试汇编程序

    使用bochs调试汇编程序

    前面我们已经搭建好了bochs的环境,并且将我们的汇编程序写入了硬盘里面,现在我们来看看如何通过bochs来调试我们的程序。

    前文:https://www.cnblogs.com/Sna1lGo/p/15695712.html

    bochs安装文件夹

    安装完bochs后,在安装目录下我们可以看到有两个.exe可执行程序:

    bochs.exe就和正常的安装的虚拟机一样了,就是直接运行了。

    而bochsdbg.exe才是我们要的可调试的虚拟机。

    同时可以看到这个文件夹下面有一个docs文件夹,里面包含了bocsh的一些手册,感兴趣的可以读一下。

    进行bochsdbg调试:

    首先要启动bochsdbg虚拟机:

    然后得添加配置文件,前面的章节我们是添加了配置的,但是如果你没保存,那么很不幸你得再来一次了,需要注意的是保存的配置文件千万不要修改配置文件的名字。这里由于我保存了的我就直接start了。

    加载后是这样一个界面:

    左边的display就类似于显示器了,然后右边的就是命令行调试窗口,启动后命令行调试窗口前面显示的是一系列虚拟机的状态信息。

    bochs会在执行第一条指令的时候停下来,等待我们的操作,也就是这里的命令行调试界面:

    bochs的调试指令可以看bochs的官方文档上看到:

    The Bochs internal debugger (sourceforge.io)

    当然它也包含在了我们安装bochs文件夹的docs文件夹下面。

    Next at t=0
    (0) [0x0000fffffff0] f000:fff0 (unk. ctxt): jmpf 0xf000:e05b         ; ea5be000f0

    0x0000fffffff0 代表真实的物理地址

    f000:fff0 是逻辑地址

    jmpf 0xf000:e05b 是汇编指令

    ea5be000f0 是机器指令

    Next at t=0 中的t是执行的指令个数,也叫做内部时钟

    注:细心的朋友可能会发现,这里的f000:fff0的逻辑运算是无法计算得到0x0000fffffff0地址的,这个只会在处理器刚启动的时候发生,然后后面会解释,这里先暂时就这个样子。

    肯定有同学会奇怪啊,前面我们说了8086启动的第一条指令是jmp FFFF:0000啊,这里怎么不一样,那说明这个cpu不是8086呀。哈哈。

    我们把这个调试界面往前翻一下,查看前面的状态信息可以看到:

    这里的CPU configuration也就是CPU的配置信息是x86-64的,是intel的支持x86的CPU并不是8086CPU。

     

    这里我们可以先单步执行看看内容,这里是所有的执行指令:

      c                           continue executing
    cont
    continue

    s     [count]               execute count instructions, default is 1
    step [count]

    s     [cpu] [count]         for SMP simulation, execute count instructions on cpu, default is 1
    step [cpu] [count]

    s     all [count]           for SMP simulation, execute count instructions on all cpus
    step all [count]

    Ctrl-C                      stop execution, and return to command line prompt
    Ctrl-D                      if at empty line on command line, exit

    q                           quit debugger and execution
    quit
     exit

    可以采用s来单步执行,默认是只执行一条,就到了这里:

    可以看到这个地址区域,是满足前面我们将的cpu地址分区的BIOS地址区域的在F0000~FFFFF之间。

    那么如果我们想直接到我们写的主引导扇区里面怎么办呢,我们可以给主引导扇区的第一条代码打一个断点,这里是所有的断点指令:

    NOTE: The format of 'seg', 'off', and 'addr' in these descriptions,
          are as follows. I don't have any way to set the current radix.

          hexidecimal:   0xcdef0123
          decimal:        123456789
          octal:          01234567

    vbreak seg:off             Set a virtual address instruction breakpoint
    vb     seg:off

    vbreak seg:off if "expr"   Set a conditional virtual address instruction breakpoint
    vb     seg:off if "expr"

    lbreak addr                 Set a linear address instruction breakpoint
    lb     addr

    lbreak addr if "expr"       Set a conditional linear address instruction breakpoint
    lb     addr if "expr"

    pbreak [*] addr             Set a physical address instruction breakpoint
    pb     [*] addr             (the '*' is optional for GDB compatibility)
    break [*] addr
    b     [*] addr

    pbreak [*] addr if "expr"   Set a conditional physical address instruction breakpoint
    pb     [*] addr if "expr"   (the '*' is optional for GDB compatibility)
    break [*] addr if "expr"
    b     [*] addr if "expr"

    info break                 Display state of all current breakpoints
    bpe   n                   Enable a breakpoint
    bpd   n                   Disable a breakpoint
    delete n                   Delete a breakpoint
    del   n
    d     n

    那么打断点肯定是不能像vs调试一样直接给代码打上断点然后直接调试就完事了,在cpu的层面是直接给地址打断点,执行到该处就直接停下来。前面的博客我们学习到,CPU会把主引导扇区的内容加载到内存的 0000:7c00处,那么我们直接给这个地址打个断点然后运行到这个地址就停下来不就行了吗。

    b 0x7c00

    解释一下这条命令,上面我写了所有的断点指令,然后单独的 b 后面是跟着addr,然后逻辑地址的0000:7c00就是物理地址的 7c00,所以我这样使用是没问题的。

    然后我们可以输入执行指令,来直接执行到设置的断点处:

    这里就是我们前面写好的汇编代码了。

    然后采用s指令单步调试来把我们的代码都执行完:

    好这里就执行完了,然后可以查看一下寄存器,看看ax,dx是不是我们设想的内容。

    以下是查看bochs虚拟机信息的所有指令:

      r|reg|regs|registers         List of CPU integer registers and their contents
    fp|fpu                       List of all FPU registers and their contents
    mmx                         List of all MMX registers and their contents
    sse|xmm                     List of all SSE registers and their contents
    ymm                         List of all AVX registers and their contents
    sreg                         Show segment registers and their contents
    dreg                         Show debug registers and their contents
    creg                         Show control registers and their contents

    info cpu                     List of all CPU registers and their contents
    info eflags                 Show decoded EFLAGS register
    info break                   Information about current breakpoint status
    info tab                     Show paging address translation
    info device                 Show state of the specified device

    可以看到ax和dx确实是我们想要的内容,0x00F0和0x00c0。

    这样就很完美了。

     

    由于我们这次写的汇编代码很简单,所以就这么几条就执行完了。

    可以使用退出指令来直接退出该bochs虚拟机了:

    q                           quit debugger and execution
    quit
    exit

    这样摁完Enter回车就可以美美哒结束了。

    小结

    采用原原本本的通过cpu来执行汇编代码的方式虽然比较繁琐,但是逼格很高,哈哈,至少我是这样认为的。

    总结一下流程:

    先写好汇编代码,然后编译,编译好之后,和512个字节进行比对,不够的要填充,填充的时候注意最后两个字节的内容必须为55和AA,然后将其写入硬盘里。再启动bochs虚拟机给系统主引导扇区的地址打断点,然后执行continue运行,会停止到断点处,就可以进行对我们自己的汇编语言代码进行调试了。而且是绕过了Windows\linux这样的大型操作系统,这多酷。然后如果调试遇到指令问题就查看bochs的文档就可以了。

  • 相关阅读:
    VScode快捷键:单行注释和多行注释
    常见状态码的含义
    2019年10月22日 文件操作复习
    2019年10月7日 函数复习
    2019年10月4日 元类
    2019年10月2日 property补充
    2019年10月1日 实现延迟计算功能
    2019年9月30日 property流程分析
    2019年9月29日 自定制property
    2019年9月23日 类的装饰器的应用
  • 原文地址:https://www.cnblogs.com/Sna1lGo/p/15704154.html
Copyright © 2011-2022 走看看