IT

Visual Studio 디버거를 사용하여 값이 변경되면 중단

lottoking 2020. 5. 16. 10:28
반응형

Visual Studio 디버거를 사용하여 값이 변경되면 중단


변수에 시계를 배치하고 해당 값이 변경 될 때만 Visual Studio가 중단되는 방법이 있습니까?

까다로운 상태 문제를 훨씬 쉽게 찾을 수 있습니다.

이것을 할 수 있습니까?

중단 점 조건에는 여전히 중단 점 세트가 필요하며 시계를 설정하고 Visual Studio에서 상태 변경시 중단 점을 설정하도록합니다.


Visual Studio 2005 메뉴에서 :

디버그 -> 새로운 중단 점 -> 새로운 데이터 중단 점

시작하다:

&myVariable

코드에서 명시 적으로 중단하도록 선택할 수도 있습니다.

// Assuming C#
if (condition)
{
    System.Diagnostics.Debugger.Break();
}

MSDN에서 :

디버거 브레이크 : 디버거가 연결되어 있지 않으면 사용자에게 디버거를 연결할지 묻는 메시지가 표시됩니다. 그렇다면 디버거가 시작됩니다. 디버거가 연결되어 있으면 디버거에 사용자 중단 점 이벤트가 표시되고 디버거는 마치 디버거 중단 점에 도달 한 것처럼 프로세스 실행을 일시 중단합니다.

그러나 이것은 단지 폴백입니다. 다른 의견에서 설명한 것처럼 Visual Studio에서 조건부 중단 점을 설정하는 것이 더 좋습니다.


정말 오래된 게시물이지만 누군가 알지 못하는 경우 ...

에서 비주얼 스튜디오 2015 , 당신은에 중단 점을 배치 할 수 있습니다 set자동 구현 속성의 접근 및 디버거는 속성이 업데이트 될 때 중단됩니다

public bool IsUpdated
{
    get;
    set;    //set breakpoint on this line
}

최신 정보

대안 적으로; @AbdulRaufMujahid은 자동 구현 속성은 한 줄에있는 경우, 당신은 위치에 커서를 할 수 있다는 의견에서 지적 get;이나 set;와 충돌 F9및 중단 점을 따라 배치됩니다. 좋은!

public bool IsUpdated { get; set; }

다음 선언을 가진 A라는 클래스가 있다고 가정하십시오.

class A  
{  
    public:  
        A();

    private:
        int m_value;
};

누군가 "m_value"값을 수정할 때 프로그램을 중지하려고합니다.

클래스 정의로 이동하여 A의 생성자에 중단 점을 두십시오.

A::A()
{
    ... // set breakpoint here
}

프로그램을 중지하면 :

디버그-> 새로운 중단 점-> 새로운 데이터 중단 점 ...

주소 : & (this-> m_value)
바이트 수 : 4 (int는 4 바이트이므로)

이제 프로그램을 재개 할 수 있습니다. 값이 변경되면 디버거가 중지됩니다.

상속 된 클래스 나 복합 클래스에서도 동일한 작업을 수행 할 수 있습니다.

class B
{
   private:
       A m_a;
};

주소 : & (this-> m_a.m_value)

If you don't know the number of bytes of the variable you want to inspect, you can use the sizeof operator.

For example:

// to know the size of the word processor,  
// if you want to inspect a pointer.
int wordTam = sizeof (void* ); 

If you look at the "Call stack" you can see the function that changed the value of the variable.


Change the variable into a property and add a breakpoint in the set method. Example:

private bool m_Var = false;
protected bool var
{
    get { 
        return m_var;
    }

    set { 
        m_var = value;
    }
}

If you are using WPF, there is an awesome tool : WPF Inspector.
It attaches itself to a WPF app and display the full tree of controls with the all properties, an it allows you (amongst other things) to break on any property change.

But sadly I didn't find any tool that would allow you to do the same with ANY property or variable.


I remember the way you described it using Visual Basic 6.0. In Visual Studio, the only way I have found so far is by specifying a breakpoint condition.


Right click on the breakpoint works fine for me (though mostly I am using it for conditional breakpoints on specific variable values. Even breaking on expressions involving a thread name works which is very useful if you're trying to spot threading issues).


As Peter Mortensen wrote:

In the Visual Studio 2005 menu:

Debug -> New Breakpoint -> New Data Breakpoint

Enter: &myVariable

Additional information:

Obviously, the system must know which address in memory to watch. So - set a normal breakpoint to the initialisation of myVariable (or myClass.m_Variable) - run the system and wait till it stops at that breakpoint. - Now the Menu entry is enabled, and you can watch the variable by entering &myVariable, or the instance by entering &myClass.m_Variable. Now the addresses are well defined.

Sorry when I did things wrong by explaining an already given solution. But I could not add a comment, and there has been some comments regarding this.


You can use a memory watchpoint in unmanaged code. Not sure if these are available in managed code though.


You can probably make a clever use of the DebugBreak() function.


You can optionally overload the = operator for the variable and can put the breakpoint inside the overloaded function on specific condition.


Update in 2019:

This is now officially supported in Visual Studio 2019 Preview 2 for .Net Core 3.0 or higher. Of course, you may have to put some thoughts in potential risks of using a Preview version of IDE. I imagine in the near future this will be included in the official Visual Studio.

https://blogs.msdn.microsoft.com/visualstudio/2019/02/12/break-when-value-changes-data-breakpoints-for-net-core-in-visual-studio-2019/

Fortunately, data breakpoints are no longer a C++ exclusive because they are now available for .NET Core (3.0 or higher) in Visual Studio 2019 Preview 2!

참고URL : https://stackoverflow.com/questions/160045/break-when-a-value-changes-using-the-visual-studio-debugger

반응형