{"id":7031,"date":"2015-09-28T14:27:00","date_gmt":"2015-09-28T14:27:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2015\/09\/28\/debug-visualizers-in-visual-c-2015\/"},"modified":"2021-10-11T13:31:44","modified_gmt":"2021-10-11T13:31:44","slug":"debug-visualizers-in-visual-c-2015","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/debug-visualizers-in-visual-c-2015\/","title":{"rendered":"Debug Visualizers in Visual C++ 2015"},"content":{"rendered":"<p>When debugging your native applications, it is often useful to view the values of the objects in memory in a specific way, whether that be with custom string formatting, or even performing an operation on the data to make it more meaningful and easy to interpret.&nbsp; Since VS2012, Visual Studio had provided the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/jj620914.aspx\">.natvis visualizer format<\/a> for declaring custom visualizations for different C\/C++ types.&nbsp; &nbsp;Visualized types change the way objects are shown in the native expression evaluator which populates the watch and variable windows, as well as debug data tips. For additional information on the expression evaluator beyond what is needed for reading this post, consult <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/y2t7ahxk.aspx\">this documentation<\/a> and <a href=\"http:\/\/blogs.msdn.com\/b\/vcblog\/archive\/2013\/03\/07\/guest-post-the-expression-evaluator.aspx\">this blog post<\/a>.<\/p>\n<p><span style=\"font-size: medium\"><strong>Writing a Simple Visualizer<\/strong><\/span><\/p>\n<p>Let&rsquo;s start by creating a native for the following Volcano class:<\/p>\n<pre class=\"scroll\" style=\"padding-left: 30px\"><code class=\"cplusplus\">using namespace std;<br \/><br \/>class Volcano<br \/>{<br \/> private:<br \/>&nbsp;&nbsp;&nbsp;&nbsp; string m_EnglishName;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; string m_nativeName;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; string m_meaning;<a>Publish<\/a><br \/>&nbsp;&nbsp;&nbsp;&nbsp; int m_elevation; <br \/><br \/>\/\/...rest of class definition <br \/>}<\/code><\/pre>\n<p><strong>Natvis Item Template<\/strong><\/p>\n<p>Adding new .natvis files to a project is easy in VS2015 with the new built-in template which can be found under <em>Project-&gt;Add New Item<\/em>-&gt;<em>Visual C++-&gt;Utility-&gt;<\/em><em>Debugger visualization file (.natvis)<\/em>:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/0361.icon_.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/0361.icon_.png\" alt=\"Image 0361 icon\" width=\"365\" height=\"57\" class=\"aligncenter size-full wp-image-29525\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/0361.icon_.png 365w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/0361.icon_-300x47.png 300w\" sizes=\"(max-width: 365px) 100vw, 365px\" \/><\/a><\/p>\n<p>In order to make our Volcano objects easier to debug, we will make the <em>display string<\/em> equal to the English name since that is the most recognizable entity where referring to a volcano object.&nbsp; The we will create an <em>expand<\/em> view to display the contained members. &nbsp;To remove quotations on the first two strings we will apply the sb <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/75w45ekt.aspx\">format specifier<\/a> to both the native and English names, but will leave it off the meaning member since quotation marks will help differentiate the definition when debugging.&nbsp; Since our integer m_elevation is already clearly readably, we will simply list the variable without modification.<\/p>\n<p>Below is the simple .natvis visualizer for displaying our Volcano objects as desired in the C++ expression evaluator when debugging. &nbsp;Notice that the curly brace operator on the <span style=\"font-family: 'courier new', courier\">&lt;DisplayString&gt;<\/span> node which relays values from application being debugged.&nbsp; This allows for use of regular text in the output as well as debuggee values:<\/p>\n<pre class=\"scroll\" style=\"padding-left: 30px\"><code class=\"cplusplus\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;<br \/> &lt;AutoVisualizer xmlns=\"<a href=\"http:\/\/schemas.microsoft.com\/vstudio\/debugger\/natvis\/2010\">http:\/\/schemas.microsoft.com\/vstudio\/debugger\/natvis\/2010<\/a>\"&gt;<br \/> &nbsp; &lt;Type Name=\"Volcano\"&gt;<br \/> &nbsp;&nbsp;&nbsp; &lt;DisplayString&gt;Name: {m_EnglishName,sb}&lt;\/DisplayString&gt;<br \/> &nbsp;&nbsp;&nbsp; &lt;Expand&gt;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Item Name=\"Native name\"&gt;m_nativeName,sb&lt;\/Item&gt;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Item Name=\"Meaning\"&gt;m_meaning&lt;\/Item&gt;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Item Name=\"Elevation\"&gt;m_elevation&lt;\/Item&gt;<br \/> &nbsp;&nbsp;&nbsp; &lt;\/Expand&gt;<br \/> &nbsp; &lt;\/Type&gt;<br \/> &lt;\/AutoVisualizer&gt;<\/code><\/pre>\n<p>In order to make our Volcano objects easier to debug, we will make the <em>display string<\/em> equal to the English name since that is the most recognizable entity where referring to a volcano object. &nbsp;Notice that the curly brace operator on the DisplayString node which relays values from application being debugged, which allows for use of regular text in the output as well as debuggee values. &nbsp;Then we will create an <em>expand<\/em> view to display the contained members. &nbsp;To remove quotations on the first two strings we will apply the sb <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/75w45ekt.aspx\">format specifier<\/a> to both the native and English names, but will leave it off the meaning member since quotation marks will help differentiate the definition when debugging.&nbsp; Since our integer m_elevation is already clearly readable, we will simply list the variable without modification.<\/p>\n<p><strong>Visualized View of a Volcano Object<\/strong><\/p>\n<p>Once the .natvis file is integrated into the project, a Volcano object will be the visualized as shown in the watch window below.&nbsp; The [Raw View] node can be expanded to easily see the default view:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/2450.watch_.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/2450.watch_.png\" alt=\"Image 2450 watch\" width=\"719\" height=\"136\" class=\"aligncenter size-full wp-image-29526\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/2450.watch_.png 719w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/2450.watch_-300x57.png 300w\" sizes=\"(max-width: 719px) 100vw, 719px\" \/><\/a><\/p>\n<p>For more information on writing type visualizers inside .natvis files, check out the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/jj620914.aspx\">full documentation<\/a> of writing basic type visualizers.<\/p>\n<p><span style=\"font-size: medium\"><strong>New Visualization Features in VS2015<\/strong><\/span><\/p>\n<p><strong>Project Integration of Natvis Files<\/strong><\/p>\n<p>As shown with the above template and in an earlier <a href=\"http:\/\/blogs.msdn.com\/b\/vcblog\/archive\/2014\/06\/12\/project-support-for-natvis.aspx\">preview of VS2015<\/a>, .natvis files can now be added to projects or solutions and benefit from source control, as well as the ability to link into the PDB during a build for future consumption by the debugger. Besides individual projects, you can also add a .natvis file as a top-level solution item for <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/0bxe8ytt.aspx\">.exe projects<\/a>.&nbsp; Manually copying .natvis files to the special directories is longer necessary!<\/p>\n<p>If multiple valid entries are encountered for the same type, the first one in the following list will be used:<\/p>\n<ul>\n<li>PDB<\/li>\n<li>Project\/solution<\/li>\n<li>User directory: %USERPROFILE%\\My Documents\\Visual Studio 2015\\Visualizers<\/li>\n<li>Install directory: %VSINSTALLDIR%\\Common7\\Packages\\Debugger\\Visualizers<\/li>\n<\/ul>\n<div>\n<p><strong>Modify Visualizers While Debugging<\/strong><\/p>\n<p>The following animation shows to the top level display string for the object being changed the show the m_nativeName instead of the m_EnglishName.&nbsp; The changes to the .natvis file are immediately picked up by the debugger and the difference is shown in <span style=\"color: #ff0000\">red<\/span> text:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/5078.liveedit2.gif\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/5078.liveedit2.gif\" alt=\"Image 5078 liveedit2\" width=\"775\" height=\"366\" class=\"aligncenter size-full wp-image-29529\" \/><\/a><\/p>\n<p><strong>Improved Array and Vector Debugging<\/strong><\/p>\n<p>The [] element access operator is now supported in the expression evaluator, and can be used watch windows as well as in &lt;ArrayItems&gt; and &lt;IndexListItems&gt; elements of a .natvis file. Here is an example in the watch window:<\/p>\n<\/div>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/8345.watch2_.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/8345.watch2_.png\" alt=\"Image 8345 watch2\" width=\"362\" height=\"73\" class=\"aligncenter size-full wp-image-29531\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/8345.watch2_.png 362w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/8345.watch2_-300x60.png 300w\" sizes=\"(max-width: 362px) 100vw, 362px\" \/><\/a><\/p>\n<div>\n<p><strong>Hashtable visualization for CAtlMap<\/strong><\/p>\n<p>Members of CATLMap data structures can now be expanded to visualize key and value pairs.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/5584.catlmap.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/5584.catlmap.png\" alt=\"Image 5584 catlmap\" width=\"643\" height=\"182\" class=\"aligncenter size-full wp-image-29530\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/5584.catlmap.png 643w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/5584.catlmap-300x85.png 300w\" sizes=\"(max-width: 643px) 100vw, 643px\" \/><\/a>\n<\/div>\n<p><strong>Natvis Diagnostic Improvements<\/strong><\/p>\n<p>Diagnosing problems in your custom .natvis files is now much easier.&nbsp; The old <em>EnableNatvisDiagnostics<\/em> registry switch is no longer used.&nbsp; Instead, you can now enable natvis diagnostics in <em>Tools-&gt;<\/em><em>Options-&gt;<\/em><em>Debugging-&gt;<\/em><em>Ouput Window<\/em> and select one of the options: &ldquo;Off,&rdquo; &ldquo;Error&rdquo;, &ldquo;Warning&rdquo;, and &ldquo;Verbose&rdquo;, which will provide additional information concerning the parse failures of the XML:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/3858.diag_.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/3858.diag_.png\" alt=\"Image 3858 diag\" width=\"730\" height=\"347\" class=\"aligncenter size-full wp-image-29528\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/3858.diag_.png 730w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/3858.diag_-300x143.png 300w\" sizes=\"(max-width: 730px) 100vw, 730px\" \/><\/a><\/p>\n<p><span style=\"font-size: medium\"><strong>New Natvis Attributes<\/strong><\/span><\/p>\n<p><strong>&nbsp;&ldquo;Optional&rdquo; Attribute<\/strong><\/p>\n<ul>\n<li>Any node can have the &ldquo;Optional&rdquo; attribute.<\/li>\n<li>If any sub-expression inside of an optional node fails to parse, only that node will be thrown out.&nbsp; The rest of the &lt;Type&gt; entry is still valid.&nbsp; Previously the entire visualizer would fail for the type.<\/li>\n<li>For example, you may have a member that only exists when in debug configuration, and this feature allows you to provide a single .natvis entry for the type that works across configurations:<\/li>\n<\/ul>\n<pre class=\"scroll\" style=\"padding-left: 30px\"><code class=\"cplusplus\">struct Foo<br \/>{<br \/> &nbsp;&nbsp;&nbsp;&nbsp;int i;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;int j;<br \/>#if _DEBUG<br \/> &nbsp;&nbsp;&nbsp; std::string debugMessage;<br \/>#endif<br \/> };<\/code><\/pre>\n<p><strong>&nbsp;&ldquo;Inheritable&rdquo; Attribute on &lt;Type&gt;<\/strong><\/p>\n<ul>\n<li>In VS2013, every natvis entry would apply to not only the type specified, but all derived types.<\/li>\n<li>This works most of the time, but based on feedback, it is sometimes undesirable. (For example: you may want to see the raw members of the derived object without the natvis for the base object masking it)<\/li>\n<li>In VS2015, the &ldquo;Inheritable&rdquo; attribute allows you to control this behavior.&nbsp; Default is &ldquo;true&rdquo;.&nbsp; If false, natvis entry applies to base class only.<\/li>\n<\/ul>\n<p><strong>&nbsp;&ldquo;Priority&rdquo; Attribute<\/strong><\/p>\n<ul>\n<li>&ldquo;Low&rdquo;, &ldquo;MediumLow&rdquo;, &ldquo;Medium&rdquo;, &ldquo;MediumHigh&rdquo; &ldquo;High&rdquo; , default is &ldquo;Medium&rdquo;<\/li>\n<li>Where the file comes from (project, user dir, install dir) trumps priority attribute<\/li>\n<li>An example: std::vector for VS2013 declares a &ldquo;MediumLow&rdquo; priority so that the VS2015 version is used by default, even though both are still defined in the visualizer file to maintain legacy debugging support.&nbsp; If you wanted to utilize the legacy visualizer when targeting the older v120 toolset, switching the VC2013 implementation to &ldquo;Medium High&rdquo; or &ldquo;High&rdquo; would enable this to override the default priority of medium for the v140 implementation.<\/li>\n<\/ul>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/2664.stl_.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/2664.stl_.png\" alt=\"Image 2664 stl\" width=\"791\" height=\"388\" class=\"aligncenter size-full wp-image-29527\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/2664.stl_.png 791w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/2664.stl_-300x147.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2015\/09\/2664.stl_-768x377.png 768w\" sizes=\"(max-width: 791px) 100vw, 791px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: medium\"><strong>Android Debugging<\/strong><\/span><\/p>\n<p>VS2015 introduced support to develop and debug C++ Android applications, including <a href=\"http:\/\/blogs.msdn.com\/b\/vcblog\/archive\/2015\/04\/29\/natvis-support-for-android-debugging.aspx\">basic .natvis support for many commonly used stl containers<\/a>.<\/p>\n<p><span style=\"font-size: medium\"><strong>Other Natvis Improvements<\/strong><\/span><\/p>\n<ul>\n<li>&lt;IncludeView&gt; and &lt;ExcludeView&gt; can now be used at the &lt;Type&gt; level, rather than only on individual elements<\/li>\n<li>&lt;CustomVisualizer&gt; nodes can now specify &ldquo;Condition&rdquo;, &ldquo;IncludeView&rdquo;, or &ldquo;ExcludeView&rdquo; attributes<\/li>\n<li>Natvis now works on function return values in autos window<\/li>\n<li>Natvis now works on structs that are optimized into a register<\/li>\n<li>Improved robustness when natvis entries recursively reference other natvis entries<\/li>\n<\/ul>\n<p>Future blog posts will cover more advanced debug visualizer options that are available inside Visual Studio.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When debugging your native applications, it is often useful to view the values of the objects in memory in a specific way, whether that be with custom string formatting, or even performing an operation on the data to make it more meaningful and easy to interpret.&nbsp; Since VS2012, Visual Studio had provided the .natvis visualizer [&hellip;]<\/p>\n","protected":false},"author":295,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[100,55],"class_list":["post-7031","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus","tag-c-language","tag-debugging"],"acf":[],"blog_post_summary":"<p>When debugging your native applications, it is often useful to view the values of the objects in memory in a specific way, whether that be with custom string formatting, or even performing an operation on the data to make it more meaningful and easy to interpret.&nbsp; Since VS2012, Visual Studio had provided the .natvis visualizer [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/7031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/295"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=7031"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/7031\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=7031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=7031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=7031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}