从代码观看内存位置/安装“数据断点”? [英] Watch a memory location/install 'data breakpoint' from code?
问题描述
这是一个经典的C / C ++错误,并且非常难找到。
我想知道是否有办法添加一些调试代码将会看到这个内存位置,一旦更改就会调用回调函数。这基本上是调试器在调试模式下可以做的(数据断点),但是我们需要类似的版本。
如果您可以控制变量的位置,那么您可以将它分配到专用页面上,并设置页面的权限,以允许只读使用 VirtualProtect (在Windows上...不确定为Linux)。
当有人尝试写入时,您将获得访问冲突的方式。使用异常翻译功能,您可以将其视为回调。
即使您无法直接移动变量(例如,它是一个类成员),也许您可以在变量周围添加足够的填充,以确保它在专用页面上并使用相同的方法。
We have a memory overwrite problem. At some point, during the course of our program, a memory location is being overwritten and causing our program to crash. the problem happens only in release mode. when in debug, all is well. that is a classic C/C++ bug, and a very hard one to locate.
I wondered if there's a way to add some 'debugging code' that will watch this memory location and will call a callback once its changed. This is basically what a debugger could do in debug mode (a 'data breakpoint') but we need something similar in release.
If you can control the location of the variable then you can allocate it on a dedicated page and set the permissions of the page to allow reads only using VirtualProtect (on Windows ... not sure for Linux).
This way you will get an access violation when someone tries to write to it. With an exception translator function you could treat this as a callback.
Even if you can't move the variable directly (eg. it is a class member), maybe you could add sufficient padding around the variable to ensure it is on a dedicated page and use the same approach.
这篇关于从代码观看内存位置/安装“数据断点”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!