Visual Studio 11 visualizers for Boost.Geometry

Some time ago Barend Gehrels blogged about Visual Studio 2010 debugger visualizers for Boost.Geometry. Barend’s templates added to autoexp.dat file are of great help while working with the Boost.Geometry library in Visual Studio.

Recently, I’ve started using Visual Studio 11 Developer Preview and discovered the new style debugger visualizers this new edition introduces. I decided to port Barend’s work to the new XML-based format in .natvis files.

vs11-visualizers-boost-geometry

All my visualizers, not only for Boost.Geometry but also for other libraries, are available in visualstudio11 Git repository on my GitHub. The .natvis files are not overly complicated and the installation is dead easy, see README file attached.

Comments, suggestions and fixes are welcome.

Quick fix for CMake broken Visual Studio 11 .sln

Current CMake 2.8.6 generates broken .sln files for Visual Studio 11 Developer Preview. The bug has been fixed already in CMake upstream.

Meanwhile, I have become annoyed by manually fixing the solution files. Here is quick .bat script I call from my cmake-{project}.bat proxy scripts I maintain for every project configured with CMake. So the fix is applied automatically every time I run CMake:

@echo off
REM Fix broken Visual Studio 11 .sln generated by CMake 2.8.6 (or earlier)
set SED="C:\Program Files (x86)\GnuWin32\bin\sed.exe"
set SLN=%1
copy %SLN% %SLN%.copy
%SED% "s/Studio 2011/Studio 11/g" %SLN%.copy > %SLN%
del /Q %SLN%.copy

Modular visualizers in Visual Studio 11

Previously, I talked about the new debugger visualizers in Visual Studio 11 (Developer Preview) based on XML. By the way, I haven’t any luck trying to figure out where does the file type .NATVIS come from.

In previous versions of the Visual Studio, all templates of debugger visualizers have to be saved in single file: %VSINSTALLDIR%\Common7\Packages\Debugger\autoexp.dat. In case one wants to extend or improve the templates definition, maintaining everything in one system-wide file is neither practical nor safe.

The visualizers in Visual Studio 11 seem to solve the maintenance hassle by allowing more modular approach. First nice feature is that custom templates can be defined without touching any of the installed Visual Studio files and can be put in %USERPROFILE%\Documents\Visual Studio 11\Visualizers. Second nice feature I’ve just discovered is that definitions of visualizers can be split into several separate .natvis files. It is especially useful if one wants to maintain visualizers grouped according to libraries (e.g. Loki, Boost, POCO).

Basically, %USERPROFILE%\Documents\Visual Studio 11\Visualizers works like a package of custom visualizers now:

vs11-user-natvis

Having the visualizers diagnostics enabled, whole process of parsing and loading .natvis files will be traced in Visual Studio 11 output window:

vs11-modular-natvis

No more copying and pasting debugger visualizers for Boost and others into big muddy autoexp.dat.

BTW, who knows where does NATVIS come from, hmm? :-)

Setting EnableNatvisDiagnostics in Visual Studio 11

Previously, I posted about the new XML-style debugger visualizers I discovered in Visual Studio 11. I mentioned that one of reasons the feature has been redesigned is to enable debugging of the visualizers templates defined in default or user-defined .natvis files. So, it looks there is something more to discover: how to actually make use of the diagnostics capability.

Unfortunately, it seems the new debugger visualizers have not been documented anywhere. At the time of writing this blog, Google reports one link about defaultvis.natvis, it is the brief post on Visual C++ forum.

Luckily, there is a short manual included as comment in header of the %ProgramFiles%\Microsoft Visual Studio 11.0\Common7\Packages\Debugger\Visualizers\defaultvis.natvis file. It says:

1) give better diagnostic reporting.
a) Set the name-value entry under the Debugger registry key: EnableNatvisDiagnostics which is a REG_DWORD to a value of 1 this will output each expression string that is evaluated under the native visualizer into the output window and is good for debugging type definition typos in .natvis files.

The first thing is to figure out where to set the EnableNatvisDiagnostics flag. The Visual Studio 11 installer does not put it into the registry. Also, there are several Debugger keys. However, a few experiments proved the flag should be set under HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\11.0\Debugger:

vs11-visualizers-enable-natvis-diagnostics

Once it is set, every time Visual Studio 11 debugger is launched and use of visualized types is determined, the .natvis files are parsed and evaluated templates validated. The process is reported in the Output window:

