Search code examples
visual-studiovisual-studio-2013

Saving values of debug session from watch window to a file


I have to trace some events I want to trace to monitor the operation of a multi-threaded application. For this purpose I defined an array of structs. Each element is one trace record.

enum Event { start, stop, pause };
struct A
{
    Event e;
    int x, y, z;
};

main()
{
  A a[100];
}

There is also a function the writes the event to the array. The array a can be displayed in the watch window of Visual Studio, although not all struct members are displayed:

-       a               0x008ff4bc {{e=0xcccccccc x=0xcccccccc y=0xcccccccc ...}, {e=0xcccccccc x=0xcccccccc y=0xcccccccc ...}, ...}    A[0x00000064]
+       [0x00000000]    {e=0xcccccccc x=0xcccccccc y=0xcccccccc ...}    A
+       [0x00000001]    {e=0xcccccccc x=0xcccccccc y=0xcccccccc ...}    A
+       [0x00000002]    {e=0xcccccccc x=0xcccccccc y=0xcccccccc ...}    A

Since the array is rather large and the limitation of the watch I want to export the entire array content to a file. This could be done by adding a file export function to the code that is being debugged. But this is no handy, since the debugger is just in a breakpoint and it's not always possible to tell the application to run the export function.

How can I export the array with the values of all members? Is there an option to use the VS command window or probably immediate window to create a text file with the data?


Solution

  • Does it have to be a one-click export? You can select all, copy and paste into your output file. You can do it from Watch window, but the output is a bit more clear from either Immediate or Command windows.

    You can beautify the format by creating a custom visualizer in C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Packages\Debugger\Visualizers (use your version of the VS).

    Here is how it may look, for example:

    <?xml version="1.0" encoding="utf-8"?>
    <AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
      <Type Name="A">
        <DisplayString>({e}:{x},{y},{z})</DisplayString>
      </Type>
    </AutoVisualizer>