Code Focused

Use Custom Visualizers for C++ Objects

Sometimes the default view of objects in the debugger isn't enough, especially if you operate on complex data structures. Here's one way to alter the view and use a customized one.

In my article, "How Helper Variables Can Simplify Debugging," I mentioned that it might be handy to add special debug variables for the objects in your code. We can go further with this idea. For example, in Visual Studio you can write custom visualizers for your objects.

Before Visual Studio 2012 you could customize the debugging view of your objects by editing autoexp.dat file. It was a bit clumsy to use, though. Both the Automatic expansion of structures and avoid stepping into particular functions were configured using this file. Since VS 2012 we got a new framework called Natvis that uses XML to describe rules for debugging views.

For a simple example, for our SimpleParam structure shown here:

class SimpleParam
{
public:
    SimpleParam(const string& str, int val);

    void Init();
    string GetStrVal() const;

private:
    string mStr;
    int mVal;
};

we get the view in the debugger shown in Figure 1.

New Debugger View
[Click on image for larger view.] Figure 1: New Debugger View

Not bad; we can easily see all the members. But with Natvis we can create a custom view of that.

In the code download accompanying this article is a file called NatvisFile.natvis. This is a configuration (stored per-project) that describes a custom visualizer. Here's what it looks like:

<AutoVisualizer xmlns = "...">
    <Type Name = "SimpleParam">
        <DisplayString>String: {mStr, sb}, Integer Value{ mVal }, StrLen{ strlen(&mStr[0]) }</DisplayString>
    <Expand>
        <Item Name = "String">mStr, sb< / Item>
        <Item Name = "Integer Value">mVal< / Item>
        </Expand>
    </Type>
</AutoVisualizer>

What it does is a simple alteration of the view, shown in Figure 2, that adds a custom description of the member fields and the string length information.

Debugger View Customized with More Info
[Click on image for larger view.] Figure 2: Debugger View Customized with More Info

Note that there's still the [Raw View] option that displays the default view.

You could do the following things with the framework:

  • work on types, templates
  • change names, select variables to be shown
  • use expressions on variables (but not with side effects), some predefined Debugger intrinsic functions are available (like strlen)
  • use conditional expressions to show more/less information
  • show arrays, or even multi-dimensional arrays, linked lists structures
  • add custom logic for traversing the structures
  • set rules per project, or per system user or global (by setting a file in global directories)

If the file is attached to a project, then the debugger will dynamically reload it after resaving (even while debugging!). You can also enable diagnostics to see problems in the code. Check out the full documentation linked in the reference list below for even more features of the framework.

 

About the Author

Bartlomiej Filipek is a software developer in Poland who specializes in C++, Windows and graphics programming. He worked for a number of companies developing multimedia, document editors, games, graphics drivers and flight planning systems, and has taught game development at local university. Follow Bart's blog at http://www.bfilipek.com and on Twitter @fenbf.

comments powered by Disqus

Featured

  • Microsoft Revamps Fledgling AutoGen Framework for Agentic AI

    Only at v0.4, Microsoft's AutoGen framework for agentic AI -- the hottest new trend in AI development -- has already undergone a complete revamp, going to an asynchronous, event-driven architecture.

  • IDE Irony: Coding Errors Cause 'Critical' Vulnerability in Visual Studio

    In a larger-than-normal Patch Tuesday, Microsoft warned of a "critical" vulnerability in Visual Studio that should be fixed immediately if automatic patching isn't enabled, ironically caused by coding errors.

  • Building Blazor Applications

    A trio of Blazor experts will conduct a full-day workshop for devs to learn everything about the tech a a March developer conference in Las Vegas keynoted by Microsoft execs and featuring many Microsoft devs.

  • Gradient Boosting Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the gradient boosting regression technique, where the goal is to predict a single numeric value. Compared to existing library implementations of gradient boosting regression, a from-scratch implementation allows much easier customization and integration with other .NET systems.

  • Microsoft Execs to Tackle AI and Cloud in Dev Conference Keynotes

    AI unsurprisingly is all over keynotes that Microsoft execs will helm to kick off the Visual Studio Live! developer conference in Las Vegas, March 10-14, which the company described as "a must-attend event."

Subscribe on YouTube