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

  • Full Stack Hands-On Development with .NET

    In the fast-paced realm of modern software development, proficiency across a full stack of technologies is not just beneficial, it's essential. Microsoft has an entire stack of open source development components in its .NET platform (formerly known as .NET Core) that can be used to build an end-to-end set of applications.

  • .NET-Centric Uno Platform Debuts 'Single Project' for 9 Targets

    "We've reduced the complexity of project files and eliminated the need for explicit NuGet package references, separate project libraries, or 'shared' projects."

  • Creating Reactive Applications in .NET

    In modern applications, data is being retrieved in asynchronous, real-time streams, as traditional pull requests where the clients asks for data from the server are becoming a thing of the past.

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

Subscribe on YouTube