Visual Studio. Debug. How to save to a file all th

2019-01-26 00:07发布

I am watching a variable that gets assigned thousands of values during a single run. It is being set several times and is hard to keep track of the code workflow just by setting breakpoints where it is being reassigned. Is there a way to output all the values the variable had to a .txt file? I want to achive this without modifying the actual C++ code, just using the debugging tools.

1条回答
甜甜的少女心
2楼-- · 2019-01-26 00:20

To get really close to what you're looking for, you need to combine two features of Visual Studio: Data breakpoints and trace points. Data breakpoints allow you to notify the debugger, whenever the store value in memory changes. To set a data breakpoint, launch the debugger, and select DebugNew BreakpointData Breakpoint...:

New Data Breakpoint

Since we are interested in the variable myValue, we're setting the address to &myValue, and set the size to 4 bytes (since that's what an int is on Windows):

Data Breakpoint Settings

Now that would be pretty cool as-is already. But for non-intrusive logging, we'll set the Actions as well:

Data Breakpoint Actions

The important settings are the log message ([MYSIG] is an arbitrary signature, so that the interesting log entries can later be filtered), that dumps the value of the variable, as well as the Continue execution check box. The latter makes this non-intrusive (or low-intrusive; logging lots of information does have a noticeable impact on runtime performance).

With that in place, running this code

inline void DataTracepoint() {
    volatile int myValue{ 0 };
    for ( int i = 0; i < 10; ++i ) {
        myValue = i;
    }
}

produces the following output in the Debug output pane:

[MYSIG] myValue = 0
[MYSIG] myValue = 1
[MYSIG] myValue = 2
[MYSIG] myValue = 3
[MYSIG] myValue = 4
[MYSIG] myValue = 5
[MYSIG] myValue = 6
[MYSIG] myValue = 7
[MYSIG] myValue = 8
[MYSIG] myValue = 9

This output can then be easily analyzed in your favorite text processor.

查看更多
登录 后发表回答