简体   繁体   English

如何在 CDB (WinDbg) 中的变量地址上设置数据断点

[英]How to set a data breakpoint on a variable address in CDB (WinDbg)

class Test
{
public:
    Test() 
    { 
        std::cout << "ctor" << std::endl; 
        ptr = new int(5);
        *(ptr + 1) = 42;
    };
    ~Test() { std::cout << "dtor" << std::endl; };

    int* ptr;
};

void foo()
{
    Test test;
}

int main()
{
    foo();
    return 0;
}

In the above example I want to set a data breakpoint at the address pointed to by ptr plus an offset (4 bytes in this case), to detect the heap corruption.在上面的示例中,我想在ptr指向的地址加上一个偏移量(在这种情况下为 4 个字节)设置一个数据断点,以检测堆损坏。

After I break inside the Test constructor, I have tried using the following to set the breakpoint but have failed:在 Test 构造函数内部中断后,我尝试使用以下方法设置断点,但失败了:

0:000> ba w 4 (ptr + 4)
Bp expression '(ptr + 4)' could not be resolved, adding deferred bp
*** Bp expression '(ptr + 4)' contains symbols not qualified with module name

If I put the address manually then it obviously works.如果我手动输入地址,那么它显然可以工作。 But I don't want to hard code the address in the command because I am doing this as part of a script and the address inside ptr can change every time.但是我不想在命令中硬编码地址,因为我是作为脚本的一部分执行此操作的,并且ptr中的地址每次都可以更改。

What is the correct syntax for adding the data breakpoint without hardcoding the address?在不硬编码地址的情况下添加数据断点的正确语法是什么?

You need @@c++() or .expr /s c++ :您需要@@c++().expr /s c++

0:000> bp MemoryBreak!Test::Test

0:000> g
Breakpoint 2 hit
MemoryBreak!Test::Test:
[...]

0:000> l+t
Source options are 1:
     1/t - Step/trace by source line
0:000> p
MemoryBreak!Test::Test+0x42:

0:000> ? @@c++(ptr)
Evaluate expression: 2790386541360 = 00000289`afffa330

0:000> ba w 4 @@c++(ptr)+4
0:000> bl
     1 e Disable Clear  00007ff7`5fac2720  [C:\....cpp @ 23]  0001 (0001)  0:**** MemoryBreak!main
     2 e Disable Clear  00007ff7`5fac1fa0  [C:\....cpp @ 6]  0001 (0001)  0:**** MemoryBreak!Test::Test
     3 e Disable Clear  00000289`afffa334 w 4 0001 (0001)  0:**** 

0:000> g
Breakpoint 3 hit
MemoryBreak!Test::Test+0xa7:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM