如何捕获内存写入并使用写入地址调用函数
我想捕获对特定内存范围的内存写入,并使用正在写入的内存位置的地址调用函数。最好是在写入存储器已经发生之后。
我知道操作系统可以通过调整页表条目来完成此操作。然而,如何在想要执行此操作的应用程序中实现类似的效果呢?
I would like to catch memory writes to specific memory ranges and call a function with the address of the memory location being written to. Preferably, after the write to memory has already happened.
I know this can be done by the operating system by twiddling with the page table entries. However, how can this be similar accomplished from within an application that wants to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,您可以执行如下操作:
输出(在 Windows XP 上运行):
这就是想法。
您可能需要进行一些更改,以使代码在多个线程中正常工作,使其与其他
SEH
代码(如果有)以及 C++ 异常(如果适用)一起工作。当然,如果你真的想要它,你可以让它在写入完成后调用写入监控回调函数。为此,您需要将
STATUS_ACCESS_VIOLATION
案例中的内存地址保存在某处(TLS
?),以便STATUS_SINGLE_STEP
案例可以拾取它稍后并传递给该函数。Well, you could do something like this:
Output (run on Windows XP):
That's the idea.
You will likely need to change things around to make the code work well in multiple threads, make it work with other
SEH
code (if any), with C++ exceptions (if applicable).And, of course, if you really want it, you can make it call the writes monitoring callback function after the write's been completed. For that you'll need to save the memory address from the
STATUS_ACCESS_VIOLATION
case somewhere (TLS
?) so that theSTATUS_SINGLE_STEP
case can pick it up later and pass to the function.或者,您可以使用Page Guards,这同样会导致访问异常,但会被系统自动清除(一次性)。这些也应该适用于只读内存。
在您的情况下,您仍然需要单步陷阱技巧来重新启用页面防护。
例如 vkTrace< 使用/a> 也可能由 OpenGL/Vulkan 持久映射缓冲区驱动程序实现本身实现。
vkTrace 源代码还展示了如何在 Linux 和 Android 上执行此类操作。
Alternatively you may use Page Guards which similarly cause an exception on access but are automatically cleared by the system (one-shot). Those should also work for read-only memory.
In your case you still need the single-step trap trick though to re-enable the page guard.
Used for example by vkTrace and potentially also by OpenGL/Vulkan Persistently Mapped Buffer driver implementations themselves.
vkTrace source code also shows how to do this kind of thing on Linux and Android.