Watch a memory location/install 'data breakpoint' from code?

心不动则不痛 提交于 2019-11-29 14:26:27

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.

You can still generate debug symbols for a "release" piece of code. This can still be run through a debugger just like you would in "debug" mode.

I recently did something similiar with one of our release drivers so that we could run it through vtune. For the Microsfot LINK, I added the -DEBUG flag, for Microsoft CC I added -Zi. Everything works fine. MSKB link

You might find this link useful.

assuming you're using windows use windbg to debug your program and check out the ba command-this will break when the memory is accessed.

There are tools for this - like heap agent and boundschecker and many others that will discover overwrites. Basically you need some sentinels at the end of your memory allocations and they need to be checked.

Debugging APIs are platform-specific, but they do exist. Windows and UNIX APIs can be found online.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!