观察内存位置/从代码安装“数据断点”? [英] Watch a memory location/install 'data breakpoint' from code?

查看:148
本文介绍了观察内存位置/从代码安装“数据断点”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个内存覆盖问题。在某些时候,在我们的程序的过程中,一个内存位置正在被覆盖,并导致我们的程序崩溃。该问题仅在释放模式下发生。当在调试,一切都很好。
是一个经典的C / C ++错误,而且很难找到。

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.

推荐答案

如果您可以控制变量的位置,那么您可以在专用页面上分配它,并将页面的权限设置为只允许使用 VirtualProtect (在Windows上不能确定Linux)。

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屋!

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