Share via


Visual Studio 2012 and Debugger .NATVIS files – what can I do with them?

In Visual Studio 2012 , Visual Studio native debugger introduces a new type visualization framework that allows customizing the way Visual Studio displays native types in debugger variable windows (watch, locals, data tips etc.). It supersedes the autoexp.dat file that has been used in earlier versions of Visual Studio and offers xml syntax, better diagnostics, versioning and multiple file support. Component authors can use this framework to create visualization rules for their types making it easy for developers to inspect them during debugging.

Visualizers for native types are specified in .natvis files. A natvis file is simply an xml file (with a .natvis extension) with its schema defined in <VSINSTALLDIR>\Xml\Schemas\natvis.xsd. Visual Studio ships with a few natvis files in <VSINSTALLDIR>\Common7\Packages\Debugger\Visualizers folder. These files contain visualization rules for many common types and can serve as examples when writing visualizers for new types.

Not trying to provide all the details about .natvis files (as it is a topic for entire separate blog/article), I will rather try to demonstrate briefly the flexibility and power of the framework that can be used beyond simplistic visualization of data.

Let’s pretend that you have your own data type:

image

When you look at variables of this type in debugger Watch, Locals or Auto window, you see the expected data:

image

Now copy the below XML snippet into the file with extension .natvis, and place it in %USERPROFILE%\My Documents\Visual Studio 11\Visualizers\. Besides many different ways to display your types in friendly ways, you can create synthetic expansions:

mytypes.natvis:

<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="https://schemas.microsoft.com/vstudio/debugger/natvis/2010">
<Type Name="Rectangle">
<Expand>
<Synthetic Name="Am I a square?"
Condition="height==width">
<DisplayString>Yes I am.
</DisplayString>
</Synthetic>
<Synthetic Name="Am I a square?"
Condition="height!=width">
<DisplayString>No I am not.
</DisplayString>
</Synthetic>
</Expand>
</Type>
</AutoVisualizer>

And now your visualization is richer and allows you to see not only raw data of your objects, but also some post-processing of it (which can be very handy, especially if you are dealing with complicated data types that require some non-trivial evaluation).

image

Enjoy!

Update: more data on this subject is available here: https://code.msdn.microsoft.com/Writing-type-visualizers-2eae77a2

Comments

  • Anonymous
    June 20, 2012
    What happened to defaultvis.natvis? It is not in my C:Program Files (x86)Microsoft Visual Studio 11.0Common7PackagesDebuggerVisualizers directory, although others, such as windows.natvis, are. Is there any documentation on how to write a visualizer for unmanaged C++ code?

  • Anonymous
    June 27, 2012
    Defaultvis.natvis is replaced by others (windows.natvis, stl.natvis etc.). The documentation is yet to come.

  • Anonymous
    July 26, 2012
    Is there any chance to get this working when debugging in mixed-mode (debugger is attached to .NET and Native debugging)? My .natvis file works quite well when debugging native only, but I also need to see on what's going on in the managed part (we use some P/Invoke calls out of a C# web service).

  • Anonymous
    August 02, 2012
    Thomas - no, it is currently not supported.

  • Anonymous
    April 28, 2014
    Having .natvis files working in mixed-mode would be great. The old visualizers in VS 2008 did work in mixed mode - not supporting .natvis files in mixed-mode is a degrade of the debugging experience.

  • Anonymous
    May 12, 2014
    Does this feature provide a way how to correctly display UTF-32 strings in the debugger?

  • Anonymous
    July 23, 2015
    The comment has been removed