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

编程入门 行业动态 更新时间:2024-10-26 16:30:47
观看内存位置/从代码安装“数据断点”?(Watch a memory location/install 'data breakpoint' from code?)

我们有一个内存覆盖问题。 在某些时候,在我们的程序过程中,内存位置被覆盖并导致我们的程序崩溃。 该问题仅在发布模式下发生。 在调试时,一切都很好。 这是一个经典的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.

更多推荐

本文发布于:2023-08-03 15:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1394537.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:断点   内存   位置   代码   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!