如何在C ++代码中使用断点? [英] How do breakpoints work in C++ code?

查看:147
本文介绍了如何在C ++代码中使用断点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

断点如何在C ++代码中工作?当编译代码时,它们是在特定汇编器指令之间插入特殊指令吗?还是有别的地方?此外,如何实现分步实现代码?和断点一样...?

解决方案

这主要取决于CPU和调试器。



例如,x86 CPU上可能的解决方案之一:




  • 将一个字节的INT3指令插入

  • 比较例外地址到断点列表以确定哪一个

  • 执行断点操作

  • 将INT3替换为原始字节,并将调试的进程切换为跟踪模式(逐步执行CPU指令)

  • 继续调试过程

  • 立即捕获跟踪异常 - 指令已执行

  • 将INT3返回



    • 观察点可以以类似的方式实现,但是不是INT3,而是将观察变量所在的内存页设置为只读,或者不进入访问模式,并等待分段异常。



      也可以使用跟踪模式完成装配。根据调试数据,可以通过将断点放在下一条指令上来完成源线的跳过。



      此外,某些CPU具有硬件断点支持,注册。


      How do breakpoints work in C++ code? Are they special instructions inserted in between some assembler instructions when the code is compiled? Or is there something else in place? Also, how are stepping-through-the-code implemented? The same way as breakpoints...?

      解决方案

      This is heavly depend on the CPU and debugger.

      For example, one of the possible solution on x86 CPU:

      • Insert one-byte INT3 instruction on the required place
      • Wait until breakpoint exception hits
      • Compare exception address to the list of breakpoint to determine which one
      • Do breakpoint actions
      • Replace INT3 with original byte and switch the debugged process into trace mode (step-by-step execution of CPU instructions)
      • Continue debugged process
      • Immediately you catch trace exception - the instruction was executed
      • Put INT3 back

      Watchpoints can be implemented in the similar way, but instead of INT3 you put the memory page where watched variable is into read only, or into no access mode, and wait for segmentation exception.

      Stepping through assembly can also be done by using trace mode. Stepping through source lines can also be done by placing breakpoints onto next instructions, based on debug data.

      Also some CPU has hardware breakpoint support, when you just load address into some register.

      这篇关于如何在C ++代码中使用断点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