vs11-evaluating-visualizer-1

If anything goes wrong, a type definition template is incorrect and its evaluation fails, it is also reported in the Output window:

vs11-evaluating-visualizer-2-validation

It will be a very useful feature especially when defining visualizers of complex types. Previous versions of Visual Studio used old format of templates defined in autoexp.dat file which stated its own DSL based on regular expressions, so the syntax was complex and fragile, and as Andy Pennel has confessed, not documented. (Here kudos to Avery Lee who did great job reverse-engineering the autoexp.dat syntax.)

At least, this time folks get the XML Schema for the Visual Studio debugger visualizers.

Debugger visualizers in Visual Sudio 11 Developer Preview

Visual Studio 11 Developer Preview introduces new style debugger visualizers. The autoexp.dat definition of templates in previous versions of the Visual Studio debugger is being replaced with new style definitions based on XML and stored in defaultvis.natvis file.

Microsoft explains the motivation behind redesigning the native type visualization mechanism is to give better diagnostic reporting (visualizer expressions can be now evaluated and displayed in the output window to help debugging .natvis file), provide XSD schema defining type definition, support of multiple files for type definition: system-wide in %ProgramFiles%\Microsoft Visual Studio 11.0\Common7\Packages\Debugger\Visualizers\defaultvis.natvis and user-defined in %USERPROFILE%\My Documents\Visual Studio Dev11\Visualizers\xxx.natvis.

It sounds really good and has a great usability potential for programmers who uses C++ generative programming capabilities, defines complex C++ types, heavily uses C++ Standard Library and Boost C++ Libraries.

Here is example of new style visualizer defined in XML copied from the defaultvis.natvis:

  <!--std::unique_ptr from <memory>-->
  <Type Name="std::unique_ptr<*>">
    <DisplayString Condition="_Myptr == 0">empty</DisplayString>
    <DisplayString>unique_ptr {*_Myptr}</DisplayString>
    <Expand>
      <Item Name="[ptr]" Condition="_Myptr != 0">_Myptr</Item>
    </Expand>
  </Type>

For most types from C++ Standard Library and Windows SDK, the new visualizers work very well. However, the currently available Visual Studio 11 Developer Preview (Version 11.0.40825.2 PREREL) is shipped with incorrect definitions in the defaultvis.natvis. The C++TR1 definitions introduced in Visual Studio 2008 are only forwarded (aliased) from std:tr1:: to std:: in Visual Studio 2010. In Visual Studio 11, they have completely moved from std::tr1:: to std:: to align with C++11 standard. But, the new visualizers in defaultvis.natvis define templates are still referring to the C++11 types in std::tr1:: namespace.

This prevents the correct visualization for types like std::shared_ptr:

vs11-visualizers-broken-2

There is an easy workaround possible:

  1. Make private copy of defaultvis.natvis in %USERPROFILE%\My Documents\Visual Studio Dev11\Visualizers\xxx.natvis where xxx is your preferred name.
  2. Open xxx.natvis in a text editor.
  3. Find std::tr1:: and replace with std::.
  4. Save the changes and relaunch the Visual Studio 11.
  5. You should see objects of the C++11 types correctly visualized.

vs11-visualizers-fixed-3

I have reported this issue to Microsoft Connect in Incorrect Type Name references in defaultvis.natvis visualizers for standard library ticket.

Building Boost with Visual Studio 11 Developer Preview

Yesterday, I installed Visual Studio 11 Developer Preview. The new release is available for free and is usable for free until June 2012, so it’s a great opportunity to try and learn what Microsoft has to offer for C++ programmers. And, it looks it offers a lot, including SCARY features.

The first test I usually do when it comes to test a new C++ toolset is to build Boost C++ Libraries using mainstream development sources.

The Boost simply builds well. I have even configured build of Boost.Python using Python version 3.2.

There are no hacks required to make the build happen. Simply, follow Boost Getting Started on Windows, section 5.3. Or, Build Binaries From Source. Once the Boost.Build tools bootstrapped, I issued the following command:

b2 --toolset=msvc variant=debug runtime-link=shared threading=multi define=_CRT_NONSTDC_NO_DEPRECATE define=_CRT_SECURE_NO_DEPRECATE define=_SCL_SECURE_NO_DEPRECATE stage

Fifteen minutes later, the Boost build was ready and staged.

By the way, some time ago Christian Henning posted a quick cheat sheet of bjam commands for Visual Studio. It’s very handy.