visual-studio 当内存位置的内容改变或被读取时自动中断

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1261198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:51:34  来源:igfitidea点击:

Automatic break when contents of a memory location changes or is read

c++visual-studiodebugging

提问by Matt Price

The old DEC Tru64 UNIX debugger had a feature (called "watchpoints to monitor variables") that would watch a memory location (or range of addresses) for read or write activity and when it detected such activity would break the program so you could investigate why. See for details:

旧的 DEC Tru64 UNIX 调试器有一个功能(称为“监视变量的观察点”),可以监视内存位置(或地址范围)的读或写活动,当检测到此类活动时会破坏程序,因此您可以调查原因. 详情请参阅:

http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/V50_HTML/ARH9QATE/DOCU_009.HTM

http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/V50_HTML/ARH9QATE/DOCU_009.HTM

Is there any way to do this sort of thing in the VisualStudio debugger? Or is there an add-on or some other tool that can do this under Windows?

有没有办法在 VisualStudio 调试器中做这种事情?或者是否有可以在 Windows 下执行此操作的附加组件或其他工具?

回答by Matt Price

Yeah, you can do this in visual studio. You can create a "New Data Breakpoint" under the debug menu while you're broken in a running program. You then specify the address to watch and the number of bytes.

是的,您可以在 Visual Studio 中执行此操作。当您在运行的程序中出现故障时,您可以在调试菜单下创建一个“新数据断点”。然后指定要监视的地址和字节数。

This only works for changing the value. I don't know how to watch for read access. However it's a very common question to want to know where a value got changed. I find that I don't want to know who reads a value as often.

这仅适用于更改值。我不知道如何观看读取访问。然而,想知道值在哪里改变是一个非常常见的问题。我发现我不想知道谁经常读取值。

回答by Kirill V. Lyadvinsky

Visual Studio allows to setbreakpoints on memory location only 4 bytes length (on 32-bit Windows version). To catch memory access (read or write) you could use the following class:

Visual Studio允许在仅 4 字节长度的内存位置上设置断点(在 32 位 Windows 版本上)。要捕获内存访问(读取或写入),您可以使用以下类:

struct protect_mem_t {
    protect_mem_t( void* addr, size_t size ) : addr(addr), size(size), is_protected(FALSE) { 
        protect(); 
    }
    ~protect_mem_t() { release(); }
    BOOL protect() { 
        if ( !is_protected ) {
            // To catch only read access you should change PAGE_NOACCESS to PAGE_READONLY
            is_protected = VirtualProtect( addr, size, PAGE_NOACCESS, &old_protect );
        }
        return is_protected;
    }
    BOOL release() { 
        if ( is_protected ) 
            is_protected = !VirtualProtect( addr, size, old_protect, &old_protect );
        return !is_protected;
    }

protected:
    void*   addr;
    size_t  size;
    BOOL    is_protected;
    DWORD   old_protect;
};

It changes access mode on selected memory pages. Page size is equal to 4096 bytes on 32-bit systems. Exception will be thrown on every access to protected memory. This class is limited in use to only large memory areas, but I hope it can be helpful.

它更改选定内存页的访问模式。页大小在 32 位系统上等于 4096 字节。每次访问受保护内存时都会抛出异常。这个类仅限于大内存区域的使用,但我希望它可以有所帮助。

It could be used in the following way:

它可以通过以下方式使用:

// some_array should be aligned on PAGE_SIZE boundaries
protect_mem_t guard( &some_array, PAGE_SIZE );

回答by steve

You can use WINDBG and set a ba breakpoint on the desired address

您可以使用 WINDBG 并在所需地址上设置 ba 断点

回答by ulatekh

I recommend the hwbreak project. It even allows a data-breakpoint to be set when a location is read.

我推荐hwbreak 项目。它甚至允许在读取位置时设置数据断点。

I modified it to only create one thread, and to reuse that thread for all data-breakpoints, but that was just for efficiency.

我修改它只创建一个线程,并为所有数据断点重用该线程,但这只是为了提高效率。

回答by MSalters

Yes, data breakpoints can detect writes. Don't know if it's possible to check for read, though. I don't believe x86 has native support for that.

是的,数据断点可以检测写入。不过,不知道是否可以检查读取。我不相信 x86 对此有本机支持。