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

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube